# Contributing

Contributions are welcome: bug reports, fixes, new features, extensions, and corrections to this manual. This page
describes how to report a problem, how changes are made and what a pull request should contain.

Marginalia is released under the [MIT License](https://github.com/Enerccio/Marginalia/blob/master/LICENSE). By
contributing you agree that your contribution is released under the same license.

## Reporting bugs and ideas

Open an issue on [GitHub](https://github.com/Enerccio/Marginalia/issues). For a bug, include:

- what you did, what happened and what you expected;
- the version (desktop app archive name, or the commit for a Docker server) and how you run it;
- the inference provider type and model, if the problem is about generation (never your API key);
- the relevant part of the log - see [Where are the logs?](../user/troubleshooting.md#where-are-the-logs). Logs may
  contain story text; remove what you don't want to share.

Security problems (access to other users' data, login, cookies) should not be reported in a public issue - contact the
maintainer privately through GitHub first.

### Planned work

[`TODO.md`](https://github.com/Enerccio/Marginalia/blob/master/TODO.md) is the list of planned work for the first
release, ordered by priority. Items marked 🧩 are good candidates for an extension instead of a change to the core.

## Before you start

- For anything larger than a small fix, open an issue (or comment on an existing one) first and describe what you
  plan. Some features belong in an extension rather than in the core - see [Plugin development](plugins/index.md).
- Read [Architecture](architecture.md) and the page about the area you are changing.
- Set up the build and make sure `mvn test` passes on a clean checkout - see [Building from source](building.md).

## Workflow

1. Fork the repository and create a branch from `master` with a short descriptive name (`fix-summary-prompt`,
   `lorebook-tag-filter`).
2. Make the change, with tests where the area is testable (see [Testing](testing.md)).
3. Run `mvn test` in `marginalia/`. If you touched a plugin or classes plugins use, build the plugins too.
4. Start Marginalia and try the change in the browser - most of the application is UI, which the tests don't cover.
5. Update the manual if users or developers will notice the change (see [Documentation](#documentation)).
6. Push and open a pull request against `master`.

CI runs the tests and builds the desktop app on all platforms for every pull request that changes `marginalia/`
(see [Continuous integration](packaging.md#continuous-integration)). A pull request is merged when CI is green and
the change has been reviewed.

## Commits

Commit messages follow the style already in the history: one line, **lowercase**, starting with a verb in the
imperative, class and method names in backticks, related changes separated by a semicolon:

```
add `tagMultiComboBox` to `LorebookView`; enable tagging functionality and integration with current lorebook
throw `IllegalArgumentException` for invalid lorebook format in `LorebookServiceImpl`
fix `setSummaryPrompt` assignment error; correct `userPrompt` reference in manuscript save logic
```

Common verbs: `add`, `fix`, `update`, `remove`, `refactor`, `rename`, `migrate`, `handle`. Mention the issue number
when the commit fixes one (`... (#17)`).

Keep commits focused - one fix or one step of a feature per commit, not a mix of a feature, a reformat and an
unrelated fix. Don't commit generated or local files: `target/`, `src/main/frontend/generated/`,
`src/main/bundles/`, `flow-build-info.json` in `src/main/resources` and `.idea/` are ignored by Git for this reason.

## Pull requests

A good pull request:

- does one thing, and says in the description **what** changed and **why**, with the issue it fixes;
- for a UI change, includes a screenshot or a short description of what to click to see it;
- adds or updates tests where it can, and passes `mvn test`;
- adds a Flyway migration for every entity change (see [Database & migrations](database.md)) - never edit a
  migration that is already in `master`;
- updates the manual;
- doesn't change formatting of code it doesn't otherwise touch.

## Code style

There is no formatter configuration in the repository; match the code around your change. In short:

### Java

- Java 25. Modern language features (records, `switch` expressions, pattern matching, text blocks, `var` where the
  type is obvious) are welcome.
- 4 spaces, no tabs; opening braces on the same line; lines up to about 120 characters.
- Javadoc on classes and on public methods whose purpose isn't obvious from the name. Write *why* and the contract
  (what `null` means, which thread, which transaction), not a restatement of the signature.
- Logging through SLF4J (`private static final Logger log = LoggerFactory.getLogger(X.class)`), with `{}`
  placeholders. Story content, prompts and lore are logged at `DEBUG` only; API keys and passwords never. Plugins
  don't use `LoggerFactory`: they take the logger from `OsgiService.createLogger(bundle)` so their lines reach the
  extension log ([Logging](plugins/overview.md#logging)).
- Return `null` for "not found" where the existing service API does (`find`, `findForUser`), and keep that consistent
  within a class.

### Naming

- Entities keep their historical names: a book is `Manuscript`, a part is `ChatMessage`, an inference provider is
  `AI`. Use the user-facing words (*book*, *part*, *inference provider*) in UI text, the manual and log messages.
- Services are an interface in `domain/service` and an implementation `<Name>Impl` in `domain/service/impl`,
  registered in the Spring XML (see [Services](services.md#writing-a-new-service)).
- UI classes: a tab of the book window is `Manuscript<Name>Part`, a dialog `<Name>Dialog`, a reusable component in
  `ui/components` or `ui/widgets`.

### Architecture rules

- **Transactions** live on services (`@CommonTx`, `@CommonTxReadOnly`), never on UI classes. Entities held by the UI
  are detached; reload them through the service before changing them.
- **The current user** comes from the session-scoped `User` bean; services check ownership (`findForUser`) - the UI
  must not be the only thing protecting other users' data.
- **UI text** is never hard-coded: add a key to `loc/M_L.java` (a text of the application) or `loc/L.java` (a text
  of the platform) and the text to its English bundle,
  `loc/MarginaliaLocalizationBundleEN.java` or `loc/LocalizationBundleEN.java` (and to the `JP`, `FR`, `ES`, `DE`,
  `KO` and `IT` ones of the same enum and the Chinese plugin's `MarginaliaLocalizationBundleZH.java` /
  `LocalizationBundleZH.java`; a language without the key shows the English text), and
  use `loc.getValue(M_L.KEY)` (see [User interface](ui.md#text)).
- **Plugin UI text** goes through the `PluginLocalizationBundle` of the plugin: the English text is a `public static final String` constant
  of the bundle class, named after its words, never a literal at the call site (`T.localized(EXPORT_INSTRUCTIONS)`); the Japanese, Chinese, French, Spanish, German, Korean and Italian
  texts are `ja(...)` / `zh(...)` / `fr(...)` / `es(...)` / `de(...)` / `ko(...)` / `it(...)` calls in `loadKeys()` that take the constant as the key (see
  [Texts in several languages](plugins/ui-extensions.md#texts-in-several-languages)).
- **Dialogs that take input** put the cursor into the first field when they open (`DialogFocus.onOpen`) and send from the keyboard with
  `DialogShortcuts.submitWith(mainButton, fields...)`: Enter in a single line field, Ctrl/Cmd+Enter in a text area (Enter there is a new
  line); a prompt dialog (a call to the model) always gets the Ctrl+Enter send. List every input field of the dialog; bulk dialogs and read
  only text areas are left out. Never write your own `addKeyDownListener(Key.ENTER)` (see [User interface](ui.md)).
- **Threads**: long work runs off the UI thread, and UI updates go through `ui.access(...)` (see
  [Background work and push](ui.md#background-work-and-push)).
- **Extension points**: methods that extensions decorate are `@Extendable`, and each of them declares its signature
  with `@PublicAPI` (`PublicAPISignatureTest` fails when a method has none or the annotation is out of date). Renaming
  or removing such a method, its parameters or its local variables breaks plugins - update the annotation, check the
  bundled plugins and mention the change in the pull request (see
  [The public API](plugins/extendable.md#the-public-api) and
  [Keeping hooks working](plugins/extendable.md#keeping-hooks-working)).
- **Schema**: every entity change comes with a migration; Hibernate only validates the schema.

### Tests

Use JUnit Jupiter and AssertJ, the [test bases](testing.md#test-bases) and the fake LLM server; no real network calls.
See the [checklist for a new test](testing.md#checklist-for-a-new-test).

To try a **prompt on a real model** (a reasoning model needs a big output budget: its thinking counts against `max_tokens`), `tools/LocalLlm.java` (one file, JDK and `sqlite3` only,
no build) sends one chat completion to the AI set up in your dev database `~/.marginalia/marginalia.sqlite`, reading the key from there and never printing it:
`java tools/LocalLlm.java --list`, then for example `java tools/LocalLlm.java --system-file sys.txt --user-file scene.txt --samples 3`. The other options (`--json-schema`,
`--reasoning` to show the thinking, `--extra` for more raw JSON members of the request) are listed at the top of the file. Say "3 of 3 samples", not rates, and write down the model and the token numbers with the result.

`python3 tools/plugin_api_diff.py <tag> [<tag>] [--changelog]` prints the `@PublicAPI` methods and fields that changed between two git revisions, for the plugin-visible part of the CHANGELOG (see [@Extendable hooks](plugins/extendable.md#the-public-api)).

`python3 tools/marginalia_api.py` is a command line client of the [HTTP API](api.md) (standard library only; the server and the key come
from `MARGINALIA_URL` and `MARGINALIA_API_KEY_FILE`): the version of a server, whether plugins can be installed without an update,
installing them. `tools/version_from_tag.sh` sets every POM to the version of a `v*` tag in the release builds ([Packaging](packaging.md#making-a-release)).

`tools/update-watcher` is the host side of *Admin → Update* of a Docker installation: `watch.sh` runs `bin/rebuild.sh` when the application asks for it, `install.sh` makes it systemd units (see [Packaging](packaging.md)).

`tools/gpu-control` is a small controller (Python standard library, `gpu_control.py`, with its own README, compose file and tests) that starts and stops the containers that take turns on one GPU (a speech server, ComfyUI), behind a Docker socket proxy, and unloads the models of a `llama-server` router except the ones a caller keeps (`POST /llama/unload`); the acquire and release steps of a [GPU lock](plugins/gpu-locks.md) call it (through LiteLLM pass-throughs), see [Setting up the speech server](../user/extensions/audiobook-server.md). `tools/workset_run.sh` runs the dataset harness with the plugins on the classpath ([The dataset book](testing.md#the-dataset-book)).

## Documentation

The manual you are reading is in `manual/` (Markdown for [Retype](https://retype.com)); `docs/` is the generated site, which is not in the repository.

- Change the Markdown in `manual/`, then run `tools/retype_build.sh` (`retype build` with the project version and commit in the site, for the English site and for each language that has translations) to look at the result in `docs/` (ignored by git). The site is published by the `Manual` workflow when the change reaches `master`, see
  [Publishing the manual](packaging.md#publishing-the-manual).
  The user manual can be translated with `tools/manual_i18n.py` (only the pages whose English text changed are sent again, and the code, the link targets and the tags are not sent at all, they are put back in the answer; `verify` checks that), see [Translating the manual](translating-the-manual.md).
- Preview with `retype start`.
- Write for the reader of that part: the [User guide](../user/index.md) talks about what users see and click (UI
  names in *italics*, no class names), the developer guide about code (paths relative to the main package, class
  names in backticks).
- Screenshots go to `manual/images/`. When a page needs a screenshot you can't make, link the path anyway and add a
  line to [`TODO.IMAGES.md`](https://github.com/Enerccio/Marginalia/blob/master/TODO.IMAGES.md) describing what it
  should show. Diagrams are Mermaid code blocks, not images.
- The screenshots are retaken with the scripted environment in the `screenshots/` folder (its own data folder, a fake
  model server and headless Chrome, so no API key and no personal data), described in
  [`screenshots/SCREENSHOTS.md`](https://github.com/Enerccio/Marginalia/blob/master/screenshots/SCREENSHOTS.md): all
  pictures show the same dataset, and a single picture can be retaken from the step backup it belongs to.
- A screenshot that shows a third-party picture needs a credit on the [Credits](../credits.md) page (author, source,
  license, changes), and must be shared under the license of the picture.
- The class labels on `dev-ui-map-book.png` and `dev-ui-map-workspace.png` are drawn by `tools/annotate_ui_maps.py`
  (needs Pillow) onto a plain screenshot saved as `dev-ui-map-<name>-not-annotated.png` next to it. After the screen
  changes, take a new plain screenshot, adjust the coordinates at the top of the script if something moved, and run it.
- Every developer page has `verified` (the commit it was last checked against the code) and `covers` (the code it
  describes) in its front matter. `python3 tools/check_dev_docs.py` lists the pages whose covered code changed since
  then (`-v` names modified files, `--check` fails when any page is behind). After you review a page, mark it with
  `--stamp <page.md>` in a commit of its own. When a page starts describing other code, extend its `covers`.
- Bugs you find while writing documentation are reported as an issue (see [Reporting bugs and ideas](#reporting-bugs-and-ideas)).

## Extensions

New features that not every user needs - and anything experimental - are often better as an extension than as a
change to the core. An extension can be developed and released on its own, and doesn't need a pull request at all. If
you want it bundled with Marginalia in `marginalia/plugins/`, open an issue first. See
[Plugin development](plugins/index.md) and the [example plugin](plugins/example.md).
