diff --git a/.gitattributes b/.gitattributes index 6df6a37..cc78a1f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,3 +2,7 @@ prompts/system.md text eol=lf # Keep bundled skill source digests stable across Windows and Linux checkouts. skills/**/SKILL.md text eol=lf + +# Check the desktop frontend out with LF on every platform. Biome's formatter +# check expects LF, and a CRLF checkout on Windows would fail it. +crates/omgcode-desktop/ui/** text=auto eol=lf diff --git a/.github/workflows/frontend.yml b/.github/workflows/frontend.yml new file mode 100644 index 0000000..6e96253 --- /dev/null +++ b/.github/workflows/frontend.yml @@ -0,0 +1,47 @@ +name: Frontend + +on: + pull_request: + branches: [main] + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: frontend-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + check: + name: Desktop frontend (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04, windows-2025] + runs-on: ${{ matrix.os }} + timeout-minutes: 15 + defaults: + run: + working-directory: crates/omgcode-desktop/ui + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + - name: Install the locked dependencies + # The flag repeats what .npmrc sets, so no package install script runs + # even if that file changes. + run: npm ci --ignore-scripts + - name: Check types + run: npm run check + - name: Check formatting and lints + run: npm run lint + - name: Run tests + run: npm test + - name: Build and check the bundles + run: npm run test:dist diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 3b8c694..9092db6 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -22,7 +22,7 @@ jobs: matrix: os: [ubuntu-24.04, windows-2025] runs-on: ${{ matrix.os }} - timeout-minutes: 20 + timeout-minutes: 40 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -31,6 +31,25 @@ jobs: run: rustup toolchain install 1.99.1 --profile minimal --component rustfmt --component clippy - name: Check formatting run: cargo fmt --all -- --check + - name: Install the desktop shell's Linux build packages + if: runner.os == 'Linux' + run: | + # The Tauri prerequisites for Debian and Ubuntu. They are needed to + # compile the desktop crate; no test here starts a webview. + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + libwebkit2gtk-4.1-dev build-essential curl wget file libxdo-dev \ + libssl-dev libayatana-appindicator3-dev librsvg2-dev + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + - name: Build the desktop frontend + # The desktop crate embeds ui/dist at compile time, so the frontend + # must be built before any workspace-wide Cargo step. + working-directory: crates/omgcode-desktop/ui + run: | + npm ci --ignore-scripts + npm run build - name: Check lints and compilation run: cargo clippy --workspace --all-targets --locked -- -D warnings - name: Start a user service manager for delegated cgroups diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 568bf9d..996912b 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -15,8 +15,39 @@ concurrency: cancel-in-progress: true jobs: + # The frontend tests run dependency code, so they run in a job that is never + # given the Sonar credential. The analysis job receives only the report. + frontend-coverage: + name: Frontend coverage + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-24.04 + timeout-minutes: 15 + defaults: + run: + working-directory: crates/omgcode-desktop/ui + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + - name: Install the locked dependencies + # The flag repeats what .npmrc sets, so no package install script runs + # even if that file changes. + run: npm ci --ignore-scripts + - name: Run the tests with coverage + run: npm run coverage + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: frontend-coverage + path: crates/omgcode-desktop/ui/coverage/lcov.info + if-no-files-found: error + retention-days: 1 + analyze: name: Sonar analysis + needs: frontend-coverage if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-24.04 timeout-minutes: 15 @@ -25,6 +56,11 @@ jobs: with: fetch-depth: 0 persist-credentials: false + - name: Fetch the frontend coverage report + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: frontend-coverage + path: crates/omgcode-desktop/ui/coverage - name: Require the analysis credential env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.gitignore b/.gitignore index 56de40e..42057a5 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,8 @@ rustc-ice-*.txt # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# Desktop UI (Node) +node_modules/ +dist/ +coverage/ diff --git a/AGENTS.md b/AGENTS.md index 917a6cd..090dd27 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,7 +7,11 @@ repository contains a pinned Rust workspace with configuration-only workspace preferences, a bounded native file broker exposed through trusted CLI commands, and native identity checks. Follow [workspace settings](docs/workspace-permissions.md) and [workspace file access](docs/file-access.md) for their contracts. Enabled -preferences never authorize execution. The prototype is not a desktop app. +preferences never authorize execution. A prototype Tauri desktop shell adds a +session tree, full-text search, chat, and an isolated approvals window. It is +unpackaged, has run only on Windows, and gives the webview no authority: the +Rust side authorizes every command and the trusted CLI remains. Follow the +[desktop interface](docs/desktop-interface.md) contract. Chat offers the local LM Studio model read and list tools; when the workspace also enables `read` and `write`, one exact-edit tool; and, when it also enables `execute`, `read`, and `write`, one command tool. The edit and command tools @@ -30,19 +34,36 @@ no run uses them. The trusted `packages fetch` command downloads from declared origins into a per-workspace cache. A selected root is exposed read-only on Linux and Windows; Windows needs a read-execute container grant, and Linux falls back to application storage. Provider profiles record connection shapes -without credentials or adapters; no adapter or login uses them. Trusted CLI -commands can store API keys in protected OS storage (DPAPI on Windows, the -Secret Service on Linux) and bind them to `api_key` profiles; no adapter reads -them yet. Trusted +without credentials. Trusted CLI commands can store API keys in protected OS +storage (DPAPI on Windows, the Secret Service on Linux) and bind them to +`api_key` profiles. An `lm_studio` profile can record a loopback endpoint. +Only the local LM Studio adapter reads a credential, through an +operator-selected profile that supplies both the endpoint and the token +(ADR-0047, ADR-0048); no cloud adapter or login exists. Trusted single-use approval records authorize one exact model-tool call: only a later -identical call may consume an approved record. `workspace_run_command` then +identical call may consume an approved record. The trusted CLI or the desktop +approvals window can decide a record; the window shows a request only when core +reproduces its digest, and approval needs a single-use review token. +`workspace_run_command` then invokes the trusted runner, and `workspace_edit_file` then calls the checkpointed apply with the size and digest of a live trusted read ([ADR-0043](docs/adr/0043-gate-the-model-file-edit-tool-with-approvals-and-checkpointed-apply.md)). Neither route detects a concurrent writer. Trusted CLI commands record, apply, and revoke Windows container ACL grants. Follow the [execution contract](docs/execution.md). -The bundled web interface and most planned features are not implemented. +Sessions are indexed in a derived SQLite index that the journals can rebuild. +Chat can stream the local model's reply (`chat --stream` on the +CLI; on by default in the desktop shell, `--stream off` at launch). Streamed +text is provisional, and the journals record only the validated reply. The +shell pushes chat events to the main window through one caller-scoped channel +that needs no `core:` permission ([ADR-0049](docs/adr/0049-stream-local-model-replies-and-push-chat-events.md)). +A cancel request interrupts a streamed model request and the turn is recorded as +cancelled; a non-streamed request, the compaction request, and a tool call in +progress are not interruptible +([ADR-0050](docs/adr/0050-interrupt-a-streamed-model-request-on-cancellation.md)). +The desktop shell has no settings interface, development panels, packaging, or +recovery interface. Most other planned features are not +implemented. The core validates replacement records and can prepare protected checkpoints on Linux and Windows through a live authorized binding. The CLI provides existing-only checkpoint inspection and acknowledgement. Trusted CLI apply can diff --git a/CHANGELOG.md b/CHANGELOG.md index 1acaf9d..271195f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,364 @@ Changes are recorded here before a release version is assigned. ### Added +- Version 0.33.0 interrupts a streamed model request when a turn is cancelled + (Phase D5). See ADR-0050 and + [cancellation](docs/provider-authentication.md#cancellation-0330). Until now + cancellation was checked only between model rounds and tool calls, a request + in flight ran to its end or to the thirty-second deadline, a text-only turn + had no cancellation point, and on exit the desktop abandoned a turn still + waiting on the model, leaving an open turn. It is a prototype on branch + `codex/desktop-interface`; it is not merged. It adds no dependency and no + cargo feature. + - **Adapter:** `complete_streaming` and `complete_with_tools_streaming` take a + `CancelToken`. The streamed request runs on an async HTTP client inside a + single-thread runtime created for that one request. The client is built + inside that runtime and is not reused. Its settings equal the blocking + client's: no proxy, no redirect, no retry, the same connect timeout, and + the same thirty-second deadline for the whole response. The token is + checked before the request is sent, so an already cancelled turn makes no + connection, while waiting for the response head, and while waiting for each + piece of the body, every 25 ms, and also before each piece. When it is set + the request is dropped, which closes the connection, and the method returns + the new error `ProviderError::Cancelled` (`local model request was + cancelled`). Text already delivered stays delivered and is still + provisional. The event-stream rules of ADR-0049 are unchanged; the reader + is now a parser fed bytes in any chunking. The non-streaming methods keep + the blocking client and cannot be interrupted. + - **Runtime and recorded outcome:** `ToolModelRequest` carries the turn's + cancel token and `ModelError` gains `Cancelled`. When a model returns it + and the token is set, the turn ends as it does for a cancellation between + rounds: failure reason `cancelled`, error `TurnError::Cancelled`. A model + that returns it while the token is not set is recorded as a model error. A + streamed text-only turn now creates a cancel token and hands it to the + front end through the existing `on_cancel_token` hook. A cancelled request, + or a cancellation seen after the reply arrived and before it is recorded, + ends the turn with the failure reason `cancelled`. A non-streamed text-only + turn creates no token and cannot be cancelled. A cancelled turn has a + turn-failed record with the reason `cancelled`, no assistant message, and + no open turn. The partial text is never recorded. + - **Error kind:** the application layer reports a cancelled turn with the new + kind `cancelled` and the message `turn was cancelled`. The tool path + reported it with the general kind `turn` before. + - **Desktop and frontend:** `chat_cancel` now stops a streamed request. The + turn ends as `failed` with the error code `cancelled`, `finished` is + pushed, and the main window shows the plain notice "Turn cancelled." with + no error styling, decided by the error code only. Exit cancels every + running turn, so a streamed turn ends within the five-second grace period + and leaves no open turn. No command, argument, event, or wire type + changes; the shell still has 19 commands. The content security policy, the + windows, and the navigation guard are unchanged. + - **Compatibility:** no journal record changes shape, and sessions stay at + schema version 3. + - **Limits:** these are not interrupted: a non-streamed request + (`omgcode-desktop --stream off`, or the CLI without `--stream`), the + compaction request before a turn, and a tool call in progress. The CLI has + no cancel source, so stopping its process still leaves an open turn. A + long generation still fails at the thirty-second deadline. No live LM + Studio server was used, and no window has been opened on Linux. The + Windows shell was driven end to end against a mock that holds a stream + open, which does not qualify a live server. The CI workflows have still + never run. +- Version 0.32.0 streams the local model's reply and pushes chat events to the + desktop window (Phases D4 and E6). See ADR-0049 and + [streamed replies](docs/provider-authentication.md#streamed-replies-0320). + Until now the adapter sent one non-streaming request, and the desktop + learned that a turn had ended by calling `chat_status` every 750 ms. It is a + prototype on branch `codex/desktop-interface`; it is not merged. It adds no + dependency. + - **Adapter:** `LocalModelClient::complete_streaming` and + `complete_with_tools_streaming` send the same body as the non-streaming + methods with `stream: true` and `Accept: text/event-stream`. + `stream_options` is never sent. The non-streaming methods are unchanged + and remain the default. The response is read as server-sent events under + strict rules: a success status and the `text/event-stream` content type; + the 1 MiB response limit and the thirty-second request deadline cover the + whole stream; LF or CRLF lines; empty lines, comment lines, and the + `event`, `id`, and `retry` fields are ignored and any other non-`data` + line is refused; `[DONE]` ends the stream; end of input without `[DONE]` + is the new error `IncompleteStream` (`local model stream ended before + completion`); every payload goes through the parser that refuses duplicate + keys; a non-null `error` member is refused and never reported; zero or one + choice and one finish reason, with the same accepted finish reasons as + before. The text method refuses any tool call. Tool calls are assembled by + `index` (in order from 0, below 64, only `index`, `id`, `type`, and + `function` with `name` and `arguments`, `id` set once, pieces + concatenated, a null member treated as absent). The assembled value holds + only the members received, so the existing tool-call validation applies + unchanged. Usage counters are present only when the server sends them. + - **Provisional text and redaction:** text goes to the caller as each payload + passes validation. It is provisional: a later failure fails the request and + the delivered text is not a reply. `ChatHooks::on_stream` receives + `ChatStreamEvent::RequestStarted` before each model request of a turn and + `ChatStreamEvent::Text` for each piece. A turn sends streamed requests only + when the hook is set, and compaction requests are never streamed. + `StreamRedactor` removes the known server token across piece boundaries by + withholding any tail that could be the start of the token. If its output + would itself contain the token, it emits nothing further for that request. + The final reply is still redacted as a whole before it is recorded. + Streaming changes no journal record, and the journal does not say whether a + reply was streamed. Sessions stay at schema version 3. + - **CLI:** `omgcode chat --stream` writes the provisional text to standard + error as it arrives, escaped by `DisplaySafeStream`, the streaming form of + core's display-safe escaping, so model output cannot send control sequences + to a terminal. It writes one line per model request that produced text. + Standard output still carries the one JSON result. A failed write to + standard error stops further writes and does not fail the turn. Without the + flag the request is not streamed. `session compact` has no such flag. + `capabilities` reports `"streaming_adapters": ["lm_studio"]`. + - **Desktop:** a new command, `chat_subscribe`, is allowed for `main` only, + so the shell has 19 commands (16 for `main`, 3 for `approvals`). Its + argument `on_event` is a Tauri IPC channel created by the caller. The + service keeps at most one subscriber; a new subscription replaces the old + one and first replays the preview of every running turn. Events are tagged + by `kind`: `request_started`, `delta` (text of at most 1,024 bytes), + `preview_truncated` (the 16 KiB preview bound for one request was reached), + and `finished` (the turn left `running`; the caller reads `chat_status`). + The service keeps each running turn's preview for the current request + only. Preview text is returned as received after token redaction, like + transcript text. A guard sends `finished` after the result is recorded and + records a panicked turn as failed first, so `chat_status` read after + `finished` never reports `running`. Events are sent while the hub lock is + held, and the `turns` and `events` locks are never held together. A channel + needs no capability beyond the command's own permission; Tauri events would + need a `core:event` permission, and both windows still have no `core:` + permission. The launch argument `omgcode-desktop --stream ` + defaults to `on`; `off` sends non-streaming requests, and `finished` is + still pushed. Any other value is a usage error with exit status 2. It + exists because the stream rules have not met a live LM Studio server. + - **Frontend:** the main window subscribes once, on the first send or watch. + It no longer polls `chat_status` every 750 ms. It reads `chat_status` once + on `finished`, keeps a five-second reconcile check while a turn runs in + case an event is lost, and falls back to the 750 ms poll if subscribing + fails. While a turn generates, the open session shows one live entry + labelled "Assistant (generating, not saved yet)", updated at most once per + animation frame and replaced by the recorded transcript when the turn + settles. A note appears when the preview is cut off. The frontend keeps + previews for up to 4 sessions it is not tracking and bounds each at 64 KiB + of UTF-16 code units. It renders preview text as text and marks + bidirectional overrides. The command name lives in `src/ipc/chat_events.ts`, + which only the main bundle imports, and a bundle test checks that no + approvals asset contains `chat_subscribe`. The approvals polls (3 seconds) + are unchanged. + - **Compatibility:** the non-streaming adapter methods, the journal records, + schema version 3, the content security policy, the windows, and the + navigation guard are unchanged. Cancellation is still cooperative, between + rounds and tool calls; a request in flight is not interrupted. + - **Limits:** the stream rules were checked against local fixtures only, and + no live LM Studio server was used. The Windows shell was driven end to end + against a paced event-stream mock, which does not qualify a live server. + No window has been opened on Linux, so the channel is unverified there. A streamed text reply has no usage + counters unless the server sends them. Preview text can differ from the + recorded reply: a later failure discards it, and a reply over the 16 KiB + session message limit is shown in part and then fails as before. The + request deadline is still thirty seconds for the whole response, so a long + generation fails as before. The approvals polls and the session tree still + do not use push. The CI workflows have still never run. +- Version 0.31.0 binds a stored credential to its profile's endpoint (Phase + D3). See ADR-0048 and + [endpoint binding](docs/provider-authentication.md#endpoint-binding-0310). + In 0.30.0 a stored token went to whichever literal loopback endpoint the + operator named with `--endpoint`. It is a prototype on branch + `codex/desktop-interface`; it is not merged. It tightens the 0.30.0 + behaviour, which was never merged or released, and adds no dependency. + - **Profile endpoint:** `workspace provider-profile-add ... --endpoint + ` records one endpoint on a profile. `provider-profile-show` and + `status` print `"endpoint"` only when one is recorded; a profile without + one prints as before. An endpoint is not a secret. It is fixed for the + profile's life: no command changes it. To use another endpoint, remove the + profile and add a new one. Removing a profile already records the unbinding + and deletes its credential, so a stored credential cannot be moved to + another endpoint. + - **Validation:** only an `lm_studio` profile accepts an endpoint. It must be + `http://:/v1` (or `/v1/`), with no user + information, query, or fragment, and at most 256 bytes. This is the rule + the local adapter enforces on every request. Core has one implementation + (`validate_base_url`), used by the provider client and the profile record, + and journal replay applies it again. Every other provider kind refuses an + endpoint for now. The text is stored exactly as given. The refusals are + `provider profile refused: an lm_studio endpoint must be + http://:/v1 with no user information, query, or + fragment`, `... the provider endpoint is longer than 256 bytes`, and `... + this provider kind does not accept an endpoint yet`. + - **Selection:** a selected profile supplies the endpoint and the token. + `ControlStore::provider_credential_binding` returns the recorded endpoint + with the credential decision, so the two cannot come from different + places. It still takes no credential store. It checks the settings store, + the profile, the provider kind, the endpoint, and then the authentication + mode. A selected profile with no recorded endpoint is refused in every + authentication mode with a `credential` error that starts + `endpoint_not_recorded:`, before any native store access or request. + `--endpoint` and `--provider-profile` together are a usage error with exit + status 2 in `omgcode chat`, `omgcode session compact`, and + `omgcode-desktop`. The default value of `--endpoint` does not trigger it. + Without a profile, `--endpoint`, its default, and `OMGCODE_LM_TOKEN` behave + as before. + - **Desktop:** with a profile, the shell resolves the endpoint once at + launch, and a failure exits with status 1 before any window opens. That + value is the process's fixed endpoint: the webview is shown it and each chat + request is compared with it. Each turn resolves the profile again. If the + recorded endpoint no longer equals the launch value, because the profile + was removed and added again, `chat_send` fails at once with a `credential` + error that starts `endpoint_changed:` and no turn starts. A replaced + profile with the same endpoint and a new credential is accepted. The wire + contract is unchanged. + - **Compatibility:** the profile record's `endpoint` field is written only + when present, so a control journal without endpoints replays and serializes + with its exact bytes. A journal that contains an endpoint cannot be read by + 0.30.0 or earlier, which reject the unknown field. Replay rejects an + invalid endpoint, an endpoint on a kind that does not accept one, and any + unknown field. A profile recorded before 0.31.0 has no endpoint and cannot + be selected until it is removed and added again with `--endpoint`; the + credential must then be stored again. + - **Capabilities:** `capabilities` adds `"provider_profile_endpoints": true`. + - **Limits:** only `lm_studio` profiles accept an endpoint. No endpoint rule + exists for a non-loopback provider, HTTPS, or certificates, and no cloud + adapter exists. Any process running as the same user can still read a stored + credential. A hand-edited control journal is checked only by replay + validation and is not tamper-evident. The frontend shows nothing about the + credential source beyond the endpoint text it already displayed. +- Version 0.30.0 adds stored credential use for the local LM Studio adapter + (Phase D2). See ADR-0047 and + [stored credential use](docs/provider-authentication.md#stored-credential-use-0300). + Until now nothing read a stored credential, and the adapter took its optional + server token only from `OMGCODE_LM_TOKEN`. It is a prototype on branch + `codex/desktop-interface`; it is not merged. It adds no dependency outside + the existing workspace set (`omgcode-app` now uses `zeroize`). + - **Selection:** `omgcode chat --provider-profile ` and `omgcode + session compact --provider-profile ` read the token from the + credential bound to that recorded `lm_studio` profile. `omgcode-desktop + --provider-profile ` does the same for the whole process, fixed at + launch; the webview cannot choose or see it and the wire contract is + unchanged. With a profile selected, `OMGCODE_LM_TOKEN` is not read. + Without one, behaviour is unchanged and the variable remains the + prototype input, which still does not meet the credential-service + requirement. + - **Core checks:** `ControlStore::provider_credential_binding(name, + adapter_kind)` takes no credential store, so it cannot touch native + storage. It refuses an unknown profile, a profile of another provider + kind, a `subscription` profile, and an `api_key` profile with no bound + credential, and it yields no token for a `none` profile. The native store + is opened only when it returns a binding. + `ControlStore::load_provider_credential` then refuses a binding stored by + another backend before it reads any secret. The adapter passes + `lm_studio`, so a key recorded for another provider is never sent to it. + - **Failure:** any failure on the profile path ends the turn with a + `credential` error before a model request is built. The message is `: `, with the codes `unknown_profile`, `wrong_provider`, + `subscription_unsupported`, `not_bound`, `other_backend`, + `settings_unusable`, `credential_not_found`, `credential_locked`, + `credential_unavailable`, `credential_corrupt`, + `credential_prompt_required`, `credential_io`, `credential_refused`, and + `credential_invalid`. A missing or busy settings store is also a + `credential` error, with no cause code, and a busy store is not retried. + There is no fallback to no token or to another token. A locked Secret + Service is an error; OMGCode still never runs a prompt. In the CLI the + credential is resolved before the endpoint check and before any session or + journal exists, so the error JSON has no session locator. + - **Rotation and lifetime:** the secret is loaded while the settings store is + held, so a rotation cannot delete the item between the binding lookup and + the load. The store is released before any network request. A read records + nothing: no control-journal entry and no revision change, and the session + journal does not name the profile or the credential. The token is held in + a zeroing buffer with a redacted `Debug` and no serialization for one + turn, and the existing redaction of the token from recorded and returned + text applies. The provider clients' request-header copies and transport + buffers are not zeroed. + - **Desktop:** the service resolves the token again for every turn and caches + nothing, so a rotated credential applies to the next turn. An unreadable + credential is a synchronous `credential` error and no turn thread starts. + With a profile selected, the shell also resolves once at launch and exits + with status 1 and `omgcode-desktop: credential: : ` before any + window opens. The frontend shows nothing about the credential source. + - **Capabilities:** `capabilities` adds `"stored_credential_adapters": + ["lm_studio"]`. `provider_adapters` stays `false`: no adapter exists for + any provider other than the local LM Studio route. + - **Limits:** the credential is bound to a provider kind, not to an endpoint, + so a stored LM Studio token is sent to whichever literal loopback endpoint + the operator selects. Any process running as the same user can still read + the stored credential. A memory-only session credential is not + implemented, and no cloud adapter, subscription sign-in, token refresh, or + account lifecycle exists. +- Version 0.29.0 adds the desktop interface and session tree (Phase E). See + ADR-0044, ADR-0045, and ADR-0046, and the + [desktop interface guide](docs/desktop-interface.md). It is a prototype on + branch `codex/desktop-interface`; it has no installer and has run only on + Windows. No CLI command changed behaviour: every existing CLI test suite + passes unmodified, and `model_tools.rs` and `context_cli.rs` moved from + `omgcode-cli` into the new `omgcode-app` crate. + - **Journal inspection:** `SessionInspection::inspect` reads one journal + without any lock call, without following links, and with the writer's + validator, so a viewer never makes a writer report `Busy`. On Windows a + lock or sharing violation becomes `Busy` with no retry. Bytes after the last + line feed are ignored and reported as `incomplete_tail`; the file is never + changed, and `SessionJournal::open` still rejects such a file. The result + adds `created_at`, `updated_at`, and an ordered list of transcript entries; + a request manifest entry carries the model, prompt version, and tool names + only. `MessageSource` is now public. + - **Discovery:** `session::discover` and `session::locate` walk only the dated + `sessions` layout with a strict file-name parser, never open a journal or + follow a link or reparse point, and stop with `LimitReached` after 50,000 + journals or 500,000 directory entries. + - **Session index:** `session_index::SessionIndex` keeps a derived SQLite + index with FTS5 in `/session-index-v1/index.sqlite3`. It is a + cache that is deleted and rebuilt when it is foreign or corrupt at open. + Refresh is incremental and bounded to 1,000 journals per call; a busy + journal keeps its last metadata, and an invalid journal is listed with a + code. Search quotes every term and binds the expression as a parameter; it + refuses more than 16 terms or 256 characters. Titles and the user, + assistant, tool, and summary text are indexed; tool arguments, manifests, + and instructions are not. New dependency: `rusqlite` 0.43.0 with the + bundled SQLite 3.54.1 (MIT), which compiles C source at build time, so a C + compiler is required. The lockfile gains `rusqlite`, `libsqlite3-sys`, + `fallible-iterator`, and `fallible-streaming-iterator`. + - **Application library:** `omgcode-app` holds the chat turn, compaction, + session creation, the data-root resolver, token redaction, and + `DesktopService`. The CLI and the desktop shell both call it. It grants no + authority. Chat gains a cooperative cancellation hook; the CLI does not use + it, and it cannot interrupt a provider request in flight. + - **Approval review:** core adds `review_approval` and display-safe text. It + recomputes an approval's digest from the recorded tool request with the + tool handler's own function and returns `exact`, `stale`, or `not_found`. + It decides, consumes, and applies nothing. `ControlStore` gains + `workspaces()`, `directory_path()`, and `directory_display()`. + - **Desktop shell:** the new `omgcode-desktop` crate is a Tauri 2 shell with + two windows, 18 commands, a capability file per window and no `core:` + permission, a Rust window check as the first statement of every handler, a + content security policy, a navigation guard, and a five second bounded + shutdown. It takes `--data-dir` and `--endpoint` and reads no other + configuration. The webview receives no host path, journal path, or token. + Chat tools follow the workspace settings as in the CLI. New dependencies: + `tauri` 2.12.6 (`wry` and `x11` features only) and `tauri-build` 2.7.5, + Apache-2.0 OR MIT, with no plugin, tray, updater, asset protocol, or + devtools feature. The lockfile grows by about 283 entries. A Linux build + needs the WebKitGTK development packages. + - **Approvals window:** a separate `approvals` window holds the only decision + controls. Approving needs an `exact` review binding and a single-use review + token that lives 120 seconds; a file edit also needs the writers-stopped + checkbox. The CLI `approvals` commands are unchanged. The window is still + web content and is not a trusted computing base. + - **Frontend:** `crates/omgcode-desktop/ui` is strict TypeScript with Vite, + Vitest (jsdom), and Biome, with one runtime dependency, `@tauri-apps/api` + 2.12.5. Versions are exact, the lockfile is committed, and `.npmrc` sets + `ignore-scripts=true`. The interface builds its document from text nodes + only; tests scan for markup injection, inline script, remote resources, and + browser storage. It polls for chat status and approvals; there is no push + channel. + - **Continuous integration:** the Rust workflow installs the Tauri Linux + packages, sets up Node 24, builds the frontend before Clippy, and has a 40 + minute timeout (was 20). A new Frontend workflow runs the type check, Biome, + Vitest, and the bundle checks on Ubuntu 24.04 and Windows Server 2025. + `.gitattributes` checks the frontend out with LF. The root `.gitignore` + gains `node_modules/` and `dist/`. + - **Limits:** on Linux the shell builds and the workspace tests pass (WSL + 6.6, Ubuntu 24.04), but no window has been opened there, so its webview, + IPC, and capability enforcement are unverified on Linux. The workflow + changes have not run on GitHub, and CodeQL's JavaScript/TypeScript + selection is unverified. Two link tests are skipped on a host without + symlink privilege; they ran on Linux. The shell has no packaging, streaming, settings + interface, development panels, or recovery interface. Corruption inside an + index table behind a valid header is not rebuilt automatically, and two + versions sharing a data root rebuild each other's index. - Version 0.28.0 adds the approval-gated model edit tool (Phase C17). See ADR-0043. - **Tool:** `workspace_edit_file { path, old_text, new_text }` replaces @@ -469,6 +827,38 @@ Changes are recorded here before a release version is assigned. ### Fixed +- Findings from the first Sonar analysis of the desktop frontend: + - Both workflows now install the frontend's dependencies with + `npm ci --ignore-scripts`. `ui/.npmrc` already set this; the flag makes + it hold even if that file changes. + - Four lines of frontend source and tests held raw bidirectional control + characters, inside the pattern and the test strings that deal with those + characters. They are now written as `\u` escapes, which denote the same + characters, and a frontend test fails if a raw one is added. + - `sonar-project.properties` declares test code as test code. Every + duplicated line Sonar reported, and about half the lines it counted as + code to cover, were in test files analysed as production code. + - Sonar's condition on coverage of new code failed because no coverage + report existed for the frontend's TypeScript sources. `npm run coverage` + now writes one with `@vitest/coverage-v8` 5.1.0, a new development + dependency pinned to the Vitest version. The Sonar workflow runs it in a + separate job that never references the Sonar credential and passes only + the report to the analysis job. Nothing is excluded from the measure. It + stood at 85.7% of lines and 76.0% of branches on 2026-11-17, about 82% + by Sonar's combined measure against a required 80%. Rust is still not + measured for coverage. +- The CLI test `concurrent_runs_complete_and_are_recorded` no longer depends + on host speed. On Windows each run spun a fixed loop of 30,000 `cd .` + iterations before writing its file, and the test allowed 60 seconds for + both. On a host held near 100% CPU by an unrelated build, one such loop took + 22 to 24 seconds in a plain shell, and the test failed three times in a row, + on this change and on the commit before it. Each run now writes its own file + and waits until it sees the other run's file. Exit code 0 from both proves + the runs were alive at the same time, which the old scripts did not: two + runs made one after the other would also have passed. A run that never sees + the other's file exits non-zero. The Linux scripts use the same rendezvous + in place of `sleep 1`. The assertions are unchanged, and no limit was + raised. No production code changed. - PR #32 review follow-up: - Compaction splits summarizer input at the 128-message prompt-assembly limit as well as the byte limit, in history chunks and combination diff --git a/Cargo.lock b/Cargo.lock index 1008554..7d81eb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "aes" version = "0.9.4" @@ -10,7 +16,25 @@ checksum = "fc0fc36d7440d015b45468ba08a21e80e056ebbd014c611c6298bb7503e04bd1" dependencies = [ "cipher", "cpubits", - "cpufeatures", + "cpufeatures 0.3.1", +] + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", ] [[package]] @@ -49,7 +73,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -60,9 +84,15 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys", + "windows-sys 0.61.2", ] +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + [[package]] name = "async-broadcast" version = "0.7.2" @@ -97,6 +127,29 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "atk" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "241b621213072e993be4f6f3a9e4b45f65b7e6faad43001be957184b7bb1824b" +dependencies = [ + "atk-sys", + "glib", + "libc", +] + +[[package]] +name = "atk-sys" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5e48b684b0ca77d2bbadeef17424c2ea3c897d44d566a1617e7e8f30614d086" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -109,17 +162,71 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "base64" version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec 0.8.0", +] + +[[package]] +name = "bit-set" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d87354e4229f54a44f7bf2435906a4656dba36026ab6eaca629a2c436a691c" +dependencies = [ + "bit-vec 0.10.2", +] + +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + +[[package]] +name = "bit-vec" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72503af1e72be411d74ca45cd6b309878fcb35354e252fc2a3a8bda3a97082a0" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +dependencies = [ + "serde_core", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] [[package]] name = "block-buffer" @@ -139,17 +246,118 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2", +] + +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] + [[package]] name = "bumpalo" version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" +dependencies = [ + "serde", +] + +[[package]] +name = "cairo-rs" +version = "0.18.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ca26ef0159422fb77631dc9d17b102f253b876fe1586b03b803e63a309b4ee2" +dependencies = [ + "bitflags 2.13.2", + "cairo-sys-rs", + "glib", + "libc", + "once_cell", + "thiserror 1.0.69", +] + +[[package]] +name = "cairo-sys-rs" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685c9fa8e590b8b3d678873528d83411db17242a73fccaed827770ea0fedda51" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "camino" +version = "1.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbad30e4b4c14a39e3cc8aed085a12a327257c316619c93581e017bc52be591" +dependencies = [ + "serde_core", +] + +[[package]] +name = "cargo-platform" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e35af189006b9c0f00a064685c727031e3ed2d8020f7ba284d78cc2671bd36ea" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd5eb614ed4c27c5d706420e4320fbe3216ab31fa1c33cd8246ac36dae4479ba" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "cargo_toml" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f4b26e751e711a5302649417f2da046dce6391b2ea30a4820f37462314f0b9" +dependencies = [ + "semver", + "serde", + "toml 1.2.1+spec-1.1.0", +] [[package]] name = "cbc" @@ -170,19 +378,58 @@ dependencies = [ "shlex", ] +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + +[[package]] +name = "cfb" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a347dcabdae9c31b0825fd6a8bed285ec9c2acb89c47827126d52fa4f59cece3" +dependencies = [ + "fnv", + "uuid", + "web-time", +] + +[[package]] +name = "cfg-expr" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d067ad48b8650848b989a59a86c6c36a995d02d2bf778d45c3c5d57bc2718f02" +dependencies = [ + "smallvec", + "target-lexicon", +] + [[package]] name = "cfg-if" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "num-traits", + "serde", + "windows-link", +] + [[package]] name = "cipher" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" dependencies = [ - "crypto-common", + "crypto-common 0.2.2", "inout", ] @@ -214,7 +461,7 @@ version = "4.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", "syn 3.0.9", @@ -238,12 +485,32 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "combine" +version = "4.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4356eba6976435498d05a311ee9fc486da3bc81c200d3f0d7dd9703b41913f92" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "const-oid" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" +[[package]] +name = "cookie" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a373e3602691c3cdea496d2f0ee5935151e6168fe87739483c463db1b2f2f87" +dependencies = [ + "time", + "version_check", +] + [[package]] name = "core-foundation" version = "0.10.1" @@ -260,12 +527,45 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "core-graphics" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "064badf302c3194842cf2c5d61f56cc88e54a759313879cdf03abdd27d0c3b97" +dependencies = [ + "bitflags 2.13.2", + "core-foundation", + "core-graphics-types", + "foreign-types 0.5.0", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d44a101f213f6c4cdc1853d4b78aef6db6bdfa3468798cc1d9912f4735013eb" +dependencies = [ + "bitflags 2.13.2", + "core-foundation", + "libc", +] + [[package]] name = "cpubits" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "cpufeatures" version = "0.3.1" @@ -276,109 +576,394 @@ dependencies = [ ] [[package]] -name = "crypto-common" -version = "0.2.2" +name = "crc32fast" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +checksum = "2b929dd2a135771f40afabd1073f85eed59c800b7c61901e5c254270497dad64" dependencies = [ - "hybrid-array", + "cfg-if", ] [[package]] -name = "ctutils" -version = "0.4.8" +name = "crossbeam-channel" +version = "0.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" +checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1" dependencies = [ - "cmov", + "crossbeam-utils", ] [[package]] -name = "deranged" -version = "0.5.9" +name = "crossbeam-utils" +version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ - "serde_core", + "generic-array", + "typenum", ] [[package]] -name = "digest" -version = "0.11.3" +name = "crypto-common" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" dependencies = [ - "block-buffer", - "const-oid", - "crypto-common", - "ctutils", + "hybrid-array", ] [[package]] -name = "directories" -version = "6.0.0" +name = "cssparser" +version = "0.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d" +checksum = "8c9cdaae01d5ed7882b04d795e7f752f46ff52d2fa3b50a20d28c464510bba98" dependencies = [ - "dirs-sys", + "cssparser-macros", + "dtoa-short", + "itoa", + "phf 0.13.1", + "smallvec", ] [[package]] -name = "dirs-sys" -version = "0.5.0" +name = "cssparser" +version = "0.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +checksum = "11119743ad110e8c1bdccd930d7f5c30c99e5fc76a7b63ec9807e84eef0c5f59" dependencies = [ - "libc", - "option-ext", - "redox_users", - "windows-sys", + "cssparser-macros", + "dtoa-short", + "itoa", + "phf 0.14.0", + "smallvec", ] [[package]] -name = "displaydoc" -version = "0.2.7" +name = "cssparser-macros" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +checksum = "d045de693cb712d0b22c6a64be5b953f67b3ce00ab5ad3dd5d8b441886ab8e1a" dependencies = [ - "proc-macro2", "quote", "syn 3.0.9", ] [[package]] -name = "endi" -version = "1.1.1" +name = "ctor" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" +checksum = "ea691ed9f36489d087419b8b2ba3ef27d89621380b4178c73e2445a379e4e0f4" +dependencies = [ + "link-section", +] [[package]] -name = "enumflags2" -version = "0.7.12" +name = "ctutils" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1027f7680c853e056ebcec683615fb6fbbc07dbaa13b4d5d9442b146ded4ecef" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" dependencies = [ - "enumflags2_derive", - "serde", + "cmov", ] [[package]] -name = "enumflags2_derive" -version = "0.7.12" +name = "darling" +version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67c78a4d8fdf9953a5c9d458f9efe940fd97a0cab0941c075a813ac594733827" +checksum = "ed17f5901b6630b993ca003def43f2f8ef4014fc13b047b57aad617ff32bc2ec" dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", + "darling_core", + "darling_macro", ] [[package]] -name = "equivalent" -version = "1.0.3" +name = "darling_core" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6837e2cf7485aaae18f86181d2f0e9a7ed297a025e220aeabf63fdebd3a2ddff" +dependencies = [ + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 3.0.9", +] + +[[package]] +name = "darling_macro" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785" +dependencies = [ + "darling_core", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "deranged" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.119", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "crypto-common 0.1.7", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid", + "crypto-common 0.2.2", + "ctutils", +] + +[[package]] +name = "directories" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d57d423b3c82e89b9a24ca3091fee61f456a26edbd28d26c65906f4bc1dcd8f" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.61.2", +] + +[[package]] +name = "dispatch2" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" +dependencies = [ + "bitflags 2.13.2", + "block2", + "libc", + "objc2", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "dlopen2" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e2c5bd4158e66d1e215c49b837e11d62f3267b30c92f1d171c4d3105e3dc4d4" +dependencies = [ + "dlopen2_derive", + "libc", + "once_cell", + "winapi", +] + +[[package]] +name = "dlopen2_derive" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fbbb781877580993a8707ec48672673ec7b81eeba04cfd2310bd28c08e47c8f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "dom_query" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fac5fca71e65e94cc718a6e2af65d6e0f9c6027751c2aa562fbb5087fda639bc" +dependencies = [ + "bit-set 0.8.0", + "cssparser 0.37.0", + "foldhash", + "html5ever 0.39.0", + "precomputed-hash", + "selectors 0.38.0", + "tendril", +] + +[[package]] +name = "dom_query" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498c698ac51127148a3984450e51fca7f01805a91d7a1f7a24af0a5f761e3385" +dependencies = [ + "bit-set 0.11.1", + "cssparser 0.38.0", + "foldhash", + "html5ever 0.40.1", + "precomputed-hash", + "selectors 0.41.0", + "tendril", +] + +[[package]] +name = "dpi" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" +dependencies = [ + "serde", +] + +[[package]] +name = "dtoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c3cf4824e2d5f025c7b531afcb2325364084a16806f6d47fbc1f5fbd9960590" + +[[package]] +name = "dtoa-short" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd1511a7b6a56299bd043a9c167a6d2bfb37bf84a6dfceaba651168adfb43c87" +dependencies = [ + "dtoa", +] + +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + +[[package]] +name = "embed-resource" +version = "3.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6168487b5e7eb2673e6efe1200b1848016e436a6ca0b8031bf04bd9d493a249f" +dependencies = [ + "cc", + "memchr", + "rustc_version", + "toml 1.2.1+spec-1.1.0", + "vswhom", + "winreg", +] + +[[package]] +name = "embed_plist" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7" + +[[package]] +name = "endi" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" + +[[package]] +name = "enumflags2" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1027f7680c853e056ebcec683615fb6fbbc07dbaa13b4d5d9442b146ded4ecef" +dependencies = [ + "enumflags2_derive", + "serde", +] + +[[package]] +name = "enumflags2_derive" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67c78a4d8fdf9953a5c9d458f9efe940fd97a0cab0941c075a813ac594733827" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "equivalent" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" +[[package]] +name = "erased-serde" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e59b668157f32d3459d4bd634861a06ffa2503a461ca5f0916b9f0252605e66" +dependencies = [ + "serde", + "serde_core", + "typeid", +] + [[package]] name = "errno" version = "0.3.14" @@ -386,7 +971,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -409,25 +994,100 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fastrand" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "field-offset" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38e2275cc4e4fc009b0669731a1e5ab7ebf11f469eaede2bab9309a5b4d6057f" +dependencies = [ + "memoffset", + "rustc_version", +] + [[package]] name = "find-msvc-tools" version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" +[[package]] +name = "flate2" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626d4928326b646e70f65ed36fc7cf9f206bdb68a0019ee7c6b4709da4e084c4" +dependencies = [ + "crc32fast", + "miniz_oxide 0.9.1", + "zlib-rs", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "foreign-types" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" dependencies = [ - "foreign-types-shared", + "foreign-types-shared 0.1.1", +] + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared 0.3.1", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5190182e6915eb873ddbc16e23b711b6eb1f9c00a0d0a3a91b5f6228475225" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", ] [[package]] @@ -436,6 +1096,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -461,6 +1127,17 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" +[[package]] +name = "futures-executor" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "031b47cf1a3c6cc8bc2fc76cd437f521619387907d469316e7c0bc278f1f5432" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.34" @@ -520,22 +1197,303 @@ dependencies = [ ] [[package]] -name = "getrandom" -version = "0.4.3" +name = "gdk" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +checksum = "d9f245958c627ac99d8e529166f9823fb3b838d1d41fd2b297af3075093c2691" dependencies = [ - "cfg-if", + "cairo-rs", + "gdk-pixbuf", + "gdk-sys", + "gio", + "glib", "libc", - "r-efi", + "pango", ] +[[package]] +name = "gdk-pixbuf" +version = "0.18.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50e1f5f1b0bfb830d6ccc8066d18db35c487b1b2b1e8589b5dfe9f07e8defaec" +dependencies = [ + "gdk-pixbuf-sys", + "gio", + "glib", + "libc", + "once_cell", +] + +[[package]] +name = "gdk-pixbuf-sys" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9839ea644ed9c97a34d129ad56d38a25e6756f99f3a88e15cd39c20629caf7" +dependencies = [ + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gdk-sys" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c2d13f38594ac1e66619e188c6d5a1adb98d11b2fcf7894fc416ad76aa2f3f7" +dependencies = [ + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", + "pkg-config", + "system-deps", +] + +[[package]] +name = "gdkwayland-sys" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "140071d506d223f7572b9f09b5e155afbd77428cd5cc7af8f2694c41d98dfe69" +dependencies = [ + "gdk-sys", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", + "system-deps", +] + +[[package]] +name = "gdkx11" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3caa00e14351bebbc8183b3c36690327eb77c49abc2268dd4bd36b856db3fbfe" +dependencies = [ + "gdk", + "gdkx11-sys", + "gio", + "glib", + "libc", + "x11", +] + +[[package]] +name = "gdkx11-sys" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e7445fe01ac26f11601db260dd8608fe172514eb63b3b5e261ea6b0f4428d" +dependencies = [ + "gdk-sys", + "glib-sys", + "libc", + "system-deps", + "x11", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", +] + +[[package]] +name = "gio" +version = "0.18.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fc8f532f87b79cbc51a79748f16a6828fb784be93145a322fa14d06d354c73" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-util", + "gio-sys", + "glib", + "libc", + "once_cell", + "pin-project-lite", + "smallvec", + "thiserror 1.0.69", +] + +[[package]] +name = "gio-sys" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37566df850baf5e4cb0dfb78af2e4b9898d817ed9263d1090a2df958c64737d2" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", + "winapi", +] + +[[package]] +name = "glib" +version = "0.18.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233daaf6e83ae6a12a52055f568f9d7cf4671dabb78ff9560ab6da230ce00ee5" +dependencies = [ + "bitflags 2.13.2", + "futures-channel", + "futures-core", + "futures-executor", + "futures-task", + "futures-util", + "gio-sys", + "glib-macros", + "glib-sys", + "gobject-sys", + "libc", + "memchr", + "once_cell", + "smallvec", + "thiserror 1.0.69", +] + +[[package]] +name = "glib-macros" +version = "0.18.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bb0228f477c0900c880fd78c8759b95c7636dbd7842707f49e132378aa2acdc" +dependencies = [ + "heck 0.4.1", + "proc-macro-crate 2.0.2", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "glib-sys" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063ce2eb6a8d0ea93d2bf8ba1957e78dbab6be1c2220dd3daca57d5a9d869898" +dependencies = [ + "libc", + "system-deps", +] + +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + +[[package]] +name = "gobject-sys" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0850127b514d1c4a4654ead6dedadb18198999985908e6ffe4436f53c785ce44" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gtk" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd56fb197bfc42bd5d2751f4f017d44ff59fbb58140c6b49f9b3b2bdab08506a" +dependencies = [ + "atk", + "cairo-rs", + "field-offset", + "futures-channel", + "gdk", + "gdk-pixbuf", + "gio", + "glib", + "gtk-sys", + "gtk3-macros", + "libc", + "pango", + "pkg-config", +] + +[[package]] +name = "gtk-sys" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f29a1c21c59553eb7dd40e918be54dccd60c52b049b75119d5d96ce6b624414" +dependencies = [ + "atk-sys", + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gdk-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", + "system-deps", +] + +[[package]] +name = "gtk3-macros" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ff3c5b21f14f0736fed6dcfc0bfb4225ebf5725f3c0209edeec181e4d73e9d" +dependencies = [ + "proc-macro-crate 1.3.1", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "heck" version = "0.5.0" @@ -563,7 +1521,28 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" dependencies = [ - "digest", + "digest 0.11.3", +] + +[[package]] +name = "html5ever" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46a1761807faccc9a19e86944bbf40610014066306f96edcdedc2fb714bcb7b8" +dependencies = [ + "log", + "markup5ever 0.39.0", +] + +[[package]] +name = "html5ever" +version = "0.40.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456a1a377e608e555d22ddab27ac0114bc7a7b4199078108e34c2aeae6c9b130" +dependencies = [ + "log", + "markup5ever 0.40.0", + "memchr", ] [[package]] @@ -655,7 +1634,7 @@ version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" dependencies = [ - "base64", + "base64 0.23.1", "bytes", "futures-channel", "futures-util", @@ -673,6 +1652,40 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ico" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75e94d66081e4d8dcd098bd8dc5254800fea14a26389b298410158d834de167" +dependencies = [ + "byteorder", + "png", +] + [[package]] name = "icu_collections" version = "2.4.0" @@ -756,6 +1769,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "1.1.0" @@ -777,6 +1796,17 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", + "serde", +] + [[package]] name = "indexmap" version = "2.14.2" @@ -784,7 +1814,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "infer" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4200d433cbd5178df7797c9c2e75b348b728e39631cf14520d1e2fc424201f4" +dependencies = [ + "cfb", ] [[package]] @@ -816,73 +1857,319 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" [[package]] -name = "js-sys" -version = "0.3.107" +name = "javascriptcore-rs" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +checksum = "ca5671e9ffce8ffba57afc24070e906da7fc4b1ba66f2cabebf61bf2ea257fcc" dependencies = [ - "cfg-if", - "futures-util", - "wasm-bindgen", + "bitflags 1.3.2", + "glib", + "javascriptcore-rs-sys", ] [[package]] -name = "libc" -version = "0.2.191" +name = "javascriptcore-rs-sys" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" +checksum = "af1be78d14ffa4b75b66df31840478fef72b51f8c2465d4ca7c194da9f7a5124" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] [[package]] -name = "libredox" -version = "0.1.25" +name = "jiff" +version = "0.2.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +checksum = "175dc66334db3b9ab9985bf8b61cfed50dc16d20964bb47f9e479adcae984a7f" dependencies = [ - "libc", + "jiff-core", + "jiff-static", + "jiff-tzdb-platform", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", + "windows-link", ] [[package]] -name = "linux-raw-sys" -version = "0.12.1" +name = "jiff-core" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" +checksum = "e599b80292e597019b8b3e392de3f4bea2959ddfb8c7dff60e035e9e311a2c1b" +dependencies = [ + "log", +] [[package]] -name = "litemap" -version = "0.8.3" +name = "jiff-static" +version = "0.2.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" +checksum = "c7b1af7547d47354bc438d797e3854601bde8f8d185780c5ba4ab27afa54f1ba" +dependencies = [ + "jiff-core", + "proc-macro2", + "quote", + "syn 2.0.119", +] [[package]] -name = "log" -version = "0.4.35" +name = "jiff-tzdb" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" +checksum = "0980fca2cc272465bd60e82397607ba9f33add9e48defd77923301bbd03808f2" [[package]] -name = "memchr" -version = "2.8.3" +name = "jiff-tzdb-platform" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +checksum = "875a5a69ac2bab1a891711cf5eccbec1ce0341ea805560dcd90b7a2e925132e8" +dependencies = [ + "jiff-tzdb", +] [[package]] -name = "memoffset" -version = "0.9.1" +name = "jni" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" dependencies = [ - "autocfg", + "cesu8", + "cfg-if", + "combine", + "jni-sys 0.3.1", + "log", + "thiserror 1.0.69", + "walkdir", + "windows-sys 0.45.0", ] [[package]] -name = "mio" -version = "1.2.4" +name = "jni-sys" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +checksum = "41a652e1f9b6e0275df1f15b32661cf0d4b78d4d87ddec5e0c3c20f097433258" dependencies = [ - "libc", - "wasi", - "windows-sys", + "jni-sys 0.4.1", +] + +[[package]] +name = "jni-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6377a88cb3910bee9b0fa88d4f42e1d2da8e79915598f65fb0c7ee14c878af2" +dependencies = [ + "jni-sys-macros", +] + +[[package]] +name = "jni-sys-macros" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264" +dependencies = [ + "quote", + "syn 2.0.119", +] + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "json-patch" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7421438de105a0827e44fadd05377727847d717c80ce29a229f85fd04c427b72" +dependencies = [ + "jsonptr", + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "jsonptr" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5a3cc660ba5d72bce0b3bb295bf20847ccbb40fd423f3f05b61273672e561fe" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "keyboard-types" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fbe853b403ae61a04233030ae8a79d94975281ed9770a1f9e246732b534b28d" +dependencies = [ + "bitflags 2.13.2", + "serde", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libredox" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +dependencies = [ + "libc", +] + +[[package]] +name = "libsqlite3-sys" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b06e69070b7e12262b0cd90832f0a7396f142a2b330181255562decf9f0c6215" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "link-section" +version = "0.19.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d9aae57673c1a235149777a74d152f3210ebd3e3b13346d2cdb6809e566bd3c" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "markup5ever" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7122d987ec5f704ee56f6e5b41a7d93722e9aae27ae07cafa4036c4d3f9757de" +dependencies = [ + "log", + "tendril", + "web_atoms 0.2.6", +] + +[[package]] +name = "markup5ever" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ab3dc68ac4a0f5719e560136778c1ee716e296030d75dbd4484e37e39e3a842" +dependencies = [ + "log", + "tendril", + "web_atoms 0.3.0", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "mime" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5848f5a9e8117cf94f2f90faac24a87ff81600ff8628c1e3deaab572e76bfa1" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "muda" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cca139e57da4383727f189e43e66e84fa372347027fc996f9f1a007ec0a37996" +dependencies = [ + "crossbeam-channel", + "dpi", + "gtk", + "keyboard-types", + "objc2", + "objc2-app-kit", + "objc2-core-foundation", + "objc2-foundation", + "once_cell", + "png", + "serde", + "thiserror 2.0.21", + "windows-sys 0.61.2", ] [[package]] @@ -902,6 +2189,42 @@ dependencies = [ "tempfile", ] +[[package]] +name = "ndk" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4" +dependencies = [ + "bitflags 2.13.2", + "jni-sys 0.3.1", + "log", + "ndk-sys", + "num_enum", + "raw-window-handle", + "thiserror 1.0.69", +] + +[[package]] +name = "ndk-context" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b" + +[[package]] +name = "ndk-sys" +version = "0.6.0+11769913" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee6cda3051665f1fb8d9e08fc35c96d5a244fb1be711a03b71118828afc9a873" +dependencies = [ + "jni-sys 0.3.1", +] + +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "num" version = "0.4.3" @@ -981,666 +2304,2505 @@ dependencies = [ ] [[package]] -name = "omgcode-cli" -version = "0.28.0" +name = "num_enum" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0bca838442ec211fa11de3a8b0e0e8f3a4522575b5c4c06ed722e005036f26" dependencies = [ - "clap", - "directories", - "omgcode-core", - "serde", - "serde_json", - "tempfile", - "uuid", + "num_enum_derive", + "rustversion", ] [[package]] -name = "omgcode-core" -version = "0.28.0" +name = "num_enum_derive" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8" dependencies = [ - "libc", - "omgcode-platform", - "reqwest", - "serde", - "serde_json", - "sha2", - "tempfile", - "thiserror", - "time", - "tokio", - "uuid", + "proc-macro-crate 3.5.0", + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] -name = "omgcode-platform" -version = "0.28.0" +name = "objc2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8" dependencies = [ - "libc", - "rustix", - "secret-service", - "tempfile", - "tokio", - "uuid", - "windows-sys", - "zbus", - "zeroize", + "objc2-encode", + "objc2-exception-helper", ] [[package]] -name = "once_cell" -version = "1.21.4" +name = "objc2-app-kit" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c" +dependencies = [ + "bitflags 2.13.2", + "block2", + "objc2", + "objc2-core-foundation", + "objc2-foundation", + "objc2-quartz-core", +] [[package]] -name = "once_cell_polyfill" -version = "1.70.2" +name = "objc2-cloud-kit" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +checksum = "73ad74d880bb43877038da939b7427bba67e9dd42004a18b809ba7d87cee241c" +dependencies = [ + "bitflags 2.13.2", + "objc2", + "objc2-foundation", +] [[package]] -name = "openssl" -version = "0.10.81" +name = "objc2-core-data" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77823a27f0babb03091cb9ed9ef80af3b39dbc82f97e8fa530374b7dafd87a45" +checksum = "0b402a653efbb5e82ce4df10683b6b28027616a2715e90009947d50b8dd298fa" dependencies = [ - "bitflags", - "cfg-if", - "foreign-types", - "libc", - "openssl-macros", - "openssl-sys", + "objc2", + "objc2-foundation", ] [[package]] -name = "openssl-macros" -version = "0.1.1" +name = "objc2-core-foundation" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", + "bitflags 2.13.2", + "dispatch2", + "objc2", ] [[package]] -name = "openssl-probe" -version = "0.2.2" +name = "objc2-core-graphics" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e036b411754132fd6b74e94b74e9a13c5cf606fa93d1885c35860d3060820b1" +checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807" +dependencies = [ + "bitflags 2.13.2", + "dispatch2", + "objc2", + "objc2-core-foundation", + "objc2-io-surface", +] [[package]] -name = "openssl-sys" -version = "0.9.117" +name = "objc2-core-image" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b47e7e6bb2c38cd930d25a23b40fa52e068c10e85f3e03a7f5ba5aaca5713695" +checksum = "e5d563b38d2b97209f8e861173de434bd0214cf020e3423a52624cd1d989f006" dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", + "objc2", + "objc2-foundation", ] [[package]] -name = "option-ext" -version = "0.2.0" +name = "objc2-core-location" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +checksum = "ca347214e24bc973fc025fd0d36ebb179ff30536ed1f80252706db19ee452009" +dependencies = [ + "objc2", + "objc2-foundation", +] [[package]] -name = "ordered-stream" +name = "objc2-core-text" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cde0dfb48d25d2b4862161a4d5fcc0e3c24367869ad306b0c9ec0073bfed92d" +dependencies = [ + "bitflags 2.13.2", + "objc2", + "objc2-core-foundation", + "objc2-core-graphics", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + +[[package]] +name = "objc2-exception-helper" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7a1c5fbb72d7735b076bb47b578523aedc40f3c439bea6dfd595c089d79d98a" +dependencies = [ + "cc", +] + +[[package]] +name = "objc2-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272" +dependencies = [ + "bitflags 2.13.2", + "block2", + "objc2", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-io-surface" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180788110936d59bab6bd83b6060ffdfffb3b922ba1396b312ae795e1de9d81d" +dependencies = [ + "bitflags 2.13.2", + "objc2", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" +dependencies = [ + "bitflags 2.13.2", + "objc2", + "objc2-core-foundation", + "objc2-foundation", +] + +[[package]] +name = "objc2-ui-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87d638e33c06f577498cbcc50491496a3ed4246998a7fbba7ccb98b1e7eab22" +dependencies = [ + "bitflags 2.13.2", + "block2", + "objc2", + "objc2-cloud-kit", + "objc2-core-data", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-core-image", + "objc2-core-location", + "objc2-core-text", + "objc2-foundation", + "objc2-quartz-core", + "objc2-user-notifications", +] + +[[package]] +name = "objc2-user-notifications" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9df9128cbbfef73cda168416ccf7f837b62737d748333bfe9ab71c245d76613e" +dependencies = [ + "objc2", + "objc2-foundation", +] + +[[package]] +name = "objc2-web-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2e5aaab980c433cf470df9d7af96a7b46a9d892d521a2cbbb2f8a4c16751e7f" +dependencies = [ + "bitflags 2.13.2", + "block2", + "objc2", + "objc2-app-kit", + "objc2-core-foundation", + "objc2-foundation", +] + +[[package]] +name = "omgcode-app" +version = "0.33.0" +dependencies = [ + "directories", + "omgcode-core", + "serde", + "serde_json", + "sha2 0.11.1", + "tempfile", + "time", + "uuid", + "zeroize", +] + +[[package]] +name = "omgcode-cli" +version = "0.33.0" +dependencies = [ + "clap", + "omgcode-app", + "omgcode-core", + "serde", + "serde_json", + "tempfile", + "uuid", +] + +[[package]] +name = "omgcode-core" +version = "0.33.0" +dependencies = [ + "libc", + "omgcode-platform", + "reqwest", + "rusqlite", + "serde", + "serde_json", + "sha2 0.11.1", + "tempfile", + "thiserror 2.0.21", + "time", + "tokio", + "uuid", +] + +[[package]] +name = "omgcode-desktop" +version = "0.33.0" +dependencies = [ + "omgcode-app", + "serde", + "serde_json", + "tauri", + "tauri-build", + "uuid", +] + +[[package]] +name = "omgcode-platform" +version = "0.33.0" +dependencies = [ + "libc", + "rustix", + "secret-service", + "tempfile", + "tokio", + "uuid", + "windows-sys 0.61.2", + "zbus", + "zeroize", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "openssl" +version = "0.10.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77823a27f0babb03091cb9ed9ef80af3b39dbc82f97e8fa530374b7dafd87a45" +dependencies = [ + "bitflags 2.13.2", + "cfg-if", + "foreign-types 0.3.2", + "libc", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "openssl-probe" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e036b411754132fd6b74e94b74e9a13c5cf606fa93d1885c35860d3060820b1" + +[[package]] +name = "openssl-sys" +version = "0.9.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b47e7e6bb2c38cd930d25a23b40fa52e068c10e85f3e03a7f5ba5aaca5713695" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "ordered-stream" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aa2b01e1d916879f73a53d01d1d6cee68adbb31d6d9177a8cfce093cced1d50" +checksum = "9aa2b01e1d916879f73a53d01d1d6cee68adbb31d6d9177a8cfce093cced1d50" +dependencies = [ + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "pango" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ca27ec1eb0457ab26f3036ea52229edbdb74dee1edd29063f5b9b010e7ebee4" +dependencies = [ + "gio", + "glib", + "libc", + "once_cell", + "pango-sys", +] + +[[package]] +name = "pango-sys" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "436737e391a843e5933d6d9aa102cb126d501e815b83601365a948a518555dc5" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_macros 0.13.1", + "phf_shared 0.13.1", + "serde", +] + +[[package]] +name = "phf" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "010378780309880b08997fae13be7834dba947d36393bd372f2b1556deb2a2f6" +dependencies = [ + "phf_macros 0.14.0", + "phf_shared 0.14.0", + "serde", +] + +[[package]] +name = "phf_codegen" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49aa7f9d80421bca176ca8dbfebe668cc7a2684708594ec9f3c0db0805d5d6e1" +dependencies = [ + "phf_generator 0.13.1", + "phf_shared 0.13.1", +] + +[[package]] +name = "phf_codegen" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b585a510fb76fdebead6897982ef2a03a21d8e6cbcca904999742a4afc6ffe" +dependencies = [ + "phf_generator 0.14.0", + "phf_shared 0.14.0", +] + +[[package]] +name = "phf_generator" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +dependencies = [ + "fastrand", + "phf_shared 0.13.1", +] + +[[package]] +name = "phf_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aeb62e0959d5a1bebc965f4d15d9e2b7cea002b6b0f5ba8cde6cc26738467100" +dependencies = [ + "fastrand", + "phf_shared 0.14.0", +] + +[[package]] +name = "phf_macros" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f032b54b1e759ccd5f8b6677695d5268c588701effba24601f6932f8269ef" +dependencies = [ + "phf_generator 0.13.1", + "phf_shared 0.13.1", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "phf_macros" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fa8d0ca26d424d27630da600c6624696e7dec8bf7b3b492b383c5dc49e5e085" +dependencies = [ + "phf_generator 0.14.0", + "phf_shared 0.14.0", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher", +] + +[[package]] +name = "phf_shared" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fd9027e2d9319be6349febd1db4e8d02aa544921200c9b777720ac34a3aa89" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "plist" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327a4cb2ad49dc48adfdc9da89e22c64d488b508c67a044d71ba729e4521a285" +dependencies = [ + "base64 0.23.1", + "indexmap 2.14.2", + "quick-xml", + "serde", + "time", +] + +[[package]] +name = "png" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61" +dependencies = [ + "bitflags 2.13.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.8.9", +] + +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "portable-atomic-util" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + +[[package]] +name = "proc-macro-crate" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +dependencies = [ + "once_cell", + "toml_edit 0.19.15", +] + +[[package]] +name = "proc-macro-crate" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b00f26d3400549137f92511a46ac1cd8ce37cb5598a96d382381458b992a5d24" +dependencies = [ + "toml_datetime 0.6.3", + "toml_edit 0.20.2", +] + +[[package]] +name = "proc-macro-crate" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f" +dependencies = [ + "toml_edit 0.25.19+spec-1.1.0", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quick-xml" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b1177fdf999d2321d3fb46ff47159d9c1fb9ad66a4879f8c50a0b504615e9b" +dependencies = [ + "memchr", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "raw-window-handle" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "redox_users" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60dc65c0ff1a7ae1294b0c67b9f14baf70b644404010370171787bfac1038fc0" +dependencies = [ + "libredox", + "thiserror 2.0.21", +] + +[[package]] +name = "ref-cast" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e440fb4e4b4147295338efb76001ab9e4efc0e5839df2c47fc5ac2381d365c3" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "reqwest" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16a1cfa75cc186dd73d5818e510e042e40927bccc9c236b061cea97e1eb08029" +dependencies = [ + "base64 0.23.1", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-tls", + "hyper-util", + "js-sys", + "log", + "native-tls", + "percent-encoding", + "pin-project-lite", + "rustls-pki-types", + "serde", + "serde_json", + "sync_wrapper", + "tokio", + "tokio-native-tls", + "tokio-util", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", +] + +[[package]] +name = "rusqlite" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f172c4812a19b26d1c23cdb0070e69db335503dfa378b910eb857386e5b8beba" +dependencies = [ + "bitflags 2.13.2", + "fallible-iterator", + "fallible-streaming-iterator", + "libsqlite3-sys", + "smallvec", +] + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags 2.13.2", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls-pki-types" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "102f35f7de7b06d8c61f1cff6592f5253e0dea431c74867aad5dfd70f44663fb" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "schannel" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cb23c424d8ee841bb4d813677ca237e2a610067d594fec130f87d6093495ab5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "schemars" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fbf2ae1b8bc8e02df939598064d22402220cd5bbcca1c76f7d6a310974d5615" +dependencies = [ + "dyn-clone", + "indexmap 1.9.3", + "schemars_derive", + "serde", + "serde_json", + "url", + "uuid", +] + +[[package]] +name = "schemars" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd191f9397d57d581cddd31014772520aa448f65ef991055d7f61582c65165f" +dependencies = [ + "dyn-clone", + "ref-cast", + "serde", + "serde_json", +] + +[[package]] +name = "schemars" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab508826f74a77ca9d5aba6ff19b522583ee3eaf28a19384ff3d0e5835fadf6e" +dependencies = [ + "dyn-clone", + "ref-cast", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32e265784ad618884abaea0600a9adf15393368d840e0222d101a072f3f7534d" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 2.0.119", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "secret-service" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5107b24b91445dd2aa449a258a1807b63240942157292354dc5bfdbeb8bc6db8" +dependencies = [ + "aes", + "cbc", + "futures-util", + "getrandom 0.4.3", + "hkdf", + "hybrid-array", + "num", + "once_cell", + "serde", + "sha2 0.11.1", + "zbus", +] + +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags 2.13.2", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "selectors" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8adfa1c298912827b8a28b223b3b874357397ae706e6190acd9bf28cee99114d" +dependencies = [ + "bitflags 2.13.2", + "cssparser 0.37.0", + "derive_more", + "log", + "new_debug_unreachable", + "phf 0.13.1", + "phf_codegen 0.13.1", + "precomputed-hash", + "rustc-hash", + "servo_arc 0.4.3", + "smallvec", +] + +[[package]] +name = "selectors" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8322aebea94a6cc288ff884e8a49bdbceef60206c400c6c46d22e62fc4fd91a6" +dependencies = [ + "bitflags 2.13.2", + "cssparser 0.38.0", + "derive_more", + "log", + "new_debug_unreachable", + "precomputed-hash", + "rustc-hash", + "servo_arc 0.5.0", + "smallvec", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde-untagged" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9faf48a4a2d2693be24c6289dbe26552776eb7737074e6722891fadbe6c5058" +dependencies = [ + "erased-serde", + "serde", + "serde_core", + "typeid", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_derive_internals" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_repr" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d3b1629de253c70a0508c3899572da79ca359fdab27c7920ff00406df418906" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_spanned" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c187e0a369471c550af028cba56d4f4fa2a489ad28151ae95a4d340a2a8b782" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_with" +version = "3.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9adc193c780ef8f159aee8b61e2d5801aaa555e6eb0947fe45530ec506296f" +dependencies = [ + "base64 0.23.1", + "bs58", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.14.2", + "jiff", + "schemars 0.9.0", + "schemars 1.2.3", + "serde_core", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e17bbc68e28663bbbb90df47e058aa7eda4fb445b89fe70457bb94fbccf6e49" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serialize-to-javascript" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04f3666a07a197cdb77cdf306c32be9b7f598d7060d50cfd4d5aa04bfd92f6c5" +dependencies = [ + "serde", + "serde_json", + "serialize-to-javascript-impl", +] + +[[package]] +name = "serialize-to-javascript-impl" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "772ee033c0916d670af7860b6e1ef7d658a4629a6d0b4c8c3e67f09b3765b75d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "servo_arc" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "170fb83ab34de17dc69aa7c67482b22218ddb85da56546f9bd6b929e32a05930" +dependencies = [ + "stable_deref_trait", +] + +[[package]] +name = "servo_arc" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4eceb519ec34d8290aaf6e46f435102f2bfa63f22b05a144fb16ab266d3b198" +dependencies = [ + "stable_deref_trait", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "softbuffer" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aac18da81ebbf05109ab275b157c22a653bb3c12cf884450179942f81bcbf6c3" +dependencies = [ + "bytemuck", + "js-sys", + "ndk", + "objc2", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation", + "objc2-quartz-core", + "raw-window-handle", + "redox_syscall", + "tracing", + "wasm-bindgen", + "web-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "soup3" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "471f924a40f31251afc77450e781cb26d55c0b650842efafc9c6cbd2f7cc4f9f" +dependencies = [ + "futures-channel", + "gio", + "glib", + "libc", + "soup3-sys", +] + +[[package]] +name = "soup3-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ebe8950a680a12f24f15ebe1bf70db7af98ad242d9db43596ad3108aab86c27" +dependencies = [ + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "static_vcruntime" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff7589a1859b09232522bb9edf076ca7fcf14ded96685d3fa629db73023ffbb7" + +[[package]] +name = "string_cache" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18596f8c785a729f2819c0f6a7eae6ebeebdfffbfe4214ae6b087f690e31901" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared 0.13.1", + "precomputed-hash", +] + +[[package]] +name = "string_cache" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffa8a5dbe8b3f0bbe29d4c3225daafaeead63afdc1b65fc4c01a1384166038e6" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared 0.14.0", + "precomputed-hash", +] + +[[package]] +name = "string_cache_codegen" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585635e46db231059f76c5849798146164652513eb9e8ab2685939dd90f29b69" +dependencies = [ + "phf_generator 0.13.1", + "phf_shared 0.13.1", + "proc-macro2", + "quote", +] + +[[package]] +name = "string_cache_codegen" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928dcdf75e47626b3617a976ec205d9f057584c371c1f23b782129268d0e6edc" +dependencies = [ + "phf_generator 0.14.0", + "phf_shared 0.14.0", + "proc-macro2", + "quote", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "swift-rs" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cefd87076fd385308ee4aff597256c902f1fbb4d031de1558efc12f8ffefadd9" +dependencies = [ + "base64 0.21.7", + "serde", + "serde_json", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "system-deps" +version = "6.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e535eb8dded36d55ec13eddacd30dec501792ff23a0b1682c38601b8cf2349" +dependencies = [ + "cfg-expr", + "heck 0.5.0", + "pkg-config", + "toml 0.8.2", + "version-compare", +] + +[[package]] +name = "tao" +version = "0.37.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5777b06fe6435126ae8d816eba269e050e1e10d302cf5461e9c49eccabc3548" +dependencies = [ + "bitflags 2.13.2", + "block2", + "core-foundation", + "core-graphics", + "crossbeam-channel", + "dispatch2", + "dlopen2", + "dpi", + "gdkwayland-sys", + "gdkx11-sys", + "gtk", + "jni", + "libc", + "log", + "ndk", + "ndk-context", + "ndk-sys", + "objc2", + "objc2-app-kit", + "objc2-foundation", + "objc2-ui-kit", + "parking_lot", + "percent-encoding", + "raw-window-handle", + "tao-macros", + "unicode-segmentation", + "url", + "windows", + "windows-core", + "windows-version", + "x11-dl", +] + +[[package]] +name = "tao-macros" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f7eeb6d99155545da6150a1795945f16ac9c178deb2a5f2e74d776107bd5849" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + +[[package]] +name = "tauri" +version = "2.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c2a5cad3827d1fa46c206a6bcaee8ca14203e9b0aaabe447dd4acef33ecd065" +dependencies = [ + "anyhow", + "bytes", + "cookie", + "dirs", + "dunce", + "embed_plist", + "getrandom 0.3.4", + "glob", + "gtk", + "heck 0.5.0", + "http", + "jni", + "libc", + "log", + "mime", + "muda", + "objc2", + "objc2-app-kit", + "objc2-foundation", + "objc2-ui-kit", + "objc2-web-kit", + "percent-encoding", + "plist", + "raw-window-handle", + "reqwest", + "serde", + "serde_json", + "serde_repr", + "serialize-to-javascript", + "swift-rs", + "tauri-build", + "tauri-macros", + "tauri-runtime", + "tauri-runtime-wry", + "tauri-utils", + "thiserror 2.0.21", + "tokio", + "url", + "webkit2gtk", + "webview2-com", + "window-vibrancy", + "windows", +] + +[[package]] +name = "tauri-build" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e03658bfc9cb60f96a05b7d07a7f7cdfbf4c30b1bbfcb5cf6b5342cc2c9e0bc" +dependencies = [ + "anyhow", + "cargo_toml", + "dirs", + "glob", + "heck 0.5.0", + "json-patch", + "schemars 0.8.22", + "semver", + "serde", + "serde_json", + "static_vcruntime", + "strsim", + "tauri-utils", + "tauri-winres", + "walkdir", +] + +[[package]] +name = "tauri-codegen" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c454a15120b885bf57d0cb9ab218c01e12bc672f793452cb6527b460db883aa" +dependencies = [ + "base64 0.23.1", + "ico", + "json-patch", + "plist", + "png", + "proc-macro2", + "quote", + "semver", + "serde", + "serde_json", + "sha2 0.11.1", + "syn 2.0.119", + "tauri-utils", + "thiserror 2.0.21", + "time", + "url", + "uuid", + "walkdir", +] + +[[package]] +name = "tauri-macros" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8f7513896f637528ec27ff76effd90638b1cdfa77499fae3db078043aed7ecb" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.119", + "tauri-codegen", + "tauri-utils", +] + +[[package]] +name = "tauri-runtime" +version = "2.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c4b2a71c4bf51cde5cdcf1b45d7943282c7c669d3d26f7a324cce25f1b53e75" +dependencies = [ + "cookie", + "dpi", + "gtk", + "http", + "jni", + "objc2", + "objc2-ui-kit", + "objc2-web-kit", + "raw-window-handle", + "serde", + "serde_json", + "tauri-utils", + "thiserror 2.0.21", + "url", + "webkit2gtk", + "webview2-com", + "windows", +] + +[[package]] +name = "tauri-runtime-wry" +version = "2.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d9be5e7af9560a4a60cdd441389a066cdd3e7c5d464a006ae34cb276cc78abd" +dependencies = [ + "gtk", + "http", + "jni", + "log", + "objc2", + "objc2-app-kit", + "percent-encoding", + "raw-window-handle", + "softbuffer", + "tao", + "tauri-runtime", + "tauri-utils", + "url", + "webkit2gtk", + "webview2-com", + "windows", + "wry", +] + +[[package]] +name = "tauri-utils" +version = "2.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bf649f12d6fb11e1867214973cd37d89786ae2c0904f79b787b1db4fdcd14c" +dependencies = [ + "anyhow", + "cargo_metadata", + "ctor", + "dom_query 0.28.0", + "dunce", + "glob", + "http", + "infer", + "json-patch", + "log", + "memchr", + "phf 0.13.1", + "plist", + "proc-macro2", + "quote", + "regex", + "schemars 0.8.22", + "semver", + "serde", + "serde-untagged", + "serde_json", + "serde_with", + "swift-rs", + "thiserror 2.0.21", + "toml 1.2.1+spec-1.1.0", + "url", + "urlpattern", + "uuid", + "walkdir", +] + +[[package]] +name = "tauri-winres" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c52bb6904475a6ca662f15ef787f18ade61d5740a1ee12c45d3b748efdb6f796" +dependencies = [ + "dunce", + "embed-resource", + "toml 1.2.1+spec-1.1.0", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "tendril" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fed54709c5b3a53d09bb1c113ea4f5ceafd1e772ddcb0030a82e1d56c087b08" +dependencies = [ + "new_debug_unreachable", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl 2.0.21", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ - "futures-core", - "pin-project-lite", + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] -name = "parking" -version = "2.2.1" +name = "thiserror-impl" +version = "2.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] [[package]] -name = "percent-encoding" -version = "2.3.2" +name = "time" +version = "0.3.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] [[package]] -name = "pin-project-lite" -version = "0.2.17" +name = "time-core" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" [[package]] -name = "pkg-config" -version = "0.3.34" +name = "time-macros" +version = "0.2.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" +checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85" +dependencies = [ + "num-conv", + "time-core", +] [[package]] -name = "potential_utf" -version = "0.1.6" +name = "tinystr" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" dependencies = [ + "displaydoc", "zerovec", ] [[package]] -name = "powerfmt" -version = "0.2.1" +name = "tinyvec" +version = "1.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" +checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" [[package]] -name = "proc-macro-crate" -version = "3.5.0" +name = "tokio" +version = "1.54.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f" +checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" dependencies = [ - "toml_edit", + "bytes", + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tracing", + "windows-sys 0.61.2", ] [[package]] -name = "proc-macro2" -version = "1.0.107" +name = "tokio-native-tls" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" dependencies = [ - "unicode-ident", + "native-tls", + "tokio", ] [[package]] -name = "quote" -version = "1.0.47" +name = "tokio-util" +version = "0.7.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" dependencies = [ - "proc-macro2", + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", ] [[package]] -name = "r-efi" -version = "6.0.0" +name = "toml" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" +dependencies = [ + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.3", + "toml_edit 0.20.2", +] [[package]] -name = "redox_users" +name = "toml" +version = "1.2.1+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5755c51863cdac711f1956baf8eb9588e060755f7d3c4c979e70ea1e1f32cd7" +dependencies = [ + "indexmap 2.14.2", + "serde_core", + "serde_spanned 1.1.3", + "toml_datetime 1.1.3+spec-1.1.0", + "toml_parser", + "toml_writer", + "winnow 1.0.5", +] + +[[package]] +name = "toml_datetime" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_datetime" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173f25c883cd2176a2d8212155638c0a60d484a9d84546d270866f40f56b6e49" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap 2.14.2", + "toml_datetime 0.6.3", + "winnow 0.5.40", +] + +[[package]] +name = "toml_edit" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" +dependencies = [ + "indexmap 2.14.2", + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.3", + "winnow 0.5.40", +] + +[[package]] +name = "toml_edit" +version = "0.25.19+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd43688c4858d7264ae1cf9a9f874a67c3feb74a883b1cd7bebd6a4ea8307218" +dependencies = [ + "indexmap 2.14.2", + "toml_datetime 1.1.3+spec-1.1.0", + "toml_parser", + "winnow 1.0.5", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow 1.0.5", +] + +[[package]] +name = "toml_writer" +version = "1.2.0+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "009dbb7700f11c55ef83aebbe4c0e9cf1f1ac0844bd8d6f00dc38f302026e13a" + +[[package]] +name = "tower" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60dc65c0ff1a7ae1294b0c67b9f14baf70b644404010370171787bfac1038fc0" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" dependencies = [ - "libredox", - "thiserror", + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", ] [[package]] -name = "reqwest" -version = "0.13.5" +name = "tower-http" +version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16a1cfa75cc186dd73d5818e510e042e40927bccc9c236b061cea97e1eb08029" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" dependencies = [ - "base64", + "bitflags 2.13.2", "bytes", - "futures-channel", - "futures-core", "futures-util", "http", "http-body", - "http-body-util", - "hyper", - "hyper-tls", - "hyper-util", - "js-sys", - "log", - "native-tls", - "percent-encoding", "pin-project-lite", - "rustls-pki-types", - "serde", - "serde_json", - "sync_wrapper", - "tokio", - "tokio-native-tls", "tower", - "tower-http", + "tower-layer", "tower-service", "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", ] [[package]] -name = "rustix" -version = "1.1.6" +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "typeid" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "uds_windows" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", + "memoffset", + "tempfile", + "windows-sys 0.61.2", ] [[package]] -name = "rustls-pki-types" -version = "1.16.0" +name = "unicode-ident" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "102f35f7de7b06d8c61f1cff6592f5253e0dea431c74867aad5dfd70f44663fb" -dependencies = [ - "zeroize", -] +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" [[package]] -name = "rustversion" -version = "1.0.23" +name = "unicode-segmentation" +version = "1.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" +checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" [[package]] -name = "schannel" -version = "0.1.30" +name = "url" +version = "2.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cb23c424d8ee841bb4d813677ca237e2a610067d594fec130f87d6093495ab5" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" dependencies = [ - "windows-sys", + "form_urlencoded", + "idna", + "percent-encoding", + "serde", + "serde_derive", ] [[package]] -name = "secret-service" -version = "5.2.0" +name = "urlpattern" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5107b24b91445dd2aa449a258a1807b63240942157292354dc5bfdbeb8bc6db8" +checksum = "df16f50ef4cc145211879a3867ba757076b25dfee812040dcb0658bd9ae7904b" dependencies = [ - "aes", - "cbc", - "futures-util", - "getrandom", - "hkdf", - "hybrid-array", - "num", - "once_cell", + "icu_properties", + "regex", "serde", - "sha2", - "zbus", + "url", ] [[package]] -name = "security-framework" -version = "3.7.0" +name = "utf8_iter" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +checksum = "af3bd9d3e571c26fcce9d033b87ea5ab061b8d2ffe9b7a5675d810d68f03edf0" dependencies = [ - "bitflags", - "core-foundation", - "core-foundation-sys", - "libc", - "security-framework-sys", + "utf_types", ] [[package]] -name = "security-framework-sys" -version = "2.17.0" +name = "utf8parse" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" -dependencies = [ - "core-foundation-sys", - "libc", -] +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] -name = "serde" -version = "1.0.229" +name = "utf_types" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" -dependencies = [ - "serde_core", - "serde_derive", -] +checksum = "3a3b425286980a367278453f3b6d28cdcb134b574e5a35824a6ff5eebad50bb6" [[package]] -name = "serde_core" -version = "1.0.229" +name = "uuid" +version = "1.29.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +checksum = "4a06f08a856b009bc7bbaf7d3e79808595d032dd19074c0c2ef26e49e03e6bdc" dependencies = [ - "serde_derive", + "getrandom 0.4.3", + "js-sys", + "serde_core", + "wasm-bindgen", ] [[package]] -name = "serde_derive" -version = "1.0.229" +name = "vcpkg" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] -name = "serde_json" -version = "1.0.154" +name = "version-compare" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" -dependencies = [ - "itoa", - "memchr", - "serde", - "serde_core", - "zmij", -] +checksum = "03c2856837ef78f57382f06b2b8563a2f512f7185d732608fd9176cb3b8edf0e" [[package]] -name = "serde_repr" -version = "0.1.21" +name = "version_check" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3b1629de253c70a0508c3899572da79ca359fdab27c7920ff00406df418906" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] -name = "sha2" -version = "0.11.1" +name = "vswhom" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +checksum = "be979b7f07507105799e854203b470ff7c78a1639e330a58f183b5fea574608b" dependencies = [ - "cfg-if", - "cpufeatures", - "digest", + "libc", + "vswhom-sys", ] [[package]] -name = "shlex" -version = "2.0.1" +name = "vswhom-sys" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +checksum = "fb067e4cbd1ff067d1df46c9194b5de0e98efd2810bbc95c5d5e5f25a3231150" +dependencies = [ + "cc", + "libc", +] [[package]] -name = "signal-hook-registry" -version = "1.4.8" +name = "walkdir" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ - "errno", - "libc", + "same-file", + "winapi-util", ] [[package]] -name = "slab" -version = "0.4.12" +name = "want" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +checksum = "ec4cdd0dd910afe868b7ef477227d8d538b46b3075031afee8a9f2acb0a2ed0b" +dependencies = [ + "try-lock", +] [[package]] -name = "smallvec" -version = "1.16.5" +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" [[package]] -name = "socket2" -version = "0.6.5" +name = "wasip2" +version = "1.0.4+wasi-0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" dependencies = [ - "libc", - "windows-sys", + "wit-bindgen", ] [[package]] -name = "stable_deref_trait" -version = "1.2.1" +name = "wasm-bindgen" +version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] [[package]] -name = "strsim" -version = "0.11.1" +name = "wasm-bindgen-futures" +version = "0.4.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +checksum = "5e14e0327cb5368ad60ccb22d04a9827a693c95a926d8d125e9fa39e0baec501" +dependencies = [ + "js-sys", + "tokio", + "wasm-bindgen", +] [[package]] -name = "syn" -version = "2.0.119" +name = "wasm-bindgen-macro" +version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" dependencies = [ - "proc-macro2", "quote", - "unicode-ident", + "wasm-bindgen-macro-support", ] [[package]] -name = "syn" -version = "3.0.9" +name = "wasm-bindgen-macro-support" +version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" dependencies = [ + "bumpalo", "proc-macro2", "quote", - "unicode-ident", + "syn 3.0.9", + "wasm-bindgen-shared", ] [[package]] -name = "sync_wrapper" -version = "1.0.2" +name = "wasm-bindgen-shared" +version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" dependencies = [ - "futures-core", + "unicode-ident", ] [[package]] -name = "synstructure" -version = "0.14.0" +name = "wasm-streams" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +checksum = "9d1ec4f6517c9e11ae630e200b2b65d193279042e28edd4a2cda233e46670bbb" dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", ] [[package]] -name = "tempfile" -version = "3.27.0" +name = "web-sys" +version = "0.3.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +checksum = "d7d179596ff5ec0f6de4db64d54011bde6289cc49603d3a6e4ce3f329207ab88" dependencies = [ - "fastrand", - "getrandom", - "once_cell", - "rustix", - "windows-sys", + "js-sys", + "wasm-bindgen", ] [[package]] -name = "thiserror" -version = "2.0.21" +name = "web-time" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" dependencies = [ - "thiserror-impl", + "js-sys", + "wasm-bindgen", ] [[package]] -name = "thiserror-impl" -version = "2.0.21" +name = "web_atoms" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +checksum = "ba8b815c1b593dc0baf78dd0f4fc8fdb2de53198fb1163738093e9a311c33fb3" dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", + "phf 0.13.1", + "phf_codegen 0.13.1", + "string_cache 0.9.0", + "string_cache_codegen 0.6.1", ] [[package]] -name = "time" -version = "0.3.55" +name = "web_atoms" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +checksum = "7572660c8890448ba236b7376f27e389c6a7e1c70195622faced601f855c0ada" dependencies = [ - "deranged", - "num-conv", - "powerfmt", - "serde_core", - "time-core", - "time-macros", + "phf 0.14.0", + "phf_codegen 0.14.0", + "string_cache 0.11.0", + "string_cache_codegen 0.11.2", ] [[package]] -name = "time-core" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" - -[[package]] -name = "time-macros" -version = "0.2.32" +name = "webkit2gtk" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85" +checksum = "a1027150013530fb2eaf806408df88461ae4815a45c541c8975e61d6f2fc4793" dependencies = [ - "num-conv", - "time-core", + "bitflags 1.3.2", + "cairo-rs", + "gdk", + "gdk-sys", + "gio", + "gio-sys", + "glib", + "glib-sys", + "gobject-sys", + "gtk", + "gtk-sys", + "javascriptcore-rs", + "libc", + "once_cell", + "soup3", + "webkit2gtk-sys", ] [[package]] -name = "tinystr" -version = "0.8.5" +name = "webkit2gtk-sys" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" +checksum = "916a5f65c2ef0dfe12fff695960a2ec3d4565359fdbb2e9943c974e06c734ea5" dependencies = [ - "displaydoc", - "zerovec", + "bitflags 1.3.2", + "cairo-sys-rs", + "gdk-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "gtk-sys", + "javascriptcore-rs-sys", + "libc", + "pkg-config", + "soup3-sys", + "system-deps", ] [[package]] -name = "tokio" -version = "1.54.1" +name = "webview2-com" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" +checksum = "3f89fca7a704cee10dcb3654c1dbb8941d1783132f1917358af75bec37a7d7e6" dependencies = [ - "bytes", - "libc", - "mio", - "pin-project-lite", - "signal-hook-registry", - "socket2", - "tracing", - "windows-sys", + "webview2-com-macros", + "webview2-com-sys", + "windows", + "windows-core", ] [[package]] -name = "tokio-native-tls" -version = "0.3.1" +name = "webview2-com-macros" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +checksum = "67a921c1b6914c367b2b823cd4cde6f96beec77d30a939c8199bb377cf9b9b54" dependencies = [ - "native-tls", - "tokio", + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] -name = "toml_datetime" -version = "1.1.3+spec-1.1.0" +name = "webview2-com-sys" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "173f25c883cd2176a2d8212155638c0a60d484a9d84546d270866f40f56b6e49" +checksum = "b3a07132775117d6065853d9d1178157b8c90e228de47129d6bce2c7edebedfb" dependencies = [ - "serde_core", + "thiserror 2.0.21", + "windows", + "windows-core", ] [[package]] -name = "toml_edit" -version = "0.25.19+spec-1.1.0" +name = "winapi" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd43688c4858d7264ae1cf9a9f874a67c3feb74a883b1cd7bebd6a4ea8307218" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ - "indexmap", - "toml_datetime", - "toml_parser", - "winnow", + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", ] [[package]] -name = "toml_parser" -version = "1.1.5+spec-1.1.0" +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "winnow", + "windows-sys 0.61.2", ] [[package]] -name = "tower" -version = "0.5.3" +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "window-vibrancy" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "111e51caca442cafd9bab396628ac4d814880eaea4e63dfd76a12e9f342b5580" dependencies = [ - "futures-core", - "futures-util", - "pin-project-lite", - "sync_wrapper", - "tokio", - "tower-layer", - "tower-service", + "objc2", + "objc2-app-kit", + "objc2-core-foundation", + "objc2-foundation", + "objc2-quartz-core", + "raw-window-handle", + "windows-sys 0.61.2", + "windows-version", ] [[package]] -name = "tower-http" -version = "0.6.11" +name = "windows" +version = "0.62.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" dependencies = [ - "bitflags", - "bytes", - "futures-util", - "http", - "http-body", - "pin-project-lite", - "tower", - "tower-layer", - "tower-service", - "url", + "windows-collections", + "windows-core", + "windows-future", + "windows-numerics", ] [[package]] -name = "tower-layer" -version = "0.3.3" +name = "windows-collections" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" +checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610" +dependencies = [ + "windows-core", +] [[package]] -name = "tower-service" -version = "0.3.3" +name = "windows-core" +version = "0.62.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] [[package]] -name = "tracing" -version = "0.1.44" +name = "windows-future" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" dependencies = [ - "pin-project-lite", - "tracing-attributes", - "tracing-core", + "windows-core", + "windows-link", + "windows-threading", ] [[package]] -name = "tracing-attributes" -version = "0.1.31" +name = "windows-implement" +version = "0.60.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" dependencies = [ "proc-macro2", "quote", @@ -1648,203 +4810,246 @@ dependencies = [ ] [[package]] -name = "tracing-core" -version = "0.1.36" +name = "windows-interface" +version = "0.59.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" dependencies = [ - "once_cell", + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] -name = "try-lock" -version = "0.2.5" +name = "windows-link" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" [[package]] -name = "typenum" -version = "1.20.1" +name = "windows-numerics" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" +dependencies = [ + "windows-core", + "windows-link", +] [[package]] -name = "uds_windows" -version = "1.2.1" +name = "windows-result" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" dependencies = [ - "memoffset", - "tempfile", - "windows-sys", + "windows-link", ] [[package]] -name = "unicode-ident" -version = "1.0.27" +name = "windows-strings" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] [[package]] -name = "url" -version = "2.5.8" +name = "windows-sys" +version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", - "serde", + "windows-targets", ] [[package]] -name = "utf8_iter" -version = "1.1.0" +name = "windows-sys" +version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af3bd9d3e571c26fcce9d033b87ea5ab061b8d2ffe9b7a5675d810d68f03edf0" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "utf_types", + "windows-link", ] [[package]] -name = "utf8parse" -version = "0.2.2" +name = "windows-targets" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] [[package]] -name = "utf_types" -version = "1.0.0" +name = "windows-threading" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a3b425286980a367278453f3b6d28cdcb134b574e5a35824a6ff5eebad50bb6" +checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" +dependencies = [ + "windows-link", +] [[package]] -name = "uuid" -version = "1.29.1" +name = "windows-version" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a06f08a856b009bc7bbaf7d3e79808595d032dd19074c0c2ef26e49e03e6bdc" +checksum = "e4060a1da109b9d0326b7262c8e12c84df67cc0dbc9e33cf49e01ccc2eb63631" dependencies = [ - "getrandom", - "js-sys", - "serde_core", - "wasm-bindgen", + "windows-link", ] [[package]] -name = "vcpkg" -version = "0.2.15" +name = "windows_aarch64_gnullvm" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" [[package]] -name = "want" -version = "0.3.2" +name = "windows_aarch64_msvc" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec4cdd0dd910afe868b7ef477227d8d538b46b3075031afee8a9f2acb0a2ed0b" -dependencies = [ - "try-lock", -] +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" [[package]] -name = "wasi" -version = "0.11.1+wasi-snapshot-preview1" +name = "windows_i686_gnu" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" [[package]] -name = "wasm-bindgen" -version = "0.2.130" +name = "windows_i686_msvc" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" -dependencies = [ - "cfg-if", - "rustversion", - "wasm-bindgen-macro", - "wasm-bindgen-shared", -] +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" [[package]] -name = "wasm-bindgen-futures" -version = "0.4.80" +name = "windows_x86_64_gnu" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e14e0327cb5368ad60ccb22d04a9827a693c95a926d8d125e9fa39e0baec501" -dependencies = [ - "js-sys", - "tokio", - "wasm-bindgen", -] +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" [[package]] -name = "wasm-bindgen-macro" -version = "0.2.130" +name = "windows_x86_64_gnullvm" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" [[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.130" +name = "windows_x86_64_msvc" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" dependencies = [ - "bumpalo", - "proc-macro2", - "quote", - "syn 3.0.9", - "wasm-bindgen-shared", + "memchr", ] [[package]] -name = "wasm-bindgen-shared" -version = "0.2.130" +name = "winnow" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +checksum = "8c3b93199df0ab874cdf0c6a94f0a0dc3423fe41f1cafb62b8bd7b1deb277aef" dependencies = [ - "unicode-ident", + "memchr", ] [[package]] -name = "web-sys" -version = "0.3.107" +name = "winreg" +version = "0.56.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7d179596ff5ec0f6de4db64d54011bde6289cc49603d3a6e4ce3f329207ab88" +checksum = "7d6f32a0ff4a9f6f01231eb2059cc85479330739333e0e58cadf03b6af2cca10" dependencies = [ - "js-sys", - "wasm-bindgen", + "cfg-if", + "windows-sys 0.61.2", ] [[package]] -name = "windows-link" -version = "0.2.1" +name = "wit-bindgen" +version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" [[package]] -name = "windows-sys" -version = "0.61.2" +name = "writeable" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "wry" +version = "0.57.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a490119196d02a51d0871df7b9119a1f3e46d75b7e90053a801454d5c6b3491c" dependencies = [ - "windows-link", + "base64 0.23.1", + "block2", + "cookie", + "crossbeam-channel", + "dirs", + "dom_query 0.29.0", + "dpi", + "dunce", + "gdkx11", + "gtk", + "http", + "javascriptcore-rs", + "jni", + "libc", + "ndk", + "objc2", + "objc2-app-kit", + "objc2-core-foundation", + "objc2-foundation", + "objc2-ui-kit", + "objc2-web-kit", + "percent-encoding", + "raw-window-handle", + "sha2 0.10.9", + "soup3", + "tao-macros", + "thiserror 2.0.21", + "url", + "webkit2gtk", + "webkit2gtk-sys", + "webview2-com", + "windows", + "windows-core", + "windows-version", + "x11-dl", ] [[package]] -name = "winnow" -version = "1.0.5" +name = "x11" +version = "2.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c3b93199df0ab874cdf0c6a94f0a0dc3423fe41f1cafb62b8bd7b1deb277aef" +checksum = "502da5464ccd04011667b11c435cb992822c2c0dbde1770c988480d312a0db2e" dependencies = [ - "memchr", + "libc", + "pkg-config", ] [[package]] -name = "writeable" -version = "0.6.4" +name = "x11-dl" +version = "2.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" +checksum = "38735924fedd5314a6e548792904ed8c6de6636285cb9fec04d5b1db85c1516f" +dependencies = [ + "libc", + "once_cell", + "pkg-config", +] [[package]] name = "yoke" @@ -1892,8 +5097,8 @@ dependencies = [ "tracing", "uds_windows", "uuid", - "windows-sys", - "winnow", + "windows-sys 0.61.2", + "winnow 1.0.5", "zbus_macros", "zbus_names", "zvariant", @@ -1905,7 +5110,7 @@ version = "5.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2990635d09ade6df1868f72f8cac69a876a90981e8bd3c40b1be413f8dc88f40" dependencies = [ - "proc-macro-crate", + "proc-macro-crate 3.5.0", "proc-macro2", "quote", "syn 3.0.9", @@ -1921,7 +5126,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "812a12be4ead7a59e18ea8f047d08add0066b73bc62fc1467066a2472ae088dd" dependencies = [ "serde", - "winnow", + "winnow 1.0.5", "zvariant", ] @@ -1994,6 +5199,12 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + [[package]] name = "zmij" version = "1.0.23" @@ -2009,7 +5220,7 @@ dependencies = [ "endi", "enumflags2", "serde", - "winnow", + "winnow 1.0.5", "zcheapstr", "zvariant_derive", "zvariant_utils", @@ -2021,7 +5232,7 @@ version = "5.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef2672456ba1cb54b5eaec136e63135b3c7495fb938e53b28a62e633496d486d" dependencies = [ - "proc-macro-crate", + "proc-macro-crate 3.5.0", "proc-macro2", "quote", "syn 3.0.9", @@ -2038,5 +5249,5 @@ dependencies = [ "quote", "serde", "syn 3.0.9", - "winnow", + "winnow 1.0.5", ] diff --git a/Cargo.toml b/Cargo.toml index 23c91cf..604c080 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [workspace] resolver = "3" -members = ["crates/omgcode-core", "crates/omgcode-cli", "crates/omgcode-platform"] +members = ["crates/omgcode-core", "crates/omgcode-cli", "crates/omgcode-platform", "crates/omgcode-app", "crates/omgcode-desktop"] [workspace.package] -version = "0.28.0" +version = "0.33.0" edition = "2024" rust-version = "1.99.1" license = "Apache-2.0" @@ -25,6 +25,8 @@ tempfile = "3" libc = "0.2.191" rustix = { version = "1.1.6", features = ["fs", "process"] } zeroize = { version = "1.8", default-features = false, features = ["alloc"] } +# Derived session index cache (ADR pending). Bundled SQLite; no extension loading. +rusqlite = { version = "0.43.0", default-features = false, features = ["bundled"] } secret-service = { version = "5.2", default-features = false, features = ["rt-tokio-crypto-rust"] } # Pinned to the version secret-service already uses, with the same features, # for raw Secret Service calls that refuse prompts (ADR-0042). diff --git a/IMPLEMENTATION_PLAN.md b/IMPLEMENTATION_PLAN.md index 8412a0c..ce11dec 100644 --- a/IMPLEMENTATION_PLAN.md +++ b/IMPLEMENTATION_PLAN.md @@ -1,6 +1,6 @@ # OMGCode implementation plan -Last verified: 2026-11-15. +Last verified: 2026-11-16. This guide is part of `main` after PR #9. Read it before selecting the next increment. Checkpoint preparation does not mean native replacement exists. @@ -66,21 +66,45 @@ This is a resume guide, not a replacement for the owning contracts in its native end-to-end acceptance test (C15). - **PR #31** merged at `28da2cf` on 2026-11-15: PR #30 review fixes, including asynchronous package-fetch file I/O. -- This branch, `codex/context-compaction`: **Phase C16**, version **0.26.0**, - with manual and pre-turn automatic context compaction (ADR-0041). It starts - from `97c733e`, the PR #31 head. No run accepts a profile or has - destination-scoped networking; no provider adapter is enabled, and the - command tool is the only model tool that can request a process. -- Commit `e88df5b` on this branch: **Phase D1**, version **0.27.0**, the - provider credential service (ADR-0042). No adapter reads a stored credential. -- This change: **Phase C17**, version **0.28.0**, the approval-gated model - edit tool (ADR-0043). It writes only through the checkpointed apply after an - approved, writers-stopped decision. +- **PR #32** merged at `60430ee` on 2026-11-16, from + `codex/context-compaction`. The merge was verified through GitHub. It + carried three phases: + - **Phase C16**, version **0.26.0**, with manual and pre-turn automatic + context compaction (ADR-0041). No run accepts a profile or has + destination-scoped networking, and no provider adapter is enabled. + - **Phase D1**, version **0.27.0**, the provider credential service + (ADR-0042). No adapter reads a stored credential. + - **Phase C17**, version **0.28.0**, the approval-gated model edit tool + (ADR-0043). It writes only through the checkpointed apply after an + approved, writers-stopped decision. +- Branch `codex/desktop-interface`: **Phase E**, version **0.29.0**, the + desktop interface and session tree (ADR-0044, ADR-0045, ADR-0046). Unlike + the entries above, it is **not merged and has no pull request yet**. It is a + prototype whose windows have run only on Windows; on Linux it builds and its + tests pass. See [the desktop interface guide](docs/desktop-interface.md). +- Same branch, after the Phase E commit `cbbec85`: **Phase D2**, version + **0.30.0**, which lets the local LM Studio adapter read its token from a + stored credential through an operator-selected profile (ADR-0047). It is + **not merged and has no pull request**. +- Same branch, after the Phase D2 commit `b9f7d11`: **Phase D3**, version + **0.31.0**, which binds a stored credential to its profile's endpoint + (ADR-0048). A profile can record a loopback endpoint, and a selected profile + supplies both the endpoint and the token. It is **not merged and has no pull + request**. +- Same branch, after the Phase D3 commit `19b3747`: **Phase D4 and E6**, version + **0.32.0**, which streams the local model's reply and pushes chat events to + the desktop window (ADR-0049). It is **not merged and has no pull request**. +- Same branch, after the Phase D4 and E6 commit `02221e9`: **Phase D5**, version + **0.33.0**, which interrupts a streamed model request when a turn is + cancelled (ADR-0050). A cancelled turn is recorded with the failure reason + `cancelled`, and the desktop exit no longer abandons a streamed turn. It is + **not merged and has no pull request**. Implemented today: -- Pinned Rust 1.99.1 workspace with `omgcode-core`, `omgcode-cli`, and - `omgcode-platform`. +- Pinned Rust 1.99.1 workspace with `omgcode-core`, `omgcode-cli`, + `omgcode-platform`, `omgcode-app`, and `omgcode-desktop`. The last two exist + only on the unmerged Phase E branch. - Compiled, versioned base prompt `0.1.4` with a visible digest. - Workspace settings, native directory identity, process-local bindings, and a separate settings/audit journal. @@ -101,7 +125,9 @@ Implemented today: - Read-only `file diff`: trusted CLI preview of a broker-read file against complete proposed UTF-8 content. Exact digests, bounded one-hunk diff, no workspace mutation, no checkpoint, no replacement. -- LM Studio Chat Completions chat with non-streaming requests. It is text-only +- LM Studio Chat Completions chat. Requests are non-streaming by default, and + `chat --stream` and the desktop shell stream the reply as provisional text + (ADR-0049). It is text-only unless the session's workspace enables `model_tools`; then it offers the read and list tools, the approval-gated edit tool when `read` and `write` also allow it, and the approval-gated command tool when `execute`, `read`, and @@ -177,7 +203,11 @@ Implemented today: `workspace provider-credential-set` reads an API key from standard input and binds a new opaque identifier to an `api_key` profile in the control journal; `provider-credential-remove` and `provider-profile-remove` unbind - and then delete the native item. No adapter reads a stored credential. + and then delete the native item. Only the local LM Studio adapter reads a + stored credential, through a selected `lm_studio` profile (ADR-0047). An + `lm_studio` profile can record one endpoint, fixed for its life, and a + selected profile supplies both the endpoint and the token (ADR-0048). No + cloud adapter exists. - The `workspace_run_command` model tool (ADR-0040) is offered only with live `model_tools`, `execute`, `read`, and `write`. Its first call records a pending approval and starts nothing; an approved exact call consumes the @@ -203,12 +233,23 @@ Not implemented: compaction, and standalone CLI checkpoint preparation. - Model create, delete, rename, multi-edit, network, Git, and MCP tools; concurrent-writer detection for any apply route; live provider tool-call - qualification; and streaming, discovery, and cancellation in a live provider. -- A run network destination allowlist, runtime skills, memories, desktop/UI - panels, mid-turn and provider-native compaction, and caches. Nothing calls the unconfined process runner. -- Provider adapters and subscription/API-key login; the recorded provider - profiles are configuration only, and no adapter reads a stored credential. -- Subscription token storage and a memory-only session credential. + qualification; and streaming against a live server, discovery, and + cancellation of a streamed request against a live provider. A non-streamed + request, the compaction request, and a tool call in progress cannot be + interrupted. +- A run network destination allowlist, runtime skills, memories, mid-turn and + provider-native compaction, and caches. Nothing calls the unconfined process + runner. +- Desktop development panels (terminal tabs, task logs, and diffs), a settings + interface, a recovery interface, packaging and installers, and a + Linux run of the desktop shell's windows. The prototype shell, session tree, + search, chat, and approvals window exist on the Phase E branch only. +- Cloud provider adapters and subscription/API-key login. Only the local LM + Studio adapter reads a stored credential, and only at the loopback endpoint + recorded with its profile. No endpoint rule exists for a non-loopback + provider, HTTPS, or certificates. +- Subscription token storage, token refresh, account lifecycle, and a + memory-only session credential. ## Resume steps @@ -237,6 +278,20 @@ Not implemented: ## Verification baseline +Build the desktop frontend before any Cargo step that includes +`omgcode-desktop`. The crate embeds `crates/omgcode-desktop/ui/dist` at compile +time, and `cargo check`, `clippy`, `build`, and `test` fail without it: + +```text +cd crates/omgcode-desktop/ui +npm ci +npm run build +``` + +The frontend checks, run from the same directory, are `npm run check`, +`npm run lint`, `npm test`, and `npm run test:dist`. They need Node 24. The +Frontend workflow runs them on Linux and Windows. + Run from the repository root with the pinned toolchain: ```text @@ -261,6 +316,254 @@ suite on both hosts with default parallelism, as CI does. Use provider fixtures; record failures before any isolated rerun or full retry. A serial run alone cannot detect races between test threads. +On a Windows host whose repository is on ReFS, point `TEMP` and `TMP` at a +directory on NTFS. The private-store, credential, container, and replacement +tests need NTFS. Earlier notes here put `TEMP` and `TMP` on the same volume as +the fixtures; that works only when the repository volume is NTFS. Phase E +development ran with the repository on a ReFS volume. See +[testing standards](docs/testing-standards.md#desktop-shell-and-frontend). + +Phase E final verification on 2026-11-16 used Rust 1.99.1 and default test +parallelism, with the Windows and Linux runs one after the other. + +- **Windows** (Windows 11 build 26300, repository on ReFS, `TEMP` and `TMP` + on NTFS): formatting, Clippy with `--locked`, all-target tests, and doc + tests each exited zero. 782 tests in 36 test binaries and one doc test + passed. None failed. Eight were ignored, the same eight as before this + phase: one in `run_workflow`, six in `toolchain_acceptance`, and one in + `omgcode-platform`. The two file-symlink tests printed `SKIPPED link + assertions`. The frontend passed `npm run check`, `npm run lint`, and 113 + Vitest tests, including the four bundle checks. +- **Linux** (WSL 6.6, Ubuntu 24.04, ext4, a clean LF clone of `60430ee` with + the changed files overlaid and the frontend bundle built on Windows): + formatting, Clippy with `--locked`, all-target tests with `--no-fail-fast`, + and doc tests each exited zero. 776 tests in 36 test binaries and one doc + test passed. None failed. Eight were ignored. The platform suite re-runs + four tests inside `systemd-run` scopes and prints a nested result line for + each; those four are not counted twice here. The frontend commands did not + run on Linux, because that host has Node 18. +- **Failures before the final runs.** The first complete Linux run failed 19 + tests. Eighteen credential tests failed because the login keyring was + locked; they passed after it was unlocked, with no code change. One desktop + approval test failed because its fixture created the data root with the + default mode; the fixture now creates it with mode 0700. An earlier Linux + run stopped at one `omgcode-app` test that assumed the Windows spelling of + the data directory; that assertion is now case-insensitive. +- No test timed out and no provider fixture was retried on either host. + +Phase D2 final verification on 2026-11-16 used Rust 1.99.1, default test +parallelism, and `--no-fail-fast`, with the Windows and Linux runs one after +the other. + +- **Windows** (Windows 11 build 26300, `TEMP` and `TMP` on NTFS, real DPAPI + store): formatting, Clippy with `--locked`, all-target tests, and doc tests + each exited zero. 815 tests in 39 test binaries and one doc test passed. + None failed. The same eight were ignored. The run took under four minutes. +- **Linux** (WSL 6.6, Ubuntu 24.04, ext4, a clean LF clone of `cbbec85` with + the changed files overlaid, real Secret Service with the login collection + unlocked): the same four commands each exited zero. 807 tests in 39 test + binaries and one doc test passed, not counting five nested result lines from + the platform suite's `systemd-run` re-runs. None failed. Eight were ignored. +- **Failures before the final runs.** + - Linux Clippy failed once on `drop_non_drop`: `CredentialStore` has drop + glue on Windows and none on Linux, so an explicit `drop` of it was a lint + error only there. A block scope replaced it. + - Three Linux desktop credential tests failed once because the WSL virtual + machine had restarted and the login collection was locked again. They + passed after it was unlocked, with no code change. + - The implementing worker's own Windows run had one `run_workflow` test + exceed its 60-second limit while another project was compiling on the + host. It passed on a single rerun and in both full runs above. No timeout + was changed. +- The frontend did not change in D2, so its commands were not rerun. +- A manual run with the real binaries on Windows showed the stored token + arriving as the bearer header while a different `OMGCODE_LM_TOKEN` value was + ignored, an unknown profile refused with exit 1 by both `omgcode chat` and + `omgcode-desktop`, and the shell starting with a valid profile. It was done + before the profile checks were moved ahead of the native store open and was + not repeated afterwards; the automated tests cover the final order. +- Not run: a chat turn with a stored credential through the real shell's + windows, on either platform. + +Phase D3 final verification on 2026-11-16 used Rust 1.99.1, default test +parallelism, and `--no-fail-fast`, with the Windows and Linux runs one after +the other. + +- **Windows** (Windows 11 build 26300, `TEMP` and `TMP` on NTFS, real DPAPI + store): formatting, Clippy with `--locked`, all-target tests, and doc tests + each exited zero. 839 tests in 40 test binaries and one doc test passed. + None failed. The same eight were ignored. The run took about two minutes. +- **Linux** (WSL 6.6, Ubuntu 24.04, ext4, a clean LF clone of `b9f7d11` with + the changed files overlaid, real Secret Service with the login collection + unlocked): the same four commands each exited zero. 831 tests in 40 test + binaries and one doc test passed, not counting five nested result lines from + the platform suite's `systemd-run` re-runs. None failed. Eight were ignored. + The new `launch_flags` suite ran the real desktop binary there: both flags + together exited 2, and a profile with no settings store exited 1 and + created nothing, each before any window. +- No command failed or timed out in these two final runs. +- The frontend did not change in D3, so its commands were not rerun. +- A manual run with the real binaries on Windows used two mock local servers. + The stored token arrived only at the endpoint the profile records while a + different `OMGCODE_LM_TOKEN` value was set. `--endpoint` beside + `--provider-profile` was a usage error with exit 2 and no request, for both + `omgcode chat` and `omgcode-desktop`. The shell started with a valid profile + and reported the profile's endpoint. The first attempt at the shell check + attached to another application that already held the WebView2 debugging + port; it was repeated on a free port. +- Not run: a chat turn with a stored credential through the real shell's + windows, on either platform. + +Phase D4 and E6 final verification on 2026-11-16 used Rust 1.99.1, default +test parallelism, and `--no-fail-fast`. + +- **Windows** (Windows 11 build 26300, `TEMP` and `TMP` on NTFS, real DPAPI + store): formatting, Clippy with `--locked`, all-target tests, and doc tests + each exited zero. 923 tests in 41 test binaries and one doc test passed. + None failed. The same eight were ignored. The test run took about two + minutes. +- **Linux** (WSL 6.6, Ubuntu 24.04, ext4, a clean LF clone of `19b3747` with + the changed files overlaid, real Secret Service with the login collection + unlocked): the same four commands each exited zero. 915 tests in 41 test + binaries and one doc test passed, not counting five nested result lines from + the platform suite's `systemd-run` re-runs. None failed. Eight were ignored. + The desktop binary exited 2 for `--stream maybe`. +- **Frontend** (Windows, Node 24): the type check and Biome were clean. 155 + tests in 11 files passed, and the five bundle tests passed after a rebuild. + The frontend commands were not run on Linux, whose Node is too old. +- **Failures before the final runs.** + - The first Linux run did not run at all: `cargo` was not on the path of + the non-login shell, and every step exited 127. The script now loads the + Cargo environment and stops if `cargo` is missing. The rerun is the result + above. + - The first end-to-end run reported one failed step. The driver fetched the + approvals bundle from page script, and the content security policy + blocked the fetch. The check was removed, because the bundle test already + covers it, and the whole run was repeated. + - Workers' own test failures were in tests they were writing. No existing + assertion was weakened and no timeout was changed. +- **Real shell, Windows.** The built shell was driven over the WebView2 + debugging port against a paced event-stream mock server and a scratch data + root. All eleven steps passed on the repeated run: + - a streamed tool-enabled turn showed the live entry for the first request, + reset it for the second, and showed seven distinct preview states; + - markup in the stream stayed literal text, no element was created, no + script ran, and a bidirectional override was shown as `[U+202E]`; + - both requests carried `"stream":true` and `Accept: text/event-stream`, and + none carried `stream_options`; + - the recorded reply appeared 49 ms after the mock ended the stream, which + is the pushed `finished` event, not the five-second check; + - a stream that ended without `[DONE]` showed its partial text, then failed + with `local model stream ended before completion`, and the partial text + was not in the transcript; + - a second subscription made from page script received `request_started`, + three `delta` events, and `finished` in order, and the page, whose own + subscription it had replaced, settled about four seconds later through + the reconcile check; + - `approvals` was refused `chat_subscribe` by its capability; + - with `--stream off` the request carried `"stream":false`, no live entry + appeared, and the reply appeared 79 ms after the response; + - both launches closed with exit status 0 and left no process. +- Not run: any live LM Studio server; the channel or any window on Linux; the + 750 ms fallback poll in the real shell (the frontend tests cover it); + `chat --stream` by hand in a terminal (the CLI tests run the real binary). + +Phase D5 final verification on 2026-11-16 used Rust 1.99.1, default test +parallelism, and `--no-fail-fast`. An unrelated project was building on the +host for the whole period, and the host stayed between 82% and 100% CPU on all +16 logical processors. A watcher waited about 20 minutes for the load to drop. +It did not, so the runs were made under that load. + +- **Windows** (Windows 11 build 26300, `TEMP` and `TMP` on NTFS, real DPAPI + store): formatting, Clippy with `--locked`, and doc tests each exited zero. + The all-target test run exited non-zero: 942 tests passed, **one failed**, + and the same eight were ignored, in 41 test binaries. The run took about four + and a half minutes, against about two minutes on a quiet host. It was run + twice with the same result. +- **The Windows failure** is + `run_workflow::concurrent_runs_complete_and_are_recorded`, which reached its + 60-second limit. It runs a CPU-bound loop in the confined runner and does not + use the provider or chat code. The same test, run alone against a clean + checkout of `02221e9` at 90% load, failed the same way. That commit passed it + on a quiet host earlier the same day. This change touches nothing in + `omgcode-platform` or the run path. The cause is taken to be host load. It + is still a failed check in this run, and it was not rerun on a quiet host. + No timeout was changed. +- **A third Windows run after the commit** (`f3d8be2`, host at 77% to 100% + CPU) gave the same result: 942 passed, the same one failed, eight ignored. + The test's own shell loop was then timed outside OMGCode on the same host: + one `cmd` loop of 30,000 `cd .` iterations took 22 to 24 seconds, and two at + once took 38 seconds, before any confinement, limit sampling, or journal + work. The test allows 60 seconds for two confined runs. Its duration + therefore depends on host speed, and it has not been seen to fail on a quiet + host. +- **The test was then changed** so that it no longer depends on host speed. + Each run writes its file and waits until it sees the other run's file, on + both platforms, so exit code 0 from both also proves the runs overlapped. + The assertions and the 60-second limit are unchanged, and no production + code changed. The script's exit behaviour was first checked in a plain + shell: exit 3 when the other file never appears, exit 0 at once when it is + present, and both exit 0 when two start together. The changed test then + passed three runs in 1.2 to 3.0 seconds at 82% to 91% CPU. +- **Full runs with the changed test**, on the same loaded host. + - Windows, at 100% CPU: formatting, Clippy with `--locked`, all-target + tests, and doc tests each exited zero. 943 tests in 41 test binaries and + one doc test passed. None failed. Eight were ignored. + - Linux (a clean LF clone of `f3d8be2` with the changed files overlaid): + the same four commands each exited zero. 935 tests in 41 test binaries and + one doc test passed, not counting the five nested result lines. None + failed. Eight were ignored. The `run_workflow` binary, which holds the + changed test, took 2.2 seconds. + - Still not run: either suite on a quiet host after these changes. +- **Linux** (WSL 6.6, Ubuntu 24.04, ext4, a clean LF clone of `02221e9` with + the changed files overlaid, real Secret Service with the login collection + unlocked): formatting, Clippy, all-target tests, and doc tests each exited + zero on the final run. 935 tests in 41 test binaries and one doc test passed, + not counting five nested result lines from the platform suite's + `systemd-run` re-runs. None failed. Eight were ignored. +- **Frontend** (Windows, Node 24): the type check and Biome were clean. 159 + tests in 11 files passed, and the five bundle tests passed after a rebuild. + The frontend commands were not run on Linux. +- **Failures before the final runs.** + - The first Linux run had one failed test, + `cancelling_while_the_response_head_is_awaited_stops_the_request`. The test + cancelled after a fixed 150 ms sleep. On the loaded host the client had not + connected by then, so the adapter returned `Cancelled` without connecting, + which is correct, and the mock server timed out waiting for a connection. + The test now cancels when the server signals that it has read the request. + It then passed three runs on Windows and the full Linux rerun above. The + adapter was not changed. + - The first end-to-end run reported one failed step. The driver timed the + exit from the start of the PowerShell helper that posts the close message, + and on the loaded host an idle application took 4.6 seconds by that + measure. The helper now prints the moment it posts the message, and the + whole run was repeated. + - The implementing worker reported the Windows failure above and one Clippy + lint in a test it was writing, which it fixed. +- **Real shell, Windows.** The built shell was driven over the WebView2 + debugging port against a mock server that holds a stream open, with a + scratch data root and a workspace without model tools, so every turn was + text-only. All nine steps passed on the repeated run: + - the Cancel button ended a held turn: the window settled 74 ms after the + click with the notice `Turn cancelled.` and no error styling, the server + saw the connection close 28 ms after the click, and `chat_status` reported + `failed` with the code `cancelled`; + - the next turn in the session was accepted and completed; + - closing the window while a second stream was held ended the process with + status 0, 148 ms after the close message was posted (101 ms for an idle + application in the same run), and the server saw the connection close; + - after a restart the session was idle, showed both turns as + `Turn failed: cancelled`, held none of the partial text, and accepted a + new turn. +- Not run: any live LM Studio server; any window on Linux; the Windows suite + on a quiet host; cancellation of a tool-enabled turn in the real shell (the + service tests cover it); `--stream off` in the real shell for this change. + +These local results do not establish remote CI status: neither workflow change +has run. The real shell was driven end to end on Windows only +([testing standards](docs/testing-standards.md#desktop-shell-and-frontend)). +No window has been opened on Linux. + A2b local verification on 2026-11-10 passed formatting, Clippy, serial all-target tests, and doc tests with Rust 1.99.1. Ubuntu 24.04 under WSL, uid 1000, passed 266 tests and one doc test. Native Windows passed 235 tests and one doc test. @@ -599,7 +902,8 @@ on Windows, and trusted CLI commands as the first caller. See [ADR-0037](docs/adr/0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md). - **C13 (merged in PR #30, version 0.24.0):** provider-profile records and trusted per-call approval records. Profiles store only a name, kind, and - authentication mode; no credential, endpoint, adapter, or login exists. + authentication mode; no credential, endpoint, adapter, or login exists at + this version (0.31.0 later lets an `lm_studio` profile record an endpoint). Approvals are bound, expiring, single-use records; the first consumer is the command tool in C14. See [ADR-0038](docs/adr/0038-record-provider-profiles-without-credentials.md) and @@ -655,8 +959,10 @@ on Windows, and trusted CLI commands as the first caller. See Next decisions, not yet scheduled: -- provider adapters that read stored credentials, and subscription/API-key - login; +- cloud provider adapters, subscription/API-key login, token refresh, and + endpoint rules for non-loopback providers, HTTPS, and certificates (one + adapter, the local LM Studio route, now reads a stored credential, only at + its profile's loopback endpoint); - model create, delete, rename, and multi-edit tools, and network, Git, and MCP tools; - registry credentials and development-server networking. @@ -684,12 +990,156 @@ login or entitlement route. `credential_orphaned` warning. Only `api_key` profiles accept a credential. Native tests run on both hosts; Linux needs an unlocked Secret Service, which CI provides with gnome-keyring. No adapter reads a stored credential. +- **D2 (version 0.30.0):** the first stored-credential read. See + [ADR-0047](docs/adr/0047-read-the-local-provider-token-from-the-credential-store.md). + `omgcode chat --provider-profile `, `session compact + --provider-profile `, and `omgcode-desktop --provider-profile ` + (fixed at launch) make the LM Studio adapter read its token from the + profile's stored credential and ignore `OMGCODE_LM_TOKEN`. Core's + `provider_credential_binding` takes no credential store and refuses an + unknown profile, another provider kind, a `subscription` profile, and an + unbound `api_key` profile; a `none` profile gives no token there. The native + store is opened only after it returns a binding, and + `load_provider_credential` refuses a binding from another backend before it + reads a secret. Every failure ends the turn with a `credential` error and a + cause code, with no fallback to no token or to another token. The secret is loaded + while the settings store is held and the store is released before any + request. The desktop resolves it again each turn and checks it once at + launch. A read records nothing. The token is held in a zeroing buffer; the + provider clients' header copies are not zeroed. `capabilities` reports + `stored_credential_adapters: ["lm_studio"]`; `provider_adapters` stays + `false`. +- **D3 (version 0.31.0):** the stored credential is bound to its profile's + endpoint. See + [ADR-0048](docs/adr/0048-bind-a-stored-credential-to-its-profile-endpoint.md). + `provider-profile-add --endpoint ` records one endpoint on an + `lm_studio` profile. It must pass the rule the adapter enforces on every + request (plain `http`, a literal loopback IP, a non-zero port, `/v1`, no + user information, query, or fragment, at most 256 bytes), through one core + implementation that the record and replay also use. Every other kind refuses + an endpoint. The endpoint is fixed for the profile's life: to change it, + remove the profile (which deletes its credential) and add a new one. + `provider_credential_binding` returns the recorded endpoint with the + credential decision, and a selected profile with no endpoint fails with + `endpoint_not_recorded` before any native store access. `--endpoint` and + `--provider-profile` are mutually exclusive in `chat`, `session compact`, and + `omgcode-desktop`. The desktop fixes the profile's endpoint at launch and + refuses a turn with `endpoint_changed` if a re-added profile records another. + The profile record's `endpoint` field is written only when present, so a + journal without endpoints keeps its bytes; a journal with one is not + readable by 0.30.0 or earlier. A profile recorded before 0.31.0 must be + removed and added again. `capabilities` reports + `provider_profile_endpoints: true`. +- **D4 (version 0.32.0):** the local adapter can stream. See + [ADR-0049](docs/adr/0049-stream-local-model-replies-and-push-chat-events.md). + `LocalModelClient::complete_streaming` and `complete_with_tools_streaming` + send the same body as the non-streaming methods with `stream: true` and read + server-sent events under strict rules (success status and content type, the + existing 1 MiB and thirty-second bounds over the whole stream, no repair, + `[DONE]` required or `IncompleteStream`, duplicate-key refusal, a non-null + `error` refused, tool calls assembled by `index`). `stream_options` is never + sent. The non-streaming methods stay the default. `ChatHooks::on_stream` + makes a turn stream and receives `RequestStarted` and `Text` events; + compaction requests never stream. `StreamRedactor` removes the known token + across piece boundaries, and the final reply is still redacted as a whole. + The journals record only the validated reply. `omgcode chat --stream` + writes the provisional text to standard error through `DisplaySafeStream`. + `capabilities` reports `streaming_adapters: ["lm_studio"]`. The stream rules + were checked against local fixtures only. +- **D5 (version 0.33.0):** a cancelled streamed request is interrupted. See + [ADR-0050](docs/adr/0050-interrupt-a-streamed-model-request-on-cancellation.md). + `complete_streaming` and `complete_with_tools_streaming` take a + `CancelToken`. The streamed request runs on an async client inside a + single-thread runtime created for that request, with the blocking client's + settings and the same thirty-second deadline. The token is checked before the + request, while waiting for the response head, and while waiting for each + piece of the body, every 25 ms. A set token drops the request and returns + `ProviderError::Cancelled`. `ToolModelRequest` carries the token and + `ModelError::Cancelled` ends the turn with the failure reason `cancelled` + when the token is set. A streamed text-only turn now has a cancel token. The + application error kind `cancelled` replaces `turn` for a cancelled + tool-enabled turn. `chat_cancel` stops a streamed request, the window shows + "Turn cancelled.", and exit leaves no open turn for a streamed turn. No + journal record changes shape. A non-streamed request, the compaction request, + and a tool call in progress are not interrupted, and the CLI has no cancel + source. The cancel path was checked against local fixtures only. + +Still open in Phase D: cloud provider adapters, subscription and API-key +login, token refresh, account lifecycle, a memory-only session credential, +endpoint rules for non-loopback providers, HTTPS, and certificates, +qualification of the stream rules and of cancellation against a live LM Studio +server, a cancel source for the CLI, interrupting non-streamed and compaction +requests, and a longer deadline for long +generations (the thirty-second deadline covers the whole response). The frontend shows nothing about the credential source beyond the +endpoint text it already displayed. ### Phase E: desktop interface and session tree The Rust core remains the authority. The UI must not receive raw journal paths, credentials, or unrestricted host capabilities. Follow the session, permission, -context, and security contracts. +context, and security contracts. Follow the +[desktop interface guide](docs/desktop-interface.md). + +Version 0.29.0 builds a prototype on branch `codex/desktop-interface`. It is +not merged, and no pull request exists yet. + +- **E1 (ADR-0044):** lock-free journal inspection. `SessionInspection::inspect` + reads one journal with no lock call, no link following, and the writer's + validator. Windows lock and sharing violations become `Busy`. Bytes after the + last line feed are reported as `incomplete_tail`. It adds times and a bounded + transcript entry list. `session::discover` walks only the dated layout and + never opens a journal or follows a link. Limits: 50,000 journals and 500,000 + directory entries. +- **E2 (ADR-0044):** a derived SQLite index in + `/session-index-v1/index.sqlite3` through `rusqlite` 0.43.0 with + the bundled SQLite and FTS5. Incremental, bounded refresh; a busy journal + keeps its last metadata; an invalid journal is listed with a code. A + missing, corrupt, or foreign index is deleted and rebuilt. +- **E3 (ADR-0045):** the `omgcode-app` library. The chat turn, compaction, + session creation, the data-root resolver, and token redaction moved out of + the CLI, which is now a thin adapter. `DesktopService` is the + front-end-neutral command service, including review tokens. Core adds + `review_approval` and display-safe text. No CLI behaviour changed. +- **E4 (ADR-0045, ADR-0046):** the Tauri shell `omgcode-desktop`: two windows, + 18 commands (19 since E6), a capability file per window, the Rust window + policy, the content + policy, the navigation guard, and a bounded shutdown. +- **E5 (ADR-0045, ADR-0046):** the strict TypeScript frontend with Vite, + Vitest, and Biome: the session tree, search, transcript, composer, and the + separate approvals bundle. +- **E6 (ADR-0049):** the command `chat_subscribe` (`main` only; 19 commands) + takes a caller-created Tauri IPC channel and needs no `core:` permission. + The service keeps one subscriber, replays the preview of every running turn + to a new one, and pushes `request_started`, `delta` (at most 1,024 bytes), + `preview_truncated` (16 KiB preview per request), and `finished`. A guard + sends `finished` after the result is recorded and records a panicked turn as + failed first. `omgcode-desktop --stream ` defaults to `on`. The + frontend subscribes once, reads `chat_status` on `finished`, keeps a + five-second reconcile check, falls back to the 750 ms poll if subscribing + fails, and shows one live entry "Assistant (generating, not saved yet)" with + the preview. The approvals polls are unchanged. +- **D5 (ADR-0050):** `chat_cancel` interrupts a streamed turn, the main window + shows the notice "Turn cancelled." decided by the error code `cancelled`, and + exit cancels every running turn. No command or wire type changes. + +Still open for the desktop: + +- opening the shell's windows on Linux. It builds and its tests pass there, + including the Unix-only inspection and index branches, but its webview, IPC, + capability enforcement, and navigation guard have not run on Linux; +- development panels (terminal tabs, task logs, and diffs), a settings + interface, and persisted chat settings; +- push for approvals and the session tree, which still poll or refresh on + request, and a run of the event channel on Linux, where no window has been + opened; +- a recovery interface for an incomplete tail or a damaged journal, plus + `session.archive_requested` stop-and-archive and storage-use display; +- Git, MCP, skills, and memory interfaces; +- a native confirmation dialog for approvals, which would remove the webview + from the final decision; +- packaging, signing, and installers (Phase G); +- automatic repair of a corrupt index behind a valid header, and a protection + against two versions sharing one data root and rebuilding each other's index. ### Phase F: runtime skills, MCP, memories, compaction, and caches @@ -713,6 +1163,29 @@ release readiness remain separate gates. ## Known cautions +- Build `crates/omgcode-desktop/ui/dist` before any Cargo command that includes + `omgcode-desktop`. Without it the build fails with `The frontendDist + configuration is set to "ui/dist" but this path doesn't exist`. +- Never run `tauri dev` or the shell without `--data-dir `. Without it + the shell uses the real per-user data directory. +- Two tests print `SKIPPED link assertions` and return on a host that cannot + create file symbolic links (error 1314): `a_symlinked_journal_is_rejected` + and `a_database_file_that_is_a_link_is_refused`. They did not run on the + Phase E Windows host; they ran in the Linux run. A privileged Windows run + has not executed them. +- A Linux login keyring can exist and still be locked, which fails every + credential suite with "an unlocked Secret Service default collection is + required". Check the collection's `Locked` property before a Linux run. +- A test fixture that creates its own data root must make it mode 0700 on + Unix. The checkpoint store refuses the default mode. +- A data root under the user's Temp directory inherits an ACL that the private + store refuses. Reset it before a manual run: + `icacls /inheritance:r /grant:r *:(OI)(CI)F`. +- The session index file carries a version. A binary with a different index + version deletes and rebuilds the index, so two versions sharing a data root + rebuild each other's index. Rebuild also fails on Windows while another + process has the database open. +- `.claude/` is untracked local tooling state. Do not commit it. - Linux CLI integration tests raced on inherited `flock` descriptors, producing `Busy` and mock-provider timeouts. The spawn gate in [testing standards](docs/testing-standards.md#isolation-and-concurrency) covers @@ -724,7 +1197,11 @@ release readiness remain separate gates. deadline instead of a killed CLI with a pending run. The pre-existing multi-fixture provider tests also timed out locally under host load; they pass individually and passed CI on `987a5aa`. Recheck timeouts on a clean - host rather than weakening an assertion. + host rather than weakening an assertion. A test that fails only under load + may be measuring the host: `concurrent_runs_complete_and_are_recorded` spun + a fixed loop and was changed to a file rendezvous. To tell load from a + regression, run the failing test alone on a clean checkout of the previous + commit, and time its workload outside OMGCode. - `.serena/` is untracked and unrelated. Do not commit or delete it without deciding what it is. - The turn runtime is library/test infrastructure until a provider adapter and diff --git a/README.md b/README.md index 89a3aa5..d40e80b 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,11 @@ native workspace file broker is available through trusted CLI commands. A core turn/tool runtime and schema-v2 session format let chat offer the local LM Studio model read, list, approval-gated exact-edit, and approval-gated command tools, only for a workspace that enables `model_tools` and each tool's own -preferences. Model file creation, deletion, and renaming, a desktop interface, -and Git workflows remain planned. The trusted CLI `run` command starts one program for a workspace whose +preferences. Model file creation, deletion, and renaming, and Git workflows +remain planned. A prototype desktop shell (Tauri, with a strict TypeScript +frontend) browses and searches sessions, runs chat, and decides model-tool +approvals in a separate window; it is unpackaged and has run only on Windows. +The trusted CLI `run` command starts one program for a workspace whose `execute` preference is enabled. It uses an operator-declared toolchain profile, records each run in a run journal, and confines the process tree with Landlock and a seccomp socket filter on Linux or a per-workspace AppContainer @@ -20,8 +23,9 @@ directory grants. > compiled prompt, report implemented capabilities, manage JSONL sessions and > workspace permission preferences, perform bounded workspace reads/listings, > create new files/directories through the trusted CLI, preview bounded diffs -> against trusted proposed content, and send one non-streaming chat request to -> an explicitly selected local LM Studio model. +> against trusted proposed content, and send a chat request to +> an explicitly selected local LM Studio model. `omgcode chat --stream` streams +> that model's reply to standard error as provisional text. > When a workspace enables the `model_tools` preference, chat lets the local > model read and list files inside that workspace through a bounded runtime. > When `execute`, `read`, and `write` are also enabled, it offers a confined @@ -50,13 +54,16 @@ directory grants. > declared origins into a private per-workspace cache. A selected cache root > is exposed read-only on Linux and Windows; Windows needs a read-execute > container grant, and Linux falls back to application storage when no root is -> selected. Provider profiles record connection shapes, and trusted -> single-use approval records prepare per-call model-tool consent; neither -> enables an adapter, login, or model tool. API keys can be stored in protected -> OS storage (DPAPI on Windows, the Secret Service on Linux) and bound to a -> profile, but no adapter reads them yet. Runs have memory, process-count, -> CPU-time, and sampled disk-growth limits; the disk limit is not a filesystem -> quota. See [command execution](docs/execution.md#tree-limits). +> selected. Provider profiles record connection shapes and enable no cloud +> adapter or login. Trusted single-use approval records authorize one exact +> model-tool call; the trusted CLI or the desktop approvals window can decide +> them. API keys can be stored in protected OS storage (DPAPI on Windows, the +> Secret Service on Linux) and bound to a profile. Only the local LM Studio +> adapter reads one, through an operator-selected `lm_studio` profile +> (`--provider-profile`) that also supplies the endpoint; no other adapter +> reads them. Runs have memory, +> process-count, CPU-time, and sampled disk-growth limits; the disk limit is +> not a filesystem quota. See [command execution](docs/execution.md#tree-limits). ## Goals @@ -71,8 +78,10 @@ Security takes priority when an optimization would weaken these boundaries. ## Current prototype -The workspace contains three Rust crates: `omgcode-core`, `omgcode-cli`, -and `omgcode-platform`. The CLI command is `omgcode`. It provides: +The workspace contains five Rust crates: `omgcode-core`, `omgcode-cli`, +`omgcode-platform`, `omgcode-app`, and `omgcode-desktop`. The CLI command is +`omgcode`. The desktop shell is the `omgcode-desktop` binary, with its +frontend in `crates/omgcode-desktop/ui`. The CLI provides: - `prompt`: print the compiled, read-only base prompt, version, and SHA-256 digest. - `capabilities`: report what this prototype implements and rejects. @@ -123,6 +132,20 @@ It has no create, delete, rename, Git, MCP, or network tool. New sessions use schema version 3; version 1 and 2 journals remain readable and writable with their original subsets. +The desktop shell is a prototype. It has a session tree grouped by workspace +folder, full-text search over a derived SQLite index, a paged transcript view, +chat that runs the same turn as `omgcode chat`, and an approvals window that +shows an exact reviewed request before it lets the operator decide. The webview +gets no authority: the Rust side authorizes every command, and no command +accepts a host path. The webview is still not a trusted computing base. It has +no packaging, settings interface, development panels, or recovery +interface, and it has not been run on Linux. Chat streams the local model's +reply into the window by default (`--stream off` at launch disables it). The +text is provisional, and the journal records only the validated reply. The +shell pushes chat events to the main window through one caller-scoped channel +that needs no Tauri `core:` permission (ADR-0049). See the +[desktop interface guide](docs/desktop-interface.md). + The platform crate checks opened journal handles, captures native directory identity for workspace registration, and implements handle-relative file operations. The broker applies the current registered workspace checks, but it @@ -147,9 +170,19 @@ guide](docs/development.md) for limits and command details. The CLI stores provider API keys in operating-system protected storage through trusted `workspace provider-credential-set` and `provider-credential-remove` -commands (ADR-0042). No provider adapter reads them yet. The optional local -server token input for chat remains prototype wiring; it is not a release -credential boundary. Do not treat the prototype as release-ready or use it to handle +commands (ADR-0042). Only the local LM Studio adapter reads one: with +`--provider-profile `, `chat` and `session compact` (and the desktop +shell, at launch) take the server token from the stored credential of that +`lm_studio` profile and do not read `OMGCODE_LM_TOKEN` (ADR-0047). A +credential that cannot be read stops the turn. An `lm_studio` profile can +record one loopback endpoint with `provider-profile-add --endpoint`, and a +selected profile supplies both its endpoint and its token, so the stored token +goes only to that endpoint (ADR-0048). `--endpoint` and `--provider-profile` +cannot be combined. A profile recorded before 0.31.0 has no endpoint and must be +removed and added again before it can be selected. Without the flag, `--endpoint` +and its default apply, and the optional local server token comes from +`OMGCODE_LM_TOKEN`, which remains prototype wiring; it is not +a release credential boundary. Do not treat the prototype as release-ready or use it to handle sensitive credentials. To build and run the prototype with the pinned Rust toolchain: @@ -209,9 +242,12 @@ check the server's configuration before sending sensitive text. pull, and push, with controlled remote access and credential handling. - **Development panels:** Terminal tabs, background task status and logs, and file and hunk diffs. -- **Session interface:** A searchable desktop session tree grouped by working - folder, with sorting and status. The current CLI stores versioned JSONL - journals and supports inspect, archive, and restore. +- **Session interface:** The prototype desktop shell has a searchable session + tree grouped by working folder, with sorting, archived and active views, and + status; it supports archive, restore, and interrupt. Recovery workflows, + storage-use display, and packaging remain planned. The CLI stores the + versioned JSONL journals and supports inspect, archive, and restore. See the + [desktop interface guide](docs/desktop-interface.md). - **MCP support:** Connect local stdio and remote Streamable HTTP servers for tools, resources, and prompt templates through core-owned permissions. See [MCP support](docs/mcp.md). @@ -243,7 +279,7 @@ check the server's configuration before sending sensitive text. | Ollama Local | Planned: local Ollama API | | Ollama Cloud | Planned: authenticated cloud API | | OpenRouter | Planned: explicit upstream routing controls | -| LM Studio | Prototype: bounded Chat Completions over literal loopback HTTP; model discovery, streaming, and release authentication remain unqualified | +| LM Studio | Prototype: bounded Chat Completions over literal loopback HTTP; a selected profile supplies the endpoint and the optional server token from its stored credential, otherwise `--endpoint` and `OMGCODE_LM_TOKEN` apply; model discovery and release authentication remain unqualified, and streamed replies have met local fixtures only | API-key and subscription connections are distinct authentication and billing profiles. Subscription-backed usage for both ChatGPT and Claude is required for @@ -262,15 +298,18 @@ explicit rather than being assumed to work identically across providers. authentication, permissions, file access, toolchain profiles, Git operations, task supervision, prompt assembly, context management, caching, skills, MCP, and scoped memories. -- **Desktop interface:** Tauri with a bundled TypeScript web interface. +- **Desktop interface:** Tauri with a bundled TypeScript web interface. A + prototype exists (`omgcode-desktop`); see the + [desktop interface guide](docs/desktop-interface.md). - **Execution backends:** Separate Windows and Linux sandbox implementations. - **Local storage:** Versioned JSONL session journals, archive/restore events, - a rebuildable folder/search index, settings, scoped memories, and referenced + a rebuildable SQLite folder and search index (implemented), settings, scoped memories, and referenced task artifacts. - **Credential handling:** Operating-system credential storage, accessed through narrow services in the Rust core. -The interface will use a limited command interface to the Rust core. Provider +The desktop prototype uses a limited command interface to the Rust core: 19 +commands, no host paths, and per-window permissions checked in Rust. Provider keys will stay out of frontend state, repository files, and task environments. ## Development tools @@ -332,8 +371,11 @@ cannot grant permissions. See [context management](docs/context-management.md) a 2. Resolve subscription integration eligibility and validate both login/usage paths. 3. Prove Windows/Linux isolation, representative toolchains, and process cleanup. 4. Extend the Rust core and build the desktop interface, session tree, and - remaining persistence and recovery workflows. -5. Expand provider adapters and add streaming and core-authorized tool workflows. + remaining persistence and recovery workflows. The first desktop prototype + (session tree, search, chat, and approvals) is implemented; recovery, + settings, development panels, and packaging remain. Streaming of the local + model's reply is implemented for that adapter only (ADR-0049). +5. Expand provider adapters and add core-authorized tool workflows. 6. Add visible instructions, token accounting, compaction, safe caching, skills, and project/global memories with boundary tests and efficiency measurements. 7. Add model-scoped file tools, sandboxed terminals, background tasks, and MCP clients. diff --git a/crates/omgcode-app/Cargo.toml b/crates/omgcode-app/Cargo.toml new file mode 100644 index 0000000..03ffa1a --- /dev/null +++ b/crates/omgcode-app/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "omgcode-app" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +description = "Front-end-neutral application services shared by the OMGCode CLI and desktop shell" + +[dependencies] +omgcode-core = { path = "../omgcode-core" } +directories.workspace = true +zeroize.workspace = true +serde.workspace = true +serde_json.workspace = true +sha2.workspace = true +time.workspace = true +uuid.workspace = true + +[dev-dependencies] +tempfile.workspace = true + +[lints] +workspace = true diff --git a/crates/omgcode-app/src/chat.rs b/crates/omgcode-app/src/chat.rs new file mode 100644 index 0000000..27e4de0 --- /dev/null +++ b/crates/omgcode-app/src/chat.rs @@ -0,0 +1,271 @@ +//! One chat turn against the selected local model, without any front end. +//! +//! This module performs no printing, no environment reads, no argument +//! parsing, and never exits the process. Whether the turn offers workspace +//! tools is decided from live settings by the core; this module grants no +//! authority. The settings store is opened and dropped inside its own steps and +//! is never held while a model request is in flight. + +mod output; +mod preflight; +mod stream; +mod text; +mod tools; + +use std::path::Path; + +use omgcode_core::context::DEFAULT_COMPACT_THRESHOLD; +use omgcode_core::prompt::{self, BASE_PROMPT, BASE_PROMPT_VERSION}; +use omgcode_core::provider::{LocalModelClient, LocalModelConfig}; +use omgcode_core::session::{RequestManifest, SessionJournal}; +use omgcode_core::turn::CancelToken; + +pub use self::output::ChatTurnOutput; +use crate::context::summary_output_tokens; +use crate::error::{AppError, ErrorKind}; +use crate::secrets::redact_known_secret; +use crate::tool_mode::{TOOL_SCHEMA_ESTIMATE_BYTES, ToolMode, select}; + +/// Selected model, budgets, and user-level preferences for chat turns. +/// +/// The settings carry no credential. They are user preferences and cannot +/// replace the compiled base prompt or grant any workspace authority. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ChatSettings { + /// A literal loopback HTTP URL with an explicit port and `/v1` path. + pub endpoint: String, + /// The model identifier already available to the selected server. + pub model: String, + /// Validated context capacity for the selected loaded model. + pub context_tokens: u64, + /// Output reserve for each model request. + pub max_output_tokens: u32, + /// User-level preferences, sent as untrusted-origin custom instructions. + pub instructions: String, + /// Compact before a turn when the conservative estimate reaches this value. + pub compact_threshold: u64, + /// Whether to compact at the effective trigger before a turn. + pub auto_compact: bool, +} + +impl ChatSettings { + /// Settings with the CLI's defaults for everything but the model and its context. + pub fn new(endpoint: impl Into, model: impl Into, context_tokens: u64) -> Self { + Self { + endpoint: endpoint.into(), + model: model.into(), + context_tokens, + max_output_tokens: 1024, + instructions: String::new(), + compact_threshold: DEFAULT_COMPACT_THRESHOLD, + auto_compact: false, + } + } +} + +/// The inputs of one chat turn. +/// +/// It has no `Debug` implementation because `token` is a credential. +pub struct ChatTurnInput<'a> { + /// The current user request. Known-token text is removed before use. + pub message: &'a str, + /// The model and budgets for this turn. + pub settings: &'a ChatSettings, + /// The optional local server token, used for the request header and for + /// removing the same text from everything recorded or sent. It is borrowed + /// for the call and never stored, logged, or serialized. + pub token: Option<&'a str>, +} + +/// One provisional event of a streamed model request (ADR-0049). +/// +/// Streamed text is provisional and untrusted. The journal, not this text, is +/// the record: a request that fails later leaves text that was never a reply. +/// The turn has already removed the known server token from `Text`, but the +/// text is otherwise as the model sent it, so a front end must render it as +/// text, never as markup or terminal control data. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ChatStreamEvent<'a> { + /// A model request of the turn is about to start. Text of earlier requests + /// of the same turn is superseded. + RequestStarted, + /// A non-empty piece of assistant text of the current request. + Text(&'a str), +} + +/// Optional callbacks a front end can supply to a turn. +/// +/// One hook delivers the cancellation token. Cancellation is cooperative: the +/// core checks it between rounds and tool calls, and a streamed model request +/// also watches it and stops within about 25 ms (ADR-0050). A request that is +/// not streamed cannot be interrupted. The other hook streams provisional +/// text. +#[derive(Default)] +pub struct ChatHooks<'a> { + /// Called once, before the first model request: on the tool-enabled path + /// right after the turn runtime is built, and on a text-only turn only when + /// it streams (`on_stream` is set). A text-only turn that does not stream + /// never calls it, because a non-streamed request cannot be interrupted. + /// The callback runs on the turn's thread and must not block; hand the + /// token to another thread and return. + pub on_cancel_token: Option<&'a mut dyn FnMut(CancelToken)>, + /// When set, every model request of the turn is streamed and this callback + /// receives [`ChatStreamEvent::RequestStarted`] before each request and + /// [`ChatStreamEvent::Text`] for each piece. It runs on the turn's thread + /// while the response is being read, so it must not block and must not + /// hold a lock that the turn's caller needs. When `None`, every request is + /// non-streaming. Compaction requests are never streamed, and tool-call + /// names and arguments are never passed to it. + pub on_stream: Option<&'a mut dyn FnMut(ChatStreamEvent<'_>)>, +} + +/// The two validated model clients a turn uses. +/// +/// Building them validates the endpoint and token before any storage is +/// opened. The clients keep their own copy of the token header; they do not +/// implement `Debug` output of it. +pub struct ChatClients { + chat: LocalModelClient, + summary: LocalModelClient, +} + +impl ChatClients { + /// Validate `settings` and build the chat client and the summary client. + /// + /// Fails with `provider` when the endpoint, model budgets, or token are + /// unusable. No network request is made. + pub fn new(settings: &ChatSettings, token: Option<&str>) -> Result { + let chat = LocalModelClient::new( + LocalModelConfig { + base_url: settings.endpoint.clone(), + model: settings.model.clone(), + context_tokens: settings.context_tokens, + max_output_tokens: settings.max_output_tokens, + }, + // The client owns its header; the caller keeps one copy for redaction. + token.map(str::to_owned), + ) + .map_err(AppError::provider)?; + let summary = LocalModelClient::new( + LocalModelConfig { + base_url: settings.endpoint.clone(), + model: settings.model.clone(), + context_tokens: settings.context_tokens, + max_output_tokens: summary_output_tokens(settings.context_tokens), + }, + token.map(str::to_owned), + ) + .map_err(AppError::provider)?; + Ok(Self { chat, summary }) + } +} + +/// The pieces of one turn after secret redaction, shared by its steps. +struct Turn<'a> { + message: &'a str, + instructions: &'a str, + settings: &'a ChatSettings, + token: Option<&'a str>, + data_root: &'a Path, +} + +/// Run one chat turn, building the clients from the settings first. +/// +/// This is the whole turn for a caller that supplies only settings, a message, +/// and the optional token. See [`run_chat_turn_with_clients`] for the order of +/// checks and the errors. +pub fn run_chat_turn( + journal: &mut SessionJournal, + data_root: &Path, + input: &ChatTurnInput<'_>, + hooks: &mut ChatHooks<'_>, +) -> Result { + let clients = ChatClients::new(input.settings, input.token)?; + run_chat_turn_with_clients(journal, data_root, input, &clients, hooks) +} + +/// Run one chat turn with clients built earlier by [`ChatClients::new`]. +/// +/// Deterministic failures return before the turn starts: an archived session or +/// an incomplete previous turn (`session`), unreadable settings (`control`), +/// an unreachable context trigger without auto-compaction (`context`), and a +/// prompt that cannot be assembled (`prompt`). After the turn starts, a failed +/// provider request or runtime is recorded in the journal where the core can +/// do so, and an uncertain storage failure is reported, never papered over. +/// +/// The message and instructions are cleaned of the known token before use. The +/// token is used only for redaction and is never part of any returned error. +pub fn run_chat_turn_with_clients( + journal: &mut SessionJournal, + data_root: &Path, + input: &ChatTurnInput<'_>, + clients: &ChatClients, + hooks: &mut ChatHooks<'_>, +) -> Result { + let settings = input.settings; + let message = redact_known_secret(input.message, input.token); + let instructions = redact_known_secret(&settings.instructions, input.token); + let turn = Turn { + message: &message, + instructions: &instructions, + settings, + token: input.token, + data_root, + }; + if journal.snapshot().archived { + return Err(AppError::new( + ErrorKind::Session, + "Restore the archived session before chatting", + )); + } + if journal.snapshot().active_turn.is_some() { + return Err(AppError::new( + ErrorKind::Session, + tools::ACTIVE_TURN_MESSAGE, + )); + } + let mode = select(data_root, journal)?; + let tools_enabled = matches!(mode, ToolMode::Enabled { .. }); + let tool_extra = if tools_enabled { + TOOL_SCHEMA_ESTIMATE_BYTES + } else { + 0 + }; + let meter = + preflight::preflight_context(journal, &turn, &clients.summary, tool_extra, tools_enabled)?; + let manifest = RequestManifest { + base_prompt: BASE_PROMPT.to_owned(), + prompt_version: BASE_PROMPT_VERSION.to_owned(), + prompt_sha256: prompt::base_prompt_sha256(), + custom_instructions: instructions.clone(), + model: settings.model.clone(), + base_url: settings.endpoint.clone(), + context_tokens: settings.context_tokens, + max_output_tokens: settings.max_output_tokens, + tool_schema_version: None, + tools: None, + }; + // Live settings decide whether this turn offers the workspace tools. + match mode { + ToolMode::Enabled { workspace_id } => tools::run( + journal, + workspace_id, + &clients.chat, + &turn, + manifest, + meter, + hooks, + ) + .map(ChatTurnOutput::tools), + ToolMode::Disabled { reason } => text::run( + journal, + &clients.chat, + &turn, + reason, + manifest, + meter, + hooks, + ) + .map(ChatTurnOutput::text), + } +} diff --git a/crates/omgcode-app/src/chat/output.rs b/crates/omgcode-app/src/chat/output.rs new file mode 100644 index 0000000..b067aaa --- /dev/null +++ b/crates/omgcode-app/src/chat/output.rs @@ -0,0 +1,185 @@ +//! The result of one completed chat turn. + +use omgcode_core::provider::ModelReply; +use serde::ser::Error as _; +use serde::{Serialize, Serializer}; +use serde_json::{Map, Value, to_value}; + +use crate::context::ContextMeter; + +/// Assistant text from a tool-enabled turn. It remains untrusted model output. +#[derive(Clone, Debug, Serialize)] +pub(crate) struct ToolTurnReply { + pub(crate) content: String, + /// `stop`, or `length` when the token limit cut the reply short. + pub(crate) finish_reason: String, +} + +/// Bounded counters for a tool-enabled turn. +#[derive(Clone, Debug, Serialize)] +pub(crate) struct ToolTurnSummary { + pub(crate) mode: &'static str, + pub(crate) workspace_id: String, + pub(crate) rounds: usize, + pub(crate) tool_calls: usize, + pub(crate) tool_bytes: u64, + pub(crate) withheld_tool_results: usize, +} + +/// Summary of one completed tool-enabled turn. +#[derive(Clone, Debug, Serialize)] +pub(crate) struct ToolTurnOutput { + pub(crate) reply: ToolTurnReply, + pub(crate) model_tools: ToolTurnSummary, + pub(crate) context: ContextMeter, +} + +/// Summary of one completed text-only turn. +#[derive(Clone, Debug)] +pub(crate) struct TextTurnOutput { + pub(crate) reply: ModelReply, + pub(crate) reason: &'static str, + pub(crate) withheld_tool_results: usize, + pub(crate) context: ContextMeter, +} + +impl Serialize for TextTurnOutput { + /// Build the same key-ordered object the CLI printed before the extraction. + fn serialize(&self, serializer: S) -> Result { + let mut model_tools = Map::new(); + model_tools.insert("mode".to_owned(), Value::from("disabled")); + model_tools.insert("reason".to_owned(), Value::from(self.reason)); + model_tools.insert( + "withheld_tool_results".to_owned(), + Value::from(self.withheld_tool_results), + ); + let mut object = Map::new(); + object.insert( + "reply".to_owned(), + to_value(&self.reply).map_err(S::Error::custom)?, + ); + object.insert("model_tools".to_owned(), Value::Object(model_tools)); + object.insert( + "context".to_owned(), + to_value(&self.context).map_err(S::Error::custom)?, + ); + Value::Object(object).serialize(serializer) + } +} + +/// The two turn shapes behind [`ChatTurnOutput`]. +#[derive(Clone, Debug)] +enum Shape { + Tools(ToolTurnOutput), + Text(TextTurnOutput), +} + +/// One completed chat turn: the reply, the tool outcome, and the context meter. +/// +/// It serializes to the object the trusted CLI prints after the session +/// locator. Typed accessors let another front end avoid parsing that JSON. The +/// reply is untrusted model output. +#[derive(Clone, Debug)] +pub struct ChatTurnOutput { + shape: Shape, +} + +impl ChatTurnOutput { + pub(crate) fn tools(output: ToolTurnOutput) -> Self { + Self { + shape: Shape::Tools(output), + } + } + + pub(crate) fn text(output: TextTurnOutput) -> Self { + Self { + shape: Shape::Text(output), + } + } + + /// The assistant reply text. It is untrusted model output. + pub fn reply(&self) -> &str { + match &self.shape { + Shape::Tools(output) => &output.reply.content, + Shape::Text(output) => &output.reply.content, + } + } + + /// The provider finish reason: `stop`, or `length` when the limit cut the reply. + pub fn finish_reason(&self) -> &str { + match &self.shape { + Shape::Tools(output) => &output.reply.finish_reason, + Shape::Text(output) => &output.reply.finish_reason, + } + } + + /// Whether this turn offered the workspace tools. + pub fn tools_enabled(&self) -> bool { + matches!(self.shape, Shape::Tools(_)) + } + + /// Why a text-only turn did not offer tools; `None` when tools were offered. + pub fn disabled_reason(&self) -> Option<&'static str> { + match &self.shape { + Shape::Tools(_) => None, + Shape::Text(output) => Some(output.reason), + } + } + + /// The registered workspace whose tools were offered, as text. + pub fn workspace_id(&self) -> Option<&str> { + match &self.shape { + Shape::Tools(output) => Some(&output.model_tools.workspace_id), + Shape::Text(_) => None, + } + } + + /// Model requests made; `None` for a text-only turn, which does not count them. + pub fn rounds(&self) -> Option { + match &self.shape { + Shape::Tools(output) => Some(output.model_tools.rounds), + Shape::Text(_) => None, + } + } + + /// Tool calls dispatched; `None` for a text-only turn. + pub fn tool_calls(&self) -> Option { + match &self.shape { + Shape::Tools(output) => Some(output.model_tools.tool_calls), + Shape::Text(_) => None, + } + } + + /// Aggregate tool-message bytes; `None` for a text-only turn. + pub fn tool_bytes(&self) -> Option { + match &self.shape { + Shape::Tools(output) => Some(output.model_tools.tool_bytes), + Shape::Text(_) => None, + } + } + + /// Saved tool results replaced by a withheld notice when the turn started. + pub fn withheld_tool_results(&self) -> usize { + match &self.shape { + Shape::Tools(output) => output.model_tools.withheld_tool_results, + Shape::Text(output) => output.withheld_tool_results, + } + } + + /// The conservative pre-turn context accounting. + pub fn context(&self) -> &ContextMeter { + match &self.shape { + Shape::Tools(output) => &output.context, + Shape::Text(output) => &output.context, + } + } +} + +impl Serialize for ChatTurnOutput { + fn serialize(&self, serializer: S) -> Result { + match &self.shape { + Shape::Tools(output) => output.serialize(serializer), + Shape::Text(output) => output.serialize(serializer), + } + } +} diff --git a/crates/omgcode-app/src/chat/preflight.rs b/crates/omgcode-app/src/chat/preflight.rs new file mode 100644 index 0000000..5db2145 --- /dev/null +++ b/crates/omgcode-app/src/chat/preflight.rs @@ -0,0 +1,140 @@ +//! The pre-turn context trigger and visible meter. + +use std::path::Path; + +use omgcode_core::context::{ContextBudget, estimate_history_input_tokens}; +use omgcode_core::prompt::{self, BASE_PROMPT, ConversationMessage, ConversationRole, PromptError}; +use omgcode_core::provider::LocalModelClient; +use omgcode_core::session::SessionJournal; +use omgcode_core::turn::replay_active; + +use super::Turn; +use crate::context::{ContextMeter, compact_journal, summary_output_tokens}; +use crate::error::{AppError, ErrorKind}; +use crate::tool_mode::tool_replay_allowed; + +/// Apply the context trigger before a turn and return the visible meter. +/// +/// A zero threshold is a `configuration` error. Reaching the trigger without +/// auto-compaction is a `context` error that names the operator's options. +/// With auto-compaction, the summary client compacts the journal and the +/// estimate is recomputed from live settings. No control store is held across +/// the summarizer requests. +pub(super) fn preflight_context( + journal: &mut SessionJournal, + turn: &Turn<'_>, + summary_client: &LocalModelClient, + fixed_extra_bytes: usize, + tools_enabled: bool, +) -> Result { + let settings = turn.settings; + let (data_root, token) = (turn.data_root, turn.token); + if settings.compact_threshold == 0 { + return Err(AppError::new( + ErrorKind::Configuration, + "The compaction threshold must be a positive token count", + )); + } + let budget = ContextBudget { + threshold: settings.compact_threshold, + context_tokens: settings.context_tokens, + max_output_tokens: settings.max_output_tokens, + }; + // Summary chunks are sized from the summary request's own output reserve. + let summary_budget = ContextBudget { + threshold: u64::MAX, + context_tokens: settings.context_tokens, + max_output_tokens: summary_output_tokens(settings.context_tokens), + }; + let (mut estimate, mut assembly_blocked) = estimate_with_user( + journal, + turn, + fixed_extra_bytes, + replay_allowed_now(journal, data_root, tools_enabled), + )?; + let mut summarizer_calls = None; + if budget.should_compact(estimate) || assembly_blocked { + if !settings.auto_compact { + let reason = if assembly_blocked { + "Active history exceeds the prompt assembly limits".to_owned() + } else { + format!( + "The conservative input estimate ({estimate} tokens) reaches the compaction trigger ({})", + budget.trigger() + ) + }; + return Err(AppError::new( + ErrorKind::Context, + format!("{reason}. Use session compact or rerun chat with --auto-compact."), + )); + } + let outcome = compact_journal(journal, data_root, summary_client, token, summary_budget)?; + summarizer_calls = Some(outcome.calls); + // Settings can change during the summarizer requests, so read the + // replay authority again for the compacted history. + (estimate, assembly_blocked) = estimate_with_user( + journal, + turn, + fixed_extra_bytes, + replay_allowed_now(journal, data_root, tools_enabled), + )?; + if budget.should_compact(estimate) || assembly_blocked { + return Err(AppError::new( + ErrorKind::Context, + "Compaction did not reduce the context below the trigger and assembly limits", + )); + } + } + Ok(ContextMeter::new( + journal, + budget, + settings.max_output_tokens, + settings.auto_compact, + estimate, + summarizer_calls, + )) +} + +/// Whether the next request may replay saved tool results, read from live +/// settings at each call. Text-only chat never replays them; a tool turn +/// replays them only under live `model_tools` and `read`, as the runtime +/// does. Unreadable settings fail closed to the withheld form. +fn replay_allowed_now(journal: &SessionJournal, data_root: &Path, tools_enabled: bool) -> bool { + tools_enabled + && tool_replay_allowed( + data_root, + journal.snapshot().workspace_id, + journal.schema_version(), + ) + .unwrap_or(false) +} + +/// The input-only estimate for the next request, and whether its active +/// history plus the pending user message exceeds a prompt-assembly limit that +/// compaction can relieve (message count or total assembled bytes). +fn estimate_with_user( + journal: &SessionJournal, + turn: &Turn<'_>, + fixed_extra_bytes: usize, + tool_results_allowed: bool, +) -> Result<(u64, bool), AppError> { + // Estimate the history this mode will send: saved tool results, and a + // summary built from them, are withheld without live tool authority. + let (mut history, _) = replay_active(journal, tool_results_allowed); + history.push(ConversationMessage { + role: ConversationRole::User, + content: turn.message.to_owned(), + }); + let estimate = estimate_history_input_tokens( + BASE_PROMPT.len(), + turn.instructions, + &history, + fixed_extra_bytes, + ) + .map_err(|error| AppError::new(ErrorKind::Context, error.to_string()))?; + let assembly_blocked = matches!( + prompt::assemble(turn.instructions, &history), + Err(PromptError::TooManyHistoryMessages | PromptError::AssembledPromptTooLarge) + ); + Ok((estimate, assembly_blocked)) +} diff --git a/crates/omgcode-app/src/chat/stream.rs b/crates/omgcode-app/src/chat/stream.rs new file mode 100644 index 0000000..5b3749e --- /dev/null +++ b/crates/omgcode-app/src/chat/stream.rs @@ -0,0 +1,58 @@ +//! Delivery of provisional streamed text to a front end. +//! +//! One sink serves a whole turn. Each model request gets a fresh redactor, so +//! the known server token is removed across piece boundaries before any text +//! leaves the application layer. The journal never sees this path. + +use super::ChatStreamEvent; +use crate::secrets::StreamRedactor; + +/// Forwards redacted text pieces of the current request to the front end. +pub(super) struct StreamSink<'s> { + on_stream: &'s mut dyn FnMut(ChatStreamEvent<'_>), + token: Option<&'s str>, + redactor: StreamRedactor<'s>, +} + +impl<'s> StreamSink<'s> { + /// Build a sink for one turn. `token` is the known server token, if any. + pub(super) fn new( + on_stream: &'s mut dyn FnMut(ChatStreamEvent<'_>), + token: Option<&'s str>, + ) -> Self { + Self { + on_stream, + token, + redactor: StreamRedactor::new(token), + } + } + + /// Announce a model request and start a fresh redactor for it. + pub(super) fn start_request(&mut self) { + self.redactor = StreamRedactor::new(self.token); + (self.on_stream)(ChatStreamEvent::RequestStarted); + } + + /// Redact one piece of the current request and forward what is safe. + pub(super) fn text(&mut self, piece: &str) { + let safe = self.redactor.push(piece); + self.emit(&safe); + } + + /// End a request that succeeded and forward the redactor's withheld tail. + /// + /// A failed request calls nothing: its withheld tail is dropped with the + /// redactor, because the stream is not a reply. + pub(super) fn finish_request(&mut self) { + let redactor = std::mem::replace(&mut self.redactor, StreamRedactor::new(self.token)); + let tail = redactor.finish(); + self.emit(&tail); + } + + /// Never emit an empty `Text`. + fn emit(&mut self, text: &str) { + if !text.is_empty() { + (self.on_stream)(ChatStreamEvent::Text(text)); + } + } +} diff --git a/crates/omgcode-app/src/chat/text.rs b/crates/omgcode-app/src/chat/text.rs new file mode 100644 index 0000000..7f53f28 --- /dev/null +++ b/crates/omgcode-app/src/chat/text.rs @@ -0,0 +1,140 @@ +//! The text-only chat turn. +//! +//! Text-only chat has no model file-read authority. The turn is one provider +//! request. A streamed request watches a cancel token and can be interrupted; a +//! request that is not streamed cannot be (ADR-0050). + +use omgcode_core::prompt::{self, ConversationMessage, ConversationRole}; +use omgcode_core::provider::{LocalModelClient, ProviderError}; +use omgcode_core::session::{MessageRole, RequestManifest, SessionError, SessionJournal}; +use omgcode_core::turn::{CancelToken, replay_active}; + +use super::output::TextTurnOutput; +use super::stream::StreamSink; +use super::tools::CANCELLED_MESSAGE; +use super::{ChatHooks, Turn}; +use crate::context::ContextMeter; +use crate::error::{AppError, ErrorKind}; +use crate::secrets::redact_known_secret; + +/// Largest assistant text kept resumable within the journal and prompt limits. +const MAX_REPLY_BYTES: usize = 16 * 1024; + +/// Run one text-only turn. Deterministic failures return before the turn starts; +/// uncertain storage state after a failed write is preserved, not overwritten. +pub(super) fn run( + journal: &mut SessionJournal, + client: &LocalModelClient, + turn: &Turn<'_>, + reason: &'static str, + manifest: RequestManifest, + context: ContextMeter, + hooks: &mut ChatHooks<'_>, +) -> Result { + let token = turn.token; + // Text-only chat has no model file-read authority, so saved tool results, + // and a summary built from them, are withheld rather than sent again. + let (mut history, withheld_tool_results) = replay_active(journal, false); + for message in &mut history { + message.content = redact_known_secret(&message.content, token); + } + history.push(ConversationMessage { + role: ConversationRole::User, + content: turn.message.to_owned(), + }); + let messages = prompt::assemble(turn.instructions, &history) + .map_err(|error| AppError::new(ErrorKind::Prompt, error.to_string()))?; + journal + .preflight_request(turn.message, &manifest) + .map_err(AppError::session)?; + let request_id = journal.start_turn().map_err(AppError::session)?; + journal + .record_message(MessageRole::User, turn.message) + .map_err(AppError::session)?; + journal + .record_manifest(request_id, manifest) + .map_err(AppError::session)?; + // A streamed request shows provisional redacted text. The journal is + // computed below from the whole reply, exactly as without streaming. + let result = match hooks.on_stream.as_deref_mut() { + Some(on_stream) => { + // Only a streamed request can be interrupted, so only it gets a + // token. The front end receives its clone before the request. + let cancel = CancelToken::new(); + if let Some(hook) = hooks.on_cancel_token.as_deref_mut() { + hook(cancel.clone()); + } + let mut sink = StreamSink::new(on_stream, token); + sink.start_request(); + let mut result = + client.complete_streaming(&messages, &mut |piece: &str| sink.text(piece), &cancel); + // As in the runtime, a cancellation seen once the reply has + // arrived still ends the turn as cancelled, recording no reply. + if result.is_ok() && cancel.is_cancelled() { + result = Err(ProviderError::Cancelled); + } + if result.is_ok() { + sink.finish_request(); + } + result + } + None => client.complete(&messages), + }; + let mut reply = match result { + Ok(reply) => reply, + Err(ProviderError::Cancelled) => { + journal.fail_turn(request_id, "cancelled").map_err(|_| { + AppError::new( + ErrorKind::Persistence, + "Turn was cancelled and the cancellation could not be recorded", + ) + })?; + return Err(AppError::new(ErrorKind::Cancelled, CANCELLED_MESSAGE)); + } + Err(error) => { + journal + .fail_turn(request_id, "provider_error") + .map_err(|_| { + AppError::new( + ErrorKind::Persistence, + "Provider failed and failure could not be recorded", + ) + })?; + return Err(AppError::provider(error)); + } + }; + reply.content = redact_known_secret(&reply.content, token); + // Keep every accepted reply resumable within the journal and prompt limits. + // The wire response limit also covers protocol metadata, so it is larger. + if reply.content.len() > MAX_REPLY_BYTES { + journal + .fail_turn(request_id, "response_too_large") + .map_err(AppError::session)?; + return Err(AppError::new( + ErrorKind::Provider, + "Assistant text exceeds the 16 KiB session message limit", + )); + } + if let Err(error) = journal.record_message(MessageRole::Assistant, &reply.content) { + if matches!( + error, + SessionError::LimitReached | SessionError::InvalidValue(_) + ) { + journal + .fail_turn(request_id, "response_too_large") + .map_err(AppError::session)?; + } + // Failed writes/syncs can have changed storage. Do not overwrite that + // uncertain state with a fabricated terminal event or an automatic retry. + return Err(AppError::session(error)); + } + journal + .complete_turn(request_id) + .map_err(AppError::session)?; + Ok(TextTurnOutput { + reply, + reason, + withheld_tool_results, + context, + }) +} diff --git a/crates/omgcode-app/src/chat/tools.rs b/crates/omgcode-app/src/chat/tools.rs new file mode 100644 index 0000000..e635384 --- /dev/null +++ b/crates/omgcode-app/src/chat/tools.rs @@ -0,0 +1,313 @@ +//! The tool-enabled chat turn. +//! +//! Authority comes from the live settings preference, rechecked by the core +//! runtime before every tool call, and each call stays inside the selected +//! workspace. The edit and command tools also need a trusted single-use +//! approval, which the core checks; nothing here grants or widens it. + +use std::cell::RefCell; +use std::time::Duration; + +use omgcode_core::policy::WorkspaceId; +use omgcode_core::prompt::ModelMessage; +use omgcode_core::provider::{LocalModelClient, LocalToolModel, ProviderError}; +use omgcode_core::session::{RequestManifest, SessionJournal}; +use omgcode_core::turn::{ + ModelError, ToolModel, ToolModelReply, ToolModelRequest, TurnError, TurnLimits, TurnReport, + TurnRuntime, +}; +use serde_json::Value; + +use super::output::{ToolTurnOutput, ToolTurnReply, ToolTurnSummary}; +use super::stream::StreamSink; +use super::{ChatHooks, Turn}; +use crate::context::ContextMeter; +use crate::error::{AppError, ErrorKind}; +use crate::secrets::redact_known_secret; + +/// Cooperative deadline for one tool-enabled chat turn. +/// +/// Each local model request may take up to 30 seconds, and a turn makes at most +/// four requests, so the 30-second library default would stop ordinary turns. +const CHAT_TURN_DEADLINE: Duration = Duration::from_secs(120); + +/// Runtime limits for tool-enabled chat: library maxima with a chat deadline. +fn chat_limits() -> TurnLimits { + TurnLimits { + deadline: CHAT_TURN_DEADLINE, + ..TurnLimits::default() + } +} + +/// Run one tool-enabled turn through the core runtime. +/// +/// `hooks.on_cancel_token` is called once, right after the runtime is built +/// and before the first model request. +pub(super) fn run( + journal: &mut SessionJournal, + workspace_id: WorkspaceId, + client: &LocalModelClient, + turn: &Turn<'_>, + manifest: RequestManifest, + context: ContextMeter, + hooks: &mut ChatHooks<'_>, +) -> Result { + let token = turn.token; + // Streaming is on only when the front end asked for provisional text. One + // sink serves every request of the turn; each request restarts it. + let sink = hooks + .on_stream + .as_deref_mut() + .map(|on_stream| RefCell::new(StreamSink::new(on_stream, token))); + let mut forward = |piece: &str| { + if let Some(sink) = &sink { + // The sink is borrowed only for the span of one call and its + // callback cannot reach this cell, so a failed borrow cannot + // happen. If it did, dropping a provisional piece is harmless. + if let Ok(mut sink) = sink.try_borrow_mut() { + sink.text(piece); + } + } + }; + let inner = if sink.is_some() { + LocalToolModel::streaming(client, &mut forward) + } else { + LocalToolModel::new(client) + }; + let mut model = RedactingModel { + inner, + token, + stream: sink.as_ref(), + secret_in_tool_call: false, + }; + // Tool results are recorded before the next model request, so they need + // their own redaction; the model wrapper sees them only afterwards. + let redact_tool_text = |text: &str| redact_known_secret(text, token); + let report: TurnReport = TurnRuntime::new(journal, turn.data_root, workspace_id, chat_limits()) + .and_then(|runtime| { + if let Some(hook) = hooks.on_cancel_token.as_deref_mut() { + hook(runtime.cancel_token()); + } + runtime.with_tool_text_filter(&redact_tool_text).run( + &mut model, + turn.message, + turn.instructions, + manifest, + ) + }) + .map_err(|error| { + if model.secret_in_tool_call { + return AppError::new( + ErrorKind::Provider, + "The model returned a tool call containing the configured server token; it was not recorded or run", + ); + } + turn_error(error, model.inner.last_error()) + })?; + Ok(ToolTurnOutput { + reply: ToolTurnReply { + content: report.reply, + finish_reason: report.finish_reason, + }, + model_tools: ToolTurnSummary { + mode: "enabled", + workspace_id: workspace_id.to_string(), + rounds: report.rounds, + tool_calls: report.tool_calls, + tool_bytes: report.tool_bytes, + withheld_tool_results: report.withheld_tool_results, + }, + context, + }) +} + +/// Remove a known local server token from text sent to and received from the model. +/// +/// A tool call that contains the token is refused rather than redacted, because +/// editing its arguments could change the requested path. The runtime records +/// calls before any result filter runs, so the refusal happens here. +struct RedactingModel<'a, 's> { + inner: LocalToolModel<'a>, + token: Option<&'a str>, + /// The turn's stream sink, shared with `inner`'s text callback. + stream: Option<&'a RefCell>>, + secret_in_tool_call: bool, +} + +impl RedactingModel<'_, '_> { + /// Run `step` on the stream sink, when the turn streams. + fn with_stream(&self, step: impl for<'x> FnOnce(&mut StreamSink<'x>)) { + // Borrowed only for this call, which no callback can re-enter. + if let Some(Ok(mut sink)) = self.stream.map(RefCell::try_borrow_mut) { + step(&mut sink); + } + } +} + +impl ToolModel for RedactingModel<'_, '_> { + fn complete(&mut self, request: ToolModelRequest<'_>) -> Result { + let messages: Vec = request + .messages + .iter() + .map(|message| ModelMessage { + role: message.role, + content: redact_known_secret(&message.content, self.token), + }) + .collect(); + self.with_stream(|sink| sink.start_request()); + let result = self.inner.complete(ToolModelRequest { + messages: &messages, + tools: request.tools, + cancel: request.cancel, + }); + if result.is_ok() { + self.with_stream(|sink| sink.finish_request()); + } + let mut reply = result?; + if let Some(token) = self.token.filter(|token| !token.is_empty()) + && reply + .tool_calls + .iter() + .any(|call| contains_secret(call, token, true)) + { + self.secret_in_tool_call = true; + return Err(ModelError::InvalidResponse); + } + reply.content = redact_known_secret(&reply.content, self.token); + Ok(reply) + } +} + +/// Report whether any key or string in a raw tool-call value contains `token`. +/// +/// Tool-call arguments arrive as a JSON-encoded string, so a string that parses +/// as JSON is searched once more after decoding. Decoding is not repeated +/// inside that nested value. +fn contains_secret(value: &Value, token: &str, decode_nested: bool) -> bool { + match value { + Value::String(text) => { + text.contains(token) + || (decode_nested + && serde_json::from_str::(text) + .is_ok_and(|nested| contains_secret(&nested, token, false))) + } + Value::Array(items) => items + .iter() + .any(|item| contains_secret(item, token, decode_nested)), + Value::Object(fields) => fields + .iter() + .any(|(key, item)| key.contains(token) || contains_secret(item, token, decode_nested)), + Value::Null | Value::Bool(_) | Value::Number(_) => false, + } +} + +/// Map a runtime failure to a stable application error without echoing model text. +fn turn_error(error: TurnError, provider: Option) -> AppError { + match error { + TurnError::Model(_) => provider.map_or_else( + || AppError::new(ErrorKind::Provider, "Local model request failed"), + AppError::provider, + ), + TurnError::ToolDenied(_) => AppError::new( + ErrorKind::Permission, + "A model tool call was denied by the current workspace preferences", + ), + TurnError::InvalidToolCall(error) => AppError::new( + ErrorKind::ToolCall, + format!("The model returned an invalid tool call: {error}"), + ), + TurnError::Limit(limit) => AppError::new( + ErrorKind::ToolLimit, + format!("Turn limit exceeded: {limit}"), + ), + TurnError::Session(error) => AppError::session(error), + TurnError::Prompt(error) => AppError::new(ErrorKind::Prompt, error.to_string()), + TurnError::Archived => AppError::new( + ErrorKind::Session, + "Restore the archived session before chatting", + ), + TurnError::ActiveTurn => AppError::new(ErrorKind::Session, ACTIVE_TURN_MESSAGE), + TurnError::Cancelled => AppError::new(ErrorKind::Cancelled, CANCELLED_MESSAGE), + other => AppError::new(ErrorKind::Turn, other.to_string()), + } +} + +/// The message of a cancelled turn, on the text-only and the tool-enabled path. +pub(super) const CANCELLED_MESSAGE: &str = "turn was cancelled"; + +/// The message for a session whose previous turn never reached a terminal event. +pub(super) const ACTIVE_TURN_MESSAGE: &str = "The previous turn is incomplete; inspect it, then use session interrupt to mark it failed without retrying"; + +#[cfg(test)] +mod tests { + use omgcode_core::session::SessionError; + use omgcode_core::turn::TurnLimit; + + use super::*; + + fn pair(error: AppError) -> (&'static str, String) { + (error.code(), error.into_message()) + } + + #[test] + fn model_failures_keep_the_provider_message_or_the_generic_one() { + assert_eq!( + pair(turn_error( + TurnError::Model(ModelError::InvalidResponse), + Some(ProviderError::Transport) + )), + ("provider", "local model transport failed".to_owned()) + ); + assert_eq!( + pair(turn_error( + TurnError::Model(ModelError::InvalidResponse), + None + )), + ("provider", "Local model request failed".to_owned()) + ); + } + + #[test] + fn runtime_failures_map_to_the_published_codes_and_messages() { + assert_eq!( + pair(turn_error(TurnError::Archived, None)), + ( + "session", + "Restore the archived session before chatting".to_owned() + ) + ); + assert_eq!( + pair(turn_error(TurnError::ActiveTurn, None)), + ("session", ACTIVE_TURN_MESSAGE.to_owned()) + ); + assert_eq!( + pair(turn_error(TurnError::Limit(TurnLimit::Rounds), None)), + ( + "tool_limit", + "Turn limit exceeded: model round limit".to_owned() + ) + ); + assert_eq!( + pair(turn_error(TurnError::Cancelled, None)), + ("cancelled", "turn was cancelled".to_owned()) + ); + assert_eq!( + pair(turn_error( + TurnError::Session(SessionError::LimitReached), + None + )), + ("session", SessionError::LimitReached.to_string()) + ); + } + + #[test] + fn secrets_are_found_in_keys_values_and_nested_arguments() { + let call = serde_json::json!({ + "id": "c1", + "function": {"name": "read", "arguments": "{\"path\":\"has-\\u0073ecret-9\"}"} + }); + assert!(contains_secret(&call, "secret-9", true)); + assert!(!contains_secret(&call, "secret-9", false)); + assert!(!contains_secret(&call, "other", true)); + } +} diff --git a/crates/omgcode-app/src/context.rs b/crates/omgcode-app/src/context.rs new file mode 100644 index 0000000..75c7da1 --- /dev/null +++ b/crates/omgcode-app/src/context.rs @@ -0,0 +1,210 @@ +//! Bounded conversation compaction and the visible context meter. +//! +//! The core owns the cutoff, journal event, and summary validation. This module +//! supplies the local-model summarizer and applies the configured secret +//! redaction before the summary is recorded or shown. Summaries are untrusted +//! evidence and grant no authority. + +use std::path::Path; + +use omgcode_core::context::{ + COMPACTION_INSTRUCTIONS, CompactionLimits, CompactionOutcome, ContextBudget, ContextError, + MAX_SUMMARY_BYTES, SummaryReply, compact, +}; +use omgcode_core::prompt::{self, ConversationMessage}; +use omgcode_core::provider::LocalModelClient; +use omgcode_core::session::SessionJournal; +use serde::Serialize; + +use crate::error::{AppError, ErrorKind}; +use crate::secrets::redact_known_secret; +use crate::tool_mode::tool_replay_allowed; + +/// Most output tokens reserved for one summary request. +const MAX_SUMMARY_OUTPUT_TOKENS: u64 = 2048; + +/// Output reserve for an automatic summary request: a quarter of the context, +/// at most 2048 tokens and at least one. +pub fn summary_output_tokens(context_tokens: u64) -> u32 { + u32::try_from((context_tokens / 4).clamp(1, MAX_SUMMARY_OUTPUT_TOKENS)).unwrap_or(1) +} + +/// Compact the active context with the selected local model. +/// +/// `summary_budget` sizes the summarizer chunks; its `max_output_tokens` must +/// be the summary client's output reserve. Live `model_tools` and `read` +/// settings for the session's workspace are read before the summarizer input +/// is built and again before each request that carries tool-derived content; +/// a withdrawal stops compaction with a `permission` error and records nothing. +/// Unreadable settings fail closed: no tool data reaches the summarizer. Other +/// failures are `context`. `token` is removed from summarizer input and output. +pub fn compact_journal( + journal: &mut SessionJournal, + data_root: &Path, + client: &LocalModelClient, + token: Option<&str>, + summary_budget: ContextBudget, +) -> Result { + let model = client.model().to_owned(); + let workspace = journal.snapshot().workspace_id; + let schema_version = journal.schema_version(); + compact( + journal, + CompactionLimits { + max_chunk_bytes: summary_budget.summarizer_chunk_bytes(), + max_summary_bytes: MAX_SUMMARY_BYTES, + }, + model, + // Unreadable settings fail closed: no tool data reaches the summarizer. + || tool_replay_allowed(data_root, workspace, schema_version).unwrap_or(false), + |history: &[ConversationMessage]| { + let redacted: Vec = history + .iter() + .map(|message| ConversationMessage { + role: message.role, + content: redact_known_secret(&message.content, token), + }) + .collect(); + let messages = prompt::assemble(COMPACTION_INSTRUCTIONS, &redacted) + .map_err(|_| ContextError::TooLarge)?; + let reply = client.complete(&messages).map_err(ContextError::Provider)?; + let usage = reply.usage.as_ref(); + Ok(SummaryReply { + text: redact_known_secret(&reply.content, token), + prompt_tokens: usage.and_then(|usage| usage.prompt_tokens), + completion_tokens: usage.and_then(|usage| usage.completion_tokens), + }) + }, + ) + .map_err(|error| match error { + ContextError::AuthorityWithdrawn => AppError::new(ErrorKind::Permission, error.to_string()), + error => AppError::new(ErrorKind::Context, error.to_string()), + }) +} + +/// Visible, conservative context accounting for one chat turn. +#[derive(Clone, Debug, Serialize)] +pub struct ContextMeter { + estimated_input_tokens: u64, + context_tokens: u64, + max_output_tokens: u32, + effective_trigger: u64, + auto_compaction: bool, + estimate_scope: &'static str, + #[serde(skip_serializing_if = "Option::is_none")] + compaction: Option, +} + +impl ContextMeter { + /// Build the meter from a finished pre-turn estimate and the active journal. + pub(crate) fn new( + journal: &SessionJournal, + budget: ContextBudget, + max_output_tokens: u32, + auto_compaction: bool, + estimate: u64, + summarizer_calls: Option, + ) -> Self { + let compaction = journal + .snapshot() + .compaction + .as_ref() + .map(|record| CompactionMeter { + compacted_messages: record.compacted_messages, + retained_messages: u64::try_from(journal.snapshot().messages.len()) + .unwrap_or(u64::MAX) + .saturating_sub(record.compacted_messages), + summarizer_calls, + summary_sha256: record.summary_sha256.clone(), + }); + Self { + estimated_input_tokens: estimate, + context_tokens: budget.context_tokens, + max_output_tokens, + effective_trigger: budget.trigger(), + auto_compaction, + estimate_scope: "pre_turn_conservative", + compaction, + } + } + + /// The conservative input estimate for the turn, in tokens. + pub fn estimated_input_tokens(&self) -> u64 { + self.estimated_input_tokens + } + + /// The validated context capacity of the selected model. + pub fn context_tokens(&self) -> u64 { + self.context_tokens + } + + /// The output reserve for the turn. + pub fn max_output_tokens(&self) -> u32 { + self.max_output_tokens + } + + /// The estimate at which compaction triggers. + pub fn effective_trigger(&self) -> u64 { + self.effective_trigger + } + + /// Whether compaction before a turn was enabled. + pub fn auto_compaction(&self) -> bool { + self.auto_compaction + } + + /// The fixed label of what the estimate covers. + pub fn estimate_scope(&self) -> &'static str { + self.estimate_scope + } + + /// The active compaction boundary, when the session has one. + pub fn compaction(&self) -> Option<&CompactionMeter> { + self.compaction.as_ref() + } +} + +/// The active compaction boundary and summary identity. +#[derive(Clone, Debug, Serialize)] +pub struct CompactionMeter { + compacted_messages: u64, + retained_messages: u64, + #[serde(skip_serializing_if = "Option::is_none")] + summarizer_calls: Option, + summary_sha256: String, +} + +impl CompactionMeter { + /// Journal messages now represented by the summary. + pub fn compacted_messages(&self) -> u64 { + self.compacted_messages + } + + /// Journal messages retained verbatim after the boundary. + pub fn retained_messages(&self) -> u64 { + self.retained_messages + } + + /// Summarizer calls made during this turn's preflight, if it compacted. + pub fn summarizer_calls(&self) -> Option { + self.summarizer_calls + } + + /// Digest of the active summary text. + pub fn summary_sha256(&self) -> &str { + &self.summary_sha256 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_summary_reserve_is_a_quarter_of_the_context_within_bounds() { + assert_eq!(summary_output_tokens(0), 1); + assert_eq!(summary_output_tokens(3), 1); + assert_eq!(summary_output_tokens(400), 100); + assert_eq!(summary_output_tokens(1_000_000), 2048); + } +} diff --git a/crates/omgcode-app/src/credential_source.rs b/crates/omgcode-app/src/credential_source.rs new file mode 100644 index 0000000..4967b73 --- /dev/null +++ b/crates/omgcode-app/src/credential_source.rs @@ -0,0 +1,293 @@ +//! The one place the local adapter's endpoint and server token are obtained +//! (ADR-0047, ADR-0048). +//! +//! The operator chooses a [`ConnectionSource`] once, outside any model or +//! webview influence. [`resolve_local_connection`] then returns the endpoint +//! and the optional token for one turn from that one source, so a stored token +//! can never be paired with an endpoint from somewhere else: +//! +//! - [`ConnectionSource::Environment`] keeps the prototype behaviour: the +//! endpoint is the one given (the command line or its default) and the token +//! is the `OMGCODE_LM_TOKEN` variable, through [`read_lm_token`]. +//! - [`ConnectionSource::Profile`] reads the endpoint recorded with a +//! recorded `lm_studio` provider profile and the credential bound to it from +//! protected OS storage. The environment variable is not read at all, and +//! every failure ends the turn with a `credential` error. There is no +//! fallback to no token, to another token, or to another endpoint. A profile +//! that records no endpoint is refused with `endpoint_not_recorded`. +//! +//! Order of operations for a profile, with the settings store open throughout so +//! a rotation cannot delete the item between the binding lookup and the load: +//! +//! 1. open the existing settings store; +//! 2. `provider_credential_binding`, which refuses an unknown profile, a profile +//! of another provider, a profile with no recorded endpoint, a +//! `subscription` profile and an unbound `api_key` profile, and which +//! touches no native storage; +//! 3. a `none` profile ends here with its endpoint and no token, so it works on +//! a host whose protected store is unavailable; +//! 4. only for a bound `api_key` profile, open the native credential store and +//! call `load_provider_credential`; +//! 5. close both stores before returning, so none is held while a model request +//! is in flight. Nothing is cached: each turn resolves again. + +use std::fmt; +use std::path::Path; + +use omgcode_core::control::{ + ControlError, ControlStore, CredentialStore, LoadedProviderConnection, + ProviderCredentialUseError, ProviderKind, Secret, +}; +use zeroize::Zeroizing; + +use crate::error::{AppError, ErrorKind}; +use crate::secrets::read_lm_token; + +/// Where a desktop process gets the optional local server token. The endpoint +/// of a desktop process is fixed at launch and held beside this value. +/// +/// A profile name is an operator-chosen label, not a secret, so this type has +/// ordinary `Debug` output. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum TokenSource { + /// The `OMGCODE_LM_TOKEN` process environment variable. + Environment, + /// The credential bound to the named recorded provider profile, whose own + /// recorded endpoint is the only endpoint it can use. + Profile(String), +} + +impl TokenSource { + /// The connection source for one turn, given the endpoint this process was + /// started with. A profile source ignores `launch_endpoint`: the profile's + /// recorded endpoint is the only one it can use. + pub fn connection(&self, launch_endpoint: &str) -> ConnectionSource { + match self { + Self::Environment => ConnectionSource::Environment { + endpoint: launch_endpoint.to_owned(), + }, + Self::Profile(name) => ConnectionSource::Profile(name.clone()), + } + } +} + +/// Where a front end gets the local adapter's endpoint and optional token. +/// +/// A profile name and an endpoint are not secrets, so this type has ordinary +/// `Debug` output. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum ConnectionSource { + /// The given endpoint (the command line or its default), with the token + /// from the `OMGCODE_LM_TOKEN` process environment variable. + Environment { + /// The endpoint. It is validated when the clients are built. + endpoint: String, + }, + /// The endpoint and the credential recorded for the named provider + /// profile. Nothing else supplies either. + Profile(String), +} + +/// The local server token, held only while one turn needs it. +/// +/// The text zeroes itself when dropped. The type has no `Serialize`, and its +/// `Debug` output is a fixed redaction. +pub struct LmToken(Zeroizing); + +impl LmToken { + /// Take ownership of an environment token. The caller's `String` is moved, + /// not copied. + pub(crate) fn new(value: String) -> Self { + Self(Zeroizing::new(value)) + } + + /// Borrow the token text for a request header or for redaction. It must not + /// be logged, serialized, or placed in an error. + pub fn expose(&self) -> &str { + &self.0 + } +} + +impl fmt::Debug for LmToken { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("LmToken()") + } +} + +/// The endpoint and the optional token for one turn, from one source. +/// +/// `Debug` prints the endpoint, which is not secret, and the token's fixed +/// redaction. The type has no `Serialize`. +#[derive(Debug)] +pub struct LocalConnection { + /// The endpoint every request of this turn uses. + pub endpoint: String, + /// The optional server token, zeroed on drop. + pub token: Option, +} + +/// Obtain the endpoint and token for one turn from `source`. +/// +/// `data_root` locates the settings store and the credential store; it is not +/// read for [`ConnectionSource::Environment`]. A `none` profile gives its +/// endpoint and no token. +/// +/// # Errors +/// +/// [`ConnectionSource::Environment`] fails exactly as [`read_lm_token`] does. +/// [`ConnectionSource::Profile`] fails with a `credential` error when no +/// settings store exists, when another process holds it (no retry here), when +/// core refuses the profile (including `endpoint_not_recorded`), when the +/// native store fails, or when the stored secret is not non-empty UTF-8 text. +/// The message begins with a stable cause code. +pub fn resolve_local_connection( + data_root: &Path, + source: &ConnectionSource, +) -> Result { + match source { + ConnectionSource::Environment { endpoint } => Ok(LocalConnection { + endpoint: endpoint.clone(), + token: read_lm_token()?.map(LmToken::new), + }), + ConnectionSource::Profile(name) => { + let (endpoint, secret) = load_profile_connection(data_root, name)?; + let token = match secret { + Some(secret) => Some(token_from_secret(&secret)?), + None => None, + }; + Ok(LocalConnection { endpoint, token }) + } + } +} + +/// Load the profile's endpoint and secret while the settings store is open, +/// then close both stores. +fn load_profile_connection( + data_root: &Path, + name: &str, +) -> Result<(String, Option), AppError> { + let control = match ControlStore::open_existing(data_root) { + Ok(Some(control)) => control, + Ok(None) => { + return Err(credential_error(format!( + "no settings store exists, so provider profile {name} cannot be used" + ))); + } + Err(ControlError::Busy) => { + return Err(credential_error( + "the settings store is in use by another process; try again".to_owned(), + )); + } + Err(error) => { + return Err(credential_error(format!( + "settings_unusable: the settings store could not be opened: {error}" + ))); + } + }; + // Every refusal and the `none` profile are decided here, with no native + // store access, so they report their own cause on any host. + let keyless_endpoint = match control.provider_credential_binding(name, ProviderKind::LmStudio) { + Ok(connection) if connection.binding().is_none() => { + Some(connection.endpoint().as_str().to_owned()) + } + Ok(_) => None, + Err(error) => return Err(credential_error(format!("{}: {error}", error.code()))), + }; + if let Some(endpoint) = keyless_endpoint { + return Ok((endpoint, None)); + } + // The settings store stays open across the load, so a rotation cannot + // delete the item between the binding lookup and the read. The native + // store is released at the end of this block, before the settings store. + let loaded = { + let credentials = CredentialStore::open(data_root).map_err(|error| { + credential_error(format!( + "{}: {error}", + ProviderCredentialUseError::native_code(&error) + )) + })?; + control.load_provider_credential(&credentials, name, ProviderKind::LmStudio) + }; + drop(control); + loaded + .map(LoadedProviderConnection::into_parts) + .map_err(|error| credential_error(format!("{}: {error}", error.code()))) +} + +/// Convert a stored secret without an intermediate un-zeroed copy. +/// +/// The bytes are validated in place and copied once into a zeroing string; the +/// [`Secret`] zeroes its own buffer when it drops. A failed conversion never +/// builds an owned error value from the bytes. +fn token_from_secret(secret: &Secret) -> Result { + let text = std::str::from_utf8(secret.expose()).map_err(|_| { + credential_error("credential_invalid: the stored credential is not UTF-8 text".to_owned()) + })?; + if text.is_empty() { + return Err(credential_error( + "credential_invalid: the stored credential is empty".to_owned(), + )); + } + Ok(LmToken(Zeroizing::new(text.to_owned()))) +} + +fn credential_error(message: String) -> AppError { + AppError::new(ErrorKind::Credential, message) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn debug_output_never_contains_the_token() { + let token = LmToken::new("fake-token-value".to_owned()); + assert_eq!(token.expose(), "fake-token-value"); + assert_eq!(format!("{token:?}"), "LmToken()"); + assert_eq!(format!("{token:#?}"), "LmToken()"); + let connection = LocalConnection { + endpoint: "http://127.0.0.1:1234/v1".to_owned(), + token: Some(token), + }; + let printed = format!("{connection:?}"); + assert!(!printed.contains("fake-token-value"), "{printed}"); + assert!(printed.contains("http://127.0.0.1:1234/v1"), "{printed}"); + } + + #[test] + fn a_secret_must_be_non_empty_utf8() { + let secret = Secret::new(b"fake-token".to_vec()).expect("secret"); + assert_eq!( + token_from_secret(&secret).expect("token").expose(), + "fake-token" + ); + let secret = Secret::new(vec![0xff, 0xfe, 0x41]).expect("secret"); + let error = token_from_secret(&secret).expect_err("refused"); + assert_eq!(error.code(), "credential"); + assert!(error.message().starts_with("credential_invalid")); + } + + #[test] + fn a_profile_source_has_plain_debug_output() { + let source = TokenSource::Profile("lms".to_owned()); + assert_eq!(format!("{source:?}"), "Profile(\"lms\")"); + let source = ConnectionSource::Profile("lms".to_owned()); + assert_eq!(format!("{source:?}"), "Profile(\"lms\")"); + } + + /// A profile source never takes the launch endpoint, so no endpoint from + /// elsewhere can sit beside a selected profile. + #[test] + fn a_profile_token_source_ignores_the_launch_endpoint() { + assert_eq!( + TokenSource::Environment.connection("http://127.0.0.1:1/v1"), + ConnectionSource::Environment { + endpoint: "http://127.0.0.1:1/v1".to_owned() + } + ); + assert_eq!( + TokenSource::Profile("lms".to_owned()).connection("http://127.0.0.1:1/v1"), + ConnectionSource::Profile("lms".to_owned()) + ); + } +} diff --git a/crates/omgcode-app/src/desktop.rs b/crates/omgcode-app/src/desktop.rs new file mode 100644 index 0000000..ac53d08 --- /dev/null +++ b/crates/omgcode-app/src/desktop.rs @@ -0,0 +1,279 @@ +//! The front-end-neutral desktop service (ADR-0044, ADR-0045, ADR-0046). +//! +//! [`DesktopService`] is what a thin shell adapter calls, one method per +//! command, from blocking tasks. It grants no authority. Every decision that +//! matters (workspace preferences, tool approvals, checkpointed edits, +//! confinement, journal validation) is made by `omgcode-core`; this module +//! orders calls to core, bounds them, and shapes their results for the +//! interface. +//! +//! # What the service never does +//! +//! - It accepts no filesystem path from a caller. Sessions are addressed by +//! UUID, workspaces by their registered identifier, approvals by identifier. +//! A journal path is resolved only through `SessionIndex::journal_path`. +//! - It returns no journal path and no absolute path except workspace folder +//! display text, which is escaped by core's `display_safe_line`. +//! - It never returns, stores in a response, or logs the local server token. +//! The token is resolved for one turn from the operator-chosen +//! [`TokenSource`] (the environment, or a provider profile's stored +//! credential, ADR-0047), held in a zeroing type with a redacted `Debug`, +//! removed from every recorded and returned message, and dropped when the +//! turn thread ends. Nothing is cached between turns. The interface cannot +//! choose or see the source. +//! +//! # State and locks +//! +//! The service state is shared by the service and by each running chat turn's +//! thread through one `Arc`. It holds: +//! +//! | field | guards | +//! | --- | --- | +//! | `index` | the one `SessionIndex` connection and its refresh bookkeeping | +//! | `turns` | the table of chat turns, their cancel tokens and thread handles | +//! | `reviews` | outstanding single-use approval review tokens | +//! | `events` | the one chat event subscriber and the running turns' previews | +//! | `pending_cache` | an atomic copy of the last pending-approval count read | +//! +//! **Lock order** when two are ever needed: `turns`, then `index`, then +//! `reviews`. The code takes one at a time. The `events` lock is never held +//! together with the `turns` lock (where both are needed, `turns` is taken and +//! released first) and never while `index` or `reviews` is taken. It is held +//! while a subscriber sink runs, so a sink must not block or call the service +//! (see [`ChatEventSink`]). No service lock and no open +//! control store is held across provider I/O, a chat turn, a journal +//! inspection, a thread join, or a sleep. The one deliberate exception is the +//! `index` lock during a refresh: a refresh needs `&mut SessionIndex`, so it +//! holds the lock for at most two seconds plus one chunk of 200 journal +//! inspections. A poisoned lock never panics and is never ignored: the call +//! returns `DesktopError` code `internal`. +//! +//! # Chat turns +//! +//! `chat_send` validates everything that can be checked without running the +//! turn, reserves a slot in the table under the `turns` lock, and starts one +//! named thread (`omgcode-turn`). The thread owns the journal handle, the +//! clients, and the token for the whole turn. It opens the journal, runs the +//! turn, drops the journal (which releases the writer lock), refreshes that +//! one session in the index, and only then records the outcome in its slot and +//! pushes `finished` to the event subscriber. It does not hold the `turns` lock while the turn runs. At most two turns run at +//! once. +//! +//! The slot keeps the thread's `JoinHandle`. `chat_status`, `chat_send`, and +//! the session commands reap finished threads: they take the handle out of the +//! slot under the lock, join it after releasing the lock, and record a thread +//! that panicked as `failed` with `internal`. Finished results stay until the +//! next `chat_send` for that session; at most 64 finished entries are kept and +//! the oldest is evicted first. +//! +//! Cancellation is cooperative. A turn hands its `CancelToken` to the slot when +//! it streams, or when it is tool-enabled. A streamed model request watches the +//! token and stops within about 25 ms (ADR-0050). A text-only turn whose +//! request is not streamed has no token, so a cancel request on it changes +//! nothing and the status stays `running`. +//! +//! # Shutdown +//! +//! [`DesktopService::shutdown`] requests cancellation of every running turn +//! and polls `JoinHandle::is_finished` until the grace period ends. A streamed +//! request is interrupted, but a request that is not streamed cannot be, so +//! such a turn can outlast the grace period. Shutdown then returns without +//! waiting; the abandoned thread leaves +//! its journal with an open turn, which the operator can close later with the +//! interrupt command or the trusted CLI. After shutdown no new turn starts. + +mod approvals; +mod chat; +mod error; +mod events; +mod review_tokens; +mod sessions; +mod state; +mod turns; +mod wire; +mod workspaces; + +use std::path::{Path, PathBuf}; +use std::sync::atomic::AtomicU64; +use std::sync::{Arc, Mutex}; + +use omgcode_core::session_index::SessionIndex; + +pub use self::error::DesktopError; +pub use self::events::ChatEventSink; +use self::events::EventHub; +use self::review_tokens::ReviewTokens; +use self::state::IndexState; +pub use self::turns::ShutdownReport; +use self::turns::TurnTable; +pub use self::wire::{ + ApprovalDecideRequest, ApprovalDetail, ApprovalField, ApprovalList, ApprovalOperationDto, + ApprovalRequest, ApprovalStateDto, ApprovalView, ApprovalsSummary, Binding, ChatEvent, + ChatOutcome, ChatSendRequest, ChatSettingsDto, ChatState, ChatStatus, ContextMeterDto, + Decision, EntryKind, MessageRoleDto, SearchHit, SearchRequest, SearchResult, SearchRoleDto, + SessionCreateRequest, SessionRequest, SessionStatus, SessionSummary, SessionTree, SnippetPart, + ToolOutcomeDto, ToolsMode, TranscriptEntry, TranscriptPage, TranscriptRequest, TreeRequest, + TreeSort, TreeView, WorkspaceChoice, WorkspaceGroup, WorkspaceList, +}; +use crate::{AppError, ErrorKind, LmToken, LocalConnection, TokenSource, resolve_local_connection}; + +/// Fixed settings of one desktop process. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct DesktopConfig { + /// Trusted application storage. It comes from the operator, never from the + /// interface. + pub data_root: PathBuf, + /// The chat endpoint, fixed for the process lifetime. A chat request whose + /// endpoint differs byte-for-byte is refused. With a profile token source + /// it is the profile's recorded endpoint, resolved once at launch; each + /// turn resolves the profile again and refuses the turn with + /// `endpoint_changed` when the profile's endpoint no longer equals it. + pub endpoint: String, + /// Where each chat turn gets the optional local server token (and, for a + /// profile, the endpoint it must still equal). It is fixed + /// for the process lifetime and never comes from the interface. + pub token_source: TokenSource, + /// Whether chat turns send streamed model requests and push their + /// provisional text to the subscriber (ADR-0049). When `false`, turns send + /// non-streaming requests and only `finished` events are pushed. + pub stream: bool, +} + +/// A replacement token supplier for an embedder or a test that must avoid the +/// process environment and the credential store. +/// +/// [`DesktopService::open`] resolves `DesktopConfig::token_source` with +/// [`resolve_local_connection`]; [`DesktopService::open_with_token_source`] uses this +/// function instead and ignores the configured source. +pub type InjectedTokenFn = fn() -> Result, AppError>; + +/// How a turn obtains its token. +#[derive(Clone)] +enum TokenResolver { + /// The production path: [`resolve_local_connection`]. + Source(TokenSource), + /// A test or embedder supplier. + Injected(InjectedTokenFn), +} + +impl TokenResolver { + /// Resolve the endpoint and token for one turn. Nothing is retained. + /// + /// `launch_endpoint` is the endpoint this process was started with. An + /// environment or injected source uses it. A profile source takes the + /// profile's recorded endpoint instead, which must still equal the launch + /// endpoint: a profile removed and added again with another endpoint while + /// the process ran is refused with `endpoint_changed` (ADR-0048), because + /// the webview and the window show the launch value. + fn resolve( + &self, + data_root: &Path, + launch_endpoint: &str, + ) -> Result { + match self { + Self::Source(source) => { + let connection = + resolve_local_connection(data_root, &source.connection(launch_endpoint))?; + if connection.endpoint != launch_endpoint { + return Err(AppError::new( + ErrorKind::Credential, + "endpoint_changed: the provider profile's endpoint changed since launch; restart the application", + )); + } + Ok(connection) + } + Self::Injected(supplier) => Ok(LocalConnection { + endpoint: launch_endpoint.to_owned(), + token: supplier()?.map(LmToken::new), + }), + } + } +} + +/// The state shared by the service and its turn threads. +/// +/// Owners: the [`DesktopService`] and one clone per running turn thread. The +/// last owner frees it. There is no reference cycle: the thread handles that +/// the table keeps do not own this `Arc`. +struct Inner { + data_root: PathBuf, + endpoint: String, + token: TokenResolver, + /// Whether turns send streamed requests ([`DesktopConfig::stream`]). + stream: bool, + index: Mutex, + turns: Mutex, + reviews: Mutex, + /// The one chat event subscriber and the running turns' previews. + events: Mutex, + /// The last pending-approval count read, served when the settings store is + /// busy. Advisory only; it authorizes nothing. + pending_cache: AtomicU64, +} + +/// The desktop command service. See the [module documentation](self). +/// +/// It is `Send + Sync`; a shell may keep one in application state and call it +/// from blocking tasks. +pub struct DesktopService { + inner: Arc, +} + +impl DesktopService { + /// Open the service for `config`, creating the session index directory. + /// + /// The index is not refreshed here. The local server token is resolved from + /// `config.token_source` per chat turn; nothing is read or cached now. + /// + /// # Errors + /// + /// `busy` when another process holds the index database, `internal` when + /// the index location is unusable. + pub fn open(config: DesktopConfig) -> Result { + let resolver = TokenResolver::Source(config.token_source.clone()); + Self::open_with(config, resolver) + } + + /// Open the service with an [`InjectedTokenFn`] that replaces + /// `config.token_source`, so a test can avoid the process environment and + /// the credential store. + /// + /// # Errors + /// + /// The same as [`open`](Self::open). + pub fn open_with_token_source( + config: DesktopConfig, + supplier: InjectedTokenFn, + ) -> Result { + Self::open_with(config, TokenResolver::Injected(supplier)) + } + + fn open_with(config: DesktopConfig, token: TokenResolver) -> Result { + let index = SessionIndex::open(&config.data_root) + .map_err(|error| DesktopError::from_index(&error))?; + Ok(Self { + inner: Arc::new(Inner { + data_root: config.data_root, + endpoint: config.endpoint, + token, + stream: config.stream, + index: Mutex::new(IndexState::new(index)), + turns: Mutex::new(TurnTable::default()), + reviews: Mutex::new(ReviewTokens::default()), + events: Mutex::new(EventHub::default()), + pending_cache: AtomicU64::new(0), + }), + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_service_can_be_shared_between_threads() { + fn assert_send_sync() {} + assert_send_sync::(); + } +} diff --git a/crates/omgcode-app/src/desktop/approvals.rs b/crates/omgcode-app/src/desktop/approvals.rs new file mode 100644 index 0000000..be34327 --- /dev/null +++ b/crates/omgcode-app/src/desktop/approvals.rs @@ -0,0 +1,316 @@ +//! Approval commands (ADR-0046). +//! +//! The service decides nothing about authority. Core owns the approval +//! records, the exact-binding review, and the refusals. This module lists +//! records, asks core to review the recorded request an approval is bound to, +//! issues a single-use review token for an exact review, and refuses an +//! approve whose token is missing, expired, or for another approval. + +use std::collections::{HashMap, HashSet}; +use std::sync::atomic::Ordering; +use std::time::Instant; + +use omgcode_core::control::{ApprovalOperation, ApprovalState, ApprovalStatus, now_unix}; +use omgcode_core::session::{SessionInspection, TranscriptEntryKind}; +use omgcode_core::turn::{ApprovalReview, ReviewCandidate, display_safe_line, review_approval}; +use uuid::Uuid; + +use super::error::DesktopError; +use super::review_tokens::RedeemError; +use super::wire::{ + ApprovalDecideRequest, ApprovalDetail, ApprovalField, ApprovalList, ApprovalRequest, + ApprovalView, ApprovalsSummary, Binding, Decision, +}; +use super::{DesktopService, Inner}; + +/// Most non-pending approvals `approvals_list` returns, the most recent ones. +const MAX_LISTED_RESOLVED: usize = 50; + +impl DesktopService { + /// How many approvals are pending. + /// + /// Without a settings store the count is 0. When the store stays busy past + /// the bounded wait, the last count read is returned: this is an advisory + /// badge and grants nothing. + /// + /// # Errors + /// + /// `control` when the settings store is invalid. + pub fn approvals_summary(&self) -> Result { + let inner = &self.inner; + let pending = match inner.open_control() { + Ok(Some(store)) => { + let statuses = store.approval_statuses(now_unix()); + drop(store); + inner.note_pending(&statuses) + } + Ok(None) => { + inner.pending_cache.store(0, Ordering::Relaxed); + 0 + } + Err(error) if error.code() == "busy" => inner.pending_cache.load(Ordering::Relaxed), + Err(error) => return Err(error), + }; + Ok(ApprovalsSummary { pending }) + } + + /// Every pending approval, then the 50 most recent others, each joined with + /// its session's title and folder when the index knows the session. + /// + /// # Errors + /// + /// `busy` or `control` for the settings store, `internal` for service state. + pub fn approvals_list(&self) -> Result { + let inner = &self.inner; + let now = now_unix(); + // Statuses first; the store is released before the index is locked. + let statuses = match inner.open_control()? { + Some(store) => { + let statuses = store.approval_statuses(now); + drop(store); + statuses + } + None => Vec::new(), + }; + inner.note_pending(&statuses); + let (pending, resolved): (Vec, Vec) = statuses + .into_iter() + .partition(|status| status.state == ApprovalState::Pending); + let skip = resolved.len().saturating_sub(MAX_LISTED_RESOLVED); + let chosen: Vec = pending + .into_iter() + .chain(resolved.into_iter().skip(skip)) + .collect(); + let approvals = inner.views_of(&chosen)?; + Ok(ApprovalList { + now_unix: now, + approvals, + }) + } + + /// Review the recorded request that one approval is bound to. + /// + /// A non-pending approval is `stale` and nothing is read. For a pending + /// one, the session's journal is inspected without the writer lock and core + /// finds the recorded request whose digest the approval holds. Only an + /// `exact` result carries the tool, the fields, and a fresh single-use + /// review token, which replaces any earlier token for this approval. + /// + /// # Errors + /// + /// `not_found` for an unknown approval, `busy` when a turn runs here for the + /// session or a store or journal is held, `invalid_journal`, `control`. + pub fn approval_detail( + &self, + request: ApprovalRequest, + ) -> Result { + let inner = &self.inner; + let status = inner.status_of_approval(request.id)?; + let not_reviewed = |binding: Binding| -> Result { + Ok(ApprovalDetail { + approval: inner.view_of(&status)?, + binding, + tool: None, + fields: Vec::new(), + review_token: None, + }) + }; + if status.state != ApprovalState::Pending { + return not_reviewed(Binding::Stale); + } + if inner.is_running(status.session_id)? { + return Err(DesktopError::busy()); + } + let path = match inner.resolve_path(status.session_id) { + Ok(path) => path, + Err(error) if error.code() == "not_found" => return not_reviewed(Binding::NotFound), + Err(error) => return Err(error), + }; + let inspection = SessionInspection::inspect(&path) + .map_err(|error| DesktopError::from_session(&error))?; + let candidates: Vec> = inspection + .entries + .iter() + .filter_map(|entry| match &entry.kind { + TranscriptEntryKind::ToolRequested { + tool, arguments, .. + } => Some(ReviewCandidate { tool, arguments }), + _ => None, + }) + .collect(); + // No service lock and no control store is held; core opens its own. + let review = review_approval(&inner.data_root, &status, &candidates) + .map_err(|error| DesktopError::from_review(&error))?; + match review { + ApprovalReview::Exact(reviewed) => { + let token = + inner + .lock_reviews()? + .issue(status.id, &status.request_sha256, Instant::now()); + Ok(ApprovalDetail { + approval: inner.view_of(&status)?, + binding: Binding::Exact, + tool: Some(reviewed.tool), + fields: reviewed + .fields + .into_iter() + .map(|field| ApprovalField { + label: field.label.to_owned(), + value: field.value, + multiline: field.multiline, + }) + .collect(), + review_token: Some(token), + }) + } + ApprovalReview::Stale(_) => not_reviewed(Binding::Stale), + ApprovalReview::NotFound => not_reviewed(Binding::NotFound), + } + } + + /// Approve or deny one pending approval. + /// + /// Deny needs no token. Approve first redeems the review token against the + /// approval's current digest, read from the store in the same hold as the + /// decision. The token is consumed by every attempt, so a refused approve + /// needs a fresh `approval_detail`. A `file_write` approve also needs + /// `writers_stopped` and calls core's acknowledgement path; other + /// operations ignore the field. Core still refuses the wrong combination. + /// + /// # Errors + /// + /// `not_found`, `not_pending`, `review_expired`, `review_mismatch`, + /// `writers_stopped_required`, `busy`, or `control` with core's refusal. + pub fn approval_decide( + &self, + request: ApprovalDecideRequest, + ) -> Result { + let inner = &self.inner; + let mut store = inner + .open_control()? + .ok_or_else(|| DesktopError::not_found("approval"))?; + let now = now_unix(); + let status = store + .approval_status(request.id, now) + .ok_or_else(|| DesktopError::not_found("approval"))?; + if status.state != ApprovalState::Pending { + return Err(DesktopError::not_pending()); + } + let decided = match request.decision { + Decision::Deny => store.decide_approval(request.id, false, now), + Decision::Approve => { + // Only the redeem takes the reviews lock; the store stays open + // so the record cannot change between the check and the decision. + inner.redeem( + request.id, + request.review_token.as_deref(), + &status.request_sha256, + )?; + if status.operation == ApprovalOperation::FileWrite { + if !request.writers_stopped { + return Err(DesktopError::writers_stopped_required()); + } + store.approve_with_writers_stopped(request.id, now) + } else { + store.decide_approval(request.id, true, now) + } + } + }; + decided.map_err(|error| DesktopError::from_control(&error))?; + let updated = store + .approval_status(request.id, now) + .ok_or_else(|| DesktopError::not_found("approval"))?; + let statuses = store.approval_statuses(now); + drop(store); + inner.note_pending(&statuses); + inner.view_of(&updated) + } +} + +impl Inner { + /// Cache and return how many of `statuses` are pending. + fn note_pending(&self, statuses: &[ApprovalStatus]) -> u64 { + let pending = statuses + .iter() + .filter(|status| status.state == ApprovalState::Pending) + .count(); + let pending = u64::try_from(pending).unwrap_or(u64::MAX); + self.pending_cache.store(pending, Ordering::Relaxed); + pending + } + + /// The status of one approval; the store is released before returning. + fn status_of_approval(&self, id: Uuid) -> Result { + let store = self + .open_control()? + .ok_or_else(|| DesktopError::not_found("approval"))?; + let status = store.approval_status(id, now_unix()); + drop(store); + status.ok_or_else(|| DesktopError::not_found("approval")) + } + + /// Use the review token for an approval. + fn redeem( + &self, + approval: Uuid, + presented: Option<&str>, + digest: &str, + ) -> Result<(), DesktopError> { + let outcome = self + .lock_reviews()? + .redeem(approval, presented, digest, Instant::now()); + outcome.map_err(|error| match error { + RedeemError::Expired => DesktopError::review_expired(), + RedeemError::Mismatch => DesktopError::review_mismatch(), + }) + } + + fn view_of(&self, status: &ApprovalStatus) -> Result { + let mut views = self.views_of(std::slice::from_ref(status))?; + views + .pop() + .ok_or_else(|| DesktopError::internal("The approval could not be shown.")) + } + + /// Join statuses with session labels from the index, which is locked once. + /// + /// A label the index cannot supply is `null`: it is cosmetic, and the + /// record itself comes from the settings store. + fn views_of(&self, statuses: &[ApprovalStatus]) -> Result, DesktopError> { + let labels: HashMap = self.with_index(|state| { + let mut labels = HashMap::new(); + for status in statuses { + if labels.contains_key(&status.session_id) { + continue; + } + if let Ok(Some(session)) = state.index.session(status.session_id) { + let title = super::state::summary_of(&session, &HashSet::new()).title; + labels.insert( + status.session_id, + (title, super::state::folder_of(&session)), + ); + } + } + Ok(labels) + })?; + Ok(statuses + .iter() + .map(|status| { + let label = labels.get(&status.session_id); + ApprovalView { + id: status.id, + session_id: status.session_id, + session_title: label.map(|(title, _)| title.clone()), + folder_display: label.map(|(_, folder)| folder.clone()), + operation: status.operation.into(), + summary: display_safe_line(&status.summary), + state: status.state.into(), + created_unix: status.created_unix, + expires_unix: status.expires_unix, + request_sha256: status.request_sha256.clone(), + writers_stopped: status.writers_stopped, + } + }) + .collect()) + } +} diff --git a/crates/omgcode-app/src/desktop/chat.rs b/crates/omgcode-app/src/desktop/chat.rs new file mode 100644 index 0000000..1ae1164 --- /dev/null +++ b/crates/omgcode-app/src/desktop/chat.rs @@ -0,0 +1,279 @@ +//! Chat commands: defaults, send, status, cancel, and shutdown. + +use std::time::Duration; + +use omgcode_core::context::DEFAULT_COMPACT_THRESHOLD; +use omgcode_core::session_index::IndexedState; +use uuid::Uuid; + +use super::ChatEventSink; +use super::error::DesktopError; +use super::turns::{MAX_RUNNING_TURNS, ShutdownReport, SlotState, TurnJob, spawn_turn}; +use super::wire::{ChatSendRequest, ChatSettingsDto, ChatState, ChatStatus, SessionRequest}; +use super::{DesktopService, Inner}; +use crate::{ChatClients, ChatSettings, LmToken, LocalConnection}; + +/// Largest message accepted before core applies its own, smaller bound. It only +/// keeps an oversized request from being copied into a turn thread. +const MAX_MESSAGE_INPUT_BYTES: usize = 64 * 1024; +/// Largest custom-instruction text accepted before core applies its bound. +const MAX_INSTRUCTIONS_INPUT_BYTES: usize = 64 * 1024; +/// Output reserve of the default settings. +const DEFAULT_MAX_OUTPUT_TOKENS: u32 = 1024; + +impl DesktopService { + /// The settings the interface starts from: the fixed endpoint, an empty + /// model, and the CLI's budgets. + pub fn chat_defaults(&self) -> ChatSettingsDto { + ChatSettingsDto { + endpoint: self.inner.endpoint.clone(), + model: String::new(), + context_tokens: 0, + max_output_tokens: DEFAULT_MAX_OUTPUT_TOKENS, + instructions: String::new(), + compact_threshold: DEFAULT_COMPACT_THRESHOLD, + auto_compact: false, + } + } + + /// Start one chat turn and return at once with state `running`. + /// + /// Everything that can be checked without running the turn is checked + /// first, so a bad endpoint, model, or session is refused synchronously. + /// The turn then runs on its own thread; poll [`chat_status`](Self::chat_status). + /// + /// # Errors + /// + /// `invalid_request` (endpoint differs, empty or oversized message), + /// `not_found`, `invalid_journal`, `busy`, `archived`, `turn_open`, + /// `turn_running`, `too_many_turns`, `shutting_down`, `internal` (thread + /// start failure), and the application layer's `provider`, `configuration`, + /// and `credential` codes for unusable settings or token. + pub fn chat_send(&self, request: ChatSendRequest) -> Result { + let inner = &self.inner; + let session_id = request.session_id; + if request.settings.endpoint != inner.endpoint { + return Err(DesktopError::invalid_request( + "The endpoint is fixed by the application.", + )); + } + if request.message.trim().is_empty() { + return Err(DesktopError::invalid_request("The message is empty.")); + } + if request.message.len() > MAX_MESSAGE_INPUT_BYTES + || request.settings.instructions.len() > MAX_INSTRUCTIONS_INPUT_BYTES + { + return Err(DesktopError::invalid_request("The request is too large.")); + } + inner.reap()?; + check_slot(inner, session_id)?; + + // A fresh read of this one journal, so archive and open-turn state is + // current. The path comes only from the index. + let (session, path) = inner.with_index(|state| { + let session = state + .index + .refresh_session(session_id) + .map_err(|error| DesktopError::from_index(&error))? + .ok_or_else(|| DesktopError::not_found("session"))?; + let path = state + .index + .journal_path(session_id) + .map_err(|error| DesktopError::from_index(&error))? + .ok_or_else(|| DesktopError::not_found("session"))?; + Ok((session, path)) + })?; + match session.state { + IndexedState::Invalid => return Err(DesktopError::invalid_journal()), + IndexedState::Busy => return Err(DesktopError::busy()), + IndexedState::Ok => {} + } + if session.archived { + return Err(DesktopError::archived()); + } + if session.active_turn.is_some() { + return Err(DesktopError::turn_open()); + } + + // Resolve the connection and build the clients now, so an unreadable + // credential, a profile whose endpoint changed since launch, a bad + // model, context, or endpoint is a synchronous error and no thread + // starts. The connection is resolved again for every turn, so a + // rotated credential applies to the next one. The resolved endpoint is + // the only endpoint the turn uses; it equals the launch endpoint, + // which was compared with the request above. + let LocalConnection { endpoint, token } = inner + .token + .resolve(&inner.data_root, &inner.endpoint) + .map_err(|error| DesktopError::from_app(&error))?; + let settings = chat_settings(request.settings, endpoint); + let clients = ChatClients::new(&settings, token.as_ref().map(LmToken::expose)) + .map_err(|error| DesktopError::from_app(&error))?; + let job = TurnJob { + session_id, + path, + message: request.message, + settings, + token, + clients, + }; + + // Reserve the slot and start the thread under one lock hold, so two + // racing calls cannot both pass the limits. Starting a thread does not + // block; the thread waits for this lock only to store its cancel token. + let mut turns = inner.lock_turns()?; + if turns.is_closed() { + return Err(DesktopError::shutting_down()); + } + if turns.is_running(session_id) { + return Err(DesktopError::turn_running()); + } + if turns.running_count() >= MAX_RUNNING_TURNS { + return Err(DesktopError::too_many_turns()); + } + let handle = spawn_turn(inner, job)?; + turns.insert_running(session_id, handle); + drop(turns); + Ok(status_of( + session_id, + Some(&SlotState::Running { + cancel_requested: false, + cancel: None, + }), + )) + } + + /// The state of a session's chat turn. + /// + /// Reaps finished turn threads first. A session this service has no record + /// of is `idle`. A finished result stays until the next `chat_send` for the + /// session. + /// + /// # Errors + /// + /// `internal` when the service state is unavailable. + pub fn chat_status(&self, request: SessionRequest) -> Result { + self.inner.reap()?; + self.read_status(request.session_id) + } + + /// Request cooperative cancellation of a running turn. + /// + /// A turn that streams, text-only or tool-enabled, hands its cancel token + /// to the slot. The request is then seen at once, the streamed model + /// request stops within about 25 ms, and the turn ends as `failed` with the + /// error code `cancelled` (ADR-0050). A tool-enabled turn also checks the + /// token between model rounds and tool calls. A turn whose request is not + /// streamed (`--stream off`) has no cancel token: the request is ignored, + /// the status stays `running`, and the request in flight is not + /// interrupted. `cancelling` is reported only when a cancel token exists. + /// + /// # Errors + /// + /// `internal` when the service state is unavailable. + pub fn chat_cancel(&self, request: SessionRequest) -> Result { + self.inner.lock_turns()?.request_cancel(request.session_id); + self.inner.reap()?; + self.read_status(request.session_id) + } + + /// Register the one chat event subscriber, replacing any earlier one, and + /// replay the preview of every running turn (ADR-0049). + /// + /// The earlier subscriber receives nothing more. The replay holds, for each + /// running turn that has started a model request, `request_started`, the + /// text so far as `delta` events, and `preview_truncated` when the bound was + /// reached, in ascending session order. Later events follow in order. + /// + /// Events are sent while the service's event lock is held, so the sink must + /// return quickly, must not block, and must not call back into the service. + /// A sink that returns `false` is dropped; no event is sent until the next + /// subscription. No event or preview text is logged. + /// + /// # Errors + /// + /// `internal` when the event state is unavailable (a poisoned lock). Turn + /// threads meet the same condition by dropping events; their turns are not + /// affected. + pub fn chat_subscribe(&self, sink: ChatEventSink) -> Result<(), DesktopError> { + self.inner.lock_events()?.subscribe(sink); + Ok(()) + } + + /// Request cancellation of every running turn and wait up to `grace` for + /// the turn threads to finish, polling for completion. + /// + /// Returns without waiting past `grace`. A streamed request stops within + /// about 25 ms, so a streamed turn finishes inside the grace period and + /// leaves no open turn. A request that is not streamed cannot be + /// interrupted: such a turn is reported in `still_running` and leaves its + /// journal with an open turn, which the operator can close later with the + /// interrupt command. After shutdown, `chat_send` is refused. + pub fn shutdown(&self, grace: Duration) -> ShutdownReport { + self.inner.shutdown_turns(grace) + } + + fn read_status(&self, session_id: Uuid) -> Result { + let turns = self.inner.lock_turns()?; + Ok(status_of( + session_id, + turns.slot(session_id).map(|slot| &slot.state), + )) + } +} + +/// Refuse early when a turn for the session runs or the running limit is hit. +fn check_slot(inner: &Inner, session_id: Uuid) -> Result<(), DesktopError> { + let turns = inner.lock_turns()?; + if turns.is_closed() { + return Err(DesktopError::shutting_down()); + } + if turns.is_running(session_id) { + return Err(DesktopError::turn_running()); + } + if turns.running_count() >= MAX_RUNNING_TURNS { + return Err(DesktopError::too_many_turns()); + } + Ok(()) +} + +fn chat_settings(settings: ChatSettingsDto, endpoint: String) -> ChatSettings { + ChatSettings { + endpoint, + model: settings.model, + context_tokens: settings.context_tokens, + max_output_tokens: settings.max_output_tokens, + instructions: settings.instructions, + compact_threshold: settings.compact_threshold, + auto_compact: settings.auto_compact, + } +} + +/// The wire status of a slot, or `idle` when there is none. +fn status_of(session_id: Uuid, state: Option<&SlotState>) -> ChatStatus { + let (state, outcome, error) = match state { + None => (ChatState::Idle, None, None), + Some(SlotState::Running { + cancel_requested, + cancel, + }) => ( + if *cancel_requested && cancel.is_some() { + ChatState::Cancelling + } else { + ChatState::Running + }, + None, + None, + ), + Some(SlotState::Finished(result)) => match result.as_ref() { + Ok(outcome) => (ChatState::Completed, Some(outcome.clone()), None), + Err(error) => (ChatState::Failed, None, Some(error.clone())), + }, + }; + ChatStatus { + session_id, + state, + outcome, + error, + } +} diff --git a/crates/omgcode-app/src/desktop/error.rs b/crates/omgcode-app/src/desktop/error.rs new file mode 100644 index 0000000..c7bfb38 --- /dev/null +++ b/crates/omgcode-app/src/desktop/error.rs @@ -0,0 +1,237 @@ +//! The one failure shape the desktop commands return. +//! +//! A [`DesktopError`] serializes as exactly `{ "code": string, "message": +//! string }`. The code is a stable `snake_case` identifier that the interface +//! may branch on. The message is short English text. It never carries an +//! absolute path, recorded file content, or the local server token: every +//! message is fixed text, a static reason from core, or the message of an +//! [`AppError`], which the application layer already keeps free of secrets. + +use std::fmt; + +use omgcode_core::control::ControlError; +use omgcode_core::session::SessionError; +use omgcode_core::session_index::IndexError; +use omgcode_core::turn::ReviewError; +use serde::Serialize; + +use crate::error::{AppError, ErrorKind}; + +/// A controlled failure of one desktop command. +/// +/// Codes defined by this service: `busy`, `invalid_journal`, `review_expired`, +/// `review_mismatch`, `not_found`, `invalid_request`, `archived`, +/// `turn_running`, `turn_open`, `too_many_turns`, `not_pending`, +/// `writers_stopped_required`, `unknown_workspace`, `shutting_down`, and +/// `internal`. Failures that come from the application layer keep their +/// [`ErrorKind`] code (`provider`, `session`, `control`, `context`, +/// `permission`, `turn`, and the others). +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct DesktopError { + code: &'static str, + message: String, +} + +impl DesktopError { + fn new(code: &'static str, message: impl Into) -> Self { + Self { + code, + message: message.into(), + } + } + + /// The stable machine-readable code. + pub const fn code(&self) -> &'static str { + self.code + } + + /// The short human-readable message. + pub fn message(&self) -> &str { + &self.message + } + + /// A journal, the settings store, or the index is held elsewhere. Retry later. + pub(crate) fn busy() -> Self { + Self::new( + "busy", + "The data is in use by another operation. Try again shortly.", + ) + } + + /// The session's journal failed validation. + pub(crate) fn invalid_journal() -> Self { + Self::new("invalid_journal", "The session journal failed validation.") + } + + /// The review token is missing, used, or older than its lifetime. + pub(crate) fn review_expired() -> Self { + Self::new( + "review_expired", + "The review has expired. Open the approval again.", + ) + } + + /// The review token does not belong to this approval. + pub(crate) fn review_mismatch() -> Self { + Self::new( + "review_mismatch", + "The review does not match this approval. Open the approval again.", + ) + } + + /// No such session or approval. + pub(crate) fn not_found(what: &'static str) -> Self { + Self::new("not_found", format!("The {what} was not found.")) + } + + /// The request is not acceptable as sent. + pub(crate) fn invalid_request(message: impl Into) -> Self { + Self::new("invalid_request", message) + } + + /// The session is archived. + pub(crate) fn archived() -> Self { + Self::new("archived", "Restore the archived session before chatting.") + } + + /// A turn for the session is running in this application. + pub(crate) fn turn_running() -> Self { + Self::new( + "turn_running", + "A chat turn is already running for this session.", + ) + } + + /// The journal records a turn that never finished. + pub(crate) fn turn_open() -> Self { + Self::new( + "turn_open", + "The session has an unfinished turn. Interrupt it first.", + ) + } + + /// The running-turn limit is reached. + pub(crate) fn too_many_turns() -> Self { + Self::new( + "too_many_turns", + "Too many chat turns are running. Wait for one to finish.", + ) + } + + /// The approval already has a decision, or it expired. + pub(crate) fn not_pending() -> Self { + Self::new("not_pending", "The approval is no longer pending.") + } + + /// A file write needs the writers-stopped acknowledgement. + pub(crate) fn writers_stopped_required() -> Self { + Self::new( + "writers_stopped_required", + "Confirm that other writers to the file are stopped to approve a file write.", + ) + } + + /// The workspace is not registered. + pub(crate) fn unknown_workspace() -> Self { + Self::new("unknown_workspace", "The workspace is not registered.") + } + + /// The application is shutting down. + pub(crate) fn shutting_down() -> Self { + Self::new("shutting_down", "The application is shutting down.") + } + + /// Service state or a lower layer is unavailable. + pub(crate) fn internal(message: &'static str) -> Self { + Self::new("internal", message) + } + + /// A poisoned service lock. State whose invariants are uncertain is not used. + pub(crate) fn poisoned() -> Self { + Self::internal("The desktop service state is unavailable.") + } + + /// Keep the application layer's code and message. + pub(crate) fn from_app(error: &AppError) -> Self { + Self::new(error.code(), error.message()) + } + + /// Remove a known secret from the message. + pub(crate) fn redacted(mut self, secret: Option<&str>) -> Self { + self.message = crate::redact_known_secret(&self.message, secret); + self + } + + /// Map a title-validation failure from session creation to `invalid_request`. + pub(crate) fn from_create(error: &AppError) -> Self { + if error.kind() == ErrorKind::Session + && error.message().starts_with("invalid session value") + { + Self::invalid_request(error.message()) + } else { + Self::from_app(error) + } + } + + /// Map a journal failure. + pub(crate) fn from_session(error: &SessionError) -> Self { + match error { + SessionError::Busy => Self::busy(), + SessionError::InvalidJournal(_) | SessionError::LimitReached => Self::invalid_journal(), + SessionError::ActiveTurn => Self::turn_open(), + SessionError::Io(io) if io.kind() == std::io::ErrorKind::NotFound => { + Self::not_found("session") + } + SessionError::Io(_) => Self::new("session", "The session journal could not be read."), + SessionError::InvalidValue(reason) => Self::invalid_request(*reason), + SessionError::WrongTurn | SessionError::Poisoned => { + Self::new("session", error.to_string()) + } + } + } + + /// Map an index failure. Index text and stored values never reach the caller. + pub(crate) fn from_index(error: &IndexError) -> Self { + match error { + IndexError::Busy => Self::busy(), + IndexError::InvalidQuery(reason) => Self::invalid_request(*reason), + IndexError::InvalidIndexEntry(_) => Self::invalid_journal(), + IndexError::UnsafeLocation(_) + | IndexError::Session(_) + | IndexError::Io(_) + | IndexError::Sqlite(_) => Self::internal("The session index is unavailable."), + } + } + + /// Map a settings-store failure. + pub(crate) fn from_control(error: &ControlError) -> Self { + match error { + ControlError::Busy => Self::busy(), + ControlError::UnknownWorkspace => Self::unknown_workspace(), + ControlError::UnknownApproval => Self::not_found("approval"), + ControlError::Io(_) + | ControlError::Container(_) + | ControlError::ProfileRemoval(_) + | ControlError::WorkspacePath(_) => { + Self::new("control", "The settings store could not be used.") + } + other => Self::new("control", other.to_string()), + } + } + + /// Map an approval-review failure. + pub(crate) fn from_review(error: &ReviewError) -> Self { + match error { + ReviewError::Busy => Self::busy(), + other => Self::new("control", other.to_string()), + } + } +} + +impl fmt::Display for DesktopError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "{}: {}", self.code, self.message) + } +} + +impl std::error::Error for DesktopError {} diff --git a/crates/omgcode-app/src/desktop/events.rs b/crates/omgcode-app/src/desktop/events.rs new file mode 100644 index 0000000..a7e5ab5 --- /dev/null +++ b/crates/omgcode-app/src/desktop/events.rs @@ -0,0 +1,575 @@ +//! The chat event hub: one subscriber and the live preview of each running turn +//! (ADR-0049). +//! +//! A turn thread feeds the hub with the provisional text of a streamed model +//! request. The hub keeps, for each running turn, the preview of the current +//! request (at most [`MAX_PREVIEW_BYTES`]) and pushes events to the one +//! subscriber. The subscriber is a [`ChatEventSink`], so this crate stays free +//! of any user-interface framework. +//! +//! # Authority +//! +//! Preview text is provisional and untrusted. It grants nothing and is never +//! written to the journal. The journal and `chat_status` stay the record. Neither +//! preview text nor an event is ever logged. +//! +//! # Locking +//! +//! The hub lives behind `Inner::events`. Events are sent **while that lock is +//! held**, so a replay and a live event cannot reach the subscriber out of +//! order. That is why a sink must not block and must not call back into the +//! service: it would stall every turn thread, or deadlock on this lock. +//! +//! Lock order: the `turns` lock and the `events` lock are never held together. +//! Where both are needed, `turns` is taken and released first. The `events` lock +//! is never held while the `index` or `reviews` lock is taken. +//! +//! A poisoned `events` lock never panics. A turn thread drops the event and +//! carries on (streaming stops; the turn itself is unaffected). `chat_subscribe` +//! returns `internal`. + +use std::collections::HashMap; +use std::sync::MutexGuard; + +use uuid::Uuid; + +use super::Inner; +use super::error::DesktopError; +use super::wire::ChatEvent; +use crate::ChatStreamEvent; + +/// Most preview text kept, and sent, for one model request. +pub(super) const MAX_PREVIEW_BYTES: usize = 16 * 1024; +/// Most text bytes in one `delta` event. +pub(super) const MAX_DELTA_BYTES: usize = 1024; + +/// Delivers one event to the subscriber. Returns `false` when the subscriber +/// is gone; the hub then drops it. It must not block and must not call back +/// into the service. +pub type ChatEventSink = Box bool + Send + Sync>; + +/// The live preview of one running turn's current request. +struct Preview { + /// The request number, from 1. `0` means no request has started yet. + request: u32, + /// The text accepted for the current request, at most [`MAX_PREVIEW_BYTES`]. + text: String, + /// Some text of the current request did not fit. + truncated: bool, +} + +/// The subscriber and the running turns' previews. +#[derive(Default)] +pub(super) struct EventHub { + /// At most one subscriber; a new one replaces it. + sink: Option, + /// One entry per running turn, removed by [`finish`](Self::finish). + live: HashMap, +} + +/// The longest prefix of `text` that is at most `max` bytes and ends on a +/// character boundary. +fn prefix_len(text: &str, max: usize) -> usize { + let mut end = max.min(text.len()); + while end > 0 && !text.is_char_boundary(end) { + end -= 1; + } + end +} + +/// Send one event. A sink that returns `false` is dropped at once. +/// +/// Returns whether a subscriber is still attached afterwards. +fn send(sink: &mut Option, event: &ChatEvent) -> bool { + let kept = sink.as_ref().is_some_and(|deliver| deliver(event)); + if !kept { + *sink = None; + } + kept +} + +/// Send `text` as `delta` events of at most [`MAX_DELTA_BYTES`] bytes, split +/// only at character boundaries. Stops when the subscriber is gone. +fn send_deltas(sink: &mut Option, session_id: Uuid, request: u32, text: &str) { + let mut rest = text; + while !rest.is_empty() { + let end = prefix_len(rest, MAX_DELTA_BYTES); + // `end` is zero only for a bound smaller than one character, which the + // constant is not; stop rather than loop if that ever changed. + let (Some(piece), Some(tail)) = (rest.get(..end), rest.get(end..)) else { + return; + }; + if piece.is_empty() { + return; + } + let event = ChatEvent::Delta { + session_id, + request, + text: piece.to_owned(), + }; + if !send(sink, &event) { + return; + } + rest = tail; + } +} + +impl EventHub { + /// Register a running turn with no request yet. + pub(super) fn begin(&mut self, session_id: Uuid) { + self.live.insert( + session_id, + Preview { + request: 0, + text: String::new(), + truncated: false, + }, + ); + } + + /// A model request of the turn started: supersede the preview. + /// + /// Does nothing for a session that is not live. + pub(super) fn request_started(&mut self, session_id: Uuid) { + let Some(preview) = self.live.get_mut(&session_id) else { + return; + }; + preview.request = preview.request.saturating_add(1); + preview.text.clear(); + preview.truncated = false; + let event = ChatEvent::RequestStarted { + session_id, + request: preview.request, + }; + send(&mut self.sink, &event); + } + + /// Provisional text of the current request. + /// + /// Text that arrives before any request started behaves as if + /// `request_started` had come first. Only what fits under + /// [`MAX_PREVIEW_BYTES`] is kept and sent; the first time something does not + /// fit, one `preview_truncated` event is sent and the rest of the request's + /// text is dropped without notice. + pub(super) fn text(&mut self, session_id: Uuid, text: &str) { + if self + .live + .get(&session_id) + .is_some_and(|preview| preview.request == 0) + { + self.request_started(session_id); + } + let Some(preview) = self.live.get_mut(&session_id) else { + return; + }; + if preview.truncated || text.is_empty() { + return; + } + let room = MAX_PREVIEW_BYTES.saturating_sub(preview.text.len()); + let fit = prefix_len(text, room); + let request = preview.request; + if let Some(part) = text.get(..fit) + && !part.is_empty() + { + preview.text.push_str(part); + send_deltas(&mut self.sink, session_id, request, part); + } + if fit < text.len() { + preview.truncated = true; + send( + &mut self.sink, + &ChatEvent::PreviewTruncated { + session_id, + request, + }, + ); + } + } + + /// The turn left `running`: forget its preview and send `finished`. + /// + /// Sent even when no request ever started. + pub(super) fn finish(&mut self, session_id: Uuid) { + self.live.remove(&session_id); + send(&mut self.sink, &ChatEvent::Finished { session_id }); + } + + /// Replace the subscriber, then replay the preview of every running turn + /// that has started a request, in ascending session order. + /// + /// The replay for a session is `request_started`, its text as `delta` + /// events, and `preview_truncated` when flagged. A sink that refuses an + /// event is dropped and the replay stops. + pub(super) fn subscribe(&mut self, sink: ChatEventSink) { + self.sink = Some(sink); + let mut ids: Vec = self.live.keys().copied().collect(); + ids.sort_unstable(); + for session_id in ids { + let Some(preview) = self.live.get(&session_id) else { + continue; + }; + if preview.request == 0 { + continue; + } + let request = preview.request; + let started = ChatEvent::RequestStarted { + session_id, + request, + }; + if !send(&mut self.sink, &started) { + return; + } + send_deltas(&mut self.sink, session_id, request, &preview.text); + if preview.truncated + && !send( + &mut self.sink, + &ChatEvent::PreviewTruncated { + session_id, + request, + }, + ) + { + return; + } + if self.sink.is_none() { + return; + } + } + } +} + +impl Inner { + /// Register a running turn with the hub. A poisoned lock drops the call. + pub(super) fn events_begin(&self, session_id: Uuid) { + if let Ok(mut hub) = self.events.lock() { + hub.begin(session_id); + } + } + + /// Forward one stream event of a turn to the hub. A poisoned lock drops it. + pub(super) fn events_stream(&self, session_id: Uuid, event: ChatStreamEvent<'_>) { + if let Ok(mut hub) = self.events.lock() { + match event { + ChatStreamEvent::RequestStarted => hub.request_started(session_id), + ChatStreamEvent::Text(text) => hub.text(session_id, text), + } + } + } + + /// Tell the hub the turn left `running`. A poisoned lock drops the call. + /// + /// The caller records the result in the turn table first and has released + /// the `turns` lock. + pub(super) fn events_finish(&self, session_id: Uuid) { + if let Ok(mut hub) = self.events.lock() { + hub.finish(session_id); + } + } + + /// Lock the hub for a subscription; `internal` when it is poisoned. + pub(super) fn lock_events(&self) -> Result, DesktopError> { + self.events.lock().map_err(|_| DesktopError::poisoned()) + } +} + +#[cfg(test)] +mod tests { + use std::sync::{Arc, Mutex}; + + use super::*; + + type Log = Arc>>; + + fn collector() -> (Log, ChatEventSink) { + let log: Log = Arc::default(); + let writer = Arc::clone(&log); + let sink: ChatEventSink = Box::new(move |event| { + writer.lock().expect("log").push(event.clone()); + true + }); + (log, sink) + } + + fn events(log: &Log) -> Vec { + log.lock().expect("log").clone() + } + + fn id(value: u128) -> Uuid { + Uuid::from_u128(value) + } + + fn delta_text(events: &[ChatEvent]) -> String { + events + .iter() + .filter_map(|event| match event { + ChatEvent::Delta { text, .. } => Some(text.as_str()), + _ => None, + }) + .collect() + } + + #[test] + fn text_before_any_request_starts_one_first() { + // Given a live turn and a subscriber. + let mut hub = EventHub::default(); + let (log, sink) = collector(); + hub.begin(id(1)); + hub.subscribe(sink); + + // When text arrives with no RequestStarted. + hub.text(id(1), "hi"); + + // Then request 1 starts first and the text follows. + assert_eq!( + events(&log), + [ + ChatEvent::RequestStarted { + session_id: id(1), + request: 1 + }, + ChatEvent::Delta { + session_id: id(1), + request: 1, + text: "hi".to_owned() + }, + ] + ); + } + + #[test] + fn deltas_split_only_at_character_boundaries() { + // Given text of three-byte characters, 2,100 bytes in all. + let mut hub = EventHub::default(); + let (log, sink) = collector(); + hub.begin(id(1)); + hub.subscribe(sink); + let text = "\u{20ac}".repeat(700); + + // When it is sent. + hub.text(id(1), &text); + + // Then each delta is at most 1,024 bytes and ends on a boundary, the + // first is 1,023 bytes, and the pieces rebuild the text. + let seen = events(&log); + let lengths: Vec = seen + .iter() + .filter_map(|event| match event { + ChatEvent::Delta { text, .. } => Some(text.len()), + _ => None, + }) + .collect(); + assert_eq!(lengths, [1023, 1023, 54]); + assert_eq!(delta_text(&seen), text); + } + + #[test] + fn the_preview_stops_at_the_bound_on_a_character_boundary() { + // Given a reply of three-byte characters longer than the bound. + let mut hub = EventHub::default(); + let (log, sink) = collector(); + hub.begin(id(1)); + hub.subscribe(sink); + let text = "\u{20ac}".repeat(6000); + + // When it is sent, and more text follows for the same request. + hub.text(id(1), &text); + hub.text(id(1), "later"); + + // Then 16,383 bytes were sent (the largest whole-character fit), one + // truncation event followed, and nothing came after it. + let seen = events(&log); + assert_eq!(delta_text(&seen).len(), 16_383); + let truncations = seen + .iter() + .filter(|event| matches!(event, ChatEvent::PreviewTruncated { .. })) + .count(); + assert_eq!(truncations, 1); + assert!(matches!( + seen.last(), + Some(ChatEvent::PreviewTruncated { .. }) + )); + + // And a new request starts a fresh preview. + hub.request_started(id(1)); + hub.text(id(1), "again"); + let seen = events(&log); + assert_eq!( + seen.last(), + Some(&ChatEvent::Delta { + session_id: id(1), + request: 2, + text: "again".to_owned() + }) + ); + } + + #[test] + fn text_that_exactly_fills_the_bound_is_not_truncated() { + // Given text of exactly the preview bound. + let mut hub = EventHub::default(); + let (log, sink) = collector(); + hub.begin(id(1)); + hub.subscribe(sink); + hub.text(id(1), &"a".repeat(MAX_PREVIEW_BYTES)); + assert!( + !events(&log) + .iter() + .any(|event| matches!(event, ChatEvent::PreviewTruncated { .. })) + ); + + // When one more byte arrives. + hub.text(id(1), "b"); + + // Then the truncation is reported once and the byte is not sent. + let seen = events(&log); + assert_eq!(delta_text(&seen).len(), MAX_PREVIEW_BYTES); + assert!(matches!( + seen.last(), + Some(ChatEvent::PreviewTruncated { request: 1, .. }) + )); + } + + #[test] + fn a_subscription_replays_running_turns_in_ascending_session_order() { + // Given three turns, started in a different order than their ids. + let mut hub = EventHub::default(); + hub.begin(id(9)); + hub.begin(id(2)); + hub.begin(id(5)); + hub.text(id(9), "nine"); + hub.text(id(2), &"x".repeat(MAX_PREVIEW_BYTES + 1)); + // The turn with no request yet has nothing to replay. + + // When a subscriber arrives. + let (log, sink) = collector(); + hub.subscribe(sink); + + // Then session 2 comes first with its text and truncation, then 9. + let seen = events(&log); + assert_eq!( + seen.first(), + Some(&ChatEvent::RequestStarted { + session_id: id(2), + request: 1 + }) + ); + let truncated_at = seen + .iter() + .position(|event| matches!(event, ChatEvent::PreviewTruncated { .. })) + .expect("truncation replayed"); + assert_eq!( + seen.get(truncated_at.saturating_add(1)), + Some(&ChatEvent::RequestStarted { + session_id: id(9), + request: 1 + }) + ); + assert_eq!( + seen.last(), + Some(&ChatEvent::Delta { + session_id: id(9), + request: 1, + text: "nine".to_owned() + }) + ); + assert!( + !seen.iter().any(|event| matches!( + event, + ChatEvent::RequestStarted { session_id, .. } if *session_id == id(5) + )), + "a turn with no request is not replayed" + ); + } + + #[test] + fn a_replay_holds_only_the_current_request() { + // Given a turn on its second request. + let mut hub = EventHub::default(); + hub.begin(id(1)); + hub.text(id(1), "first request"); + hub.request_started(id(1)); + hub.text(id(1), "second"); + + // When a subscriber arrives. + let (log, sink) = collector(); + hub.subscribe(sink); + + // Then only request 2 is replayed. + assert_eq!( + events(&log), + [ + ChatEvent::RequestStarted { + session_id: id(1), + request: 2 + }, + ChatEvent::Delta { + session_id: id(1), + request: 2, + text: "second".to_owned() + }, + ] + ); + } + + #[test] + fn finish_without_any_request_is_sent_and_clears_the_turn() { + // Given a turn that never streamed. + let mut hub = EventHub::default(); + let (log, sink) = collector(); + hub.begin(id(1)); + hub.subscribe(sink); + + // When it finishes. + hub.finish(id(1)); + + // Then only `finished` was sent, and a later subscriber replays nothing. + assert_eq!(events(&log), [ChatEvent::Finished { session_id: id(1) }]); + let (later, sink) = collector(); + hub.subscribe(sink); + assert!(events(&later).is_empty()); + } + + #[test] + fn a_sink_that_returns_false_is_dropped_and_later_events_go_nowhere() { + // Given a sink that accepts one event and then reports it is gone. + let mut hub = EventHub::default(); + let calls = Arc::new(Mutex::new(0_u32)); + let counter = Arc::clone(&calls); + hub.begin(id(1)); + hub.subscribe(Box::new(move |_| { + let mut count = counter.lock().expect("count"); + *count += 1; + *count < 2 + })); + + // When more events follow. + hub.text(id(1), "one"); + hub.text(id(1), "two"); + hub.finish(id(1)); + + // Then the sink saw exactly the two calls up to its refusal. + assert_eq!(*calls.lock().expect("count"), 2); + assert!(hub.sink.is_none()); + } + + #[test] + fn a_second_subscriber_replaces_the_first() { + // Given a subscriber. + let mut hub = EventHub::default(); + let (first, sink) = collector(); + hub.begin(id(1)); + hub.subscribe(sink); + hub.text(id(1), "a"); + let before = events(&first).len(); + + // When another subscribes and more text arrives. + let (second, sink) = collector(); + hub.subscribe(sink); + hub.text(id(1), "b"); + + // Then the first sees nothing more, and the second sees the replay and + // the new text. + assert_eq!(events(&first).len(), before); + assert_eq!(delta_text(&events(&second)), "ab"); + } +} diff --git a/crates/omgcode-app/src/desktop/review_tokens.rs b/crates/omgcode-app/src/desktop/review_tokens.rs new file mode 100644 index 0000000..5d3acfb --- /dev/null +++ b/crates/omgcode-app/src/desktop/review_tokens.rs @@ -0,0 +1,276 @@ +//! Single-use review tokens (ADR-0046, decision 4). +//! +//! A token proves that `approval_detail` showed the operator the exact request +//! an approval is bound to. It is random, bound to one approval and its digest, +//! lives for [`TOKEN_LIFETIME`], and is removed by the first attempt to redeem +//! it, successful or not. Tokens live in memory only. This module is pure: the +//! caller supplies every `Instant`, so expiry is testable without sleeping. + +use std::time::{Duration, Instant}; + +use uuid::Uuid; + +/// How long a token stays valid. A token is expired at exactly this age. +pub(super) const TOKEN_LIFETIME: Duration = Duration::from_secs(120); + +/// Most tokens kept at once. A new token evicts the oldest beyond this. +pub(super) const MAX_OUTSTANDING_TOKENS: usize = 64; + +/// Why a redeem attempt failed. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(super) enum RedeemError { + /// No token exists for the approval, none was presented, or it is too old. + Expired, + /// The presented token or the approval's digest differs from the issued one. + Mismatch, +} + +/// One outstanding token. +struct Entry { + approval: Uuid, + token: String, + digest: String, + issued: Instant, +} + +/// The outstanding tokens, oldest first. A token is a credential for one +/// approve, so this type has no `Debug` output. +#[derive(Default)] +pub(super) struct ReviewTokens { + entries: Vec, +} + +impl ReviewTokens { + /// Issue a fresh token for `approval`, replacing any earlier one. + /// + /// Expired tokens are dropped first, then the oldest is evicted while the + /// table would exceed [`MAX_OUTSTANDING_TOKENS`]. + pub(super) fn issue(&mut self, approval: Uuid, digest: &str, now: Instant) -> String { + self.entries.retain(|entry| entry.approval != approval); + self.entries.retain(|entry| !is_expired(entry, now)); + while self.entries.len() >= MAX_OUTSTANDING_TOKENS { + self.entries.remove(0); + } + let token = Uuid::new_v4().to_string(); + self.entries.push(Entry { + approval, + token: token.clone(), + digest: digest.to_owned(), + issued: now, + }); + token + } + + /// Use the token for `approval`. The entry is removed on every attempt. + /// + /// `presented` is `None` when the caller sent no token, which is `Expired`. + pub(super) fn redeem( + &mut self, + approval: Uuid, + presented: Option<&str>, + digest: &str, + now: Instant, + ) -> Result<(), RedeemError> { + let position = self + .entries + .iter() + .position(|entry| entry.approval == approval); + let entry = match position { + Some(index) => self.entries.remove(index), + None => return Err(RedeemError::Expired), + }; + if is_expired(&entry, now) { + return Err(RedeemError::Expired); + } + let Some(presented) = presented else { + return Err(RedeemError::Expired); + }; + let token_matches = constant_time_equal(presented.as_bytes(), entry.token.as_bytes()); + let digest_matches = constant_time_equal(digest.as_bytes(), entry.digest.as_bytes()); + if token_matches && digest_matches { + Ok(()) + } else { + Err(RedeemError::Mismatch) + } + } + + /// How many tokens are outstanding. + #[cfg(test)] + pub(super) fn len(&self) -> usize { + self.entries.len() + } +} + +/// Whether `entry` has reached [`TOKEN_LIFETIME`] at `now`. +fn is_expired(entry: &Entry, now: Instant) -> bool { + now.saturating_duration_since(entry.issued) >= TOKEN_LIFETIME +} + +/// Compare two byte strings without stopping at the first difference. +/// +/// Lengths are not secret: tokens and digests have fixed lengths. +fn constant_time_equal(left: &[u8], right: &[u8]) -> bool { + if left.len() != right.len() { + return false; + } + let difference = left + .iter() + .zip(right) + .fold(0_u8, |accumulated, (a, b)| accumulated | (a ^ b)); + difference == 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + const DIGEST: &str = "aa"; + + fn id() -> Uuid { + Uuid::new_v4() + } + + #[test] + fn a_token_redeems_once() { + let mut tokens = ReviewTokens::default(); + let start = Instant::now(); + let approval = id(); + let token = tokens.issue(approval, DIGEST, start); + assert_eq!(tokens.redeem(approval, Some(&token), DIGEST, start), Ok(())); + assert_eq!( + tokens.redeem(approval, Some(&token), DIGEST, start), + Err(RedeemError::Expired) + ); + } + + #[test] + fn a_token_is_valid_just_before_the_lifetime_and_expired_at_it() { + let start = Instant::now(); + let approval = id(); + let mut tokens = ReviewTokens::default(); + let token = tokens.issue(approval, DIGEST, start); + let almost = start + TOKEN_LIFETIME - Duration::from_millis(1); + assert_eq!( + tokens.redeem(approval, Some(&token), DIGEST, almost), + Ok(()) + ); + + let token = tokens.issue(approval, DIGEST, start); + assert_eq!( + tokens.redeem(approval, Some(&token), DIGEST, start + TOKEN_LIFETIME), + Err(RedeemError::Expired) + ); + } + + #[test] + fn issuing_again_replaces_the_earlier_token() { + let start = Instant::now(); + let approval = id(); + let mut tokens = ReviewTokens::default(); + let first = tokens.issue(approval, DIGEST, start); + let second = tokens.issue(approval, DIGEST, start); + assert_ne!(first, second); + assert_eq!(tokens.len(), 1); + assert_eq!( + tokens.redeem(approval, Some(&first), DIGEST, start), + Err(RedeemError::Mismatch) + ); + // The failed attempt removed the entry, so the second token is gone too. + assert_eq!( + tokens.redeem(approval, Some(&second), DIGEST, start), + Err(RedeemError::Expired) + ); + } + + #[test] + fn a_wrong_token_or_digest_is_a_mismatch_and_removes_the_entry() { + let start = Instant::now(); + let approval = id(); + let mut tokens = ReviewTokens::default(); + let token = tokens.issue(approval, DIGEST, start); + assert_eq!( + tokens.redeem(approval, Some("not-the-token"), DIGEST, start), + Err(RedeemError::Mismatch) + ); + assert_eq!( + tokens.redeem(approval, Some(&token), DIGEST, start), + Err(RedeemError::Expired) + ); + + let token = tokens.issue(approval, DIGEST, start); + assert_eq!( + tokens.redeem(approval, Some(&token), "bb", start), + Err(RedeemError::Mismatch) + ); + assert_eq!(tokens.len(), 0); + } + + #[test] + fn a_missing_token_is_expired_and_removes_the_entry() { + let start = Instant::now(); + let approval = id(); + let mut tokens = ReviewTokens::default(); + let token = tokens.issue(approval, DIGEST, start); + assert_eq!( + tokens.redeem(approval, None, DIGEST, start), + Err(RedeemError::Expired) + ); + assert_eq!( + tokens.redeem(approval, Some(&token), DIGEST, start), + Err(RedeemError::Expired) + ); + } + + #[test] + fn a_token_for_one_approval_does_not_redeem_another() { + let start = Instant::now(); + let (a, b) = (id(), id()); + let mut tokens = ReviewTokens::default(); + let token_a = tokens.issue(a, DIGEST, start); + assert_eq!( + tokens.redeem(b, Some(&token_a), DIGEST, start), + Err(RedeemError::Expired) + ); + assert_eq!(tokens.redeem(a, Some(&token_a), DIGEST, start), Ok(())); + } + + #[test] + fn the_sixty_fifth_token_evicts_the_oldest() { + let start = Instant::now(); + let mut tokens = ReviewTokens::default(); + let approvals: Vec = (0..=MAX_OUTSTANDING_TOKENS).map(|_| id()).collect(); + let issued: Vec = approvals + .iter() + .map(|approval| tokens.issue(*approval, DIGEST, start)) + .collect(); + assert_eq!(tokens.len(), MAX_OUTSTANDING_TOKENS); + assert_eq!( + tokens.redeem(approvals[0], Some(&issued[0]), DIGEST, start), + Err(RedeemError::Expired) + ); + assert_eq!( + tokens.redeem(approvals[1], Some(&issued[1]), DIGEST, start), + Ok(()) + ); + } + + #[test] + fn expired_tokens_are_dropped_when_a_new_one_is_issued() { + let start = Instant::now(); + let mut tokens = ReviewTokens::default(); + for _ in 0..10 { + tokens.issue(id(), DIGEST, start); + } + tokens.issue(id(), DIGEST, start + TOKEN_LIFETIME); + assert_eq!(tokens.len(), 1); + } + + #[test] + fn tokens_are_random_version_four_identifiers() { + let start = Instant::now(); + let mut tokens = ReviewTokens::default(); + let token = tokens.issue(id(), DIGEST, start); + let parsed = Uuid::try_parse(&token).expect("a uuid token"); + assert_eq!(parsed.get_version_num(), 4); + } +} diff --git a/crates/omgcode-app/src/desktop/sessions.rs b/crates/omgcode-app/src/desktop/sessions.rs new file mode 100644 index 0000000..65f194f --- /dev/null +++ b/crates/omgcode-app/src/desktop/sessions.rs @@ -0,0 +1,450 @@ +//! Session tree, search, transcript, and lifecycle commands. + +use std::fmt::Write as _; + +use omgcode_core::session::{NativePath, SessionInspection, SessionJournal, TranscriptEntryKind}; +use omgcode_core::session_index::SnippetPart as IndexSnippetPart; +use omgcode_core::session_index::{ + ArchiveView, IndexedSession, SearchRole, SessionQuery, SessionSort, +}; +use sha2::{Digest, Sha256}; +use uuid::Uuid; + +use super::error::DesktopError; +use super::state::{folder_of, folder_of_path, format_time, summary_of, summary_of_inspection}; +use super::wire::{ + EntryKind, SearchHit, SearchRequest, SearchResult, SearchRoleDto, SessionRequest, + SessionSummary, SessionTree, SnippetPart, TranscriptEntry, TranscriptPage, TranscriptRequest, + TreeRequest, TreeSort, TreeView, WorkspaceGroup, source_label, +}; +use super::{DesktopService, Inner}; + +/// Most sessions one tree listing returns. +const LISTING_LIMIT: usize = 5000; +/// Most search hits one request returns. +const MAX_SEARCH_HITS: usize = 200; +/// Most transcript entries one page returns. +const MAX_PAGE_ENTRIES: usize = 200; +/// Most bytes of text one transcript page carries. A page always holds at +/// least one entry. +const MAX_PAGE_TEXT_BYTES: usize = 1024 * 1024; +/// Failure code recorded when the operator closes an open turn. +const INTERRUPT_CODE: &str = "operator_interrupted"; +/// Characters of a group key that make the opaque group identifier. +const GROUP_ID_CHARS: usize = 16; + +impl DesktopService { + /// List sessions grouped by their recorded workspace folder. + /// + /// The index is refreshed first, within a time bound. Groups appear in the + /// order their first session appears in the sorted listing, and sessions + /// keep the listing order. At most 5000 sessions are listed; + /// `indexed_sessions` counts the whole view. + /// + /// # Errors + /// + /// `busy` when the index is held elsewhere, `internal` otherwise. + pub fn session_tree(&self, request: TreeRequest) -> Result { + let inner = &self.inner; + let running = inner.running_sessions()?; + let query = SessionQuery { + view: archive_view(request.view), + sort: match request.sort { + TreeSort::Updated => SessionSort::Updated, + TreeSort::Created => SessionSort::Created, + TreeSort::Title => SessionSort::Title, + }, + descending: request.descending, + limit: LISTING_LIMIT, + }; + let (listing, skipped_entries) = inner.with_index(|state| { + state.refresh_bounded(false)?; + let listing = state + .index + .sessions(&query) + .map_err(|error| DesktopError::from_index(&error))?; + Ok((listing, state.skipped_entries)) + })?; + + let mut groups: Vec = Vec::new(); + let mut positions: std::collections::HashMap = + std::collections::HashMap::new(); + for session in &listing.sessions { + let key = group_key(session.workspace.as_ref()); + let summary = summary_of(session, &running); + if let Some(index) = positions.get(&key).copied() + && let Some(group) = groups.get_mut(index) + { + group.sessions.push(summary); + continue; + } + positions.insert(key.clone(), groups.len()); + groups.push(WorkspaceGroup { + group_id: key.chars().take(GROUP_ID_CHARS).collect(), + folder_display: folder_of(session), + sessions: vec![summary], + }); + } + Ok(SessionTree { + groups, + indexed_sessions: listing.total, + skipped_entries, + }) + } + + /// Search transcript text. + /// + /// The index is refreshed first. The query is never search-engine syntax; + /// an unusable query is `invalid_request`. The limit is clamped to 1 + /// through 200. + /// + /// # Errors + /// + /// `invalid_request`, `busy`, or `internal`. + pub fn session_search(&self, request: SearchRequest) -> Result { + let inner = &self.inner; + let running = inner.running_sessions()?; + let limit = usize::try_from(request.limit) + .unwrap_or(MAX_SEARCH_HITS) + .clamp(1, MAX_SEARCH_HITS); + let listing = inner.with_index(|state| { + state.refresh_bounded(false)?; + state + .index + .search(&request.query, archive_view(request.view), limit) + .map_err(|error| DesktopError::from_index(&error)) + })?; + let hits = listing + .hits + .iter() + .map(|hit| SearchHit { + session: summary_of(&hit.session, &running), + folder_display: folder_of(&hit.session), + seq: hit.seq, + role: match hit.role { + SearchRole::User => SearchRoleDto::User, + SearchRole::Assistant => SearchRoleDto::Assistant, + SearchRole::Tool => SearchRoleDto::Tool, + SearchRole::Summary => SearchRoleDto::Summary, + SearchRole::Title => SearchRoleDto::Title, + }, + snippet: hit.snippet.iter().map(snippet_part).collect(), + }) + .collect(); + Ok(SearchResult { + hits, + truncated: listing.truncated, + }) + } + + /// Read one page of a session's transcript without the writer lock. + /// + /// Returns entries with a sequence number above `after_seq` (all when it is + /// `null`), at most `limit` (clamped to 1 through 200) and at most 1 MiB of + /// text, always at least one. `next_after_seq` is the last returned + /// sequence number when more entries remain. + /// + /// # Errors + /// + /// `not_found`; `busy` when a turn runs here for the session or another + /// process holds the journal; `invalid_journal` when it fails validation. + pub fn session_transcript( + &self, + request: TranscriptRequest, + ) -> Result { + let inner = &self.inner; + if inner.is_running(request.session_id)? { + return Err(DesktopError::busy()); + } + let path = inner.resolve_path(request.session_id)?; + let inspection = SessionInspection::inspect(&path) + .map_err(|error| DesktopError::from_session(&error))?; + let limit = usize::try_from(request.limit) + .unwrap_or(MAX_PAGE_ENTRIES) + .clamp(1, MAX_PAGE_ENTRIES); + let mut entries = Vec::new(); + let mut text_bytes = 0_usize; + let mut more = false; + for entry in inspection + .entries + .iter() + .filter(|entry| request.after_seq.is_none_or(|after| entry.seq > after)) + { + let kind = entry_kind(&entry.kind); + let size = entry_text_bytes(&kind); + let next_total = text_bytes.saturating_add(size); + if !entries.is_empty() && (entries.len() >= limit || next_total > MAX_PAGE_TEXT_BYTES) { + more = true; + break; + } + let timestamp = format_time(entry.timestamp) + .ok_or_else(|| DesktopError::internal("A recorded time could not be formatted."))?; + text_bytes = next_total; + entries.push(TranscriptEntry { + seq: entry.seq, + timestamp, + kind, + }); + } + let next_after_seq = if more { + entries.last().map(|entry| entry.seq) + } else { + None + }; + Ok(TranscriptPage { + session: summary_of_inspection(&inspection), + folder_display: folder_of_path(&inspection.workspace), + entries, + next_after_seq, + incomplete_tail: inspection.incomplete_tail, + }) + } + + /// Archive an idle session. Archiving an archived session changes nothing. + /// + /// # Errors + /// + /// `turn_running`, `turn_open`, `busy`, `not_found`, or `invalid_journal`. + pub fn session_archive(&self, request: SessionRequest) -> Result { + self.lifecycle(request.session_id, |journal| { + journal + .archive() + .map_err(|error| DesktopError::from_session(&error)) + }) + } + + /// Restore an archived session. Restoring an active session changes nothing. + /// + /// # Errors + /// + /// `turn_running`, `busy`, `not_found`, or `invalid_journal`. + pub fn session_restore(&self, request: SessionRequest) -> Result { + self.lifecycle(request.session_id, |journal| { + journal + .restore() + .map_err(|error| DesktopError::from_session(&error)) + }) + } + + /// Close a session's unfinished turn with the failure code + /// `operator_interrupted`. It starts no work and replays nothing. + /// + /// # Errors + /// + /// `invalid_request` when no turn is open, `turn_running`, `busy`, + /// `not_found`, or `invalid_journal`. + pub fn session_interrupt( + &self, + request: SessionRequest, + ) -> Result { + self.lifecycle(request.session_id, |journal| { + let Some(turn) = journal.snapshot().active_turn else { + return Err(DesktopError::invalid_request("No turn is open.")); + }; + journal + .fail_turn(turn, INTERRUPT_CODE) + .map_err(|error| DesktopError::from_session(&error)) + }) + } + + /// Open the journal, apply one change, release it, refresh the index for + /// this session, and summarize it. + fn lifecycle( + &self, + session_id: Uuid, + change: impl FnOnce(&mut SessionJournal) -> Result<(), DesktopError>, + ) -> Result { + let inner = &self.inner; + if inner.is_running(session_id)? { + return Err(DesktopError::turn_running()); + } + let path = inner.resolve_path(session_id)?; + let mut journal = + SessionJournal::open(&path).map_err(|error| DesktopError::from_session(&error))?; + let changed = change(&mut journal); + drop(journal); + changed?; + refreshed_summary(inner, session_id) + } +} + +/// Refresh one session in the index and summarize it. +pub(super) fn refreshed_summary( + inner: &Inner, + session_id: Uuid, +) -> Result { + let running = inner.running_sessions()?; + let session: IndexedSession = inner + .with_index(|state| { + state + .index + .refresh_session(session_id) + .map_err(|error| DesktopError::from_index(&error)) + })? + .ok_or_else(|| DesktopError::not_found("session"))?; + Ok(summary_of(&session, &running)) +} + +const fn archive_view(view: TreeView) -> ArchiveView { + match view { + TreeView::Active => ArchiveView::Active, + TreeView::Archived => ArchiveView::Archived, + } +} + +fn snippet_part(part: &IndexSnippetPart) -> SnippetPart { + SnippetPart { + text: part.text.clone(), + matched: part.matched, + } +} + +/// The grouping key of a recorded workspace path: the hex SHA-256 over the +/// path's encoding tag and units (exact equality of paths, up to a hash +/// collision), or the literal `unknown` when the journal recorded none. The +/// group identifier is the first 16 characters of the key. +fn group_key(path: Option<&NativePath>) -> String { + let Some(path) = path else { + return String::from("unknown"); + }; + let mut hasher = Sha256::new(); + match path { + NativePath::UnixBytes(bytes) => { + hasher.update(b"unix_bytes\0"); + hasher.update(bytes); + } + NativePath::WindowsUtf16(units) => { + hasher.update(b"windows_utf16\0"); + for unit in units { + hasher.update(unit.to_le_bytes()); + } + } + } + let digest = hasher.finalize(); + let mut text = String::with_capacity(64); + for byte in digest.iter() { + // Writing to a String cannot fail. + if write!(text, "{byte:02x}").is_err() { + break; + } + } + text +} + +/// Map one recorded event to the wire union. +fn entry_kind(kind: &TranscriptEntryKind) -> EntryKind { + match kind { + TranscriptEntryKind::Message { + role, + source, + text, + call_id, + } => EntryKind::Message { + role: (*role).into(), + source: source_label(*source).to_owned(), + text: text.clone(), + call_id: call_id.clone(), + }, + TranscriptEntryKind::TurnStarted { request_id } => EntryKind::TurnStarted { + request_id: *request_id, + }, + TranscriptEntryKind::TurnCompleted { request_id } => EntryKind::TurnCompleted { + request_id: *request_id, + }, + TranscriptEntryKind::TurnFailed { request_id, code } => EntryKind::TurnFailed { + request_id: *request_id, + code: Some(code.clone()), + }, + TranscriptEntryKind::ToolRequested { + request_id, + call_id, + tool, + arguments, + } => EntryKind::ToolRequested { + request_id: *request_id, + call_id: call_id.clone(), + tool: tool.clone(), + arguments: arguments.clone(), + }, + TranscriptEntryKind::ToolCompleted { + request_id, + call_id, + outcome, + code, + output_bytes, + output_sha256: _, + } => EntryKind::ToolCompleted { + request_id: *request_id, + call_id: call_id.clone(), + outcome: (*outcome).into(), + code: code.clone(), + output_bytes: *output_bytes, + }, + TranscriptEntryKind::RequestManifest { + request_id, + model, + prompt_version, + tools, + } => EntryKind::RequestManifest { + request_id: *request_id, + model: model.clone(), + prompt_version: prompt_version.clone(), + tools: tools.clone(), + }, + TranscriptEntryKind::ContextCompacted { record } => EntryKind::ContextCompacted { + summary: record.summary.clone(), + compacted_messages: record.compacted_messages, + model: record.model.clone(), + }, + TranscriptEntryKind::SessionArchived => EntryKind::SessionArchived, + TranscriptEntryKind::SessionRestored => EntryKind::SessionRestored, + } +} + +/// Bytes of recorded text an entry adds to a page. +fn entry_text_bytes(kind: &EntryKind) -> usize { + match kind { + EntryKind::Message { text, .. } => text.len(), + EntryKind::ToolRequested { + arguments, tool, .. + } => arguments.len().saturating_add(tool.len()), + EntryKind::ContextCompacted { summary, .. } => summary.len(), + EntryKind::RequestManifest { tools, .. } => tools + .iter() + .fold(0_usize, |total, tool| total.saturating_add(tool.len())), + EntryKind::TurnStarted { .. } + | EntryKind::TurnCompleted { .. } + | EntryKind::TurnFailed { .. } + | EntryKind::ToolCompleted { .. } + | EntryKind::SessionArchived + | EntryKind::SessionRestored => 0, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn group_ids_are_sixteen_hex_characters_and_depend_on_the_encoding() { + let unix = group_key(Some(&NativePath::UnixBytes(vec![b'a']))); + assert_eq!(unix.len(), 64); + let windows = group_key(Some(&NativePath::WindowsUtf16(vec![u16::from(b'a')]))); + assert_eq!(unix.chars().take(GROUP_ID_CHARS).count(), 16); + assert!(unix.bytes().all(|byte| byte.is_ascii_hexdigit())); + assert_ne!(unix, windows); + assert_eq!(group_key(None), "unknown"); + } + + #[test] + fn paths_that_differ_only_by_the_verbatim_prefix_are_separate_groups() { + let units = |text: &str| NativePath::WindowsUtf16(text.encode_utf16().collect()); + let verbatim = group_key(Some(&units(r"\\?\C:\proj"))); + let plain = group_key(Some(&units(r"C:\proj"))); + assert_ne!(verbatim, plain); + assert_ne!( + verbatim.chars().take(GROUP_ID_CHARS).collect::(), + plain.chars().take(GROUP_ID_CHARS).collect::() + ); + } +} diff --git a/crates/omgcode-app/src/desktop/state.rs b/crates/omgcode-app/src/desktop/state.rs new file mode 100644 index 0000000..38d2c50 --- /dev/null +++ b/crates/omgcode-app/src/desktop/state.rs @@ -0,0 +1,361 @@ +//! Shared helpers over the service state: locks, bounded refresh, journal +//! resolution, the settings-store opener, and session summaries. + +use std::collections::HashSet; +use std::path::PathBuf; +use std::sync::MutexGuard; +use std::thread; +use std::time::{Duration, Instant}; + +use omgcode_core::control::{ControlError, ControlStore}; +use omgcode_core::session::{NativePath, SessionInspection}; +use omgcode_core::session_index::{IndexedSession, IndexedState, SessionIndex}; +use omgcode_core::turn::display_safe_line; +use time::OffsetDateTime; +use time::format_description::well_known::Rfc3339; +use uuid::Uuid; + +use super::Inner; +use super::error::DesktopError; +use super::review_tokens::ReviewTokens; +use super::turns::TurnTable; +use super::wire::{SessionStatus, SessionSummary}; + +/// Journals inspected per `refresh` call. +const REFRESH_CHUNK: usize = 200; +/// Longest a bounded refresh keeps starting new chunks. +const REFRESH_DEADLINE: Duration = Duration::from_secs(2); +/// A completed refresh newer than this makes the next listing skip refreshing. +const REFRESH_REUSE: Duration = Duration::from_secs(1); +/// Longest a settings-store open waits for another holder. +const CONTROL_RETRY_DEADLINE: Duration = Duration::from_secs(2); +/// Pause between settings-store open attempts. +const CONTROL_RETRY_INTERVAL: Duration = Duration::from_millis(20); + +/// Folder text for a session whose journal recorded no workspace. +pub(super) const UNKNOWN_FOLDER: &str = "(unknown folder)"; +/// Title of a busy session the index never read. +const TITLE_IN_USE: &str = "(session in use)"; +/// Title of a session whose journal failed validation. +const TITLE_UNREADABLE: &str = "(unreadable session)"; +/// Title of a readable session that somehow has none. +const TITLE_UNTITLED: &str = "(untitled session)"; +/// Folder text for a path that another operating system recorded. +const FOLDER_NOT_REPRESENTABLE: &str = "(path not representable on this system)"; + +/// The index connection with its refresh bookkeeping. +pub(super) struct IndexState { + pub(super) index: SessionIndex, + /// When the last refresh that reached `remaining == 0` finished. + last_refresh: Option, + /// `skipped_links + unrecognized` of the last refresh report. + pub(super) skipped_entries: u64, +} + +impl IndexState { + pub(super) const fn new(index: SessionIndex) -> Self { + Self { + index, + last_refresh: None, + skipped_entries: 0, + } + } + + /// Bring the index up to date within a time bound. + /// + /// Calls `refresh(200)` until nothing remains or the deadline passes. A + /// refresh that stops at the deadline is partial; the next call continues. + /// Unless `force` is set, a completed refresh less than a second old is + /// reused. + pub(super) fn refresh_bounded(&mut self, force: bool) -> Result<(), DesktopError> { + if !force + && self + .last_refresh + .is_some_and(|finished| finished.elapsed() < REFRESH_REUSE) + { + return Ok(()); + } + let started = Instant::now(); + loop { + let report = self + .index + .refresh(REFRESH_CHUNK) + .map_err(|error| DesktopError::from_index(&error))?; + self.skipped_entries = report.skipped_links.saturating_add(report.unrecognized); + if report.remaining == 0 { + self.last_refresh = Some(Instant::now()); + return Ok(()); + } + if started.elapsed() >= REFRESH_DEADLINE { + return Ok(()); + } + } + } +} + +impl Inner { + pub(super) fn lock_index(&self) -> Result, DesktopError> { + self.index.lock().map_err(|_| DesktopError::poisoned()) + } + + pub(super) fn lock_turns(&self) -> Result, DesktopError> { + self.turns.lock().map_err(|_| DesktopError::poisoned()) + } + + pub(super) fn lock_reviews(&self) -> Result, DesktopError> { + self.reviews.lock().map_err(|_| DesktopError::poisoned()) + } + + /// Run `action` with the index locked. Keep `action` short. + pub(super) fn with_index( + &self, + action: impl FnOnce(&mut IndexState) -> Result, + ) -> Result { + let mut state = self.lock_index()?; + action(&mut state) + } + + /// The sessions with a chat turn running in this service. + pub(super) fn running_sessions(&self) -> Result, DesktopError> { + self.reap()?; + Ok(self.lock_turns()?.running_ids()) + } + + /// Whether a chat turn for `session_id` is running in this service. + pub(super) fn is_running(&self, session_id: Uuid) -> Result { + self.reap()?; + Ok(self.lock_turns()?.is_running(session_id)) + } + + /// Resolve a session's journal path, only through the index. + /// + /// An unknown session triggers one bounded refresh and a retry. + pub(super) fn resolve_path(&self, session_id: Uuid) -> Result { + self.with_index(|state| { + let lookup = |state: &IndexState| { + state + .index + .journal_path(session_id) + .map_err(|error| DesktopError::from_index(&error)) + }; + if let Some(path) = lookup(state)? { + return Ok(path); + } + state.refresh_bounded(true)?; + lookup(state)?.ok_or_else(|| DesktopError::not_found("session")) + }) + } + + /// Open the existing settings store, waiting briefly for another holder. + /// + /// `Ok(None)` means no store exists. No service lock is held while waiting. + pub(super) fn open_control(&self) -> Result, DesktopError> { + let deadline = Instant::now() + CONTROL_RETRY_DEADLINE; + loop { + match ControlStore::open_existing(&self.data_root) { + Ok(store) => return Ok(store), + Err(ControlError::Busy) if Instant::now() < deadline => { + thread::sleep(CONTROL_RETRY_INTERVAL); + } + Err(error) => return Err(DesktopError::from_control(&error)), + } + } + } +} + +/// The status of an indexed session. A turn running here wins over everything, +/// because on Windows the journal is locked while the turn runs. +pub(super) fn status_of(session: &IndexedSession, running: &HashSet) -> SessionStatus { + if running.contains(&session.session_id) { + return SessionStatus::Running; + } + match session.state { + IndexedState::Invalid => SessionStatus::Invalid, + IndexedState::Busy => SessionStatus::Busy, + IndexedState::Ok if session.active_turn.is_some() => SessionStatus::TurnOpen, + IndexedState::Ok => SessionStatus::Idle, + } +} + +/// Summarize an indexed session for the interface. +pub(super) fn summary_of(session: &IndexedSession, running: &HashSet) -> SessionSummary { + let status = status_of(session, running); + let placeholder = match status { + SessionStatus::Invalid => TITLE_UNREADABLE, + SessionStatus::Idle | SessionStatus::TurnOpen => TITLE_UNTITLED, + SessionStatus::Running | SessionStatus::Busy => TITLE_IN_USE, + }; + SessionSummary { + session_id: session.session_id, + title: session + .title + .as_deref() + .map_or_else(|| placeholder.to_owned(), display_safe_line), + created_at: session.created_at.and_then(format_time), + updated_at: session.updated_at.and_then(format_time), + status, + archived: session.archived, + event_count: session.event_count, + invalid_code: if status == SessionStatus::Invalid { + session.invalid_code.clone() + } else { + None + }, + } +} + +/// Rewrite a Windows verbatim path as the text people expect to read. +/// +/// `\\?\UNC\server\share\x` becomes `\\server\share\x` and `\\?\C:\x` becomes +/// `C:\x`. Every other text is returned unchanged: other verbatim forms such as +/// `\\?\Volume{...}\`, `\\.\` device paths, a `\\?\` that is not at the start, +/// and every non-Windows path. +/// +/// This is display text only. Never use the result to open, compare, group, or +/// authorize a path; those keep the recorded value. Escape the result with +/// [`display_safe_line`] afterwards. +pub(super) fn windows_display_path(text: &str) -> String { + const VERBATIM: &str = r"\\?\"; + let Some(rest) = text.strip_prefix(VERBATIM) else { + return text.to_owned(); + }; + // `get(..4)` is a character-boundary-checked slice, so no panic on text. + if let Some(head) = rest.get(..4) + && head.eq_ignore_ascii_case(r"UNC\") + && let Some(share) = rest.get(4..) + && !share.is_empty() + { + return format!(r"\\{share}"); + } + let mut characters = rest.chars(); + if let (Some(letter), Some(':')) = (characters.next(), characters.next()) + && letter.is_ascii_alphabetic() + && matches!(characters.next(), None | Some('\\')) + { + return rest.to_owned(); + } + text.to_owned() +} + +/// Escape folder text for display after the verbatim prefix is removed. +fn folder_text(raw: &str) -> String { + display_safe_line(&windows_display_path(raw)) +} + +/// The display-safe folder text of an indexed session. +pub(super) fn folder_of(session: &IndexedSession) -> String { + session + .folder_display + .as_deref() + .filter(|text| !text.is_empty()) + .map_or_else(|| UNKNOWN_FOLDER.to_owned(), folder_text) +} + +/// The display-safe folder text of a registered workspace directory, or the +/// not-representable text when this system cannot form the path. +pub(super) fn folder_of_directory(directory: Option) -> String { + directory.map_or_else( + || FOLDER_NOT_REPRESENTABLE.to_owned(), + |path| folder_text(&path.to_string_lossy()), + ) +} + +/// Summarize a fresh inspection. No turn of this session runs here. +pub(super) fn summary_of_inspection(inspection: &SessionInspection) -> SessionSummary { + SessionSummary { + session_id: inspection.session_id, + title: display_safe_line(&inspection.title), + created_at: format_time(inspection.created_at), + updated_at: format_time(inspection.updated_at), + status: if inspection.active_turn.is_some() { + SessionStatus::TurnOpen + } else { + SessionStatus::Idle + }, + archived: inspection.archived, + event_count: inspection.event_count, + invalid_code: None, + } +} + +/// The display-safe folder text of a recorded native path. +pub(super) fn folder_of_path(path: &NativePath) -> String { + folder_of_directory(path.to_path_buf().ok()) +} + +/// Format a time as RFC 3339, or `None` when it is outside the format's range. +pub(super) fn format_time(time: OffsetDateTime) -> Option { + time.format(&Rfc3339).ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn verbatim_drive_and_unc_paths_are_rewritten() { + assert_eq!( + windows_display_path(r"\\?\C:\Users\me\proj"), + r"C:\Users\me\proj" + ); + assert_eq!(windows_display_path(r"\\?\c:\x"), r"c:\x"); + assert_eq!(windows_display_path(r"\\?\Z:"), "Z:"); + assert_eq!( + windows_display_path(r"\\?\UNC\server\share\x"), + r"\\server\share\x" + ); + assert_eq!( + windows_display_path(r"\\?\unc\server\share"), + r"\\server\share" + ); + } + + #[test] + fn other_text_is_returned_unchanged() { + for text in [ + "", + "/home/me/proj", + r"C:\Users\me", + r"\\server\share", + r"\\?\Volume{01234567-89ab-cdef-0123-456789abcdef}\x", + r"\\.\COM1", + r"\\.\C:\x", + r"\\?\UNC\", + r"\\?\UNC", + r"\\?\C", + r"\\?\1:\x", + r"\\?\C:x", + r"D:\a\\?\C:\x", + r"\\?\", + "\\\\?\\\u{e9}\u{e9}\u{e9}", + ] { + assert_eq!(windows_display_path(text), text, "{text:?}"); + } + } + + #[test] + fn hostile_text_after_the_prefix_is_still_escaped() { + assert_eq!( + folder_text("\\\\?\\C:\\evil\u{202e}dir\nnext"), + "C:\\evil\\u{202e}dir\\u{000a}next" + ); + assert_eq!( + folder_text("\\\\?\\UNC\\srv\u{200b}\\share"), + "\\\\srv\\u{200b}\\share" + ); + // A forged escape after the rewrite is still neutralised. + assert_eq!(folder_text("\\\\?\\C:\\u{202e}"), "C:\\u{5c}u{202e}"); + } + + #[test] + fn a_directory_that_cannot_be_formed_keeps_its_text() { + assert_eq!( + folder_of_directory(None), + "(path not representable on this system)" + ); + assert_eq!( + folder_of_directory(Some(PathBuf::from("/work/a\u{202e}b"))), + "/work/a\\u{202e}b" + ); + } +} diff --git a/crates/omgcode-app/src/desktop/turns.rs b/crates/omgcode-app/src/desktop/turns.rs new file mode 100644 index 0000000..23451e3 --- /dev/null +++ b/crates/omgcode-app/src/desktop/turns.rs @@ -0,0 +1,583 @@ +//! The table of chat turns and the thread that runs each one. +//! +//! A turn owns, for its whole life, the journal handle, the validated clients, +//! and the server token, all on its own thread. The table holds only what other +//! threads need: the state, the cancel token, and the `JoinHandle`. The table +//! lock is held for short bookkeeping and never while the turn runs, while a +//! thread is joined, or while the index is refreshed. + +use std::collections::{HashMap, HashSet}; +use std::path::PathBuf; +use std::sync::Arc; +use std::thread::{self, JoinHandle}; +use std::time::{Duration, Instant}; + +use omgcode_core::session::SessionJournal; +use omgcode_core::turn::CancelToken; +use uuid::Uuid; + +use super::Inner; +use super::error::DesktopError; +use super::wire::{ChatOutcome, ContextMeterDto, ToolsMode}; +use crate::{ + ChatClients, ChatHooks, ChatSettings, ChatStreamEvent, ChatTurnInput, ChatTurnOutput, LmToken, + run_chat_turn_with_clients, +}; + +/// Most chat turns that run at once. +pub(super) const MAX_RUNNING_TURNS: usize = 2; +/// Most finished results kept; the oldest is evicted first. +pub(super) const MAX_FINISHED_TURNS: usize = 64; +/// Name of every turn thread. +pub(super) const TURN_THREAD_NAME: &str = "omgcode-turn"; +/// How often shutdown checks whether turn threads have finished. +const SHUTDOWN_POLL: Duration = Duration::from_millis(10); +/// Message of a turn thread that ended without recording a result. +const STOPPED_UNEXPECTEDLY: &str = "The chat turn stopped unexpectedly."; + +/// What happened to the threads that were in flight when shutdown began. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ShutdownReport { + /// Turn threads that were in flight at the start and have finished. + pub finished: usize, + /// Turn threads still running when the grace period ended. A streamed + /// request is interrupted by the cancel request, so this is a turn whose + /// request is not streamed, or one in a step that cannot be interrupted + /// (a tool call, or compaction). Such a thread may still be waiting; its + /// journal is left with an open turn. + pub still_running: usize, +} + +/// Where one session's turn stands. +pub(super) enum SlotState { + /// The turn is running. + Running { + /// A cancel was requested (it may have no token to act on yet). + cancel_requested: bool, + /// The turn's cancel token, once it exists. A streamed turn has one; a + /// turn whose request is not streamed has none unless it uses tools. + cancel: Option, + }, + /// The turn ended. + Finished(Box>), +} + +/// One session's slot. +pub(super) struct Slot { + pub(super) state: SlotState, + /// The turn thread, until it is joined. + handle: Option>, + /// Order in which finished slots were finished; smaller is older. + finished_order: u64, +} + +/// All slots, keyed by session. +#[derive(Default)] +pub(super) struct TurnTable { + slots: HashMap, + next_order: u64, + /// Set by shutdown; no new turn starts afterwards. + closed: bool, +} + +impl TurnTable { + pub(super) const fn is_closed(&self) -> bool { + self.closed + } + + pub(super) fn slot(&self, session_id: Uuid) -> Option<&Slot> { + self.slots.get(&session_id) + } + + pub(super) fn is_running(&self, session_id: Uuid) -> bool { + self.slots + .get(&session_id) + .is_some_and(|slot| matches!(slot.state, SlotState::Running { .. })) + } + + pub(super) fn running_ids(&self) -> HashSet { + self.slots + .iter() + .filter(|(_, slot)| matches!(slot.state, SlotState::Running { .. })) + .map(|(id, _)| *id) + .collect() + } + + pub(super) fn running_count(&self) -> usize { + self.slots + .values() + .filter(|slot| matches!(slot.state, SlotState::Running { .. })) + .count() + } + + /// Slots whose thread has not been joined yet. + fn unjoined_count(&self) -> usize { + self.slots + .values() + .filter(|slot| slot.handle.is_some()) + .count() + } + + /// Insert a running slot, replacing a finished one for the same session and + /// evicting the oldest joined finished slots beyond the bound. + pub(super) fn insert_running(&mut self, session_id: Uuid, handle: JoinHandle<()>) { + self.slots.insert( + session_id, + Slot { + state: SlotState::Running { + cancel_requested: false, + cancel: None, + }, + handle: Some(handle), + finished_order: 0, + }, + ); + loop { + let mut finished: Vec<(Uuid, u64)> = self + .slots + .iter() + .filter(|(_, slot)| { + matches!(slot.state, SlotState::Finished(_)) && slot.handle.is_none() + }) + .map(|(id, slot)| (*id, slot.finished_order)) + .collect(); + if finished.len() <= MAX_FINISHED_TURNS { + return; + } + finished.sort_by_key(|(_, order)| *order); + match finished.first() { + Some((oldest, _)) => { + self.slots.remove(oldest); + } + None => return, + } + } + } + + /// Record a cancel request. Returns without effect for a slot that is not running. + pub(super) fn request_cancel(&mut self, session_id: Uuid) { + if let Some(Slot { + state: + SlotState::Running { + cancel_requested, + cancel, + }, + .. + }) = self.slots.get_mut(&session_id) + { + *cancel_requested = true; + if let Some(token) = cancel { + token.cancel(); + } + } + } + + /// Request cancellation of every running turn. + fn request_cancel_all(&mut self) { + for slot in self.slots.values_mut() { + if let SlotState::Running { + cancel_requested, + cancel, + } = &mut slot.state + { + *cancel_requested = true; + if let Some(token) = cancel { + token.cancel(); + } + } + } + } + + /// Store the turn's cancel token, acting on an earlier request. + fn store_cancel_token(&mut self, session_id: Uuid, token: CancelToken) { + if let Some(Slot { + state: + SlotState::Running { + cancel_requested, + cancel, + }, + .. + }) = self.slots.get_mut(&session_id) + { + if *cancel_requested { + token.cancel(); + } + *cancel = Some(token); + } + } + + /// End a running slot with `result`. + fn finish(&mut self, session_id: Uuid, result: Result) { + let order = self.next_order; + if let Some(slot) = self.slots.get_mut(&session_id) + && matches!(slot.state, SlotState::Running { .. }) + { + slot.state = SlotState::Finished(Box::new(result)); + slot.finished_order = order; + self.next_order = order.saturating_add(1); + } + } +} + +impl Inner { + /// Join turn threads that have finished. + /// + /// Handles are taken out under the lock and joined after it is released. A + /// thread that panicked leaves its slot `running`, so it is recorded as + /// `failed` with `internal`. + pub(super) fn reap(&self) -> Result<(), DesktopError> { + let finished: Vec<(Uuid, JoinHandle<()>)> = { + let mut turns = self.lock_turns()?; + let ids: Vec = turns + .slots + .iter() + .filter(|(_, slot)| slot.handle.as_ref().is_some_and(JoinHandle::is_finished)) + .map(|(id, _)| *id) + .collect(); + ids.into_iter() + .filter_map(|id| { + turns + .slots + .get_mut(&id) + .and_then(|slot| slot.handle.take()) + .map(|handle| (id, handle)) + }) + .collect() + }; + for (session_id, handle) in finished { + // The thread already finished, so this join returns at once. + if handle.join().is_err() { + self.lock_turns()?.finish( + session_id, + Err(DesktopError::internal(STOPPED_UNEXPECTEDLY)), + ); + } + } + Ok(()) + } + + /// Request cancellation of every turn, wait up to `grace`, and report. + pub(super) fn shutdown_turns(&self, grace: Duration) -> ShutdownReport { + let deadline = Instant::now().checked_add(grace); + let in_flight = match self.lock_turns() { + Ok(mut turns) => { + turns.closed = true; + turns.request_cancel_all(); + turns.unjoined_count() + } + // A poisoned table cannot be inspected or cancelled; nothing is + // known to be finished. + Err(_) => { + return ShutdownReport { + finished: 0, + still_running: 0, + }; + } + }; + let mut remaining = in_flight; + loop { + if self.reap().is_err() { + break; + } + remaining = self + .lock_turns() + .map_or(remaining, |turns| turns.unjoined_count()); + let expired = deadline.is_none_or(|limit| Instant::now() >= limit); + if remaining == 0 || expired { + break; + } + thread::sleep(SHUTDOWN_POLL); + } + ShutdownReport { + finished: in_flight.saturating_sub(remaining), + still_running: remaining, + } + } + + /// Hand the turn's cancel token to the slot. + fn hand_over_cancel_token(&self, session_id: Uuid, token: CancelToken) { + // A poisoned table is reported to every caller of the service; the + // turn simply cannot be cancelled and runs to its deadline. + if let Ok(mut turns) = self.lock_turns() { + turns.store_cancel_token(session_id, token); + } + } + + /// Record the end of a turn. + fn record_finish(&self, session_id: Uuid, result: Result) { + // A poisoned table is reported to every caller of the service; the + // thread has nothing left to do either way. + if let Ok(mut turns) = self.lock_turns() { + turns.finish(session_id, result); + } + } +} + +/// Everything a turn thread owns. +pub(super) struct TurnJob { + pub(super) session_id: Uuid, + pub(super) path: PathBuf, + pub(super) message: String, + pub(super) settings: ChatSettings, + pub(super) token: Option, + pub(super) clients: ChatClients, +} + +/// Start the turn thread. +/// +/// The caller holds the `turns` lock; the thread does not need it until it +/// stores its cancel token, so it waits there briefly if it is fast. +pub(super) fn spawn_turn(inner: &Arc, job: TurnJob) -> Result, DesktopError> { + let worker = Arc::clone(inner); + thread::Builder::new() + .name(TURN_THREAD_NAME.to_owned()) + .spawn(move || run_turn(&worker, job)) + .map_err(|_| DesktopError::internal("The chat turn could not be started.")) +} + +/// Ends a turn exactly once: records its result, then pushes `finished`. +/// +/// Created at the top of the thread body. [`complete`](Self::complete) records +/// the result in the turn table and only then tells the event hub, so a caller +/// that reads `chat_status` after a `finished` event never sees `running`. If +/// the guard is dropped without completing, the thread is unwinding from a +/// panic: `Drop` records `internal` as the result first and then sends +/// `finished`, so the event is not lost. `Drop` takes locks only with +/// `if let Ok(..)`, so it cannot panic; a poisoned lock leaves the step undone. +/// +/// The `turns` lock is released before the `events` lock is taken. +struct FinishGuard<'a> { + inner: &'a Inner, + session_id: Uuid, + completed: bool, +} + +impl<'a> FinishGuard<'a> { + const fn new(inner: &'a Inner, session_id: Uuid) -> Self { + Self { + inner, + session_id, + completed: false, + } + } + + /// Record `result`, then push `finished`. + fn complete(mut self, result: Result) { + self.completed = true; + self.inner.record_finish(self.session_id, result); + self.inner.events_finish(self.session_id); + } +} + +impl Drop for FinishGuard<'_> { + fn drop(&mut self) { + if !self.completed { + self.inner.record_finish( + self.session_id, + Err(DesktopError::internal(STOPPED_UNEXPECTEDLY)), + ); + self.inner.events_finish(self.session_id); + } + } +} + +/// The body of a turn thread. +fn run_turn(inner: &Inner, job: TurnJob) { + let session_id = job.session_id; + let guard = FinishGuard::new(inner, session_id); + inner.events_begin(session_id); + let result = execute(inner, &job); + // The journal handle is gone, so the index can read the finished journal. + // A failed refresh leaves the cache stale until the next listing refresh; + // the journal remains the source of truth, so it is not a turn failure. + drop(inner.with_index(|state| { + state + .index + .refresh_session(session_id) + .map_err(|error| DesktopError::from_index(&error)) + })); + guard.complete(result); +} + +/// Open the journal, run the turn, and drop the journal before returning. +fn execute(inner: &Inner, job: &TurnJob) -> Result { + let token = job.token.as_ref().map(LmToken::expose); + let mut journal = + SessionJournal::open(&job.path).map_err(|error| DesktopError::from_session(&error))?; + let session_id = job.session_id; + let mut hand_over = |cancel: CancelToken| inner.hand_over_cancel_token(session_id, cancel); + let mut forward = |event: ChatStreamEvent<'_>| inner.events_stream(session_id, event); + let mut hooks = ChatHooks { + on_cancel_token: Some(&mut hand_over), + on_stream: if inner.stream { + Some(&mut forward) + } else { + None + }, + }; + let result = run_chat_turn_with_clients( + &mut journal, + &inner.data_root, + &ChatTurnInput { + message: &job.message, + settings: &job.settings, + token, + }, + &job.clients, + &mut hooks, + ); + drop(journal); + match result { + Ok(output) => Ok(outcome_of(&output)), + Err(error) => { + // Defence in depth: the application layer keeps the token out of + // messages, and this removes it again if that ever failed. + Err(DesktopError::from_app(&error).redacted(token)) + } + } +} + +/// Map a completed turn to the wire outcome. +fn outcome_of(output: &ChatTurnOutput) -> ChatOutcome { + let context = output.context(); + let count = + |value: Option| value.map_or(0, |count| u64::try_from(count).unwrap_or(u64::MAX)); + ChatOutcome { + finish_reason: output.finish_reason().to_owned(), + tools_mode: if output.tools_enabled() { + ToolsMode::Enabled + } else { + ToolsMode::Disabled + }, + tools_disabled_reason: output.disabled_reason().map(str::to_owned), + rounds: count(output.rounds()), + tool_calls: count(output.tool_calls()), + withheld_tool_results: u64::try_from(output.withheld_tool_results()).unwrap_or(u64::MAX), + context: Some(ContextMeterDto { + estimated_tokens: context.estimated_input_tokens(), + trigger_tokens: context.effective_trigger(), + context_tokens: context.context_tokens(), + compacted: context.compaction().is_some(), + }), + } +} + +#[cfg(test)] +mod tests { + use std::sync::Mutex; + + use super::super::wire::ChatEvent; + use super::super::{ChatEventSink, DesktopConfig, DesktopService}; + use super::*; + use crate::TokenSource; + + /// What the sink saw at each `finished` event: whether the table still + /// held the slot as running, and a record of every event. + struct Seen { + events: Vec, + running_at_finished: Vec, + } + + fn service_with_slot(session_id: Uuid) -> (tempfile::TempDir, DesktopService) { + let directory = tempfile::tempdir().expect("data root"); + let service = DesktopService::open(DesktopConfig { + data_root: directory.path().to_path_buf(), + endpoint: "http://127.0.0.1:1/v1".to_owned(), + token_source: TokenSource::Environment, + stream: true, + }) + .expect("open service"); + let handle = thread::spawn(|| ()); + service + .inner + .lock_turns() + .expect("turns") + .insert_running(session_id, handle); + (directory, service) + } + + /// Subscribe a sink that notes the table state when `finished` arrives. + /// + /// The sink takes the `turns` lock while the hub lock is held. Production + /// code never does; the test relies on the guard having released `turns` + /// before it sends `finished`. + fn watch(service: &DesktopService) -> Arc> { + let seen = Arc::new(Mutex::new(Seen { + events: Vec::new(), + running_at_finished: Vec::new(), + })); + let writer = Arc::clone(&seen); + let inner = Arc::clone(&service.inner); + let sink: ChatEventSink = Box::new(move |event| { + let mut log = writer.lock().expect("log"); + if let ChatEvent::Finished { session_id } = event { + let running = inner + .lock_turns() + .map(|turns| turns.is_running(*session_id)) + .expect("turns"); + log.running_at_finished.push(running); + } + log.events.push(event.clone()); + true + }); + service.chat_subscribe(sink).expect("subscribe"); + seen + } + + #[test] + fn completing_records_the_result_and_then_sends_finished() { + // Given a running slot and a subscriber. + let session_id = Uuid::from_u128(7); + let (_directory, service) = service_with_slot(session_id); + let seen = watch(&service); + service.inner.events_begin(session_id); + + // When the guard completes with an error result. + FinishGuard::new(&service.inner, session_id) + .complete(Err(DesktopError::internal("recorded result"))); + + // Then the slot holds that result, and it was no longer running when + // `finished` was sent. + let table = service.inner.lock_turns().expect("turns"); + match table.slot(session_id).map(|slot| &slot.state) { + Some(SlotState::Finished(result)) => { + let error = result.as_ref().as_ref().expect_err("the given error"); + assert_eq!(error.message(), "recorded result"); + } + _ => panic!("the slot is not finished"), + } + drop(table); + let log = seen.lock().expect("log"); + assert_eq!(log.events, [ChatEvent::Finished { session_id }]); + assert_eq!(log.running_at_finished, [false]); + } + + #[test] + fn dropping_an_uncompleted_guard_records_internal_and_then_sends_finished() { + // Given a running slot and a subscriber. + let session_id = Uuid::from_u128(8); + let (_directory, service) = service_with_slot(session_id); + let seen = watch(&service); + service.inner.events_begin(session_id); + + // When the guard is dropped without completing, as in an unwind. + { + let _guard = FinishGuard::new(&service.inner, session_id); + } + + // Then the slot holds `internal` and `finished` followed it. + let table = service.inner.lock_turns().expect("turns"); + match table.slot(session_id).map(|slot| &slot.state) { + Some(SlotState::Finished(result)) => { + let error = result.as_ref().as_ref().expect_err("a failure"); + assert_eq!(error.code(), "internal"); + assert_eq!(error.message(), STOPPED_UNEXPECTEDLY); + } + _ => panic!("the slot is not finished"), + } + drop(table); + let log = seen.lock().expect("log"); + assert_eq!(log.events, [ChatEvent::Finished { session_id }]); + assert_eq!(log.running_at_finished, [false]); + } +} diff --git a/crates/omgcode-app/src/desktop/wire.rs b/crates/omgcode-app/src/desktop/wire.rs new file mode 100644 index 0000000..bd6a41f --- /dev/null +++ b/crates/omgcode-app/src/desktop/wire.rs @@ -0,0 +1,703 @@ +//! Request and response shapes of the desktop command surface. +//! +//! These types are the Rust side of the wire contract that the interface +//! declares in `crates/omgcode-desktop/ui/src/ipc/types.ts`. Field names are +//! `snake_case`; an optional value that the interface types as `T | null` is +//! always present and serializes as `null` when absent. +//! +//! Requests derive `Deserialize` with `deny_unknown_fields`, so a caller cannot +//! smuggle an extra field, and no request type carries a filesystem path. +//! Responses derive `Serialize` only. Every response label that comes from +//! recorded data has already passed through core's display-safe escaping where +//! the service contract says so; transcript text, tool arguments, and search +//! snippets are returned as recorded and must be rendered as text. + +use omgcode_core::control::{ApprovalOperation, ApprovalState}; +use omgcode_core::session::{MessageRole, MessageSource, ToolOutcome}; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use super::error::DesktopError; + +/// Whether a listing or search covers active or archived sessions. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum TreeView { + /// Sessions that are not archived. + Active, + /// Archived sessions. + Archived, +} + +/// The key a session listing sorts by. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum TreeSort { + /// Time of the last complete event. + Updated, + /// Time of the session start. + Created, + /// Title, case-insensitive. + Title, +} + +/// Arguments of `session_tree`. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct TreeRequest { + /// Active or archived sessions. + pub view: TreeView, + /// The sort key. + pub sort: TreeSort, + /// Sort largest or latest first (last to first for titles). + pub descending: bool, +} + +/// Arguments of `session_search`. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct SearchRequest { + /// The text to search for. It is never search-engine syntax. + pub query: String, + /// Active or archived sessions. + pub view: TreeView, + /// Most hits to return, clamped to 1 through 200. + pub limit: u32, +} + +/// Arguments of `session_transcript`. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct TranscriptRequest { + /// The session to read. + pub session_id: Uuid, + /// Return entries with a larger sequence number; `null` starts at the top. + pub after_seq: Option, + /// Most entries to return, clamped to 1 through 200. + pub limit: u32, +} + +/// Arguments of every command that names only a session. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct SessionRequest { + /// The session to act on. + pub session_id: Uuid, +} + +/// Arguments of `session_create`. A caller names a registered workspace by +/// identifier and never supplies a path. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct SessionCreateRequest { + /// A workspace the operator registered with the trusted CLI. + pub workspace_id: Uuid, + /// The new session's title; core validates it. + pub title: String, +} + +/// Chat settings as the interface sends and receives them. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(deny_unknown_fields)] +pub struct ChatSettingsDto { + /// Must equal the endpoint the service was opened with. + pub endpoint: String, + /// The model identifier available on the selected server. + pub model: String, + /// Validated context capacity of the loaded model. + pub context_tokens: u64, + /// Output reserve for each model request. + pub max_output_tokens: u32, + /// User-level preferences, sent as untrusted-origin custom instructions. + pub instructions: String, + /// Compact before a turn when the estimate reaches this value. + pub compact_threshold: u64, + /// Whether to compact at the effective trigger before a turn. + pub auto_compact: bool, +} + +/// Arguments of `chat_send`. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct ChatSendRequest { + /// The session to chat in. + pub session_id: Uuid, + /// The user's message. + pub message: String, + /// Model and budgets for this turn. + pub settings: ChatSettingsDto, +} + +/// Arguments of `approval_detail`. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct ApprovalRequest { + /// The approval record to review. + pub id: Uuid, +} + +/// The operator's decision. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum Decision { + /// Grant the approval. + Approve, + /// Refuse the approval. + Deny, +} + +/// Arguments of `approval_decide`. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct ApprovalDecideRequest { + /// The approval record to decide. + pub id: Uuid, + /// Approve or deny. + pub decision: Decision, + /// The operator's writers-stopped acknowledgement; used for `file_write`. + pub writers_stopped: bool, + /// The token `approval_detail` returned; required to approve. + pub review_token: Option, +} + +/// How a session is doing, for display. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SessionStatus { + /// Nothing is running and no turn is open. + Idle, + /// A chat turn is running in this service. + Running, + /// The journal records a turn that never finished. + TurnOpen, + /// Another process holds the journal. + Busy, + /// The journal failed validation. + Invalid, +} + +/// One session as the tree, search, and lifecycle commands show it. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SessionSummary { + /// The session identifier. + pub session_id: Uuid, + /// Display-safe title, or a fixed placeholder when none is known. + pub title: String, + /// RFC 3339 start time, when known. + pub created_at: Option, + /// RFC 3339 time of the last event, when known. + pub updated_at: Option, + /// The session's status. + pub status: SessionStatus, + /// Whether the session is archived. + pub archived: bool, + /// Number of complete journal events. + pub event_count: u64, + /// Stable failure code for an `invalid` journal. + pub invalid_code: Option, +} + +/// Sessions that share a recorded workspace folder. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct WorkspaceGroup { + /// An opaque stable identifier of the folder. + pub group_id: String, + /// Display-safe folder text. Never used to open anything. + pub folder_display: String, + /// The group's sessions in listing order. + pub sessions: Vec, +} + +/// The `session_tree` response. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SessionTree { + /// Groups in order of first appearance. + pub groups: Vec, + /// How many sessions the view holds before the listing limit applied. + pub indexed_sessions: u64, + /// Directory entries the last refresh skipped as links or unrecognized. + pub skipped_entries: u64, +} + +/// A run of snippet text. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SnippetPart { + /// Recorded text. + pub text: String, + /// Whether the search matched this run. + pub matched: bool, +} + +/// What kind of text a search hit matched. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SearchRoleDto { + /// A user message. + User, + /// An assistant message. + Assistant, + /// A tool result message. + Tool, + /// A compaction summary. + Summary, + /// The session title. + Title, +} + +/// One matching text. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SearchHit { + /// The session the text belongs to. + pub session: SessionSummary, + /// Display-safe folder text. + pub folder_display: String, + /// The journal sequence number, or `null` for a title. + pub seq: Option, + /// What matched. + pub role: SearchRoleDto, + /// Text around the match. + pub snippet: Vec, +} + +/// The `session_search` response. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SearchResult { + /// Hits in relevance order. + pub hits: Vec, + /// Whether more hits exist than the limit allowed. + pub truncated: bool, +} + +/// The role of a recorded message. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum MessageRoleDto { + /// A user message. + User, + /// An assistant message. + Assistant, + /// A tool result message. + Tool, +} + +impl From for MessageRoleDto { + fn from(role: MessageRole) -> Self { + match role { + MessageRole::User => Self::User, + MessageRole::Assistant => Self::Assistant, + MessageRole::Tool => Self::Tool, + } + } +} + +/// The core-observed outcome of a tool call. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ToolOutcomeDto { + /// The operation completed. + Succeeded, + /// The request failed. + Failed, + /// Policy denied the operation. + Denied, +} + +impl From for ToolOutcomeDto { + fn from(outcome: ToolOutcome) -> Self { + match outcome { + ToolOutcome::Succeeded => Self::Succeeded, + ToolOutcome::Failed => Self::Failed, + ToolOutcome::Denied => Self::Denied, + } + } +} + +/// The recorded origin label of a message, as text. +pub(super) const fn source_label(source: MessageSource) -> &'static str { + match source { + MessageSource::UserCli => "user_cli", + MessageSource::Provider => "provider", + MessageSource::CoreTool => "core_tool", + } +} + +/// The recorded data of one transcript event. All text is untrusted. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[serde(tag = "kind", rename_all = "snake_case")] +pub enum EntryKind { + /// A user, assistant, or tool message. + Message { + /// Display role. + role: MessageRoleDto, + /// Recorded origin label. + source: String, + /// Message text as recorded. + text: String, + /// Tool call identifier, only on tool messages. + call_id: Option, + }, + /// A turn began. + TurnStarted { + /// The turn identifier. + request_id: Uuid, + }, + /// A turn completed. + TurnCompleted { + /// The turn identifier. + request_id: Uuid, + }, + /// A turn failed. + TurnFailed { + /// The turn identifier. + request_id: Uuid, + /// The recorded failure code. + code: Option, + }, + /// The model asked for a tool call. + ToolRequested { + /// The turn identifier. + request_id: Uuid, + /// Provider call identifier. + call_id: String, + /// Requested tool name. + tool: String, + /// Exact arguments text as recorded. + arguments: String, + }, + /// Core recorded a tool call's outcome. + ToolCompleted { + /// The turn identifier. + request_id: Uuid, + /// Provider call identifier. + call_id: String, + /// The observed outcome. + outcome: ToolOutcomeDto, + /// Stable error code for a failed or denied call. + code: Option, + /// Size of the rendered output. + output_bytes: u64, + }, + /// The descriptive part of a request manifest. + RequestManifest { + /// The turn identifier. + request_id: Uuid, + /// The model used. + model: String, + /// The compiled base prompt's version label. + prompt_version: String, + /// Tool names advertised to the model. + tools: Vec, + }, + /// A summary replaced earlier messages. + ContextCompacted { + /// The summary text, which is untrusted model output. + summary: String, + /// How many messages it represents. + compacted_messages: u64, + /// The summarizer model. + model: String, + }, + /// The session was archived. + SessionArchived, + /// The session was restored. + SessionRestored, +} + +/// One transcript event. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct TranscriptEntry { + /// Zero-based journal sequence number. + pub seq: u64, + /// RFC 3339 event time. + pub timestamp: String, + /// The event and its data. + #[serde(flatten)] + pub kind: EntryKind, +} + +/// The `session_transcript` response. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct TranscriptPage { + /// The session, summarized from the inspection this page came from. + pub session: SessionSummary, + /// Display-safe folder text. + pub folder_display: String, + /// Entries in journal order. + pub entries: Vec, + /// The last returned sequence number when more entries remain. + pub next_after_seq: Option, + /// Whether bytes followed the last complete record when inspected. + pub incomplete_tail: bool, +} + +/// How full the model context is. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +pub struct ContextMeterDto { + /// Conservative estimate of the input tokens. + pub estimated_tokens: u64, + /// The effective compaction trigger. + pub trigger_tokens: u64, + /// The model's context capacity. + pub context_tokens: u64, + /// Whether a compaction summary is active. + pub compacted: bool, +} + +/// Whether the turn offered workspace tools. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ToolsMode { + /// Tools were offered. + Enabled, + /// A text-only turn. + Disabled, +} + +/// What a completed turn reports. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ChatOutcome { + /// The provider finish reason. + pub finish_reason: String, + /// Whether tools were offered. + pub tools_mode: ToolsMode, + /// Why a text-only turn offered no tools. + pub tools_disabled_reason: Option, + /// Model requests made; 0 for a text-only turn. + pub rounds: u64, + /// Tool calls dispatched; 0 for a text-only turn. + pub tool_calls: u64, + /// Saved tool results replaced by a withheld notice. + pub withheld_tool_results: u64, + /// The context accounting. + pub context: Option, +} + +/// The state of a session's chat turn. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ChatState { + /// The service has no record for the session. + Idle, + /// A turn is running. + Running, + /// A turn is running and cancellation was requested. + Cancelling, + /// The last turn completed. + Completed, + /// The last turn failed. + Failed, +} + +/// The `chat_*` response. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ChatStatus { + /// The session. + pub session_id: Uuid, + /// The turn state. + pub state: ChatState, + /// The outcome of a completed turn. + pub outcome: Option, + /// The failure of a failed turn. + pub error: Option, +} + +/// One pushed chat event (ADR-0049). +/// +/// Events carry provisional text only. The journal and `chat_status` hold the +/// record; `Delta` text is already token-redacted but is otherwise as the model +/// sent it, so a front end must render it as text. The `kind` member names the +/// variant. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[serde(tag = "kind", rename_all = "snake_case")] +pub enum ChatEvent { + /// A model request of the turn began. Earlier preview text is superseded. + RequestStarted { + /// The session. + session_id: Uuid, + /// The request number within the turn, counted from 1. + request: u32, + }, + /// Provisional assistant text of the current request, at most 1,024 bytes. + Delta { + /// The session. + session_id: Uuid, + /// The request number within the turn. + request: u32, + /// The text. + text: String, + }, + /// The 16 KiB preview bound was reached for this request; later text of the + /// request is not sent. + PreviewTruncated { + /// The session. + session_id: Uuid, + /// The request number within the turn. + request: u32, + }, + /// The turn left `running`. The caller reads `chat_status`. + Finished { + /// The session. + session_id: Uuid, + }, +} + +/// A workspace the operator registered, with its saved preferences. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct WorkspaceChoice { + /// The workspace identifier. + pub workspace_id: Uuid, + /// Display-safe folder text. + pub folder_display: String, + /// The model-tools preference. + pub model_tools: bool, + /// The read preference. + pub read: bool, + /// The write preference. + pub write: bool, + /// The execute preference. + pub execute: bool, +} + +/// The `workspace_list` response. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct WorkspaceList { + /// Whether the operator selected a development root. + pub development_root_set: bool, + /// Registered workspaces. + pub workspaces: Vec, +} + +/// The `approvals_summary` response. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +pub struct ApprovalsSummary { + /// How many approvals are pending. + pub pending: u64, +} + +/// What an approval covers. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ApprovalOperationDto { + /// Start a confined command. + Command, + /// Replace a workspace file. + FileWrite, + /// A Git operation with remote access. + GitPush, + /// A destination-scoped network operation. + Network, +} + +impl From for ApprovalOperationDto { + fn from(operation: ApprovalOperation) -> Self { + match operation { + ApprovalOperation::Command => Self::Command, + ApprovalOperation::FileWrite => Self::FileWrite, + ApprovalOperation::GitPush => Self::GitPush, + ApprovalOperation::Network => Self::Network, + } + } +} + +/// Where an approval stands. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ApprovalStateDto { + /// Waiting for a decision. + Pending, + /// Granted, not yet used. + Approved, + /// Refused. + Denied, + /// Used by exactly one call. + Consumed, + /// Past its lifetime. + Expired, +} + +impl From for ApprovalStateDto { + fn from(state: ApprovalState) -> Self { + match state { + ApprovalState::Pending => Self::Pending, + ApprovalState::Approved => Self::Approved, + ApprovalState::Denied => Self::Denied, + ApprovalState::Consumed => Self::Consumed, + ApprovalState::Expired => Self::Expired, + } + } +} + +/// One approval record, joined with its session's label. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ApprovalView { + /// The approval identifier. + pub id: Uuid, + /// The session that asked. + pub session_id: Uuid, + /// Display-safe session title, when the index knows the session. + pub session_title: Option, + /// Display-safe folder text, when the index knows the session. + pub folder_display: Option, + /// What the approval covers. + pub operation: ApprovalOperationDto, + /// Display-safe one-line summary. + pub summary: String, + /// Where the approval stands. + pub state: ApprovalStateDto, + /// Unix time the request was recorded. + pub created_unix: u64, + /// Unix time the approval expires. + pub expires_unix: u64, + /// SHA-256 of the exact request the approval is bound to. + pub request_sha256: String, + /// Whether the operator acknowledged that other writers are stopped. + pub writers_stopped: bool, +} + +/// The `approvals_list` response. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ApprovalList { + /// The service's current Unix time. + pub now_unix: u64, + /// Pending approvals first, then the most recent others. + pub approvals: Vec, +} + +/// One labelled, display-safe value of a reviewed request. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ApprovalField { + /// The field label. + pub label: String, + /// The display-safe value. + pub value: String, + /// Whether the value may span lines. + pub multiline: bool, +} + +/// Whether the recorded request that an approval is bound to was found. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum Binding { + /// A recorded request reproduces the approval's digest. + Exact, + /// Something of the right kind exists but nothing matches the digest. + Stale, + /// No recorded request exists to review. + NotFound, +} + +/// The `approval_detail` response. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ApprovalDetail { + /// The approval record. + pub approval: ApprovalView, + /// The review result. + pub binding: Binding, + /// The reviewed tool, only for an `exact` binding. + pub tool: Option, + /// The reviewed fields, only for an `exact` binding. + pub fields: Vec, + /// A single-use token that allows one approve, only for an `exact`, + /// pending approval. + pub review_token: Option, +} diff --git a/crates/omgcode-app/src/desktop/workspaces.rs b/crates/omgcode-app/src/desktop/workspaces.rs new file mode 100644 index 0000000..9f8d252 --- /dev/null +++ b/crates/omgcode-app/src/desktop/workspaces.rs @@ -0,0 +1,95 @@ +//! Registered workspaces and session creation. +//! +//! A new session can be created only in a workspace the operator registered +//! with the trusted CLI. The caller names it by identifier; the path comes from +//! the settings store, never from the caller. + +use super::DesktopService; +use super::error::DesktopError; +use super::sessions::refreshed_summary; +use super::state::folder_of_directory; +use super::wire::{SessionCreateRequest, SessionSummary, WorkspaceChoice, WorkspaceList}; +use crate::create_session; + +impl DesktopService { + /// List the workspaces the operator registered, with their preferences. + /// + /// Without a settings store the list is empty and `development_root_set` is + /// false. The store is opened and released inside this call. + /// + /// # Errors + /// + /// `busy` when the settings store stays held past a two-second wait, + /// `control` when it is invalid. + pub fn workspace_list(&self) -> Result { + let Some(store) = self.inner.open_control()? else { + return Ok(WorkspaceList { + development_root_set: false, + workspaces: Vec::new(), + }); + }; + let development_root_set = store.has_development_root(); + let workspaces = store + .workspaces() + .map(|workspace| { + let permissions = workspace.permissions(); + WorkspaceChoice { + workspace_id: *workspace.id().as_uuid(), + folder_display: folder_of_directory(workspace.directory_path()), + model_tools: permissions.model_tools, + read: permissions.read, + write: permissions.write, + execute: permissions.execute, + } + }) + .collect(); + drop(store); + Ok(WorkspaceList { + development_root_set, + workspaces, + }) + } + + /// Create a session in a registered workspace. + /// + /// Core validates the title. The session is created exactly as the trusted + /// CLI creates one, its journal is released, the index learns about it, and + /// its summary is returned. + /// + /// # Errors + /// + /// `unknown_workspace` when the identifier is not registered, + /// `invalid_request` for an unusable title or a workspace path this system + /// cannot represent, `busy`, `control`, or `session`. + pub fn session_create( + &self, + request: SessionCreateRequest, + ) -> Result { + let inner = &self.inner; + let directory = { + let store = inner + .open_control()? + .ok_or_else(DesktopError::unknown_workspace)?; + let found = store + .workspaces() + .find(|workspace| *workspace.id().as_uuid() == request.workspace_id) + .map(|workspace| workspace.directory_path()); + drop(store); + found + }; + let path = match directory { + None => return Err(DesktopError::unknown_workspace()), + Some(None) => { + return Err(DesktopError::invalid_request( + "The workspace path is not usable on this system.", + )); + } + Some(Some(path)) => path, + }; + let journal = create_session(&inner.data_root, &path, &request.title) + .map_err(|error| DesktopError::from_create(&error))?; + let session_id = journal.snapshot().session_id; + drop(journal); + refreshed_summary(inner, session_id) + } +} diff --git a/crates/omgcode-app/src/error.rs b/crates/omgcode-app/src/error.rs new file mode 100644 index 0000000..fb8f5b9 --- /dev/null +++ b/crates/omgcode-app/src/error.rs @@ -0,0 +1,158 @@ +//! The typed failure shared by every application service. + +use std::fmt; + +use omgcode_core::provider::ProviderError; +use omgcode_core::session::SessionError; + +/// The stable failure category behind an [`AppError`] code. +/// +/// Policy and front ends may branch on the kind. The code strings are part of +/// the trusted CLI's JSON error contract and must not change. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ErrorKind { + /// Invalid operator input or an unusable local configuration. + Configuration, + /// A session journal could not be read, written, or used in its state. + Session, + /// The local model provider failed or returned an unusable reply. + Provider, + /// The workspace settings (control store) failed. + Control, + /// Context accounting or compaction refused or failed. + Context, + /// Current workspace preferences denied a requested action. + Permission, + /// Prompt assembly rejected the bounded request. + Prompt, + /// A terminal journal event could not be made durable. + Persistence, + /// The model returned an invalid tool call. + ToolCall, + /// A bounded turn limit stopped the request. + ToolLimit, + /// Any other controlled turn failure, such as a timeout. + Turn, + /// The operator cancelled the turn. The journal records the failure + /// reason `cancelled` and no assistant message (ADR-0050). + Cancelled, + /// A selected provider credential could not be used. The message names a + /// stable cause code and never contains the secret. + Credential, +} + +impl ErrorKind { + /// The stable machine-readable code for this category. + pub const fn code(self) -> &'static str { + match self { + Self::Configuration => "configuration", + Self::Session => "session", + Self::Provider => "provider", + Self::Control => "control", + Self::Context => "context", + Self::Permission => "permission", + Self::Prompt => "prompt", + Self::Persistence => "persistence", + Self::ToolCall => "tool_call", + Self::ToolLimit => "tool_limit", + Self::Turn => "turn", + Self::Cancelled => "cancelled", + Self::Credential => "credential", + } + } +} + +/// A controlled application failure: a stable category and a safe message. +/// +/// Messages come from fixed text or from core error `Display` output. They +/// never include the configured server token, model output, or tool text. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct AppError { + kind: ErrorKind, + message: String, +} + +impl AppError { + /// Build an error. `message` must not contain secrets or untrusted text. + pub fn new(kind: ErrorKind, message: impl Into) -> Self { + Self { + kind, + message: message.into(), + } + } + + /// Map a session journal failure to the `session` category. + pub fn session(error: SessionError) -> Self { + Self::new(ErrorKind::Session, error.to_string()) + } + + /// Map a provider failure to the `provider` category. + pub fn provider(error: ProviderError) -> Self { + Self::new(ErrorKind::Provider, error.to_string()) + } + + /// The failure category. + pub const fn kind(&self) -> ErrorKind { + self.kind + } + + /// The stable machine-readable code for the category. + pub const fn code(&self) -> &'static str { + self.kind.code() + } + + /// The human-readable message. + pub fn message(&self) -> &str { + &self.message + } + + /// Consume the error and return its message. + pub fn into_message(self) -> String { + self.message + } +} + +impl fmt::Display for AppError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "{}: {}", self.code(), self.message) + } +} + +impl std::error::Error for AppError {} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_kind_keeps_its_published_code() { + let codes = [ + (ErrorKind::Configuration, "configuration"), + (ErrorKind::Session, "session"), + (ErrorKind::Provider, "provider"), + (ErrorKind::Control, "control"), + (ErrorKind::Context, "context"), + (ErrorKind::Permission, "permission"), + (ErrorKind::Prompt, "prompt"), + (ErrorKind::Persistence, "persistence"), + (ErrorKind::ToolCall, "tool_call"), + (ErrorKind::ToolLimit, "tool_limit"), + (ErrorKind::Turn, "turn"), + (ErrorKind::Cancelled, "cancelled"), + (ErrorKind::Credential, "credential"), + ]; + for (kind, code) in codes { + assert_eq!(AppError::new(kind, "m").code(), code); + } + } + + #[test] + fn provider_and_session_mappers_use_the_core_display_text() { + let provider = AppError::provider(ProviderError::Transport); + assert_eq!(provider.code(), "provider"); + assert_eq!(provider.message(), "local model transport failed"); + let session = AppError::session(SessionError::LimitReached); + assert_eq!(session.code(), "session"); + assert_eq!(session.message(), SessionError::LimitReached.to_string()); + } +} diff --git a/crates/omgcode-app/src/lib.rs b/crates/omgcode-app/src/lib.rs new file mode 100644 index 0000000..ae94d61 --- /dev/null +++ b/crates/omgcode-app/src/lib.rs @@ -0,0 +1,42 @@ +//! Front-end-neutral application services for OMGCode. +//! +//! The trusted CLI and the desktop shell both call this crate so that they run +//! exactly the same chat turn, session creation, and compaction. The crate +//! grants no authority of its own: every authorization decision (workspace +//! preferences, tool approvals, checkpointed edits, confinement) stays in +//! `omgcode-core`. It does no argument parsing, printing, or process exit, and +//! it reads the process environment only in [`read_lm_token`] (also reached +//! through [`resolve_local_connection`] with [`ConnectionSource::Environment`]), which the caller +//! invokes. +//! +//! The local endpoint and the optional server token come from one source: the command line and +//! the environment, or a selected provider profile (ADR-0048). The token is a credential, read from the environment or +//! from a selected provider profile in protected storage (ADR-0047). It is borrowed per call, +//! used to build a request header and to remove the same text from everything +//! recorded or sent, and never appears in an [`AppError`], a `Debug` string, or +//! serialized output. +#![forbid(unsafe_code)] + +mod chat; +mod context; +mod credential_source; +pub mod desktop; +mod error; +mod paths; +mod secrets; +mod sessions; +mod tool_mode; + +pub use chat::{ + ChatClients, ChatHooks, ChatSettings, ChatStreamEvent, ChatTurnInput, ChatTurnOutput, + run_chat_turn, run_chat_turn_with_clients, +}; +pub use context::{CompactionMeter, ContextMeter, compact_journal, summary_output_tokens}; +pub use credential_source::{ + ConnectionSource, LmToken, LocalConnection, TokenSource, resolve_local_connection, +}; +pub use error::{AppError, ErrorKind}; +pub use paths::data_root; +pub use secrets::{StreamRedactor, read_lm_token, redact_known_secret}; +pub use sessions::create_session; +pub use tool_mode::{MODEL_TOOLS_SUPPORTED, ToolMode, select, tool_replay_allowed}; diff --git a/crates/omgcode-app/src/paths.rs b/crates/omgcode-app/src/paths.rs new file mode 100644 index 0000000..f37c2e6 --- /dev/null +++ b/crates/omgcode-app/src/paths.rs @@ -0,0 +1,55 @@ +//! Trusted application storage location. + +use std::path::PathBuf; + +use directories::ProjectDirs; + +use crate::error::{AppError, ErrorKind}; + +/// Choose trusted application storage. +/// +/// A supplied override is returned as given; it must come from the trusted +/// operator, never from repository content. Without one, the per-user local +/// data directory for OMGCode is used. No directory is created or checked here. +/// Fails with `configuration` when the platform has no per-user data directory. +pub fn data_root(override_path: Option) -> Result { + if let Some(path) = override_path { + return Ok(path); + } + ProjectDirs::from("org", "OMG-Software", "OMGCode") + .map(|directories| directories.data_local_dir().to_owned()) + .ok_or_else(|| { + AppError::new( + ErrorKind::Configuration, + "No per-user application data directory", + ) + }) +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use super::*; + + #[test] + fn an_override_is_returned_unchanged() { + let path = PathBuf::from("trusted-override"); + assert_eq!(data_root(Some(path.clone())).expect("override"), path); + } + + #[test] + fn the_default_is_the_per_user_omgcode_directory() { + // Hosts without a home directory legitimately fail closed. + if let Ok(path) = data_root(None) { + assert!(path.is_absolute()); + // The platform convention decides the spelling: Windows keeps + // `OMG-Software\OMGCode`, while Linux XDG paths use `omgcode`. + assert!( + path.to_string_lossy() + .to_ascii_lowercase() + .contains("omgcode") + ); + } + } +} diff --git a/crates/omgcode-app/src/secrets.rs b/crates/omgcode-app/src/secrets.rs new file mode 100644 index 0000000..660c32b --- /dev/null +++ b/crates/omgcode-app/src/secrets.rs @@ -0,0 +1,312 @@ +//! Handling of the optional local server token. +//! +//! The token is a credential. It is never logged, serialized, or placed in an +//! error message; the only treatment here is removing it from untrusted text. + +use std::env; + +use crate::error::{AppError, ErrorKind}; + +/// The environment variable that carries the optional local server token. +const TOKEN_VARIABLE: &str = "OMGCODE_LM_TOKEN"; + +/// Read the trusted operator's credential input from the environment. +/// +/// This performs the process-environment read; the caller decides when. An +/// unset or empty variable gives `None`. A non-Unicode value is a +/// `configuration` error that does not include the value. +pub fn read_lm_token() -> Result, AppError> { + match env::var(TOKEN_VARIABLE) { + Ok(value) if !value.is_empty() => Ok(Some(value)), + Ok(_) | Err(env::VarError::NotPresent) => Ok(None), + Err(env::VarError::NotUnicode(_)) => Err(AppError::new( + ErrorKind::Configuration, + "Server token must be Unicode", + )), + } +} + +/// Remove the configured credential from untrusted text before retention or egress. +/// +/// If replacement would itself contain the credential, the text is discarded +/// and an empty string is returned. +pub fn redact_known_secret(text: &str, token: Option<&str>) -> String { + match token.filter(|token| !token.is_empty()) { + Some(token) => { + let result = text.replace(token, "[REDACTED]"); + if result.contains(token) { + String::new() + } else { + result + } + } + None => text.to_owned(), + } +} + +/// Text that replaces each complete occurrence of the token. +const REDACTION: &str = "[REDACTED]"; + +/// Remove the known token from text that arrives in pieces. +/// +/// A token can be split across two pieces, so the redactor withholds the +/// longest tail of its unemitted text that equals a proper prefix of the token +/// until more text arrives. Every complete occurrence is replaced with +/// `[REDACTED]` left to right, as [`redact_known_secret`] does, so for any +/// split of a text the concatenation of every [`push`](Self::push) result and +/// [`finish`](Self::finish) equals `redact_known_secret` of the whole text. +/// +/// One case differs. `redact_known_secret` discards the whole text when the +/// redacted result still contains the token (for example when `[REDACTED]` +/// contains it, or the replacement joins its neighbours into it). Already +/// emitted text cannot be discarded, so the redactor remembers the last +/// `token.len() - 1` bytes it emitted; if that tail plus the next output would +/// contain the token, it emits nothing for that output and nothing further, +/// including from `finish`. Its output therefore never contains the token. +/// +/// With no token, or an empty one, text passes through. The redactor borrows +/// the token, holds at most `token.len() - 1` bytes of withheld text, and has +/// no `Debug` output. +pub struct StreamRedactor<'a> { + token: Option<&'a str>, + /// Unemitted raw text: a proper prefix of the token, or empty. + withheld: String, + /// The last `token.len() - 1` bytes already emitted. + emitted_tail: Vec, + poisoned: bool, +} + +impl<'a> StreamRedactor<'a> { + /// Start a redactor for one model request. + pub fn new(token: Option<&'a str>) -> Self { + Self { + token: token.filter(|token| !token.is_empty()), + withheld: String::new(), + emitted_tail: Vec::new(), + poisoned: false, + } + } + + /// Accept one piece and return the text that is safe to emit now. + pub fn push(&mut self, text: &str) -> String { + if self.poisoned { + return String::new(); + } + let Some(token) = self.token else { + return text.to_owned(); + }; + self.withheld.push_str(text); + let mut output = String::new(); + let mut cursor = 0; + while let Some(found) = self + .withheld + .get(cursor..) + .and_then(|rest| rest.find(token)) + { + let start = cursor.saturating_add(found); + output.push_str(self.withheld.get(cursor..start).unwrap_or_default()); + output.push_str(REDACTION); + cursor = start.saturating_add(token.len()); + } + // No complete occurrence remains in `rest`, so only a proper prefix of + // the token at its end can still become one. + let rest = self.withheld.get(cursor..).unwrap_or_default(); + let split = rest.len().saturating_sub(prefix_suffix_len(rest, token)); + output.push_str(rest.get(..split).unwrap_or_default()); + let held = rest.get(split..).unwrap_or_default().to_owned(); + self.withheld = held; + self.release(output) + } + + /// End the text and return the withheld tail, which cannot hold a whole token. + pub fn finish(mut self) -> String { + if self.poisoned { + return String::new(); + } + let tail = std::mem::take(&mut self.withheld); + self.release(tail) + } + + /// Emit `output` unless it would complete the token with the emitted tail. + fn release(&mut self, output: String) -> String { + let Some(token) = self.token else { + return output; + }; + let mut window = std::mem::take(&mut self.emitted_tail); + window.extend_from_slice(output.as_bytes()); + if window + .windows(token.len()) + .any(|candidate| candidate == token.as_bytes()) + { + self.poisoned = true; + self.withheld.clear(); + return String::new(); + } + let keep = token.len().saturating_sub(1); + let start = window.len().saturating_sub(keep); + self.emitted_tail = window.split_off(start); + output + } +} + +/// The length of the longest suffix of `text` that is a proper prefix of `token`. +/// +/// The comparison is on bytes. A suffix that would start inside a character of +/// `text` is skipped, so the caller can cut `text` at the result. +fn prefix_suffix_len(text: &str, token: &str) -> usize { + let longest = token.len().saturating_sub(1).min(text.len()); + (1..=longest) + .rev() + .find(|length| { + let start = text.len().saturating_sub(*length); + matches!( + (text.get(start..), token.as_bytes().get(..*length)), + (Some(suffix), Some(prefix)) if suffix.as_bytes() == prefix + ) + }) + .unwrap_or(0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_known_token_is_replaced_everywhere() { + assert_eq!( + redact_known_secret("a secret-1 b secret-1", Some("secret-1")), + "a [REDACTED] b [REDACTED]" + ); + } + + #[test] + fn text_is_unchanged_without_a_token() { + assert_eq!(redact_known_secret("plain", None), "plain"); + assert_eq!(redact_known_secret("plain", Some("")), "plain"); + } + + #[test] + fn text_is_discarded_when_replacement_recreates_the_token() { + assert_eq!(redact_known_secret("xx[REDACTED]", Some("x[RED")), ""); + } + + /// Feed `pieces` through one redactor and join every result with `finish`. + fn streamed(token: &str, pieces: &[&str]) -> String { + let mut redactor = StreamRedactor::new(Some(token)); + let mut output = String::new(); + for piece in pieces { + output.push_str(&redactor.push(piece)); + } + output.push_str(&redactor.finish()); + output + } + + /// Token and text pairs: split tokens, repeats, overlaps, multi-byte text, + /// tokens inside the replacement, and text that forms the token only + /// together with the replacement. + const CASES: &[(&str, &str)] = &[ + ("secret-1", "a secret-1 b secret-1 c"), + ("secret-1", "secret-1"), + ("secret-1", "secret-1secret-1"), + ("secret-1", "no match secret- and secret-2 end sec"), + ("aab", "aaab"), + ("aab", "aaaab aab aa"), + ("aba", "ababa"), + ("aba", "aba"), + ("abab", "abababab"), + ("a", "banana"), + ( + "t\u{f6}k\u{e9}n-\u{e9}", + "a t\u{f6}k\u{e9}n-\u{e9} b t\u{f6}k t\u{f6}k\u{e9}n", + ), + ( + "\u{65e5}\u{672c}\u{8a9e}", + "\u{65e5}\u{672c}\u{65e5}\u{672c}\u{8a9e}\u{65e5}\u{672c}\u{8a9e}", + ), + ("\u{1f511}k", "x\u{1f511}k\u{1f511} \u{1f511}k"), + ("RED", "a RED b"), + ("[RED", "x[RED"), + ("ED]", "ED]"), + ("x[RED", "xx[REDACTED]"), + ("]a", "]aa"), + ("]a", "x]a b"), + ]; + + /// All two-piece and three-piece splits of `text` at character boundaries. + fn splits(text: &str) -> Vec> { + let boundaries: Vec = text + .char_indices() + .map(|(index, _)| index) + .chain([text.len()]) + .collect(); + let mut all = vec![vec![text]]; + for &first in &boundaries { + all.push(vec![&text[..first], &text[first..]]); + for &second in boundaries.iter().filter(|cut| **cut >= first) { + all.push(vec![&text[..first], &text[first..second], &text[second..]]); + } + } + all + } + + #[test] + fn every_split_never_emits_the_token_and_matches_whole_text_redaction() { + for (token, whole) in CASES { + let expected = redact_known_secret(whole, Some(token)); + // The empty result is the discard case: the redacted text still + // held the token, so already-emitted text cannot match it. + let discards = whole.replace(token, REDACTION).contains(token); + assert_eq!(expected.is_empty() && !whole.is_empty(), discards); + for pieces in splits(whole) { + let output = streamed(token, &pieces); + assert!( + !output.contains(token), + "{token:?} leaked from {pieces:?}: {output:?}" + ); + if !discards { + assert_eq!(output, expected, "{token:?} over {pieces:?}"); + } + } + } + } + + #[test] + fn a_split_token_is_withheld_until_it_is_complete() { + let mut redactor = StreamRedactor::new(Some("secret-1")); + assert_eq!(redactor.push("see sec"), "see "); + assert_eq!(redactor.push("ret-"), ""); + assert_eq!(redactor.push("1 now"), "[REDACTED] now"); + assert_eq!(redactor.finish(), ""); + + // A withheld tail that never completes is emitted at the end. + let mut redactor = StreamRedactor::new(Some("secret-1")); + assert_eq!(redactor.push("end sec"), "end "); + assert_eq!(redactor.finish(), "sec"); + } + + #[test] + fn no_token_or_an_empty_token_passes_text_through() { + for token in [None, Some("")] { + let mut redactor = StreamRedactor::new(token); + assert_eq!(redactor.push("a sec"), "a sec"); + assert_eq!(redactor.push("ret"), "ret"); + assert_eq!(redactor.finish(), ""); + } + } + + #[test] + fn output_that_would_recreate_the_token_poisons_the_redactor() { + // `[REDACTED]` contains the token, so no output is ever safe. + let mut redactor = StreamRedactor::new(Some("RED")); + assert_eq!(redactor.push("a RED b"), ""); + assert_eq!(redactor.push("more text"), ""); + assert_eq!(redactor.finish(), ""); + + // Text emitted before the join stays emitted; nothing follows it. + let mut redactor = StreamRedactor::new(Some("]a")); + assert_eq!(redactor.push("hello "), "hello "); + assert_eq!(redactor.push("]a"), "[REDACTED]"); + assert_eq!(redactor.push("a"), ""); + assert_eq!(redactor.finish(), ""); + } +} diff --git a/crates/omgcode-app/src/sessions.rs b/crates/omgcode-app/src/sessions.rs new file mode 100644 index 0000000..6929cbb --- /dev/null +++ b/crates/omgcode-app/src/sessions.rs @@ -0,0 +1,45 @@ +//! Session creation with current workspace registry metadata. + +use std::path::Path; + +use omgcode_core::control::ControlStore; +use omgcode_core::session::SessionJournal; + +use crate::error::{AppError, ErrorKind}; + +/// Create a session with current registry metadata when a trusted root is configured. +/// +/// An absent or valid unconfigured store preserves legacy session creation. Corrupt +/// control state or an invalid workspace under a configured root fails closed with +/// `control`; journal failures are `session`. The control store is opened and +/// closed inside this call and is never held while the journal is created. +pub fn create_session( + data_root: &Path, + workspace: &Path, + title: &str, +) -> Result { + let control_error = |error: omgcode_core::control::ControlError| { + AppError::new(ErrorKind::Control, error.to_string()) + }; + let registration_id = match ControlStore::open_existing(data_root).map_err(control_error)? { + Some(mut store) if store.has_development_root() => { + let registration = store + .register_workspace(workspace, store.revision()) + .map_err(control_error)?; + let id = registration.id(); + drop(store); + Some(id) + } + Some(store) => { + drop(store); + None + } + None => None, + }; + + match registration_id { + Some(id) => SessionJournal::create_with_workspace_id(data_root, workspace, title, id), + None => SessionJournal::create(data_root, workspace, title), + } + .map_err(AppError::session) +} diff --git a/crates/omgcode-app/src/tool_mode.rs b/crates/omgcode-app/src/tool_mode.rs new file mode 100644 index 0000000..90a5d77 --- /dev/null +++ b/crates/omgcode-app/src/tool_mode.rs @@ -0,0 +1,142 @@ +//! Whether a chat turn offers the model the workspace tools. +//! +//! The session journal only selects which registered workspace to consult. +//! Authority comes from the live settings preference, rechecked by the core +//! runtime before every tool call. Nothing here grants authority. + +use std::path::Path; + +use omgcode_core::control::ControlStore; +use omgcode_core::policy::WorkspaceId; +use omgcode_core::session::SessionJournal; +use uuid::Uuid; + +use crate::error::{AppError, ErrorKind}; + +/// Whether this build can serve the model read/list tools. +/// +/// The capability report and tool selection share this value, so chat never +/// offers tools the platform cannot run. +pub const MODEL_TOOLS_SUPPORTED: bool = cfg!(any(target_os = "linux", target_os = "windows")); + +/// Conservative byte allowance for the advertised tool definitions. +/// +/// The largest set (read, list, edit, and command) serializes to 2,763 bytes +/// in the adapter's wire shape; a unit test keeps this allowance above it. +pub(crate) const TOOL_SCHEMA_ESTIMATE_BYTES: usize = 4 * 1024; + +/// Whether this chat turn offers the model the workspace tools. +#[derive(Debug)] +pub enum ToolMode { + /// The selected workspace's live preference enables model tools. + Enabled { + /// The registered workspace taken from current settings. + workspace_id: WorkspaceId, + }, + /// Text-only chat, with the reason tools were not offered. + Disabled { + /// A stable reason code, such as `preference_disabled`. + reason: &'static str, + }, +} + +/// Decide the tool mode from current settings without holding the settings lock. +/// +/// An unsupported platform, an absent store, an unregistered workspace, or a +/// disabled preference gives text-only chat. Invalid settings fail closed with +/// a `control` error. A version 1 journal cannot record tool events, so it +/// stays text-only. The settings lock is released before this returns. +pub fn select(data_root: &Path, journal: &SessionJournal) -> Result { + if !MODEL_TOOLS_SUPPORTED { + return Ok(ToolMode::Disabled { + reason: "platform_unsupported", + }); + } + let Some(control) = ControlStore::open_existing(data_root) + .map_err(|error| AppError::new(ErrorKind::Control, error.to_string()))? + else { + return Ok(ToolMode::Disabled { + reason: "settings_absent", + }); + }; + // The journal's UUID only selects a registration; the ID used afterwards + // and its preference come from current settings. + let (workspace_id, enabled) = + match control.registered_workspace(journal.snapshot().workspace_id) { + None => { + return Ok(ToolMode::Disabled { + reason: "workspace_unregistered", + }); + } + Some(workspace) => (workspace.id(), workspace.permissions().model_tools), + }; + // Release the settings lock before any model request. + drop(control); + if !enabled { + return Ok(ToolMode::Disabled { + reason: "preference_disabled", + }); + } + if journal.schema_version() < 2 { + return Ok(ToolMode::Disabled { + reason: "session_schema_v1", + }); + } + Ok(ToolMode::Enabled { workspace_id }) +} + +/// Whether live settings authorize replaying saved tool results for a session: +/// the same `model_tools` and `read` rule the turn runtime applies. +/// +/// The session is named by its recorded workspace UUID and schema version, so +/// compaction can recheck while it holds the journal. Each call reads current +/// settings. Every condition that gives text-only chat gives `false`; invalid +/// settings return a `control` error. +pub fn tool_replay_allowed( + data_root: &Path, + workspace: Uuid, + schema_version: u32, +) -> Result { + if !MODEL_TOOLS_SUPPORTED || schema_version < 2 { + return Ok(false); + } + let Some(control) = ControlStore::open_existing(data_root) + .map_err(|error| AppError::new(ErrorKind::Control, error.to_string()))? + else { + return Ok(false); + }; + Ok(control + .registered_workspace(workspace) + .is_some_and(|workspace| { + let permissions = workspace.permissions(); + permissions.model_tools && permissions.read + })) +} + +#[cfg(test)] +mod tests { + use omgcode_core::turn::{command_tool_definition, edit_tool_definition, tool_definitions}; + use serde_json::{Value, json}; + + use super::TOOL_SCHEMA_ESTIMATE_BYTES; + + /// The estimate must cover the largest advertised tool set in the exact + /// OpenAI-compatible wire shape the adapter sends. + #[test] + fn the_schema_estimate_covers_every_advertised_tool_definition() { + let mut definitions = tool_definitions(); + definitions.push(edit_tool_definition()); + definitions.push(command_tool_definition()); + let wire: Vec = definitions + .iter() + .map(|definition| json!({"type": "function", "function": definition})) + .collect(); + let bytes = serde_json::to_vec(&wire).expect("serialize tool definitions"); + println!("advertised tool definitions: {} bytes", bytes.len()); + assert!( + bytes.len() <= TOOL_SCHEMA_ESTIMATE_BYTES, + "{} bytes exceed the {TOOL_SCHEMA_ESTIMATE_BYTES}-byte estimate", + bytes.len() + ); + } +} diff --git a/crates/omgcode-app/tests/chat_turn.rs b/crates/omgcode-app/tests/chat_turn.rs new file mode 100644 index 0000000..904c252 --- /dev/null +++ b/crates/omgcode-app/tests/chat_turn.rs @@ -0,0 +1,1128 @@ +//! Behaviour of the front-end-neutral chat turn against a local mock provider. +//! +//! Every test uses a private temporary data root and a loopback server bound to +//! an ephemeral port. No process is spawned on Unix; on Windows the private +//! data root is prepared with `icacls`, which holds no store handle. + +use std::cell::RefCell; +use std::fs; +use std::io::{Read, Write}; +use std::net::{TcpListener, TcpStream}; +use std::path::{Path, PathBuf}; +use std::sync::mpsc::{self, Receiver}; +use std::thread::{self, JoinHandle}; +use std::time::{Duration, Instant}; + +mod common; + +use omgcode_app::{ + ChatHooks, ChatSettings, ChatStreamEvent, ChatTurnInput, ChatTurnOutput, create_session, + run_chat_turn, +}; +use omgcode_core::control::ControlStore; +use omgcode_core::policy::PermissionSet; +use omgcode_core::session::SessionJournal; +use omgcode_core::turn::CancelToken; +use serde_json::{Value, json}; +use tempfile::TempDir; + +const MOCK_TIMEOUT: Duration = Duration::from_secs(8); +const MAX_HTTP_BYTES: usize = 1024 * 1024; +const SECRET: &str = "fixture-token-7f3a91"; + +/// Serve one scripted response per connection and optionally cancel a turn first. +struct SequenceServer { + endpoint: String, + worker: JoinHandle>, +} + +impl SequenceServer { + /// `cancel_source` delivers the token the hook captured; the server cancels + /// it after the first request arrives and before it replies. + fn start(responses: Vec, cancel_source: Option>) -> Self { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind mock server"); + listener.set_nonblocking(true).expect("bounded accept"); + let address = listener.local_addr().expect("mock address"); + let worker = thread::spawn(move || { + let mut served = 0; + for response in responses { + let Ok(mut stream) = accept(&listener) else { + break; + }; + read_body(&mut stream)?; + if served == 0 + && let Some(source) = &cancel_source + { + let token = source + .recv_timeout(MOCK_TIMEOUT) + .map_err(|error| error.to_string())?; + token.cancel(); + } + let bytes = serde_json::to_vec(&response).map_err(|error| error.to_string())?; + let head = format!( + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + bytes.len() + ); + stream + .write_all(head.as_bytes()) + .and_then(|()| stream.write_all(&bytes)) + .map_err(|error| error.to_string())?; + served += 1; + } + Ok(served) + }); + Self { + endpoint: format!("http://{address}/v1"), + worker, + } + } + + /// Wait for the server thread and return how many requests it answered. + fn finish(self) -> usize { + self.worker + .join() + .expect("mock server thread") + .expect("mock server exchange") + } +} + +fn accept(listener: &TcpListener) -> Result { + let deadline = Instant::now() + MOCK_TIMEOUT; + loop { + match listener.accept() { + Ok((stream, _)) => { + stream + .set_nonblocking(false) + .map_err(|error| error.to_string())?; + stream + .set_read_timeout(Some(MOCK_TIMEOUT)) + .map_err(|error| error.to_string())?; + return Ok(stream); + } + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { + if Instant::now() >= deadline { + return Err("no further request".to_owned()); + } + thread::sleep(Duration::from_millis(5)); + } + Err(error) => return Err(error.to_string()), + } + } +} + +fn read_body(stream: &mut TcpStream) -> Result, String> { + let mut bytes = Vec::new(); + let mut buffer = [0_u8; 4096]; + loop { + if let Some(end) = bytes.windows(4).position(|window| window == b"\r\n\r\n") { + let head = String::from_utf8_lossy(&bytes[..end]).to_ascii_lowercase(); + let length: usize = head + .lines() + .find_map(|line| line.strip_prefix("content-length:")) + .ok_or("missing content length")? + .trim() + .parse() + .map_err(|_| "invalid content length")?; + if length > MAX_HTTP_BYTES { + return Err("request too large".to_owned()); + } + while bytes.len() < end + 4 + length { + let read = stream + .read(&mut buffer) + .map_err(|error| error.to_string())?; + if read == 0 { + return Err("truncated body".to_owned()); + } + bytes.extend_from_slice(&buffer[..read]); + } + return Ok(bytes[end + 4..end + 4 + length].to_vec()); + } + if bytes.len() > MAX_HTTP_BYTES { + return Err("headers too large".to_owned()); + } + let read = stream + .read(&mut buffer) + .map_err(|error| error.to_string())?; + if read == 0 { + return Err("connection closed before headers".to_owned()); + } + bytes.extend_from_slice(&buffer[..read]); + } +} + +fn text(content: &str) -> Value { + json!({ + "choices": [{ + "index": 0, + "message": {"role": "assistant", "content": content}, + "finish_reason": "stop" + }] + }) +} + +fn tool_call(id: &str, name: &str, arguments: &Value) -> Value { + json!({ + "choices": [{ + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [{ + "id": id, + "type": "function", + "function": {"name": name, "arguments": arguments.to_string()} + }] + }, + "finish_reason": "tool_calls" + }] + }) +} + +/// Private application data and one workspace; optionally a registered +/// workspace whose `model_tools` and `read` preferences are enabled. +struct Fixture { + _temporary: TempDir, + data: PathBuf, + workspace: PathBuf, +} + +impl Fixture { + fn new(tools_enabled: bool) -> Self { + let temporary = tempfile::tempdir().expect("private fixture root"); + let data = temporary.path().join("application-data"); + let development = temporary.path().join("development"); + let workspace = development.join("project"); + fs::create_dir_all(&workspace).expect("workspace directories"); + fs::create_dir_all(&data).expect("application data"); + if tools_enabled { + #[cfg(windows)] + restrict_to_current_user(&data); + let mut control = ControlStore::open(&data).expect("private control store"); + control + .set_development_root(&development, control.revision()) + .expect("development root"); + let id = control + .register_workspace(&workspace, control.revision()) + .expect("registered workspace") + .id(); + control + .set_permissions( + id, + PermissionSet { + read: true, + write: false, + execute: false, + task_network: false, + model_tools: true, + host_services: false, + }, + control.revision(), + ) + .expect("model tool preferences"); + } + Self { + _temporary: temporary, + data, + workspace, + } + } + + fn journal(&self) -> SessionJournal { + create_session(&self.data, &self.workspace, "Fixture session").expect("session") + } +} + +/// Give `data` the private ACL the Windows control store requires of its root. +#[cfg(windows)] +fn restrict_to_current_user(data: &Path) { + let whoami = std::process::Command::new("whoami") + .args(["/user", "/fo", "csv", "/nh"]) + .output() + .expect("whoami"); + assert!(whoami.status.success(), "whoami fixture failed"); + let identity = String::from_utf8_lossy(&whoami.stdout).into_owned(); + let sid = identity + .trim() + .rsplit(',') + .next() + .expect("SID column") + .trim_matches('"') + .to_owned(); + let granted = std::process::Command::new("icacls") + .arg(data) + .args(["/inheritance:r", "/grant:r", &format!("*{sid}:(OI)(CI)F")]) + .output() + .expect("icacls"); + assert!(granted.status.success(), "icacls fixture failed"); +} + +fn settings(endpoint: &str) -> ChatSettings { + let mut settings = ChatSettings::new(endpoint, "fixture", 131_072); + settings.max_output_tokens = 64; + settings +} + +fn turn( + journal: &mut SessionJournal, + data: &Path, + settings: &ChatSettings, + token: Option<&str>, + message: &str, + hooks: &mut ChatHooks<'_>, +) -> Result { + run_chat_turn( + journal, + data, + &ChatTurnInput { + message, + settings, + token, + }, + hooks, + ) +} + +/// Close the journal, which holds an exclusive lock, and read its events. +fn close_and_read(journal: SessionJournal) -> Vec { + let path = journal.path().to_owned(); + drop(journal); + fs::read_to_string(path) + .expect("journal file") + .lines() + .map(|line| serde_json::from_str(line).expect("journal event")) + .collect() +} + +fn last_failure_code(events: &[Value]) -> Option { + events + .iter() + .rev() + .find(|event| event["type"] == "turn.failed") + .and_then(|event| event["payload"]["code"].as_str().map(str::to_owned)) +} + +fn unused_endpoint() -> String { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("reserve port"); + let port = listener.local_addr().expect("address").port(); + drop(listener); + format!("http://127.0.0.1:{port}/v1") +} + +#[test] +fn an_archived_session_is_refused_with_the_published_message() { + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + journal.archive().expect("archive"); + let error = turn( + &mut journal, + &fixture.data, + &settings(&unused_endpoint()), + None, + "hello", + &mut ChatHooks::default(), + ) + .expect_err("archived sessions cannot chat"); + assert_eq!(error.code(), "session"); + assert_eq!( + error.message(), + "Restore the archived session before chatting" + ); +} + +#[test] +fn an_incomplete_previous_turn_is_refused_with_the_published_message() { + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + journal.start_turn().expect("start turn"); + let error = turn( + &mut journal, + &fixture.data, + &settings(&unused_endpoint()), + None, + "hello", + &mut ChatHooks::default(), + ) + .expect_err("an active turn blocks chat"); + assert_eq!(error.code(), "session"); + assert_eq!( + error.message(), + "The previous turn is incomplete; inspect it, then use session interrupt to mark it failed without retrying" + ); +} + +#[test] +fn reaching_the_trigger_without_auto_compact_is_a_context_error() { + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let mut chat = settings(&unused_endpoint()); + chat.compact_threshold = 1; + let error = turn( + &mut journal, + &fixture.data, + &chat, + None, + "hello", + &mut ChatHooks::default(), + ) + .expect_err("the trigger is reached"); + assert_eq!(error.code(), "context"); + let message = error.message(); + assert!( + message.starts_with("The conservative input estimate ("), + "{message}" + ); + assert!( + message.ends_with(". Use session compact or rerun chat with --auto-compact."), + "{message}" + ); + assert!( + close_and_read(journal) + .iter() + .all(|event| event["type"] != "turn.started"), + "a refused preflight must not start a turn" + ); +} + +#[test] +fn a_zero_threshold_is_a_configuration_error() { + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let mut chat = settings(&unused_endpoint()); + chat.compact_threshold = 0; + let error = turn( + &mut journal, + &fixture.data, + &chat, + None, + "hello", + &mut ChatHooks::default(), + ) + .expect_err("a zero threshold is invalid"); + assert_eq!(error.code(), "configuration"); + assert_eq!( + error.message(), + "The compaction threshold must be a positive token count" + ); +} + +#[test] +fn an_invalid_endpoint_is_a_provider_error_before_any_storage_use() { + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let error = turn( + &mut journal, + &fixture.data, + &settings("http://example.com/v1"), + None, + "hello", + &mut ChatHooks::default(), + ) + .expect_err("only loopback endpoints are accepted"); + assert_eq!(error.code(), "provider"); + assert_eq!(error.message(), "invalid local model configuration"); +} + +#[test] +fn a_transport_failure_is_recorded_and_never_mentions_the_token() { + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let error = turn( + &mut journal, + &fixture.data, + &settings(&unused_endpoint()), + Some(SECRET), + "hello", + &mut ChatHooks::default(), + ) + .expect_err("nothing is listening"); + assert_eq!(error.code(), "provider"); + assert_eq!(error.message(), "local model transport failed"); + assert!(!format!("{error:?}{error}").contains(SECRET)); + assert_eq!(journal.snapshot().active_turn, None); + let events = close_and_read(journal); + assert_eq!( + last_failure_code(&events).as_deref(), + Some("provider_error") + ); +} + +#[test] +fn a_text_only_turn_serializes_the_published_object_and_skips_the_cancel_hook() { + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let server = SequenceServer::start(vec![text(&format!("pong {SECRET}"))], None); + let mut calls = 0_usize; + let mut on_cancel_token = |_token: CancelToken| calls += 1; + let output = turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + Some(SECRET), + &format!("ping {SECRET}"), + &mut ChatHooks { + on_cancel_token: Some(&mut on_cancel_token), + on_stream: None, + }, + ) + .expect("text-only turn"); + assert_eq!(server.finish(), 1); + assert_eq!( + calls, 0, + "a request that is not streamed cannot be interrupted" + ); + + assert!(!output.tools_enabled()); + assert_eq!(output.disabled_reason(), Some("settings_absent")); + assert_eq!(output.reply(), "pong [REDACTED]"); + assert_eq!(output.finish_reason(), "stop"); + assert_eq!(output.rounds(), None); + assert_eq!(output.withheld_tool_results(), 0); + assert_eq!(output.context().estimate_scope(), "pre_turn_conservative"); + + let serialized = serde_json::to_string(&output).expect("serialize"); + assert!(!serialized.contains(SECRET)); + let value: Value = serde_json::from_str(&serialized).expect("JSON"); + assert_eq!(value["reply"]["content"], "pong [REDACTED]"); + assert_eq!(value["reply"]["finish_reason"], "stop"); + assert_eq!( + value["model_tools"], + json!({"mode": "disabled", "reason": "settings_absent", "withheld_tool_results": 0}) + ); + let mut keys: Vec<&str> = value + .as_object() + .expect("object") + .keys() + .map(String::as_str) + .collect(); + keys.sort_unstable(); + assert_eq!(keys, ["context", "model_tools", "reply"]); + // The recorded user message is the redacted one. + let events = close_and_read(journal); + assert!(!format!("{events:?}").contains(SECRET)); +} + +#[test] +fn a_tool_turn_calls_the_cancel_hook_once_and_serializes_the_published_object() { + let fixture = Fixture::new(true); + let mut journal = fixture.journal(); + let server = SequenceServer::start(vec![text("done")], None); + let mut calls = 0_usize; + let mut on_cancel_token = |token: CancelToken| { + assert!(!token.is_cancelled()); + calls += 1; + }; + let output = turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "hello", + &mut ChatHooks { + on_cancel_token: Some(&mut on_cancel_token), + on_stream: None, + }, + ) + .expect("tool turn"); + assert_eq!(server.finish(), 1); + assert_eq!(calls, 1); + + assert!(output.tools_enabled()); + assert_eq!(output.disabled_reason(), None); + assert_eq!(output.reply(), "done"); + assert_eq!(output.rounds(), Some(1)); + assert_eq!(output.tool_calls(), Some(0)); + assert_eq!(output.tool_bytes(), Some(0)); + assert_eq!(output.withheld_tool_results(), 0); + assert!(output.workspace_id().is_some()); + + let serialized = serde_json::to_string(&output).expect("serialize"); + let reply_at = serialized.find("\"reply\"").expect("reply key"); + let tools_at = serialized.find("\"model_tools\"").expect("tools key"); + let context_at = serialized.find("\"context\"").expect("context key"); + assert!(reply_at < tools_at && tools_at < context_at, "{serialized}"); + let value: Value = serde_json::from_str(&serialized).expect("JSON"); + assert_eq!(value["model_tools"]["mode"], "enabled"); + assert_eq!(value["model_tools"]["rounds"], 1); + assert_eq!(value["model_tools"]["tool_calls"], 0); + assert_eq!(value["model_tools"]["tool_bytes"], 0); + assert_eq!(value["model_tools"]["withheld_tool_results"], 0); + assert_eq!(value["reply"]["content"], "done"); +} + +#[test] +fn a_cancelled_token_ends_the_tool_turn_with_the_core_cancelled_failure() { + let fixture = Fixture::new(true); + let mut journal = fixture.journal(); + let (sender, receiver) = mpsc::channel::(); + let server = SequenceServer::start( + vec![tool_call( + "call-1", + "workspace_list_directory", + &json!({"path": "."}), + )], + Some(receiver), + ); + let mut calls = 0_usize; + let mut on_cancel_token = |token: CancelToken| { + calls += 1; + sender.send(token).expect("hand the token to the server"); + }; + let error = turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "list the files", + &mut ChatHooks { + on_cancel_token: Some(&mut on_cancel_token), + on_stream: None, + }, + ) + .expect_err("cancellation stops the turn"); + assert_eq!(server.finish(), 1); + assert_eq!(calls, 1); + assert_eq!(error.code(), "cancelled"); + assert_eq!(error.message(), "turn was cancelled"); + assert_eq!(journal.snapshot().active_turn, None); + let events = close_and_read(journal); + assert_eq!(last_failure_code(&events).as_deref(), Some("cancelled")); +} + +/// Record stream events as text: `request_started` and `text:`. +fn capture(log: &mut Vec) -> impl FnMut(ChatStreamEvent<'_>) + '_ { + move |event| { + log.push(match event { + ChatStreamEvent::RequestStarted => "request_started".to_owned(), + ChatStreamEvent::Text(text) => format!("text:{text}"), + }); + } +} + +/// The event kinds of a journal, with role and text for each recorded message. +/// Identifiers, times, and the endpoint in the manifest are left out. +fn journal_shape(events: &[Value]) -> Vec { + events + .iter() + .map(|event| { + let kind = event["type"].as_str().unwrap_or("?"); + if kind == "message.recorded" { + format!( + "{kind}|{}|{}", + event["payload"]["role"], event["payload"]["text"] + ) + } else { + kind.to_owned() + } + }) + .collect() +} + +fn request_body(recorded: &common::Recorded) -> Value { + serde_json::from_str(&recorded.body).expect("request JSON") +} + +#[test] +fn a_streamed_text_turn_delivers_pieces_and_journals_like_a_non_streaming_turn() { + // Given the same reply served once as one JSON document and once as a stream. + let plain_fixture = Fixture::new(false); + let mut plain_journal = plain_fixture.journal(); + let plain_server = SequenceServer::start(vec![text("Hello streamed world")], None); + let plain = turn( + &mut plain_journal, + &plain_fixture.data, + &settings(&plain_server.endpoint), + None, + "greet me", + &mut ChatHooks::default(), + ) + .expect("non-streaming turn"); + assert_eq!(plain_server.finish(), 1); + + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let server = common::MockServer::start(vec![common::Step::stream(common::sse_text(&[ + "Hello ", + "streamed ", + "world", + ]))]); + let mut log = Vec::new(); + + // When the streaming turn runs with a stream hook. + let output = { + let mut on_stream = capture(&mut log); + turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "greet me", + &mut ChatHooks { + on_stream: Some(&mut on_stream), + ..ChatHooks::default() + }, + ) + .expect("streamed turn") + }; + let (served, recorded) = server.finish_recorded(); + + // Then the request started once, the pieces arrived in order, and the + // reply and journal match the non-streaming turn. + assert_eq!( + log, + [ + "request_started", + "text:Hello ", + "text:streamed ", + "text:world" + ] + ); + assert_eq!(served, 1); + let request = request_body(&recorded[0]); + assert_eq!(request["stream"], true); + assert!(request.get("stream_options").is_none()); + assert_eq!( + recorded[0].header("accept").as_deref(), + Some("text/event-stream") + ); + assert_eq!(output.reply(), plain.reply()); + assert_eq!(output.finish_reason(), plain.finish_reason()); + assert_eq!( + journal_shape(&close_and_read(journal)), + journal_shape(&close_and_read(plain_journal)) + ); +} + +#[test] +fn a_token_split_across_pieces_never_reaches_events_or_the_journal() { + // Given a token split across two pieces and used again whole. + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let server = common::MockServer::start(vec![common::Step::stream(common::sse_text(&[ + "key is fixture-tok", + "en-7f3a91 and again ", + SECRET, + " end", + ]))]); + let mut log = Vec::new(); + + // When the streamed turn runs with the token configured. + let output = { + let mut on_stream = capture(&mut log); + turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + Some(SECRET), + "what is the key", + &mut ChatHooks { + on_stream: Some(&mut on_stream), + ..ChatHooks::default() + }, + ) + .expect("streamed turn") + }; + server.finish(); + + // Then neither the events nor the journal hold it, and the reply is redacted. + let shown: String = log + .iter() + .filter_map(|event| event.strip_prefix("text:")) + .collect(); + assert_eq!(shown, "key is [REDACTED] and again [REDACTED] end"); + assert!(!log.join("\n").contains(SECRET)); + assert!(!log.join("\n").contains("fixture-tok")); + assert_eq!(output.reply(), "key is [REDACTED] and again [REDACTED] end"); + let events = close_and_read(journal); + assert!(!format!("{events:?}").contains(SECRET)); + assert!(format!("{events:?}").contains("[REDACTED]")); +} + +#[test] +fn a_streamed_tool_turn_starts_two_requests_and_runs_the_tool() { + // Given a streamed tool call followed by a streamed text reply. + let fixture = Fixture::new(true); + let mut journal = fixture.journal(); + let server = common::MockServer::start(vec![ + common::Step::stream(common::sse_tool_call( + "call-1", + "workspace_list_directory", + &json!({"path": ""}), + )), + common::Step::stream(common::sse_text(&["all ", "done"])), + ]); + let mut log = Vec::new(); + + // When the tool-enabled streamed turn runs. + let output = { + let mut on_stream = capture(&mut log); + turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "list the files", + &mut ChatHooks { + on_stream: Some(&mut on_stream), + ..ChatHooks::default() + }, + ) + .expect("streamed tool turn") + }; + let (served, recorded) = server.finish_recorded(); + + // Then each request announced itself, tool-call pieces were never shown, + // and the tool ran as in the non-streaming test. + assert_eq!( + log, + [ + "request_started", + "request_started", + "text:all ", + "text:done" + ] + ); + assert_eq!(served, 2); + for request in &recorded { + assert_eq!(request_body(request)["stream"], true); + } + assert_eq!(output.reply(), "all done"); + assert_eq!(output.rounds(), Some(2)); + assert_eq!(output.tool_calls(), Some(1)); +} + +#[test] +fn a_stream_that_fails_after_two_pieces_keeps_them_and_records_a_provider_failure() { + // Given a stream that ends after two pieces without `[DONE]`. + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let server = + common::MockServer::start(vec![common::Step::stream(common::sse_text_unfinished(&[ + "first-piece ", + "second-piece", + ]))]); + let mut log = Vec::new(); + + // When the streamed turn runs. + let error = { + let mut on_stream = capture(&mut log); + turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "say something", + &mut ChatHooks { + on_stream: Some(&mut on_stream), + ..ChatHooks::default() + }, + ) + .expect_err("incomplete stream fails the turn") + }; + server.finish(); + + // Then the two pieces were delivered, nothing followed, and the failure + // is the provider error recorded as `provider_error`. + assert_eq!( + log, + ["request_started", "text:first-piece ", "text:second-piece"] + ); + assert_eq!(error.code(), "provider"); + assert_eq!( + error.message(), + "local model stream ended before completion" + ); + assert_eq!(journal.snapshot().active_turn, None); + let events = close_and_read(journal); + assert_eq!( + last_failure_code(&events).as_deref(), + Some("provider_error") + ); + assert!(!format!("{events:?}").contains("second-piece")); +} + +#[test] +fn a_turn_without_a_stream_hook_sends_stream_false() { + for tools_enabled in [false, true] { + let fixture = Fixture::new(tools_enabled); + let mut journal = fixture.journal(); + let server = common::MockServer::start(vec![common::Step::reply(common::text("plain"))]); + let output = turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "hello", + &mut ChatHooks::default(), + ) + .expect("plain turn"); + let (served, recorded) = server.finish_recorded(); + assert_eq!(served, 1); + assert_eq!(output.reply(), "plain"); + let request = request_body(&recorded[0]); + assert_eq!(request["stream"], false, "tools_enabled={tools_enabled}"); + assert!(request.get("stream_options").is_none()); + assert_ne!( + recorded[0].header("accept").as_deref(), + Some("text/event-stream") + ); + } +} + +// ---- Cancelling a streamed turn (ADR-0050) ---- + +/// The slowest acceptable cancellation. The request deadline is 30 seconds. +const CANCEL_BOUND: Duration = Duration::from_secs(5); + +/// What a streamed turn did when its token was cancelled by the first piece. +struct CancelledTurn { + error: omgcode_app::AppError, + log: Vec, + handed: usize, + elapsed: Duration, + served: usize, + has_active_turn: bool, + events: Vec, +} + +/// Run a streamed turn against a server that sends one piece and holds the +/// rest of the body on a gate nobody opens until the turn has returned. The +/// stream hook cancels the turn's token when it sees the first piece. +fn cancel_after_the_first_piece(tools_enabled: bool) -> CancelledTurn { + let fixture = Fixture::new(tools_enabled); + let mut journal = fixture.journal(); + let (release, gate) = common::gate(); + let server = common::MockServer::start(vec![common::Step::stream_held( + common::sse_text_unfinished(&["one "]), + gate, + common::sse_text(&["second-piece "]), + )]); + let slot: RefCell> = RefCell::new(None); + let mut handed = 0_usize; + let mut log = Vec::new(); + let mut on_cancel_token = |token: CancelToken| { + handed += 1; + *slot.borrow_mut() = Some(token); + }; + let mut on_stream = |event: ChatStreamEvent<'_>| match event { + ChatStreamEvent::RequestStarted => log.push("request_started".to_owned()), + ChatStreamEvent::Text(text) => { + log.push(format!("text:{text}")); + if let Some(token) = slot.borrow().as_ref() { + token.cancel(); + } + } + }; + + let started = Instant::now(); + let result = turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "say something", + &mut ChatHooks { + on_cancel_token: Some(&mut on_cancel_token), + on_stream: Some(&mut on_stream), + }, + ); + let elapsed = started.elapsed(); + // Let the server finish; it never saw the rest of its body read. + drop(release); + let served = server.finish(); + let has_active_turn = journal.snapshot().active_turn.is_some(); + CancelledTurn { + error: result.expect_err("the cancelled turn fails"), + log, + handed, + elapsed, + served, + has_active_turn, + events: close_and_read(journal), + } +} + +/// Whether the journal recorded any assistant message. +fn has_assistant_message(events: &[Value]) -> bool { + events + .iter() + .any(|event| event["type"] == "message.recorded" && event["payload"]["role"] == "assistant") +} + +#[test] +fn a_streamed_text_turn_cancelled_mid_stream_is_recorded_as_cancelled() { + // Given a streamed text-only turn whose server holds the body after the + // first piece, and a stream hook that cancels on that piece. + // When the turn runs. + let ended = cancel_after_the_first_piece(false); + + // Then it fails as cancelled, promptly, and the one piece stays delivered. + assert_eq!(ended.error.code(), "cancelled"); + assert_eq!(ended.error.message(), "turn was cancelled"); + assert_eq!(ended.handed, 1); + assert_eq!(ended.log, ["request_started", "text:one "]); + assert!( + ended.elapsed < CANCEL_BOUND, + "cancellation took {:?}", + ended.elapsed + ); + + // And the journal holds a cancelled failure, no reply, and no open turn. + assert_eq!( + last_failure_code(&ended.events).as_deref(), + Some("cancelled") + ); + assert!(!has_assistant_message(&ended.events)); + assert!(!ended.has_active_turn); + assert!(!format!("{:?}", ended.events).contains("second-piece")); +} + +#[test] +fn a_streamed_tool_turn_cancelled_in_its_first_request_runs_no_tool() { + // Given a streamed tool-enabled turn held after its first piece. + // When the stream hook cancels on that piece. + let ended = cancel_after_the_first_piece(true); + + // Then the outcome is the same as on the text-only path, and no tool ran. + assert_eq!(ended.error.code(), "cancelled"); + assert_eq!(ended.error.message(), "turn was cancelled"); + assert_eq!(ended.handed, 1); + assert_eq!(ended.log, ["request_started", "text:one "]); + assert_eq!(ended.served, 1); + assert!( + ended.elapsed < CANCEL_BOUND, + "cancellation took {:?}", + ended.elapsed + ); + assert_eq!( + last_failure_code(&ended.events).as_deref(), + Some("cancelled") + ); + assert!(!has_assistant_message(&ended.events)); + assert!(!ended.has_active_turn); + assert!( + ended + .events + .iter() + .all(|event| !event["type"].as_str().unwrap_or("").starts_with("tool.")), + "no tool was requested or run" + ); +} + +#[test] +fn a_streamed_text_turn_that_is_not_cancelled_hands_over_a_live_token_and_completes() { + // Given a streamed text-only turn and a hook that keeps the token. + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let server = + common::MockServer::start(vec![common::Step::stream(common::sse_text(&["Hel", "lo"]))]); + let slot: RefCell> = RefCell::new(None); + let mut handed = 0_usize; + let mut log = Vec::new(); + let mut on_cancel_token = |token: CancelToken| { + handed += 1; + *slot.borrow_mut() = Some(token); + }; + + // When the turn runs. + let output = { + let mut on_stream = capture(&mut log); + turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "greet me", + &mut ChatHooks { + on_cancel_token: Some(&mut on_cancel_token), + on_stream: Some(&mut on_stream), + }, + ) + .expect("streamed turn") + }; + server.finish(); + + // Then one token was handed over, it was never cancelled, and the turn + // completed as it does without a hook. + assert_eq!(handed, 1); + assert!( + slot.borrow() + .as_ref() + .is_some_and(|token| !token.is_cancelled()) + ); + assert_eq!(output.reply(), "Hello"); + assert_eq!(log, ["request_started", "text:Hel", "text:lo"]); + assert_eq!(journal.snapshot().active_turn, None); +} + +#[test] +fn a_cancel_seen_only_after_the_reply_arrived_still_ends_the_turn_as_cancelled() { + // Given a streamed text-only turn whose whole reply arrives at once and + // whose hook cancels on its piece. + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let server = common::MockServer::start(vec![common::Step::stream(common::sse_text(&["done"]))]); + let slot: RefCell> = RefCell::new(None); + let mut on_cancel_token = |token: CancelToken| *slot.borrow_mut() = Some(token); + let mut on_stream = |event: ChatStreamEvent<'_>| { + if matches!(event, ChatStreamEvent::Text(_)) + && let Some(token) = slot.borrow().as_ref() + { + token.cancel(); + } + }; + + // When the turn runs. + let error = turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "say it", + &mut ChatHooks { + on_cancel_token: Some(&mut on_cancel_token), + on_stream: Some(&mut on_stream), + }, + ) + .expect_err("the cancelled turn fails"); + server.finish(); + + // Then the turn is cancelled whether the stream or the checkpoint saw the + // token, and no reply was recorded. + assert_eq!(error.code(), "cancelled"); + assert_eq!(journal.snapshot().active_turn, None); + let events = close_and_read(journal); + assert_eq!(last_failure_code(&events).as_deref(), Some("cancelled")); + assert!(!has_assistant_message(&events)); +} + +#[test] +fn a_non_streamed_text_turn_never_calls_the_cancel_hook_and_cannot_be_cancelled() { + // Given a non-streamed text-only turn and a hook that would cancel. + let fixture = Fixture::new(false); + let mut journal = fixture.journal(); + let server = common::MockServer::start(vec![common::Step::reply(common::text("plain"))]); + let mut calls = 0_usize; + let mut on_cancel_token = |token: CancelToken| { + calls += 1; + token.cancel(); + }; + + // When the turn runs. + let output = turn( + &mut journal, + &fixture.data, + &settings(&server.endpoint), + None, + "hello", + &mut ChatHooks { + on_cancel_token: Some(&mut on_cancel_token), + on_stream: None, + }, + ) + .expect("a request that is not streamed is not interrupted"); + server.finish(); + + // Then no token was created and the turn completed. + assert_eq!(calls, 0); + assert_eq!(output.reply(), "plain"); +} diff --git a/crates/omgcode-app/tests/common/credential.rs b/crates/omgcode-app/tests/common/credential.rs new file mode 100644 index 0000000..660b665 --- /dev/null +++ b/crates/omgcode-app/tests/common/credential.rs @@ -0,0 +1,117 @@ +//! A provider profile and its stored credential in the real settings store and +//! the real native credential store, for the stored-token tests. +//! +//! The native store is shared with the user's account, so a test deletes only +//! the items it created: [`Vault`] remembers each identifier it bound and +//! deletes exactly those when it drops, including after a failed assertion. + +use std::path::{Path, PathBuf}; + +use omgcode_core::control::{ + ControlStore, CredentialId, CredentialStore, ProviderProfileDeclaration, Secret, +}; + +/// The profiles and native items one test created. +pub struct Vault { + data: PathBuf, + created: Vec, +} + +impl Vault { + /// A vault over the settings store in `data`. The data root's settings + /// store must already exist. + pub fn new(data: &Path) -> Self { + Self { + data: data.to_owned(), + created: Vec::new(), + } + } + + /// Record a provider profile with no endpoint, as one recorded before + /// endpoints existed. A selected profile needs an endpoint, so most tests + /// use [`Vault::add_profile_at`]. + pub fn add_profile(&self, name: &str, kind: &str, auth: &str) { + self.add(name, kind, auth, None); + } + + /// Record a provider profile that names its endpoint. + pub fn add_profile_at(&self, name: &str, kind: &str, auth: &str, endpoint: &str) { + self.add(name, kind, auth, Some(endpoint)); + } + + fn add(&self, name: &str, kind: &str, auth: &str, endpoint: Option<&str>) { + let mut control = ControlStore::open(&self.data).expect("settings store"); + let revision = control.revision(); + control + .add_provider_profile( + &ProviderProfileDeclaration { + name: name.into(), + kind: kind.into(), + auth: auth.into(), + endpoint: endpoint.map(str::to_owned), + }, + revision, + ) + .expect("provider profile"); + } + + /// Store `bytes` as the profile's credential. A second call rotates it. + pub fn bind(&mut self, name: &str, bytes: &[u8]) -> CredentialId { + let credentials = CredentialStore::open(&self.data).expect("native credential store"); + let mut control = ControlStore::open(&self.data).expect("settings store"); + let revision = control.revision(); + let set = control + .set_provider_credential( + &credentials, + name, + &Secret::new(bytes.to_vec()).expect("secret"), + revision, + ) + .expect("bind credential"); + let id = set.binding.id().clone(); + self.created.push(id.clone()); + id + } + + /// Remove a profile and its credential, as `provider-profile-remove` does. + pub fn remove_profile(&self, name: &str) { + let mut control = ControlStore::open(&self.data).expect("settings store"); + let revision = control.revision(); + let removal = control + .remove_provider_profile(name, revision) + .expect("remove profile"); + if let Some(binding) = removal.credential { + let credentials = CredentialStore::open(&self.data).expect("native credential store"); + credentials + .delete(binding.id()) + .expect("delete the item this test created"); + } + } + + /// Delete a native item while its binding stays recorded. + pub fn delete_native(&self, id: &CredentialId) { + CredentialStore::open(&self.data) + .expect("native credential store") + .delete(id) + .expect("delete the item this test created"); + } + + /// The settings revision now. + pub fn revision(&self) -> u64 { + ControlStore::open(&self.data) + .expect("settings store") + .revision() + } +} + +impl Drop for Vault { + fn drop(&mut self) { + // Best effort, and only for items this vault created. An item a + // rotation already deleted reports `NotFound`, which is fine. + if let Ok(credentials) = CredentialStore::open(&self.data) { + for id in &self.created { + credentials.delete(id).ok(); + } + } + } +} diff --git a/crates/omgcode-app/tests/common/mod.rs b/crates/omgcode-app/tests/common/mod.rs new file mode 100644 index 0000000..cb25f60 --- /dev/null +++ b/crates/omgcode-app/tests/common/mod.rs @@ -0,0 +1,671 @@ +//! Fixtures shared by the desktop service tests: a private data root, a +//! registered development root and workspaces, a scripted loopback model +//! server, and bounded polling helpers. +//! +//! Each test crate uses a subset of these helpers, so unused-item warnings are +//! allowed for this one shared module. +#![allow(dead_code)] + +pub mod credential; + +use std::fs; +use std::io::{Read, Write}; +use std::net::{TcpListener, TcpStream}; +use std::path::PathBuf; +use std::sync::mpsc::{self, Receiver, Sender}; +use std::thread::{self, JoinHandle}; +use std::time::{Duration, Instant}; + +use omgcode_app::desktop::{ChatState, ChatStatus, DesktopConfig, DesktopService, SessionRequest}; +use omgcode_app::{AppError, TokenSource}; +use omgcode_core::control::{ControlStore, ProfileDeclaration}; +use omgcode_core::policy::PermissionSet; +use serde::Serialize; +use serde::de::DeserializeOwned; +use serde_json::{Value, json}; +use tempfile::TempDir; +use uuid::Uuid; + +/// A made-up token, used only to prove it never leaks. +pub const SECRET: &str = "fixture-token-7f3a91"; +/// How long the scripted server waits for a request or a release. +const SERVER_TIMEOUT: Duration = Duration::from_secs(30); +const MAX_HTTP_BYTES: usize = 1024 * 1024; +/// How long a test polls for a state before it fails. +pub const POLL_DEADLINE: Duration = Duration::from_secs(30); + +/// No token. +pub fn no_token() -> Result, AppError> { + Ok(None) +} + +/// The fixture token. +pub fn fixed_token() -> Result, AppError> { + Ok(Some(SECRET.to_owned())) +} + +/// Build a request or any wire type from JSON, as the shell would. +pub fn wire(value: Value) -> T { + serde_json::from_value(value).expect("wire value deserializes") +} + +/// Serialize a response to text, as the shell would. +pub fn text_of(value: &T) -> String { + serde_json::to_string(value).expect("response serializes") +} + +/// Serialize a response to a JSON value. +pub fn json_of(value: &T) -> Value { + serde_json::to_value(value).expect("response serializes") +} + +/// The request object that names only a session. +pub fn session_request(id: Uuid) -> Value { + json!({"session_id": id}) +} + +/// One scripted reply of the loopback model server. +pub struct Step { + reply: Value, + /// A raw `text/event-stream` body, sent instead of `reply` when set. + event_stream: Option, + /// For a held stream: the gate to wait on, and the rest of the body that + /// follows it. The `event_stream` text is sent first. + held: Option<(Receiver<()>, String)>, + gate: Option>, + arrived: Option>, +} + +impl Step { + /// Reply as soon as the request has been read. + pub fn reply(reply: Value) -> Self { + Self { + reply, + event_stream: None, + held: None, + gate: None, + arrived: None, + } + } + + /// Answer with a `text/event-stream` body, as a streamed request expects. + pub fn stream(body: String) -> Self { + Self { + reply: Value::Null, + event_stream: Some(body), + held: None, + gate: None, + arrived: None, + } + } + + /// Answer with a `text/event-stream` body in two parts: `first` is sent at + /// once, and `rest` only after the gate is released (or its sender dropped). + /// The response length covers both parts, so the client keeps reading. + pub fn stream_held(first: String, gate: Receiver<()>, rest: String) -> Self { + Self { + reply: Value::Null, + event_stream: Some(first), + held: Some((gate, rest)), + gate: None, + arrived: None, + } + } + + /// Wait for the gate to be released (or its sender dropped) before replying. + pub fn gated(reply: Value, gate: Receiver<()>) -> Self { + Self { + reply, + event_stream: None, + held: None, + gate: Some(gate), + arrived: None, + } + } + + /// Announce on `arrived` once the request has been read. + pub fn announcing(mut self, arrived: Sender<()>) -> Self { + self.arrived = Some(arrived); + self + } +} + +/// One request the loopback model server received: its header block (request +/// line and headers, as sent) and its body text. +#[derive(Clone, Debug)] +pub struct Recorded { + pub head: String, + pub body: String, +} + +impl Recorded { + /// The value of a header, compared by lowercase name. + pub fn header(&self, name: &str) -> Option { + let prefix = format!("{}:", name.to_ascii_lowercase()); + self.head.lines().find_map(|line| { + line.to_ascii_lowercase() + .starts_with(&prefix) + .then(|| line[prefix.len()..].trim().to_owned()) + }) + } +} + +/// A loopback model server that answers one scripted reply per connection. +pub struct MockServer { + pub endpoint: String, + worker: JoinHandle), String>>, +} + +impl MockServer { + pub fn start(steps: Vec) -> Self { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind mock server"); + listener.set_nonblocking(true).expect("bounded accept"); + let address = listener.local_addr().expect("mock address"); + let worker = thread::spawn(move || { + let mut served = 0; + let mut recorded = Vec::new(); + for step in steps { + let Ok(mut stream) = accept(&listener) else { + break; + }; + let (head, body) = read_request(&mut stream)?; + recorded.push(Recorded { + head, + body: String::from_utf8_lossy(&body).into_owned(), + }); + if let Some(arrived) = &step.arrived { + // The test may have stopped listening; that is not an error. + arrived.send(()).ok(); + } + if let Some(gate) = &step.gate { + // A release and a dropped sender both let the reply go. + gate.recv_timeout(SERVER_TIMEOUT).ok(); + } + let (content_type, bytes) = match &step.event_stream { + Some(body) => ("text/event-stream", body.clone().into_bytes()), + None => ( + "application/json", + serde_json::to_vec(&step.reply).map_err(|error| error.to_string())?, + ), + }; + let rest = step + .held + .as_ref() + .map(|(_, rest)| rest.clone().into_bytes()) + .unwrap_or_default(); + let head = format!( + "HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + bytes.len() + rest.len() + ); + // The client may have given up (a cancelled turn); that is not an error. + stream.set_nodelay(true).ok(); + stream + .write_all(head.as_bytes()) + .and_then(|()| stream.write_all(&bytes)) + .and_then(|()| stream.flush()) + .ok(); + if let Some((hold, _)) = &step.held { + // A release and a dropped sender both let the rest go. + hold.recv_timeout(SERVER_TIMEOUT).ok(); + stream.write_all(&rest).and_then(|()| stream.flush()).ok(); + } + served += 1; + } + Ok((served, recorded)) + }); + Self { + endpoint: format!("http://{address}/v1"), + worker, + } + } + + /// Wait for the server thread and return how many requests it answered. + pub fn finish(self) -> usize { + self.finish_recorded().0 + } + + /// Wait for the server thread and return how many requests it answered and + /// each request it received, in order. + pub fn finish_recorded(self) -> (usize, Vec) { + self.worker + .join() + .expect("mock server thread") + .expect("mock server exchange") + } +} + +fn accept(listener: &TcpListener) -> Result { + let deadline = Instant::now() + SERVER_TIMEOUT; + loop { + match listener.accept() { + Ok((stream, _)) => { + stream + .set_nonblocking(false) + .map_err(|error| error.to_string())?; + stream + .set_read_timeout(Some(SERVER_TIMEOUT)) + .map_err(|error| error.to_string())?; + return Ok(stream); + } + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { + if Instant::now() >= deadline { + return Err("no further request".to_owned()); + } + thread::sleep(Duration::from_millis(5)); + } + Err(error) => return Err(error.to_string()), + } + } +} + +fn read_request(stream: &mut TcpStream) -> Result<(String, Vec), String> { + let mut bytes = Vec::new(); + let mut buffer = [0_u8; 4096]; + loop { + if let Some(end) = bytes.windows(4).position(|window| window == b"\r\n\r\n") { + let head_text = String::from_utf8_lossy(&bytes[..end]).into_owned(); + let head = head_text.to_ascii_lowercase(); + let length: usize = head + .lines() + .find_map(|line| line.strip_prefix("content-length:")) + .ok_or("missing content length")? + .trim() + .parse() + .map_err(|_| "invalid content length")?; + if length > MAX_HTTP_BYTES { + return Err("request too large".to_owned()); + } + while bytes.len() < end + 4 + length { + let read = stream + .read(&mut buffer) + .map_err(|error| error.to_string())?; + if read == 0 { + return Err("truncated body".to_owned()); + } + bytes.extend_from_slice(&buffer[..read]); + } + return Ok((head_text, bytes[end + 4..end + 4 + length].to_vec())); + } + if bytes.len() > MAX_HTTP_BYTES { + return Err("headers too large".to_owned()); + } + let read = stream + .read(&mut buffer) + .map_err(|error| error.to_string())?; + if read == 0 { + return Err("connection closed before headers".to_owned()); + } + bytes.extend_from_slice(&buffer[..read]); + } +} + +/// A plain assistant reply. +pub fn text(content: &str) -> Value { + json!({ + "choices": [{ + "index": 0, + "message": {"role": "assistant", "content": content}, + "finish_reason": "stop" + }] + }) +} + +/// An assistant reply that requests one tool call. +pub fn tool_call(id: &str, name: &str, arguments: &Value) -> Value { + json!({ + "choices": [{ + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [{ + "id": id, + "type": "function", + "function": {"name": name, "arguments": arguments.to_string()} + }] + }, + "finish_reason": "tool_calls" + }] + }) +} + +/// One server-sent event holding a JSON payload. +fn event(payload: &Value) -> String { + format!("data: {payload}\n\n") +} + +/// A complete streamed text reply: one event per piece, a finish reason, and `[DONE]`. +pub fn sse_text(pieces: &[&str]) -> String { + let mut body = sse_text_unfinished(pieces); + body.push_str(&event( + &json!({"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}), + )); + body.push_str("data: [DONE]\n\n"); + body +} + +/// Streamed text pieces only: a stream that stops before its finish reason. +pub fn sse_text_unfinished(pieces: &[&str]) -> String { + pieces + .iter() + .map(|piece| { + event(&json!({"choices": [{"index": 0, "delta": {"role": "assistant", "content": piece}, "finish_reason": null}]})) + }) + .collect() +} + +/// A complete streamed reply that requests one tool call, split into pieces +/// the way LM Studio documents: the first carries the id, type and name. +pub fn sse_tool_call(id: &str, name: &str, arguments: &Value) -> String { + let arguments = arguments.to_string(); + let middle = arguments + .char_indices() + .nth(arguments.chars().count() / 2) + .map_or(arguments.len(), |(index, _)| index); + let (first, second) = arguments.split_at(middle); + let mut body = event(&json!({"choices": [{"index": 0, "delta": {"tool_calls": [{ + "index": 0, "id": id, "type": "function", + "function": {"name": name, "arguments": ""} + }]}, "finish_reason": null}]})); + for fragment in [first, second] { + body.push_str(&event( + &json!({"choices": [{"index": 0, "delta": {"tool_calls": [{ + "index": 0, "function": {"arguments": fragment} + }]}, "finish_reason": null}]}), + )); + } + body.push_str(&event( + &json!({"choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}]}), + )); + body.push_str("data: [DONE]\n\n"); + body +} + +/// A loopback listener that must never be contacted. A connection attempt +/// waits in the listen backlog, so [`NoCallServer::assert_untouched`] sees it +/// even when it arrived earlier. +pub struct NoCallServer { + pub endpoint: String, + listener: TcpListener, +} + +impl NoCallServer { + pub fn start() -> Self { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind no-call server"); + listener.set_nonblocking(true).expect("nonblocking"); + let address = listener.local_addr().expect("no-call address"); + Self { + endpoint: format!("http://{address}/v1"), + listener, + } + } + + /// Fail unless nothing connected. + pub fn assert_untouched(self) { + match self.listener.accept() { + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {} + other => panic!("the no-call server was contacted: {other:?}"), + } + } +} + +/// An endpoint nothing listens on. +pub fn unused_endpoint() -> String { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("reserve port"); + let port = listener.local_addr().expect("address").port(); + drop(listener); + format!("http://127.0.0.1:{port}/v1") +} + +/// A registered workspace. +#[derive(Clone, Debug)] +pub struct Workspace { + pub id: Uuid, + pub path: PathBuf, +} + +/// Private application data, a development root, and registered workspaces. +pub struct Fixture { + _temporary: TempDir, + pub data: PathBuf, + pub development: PathBuf, +} + +impl Fixture { + /// Application data only; no settings store exists. + pub fn bare() -> Self { + let temporary = tempfile::tempdir().expect("private fixture root"); + let data = temporary.path().join("application-data"); + let development = temporary.path().join("development"); + // The checkpoint store refuses a data root that is not private. On + // Unix that means mode 0700, which the default create mode is not. + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt; + fs::DirBuilder::new() + .mode(0o700) + .create(&data) + .expect("application data"); + } + #[cfg(not(unix))] + fs::create_dir_all(&data).expect("application data"); + fs::create_dir_all(&development).expect("development root"); + #[cfg(windows)] + restrict_to_current_user(&data); + Self { + _temporary: temporary, + data, + development, + } + } + + /// Application data with a settings store that has a development root. + pub fn new() -> Self { + let fixture = Self::bare(); + let mut control = ControlStore::open(&fixture.data).expect("private control store"); + control + .set_development_root(&fixture.development, control.revision()) + .expect("development root"); + fixture + } + + /// Register `name` below the development root with `permissions`. + pub fn workspace(&self, name: &str, permissions: PermissionSet) -> Workspace { + let path = self.development.join(name); + fs::create_dir_all(&path).expect("workspace directory"); + let mut control = ControlStore::open(&self.data).expect("control store"); + let id = control + .register_workspace(&path, control.revision()) + .expect("registered workspace") + .id(); + control + .set_permissions(id, permissions, control.revision()) + .expect("workspace permissions"); + Workspace { + id: *id.as_uuid(), + path, + } + } + + /// Declare a toolchain profile so the command tool can be offered. + pub fn declare_profile(&self) { + let tools = self.data.parent().expect("temporary root").join("tools"); + fs::create_dir_all(&tools).expect("toolchain root"); + let mut control = ControlStore::open(&self.data).expect("control store"); + control + .add_profile( + &ProfileDeclaration { + name: "system".into(), + read_only_roots: vec![tools], + path_entries: Vec::new(), + environment: Vec::new(), + }, + control.revision(), + ) + .expect("toolchain profile"); + } + + pub fn service(&self, endpoint: &str) -> DesktopService { + self.service_with(endpoint, no_token) + } + + /// A service whose turns send non-streaming requests, for a scripted server + /// that answers with plain JSON. + pub fn service_with( + &self, + endpoint: &str, + source: fn() -> Result, AppError>, + ) -> DesktopService { + self.service_streaming(endpoint, source, false) + } + + /// A service with `stream` set: when true, turns send streamed requests and + /// the scripted server must answer with an event stream. + pub fn service_streaming( + &self, + endpoint: &str, + source: fn() -> Result, AppError>, + stream: bool, + ) -> DesktopService { + DesktopService::open_with_token_source( + DesktopConfig { + data_root: self.data.clone(), + endpoint: endpoint.to_owned(), + token_source: TokenSource::Environment, + stream, + }, + source, + ) + .expect("open desktop service") + } +} + +/// Permissions that offer the read and list tools. +pub fn read_tools() -> PermissionSet { + PermissionSet { + read: true, + write: false, + model_tools: true, + ..PermissionSet::default() + } +} + +/// Permissions that offer every model tool. +pub fn all_tools() -> PermissionSet { + PermissionSet { + read: true, + write: true, + execute: true, + model_tools: true, + ..PermissionSet::default() + } +} + +/// Registered but with no model tools: chats are text-only. +pub fn text_only() -> PermissionSet { + PermissionSet { + read: false, + write: false, + ..PermissionSet::default() + } +} + +/// Settings the fixture model accepts. +pub fn settings_json(endpoint: &str) -> Value { + json!({ + "endpoint": endpoint, + "model": "fixture", + "context_tokens": 131_072, + "max_output_tokens": 64, + "instructions": "", + "compact_threshold": 200_000, + "auto_compact": false + }) +} + +/// Poll `chat_status` until `done` holds, or fail at the deadline. +pub fn wait_for( + service: &DesktopService, + session_id: Uuid, + done: impl Fn(&ChatStatus) -> bool, +) -> ChatStatus { + let deadline = Instant::now() + POLL_DEADLINE; + loop { + let status = service + .chat_status(wire::(session_request(session_id))) + .expect("chat status"); + if done(&status) { + return status; + } + assert!( + Instant::now() < deadline, + "timed out waiting; last state {:?}", + status.state + ); + thread::sleep(Duration::from_millis(10)); + } +} + +/// Wait until the turn is no longer running. +pub fn wait_finished(service: &DesktopService, session_id: Uuid) -> ChatStatus { + wait_for(service, session_id, |status| { + matches!(status.state, ChatState::Completed | ChatState::Failed) + }) +} + +/// Wait for a signal from the mock server. +pub fn wait_arrived(arrived: &Receiver<()>) { + arrived + .recv_timeout(POLL_DEADLINE) + .expect("the model request arrived"); +} + +/// A fresh gate: release by sending or dropping the sender. +pub fn gate() -> (Sender<()>, Receiver<()>) { + mpsc::channel() +} + +/// Give `data` the private ACL the Windows control store requires of its root. +#[cfg(windows)] +fn restrict_to_current_user(data: &std::path::Path) { + let whoami = std::process::Command::new("whoami") + .args(["/user", "/fo", "csv", "/nh"]) + .output() + .expect("whoami"); + assert!(whoami.status.success(), "whoami fixture failed"); + let identity = String::from_utf8_lossy(&whoami.stdout).into_owned(); + let sid = identity + .trim() + .rsplit(',') + .next() + .expect("SID column") + .trim_matches('"') + .to_owned(); + let granted = std::process::Command::new("icacls") + .arg(data) + .args(["/inheritance:r", "/grant:r", &format!("*{sid}:(OI)(CI)F")]) + .output() + .expect("icacls"); + assert!(granted.status.success(), "icacls fixture failed"); +} + +/// Assert that no serialized response names the data root, a journal file, or +/// the server token. +pub fn assert_no_leaks(fixture: &Fixture, responses: &[String]) { + let data = fixture.data.to_string_lossy().into_owned(); + // The JSON form of the same path, with its backslashes escaped. + let escaped = serde_json::to_string(&data).expect("escape path"); + let escaped = escaped.trim_matches('"').to_owned(); + for response in responses { + assert!(!response.contains(&data), "data root in {response}"); + assert!( + !response.contains(&escaped), + "escaped data root in {response}" + ); + assert!( + !response.contains(".jsonl"), + "journal file name in {response}" + ); + assert!(!response.contains(SECRET), "token in {response}"); + } +} diff --git a/crates/omgcode-app/tests/desktop_approvals.rs b/crates/omgcode-app/tests/desktop_approvals.rs new file mode 100644 index 0000000..2f25165 --- /dev/null +++ b/crates/omgcode-app/tests/desktop_approvals.rs @@ -0,0 +1,580 @@ +//! Approval commands of the desktop service (ADR-0046) end to end: a scripted +//! model requests a gated tool call, the operator reviews and decides through +//! the service, and the identical call in a later turn consumes the approval. + +use std::fs; +use std::time::Duration; + +use omgcode_app::desktop::{ + ApprovalOperationDto, ApprovalStateDto, ApprovalView, Binding, ChatState, DesktopService, +}; +use omgcode_core::control::{ApprovalDeclaration, ApprovalOperation, ControlStore, now_unix}; +use omgcode_core::policy::WorkspaceId; +use serde_json::{Value, json}; +use uuid::Uuid; + +mod common; +use common::{ + Fixture, MockServer, Step, Workspace, all_tools, assert_no_leaks, json_of, settings_json, text, + text_of, tool_call, wait_finished, wire, +}; + +const EDIT_TOOL: &str = "workspace_edit_file"; +const COMMAND_TOOL: &str = "workspace_run_command"; +const FILE_AUDIT_NAME: &str = "file-operations-v1.jsonl"; + +struct Setup { + fixture: Fixture, + workspace: Workspace, + service: DesktopService, + server: MockServer, +} + +fn setup(steps: Vec) -> Setup { + let fixture = Fixture::new(); + fixture.declare_profile(); + let workspace = fixture.workspace("project", all_tools()); + let server = MockServer::start(steps); + let service = fixture.service(&server.endpoint); + Setup { + fixture, + workspace, + service, + server, + } +} + +fn edit_args(path: &str) -> Value { + json!({"path": path, "old_text": "bc", "new_text": "b"}) +} + +fn command_args() -> Value { + json!({"profile": "system", "program": "echo", "arguments": ["hello"]}) +} + +impl Setup { + fn session(&self, title: &str) -> Uuid { + self.service + .session_create(wire( + json!({"workspace_id": self.workspace.id, "title": title}), + )) + .expect("create session") + .session_id + } + + /// Send one message and wait for the turn to complete. + fn turn(&self, session: Uuid, message: &str) { + self.service + .chat_send(wire(json!({ + "session_id": session, + "message": message, + "settings": settings_json(&self.server.endpoint), + }))) + .expect("send"); + let done = wait_finished(&self.service, session); + assert_eq!(done.state, ChatState::Completed, "{:?}", done.error); + } + + fn list(&self) -> Vec { + self.service.approvals_list().expect("approvals").approvals + } + + fn pending(&self) -> Vec { + self.list() + .into_iter() + .filter(|view| view.state == ApprovalStateDto::Pending) + .collect() + } + + fn detail(&self, id: Uuid) -> omgcode_app::desktop::ApprovalDetail { + self.service + .approval_detail(wire(json!({"id": id}))) + .expect("detail") + } + + fn decide( + &self, + id: Uuid, + decision: &str, + writers_stopped: bool, + token: Option<&str>, + ) -> Result { + self.service.approval_decide(wire(json!({ + "id": id, + "decision": decision, + "writers_stopped": writers_stopped, + "review_token": token, + }))) + } + + fn state_of(&self, id: Uuid) -> ApprovalStateDto { + self.list() + .into_iter() + .find(|view| view.id == id) + .expect("listed approval") + .state + } + + fn audit_lines(&self) -> usize { + fs::read_to_string(self.fixture.data.join(FILE_AUDIT_NAME)) + .map(|text| text.lines().count()) + .unwrap_or(0) + } +} + +#[test] +fn an_edit_approval_is_reviewed_approved_and_consumed_by_the_identical_call() { + let setup = setup(vec![ + Step::reply(tool_call("edit-1", EDIT_TOOL, &edit_args("notes.txt"))), + Step::reply(text("waiting for approval")), + Step::reply(tool_call("edit-2", EDIT_TOOL, &edit_args("notes.txt"))), + Step::reply(text("applied")), + ]); + let notes = setup.workspace.path.join("notes.txt"); + fs::write(¬es, "alpha bcc\n").expect("workspace file"); + let session = setup.session("Edits"); + let service = &setup.service; + let mut responses = Vec::new(); + + assert_eq!(service.approvals_summary().expect("summary").pending, 0); + setup.turn(session, "fix the typo"); + let summary = service.approvals_summary().expect("summary"); + assert_eq!(summary.pending, 1); + responses.push(text_of(&summary)); + + // The list shows it with the session's title and folder. + let listed = service.approvals_list().expect("list"); + responses.push(text_of(&listed)); + assert_eq!(listed.approvals.len(), 1); + let approval = &listed.approvals[0]; + assert_eq!(approval.session_id, session); + assert_eq!(approval.session_title.as_deref(), Some("Edits")); + assert!( + approval + .folder_display + .as_deref() + .is_some_and(|folder| folder.ends_with("project")) + ); + assert_eq!(approval.operation, ApprovalOperationDto::FileWrite); + assert_eq!(approval.state, ApprovalStateDto::Pending); + assert!(!approval.writers_stopped); + assert_eq!(approval.request_sha256.len(), 64); + assert!(listed.now_unix <= approval.expires_unix); + let id = approval.id; + + // Review: exact, with the edit's fields and a token. Nothing is changed. + let detail = setup.detail(id); + responses.push(text_of(&detail)); + assert_eq!(detail.binding, Binding::Exact); + assert_eq!(detail.tool.as_deref(), Some(EDIT_TOOL)); + let field = |label: &str| { + detail + .fields + .iter() + .find(|field| field.label == label) + .unwrap_or_else(|| panic!("field {label}")) + }; + assert_eq!(field("File").value, "notes.txt"); + assert_eq!(field("Old text").value, "bc"); + assert!(field("Old text").multiline); + assert_eq!(field("New text").value, "b"); + assert_eq!(field("Size").value, "10 -> 9 bytes"); + assert_eq!(fs::read_to_string(¬es).expect("read"), "alpha bcc\n"); + let first_token = detail.review_token.clone().expect("a review token"); + + // Approve without the writers-stopped acknowledgement is refused, and the + // refused attempt consumed the token. + let refused = setup + .decide(id, "approve", false, Some(&first_token)) + .expect_err("a file write needs the acknowledgement"); + assert_eq!(refused.code(), "writers_stopped_required"); + assert_eq!(setup.state_of(id), ApprovalStateDto::Pending); + let reused = setup + .decide(id, "approve", true, Some(&first_token)) + .expect_err("the token is single use"); + assert_eq!(reused.code(), "review_expired"); + + // A fresh review gives a fresh token, and with the acknowledgement it works. + let again = setup.detail(id); + let second_token = again.review_token.expect("a fresh token"); + assert_ne!(second_token, first_token); + let approved = setup + .decide(id, "approve", true, Some(&second_token)) + .expect("approve"); + responses.push(text_of(&approved)); + assert_eq!(approved.state, ApprovalStateDto::Approved); + assert!(approved.writers_stopped); + assert_eq!(service.approvals_summary().expect("summary").pending, 0); + assert_eq!(fs::read_to_string(¬es).expect("read"), "alpha bcc\n"); + + // The model repeats the identical call; core consumes the approval and applies. + setup.turn(session, "try again"); + assert_eq!(fs::read_to_string(¬es).expect("read"), "alpha bc\n"); + assert_eq!(setup.state_of(id), ApprovalStateDto::Consumed); + responses.push(text_of(&setup.list())); + + assert_no_leaks(&setup.fixture, &responses); + assert_eq!(setup.server.finish(), 4); +} + +#[test] +fn a_command_approval_is_consumed_by_the_identical_call() { + let setup = setup(vec![ + Step::reply(tool_call("cmd-1", COMMAND_TOOL, &command_args())), + Step::reply(text("waiting")), + Step::reply(tool_call("cmd-2", COMMAND_TOOL, &command_args())), + Step::reply(text("ran")), + ]); + let session = setup.session("Commands"); + setup.turn(session, "say hello"); + let pending = setup.pending(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].operation, ApprovalOperationDto::Command); + let id = pending[0].id; + + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Exact); + assert_eq!(detail.tool.as_deref(), Some(COMMAND_TOOL)); + let value_of = |label: &str| { + detail + .fields + .iter() + .find(|field| field.label == label) + .map(|field| field.value.as_str()) + }; + assert_eq!(value_of("Profile"), Some("system")); + assert_eq!(value_of("Program"), Some("echo")); + assert_eq!(value_of("Arguments"), Some("1: hello")); + + // `writers_stopped` is ignored for a command. + let token = detail.review_token.expect("a token"); + let approved = setup + .decide(id, "approve", false, Some(&token)) + .expect("approve a command"); + assert_eq!(approved.state, ApprovalStateDto::Approved); + assert!(!approved.writers_stopped); + + setup.turn(session, "again"); + assert_eq!(setup.state_of(id), ApprovalStateDto::Consumed); + assert_eq!(setup.server.finish(), 4); +} + +/// Two sessions, each with one pending edit approval on its own file. +fn two_pending() -> (Setup, Uuid, Uuid) { + let setup = setup(vec![ + Step::reply(tool_call("edit-a", EDIT_TOOL, &edit_args("a.txt"))), + Step::reply(text("a asked")), + Step::reply(tool_call("edit-b", EDIT_TOOL, &edit_args("b.txt"))), + Step::reply(text("b asked")), + ]); + fs::write(setup.workspace.path.join("a.txt"), "abcc\n").expect("a"); + fs::write(setup.workspace.path.join("b.txt"), "bbcc\n").expect("b"); + let first = setup.session("First"); + setup.turn(first, "edit a"); + let second = setup.session("Second"); + setup.turn(second, "edit b"); + let pending = setup.pending(); + assert_eq!(pending.len(), 2); + let id_for = |session: Uuid| { + pending + .iter() + .find(|view| view.session_id == session) + .expect("approval for session") + .id + }; + let (a, b) = (id_for(first), id_for(second)); + (setup, a, b) +} + +#[test] +fn approve_needs_a_matching_unused_unexpired_review_token() { + let (setup, a, b) = two_pending(); + + // No token at all. + let none = setup + .decide(a, "approve", true, None) + .expect_err("no token"); + assert_eq!(none.code(), "review_expired"); + + // A wrong token is a mismatch, and it burns the right one. + let token = setup.detail(a).review_token.expect("token"); + let wrong = setup + .decide(a, "approve", true, Some("not-the-token")) + .expect_err("wrong token"); + assert_eq!(wrong.code(), "review_mismatch"); + let burnt = setup + .decide(a, "approve", true, Some(&token)) + .expect_err("the right token no longer works"); + assert_eq!(burnt.code(), "review_expired"); + + // A token for approval A cannot approve B, and does not disturb A's own. + let token_a = setup.detail(a).review_token.expect("token for a"); + let foreign = setup + .decide(b, "approve", true, Some(&token_a)) + .expect_err("a's token on b"); + assert_eq!(foreign.code(), "review_expired"); + let token_b = setup.detail(b).review_token.expect("token for b"); + let crossed = setup + .decide(b, "approve", true, Some(&token_a)) + .expect_err("a's token on b with b reviewed"); + assert_eq!(crossed.code(), "review_mismatch"); + assert_eq!(setup.state_of(b), ApprovalStateDto::Pending); + let still = setup + .decide(a, "approve", true, Some(&token_a)) + .expect("a's own token still works"); + assert_eq!(still.state, ApprovalStateDto::Approved); + // B's token was burnt by the crossed attempt. + let burnt_b = setup + .decide(b, "approve", true, Some(&token_b)) + .expect_err("b's token was removed by the failed attempt"); + assert_eq!(burnt_b.code(), "review_expired"); +} + +#[test] +fn deny_needs_no_token_and_a_second_decision_is_not_pending() { + let (setup, a, b) = two_pending(); + let denied = setup.decide(a, "deny", false, None).expect("deny"); + assert_eq!(denied.state, ApprovalStateDto::Denied); + + for decision in ["deny", "approve"] { + let twice = setup + .decide(a, decision, true, Some("whatever")) + .expect_err("already decided"); + assert_eq!(twice.code(), "not_pending"); + } + let unknown = setup + .decide(Uuid::new_v4(), "deny", false, None) + .expect_err("unknown approval"); + assert_eq!(unknown.code(), "not_found"); + + // The list shows pending first, then the resolved one. + let listed = setup.list(); + assert_eq!(listed[0].id, b); + assert_eq!(listed[1].id, a); + assert_eq!( + setup.service.approvals_summary().expect("summary").pending, + 1 + ); +} + +#[test] +fn reviewing_a_non_pending_approval_reads_nothing_and_returns_stale_without_a_token() { + let (setup, a, b) = two_pending(); + setup.decide(a, "deny", false, None).expect("deny"); + + let before = setup.audit_lines(); + let detail = setup.detail(a); + assert_eq!(detail.binding, Binding::Stale); + assert_eq!(detail.tool, None); + assert!(detail.fields.is_empty()); + assert_eq!(detail.review_token, None); + assert_eq!(detail.approval.state, ApprovalStateDto::Denied); + assert_eq!(setup.audit_lines(), before, "no file was read"); + + // A pending review reads exactly the target file once, through the audit. + let exact = setup.detail(b); + assert_eq!(exact.binding, Binding::Exact); + assert!( + setup.audit_lines() > before, + "the exact review read the file" + ); + + let unknown = setup + .service + .approval_detail(wire(json!({"id": Uuid::new_v4()}))) + .expect_err("unknown approval"); + assert_eq!(unknown.code(), "not_found"); +} + +#[test] +fn a_changed_target_makes_the_review_stale_and_blocks_approval() { + let (setup, a, _) = two_pending(); + fs::write(setup.workspace.path.join("a.txt"), "completely different\n").expect("change a"); + let detail = setup.detail(a); + assert_eq!(detail.binding, Binding::Stale); + assert_eq!(detail.review_token, None); + assert!(detail.fields.is_empty()); + let refused = setup + .decide(a, "approve", true, None) + .expect_err("a stale review has no token"); + assert_eq!(refused.code(), "review_expired"); +} + +#[test] +fn a_turn_running_for_the_session_makes_the_review_busy() { + let (release, hold) = common::gate(); + let (announce, arrived) = std::sync::mpsc::channel(); + let setup = setup(vec![ + Step::reply(tool_call("edit-a", EDIT_TOOL, &edit_args("a.txt"))), + Step::reply(text("asked")), + Step::gated(text("later"), hold).announcing(announce), + ]); + fs::write(setup.workspace.path.join("a.txt"), "abcc\n").expect("a"); + let session = setup.session("Busy review"); + setup.turn(session, "edit"); + let id = setup.pending()[0].id; + + setup + .service + .chat_send(wire(json!({ + "session_id": session, + "message": "again", + "settings": settings_json(&setup.server.endpoint), + }))) + .expect("send"); + common::wait_arrived(&arrived); + let busy = setup + .service + .approval_detail(wire(json!({"id": id}))) + .expect_err("the journal belongs to the running turn"); + assert_eq!(busy.code(), "busy"); + release.send(()).expect("release"); + wait_finished(&setup.service, session); +} + +#[test] +fn the_list_orders_pending_first_caps_resolved_ones_and_escapes_summaries() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", all_tools()); + let service = fixture.service("http://127.0.0.1:1/v1"); + let workspace_id: WorkspaceId = { + let control = ControlStore::open(&fixture.data).expect("control"); + control + .workspaces() + .find(|registered| *registered.id().as_uuid() == workspace.id) + .expect("registered") + .id() + }; + let session = Uuid::new_v4(); + let mut ids = Vec::new(); + { + let mut control = ControlStore::open(&fixture.data).expect("control"); + let now = now_unix(); + for number in 0..62_u32 { + let summary = if number == 61 { + "hostile \u{202e}summary".to_owned() + } else { + format!("network request {number}") + }; + let status = control + .request_approval( + &ApprovalDeclaration { + session_id: session, + workspace_id, + operation: ApprovalOperation::Network, + request_sha256: format!("{number:064x}"), + summary, + ttl_secs: 600, + }, + now, + ) + .expect("request an approval"); + ids.push(status.id); + } + // Deny all but the first and the last: 60 resolved, 2 pending. + for id in &ids[1..61] { + control.decide_approval(*id, false, now).expect("deny"); + } + } + + let listed = service.approvals_list().expect("list").approvals; + assert_eq!(listed.len(), 2 + 50); + assert_eq!(listed[0].id, ids[0]); + assert_eq!(listed[1].id, ids[61]); + assert_eq!( + listed[1].summary, + omgcode_core::turn::display_safe_line("hostile \u{202e}summary") + ); + assert!(listed[1].summary.contains("\\u{202e}")); + // The 50 most recent resolved ones, in store order: numbers 11 through 60. + let resolved: Vec = listed[2..].iter().map(|view| view.id).collect(); + assert_eq!(resolved, ids[11..61].to_vec()); + assert!( + listed[2..] + .iter() + .all(|view| view.state == ApprovalStateDto::Denied) + ); + // An unknown session has no labels. + assert!(listed[0].session_title.is_none() && listed[0].folder_display.is_none()); + assert_eq!(listed[0].operation, ApprovalOperationDto::Network); + assert_eq!(service.approvals_summary().expect("summary").pending, 2); + + // A network record has no reviewable tool: its review is `not_found`, not an error. + let detail = service + .approval_detail(wire(json!({"id": ids[0]}))) + .expect("detail"); + assert_eq!(detail.binding, Binding::NotFound); + assert!(detail.review_token.is_none()); +} + +#[test] +fn the_summary_serves_its_last_count_while_the_store_is_held_and_the_list_reports_busy() { + let fixture = Fixture::new(); + let service = fixture.service("http://127.0.0.1:1/v1"); + // No approvals yet; remember the count of zero, then add one and read it. + assert_eq!(service.approvals_summary().expect("summary").pending, 0); + let workspace = fixture.workspace("project", all_tools()); + { + let mut control = ControlStore::open(&fixture.data).expect("control"); + let id = control + .workspaces() + .find(|registered| *registered.id().as_uuid() == workspace.id) + .expect("registered") + .id(); + control + .request_approval( + &ApprovalDeclaration { + session_id: Uuid::new_v4(), + workspace_id: id, + operation: ApprovalOperation::Network, + request_sha256: "a".repeat(64), + summary: "one".to_owned(), + ttl_secs: 600, + }, + now_unix(), + ) + .expect("request"); + } + assert_eq!(service.approvals_summary().expect("summary").pending, 1); + + let held = ControlStore::open(&fixture.data).expect("hold the settings store"); + let begun = std::time::Instant::now(); + let cached = service + .approvals_summary() + .expect("a busy store is not an error"); + assert_eq!(cached.pending, 1, "the last count read is served"); + assert!( + begun.elapsed() >= Duration::from_millis(1500), + "bounded retry" + ); + let busy = service + .approvals_list() + .expect_err("the list needs the store"); + assert_eq!(busy.code(), "busy"); + let busy = service + .workspace_list() + .expect_err("the workspace list needs the store"); + assert_eq!(busy.code(), "busy"); + drop(held); + assert_eq!(service.approvals_summary().expect("summary").pending, 1); +} + +#[test] +fn without_a_settings_store_there_are_no_approvals() { + let fixture = Fixture::bare(); + let service = fixture.service("http://127.0.0.1:1/v1"); + assert_eq!(service.approvals_summary().expect("summary").pending, 0); + let listed = service.approvals_list().expect("list"); + assert!(listed.approvals.is_empty()); + let missing = service + .approval_detail(wire(json!({"id": Uuid::new_v4()}))) + .expect_err("no such approval"); + assert_eq!(missing.code(), "not_found"); + let missing = service + .approval_decide(wire(json!({ + "id": Uuid::new_v4(), "decision": "deny", "writers_stopped": false, "review_token": null + }))) + .expect_err("no such approval"); + assert_eq!(missing.code(), "not_found"); + assert!(json_of(&listed).get("now_unix").is_some()); +} diff --git a/crates/omgcode-app/tests/desktop_chat.rs b/crates/omgcode-app/tests/desktop_chat.rs new file mode 100644 index 0000000..8c9b500 --- /dev/null +++ b/crates/omgcode-app/tests/desktop_chat.rs @@ -0,0 +1,540 @@ +//! Chat commands of the desktop service end to end: a real journal, a real +//! index, and a scripted loopback model server. Every wait is bounded. + +use std::time::{Duration, Instant}; + +use omgcode_app::desktop::{ + ChatState, ChatStatus, DesktopError, DesktopService, SessionStatus, ToolsMode, +}; +use serde_json::json; +use uuid::Uuid; + +mod common; +use common::{ + Fixture, MockServer, SECRET, Step, Workspace, fixed_token, gate, json_of, read_tools, + session_request, settings_json, text, text_of, text_only, tool_call, unused_endpoint, + wait_arrived, wait_finished, wire, +}; + +fn new_session(service: &DesktopService, workspace: &Workspace, title: &str) -> Uuid { + service + .session_create(wire(json!({"workspace_id": workspace.id, "title": title}))) + .expect("create session") + .session_id +} + +fn send( + service: &DesktopService, + session: Uuid, + message: &str, + endpoint: &str, +) -> Result { + service.chat_send(wire(json!({ + "session_id": session, + "message": message, + "settings": settings_json(endpoint), + }))) +} + +fn status(service: &DesktopService, session: Uuid) -> ChatStatus { + service + .chat_status(wire(session_request(session))) + .expect("status") +} + +fn texts(service: &DesktopService, session: Uuid) -> Vec<(String, String)> { + let page = service + .session_transcript(wire( + json!({"session_id": session, "after_seq": null, "limit": 200}), + )) + .expect("transcript"); + page.entries + .iter() + .filter_map(|entry| { + let value = json_of(entry); + (value["kind"] == "message").then(|| { + ( + value["role"].as_str().unwrap_or_default().to_owned(), + value["text"].as_str().unwrap_or_default().to_owned(), + ) + }) + }) + .collect() +} + +/// A service on a fresh fixture with one workspace and its model server. +fn started(permissions: omgcode_core::policy::PermissionSet, steps: Vec) -> Started { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", permissions); + let server = MockServer::start(steps); + let service = fixture.service(&server.endpoint); + Started { + _fixture: fixture, + workspace, + server, + service, + } +} + +struct Started { + /// Keeps the private data root alive for the test. + _fixture: Fixture, + workspace: Workspace, + server: MockServer, + service: DesktopService, +} + +#[test] +fn a_text_only_turn_runs_in_the_background_and_completes_with_its_outcome() { + let started = started(text_only(), vec![Step::reply(text("pong"))]); + let service = &started.service; + let session = new_session(service, &started.workspace, "Chat"); + + let running = send(service, session, "ping", &started.server.endpoint).expect("send"); + assert_eq!(running.state, ChatState::Running); + assert_eq!(running.session_id, session); + assert!(running.outcome.is_none() && running.error.is_none()); + + let done = wait_finished(service, session); + assert_eq!(done.state, ChatState::Completed); + let outcome = done.outcome.expect("an outcome"); + assert_eq!(outcome.finish_reason, "stop"); + assert_eq!(outcome.tools_mode, ToolsMode::Disabled); + assert!(outcome.tools_disabled_reason.is_some()); + assert_eq!((outcome.rounds, outcome.tool_calls), (0, 0)); + assert_eq!(outcome.withheld_tool_results, 0); + let context = outcome.context.expect("a context meter"); + assert_eq!(context.context_tokens, 131_072); + assert!(context.estimated_tokens > 0 && context.trigger_tokens > 0); + assert!(!context.compacted); + assert!(done.error.is_none()); + // Reading the result again returns the same one until the next send. + assert_eq!(status(service, session).state, ChatState::Completed); + + assert_eq!( + texts(service, session), + [ + ("user".to_owned(), "ping".to_owned()), + ("assistant".to_owned(), "pong".to_owned()) + ] + ); + let tree = service + .session_tree(wire( + json!({"view": "active", "sort": "updated", "descending": true}), + )) + .expect("tree"); + let summary = &tree.groups[0].sessions[0]; + assert_eq!(summary.status, SessionStatus::Idle); + assert!(summary.event_count > 1, "the index saw the finished turn"); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn a_tool_turn_reports_rounds_and_tool_calls() { + let started = started( + read_tools(), + vec![ + Step::reply(tool_call( + "call-1", + "workspace_list_directory", + &json!({"path": ""}), + )), + Step::reply(text("listed")), + ], + ); + let service = &started.service; + std::fs::write( + started.workspace.path.join("notes.txt"), + "alpha +", + ) + .expect("workspace file"); + let session = new_session(service, &started.workspace, "Tools"); + send(service, session, "list", &started.server.endpoint).expect("send"); + let done = wait_finished(service, session); + let outcome = done + .outcome + .clone() + .unwrap_or_else(|| panic!("failed: {:?}", done.error)); + assert_eq!(outcome.tools_mode, ToolsMode::Enabled); + assert_eq!(outcome.tools_disabled_reason, None); + assert_eq!((outcome.rounds, outcome.tool_calls), (2, 1)); + assert_eq!(started.server.finish(), 2); +} + +#[test] +fn concurrent_turns_are_limited_per_session_and_in_total_and_a_finished_session_can_chat_again() { + let (release_one, gate_one) = gate(); + let (release_two, gate_two) = gate(); + let (announce, arrived) = std::sync::mpsc::channel(); + let started = started( + text_only(), + vec![ + Step::gated(text("one"), gate_one).announcing(announce), + Step::gated(text("two"), gate_two), + Step::reply(text("three")), + ], + ); + let service = &started.service; + let endpoint = started.server.endpoint.clone(); + let first = new_session(service, &started.workspace, "First"); + let second = new_session(service, &started.workspace, "Second"); + let third = new_session(service, &started.workspace, "Third"); + + send(service, first, "a", &endpoint).expect("first turn"); + wait_arrived(&arrived); + send(service, second, "b", &endpoint).expect("second turn"); + + // The same session again, and a third session beyond the limit of two. + let again = send(service, first, "again", &endpoint).expect_err("one turn per session"); + assert_eq!(again.code(), "turn_running"); + let over = send(service, third, "c", &endpoint).expect_err("two turns at most"); + assert_eq!(over.code(), "too_many_turns"); + assert_eq!(status(service, third).state, ChatState::Idle); + + // While running: the tree says so, and journal access is refused. + let tree = service + .session_tree(wire( + json!({"view": "active", "sort": "title", "descending": false}), + )) + .expect("tree"); + let state_of = |id: Uuid| { + tree.groups + .iter() + .flat_map(|group| &group.sessions) + .find(|session| session.session_id == id) + .map(|session| session.status) + .expect("listed") + }; + assert_eq!(state_of(first), SessionStatus::Running); + assert_eq!(state_of(second), SessionStatus::Running); + assert_eq!(state_of(third), SessionStatus::Idle); + let busy = service + .session_transcript(wire( + json!({"session_id": first, "after_seq": null, "limit": 5}), + )) + .expect_err("no inspection while a turn runs here"); + assert_eq!(busy.code(), "busy"); + for refused in [ + service.session_archive(wire(session_request(first))), + service.session_restore(wire(session_request(first))), + service.session_interrupt(wire(session_request(first))), + ] { + assert_eq!(refused.expect_err("running").code(), "turn_running"); + } + + release_one.send(()).expect("release the first model reply"); + release_two + .send(()) + .expect("release the second model reply"); + assert_eq!(wait_finished(service, first).state, ChatState::Completed); + assert_eq!(wait_finished(service, second).state, ChatState::Completed); + + // With the slots free, a third session can chat; a finished session can + // chat again and its old result is replaced. + send(service, third, "c", &endpoint).expect("third turn after the others finished"); + assert_eq!(wait_finished(service, third).state, ChatState::Completed); + assert_eq!(started.server.finish(), 3); +} + +#[test] +fn a_refused_send_starts_no_thread_and_records_nothing() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let endpoint = unused_endpoint(); + let service = fixture.service(&endpoint); + let session = new_session(&service, &workspace, "Refusals"); + + // A different endpoint than the one fixed at launch. + let other = send(&service, session, "hi", "http://127.0.0.1:9/v1").expect_err("endpoint"); + assert_eq!(other.code(), "invalid_request"); + assert_eq!(other.message(), "The endpoint is fixed by the application."); + + // Nothing to send, or too much. + let blank = send(&service, session, " \n\t", &endpoint).expect_err("blank"); + assert_eq!(blank.code(), "invalid_request"); + let huge = "x".repeat(65 * 1024); + let large = send(&service, session, &huge, &endpoint).expect_err("oversized"); + assert_eq!(large.code(), "invalid_request"); + + // Bad model settings are a synchronous provider error. + let mut settings = settings_json(&endpoint); + settings["model"] = json!(""); + let bad_model = service + .chat_send(wire( + json!({"session_id": session, "message": "hi", "settings": settings}), + )) + .expect_err("an empty model"); + assert_eq!(bad_model.code(), "provider"); + let mut settings = settings_json(&endpoint); + settings["context_tokens"] = json!(0); + let bad_context = service + .chat_send(wire( + json!({"session_id": session, "message": "hi", "settings": settings}), + )) + .expect_err("a zero context"); + assert_eq!(bad_context.code(), "provider"); + + // An unknown session. + let unknown = send(&service, Uuid::new_v4(), "hi", &endpoint).expect_err("unknown"); + assert_eq!(unknown.code(), "not_found"); + + // None of those started a turn or touched the journal. + assert_eq!(status(&service, session).state, ChatState::Idle); + let report = service.shutdown(Duration::from_secs(5)); + assert_eq!((report.finished, report.still_running), (0, 0)); + assert!(texts(&service, session).is_empty()); +} + +#[test] +fn an_archived_session_and_an_open_turn_refuse_a_send() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let endpoint = unused_endpoint(); + let service = fixture.service(&endpoint); + + let archived = new_session(&service, &workspace, "Archived"); + service + .session_archive(wire(session_request(archived))) + .expect("archive"); + let error = send(&service, archived, "hi", &endpoint).expect_err("archived"); + assert_eq!(error.code(), "archived"); + + let open = new_session(&service, &workspace, "Open turn"); + // Leave a turn open in the journal, as a crashed process would. + let path = omgcode_core::session::locate(&fixture.data, open) + .expect("locate") + .expect("journal"); + { + let mut journal = omgcode_core::session::SessionJournal::open(&path).expect("open"); + journal + .record_message(omgcode_core::session::MessageRole::User, "q") + .expect("message"); + journal.start_turn().expect("start turn"); + } + let error = send(&service, open, "hi", &endpoint).expect_err("open turn"); + assert_eq!(error.code(), "turn_open"); +} + +#[test] +fn a_provider_transport_failure_ends_failed_with_code_provider_and_no_token() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let endpoint = unused_endpoint(); + let service = fixture.service_with(&endpoint, fixed_token); + let session = new_session(&service, &workspace, "Unreachable"); + + send(&service, session, &format!("hello {SECRET}"), &endpoint).expect("send"); + let done = wait_finished(&service, session); + assert_eq!(done.state, ChatState::Failed); + assert!(done.outcome.is_none()); + let error = done.error.as_ref().expect("an error"); + assert_eq!(error.code(), "provider"); + let serialized = text_of(&done); + assert!(!serialized.contains(SECRET), "{serialized}"); + assert!(!format!("{done:?}").contains(SECRET)); + + // The journal recorded the failed turn and the redacted message only. + let page = service + .session_transcript(wire( + json!({"session_id": session, "after_seq": null, "limit": 50}), + )) + .expect("transcript"); + let all = text_of(&page); + assert!(!all.contains(SECRET), "the token was recorded: {all}"); + assert!(all.contains("[REDACTED]")); + assert!(all.contains("turn_failed")); +} + +#[test] +fn the_server_token_never_reaches_a_response_even_when_the_model_echoes_it() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let server = MockServer::start(vec![Step::reply(text(&format!("echo {SECRET}")))]); + let service = fixture.service_with(&server.endpoint, fixed_token); + let session = new_session(&service, &workspace, "Echo"); + send( + &service, + session, + &format!("say {SECRET}"), + &server.endpoint, + ) + .expect("send"); + let done = wait_finished(&service, session); + assert_eq!(done.state, ChatState::Completed); + + let mut responses = vec![text_of(&done)]; + responses.push(text_of( + &service + .session_transcript(wire( + json!({"session_id": session, "after_seq": null, "limit": 50}), + )) + .expect("transcript"), + )); + responses.push(text_of( + &service + .session_tree(wire( + json!({"view": "active", "sort": "updated", "descending": true}), + )) + .expect("tree"), + )); + responses.push(text_of( + &service + .session_search(wire( + json!({"query": "echo", "view": "active", "limit": 10}), + )) + .expect("search"), + )); + responses.push(text_of(&service.workspace_list().expect("workspaces"))); + responses.push(text_of(&service.approvals_list().expect("approvals"))); + responses.push(text_of(&service.chat_defaults())); + common::assert_no_leaks(&fixture, &responses); + assert!(responses[1].contains("[REDACTED]")); + assert_eq!(server.finish(), 1); +} + +#[test] +fn cancelling_a_tool_turn_reports_cancelling_then_fails_with_the_core_error() { + let (release, hold) = gate(); + let (announce, arrived) = std::sync::mpsc::channel(); + let started = started( + read_tools(), + vec![ + Step::gated( + tool_call("call-1", "workspace_list_directory", &json!({"path": "."})), + hold, + ) + .announcing(announce), + ], + ); + let service = &started.service; + let session = new_session(service, &started.workspace, "Cancel me"); + send(service, session, "list", &started.server.endpoint).expect("send"); + wait_arrived(&arrived); + + let cancelling = service + .chat_cancel(wire(session_request(session))) + .expect("cancel"); + assert_eq!(cancelling.state, ChatState::Cancelling); + assert_eq!(status(service, session).state, ChatState::Cancelling); + release.send(()).expect("release the model reply"); + + let done = wait_finished(service, session); + assert_eq!(done.state, ChatState::Failed); + let error = done.error.expect("an error"); + assert_eq!(error.code(), "cancelled"); + assert_eq!(error.message(), "turn was cancelled"); + let page = service + .session_transcript(wire( + json!({"session_id": session, "after_seq": null, "limit": 50}), + )) + .expect("transcript"); + let failure = page + .entries + .iter() + .map(json_of) + .find(|entry| entry["kind"] == "turn_failed") + .expect("a failed turn is recorded"); + assert_eq!(failure["code"], "cancelled"); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn a_cancel_request_does_not_pretend_to_stop_a_text_only_turn() { + let (release, hold) = gate(); + let (announce, arrived) = std::sync::mpsc::channel(); + let started = started( + text_only(), + vec![Step::gated(text("late answer"), hold).announcing(announce)], + ); + let service = &started.service; + let session = new_session(service, &started.workspace, "Uncancellable"); + send(service, session, "hi", &started.server.endpoint).expect("send"); + wait_arrived(&arrived); + + let after = service + .chat_cancel(wire(session_request(session))) + .expect("cancel"); + assert_eq!( + after.state, + ChatState::Running, + "no cancellation point exists" + ); + assert_eq!(status(service, session).state, ChatState::Running); + release.send(()).expect("release the model reply"); + assert_eq!(wait_finished(service, session).state, ChatState::Completed); + + // Cancelling something that is not running or unknown is idle or unchanged. + let unknown = service + .chat_cancel(wire(session_request(Uuid::new_v4()))) + .expect("cancel unknown"); + assert_eq!(unknown.state, ChatState::Idle); + assert_eq!( + service + .chat_cancel(wire(session_request(session))) + .expect("cancel finished") + .state, + ChatState::Completed + ); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn shutdown_does_not_wait_past_its_grace_and_later_reports_the_turn_finished() { + let (release, hold) = gate(); + let (announce, arrived) = std::sync::mpsc::channel(); + let started = started( + text_only(), + vec![Step::gated(text("slow"), hold).announcing(announce)], + ); + let service = &started.service; + let session = new_session(service, &started.workspace, "Slow"); + send(service, session, "hi", &started.server.endpoint).expect("send"); + wait_arrived(&arrived); + + let grace = Duration::from_millis(150); + let begun = Instant::now(); + let report = service.shutdown(grace); + let elapsed = begun.elapsed(); + assert_eq!((report.finished, report.still_running), (0, 1)); + assert!(elapsed >= grace, "waited the grace period: {elapsed:?}"); + assert!( + elapsed < grace + Duration::from_secs(2), + "did not block past the grace period: {elapsed:?}" + ); + assert_eq!(status(service, session).state, ChatState::Running); + let after = send(service, session, "more", &started.server.endpoint) + .expect_err("no new turn after shutdown"); + assert_eq!(after.code(), "shutting_down"); + + // Once the model answers, the thread ends and shutdown reports it finished. + release.send(()).expect("release the model reply"); + let report = service.shutdown(Duration::from_secs(20)); + assert_eq!((report.finished, report.still_running), (1, 0)); + assert_eq!(status(service, session).state, ChatState::Completed); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn shutdown_with_no_turns_returns_at_once() { + let fixture = Fixture::bare(); + let service = fixture.service(&unused_endpoint()); + let begun = Instant::now(); + let report = service.shutdown(Duration::from_secs(30)); + assert_eq!((report.finished, report.still_running), (0, 0)); + assert!(begun.elapsed() < Duration::from_secs(2)); +} + +#[test] +fn chat_defaults_are_the_cli_defaults_with_the_fixed_endpoint() { + let fixture = Fixture::bare(); + let endpoint = "http://127.0.0.1:1234/v1"; + let defaults = fixture.service(endpoint).chat_defaults(); + assert_eq!(defaults.endpoint, endpoint); + assert_eq!(defaults.model, ""); + assert_eq!(defaults.context_tokens, 0); + assert_eq!(defaults.max_output_tokens, 1024); + assert_eq!(defaults.instructions, ""); + assert_eq!(defaults.compact_threshold, 200_000); + assert!(!defaults.auto_compact); +} diff --git a/crates/omgcode-app/tests/desktop_events.rs b/crates/omgcode-app/tests/desktop_events.rs new file mode 100644 index 0000000..e9e1d22 --- /dev/null +++ b/crates/omgcode-app/tests/desktop_events.rs @@ -0,0 +1,684 @@ +//! Pushed chat events of the desktop service (ADR-0049) end to end: a real +//! journal and index, a scripted loopback model server that answers with +//! server-sent events, and a test sink that records what the service pushes. +//! Every wait is bounded and fails the test at its deadline. + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::{Duration, Instant}; + +use omgcode_app::AppError; +use omgcode_app::desktop::{ + ChatEvent, ChatEventSink, ChatState, ChatStatus, DesktopService, SessionStatus, ToolsMode, +}; +use omgcode_core::policy::PermissionSet; +use serde_json::{Value, json}; +use uuid::Uuid; + +mod common; +use common::{ + Fixture, MockServer, POLL_DEADLINE, SECRET, Step, Workspace, fixed_token, gate, no_token, + read_tools, session_request, settings_json, sse_text, sse_text_unfinished, sse_tool_call, text, + text_only, unused_endpoint, wire, +}; + +type Log = Arc>>; + +/// A sink that appends every event to a log and always stays subscribed. +fn recording_sink() -> (Log, ChatEventSink) { + let log: Log = Arc::default(); + let writer = Arc::clone(&log); + let sink: ChatEventSink = Box::new(move |event| { + writer.lock().expect("event log").push(event.clone()); + true + }); + (log, sink) +} + +fn subscribe(service: &DesktopService) -> Log { + let (log, sink) = recording_sink(); + service.chat_subscribe(sink).expect("subscribe"); + log +} + +fn snapshot(log: &Log) -> Vec { + log.lock().expect("event log").clone() +} + +/// Poll the log until `done` holds, or fail at the deadline. +fn wait_events(log: &Log, done: impl Fn(&[ChatEvent]) -> bool) -> Vec { + let deadline = Instant::now() + POLL_DEADLINE; + loop { + let seen = snapshot(log); + if done(&seen) { + return seen; + } + assert!( + Instant::now() < deadline, + "timed out waiting for events; saw {} events", + seen.len() + ); + thread::sleep(Duration::from_millis(5)); + } +} + +fn has_finished(events: &[ChatEvent]) -> bool { + events + .iter() + .any(|event| matches!(event, ChatEvent::Finished { .. })) +} + +fn delta_text(events: &[ChatEvent]) -> String { + events + .iter() + .filter_map(|event| match event { + ChatEvent::Delta { text, .. } => Some(text.as_str()), + _ => None, + }) + .collect() +} + +fn truncations(events: &[ChatEvent]) -> usize { + events + .iter() + .filter(|event| matches!(event, ChatEvent::PreviewTruncated { .. })) + .count() +} + +fn request_numbers(events: &[ChatEvent]) -> Vec { + events + .iter() + .filter_map(|event| match event { + ChatEvent::RequestStarted { request, .. } => Some(*request), + _ => None, + }) + .collect() +} + +struct Started { + /// Keeps the private data root alive for the test. + _fixture: Fixture, + workspace: Workspace, + server: MockServer, + service: DesktopService, +} + +fn started( + permissions: PermissionSet, + steps: Vec, + source: fn() -> Result, AppError>, + stream: bool, +) -> Started { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", permissions); + let server = MockServer::start(steps); + let service = fixture.service_streaming(&server.endpoint, source, stream); + Started { + _fixture: fixture, + workspace, + server, + service, + } +} + +fn new_session(started: &Started) -> Uuid { + started + .service + .session_create(wire( + json!({"workspace_id": started.workspace.id, "title": "Events"}), + )) + .expect("create session") + .session_id +} + +fn send(started: &Started, session: Uuid, message: &str) { + started + .service + .chat_send(wire(json!({ + "session_id": session, + "message": message, + "settings": settings_json(&started.server.endpoint), + }))) + .expect("send"); +} + +fn status(service: &DesktopService, session: Uuid) -> ChatStatus { + service + .chat_status(wire(session_request(session))) + .expect("status") +} + +#[test] +fn a_streamed_turn_pushes_started_deltas_and_finished_and_status_is_settled() { + // Given a subscriber and a model that streams three pieces. + let started = started( + text_only(), + vec![Step::stream(sse_text(&["Hel", "lo ", "there"]))], + no_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + + // When a turn is sent and its `finished` event is observed. + send(&started, session, "hi"); + let seen = wait_events(&log, has_finished); + + // Then the events are one request, the text, and the end, in that order. + assert_eq!( + seen.first(), + Some(&ChatEvent::RequestStarted { + session_id: session, + request: 1 + }) + ); + assert_eq!( + seen.last(), + Some(&ChatEvent::Finished { + session_id: session + }) + ); + let middle = &seen[1..seen.len() - 1]; + assert!(!middle.is_empty()); + assert!( + middle + .iter() + .all(|event| matches!(event, ChatEvent::Delta { request: 1, session_id, .. } if *session_id == session)) + ); + assert_eq!(delta_text(&seen), "Hello there"); + + // And the status read right after `finished` is already settled. + assert_eq!( + status(&started.service, session).state, + ChatState::Completed + ); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn a_streamed_tool_turn_announces_each_model_request() { + // Given a model that calls a tool and then answers, both streamed. + let started = started( + read_tools(), + vec![ + Step::stream(sse_tool_call( + "call-1", + "workspace_list_directory", + &json!({"path": ""}), + )), + Step::stream(sse_text(&["listed"])), + ], + no_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + + // When the turn runs. + send(&started, session, "list"); + let seen = wait_events(&log, has_finished); + + // Then there is a `request_started` for each of the two requests, and the + // text of the second follows its announcement. + assert_eq!(request_numbers(&seen), [1, 2]); + assert_eq!(delta_text(&seen), "listed"); + let second = seen + .iter() + .position(|event| matches!(event, ChatEvent::RequestStarted { request: 2, .. })) + .expect("second request announced"); + assert!(matches!( + seen.get(second + 1), + Some(ChatEvent::Delta { request: 2, .. }) + )); + let done = status(&started.service, session); + assert_eq!(done.state, ChatState::Completed); + assert_eq!( + done.outcome.expect("outcome").tools_mode, + ToolsMode::Enabled + ); + assert_eq!(started.server.finish(), 2); +} + +#[test] +fn a_token_split_across_pieces_never_reaches_a_subscriber() { + // Given a server token and a model that echoes it across two pieces. + let half = SECRET.len() / 2; + let (head, tail) = SECRET.split_at(half); + let first = format!("the key is {head}"); + let second = format!("{tail} and more"); + let started = started( + text_only(), + vec![Step::stream(sse_text(&[&first, &second]))], + fixed_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + + // When the turn runs. + send(&started, session, "say it"); + let seen = wait_events(&log, has_finished); + + // Then no event holds the token or either half joined to the other, and + // text before the token was delivered. + let wire_text = serde_json::to_string( + &seen + .iter() + .map(|event| serde_json::to_value(event).expect("event serializes")) + .collect::>(), + ) + .expect("serialize events"); + assert!(!wire_text.contains(SECRET), "{wire_text}"); + assert!(!delta_text(&seen).contains(SECRET)); + assert!(delta_text(&seen).starts_with("the key is ")); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn a_reply_over_the_preview_bound_is_cut_once_and_the_turn_ends_by_the_message_rule() { + // Given a reply of 20,000 bytes, streamed in 1,000-byte pieces. + let pieces: Vec = (0..20).map(|_| "a".repeat(1000)).collect(); + let borrowed: Vec<&str> = pieces.iter().map(String::as_str).collect(); + let started = started( + text_only(), + vec![Step::stream(sse_text(&borrowed))], + no_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + + // When the turn runs. + send(&started, session, "long"); + let seen = wait_events(&log, has_finished); + + // Then 16,384 bytes were pushed, the cut was reported once, and nothing + // for the request came after it. + assert_eq!(delta_text(&seen).len(), 16 * 1024); + assert_eq!(truncations(&seen), 1); + let cut = seen + .iter() + .position(|event| matches!(event, ChatEvent::PreviewTruncated { .. })) + .expect("truncation reported"); + assert!( + seen[cut + 1..] + .iter() + .all(|event| matches!(event, ChatEvent::Finished { .. })), + "events after the cut: {:?}", + &seen[cut + 1..] + ); + + // And the turn ends as the 16 KiB session message rule says. + let done = status(&started.service, session); + assert_eq!(done.state, ChatState::Failed); + let error = done.error.expect("an error"); + assert!(error.message().contains("16 KiB"), "{}", error.message()); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn every_delta_is_at_most_a_kilobyte_of_valid_text_for_multibyte_replies() { + // Given a reply of three- and four-byte characters, in one piece. + let reply = "\u{20ac}\u{1F600}x".repeat(1500); + let started = started( + text_only(), + vec![Step::stream(sse_text(&[&reply]))], + no_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + + // When the turn runs. + send(&started, session, "wide"); + let seen = wait_events(&log, has_finished); + + // Then each delta is within the bound and the pieces rebuild the reply. + let mut count = 0; + for event in &seen { + if let ChatEvent::Delta { text, .. } = event { + assert!(text.len() <= 1024, "{} bytes", text.len()); + count += 1; + } + } + assert!(count > 1, "the reply was not split"); + assert_eq!(delta_text(&seen), reply); + assert_eq!( + status(&started.service, session).state, + ChatState::Completed + ); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn subscribing_mid_stream_replays_the_text_so_far_and_the_rest_follows() { + // Given a model that holds its reply open after the first piece. + let (release, held) = gate(); + let started = started( + text_only(), + vec![Step::stream_held( + sse_text_unfinished(&["Hello "]), + held, + sse_text(&["world"]), + )], + no_token, + true, + ); + let session = new_session(&started); + let first = subscribe(&started.service); + send(&started, session, "go"); + wait_events(&first, |events| delta_text(events) == "Hello "); + let first_count = snapshot(&first).len(); + + // When a second subscriber arrives while the reply is still open. + let second = subscribe(&started.service); + + // Then it is brought up to date at once. + assert_eq!( + snapshot(&second), + [ + ChatEvent::RequestStarted { + session_id: session, + request: 1 + }, + ChatEvent::Delta { + session_id: session, + request: 1, + text: "Hello ".to_owned() + }, + ] + ); + + // And when the model finishes, the rest continues the same request on the + // new subscriber only. + release.send(()).expect("release the reply"); + let seen = wait_events(&second, has_finished); + assert_eq!(delta_text(&seen), "Hello world"); + assert_eq!(request_numbers(&seen), [1]); + assert_eq!(snapshot(&first).len(), first_count); + assert_eq!( + status(&started.service, session).state, + ChatState::Completed + ); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn a_second_subscriber_replaces_the_first() { + // Given two subscriptions made one after the other. + let started = started( + text_only(), + vec![Step::stream(sse_text(&["one"]))], + no_token, + true, + ); + let session = new_session(&started); + let first = subscribe(&started.service); + let second = subscribe(&started.service); + + // When a turn runs. + send(&started, session, "hi"); + let seen = wait_events(&second, has_finished); + + // Then the second sees it and the first never saw anything. + assert_eq!(delta_text(&seen), "one"); + assert!(snapshot(&first).is_empty()); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn a_sink_that_reports_it_is_gone_gets_nothing_more_and_the_turn_completes() { + // Given a sink that refuses its first event. + let started = started( + text_only(), + vec![Step::stream(sse_text(&["a", "b", "c"]))], + no_token, + true, + ); + let session = new_session(&started); + let calls = Arc::new(AtomicUsize::new(0)); + let counter = Arc::clone(&calls); + started + .service + .chat_subscribe(Box::new(move |_| { + counter.fetch_add(1, Ordering::SeqCst); + false + })) + .expect("subscribe"); + + // When the turn runs to its end. + send(&started, session, "hi"); + let done = common::wait_finished(&started.service, session); + + // Then the turn completed and the sink was called exactly once. + assert_eq!(done.state, ChatState::Completed); + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn a_failing_stream_pushes_its_text_and_then_finished_and_the_turn_fails() { + // Given a stream that ends without its `[DONE]` marker. + let started = started( + text_only(), + vec![Step::stream(sse_text_unfinished(&["partial"]))], + no_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + + // When the turn runs. + send(&started, session, "hi"); + let seen = wait_events(&log, has_finished); + + // Then the provisional text was pushed, `finished` came last, and the + // status is failed. + assert_eq!(delta_text(&seen), "partial"); + assert_eq!( + seen.last(), + Some(&ChatEvent::Finished { + session_id: session + }) + ); + assert_eq!(status(&started.service, session).state, ChatState::Failed); + assert_eq!(started.server.finish(), 1); +} + +#[test] +fn with_streaming_off_the_request_is_not_streamed_and_only_finished_is_pushed() { + // Given a service with streaming off and a model that answers in JSON. + let started = started( + text_only(), + vec![Step::reply(text("pong"))], + no_token, + false, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + + // When the turn runs. + send(&started, session, "ping"); + let seen = wait_events(&log, has_finished); + + // Then the only event is `finished`, and the request said `stream` false. + assert_eq!( + seen, + [ChatEvent::Finished { + session_id: session + }] + ); + assert_eq!( + status(&started.service, session).state, + ChatState::Completed + ); + let (served, requests) = started.server.finish_recorded(); + assert_eq!(served, 1); + let body: Value = serde_json::from_str(&requests[0].body).expect("request body"); + assert_eq!(body["stream"], json!(false)); +} + +#[test] +fn a_send_refused_synchronously_pushes_no_event() { + // Given a subscriber and a service that will never be contacted. + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let endpoint = unused_endpoint(); + let service = fixture.service_streaming(&endpoint, no_token, true); + let session = service + .session_create(wire( + json!({"workspace_id": workspace.id, "title": "Refused"}), + )) + .expect("create session") + .session_id; + let log = subscribe(&service); + + // When an empty message is sent. + let refused = service + .chat_send(wire(json!({ + "session_id": session, + "message": " ", + "settings": settings_json(&endpoint), + }))) + .expect_err("an empty message is refused"); + + // Then it failed before any turn started, and nothing was pushed. + assert_eq!(refused.code(), "invalid_request"); + assert!(snapshot(&log).is_empty()); + assert_eq!(status(&service, session).state, ChatState::Idle); + let tree = service + .session_tree(wire( + json!({"view": "active", "sort": "title", "descending": false}), + )) + .expect("tree"); + assert!( + tree.groups + .iter() + .flat_map(|group| group.sessions.iter()) + .all(|summary| summary.status != SessionStatus::Running) + ); +} + +// ---- Cancelling and shutting down a streamed turn (ADR-0050) ---- + +/// The slowest acceptable cancellation. The request deadline is 30 seconds. +const CANCEL_BOUND: Duration = Duration::from_secs(5); + +/// The status of one session in the active session tree. +fn tree_status(service: &DesktopService, session: Uuid) -> SessionStatus { + service + .session_tree(wire( + json!({"view": "active", "sort": "title", "descending": false}), + )) + .expect("tree") + .groups + .iter() + .flat_map(|group| group.sessions.iter()) + .find(|summary| summary.session_id == session) + .map(|summary| summary.status) + .expect("the session is listed") +} + +#[test] +fn chat_cancel_stops_a_streamed_text_turn_held_mid_stream() { + // Given a streamed text-only turn whose server sent one piece and holds + // the rest of the body, and a second scripted reply for a later turn. + let (release, hold) = gate(); + let started = started( + text_only(), + vec![ + Step::stream_held(sse_text_unfinished(&["first "]), hold, sse_text(&["rest"])), + Step::stream(sse_text(&["again"])), + ], + no_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + send(&started, session, "hi"); + wait_events(&log, |events| delta_text(events) == "first "); + + // When the turn is cancelled. + let begun = Instant::now(); + let after = started + .service + .chat_cancel(wire(session_request(session))) + .expect("cancel"); + + // Then the status goes `cancelling`, then `failed` with the code + // `cancelled`, and a `finished` event is pushed well inside the deadline. + assert_eq!(after.state, ChatState::Cancelling); + let seen = wait_events(&log, has_finished); + let elapsed = begun.elapsed(); + assert!(elapsed < CANCEL_BOUND, "cancellation took {elapsed:?}"); + assert_eq!(delta_text(&seen), "first "); + let done = status(&started.service, session); + assert_eq!(done.state, ChatState::Failed); + assert_eq!( + done.error.as_ref().map(|error| error.code()), + Some("cancelled") + ); + assert_eq!( + done.error.as_ref().map(|error| error.message()), + Some("turn was cancelled") + ); + assert!(done.outcome.is_none()); + + // And the journal has no open turn, so the next send is accepted. The + // server takes its next request once the first reply is released. + assert_eq!(tree_status(&started.service, session), SessionStatus::Idle); + drop(release); + send(&started, session, "once more"); + wait_events(&log, |events| { + events + .iter() + .filter(|event| matches!(event, ChatEvent::Finished { .. })) + .count() + == 2 + }); + assert_eq!( + status(&started.service, session).state, + ChatState::Completed + ); + assert_eq!(started.server.finish(), 2); +} + +#[test] +fn shutdown_ends_a_streamed_turn_held_mid_stream_inside_its_grace() { + // Given a streamed text-only turn held mid-stream. + let (release, hold) = gate(); + let started = started( + text_only(), + vec![Step::stream_held( + sse_text_unfinished(&["first "]), + hold, + sse_text(&["rest"]), + )], + no_token, + true, + ); + let session = new_session(&started); + let log = subscribe(&started.service); + send(&started, session, "hi"); + wait_events(&log, |events| delta_text(events) == "first "); + + // When the service shuts down with a long grace period. + let begun = Instant::now(); + let report = started.service.shutdown(Duration::from_secs(20)); + let elapsed = begun.elapsed(); + + // Then the turn finished well inside the grace period, as cancelled, and + // left no open turn. + assert_eq!((report.finished, report.still_running), (1, 0)); + assert!(elapsed < CANCEL_BOUND, "shutdown took {elapsed:?}"); + let done = status(&started.service, session); + assert_eq!(done.state, ChatState::Failed); + assert_eq!( + done.error.as_ref().map(|error| error.code()), + Some("cancelled") + ); + assert_eq!(tree_status(&started.service, session), SessionStatus::Idle); + drop(release); + assert_eq!(started.server.finish(), 1); +} diff --git a/crates/omgcode-app/tests/desktop_sessions.rs b/crates/omgcode-app/tests/desktop_sessions.rs new file mode 100644 index 0000000..fdd3e6a --- /dev/null +++ b/crates/omgcode-app/tests/desktop_sessions.rs @@ -0,0 +1,717 @@ +//! Session tree, search, transcript, lifecycle, workspace, and creation +//! commands of the desktop service, against real journals and a real index. + +use std::fs::{self, OpenOptions}; +use std::io::Write; +use std::path::PathBuf; +use std::thread; +use std::time::Duration; + +use omgcode_app::desktop::{ + DesktopService, SearchRoleDto, SessionStatus, SessionSummary, SessionTree, TranscriptPage, +}; +use omgcode_core::session::{ + CompactionRecord, MessageRole, RequestManifest, SessionJournal, ToolOutcome, +}; +use omgcode_core::turn::display_safe_line; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use uuid::Uuid; + +mod common; +use common::{Fixture, Workspace, json_of, no_token, read_tools, session_request, text_only, wire}; + +/// A short pause so consecutive journal events carry distinct times. +const TICK: Duration = Duration::from_millis(8); +const NO_ENDPOINT: &str = "http://127.0.0.1:1/v1"; + +fn service(fixture: &Fixture) -> DesktopService { + fixture.service(NO_ENDPOINT) +} + +fn tree(service: &DesktopService, view: &str, sort: &str, descending: bool) -> SessionTree { + service + .session_tree(wire( + json!({"view": view, "sort": sort, "descending": descending}), + )) + .expect("session tree") +} + +fn create(service: &DesktopService, workspace: &Workspace, title: &str) -> SessionSummary { + thread::sleep(TICK); + service + .session_create(wire(json!({"workspace_id": workspace.id, "title": title}))) + .expect("create session") +} + +fn titles(tree: &SessionTree) -> Vec> { + tree.groups + .iter() + .map(|group| group.sessions.iter().map(|s| s.title.clone()).collect()) + .collect() +} + +fn manifest() -> RequestManifest { + RequestManifest { + base_prompt: "BASE".into(), + prompt_version: "7".into(), + prompt_sha256: "a".repeat(64), + custom_instructions: String::new(), + model: "local-model".into(), + base_url: "http://127.0.0.1:1234/v1".into(), + context_tokens: 32_000, + max_output_tokens: 2_000, + tool_schema_version: Some("1".into()), + tools: Some(vec!["workspace_read_file".into()]), + } +} + +fn sha256_hex(text: &str) -> String { + Sha256::digest(text.as_bytes()) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} + +/// Create a journal directly with the core, release it, and return id and path. +fn core_session(fixture: &Fixture, workspace: &Workspace, title: &str) -> (Uuid, PathBuf) { + let journal = SessionJournal::create(&fixture.data, &workspace.path, title).expect("create"); + (journal.snapshot().session_id, journal.path().to_path_buf()) +} + +fn fixture_with_workspaces() -> (Fixture, Workspace, Workspace) { + let fixture = Fixture::new(); + let first = fixture.workspace("alpha-project", text_only()); + let second = fixture.workspace("beta-project", text_only()); + (fixture, first, second) +} + +#[test] +fn the_tree_groups_by_folder_orders_each_sort_and_filters_the_view() { + let (fixture, alpha, beta) = fixture_with_workspaces(); + let service = service(&fixture); + let charlie = create(&service, &alpha, "Charlie"); + create(&service, &beta, "alpha"); + create(&service, &alpha, "Bravo"); + let archived = create(&service, &beta, "Zulu archived"); + service + .session_archive(wire(session_request(archived.session_id))) + .expect("archive"); + + // Updated, newest first: Bravo, alpha, Charlie -> groups alpha-project, beta-project. + let by_updated = tree(&service, "active", "updated", true); + assert_eq!(by_updated.indexed_sessions, 3); + assert_eq!(by_updated.skipped_entries, 0); + assert_eq!( + titles(&by_updated), + [vec!["Bravo", "Charlie"], vec!["alpha"]] + ); + // Oldest first: Charlie, alpha, Bravo. + assert_eq!( + titles(&tree(&service, "active", "updated", false)), + [vec!["Charlie", "Bravo"], vec!["alpha"]] + ); + // Created, ascending and descending. + assert_eq!( + titles(&tree(&service, "active", "created", false)), + [vec!["Charlie", "Bravo"], vec!["alpha"]] + ); + assert_eq!( + titles(&tree(&service, "active", "created", true)), + [vec!["Bravo", "Charlie"], vec!["alpha"]] + ); + // Title, case-insensitive: alpha, Bravo, Charlie. + assert_eq!( + titles(&tree(&service, "active", "title", false)), + [vec!["alpha"], vec!["Bravo", "Charlie"]] + ); + assert_eq!( + titles(&tree(&service, "active", "title", true)), + [vec!["Charlie", "Bravo"], vec!["alpha"]] + ); + + // The group folders and their opaque, stable, distinct identifiers. + let groups = &by_updated.groups; + assert!(groups[0].folder_display.ends_with("alpha-project")); + assert!(groups[1].folder_display.ends_with("beta-project")); + assert_eq!(groups[0].group_id.len(), 16); + assert_ne!(groups[0].group_id, groups[1].group_id); + let again = tree(&service, "active", "title", false); + let alpha_group = again + .groups + .iter() + .find(|group| group.folder_display.ends_with("alpha-project")) + .expect("alpha group"); + assert_eq!(alpha_group.group_id, groups[0].group_id); + + // The archived view holds only the archived session. + let archived_view = tree(&service, "archived", "updated", true); + assert_eq!(archived_view.indexed_sessions, 1); + assert_eq!(titles(&archived_view), [vec!["Zulu archived"]]); + assert!(archived_view.groups[0].sessions[0].archived); + assert_eq!(charlie.status, SessionStatus::Idle); +} + +#[test] +fn a_session_without_a_registered_workspace_still_groups_by_its_recorded_folder() { + let fixture = Fixture::new(); + let loose = fixture.workspace("loose", text_only()); + let (id, _) = core_session(&fixture, &loose, "Direct"); + let service = service(&fixture); + let tree = tree(&service, "active", "updated", true); + assert_eq!(tree.groups.len(), 1); + assert_eq!(tree.groups[0].sessions[0].session_id, id); + assert!(tree.groups[0].folder_display.ends_with("loose")); +} + +#[test] +fn an_open_turn_is_reported_and_interrupting_closes_it_with_the_operator_code() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let (id, path) = core_session(&fixture, &alpha, "Unfinished"); + { + let mut journal = SessionJournal::open(&path).expect("open"); + journal + .record_message(MessageRole::User, "question") + .expect("message"); + journal.start_turn().expect("start turn"); + } + let service = service(&fixture); + let listed = tree(&service, "active", "updated", true); + assert_eq!(listed.groups[0].sessions[0].status, SessionStatus::TurnOpen); + + // Archiving is refused while a turn is open. + let refused = service + .session_archive(wire(session_request(id))) + .expect_err("an open turn blocks archiving"); + assert_eq!(refused.code(), "turn_open"); + + let closed = service + .session_interrupt(wire(session_request(id))) + .expect("interrupt"); + assert_eq!(closed.status, SessionStatus::Idle); + let page = transcript(&service, id, None, 50); + let failure = json_of(page.entries.last().expect("entries")); + assert_eq!(failure["kind"], "turn_failed"); + assert_eq!(failure["code"], "operator_interrupted"); + + let again = service + .session_interrupt(wire(session_request(id))) + .expect_err("no turn is open"); + assert_eq!(again.code(), "invalid_request"); + assert_eq!(again.message(), "No turn is open."); +} + +#[test] +fn archive_and_restore_change_the_listing_and_are_idempotent() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let service = service(&fixture); + let session = create(&service, &alpha, "Keep or file"); + let request = || wire(session_request(session.session_id)); + + let archived = service.session_archive(request()).expect("archive"); + assert!(archived.archived); + assert_eq!(archived.event_count, session.event_count + 1); + let twice = service.session_archive(request()).expect("archive again"); + assert!(twice.archived); + assert_eq!(twice.event_count, archived.event_count, "no second event"); + assert_eq!( + tree(&service, "active", "updated", true).indexed_sessions, + 0 + ); + assert_eq!( + tree(&service, "archived", "updated", true).indexed_sessions, + 1 + ); + + let restored = service.session_restore(request()).expect("restore"); + assert!(!restored.archived); + let twice = service.session_restore(request()).expect("restore again"); + assert_eq!(twice.event_count, restored.event_count); + assert_eq!( + tree(&service, "active", "updated", true).indexed_sessions, + 1 + ); + + let missing = service + .session_archive(wire(session_request(Uuid::new_v4()))) + .expect_err("unknown session"); + assert_eq!(missing.code(), "not_found"); +} + +#[test] +fn a_corrupted_journal_is_invalid_with_a_code_and_a_placeholder_title() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let (id, path) = core_session(&fixture, &alpha, "Soon broken"); + fs::write(&path, b"this is not a journal\n").expect("corrupt the journal"); + let service = service(&fixture); + let listed = tree(&service, "active", "updated", true); + let broken = listed + .groups + .iter() + .flat_map(|group| &group.sessions) + .find(|session| session.session_id == id) + .expect("the corrupt session is listed"); + assert_eq!(broken.status, SessionStatus::Invalid); + assert_eq!(broken.title, "(unreadable session)"); + let code = broken.invalid_code.as_deref().expect("an invalid code"); + assert!(code.starts_with("invalid_journal"), "{code}"); + + let error = service + .session_transcript(wire( + json!({"session_id": id, "after_seq": null, "limit": 10}), + )) + .expect_err("a corrupt journal has no transcript"); + assert_eq!(error.code(), "invalid_journal"); + let error = service + .session_archive(wire(session_request(id))) + .expect_err("a corrupt journal cannot be archived"); + assert_eq!(error.code(), "invalid_journal"); +} + +#[cfg(windows)] +#[test] +fn a_journal_held_by_another_writer_is_busy() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let writer = SessionJournal::create(&fixture.data, &alpha.path, "Held elsewhere") + .expect("create and hold"); + let id = writer.snapshot().session_id; + let service = service(&fixture); + let listed = tree(&service, "active", "updated", true); + let held = &listed.groups[0].sessions[0]; + assert_eq!(held.session_id, id); + assert_eq!(held.status, SessionStatus::Busy); + assert_eq!(held.title, "(session in use)"); + + let error = service + .session_transcript(wire( + json!({"session_id": id, "after_seq": null, "limit": 10}), + )) + .expect_err("a held journal cannot be inspected on Windows"); + assert_eq!(error.code(), "busy"); + let error = service + .session_archive(wire(session_request(id))) + .expect_err("a held journal cannot be opened"); + assert_eq!(error.code(), "busy"); + drop(writer); +} + +#[test] +fn hostile_titles_and_folders_come_back_escaped() { + let fixture = Fixture::new(); + let hostile_folder = "evil\u{202e}dir"; + let workspace = fixture.workspace(hostile_folder, text_only()); + let hostile_title = "Pay \u{202e}txt.exe\u{200b} now\u{1b}[2J"; + let (id, _) = core_session(&fixture, &workspace, hostile_title); + let service = service(&fixture); + let listed = tree(&service, "active", "updated", true); + let group = &listed.groups[0]; + let session = &group.sessions[0]; + assert_eq!(session.session_id, id); + assert_eq!(session.title, display_safe_line(hostile_title)); + assert!(session.title.contains("\\u{202e}")); + for text in [&session.title, &group.folder_display] { + assert!( + text.chars() + .all(|c| !c.is_control() && !matches!(c, '\u{202e}' | '\u{200b}')), + "hidden characters must be escaped in {text:?}" + ); + } + assert!(group.folder_display.contains("\\u{202e}")); + + // The transcript page and search results are escaped the same way. + let page = transcript(&service, id, None, 10); + assert_eq!(page.session.title, session.title); + assert_eq!(page.folder_display, group.folder_display); +} + +fn transcript( + service: &DesktopService, + id: Uuid, + after: Option, + limit: u32, +) -> TranscriptPage { + service + .session_transcript(wire( + json!({"session_id": id, "after_seq": after, "limit": limit}), + )) + .expect("transcript") +} + +#[test] +fn search_maps_roles_sequence_numbers_snippets_and_the_view() { + let (fixture, alpha, beta) = fixture_with_workspaces(); + let (first, path) = core_session(&fixture, &alpha, "Orchard notes"); + { + let mut journal = SessionJournal::open(&path).expect("open"); + journal + .record_message(MessageRole::User, "tell me about the quince tree") + .expect("user"); + journal + .record_message(MessageRole::Assistant, "a quince is a fruit") + .expect("assistant"); + } + let (second, _) = core_session(&fixture, &beta, "Quince recipes"); + let service = service(&fixture); + + let search = |query: &str, view: &str, limit: u32| { + service + .session_search(wire(json!({"query": query, "view": view, "limit": limit}))) + .expect("search") + }; + let result = search("quince", "active", 50); + assert!(!result.truncated); + let mut found: Vec<(Uuid, Option, SearchRoleDto)> = result + .hits + .iter() + .map(|hit| (hit.session.session_id, hit.seq, hit.role)) + .collect(); + found.sort_by_key(|(id, seq, _)| (*id, *seq)); + let mut expected = vec![ + (first, Some(1), SearchRoleDto::User), + (first, Some(2), SearchRoleDto::Assistant), + (second, None, SearchRoleDto::Title), + ]; + expected.sort_by_key(|(id, seq, _)| (*id, *seq)); + assert_eq!(found, expected); + for hit in &result.hits { + assert!(hit.snippet.iter().any(|part| part.matched), "{hit:?}"); + let joined: String = hit.snippet.iter().map(|part| part.text.as_str()).collect(); + assert!(joined.to_lowercase().contains("quince")); + assert!(!hit.folder_display.is_empty()); + } + + // The limit clamps to at least one hit, and truncation is reported. + let clamped = search("quince", "active", 0); + assert_eq!(clamped.hits.len(), 1); + assert!(clamped.truncated); + + // The view filter: archive the first session. + service + .session_archive(wire(session_request(first))) + .expect("archive"); + assert_eq!(search("quince", "active", 50).hits.len(), 1); + assert_eq!(search("quince", "archived", 50).hits.len(), 2); + + let error = service + .session_search(wire(json!({"query": " ", "view": "active", "limit": 10}))) + .expect_err("an empty query is invalid"); + assert_eq!(error.code(), "invalid_request"); +} + +/// A journal that records every kind of event, as the core inspection test does. +fn rich_journal(fixture: &Fixture, workspace: &Workspace) -> (Uuid, PathBuf, Uuid, Uuid) { + let mut journal = + SessionJournal::create(&fixture.data, &workspace.path, "Everything").expect("create"); + let id = journal.snapshot().session_id; + let path = journal.path().to_path_buf(); + journal + .record_message(MessageRole::User, "hello") + .expect("user"); + let first = journal.start_turn().expect("turn one"); + journal + .record_manifest(first, manifest()) + .expect("manifest"); + journal + .record_tool_request( + first, + "call-1", + "workspace_read_file", + "{\"path\":\"a.txt\"}", + ) + .expect("tool request"); + let rendered = "file contents"; + journal + .record_tool_result( + first, + "call-1", + ToolOutcome::Succeeded, + None, + u64::try_from(rendered.len()).expect("size"), + &sha256_hex(rendered), + ) + .expect("tool result"); + journal + .record_tool_message("call-1", rendered) + .expect("tool message"); + journal + .record_message(MessageRole::Assistant, "done") + .expect("assistant"); + journal.complete_turn(first).expect("complete"); + journal + .record_message(MessageRole::User, "second") + .expect("second user"); + let second = journal.start_turn().expect("turn two"); + journal + .fail_turn(second, "provider_unreachable") + .expect("fail"); + let summary = "Earlier work read a file."; + journal + .record_compaction(CompactionRecord { + summary: summary.into(), + compacted_messages: 3, + includes_tool_results: true, + summary_sha256: sha256_hex(summary), + model: "fixture".into(), + prompt_tokens: Some(10), + completion_tokens: Some(4), + }) + .expect("compact"); + journal.archive().expect("archive"); + journal.restore().expect("restore"); + (id, path, first, second) +} + +#[test] +fn every_transcript_entry_kind_maps_to_its_wire_shape() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let (id, _, first, second) = rich_journal(&fixture, &alpha); + let service = service(&fixture); + let page = transcript(&service, id, None, 200); + assert_eq!(page.next_after_seq, None); + assert!(!page.incomplete_tail); + assert_eq!(page.session.session_id, id); + assert_eq!(page.session.status, SessionStatus::Idle); + assert!(page.folder_display.ends_with("alpha-project")); + + let entries: Vec = page.entries.iter().map(json_of).collect(); + let kinds: Vec<&str> = entries + .iter() + .map(|entry| entry["kind"].as_str().expect("kind")) + .collect(); + assert_eq!( + kinds, + [ + "message", + "turn_started", + "request_manifest", + "tool_requested", + "tool_completed", + "message", + "message", + "turn_completed", + "message", + "turn_started", + "turn_failed", + "context_compacted", + "session_archived", + "session_restored", + ] + ); + let seqs: Vec = entries + .iter() + .map(|entry| entry["seq"].as_u64().expect("seq")) + .collect(); + assert_eq!(seqs, (1..=14).collect::>()); + + assert_eq!(entries[0]["role"], "user"); + assert_eq!(entries[0]["source"], "user_cli"); + assert_eq!(entries[0]["text"], "hello"); + assert!(entries[0]["call_id"].is_null()); + assert_eq!(entries[1]["request_id"], first.to_string()); + assert_eq!(entries[2]["model"], "local-model"); + assert_eq!(entries[2]["prompt_version"], "7"); + assert_eq!(entries[2]["tools"], json!(["workspace_read_file"])); + assert_eq!(entries[3]["tool"], "workspace_read_file"); + assert_eq!(entries[3]["arguments"], "{\"path\":\"a.txt\"}"); + assert_eq!(entries[4]["outcome"], "succeeded"); + assert!(entries[4]["code"].is_null()); + assert_eq!(entries[4]["output_bytes"], 13); + assert!(entries[4].get("output_sha256").is_none(), "digest omitted"); + assert_eq!(entries[5]["role"], "tool"); + assert_eq!(entries[5]["source"], "core_tool"); + assert_eq!(entries[5]["call_id"], "call-1"); + assert_eq!(entries[6]["role"], "assistant"); + assert_eq!(entries[6]["source"], "provider"); + assert_eq!(entries[10]["request_id"], second.to_string()); + assert_eq!(entries[10]["code"], "provider_unreachable"); + assert_eq!(entries[11]["summary"], "Earlier work read a file."); + assert_eq!(entries[11]["compacted_messages"], 3); + assert_eq!(entries[11]["model"], "fixture"); + for entry in &entries { + let stamp = entry["timestamp"].as_str().expect("timestamp"); + assert!(stamp.ends_with('Z') && stamp.contains('T'), "{stamp}"); + } +} + +#[test] +fn transcript_pages_follow_after_seq_and_limit_until_no_entries_remain() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let (id, _, _, _) = rich_journal(&fixture, &alpha); + let service = service(&fixture); + + let mut seen = Vec::new(); + let mut after = None; + let mut pages = 0; + loop { + let page = transcript(&service, id, after, 5); + assert!(page.entries.len() <= 5); + seen.extend(page.entries.iter().map(|entry| entry.seq)); + pages += 1; + match page.next_after_seq { + Some(next) => { + assert_eq!(Some(next), page.entries.last().map(|entry| entry.seq)); + after = Some(next); + } + None => break, + } + assert!(pages < 10, "paging must terminate"); + } + assert_eq!(seen, (1..=14).collect::>()); + assert_eq!(pages, 3); + + // An exact fit has no next page; a limit of zero clamps to one entry. + assert_eq!(transcript(&service, id, Some(9), 5).next_after_seq, None); + let one = transcript(&service, id, None, 0); + assert_eq!(one.entries.len(), 1); + assert_eq!(one.next_after_seq, Some(1)); + // Past the end there is nothing. + let none = transcript(&service, id, Some(14), 5); + assert!(none.entries.is_empty() && none.next_after_seq.is_none()); +} + +#[test] +fn a_transcript_page_holds_at_most_one_mebibyte_of_text() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let (id, path) = core_session(&fixture, &alpha, "Heavy"); + let chunk = "x".repeat(16_000); + { + let mut journal = SessionJournal::open(&path).expect("open"); + for _ in 0..80 { + journal + .record_message(MessageRole::User, &chunk) + .expect("message"); + } + } + let service = service(&fixture); + let page = transcript(&service, id, None, 200); + // 65 entries of 16,000 bytes fit in 1 MiB; a 66th would exceed it. + assert_eq!(page.entries.len(), 65); + assert_eq!(page.next_after_seq, Some(65)); + let rest = transcript(&service, id, page.next_after_seq, 200); + assert_eq!(rest.entries.len(), 15); + assert_eq!(rest.next_after_seq, None); +} + +#[test] +fn an_incomplete_tail_is_reported_and_an_unknown_session_is_not_found() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let (id, path) = core_session(&fixture, &alpha, "Mid-append"); + OpenOptions::new() + .append(true) + .open(&path) + .expect("open for append") + .write_all(b"{\"partial") + .expect("partial record"); + let service = service(&fixture); + let page = transcript(&service, id, None, 10); + assert!(page.incomplete_tail); + + let missing = service + .session_transcript(wire( + json!({"session_id": Uuid::new_v4(), "after_seq": null, "limit": 10}), + )) + .expect_err("unknown session"); + assert_eq!(missing.code(), "not_found"); +} + +#[test] +fn a_session_the_index_has_not_seen_is_found_after_one_refresh() { + let (fixture, alpha, _) = fixture_with_workspaces(); + let service = service(&fixture); + // Warm the index, then add a journal behind its back. + tree(&service, "active", "updated", true); + let (id, _) = core_session(&fixture, &alpha, "Late arrival"); + let page = transcript(&service, id, None, 10); + assert_eq!(page.session.title, "Late arrival"); +} + +#[test] +fn workspace_listing_reports_the_store_state_and_permissions() { + let bare = Fixture::bare(); + let service = service(&bare); + let empty = service.workspace_list().expect("workspace list"); + assert!(!empty.development_root_set); + assert!(empty.workspaces.is_empty()); + let error = service + .session_create(wire(json!({"workspace_id": Uuid::new_v4(), "title": "t"}))) + .expect_err("no store, no workspace"); + assert_eq!(error.code(), "unknown_workspace"); + + let fixture = Fixture::new(); + let service = fixture.service(NO_ENDPOINT); + let rooted = service.workspace_list().expect("workspace list"); + assert!(rooted.development_root_set); + assert!(rooted.workspaces.is_empty()); + + let tools = fixture.workspace("tools-on", read_tools()); + let plain = fixture.workspace("plain", text_only()); + let listed = service.workspace_list().expect("workspace list"); + assert_eq!(listed.workspaces.len(), 2); + let find = |id: Uuid| { + listed + .workspaces + .iter() + .find(|choice| choice.workspace_id == id) + .expect("registered workspace") + }; + let tools_on = find(tools.id); + assert!(tools_on.folder_display.ends_with("tools-on")); + assert!(tools_on.model_tools && tools_on.read && !tools_on.write && !tools_on.execute); + let plain_off = find(plain.id); + assert!(!plain_off.model_tools && !plain_off.read && !plain_off.write && !plain_off.execute); +} + +#[test] +fn creating_a_session_uses_a_registered_workspace_and_core_validates_the_title() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("created-here", text_only()); + let service = service(&fixture); + + let summary = service + .session_create(wire( + json!({"workspace_id": workspace.id, "title": "Fresh \u{202e}title"}), + )) + .expect("create"); + assert_eq!(summary.status, SessionStatus::Idle); + assert!(!summary.archived); + assert_eq!(summary.event_count, 1); + assert_eq!(summary.title, display_safe_line("Fresh \u{202e}title")); + assert!(summary.created_at.is_some() && summary.updated_at.is_some()); + + let listed = tree(&service, "active", "updated", true); + assert_eq!(listed.groups.len(), 1); + assert_eq!(listed.groups[0].sessions[0].session_id, summary.session_id); + assert!(listed.groups[0].folder_display.ends_with("created-here")); + + let unknown = service + .session_create(wire(json!({"workspace_id": Uuid::new_v4(), "title": "t"}))) + .expect_err("unregistered workspace"); + assert_eq!(unknown.code(), "unknown_workspace"); + let empty = service + .session_create(wire(json!({"workspace_id": workspace.id, "title": " "}))) + .expect_err("a blank title"); + assert_eq!(empty.code(), "invalid_request"); + assert_eq!( + tree(&service, "active", "updated", true).indexed_sessions, + 1, + "a refused create leaves no session" + ); +} + +#[test] +fn a_service_opens_on_a_new_data_root_and_lists_nothing() { + let fixture = Fixture::bare(); + let service = DesktopService::open_with_token_source( + omgcode_app::desktop::DesktopConfig { + data_root: fixture.data.join("not-yet-created"), + endpoint: NO_ENDPOINT.to_owned(), + token_source: omgcode_app::TokenSource::Environment, + stream: true, + }, + no_token, + ) + .expect("open on a missing directory"); + let empty = tree(&service, "active", "updated", true); + assert!(empty.groups.is_empty()); + assert_eq!(empty.indexed_sessions, 0); +} diff --git a/crates/omgcode-app/tests/desktop_stored_credential.rs b/crates/omgcode-app/tests/desktop_stored_credential.rs new file mode 100644 index 0000000..eea24ea --- /dev/null +++ b/crates/omgcode-app/tests/desktop_stored_credential.rs @@ -0,0 +1,259 @@ +//! The desktop service reads the local adapter's token from a stored provider +//! credential for each turn (ADR-0045, ADR-0047), against the real settings and +//! native credential stores. On Linux these need an unlocked Secret Service. +//! Test tokens are obviously fake, and a `Vault` deletes only what it stored. + +use omgcode_app::TokenSource; +use omgcode_app::desktop::{ChatState, ChatStatus, DesktopConfig, DesktopService}; +use serde_json::json; +use uuid::Uuid; + +mod common; +use common::credential::Vault; +use common::{ + Fixture, MockServer, NoCallServer, Step, session_request, settings_json, text, text_of, + text_only, unused_endpoint, wait_finished, wire, +}; + +const STORED: &str = "fake-desktop-token-27ad90e1"; +const ROTATED: &str = "fake-desktop-rotated-6e0c5b34"; + +fn service(fixture: &Fixture, endpoint: &str, profile: &str) -> DesktopService { + DesktopService::open(DesktopConfig { + data_root: fixture.data.clone(), + endpoint: endpoint.to_owned(), + token_source: TokenSource::Profile(profile.to_owned()), + // The scripted model server answers with plain JSON. + stream: false, + }) + .expect("open desktop service") +} + +fn new_session(service: &DesktopService, workspace: Uuid) -> Uuid { + service + .session_create(wire( + json!({"workspace_id": workspace, "title": "stored token"}), + )) + .expect("create session") + .session_id +} + +fn send( + service: &DesktopService, + session: Uuid, + message: &str, + endpoint: &str, +) -> Result { + service.chat_send(wire(json!({ + "session_id": session, + "message": message, + "settings": settings_json(endpoint), + }))) +} + +#[test] +fn each_turn_reads_the_stored_credential_so_a_rotation_applies_to_the_next_turn() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let mut vault = Vault::new(&fixture.data); + let server = MockServer::start(vec![ + Step::reply(text("first")), + Step::reply(text("second")), + ]); + vault.add_profile_at("lms", "lm_studio", "api_key", &server.endpoint); + vault.bind("lms", STORED.as_bytes()); + let service = service(&fixture, &server.endpoint, "lms"); + let session = new_session(&service, workspace.id); + + let started = send(&service, session, "hello", &server.endpoint).expect("first turn"); + assert!(matches!(started.state, ChatState::Running)); + let done = wait_finished(&service, session); + assert!(matches!(done.state, ChatState::Completed), "{done:?}"); + let mut everything = text_of(&started) + &text_of(&done); + + // Rotate through the settings API between turns. + vault.bind("lms", ROTATED.as_bytes()); + + send(&service, session, "again", &server.endpoint).expect("second turn"); + let done = wait_finished(&service, session); + assert!(matches!(done.state, ChatState::Completed), "{done:?}"); + everything += &text_of(&done); + everything += &text_of( + &service + .session_transcript(wire( + json!({"session_id": session, "after_seq": null, "limit": 200}), + )) + .expect("transcript"), + ); + + let (served, requests) = server.finish_recorded(); + assert_eq!(served, 2); + assert_eq!( + requests[0].header("authorization"), + Some(format!("Bearer {STORED}")) + ); + assert_eq!( + requests[1].header("authorization"), + Some(format!("Bearer {ROTATED}")) + ); + for secret in [STORED, ROTATED] { + assert!(!everything.contains(secret), "a response carries a token"); + } +} + +#[test] +fn an_unreadable_credential_is_a_synchronous_credential_error_and_no_turn_starts() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let mut vault = Vault::new(&fixture.data); + // Nothing listens: a request would fail as `provider`, not `credential`. + let endpoint = unused_endpoint(); + vault.add_profile_at("lms", "lm_studio", "api_key", &endpoint); + let id = vault.bind("lms", STORED.as_bytes()); + let service = service(&fixture, &endpoint, "lms"); + let session = new_session(&service, workspace.id); + + vault.delete_native(&id); + let error = send(&service, session, "hello", &endpoint).expect_err("refused"); + assert_eq!(error.code(), "credential"); + assert!( + error.message().starts_with("credential_not_found:"), + "{}", + error.message() + ); + assert!(!text_of(&error).contains(STORED)); + + // No thread started and no slot exists: the session stays idle. + let status = service + .chat_status(wire(session_request(session))) + .expect("status"); + assert!(matches!(status.state, ChatState::Idle), "{status:?}"); + let transcript = service + .session_transcript(wire( + json!({"session_id": session, "after_seq": null, "limit": 200}), + )) + .expect("transcript"); + assert!(transcript.entries.is_empty(), "no turn was recorded"); +} + +#[test] +fn an_unknown_wrong_provider_or_unbound_profile_is_refused_before_a_turn() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let mut vault = Vault::new(&fixture.data); + vault.add_profile("openai-key", "openai", "api_key"); + let endpoint = unused_endpoint(); + vault.add_profile_at("unbound", "lm_studio", "api_key", &endpoint); + vault.bind("openai-key", STORED.as_bytes()); + for (name, expected) in [ + ("missing", "unknown_profile:"), + ("openai-key", "wrong_provider:"), + ("unbound", "not_bound:"), + ] { + let service = service(&fixture, &endpoint, name); + let session = new_session(&service, workspace.id); + let error = send(&service, session, "hello", &endpoint).expect_err("refused"); + assert_eq!(error.code(), "credential", "{name}"); + assert!(error.message().starts_with(expected), "{}", error.message()); + assert!(!text_of(&error).contains(STORED)); + } +} + +#[test] +fn a_keyless_profile_sends_no_authorization_header() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let vault = Vault::new(&fixture.data); + let server = MockServer::start(vec![Step::reply(text("ok"))]); + vault.add_profile_at("open", "lm_studio", "none", &server.endpoint); + let service = service(&fixture, &server.endpoint, "open"); + let session = new_session(&service, workspace.id); + send(&service, session, "hello", &server.endpoint).expect("turn"); + let done = wait_finished(&service, session); + assert!(matches!(done.state, ChatState::Completed), "{done:?}"); + let (_, requests) = server.finish_recorded(); + assert_eq!(requests[0].header("authorization"), None); +} + +#[test] +fn a_profile_without_an_endpoint_is_refused_before_a_turn() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let mut vault = Vault::new(&fixture.data); + vault.add_profile("old-none", "lm_studio", "none"); + vault.add_profile("old-key", "lm_studio", "api_key"); + vault.bind("old-key", STORED.as_bytes()); + let endpoint = unused_endpoint(); + for name in ["old-none", "old-key"] { + let service = service(&fixture, &endpoint, name); + let session = new_session(&service, workspace.id); + let error = send(&service, session, "hello", &endpoint).expect_err("refused"); + assert_eq!(error.code(), "credential", "{name}"); + assert!( + error.message().starts_with("endpoint_not_recorded:"), + "{}", + error.message() + ); + assert!(!text_of(&error).contains(STORED)); + } +} + +/// The service's fixed endpoint is the profile's, and a profile removed and +/// added again with another endpoint while the service runs ends the next turn +/// synchronously: no thread starts and neither listener is contacted. +#[test] +fn a_profile_replaced_with_another_endpoint_refuses_the_next_turn() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let mut vault = Vault::new(&fixture.data); + let server = MockServer::start(vec![Step::reply(text("first"))]); + let replacement = NoCallServer::start(); + vault.add_profile_at("lms", "lm_studio", "api_key", &server.endpoint); + vault.bind("lms", STORED.as_bytes()); + // What the shell would resolve once at launch and fix for the process. + let launch = omgcode_app::resolve_local_connection( + &fixture.data, + &omgcode_app::ConnectionSource::Profile("lms".to_owned()), + ) + .expect("the launch check"); + assert_eq!(launch.endpoint, server.endpoint); + let service = service(&fixture, &launch.endpoint, "lms"); + assert_eq!(service.chat_defaults().endpoint, server.endpoint); + let session = new_session(&service, workspace.id); + + send(&service, session, "hello", &launch.endpoint).expect("first turn"); + let done = wait_finished(&service, session); + assert!(matches!(done.state, ChatState::Completed), "{done:?}"); + + // Replace the profile through the settings API: same name, new endpoint. + vault.remove_profile("lms"); + vault.add_profile_at("lms", "lm_studio", "api_key", &replacement.endpoint); + vault.bind("lms", ROTATED.as_bytes()); + + let error = send(&service, session, "again", &launch.endpoint).expect_err("refused"); + assert_eq!(error.code(), "credential"); + assert!( + error.message().starts_with("endpoint_changed:"), + "{}", + error.message() + ); + assert!(error.message().contains("restart"), "{}", error.message()); + let rendered = text_of(&error); + assert!(!rendered.contains(STORED) && !rendered.contains(ROTATED)); + + // No thread started: the finished slot of the first turn is untouched, and + // the interface still shows the launch endpoint. + let status = service + .chat_status(wire(session_request(session))) + .expect("status"); + assert!(matches!(status.state, ChatState::Completed), "{status:?}"); + assert_eq!(service.chat_defaults().endpoint, server.endpoint); + + let (served, requests) = server.finish_recorded(); + assert_eq!(served, 1, "only the first turn reached the launch endpoint"); + assert_eq!( + requests[0].header("authorization"), + Some(format!("Bearer {STORED}")) + ); + replacement.assert_untouched(); +} diff --git a/crates/omgcode-app/tests/desktop_wire.rs b/crates/omgcode-app/tests/desktop_wire.rs new file mode 100644 index 0000000..cff44d5 --- /dev/null +++ b/crates/omgcode-app/tests/desktop_wire.rs @@ -0,0 +1,989 @@ +//! The wire contract of the desktop service: exact JSON keys and types of every +//! response, the closed request shapes, and agreement with the TypeScript +//! declarations in `crates/omgcode-desktop/ui/src/ipc/types.ts`. + +use std::collections::BTreeSet; +use std::fs; + +use omgcode_app::desktop::{ + ApprovalDecideRequest, ApprovalDetail, ApprovalField, ApprovalList, ApprovalOperationDto, + ApprovalRequest, ApprovalStateDto, ApprovalView, ApprovalsSummary, Binding, ChatEvent, + ChatOutcome, ChatSendRequest, ChatSettingsDto, ChatState, ChatStatus, ContextMeterDto, + Decision, DesktopError, EntryKind, MessageRoleDto, SearchHit, SearchRequest, SearchResult, + SearchRoleDto, SessionCreateRequest, SessionRequest, SessionStatus, SessionSummary, + SessionTree, SnippetPart, ToolOutcomeDto, ToolsMode, TranscriptEntry, TranscriptPage, + TranscriptRequest, TreeRequest, TreeSort, TreeView, WorkspaceChoice, WorkspaceGroup, + WorkspaceList, +}; +use serde_json::{Value, json}; +use uuid::Uuid; + +mod common; +use common::{json_of, text_of, wire}; + +const TYPES_TS: &str = concat!( + env!("CARGO_MANIFEST_DIR"), + "/../omgcode-desktop/ui/src/ipc/types.ts" +); +const CLIENT_TS: &str = concat!( + env!("CARGO_MANIFEST_DIR"), + "/../omgcode-desktop/ui/src/ipc/client.ts" +); +const APPROVALS_CLIENT_TS: &str = concat!( + env!("CARGO_MANIFEST_DIR"), + "/../omgcode-desktop/ui/src/ipc/approvals_client.ts" +); + +/// Assert that `value` is an object with exactly these keys and JSON kinds. +/// A kind is `string`, `number`, `bool`, `array`, `object`, or one of those +/// followed by `|null`. +fn assert_shape(value: &Value, expected: &[(&str, &str)]) { + let object = value.as_object().expect("a JSON object"); + let actual: BTreeSet<&str> = object.keys().map(String::as_str).collect(); + let wanted: BTreeSet<&str> = expected.iter().map(|(key, _)| *key).collect(); + assert_eq!(actual, wanted, "exact key set of {value}"); + for (key, kind) in expected { + let field = &object[*key]; + let (base, nullable) = kind + .strip_suffix("|null") + .map_or((*kind, false), |base| (base, true)); + let matches = match base { + "string" => field.is_string(), + "number" => field.is_u64() || field.is_i64(), + "bool" => field.is_boolean(), + "array" => field.is_array(), + "object" => field.is_object(), + other => panic!("unknown kind {other}"), + }; + assert!( + matches || (nullable && field.is_null()), + "{key} must be {kind}, got {field}" + ); + } +} + +fn id() -> Uuid { + Uuid::new_v4() +} + +fn summary(created: Option<&str>, invalid: Option<&str>) -> SessionSummary { + SessionSummary { + session_id: id(), + title: "Title".to_owned(), + created_at: created.map(str::to_owned), + updated_at: created.map(str::to_owned), + status: SessionStatus::Idle, + archived: false, + event_count: 3, + invalid_code: invalid.map(str::to_owned), + } +} + +const SUMMARY_SHAPE: &[(&str, &str)] = &[ + ("session_id", "string"), + ("title", "string"), + ("created_at", "string|null"), + ("updated_at", "string|null"), + ("status", "string"), + ("archived", "bool"), + ("event_count", "number"), + ("invalid_code", "string|null"), +]; + +fn approval_view(with_labels: bool) -> ApprovalView { + ApprovalView { + id: id(), + session_id: id(), + session_title: with_labels.then(|| "Session".to_owned()), + folder_display: with_labels.then(|| "/work".to_owned()), + operation: ApprovalOperationDto::FileWrite, + summary: "edit".to_owned(), + state: ApprovalStateDto::Pending, + created_unix: 1, + expires_unix: 2, + request_sha256: "ab".repeat(32), + writers_stopped: false, + } +} + +const APPROVAL_VIEW_SHAPE: &[(&str, &str)] = &[ + ("id", "string"), + ("session_id", "string"), + ("session_title", "string|null"), + ("folder_display", "string|null"), + ("operation", "string"), + ("summary", "string"), + ("state", "string"), + ("created_unix", "number"), + ("expires_unix", "number"), + ("request_sha256", "string"), + ("writers_stopped", "bool"), +]; + +fn outcome() -> ChatOutcome { + ChatOutcome { + finish_reason: "stop".to_owned(), + tools_mode: ToolsMode::Disabled, + tools_disabled_reason: Some("reason".to_owned()), + rounds: 0, + tool_calls: 0, + withheld_tool_results: 0, + context: Some(ContextMeterDto { + estimated_tokens: 1, + trigger_tokens: 2, + context_tokens: 3, + compacted: false, + }), + } +} + +#[test] +fn session_summaries_have_exactly_the_declared_keys_and_nullability() { + assert_shape( + &json_of(&summary(Some("2026-01-01T00:00:00Z"), None)), + SUMMARY_SHAPE, + ); + let bare = json_of(&summary(None, Some("invalid_header"))); + assert_shape(&bare, SUMMARY_SHAPE); + assert!(bare["created_at"].is_null() && bare["invalid_code"].is_string()); +} + +#[test] +fn the_tree_search_and_transcript_responses_have_exactly_the_declared_keys() { + let group = WorkspaceGroup { + group_id: "0123456789abcdef".to_owned(), + folder_display: "/work".to_owned(), + sessions: vec![summary(None, None)], + }; + let tree = SessionTree { + groups: vec![group], + indexed_sessions: 1, + skipped_entries: 0, + }; + let tree = json_of(&tree); + assert_shape( + &tree, + &[ + ("groups", "array"), + ("indexed_sessions", "number"), + ("skipped_entries", "number"), + ], + ); + assert_shape( + &tree["groups"][0], + &[ + ("group_id", "string"), + ("folder_display", "string"), + ("sessions", "array"), + ], + ); + + let hit = |seq: Option| SearchHit { + session: summary(None, None), + folder_display: "/work".to_owned(), + seq, + role: SearchRoleDto::Summary, + snippet: vec![SnippetPart { + text: "x".to_owned(), + matched: true, + }], + }; + let result = json_of(&SearchResult { + hits: vec![hit(Some(4)), hit(None)], + truncated: false, + }); + assert_shape(&result, &[("hits", "array"), ("truncated", "bool")]); + for (index, kind) in [(0, "number"), (1, "number|null")] { + assert_shape( + &result["hits"][index], + &[ + ("session", "object"), + ("folder_display", "string"), + ("seq", kind), + ("role", "string"), + ("snippet", "array"), + ], + ); + } + assert_shape( + &result["hits"][0]["snippet"][0], + &[("text", "string"), ("matched", "bool")], + ); + + let page = json_of(&TranscriptPage { + session: summary(None, None), + folder_display: "/work".to_owned(), + entries: Vec::new(), + next_after_seq: None, + incomplete_tail: false, + }); + assert_shape( + &page, + &[ + ("session", "object"), + ("folder_display", "string"), + ("entries", "array"), + ("next_after_seq", "number|null"), + ("incomplete_tail", "bool"), + ], + ); +} + +#[test] +fn chat_events_have_exactly_the_declared_shapes() { + // Given one of each pushed chat event. + let session = id(); + let events = [ + ( + ChatEvent::RequestStarted { + session_id: session, + request: 1, + }, + json!({"kind": "request_started", "session_id": session, "request": 1}), + ), + ( + ChatEvent::Delta { + session_id: session, + request: 2, + text: "hi \u{e9}".to_owned(), + }, + json!({"kind": "delta", "session_id": session, "request": 2, "text": "hi \u{e9}"}), + ), + ( + ChatEvent::PreviewTruncated { + session_id: session, + request: 3, + }, + json!({"kind": "preview_truncated", "session_id": session, "request": 3}), + ), + ( + ChatEvent::Finished { + session_id: session, + }, + json!({"kind": "finished", "session_id": session}), + ), + ]; + + // When each is serialized, then it is exactly the pinned JSON. + for (event, expected) in events { + assert_eq!(json_of(&event), expected); + } +} + +#[test] +fn chat_responses_have_exactly_the_declared_keys() { + let completed = json_of(&ChatStatus { + session_id: id(), + state: ChatState::Completed, + outcome: Some(outcome()), + error: None, + }); + assert_shape( + &completed, + &[ + ("session_id", "string"), + ("state", "string"), + ("outcome", "object|null"), + ("error", "object|null"), + ], + ); + assert_shape( + &completed["outcome"], + &[ + ("finish_reason", "string"), + ("tools_mode", "string"), + ("tools_disabled_reason", "string|null"), + ("rounds", "number"), + ("tool_calls", "number"), + ("withheld_tool_results", "number"), + ("context", "object|null"), + ], + ); + assert_shape( + &completed["outcome"]["context"], + &[ + ("estimated_tokens", "number"), + ("trigger_tokens", "number"), + ("context_tokens", "number"), + ("compacted", "bool"), + ], + ); + assert!(completed["error"].is_null()); + + // A failed status carries the error shape and no outcome. + let failed = ChatStatus { + session_id: id(), + state: ChatState::Failed, + outcome: None, + error: Some(error_value()), + }; + let failed = json_of(&failed); + assert!(failed["outcome"].is_null()); + assert_shape( + &failed["error"], + &[("code", "string"), ("message", "string")], + ); +} + +/// A `DesktopError` as the service produces it, without a public constructor: +/// a request the service refuses without touching storage. +fn error_value() -> DesktopError { + let directory = tempfile::tempdir().expect("data root"); + let service = omgcode_app::desktop::DesktopService::open_with_token_source( + omgcode_app::desktop::DesktopConfig { + data_root: directory.path().to_owned(), + endpoint: "http://127.0.0.1:1/v1".to_owned(), + token_source: omgcode_app::TokenSource::Environment, + stream: true, + }, + common::no_token, + ) + .expect("service"); + service + .chat_send(wire::(json!({ + "session_id": id(), + "message": " ", + "settings": common::settings_json("http://127.0.0.1:1/v1"), + }))) + .expect_err("an empty message is refused") +} + +#[test] +fn a_desktop_error_serializes_as_exactly_code_and_message() { + let error = error_value(); + let value = json_of(&error); + assert_shape(&value, &[("code", "string"), ("message", "string")]); + assert_eq!(value["code"], "invalid_request"); + assert_eq!(error.code(), "invalid_request"); +} + +#[test] +fn workspace_and_approval_responses_have_exactly_the_declared_keys() { + let list = json_of(&WorkspaceList { + development_root_set: true, + workspaces: vec![WorkspaceChoice { + workspace_id: id(), + folder_display: "/work".to_owned(), + model_tools: true, + read: true, + write: false, + execute: false, + }], + }); + assert_shape( + &list, + &[("development_root_set", "bool"), ("workspaces", "array")], + ); + assert_shape( + &list["workspaces"][0], + &[ + ("workspace_id", "string"), + ("folder_display", "string"), + ("model_tools", "bool"), + ("read", "bool"), + ("write", "bool"), + ("execute", "bool"), + ], + ); + assert_shape( + &json_of(&ApprovalsSummary { pending: 2 }), + &[("pending", "number")], + ); + + assert_shape(&json_of(&approval_view(true)), APPROVAL_VIEW_SHAPE); + let unlabelled = json_of(&approval_view(false)); + assert_shape(&unlabelled, APPROVAL_VIEW_SHAPE); + assert!(unlabelled["session_title"].is_null() && unlabelled["folder_display"].is_null()); + + let approvals = json_of(&ApprovalList { + now_unix: 9, + approvals: vec![approval_view(true)], + }); + assert_shape( + &approvals, + &[("now_unix", "number"), ("approvals", "array")], + ); + + let detail = |binding, token: Option<&str>| ApprovalDetail { + approval: approval_view(true), + binding, + tool: token.map(|_| "workspace_edit_file".to_owned()), + fields: vec![ApprovalField { + label: "File".to_owned(), + value: "a".to_owned(), + multiline: false, + }], + review_token: token.map(str::to_owned), + }; + for (value, tool_kind) in [ + (detail(Binding::Exact, Some("token")), "string"), + (detail(Binding::Stale, None), "string|null"), + ] { + let value = json_of(&value); + assert_shape( + &value, + &[ + ("approval", "object"), + ("binding", "string"), + ("tool", tool_kind), + ("fields", "array"), + ("review_token", "string|null"), + ], + ); + assert_shape( + &value["fields"][0], + &[ + ("label", "string"), + ("value", "string"), + ("multiline", "bool"), + ], + ); + } +} + +#[test] +fn every_transcript_entry_kind_has_exactly_its_declared_keys() { + let request = id(); + let entries = [ + ( + EntryKind::Message { + role: MessageRoleDto::Tool, + source: "core_tool".to_owned(), + text: "t".to_owned(), + call_id: None, + }, + "message", + vec![ + ("role", "string"), + ("source", "string"), + ("text", "string"), + ("call_id", "string|null"), + ], + ), + ( + EntryKind::TurnStarted { + request_id: request, + }, + "turn_started", + vec![("request_id", "string")], + ), + ( + EntryKind::TurnCompleted { + request_id: request, + }, + "turn_completed", + vec![("request_id", "string")], + ), + ( + EntryKind::TurnFailed { + request_id: request, + code: Some("c".to_owned()), + }, + "turn_failed", + vec![("request_id", "string"), ("code", "string|null")], + ), + ( + EntryKind::ToolRequested { + request_id: request, + call_id: "c".to_owned(), + tool: "t".to_owned(), + arguments: "{}".to_owned(), + }, + "tool_requested", + vec![ + ("request_id", "string"), + ("call_id", "string"), + ("tool", "string"), + ("arguments", "string"), + ], + ), + ( + EntryKind::ToolCompleted { + request_id: request, + call_id: "c".to_owned(), + outcome: ToolOutcomeDto::Denied, + code: None, + output_bytes: 4, + }, + "tool_completed", + vec![ + ("request_id", "string"), + ("call_id", "string"), + ("outcome", "string"), + ("code", "string|null"), + ("output_bytes", "number"), + ], + ), + ( + EntryKind::RequestManifest { + request_id: request, + model: "m".to_owned(), + prompt_version: "v".to_owned(), + tools: vec!["a".to_owned()], + }, + "request_manifest", + vec![ + ("request_id", "string"), + ("model", "string"), + ("prompt_version", "string"), + ("tools", "array"), + ], + ), + ( + EntryKind::ContextCompacted { + summary: "s".to_owned(), + compacted_messages: 2, + model: "m".to_owned(), + }, + "context_compacted", + vec![ + ("summary", "string"), + ("compacted_messages", "number"), + ("model", "string"), + ], + ), + (EntryKind::SessionArchived, "session_archived", Vec::new()), + (EntryKind::SessionRestored, "session_restored", Vec::new()), + ]; + let ts = fs::read_to_string(TYPES_TS).expect("types.ts"); + for (kind, name, fields) in entries { + let value = json_of(&TranscriptEntry { + seq: 3, + timestamp: "2026-01-01T00:00:00Z".to_owned(), + kind, + }); + let mut expected = vec![ + ("seq", "number"), + ("timestamp", "string"), + ("kind", "string"), + ]; + expected.extend(fields); + assert_shape(&value, &expected); + assert_eq!(value["kind"], name); + // The TypeScript variant declares the same fields. + let declared = variant_fields(&ts, name); + let serialized: BTreeSet = + value.as_object().expect("object").keys().cloned().collect(); + assert_eq!(declared, serialized, "TypeScript variant {name}"); + } +} + +/// The field names that `types.ts` declares for the transcript variant `kind`, +/// including the shared `seq` and `timestamp`. +fn variant_fields(ts: &str, kind: &str) -> BTreeSet { + let marker = format!("kind: \"{kind}\""); + let at = ts.find(&marker).unwrap_or_else(|| panic!("variant {kind}")); + let start = ts[..at].rfind('{').expect("opening brace"); + let end = at + ts[at..].find('}').expect("closing brace"); + let mut names: BTreeSet = declared_names(&ts[start + 1..end]); + names.insert("seq".to_owned()); + names.insert("timestamp".to_owned()); + names +} + +/// Names before a colon in a `;`, `,`, or newline separated field list. +fn declared_names(body: &str) -> BTreeSet { + body.split([';', ',', '\n']) + .filter_map(|piece| { + let (name, _) = piece.trim().split_once(':')?; + let name = name.trim().trim_end_matches('?'); + (!name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')) + .then(|| name.to_owned()) + }) + .collect() +} + +/// The field names of `export interface `. +fn interface_fields(ts: &str, name: &str) -> BTreeSet { + let header = format!("export interface {name} {{"); + let start = ts + .find(&header) + .unwrap_or_else(|| panic!("interface {name}")); + let body = &ts[start + header.len()..]; + let end = body.find("\n}").expect("end of interface"); + declared_names(&body[..end]) +} + +fn keys_of(value: &Value) -> BTreeSet { + value.as_object().expect("object").keys().cloned().collect() +} + +#[test] +fn every_serialized_interface_has_exactly_the_fields_types_ts_declares() { + let ts = fs::read_to_string(TYPES_TS).expect("types.ts"); + let error = json_of(&error_value()); + let tree = json_of(&SessionTree { + groups: vec![WorkspaceGroup { + group_id: "g".to_owned(), + folder_display: "f".to_owned(), + sessions: vec![summary(None, None)], + }], + indexed_sessions: 0, + skipped_entries: 0, + }); + let hit = json_of(&SearchHit { + session: summary(None, None), + folder_display: "f".to_owned(), + seq: None, + role: SearchRoleDto::Title, + snippet: vec![SnippetPart { + text: "t".to_owned(), + matched: false, + }], + }); + let page = json_of(&TranscriptPage { + session: summary(None, None), + folder_display: "f".to_owned(), + entries: Vec::new(), + next_after_seq: None, + incomplete_tail: false, + }); + let status = json_of(&ChatStatus { + session_id: id(), + state: ChatState::Idle, + outcome: Some(outcome()), + error: None, + }); + let settings = json_of(&ChatSettingsDto { + endpoint: "e".to_owned(), + model: "m".to_owned(), + context_tokens: 1, + max_output_tokens: 1, + instructions: String::new(), + compact_threshold: 1, + auto_compact: false, + }); + let workspaces = json_of(&WorkspaceList { + development_root_set: false, + workspaces: vec![WorkspaceChoice { + workspace_id: id(), + folder_display: "f".to_owned(), + model_tools: false, + read: false, + write: false, + execute: false, + }], + }); + let detail = json_of(&ApprovalDetail { + approval: approval_view(false), + binding: Binding::NotFound, + tool: None, + fields: vec![ApprovalField { + label: "l".to_owned(), + value: "v".to_owned(), + multiline: true, + }], + review_token: None, + }); + let list = json_of(&ApprovalList { + now_unix: 1, + approvals: vec![approval_view(false)], + }); + let checks: Vec<(&str, BTreeSet)> = vec![ + ("IpcError", keys_of(&error)), + ("SessionSummary", keys_of(&tree["groups"][0]["sessions"][0])), + ("WorkspaceGroup", keys_of(&tree["groups"][0])), + ("SessionTree", keys_of(&tree)), + ("SnippetPart", keys_of(&hit["snippet"][0])), + ("SearchHit", keys_of(&hit)), + ( + "SearchResult", + keys_of(&json_of(&SearchResult { + hits: Vec::new(), + truncated: false, + })), + ), + ("TranscriptPage", keys_of(&page)), + ("ChatSettings", keys_of(&settings)), + ("ContextMeter", keys_of(&status["outcome"]["context"])), + ("ChatOutcome", keys_of(&status["outcome"])), + ("ChatStatus", keys_of(&status)), + ("WorkspaceChoice", keys_of(&workspaces["workspaces"][0])), + ("WorkspaceList", keys_of(&workspaces)), + ("ApprovalView", keys_of(&list["approvals"][0])), + ("ApprovalList", keys_of(&list)), + ("ApprovalField", keys_of(&detail["fields"][0])), + ("ApprovalDetail", keys_of(&detail)), + ]; + for (name, serialized) in checks { + assert_eq!(interface_fields(&ts, name), serialized, "interface {name}"); + } +} + +#[test] +fn closed_unions_serialize_to_the_values_types_ts_declares() { + let ts = fs::read_to_string(TYPES_TS).expect("types.ts"); + let union = |name: &str| -> BTreeSet { + let header = format!("export type {name} = "); + let start = ts.find(&header).expect("type alias") + header.len(); + let end = start + ts[start..].find(';').expect("semicolon"); + ts[start..end] + .split('|') + .map(|part| part.trim().trim_matches('"').to_owned()) + .collect() + }; + let strings = |values: Vec| -> BTreeSet { + values + .iter() + .map(|value| value.as_str().expect("string").to_owned()) + .collect() + }; + assert_eq!( + union("SessionStatus"), + strings( + [ + SessionStatus::Idle, + SessionStatus::Running, + SessionStatus::TurnOpen, + SessionStatus::Busy, + SessionStatus::Invalid + ] + .iter() + .map(json_of) + .collect() + ) + ); + assert_eq!( + union("ApprovalState"), + strings( + [ + ApprovalStateDto::Pending, + ApprovalStateDto::Approved, + ApprovalStateDto::Denied, + ApprovalStateDto::Consumed, + ApprovalStateDto::Expired + ] + .iter() + .map(json_of) + .collect() + ) + ); + assert_eq!( + union("ApprovalOperation"), + strings( + [ + ApprovalOperationDto::Command, + ApprovalOperationDto::FileWrite, + ApprovalOperationDto::GitPush, + ApprovalOperationDto::Network + ] + .iter() + .map(json_of) + .collect() + ) + ); + assert_eq!( + union("TreeView"), + strings( + [TreeView::Active, TreeView::Archived] + .iter() + .map(json_of) + .collect() + ) + ); + assert_eq!( + union("TreeSort"), + strings( + [TreeSort::Updated, TreeSort::Created, TreeSort::Title] + .iter() + .map(json_of) + .collect() + ) + ); + let chat_states: BTreeSet = strings( + [ + ChatState::Idle, + ChatState::Running, + ChatState::Cancelling, + ChatState::Completed, + ChatState::Failed, + ] + .iter() + .map(json_of) + .collect(), + ); + assert!( + ts.contains("state: \"idle\" | \"running\" | \"cancelling\" | \"completed\" | \"failed\";") + ); + assert_eq!(chat_states.len(), 5); + assert!(ts.contains("binding: \"exact\" | \"stale\" | \"not_found\";")); + assert_eq!(json_of(&Binding::NotFound), "not_found"); + assert!(ts.contains("role: \"user\" | \"assistant\" | \"tool\" | \"summary\" | \"title\";")); + assert_eq!(json_of(&SearchRoleDto::Summary), "summary"); +} + +#[test] +fn the_summary_command_returns_the_object_the_client_declares() { + let client = fs::read_to_string(CLIENT_TS).expect("client.ts"); + assert!(client.contains("invokeCommand<{ pending: number }>(\"approvals_summary\"")); + assert_shape( + &json_of(&ApprovalsSummary { pending: 0 }), + &[("pending", "number")], + ); +} + +#[test] +fn requests_match_the_argument_objects_the_client_sends() { + let client = fs::read_to_string(CLIENT_TS).expect("client.ts"); + let approvals = fs::read_to_string(APPROVALS_CLIENT_TS).expect("approvals_client.ts"); + let session = id(); + + assert!(client.contains("\"session_tree\", { view, sort, descending }")); + let tree: TreeRequest = wire(json!({"view": "active", "sort": "updated", "descending": true})); + assert_eq!(tree.sort, TreeSort::Updated); + + assert!(client.contains("\"session_search\", { query, view, limit }")); + let search: SearchRequest = wire(json!({"query": "q", "view": "archived", "limit": 20})); + assert_eq!(search.view, TreeView::Archived); + + assert!(client.contains("session_id: sessionId,\n after_seq: afterSeq,\n limit,")); + let transcript: TranscriptRequest = + wire(json!({"session_id": session, "after_seq": null, "limit": 50})); + assert_eq!(transcript.after_seq, None); + let transcript: TranscriptRequest = + wire(json!({"session_id": session, "after_seq": 7, "limit": 50})); + assert_eq!(transcript.after_seq, Some(7)); + + assert!(client.contains("\"session_archive\", { session_id: sessionId }")); + let by_session: SessionRequest = wire(json!({"session_id": session})); + assert_eq!(by_session.session_id, session); + + assert!(client.contains("{ workspace_id: workspaceId, title }")); + let create: SessionCreateRequest = wire(json!({"workspace_id": session, "title": "t"})); + assert_eq!(create.title, "t"); + + assert!(client.contains("{ session_id: sessionId, message, settings }")); + let send: ChatSendRequest = wire(json!({ + "session_id": session, + "message": "m", + "settings": common::settings_json("http://127.0.0.1:1/v1"), + })); + assert_eq!(send.message, "m"); + + assert!(approvals.contains("\"approval_detail\", { id }")); + let detail: ApprovalRequest = wire(json!({"id": session})); + assert_eq!(detail.id, session); + + assert!( + approvals.contains("writers_stopped: writersStopped,\n review_token: reviewToken,") + ); + let decide: ApprovalDecideRequest = wire(json!({ + "id": session, + "decision": "approve", + "writers_stopped": true, + "review_token": null, + })); + assert_eq!(decide.decision, Decision::Approve); + assert_eq!(decide.review_token, None); +} + +#[test] +fn requests_reject_unknown_fields_and_wrong_values() { + let session = id(); + let reject = |result: Result, what: &str| { + assert!(result.is_err(), "{what} must be rejected"); + }; + macro_rules! parse { + ($type:ty, $value:expr) => { + serde_json::from_value::<$type>($value).map(|_| Value::Null) + }; + } + reject( + parse!( + TreeRequest, + json!({"view": "active", "sort": "updated", "descending": false, "path": "/x"}) + ), + "an extra tree field", + ); + reject( + parse!( + TreeRequest, + json!({"view": "all", "sort": "updated", "descending": false}) + ), + "a wrong view", + ); + reject( + parse!( + TreeRequest, + json!({"view": "active", "sort": "size", "descending": false}) + ), + "a wrong sort", + ); + reject( + parse!( + SearchRequest, + json!({"query": "q", "view": "active", "limit": -1}) + ), + "a negative limit", + ); + reject( + parse!(SessionRequest, json!({"session_id": "not-a-uuid"})), + "a malformed id", + ); + reject( + parse!( + SessionRequest, + json!({"session_id": session, "path": "C:\\x"}) + ), + "a path field", + ); + reject( + parse!( + SessionCreateRequest, + json!({"workspace_id": session, "title": "t", "path": "/x"}) + ), + "a path on session_create", + ); + reject( + parse!( + ApprovalDecideRequest, + json!({ + "id": session, "decision": "maybe", "writers_stopped": false, "review_token": null + }) + ), + "a wrong decision", + ); + reject( + parse!( + ApprovalDecideRequest, + json!({ + "id": session, "decision": "deny", "writers_stopped": false, "review_token": null, "extra": 1 + }) + ), + "an extra decide field", + ); + reject( + parse!( + ChatSendRequest, + json!({ + "session_id": session, "message": "m", "settings": {"endpoint": "e"} + }) + ), + "incomplete settings", + ); + let mut settings = common::settings_json("e"); + settings["secret"] = json!("x"); + reject( + parse!( + ChatSendRequest, + json!({"session_id": session, "message": "m", "settings": settings}) + ), + "an extra settings field", + ); +} + +#[test] +fn a_session_create_request_has_no_way_to_carry_a_path() { + // Compile-time: this destructuring lists every field of the request type. + let SessionCreateRequest { + workspace_id: _, + title: _, + } = wire(json!({"workspace_id": Uuid::new_v4(), "title": "t"})); +} + +#[test] +fn an_error_serializes_code_before_message() { + // The serialized form of an error is stable text with no path markers. + let text = text_of(&error_value()); + assert!(text.starts_with("{\"code\":\"invalid_request\",\"message\":")); +} diff --git a/crates/omgcode-app/tests/stored_credential.rs b/crates/omgcode-app/tests/stored_credential.rs new file mode 100644 index 0000000..db195d0 --- /dev/null +++ b/crates/omgcode-app/tests/stored_credential.rs @@ -0,0 +1,426 @@ +//! The local adapter's endpoint and token from a stored provider profile +//! (ADR-0047, ADR-0048), against the real settings store, the real native +//! credential store, and the scripted loopback model server. +//! +//! On Linux these need an unlocked Secret Service default collection and fail +//! without one. Every item a test stores is deleted when its `Vault` drops. Test +//! tokens are obviously fake. + +use std::fs; + +use omgcode_app::{ + AppError, ChatHooks, ChatSettings, ChatTurnInput, ConnectionSource, ErrorKind, LmToken, + create_session, read_lm_token, redact_known_secret, resolve_local_connection, run_chat_turn, +}; +use omgcode_core::control::ControlStore; +use serde_json::json; + +mod common; +use common::credential::Vault; +use common::{Fixture, MockServer, NoCallServer, Step, text, text_only}; + +const STORED: &str = "fake-stored-token-4c19e7a2"; +const ROTATED: &str = "fake-rotated-token-90b3d1f6"; +/// An endpoint for a profile whose test sends no request. +const RECORDED: &str = "http://127.0.0.1:1/v1"; + +fn profile(name: &str) -> ConnectionSource { + ConnectionSource::Profile(name.to_owned()) +} + +/// The token of a resolution, so each test can keep asserting on it alone. +fn resolve_token( + data: &std::path::Path, + source: &ConnectionSource, +) -> Result, AppError> { + resolve_local_connection(data, source).map(|connection| connection.token) +} + +/// The failure of a resolution, which must never expose `STORED`. +fn refusal(fixture: &Fixture, source: &ConnectionSource) -> (String, String) { + let error = resolve_local_connection(&fixture.data, source) + .map(|connection| { + ( + connection.endpoint, + connection.token.map(|token| token.expose().to_owned()), + ) + }) + .expect_err("the source is refused"); + assert_eq!(error.kind(), ErrorKind::Credential); + assert_eq!(error.code(), "credential"); + for text in [error.message().to_owned(), format!("{error:?}")] { + assert!(!text.contains(STORED), "{text}"); + } + (error.code().to_owned(), error.message().to_owned()) +} + +#[test] +fn the_environment_source_is_the_existing_environment_read() { + // No settings store exists, and none is needed or touched. + let fixture = Fixture::bare(); + let source = ConnectionSource::Environment { + endpoint: "http://127.0.0.1:4321/v1".to_owned(), + }; + let connection = resolve_local_connection(&fixture.data, &source).expect("environment"); + assert_eq!(connection.endpoint, "http://127.0.0.1:4321/v1"); + assert_eq!( + connection.token.map(|token| token.expose().to_owned()), + read_lm_token().expect("environment token") + ); + assert!(!fixture.data.join("control-v1.jsonl").exists()); +} + +#[test] +fn a_profile_source_returns_its_endpoint_and_the_stored_token_and_none_for_a_keyless_profile() { + let fixture = Fixture::new(); + let mut vault = Vault::new(&fixture.data); + vault.add_profile_at("lms", "lm_studio", "api_key", RECORDED); + vault.add_profile_at("open", "lm_studio", "none", "http://[::1]:2/v1/"); + vault.bind("lms", STORED.as_bytes()); + + let connection = resolve_local_connection(&fixture.data, &profile("LMS")).expect("stored"); + assert_eq!(connection.endpoint, RECORDED, "exactly the recorded text"); + let token = connection.token.expect("a token"); + assert_eq!(token.expose(), STORED); + assert_eq!(format!("{token:?}"), "LmToken()"); + + // A `none` profile selects no token at all, and still has its endpoint. + let open = resolve_local_connection(&fixture.data, &profile("open")).expect("none profile"); + assert_eq!(open.endpoint, "http://[::1]:2/v1/"); + assert!(open.token.is_none()); +} + +#[test] +fn every_refusal_is_a_credential_error_with_its_cause_code() { + let fixture = Fixture::bare(); + // No settings store exists. + let (_, message) = refusal(&fixture, &profile("lms")); + assert!(message.contains("no settings store exists"), "{message}"); + assert!( + !fixture.data.join("control-v1.jsonl").exists(), + "resolving creates no settings store" + ); + + let fixture = Fixture::new(); + let mut vault = Vault::new(&fixture.data); + vault.add_profile_at("lms", "lm_studio", "api_key", RECORDED); + vault.add_profile_at("unbound", "lm_studio", "api_key", RECORDED); + vault.add_profile("openai-key", "openai", "api_key"); + vault.add_profile("plan", "anthropic", "subscription"); + vault.bind("openai-key", STORED.as_bytes()); + let lost = vault.bind("lms", STORED.as_bytes()); + + let (_, message) = refusal(&fixture, &profile("missing")); + assert!(message.starts_with("unknown_profile:"), "{message}"); + // An OpenAI key is never read for the local adapter. + let (_, message) = refusal(&fixture, &profile("openai-key")); + assert!(message.starts_with("wrong_provider:"), "{message}"); + let (_, message) = refusal(&fixture, &profile("plan")); + assert!(message.starts_with("wrong_provider:"), "{message}"); + let (_, message) = refusal(&fixture, &profile("unbound")); + assert!(message.starts_with("not_bound:"), "{message}"); + + // The native item disappears while its binding stays recorded. + vault.delete_native(&lost); + let (_, message) = refusal(&fixture, &profile("lms")); + assert!(message.starts_with("credential_not_found:"), "{message}"); + assert!(message.contains(lost.as_str()), "{message}"); +} + +/// A profile recorded without an endpoint is refused whatever its +/// authentication mode, before any native store access: the bound credential's +/// native item is gone, so a load would have reported that instead. +#[test] +fn a_profile_without_an_endpoint_is_refused_with_endpoint_not_recorded() { + let fixture = Fixture::new(); + let mut vault = Vault::new(&fixture.data); + vault.add_profile("old-none", "lm_studio", "none"); + vault.add_profile("old-key", "lm_studio", "api_key"); + vault.add_profile("old-unbound", "lm_studio", "api_key"); + let id = vault.bind("old-key", STORED.as_bytes()); + vault.delete_native(&id); + for name in ["old-none", "old-key", "old-unbound", "OLD-KEY"] { + let (_, message) = refusal(&fixture, &profile(name)); + assert!( + message.starts_with("endpoint_not_recorded:"), + "{name}: {message}" + ); + assert!(message.contains("no recorded endpoint"), "{message}"); + } +} + +#[test] +fn a_busy_settings_store_is_refused_without_a_retry() { + let fixture = Fixture::new(); + let mut vault = Vault::new(&fixture.data); + vault.add_profile_at("lms", "lm_studio", "api_key", RECORDED); + vault.bind("lms", STORED.as_bytes()); + + let holder = ControlStore::open(&fixture.data).expect("another holder"); + let (_, message) = refusal(&fixture, &profile("lms")); + assert!(message.contains("in use by another process"), "{message}"); + drop(holder); + + // Released, the same resolution succeeds. + assert!( + resolve_token(&fixture.data, &profile("lms")) + .expect("resolves") + .is_some() + ); +} + +#[test] +fn a_stored_secret_that_is_not_utf8_text_is_refused() { + let fixture = Fixture::new(); + let mut vault = Vault::new(&fixture.data); + vault.add_profile_at("lms", "lm_studio", "api_key", RECORDED); + vault.bind("lms", &[0xff, 0xfe, 0x41]); + let (_, message) = refusal(&fixture, &profile("lms")); + assert!(message.starts_with("credential_invalid:"), "{message}"); +} + +#[test] +fn the_stored_token_is_sent_as_the_bearer_and_recorded_nowhere() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let mut vault = Vault::new(&fixture.data); + let server = MockServer::start(vec![Step::reply(text(&format!( + "I will not repeat {STORED}" + )))]); + vault.add_profile_at("lms", "lm_studio", "api_key", &server.endpoint); + vault.bind("lms", STORED.as_bytes()); + let connection = resolve_local_connection(&fixture.data, &profile("lms")).expect("connection"); + let settings = ChatSettings::new(connection.endpoint, "fixture", 131_072); + let token = connection.token; + // The CLI removes the token from a title before it creates the session. + let title = redact_known_secret( + &format!("title {STORED}"), + token.as_ref().map(LmToken::expose), + ); + let mut journal = create_session(&fixture.data, &workspace.path, &title).expect("session"); + let path = journal.path().to_owned(); + + let output = run_chat_turn( + &mut journal, + &fixture.data, + &ChatTurnInput { + message: &format!("please use {STORED} here"), + settings: &settings, + token: token.as_ref().map(LmToken::expose), + }, + &mut ChatHooks::default(), + ) + .expect("turn"); + drop(journal); + + let (served, requests) = server.finish_recorded(); + assert_eq!(served, 1); + assert_eq!( + requests[0].header("authorization"), + Some(format!("Bearer {STORED}")) + ); + assert!( + !requests[0].body.contains(STORED), + "the request body carries no token" + ); + assert!(requests[0].body.contains("[REDACTED]")); + + let returned = serde_json::to_string(&output).expect("output serializes"); + assert!(!returned.contains(STORED), "{returned}"); + assert!(returned.contains("[REDACTED]"), "{returned}"); + + let bytes = fs::read(&path).expect("journal"); + let journal_text = String::from_utf8_lossy(&bytes); + assert!(!journal_text.contains(STORED), "the journal holds no token"); + assert!(journal_text.contains("[REDACTED]")); + // The journal names neither the profile nor the credential. + assert!(!journal_text.contains("lms\"")); + + // The control journal never held the secret either. + let control = fs::read(fixture.data.join("control-v1.jsonl")).expect("control journal"); + assert!( + !String::from_utf8_lossy(&control).contains(STORED), + "the settings journal holds no token" + ); +} + +/// A turn that uses the resolved connection reaches the profile's endpoint +/// with the stored bearer, and no other listener sees a request. +#[test] +fn a_turn_with_a_profile_sends_to_the_profile_endpoint_and_nowhere_else() { + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", text_only()); + let mut vault = Vault::new(&fixture.data); + let profile_server = MockServer::start(vec![Step::reply(text("from the profile endpoint"))]); + let other = NoCallServer::start(); + assert_ne!(profile_server.endpoint, other.endpoint); + vault.add_profile_at("lms", "lm_studio", "api_key", &profile_server.endpoint); + vault.bind("lms", STORED.as_bytes()); + + let connection = resolve_local_connection(&fixture.data, &profile("lms")).expect("connection"); + assert_eq!(connection.endpoint, profile_server.endpoint); + let settings = ChatSettings::new(connection.endpoint, "fixture", 131_072); + let mut journal = create_session(&fixture.data, &workspace.path, "endpoint").expect("session"); + run_chat_turn( + &mut journal, + &fixture.data, + &ChatTurnInput { + message: "hello", + settings: &settings, + token: connection.token.as_ref().map(LmToken::expose), + }, + &mut ChatHooks::default(), + ) + .expect("turn"); + drop(journal); + + let (served, requests) = profile_server.finish_recorded(); + assert_eq!(served, 1); + assert_eq!( + requests[0].header("authorization"), + Some(format!("Bearer {STORED}")) + ); + other.assert_untouched(); +} + +#[test] +fn a_rotated_credential_is_used_by_the_next_resolution() { + let fixture = Fixture::new(); + let mut vault = Vault::new(&fixture.data); + vault.add_profile_at("lms", "lm_studio", "api_key", RECORDED); + vault.bind("lms", STORED.as_bytes()); + let first = resolve_token(&fixture.data, &profile("lms")) + .expect("first") + .expect("token"); + assert_eq!(first.expose(), STORED); + vault.bind("lms", ROTATED.as_bytes()); + let second = resolve_token(&fixture.data, &profile("lms")) + .expect("second") + .expect("token"); + assert_eq!(second.expose(), ROTATED); +} + +/// On Windows the native store refuses a data root that grants access to a +/// principal other than the current user, the system, and administrators, so +/// such a root makes `CredentialStore::open` fail. The early outcomes are +/// decided from the settings store alone and must still report their own +/// cause, and a `none` profile must still resolve. +/// +/// The test grants the built-in Users group read access itself. Whether a +/// temporary directory inherits such a grant depends on the host: it does on +/// some machines and not on others, so the inherited ACL alone proves nothing. +#[cfg(windows)] +#[test] +fn early_outcomes_need_no_native_store() { + let temporary = tempfile::tempdir().expect("temporary directory"); + let data = temporary.path().join("application-data"); + fs::create_dir(&data).expect("data root"); + // S-1-5-32-545 is the built-in Users group, named by its identifier so + // the command does not depend on the display language. + let granted = std::process::Command::new("icacls") + .arg(&data) + .args(["/grant", "*S-1-5-32-545:(RX)"]) + .output() + .expect("icacls"); + assert!(granted.status.success(), "icacls fixture failed"); + let vault = Vault::new(&data); + vault.add_profile_at("open", "lm_studio", "none", RECORDED); + vault.add_profile_at("unbound", "lm_studio", "api_key", RECORDED); + vault.add_profile("old-key", "lm_studio", "api_key"); + vault.add_profile("openai-key", "openai", "api_key"); + + // Prove the premise: the native store cannot open under this root. + let premise = omgcode_core::control::CredentialStore::open(&data).err(); + assert!( + premise.is_some_and(|error| error.to_string().contains("untrusted principal")), + "the native store must refuse this data root" + ); + + let open = resolve_local_connection(&data, &profile("open")).expect("none profile"); + assert_eq!(open.endpoint, RECORDED); + assert!(open.token.is_none()); + for (name, expected) in [ + ("missing", "unknown_profile:"), + ("openai-key", "wrong_provider:"), + ("old-key", "endpoint_not_recorded:"), + ("unbound", "not_bound:"), + ] { + let error = resolve_token(&data, &profile(name)).expect_err("refused"); + assert_eq!(error.code(), "credential"); + assert!(error.message().starts_with(expected), "{}", error.message()); + } +} + +/// The desktop launch check and the service both accept a `none` profile +/// without any native store. The shell's own startup code needs `omgcode-core` +/// to build the settings fixture, so this runs at the service level. +#[test] +fn a_keyless_profile_passes_the_launch_check_and_opens_the_service() { + use omgcode_app::TokenSource; + use omgcode_app::desktop::{DesktopConfig, DesktopService}; + let fixture = Fixture::new(); + let vault = Vault::new(&fixture.data); + vault.add_profile_at("open", "lm_studio", "none", "http://127.0.0.1:1/v1"); + let launch = + resolve_local_connection(&fixture.data, &profile("open")).expect("the launch check"); + assert!(launch.token.is_none()); + DesktopService::open(DesktopConfig { + data_root: fixture.data.clone(), + endpoint: launch.endpoint, + token_source: TokenSource::Profile("open".to_owned()), + stream: true, + }) + .expect("the service opens"); +} + +/// The stored token is refused inside a tool call exactly like the +/// environment token: the call is not run and the turn records no token. +#[test] +fn a_tool_call_containing_the_stored_token_is_refused() { + use common::{read_tools, tool_call}; + let fixture = Fixture::new(); + let workspace = fixture.workspace("project", read_tools()); + fs::write(workspace.path.join("note.txt"), "note\n").expect("note"); + let mut vault = Vault::new(&fixture.data); + let server = MockServer::start(vec![Step::reply(tool_call( + "call-1", + "workspace_read_file", + &json!({"path": "note.txt", "note": STORED}), + ))]); + vault.add_profile_at("lms", "lm_studio", "api_key", &server.endpoint); + vault.bind("lms", STORED.as_bytes()); + let connection = resolve_local_connection(&fixture.data, &profile("lms")).expect("connection"); + let settings = ChatSettings::new(connection.endpoint, "fixture", 131_072); + let token = connection.token; + let mut journal = create_session(&fixture.data, &workspace.path, "tool call").expect("session"); + let path = journal.path().to_owned(); + let result = run_chat_turn( + &mut journal, + &fixture.data, + &ChatTurnInput { + message: "read it", + settings: &settings, + token: token.as_ref().map(LmToken::expose), + }, + &mut ChatHooks::default(), + ); + drop(journal); + assert_eq!(server.finish(), 1, "the call was made, then refused"); + let rendered = match &result { + Ok(output) => serde_json::to_string(output).expect("output"), + Err(error) => format!("{error}"), + }; + assert!(!rendered.contains(STORED), "{rendered}"); + let error = result.as_ref().expect_err("the turn must not complete"); + assert!( + error + .message() + .contains("tool call containing the configured server token"), + "{error}" + ); + let bytes = fs::read(&path).expect("journal"); + assert!( + !String::from_utf8_lossy(&bytes).contains(STORED), + "the journal holds no token" + ); +} diff --git a/crates/omgcode-cli/Cargo.toml b/crates/omgcode-cli/Cargo.toml index df79cd7..38fd3ba 100644 --- a/crates/omgcode-cli/Cargo.toml +++ b/crates/omgcode-cli/Cargo.toml @@ -12,9 +12,9 @@ name = "omgcode" path = "src/main.rs" [dependencies] +omgcode-app = { path = "../omgcode-app" } omgcode-core = { path = "../omgcode-core" } clap.workspace = true -directories.workspace = true serde.workspace = true serde_json.workspace = true uuid.workspace = true diff --git a/crates/omgcode-cli/src/context_cli.rs b/crates/omgcode-cli/src/context_cli.rs deleted file mode 100644 index 52c080d..0000000 --- a/crates/omgcode-cli/src/context_cli.rs +++ /dev/null @@ -1,76 +0,0 @@ -//! CLI glue for bounded conversation compaction. -//! -//! The core owns the cutoff, journal event, and summary validation. This module -//! supplies the local-model summarizer and applies the configured secret -//! redaction before the summary is recorded or shown. - -use omgcode_core::context::{ - COMPACTION_INSTRUCTIONS, CompactionLimits, CompactionOutcome, ContextBudget, ContextError, - MAX_SUMMARY_BYTES, SummaryReply, compact, -}; -use omgcode_core::prompt::{self, ConversationMessage}; -use omgcode_core::provider::LocalModelClient; -use omgcode_core::session::SessionJournal; -use std::path::Path; - -use crate::{CliError, model_tools, redact_known_secret}; - -/// Most output tokens reserved for one summary request. -const MAX_SUMMARY_OUTPUT_TOKENS: u64 = 2048; - -/// Output reserve for an automatic summary request: a quarter of the context, -/// at most 2048 tokens and at least one. -pub(crate) fn summary_output_tokens(context_tokens: u64) -> u32 { - u32::try_from((context_tokens / 4).clamp(1, MAX_SUMMARY_OUTPUT_TOKENS)).unwrap_or(1) -} - -/// Compact the active context with the selected local model. -/// -/// `summary_budget` sizes the summarizer chunks; its `max_output_tokens` must -/// be the summary client's output reserve. Live `model_tools` and `read` -/// settings for the session's workspace are read before the summarizer input -/// is built and again before each request that carries tool-derived content; -/// a withdrawal stops compaction with a `permission` error and records nothing. -pub(crate) fn compact_journal( - journal: &mut SessionJournal, - data_root: &Path, - client: &LocalModelClient, - token: Option<&str>, - summary_budget: ContextBudget, -) -> Result { - let model = client.model().to_owned(); - let workspace = journal.snapshot().workspace_id; - let schema_version = journal.schema_version(); - compact( - journal, - CompactionLimits { - max_chunk_bytes: summary_budget.summarizer_chunk_bytes(), - max_summary_bytes: MAX_SUMMARY_BYTES, - }, - model, - // Unreadable settings fail closed: no tool data reaches the summarizer. - || model_tools::tool_replay_allowed(data_root, workspace, schema_version).unwrap_or(false), - |history: &[ConversationMessage]| { - let redacted: Vec = history - .iter() - .map(|message| ConversationMessage { - role: message.role, - content: redact_known_secret(&message.content, token), - }) - .collect(); - let messages = prompt::assemble(COMPACTION_INSTRUCTIONS, &redacted) - .map_err(|_| ContextError::TooLarge)?; - let reply = client.complete(&messages).map_err(ContextError::Provider)?; - let usage = reply.usage.as_ref(); - Ok(SummaryReply { - text: redact_known_secret(&reply.content, token), - prompt_tokens: usage.and_then(|usage| usage.prompt_tokens), - completion_tokens: usage.and_then(|usage| usage.completion_tokens), - }) - }, - ) - .map_err(|error| match error { - ContextError::AuthorityWithdrawn => CliError::new("permission", error.to_string()), - error => CliError::new("context", error.to_string()), - }) -} diff --git a/crates/omgcode-cli/src/control_cli.rs b/crates/omgcode-cli/src/control_cli.rs index 5f234e1..dcae3b6 100644 --- a/crates/omgcode-cli/src/control_cli.rs +++ b/crates/omgcode-cli/src/control_cli.rs @@ -88,7 +88,8 @@ pub(crate) enum WorkspaceCommand { #[arg(long)] revision: u64, }, - /// Record a provider connection shape. No credential or adapter is used. + /// Record a provider connection shape, optionally with its endpoint. No + /// credential is stored; the local lm_studio adapter reads a selected profile. ProviderProfileAdd(ProviderProfileAddArgs), /// Show one recorded provider profile. ProviderProfileShow { name: String }, @@ -103,7 +104,8 @@ pub(crate) enum WorkspaceCommand { /// /// The secret is read from standard input only, up to 16 KiB; one /// trailing newline is removed. A previously bound credential is deleted - /// after the new binding is recorded. No adapter uses the credential yet. + /// after the new binding is recorded. The local lm_studio adapter reads it + /// for a selected profile. ProviderCredentialSet { name: String, #[arg(long)] @@ -128,6 +130,11 @@ pub(crate) struct ProviderProfileAddArgs { /// One of: subscription, api_key, none. #[arg(long)] auth: String, + /// The endpoint the profile's adapter uses. Only lm_studio accepts one: + /// http://:/v1. It cannot be changed later; remove the + /// profile and add it again. + #[arg(long)] + endpoint: Option, #[arg(long)] revision: u64, } @@ -491,6 +498,7 @@ fn provider_profile_add(args: ProviderProfileAddArgs, data_root: &Path) -> Resul name: args.name, kind: args.kind, auth: args.auth, + endpoint: args.endpoint, }, args.revision, ) diff --git a/crates/omgcode-cli/src/main.rs b/crates/omgcode-cli/src/main.rs index 6c2e643..fd9822f 100644 --- a/crates/omgcode-cli/src/main.rs +++ b/crates/omgcode-cli/src/main.rs @@ -2,33 +2,28 @@ #![forbid(unsafe_code)] mod approvals_cli; -mod context_cli; mod control_cli; mod credential_cli; mod file_cli; -mod model_tools; mod packages_cli; mod run_cli; +mod stream_cli; use clap::{Args, Parser, Subcommand}; -use directories::ProjectDirs; -use omgcode_core::context::{ - ContextBudget, DEFAULT_COMPACT_THRESHOLD, estimate_history_input_tokens, -}; -use omgcode_core::control::ControlStore; -use omgcode_core::prompt::{ - self, BASE_PROMPT, BASE_PROMPT_VERSION, ConversationMessage, ConversationRole, PromptError, +use omgcode_app::{ + AppError, ChatClients, ChatHooks, ChatSettings, ChatStreamEvent, ChatTurnInput, + ConnectionSource, LmToken, LocalConnection, MODEL_TOOLS_SUPPORTED, compact_journal, + create_session, data_root, read_lm_token, redact_known_secret, resolve_local_connection, + run_chat_turn_with_clients, }; +use omgcode_core::context::{ContextBudget, DEFAULT_COMPACT_THRESHOLD}; +use omgcode_core::prompt::{self, BASE_PROMPT, BASE_PROMPT_VERSION}; use omgcode_core::provider::{LocalModelClient, LocalModelConfig}; -use omgcode_core::session::{ - MessageRole, NativePath, RequestManifest, SessionError, SessionJournal, -}; -use omgcode_core::turn::replay_active; +use omgcode_core::session::{NativePath, SessionJournal}; use serde::Serialize; use serde_json::json; -use std::env; use std::io::{self, Write}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::ExitCode; /// Whether trusted runs and toolchain profiles exist on this platform. @@ -117,7 +112,13 @@ enum SessionCommand { Compact { #[command(flatten)] locator: JournalArgs, - #[arg(long, default_value = "http://127.0.0.1:1234/v1")] + /// The loopback endpoint. Not allowed with --provider-profile, whose + /// recorded endpoint is the only one a profile uses. + #[arg( + long, + default_value = "http://127.0.0.1:1234/v1", + conflicts_with = "provider_profile" + )] endpoint: String, #[arg(long)] model: String, @@ -125,6 +126,11 @@ enum SessionCommand { context_tokens: u64, #[arg(long, default_value_t = 2048)] summary_tokens: u32, + /// Read the server token from this recorded `lm_studio` provider + /// profile's stored credential instead of OMGCODE_LM_TOKEN. The endpoint + /// then comes from the profile too. + #[arg(long, value_parser = non_empty_name)] + provider_profile: Option, }, } @@ -149,7 +155,13 @@ struct ChatArgs { journal_native: Option, #[arg(long, default_value = "Local model chat")] title: String, - #[arg(long, default_value = "http://127.0.0.1:1234/v1")] + /// The loopback endpoint. Not allowed with --provider-profile, whose + /// recorded endpoint is the only one a profile uses. + #[arg( + long, + default_value = "http://127.0.0.1:1234/v1", + conflicts_with = "provider_profile" + )] endpoint: String, #[arg(long)] model: String, @@ -167,10 +179,29 @@ struct ChatArgs { /// Enable compaction at the effective trigger before a turn. #[arg(long)] auto_compact: bool, + /// Read the server token from this recorded `lm_studio` provider profile's + /// stored credential instead of OMGCODE_LM_TOKEN. The variable is then + /// not read, and the endpoint is the one recorded with the profile. + #[arg(long, value_parser = non_empty_name)] + provider_profile: Option, + /// Stream the model's text to standard error as it arrives. The text is + /// provisional and escaped for display; standard output still carries the + /// one JSON result, and the journal records only the validated reply. + #[arg(long)] + stream: bool, /// Current user request. Quote it as one shell argument. message: String, } +/// Reject an empty profile name. Core validates it against recorded profiles. +fn non_empty_name(value: &str) -> Result { + if value.is_empty() { + Err("the provider profile name must not be empty".to_owned()) + } else { + Ok(value.to_owned()) + } +} + #[derive(Serialize)] struct CliError { code: &'static str, @@ -195,27 +226,10 @@ struct JournalOutput { detail: T, } -/// Visible, conservative context accounting for one chat turn. -#[derive(Clone, Serialize)] -pub(crate) struct ContextMeter { - estimated_input_tokens: u64, - context_tokens: u64, - max_output_tokens: u32, - effective_trigger: u64, - auto_compaction: bool, - estimate_scope: &'static str, - #[serde(skip_serializing_if = "Option::is_none")] - compaction: Option, -} - -/// The active compaction boundary and summary identity. -#[derive(Clone, Serialize)] -struct CompactionMeter { - compacted_messages: u64, - retained_messages: u64, - #[serde(skip_serializing_if = "Option::is_none")] - summarizer_calls: Option, - summary_sha256: String, +impl From for CliError { + fn from(error: AppError) -> Self { + Self::new(error.code(), error.into_message()) + } } impl CliError { @@ -283,7 +297,7 @@ fn run(cli: Cli) -> Result<(), CliError> { "checkpoint_restore": cfg!(any(target_os = "linux", target_os = "windows")), "checkpoint_deletion": cfg!(any(target_os = "linux", target_os = "windows")), "edit_previews": cfg!(any(target_os = "linux", target_os = "windows")), - "model_read_list_tools": model_tools::MODEL_TOOLS_SUPPORTED, + "model_read_list_tools": MODEL_TOOLS_SUPPORTED, "model_command_tool": "approval_required", "model_write_tools": "approval_required", "workspace_container_grants": omgcode_core::control::CONTAINER_GRANTS_SUPPORTED, @@ -293,7 +307,10 @@ fn run(cli: Cli) -> Result<(), CliError> { "package_network_enforcement": false, "package_fetch": true, "provider_profiles": true, + "provider_profile_endpoints": true, "provider_adapters": false, + "stored_credential_adapters": ["lm_studio"], + "streaming_adapters": ["lm_studio"], "provider_credentials": omgcode_core::control::CredentialBackend::native().is_some(), "credential_store": credential_cli::capability(), "approvals": true, @@ -357,6 +374,7 @@ fn run(cli: Cli) -> Result<(), CliError> { model, context_tokens, summary_tokens, + provider_profile, } => compact_session( cli.data_dir, locator, @@ -364,6 +382,7 @@ fn run(cli: Cli) -> Result<(), CliError> { model, context_tokens, summary_tokens, + provider_profile, ), }, Command::Workspace { action } => { @@ -381,32 +400,27 @@ fn run(cli: Cli) -> Result<(), CliError> { /// Validate the provider before opening storage, then attach recovery information. fn chat(data_dir: Option, mut args: ChatArgs) -> Result<(), CliError> { - let token = read_lm_token()?; - let client = LocalModelClient::new( - LocalModelConfig { - base_url: args.endpoint.clone(), - model: args.model.clone(), - context_tokens: args.context_tokens, - max_output_tokens: args.max_output_tokens, - }, - // CLI retains one copy for text redaction; the client owns its header. - token.clone(), - ) - .map_err(provider_error)?; - let summary_tokens = context_cli::summary_output_tokens(args.context_tokens); - let summary_client = LocalModelClient::new( - LocalModelConfig { - base_url: args.endpoint.clone(), - model: args.model.clone(), - context_tokens: args.context_tokens, - max_output_tokens: summary_tokens, - }, - token.clone(), - ) - .map_err(provider_error)?; - args.title = redact_known_secret(&args.title, token.as_deref()); - args.message = redact_known_secret(&args.message, token.as_deref()); - args.instructions = redact_known_secret(&args.instructions, token.as_deref()); + // The connection comes first, so an unreadable selected credential or a + // profile with no endpoint stops the command before the endpoint is + // checked and before any journal exists. With a profile, its recorded + // endpoint is the only endpoint used. + let LocalConnection { endpoint, token } = local_connection( + data_dir.as_deref(), + args.endpoint, + args.provider_profile.as_deref(), + )?; + let token_text = token.as_ref().map(LmToken::expose); + let settings = ChatSettings { + endpoint, + model: args.model, + context_tokens: args.context_tokens, + max_output_tokens: args.max_output_tokens, + instructions: args.instructions, + compact_threshold: args.compact_threshold, + auto_compact: args.auto_compact, + }; + let clients = ChatClients::new(&settings, token_text)?; + let title = redact_known_secret(&args.title, token_text); let path = if args.journal.is_some() || args.journal_native.is_some() { Some(resolve_journal( args.journal.take(), @@ -422,178 +436,36 @@ fn chat(data_dir: Option, mut args: ChatArgs) -> Result<(), CliError> { let workspace = args.workspace.as_deref().ok_or_else(|| { CliError::new("configuration", "A workspace or journal is required") })?; - create_session(&data_root, workspace, &args.title)? + create_session(&data_root, workspace, &title)? } }; let locator = journal_locator(&journal)?; - chat_turn( - &mut journal, - &data_root, - &client, - &summary_client, - args, - token.as_deref(), - ) - .map_err(|error| error.with_session(locator)) -} - -/// Apply the context trigger before a turn and return the visible meter. -fn preflight_context( - journal: &mut SessionJournal, - data_root: &std::path::Path, - args: &ChatArgs, - summary_client: &LocalModelClient, - token: Option<&str>, - fixed_extra_bytes: usize, - tools_enabled: bool, -) -> Result { - if args.compact_threshold == 0 { - return Err(CliError::new( - "configuration", - "The compaction threshold must be a positive token count", - )); - } - let budget = ContextBudget { - threshold: args.compact_threshold, - context_tokens: args.context_tokens, - max_output_tokens: args.max_output_tokens, - }; - // Summary chunks are sized from the summary request's own output reserve. - let summary_budget = ContextBudget { - threshold: u64::MAX, - context_tokens: args.context_tokens, - max_output_tokens: context_cli::summary_output_tokens(args.context_tokens), + let input = ChatTurnInput { + message: &args.message, + settings: &settings, + token: token_text, }; - let (mut estimate, mut assembly_blocked) = estimate_with_user( - journal, - args, - fixed_extra_bytes, - replay_allowed_now(journal, data_root, tools_enabled), - )?; - let mut summarizer_calls = None; - if budget.should_compact(estimate) || assembly_blocked { - if !args.auto_compact { - let reason = if assembly_blocked { - "Active history exceeds the prompt assembly limits".to_owned() - } else { - format!( - "The conservative input estimate ({estimate} tokens) reaches the compaction trigger ({})", - budget.trigger() - ) - }; - return Err(CliError::new( - "context", - format!("{reason}. Use session compact or rerun chat with --auto-compact."), - )); - } - let outcome = context_cli::compact_journal( - journal, - data_root, - summary_client, - token, - summary_budget, - )?; - summarizer_calls = Some(outcome.calls); - // Settings can change during the summarizer requests, so read the - // replay authority again for the compacted history. - (estimate, assembly_blocked) = estimate_with_user( - journal, - args, - fixed_extra_bytes, - replay_allowed_now(journal, data_root, tools_enabled), - )?; - if budget.should_compact(estimate) || assembly_blocked { - return Err(CliError::new( - "context", - "Compaction did not reduce the context below the trigger and assembly limits", - )); + // Provisional text goes to standard error through one printer, which locks + // standard error only for each write, never across a model request. + let mut printer = stream_cli::StreamPrinter::new(std::io::stderr()); + let turn = { + let mut on_stream = |event: ChatStreamEvent<'_>| printer.handle(event); + let mut hooks = ChatHooks::default(); + if args.stream { + hooks.on_stream = Some(&mut on_stream); } - } - Ok(context_meter( - journal, - args, - budget, - estimate, - summarizer_calls, - )) -} - -/// Whether the next request may replay saved tool results, read from live -/// settings at each call. Text-only chat never replays them; a tool turn -/// replays them only under live `model_tools` and `read`, as the runtime -/// does. Unreadable settings fail closed to the withheld form. -fn replay_allowed_now( - journal: &SessionJournal, - data_root: &std::path::Path, - tools_enabled: bool, -) -> bool { - tools_enabled - && model_tools::tool_replay_allowed( - data_root, - journal.snapshot().workspace_id, - journal.schema_version(), - ) - .unwrap_or(false) -} - -/// The input-only estimate for the next request, and whether its active -/// history plus the pending user message exceeds a prompt-assembly limit that -/// compaction can relieve (message count or total assembled bytes). -fn estimate_with_user( - journal: &SessionJournal, - args: &ChatArgs, - fixed_extra_bytes: usize, - tool_results_allowed: bool, -) -> Result<(u64, bool), CliError> { - // Estimate the history this mode will send: saved tool results, and a - // summary built from them, are withheld without live tool authority. - let (mut history, _) = replay_active(journal, tool_results_allowed); - history.push(ConversationMessage { - role: ConversationRole::User, - content: args.message.clone(), + run_chat_turn_with_clients(&mut journal, &data_root, &input, &clients, &mut hooks) + }; + // End the last streamed line, also after a failed turn. + printer.finish(); + // Every post-open failure, including output failures, carries the locator. + let result = turn.map_err(CliError::from).and_then(|output| { + print_json(&JournalOutput { + locator: journal_locator(&journal)?, + detail: output, + }) }); - let estimate = estimate_history_input_tokens( - BASE_PROMPT.len(), - &args.instructions, - &history, - fixed_extra_bytes, - ) - .map_err(|error| CliError::new("context", error.to_string()))?; - let assembly_blocked = matches!( - prompt::assemble(&args.instructions, &history), - Err(PromptError::TooManyHistoryMessages | PromptError::AssembledPromptTooLarge) - ); - Ok((estimate, assembly_blocked)) -} - -fn context_meter( - journal: &SessionJournal, - args: &ChatArgs, - budget: ContextBudget, - estimate: u64, - summarizer_calls: Option, -) -> ContextMeter { - let compaction = journal - .snapshot() - .compaction - .as_ref() - .map(|record| CompactionMeter { - compacted_messages: record.compacted_messages, - retained_messages: u64::try_from(journal.snapshot().messages.len()) - .unwrap_or(u64::MAX) - .saturating_sub(record.compacted_messages), - summarizer_calls, - summary_sha256: record.summary_sha256.clone(), - }); - ContextMeter { - estimated_input_tokens: estimate, - context_tokens: budget.context_tokens, - max_output_tokens: args.max_output_tokens, - effective_trigger: budget.trigger(), - auto_compaction: args.auto_compact, - estimate_scope: "pre_turn_conservative", - compaction, - } + result.map_err(|error| error.with_session(locator)) } fn compact_session( @@ -603,6 +475,7 @@ fn compact_session( model: String, context_tokens: u64, summary_tokens: u32, + provider_profile: Option, ) -> Result<(), CliError> { if summary_tokens == 0 || u64::from(summary_tokens) >= context_tokens { return Err(CliError::new( @@ -610,7 +483,9 @@ fn compact_session( "summary-tokens must be positive and below context-tokens", )); } - let token = read_lm_token()?; + let LocalConnection { endpoint, token } = + local_connection(data_dir.as_deref(), endpoint, provider_profile.as_deref())?; + let token_text = token.as_ref().map(LmToken::expose); let client = LocalModelClient::new( LocalModelConfig { base_url: endpoint, @@ -618,18 +493,18 @@ fn compact_session( context_tokens, max_output_tokens: summary_tokens, }, - token.clone(), + token_text.map(str::to_owned), ) .map_err(provider_error)?; let data_root = data_root(data_dir)?; let path = resolve_journal(locator.journal, locator.journal_native)?; let mut journal = SessionJournal::open(&path).map_err(session_error)?; // Live settings decide whether saved tool results may reach the summarizer. - let outcome = context_cli::compact_journal( + let outcome = compact_journal( &mut journal, &data_root, &client, - token.as_deref(), + token_text, ContextBudget { threshold: u64::MAX, context_tokens, @@ -647,196 +522,31 @@ fn compact_session( }) } -/// Preflight deterministic failures before starting; preserve uncertain I/O state. -fn chat_turn( - journal: &mut SessionJournal, - data_root: &std::path::Path, - client: &LocalModelClient, - summary_client: &LocalModelClient, - args: ChatArgs, - token: Option<&str>, -) -> Result<(), CliError> { - if journal.snapshot().archived { - return Err(CliError::new( - "session", - "Restore the archived session before chatting", - )); - } - if journal.snapshot().active_turn.is_some() { - return Err(CliError::new( - "session", - "The previous turn is incomplete; inspect it, then use session interrupt to mark it failed without retrying", - )); - } - let mode = model_tools::select(data_root, journal)?; - let tool_extra = match &mode { - model_tools::ToolMode::Enabled { .. } => model_tools::TOOL_SCHEMA_ESTIMATE_BYTES, - model_tools::ToolMode::Disabled { .. } => 0, - }; - let tools_enabled = matches!(mode, model_tools::ToolMode::Enabled { .. }); - let meter = preflight_context( - journal, - data_root, - &args, - summary_client, - token, - tool_extra, - tools_enabled, - )?; - let manifest = RequestManifest { - base_prompt: BASE_PROMPT.to_owned(), - prompt_version: BASE_PROMPT_VERSION.to_owned(), - prompt_sha256: prompt::base_prompt_sha256(), - custom_instructions: args.instructions.clone(), - model: args.model.clone(), - base_url: args.endpoint.clone(), - context_tokens: args.context_tokens, - max_output_tokens: args.max_output_tokens, - tool_schema_version: None, - tools: None, - }; - // Live settings decide whether this turn offers the workspace tools. - let reason = match mode { - model_tools::ToolMode::Enabled { workspace_id } => { - let mut output = model_tools::run( - journal, - data_root, - workspace_id, - client, - token, - model_tools::UserTurn { - message: &args.message, - instructions: &args.instructions, - manifest, - }, - )?; - output.context = Some(meter.clone()); - return print_json(&JournalOutput { - locator: journal_locator(journal)?, - detail: output, - }); - } - model_tools::ToolMode::Disabled { reason } => reason, - }; - // Text-only chat has no model file-read authority, so saved tool results, - // and a summary built from them, are withheld rather than sent again. - let (mut history, withheld_tool_results) = replay_active(journal, false); - for message in &mut history { - message.content = redact_known_secret(&message.content, token); - } - history.push(ConversationMessage { - role: ConversationRole::User, - content: args.message.clone(), - }); - let messages = prompt::assemble(&args.instructions, &history) - .map_err(|error| CliError::new("prompt", error.to_string()))?; - journal - .preflight_request(&args.message, &manifest) - .map_err(session_error)?; - let request_id = journal.start_turn().map_err(session_error)?; - journal - .record_message(MessageRole::User, &args.message) - .map_err(session_error)?; - journal - .record_manifest(request_id, manifest) - .map_err(session_error)?; - let mut reply = match client.complete(&messages) { - Ok(reply) => reply, - Err(error) => { - journal - .fail_turn(request_id, "provider_error") - .map_err(|_| { - CliError::new( - "persistence", - "Provider failed and failure could not be recorded", - ) - })?; - return Err(provider_error(error)); - } - }; - reply.content = redact_known_secret(&reply.content, token); - // Keep every accepted reply resumable within the journal and prompt limits. - // The wire response limit also covers protocol metadata, so it is larger. - if reply.content.len() > 16 * 1024 { - journal - .fail_turn(request_id, "response_too_large") - .map_err(session_error)?; - return Err(CliError::new( - "provider", - "Assistant text exceeds the 16 KiB session message limit", - )); - } - if let Err(error) = journal.record_message(MessageRole::Assistant, &reply.content) { - if matches!( - error, - SessionError::LimitReached | SessionError::InvalidValue(_) - ) { - journal - .fail_turn(request_id, "response_too_large") - .map_err(session_error)?; - } - // Failed writes/syncs can have changed storage. Do not overwrite that - // uncertain state with a fabricated terminal event or an automatic retry. - return Err(session_error(error)); - } - journal.complete_turn(request_id).map_err(session_error)?; - print_json(&JournalOutput { - locator: journal_locator(journal)?, - detail: json!({ - "reply": reply, - "model_tools": { - "mode": "disabled", - "reason": reason, - "withheld_tool_results": withheld_tool_results, - }, - "context": meter, - }), - }) -} - -/// Create a session with current registry metadata when a trusted root is configured. +/// Obtain the endpoint and server token for `chat` or `session compact`. /// -/// An absent or valid unconfigured store preserves legacy session creation. Corrupt -/// control state or an invalid workspace under a configured root fails closed. -fn create_session( - data_root: &std::path::Path, - workspace: &std::path::Path, - title: &str, -) -> Result { - let registration_id = match ControlStore::open_existing(data_root) - .map_err(|error| CliError::new("control", error.to_string()))? - { - Some(mut store) if store.has_development_root() => { - let registration = store - .register_workspace(workspace, store.revision()) - .map_err(|error| CliError::new("control", error.to_string()))?; - let id = registration.id(); - drop(store); - Some(id) - } - Some(store) => { - drop(store); - None +/// Without a profile this is exactly what it always was: the command-line +/// endpoint and the environment token, with no data root computed. With one, +/// the endpoint and the token come only from that profile (the clap +/// definitions refuse `--endpoint` beside it) and `OMGCODE_LM_TOKEN` is not +/// read. +fn local_connection( + data_dir: Option<&Path>, + endpoint: String, + provider_profile: Option<&str>, +) -> Result { + match provider_profile { + None => Ok(resolve_local_connection( + Path::new(""), + &ConnectionSource::Environment { endpoint }, + )?), + Some(name) => { + let root = data_root(data_dir.map(Path::to_path_buf))?; + Ok(resolve_local_connection( + &root, + &ConnectionSource::Profile(name.to_owned()), + )?) } - None => None, - }; - - let journal = match registration_id { - Some(id) => SessionJournal::create_with_workspace_id(data_root, workspace, title, id), - None => SessionJournal::create(data_root, workspace, title), } - .map_err(session_error)?; - Ok(journal) -} - -/// Choose trusted application storage; repository content cannot set this value. -fn data_root(override_path: Option) -> Result { - if let Some(path) = override_path { - return Ok(path); - } - ProjectDirs::from("org", "OMG-Software", "OMGCode") - .map(|directories| directories.data_local_dir().to_owned()) - .ok_or_else(|| CliError::new("configuration", "No per-user application data directory")) } fn print_session(journal: &SessionJournal) -> Result<(), CliError> { @@ -885,35 +595,6 @@ fn resolve_journal(path: Option, native: Option) -> Result Result, CliError> { - match env::var("OMGCODE_LM_TOKEN") { - Ok(value) if !value.is_empty() => Ok(Some(value)), - Ok(_) | Err(env::VarError::NotPresent) => Ok(None), - Err(env::VarError::NotUnicode(_)) => Err(CliError::new( - "configuration", - "Server token must be Unicode", - )), - } -} - -/// Remove the configured credential from untrusted text before retention or egress. -/// If replacement would itself contain the credential, discard that text instead. -fn redact_known_secret(text: &str, token: Option<&str>) -> String { - match token.filter(|token| !token.is_empty()) { - Some(token) => { - let result = text.replace(token, "[REDACTED]"); - if result.contains(token) { - String::new() - } else { - result - } - } - None => text.to_owned(), - } -} - /// Serialize untrusted strings as escaped JSON and propagate broken output errors. fn print_json(value: &impl Serialize) -> Result<(), CliError> { let mut output = io::stdout().lock(); @@ -925,9 +606,9 @@ fn print_json(value: &impl Serialize) -> Result<(), CliError> { } fn session_error(error: omgcode_core::session::SessionError) -> CliError { - CliError::new("session", error.to_string()) + AppError::session(error).into() } fn provider_error(error: omgcode_core::provider::ProviderError) -> CliError { - CliError::new("provider", error.to_string()) + AppError::provider(error).into() } diff --git a/crates/omgcode-cli/src/model_tools.rs b/crates/omgcode-cli/src/model_tools.rs deleted file mode 100644 index a695c41..0000000 --- a/crates/omgcode-cli/src/model_tools.rs +++ /dev/null @@ -1,339 +0,0 @@ -//! Chat with the persisted model-tool preference. -//! -//! The session journal only selects which registered workspace to consult. -//! Authority comes from the live settings preference, rechecked by the core -//! runtime before every tool call, and each call stays inside that workspace. -//! The edit and command tools also need a trusted single-use approval. - -use std::path::Path; -use std::time::Duration; - -use omgcode_core::control::ControlStore; -use omgcode_core::policy::WorkspaceId; -use omgcode_core::prompt::ModelMessage; -use omgcode_core::provider::{LocalModelClient, LocalToolModel, ProviderError}; -use omgcode_core::session::{RequestManifest, SessionJournal}; -use omgcode_core::turn::{ - ModelError, ToolModel, ToolModelReply, ToolModelRequest, TurnError, TurnLimits, TurnReport, - TurnRuntime, -}; -use serde::Serialize; -use serde_json::Value; -use uuid::Uuid; - -use crate::{CliError, provider_error, redact_known_secret}; - -/// Whether this build can serve the model read/list tools. -/// -/// The capability report and tool selection share this value, so chat never -/// offers tools the platform cannot run. -pub(crate) const MODEL_TOOLS_SUPPORTED: bool = - cfg!(any(target_os = "linux", target_os = "windows")); - -/// Conservative byte allowance for the advertised tool definitions. -/// -/// The largest set (read, list, edit, and command) serializes to 2,763 bytes -/// in the adapter's wire shape; a unit test keeps this allowance above it. -pub(crate) const TOOL_SCHEMA_ESTIMATE_BYTES: usize = 4 * 1024; - -/// Whether this chat turn offers the model the workspace read/list tools. -#[derive(Debug)] -pub(crate) enum ToolMode { - /// The selected workspace's live preference enables model tools. - Enabled { workspace_id: WorkspaceId }, - /// Text-only chat, with the reason tools were not offered. - Disabled { reason: &'static str }, -} - -/// Decide the tool mode from current settings without holding the settings lock. -/// -/// An unsupported platform, an absent store, an unregistered workspace, or a -/// disabled preference gives text-only chat. Invalid settings fail closed. A -/// version 1 journal cannot record tool events, so it stays text-only. -pub(crate) fn select(data_root: &Path, journal: &SessionJournal) -> Result { - if !MODEL_TOOLS_SUPPORTED { - return Ok(ToolMode::Disabled { - reason: "platform_unsupported", - }); - } - let Some(control) = ControlStore::open_existing(data_root) - .map_err(|error| CliError::new("control", error.to_string()))? - else { - return Ok(ToolMode::Disabled { - reason: "settings_absent", - }); - }; - // The journal's UUID only selects a registration; the ID used afterwards - // and its preference come from current settings. - let (workspace_id, enabled) = - match control.registered_workspace(journal.snapshot().workspace_id) { - None => { - return Ok(ToolMode::Disabled { - reason: "workspace_unregistered", - }); - } - Some(workspace) => (workspace.id(), workspace.permissions().model_tools), - }; - // Release the settings lock before any model request. - drop(control); - if !enabled { - return Ok(ToolMode::Disabled { - reason: "preference_disabled", - }); - } - if journal.schema_version() < 2 { - return Ok(ToolMode::Disabled { - reason: "session_schema_v1", - }); - } - Ok(ToolMode::Enabled { workspace_id }) -} - -/// Whether live settings authorize replaying saved tool results for a session: -/// the same `model_tools` and `read` rule the turn runtime applies. -/// -/// The session is named by its recorded workspace UUID and schema version, so -/// compaction can recheck while it holds the journal. Each call reads current -/// settings. Every condition that gives text-only chat gives `false`; invalid -/// settings return an error. -pub(crate) fn tool_replay_allowed( - data_root: &Path, - workspace: Uuid, - schema_version: u32, -) -> Result { - if !MODEL_TOOLS_SUPPORTED || schema_version < 2 { - return Ok(false); - } - let Some(control) = ControlStore::open_existing(data_root) - .map_err(|error| CliError::new("control", error.to_string()))? - else { - return Ok(false); - }; - Ok(control - .registered_workspace(workspace) - .is_some_and(|workspace| { - let permissions = workspace.permissions(); - permissions.model_tools && permissions.read - })) -} - -/// Summary of one completed tool-enabled turn. -#[derive(Serialize)] -pub(crate) struct ToolTurnOutput { - pub(crate) reply: ToolTurnReply, - pub(crate) model_tools: ToolTurnSummary, - pub(crate) context: Option, -} - -/// Assistant text from a tool-enabled turn. It remains untrusted model output. -#[derive(Serialize)] -pub(crate) struct ToolTurnReply { - pub(crate) content: String, - /// `stop`, or `length` when the token limit cut the reply short. - pub(crate) finish_reason: String, -} - -/// Bounded counters for a tool-enabled turn. -#[derive(Serialize)] -pub(crate) struct ToolTurnSummary { - mode: &'static str, - workspace_id: String, - rounds: usize, - tool_calls: usize, - tool_bytes: u64, - withheld_tool_results: usize, -} - -/// The user's request for one tool-enabled turn. -pub(crate) struct UserTurn<'a> { - pub(crate) message: &'a str, - pub(crate) instructions: &'a str, - pub(crate) manifest: RequestManifest, -} - -/// Run one tool-enabled turn through the core runtime. -pub(crate) fn run( - journal: &mut SessionJournal, - data_root: &Path, - workspace_id: WorkspaceId, - client: &LocalModelClient, - token: Option<&str>, - turn: UserTurn<'_>, -) -> Result { - let mut model = RedactingModel { - inner: LocalToolModel::new(client), - token, - secret_in_tool_call: false, - }; - // Tool results are recorded before the next model request, so they need - // their own redaction; the model wrapper sees them only afterwards. - let redact_tool_text = |text: &str| redact_known_secret(text, token); - let report: TurnReport = TurnRuntime::new(journal, data_root, workspace_id, chat_limits()) - .and_then(|runtime| { - runtime.with_tool_text_filter(&redact_tool_text).run( - &mut model, - turn.message, - turn.instructions, - turn.manifest, - ) - }) - .map_err(|error| { - if model.secret_in_tool_call { - return CliError::new( - "provider", - "The model returned a tool call containing the configured server token; it was not recorded or run", - ); - } - turn_error(error, model.inner.last_error()) - })?; - Ok(ToolTurnOutput { - reply: ToolTurnReply { - content: report.reply, - finish_reason: report.finish_reason, - }, - model_tools: ToolTurnSummary { - mode: "enabled", - workspace_id: workspace_id.to_string(), - rounds: report.rounds, - tool_calls: report.tool_calls, - tool_bytes: report.tool_bytes, - withheld_tool_results: report.withheld_tool_results, - }, - context: None, - }) -} - -/// Cooperative deadline for one tool-enabled chat turn. -/// -/// Each local model request may take up to 30 seconds, and a turn makes at most -/// four requests, so the 30-second library default would stop ordinary turns. -const CHAT_TURN_DEADLINE: Duration = Duration::from_secs(120); - -/// Runtime limits for tool-enabled chat: library maxima with a chat deadline. -fn chat_limits() -> TurnLimits { - TurnLimits { - deadline: CHAT_TURN_DEADLINE, - ..TurnLimits::default() - } -} - -/// Remove a known local server token from text sent to and received from the model. -/// -/// A tool call that contains the token is refused rather than redacted, because -/// editing its arguments could change the requested path. The runtime records -/// calls before any result filter runs, so the refusal happens here. -struct RedactingModel<'a> { - inner: LocalToolModel<'a>, - token: Option<&'a str>, - secret_in_tool_call: bool, -} - -impl ToolModel for RedactingModel<'_> { - fn complete(&mut self, request: ToolModelRequest<'_>) -> Result { - let messages: Vec = request - .messages - .iter() - .map(|message| ModelMessage { - role: message.role, - content: redact_known_secret(&message.content, self.token), - }) - .collect(); - let mut reply = self.inner.complete(ToolModelRequest { - messages: &messages, - tools: request.tools, - })?; - if let Some(token) = self.token.filter(|token| !token.is_empty()) - && reply - .tool_calls - .iter() - .any(|call| contains_secret(call, token, true)) - { - self.secret_in_tool_call = true; - return Err(ModelError::InvalidResponse); - } - reply.content = redact_known_secret(&reply.content, self.token); - Ok(reply) - } -} - -/// Report whether any key or string in a raw tool-call value contains `token`. -/// -/// Tool-call arguments arrive as a JSON-encoded string, so a string that parses -/// as JSON is searched once more after decoding. Decoding is not repeated -/// inside that nested value. -fn contains_secret(value: &Value, token: &str, decode_nested: bool) -> bool { - match value { - Value::String(text) => { - text.contains(token) - || (decode_nested - && serde_json::from_str::(text) - .is_ok_and(|nested| contains_secret(&nested, token, false))) - } - Value::Array(items) => items - .iter() - .any(|item| contains_secret(item, token, decode_nested)), - Value::Object(fields) => fields - .iter() - .any(|(key, item)| key.contains(token) || contains_secret(item, token, decode_nested)), - Value::Null | Value::Bool(_) | Value::Number(_) => false, - } -} - -/// Map a runtime failure to a stable CLI error without echoing model text. -fn turn_error(error: TurnError, provider: Option) -> CliError { - match error { - TurnError::Model(_) => provider.map_or_else( - || CliError::new("provider", "Local model request failed"), - provider_error, - ), - TurnError::ToolDenied(_) => CliError::new( - "permission", - "A model tool call was denied by the current workspace preferences", - ), - TurnError::InvalidToolCall(error) => CliError::new( - "tool_call", - format!("The model returned an invalid tool call: {error}"), - ), - TurnError::Limit(limit) => { - CliError::new("tool_limit", format!("Turn limit exceeded: {limit}")) - } - TurnError::Session(error) => crate::session_error(error), - TurnError::Prompt(error) => CliError::new("prompt", error.to_string()), - TurnError::Archived => { - CliError::new("session", "Restore the archived session before chatting") - } - TurnError::ActiveTurn => CliError::new( - "session", - "The previous turn is incomplete; inspect it, then use session interrupt to mark it failed without retrying", - ), - other => CliError::new("turn", other.to_string()), - } -} - -#[cfg(test)] -mod tests { - use omgcode_core::turn::{command_tool_definition, edit_tool_definition, tool_definitions}; - use serde_json::{Value, json}; - - use super::TOOL_SCHEMA_ESTIMATE_BYTES; - - /// The estimate must cover the largest advertised tool set in the exact - /// OpenAI-compatible wire shape the adapter sends. - #[test] - fn the_schema_estimate_covers_every_advertised_tool_definition() { - let mut definitions = tool_definitions(); - definitions.push(edit_tool_definition()); - definitions.push(command_tool_definition()); - let wire: Vec = definitions - .iter() - .map(|definition| json!({"type": "function", "function": definition})) - .collect(); - let bytes = serde_json::to_vec(&wire).expect("serialize tool definitions"); - println!("advertised tool definitions: {} bytes", bytes.len()); - assert!( - bytes.len() <= TOOL_SCHEMA_ESTIMATE_BYTES, - "{} bytes exceed the {TOOL_SCHEMA_ESTIMATE_BYTES}-byte estimate", - bytes.len() - ); - } -} diff --git a/crates/omgcode-cli/src/stream_cli.rs b/crates/omgcode-cli/src/stream_cli.rs new file mode 100644 index 0000000..717aac3 --- /dev/null +++ b/crates/omgcode-cli/src/stream_cli.rs @@ -0,0 +1,163 @@ +//! Provisional streamed chat text on standard error (`chat --stream`). +//! +//! Streamed text is untrusted model output, so every piece goes through core's +//! streaming display-safe escaping before it reaches a terminal. Standard +//! output is never used here: it carries the one JSON result of the command. +//! +//! The writer is `io::Stderr`, which locks only for the span of one write. No +//! lock is held while a model request is in flight. + +use std::io::Write; + +use omgcode_app::ChatStreamEvent; +use omgcode_core::turn::DisplaySafeStream; + +/// Writes escaped streamed text, one line per model request that produced text. +pub(crate) struct StreamPrinter { + out: W, + safe: DisplaySafeStream, + /// Whether the current request has delivered any text. + wrote_text: bool, + /// Set by the first failed write; later text is dropped. + failed: bool, +} + +impl StreamPrinter { + /// Print to `out`, normally standard error. + pub(crate) fn new(out: W) -> Self { + Self { + out, + safe: DisplaySafeStream::new(), + wrote_text: false, + failed: false, + } + } + + /// Handle one event of the turn. + pub(crate) fn handle(&mut self, event: ChatStreamEvent<'_>) { + match event { + ChatStreamEvent::RequestStarted => self.end_request(), + ChatStreamEvent::Text(text) => { + self.wrote_text = true; + let escaped = self.safe.push(text); + self.write(&escaped); + } + } + } + + /// End the turn: complete the last request's line, if it printed text. + pub(crate) fn finish(&mut self) { + self.end_request(); + } + + /// Flush the held tail of a request that wrote text and end its line. + fn end_request(&mut self) { + let safe = std::mem::take(&mut self.safe); + if std::mem::take(&mut self.wrote_text) { + let tail = safe.finish(); + self.write(&tail); + self.write("\n"); + } + } + + fn write(&mut self, text: &str) { + if self.failed || text.is_empty() { + return; + } + if self.out.write_all(text.as_bytes()).is_err() { + // Provisional text is a convenience on a side channel. A closed or + // full standard error must not fail a turn whose result is still + // recorded and printed, so stop writing and discard the error. + self.failed = true; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io; + + fn run(events: &[ChatStreamEvent<'_>]) -> String { + let mut bytes = Vec::new(); + let mut printer = StreamPrinter::new(&mut bytes); + for event in events { + printer.handle(*event); + } + printer.finish(); + String::from_utf8(bytes).expect("escaped text is UTF-8") + } + + #[test] + fn each_request_with_text_ends_in_one_line_feed() { + let output = run(&[ + ChatStreamEvent::RequestStarted, + ChatStreamEvent::Text("Hel"), + ChatStreamEvent::Text("lo"), + ChatStreamEvent::RequestStarted, + ChatStreamEvent::Text("again"), + ]); + assert_eq!(output, "Hello\nagain\n"); + } + + #[test] + fn a_request_without_text_prints_nothing() { + let output = run(&[ + ChatStreamEvent::RequestStarted, + ChatStreamEvent::RequestStarted, + ]); + assert_eq!(output, ""); + } + + #[test] + fn control_and_bidi_characters_are_escaped_across_pieces() { + let output = run(&[ + ChatStreamEvent::RequestStarted, + ChatStreamEvent::Text("a\u{1b}[31m \u{202e}b \\"), + ChatStreamEvent::Text("u{41} end\\"), + ]); + assert_eq!(output, "a\\u{001b}[31m \\u{202e}b \\u{5c}u{41} end\\\n"); + } + + /// A writer that accepts `limit` writes and then fails every write. + struct FailAfter { + limit: usize, + accepted: Vec, + attempts: usize, + } + + impl Write for FailAfter { + fn write(&mut self, buffer: &[u8]) -> io::Result { + self.attempts += 1; + if self.attempts > self.limit { + return Err(io::Error::from(io::ErrorKind::BrokenPipe)); + } + self.accepted.extend_from_slice(buffer); + Ok(buffer.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + } + + #[test] + fn a_failed_write_stops_all_later_writes_without_panicking() { + let mut writer = FailAfter { + limit: 1, + accepted: Vec::new(), + attempts: 0, + }; + { + let mut printer = StreamPrinter::new(&mut writer); + printer.handle(ChatStreamEvent::RequestStarted); + printer.handle(ChatStreamEvent::Text("kept")); + printer.handle(ChatStreamEvent::Text("lost")); + printer.handle(ChatStreamEvent::RequestStarted); + printer.handle(ChatStreamEvent::Text("also lost")); + printer.finish(); + } + assert_eq!(writer.accepted, b"kept"); + assert_eq!(writer.attempts, 2, "no write is attempted after a failure"); + } +} diff --git a/crates/omgcode-cli/tests/cli_workflow.rs b/crates/omgcode-cli/tests/cli_workflow.rs index 4700848..bdaa70c 100644 --- a/crates/omgcode-cli/tests/cli_workflow.rs +++ b/crates/omgcode-cli/tests/cli_workflow.rs @@ -26,6 +26,14 @@ struct MockServer { impl MockServer { /// Accept exactly one local request with independent bounds and observed cleanup. fn start(response: Value) -> Self { + Self::start_raw( + "application/json", + serde_json::to_vec(&response).expect("serialize mock response"), + ) + } + + /// Like [`MockServer::start`], but answers with the given body and content type. + fn start_raw(content_type: &'static str, response: Vec) -> Self { let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind private mock server"); listener .set_nonblocking(true) @@ -51,7 +59,7 @@ impl MockServer { sender .send(Ok(request_json)) .map_err(|error| error.to_string())?; - write_http_response(&mut stream, &response) + write_http_response(&mut stream, content_type, &response) })(); if let Err(error) = &result { let _ = sender.send(Err(error.clone())); @@ -154,19 +162,38 @@ fn find_header_end(bytes: &[u8]) -> Option { bytes.windows(4).position(|window| window == b"\r\n\r\n") } -/// Send deterministic JSON without depending on a real provider or live account. -fn write_http_response(stream: &mut TcpStream, body: &Value) -> Result<(), String> { - let body = serde_json::to_vec(body).map_err(|error| error.to_string())?; +/// Send a deterministic body without depending on a real provider or live account. +fn write_http_response( + stream: &mut TcpStream, + content_type: &str, + body: &[u8], +) -> Result<(), String> { let headers = format!( - "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + "HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len() ); stream .write_all(headers.as_bytes()) - .and_then(|()| stream.write_all(&body)) + .and_then(|()| stream.write_all(body)) .map_err(|error| error.to_string()) } +/// A complete streamed text reply, one event per piece. +fn event_stream(pieces: &[&str]) -> Vec { + let mut body = String::new(); + for piece in pieces { + body.push_str(&format!( + "data: {}\n\n", + json!({"choices": [{"index": 0, "delta": {"role": "assistant", "content": piece}, "finish_reason": null}]}) + )); + } + body.push_str(&format!( + "data: {}\n\ndata: [DONE]\n\n", + json!({"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}) + )); + body.into_bytes() +} + fn completion_response(content: &str) -> Value { json!({ "id": "fixture-response", @@ -994,3 +1021,99 @@ fn native_locator_reopens_a_journal_path_with_an_unpaired_surrogate() { result["session"]["session_id"] ); } + +/// Run `chat` with optional extra flags before the message. +fn run_chat_with(data_dir: &Path, workspace: &Path, endpoint: &str, extra: &[&str]) -> Output { + let data_dir = data_dir.to_str().expect("temporary path is Unicode"); + let workspace = workspace.to_str().expect("temporary path is Unicode"); + gated_output( + cli() + .args([ + "--data-dir", + data_dir, + "chat", + "--workspace", + workspace, + "--endpoint", + endpoint, + "--model", + "fixture", + "--context-tokens", + "131072", + "--max-output-tokens", + "32", + ]) + .args(extra) + .arg("say something"), + ) + .expect("run OMGCode chat command") +} + +#[test] +fn chat_stream_writes_escaped_text_to_stderr_and_one_json_document_to_stdout() { + // Given a server that streams text holding a control sequence and a bidi override. + let temporary = TempDir::new().expect("create private test directory"); + let data_dir = temporary.path().join("application-data"); + let workspace = temporary.path().join("workspace"); + std::fs::create_dir(&workspace).expect("create private workspace"); + let server = MockServer::start_raw( + "text/event-stream", + event_stream(&["Hel", "lo \u{1b}[31mred", " \u{202e}flip \\", "u{41}"]), + ); + + // When chat runs with --stream. + let output = run_chat_with(&data_dir, &workspace, &server.endpoint, &["--stream"]); + let request = server.finish(); + + // Then the request asked for a stream, standard error shows the escaped + // text on one line, and standard output is one JSON document with the + // same reply. + assert_success(&output); + assert_eq!(request["stream"], true); + let stderr = String::from_utf8(output.stderr.clone()).expect("escaped stderr is UTF-8"); + assert_eq!( + stderr, + "Hello \\u{001b}[31mred \\u{202e}flip \\u{5c}u{41}\n" + ); + assert!(!stderr.contains('\u{1b}')); + assert!(!stderr.contains('\u{202e}')); + let result = output_json(&output); + assert_eq!( + result["reply"]["content"], + "Hello \u{1b}[31mred \u{202e}flip \\u{41}" + ); + let stdout = String::from_utf8(output.stdout.clone()).expect("stdout is UTF-8"); + assert_eq!(stdout.trim_end().lines().count(), 1, "one JSON document"); +} + +#[test] +fn chat_without_stream_sends_stream_false_and_leaves_stderr_empty() { + let temporary = TempDir::new().expect("create private test directory"); + let data_dir = temporary.path().join("application-data"); + let workspace = temporary.path().join("workspace"); + std::fs::create_dir(&workspace).expect("create private workspace"); + let server = MockServer::start(completion_response("plain answer")); + + let output = run_chat_with(&data_dir, &workspace, &server.endpoint, &[]); + let request = server.finish(); + + assert_success(&output); + assert_eq!(request["stream"], false); + assert!(request.get("stream_options").is_none()); + assert!( + output.stderr.is_empty(), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!(output_json(&output)["reply"]["content"], "plain answer"); +} + +#[test] +fn capabilities_report_the_streaming_adapters() { + let output = run_cli(&["capabilities"]); + assert_success(&output); + assert_eq!( + output_json(&output)["streaming_adapters"], + json!(["lm_studio"]) + ); +} diff --git a/crates/omgcode-cli/tests/run_workflow.rs b/crates/omgcode-cli/tests/run_workflow.rs index 6fea459..c002f52 100644 --- a/crates/omgcode-cli/tests/run_workflow.rs +++ b/crates/omgcode-cli/tests/run_workflow.rs @@ -465,16 +465,29 @@ fn the_deadline_terminates_and_is_recorded() { } /// Two runs in the same workspace at once both complete and are recorded. +/// +/// Each run writes its own file and then waits until it sees the other run's +/// file, so exit code 0 from both proves the two runs were alive at the same +/// time. A run that never sees the other's file exits non-zero. The wait ends +/// as soon as the file appears, so the test does not depend on how fast the +/// host does a fixed amount of work. The wait is bounded: 20 seconds of +/// polling on Linux, and on Windows the loop count or the run's CPU-time limit, +/// whichever comes first. #[test] fn concurrent_runs_complete_and_are_recorded() { let scene = Scene::new(); assert_success(&scene.add_profile("system", &[])); #[cfg(target_os = "linux")] - let scripts = ["sleep 1; echo one > one.txt", "sleep 1; echo two > two.txt"]; + let scripts = [ + "echo one > one.txt; i=0; while [ ! -e two.txt ] && [ $i -lt 400 ]; do sleep 0.05; i=$((i+1)); done; test -e two.txt", + "echo two > two.txt; i=0; while [ ! -e one.txt ] && [ $i -lt 400 ]; do sleep 0.05; i=$((i+1)); done; test -e one.txt", + ]; + // `exit 0` inside the loop ends `cmd` at once; running out of iterations + // falls through to `exit 3`. #[cfg(windows)] let scripts = [ - "(for /l %i in (1,1,30000) do @cd .)& echo one>one.txt", - "(for /l %i in (1,1,30000) do @cd .)& echo two>two.txt", + "echo one>one.txt& (for /l %i in (1,1,1000000) do @if exist two.txt exit 0)& exit 3", + "echo two>two.txt& (for /l %i in (1,1,1000000) do @if exist one.txt exit 0)& exit 3", ]; let children: Vec<_> = scripts .iter() diff --git a/crates/omgcode-cli/tests/stored_credential.rs b/crates/omgcode-cli/tests/stored_credential.rs new file mode 100644 index 0000000..45352a4 --- /dev/null +++ b/crates/omgcode-cli/tests/stored_credential.rs @@ -0,0 +1,713 @@ +//! `chat` and `session compact` with `--provider-profile` through the real +//! binary, the real settings store, the real native credential store, and a +//! recording loopback model server (ADR-0047). On Linux these need an unlocked +//! Secret Service default collection and fail without one. +//! +//! Test tokens are obviously fake. A scene deletes only the native items it +//! stored, when it drops. + +mod support; + +use std::fs::{self, File}; +use std::io::{Read, Write}; +use std::net::{TcpListener, TcpStream}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Output, Stdio}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::thread::{self, JoinHandle}; +use std::time::{Duration, Instant}; + +use omgcode_core::control::{CredentialId, CredentialStore}; +use serde_json::{Value, json}; + +use support::workspace_fixture::Fixture; +use support::{ + CLI_DEADLINE, assert_error_code, assert_success, cli_command, json_output, path_arg, run_cli, + run_with_deadline, +}; + +const STORED: &str = "fake-cli-stored-token-81f2c0d4"; +const FROM_ENV: &str = "fake-cli-environment-token-3a7be915"; +const MOCK_DEADLINE: Duration = Duration::from_secs(30); +const MAX_HTTP_BYTES: usize = 1024 * 1024; + +/// One request the recording server received. +struct Recorded { + head: String, + body: String, +} + +impl Recorded { + fn header(&self, name: &str) -> Option { + let prefix = format!("{}:", name.to_ascii_lowercase()); + self.head.lines().find_map(|line| { + line.to_ascii_lowercase() + .starts_with(&prefix) + .then(|| line[prefix.len()..].trim().to_owned()) + }) + } +} + +/// A loopback server that answers every request with a fixed completion and +/// records it, until it is finished. +struct Recorder { + endpoint: String, + stop: Arc, + worker: JoinHandle>, +} + +impl Recorder { + fn start(reply: &'static str) -> Self { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind recorder"); + listener.set_nonblocking(true).expect("bounded accept"); + let address = listener.local_addr().expect("recorder address"); + let stop = Arc::new(AtomicBool::new(false)); + let flag = Arc::clone(&stop); + let worker = thread::spawn(move || { + let deadline = Instant::now() + MOCK_DEADLINE; + let mut recorded = Vec::new(); + while !flag.load(Ordering::SeqCst) && Instant::now() < deadline { + match listener.accept() { + Ok((mut stream, _)) => { + if let Some(request) = serve(&mut stream, reply) { + recorded.push(request); + } + } + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { + thread::sleep(Duration::from_millis(5)); + } + Err(_) => break, + } + } + recorded + }); + Self { + endpoint: format!("http://{address}/v1"), + stop, + worker, + } + } + + /// Stop the server after the CLI has exited, and return what it received. + fn finish(self) -> Vec { + self.stop.store(true, Ordering::SeqCst); + self.worker.join().expect("recorder thread") + } +} + +fn serve(stream: &mut TcpStream, reply: &str) -> Option { + stream.set_nonblocking(false).ok()?; + stream + .set_read_timeout(Some(Duration::from_secs(10))) + .ok()?; + let mut bytes = Vec::new(); + let mut buffer = [0_u8; 4096]; + let (head, body) = loop { + if let Some(end) = bytes.windows(4).position(|window| window == b"\r\n\r\n") { + let head = String::from_utf8_lossy(&bytes[..end]).into_owned(); + let length: usize = head + .to_ascii_lowercase() + .lines() + .find_map(|line| line.strip_prefix("content-length:").map(str::to_owned))? + .trim() + .parse() + .ok()?; + if length > MAX_HTTP_BYTES { + return None; + } + while bytes.len() < end + 4 + length { + let read = stream.read(&mut buffer).ok()?; + if read == 0 { + return None; + } + bytes.extend_from_slice(&buffer[..read]); + } + break (head, bytes[end + 4..end + 4 + length].to_vec()); + } + let read = stream.read(&mut buffer).ok()?; + if read == 0 || bytes.len() > MAX_HTTP_BYTES { + return None; + } + bytes.extend_from_slice(&buffer[..read]); + }; + let body_text = String::from_utf8_lossy(&body).into_owned(); + let payload = serde_json::to_vec(&json!({ + "id": "fixture-response", + "object": "chat.completion", + "created": 1, + "model": "fixture", + "choices": [{ + "index": 0, + "message": {"role": "assistant", "content": reply}, + "finish_reason": "stop" + }], + "usage": {"prompt_tokens": 11, "completion_tokens": 3, "total_tokens": 14} + })) + .ok()?; + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + payload.len() + ); + stream + .write_all(response.as_bytes()) + .and_then(|()| stream.write_all(&payload)) + .ok()?; + Some(Recorded { + head, + body: body_text, + }) +} + +/// A registered workspace in a private data root, and the native items this +/// test stored. +struct Scene { + fixture: Fixture, + created: Vec, +} + +impl Scene { + fn new() -> Self { + Self { + fixture: Fixture::new(b"note\n"), + created: Vec::new(), + } + } + + fn data(&self) -> &Path { + &self.fixture.data + } + + fn revision(&self) -> String { + let output = run_cli(self.data(), &["workspace", "status"]); + assert_success(&output); + json_output(&output)["revision"] + .as_u64() + .expect("revision") + .to_string() + } + + /// Record a profile with no endpoint, as one recorded before endpoints + /// existed. + fn add_profile(&self, name: &str, kind: &str, auth: &str) { + let output = self.try_add_profile(name, kind, auth, None); + assert_success(&output); + } + + /// Record a profile that names its endpoint. + fn add_profile_at(&self, name: &str, kind: &str, auth: &str, endpoint: &str) { + let output = self.try_add_profile(name, kind, auth, Some(endpoint)); + assert_success(&output); + } + + fn try_add_profile( + &self, + name: &str, + kind: &str, + auth: &str, + endpoint: Option<&str>, + ) -> Output { + let revision = self.revision(); + let mut args = vec![ + "workspace", + "provider-profile-add", + name, + "--kind", + kind, + "--auth", + auth, + "--revision", + &revision, + ]; + if let Some(endpoint) = endpoint { + args.extend(["--endpoint", endpoint]); + } + run_cli(self.data(), &args) + } + + /// Store `secret` from standard input and bind it to `name`. + fn bind(&mut self, name: &str, secret: &str) { + let input = self.fixture._temporary.path().join("stdin-input"); + fs::write(&input, secret).expect("stdin fixture"); + let revision = self.revision(); + let mut command = cli_command( + self.data(), + &[ + "workspace", + "provider-credential-set", + name, + "--revision", + &revision, + ], + ); + command.stdin(Stdio::from(File::open(&input).expect("open stdin fixture"))); + let result = run_with_deadline(command, CLI_DEADLINE).expect("run CLI"); + assert!(!result.timed_out, "CLI timed out"); + fs::remove_file(&input).expect("remove stdin fixture"); + assert_success(&result.output); + let id = json_output(&result.output)["provider_credential"]["credential_id"] + .as_str() + .expect("credential id") + .to_owned(); + self.created + .push(CredentialId::parse(&id).expect("valid id")); + } + + /// The `chat` argument list for a new session, ending at the message. A + /// profile supplies its own endpoint, so a caller passes one or the other + /// except in the tests of the usage error. + fn chat_args( + &self, + endpoint: Option<&str>, + profile: Option<&str>, + message: &str, + ) -> Vec { + let mut args: Vec = [ + "chat", + "--workspace", + path_arg(&self.fixture.workspace), + "--model", + "fixture", + "--context-tokens", + "131072", + "--max-output-tokens", + "32", + ] + .map(str::to_owned) + .to_vec(); + if let Some(endpoint) = endpoint { + args.extend(["--endpoint".to_owned(), endpoint.to_owned()]); + } + if let Some(profile) = profile { + args.extend(["--provider-profile".to_owned(), profile.to_owned()]); + } + args.push(message.to_owned()); + args + } +} + +impl Drop for Scene { + fn drop(&mut self) { + // Best effort, and only for items this scene stored. + if let Ok(credentials) = CredentialStore::open(&self.fixture.data) { + for id in &self.created { + credentials.delete(id).ok(); + } + } + } +} + +/// Run the CLI with an extra environment variable. +fn run_with_env(data: &Path, args: &[String], env: Option<(&str, &str)>) -> Output { + let borrowed: Vec<&str> = args.iter().map(String::as_str).collect(); + let mut command: Command = cli_command(data, &borrowed); + if let Some((name, value)) = env { + command.env(name, value); + } + let result = run_with_deadline(command, CLI_DEADLINE).expect("run CLI"); + assert!(!result.timed_out, "CLI exceeded its deadline"); + result.output +} + +fn output_text(output: &Output) -> String { + format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ) +} + +fn assert_no_token(text: &str, tokens: &[&str]) { + for token in tokens { + assert!(!text.contains(token), "a token leaked into: {text}"); + } +} + +/// Every file under the data root and workspace, for token text. +fn assert_no_token_in_files(root: &Path, tokens: &[&str]) { + let mut pending: Vec = vec![root.to_path_buf()]; + while let Some(directory) = pending.pop() { + for entry in fs::read_dir(&directory).expect("read directory") { + let entry = entry.expect("entry"); + let kind = entry.file_type().expect("file type"); + if kind.is_dir() { + pending.push(entry.path()); + } else if kind.is_file() { + let bytes = fs::read(entry.path()).expect("read file"); + let text = String::from_utf8_lossy(&bytes); + for token in tokens { + assert!( + !text.contains(token), + "{} contains a token", + entry.path().display() + ); + } + } + } + } +} + +#[test] +fn the_stored_token_is_the_bearer_and_the_environment_token_is_never_read() { + let mut scene = Scene::new(); + let server = Recorder::start("hello"); + scene.add_profile_at("lms", "lm_studio", "api_key", &server.endpoint); + scene.bind("lms", STORED); + + let args = scene.chat_args(None, Some("lms"), &format!("use {STORED}")); + let output = run_with_env(scene.data(), &args, Some(("OMGCODE_LM_TOKEN", FROM_ENV))); + let requests = server.finish(); + assert_success(&output); + + assert_eq!(requests.len(), 1); + assert_eq!( + requests[0].header("authorization"), + Some(format!("Bearer {STORED}")) + ); + for request in &requests { + assert_no_token(&request.head, &[FROM_ENV]); + assert_no_token(&request.body, &[STORED, FROM_ENV]); + } + assert!(requests[0].body.contains("[REDACTED]")); + assert_no_token(&output_text(&output), &[STORED, FROM_ENV]); + assert_no_token_in_files(scene.data(), &[STORED, FROM_ENV]); +} + +#[test] +fn without_the_flag_the_environment_token_is_still_the_bearer() { + let scene = Scene::new(); + let server = Recorder::start("hello"); + let args = scene.chat_args(Some(&server.endpoint), None, "hello"); + let output = run_with_env(scene.data(), &args, Some(("OMGCODE_LM_TOKEN", FROM_ENV))); + let requests = server.finish(); + assert_success(&output); + assert_eq!( + requests[0].header("authorization"), + Some(format!("Bearer {FROM_ENV}")) + ); +} + +#[test] +fn a_keyless_profile_sends_no_authorization_even_when_the_environment_is_set() { + let scene = Scene::new(); + let server = Recorder::start("hello"); + scene.add_profile_at("open", "lm_studio", "none", &server.endpoint); + let args = scene.chat_args(None, Some("open"), "hello"); + let output = run_with_env(scene.data(), &args, Some(("OMGCODE_LM_TOKEN", FROM_ENV))); + let requests = server.finish(); + assert_success(&output); + assert_eq!(requests.len(), 1); + assert_eq!(requests[0].header("authorization"), None); + assert_no_token(&requests[0].head, &[FROM_ENV]); + assert_no_token(&requests[0].body, &[FROM_ENV]); + // No credential was ever stored in this data root, so a keyless profile + // needed no native store to succeed. + #[cfg(windows)] + assert!( + !scene.data().join("credentials-v1").exists(), + "no native credential store was created" + ); +} + +#[test] +fn an_unusable_profile_fails_with_a_credential_error_before_any_request_or_session() { + let mut scene = Scene::new(); + let server = Recorder::start("hello"); + scene.add_profile_at("unbound", "lm_studio", "api_key", &server.endpoint); + scene.add_profile("openai-key", "openai", "api_key"); + scene.bind("openai-key", STORED); + + for (name, cause) in [ + ("missing", "unknown_profile"), + ("openai-key", "wrong_provider"), + ("unbound", "not_bound"), + ] { + let args = scene.chat_args(None, Some(name), "hello"); + let output = run_with_env(scene.data(), &args, Some(("OMGCODE_LM_TOKEN", FROM_ENV))); + assert_error_code(&output, "credential"); + let error: Value = serde_json::from_slice(&output.stderr).expect("error JSON"); + let message = error["error"]["message"].as_str().expect("message"); + assert!(message.starts_with(cause), "{name}: {message}"); + // The failure precedes the session, so there is no locator to resume. + assert!(error["error"].get("session").is_none(), "{error}"); + assert_no_token(&output_text(&output), &[STORED, FROM_ENV]); + } + let requests = server.finish(); + assert!(requests.is_empty(), "no model request was made"); +} + +#[test] +fn an_empty_profile_name_is_a_usage_error() { + let scene = Scene::new(); + let args = scene.chat_args(None, Some(""), "hello"); + let output = run_with_env(scene.data(), &args, None); + assert!(!output.status.success()); + assert_eq!(output.status.code(), Some(2)); +} + +#[test] +fn session_compact_uses_the_stored_token() { + let mut scene = Scene::new(); + let server = Recorder::start("a reply"); + scene.add_profile_at("lms", "lm_studio", "api_key", &server.endpoint); + scene.bind("lms", STORED); + + // Two turns, with no token, give the journal something to compact. + let first = scene.chat_args(Some(&server.endpoint), None, "first request"); + let output = run_with_env(scene.data(), &first, None); + assert_success(&output); + let journal = json_output(&output)["journal"] + .as_str() + .expect("journal path") + .to_owned(); + let mut second = scene.chat_args(Some(&server.endpoint), None, "latest request"); + // Resume the same journal instead of creating a session. + second.splice(1..3, ["--journal".to_owned(), journal.clone()]); + let output = run_with_env(scene.data(), &second, None); + assert_success(&output); + + let compact: Vec = [ + "session", + "compact", + &journal, + "--model", + "fixture", + "--context-tokens", + "131072", + "--summary-tokens", + "256", + "--provider-profile", + "lms", + ] + .map(str::to_owned) + .to_vec(); + let output = run_with_env(scene.data(), &compact, Some(("OMGCODE_LM_TOKEN", FROM_ENV))); + let requests = server.finish(); + assert_success(&output); + assert_eq!(requests.len(), 3); + assert_eq!(requests[0].header("authorization"), None); + assert_eq!(requests[1].header("authorization"), None); + assert_eq!( + requests[2].header("authorization"), + Some(format!("Bearer {STORED}")) + ); + assert_no_token(&output_text(&output), &[STORED, FROM_ENV]); + assert_no_token_in_files(scene.data(), &[STORED, FROM_ENV]); + + // An unusable profile stops compaction before any request or change. + let server = Recorder::start("a reply"); + let mut refused = compact.clone(); + refused.truncate(refused.len() - 1); + refused.push("missing".to_owned()); + let before = fs::read(&journal).expect("journal bytes"); + let output = run_with_env(scene.data(), &refused, None); + assert_error_code(&output, "credential"); + assert!(server.finish().is_empty()); + assert_eq!(fs::read(&journal).expect("journal bytes"), before); +} + +#[test] +fn capabilities_report_the_stored_credential_adapters_and_no_provider_adapters() { + let scene = Scene::new(); + let output = run_cli(scene.data(), &["capabilities"]); + assert_success(&output); + let capabilities = json_output(&output); + assert_eq!( + capabilities["stored_credential_adapters"], + json!(["lm_studio"]) + ); + assert_eq!(capabilities["provider_adapters"], false); +} + +/// The error message of a failed command. +fn error_message(output: &Output) -> String { + let error: Value = serde_json::from_slice(&output.stderr).expect("error JSON"); + error["error"]["message"] + .as_str() + .expect("message") + .to_owned() +} + +#[test] +fn a_profile_records_and_shows_its_endpoint_and_one_without_prints_as_before() { + let scene = Scene::new(); + let endpoint = "http://127.0.0.1:1234/v1"; + let revision = scene.revision(); + let output = scene.try_add_profile("lms", "lm_studio", "api_key", Some(endpoint)); + assert_success(&output); + let added = json_output(&output); + assert_eq!( + added["snapshot"]["provider_profiles"][0]["endpoint"], + endpoint + ); + assert!(revision != scene.revision()); + scene.add_profile("old", "lm_studio", "none"); + + let shown = json_output(&run_cli( + scene.data(), + &["workspace", "provider-profile-show", "lms"], + )); + assert_eq!(shown["provider_profile"]["endpoint"], endpoint); + let old = json_output(&run_cli( + scene.data(), + &["workspace", "provider-profile-show", "old"], + )); + let old = old["provider_profile"].as_object().expect("profile object"); + assert!(!old.contains_key("endpoint"), "{old:?}"); + let mut keys: Vec<&str> = old.keys().map(String::as_str).collect(); + keys.sort_unstable(); + assert_eq!( + keys, + ["auth", "credential", "id", "kind", "name", "revision"] + ); + + let status = json_output(&run_cli(scene.data(), &["workspace", "status"])); + let profiles = status["provider_profiles"].as_array().expect("profiles"); + assert_eq!(profiles[0]["endpoint"], endpoint); + assert!(profiles[1].get("endpoint").is_none()); +} + +#[test] +fn an_endpoint_the_adapter_would_refuse_is_refused_when_the_profile_is_added() { + let scene = Scene::new(); + let long = format!("http://127.0.0.1:{}1234/v1", "0".repeat(300)); + let cases: Vec<(&str, String)> = [ + ("lm_studio", "https://127.0.0.1:1234/v1"), + ("lm_studio", "http://localhost:1234/v1"), + ("lm_studio", "http://192.0.2.1:1234/v1"), + ("lm_studio", "http://127.0.0.1/v1"), + ("lm_studio", "http://127.0.0.1:0/v1"), + ("lm_studio", "http://user@127.0.0.1:1234/v1"), + ("lm_studio", "http://127.0.0.1:1234/v1?q=1"), + ("lm_studio", "http://127.0.0.1:1234/v1#f"), + ("lm_studio", "http://127.0.0.1:1234/other"), + ("lm_studio", "http://127.0.0.1:1234/v1\u{7}"), + ] + .into_iter() + .map(|(kind, endpoint)| (kind, endpoint.to_owned())) + .chain([("lm_studio", long)]) + .collect(); + for (kind, endpoint) in &cases { + let output = scene.try_add_profile("bad", kind, "none", Some(endpoint)); + assert_error_code(&output, "control"); + let message = error_message(&output); + assert!( + message.contains("lm_studio endpoint") || message.contains("256 bytes"), + "{endpoint:?}: {message}" + ); + } + let output = scene.try_add_profile("bad", "openai", "api_key", Some("http://127.0.0.1:1/v1")); + assert_error_code(&output, "control"); + assert!(error_message(&output).contains("does not accept an endpoint")); + let status = json_output(&run_cli(scene.data(), &["workspace", "status"])); + assert!(status.get("provider_profiles").is_none(), "{status}"); +} + +#[test] +fn chat_with_a_profile_sends_to_the_profile_endpoint_and_to_no_other() { + let mut scene = Scene::new(); + let profile_server = Recorder::start("hello"); + let other = Recorder::start("hello"); + scene.add_profile_at("lms", "lm_studio", "api_key", &profile_server.endpoint); + scene.bind("lms", STORED); + + let args = scene.chat_args(None, Some("lms"), "hello"); + let output = run_with_env(scene.data(), &args, Some(("OMGCODE_LM_TOKEN", FROM_ENV))); + let requests = profile_server.finish(); + let untouched = other.finish(); + assert_success(&output); + assert_eq!(requests.len(), 1); + assert_eq!( + requests[0].header("authorization"), + Some(format!("Bearer {STORED}")) + ); + assert!(untouched.is_empty(), "the other endpoint saw a request"); +} + +#[test] +fn an_endpoint_beside_a_profile_is_a_usage_error_and_nothing_is_sent() { + let mut scene = Scene::new(); + let profile_server = Recorder::start("hello"); + let other = Recorder::start("hello"); + scene.add_profile_at("lms", "lm_studio", "api_key", &profile_server.endpoint); + scene.bind("lms", STORED); + + // `chat`, with the endpoint of the profile itself and with another one. + for endpoint in [&profile_server.endpoint, &other.endpoint] { + let args = scene.chat_args(Some(endpoint), Some("lms"), "hello"); + let output = run_with_env(scene.data(), &args, None); + assert_eq!(output.status.code(), Some(2), "{}", output_text(&output)); + assert!(output_text(&output).contains("--endpoint")); + } + // `session compact`, which needs no journal to reach argument parsing. + let compact: Vec = [ + "session", + "compact", + "journal.jsonl", + "--endpoint", + &other.endpoint, + "--model", + "fixture", + "--context-tokens", + "131072", + "--provider-profile", + "lms", + ] + .map(str::to_owned) + .to_vec(); + let output = run_with_env(scene.data(), &compact, None); + assert_eq!(output.status.code(), Some(2), "{}", output_text(&output)); + assert!(profile_server.finish().is_empty()); + assert!(other.finish().is_empty()); +} + +#[test] +fn a_profile_without_an_endpoint_fails_chat_and_compact_before_any_request() { + let mut scene = Scene::new(); + scene.add_profile("old-none", "lm_studio", "none"); + scene.add_profile("old-key", "lm_studio", "api_key"); + scene.bind("old-key", STORED); + let server = Recorder::start("hello"); + let journal = scene.fixture.data.join("unused.jsonl"); + for name in ["old-none", "old-key"] { + let args = scene.chat_args(None, Some(name), "hello"); + let output = run_with_env(scene.data(), &args, Some(("OMGCODE_LM_TOKEN", FROM_ENV))); + assert_error_code(&output, "credential"); + assert!(!output.status.success()); + let message = error_message(&output); + assert!(message.starts_with("endpoint_not_recorded:"), "{message}"); + assert!( + message.contains("add it again with an endpoint"), + "{message}" + ); + assert_no_token(&output_text(&output), &[STORED, FROM_ENV]); + + let compact: Vec = [ + "session", + "compact", + path_arg(&journal), + "--model", + "fixture", + "--context-tokens", + "131072", + "--provider-profile", + name, + ] + .map(str::to_owned) + .to_vec(); + let output = run_with_env(scene.data(), &compact, None); + assert_error_code(&output, "credential"); + assert!(error_message(&output).starts_with("endpoint_not_recorded:")); + } + assert!(server.finish().is_empty()); +} + +#[test] +fn capabilities_report_that_provider_profiles_can_record_an_endpoint() { + let scene = Scene::new(); + let output = run_cli(scene.data(), &["capabilities"]); + assert_success(&output); + let capabilities = json_output(&output); + assert_eq!(capabilities["provider_profile_endpoints"], true); + assert_eq!(capabilities["provider_profiles"], true); +} diff --git a/crates/omgcode-core/Cargo.toml b/crates/omgcode-core/Cargo.toml index eb29888..62baecc 100644 --- a/crates/omgcode-core/Cargo.toml +++ b/crates/omgcode-core/Cargo.toml @@ -16,6 +16,7 @@ thiserror.workspace = true reqwest.workspace = true tokio.workspace = true sha2.workspace = true +rusqlite.workspace = true [dev-dependencies] tempfile.workspace = true diff --git a/crates/omgcode-core/src/control.rs b/crates/omgcode-core/src/control.rs index 0893992..9c8ca7d 100644 --- a/crates/omgcode-core/src/control.rs +++ b/crates/omgcode-core/src/control.rs @@ -7,7 +7,7 @@ use std::collections::{BTreeMap, HashSet}; use std::fs::File; use std::io::{self, Read, Seek, SeekFrom, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; use serde::{Deserialize, Deserializer, Serialize}; use thiserror::Error; @@ -41,9 +41,10 @@ pub use container::{ }; pub use credential::{ CredentialBackend, CredentialBinding, CredentialError, CredentialId, CredentialStore, - CredentialStoreStatus, MAX_SECRET_BYTES, OrphanedCredential, ProviderCredentialError, - ProviderCredentialRemoval, ProviderCredentialSet, ProviderProfileRemoval, Secret, - delete_unbound_credential, probe_credential_store, + CredentialStoreStatus, LoadedProviderConnection, MAX_SECRET_BYTES, OrphanedCredential, + ProviderConnection, ProviderCredentialError, ProviderCredentialRemoval, ProviderCredentialSet, + ProviderCredentialUseError, ProviderProfileRemoval, Secret, delete_unbound_credential, + probe_credential_store, }; /// Whether this platform can record and apply container grants. pub use omgcode_platform::container::CONTAINER_GRANTS_SUPPORTED; @@ -60,8 +61,8 @@ pub use profile::{ is_reserved_variable, valid_profile_name, }; pub use provider::{ - MAX_PROVIDER_PROFILES, ProviderAuth, ProviderKind, ProviderProfile, ProviderProfileDeclaration, - ProviderProfileStatus, + MAX_PROVIDER_ENDPOINT_BYTES, MAX_PROVIDER_PROFILES, ProviderAuth, ProviderEndpoint, + ProviderKind, ProviderProfile, ProviderProfileDeclaration, ProviderProfileStatus, }; const FILE_NAME: &str = "control-v1.jsonl"; @@ -183,6 +184,32 @@ impl WorkspaceRegistration { pub const fn permissions(&self) -> PermissionSet { self.permissions } + + /// The directory recorded at registration, as a native path. + /// + /// This is a recorded value, not proof that the directory still exists or + /// is the one that was registered. A caller must open it through the + /// session or file layers, which re-validate the live identity, the + /// development root, and the permissions. The path alone grants nothing. + /// Returns `None` when the record was written with another operating + /// system's path encoding. + pub fn directory_path(&self) -> Option { + self.directory.path.to_path_buf().ok() + } + + /// The recorded directory as display text. + /// + /// The text is lossy and has control, invisible, and bidirectional + /// formatting characters replaced by visible escapes (see + /// [`crate::turn::display_safe_line`]). It is for people to read. Never use + /// it as an authorization key or to reopen the directory; use + /// [`Self::id`] and [`Self::directory_path`]. + pub fn directory_display(&self) -> String { + self.directory_path().map_or_else( + || String::from("(path not representable on this system)"), + |path| crate::turn::display_safe_line(&path.to_string_lossy()), + ) + } } /// Reject malformed persisted IDs before constructing typed workspace metadata. @@ -749,6 +776,15 @@ impl ControlStore { .find(|status| status.workspace_id == workspace_id) .map(|status| &status.directory) } + /// Iterate every registered workspace in registration order. + /// + /// The order is the settings journal's replay order, so it is + /// deterministic for one store state. The registrations are descriptive + /// settings, not capabilities; listing one grants no access. + pub fn workspaces(&self) -> impl Iterator { + self.snapshot.workspaces.iter() + } + /// Look up descriptive workspace metadata by its stable identifier. pub fn workspace(&self, id: WorkspaceId) -> Option<&WorkspaceRegistration> { self.snapshot diff --git a/crates/omgcode-core/src/control/approval/tests.rs b/crates/omgcode-core/src/control/approval/tests.rs index 4960187..968f98d 100644 --- a/crates/omgcode-core/src/control/approval/tests.rs +++ b/crates/omgcode-core/src/control/approval/tests.rs @@ -193,6 +193,7 @@ fn binding_and_settings_changes_refuse_consumption() { name: "work".into(), kind: "openai".into(), auth: "api_key".into(), + endpoint: None, }, store.revision(), ) diff --git a/crates/omgcode-core/src/control/credential.rs b/crates/omgcode-core/src/control/credential.rs index 225834f..c70f5b7 100644 --- a/crates/omgcode-core/src/control/credential.rs +++ b/crates/omgcode-core/src/control/credential.rs @@ -15,8 +15,9 @@ //! reported as uncertain. A recorded binding therefore never names an item //! that OMGCode deleted. //! -//! Only `api_key` profiles accept a credential for now. No provider adapter -//! reads a bound credential yet. +//! Only `api_key` profiles accept a credential for now. The local LM Studio +//! adapter is the only reader of a bound credential +//! ([`ControlStore::load_provider_credential`], ADR-0047). use serde::{Deserialize, Deserializer, Serialize, Serializer}; use thiserror::Error; @@ -27,7 +28,10 @@ pub use omgcode_platform::credentials::{ MAX_SECRET_BYTES, Secret, probe as probe_credential_store, }; -use super::{Action, ControlError, ControlSnapshot, ControlStore, ProviderAuth}; +use super::{ + Action, ControlError, ControlSnapshot, ControlStore, ProviderAuth, ProviderEndpoint, + ProviderKind, +}; /// One recorded credential binding: an opaque identifier and its backend. #[derive(Clone, Debug, Eq, PartialEq)] @@ -190,6 +194,182 @@ pub struct ProviderProfileRemoval { pub credential: Option, } +/// The decision for one selected provider profile: the endpoint it must use +/// and the binding it would read, if any. It borrows the control store, so it +/// cannot outlive the store that decided it. It holds no secret. +#[derive(Clone, Copy, Debug)] +pub struct ProviderConnection<'a> { + endpoint: &'a ProviderEndpoint, + binding: Option<&'a CredentialBinding>, +} + +impl<'a> ProviderConnection<'a> { + /// The profile's recorded endpoint. Nothing else may supply one. + pub const fn endpoint(&self) -> &'a ProviderEndpoint { + self.endpoint + } + + /// The bound credential, or `None` for an authentication mode that uses + /// none (`none`). + pub const fn binding(&self) -> Option<&'a CredentialBinding> { + self.binding + } +} + +/// A selected profile's endpoint and, for `api_key`, its secret, read from +/// one decision. The secret zeroes itself on drop and `Debug` redacts it. +pub struct LoadedProviderConnection { + endpoint: String, + secret: Option, +} + +impl LoadedProviderConnection { + /// The profile's recorded endpoint, exactly as validated. + pub fn endpoint(&self) -> &str { + &self.endpoint + } + + /// The loaded secret, if the profile uses one. + pub const fn secret(&self) -> Option<&Secret> { + self.secret.as_ref() + } + + /// Take the endpoint and secret apart. + pub fn into_parts(self) -> (String, Option) { + (self.endpoint, self.secret) + } +} + +impl std::fmt::Debug for LoadedProviderConnection { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter + .debug_struct("LoadedProviderConnection") + .field("endpoint", &self.endpoint) + .field("secret", &self.secret.as_ref().map(|_| "")) + .finish() + } +} + +/// A refusal or failure to read a bound provider credential for use. +/// +/// Every variant has a stable machine code ([`Self::code`]). No message +/// contains secret bytes; a profile name and an opaque credential identifier +/// may appear. A native failure keeps its category in +/// [`ProviderCredentialUseError::Store`]. +#[derive(Debug, Error)] +pub enum ProviderCredentialUseError { + /// The settings store could not be used (for example, a poisoned writer). + #[error("the settings store cannot be used: {0}")] + Settings(#[source] ControlError), + /// No provider profile has this name. + #[error("provider profile {profile} is not recorded")] + UnknownProfile { + /// The requested name. + profile: String, + }, + /// The profile is for a different provider than the adapter asking, so + /// its credential is never read for this adapter. + #[error( + "provider profile {profile} is for {}, not {}; its credential is not used for this adapter", + .actual.as_str(), + .expected.as_str() + )] + WrongProvider { + /// The profile name as recorded. + profile: String, + /// The provider kind of the adapter that asked. + expected: ProviderKind, + /// The provider kind the profile records. + actual: ProviderKind, + }, + /// The profile records no endpoint, so a selected profile has nowhere to + /// send a request. This applies to every authentication mode, and the + /// profile's endpoint cannot be set later (ADR-0048). + #[error( + "provider profile {profile} has no recorded endpoint; remove it and add it again with an endpoint" + )] + EndpointNotRecorded { + /// The profile name as recorded. + profile: String, + }, + /// The profile authenticates by subscription, which no adapter supports. + #[error("provider profile {profile} uses subscription authentication, which is not supported")] + SubscriptionUnsupported { + /// The profile name as recorded. + profile: String, + }, + /// The profile is `api_key` but has no bound credential. It never + /// becomes a request without a credential. + #[error("provider profile {profile} has no credential bound")] + NotBound { + /// The profile name as recorded. + profile: String, + }, + /// The bound credential was stored by a different backend than the one + /// this host opened. + #[error( + "credential {credential_id} of provider profile {profile} was stored by {}, not {}", + .stored.as_str(), + .current.as_str() + )] + OtherBackend { + /// The profile name as recorded. + profile: String, + /// The bound credential's identifier. + credential_id: CredentialId, + /// The backend recorded in the binding. + stored: CredentialBackend, + /// The backend of the open credential store. + current: CredentialBackend, + }, + /// The native store could not return the bound credential. + #[error("credential {credential_id} of provider profile {profile} could not be read: {error}")] + Store { + /// The profile name as recorded. + profile: String, + /// The bound credential's identifier. + credential_id: CredentialId, + /// The native failure, with its category. + #[source] + error: CredentialError, + }, +} + +impl ProviderCredentialUseError { + /// The stable machine-readable code of this failure. + /// + /// A [`Self::Store`] failure reports its native category. + pub const fn code(&self) -> &'static str { + match self { + Self::Settings(_) => "settings_unusable", + Self::UnknownProfile { .. } => "unknown_profile", + Self::WrongProvider { .. } => "wrong_provider", + Self::EndpointNotRecorded { .. } => "endpoint_not_recorded", + Self::SubscriptionUnsupported { .. } => "subscription_unsupported", + Self::NotBound { .. } => "not_bound", + Self::OtherBackend { .. } => "other_backend", + Self::Store { error, .. } => Self::native_code(error), + } + } + + /// The stable code of a native store failure's category. A caller that + /// opens the native store itself uses it to report an open failure with + /// the same codes. + pub const fn native_code(error: &CredentialError) -> &'static str { + match error { + CredentialError::NotFound => "credential_not_found", + CredentialError::Locked => "credential_locked", + CredentialError::Unavailable(_) => "credential_unavailable", + CredentialError::Corrupt(_) => "credential_corrupt", + CredentialError::PromptRequired => "credential_prompt_required", + CredentialError::Io(_) => "credential_io", + CredentialError::AlreadyExists + | CredentialError::TooLarge + | CredentialError::Invalid(_) => "credential_refused", + } + } +} + /// A credential command failure. Journal and native-store categories stay /// distinct, and no message contains secret bytes. #[derive(Debug, Error)] @@ -337,6 +517,142 @@ impl ControlStore { }) } + /// Decide whether the named profile may be used by one provider adapter, + /// and return the endpoint it must use with the binding it would read. + /// This takes no [`CredentialStore`], so it cannot touch native storage. + /// + /// The checks run in this order: + /// + /// 1. the store is healthy and the profile exists; + /// 2. the profile's kind equals `adapter`, so a key recorded for another + /// provider is never read for this one; + /// 3. the profile records an endpoint, whatever its authentication mode + /// ([`ProviderCredentialUseError::EndpointNotRecorded`]); + /// 4. the authentication mode: `none` yields no binding (no credential by + /// design), `subscription` is refused, and `api_key` continues; + /// 5. a credential is bound (an `api_key` profile without one is an error, + /// never a connection without a binding). + /// + /// The endpoint and the binding come from the same profile in one call, so + /// a caller cannot pair a credential with a destination from elsewhere. A + /// caller whose connection has no binding needs no native store at all. It + /// records nothing and does not change the revision. + /// + /// # Errors + /// + /// See [`ProviderCredentialUseError`]; each variant has a stable code. + pub fn provider_credential_binding( + &self, + name: &str, + adapter: ProviderKind, + ) -> Result, ProviderCredentialUseError> { + self.ensure_healthy() + .map_err(ProviderCredentialUseError::Settings)?; + let status = self.provider_profile(name).ok_or_else(|| { + ProviderCredentialUseError::UnknownProfile { + profile: name.to_owned(), + } + })?; + let profile = status.profile(); + if profile.kind() != adapter { + return Err(ProviderCredentialUseError::WrongProvider { + profile: profile.name().to_owned(), + expected: adapter, + actual: profile.kind(), + }); + } + let endpoint = + profile + .endpoint() + .ok_or_else(|| ProviderCredentialUseError::EndpointNotRecorded { + profile: profile.name().to_owned(), + })?; + match profile.auth() { + ProviderAuth::None => { + return Ok(ProviderConnection { + endpoint, + binding: None, + }); + } + ProviderAuth::Subscription => { + return Err(ProviderCredentialUseError::SubscriptionUnsupported { + profile: profile.name().to_owned(), + }); + } + ProviderAuth::ApiKey => {} + } + let binding = status + .credential() + .ok_or_else(|| ProviderCredentialUseError::NotBound { + profile: profile.name().to_owned(), + })?; + Ok(ProviderConnection { + endpoint, + binding: Some(binding), + }) + } + + /// Read the connection of the named profile for one provider adapter: its + /// recorded endpoint and, for an `api_key` profile, its credential. + /// + /// It runs [`Self::provider_credential_binding`] (checks 1 to 5), then: + /// + /// 6. the binding's backend is the one `credentials` opened. + /// + /// Only then does it call [`CredentialStore::load`]. Every refusal comes + /// before any native store access; a `none` profile returns its endpoint + /// and no secret, and its caller needs no native store. + /// + /// A read records nothing: it appends no journal event and does not change + /// the revision. The caller must keep this `ControlStore` open across the + /// call, so a rotation (which needs `&mut self` and the journal lock) cannot + /// delete the item between the binding lookup and the load, and must drop + /// it before any network I/O. The returned secret zeroes itself on drop. + /// + /// # Errors + /// + /// See [`ProviderCredentialUseError`]; each variant has a stable code. + pub fn load_provider_credential( + &self, + credentials: &CredentialStore, + name: &str, + adapter: ProviderKind, + ) -> Result { + let connection = self.provider_credential_binding(name, adapter)?; + let endpoint = connection.endpoint.as_str().to_owned(); + let Some(binding) = connection.binding else { + return Ok(LoadedProviderConnection { + endpoint, + secret: None, + }); + }; + // The binding exists, so the profile does; its recorded spelling names it. + let profile = self.provider_profile(name).map_or_else( + || name.to_owned(), + |status| status.profile().name().to_owned(), + ); + if binding.backend() != credentials.backend() { + return Err(ProviderCredentialUseError::OtherBackend { + profile, + credential_id: binding.id().clone(), + stored: binding.backend(), + current: credentials.backend(), + }); + } + let secret = + credentials + .load(binding.id()) + .map_err(|error| ProviderCredentialUseError::Store { + profile, + credential_id: binding.id().clone(), + error, + })?; + Ok(LoadedProviderConnection { + endpoint, + secret: Some(secret), + }) + } + /// Check the revision and that the profile accepts a credential. fn credential_profile(&self, name: &str, expected_revision: u64) -> Result { self.check_revision(expected_revision)?; diff --git a/crates/omgcode-core/src/control/credential/tests.rs b/crates/omgcode-core/src/control/credential/tests.rs index db19411..5fd0200 100644 --- a/crates/omgcode-core/src/control/credential/tests.rs +++ b/crates/omgcode-core/src/control/credential/tests.rs @@ -47,6 +47,7 @@ fn fixture() -> Fixture { name: name.into(), kind: kind.into(), auth: auth.into(), + endpoint: None, }, revision, ) @@ -675,3 +676,5 @@ fn recorded_bindings_reject_malformed_identifiers_and_backends() { ); } } + +mod loading; diff --git a/crates/omgcode-core/src/control/credential/tests/loading.rs b/crates/omgcode-core/src/control/credential/tests/loading.rs new file mode 100644 index 0000000..cedd515 --- /dev/null +++ b/crates/omgcode-core/src/control/credential/tests/loading.rs @@ -0,0 +1,439 @@ +//! Reading a bound credential for use (ADR-0047) against the real control +//! journal and the real native credential store. + +use super::*; + +const ENDPOINT: &str = "http://127.0.0.1:4242/v1"; + +/// Add an `lm_studio` profile with the given authentication mode and the +/// endpoint every selected profile needs (ADR-0048). +fn add_lm_studio(store: &mut ControlStore, name: &str, auth: &str) { + add_lm_studio_with(store, name, auth, Some(ENDPOINT)); +} + +/// Add an `lm_studio` profile that may record no endpoint, as one recorded +/// before endpoints existed. +fn add_lm_studio_with(store: &mut ControlStore, name: &str, auth: &str, endpoint: Option<&str>) { + let revision = store.revision(); + store + .add_provider_profile( + &ProviderProfileDeclaration { + name: name.into(), + kind: "lm_studio".into(), + auth: auth.into(), + endpoint: endpoint.map(str::to_owned), + }, + revision, + ) + .expect("lm_studio profile"); +} + +/// Give a recorded profile an endpoint without the add path, to reach the +/// checks that follow the endpoint check for a kind that cannot record one. +fn force_endpoint(store: &mut ControlStore, name: &str) { + store + .snapshot + .provider_profiles + .iter_mut() + .find(|status| status.profile().name() == name) + .expect("profile") + .profile + .endpoint = Some(ProviderEndpoint(ENDPOINT.to_owned())); +} + +fn lm_studio( + store: &ControlStore, + credentials: &CredentialStore, + name: &str, +) -> Result, ProviderCredentialUseError> { + store + .load_provider_credential(credentials, name, ProviderKind::LmStudio) + .map(|connection| connection.into_parts().1) +} + +/// A use of a stored credential returns exactly the stored bytes, records +/// nothing, and follows a rotation to the new secret. +#[test] +fn a_load_returns_the_stored_bytes_records_nothing_and_follows_rotation() { + let fixture = fixture(); + let (data, credentials) = (&fixture.data, &fixture.credentials); + let mut store = fixture.store(); + add_lm_studio(&mut store, "lms", "api_key"); + let revision = store.revision(); + let set = store + .set_provider_credential(credentials, "lms", &secret(SENTINEL), revision) + .expect("set"); + let revision = store.revision(); + let entries = store.entries.len(); + let length = store.bytes; + // The journal is locked while the store is open, so compare bytes through + // the store's own accounting and, after a reopen, through the file. + let loaded = lm_studio(&store, credentials, "LMS") + .expect("load") + .expect("a secret"); + assert_eq!(loaded.expose(), SENTINEL); + assert_eq!(format!("{loaded:?}"), "Secret()"); + assert_eq!(store.revision(), revision, "a read changes no revision"); + assert_eq!(store.entries.len(), entries); + assert_eq!(store.bytes, length, "a read appends nothing"); + drop(store); + let bytes_before = journal(data); + assert_eq!(bytes_before.len(), length); + let store = fixture.store(); + // The lookup is case-insensitive, like every profile lookup. + assert!(lm_studio(&store, credentials, "lms").is_ok()); + drop(store); + assert_eq!(journal(data), bytes_before, "a read appends nothing"); + let mut store = fixture.store(); + + let rotated = store + .set_provider_credential(credentials, "lms", &secret(b"rotated-key"), revision) + .expect("rotate"); + assert_ne!(rotated.binding.id(), set.binding.id()); + let loaded = lm_studio(&store, credentials, "lms") + .expect("load") + .expect("a secret"); + assert_eq!(loaded.expose(), b"rotated-key"); + + let revision = store.revision(); + let removal = store + .remove_provider_credential(credentials, "lms", revision) + .expect("cleanup"); + assert!(removal.orphan.is_none()); +} + +/// Each refusal is typed and carries a stable code. With the native item +/// deleted, a wrong-provider answer proves no load happened, because a load +/// would have reported the missing item instead. +#[test] +fn refusals_are_typed_and_precede_any_native_load() { + let fixture = fixture(); + let credentials = &fixture.credentials; + let mut store = fixture.store(); + add_lm_studio(&mut store, "lms-none", "none"); + add_lm_studio(&mut store, "lms-unbound", "api_key"); + + let error = lm_studio(&store, credentials, "missing").expect_err("unknown"); + assert!(matches!( + error, + ProviderCredentialUseError::UnknownProfile { .. } + )); + assert_eq!(error.code(), "unknown_profile"); + + // An `openai` key is never read for the LM Studio adapter. Its native item + // is gone, so a load would have reported not-found instead. + let revision = store.revision(); + let set = store + .set_provider_credential(credentials, "keys", &secret(SENTINEL), revision) + .expect("openai key"); + credentials.delete(set.binding.id()).expect("delete item"); + let error = lm_studio(&store, credentials, "keys").expect_err("wrong provider"); + assert!( + matches!( + error, + ProviderCredentialUseError::WrongProvider { + expected: ProviderKind::LmStudio, + actual: ProviderKind::OpenAi, + .. + } + ), + "{error:?}" + ); + assert_eq!(error.code(), "wrong_provider"); + assert!(error.to_string().contains("openai"), "{error}"); + // The kind check precedes the authentication check, so a keyless profile + // of another kind is refused too. + assert_eq!( + lm_studio(&store, credentials, "local") + .expect_err("kind") + .code(), + "wrong_provider" + ); + + // A `none` profile of the asking kind yields no credential. + assert!( + lm_studio(&store, credentials, "lms-none") + .expect("none") + .is_none() + ); + + // No LM Studio profile can be a subscription, so ask as the provider + // whose recorded profile is one. That kind cannot record an endpoint, so + // the endpoint check (which precedes the authentication check) refuses it + // first; `force_endpoint` then reaches the subscription refusal. + let error = store + .load_provider_credential(credentials, "plan", ProviderKind::Anthropic) + .expect_err("no endpoint"); + assert_eq!(error.code(), "endpoint_not_recorded"); + force_endpoint(&mut store, "plan"); + let error = store + .load_provider_credential(credentials, "plan", ProviderKind::Anthropic) + .expect_err("subscription"); + assert!(matches!( + error, + ProviderCredentialUseError::SubscriptionUnsupported { .. } + )); + assert_eq!(error.code(), "subscription_unsupported"); + + // An `api_key` profile with no binding is an error, never `None`. + let error = lm_studio(&store, credentials, "lms-unbound").expect_err("unbound"); + assert!(matches!(error, ProviderCredentialUseError::NotBound { .. })); + assert_eq!(error.code(), "not_bound"); + + // A binding by another backend is refused before any load. + let foreign = match credentials.backend() { + CredentialBackend::WindowsDpapi => CredentialBackend::LinuxSecretService, + CredentialBackend::LinuxSecretService => CredentialBackend::WindowsDpapi, + }; + let foreign_id = CredentialId::parse(&Uuid::new_v4().simple().to_string()).expect("id"); + store + .snapshot + .provider_profiles + .iter_mut() + .find(|status| status.profile().name() == "lms-unbound") + .expect("profile") + .credential = Some(CredentialBinding { + id: foreign_id.clone(), + backend: foreign, + }); + let error = lm_studio(&store, credentials, "lms-unbound").expect_err("backend"); + assert_eq!(error.code(), "other_backend"); + assert!(error.to_string().contains(foreign_id.as_str()), "{error}"); +} + +/// A binding whose native item is gone reports its category and identifier. +#[test] +fn a_missing_native_item_is_a_typed_not_found() { + let fixture = fixture(); + let credentials = &fixture.credentials; + let mut store = fixture.store(); + add_lm_studio(&mut store, "lms", "api_key"); + let revision = store.revision(); + let set = store + .set_provider_credential(credentials, "lms", &secret(SENTINEL), revision) + .expect("set"); + credentials.delete(set.binding.id()).expect("delete item"); + let error = lm_studio(&store, credentials, "lms").expect_err("not found"); + assert!( + matches!( + &error, + ProviderCredentialUseError::Store { + error: CredentialError::NotFound, + .. + } + ), + "{error:?}" + ); + assert_eq!(error.code(), "credential_not_found"); + let text = error.to_string(); + assert!(text.contains(set.binding.id().as_str()), "{text}"); + assert!(text.contains("lms"), "{text}"); + assert!(!text.contains("omgcode-core-sentinel"), "{text}"); + assert!(!format!("{error:?}").contains("omgcode-core-sentinel")); +} + +/// A poisoned store refuses a read before it looks at any profile. +#[test] +fn a_poisoned_store_refuses_a_read() { + let fixture = fixture(); + let mut store = fixture.store(); + add_lm_studio(&mut store, "lms", "none"); + store.poisoned = true; + let error = lm_studio(&store, &fixture.credentials, "lms").expect_err("poisoned"); + assert!(matches!( + error, + ProviderCredentialUseError::Settings(ControlError::Poisoned) + )); + assert_eq!(error.code(), "settings_unusable"); +} + +/// Native categories map to distinct, published codes. +#[test] +fn use_error_codes_are_stable() { + let id = CredentialId::parse(&Uuid::new_v4().simple().to_string()).expect("id"); + let store_error = |error| ProviderCredentialUseError::Store { + profile: "p".into(), + credential_id: id.clone(), + error, + }; + let cases = [ + (CredentialError::NotFound, "credential_not_found"), + (CredentialError::Locked, "credential_locked"), + ( + CredentialError::Unavailable("x".into()), + "credential_unavailable", + ), + (CredentialError::Corrupt("x"), "credential_corrupt"), + ( + CredentialError::PromptRequired, + "credential_prompt_required", + ), + ( + CredentialError::Io(std::io::Error::other("x")), + "credential_io", + ), + (CredentialError::TooLarge, "credential_refused"), + ]; + for (error, code) in cases { + assert_eq!(store_error(error).code(), code); + } +} + +/// The binding check decides every early outcome with no native store open: +/// nothing here constructs a `CredentialStore`. Each case that would hit two +/// refusals proves the order: unknown profile, kind, endpoint, then the +/// authentication checks. +#[test] +fn the_binding_check_decides_without_any_native_store() { + let directory = tempfile::tempdir().expect("temporary directory"); + let data = directory.path().join("data"); + private_data_root(&data); + let mut store = ControlStore::open(&data).expect("control store"); + add_lm_studio(&mut store, "lms-key", "api_key"); + add_lm_studio(&mut store, "lms-none", "none"); + add_lm_studio(&mut store, "lms-unbound", "api_key"); + // Recorded without an endpoint, as before endpoints existed. The + // api_key one is also unbound, so it would hit two refusals. + add_lm_studio_with(&mut store, "old-none", "none", None); + add_lm_studio_with(&mut store, "old-key", "api_key", None); + for (name, kind, auth) in [ + ("keys", "openai", "api_key"), + ("plan", "anthropic", "subscription"), + ] { + let revision = store.revision(); + store + .add_provider_profile( + &ProviderProfileDeclaration { + name: name.into(), + kind: kind.into(), + auth: auth.into(), + endpoint: None, + }, + revision, + ) + .expect("profile"); + } + let binding = CredentialBinding { + id: CredentialId::parse(&Uuid::new_v4().simple().to_string()).expect("id"), + backend: CredentialBackend::WindowsDpapi, + }; + store + .snapshot + .provider_profiles + .iter_mut() + .find(|status| status.profile().name() == "lms-key") + .expect("profile") + .credential = Some(binding.clone()); + let revision = store.revision(); + let entries = store.entries.len(); + + { + let check = |name: &str, adapter| store.provider_credential_binding(name, adapter); + let code = |name: &str, adapter| check(name, adapter).expect_err("refused").code(); + assert_eq!(code("missing", ProviderKind::LmStudio), "unknown_profile"); + assert_eq!(code("keys", ProviderKind::LmStudio), "wrong_provider"); + assert_eq!(code("lms-none", ProviderKind::OpenAi), "wrong_provider"); + // The kind check precedes the endpoint check: these profiles have no + // endpoint and are also for another kind. + assert_eq!(code("old-none", ProviderKind::OpenAi), "wrong_provider"); + assert_eq!(code("old-key", ProviderKind::OpenAi), "wrong_provider"); + // The endpoint check precedes every authentication check: `none`, + // `api_key` that is unbound, and `subscription` all report it. + assert_eq!( + code("old-none", ProviderKind::LmStudio), + "endpoint_not_recorded" + ); + assert_eq!( + code("old-key", ProviderKind::LmStudio), + "endpoint_not_recorded" + ); + assert_eq!( + code("plan", ProviderKind::Anthropic), + "endpoint_not_recorded" + ); + let error = check("OLD-KEY", ProviderKind::LmStudio).expect_err("refused"); + let text = error.to_string(); + assert!(text.contains("old-key"), "{text}"); + assert!(text.contains("no recorded endpoint"), "{text}"); + assert!(text.contains("add it again with an endpoint"), "{text}"); + assert_eq!(code("lms-unbound", ProviderKind::LmStudio), "not_bound"); + + let none = check("LMS-NONE", ProviderKind::LmStudio).expect("none"); + assert_eq!(none.endpoint().as_str(), ENDPOINT); + assert!(none.binding().is_none()); + let bound = check("lms-key", ProviderKind::LmStudio).expect("bound"); + assert_eq!(bound.endpoint().as_str(), ENDPOINT); + assert_eq!(bound.binding(), Some(&binding)); + } + // The authentication checks follow once an endpoint exists. + force_endpoint(&mut store, "plan"); + assert_eq!( + store + .provider_credential_binding("plan", ProviderKind::Anthropic) + .expect_err("subscription") + .code(), + "subscription_unsupported" + ); + assert_eq!(store.revision(), revision); + assert_eq!(store.entries.len(), entries, "a check records nothing"); + + store.poisoned = true; + assert_eq!( + store + .provider_credential_binding("lms-none", ProviderKind::LmStudio) + .expect_err("poisoned") + .code(), + "settings_unusable" + ); +} + +/// The loaded connection carries the endpoint the decision returned, exactly +/// as recorded, for both a keyless and a keyed profile, and a recorded +/// endpoint is not a secret while the secret never prints. +#[test] +fn a_load_returns_the_recorded_endpoint_with_the_secret() { + let fixture = fixture(); + let credentials = &fixture.credentials; + let mut store = fixture.store(); + add_lm_studio(&mut store, "lms-none", "none"); + add_lm_studio_with(&mut store, "lms", "api_key", Some("http://[::1]:4343/v1/")); + add_lm_studio_with(&mut store, "old", "api_key", None); + let revision = store.revision(); + let _ = store + .set_provider_credential(credentials, "lms", &secret(SENTINEL), revision) + .expect("set"); + let before = store.revision(); + + let connection = store + .load_provider_credential(credentials, "LMS", ProviderKind::LmStudio) + .expect("load"); + assert_eq!(connection.endpoint(), "http://[::1]:4343/v1/"); + assert_eq!(connection.secret().expect("secret").expose(), SENTINEL); + let printed = format!("{connection:?}"); + assert!(printed.contains("http://[::1]:4343/v1/"), "{printed}"); + assert!(printed.contains(""), "{printed}"); + assert!(!printed.contains("omgcode-core-sentinel"), "{printed}"); + let (endpoint, secret_value) = connection.into_parts(); + assert_eq!(endpoint, "http://[::1]:4343/v1/"); + assert_eq!(secret_value.expect("secret").expose(), SENTINEL); + + let none = store + .load_provider_credential(credentials, "lms-none", ProviderKind::LmStudio) + .expect("none"); + assert_eq!(none.endpoint(), ENDPOINT); + assert!(none.secret().is_none()); + + // No endpoint: refused before the native store is read, even though a + // keyed profile would otherwise report that nothing is bound. + let error = store + .load_provider_credential(credentials, "old", ProviderKind::LmStudio) + .expect_err("no endpoint"); + assert_eq!(error.code(), "endpoint_not_recorded"); + assert_eq!(store.revision(), before); + + let revision = store.revision(); + let removal = store + .remove_provider_credential(credentials, "lms", revision) + .expect("cleanup"); + assert!(removal.orphan.is_none()); +} diff --git a/crates/omgcode-core/src/control/provider.rs b/crates/omgcode-core/src/control/provider.rs index b552c43..2e5c7a1 100644 --- a/crates/omgcode-core/src/control/provider.rs +++ b/crates/omgcode-core/src/control/provider.rs @@ -1,20 +1,49 @@ //! Operator-declared provider connection profiles. //! -//! A profile records only a name, a provider kind, and an authentication mode. -//! It stores no endpoint, account, token, password, or other credential, and -//! no adapter uses it yet. It is configuration only. Its status can name the -//! opaque identifier of a credential bound through `credential`, never the -//! secret. See `docs/provider-authentication.md`, ADR-0038, and ADR-0042. +//! A profile records a name, a provider kind, an authentication mode, and +//! optionally an endpoint. It stores no account, token, password, or other +//! credential. Its status can name the opaque identifier of a credential bound +//! through `credential`, never the secret. +//! +//! An `lm_studio` profile may record the endpoint its adapter uses. The text +//! must pass the same rule the local adapter applies to every request +//! (`provider::validate_base_url`), is stored exactly as validated, and is +//! fixed for the life of the profile: no operation changes it, so a bound +//! credential cannot be moved to another destination. Every other kind refuses +//! an endpoint until an adapter defines its rule. Replay applies the same +//! validation. The local adapter reads a selected profile only through +//! `ControlStore::provider_credential_binding` (ADR-0047, ADR-0048). See +//! `docs/provider-authentication.md`, ADR-0038, and ADR-0042. use serde::{Deserialize, Serialize}; use uuid::Uuid; use super::credential::{self, CredentialBinding, ProviderProfileRemoval}; use super::{Action, ControlError, ControlSnapshot, ControlStore, valid_profile_name, valid_uuid}; +use crate::provider::validate_base_url; /// Most recorded provider profiles. pub const MAX_PROVIDER_PROFILES: usize = 32; +/// Most bytes of a recorded provider endpoint, checked before it is parsed. +pub const MAX_PROVIDER_ENDPOINT_BYTES: usize = 256; + +/// The endpoint a profile records, exactly as it was validated. +/// +/// Only [`ProviderProfile`] holds one. A value read from a journal is checked +/// by replay before the profile is used, and the store never returns an +/// unchecked profile. It is not a secret. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(transparent)] +pub struct ProviderEndpoint(pub(super) String); + +impl ProviderEndpoint { + /// The validated text, unchanged. + pub fn as_str(&self) -> &str { + &self.0 + } +} + /// A provider that a profile may name. #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum ProviderKind { @@ -100,9 +129,18 @@ pub struct ProviderProfile { name: String, kind: ProviderKind, auth: ProviderAuth, + /// Written only when present, so a profile without one keeps the bytes it + /// had before endpoints existed. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub(super) endpoint: Option, } impl ProviderProfile { + /// The recorded endpoint, if any. It never changes for this profile. + pub const fn endpoint(&self) -> Option<&ProviderEndpoint> { + self.endpoint.as_ref() + } + /// Stable profile identifier. pub const fn id(&self) -> Uuid { self.id @@ -129,7 +167,7 @@ impl ProviderProfile { #[derive(Clone, Debug, Serialize)] pub struct ProviderProfileStatus { #[serde(flatten)] - profile: ProviderProfile, + pub(super) profile: ProviderProfile, revision: u64, #[serde(serialize_with = "credential::serialize_state")] pub(super) credential: Option, @@ -161,6 +199,9 @@ pub struct ProviderProfileDeclaration { pub kind: String, /// `subscription`, `api_key`, or `none`. pub auth: String, + /// The endpoint to record. Only `lm_studio` accepts one for now, and it + /// must pass the local adapter's rule. It is stored exactly as given. + pub endpoint: Option, } impl ControlStore { @@ -205,6 +246,7 @@ impl ControlStore { name: declaration.name.clone(), kind, auth, + endpoint: declaration.endpoint.clone().map(ProviderEndpoint), }; validate_profile(&profile)?; self.append( @@ -314,9 +356,38 @@ fn validate_profile(profile: &ProviderProfile) -> Result<(), ControlError> { "this provider does not support the selected authentication mode", )); } + if let Some(endpoint) = &profile.endpoint { + validate_endpoint(profile.kind, endpoint.as_str())?; + } Ok(()) } +/// Check an endpoint for its provider kind. The length bound comes first, so +/// no parser sees an unbounded string. +fn validate_endpoint(kind: ProviderKind, text: &str) -> Result<(), ControlError> { + match kind { + ProviderKind::LmStudio => { + if text.len() > MAX_PROVIDER_ENDPOINT_BYTES { + return Err(ControlError::ProviderRefused( + "the provider endpoint is longer than 256 bytes", + )); + } + validate_base_url(text).map(|_| ()).map_err(|_| { + ControlError::ProviderRefused( + "an lm_studio endpoint must be http://:/v1 with no user information, query, or fragment", + ) + }) + } + ProviderKind::OpenAi + | ProviderKind::Anthropic + | ProviderKind::OllamaLocal + | ProviderKind::OllamaCloud + | ProviderKind::OpenRouter => Err(ControlError::ProviderRefused( + "this provider kind does not accept an endpoint yet", + )), + } +} + fn auth_allowed(kind: ProviderKind, auth: ProviderAuth) -> bool { match kind { ProviderKind::OpenAi | ProviderKind::Anthropic => { diff --git a/crates/omgcode-core/src/control/provider/tests.rs b/crates/omgcode-core/src/control/provider/tests.rs index be16849..50cb2f3 100644 --- a/crates/omgcode-core/src/control/provider/tests.rs +++ b/crates/omgcode-core/src/control/provider/tests.rs @@ -15,6 +15,7 @@ fn declaration(name: &str, kind: &str, auth: &str) -> ProviderProfileDeclaration name: name.into(), kind: kind.into(), auth: auth.into(), + endpoint: None, } } @@ -117,3 +118,239 @@ fn provider_authentication_pairs_are_supported() { .unwrap_or_else(|error| panic!("{kind}/{auth}: {error}")); } } + +const ENDPOINT: &str = "http://127.0.0.1:1234/v1"; + +fn with_endpoint( + name: &str, + kind: &str, + auth: &str, + endpoint: Option<&str>, +) -> ProviderProfileDeclaration { + ProviderProfileDeclaration { + endpoint: endpoint.map(str::to_owned), + ..declaration(name, kind, auth) + } +} + +fn refusal(result: Result) -> &'static str { + match result { + Err(ControlError::ProviderRefused(message)) => message, + other => panic!("expected a refusal, got {other:?}"), + } +} + +/// An `lm_studio` profile records the exact endpoint text, which survives +/// replay and appears in the serialized profile and status. +#[test] +fn an_lm_studio_profile_records_its_endpoint_exactly() { + let (_directory, data, mut store) = store(); + for (index, text) in [ENDPOINT, "http://[::1]:4321/v1/"].into_iter().enumerate() { + let revision = store.revision(); + let profile = store + .add_provider_profile( + &with_endpoint(&format!("p{index}"), "lm_studio", "api_key", Some(text)), + revision, + ) + .expect("profile"); + assert_eq!(profile.endpoint().map(ProviderEndpoint::as_str), Some(text)); + } + let status = store.provider_profile("P0").expect("profile"); + assert_eq!( + status.profile().endpoint().map(ProviderEndpoint::as_str), + Some(ENDPOINT) + ); + let encoded = serde_json::to_value(store.snapshot()).expect("snapshot"); + assert_eq!(encoded["provider_profiles"][0]["endpoint"], ENDPOINT); + assert_eq!( + encoded["provider_profiles"][1]["endpoint"], "http://[::1]:4321/v1/", + "stored without normalisation" + ); + drop(store); + let store = ControlStore::open(&data).expect("replay"); + assert_eq!( + store + .provider_profile("p1") + .and_then(|status| status.profile().endpoint()) + .map(ProviderEndpoint::as_str), + Some("http://[::1]:4321/v1/") + ); +} + +/// The profile uses the adapter's own rule: every destination the provider +/// module refuses is refused at add time, and nothing is recorded. +#[test] +fn every_endpoint_the_adapter_refuses_is_refused_at_add_time() { + let (_directory, _data, mut store) = store(); + for text in crate::provider::REFUSED_ENDPOINTS { + let message = refusal( + store.add_provider_profile(&with_endpoint("bad", "lm_studio", "none", Some(text)), 0), + ); + assert!( + message.contains("lm_studio endpoint"), + "{text:?}: {message}" + ); + } + let long = format!("http://127.0.0.1:{}1234/v1", "0".repeat(300)); + assert!( + crate::provider::validate_base_url(&long).is_ok(), + "only the length bound refuses this one" + ); + let message = refusal( + store.add_provider_profile(&with_endpoint("bad", "lm_studio", "none", Some(&long)), 0), + ); + assert!(message.contains("256 bytes"), "{message}"); + assert_eq!(MAX_PROVIDER_ENDPOINT_BYTES, 256); + assert_eq!(store.revision(), 0, "a refusal records nothing"); + let exact = format!( + "http://127.0.0.1:{}1234/v1", + "0".repeat(MAX_PROVIDER_ENDPOINT_BYTES - ENDPOINT.len()) + ); + assert_eq!(exact.len(), MAX_PROVIDER_ENDPOINT_BYTES); + store + .add_provider_profile(&with_endpoint("edge", "lm_studio", "none", Some(&exact)), 0) + .expect("exactly 256 bytes is accepted"); +} + +#[test] +fn a_refused_endpoint_writes_no_event() { + let (_directory, data, mut store) = store(); + let _ = refusal(store.add_provider_profile( + &with_endpoint("bad", "lm_studio", "none", Some("https://127.0.0.1:1/v1")), + 0, + )); + drop(store); + let text = fs::read_to_string(data.join(FILE_NAME)).expect("journal text"); + assert_eq!(text.matches("provider_profile_added").count(), 0, "{text}"); +} + +/// Kinds without an adapter refuse an endpoint, whatever its text. +#[test] +fn other_provider_kinds_refuse_an_endpoint() { + let (_directory, _data, mut store) = store(); + for (kind, auth) in [ + ("openai", "api_key"), + ("anthropic", "subscription"), + ("ollama_local", "none"), + ("ollama_cloud", "api_key"), + ("open_router", "api_key"), + ] { + let message = refusal( + store.add_provider_profile(&with_endpoint("other", kind, auth, Some(ENDPOINT)), 0), + ); + assert!(message.contains("does not accept an endpoint"), "{kind}"); + } + assert_eq!(store.revision(), 0); +} + +/// A profile without an endpoint still works and writes exactly the bytes it +/// wrote before endpoints existed. +#[test] +fn a_profile_without_an_endpoint_keeps_its_pre_endpoint_bytes() { + let (_directory, data, mut store) = store(); + let profile = store + .add_provider_profile(&declaration("legacy", "lm_studio", "none"), 0) + .expect("profile"); + assert!(profile.endpoint().is_none()); + let encoded = serde_json::to_string(&profile).expect("profile JSON"); + assert_eq!( + encoded, + format!( + r#"{{"id":"{}","name":"legacy","kind":"lm_studio","auth":"none"}}"#, + profile.id() + ) + ); + let status = serde_json::to_value(store.snapshot()).expect("snapshot"); + assert!(status["provider_profiles"][0].get("endpoint").is_none()); + drop(store); + let text = fs::read_to_string(data.join(FILE_NAME)).expect("journal text"); + let event = text + .lines() + .find(|line| line.contains("provider_profile_added")) + .expect("event"); + assert!(!event.contains("endpoint"), "{event}"); +} + +/// A pre-endpoint profile record deserializes with no endpoint and +/// re-serializes to the identical text. +#[test] +fn a_pre_endpoint_profile_record_round_trips() { + let text = r#"{"id":"0b7f2c2e-6f0a-4f6c-8f7e-1d2f3a4b5c6d","name":"legacy","kind":"lm_studio","auth":"api_key"}"#; + let profile: ProviderProfile = serde_json::from_str(text).expect("pre-change record"); + assert!(profile.endpoint().is_none()); + assert_eq!(serde_json::to_string(&profile).expect("JSON"), text); +} + +/// A journal written without endpoints replays, and re-serializing its +/// events reproduces the same bytes. +#[test] +fn a_pre_endpoint_journal_replays_and_reserializes_identically() { + let (_directory, data, mut store) = store(); + store + .add_provider_profile(&declaration("legacy", "lm_studio", "api_key"), 0) + .expect("profile"); + drop(store); + let bytes = fs::read(data.join(FILE_NAME)).expect("journal"); + let copy = tempfile::tempdir().expect("copy"); + let copied = copy.path().join("data"); + drop(ControlStore::open(&copied).expect("create the directory")); + fs::write(copied.join(FILE_NAME), &bytes).expect("fixture"); + let store = ControlStore::open(&copied).expect("replay"); + assert!( + store + .provider_profile("legacy") + .expect("profile") + .profile() + .endpoint() + .is_none() + ); + let mut again = Vec::new(); + for entry in &store.entries { + again.extend(serde_json::to_vec(entry).expect("event JSON")); + again.push(b'\n'); + } + assert_eq!(again, bytes); +} + +/// Replay applies the validation: a hand-edited journal cannot introduce an +/// endpoint the command would refuse, or any unknown field. +#[test] +fn replay_rejects_invalid_endpoints_and_unknown_fields() { + let (_directory, data, mut store) = store(); + store + .add_provider_profile( + &with_endpoint("edited", "lm_studio", "none", Some(ENDPOINT)), + 0, + ) + .expect("profile"); + store + .add_provider_profile(&declaration("plain", "openai", "api_key"), 1) + .expect("profile"); + drop(store); + let original = fs::read_to_string(data.join(FILE_NAME)).expect("journal"); + assert!(original.contains(ENDPOINT)); + let cases = [ + (ENDPOINT, "http://localhost:1234/v1"), + (ENDPOINT, "https://127.0.0.1:1234/v1"), + (ENDPOINT, "http://192.0.2.1:1234/v1"), + ( + r#""kind":"openai","auth":"api_key""#, + r#""kind":"openai","auth":"api_key","endpoint":"http://127.0.0.1:1234/v1""#, + ), + (r#""kind":"lm_studio""#, r#""kind":"lm_studio","extra":1"#), + ]; + for (from, to) in cases { + assert!(original.contains(from), "{from}"); + let edited = original.replacen(from, to, 1); + let directory = tempfile::tempdir().expect("copy"); + let path = directory.path().join("data"); + drop(ControlStore::open(&path).expect("directory")); + fs::write(path.join(FILE_NAME), edited).expect("edited fixture"); + let result = ControlStore::open(&path); + assert!( + matches!(result, Err(ControlError::Invalid(_))), + "{to}: {:?}", + result.err() + ); + } +} diff --git a/crates/omgcode-core/src/lib.rs b/crates/omgcode-core/src/lib.rs index bda43eb..8f01da1 100644 --- a/crates/omgcode-core/src/lib.rs +++ b/crates/omgcode-core/src/lib.rs @@ -14,6 +14,7 @@ pub mod replacement; pub mod run; pub mod run_journal; pub mod session; +pub mod session_index; mod storage; pub mod turn; mod workspace; diff --git a/crates/omgcode-core/src/provider.rs b/crates/omgcode-core/src/provider.rs index 3088b19..2b84f36 100644 --- a/crates/omgcode-core/src/provider.rs +++ b/crates/omgcode-core/src/provider.rs @@ -1,9 +1,14 @@ +mod stream; + use crate::prompt::{BASE_PROMPT, ModelMessage, ModelRole}; use crate::storage::parse_strict_json; -use crate::turn::{ModelError, ToolDefinition, ToolModel, ToolModelReply, ToolModelRequest}; +use crate::turn::{ + CancelToken, ModelError, ToolDefinition, ToolModel, ToolModelReply, ToolModelRequest, +}; +use reqwest::StatusCode; use reqwest::Url; use reqwest::blocking::{Client, Response}; -use reqwest::header::{AUTHORIZATION, HeaderValue}; +use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue}; use reqwest::redirect::Policy; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -17,6 +22,9 @@ const MAX_REQUEST_BYTES: usize = 256 * 1024; const MAX_RESPONSE_BYTES: u64 = 1024 * 1024; const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); const CONNECT_TIMEOUT: Duration = Duration::from_secs(5); +/// How often a streamed request looks at its cancel token while it waits. +/// It is also the longest a cancellation waits for the next wake-up. +const CANCEL_POLL: Duration = Duration::from_millis(25); const MESSAGE_OVERHEAD_ESTIMATE: u64 = 256; const PROMPT_HEADROOM_ESTIMATE: u64 = 512; /// Upper bound on advertised tool definitions in one request. @@ -116,6 +124,13 @@ pub enum ProviderError { /// The server requested a tool or function call, which this adapter does not support. #[error("local model tool calls are not supported")] UnsupportedToolCall, + /// A streamed response ended before its `[DONE]` marker. + #[error("local model stream ended before completion")] + IncompleteStream, + /// The caller's cancel token stopped a streamed request. Text already + /// delivered stays delivered and no reply exists. + #[error("local model request was cancelled")] + Cancelled, } #[derive(Serialize)] @@ -217,15 +232,63 @@ impl LocalModelClient { /// Model output remains untrusted plain text and is never interpreted as an /// action by this adapter. pub fn complete(&self, messages: &[ModelMessage]) -> Result { + let body = self.text_body(messages, false)?; + parse_response(self.send(body)?) + } + + /// Send one bounded streaming request and pass assistant text to `on_text`. + /// + /// The request is the same as [`Self::complete`] with `stream` set to `true` + /// and an `Accept: text/event-stream` header; `stream_options` is never + /// sent. The response is read as server-sent events under the strict rules + /// of ADR-0049. `on_text` receives each non-empty piece, in order, only + /// after the payload that carried it passed validation. That text is + /// provisional: if the stream fails later, the text already delivered stays + /// delivered, this method returns the error, and no reply exists. + /// + /// The request runs on an async client inside a single-thread runtime made + /// for this one call (ADR-0050). `cancel` is checked before anything is + /// sent, while the response head is awaited, and while each piece of the + /// body is awaited, at least every 25 ms. When it is set the request is + /// dropped, which closes the connection, and the result is `Cancelled`. + /// Dropping the request undoes no effect on the server. The method must not + /// be called from inside another async runtime. + /// + /// # Errors + /// + /// Besides the errors of [`Self::complete`], a stream that ends before + /// `[DONE]` is `IncompleteStream`, a response whose content type is not + /// `text/event-stream` is `MalformedResponse`, a read error or the request + /// deadline is `Transport`, and a set `cancel` is `Cancelled`. + pub fn complete_streaming( + &self, + messages: &[ModelMessage], + on_text: &mut dyn FnMut(&str), + cancel: &CancelToken, + ) -> Result { + let body = self.text_body(messages, true)?; + let outcome = self.stream_request(body, stream::StreamMode::Text, on_text, cancel)?; + Ok(ModelReply { + content: outcome.content, + finish_reason: outcome.finish_reason, + usage: outcome.usage, + }) + } + + /// Encode the text request with the chosen `stream` setting. + fn text_body( + &self, + messages: &[ModelMessage], + streaming: bool, + ) -> Result, ProviderError> { self.check_messages(messages, 0)?; - let body = serde_json::to_vec(&ChatRequest { + serde_json::to_vec(&ChatRequest { model: &self.model, messages, - stream: false, + stream: streaming, max_tokens: self.max_output_tokens, }) - .map_err(|_| ProviderError::MalformedResponse)?; - parse_response(self.send(body)?) + .map_err(|_| ProviderError::MalformedResponse) } /// Send one bounded request that advertises the supplied function tools. @@ -241,6 +304,43 @@ impl LocalModelClient { messages: &[ModelMessage], tools: &[ToolDefinition], ) -> Result { + let body = self.tool_body(messages, tools, false)?; + parse_tool_response(self.send(body)?) + } + + /// Send one bounded streaming request that advertises the supplied tools. + /// + /// This is [`Self::complete_with_tools`] with the stream rules of + /// [`Self::complete_streaming`]. Tool calls arrive in pieces and are + /// assembled by `index`; the returned calls hold only the members that were + /// received and stay raw and unvalidated, so the caller's validation judges + /// them exactly as it judges a non-streaming call. Only assistant text is + /// passed to `on_text`; call names and arguments never are. `cancel` stops + /// the request exactly as it does for [`Self::complete_streaming`], and a + /// partly received tool call is discarded with it. + pub fn complete_with_tools_streaming( + &self, + messages: &[ModelMessage], + tools: &[ToolDefinition], + on_text: &mut dyn FnMut(&str), + cancel: &CancelToken, + ) -> Result { + let body = self.tool_body(messages, tools, true)?; + let outcome = self.stream_request(body, stream::StreamMode::Tools, on_text, cancel)?; + Ok(ToolModelReply { + content: outcome.content, + tool_calls: outcome.tool_calls, + finish_reason: outcome.finish_reason, + }) + } + + /// Encode the tool request with the chosen `stream` setting. + fn tool_body( + &self, + messages: &[ModelMessage], + tools: &[ToolDefinition], + streaming: bool, + ) -> Result, ProviderError> { if tools.is_empty() || tools.len() > MAX_TOOL_DEFINITIONS { return Err(ProviderError::InvalidConfiguration); } @@ -256,16 +356,15 @@ impl LocalModelClient { messages, u64::try_from(tool_bytes.len()).map_err(|_| ProviderError::RequestTooLarge)?, )?; - let body = serde_json::to_vec(&ToolChatRequest { + serde_json::to_vec(&ToolChatRequest { model: &self.model, messages, - stream: false, + stream: streaming, max_tokens: self.max_output_tokens, tools: &tools, tool_choice: "auto", }) - .map_err(|_| ProviderError::MalformedResponse)?; - parse_tool_response(self.send(body)?) + .map_err(|_| ProviderError::MalformedResponse) } /// Require the compiled base prompt as the only system message and bound the request. @@ -304,7 +403,8 @@ impl LocalModelClient { Ok(()) } - /// Post one encoded request body to the validated loopback endpoint. + /// Post one encoded non-streaming request body to the validated loopback + /// endpoint with the blocking client. fn send(&self, body: Vec) -> Result { if body.len() > MAX_REQUEST_BYTES { return Err(ProviderError::RequestTooLarge); @@ -323,6 +423,114 @@ impl LocalModelClient { .send() .map_err(|_| ProviderError::Transport) } + + /// Run one streamed request on a single-thread runtime made for this call. + /// + /// The runtime and the async client are built here and dropped here: a + /// pooled connection must not outlive the runtime that drives it, so no + /// client is kept. A runtime that cannot be built is `Transport`. + fn stream_request( + &self, + body: Vec, + mode: stream::StreamMode, + on_text: &mut dyn FnMut(&str), + cancel: &CancelToken, + ) -> Result { + if body.len() > MAX_REQUEST_BYTES { + return Err(ProviderError::RequestTooLarge); + } + // Nothing is created, let alone connected, for a turn already cancelled. + if cancel.is_cancelled() { + return Err(ProviderError::Cancelled); + } + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_io() + .enable_time() + .build() + .map_err(|_| ProviderError::Transport)?; + runtime.block_on(self.stream_exchange(body, mode, on_text, cancel)) + } + + /// Send the request and feed the response body to the stream parser. + /// + /// Every wait goes through [`until_cancelled`], and the token is also + /// checked before each piece of the body, so a fast stream cannot starve + /// the check. Dropping the response or the pending request on any return + /// closes the connection. Text already passed to `on_text` stays delivered. + async fn stream_exchange( + &self, + body: Vec, + mode: stream::StreamMode, + on_text: &mut dyn FnMut(&str), + cancel: &CancelToken, + ) -> Result { + // The settings equal the blocking client's. The async client's + // `timeout` is a total deadline from the start of the request until the + // body has finished (reqwest 0.13.5, `async_impl/client.rs`). + let client = reqwest::Client::builder() + .no_proxy() + .redirect(Policy::none()) + .retry(reqwest::retry::never()) + .connect_timeout(CONNECT_TIMEOUT) + .timeout(self.request_timeout) + .build() + .map_err(|_| ProviderError::Transport)?; + let mut request = client + .post(self.endpoint.clone()) + .timeout(self.request_timeout) + .header(reqwest::header::CONTENT_TYPE, "application/json") + .header(reqwest::header::ACCEPT, "text/event-stream"); + if let Some(authorization) = &self.authorization { + request = request.header(AUTHORIZATION, authorization.clone()); + } + + let mut response = until_cancelled(request.body(body).send(), cancel) + .await? + .map_err(|_| ProviderError::Transport)?; + check_streaming_head(response.status(), response.headers())?; + + let mut parser = stream::StreamParser::new(mode); + loop { + // A fast stream never lets the poll interval elapse, so look here. + if cancel.is_cancelled() { + return Err(ProviderError::Cancelled); + } + let chunk = until_cancelled(response.chunk(), cancel) + .await? + .map_err(|_| ProviderError::Transport)?; + let Some(chunk) = chunk else { + // The body ended without `[DONE]`. + return parser.finish(); + }; + if parser.push(&chunk, on_text)? == stream::Flow::Done { + // A cancellation after `[DONE]` no longer changes the reply. + return parser.finish(); + } + } + } +} + +/// Await `future` while watching `cancel` every [`CANCEL_POLL`]. +/// +/// The future is pinned and polled through a timeout on `&mut`, so an elapsed +/// poll does not drop it and the request is not restarted. When the token is +/// set the future is dropped and the result is `Cancelled`. Whatever deadline +/// the future carries itself still applies. +async fn until_cancelled( + future: F, + cancel: &CancelToken, +) -> Result { + let mut future = std::pin::pin!(future); + loop { + match tokio::time::timeout(CANCEL_POLL, future.as_mut()).await { + Ok(output) => return Ok(output), + Err(_elapsed) => { + if cancel.is_cancelled() { + return Err(ProviderError::Cancelled); + } + } + } + } } /// Map the local client onto the turn runtime's model interface. @@ -331,6 +539,7 @@ impl LocalModelClient { /// it precisely; the runtime records only the coarse [`ModelError`]. pub struct LocalToolModel<'a> { client: &'a LocalModelClient, + on_text: Option<&'a mut dyn FnMut(&str)>, last_error: Option, } @@ -339,6 +548,20 @@ impl<'a> LocalToolModel<'a> { pub const fn new(client: &'a LocalModelClient) -> Self { Self { client, + on_text: None, + last_error: None, + } + } + + /// Wrap a validated client so each request is streamed. + /// + /// `on_text` receives the provisional assistant text of every request of + /// the turn, as described for + /// [`LocalModelClient::complete_with_tools_streaming`]. + pub const fn streaming(client: &'a LocalModelClient, on_text: &'a mut dyn FnMut(&str)) -> Self { + Self { + client, + on_text: Some(on_text), last_error: None, } } @@ -352,12 +575,21 @@ impl<'a> LocalToolModel<'a> { impl ToolModel for LocalToolModel<'_> { /// Send one request and remember the precise failure for reporting. fn complete(&mut self, request: ToolModelRequest<'_>) -> Result { - self.client - .complete_with_tools(request.messages, request.tools) - .map_err(|error| { - self.last_error = Some(error); - model_error(error) - }) + let result = match self.on_text.as_mut() { + Some(on_text) => self.client.complete_with_tools_streaming( + request.messages, + request.tools, + &mut **on_text, + request.cancel, + ), + None => self + .client + .complete_with_tools(request.messages, request.tools), + }; + result.map_err(|error| { + self.last_error = Some(error); + model_error(error) + }) } } @@ -365,7 +597,10 @@ impl ToolModel for LocalToolModel<'_> { const fn model_error(error: ProviderError) -> ModelError { match error { ProviderError::RequestTooLarge | ProviderError::ContextLimit => ModelError::RequestTooLarge, - ProviderError::Transport | ProviderError::HttpStatus(_) => ModelError::Transport, + ProviderError::Transport + | ProviderError::HttpStatus(_) + | ProviderError::IncompleteStream => ModelError::Transport, + ProviderError::Cancelled => ModelError::Cancelled, ProviderError::InvalidConfiguration | ProviderError::InvalidToken | ProviderError::ResponseTooLarge @@ -375,7 +610,11 @@ const fn model_error(error: ProviderError) -> ModelError { } /// Reject destinations which could redirect prompts or credentials off loopback. -fn validate_base_url(base_url: &str) -> Result { +/// +/// This is the one implementation of the local endpoint rule. The client +/// applies it to every request configuration, and a provider profile applies +/// it before it records an endpoint (ADR-0048), so the two cannot disagree. +pub(crate) fn validate_base_url(base_url: &str) -> Result { if base_url.chars().any(char::is_control) || !base_url.starts_with("http://") { return Err(ProviderError::InvalidConfiguration); } @@ -492,6 +731,28 @@ fn json_string_len(value: &str) -> Result { }) } +/// Accept only a successful `text/event-stream` head for a streamed request. +/// +/// A server that answers with one JSON document is refused rather than parsed: +/// the caller asked for a stream and nothing is repaired. Content-type +/// parameters such as `charset` are allowed and the media type is compared +/// case-insensitively. +fn check_streaming_head(status: StatusCode, headers: &HeaderMap) -> Result<(), ProviderError> { + if !status.is_success() { + return Err(ProviderError::HttpStatus(status.as_u16())); + } + let is_stream = headers + .get(reqwest::header::CONTENT_TYPE) + .and_then(|value| value.to_str().ok()) + .and_then(|value| value.split(';').next()) + .is_some_and(|media_type| media_type.trim().eq_ignore_ascii_case("text/event-stream")); + if is_stream { + Ok(()) + } else { + Err(ProviderError::MalformedResponse) + } +} + /// Read a successful response body within the byte limit. fn read_body(response: Response) -> Result, ProviderError> { let status = response.status(); @@ -660,10 +921,53 @@ fn parse_usage(value: &Value) -> Result { }) } +/// Destinations the endpoint rule refuses. The provider profile tests use the +/// same list, so the rule is proved to be shared. +#[cfg(test)] +pub(crate) const REFUSED_ENDPOINTS: &[&str] = &[ + "http://localhost:1234/v1", + "http://192.0.2.1:1234/v1", + "https://127.0.0.1:1234/v1", + "http://127.0.0.1/v1", + "http://127.0.0.1:0/v1", + "http://127.0.0.1:1234/v1/other", + "http://user@127.0.0.1:1234/v1", + "http://127.0.0.1:1234/v1?query=1", + "http://127.0.0.1:1234/v1#fragment", + "http://127.0.0.1:1234/v1\u{7}", + "", +]; + #[cfg(test)] mod tests { use super::*; use std::io::Write; + + /// The client refuses every listed destination; the profile record tests + /// run the same list through `validate_base_url`'s other caller. + #[test] + fn the_client_refuses_every_listed_endpoint() { + for base_url in REFUSED_ENDPOINTS { + let result = LocalModelClient::new( + LocalModelConfig { + base_url: (*base_url).to_owned(), + model: "m".to_owned(), + context_tokens: 4096, + max_output_tokens: 64, + }, + None, + ); + assert_eq!( + result.err(), + Some(ProviderError::InvalidConfiguration), + "{base_url:?}" + ); + assert!(validate_base_url(base_url).is_err(), "{base_url:?}"); + } + assert!(validate_base_url("http://127.0.0.1:1234/v1").is_ok()); + assert!(validate_base_url("http://[::1]:1234/v1/").is_ok()); + } + use std::net::TcpListener; use std::sync::mpsc; use std::thread; @@ -728,6 +1032,7 @@ mod tests { /// Give the server its own deadline and cancellation path, independent of HTTP timeouts. fn slow_response( drip_body: bool, + streamed: bool, ) -> (LocalModelClient, mpsc::Sender<()>, thread::JoinHandle) { let listener = TcpListener::bind("127.0.0.1:0").expect("private fixture"); listener.set_nonblocking(true).expect("bounded accept"); @@ -782,9 +1087,18 @@ mod tests { bytes.extend_from_slice(&buffer[..count]); } if drip_body { - let body = br#"{"choices":[{"message":{"role":"assistant","content":"completed slowly"},"finish_reason":"stop"}]}"#; + let body: &[u8] = if streamed { + b"data: {\"choices\":[{\"delta\":{\"content\":\"completed slowly\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n" + } else { + br#"{"choices":[{"message":{"role":"assistant","content":"completed slowly"},"finish_reason":"stop"}]}"# + }; + let content_type = if streamed { + "Content-Type: text/event-stream\r\n" + } else { + "" + }; let headers = format!( - "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + "HTTP/1.1 200 OK\r\n{content_type}Content-Length: {}\r\nConnection: close\r\n\r\n", body.len() ); if stream.write_all(headers.as_bytes()).is_err() { @@ -824,10 +1138,10 @@ mod tests { } /// Observe fixture cleanup even when the timeout assertion is expected to fail. - fn assert_bounded_transport_failure(drip_body: bool) { - let (client, cancel, task) = slow_response(drip_body); + fn assert_bounded_transport_failure(drip_body: bool, streamed: bool) { + let (client, cancel, task) = slow_response(drip_body, streamed); let started = Instant::now(); - let result = client.complete(&[ + let messages = [ ModelMessage { role: ModelRole::System, content: BASE_PROMPT.to_owned(), @@ -836,7 +1150,12 @@ mod tests { role: ModelRole::User, content: "request".to_owned(), }, - ]); + ]; + let result = if streamed { + client.complete_streaming(&messages, &mut |_: &str| {}, &CancelToken::new()) + } else { + client.complete(&messages) + }; let elapsed = started.elapsed(); // Receiver disconnection also requests cleanup; either outcome is safe. // A disconnected receiver means the fixture is already exiting. The @@ -860,11 +1179,21 @@ mod tests { #[test] fn request_deadline_bounds_stalled_response_headers() { - assert_bounded_transport_failure(false); + assert_bounded_transport_failure(false, false); + } + + #[test] + fn request_deadline_bounds_stalled_streamed_response_headers() { + assert_bounded_transport_failure(false, true); } #[test] fn request_deadline_bounds_a_dripping_response_body() { - assert_bounded_transport_failure(true); + assert_bounded_transport_failure(true, false); + } + + #[test] + fn request_deadline_bounds_a_dripping_streamed_body() { + assert_bounded_transport_failure(true, true); } } diff --git a/crates/omgcode-core/src/provider/stream.rs b/crates/omgcode-core/src/provider/stream.rs new file mode 100644 index 0000000..c84fc4a --- /dev/null +++ b/crates/omgcode-core/src/provider/stream.rs @@ -0,0 +1,1065 @@ +//! Strict reader for one streamed (server-sent events) Chat Completions reply. +//! +//! The parser is fed bytes and does no I/O, so the stream rules can be tested +//! without a socket and apply to any chunking of the input. It refuses anything it does not understand and +//! repairs nothing. Text reaches the caller only from a payload that passed +//! every check, so a later failure can discard text but never deliver text +//! from a payload that was itself refused (ADR-0049). + +use super::{ + MAX_RESPONSE_BYTES, MAX_RESPONSE_TOOL_CALLS, ProviderError, Usage, parse_usage, reject_actions, +}; +use crate::storage::parse_strict_json; +use serde_json::{Map, Value}; +#[cfg(test)] +use std::io::Read; + +/// Whether the request advertised tools, which decides how a reply is judged. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(super) enum StreamMode { + /// A text-only request: any tool call or tool finish reason is refused. + Text, + /// A tool-capable request: tool calls are assembled from their pieces. + Tools, +} + +/// A fully validated streamed reply. +#[derive(Debug, PartialEq)] +pub(super) struct StreamOutcome { + /// The concatenated assistant text. It is untrusted model output. + pub(super) content: String, + /// The one finish reason the stream reported. + pub(super) finish_reason: String, + /// The last usage object the server sent, when any. + pub(super) usage: Option, + /// Assembled raw tool calls holding only the members that were received. + pub(super) tool_calls: Vec, +} + +/// Whether the parser wants more bytes. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(super) enum Flow { + /// The stream is not complete; feed more bytes. + Continue, + /// `[DONE]` was processed. Nothing after it is interpreted. + Done, +} + +/// An incremental parser for one event stream, fed bytes in any chunking. +/// +/// The parser owns the stream state, the partial line, and the byte total, so +/// the same rules apply however the transport splits the bytes. It performs no +/// I/O and never waits. `on_text` receives each non-empty piece of assistant +/// text, in order, after the payload that carried it passed validation. The +/// whole stream is bounded by the response byte limit. After an error the +/// parser must be discarded. +pub(super) struct StreamParser { + state: StreamState, + line: Vec, + total: u64, + done: bool, +} + +impl StreamParser { + /// Start a parser for one reply. + pub(super) const fn new(mode: StreamMode) -> Self { + Self { + state: StreamState::new(mode), + line: Vec::new(), + total: 0, + done: false, + } + } + + /// Feed the next bytes of the stream. + /// + /// Returns [`Flow::Done`] once `[DONE]` has been processed; later bytes, + /// including the rest of this chunk, are neither counted nor interpreted. + /// + /// # Errors + /// + /// A line or payload that breaks the stream rules fails as in ADR-0049, + /// and more than the response byte limit is `ResponseTooLarge`. + pub(super) fn push( + &mut self, + bytes: &[u8], + on_text: &mut dyn FnMut(&str), + ) -> Result { + if self.done { + return Ok(Flow::Done); + } + for &byte in bytes { + // Counting per byte stops exactly at the limit and reads no byte + // beyond a `[DONE]` that arrived in the same chunk. + self.total = self + .total + .checked_add(1) + .filter(|total| *total <= MAX_RESPONSE_BYTES) + .ok_or(ProviderError::ResponseTooLarge)?; + if byte != b'\n' { + self.line.push(byte); + continue; + } + if self.line.last() == Some(&b'\r') { + self.line.pop(); + } + let flow = self.state.handle_line(&self.line, on_text)?; + self.line.clear(); + if flow == Flow::Done { + self.done = true; + return Ok(Flow::Done); + } + } + Ok(Flow::Continue) + } + + /// Finish the stream and return the validated reply. + /// + /// Call it when [`Self::push`] returned [`Flow::Done`] or when the input + /// ended. A stream that ended before `[DONE]` is `IncompleteStream`, and a + /// partial last line is discarded: it was never a complete event. + pub(super) fn finish(self) -> Result { + if self.done { + self.state.finish() + } else { + Err(ProviderError::IncompleteStream) + } + } +} + +/// Read one event stream to its `[DONE]` marker. +/// +/// A read error is `Transport`; a stream that ends before `[DONE]` is +/// `IncompleteStream`. The production transport feeds [`StreamParser`] from an +/// async body; tests use this wrapper to exercise the same rules over [`Read`]. +#[cfg(test)] +pub(super) fn read_event_stream( + mut reader: impl Read, + mode: StreamMode, + on_text: &mut dyn FnMut(&str), +) -> Result { + let mut parser = StreamParser::new(mode); + let mut chunk = [0_u8; 8192]; + loop { + let count = reader + .read(&mut chunk) + .map_err(|_| ProviderError::Transport)?; + if count == 0 { + return parser.finish(); + } + let bytes = chunk.get(..count).ok_or(ProviderError::Transport)?; + if parser.push(bytes, on_text)? == Flow::Done { + return parser.finish(); + } + } +} + +/// One tool call being assembled. Fields stay unset until a piece carries them. +#[derive(Default)] +struct PartialCall { + id: Option, + kind_seen: bool, + function_seen: bool, + name: Option, + arguments: Option, +} + +impl PartialCall { + /// Merge one call piece. Only the keys the adapter understands are allowed. + /// + /// A null member is treated as absent: it carries no data, and servers + /// send null for the members a later piece does not repeat. + fn apply(&mut self, entry: &Map) -> Result<(), ProviderError> { + if let Some(id) = present(entry.get("id")) { + let id = id.as_str().ok_or(ProviderError::MalformedResponse)?; + match &self.id { + Some(existing) if existing != id => { + return Err(ProviderError::MalformedResponse); + } + Some(_) => {} + None => self.id = Some(id.to_owned()), + } + } + if let Some(kind) = present(entry.get("type")) { + if kind.as_str() != Some("function") { + return Err(ProviderError::MalformedResponse); + } + self.kind_seen = true; + } + if let Some(function) = present(entry.get("function")) { + let function = function + .as_object() + .ok_or(ProviderError::MalformedResponse)?; + if function + .keys() + .any(|key| !matches!(key.as_str(), "name" | "arguments")) + { + return Err(ProviderError::MalformedResponse); + } + self.function_seen = true; + append_piece(&mut self.name, function.get("name"))?; + append_piece(&mut self.arguments, function.get("arguments"))?; + } + Ok(()) + } + + /// Build the raw call value from the members that were actually received. + fn into_value(self) -> Value { + let mut call = Map::new(); + if let Some(id) = self.id { + call.insert("id".to_owned(), Value::String(id)); + } + if self.kind_seen { + call.insert("type".to_owned(), Value::String("function".to_owned())); + } + if self.function_seen { + let mut function = Map::new(); + if let Some(name) = self.name { + function.insert("name".to_owned(), Value::String(name)); + } + if let Some(arguments) = self.arguments { + function.insert("arguments".to_owned(), Value::String(arguments)); + } + call.insert("function".to_owned(), Value::Object(function)); + } + Value::Object(call) + } +} + +/// A member that is present and not null. +fn present(value: Option<&Value>) -> Option<&Value> { + value.filter(|value| !value.is_null()) +} + +/// Append one string piece; absent or null pieces change nothing, other types fail. +fn append_piece(slot: &mut Option, piece: Option<&Value>) -> Result<(), ProviderError> { + match piece { + None | Some(Value::Null) => Ok(()), + Some(Value::String(text)) => { + slot.get_or_insert_with(String::new).push_str(text); + Ok(()) + } + Some(_) => Err(ProviderError::MalformedResponse), + } +} + +struct StreamState { + mode: StreamMode, + content: String, + finish_reason: Option, + usage: Option, + calls: Vec, +} + +impl StreamState { + const fn new(mode: StreamMode) -> Self { + Self { + mode, + content: String::new(), + finish_reason: None, + usage: None, + calls: Vec::new(), + } + } + + /// Judge one complete line (without its line ending). + fn handle_line( + &mut self, + raw: &[u8], + on_text: &mut dyn FnMut(&str), + ) -> Result { + let line = std::str::from_utf8(raw).map_err(|_| ProviderError::MalformedResponse)?; + if line.is_empty() || line.starts_with(':') { + return Ok(Flow::Continue); + } + let (field, value) = line.split_once(':').unwrap_or((line, "")); + let value = value.strip_prefix(' ').unwrap_or(value); + match field { + "data" => {} + "event" | "id" | "retry" => return Ok(Flow::Continue), + _ => return Err(ProviderError::MalformedResponse), + } + if value == "[DONE]" { + return if self.finish_reason.is_some() { + Ok(Flow::Done) + } else { + Err(ProviderError::MalformedResponse) + }; + } + self.handle_payload(value.as_bytes(), on_text)?; + Ok(Flow::Continue) + } + + /// Validate one JSON payload completely, then deliver its text. + fn handle_payload( + &mut self, + bytes: &[u8], + on_text: &mut dyn FnMut(&str), + ) -> Result<(), ProviderError> { + let value = parse_strict_json(bytes).map_err(|_| ProviderError::MalformedResponse)?; + let object = value.as_object().ok_or(ProviderError::MalformedResponse)?; + // The error body is never inspected or reported. + if object.get("error").is_some_and(|error| !error.is_null()) { + return Err(ProviderError::MalformedResponse); + } + let usage = match object.get("usage") { + None | Some(Value::Null) => None, + Some(usage) => Some(parse_usage(usage)?), + }; + let choices = object + .get("choices") + .and_then(Value::as_array) + .ok_or(ProviderError::MalformedResponse)?; + let piece = match choices.as_slice() { + [] => None, + [choice] => self.handle_choice(choice)?, + _ => return Err(ProviderError::MalformedResponse), + }; + if usage.is_some() { + self.usage = usage; + } + if let Some(text) = piece.filter(|text| !text.is_empty()) { + self.content.push_str(text); + on_text(text); + } + Ok(()) + } + + /// Validate the one choice of a payload and return its text piece. + fn handle_choice<'v>(&mut self, choice: &'v Value) -> Result, ProviderError> { + if self.finish_reason.is_some() { + return Err(ProviderError::MalformedResponse); + } + let choice = choice.as_object().ok_or(ProviderError::MalformedResponse)?; + if let Some(index) = choice.get("index") + && index.as_u64() != Some(0) + { + return Err(ProviderError::MalformedResponse); + } + reject_actions(choice.get("tool_calls"), choice.get("function_call"))?; + let delta = choice + .get("delta") + .and_then(Value::as_object) + .ok_or(ProviderError::MalformedResponse)?; + if let Some(role) = present(delta.get("role")) + && role.as_str() != Some("assistant") + { + return Err(ProviderError::MalformedResponse); + } + reject_actions(None, delta.get("function_call"))?; + match (self.mode, delta.get("tool_calls")) { + (_, None | Some(Value::Null)) => {} + (StreamMode::Text, Some(Value::Array(calls))) if calls.is_empty() => {} + (StreamMode::Text, Some(Value::Array(_))) => { + return Err(ProviderError::UnsupportedToolCall); + } + (StreamMode::Tools, Some(Value::Array(calls))) => self.assemble(calls)?, + (_, Some(_)) => return Err(ProviderError::MalformedResponse), + } + let piece = match delta.get("content") { + None | Some(Value::Null) => None, + Some(Value::String(text)) => Some(text.as_str()), + Some(_) => return Err(ProviderError::MalformedResponse), + }; + match choice.get("finish_reason") { + None | Some(Value::Null) => {} + Some(Value::String(reason)) => self.accept_finish(reason)?, + Some(_) => return Err(ProviderError::MalformedResponse), + } + Ok(piece) + } + + /// Merge the call pieces of one delta, in order, by `index`. + fn assemble(&mut self, entries: &[Value]) -> Result<(), ProviderError> { + for entry in entries { + let entry = entry.as_object().ok_or(ProviderError::MalformedResponse)?; + if entry + .keys() + .any(|key| !matches!(key.as_str(), "index" | "id" | "type" | "function")) + { + return Err(ProviderError::MalformedResponse); + } + let index = entry + .get("index") + .and_then(Value::as_u64) + .and_then(|index| usize::try_from(index).ok()) + .ok_or(ProviderError::MalformedResponse)?; + if index == self.calls.len() && index < MAX_RESPONSE_TOOL_CALLS { + self.calls.push(PartialCall::default()); + } + self.calls + .get_mut(index) + .ok_or(ProviderError::MalformedResponse)? + .apply(entry)?; + } + Ok(()) + } + + /// Accept the one finish reason, judged like the non-streaming parsers. + fn accept_finish(&mut self, reason: &str) -> Result<(), ProviderError> { + match self.mode { + StreamMode::Text => { + if matches!(reason, "tool_calls" | "function_call") { + return Err(ProviderError::UnsupportedToolCall); + } + if !matches!(reason, "stop" | "length") { + return Err(ProviderError::MalformedResponse); + } + } + StreamMode::Tools => { + let accepted = if self.calls.is_empty() { + matches!(reason, "stop" | "length") + } else { + matches!(reason, "tool_calls" | "stop") + }; + if !accepted { + return Err(ProviderError::MalformedResponse); + } + } + } + self.finish_reason = Some(reason.to_owned()); + Ok(()) + } + + fn finish(self) -> Result { + let finish_reason = self.finish_reason.ok_or(ProviderError::MalformedResponse)?; + Ok(StreamOutcome { + content: self.content, + finish_reason, + usage: self.usage, + tool_calls: self + .calls + .into_iter() + .map(PartialCall::into_value) + .collect(), + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + /// Run the reader over a fixed input and collect the delivered pieces. + fn run(mode: StreamMode, input: &[u8]) -> (Result, Vec) { + let mut pieces = Vec::new(); + let result = read_event_stream(Cursor::new(input.to_vec()), mode, &mut |text: &str| { + pieces.push(text.to_owned()); + }); + (result, pieces) + } + + fn delta(content: &str) -> String { + format!( + "data: {}\n\n", + serde_json::json!({"choices":[{"index":0,"delta":{"role":"assistant","content":content},"finish_reason":null}]}) + ) + } + + fn finish(reason: &str) -> String { + format!( + "data: {}\n\n", + serde_json::json!({"choices":[{"index":0,"delta":{},"finish_reason":reason}]}) + ) + } + + const DONE: &str = "data: [DONE]\n\n"; + + fn text_stream(parts: &[&str]) -> String { + let mut stream = String::new(); + for part in parts { + stream.push_str(&delta(part)); + } + stream.push_str(&finish("stop")); + stream.push_str(DONE); + stream + } + + #[test] + fn text_pieces_arrive_in_order_and_build_the_reply() { + // Given a stream of three text pieces and a finish reason. + let input = text_stream(&["Hel", "lo ", "there"]); + + // When the reader runs. + let (result, pieces) = run(StreamMode::Text, input.as_bytes()); + + // Then the reply is the concatenation and the callback saw each piece. + let outcome = result.expect("valid stream"); + assert_eq!(outcome.content, "Hello there"); + assert_eq!(outcome.finish_reason, "stop"); + assert_eq!(outcome.usage, None); + assert!(outcome.tool_calls.is_empty()); + assert_eq!(pieces, ["Hel", "lo ", "there"]); + } + + #[test] + fn crlf_comments_and_ignored_fields_are_accepted() { + // Given CRLF line ends, comments, ignored fields, and `data:` without a space. + let first = delta("a").trim_end().replace('\n', "\r\n"); + let input = format!( + ": keep-alive\r\nevent: message\r\nid: 7\r\nretry: 100\r\n{first}\r\n\r\ndata:{}\r\ndata: [DONE]\r\n", + serde_json::json!({"choices":[{"index":0,"delta":{"content":"b"},"finish_reason":"stop"}]}) + ); + + // When the reader runs. + let (result, pieces) = run(StreamMode::Text, input.as_bytes()); + + // Then every piece is delivered and the ignored lines changed nothing. + assert_eq!(result.expect("valid stream").content, "ab"); + assert_eq!(pieces, ["a", "b"]); + } + + #[test] + fn an_unknown_field_line_is_refused() { + let input = format!("{}bogus: 1\n\n{}", delta("a"), DONE); + let (result, _) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result, Err(ProviderError::MalformedResponse)); + } + + #[test] + fn usage_on_a_payload_without_choices_is_taken_and_absent_usage_stays_absent() { + // Given a usage payload with empty choices after the finish reason. + let mut input = delta("hi"); + input.push_str(&finish("stop")); + input.push_str( + "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":1,\"total_tokens\":4}}\n\n", + ); + input.push_str(DONE); + + // When the reader runs. + let (result, _) = run(StreamMode::Text, input.as_bytes()); + + // Then the usage is kept; a stream without usage has none. + assert_eq!( + result.expect("valid stream").usage, + Some(Usage { + prompt_tokens: Some(3), + completion_tokens: Some(1), + total_tokens: Some(4), + }) + ); + let (plain, _) = run(StreamMode::Text, text_stream(&["x"]).as_bytes()); + assert_eq!(plain.expect("valid stream").usage, None); + } + + #[test] + fn end_of_input_before_done_is_incomplete() { + let mut input = delta("hi"); + input.push_str(&finish("stop")); + let (result, pieces) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result, Err(ProviderError::IncompleteStream)); + assert_eq!(pieces, ["hi"]); + + // A final line without its line feed was never a complete event. + let (result, _) = run(StreamMode::Text, format!("{input}data: [DONE]").as_bytes()); + assert_eq!(result, Err(ProviderError::IncompleteStream)); + } + + #[test] + fn a_refused_stream_shape_fails_without_repair() { + let cases: Vec<(&str, String)> = vec![ + ("done before finish", format!("{}{DONE}", delta("a"))), + ( + "content after finish", + format!("{}{}{DONE}", finish("stop"), delta("late")), + ), + ( + "second finish", + format!("{}{}{DONE}", finish("stop"), finish("stop")), + ), + ( + "duplicate key", + format!( + "data: {{\"choices\":[{{\"delta\":{{\"content\":\"a\",\"content\":\"b\"}}}}]}}\n\n{DONE}" + ), + ), + ("non-object payload", format!("data: [1]\n\n{DONE}")), + ( + "two choices", + format!("data: {{\"choices\":[{{\"delta\":{{}}}},{{\"delta\":{{}}}}]}}\n\n{DONE}"), + ), + ( + "choice index 1", + format!( + "data: {{\"choices\":[{{\"index\":1,\"delta\":{{\"content\":\"a\"}}}}]}}\n\n{DONE}" + ), + ), + ( + "role user", + format!( + "data: {{\"choices\":[{{\"delta\":{{\"role\":\"user\",\"content\":\"a\"}}}}]}}\n\n{DONE}" + ), + ), + ( + "error member", + format!("data: {{\"error\":{{\"message\":\"boom\"}},\"choices\":[]}}\n\n{DONE}"), + ), + ( + "numeric content", + format!("data: {{\"choices\":[{{\"delta\":{{\"content\":5}}}}]}}\n\n{DONE}"), + ), + ( + "missing delta", + format!("data: {{\"choices\":[{{\"index\":0}}]}}\n\n{DONE}"), + ), + ( + "unknown finish reason", + format!("{}{DONE}", finish("content_filter")), + ), + ("empty payload", format!("data:\n\n{DONE}")), + ]; + for (name, input) in cases { + let (result, _) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result, Err(ProviderError::MalformedResponse), "{name}"); + } + } + + #[test] + fn text_delivered_before_a_malformed_payload_stays_delivered() { + let input = format!("{}{}data: not json\n\n{DONE}", delta("one"), delta("two")); + let (result, pieces) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result, Err(ProviderError::MalformedResponse)); + assert_eq!(pieces, ["one", "two"]); + } + + #[test] + fn a_refused_payload_delivers_none_of_its_own_text() { + // Given a payload whose content is fine but whose role is refused. + let input = + "data: {\"choices\":[{\"delta\":{\"role\":\"tool\",\"content\":\"leak\"}}]}\n\n"; + let (result, pieces) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result, Err(ProviderError::MalformedResponse)); + assert!(pieces.is_empty()); + } + + #[test] + fn empty_content_is_never_delivered() { + let input = text_stream(&["", "a", ""]); + let (result, pieces) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result.expect("valid stream").content, "a"); + assert_eq!(pieces, ["a"]); + } + + #[test] + fn a_stream_over_one_mebibyte_is_refused() { + let mut input = String::new(); + while input.len() <= 1024 * 1024 { + input.push_str(": padding padding padding padding padding padding padding\n"); + } + let (result, _) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result, Err(ProviderError::ResponseTooLarge)); + } + + #[test] + fn nothing_after_done_is_read() { + let input = format!("{}garbage that is not an event", text_stream(&["a"])); + let (result, _) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result.expect("valid stream").content, "a"); + } + + /// A reader that fails after its data, like a connection that times out. + struct FailingReader(Cursor>); + + impl Read for FailingReader { + fn read(&mut self, buffer: &mut [u8]) -> std::io::Result { + match self.0.read(buffer)? { + 0 => Err(std::io::Error::from(std::io::ErrorKind::TimedOut)), + count => Ok(count), + } + } + } + + #[test] + fn a_read_error_is_a_transport_failure_and_keeps_delivered_text() { + let mut pieces = Vec::new(); + let reader = FailingReader(Cursor::new(delta("kept").into_bytes())); + let result = read_event_stream(reader, StreamMode::Text, &mut |text: &str| { + pieces.push(text.to_owned()); + }); + assert_eq!(result, Err(ProviderError::Transport)); + assert_eq!(pieces, ["kept"]); + } + + #[test] + fn the_text_mode_refuses_tool_calls_and_tool_finish_reasons() { + let call_delta = "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"a\",\"type\":\"function\",\"function\":{\"name\":\"n\",\"arguments\":\"\"}}]}}]}\n\n"; + let (result, pieces) = run(StreamMode::Text, call_delta.as_bytes()); + assert_eq!(result, Err(ProviderError::UnsupportedToolCall)); + assert!(pieces.is_empty()); + + for reason in ["tool_calls", "function_call"] { + let input = format!("{}{DONE}", finish(reason)); + let (result, _) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result, Err(ProviderError::UnsupportedToolCall), "{reason}"); + } + let legacy = "data: {\"choices\":[{\"delta\":{\"function_call\":{\"name\":\"n\"}}}]}\n\n"; + let (result, _) = run(StreamMode::Text, legacy.as_bytes()); + assert_eq!(result, Err(ProviderError::UnsupportedToolCall)); + + // An empty array is LM Studio's ordinary "no action" form. + let empty = "data: {\"choices\":[{\"delta\":{\"content\":\"ok\",\"tool_calls\":[]},\"finish_reason\":\"stop\"}]}\n\n"; + let (result, _) = run(StreamMode::Text, format!("{empty}{DONE}").as_bytes()); + assert_eq!(result.expect("empty list is no action").content, "ok"); + } + + fn call_payload(entries: &str, finish_reason: Option<&str>) -> String { + let finish_reason = + finish_reason.map_or_else(|| "null".to_owned(), |reason| format!("\"{reason}\"")); + format!( + "data: {{\"choices\":[{{\"index\":0,\"delta\":{{\"tool_calls\":[{entries}]}},\"finish_reason\":{finish_reason}}}]}}\n\n" + ) + } + + fn tool_run(parts: &[String], finish_reason: &str) -> Result { + let mut input = parts.concat(); + input.push_str(&finish(finish_reason)); + input.push_str(DONE); + run(StreamMode::Tools, input.as_bytes()).0 + } + + #[test] + fn documented_tool_call_pieces_assemble_into_one_raw_call() { + // Given the first piece with id, type, name and empty arguments, then fragments. + let parts = vec![ + call_payload( + r#"{"index":0,"id":"call_1","type":"function","function":{"name":"workspace_read_file","arguments":""}}"#, + None, + ), + call_payload(r#"{"index":0,"function":{"arguments":"{\"pa"}}"#, None), + call_payload( + r#"{"index":0,"function":{"arguments":"th\":\"a.txt\"}"}}"#, + None, + ), + ]; + + // When the stream finishes with tool_calls. + let outcome = tool_run(&parts, "tool_calls").expect("valid tool stream"); + + // Then one call holds only the received members and parses. + assert_eq!( + outcome.tool_calls, + vec![serde_json::json!({ + "id": "call_1", + "type": "function", + "function": {"name": "workspace_read_file", "arguments": "{\"path\":\"a.txt\"}"} + })] + ); + let call = outcome.tool_calls.first().expect("one call"); + assert!(crate::turn::ToolCall::parse(call).is_ok()); + assert_eq!(outcome.content, ""); + assert_eq!(outcome.finish_reason, "tool_calls"); + } + + #[test] + fn a_tool_name_sent_in_pieces_is_concatenated_and_a_missing_id_is_absent() { + let parts = vec![ + call_payload(r#"{"index":0,"function":{"name":"workspace_"}}"#, None), + call_payload( + r#"{"index":0,"function":{"name":"read_file","arguments":"{}"}}"#, + None, + ), + ]; + let outcome = tool_run(&parts, "stop").expect("finish stop with calls is accepted"); + assert_eq!( + outcome.tool_calls, + vec![serde_json::json!({ + "function": {"name": "workspace_read_file", "arguments": "{}"} + })] + ); + } + + #[test] + fn null_members_of_later_pieces_are_treated_as_absent() { + // Given a first piece with every member and later pieces that send + // null for the members they do not repeat. + let parts = vec![ + call_payload( + r#"{"index":0,"id":"a","type":"function","function":{"name":"workspace_read_file","arguments":""}}"#, + None, + ), + call_payload( + r#"{"index":0,"id":null,"type":null,"function":{"name":null,"arguments":"{}"}}"#, + None, + ), + call_payload(r#"{"index":0,"function":null}"#, None), + ]; + + // When the stream is read. + let outcome = tool_run(&parts, "tool_calls").expect("null members are absent"); + + // Then the nulls changed nothing. + assert_eq!( + outcome.tool_calls, + vec![serde_json::json!({ + "id": "a", + "type": "function", + "function": {"name": "workspace_read_file", "arguments": "{}"} + })] + ); + } + + #[test] + fn a_null_role_is_treated_as_absent() { + let input = format!( + "data: {{\"choices\":[{{\"delta\":{{\"role\":null,\"content\":\"a\"}}}}]}}\n\n{}{DONE}", + finish("stop") + ); + let (result, pieces) = run(StreamMode::Text, input.as_bytes()); + assert_eq!(result.expect("a null role is absent").content, "a"); + assert_eq!(pieces, ["a"]); + } + + #[test] + fn a_repeated_equal_id_is_accepted_and_a_different_id_is_refused() { + let same = vec![ + call_payload(r#"{"index":0,"id":"a"}"#, None), + call_payload(r#"{"index":0,"id":"a"}"#, None), + ]; + assert!(tool_run(&same, "tool_calls").is_ok()); + + let different = vec![ + call_payload(r#"{"index":0,"id":"a"}"#, None), + call_payload(r#"{"index":0,"id":"b"}"#, None), + ]; + assert_eq!( + tool_run(&different, "tool_calls"), + Err(ProviderError::MalformedResponse) + ); + } + + #[test] + fn malformed_call_pieces_are_refused() { + let cases = [ + ("index gap", r#"{"index":1,"id":"a"}"#), + ("no index", r#"{"id":"a"}"#), + ("string index", r#"{"index":"0","id":"a"}"#), + ("unknown call key", r#"{"index":0,"extra":1}"#), + ("wrong type", r#"{"index":0,"type":"other"}"#), + ( + "unknown function key", + r#"{"index":0,"function":{"other":"x"}}"#, + ), + ("numeric name", r#"{"index":0,"function":{"name":5}}"#), + ("numeric id", r#"{"index":0,"id":5}"#), + ("non-object function", r#"{"index":0,"function":"x"}"#), + ("non-object call", "5"), + ]; + for (name, entry) in cases { + let parts = vec![call_payload(entry, None)]; + assert_eq!( + tool_run(&parts, "tool_calls"), + Err(ProviderError::MalformedResponse), + "{name}" + ); + } + } + + #[test] + fn the_call_limit_is_enforced_at_index_sixty_four() { + let ids: Vec = (0..64) + .map(|index| call_payload(&format!(r#"{{"index":{index},"id":"c{index}"}}"#), None)) + .collect(); + assert_eq!( + tool_run(&ids, "tool_calls") + .expect("sixty-four calls") + .tool_calls + .len(), + 64 + ); + let mut over = ids; + over.push(call_payload(r#"{"index":64,"id":"c64"}"#, None)); + assert_eq!( + tool_run(&over, "tool_calls"), + Err(ProviderError::MalformedResponse) + ); + } + + #[test] + fn tool_finish_reasons_follow_the_non_streaming_rules() { + let call = vec![call_payload(r#"{"index":0,"id":"a"}"#, None)]; + assert_eq!( + tool_run(&call, "length"), + Err(ProviderError::MalformedResponse) + ); + assert_eq!( + tool_run(&call, "function_call"), + Err(ProviderError::MalformedResponse) + ); + assert!(tool_run(&call, "stop").is_ok()); + // `tool_calls` with no call, and `length` text, mirror the buffered parser. + assert_eq!( + tool_run(&[delta("text")], "tool_calls"), + Err(ProviderError::MalformedResponse) + ); + assert!(tool_run(&[delta("text")], "length").is_ok()); + } + + #[test] + fn the_finish_reason_may_share_a_payload_with_the_last_call_piece() { + let mut input = call_payload( + r#"{"index":0,"id":"a","type":"function","function":{"name":"workspace_list_directory","arguments":"{}"}}"#, + Some("tool_calls"), + ); + input.push_str(DONE); + let (result, _) = run(StreamMode::Tools, input.as_bytes()); + assert_eq!(result.expect("valid").tool_calls.len(), 1); + } + + #[test] + fn tool_mode_text_alongside_calls_is_kept() { + let parts = vec![ + delta("Looking."), + call_payload(r#"{"index":0,"id":"a"}"#, None), + ]; + let outcome = tool_run(&parts, "tool_calls").expect("valid"); + assert_eq!(outcome.content, "Looking."); + } + + /// Feed the parser the given chunks and collect the result and the pieces. + fn run_chunks<'c>( + mode: StreamMode, + chunks: impl IntoIterator, + ) -> (Result, Vec) { + let mut parser = StreamParser::new(mode); + let mut pieces = Vec::new(); + let mut sink = |text: &str| pieces.push(text.to_owned()); + for chunk in chunks { + match parser.push(chunk, &mut sink) { + Ok(Flow::Continue) => {} + Ok(Flow::Done) => return (parser.finish(), pieces), + Err(error) => return (Err(error), pieces), + } + } + (parser.finish(), pieces) + } + + /// Streams that end in success, in failure, and before `[DONE]`. + fn chunking_cases() -> Vec<(&'static str, StreamMode, Vec)> { + let first = delta("a").trim_end().replace('\n', "\r\n"); + let crlf = format!( + ": keep-alive\r\nevent: message\r\n{first}\r\n\r\ndata:{}\r\ndata: [DONE]\r\n", + serde_json::json!({"choices":[{"index":0,"delta":{"content":"b"},"finish_reason":"stop"}]}) + ); + let with_call = [ + delta("Looking."), + call_payload( + r#"{"index":0,"id":"a","type":"function","function":{"name":"workspace_list_directory","arguments":"{\"path\":"}}"#, + None, + ), + call_payload(r#"{"index":0,"function":{"arguments":"\"\"}"}}"#, None), + finish("tool_calls"), + DONE.to_owned(), + ] + .concat(); + let mut over_limit = delta("start"); + while over_limit.len() <= 1024 * 1024 { + over_limit.push_str(": padding padding padding padding padding\n"); + } + vec![ + ( + "valid text", + StreamMode::Text, + text_stream(&["Hel", "lo ", "there"]).into_bytes(), + ), + ("valid crlf", StreamMode::Text, crlf.into_bytes()), + ("valid tool call", StreamMode::Tools, with_call.into_bytes()), + ( + "unknown field", + StreamMode::Text, + format!("{}bogus: 1\n\n{DONE}", delta("a")).into_bytes(), + ), + ( + "malformed payload after text", + StreamMode::Text, + format!("{}data: {{broken\n\n{DONE}", delta("a")).into_bytes(), + ), + ( + "tool call in a text stream", + StreamMode::Text, + call_payload(r#"{"index":0,"id":"a"}"#, None).into_bytes(), + ), + ( + "ends before done", + StreamMode::Text, + format!("{}{}", delta("a"), finish("stop")).into_bytes(), + ), + ( + "ends inside the final line", + StreamMode::Text, + format!("{}{}data: [DONE]", delta("a"), finish("stop")).into_bytes(), + ), + ( + "text after done is not read", + StreamMode::Text, + format!( + "{}garbage with no newline\ndata: {{broken\n\n", + text_stream(&["a"]) + ) + .into_bytes(), + ), + ( + "over the byte limit", + StreamMode::Text, + over_limit.into_bytes(), + ), + ] + } + + #[test] + fn one_byte_at_a_time_gives_the_same_result_and_pieces_as_one_push() { + for (name, mode, input) in chunking_cases() { + // Given one input, delivered whole and then one byte per push. + let whole = run_chunks(mode, [input.as_slice()]); + + // When the parser is fed single bytes. + let bytewise = run_chunks(mode, input.chunks(1)); + + // Then the result and the delivered pieces are identical. + assert_eq!(bytewise, whole, "{name}"); + } + } + + #[test] + fn a_split_at_every_position_gives_the_same_result_and_pieces() { + // Given a stream with CRLF framing, a comment, usage, and two pieces. + let input = format!( + ": ping\r\n\r\nevent: message\r\ndata:{}\r\n\r\ndata: {}\r\n\r\ndata: {}\r\n\r\ndata: [DONE]\r\n\r\n", + serde_json::json!({"choices":[{"index":0,"delta":{"role":"assistant","content":"h\u{e9}llo"}}]}), + serde_json::json!({"choices":[{"index":0,"delta":{"content":" there"},"finish_reason":"stop"}]}), + serde_json::json!({"choices":[],"usage":{"prompt_tokens":5,"completion_tokens":2,"total_tokens":7}}), + ) + .into_bytes(); + let whole = run_chunks(StreamMode::Text, [input.as_slice()]); + assert_eq!( + whole.0.as_ref().map(|outcome| outcome.content.as_str()), + Ok("h\u{e9}llo there") + ); + + // When it is split into two pushes at every byte position, including + // inside a multi-byte character and between a CR and its LF. + for at in 0..=input.len() { + let (head, tail) = input.split_at(at); + let split = run_chunks(StreamMode::Text, [head, tail]); + + // Then every split matches the single push. + assert_eq!(split, whole, "split at {at}"); + } + } + + #[test] + fn nothing_is_counted_or_read_after_done_in_any_chunking() { + // Given a complete stream followed by more than the byte limit of text. + let mut input = text_stream(&["a"]).into_bytes(); + let padding = vec![b'x'; 2 * 1024 * 1024]; + input.extend_from_slice(&padding); + + // When it is pushed whole and in large pieces. + let whole = run_chunks(StreamMode::Text, [input.as_slice()]); + let pieces = run_chunks(StreamMode::Text, input.chunks(4096)); + + // Then the reply is accepted and the trailing bytes never matter. + assert_eq!(whole.0.expect("valid stream").content, "a"); + assert_eq!(pieces.0.expect("valid stream").content, "a"); + } +} diff --git a/crates/omgcode-core/src/session.rs b/crates/omgcode-core/src/session.rs index c1249c6..0800412 100644 --- a/crates/omgcode-core/src/session.rs +++ b/crates/omgcode-core/src/session.rs @@ -18,6 +18,20 @@ use uuid::Uuid; use crate::storage::{self, StorageError}; +mod discovery; +mod filename; +mod inspection; + +pub use discovery::{DiscoveredJournal, Discovery, discover, locate}; +pub use inspection::{SessionInspection, TranscriptEntry, TranscriptEntryKind}; + +/// Return the session UUID of a file name that matches the writer's exact +/// journal name format, or `None`. The session index uses this to revalidate a +/// stored path before handing it to a writer open. +pub(crate) fn strict_journal_session_id(name: &str) -> Option { + filename::parse_strict(name) +} + const SCHEMA_VERSION_V1: u32 = 1; const SCHEMA_VERSION_V2: u32 = 2; const SCHEMA_VERSION: u32 = 3; @@ -308,14 +322,7 @@ impl SessionJournal { .join(format!("{:02}", u8::from(now.month()))) .join(format!("{:02}", now.day())); storage::create_private_directories(&dir).map_err(map_storage_error)?; - let filename = format!( - "omgcode-{:02}-{:02}-{:02}-{:03}Z-{session_id}.jsonl", - now.hour(), - now.minute(), - now.second(), - now.millisecond() - ); - let path = dir.join(filename); + let path = dir.join(filename::journal_file_name(now, session_id)); let mut file = storage::open_locked_file(&path, true).map_err(map_storage_error)?; let started = SessionStarted { @@ -375,16 +382,7 @@ impl SessionJournal { .file_name() .and_then(|name| name.to_str()) .ok_or(SessionError::InvalidJournal("filename is not valid UTF-8"))?; - let id_text = filename - .strip_prefix("omgcode-") - .and_then(|name| name.strip_suffix(".jsonl")) - .and_then(|name| name.rsplit_once("Z-")) - .map(|(_, id)| id) - .ok_or(SessionError::InvalidJournal( - "filename does not contain a session UUID", - ))?; - let filename_id = Uuid::parse_str(id_text) - .map_err(|_| SessionError::InvalidJournal("filename has an invalid session UUID"))?; + let filename_id = filename::lenient_session_id(filename)?; let metadata = fs::symlink_metadata(path)?; if metadata.file_type().is_symlink() || !metadata.is_file() { return Err(SessionError::InvalidJournal( @@ -393,7 +391,12 @@ impl SessionJournal { } let mut file = storage::open_locked_file(path, false).map_err(map_storage_error)?; let contents = read_bounded(&mut file)?; - let (snapshot, tool_state, tool_requests) = replay(&contents, filename_id)?; + let Replayed { + snapshot, + tool_state, + tool_requests, + .. + } = replay(&contents, filename_id, None)?; let bytes = u64::try_from(contents.len()).map_err(|_| SessionError::LimitReached)?; let next_seq = snapshot.event_count; let schema_version = snapshot.schema_version; @@ -987,11 +990,15 @@ struct MessagePayload { call_id: Option, } +/// The recorded origin label of a message. A label never grants authority. #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(rename_all = "snake_case")] -enum MessageSource { +pub enum MessageSource { + /// Text entered through the trusted CLI. UserCli, + /// Text returned by the model provider. Provider, + /// Text rendered by core from a tool result. Schema version 2 and later. CoreTool, } @@ -1354,11 +1361,43 @@ fn read_bounded(file: &mut File) -> Result, SessionError> { Ok(contents) } +/// The validated result of replaying a complete journal. +struct Replayed { + snapshot: SessionSnapshot, + tool_state: ToolState, + tool_requests: Vec, + /// Timestamp of the `session.started` event. + created_at: OffsetDateTime, + /// Timestamp of the last replayed event. + updated_at: OffsetDateTime, +} + +/// Queue one transcript entry when a caller asked for entries. The kind is +/// built lazily so a writer-side replay clones nothing. +fn note_entry( + entries: &mut Option<&mut Vec>, + seq: u64, + timestamp: OffsetDateTime, + kind: impl FnOnce() -> TranscriptEntryKind, +) { + if let Some(sink) = entries.as_mut() { + sink.push(TranscriptEntry { + seq, + timestamp, + kind: kind(), + }); + } +} + /// Validate every event and transition without interpreting content as work. +/// +/// `entries` receives one record per event after `session.started` when the +/// caller is an observer. Both the writer and the observer share these rules. fn replay( contents: &[u8], filename_id: Uuid, -) -> Result<(SessionSnapshot, ToolState, Vec), SessionError> { + mut entries: Option<&mut Vec>, +) -> Result { if contents.is_empty() || contents[0..contents.len().min(3)] == [0xef, 0xbb, 0xbf] { return Err(SessionError::InvalidJournal( "journal is empty or has a byte-order mark", @@ -1378,6 +1417,8 @@ fn replay( let mut expected_seq = 0_u64; let mut total_events = 0_usize; let mut consumed_bytes = 0_usize; + let mut created_at = None; + let mut updated_at = None; for raw_line in contents.split_inclusive(|byte| *byte == b'\n') { if raw_line.len() > MAX_EVENT_BYTES { return Err(SessionError::InvalidJournal("event exceeds 64 KiB")); @@ -1442,7 +1483,8 @@ fn replay( if !event_ids.insert(envelope.event_id) { return Err(SessionError::InvalidJournal("duplicate event UUID")); } - validate_timestamp(&envelope.timestamp)?; + let at = validate_timestamp(&envelope.timestamp)?; + let seq = envelope.seq; match envelope.event_type.as_str() { "session.started" => { if snapshot.is_some() || expected_seq != 0 { @@ -1453,6 +1495,7 @@ fn replay( let started: SessionStarted = decode_payload(envelope.payload)?; validate_title(&started.title) .map_err(|_| SessionError::InvalidJournal("initial title is invalid"))?; + created_at = Some(at); snapshot = Some(SessionSnapshot { schema_version: envelope.schema_version, session_id: envelope.session_id, @@ -1537,6 +1580,12 @@ fn replay( )); } } + note_entry(&mut entries, seq, at, || TranscriptEntryKind::Message { + role: message.role, + source: message.source, + text: message.text.clone(), + call_id: message.call_id.clone(), + }); target.messages.push(RecordedMessage { role: message.role, text: message.text, @@ -1552,6 +1601,9 @@ fn replay( } let payload: TurnPayload = decode_payload(envelope.payload)?; tool_state.clear(); + note_entry(&mut entries, seq, at, || TranscriptEntryKind::TurnStarted { + request_id: payload.request_id, + }); target.active_turn = Some(payload.request_id); } "tool.requested" => { @@ -1590,6 +1642,14 @@ fn replay( )); } tool_state.pending.insert(payload.call_id.clone()); + note_entry(&mut entries, seq, at, || { + TranscriptEntryKind::ToolRequested { + request_id: payload.request_id, + call_id: payload.call_id.clone(), + tool: payload.tool.clone(), + arguments: payload.arguments.clone(), + } + }); tool_requests.push(RecordedToolRequest { request_id: payload.request_id, call_id: payload.call_id, @@ -1632,6 +1692,16 @@ fn replay( )?; tool_state.pending.remove(&payload.call_id); tool_state.completed.insert(payload.call_id.clone()); + note_entry(&mut entries, seq, at, || { + TranscriptEntryKind::ToolCompleted { + request_id: payload.request_id, + call_id: payload.call_id.clone(), + outcome: payload.outcome, + code: payload.code.clone(), + output_bytes: payload.output_bytes, + output_sha256: payload.output_sha256.clone(), + } + }); tool_state.outputs.insert( payload.call_id, (payload.output_bytes, payload.output_sha256), @@ -1653,6 +1723,14 @@ fn replay( validate_manifest(&payload.manifest, envelope.schema_version).map_err(|_| { SessionError::InvalidJournal("request manifest exceeds event size limit") })?; + note_entry(&mut entries, seq, at, || { + TranscriptEntryKind::RequestManifest { + request_id: payload.request_id, + model: payload.manifest.model.clone(), + prompt_version: payload.manifest.prompt_version.clone(), + tools: payload.manifest.tools.clone().unwrap_or_default(), + } + }); } "turn.completed" | "turn.failed" => { let target = snapshot @@ -1687,6 +1765,18 @@ fn replay( "completed turn has unresolved or unrecorded tool calls", )); } + note_entry(&mut entries, seq, at, || { + if envelope.event_type == "turn.failed" { + TranscriptEntryKind::TurnFailed { + request_id: payload.request_id, + code: payload.code.clone().unwrap_or_default(), + } + } else { + TranscriptEntryKind::TurnCompleted { + request_id: payload.request_id, + } + } + }); target.active_turn = None; tool_state.clear(); } @@ -1698,6 +1788,9 @@ fn replay( if target.active_turn.is_some() { return Err(SessionError::InvalidJournal("active session was archived")); } + note_entry(&mut entries, seq, at, || { + TranscriptEntryKind::SessionArchived + }); target.archived = true; tool_state.clear(); } @@ -1706,6 +1799,9 @@ fn replay( .as_mut() .ok_or(SessionError::InvalidJournal("event before start"))?; let _: EmptyPayload = decode_payload(envelope.payload)?; + note_entry(&mut entries, seq, at, || { + TranscriptEntryKind::SessionRestored + }); target.archived = false; } "context.compacted" => { @@ -1730,6 +1826,11 @@ fn replay( .map(|record| record.compacted_messages); validate_compaction(&record, &target.messages, prior) .map_err(|_| SessionError::InvalidJournal("invalid compaction record"))?; + note_entry(&mut entries, seq, at, || { + TranscriptEntryKind::ContextCompacted { + record: record.clone(), + } + }); target.compaction = Some(record); } _ => return Err(SessionError::InvalidJournal("unknown event type")), @@ -1745,6 +1846,7 @@ fn replay( expected_seq = expected_seq .checked_add(1) .ok_or(SessionError::LimitReached)?; + updated_at = Some(at); if let Some(target) = snapshot.as_mut() { target.event_count = u64::try_from(total_events).map_err(|_| SessionError::LimitReached)?; @@ -1753,7 +1855,18 @@ fn replay( let snapshot = snapshot.ok_or(SessionError::InvalidJournal( "missing session.started event", ))?; - Ok((snapshot, tool_state, tool_requests)) + let (Some(created_at), Some(updated_at)) = (created_at, updated_at) else { + return Err(SessionError::InvalidJournal( + "missing session.started event", + )); + }; + Ok(Replayed { + snapshot, + tool_state, + tool_requests, + created_at, + updated_at, + }) } /// Reject transcript changes while replay says the session is archived. @@ -1768,13 +1881,13 @@ fn ensure_not_archived(snapshot: &SessionSnapshot) -> Result<(), SessionError> { } /// Require parseable UTC metadata without using clocks to reorder events. -fn validate_timestamp(value: &str) -> Result<(), SessionError> { +fn validate_timestamp(value: &str) -> Result { let timestamp = OffsetDateTime::parse(value, &Rfc3339) .map_err(|_| SessionError::InvalidJournal("timestamp is not RFC 3339"))?; if timestamp.offset().whole_seconds() != 0 { return Err(SessionError::InvalidJournal("timestamp is not UTC")); } - Ok(()) + Ok(timestamp) } /// Apply the selected event's typed schema and reject unsupported fields. diff --git a/crates/omgcode-core/src/session/discovery.rs b/crates/omgcode-core/src/session/discovery.rs new file mode 100644 index 0000000..0a5ed6f --- /dev/null +++ b/crates/omgcode-core/src/session/discovery.rs @@ -0,0 +1,313 @@ +//! Bounded discovery of session journals below a trusted data root. +//! +//! Discovery walks only `/sessions///
/` and lists +//! files whose names match the writer's exact pattern. It never follows a +//! symbolic link or Windows reparse point, never opens a journal, and never +//! reads journal content. A listing is a locator, not validation: inspect or +//! open a journal to learn whether it is valid. + +use std::fs::{self, DirEntry, FileType}; +use std::io; +use std::path::{Path, PathBuf}; +use std::time::SystemTime; + +use uuid::Uuid; + +use super::{SessionError, filename}; + +/// The most journals one discovery may list. +const MAX_DISCOVERED_JOURNALS: usize = 50_000; +/// The most directory entries one discovery may examine, at every level. +const MAX_DIRECTORY_ENTRIES: usize = 500_000; +/// Directory levels below `sessions`: year, month, day. +const DATE_LEVELS: usize = 3; +/// Required ASCII digit counts for the year, month, and day directory names. +const DATE_WIDTHS: [usize; DATE_LEVELS] = [4, 2, 2]; +const SESSIONS_DIRECTORY: &str = "sessions"; + +/// One journal file found by discovery. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct DiscoveredJournal { + /// The session UUID taken from the file name. Replay must still confirm it. + pub session_id: Uuid, + /// The journal path: the absolute form of the data root joined with + /// `relative_path`. Symbolic links are not resolved. + pub path: PathBuf, + /// The path below the data root, such as + /// `sessions/2026/11/16/omgcode-...jsonl`. It is a stable key. + pub relative_path: PathBuf, + /// The file size in bytes when discovery listed it. + pub size: u64, + /// The file's last-modified time as reported by the operating system. + pub modified: SystemTime, +} + +/// The result of one discovery walk. +#[derive(Clone, Debug, Default, Eq, PartialEq)] +pub struct Discovery { + /// Journals sorted by `relative_path`. + pub journals: Vec, + /// Symbolic links and reparse points that were skipped at any level. + pub skipped_links: u64, + /// Entries that do not match the layout, including non-UTF-8 names, + /// wrongly named files and directories, and entries that vanished mid-walk. + pub unrecognized: u64, +} + +/// List journals below `data_root` in a deterministic order. +/// +/// `data_root` must come from trusted application settings. A missing +/// `sessions` directory yields an empty result. A `sessions` directory that is +/// itself a link is skipped and counted. Concurrent changes can make a listing +/// stale; it is not a snapshot. +/// +/// # Errors +/// +/// Returns `SessionError::LimitReached` after more than 50,000 journals or +/// 500,000 directory entries, and `SessionError::Io` when a directory cannot be +/// read. +pub fn discover(data_root: &Path) -> Result { + discover_with_limits(data_root, MAX_DISCOVERED_JOURNALS, MAX_DIRECTORY_ENTRIES) +} + +/// Run `discover` with explicit caps so tests can reach them cheaply. +fn discover_with_limits( + data_root: &Path, + max_journals: usize, + max_entries: usize, +) -> Result { + let absolute_root = std::path::absolute(data_root)?; + let mut walk = Walk { + absolute_root, + max_journals, + max_entries, + examined: 0, + discovery: Discovery::default(), + }; + let sessions = data_root.join(SESSIONS_DIRECTORY); + let metadata = match fs::symlink_metadata(&sessions) { + Ok(metadata) => metadata, + Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(walk.discovery), + Err(error) => return Err(error.into()), + }; + if metadata.file_type().is_symlink() || is_reparse_point(&metadata) { + walk.discovery.skipped_links += 1; + return Ok(walk.discovery); + } + if !metadata.is_dir() { + walk.discovery.unrecognized += 1; + return Ok(walk.discovery); + } + walk.directory(&sessions, Path::new(SESSIONS_DIRECTORY), 0)?; + let mut discovery = walk.discovery; + discovery + .journals + .sort_by(|left, right| left.relative_path.cmp(&right.relative_path)); + Ok(discovery) +} + +/// Find the one journal path for `session_id` below `data_root`. +/// +/// A UI addresses sessions by UUID and never supplies a path. This runs a full +/// `discover` walk and compares file-name UUIDs. It returns `None` when no +/// journal carries the UUID. +/// +/// # Errors +/// +/// Returns `SessionError::InvalidJournal` when two files carry the same UUID, +/// because choosing one would be a guess. It also returns every error of +/// `discover`. +pub fn locate(data_root: &Path, session_id: Uuid) -> Result, SessionError> { + let mut matches = discover(data_root)? + .journals + .into_iter() + .filter(|journal| journal.session_id == session_id); + let first = matches.next(); + if matches.next().is_some() { + return Err(SessionError::InvalidJournal( + "more than one journal has this session UUID", + )); + } + Ok(first.map(|journal| journal.path)) +} + +/// Mutable walk state with the shared entry budget. +struct Walk { + absolute_root: PathBuf, + max_journals: usize, + max_entries: usize, + examined: usize, + discovery: Discovery, +} + +impl Walk { + /// Visit one directory. `level` 0 holds years, 1 months, 2 days, 3 files. + fn directory( + &mut self, + directory: &Path, + relative: &Path, + level: usize, + ) -> Result<(), SessionError> { + let entries = match fs::read_dir(directory) { + Ok(entries) => entries, + Err(error) if level > 0 && error.kind() == io::ErrorKind::NotFound => { + self.discovery.unrecognized += 1; + return Ok(()); + } + Err(error) => return Err(error.into()), + }; + for entry in entries { + let entry = entry?; + self.examined += 1; + if self.examined > self.max_entries { + return Err(SessionError::LimitReached); + } + self.entry(&entry, relative, level)?; + } + Ok(()) + } + + /// Classify one entry without following it. + fn entry( + &mut self, + entry: &DirEntry, + relative: &Path, + level: usize, + ) -> Result<(), SessionError> { + let file_type = match entry.file_type() { + Ok(file_type) => file_type, + Err(error) if error.kind() == io::ErrorKind::NotFound => { + self.discovery.unrecognized += 1; + return Ok(()); + } + Err(error) => return Err(error.into()), + }; + if file_type.is_symlink() || entry_is_reparse_point(entry, &file_type)? { + self.discovery.skipped_links += 1; + return Ok(()); + } + let file_name = entry.file_name(); + let Some(name) = file_name.to_str() else { + self.discovery.unrecognized += 1; + return Ok(()); + }; + if let Some(width) = DATE_WIDTHS.get(level) { + if file_type.is_dir() && has_ascii_digits(name, *width) { + return self.directory(&entry.path(), &relative.join(name), level + 1); + } + } else if file_type.is_file() + && let Some(session_id) = filename::parse_strict(name) + { + return self.journal(entry, relative, name, session_id); + } + self.discovery.unrecognized += 1; + Ok(()) + } + + /// Record one matching file, or count it when it vanished. + fn journal( + &mut self, + entry: &DirEntry, + relative: &Path, + name: &str, + session_id: Uuid, + ) -> Result<(), SessionError> { + let metadata = match entry.metadata() { + Ok(metadata) => metadata, + Err(error) if error.kind() == io::ErrorKind::NotFound => { + self.discovery.unrecognized += 1; + return Ok(()); + } + Err(error) => return Err(error.into()), + }; + if self.discovery.journals.len() >= self.max_journals { + return Err(SessionError::LimitReached); + } + let relative_path = relative.join(name); + self.discovery.journals.push(DiscoveredJournal { + session_id, + path: self.absolute_root.join(&relative_path), + relative_path, + size: metadata.len(), + modified: metadata.modified()?, + }); + Ok(()) + } +} + +/// Return whether `name` is exactly `width` ASCII digits. +fn has_ascii_digits(name: &str, width: usize) -> bool { + name.len() == width && name.bytes().all(|byte| byte.is_ascii_digit()) +} + +/// Detect a Windows reparse point that `FileType` did not report as a link. +#[cfg(windows)] +fn entry_is_reparse_point(entry: &DirEntry, _file_type: &FileType) -> io::Result { + match entry.metadata() { + Ok(metadata) => Ok(is_reparse_point(&metadata)), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false), + Err(error) => Err(error), + } +} + +/// Other platforms report links through `FileType` alone. +#[cfg(not(windows))] +fn entry_is_reparse_point(_entry: &DirEntry, _file_type: &FileType) -> io::Result { + Ok(false) +} + +/// Return whether metadata carries the Windows reparse-point attribute. +#[cfg(windows)] +fn is_reparse_point(metadata: &fs::Metadata) -> bool { + use std::os::windows::fs::MetadataExt; + const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0400; + metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 +} + +/// Other platforms have no reparse points. +#[cfg(not(windows))] +fn is_reparse_point(_metadata: &fs::Metadata) -> bool { + false +} + +#[cfg(test)] +mod tests { + use super::discover_with_limits; + use crate::session::{SessionError, filename}; + use std::fs; + use time::OffsetDateTime; + use uuid::Uuid; + + fn day_with_journals(count: usize) -> tempfile::TempDir { + let temporary = tempfile::tempdir().expect("temporary directory"); + let day = temporary.path().join("sessions/2026/01/02"); + fs::create_dir_all(&day).expect("day directory"); + for _ in 0..count { + let name = filename::journal_file_name(OffsetDateTime::now_utc(), Uuid::new_v4()); + fs::write(day.join(name), b"").expect("journal placeholder"); + } + temporary + } + + #[test] + fn journal_cap_returns_limit_reached_only_when_exceeded() { + let temporary = day_with_journals(3); + let at_cap = discover_with_limits(temporary.path(), 3, 1_000).expect("at the cap"); + assert_eq!(at_cap.journals.len(), 3); + assert!(matches!( + discover_with_limits(temporary.path(), 2, 1_000), + Err(SessionError::LimitReached) + )); + } + + #[test] + fn entry_cap_counts_directories_and_files_together() { + // Entries examined: year, month, day, then three files. + let temporary = day_with_journals(3); + discover_with_limits(temporary.path(), 100, 6).expect("exactly at the cap"); + assert!(matches!( + discover_with_limits(temporary.path(), 100, 5), + Err(SessionError::LimitReached) + )); + } +} diff --git a/crates/omgcode-core/src/session/filename.rs b/crates/omgcode-core/src/session/filename.rs new file mode 100644 index 0000000..3945788 --- /dev/null +++ b/crates/omgcode-core/src/session/filename.rs @@ -0,0 +1,122 @@ +//! Journal file naming shared by the writer, `SessionJournal::open`, the +//! read-only inspector, and discovery. +//! +//! A name is `omgcode----Z-.jsonl`. The name is only a +//! locator. Replay still requires every event to carry the same session UUID. + +use time::OffsetDateTime; +use uuid::Uuid; + +use super::SessionError; + +const NAME_PREFIX: &str = "omgcode-"; +const NAME_SUFFIX: &str = ".jsonl"; +const UUID_TEXT_BYTES: usize = 36; + +/// Format the file name the writer creates for a new session at `at`. +pub(super) fn journal_file_name(at: OffsetDateTime, session_id: Uuid) -> String { + format!( + "{NAME_PREFIX}{:02}-{:02}-{:02}-{:03}Z-{session_id}{NAME_SUFFIX}", + at.hour(), + at.minute(), + at.second(), + at.millisecond() + ) +} + +/// Extract the session UUID the way `SessionJournal::open` always has. +/// +/// This accepts any UUID spelling that `Uuid::parse_str` accepts and does not +/// check the time fields. Replay then rejects any event whose UUID differs. +pub(super) fn lenient_session_id(filename: &str) -> Result { + let id_text = filename + .strip_prefix(NAME_PREFIX) + .and_then(|name| name.strip_suffix(NAME_SUFFIX)) + .and_then(|name| name.rsplit_once("Z-")) + .map(|(_, id)| id) + .ok_or(SessionError::InvalidJournal( + "filename does not contain a session UUID", + ))?; + Uuid::parse_str(id_text) + .map_err(|_| SessionError::InvalidJournal("filename has an invalid session UUID")) +} + +/// Parse exactly the name the writer produces, or return `None`. +/// +/// The time fields must be fixed-width ASCII digits within range, and the UUID +/// must be a lowercase hyphenated version 4 value. +pub(super) fn parse_strict(name: &str) -> Option { + let body = name.strip_prefix(NAME_PREFIX)?.strip_suffix(NAME_SUFFIX)?; + let (stamp, id_text) = body.split_once("Z-")?; + let mut parts = stamp.split('-'); + let hour = fixed_digits(parts.next()?, 2)?; + let minute = fixed_digits(parts.next()?, 2)?; + let second = fixed_digits(parts.next()?, 2)?; + let millisecond = fixed_digits(parts.next()?, 3)?; + if parts.next().is_some() || hour > 23 || minute > 59 || second > 59 || millisecond > 999 { + return None; + } + if id_text.len() != UUID_TEXT_BYTES { + return None; + } + let id = Uuid::try_parse(id_text).ok()?; + if id.get_version_num() != 4 || id.as_hyphenated().to_string() != id_text { + return None; + } + Some(id) +} + +/// Parse a field of exactly `width` ASCII digits. +fn fixed_digits(text: &str, width: usize) -> Option { + if text.len() != width || !text.bytes().all(|byte| byte.is_ascii_digit()) { + return None; + } + text.parse().ok() +} + +#[cfg(test)] +mod tests { + use super::{journal_file_name, lenient_session_id, parse_strict}; + use time::OffsetDateTime; + use uuid::Uuid; + + #[test] + fn writer_names_round_trip_through_the_strict_parser() { + let id = Uuid::new_v4(); + let name = journal_file_name(OffsetDateTime::now_utc(), id); + assert_eq!(parse_strict(&name), Some(id)); + assert_eq!(lenient_session_id(&name).expect("lenient"), id); + } + + #[test] + fn strict_parser_rejects_every_deviation_from_the_writer_format() { + let id = Uuid::new_v4(); + let good = format!("omgcode-01-02-03-004Z-{id}.jsonl"); + assert_eq!(parse_strict(&good), Some(id)); + let upper = format!( + "omgcode-01-02-03-004Z-{}.jsonl", + id.to_string().to_uppercase() + ); + let simple = format!("omgcode-01-02-03-004Z-{}.jsonl", id.simple()); + let nil = "omgcode-01-02-03-004Z-00000000-0000-0000-0000-000000000000.jsonl"; + let bad = [ + upper, + simple, + nil.to_owned(), + format!("omgcode-24-02-03-004Z-{id}.jsonl"), + format!("omgcode-01-60-03-004Z-{id}.jsonl"), + format!("omgcode-01-02-60-004Z-{id}.jsonl"), + format!("omgcode-1-02-03-004Z-{id}.jsonl"), + format!("omgcode-01-02-03-4Z-{id}.jsonl"), + format!("omgcode-01-02-03-004-{id}.jsonl"), + format!("omgcode-01-02-03-004-05Z-{id}.jsonl"), + format!("omgcode-+1-02-03-004Z-{id}.jsonl"), + format!("omgcode-01-02-03-004Z-{id}.json"), + format!("omgcode-01-02-03-004Z-{id}.jsonl.bak"), + format!("other-01-02-03-004Z-{id}.jsonl"), + ]; + for name in bad { + assert_eq!(parse_strict(&name), None, "{name}"); + } + } +} diff --git a/crates/omgcode-core/src/session/inspection.rs b/crates/omgcode-core/src/session/inspection.rs new file mode 100644 index 0000000..f716a44 --- /dev/null +++ b/crates/omgcode-core/src/session/inspection.rs @@ -0,0 +1,244 @@ +//! Read-only inspection of one session journal without the writer lock. +//! +//! An inspector never calls `try_lock`, `lock`, or `lock_shared`, so scanning +//! cannot make a writer in another process report `Busy`. It validates the +//! journal with the same replay rules as `SessionJournal::open`, never +//! modifies the file, and never executes or resumes recorded work. Everything +//! it returns is untrusted historical data. + +use std::fs; +use std::path::Path; + +use serde::Serialize; +use time::OffsetDateTime; +use uuid::Uuid; + +use super::{ + CompactionRecord, MessageRole, MessageSource, NativePath, Replayed, SessionError, ToolOutcome, + filename, map_storage_error, read_bounded, replay, +}; +use crate::storage; + +/// A validated, read-only view of one journal. +/// +/// Fields mirror the header facts of `SessionSnapshot` and add event times and +/// the ordered transcript. Nothing here grants authority or starts work. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SessionInspection { + /// The UUID shared by all events in this journal. + pub session_id: Uuid, + /// Workspace association recorded at creation. Descriptive only. + pub workspace_id: Uuid, + /// Canonical workspace path recorded at creation. Descriptive only. + pub workspace: NativePath, + /// Session title recorded at creation. + pub title: String, + /// Whether the session is archived after the last complete event. + pub archived: bool, + /// The turn left active by the last complete event, if any. Inspecting + /// never resumes it, and a live writer may already have finished it. + pub active_turn: Option, + /// The journal schema version that every event shares. + pub schema_version: u32, + /// Number of complete events, including `session.started`. + pub event_count: u64, + /// Timestamp of the `session.started` event. + #[serde(with = "time::serde::rfc3339")] + pub created_at: OffsetDateTime, + /// Timestamp of the last complete event. + #[serde(with = "time::serde::rfc3339")] + pub updated_at: OffsetDateTime, + /// True when bytes followed the last LF. A writer may be mid-append, so the + /// partial record was ignored. The file was not changed. + pub incomplete_tail: bool, + /// Every event after `session.started`, in journal order. + pub entries: Vec, +} + +/// One validated event after `session.started`. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct TranscriptEntry { + /// Zero-based journal sequence number. Sequence, not time, orders events. + pub seq: u64, + /// The recorded UTC event time. + #[serde(with = "time::serde::rfc3339")] + pub timestamp: OffsetDateTime, + /// The event type and its recorded data. + #[serde(flatten)] + pub kind: TranscriptEntryKind, +} + +/// The recorded data of one event. All text is untrusted. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[serde(tag = "kind", rename_all = "snake_case")] +pub enum TranscriptEntryKind { + /// A user, assistant, or tool message. + Message { + /// The recorded display role. + role: MessageRole, + /// The recorded origin label. + source: MessageSource, + /// The message text, at most 16 KiB. + text: String, + /// Tool call identifier. Present only on tool messages. + #[serde(skip_serializing_if = "Option::is_none")] + call_id: Option, + }, + /// A turn began. + TurnStarted { + /// The turn identifier. + request_id: Uuid, + }, + /// A turn completed. + TurnCompleted { + /// The turn identifier. + request_id: Uuid, + }, + /// A turn failed. Replay requires a code on every failed turn. + TurnFailed { + /// The turn identifier. + request_id: Uuid, + /// The bounded, non-secret failure code. + code: String, + }, + /// The model asked for a tool call. Schema version 2 and later. + ToolRequested { + /// The turn identifier. + request_id: Uuid, + /// Provider call identifier, unique within its turn. + call_id: String, + /// Requested tool name. + tool: String, + /// Exact strict-JSON arguments string, at most 4 KiB. + arguments: String, + }, + /// Core recorded the outcome of a tool call. Schema version 2 and later. + ToolCompleted { + /// The turn identifier. + request_id: Uuid, + /// Provider call identifier. + call_id: String, + /// The core-observed outcome. + outcome: ToolOutcome, + /// Stable error code. Present only when the call failed or was denied. + #[serde(skip_serializing_if = "Option::is_none")] + code: Option, + /// Size in bytes of the rendered output. + output_bytes: u64, + /// Lowercase SHA-256 of the rendered output. + output_sha256: String, + }, + /// The request manifest, reduced to its small descriptive fields. The base + /// prompt and custom instructions are deliberately not included. + RequestManifest { + /// The turn identifier. + request_id: Uuid, + /// The model name used for the request. + model: String, + /// The version label of the compiled base prompt. + prompt_version: String, + /// Tool names advertised to the model. Empty before schema version 2. + tools: Vec, + }, + /// A compaction summary replaced earlier messages. Schema version 3 only. + ContextCompacted { + /// The recorded compaction. Its summary is untrusted model output. + record: CompactionRecord, + }, + /// The session was archived. + SessionArchived, + /// The session was restored from the archive. + SessionRestored, +} + +impl SessionInspection { + /// Inspect the journal at `path` without taking the writer lock. + /// + /// The file is opened read-only without following links, checked with the + /// same platform handle validation as the writer, and read within the + /// journal size bound. Bytes after the last LF are ignored and reported + /// through `incomplete_tail`, because a writer may be mid-append. A journal + /// with no complete record is invalid. The file is never changed. + /// + /// # Errors + /// + /// Returns `SessionError::Busy` when Windows reports that a writer's + /// byte-range lock or a sharing rule blocks the read. The call does not + /// retry. Returns `SessionError::InvalidJournal` or + /// `SessionError::LimitReached` for a journal that fails validation, and + /// `SessionError::Io` for any other operating-system failure. + pub fn inspect(path: &Path) -> Result { + let filename = path + .file_name() + .and_then(|name| name.to_str()) + .ok_or(SessionError::InvalidJournal("filename is not valid UTF-8"))?; + let filename_id = filename::lenient_session_id(filename)?; + let metadata = fs::symlink_metadata(path).map_err(classify_lock_conflict)?; + if metadata.file_type().is_symlink() || !metadata.is_file() { + return Err(SessionError::InvalidJournal( + "journal is not a regular file", + )); + } + let mut file = storage::open_read_only_file(path) + .map_err(|error| classify_lock_conflict(map_storage_error(error)))?; + let contents = read_bounded(&mut file).map_err(classify_lock_conflict)?; + let complete_len = contents + .iter() + .rposition(|byte| *byte == b'\n') + .map(|index| index + 1) + .ok_or(SessionError::InvalidJournal( + "journal has no complete record", + ))?; + let complete = contents + .get(..complete_len) + .ok_or(SessionError::InvalidJournal( + "journal has no complete record", + ))?; + let mut entries = Vec::new(); + let Replayed { + snapshot, + created_at, + updated_at, + .. + } = replay(complete, filename_id, Some(&mut entries))?; + Ok(Self { + session_id: snapshot.session_id, + workspace_id: snapshot.workspace_id, + workspace: snapshot.workspace, + title: snapshot.title, + archived: snapshot.archived, + active_turn: snapshot.active_turn, + schema_version: snapshot.schema_version, + event_count: snapshot.event_count, + created_at, + updated_at, + incomplete_tail: complete_len != contents.len(), + entries, + }) + } +} + +/// Turn a Windows lock or sharing violation into the distinct `Busy` outcome. +/// +/// Raw error 33 is `ERROR_LOCK_VIOLATION` and 32 is `ERROR_SHARING_VIOLATION`. +/// Other platforms and error kinds pass through unchanged. +fn classify_lock_conflict(error: impl Into) -> SessionError { + let error = error.into(); + #[cfg(windows)] + if let SessionError::Io(io_error) = &error + && is_windows_lock_conflict(io_error) + { + return SessionError::Busy; + } + error +} + +#[cfg(windows)] +fn is_windows_lock_conflict(error: &std::io::Error) -> bool { + const ERROR_SHARING_VIOLATION: i32 = 32; + const ERROR_LOCK_VIOLATION: i32 = 33; + matches!( + error.raw_os_error(), + Some(ERROR_SHARING_VIOLATION | ERROR_LOCK_VIOLATION) + ) +} diff --git a/crates/omgcode-core/src/session_index.rs b/crates/omgcode-core/src/session_index.rs new file mode 100644 index 0000000..4b02534 --- /dev/null +++ b/crates/omgcode-core/src/session_index.rs @@ -0,0 +1,220 @@ +//! A derived, rebuildable index over session journals. +//! +//! The JSONL journals stay the only source of truth. This index is a cache that +//! lets a session tree list titles, folders, times, and status without +//! re-reading every journal, and lets the user search transcripts. Losing, +//! deleting, or corrupting the index never loses data: [`SessionIndex::open`] +//! discards an unusable database and the next [`SessionIndex::refresh`] +//! rebuilds it from the journals. The index grants no authority. Every value it +//! returns is untrusted historical data, and a stored path is revalidated +//! before [`SessionIndex::journal_path`] returns it. +//! +//! # Storage +//! +//! The index is one SQLite database, `/session-index-v1/index.sqlite3`, +//! built with the bundled SQLite and its FTS5 extension. It uses the default +//! rollback journal (not WAL) and these settings on every connection: +//! `application_id` [`APPLICATION_ID`], `user_version` 1, `foreign_keys=ON`, +//! `trusted_schema=OFF`, `secure_delete=ON` (the file holds transcript text), +//! a two-second busy timeout, `SQLITE_OPEN_NOFOLLOW`, no URI filenames, no +//! shared cache, and extension loading disabled. All statements are +//! parameterized. A sort order comes from a closed enum that maps to static SQL. +//! The directory and the database file are refused when either is a symbolic +//! link or Windows reparse point ([`IndexError::UnsafeLocation`]). +//! +//! A database that is not SQLite, is corrupt at open, or carries another +//! `application_id` or `user_version` is deleted together with its `-journal` +//! file, and an empty index is created. Nothing outside `session-index-v1/` is +//! touched. The schema is numbered as migration 1; a later migration can be +//! appended to the migration list. +//! +//! # What is indexed +//! +//! Per journal: the title, every message (user, assistant, tool), and a +//! compaction summary, each as one searchable text. Tool-call arguments, request +//! manifests, outcomes, and failure codes are not indexed. Journals are read only +//! through [`SessionInspection::inspect`], which never takes the writer lock. +//! The code points U+E000 and U+E001 are reserved as snippet markers, so they +//! are replaced with U+FFFD before text enters the index. +//! +//! # Concurrency +//! +//! A [`SessionIndex`] owns one connection and is used from one thread at a time +//! (`&mut self` for refresh). Two processes may open the same index; SQLite's +//! own locking arbitrates and contention that outlasts the busy timeout surfaces +//! as [`IndexError::Busy`]. Each transaction covers exactly one journal, and a +//! journal is inspected before its transaction opens, never inside it. +//! +//! # Busy and invalid journals +//! +//! A journal that a writer holds (Windows) keeps its previous metadata and text +//! with state `busy` and is retried on every refresh. A journal that fails +//! validation becomes `invalid` with a stable code, loses its text, and is not +//! inspected again until its size or modification time changes. An operating +//! system failure marks the journal `invalid` with `io_error` and is retried. +//! One bad journal never fails a refresh. + +mod query; +mod refresh; +mod schema; +mod snippet; + +use std::io; +use std::path::{Path, PathBuf}; + +use rusqlite::Connection; +use serde::Serialize; +use thiserror::Error; +use time::OffsetDateTime; +use uuid::Uuid; + +use crate::session::{NativePath, SessionError}; + +pub use query::{ + ArchiveView, SearchHit, SearchListing, SearchRole, SessionListing, SessionQuery, SessionSort, + SnippetPart, +}; +pub use refresh::RefreshReport; + +/// SQLite `application_id` stamped into the database header: the ASCII bytes +/// `OMGI` read as a big-endian integer. A database with another value is not +/// this index and is rebuilt. +pub const APPLICATION_ID: i32 = 0x4f4d_4749; + +/// Directory below the data root that holds the index. The `v1` names the +/// on-disk layout; a different layout would use a new directory. +pub const INDEX_DIRECTORY: &str = "session-index-v1"; + +/// Database file name inside [`INDEX_DIRECTORY`]. +pub const INDEX_FILE: &str = "index.sqlite3"; + +/// A failure while opening, refreshing, or querying the session index. +/// +/// No variant embeds transcript text or an absolute path. +#[derive(Debug, Error)] +pub enum IndexError { + /// Another connection held a conflicting lock past the busy timeout. + #[error("session index is busy")] + Busy, + /// The caller's search text cannot be used. + #[error("invalid search query: {0}")] + InvalidQuery(&'static str), + /// A stored value failed validation. The database is a cache that can be + /// tampered with, so nothing stored is trusted. + #[error("invalid session index entry: {0}")] + InvalidIndexEntry(&'static str), + /// The index directory or database file is a link, a reparse point, or not + /// the expected kind of file. + #[error("unsafe session index location: {0}")] + UnsafeLocation(&'static str), + /// Journal discovery failed. + #[error(transparent)] + Session(#[from] SessionError), + /// A filesystem operation on the index location failed. + #[error("session index I/O failed: {0}")] + Io(#[from] io::Error), + /// SQLite reported any other failure. + #[error("session index database operation failed")] + Sqlite(#[source] rusqlite::Error), +} + +impl From for IndexError { + fn from(error: rusqlite::Error) -> Self { + use rusqlite::ErrorCode; + if let rusqlite::Error::SqliteFailure(failure, _) = &error + && matches!( + failure.code, + ErrorCode::DatabaseBusy | ErrorCode::DatabaseLocked + ) + { + return Self::Busy; + } + Self::Sqlite(error) + } +} + +/// How a journal was last classified by the index. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum IndexedState { + /// Inspected successfully. Metadata and text are current as of the stored + /// size and modification time. + Ok, + /// A writer held the journal at the last attempt. Metadata and text, if any, + /// are from an earlier inspection. + Busy, + /// The journal failed validation or could not be read. It has no text. + Invalid, +} + +/// One session as the index last saw it. Everything here is untrusted +/// historical data and grants no authority. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct IndexedSession { + /// The session UUID taken from the journal file name. + pub session_id: Uuid, + /// How the journal was last classified. + pub state: IndexedState, + /// Stable code and static detail for an `Invalid` journal. + pub invalid_code: Option, + /// Session title, when the journal has been inspected. + pub title: Option, + /// Workspace association recorded at creation. + pub workspace_id: Option, + /// Lossless workspace path recorded at creation. Descriptive only. + pub workspace: Option, + /// Lossy workspace path text for display. Never reopen a path from it. + pub folder_display: Option, + /// Time of the `session.started` event. + #[serde(with = "time::serde::rfc3339::option")] + pub created_at: Option, + /// Time of the last complete event. + #[serde(with = "time::serde::rfc3339::option")] + pub updated_at: Option, + /// Whether the session was archived after its last complete event. `false` + /// when unknown. + pub archived: bool, + /// The turn left active by the last complete event, if any. + pub active_turn: Option, + /// Number of complete events, including `session.started`. + pub event_count: u64, + /// Whether bytes followed the last complete record when inspected. + pub incomplete_tail: bool, +} + +/// A handle to the derived session index. See the module documentation. +#[derive(Debug)] +pub struct SessionIndex { + data_root: PathBuf, + connection: Connection, +} + +impl SessionIndex { + /// Open the index below `data_root`, creating it when absent. + /// + /// `data_root` must come from trusted application settings. The index lives + /// in `/session-index-v1/`. A database that is not SQLite, is + /// corrupt, or has another `application_id` or `user_version` is deleted + /// (only the index's own files) and recreated empty; call + /// [`refresh`](Self::refresh) to repopulate it. + /// + /// # Errors + /// + /// Returns [`IndexError::UnsafeLocation`] when the index directory or file is + /// a link or reparse point or is not the expected kind of object, + /// [`IndexError::Busy`] when another connection holds the database past the + /// busy timeout, [`IndexError::Io`] for filesystem failures, and + /// [`IndexError::Sqlite`] for other database failures. + pub fn open(data_root: &Path) -> Result { + let connection = schema::open_connection(data_root)?; + Ok(Self { + data_root: data_root.to_path_buf(), + connection, + }) + } + + /// The data root this index describes journals for. + pub fn data_root(&self) -> &Path { + &self.data_root + } +} diff --git a/crates/omgcode-core/src/session_index/query.rs b/crates/omgcode-core/src/session_index/query.rs new file mode 100644 index 0000000..41c22fc --- /dev/null +++ b/crates/omgcode-core/src/session_index/query.rs @@ -0,0 +1,486 @@ +//! Listing, lookup, and full-text search over the index. + +use std::path::PathBuf; + +use rusqlite::Row; +use serde::Serialize; +use time::OffsetDateTime; +use uuid::Uuid; + +use super::snippet::{MARK_END, MARK_START, match_expression, parse_snippet}; +use super::{IndexError, IndexedSession, IndexedState, SessionIndex}; +use crate::session::{NativePath, strict_journal_session_id}; + +/// The most sessions one listing returns. +const MAX_LISTING: usize = 5000; +/// The most hits one search returns. +const MAX_HITS: usize = 200; +/// Tokens of context around a match in a snippet. +const SNIPPET_TOKENS: i64 = 24; +/// The code recorded for a session UUID carried by two journal files. +const CODE_DUPLICATE: &str = "duplicate_session_id"; + +/// The columns `IndexedSession` needs, in the order `RawSession::read` expects. +/// Statements alias `journal` as `j`. +macro_rules! session_columns { + () => { + "j.session_id, j.state, j.invalid_code, j.title, j.workspace_id, \ + j.workspace_encoding, j.workspace_units, j.folder_display, j.created_ns, \ + j.updated_ns, j.archived, j.active_turn, j.event_count, j.incomplete_tail" + }; +} + +/// A listing statement with a fixed `ORDER BY`. The order text is a literal at +/// the call site, never caller input. +macro_rules! list_sql { + ($order:literal) => { + concat!( + "SELECT ", + session_columns!(), + " FROM journal j WHERE j.archived = ?1 ORDER BY ", + $order, + " LIMIT ?2" + ) + }; +} + +/// Which sessions a listing or search covers. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ArchiveView { + /// Sessions that are not archived, including those with unknown status + /// because their journal is busy or invalid and was never read. + Active, + /// Archived sessions. + Archived, +} + +impl ArchiveView { + fn archived_flag(self) -> i64 { + match self { + Self::Active => 0, + Self::Archived => 1, + } + } +} + +/// The key a listing sorts by. Rows without a value for the key sort last in +/// either direction, and ties break by session UUID ascending. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SessionSort { + /// Time of the last complete event. + Updated, + /// Time of the `session.started` event. + Created, + /// Title, compared case-insensitively by Unicode lowercase. + Title, +} + +/// A listing request. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct SessionQuery { + /// Archived or active sessions. + pub view: ArchiveView, + /// The sort key. + pub sort: SessionSort, + /// Sort largest or latest first, or last to first for titles. + pub descending: bool, + /// The most sessions to return, clamped to 1 through 5000. + pub limit: usize, +} + +/// One page of a listing. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SessionListing { + /// Sessions in the requested order, at most `limit`. + pub sessions: Vec, + /// How many sessions the view holds before `limit` applied. + pub total: u64, +} + +/// What kind of text a search hit matched. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SearchRole { + /// A user message. + User, + /// An assistant message. + Assistant, + /// A tool result message. + Tool, + /// A context compaction summary. + Summary, + /// The session title. + Title, +} + +/// A run of snippet text, plain or matched. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SnippetPart { + /// Transcript text. It never contains the reserved marker code points. + pub text: String, + /// Whether the search matched this run. + pub matched: bool, +} + +/// One matching text. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SearchHit { + /// The session the text belongs to. + pub session: IndexedSession, + /// The journal sequence number, or `None` for a title. + pub seq: Option, + /// What kind of text matched. + pub role: SearchRole, + /// About 24 tokens around the match. + pub snippet: Vec, +} + +/// Search results. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct SearchListing { + /// Hits ordered by relevance, then newest session, then position. + pub hits: Vec, + /// Whether more hits exist than `limit` allowed. + pub truncated: bool, +} + +/// The stored values of one `journal` row before validation. +struct RawSession { + session_id: String, + state: String, + invalid_code: Option, + title: Option, + workspace_id: Option, + workspace_encoding: Option, + workspace_units: Option>, + folder_display: Option, + created_ns: Option, + updated_ns: Option, + archived: i64, + active_turn: Option, + event_count: i64, + incomplete_tail: i64, +} + +impl RawSession { + fn read(row: &Row<'_>) -> rusqlite::Result { + Ok(Self { + session_id: row.get(0)?, + state: row.get(1)?, + invalid_code: row.get(2)?, + title: row.get(3)?, + workspace_id: row.get(4)?, + workspace_encoding: row.get(5)?, + workspace_units: row.get(6)?, + folder_display: row.get(7)?, + created_ns: row.get(8)?, + updated_ns: row.get(9)?, + archived: row.get(10)?, + active_turn: row.get(11)?, + event_count: row.get(12)?, + incomplete_tail: row.get(13)?, + }) + } + + /// Validate every stored value. The database is a cache that may have been + /// altered, so nothing is trusted. + fn into_session(self) -> Result { + let state = match self.state.as_str() { + "ok" => IndexedState::Ok, + "busy" => IndexedState::Busy, + "invalid" => IndexedState::Invalid, + _ => return Err(IndexError::InvalidIndexEntry("unknown journal state")), + }; + Ok(IndexedSession { + session_id: parse_uuid(&self.session_id)?, + state, + invalid_code: self.invalid_code, + title: self.title, + workspace_id: self.workspace_id.as_deref().map(parse_uuid).transpose()?, + workspace: decode_workspace(self.workspace_encoding, self.workspace_units)?, + folder_display: self.folder_display, + created_at: self.created_ns.map(timestamp).transpose()?, + updated_at: self.updated_ns.map(timestamp).transpose()?, + archived: self.archived != 0, + active_turn: self.active_turn.as_deref().map(parse_uuid).transpose()?, + event_count: u64::try_from(self.event_count) + .map_err(|_| IndexError::InvalidIndexEntry("negative event count"))?, + incomplete_tail: self.incomplete_tail != 0, + }) + } +} + +fn parse_uuid(text: &str) -> Result { + Uuid::try_parse(text).map_err(|_| IndexError::InvalidIndexEntry("malformed UUID")) +} + +fn timestamp(nanos: i64) -> Result { + OffsetDateTime::from_unix_timestamp_nanos(i128::from(nanos)) + .map_err(|_| IndexError::InvalidIndexEntry("timestamp out of range")) +} + +fn decode_workspace( + encoding: Option, + units: Option>, +) -> Result, IndexError> { + match (encoding.as_deref(), units) { + (None, None) => Ok(None), + (Some("unix_bytes"), Some(bytes)) => Ok(Some(NativePath::UnixBytes(bytes))), + (Some("windows_utf16"), Some(bytes)) => { + if bytes.len() % 2 != 0 { + return Err(IndexError::InvalidIndexEntry("odd UTF-16 byte count")); + } + let (pairs, _) = bytes.as_chunks::<2>(); + let units = pairs.iter().map(|pair| u16::from_le_bytes(*pair)).collect(); + Ok(Some(NativePath::WindowsUtf16(units))) + } + _ => Err(IndexError::InvalidIndexEntry("malformed workspace path")), + } +} + +impl SessionIndex { + /// List sessions in one view with a fixed, deterministic order. + /// + /// `total` counts the whole view before `limit` (clamped to 1 through 5000) + /// applies. Busy and invalid sessions are listed with their state. + /// + /// # Errors + /// + /// Returns database failures and [`IndexError::InvalidIndexEntry`] when a + /// stored row fails validation. + pub fn sessions(&self, query: &SessionQuery) -> Result { + let sql = match (query.sort, query.descending) { + (SessionSort::Updated, false) => { + list_sql!("j.updated_ns IS NULL, j.updated_ns ASC, j.session_id ASC") + } + (SessionSort::Updated, true) => { + list_sql!("j.updated_ns IS NULL, j.updated_ns DESC, j.session_id ASC") + } + (SessionSort::Created, false) => { + list_sql!("j.created_ns IS NULL, j.created_ns ASC, j.session_id ASC") + } + (SessionSort::Created, true) => { + list_sql!("j.created_ns IS NULL, j.created_ns DESC, j.session_id ASC") + } + (SessionSort::Title, false) => { + list_sql!("j.title_key IS NULL, j.title_key ASC, j.session_id ASC") + } + (SessionSort::Title, true) => { + list_sql!("j.title_key IS NULL, j.title_key DESC, j.session_id ASC") + } + }; + let limit = i64::try_from(query.limit.clamp(1, MAX_LISTING)).unwrap_or(i64::MAX); + let archived = query.view.archived_flag(); + let total: i64 = self.connection.query_row( + "SELECT count(*) FROM journal WHERE archived = ?1", + [archived], + |row| row.get(0), + )?; + let mut statement = self.connection.prepare(sql)?; + let raws = statement + .query_map(rusqlite::params![archived, limit], RawSession::read)? + .collect::, _>>()?; + let sessions = raws + .into_iter() + .map(RawSession::into_session) + .collect::, _>>()?; + Ok(SessionListing { + sessions, + total: u64::try_from(total).unwrap_or(0), + }) + } + + /// Look up one session by UUID. + /// + /// # Errors + /// + /// Returns database failures and [`IndexError::InvalidIndexEntry`]. + pub fn session(&self, session_id: Uuid) -> Result, IndexError> { + let key = session_id.as_hyphenated().to_string(); + let mut statement = self.connection.prepare(concat!( + "SELECT ", + session_columns!(), + " FROM journal j WHERE j.session_id = ?1" + ))?; + let mut rows = statement.query_map([key], RawSession::read)?; + rows.next() + .transpose()? + .map(RawSession::into_session) + .transpose() + } + + /// Resolve a session UUID to its journal path for a later writer open. + /// + /// The database is a cache and could have been altered, so the stored + /// relative path is revalidated before it is joined to the data root: it + /// must be exactly `sessions///
/` where `` is the + /// writer's exact journal name carrying `session_id`. Nothing on the + /// filesystem is touched, and the journal may no longer exist. + /// + /// Returns `None` when the index has no row for the session. + /// + /// # Errors + /// + /// Returns [`IndexError::InvalidIndexEntry`] for a stored path that fails + /// validation, and for a session whose UUID two journal files carry. + pub fn journal_path(&self, session_id: Uuid) -> Result, IndexError> { + let key = session_id.as_hyphenated().to_string(); + let mut statement = self + .connection + .prepare("SELECT relative_path, invalid_code FROM journal WHERE session_id = ?1")?; + let mut rows = statement.query_map([key], |row| { + Ok((row.get::<_, String>(0)?, row.get::<_, Option>(1)?)) + })?; + let Some(row) = rows.next().transpose()? else { + return Ok(None); + }; + let (relative_path, invalid_code) = row; + if invalid_code.as_deref() == Some(CODE_DUPLICATE) { + return Err(IndexError::InvalidIndexEntry( + "more than one journal has this session UUID", + )); + } + let mut path = self.data_root.clone(); + for part in validated_components(&relative_path, session_id)? { + path.push(part); + } + Ok(Some(path)) + } + + /// Search transcripts for `text`. + /// + /// The text is never FTS5 syntax. Whitespace-separated terms (at most 16, + /// at most 256 characters in all) must all match, case- and + /// diacritic-insensitively, and the last term also matches as a prefix. + /// Only `ok` and `busy` sessions in `view` can match. `limit` is clamped to + /// 1 through 200. Results order by relevance, then most recently updated + /// session, then session and position. + /// + /// # Errors + /// + /// Returns [`IndexError::InvalidQuery`] for unusable text, database + /// failures, and [`IndexError::InvalidIndexEntry`]. + pub fn search( + &self, + text: &str, + view: ArchiveView, + limit: usize, + ) -> Result { + let expression = match_expression(text)?; + let limit = limit.clamp(1, MAX_HITS); + let fetch = i64::try_from(limit + 1).unwrap_or(i64::MAX); + let mut statement = self.connection.prepare(concat!( + "SELECT ", + session_columns!(), + ", entry_text.seq, entry_text.role, snippet(entry_text, 0, ?4, ?5, '', ?6) \ + FROM entry_text JOIN journal j ON j.session_id = entry_text.session_id \ + WHERE entry_text MATCH ?1 AND j.state IN ('ok', 'busy') AND j.archived = ?2 \ + ORDER BY rank, j.updated_ns DESC, j.session_id ASC, entry_text.seq ASC \ + LIMIT ?3" + ))?; + let start = MARK_START.to_string(); + let end = MARK_END.to_string(); + let raws = statement + .query_map( + rusqlite::params![ + expression, + view.archived_flag(), + fetch, + start, + end, + SNIPPET_TOKENS + ], + |row| { + Ok(( + RawSession::read(row)?, + row.get::<_, Option>(14)?, + row.get::<_, String>(15)?, + row.get::<_, String>(16)?, + )) + }, + )? + .collect::, _>>()?; + let mut hits = Vec::new(); + for (raw, seq, role, marked) in raws { + hits.push(SearchHit { + session: raw.into_session()?, + seq: seq + .map(u64::try_from) + .transpose() + .map_err(|_| IndexError::InvalidIndexEntry("negative sequence number"))?, + role: parse_role(&role)?, + snippet: parse_snippet(&marked), + }); + } + let truncated = hits.len() > limit; + hits.truncate(limit); + Ok(SearchListing { hits, truncated }) + } +} + +fn parse_role(role: &str) -> Result { + match role { + "user" => Ok(SearchRole::User), + "assistant" => Ok(SearchRole::Assistant), + "tool" => Ok(SearchRole::Tool), + "summary" => Ok(SearchRole::Summary), + "title" => Ok(SearchRole::Title), + _ => Err(IndexError::InvalidIndexEntry("unknown text role")), + } +} + +/// Check a stored relative path against the exact layout the writer produces +/// and return its components. +fn validated_components(relative: &str, session_id: Uuid) -> Result, IndexError> { + const INVALID: IndexError = IndexError::InvalidIndexEntry("stored journal path is not valid"); + if relative.contains(['\\', '\0']) { + return Err(INVALID); + } + let parts: Vec<&str> = relative.split('/').collect(); + let [sessions, year, month, day, file] = parts.as_slice() else { + return Err(INVALID); + }; + let layout_ok = *sessions == "sessions" + && has_ascii_digits(year, 4) + && has_ascii_digits(month, 2) + && has_ascii_digits(day, 2); + if !layout_ok || strict_journal_session_id(file) != Some(session_id) { + return Err(INVALID); + } + Ok(parts) +} + +fn has_ascii_digits(text: &str, width: usize) -> bool { + text.len() == width && text.bytes().all(|byte| byte.is_ascii_digit()) +} + +#[cfg(test)] +mod tests { + use super::validated_components; + use uuid::Uuid; + + #[test] + fn only_the_exact_writer_layout_for_the_same_session_validates() { + let id = Uuid::new_v4(); + let other = Uuid::new_v4(); + let good = format!("sessions/2026/11/16/omgcode-01-02-03-004Z-{id}.jsonl"); + assert_eq!(validated_components(&good, id).expect("valid").len(), 5); + let bad = [ + "../../evil.jsonl".to_owned(), + format!("/sessions/2026/11/16/omgcode-01-02-03-004Z-{id}.jsonl"), + format!("sessions\\2026\\11\\16\\omgcode-01-02-03-004Z-{id}.jsonl"), + format!("sessions/2026/11/16/omgcode-01-02-03-004Z-{other}.jsonl"), + format!("sessions/2026/11/16/extra/omgcode-01-02-03-004Z-{id}.jsonl"), + format!("sessions/2026/11/../16/omgcode-01-02-03-004Z-{id}.jsonl"), + format!("sessions/26/11/16/omgcode-01-02-03-004Z-{id}.jsonl"), + format!("sessions/2026/11/16/omgcode-01-02-03-004Z-{id}.jsonl\0"), + format!("other/2026/11/16/omgcode-01-02-03-004Z-{id}.jsonl"), + String::new(), + ]; + for path in bad { + assert!(validated_components(&path, id).is_err(), "{path:?}"); + } + } +} diff --git a/crates/omgcode-core/src/session_index/refresh.rs b/crates/omgcode-core/src/session_index/refresh.rs new file mode 100644 index 0000000..39e9100 --- /dev/null +++ b/crates/omgcode-core/src/session_index/refresh.rs @@ -0,0 +1,691 @@ +//! Incremental, bounded refresh of the index from the journals. +//! +//! A refresh inspects at most a caller-chosen number of journals, one at a time, +//! with no database transaction open. It then writes each result in its own +//! short transaction. Journals are opened only through +//! `SessionInspection::inspect`, which never takes the writer lock. + +use std::collections::{HashMap, HashSet}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use rusqlite::{Connection, OptionalExtension, TransactionBehavior, named_params}; +use serde::Serialize; +use time::OffsetDateTime; +use uuid::Uuid; + +use super::snippet::sanitize_text; +use super::{IndexError, IndexedSession, SessionIndex}; +use crate::session::{ + self, DiscoveredJournal, MessageRole, NativePath, SessionError, SessionInspection, + TranscriptEntryKind, +}; + +/// The most journals one refresh call inspects, however large the request. +const MAX_JOURNALS_PER_REFRESH: usize = 1000; + +/// Stable code for a session UUID that two journal files carry. +const CODE_DUPLICATE: &str = "duplicate_session_id"; +/// Stable code for an operating-system failure. The journal is retried. +const CODE_IO: &str = "io_error"; + +/// What one `refresh` call did. Counts of `busy` and `invalid` cover only the +/// journals this call inspected or reclassified. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)] +pub struct RefreshReport { + /// Journals found by discovery. + pub discovered: u64, + /// Journals this call opened for inspection. + pub inspected: u64, + /// Journals skipped because size, modification time, and path match a + /// settled row. + pub unchanged: u64, + /// Journals this call found held by a writer. + pub busy: u64, + /// Journals this call classified as invalid, including duplicates. + pub invalid: u64, + /// Rows removed because their journal is no longer discovered. + pub removed: u64, + /// Journals that need inspection but exceeded this call's budget. Call + /// again to continue. + pub remaining: u64, + /// Links and reparse points that discovery skipped. + pub skipped_links: u64, + /// Entries that did not match the journal layout. + pub unrecognized: u64, +} + +/// The facts a stored row contributes to the unchanged check. +struct Existing { + relative_path: String, + size: i64, + modified_ns: i64, + state: String, + invalid_code: Option, +} + +impl Existing { + /// A row is settled when nothing about the file changed and the last result + /// will not change without the file changing. + fn is_settled_for(&self, journal: &DiscoveredJournal, relative_path: &str) -> bool { + let same_file = self.relative_path == relative_path + && self.size == size_value(journal.size) + && self.modified_ns == modified_value(journal.modified); + if !same_file { + return false; + } + match self.state.as_str() { + "ok" => true, + "invalid" => !matches!( + self.invalid_code.as_deref(), + Some(CODE_IO | CODE_DUPLICATE) | None + ), + _ => false, + } + } + + fn is_settled_duplicate(&self, journal: &DiscoveredJournal, relative_path: &str) -> bool { + self.state == "invalid" + && self.invalid_code.as_deref() == Some(CODE_DUPLICATE) + && self.relative_path == relative_path + && self.size == size_value(journal.size) + && self.modified_ns == modified_value(journal.modified) + } +} + +/// The result of inspecting one journal, ready to store. +enum Outcome { + Indexed(Box), + Busy, + Invalid { + code: String, + /// Whether to record size and modification time so the journal is not + /// inspected again until it changes. + record_stat: bool, + }, +} + +/// Metadata and searchable texts of an inspected journal. +struct Indexed { + metadata: Metadata, + rows: Vec, +} + +struct Metadata { + title: String, + title_key: String, + workspace_id: String, + workspace_encoding: &'static str, + workspace_units: Vec, + folder_display: String, + created_ns: i64, + updated_ns: i64, + archived: bool, + active_turn: Option, + event_count: i64, + schema_version: i64, + incomplete_tail: bool, +} + +struct TextRow { + text: String, + seq: Option, + role: &'static str, +} + +/// The values for one `journal` row. `metadata` is `None` for busy or invalid. +struct JournalRow<'a> { + session_id: &'a str, + relative_path: &'a str, + size: i64, + modified_ns: i64, + state: &'static str, + invalid_code: Option<&'a str>, + metadata: Option<&'a Metadata>, + fts: Option<(i64, i64)>, +} + +impl SessionIndex { + /// Bring the index up to date with the journals on disk. + /// + /// Discovers journals, removes rows for vanished ones, then inspects at most + /// `max_journals` (clamped to 1 through 1000) whose size, modification time, + /// or path changed, or whose last result was `busy` or an operating-system + /// failure. Journals left over are counted in `remaining`; call again until + /// it is zero. A journal held by a writer keeps its earlier data with state + /// `busy`. An invalid journal is recorded and not re-inspected until it + /// changes. Two files carrying one session UUID make that session invalid + /// with `duplicate_session_id` and neither file's text is indexed. + /// + /// # Errors + /// + /// Returns discovery failures as [`IndexError::Session`], and database + /// failures, including [`IndexError::Busy`]. A failure in one journal is + /// recorded for that journal and never fails the refresh. Work committed + /// before an error stays committed. + pub fn refresh(&mut self, max_journals: usize) -> Result { + let budget = max_journals.clamp(1, MAX_JOURNALS_PER_REFRESH); + let discovery = session::discover(&self.data_root)?; + let mut report = RefreshReport { + discovered: count(discovery.journals.len()), + skipped_links: discovery.skipped_links, + unrecognized: discovery.unrecognized, + ..RefreshReport::default() + }; + let existing = load_existing(&self.connection)?; + + let present: HashSet = discovery + .journals + .iter() + .map(|journal| journal.session_id) + .collect(); + for key in existing.keys() { + let still_present = Uuid::try_parse(key).is_ok_and(|id| present.contains(&id)); + if !still_present { + remove_session(&mut self.connection, key)?; + report.removed += 1; + } + } + + let mut copies: HashMap = HashMap::new(); + for journal in &discovery.journals { + *copies.entry(journal.session_id).or_default() += 1; + } + let mut handled_duplicates: HashSet = HashSet::new(); + for journal in &discovery.journals { + let relative = relative_text(journal)?; + let key = journal.session_id.as_hyphenated().to_string(); + let row = existing.get(&key); + if copies.get(&journal.session_id).copied().unwrap_or(0) > 1 { + if !handled_duplicates.insert(journal.session_id) { + continue; + } + if row.is_some_and(|row| row.is_settled_duplicate(journal, &relative)) { + report.unchanged += 1; + } else { + self.record_duplicate(journal, &relative)?; + report.invalid += 1; + } + continue; + } + if row.is_some_and(|row| row.is_settled_for(journal, &relative)) { + report.unchanged += 1; + } else if report.inspected >= count(budget) { + report.remaining += 1; + } else { + report.inspected += 1; + match self.index_journal(journal, &relative, row)? { + IndexedAs::Ok => {} + IndexedAs::Busy => report.busy += 1, + IndexedAs::Invalid => report.invalid += 1, + } + } + } + Ok(report) + } + + /// Re-inspect exactly one session now, whatever its size and modification + /// time. Use it right after the application's own writer closes a journal. + /// + /// Returns `None` when no journal has `session_id`, and removes a stale row. + /// A journal held by a writer yields its row with state `busy`. + /// + /// # Errors + /// + /// Returns the same errors as [`refresh`](Self::refresh). + pub fn refresh_session( + &mut self, + session_id: Uuid, + ) -> Result, IndexError> { + let discovery = session::discover(&self.data_root)?; + let matches: Vec<&DiscoveredJournal> = discovery + .journals + .iter() + .filter(|journal| journal.session_id == session_id) + .collect(); + let key = session_id.as_hyphenated().to_string(); + let existing = load_existing_one(&self.connection, &key)?; + match matches.as_slice() { + [] => { + remove_session(&mut self.connection, &key)?; + return Ok(None); + } + [journal] => { + let relative = relative_text(journal)?; + self.index_journal(journal, &relative, existing.as_ref())?; + } + [first, ..] => { + let relative = relative_text(first)?; + self.record_duplicate(first, &relative)?; + } + } + self.session(session_id) + } + + /// Inspect `journal` with no transaction open, then store the result. + fn index_journal( + &mut self, + journal: &DiscoveredJournal, + relative: &str, + existing: Option<&Existing>, + ) -> Result { + let outcome = inspect(journal); + let key = journal.session_id.as_hyphenated().to_string(); + let transaction = self + .connection + .transaction_with_behavior(TransactionBehavior::Immediate)?; + clear_path_conflicts(&transaction, &key, relative)?; + let size = size_value(journal.size); + let modified_ns = modified_value(journal.modified); + let stored = match outcome { + Outcome::Indexed(indexed) => { + delete_text(&transaction, &key)?; + let fts = insert_text(&transaction, &key, &indexed.rows)?; + write_row( + &transaction, + &JournalRow { + session_id: &key, + relative_path: relative, + size, + modified_ns, + state: "ok", + invalid_code: None, + metadata: Some(&indexed.metadata), + fts, + }, + )?; + IndexedAs::Ok + } + Outcome::Busy => { + // Keep earlier metadata and text, and keep the old size and + // time so the next refresh retries. + let changed = transaction.execute( + "UPDATE journal SET state = 'busy', invalid_code = NULL, relative_path = ?2 \ + WHERE session_id = ?1", + rusqlite::params![key, relative], + )?; + if changed == 0 { + write_row( + &transaction, + &JournalRow { + session_id: &key, + relative_path: relative, + size: 0, + modified_ns: 0, + state: "busy", + invalid_code: None, + metadata: None, + fts: None, + }, + )?; + } + IndexedAs::Busy + } + Outcome::Invalid { code, record_stat } => { + delete_text(&transaction, &key)?; + let (size, modified_ns) = if record_stat { + (size, modified_ns) + } else { + existing.map_or((0, 0), |row| (row.size, row.modified_ns)) + }; + write_row( + &transaction, + &JournalRow { + session_id: &key, + relative_path: relative, + size, + modified_ns, + state: "invalid", + invalid_code: Some(&code), + metadata: None, + fts: None, + }, + )?; + IndexedAs::Invalid + } + }; + transaction.commit()?; + Ok(stored) + } + + /// Mark a session invalid because two files carry its UUID. Neither file is + /// inspected and no text stays indexed. + fn record_duplicate( + &mut self, + first: &DiscoveredJournal, + relative: &str, + ) -> Result<(), IndexError> { + let key = first.session_id.as_hyphenated().to_string(); + let transaction = self + .connection + .transaction_with_behavior(TransactionBehavior::Immediate)?; + clear_path_conflicts(&transaction, &key, relative)?; + delete_text(&transaction, &key)?; + write_row( + &transaction, + &JournalRow { + session_id: &key, + relative_path: relative, + size: size_value(first.size), + modified_ns: modified_value(first.modified), + state: "invalid", + invalid_code: Some(CODE_DUPLICATE), + metadata: None, + fts: None, + }, + )?; + transaction.commit()?; + Ok(()) + } +} + +/// How a stored journal ended up, for the report. +enum IndexedAs { + Ok, + Busy, + Invalid, +} + +/// Inspect one journal and turn the result into a storable outcome. No error +/// from one journal escapes: it becomes an outcome for that journal. +fn inspect(journal: &DiscoveredJournal) -> Outcome { + match SessionInspection::inspect(&journal.path) { + Ok(inspection) => { + if inspection.session_id != journal.session_id { + return Outcome::Invalid { + code: "session_id_mismatch".to_owned(), + record_stat: true, + }; + } + match build_indexed(&inspection) { + Some(indexed) => Outcome::Indexed(Box::new(indexed)), + None => Outcome::Invalid { + code: "timestamp_out_of_range".to_owned(), + record_stat: true, + }, + } + } + Err(SessionError::Busy) => Outcome::Busy, + Err(SessionError::InvalidJournal(detail)) => Outcome::Invalid { + code: format!("invalid_journal: {detail}"), + record_stat: true, + }, + Err(SessionError::LimitReached) => Outcome::Invalid { + code: "limit_reached".to_owned(), + record_stat: true, + }, + Err(_) => Outcome::Invalid { + code: CODE_IO.to_owned(), + record_stat: false, + }, + } +} + +/// Reduce an inspection to the stored metadata and searchable texts. Returns +/// `None` when a timestamp does not fit the stored integer. +fn build_indexed(inspection: &SessionInspection) -> Option { + let (workspace_encoding, workspace_units) = encode_workspace(&inspection.workspace); + let folder_display = inspection + .workspace + .to_path_buf() + .map(|path| path.to_string_lossy().into_owned()) + .unwrap_or_default(); + let title = sanitize_text(&inspection.title); + let metadata = Metadata { + title_key: title.to_lowercase(), + title, + workspace_id: inspection.workspace_id.as_hyphenated().to_string(), + workspace_encoding, + workspace_units, + folder_display, + created_ns: nanos(inspection.created_at)?, + updated_ns: nanos(inspection.updated_at)?, + archived: inspection.archived, + active_turn: inspection + .active_turn + .map(|turn| turn.as_hyphenated().to_string()), + event_count: i64::try_from(inspection.event_count).ok()?, + schema_version: i64::from(inspection.schema_version), + incomplete_tail: inspection.incomplete_tail, + }; + let mut rows = Vec::new(); + if !inspection.title.is_empty() { + rows.push(TextRow { + text: sanitize_text(&inspection.title), + seq: None, + role: "title", + }); + } + for entry in &inspection.entries { + let seq = i64::try_from(entry.seq).ok(); + match &entry.kind { + TranscriptEntryKind::Message { role, text, .. } => rows.push(TextRow { + text: sanitize_text(text), + seq, + role: match role { + MessageRole::User => "user", + MessageRole::Assistant => "assistant", + MessageRole::Tool => "tool", + }, + }), + TranscriptEntryKind::ContextCompacted { record } => rows.push(TextRow { + text: sanitize_text(&record.summary), + seq, + role: "summary", + }), + _ => {} + } + } + Some(Indexed { metadata, rows }) +} + +/// Store a workspace path as an encoding tag and unit bytes that round-trip. +fn encode_workspace(workspace: &NativePath) -> (&'static str, Vec) { + match workspace { + NativePath::UnixBytes(bytes) => ("unix_bytes", bytes.clone()), + NativePath::WindowsUtf16(units) => ( + "windows_utf16", + units.iter().flat_map(|unit| unit.to_le_bytes()).collect(), + ), + } +} + +fn nanos(time: OffsetDateTime) -> Option { + i64::try_from(time.unix_timestamp_nanos()).ok() +} + +fn count(value: usize) -> u64 { + u64::try_from(value).unwrap_or(u64::MAX) +} + +fn size_value(size: u64) -> i64 { + i64::try_from(size).unwrap_or(i64::MAX) +} + +fn modified_value(modified: SystemTime) -> i64 { + match modified.duration_since(UNIX_EPOCH) { + Ok(after) => i64::try_from(after.as_nanos()).unwrap_or(i64::MAX), + Err(before) => i64::try_from(before.duration().as_nanos()) + .map(|nanos| -nanos) + .unwrap_or(i64::MIN), + } +} + +/// The forward-slash form of the journal's path below the data root. +fn relative_text(journal: &DiscoveredJournal) -> Result { + let mut text = String::new(); + for component in journal.relative_path.components() { + let part = component + .as_os_str() + .to_str() + .ok_or(IndexError::InvalidIndexEntry("journal path is not UTF-8"))?; + if !text.is_empty() { + text.push('/'); + } + text.push_str(part); + } + Ok(text) +} + +fn load_existing(connection: &Connection) -> Result, IndexError> { + let mut statement = connection.prepare( + "SELECT session_id, relative_path, size, modified_ns, state, invalid_code FROM journal", + )?; + let rows = statement.query_map([], |row| { + Ok(( + row.get::<_, String>(0)?, + Existing { + relative_path: row.get(1)?, + size: row.get(2)?, + modified_ns: row.get(3)?, + state: row.get(4)?, + invalid_code: row.get(5)?, + }, + )) + })?; + let mut map = HashMap::new(); + for row in rows { + let (key, existing) = row?; + map.insert(key, existing); + } + Ok(map) +} + +fn load_existing_one(connection: &Connection, key: &str) -> Result, IndexError> { + Ok(connection + .query_row( + "SELECT relative_path, size, modified_ns, state, invalid_code FROM journal \ + WHERE session_id = ?1", + [key], + |row| { + Ok(Existing { + relative_path: row.get(0)?, + size: row.get(1)?, + modified_ns: row.get(2)?, + state: row.get(3)?, + invalid_code: row.get(4)?, + }) + }, + ) + .optional()?) +} + +/// Remove a session's row and text in one transaction. +fn remove_session(connection: &mut Connection, key: &str) -> Result<(), IndexError> { + let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?; + delete_text(&transaction, key)?; + transaction.execute("DELETE FROM journal WHERE session_id = ?1", [key])?; + transaction.commit()?; + Ok(()) +} + +/// Delete another session's row that claims `relative_path`. A name carries its +/// own UUID, so this only happens when the stored rows were altered. +fn clear_path_conflicts( + connection: &Connection, + key: &str, + relative_path: &str, +) -> Result<(), IndexError> { + let mut statement = connection + .prepare("SELECT session_id FROM journal WHERE relative_path = ?1 AND session_id <> ?2")?; + let others = statement + .query_map([relative_path, key], |row| row.get::<_, String>(0))? + .collect::, _>>()?; + for other in others { + delete_text(connection, &other)?; + connection.execute("DELETE FROM journal WHERE session_id = ?1", [other])?; + } + Ok(()) +} + +/// Delete the session's `entry_text` rows by the rowid range its row records. +/// The session filter keeps an altered range from deleting another session's +/// text. +fn delete_text(connection: &Connection, key: &str) -> Result<(), IndexError> { + let range: Option<(Option, Option)> = connection + .query_row( + "SELECT fts_first, fts_last FROM journal WHERE session_id = ?1", + [key], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .optional()?; + if let Some((Some(first), Some(last))) = range { + connection.execute( + "DELETE FROM entry_text WHERE rowid BETWEEN ?1 AND ?2 AND session_id = ?3", + rusqlite::params![first, last, key], + )?; + } + Ok(()) +} + +/// Insert searchable texts. Inside the write transaction the rowids are +/// assigned in increasing order, so the first and last bound all of them. +fn insert_text( + connection: &Connection, + key: &str, + rows: &[TextRow], +) -> Result, IndexError> { + let mut statement = connection + .prepare("INSERT INTO entry_text (text, session_id, seq, role) VALUES (?1, ?2, ?3, ?4)")?; + let mut bounds: Option<(i64, i64)> = None; + for row in rows { + statement.execute(rusqlite::params![row.text, key, row.seq, row.role])?; + let rowid = connection.last_insert_rowid(); + bounds = Some(bounds.map_or((rowid, rowid), |(first, _)| (first, rowid))); + } + Ok(bounds) +} + +/// Insert or replace the `journal` row for one session. +fn write_row(connection: &Connection, row: &JournalRow<'_>) -> Result<(), IndexError> { + let metadata = row.metadata; + let (first, last) = row.fts.unzip(); + connection.execute( + "INSERT INTO journal ( + session_id, relative_path, size, modified_ns, state, invalid_code, + title, title_key, workspace_id, workspace_encoding, workspace_units, + folder_display, created_ns, updated_ns, archived, active_turn, + event_count, schema_version, incomplete_tail, fts_first, fts_last + ) VALUES ( + :session_id, :relative_path, :size, :modified_ns, :state, :invalid_code, + :title, :title_key, :workspace_id, :workspace_encoding, :workspace_units, + :folder_display, :created_ns, :updated_ns, :archived, :active_turn, + :event_count, :schema_version, :incomplete_tail, :fts_first, :fts_last + ) ON CONFLICT (session_id) DO UPDATE SET + relative_path = excluded.relative_path, size = excluded.size, + modified_ns = excluded.modified_ns, state = excluded.state, + invalid_code = excluded.invalid_code, title = excluded.title, + title_key = excluded.title_key, workspace_id = excluded.workspace_id, + workspace_encoding = excluded.workspace_encoding, + workspace_units = excluded.workspace_units, + folder_display = excluded.folder_display, created_ns = excluded.created_ns, + updated_ns = excluded.updated_ns, archived = excluded.archived, + active_turn = excluded.active_turn, event_count = excluded.event_count, + schema_version = excluded.schema_version, + incomplete_tail = excluded.incomplete_tail, + fts_first = excluded.fts_first, fts_last = excluded.fts_last", + named_params! { + ":session_id": row.session_id, + ":relative_path": row.relative_path, + ":size": row.size, + ":modified_ns": row.modified_ns, + ":state": row.state, + ":invalid_code": row.invalid_code, + ":title": metadata.map(|m| m.title.as_str()), + ":title_key": metadata.map(|m| m.title_key.as_str()), + ":workspace_id": metadata.map(|m| m.workspace_id.as_str()), + ":workspace_encoding": metadata.map(|m| m.workspace_encoding), + ":workspace_units": metadata.map(|m| m.workspace_units.as_slice()), + ":folder_display": metadata.map(|m| m.folder_display.as_str()), + ":created_ns": metadata.map(|m| m.created_ns), + ":updated_ns": metadata.map(|m| m.updated_ns), + ":archived": metadata.is_some_and(|m| m.archived), + ":active_turn": metadata.and_then(|m| m.active_turn.as_deref()), + ":event_count": metadata.map_or(0, |m| m.event_count), + ":schema_version": metadata.map(|m| m.schema_version), + ":incomplete_tail": metadata.is_some_and(|m| m.incomplete_tail), + ":fts_first": first, + ":fts_last": last, + }, + )?; + Ok(()) +} diff --git a/crates/omgcode-core/src/session_index/schema.rs b/crates/omgcode-core/src/session_index/schema.rs new file mode 100644 index 0000000..c16ae55 --- /dev/null +++ b/crates/omgcode-core/src/session_index/schema.rs @@ -0,0 +1,285 @@ +//! Opening the database, its pragmas, and its numbered migrations. + +use std::fs; +use std::io; +use std::path::Path; +use std::time::Duration; + +use rusqlite::config::DbConfig; +use rusqlite::{Connection, ErrorCode, OpenFlags, TransactionBehavior}; + +use super::{APPLICATION_ID, INDEX_DIRECTORY, INDEX_FILE, IndexError}; +use crate::storage::{self, StorageError}; + +/// How long a statement waits for another connection's lock. +const BUSY_TIMEOUT: Duration = Duration::from_secs(2); + +/// Migration 1: the first schema. Append later migrations to `MIGRATIONS`; +/// never edit an applied one. The index is a cache, so a version that is not +/// in `1..=MIGRATIONS.len()` is rebuilt rather than migrated. +/// +/// Times are UTC nanoseconds since the Unix epoch so they sort numerically. +/// `workspace_units` holds `NativePath` units: raw bytes for `unix_bytes`, or +/// little-endian UTF-16 code units for `windows_utf16`. `fts_first` and +/// `fts_last` bound the `entry_text` rowids written for the journal so they can +/// be deleted by range instead of by scanning the full-text table. +pub(super) const MIGRATION_1: &str = " +CREATE TABLE journal ( + session_id TEXT NOT NULL PRIMARY KEY, + relative_path TEXT NOT NULL UNIQUE, + size INTEGER NOT NULL, + modified_ns INTEGER NOT NULL, + state TEXT NOT NULL CHECK (state IN ('ok', 'busy', 'invalid')), + invalid_code TEXT, + title TEXT, + title_key TEXT, + workspace_id TEXT, + workspace_encoding TEXT CHECK (workspace_encoding IN ('unix_bytes', 'windows_utf16')), + workspace_units BLOB, + folder_display TEXT, + created_ns INTEGER, + updated_ns INTEGER, + archived INTEGER NOT NULL DEFAULT 0 CHECK (archived IN (0, 1)), + active_turn TEXT, + event_count INTEGER NOT NULL DEFAULT 0, + schema_version INTEGER, + incomplete_tail INTEGER NOT NULL DEFAULT 0 CHECK (incomplete_tail IN (0, 1)), + fts_first INTEGER, + fts_last INTEGER +) STRICT; +CREATE INDEX journal_archived_updated ON journal (archived, updated_ns); +CREATE VIRTUAL TABLE entry_text USING fts5( + text, + session_id UNINDEXED, + seq UNINDEXED, + role UNINDEXED, + tokenize = 'unicode61 remove_diacritics 2' +); +"; + +/// Ordered migrations. Migration `n` is `MIGRATIONS[n - 1]`. +const MIGRATIONS: [&str; 1] = [MIGRATION_1]; + +/// The `user_version` after every migration has run. +const SCHEMA_VERSION: i64 = 1; +const _: () = assert!( + MIGRATIONS.len() == 1, + "update SCHEMA_VERSION with MIGRATIONS" +); + +/// What opening one database file found. +enum Opened { + Ready(Connection), + /// The file is not a usable index of this version. It is a cache, so the + /// caller deletes it and starts again. + Rebuild, +} + +/// Open or create the index database below `data_root`. +pub(super) fn open_connection(data_root: &Path) -> Result { + let directory = data_root.join(INDEX_DIRECTORY); + check_location(&directory, LocationKind::Directory)?; + storage::create_private_directories(&directory).map_err(storage_error)?; + check_location(&directory, LocationKind::Directory)?; + let database = std::path::absolute(directory.join(INDEX_FILE))?; + if database + .to_str() + .is_some_and(|text| text.starts_with("file:")) + { + // The bundled SQLite is built with URI filenames on by default. An + // absolute native path never starts this way, but refuse it anyway. + return Err(IndexError::UnsafeLocation( + "index path looks like a URI filename", + )); + } + check_location(&database, LocationKind::File)?; + match try_open(&database)? { + Opened::Ready(connection) => Ok(connection), + Opened::Rebuild => { + remove_index_files(&directory)?; + check_location(&database, LocationKind::File)?; + match try_open(&database)? { + Opened::Ready(connection) => Ok(connection), + Opened::Rebuild => Err(IndexError::InvalidIndexEntry( + "a newly created index failed validation", + )), + } + } + } +} + +#[derive(Clone, Copy)] +enum LocationKind { + Directory, + File, +} + +/// Refuse a link, a reparse point, or an object of the wrong kind. A missing +/// object is fine. A race between this check and the open is not excluded: the +/// data root must be trusted, and `SQLITE_OPEN_NOFOLLOW` covers the final +/// component on platforms that honor it. +fn check_location(path: &Path, kind: LocationKind) -> Result<(), IndexError> { + let metadata = match fs::symlink_metadata(path) { + Ok(metadata) => metadata, + Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()), + Err(error) => return Err(error.into()), + }; + if metadata.file_type().is_symlink() || is_reparse_point(&metadata) { + return Err(IndexError::UnsafeLocation( + "index location is a link or reparse point", + )); + } + let expected = match kind { + LocationKind::Directory => metadata.is_dir(), + LocationKind::File => metadata.is_file(), + }; + if expected { + Ok(()) + } else { + Err(IndexError::UnsafeLocation( + "index location is not the expected kind of file", + )) + } +} + +#[cfg(windows)] +fn is_reparse_point(metadata: &fs::Metadata) -> bool { + use std::os::windows::fs::MetadataExt; + const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0400; + metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 +} + +#[cfg(not(windows))] +fn is_reparse_point(_metadata: &fs::Metadata) -> bool { + false +} + +fn storage_error(error: StorageError) -> IndexError { + match error { + StorageError::Io(error) => IndexError::Io(error), + StorageError::Invalid(detail) => IndexError::UnsafeLocation(detail), + StorageError::Busy => IndexError::Busy, + } +} + +/// Delete only the index's own database and rollback-journal files. +fn remove_index_files(directory: &Path) -> Result<(), IndexError> { + for name in [INDEX_FILE, "index.sqlite3-journal"] { + match fs::remove_file(directory.join(name)) { + Ok(()) => {} + Err(error) if error.kind() == io::ErrorKind::NotFound => {} + Err(error) => return Err(error.into()), + } + } + Ok(()) +} + +fn is_unusable_database(error: &rusqlite::Error) -> bool { + matches!( + error, + rusqlite::Error::SqliteFailure(failure, _) + if matches!(failure.code, ErrorCode::NotADatabase | ErrorCode::DatabaseCorrupt) + ) +} + +/// Open `database` with the fixed flags and settings and validate its header. +fn try_open(database: &Path) -> Result { + // No URI flag and no shared cache. NO_MUTEX because a connection is used by + // one thread at a time. + let flags = OpenFlags::SQLITE_OPEN_READ_WRITE + | OpenFlags::SQLITE_OPEN_CREATE + | OpenFlags::SQLITE_OPEN_NOFOLLOW + | OpenFlags::SQLITE_OPEN_NO_MUTEX; + let mut connection = match Connection::open_with_flags(database, flags) { + Ok(connection) => connection, + Err(error) if is_unusable_database(&error) => return Ok(Opened::Rebuild), + Err(error) => return Err(error.into()), + }; + match prepare(&mut connection) { + Ok(true) => Ok(Opened::Ready(connection)), + Ok(false) => Ok(Opened::Rebuild), + Err(error) if is_unusable_database(&error) => Ok(Opened::Rebuild), + Err(error) => Err(error.into()), + } +} + +/// Apply connection settings, then validate or initialize the schema. Returns +/// `false` when the file is not a usable index. +fn prepare(connection: &mut Connection) -> rusqlite::Result { + connection.busy_timeout(BUSY_TIMEOUT)?; + connection.set_db_config(DbConfig::SQLITE_DBCONFIG_TRUSTED_SCHEMA, false)?; + connection.set_db_config(DbConfig::SQLITE_DBCONFIG_DEFENSIVE, true)?; + connection.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY, true)?; + connection.pragma_update(None, "secure_delete", "ON")?; + // The first statement that reads the file: a non-database fails here. + let (application_id, user_version) = read_header(connection)?; + if application_id == 0 && user_version == 0 { + if !is_empty_database(connection)? { + return Ok(false); + } + return initialize(connection); + } + if application_id != APPLICATION_ID || !(1..=SCHEMA_VERSION).contains(&user_version) { + return Ok(false); + } + if user_version < SCHEMA_VERSION { + migrate(connection, user_version)?; + } + has_expected_tables(connection) +} + +fn read_header(connection: &Connection) -> rusqlite::Result<(i32, i64)> { + let application_id = connection.pragma_query_value(None, "application_id", |row| row.get(0))?; + let user_version = connection.pragma_query_value(None, "user_version", |row| row.get(0))?; + Ok((application_id, user_version)) +} + +fn is_empty_database(connection: &Connection) -> rusqlite::Result { + let objects: i64 = + connection.query_row("SELECT count(*) FROM sqlite_schema", [], |row| row.get(0))?; + Ok(objects == 0) +} + +fn has_expected_tables(connection: &Connection) -> rusqlite::Result { + let tables: i64 = connection.query_row( + "SELECT count(*) FROM sqlite_schema WHERE type = 'table' AND name IN ('journal', 'entry_text')", + [], + |row| row.get(0), + )?; + Ok(tables == 2) +} + +/// Create the schema in one immediate transaction. Another process may have +/// initialized the file while this one waited for the lock, so re-check first. +fn initialize(connection: &mut Connection) -> rusqlite::Result { + let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?; + let (application_id, user_version) = read_header(&transaction)?; + if application_id == 0 && user_version == 0 && is_empty_database(&transaction)? { + transaction.pragma_update(None, "application_id", APPLICATION_ID)?; + migrate_in(&transaction, 0)?; + transaction.commit()?; + return has_expected_tables(connection); + } + drop(transaction); + let (application_id, user_version) = read_header(connection)?; + if application_id != APPLICATION_ID || user_version != SCHEMA_VERSION { + return Ok(false); + } + has_expected_tables(connection) +} + +/// Bring an older supported version up to date in one transaction. +fn migrate(connection: &mut Connection, from: i64) -> rusqlite::Result<()> { + let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?; + migrate_in(&transaction, from)?; + transaction.commit() +} + +/// Run every migration after version `from` and record the new version. +fn migrate_in(connection: &Connection, from: i64) -> rusqlite::Result<()> { + let skip = usize::try_from(from).unwrap_or(MIGRATIONS.len()); + for migration in MIGRATIONS.iter().skip(skip) { + connection.execute_batch(migration)?; + } + connection.pragma_update(None, "user_version", SCHEMA_VERSION) +} diff --git a/crates/omgcode-core/src/session_index/snippet.rs b/crates/omgcode-core/src/session_index/snippet.rs new file mode 100644 index 0000000..787a4a9 --- /dev/null +++ b/crates/omgcode-core/src/session_index/snippet.rs @@ -0,0 +1,152 @@ +//! Safe construction of FTS5 queries and parsing of marked snippets. +//! +//! Caller text is never passed to FTS5 as query syntax. This module quotes each +//! term itself, and it reserves two private-use code points as snippet markers +//! so transcript text cannot forge a match highlight. + +use super::IndexError; +use super::query::SnippetPart; + +/// Marks the start of a matched run in an FTS5 snippet. +pub(super) const MARK_START: char = '\u{e000}'; +/// Marks the end of a matched run in an FTS5 snippet. +pub(super) const MARK_END: char = '\u{e001}'; + +/// The most characters a search text may have. +pub(super) const MAX_QUERY_CHARS: usize = 256; +/// The most whitespace-separated terms a search text may have. +pub(super) const MAX_QUERY_TERMS: usize = 16; + +/// Replace the marker code points so indexed text can never contain them. +pub(super) fn sanitize_text(text: &str) -> String { + text.replace([MARK_START, MARK_END], "\u{fffd}") +} + +/// Build the FTS5 `MATCH` expression for the caller's text. +/// +/// Each whitespace-separated term becomes a double-quoted FTS5 string with +/// embedded quotes doubled, so operators, column filters, and `NEAR` are plain +/// text. Terms join with spaces (implicit AND). The last term gets a trailing +/// `*` for prefix matching while typing. +/// +/// # Errors +/// +/// `InvalidQuery` for text over 256 characters, with no terms, with more than +/// 16 terms, or containing NUL. +pub(super) fn match_expression(text: &str) -> Result { + if text.chars().count() > MAX_QUERY_CHARS { + return Err(IndexError::InvalidQuery("search text is too long")); + } + if text.contains('\0') { + return Err(IndexError::InvalidQuery("search text contains NUL")); + } + let terms: Vec<&str> = text.split_whitespace().collect(); + if terms.is_empty() { + return Err(IndexError::InvalidQuery("search text has no terms")); + } + if terms.len() > MAX_QUERY_TERMS { + return Err(IndexError::InvalidQuery("search text has too many terms")); + } + let mut expression = String::new(); + for (index, term) in terms.iter().enumerate() { + if index > 0 { + expression.push(' '); + } + expression.push('"'); + expression.push_str(&term.replace('"', "\"\"")); + expression.push('"'); + if index + 1 == terms.len() { + expression.push('*'); + } + } + Ok(expression) +} + +/// Split an FTS5 snippet into plain and matched parts. Marker code points never +/// appear in the output, and empty parts are omitted. +pub(super) fn parse_snippet(marked: &str) -> Vec { + let mut parts = Vec::new(); + let mut current = String::new(); + let mut matched = false; + for character in marked.chars() { + let next = match character { + MARK_START => Some(true), + MARK_END => Some(false), + _ => None, + }; + match next { + Some(state) => { + flush(&mut parts, &mut current, matched); + matched = state; + } + None => current.push(character), + } + } + flush(&mut parts, &mut current, matched); + parts +} + +fn flush(parts: &mut Vec, current: &mut String, matched: bool) { + if !current.is_empty() { + parts.push(SnippetPart { + text: std::mem::take(current), + matched, + }); + } +} + +#[cfg(test)] +mod tests { + use super::{match_expression, parse_snippet, sanitize_text}; + use crate::session_index::IndexError; + + #[test] + fn terms_are_quoted_and_only_the_last_gets_a_prefix_star() { + assert_eq!( + match_expression(" alpha be\"ta gam ").expect("expression"), + "\"alpha\" \"be\"\"ta\" \"gam\"*" + ); + assert_eq!( + match_expression("a OR b").expect("expression"), + "\"a\" \"OR\" \"b\"*" + ); + assert_eq!(match_expression("\"").expect("expression"), "\"\"\"\"*"); + } + + #[test] + fn unusable_text_is_rejected_with_a_typed_error() { + for text in ["", " \t\n", &"x".repeat(257), "a\0b", &"a ".repeat(17)] { + assert!( + matches!(match_expression(text), Err(IndexError::InvalidQuery(_))), + "{text:?}" + ); + } + assert!(match_expression(&"x".repeat(256)).is_ok()); + assert!(match_expression(&"a ".repeat(16)).is_ok()); + } + + #[test] + fn snippet_parts_alternate_and_never_contain_markers() { + let parts = parse_snippet("one \u{e000}two\u{e001} three \u{e000}four\u{e001}"); + let shown: Vec<(&str, bool)> = parts + .iter() + .map(|part| (part.text.as_str(), part.matched)) + .collect(); + assert_eq!( + shown, + [ + ("one ", false), + ("two", true), + (" three ", false), + ("four", true) + ] + ); + assert!(parse_snippet("").is_empty()); + assert!(parse_snippet("\u{e000}\u{e001}").is_empty()); + } + + #[test] + fn sanitizing_removes_both_markers() { + assert_eq!(sanitize_text("a\u{e000}b\u{e001}c"), "a\u{fffd}b\u{fffd}c"); + } +} diff --git a/crates/omgcode-core/src/storage.rs b/crates/omgcode-core/src/storage.rs index 014b73d..f1a3ef2 100644 --- a/crates/omgcode-core/src/storage.rs +++ b/crates/omgcode-core/src/storage.rs @@ -69,6 +69,28 @@ pub(crate) fn open_locked_file(path: &Path, create_new: bool) -> Result Result { + let mut options = OpenOptions::new(); + options.read(true); + configure_open_options(&mut options); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.custom_flags(libc::O_NOFOLLOW); + } + let file = options.open(path)?; + validate_open_file(path, &file)?; + Ok(file) +} + /// Sync the containing directory entry on Unix. Rust does not provide a /// qualified safe directory-handle sync contract on Windows, so this is a /// documented no-op there. The caller chooses the parent directory to sync. diff --git a/crates/omgcode-core/src/turn.rs b/crates/omgcode-core/src/turn.rs index 6eb23c7..65655ec 100644 --- a/crates/omgcode-core/src/turn.rs +++ b/crates/omgcode-core/src/turn.rs @@ -26,7 +26,7 @@ use crate::control::{ ControlStore, now_unix, valid_profile_name, }; use crate::files::{FileBroker, FileError, FileListing, FileTarget}; -use crate::policy::{DecisionReason, PermissionSet, WorkspaceId}; +use crate::policy::{DecisionReason, PermissionSet, SessionBinding, WorkspaceId}; use crate::prompt::{ self, BASE_PROMPT, BASE_PROMPT_VERSION, ConversationMessage, ConversationRole, ModelMessage, PromptError, @@ -37,6 +37,13 @@ use crate::session::{ SessionJournal, ToolOutcome, sha256_hex, }; +mod review; + +pub use review::{ + ApprovalReview, DisplaySafeStream, ReviewCandidate, ReviewError, ReviewField, ReviewedRequest, + StaleReason, display_safe, display_safe_line, review_approval, +}; + /// Revision of the tool schemas advertised by this runtime. /// /// Version `0.3.0` adds `workspace_edit_file`. Journals record the version @@ -107,6 +114,11 @@ pub struct ToolModelRequest<'a> { pub messages: &'a [ModelMessage], /// The exact tool definitions advertised for this request. pub tools: &'a [ToolDefinition], + /// The turn's cancel token. A model that can interrupt its request watches + /// it and returns [`ModelError::Cancelled`] once it is set; a model that + /// cannot interrupt may ignore it, and the runtime still checks it between + /// requests. + pub cancel: &'a CancelToken, } /// One model response, with tool calls still untrusted and unparsed. @@ -133,6 +145,10 @@ pub enum ModelError { /// The assembled request exceeds a supported bound. #[error("model request exceeds the supported limit")] RequestTooLarge, + /// The request was interrupted because the turn's cancel token was set. + /// The runtime ends the turn as cancelled only when the token is set. + #[error("model request was cancelled")] + Cancelled, } /// One tool definition exposed to a model interface. @@ -1026,8 +1042,14 @@ impl<'a> TurnRuntime<'a> { let reply = match model.complete(ToolModelRequest { messages: &messages, tools: &tools, + cancel: &self.cancel, }) { Ok(reply) => reply, + // Only a set token makes this a cancellation. A model that + // reports it otherwise is faulty and is recorded as such. + Err(ModelError::Cancelled) if self.cancel.is_cancelled() => { + return Err(self.abort_for(request_id, TurnError::Cancelled)); + } Err(error) => { return Err(self.abort(request_id, "model_error", error.into())); } @@ -1496,36 +1518,21 @@ impl WorkspaceToolDispatcher<'_> { ); } }; - let target = FileTarget { - session_id: self.session_id, - workspace_id: self.workspace_id, - path: PathBuf::from(path), - }; - let current = - match FileBroker::new(&mut control).read(&binding, &target, MAX_EDIT_FILE_BYTES) { - Ok(read) => read.bytes, - Err(error) => return classify_edit_error(call, error), - }; - let Ok(text) = std::str::from_utf8(¤t) else { - return ToolExecution::recoverable("non_utf8", call); - }; - let start = match unique_match(text, old_text) { - TextMatch::None => return ToolExecution::recoverable("edit_no_match", call), - TextMatch::Many => return ToolExecution::recoverable("edit_ambiguous", call), - TextMatch::Once(start) => start, + let target = edit_target(self.session_id, self.workspace_id, path); + let current = match read_edit_source(&mut control, &binding, &target) { + Ok(bytes) => bytes, + Err(error) => return classify_edit_error(call, error), }; - if old_text == new_text { - return ToolExecution::recoverable("edit_unchanged", call); - } - let Some(proposed) = replace_once(text, start, old_text.len(), new_text) else { - return ToolExecution::recoverable("edit_too_large", call); + let prepared = match prepare_edit(path, old_text, new_text, ¤t) { + Ok(prepared) => prepared, + Err(code) => return ToolExecution::recoverable(code, call), }; - let (Some(current_artifact), Some(proposed_artifact)) = - (artifact(¤t), artifact(proposed.as_bytes())) - else { - return ToolExecution::recoverable("edit_too_large", call); - }; - let normalized = normalized_path(path); + let PreparedEdit { + normalized, + current_artifact, + proposed, + proposed_artifact, + } = prepared; let digest = edit_digest( self.session_id, self.workspace_id, @@ -1657,17 +1664,8 @@ impl WorkspaceToolDispatcher<'_> { else { return ToolExecution::recoverable("invalid_command", call); }; - let request = crate::run::RunRequest { - workspace_id: self.workspace_id, - profile: profile.clone(), - program: PathBuf::from(program), - arguments: arguments.iter().map(OsString::from).collect(), - working_directory: cwd.as_ref().map(PathBuf::from), - host_services: false, - network: false, - model_tools: true, - limits: command_limits(), - }; + let request = + command_run_request(self.workspace_id, profile, program, arguments, cwd.as_ref()); let digest = command_digest(self.session_id, &request); let now = now_unix(); let revision = control.revision(); @@ -1813,6 +1811,32 @@ impl WorkspaceToolDispatcher<'_> { } } +/// Build the run request for one model command call. +/// +/// This is the only place that turns parsed command arguments into a +/// [`crate::run::RunRequest`]. Dispatch runs and digests the result, and +/// approval review recomputes the same digest from it, so the two cannot +/// diverge. Network and host services are always off and the limits are fixed. +fn command_run_request( + workspace_id: WorkspaceId, + profile: &str, + program: &str, + arguments: &[String], + cwd: Option<&String>, +) -> crate::run::RunRequest { + crate::run::RunRequest { + workspace_id, + profile: profile.to_owned(), + program: PathBuf::from(program), + arguments: arguments.iter().map(OsString::from).collect(), + working_directory: cwd.map(PathBuf::from), + host_services: false, + network: false, + model_tools: true, + limits: command_limits(), + } +} + /// Fixed limits for one model command run. fn command_limits() -> crate::run::ProcessLimits { crate::run::ProcessLimits { @@ -1943,6 +1967,75 @@ fn replace_once(text: &str, start: usize, length: usize, replacement: &str) -> O Some(proposed) } +/// One exact edit prepared against the live bytes of its target file. +struct PreparedEdit { + /// The `/`-joined path used in the digest and the summary. + normalized: String, + /// Size and SHA-256 of the live current bytes. + current_artifact: RecordedArtifact, + /// The complete proposed file text. + proposed: String, + /// Size and SHA-256 of the proposed bytes. + proposed_artifact: RecordedArtifact, +} + +/// The broker target for one model edit path. +fn edit_target(session_id: Uuid, workspace_id: WorkspaceId, path: &str) -> FileTarget { + FileTarget { + session_id, + workspace_id, + path: PathBuf::from(path), + } +} + +/// Read the live current bytes of an edit target through the audited broker. +/// +/// This is the only edit read. The broker checks the live `read` permission +/// and appends its own audit record. Dispatch and approval review both call it. +fn read_edit_source( + control: &mut ControlStore, + binding: &SessionBinding, + target: &FileTarget, +) -> Result, FileError> { + FileBroker::new(control) + .read(binding, target, MAX_EDIT_FILE_BYTES) + .map(|read| read.bytes) +} + +/// Turn parsed edit arguments and the live current bytes into the exact edit. +/// +/// This is the only place that applies the unique-match rule, the no-op check, +/// and the size bounds. An error is the recoverable tool code that dispatch +/// reports; approval review treats any of them as "not this request". +fn prepare_edit( + path: &str, + old_text: &str, + new_text: &str, + current: &[u8], +) -> Result { + let text = std::str::from_utf8(current).map_err(|_| "non_utf8")?; + let start = match unique_match(text, old_text) { + TextMatch::None => return Err("edit_no_match"), + TextMatch::Many => return Err("edit_ambiguous"), + TextMatch::Once(start) => start, + }; + if old_text == new_text { + return Err("edit_unchanged"); + } + let proposed = replace_once(text, start, old_text.len(), new_text).ok_or("edit_too_large")?; + let (Some(current_artifact), Some(proposed_artifact)) = + (artifact(current), artifact(proposed.as_bytes())) + else { + return Err("edit_too_large"); + }; + Ok(PreparedEdit { + normalized: normalized_path(path), + current_artifact, + proposed, + proposed_artifact, + }) +} + /// The labelled report of an applied edit. It never contains file text. fn edit_success_message( call: &ToolCall, @@ -2047,14 +2140,38 @@ fn edit_summary(path: &str, current: &RecordedArtifact, proposed: &RecordedArtif /// Invisible, line-breaking, or bidirectional formatting characters that can /// hide or reorder displayed text. +/// +/// Categories: bidirectional and zero-width marks (U+061C, U+200B to U+200F, +/// U+2028 to U+202E, U+2060 to U+2069, U+FEFF); characters that render as +/// nothing or merge into a neighbour (soft hyphen U+00AD, combining grapheme +/// joiner U+034F, Hangul fillers U+115F, U+1160, U+3164, U+FFA0, Khmer +/// invisible vowels U+17B4 to U+17B5, Mongolian selectors and separator +/// U+180B to U+180F); variation selectors (U+FE00 to U+FE0F); interlinear +/// annotation anchors and the object replacement character (U+FFF9 to +/// U+FFFC); musical formatting controls (U+1D173 to U+1D17A); and the tag +/// characters with the variation selectors supplement (U+E0000 to U+E0FFF). +/// +/// This list does not cover look-alike letters, mixed scripts, or ordinary +/// spaces. const fn is_hidden_format(character: char) -> bool { matches!( character, - '\u{061c}' + '\u{00ad}' + | '\u{034f}' + | '\u{061c}' + | '\u{115f}'..='\u{1160}' + | '\u{17b4}'..='\u{17b5}' + | '\u{180b}'..='\u{180f}' | '\u{200b}'..='\u{200f}' | '\u{2028}'..='\u{202e}' | '\u{2060}'..='\u{2069}' + | '\u{3164}' + | '\u{fe00}'..='\u{fe0f}' | '\u{feff}' + | '\u{ffa0}' + | '\u{fff9}'..='\u{fffc}' + | '\u{1d173}'..='\u{1d17a}' + | '\u{e0000}'..='\u{e0fff}' ) } diff --git a/crates/omgcode-core/src/turn/review.rs b/crates/omgcode-core/src/turn/review.rs new file mode 100644 index 0000000..60e8163 --- /dev/null +++ b/crates/omgcode-core/src/turn/review.rs @@ -0,0 +1,846 @@ +//! Read-only review of the recorded request an approval is bound to (ADR-0046). +//! +//! An approval record stores a digest and a short summary, never the tool +//! arguments. A reviewer that shows arguments next to an Approve control must +//! show only arguments that reproduce that digest. This module is the one +//! place that does the matching, using the same argument parser, request +//! construction, edit preparation, and digest functions as the tool dispatcher. +//! It grants nothing: it never requests, decides, or consumes an approval. + +use std::collections::HashSet; +use std::path::Path; + +use serde::Serialize; +use serde_json::json; +use thiserror::Error; + +use super::{ + PreparedEdit, ToolArguments, ToolCall, ToolName, command_digest, command_run_request, + edit_digest, edit_target, is_hidden_format, prepare_edit, read_edit_source, +}; +use crate::control::{ApprovalOperation, ApprovalStatus, ControlError, ControlStore}; +use crate::file_audit::FileAuditError; +use crate::files::FileError; +use crate::replacement::CheckpointError; + +/// Most distinct edit requests evaluated for one review. +/// +/// Each evaluation reads a workspace file through the audited broker and +/// writes one audit record, so the work and the audit growth are bounded. +const MAX_REVIEW_EDIT_CANDIDATES: usize = 8; + +/// One recorded model tool request offered as a candidate for review. +/// +/// Both fields are untrusted model data, usually copied from the session +/// journal's recorded tool requests. Offering a candidate grants nothing. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ReviewCandidate<'a> { + /// The requested tool name. + pub tool: &'a str, + /// The exact arguments JSON string that was recorded. + pub arguments: &'a str, +} + +/// What core found for one approval record. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum ApprovalReview { + /// A recorded request reproduces the approval's digest under the current + /// settings, so a retry of it would consume the approval. + Exact(ReviewedRequest), + /// Candidates of the right tool exist, or the settings revision changed, + /// but nothing reproduces the digest. The approval cannot be consumed by + /// any request core can show. + Stale(StaleReason), + /// No recorded request of the right tool exists, the operation has no + /// reviewable tool, or the settings store or workspace is absent. + NotFound, +} + +/// Why a review is stale. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum StaleReason { + /// The settings revision changed after the approval was requested. The + /// record can never be consumed. + SettingsChanged, + /// No candidate reproduces the digest, for example because the target + /// file changed after the request. + NoMatchingRequest, +} + +/// The request an approval is bound to, rendered for a person to read. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ReviewedRequest { + /// The wire name of the tool that makes the request. + pub tool: String, + /// Labelled values, each already passed through [`display_safe`]. + pub fields: Vec, +} + +/// One labelled, display-safe value of a reviewed request. +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ReviewField { + /// A fixed English label. + pub label: &'static str, + /// Display-safe text. It may contain `\n` only when `multiline` is true. + pub value: String, + /// Whether the value is meant to be shown as several lines. + pub multiline: bool, +} + +/// A review that could not be completed. Messages hold no file contents, +/// request text, or absolute paths; the codes are fixed strings. +#[derive(Debug, Error)] +pub enum ReviewError { + /// Another process holds the settings store or the file-operation store. + /// Retry later; nothing was changed. + #[error("the settings or file-operation store is in use by another process")] + Busy, + /// The settings store could not be read. + #[error("the settings store could not be read ({0})")] + Control(&'static str), + /// A review file read failed in a way that is not a plain non-match. + #[error("the review file read could not complete ({0})")] + Files(&'static str), +} + +/// Find the recorded request that an approval record is bound to. +/// +/// `candidates` are the recorded tool requests of the approval's session in +/// journal order, oldest first. They are untrusted; the result depends only on +/// whether a candidate reproduces `approval.request_sha256`, so a candidate +/// from another session or workspace can match only by reproducing the digest +/// of the approval's own session and workspace. +/// +/// 1. Only [`ApprovalOperation::Command`] and [`ApprovalOperation::FileWrite`] +/// are reviewable. Others give [`ApprovalReview::NotFound`]. +/// 2. The existing settings store is opened once, without retries. A missing +/// store or unregistered workspace gives `NotFound`, a busy store gives +/// [`ReviewError::Busy`], and a settings revision that differs from the +/// approval's gives `Stale(SettingsChanged)`. +/// 3. Only candidates whose tool is the operation's tool are considered; with +/// none, the result is `NotFound`. +/// 4. They are tried newest first, each parsed by [`ToolCall::parse`]. A +/// candidate that fails to parse is skipped. A command candidate is built +/// into a run request by the dispatcher's own constructor. An edit candidate +/// is read live through the audited broker under the live `read` +/// requirement and prepared by the dispatcher's own edit preparation. At +/// most eight distinct edit candidates (distinct by exact arguments) are +/// evaluated, newest first. A candidate whose preparation fails for a +/// recoverable reason (missing file, no match, ambiguous match, too large, +/// read denied) is not a match. +/// 5. If none reproduces the digest, the result is `Stale(NoMatchingRequest)`. +/// +/// The fields of an `Exact` result come from the request that reproduced the +/// digest, not from the newest candidate and not from re-reading the JSON. +/// +/// # Side effects +/// +/// None on approvals. The function never requests, decides, or consumes a +/// record, never applies an edit, never creates a checkpoint, and never starts +/// a process. For an edit candidate it reads one workspace file through the +/// broker, which appends one audit record per read; nothing else is written. +/// +/// # Locking +/// +/// The settings store is opened once, the same way the dispatcher opens it. A +/// command review releases it before comparing candidates. An edit review +/// holds it while it reads, as dispatch does, because the broker needs the +/// writer; the broker takes its own subordinate stores one at a time under the +/// documented order (settings, then file audit). The settings store is never +/// held across anything but those bounded reads and is dropped before return. +/// A store held elsewhere gives [`ReviewError::Busy`]; the function does not +/// retry. +/// +/// # Not validated +/// +/// Whether `approval` is still pending, approved, or unexpired. The caller +/// shows state separately. A consumed or expired record can still be `Exact`. +/// An `Exact` result is a snapshot: a file can change before an approval is +/// decided, and core still checks the binding when the call is consumed. +pub fn review_approval( + data_directory: &Path, + approval: &ApprovalStatus, + candidates: &[ReviewCandidate<'_>], +) -> Result { + let tool = match approval.operation { + ApprovalOperation::Command => ToolName::Command, + ApprovalOperation::FileWrite => ToolName::EditFile, + ApprovalOperation::GitPush | ApprovalOperation::Network => { + return Ok(ApprovalReview::NotFound); + } + }; + let control = match ControlStore::open_existing(data_directory) { + Ok(Some(control)) => control, + Ok(None) => return Ok(ApprovalReview::NotFound), + Err(ControlError::Busy) => return Err(ReviewError::Busy), + Err(error) => return Err(ReviewError::Control(control_code(&error))), + }; + if approval.settings_revision != control.revision() { + return Ok(ApprovalReview::Stale(StaleReason::SettingsChanged)); + } + if control.workspace(approval.workspace_id).is_none() { + return Ok(ApprovalReview::NotFound); + } + let mut relevant = candidates + .iter() + .rev() + .filter(|candidate| candidate.tool == tool.as_str()) + .peekable(); + if relevant.peek().is_none() { + return Ok(ApprovalReview::NotFound); + } + let found = match tool { + ToolName::Command => { + drop(control); + review_command(approval, relevant) + } + _ => review_edit(control, approval, relevant)?, + }; + Ok(found.map_or( + ApprovalReview::Stale(StaleReason::NoMatchingRequest), + ApprovalReview::Exact, + )) +} + +/// Parse a candidate with the dispatcher's strict parser. +/// +/// The parser takes a whole provider call, so the candidate is wrapped in one +/// with a fixed identifier. Everything else it checks applies unchanged. +fn parse_candidate(candidate: &ReviewCandidate<'_>, tool: ToolName) -> Option { + let value = json!({ + "id": "review", + "type": "function", + "function": {"name": candidate.tool, "arguments": candidate.arguments}, + }); + ToolCall::parse(&value) + .ok() + .filter(|call| call.name == tool) +} + +/// Find the newest command candidate that reproduces the approval's digest. +fn review_command<'a>( + approval: &ApprovalStatus, + candidates: impl Iterator>, +) -> Option { + for candidate in candidates { + let Some(call) = parse_candidate(candidate, ToolName::Command) else { + continue; + }; + let ToolArguments::Command { + profile, + program, + arguments, + cwd, + } = &call.arguments + else { + continue; + }; + let request = command_run_request( + approval.workspace_id, + profile, + program, + arguments, + cwd.as_ref(), + ); + if command_digest(approval.session_id, &request) == approval.request_sha256 { + return Some(ReviewedRequest { + tool: ToolName::Command.as_str().to_owned(), + fields: command_fields(&request), + }); + } + } + None +} + +/// Find the newest edit candidate that reproduces the approval's digest. +/// +/// Evaluates at most [`MAX_REVIEW_EDIT_CANDIDATES`] distinct candidates. +fn review_edit<'a>( + mut control: ControlStore, + approval: &ApprovalStatus, + candidates: impl Iterator>, +) -> Result, ReviewError> { + let binding = control + .bind_session(approval.session_id, approval.workspace_id) + .map_err(|error| match error { + ControlError::Busy => ReviewError::Busy, + other => ReviewError::Control(control_code(&other)), + })?; + let mut seen = HashSet::new(); + let mut evaluated = 0usize; + for candidate in candidates { + let Some(call) = parse_candidate(candidate, ToolName::EditFile) else { + continue; + }; + let ToolArguments::EditFile { + path, + old_text, + new_text, + } = &call.arguments + else { + continue; + }; + if !seen.insert(candidate.arguments) { + continue; + } + if evaluated == MAX_REVIEW_EDIT_CANDIDATES { + break; + } + evaluated += 1; + let target = edit_target(approval.session_id, approval.workspace_id, path); + let current = match read_edit_source(&mut control, &binding, &target) { + Ok(bytes) => bytes, + Err(error) => { + classify_review_read(error)?; + continue; + } + }; + let Ok(prepared) = prepare_edit(path, old_text, new_text, ¤t) else { + continue; + }; + let digest = edit_digest( + approval.session_id, + approval.workspace_id, + &prepared.normalized, + &prepared.current_artifact, + &prepared.proposed_artifact, + ); + if digest == approval.request_sha256 { + return Ok(Some(ReviewedRequest { + tool: ToolName::EditFile.as_str().to_owned(), + fields: edit_fields(&prepared, old_text, new_text), + })); + } + } + Ok(None) +} + +/// Decide whether a failed review read is a plain non-match (`Ok`) or leaves +/// the review unable to continue (`Err`). +/// +/// Permission denial, a missing or unsuitable file, and a file over the edit +/// bound mean this candidate cannot be what the approval is bound to. A busy +/// store, an unresolved file operation, or an audit failure mean core could +/// not look, so claiming the approval is stale would be wrong. +fn classify_review_read(error: FileError) -> Result<(), ReviewError> { + match error { + FileError::Denied(_) + | FileError::Native(_) + | FileError::InvalidPath(_) + | FileError::Invalid(_) => Ok(()), + FileError::Control(ControlError::Busy) | FileError::Checkpoint(CheckpointError::Busy) => { + Err(ReviewError::Busy) + } + FileError::Control(other) => Err(ReviewError::Control(control_code(&other))), + FileError::Audit(source) => { + if matches!( + source.downcast_ref::(), + Some(FileAuditError::Busy) + ) { + Err(ReviewError::Busy) + } else { + Err(ReviewError::Files("audit_failed")) + } + } + FileError::Pending { .. } => Err(ReviewError::Files("operation_pending")), + _ => Err(ReviewError::Files("read_failed")), + } +} + +/// A fixed, content-free code for a settings-store failure. +const fn control_code(error: &ControlError) -> &'static str { + match error { + ControlError::Io(_) => "io", + ControlError::Invalid(_) => "invalid", + ControlError::Busy => "busy", + ControlError::UnknownWorkspace => "unknown_workspace", + ControlError::DirectoryChanged => "directory_changed", + _ => "unavailable", + } +} + +/// Command fields, taken from the constructed run request so the display +/// cannot diverge from what would run. +fn command_fields(request: &crate::run::RunRequest) -> Vec { + let arguments = if request.arguments.is_empty() { + String::from("(none)") + } else { + request + .arguments + .iter() + .enumerate() + .map(|(index, argument)| { + format!( + "{}: {}", + index + 1, + display_safe_line(&argument.to_string_lossy()) + ) + }) + .collect::>() + .join("\n") + }; + let directory = request.working_directory.as_ref().map_or_else( + || String::from("(workspace root)"), + |directory| display_safe_line(&directory.to_string_lossy()), + ); + let limits = &request.limits; + let output = if limits.max_stdout_bytes == limits.max_stderr_bytes { + format!("{} bytes per stream", limits.max_stdout_bytes) + } else { + format!( + "stdout {} bytes, stderr {} bytes", + limits.max_stdout_bytes, limits.max_stderr_bytes + ) + }; + vec![ + single("Profile", display_safe_line(&request.profile)), + single( + "Program", + display_safe_line(&request.program.to_string_lossy()), + ), + ReviewField { + label: "Arguments", + value: arguments, + multiline: true, + }, + single("Working directory", directory), + single("Network", on_off(request.network)), + single("Host services", on_off(request.host_services)), + single( + "Time limit", + format!("{} seconds", limits.deadline.as_secs()), + ), + single("Output limit", output), + ] +} + +/// Edit fields. The path, sizes, and digests come from the prepared edit and +/// the old and new text from the parsed arguments that produced it. +fn edit_fields(prepared: &PreparedEdit, old_text: &str, new_text: &str) -> Vec { + vec![ + single("File", display_safe_line(&prepared.normalized)), + ReviewField { + label: "Old text", + value: display_safe(old_text), + multiline: true, + }, + ReviewField { + label: "New text", + value: display_safe(new_text), + multiline: true, + }, + single( + "Size", + format!( + "{} -> {} bytes", + prepared.current_artifact.bytes, prepared.proposed_artifact.bytes + ), + ), + single("Current SHA-256", prepared.current_artifact.sha256.clone()), + single( + "Proposed SHA-256", + prepared.proposed_artifact.sha256.clone(), + ), + ] +} + +fn single(label: &'static str, value: String) -> ReviewField { + ReviewField { + label, + value, + multiline: false, + } +} + +fn on_off(enabled: bool) -> String { + String::from(if enabled { "on" } else { "off" }) +} + +/// Replace characters that can hide or reorder displayed text with visible +/// escapes, keeping line feeds so multi-line text stays readable. +/// +/// Every `char::is_control` character except `\n` (so `\r`, `\t`, NUL, and +/// C1 controls) and every invisible or bidirectional formatting character that +/// the approval summaries already replace becomes `\u{XXXX}`: lowercase hex, at +/// least four digits. The ranges are listed on `is_hidden_format`; they include +/// bidirectional and zero-width marks, soft hyphen, fillers, variation +/// selectors, interlinear anchors, and tag characters. Variation selectors are +/// shown on purpose, so an emoji followed by U+FE0F displays the escape. +/// +/// Limits: this does not detect look-alike letters (homoglyphs) or mixed +/// scripts, and it leaves ordinary trailing spaces as typed. +/// +/// A backslash that begins the two characters `u{` would let text forge an +/// escape, so it becomes `\u{5c}`. A backslash directly before a character that +/// is itself escaped is escaped the same way, so a forged and a real escape +/// stay distinguishable. The output holds no control or hidden character other +/// than `\n`. The result is for display only; it is not reversible text and +/// not an authorization key. +pub fn display_safe(text: &str) -> String { + escape_text(text, true) +} + +/// Like [`display_safe`], but `\n` is escaped too, so the result is one line. +/// +/// Use it for values that must not be able to start a new displayed line, such +/// as a command argument that could imitate the next numbered argument. +pub fn display_safe_line(text: &str) -> String { + escape_text(text, false) +} + +/// A streaming form of [`display_safe`] for text that arrives in pieces. +/// +/// For any split of a string into pieces, the concatenation of every +/// [`push`](Self::push) result followed by [`finish`](Self::finish) equals +/// `display_safe` of the whole string. A backslash is judged by the characters +/// after it, so `push` holds back a trailing run of backslashes, and a trailing +/// run followed by one `u`, until more text arrives; `finish` escapes that tail +/// as the end of the text. The held tail is only backslashes and one `u`, so a +/// caller that never finishes loses at most that tail. +#[derive(Debug, Default)] +pub struct DisplaySafeStream { + held: String, +} + +impl DisplaySafeStream { + /// Start an empty stream. + pub const fn new() -> Self { + Self { + held: String::new(), + } + } + + /// Accept one piece and return the display-safe text that is now decided. + pub fn push(&mut self, text: &str) -> String { + self.held.push_str(text); + // Backslashes then `u` at the end could still become `\u{`; backslashes + // alone could still precede a character that is escaped. Everything + // before them depends on no later text. + let without_u = match self.held.strip_suffix('u') { + Some(rest) if rest.ends_with('\\') => rest, + _ => self.held.as_str(), + }; + let decided = without_u.trim_end_matches('\\').len(); + let tail = self.held.split_off(decided); + let output = escape_text(&self.held, true); + self.held = tail; + output + } + + /// End the text and return the escaped held tail. + pub fn finish(self) -> String { + escape_text(&self.held, true) + } +} + +fn escape_text(text: &str, keep_newline: bool) -> String { + let characters: Vec = text.chars().collect(); + let mut escaped = vec![false; characters.len()]; + // Right to left, so a backslash can see whether its successor is escaped. + for index in (0..characters.len()).rev() { + let character = characters[index]; + escaped[index] = if character == '\\' { + let forges = + characters.get(index + 1) == Some(&'u') && characters.get(index + 2) == Some(&'{'); + forges || escaped.get(index + 1).copied().unwrap_or(false) + } else { + character.is_control() && !(keep_newline && character == '\n') + || is_hidden_format(character) + }; + } + let mut output = String::with_capacity(text.len()); + for (character, escape) in characters.into_iter().zip(escaped) { + if !escape { + output.push(character); + } else if character == '\\' { + output.push_str("\\u{5c}"); + } else { + output.push_str(&format!("\\u{{{:04x}}}", u32::from(character))); + } + } + output +} + +#[cfg(test)] +mod tests { + use super::{DisplaySafeStream, display_safe, display_safe_line}; + + /// Output must hold no control or hidden character except `\n`. + fn assert_clean(output: &str) { + assert!( + output + .chars() + .all(|c| c == '\n' || (!c.is_control() && !super::is_hidden_format(c))), + "unsafe character in {output:?}" + ); + } + + #[test] + fn plain_text_and_line_feeds_pass_through_unchanged() { + assert_eq!( + display_safe("fn main() {\n é 日本 😀\n}"), + "fn main() {\n é 日本 😀\n}" + ); + assert_eq!(display_safe(""), ""); + } + + #[test] + fn controls_other_than_line_feed_become_visible_escapes() { + assert_eq!( + display_safe("a\rb\tc\0d"), + "a\\u{000d}b\\u{0009}c\\u{0000}d" + ); + assert_eq!( + display_safe("\u{7f}\u{85}\u{9f}"), + "\\u{007f}\\u{0085}\\u{009f}" + ); + assert_eq!(display_safe("\u{1b}[31m"), "\\u{001b}[31m"); + } + + #[test] + fn every_hidden_format_range_boundary_is_escaped_and_neighbours_are_not() { + let hidden = [ + '\u{061c}', '\u{200b}', '\u{200f}', '\u{2028}', '\u{202e}', '\u{2060}', '\u{2069}', + '\u{feff}', + ]; + for character in hidden { + let text = format!("a{character}b"); + let escaped = display_safe(&text); + assert_eq!( + escaped, + format!("a\\u{{{:04x}}}b", u32::from(character)), + "{character:?}" + ); + assert_clean(&escaped); + } + for character in [ + '\u{061b}', '\u{061d}', '\u{200a}', '\u{2010}', '\u{2027}', '\u{202f}', '\u{205f}', + '\u{206a}', '\u{fefe}', '\u{ff00}', + ] { + let text = format!("a{character}b"); + assert_eq!(display_safe(&text), text, "{character:?} must pass"); + } + // Every member of each range, not only its ends. + for code in (0x200b..=0x200f) + .chain(0x2028..=0x202e) + .chain(0x2060..=0x2069) + { + let character = char::from_u32(code).expect("scalar"); + assert_eq!( + display_safe(&character.to_string()), + format!("\\u{{{code:04x}}}") + ); + } + } + + /// The documented inclusive ranges, written out independently of the + /// function under test. + const HIDDEN_RANGES: [(u32, u32); 16] = [ + (0x00ad, 0x00ad), + (0x034f, 0x034f), + (0x061c, 0x061c), + (0x115f, 0x1160), + (0x17b4, 0x17b5), + (0x180b, 0x180f), + (0x200b, 0x200f), + (0x2028, 0x202e), + (0x2060, 0x2069), + (0x3164, 0x3164), + (0xfe00, 0xfe0f), + (0xfeff, 0xfeff), + (0xffa0, 0xffa0), + (0xfff9, 0xfffc), + (0x1d173, 0x1d17a), + (0xe0000, 0xe0fff), + ]; + + fn listed(code: u32) -> bool { + HIDDEN_RANGES + .iter() + .any(|&(first, last)| (first..=last).contains(&code)) + } + + #[test] + fn every_range_boundary_is_hidden_and_the_code_points_outside_are_not() { + for (first, last) in HIDDEN_RANGES { + for code in [first, last] { + let character = char::from_u32(code).expect("scalar"); + assert!(super::is_hidden_format(character), "U+{code:04X}"); + assert_eq!( + display_safe(&format!("a{character}b")), + format!("a\\u{{{code:04x}}}b"), + "U+{code:04X}" + ); + } + for code in [first - 1, last + 1] { + let character = char::from_u32(code).expect("scalar"); + assert_eq!( + super::is_hidden_format(character), + listed(code), + "U+{code:04X}" + ); + if !listed(code) && !character.is_control() { + let text = format!("a{character}b"); + assert_eq!(display_safe(&text), text, "U+{code:04X} must pass"); + } + } + } + } + + #[test] + fn the_hidden_set_matches_the_documented_ranges_for_every_scalar() { + for code in 0..=0x0010_ffff_u32 { + if let Some(character) = char::from_u32(code) { + assert_eq!( + super::is_hidden_format(character), + listed(code), + "U+{code:04X}" + ); + } + } + } + + #[test] + fn ordinary_text_and_emoji_without_a_selector_are_unchanged() { + for text in [ + "plain ASCII 123 ~!", + "caf\u{e9} na\u{ef}ve \u{c5}ngstr\u{f6}m", + "\u{65e5}\u{672c}\u{8a9e} \u{d55c}\u{ae00}", + "\u{1f600} \u{1f44d} \u{2764}", + "\u{1f44d}\u{1f3fd}", + ] { + assert_eq!(display_safe(text), text); + assert_eq!(display_safe_line(text), text); + } + } + + #[test] + fn a_variation_selector_after_an_emoji_is_shown_on_purpose() { + assert_eq!(display_safe("\u{2764}\u{fe0f}"), "\u{2764}\\u{fe0f}"); + assert_eq!( + display_safe("\u{1f600}\u{e0001}\u{e007f}"), + "\u{1f600}\\u{e0001}\\u{e007f}" + ); + } + + #[test] + fn look_alikes_and_trailing_spaces_are_not_addressed() { + // Cyrillic a, mixed script, and trailing spaces pass through. This + // documents the limit; it is not a promise to detect them. + assert_eq!(display_safe("p\u{430}ypal "), "p\u{430}ypal "); + } + + #[test] + fn a_literal_escape_cannot_forge_a_real_one() { + let real = display_safe("\u{202e}"); + assert_eq!(real, "\\u{202e}"); + let forged = display_safe("\\u{202e}"); + assert_eq!(forged, "\\u{5c}u{202e}"); + assert_ne!(real, forged); + // A backslash before a real escape is not left to merge with it. + assert_eq!(display_safe("\\\u{202e}"), "\\u{5c}\\u{202e}"); + // Backslashes that do not start `u{` stay as typed. + assert_eq!(display_safe(r"C:\dir\u1234 \n"), r"C:\dir\u1234 \n"); + // A run of backslashes ahead of `u{` is escaped as a unit. + assert_eq!(display_safe("\\\\u{41}"), "\\u{5c}\\u{5c}u{41}"); + assert_clean(&display_safe("x\\\\u{202e}\u{202e}\\")); + } + + #[test] + fn mixed_hostile_text_has_no_unsafe_character_left() { + let hostile = "ok\u{202e}evil\u{200b}\r\n\ttab\0nul\u{2028}\u{feff}\\u{1b}end\n"; + let output = display_safe(hostile); + assert_clean(&output); + assert_eq!( + output.matches('\n').count(), + 2, + "only real line feeds remain" + ); + assert!(output.starts_with("ok\\u{202e}evil\\u{200b}\\u{000d}\n")); + } + + /// Push every piece through one stream and join the results with `finish`. + fn streamed(pieces: &[&str]) -> String { + let mut stream = DisplaySafeStream::new(); + let mut output = String::new(); + for piece in pieces { + output.push_str(&stream.push(piece)); + } + output.push_str(&stream.finish()); + output + } + + /// Adversarial inputs: forged escapes, backslash runs, hidden characters. + const STREAM_CASES: &[&str] = &[ + "", + "plain text", + "fn main() {\n \u{e9} \u{65e5}\u{672c} \u{1f600}\n}", + "\\u{41}", + "a\\\\u{202e}b", + "\\\\\\u{", + "\\\u{7}x", + "x\\\n", + "tail\\\\\\", + "tail\\u", + "tail\\\\u", + "\\u\\u{", + "\\u1234 \\n", + "ok\u{202e}evil\u{200b}\r\n\ttab\0nul\u{2028}\u{feff}", + "\\\u{202e}\\\\\u{200b}\\", + "\u{2764}\u{fe0f}\\u", + "u\\u{u{", + ]; + + #[test] + fn streamed_display_safe_equals_the_whole_text_for_every_two_piece_split() { + for whole in STREAM_CASES { + let expected = display_safe(whole); + for (cut, _) in whole.char_indices().chain([(whole.len(), ' ')]) { + let (first, second) = whole.split_at(cut); + assert_eq!(streamed(&[first, second]), expected, "{whole:?} at {cut}"); + } + } + } + + #[test] + fn streamed_display_safe_equals_the_whole_text_for_every_three_piece_split() { + for whole in STREAM_CASES { + let expected = display_safe(whole); + let boundaries: Vec = whole + .char_indices() + .map(|(index, _)| index) + .chain([whole.len()]) + .collect(); + for &first_cut in &boundaries { + for &second_cut in boundaries.iter().filter(|cut| **cut >= first_cut) { + let (head, rest) = whole.split_at(first_cut); + let (middle, tail) = rest.split_at(second_cut - first_cut); + assert_eq!( + streamed(&[head, middle, tail]), + expected, + "{whole:?} at {first_cut} and {second_cut}" + ); + } + } + } + } + + #[test] + fn streamed_display_safe_holds_back_only_a_backslash_tail() { + let mut stream = DisplaySafeStream::new(); + assert_eq!(stream.push("ab\\\\"), "ab"); + assert_eq!(stream.push("u"), ""); + assert_eq!(stream.push("{"), "\\u{5c}\\u{5c}u{"); + assert_eq!(stream.push("tail\\"), "tail"); + assert_eq!(stream.finish(), "\\"); + assert_eq!(DisplaySafeStream::default().finish(), ""); + } + + #[test] + fn the_line_variant_escapes_line_feeds_too() { + assert_eq!(display_safe_line("a\n2: rm"), "a\\u{000a}2: rm"); + assert!(!display_safe_line("a\nb\r\n").contains('\n')); + assert_eq!(display_safe_line("plain"), "plain"); + } +} diff --git a/crates/omgcode-core/tests/approval_review.rs b/crates/omgcode-core/tests/approval_review.rs new file mode 100644 index 0000000..26a3f0e --- /dev/null +++ b/crates/omgcode-core/tests/approval_review.rs @@ -0,0 +1,805 @@ +//! Approval review: core finds the recorded request an approval is bound to, +//! using the dispatcher's own parser, request construction, and digests. +//! +//! These tests drive real turns with a scripted model, real files, and the +//! real control store. Edit applies need a private data root; on Windows the +//! temporary directory must be on NTFS (set `TEMP` and `TMP` accordingly). + +use std::collections::VecDeque; +use std::fs; +use std::path::{Path, PathBuf}; + +use omgcode_core::control::{ + ApprovalOperation, ApprovalState, ApprovalStatus, ControlStore, ProfileDeclaration, now_unix, +}; +use omgcode_core::policy::{PermissionSet, WorkspaceId}; +use omgcode_core::prompt::{self, BASE_PROMPT, BASE_PROMPT_VERSION}; +use omgcode_core::session::{RecordedToolRequest, RequestManifest, SessionJournal}; +use omgcode_core::turn::{ + ApprovalReview, ModelError, ReviewCandidate, ReviewError, ReviewField, ReviewedRequest, + StaleReason, ToolModel, ToolModelReply, ToolModelRequest, TurnLimits, TurnRuntime, + review_approval, +}; +use serde_json::{Value, json}; +use tempfile::TempDir; +use uuid::Uuid; + +const FILE_AUDIT_NAME: &str = "file-operations-v1.jsonl"; +const COMMAND_TOOL: &str = "workspace_run_command"; +const EDIT_TOOL: &str = "workspace_edit_file"; + +struct Fixture { + _temporary: TempDir, + data: PathBuf, + workspace: PathBuf, + workspace_id: WorkspaceId, + journal: SessionJournal, +} + +impl Fixture { + fn new() -> Self { + let temporary = TempDir::new().expect("private temporary directory"); + let data = temporary.path().join("application-data"); + let root = temporary.path().join("development-root"); + let workspace = root.join("workspace"); + let tools = temporary.path().join("tools"); + fs::create_dir(&root).expect("create development root"); + fs::create_dir(&workspace).expect("create workspace"); + fs::create_dir(&tools).expect("create toolchain root"); + // Checkpointed edits need a private data root on Windows. + #[cfg(windows)] + make_private_root(&data); + + let mut control = ControlStore::open(&data).expect("open control store"); + control + .set_development_root(&root, control.revision()) + .expect("select development root"); + let workspace_id = control + .register_workspace(&workspace, control.revision()) + .expect("register workspace") + .id(); + control + .set_permissions( + workspace_id, + PermissionSet { + model_tools: true, + execute: true, + read: true, + write: true, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("enable the command and edit tools"); + control + .add_profile( + &ProfileDeclaration { + name: "system".into(), + read_only_roots: vec![tools], + path_entries: Vec::new(), + environment: Vec::new(), + }, + control.revision(), + ) + .expect("declare a toolchain profile"); + drop(control); + let journal = + SessionJournal::create_with_workspace_id(&data, &workspace, "Review", workspace_id) + .expect("create session"); + Self { + _temporary: temporary, + data, + workspace, + workspace_id, + journal, + } + } + + /// Run one turn in which the model makes `calls` and then replies. + fn turn(&mut self, calls: Vec) { + let mut model = ScriptedModel { + replies: VecDeque::from([ + Ok(ToolModelReply { + content: String::new(), + tool_calls: calls, + finish_reason: "tool_calls".to_owned(), + }), + Ok(ToolModelReply { + content: "reported".to_owned(), + tool_calls: Vec::new(), + finish_reason: "stop".to_owned(), + }), + ]), + }; + let mut runtime = TurnRuntime::new( + &mut self.journal, + &self.data, + self.workspace_id, + TurnLimits::default(), + ) + .expect("build runtime"); + runtime + .run(&mut model, "go", "", manifest()) + .expect("turn completes"); + } + + fn recorded(&self) -> Vec { + self.journal.tool_requests().to_vec() + } + + fn approvals(&self) -> Vec { + ControlStore::open_existing(&self.data) + .expect("open control") + .expect("control store") + .approval_statuses(now_unix()) + } + + fn pending(&self, operation: ApprovalOperation) -> ApprovalStatus { + let found: Vec<_> = self + .approvals() + .into_iter() + .filter(|status| { + status.operation == operation && status.state == ApprovalState::Pending + }) + .collect(); + assert_eq!(found.len(), 1, "one pending approval: {found:?}"); + found.into_iter().next().expect("one pending approval") + } + + fn state(&self, id: Uuid) -> ApprovalState { + ControlStore::open_existing(&self.data) + .expect("open control") + .expect("control store") + .approval_status(id, now_unix()) + .expect("approval status") + .state + } + + fn review( + &self, + approval: &ApprovalStatus, + recorded: &[RecordedToolRequest], + ) -> Result { + let candidates = candidates(recorded); + review_approval(&self.data, approval, &candidates) + } + + fn audit_reads(&self) -> usize { + read_events(&self.data.join(FILE_AUDIT_NAME)) + .iter() + .filter(|event| { + event["action"]["type"] == "operation_started" + && event["action"]["payload"]["operation"] == "read" + }) + .count() + } + + fn checkpoint_operations(&self) -> usize { + let mut control = ControlStore::open_existing(&self.data) + .expect("open control") + .expect("control store"); + omgcode_core::files::FileBroker::new(&mut control) + .checkpoint_inspect() + .expect("inspect checkpoints") + .operations + .len() + } + + fn write_file(&self, name: &str, contents: &str) -> PathBuf { + let path = self.workspace.join(name); + fs::write(&path, contents).expect("write workspace file"); + path + } +} + +/// Gives the data root a protected DACL for the current user only, as the +/// Windows checkpoint store requires of its private root. +#[cfg(windows)] +fn make_private_root(data: &Path) { + let whoami = std::process::Command::new("whoami") + .args(["/user", "/fo", "csv", "/nh"]) + .output() + .expect("whoami"); + assert!(whoami.status.success(), "whoami fixture failed"); + let identity = String::from_utf8_lossy(&whoami.stdout).into_owned(); + let sid = identity + .trim() + .rsplit(',') + .next() + .expect("SID column") + .trim_matches('"') + .to_owned(); + fs::create_dir(data).expect("application data"); + let granted = std::process::Command::new("icacls") + .arg(data) + .args(["/inheritance:r", "/grant:r", &format!("*{sid}:(OI)(CI)F")]) + .output() + .expect("icacls"); + assert!(granted.status.success(), "icacls fixture failed"); +} + +struct ScriptedModel { + replies: VecDeque>, +} + +impl ToolModel for ScriptedModel { + fn complete(&mut self, _request: ToolModelRequest<'_>) -> Result { + self.replies + .pop_front() + .unwrap_or(Err(ModelError::InvalidResponse)) + } +} + +fn manifest() -> RequestManifest { + RequestManifest { + base_prompt: BASE_PROMPT.to_owned(), + prompt_version: BASE_PROMPT_VERSION.to_owned(), + prompt_sha256: prompt::base_prompt_sha256(), + custom_instructions: String::new(), + model: "mock-model".into(), + base_url: "http://127.0.0.1:1234/v1".into(), + context_tokens: 32_000, + max_output_tokens: 1_000, + tool_schema_version: None, + tools: None, + } +} + +fn call(id: &str, name: &str, arguments: Value) -> Value { + json!({ + "id": id, + "type": "function", + "function": {"name": name, "arguments": arguments.to_string()} + }) +} + +fn command_call(id: &str, arguments: Value) -> Value { + call(id, COMMAND_TOOL, arguments) +} + +fn edit_call(id: &str, path: &str, old_text: &str, new_text: &str) -> Value { + call( + id, + EDIT_TOOL, + json!({"path": path, "old_text": old_text, "new_text": new_text}), + ) +} + +fn candidates(recorded: &[RecordedToolRequest]) -> Vec> { + recorded + .iter() + .map(|request| ReviewCandidate { + tool: &request.tool, + arguments: &request.arguments, + }) + .collect() +} + +fn read_events(path: &Path) -> Vec { + let Ok(bytes) = fs::read(path) else { + return Vec::new(); + }; + bytes + .split(|byte| *byte == b'\n') + .filter(|line| !line.is_empty()) + .map(|line| serde_json::from_slice(line).expect("bounded JSONL event")) + .collect() +} + +fn sha256(bytes: &[u8]) -> String { + use sha2::{Digest, Sha256}; + Sha256::digest(bytes) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} + +fn exact(review: Result) -> ReviewedRequest { + match review.expect("review completes") { + ApprovalReview::Exact(request) => request, + other => panic!("expected an exact match, found {other:?}"), + } +} + +fn field<'a>(request: &'a ReviewedRequest, label: &str) -> &'a ReviewField { + request + .fields + .iter() + .find(|field| field.label == label) + .unwrap_or_else(|| panic!("no {label:?} field in {request:?}")) +} + +/// A value the review may show: no control or hidden character but `\n`. +fn assert_display_clean(text: &str) { + for character in text.chars() { + let hidden = matches!( + character, + '\u{061c}' + | '\u{200b}'..='\u{200f}' + | '\u{2028}'..='\u{202e}' + | '\u{2060}'..='\u{2069}' + | '\u{feff}' + ); + assert!( + character == '\n' || (!character.is_control() && !hidden), + "unsafe character {character:?} in {text:?}" + ); + } +} + +/// A workspace identifier that is registered in no store this test opens. +fn foreign_workspace_id() -> WorkspaceId { + let temporary = TempDir::new().expect("private temporary directory"); + let root = temporary.path().join("root"); + let workspace = root.join("other"); + fs::create_dir_all(&workspace).expect("create workspace"); + let mut control = ControlStore::open(&temporary.path().join("data")).expect("open store"); + control + .set_development_root(&root, control.revision()) + .expect("select development root"); + control + .register_workspace(&workspace, control.revision()) + .expect("register workspace") + .id() +} + +fn statuses_json(statuses: &[ApprovalStatus]) -> String { + serde_json::to_string(statuses).expect("serialize statuses") +} + +// ---------------------------------------------------------------- commands + +#[test] +fn a_command_approval_reviews_exact_fields_and_changes_nothing() { + let mut fixture = Fixture::new(); + fixture.turn(vec![command_call( + "cmd-1", + json!({ + "profile": "system", + "program": "echo", + "arguments": ["hello", "two words", ""], + "cwd": "sub/dir", + }), + )]); + let approval = fixture.pending(ApprovalOperation::Command); + let recorded = fixture.recorded(); + let before = statuses_json(&fixture.approvals()); + + let request = exact(fixture.review(&approval, &recorded)); + + assert_eq!(request.tool, COMMAND_TOOL); + let values: Vec<(&str, &str, bool)> = request + .fields + .iter() + .map(|field| (field.label, field.value.as_str(), field.multiline)) + .collect(); + assert_eq!( + values, + [ + ("Profile", "system", false), + ("Program", "echo", false), + ("Arguments", "1: hello\n2: two words\n3: ", true), + ("Working directory", "sub/dir", false), + ("Network", "off", false), + ("Host services", "off", false), + ("Time limit", "20 seconds", false), + ("Output limit", "4096 bytes per stream", false), + ] + ); + // Reviewing is read-only: the record is still pending and unchanged, and + // nothing ran. + assert_eq!(statuses_json(&fixture.approvals()), before); + assert_eq!(fixture.state(approval.id), ApprovalState::Pending); + assert!(!fixture.data.join("runs-v1.jsonl").exists()); +} + +/// The review digest and the dispatch digest are the same value: after an +/// exact review, approving the record lets the identical call consume it and +/// reach the runner. As in the existing approved-command test the fixture +/// profile has no PATH entry, so the run is refused after consumption +/// (`command_failed`); that proves the gate and the hand-off, and this +/// suite does not qualify native confinement. +#[test] +fn an_exactly_reviewed_command_is_the_call_that_consumes_the_approval() { + let mut fixture = Fixture::new(); + let arguments = json!({"profile": "system", "program": "echo", "arguments": ["hello"]}); + fixture.turn(vec![command_call("cmd-1", arguments.clone())]); + let approval = fixture.pending(ApprovalOperation::Command); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store") + .decide_approval(approval.id, true, now_unix()) + .expect("approve"); + fixture.turn(vec![command_call("cmd-2", arguments)]); + + assert_eq!(fixture.state(approval.id), ApprovalState::Consumed); + // The journal file is locked while the session is open, so read the + // recorded tool result from its snapshot. + assert!( + fixture + .journal + .snapshot() + .messages + .iter() + .any(|message| message.text.contains("\"status\":\"command_failed\"")), + "the approved call did not reach the runner" + ); +} + +#[test] +fn an_earlier_matching_command_is_reviewed_not_the_newest_candidate() { + let mut fixture = Fixture::new(); + fixture.turn(vec![command_call( + "cmd-1", + json!({"profile": "system", "program": "echo"}), + )]); + let approval = fixture.pending(ApprovalOperation::Command); + let mut recorded = fixture.recorded(); + // Newer requests that were never approved, one valid and one malformed. + for (index, arguments) in [ + r#"{"profile":"system","program":"rm","arguments":["-rf","x"]}"#, + r#"{"profile":"system","program":"echo","unexpected":true}"#, + "not json at all", + ] + .into_iter() + .enumerate() + { + recorded.push(RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: format!("later-{index}"), + tool: COMMAND_TOOL.to_owned(), + arguments: arguments.to_owned(), + }); + } + + let request = exact(fixture.review(&approval, &recorded)); + + assert_eq!(field(&request, "Program").value, "echo"); + assert_eq!(field(&request, "Arguments").value, "(none)"); + assert_eq!( + field(&request, "Working directory").value, + "(workspace root)" + ); +} + +#[test] +fn a_command_review_is_stale_or_not_found_when_nothing_matches() { + let mut fixture = Fixture::new(); + fixture.turn(vec![command_call( + "cmd-1", + json!({"profile": "system", "program": "echo", "arguments": ["a"]}), + )]); + let approval = fixture.pending(ApprovalOperation::Command); + let recorded = fixture.recorded(); + + // Another session's approval for identical arguments is a different digest. + let other_session = ApprovalStatus { + session_id: Uuid::new_v4(), + ..approval.clone() + }; + assert_eq!( + fixture.review(&other_session, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + // Candidates of another tool do not count. + let edits_only = [RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: "e".into(), + tool: EDIT_TOOL.into(), + arguments: r#"{"path":"a","old_text":"b","new_text":"c"}"#.into(), + }]; + assert_eq!( + fixture.review(&approval, &edits_only).expect("review"), + ApprovalReview::NotFound + ); + assert_eq!( + fixture.review(&approval, &[]).expect("review"), + ApprovalReview::NotFound + ); + // Operations without a reviewable tool. + for operation in [ApprovalOperation::GitPush, ApprovalOperation::Network] { + let unreviewable = ApprovalStatus { + operation, + ..approval.clone() + }; + assert_eq!( + fixture.review(&unreviewable, &recorded).expect("review"), + ApprovalReview::NotFound + ); + } + // An unregistered workspace. + let unknown = ApprovalStatus { + workspace_id: foreign_workspace_id(), + ..approval.clone() + }; + assert_eq!( + fixture.review(&unknown, &recorded).expect("review"), + ApprovalReview::NotFound + ); + + // A settings change bumps the revision: the record can never be consumed. + let mut control = ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store"); + control + .set_permissions( + fixture.workspace_id, + PermissionSet { + model_tools: true, + execute: true, + read: true, + write: true, + host_services: true, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("change settings"); + drop(control); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::SettingsChanged) + ); +} + +#[test] +fn a_missing_settings_store_is_not_found() { + let empty = TempDir::new().expect("empty data directory"); + let approval = ApprovalStatus { + id: Uuid::new_v4(), + session_id: Uuid::new_v4(), + workspace_id: foreign_workspace_id(), + operation: ApprovalOperation::Command, + request_sha256: "0".repeat(64), + summary: "run".into(), + settings_revision: 1, + created_unix: 0, + expires_unix: 0, + state: ApprovalState::Pending, + writers_stopped: false, + }; + let candidate = ReviewCandidate { + tool: COMMAND_TOOL, + arguments: r#"{"profile":"system","program":"echo"}"#, + }; + assert_eq!( + review_approval(empty.path(), &approval, &[candidate]).expect("review"), + ApprovalReview::NotFound + ); + assert!( + !empty.path().join("control-v1.jsonl").exists(), + "nothing created" + ); +} + +#[test] +fn a_held_settings_store_makes_the_review_busy() { + let mut fixture = Fixture::new(); + fixture.turn(vec![command_call( + "cmd-1", + json!({"profile": "system", "program": "echo"}), + )]); + let approval = fixture.pending(ApprovalOperation::Command); + let recorded = fixture.recorded(); + let held = ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store"); + + let result = fixture.review(&approval, &recorded); + assert!(matches!(result, Err(ReviewError::Busy)), "{result:?}"); + let message = result.expect_err("busy").to_string(); + assert!(!message.contains(fixture.data.to_string_lossy().as_ref())); + + drop(held); + exact(fixture.review(&approval, &recorded)); +} + +#[test] +fn hostile_command_text_is_escaped_and_cannot_add_lines() { + let mut fixture = Fixture::new(); + let arguments = [ + "plain", + "a\u{202e}b", + "zero\u{200b}width", + "cr\rtab\tend", + "line\n9: forged argument", + r"literal \u{202e} text", + ]; + fixture.turn(vec![command_call( + "cmd-1", + json!({"profile": "system", "program": "echo", "arguments": arguments}), + )]); + let approval = fixture.pending(ApprovalOperation::Command); + let recorded = fixture.recorded(); + + let request = exact(fixture.review(&approval, &recorded)); + + for field in &request.fields { + assert_display_clean(&field.value); + } + let shown = field(&request, "Arguments"); + let lines: Vec<&str> = shown.value.lines().collect(); + assert_eq!( + lines.len(), + arguments.len(), + "one line per argument: {lines:?}" + ); + assert_eq!(lines[1], "2: a\\u{202e}b"); + assert_eq!(lines[2], "3: zero\\u{200b}width"); + assert_eq!(lines[3], "4: cr\\u{000d}tab\\u{0009}end"); + assert_eq!(lines[4], "5: line\\u{000a}9: forged argument"); + assert_eq!(lines[5], "6: literal \\u{5c}u{202e} text"); +} + +// ------------------------------------------------------------------- edits + +#[test] +fn an_edit_approval_reviews_exact_fields_without_touching_the_file() { + let mut fixture = Fixture::new(); + let target = fixture.write_file("notes.txt", "alpha bcc\n"); + fixture.turn(vec![edit_call("edit-1", "notes.txt", "bc", "b")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let before = statuses_json(&fixture.approvals()); + let reads = fixture.audit_reads(); + + let request = exact(fixture.review(&approval, &recorded)); + + assert_eq!(request.tool, EDIT_TOOL); + let values: Vec<(&str, String, bool)> = request + .fields + .iter() + .map(|field| (field.label, field.value.clone(), field.multiline)) + .collect(); + assert_eq!( + values, + [ + ("File", "notes.txt".to_owned(), false), + ("Old text", "bc".to_owned(), true), + ("New text", "b".to_owned(), true), + ("Size", "10 -> 9 bytes".to_owned(), false), + ("Current SHA-256", sha256(b"alpha bcc\n"), false), + ("Proposed SHA-256", sha256(b"alpha bc\n"), false), + ] + ); + assert_eq!(fs::read(&target).expect("read"), b"alpha bcc\n"); + assert_eq!(fixture.checkpoint_operations(), 0); + assert_eq!(statuses_json(&fixture.approvals()), before); + assert_eq!(fixture.audit_reads(), reads + 1, "one audited read"); +} + +/// After an exact review and an approval with the writers-stopped +/// acknowledgement, the identical call applies the edit: the review digest is +/// the dispatch digest. +#[test] +fn an_exactly_reviewed_edit_is_the_call_that_applies() { + let mut fixture = Fixture::new(); + let target = fixture.write_file("notes.txt", "alpha bcc\n"); + let arguments = edit_call("edit-1", "notes.txt", "bc", "b"); + fixture.turn(vec![arguments.clone()]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store") + .approve_with_writers_stopped(approval.id, now_unix()) + .expect("approve the edit"); + fixture.turn(vec![edit_call("edit-2", "notes.txt", "bc", "b")]); + + assert_eq!(fs::read_to_string(&target).expect("read"), "alpha bc\n"); + assert_eq!(fixture.state(approval.id), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 1); +} + +#[test] +fn an_edit_review_is_stale_after_the_file_changes() { + let mut fixture = Fixture::new(); + let target = fixture.write_file("notes.txt", "xa"); + fixture.turn(vec![edit_call("edit-1", "notes.txt", "x", "")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + fs::write(&target, "ax").expect("change the file after the request"); + + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + // A removed file is also simply not a match. + fs::remove_file(&target).expect("remove the file"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + assert_eq!(fixture.checkpoint_operations(), 0); +} + +fn recorded_edit(index: usize, arguments: String) -> RecordedToolRequest { + RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: format!("edit-{index}"), + tool: EDIT_TOOL.to_owned(), + arguments, + } +} + +fn distinct_edit_arguments(index: usize) -> String { + json!({"path": "notes.txt", "old_text": "alpha", "new_text": format!("other{index}")}) + .to_string() +} + +#[test] +fn at_most_eight_distinct_edit_candidates_are_read_newest_first() { + let mut fixture = Fixture::new(); + fixture.write_file("notes.txt", "alpha bcc\n"); + fixture.turn(vec![edit_call("edit-1", "notes.txt", "bc", "b")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let matching = fixture.recorded(); + + // Seven newer non-matching candidates: the matching one is the eighth. + let mut within = matching.clone(); + for index in 0..7 { + within.push(recorded_edit(index, distinct_edit_arguments(index))); + } + let reads = fixture.audit_reads(); + exact(fixture.review(&approval, &within)); + assert_eq!(fixture.audit_reads() - reads, 8); + + // Eight newer non-matching candidates push it past the bound. Repeats of + // one arguments string count once, so the bound is on distinct requests. + let mut beyond = matching; + for index in 0..8 { + beyond.push(recorded_edit(index, distinct_edit_arguments(index))); + beyond.push(recorded_edit(index + 100, distinct_edit_arguments(index))); + } + let reads = fixture.audit_reads(); + let result = fixture.review(&approval, &beyond).expect("review"); + assert_eq!( + result, + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + assert!( + fixture.audit_reads() - reads <= 8, + "{} audited reads", + fixture.audit_reads() - reads + ); + assert_eq!(fixture.audit_reads() - reads, 8); +} + +#[test] +fn hostile_edit_text_is_escaped_and_a_literal_escape_stays_distinguishable() { + let mut fixture = Fixture::new(); + // The file holds every kind of hostile character, including a NUL. + let contents = "x\u{202e}rtl\u{200b}zw\r\ttab\0nul \\u{202e} literal\nsecond\n"; + fixture.write_file("odd.txt", contents); + let replacement = "ok\u{2028}\u{feff}\r\\u{200b}"; + fixture.turn(vec![edit_call("edit-1", "odd.txt", contents, replacement)]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + + let request = exact(fixture.review(&approval, &recorded)); + + for field in &request.fields { + assert_display_clean(&field.value); + } + let old = &field(&request, "Old text").value; + assert_eq!( + old, + "x\\u{202e}rtl\\u{200b}zw\\u{000d}\\u{0009}tab\\u{0000}nul \\u{5c}u{202e} literal\nsecond\n" + ); + assert!(old.contains("\\u{202e}rtl"), "the real override is shown"); + assert!( + old.contains("\\u{5c}u{202e}"), + "the forged literal is marked" + ); + assert_eq!( + field(&request, "New text").value, + "ok\\u{2028}\\u{feff}\\u{000d}\\u{5c}u{200b}" + ); +} diff --git a/crates/omgcode-core/tests/local_provider.rs b/crates/omgcode-core/tests/local_provider.rs index c38f500..753958d 100644 --- a/crates/omgcode-core/tests/local_provider.rs +++ b/crates/omgcode-core/tests/local_provider.rs @@ -2,10 +2,11 @@ use omgcode_core::prompt::{BASE_PROMPT, ModelMessage, ModelRole}; use omgcode_core::provider::{ LocalModelClient, LocalModelConfig, ModelReply, ProviderError, Usage, }; -use omgcode_core::turn::{ToolCall, ToolModelReply, tool_definitions}; +use omgcode_core::turn::{CancelToken, ToolCall, ToolModelReply, tool_definitions}; use serde_json::Value; use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; +use std::sync::mpsc; use std::thread::{self, JoinHandle}; use std::time::{Duration, Instant}; @@ -645,3 +646,703 @@ fn tool_responses_with_duplicate_keys_or_unsupported_shapes_are_refused() { ); } } + +const EVENT_STREAM: &str = "Content-Type: text/event-stream\r\n"; + +fn text_event(content: &str) -> String { + format!( + "data: {}\n\n", + serde_json::json!({"choices":[{"index":0,"delta":{"content":content},"finish_reason":null}]}) + ) +} + +fn finish_event(reason: &str) -> String { + format!( + "data: {}\n\n", + serde_json::json!({"choices":[{"index":0,"delta":{},"finish_reason":reason}]}) + ) +} + +fn text_stream(pieces: &[&str]) -> String { + let mut stream: String = pieces.iter().map(|piece| text_event(piece)).collect(); + stream.push_str(&finish_event("stop")); + stream.push_str("data: [DONE]\n\n"); + stream +} + +/// The outcome of one streamed text exchange. +struct StreamExchange { + result: Result, + pieces: Vec, + request: Value, + head: String, +} + +fn stream_exchange(status: &str, body: &str, headers: &str) -> StreamExchange { + let fixture = Fixture::start(status, body.as_bytes(), headers); + let mut pieces = Vec::new(); + let result = client(fixture.address.clone(), None).complete_streaming( + &messages(), + &mut |text: &str| pieces.push(text.to_owned()), + &CancelToken::new(), + ); + let (head, request) = fixture.finish(); + StreamExchange { + result, + pieces, + request: serde_json::from_slice(&request).expect("request JSON"), + head, + } +} + +fn tool_stream_exchange(body: &str) -> (Result, Vec, Value) { + let fixture = Fixture::start("200 OK", body.as_bytes(), EVENT_STREAM); + let mut pieces = Vec::new(); + let result = client(fixture.address.clone(), None).complete_with_tools_streaming( + &messages(), + &tool_definitions(), + &mut |text: &str| pieces.push(text.to_owned()), + &CancelToken::new(), + ); + let (_, request) = fixture.finish(); + ( + result, + pieces, + serde_json::from_slice(&request).expect("request JSON"), + ) +} + +fn call_event(entry: &str) -> String { + format!( + "data: {{\"choices\":[{{\"index\":0,\"delta\":{{\"tool_calls\":[{entry}]}},\"finish_reason\":null}}]}}\n\n" + ) +} + +#[test] +fn a_streamed_text_request_sends_stream_true_and_delivers_pieces_in_order() { + // Given a server that streams three pieces. + let body = text_stream(&["Hel", "lo ", "there"]); + + // When a streamed text request runs. + let exchange = stream_exchange("200 OK", &body, EVENT_STREAM); + + // Then the reply is the concatenation, the pieces arrived in order, and + // the wire request asked for a stream without `stream_options`. + let reply = exchange.result.expect("valid stream"); + assert_eq!(reply.content, "Hello there"); + assert_eq!(reply.finish_reason, "stop"); + assert_eq!(reply.usage, None); + assert_eq!(exchange.pieces, ["Hel", "lo ", "there"]); + assert_eq!(exchange.request["stream"], true); + assert!(exchange.request.get("stream_options").is_none()); + assert!(exchange.request.get("tools").is_none()); + assert!( + exchange + .head + .to_ascii_lowercase() + .contains("accept: text/event-stream\r\n") + ); +} + +#[test] +fn non_streaming_requests_still_send_stream_false_and_no_accept_header() { + // Given the non-streaming text and tool methods. + let fixture = Fixture::start("200 OK", SUCCESS.as_bytes(), ""); + client(fixture.address.clone(), None) + .complete(&messages()) + .expect("reply"); + + // When their requests are captured. + let (head, body) = fixture.finish(); + let request: Value = serde_json::from_slice(&body).expect("request JSON"); + let (_, tool_request) = tool_exchange( + r#"{"choices":[{"message":{"role":"assistant","content":"x"},"finish_reason":"stop"}]}"#, + ); + + // Then both send `stream: false`, no `stream_options`, and no event-stream Accept. + assert_eq!(request["stream"], false); + assert!(request.get("stream_options").is_none()); + assert!(!head.to_ascii_lowercase().contains("text/event-stream")); + assert_eq!(tool_request["stream"], false); + assert!(tool_request.get("stream_options").is_none()); +} + +#[test] +fn a_streamed_tool_request_sends_stream_true_with_the_tools() { + let (result, _, request) = tool_stream_exchange(&text_stream(&["ok"])); + assert_eq!(result.expect("valid stream").content, "ok"); + assert_eq!(request["stream"], true); + assert_eq!(request["tool_choice"], "auto"); + assert!( + request["tools"] + .as_array() + .is_some_and(|tools| !tools.is_empty()) + ); + assert!(request.get("stream_options").is_none()); +} + +#[test] +fn a_content_type_with_parameters_in_any_case_is_accepted() { + let exchange = stream_exchange( + "200 OK", + &text_stream(&["a"]), + "Content-Type: Text/Event-Stream; charset=utf-8\r\n", + ); + assert_eq!(exchange.result.expect("valid stream").content, "a"); + assert_eq!(exchange.pieces, ["a"]); +} + +#[test] +fn a_json_reply_to_a_streamed_request_is_refused() { + let exchange = stream_exchange("200 OK", SUCCESS, "Content-Type: application/json\r\n"); + assert_eq!(exchange.result, Err(ProviderError::MalformedResponse)); + assert!(exchange.pieces.is_empty()); + + let missing = stream_exchange("200 OK", &text_stream(&["a"]), ""); + assert_eq!(missing.result, Err(ProviderError::MalformedResponse)); +} + +#[test] +fn a_non_success_status_to_a_streamed_request_is_an_http_status_error() { + let exchange = stream_exchange( + "503 Service Unavailable", + &text_stream(&["a"]), + EVENT_STREAM, + ); + assert_eq!(exchange.result, Err(ProviderError::HttpStatus(503))); + assert!(exchange.pieces.is_empty()); +} + +#[test] +fn a_streamed_reply_without_done_is_incomplete_and_keeps_delivered_text() { + let body = format!("{}{}", text_event("one"), text_event("two")); + let exchange = stream_exchange("200 OK", &body, EVENT_STREAM); + assert_eq!(exchange.result, Err(ProviderError::IncompleteStream)); + assert_eq!(exchange.pieces, ["one", "two"]); +} + +#[test] +fn text_before_a_malformed_payload_stays_delivered_and_the_call_fails() { + let body = format!( + "{}{}data: {{broken\n\n", + text_event("one"), + text_event("two") + ); + let exchange = stream_exchange("200 OK", &body, EVENT_STREAM); + assert_eq!(exchange.result, Err(ProviderError::MalformedResponse)); + assert_eq!(exchange.pieces, ["one", "two"]); +} + +#[test] +fn a_streamed_reply_over_one_mebibyte_is_refused() { + let mut body = text_event("start"); + while body.len() <= 1024 * 1024 { + body.push_str(": padding padding padding padding padding padding\n"); + } + let exchange = stream_exchange("200 OK", &body, EVENT_STREAM); + assert_eq!(exchange.result, Err(ProviderError::ResponseTooLarge)); + assert_eq!(exchange.pieces, ["start"]); +} + +#[test] +fn streamed_usage_and_crlf_framing_are_accepted() { + let body = format!( + ": ping\r\n\r\nevent: message\r\ndata:{}\r\n\r\ndata: {}\r\n\r\ndata: {}\r\n\r\ndata: [DONE]\r\n\r\n", + serde_json::json!({"choices":[{"index":0,"delta":{"role":"assistant","content":"hi"}}]}), + serde_json::json!({"choices":[{"index":0,"delta":{},"finish_reason":"length"}]}), + serde_json::json!({"choices":[],"usage":{"prompt_tokens":5,"completion_tokens":2,"total_tokens":7}}), + ); + let exchange = stream_exchange("200 OK", &body, EVENT_STREAM); + assert_eq!( + exchange.result, + Ok(ModelReply { + content: "hi".to_owned(), + finish_reason: "length".to_owned(), + usage: Some(Usage { + prompt_tokens: Some(5), + completion_tokens: Some(2), + total_tokens: Some(7), + }), + }) + ); + assert_eq!(exchange.pieces, ["hi"]); +} + +#[test] +fn the_streamed_text_method_refuses_tool_calls_and_tool_finish_reasons() { + let with_call = call_event( + r#"{"index":0,"id":"a","type":"function","function":{"name":"workspace_read_file","arguments":"{}"}}"#, + ); + let exchange = stream_exchange("200 OK", &with_call, EVENT_STREAM); + assert_eq!(exchange.result, Err(ProviderError::UnsupportedToolCall)); + + let finish = format!("{}data: [DONE]\n\n", finish_event("tool_calls")); + let exchange = stream_exchange("200 OK", &finish, EVENT_STREAM); + assert_eq!(exchange.result, Err(ProviderError::UnsupportedToolCall)); +} + +#[test] +fn documented_streamed_tool_call_pieces_assemble_into_a_valid_call() { + // Given the first piece with id, type, name, and empty arguments, then fragments. + let mut body = text_event("Reading."); + body.push_str(&call_event( + r#"{"index":0,"id":"call_9","type":"function","function":{"name":"workspace_read_file","arguments":""}}"#, + )); + body.push_str(&call_event( + r#"{"index":0,"function":{"arguments":"{\"path\":"}}"#, + )); + body.push_str(&call_event( + r#"{"index":0,"function":{"arguments":"\"notes.txt\"}"}}"#, + )); + body.push_str(&finish_event("tool_calls")); + body.push_str("data: [DONE]\n\n"); + + // When the streamed tool request runs. + let (result, pieces, _) = tool_stream_exchange(&body); + + // Then one raw call parses with the real validator and the text was delivered. + let reply = result.expect("valid tool stream"); + assert_eq!(reply.content, "Reading."); + assert_eq!(reply.finish_reason, "tool_calls"); + assert_eq!(pieces, ["Reading."]); + let [raw_call] = reply.tool_calls.as_slice() else { + panic!("exactly one call: {:?}", reply.tool_calls); + }; + assert!(ToolCall::parse(raw_call).is_ok()); +} + +/// Stream the given call pieces and a finish reason through the tool method. +fn assembled(entries: &[&str], reason: &str) -> Result { + let mut body: String = entries.iter().map(|entry| call_event(entry)).collect(); + body.push_str(&finish_event(reason)); + body.push_str("data: [DONE]\n\n"); + tool_stream_exchange(&body).0 +} + +#[test] +fn streamed_tool_call_pieces_follow_the_assembly_rules() { + // A name sent in pieces, a repeated equal id, and finish `stop` with calls. + let reply = assembled( + &[ + r#"{"index":0,"id":"a","function":{"name":"workspace_list_"}}"#, + r#"{"index":0,"id":"a","type":"function","function":{"name":"directory","arguments":"{\"path\":\"\"}"}}"#, + ], + "stop", + ) + .expect("assembled"); + let [raw_call] = reply.tool_calls.as_slice() else { + panic!("exactly one call: {:?}", reply.tool_calls); + }; + assert!(ToolCall::parse(raw_call).is_ok()); + + // A call that never received an id has no `id` member and fails validation. + let reply = assembled( + &[r#"{"index":0,"type":"function","function":{"name":"x","arguments":"{}"}}"#], + "tool_calls", + ) + .expect("assembled"); + let [raw_call] = reply.tool_calls.as_slice() else { + panic!("exactly one call: {:?}", reply.tool_calls); + }; + assert!(raw_call.get("id").is_none()); + assert!(ToolCall::parse(raw_call).is_err()); +} + +#[test] +fn streamed_tool_call_pieces_that_break_the_rules_are_refused() { + let refused: [&[&str]; 5] = [ + &[r#"{"index":0,"id":"a"}"#, r#"{"index":0,"id":"b"}"#], + &[r#"{"index":2,"id":"a"}"#], + &[r#"{"index":0,"surprise":true}"#], + &[r#"{"index":0,"type":"custom"}"#], + &[r#"{"index":0,"function":{"extra":"x"}}"#], + ]; + for entries in refused { + assert_eq!( + assembled(entries, "tool_calls").expect_err("refused"), + ProviderError::MalformedResponse, + "{entries:?}" + ); + } + + // A reply cut short by the token limit cannot carry tool calls. + assert_eq!( + assembled(&[r#"{"index":0,"id":"a"}"#], "length").expect_err("refused"), + ProviderError::MalformedResponse + ); + + // Sixty-four calls are accepted; index 64 is refused. + let many: Vec = (0..65) + .map(|index| format!(r#"{{"index":{index},"id":"c{index}"}}"#)) + .collect(); + let many: Vec<&str> = many.iter().map(String::as_str).collect(); + assert_eq!( + assembled(&many, "tool_calls").expect_err("refused"), + ProviderError::MalformedResponse + ); + assert_eq!( + assembled(many.get(..64).expect("sixty-four"), "tool_calls") + .expect("sixty-four calls") + .tool_calls + .len(), + 64 + ); +} + +// ---- Cancellation of a streamed request (ADR-0050) ---- + +/// The response head of a streamed reply whose body ends only when the server closes. +const STREAM_HEAD: &str = + "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n"; + +/// How long the fixtures hold a connection open for a client that never closes it. +const HOLD: Duration = Duration::from_secs(5); + +/// The slowest acceptable cancellation. The request deadline is 30 seconds. +const CANCEL_BOUND: Duration = Duration::from_secs(2); + +/// Accept one connection within a bound. +fn accept_bounded(listener: &TcpListener) -> TcpStream { + let deadline = Instant::now() + Duration::from_secs(5); + loop { + match listener.accept() { + Ok((stream, _)) => return stream, + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { + assert!(Instant::now() < deadline, "fixture accept timed out"); + thread::sleep(Duration::from_millis(2)); + } + Err(error) => panic!("fixture accept failed: {error}"), + } + } +} + +/// Serve one connection: read the request, write `response` (when any), then +/// hold the connection open. The task returns whether the client closed it +/// within [`HOLD`]. `request_read` is signalled once the whole request has been +/// read, which is the moment the client starts waiting for a response head. +fn holding_server( + response: Option>, + request_read: mpsc::Sender<()>, +) -> (String, JoinHandle) { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind local fixture"); + listener + .set_nonblocking(true) + .expect("make fixture accept bounded"); + let address = format!("http://{}/v1", listener.local_addr().expect("address")); + let task = thread::spawn(move || { + let mut stream = accept_bounded(&listener); + stream + .set_nonblocking(false) + .expect("make accepted fixture stream blocking"); + stream + .set_read_timeout(Some(Duration::from_secs(3))) + .expect("set fixture read timeout"); + stream + .set_write_timeout(Some(Duration::from_secs(3))) + .expect("set fixture write timeout"); + read_request(&mut stream).expect("read bounded request"); + // A call that cancels on delivered pieces does not listen for this, so + // a failed send carries no information. + let _ignored = request_read.send(()); + if let Some(bytes) = response { + stream.write_all(&bytes).expect("write fixture response"); + stream.flush().expect("flush fixture response"); + } + stream + .set_read_timeout(Some(Duration::from_millis(25))) + .expect("set fixture poll timeout"); + let end = Instant::now() + HOLD; + let mut buffer = [0_u8; 64]; + loop { + match stream.read(&mut buffer) { + Ok(0) => return true, + Ok(_) => {} + Err(error) + if matches!( + error.kind(), + std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut + ) => + { + if Instant::now() >= end { + return false; + } + } + Err(error) + if matches!( + error.kind(), + std::io::ErrorKind::ConnectionReset + | std::io::ErrorKind::ConnectionAborted + | std::io::ErrorKind::BrokenPipe + ) => + { + return true; + } + Err(error) => panic!("fixture read failed: {error}"), + } + } + }); + (address, task) +} + +/// What one cancelled-or-not streamed call did. +struct HeldCall { + result: Result, + pieces: Vec, + elapsed: Duration, + /// Whether the server saw the client close the connection. + closed: bool, +} + +/// Run one streamed call against a server that writes `response` and then +/// holds the body open. A helper thread cancels the call once +/// `pieces_before_cancel` pieces were delivered, so the call is waiting for the +/// next piece of the body when it is cancelled. With `None` the thread cancels +/// once the server has read the whole request, so the call is waiting for a +/// response head however slow the host is. +fn held_call( + response: Option>, + pieces_before_cancel: Option, + run: impl FnOnce(&LocalModelClient, &mut dyn FnMut(&str), &CancelToken) -> Result, +) -> HeldCall { + let (request_read, request_seen) = mpsc::channel::<()>(); + let (address, server) = holding_server(response, request_read); + let cancel = CancelToken::new(); + let (delivered, seen) = mpsc::channel::<()>(); + let canceller = { + let cancel = cancel.clone(); + thread::spawn(move || match pieces_before_cancel { + Some(count) => { + for _ in 0..count { + seen.recv_timeout(Duration::from_secs(5)) + .expect("the pieces arrive before the bound"); + } + cancel.cancel(); + } + None => { + request_seen + .recv_timeout(Duration::from_secs(5)) + .expect("the server reads the request before the bound"); + cancel.cancel(); + } + }) + }; + let mut pieces = Vec::new(); + let started = Instant::now(); + let result = run( + &client(address, None), + &mut |text: &str| { + pieces.push(text.to_owned()); + // The helper drops its receiver once it has cancelled, so a send after + // that point fails by design and carries no information. + let _ignored = delivered.send(()); + }, + &cancel, + ); + let elapsed = started.elapsed(); + join_bounded(canceller, "canceller completes"); + let closed = join_bounded(server, "fixture completes"); + HeldCall { + result, + pieces, + elapsed, + closed, + } +} + +fn held_body(head_and_events: &str) -> Option> { + Some(format!("{STREAM_HEAD}{head_and_events}").into_bytes()) +} + +#[test] +fn a_token_cancelled_before_the_call_makes_no_connection() { + // Given a listening socket and a token that is already cancelled. + let listener = TcpListener::bind("127.0.0.1:0").expect("bind no-call fixture"); + listener + .set_nonblocking(true) + .expect("make no-call fixture nonblocking"); + let address = format!("http://{}/v1", listener.local_addr().expect("address")); + let model = client(address, None); + let cancel = CancelToken::new(); + cancel.cancel(); + let mut pieces = Vec::new(); + + // When each streamed method is called. + let text = model.complete_streaming( + &messages(), + &mut |piece: &str| pieces.push(piece.to_owned()), + &cancel, + ); + let tools = model.complete_with_tools_streaming( + &messages(), + &tool_definitions(), + &mut |piece: &str| pieces.push(piece.to_owned()), + &cancel, + ); + + // Then both are cancelled, nothing was delivered, and nothing connected. + assert_eq!(text, Err(ProviderError::Cancelled)); + assert_eq!(tools.err(), Some(ProviderError::Cancelled)); + assert!(pieces.is_empty()); + assert!( + matches!( + listener.accept(), + Err(ref error) if error.kind() == std::io::ErrorKind::WouldBlock + ), + "no connection was made" + ); +} + +#[test] +fn cancelling_while_the_response_head_is_awaited_stops_the_request() { + // Given a server that accepts and never sends a response head. + // When the token is cancelled once the server has read the request. + let call = held_call(None, None, |model, on_text, cancel| { + model.complete_streaming(&messages(), on_text, cancel) + }); + + // Then the call is cancelled far inside the 30-second deadline, and the + // server saw the connection close. + assert_eq!(call.result, Err(ProviderError::Cancelled)); + assert!(call.pieces.is_empty()); + assert!( + call.elapsed < CANCEL_BOUND, + "cancellation took {:?}", + call.elapsed + ); + assert!(call.closed, "the connection was closed on cancellation"); +} + +#[test] +fn cancelling_while_a_text_body_is_held_open_keeps_the_delivered_pieces() { + // Given a server that sends two pieces and holds the body open. + let body = held_body(&format!("{}{}", text_event("one"), text_event("two"))); + + // When the token is cancelled after both pieces were delivered. + let call = held_call(body, Some(2), |model, on_text, cancel| { + model.complete_streaming(&messages(), on_text, cancel) + }); + + // Then both pieces stay delivered, the call is cancelled promptly, and the + // connection was closed. + assert_eq!(call.result, Err(ProviderError::Cancelled)); + assert_eq!(call.pieces, ["one", "two"]); + assert!( + call.elapsed < CANCEL_BOUND, + "cancellation took {:?}", + call.elapsed + ); + assert!(call.closed, "the connection was closed on cancellation"); +} + +#[test] +fn cancelling_a_streamed_tool_request_discards_a_partly_sent_call() { + // Given a server that sends text and the first piece of a tool call, then + // holds the body open. + let mut events = text_event("Reading."); + events.push_str(&call_event( + r#"{"index":0,"id":"call_9","type":"function","function":{"name":"workspace_read_file","arguments":"{\"path\":"}}"#, + )); + + // When the token is cancelled after the text was delivered. + let call = held_call(held_body(&events), Some(1), |model, on_text, cancel| { + model.complete_with_tools_streaming(&messages(), &tool_definitions(), on_text, cancel) + }); + + // Then the call is cancelled, no reply and no call exist, and the text stays delivered. + assert_eq!(call.result.err(), Some(ProviderError::Cancelled)); + assert_eq!(call.pieces, ["Reading."]); + assert!( + call.elapsed < CANCEL_BOUND, + "cancellation took {:?}", + call.elapsed + ); + assert!(call.closed, "the connection was closed on cancellation"); +} + +#[test] +fn a_cancel_after_done_was_processed_does_not_change_the_reply() { + // Given a complete stream, sent in one write, whose server then keeps the + // connection open so that the client must not wait for the body to end. + let body = held_body(&text_stream(&["one", "two"])); + + // When the token is cancelled while the pieces are delivered. + let call = held_call(body, Some(2), |model, on_text, cancel| { + model.complete_streaming(&messages(), on_text, cancel) + }); + + // Then the reply is the validated one, and nothing waited for the server. + let reply = call.result.expect("a reply that was complete"); + assert_eq!(reply.content, "onetwo"); + assert_eq!(reply.finish_reason, "stop"); + assert_eq!(call.pieces, ["one", "two"]); + assert!( + call.elapsed < CANCEL_BOUND, + "the call waited {:?}", + call.elapsed + ); +} + +#[test] +fn a_cancel_after_done_does_not_change_a_streamed_tool_reply() { + // Given a complete tool stream with a call, held open after `[DONE]`. + let mut events = text_event("Reading."); + events.push_str(&call_event( + r#"{"index":0,"id":"call_9","type":"function","function":{"name":"workspace_read_file","arguments":"{\"path\":\"notes.txt\"}"}}"#, + )); + events.push_str(&finish_event("tool_calls")); + events.push_str("data: [DONE]\n\n"); + + // When the token is cancelled while the text is delivered. + let call = held_call(held_body(&events), Some(1), |model, on_text, cancel| { + model.complete_with_tools_streaming(&messages(), &tool_definitions(), on_text, cancel) + }); + + // Then the reply with its call is returned. + let reply = call.result.expect("a reply that was complete"); + assert_eq!(reply.tool_calls.len(), 1); + assert_eq!(reply.finish_reason, "tool_calls"); + assert!(call.elapsed < CANCEL_BOUND); +} + +#[test] +fn a_token_that_is_never_cancelled_does_not_disturb_a_slow_stream() { + // Given a server that sends its pieces more than one poll interval apart. + let listener = TcpListener::bind("127.0.0.1:0").expect("bind local fixture"); + listener.set_nonblocking(true).expect("bounded accept"); + let address = format!("http://{}/v1", listener.local_addr().expect("address")); + let server = thread::spawn(move || { + let mut stream = accept_bounded(&listener); + stream + .set_nonblocking(false) + .expect("make accepted fixture stream blocking"); + stream + .set_read_timeout(Some(Duration::from_secs(3))) + .expect("set fixture read timeout"); + read_request(&mut stream).expect("read bounded request"); + stream + .write_all(STREAM_HEAD.as_bytes()) + .expect("write head"); + for event in [text_event("a"), text_event("b"), text_event("c")] { + thread::sleep(Duration::from_millis(80)); + stream.write_all(event.as_bytes()).expect("write piece"); + stream.flush().expect("flush piece"); + } + thread::sleep(Duration::from_millis(80)); + stream + .write_all(format!("{}data: [DONE]\n\n", finish_event("stop")).as_bytes()) + .expect("write end"); + }); + let mut pieces = Vec::new(); + + // When the streamed request runs with a token nobody cancels. + let result = client(address, None).complete_streaming( + &messages(), + &mut |piece: &str| pieces.push(piece.to_owned()), + &CancelToken::new(), + ); + join_bounded(server, "fixture completes"); + + // Then the polling did not restart or disturb the request. + assert_eq!(result.expect("a complete stream").content, "abc"); + assert_eq!(pieces, ["a", "b", "c"]); +} diff --git a/crates/omgcode-core/tests/session_index.rs b/crates/omgcode-core/tests/session_index.rs new file mode 100644 index 0000000..7f53b4b --- /dev/null +++ b/crates/omgcode-core/tests/session_index.rs @@ -0,0 +1,1390 @@ +//! Native tests for the derived session index. Journals are real files made by +//! the real `SessionJournal`, and the index is a real SQLite database. + +use std::fs; +use std::path::{Path, PathBuf}; + +use omgcode_core::session::{ + CompactionRecord, MessageRole, NativePath, RequestManifest, SessionInspection, SessionJournal, + ToolOutcome, +}; +use omgcode_core::session_index::{ + APPLICATION_ID, ArchiveView, INDEX_DIRECTORY, INDEX_FILE, IndexError, IndexedSession, + IndexedState, SearchListing, SearchRole, SessionIndex, SessionListing, SessionQuery, + SessionSort, +}; +use rusqlite::Connection; +use sha2::{Digest, Sha256}; +use tempfile::TempDir; +use time::OffsetDateTime; +use uuid::Uuid; + +fn setup() -> (TempDir, PathBuf, PathBuf) { + let temp = TempDir::new().expect("temporary directory"); + let root = temp.path().join("app-data"); + let workspace = temp.path().join("workspace"); + fs::create_dir_all(&workspace).expect("workspace directory"); + (temp, root, workspace) +} + +fn sha256_hex(text: &str) -> String { + Sha256::digest(text.as_bytes()) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} + +fn manifest() -> RequestManifest { + RequestManifest { + base_prompt: "BASE-PROMPT-TEXT".into(), + prompt_version: "7".into(), + prompt_sha256: "a".repeat(64), + custom_instructions: "CUSTOM-INSTRUCTIONS-TEXT".into(), + model: "local-model".into(), + base_url: "http://127.0.0.1:1234/v1".into(), + context_tokens: 32_000, + max_output_tokens: 2_000, + tool_schema_version: Some("1".into()), + tools: Some(vec!["workspace_read_file".into()]), + } +} + +/// Create a journal with user messages, release it, and return id and path. +fn session(root: &Path, workspace: &Path, title: &str, messages: &[&str]) -> (Uuid, PathBuf) { + let mut journal = SessionJournal::create(root, workspace, title).expect("create"); + let id = journal.snapshot().session_id; + for message in messages { + journal + .record_message(MessageRole::User, message) + .expect("message"); + } + (id, journal.path().to_path_buf()) +} + +/// Move a journal file under another day directory and return its new path. +fn move_to_day(root: &Path, path: &Path, day: &str) -> PathBuf { + let directory = root.join("sessions").join(day); + fs::create_dir_all(&directory).expect("day directory"); + let target = directory.join(path.file_name().expect("file name")); + fs::rename(path, &target).expect("move journal"); + target +} + +fn append(path: &Path, text: &str) { + let mut journal = SessionJournal::open(path).expect("open writer"); + journal + .record_message(MessageRole::User, text) + .expect("append"); +} + +fn database_path(root: &Path) -> PathBuf { + root.join(INDEX_DIRECTORY).join(INDEX_FILE) +} + +fn refreshed(root: &Path) -> SessionIndex { + let mut index = SessionIndex::open(root).expect("open index"); + index.refresh(1000).expect("refresh"); + index +} + +fn query(view: ArchiveView, sort: SessionSort, descending: bool, limit: usize) -> SessionQuery { + SessionQuery { + view, + sort, + descending, + limit, + } +} + +fn list(index: &SessionIndex, view: ArchiveView) -> SessionListing { + index + .sessions(&query(view, SessionSort::Title, false, 5000)) + .expect("listing") +} + +fn ids(listing: &SessionListing) -> Vec { + listing.sessions.iter().map(|s| s.session_id).collect() +} + +fn find(index: &SessionIndex, text: &str, view: ArchiveView) -> SearchListing { + index.search(text, view, 50).expect("search") +} + +fn nanos(time: OffsetDateTime) -> i128 { + time.unix_timestamp_nanos() +} + +fn raw(root: &Path) -> Connection { + Connection::open(database_path(root)).expect("raw connection") +} + +fn read_all(paths: &[PathBuf]) -> Vec> { + paths + .iter() + .map(|path| fs::read(path).expect("journal bytes")) + .collect() +} + +#[test] +fn first_build_lists_every_session_with_the_inspected_metadata() { + // Given sessions in two workspaces on different days, one archived. + let (temp, root, workspace_a) = setup(); + let workspace_b = temp.path().join("workspace-b"); + fs::create_dir_all(&workspace_b).expect("second workspace"); + let (a1, p1) = session(&root, &workspace_a, "Alpha", &["one"]); + let (b1, p2) = session(&root, &workspace_b, "Bravo", &["two", "three"]); + let (a2, p3) = session(&root, &workspace_a, "Charlie", &[]); + let p1 = move_to_day(&root, &p1, "2025/03/04"); + let p2 = move_to_day(&root, &p2, "2025/03/05"); + let _ = move_to_day(&root, &p3, "2024/12/31"); + { + let mut journal = SessionJournal::open(&p2).expect("open"); + journal.archive().expect("archive"); + } + + // When the index is built. + let mut index = SessionIndex::open(&root).expect("open"); + let report = index.refresh(1000).expect("refresh"); + + // Then every journal was inspected and metadata matches the inspection. + assert_eq!(report.discovered, 3); + assert_eq!(report.inspected, 3); + assert_eq!( + report.unchanged + report.busy + report.invalid + report.removed, + 0 + ); + assert_eq!(report.remaining, 0); + let active = list(&index, ArchiveView::Active); + let archived = list(&index, ArchiveView::Archived); + assert_eq!((active.total, archived.total), (2, 1)); + assert_eq!(ids(&archived), vec![b1]); + let by_id = |listing: &SessionListing, id: Uuid| -> IndexedSession { + listing + .sessions + .iter() + .find(|s| s.session_id == id) + .expect("session listed") + .clone() + }; + for (id, path, listing) in [(a1, &p1, &active), (b1, &p2, &archived)] { + let inspection = SessionInspection::inspect(path).expect("inspect"); + let indexed = by_id(listing, id); + assert_eq!(indexed.state, IndexedState::Ok); + assert_eq!(indexed.invalid_code, None); + assert_eq!(indexed.title.as_deref(), Some(inspection.title.as_str())); + assert_eq!(indexed.workspace_id, Some(inspection.workspace_id)); + assert_eq!(indexed.workspace, Some(inspection.workspace.clone())); + assert_eq!( + indexed.folder_display, + Some( + inspection + .workspace + .to_path_buf() + .expect("native path") + .to_string_lossy() + .into_owned() + ) + ); + assert_eq!( + indexed.created_at.map(nanos), + Some(nanos(inspection.created_at)) + ); + assert_eq!( + indexed.updated_at.map(nanos), + Some(nanos(inspection.updated_at)) + ); + assert_eq!(indexed.archived, inspection.archived); + assert_eq!(indexed.active_turn, None); + assert_eq!(indexed.event_count, inspection.event_count); + assert!(!indexed.incomplete_tail); + assert_eq!(index.session(id).expect("lookup"), Some(indexed)); + } + assert_eq!(by_id(&active, a2).title.as_deref(), Some("Charlie")); + assert_eq!(by_id(&active, a1).workspace, by_id(&active, a2).workspace); + assert_ne!( + by_id(&active, a1).workspace_id, + by_id(&archived, b1).workspace_id + ); + assert_eq!(index.session(Uuid::new_v4()).expect("unknown"), None); +} + +#[test] +fn every_sort_key_orders_in_both_directions_with_deterministic_ties() { + // Given sessions where three titles differ only by case. + let (_temp, root, workspace) = setup(); + let mut known = Vec::new(); + for title in ["delta", "Same", "same", "SAME", "Éclair", "apple"] { + let (id, path) = session(&root, &workspace, title, &["x"]); + known.push(( + id, + title.to_owned(), + SessionInspection::inspect(&path).expect("inspect"), + )); + } + let index = refreshed(&root); + + for sort in [ + SessionSort::Updated, + SessionSort::Created, + SessionSort::Title, + ] { + for descending in [false, true] { + let mut expected: Vec<_> = known.iter().collect(); + expected.sort_by(|a, b| { + let primary = match sort { + SessionSort::Updated => nanos(a.2.updated_at).cmp(&nanos(b.2.updated_at)), + SessionSort::Created => nanos(a.2.created_at).cmp(&nanos(b.2.created_at)), + SessionSort::Title => a.1.to_lowercase().cmp(&b.1.to_lowercase()), + }; + let primary = if descending { + primary.reverse() + } else { + primary + }; + primary.then(a.0.cmp(&b.0)) + }); + let listing = index + .sessions(&query(ArchiveView::Active, sort, descending, 5000)) + .expect("listing"); + let expected_ids: Vec = expected.iter().map(|k| k.0).collect(); + assert_eq!( + ids(&listing), + expected_ids, + "{sort:?} descending={descending}" + ); + } + } + // The tied titles come out in ascending session id whichever way titles sort. + for descending in [false, true] { + let listing = index + .sessions(&query( + ArchiveView::Active, + SessionSort::Title, + descending, + 5000, + )) + .expect("listing"); + let tied: Vec = listing + .sessions + .iter() + .filter(|s| { + s.title + .as_deref() + .is_some_and(|t| t.eq_ignore_ascii_case("same")) + }) + .map(|s| s.session_id) + .collect(); + let mut sorted = tied.clone(); + sorted.sort(); + assert_eq!(tied, sorted); + assert_eq!(tied.len(), 3); + } +} + +#[test] +fn listing_reports_the_view_total_before_the_limit_and_clamps_the_limit() { + let (_temp, root, workspace) = setup(); + for index in 0..5 { + session(&root, &workspace, &format!("Session {index}"), &[]); + } + let index = refreshed(&root); + + let two = index + .sessions(&query(ArchiveView::Active, SessionSort::Title, false, 2)) + .expect("two"); + assert_eq!((two.sessions.len(), two.total), (2, 5)); + let zero = index + .sessions(&query(ArchiveView::Active, SessionSort::Title, false, 0)) + .expect("zero clamps to one"); + assert_eq!((zero.sessions.len(), zero.total), (1, 5)); + let huge = index + .sessions(&query( + ArchiveView::Active, + SessionSort::Title, + false, + usize::MAX, + )) + .expect("huge clamps"); + assert_eq!((huge.sessions.len(), huge.total), (5, 5)); + let archived = list(&index, ArchiveView::Archived); + assert_eq!((archived.sessions.len(), archived.total), (0, 0)); +} + +#[test] +fn a_second_refresh_inspects_nothing_and_an_append_reindexes_only_that_journal() { + let (_temp, root, workspace) = setup(); + let mut paths = Vec::new(); + for index in 0..4 { + paths.push(session(&root, &workspace, &format!("S{index}"), &["first"]).1); + } + let mut index = SessionIndex::open(&root).expect("open"); + assert_eq!(index.refresh(1000).expect("first").inspected, 4); + + let second = index.refresh(1000).expect("second"); + assert_eq!( + (second.discovered, second.unchanged, second.inspected), + (4, 4, 0) + ); + assert_eq!(second.remaining, 0); + + append(&paths[2], "uniqueappended"); + let third = index.refresh(1000).expect("third"); + assert_eq!((third.inspected, third.unchanged), (1, 3)); + assert_eq!( + find(&index, "uniqueappended", ArchiveView::Active) + .hits + .len(), + 1 + ); +} + +#[test] +fn a_deleted_journal_leaves_the_listing_and_the_search() { + let (_temp, root, workspace) = setup(); + let (keep, _) = session(&root, &workspace, "Keep", &["keepword"]); + let (gone, gone_path) = session(&root, &workspace, "Gone", &["goneword"]); + let mut index = SessionIndex::open(&root).expect("open"); + index.refresh(1000).expect("first"); + assert_eq!(find(&index, "goneword", ArchiveView::Active).hits.len(), 1); + + fs::remove_file(&gone_path).expect("delete journal"); + let report = index.refresh(1000).expect("second"); + + assert_eq!( + (report.removed, report.discovered, report.unchanged), + (1, 1, 1) + ); + assert_eq!(ids(&list(&index, ArchiveView::Active)), vec![keep]); + assert!( + find(&index, "goneword", ArchiveView::Active) + .hits + .is_empty() + ); + assert_eq!(find(&index, "keepword", ArchiveView::Active).hits.len(), 1); + assert_eq!(index.session(gone).expect("lookup"), None); + assert_eq!(index.journal_path(gone).expect("path lookup"), None); +} + +#[test] +fn a_small_budget_leaves_remaining_work_and_repeated_calls_converge() { + let (_temp, root, workspace) = setup(); + for index in 0..5 { + session(&root, &workspace, &format!("S{index}"), &[]); + } + let mut index = SessionIndex::open(&root).expect("open"); + + let first = index.refresh(2).expect("first"); + assert_eq!((first.inspected, first.remaining), (2, 3)); + assert_eq!(list(&index, ArchiveView::Active).total, 2); + let second = index.refresh(2).expect("second"); + assert_eq!( + (second.inspected, second.unchanged, second.remaining), + (2, 2, 1) + ); + let third = index.refresh(2).expect("third"); + assert_eq!( + (third.inspected, third.unchanged, third.remaining), + (1, 4, 0) + ); + assert_eq!(list(&index, ArchiveView::Active).total, 5); + // A zero budget is clamped to one journal rather than doing nothing. + session(&root, &workspace, "Late", &[]); + assert_eq!(index.refresh(0).expect("clamped").inspected, 1); +} + +#[test] +fn archive_and_restore_are_reflected_after_a_refresh() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Cycle", &["body"]); + let mut index = SessionIndex::open(&root).expect("open"); + index.refresh(1000).expect("first"); + assert_eq!(ids(&list(&index, ArchiveView::Active)), vec![id]); + + SessionJournal::open(&path) + .expect("open") + .archive() + .expect("archive"); + index.refresh(1000).expect("after archive"); + assert!(list(&index, ArchiveView::Active).sessions.is_empty()); + assert_eq!(ids(&list(&index, ArchiveView::Archived)), vec![id]); + assert!(index.session(id).expect("lookup").expect("row").archived); + assert_eq!(find(&index, "body", ArchiveView::Archived).hits.len(), 1); + assert!(find(&index, "body", ArchiveView::Active).hits.is_empty()); + + SessionJournal::open(&path) + .expect("open") + .restore() + .expect("restore"); + index.refresh(1000).expect("after restore"); + assert_eq!(ids(&list(&index, ArchiveView::Active)), vec![id]); + assert!(list(&index, ArchiveView::Archived).sessions.is_empty()); +} + +#[test] +fn an_unfinished_turn_is_reported_as_the_active_turn() { + let (_temp, root, workspace) = setup(); + let mut journal = SessionJournal::create(&root, &workspace, "Busy turn").expect("create"); + let id = journal.snapshot().session_id; + let turn = journal.start_turn().expect("turn"); + drop(journal); + + let index = refreshed(&root); + + let indexed = index.session(id).expect("lookup").expect("row"); + assert_eq!(indexed.state, IndexedState::Ok); + assert_eq!(indexed.active_turn, Some(turn)); +} + +#[cfg(windows)] +#[test] +fn a_journal_held_by_a_writer_keeps_its_previous_data_and_is_retried() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Orchard", &["original apple"]); + let mut index = SessionIndex::open(&root).expect("open"); + index.refresh(1000).expect("first"); + let before = index.session(id).expect("lookup").expect("row"); + + let mut writer = SessionJournal::open(&path).expect("hold the writer lock"); + writer + .record_message(MessageRole::User, "added banana") + .expect("append"); + let held = index.refresh(1000).expect("refresh beside writer"); + assert_eq!((held.inspected, held.busy), (1, 1)); + let busy = index.session(id).expect("lookup").expect("row"); + assert_eq!(busy.state, IndexedState::Busy); + assert_eq!(busy.title, before.title); + assert_eq!(busy.updated_at, before.updated_at); + assert_eq!(busy.event_count, before.event_count); + assert_eq!(find(&index, "apple", ArchiveView::Active).hits.len(), 1); + assert!(find(&index, "banana", ArchiveView::Active).hits.is_empty()); + // A busy row is retried on every refresh, never treated as unchanged. + assert_eq!(index.refresh(1000).expect("again").inspected, 1); + + drop(writer); + let released = index.refresh(1000).expect("refresh after release"); + assert_eq!((released.inspected, released.busy), (1, 0)); + let ok = index.session(id).expect("lookup").expect("row"); + assert_eq!(ok.state, IndexedState::Ok); + assert_eq!(ok.event_count, before.event_count + 1); + assert_eq!(find(&index, "banana", ArchiveView::Active).hits.len(), 1); +} + +#[cfg(windows)] +#[test] +fn a_never_indexed_journal_held_by_a_writer_gets_a_minimal_busy_row() { + let (_temp, root, workspace) = setup(); + let writer = SessionJournal::create(&root, &workspace, "Held").expect("create"); + let id = writer.snapshot().session_id; + let mut index = SessionIndex::open(&root).expect("open"); + + let report = index.refresh(1000).expect("refresh"); + + assert_eq!((report.inspected, report.busy), (1, 1)); + let row = index.session(id).expect("lookup").expect("row"); + assert_eq!(row.state, IndexedState::Busy); + assert_eq!( + (row.title, row.workspace, row.created_at, row.updated_at), + (None, None, None, None) + ); + assert_eq!((row.archived, row.event_count), (false, 0)); + // A busy row with no data counts as active and sorts after known rows. + assert_eq!(ids(&list(&index, ArchiveView::Active)), vec![id]); + let refreshed_row = index + .refresh_session(id) + .expect("refresh one") + .expect("row"); + assert_eq!(refreshed_row.state, IndexedState::Busy); + drop(writer); + index.refresh(1000).expect("after release"); + assert_eq!( + index + .session(id) + .expect("lookup") + .expect("row") + .title + .as_deref(), + Some("Held") + ); +} + +#[cfg(unix)] +#[test] +fn inspection_beside_a_live_writer_indexes_the_journal() { + let (_temp, root, workspace) = setup(); + let mut writer = SessionJournal::create(&root, &workspace, "Live").expect("create"); + let id = writer.snapshot().session_id; + writer + .record_message(MessageRole::User, "written while held") + .expect("message"); + let mut index = SessionIndex::open(&root).expect("open"); + + let report = index.refresh(1000).expect("refresh"); + + assert_eq!((report.inspected, report.busy), (1, 0)); + assert_eq!( + index.session(id).expect("lookup").expect("row").state, + IndexedState::Ok + ); + assert_eq!(find(&index, "written", ArchiveView::Active).hits.len(), 1); +} + +#[test] +fn a_corrupt_journal_is_invalid_without_text_and_not_reinspected_until_it_changes() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Fragile", &["valuableword"]); + let valid = fs::read(&path).expect("valid bytes"); + let mut index = SessionIndex::open(&root).expect("open"); + index.refresh(1000).expect("first"); + assert_eq!( + find(&index, "valuableword", ArchiveView::Active).hits.len(), + 1 + ); + + fs::write(&path, b"this is not a journal\n").expect("corrupt"); + let corrupt = index.refresh(1000).expect("second"); + assert_eq!((corrupt.inspected, corrupt.invalid), (1, 1)); + let row = index.session(id).expect("lookup").expect("row"); + assert_eq!(row.state, IndexedState::Invalid); + let code = row.invalid_code.expect("code"); + assert!(code.starts_with("invalid_journal"), "{code}"); + assert_eq!( + (row.title, row.created_at, row.workspace), + (None, None, None) + ); + assert!( + find(&index, "valuableword", ArchiveView::Active) + .hits + .is_empty() + ); + assert!(find(&index, "Fragile", ArchiveView::Active).hits.is_empty()); + + let settled = index.refresh(1000).expect("third"); + assert_eq!( + (settled.inspected, settled.unchanged, settled.invalid), + (0, 1, 0) + ); + + fs::write(&path, &valid).expect("replace with a valid file"); + let fixed = index.refresh(1000).expect("fourth"); + assert_eq!((fixed.inspected, fixed.invalid), (1, 0)); + let row = index.session(id).expect("lookup").expect("row"); + assert_eq!(row.state, IndexedState::Ok); + assert_eq!(row.invalid_code, None); + assert_eq!( + find(&index, "valuableword", ArchiveView::Active).hits.len(), + 1 + ); +} + +#[test] +fn a_journal_whose_events_name_another_session_is_invalid() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Mismatch", &["text"]); + let other = Uuid::new_v4(); + let text = fs::read_to_string(&path) + .expect("journal text") + .replace(&id.to_string(), &other.to_string()); + // The file name still carries `id`; every event now carries `other`. + fs::write(&path, text).expect("rewrite"); + + let index = refreshed(&root); + + let row = index.session(id).expect("lookup").expect("row"); + assert_eq!(row.state, IndexedState::Invalid); + assert!(row.invalid_code.is_some()); + assert!(find(&index, "text", ArchiveView::Active).hits.is_empty()); +} + +#[test] +fn two_files_with_one_session_id_are_invalid_and_neither_is_indexed() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Twin", &["twinword"]); + let copy_directory = root.join("sessions/2020/01/01"); + fs::create_dir_all(©_directory).expect("directory"); + let copy = copy_directory.join(path.file_name().expect("name")); + fs::copy(&path, ©).expect("duplicate file"); + let mut index = SessionIndex::open(&root).expect("open"); + + let report = index.refresh(1000).expect("refresh"); + + assert_eq!( + (report.discovered, report.invalid, report.inspected), + (2, 1, 0) + ); + let row = index.session(id).expect("lookup").expect("row"); + assert_eq!(row.state, IndexedState::Invalid); + assert_eq!(row.invalid_code.as_deref(), Some("duplicate_session_id")); + assert!( + find(&index, "twinword", ArchiveView::Active) + .hits + .is_empty() + ); + assert!(matches!( + index.journal_path(id), + Err(IndexError::InvalidIndexEntry(_)) + )); + let settled = index.refresh(1000).expect("again"); + assert_eq!( + (settled.unchanged, settled.invalid, settled.inspected), + (1, 0, 0) + ); + + fs::remove_file(©).expect("remove the duplicate"); + let resolved = index.refresh(1000).expect("resolved"); + assert_eq!((resolved.inspected, resolved.invalid), (1, 0)); + assert_eq!( + index.session(id).expect("lookup").expect("row").state, + IndexedState::Ok + ); + assert_eq!(find(&index, "twinword", ArchiveView::Active).hits.len(), 1); +} + +#[test] +fn refresh_session_reinspects_one_session_regardless_of_size_and_time() { + let (_temp, root, workspace) = setup(); + let (id, _) = session(&root, &workspace, "Genuine title", &["body"]); + let mut index = SessionIndex::open(&root).expect("open"); + index.refresh(1000).expect("first"); + raw(&root) + .execute("UPDATE journal SET title = 'Tampered title'", []) + .expect("tamper"); + index.refresh(1000).expect("cache hit"); + assert_eq!( + index + .session(id) + .expect("lookup") + .expect("row") + .title + .as_deref(), + Some("Tampered title") + ); + + let row = index + .refresh_session(id) + .expect("refresh one") + .expect("row"); + + assert_eq!(row.title.as_deref(), Some("Genuine title")); + assert_eq!( + index.refresh_session(Uuid::new_v4()).expect("unknown"), + None + ); +} + +#[test] +fn refresh_session_forgets_a_vanished_journal() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Vanishing", &["vanishword"]); + let mut index = refreshed(&root); + fs::remove_file(path).expect("delete"); + + assert_eq!(index.refresh_session(id).expect("refresh one"), None); + + assert_eq!(index.session(id).expect("lookup"), None); + assert!( + find(&index, "vanishword", ArchiveView::Active) + .hits + .is_empty() + ); +} + +fn sample_index() -> (TempDir, PathBuf, SessionIndex) { + let (temp, root, workspace) = setup(); + session( + &root, + &workspace, + "Zebra planning", + &[ + "The quick brown fox jumps over the lazy dog", + "Caf\u{e9} r\u{e9}sum\u{e9} and na\u{ef}ve fa\u{e7}ade", + ], + ); + session(&root, &workspace, "Other", &["a only b", "text only"]); + let index = refreshed(&root); + (temp, root, index) +} + +#[test] +fn search_requires_every_term_and_matches_a_prefix_on_the_last_term_only() { + let (_temp, _root, index) = sample_index(); + let hits = |text: &str| find(&index, text, ArchiveView::Active).hits.len(); + + assert_eq!(hits("quick"), 1); + assert_eq!(hits("quick dog"), 1); + assert_eq!(hits("quick cat"), 0); + assert_eq!(hits("dog quick"), 1); + // The last term is a prefix; an earlier term must match a whole token. + assert_eq!(hits("qui"), 1); + assert_eq!(hits("brown fo"), 1); + assert_eq!(hits("qui brown"), 0); + // Case and diacritics do not matter. + assert_eq!(hits("FOX"), 1); + assert_eq!(hits("cafe"), 1); + assert_eq!(hits("RESUME"), 1); + assert_eq!(hits("naive facade"), 1); + assert_eq!(hits("caf\u{e9}"), 1); +} + +#[test] +fn title_hits_have_no_sequence_and_message_hits_carry_their_role_and_sequence() { + let (_temp, root, workspace) = setup(); + let mut journal = SessionJournal::create(&root, &workspace, "Zebra planning").expect("create"); + journal + .record_message(MessageRole::User, "usermarker line") + .expect("user"); + let turn = journal.start_turn().expect("turn"); + journal.record_manifest(turn, manifest()).expect("manifest"); + journal + .record_tool_request( + turn, + "call-1", + "workspace_read_file", + "{\"path\":\"a.txt\"}", + ) + .expect("request"); + let rendered = "toolmarker output"; + journal + .record_tool_result( + turn, + "call-1", + ToolOutcome::Succeeded, + None, + u64::try_from(rendered.len()).expect("size"), + &sha256_hex(rendered), + ) + .expect("result"); + journal + .record_tool_message("call-1", rendered) + .expect("tool message"); + journal + .record_message(MessageRole::Assistant, "assistantmarker reply") + .expect("assistant"); + journal.complete_turn(turn).expect("complete"); + journal + .record_message(MessageRole::User, "after the turn") + .expect("later user"); + let summary = "summarymarker of earlier work"; + journal + .record_compaction(CompactionRecord { + summary: summary.into(), + compacted_messages: 3, + includes_tool_results: true, + summary_sha256: sha256_hex(summary), + model: "fixture".into(), + prompt_tokens: None, + completion_tokens: None, + }) + .expect("compaction"); + let path = journal.path().to_path_buf(); + drop(journal); + let inspection = SessionInspection::inspect(&path).expect("inspect"); + let index = refreshed(&root); + + let title = &find(&index, "zebra", ArchiveView::Active).hits; + assert_eq!( + (title.len(), title[0].seq, title[0].role), + (1, None, SearchRole::Title) + ); + for (term, role) in [ + ("usermarker", SearchRole::User), + ("toolmarker", SearchRole::Tool), + ("assistantmarker", SearchRole::Assistant), + ("summarymarker", SearchRole::Summary), + ] { + let hits = find(&index, term, ArchiveView::Active).hits; + assert_eq!(hits.len(), 1, "{term}"); + assert_eq!(hits[0].role, role, "{term}"); + let seq = hits[0].seq.expect("message hits have a sequence"); + assert!( + inspection.entries.iter().any(|entry| entry.seq == seq), + "{term}" + ); + } + // Tool-call arguments and manifests are not indexed. + for unindexed in [ + "workspace_read_file", + "a.txt", + "CUSTOM-INSTRUCTIONS-TEXT", + "local-model", + ] { + assert!( + find(&index, unindexed, ArchiveView::Active).hits.is_empty(), + "{unindexed}" + ); + } +} + +#[test] +fn search_reports_truncation_honors_the_view_and_clamps_the_limit() { + let (_temp, root, workspace) = setup(); + for number in 0..5 { + session( + &root, + &workspace, + &format!("S{number}"), + &["commonword here"], + ); + } + let (_, archived_path) = session(&root, &workspace, "Old", &["commonword archived"]); + SessionJournal::open(&archived_path) + .expect("open") + .archive() + .expect("archive"); + let many: Vec = (0..205).map(|n| format!("needleword {n}")).collect(); + let many_refs: Vec<&str> = many.iter().map(String::as_str).collect(); + session(&root, &workspace, "Many", &many_refs); + let index = refreshed(&root); + + let three = index + .search("commonword", ArchiveView::Active, 3) + .expect("three"); + assert_eq!((three.hits.len(), three.truncated), (3, true)); + let all = index + .search("commonword", ArchiveView::Active, 5) + .expect("five"); + assert_eq!((all.hits.len(), all.truncated), (5, false)); + let zero = index + .search("commonword", ArchiveView::Active, 0) + .expect("zero"); + assert_eq!((zero.hits.len(), zero.truncated), (1, true)); + let archived = index + .search("commonword", ArchiveView::Archived, 10) + .expect("archived"); + assert_eq!(archived.hits.len(), 1); + assert!(archived.hits[0].session.archived); + let clamped = index + .search("needleword", ArchiveView::Active, usize::MAX) + .expect("clamp"); + assert_eq!((clamped.hits.len(), clamped.truncated), (200, true)); +} + +#[test] +fn unusable_search_text_is_rejected_with_a_typed_error() { + let (_temp, _root, index) = sample_index(); + for text in ["", " ", "\t\n", &"x".repeat(257), &"a ".repeat(17)] { + assert!( + matches!( + index.search(text, ArchiveView::Active, 10), + Err(IndexError::InvalidQuery(_)) + ), + "{text:?}" + ); + } + assert!( + index + .search(&"x".repeat(256), ArchiveView::Active, 10) + .is_ok() + ); +} + +#[test] +fn hostile_search_text_never_errors_and_is_matched_literally() { + // Given one session that contains the operator text and a control that does not. + let (_temp, root, workspace) = setup(); + session( + &root, + &workspace, + "Literal", + &[ + "x a OR b y", + "x a AND b y", + "x NEAR(a b) y", + "x text:a y", + "x (a) OR (b y", + ], + ); + session(&root, &workspace, "Control", &["a only b", "text only"]); + let index = refreshed(&root); + let control_title = |hits: &SearchListing| { + hits.hits + .iter() + .any(|hit| hit.session.title.as_deref() == Some("Control")) + }; + + // Every hostile string is accepted and never reaches FTS5 as syntax. + let hostile = [ + "\"", + "\"\"", + "a\"b", + "NEAR(a b)", + "a OR b", + "a AND b", + "-a", + "^a", + "a*", + "text:a", + "(", + ")", + "a) OR (b", + "a NOT b", + "{text}: a", + "a + b", + "*", + "NEAR/2", + &"z".repeat(256), + ]; + for text in hostile { + let result = index.search(text, ArchiveView::Active, 50); + assert!(result.is_ok(), "{text:?}: {result:?}"); + } + // Operator words are plain terms: all must be present, so the control + // session (which has no `or`, `and`, `near`, or `text:` run) cannot match. + for text in ["a OR b", "a AND b", "NEAR(a b)", "text:a", "a) OR (b"] { + let hits = find(&index, text, ArchiveView::Active); + assert!(!control_title(&hits), "{text}"); + assert!( + !hits.hits.is_empty(), + "{text} should match its literal text" + ); + } + assert!(find(&index, "zzzz", ArchiveView::Active).hits.is_empty()); +} + +#[test] +fn snippets_mark_the_matched_terms_and_reconstruct_a_substring_of_the_source() { + let (_temp, root, workspace) = setup(); + let source = "The quick brown fox jumps over the lazy dog"; + let long: String = + (0..300).map(|n| format!("filler{n} ")).collect::() + "needle " + "tail"; + session(&root, &workspace, "Snippets", &[source, &long]); + let index = refreshed(&root); + + let hits = find(&index, "brown fox", ArchiveView::Active).hits; + assert_eq!(hits.len(), 1); + let parts = &hits[0].snippet; + let joined: String = parts.iter().map(|p| p.text.as_str()).collect(); + assert!(source.contains(&joined), "{joined:?}"); + let matched: Vec<&str> = parts + .iter() + .filter(|p| p.matched) + .map(|p| p.text.as_str()) + .collect(); + assert_eq!(matched, ["brown", "fox"]); + assert!( + parts + .iter() + .all(|p| !p.text.contains(['\u{e000}', '\u{e001}'])) + ); + + let hits = find(&index, "needle", ArchiveView::Active).hits; + let parts = &hits[0].snippet; + let joined: String = parts.iter().map(|p| p.text.as_str()).collect(); + assert!(long.contains(&joined)); + assert!( + joined.len() < long.len() / 4, + "the snippet is a window, not the whole text" + ); + assert_eq!( + parts + .iter() + .filter(|p| p.matched) + .map(|p| p.text.as_str()) + .collect::>(), + ["needle"] + ); +} + +#[test] +fn transcript_text_cannot_forge_a_snippet_match() { + let (_temp, root, workspace) = setup(); + let hostile = "before \u{e000}zebra\u{e001} after marker \u{e001}\u{e000} end"; + session(&root, &workspace, "Spoof", &[hostile]); + let index = refreshed(&root); + + let hits = find(&index, "marker", ArchiveView::Active).hits; + + assert_eq!(hits.len(), 1); + let parts = &hits[0].snippet; + assert!( + parts + .iter() + .all(|p| !p.text.contains(['\u{e000}', '\u{e001}'])) + ); + let matched: Vec<&str> = parts + .iter() + .filter(|p| p.matched) + .map(|p| p.text.as_str()) + .collect(); + assert_eq!(matched, ["marker"]); + // The spoofed word is plain text, not a highlight. + assert!(parts.iter().any(|p| !p.matched && p.text.contains("zebra"))); + // The markers were replaced, so they cannot be searched for either. + assert!( + find(&index, "\u{e000}zebra", ArchiveView::Active) + .hits + .len() + <= 1 + ); +} + +#[test] +fn sql_metacharacters_in_titles_and_messages_are_stored_and_found_literally() { + let (_temp, root, workspace) = setup(); + let hostile = "'; DROP TABLE journal;--"; + let (id, _) = session( + &root, + &workspace, + hostile, + &[hostile, "x'); DELETE FROM entry_text;--"], + ); + session(&root, &workspace, "Another", &["still here"]); + let index = refreshed(&root); + + let row = index.session(id).expect("lookup").expect("row"); + assert_eq!(row.title.as_deref(), Some(hostile)); + assert_eq!(list(&index, ArchiveView::Active).total, 2); + let hits = find(&index, "DROP TABLE journal", ArchiveView::Active).hits; + assert_eq!(hits.len(), 2); + assert_eq!( + find(&index, "still here", ArchiveView::Active).hits.len(), + 1 + ); + assert_eq!( + find(&index, "DELETE FROM entry_text", ArchiveView::Active) + .hits + .len(), + 1 + ); +} + +#[test] +fn deleting_the_index_directory_rebuilds_identical_listings_and_leaves_journals_alone() { + let (_temp, root, workspace) = setup(); + let mut paths = Vec::new(); + for (title, body) in [ + ("One", "firstbody"), + ("Two", "secondbody"), + ("Three", "thirdbody"), + ] { + paths.push(session(&root, &workspace, title, &[body]).1); + } + let bytes_before = read_all(&paths); + let index = refreshed(&root); + let listing_before = list(&index, ArchiveView::Active); + let hits_before = find(&index, "secondbody", ArchiveView::Active); + drop(index); + + fs::remove_dir_all(root.join(INDEX_DIRECTORY)).expect("delete the whole index"); + let mut rebuilt = SessionIndex::open(&root).expect("reopen"); + assert_eq!(list(&rebuilt, ArchiveView::Active).total, 0); + let report = rebuilt.refresh(1000).expect("refresh"); + + assert_eq!(report.inspected, 3); + assert_eq!(list(&rebuilt, ArchiveView::Active), listing_before); + assert_eq!( + find(&rebuilt, "secondbody", ArchiveView::Active), + hits_before + ); + assert_eq!(read_all(&paths), bytes_before); +} + +#[test] +fn a_garbage_database_is_replaced_without_touching_anything_else() { + let (_temp, root, workspace) = setup(); + let (_, path) = session(&root, &workspace, "Survivor", &["survivorword"]); + let bytes_before = fs::read(&path).expect("bytes"); + drop(refreshed(&root)); + let sibling = root.join(INDEX_DIRECTORY).join("keep.txt"); + fs::write(&sibling, "not an index file").expect("sibling"); + fs::write(database_path(&root), vec![b'x'; 4096]).expect("overwrite with garbage"); + + let mut index = SessionIndex::open(&root).expect("open recovers"); + assert_eq!(list(&index, ArchiveView::Active).total, 0); + assert_eq!(index.refresh(1000).expect("refresh").inspected, 1); + + assert_eq!( + find(&index, "survivorword", ArchiveView::Active).hits.len(), + 1 + ); + assert_eq!( + fs::read(&sibling).expect("sibling kept"), + b"not an index file" + ); + assert_eq!(fs::read(&path).expect("journal"), bytes_before); +} + +#[test] +fn a_different_user_version_or_application_id_rebuilds_the_index() { + let (_temp, root, workspace) = setup(); + session(&root, &workspace, "Versioned", &["versionword"]); + for (pragma, value) in [ + ("user_version", 2_i64), + ("user_version", 0), + ("user_version", 99), + ("application_id", 12_345), + ("application_id", 0), + ] { + drop(refreshed(&root)); + raw(&root) + .pragma_update(None, pragma, value) + .expect("change the header"); + + let mut index = SessionIndex::open(&root).expect("open recovers"); + assert_eq!( + list(&index, ArchiveView::Active).total, + 0, + "{pragma}={value}" + ); + assert_eq!( + index.refresh(1000).expect("refresh").inspected, + 1, + "{pragma}={value}" + ); + assert_eq!( + find(&index, "versionword", ArchiveView::Active).hits.len(), + 1 + ); + drop(index); + let connection = raw(&root); + let id: i32 = connection + .pragma_query_value(None, "application_id", |row| row.get(0)) + .expect("application_id"); + let version: i64 = connection + .pragma_query_value(None, "user_version", |row| row.get(0)) + .expect("user_version"); + assert_eq!((id, version), (APPLICATION_ID, 1)); + } +} + +#[test] +fn a_database_with_the_right_header_but_no_tables_is_rebuilt() { + let (_temp, root, workspace) = setup(); + session(&root, &workspace, "Hollow", &["hollowword"]); + drop(refreshed(&root)); + { + let connection = raw(&root); + connection + .execute_batch("DROP TABLE entry_text; DROP TABLE journal;") + .expect("drop tables"); + } + + let mut index = SessionIndex::open(&root).expect("open recovers"); + index.refresh(1000).expect("refresh"); + + assert_eq!( + find(&index, "hollowword", ArchiveView::Active).hits.len(), + 1 + ); +} + +#[test] +fn journal_path_resolves_a_real_session_to_a_path_a_writer_can_open() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Resolve", &["body"]); + let path = move_to_day(&root, &path, "2025/03/04"); + let index = refreshed(&root); + + let resolved = index.journal_path(id).expect("resolve").expect("known"); + + assert_eq!( + resolved, + root.join("sessions/2025/03/04") + .join(path.file_name().expect("name")) + ); + let writer = SessionJournal::open(&resolved).expect("writer opens the resolved path"); + assert_eq!(writer.snapshot().session_id, id); + assert_eq!(index.journal_path(Uuid::new_v4()).expect("unknown"), None); +} + +#[test] +fn a_tampered_stored_path_is_rejected_with_a_typed_error() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Tamper", &["body"]); + let file = path + .file_name() + .expect("name") + .to_string_lossy() + .into_owned(); + let other = Uuid::new_v4(); + let index = refreshed(&root); + let connection = raw(&root); + let tampered = [ + "../../evil.jsonl".to_owned(), + format!("{}/evil.jsonl", root.display()), + "/etc/passwd".to_owned(), + "C:/Windows/evil.jsonl".to_owned(), + format!("sessions\\2026\\01\\02\\{file}"), + format!("sessions/2026/01/02/omgcode-01-02-03-004Z-{other}.jsonl"), + format!("sessions/2026/01/02/extra/{file}"), + format!("sessions/2026/01/../02/{file}"), + format!("sessions/2026/01/{file}"), + format!("sessions/2026/01/02/{file}/"), + String::new(), + ]; + // A failure names the case by its position in the list above. Some of the + // paths are built from a session identifier, and a test does not print one. + for (case, value) in tampered.into_iter().enumerate() { + connection + .execute( + "UPDATE journal SET relative_path = ?1 WHERE session_id = ?2", + rusqlite::params![value, id.to_string()], + ) + .expect("tamper"); + assert!( + matches!( + index.journal_path(id), + Err(IndexError::InvalidIndexEntry(_)) + ), + "tampered path case {case} was accepted" + ); + } +} + +#[test] +fn a_refresh_repairs_a_tampered_stored_path() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Repair", &["body"]); + let mut index = refreshed(&root); + raw(&root) + .execute("UPDATE journal SET relative_path = '../../evil.jsonl'", []) + .expect("tamper"); + assert!(index.journal_path(id).is_err()); + + let report = index.refresh(1000).expect("refresh"); + + assert_eq!(report.inspected, 1); + assert_eq!(index.journal_path(id).expect("resolve"), Some(path)); +} + +#[test] +fn an_index_directory_that_is_a_link_is_refused() { + let (temp, root, _workspace) = setup(); + fs::create_dir_all(&root).expect("root"); + let elsewhere = temp.path().join("elsewhere"); + fs::create_dir_all(&elsewhere).expect("target"); + if !try_symlink_dir(&elsewhere, &root.join(INDEX_DIRECTORY)) { + return; + } + + let result = SessionIndex::open(&root); + + assert!( + matches!(result, Err(IndexError::UnsafeLocation(_))), + "{result:?}" + ); + assert_eq!(fs::read_dir(&elsewhere).expect("target").count(), 0); +} + +#[test] +fn a_database_file_that_is_a_link_is_refused() { + let (temp, root, _workspace) = setup(); + let directory = root.join(INDEX_DIRECTORY); + fs::create_dir_all(&directory).expect("index directory"); + let target = temp.path().join("target.sqlite3"); + fs::write(&target, b"untouched").expect("target"); + if !try_symlink_file(&target, &directory.join(INDEX_FILE)) { + return; + } + + let result = SessionIndex::open(&root); + + assert!( + matches!(result, Err(IndexError::UnsafeLocation(_))), + "{result:?}" + ); + assert_eq!(fs::read(&target).expect("target"), b"untouched"); +} + +#[test] +fn contention_past_the_busy_timeout_is_a_distinct_busy_error() { + let (_temp, root, workspace) = setup(); + session(&root, &workspace, "Locked", &[]); + let index = refreshed(&root); + let connection = raw(&root); + connection + .execute_batch("BEGIN EXCLUSIVE") + .expect("exclusive lock"); + + let result = index.sessions(&query(ArchiveView::Active, SessionSort::Title, false, 10)); + + assert!(matches!(result, Err(IndexError::Busy)), "{result:?}"); + connection.execute_batch("ROLLBACK").expect("release"); + assert_eq!(list(&index, ArchiveView::Active).total, 1); +} + +#[test] +fn the_workspace_path_round_trips_losslessly_through_the_stored_units() { + let (_temp, root, workspace) = setup(); + let (id, path) = session(&root, &workspace, "Units", &[]); + let inspection = SessionInspection::inspect(&path).expect("inspect"); + let index = refreshed(&root); + + let row = index.session(id).expect("lookup").expect("row"); + + assert_eq!(row.workspace, Some(inspection.workspace.clone())); + match row.workspace.expect("workspace") { + NativePath::WindowsUtf16(units) => assert!(!units.is_empty()), + NativePath::UnixBytes(bytes) => assert!(!bytes.is_empty()), + } +} + +#[test] +fn the_bundled_sqlite_has_fts5_and_refuses_sql_extension_loading() { + let connection = Connection::open_in_memory().expect("in-memory database"); + connection + .execute_batch( + "CREATE VIRTUAL TABLE probe USING fts5(text, tokenize = 'unicode61 remove_diacritics 2'); + INSERT INTO probe (text) VALUES ('Caf\u{e9} au lait');", + ) + .expect("FTS5 is compiled in"); + let hits: i64 = connection + .query_row( + "SELECT count(*) FROM probe WHERE probe MATCH 'cafe'", + [], + |row| row.get(0), + ) + .expect("match"); + assert_eq!(hits, 1); + let version: String = connection + .query_row("SELECT sqlite_version()", [], |row| row.get(0)) + .expect("version"); + eprintln!("bundled SQLite version: {version}"); + assert!( + connection + .query_row("SELECT load_extension('no-such-extension')", [], |_| Ok(())) + .is_err() + ); +} + +// Link helpers, as in `tests/session_inspection.rs`. + +fn try_symlink_file(target: &Path, link: &Path) -> bool { + #[cfg(unix)] + let result = std::os::unix::fs::symlink(target, link); + #[cfg(windows)] + let result = std::os::windows::fs::symlink_file(target, link); + report_symlink(result) +} + +fn try_symlink_dir(target: &Path, link: &Path) -> bool { + #[cfg(unix)] + let result = std::os::unix::fs::symlink(target, link); + #[cfg(windows)] + let result = std::os::windows::fs::symlink_dir(target, link).or_else(|_| { + // A junction needs no privilege and is also a reparse point. + let status = std::process::Command::new("cmd") + .arg("/C") + .arg("mklink") + .arg("/J") + .arg(link) + .arg(target) + .stdout(std::process::Stdio::null()) + .status()?; + if status.success() { + Ok(()) + } else { + Err(std::io::Error::other("mklink /J failed")) + } + }); + report_symlink(result) +} + +fn report_symlink(result: std::io::Result<()>) -> bool { + match result { + Ok(()) => true, + Err(error) => { + eprintln!("SKIPPED link assertions: this host cannot create a symlink: {error}"); + false + } + } +} diff --git a/crates/omgcode-core/tests/session_inspection.rs b/crates/omgcode-core/tests/session_inspection.rs new file mode 100644 index 0000000..00dc421 --- /dev/null +++ b/crates/omgcode-core/tests/session_inspection.rs @@ -0,0 +1,678 @@ +//! Native tests for read-only journal inspection, discovery, and resolution. + +use std::fs::{self, OpenOptions}; +use std::io::Write; +use std::path::{Path, PathBuf}; + +use omgcode_core::session::{ + CompactionRecord, MessageRole, MessageSource, RequestManifest, SessionError, SessionInspection, + SessionJournal, ToolOutcome, TranscriptEntryKind, discover, locate, +}; +use sha2::{Digest, Sha256}; +use tempfile::TempDir; +use uuid::Uuid; + +fn setup() -> (TempDir, PathBuf, PathBuf) { + let temp = TempDir::new().expect("temporary directory"); + let root = temp.path().join("app-data"); + let workspace = temp.path().join("workspace"); + fs::create_dir_all(&workspace).expect("workspace directory"); + (temp, root, workspace) +} + +fn sha256_hex(text: &str) -> String { + Sha256::digest(text.as_bytes()) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} + +fn manifest() -> RequestManifest { + RequestManifest { + base_prompt: "BASE-PROMPT-TEXT".into(), + prompt_version: "7".into(), + prompt_sha256: "a".repeat(64), + custom_instructions: "SECRET-CUSTOM-INSTRUCTIONS".into(), + model: "local-model".into(), + base_url: "http://127.0.0.1:1234/v1".into(), + context_tokens: 32_000, + max_output_tokens: 2_000, + tool_schema_version: Some("1".into()), + tools: Some(vec!["workspace_read_file".into(), "workspace_list".into()]), + } +} + +/// Create a small idle journal with `messages` user messages and release it. +fn small_journal(root: &Path, workspace: &Path, messages: usize) -> PathBuf { + let mut journal = SessionJournal::create(root, workspace, "Small").expect("create"); + for index in 0..messages { + journal + .record_message(MessageRole::User, &format!("message {index}")) + .expect("message"); + } + journal.path().to_path_buf() +} + +/// Replace the journal's text with `edit(text)`. +fn rewrite(path: &Path, edit: impl FnOnce(String) -> String) { + let text = fs::read_to_string(path).expect("journal text"); + fs::write(path, edit(text)).expect("rewrite journal"); +} + +/// Create a file symlink. Returns false, after saying so, when this host does +/// not allow the calling account to create one. +fn try_symlink_file(target: &Path, link: &Path) -> bool { + #[cfg(unix)] + let result = std::os::unix::fs::symlink(target, link); + #[cfg(windows)] + let result = std::os::windows::fs::symlink_file(target, link); + report_symlink(result) +} + +fn try_symlink_dir(target: &Path, link: &Path) -> bool { + #[cfg(unix)] + let result = std::os::unix::fs::symlink(target, link); + #[cfg(windows)] + let result = std::os::windows::fs::symlink_dir(target, link).or_else(|_| { + // A junction needs no privilege and is also a reparse point. + let status = std::process::Command::new("cmd") + .arg("/C") + .arg("mklink") + .arg("/J") + .arg(link) + .arg(target) + .stdout(std::process::Stdio::null()) + .status()?; + if status.success() { + Ok(()) + } else { + Err(std::io::Error::other("mklink /J failed")) + } + }); + report_symlink(result) +} + +fn report_symlink(result: std::io::Result<()>) -> bool { + match result { + Ok(()) => true, + Err(error) => { + eprintln!("SKIPPED link assertions: this host cannot create a symlink: {error}"); + false + } + } +} + +#[test] +fn inspection_reports_header_and_every_recorded_event() { + let (_temp, root, workspace) = setup(); + let mut journal = + SessionJournal::create(&root, &workspace, "Inspect everything").expect("create"); + let path = journal.path().to_path_buf(); + let workspace_id = journal.snapshot().workspace_id; + let session_id = journal.snapshot().session_id; + let native_workspace = journal.snapshot().workspace.clone(); + + journal + .record_message(MessageRole::User, "hello") + .expect("user"); + let first = journal.start_turn().expect("turn one"); + journal + .record_manifest(first, manifest()) + .expect("manifest"); + journal + .record_tool_request( + first, + "call-1", + "workspace_read_file", + "{\"path\":\"a.txt\"}", + ) + .expect("tool request"); + let rendered = "file contents"; + journal + .record_tool_result( + first, + "call-1", + ToolOutcome::Succeeded, + None, + u64::try_from(rendered.len()).expect("size"), + &sha256_hex(rendered), + ) + .expect("tool result"); + journal + .record_tool_message("call-1", rendered) + .expect("tool message"); + journal + .record_message(MessageRole::Assistant, "done") + .expect("assistant"); + journal.complete_turn(first).expect("complete"); + journal + .record_message(MessageRole::User, "second") + .expect("second user"); + let second = journal.start_turn().expect("turn two"); + journal + .fail_turn(second, "provider_unreachable") + .expect("fail"); + let summary = "Earlier work read a file."; + let record = CompactionRecord { + summary: summary.into(), + compacted_messages: 3, + includes_tool_results: true, + summary_sha256: sha256_hex(summary), + model: "fixture".into(), + prompt_tokens: Some(10), + completion_tokens: Some(4), + }; + journal.record_compaction(record.clone()).expect("compact"); + journal.archive().expect("archive"); + journal.restore().expect("restore"); + drop(journal); + + let inspection = SessionInspection::inspect(&path).expect("inspect"); + + assert_eq!(inspection.session_id, session_id); + assert_eq!(inspection.workspace_id, workspace_id); + assert_eq!(inspection.workspace, native_workspace); + assert_eq!(inspection.title, "Inspect everything"); + assert!(!inspection.archived); + assert_eq!(inspection.active_turn, None); + assert_eq!(inspection.schema_version, 3); + assert_eq!(inspection.event_count, 15); + assert!(!inspection.incomplete_tail); + assert!(inspection.created_at <= inspection.updated_at); + let last = inspection.entries.last().expect("entries"); + assert_eq!(inspection.updated_at, last.timestamp); + + let seqs: Vec = inspection.entries.iter().map(|entry| entry.seq).collect(); + assert_eq!(seqs, (1..=14).collect::>()); + let kinds: Vec<&TranscriptEntryKind> = + inspection.entries.iter().map(|entry| &entry.kind).collect(); + let expected = vec![ + TranscriptEntryKind::Message { + role: MessageRole::User, + source: MessageSource::UserCli, + text: "hello".into(), + call_id: None, + }, + TranscriptEntryKind::TurnStarted { request_id: first }, + TranscriptEntryKind::RequestManifest { + request_id: first, + model: "local-model".into(), + prompt_version: "7".into(), + tools: vec!["workspace_read_file".into(), "workspace_list".into()], + }, + TranscriptEntryKind::ToolRequested { + request_id: first, + call_id: "call-1".into(), + tool: "workspace_read_file".into(), + arguments: "{\"path\":\"a.txt\"}".into(), + }, + TranscriptEntryKind::ToolCompleted { + request_id: first, + call_id: "call-1".into(), + outcome: ToolOutcome::Succeeded, + code: None, + output_bytes: 13, + output_sha256: sha256_hex(rendered), + }, + TranscriptEntryKind::Message { + role: MessageRole::Tool, + source: MessageSource::CoreTool, + text: rendered.into(), + call_id: Some("call-1".into()), + }, + TranscriptEntryKind::Message { + role: MessageRole::Assistant, + source: MessageSource::Provider, + text: "done".into(), + call_id: None, + }, + TranscriptEntryKind::TurnCompleted { request_id: first }, + TranscriptEntryKind::Message { + role: MessageRole::User, + source: MessageSource::UserCli, + text: "second".into(), + call_id: None, + }, + TranscriptEntryKind::TurnStarted { request_id: second }, + TranscriptEntryKind::TurnFailed { + request_id: second, + code: "provider_unreachable".into(), + }, + TranscriptEntryKind::ContextCompacted { record }, + TranscriptEntryKind::SessionArchived, + TranscriptEntryKind::SessionRestored, + ]; + assert_eq!(kinds, expected.iter().collect::>()); + + // The serialized form tags each entry, uses RFC 3339 times, and leaves the + // base prompt and custom instructions out of the manifest entry. + let json = serde_json::to_value(&inspection).expect("serialize"); + assert_eq!(json["entries"][0]["kind"], "message"); + assert_eq!(json["entries"][2]["kind"], "request_manifest"); + assert_eq!(json["entries"][10]["kind"], "turn_failed"); + assert!( + json["created_at"] + .as_str() + .is_some_and(|text| text.ends_with('Z')) + ); + let text = json.to_string(); + assert!(!text.contains("BASE-PROMPT-TEXT")); + assert!(!text.contains("SECRET-CUSTOM-INSTRUCTIONS")); +} + +#[test] +fn inspection_reports_an_active_turn_and_an_archived_session() { + let (_temp, root, workspace) = setup(); + let mut journal = SessionJournal::create(&root, &workspace, "States").expect("create"); + let path = journal.path().to_path_buf(); + let request = journal.start_turn().expect("turn"); + drop(journal); + let active = SessionInspection::inspect(&path).expect("inspect active"); + assert_eq!(active.active_turn, Some(request)); + assert!(!active.archived); + + let mut journal = SessionJournal::open(&path).expect("reopen"); + journal.complete_turn(request).expect("complete"); + journal.archive().expect("archive"); + drop(journal); + let archived = SessionInspection::inspect(&path).expect("inspect archived"); + assert!(archived.archived); + assert_eq!(archived.active_turn, None); +} + +#[cfg(windows)] +#[test] +fn inspection_of_a_live_writer_is_busy_and_disturbs_nothing() { + let (_temp, root, workspace) = setup(); + let mut journal = SessionJournal::create(&root, &workspace, "Live").expect("create"); + let path = journal.path().to_path_buf(); + for _ in 0..5 { + assert!(matches!( + SessionInspection::inspect(&path), + Err(SessionError::Busy) + )); + } + // The writer is unaffected, and the observer did not take the lock. + journal + .record_message(MessageRole::User, "still appending") + .expect("append after refused inspections"); + drop(journal); + let inspection = SessionInspection::inspect(&path).expect("inspect after release"); + assert_eq!(inspection.event_count, 2); +} + +#[cfg(unix)] +#[test] +fn inspection_of_a_live_writer_succeeds_and_disturbs_nothing() { + let (_temp, root, workspace) = setup(); + let mut journal = SessionJournal::create(&root, &workspace, "Live").expect("create"); + let path = journal.path().to_path_buf(); + let inspection = SessionInspection::inspect(&path).expect("inspect beside a writer"); + assert_eq!(inspection.event_count, 1); + journal + .record_message(MessageRole::User, "still appending") + .expect("append after inspection"); + assert_eq!( + SessionInspection::inspect(&path) + .expect("again") + .event_count, + 2 + ); +} + +#[test] +fn a_writer_opens_immediately_after_many_inspections() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 2); + for _ in 0..200 { + SessionInspection::inspect(&path).expect("inspect idle journal"); + // An inspection that returned must leave no lock behind. + drop(SessionJournal::open(&path).expect("writer opens right after an inspection")); + } + for _ in 0..200 { + SessionInspection::inspect(&path).expect("inspect idle journal"); + } + let mut writer = SessionJournal::open(&path).expect("writer opens after the loop"); + writer + .record_message(MessageRole::Assistant, "ok") + .expect("append"); +} + +#[test] +fn an_incomplete_tail_is_ignored_without_changing_the_file() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 2); + let mut file = OpenOptions::new().append(true).open(&path).expect("append"); + file.write_all(b"{\"schema_version\":3,\"session_id\":\"partial") + .expect("partial record"); + drop(file); + let before = fs::read(&path).expect("bytes"); + + let inspection = SessionInspection::inspect(&path).expect("inspect with tail"); + assert!(inspection.incomplete_tail); + assert_eq!(inspection.event_count, 3); + assert_eq!(inspection.entries.len(), 2); + assert_eq!(fs::read(&path).expect("bytes after"), before); + + // The writer path is unchanged: it still refuses the incomplete tail. + assert!(matches!( + SessionJournal::open(&path), + Err(SessionError::InvalidJournal(_)) + )); + assert_eq!(fs::read(&path).expect("bytes after open"), before); + // And that refused open released its lock. + assert!(SessionInspection::inspect(&path).is_ok()); +} + +#[test] +fn a_journal_with_no_complete_record_is_invalid() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 0); + let file = OpenOptions::new().write(true).open(&path).expect("open"); + file.set_len(20) + .expect("truncate to a partial first record"); + drop(file); + assert!(matches!( + SessionInspection::inspect(&path), + Err(SessionError::InvalidJournal(_)) + )); + fs::write(&path, b"").expect("empty file"); + assert!(matches!( + SessionInspection::inspect(&path), + Err(SessionError::InvalidJournal(_)) + )); +} + +#[test] +fn interior_corruption_is_rejected() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 3); + rewrite(&path, |text| { + let mut lines: Vec<&str> = text.lines().collect(); + lines[1] = "{not json}"; + lines.join("\n") + "\n" + }); + assert!(matches!( + SessionInspection::inspect(&path), + Err(SessionError::InvalidJournal(_)) + )); +} + +#[test] +fn duplicate_keys_are_rejected() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 2); + rewrite(&path, |text| { + text.replacen("\"seq\":1,", "\"seq\":1,\"seq\":1,", 1) + }); + assert!(matches!( + SessionInspection::inspect(&path), + Err(SessionError::InvalidJournal(_)) + )); +} + +#[test] +fn a_session_id_that_differs_from_the_filename_is_rejected() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 1); + let name = path + .file_name() + .and_then(|name| name.to_str()) + .expect("name"); + let (stamp, _) = name.rsplit_once("Z-").expect("stamp"); + let renamed = path.with_file_name(format!("{stamp}Z-{}.jsonl", Uuid::new_v4())); + fs::rename(&path, &renamed).expect("rename"); + assert!(matches!( + SessionInspection::inspect(&renamed), + Err(SessionError::InvalidJournal(_)) + )); +} + +#[test] +fn a_non_contiguous_sequence_is_rejected() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 3); + rewrite(&path, |text| text.replacen("\"seq\":2,", "\"seq\":5,", 1)); + assert!(matches!( + SessionInspection::inspect(&path), + Err(SessionError::InvalidJournal(_)) + )); +} + +#[test] +fn an_oversized_journal_is_rejected_without_reading_it_all() { + let (_temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 1); + let mut file = OpenOptions::new().append(true).open(&path).expect("append"); + file.write_all(&vec![b'a'; 9 * 1024 * 1024]) + .expect("padding"); + drop(file); + assert!(matches!( + SessionInspection::inspect(&path), + Err(SessionError::InvalidJournal(_)) + )); +} + +#[test] +fn a_symlinked_journal_is_rejected() { + let (temp, root, workspace) = setup(); + let path = small_journal(&root, &workspace, 1); + let name = path.file_name().expect("name"); + let alias_directory = temp.path().join("alias"); + fs::create_dir(&alias_directory).expect("alias directory"); + let alias = alias_directory.join(name); + if !try_symlink_file(&path, &alias) { + return; + } + assert!(matches!( + SessionInspection::inspect(&alias), + Err(SessionError::InvalidJournal(_)) + )); +} + +#[test] +fn an_unnamed_or_missing_journal_is_a_controlled_failure() { + let (temp, _root, _workspace) = setup(); + let bad_name = temp.path().join("not-a-journal.jsonl"); + fs::write(&bad_name, b"").expect("file"); + assert!(matches!( + SessionInspection::inspect(&bad_name), + Err(SessionError::InvalidJournal(_)) + )); + let missing = temp + .path() + .join(format!("omgcode-00-00-00-000Z-{}.jsonl", Uuid::new_v4())); + assert!(matches!( + SessionInspection::inspect(&missing), + Err(SessionError::Io(_)) + )); +} + +fn journal_name(id: Uuid) -> String { + format!("omgcode-01-02-03-004Z-{id}.jsonl") +} + +fn place(root: &Path, day: &str, name: &str, bytes: &[u8]) -> PathBuf { + let directory = root.join("sessions").join(day); + fs::create_dir_all(&directory).expect("directory"); + let path = directory.join(name); + fs::write(&path, bytes).expect("file"); + path +} + +#[test] +fn discovery_lists_a_nested_layout_in_relative_path_order() { + let (temp, _root, _workspace) = setup(); + let root = temp.path().join("data"); + let ids: Vec = (0..4).map(|_| Uuid::new_v4()).collect(); + place(&root, "2026/01/06", &journal_name(ids[0]), b"abc"); + place(&root, "2025/12/31", &journal_name(ids[1]), b""); + place(&root, "2026/01/05", &journal_name(ids[2]), b"hello"); + place(&root, "2026/01/05", &journal_name(ids[3]), b"x"); + + let found = discover(&root).expect("discover"); + assert_eq!(found.skipped_links, 0); + assert_eq!(found.unrecognized, 0); + let relative: Vec = found + .journals + .iter() + .map(|item| item.relative_path.clone()) + .collect(); + let mut sorted = relative.clone(); + sorted.sort(); + assert_eq!(relative, sorted, "sorted by relative path"); + assert_eq!(found.journals.len(), 4); + assert_eq!(found.journals[0].session_id, ids[1]); + assert_eq!(found.journals[0].size, 0); + assert_eq!( + found.journals[0].relative_path, + Path::new("sessions") + .join("2025") + .join("12") + .join("31") + .join(journal_name(ids[1])) + ); + for item in &found.journals { + assert!(item.path.is_absolute()); + assert_eq!(item.path, root.join(&item.relative_path)); + assert!(item.path.is_file()); + assert_eq!(item.size, fs::metadata(&item.path).expect("metadata").len()); + } + let again = discover(&root).expect("discover again"); + assert_eq!(again, found, "deterministic"); +} + +#[test] +fn discovery_counts_and_ignores_names_outside_the_layout() { + let (temp, _root, _workspace) = setup(); + let root = temp.path().join("data"); + let id = Uuid::new_v4(); + let good = place(&root, "2026/01/05", &journal_name(id), b""); + // Each of these is one unrecognized entry. + place(&root, "2026/01/05", "notes.txt", b""); + place( + &root, + "2026/01/05", + &format!("{}.bak", journal_name(Uuid::new_v4())), + b"", + ); + place( + &root, + "2026/01/05", + &journal_name(Uuid::new_v4()).to_uppercase(), + b"", + ); + place( + &root, + "2026/01/05", + "omgcode-25-00-00-000Z-00000000-0000-4000-8000-000000000000.jsonl", + b"", + ); + // A journal-named file one level too high. + place(&root, "2026/01", &journal_name(Uuid::new_v4()), b""); + // Wrong-length directories, with a valid journal inside each. + place(&root, "26/01/05", &journal_name(Uuid::new_v4()), b""); + place(&root, "2026/1/05", &journal_name(Uuid::new_v4()), b""); + place(&root, "2026/01/5", &journal_name(Uuid::new_v4()), b""); + place(&root, "abcd/01/05", &journal_name(Uuid::new_v4()), b""); + // A plain file where a year directory belongs. + fs::write(root.join("sessions").join("2027"), b"").expect("file"); + + let found = discover(&root).expect("discover"); + assert_eq!(found.journals.len(), 1); + assert_eq!(found.journals[0].session_id, id); + assert_eq!(found.journals[0].path, good); + // 4 bad files, 1 high file, 4 bad directories, 1 plain file. + assert_eq!(found.unrecognized, 10); + assert_eq!(found.skipped_links, 0); +} + +#[test] +fn discovery_skips_a_directory_link_that_points_outside() { + let (temp, _root, _workspace) = setup(); + let root = temp.path().join("data"); + let outside = temp.path().join("outside"); + let outside_id = Uuid::new_v4(); + place(&outside, "x/01/05", &journal_name(outside_id), b""); + let inner = outside.join("sessions").join("x"); + fs::create_dir_all(root.join("sessions")).expect("sessions"); + // The link takes a valid year name and points at a tree holding a journal. + if !try_symlink_dir(&inner, &root.join("sessions").join("2030")) { + return; + } + let real = Uuid::new_v4(); + place(&root, "2026/01/05", &journal_name(real), b""); + + let found = discover(&root).expect("discover"); + assert_eq!(found.skipped_links, 1); + assert_eq!(found.journals.len(), 1); + assert_eq!(found.journals[0].session_id, real); + assert_eq!(locate(&root, outside_id).expect("locate"), None); +} + +#[test] +fn discovery_skips_a_sessions_directory_that_is_a_link() { + let (temp, _root, _workspace) = setup(); + let root = temp.path().join("data"); + let outside = temp.path().join("outside"); + place(&outside, "2026/01/05", &journal_name(Uuid::new_v4()), b""); + fs::create_dir_all(&root).expect("root"); + if !try_symlink_dir(&outside.join("sessions"), &root.join("sessions")) { + return; + } + let found = discover(&root).expect("discover"); + assert!(found.journals.is_empty()); + assert_eq!(found.skipped_links, 1); +} + +#[test] +fn a_missing_sessions_directory_is_an_empty_result() { + let (temp, _root, _workspace) = setup(); + let found = discover(&temp.path().join("never-created")).expect("empty"); + assert!(found.journals.is_empty()); + assert_eq!(found.skipped_links, 0); + assert_eq!(found.unrecognized, 0); + fs::create_dir_all(temp.path().join("data")).expect("root without sessions"); + assert!( + discover(&temp.path().join("data")) + .expect("empty") + .journals + .is_empty() + ); +} + +#[test] +fn locate_resolves_real_journals_by_uuid_only() { + let (_temp, root, workspace) = setup(); + let first = SessionJournal::create(&root, &workspace, "One").expect("one"); + let second = SessionJournal::create(&root, &workspace, "Two").expect("two"); + let (first_id, first_path) = (first.snapshot().session_id, first.path().to_path_buf()); + let second_id = second.snapshot().session_id; + drop((first, second)); + + assert_eq!(locate(&root, first_id).expect("locate"), Some(first_path)); + assert_ne!( + locate(&root, first_id).expect("locate"), + locate(&root, second_id).expect("locate") + ); + assert_eq!(locate(&root, Uuid::new_v4()).expect("unknown"), None); + let inspected = + SessionInspection::inspect(&locate(&root, second_id).expect("locate").expect("found")) + .expect("inspect located journal"); + assert_eq!(inspected.title, "Two"); +} + +#[test] +fn locate_refuses_a_duplicate_uuid_instead_of_picking_one() { + let (temp, _root, _workspace) = setup(); + let root = temp.path().join("data"); + let id = Uuid::new_v4(); + place(&root, "2026/01/05", &journal_name(id), b""); + place(&root, "2026/01/06", &journal_name(id), b""); + assert_eq!(discover(&root).expect("discover").journals.len(), 2); + assert!(matches!( + locate(&root, id), + Err(SessionError::InvalidJournal(_)) + )); + assert_eq!(locate(&root, Uuid::new_v4()).expect("other id"), None); +} diff --git a/crates/omgcode-core/tests/turn_runtime.rs b/crates/omgcode-core/tests/turn_runtime.rs index ac1eccb..a1d4cbb 100644 --- a/crates/omgcode-core/tests/turn_runtime.rs +++ b/crates/omgcode-core/tests/turn_runtime.rs @@ -173,6 +173,9 @@ struct ScriptedModel { replies: VecDeque>, requests: Vec, cancel_on_reply: Option, + /// Whether `request.cancel` was set when each request was answered, after + /// `cancel_on_reply` acted. + request_cancel_seen: Vec, } impl ScriptedModel { @@ -181,6 +184,7 @@ impl ScriptedModel { replies: replies.into(), requests: Vec::new(), cancel_on_reply: None, + request_cancel_seen: Vec::new(), } } } @@ -198,6 +202,7 @@ impl ToolModel for ScriptedModel { if let Some(token) = &self.cancel_on_reply { token.cancel(); } + self.request_cancel_seen.push(request.cancel.is_cancelled()); self.replies .pop_front() .unwrap_or(Err(ModelError::InvalidResponse)) @@ -1105,6 +1110,111 @@ fn cancellation_before_dispatch_prevents_file_access() { assert_eq!(fixture.last_failure_code().as_deref(), Some("cancelled")); } +/// A model that cancels the turn through the token it was handed. +struct CancelThroughRequest { + requests: usize, +} + +impl ToolModel for CancelThroughRequest { + fn complete(&mut self, request: ToolModelRequest<'_>) -> Result { + self.requests += 1; + request.cancel.cancel(); + text_reply("unused") + } +} + +#[test] +fn a_cancelled_model_request_of_a_cancelled_turn_records_cancelled() { + // Given a model that is interrupted after the turn's token was cancelled. + let mut fixture = Fixture::new(); + let mut model = ScriptedModel::new(vec![Err(ModelError::Cancelled)]); + let journal = fixture.journal.as_mut().expect("journal is open"); + let mut runtime = TurnRuntime::new( + journal, + &fixture.data, + fixture.workspace_id, + TurnLimits::default(), + ) + .expect("build runtime"); + model.cancel_on_reply = Some(runtime.cancel_token()); + + // When the turn runs. + let error = runtime + .run(&mut model, "read the file", "", manifest()) + .expect_err("the interrupted request ends the turn"); + drop(runtime); + + // Then the turn is cancelled, not a model failure, and the request saw the + // token that the test cancelled through the runtime. + assert!(matches!(error, TurnError::Cancelled), "{error:?}"); + assert_eq!(model.requests.len(), 1); + assert_eq!(model.request_cancel_seen, [true]); + assert_eq!(fixture.snapshot().active_turn, None); + fixture.close_journal(); + assert_eq!(fixture.last_failure_code().as_deref(), Some("cancelled")); +} + +#[test] +fn a_cancelled_model_error_without_a_cancelled_turn_is_a_model_error() { + // Given a model that reports an interrupted request though nobody cancelled. + let mut fixture = Fixture::new(); + let mut model = ScriptedModel::new(vec![Err(ModelError::Cancelled)]); + let journal = fixture.journal.as_mut().expect("journal is open"); + let mut runtime = TurnRuntime::new( + journal, + &fixture.data, + fixture.workspace_id, + TurnLimits::default(), + ) + .expect("build runtime"); + + // When the turn runs. + let error = runtime + .run(&mut model, "read the file", "", manifest()) + .expect_err("a faulty report ends the turn"); + drop(runtime); + + // Then it is recorded like any other model failure. + assert!( + matches!(error, TurnError::Model(ModelError::Cancelled)), + "{error:?}" + ); + assert_eq!(model.request_cancel_seen, [false]); + assert_eq!(fixture.snapshot().active_turn, None); + fixture.close_journal(); + assert_eq!(fixture.last_failure_code().as_deref(), Some("model_error")); +} + +#[test] +fn the_token_in_the_request_is_the_runtimes_token() { + // Given a model that cancels through `request.cancel` and then replies. + let mut fixture = Fixture::new(); + let mut model = CancelThroughRequest { requests: 0 }; + let journal = fixture.journal.as_mut().expect("journal is open"); + let mut runtime = TurnRuntime::new( + journal, + &fixture.data, + fixture.workspace_id, + TurnLimits::default(), + ) + .expect("build runtime"); + let token = runtime.cancel_token(); + + // When the turn runs. + let error = runtime + .run(&mut model, "read the file", "", manifest()) + .expect_err("the checkpoint after the request sees the cancellation"); + drop(runtime); + + // Then the runtime's own token was set through the request, and the turn + // ended as cancelled with no assistant message recorded. + assert!(token.is_cancelled()); + assert!(matches!(error, TurnError::Cancelled), "{error:?}"); + assert_eq!(model.requests, 1); + fixture.close_journal(); + assert_eq!(fixture.last_failure_code().as_deref(), Some("cancelled")); +} + #[test] fn cancellation_before_the_first_request_leaves_the_journal_unchanged() { let mut fixture = Fixture::new(); diff --git a/crates/omgcode-core/tests/workspace_listing.rs b/crates/omgcode-core/tests/workspace_listing.rs new file mode 100644 index 0000000..2339553 --- /dev/null +++ b/crates/omgcode-core/tests/workspace_listing.rs @@ -0,0 +1,93 @@ +//! Read-only workspace listing for an open control store. + +use std::fs; + +use omgcode_core::control::ControlStore; +use tempfile::TempDir; + +fn store_with(names: &[&str]) -> (TempDir, ControlStore, Vec) { + let temporary = TempDir::new().expect("private temporary directory"); + let data = temporary.path().join("application-data"); + let root = temporary.path().join("development-root"); + fs::create_dir(&root).expect("create development root"); + let mut control = ControlStore::open(&data).expect("open control store"); + control + .set_development_root(&root, control.revision()) + .expect("select development root"); + let mut directories = Vec::new(); + for name in names { + let directory = root.join(name); + fs::create_dir(&directory).expect("create workspace"); + control + .register_workspace(&directory, control.revision()) + .expect("register workspace"); + directories.push(directory); + } + (temporary, control, directories) +} + +#[test] +fn an_empty_store_lists_no_workspaces() { + let (_temporary, control, _) = store_with(&[]); + assert_eq!(control.workspaces().count(), 0); +} + +#[test] +fn workspaces_are_listed_in_registration_order_with_their_recorded_directory() { + let (_temporary, control, directories) = store_with(&["zeta", "alpha", "mid"]); + + let listed: Vec<_> = control.workspaces().collect(); + + assert_eq!(listed.len(), 3); + for (registration, directory) in listed.iter().zip(&directories) { + let recorded = registration.directory_path().expect("native path"); + assert_eq!( + fs::canonicalize(&recorded).expect("recorded path exists"), + fs::canonicalize(directory).expect("directory exists") + ); + assert!( + registration.directory_display().ends_with( + directory + .file_name() + .and_then(|name| name.to_str()) + .expect("name") + ), + "{}", + registration.directory_display() + ); + assert_eq!( + control.workspace(registration.id()).map(|found| found.id()), + Some(registration.id()) + ); + } + // The order is stable across calls. + let first: Vec<_> = control + .workspaces() + .map(|workspace| workspace.id()) + .collect(); + let second: Vec<_> = control + .workspaces() + .map(|workspace| workspace.id()) + .collect(); + assert_eq!(first, second); +} + +#[test] +fn directory_display_text_escapes_hidden_formatting_characters() { + // NTFS and most Unix filesystems admit U+202E in a name. + let (_temporary, control, _) = store_with(&["name\u{202e}gpj"]); + + let registration = control.workspaces().next().expect("one workspace"); + let display = registration.directory_display(); + + assert!(display.contains("name\\u{202e}gpj"), "{display}"); + assert!(!display.contains('\u{202e}')); + // The path used to reopen the directory is untouched. + assert!( + registration + .directory_path() + .expect("native path") + .to_string_lossy() + .contains('\u{202e}') + ); +} diff --git a/crates/omgcode-desktop/.gitignore b/crates/omgcode-desktop/.gitignore new file mode 100644 index 0000000..b50b9bb --- /dev/null +++ b/crates/omgcode-desktop/.gitignore @@ -0,0 +1,3 @@ +# Generated by tauri-build: ACL manifests and capability schemas. +/gen/ +/permissions/autogenerated/ diff --git a/crates/omgcode-desktop/Cargo.toml b/crates/omgcode-desktop/Cargo.toml new file mode 100644 index 0000000..91d158b --- /dev/null +++ b/crates/omgcode-desktop/Cargo.toml @@ -0,0 +1,43 @@ +[package] +name = "omgcode-desktop" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +description = "Tauri 2 desktop shell for OMGCode with a capability-gated command surface and two isolated webview windows" +build = "build.rs" + +[lib] +# A distinct library name keeps the binary and library outputs from colliding on Windows. +name = "omgcode_desktop_lib" +path = "src/lib.rs" + +[[bin]] +name = "omgcode-desktop" +path = "src/main.rs" + +[features] +# Tauri embeds the built frontend (`ui/dist`) only with `tauri/custom-protocol`; without +# it the shell loads `build.devUrl`, which a release build's navigation guard refuses. +# Embedding is the default so a plain `cargo build -p omgcode-desktop` yields a working +# shell. The Tauri CLI's `tauri dev` drops any default feature that enables +# `tauri/custom-protocol` and passes `--no-default-features`, so it still reaches the +# dev server. Do not rename this feature or change what it enables. +default = ["custom-protocol"] +custom-protocol = ["tauri/custom-protocol"] + +[dependencies] +omgcode-app = { path = "../omgcode-app" } +serde.workspace = true +serde_json.workspace = true +uuid.workspace = true +# Stable 2.x only (3.0 is alpha). `wry` is the webview runtime. `x11` lets WebKitGTK +# run under X11 sessions on Linux. No devtools, tray, updater, asset protocol, or plugin. +tauri = { version = "2.12.6", default-features = false, features = ["wry", "x11"] } + +[build-dependencies] +tauri-build = { version = "2.7.5", default-features = false, features = ["config-json"] } + +[lints] +workspace = true diff --git a/crates/omgcode-desktop/build.rs b/crates/omgcode-desktop/build.rs new file mode 100644 index 0000000..13e5456 --- /dev/null +++ b/crates/omgcode-desktop/build.rs @@ -0,0 +1,17 @@ +//! Declares every application command to Tauri's permission layer. +//! +//! With `AppManifest::commands`, each command gets generated `allow-` and +//! `deny-` permissions and is refused unless a capability grants it to the +//! calling window. Without a manifest, every registered command is open to +//! every window. The list is shared with `src/policy.rs` so the two cannot drift. + +include!("src/command_names.rs"); + +fn main() { + let manifest = tauri_build::AppManifest::new().commands(COMMAND_NAMES); + let attributes = tauri_build::Attributes::new().app_manifest(manifest); + if let Err(error) = tauri_build::try_build(attributes) { + eprintln!("tauri build script failed: {error:#}"); + std::process::exit(1); + } +} diff --git a/crates/omgcode-desktop/capabilities/approvals.json b/crates/omgcode-desktop/capabilities/approvals.json new file mode 100644 index 0000000..7d4b4e7 --- /dev/null +++ b/crates/omgcode-desktop/capabilities/approvals.json @@ -0,0 +1,14 @@ +{ + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "approvals-window", + "description": "The three approval commands for the approvals window only. No core permissions.", + "local": true, + "windows": [ + "approvals" + ], + "permissions": [ + "allow-approvals-list", + "allow-approval-detail", + "allow-approval-decide" + ] +} diff --git a/crates/omgcode-desktop/capabilities/main.json b/crates/omgcode-desktop/capabilities/main.json new file mode 100644 index 0000000..712f81b --- /dev/null +++ b/crates/omgcode-desktop/capabilities/main.json @@ -0,0 +1,27 @@ +{ + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "main-window", + "description": "Application commands for the main window only. No core permissions and no approval-decision commands.", + "local": true, + "windows": [ + "main" + ], + "permissions": [ + "allow-app-info", + "allow-session-tree", + "allow-session-search", + "allow-session-transcript", + "allow-session-archive", + "allow-session-restore", + "allow-session-interrupt", + "allow-workspace-list", + "allow-session-create", + "allow-chat-defaults", + "allow-chat-send", + "allow-chat-status", + "allow-chat-cancel", + "allow-chat-subscribe", + "allow-approvals-summary", + "allow-approvals-show-window" + ] +} diff --git a/crates/omgcode-desktop/icons/128x128.png b/crates/omgcode-desktop/icons/128x128.png new file mode 100644 index 0000000..c113ef5 Binary files /dev/null and b/crates/omgcode-desktop/icons/128x128.png differ diff --git a/crates/omgcode-desktop/icons/128x128@2x.png b/crates/omgcode-desktop/icons/128x128@2x.png new file mode 100644 index 0000000..ccc642c Binary files /dev/null and b/crates/omgcode-desktop/icons/128x128@2x.png differ diff --git a/crates/omgcode-desktop/icons/32x32.png b/crates/omgcode-desktop/icons/32x32.png new file mode 100644 index 0000000..7f87a5e Binary files /dev/null and b/crates/omgcode-desktop/icons/32x32.png differ diff --git a/crates/omgcode-desktop/icons/icon.ico b/crates/omgcode-desktop/icons/icon.ico new file mode 100644 index 0000000..3ead97c Binary files /dev/null and b/crates/omgcode-desktop/icons/icon.ico differ diff --git a/crates/omgcode-desktop/icons/icon.png b/crates/omgcode-desktop/icons/icon.png new file mode 100644 index 0000000..cdc5b74 Binary files /dev/null and b/crates/omgcode-desktop/icons/icon.png differ diff --git a/crates/omgcode-desktop/src/command_names.rs b/crates/omgcode-desktop/src/command_names.rs new file mode 100644 index 0000000..8c81f53 --- /dev/null +++ b/crates/omgcode-desktop/src/command_names.rs @@ -0,0 +1,26 @@ +// Single source of truth for the command names. It is `include!`d by `build.rs` +// (to declare the permissions) and by `policy.rs` (to check the `Command` enum +// against it in tests). Keep it a plain constant: no `use`, no items besides it. + +/// Every application command, in `snake_case`, exactly as the frontend invokes it. +pub const COMMAND_NAMES: &[&str] = &[ + "app_info", + "session_tree", + "session_search", + "session_transcript", + "session_archive", + "session_restore", + "session_interrupt", + "workspace_list", + "session_create", + "chat_defaults", + "chat_send", + "chat_status", + "chat_cancel", + "chat_subscribe", + "approvals_summary", + "approvals_show_window", + "approvals_list", + "approval_detail", + "approval_decide", +]; diff --git a/crates/omgcode-desktop/src/commands.rs b/crates/omgcode-desktop/src/commands.rs new file mode 100644 index 0000000..cd6238c --- /dev/null +++ b/crates/omgcode-desktop/src/commands.rs @@ -0,0 +1,157 @@ +//! Command handlers exposed to the webviews. +//! +//! Every handler: +//! 1. takes the calling [`tauri::Webview`] and calls [`authorize`] first, with +//! the runtime-supplied label of that webview, before it reads any state or +//! argument-dependent data; +//! 2. returns only fixed-shape data or a [`CommandError`]. +//! +//! Scheduling (Tauri 2, "Calling Rust from the Frontend"): a command without +//! `async` runs on the main (UI) thread unless declared +//! `#[tauri::command(async)]`; an `async` command runs on the async runtime's +//! worker threads. The service blocks on files, `SQLite`, and the settings +//! store (and `approvals_summary` for up to two seconds), so every service +//! handler is `async` and moves the call into +//! [`tauri::async_runtime::spawn_blocking`] through [`blocking`]. The service +//! is managed state of type `Arc`; `State<'_, T>` is borrowed +//! for the handler, so a handler clones the `Arc` into the blocking closure. +//! The two handlers below that touch no service only read constants or send a +//! window message, so they stay synchronous. +//! +//! Arguments are matched by name: Tauri deserializes each parameter from the +//! key of the invoke argument object with the same name (`rename_all = +//! "snake_case"` keeps the Rust names; the default would expect camelCase +//! keys). Each parameter list therefore equals the keys the frontend passes, +//! which `tests/layers.rs` checks mechanically. + +use std::sync::Arc; + +use omgcode_app::desktop::{DesktopError, DesktopService}; +use serde::Serialize; +use tauri::{Manager, State, Webview}; + +use crate::error::CommandError; +use crate::policy::{Command, WindowLabel, authorize}; + +// Handlers are referenced by their full module path in `generate_handler!`: +// the macro each `#[tauri::command]` generates is not re-exported by `pub use`. +pub mod approvals; +pub mod chat; +pub mod sessions; + +/// The service as Tauri state. +pub type Service<'a> = State<'a, Arc>; + +/// Runs one service call on the blocking pool and maps its failures. +/// +/// The caller has already passed [`authorize`]. A [`DesktopError`] becomes the +/// same `code` and `message`; a task that panicked or was cancelled becomes +/// `internal` with a fixed message. +pub(crate) async fn blocking(state: &Service<'_>, work: F) -> Result +where + T: Send + 'static, + F: FnOnce(&DesktopService) -> Result + Send + 'static, +{ + let service = Arc::clone(state.inner()); + match tauri::async_runtime::spawn_blocking(move || work(&service)).await { + Ok(result) => result.map_err(CommandError::from), + Err(_) => Err(CommandError::internal()), + } +} + +/// The reply to `app_info`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct AppInfo { + /// This crate's package version. + pub version: String, + /// `"windows"`, `"linux"`, or `"other"`. + pub platform: &'static str, + /// Whether the build supports model tools at all (not whether any is enabled). + pub model_tools_supported: bool, +} + +/// The platform name reported to the frontend. +const fn platform_name() -> &'static str { + if cfg!(target_os = "windows") { + "windows" + } else if cfg!(target_os = "linux") { + "linux" + } else { + "other" + } +} + +/// Builds the `app_info` reply. Separate from the handler so tests need no webview. +#[must_use] +pub fn app_info_value() -> AppInfo { + AppInfo { + version: env!("CARGO_PKG_VERSION").to_owned(), + platform: platform_name(), + model_tools_supported: omgcode_app::MODEL_TOOLS_SUPPORTED, + } +} + +/// `main` only. Returns the version, platform, and model-tool support. +/// +/// # Errors +/// +/// `forbidden_window` when the caller is not `main`. +#[tauri::command(rename_all = "snake_case")] +pub fn app_info(webview: Webview) -> Result { + authorize(Command::AppInfo, webview.label())?; + Ok(app_info_value()) +} + +/// `main` only. Shows, unminimizes, and focuses the existing `approvals` window. +/// It creates no window and navigates nothing. +/// +/// # Errors +/// +/// `forbidden_window` when the caller is not `main`; `window_unavailable` when +/// the approvals window is missing or the platform refuses a step. +#[tauri::command(rename_all = "snake_case")] +pub fn approvals_show_window(webview: Webview) -> Result<(), CommandError> { + authorize(Command::ApprovalsShowWindow, webview.label())?; + let window = webview + .app_handle() + .get_webview_window(WindowLabel::Approvals.as_str()) + .ok_or_else(CommandError::window_unavailable)?; + window + .unminimize() + .map_err(|_| CommandError::window_unavailable())?; + window + .show() + .map_err(|_| CommandError::window_unavailable())?; + window + .set_focus() + .map_err(|_| CommandError::window_unavailable())?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn app_info_reports_package_version_platform_and_tool_support() { + // When the reply is built. + let info = app_info_value(); + + // Then it carries this crate's version and the app crate's constant. + assert_eq!(info.version, env!("CARGO_PKG_VERSION")); + assert_eq!( + info.model_tools_supported, + omgcode_app::MODEL_TOOLS_SUPPORTED + ); + assert!(["windows", "linux", "other"].contains(&info.platform)); + } + + #[test] + fn app_info_serializes_with_snake_case_fields() { + let value = serde_json::to_value(app_info_value()).expect("serialize"); + let object = value.as_object().expect("object"); + let mut keys: Vec<&str> = object.keys().map(String::as_str).collect(); + keys.sort_unstable(); + assert_eq!(keys, ["model_tools_supported", "platform", "version"]); + } +} diff --git a/crates/omgcode-desktop/src/commands/approvals.rs b/crates/omgcode-desktop/src/commands/approvals.rs new file mode 100644 index 0000000..448e8c7 --- /dev/null +++ b/crates/omgcode-desktop/src/commands/approvals.rs @@ -0,0 +1,83 @@ +//! Approval handlers. `approvals_summary` belongs to `main`; the other three +//! belong to the `approvals` window only (ADR-0046 decisions 1 and 2). + +use omgcode_app::desktop::{ + ApprovalDecideRequest, ApprovalDetail, ApprovalList, ApprovalRequest, ApprovalView, + ApprovalsSummary, Decision, +}; +use tauri::Webview; +use uuid::Uuid; + +use super::{Service, blocking}; +use crate::error::CommandError; +use crate::policy::{Command, authorize}; + +/// `main` only. Counts pending approvals for the badge. It waits up to two +/// seconds for the settings store, which is why it runs on the blocking pool. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn approvals_summary( + webview: Webview, + state: Service<'_>, +) -> Result { + authorize(Command::ApprovalsSummary, webview.label())?; + blocking(&state, |service| service.approvals_summary()).await +} + +/// `approvals` only. Lists approval records. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn approvals_list( + webview: Webview, + state: Service<'_>, +) -> Result { + authorize(Command::ApprovalsList, webview.label())?; + blocking(&state, |service| service.approvals_list()).await +} + +/// `approvals` only. Returns the full review of one approval and a single-use +/// review token. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn approval_detail( + webview: Webview, + state: Service<'_>, + id: Uuid, +) -> Result { + authorize(Command::ApprovalDetail, webview.label())?; + let request = ApprovalRequest { id }; + blocking(&state, move |service| service.approval_detail(request)).await +} + +/// `approvals` only. Approves or denies one approval. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn approval_decide( + webview: Webview, + state: Service<'_>, + id: Uuid, + decision: Decision, + writers_stopped: bool, + review_token: Option, +) -> Result { + authorize(Command::ApprovalDecide, webview.label())?; + let request = ApprovalDecideRequest { + id, + decision, + writers_stopped, + review_token, + }; + blocking(&state, move |service| service.approval_decide(request)).await +} diff --git a/crates/omgcode-desktop/src/commands/chat.rs b/crates/omgcode-desktop/src/commands/chat.rs new file mode 100644 index 0000000..454ceb9 --- /dev/null +++ b/crates/omgcode-desktop/src/commands/chat.rs @@ -0,0 +1,112 @@ +//! `main`-window handlers for chat settings, turns, status, and cancellation. + +use omgcode_app::desktop::{ + ChatEvent, ChatEventSink, ChatSendRequest, ChatSettingsDto, ChatStatus, SessionRequest, +}; +use tauri::Webview; +use tauri::ipc::Channel; +use uuid::Uuid; + +use super::{Service, blocking}; +use crate::error::CommandError; +use crate::policy::{Command, authorize}; + +/// `main` only. Returns the process-wide chat defaults, including the fixed +/// endpoint the process was started with. +/// +/// # Errors +/// +/// `forbidden_window`, or `internal` when the blocking task fails. +#[tauri::command(rename_all = "snake_case")] +pub async fn chat_defaults( + webview: Webview, + state: Service<'_>, +) -> Result { + authorize(Command::ChatDefaults, webview.label())?; + blocking(&state, |service| Ok(service.chat_defaults())).await +} + +/// `main` only. Starts one chat turn on a service thread and returns at once. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error (`invalid_request` when the +/// endpoint differs from the process endpoint). +#[tauri::command(rename_all = "snake_case")] +pub async fn chat_send( + webview: Webview, + state: Service<'_>, + session_id: Uuid, + message: String, + settings: ChatSettingsDto, +) -> Result { + authorize(Command::ChatSend, webview.label())?; + let request = ChatSendRequest { + session_id, + message, + settings, + }; + blocking(&state, move |service| service.chat_send(request)).await +} + +/// `main` only. Reports a session's chat turn state. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn chat_status( + webview: Webview, + state: Service<'_>, + session_id: Uuid, +) -> Result { + authorize(Command::ChatStatus, webview.label())?; + let request = SessionRequest { session_id }; + blocking(&state, move |service| service.chat_status(request)).await +} + +/// `main` only. Requests cooperative cancellation of a running turn. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn chat_cancel( + webview: Webview, + state: Service<'_>, + session_id: Uuid, +) -> Result { + authorize(Command::ChatCancel, webview.label())?; + let request = SessionRequest { session_id }; + blocking(&state, move |service| service.chat_cancel(request)).await +} + +/// `main` only. Registers the one chat event subscriber, replacing any earlier +/// one, and replays the preview of every running turn (ADR-0049). +/// +/// The caller creates the [`tauri::ipc::Channel`] and passes it as `on_event`. +/// A channel needs no capability beyond this command's own permission. A Tauri +/// event (`emit` and `listen`) would need a `core:event` permission, which both +/// windows are kept without (ADR-0045). The sink sends each event on the +/// channel and reports whether that worked; the service drops a sink that +/// reports failure, so a closed webview ends its own subscription. +/// +/// `Channel::send` serializes the event and queues one script evaluation for the +/// webview; it does not wait for the page, so the sink does not block the turn +/// thread that calls it while the service's event lock is held. +/// +/// # Errors +/// +/// `forbidden_window`, or `internal` when the service's event state is +/// unavailable or the blocking task fails. +#[tauri::command(rename_all = "snake_case")] +pub async fn chat_subscribe( + webview: Webview, + state: Service<'_>, + on_event: Channel, +) -> Result<(), CommandError> { + authorize(Command::ChatSubscribe, webview.label())?; + let sink: ChatEventSink = + Box::new(move |event: &ChatEvent| on_event.send(event.clone()).is_ok()); + blocking(&state, move |service| service.chat_subscribe(sink)).await +} diff --git a/crates/omgcode-desktop/src/commands/sessions.rs b/crates/omgcode-desktop/src/commands/sessions.rs new file mode 100644 index 0000000..5f918de --- /dev/null +++ b/crates/omgcode-desktop/src/commands/sessions.rs @@ -0,0 +1,158 @@ +//! `main`-window handlers for the session tree, search, transcripts, the +//! session lifecycle, workspaces, and session creation. + +use omgcode_app::desktop::{ + SearchRequest, SearchResult, SessionCreateRequest, SessionRequest, SessionSummary, SessionTree, + TranscriptPage, TranscriptRequest, TreeRequest, TreeSort, TreeView, WorkspaceList, +}; +use tauri::Webview; +use uuid::Uuid; + +use super::{Service, blocking}; +use crate::error::CommandError; +use crate::policy::{Command, authorize}; + +/// `main` only. Lists sessions grouped by workspace folder. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn session_tree( + webview: Webview, + state: Service<'_>, + view: TreeView, + sort: TreeSort, + descending: bool, +) -> Result { + authorize(Command::SessionTree, webview.label())?; + let request = TreeRequest { + view, + sort, + descending, + }; + blocking(&state, move |service| service.session_tree(request)).await +} + +/// `main` only. Searches session text. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn session_search( + webview: Webview, + state: Service<'_>, + query: String, + view: TreeView, + limit: u32, +) -> Result { + authorize(Command::SessionSearch, webview.label())?; + let request = SearchRequest { query, view, limit }; + blocking(&state, move |service| service.session_search(request)).await +} + +/// `main` only. Reads one page of a session transcript. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn session_transcript( + webview: Webview, + state: Service<'_>, + session_id: Uuid, + after_seq: Option, + limit: u32, +) -> Result { + authorize(Command::SessionTranscript, webview.label())?; + let request = TranscriptRequest { + session_id, + after_seq, + limit, + }; + blocking(&state, move |service| service.session_transcript(request)).await +} + +/// `main` only. Archives a session. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn session_archive( + webview: Webview, + state: Service<'_>, + session_id: Uuid, +) -> Result { + authorize(Command::SessionArchive, webview.label())?; + let request = SessionRequest { session_id }; + blocking(&state, move |service| service.session_archive(request)).await +} + +/// `main` only. Restores an archived session. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn session_restore( + webview: Webview, + state: Service<'_>, + session_id: Uuid, +) -> Result { + authorize(Command::SessionRestore, webview.label())?; + let request = SessionRequest { session_id }; + blocking(&state, move |service| service.session_restore(request)).await +} + +/// `main` only. Closes a session's unfinished turn as operator-interrupted. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn session_interrupt( + webview: Webview, + state: Service<'_>, + session_id: Uuid, +) -> Result { + authorize(Command::SessionInterrupt, webview.label())?; + let request = SessionRequest { session_id }; + blocking(&state, move |service| service.session_interrupt(request)).await +} + +/// `main` only. Lists the workspaces the operator registered. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error. +#[tauri::command(rename_all = "snake_case")] +pub async fn workspace_list( + webview: Webview, + state: Service<'_>, +) -> Result { + authorize(Command::WorkspaceList, webview.label())?; + blocking(&state, |service| service.workspace_list()).await +} + +/// `main` only. Creates a session in a registered workspace. +/// +/// # Errors +/// +/// `forbidden_window`, or the service's error (`unknown_workspace` for an +/// identifier the operator did not register). +#[tauri::command(rename_all = "snake_case")] +pub async fn session_create( + webview: Webview, + state: Service<'_>, + workspace_id: Uuid, + title: String, +) -> Result { + authorize(Command::SessionCreate, webview.label())?; + let request = SessionCreateRequest { + workspace_id, + title, + }; + blocking(&state, move |service| service.session_create(request)).await +} diff --git a/crates/omgcode-desktop/src/error.rs b/crates/omgcode-desktop/src/error.rs new file mode 100644 index 0000000..4c23b07 --- /dev/null +++ b/crates/omgcode-desktop/src/error.rs @@ -0,0 +1,102 @@ +//! The error type returned to the webview. + +use std::borrow::Cow; + +use omgcode_app::desktop::DesktopError; +use serde::Serialize; + +/// An error that crosses the IPC boundary. +/// +/// The webview is untrusted, so a message is never built from a lower-level error +/// here. It is either a fixed string chosen in this crate or the message of a +/// [`DesktopError`], which the service keeps free of absolute paths, recorded +/// content, and the local server token. The `code` is a stable machine-readable +/// token the frontend may branch on. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct CommandError { + code: &'static str, + message: Cow<'static, str>, +} + +impl CommandError { + /// The calling webview is not the window this command belongs to. + pub(crate) const fn forbidden_window() -> Self { + Self { + code: "forbidden_window", + message: Cow::Borrowed("This window may not use this command."), + } + } + + /// A window the command needs is missing or could not be controlled. + pub(crate) const fn window_unavailable() -> Self { + Self { + code: "window_unavailable", + message: Cow::Borrowed("The requested window is not available."), + } + } + + /// A blocking task failed or panicked before it produced a result. + #[must_use] + pub const fn internal() -> Self { + Self { + code: "internal", + message: Cow::Borrowed("The command could not be completed."), + } + } + + /// The stable error code. + #[must_use] + pub const fn code(&self) -> &'static str { + self.code + } + + /// The message shown to the user. + #[must_use] + pub fn message(&self) -> &str { + &self.message + } +} + +impl From for CommandError { + /// Keeps the service's `code` and `message` exactly. + fn from(error: DesktopError) -> Self { + Self { + code: error.code(), + message: Cow::Owned(error.message().to_owned()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn serializes_as_code_and_message_only() { + // Given a forbidden-window error. + let error = CommandError::forbidden_window(); + + // When it is serialized for the webview. + let value = serde_json::to_value(&error).expect("serialize"); + + // Then it has exactly the code and message fields. + assert_eq!( + value, + serde_json::json!({ + "code": "forbidden_window", + "message": "This window may not use this command." + }) + ); + } + + #[test] + fn messages_hold_no_path_separators() { + for error in [ + CommandError::forbidden_window(), + CommandError::window_unavailable(), + ] { + assert!(!error.message.contains(['/', '\\', ':'])); + assert!(!error.code().is_empty()); + } + } +} diff --git a/crates/omgcode-desktop/src/launch.rs b/crates/omgcode-desktop/src/launch.rs new file mode 100644 index 0000000..c1300cd --- /dev/null +++ b/crates/omgcode-desktop/src/launch.rs @@ -0,0 +1,324 @@ +//! Launch configuration, read from the process command line only. +//! +//! ```text +//! omgcode-desktop [--data-dir ] [--endpoint ] [--provider-profile ] +//! [--stream ] +//! ``` +//! +//! The command line belongs to the operator who starts the process. The +//! webviews cannot influence it, and neither can workspace content. This module +//! reads no environment variable and no configuration file. The local server +//! token is a separate matter and this module never touches it. Without +//! `--provider-profile` the service reads `OMGCODE_LM_TOKEN` for each chat +//! turn. With it, the service reads the credential bound to that recorded +//! provider profile instead and never reads the variable (ADR-0047). The flag +//! carries only a profile name, not a secret. A selected profile also supplies +//! the endpoint: `--endpoint` and `--provider-profile` together are a +//! [`UsageError`], and with a profile [`LaunchArgs::endpoint`] holds only the +//! default and is not used (ADR-0048). +//! +//! `--stream` chooses whether chat turns send streamed model requests and push +//! their text to the window (`on`, the default) or send non-streaming requests +//! (`off`). Only the exact values `on` and `off` are accepted (ADR-0049). +//! +//! Any unknown argument, a flag without a value, a value that looks like +//! another flag, an empty value, a non-UTF-8 endpoint, a repeated flag, or +//! both `--endpoint` and `--provider-profile` is a [`UsageError`]. The caller +//! prints [`USAGE`] and exits before a window opens. + +use std::ffi::OsString; +use std::path::PathBuf; + +/// The endpoint used when `--endpoint` is absent. It is the CLI's default. +pub const DEFAULT_ENDPOINT: &str = "http://127.0.0.1:1234/v1"; + +/// The fixed message printed to stderr for a usage error. +pub const USAGE: &str = "usage: omgcode-desktop [--data-dir ] [--endpoint ] [--provider-profile ] [--stream ]\n \ +--data-dir application data root (default: the per-user OMGCode data directory)\n \ +--endpoint local model endpoint, a loopback http URL (default: http://127.0.0.1:1234/v1)\n \ +--provider-profile use this lm_studio provider profile's recorded endpoint and stored credential\n (default: --endpoint and the OMGCODE_LM_TOKEN environment variable)\n cannot be combined with --endpoint\n \ +--stream stream model replies into the window as they arrive (default: on); off sends non-streaming requests"; + +/// What the command line asked for. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct LaunchArgs { + /// The data root override, or `None` for the per-user default. + pub data_dir: Option, + /// The chat endpoint for the whole process. With a provider profile this is + /// the default and is not used: the profile's recorded endpoint is. + pub endpoint: String, + /// The recorded provider profile whose stored credential is the server + /// token, or `None` to use the `OMGCODE_LM_TOKEN` variable. + pub provider_profile: Option, + /// Whether chat turns send streamed requests and push their text to the + /// window. `--stream off` sets it to `false` (ADR-0049). + pub stream: bool, +} + +/// The command line was not understood. It carries no detail on purpose: the +/// usage text is the only message. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct UsageError; + +/// Parses the arguments after the program name. +/// +/// A path is kept as an `OsString` so a non-UTF-8 path survives. A value must +/// not begin with `--`, so a forgotten value is not read as the next flag's +/// name. A flag given twice is refused, not merged. +/// +/// # Errors +/// +/// [`UsageError`] for anything outside the contract above. +pub fn parse_args(args: I) -> Result +where + I: IntoIterator, +{ + let mut data_dir: Option = None; + let mut endpoint: Option = None; + let mut provider_profile: Option = None; + let mut stream: Option = None; + let mut args = args.into_iter(); + while let Some(flag) = args.next() { + let value = args.next().ok_or(UsageError)?; + if value.is_empty() || value.as_encoded_bytes().starts_with(b"--") { + return Err(UsageError); + } + match flag.to_str() { + Some("--data-dir") if data_dir.is_none() => data_dir = Some(PathBuf::from(value)), + Some("--endpoint") if endpoint.is_none() => { + endpoint = Some(value.into_string().map_err(|_| UsageError)?); + } + Some("--provider-profile") if provider_profile.is_none() => { + provider_profile = Some(value.into_string().map_err(|_| UsageError)?); + } + Some("--stream") if stream.is_none() => { + stream = Some(match value.to_str() { + Some("on") => true, + Some("off") => false, + _ => return Err(UsageError), + }); + } + _ => return Err(UsageError), + } + } + // A selected profile is the only source of its endpoint, so there is no + // "allowed when equal" case. + if endpoint.is_some() && provider_profile.is_some() { + return Err(UsageError); + } + Ok(LaunchArgs { + data_dir, + endpoint: endpoint.unwrap_or_else(|| DEFAULT_ENDPOINT.to_owned()), + provider_profile, + stream: stream.unwrap_or(true), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(args: &[&str]) -> Result { + parse_args(args.iter().map(OsString::from)) + } + + #[test] + fn no_arguments_gives_the_defaults() { + assert_eq!( + parse(&[]), + Ok(LaunchArgs { + data_dir: None, + endpoint: DEFAULT_ENDPOINT.to_owned(), + provider_profile: None, + stream: true, + }) + ); + } + + #[test] + fn each_flag_sets_its_value_in_either_order() { + let both = LaunchArgs { + data_dir: Some(PathBuf::from("data")), + endpoint: "http://127.0.0.1:9/v1".to_owned(), + provider_profile: None, + stream: true, + }; + assert_eq!( + parse(&["--data-dir", "data", "--endpoint", "http://127.0.0.1:9/v1"]), + Ok(both.clone()) + ); + assert_eq!( + parse(&["--endpoint", "http://127.0.0.1:9/v1", "--data-dir", "data"]), + Ok(both) + ); + assert_eq!( + parse(&["--data-dir", "C:\\a b\\c"]) + .expect("data dir") + .data_dir, + Some(PathBuf::from("C:\\a b\\c")) + ); + assert_eq!( + parse(&["--endpoint", "http://127.0.0.1:9/v1"]) + .expect("endpoint") + .data_dir, + None + ); + } + + #[test] + fn unknown_missing_repeated_and_malformed_arguments_are_refused() { + for args in [ + &["--bogus"][..], + &["--bogus", "x"], + &["positional"], + &["--data-dir"], + &["--endpoint"], + &["--data-dir", "a", "--endpoint"], + &["--data-dir", "a", "--data-dir", "b"], + &["--endpoint", "u", "--endpoint", "v"], + &["--data-dir", "--endpoint", "u"], + &["--data-dir", ""], + &["--endpoint", ""], + &["--data-dir=a"], + &["--endpoint=http://127.0.0.1:1/v1"], + &["-h"], + &["--help"], + &["--data-dir", "a", "extra"], + &["--provider-profile"], + &["--provider-profile", ""], + &["--provider-profile", "--endpoint", "u"], + &["--provider-profile", "--data-dir"], + &["--provider-profile", "a", "--provider-profile", "b"], + &["--provider-profile=a"], + &["--endpoint", "u", "--provider-profile"], + &[ + "--endpoint", + "http://127.0.0.1:9/v1", + "--provider-profile", + "lms", + ], + &[ + "--provider-profile", + "lms", + "--endpoint", + "http://127.0.0.1:9/v1", + ], + &["--provider-profile", "lms", "--endpoint", DEFAULT_ENDPOINT], + &[ + "--data-dir", + "d", + "--provider-profile", + "lms", + "--endpoint", + "u", + ], + ] { + assert_eq!(parse(args), Err(UsageError), "{args:?}"); + } + } + + #[test] + fn the_provider_profile_flag_takes_one_name_in_any_order() { + // The endpoint is the default and unused: the profile supplies it. + let expected = LaunchArgs { + data_dir: Some(PathBuf::from("data")), + endpoint: DEFAULT_ENDPOINT.to_owned(), + provider_profile: Some("lms".to_owned()), + stream: true, + }; + assert_eq!( + parse(&["--provider-profile", "lms", "--data-dir", "data"]), + Ok(expected.clone()) + ); + assert_eq!( + parse(&["--data-dir", "data", "--provider-profile", "lms"]), + Ok(expected) + ); + // The name is kept verbatim; core validates it against recorded profiles. + assert_eq!( + parse(&["--provider-profile", "My Profile"]) + .expect("name") + .provider_profile, + Some("My Profile".to_owned()) + ); + assert_eq!(parse(&[]).expect("none").provider_profile, None); + } + + #[test] + fn streaming_is_on_by_default_and_the_flag_takes_on_or_off() { + // Given no flag, then streaming is on. + assert!(parse(&[]).expect("defaults").stream); + + // When the flag says `on` or `off`, then that is the value, in any order. + assert!(parse(&["--stream", "on"]).expect("on").stream); + assert!(!parse(&["--stream", "off"]).expect("off").stream); + let both = parse(&["--stream", "off", "--data-dir", "data"]).expect("both"); + assert!(!both.stream); + assert_eq!(both.data_dir, Some(PathBuf::from("data"))); + let profile = parse(&["--provider-profile", "lms", "--stream", "off"]).expect("profile"); + assert!(!profile.stream); + assert_eq!(profile.provider_profile.as_deref(), Some("lms")); + } + + #[test] + fn a_bad_missing_or_repeated_stream_value_is_refused() { + for args in [ + &["--stream", "maybe"][..], + &["--stream", "ON"], + &["--stream", "true"], + &["--stream", "1"], + &["--stream", " on"], + &["--stream", ""], + &["--stream"], + &["--stream", "--data-dir", "a"], + &["--stream", "on", "--stream", "on"], + &["--stream", "on", "--stream", "off"], + &["--stream=on"], + ] { + assert_eq!(parse(args), Err(UsageError), "{args:?}"); + } + } + + #[test] + fn usage_text_names_every_flag_and_the_default() { + assert!(USAGE.contains("--stream ")); + assert!(USAGE.contains("--data-dir") && USAGE.contains("--endpoint")); + assert!(USAGE.contains("--provider-profile")); + assert!(USAGE.contains(DEFAULT_ENDPOINT)); + assert!(USAGE.contains("cannot be combined with --endpoint")); + } + + #[cfg(windows)] + #[test] + fn a_non_utf8_data_dir_is_kept_and_a_non_utf8_endpoint_is_refused() { + use std::os::windows::ffi::{OsStrExt, OsStringExt}; + + // Given a path with an unpaired surrogate, which is valid on Windows only. + let mut wide: Vec = OsString::from("C:\\data-").encode_wide().collect(); + wide.push(0xD800); + let odd = OsString::from_wide(&wide); + assert!(odd.to_str().is_none()); + + // Then the data root keeps it unchanged and the endpoint refuses it. + let kept = parse_args([OsString::from("--data-dir"), odd.clone()]).expect("path"); + assert_eq!(kept.data_dir, Some(PathBuf::from(&odd))); + assert_eq!( + parse_args([OsString::from("--endpoint"), odd]), + Err(UsageError) + ); + } + + #[cfg(unix)] + #[test] + fn a_non_utf8_data_dir_is_kept_and_a_non_utf8_endpoint_is_refused() { + use std::os::unix::ffi::OsStringExt; + + let odd = OsString::from_vec(b"/data-\xff".to_vec()); + let kept = parse_args([OsString::from("--data-dir"), odd.clone()]).expect("path"); + assert_eq!(kept.data_dir, Some(PathBuf::from(&odd))); + assert_eq!( + parse_args([OsString::from("--endpoint"), odd]), + Err(UsageError) + ); + } +} diff --git a/crates/omgcode-desktop/src/lib.rs b/crates/omgcode-desktop/src/lib.rs new file mode 100644 index 0000000..1bed937 --- /dev/null +++ b/crates/omgcode-desktop/src/lib.rs @@ -0,0 +1,229 @@ +//! OMGCode desktop shell (Tauri 2). +//! +//! The Rust side is the only authority. Both webviews render untrusted text and +//! reach Rust only through commands that are (1) gated by a per-window Tauri +//! capability and (2) checked again against the window policy in +//! [`policy`]. See ADR-0045 and ADR-0046. +//! +//! The data root, and either the model endpoint with the environment token or a +//! provider profile (whose recorded endpoint and stored credential are used +//! instead), come from the process command line ([`launch`]) and nowhere else. + +pub mod commands; +pub mod error; +pub mod launch; +pub mod policy; +pub mod shutdown; +pub mod windows; + +use std::process::ExitCode; +use std::sync::Arc; + +use omgcode_app::desktop::{DesktopConfig, DesktopService}; +use omgcode_app::{ + ChatClients, ChatSettings, ConnectionSource, LocalConnection, TokenSource, + resolve_local_connection, +}; +use tauri::{Manager, RunEvent}; + +use crate::launch::{LaunchArgs, USAGE}; +use crate::shutdown::ShutdownGate; + +/// Exit code for a command line that was not understood. +const EXIT_USAGE: u8 = 2; + +/// A context size that is valid for any model, used only to validate the endpoint. +const PROBE_CONTEXT_TOKENS: u64 = 8192; + +/// Builds and runs the application. +/// +/// Exit codes: `0` after a normal close, `2` for a command-line usage error, +/// `1` for any other startup failure (endpoint rejected, data root or index +/// unusable, a selected provider profile unusable (unknown, no recorded endpoint, +/// or an unreadable credential), runtime could +/// not start). Every failure happens before a window +/// opens and prints a fixed or provider message to stderr. In a release build +/// on Windows the process has no console, so stderr is visible only when it is +/// redirected. +#[must_use] +pub fn run() -> ExitCode { + let Ok(args) = launch::parse_args(std::env::args_os().skip(1)) else { + eprintln!("{USAGE}"); + return ExitCode::from(EXIT_USAGE); + }; + let service = match open_service(args) { + Ok(service) => Arc::new(service), + Err(message) => { + eprintln!("omgcode-desktop: {message}"); + return ExitCode::FAILURE; + } + }; + run_app(service) +} + +/// Validates the endpoint and opens the one service for this process. +fn open_service(args: LaunchArgs) -> Result { + let (data_root, endpoint, token_source) = match args.provider_profile { + None => { + // Validate the endpoint the way a chat turn would, so a malformed + // or non-loopback endpoint fails now, with the provider's message. + check_endpoint(&args.endpoint)?; + let data_root = + omgcode_app::data_root(args.data_dir).map_err(|error| error.to_string())?; + (data_root, args.endpoint, TokenSource::Environment) + } + Some(name) => { + // A selected profile is the only source of the endpoint, and the + // launch default in `args.endpoint` is not used (ADR-0048). The + // profile is resolved once now, so an unknown profile, a profile + // of another provider, a profile with no recorded endpoint, or an + // unreadable credential stops the launch before a window opens. + let data_root = + omgcode_app::data_root(args.data_dir).map_err(|error| error.to_string())?; + let connection = + resolve_local_connection(&data_root, &ConnectionSource::Profile(name.clone())) + .map_err(|error| error.to_string())?; + // The token is dropped at once; each turn resolves it again. The + // endpoint is the one the profile recorded, and it is validated + // like any other so a hand-edited journal cannot slip past. + let LocalConnection { endpoint, token } = connection; + drop(token); + check_endpoint(&endpoint)?; + (data_root, endpoint, TokenSource::Profile(name)) + } + }; + DesktopService::open(DesktopConfig { + data_root, + endpoint, + token_source, + stream: args.stream, + }) + .map_err(|error| format!("{}: {}", error.code(), error.message())) +} + +/// Validate an endpoint the way a chat turn would. The clients are discarded; +/// no request is made and no token is read. +fn check_endpoint(endpoint: &str) -> Result<(), String> { + let probe = ChatSettings::new(endpoint, "endpoint-check", PROBE_CONTEXT_TOKENS); + ChatClients::new(&probe, None) + .map(drop) + .map_err(|error| error.to_string()) +} + +fn run_app(service: Arc) -> ExitCode { + let built = tauri::Builder::default() + .manage(service) + .manage(ShutdownGate::default()) + .setup(|app| { + windows::create_windows(app)?; + Ok(()) + }) + .invoke_handler(tauri::generate_handler![ + commands::app_info, + commands::sessions::session_tree, + commands::sessions::session_search, + commands::sessions::session_transcript, + commands::sessions::session_archive, + commands::sessions::session_restore, + commands::sessions::session_interrupt, + commands::sessions::workspace_list, + commands::sessions::session_create, + commands::chat::chat_defaults, + commands::chat::chat_send, + commands::chat::chat_status, + commands::chat::chat_cancel, + commands::chat::chat_subscribe, + commands::approvals::approvals_summary, + commands::approvals_show_window, + commands::approvals::approvals_list, + commands::approvals::approval_detail, + commands::approvals::approval_decide + ]) + .build(tauri::generate_context!()); + match built { + Ok(app) => { + app.run(|handle, event| { + // Any exit path that did not come from the bounded shutdown is + // stopped, run through that shutdown once, and then allowed. + if let RunEvent::ExitRequested { code, api, .. } = event + && !handle.state::().is_done() + { + api.prevent_exit(); + shutdown::begin(handle, code.unwrap_or(0)); + } + }); + ExitCode::SUCCESS + } + Err(_) => { + eprintln!("omgcode-desktop: the application could not start"); + ExitCode::FAILURE + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A selected profile is checked at launch, before the service or any + /// window exists, and the check creates nothing in the data root. + #[test] + fn a_selected_profile_with_no_settings_store_stops_the_launch() { + let root = std::env::temp_dir().join(format!( + "omgcode-desktop-launch-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_nanos()) + )); + let failure = open_service(LaunchArgs { + data_dir: Some(root.clone()), + endpoint: launch::DEFAULT_ENDPOINT.to_owned(), + provider_profile: Some("lms".to_owned()), + stream: true, + }) + .err() + .expect("no settings store exists"); + assert!( + failure.starts_with("credential: no settings store exists"), + "{failure}" + ); + assert!(!root.exists(), "the launch check created the data root"); + } + + /// Without a profile the endpoint is validated first, so a bad endpoint is + /// the reported failure and no data root is touched. + #[test] + fn a_bad_endpoint_without_a_profile_is_refused() { + let failure = open_service(LaunchArgs { + data_dir: Some(std::env::temp_dir().join("omgcode-desktop-never-created")), + endpoint: "http://example.com:1234/v1".to_owned(), + provider_profile: None, + stream: true, + }) + .err() + .expect("a non-loopback endpoint is refused"); + assert!(failure.starts_with("provider:"), "{failure}"); + } + + /// With a profile the launch endpoint is not used at all (ADR-0048): a bad + /// value there is ignored and the profile decides, here by having no + /// settings store. + #[test] + fn a_selected_profile_ignores_the_launch_endpoint() { + let root = std::env::temp_dir().join("omgcode-desktop-never-created-2"); + let failure = open_service(LaunchArgs { + data_dir: Some(root.clone()), + endpoint: "http://example.com:1234/v1".to_owned(), + provider_profile: Some("lms".to_owned()), + stream: true, + }) + .err() + .expect("no settings store exists"); + assert!( + failure.starts_with("credential: no settings store exists"), + "{failure}" + ); + assert!(!root.exists()); + } +} diff --git a/crates/omgcode-desktop/src/main.rs b/crates/omgcode-desktop/src/main.rs new file mode 100644 index 0000000..7c358b0 --- /dev/null +++ b/crates/omgcode-desktop/src/main.rs @@ -0,0 +1,8 @@ +// Hide the console window in release builds on Windows. +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] + +use std::process::ExitCode; + +fn main() -> ExitCode { + omgcode_desktop_lib::run() +} diff --git a/crates/omgcode-desktop/src/policy.rs b/crates/omgcode-desktop/src/policy.rs new file mode 100644 index 0000000..c02a9c0 --- /dev/null +++ b/crates/omgcode-desktop/src/policy.rs @@ -0,0 +1,229 @@ +//! The window policy: which webview may call which command. +//! +//! This is the second of two layers (ADR-0046 decision 2). Tauri's capability +//! files are the first. Every command handler calls [`authorize`] before doing +//! anything else, so a capability file edited by mistake cannot expose a decision +//! command to the wrong window. The calling label comes from the runtime, which +//! binds the IPC channel to the webview that owns it; page script cannot choose it. + +use crate::error::CommandError; + +/// The two webview windows. Labels are fixed at creation. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WindowLabel { + /// Session tree, transcripts, and chat. + Main, + /// The only window that may list, review, and decide approvals. + Approvals, +} + +impl WindowLabel { + /// The label string passed to the window builder and matched in capabilities. + #[must_use] + pub const fn as_str(self) -> &'static str { + match self { + Self::Main => "main", + Self::Approvals => "approvals", + } + } +} + +/// Every application command. The set is closed: adding a command means adding +/// a variant here, a name in `command_names.rs`, and a capability grant. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Command { + AppInfo, + SessionTree, + SessionSearch, + SessionTranscript, + SessionArchive, + SessionRestore, + SessionInterrupt, + WorkspaceList, + SessionCreate, + ChatDefaults, + ChatSend, + ChatStatus, + ChatCancel, + ChatSubscribe, + ApprovalsSummary, + ApprovalsShowWindow, + ApprovalsList, + ApprovalDetail, + ApprovalDecide, +} + +impl Command { + /// All commands, in the order of `command_names.rs`. + pub const ALL: [Self; 19] = [ + Self::AppInfo, + Self::SessionTree, + Self::SessionSearch, + Self::SessionTranscript, + Self::SessionArchive, + Self::SessionRestore, + Self::SessionInterrupt, + Self::WorkspaceList, + Self::SessionCreate, + Self::ChatDefaults, + Self::ChatSend, + Self::ChatStatus, + Self::ChatCancel, + Self::ChatSubscribe, + Self::ApprovalsSummary, + Self::ApprovalsShowWindow, + Self::ApprovalsList, + Self::ApprovalDetail, + Self::ApprovalDecide, + ]; + + /// The command name exactly as the frontend invokes it. + #[must_use] + pub const fn name(self) -> &'static str { + match self { + Self::AppInfo => "app_info", + Self::SessionTree => "session_tree", + Self::SessionSearch => "session_search", + Self::SessionTranscript => "session_transcript", + Self::SessionArchive => "session_archive", + Self::SessionRestore => "session_restore", + Self::SessionInterrupt => "session_interrupt", + Self::WorkspaceList => "workspace_list", + Self::SessionCreate => "session_create", + Self::ChatDefaults => "chat_defaults", + Self::ChatSend => "chat_send", + Self::ChatStatus => "chat_status", + Self::ChatCancel => "chat_cancel", + Self::ChatSubscribe => "chat_subscribe", + Self::ApprovalsSummary => "approvals_summary", + Self::ApprovalsShowWindow => "approvals_show_window", + Self::ApprovalsList => "approvals_list", + Self::ApprovalDetail => "approval_detail", + Self::ApprovalDecide => "approval_decide", + } + } + + /// The one window allowed to call this command. + #[must_use] + pub const fn window(self) -> WindowLabel { + match self { + Self::ApprovalsList | Self::ApprovalDetail | Self::ApprovalDecide => { + WindowLabel::Approvals + } + Self::AppInfo + | Self::SessionTree + | Self::SessionSearch + | Self::SessionTranscript + | Self::SessionArchive + | Self::SessionRestore + | Self::SessionInterrupt + | Self::WorkspaceList + | Self::SessionCreate + | Self::ChatDefaults + | Self::ChatSend + | Self::ChatStatus + | Self::ChatCancel + | Self::ChatSubscribe + | Self::ApprovalsSummary + | Self::ApprovalsShowWindow => WindowLabel::Main, + } + } +} + +/// Allows `command` only when `caller_label` is exactly its window's label. +/// +/// Call this first in every handler, with the label of the webview that sent +/// the request (never a label taken from the request body). An unknown or empty +/// label is refused. +/// +/// # Errors +/// +/// Returns `forbidden_window` when the caller is not the command's window. +pub fn authorize(command: Command, caller_label: &str) -> Result<(), CommandError> { + if caller_label == command.window().as_str() { + Ok(()) + } else { + Err(CommandError::forbidden_window()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::BTreeSet; + + include!("command_names.rs"); + + fn with_window(window: WindowLabel) -> Vec { + Command::ALL + .into_iter() + .filter(|command| command.window() == window) + .collect() + } + + #[test] + fn every_command_has_exactly_one_window_and_the_counts_match() { + assert_eq!(Command::ALL.len(), 19); + assert_eq!(with_window(WindowLabel::Main).len(), 16); + assert_eq!(with_window(WindowLabel::Approvals).len(), 3); + } + + #[test] + fn approval_commands_are_approvals_only() { + for command in [ + Command::ApprovalsList, + Command::ApprovalDetail, + Command::ApprovalDecide, + ] { + assert_eq!(command.window(), WindowLabel::Approvals); + assert_eq!(authorize(command, "approvals"), Ok(())); + for label in ["main", "unknown", "", "Approvals", "approvals ", "main\0"] { + assert_eq!( + authorize(command, label), + Err(CommandError::forbidden_window()), + "{} must be refused for {label:?}", + command.name() + ); + } + } + } + + #[test] + fn main_commands_are_refused_for_every_other_label() { + for command in with_window(WindowLabel::Main) { + assert_eq!(authorize(command, "main"), Ok(())); + for label in ["approvals", "unknown", ""] { + assert_eq!( + authorize(command, label), + Err(CommandError::forbidden_window()), + "{} must be refused for {label:?}", + command.name() + ); + } + } + } + + #[test] + fn names_are_unique_snake_case() { + let names: BTreeSet<&str> = Command::ALL.iter().map(|c| c.name()).collect(); + assert_eq!(names.len(), Command::ALL.len()); + for name in names { + assert!(!name.is_empty()); + assert!( + name.bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_'), + "{name} is not snake_case" + ); + assert!(!name.starts_with('_') && !name.ends_with('_') && !name.contains("__")); + } + } + + #[test] + fn shared_name_list_has_nineteen_unique_names_matching_the_enum() { + let shared: BTreeSet<&str> = COMMAND_NAMES.iter().copied().collect(); + assert_eq!(COMMAND_NAMES.len(), 19); + assert_eq!(shared.len(), 19); + let from_enum: Vec<&str> = Command::ALL.iter().map(|c| c.name()).collect(); + assert_eq!(from_enum, COMMAND_NAMES); + } +} diff --git a/crates/omgcode-desktop/src/shutdown.rs b/crates/omgcode-desktop/src/shutdown.rs new file mode 100644 index 0000000..9bf0a69 --- /dev/null +++ b/crates/omgcode-desktop/src/shutdown.rs @@ -0,0 +1,117 @@ +//! Bounded, idempotent shutdown. +//! +//! Closing `main` (or any other exit path) must not freeze the UI thread for +//! the service's grace period. The first request hides both windows at once, +//! then runs [`DesktopService::shutdown`] on a separate thread and exits the +//! application. A request that arrives while shutdown runs does nothing. +//! +//! A streamed model request is interrupted by the cancel request, so its turn +//! ends inside the grace period (ADR-0050). A turn still waiting when the grace +//! period ends is abandoned: a request that is not streamed cannot be +//! interrupted. Its journal keeps an open turn, which the operator can close +//! later with the interrupt command or the trusted CLI. + +use std::sync::Arc; +use std::sync::atomic::{AtomicU8, Ordering}; +use std::time::Duration; + +use omgcode_app::desktop::DesktopService; +use tauri::{AppHandle, Manager}; + +use crate::policy::WindowLabel; + +/// How long running turns get to stop before the process exits anyway. +pub const GRACE: Duration = Duration::from_secs(5); + +const IDLE: u8 = 0; +const RUNNING: u8 = 1; +const DONE: u8 = 2; + +/// The shutdown progress, shared as Tauri state. +#[derive(Debug, Default)] +pub struct ShutdownGate { + phase: AtomicU8, +} + +impl ShutdownGate { + /// Moves from idle to running. Returns `true` for exactly one caller. + #[must_use] + pub fn begin(&self) -> bool { + self.phase + .compare_exchange(IDLE, RUNNING, Ordering::AcqRel, Ordering::Acquire) + .is_ok() + } + + /// Records that the bounded shutdown has finished. + pub fn finish(&self) { + self.phase.store(DONE, Ordering::Release); + } + + /// Whether the bounded shutdown has finished, so an exit may proceed. + #[must_use] + pub fn is_done(&self) -> bool { + self.phase.load(Ordering::Acquire) == DONE + } +} + +/// Starts the shutdown once. Later calls, from any thread, do nothing. +/// +/// Both windows are hidden first so the user sees the application close at +/// once. The service shutdown and the final `exit(code)` run on one new thread. +pub fn begin(app: &AppHandle, code: i32) { + let gate = app.state::(); + if !gate.begin() { + return; + } + for label in [WindowLabel::Main, WindowLabel::Approvals] { + if let Some(window) = app.get_webview_window(label.as_str()) + && window.hide().is_err() + { + eprintln!("omgcode-desktop: could not hide a window during shutdown"); + } + } + let service = Arc::clone(&*app.state::>()); + let handle = app.clone(); + let spawned = std::thread::Builder::new() + .name("omgcode-shutdown".to_owned()) + .spawn(move || { + let report = service.shutdown(GRACE); + if report.still_running > 0 { + eprintln!( + "omgcode-desktop: {} chat turn(s) were still waiting on the provider and \ + were abandoned; interrupt each session to close its open turn", + report.still_running + ); + } + handle.state::().finish(); + handle.exit(code); + }); + if spawned.is_err() { + // No thread: exit now rather than hang with hidden windows. Open turns stay open. + eprintln!("omgcode-desktop: could not start the shutdown thread"); + gate.finish(); + app.exit(code); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_the_first_begin_wins_and_finish_is_observed() { + // Given a fresh gate. + let gate = ShutdownGate::default(); + assert!(!gate.is_done()); + + // When shutdown is requested twice. + assert!(gate.begin()); + assert!(!gate.begin()); + + // Then it is not done until finished, and stays begun afterwards. + assert!(!gate.is_done()); + gate.finish(); + assert!(gate.is_done()); + assert!(!gate.begin()); + } +} diff --git a/crates/omgcode-desktop/src/windows.rs b/crates/omgcode-desktop/src/windows.rs new file mode 100644 index 0000000..66aa1ba --- /dev/null +++ b/crates/omgcode-desktop/src/windows.rs @@ -0,0 +1,207 @@ +//! Window creation, the navigation guard, and close behaviour. +//! +//! Both windows are created in Rust so that the navigation and new-window +//! hooks are attached before the first page loads. Neither window may leave +//! the application's own origin: the webviews hold the IPC capability, so an +//! external page inside one would be a privilege problem. + +use tauri::webview::NewWindowResponse; +use tauri::{App, Url, WebviewUrl, WebviewWindowBuilder, WindowEvent}; + +use crate::policy::WindowLabel; +use crate::shutdown; + +/// The development server origin. Honoured in debug builds only. +const DEV_HOST: &str = "localhost"; +const DEV_PORT: u16 = 1420; + +/// Tauri's bundled-asset origin: `http://tauri.localhost` on Windows (the +/// default scheme; `useHttpsScheme` stays off because the CSP names +/// `http://ipc.localhost`) and `tauri://localhost` elsewhere. +const APP_HOST: &str = "localhost"; +const WINDOWS_APP_HOST: &str = "tauri.localhost"; + +/// Decides whether a webview may navigate to `url`. +/// +/// Allowed: the platform's bundled-asset origin, and `http://localhost:1420` in +/// debug builds. Everything else is denied, including `about:blank`, `file:`, +/// `data:`, `javascript:`, `blob:`, other loopback addresses and ports, and any +/// URL with credentials. The origin is compared on parsed scheme, host, and +/// port, never on a string prefix, so `tauri.localhost.evil.test` and +/// `http://tauri.localhost@evil.test/` are refused. `about:blank` is denied +/// because no page in this application needs it. +#[must_use] +pub fn navigation_allowed(url: &Url, debug: bool) -> bool { + allowed_for(url, cfg!(windows), debug) +} + +/// [`navigation_allowed`] with the platform made explicit so both can be tested. +fn allowed_for(url: &Url, windows: bool, debug: bool) -> bool { + if !url.username().is_empty() || url.password().is_some() { + return false; + } + let Some(host) = url.host_str() else { + return false; + }; + let port = url.port(); + match url.scheme() { + "http" => { + (windows && host == WINDOWS_APP_HOST && port.is_none()) + || (debug && host == DEV_HOST && port == Some(DEV_PORT)) + } + "tauri" => !windows && host == APP_HOST && port.is_none(), + _ => false, + } +} + +/// Creates the `main` and `approvals` windows. +/// +/// `approvals` starts hidden. Devtools exist only in debug builds. File +/// drag-and-drop is disabled so a dropped file cannot navigate a webview. +/// +/// # Errors +/// +/// Returns the runtime's error when a window cannot be built. +pub fn create_windows(app: &App) -> tauri::Result<()> { + let debug = cfg!(debug_assertions); + + let main = WebviewWindowBuilder::new( + app, + WindowLabel::Main.as_str(), + WebviewUrl::App("index.html".into()), + ) + .title("OMGCode") + .inner_size(1200.0, 800.0) + .min_inner_size(800.0, 560.0) + .disable_drag_drop_handler() + .devtools(debug) + .on_navigation(move |url| navigation_allowed(url, debug)) + .on_new_window(|_, _| NewWindowResponse::Deny) + .build()?; + let handle = app.handle().clone(); + main.on_window_event(move |event| { + // Closing `main` ends the application; the hidden approvals window must + // not keep the process alive. The close is held back so both windows can + // be hidden at once and the bounded shutdown can run off this thread. + if let WindowEvent::CloseRequested { api, .. } = event { + api.prevent_close(); + shutdown::begin(&handle, 0); + } + }); + + let approvals = WebviewWindowBuilder::new( + app, + WindowLabel::Approvals.as_str(), + WebviewUrl::App("approvals.html".into()), + ) + .title("OMGCode \u{2014} Tool approvals") + .inner_size(760.0, 720.0) + .min_inner_size(560.0, 480.0) + .visible(false) + .disable_drag_drop_handler() + .devtools(debug) + .on_navigation(move |url| navigation_allowed(url, debug)) + .on_new_window(|_, _| NewWindowResponse::Deny) + .build()?; + let hidden = approvals.clone(); + approvals.on_window_event(move |event| { + // Closing hides the window so its state and the process survive. + if let WindowEvent::CloseRequested { api, .. } = event { + api.prevent_close(); + if hidden.hide().is_err() { + eprintln!("omgcode-desktop: could not hide the approvals window"); + } + } + }); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn url(text: &str) -> Url { + Url::parse(text).expect("test URL parses") + } + + #[test] + fn app_origin_is_allowed_per_platform() { + assert!(allowed_for( + &url("http://tauri.localhost/index.html"), + true, + false + )); + assert!(allowed_for(&url("http://tauri.localhost/"), true, true)); + assert!(allowed_for( + &url("tauri://localhost/approvals.html"), + false, + false + )); + // The other platform's origin is not accepted. + assert!(!allowed_for(&url("tauri://localhost/"), true, false)); + assert!(!allowed_for(&url("http://tauri.localhost/"), false, false)); + assert!(!allowed_for(&url("https://tauri.localhost/"), true, false)); + } + + #[test] + fn dev_server_is_allowed_only_in_debug() { + for windows in [true, false] { + assert!(allowed_for(&url("http://localhost:1420/"), windows, true)); + assert!(!allowed_for(&url("http://localhost:1420/"), windows, false)); + assert!(!allowed_for(&url("http://localhost:1421/"), windows, true)); + assert!(!allowed_for(&url("http://localhost/"), windows, true)); + assert!(!allowed_for(&url("https://localhost:1420/"), windows, true)); + } + } + + #[test] + fn foreign_and_unusual_urls_are_denied() { + for windows in [true, false] { + for debug in [true, false] { + for text in [ + "https://example.com/", + "http://127.0.0.1:1420/", + "http://[::1]:1420/", + "file:///", + "file:///C:/Windows/win.ini", + "data:text/html,

x

", + "javascript:alert(1)", + "about:blank", + "blob:http://tauri.localhost/1", + "http://tauri.localhost.evil.test/", + "http://tauri.localhost@evil.test/", + "http://tauri.localhost:80@evil.test/", + "http://evil.test@tauri.localhost/", + "http://user:pw@tauri.localhost/", + "http://tauri.localhost:8080/", + "http://evil.test/tauri.localhost", + "tauri://localhost.evil.test/", + "tauri://evil@localhost/", + "ipc://localhost/app_info", + ] { + assert!( + !allowed_for(&url(text), windows, debug), + "{text} must be denied (windows={windows}, debug={debug})" + ); + } + } + } + } + + #[test] + fn host_case_and_default_port_normalize() { + assert!(allowed_for(&url("http://TAURI.LOCALHOST:80/"), true, false)); + assert!(allowed_for(&url("HTTP://Tauri.Localhost/"), true, false)); + } + + #[test] + fn public_function_uses_the_build_platform() { + let own = if cfg!(windows) { + "http://tauri.localhost/" + } else { + "tauri://localhost/" + }; + assert!(navigation_allowed(&url(own), false)); + assert!(!navigation_allowed(&url("https://example.com/"), true)); + } +} diff --git a/crates/omgcode-desktop/tauri.conf.json b/crates/omgcode-desktop/tauri.conf.json new file mode 100644 index 0000000..67566b5 --- /dev/null +++ b/crates/omgcode-desktop/tauri.conf.json @@ -0,0 +1,30 @@ +{ + "$schema": "https://schema.tauri.app/config/2", + "productName": "OMGCode", + "identifier": "org.omg-software.omgcode", + "build": { + "frontendDist": "ui/dist", + "devUrl": "http://localhost:1420", + "beforeDevCommand": { "script": "npm run dev", "cwd": "ui" }, + "beforeBuildCommand": { "script": "npm run build", "cwd": "ui" } + }, + "app": { + "withGlobalTauri": false, + "windows": [], + "security": { + "csp": "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; connect-src ipc: http://ipc.localhost; base-uri 'none'; form-action 'none'; frame-ancestors 'none'", + "freezePrototype": true, + "assetProtocol": { "enable": false, "scope": [] }, + "capabilities": ["main-window", "approvals-window"] + } + }, + "bundle": { + "active": false, + "icon": [ + "icons/32x32.png", + "icons/128x128.png", + "icons/128x128@2x.png", + "icons/icon.ico" + ] + } +} diff --git a/crates/omgcode-desktop/tests/launch_flags.rs b/crates/omgcode-desktop/tests/launch_flags.rs new file mode 100644 index 0000000..04c1b51 --- /dev/null +++ b/crates/omgcode-desktop/tests/launch_flags.rs @@ -0,0 +1,81 @@ +//! The real `omgcode-desktop` binary refuses `--endpoint` beside +//! `--provider-profile` before any window or service exists (ADR-0048). +//! +//! Both cases fail during argument handling or the profile check, so no window +//! opens and the process exits at once. + +use std::process::{Command, Output}; + +fn run(args: &[&str]) -> Output { + Command::new(env!("CARGO_BIN_EXE_omgcode-desktop")) + .args(args) + .output() + .expect("run omgcode-desktop") +} + +#[test] +fn an_endpoint_beside_a_profile_prints_the_usage_and_exits_with_two() { + let root = std::env::temp_dir().join(format!("omgcode-desktop-flags-{}", std::process::id())); + let data = root.to_string_lossy().into_owned(); + for args in [ + &[ + "--data-dir", + data.as_str(), + "--endpoint", + "http://127.0.0.1:9/v1", + "--provider-profile", + "lms", + ][..], + &[ + "--provider-profile", + "lms", + "--endpoint", + "http://127.0.0.1:1234/v1", + ], + ] { + let output = run(args); + assert_eq!(output.status.code(), Some(2), "{args:?}"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.starts_with("usage: omgcode-desktop"), "{stderr}"); + assert!(stderr.contains("cannot be combined with --endpoint")); + } + assert!(!root.exists(), "a usage error created the data root"); +} + +#[test] +fn a_bad_stream_value_prints_the_usage_and_exits_with_two_without_a_window() { + // Given a scratch data directory that must stay uncreated. + let root = std::env::temp_dir().join(format!( + "omgcode-desktop-flags-stream-{}", + std::process::id() + )); + let data = root.to_string_lossy().into_owned(); + + // When the binary is started with a stream value that is not on or off. + let output = run(&["--data-dir", data.as_str(), "--stream", "maybe"]); + + // Then it prints the usage, exits 2 at argument handling, and opened + // nothing: it returned at once and created no data directory. + assert_eq!(output.status.code(), Some(2)); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.starts_with("usage: omgcode-desktop"), "{stderr}"); + assert!(stderr.contains("--stream "), "{stderr}"); + assert!(!root.exists(), "a usage error created the data root"); +} + +#[test] +fn a_profile_with_no_settings_store_exits_with_one_and_creates_nothing() { + let root = std::env::temp_dir().join(format!( + "omgcode-desktop-flags-store-{}", + std::process::id() + )); + let data = root.to_string_lossy().into_owned(); + let output = run(&["--data-dir", data.as_str(), "--provider-profile", "lms"]); + assert_eq!(output.status.code(), Some(1)); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + stderr.starts_with("omgcode-desktop: credential: no settings store exists"), + "{stderr}" + ); + assert!(!root.exists(), "the launch check created the data root"); +} diff --git a/crates/omgcode-desktop/tests/layers.rs b/crates/omgcode-desktop/tests/layers.rs new file mode 100644 index 0000000..b8d8f71 --- /dev/null +++ b/crates/omgcode-desktop/tests/layers.rs @@ -0,0 +1,743 @@ +//! Agreement between the layers that gate a command: the Rust policy, the +//! capability files, the build manifest, the frontend's calls, and the Tauri +//! configuration. None of these tests starts a webview. + +use std::collections::{BTreeMap, BTreeSet}; +use std::fs; +use std::path::{Path, PathBuf}; + +use omgcode_app::desktop::{DesktopConfig, DesktopService, SessionRequest}; +use omgcode_desktop_lib::error::CommandError; +use omgcode_desktop_lib::policy::{Command, WindowLabel}; +use serde_json::Value; + +const COMMAND_NAMES_SOURCE: &str = include_str!("../src/command_names.rs"); + +fn crate_dir() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) +} + +fn read(path: &Path) -> String { + fs::read_to_string(path).unwrap_or_else(|error| panic!("read {}: {error}", path.display())) +} + +fn json(path: &Path) -> Value { + serde_json::from_str(&read(path)) + .unwrap_or_else(|error| panic!("parse {}: {error}", path.display())) +} + +/// The permission identifier Tauri generates for an application command. +fn permission_for(command: Command) -> String { + format!("allow-{}", command.name().replace('_', "-")) +} + +fn commands_for(window: WindowLabel) -> BTreeSet<&'static str> { + Command::ALL + .into_iter() + .filter(|command| command.window() == window) + .map(Command::name) + .collect() +} + +fn capability_files() -> Vec { + let mut files: Vec = fs::read_dir(crate_dir().join("capabilities")) + .expect("capabilities directory") + .map(|entry| entry.expect("entry").path()) + .collect(); + files.sort(); + files +} + +fn capability_for(window: WindowLabel) -> Value { + let matching: Vec = capability_files() + .iter() + .map(|path| json(path)) + .filter(|capability| capability["windows"] == serde_json::json!([window.as_str()])) + .collect(); + assert_eq!(matching.len(), 1, "one capability per window"); + matching.into_iter().next().expect("one capability") +} + +fn string_array(value: &Value) -> Vec { + value + .as_array() + .expect("array") + .iter() + .map(|item| item.as_str().expect("plain string entry").to_owned()) + .collect() +} + +#[test] +fn capability_grants_equal_the_policy_for_each_window() { + for window in [WindowLabel::Main, WindowLabel::Approvals] { + // Given the capability that names this window. + let capability = capability_for(window); + + // When its permissions are read. + let granted = string_array(&capability["permissions"]); + let granted_set: BTreeSet<&str> = granted.iter().map(String::as_str).collect(); + + // Then they are exactly the policy's commands for the window, once each. + let expected: Vec = Command::ALL + .into_iter() + .filter(|command| command.window() == window) + .map(permission_for) + .collect(); + let expected_set: BTreeSet<&str> = expected.iter().map(String::as_str).collect(); + assert_eq!(granted_set, expected_set, "{window:?} grants"); + assert_eq!(granted.len(), expected.len(), "no duplicate grants"); + assert!( + granted.iter().all(|p| !p.contains(':')), + "no core or plugin permission" + ); + } +} + +#[test] +fn each_capability_names_one_local_window_and_no_remote_url() { + let files = capability_files(); + assert_eq!(files.len(), 2, "exactly two capability files"); + for path in files { + let capability = json(&path); + let windows = string_array(&capability["windows"]); + assert_eq!(windows.len(), 1, "{}", path.display()); + assert!( + !windows[0].contains(['*', '?', '[']), + "no glob in {}", + path.display() + ); + assert_eq!(capability["local"], Value::Bool(true)); + assert!(capability.get("remote").is_none(), "no remote access"); + assert!(capability.get("webviews").is_none(), "windows only"); + assert!(capability.get("platforms").is_none(), "all platforms"); + } + let labels: BTreeSet = capability_files() + .iter() + .flat_map(|path| string_array(&json(path)["windows"])) + .collect(); + assert_eq!( + labels, + BTreeSet::from(["main".to_owned(), "approvals".to_owned()]) + ); +} + +#[test] +fn decision_commands_are_granted_to_no_other_window() { + let main = string_array(&capability_for(WindowLabel::Main)["permissions"]); + for command in [ + Command::ApprovalsList, + Command::ApprovalDetail, + Command::ApprovalDecide, + ] { + assert!(!main.contains(&permission_for(command))); + } + let approvals = string_array(&capability_for(WindowLabel::Approvals)["permissions"]); + assert_eq!(approvals.len(), 3); +} + +#[test] +fn build_script_declares_the_shared_command_list() { + // Given the shared list and the build script. + let build = read(&crate_dir().join("build.rs")); + + // Then the script includes that file, so the manifest is the policy's list. + assert!(build.contains("include!(\"src/command_names.rs\")")); + assert!(build.contains(".commands(COMMAND_NAMES)")); + + // And the shared list holds the 19 unique policy names. + let listed: Vec<&str> = COMMAND_NAMES_SOURCE + .lines() + .filter_map(|line| line.trim().strip_prefix('"')) + .filter_map(|rest| rest.strip_suffix("\",")) + .collect(); + let unique: BTreeSet<&str> = listed.iter().copied().collect(); + assert_eq!(listed.len(), 19); + assert_eq!(unique.len(), 19); + let policy: Vec<&str> = Command::ALL.into_iter().map(Command::name).collect(); + assert_eq!(listed, policy); +} + +/// Command names passed to `invokeCommand("name", ...)` in a TypeScript file. +fn invoked_commands(source: &str) -> BTreeSet { + let mut found = BTreeSet::new(); + let mut rest = source; + while let Some(at) = rest.find("invokeCommand<") { + rest = &rest[at + "invokeCommand<".len()..]; + let Some(open) = rest.find(">(\"") else { break }; + // The generic argument and `(` must belong to one expression. + if rest[..open].contains([';', '\n']) { + continue; + } + let name_start = open + 3; + if let Some(len) = rest[name_start..].find('"') { + found.insert(rest[name_start..name_start + len].to_owned()); + } + } + found +} + +/// Command names passed to `invokeWithChannel("name", ...)`, the push-channel +/// form of an invocation (`ui/src/ipc/chat_events.ts`). +fn channel_commands(source: &str) -> BTreeSet { + const CALL: &str = "invokeWithChannel(\""; + let mut found = BTreeSet::new(); + let mut rest = source; + while let Some(at) = rest.find(CALL) { + rest = &rest[at + CALL.len()..]; + if let Some(len) = rest.find('"') { + found.insert(rest[..len].to_owned()); + } + } + found +} + +/// The argument keys `invokeWithChannel` in `client.ts` passes to the command it +/// runs, once per transport form (the real `Channel` and the plain object). +fn channel_argument_keys(client: &str) -> Vec> { + const CALL: &str = "invokeCommand(command, "; + let start = client + .find("export async function invokeWithChannel(") + .expect("invokeWithChannel is defined"); + let body = &client[start..]; + // The function ends at the first closing brace in column zero. + let end = body.find("\n}").expect("end of invokeWithChannel"); + let body = &body[..end]; + let mut found = Vec::new(); + let mut rest = body; + while let Some(at) = rest.find(CALL) { + rest = &rest[at + CALL.len()..]; + assert!(rest.starts_with('{'), "an argument object follows the name"); + let close = matching(rest, 0); + found.push( + split_top_level(&rest[1..close]) + .into_iter() + .map(|entry| { + entry + .split_once(':') + .map_or(entry.as_str(), |(key, _)| key) + .trim() + .to_owned() + }) + .collect(), + ); + } + found +} + +#[test] +fn frontend_clients_call_only_commands_the_policy_gives_their_window() { + // Given the two IPC client modules. + let ui = crate_dir().join("ui/src/ipc"); + let main_calls = invoked_commands(&read(&ui.join("client.ts"))); + let approval_calls = invoked_commands(&read(&ui.join("approvals_client.ts"))); + // The one push-channel command is invoked from `chat_events.ts`. + let channel_calls = channel_commands(&read(&ui.join("chat_events.ts"))); + assert_eq!( + channel_calls, + BTreeSet::from(["chat_subscribe".to_owned()]), + "the push channel opens exactly one command" + ); + let main_calls: BTreeSet = main_calls.union(&channel_calls).cloned().collect(); + + // Then the main side uses a subset of the main commands (all 16 today)... + let main_commands: BTreeSet = commands_for(WindowLabel::Main) + .into_iter() + .map(str::to_owned) + .collect(); + assert!(main_calls.is_subset(&main_commands), "{main_calls:?}"); + assert_eq!(main_calls.len(), 16); + assert_eq!(main_calls, main_commands); + + // ...the approvals client uses exactly the three approval commands... + let approval_commands: BTreeSet = commands_for(WindowLabel::Approvals) + .into_iter() + .map(str::to_owned) + .collect(); + assert_eq!(approval_calls, approval_commands); + + // ...and together they cover all 19 policy commands. + let union: BTreeSet = main_calls.union(&approval_calls).cloned().collect(); + let all: BTreeSet = Command::ALL + .into_iter() + .map(|c| c.name().to_owned()) + .collect(); + assert_eq!(union, all); +} + +fn csp_directives(csp: &str) -> Vec<(String, Vec)> { + csp.split(';') + .map(str::trim) + .filter(|directive| !directive.is_empty()) + .map(|directive| { + let mut parts = directive.split_whitespace().map(str::to_owned); + let name = parts.next().expect("directive name"); + (name, parts.collect()) + }) + .collect() +} + +#[test] +fn tauri_config_is_locked_down() { + let config = json(&crate_dir().join("tauri.conf.json")); + + // Globals, bundling, windows, and assets. + assert_eq!(config["app"]["withGlobalTauri"], Value::Bool(false)); + assert_eq!(config["bundle"]["active"], Value::Bool(false)); + assert_eq!(config["app"]["windows"], serde_json::json!([])); + assert_eq!( + config["app"]["security"]["freezePrototype"], + Value::Bool(true) + ); + assert_eq!( + config["app"]["security"]["assetProtocol"]["enable"], + Value::Bool(false) + ); + assert!( + config["app"]["security"] + .as_object() + .expect("security") + .keys() + .all(|key| !key.starts_with("dangerous")), + "no dangerous flag" + ); + + // The capability list is exactly the two files' identifiers. + let listed = string_array(&config["app"]["security"]["capabilities"]); + let from_files: Vec = capability_files() + .iter() + .map(|path| { + json(path)["identifier"] + .as_str() + .expect("identifier") + .to_owned() + }) + .collect(); + let listed_set: BTreeSet<&String> = listed.iter().collect(); + let files_set: BTreeSet<&String> = from_files.iter().collect(); + assert_eq!(listed.len(), 2); + assert_eq!(listed_set, files_set); + + // The CSP carries each required directive and no loose source. + let csp = config["app"]["security"]["csp"] + .as_str() + .expect("csp string"); + let directives = csp_directives(csp); + let get = |name: &str| -> Vec { + let matching: Vec<_> = directives.iter().filter(|(n, _)| n == name).collect(); + assert_eq!(matching.len(), 1, "directive {name} appears once"); + matching[0].1.clone() + }; + assert_eq!(get("default-src"), ["'none'"]); + assert_eq!(get("script-src"), ["'self'"]); + assert_eq!(get("style-src"), ["'self'"]); + assert_eq!(get("img-src"), ["'self'"]); + assert_eq!(get("font-src"), ["'self'"]); + assert_eq!(get("connect-src"), ["ipc:", "http://ipc.localhost"]); + assert_eq!(get("base-uri"), ["'none'"]); + assert_eq!(get("form-action"), ["'none'"]); + assert_eq!(get("frame-ancestors"), ["'none'"]); + for (_, sources) in &directives { + for source in sources { + assert!( + !matches!(source.as_str(), "'unsafe-inline'" | "'unsafe-eval'" | "*"), + "loose source {source}" + ); + assert!(!source.starts_with("data:") && !source.starts_with("blob:")); + if source.starts_with("http://") || source.starts_with("https://") { + assert_eq!(source, "http://ipc.localhost"); + } + } + } +} + +// --------------------------------------------------------------------------- +// Handlers: registration, window policy call, and argument names. +// --------------------------------------------------------------------------- + +/// Every source file that may hold a `#[tauri::command]`. +fn handler_sources() -> Vec<(PathBuf, String)> { + let mut paths = vec![crate_dir().join("src/commands.rs")]; + let mut nested: Vec = fs::read_dir(crate_dir().join("src/commands")) + .expect("commands directory") + .map(|entry| entry.expect("entry").path()) + .filter(|path| path.extension().is_some_and(|ext| ext == "rs")) + .collect(); + nested.sort(); + paths.extend(nested); + paths + .into_iter() + .map(|path| { + let text = read(&path); + (path, text) + }) + .collect() +} + +/// One `#[tauri::command]` function as found in source text. +struct Handler { + name: String, + is_async: bool, + /// The parameter list as `(name, type)` pairs. + params: Vec<(String, String)>, + /// The text from the opening brace of the body to the end of the handler. + body: String, +} + +/// Splits `text` at commas that are not nested in `<>`, `()`, `[]`, or `{}`. +fn split_top_level(text: &str) -> Vec { + let mut parts = Vec::new(); + let mut depth = 0_i32; + let mut current = String::new(); + let mut previous = ' '; + for ch in text.chars() { + match ch { + '<' | '(' | '[' | '{' => depth += 1, + // `->` is not a closing angle bracket. + '>' if previous == '-' => {} + '>' | ')' | ']' | '}' => depth -= 1, + ',' if depth == 0 => { + parts.push(std::mem::take(&mut current)); + previous = ch; + continue; + } + _ => {} + } + current.push(ch); + previous = ch; + } + parts.push(current); + parts + .into_iter() + .map(|part| part.trim().to_owned()) + .filter(|part| !part.is_empty()) + .collect() +} + +/// Index of the delimiter closing the one opened at `open` in `text`. +fn matching(text: &str, open: usize) -> usize { + let bytes = text.as_bytes(); + let (opening, closing) = match bytes[open] { + b'(' => (b'(', b')'), + b'{' => (b'{', b'}'), + other => panic!("not a delimiter: {}", other as char), + }; + let mut depth = 0_i32; + for (offset, byte) in bytes[open..].iter().enumerate() { + if *byte == opening { + depth += 1; + } else if *byte == closing { + depth -= 1; + if depth == 0 { + return open + offset; + } + } + } + panic!("unbalanced delimiter"); +} + +fn parse_handlers(source: &str) -> Vec { + let test_module = source.find("#[cfg(test)]").unwrap_or(source.len()); + let source = &source[..test_module]; + let mut handlers = Vec::new(); + let mut rest = source; + while let Some(at) = rest.find( + " +#[tauri::command", + ) { + rest = &rest[at + + " +#[tauri::command" + .len()..]; + let end_of_handler = rest + .find( + " +#[tauri::command", + ) + .unwrap_or(rest.len()); + let chunk = &rest[..end_of_handler]; + let fn_at = chunk.find("fn ").expect("a function follows the attribute"); + let is_async = chunk[..fn_at].trim_end().ends_with("async"); + let after_fn = &chunk[fn_at + 3..]; + let paren = after_fn.find('(').expect("parameter list"); + let name = after_fn[..paren].trim().to_owned(); + let close = matching(after_fn, paren); + let params = split_top_level(&after_fn[paren + 1..close]) + .into_iter() + .map(|param| { + let (n, t) = param.split_once(':').expect("typed parameter"); + (n.trim().to_owned(), t.trim().to_owned()) + }) + .collect(); + let brace = close + after_fn[close..].find('{').expect("body"); + handlers.push(Handler { + name, + is_async, + params, + body: after_fn[brace..].to_owned(), + }); + } + handlers +} + +fn all_handlers() -> Vec { + handler_sources() + .iter() + .flat_map(|(_, text)| parse_handlers(text)) + .collect() +} + +fn injected(param: &(String, String)) -> bool { + let ty = param.1.as_str(); + ty.starts_with("Webview") || ty.starts_with("Service<") || ty.starts_with("State<") +} + +#[test] +fn generate_handler_registers_exactly_the_nineteen_policy_commands() { + // Given the invoke handler in lib.rs. + let lib = read(&crate_dir().join("src/lib.rs")); + let start = + lib.find("generate_handler![").expect("generate_handler") + "generate_handler![".len(); + let end = start + lib[start..].find(']').expect("closing bracket"); + + // When the registered function names (last path segment) are collected. + let registered: Vec<&str> = lib[start..end] + .split(',') + .map(str::trim) + .filter(|entry| !entry.is_empty()) + .map(|entry| entry.rsplit("::").next().expect("segment")) + .collect(); + + // Then they are the 19 shared names, once each. + let unique: BTreeSet<&str> = registered.iter().copied().collect(); + assert_eq!(registered.len(), 19); + assert_eq!(unique.len(), 19); + let shared: BTreeSet<&str> = COMMAND_NAMES_SOURCE + .lines() + .filter_map(|line| line.trim().strip_prefix('"')) + .filter_map(|rest| rest.strip_suffix("\",")) + .collect(); + assert_eq!(unique, shared); + + // And every registered name is a handler in the commands sources. + let defined: BTreeSet = all_handlers().into_iter().map(|h| h.name).collect(); + let defined: BTreeSet<&str> = defined.iter().map(String::as_str).collect(); + assert_eq!(defined, shared); +} + +#[test] +fn every_handler_authorizes_its_own_command_before_touching_the_service() { + let handlers = all_handlers(); + assert_eq!(handlers.len(), 19); + for handler in handlers { + // Given the command the function name denotes. + let command = Command::ALL + .into_iter() + .find(|command| command.name() == handler.name) + .unwrap_or_else(|| panic!("{} is not a policy command", handler.name)); + assert!( + handler + .params + .first() + .is_some_and(|p| p.1.starts_with("Webview")), + "{} must take the calling Webview first", + handler.name + ); + + // When the body is searched for its policy call. + let call = format!("authorize(Command::{command:?}, webview.label())?;"); + let authorize_at = handler + .body + .find(&call) + .unwrap_or_else(|| panic!("{} lacks `{call}`", handler.name)); + assert_eq!( + handler.body.matches("authorize(").count(), + 1, + "{} calls authorize exactly once", + handler.name + ); + + // Then nothing but the opening brace precedes it, and any service use follows it. + assert!( + handler.body[1..authorize_at].trim().is_empty(), + "{}: authorize must be the first statement", + handler.name + ); + if handler + .params + .iter() + .any(|p| p.1.starts_with("Service<") || p.1.starts_with("State<")) + { + assert!( + handler.is_async, + "{} uses the service, so it is async", + handler.name + ); + let state_at = handler.body.find("&state").expect("state is used"); + assert!( + authorize_at < state_at, + "{}: state used before authorize", + handler.name + ); + assert!( + handler.body.contains("blocking(&state"), + "{} must run the service through the blocking helper", + handler.name + ); + assert!( + !handler.body.contains("state.inner()"), + "{} must not reach the service except through the helper", + handler.name + ); + } + } +} + +#[test] +fn the_blocking_helper_uses_the_blocking_pool_and_maps_failures() { + let commands = read(&crate_dir().join("src/commands.rs")); + assert!(commands.contains("tauri::async_runtime::spawn_blocking")); + assert!(commands.contains("CommandError::from")); + assert!(commands.contains("CommandError::internal()")); +} + +/// The keys of every argument object the frontend passes to each command. +fn frontend_arguments(source: &str) -> BTreeMap> { + let mut found = BTreeMap::new(); + let mut rest = source; + while let Some(at) = rest.find("invokeCommand<") { + rest = &rest[at + "invokeCommand<".len()..]; + let Some(open) = rest.find(">(\"") else { break }; + if rest[..open].contains([';', '\n']) { + continue; + } + let name_start = open + 3; + let Some(len) = rest[name_start..].find('"') else { + break; + }; + let name = rest[name_start..name_start + len].to_owned(); + let after_name = &rest[name_start + len + 1..]; + let brace = after_name.find('{').expect("argument object"); + assert!( + after_name[..brace].trim() == ",", + "{name}: argument object must follow the name directly" + ); + let close = matching(after_name, brace); + let keys = split_top_level(&after_name[brace + 1..close]) + .into_iter() + .map(|entry| { + entry + .split_once(':') + .map_or(entry.as_str(), |(key, _)| key) + .trim() + .to_owned() + }) + .collect(); + found.insert(name, keys); + } + found +} + +#[test] +fn handler_parameters_match_the_keys_the_frontend_passes() { + // Given the argument keys each frontend call passes. + let ui = crate_dir().join("ui/src/ipc"); + let mut expected = frontend_arguments(&read(&ui.join("client.ts"))); + expected.extend(frontend_arguments(&read(&ui.join("approvals_client.ts")))); + // The push-channel command passes one argument, `on_event`, in both of the + // transport forms of `invokeWithChannel`. + let channel_keys = channel_argument_keys(&read(&ui.join("client.ts"))); + assert_eq!(channel_keys.len(), 2, "one call per transport form"); + for keys in &channel_keys { + assert_eq!(keys, &BTreeSet::from(["on_event".to_owned()])); + } + for name in channel_commands(&read(&ui.join("chat_events.ts"))) { + assert!( + expected + .insert(name.clone(), channel_keys[0].clone()) + .is_none(), + "{name} is also a plain invocation" + ); + } + assert_eq!( + expected.len(), + 19, + "all commands are called by the frontend" + ); + + // Then each handler's non-injected parameters are exactly those keys. + for handler in all_handlers() { + let actual: BTreeSet = handler + .params + .iter() + .filter(|param| !injected(param)) + .map(|param| param.0.clone()) + .collect(); + let wanted = expected + .get(&handler.name) + .unwrap_or_else(|| panic!("{} is never called by the frontend", handler.name)); + assert_eq!(&actual, wanted, "{} parameters", handler.name); + } +} + +#[test] +fn every_handler_uses_snake_case_argument_matching() { + for (path, text) in handler_sources() { + let attributes = text + .matches( + " +#[tauri::command", + ) + .count(); + let renamed = text + .matches( + " +#[tauri::command(rename_all = \"snake_case\")]", + ) + .count(); + assert_eq!(attributes, renamed, "{}", path.display()); + } +} + +#[test] +fn command_error_keeps_the_service_code_and_message() { + // Given a real service failure: archiving a session that does not exist. + let root = std::env::temp_dir().join(format!( + "omgcode-desktop-layers-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_nanos()) + )); + let service = DesktopService::open(DesktopConfig { + data_root: root.clone(), + endpoint: "http://127.0.0.1:1234/v1".to_owned(), + token_source: omgcode_app::TokenSource::Environment, + stream: true, + }) + .expect("open service"); + let failure = service + .session_archive(SessionRequest { + session_id: uuid::Uuid::from_u128(7), + }) + .expect_err("no such session"); + + // When it is converted for the webview. + let converted = CommandError::from(failure.clone()); + + // Then the serialized forms are identical and the accessors agree. + assert_eq!(converted.code(), failure.code()); + assert_eq!(converted.message(), failure.message()); + assert_eq!( + serde_json::to_value(&converted).expect("command error"), + serde_json::to_value(&failure).expect("desktop error") + ); + drop(service); + let _ = fs::remove_dir_all(root); +} + +#[test] +fn internal_error_is_fixed_and_carries_no_detail() { + let value = serde_json::to_value(CommandError::internal()).expect("serialize"); + assert_eq!(value["code"], "internal"); + assert_eq!(value["message"], "The command could not be completed."); +} diff --git a/crates/omgcode-desktop/ui/.npmrc b/crates/omgcode-desktop/ui/.npmrc new file mode 100644 index 0000000..7c6e338 --- /dev/null +++ b/crates/omgcode-desktop/ui/.npmrc @@ -0,0 +1,2 @@ +ignore-scripts=true +save-exact=true diff --git a/crates/omgcode-desktop/ui/README.md b/crates/omgcode-desktop/ui/README.md new file mode 100644 index 0000000..4c1435c --- /dev/null +++ b/crates/omgcode-desktop/ui/README.md @@ -0,0 +1,79 @@ +# OMGCode desktop UI + +Webview frontend for the Tauri 2 desktop app. The Rust core is the only authority. +This code renders model- and repository-controlled text and must treat it as hostile. + +Vanilla strict TypeScript, Vite (multi-page), Vitest with jsdom, and Biome for both +formatting and linting. The only runtime dependency is `@tauri-apps/api`. Dependencies are +pinned exactly, `package-lock.json` is committed, and `.npmrc` sets `ignore-scripts=true`. + +## Scripts + +| Script | Purpose | +| ------------------- | ------------------------------------------------------------------------------------- | +| `npm run dev` | Vite dev server on port 1420 (strict port) | +| `npm run build` | `tsc --noEmit`, then `vite build` into `dist/` | +| `npm run check` | Strict type check | +| `npm run lint` | `biome check .` (lint, format check, import order) | +| `npm run format` | `biome format --write .` | +| `npm test` | Vitest. The `dist/` checks run only if `dist/` exists; otherwise they show as skipped | +| `npm run coverage` | The same tests with coverage; writes `coverage/lcov.info` and prints a summary | +| `npm run test:dist` | Build, then run `tests/dist.test.ts` | +| `npm run tauri` | Tauri CLI, run from `crates/omgcode-desktop` (`cd .. && tauri`) | + +## Running the desktop app + +Run this from `crates/omgcode-desktop/ui` (the script changes to the parent directory, +where `tauri.conf.json` lives, so it works in PowerShell, Git Bash, and Linux shells): + +``` +npm run tauri -- dev -- -- --data-dir +``` + +- **Always pass `--data-dir`.** Without it the app uses the real per-user data directory, + including your real sessions and approvals. Use a fresh directory for development. +- The first `--` ends `npm` options, the second ends the Tauri CLI options, and the rest go to + the app. `tauri dev` starts Vite on port 1420 through `beforeDevCommand` (`cwd: ui`) and + builds the Rust shell without the `custom-protocol` feature. +- `npm run tauri -- --version` and `npm run tauri -- info` also work from here. +- `ui/dist` must exist before any `cargo check`, `clippy`, `build`, or `test` that includes + the desktop crate, because the Tauri build embeds it. Run `npm run build` first. + +## Windows + +- `index.html` is the main window: session tree, search, transcript, composer. +- `approvals.html` is the approvals window. Only this entry imports + `src/ipc/approvals_client.ts`, so the main bundle never contains `approval_decide` or + `approval_detail`. It renders no transcript text. + +## Rules + +- Untrusted text is rendered only as text nodes through `src/dom.ts`. Bidirectional override + characters are replaced by visible `[U+XXXX]` markers. No `innerHTML`, `eval`, string timers, + computed dynamic imports, `window.open`, links, or web storage. A test scans `src/`. +- No inline script or style, and no remote resources. The build must run under + `default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; + connect-src ipc: http://ipc.localhost; base-uri 'none'; form-action 'none'; frame-ancestors 'none'`. +- All IPC goes through `src/ipc/client.ts`, the only file that imports `@tauri-apps/api`. + Core error messages are shown as text. Client-side limits are usability only; the core + validates everything. +- The model endpoint is fixed when the desktop process starts. The composer shows the value + from `chat_defaults` as read-only text and sends back exactly that value. +- The main window subscribes once with `chat_subscribe`, a Tauri channel. It reads + `chat_status` once when a `finished` event arrives and keeps a five-second reconcile check + while a turn runs, in case an event is lost. If subscribing fails, it polls `chat_status` + every 750 ms instead. The command name lives in `src/ipc/chat_events.ts`, which only the main + window imports, so the approvals bundle never contains it. Streamed text is provisional + and is rendered as text. A cancelled turn shows `Turn cancelled.` and is recognised by the + `cancelled` error code, never by message text. Pollers (`src/poll.ts`) never overlap calls and pause while the + page is hidden; the approvals window still polls. + +## Mock backend + +When `import.meta.env.DEV` is true and Tauri is absent, `client.ts` loads `src/mock/` with a +literal dynamic `import()`. Production builds drop that branch. The mock holds 12 sessions in +all five statuses, a 255-entry transcript, hostile strings (markup, ``, a right-to-left +override, a 5,000-character token, a `javascript:` URL), a chat turn that completes after 2 s +(model `fail` fails; cancelling ends it at once as `cancelled`), and three approvals (two exact, one stale). `OMGCODE_MOCK_FIXTURE_MARKER` +must never appear in `dist/`; `tests/dist.test.ts` checks that. A production build with no +Tauri shows "This page must run inside OMGCode." diff --git a/crates/omgcode-desktop/ui/approvals.html b/crates/omgcode-desktop/ui/approvals.html new file mode 100644 index 0000000..8d967f8 --- /dev/null +++ b/crates/omgcode-desktop/ui/approvals.html @@ -0,0 +1,14 @@ + + + + + + + OMGCode approvals + + + +
+ + + diff --git a/crates/omgcode-desktop/ui/biome.json b/crates/omgcode-desktop/ui/biome.json new file mode 100644 index 0000000..f3d88e2 --- /dev/null +++ b/crates/omgcode-desktop/ui/biome.json @@ -0,0 +1,14 @@ +{ + "$schema": "https://biomejs.dev/schemas/2.6.8/schema.json", + "files": { "includes": ["**", "!dist", "!node_modules"] }, + "formatter": { "enabled": true, "indentStyle": "space", "indentWidth": 2, "lineWidth": 100 }, + "linter": { + "enabled": true, + "rules": { + "preset": "recommended", + "suspicious": { "noExplicitAny": "error", "noDebugger": "error", "noConsole": "error" }, + "style": { "noNonNullAssertion": "error", "useImportType": "error" } + } + }, + "assist": { "actions": { "source": { "organizeImports": "on" } } } +} diff --git a/crates/omgcode-desktop/ui/index.html b/crates/omgcode-desktop/ui/index.html new file mode 100644 index 0000000..4e77511 --- /dev/null +++ b/crates/omgcode-desktop/ui/index.html @@ -0,0 +1,14 @@ + + + + + + + OMGCode + + + +
+ + + diff --git a/crates/omgcode-desktop/ui/package-lock.json b/crates/omgcode-desktop/ui/package-lock.json new file mode 100644 index 0000000..c70b66b --- /dev/null +++ b/crates/omgcode-desktop/ui/package-lock.json @@ -0,0 +1,2553 @@ +{ + "name": "omgcode-desktop-ui", + "version": "0.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "omgcode-desktop-ui", + "version": "0.0.0", + "license": "Apache-2.0", + "dependencies": { + "@tauri-apps/api": "2.12.5" + }, + "devDependencies": { + "@biomejs/biome": "2.6.8", + "@tauri-apps/cli": "2.12.5", + "@vitest/coverage-v8": "5.1.0", + "jsdom": "31.0.1", + "typescript": "7.0.2", + "vite": "8.3.8", + "vitest": "5.1.0" + } + }, + "node_modules/@babel/helper-string-parser": { + "version": "7.29.14", + "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.29.14.tgz", + "integrity": "sha512-CXvKMFvM80ihI7Vox9jb+kQjnyjESQNPM+vB7U0Zz6TWuZA+Z/FL0QPMH300h33qJtIx+REeBhXqWOPW48TcAQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/helper-validator-identifier": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.29.7.tgz", + "integrity": "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/parser": { + "version": "7.29.14", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.29.14.tgz", + "integrity": "sha512-VL220vXuyO2z3rNIQwY7ftj9UwpqOt+33sH+sBWkOekbsSR2g7EEMnOx24ZlCH0xOA3DM/33SifVFGbnJD8r/w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/types": "^7.29.14" + }, + "bin": { + "parser": "bin/babel-parser.js" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@babel/types": { + "version": "7.29.14", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.29.14.tgz", + "integrity": "sha512-UjI/dKmwJwT9pOszJitq2G6EObZPnsZUfdCr92z2TlF3EZK9J3EdfOKUrCzHl6wFthbzwhzl1Soc/UIwxRiq6w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-string-parser": "^7.29.14", + "@babel/helper-validator-identifier": "^7.29.7" + }, + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@bcoe/v8-coverage": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-1.0.2.tgz", + "integrity": "sha512-6zABk/ECA/QYSCQ1NGiVwwbQerUCZ+TQbp64Q3AgmfNvurHH0j8TtXa1qbShXA6qqkpAj4V5W8pP6mLe1mcMqA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@biomejs/biome": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/biome/-/biome-2.6.8.tgz", + "integrity": "sha512-kqZUFvX6/aYQOmzfRN5ANcfNS9wLH4aHLWWP/dQKyhRZVoA6rdWszA7DGugFcxPbMiznUyVYyukVl+ExZN4PQA==", + "dev": true, + "license": "MIT OR Apache-2.0", + "bin": { + "biome": "bin/biome" + }, + "engines": { + "node": ">=14.21.3" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/biome" + }, + "optionalDependencies": { + "@biomejs/cli-android-arm64": "2.6.8", + "@biomejs/cli-android-x64": "2.6.8", + "@biomejs/cli-darwin-arm64": "2.6.8", + "@biomejs/cli-darwin-x64": "2.6.8", + "@biomejs/cli-linux-arm64": "2.6.8", + "@biomejs/cli-linux-arm64-musl": "2.6.8", + "@biomejs/cli-linux-x64": "2.6.8", + "@biomejs/cli-linux-x64-musl": "2.6.8", + "@biomejs/cli-win32-arm64": "2.6.8", + "@biomejs/cli-win32-x64": "2.6.8" + } + }, + "node_modules/@biomejs/cli-android-arm64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-android-arm64/-/cli-android-arm64-2.6.8.tgz", + "integrity": "sha512-X4JKPLK85kfUTwuuaORFplnBfGjKkp62PsFhhcL7F/Tpltc7pyUofjlkkMnlZZLWsifFl7LNB8ngVrwJg1uo1Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-android-x64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-android-x64/-/cli-android-x64-2.6.8.tgz", + "integrity": "sha512-C3Hk485vl+deEj53K08+SYYelPIPkdSZL3qHuD31Xi+qydVa4q2ze+aCYo1k3iJGWtvWtjHaGkDh7S2sX6wk8w==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-darwin-arm64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-arm64/-/cli-darwin-arm64-2.6.8.tgz", + "integrity": "sha512-DHyj+JnRHcn4YdvpiqgTFZMYKSPELMCfFpNNlEuu1L8DKTGXhzW7raIwdsbo2Ie9nCljQLAQkVWQk/mwM0ueiA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-darwin-x64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-x64/-/cli-darwin-x64-2.6.8.tgz", + "integrity": "sha512-QWWUPjlA0RnkkgYnFLUHZu+RrDc0FdK0A+wEPA+dENQJwsuSHMH89lspnAw2QUWWc9VHyfluGx1Xmp6Y2GZi8Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-arm64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-arm64/-/cli-linux-arm64-2.6.8.tgz", + "integrity": "sha512-0Bjim8lKwnrvXUHoI4k3Tb51BjoiLEwDnovCI8nSzkk6wj3dShiuAjrx6+X0Hjf4ejR1sRZgTQiSDHlpy9vB7w==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-arm64-musl": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-arm64-musl/-/cli-linux-arm64-musl-2.6.8.tgz", + "integrity": "sha512-WB9PhOitX+bRc0G3+GNJGlvbKoWn3JoB9z4E04JQizIC9aPCQyIvwvWG7QZHxw6k20Fi+dHLmZ2fSEmBa9HqiA==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-x64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-x64/-/cli-linux-x64-2.6.8.tgz", + "integrity": "sha512-XIk3pLQT56I6Tti+u0DF8FtSNXjCMTPbxVTzfJ9cS2t1ImVKvrqV5b52GuEaJg6/wuiXvz0sjfb5saB4TPRjzA==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-x64-musl": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-x64-musl/-/cli-linux-x64-musl-2.6.8.tgz", + "integrity": "sha512-64Yzo+J4fEk7Kf9SMnVdVxC+Y2A2e/L86QTP9JjGQ57lsddyELLpbBcNMxSJggre2T1AvISwwSrOApkBAHDqNA==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-win32-arm64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-win32-arm64/-/cli-win32-arm64-2.6.8.tgz", + "integrity": "sha512-ezjCEZzHIWE2pdh83a7zLVcAAcDaERcTcvfA7uWlxqz/1CuDxgtfO19pDg9B6YY4wKYIDvYBw2QCMnqIshSwmw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-win32-x64": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/@biomejs/cli-win32-x64/-/cli-win32-x64-2.6.8.tgz", + "integrity": "sha512-cD6HvGczZUiAYsY8s2TxxXj2AHFzEdMomvVJfEjzOjBM45nGqIDfBpbtvwGQzDCN8X2yKKk9u1jnDZisT2nxIg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT OR Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@emnapi/core": { + "version": "2.0.0-beta.2", + "resolved": "https://registry.npmjs.org/@emnapi/core/-/core-2.0.0-beta.2.tgz", + "integrity": "sha512-MzCVoWtcg4Pm2B3je5mMvYMvj3MmBKw+fU7vbsJc9/iT8ip6/eYIHo/Dsm/mr+wsHB6SEyJDuxB09Yy6r/8+4A==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@emnapi/wasi-threads": "2.2.0", + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/runtime": { + "version": "2.0.0-beta.2", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-2.0.0-beta.2.tgz", + "integrity": "sha512-JcrSPzPvZuNZvJqPTF9Fw25LbOJ8avzFVlaf2grmH4Cku0oGP47tRgqPZadcfj3YoZMHkFVnCYFbQM8I+i5QEw==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/wasi-threads": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@emnapi/wasi-threads/-/wasi-threads-2.2.0.tgz", + "integrity": "sha512-JP7OYH38Q7+pyTAe14raZHLQIK7BDKbdwji+Xi3pcd/6QkJYQH3SX4wu6EplGrGJWRepoNzedfm0G3B9Z4e9Rw==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@exodus/bytes": { + "version": "1.16.0", + "resolved": "https://registry.npmjs.org/@exodus/bytes/-/bytes-1.16.0.tgz", + "integrity": "sha512-IcpW84uEn3N7ETtNZMlxKhfl6Pec8rUNGOTBtWbK1FKhJxIFAptZyVrvVRVBimAJxJCgc3PxepxkdWWG4DVzfA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=24.0.0" + }, + "peerDependencies": { + "@noble/hashes": "^1.8.0 || ^2.0.0" + }, + "peerDependenciesMeta": { + "@noble/hashes": { + "optional": true + } + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.3.tgz", + "integrity": "sha512-lhTtjtoEArWBjismtfS761S9Hlyn+nIZqOZVYoZ1Xn82PMNMVnk+jj/NhC4fAXGTs7ydONcJiR5vyQBTMDu27g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz", + "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, + "node_modules/@napi-rs/wasm-runtime": { + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.2.6.tgz", + "integrity": "sha512-mRyDmEw9/fCDXCGfL1sbgIw/GUK0PVxRW4YwgPN61F3LQ+fSyoBFarJVzgGyhF6kRzEQZ0FejDqm4WLXSeqQmw==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@tybys/wasm-util": "^0.10.3" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=23.5.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + }, + "peerDependencies": { + "@emnapi/core": "^1.7.1 || 2.0.0-beta.2", + "@emnapi/runtime": "^1.7.1 || 2.0.0-beta.2" + } + }, + "node_modules/@oxc-project/types": { + "version": "0.156.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.156.0.tgz", + "integrity": "sha512-1Duyg48TdJNdyhEB8Xn1L36bT9Pv6GUtlKjHxOys8xkaF/rQq3XJE+bAurSK/AgIz5mGFsb77cn9Pb20+WCScw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/oxc-project" + } + }, + "node_modules/@rolldown/binding-android-arm-eabi": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.19.tgz", + "integrity": "sha512-e+Ebe0WT+9tbXmNKPIiC8YrWAHYwug3m6i4p3rZGGulfZri1jtI4HslT+OZnFf/OIHowWF71DTlHwXBP2SiA7A==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-android-arm64": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.2.19.tgz", + "integrity": "sha512-hOombrTJ+szt2ciEzS7oCA78lMPBgF5UMQWCJIyNfnrI7GLl8HYoFqCnpL2gcPOaXMqryGCU5ExdQKyDDcC+yA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.2.19.tgz", + "integrity": "sha512-XoMA2pwCKGTQzUjPgtRwkedH4WmXhdIPKT6l6oOxd1PUbNodGI6DcBekgN7EC//+x3pfBYX/qL1gNmZAqivw5g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-x64": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.2.19.tgz", + "integrity": "sha512-zo8EsSSwMZCY/e+fRnGaX02YGgvJD0yvhNi/9hpGPA2MJ9ZF8dPCw75UGfpI6OpQ/myR/dIYmogRvqiZD0TrXg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.2.19.tgz", + "integrity": "sha512-O8ISAOfI1nj7AuBPd30FSF47z7f7lzlk5poQVGgiikvQuGEBFYvW8ki+UEnT+YJPnNGjQzn304klt4O9FUE7mA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.2.19.tgz", + "integrity": "sha512-/lYCba5NO0yq4S0jGMH8QTBtkvQxoXjoQWzF5BcWp9MjB9jxVRD1GQDNofkVjcOj30DWyhLIN/4Oe60JKzaONw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.2.19.tgz", + "integrity": "sha512-pm/apFMBnSbFb3O6nG5pHO1iqHhZrjGDFEjSm+ot9J17XXPvH9L90y5h+jgfHCE+Qdlsvy8qKEcU4VKxgjD+vA==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.2.19.tgz", + "integrity": "sha512-K1SmQp3FFichn8oqrNIwvFoZ5wcvjC7goBzpaMNvLf9FZc+QB/QvLmnpJZgAAGHiD980tP8FZZnXk2K94rvlxg==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.2.19.tgz", + "integrity": "sha512-I0xwD57jP/3A7+5UPZin2w8Y7XHEmEM5zqpYZOcQ45MlNW6tUJUziymV3yRr7rL4vwyfEcRsRksC4a7Kc08p5Q==", + "cpu": [ + "ppc64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.2.19.tgz", + "integrity": "sha512-BrtEHT24SNXj9HiXqy+s/kXiSiQGt6EKCPUlRirAoIioVxKGnbXBUEkbZASAei7iZJB0/Ynm+0JP00Ipx7AnSw==", + "cpu": [ + "s390x" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.2.19.tgz", + "integrity": "sha512-rm75/9DkHKeU2WnA8w4X94WGYgLlDCFCfkQC+4bguVJmC/jvDRv2B+2wLOYhyE0fVuv7//Ckk39OoOufyCu76Q==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.2.19.tgz", + "integrity": "sha512-irh/4+iYFM4GAzpluDErgjz89bj6NDtqqMAG/6O1LG38Ed3NpyebzCpcVNkFW8uhy+/3/BocUvnXDR33CXWe9A==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.2.19.tgz", + "integrity": "sha512-+KWTWNoPt+CfSq94CiceWu3yPE+QzkaG+phPyz9G60tmZLwfQkcA4AeksafzvJAw4pvg1mcd5a1TkUqVv0s4JA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-wasm32-wasi": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-wasm32-wasi/-/binding-wasm32-wasi-1.2.19.tgz", + "integrity": "sha512-7EH+skOsl5qpWnK0vzamgJFhOv1cwxQpsxm8tI883K9fur5c27p8q87fzEO6BbBPabA2/+s/zR63/pLc137iOg==", + "cpu": [ + "wasm32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@emnapi/core": "2.0.0-beta.2", + "@emnapi/runtime": "2.0.0-beta.2", + "@napi-rs/wasm-runtime": "1.2.6" + }, + "engines": { + "node": "^20.19.0 || ^22.13.0 || >=23.5.0" + } + }, + "node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.2.19.tgz", + "integrity": "sha512-3Q1Yg1Q7tEvBWoZf5rFvG/GKZQPgwfru+p4wBajDfJFBs0SHCpL++dfTjW9gyAKd1T7ZpIYvdaUax7Pa/IhFVQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.2.19.tgz", + "integrity": "sha512-y/sqRRc6d7hECzOFc5n3LSeqctmIp9mMjY1Kq4s14uLPd/LZ4z7i1zrW6fMtp2mPeuQIC4e6ULMcwQzMomqQag==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/pluginutils": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.1.1.tgz", + "integrity": "sha512-glFIJDvLNDtJhT9qrMFWLPbvS+skxgt4L0K8Ei2YtnhPysAJnsI7nyxSJxKj4ADeKX0OLjC4ntRkwpLOoS3rwA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@tauri-apps/api": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/api/-/api-2.12.5.tgz", + "integrity": "sha512-G9QIMt5fzED+aS4QhVK3w1HWLKpLIwH/MnpCr9z1ugGEHnS2ORmqrLkc0uALOjRK4qXJy+4Yiw8LDl/KicC1Hg==", + "license": "Apache-2.0 OR MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/tauri" + } + }, + "node_modules/@tauri-apps/cli": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli/-/cli-2.12.5.tgz", + "integrity": "sha512-Dm8s/4aiE52HsBh+Gz3uSrh+fEykRICA1VoAnQA80IqHoTRncPg+q4rGvuhmyNsHUPg3QrPjUrTUSfWmXLVVEw==", + "dev": true, + "license": "Apache-2.0 OR MIT", + "bin": { + "tauri": "tauri.js" + }, + "engines": { + "node": ">= 10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/tauri" + }, + "optionalDependencies": { + "@tauri-apps/cli-darwin-arm64": "2.12.5", + "@tauri-apps/cli-darwin-x64": "2.12.5", + "@tauri-apps/cli-linux-arm-gnueabihf": "2.12.5", + "@tauri-apps/cli-linux-arm64-gnu": "2.12.5", + "@tauri-apps/cli-linux-arm64-musl": "2.12.5", + "@tauri-apps/cli-linux-riscv64-gnu": "2.12.5", + "@tauri-apps/cli-linux-x64-gnu": "2.12.5", + "@tauri-apps/cli-linux-x64-musl": "2.12.5", + "@tauri-apps/cli-win32-arm64-msvc": "2.12.5", + "@tauri-apps/cli-win32-ia32-msvc": "2.12.5", + "@tauri-apps/cli-win32-x64-msvc": "2.12.5" + } + }, + "node_modules/@tauri-apps/cli-darwin-arm64": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-darwin-arm64/-/cli-darwin-arm64-2.12.5.tgz", + "integrity": "sha512-VVvBRBdTFF7BP7RqGeItp9yDiyXj0DI1laRCEN2q6GLrNkw4wrHeWAxHp23PwtQtn/XET+yTN1jOKrq+Yor5PQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-darwin-x64": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-darwin-x64/-/cli-darwin-x64-2.12.5.tgz", + "integrity": "sha512-A84QYNj93kaG06N2mIFBRZM/X3lf9TyAVQeQwOmP2AJZ2wCt1WLDJgHRCvjeJmwJjJ5Ud0jUbIKbXg8NEGvsVQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-linux-arm-gnueabihf": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-linux-arm-gnueabihf/-/cli-linux-arm-gnueabihf-2.12.5.tgz", + "integrity": "sha512-tb/5xG5HV6CJR9AZBmTyI2GCi05vcH8XUjUIjBquAx7x+AE4f1vMWngvq7JW2AqYK8nNl79LJH1GR9Hf5QQc5g==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-linux-arm64-gnu": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-linux-arm64-gnu/-/cli-linux-arm64-gnu-2.12.5.tgz", + "integrity": "sha512-Vb7SlbIWlmIqekfsGYT64wH1cQNg5MET8XFyT6pUlVJ7Y/z6LsOIh4MUal0Z5FCPL83GMuEUNxpYUHpt36teHQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-linux-arm64-musl": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-linux-arm64-musl/-/cli-linux-arm64-musl-2.12.5.tgz", + "integrity": "sha512-b/YfVGueoTOtENQHY1STBF9XlU4uZDtOiMUErp2Nao/YrDWCKggjxxBZU3ycbeDWUYxl8U8M9seE4HYag1xjiQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-linux-riscv64-gnu": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-linux-riscv64-gnu/-/cli-linux-riscv64-gnu-2.12.5.tgz", + "integrity": "sha512-/EXbTfE9FM9GKSSRL9YTRHcMglTVNzbcwegXM0MBR3ozVgoE+pFq46Ej/InJBs1l+sjSpu6jyi38MZ+xyU/3NQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-linux-x64-gnu": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-linux-x64-gnu/-/cli-linux-x64-gnu-2.12.5.tgz", + "integrity": "sha512-OIun574QsULxaZ5SmPDX4D8bO95RBmYJshu8yGFy0RSyCMKEs/y+o3BThot4fbCv4ttC1Tq6Hzt0QRHXCIgp7w==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-linux-x64-musl": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-linux-x64-musl/-/cli-linux-x64-musl-2.12.5.tgz", + "integrity": "sha512-GDzWcb/6OzJrHnVGQ/n3XndLan67bZ7cu4zJN9+zFmV10nBgKz5jDDm8sRQO4sGb7bu6Ztiy+Al2XY2ptRY0RQ==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-win32-arm64-msvc": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-win32-arm64-msvc/-/cli-win32-arm64-msvc-2.12.5.tgz", + "integrity": "sha512-61KCX5vo0vex7P/P7sbXdfFhHqs+rFaG+UXK/UBwz/VlLveDx6vgpHkF+rfUIfCqj0VvLArpxR3+rQIzRwxKgw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-win32-ia32-msvc": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-win32-ia32-msvc/-/cli-win32-ia32-msvc-2.12.5.tgz", + "integrity": "sha512-mGujjR/MlXwJiQcue4wlXrBYlIEdDCoap0g+GNPlLCb75ilSbWMC8qTsXVuGcLIMumIJvc2v+veDnCHnels/Zg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tauri-apps/cli-win32-x64-msvc": { + "version": "2.12.5", + "resolved": "https://registry.npmjs.org/@tauri-apps/cli-win32-x64-msvc/-/cli-win32-x64-msvc-2.12.5.tgz", + "integrity": "sha512-o1+EOjvi/nFwKx5XamCXSr+GUr2ko8TH7+0wnWuZjE+lZwE+/VWCbE49HPHa3/V59M3bJ0dlK1yhogGQ15OVPg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0 OR MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@tybys/wasm-util": { + "version": "0.10.5", + "resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.5.tgz", + "integrity": "sha512-dOGOPxlaNV+G046mZzN+9C0dAcQZeSKcXiVaM0TYy/LM4ttTmgSBDxYfovcYI8Em3+eAbiAYpvWLOv8+ZVRJKA==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/estree": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.10.tgz", + "integrity": "sha512-lTyOMnOcmRBzdCv6/4zUZCrEhV0xrqQiUZBZtVcbmLHBXKLnN8q89v/wKRF/Q0HbwVsqbIG3o5HDoFl9sduM+g==", + "dev": true, + "license": "MIT" + }, + "node_modules/@typescript/typescript-aix-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz", + "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz", + "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz", + "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz", + "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz", + "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz", + "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz", + "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-loong64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz", + "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-mips64el": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz", + "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz", + "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-riscv64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz", + "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-s390x": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz", + "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz", + "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz", + "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz", + "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz", + "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz", + "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-sunos-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz", + "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz", + "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz", + "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@vitest/coverage-v8": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@vitest/coverage-v8/-/coverage-v8-5.1.0.tgz", + "integrity": "sha512-OY9aShBQG5KOIw2zCo9fDoCHI9RHliNHJ//nM7m6jGg41Hyke9JOGWmeMlK74xYErsxMoK8mIB3gAeEyFVA7LA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@bcoe/v8-coverage": "^1.0.2", + "@vitest/istanbul-lib-coverage": "^2.1.0", + "@vitest/istanbul-lib-report": "^2.1.0", + "ast-v8-to-istanbul": "^1.0.7", + "magicast": "^0.5.5", + "obug": "^3.0.0", + "std-env": "^4.3.0", + "tinyrainbow": "^3.2.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@vitest/browser": "5.1.0", + "vitest": "5.1.0" + }, + "peerDependenciesMeta": { + "@vitest/browser": { + "optional": true + } + } + }, + "node_modules/@vitest/istanbul-lib-coverage": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@vitest/istanbul-lib-coverage/-/istanbul-lib-coverage-2.1.0.tgz", + "integrity": "sha512-WOjuS+xcjSIy7ruYBQ+9BUcV7XZQ0FyVcbWbWxW9TRx/kLGx5SQaeFRIFDJxjNplb6YsT7SpKo2zuXXUo91OIQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=22" + } + }, + "node_modules/@vitest/istanbul-lib-report": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@vitest/istanbul-lib-report/-/istanbul-lib-report-2.1.0.tgz", + "integrity": "sha512-+ZFta25nWtGCOBG9/IET8w1RO5Oawsd6H8Uf6AJ9EA/StFuKVtKAEJEJ6uRt84OH+T4YkI0ASmIHAhRA+0KVxw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/istanbul-lib-coverage": "2.1.0" + }, + "engines": { + "node": ">=22" + } + }, + "node_modules/@vitest/mocker": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-5.1.0.tgz", + "integrity": "sha512-5umB9/jlWcqVS6fsrFmv5Gdnpu6wAUio/lEOwAmvB7v+lFo/IW2B9FhAD0p19GCULTgMzLCdUmuCDCnAzWgppQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.31", + "@vitest/spy": "5.1.0", + "estree-walker": "^3.0.3", + "magic-string": "^1.4.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9 || ^3.0.0", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/spy": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-5.1.0.tgz", + "integrity": "sha512-2DPO5OMHWKrBngC+fCmTQqemNF0gidRhp3lxbBmRhW+M1XJrfgPpqNqWVevXtn2f2ufD5ZaR1B6B641aAlcX5A==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, + "node_modules/ast-v8-to-istanbul": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/ast-v8-to-istanbul/-/ast-v8-to-istanbul-1.0.8.tgz", + "integrity": "sha512-lPdpagk13ANXV+HClJEpCZM7kUsl5XcvgqKzZhaj6Ve1ZFICcY2sGhAN1n1vT0tML6EclT7bfVgTxqL57Gkpow==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "^0.3.31", + "estree-walker": "^3.0.3", + "js-tokens": "^10.0.0" + } + }, + "node_modules/bcp-47": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/bcp-47/-/bcp-47-2.1.2.tgz", + "integrity": "sha512-wsv86UuNFuPnVieKLyECTT+CP1WSAHeoIg5P3xu2kA9Z+7fVPoKt7dxzSeEhtJel0AI+NW3fIW3KtTKQKUCngg==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-alphabetical": "^2.0.0", + "is-alphanumerical": "^2.0.0", + "is-decimal": "^2.0.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, + "node_modules/bcp-47-match": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/bcp-47-match/-/bcp-47-match-2.0.3.tgz", + "integrity": "sha512-JtTezzbAibu8G0R9op9zb3vcWZd9JF6M0xOYGPn0fNCd7wOpRB1mU2mH9T8gaBGbAAyIIVgB2G7xG0GP98zMAQ==", + "dev": true, + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, + "node_modules/chai": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-6.3.0.tgz", + "integrity": "sha512-XWAtwJ6OHO+tj0EKCs0Y2UamnyOxseZWltU4x2U2wh8g4AigdjwvtUjvLP2tqkA/avxHEtzxNaqGq/YGNwckKg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/data-urls": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/data-urls/-/data-urls-8.0.0.tgz", + "integrity": "sha512-JQUCCK+/CAGbGGx618PlFZMXH81Jtj7t73CHv9dd/zVWvxgGT2zLFRjnKb+Ng46GIwcy+f7f88qd4duqllK+WQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "whatwg-mimetype": "^5.0.0", + "whatwg-url": "^17.1.2" + }, + "engines": { + "node": "^22.14.0 || >=24.0.0" + } + }, + "node_modules/decimal.js": { + "version": "10.6.0", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.6.0.tgz", + "integrity": "sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==", + "dev": true, + "license": "MIT" + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/entities": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-9.0.0.tgz", + "integrity": "sha512-oJFNDJg3kTPwAD1cNn2+WmUq4wFoo09npc3Ydf1D7ED4smRiWrsKnaLxykrTWgyGEcusc87P5dxlAleol9kpkg==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/es-module-lexer": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.3.2.tgz", + "integrity": "sha512-poHGpORABojJJucnV9KbOavETW8lBVnphkW77ER5/BQ5Fz7oXSoCNek7IH3vR5nRjdsEz926ibFYX8KtLQmdyw==", + "dev": true, + "license": "MIT" + }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, + "node_modules/expect-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", + "integrity": "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/html-encoding-sniffer": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-7.0.0.tgz", + "integrity": "sha512-UikN5yr7xsCDAq87Or5or0PAlD3HJJOKVzM05az588WnpDJ4Ux7a2A53Qi6gofGg2/EtvF/H4hCi/TXfCW4Y6w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@exodus/bytes": "^1.15.1" + }, + "engines": { + "node": "^22.13.0 || >=24.0.0" + } + }, + "node_modules/is-alphabetical": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is-alphabetical/-/is-alphabetical-2.0.1.tgz", + "integrity": "sha512-FWyyY60MeTNyeSRpkM2Iry0G9hpr7/9kD40mD/cGQEuilcZYS4okz8SN2Q6rLCJ8gbCt6fN+rC+6tMGS99LaxQ==", + "dev": true, + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, + "node_modules/is-alphanumerical": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is-alphanumerical/-/is-alphanumerical-2.0.1.tgz", + "integrity": "sha512-hmbYhX/9MUMF5uh7tOXyK/n0ZvWpad5caBA17GsC6vyuCqaWliRG5K1qS9inmUhEMaOBIW7/whAnSwveW/LtZw==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-alphabetical": "^2.0.0", + "is-decimal": "^2.0.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, + "node_modules/is-decimal": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is-decimal/-/is-decimal-2.0.1.tgz", + "integrity": "sha512-AAB9hiomQs5DXWcRB1rqsxGUstbRroFOPPVAomNk/3XHR5JyEZChOyTWe2oayKnsSsr/kcGqF+z6yuH6HHpN0A==", + "dev": true, + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, + "node_modules/is-potential-custom-element-name": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz", + "integrity": "sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/js-tokens": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-10.0.0.tgz", + "integrity": "sha512-lM/UBzQmfJRo9ABXbPWemivdCW8V2G8FHaHdypQaIy523snUjog0W71ayWXTjiR+ixeMyVHN2XcpnTd/liPg/Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/jsdom": { + "version": "31.0.1", + "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-31.0.1.tgz", + "integrity": "sha512-CbhJL8ZQg3eKswxSZth4QO9tW0jPIhmR0vtSXQSbKSfWbwIT6hoRc17wdHIVTQ43KI0mdV5iTJr3281IDgVZ5g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@exodus/bytes": "^1.16.0", + "bcp-47": "^2.1.2", + "bcp-47-match": "^2.0.3", + "data-urls": "^8.0.0", + "decimal.js": "^10.6.0", + "html-encoding-sniffer": "^7.0.0", + "is-potential-custom-element-name": "^1.0.1", + "lru-cache": "^11.5.3", + "parse5": "^8.1.0", + "parse5-parser-stream": "^8.1.0", + "saxes": "^6.0.0", + "tough-cookie": "^6.0.2", + "undici": "^8.12.3", + "w3c-xmlserializer": "^6.0.0", + "webidl-conversions": "^8.0.1", + "whatwg-mimetype": "^5.0.0", + "whatwg-url": "^17.3.0", + "xml-name-validator": "^5.0.0" + }, + "engines": { + "node": "^22.22.2 || ^24.15.0 || >=26.0.0" + }, + "peerDependencies": { + "@napi-rs/canvas": "^1.0.13" + }, + "peerDependenciesMeta": { + "@napi-rs/canvas": { + "optional": true + } + } + }, + "node_modules/lightningcss": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.33.0.tgz", + "integrity": "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==", + "dev": true, + "license": "MPL-2.0", + "dependencies": { + "detect-libc": "^2.0.3" + }, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "lightningcss-android-arm64": "1.33.0", + "lightningcss-darwin-arm64": "1.33.0", + "lightningcss-darwin-x64": "1.33.0", + "lightningcss-freebsd-x64": "1.33.0", + "lightningcss-linux-arm-gnueabihf": "1.33.0", + "lightningcss-linux-arm64-gnu": "1.33.0", + "lightningcss-linux-arm64-musl": "1.33.0", + "lightningcss-linux-x64-gnu": "1.33.0", + "lightningcss-linux-x64-musl": "1.33.0", + "lightningcss-win32-arm64-msvc": "1.33.0", + "lightningcss-win32-x64-msvc": "1.33.0" + } + }, + "node_modules/lightningcss-android-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.33.0.tgz", + "integrity": "sha512-gEpRTalKdosp4Bb8qWtc2iOgE5SeIHlpS1up9bFq2wAyYhl1UdTObYiHe98zEM9SQvSoqQZ1IQD0JNpg3Ml5pg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.33.0.tgz", + "integrity": "sha512-Sciaz8eenNTKn9b3t7+xr0ipTp9YxKQY4npwQ3mrRuL0BAVHBLyZxofhaKBAVtzmtRZ/zTyo0/to4B1uWG/Djg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.33.0.tgz", + "integrity": "sha512-Z5UPAxzrjlWNNyGy6i65cJzzvgJ5D3T6wMvs+gWpY9d7qRhANrxqAp6LhxIgZhWEw18RfJTGcRxjuLIBr+m8XQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-freebsd-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.33.0.tgz", + "integrity": "sha512-QQM/Ti/hQajJwCY+RiWuCZ9sdtI/XQk7nDK5vC8kkdwixezOlDgvDx7+RT+QjK6FcFT4MpsuoBnHIo/O3StRRg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm-gnueabihf": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.33.0.tgz", + "integrity": "sha512-N7FVBe6iS24MlM6R/4RBTxGhQheZGs7tiQ9U32UtF75NzP5Q7xWPRqLBCKxlRQRk3rY1jCIPLzx7WzOhuUIRLQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.33.0.tgz", + "integrity": "sha512-j2v/itmy4HlNxlc6voKXYgBqNi0Ng2LShg4z7GufpEgs05P+2suBVyi9I6YHq5uoVFx9ETin3eCEhLVyXGQnKg==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.33.0.tgz", + "integrity": "sha512-yiO5ROMuYQgXbC60yjZU5CYSFZGKXL0HFATXt9mHJn1+zW55oCtMI9NfcVhYLMFDL7gV7oBPon/EmMMGg2OvtQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.33.0.tgz", + "integrity": "sha512-ar+Ju7LmcN0Jo4FpL4hpFybwNG9/3A/Br5KW2n2jyODg3MEZXaDYADdemoNS+BDNfMgKvylJLj4S5tyRActuAg==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.33.0.tgz", + "integrity": "sha512-RYiYbkokw0trfKqqzfF55lginwEPrD3OJDfTuJzFs1MK6iFnDenaz1fqLLtX4ITG3OktJQXOeTaw1awrBAlZPw==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-arm64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.33.0.tgz", + "integrity": "sha512-1K+MPfLSFVpphzpdbfkhlWk6wBrTObBzS2T6db10PNOZgR9GoVsAWzwNyuhUYYbTp23j+4RrncfujZ4uAzXvwA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-x64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.33.0.tgz", + "integrity": "sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lru-cache": { + "version": "11.5.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.5.3.tgz", + "integrity": "sha512-U4N8FgzmWxc8k1VH8Kr6lQg18U7Fjvby6wXHVRX/ZZ7IwWbRMgrRbP0Wrb5q5NVinryp4SQampHKdvtecItxUg==", + "dev": true, + "license": "BlueOak-1.0.0", + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/magic-string": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.5.tgz", + "integrity": "sha512-oLLC2+YvQUfAa+vxoufE/yc1icb87M9iXIRqAoR/2EhE/8cb3SF/+Gi0c2By1B+5Vz2e2vQtUvHiu1+63Znh+w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, + "node_modules/magicast": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/magicast/-/magicast-0.5.5.tgz", + "integrity": "sha512-UicdXN8zQ3JHlxVq+28afMXPr1z7WNY6+7EJnzTdQWkTAlMLF5fNCCKxJHBQwGaNGR11581EiQmQzx73+MvszA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/parser": "^7.29.7", + "@babel/types": "^7.29.7", + "source-map-js": "^1.2.1" + } + }, + "node_modules/nanoid": { + "version": "3.3.20", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.20.tgz", + "integrity": "sha512-uKdg2G3GNCKQn9byYOpxbGqrT2fGO5KRt5J/8b3pok8rT6qxGWF6hxMyJiEYtAf+FVyYuD9hRaDqX5uPFYJ4ZQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/obug": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", + "integrity": "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT", + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/parse5": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/parse5/-/parse5-8.1.1.tgz", + "integrity": "sha512-EcR/6WDcU7b2N3vQSxKALCk9lXGtUMQxXio008OEMBE0kavu/7JLfBEeFPzi43nMqVDSbEn3QCK+BMCc9zO//w==", + "dev": true, + "license": "MIT", + "dependencies": { + "entities": "^9.0.0" + }, + "funding": { + "url": "https://github.com/inikulin/parse5?sponsor=1" + } + }, + "node_modules/parse5-parser-stream": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/parse5-parser-stream/-/parse5-parser-stream-8.1.1.tgz", + "integrity": "sha512-hMcVbbUJcQmP8rURWUdYrT1flRiBiBVjq/RYGguJpDUgIXSqdYKlE0CxthmdMWoSlBAuL5bUBa73g0pttSy7Hw==", + "dev": true, + "license": "MIT", + "dependencies": { + "parse5": "^8.1.1" + }, + "funding": { + "url": "https://github.com/inikulin/parse5?sponsor=1" + } + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.7.tgz", + "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/postcss": { + "version": "8.5.33", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.33.tgz", + "integrity": "sha512-RVHEU9/Pp1Fnjr/MpAJsZoADukCKhfIi15OunEYiGktIkYLkPVvC1sioWolGDS7XByMTx9m/IGbro0Js13zhnQ==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.20", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.2" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/rolldown": { + "version": "1.2.19", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.19.tgz", + "integrity": "sha512-QIiV1J9d0pzN4jeeTH4sjPy0AZ6cAy2cG0ExeoE1+U8Rvj+hR3gYkokx0YbeUNGlZqiQJJ2utcETe0oKXwzsuA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@oxc-project/types": "=0.156.0", + "@rolldown/pluginutils": "^1.0.0" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm-eabi": "1.2.19", + "@rolldown/binding-android-arm64": "1.2.19", + "@rolldown/binding-darwin-arm64": "1.2.19", + "@rolldown/binding-darwin-x64": "1.2.19", + "@rolldown/binding-freebsd-x64": "1.2.19", + "@rolldown/binding-linux-arm-gnueabihf": "1.2.19", + "@rolldown/binding-linux-arm64-gnu": "1.2.19", + "@rolldown/binding-linux-arm64-musl": "1.2.19", + "@rolldown/binding-linux-ppc64-gnu": "1.2.19", + "@rolldown/binding-linux-s390x-gnu": "1.2.19", + "@rolldown/binding-linux-x64-gnu": "1.2.19", + "@rolldown/binding-linux-x64-musl": "1.2.19", + "@rolldown/binding-openharmony-arm64": "1.2.19", + "@rolldown/binding-wasm32-wasi": "1.2.19", + "@rolldown/binding-win32-arm64-msvc": "1.2.19", + "@rolldown/binding-win32-x64-msvc": "1.2.19" + } + }, + "node_modules/saxes": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/saxes/-/saxes-6.0.0.tgz", + "integrity": "sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==", + "dev": true, + "license": "ISC", + "dependencies": { + "xmlchars": "^2.2.0" + }, + "engines": { + "node": ">=v12.22.7" + } + }, + "node_modules/source-map-js": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.2.tgz", + "integrity": "sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/std-env": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-4.3.0.tgz", + "integrity": "sha512-OtU/EgQ1kIm5KwqQpBC6ZEMXrZRui11w8zgfTWp8cdO9B8OaPsbA8bTHO2P+HNo1VlUTGMVBwPhydu6poeXiag==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinybench": { + "version": "6.2.3", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-6.2.3.tgz", + "integrity": "sha512-Al01C2JIBnO2CXCAPbpAl6sUiOZHeThGtHYNTa51JRw9PyWOCdr+9Ong05R4qg/Sk3+H645E8ov1h2c58ityNw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/tinyexec": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.3.1.tgz", + "integrity": "sha512-GCvB3aoys96IuDFBMcTB46JOR6mdMtAToqwiW8JlWhsoh1mhHi/xn9ss/Dg7N555GiJyEt2qzoG/NHCwM6h1EA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/tinyglobby": { + "version": "0.2.17", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", + "integrity": "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==", + "dev": true, + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.4" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/tinyrainbow": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.2.0.tgz", + "integrity": "sha512-LgO3D9yZJjApUiuUfl9iFAwrtaX4+lok3wJIqttGoKCHlWUqHqbQpnxCf82L8FjgKsh4iGo78hqJwgL8F6To2A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/tldts": { + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/tldts/-/tldts-7.5.4.tgz", + "integrity": "sha512-Rn2ABgjHZrA+jzqom5zhxA0hLohzYrIgfCPyhpTUxqsUrcsBYuab49LseR9waB9Jr8/MxdodvMHA91oRiGLO3Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "tldts-core": "^7.5.4" + }, + "bin": { + "tldts": "bin/cli.js" + } + }, + "node_modules/tldts-core": { + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/tldts-core/-/tldts-core-7.5.4.tgz", + "integrity": "sha512-9+4zWtKEDZ3V3FoZ1B97rv+5z5oqp7yeXGVze3CxgQ+CJro9do9DCPD00wxstYZDXDMXsurkBerFfiB6GYtPbA==", + "dev": true, + "license": "MIT" + }, + "node_modules/tough-cookie": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-6.1.0.tgz", + "integrity": "sha512-FTxc8u8Kl2tz0Lat1u8ksvcy8bwHRyEMawwTWybo51JqaC6O7SvfuhAKg+Ld6VqyIe2QRJBzY9HA7y2WyFRT2w==", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "tldts": "^7.0.5" + }, + "engines": { + "node": ">=16" + } + }, + "node_modules/tr46": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-7.0.0.tgz", + "integrity": "sha512-BcDSDW+SIGWVOns1Zm6Yg91q9AEj7eaID0QDY9oIqJD1A8dscNwMcl9Sms2DeuWCNov1X0/oXanHGYQBSLwZsg==", + "dev": true, + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": "^22.14.0 || >=24.0.0" + } + }, + "node_modules/tslib": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.9.1.tgz", + "integrity": "sha512-a4Ux9VKuIMHsOS7ENbEq6hK2ZGYIT27Rtl0E4eD9KqwxhKiQeMZE/9xb3IdYwWaSOyIBQLExhQgXDTYoiCxnWA==", + "dev": true, + "license": "0BSD", + "optional": true + }, + "node_modules/typescript": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz", + "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc" + }, + "engines": { + "node": ">=16.20.0" + }, + "optionalDependencies": { + "@typescript/typescript-aix-ppc64": "7.0.2", + "@typescript/typescript-darwin-arm64": "7.0.2", + "@typescript/typescript-darwin-x64": "7.0.2", + "@typescript/typescript-freebsd-arm64": "7.0.2", + "@typescript/typescript-freebsd-x64": "7.0.2", + "@typescript/typescript-linux-arm": "7.0.2", + "@typescript/typescript-linux-arm64": "7.0.2", + "@typescript/typescript-linux-loong64": "7.0.2", + "@typescript/typescript-linux-mips64el": "7.0.2", + "@typescript/typescript-linux-ppc64": "7.0.2", + "@typescript/typescript-linux-riscv64": "7.0.2", + "@typescript/typescript-linux-s390x": "7.0.2", + "@typescript/typescript-linux-x64": "7.0.2", + "@typescript/typescript-netbsd-arm64": "7.0.2", + "@typescript/typescript-netbsd-x64": "7.0.2", + "@typescript/typescript-openbsd-arm64": "7.0.2", + "@typescript/typescript-openbsd-x64": "7.0.2", + "@typescript/typescript-sunos-x64": "7.0.2", + "@typescript/typescript-win32-arm64": "7.0.2", + "@typescript/typescript-win32-x64": "7.0.2" + } + }, + "node_modules/undici": { + "version": "8.12.4", + "resolved": "https://registry.npmjs.org/undici/-/undici-8.12.4.tgz", + "integrity": "sha512-8Mx+uNKKER2bA4AdtedkqRbwUCIJFr2nJckXRe5yrDqzePqwLP7VNcctN8/XdmDSbF7mS+jJKIszlL+vbam1VA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/vite": { + "version": "8.3.8", + "resolved": "https://registry.npmjs.org/vite/-/vite-8.3.8.tgz", + "integrity": "sha512-LzOn3h9rG/0vhLdXrV4yJ4QFA9eEoqCl+Yx+kGJd7aB9w6nfNgVHKSYwLPZUHrz4ZGa+t+kXjV9itvcHDLQouA==", + "dev": true, + "license": "MIT", + "dependencies": { + "lightningcss": "^1.33.0", + "picomatch": "^4.0.7", + "postcss": "^8.5.32", + "rolldown": "~1.2.18", + "tinyglobby": "^0.2.17" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "@vitejs/devtools": "^0.7.1 || ^0.8.0", + "esbuild": "^0.27.0 || ^0.28.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "@vitejs/devtools": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vitest": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-5.1.0.tgz", + "integrity": "sha512-MzmNFnrwa8v8lF1bLxw3UJ4EREwVOVb75VqJZmUDRjJ6EluK/x9th7qTbaeDk41WxE1GxWqmTXcdsfobfH5sOA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/mocker": "5.1.0", + "chai": "^6.3.0", + "es-module-lexer": "^2.3.2", + "expect-type": "^1.4.0", + "magic-string": "^1.4.3", + "obug": "^3.0.0", + "picomatch": "^4.0.7", + "std-env": "^4.3.0", + "tinybench": "^6.2.2", + "tinyexec": "^1.3.1", + "tinyglobby": "^0.2.17", + "why-is-node-running": "3.2.1" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^22.12.0 || ^24.0.0 || >=26.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@opentelemetry/api": "^1.9.0", + "@types/node": "^22.0.0 || >=24.0.0", + "@vitest/browser-playwright": "5.1.0", + "@vitest/browser-preview": "5.1.0", + "@vitest/browser-webdriverio": "^5.0.0-beta.5 || >=5.0.0", + "@vitest/coverage-istanbul": "5.1.0", + "@vitest/coverage-v8": "5.1.0", + "@vitest/ui": "5.1.0", + "happy-dom": "*", + "jsdom": "*", + "vite": "^6.4.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser-playwright": { + "optional": true + }, + "@vitest/browser-preview": { + "optional": true + }, + "@vitest/browser-webdriverio": { + "optional": true + }, + "@vitest/coverage-istanbul": { + "optional": true + }, + "@vitest/coverage-v8": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + }, + "vite": { + "optional": false + } + } + }, + "node_modules/w3c-xmlserializer": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-6.0.0.tgz", + "integrity": "sha512-4Nsy8K5Tr6SPDH9jhKJOHf7ChDrc1zufZTVSF7x72hwuEXBqxqk9G6cK+K2NRUtB3iELRJqjXb4JPDMBjMTl2Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "xml-name-validator": "^5.0.0" + }, + "engines": { + "node": "^22.22.2 || ^24.15.0 || >=26.0.0" + } + }, + "node_modules/webidl-conversions": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-8.0.1.tgz", + "integrity": "sha512-BMhLD/Sw+GbJC21C/UgyaZX41nPt8bUTg+jWyDeg7e7YN4xOM05YPSIXceACnXVtqyEw/LMClUQMtMZ+PGGpqQ==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=20" + } + }, + "node_modules/whatwg-mimetype": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-5.0.0.tgz", + "integrity": "sha512-sXcNcHOC51uPGF0P/D4NVtrkjSU2fNsm9iog4ZvZJsL3rjoDAzXZhkm2MWt1y+PUdggKAYVoMAIYcs78wJ51Cw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20" + } + }, + "node_modules/whatwg-url": { + "version": "17.3.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-17.3.0.tgz", + "integrity": "sha512-KwOAJrpf+GTgBvcBQJcP38sMF0DsQKdwtG+TxM2pW44P6E5k0hDbWrDmyuJDIF0Uef0aQq0ZoJs+5oI+k8ddhg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@exodus/bytes": "^1.16.0", + "tr46": "^7.0.0", + "webidl-conversions": "^8.0.1" + }, + "engines": { + "node": "^22.14.0 || >=24.0.0" + } + }, + "node_modules/why-is-node-running": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-3.2.1.tgz", + "integrity": "sha512-Tb2FUhB4vUsGQlfSquQLYkApkuPAFQXGFzxWKHHumVz2dK+X1RUm/HnID4+TfIGYJ1kTcwOaCk/buYCEJr6YjQ==", + "dev": true, + "license": "MIT", + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=20.11" + } + }, + "node_modules/xml-name-validator": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-5.0.0.tgz", + "integrity": "sha512-EvGK8EJ3DhaHfbRlETOWAS5pO9MZITeauHKJyb8wyajUfQUenkIg2MvLDTZ4T/TgIcm3HU0TFBgWWboAZ30UHg==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=18" + } + }, + "node_modules/xmlchars": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", + "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==", + "dev": true, + "license": "MIT" + } + } +} diff --git a/crates/omgcode-desktop/ui/package.json b/crates/omgcode-desktop/ui/package.json new file mode 100644 index 0000000..52f55b8 --- /dev/null +++ b/crates/omgcode-desktop/ui/package.json @@ -0,0 +1,31 @@ +{ + "name": "omgcode-desktop-ui", + "version": "0.0.0", + "private": true, + "type": "module", + "license": "Apache-2.0", + "description": "Webview frontend for the OMGCode desktop app. It renders untrusted content as text only.", + "scripts": { + "dev": "vite", + "build": "tsc --noEmit && vite build", + "check": "tsc --noEmit", + "lint": "biome check .", + "format": "biome format --write .", + "test": "vitest run", + "coverage": "vitest run --coverage", + "test:dist": "npm run build && vitest run tests/dist.test.ts", + "tauri": "cd .. && tauri" + }, + "dependencies": { + "@tauri-apps/api": "2.12.5" + }, + "devDependencies": { + "@biomejs/biome": "2.6.8", + "@tauri-apps/cli": "2.12.5", + "@vitest/coverage-v8": "5.1.0", + "jsdom": "31.0.1", + "typescript": "7.0.2", + "vite": "8.3.8", + "vitest": "5.1.0" + } +} diff --git a/crates/omgcode-desktop/ui/src/approvals.ts b/crates/omgcode-desktop/ui/src/approvals.ts new file mode 100644 index 0000000..d190749 --- /dev/null +++ b/crates/omgcode-desktop/ui/src/approvals.ts @@ -0,0 +1,12 @@ +import { startApprovals } from "./approvals/app"; +import { el, replaceChildren } from "./dom"; +import { hostAvailable, NO_HOST_MESSAGE } from "./ipc/client"; + +const root = document.getElementById("app"); +if (root !== null) { + if (hostAvailable()) { + startApprovals(root); + } else { + replaceChildren(root, el("p", { class: "no-host", text: NO_HOST_MESSAGE })); + } +} diff --git a/crates/omgcode-desktop/ui/src/approvals/app.ts b/crates/omgcode-desktop/ui/src/approvals/app.ts new file mode 100644 index 0000000..eb2d8a9 --- /dev/null +++ b/crates/omgcode-desktop/ui/src/approvals/app.ts @@ -0,0 +1,263 @@ +import { el, replaceChildren } from "../dom"; +import { formatCountdown, formatUnix } from "../format"; +import { approvalsApi } from "../ipc/approvals_client"; +import { toIpcError } from "../ipc/errors"; +import type { ApprovalList, ApprovalView } from "../ipc/types"; +import { createPoller } from "../poll"; +import { operationLabel, orderApprovals, remainingSeconds } from "./model"; +import { createReviewPanel } from "./review"; + +export const LIST_POLL_MS = 3000; +const COUNTDOWN_MS = 1000; + +export interface ApprovalsApp { + dispose(): void; +} + +interface RowRef { + li: HTMLElement; + /** Everything the row displays; a row is rebuilt only when this changes. */ + signature: string; + /** Pending rows only. */ + expires: number | null; + countdown: HTMLElement | null; + button: HTMLButtonElement | null; +} + +function signatureOf(approval: ApprovalView): string { + return JSON.stringify([ + approval.state, + approval.summary, + approval.expires_unix, + approval.created_unix, + approval.session_title, + approval.folder_display, + approval.operation, + approval.writers_stopped, + ]); +} + +/** Makes `parent` hold exactly `wanted`, in order, moving only nodes that are out of place. */ +function reconcile(parent: HTMLElement, wanted: readonly HTMLElement[]): void { + let cursor = parent.firstChild; + for (const node of wanted) { + if (node === cursor) { + cursor = cursor.nextSibling; + } else { + parent.insertBefore(node, cursor); + } + } + while (cursor !== null) { + const next = cursor.nextSibling; + parent.removeChild(cursor); + cursor = next; + } +} + +/** + * Builds the approvals window. It never renders transcript text: it shows only the + * fields the core returns for each approval. + */ +export function startApprovals(root: HTMLElement): ApprovalsApp { + let received: ApprovalList = { now_unix: 0, approvals: [] }; + let receivedAtMs = performance.now(); + let selectedId: string | null = null; + let rows = new Map(); + let focusedOnce = false; + let decidedId: string | null = null; + + const emptyNote = el("p", { class: "empty", text: "No approvals are pending." }); + const pendingList = el("ul", { class: "approvals-ul" }); + const decidedHeading = el("h2", { text: "Recently decided" }); + const decidedList = el("ul", { class: "approvals-ul" }); + const listBox = el( + "div", + { class: "approval-list", attrs: { tabindex: "-1" } }, + emptyNote, + pendingList, + decidedHeading, + decidedList, + ); + const errorLine = el("p", { class: "error", attrs: { role: "status" } }); + errorLine.hidden = true; + const review = createReviewPanel({ + reload: (id, notice) => loadDetail(id, notice), + onDecided: (view) => { + decidedId = view.id; + void poller.tick(); + }, + }); + + replaceChildren( + root, + el( + "main", + { class: "approvals" }, + el("h1", { text: "Tool approvals" }), + el("p", { + class: "intro", + text: "Approving lets the model's next identical request run once. Nothing runs when you approve.", + }), + errorLine, + listBox, + review.element, + ), + ); + + function elapsedMs(): number { + return performance.now() - receivedAtMs; + } + + function updateCountdowns(): void { + for (const row of rows.values()) { + if (row.expires === null || row.countdown === null) { + continue; + } + const left = remainingSeconds(row.expires, received.now_unix, elapsedMs()); + const label = left === 0 ? "expired" : `expires in ${formatCountdown(left)}`; + if (row.countdown.textContent !== label) { + replaceChildren(row.countdown, label); + } + row.li.classList.toggle("expired", left === 0); + } + } + + function syncSelection(): void { + for (const [id, row] of rows) { + row.button?.setAttribute("aria-pressed", String(id === selectedId)); + } + } + + function approvalRow(approval: ApprovalView, pending: boolean): RowRef { + const signature = signatureOf(approval); + const who = [approval.session_title, approval.folder_display] + .filter((part): part is string => part !== null) + .join(" · "); + const body = [ + el("span", { class: "op", text: operationLabel(approval.operation) }), + el("span", { class: "who clamp-1", text: who === "" ? "Unknown session" : who }), + el("span", { class: "summary clamp-3", text: approval.summary }), + el("span", { class: "when", text: `Created ${formatUnix(approval.created_unix)}` }), + ]; + if (!pending) { + const li = el( + "li", + { class: "approval decided" }, + ...body, + el("span", { class: "state", text: approval.state }), + ); + return { li, signature, expires: null, countdown: null, button: null }; + } + const countdown = el("span", { class: "countdown" }); + const select = el("button", { class: "approval-button" }, ...body, countdown); + select.type = "button"; + select.setAttribute("aria-pressed", String(approval.id === selectedId)); + select.dataset.approvalId = approval.id; + select.addEventListener("click", () => { + selectedId = approval.id; + void loadDetail(approval.id); + syncSelection(); + }); + return { + li: el("li", { class: "approval pending" }, select), + signature, + expires: approval.expires_unix, + countdown, + button: select, + }; + } + + function draw(): void { + const activeId = + document.activeElement instanceof HTMLElement + ? document.activeElement.dataset.approvalId + : undefined; + const { pending, decided } = orderApprovals(received.approvals); + const next = new Map(); + const rowsFor = (list: ApprovalView[], isPending: boolean): HTMLElement[] => + list.map((approval) => { + const old = rows.get(approval.id); + const row = + old !== undefined && old.signature === signatureOf(approval) + ? old + : approvalRow(approval, isPending); + next.set(approval.id, row); + return row.li; + }); + const pendingNodes = rowsFor(pending, true); + const decidedNodes = rowsFor(decided, false); + rows = next; + reconcile(pendingList, pendingNodes); + reconcile(decidedList, decidedNodes); + emptyNote.hidden = pending.length > 0; + pendingList.hidden = pending.length === 0; + decidedHeading.hidden = decided.length === 0; + decidedList.hidden = decided.length === 0; + updateCountdowns(); + if (activeId !== undefined) { + const focused = rows.get(activeId)?.button; + if (focused !== null && focused !== undefined && document.activeElement !== focused) { + focused.focus(); + } + } + if (!focusedOnce) { + focusedOnce = true; + (listBox.querySelector(".approval-button") ?? listBox).focus(); + } + } + + async function loadDetail(id: string, notice?: string): Promise { + try { + const detail = await approvalsApi.detail(id); + if (selectedId === id) { + review.show(detail, notice); + } + } catch (reason) { + if (selectedId === id) { + const error = toIpcError(reason); + review.clear(); + showError(`${error.message} (${error.code})`); + } + } + } + + function showError(message: string | null): void { + replaceChildren(errorLine, message ?? ""); + errorLine.hidden = message === null; + } + + async function refresh(): Promise { + try { + received = await approvalsApi.list(); + receivedAtMs = performance.now(); + showError(null); + } catch (reason) { + const error = toIpcError(reason); + showError(`${error.message} (${error.code})`); + return; + } + draw(); + const selected = received.approvals.find((a) => a.id === selectedId); + if ( + selectedId !== null && + (selected === undefined || selected.state !== "pending") && + selectedId !== decidedId + ) { + selectedId = null; + review.clear(); + } + } + + const poller = createPoller(refresh, LIST_POLL_MS); + const countdownTimer = setInterval(updateCountdowns, COUNTDOWN_MS); + void poller.tick(); + poller.start(); + + return { + dispose() { + poller.stop(); + clearInterval(countdownTimer); + review.dispose(); + }, + }; +} diff --git a/crates/omgcode-desktop/ui/src/approvals/model.ts b/crates/omgcode-desktop/ui/src/approvals/model.ts new file mode 100644 index 0000000..f0b6e64 --- /dev/null +++ b/crates/omgcode-desktop/ui/src/approvals/model.ts @@ -0,0 +1,107 @@ +import type { ApprovalDetail, ApprovalOperation, ApprovalView } from "../ipc/types"; + +export const REVIEW_DELAY_MS = 1000; +export const WRITERS_STOPPED_TEXT = + "I have stopped every other program that could write to this file. OMGCode cannot detect a concurrent writer."; + +const OPERATION_LABELS: Record = { + command: "Command", + file_write: "File edit", + git_push: "Git push", + network: "Network", +}; + +export function operationLabel(operation: ApprovalOperation): string { + return OPERATION_LABELS[operation]; +} + +/** Pending approvals first (soonest expiry first), then decided ones (newest first). */ +export function orderApprovals(approvals: readonly ApprovalView[]): { + pending: ApprovalView[]; + decided: ApprovalView[]; +} { + const pending = approvals + .filter((a) => a.state === "pending") + .sort((a, b) => a.expires_unix - b.expires_unix); + const decided = approvals + .filter((a) => a.state !== "pending") + .sort((a, b) => b.created_unix - a.created_unix); + return { pending, decided }; +} + +/** + * Seconds left before `expiresUnix`. The base is the core's clock (`listNowUnix`), advanced + * by `elapsedMs` of monotonic time since the list arrived. The local wall clock is not used. + */ +export function remainingSeconds( + expiresUnix: number, + listNowUnix: number, + elapsedMs: number, +): number { + return Math.max(0, expiresUnix - (listNowUnix + Math.floor(elapsedMs / 1000))); +} + +export interface ReviewInput { + /** The "writers stopped" checkbox. */ + writersStopped: boolean; + /** True once the review delay has passed. */ + armed: boolean; + /** True while a decision is being sent. */ + busy: boolean; +} + +export interface Gate { + enabled: boolean; + /** Why Approve is disabled, shown as text. */ + reason: string | null; +} + +/** Decides whether Approve may be used for the shown detail. */ +export function approveGate(detail: ApprovalDetail, input: ReviewInput): Gate { + if (detail.approval.state !== "pending") { + return { enabled: false, reason: "This approval is no longer pending." }; + } + if (detail.binding !== "exact") { + return { enabled: false, reason: "The recorded request cannot be approved." }; + } + if (detail.review_token === null) { + return { enabled: false, reason: "No review token was issued. Reload the review." }; + } + if (detail.approval.operation === "file_write" && !input.writersStopped) { + return { enabled: false, reason: "Confirm that other writers are stopped." }; + } + if (!input.armed) { + return { enabled: false, reason: "Approve unlocks after a short review delay." }; + } + if (input.busy) { + return { enabled: false, reason: null }; + } + return { enabled: true, reason: null }; +} + +export interface DecisionArgs { + decision: "approve" | "deny"; + writersStopped: boolean; + reviewToken: string | null; +} + +/** Arguments for `approval_decide`. Deny never carries a token or an acknowledgement. */ +export function decisionArgs( + decision: "approve" | "deny", + detail: ApprovalDetail, + input: ReviewInput, +): DecisionArgs { + if (decision === "deny") { + return { decision, writersStopped: false, reviewToken: null }; + } + return { + decision, + writersStopped: detail.approval.operation === "file_write" && input.writersStopped, + reviewToken: detail.review_token, + }; +} + +export const REVIEW_AGAIN_CODES: ReadonlySet = new Set([ + "review_expired", + "review_mismatch", +]); diff --git a/crates/omgcode-desktop/ui/src/approvals/review.ts b/crates/omgcode-desktop/ui/src/approvals/review.ts new file mode 100644 index 0000000..f4fa188 --- /dev/null +++ b/crates/omgcode-desktop/ui/src/approvals/review.ts @@ -0,0 +1,177 @@ +import { append, el, replaceChildren } from "../dom"; +import { approvalsApi } from "../ipc/approvals_client"; +import { toIpcError } from "../ipc/errors"; +import type { ApprovalDetail, ApprovalView } from "../ipc/types"; +import { + approveGate, + decisionArgs, + REVIEW_AGAIN_CODES, + REVIEW_DELAY_MS, + type ReviewInput, + WRITERS_STOPPED_TEXT, +} from "./model"; + +export interface ReviewPanel { + element: HTMLElement; + /** Shows the detail and restarts the approve delay. `notice` is shown as an error line. */ + show(detail: ApprovalDetail, notice?: string): void; + clear(): void; + dispose(): void; +} + +export interface ReviewHandlers { + /** Reloads the detail for an id and shows `notice` with it. */ + reload(id: string, notice: string): Promise; + /** Called after a decision so the list can refresh. */ + onDecided(view: ApprovalView): void; +} + +function explanation(detail: ApprovalDetail): string | null { + if (detail.binding === "stale") { + return detail.approval.operation === "file_write" + ? "The request no longer matches what was recorded: the file changed since the request. Approving would have no effect." + : "The request no longer matches what was recorded. Approving would have no effect."; + } + if (detail.binding === "not_found") { + return "The recorded request could not be found. Approving would have no effect."; + } + return null; +} + +/** Builds the review panel. It renders approval fields as text and gates Approve. */ +export function createReviewPanel(handlers: ReviewHandlers): ReviewPanel { + const element = el("section", { class: "review", attrs: { "aria-label": "Review" } }); + element.hidden = true; + let timer: ReturnType | null = null; + + function cancelTimer(): void { + if (timer !== null) { + clearTimeout(timer); + timer = null; + } + } + + function show(detail: ApprovalDetail, notice?: string): void { + cancelTimer(); + const input: ReviewInput = { writersStopped: false, armed: false, busy: false }; + const isFile = detail.approval.operation === "file_write"; + const pending = detail.approval.state === "pending"; + const exact = detail.binding === "exact"; + + const result = el("p", { class: "result", attrs: { role: "status" } }); + if (notice !== undefined) { + replaceChildren(result, notice); + result.classList.add("error"); + } + const hint = el("p", { class: "muted" }); + const approve = el("button", { class: "primary", text: "Approve" }); + approve.type = "button"; + const deny = el("button", { class: "danger", text: "Deny" }); + deny.type = "button"; + const checkbox = el("input", { attrs: { type: "checkbox", id: "writers-stopped" } }); + const checkLabel = el( + "label", + { class: "field-check", attrs: { for: "writers-stopped" } }, + checkbox, + el("span", { text: WRITERS_STOPPED_TEXT }), + ); + + function sync(): void { + const gate = approveGate(detail, input); + approve.disabled = !gate.enabled; + deny.disabled = input.busy; + replaceChildren(hint, gate.reason ?? ""); + hint.hidden = gate.reason === null || !exact; + } + + async function decide(decision: "approve" | "deny"): Promise { + const args = decisionArgs(decision, detail, input); + input.busy = true; + sync(); + try { + const view = await approvalsApi.decide( + detail.approval.id, + args.decision, + args.writersStopped, + args.reviewToken, + ); + cancelTimer(); + replaceChildren(result, `Decision recorded: ${view.state}.`); + result.classList.remove("error"); + approve.disabled = true; + deny.disabled = true; + checkbox.disabled = true; + handlers.onDecided(view); + } catch (reason) { + const error = toIpcError(reason); + input.busy = false; + if (REVIEW_AGAIN_CODES.has(error.code)) { + await handlers.reload( + detail.approval.id, + "The review expired or the request changed. Review it again.", + ); + return; + } + replaceChildren(result, `${error.message} (${error.code})`); + result.classList.add("error"); + sync(); + } + } + + approve.addEventListener("click", () => void decide("approve")); + deny.addEventListener("click", () => void decide("deny")); + checkbox.addEventListener("change", () => { + input.writersStopped = checkbox.checked; + sync(); + }); + + const fields = el("dl", { class: "fields" }); + for (const field of detail.fields) { + append( + fields, + el("dt", { text: field.label }), + el("dd", {}, field.multiline ? el("pre", { text: field.value }) : field.value), + ); + } + const explain = explanation(detail); + replaceChildren( + element, + el("h2", { text: "Review" }), + explain === null ? null : el("p", { class: "banner", text: explain }), + exact && detail.tool !== null + ? el("p", { class: "tool", text: `Tool: ${detail.tool}` }) + : null, + exact ? fields : null, + exact + ? el( + "p", + { class: "hash" }, + "Request SHA-256: ", + el("code", { text: detail.approval.request_sha256 }), + ) + : null, + pending && isFile && exact ? checkLabel : null, + el("div", { class: "decision" }, ...(pending ? [deny, exact ? approve : null] : [])), + hint, + result, + ); + element.hidden = false; + sync(); + timer = setTimeout(() => { + timer = null; + input.armed = true; + sync(); + }, REVIEW_DELAY_MS); + } + + return { + element, + show, + clear() { + cancelTimer(); + element.hidden = true; + element.replaceChildren(); + }, + dispose: cancelTimer, + }; +} diff --git a/crates/omgcode-desktop/ui/src/dom.ts b/crates/omgcode-desktop/ui/src/dom.ts new file mode 100644 index 0000000..ce5a591 --- /dev/null +++ b/crates/omgcode-desktop/ui/src/dom.ts @@ -0,0 +1,84 @@ +/** + * Minimal typed DOM builder. Text always goes through text nodes, so untrusted strings + * can never become markup. Attributes that can execute or navigate are refused. + */ + +export type Child = Node | string | null | undefined | false; + +export interface ElOptions { + class?: string; + text?: string; + attrs?: Readonly>; +} + +const BIDI_CONTROLS = /[\u202a-\u202e\u2066-\u2069]/g; + +/** Makes bidirectional override characters visible so they cannot reorder displayed text. */ +export function neutralise(value: string): string { + return value.replace(BIDI_CONTROLS, (c) => { + const hex = c.charCodeAt(0).toString(16).toUpperCase().padStart(4, "0"); + return `[U+${hex}]`; + }); +} + +function attributeAllowed(name: string): boolean { + const lower = name.toLowerCase(); + const refused = lower === "style" || lower === "href" || lower === "src" || lower === "srcdoc"; + return !(refused || lower.startsWith("on")); +} + +/** Creates a text node for untrusted text. */ +export function text(value: string): Text { + return document.createTextNode(neutralise(value)); +} + +/** Appends children; strings become text nodes. */ +export function append(parent: Node, ...children: Child[]): void { + for (const child of children) { + if (child === null || child === undefined || child === false) { + continue; + } + parent.appendChild(typeof child === "string" ? text(child) : child); + } +} + +/** Replaces all children of `parent`. */ +export function replaceChildren(parent: Element, ...children: Child[]): void { + parent.replaceChildren(); + append(parent, ...children); +} + +/** Creates an element. Strings in `children` become text nodes. */ +export function el( + tag: K, + options: ElOptions = {}, + ...children: Child[] +): HTMLElementTagNameMap[K] { + const node = document.createElement(tag); + if (options.class !== undefined) { + node.className = options.class; + } + if (options.text !== undefined) { + node.textContent = neutralise(options.text); + } + for (const [name, value] of Object.entries(options.attrs ?? {})) { + if (!attributeAllowed(name)) { + throw new Error(`Attribute ${name} is not allowed.`); + } + node.setAttribute(name, value); + } + append(node, ...children); + return node; +} + +/** Creates a `