Compare commits

..

91 commits
5.0.4 ... main

Author SHA1 Message Date
murashit
8dc4d82c26 chore(release): 5.2.1 2026-07-22 14:42:14 +09:00
murashit
811a4b7158 fix(threads): publish fork activity with list replacement 2026-07-22 13:23:18 +09:00
murashit
05ea87643a refactor(threads): coordinate fork publication atomically 2026-07-22 12:31:16 +09:00
murashit
bb673b2a75 refactor(chat): replace rollback RPC with marker fork 2026-07-22 11:10:22 +09:00
murashit
01a20dcf70 feat(chat): honor app-server direct input capability 2026-07-22 09:47:18 +09:00
murashit
73be2beea7 chore(app-server): update bindings for Codex CLI 0.145.0 2026-07-22 09:09:31 +09:00
murashit
14adac592d chore(deps): apply npm audit dependency updates 2026-07-21 21:07:10 +09:00
murashit
f4616cf945 refactor: consolidate thin module ownership 2026-07-21 20:59:09 +09:00
murashit
67289765c6 refactor(selection-rewrite): consolidate transport output 2026-07-21 20:59:09 +09:00
murashit
ab0a9047f4 refactor(diff): replace custom LCS with jsdiff 2026-07-21 20:58:23 +09:00
murashit
488344a74d refactor(markdown): derive code ranges from mdast 2026-07-21 20:57:59 +09:00
murashit
6bdd9e5972 refactor(archive): consolidate export ownership 2026-07-21 20:57:49 +09:00
murashit
5b6fce1a4d fix(archive): normalize markdown links with mdast 2026-07-21 19:40:52 +09:00
murashit
05f9740e8e chore(release): 5.2.0 2026-07-21 17:05:36 +09:00
murashit
6b2a20cf0d perf(chat): resume before shared resource hydration 2026-07-21 16:58:21 +09:00
murashit
d0965d19b1 feat(context): replace metadata envelopes with thread links 2026-07-21 16:39:27 +09:00
murashit
381e2a5467 chore(release): 5.1.2 2026-07-21 14:40:40 +09:00
murashit
03305a47ef fix(context): hide normalized resume manifests 2026-07-21 14:35:33 +09:00
murashit
d74747fbf0 fix(threads): serialize rename saves 2026-07-21 14:33:04 +09:00
murashit
f707da0274 fix(threads): keep errors out of navigation rows 2026-07-21 14:25:44 +09:00
murashit
2fb7d29841 fix(threads): disable unavailable auto-name actions 2026-07-21 14:25:44 +09:00
murashit
8a798a5bca fix(ui): disable operations with unmet preconditions 2026-07-21 14:25:44 +09:00
murashit
689368fe8d docs(design): prefer coherent UI over explanatory copy 2026-07-21 13:19:03 +09:00
murashit
5cd106973c refactor(threads): make auto-name exclusive and cancellable 2026-07-21 10:43:01 +09:00
murashit
f3d987ee03 refactor(settings): serialize dynamic refreshes 2026-07-21 10:26:57 +09:00
murashit
bbe311c4de refactor(chat): flatten session runtime indirection 2026-07-21 10:09:47 +09:00
murashit
4f3ebcaa31 docs: remove defensive reference caveats 2026-07-21 08:29:05 +09:00
murashit
96b88c1715 test: remove obsolete specification guards 2026-07-21 07:11:27 +09:00
murashit
193b680f06 refactor(runtime): remove obsolete lifetime and query abstractions 2026-07-20 23:47:49 +09:00
murashit
2d9f98f434 refactor(query): consolidate runtime-owned resources 2026-07-20 23:35:21 +09:00
murashit
ffcc814ba5 refactor(runtime): simplify disposable runtime boundaries 2026-07-20 23:33:43 +09:00
murashit
1f5aa50862 chore(release): 5.1.1 2026-07-20 21:31:36 +09:00
murashit
a2c896d0ae docs(review): require proportional evidence for findings 2026-07-20 19:32:45 +09:00
murashit
205893cc1f fix(thread-picker): keep one active picker per runtime 2026-07-20 19:07:33 +09:00
murashit
b00f6292c4 refactor: remove test-only source exports 2026-07-20 18:22:21 +09:00
murashit
8d952fcfbf chore: ignore local pnpm store 2026-07-20 18:00:02 +09:00
murashit
54dcbd8759 refactor(query): require complete runtime-owned context 2026-07-20 18:00:02 +09:00
murashit
381646ebfb refactor(settings): bind dynamic data to one runtime lease 2026-07-20 18:00:02 +09:00
murashit
aaa04507e0 refactor(chat): make reconnect reset one scoped transition 2026-07-20 17:58:03 +09:00
murashit
a92f3b031d refactor(chat): construct thread lifecycle without late binding 2026-07-20 17:52:33 +09:00
murashit
567c744624 refactor(protocol): isolate released legacy reference decoding
Decode released /refer envelopes and file references only in a dedicated protocol adapter.
Keep the compatibility layer removable without changing the current message path.
2026-07-20 17:27:57 +09:00
murashit
11c233b8fe refactor(runtime): replace executable-bound runtime atomically
Own executable- and vault-bound resources in one disposable runtime.
On executable changes, detach stable view shells, dispose all bound work,
and publish a fully attached replacement runtime.
2026-07-20 17:27:57 +09:00
murashit
aadcf179bc refactor(runtime): make vault the fixed workspace root
Remove protocol thread cwd from panel state and operations.
Require the immutable vault root wherever path presentation needs a workspace base.
2026-07-20 17:27:56 +09:00
murashit
52c350093a fix(query): align resource refresh semantics 2026-07-20 09:56:39 +09:00
murashit
a35c9f4263 feat(threads): share paginated thread history and search 2026-07-20 09:09:41 +09:00
murashit
dc467695f4 refactor(chat): replace thread stream presenter facade 2026-07-19 06:31:34 +09:00
murashit
1ef344b02a test(chat): split view lifecycle coverage for parallel execution 2026-07-19 06:22:46 +09:00
murashit
f93570848f refactor(chat): derive Threads activity from panel state 2026-07-19 06:17:10 +09:00
murashit
cf0548a0d0 refactor(app-server): make TanStack Query authoritative 2026-07-19 05:40:36 +09:00
murashit
ea665af455 chore(lint): re-enable Obsidian setting definition rule 2026-07-18 20:14:21 +09:00
murashit
7ec4acf2b1 test(chat): cover auto-review audit history 2026-07-18 19:33:10 +09:00
murashit
44128caf2c test(chat): protect stale reducer transitions 2026-07-18 19:33:10 +09:00
murashit
e7efd773b3 test(protocol): protect app-server boundary contracts 2026-07-18 19:28:21 +09:00
murashit
9c71b0eb88 test: expand mutation coverage at protocol boundaries 2026-07-18 19:11:18 +09:00
murashit
d3b426d500 test(runtime): cover approval policy commit state 2026-07-18 19:05:58 +09:00
murashit
d114c3e473 test(chat): protect context manifest placement 2026-07-18 19:05:58 +09:00
murashit
eecf73dc77 fix(chat): preserve accepted MCP elicitation URLs 2026-07-18 19:05:58 +09:00
murashit
358cdd7b7c test(chat): harden context serialization boundaries 2026-07-18 18:18:36 +09:00
murashit
5cd898fabe test(chat): harden stream behavior contracts 2026-07-18 18:18:28 +09:00
murashit
8351156589 test: harden vault and runtime boundaries 2026-07-18 18:18:24 +09:00
murashit
ef5d1d803d feat(chat): show live subagent activity summaries 2026-07-18 18:15:41 +09:00
murashit
760421613d test: add exploratory domain mutation testing 2026-07-18 17:43:34 +09:00
murashit
46ecf3addd test(chat): cover pending request UI boundaries 2026-07-18 14:50:45 +09:00
murashit
93c4ba50a1 test(settings): cover declarative API parity 2026-07-18 14:50:42 +09:00
murashit
d568f9abb1 test(app-server): cover RPC boundary failures 2026-07-18 14:48:22 +09:00
murashit
d1337a6c85 test(ui): cover turn diff and toolbar boundaries 2026-07-18 14:35:05 +09:00
murashit
eb6d4a3c2d test(app-server): cover client and transport lifecycles 2026-07-18 14:35:00 +09:00
murashit
4820fd5821 test(chat): cover pending requests and agent states 2026-07-18 14:34:14 +09:00
murashit
f588719440 test: add source coverage reporting 2026-07-18 14:17:42 +09:00
murashit
6524409313 chore(release): 5.1.0 2026-07-18 12:00:49 +09:00
murashit
d8f4f8a899 refactor(input): distinguish Vault file references from mentions 2026-07-18 11:40:43 +09:00
murashit
9b2585b2bd refactor(context): model Obsidian excerpts as references 2026-07-18 11:03:48 +09:00
murashit
4581ee4c5f feat(context): bound and persist explicit turn context 2026-07-18 10:47:10 +09:00
murashit
56a7662b2e docs: recast README around user workflow 2026-07-18 09:16:06 +09:00
murashit
93d0021391 docs: correct app-server compatibility key 2026-07-18 09:01:09 +09:00
murashit
dfeeb0e4b2 refactor(threads): bound lifecycle event arbitration 2026-07-18 08:23:49 +09:00
murashit
ffb11490ce fix(composer): make attachment saves explicit editing state 2026-07-18 08:23:49 +09:00
murashit
51994c6777 refactor(metadata): publish app-server resources independently 2026-07-18 08:19:02 +09:00
murashit
5e2007c899 refactor(chat): remove redundant auto-title tracking 2026-07-18 07:50:36 +09:00
murashit
4ddb17a27b refactor(chat): narrow lifecycle coordination guarantees 2026-07-18 07:27:01 +09:00
murashit
48bd261f64 refactor(settings): centralize dynamic data ownership 2026-07-18 07:26:21 +09:00
murashit
58f8805328 docs(design): keep architectural guidance conceptual 2026-07-18 07:25:19 +09:00
murashit
6800bcc339 refactor(runtime): simplify thread settings serialization 2026-07-18 07:23:29 +09:00
murashit
562513099f refactor(async): unify scoped operation coordination 2026-07-18 07:23:29 +09:00
murashit
e056fee378 fix(settings): preserve ordered mutation facts 2026-07-18 07:23:29 +09:00
murashit
2de242819e fix(composer): preserve attachment insertion targets 2026-07-18 07:23:29 +09:00
murashit
9ad8ade044 fix(chat): serialize runtime settings commits 2026-07-18 07:23:29 +09:00
murashit
1eb074b361 refactor(chat): model scoped async operations 2026-07-18 07:23:29 +09:00
murashit
716d6a2db5 fix(chat): unsubscribe when leaving running subagents 2026-07-18 07:23:29 +09:00
murashit
4b0218169c fix(chat): centralize active panel operation policy 2026-07-17 20:28:13 +09:00
murashit
ec3bcd0312 feat(settings): support declarative settings search 2026-07-17 11:27:53 +09:00
411 changed files with 24294 additions and 9343 deletions

6
.github/release-notes/5.1.0.md vendored Normal file
View file

@ -0,0 +1,6 @@
## Changes
- Added search to Codex Panel settings.
- Large web, thread, selection, and vault references are now bounded predictably, report truncation when needed, and keep their context details after reopening a thread.
- Paste and drop attachments now preserve their intended insertion point while files are being saved, with clearer composer editing state.
- Improved reliability when switching threads, leaving running subagents, or changing runtime and app-server settings, preventing stale results and lost updates.

6
.github/release-notes/5.1.1.md vendored Normal file
View file

@ -0,0 +1,6 @@
## Changes
- Thread history now stays in sync across the chat toolbar, thread picker, and Threads view while loading older threads or refreshing.
- Thread search now uses fuzzy title matching across the complete active history instead of matching thread IDs.
- Running subagent summaries now show each agent's latest activity instead of only its initial status.
- Opening the thread picker again now closes the previous picker, and accepted MCP links are preserved in conversation history.

6
.github/release-notes/5.1.2.md vendored Normal file
View file

@ -0,0 +1,6 @@
## Changes
- Thread rename and auto-name controls now prevent conflicting edits while work is in progress, and cancelling auto-name returns to the existing draft.
- Actions with known unmet prerequisites are now disabled before execution, including unavailable auto-name and selection rewrite actions.
- Thread operation errors no longer appear as entries in navigation lists.
- Resumed conversations now hide Codex Panel's internal context metadata while preserving their context attachments.

4
.github/release-notes/5.2.0.md vendored Normal file
View file

@ -0,0 +1,4 @@
## Changes
- Thread references created with `/refer` now remain as readable, clickable links in conversation history and open the referenced thread in an available Codex Panel.
- Opening an existing thread in a new panel is now faster because resume no longer waits for background metadata and thread-list refreshes.

5
.github/release-notes/5.2.1.md vendored Normal file
View file

@ -0,0 +1,5 @@
## Changes
- Updated compatibility for Codex CLI 0.145.0.
- Subagent conversations that support replies can now be continued directly from their panel.
- Fixed malformed links in exported thread archives.

4
.gitignore vendored
View file

@ -1,4 +1,6 @@
node_modules/
.pnpm-store/
.stryker-tmp/
.jj/
.codex/
main.js
@ -7,4 +9,6 @@ data.json
*.log
.DS_Store
dist/
coverage/
reports/
tmp/

View file

@ -2,7 +2,7 @@ This repository contains the Codex Panel Obsidian plugin.
## Working Principles
- Start from evidence. Reproduce defects before fixing them; for product changes, reconstruct the current workflow, user need, alternatives, and losses before deciding what should change.
- Start from evidence. Reproduce defects before fixing them; for product changes, reconstruct the current workflow, user need, alternatives, and losses before deciding what should change. Treat theoretical failure modes and reviewer agreement as hypotheses, not evidence of meaningful user or operational harm.
- Use design documents as context, not as a substitute for current behavior and user expectations. Prefer coherent user-facing behavior and clear ownership, and remove needless abstraction or obsolete compatibility instead of preserving it by default.
- Treat implementation, tests, documentation, lint policy, and final history as one deliverable: inspect each for drift, but update only the durable contracts that changed. Documentation should record user-facing behavior, long-term design, or reusable workflow; do not add it merely to narrate an implementation change or duplicate tests and tooling.
- Parallelize only substantial concerns that can be implemented and tested independently with little shared-file contention. Keep small, tightly coupled, sequential, or coordination-heavy work together.

View file

@ -2,7 +2,7 @@
Codex Panel brings Codex into an Obsidian sidebar. It keeps Codex threads beside your notes, helps you add vault context to prompts, and lets you handle approvals and file changes without switching windows.
If the Codex CLI is already installed and authenticated, Codex Panel uses that local setup. The plugin handles the Obsidian side of the workflow, while models, credentials, sandboxing, tools, hooks, and thread state stay with Codex.
If the Codex CLI is already installed and authenticated, Codex Panel uses that local setup.
![Codex Panel](assets/screenshot.webp)
@ -35,36 +35,46 @@ You can also open the plugin page directly: <https://community.obsidian.md/plugi
## Quick start
1. Open the command palette and run **Codex Panel: Open panel**, or select the ribbon icon.
2. If Obsidian cannot find `codex`, set **Settings -> Codex Panel -> Codex executable** to an absolute path such as `/opt/homebrew/bin/codex`.
3. Ask Codex to work on the vault or project from the composer.
4. Return to earlier work from **Codex Panel: Open thread...** or **Codex Panel: Open threads view**.
1. Open the note or project you want to work on.
2. Run **Codex Panel: Open panel** from the command palette, or select the ribbon icon.
3. In the composer, reference what matters and describe the outcome you want. For example: `@active Summarize this note and suggest the next three actions.`
4. Follow the turn in the panel, respond to requests when needed, and review any file changes before continuing.
## Using Codex Panel
If Obsidian cannot find `codex`, set **Settings -> Codex Panel -> Codex executable** to an absolute path such as `/opt/homebrew/bin/codex`.
Each panel can keep a separate conversation, so related work can stay open side by side, including temporary read-only side chats. Start fresh from the panel, reopen recent threads from the picker or panel history, or use the Threads view to see active work across the vault.
## Working with Codex Panel
The composer treats Obsidian content as prompt context. It suggests vault files and recent notes for wikilinks, keeps links readable while attaching resolved files, and opens file links from Codex replies back in Obsidian. Use `@active` or `@selection` to include the active file or current Markdown selection without pasting it manually, or enable **Reference active file on send** to reference the current active file with each composer send. When Obsidian Daily Notes or the daily section of Periodic Notes is enabled, `@today`, `@tomorrow`, and `@yesterday` insert wikilinks using that daily-note folder and date format. Paste or drop files to save them in the attachment folder and reference them from the same prompt.
### Keep work separate or pick it up later
For context outside the current composer, use `/refer <thread> <message>` to send a message with recent turns from another Codex thread, or `/web <url> [message]` to fetch readable web content and attach it to the next turn without saving a note.
A panel is an independent working surface with its own active thread and draft. Open multiple panels when different tasks should stay visible and separate. Regular Codex threads remain available after a panel closes; return to them from **Codex Panel: Open thread...**, panel history, or the Threads view.
Completions cover slash commands, `$skill-name` skills, Obsidian tags, recent threads, models, reasoning levels, and permission profiles. Use `/help` for the current command list.
For a one-off question alongside the current thread, use `/btw` to open a temporary side chat. To carry context between regular threads without merging their histories, use `/refer <thread> <message>`. The submitted message keeps a normal link to the referenced thread; clicking it opens the thread in an available Codex Panel, creating one when needed.
While a turn is running, the panel streams the conversation, reasoning, tool activity, file changes, plans, and agent activity. You can answer Codex questions, approve commands, respond to requests, steer or interrupt the turn, and manage active goals above the conversation.
### Bring in the right context
When Codex changes files, open an Obsidian diff view to review the changes and copy the patch. For a focused note edit, select Markdown text and run **Codex Panel: Rewrite selection** to ask Codex for a replacement and review a selection-scoped diff before applying it.
The composer lets you point Codex to relevant material without pasting it into the prompt. Type wikilinks to reference vault files while keeping their names readable. Use `@active` for the active file or `@selection` for the current Markdown selection.
Use the toolbar, composer status row, or slash commands to inspect usage, adjust turn settings, and check Codex connection details.
When Obsidian Daily Notes or the daily section of Periodic Notes is enabled, `@today`, `@tomorrow`, and `@yesterday` resolve through its folder and date format. Paste or drop files to save them in the configured attachment folder and reference them from the same prompt. For material outside the vault, `/web <url> [message]` fetches readable page content and attaches it to the next turn without creating a note.
Threads can be archived as Markdown notes with a configurable folder, filename template, and tags. You can also archive without saving, then restore or permanently delete archived threads from settings.
### Guide a running turn
As a turn runs, the panel keeps the conversation together with requests that need your attention. You can answer questions, approve or reject actions, steer the turn with another message, or interrupt it. Plans, goals, tool activity, and agent work remain available when you need to inspect how the task is progressing.
### Review and keep the result
When Codex changes files, open the Obsidian diff view to review the changes and copy the patch. For a focused edit, select Markdown text and run **Codex Panel: Rewrite selection** to request a replacement and review a selection-scoped diff before applying it.
Archive a finished thread with or without saving it as a Markdown note. Saved notes use the configured folder, filename template, and tags; archived threads can later be restored or permanently deleted from settings.
Use `/help` for the current slash command list.
## Compatibility
| Key | Version | Policy |
| ------------------------ | --------- | --------------------------------------------------------------------------------------------------- |
| `manifest.minAppVersion` | `1.12.0` | Minimum Obsidian desktop version declared for plugin loading. |
| `obsidian` API types | `1.12.3` | TypeScript API package used for compile-time checks; kept in the same minor as `manifest` baseline. |
| `codex.testedCliVersion` | `0.144.4` | Exact CLI patch used to generate and verify bindings; compatibility is tracked by minor version. |
| Key | Version | Policy |
| -------------------------------------- | --------- | --------------------------------------------------------------------------------------------------- |
| `manifest.minAppVersion` | `1.12.0` | Minimum Obsidian desktop version declared for plugin loading. |
| `obsidian` API types | `1.12.3` | TypeScript API package used for compile-time checks; kept in the same minor as `manifest` baseline. |
| `codexAppServer.testedCliVersion` | `0.145.0` | Exact CLI patch used to generate and verify bindings; compatibility is tracked by minor version. |
Codex Panel depends on the experimental `codex app-server` API.

View file

@ -10,8 +10,14 @@ Keep the panel thin. Codex Panel owns the Obsidian experience around Codex: pane
Codex owns runtime behavior and thread state: models, credentials, sandboxing, approval policy, MCP servers, hooks, providers, agent-initiated network access, thread history, archived state, goals, and runtime settings. The panel should read and update that state through `codex app-server`, not redefine it.
The panel may provide a narrow management surface when direct configuration of Codex-owned state is impractical and app-server still owns the semantics. Hook controls serve this role: help users recognize and manage hooks they added or changed without presenting the panel as a code-audit surface.
The panel may acquire prompt context through an explicit user action. `/web` fetches the requested page through Obsidian and attaches the extracted content as untrusted turn context; it is not a search surface or agent network policy.
Panel-acquired context remains reference material rather than current user authorization. Send its bounded payload as untrusted app-server context without adding Panel-owned metadata envelopes to Codex history. Apply physical byte and part limits at the app-server boundary; keep source-specific selection and truncation policy with the context producer. Keep read-only compatibility for envelopes written by older Panel versions isolated at history projection boundaries.
Vault file references are prompt handoff data, not durable thread metadata. Keep their display state local; ordinary wikilinks remain represented by the user-authored message text. Thread references persist as ordinary, human-readable `codex://threads/` Markdown links, while their bounded transcript payload remains ephemeral turn context.
Panel settings should store only panel-specific preferences. Do not mirror Codex configuration in Obsidian settings just to display or inspect it.
## Sources of Truth
@ -24,6 +30,8 @@ Runtime controls should express visible user intent for the active thread rather
An app-server context is the pair of Codex executable and Vault root. Replacing it must invalidate context-bound connections and work before publishing the new context, keep events attributed to their source context, and discard old runtime metadata. Preserve last-known-good state only across transient failures within the same context.
Vault root is the Panel-owned workspace root for that context. Panel operations always use it as the thread working directory and do not project mutable protocol thread cwd values into panel state.
## Code Boundaries
Raw app-server protocol belongs at the app-server boundary. Boundary code should adapt protocol payloads into panel-owned domain models or small projections before those values reach features, workspace coordination, settings, or UI.
@ -44,19 +52,31 @@ Runtime UI composition is Preact-owned. Preact components should render the pane
Obsidian and app-server boundaries stay outside Preact components. External lifecycles, app-server connections, editor/workspace APIs, and rendering bridges belong in boundary modules.
Chat-visible state belongs in the chat state store and named reducer actions. Components should project that state through narrow selector-backed snapshots rather than mirror it into another reactive store.
Chat-visible state should have one authoritative owner. Components should consume narrow projections of that state rather than mirror it into another reactive store.
Each shared app-server resource should have one authoritative query record. Derived metadata may report status but must not duplicate its snapshot.
TanStack Query is the single panel-side owner of cached app-server resources. Features may project authoritative event results into that state, but should not introduce parallel cache or synchronization mechanisms. Partial read models are reconciled at explicit lifecycle boundaries rather than kept globally and continuously consistent.
Each top-level panel region should subscribe to a narrow selector-backed store projection. Region selectors should retain stable snapshots across unrelated state changes so streaming updates do not rerender the toolbar, goal, or composer unnecessarily.
Thread lifecycle changes should be projected from completed operation facts into the shared read model. When one user action changes multiple visible projections, publish them coherently without delaying live operational state or coupling independent panels.
Imperative DOM bridges are allowed when an external API, host lifecycle, hit-test, focus/selection operation, or measurement problem requires an `HTMLElement`. They should remain narrow boundary adapters, not a second UI composition system inside Preact-owned surfaces.
Reads with different completeness requirements have separate lifecycles. Complete operation-local reads must not replace bounded shared history, and transient activity should come from its owning live state rather than forcing history refreshes.
Imperative UI bridges are allowed only where the host platform requires them. They should remain narrow boundary adapters, not a second UI composition system inside Preact-owned surfaces.
## Interaction Principles
Multiple panels are separate Obsidian leaves. Treat each panel as its own Codex working surface with independent connection, thread, turn state, composer, and pending requests.
Subagent threads opened from agent activity remain persistent and restorable but stay outside ordinary thread history. Treat their panels as read-only conversation surfaces while preserving their parent and agent provenance for future specialized behavior.
Long-running actions must preserve the user intent that started them. Panel-local results may affect a panel only while that intent still owns its target; Codex facts completed in the current app-server context remain shared truth even if the initiating panel has moved on.
Coordinate conflicting work at the narrowest shared semantic owner. Independent panels should not block one another, and stale reads or writes must not overwrite newer shared state. Coalesce work only when replacement is part of the operation's meaning.
Foreground reveal and focus are the narrow workspace-wide exception: the latest user intent wins without serializing unrelated panel or app-server work. Cleanup obligations created by a committed state transition must survive the UI action that initiated them.
Do not use explanatory copy as the default remedy for an unclear or incoherent interface. Before adding instructions, warnings, status text, validation messages, confirmations, or diagnostics, determine why the interface does not communicate the necessary behavior through its structure and state. Prefer improving information hierarchy, labels, defaults, available choices, affordances, validation timing, progress indication, and feedback. Add text only when irreducible information remains and it materially helps the user decide, act, or recover; do not expose implementation concepts merely because they explain the current behavior.
Subagent threads opened from agent activity remain persistent and restorable but stay outside ordinary thread history. Use the app-server direct-input capability when available, falling back to read-only subagent panels for older servers, while preserving parent and agent provenance for other specialized behavior.
Mode-derived restrictions for the active panel thread must remain consistent across actions and UI. Keep connection state, turn busy state, and operations targeting another listed thread in their owning workflows.
Thread history and other app-server resources should follow app-server semantics. Panel-side views are read models over app-server snapshots and lifecycle events; stale or partial refreshes must not overwrite newer state. Obsidian integrations such as archive note export are convenience views, not replacements for Codex history.
@ -76,6 +96,6 @@ Tests should protect user expectations, app-server/panel responsibility boundari
Prefer tests for visible behavior and state-transition invariants across panels, threads, requests, streams, and display fallbacks.
Avoid tests that freeze incidental implementation details such as exact DOM nesting, render counts, node reuse, helper decomposition, or no-op array updates unless those details directly protect a user-visible invariant.
Avoid tests that freeze incidental implementation details unless those details directly protect a user-visible invariant.
Panel tests may define how received structured values are displayed, retained, or normalized. They should not redefine Codex-owned runtime policy, model lists, sandbox behavior, approval policy, or thread history semantics.

View file

@ -8,12 +8,18 @@ Use this document for day-to-day implementation mechanics: commands, generated f
npm ci
npm run fix
npm run check
npm run test:coverage
npm run test:mutation
```
Use the Node.js version in `.node-version`.
Use `npm run fix` for mechanical cleanup, review its diff, and run `npm run check` before handoff.
Use `npm run test:coverage` to identify source modules and branches that lack exercised behavior. It reports every authored TypeScript source file, including files not imported by tests, while excluding generated app-server bindings. Open `coverage/index.html` to inspect line-level gaps. Coverage is diagnostic and has no pass/fail threshold; prioritize user-visible behavior and state-transition invariants rather than raising the aggregate percentage.
Use `npm run test:mutation` for an exploratory mutation test of domain-owned logic, app-server protocol normalization, thread-stream event mapping, and the chat root reducer. Review surviving mutants individually instead of treating the aggregate score as a quality gate: add tests for meaningful behavior gaps, simplify equivalent or redundant code, and leave mutants alone when neither change improves the durable contract. The run ignores expensive module-initialization mutants, is intentionally manual, and writes its ignored HTML report to `reports/mutation/mutation.html`.
## Commit Messages
Use [Conventional Commits 1.0.0](https://www.conventionalcommits.org/en/v1.0.0/) for new commits:

View file

@ -20,7 +20,7 @@ jj git push --remote origin --bookmark main
git push origin X.Y.Z
```
`release:prepare` updates version files and drafts `## Changes` from selected Conventional Commits since the previous version. Review the draft against the full diff for complete, user-facing notes; consolidate and reorder entries as needed, and replace an empty placeholder.
`release:prepare` updates version files and drafts `## Changes` from selected Conventional Commits since the previous version. Use those commits to locate candidate changes, then compare the resulting user-visible behavior with the previous release before choosing the version. Review the draft against the full diff for complete, user-facing notes; consolidate and reorder entries as needed, and replace an empty placeholder.
Run `release:preflight` after the release commit is on `main`; it validates the commit range and release readiness.

View file

@ -54,8 +54,6 @@ export default defineConfig([
},
],
"@typescript-eslint/require-await": "off",
// Declarative setting definitions require Obsidian 1.13; keep display() while the supported API baseline is 1.12.
"obsidianmd/settings-tab/prefer-setting-definitions": "off",
},
},
{

View file

@ -1,7 +1,7 @@
{
"id": "codex-panel",
"name": "Codex Panel",
"version": "5.0.4",
"version": "5.2.1",
"minAppVersion": "1.12.0",
"description": "Codex in your sidebar.",
"author": "murashit",

2317
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "codex-panel",
"version": "5.0.4",
"version": "5.2.1",
"description": "Codex in your Obsidian sidebar.",
"main": "main.js",
"author": "murashit",
@ -36,14 +36,20 @@
"release:preflight": "node scripts/release/preflight.mjs",
"release:prepare": "node scripts/release/prepare.mjs",
"test": "vitest run --pool threads --maxWorkers 6",
"test:coverage": "vitest run --pool threads --maxWorkers 6 --coverage",
"test:mutation": "stryker run",
"typecheck": "tsc -p tsconfig.json --noEmit --incremental --tsBuildInfoFile node_modules/.cache/typescript/tsconfig.tsbuildinfo"
},
"devDependencies": {
"@biomejs/biome": "^2.5.3",
"@commitlint/cli": "^21.2.1",
"@commitlint/config-conventional": "^21.2.0",
"@stryker-mutator/core": "^9.6.1",
"@stryker-mutator/vitest-runner": "^9.6.1",
"@types/mdast": "^4.0.4",
"@types/node": "^26.1.1",
"concurrently": "^10.0.3",
"@vitest/coverage-v8": "^4.1.10",
"concurrently": "^9.2.4",
"conventional-changelog-conventionalcommits": "^10.2.1",
"conventional-commits-parser": "^7.1.0",
"esbuild": "^0.28.0",
@ -60,8 +66,12 @@
"dependencies": {
"@tanstack/query-core": "^5.101.2",
"defuddle": "^0.19.1",
"diff": "^9.0.0",
"mdast-util-from-markdown": "^2.0.3",
"mdast-util-to-markdown": "^2.1.2",
"micromark": "^4.0.2",
"obsidian-daily-notes-interface": "^0.9.5",
"preact": "^10.29.7"
"preact": "^10.29.7",
"unist-util-visit": "^5.1.0"
}
}

View file

@ -86,7 +86,7 @@ export async function createApiBaselineReport(options = {}) {
const initializeRequestAttestationDisabled = initializeCapabilities.requestAttestation === false;
if (!codexReadmeSemver) {
fail("README.md Compatibility table must define `codex.testedCliVersion` as X.Y.Z.");
fail("README.md Compatibility table must define `codexAppServer.testedCliVersion` as X.Y.Z.");
}
if (!codexRecordedSemver || codexRecordedSemver.version !== codexRecordedVersion) {
fail("src/app-server/connection/compatibility.json must declare codexAppServer.testedCliVersion as X.Y.Z.");
@ -180,7 +180,7 @@ function readCompatibilityBaselines(readme) {
const section = markdownSection(readme, "Compatibility");
const table = readMarkdownTableValues(section);
return {
codexTestedCliVersion: table.get("codex.testedCliVersion") ?? null,
codexTestedCliVersion: table.get("codexAppServer.testedCliVersion") ?? null,
obsidianApiTypesVersion: table.get("obsidian") ?? null,
obsidianMinAppVersion: table.get("manifest.minAppVersion") ?? null,
};

View file

@ -30,7 +30,6 @@ import type { ThreadItemsListResponse } from "../../generated/app-server/v2/Thre
import type { ThreadListResponse } from "../../generated/app-server/v2/ThreadListResponse";
import type { ThreadReadResponse } from "../../generated/app-server/v2/ThreadReadResponse";
import type { ThreadResumeResponse } from "../../generated/app-server/v2/ThreadResumeResponse";
import type { ThreadRollbackResponse } from "../../generated/app-server/v2/ThreadRollbackResponse";
import type { ThreadSetNameResponse } from "../../generated/app-server/v2/ThreadSetNameResponse";
import type { ThreadSettingsUpdateResponse } from "../../generated/app-server/v2/ThreadSettingsUpdateResponse";
import type { ThreadStartResponse } from "../../generated/app-server/v2/ThreadStartResponse";
@ -87,7 +86,6 @@ export interface ClientResponseByMethod {
"thread/delete": ThreadDeleteResponse;
"thread/unsubscribe": ThreadUnsubscribeResponse;
"thread/unarchive": ThreadUnarchiveResponse;
"thread/rollback": ThreadRollbackResponse;
"thread/name/set": ThreadSetNameResponse;
"thread/settings/update": ThreadSettingsUpdateResponse;
"thread/turns/list": ThreadTurnsListResponse;

View file

@ -1,6 +1,6 @@
{
"codexAppServer": {
"testedCliVersion": "0.144.4",
"testedCliVersion": "0.145.0",
"typeGeneration": {
"arguments": ["app-server", "generate-ts", "--experimental"]
},

View file

@ -5,32 +5,23 @@ import type { ServerRequest } from "../../generated/app-server/ServerRequest";
import { AppServerClient, type AppServerClientHandlers, type AppServerServerRequestResponder } from "./client";
export interface ConnectionManagerHandlers {
onNotification: (notification: ServerNotification, context: AppServerConnectionContext) => void;
onServerRequest: (request: ServerRequest, responder: AppServerServerRequestResponder, context: AppServerConnectionContext) => void;
onLog: (message: string, context: AppServerConnectionContext) => void;
onExit: (context: AppServerConnectionContext) => void;
onNotification: (notification: ServerNotification) => void;
onServerRequest: (request: ServerRequest, responder: AppServerServerRequestResponder) => void;
onLog: (message: string) => void;
onExit: () => void;
}
export type AppServerClientFactory = (codexPath: string, cwd: string, handlers: AppServerClientHandlers) => AppServerClient;
export interface AppServerConnectionContext {
readonly codexPath: string;
readonly cwd: string;
readonly generation: number;
}
type AppServerClientFactory = (codexPath: string, cwd: string, handlers: AppServerClientHandlers) => AppServerClient;
type ConnectionLifecycleState =
| { kind: "idle"; generation: number }
| {
kind: "connecting";
generation: number;
contextGeneration: number;
codexPath: string;
cwd: string;
client: AppServerClient;
promise: Promise<ServerInitialization>;
}
| { kind: "connected"; generation: number; contextGeneration: number; codexPath: string; cwd: string; client: AppServerClient }
| { kind: "connected"; generation: number; client: AppServerClient }
| { kind: "disconnected"; generation: number };
export class StaleConnectionError extends Error {
@ -44,25 +35,15 @@ export class ConnectionManager {
private state: ConnectionLifecycleState = { kind: "idle", generation: 0 };
constructor(
private readonly codexPath: () => string,
private readonly codexPath: string,
private readonly cwd: string,
initializeParams: InitializeParams,
private readonly clientFactory: AppServerClientFactory = (codexPath, cwd, handlers) =>
new AppServerClient({ codexPath, cwd, handlers, initializeParams }),
private readonly contextGeneration: () => number = () => 0,
) {}
currentClient(): AppServerClient | null {
return this.state.kind === "connected" && this.state.client.isConnected() && this.stateMatchesCurrentContext(this.state)
? this.state.client
: null;
}
currentConnectionContext(): AppServerConnectionContext | null {
if (this.state.kind !== "connected" || !this.state.client.isConnected() || !this.stateMatchesCurrentContext(this.state)) {
return null;
}
return { codexPath: this.state.codexPath, cwd: this.state.cwd, generation: this.state.contextGeneration };
return this.state.kind === "connected" && this.state.client.isConnected() ? this.state.client : null;
}
isConnected(): boolean {
@ -74,31 +55,29 @@ export class ConnectionManager {
if (currentClient) {
return serverInitializationFromResponse(currentClient.initializeResponse);
}
if (this.state.kind === "connecting" && this.stateMatchesCurrentContext(this.state)) return this.state.promise;
if (this.state.kind === "connecting" || this.state.kind === "connected") this.disconnect();
if (this.state.kind === "connecting") return this.state.promise;
if (this.state.kind === "connected") this.disconnect();
const generation = this.state.generation + 1;
const contextGeneration = this.contextGeneration();
const codexPath = this.codexPath();
const codexPath = this.codexPath;
const cwd = this.cwd;
const context = { codexPath, cwd, generation: contextGeneration };
const client = this.clientFactory(codexPath, cwd, {
onNotification: (notification) => {
if (this.isStale(generation)) return;
handlers.onNotification(notification, context);
handlers.onNotification(notification);
},
onServerRequest: (request, responder) => {
if (this.isStale(generation)) return;
handlers.onServerRequest(request, responder, context);
handlers.onServerRequest(request, responder);
},
onLog: (message) => {
if (this.isStale(generation)) return;
handlers.onLog(message, context);
handlers.onLog(message);
},
onExit: () => {
if (this.isStale(generation)) return;
this.state = { kind: "disconnected", generation };
handlers.onExit(context);
handlers.onExit();
},
});
const promise = client
@ -108,7 +87,7 @@ export class ConnectionManager {
client.disconnect();
throw new StaleConnectionError();
}
this.state = { kind: "connected", generation, contextGeneration, codexPath, cwd, client };
this.state = { kind: "connected", generation, client };
return serverInitializationFromResponse(response);
})
.catch((error: unknown) => {
@ -122,14 +101,10 @@ export class ConnectionManager {
throw error;
});
this.state = { kind: "connecting", generation, contextGeneration, codexPath, cwd, client, promise };
this.state = { kind: "connecting", generation, client, promise };
return promise;
}
resetConnection(): void {
this.disconnect();
}
disconnect(): void {
const previous = this.state;
this.state = { kind: "disconnected", generation: previous.generation + 1 };
@ -141,10 +116,6 @@ export class ConnectionManager {
private isStale(generation: number): boolean {
return generation !== this.state.generation;
}
private stateMatchesCurrentContext(state: Extract<ConnectionLifecycleState, { kind: "connecting" | "connected" }>): boolean {
return state.codexPath === this.codexPath() && state.cwd === this.cwd && state.contextGeneration === this.contextGeneration();
}
}
function serverInitializationFromResponse(response: ServerInitialization): ServerInitialization {

View file

@ -0,0 +1,4 @@
export interface AppServerExecutionContext {
readonly codexPath: string;
readonly vaultPath: string;
}

View file

@ -2,11 +2,17 @@ import { AppServerClient } from "./client";
import type { AppServerClientAccessOptions } from "./client-access";
import { codexPanelAppServerInitializeParams } from "./client-profile";
export interface ShortLivedAppServerClientLifetime {
created(client: AppServerClient): void;
disposed(client: AppServerClient): void;
}
export async function withShortLivedAppServerClient<T>(
codexPath: string,
cwd: string,
operation: (client: AppServerClient) => Promise<T>,
options: AppServerClientAccessOptions = {},
lifetime?: ShortLivedAppServerClientLifetime,
): Promise<T> {
const client = new AppServerClient({
codexPath,
@ -27,11 +33,13 @@ export async function withShortLivedAppServerClient<T>(
onExit: () => undefined,
},
});
lifetime?.created(client);
try {
await client.connect();
return await operation(client);
} finally {
client.disconnect();
lifetime?.disposed(client);
}
}

View file

@ -24,10 +24,7 @@ export interface AppServerTransportHandlers {
onError: (error: Error) => void;
}
export function createAppServerSpawnSpec(
codexPath: string,
options: { platform?: NodeJS.Platform; comSpec?: string } = {},
): AppServerSpawnSpec {
function createAppServerSpawnSpec(codexPath: string, options: { platform?: NodeJS.Platform; comSpec?: string } = {}): AppServerSpawnSpec {
const platform = options.platform ?? process.platform;
if (platform !== "win32" || !isWindowsCommandScript(codexPath)) {
return { command: codexPath, args: ["app-server"], killProcessTreeOnStop: false };

View file

@ -0,0 +1,148 @@
import type { VaultFileReference } from "../../domain/chat/input";
import type { ReferencedThreadMetadata } from "../../domain/threads/reference";
/*
* Compatibility boundary for user messages persisted by old Codex Panel
* versions. New request construction must not depend on this module. Once old
* thread history no longer needs recovery, delete this file and its explicit
* fallbacks in turn.ts.
*/
function legacyPanelReferencedThreadFromPrompt(text: string): { text: string; reference: ReferencedThreadMetadata } | null {
const envelope = referencedThreadEnvelopeFromPrompt(text);
return envelope ? { text: envelope.visibleText, reference: envelope.reference } : null;
}
interface LegacyPanelUserMessageContentItem {
type: string;
name?: string;
path?: string;
}
interface LegacyPanelUserMessageInput {
content: readonly LegacyPanelUserMessageContentItem[];
visibleText: string;
}
export interface LegacyPanelUserMessageProjection {
text: string;
referencedThread: ReferencedThreadMetadata | null;
fileReferences: VaultFileReference[];
mentionTextByContentIndex: ReadonlyMap<number, string>;
}
export function legacyPanelUserMessageProjection(input: LegacyPanelUserMessageInput): LegacyPanelUserMessageProjection {
const referencedThread = legacyPanelReferencedThreadFromPrompt(input.visibleText);
const fileReferences: VaultFileReference[] = [];
const mentionTextByContentIndex = new Map<number, string>();
for (const [index, item] of input.content.entries()) {
if (!isMention(item)) continue;
const reference = legacyPanelFileReference(item);
if (reference) fileReferences.push(reference);
if (!input.visibleText) {
mentionTextByContentIndex.set(index, reference ? `[file] ${reference.path}` : `[@${item.name}] ${item.path}`);
}
}
return {
text: referencedThread?.text ?? input.visibleText,
referencedThread: referencedThread?.reference ?? null,
fileReferences,
mentionTextByContentIndex,
};
}
function isMention(
input: LegacyPanelUserMessageInput["content"][number],
): input is LegacyPanelUserMessageContentItem & { type: "mention"; name: string; path: string } {
return input.type === "mention" && typeof input.name === "string" && typeof input.path === "string";
}
function legacyPanelFileReference(input: { name: string; path: string }): VaultFileReference | null {
if (!input.path || /^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(input.path)) return null;
return { name: input.name, path: input.path };
}
const REFERENCED_THREAD_ENVELOPE_START = "[Codex Panel referenced thread v1]";
const REFERENCED_THREAD_ENVELOPE_END = "[/Codex Panel referenced thread]";
interface ReferencedThreadEnvelope {
version: 1;
reference: ReferencedThreadMetadata;
visibleText: string;
}
interface ReferencedThreadEnvelopeMetadata {
version: 1;
threadId: string;
title: string;
includedTurns: number;
turnLimit: number;
}
function referencedThreadEnvelopeFromPrompt(text: string): ReferencedThreadEnvelope | null {
const headerStart = text.indexOf(REFERENCED_THREAD_ENVELOPE_START);
const requestBoundary = `\n${REFERENCED_THREAD_ENVELOPE_END}\n\nCurrent user request:\n`;
const boundaryStart = text.lastIndexOf(requestBoundary);
if (headerStart !== 0 || boundaryStart === -1) return null;
const metadataText = firstNonEmptyLine(text.slice(REFERENCED_THREAD_ENVELOPE_START.length, boundaryStart));
const metadata = referencedThreadEnvelopeMetadataFromJson(metadataText);
const visibleText = text.slice(boundaryStart + requestBoundary.length).trim();
if (!metadata || !visibleText) return null;
return {
version: 1,
visibleText,
reference: {
threadId: metadata.threadId,
title: metadata.title,
includedTurns: metadata.includedTurns,
turnLimit: metadata.turnLimit,
},
};
}
function firstNonEmptyLine(text: string): string | null {
return (
text
.split(/\r?\n/)
.map((line) => line.trim())
.find((line) => line.length > 0) ?? null
);
}
function referencedThreadEnvelopeMetadataFromJson(text: string | null): ReferencedThreadEnvelopeMetadata | null {
if (!text) return null;
let parsed: unknown;
try {
parsed = JSON.parse(text) as unknown;
} catch {
return null;
}
if (!parsed || typeof parsed !== "object") return null;
const value = parsed as Record<string, unknown>;
if (value["version"] !== 1) return null;
const threadId = stringValue(value["threadId"]);
const title = stringValue(value["title"]);
const includedTurns = finiteNonNegativeInteger(value["includedTurns"]);
const turnLimit = finitePositiveInteger(value["turnLimit"]);
if (!threadId || !title || includedTurns === null || turnLimit === null) return null;
return {
version: 1,
threadId,
title,
includedTurns,
turnLimit,
};
}
function stringValue(value: unknown): string | null {
return typeof value === "string" && value.length > 0 ? value : null;
}
function finiteNonNegativeInteger(value: unknown): number | null {
return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : null;
}
function finitePositiveInteger(value: unknown): number | null {
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : null;
}

View file

@ -0,0 +1,222 @@
import type { VaultFileReference } from "../../domain/chat/input";
import { isPanelSubmissionClientId, turnContextSubmissionId } from "../../domain/chat/submission-id";
import type { ReferencedThreadMetadata } from "../../domain/threads/reference";
/*
* Read-only compatibility for v2 metadata envelopes written by Codex Panel
* versions that predate durable, human-readable references in message text.
* Request construction must not import this module.
*/
const TURN_CONTEXT_MANIFEST_PREFIX = "[Codex Panel context v2]";
const TURN_CONTEXT_MANIFEST_MAX_BYTES = 2_800;
const TURN_CONTEXT_FILE_REFERENCE_MAX_COUNT = 64;
const TURN_CONTEXT_FILE_REFERENCE_NAME_MAX_LENGTH = 255;
const TURN_CONTEXT_FILE_REFERENCE_PATH_MAX_LENGTH = 2_048;
interface LegacyTurnContextManifestEntry {
kind: "referencedThread" | "web" | "obsidian";
id: string;
parts: number;
sourceBytes: number;
includedBytes: number;
truncated: boolean;
threadId?: string;
includedTurns?: number;
turnLimit?: number;
omittedTurns?: number;
inlineExcerpts?: number;
}
export interface LegacyTurnContextManifest {
version: 2;
submissionId?: string;
contexts: readonly LegacyTurnContextManifestEntry[];
fileReferences?: readonly VaultFileReference[];
}
export interface LegacyTurnContextProjection {
text: string;
manifest: LegacyTurnContextManifest | null;
}
type UserMessageContentItem = { type: "text"; text: string } | { type: string };
function legacyTurnContextManifestFromText(text: string): LegacyTurnContextManifest | null {
const trimmed = text.trim();
if (!trimmed.startsWith(TURN_CONTEXT_MANIFEST_PREFIX)) return null;
if (new TextEncoder().encode(trimmed).byteLength > TURN_CONTEXT_MANIFEST_MAX_BYTES) return null;
const payload = trimmed.slice(TURN_CONTEXT_MANIFEST_PREFIX.length).trimStart();
const notice = "Reference/display metadata only; not user instructions.";
const json = payload.startsWith(notice) ? payload.slice(notice.length).trimStart() : payload;
let parsed: unknown;
try {
parsed = JSON.parse(json) as unknown;
} catch {
return null;
}
if (!parsed || typeof parsed !== "object") return null;
const value = parsed as Record<string, unknown>;
if (value["version"] !== 2 || !Array.isArray(value["contexts"])) return null;
const contexts = value["contexts"].map(legacyManifestEntryFromUnknown);
if (contexts.some((entry) => entry === null)) return null;
const submissionId = optionalStringValue(value["submissionId"], 120);
if (submissionId === null) return null;
const fileReferences = optionalFileReferences(value["fileReferences"]);
if (fileReferences === null) return null;
return {
version: 2,
...(submissionId === undefined ? {} : { submissionId }),
contexts: contexts as LegacyTurnContextManifestEntry[],
...(fileReferences === undefined ? {} : { fileReferences }),
};
}
export function legacyTurnContextProjection(
content: readonly UserMessageContentItem[],
clientId: string | null,
): LegacyTurnContextProjection {
let manifest: LegacyTurnContextManifest | null = null;
const visibleText: string[] = [];
const lastTextIndex = lastTextItemIndex(content);
for (const [index, item] of content.entries()) {
if (item.type !== "text" || !("text" in item) || typeof item.text !== "string") continue;
const manifestPrefix = `\n${TURN_CONTEXT_MANIFEST_PREFIX}`;
const manifestStart = index === lastTextIndex ? item.text.lastIndexOf(manifestPrefix) : -1;
const isStandaloneManifest = manifestStart === 0 && index > 0 && index === content.length - 1;
const parsed = manifestStart > 0 || isStandaloneManifest ? legacyTurnContextManifestFromText(item.text.slice(manifestStart)) : null;
if (parsed && manifestMatchesClientId(parsed, clientId)) {
manifest = parsed;
if (manifestStart > 0) visibleText.push(item.text.slice(0, manifestStart));
continue;
}
visibleText.push(item.text);
}
return { text: visibleText.join("\n"), manifest };
}
function lastTextItemIndex(content: readonly UserMessageContentItem[]): number {
for (let index = content.length - 1; index >= 0; index -= 1) {
if (content[index]?.type === "text") return index;
}
return -1;
}
function manifestMatchesClientId(manifest: LegacyTurnContextManifest, clientId: string | null): boolean {
if (!isPanelSubmissionClientId(clientId)) return false;
const submissionId = turnContextSubmissionId(clientId);
if (manifest.submissionId !== undefined && manifest.submissionId !== submissionId) return false;
const contextIds = manifest.contexts.map((context) => context.id);
return (
(manifest.submissionId === submissionId || contextIds.length > 0) &&
new Set(contextIds).size === contextIds.length &&
contextIds.every((contextId) => new RegExp(`^${escapeRegExp(submissionId)}\\.\\d{2}$`).test(contextId))
);
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
export function referencedThreadFromLegacyManifest(manifest: LegacyTurnContextManifest | null): ReferencedThreadMetadata | null {
const context = manifest?.contexts.find((entry) => entry.kind === "referencedThread");
if (!context?.threadId || context.includedTurns === undefined || context.turnLimit === undefined || context.omittedTurns === undefined) {
return null;
}
return {
threadId: context.threadId,
title: context.threadId.slice(0, 8),
includedTurns: context.includedTurns,
turnLimit: context.turnLimit,
omittedTurns: context.omittedTurns,
truncated: context.truncated,
};
}
export function fileReferencesFromLegacyManifest(manifest: LegacyTurnContextManifest | null): VaultFileReference[] {
return manifest?.fileReferences ? [...manifest.fileReferences] : [];
}
function legacyManifestEntryFromUnknown(input: unknown): LegacyTurnContextManifestEntry | null {
if (!input || typeof input !== "object") return null;
const value = input as Record<string, unknown>;
const kind = contextKind(value["kind"]);
const id = stringValue(value["id"]);
const parts = nonNegativeInteger(value["parts"]);
const sourceBytes = nonNegativeInteger(value["sourceBytes"]);
const includedBytes = nonNegativeInteger(value["includedBytes"]);
const truncated = value["truncated"];
if (!kind || !id || parts === null || sourceBytes === null || includedBytes === null || typeof truncated !== "boolean") return null;
if (kind === "obsidian") {
const inlineExcerpts = optionalNonNegativeInteger(value["inlineExcerpts"]);
if (inlineExcerpts === null) return null;
return {
kind,
id,
parts,
sourceBytes,
includedBytes,
truncated,
...(inlineExcerpts === undefined ? {} : { inlineExcerpts }),
};
}
if (kind !== "referencedThread") return { kind, id, parts, sourceBytes, includedBytes, truncated };
const threadId = stringValue(value["threadId"]);
const includedTurns = nonNegativeInteger(value["includedTurns"]);
const turnLimit = positiveInteger(value["turnLimit"]);
const omittedTurns = nonNegativeInteger(value["omittedTurns"]);
if (!threadId || includedTurns === null || turnLimit === null || omittedTurns === null) return null;
return {
kind,
id,
parts,
sourceBytes,
includedBytes,
truncated,
threadId,
includedTurns,
turnLimit,
omittedTurns,
};
}
function contextKind(value: unknown): LegacyTurnContextManifestEntry["kind"] | null {
return value === "referencedThread" || value === "web" || value === "obsidian" ? value : null;
}
function stringValue(value: unknown): string | null {
return typeof value === "string" && value.length > 0 && value.length <= 160 ? value : null;
}
function optionalStringValue(value: unknown, maxLength: number): string | null | undefined {
if (value === undefined) return undefined;
return typeof value === "string" && value.length > 0 && value.length <= maxLength ? value : null;
}
function optionalFileReferences(value: unknown): VaultFileReference[] | null | undefined {
if (value === undefined) return undefined;
if (!Array.isArray(value) || value.length > TURN_CONTEXT_FILE_REFERENCE_MAX_COUNT) return null;
const references = value.map(manifestFileReference);
return references.some((reference) => reference === null) ? null : (references as VaultFileReference[]);
}
function manifestFileReference(input: unknown): VaultFileReference | null {
if (!input || typeof input !== "object") return null;
const reference = input as Record<string, unknown>;
const name = optionalStringValue(reference["name"], TURN_CONTEXT_FILE_REFERENCE_NAME_MAX_LENGTH);
const path = optionalStringValue(reference["path"], TURN_CONTEXT_FILE_REFERENCE_PATH_MAX_LENGTH);
return name && path ? { name, path } : null;
}
function nonNegativeInteger(value: unknown): number | null {
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : null;
}
function optionalNonNegativeInteger(value: unknown): number | null | undefined {
return value === undefined ? undefined : nonNegativeInteger(value);
}
function positiveInteger(value: unknown): number | null {
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : null;
}

View file

@ -1,4 +1,6 @@
import { splitUtf8Context } from "../../domain/chat/context-budget";
import type { CodexInputItem } from "../../domain/chat/input";
import { turnContextSubmissionId } from "../../domain/chat/submission-id";
type AppServerUserInputImageDetail = "auto" | "low" | "high" | "original";
@ -6,28 +8,75 @@ type AppServerUserInput =
| { type: "text"; text: string; text_elements: [] }
| { type: "image"; detail?: AppServerUserInputImageDetail; url: string }
| { type: "localImage"; detail?: AppServerUserInputImageDetail; path: string }
| { type: "skill"; name: string; path: string }
| { type: "mention"; name: string; path: string };
| { type: "skill"; name: string; path: string };
interface AppServerAdditionalContextEntry {
value: string;
kind: "untrusted" | "application";
}
export interface AppServerTurnInput {
input: AppServerUserInput[];
additionalContext?: Record<string, AppServerAdditionalContextEntry>;
}
const ADDITIONAL_CONTEXT_PART_BODY_MAX_BYTES = 2_800;
const ADDITIONAL_CONTEXT_MAX_PARTS = 8;
export function toAppServerUserInput(input: readonly CodexInputItem[]): AppServerUserInput[] {
return input.flatMap((item) => appServerUserInputItemFromCodexInputItem(item));
}
export function additionalContextFromCodexInput(
input: readonly CodexInputItem[],
): Record<string, AppServerAdditionalContextEntry> | undefined {
export function appServerTurnInputFromCodexInput(input: readonly CodexInputItem[], submissionId: string): AppServerTurnInput {
const additionalContext: Record<string, AppServerAdditionalContextEntry> = {};
for (const item of input) {
if (item.type !== "additionalContext") continue;
if (!item.key || !item.value) continue;
additionalContext[item.key] = { value: item.value, kind: item.kind };
const contexts = input.filter(
(item): item is Extract<CodexInputItem, { type: "additionalContext" }> =>
item.type === "additionalContext" && Boolean(item.key) && Boolean(item.value),
);
if (contexts.length > ADDITIONAL_CONTEXT_MAX_PARTS) {
throw new Error(`Too many additional context sources (${String(contexts.length)}).`);
}
return Object.keys(additionalContext).length > 0 ? additionalContext : undefined;
const partAllocations = allocatedPartCounts(contexts);
for (const [contextIndex, item] of contexts.entries()) {
const id = `${turnContextSubmissionId(submissionId)}.${String(contextIndex).padStart(2, "0")}`;
const split = splitUtf8Context(item.value, ADDITIONAL_CONTEXT_PART_BODY_MAX_BYTES, partAllocations[contextIndex] ?? 1);
const partCount = split.parts.length;
split.parts.forEach((part, partIndex) => {
const key = [
"codex_panel",
id,
safeKeyPart(item.key),
`part_${String(partIndex + 1).padStart(2, "0")}_of_${String(partCount).padStart(2, "0")}`,
].join(".");
additionalContext[key] = {
kind: item.kind,
value: [
`Codex Panel context part ${String(partIndex + 1)}/${String(partCount)}.`,
`Source: ${safeKeyPart(item.key)}`,
"",
part,
].join("\n"),
};
});
}
return {
input: toAppServerUserInput(input),
...(Object.keys(additionalContext).length > 0 ? { additionalContext } : {}),
};
}
function allocatedPartCounts(contexts: readonly Extract<CodexInputItem, { type: "additionalContext" }>[]): number[] {
const allocations = contexts.map(() => 1);
const desired = contexts.map(
(context) => splitUtf8Context(context.value, ADDITIONAL_CONTEXT_PART_BODY_MAX_BYTES, ADDITIONAL_CONTEXT_MAX_PARTS).parts.length,
);
let remaining = ADDITIONAL_CONTEXT_MAX_PARTS - contexts.length;
for (let index = 0; index < contexts.length && remaining > 0; index += 1) {
const extra = Math.min(Math.max((desired[index] ?? 1) - 1, 0), remaining);
allocations[index] = (allocations[index] ?? 1) + extra;
remaining -= extra;
}
return allocations;
}
function appServerUserInputItemFromCodexInputItem(item: CodexInputItem): AppServerUserInput[] {
@ -40,13 +89,18 @@ function appServerUserInputItemFromCodexInputItem(item: CodexInputItem): AppServ
return [{ type: "localImage", path: item.path, ...appServerImageDetailProp(item.detail) }];
case "skill":
return [{ type: "skill", name: item.name, path: item.path }];
case "mention":
return [{ type: "mention", name: item.name, path: item.path }];
case "fileReference":
return [];
case "additionalContext":
return [];
}
}
function safeKeyPart(value: string): string {
const safe = value.replace(/[^A-Za-z0-9_.-]/g, "_").slice(0, 120);
return safe || "context";
}
function appServerImageDetailProp(detail: Extract<CodexInputItem, { type: "image" | "localImage" }>["detail"]): {
detail?: AppServerUserInputImageDetail;
} {

View file

@ -21,6 +21,7 @@ export function threadFromThreadRecord(thread: ThreadRecord, options: { archived
archived: options.archived ?? false,
createdAt: finiteTimestamp(thread.createdAt),
updatedAt: finiteTimestamp(thread.updatedAt),
canAcceptDirectInput: typeof thread.canAcceptDirectInput === "boolean" ? thread.canAcceptDirectInput : null,
provenance: threadProvenance(thread),
...(hasRecencyAt ? { recencyAt: typeof recencyAt === "number" && Number.isFinite(recencyAt) ? recencyAt : null } : {}),
};

View file

@ -1,3 +1,5 @@
import type { VaultFileReference } from "../../domain/chat/input";
import type { ReferencedThreadMetadata } from "../../domain/threads/reference";
import {
nonEmptyTurnTranscriptSummaries,
type ThreadTranscriptEntry,
@ -6,13 +8,16 @@ import {
} from "../../domain/threads/transcript";
import type { ThreadItem as GeneratedThreadItem } from "../../generated/app-server/v2/ThreadItem";
import type { Turn as GeneratedTurn } from "../../generated/app-server/v2/Turn";
import { legacyPanelUserMessageProjection } from "./legacy-panel-user-message";
import {
fileReferencesFromLegacyManifest,
legacyTurnContextProjection,
referencedThreadFromLegacyManifest,
} from "./legacy-turn-context-manifest";
export type TurnItem = GeneratedThreadItem;
export type TurnRecord = GeneratedTurn;
type AppServerUserInput = Extract<TurnItem, { type: "userMessage" }>["content"][number];
type AppServerTextUserInput = Extract<AppServerUserInput, { type: "text" }>;
function transcriptEntriesFromTurnRecord(turn: TurnRecord): ThreadTranscriptEntry[] {
return turn.items.flatMap((item) => transcriptEntriesFromTurnItem(item, turn));
}
@ -48,8 +53,42 @@ export function chronologicalTurnTranscriptSummariesFromTurnRecords(turns: reado
);
}
export function turnUserItemText(item: Extract<TurnItem, { type: "userMessage" }>): string {
return userInputText(item.content);
export interface TurnUserItemProjection {
text: string;
referencedThread: ReferencedThreadMetadata | null;
fileReferences: VaultFileReference[];
contexts: TurnUserContextMetadata[];
}
interface TurnUserContextMetadata {
kind: "web" | "obsidian";
truncated: boolean;
inlineExcerpts?: number;
}
export function turnUserItemProjection(item: Extract<TurnItem, { type: "userMessage" }>): TurnUserItemProjection {
const projected = legacyTurnContextProjection(item.content, item.clientId);
if (!projected.manifest) {
const legacy = legacyPanelUserMessageProjection({
content: item.content,
visibleText: projected.text,
});
const supplementalText = nonTextUserInputText(item.content, projected.text, legacy.mentionTextByContentIndex);
return {
text: [legacy.text, supplementalText].filter(Boolean).join("\n"),
referencedThread: legacy.referencedThread,
fileReferences: legacy.fileReferences,
contexts: [],
};
}
const supplementalText = nonTextUserInputText(item.content, projected.text);
const text = [projected.text, supplementalText].filter(Boolean).join("\n");
return {
text,
referencedThread: referencedThreadFromLegacyManifest(projected.manifest),
fileReferences: fileReferencesFromLegacyManifest(projected.manifest),
contexts: legacyContextMetadata(projected.manifest.contexts),
};
}
export function lastAgentMessageTextFromTurnRecord(turn: TurnRecord): string | null {
@ -65,8 +104,20 @@ export function lastAgentMessageTextFromTurnRecord(turn: TurnRecord): string | n
function transcriptEntriesFromTurnItem(item: TurnItem, turn: TurnRecord): ThreadTranscriptEntry[] {
if (item.type === "userMessage") {
const text = turnUserItemText(item).trim();
return text ? [{ kind: "user", text, timestamp: turn.startedAt }] : [];
const projection = turnUserItemProjection(item);
const text = projection.text.trim();
const contexts = projection.contexts.map((context) => ({ kind: context.kind, truncated: context.truncated }));
return text
? [
{
kind: "user",
text,
timestamp: turn.startedAt,
...(projection.referencedThread ? { referencedThread: projection.referencedThread } : {}),
...(contexts.length > 0 ? { contexts } : {}),
},
]
: [];
}
if (item.type === "agentMessage") {
const text = item.text.trim();
@ -79,22 +130,36 @@ function transcriptEntriesFromTurnItem(item: TurnItem, turn: TurnRecord): Thread
return [];
}
function userInputText(content: readonly AppServerUserInput[]): string {
const textItems = content.filter(isTextUserInput);
const hasText = textItems.some((item) => item.text.length > 0);
const textIncludes = (value: string) => value.length > 0 && textItems.some((item) => item.text.includes(value));
function legacyContextMetadata(
contexts: readonly { kind: "referencedThread" | "web" | "obsidian"; truncated: boolean; inlineExcerpts?: number }[],
): TurnUserContextMetadata[] {
return contexts.flatMap((context) => {
if (context.kind === "referencedThread") return [];
return [
{
kind: context.kind,
truncated: context.truncated,
...(context.inlineExcerpts === undefined ? {} : { inlineExcerpts: context.inlineExcerpts }),
},
];
});
}
function nonTextUserInputText(
content: Extract<TurnItem, { type: "userMessage" }>["content"],
visibleText: string,
mentionTextByContentIndex?: ReadonlyMap<number, string>,
): string {
const hasText = visibleText.length > 0;
const textIncludes = (value: string) => value.length > 0 && visibleText.includes(value);
return content
.map((item) => {
if (item.type === "text") return item.text;
.map((item, index) => {
if (item.type === "localImage") return hasText && textIncludes(item.path) ? "" : `[local image] ${item.path}`;
if (item.type === "image") return hasText && textIncludes(item.url) ? "" : `[image] ${item.url}`;
if (item.type === "mention") return hasText ? "" : `[@${item.name}] ${item.path}`;
return hasText ? "" : `[$${item.name}] ${item.path}`;
if (item.type === "mention") return mentionTextByContentIndex?.get(index) ?? "";
if (item.type === "skill") return hasText ? "" : `[$${item.name}] ${item.path}`;
return "";
})
.filter(Boolean)
.join("\n");
}
function isTextUserInput(item: AppServerUserInput): item is AppServerTextUserInput {
return item.type === "text";
}

View file

@ -0,0 +1,65 @@
import type { InfiniteData } from "@tanstack/query-core";
import { type Thread, threadRecencyAt } from "../../domain/threads/model";
import type { ThreadPage } from "../services/threads";
import type { ThreadListMutation } from "./thread-list-mutation";
export type ActiveThreadCursor = string | null;
export type ActiveThreadData = InfiniteData<ThreadPage, ActiveThreadCursor>;
export function activeThreadsFromData(data: ActiveThreadData | undefined): readonly Thread[] | null {
if (!data) return null;
return orderedUniqueThreads(data.pages.flatMap((page) => page.threads));
}
export function recentActiveThreadsFromData(data: ActiveThreadData | undefined): readonly Thread[] | null {
if (!data) return null;
const threads = activeThreadsFromData(data) ?? [];
const recentWindowCapacity = Math.max(data.pages[0]?.fetchedSize ?? 0, threads.length > 0 ? 1 : 0);
return threads.slice(0, recentWindowCapacity);
}
export function activeThreadDataHasMore(data: ActiveThreadData | undefined): boolean {
return data?.pages.at(-1)?.nextCursor != null;
}
export function applyActiveThreadMutation(data: ActiveThreadData | undefined, mutation: ThreadListMutation): ActiveThreadData | undefined {
if (!data || mutation.list !== "active" || mutation.kind === "refresh") return data;
const pages = data.pages.map((page) => ({ ...page, threads: [...page.threads] }));
switch (mutation.kind) {
case "upsert": {
const existing = pages.some((page) => page.threads.some((thread) => thread.id === mutation.thread.id));
if (existing) {
for (const page of pages) {
page.threads = page.threads.map((thread) => (thread.id === mutation.thread.id ? mutation.thread : thread));
}
} else if (pages[0]) {
pages[0].threads = [mutation.thread, ...pages[0].threads];
}
break;
}
case "remove":
for (const page of pages) page.threads = page.threads.filter((thread) => thread.id !== mutation.threadId);
break;
case "update":
for (const page of pages) {
page.threads = page.threads.map((thread) => (thread.id === mutation.threadId ? { ...thread, ...mutation.changes } : thread));
}
break;
}
return { pages, pageParams: [...data.pageParams] };
}
function orderedUniqueThreads(threads: readonly Thread[]): readonly Thread[] {
const seen = new Set<string>();
return threads
.filter((thread) => {
if (seen.has(thread.id)) return false;
seen.add(thread.id);
return true;
})
.map((thread, index) => ({ thread, index }))
.sort((left, right) => threadRecencyAt(right.thread) - threadRecencyAt(left.thread) || left.index - right.index)
.map(({ thread }) => thread);
}

View file

@ -1,4 +1,12 @@
import { QueryClient, QueryObserver, type QueryObserverResult } from "@tanstack/query-core";
import {
CancelledError,
InfiniteQueryObserver,
type InfiniteQueryObserverOptions,
type InfiniteQueryObserverResult,
QueryClient,
QueryObserver,
type QueryObserverResult,
} from "@tanstack/query-core";
import type { ModelMetadata, SkillMetadata } from "../../domain/catalog/metadata";
import { cloneRuntimeConfigSnapshot, type RuntimeConfigSnapshot } from "../../domain/runtime/config";
import type { RateLimitSnapshot } from "../../domain/runtime/metrics";
@ -10,103 +18,88 @@ import {
diagnosticProbeOk,
diagnosticsWithProbe,
} from "../../domain/server/diagnostics";
import type { SharedServerMetadata } from "../../domain/server/metadata";
import type { SharedServerMetadata, SharedServerMetadataResource } from "../../domain/server/metadata";
import type { Thread } from "../../domain/threads/model";
import { StaleExecutionRuntimeError } from "../../shared/runtime/execution-runtime-lifetime";
import type { AppServerClient } from "../connection/client";
import type { AppServerClientAccessOptions } from "../connection/client-access";
import type { AppServerClientAccess, AppServerClientAccessOptions } from "../connection/client-access";
import type { AppServerExecutionContext } from "../connection/execution-context";
import { runtimeConfigSnapshotFromAppServerConfig } from "../protocol/runtime-config";
import { listModelMetadata } from "../services/catalog";
import { readEffectiveConfig } from "../services/runtime-metadata";
import { listThreads, readThreadPage } from "../services/threads";
import { listThreads, readThreadPage, type ThreadPage } from "../services/threads";
import {
type AppServerQueryContextIdentity as AppServerQueryContext,
activeThreadsQueryKey,
appServerModelsQueryKey,
appServerPermissionProfilesQueryKey,
appServerQueryContextIsComplete,
appServerRateLimitsQueryKey,
appServerRuntimeConfigQueryKey,
appServerSkillsQueryKey,
archivedThreadsQueryKey,
cloneAppServerQueryContextIdentity,
} from "./keys";
type ActiveThreadCursor,
type ActiveThreadData,
activeThreadDataHasMore,
activeThreadsFromData,
applyActiveThreadMutation,
recentActiveThreadsFromData,
} from "./active-thread-inventory";
import { readPermissionProfileMetadataProbe, readRateLimitMetadataProbe, readSkillMetadataProbe } from "./metadata-probes";
import type { ObservedResult, ObservedResultListener } from "./observed-result";
import { cloneModelMetadata, cloneSharedServerMetadata, cloneThreads } from "./snapshots";
import type { ObservedPaginatedResult, ObservedPaginatedResultListener, ObservedResult, ObservedResultListener } from "./observed-result";
import { cloneModelMetadata, cloneSharedServerMetadata, cloneSharedServerMetadataResource, cloneThreads } from "./snapshots";
import { applyThreadListMutation, type ThreadListKind, type ThreadListMutation } from "./thread-list-mutation";
const THREAD_LIST_STALE_TIME_MS = 10_000;
const APP_SERVER_METADATA_STALE_TIME_MS = 10_000;
const MODELS_STALE_TIME_MS = 60_000;
const APP_SERVER_QUERY_GC_TIME_MS = 5 * 60_000;
const FULL_ACTIVE_THREAD_FETCH_ATTEMPTS = 2;
export interface AppServerQueryClientRunner {
runWithClient<T>(
context: AppServerQueryContext,
operation: (client: AppServerClient) => Promise<T>,
options?: AppServerClientAccessOptions,
): Promise<T>;
}
const ACTIVE_THREADS_QUERY_KEY = ["threads", "active"] as const;
const ACTIVE_THREAD_SEARCH_INVENTORY_QUERY_KEY = ["threads", "active-search-inventory"] as const;
const ARCHIVED_THREADS_QUERY_KEY = ["threads", "archived"] as const;
const MODELS_QUERY_KEY = ["models"] as const;
const RUNTIME_CONFIG_QUERY_KEY = ["runtime-config"] as const;
const SKILLS_QUERY_KEY = ["skills"] as const;
const PERMISSION_PROFILES_QUERY_KEY = ["permission-profiles"] as const;
const RATE_LIMITS_QUERY_KEY = ["rate-limits"] as const;
interface AppServerQueryOptions<T> {
readonly queryKey: readonly unknown[];
readonly queryFn: () => Promise<T>;
readonly staleTime: number;
readonly queryFn: (context: { signal: AbortSignal }) => Promise<T>;
readonly staleTime?: number;
}
type ThreadListKind = "active" | "archived";
type ActiveThreadsQueryKey = typeof ACTIVE_THREADS_QUERY_KEY;
interface MetadataResourceSnapshot<T> {
readonly value: T;
readonly probe: DiagnosticProbeResult;
}
type MetadataResourceKind = "skills" | "permissionProfiles" | "rateLimits";
type MetadataNotificationResourceKind = "skills" | "rateLimits";
type MetadataResourceValue = readonly SkillMetadata[] | readonly RuntimePermissionProfileSummary[] | RateLimitSnapshot | null;
interface MetadataNotificationRefresh {
dirty: boolean;
readonly generation: number;
promise: Promise<void>;
}
export class AppServerQueryCache {
private readonly context: AppServerQueryContext;
private readonly context: Readonly<AppServerExecutionContext>;
private readonly client: QueryClient;
private readonly clientRunner: AppServerQueryClientRunner | null;
private activeThreadCursorKnown = false;
private activeThreadCursor: string | null = null;
private activeThreadRevision = 0;
private metadataRefreshCount = 0;
private readonly metadataProjectionListeners = new Set<() => void>();
private readonly metadataResourceFetches = new Map<MetadataResourceKind, Set<Promise<void>>>();
private readonly metadataNotificationRefreshes = new Map<MetadataNotificationResourceKind, MetadataNotificationRefresh>();
private generation = 0;
private readonly clientAccess: AppServerClientAccess;
private readonly observerUnsubscribes = new Set<() => void>();
private disposed = false;
constructor(context: AppServerQueryContext, options: { client?: QueryClient; clientRunner?: AppServerQueryClientRunner } = {}) {
this.context = cloneAppServerQueryContextIdentity(context);
this.client = options.client ?? createAppServerQueryClient();
this.clientRunner = options.clientRunner ?? null;
constructor(context: AppServerExecutionContext, clientAccess: AppServerClientAccess) {
this.context = Object.freeze({ ...context });
this.client = createAppServerQueryClient();
this.clientAccess = clientAccess;
}
dispose(): void {
if (this.disposed) return;
this.disposed = true;
this.generation += 1;
this.activeThreadCursorKnown = false;
this.activeThreadCursor = null;
this.activeThreadRevision = 0;
this.metadataRefreshCount = 0;
this.metadataProjectionListeners.clear();
this.metadataResourceFetches.clear();
this.metadataNotificationRefreshes.clear();
for (const unsubscribe of [...this.observerUnsubscribes]) unsubscribe();
this.observerUnsubscribes.clear();
this.client.clear();
}
activeThreadsSnapshot(): readonly Thread[] | null {
if (this.disposed) return null;
return this.threadListSnapshot("active");
const data = this.client.getQueryData<ActiveThreadData>(ACTIVE_THREADS_QUERY_KEY);
const threads = activeThreadsFromData(data);
return threads ? cloneThreads(threads) : null;
}
recentActiveThreadsSnapshot(): readonly Thread[] | null {
if (this.disposed) return null;
const data = this.client.getQueryData<ActiveThreadData>(ACTIVE_THREADS_QUERY_KEY);
const threads = recentActiveThreadsFromData(data);
return threads ? cloneThreads(threads) : null;
}
archivedThreadsSnapshot(): readonly Thread[] | null {
@ -114,102 +107,163 @@ export class AppServerQueryCache {
return this.threadListSnapshot("archived");
}
observeActiveThreadsResult(listener: ObservedResultListener<readonly Thread[]>, options: { emitCurrent?: boolean } = {}): () => void {
observeActiveThreadsResult(
listener: ObservedPaginatedResultListener<readonly Thread[]>,
options: { emitCurrent?: boolean } = {},
): () => void {
this.assertUsable();
return this.observeQueryResult(this.threadListQueryOptions("active"), cloneThreads, listener, options);
const observer = new InfiniteQueryObserver(this.client, {
...this.activeThreadsQueryOptions(),
enabled: false,
});
const emit = (result: InfiniteQueryObserverResult<ActiveThreadData>): void => {
if (!this.disposed) listener(this.projectObservedActiveThreadsResult(result));
};
const unsubscribe = observer.subscribe(emit);
if (options.emitCurrent ?? true) emit(observer.getCurrentResult());
return this.trackObserver(() => {
unsubscribe();
observer.destroy();
});
}
observeArchivedThreadsResult(listener: ObservedResultListener<readonly Thread[]>, options: { emitCurrent?: boolean } = {}): () => void {
this.assertUsable();
return this.observeQueryResult(this.threadListQueryOptions("archived"), cloneThreads, listener, options);
return this.observeQueryResult(this.archivedThreadsQueryOptions(), cloneThreads, listener, options);
}
async fetchActiveThreads(options: { force?: boolean } = {}): Promise<readonly Thread[]> {
this.assertUsable();
return this.fetchThreadList("active", options);
fetchActiveThreads(options: { force?: boolean } = {}): Promise<readonly Thread[]> {
return this.runWhileActive(async () => {
const key = ACTIVE_THREADS_QUERY_KEY;
if (options.force) {
if (this.client.getQueryState(key)?.fetchMeta?.fetchMore?.direction === "forward") {
await this.client.cancelQueries({ queryKey: key, exact: true });
}
await this.client.invalidateQueries({ queryKey: key, refetchType: "none" });
this.assertUsable();
}
return this.readThroughQueryCancellation(
async () => {
const data = await this.client.fetchInfiniteQuery(this.activeThreadsQueryOptions());
return cloneThreads(activeThreadsFromData(data) ?? []);
},
() => this.activeThreadsSnapshot(),
);
});
}
async fetchArchivedThreads(options: { force?: boolean } = {}): Promise<readonly Thread[]> {
this.assertUsable();
return this.fetchThreadList("archived", options);
}
async refreshActiveThreads(): Promise<readonly Thread[]> {
refreshActiveThreads(): Promise<readonly Thread[]> {
return this.fetchActiveThreads({ force: true });
}
async fetchAllActiveThreads(): Promise<readonly Thread[]> {
this.assertUsable();
if (!appServerQueryContextIsComplete(this.context)) return [];
const snapshot = this.activeThreadsSnapshot();
if (snapshot && this.activeThreadCursorKnown && !this.activeThreadCursor) return snapshot;
for (let attempt = 0; attempt < FULL_ACTIVE_THREAD_FETCH_ATTEMPTS; attempt += 1) {
const revision = this.activeThreadRevision;
const threads = await this.runWithClient((client) => listThreads(client, this.context.vaultPath));
if (this.activeThreadRevision !== revision) continue;
this.storeThreadList("active", threads);
this.rememberActiveThreadCursor(null);
return cloneThreads(threads);
}
throw new Error("Active thread inventory changed while it was being fetched.");
fetchActiveThreadSearchInventory(): Promise<readonly Thread[]> {
return this.runWhileActive(async () => {
const key = ACTIVE_THREAD_SEARCH_INVENTORY_QUERY_KEY;
const options = {
queryKey: key,
queryFn: ({ signal }: { signal: AbortSignal }) =>
this.runWithClient((client) => listThreads(client, this.context.vaultPath, { signal })),
};
return this.readFreshThroughQueryCancellation(key, async () => cloneThreads(await this.client.fetchQuery(options)));
});
}
hasMoreActiveThreads(): boolean {
if (this.disposed) return false;
if (!appServerQueryContextIsComplete(this.context)) return false;
return Boolean(this.activeThreadCursor);
return activeThreadDataHasMore(this.client.getQueryData<ActiveThreadData>(ACTIVE_THREADS_QUERY_KEY));
}
async loadMoreActiveThreads(): Promise<readonly Thread[]> {
this.assertUsable();
if (!appServerQueryContextIsComplete(this.context)) return [];
const current = this.activeThreadsSnapshot() ?? (await this.fetchActiveThreads());
const cursor = this.activeThreadCursor;
if (!cursor) return current;
const revision = this.activeThreadRevision;
const page = await this.runWithClient((client) => readThreadPage(client, this.context.vaultPath, { cursor, archived: false }));
if (page.nextCursor === cursor) throw new Error("Codex app-server returned a repeated thread list cursor.");
if (this.activeThreadRevision !== revision) return this.activeThreadsSnapshot() ?? current;
const latest = this.activeThreadsSnapshot() ?? current;
const existingIds = new Set(latest.map((thread) => thread.id));
const threads = [...latest, ...page.threads.filter((thread) => !existingIds.has(thread.id))];
this.storeThreadList("active", threads);
this.rememberActiveThreadCursor(page.nextCursor);
return cloneThreads(threads);
loadMoreActiveThreads(): Promise<readonly Thread[]> {
return this.runWhileActive(async () => {
const current = this.activeThreadsSnapshot() ?? (await this.fetchActiveThreads());
const observer = new InfiniteQueryObserver(this.client, {
...this.activeThreadsQueryOptions(),
enabled: false,
});
try {
if (!observer.getCurrentResult().hasNextPage) return current;
const result = await observer.fetchNextPage({ cancelRefetch: false, throwOnError: true });
this.assertUsable();
return result.data ? cloneThreads(activeThreadsFromData(result.data) ?? []) : current;
} catch (error) {
if (error instanceof CancelledError) return this.activeThreadsSnapshot() ?? current;
throw error;
} finally {
observer.destroy();
}
});
}
async refreshArchivedThreads(): Promise<readonly Thread[]> {
refreshArchivedThreads(): Promise<readonly Thread[]> {
return this.fetchArchivedThreads({ force: true });
}
private threadListSnapshot(kind: ThreadListKind): readonly Thread[] | null {
if (!appServerQueryContextIsComplete(this.context)) return null;
const threads = this.client.getQueryData<readonly Thread[]>(this.threadListQueryKey(kind));
return threads ? cloneThreads(threads) : null;
fetchArchivedThreads(options: { force?: boolean } = {}): Promise<readonly Thread[]> {
return this.runWhileActive(async () => {
const key = ARCHIVED_THREADS_QUERY_KEY;
if (options.force) {
await this.client.invalidateQueries({ queryKey: key, refetchType: "none" });
this.assertUsable();
}
return this.readFreshThroughQueryCancellation(key, async () =>
cloneThreads(await this.client.fetchQuery(this.archivedThreadsQueryOptions())),
);
});
}
private async fetchThreadList(kind: ThreadListKind, options: { force?: boolean } = {}): Promise<readonly Thread[]> {
if (!appServerQueryContextIsComplete(this.context)) return [];
const key = this.threadListQueryKey(kind);
if (options.force) {
await this.client.invalidateQueries({ queryKey: key });
this.assertUsable();
}
const threads = await this.client.fetchQuery(this.threadListQueryOptions(kind));
applyThreadListMutations(mutations: readonly ThreadListMutation[]): void {
this.assertUsable();
return cloneThreads(threads);
if (mutations.length === 0) return;
const activeMutations = mutations.filter((mutation) => mutation.list === "active");
if (activeMutations.length > 0) {
const key = ACTIVE_THREADS_QUERY_KEY;
const wasFetching = this.client.getQueryState(key)?.fetchStatus === "fetching";
const fetchIsNextPage = this.client.getQueryState(key)?.fetchMeta?.fetchMore?.direction === "forward";
void this.client.cancelQueries({ queryKey: key, exact: true });
const before = this.client.getQueryData<ActiveThreadData>(key);
const after = activeMutations.reduce<ActiveThreadData | undefined>(applyActiveThreadMutation, before);
if (after !== before) this.client.setQueryData(key, after);
void this.client.invalidateQueries({ queryKey: key, refetchType: "none" });
const searchKey = ACTIVE_THREAD_SEARCH_INVENTORY_QUERY_KEY;
void this.client.cancelQueries({ queryKey: searchKey, exact: true });
void this.client.invalidateQueries({ queryKey: searchKey, refetchType: "none" });
const refreshRequested = activeMutations.some((mutation) => mutation.kind === "refresh");
if ((wasFetching && !fetchIsNextPage) || (refreshRequested && before)) {
void this.fetchActiveThreads({ force: true }).catch(() => {
// Query observers retain refresh failures while the event projection remains last-known-good state.
});
}
}
const archivedMutations = mutations.filter((mutation) => mutation.list === "archived");
if (archivedMutations.length > 0) {
const key = ARCHIVED_THREADS_QUERY_KEY;
const wasFetching = this.client.getQueryState(key)?.fetchStatus === "fetching";
void this.client.cancelQueries({ queryKey: key, exact: true });
const before = this.archivedThreadsSnapshot();
const after = archivedMutations.reduce<readonly Thread[] | null>(applyThreadListMutation, before);
if (after !== before && after) this.client.setQueryData(key, cloneThreads(after));
void this.client.invalidateQueries({ queryKey: key, refetchType: "none" });
const refreshRequested = archivedMutations.some((mutation) => mutation.kind === "refresh");
if (wasFetching || (refreshRequested && before)) {
void this.fetchArchivedThreads({ force: true }).catch(() => {
// Query observers retain refresh failures while the event projection remains last-known-good state.
});
}
}
}
private storeThreadList(kind: ThreadListKind, threads: readonly Thread[]): void {
if (!appServerQueryContextIsComplete(this.context)) return;
this.client.setQueryData(this.threadListQueryKey(kind), cloneThreads(threads));
if (kind === "active") this.bumpActiveThreadRevision();
private threadListSnapshot(kind: ThreadListKind): readonly Thread[] | null {
if (kind === "active") return this.activeThreadsSnapshot();
const threads = this.client.getQueryData<readonly Thread[]>(ARCHIVED_THREADS_QUERY_KEY);
return threads ? cloneThreads(threads) : null;
}
appServerMetadataSnapshot(): SharedServerMetadata | null {
if (this.disposed) return null;
if (!appServerQueryContextIsComplete(this.context)) return null;
const runtimeConfig = this.client.getQueryData<RuntimeConfigSnapshot>(appServerRuntimeConfigQueryKey(this.context));
const runtimeConfig = this.client.getQueryData<RuntimeConfigSnapshot>(RUNTIME_CONFIG_QUERY_KEY);
if (!runtimeConfig) return null;
const skills = this.metadataResourceState("skills");
const permissionProfiles = this.metadataResourceState("permissionProfiles");
@ -227,95 +281,112 @@ export class AppServerQueryCache {
});
}
observeAppServerMetadataResult(
listener: ObservedResultListener<SharedServerMetadata>,
observeAppServerMetadataResources(
listener: (resource: SharedServerMetadataResource) => void,
options: { emitCurrent?: boolean } = {},
): () => void {
this.assertUsable();
const generation = this.generation;
const observers = [
new QueryObserver(this.client, { ...this.runtimeConfigQueryOptions(), enabled: false }),
new QueryObserver(this.client, { ...this.skillsQueryOptions(), enabled: false }),
new QueryObserver(this.client, { ...this.permissionProfilesQueryOptions(), enabled: false }),
new QueryObserver(this.client, { ...this.rateLimitsQueryOptions(), enabled: false }),
new QueryObserver(this.client, { ...this.modelsQueryOptions(), enabled: false }),
let unsubscribed = false;
const emit = (resource: SharedServerMetadataResource): void => {
if (!this.disposed && !unsubscribed) listener(cloneSharedServerMetadataResource(resource));
};
const runtimeConfig = new QueryObserver(this.client, { ...this.runtimeConfigQueryOptions(), enabled: false });
const models = new QueryObserver(this.client, { ...this.modelsQueryOptions(), enabled: false });
const skills = new QueryObserver(this.client, { ...this.skillsQueryOptions(), enabled: false });
const permissionProfiles = new QueryObserver(this.client, { ...this.permissionProfilesQueryOptions(), enabled: false });
const rateLimits = new QueryObserver(this.client, { ...this.rateLimitsQueryOptions(), enabled: false });
const emitRuntimeConfig = (result: QueryObserverResult<RuntimeConfigSnapshot>, includeFetching = false): void => {
if (result.isFetching && !includeFetching) return;
emit({ id: "runtimeConfig", value: result.data ? cloneRuntimeConfigSnapshot(result.data) : undefined });
};
const emitModels = (result: QueryObserverResult<readonly ModelMetadata[]>, includeFetching = false): void => {
if (result.isFetching && !includeFetching) return;
emit({ id: "models", value: result.data ? cloneModelMetadata(result.data) : undefined, probe: this.modelsProbe() });
};
const emitSkills = (result: QueryObserverResult<MetadataResourceSnapshot<readonly SkillMetadata[]>>, includeFetching = false): void => {
if (result.isFetching && !includeFetching) return;
const state = this.metadataResourceState("skills");
emit({ id: "skills", value: state.value ?? undefined, probe: state.probe });
};
const emitPermissionProfiles = (
result: QueryObserverResult<MetadataResourceSnapshot<readonly RuntimePermissionProfileSummary[]>>,
includeFetching = false,
): void => {
if (result.isFetching && !includeFetching) return;
const state = this.metadataResourceState("permissionProfiles");
emit({ id: "permissionProfiles", value: state.value ?? undefined, probe: state.probe });
};
const emitRateLimits = (
result: QueryObserverResult<MetadataResourceSnapshot<RateLimitSnapshot | null>>,
includeFetching = false,
): void => {
if (result.isFetching && !includeFetching) return;
const state = this.metadataResourceState("rateLimits");
emit({ id: "rateLimits", value: state.value, probe: state.probe });
};
const unsubscribers = [
runtimeConfig.subscribe(emitRuntimeConfig),
models.subscribe(emitModels),
skills.subscribe(emitSkills),
permissionProfiles.subscribe(emitPermissionProfiles),
rateLimits.subscribe(emitRateLimits),
];
let queued = false;
let disposed = false;
const emit = (): void => {
queued = false;
if (disposed || generation !== this.generation) return;
const metadata = this.appServerMetadataSnapshot();
const runtimeState = this.client.getQueryState(appServerRuntimeConfigQueryKey(this.context));
listener({
value: metadata,
error: runtimeState?.error instanceof Error ? runtimeState.error : null,
isFetching: observers.some((observer) => observer.getCurrentResult().isFetching),
});
};
const schedule = (): void => {
if (disposed || generation !== this.generation) return;
if (this.metadataRefreshCount > 0 || queued) return;
queued = true;
queueMicrotask(emit);
};
this.metadataProjectionListeners.add(schedule);
const unsubscribers = observers.map((observer) => observer.subscribe(schedule));
if (options.emitCurrent ?? true) emit();
return () => {
disposed = true;
if (options.emitCurrent ?? true) {
emitRuntimeConfig(runtimeConfig.getCurrentResult(), true);
emitModels(models.getCurrentResult(), true);
emitSkills(skills.getCurrentResult(), true);
emitPermissionProfiles(permissionProfiles.getCurrentResult(), true);
emitRateLimits(rateLimits.getCurrentResult(), true);
}
return this.trackObserver(() => {
unsubscribed = true;
for (const unsubscribe of unsubscribers) unsubscribe();
this.metadataProjectionListeners.delete(schedule);
};
runtimeConfig.destroy();
models.destroy();
skills.destroy();
permissionProfiles.destroy();
rateLimits.destroy();
});
}
async refreshAppServerMetadata(): Promise<SharedServerMetadata | null> {
this.assertUsable();
if (!appServerQueryContextIsComplete(this.context)) return null;
return this.runMetadataRefresh(async () => {
const runtimeResult = this.fetchRuntimeConfig(true).then(
refreshAppServerMetadata(): Promise<void> {
return this.runWhileActive(async () => {
const runtimeResult = this.fetchRuntimeConfig().then(
() => ({ ok: true as const }),
(error: unknown) => ({ ok: false as const, error }),
);
const [, runtime] = await Promise.all([
Promise.allSettled([
this.fetchMetadataResource("skills", true),
this.fetchMetadataResource("permissionProfiles", true),
this.fetchMetadataResource("rateLimits", true),
this.fetchMetadataResource("skills"),
this.fetchMetadataResource("permissionProfiles"),
this.fetchMetadataResource("rateLimits"),
this.fetchModels({ force: true }),
]),
runtimeResult,
]);
this.assertUsable();
if (!runtime.ok) throw runtime.error;
return this.appServerMetadataSnapshot();
});
}
async refreshSkills(): Promise<SharedServerMetadata | null> {
this.assertUsable();
if (!appServerQueryContextIsComplete(this.context)) return null;
return this.runMetadataRefresh(async () => {
const current = await this.refreshMetadataResourceNotification("skills");
refreshSkills(): Promise<void> {
return this.runWhileActive(async () => {
await this.refreshNotifiedMetadataResource("skills");
this.assertUsable();
return current ? this.appServerMetadataSnapshot() : null;
});
}
async refreshRateLimits(): Promise<SharedServerMetadata | null> {
this.assertUsable();
if (!appServerQueryContextIsComplete(this.context)) return null;
return this.runMetadataRefresh(async () => {
const current = await this.refreshMetadataResourceNotification("rateLimits");
refreshRateLimits(): Promise<void> {
return this.runWhileActive(async () => {
await this.refreshNotifiedMetadataResource("rateLimits");
this.assertUsable();
return current ? this.appServerMetadataSnapshot() : null;
});
}
modelsSnapshot(): readonly ModelMetadata[] | null {
if (this.disposed) return null;
if (!appServerQueryContextIsComplete(this.context)) return null;
const models = this.client.getQueryData<readonly ModelMetadata[]>(appServerModelsQueryKey(this.context));
const models = this.client.getQueryData<readonly ModelMetadata[]>(MODELS_QUERY_KEY);
return models ? cloneModelMetadata(models) : null;
}
@ -324,167 +395,125 @@ export class AppServerQueryCache {
return this.observeQueryResult(this.modelsQueryOptions(), cloneModelMetadata, listener, options);
}
async fetchModels(options: { force?: boolean } = {}): Promise<readonly ModelMetadata[]> {
this.assertUsable();
if (!appServerQueryContextIsComplete(this.context)) return [];
const key = appServerModelsQueryKey(this.context);
if (options.force) {
await this.client.invalidateQueries({ queryKey: key });
fetchModels(options: { force?: boolean } = {}): Promise<readonly ModelMetadata[]> {
return this.runWhileActive(async () => {
const key = MODELS_QUERY_KEY;
if (options.force) {
await this.client.invalidateQueries({ queryKey: key, refetchType: "none" });
this.assertUsable();
}
const models = await this.client.fetchQuery(this.modelsQueryOptions());
this.assertUsable();
}
const models = await this.client.fetchQuery(this.modelsQueryOptions());
this.assertUsable();
return cloneModelMetadata(models);
return cloneModelMetadata(models);
});
}
async refreshModels(): Promise<readonly ModelMetadata[]> {
refreshModels(): Promise<readonly ModelMetadata[]> {
return this.fetchModels({ force: true });
}
private threadListQueryOptions(kind: ThreadListKind): AppServerQueryOptions<readonly Thread[]> {
private activeThreadsQueryOptions(): InfiniteQueryObserverOptions<
ThreadPage,
Error,
ActiveThreadData,
ActiveThreadsQueryKey,
ActiveThreadCursor
> {
return {
queryKey: this.threadListQueryKey(kind),
queryFn: async (): Promise<readonly Thread[]> => {
if (kind === "active") {
const revision = this.activeThreadRevision;
const page = await this.runWithClient((client) => readThreadPage(client, this.context.vaultPath, { archived: false }));
if (this.activeThreadRevision !== revision) return this.activeThreadsSnapshot() ?? [];
this.rememberActiveThreadCursor(page.nextCursor);
return cloneThreads(page.threads);
}
return cloneThreads(await this.runWithClient((client) => listThreads(client, this.context.vaultPath, { archived: true })));
queryKey: ACTIVE_THREADS_QUERY_KEY,
queryFn: async ({ pageParam, signal }) => {
signal.throwIfAborted();
const page = await this.runWithClient((client) =>
readThreadPage(client, this.context.vaultPath, { cursor: pageParam, archived: false }),
);
signal.throwIfAborted();
return page;
},
staleTime: THREAD_LIST_STALE_TIME_MS,
initialPageParam: null,
getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined,
staleTime: Number.POSITIVE_INFINITY,
};
}
private threadListQueryKey(kind: ThreadListKind): ReturnType<typeof activeThreadsQueryKey> | ReturnType<typeof archivedThreadsQueryKey> {
return kind === "archived" ? archivedThreadsQueryKey(this.context) : activeThreadsQueryKey(this.context);
private archivedThreadsQueryOptions(): AppServerQueryOptions<readonly Thread[]> {
return {
queryKey: ARCHIVED_THREADS_QUERY_KEY,
queryFn: ({ signal }: { signal: AbortSignal }) =>
this.runWithClient((client) => listThreads(client, this.context.vaultPath, { archived: true, signal })).then(cloneThreads),
staleTime: Number.POSITIVE_INFINITY,
};
}
private runtimeConfigQueryOptions(): AppServerQueryOptions<RuntimeConfigSnapshot> {
return {
queryKey: appServerRuntimeConfigQueryKey(this.context),
queryKey: RUNTIME_CONFIG_QUERY_KEY,
queryFn: async (): Promise<RuntimeConfigSnapshot> =>
this.runWithClient(async (client) =>
runtimeConfigSnapshotFromAppServerConfig(await readEffectiveConfig(client, this.context.vaultPath)),
),
staleTime: APP_SERVER_METADATA_STALE_TIME_MS,
};
}
private skillsQueryOptions(forceReload = false): AppServerQueryOptions<MetadataResourceSnapshot<readonly SkillMetadata[]>> {
private skillsQueryOptions(): AppServerQueryOptions<MetadataResourceSnapshot<readonly SkillMetadata[]>> {
return {
queryKey: appServerSkillsQueryKey(this.context),
queryKey: SKILLS_QUERY_KEY,
queryFn: async () =>
this.runWithClient(async (client) =>
successfulMetadataResource(await readSkillMetadataProbe(client, this.context.vaultPath, forceReload)),
),
staleTime: APP_SERVER_METADATA_STALE_TIME_MS,
this.runWithClient(async (client) => successfulMetadataResource(await readSkillMetadataProbe(client, this.context.vaultPath))),
};
}
private permissionProfilesQueryOptions(): AppServerQueryOptions<MetadataResourceSnapshot<readonly RuntimePermissionProfileSummary[]>> {
return {
queryKey: appServerPermissionProfilesQueryKey(this.context),
queryKey: PERMISSION_PROFILES_QUERY_KEY,
queryFn: async () =>
this.runWithClient(async (client) =>
successfulMetadataResource(await readPermissionProfileMetadataProbe(client, this.context.vaultPath)),
),
staleTime: APP_SERVER_METADATA_STALE_TIME_MS,
};
}
private rateLimitsQueryOptions(): AppServerQueryOptions<MetadataResourceSnapshot<RateLimitSnapshot | null>> {
return {
queryKey: appServerRateLimitsQueryKey(this.context),
queryKey: RATE_LIMITS_QUERY_KEY,
queryFn: async () => this.runWithClient(async (client) => successfulMetadataResource(await readRateLimitMetadataProbe(client))),
staleTime: APP_SERVER_METADATA_STALE_TIME_MS,
};
}
private async fetchRuntimeConfig(force: boolean): Promise<RuntimeConfigSnapshot> {
const key = appServerRuntimeConfigQueryKey(this.context);
if (force) {
await this.client.invalidateQueries({ queryKey: key });
this.assertUsable();
}
private async fetchRuntimeConfig(): Promise<RuntimeConfigSnapshot> {
const runtimeConfig = await this.client.fetchQuery(this.runtimeConfigQueryOptions());
this.assertUsable();
return cloneRuntimeConfigSnapshot(runtimeConfig);
}
private fetchMetadataResource(resource: MetadataResourceKind, force: boolean, reloadSkills = false): Promise<void> {
const refresh = (async (): Promise<void> => {
private fetchMetadataResource(resource: MetadataResourceKind): Promise<void> {
return (async (): Promise<void> => {
if (resource === "skills") {
const options = this.skillsQueryOptions(reloadSkills);
if (force) {
await this.client.invalidateQueries({ queryKey: options.queryKey });
this.assertUsable();
}
const options = this.skillsQueryOptions();
await this.client.fetchQuery(options);
this.assertUsable();
return;
}
if (resource === "permissionProfiles") {
const options = this.permissionProfilesQueryOptions();
if (force) {
await this.client.invalidateQueries({ queryKey: options.queryKey });
this.assertUsable();
}
await this.client.fetchQuery(options);
this.assertUsable();
return;
}
const options = this.rateLimitsQueryOptions();
if (force) {
await this.client.invalidateQueries({ queryKey: options.queryKey });
this.assertUsable();
}
await this.client.fetchQuery(options);
this.assertUsable();
})();
const active = this.metadataResourceFetches.get(resource) ?? new Set<Promise<void>>();
active.add(refresh);
this.metadataResourceFetches.set(resource, active);
const cleanup = (): void => {
active.delete(refresh);
if (active.size === 0 && this.metadataResourceFetches.get(resource) === active) this.metadataResourceFetches.delete(resource);
};
void refresh.then(cleanup, cleanup);
return refresh;
}
private refreshMetadataResourceNotification(resource: MetadataNotificationResourceKind): Promise<boolean> {
const generation = this.generation;
const current = this.metadataNotificationRefreshes.get(resource);
if (current?.generation === generation) {
current.dirty = true;
return current.promise.then(() => generation === this.generation);
private async refreshNotifiedMetadataResource(resource: "skills" | "rateLimits"): Promise<void> {
const queryKey = resource === "skills" ? SKILLS_QUERY_KEY : RATE_LIMITS_QUERY_KEY;
await this.client.cancelQueries({ queryKey, exact: true });
this.assertUsable();
try {
await this.fetchMetadataResource(resource);
} catch (error) {
if (!(error instanceof CancelledError)) throw error;
}
const activeFetches = [...(this.metadataResourceFetches.get(resource) ?? [])];
const refresh: MetadataNotificationRefresh = {
dirty: false,
generation,
promise: Promise.resolve(),
};
refresh.promise = (async (): Promise<void> => {
if (activeFetches.length > 0) await Promise.allSettled(activeFetches);
if (generation !== this.generation) return;
for (;;) {
refresh.dirty = false;
await this.fetchMetadataResource(resource, true, resource === "skills").catch(() => undefined);
if (generation !== this.generation) return;
if (!metadataNotificationRefreshIsDirty(refresh)) return;
}
})();
this.metadataNotificationRefreshes.set(resource, refresh);
const cleanup = (): void => {
if (this.metadataNotificationRefreshes.get(resource) === refresh) this.metadataNotificationRefreshes.delete(resource);
};
void refresh.promise.then(cleanup, cleanup);
return refresh.promise.then(() => generation === this.generation);
}
private metadataResourceState(resource: "skills"): { value: readonly SkillMetadata[] | null; probe: DiagnosticProbeResult };
@ -495,11 +524,7 @@ export class AppServerQueryCache {
private metadataResourceState(resource: "rateLimits"): { value: RateLimitSnapshot | null; probe: DiagnosticProbeResult };
private metadataResourceState(resource: MetadataResourceKind): { value: MetadataResourceValue; probe: DiagnosticProbeResult } {
const key =
resource === "skills"
? appServerSkillsQueryKey(this.context)
: resource === "permissionProfiles"
? appServerPermissionProfilesQueryKey(this.context)
: appServerRateLimitsQueryKey(this.context);
resource === "skills" ? SKILLS_QUERY_KEY : resource === "permissionProfiles" ? PERMISSION_PROFILES_QUERY_KEY : RATE_LIMITS_QUERY_KEY;
const state = this.client.getQueryState<MetadataResourceSnapshot<MetadataResourceValue>>(key);
const failedProbe = diagnosticProbeFromError(state?.error);
return {
@ -509,7 +534,7 @@ export class AppServerQueryCache {
}
private modelsProbe(): DiagnosticProbeResult {
const state = this.client.getQueryState<readonly ModelMetadata[]>(appServerModelsQueryKey(this.context));
const state = this.client.getQueryState<readonly ModelMetadata[]>(MODELS_QUERY_KEY);
return (
diagnosticProbeFromError(state?.error) ??
(state?.data
@ -518,22 +543,9 @@ export class AppServerQueryCache {
);
}
private async runMetadataRefresh<T>(operation: () => Promise<T>): Promise<T> {
const generation = this.generation;
this.metadataRefreshCount += 1;
try {
return await operation();
} finally {
if (generation === this.generation) {
this.metadataRefreshCount -= 1;
if (this.metadataRefreshCount === 0) for (const listener of this.metadataProjectionListeners) listener();
}
}
}
private modelsQueryOptions(): AppServerQueryOptions<readonly ModelMetadata[]> {
return {
queryKey: appServerModelsQueryKey(this.context),
queryKey: MODELS_QUERY_KEY,
queryFn: async (): Promise<readonly ModelMetadata[]> => {
try {
return cloneModelMetadata(
@ -549,68 +561,107 @@ export class AppServerQueryCache {
};
}
private observeQueryResult<T>(
queryOptions: AppServerQueryOptions<T>,
clone: (value: T) => T,
listener: ObservedResultListener<T>,
private observeQueryResult<TQuery, TValue>(
queryOptions: AppServerQueryOptions<TQuery>,
project: (value: TQuery) => TValue,
listener: ObservedResultListener<TValue>,
options: { emitCurrent?: boolean },
): () => void {
const observer = new QueryObserver<T>(this.client, {
const observer = new QueryObserver<TQuery>(this.client, {
...queryOptions,
enabled: false,
});
const emit = (result: QueryObserverResult<T>): void => {
listener(this.cloneObservedResult(result, clone));
const emit = (result: QueryObserverResult<TQuery>): void => {
if (!this.disposed) listener(this.projectObservedResult(result, project));
};
const unsubscribe = observer.subscribe(emit);
if (options.emitCurrent ?? true) emit(observer.getCurrentResult());
return unsubscribe;
return this.trackObserver(() => {
unsubscribe();
observer.destroy();
});
}
private cloneObservedResult<T>(result: QueryObserverResult<T>, clone: (value: T) => T): ObservedResult<T> {
private projectObservedResult<TQuery, TValue>(
result: QueryObserverResult<TQuery>,
project: (value: TQuery) => TValue,
): ObservedResult<TValue> {
return {
value: result.data === undefined ? null : clone(result.data),
value: result.data === undefined ? null : project(result.data),
error: result.error instanceof Error ? result.error : null,
isFetching: result.isFetching,
};
}
private projectObservedActiveThreadsResult(
result: InfiniteQueryObserverResult<ActiveThreadData>,
): ObservedPaginatedResult<readonly Thread[]> {
const threads = activeThreadsFromData(result.data);
return {
value: threads ? cloneThreads(threads) : null,
error: result.error instanceof Error ? result.error : null,
isFetching: result.isFetching,
hasMore: result.hasNextPage,
isFetchingNextPage: result.isFetchingNextPage,
};
}
private runWithClient<T>(operation: (client: AppServerClient) => Promise<T>, options: AppServerClientAccessOptions = {}): Promise<T> {
this.assertUsable();
const generation = this.generation;
const runner = this.clientRunner;
if (!runner) throw new Error("Codex app-server query client runner is not configured.");
return runner.runWithClient(this.context, operation, options).then(
(result) => {
if (this.disposed || generation !== this.generation) throw new DisposedAppServerQueryCacheError();
return result;
},
(error: unknown) => {
if (this.disposed || generation !== this.generation) throw new DisposedAppServerQueryCacheError();
throw error;
},
);
return this.clientAccess.withClient(operation, options);
}
private async readThroughQueryCancellation<T>(read: () => Promise<T>, fallback?: () => T | null): Promise<T> {
for (;;) {
try {
const value = await read();
this.assertUsable();
return value;
} catch (error) {
if (!(error instanceof CancelledError)) throw error;
this.assertUsable();
const fallbackValue = fallback?.();
if (fallbackValue != null) return fallbackValue;
}
}
}
private async readFreshThroughQueryCancellation<T>(queryKey: readonly unknown[], read: () => Promise<T>): Promise<T> {
for (;;) {
const value = await this.readThroughQueryCancellation(read);
this.assertUsable();
if (!this.client.getQueryState(queryKey)?.isInvalidated) return value;
}
}
private assertUsable(): void {
if (this.disposed) throw new DisposedAppServerQueryCacheError();
if (this.disposed) throw new StaleExecutionRuntimeError();
}
private rememberActiveThreadCursor(cursor: string | null): void {
this.activeThreadCursorKnown = true;
this.activeThreadCursor = cursor;
this.bumpActiveThreadRevision();
private runWhileActive<T>(operation: () => Promise<T>): Promise<T> {
this.assertUsable();
return (async () => {
try {
const result = await operation();
this.assertUsable();
return result;
} catch (error) {
if (this.disposed) throw new StaleExecutionRuntimeError();
throw error;
}
})();
}
private bumpActiveThreadRevision(): void {
this.activeThreadRevision += 1;
}
}
class DisposedAppServerQueryCacheError extends Error {
constructor() {
super("Codex app-server query cache was disposed.");
this.name = "DisposedAppServerQueryCacheError";
private trackObserver(unsubscribe: () => void): () => void {
let subscribed = true;
const trackedUnsubscribe = (): void => {
if (!subscribed) return;
subscribed = false;
this.observerUnsubscribes.delete(trackedUnsubscribe);
unsubscribe();
};
this.observerUnsubscribes.add(trackedUnsubscribe);
return trackedUnsubscribe;
}
}
@ -630,17 +681,12 @@ function diagnosticProbeFromError(error: unknown): DiagnosticProbeResult | null
return error instanceof MetadataResourceQueryError ? error.probe : null;
}
function metadataNotificationRefreshIsDirty(refresh: MetadataNotificationRefresh): boolean {
return refresh.dirty;
}
function createAppServerQueryClient(): QueryClient {
return new QueryClient({
defaultOptions: {
queries: {
gcTime: APP_SERVER_QUERY_GC_TIME_MS,
networkMode: "always",
retry: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
},
mutations: {

View file

@ -1,104 +0,0 @@
export interface AppServerQueryContext {
codexPath: string;
vaultPath: string;
}
export interface AppServerContextLease {
readonly context: Readonly<AppServerQueryContext>;
readonly generation: number;
}
export interface AppServerQueryContextIdentity extends AppServerQueryContext {
readonly generation: number;
}
type AppServerQueryScope = readonly ["app-server", number, string, string];
export type AppServerActiveThreadsQueryKey = readonly [...AppServerQueryScope, "threads", "active"];
export type AppServerArchivedThreadsQueryKey = readonly [...AppServerQueryScope, "threads", "archived"];
export type AppServerModelsQueryKey = readonly [...AppServerQueryScope, "models"];
export type AppServerRuntimeConfigQueryKey = readonly [...AppServerQueryScope, "runtime-config"];
export type AppServerSkillsQueryKey = readonly [...AppServerQueryScope, "skills"];
export type AppServerPermissionProfilesQueryKey = readonly [...AppServerQueryScope, "permission-profiles"];
export type AppServerRateLimitsQueryKey = readonly [...AppServerQueryScope, "rate-limits"];
export function appServerQueryContextIsComplete(context: AppServerQueryContext): boolean {
return nonEmptyString(context.codexPath) && nonEmptyString(context.vaultPath);
}
function cloneAppServerQueryContext(context: AppServerQueryContext): AppServerQueryContext {
return { ...context };
}
export function createAppServerContextLease(context: AppServerQueryContext, generation: number): AppServerContextLease {
return Object.freeze({
context: Object.freeze(cloneAppServerQueryContext(context)),
generation,
});
}
export function appServerQueryContextIdentity(lease: AppServerContextLease): AppServerQueryContextIdentity {
return {
...lease.context,
generation: lease.generation,
};
}
export function cloneAppServerQueryContextIdentity(identity: AppServerQueryContextIdentity): AppServerQueryContextIdentity {
return { ...identity };
}
export function appServerQueryContextRawEquals(left: AppServerQueryContext, right: AppServerQueryContext): boolean {
return left.codexPath === right.codexPath && left.vaultPath === right.vaultPath;
}
function appServerQueryContextMatches(left: AppServerQueryContext, right: AppServerQueryContext): boolean {
return appServerQueryContextIsComplete(left) && appServerQueryContextIsComplete(right) && appServerQueryContextRawEquals(left, right);
}
function appServerQueryContextKey(context: AppServerQueryContext): string {
return `${context.codexPath}\u0000${context.vaultPath}`;
}
export function appServerQueryContextIdentityMatches(left: AppServerQueryContextIdentity, right: AppServerQueryContextIdentity): boolean {
return left.generation === right.generation && appServerQueryContextMatches(left, right);
}
export function appServerQueryContextIdentityKey(identity: AppServerQueryContextIdentity): string {
return `${String(identity.generation)}\u0000${appServerQueryContextKey(identity)}`;
}
function appServerQueryScope(context: AppServerQueryContextIdentity): AppServerQueryScope {
return ["app-server", context.generation, context.codexPath, context.vaultPath];
}
export function activeThreadsQueryKey(context: AppServerQueryContextIdentity): AppServerActiveThreadsQueryKey {
return [...appServerQueryScope(context), "threads", "active"];
}
export function archivedThreadsQueryKey(context: AppServerQueryContextIdentity): AppServerArchivedThreadsQueryKey {
return [...appServerQueryScope(context), "threads", "archived"];
}
export function appServerModelsQueryKey(context: AppServerQueryContextIdentity): AppServerModelsQueryKey {
return [...appServerQueryScope(context), "models"];
}
export function appServerRuntimeConfigQueryKey(context: AppServerQueryContextIdentity): AppServerRuntimeConfigQueryKey {
return [...appServerQueryScope(context), "runtime-config"];
}
export function appServerSkillsQueryKey(context: AppServerQueryContextIdentity): AppServerSkillsQueryKey {
return [...appServerQueryScope(context), "skills"];
}
export function appServerPermissionProfilesQueryKey(context: AppServerQueryContextIdentity): AppServerPermissionProfilesQueryKey {
return [...appServerQueryScope(context), "permission-profiles"];
}
export function appServerRateLimitsQueryKey(context: AppServerQueryContextIdentity): AppServerRateLimitsQueryKey {
return [...appServerQueryScope(context), "rate-limits"];
}
function nonEmptyString(value: string): boolean {
return value.trim().length > 0;
}

View file

@ -16,16 +16,9 @@ export type SkillMetadataProbeResult = MetadataProbeResult<SkillMetadata[], "ski
export type PermissionProfileMetadataProbeResult = MetadataProbeResult<RuntimePermissionProfileSummary[], "permissionProfiles">;
export type RateLimitMetadataProbeResult = MetadataProbeResult<RateLimitSnapshot | null, "rateLimits">;
export async function readSkillMetadataProbe(
client: AppServerRequestClient | null,
vaultPath: string,
forceReload = false,
): Promise<SkillMetadataProbeResult> {
if (!client) {
return { value: [], probe: diagnosticProbeError("skills", new Error("Codex app-server is not connected."), Date.now()) };
}
export async function readSkillMetadataProbe(client: AppServerRequestClient, vaultPath: string): Promise<SkillMetadataProbeResult> {
try {
const catalog = await listSkillCatalog(client, vaultPath, { forceReload });
const catalog = await listSkillCatalog(client, vaultPath);
return { value: catalog.skills, probe: diagnosticProbeOk("skills", `${String(catalog.totalCount)} skills`, Date.now()) };
} catch (error) {
return { value: [], probe: diagnosticProbeError("skills", error, Date.now()) };
@ -33,15 +26,9 @@ export async function readSkillMetadataProbe(
}
export async function readPermissionProfileMetadataProbe(
client: AppServerRequestClient | null,
client: AppServerRequestClient,
vaultPath: string,
): Promise<PermissionProfileMetadataProbeResult> {
if (!client) {
return {
value: [],
probe: diagnosticProbeError("permissionProfiles", new Error("Codex app-server is not connected."), Date.now()),
};
}
try {
const profiles = await listPermissionProfiles(client, vaultPath);
return { value: profiles, probe: diagnosticProbeOk("permissionProfiles", `${String(profiles.length)} profiles`, Date.now()) };
@ -50,13 +37,7 @@ export async function readPermissionProfileMetadataProbe(
}
}
export async function readRateLimitMetadataProbe(client: AppServerRequestClient | null): Promise<RateLimitMetadataProbeResult> {
if (!client) {
return {
value: null,
probe: diagnosticProbeError("rateLimits", new Error("Codex app-server is not connected."), Date.now()),
};
}
export async function readRateLimitMetadataProbe(client: AppServerRequestClient): Promise<RateLimitMetadataProbeResult> {
try {
const response = await readAccountRateLimits(client);
return {

View file

@ -6,6 +6,13 @@ export interface ObservedResult<T> {
export type ObservedResultListener<T> = (result: ObservedResult<T>) => void;
export interface ObservedPaginatedResult<T> extends ObservedResult<T> {
readonly hasMore: boolean;
readonly isFetchingNextPage: boolean;
}
export type ObservedPaginatedResultListener<T> = (result: ObservedPaginatedResult<T>) => void;
export function observedInitialLoading<T>(result: ObservedResult<T>, currentValue: T | null | undefined): boolean {
return currentValue == null && result.isFetching;
}

View file

@ -1,268 +0,0 @@
import type { ModelMetadata } from "../../domain/catalog/metadata";
import type { SharedServerMetadata } from "../../domain/server/metadata";
import type { Thread } from "../../domain/threads/model";
import { AppServerQueryCache, type AppServerQueryClientRunner } from "./cache";
import {
type AppServerContextLease,
type AppServerQueryContext,
type AppServerQueryContextIdentity,
appServerQueryContextIdentity,
appServerQueryContextIdentityKey,
appServerQueryContextIdentityMatches,
appServerQueryContextRawEquals,
createAppServerContextLease,
} from "./keys";
import type { ObservedResultListener } from "./observed-result";
export interface AppServerResourceStoreOptions {
cacheFactory?: (context: AppServerQueryContextIdentity) => AppServerQueryCache;
clientRunner?: AppServerQueryClientRunner;
}
export class StaleAppServerResourceContextError extends Error {
constructor() {
super("Codex app-server resource context changed while loading.");
this.name = "StaleAppServerResourceContextError";
}
}
export function isStaleAppServerResourceContextError(error: unknown): error is StaleAppServerResourceContextError {
return error instanceof StaleAppServerResourceContextError;
}
interface ResourceObserver<T> {
readonly observe: (cache: AppServerQueryCache, listener: (value: T) => void, options: { emitCurrent?: boolean }) => () => void;
readonly listener: (value: T) => void;
readonly options: { emitCurrent?: boolean };
firstSubscribe: boolean;
unsubscribeQuery: (() => void) | null;
}
export class AppServerResourceStore {
private readonly observers = new Set<ResourceObserver<unknown>>();
private readonly cacheFactory: (context: AppServerQueryContextIdentity) => AppServerQueryCache;
private cache: AppServerQueryCache | null = null;
private lease: AppServerContextLease | null = null;
private generation = 0;
constructor(options: AppServerResourceStoreOptions = {}) {
this.cacheFactory =
options.cacheFactory ??
((context) => new AppServerQueryCache(context, options.clientRunner ? { clientRunner: options.clientRunner } : {}));
}
initialize(context: AppServerQueryContext): AppServerContextLease {
if (this.lease) throw new Error("Codex app-server resource store is already initialized.");
this.lease = this.nextLease(context);
this.cache = this.cacheFactory(this.contextIdentity());
this.rebindObservers();
return this.lease;
}
replaceContext(context: AppServerQueryContext): AppServerContextLease {
if (!this.lease) throw new Error("Codex app-server resource store is not initialized.");
if (appServerQueryContextRawEquals(this.lease.context, context)) return this.lease;
this.unsubscribeObservers();
this.cache?.dispose();
this.lease = this.nextLease(context);
this.cache = this.cacheFactory(this.contextIdentity());
this.rebindObservers();
return this.lease;
}
reset(): void {
this.unsubscribeObservers();
this.cache?.dispose();
this.cache = null;
this.lease = null;
}
contextLease(): AppServerContextLease {
if (!this.lease) throw new Error("Codex app-server resource store is not initialized.");
return this.lease;
}
contextIdentity(): AppServerQueryContextIdentity {
return appServerQueryContextIdentity(this.contextLease());
}
contextKey(): string {
return appServerQueryContextIdentityKey(this.contextIdentity());
}
contextKeyFor(context: AppServerQueryContextIdentity): string {
return appServerQueryContextIdentityKey(context);
}
activeThreadsSnapshot(): readonly Thread[] | null {
return this.cache?.activeThreadsSnapshot() ?? null;
}
archivedThreadsSnapshot(): readonly Thread[] | null {
return this.cache?.archivedThreadsSnapshot() ?? null;
}
fetchAllActiveThreads(): Promise<readonly Thread[]> {
return this.runForCurrentContext((cache) => cache.fetchAllActiveThreads());
}
hasMoreActiveThreads(): boolean {
return this.cache?.hasMoreActiveThreads() ?? false;
}
loadMoreActiveThreads(): Promise<readonly Thread[]> {
return this.runForCurrentContext((cache) => cache.loadMoreActiveThreads());
}
refreshActiveThreads(): Promise<readonly Thread[]> {
return this.runForCurrentContext((cache) => cache.refreshActiveThreads());
}
refreshArchivedThreads(): Promise<readonly Thread[]> {
return this.runForCurrentContext((cache) => cache.refreshArchivedThreads());
}
observeActiveThreadsResult(listener: ObservedResultListener<readonly Thread[]>, options?: { emitCurrent?: boolean }): () => void {
return this.observeCurrentContext(
(cache, contextListener, observeOptions) => cache.observeActiveThreadsResult(contextListener, observeOptions),
listener,
options,
);
}
observeArchivedThreadsResult(listener: ObservedResultListener<readonly Thread[]>, options?: { emitCurrent?: boolean }): () => void {
return this.observeCurrentContext(
(cache, contextListener, observeOptions) => cache.observeArchivedThreadsResult(contextListener, observeOptions),
listener,
options,
);
}
appServerMetadataSnapshot(): SharedServerMetadata | null {
return this.cache?.appServerMetadataSnapshot() ?? null;
}
refreshAppServerMetadata(): Promise<SharedServerMetadata | null> {
return this.runForCurrentContext((cache) => cache.refreshAppServerMetadata());
}
refreshSkills(): Promise<SharedServerMetadata | null> {
return this.runForCurrentContext((cache) => cache.refreshSkills());
}
refreshRateLimits(): Promise<SharedServerMetadata | null> {
return this.runForCurrentContext((cache) => cache.refreshRateLimits());
}
observeAppServerMetadataResult(listener: ObservedResultListener<SharedServerMetadata>, options?: { emitCurrent?: boolean }): () => void {
return this.observeCurrentContext(
(cache, contextListener, observeOptions) => cache.observeAppServerMetadataResult(contextListener, observeOptions),
listener,
options,
);
}
modelsSnapshot(): readonly ModelMetadata[] | null {
return this.cache?.modelsSnapshot() ?? null;
}
fetchModels(): Promise<readonly ModelMetadata[]> {
return this.runForCurrentContext((cache) => cache.fetchModels());
}
refreshModels(): Promise<readonly ModelMetadata[]> {
return this.runForCurrentContext((cache) => cache.refreshModels());
}
observeModelsResult(listener: ObservedResultListener<readonly ModelMetadata[]>, options?: { emitCurrent?: boolean }): () => void {
return this.observeCurrentContext(
(cache, contextListener, observeOptions) => cache.observeModelsResult(contextListener, observeOptions),
listener,
options,
);
}
private nextLease(context: AppServerQueryContext): AppServerContextLease {
return createAppServerContextLease(context, ++this.generation);
}
private isCurrent(context: AppServerQueryContextIdentity): boolean {
return Boolean(this.lease && appServerQueryContextIdentityMatches(this.contextIdentity(), context));
}
private runForCurrentContext<T>(operation: (cache: AppServerQueryCache) => Promise<T>): Promise<T> {
const context = this.contextIdentity();
const cache = this.currentCache();
return this.runForIdentity(context, () => operation(cache));
}
private async runForIdentity<T>(context: AppServerQueryContextIdentity, operation: () => Promise<T>): Promise<T> {
if (!this.isCurrent(context)) throw new StaleAppServerResourceContextError();
let result: T;
try {
result = await operation();
} catch (error) {
if (!this.isCurrent(context)) throw new StaleAppServerResourceContextError();
throw error;
}
if (!this.isCurrent(context)) throw new StaleAppServerResourceContextError();
return result;
}
private observeCurrentContext<T>(
observe: ResourceObserver<T>["observe"],
listener: ResourceObserver<T>["listener"],
options: { emitCurrent?: boolean } = {},
): () => void {
const observer: ResourceObserver<T> = {
observe,
listener,
options,
firstSubscribe: true,
unsubscribeQuery: null,
};
this.observers.add(observer as ResourceObserver<unknown>);
this.bindObserver(observer);
return () => {
this.observers.delete(observer as ResourceObserver<unknown>);
observer.unsubscribeQuery?.();
observer.unsubscribeQuery = null;
};
}
private bindObserver<T>(observer: ResourceObserver<T>): void {
if (!this.lease || !this.cache) return;
const context = this.contextIdentity();
const cache = this.cache;
const observeOptions: { emitCurrent?: boolean } = {};
if (observer.firstSubscribe) {
if (observer.options.emitCurrent !== undefined) observeOptions.emitCurrent = observer.options.emitCurrent;
} else {
observeOptions.emitCurrent = true;
}
observer.unsubscribeQuery = observer.observe(
cache,
(value) => {
if (this.isCurrent(context)) observer.listener(value);
},
observeOptions,
);
observer.firstSubscribe = false;
}
private unsubscribeObservers(): void {
for (const observer of this.observers) {
observer.unsubscribeQuery?.();
observer.unsubscribeQuery = null;
}
}
private rebindObservers(): void {
for (const observer of this.observers) this.bindObserver(observer);
}
private currentCache(): AppServerQueryCache {
if (!this.cache) throw new Error("Codex app-server resource store is not initialized.");
return this.cache;
}
}

View file

@ -1,7 +1,7 @@
import type { ModelMetadata } from "../../domain/catalog/metadata";
import { cloneRuntimeConfigSnapshot } from "../../domain/runtime/config";
import type { RateLimitSnapshot } from "../../domain/runtime/metrics";
import type { SharedServerMetadata } from "../../domain/server/metadata";
import type { SharedServerMetadata, SharedServerMetadataResource } from "../../domain/server/metadata";
import { cloneToolInventorySnapshot } from "../../domain/server/tool-inventory";
import type { Thread } from "../../domain/threads/model";
@ -34,6 +34,40 @@ export function cloneSharedServerMetadata(metadata: SharedServerMetadata): Share
};
}
export function cloneSharedServerMetadataResource(resource: SharedServerMetadataResource): SharedServerMetadataResource {
switch (resource.id) {
case "runtimeConfig":
return {
id: resource.id,
value: resource.value ? cloneRuntimeConfigSnapshot(resource.value) : undefined,
};
case "models":
return {
id: resource.id,
value: resource.value ? cloneModelMetadata(resource.value) : undefined,
probe: { ...resource.probe },
};
case "skills":
return {
id: resource.id,
value: resource.value?.map((skill) => ({ ...skill })),
probe: { ...resource.probe },
};
case "permissionProfiles":
return {
id: resource.id,
value: resource.value?.map((profile) => ({ ...profile })),
probe: { ...resource.probe },
};
case "rateLimits":
return {
id: resource.id,
value: resource.value ? cloneRateLimitSnapshot(resource.value) : resource.value,
probe: { ...resource.probe },
};
}
}
function cloneRateLimitSnapshot(snapshot: RateLimitSnapshot): RateLimitSnapshot {
return {
...snapshot,

View file

@ -0,0 +1,31 @@
import type { Thread } from "../../domain/threads/model";
export type ThreadListKind = "active" | "archived";
export type ThreadListMutation =
| { readonly kind: "upsert"; readonly list: ThreadListKind; readonly thread: Thread }
| { readonly kind: "remove"; readonly list: ThreadListKind; readonly threadId: string }
| {
readonly kind: "update";
readonly list: ThreadListKind;
readonly threadId: string;
readonly changes: Partial<Pick<Thread, "name" | "recencyAt">>;
}
| { readonly kind: "refresh"; readonly list: ThreadListKind };
export function applyThreadListMutation(snapshot: readonly Thread[] | null, mutation: ThreadListMutation): readonly Thread[] | null {
switch (mutation.kind) {
case "refresh":
return snapshot;
case "upsert": {
if (!snapshot) return null;
const index = snapshot.findIndex((thread) => thread.id === mutation.thread.id);
if (index < 0) return [mutation.thread, ...snapshot];
return snapshot.map((thread) => (thread.id === mutation.thread.id ? mutation.thread : thread));
}
case "remove":
return snapshot?.filter((thread) => thread.id !== mutation.threadId) ?? null;
case "update":
return snapshot?.map((thread) => (thread.id === mutation.threadId ? { ...thread, ...mutation.changes } : thread)) ?? null;
}
}

View file

@ -38,7 +38,7 @@ export interface EphemeralStructuredTurnClient {
type EphemeralStructuredTurnRuntimeCapableClient = EphemeralStructuredTurnClient & ModelMetadataClient;
export type EphemeralStructuredTurnClientFactory = (
type EphemeralStructuredTurnClientFactory = (
codexPath: string,
cwd: string,
handlers: AppServerClientHandlers,
@ -67,6 +67,12 @@ export type EphemeralStructuredTurnRunner = (options: RunEphemeralStructuredTurn
export interface EphemeralStructuredTurnDependencies {
clientFactory?: EphemeralStructuredTurnClientFactory | undefined;
timers?: EphemeralStructuredTurnTimers | undefined;
clientLifecycle?:
| {
created(client: EphemeralStructuredTurnClient): void;
disposed(client: EphemeralStructuredTurnClient): void;
}
| undefined;
}
export async function runEphemeralStructuredTurn(
@ -131,6 +137,7 @@ export async function runEphemeralStructuredTurn(
abortOperation(new Error(options.exitedMessage));
},
});
dependencies.clientLifecycle?.created(client);
try {
await runAbortable(client.connect());
@ -175,8 +182,12 @@ export async function runEphemeralStructuredTurn(
} finally {
state = completeEphemeralStructuredTurnState(state);
timers.clearTimeout(timeout);
await deleteEphemeralStructuredTurnThread(client, threadId);
client.disconnect();
try {
await deleteEphemeralStructuredTurnThread(client, threadId);
} finally {
client.disconnect();
dependencies.clientLifecycle?.disposed(client);
}
}
}

View file

@ -6,7 +6,6 @@ import type { RuntimeServiceTierRequest, RuntimeSettingsPatch } from "../../doma
import type { ThreadActivationSnapshot } from "../../domain/threads/activation";
import type { ArchiveThreadInput } from "../../domain/threads/archive-markdown";
import type { ThreadGoal, ThreadGoalUpdate } from "../../domain/threads/goal";
import type { HistoricalTurn } from "../../domain/threads/history";
import type { Thread } from "../../domain/threads/model";
import { REFERENCED_THREAD_TURN_LIMIT } from "../../domain/threads/reference";
import type { TurnTranscriptSummary } from "../../domain/threads/transcript";
@ -23,8 +22,6 @@ import {
} from "../protocol/turn";
import type { AppServerRequestClient } from "./request-client";
const THREAD_LIST_PAGE_LIMIT = 100;
export type ThreadTurnSortDirection = "asc" | "desc";
interface TurnTranscriptSummaryPage {
@ -63,6 +60,7 @@ export interface AppServerThreadListOptions {
export interface ThreadPage {
readonly threads: readonly Thread[];
readonly nextCursor: string | null;
readonly fetchedSize: number;
}
export function startThread(
@ -107,17 +105,21 @@ export function resumeThread(
});
}
export async function listThreads(client: AppServerRequestClient, cwd: string, options: { archived?: boolean } = {}): Promise<Thread[]> {
export async function listThreads(
client: AppServerRequestClient,
cwd: string,
options: { archived?: boolean; signal?: AbortSignal } = {},
): Promise<Thread[]> {
const archived = options.archived ?? false;
const threads: Thread[] = [];
const seenCursors = new Set<string>();
let cursor: string | null = null;
for (;;) {
options.signal?.throwIfAborted();
const page = await readThreadPage(client, cwd, {
archived,
cursor,
limit: THREAD_LIST_PAGE_LIMIT,
});
threads.push(...page.threads);
@ -141,11 +143,11 @@ export async function readThreadPage(
const page = await readThreadRecordPage(client, cwd, {
...options,
archived,
limit: options.limit ?? THREAD_LIST_PAGE_LIMIT,
});
return {
threads: threadsFromThreadRecords(page.data, { archived }),
nextCursor: page.nextCursor ?? null,
fetchedSize: page.data.length,
};
}
@ -184,47 +186,61 @@ export async function readReferencedThreadTurnTranscriptSummaries(
return chronologicalTurnTranscriptSummariesFromTurnRecords(response.data);
}
export interface ThreadRollbackSnapshot {
thread: Thread;
cwd: string;
turns: readonly HistoricalTurn[];
}
type ThreadForkPosition =
| { readonly kind: "through-turn"; readonly turnId: string }
| { readonly kind: "before-turn"; readonly turnId: string };
interface ThreadRollbackResult {
readonly thread: ThreadRecord & {
readonly cwd: string;
readonly turns: readonly HistoricalTurn[];
interface AppServerForkThreadOptions {
readonly position?: ThreadForkPosition;
readonly deferGoalContinuation?: boolean;
readonly runtime?: {
readonly model?: string;
readonly reasoningEffort?: string | null;
readonly serviceTier?: ServiceTier | null;
readonly approvalPolicy?: RuntimePermissionState["approvalPolicy"];
readonly approvalsReviewer?: ApprovalsReviewer;
readonly permissions?: string;
readonly sandboxPolicy?: RuntimePermissionState["sandboxPolicy"];
};
}
function threadRollbackSnapshotFromAppServerResponse(response: ThreadRollbackResult): ThreadRollbackSnapshot {
return {
thread: threadFromThreadRecord(response.thread),
cwd: response.thread.cwd,
turns: response.thread.turns,
};
}
export async function rollbackThread(client: AppServerRequestClient, threadId: string, numTurns?: number): Promise<ThreadRollbackSnapshot> {
const response = await client.request("thread/rollback", { threadId, numTurns: numTurns ?? 1 });
return threadRollbackSnapshotFromAppServerResponse(response);
}
export async function forkThread(
client: AppServerRequestClient,
threadId: string,
cwd: string,
lastTurnId: string | null = null,
options: AppServerForkThreadOptions = {},
): Promise<Thread> {
const position = options.position;
const runtime = options.runtime;
const permissions = runtime?.permissions;
const sandbox = permissions === undefined ? appServerSandboxMode(runtime?.sandboxPolicy) : undefined;
const response = await client.request("thread/fork", {
threadId,
cwd,
excludeTurns: true,
...(lastTurnId ? { lastTurnId } : {}),
...(position?.kind === "through-turn" ? { lastTurnId: position.turnId } : {}),
...(position?.kind === "before-turn" ? { beforeTurnId: position.turnId } : {}),
...(options.deferGoalContinuation ? { deferGoalContinuation: true } : {}),
...(runtime?.model !== undefined ? { model: runtime.model } : {}),
...(runtime?.serviceTier !== undefined ? { serviceTier: runtime.serviceTier } : {}),
...(runtime?.approvalPolicy !== undefined && runtime.approvalPolicy !== null ? { approvalPolicy: runtime.approvalPolicy } : {}),
...(runtime?.approvalsReviewer !== undefined ? { approvalsReviewer: runtime.approvalsReviewer } : {}),
...(permissions !== undefined ? { permissions } : {}),
...(sandbox !== undefined ? { sandbox } : {}),
...(runtime?.reasoningEffort !== undefined ? { config: { model_reasoning_effort: runtime.reasoningEffort } } : {}),
});
return threadFromThreadRecord(response.thread);
}
function appServerSandboxMode(
policy: RuntimePermissionState["sandboxPolicy"] | undefined,
): "read-only" | "workspace-write" | "danger-full-access" | undefined {
if (!policy || policy.type === "externalSandbox") return undefined;
if (policy.type === "dangerFullAccess") return "danger-full-access";
if (policy.type === "readOnly") return "read-only";
return "workspace-write";
}
export interface EphemeralThreadForkSnapshot {
readonly activation: ThreadActivationSnapshot;
readonly sourceThreadId: string;
@ -245,13 +261,12 @@ export class EphemeralThreadCleanupRequiredError extends Error {
export async function forkEphemeralThread(
client: AppServerRequestClient,
sourceThreadId: string,
cwd: string,
vaultPath: string,
): Promise<EphemeralThreadForkSnapshot> {
const source = await client.request("thread/read", { threadId: sourceThreadId, includeTurns: false });
const config = await client.request("config/read", { cwd: source.thread.cwd });
const config = await client.request("config/read", { cwd: vaultPath });
const response = await client.request("thread/fork", {
threadId: sourceThreadId,
cwd,
cwd: vaultPath,
ephemeral: true,
sandbox: "read-only",
approvalPolicy: "never",
@ -310,7 +325,6 @@ export function threadActivationSnapshotFromAppServerResponse(response: ThreadAc
});
return {
thread: threadFromThreadRecord(response.thread),
cwd: response.cwd,
approvalPolicyKnown: "approvalPolicy" in response,
sandboxPolicyKnown: "sandbox" in response || "sandboxPolicy" in response,
permissionProfileKnown: "activePermissionProfile" in response,

View file

@ -1,7 +1,7 @@
import type { CodexInput } from "../../domain/chat/input";
import type { ClientResponseByMethod } from "../connection/client";
import type { ClientRequestParams } from "../connection/rpc-messages";
import { additionalContextFromCodexInput, toAppServerUserInput } from "../protocol/request-input";
import { appServerTurnInputFromCodexInput, toAppServerUserInput } from "../protocol/request-input";
import type { AppServerRequestClient } from "./request-client";
type AppServerTurnRuntimeOverrides = Partial<AppServerTurnRuntimeParams>;
@ -32,14 +32,14 @@ export function startTurn(
options: AppServerStartTurnOptions,
): Promise<ClientResponseByMethod["turn/start"]> {
const { threadId, cwd, input, clientUserMessageId, runtime } = options;
const additionalContext = toAdditionalContext(input);
const prepared = toTurnInput(input, contextSubmissionId(clientUserMessageId, "user"));
const params: ClientRequestParams<"turn/start"> = {
threadId,
cwd,
...(clientUserMessageId !== undefined ? { clientUserMessageId } : {}),
...(additionalContext !== undefined ? { additionalContext } : {}),
...(prepared.additionalContext !== undefined ? { additionalContext: prepared.additionalContext } : {}),
...appServerTurnRuntimeParams(runtime),
input: toUserInput(input),
input: prepared.input,
};
return client.request("turn/start", params);
}
@ -72,13 +72,13 @@ export function steerTurn(
input: string | CodexInput,
clientUserMessageId?: string | null,
): Promise<unknown> {
const additionalContext = toAdditionalContext(input);
const prepared = toTurnInput(input, contextSubmissionId(clientUserMessageId, "steer"));
return client.request("turn/steer", {
threadId,
expectedTurnId,
input: toUserInput(input),
input: prepared.input,
...(clientUserMessageId !== undefined ? { clientUserMessageId } : {}),
...(additionalContext !== undefined ? { additionalContext } : {}),
...(prepared.additionalContext !== undefined ? { additionalContext: prepared.additionalContext } : {}),
});
}
@ -86,14 +86,9 @@ export function interruptTurn(client: AppServerRequestClient, threadId: string,
return client.request("turn/interrupt", { threadId, turnId });
}
function toUserInput(input: string | CodexInput): ClientRequestParams<"turn/start">["input"] {
if (typeof input !== "string") return toAppServerUserInput(input);
return toAppServerUserInput([{ type: "text", text: input }]);
}
function toAdditionalContext(input: string | CodexInput): ClientRequestParams<"turn/start">["additionalContext"] | undefined {
if (typeof input === "string") return undefined;
return additionalContextFromCodexInput(input);
function toTurnInput(input: string | CodexInput, submissionId: string) {
if (typeof input !== "string") return appServerTurnInputFromCodexInput(input, submissionId);
return { input: toAppServerUserInput([{ type: "text", text: input }]) };
}
function appServerTurnRuntimeParams(runtime: AppServerTurnRuntimeOverrides | undefined): AppServerTurnRuntimeParams {
@ -105,3 +100,11 @@ function appServerTurnRuntimeParams(runtime: AppServerTurnRuntimeOverrides | und
if (runtime?.approvalsReviewer !== undefined) params.approvalsReviewer = runtime.approvalsReviewer;
return params;
}
let fallbackContextSubmissionSequence = 0;
function contextSubmissionId(clientUserMessageId: string | null | undefined, kind: "user" | "steer"): string {
if (clientUserMessageId) return clientUserMessageId;
fallbackContextSubmissionSequence += 1;
return `local-${kind}-${String(Date.now())}-fallback-${String(fallbackContextSubmissionSequence)}-1`;
}

View file

@ -0,0 +1,53 @@
const encoder = new TextEncoder();
export function utf8ByteLength(value: string): number {
return encoder.encode(value).byteLength;
}
export function splitUtf8Context(value: string, maxBytes: number, maxParts: number): { parts: string[]; includedBytes: number } {
const parts: string[] = [];
let rest = value;
while (rest && parts.length < maxParts) {
const prefix = utf8Prefix(rest, maxBytes);
if (!prefix) break;
const boundary = preferredBoundary(prefix, rest.length > prefix.length);
const part = prefix.slice(0, boundary);
parts.push(part);
rest = rest.slice(part.length);
}
return { parts, includedBytes: parts.reduce((total, part) => total + utf8ByteLength(part), 0) };
}
export function truncateUtf8(value: string, maxBytes: number): string {
return utf8Prefix(value, maxBytes);
}
function utf8Prefix(value: string, maxBytes: number): string {
if (maxBytes <= 0 || !value) return "";
if (utf8ByteLength(value) <= maxBytes) return value;
let low = 0;
let high = value.length;
while (low < high) {
const middle = Math.ceil((low + high) / 2);
const candidate = safeCodeUnitBoundary(value, middle);
if (utf8ByteLength(value.slice(0, candidate)) <= maxBytes) low = middle;
else high = middle - 1;
}
return value.slice(0, safeCodeUnitBoundary(value, low));
}
function safeCodeUnitBoundary(value: string, index: number): number {
if (index <= 0 || index >= value.length) return index;
const code = value.charCodeAt(index);
return code >= 0xdc00 && code <= 0xdfff ? index - 1 : index;
}
function preferredBoundary(prefix: string, hasRemainder: boolean): number {
if (!hasRemainder) return prefix.length;
const minimum = Math.floor(prefix.length / 2);
for (const marker of ["\n\n", "\n", " "]) {
const index = prefix.lastIndexOf(marker);
if (index >= minimum) return index + marker.length;
}
return prefix.length;
}

View file

@ -1,9 +1,14 @@
export interface RequestMention {
export interface VaultFileReference {
name: string;
path: string;
}
export const ACTIVE_FILE_MENTION_NAME = "<active>";
export interface SkillReference {
name: string;
path: string;
}
export const ACTIVE_FILE_REFERENCE_NAME = "<active>";
export interface RequestAdditionalContext {
key: string;
@ -16,8 +21,13 @@ export type CodexInputItem =
| { type: "image"; url: string; detail?: UserInputImageDetail }
| { type: "localImage"; path: string; detail?: UserInputImageDetail }
| { type: "skill"; name: string; path: string }
| { type: "mention"; name: string; path: string }
| { type: "additionalContext"; key: string; value: string; kind: RequestAdditionalContext["kind"] };
| { type: "fileReference"; name: string; path: string }
| {
type: "additionalContext";
key: string;
value: string;
kind: RequestAdditionalContext["kind"];
};
export type CodexInput = CodexInputItem[];
type UserInputImageDetail = "auto" | "low" | "high" | "original";
@ -26,15 +36,15 @@ export function codexTextInput(text: string): CodexInput {
return [{ type: "text", text }];
}
export function codexTextInputWithMentions(
export function codexTextInputWithReferences(
text: string,
mentions: readonly RequestMention[],
skills: readonly RequestMention[] = [],
fileReferences: readonly VaultFileReference[],
skills: readonly SkillReference[] = [],
additionalContext: readonly RequestAdditionalContext[] = [],
): CodexInput {
return [
...codexTextInput(text),
...mentions.map((mention) => ({ type: "mention" as const, name: mention.name, path: mention.path })),
...fileReferences.map((reference) => ({ type: "fileReference" as const, name: reference.name, path: reference.path })),
...skills.map((skill) => ({ type: "skill" as const, name: skill.name, path: skill.path })),
...additionalContext.map((context) => ({
type: "additionalContext" as const,

View file

@ -0,0 +1,10 @@
const PANEL_SUBMISSION_CLIENT_ID_PATTERN = /^local-(?:user|steer)-\d+-[A-Za-z0-9_-]+-[a-z0-9]+-[a-z0-9]+$/;
export function isPanelSubmissionClientId(value: string | null | undefined): value is string {
return typeof value === "string" && PANEL_SUBMISSION_CLIENT_ID_PATTERN.test(value);
}
export function turnContextSubmissionId(value: string): string {
const safe = value.replace(/[^A-Za-z0-9_.-]/g, "_").slice(0, 120);
return safe || "context";
}

View file

@ -0,0 +1,19 @@
import { fromMarkdown } from "mdast-util-from-markdown";
import { visit } from "unist-util-visit";
export type MarkdownCodeRange = readonly [start: number, end: number];
export function markdownCodeRanges(markdown: string): MarkdownCodeRange[] {
const ranges: MarkdownCodeRange[] = [];
visit(fromMarkdown(markdown), (node) => {
if (node.type !== "code" && node.type !== "inlineCode") return;
const start = node.position?.start.offset;
const end = node.position?.end.offset;
if (start !== undefined && end !== undefined) ranges.push([start, end]);
});
return ranges;
}
export function markdownCodeRangeContainsOffset(ranges: readonly MarkdownCodeRange[], offset: number): boolean {
return ranges.some(([start, end]) => offset >= start && offset < end);
}

View file

@ -1,7 +0,0 @@
export function yamlFrontmatterString(value: string): string {
return JSON.stringify(value);
}
export function yamlFrontmatterInlineList(values: readonly string[]): string {
return `[${values.map(yamlFrontmatterString).join(", ")}]`;
}

View file

@ -11,24 +11,10 @@ interface PendingRequestQueueSource {
readonly pendingMcpElicitations: readonly unknown[];
}
interface PendingRequestCountSource {
readonly pendingApprovals: number;
readonly pendingUserInputs: number;
readonly pendingMcpElicitations: number;
}
export function pendingRequestCountsFromQueues(source: PendingRequestQueueSource): PendingRequestCounts {
return pendingRequestCounts({
pendingApprovals: source.approvals.length,
pendingUserInputs: source.pendingUserInputs.length,
pendingMcpElicitations: source.pendingMcpElicitations.length,
});
}
export function pendingRequestCounts(source: PendingRequestCountSource): PendingRequestCounts {
const approvals = source.pendingApprovals;
const userInputs = source.pendingUserInputs;
const mcpElicitations = source.pendingMcpElicitations;
const approvals = source.approvals.length;
const userInputs = source.pendingUserInputs.length;
const mcpElicitations = source.pendingMcpElicitations.length;
return {
approvals,
userInputs,

View file

@ -19,7 +19,7 @@ export interface RuntimeConfigSnapshot {
readonly autoCompactTokenLimit: number | null;
}
export function emptyRuntimeConfigSnapshot(): RuntimeConfigSnapshot {
function emptyRuntimeConfigSnapshot(): RuntimeConfigSnapshot {
return {
profile: null,
model: null,

View file

@ -15,7 +15,6 @@ export interface CollaborationMode {
}
export interface RuntimeSettingsPatch {
cwd?: string | null;
approvalPolicy?: RuntimeApprovalPolicy | null;
approvalsReviewer?: ApprovalsReviewer | null;
permissions?: string | null;

View file

@ -125,12 +125,6 @@ export function upsertMcpServerDiagnostic(diagnostics: Diagnostics, server: McpS
};
}
export function upsertMcpServerStatusDiagnostics(diagnostics: Diagnostics, servers: readonly McpServerStatusSummary[]): Diagnostics {
let next = diagnostics;
for (const server of servers) next = upsertMcpServerDiagnostic(next, mcpServerDiagnosticFromStatus(server));
return next;
}
export function replaceMcpServerStatusDiagnostics(diagnostics: Diagnostics, servers: readonly McpServerStatusSummary[]): Diagnostics {
return {
...diagnostics,

View file

@ -1,8 +1,8 @@
import type { SkillMetadata } from "../catalog/metadata";
import type { ModelMetadata, SkillMetadata } from "../catalog/metadata";
import type { RuntimeConfigSnapshot } from "../runtime/config";
import type { RateLimitSnapshot } from "../runtime/metrics";
import type { RuntimePermissionProfileSummary } from "../runtime/permissions";
import type { Diagnostics } from "./diagnostics";
import type { DiagnosticProbeResult, Diagnostics } from "./diagnostics";
export interface SharedServerMetadata {
runtimeConfig: RuntimeConfigSnapshot | null;
@ -11,3 +11,26 @@ export interface SharedServerMetadata {
rateLimit: RateLimitSnapshot | null;
serverDiagnostics: Diagnostics;
}
export type SharedServerMetadataResource =
| { readonly id: "runtimeConfig"; readonly value: RuntimeConfigSnapshot | undefined }
| {
readonly id: "models";
readonly value: readonly ModelMetadata[] | undefined;
readonly probe: DiagnosticProbeResult;
}
| {
readonly id: "skills";
readonly value: readonly SkillMetadata[] | undefined;
readonly probe: DiagnosticProbeResult;
}
| {
readonly id: "permissionProfiles";
readonly value: readonly RuntimePermissionProfileSummary[] | undefined;
readonly probe: DiagnosticProbeResult;
}
| {
readonly id: "rateLimits";
readonly value: RateLimitSnapshot | null | undefined;
readonly probe: DiagnosticProbeResult;
};

View file

@ -5,7 +5,6 @@ import type { Thread } from "./model";
export interface ThreadActivationSnapshot extends RuntimePermissionState, RuntimePermissionKnownState {
thread: Thread;
cwd: string;
model: string | null;
serviceTier: ServiceTier | null;
approvalsReviewer: ApprovalsReviewer | null;

View file

@ -1,21 +1,21 @@
import { yamlFrontmatterInlineList, yamlFrontmatterString } from "../markdown/frontmatter";
import { isExternalFileHref, parseFileHref } from "../vault/file-hrefs";
import type { Link, Nodes } from "mdast";
import { fromMarkdown } from "mdast-util-from-markdown";
import { toMarkdown } from "mdast-util-to-markdown";
import { visit } from "unist-util-visit";
import { parseFileHref } from "../vault/file-hrefs";
import { isFilesystemAbsolutePath, isVaultConfigPath, normalizeFilePath, vaultRelativePath } from "../vault/paths";
import type { Thread } from "./model";
import { referencedThreadMetadataFromPrompt } from "./reference";
import { threadArchiveTitle } from "./title";
import type { ThreadTranscriptEntry } from "./transcript";
interface ParsedMarkdownLink {
raw: string;
label: string;
href: string;
interface MarkdownSourceReplacement {
start: number;
end: number;
value: string;
}
export interface ArchiveExportSettings {
archiveExportFolderTemplate: string;
archiveExportFilenameTemplate: string;
export interface ArchiveMarkdownOptions {
archiveExportTags?: string;
vaultPath?: string;
vaultConfigDir?: string;
@ -25,56 +25,31 @@ export interface ArchiveThreadInput extends Thread {
transcriptEntries: readonly ThreadTranscriptEntry[];
}
export function archivedThreadMarkdown(
thread: ArchiveThreadInput,
exportedAt = new Date(),
settings?: Partial<ArchiveExportSettings>,
): string {
export function archivedThreadMarkdown(thread: ArchiveThreadInput, exportedAt = new Date(), settings: ArchiveMarkdownOptions = {}): string {
const title = threadArchiveTitle(thread);
const tags = normalizedArchiveTags(settings?.archiveExportTags ?? "");
const lines = [
const tags = normalizedArchiveTags(settings.archiveExportTags ?? "");
const frontmatter = [
"---",
`title: ${yamlFrontmatterString(title)}`,
`thread_id: ${yamlFrontmatterString(thread.id)}`,
`created: ${yamlFrontmatterString(formatDate(exportedAt))}`,
...frontmatterTagsLines(tags),
"---",
"",
`# ${title}`,
"",
...transcriptMarkdownLines(thread.transcriptEntries),
];
const markdown = `${trimTrailingBlankLines(lines).join("\n")}\n`;
return settings?.vaultPath ? normalizeExportedMarkdownLinks(markdown, settings.vaultPath, settings.vaultConfigDir) : markdown;
].join("\n");
const body = `${trimTrailingBlankLines([`# ${title}`, "", ...transcriptMarkdownLines(thread.transcriptEntries)]).join("\n")}\n`;
const normalizedBody = settings.vaultPath ? normalizeExportedMarkdownLinks(body, settings.vaultPath, settings.vaultConfigDir) : body;
return `${frontmatter}\n\n${normalizedBody}`;
}
function normalizeExportedMarkdownLinks(markdown: string, vaultPath: string, vaultConfigDir: string | null | undefined): string {
const lines = markdown.split("\n");
let inFence = false;
return lines
.map((line) => {
if (line.trimStart().startsWith("```")) {
inFence = !inFence;
return line;
}
return inFence ? line : normalizeExportedMarkdownLinksInLine(line, vaultPath, vaultConfigDir);
})
.join("\n");
}
function normalizeExportedMarkdownLinksInLine(line: string, vaultPath: string, vaultConfigDir: string | null | undefined): string {
let output = "";
let index = 0;
while (index < line.length) {
const parsed = parseMarkdownLinkAt(line, index);
if (!parsed || isInsideInlineCode(line, index)) {
output += line[index] ?? "";
index += 1;
continue;
}
output += normalizedExportedMarkdownLink(parsed, vaultPath, vaultConfigDir);
index = parsed.end;
const replacements: MarkdownSourceReplacement[] = [];
visit(fromMarkdown(markdown), "link", (link) => {
const replacement = normalizedExportedMarkdownLink(link, vaultPath, vaultConfigDir);
if (replacement) replacements.push(replacement);
});
let output = markdown;
for (const replacement of replacements.sort((left, right) => right.start - left.start)) {
output = `${output.slice(0, replacement.start)}${replacement.value}${output.slice(replacement.end)}`;
}
return output;
}
@ -99,18 +74,19 @@ function markdownLinesFromTranscriptEntry(entry: ThreadTranscriptEntry): string[
switch (entry.kind) {
case "user": {
const heading = timestampedHeading("User", entry.timestamp);
const referenced = referencedThreadMetadataFromPrompt(entry.text);
if (referenced) {
return [
heading,
"",
referenced.text,
"",
`> Referenced: ${referenced.reference.title} (${String(referenced.reference.includedTurns)}/${String(referenced.reference.turnLimit)} turns, ${referenced.reference.threadId})`,
"",
];
}
return [heading, "", entry.text, ""];
return [
heading,
"",
entry.text,
"",
...(entry.referencedThread
? [
`> Referenced: ${entry.referencedThread.title} (${String(entry.referencedThread.includedTurns)}/${String(entry.referencedThread.turnLimit)} turns${entry.referencedThread.truncated ? ", truncated" : ""}, ${entry.referencedThread.threadId})`,
"",
]
: []),
...(entry.contexts ?? []).flatMap((context) => [`> Context: ${archiveContextLabel(context, entry.text)}`, ""]),
];
}
case "assistant":
return [timestampedHeading("Codex", entry.timestamp), "", entry.text, ""];
@ -119,6 +95,23 @@ function markdownLinesFromTranscriptEntry(entry: ThreadTranscriptEntry): string[
}
}
function archiveContextLabel(context: NonNullable<ThreadTranscriptEntry["contexts"]>[number], visibleText: string): string {
const truncated = context.truncated ? " (truncated)" : "";
if (context.kind === "obsidian") return `Obsidian context${truncated}`;
const url = visibleWebUrl(visibleText);
return `Web page${truncated}${url ? ` (${url})` : ""}`;
}
function visibleWebUrl(text: string): string | null {
const firstToken = text.trim().split(/\s+/, 1)[0] ?? "";
try {
const parsed = new URL(firstToken);
return parsed.protocol === "http:" || parsed.protocol === "https:" ? parsed.toString() : null;
} catch {
return null;
}
}
function timestampedHeading(label: string, unixSeconds: number | null): string {
const timestamp = formatUnixTimestamp(unixSeconds);
return timestamp ? `## ${label} - ${timestamp}` : `## ${label}`;
@ -146,67 +139,45 @@ function stripMatchingQuotes(value: string): string {
return (first === `"` || first === `'`) && first === last ? value.slice(1, -1) : value;
}
function unwrappedMarkdownHref(value: string): string {
if (value.startsWith("<") && value.endsWith(">")) return value.slice(1, -1);
return value;
}
function normalizedExportedMarkdownLink(
link: Link,
vaultPath: string,
vaultConfigDir: string | null | undefined,
): MarkdownSourceReplacement | null {
const start = link.position?.start.offset;
const end = link.position?.end.offset;
if (start === undefined || end === undefined || !link.url) return null;
function parseMarkdownLinkAt(line: string, start: number): ParsedMarkdownLink | null {
if (line[start] !== "[" || line[start - 1] === "!") return null;
const parsed = parseFileHref(link.url);
if (!parsed) return null;
const vaultRelative = vaultRelativePath(vaultPath, parsed.path);
if (vaultRelative && !archiveExportShouldKeepAbsolute(vaultRelative, vaultConfigDir)) {
return {
start,
end,
value: markdownNodeSource({ ...link, url: `${vaultRelative}${parsed.subpath}` }),
};
}
const labelEnd = line.indexOf("]", start + 1);
if (labelEnd === -1 || line[labelEnd + 1] !== "(") return null;
const hrefStart = labelEnd + 2;
const hrefEnd = line[hrefStart] === "<" ? line.indexOf(">)", hrefStart + 1) : line.indexOf(")", hrefStart);
if (hrefEnd === -1) return null;
const end = line[hrefStart] === "<" ? hrefEnd + 2 : hrefEnd + 1;
if (!isFilesystemAbsolutePath(normalizeFilePath(parsed.path))) return null;
return {
raw: line.slice(start, end),
label: line.slice(start + 1, labelEnd),
href: line.slice(hrefStart, hrefEnd + (line[hrefStart] === "<" ? 1 : 0)),
start,
end,
value: markdownNodeSource({
type: "paragraph",
children: [...link.children, { type: "text", value: " (" }, { type: "inlineCode", value: link.url }, { type: "text", value: ")" }],
}),
};
}
function normalizedExportedMarkdownLink(link: ParsedMarkdownLink, vaultPath: string, vaultConfigDir: string | null | undefined): string {
const href = unwrappedMarkdownHref(link.href.trim());
if (!href || isExternalFileHref(href)) return link.raw;
const parsed = parseFileHref(href);
if (!parsed) return link.raw;
const vaultRelative = vaultRelativePath(vaultPath, parsed.path);
if (vaultRelative && !archiveExportShouldKeepAbsolute(vaultRelative, vaultConfigDir)) {
return `[${link.label}](${markdownHref(`${vaultRelative}${parsed.subpath}`)})`;
}
return isFilesystemAbsolutePath(normalizeFilePath(parsed.path)) ? `${link.label} (${markdownCodeSpan(href)})` : link.raw;
function markdownNodeSource(node: Nodes): string {
return toMarkdown(node, { unsafe: [{ character: "|", inConstruct: ["label", "phrasing"] }] }).trimEnd();
}
function archiveExportShouldKeepAbsolute(vaultRelativePath: string, vaultConfigDir: string | null | undefined): boolean {
return typeof vaultConfigDir === "string" && vaultConfigDir.length > 0 && isVaultConfigPath(vaultRelativePath, vaultConfigDir);
}
function markdownHref(value: string): string {
return /[\s()]/.test(value) ? `<${value}>` : value;
}
function markdownCodeSpan(value: string): string {
let fenceLength = 1;
for (const match of value.matchAll(/`+/g)) fenceLength = Math.max(fenceLength, match[0].length + 1);
const fence = "`".repeat(fenceLength);
return fenceLength === 1 ? `${fence}${value}${fence}` : `${fence} ${value} ${fence}`;
}
function isInsideInlineCode(line: string, offset: number): boolean {
let inCode = false;
for (let index = 0; index < offset; index += 1) {
if (line[index] === "`") inCode = !inCode;
}
return inCode;
}
function formatDate(date: Date): string {
return `${String(date.getFullYear())}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}`;
}
@ -220,3 +191,11 @@ function trimTrailingBlankLines(lines: string[]): string[] {
while (result[result.length - 1] === "") result.pop();
return result;
}
function yamlFrontmatterString(value: string): string {
return JSON.stringify(value);
}
function yamlFrontmatterInlineList(values: readonly string[]): string {
return `[${values.map(yamlFrontmatterString).join(", ")}]`;
}

View file

@ -0,0 +1,29 @@
import type { Thread } from "./model";
import { threadCommandDisplayTitle } from "./title";
const CODEX_THREAD_LINK_PREFIX = "codex://threads/";
const MAX_THREAD_ID_LENGTH = 160;
export function codexThreadHref(threadId: string): string {
return `${CODEX_THREAD_LINK_PREFIX}${encodeURIComponent(threadId)}`;
}
export function codexThreadIdFromHref(href: string): string | null {
if (!href.startsWith(CODEX_THREAD_LINK_PREFIX)) return null;
const encoded = href.slice(CODEX_THREAD_LINK_PREFIX.length);
if (!encoded || encoded.includes("/") || encoded.includes("?") || encoded.includes("#")) return null;
try {
const threadId = decodeURIComponent(encoded);
return threadId.length > 0 && threadId.length <= MAX_THREAD_ID_LENGTH ? threadId : null;
} catch {
return null;
}
}
export function threadReferenceMarkdown(thread: Thread): string {
return `[${markdownLinkLabel(threadCommandDisplayTitle(thread))}](${codexThreadHref(thread.id)})`;
}
function markdownLinkLabel(value: string): string {
return value.replace(/[\\[\]]/g, "\\$&");
}

View file

@ -6,6 +6,11 @@ export interface Thread {
readonly createdAt: number;
readonly updatedAt: number;
readonly recencyAt?: number | null;
/**
* Whether the loaded app-server thread accepts direct turn input.
* `null` means the capability is unavailable, so panel mode policy decides.
*/
readonly canAcceptDirectInput?: boolean | null;
readonly provenance: ThreadProvenance;
}

View file

@ -1,5 +1,5 @@
import { truncateUtf8, utf8ByteLength } from "../chat/context-budget";
import type { Thread } from "./model";
import { threadDisplayTitle } from "./title";
import type { TurnTranscriptSummary } from "./transcript";
export const REFERENCED_THREAD_TURN_LIMIT = 20;
@ -9,162 +9,70 @@ export interface ReferencedThreadMetadata {
title: string;
includedTurns: number;
turnLimit: number;
omittedTurns?: number;
truncated?: boolean;
}
interface ReferencedThreadEnvelope {
version: 1;
reference: ReferencedThreadMetadata;
visibleText: string;
}
export interface ReferencedThreadPromptBundle {
prompt: string;
referencedThread: ReferencedThreadMetadata;
}
function referencedThreadPrompt(thread: Thread, turns: readonly TurnTranscriptSummary[], userRequest: string): string {
const reference = referencedThreadMetadata(thread, turns.length);
const envelope = referencedThreadEnvelope(reference, userRequest);
const REFERENCED_THREAD_CONTEXT_MAX_BYTES = 18_000;
export function referencedThreadContext(thread: Thread, turns: readonly TurnTranscriptSummary[]): string {
const rendered = turns.map((turn, index) => renderedReferenceTurn(turn, index + 1));
const included: string[] = [];
let bytes = 0;
for (let index = rendered.length - 1; index >= 0; index -= 1) {
const value = rendered[index];
if (value === undefined) continue;
const nextBytes = utf8ByteLength(value);
if (bytes + nextBytes > REFERENCED_THREAD_CONTEXT_MAX_BYTES) {
if (included.length === 0) {
const turn = turns[index];
if (turn) included.unshift(truncatedReferenceTurn(turn, index + 1, REFERENCED_THREAD_CONTEXT_MAX_BYTES));
}
break;
}
included.unshift(value);
bytes += nextBytes;
}
const omittedTurns = turns.length - included.length;
return [
REFERENCED_THREAD_ENVELOPE_START,
JSON.stringify(envelopeMetadata(envelope)),
"Referenced thread context for the current user input:",
`Thread: ${thread.id}`,
`Included turns: ${String(included.length)}`,
`Omitted turns: ${String(omittedTurns)}`,
"",
"Reference thread history:",
...turns.flatMap((turn, index) => {
const lines = [`Turn ${String(index + 1)}:`];
if (turn.userText) lines.push(`User:\n${turn.userText}`);
if (turn.assistantText) lines.push(`Codex:\n${turn.assistantText}`);
return ["", ...lines];
}),
"",
REFERENCED_THREAD_ENVELOPE_END,
"",
"Current user request:",
envelope.visibleText,
...included,
].join("\n");
}
function referencedThreadMetadata(thread: Thread, count: number): ReferencedThreadMetadata {
return {
threadId: thread.id,
title: threadDisplayTitle(thread),
includedTurns: count,
turnLimit: REFERENCED_THREAD_TURN_LIMIT,
};
function renderedReferenceTurn(turn: TurnTranscriptSummary, index: number): string {
const lines = [`Turn ${String(index)}:`];
if (turn.userText) lines.push(`User:\n${turn.userText}`);
if (turn.assistantText) lines.push(`Codex:\n${turn.assistantText}`);
return `${lines.join("\n")}\n\n`;
}
export function referencedThreadPromptBundle(
thread: Thread,
turns: readonly TurnTranscriptSummary[],
userRequest: string,
): ReferencedThreadPromptBundle {
const prompt = referencedThreadPrompt(thread, [...turns], userRequest);
return {
prompt,
referencedThread: referencedThreadMetadata(thread, turns.length),
};
}
export function referencedThreadMetadataFromPrompt(text: string): { text: string; reference: ReferencedThreadMetadata } | null {
const envelope = referencedThreadEnvelopeFromPrompt(text);
return envelope ? { text: envelope.visibleText, reference: envelope.reference } : null;
}
const REFERENCED_THREAD_ENVELOPE_START = "[Codex Panel referenced thread v1]";
const REFERENCED_THREAD_ENVELOPE_END = "[/Codex Panel referenced thread]";
interface ReferencedThreadEnvelopeMetadata {
version: 1;
threadId: string;
title: string;
includedTurns: number;
turnLimit: number;
}
function referencedThreadEnvelope(reference: ReferencedThreadMetadata, visibleText: string): ReferencedThreadEnvelope {
return {
version: 1,
reference,
visibleText,
};
}
function envelopeMetadata(envelope: ReferencedThreadEnvelope): ReferencedThreadEnvelopeMetadata {
return {
version: envelope.version,
threadId: envelope.reference.threadId,
title: envelope.reference.title,
includedTurns: envelope.reference.includedTurns,
turnLimit: envelope.reference.turnLimit,
};
}
function referencedThreadEnvelopeFromPrompt(text: string): ReferencedThreadEnvelope | null {
const headerStart = text.indexOf(REFERENCED_THREAD_ENVELOPE_START);
const headerEnd = text.indexOf(REFERENCED_THREAD_ENVELOPE_END);
const requestMarker = "\nCurrent user request:\n";
const requestStart = text.indexOf(requestMarker);
if (headerStart !== 0 || headerEnd === -1 || requestStart === -1 || requestStart < headerEnd) return null;
const metadataText = firstNonEmptyLine(text.slice(REFERENCED_THREAD_ENVELOPE_START.length, headerEnd));
const metadata = referencedThreadEnvelopeMetadataFromJson(metadataText);
const visibleText = text.slice(requestStart + requestMarker.length).trim();
if (!metadata || !visibleText) return null;
return {
version: 1,
visibleText,
reference: {
threadId: metadata.threadId,
title: metadata.title,
includedTurns: metadata.includedTurns,
turnLimit: metadata.turnLimit,
},
};
}
function firstNonEmptyLine(text: string): string | null {
return (
text
.split(/\r?\n/)
.map((line) => line.trim())
.find((line) => line.length > 0) ?? null
);
}
function referencedThreadEnvelopeMetadataFromJson(text: string | null): ReferencedThreadEnvelopeMetadata | null {
if (!text) return null;
let parsed: unknown;
try {
parsed = JSON.parse(text) as unknown;
} catch {
return null;
}
if (!parsed || typeof parsed !== "object") return null;
const value = parsed as Record<string, unknown>;
if (value["version"] !== 1) return null;
const threadId = stringValue(value["threadId"]);
const title = stringValue(value["title"]);
const includedTurns = finiteNonNegativeInteger(value["includedTurns"]);
const turnLimit = finitePositiveInteger(value["turnLimit"]);
if (!threadId || !title || includedTurns === null || turnLimit === null) return null;
return {
version: 1,
threadId,
title,
includedTurns,
turnLimit,
};
}
function stringValue(value: unknown): string | null {
return typeof value === "string" && value.length > 0 ? value : null;
}
function finiteNonNegativeInteger(value: unknown): number | null {
return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : null;
}
function finitePositiveInteger(value: unknown): number | null {
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : null;
function truncatedReferenceTurn(turn: TurnTranscriptSummary, index: number, maxBytes: number): string {
const user = turn.userText ?? "";
const assistant = turn.assistantText ?? "";
const prefix = `Turn ${String(index)}:\n`;
const userLabel = user ? "User:\n" : "";
const assistantLabel = assistant ? "Codex:\n" : "";
const separator = user && assistant ? "\n" : "";
const suffix = "\n[Turn fields truncated]\n\n";
const fixedBytes = utf8ByteLength(`${prefix}${userLabel}${separator}${assistantLabel}${suffix}`);
const available = Math.max(maxBytes - fixedBytes, 0);
const userBytes = utf8ByteLength(user);
const assistantBytes = utf8ByteLength(assistant);
let assistantBudget = assistant ? Math.min(assistantBytes, Math.floor(available / 2)) : 0;
let userBudget = user ? Math.min(userBytes, available - assistantBudget) : 0;
const remaining = available - userBudget - assistantBudget;
assistantBudget += Math.min(Math.max(assistantBytes - assistantBudget, 0), remaining);
userBudget += Math.min(Math.max(userBytes - userBudget, 0), available - userBudget - assistantBudget);
return [
prefix,
user ? `${userLabel}${truncateUtf8(user, userBudget)}` : "",
separator,
assistant ? `${assistantLabel}${truncateUtf8(assistant, assistantBudget)}` : "",
suffix,
].join("");
}

View file

@ -1,15 +1,25 @@
import type { ThreadTitleContext } from "./title-generation-model";
type ThreadRenameAutoNameState = { kind: "checking" } | { kind: "unavailable" } | { kind: "ready"; context: ThreadTitleContext };
export type ThreadRenameLifecycleState =
| { kind: "idle" }
| { kind: "editing"; draft: string }
| { kind: "generating"; draft: string; originalDraft: string; generationToken: number };
| { kind: "editing"; draft: string; autoName: ThreadRenameAutoNameState }
| { kind: "saving"; draft: string; autoName: ThreadRenameAutoNameState; saveToken: number }
| { kind: "generating"; draft: string; autoName: Extract<ThreadRenameAutoNameState, { kind: "ready" }>; generationToken: number };
export type ThreadRenameActiveState = Exclude<ThreadRenameLifecycleState, { kind: "idle" }>;
export type ThreadRenameGeneratingState = Extract<ThreadRenameLifecycleState, { kind: "generating" }>;
type ThreadRenameGeneratingState = Extract<ThreadRenameLifecycleState, { kind: "generating" }>;
type ThreadRenameSavingState = Extract<ThreadRenameLifecycleState, { kind: "saving" }>;
export type ThreadRenameLifecycleEvent =
| { type: "started"; draft: string }
| { type: "draft-updated"; draft: string }
| { type: "auto-name-context-resolved"; context: ThreadTitleContext | null }
| { type: "cancelled" }
| { type: "save-started"; saveToken: number }
| { type: "save-failed"; saveToken: number }
| { type: "save-succeeded"; saveToken: number }
| { type: "generation-started"; generationToken: number }
| { type: "generation-succeeded"; generationToken: number; draft: string }
| { type: "generation-finished"; generationToken: number }
@ -25,25 +35,40 @@ export function transitionThreadRenameLifecycleState(
): ThreadRenameLifecycleState {
switch (event.type) {
case "started":
return { kind: "editing", draft: event.draft };
if (state.kind === "saving") return state;
return { kind: "editing", draft: event.draft, autoName: { kind: "checking" } };
case "draft-updated":
return state.kind === "idle" ? state : { ...state, draft: event.draft };
return state.kind === "editing" ? { ...state, draft: event.draft } : state;
case "auto-name-context-resolved":
if (state.kind !== "editing" && state.kind !== "saving") return state;
return {
...state,
autoName: event.context ? { kind: "ready", context: event.context } : { kind: "unavailable" },
};
case "cancelled":
return state.kind === "idle" ? state : initialThreadRenameLifecycleState();
return state.kind === "saving" || state.kind === "idle" ? state : initialThreadRenameLifecycleState();
case "save-started":
return state.kind === "editing" ? { ...state, kind: "saving", saveToken: event.saveToken } : state;
case "save-failed":
return threadRenameSaveStillActive(state, event.saveToken)
? { kind: "editing", draft: state.draft, autoName: state.autoName }
: state;
case "save-succeeded":
return threadRenameSaveStillActive(state, event.saveToken) ? initialThreadRenameLifecycleState() : state;
case "generation-started":
if (state.kind !== "editing") return state;
if (state.kind !== "editing" || state.autoName.kind !== "ready") return state;
return {
kind: "generating",
draft: state.draft,
originalDraft: state.draft,
autoName: state.autoName,
generationToken: event.generationToken,
};
case "generation-succeeded":
if (!threadRenameGenerationStillActive(state, event.generationToken) || state.draft !== state.originalDraft) return state;
if (!threadRenameGenerationStillActive(state, event.generationToken)) return state;
return { ...state, draft: event.draft };
case "generation-finished":
if (!threadRenameGenerationStillActive(state, event.generationToken)) return state;
return { kind: "editing", draft: state.draft };
return { kind: "editing", draft: state.draft, autoName: state.autoName };
case "cleared":
return state.kind === "idle" ? state : initialThreadRenameLifecycleState();
default:
@ -58,6 +83,10 @@ export function threadRenameGenerationStillActive(
return state.kind === "generating" && state.generationToken === generationToken;
}
export function threadRenameSaveStillActive(state: ThreadRenameLifecycleState, saveToken: number): state is ThreadRenameSavingState {
return state.kind === "saving" && state.saveToken === saveToken;
}
function unhandledThreadRenameLifecycleEvent(event: never): never {
throw new Error(`Unhandled thread rename lifecycle event: ${String(event)}`);
}

View file

@ -1,4 +1,3 @@
import { shortThreadId } from "./id";
import { type Thread, threadRecencyAt } from "./model";
import { threadDisplayTitle } from "./title";
@ -16,14 +15,18 @@ export type ThreadSearchResolution =
| { kind: "none"; query: string };
const NO_THREAD_SEARCH_MATCH = -1;
const EMPTY_THREAD_SEARCH_SCORE = 5;
const TITLE_EXACT_SCORE = 0;
const TITLE_PREFIX_SCORE = 1;
const TITLE_SUBSTRING_SCORE = 2;
const TITLE_FUZZY_SCORE = 3;
const EMPTY_THREAD_SEARCH_SCORE = 4;
export function threadSearchMatches(threads: readonly Thread[], queryText: string): ThreadSearchMatch[] {
const query = normalizedThreadSearchQuery(queryText);
return threads
.map((thread, index) => {
const title = threadDisplayTitle(thread);
const score = threadSearchScore(thread, title, query);
const score = threadSearchScore(title, query);
return { thread, title, score, recencyAt: threadRecencyAt(thread), index };
})
.filter((match) => match.score !== NO_THREAD_SEARCH_MATCH)
@ -49,16 +52,21 @@ function normalizedThreadSearchQuery(queryText: string): string {
return queryText.trim().toLowerCase();
}
function threadSearchScore(thread: Thread, title: string, query: string): number {
function threadSearchScore(title: string, query: string): number {
if (!query) return EMPTY_THREAD_SEARCH_SCORE;
const id = thread.id.toLowerCase();
const normalizedTitle = title.toLowerCase();
const shortId = shortThreadId(thread.id).toLowerCase();
if (id === query || shortId === query) return 0;
if (normalizedTitle.startsWith(query)) return 1;
if (id.startsWith(query) || shortId.startsWith(query)) return 2;
if (normalizedTitle.includes(query)) return 3;
if (id.includes(query)) return 4;
return NO_THREAD_SEARCH_MATCH;
if (normalizedTitle === query) return TITLE_EXACT_SCORE;
if (normalizedTitle.startsWith(query)) return TITLE_PREFIX_SCORE;
if (normalizedTitle.includes(query)) return TITLE_SUBSTRING_SCORE;
return fuzzySubsequenceMatches(normalizedTitle, query) ? TITLE_FUZZY_SCORE : NO_THREAD_SEARCH_MATCH;
}
function fuzzySubsequenceMatches(title: string, query: string): boolean {
let queryIndex = 0;
for (const character of title) {
if (character === query[queryIndex]) queryIndex += 1;
if (queryIndex === query.length) return true;
}
return false;
}

View file

@ -6,9 +6,6 @@ const DEFAULT_CONTEXT_PAGE_LIMIT = 20;
const DEFAULT_CONTEXT_MAX_PAGES = 5;
export const THREAD_TITLE_MAX_CHARS = 40;
export const THREAD_TITLE_CONTEXT_UNAVAILABLE_MESSAGE =
"Auto-name needs completed history or visible resumed history with both user and assistant text.";
export interface ThreadTitleContext {
userRequest: string;
assistantResponse: string;

View file

@ -2,8 +2,10 @@ import { shortThreadId } from "./id";
import type { Thread } from "./model";
const MAX_ARCHIVED_THREAD_DISPLAY_TITLE_LENGTH = 96;
const MAX_THREAD_COMMAND_DISPLAY_TITLE_LENGTH = 96;
const UNTITLED_THREAD_TITLE = "Untitled thread";
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, { granularity: "grapheme" });
export function threadMeaningfulTitle(thread: Thread): string | null {
for (const value of [thread.name, thread.preview]) {
@ -17,6 +19,10 @@ export function threadDisplayTitle(thread: Thread): string {
return threadMeaningfulTitle(thread) ?? UNTITLED_THREAD_TITLE;
}
export function threadCommandDisplayTitle(thread: Thread): string {
return truncateThreadDisplayTitle(threadDisplayTitle(thread), MAX_THREAD_COMMAND_DISPLAY_TITLE_LENGTH);
}
export function threadRenameDraftTitle(thread: Thread): string {
return threadMeaningfulTitle(thread) ?? "";
}
@ -26,9 +32,7 @@ export function threadArchiveTitle(thread: Thread): string {
}
export function threadArchiveDisplayTitle(thread: Thread): string {
const title = threadArchiveTitle(thread);
if (title.length <= MAX_ARCHIVED_THREAD_DISPLAY_TITLE_LENGTH) return title;
return `${title.slice(0, MAX_ARCHIVED_THREAD_DISPLAY_TITLE_LENGTH - 3).trimEnd()}...`;
return truncateThreadDisplayTitle(threadArchiveTitle(thread), MAX_ARCHIVED_THREAD_DISPLAY_TITLE_LENGTH);
}
export function threadWindowTitle(activeThreadId: string | null, threads: readonly Thread[], fallbackTitle?: string | null): string {
@ -47,3 +51,12 @@ export function threadWindowTitle(activeThreadId: string | null, threads: readon
function normalizeThreadTitleText(value: string | null | undefined): string {
return typeof value === "string" ? value.replace(/\s+/g, " ").trim() : "";
}
function truncateThreadDisplayTitle(title: string, maxLength: number): string {
const graphemes = Array.from(GRAPHEME_SEGMENTER.segment(title), ({ segment }) => segment);
if (graphemes.length <= maxLength) return title;
return `${graphemes
.slice(0, maxLength - 3)
.join("")
.trimEnd()}...`;
}

View file

@ -4,6 +4,13 @@ export interface ThreadTranscriptEntry {
kind: ThreadTranscriptEntryKind;
text: string;
timestamp: number | null;
referencedThread?: ReferencedThreadMetadata;
contexts?: readonly ThreadTranscriptContext[];
}
interface ThreadTranscriptContext {
kind: "web" | "obsidian";
truncated: boolean;
}
export interface TurnTranscriptSummary {
@ -46,3 +53,5 @@ function lastTranscriptText(
}
return null;
}
import type { ReferencedThreadMetadata } from "./reference";

387
src/execution-runtime.ts Normal file
View file

@ -0,0 +1,387 @@
import type { App } from "obsidian";
import type { AppServerClient } from "./app-server/connection/client";
import type { AppServerClientAccess, AppServerClientAccessOptions } from "./app-server/connection/client-access";
import type { AppServerExecutionContext } from "./app-server/connection/execution-context";
import { withShortLivedAppServerClient } from "./app-server/connection/short-lived-client";
import { AppServerQueryCache } from "./app-server/query/cache";
import {
type EphemeralStructuredTurnClient,
type EphemeralStructuredTurnRunner,
runEphemeralStructuredTurn,
} from "./app-server/services/ephemeral-structured-turn";
import { createThreadGoalOperationCoordinator } from "./features/chat/application/threads/goal-actions";
import type { ChatPanelSettingsAccess, ChatRuntimeView, CodexChatHost, WorkspacePanels } from "./features/chat/host/contracts";
import { createAppServerSelectionRewriteTransport } from "./features/selection-rewrite/app-server-transport";
import type { SelectionRewriteTransport } from "./features/selection-rewrite/transport";
import { openThreadPicker, type ThreadPickerController } from "./features/thread-picker/modal.obsidian";
import { createThreadOperationsTransport, createThreadTitleTransport } from "./features/threads/app-server/workflow-transports";
import type { ThreadCatalog } from "./features/threads/catalog/thread-catalog";
import {
createThreadOperationCoordinator,
type ThreadOperationCoordinator,
} from "./features/threads/workflows/thread-operation-coordinator";
import type { ThreadLifecycleEvent } from "./features/threads/workflows/thread-operation-event";
import { projectThreadListChanges } from "./features/threads/workflows/thread-read-model-projection";
import type { ThreadsViewHost, ThreadsViewSettingsAccess } from "./features/threads-view/session";
import type { ThreadsViewPanelActivity } from "./features/threads-view/state";
import type { ThreadsRuntimeView } from "./features/threads-view/view.obsidian";
import { createSettingsAppServerDynamicData } from "./settings/app-server-dynamic-data";
import type { SettingsDynamicDataAccess } from "./settings/dynamic-data";
import type { CodexPanelSettings } from "./settings/model";
import { StaleExecutionRuntimeError } from "./shared/runtime/execution-runtime-lifetime";
import { createKeyedOperationQueue } from "./shared/runtime/keyed-operation-queue";
export interface CodexExecutionRuntimeOptions {
app: App;
context: AppServerExecutionContext;
settings: () => CodexPanelSettings;
workspace: WorkspacePanels;
onThreadLifecycleEvents(events: readonly ThreadLifecycleEvent[]): void;
openNewPanel(): Promise<unknown>;
openThreadInCurrentView(threadId: string): Promise<void>;
openThreadInAvailableView(threadId: string): Promise<void>;
openPanelActivities(): readonly ThreadsViewPanelActivity[];
}
export interface ExecutionRuntimeViews {
readonly chat: readonly ChatRuntimeView[];
readonly threads: readonly ThreadsRuntimeView[];
}
export class CodexExecutionRuntime implements AppServerClientAccess {
private readonly context: Readonly<AppServerExecutionContext>;
private readonly appServerQueries: AppServerQueryCache;
private readonly threadCatalog: ThreadCatalog;
private readonly threadOperationCoordinator: ThreadOperationCoordinator;
readonly settingsDynamicData: SettingsDynamicDataAccess;
private readonly threadNameMutations = createKeyedOperationQueue<string>();
private readonly threadGoalOperations = createThreadGoalOperationCoordinator();
private readonly runtimeSettingsCommitQueue = createKeyedOperationQueue<string>();
private readonly shortLivedClients = new Set<AppServerClient>();
private readonly structuredTurnClients = new Set<EphemeralStructuredTurnClient>();
private readonly structuredTurnOperations = new Set<AbortController>();
private activeThreadPicker: ThreadPickerController | null = null;
private readonly chatViews = new Set<ChatRuntimeView>();
private readonly threadsViews = new Set<ThreadsRuntimeView>();
private disposed = false;
constructor(private readonly options: CodexExecutionRuntimeOptions) {
this.context = Object.freeze({ ...options.context });
this.appServerQueries = new AppServerQueryCache(this.context, this);
this.threadCatalog = this.appServerQueries;
this.threadOperationCoordinator = createThreadOperationCoordinator((events) => {
this.threadCatalog.applyThreadListMutations(projectThreadListChanges(this.threadCatalog, events));
options.onThreadLifecycleEvents(events);
});
this.settingsDynamicData = createSettingsAppServerDynamicData({
vaultPath: this.context.vaultPath,
clientAccess: this,
appServerQueries: this.appServerQueries,
threadCatalog: this.threadCatalog,
threadEvents: this.threadOperationCoordinator,
});
}
private chatHost(): CodexChatHost {
this.assertActive();
return {
appServerClientAccess: this,
appServerContext: this.context,
settings: this.chatSettings(),
workspace: this.options.workspace,
appServerQueries: this.appServerQueries,
threadCatalog: this.threadCatalog,
threadOperationCoordinator: this.threadOperationCoordinator,
threadNameMutations: this.threadNameMutations,
threadTitleTransport: this.threadTitleTransport(),
threadGoalOperations: this.threadGoalOperations,
runtimeSettingsCommitQueue: this.runtimeSettingsCommitQueue,
};
}
private threadsHost(): ThreadsViewHost {
this.assertActive();
return {
settings: this.threadsSettings(),
vaultPath: this.context.vaultPath,
threadCatalog: this.threadCatalog,
threadEvents: this.threadOperationCoordinator,
threadNameMutations: this.threadNameMutations,
threadOperationsTransport: createThreadOperationsTransport(this),
threadTitleTransport: this.threadTitleTransport(),
openNewPanel: () => this.options.openNewPanel(),
openThreadInAvailableView: (threadId) => this.options.openThreadInAvailableView(threadId),
openPanelActivities: () => this.options.openPanelActivities(),
};
}
withClient<T>(operation: (client: AppServerClient) => Promise<T>, options: AppServerClientAccessOptions = {}): Promise<T> {
return this.runWithAppServerClient(operation, options);
}
attachChatView(view: ChatRuntimeView): void {
this.assertActive();
if (this.chatViews.has(view)) return;
this.chatViews.add(view);
view.attachRuntime(this.chatHost());
view.activateRuntime();
}
detachChatView(view: ChatRuntimeView): void {
if (!this.chatViews.delete(view)) return;
view.detachRuntime();
}
attachThreadsView(view: ThreadsRuntimeView): void {
this.assertActive();
if (this.threadsViews.has(view)) return;
this.threadsViews.add(view);
view.attachRuntime(this.threadsHost());
view.activateRuntime();
}
detachThreadsView(view: ThreadsRuntimeView): void {
if (!this.threadsViews.delete(view)) return;
view.detachRuntime();
}
adoptViews(views: ExecutionRuntimeViews): void {
this.assertActive();
for (const view of views.chat) {
if (
this.tryCleanup(() => {
view.attachRuntime(this.chatHost());
})
)
this.chatViews.add(view);
else
this.tryCleanup(() => {
view.detachRuntime();
});
}
for (const view of views.threads) {
if (
this.tryCleanup(() => {
view.attachRuntime(this.threadsHost());
})
)
this.threadsViews.add(view);
else
this.tryCleanup(() => {
view.detachRuntime();
});
}
for (const view of [...this.chatViews]) {
if (
this.tryCleanup(() => {
view.activateRuntime();
})
)
continue;
this.chatViews.delete(view);
this.tryCleanup(() => {
view.detachRuntime();
});
}
for (const view of [...this.threadsViews]) {
if (
this.tryCleanup(() => {
view.activateRuntime();
})
)
continue;
this.threadsViews.delete(view);
this.tryCleanup(() => {
view.detachRuntime();
});
}
}
selectionRewriteTransport(): SelectionRewriteTransport {
return createAppServerSelectionRewriteTransport({
codexPath: this.context.codexPath,
cwd: this.context.vaultPath,
runner: this.structuredTurnRunner(),
});
}
openThreadPicker(): void {
this.activeThreadPicker?.close();
const picker = openThreadPicker(
{
app: this.options.app,
threadCatalog: this.threadCatalog,
openThreadInCurrentView: (threadId) => this.options.openThreadInCurrentView(threadId),
openThreadInAvailableView: (threadId) => this.options.openThreadInAvailableView(threadId),
},
() => {
if (this.activeThreadPicker === picker) this.activeThreadPicker = null;
},
);
this.activeThreadPicker = picker;
}
dispose(): ExecutionRuntimeViews {
if (this.disposed) return { chat: [], threads: [] };
this.disposed = true;
const views: { chat: ChatRuntimeView[]; threads: ThreadsRuntimeView[] } = { chat: [], threads: [] };
for (const view of this.chatViews) {
if (
this.tryCleanup(() => {
view.detachRuntime();
})
)
views.chat.push(view);
}
for (const view of this.threadsViews) {
if (
this.tryCleanup(() => {
view.detachRuntime();
})
)
views.threads.push(view);
}
this.chatViews.clear();
this.threadsViews.clear();
this.tryCleanup(() => {
this.activeThreadPicker?.close();
});
this.activeThreadPicker = null;
for (const operation of this.structuredTurnOperations)
this.tryCleanup(() => {
operation.abort();
});
this.structuredTurnOperations.clear();
for (const client of this.structuredTurnClients)
this.tryCleanup(() => {
client.disconnect();
});
this.structuredTurnClients.clear();
for (const client of this.shortLivedClients)
this.tryCleanup(() => {
client.disconnect();
});
this.shortLivedClients.clear();
this.tryCleanup(() => {
this.appServerQueries.dispose();
});
return views;
}
private async runWithAppServerClient<T>(
operation: (client: AppServerClient) => Promise<T>,
options: AppServerClientAccessOptions = {},
): Promise<T> {
this.assertActive();
const guardedOperation = (client: AppServerClient): Promise<T> => {
this.assertActive();
return operation(client);
};
try {
const result = await withShortLivedAppServerClient(this.context.codexPath, this.context.vaultPath, guardedOperation, options, {
created: (client) => {
if (this.disposed) {
client.disconnect();
throw new StaleExecutionRuntimeError();
}
this.shortLivedClients.add(client);
},
disposed: (client) => {
this.shortLivedClients.delete(client);
},
});
this.assertActive();
return result;
} catch (error) {
if (this.disposed) throw new StaleExecutionRuntimeError();
throw error;
}
}
private structuredTurnRunner(): EphemeralStructuredTurnRunner {
return async (options) => {
this.assertActive();
const operation = new AbortController();
const abort = (): void => {
operation.abort(options.signal?.reason);
};
if (options.signal?.aborted) abort();
else options.signal?.addEventListener("abort", abort, { once: true });
this.structuredTurnOperations.add(operation);
try {
return await runEphemeralStructuredTurn(
{ ...options, signal: operation.signal },
{
clientLifecycle: {
created: (client) => {
if (this.disposed) {
client.disconnect();
throw new StaleExecutionRuntimeError();
}
this.structuredTurnClients.add(client);
},
disposed: (client) => {
this.structuredTurnClients.delete(client);
},
},
},
);
} finally {
options.signal?.removeEventListener("abort", abort);
this.structuredTurnOperations.delete(operation);
}
};
}
private threadTitleTransport() {
return createThreadTitleTransport({
clientAccess: this,
codexPath: this.context.codexPath,
vaultPath: this.context.vaultPath,
threadNamingModel: () => this.options.settings().threadNamingModel,
threadNamingEffort: () => this.options.settings().threadNamingEffort,
runner: this.structuredTurnRunner(),
});
}
private assertActive(): void {
if (this.disposed) throw new StaleExecutionRuntimeError();
}
private tryCleanup(operation: () => void): boolean {
try {
operation();
return true;
} catch {
return false;
}
}
private chatSettings(): ChatPanelSettingsAccess {
return {
referenceActiveNoteOnSend: () => this.options.settings().referenceActiveNoteOnSend,
attachmentFolder: () => this.options.settings().attachmentFolder,
archiveExportEnabled: () => this.options.settings().archiveExportEnabled,
archiveExportSettings: () => ({
archiveExportFolderTemplate: this.options.settings().archiveExportFolderTemplate,
archiveExportFilenameTemplate: this.options.settings().archiveExportFilenameTemplate,
archiveExportTags: this.options.settings().archiveExportTags,
}),
scrollThreadFromComposerEdges: () => this.options.settings().scrollThreadFromComposerEdges,
sendShortcut: () => this.options.settings().sendShortcut,
showToolbar: () => this.options.settings().showToolbar,
};
}
private threadsSettings(): ThreadsViewSettingsAccess {
return {
archiveExportEnabled: () => this.options.settings().archiveExportEnabled,
archiveExportSettings: () => ({
archiveExportFolderTemplate: this.options.settings().archiveExportFolderTemplate,
archiveExportFilenameTemplate: this.options.settings().archiveExportFilenameTemplate,
archiveExportTags: this.options.settings().archiveExportTags,
}),
};
}
}

View file

@ -1,5 +1,4 @@
import type { RequestId, ServerNotification, ServerRequest } from "../../../../app-server/connection/rpc-messages";
import type { AppServerQueryContextIdentity } from "../../../../app-server/query/keys";
import {
routeServerRequest,
serverRequestApprovalResponse,
@ -15,7 +14,7 @@ import {
type PendingUserInput,
} from "../../../../domain/pending-requests/model";
import type { TurnTranscriptSummary } from "../../../../domain/threads/transcript";
import type { ThreadCatalogEvent } from "../../../threads/catalog/thread-catalog";
import type { ThreadOperationEvent } from "../../../threads/workflows/thread-operation-event";
import type { AppServerResourceEvent } from "../../application/connection/server-metadata-actions";
import type { LocalIdSource } from "../../application/local-id-source";
import { activeThreadId, type ChatAction, type ChatState } from "../../application/state/root-reducer";
@ -32,17 +31,16 @@ import { classifyAppServerLog } from "./app-server-logs";
import { type ChatNotificationEffect, planChatNotification } from "./notification-plan";
export interface ChatInboundHandlerActions {
refreshActiveThreads: () => void;
refreshServerDiagnostics: (options?: { forceResourceProbes?: boolean }) => void;
applyAppServerResourceEvent: (event: AppServerResourceEvent) => void;
maybeNameThread: (threadId: string, turnId: string, completedTurnTranscriptSummary: TurnTranscriptSummary | null) => void;
applyThreadCatalogEvent: (event: ThreadCatalogEvent, sourceContext: AppServerQueryContextIdentity) => void;
applyThreadOperationEvent: (event: ThreadOperationEvent) => void;
respondToServerRequest: (requestId: RequestId, result: unknown) => boolean;
rejectServerRequest: (requestId: RequestId, code: number, message: string) => boolean;
}
export interface ChatInboundHandler {
handleNotification(notification: ServerNotification, sourceContext: AppServerQueryContextIdentity): void;
handleNotification(notification: ServerNotification): void;
handleServerRequest(request: ServerRequest): void;
handleAppServerLog(message: string): void;
resolveApproval(requestId: PendingRequestId, action: ApprovalAction): void;
@ -67,8 +65,8 @@ export function createChatInboundHandler(
): ChatInboundHandler {
const context: ChatInboundHandlerContext = { store, actions, localItemIds };
return {
handleNotification: (notification, sourceContext) => {
handleNotification(context, notification, sourceContext);
handleNotification: (notification) => {
handleNotification(context, notification);
},
handleServerRequest: (request) => {
handleServerRequest(context, request);
@ -108,14 +106,10 @@ function dispatch(context: ChatInboundHandlerContext, action: ChatAction): void
context.store.dispatch(action);
}
function handleNotification(
context: ChatInboundHandlerContext,
notification: ServerNotification,
sourceContext: AppServerQueryContextIdentity,
): void {
function handleNotification(context: ChatInboundHandlerContext, notification: ServerNotification): void {
const plan = planChatNotification(state(context), notification, (prefix) => localItemId(context, prefix));
for (const action of plan.actions) dispatch(context, action);
for (const effect of plan.effects) runNotificationEffect(context, effect, sourceContext);
for (const effect of plan.effects) runNotificationEffect(context, effect);
}
function handleServerRequest(context: ChatInboundHandlerContext, request: ServerRequest): void {
@ -243,15 +237,8 @@ function localItemId(context: ChatInboundHandlerContext, prefix: string): string
return context.localItemIds.next(prefix);
}
function runNotificationEffect(
context: ChatInboundHandlerContext,
effect: ChatNotificationEffect,
sourceContext: AppServerQueryContextIdentity,
): void {
function runNotificationEffect(context: ChatInboundHandlerContext, effect: ChatNotificationEffect): void {
switch (effect.type) {
case "refresh-threads":
context.actions.refreshActiveThreads();
return;
case "refresh-server-diagnostics":
context.actions.refreshServerDiagnostics({ forceResourceProbes: effect.forceResourceProbes === true });
return;
@ -261,8 +248,8 @@ function runNotificationEffect(
case "maybe-name-thread":
context.actions.maybeNameThread(effect.threadId, effect.turnId, effect.completedTurnTranscriptSummary);
return;
case "apply-thread-catalog-event":
context.actions.applyThreadCatalogEvent(effect.event, sourceContext);
case "apply-thread-operation-event":
context.actions.applyThreadOperationEvent(effect.event);
return;
}
}

View file

@ -2,17 +2,18 @@ import type { ServerNotification } from "../../../../app-server/connection/rpc-m
import { threadFromAppServerRecord } from "../../../../app-server/services/threads";
import { threadTokenUsageFromRuntimeUsage } from "../../../../domain/runtime/metrics";
import { normalizeExplicitThreadName } from "../../../../domain/threads/model";
import type { ThreadCatalogEvent } from "../../../threads/catalog/thread-catalog";
import type { ThreadOperationEvent } from "../../../threads/workflows/thread-operation-event";
import type { AppServerResourceEvent } from "../../application/connection/server-metadata-actions";
import { activeThreadSettingsAppliedAction } from "../../application/state/actions";
import { activeThreadId, activeThreadState, type ChatAction, type ChatState } from "../../application/state/root-reducer";
import type { SubagentActivityAction } from "../../application/state/subagent-activity";
import { planTurnRuntimeEvents, type TurnRuntimeOutcome } from "../../application/turns/runtime-event-plan";
import type { TurnRuntimeEvent } from "../../application/turns/runtime-events";
import { goalChangeItem } from "../../domain/thread-stream/factories/goal-items";
import { type DiagnosticStatusNotification, routeServerNotification, type ThreadLifecycleNotification } from "./notification-routing";
import { turnRuntimeEventsFromNotification } from "./runtime-events";
export type ChatNotificationEffect =
| { type: "refresh-threads" }
| {
type: "maybe-name-thread";
threadId: string;
@ -21,9 +22,9 @@ export type ChatNotificationEffect =
}
| { type: "refresh-server-diagnostics"; forceResourceProbes?: boolean }
| { type: "apply-app-server-resource-event"; event: AppServerResourceEvent }
| { type: "apply-thread-catalog-event"; event: ThreadCatalogEvent };
| { type: "apply-thread-operation-event"; event: ThreadOperationEvent };
type TurnRuntimeCompletedTurnTranscriptSummary = Extract<TurnRuntimeOutcome, { type: "turn-completed" }>["completedTurnTranscriptSummary"];
type TurnRuntimeCompletedTurnTranscriptSummary = TurnRuntimeOutcome["completedTurnTranscriptSummary"];
export interface ChatNotificationPlan {
actions: readonly ChatAction[];
@ -45,6 +46,7 @@ export function planChatNotification(
});
switch (route.kind) {
case "inactive":
return planTrackedSubagentNotification(state, route.scope.threadId, route.notification, localItemId);
case "ignored":
case "unhandled":
return EMPTY_PLAN;
@ -65,34 +67,188 @@ function runtimeEventsPlan(
notification: Parameters<typeof turnRuntimeEventsFromNotification>[0],
localItemId: LocalItemIdProvider,
): ChatNotificationPlan {
const plan = planTurnRuntimeEvents(state, turnRuntimeEventsFromNotification(notification, localItemId));
const events = turnRuntimeEventsFromNotification(notification, localItemId);
const plan = planTurnRuntimeEvents(state, events);
return {
actions: plan.actions,
actions: [
...plan.actions,
...subagentTrackingActionsFromParentEvents(state, events),
...subagentTrackingActionsFromActivityItem(state, notification),
],
effects: plan.outcomes.flatMap((outcome) => chatNotificationEffectsFromTurnRuntimeOutcome(state, outcome)),
};
}
function subagentTrackingActionsFromActivityItem(
state: ChatState,
notification: Parameters<typeof turnRuntimeEventsFromNotification>[0],
): SubagentActivityAction[] {
if (notification.method !== "item/started" && notification.method !== "item/completed") return [];
const item = notification.params.item;
if (item.type !== "subAgentActivity") return [];
const parentTurnId = activeTurnIdForState(state);
if (!parentTurnId || notification.params.turnId !== parentTurnId) return [];
const tracked: SubagentActivityAction = {
type: "subagent-activity/tracked",
threadId: item.agentThreadId,
parentTurnId,
};
return item.kind === "interrupted"
? [tracked, { type: "subagent-activity/execution-state-changed", threadId: item.agentThreadId, executionState: "failed" }]
: [tracked];
}
function planTrackedSubagentNotification(
state: ChatState,
threadId: string | null,
notification: ServerNotification,
localItemId: LocalItemIdProvider,
): ChatNotificationPlan {
if (!threadId || !state.subagentActivity.byThreadId.has(threadId)) return EMPTY_PLAN;
const events = subagentRuntimeEvents(notification, localItemId);
if (!events) return EMPTY_PLAN;
const actions = events.flatMap((event) => subagentActivityActionsFromRuntimeEvent(threadId, notification.method, event));
return actions.length > 0 ? { actions, effects: [] } : EMPTY_PLAN;
}
function subagentRuntimeEvents(notification: ServerNotification, localItemId: LocalItemIdProvider): readonly TurnRuntimeEvent[] | null {
switch (notification.method) {
case "item/agentMessage/delta":
case "item/plan/delta":
case "turn/plan/updated":
case "item/reasoning/summaryTextDelta":
case "item/reasoning/textDelta":
case "item/reasoning/summaryPartAdded":
case "item/started":
case "item/completed":
case "item/commandExecution/outputDelta":
case "item/fileChange/patchUpdated":
case "turn/diff/updated":
case "hook/started":
case "hook/completed":
case "item/mcpToolCall/progress":
case "item/autoApprovalReview/started":
case "item/autoApprovalReview/completed":
case "guardianWarning":
case "turn/started":
case "turn/completed":
return turnRuntimeEventsFromNotification(notification, localItemId);
default:
return null;
}
}
function subagentActivityActionsFromRuntimeEvent(
threadId: string,
notificationMethod: ServerNotification["method"],
event: TurnRuntimeEvent,
): SubagentActivityAction[] {
switch (event.type) {
case "turnStarted":
return [{ type: "subagent-activity/turn-started", threadId, childTurnId: event.turnId }];
case "turnCompleted":
return [
{
type: "subagent-activity/turn-completed",
threadId,
childTurnId: event.turnId,
items: event.completedItems,
executionState: event.status === "completed" ? "completed" : "failed",
},
];
case "itemUpserted":
return [
{
type: "subagent-activity/item-observed",
threadId,
item: event.item,
advance: notificationMethod === "item/started" || notificationMethod === "turn/plan/updated",
},
];
case "itemCompleted":
return [{ type: "subagent-activity/item-observed", threadId, item: event.item, advance: false }];
case "assistantDelta":
return [
{
type: "subagent-activity/assistant-delta-appended",
threadId,
childTurnId: event.turnId,
itemId: event.itemId,
delta: event.delta,
},
];
case "planDelta":
return [
{
type: "subagent-activity/plan-delta-appended",
threadId,
childTurnId: event.turnId,
itemId: event.itemId,
delta: event.delta,
},
];
case "textDelta":
if (notificationMethod === "item/reasoning/textDelta") return [];
return [
{
type: "subagent-activity/text-delta-appended",
threadId,
childTurnId: event.turnId,
itemId: event.itemId,
label: event.label,
delta: event.delta,
kind: event.kind,
},
];
case "toolOutputDelta":
return [
{
type: "subagent-activity/tool-output-appended",
threadId,
childTurnId: event.turnId,
itemId: event.itemId,
delta: event.delta,
fallbackLabel: event.fallbackLabel,
},
];
case "hookRunObserved":
return event.turnId
? [{ type: "subagent-activity/item-observed", threadId, item: { ...event.item, turnId: event.turnId }, advance: true }]
: [];
case "autoReviewUpdated":
case "reviewWarning":
return [{ type: "subagent-activity/item-observed", threadId, item: event.item, advance: true }];
case "itemOutputDelta":
case "turnDiffUpdated":
case "requestResolved":
case "systemNotice":
return [];
}
}
function subagentTrackingActionsFromParentEvents(state: ChatState, events: readonly TurnRuntimeEvent[]): SubagentActivityAction[] {
const parentTurnId = activeTurnIdForState(state);
if (!parentTurnId) return [];
const threadIds = new Set<string>();
for (const event of events) {
if (event.type !== "itemUpserted" && event.type !== "itemCompleted") continue;
if (event.item.kind !== "agent" || event.item.turnId !== parentTurnId) continue;
for (const threadId of event.item.receiverThreadIds) threadIds.add(threadId);
for (const agent of event.item.agents) threadIds.add(agent.threadId);
}
return [...threadIds].map((threadId) => ({ type: "subagent-activity/tracked", threadId, parentTurnId }));
}
function chatNotificationEffectsFromTurnRuntimeOutcome(state: ChatState, outcome: TurnRuntimeOutcome): readonly ChatNotificationEffect[] {
if (activeThreadState(state)?.lifetime?.kind === "ephemeral") return [];
switch (outcome.type) {
case "turn-started":
return [
{
type: "apply-thread-catalog-event",
event: { type: "thread-touched", threadId: outcome.threadId, recencyAt: outcome.recencyAt },
},
];
case "turn-completed":
return [
{
type: "maybe-name-thread",
threadId: outcome.threadId,
turnId: outcome.turnId,
completedTurnTranscriptSummary: outcome.completedTurnTranscriptSummary,
},
{ type: "refresh-threads" },
];
}
return [
{
type: "maybe-name-thread",
threadId: outcome.threadId,
turnId: outcome.turnId,
completedTurnTranscriptSummary: outcome.completedTurnTranscriptSummary,
},
];
}
function planDiagnosticStatus(notification: DiagnosticStatusNotification): ChatNotificationPlan {
@ -135,17 +291,23 @@ function planThreadLifecycle(
case "thread/started":
return threadStartedPlan(state, notification);
case "thread/archived":
return effectPlan({ type: "apply-thread-catalog-event", event: { type: "thread-archived", threadId: notification.params.threadId } });
return effectPlan({
type: "apply-thread-operation-event",
event: { type: "thread-archived", threadId: notification.params.threadId },
});
case "thread/deleted":
return effectPlan({ type: "apply-thread-catalog-event", event: { type: "thread-deleted", threadId: notification.params.threadId } });
return effectPlan({
type: "apply-thread-operation-event",
event: { type: "thread-deleted", threadId: notification.params.threadId },
});
case "thread/unarchived":
return effectPlan({
type: "apply-thread-catalog-event",
type: "apply-thread-operation-event",
event: { type: "thread-unarchived", threadId: notification.params.threadId },
});
case "thread/name/updated":
return effectPlan({
type: "apply-thread-catalog-event",
type: "apply-thread-operation-event",
event: {
type: "thread-renamed",
threadId: notification.params.threadId,
@ -167,19 +329,25 @@ function threadStartedPlan(
notification: Extract<ThreadLifecycleNotification, { method: "thread/started" }>,
): ChatNotificationPlan {
const thread = threadFromAppServerRecord(notification.params.thread);
const activeParentTurnId = activeTurnIdForState(state);
const trackAction: SubagentActivityAction[] =
thread.provenance.kind === "subagent" && thread.provenance.parentThreadId === activeThreadId(state) && activeParentTurnId
? [{ type: "subagent-activity/tracked", threadId: thread.id, parentTurnId: activeParentTurnId }]
: [];
const effects: ChatNotificationEffect[] =
notification.params.thread.ephemeral || thread.provenance.kind === "subagent"
? []
: [
{
type: "apply-thread-catalog-event",
event: { type: "thread-started", thread },
type: "apply-thread-operation-event",
event: {
type: "thread-upserted",
thread,
forkedFromThreadId: notification.params.thread.forkedFromId,
},
},
];
if (activeThreadId(state) === notification.params.thread.id) {
return { actions: [{ type: "active-thread/cwd-set", cwd: notification.params.thread.cwd }], effects };
}
return { actions: [], effects };
return { actions: trackAction, effects };
}
function threadGoalPlan(

View file

@ -25,7 +25,7 @@ export type ServerNotificationRoute =
| { kind: "userVisibleNotice"; notification: UserVisibleNoticeNotification }
| { kind: "ignored"; notification: ServerNotification }
| { kind: "unhandled"; notification: ServerNotification }
| { kind: "inactive"; notification: ServerNotification };
| { kind: "inactive"; notification: ServerNotification; scope: AppServerRouteScope };
type ServerNotificationScopeExtractors = Partial<{
[Method in ServerNotificationMethod]: (notification: Extract<ServerNotification, { method: Method }>) => AppServerRouteScope;
@ -105,6 +105,9 @@ const IGNORED_SERVER_NOTIFICATION_METHODS = [
"thread/status/changed",
"thread/closed",
"rawResponseItem/completed",
"rawResponse/completed",
"thread/environment/connected",
"thread/environment/disconnected",
"command/exec/outputDelta",
"process/outputDelta",
"process/exited",
@ -157,6 +160,7 @@ const SERVER_NOTIFICATION_SCOPE_EXTRACTORS: ServerNotificationScopeExtractors =
"item/autoApprovalReview/completed": threadTurnNotificationScope,
"item/completed": threadTurnNotificationScope,
"rawResponseItem/completed": threadTurnNotificationScope,
"rawResponse/completed": threadTurnNotificationScope,
"item/agentMessage/delta": threadTurnNotificationScope,
"item/plan/delta": threadTurnNotificationScope,
"command/exec/outputDelta": unscopedNotificationScope,
@ -200,13 +204,15 @@ const SERVER_NOTIFICATION_SCOPE_EXTRACTORS: ServerNotificationScopeExtractors =
"windows/worldWritableWarning": unscopedNotificationScope,
"windowsSandbox/setupCompleted": unscopedNotificationScope,
"account/login/completed": unscopedNotificationScope,
"thread/environment/connected": threadOnlyNotificationScope,
"thread/environment/disconnected": threadOnlyNotificationScope,
};
export function routeServerNotification(notification: ServerNotification, scope: ActiveRouteScope): ServerNotificationRoute {
if (isThreadCatalogNotification(notification)) return { kind: "threadLifecycle", notification };
const routeScope = serverNotificationScope(notification);
if (!isAppServerRouteScopeInActiveRouteScope(routeScope, scope)) return { kind: "inactive", notification };
if (isIdleThreadStreamUpdate(notification, routeScope, scope)) return { kind: "inactive", notification };
if (!isAppServerRouteScopeInActiveRouteScope(routeScope, scope)) return { kind: "inactive", notification, scope: routeScope };
if (isIdleThreadStreamUpdate(notification, routeScope, scope)) return { kind: "inactive", notification, scope: routeScope };
if (isStreamUpdateNotification(notification)) return { kind: "streamUpdate", notification };
if (isTurnLifecycleNotification(notification)) return { kind: "turnLifecycle", notification };

View file

@ -126,7 +126,6 @@ export function turnRuntimeEventsFromNotification(
type: "turnStarted",
threadId: notification.params.threadId,
turnId: notification.params.turn.id,
recencyAt: notification.params.turn.startedAt,
},
];
case "turn/completed":

View file

@ -1,5 +1,4 @@
import { RUNNING_EXECUTION_STATE, type ThreadStreamExecutionState } from "../../../domain/thread-stream/execution-state";
import type { ExecutionState } from "../../../domain/thread-stream/items";
import { type ExecutionState, RUNNING_EXECUTION_STATE, type ThreadStreamExecutionState } from "../../../domain/thread-stream/items";
export { RUNNING_EXECUTION_STATE };

View file

@ -2,13 +2,13 @@ import {
completedTurnTranscriptSummaryFromTurnRecord,
type TurnItem,
type TurnRecord,
turnUserItemText,
turnUserItemProjection,
} from "../../../../../app-server/protocol/turn";
import { jsonPreview } from "../../../../../domain/display/json-preview";
import type { HistoricalTurn } from "../../../../../domain/threads/history";
import { referencedThreadMetadataFromPrompt } from "../../../../../domain/threads/reference";
import type { TurnTranscriptSummary } from "../../../../../domain/threads/transcript";
import { fileMentionsFromInput } from "../../../domain/thread-stream/format/file-mentions";
import { contextAttachmentsFromHistoryContexts } from "../../../domain/thread-stream/format/context-attachments";
import { threadStreamFileReferences } from "../../../domain/thread-stream/format/file-references";
import { normalizeProposedPlanMarkdown } from "../../../domain/thread-stream/format/proposed-plan";
import { userMessageDisplayText } from "../../../domain/thread-stream/format/user-message-text";
import type { CommandThreadStreamTarget, ThreadStreamDiagnosticSection, ThreadStreamItem } from "../../../domain/thread-stream/items";
@ -130,20 +130,23 @@ function turnItemSourceFields(item: { id: string }, turnId?: string): TurnItemSo
}
function userThreadStreamItem(item: UserMessageItem, turnId?: string): ThreadStreamItem {
const text = turnUserItemText(item);
const referencedThread = referencedThreadMetadataFromPrompt(text);
const mentionedFiles = fileMentionsFromInput(item.content);
const projection = turnUserItemProjection(item);
const text = projection.text;
const referencedThread = projection.referencedThread;
const referencedFiles = threadStreamFileReferences(projection.fileReferences);
const contextAttachments = contextAttachmentsFromHistoryContexts(projection.contexts, text);
if (referencedThread) {
return {
...turnItemSourceFields(item, turnId),
kind: "dialogue",
dialogueKind: "user",
role: "user",
text: userMessageDisplayText(referencedThread.text, item.content),
copyText: referencedThread.text,
referencedThread: referencedThread.reference,
text: userMessageDisplayText(text, item.content),
copyText: text,
referencedThread,
...definedProp("clientId", item.clientId),
...(mentionedFiles.length > 0 ? { mentionedFiles } : {}),
...(referencedFiles.length > 0 ? { referencedFiles } : {}),
...(contextAttachments.length > 0 ? { contextAttachments } : {}),
};
}
return {
@ -154,7 +157,8 @@ function userThreadStreamItem(item: UserMessageItem, turnId?: string): ThreadStr
text: userMessageDisplayText(text, item.content),
copyText: text,
...definedProp("clientId", item.clientId),
...(mentionedFiles.length > 0 ? { mentionedFiles } : {}),
...(referencedFiles.length > 0 ? { referencedFiles } : {}),
...(contextAttachments.length > 0 ? { contextAttachments } : {}),
};
}

View file

@ -1,6 +1,5 @@
import type { AppServerClient } from "../../../app-server/connection/client";
import type { AppServerClientAccess, AppServerClientAccessOptions } from "../../../app-server/connection/client-access";
import { withShortLivedAppServerClient } from "../../../app-server/connection/short-lived-client";
import type { CodexInput } from "../../../domain/chat/input";
import type { ComposerInputSnapshot } from "../application/composer/input-snapshot";
import { createThreadReferenceResolver, type ThreadReferenceResolver } from "./thread-reference-resolver";
@ -13,7 +12,7 @@ import {
} from "./transports/session-transports";
export interface ChatCurrentAppServerGatewayHost {
codexPath(): string;
fallbackClientAccess: AppServerClientAccess;
vaultPath: string;
currentClient(): AppServerClient | null;
}
@ -74,7 +73,7 @@ function createCurrentClientAccess(host: ChatCurrentAppServerGatewayHost): AppSe
return {
withClient: async (operation, options: AppServerClientAccessOptions = {}) => {
if (options.serverRequests?.kind === "reject") {
return withShortLivedAppServerClient(host.codexPath(), host.vaultPath, operation, options);
return host.fallbackClientAccess.withClient(operation, options);
}
const client = host.currentClient();

View file

@ -1,9 +1,10 @@
import type { AppServerRequestClient } from "../../../app-server/services/request-client";
import { readReferencedThreadTurnTranscriptSummaries } from "../../../app-server/services/threads";
import { type CodexInput, codexTextInputWithAttachments } from "../../../domain/chat/input";
import { threadReferenceMarkdown } from "../../../domain/threads/deep-link";
import { shortThreadId } from "../../../domain/threads/id";
import type { Thread } from "../../../domain/threads/model";
import { REFERENCED_THREAD_TURN_LIMIT, referencedThreadPromptBundle } from "../../../domain/threads/reference";
import { REFERENCED_THREAD_TURN_LIMIT, referencedThreadContext } from "../../../domain/threads/reference";
import type { ComposerInputSnapshot } from "../application/composer/input-snapshot";
import type { ThreadReferenceInput } from "../application/turns/slash-command-execution";
@ -40,12 +41,20 @@ async function referencedThreadInput(
return null;
}
const messageInput = host.prepareInput(message, snapshot);
const reference = referencedThreadPromptBundle(thread, turns, messageInput.text);
const reference = referencedThreadContext(thread, turns);
const visibleText = `${threadReferenceMarkdown(thread)}\n\n${messageInput.text}`;
host.setStatus(`Referencing ${shortThreadId(thread.id)} (${String(turns.length)}/${String(REFERENCED_THREAD_TURN_LIMIT)} turns).`);
return {
text: messageInput.text,
input: codexTextInputWithAttachments(reference.prompt, messageInput.input),
referencedThread: reference.referencedThread,
text: visibleText,
input: codexTextInputWithAttachments(visibleText, [
{
type: "additionalContext",
key: "codex_panel_referenced_thread",
kind: "untrusted",
value: reference,
},
...messageInput.input,
]),
};
} catch (error) {
if (host.currentClient() !== client) return null;

View file

@ -10,7 +10,6 @@ import {
readThreadGoal,
recordThreadGoalUserMessage,
resumeThread,
rollbackThread,
setThreadGoal,
startThread,
threadActivationSnapshotFromAppServerResponse,
@ -20,8 +19,9 @@ import {
import { interruptTurn, startTurn, steerTurn } from "../../../../app-server/services/turns";
import type { RuntimeSettingsPatch } from "../../../../domain/runtime/thread-settings";
import type { ThreadTurnsPage } from "../../../../domain/threads/history";
import type { EffectOutcome } from "../../application/effect-outcome";
import type { RuntimeSettingsTransport } from "../../application/runtime/settings-transport";
import type { EphemeralThreadTransport } from "../../application/threads/ephemeral-thread-transport";
import type { EphemeralThreadForkResult, EphemeralThreadTransport } from "../../application/threads/ephemeral-thread-transport";
import type { ThreadGoalReadTransport, ThreadGoalTransport } from "../../application/threads/goal-transport";
import type {
ThreadHistoryPage,
@ -29,8 +29,9 @@ import type {
ThreadResumeSnapshot,
ThreadResumeTransport,
} from "../../application/threads/thread-loading-transport";
import type { ThreadMutationTransport, ThreadRollbackSnapshot } from "../../application/threads/thread-mutation-transport";
import type { ThreadMutationTransport } from "../../application/threads/thread-mutation-transport";
import type { ThreadStartTransport } from "../../application/threads/thread-start-transport";
import type { ThreadSubscriptionTransport } from "../../application/threads/thread-subscription-transport";
import type { ChatTurnTransport } from "../../application/turns/turn-transport";
import { threadStreamItemsFromTurns } from "../mappers/thread-stream/turn-items";
@ -65,6 +66,7 @@ export interface ChatConnectedSessionTransports {
readonly threadResume: ThreadResumeTransport;
readonly threadMutation: ThreadMutationTransport;
readonly threadEphemeral: EphemeralThreadTransport;
readonly threadSubscription: ThreadSubscriptionTransport;
readonly threadGoal: ThreadGoalTransport;
}
@ -83,6 +85,7 @@ export function createChatConnectedSessionTransports(host: ChatAppServerTranspor
threadResume: createChatThreadResumeTransport(host),
threadMutation: createChatThreadMutationTransport(host),
threadEphemeral: createChatEphemeralThreadTransport(host),
threadSubscription: createChatThreadSubscriptionTransport(host),
threadGoal: createChatThreadGoalTransport(host),
};
}
@ -90,7 +93,7 @@ export function createChatConnectedSessionTransports(host: ChatAppServerTranspor
function createChatThreadStartTransport(host: ChatCurrentAppServerTransportHost): ThreadStartTransport {
return {
startThread: (request) =>
withCurrentChatAppServerClient(host, async (client) => {
runCurrentChatAppServerEffect(host, async (client) => {
const response = await startThread(client, {
cwd: host.vaultPath,
serviceTier: request.serviceTier,
@ -105,7 +108,7 @@ function createChatTurnTransport(host: ChatAppServerTransportHost): ChatTurnTran
return {
ensureConnected: async () => (await host.connectedClient()) !== null,
startTurn: (request) =>
withCurrentChatAppServerClient(host, async (client) => {
runCurrentChatAppServerEffect(host, async (client) => {
const response = await startTurn(client, {
threadId: request.threadId,
cwd: host.vaultPath,
@ -114,13 +117,10 @@ function createChatTurnTransport(host: ChatAppServerTransportHost): ChatTurnTran
});
return { turnId: response.turn.id };
}),
steerTurn: async (request) => {
const steered = await withCurrentChatAppServerClient(host, async (client) => {
steerTurn: (request) =>
runCurrentChatAppServerEffect(host, async (client) => {
await steerTurn(client, request.threadId, request.turnId, request.input, request.clientUserMessageId);
return true;
});
return steered ?? false;
},
}),
interruptTurn: async (threadId, turnId) => {
const interrupted = await withCurrentChatAppServerClient(host, async (client) => {
await interruptTurn(client, threadId, turnId);
@ -152,48 +152,39 @@ function createChatThreadHistoryTransport(host: CurrentChatAppServerClientHost):
function createChatThreadResumeTransport(host: ChatAppServerTransportHost): ThreadResumeTransport {
return {
resumeThread: (threadId): Promise<ThreadResumeSnapshot | null> =>
withConnectedChatAppServerClient(host, (client) => resumeChatThread(client, threadId, host.vaultPath)),
ensureConnected: async () => (await host.connectedClient()) !== null,
resumeThread: (threadId): Promise<EffectOutcome<ThreadResumeSnapshot>> =>
runCurrentChatAppServerEffect(host, (client) => resumeChatThread(client, threadId, host.vaultPath)),
};
}
function createChatThreadMutationTransport(host: ChatAppServerTransportHost): ThreadMutationTransport {
return {
compactThread: async (threadId) => {
const result = await withConnectedChatAppServerClient(host, async (client) => {
ensureConnected: async () => (await host.connectedClient()) !== null,
compactThread: (threadId) =>
runCurrentChatAppServerEffect(host, async (client) => {
await compactThread(client, threadId);
return true;
});
return result ?? false;
},
forkThread: (threadId, lastTurnId = null) =>
withConnectedChatAppServerClient(host, (client) => forkThread(client, threadId, host.vaultPath, lastTurnId)),
rollbackThread: (threadId) =>
withConnectedChatAppServerClient(host, async (client): Promise<ThreadRollbackSnapshot> => {
const snapshot = await rollbackThread(client, threadId);
return {
thread: snapshot.thread,
cwd: snapshot.cwd,
items: threadStreamItemsFromTurns(snapshot.turns),
};
}),
forkThread: (threadId, options) =>
runCurrentChatAppServerEffect(host, (client) => forkThread(client, threadId, host.vaultPath, options)),
};
}
function createChatEphemeralThreadTransport(host: ChatAppServerTransportHost): EphemeralThreadTransport {
return {
forkEphemeralThread: (sourceThreadId) =>
withConnectedChatAppServerClient(host, async (client) => {
try {
const snapshot = await forkEphemeralThread(client, sourceThreadId, host.vaultPath);
return { kind: "ready" as const, ...snapshot };
} catch (error) {
if (error instanceof EphemeralThreadCleanupRequiredError) {
return { kind: "cleanup-required" as const, threadId: error.threadId };
}
throw error;
}
}),
forkEphemeralThread: async (sourceThreadId) => {
const client = host.currentClient();
if (!client) return { kind: "not-started" };
const value = await forkEphemeralThreadResult(client, sourceThreadId, host.vaultPath);
if (!chatAppServerClientIsStale(host, client)) return { kind: "completed-current", value };
const threadId = value.kind === "ready" ? value.activation.thread.id : value.threadId;
try {
await unsubscribeThread(client, threadId, { timeoutMs: 5_000 });
} catch {
// The superseded connection remains the only valid cleanup context.
}
return { kind: "completed-stale", value };
},
unsubscribeEphemeralThread: async (threadId) => {
const result = await withCurrentChatAppServerClient(host, async (client) => {
await unsubscribeThread(client, threadId, { timeoutMs: 5_000 });
@ -204,6 +195,34 @@ function createChatEphemeralThreadTransport(host: ChatAppServerTransportHost): E
};
}
async function forkEphemeralThreadResult(
client: AppServerRequestClient,
sourceThreadId: string,
vaultPath: string,
): Promise<EphemeralThreadForkResult> {
try {
const snapshot = await forkEphemeralThread(client, sourceThreadId, vaultPath);
return { kind: "ready", ...snapshot };
} catch (error) {
if (error instanceof EphemeralThreadCleanupRequiredError) {
return { kind: "cleanup-required", threadId: error.threadId };
}
throw error;
}
}
function createChatThreadSubscriptionTransport(host: ChatAppServerTransportHost): ThreadSubscriptionTransport {
return {
unsubscribeThread: async (threadId) => {
const result = await withCurrentChatAppServerClient(host, async (client) => {
await unsubscribeThread(client, threadId, { timeoutMs: 5_000 });
return true;
});
return result ?? false;
},
};
}
function createChatThreadGoalReadTransport(host: CurrentChatAppServerClientHost): ThreadGoalReadTransport {
return {
readThreadGoal: (threadId) => readThreadGoalFromCurrentClient(host, threadId),
@ -215,18 +234,12 @@ function createChatThreadGoalTransport(host: ConnectedChatAppServerClientHost):
ensureConnected: async () => (await host.connectedClient()) !== null,
readThreadGoal: (threadId) => readThreadGoalFromCurrentClient(host, threadId),
setThreadGoal: async (threadId, params) => {
const client = await host.connectedClient();
if (!client) return undefined;
const goal = await setThreadGoal(client, threadId, params);
return chatAppServerClientIsStale(host, client) ? undefined : goal;
return runCurrentChatAppServerEffect(host, (client) => setThreadGoal(client, threadId, params));
},
clearThreadGoal: async (threadId) => {
const result = await withConnectedChatAppServerClient(host, async (client) => {
clearThreadGoal: (threadId) =>
runCurrentChatAppServerEffect(host, async (client) => {
await clearThreadGoal(client, threadId);
return true;
});
return result ?? false;
},
}),
recordThreadGoalUserMessage: async (threadId, objective) => {
const result = await withCurrentChatAppServerClient(host, async (client) => {
await recordThreadGoalUserMessage(client, threadId, objective);
@ -241,14 +254,16 @@ function chatAppServerClientIsStale(host: CurrentChatAppServerClientHost, client
return host.currentClient() !== client;
}
async function withConnectedChatAppServerClient<T>(
host: ConnectedChatAppServerClientHost,
function runCurrentChatAppServerEffect<T>(
host: CurrentChatAppServerClientHost,
operation: (client: AppServerClient) => Promise<T>,
): Promise<T | null> {
const client = await host.connectedClient();
if (!client) return null;
const result = await operation(client);
return chatAppServerClientIsStale(host, client) ? null : result;
): Promise<EffectOutcome<T>> {
const client = host.currentClient();
if (!client) return Promise.resolve({ kind: "not-started" });
const effect = operation(client);
return effect.then((value) =>
chatAppServerClientIsStale(host, client) ? { kind: "completed-stale", value } : { kind: "completed-current", value },
);
}
async function withCurrentChatAppServerClient<T>(

View file

@ -1,4 +1,9 @@
import { type CodexInput, type CodexInputItem, codexTextInputWithAttachments, type RequestMention } from "../../../../domain/chat/input";
import {
type CodexInput,
type CodexInputItem,
codexTextInputWithAttachments,
type VaultFileReference,
} from "../../../../domain/chat/input";
type ComposerAttachmentKind = "image" | "file";
@ -21,14 +26,14 @@ export function codexInputWithComposerAttachments(text: string, input: CodexInpu
}
function inputItemsForAttachments(input: readonly CodexInputItem[], attachments: readonly ComposerAttachment[]): CodexInputItem[] {
const seenMentionPaths = new Set(input.flatMap((item) => (item.type === "mention" ? [item.path] : [])));
const seenFileReferencePaths = new Set(input.flatMap((item) => (item.type === "fileReference" ? [item.path] : [])));
const seenLocalImagePaths = new Set(input.flatMap((item) => (item.type === "localImage" ? [item.path] : [])));
const items: CodexInputItem[] = [];
for (const attachment of attachments) {
if (!seenMentionPaths.has(attachment.path)) {
seenMentionPaths.add(attachment.path);
items.push(mentionInputItem(attachment));
if (!seenFileReferencePaths.has(attachment.path)) {
seenFileReferencePaths.add(attachment.path);
items.push(fileReferenceInputItem(attachment));
}
if (attachment.kind === "image" && !seenLocalImagePaths.has(attachment.path)) {
seenLocalImagePaths.add(attachment.path);
@ -38,9 +43,9 @@ function inputItemsForAttachments(input: readonly CodexInputItem[], attachments:
return items;
}
function mentionInputItem(attachment: ComposerAttachment): RequestMention & { type: "mention" } {
function fileReferenceInputItem(attachment: ComposerAttachment): VaultFileReference & { type: "fileReference" } {
return {
type: "mention",
type: "fileReference",
name: attachment.name,
path: attachment.path,
};

View file

@ -34,10 +34,6 @@ export interface ComposerContextReferenceProvider {
dispose(): void;
}
export function emptyComposerContextReferences(): ComposerContextReferences {
return { activeNote: null, selection: null, activeNoteSnapshots: [], selectionSnapshots: [] };
}
export function formatComposerContextRange(range: ComposerContextRange): string {
return `${formatComposerContextPosition(range.from)}-${formatComposerContextPosition(range.to)}`;
}

View file

@ -1,7 +0,0 @@
export interface DailyNoteReferenceCandidate {
keyword: "today" | "tomorrow" | "yesterday";
display: string;
name: string;
path: string;
linktext: string;
}

View file

@ -1,6 +1,7 @@
import type { SkillMetadata } from "../../../../domain/catalog/metadata";
import type { ComposerAttachment } from "./attachments";
import type { ActiveNoteContextReference, ComposerContextReferences, SelectionContextReference } from "./context-references";
import type { ThreadCommandTarget } from "./thread-title-argument";
export interface ComposerInputSnapshot {
readonly sourcePath: string;
@ -10,4 +11,5 @@ export interface ComposerInputSnapshot {
readonly activeNoteSnapshots: readonly ActiveNoteContextReference[];
readonly selectionSnapshots: readonly SelectionContextReference[];
readonly attachments: readonly ComposerAttachment[];
readonly threadCommandTarget?: ThreadCommandTarget;
}

View file

@ -1,15 +1,33 @@
import type { DailyNoteReferenceCandidate } from "./daily-note-references";
import type { NoteCandidate } from "./suggestions";
import type { VaultFileReference } from "../../../../domain/chat/input";
export interface WikiLinkMention {
export interface DailyNoteReferenceCandidate {
keyword: "today" | "tomorrow" | "yesterday";
display: string;
name: string;
path: string;
linktext: string;
}
export interface NoteHeadingCandidate {
heading: string;
linkHeading: string;
level: number;
}
export interface NoteCandidate {
basename: string;
displayName: string;
path: string;
mtime: number;
linktext: string;
headings: NoteHeadingCandidate[];
recentIndex: number | null;
}
export interface NoteCandidateProvider {
candidates(sourcePath: string): readonly NoteCandidate[];
dailyNoteReferences(sourcePath: string): readonly DailyNoteReferenceCandidate[];
tags(): readonly string[];
resolveMention(target: string, sourcePath: string): WikiLinkMention | null;
resolveFileReference(target: string, sourcePath: string): VaultFileReference | null;
dispose(): void;
}

View file

@ -1,3 +1,5 @@
import type { ActivePanelOperation } from "../panel-operation-policy";
type SlashCommandArgsKind =
| "none"
| "optionalThread"
@ -33,7 +35,6 @@ export interface SlashCommandDefinitionShape {
argsKind: SlashCommandArgsKind;
surface: SlashCommandSurface;
detail: string;
sideChatAvailable?: false;
subcommands?: readonly SlashCommandSubcommandDefinition[];
}
@ -79,7 +80,6 @@ export const SLASH_COMMANDS = [
argsKind: "none",
surface: "panelAction",
detail: "Fork the active Codex thread.",
sideChatAvailable: false,
},
{
command: "/btw",
@ -87,7 +87,6 @@ export const SLASH_COMMANDS = [
argsKind: "none",
surface: "panelAction",
detail: "Open a temporary side chat.",
sideChatAvailable: false,
},
{
command: "/rollback",
@ -95,7 +94,6 @@ export const SLASH_COMMANDS = [
argsKind: "none",
surface: "panelAction",
detail: "Roll back the latest turn and restore its prompt.",
sideChatAvailable: false,
},
{ command: "/compact", usage: "/compact", argsKind: "none", surface: "panelAction", detail: "Compact the current thread context." },
{
@ -219,9 +217,53 @@ export function slashCommandRequiresConnection(command: SlashCommandName): boole
return !CONNECTION_INDEPENDENT_SLASH_COMMANDS.has(command);
}
export function slashCommandAvailableInSideChat(command: SlashCommandName): boolean {
const definition = slashCommandDefinition(command);
return !("sideChatAvailable" in definition);
/** Maps a parsed command to the active-panel operation it performs, if any. */
export function activePanelOperationForSlashCommand(command: SlashCommandName, args: string): ActivePanelOperation | null {
switch (command) {
case "goal":
return args.trim() ? "goal-mutation" : "goal-read";
case "compact":
return "compact";
case "fork":
return "fork";
case "rollback":
return "rollback";
case "btw":
return "start-side-chat";
case "fast":
case "auto-review":
case "plan":
return "thread-settings";
case "permissions":
return args ? "permission-settings" : null;
case "model":
case "reasoning":
return args ? "thread-settings" : null;
case "status":
case "doctor":
case "tools":
case "help":
return null;
default:
return "submit";
}
}
/** Maps a slash-completion to its mode-derived operation without assuming arguments. */
export function activePanelOperationForSlashCommandSuggestion(command: SlashCommandName): ActivePanelOperation | null {
switch (command) {
case "goal":
case "compact":
case "fork":
case "rollback":
case "btw":
case "fast":
case "auto-review":
case "plan":
return activePanelOperationForSlashCommand(command, "");
default:
return null;
}
}
export function slashCommandDefinition(command: SlashCommandName): SlashCommandDefinition {

View file

@ -10,6 +10,7 @@ import type { RuntimePermissionProfileSummary } from "../../../../domain/runtime
import { shortThreadId } from "../../../../domain/threads/id";
import type { Thread } from "../../../../domain/threads/model";
import { compareThreadSearchMatches, threadSearchMatches } from "../../../../domain/threads/search";
import { threadCommandDisplayTitle } from "../../../../domain/threads/title";
import {
type ActiveNoteContextReference,
activeNoteContextReferenceMarker,
@ -18,14 +19,15 @@ import {
type SelectionContextReference,
selectionContextReferenceMarker,
} from "./context-references";
import type { DailyNoteReferenceCandidate } from "./daily-note-references";
import type { DailyNoteReferenceCandidate, NoteCandidate, NoteHeadingCandidate } from "./note-context";
import { isSlashCommandName, SLASH_COMMANDS, type SlashCommandName, slashCommandSubcommands } from "./slash-commands";
import {
isSlashCommandName,
SLASH_COMMANDS,
type SlashCommandName,
slashCommandAvailableInSideChat,
slashCommandSubcommands,
} from "./slash-commands";
partialThreadTitleQuery,
quotedThreadTitleArgument,
THREAD_TITLE_COMMANDS,
type ThreadCommandTarget,
type ThreadTitleCommand,
} from "./thread-title-argument";
export interface ComposerSuggestion {
display: string;
@ -37,34 +39,18 @@ export interface ComposerSuggestion {
suffixOnInsert?: string;
activeNoteContext?: ActiveNoteContextReference;
selectionContext?: SelectionContextReference;
threadCommandTarget?: ThreadCommandTarget;
}
export interface ComposerSuggestionOptions {
activeThreadId?: string | null;
activeThreadEphemeral?: boolean;
activeThreadSubagent?: boolean;
slashCommandAvailable?: (command: SlashCommandName) => boolean;
contextReferences?: ComposerContextReferences;
dailyNoteReferences?: readonly DailyNoteReferenceCandidate[] | (() => readonly DailyNoteReferenceCandidate[]);
permissionProfiles?: readonly RuntimePermissionProfileSummary[];
tagCandidates?: readonly string[] | (() => readonly string[]);
}
export interface NoteCandidate {
basename: string;
displayName: string;
path: string;
mtime: number;
linktext: string;
headings: NoteHeadingCandidate[];
recentIndex: number | null;
}
interface NoteHeadingCandidate {
heading: string;
linkHeading: string;
level: number;
}
interface NoteCandidateMatch {
file: NoteCandidate;
match: SearchResult;
@ -73,16 +59,12 @@ interface NoteCandidateMatch {
path: string;
}
const THREAD_SUGGESTION_COMMANDS = ["resume", "refer", "archive", "rename"] as const;
type ThreadSuggestionCommand = (typeof THREAD_SUGGESTION_COMMANDS)[number];
interface ThreadCommandSuggestionPolicy {
excludeActiveThread: boolean;
prioritizeActiveThreadForEmptyQuery: boolean;
}
const THREAD_COMMAND_SUGGESTION_POLICIES: Record<ThreadSuggestionCommand, ThreadCommandSuggestionPolicy> = {
const THREAD_COMMAND_SUGGESTION_POLICIES: Record<ThreadTitleCommand, ThreadCommandSuggestionPolicy> = {
resume: {
excludeActiveThread: true,
prioritizeActiveThreadForEmptyQuery: false,
@ -101,7 +83,7 @@ const THREAD_COMMAND_SUGGESTION_POLICIES: Record<ThreadSuggestionCommand, Thread
},
};
const THREAD_SUGGESTION_COMMAND_PATTERN = new RegExp(`^/(${THREAD_SUGGESTION_COMMANDS.join("|")})\\s+([^\\s\\n]{0,120})$`);
const THREAD_SUGGESTION_COMMAND_PATTERN = new RegExp(`^/(${THREAD_TITLE_COMMANDS.join("|")})\\s+([^\\n]{0,120})$`);
const SELECTION_SUGGESTION_PREVIEW_LIMIT = 500;
export function parseSlashCommand(text: string): { command: SlashCommandName; args: string } | null {
@ -121,14 +103,16 @@ export function activeComposerSuggestions(
currentModel: string | null = null,
options: ComposerSuggestionOptions = {},
): ComposerSuggestion[] {
const slashSuggestions = options.activeThreadSubagent
? null
: (activeSlashSubcommandSuggestions(beforeCursor) ??
activeThreadCommandSuggestions(beforeCursor, threads, options.activeThreadId ?? null) ??
modelOverrideSuggestions(beforeCursor, models) ??
reasoningEffortOverrideSuggestions(beforeCursor, models, currentModel) ??
permissionProfileOverrideSuggestions(beforeCursor, options.permissionProfiles ?? []) ??
activeSlashCommandSuggestions(beforeCursor, options.activeThreadEphemeral ?? false, false));
const slashCommandAvailable = options.slashCommandAvailable ?? (() => true);
const slashSuggestions = slashSuggestionsForActivePanel(
beforeCursor,
threads,
models,
currentModel,
options.activeThreadId ?? null,
options.permissionProfiles ?? [],
slashCommandAvailable,
);
return (
activeWikiLinkSuggestions(beforeCursor, notes) ??
activeContextReferenceSuggestions(beforeCursor, options.contextReferences, options.dailyNoteReferences) ??
@ -139,6 +123,27 @@ export function activeComposerSuggestions(
);
}
function slashSuggestionsForActivePanel(
beforeCursor: string,
threads: readonly Thread[],
models: readonly ModelMetadata[],
currentModel: string | null,
activeThreadId: string | null,
permissionProfiles: readonly RuntimePermissionProfileSummary[],
slashCommandAvailable: (command: SlashCommandName) => boolean,
): ComposerSuggestion[] | null {
const command = /^\/([A-Za-z-]+)/.exec(beforeCursor)?.[1];
if (command && isSlashCommandName(command) && !slashCommandAvailable(command)) return [];
return (
activeSlashSubcommandSuggestions(beforeCursor, slashCommandAvailable) ??
activeThreadCommandSuggestions(beforeCursor, threads, activeThreadId) ??
modelOverrideSuggestions(beforeCursor, models) ??
reasoningEffortOverrideSuggestions(beforeCursor, models, currentModel) ??
permissionProfileOverrideSuggestions(beforeCursor, permissionProfiles) ??
activeSlashCommandSuggestions(beforeCursor, slashCommandAvailable)
);
}
function activeContextReferenceSuggestions(
beforeCursor: string,
references: ComposerContextReferences | undefined,
@ -405,8 +410,7 @@ function compareWikiLinkSuggestionTiebreakers(a: NoteCandidateMatch, b: NoteCand
function activeSlashCommandSuggestions(
beforeCursor: string,
activeThreadEphemeral: boolean,
activeThreadSubagent: boolean,
slashCommandAvailable: (command: SlashCommandName) => boolean,
): ComposerSuggestion[] | null {
const match = /^(\/[A-Za-z-]*)$/.exec(beforeCursor);
if (match?.index === undefined) return null;
@ -414,13 +418,10 @@ function activeSlashCommandSuggestions(
const rawQuery = match[1];
if (rawQuery === undefined) return null;
const query = rawQuery.toLowerCase();
if (activeThreadSubagent) return [];
if (SLASH_COMMANDS.some((item) => item.command.toLowerCase() === query)) return null;
const start = match.index + match[0].lastIndexOf("/");
return SLASH_COMMANDS.filter(
(item) =>
item.command.toLowerCase().startsWith(query) &&
(!activeThreadEphemeral || slashCommandAvailableInSideChat(item.command.slice(1) as SlashCommandName)),
(item) => item.command.toLowerCase().startsWith(query) && slashCommandAvailable(item.command.slice(1) as SlashCommandName),
)
.slice(0, 8)
.map((item) => ({
@ -432,13 +433,17 @@ function activeSlashCommandSuggestions(
}));
}
function activeSlashSubcommandSuggestions(beforeCursor: string): ComposerSuggestion[] | null {
function activeSlashSubcommandSuggestions(
beforeCursor: string,
slashCommandAvailable: (command: SlashCommandName) => boolean,
): ComposerSuggestion[] | null {
const match = /^\/([A-Za-z-]+)\s+([A-Za-z-]{0,120})$/.exec(beforeCursor);
if (!match) return null;
const command = match[1];
const rawQuery = match[2];
if (!command || !isSlashCommandName(command) || rawQuery === undefined) return null;
if (!slashCommandAvailable(command)) return [];
const query = rawQuery.toLowerCase();
const subcommands = slashCommandSubcommands(command);
@ -467,7 +472,6 @@ function activeThreadCommandSuggestions(
const { command, query, start } = completion;
const policy = THREAD_COMMAND_SUGGESTION_POLICIES[command];
if (threads.some((thread) => thread.id.toLowerCase() === query)) return null;
const candidateThreads = threads.filter((thread) => !shouldExcludeActiveThreadSuggestion(policy, thread.id, activeThreadId));
return threadSearchMatches(candidateThreads, query)
@ -476,13 +480,17 @@ function activeThreadCommandSuggestions(
return { ...match, activePriority };
})
.sort((a, b) => a.activePriority - b.activePriority || compareThreadSearchMatches(a, b))
.map(({ thread, title }) => ({
display: title,
detail: shortThreadId(thread.id),
replacement: thread.id,
start,
appendSpaceOnInsert: true,
}));
.map(({ thread }) => {
const title = threadCommandDisplayTitle(thread);
return {
display: title,
detail: shortThreadId(thread.id),
replacement: quotedThreadTitleArgument(title),
start,
appendSpaceOnInsert: true,
threadCommandTarget: { command, threadId: thread.id, title },
};
});
}
function shouldExcludeActiveThreadSuggestion(
@ -502,22 +510,20 @@ function activeThreadSuggestionPriority(
return policy.prioritizeActiveThreadForEmptyQuery && query.length === 0 && activeThreadId !== null && threadId === activeThreadId ? 0 : 1;
}
function activeThreadCommandCompletionQuery(
beforeCursor: string,
): { command: ThreadSuggestionCommand; query: string; start: number } | null {
function activeThreadCommandCompletionQuery(beforeCursor: string): { command: ThreadTitleCommand; query: string; start: number } | null {
const match = THREAD_SUGGESTION_COMMAND_PATTERN.exec(beforeCursor);
if (!match) return null;
const command = threadSuggestionCommand(match[1]);
const rawQuery = match[2];
if (!command || rawQuery === undefined) return null;
const query = rawQuery.trim().toLowerCase();
if (query.length > 0 && /\s$/.test(rawQuery)) return null;
const query = partialThreadTitleQuery(rawQuery);
if (query === null) return null;
return { command, query, start: beforeCursor.length - rawQuery.length };
}
function threadSuggestionCommand(value: string | undefined): ThreadSuggestionCommand | null {
return THREAD_SUGGESTION_COMMANDS.find((command) => command === value) ?? null;
function threadSuggestionCommand(value: string | undefined): ThreadTitleCommand | null {
return THREAD_TITLE_COMMANDS.find((command) => command === value) ?? null;
}
function modelOverrideSuggestions(beforeCursor: string, models: readonly ModelMetadata[]): ComposerSuggestion[] | null {

View file

@ -0,0 +1,77 @@
export const THREAD_TITLE_COMMANDS = ["resume", "refer", "archive", "rename"] as const;
export type ThreadTitleCommand = (typeof THREAD_TITLE_COMMANDS)[number];
export interface ThreadCommandTarget {
readonly command: ThreadTitleCommand;
readonly threadId: string;
readonly title: string;
}
export interface ParsedThreadTitleArgument {
readonly title: string;
readonly rest: string;
}
export function threadCommandTargetForDraft(draft: string, target: ThreadCommandTarget | null): ThreadCommandTarget | null {
if (!target) return null;
const match = /^\/([A-Za-z-]+)(?:\s+([\s\S]*))?$/.exec(draft.trim());
if (match?.[1] !== target.command) return null;
const parsed = parseThreadTitleArgument(match[2] ?? "");
return parsed?.title === target.title ? target : null;
}
export function quotedThreadTitleArgument(title: string): string {
return `"${title.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
}
export function parseThreadTitleArgument(input: string): ParsedThreadTitleArgument | null {
const source = input.trimStart();
if (!source) return null;
if (!source.startsWith('"')) {
const match = /^(\S+)([\s\S]*)$/.exec(source);
const title = match?.[1];
const rest = match?.[2];
return title === undefined || rest === undefined ? null : { title, rest: rest.trimStart() };
}
let title = "";
for (let index = 1; index < source.length; index += 1) {
const character = source.charAt(index);
if (character === "\\") {
const escaped = source.charAt(index + 1);
if (!escaped) return null;
if (escaped !== "\\" && escaped !== '"') title += "\\";
title += escaped;
index += 1;
continue;
}
if (character === '"') {
const rest = source.slice(index + 1);
if (rest && !/^\s/.test(rest)) return null;
return { title, rest: rest.trimStart() };
}
title += character;
}
return null;
}
export function partialThreadTitleQuery(input: string): string | null {
if (!input.startsWith('"')) return /\s/.test(input) ? null : input;
let query = "";
for (let index = 1; index < input.length; index += 1) {
const character = input.charAt(index);
if (character === "\\") {
const escaped = input.charAt(index + 1);
if (!escaped) return query;
if (escaped !== "\\" && escaped !== '"') query += "\\";
query += escaped;
index += 1;
continue;
}
if (character === '"') return null;
query += character;
}
return query;
}

View file

@ -1,16 +1,17 @@
import { parseLinktext } from "obsidian";
import type { SkillMetadata } from "../../../../domain/catalog/metadata";
import {
ACTIVE_FILE_MENTION_NAME,
ACTIVE_FILE_REFERENCE_NAME,
type CodexInput,
codexTextInputWithMentions,
codexTextInputWithReferences,
type RequestAdditionalContext,
type RequestMention,
type SkillReference,
type VaultFileReference,
} from "../../../../domain/chat/input";
import { type MarkdownCodeRange, markdownCodeRangeContainsOffset, markdownCodeRanges } from "../../../../domain/markdown/code-ranges";
import {
type ActiveNoteContextReference,
type ComposerContextReferences,
formatComposerContextRange,
type SelectionContextReference,
selectionContextReferenceMarker,
} from "./context-references";
@ -22,7 +23,13 @@ interface ParsedWikiLink {
display: string;
}
export type WikiLinkMentionResolver = (target: string) => { name: string; path: string } | null;
interface ObsidianReference {
marker: string;
path: string;
excerpt?: string;
}
export type WikiLinkFileReferenceResolver = (target: string) => VaultFileReference | null;
const OBSIDIAN_CONTEXT_ADDITIONAL_CONTEXT_KEY = "codex_panel_obsidian_context";
const WIKILINK_PATTERN = /\[\[([^\]\n]+?)\]\]/g;
@ -37,10 +44,11 @@ interface PreparedComposerInputOptions {
referenceActiveNoteOnSend: boolean;
}
function parsedWikiLinks(text: string): ParsedWikiLink[] {
function parsedWikiLinks(text: string, codeRanges: readonly MarkdownCodeRange[]): ParsedWikiLink[] {
const links: ParsedWikiLink[] = [];
const seen = new Set<string>();
for (const match of text.matchAll(WIKILINK_PATTERN)) {
if (markdownCodeRangeContainsOffset(codeRanges, match.index)) continue;
const rawValue = match[1];
if (rawValue === undefined) continue;
const raw = rawValue.trim();
@ -54,41 +62,42 @@ function parsedWikiLinks(text: string): ParsedWikiLink[] {
return links;
}
export function preparedUserInputWithWikiLinkMentionsSkillsAndContext(
export function preparedUserInputWithWikiLinkReferencesSkillsAndContext(
text: string,
resolveMention: WikiLinkMentionResolver,
resolveFileReference: WikiLinkFileReferenceResolver,
skills: readonly SkillMetadata[],
contextReferences: ComposerContextReferences,
options: PreparedComposerInputOptions,
): PreparedComposerInput {
const contextReplacement = textWithContextReferences(text, contextReferences);
const codeRanges = markdownReferenceCodeRanges(text);
const contextReplacement = textWithContextReferences(text, contextReferences, codeRanges);
const resolvedText = contextReplacement.text;
const mentions: RequestMention[] = [];
const wikilinkMappings: string[] = [];
const fileReferences: VaultFileReference[] = [];
const wikilinkReferences: ObsidianReference[] = [];
const seenPaths = new Set<string>();
const activeNoteSnapshots = contextReferences.activeNoteSnapshots ?? [];
for (const link of parsedWikiLinks(resolvedText)) {
const mention = activeNoteMentionForLink(link, activeNoteSnapshots) ?? resolveMention(link.target);
if (!mention || seenPaths.has(mention.path)) continue;
seenPaths.add(mention.path);
mentions.push(mention);
wikilinkMappings.push(`- [[${link.raw}]] -> ${mention.path}`);
for (const link of parsedWikiLinks(resolvedText, codeRanges)) {
const fileReference = activeNoteFileReferenceForLink(link, activeNoteSnapshots) ?? resolveFileReference(link.target);
if (!fileReference || seenPaths.has(fileReference.path)) continue;
seenPaths.add(fileReference.path);
fileReferences.push(fileReference);
wikilinkReferences.push({ marker: `[[${link.raw}]]`, path: fileReference.path });
}
for (const selection of contextReplacement.selections) {
if (seenPaths.has(selection.path)) continue;
seenPaths.add(selection.path);
mentions.push({ name: selection.name, path: selection.path });
fileReferences.push({ name: selection.name, path: selection.path });
}
const attachedActiveNote = options.referenceActiveNoteOnSend ? contextReferences.activeNote : null;
if (attachedActiveNote) mentions.push({ name: ACTIVE_FILE_MENTION_NAME, path: attachedActiveNote.path });
if (attachedActiveNote) fileReferences.push({ name: ACTIVE_FILE_REFERENCE_NAME, path: attachedActiveNote.path });
const skillByName = firstEnabledSkillByName(skills);
const resolvedSkills: RequestMention[] = [];
const resolvedSkills: SkillReference[] = [];
const seenSkillPaths = new Set<string>();
for (const reference of parsedSkillReferences(resolvedText)) {
for (const reference of parsedSkillReferences(resolvedText, codeRanges)) {
const skill = skillByName.get(reference.toLowerCase());
if (!skill || seenSkillPaths.has(skill.path)) continue;
seenSkillPaths.add(skill.path);
@ -97,16 +106,16 @@ export function preparedUserInputWithWikiLinkMentionsSkillsAndContext(
return {
text: resolvedText,
input: codexTextInputWithMentions(
input: codexTextInputWithReferences(
resolvedText,
mentions,
fileReferences,
resolvedSkills,
additionalContext(wikilinkMappings, contextReplacement.selections, attachedActiveNote),
additionalContext(wikilinkReferences, contextReplacement.selections, attachedActiveNote),
),
};
}
function activeNoteMentionForLink(link: ParsedWikiLink, snapshots: readonly ActiveNoteContextReference[]): RequestMention | null {
function activeNoteFileReferenceForLink(link: ParsedWikiLink, snapshots: readonly ActiveNoteContextReference[]): VaultFileReference | null {
const snapshot = snapshots.find((item) => link.raw.trim() === item.linktext && link.target === item.linktext);
return snapshot ? { name: snapshot.name, path: snapshot.path } : null;
}
@ -114,19 +123,24 @@ function activeNoteMentionForLink(link: ParsedWikiLink, snapshots: readonly Acti
function textWithContextReferences(
text: string,
contextReferences: ComposerContextReferences,
codeRanges: readonly MarkdownCodeRange[],
): { text: string; selections: SelectionContextReference[] } {
return {
text,
selections: selectionsReferencedByText(text, contextReferences.selectionSnapshots ?? []),
selections: selectionsReferencedByText(text, contextReferences.selectionSnapshots ?? [], codeRanges),
};
}
function selectionsReferencedByText(text: string, snapshots: readonly SelectionContextReference[]): SelectionContextReference[] {
function selectionsReferencedByText(
text: string,
snapshots: readonly SelectionContextReference[],
codeRanges: readonly MarkdownCodeRange[],
): SelectionContextReference[] {
const selections: SelectionContextReference[] = [];
const seen = new Set<string>();
for (const snapshot of snapshots) {
const marker = selectionContextReferenceMarker(snapshot);
if (!text.includes(marker) || seen.has(marker)) continue;
if (!includesOutsideMarkdownCode(text, marker, codeRanges) || seen.has(marker)) continue;
seen.add(marker);
selections.push(snapshot);
}
@ -134,67 +148,72 @@ function selectionsReferencedByText(text: string, snapshots: readonly SelectionC
}
function additionalContext(
wikilinkMappings: readonly string[],
wikilinkReferences: readonly ObsidianReference[],
selections: readonly SelectionContextReference[],
activeNote: ActiveNoteContextReference | null,
): RequestAdditionalContext[] {
return obsidianContextAdditionalContext(wikilinkMappings, selections, activeNote);
return obsidianContextAdditionalContext(obsidianReferences(wikilinkReferences, selections, activeNote));
}
function obsidianContextAdditionalContext(
wikilinkMappings: readonly string[],
function obsidianReferences(
wikilinkReferences: readonly ObsidianReference[],
selections: readonly SelectionContextReference[],
activeNote: ActiveNoteContextReference | null,
): RequestAdditionalContext[] {
if (wikilinkMappings.length === 0 && selections.length === 0 && !activeNote) return [];
): ObsidianReference[] {
const selectionReferences = selections.map((selection) => ({
marker: selectionContextReferenceMarker(selection),
path: selection.path,
excerpt: selection.text,
}));
const selectedWikilinks = new Set(selections.map((selection) => referenceKey(`[[${selection.linktext}]]`, selection.path)));
return [
...wikilinkReferences.filter((reference) => !selectedWikilinks.has(referenceKey(reference.marker, reference.path))),
...selectionReferences,
...(activeNote ? [{ marker: ACTIVE_FILE_REFERENCE_NAME, path: activeNote.path }] : []),
];
}
function referenceKey(marker: string, path: string): string {
return `${marker}\u0000${path}`;
}
function obsidianContextAdditionalContext(references: readonly ObsidianReference[]): RequestAdditionalContext[] {
if (references.length === 0) return [];
return [
{
key: OBSIDIAN_CONTEXT_ADDITIONAL_CONTEXT_KEY,
kind: "untrusted",
value: obsidianContextValue(wikilinkMappings, selections, activeNote),
value: obsidianContextValue(references),
},
];
}
function obsidianContextValue(
wikilinkMappings: readonly string[],
selections: readonly SelectionContextReference[],
activeNote: ActiveNoteContextReference | null,
): string {
const sections: string[][] = [];
if (wikilinkMappings.length > 0) sections.push(["Resolved wikilinks:", ...wikilinkMappings]);
if (selections.length > 0) {
sections.push(["Referenced selections:", ...selectionReferenceLines(selections), "", ...selectionBodyLines(selections)]);
}
if (activeNote) sections.push(activeNoteContextLines(activeNote));
function obsidianContextValue(references: readonly ObsidianReference[]): string {
const excerpts = references.filter((reference): reference is ObsidianReference & { excerpt: string } => reference.excerpt !== undefined);
return [
"Obsidian context for the current user input:",
...sections.flatMap((section, index) => (index === 0 ? section : ["", ...section])),
"Obsidian references for the current user input:",
...references.map(referenceLine),
...(excerpts.length > 0 ? ["", "Inline excerpts:", ...excerptLines(excerpts)] : []),
].join("\n");
}
function selectionReferenceLines(selections: readonly SelectionContextReference[]): string[] {
return selections.map((selection) => {
const location = `${selection.path} ${formatComposerContextRange(selection.range)}`;
return `- ${selectionContextReferenceMarker(selection)} -> ${location}`;
});
function referenceLine(reference: ObsidianReference): string {
return `- ${reference.marker} -> ${reference.path}${reference.excerpt === undefined ? "" : " (inline excerpt below)"}`;
}
function selectionBodyLines(selections: readonly SelectionContextReference[]): string[] {
return selections.flatMap((selection, index) => {
function excerptLines(references: readonly (ObsidianReference & { excerpt: string })[]): string[] {
return references.flatMap((reference, index) => {
const prefix = index === 0 ? [] : [""];
return [...prefix, `${selectionContextReferenceMarker(selection)}:`, selection.text];
return [...prefix, `${reference.marker}:`, reference.excerpt];
});
}
function activeNoteContextLines(activeNote: ActiveNoteContextReference): string[] {
return ["Referenced active file:", `- ${ACTIVE_FILE_MENTION_NAME} -> ${activeNote.path}`];
}
function parsedSkillReferences(text: string): string[] {
function parsedSkillReferences(text: string, codeRanges: readonly MarkdownCodeRange[]): string[] {
const references: string[] = [];
const seen = new Set<string>();
for (const match of text.matchAll(SKILL_REFERENCE_PATTERN)) {
const prefix = match[1] ?? "";
if (markdownCodeRangeContainsOffset(codeRanges, match.index + prefix.length)) continue;
const name = match[2];
if (name === undefined) continue;
const key = name.toLowerCase();
@ -205,6 +224,19 @@ function parsedSkillReferences(text: string): string[] {
return references;
}
function markdownReferenceCodeRanges(text: string): MarkdownCodeRange[] {
return text.includes("[[") || text.includes("$") ? markdownCodeRanges(text) : [];
}
function includesOutsideMarkdownCode(text: string, value: string, codeRanges: readonly MarkdownCodeRange[]): boolean {
let index = text.indexOf(value);
while (index >= 0) {
if (!markdownCodeRangeContainsOffset(codeRanges, index)) return true;
index = text.indexOf(value, index + value.length);
}
return false;
}
function parseWikiLink(raw: string): ParsedWikiLink | null {
const trimmed = raw.trim();
if (!trimmed) return null;

View file

@ -7,22 +7,23 @@ const STATUS_CONNECTION_STARTING = "Starting Codex app-server...";
const STATUS_CONNECTED = "Connected.";
const STATUS_CONNECTION_FAILED = "Connection failed.";
export interface ChatConnectionAdapter {
interface ChatConnectionAdapter {
connect(): Promise<ServerInitialization>;
isConnected(): boolean;
}
export interface ChatConnectionMetadataActions {
interface ChatConnectionMetadataActions {
refreshAppServerMetadata: () => Promise<unknown>;
}
export interface ChatConnectionDiagnosticsActions {
interface ChatConnectionDiagnosticsActions {
refreshServerDiagnostics: (options?: { appServerMetadataSnapshot?: boolean; forceResourceProbes?: boolean }) => Promise<void>;
}
export interface ChatConnectionActionsHost {
stateStore: ChatStateStore;
connection: ChatConnectionAdapter;
canConnect: () => boolean;
metadata: ChatConnectionMetadataActions;
diagnostics: ChatConnectionDiagnosticsActions;
invalidateThreadWork: () => void;
@ -34,15 +35,14 @@ export interface ChatConnectionActionsHost {
setStatus: (statusText: string, phase?: ChatConnectionPhase) => void;
addSystemMessage: (text: string) => void;
configuredCommand: () => string;
refreshLiveState: () => void;
isStaleConnectionError: (error: unknown) => boolean;
isStaleResourceContextError: (error: unknown) => boolean;
isStaleRuntimeError: (error: unknown) => boolean;
notifyConnectionFailed: () => void;
}
type ChatConnectionExitHost = Pick<
ChatConnectionActionsHost,
"invalidateThreadWork" | "setStatus" | "stateStore" | "resetThreadTurnPresence" | "refreshLiveState"
"invalidateThreadWork" | "setStatus" | "stateStore" | "resetThreadTurnPresence"
>;
function handleChatConnectionExit(host: ChatConnectionExitHost): void {
@ -50,10 +50,10 @@ function handleChatConnectionExit(host: ChatConnectionExitHost): void {
host.setStatus(STATUS_CONNECTION_STOPPED, { kind: "disconnected", message: STATUS_CONNECTION_STOPPED });
host.stateStore.dispatch({ type: "connection/scoped-cleared" });
host.resetThreadTurnPresence(false);
host.refreshLiveState();
}
export interface ChatConnectionActions {
ensureInitialized(): Promise<void>;
ensureConnected(): Promise<void>;
invalidate(): void;
handleExit(): void;
@ -64,28 +64,40 @@ export interface ChatConnectionActions {
export function createChatConnectionActions(host: ChatConnectionActionsHost): ChatConnectionActions {
let generation = 0;
let activeConnection: { generation: number; promise: Promise<void> } | null = null;
let activeConnection: { generation: number; initialization: Promise<void>; hydration: Promise<void> } | null = null;
const invalidate = (): void => {
generation += 1;
activeConnection = null;
};
const isStale = (candidateGeneration: number): boolean => candidateGeneration !== generation;
const startConnection = (): NonNullable<typeof activeConnection> => {
const connectionGeneration = generation;
const connectionIsStale = (): boolean => isStale(connectionGeneration);
const initialization = initializeConnection(host, connectionIsStale);
const hydration = initialization.then(async () => {
if (connectionIsStale() || !host.connection.isConnected()) return;
await hydrateConnectedResources(host, connectionIsStale);
});
const active = { generation: connectionGeneration, initialization, hydration };
activeConnection = active;
const clear = (): void => {
if (activeConnection === active) activeConnection = null;
};
void hydration.then(clear, clear);
return active;
};
const actions: ChatConnectionActions = {
ensureConnected: async () => {
if (activeConnection) return activeConnection.promise;
ensureInitialized: async () => {
if (!host.canConnect()) return;
if (activeConnection) return activeConnection.initialization;
if (host.connection.isConnected()) return;
const connectionGeneration = generation;
const promise = initializeConnection(host, () => isStale(connectionGeneration));
const active = { generation: connectionGeneration, promise };
activeConnection = active;
try {
await promise;
} finally {
if (activeConnection === active) {
activeConnection = null;
}
}
await startConnection().initialization;
},
ensureConnected: async () => {
if (!host.canConnect()) return;
if (activeConnection) return activeConnection.hydration;
if (host.connection.isConnected()) return;
await startConnection().hydration;
},
invalidate,
handleExit: () => {
@ -105,7 +117,7 @@ async function refreshActiveThreads(host: ChatConnectionActionsHost): Promise<vo
await host.refreshSharedThreads();
host.refreshTabHeader();
} catch (error) {
if (host.isStaleResourceContextError(error)) return;
if (host.isStaleRuntimeError(error)) return;
host.addSystemMessage(error instanceof Error ? error.message : String(error));
}
}
@ -143,22 +155,20 @@ async function initializeConnection(host: ChatConnectionActionsHost, isStale: ()
} catch (error) {
if (isStale()) return;
if (host.isStaleConnectionError(error)) return;
if (host.isStaleResourceContextError(error)) return;
if (host.isStaleRuntimeError(error)) return;
const message = connectionErrorMessage(error, host.configuredCommand());
host.setStatus(STATUS_CONNECTION_FAILED, { kind: "failed", message });
host.addSystemMessage(message);
host.notifyConnectionFailed();
return;
}
await hydrateConnectedResources(host, isStale);
}
async function hydrateConnectedResources(host: ChatConnectionActionsHost, isStale: () => boolean): Promise<void> {
try {
await host.metadata.refreshAppServerMetadata();
} catch (error) {
if (isStale() || host.isStaleResourceContextError(error)) return;
if (isStale() || host.isStaleRuntimeError(error)) return;
host.addSystemMessage(`Could not refresh Codex metadata: ${errorMessage(error)}`);
}
if (isStale()) return;
@ -166,7 +176,7 @@ async function hydrateConnectedResources(host: ChatConnectionActionsHost, isStal
try {
await host.refreshSharedThreads();
} catch (error) {
if (isStale() || host.isStaleResourceContextError(error)) return;
if (isStale() || host.isStaleRuntimeError(error)) return;
host.addSystemMessage(`Could not refresh Codex threads: ${errorMessage(error)}`);
}
if (isStale()) return;

View file

@ -1,3 +1,4 @@
import { capturePanelTargetLease, panelTargetLeaseIsCurrent } from "../state/panel-target";
import { activeThreadId, activeThreadState, type ChatConnectionPhase, panelThreadId } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
@ -5,42 +6,43 @@ const STATUS_RECONNECTING = "Reconnecting...";
export interface ChatReconnectActionsHost {
stateStore: ChatStateStore;
invalidateConnectionWork: () => void;
invalidateThreadWork: () => void;
clearDeferredDiagnostics: () => void;
resetConnection: () => void;
resetConnectionScope: () => void;
setStatus: (statusText: string, phase?: ChatConnectionPhase) => void;
ensureConnected: () => Promise<void>;
isConnected: () => boolean;
resumeThread: (threadId: string) => Promise<void>;
resumeThread: (threadId: string) => Promise<boolean>;
addSystemMessage: (text: string) => void;
}
export async function reconnectPanel(
host: ChatReconnectActionsHost,
target: { resumeThreadId: string | null; isCurrent?: () => boolean } | null = null,
): Promise<boolean> {
export function createReconnectPanelAction(host: ChatReconnectActionsHost): () => Promise<boolean> {
let activeReconnect: Promise<boolean> | null = null;
return async () => {
if (activeReconnect) return activeReconnect;
const operation = reconnectPanel(host);
activeReconnect = operation;
try {
return await operation;
} finally {
if (activeReconnect === operation) activeReconnect = null;
}
};
}
async function reconnectPanel(host: ChatReconnectActionsHost): Promise<boolean> {
const currentState = host.stateStore.getState();
const threadId = target
? target.resumeThreadId
: activeThreadState(currentState)?.lifetime?.kind === "ephemeral"
? null
: panelThreadId(currentState);
const isCurrent = target?.isCurrent ?? (() => true);
const panelTarget = capturePanelTargetLease(currentState);
const threadId = activeThreadState(currentState)?.lifetime?.kind === "ephemeral" ? null : panelThreadId(currentState);
host.stateStore.dispatch({ type: "ui/panel-set", panel: null });
host.invalidateConnectionWork();
host.invalidateThreadWork();
host.clearDeferredDiagnostics();
host.resetConnection();
host.resetConnectionScope();
host.stateStore.dispatch({ type: "connection/scoped-cleared" });
host.setStatus(STATUS_RECONNECTING, { kind: "connecting" });
await host.ensureConnected();
if (!isCurrent() || !host.isConnected()) return false;
if (!host.isConnected() || !panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget)) return false;
if (!threadId) return true;
try {
await host.resumeThread(threadId);
if (!isCurrent()) return false;
if (!(await host.resumeThread(threadId))) return false;
if (!panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget)) return false;
return activeThreadId(host.stateStore.getState()) === threadId;
} catch (error) {
host.addSystemMessage(error instanceof Error ? error.message : String(error));

View file

@ -1,10 +1,11 @@
import {
cloneServerDiagnostics,
diagnosticsWithMetadataResourceProbes,
diagnosticsWithProbe,
upsertMcpServerDiagnostic,
} from "../../../../domain/server/diagnostics";
import type { McpServerStartupStatus } from "../../../../domain/server/mcp-status";
import type { SharedServerMetadata } from "../../../../domain/server/metadata";
import type { SharedServerMetadata, SharedServerMetadataResource } from "../../../../domain/server/metadata";
import type { ChatStateStore } from "../state/store";
export type AppServerResourceEvent =
@ -15,22 +16,22 @@ export type AppServerResourceEvent =
export interface ServerMetadataActionsHost {
stateStore: ChatStateStore;
appServerMetadataSnapshot: () => SharedServerMetadata | null;
refreshAppServerMetadata: () => Promise<SharedServerMetadata | null>;
refreshSkills: () => Promise<SharedServerMetadata | null>;
refreshRateLimits: () => Promise<SharedServerMetadata | null>;
isStaleResourceContextError: (error: unknown) => boolean;
refreshAppServerMetadata: () => Promise<void>;
refreshSkills: () => Promise<void>;
refreshRateLimits: () => Promise<void>;
isStaleRuntimeError: (error: unknown) => boolean;
}
export interface ServerMetadataActions {
applyAppServerMetadata: (metadata: SharedServerMetadata) => void;
refreshAppServerMetadata: () => Promise<SharedServerMetadata | null>;
applyAppServerMetadataResource: (resource: SharedServerMetadataResource) => void;
refreshAppServerMetadata: () => Promise<void>;
applyAppServerResourceEvent: (event: AppServerResourceEvent) => Promise<void>;
}
export function createServerMetadataActions(host: ServerMetadataActionsHost): ServerMetadataActions {
return {
applyAppServerMetadata: (metadata) => {
applyAppServerMetadata(host, metadata);
applyAppServerMetadataResource: (resource) => {
applyAppServerMetadataResource(host, resource);
},
refreshAppServerMetadata: () => refreshAppServerMetadata(host),
applyAppServerResourceEvent: async (event) => {
@ -56,53 +57,65 @@ async function applyAppServerResourceEvent(host: ServerMetadataActionsHost, even
if (event.name.length > 0) {
applyMcpStartupStatusEvent(host, event.name, event.status, event.message);
}
applyCurrentAppServerMetadataSnapshot(host);
return;
}
}
function applyAppServerMetadata(host: ServerMetadataActionsHost, metadata: SharedServerMetadata): void {
const serverDiagnostics = diagnosticsWithMetadataResourceProbes(
function applyAppServerMetadataResource(host: ServerMetadataActionsHost, resource: SharedServerMetadataResource): void {
if (resource.id === "runtimeConfig") {
if (resource.value) host.stateStore.dispatch({ type: "connection/metadata-applied", runtimeConfig: resource.value });
return;
}
const serverDiagnostics = diagnosticsWithProbe(
cloneServerDiagnostics(host.stateStore.getState().connection.serverDiagnostics),
metadata.serverDiagnostics,
resource.probe,
);
host.stateStore.dispatch({
type: "connection/metadata-applied",
runtimeConfig: metadata.runtimeConfig,
availableSkills: metadata.availableSkills,
availablePermissionProfiles: metadata.availablePermissionProfiles,
rateLimit: metadata.rateLimit,
serverDiagnostics,
});
switch (resource.id) {
case "models":
host.stateStore.dispatch({
type: "connection/metadata-applied",
...(resource.value ? { availableModels: resource.value } : {}),
serverDiagnostics,
});
return;
case "skills":
host.stateStore.dispatch({
type: "connection/metadata-applied",
...(resource.value ? { availableSkills: resource.value } : {}),
serverDiagnostics,
});
return;
case "permissionProfiles":
host.stateStore.dispatch({
type: "connection/metadata-applied",
...(resource.value ? { availablePermissionProfiles: resource.value } : {}),
serverDiagnostics,
});
return;
case "rateLimits":
host.stateStore.dispatch({
type: "connection/metadata-applied",
...(resource.value !== undefined ? { rateLimit: resource.value } : {}),
serverDiagnostics,
});
return;
}
}
async function refreshAppServerMetadata(host: ServerMetadataActionsHost): Promise<SharedServerMetadata | null> {
let metadata: SharedServerMetadata | null;
async function refreshAppServerMetadata(host: ServerMetadataActionsHost): Promise<void> {
try {
metadata = await host.refreshAppServerMetadata();
await host.refreshAppServerMetadata();
} catch (error) {
if (host.isStaleResourceContextError(error)) return null;
if (host.isStaleRuntimeError(error)) return;
throw error;
}
if (!metadata) return null;
applyAppServerMetadata(host, metadata);
return metadata;
}
function applyCurrentAppServerMetadataSnapshot(host: ServerMetadataActionsHost): void {
const metadata = host.appServerMetadataSnapshot();
if (metadata) applyAppServerMetadata(host, metadata);
}
async function refreshMetadataResource(
host: ServerMetadataActionsHost,
refresh: () => Promise<SharedServerMetadata | null>,
): Promise<void> {
async function refreshMetadataResource(host: ServerMetadataActionsHost, refresh: () => Promise<void>): Promise<void> {
try {
const metadata = await refresh();
if (metadata) applyAppServerMetadata(host, metadata);
await refresh();
} catch (error) {
if (!host.isStaleResourceContextError(error)) throw error;
if (!host.isStaleRuntimeError(error)) throw error;
}
}

View file

@ -0,0 +1,14 @@
export type EffectOutcome<T> =
| { readonly kind: "not-started" }
| { readonly kind: "completed-current"; readonly value: T }
| { readonly kind: "completed-stale"; readonly value: T };
export function effectCompleted<T>(outcome: EffectOutcome<T>): outcome is Exclude<EffectOutcome<T>, { kind: "not-started" }> {
return outcome.kind !== "not-started";
}
export function effectCompletedInCurrentContext<T>(
outcome: EffectOutcome<T>,
): outcome is Extract<EffectOutcome<T>, { kind: "completed-current" }> {
return outcome.kind === "completed-current";
}

View file

@ -0,0 +1,135 @@
import type { ChatState } from "./state/root-reducer";
/**
* Operations whose availability derives from the panel's current thread mode.
*
* This intentionally excludes connection, turn-busy, and operations aimed at a
* different listed thread. Those are workflow concerns rather than properties
* of the active panel thread.
*/
export type ActivePanelOperation =
| "submit"
| "start-side-chat"
| "compact"
| "fork"
| "rollback"
| "thread-settings"
| "permission-settings"
| "goal-read"
| "goal-mutation"
| "implement-plan";
export type ActivePanelOperationDecision =
| { readonly kind: "allowed" }
| { readonly kind: "blocked"; readonly message: string }
| { readonly kind: "resume-required" };
type ActivePanelThreadFacts =
| { readonly phase: "empty" }
| { readonly phase: "awaiting-resume"; readonly provenance: "interactive" | "subagent" | null }
| {
readonly phase: "active";
readonly lifetime: "persistent" | "ephemeral";
readonly canAcceptDirectInput: boolean | null;
readonly provenance: "interactive" | "subagent" | null;
};
const ALLOWED: ActivePanelOperationDecision = { kind: "allowed" };
export function activePanelOperationDecision(state: ChatState, operation: ActivePanelOperation): ActivePanelOperationDecision {
return activePanelOperationDecisionForFacts(activePanelThreadFacts(state), operation);
}
function activePanelOperationDecisionForFacts(
facts: ActivePanelThreadFacts,
operation: ActivePanelOperation,
): ActivePanelOperationDecision {
if (facts.phase === "awaiting-resume") {
if (facts.provenance === "subagent") return agentThreadBlocked(operation);
return operation === "goal-mutation" ? { kind: "resume-required" } : ALLOWED;
}
if (facts.phase === "empty") return ALLOWED;
if (operation === "submit") {
if (facts.canAcceptDirectInput !== null) return facts.canAcceptDirectInput ? ALLOWED : directInputBlocked();
if (facts.provenance === "subagent") return agentThreadBlocked(operation);
if (facts.lifetime === "ephemeral") return sideChatDecision(operation);
return ALLOWED;
}
// Keep the stricter interpretation if a malformed or future state contains
// both mode facts. This makes mode restrictions monotonically safe.
if (facts.provenance === "subagent" && facts.lifetime === "ephemeral") {
return operation === "goal-read" ? sideChatDecision(operation) : agentThreadBlocked(operation);
}
if (facts.provenance === "subagent") return agentThreadBlocked(operation);
if (facts.lifetime === "ephemeral") return sideChatDecision(operation);
return ALLOWED;
}
function activePanelThreadFacts(state: ChatState): ActivePanelThreadFacts {
const panelThread = state.panelThread;
if (panelThread.kind === "empty") return { phase: "empty" };
if (panelThread.kind === "awaiting-resume") {
return { phase: "awaiting-resume", provenance: panelThread.provenance?.kind ?? null };
}
return {
phase: "active",
lifetime: panelThread.thread.lifetime?.kind ?? "persistent",
canAcceptDirectInput: panelThread.thread.canAcceptDirectInput,
provenance: panelThread.thread.provenance?.kind ?? null,
};
}
function directInputBlocked(): ActivePanelOperationDecision {
return { kind: "blocked", message: "This thread cannot accept messages." };
}
function agentThreadBlocked(operation: ActivePanelOperation): ActivePanelOperationDecision {
switch (operation) {
case "submit":
return { kind: "blocked", message: "Messages are unavailable in agent threads. Start a new chat to continue." };
case "goal-mutation":
return { kind: "blocked", message: "Goals are read-only in agent threads." };
case "goal-read":
return ALLOWED;
case "implement-plan":
return { kind: "blocked", message: "Plans cannot be implemented from agent threads." };
case "compact":
return { kind: "blocked", message: "Agent threads cannot be compacted." };
case "fork":
return { kind: "blocked", message: "Agent threads cannot be forked." };
case "rollback":
return { kind: "blocked", message: "Agent threads cannot be rolled back." };
case "start-side-chat":
return { kind: "blocked", message: "Side chats cannot be started from agent threads." };
case "thread-settings":
case "permission-settings":
return { kind: "blocked", message: "Thread settings are unavailable in agent threads." };
}
}
function sideChatDecision(operation: ActivePanelOperation): ActivePanelOperationDecision {
switch (operation) {
case "goal-mutation":
return { kind: "blocked", message: "Goals are unavailable in side chats." };
case "goal-read":
return { kind: "blocked", message: "Goals are unavailable in side chats." };
case "implement-plan":
return { kind: "blocked", message: "Plans cannot be implemented from side chats." };
case "compact":
return ALLOWED;
case "fork":
return { kind: "blocked", message: "Side chats cannot be forked." };
case "rollback":
return { kind: "blocked", message: "Side chats cannot be rolled back." };
case "start-side-chat":
return { kind: "blocked", message: "Side chats cannot be started from another side chat." };
case "permission-settings":
return { kind: "blocked", message: "Permission changes are unavailable in side chats." };
case "submit":
return ALLOWED;
case "thread-settings":
return { kind: "blocked", message: "Thread settings are unavailable in side chats." };
}
}

View file

@ -22,7 +22,6 @@ export interface PendingRequestActionsHost {
responder: PendingRequestResponder;
composerHasFocus: () => boolean;
focusComposer: () => void;
refreshLiveState: () => void;
}
export interface PendingRequestActions {
@ -121,6 +120,5 @@ function pendingUserInput(host: PendingRequestActionsHost, requestId: PendingReq
}
function commitRequestAction(host: PendingRequestActionsHost): void {
host.refreshLiveState();
host.focusComposer();
}

View file

@ -1,6 +1,7 @@
import type { ReasoningEffort } from "../../../../domain/catalog/metadata";
import { type RuntimeConfigSnapshot, runtimeConfigOrDefault } from "../../../../domain/runtime/config";
import type { RuntimeSettingsPatch } from "../../../../domain/runtime/thread-settings";
import { createKeyedOperationQueue, type KeyedOperationQueue } from "../../../../shared/runtime/keyed-operation-queue";
import { type CollaborationModeSelection, nextCollaborationMode, type RequestedFastMode } from "../../domain/runtime/intent";
import { modelOverrideMessage, reasoningEffortOverrideMessage } from "../../domain/runtime/labels";
import { resolveRuntimeControls } from "../../domain/runtime/resolution";
@ -9,7 +10,9 @@ import {
pendingRuntimeSettingsPatch as buildPendingRuntimeSettingsPatch,
type PendingRuntimeSettingsPatch,
} from "../../domain/runtime/thread-settings-patch";
import { activeThreadId, activeThreadState, type ChatAction, type ChatState } from "../state/root-reducer";
import { type ActivePanelOperation, activePanelOperationDecision } from "../panel-operation-policy";
import { capturePanelTargetLease, panelTargetLeaseIsCurrent } from "../state/panel-target";
import { activeThreadId, type ChatAction, type ChatState } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
import type { RuntimeSettingsTransport } from "./settings-transport";
@ -29,6 +32,15 @@ export interface RuntimeSettingsActionsHost {
addSystemMessage: (text: string) => void;
}
interface RuntimeSettingsActionsContext extends RuntimeSettingsActionsHost {
threadCommits: KeyedOperationQueue<string>;
}
interface RuntimeSettingsCommandScope {
readonly threadId: string;
readonly panelTargetRevision: number;
}
export interface ChatRuntimeSettingsActions {
applyPendingThreadSettings: () => Promise<boolean>;
requestModel: (model: string) => Promise<boolean>;
@ -51,152 +63,215 @@ export interface ChatRuntimeSettingsActions {
toggleAutoReview: () => Promise<void>;
}
export function createChatRuntimeSettingsActions(host: RuntimeSettingsActionsHost): ChatRuntimeSettingsActions {
export function createChatRuntimeSettingsActions(
host: RuntimeSettingsActionsHost,
threadCommits: KeyedOperationQueue<string> = createKeyedOperationQueue(),
): ChatRuntimeSettingsActions {
const context: RuntimeSettingsActionsContext = { ...host, threadCommits };
return {
applyPendingThreadSettings: () => applyPendingThreadSettings(host),
requestModel: (model) => requestModel(host, model),
resetModelToConfig: () => resetModelToConfig(host),
requestModelFromUi: (model) => requestModelFromUi(host, model),
requestReasoningEffort: (effort) => requestReasoningEffort(host, effort),
resetReasoningEffortToConfig: () => resetReasoningEffortToConfig(host),
requestReasoningEffortFromUi: (effort) => requestReasoningEffortFromUi(host, effort),
resetReasoningEffortToConfigFromUi: () => resetReasoningEffortToConfigFromUi(host),
requestPermissionProfile: (permissionProfile) => requestPermissionProfile(host, permissionProfile),
resetPermissionProfileToConfig: () => resetPermissionProfileToConfig(host),
enableFastMode: () => setFastMode(host, "enabled"),
disableFastMode: () => setFastMode(host, "disabled"),
toggleFastMode: () => toggleFastMode(host),
toggleCollaborationMode: () => toggleCollaborationMode(host),
setCollaborationMode: (collaborationMode) => setCollaborationMode(host, collaborationMode),
applyPendingThreadSettings: () => applyPendingThreadSettings(context),
requestModel: (model) => requestModel(context, model),
resetModelToConfig: () => resetModelToConfig(context),
requestModelFromUi: (model) => requestModelFromUi(context, model),
requestReasoningEffort: (effort) => requestReasoningEffort(context, effort),
resetReasoningEffortToConfig: () => resetReasoningEffortToConfig(context),
requestReasoningEffortFromUi: (effort) => requestReasoningEffortFromUi(context, effort),
resetReasoningEffortToConfigFromUi: () => resetReasoningEffortToConfigFromUi(context),
requestPermissionProfile: (permissionProfile) => requestPermissionProfile(context, permissionProfile),
resetPermissionProfileToConfig: () => resetPermissionProfileToConfig(context),
enableFastMode: () => setFastMode(context, "enabled"),
disableFastMode: () => setFastMode(context, "disabled"),
toggleFastMode: () => toggleFastMode(context),
toggleCollaborationMode: () => toggleCollaborationMode(context),
setCollaborationMode: (collaborationMode) => setCollaborationMode(context, collaborationMode),
requestDefaultCollaborationModeForNextTurn: () => {
requestDefaultCollaborationModeForNextTurn(host);
requestDefaultCollaborationModeForNextTurn(context);
},
enableAutoReview: () => setAutoReview(host, "enabled"),
disableAutoReview: () => setAutoReview(host, "disabled"),
toggleAutoReview: () => toggleAutoReview(host),
enableAutoReview: () => setAutoReview(context, "enabled"),
disableAutoReview: () => setAutoReview(context, "disabled"),
toggleAutoReview: () => toggleAutoReview(context),
};
}
async function applyPendingThreadSettings(host: RuntimeSettingsActionsHost): Promise<boolean> {
return (await commitPendingThreadSettings(host)).ok;
async function applyPendingThreadSettings(
host: RuntimeSettingsActionsContext,
fields?: readonly (keyof RuntimeSettingsPatch)[],
): Promise<boolean> {
return (await commitPendingThreadSettings(host, fields)).ok;
}
async function commitPendingThreadSettings(host: RuntimeSettingsActionsHost): Promise<RuntimeSettingsCommitResult> {
async function commitPendingThreadSettings(
host: RuntimeSettingsActionsContext,
fields?: readonly (keyof RuntimeSettingsPatch)[],
): Promise<RuntimeSettingsCommitResult> {
const threadId = activeThreadId(state(host));
if (!threadId) return { ok: true, collaborationModeApplied: true };
const panelTarget = capturePanelTargetLease(state(host));
const { update, collaborationModeWarning } = pendingRuntimeSettingsPatch(host);
if (collaborationModeWarning) reportCollaborationModeWarning(host, collaborationModeWarning);
const collaborationModeApplied = !collaborationModeWarning && "collaborationMode" in update;
if (Object.keys(update).length === 0) return { ok: true, collaborationModeApplied };
if (activePanelOperationBlocked(host, "thread-settings")) return { ok: false, collaborationModeApplied: false };
const scope = { threadId, panelTargetRevision: panelTarget.revision };
const ok = fields
? await commitRuntimeSettingsFields(host, scope, pickRuntimeSettingsFields(update, fields))
: await settleRuntimeSettings(host, scope);
return { ok, collaborationModeApplied: ok && collaborationModeApplied };
}
function commitRuntimeSettingsFields(
host: RuntimeSettingsActionsContext,
scope: RuntimeSettingsCommandScope,
update: RuntimeSettingsPatch,
): Promise<boolean> {
if (patchEmpty(update)) return Promise.resolve(true);
const command = { ...update };
return host.threadCommits.run(scope.threadId, async () => {
if (!runtimeSettingsScopeIsCurrent(host, scope)) return false;
if (!patchEqual(matchingPendingPatch(currentPendingRuntimeSettingsPatch(host), command), command)) return false;
const updated = await updateRuntimeSettings(host, scope, command);
if (!updated || !runtimeSettingsScopeIsCurrent(host, scope)) return false;
const committed = matchingPendingPatch(currentPendingRuntimeSettingsPatch(host), command);
if (!patchEmpty(committed)) commitRuntimeSettingsPatch(host, committed);
return patchEqual(committed, command);
});
}
async function settleRuntimeSettings(host: RuntimeSettingsActionsContext, scope: RuntimeSettingsCommandScope): Promise<boolean> {
while (runtimeSettingsScopeIsCurrent(host, scope)) {
const result = await host.threadCommits.run(scope.threadId, async (): Promise<"continue" | "failed" | "settled"> => {
if (!runtimeSettingsScopeIsCurrent(host, scope)) return "failed";
const update = currentPendingRuntimeSettingsPatch(host);
if (patchEmpty(update)) return "settled";
if (!(await updateRuntimeSettings(host, scope, update)) || !runtimeSettingsScopeIsCurrent(host, scope)) return "failed";
const committed = matchingPendingPatch(currentPendingRuntimeSettingsPatch(host), update);
if (!patchEmpty(committed)) commitRuntimeSettingsPatch(host, committed);
return patchEmpty(currentPendingRuntimeSettingsPatch(host)) ? "settled" : "continue";
});
if (result !== "continue") return result === "settled";
}
return false;
}
async function updateRuntimeSettings(
host: RuntimeSettingsActionsContext,
scope: RuntimeSettingsCommandScope,
update: RuntimeSettingsPatch,
): Promise<boolean> {
try {
if (!(await host.runtimeTransport.updateThreadSettings(threadId, update))) {
return { ok: false, collaborationModeApplied: false };
}
if (activeThreadId(state(host)) !== threadId) return { ok: false, collaborationModeApplied: false };
if (!runtimeSettingsPatchStillPending(currentPendingRuntimeSettingsPatch(host), update)) {
return { ok: false, collaborationModeApplied: false };
}
dispatch(host, { type: "runtime/pending-thread-settings-committed", update });
return { ok: true, collaborationModeApplied };
return await host.runtimeTransport.updateThreadSettings(scope.threadId, update);
} catch (error) {
if (activeThreadId(state(host)) !== threadId) return { ok: false, collaborationModeApplied: false };
host.addSystemMessage(error instanceof Error ? error.message : String(error));
return { ok: false, collaborationModeApplied: false };
if (runtimeSettingsScopeIsCurrent(host, scope)) {
host.addSystemMessage(error instanceof Error ? error.message : String(error));
}
return false;
}
}
async function requestModel(host: RuntimeSettingsActionsHost, model: string): Promise<boolean> {
if (agentThreadSettingsBlocked(host)) return false;
function runtimeSettingsScopeIsCurrent(host: RuntimeSettingsActionsHost, scope: RuntimeSettingsCommandScope): boolean {
const currentState = state(host);
return (
activeThreadId(currentState) === scope.threadId &&
panelTargetLeaseIsCurrent(currentState, {
revision: scope.panelTargetRevision,
target: { kind: "thread", threadId: scope.threadId },
})
);
}
function commitRuntimeSettingsPatch(host: RuntimeSettingsActionsHost, update: RuntimeSettingsPatch): void {
dispatch(host, { type: "runtime/pending-thread-settings-committed", update });
}
async function requestModel(host: RuntimeSettingsActionsContext, model: string): Promise<boolean> {
if (activePanelOperationBlocked(host, "thread-settings")) return false;
dispatch(host, { type: "runtime/model-requested", model });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["model"]);
}
async function resetModelToConfig(host: RuntimeSettingsActionsHost): Promise<boolean> {
if (agentThreadSettingsBlocked(host)) return false;
async function resetModelToConfig(host: RuntimeSettingsActionsContext): Promise<boolean> {
if (activePanelOperationBlocked(host, "thread-settings")) return false;
dispatch(host, { type: "runtime/model-reset-to-config" });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["model"]);
}
async function requestModelFromUi(host: RuntimeSettingsActionsHost, model: string): Promise<void> {
async function requestModelFromUi(host: RuntimeSettingsActionsContext, model: string): Promise<void> {
await runRuntimeUiCommand(host, () => requestModel(host, model), modelOverrideMessage(model));
}
async function requestReasoningEffort(host: RuntimeSettingsActionsHost, effort: ReasoningEffort): Promise<boolean> {
if (agentThreadSettingsBlocked(host)) return false;
async function requestReasoningEffort(host: RuntimeSettingsActionsContext, effort: ReasoningEffort): Promise<boolean> {
if (activePanelOperationBlocked(host, "thread-settings")) return false;
dispatch(host, { type: "runtime/reasoning-effort-requested", effort });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["effort"]);
}
async function resetReasoningEffortToConfig(host: RuntimeSettingsActionsHost): Promise<boolean> {
if (agentThreadSettingsBlocked(host)) return false;
async function resetReasoningEffortToConfig(host: RuntimeSettingsActionsContext): Promise<boolean> {
if (activePanelOperationBlocked(host, "thread-settings")) return false;
dispatch(host, { type: "runtime/reasoning-effort-reset-to-config" });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["effort"]);
}
async function requestReasoningEffortFromUi(host: RuntimeSettingsActionsHost, effort: ReasoningEffort): Promise<void> {
async function requestReasoningEffortFromUi(host: RuntimeSettingsActionsContext, effort: ReasoningEffort): Promise<void> {
await runRuntimeUiCommand(host, () => requestReasoningEffort(host, effort), reasoningEffortOverrideMessage(effort));
}
async function resetReasoningEffortToConfigFromUi(host: RuntimeSettingsActionsHost): Promise<void> {
async function resetReasoningEffortToConfigFromUi(host: RuntimeSettingsActionsContext): Promise<void> {
await runRuntimeUiCommand(host, () => resetReasoningEffortToConfig(host), reasoningEffortOverrideMessage(null));
}
async function requestPermissionProfile(host: RuntimeSettingsActionsHost, permissionProfile: string): Promise<boolean> {
if (agentThreadSettingsBlocked(host)) return false;
if (permissionChangesBlocked(host)) return false;
async function requestPermissionProfile(host: RuntimeSettingsActionsContext, permissionProfile: string): Promise<boolean> {
if (activePanelOperationBlocked(host, "permission-settings")) return false;
dispatch(host, { type: "runtime/permission-profile-requested", permissionProfile });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["permissions"]);
}
async function resetPermissionProfileToConfig(host: RuntimeSettingsActionsHost): Promise<boolean> {
if (agentThreadSettingsBlocked(host)) return false;
if (permissionChangesBlocked(host)) return false;
async function resetPermissionProfileToConfig(host: RuntimeSettingsActionsContext): Promise<boolean> {
if (activePanelOperationBlocked(host, "permission-settings")) return false;
dispatch(host, { type: "runtime/permission-profile-reset-to-config" });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["permissions"]);
}
function permissionChangesBlocked(host: RuntimeSettingsActionsHost): boolean {
if (activeThreadState(state(host))?.lifetime?.kind !== "ephemeral") return false;
host.addSystemMessage("Permission changes are unavailable in side chats.");
function activePanelOperationBlocked(host: RuntimeSettingsActionsHost, operation: ActivePanelOperation): boolean {
const decision = activePanelOperationDecision(state(host), operation);
if (decision.kind !== "blocked") return false;
host.addSystemMessage(decision.message);
return true;
}
function agentThreadSettingsBlocked(host: RuntimeSettingsActionsHost): boolean {
if (activeThreadState(state(host))?.provenance?.kind !== "subagent") return false;
host.addSystemMessage("Thread settings are unavailable in agent threads.");
return true;
}
async function toggleFastMode(host: RuntimeSettingsActionsHost): Promise<void> {
async function toggleFastMode(host: RuntimeSettingsActionsContext): Promise<void> {
const { snapshot, config } = runtimeProjection(host);
await setFastMode(host, resolveRuntimeControls(snapshot, config).fastMode.active ? "disabled" : "enabled");
}
async function setFastMode(host: RuntimeSettingsActionsHost, mode: FastModeState): Promise<void> {
if (agentThreadSettingsBlocked(host)) return;
async function setFastMode(host: RuntimeSettingsActionsContext, mode: FastModeState): Promise<void> {
if (activePanelOperationBlocked(host, "thread-settings")) return;
const fastMode: RequestedFastMode = mode;
await runRuntimeUiCommand(
host,
async () => {
dispatch(host, { type: "runtime/fast-mode-requested", fastMode });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["serviceTier"]);
},
mode === "enabled" ? "Fast mode on for subsequent turns." : "Fast mode off for subsequent turns.",
);
}
async function toggleCollaborationMode(host: RuntimeSettingsActionsHost): Promise<void> {
async function toggleCollaborationMode(host: RuntimeSettingsActionsContext): Promise<void> {
const { snapshot, config } = runtimeProjection(host);
const next = nextCollaborationMode(resolveRuntimeControls(snapshot, config).collaborationMode.effective);
await setCollaborationMode(host, next);
}
async function setCollaborationMode(host: RuntimeSettingsActionsHost, collaborationMode: CollaborationModeSelection): Promise<boolean> {
if (agentThreadSettingsBlocked(host)) return false;
async function setCollaborationMode(host: RuntimeSettingsActionsContext, collaborationMode: CollaborationModeSelection): Promise<boolean> {
if (activePanelOperationBlocked(host, "thread-settings")) return false;
dispatch(host, { type: "runtime/requested-collaboration-mode-set", collaborationMode });
const result = await commitPendingThreadSettings(host);
const result = await commitPendingThreadSettings(host, ["collaborationMode"]);
if (result.ok) closeRuntimePanel(host);
if (result.ok && result.collaborationModeApplied) {
host.addSystemMessage(collaborationMode === "plan" ? "Plan mode on for subsequent turns." : "Plan mode off for subsequent turns.");
@ -205,23 +280,23 @@ async function setCollaborationMode(host: RuntimeSettingsActionsHost, collaborat
}
function requestDefaultCollaborationModeForNextTurn(host: RuntimeSettingsActionsHost): void {
if (agentThreadSettingsBlocked(host)) return;
if (activePanelOperationBlocked(host, "thread-settings")) return;
dispatch(host, { type: "runtime/requested-collaboration-mode-set", collaborationMode: "default" });
}
async function toggleAutoReview(host: RuntimeSettingsActionsHost): Promise<void> {
async function toggleAutoReview(host: RuntimeSettingsActionsContext): Promise<void> {
const { snapshot, config } = runtimeProjection(host);
const nextState = resolveRuntimeControls(snapshot, config).autoReview.active ? "disabled" : "enabled";
await setAutoReview(host, nextState);
}
async function setAutoReview(host: RuntimeSettingsActionsHost, mode: AutoReviewState): Promise<void> {
if (agentThreadSettingsBlocked(host)) return;
async function setAutoReview(host: RuntimeSettingsActionsContext, mode: AutoReviewState): Promise<void> {
if (activePanelOperationBlocked(host, "thread-settings")) return;
await runRuntimeUiCommand(
host,
async () => {
dispatch(host, { type: "runtime/approvals-reviewer-requested", approvalsReviewer: mode === "enabled" ? "auto_review" : "user" });
return applyPendingThreadSettings(host);
return applyPendingThreadSettings(host, ["approvalsReviewer"]);
},
mode === "enabled" ? "Auto-review on for subsequent turns." : "Auto-review off for subsequent turns.",
);
@ -261,10 +336,36 @@ function currentPendingRuntimeSettingsPatch(host: RuntimeSettingsActionsHost): R
return buildPendingRuntimeSettingsPatch(snapshot, config).update;
}
function runtimeSettingsPatchStillPending(current: RuntimeSettingsPatch, applied: RuntimeSettingsPatch): boolean {
return (Object.keys(applied) as (keyof RuntimeSettingsPatch)[]).every((key) => {
return key in current && threadSettingsValueEqual(current[key], applied[key]);
});
function pickRuntimeSettingsFields(update: RuntimeSettingsPatch, fields: readonly (keyof RuntimeSettingsPatch)[]): RuntimeSettingsPatch {
const targetedUpdate: RuntimeSettingsPatch = {};
for (const key of fields) {
if (key in update) Object.assign(targetedUpdate, { [key]: update[key] });
}
return targetedUpdate;
}
function matchingPendingPatch(current: RuntimeSettingsPatch, sent: RuntimeSettingsPatch): RuntimeSettingsPatch {
const matching: RuntimeSettingsPatch = {};
for (const key of patchKeys(sent)) {
if (key in current && threadSettingsValueEqual(current[key], sent[key])) {
Object.assign(matching, { [key]: sent[key] });
}
}
return matching;
}
function patchEqual(left: RuntimeSettingsPatch, right: RuntimeSettingsPatch): boolean {
const leftKeys = patchKeys(left);
const rightKeys = patchKeys(right);
return leftKeys.length === rightKeys.length && leftKeys.every((key) => key in right && threadSettingsValueEqual(left[key], right[key]));
}
function patchEmpty(patch: RuntimeSettingsPatch): boolean {
return patchKeys(patch).length === 0;
}
function patchKeys(patch: RuntimeSettingsPatch): (keyof RuntimeSettingsPatch)[] {
return Object.keys(patch) as (keyof RuntimeSettingsPatch)[];
}
function threadSettingsValueEqual(left: unknown, right: unknown): boolean {

View file

@ -20,11 +20,11 @@ interface ResumedThreadActionParams {
preserveRequestedRuntimeSettings?: boolean;
serviceTierKnown?: boolean;
preservePendingSubmissionId?: string;
expectedPanelTargetRevision?: number;
}
interface ResumedThreadFromActiveRuntimeParams {
thread: Thread;
cwd: string;
runtime: Pick<
ActiveThreadRuntimeState,
| "model"
@ -41,12 +41,12 @@ interface ResumedThreadFromActiveRuntimeParams {
>;
listedThreads?: readonly Thread[];
items?: readonly ThreadStreamItem[];
expectedPanelTargetRevision?: number;
}
export interface ActiveThreadResumedAction extends RuntimePermissionState, RuntimePermissionKnownState {
type: "active-thread/resumed";
thread: Thread;
cwd: string;
model: string | null;
reasoningEffort: ReasoningEffort | null;
serviceTier: ServiceTier | null;
@ -57,6 +57,7 @@ export interface ActiveThreadResumedAction extends RuntimePermissionState, Runti
listedThreads?: readonly Thread[];
preserveRequestedRuntimeSettings?: boolean;
preservePendingSubmissionId?: string;
expectedPanelTargetRevision?: number;
lifetime?:
| { readonly kind: "persistent" }
| { readonly kind: "ephemeral"; readonly sourceThreadId: string; readonly sourceThreadTitle: string | null };
@ -64,7 +65,6 @@ export interface ActiveThreadResumedAction extends RuntimePermissionState, Runti
export interface ActiveThreadSettingsAppliedAction extends RuntimePermissionState, RuntimePermissionKnownState {
type: "active-thread/settings-applied";
cwd: string;
model: string | null;
reasoningEffort: ReasoningEffort | null;
collaborationMode: CollaborationModeSelection;
@ -73,7 +73,6 @@ export interface ActiveThreadSettingsAppliedAction extends RuntimePermissionStat
}
export interface ActiveThreadSettingsAppliedActionSettings extends RuntimePermissionState {
cwd: string;
model: string | null;
effort: string | null;
collaborationMode: { mode: CollaborationModeSelection };
@ -100,11 +99,16 @@ export interface ClearLocalTurnAction {
export interface ClearActiveThreadAction {
type: "active-thread/cleared";
expectedPanelTargetRevision?: number;
}
export interface ThreadListAppliedAction {
type: "thread-list/applied";
threads: readonly Thread[];
hasMore?: boolean;
isFetching?: boolean;
isFetchingNextPage?: boolean;
error?: string | null;
}
export interface DisclosureSetAction {
@ -136,7 +140,6 @@ export function resumedThreadActionFromActiveRuntime(params: ResumedThreadFromAc
return resumedThreadAction({
response: {
thread: params.thread,
cwd: params.cwd,
approvalPolicyKnown: params.runtime.approvalPolicyKnown,
sandboxPolicyKnown: params.runtime.sandboxPolicyKnown,
permissionProfileKnown: params.runtime.permissionProfileKnown,
@ -151,6 +154,7 @@ export function resumedThreadActionFromActiveRuntime(params: ResumedThreadFromAc
serviceTierKnown: params.runtime.serviceTierKnown,
...(params.listedThreads ? { listedThreads: params.listedThreads } : {}),
...(params.items ? { items: params.items } : {}),
...(params.expectedPanelTargetRevision === undefined ? {} : { expectedPanelTargetRevision: params.expectedPanelTargetRevision }),
});
}
@ -160,7 +164,6 @@ export function resumedThreadAction(params: ResumedThreadActionParams): ActiveTh
return {
type: "active-thread/resumed",
thread: response.thread,
cwd: response.cwd,
model: response.model,
reasoningEffort: response.reasoningEffort,
serviceTier: response.serviceTier,
@ -177,6 +180,7 @@ export function resumedThreadAction(params: ResumedThreadActionParams): ActiveTh
: {}),
...(params.preserveRequestedRuntimeSettings ? { preserveRequestedRuntimeSettings: true } : {}),
...(params.preservePendingSubmissionId ? { preservePendingSubmissionId: params.preservePendingSubmissionId } : {}),
...(params.expectedPanelTargetRevision === undefined ? {} : { expectedPanelTargetRevision: params.expectedPanelTargetRevision }),
};
}
@ -203,7 +207,6 @@ export function activeThreadSettingsAppliedAction(settings: ActiveThreadSettings
const permissions = runtimePermissionStateOrDefault(settings);
return {
type: "active-thread/settings-applied",
cwd: settings.cwd,
model: settings.model,
reasoningEffort: normalizeReasoningEffort(settings.effort),
collaborationMode: settings.collaborationMode.mode,

View file

@ -0,0 +1,28 @@
import { type ChatState, panelThreadId } from "./root-reducer";
type PanelTarget = { readonly kind: "empty" } | { readonly kind: "thread"; readonly threadId: string };
export interface PanelTargetLease {
readonly revision: number;
readonly target: PanelTarget;
}
export function capturePanelTargetLease(state: ChatState): PanelTargetLease {
const threadId = panelThreadId(state);
return {
revision: state.panelTargetRevision,
target: threadId ? { kind: "thread", threadId } : { kind: "empty" },
};
}
export function panelTargetLeaseIsCurrent(state: ChatState, lease: PanelTargetLease): boolean {
if (state.panelTargetRevision !== lease.revision) return false;
const threadId = panelThreadId(state);
return lease.target.kind === "empty" ? threadId === null : threadId === lease.target.threadId;
}
export function panelTargetLeasesMatch(left: PanelTargetLease, right: PanelTargetLease): boolean {
if (left.revision !== right.revision) return false;
if (left.target.kind === "empty") return right.target.kind === "empty";
return right.target.kind === "thread" && left.target.threadId === right.target.threadId;
}

View file

@ -60,6 +60,14 @@ import type {
} from "./actions";
import { definedPatch, patchObject } from "./patch";
import type { ChatPendingSubmissionState, PendingSubmissionAction } from "./pending-submission";
import {
type ChatSubagentActivityState,
initialSubagentActivityState,
isSubagentActivityAction,
reduceSubagentActivitySlice,
type SubagentActivityAction,
subagentActivityWithParentTurn,
} from "./subagent-activity";
import {
type ChatThreadStreamState,
initialChatThreadStreamState,
@ -103,15 +111,19 @@ interface ChatConnectionState {
interface ChatThreadListState {
readonly listedThreads: readonly Thread[];
readonly hasMore: boolean;
readonly isFetching: boolean;
readonly isFetchingNextPage: boolean;
readonly error: string | null;
}
export interface ChatActiveThreadState {
readonly id: string;
readonly title?: string | null;
readonly cwd: string | null;
readonly goal: ThreadGoal | null;
readonly tokenUsage: ThreadTokenUsage | null;
readonly lifetime: ActiveThreadLifetime | null;
readonly canAcceptDirectInput: boolean | null;
readonly provenance: Thread["provenance"] | null;
}
@ -131,6 +143,7 @@ type ActiveThreadLifetime =
interface ChatComposerState {
readonly draft: string;
readonly pendingAttachmentSaveIds: readonly number[];
readonly suggestSelected: number;
readonly suggestions: readonly ComposerSuggestion[];
readonly suggestionsDismissedSignature: string | null;
@ -140,9 +153,11 @@ interface ChatStateShape {
connection: ChatConnectionState;
threadList: ChatThreadListState;
panelThread: ChatPanelThreadState;
panelTargetRevision: number;
runtime: ChatRuntimeState;
turn: ChatTurnState;
threadStream: ChatThreadStreamState;
subagentActivity: ChatSubagentActivityState;
pendingSubmission: ChatPendingSubmissionState | null;
requests: ChatRequestState;
composer: ChatComposerState;
@ -166,9 +181,7 @@ type ConnectionAction =
type ThreadListAction = ThreadListAppliedAction;
type ActiveThreadAction =
| { type: "active-thread/cwd-set"; cwd: string | null }
| { type: "active-thread/token-usage-set"; tokenUsage: ThreadTokenUsage | null };
type ActiveThreadAction = { type: "active-thread/token-usage-set"; tokenUsage: ThreadTokenUsage | null };
type RuntimeAction =
| { type: "runtime/model-requested"; model: string }
@ -207,6 +220,16 @@ type TurnAction =
| TurnStartFailedAction;
type ComposerAction =
| {
type: "composer/attachment-save-started";
saveId: number;
draft: string;
}
| {
type: "composer/attachment-save-settled";
saveId: number;
draft?: string;
}
| {
type: "composer/draft-set";
draft: string;
@ -263,6 +286,7 @@ type ChatSliceAction =
| RuntimeAction
| RequestAction
| ThreadStreamAction
| SubagentActivityAction
| ComposerAction
| UiAction;
@ -271,9 +295,11 @@ export function createChatState(): ChatState {
connection: initialConnectionState(),
threadList: initialThreadListState(),
panelThread: initialPanelThreadState(),
panelTargetRevision: 0,
runtime: initialChatRuntimeState(),
turn: initialTurnState(),
threadStream: initialThreadStreamState(),
subagentActivity: initialSubagentActivityState(),
pendingSubmission: null,
requests: initialRequestState(),
composer: initialComposerState(),
@ -339,7 +365,10 @@ function reduceChatTransition(state: ChatState, action: ChatTransitionAction): C
case "connection/context-replaced":
return clearConnectionContextState(state);
case "active-thread/cleared":
return clearThreadScopedState(state);
if (action.expectedPanelTargetRevision !== undefined && action.expectedPanelTargetRevision !== state.panelTargetRevision) {
return state;
}
return clearThreadScopedState(state, { invalidatePanelTarget: true });
case "active-thread/resumed":
return reduceActiveThreadResumedTransition(state, action);
case "active-thread/settings-applied":
@ -405,15 +434,19 @@ function adoptPendingSteerItem(state: ChatThreadStreamState, item: ThreadStreamD
stableItems[matchedIndex] = {
...current,
...(item.contextAttachments ? { contextAttachments: item.contextAttachments } : {}),
...(item.mentionedFiles ? { mentionedFiles: item.mentionedFiles } : {}),
...(item.referencedFiles ? { referencedFiles: item.referencedFiles } : {}),
...(item.referencedThread ? { referencedThread: item.referencedThread } : {}),
};
return { ...state, stableItems };
}
function reduceActiveThreadResumedTransition(state: ChatState, action: ActiveThreadResumedAction): ChatState {
if (action.expectedPanelTargetRevision !== undefined && action.expectedPanelTargetRevision !== state.panelTargetRevision) {
return state;
}
const runtimeBase = action.preserveRequestedRuntimeSettings ? state.runtime : initialChatRuntimeState();
const turnScopedState = clearTurnScopedState(state);
const nextPanelTargetRevision = panelThreadId(state) === action.thread.id ? state.panelTargetRevision : state.panelTargetRevision + 1;
return patchChatState(turnScopedState, {
connection: {
...state.connection,
@ -428,13 +461,14 @@ function reduceActiveThreadResumedTransition(state: ChatState, action: ActiveThr
thread: {
id: action.thread.id,
title: (action.thread.name ?? action.thread.preview) || null,
cwd: action.cwd,
goal: null,
tokenUsage: null,
lifetime: action.lifetime ?? { kind: "persistent" },
canAcceptDirectInput: action.thread.canAcceptDirectInput ?? null,
provenance: action.thread.provenance,
},
},
panelTargetRevision: nextPanelTargetRevision,
runtime: {
...runtimeBase,
active: {
@ -465,13 +499,8 @@ function reduceActiveThreadResumedTransition(state: ChatState, action: ActiveThr
}
function reduceActiveThreadSettingsAppliedTransition(state: ChatState, action: ActiveThreadSettingsAppliedAction): ChatState {
const activeThread = activeThreadState(state);
if (!activeThread) return state;
if (!activeThreadState(state)) return state;
return patchChatState(state, {
panelThread: {
kind: "active",
thread: { ...activeThread, cwd: action.cwd },
},
runtime: {
...state.runtime,
active: {
@ -506,7 +535,7 @@ function reduceActiveThreadGoalSetTransition(state: ChatState, goal: ThreadGoal
}
function reduceRestoredThreadAppliedTransition(state: ChatState, threadId: string, fallbackTitle: string | null): ChatState {
const cleared = clearThreadScopedState(state);
const cleared = clearThreadScopedState(state, { invalidatePanelTarget: true });
return patchChatState(cleared, {
connection: { ...cleared.connection, statusText: "Thread ready to resume." },
panelThread: createAwaitingResumeThreadState(threadId, fallbackTitle),
@ -519,7 +548,7 @@ function reduceRestoredThreadRenamedTransition(state: ChatState, threadId: strin
}
function reduceViewStateClearedTransition(state: ChatState): ChatState {
const cleared = clearThreadScopedState(state);
const cleared = clearThreadScopedState(state, { invalidatePanelTarget: true });
return patchChatState(cleared, { connection: { ...cleared.connection, statusText: "Idle" } });
}
@ -531,11 +560,13 @@ function reduceTurnStartedTransition(state: ChatState, action: TurnStartedAction
if (!activeThread || activeThread.id !== action.threadId) return state;
return patchChatState(state, {
panelThread: { kind: "active", thread: activeThread },
panelTargetRevision: panelThreadId(state) === action.threadId ? state.panelTargetRevision : state.panelTargetRevision + 1,
turn: { lifecycle },
connection: { ...state.connection, statusText: STATUS_TURN_RUNNING },
threadStream: action.items
? threadStreamWithActiveTurnItems(state.threadStream, action.turnId, action.items)
: threadStreamStartActiveSegment(state.threadStream, action.turnId, []),
subagentActivity: initialSubagentActivityState(action.turnId),
});
}
@ -545,6 +576,7 @@ function reduceTurnCompletedTransition(state: ChatState, action: TurnCompletedAc
return patchChatState(state, {
turn: { lifecycle },
threadStream: threadStreamWithItems(state.threadStream, action.items),
subagentActivity: initialSubagentActivityState(),
connection: { ...state.connection, statusText: `Turn ${action.status}.` },
});
}
@ -564,6 +596,7 @@ function reduceTurnOptimisticStartedTransition(state: ChatState, action: TurnOpt
turn: { lifecycle },
pendingSubmission: action.pendingSubmissionId ? null : state.pendingSubmission,
threadStream: threadStreamStartActiveSegment(state.threadStream, null, [action.item]),
subagentActivity: initialSubagentActivityState(),
});
}
@ -576,6 +609,7 @@ function reduceTurnStartAcknowledgedTransition(state: ChatState, action: TurnSta
return patchChatState(state, {
turn: { lifecycle },
threadStream: threadStreamWithActiveTurnItems(state.threadStream, action.turnId, action.items),
subagentActivity: subagentActivityWithParentTurn(state.subagentActivity, action.turnId),
});
}
@ -585,6 +619,7 @@ function reduceTurnStartFailedTransition(state: ChatState, action: TurnStartFail
return patchChatState(state, {
turn: { lifecycle },
threadStream: threadStreamWithItems(state.threadStream, action.items),
subagentActivity: initialSubagentActivityState(),
});
}
@ -618,15 +653,18 @@ function clearTurnScopedState(state: ChatState): ChatState {
lifecycle: transitionChatTurnLifecycleState(state.turn.lifecycle, { type: "cleared" }),
},
threadStream: threadStreamWithItems(state.threadStream, threadStreamItems(state.threadStream)),
subagentActivity: initialSubagentActivityState(),
requests: initialRequestState(),
ui: clearAllRequestDisclosures(state.ui),
});
}
function clearThreadScopedState(state: ChatState): ChatState {
function clearThreadScopedState(state: ChatState, options: { invalidatePanelTarget?: boolean } = {}): ChatState {
return clearTurnScopedState(
patchChatState(state, {
panelThread: initialPanelThreadState(),
panelTargetRevision:
options.invalidatePanelTarget || panelThreadId(state) !== null ? state.panelTargetRevision + 1 : state.panelTargetRevision,
runtime: initialChatRuntimeState(),
threadStream: initialThreadStreamState(),
pendingSubmission: null,
@ -639,8 +677,11 @@ function clearThreadScopedState(state: ChatState): ChatState {
function clearConnectionScopedState(state: ChatState): ChatState {
const cleared = clearTurnScopedState(state);
const ephemeralExpired = state.panelThread.kind === "active" && state.panelThread.thread.lifetime?.kind === "ephemeral";
const nextPanelThread = panelThreadAfterConnectionExit(state.panelThread);
return patchChatState(cleared, {
panelThread: panelThreadAfterConnectionExit(state.panelThread),
panelThread: nextPanelThread,
panelTargetRevision:
panelThreadIdForState(nextPanelThread) === panelThreadId(state) ? state.panelTargetRevision : state.panelTargetRevision + 1,
runtime: initialChatRuntimeState(),
connection: {
...state.connection,
@ -659,6 +700,7 @@ function clearConnectionContextState(state: ChatState): ChatState {
const cleared = clearConnectionScopedState(state);
return patchChatState(cleared, {
panelThread: initialPanelThreadState(),
panelTargetRevision: state.panelTargetRevision + 1,
connection: {
...cleared.connection,
runtimeConfig: null,
@ -680,16 +722,25 @@ function panelThreadAfterConnectionExit(panelThread: ChatPanelThreadState): Chat
return createAwaitingResumeThreadState(panelThread.thread.id, panelThread.thread.title ?? null, panelThread.thread.provenance);
}
function panelThreadIdForState(panelThread: ChatPanelThreadState): string | null {
if (panelThread.kind === "awaiting-resume") return panelThread.threadId;
return panelThread.kind === "active" ? panelThread.thread.id : null;
}
function reduceChatSlices(state: ChatState, action: ChatSliceAction): ChatState {
return patchChatState(state, {
connection: reduceConnectionSlice(state.connection, action),
threadList: reduceThreadListSlice(state.threadList, action),
panelThread: reducePanelThreadSlice(state.panelThread, action),
panelTargetRevision: state.panelTargetRevision,
runtime: reduceRuntimeSlice(state.runtime, action),
turn: state.turn,
pendingSubmission: state.pendingSubmission,
requests: isRequestAction(action) ? reduceRequestSlice(state.requests, action) : state.requests,
threadStream: isThreadStreamAction(action) ? reduceThreadStreamSlice(state.threadStream, action) : state.threadStream,
subagentActivity: isSubagentActivityAction(action)
? reduceSubagentActivitySlice(state.subagentActivity, action)
: state.subagentActivity,
composer: reduceComposerSlice(state.composer, action),
ui: isUiAction(action) ? reduceUiSlice(state.ui, action) : state.ui,
});
@ -717,14 +768,18 @@ function reduceConnectionSlice(state: ChatConnectionState, action: ChatSliceActi
function reduceThreadListSlice(state: ChatThreadListState, action: ChatSliceAction): ChatThreadListState {
if (action.type !== "thread-list/applied") return state;
return patchObject(state, { listedThreads: action.threads });
return patchObject(state, {
listedThreads: action.threads,
...definedPatch("hasMore", action.hasMore),
...definedPatch("isFetching", action.isFetching),
...definedPatch("isFetchingNextPage", action.isFetchingNextPage),
...definedPatch("error", action.error),
});
}
function reducePanelThreadSlice(state: ChatPanelThreadState, action: ChatSliceAction): ChatPanelThreadState {
if (state.kind !== "active") return state;
switch (action.type) {
case "active-thread/cwd-set":
return patchObject(state, { thread: patchObject(state.thread, { cwd: action.cwd }) });
case "active-thread/token-usage-set":
return patchObject(state, { thread: patchObject(state.thread, { tokenUsage: action.tokenUsage }) });
default:
@ -765,6 +820,18 @@ function reduceRuntimeSlice(state: ChatRuntimeState, action: ChatSliceAction): C
function reduceComposerSlice(state: ChatComposerState, action: ChatSliceAction): ChatComposerState {
switch (action.type) {
case "composer/attachment-save-started":
return patchObject(state, {
draft: action.draft,
pendingAttachmentSaveIds: [...state.pendingAttachmentSaveIds, action.saveId],
suggestions: [],
suggestSelected: 0,
});
case "composer/attachment-save-settled":
return patchObject(state, {
...(action.draft === undefined ? {} : { draft: action.draft }),
pendingAttachmentSaveIds: state.pendingAttachmentSaveIds.filter((saveId) => saveId !== action.saveId),
});
case "composer/draft-set":
return patchObject(state, {
draft: action.draft,
@ -808,7 +875,7 @@ function initialConnectionState(): ChatConnectionState {
}
function initialThreadListState(): ChatThreadListState {
return { listedThreads: [] };
return { listedThreads: [], hasMore: false, isFetching: false, isFetchingNextPage: false, error: null };
}
function initialPanelThreadState(): ChatPanelThreadState {
@ -832,10 +899,10 @@ function createActiveThreadState(id: string): ChatActiveThreadState {
return {
id,
title: null,
cwd: null,
goal: null,
tokenUsage: null,
lifetime: null,
canAcceptDirectInput: null,
provenance: null,
};
}
@ -855,6 +922,7 @@ function initialRequestState(): ChatRequestState {
function initialComposerState(): ChatComposerState {
return {
draft: "",
pendingAttachmentSaveIds: [],
suggestSelected: 0,
suggestions: [],
suggestionsDismissedSignature: null,

View file

@ -0,0 +1,234 @@
import { streamedTextThreadStreamItem, streamedToolOutputThreadStreamItem } from "../../domain/thread-stream/factories/streaming-items";
import { normalizeProposedPlanMarkdown } from "../../domain/thread-stream/format/proposed-plan";
import type { ThreadStreamItem } from "../../domain/thread-stream/items";
import { upsertThreadStreamItemById } from "../../domain/thread-stream/updates";
interface SubagentActivityEntry {
readonly threadId: string;
readonly childTurnId: string | null;
readonly latestItem: ThreadStreamItem | null;
readonly executionState: "running" | "completed" | "failed";
}
export interface ChatSubagentActivityState {
readonly parentTurnId: string | null;
readonly byThreadId: ReadonlyMap<string, SubagentActivityEntry>;
}
export type SubagentActivityAction =
| { type: "subagent-activity/tracked"; threadId: string; parentTurnId: string }
| { type: "subagent-activity/turn-started"; threadId: string; childTurnId: string }
| { type: "subagent-activity/item-observed"; threadId: string; item: ThreadStreamItem; advance: boolean }
| { type: "subagent-activity/assistant-delta-appended"; threadId: string; childTurnId: string; itemId: string; delta: string }
| { type: "subagent-activity/plan-delta-appended"; threadId: string; childTurnId: string; itemId: string; delta: string }
| {
type: "subagent-activity/text-delta-appended";
threadId: string;
childTurnId: string;
itemId: string;
label: string;
delta: string;
kind: "tool" | "hook" | "reasoning";
}
| {
type: "subagent-activity/tool-output-appended";
threadId: string;
childTurnId: string;
itemId: string;
delta: string;
fallbackLabel: string;
}
| {
type: "subagent-activity/turn-completed";
threadId: string;
childTurnId: string;
items: readonly ThreadStreamItem[];
executionState: "completed" | "failed";
}
| { type: "subagent-activity/execution-state-changed"; threadId: string; executionState: "completed" | "failed" };
export function initialSubagentActivityState(parentTurnId: string | null = null): ChatSubagentActivityState {
return { parentTurnId, byThreadId: new Map() };
}
export function subagentActivityWithParentTurn(state: ChatSubagentActivityState, parentTurnId: string): ChatSubagentActivityState {
return state.parentTurnId === parentTurnId ? state : initialSubagentActivityState(parentTurnId);
}
export function isSubagentActivityAction(action: { type: string }): action is SubagentActivityAction {
return action.type.startsWith("subagent-activity/");
}
export function reduceSubagentActivitySlice(state: ChatSubagentActivityState, action: SubagentActivityAction): ChatSubagentActivityState {
switch (action.type) {
case "subagent-activity/tracked":
return trackSubagent(state, action.threadId, action.parentTurnId);
case "subagent-activity/turn-started":
return updateTrackedEntry(state, action.threadId, (entry) => ({
...entry,
childTurnId: action.childTurnId,
latestItem: null,
executionState: "running",
}));
case "subagent-activity/item-observed":
return updateTrackedEntry(state, action.threadId, (entry) => {
if (isStaleChildTurn(entry, action.item.turnId)) return entry;
return {
...entry,
childTurnId: action.item.turnId ?? entry.childTurnId,
latestItem: observedLatestItem(entry.latestItem, action.item, action.advance),
};
});
case "subagent-activity/assistant-delta-appended":
return updateCurrentTurnEntry(state, action.threadId, action.childTurnId, (entry) => ({
...entry,
childTurnId: action.childTurnId,
latestItem: appendAssistantDelta(entry.latestItem, action.itemId, action.childTurnId, action.delta),
}));
case "subagent-activity/plan-delta-appended":
return updateCurrentTurnEntry(state, action.threadId, action.childTurnId, (entry) => ({
...entry,
childTurnId: action.childTurnId,
latestItem: appendPlanDelta(entry.latestItem, action.itemId, action.childTurnId, action.delta),
}));
case "subagent-activity/text-delta-appended":
return updateCurrentTurnEntry(state, action.threadId, action.childTurnId, (entry) => ({
...entry,
childTurnId: action.childTurnId,
latestItem: appendTextDelta(entry.latestItem, action.itemId, action.childTurnId, action.label, action.delta, action.kind),
}));
case "subagent-activity/tool-output-appended":
return updateCurrentTurnEntry(state, action.threadId, action.childTurnId, (entry) => ({
...entry,
childTurnId: action.childTurnId,
latestItem: appendToolOutput(entry.latestItem, action.itemId, action.childTurnId, action.delta, action.fallbackLabel),
}));
case "subagent-activity/turn-completed":
return updateCurrentTurnEntry(state, action.threadId, action.childTurnId, (entry) => ({
...entry,
childTurnId: action.childTurnId,
latestItem: latestDisplayableItem(action.items) ?? entry.latestItem,
executionState: action.executionState,
}));
case "subagent-activity/execution-state-changed":
return updateTrackedEntry(state, action.threadId, (entry) => ({ ...entry, executionState: action.executionState }));
}
}
function trackSubagent(state: ChatSubagentActivityState, threadId: string, parentTurnId: string): ChatSubagentActivityState {
const current = state.parentTurnId === parentTurnId ? state : initialSubagentActivityState(parentTurnId);
if (current.byThreadId.has(threadId)) return current;
const byThreadId = new Map(current.byThreadId);
byThreadId.set(threadId, { threadId, childTurnId: null, latestItem: null, executionState: "running" });
return { ...current, byThreadId };
}
function updateTrackedEntry(
state: ChatSubagentActivityState,
threadId: string,
update: (entry: SubagentActivityEntry) => SubagentActivityEntry,
): ChatSubagentActivityState {
const entry = state.byThreadId.get(threadId);
if (!entry) return state;
const next = update(entry);
if (next === entry) return state;
const byThreadId = new Map(state.byThreadId);
byThreadId.set(threadId, next);
return { ...state, byThreadId };
}
function updateCurrentTurnEntry(
state: ChatSubagentActivityState,
threadId: string,
childTurnId: string,
update: (entry: SubagentActivityEntry) => SubagentActivityEntry,
): ChatSubagentActivityState {
return updateTrackedEntry(state, threadId, (entry) => (isStaleChildTurn(entry, childTurnId) ? entry : update(entry)));
}
function isStaleChildTurn(entry: SubagentActivityEntry, childTurnId: string | null | undefined): boolean {
return Boolean(entry.childTurnId && childTurnId && entry.childTurnId !== childTurnId);
}
function observedLatestItem(current: ThreadStreamItem | null, item: ThreadStreamItem, advance: boolean): ThreadStreamItem | null {
if (advance || !current) return item;
if (!sameSourceItem(current, item.id)) return current;
return upsertThreadStreamItemById([current], item)[0] ?? item;
}
function appendAssistantDelta(current: ThreadStreamItem | null, itemId: string, turnId: string, delta: string): ThreadStreamItem {
if (current && sameSourceItem(current, itemId) && current.kind === "dialogue" && current.dialogueKind === "assistantResponse") {
return {
...current,
text: `${current.text}${delta}`,
copyText: `${current.text}${delta}`,
turnId,
dialogueState: "streaming",
};
}
return {
id: itemId,
kind: "dialogue",
dialogueKind: "assistantResponse",
role: "assistant",
text: delta,
copyText: delta,
turnId,
sourceItemId: itemId,
provenance: { source: "appServer", channel: "notification", event: "streamingDelta", sourceItemId: itemId },
dialogueState: "streaming",
};
}
function appendPlanDelta(current: ThreadStreamItem | null, itemId: string, turnId: string, delta: string): ThreadStreamItem {
const previousText =
current && sameSourceItem(current, itemId) && current.kind === "dialogue" && current.role === "assistant" ? current.text : "";
const text = normalizeProposedPlanMarkdown(`${previousText}${delta}`);
return {
id: itemId,
kind: "dialogue",
dialogueKind: "proposedPlan",
role: "assistant",
text,
copyText: text,
turnId,
sourceItemId: itemId,
provenance: { source: "appServer", channel: "notification", event: "streamingDelta", sourceItemId: itemId },
dialogueState: "streaming",
};
}
function appendTextDelta(
current: ThreadStreamItem | null,
itemId: string,
turnId: string,
label: string,
delta: string,
kind: "tool" | "hook" | "reasoning",
): ThreadStreamItem {
if (current && sameSourceItem(current, itemId) && current.kind === kind) {
return { ...current, text: `${current.text ?? ""}${delta}`, turnId };
}
return streamedTextThreadStreamItem({ id: itemId, turnId, label, delta, kind });
}
function appendToolOutput(
current: ThreadStreamItem | null,
itemId: string,
turnId: string,
delta: string,
fallbackLabel: string,
): ThreadStreamItem {
if (current && sameSourceItem(current, itemId) && (current.kind === "tool" || current.kind === "hook")) {
return { ...current, output: `${current.output ?? ""}${delta}`, turnId };
}
return streamedToolOutputThreadStreamItem({ id: itemId, turnId, output: delta, fallbackLabel });
}
function latestDisplayableItem(items: readonly ThreadStreamItem[]): ThreadStreamItem | null {
return [...items].reverse().find((item) => item.kind !== "system" && !(item.kind === "dialogue" && item.role === "user")) ?? null;
}
function sameSourceItem(item: ThreadStreamItem, sourceItemId: string): boolean {
return item.id === sourceItemId || item.sourceItemId === sourceItemId;
}

View file

@ -5,21 +5,28 @@ import {
type ThreadRenameLifecycleEvent,
type ThreadRenameLifecycleState,
threadRenameGenerationStillActive,
threadRenameSaveStillActive,
transitionThreadRenameLifecycleState,
} from "../../../../domain/threads/rename-lifecycle";
import type { ThreadTitleContext } from "../../../../domain/threads/title-generation-model";
import type { DisclosureSetAction } from "./actions";
import { patchObject } from "./patch";
export type ChatRenameUiState = { readonly kind: "idle" } | (ThreadRenameActiveState & { readonly threadId: string });
export type ChatRenameGeneratingUiState = Extract<ChatRenameUiState, { kind: "generating" }>;
export type ChatRenameSavingUiState = Extract<ChatRenameUiState, { kind: "saving" }>;
type ChatRenameUiAction = Extract<
UiAction,
{
type:
| "ui/rename-started"
| "ui/rename-draft-updated"
| "ui/rename-auto-name-context-resolved"
| "ui/rename-cancelled"
| "ui/rename-save-started"
| "ui/rename-save-failed"
| "ui/rename-save-succeeded"
| "ui/rename-generation-started"
| "ui/rename-generation-succeeded"
| "ui/rename-generation-finished"
@ -71,7 +78,11 @@ export type UiAction =
| { type: "ui/archive-confirm-set"; threadId: string | null }
| { type: "ui/rename-started"; threadId: string; draft: string }
| { type: "ui/rename-draft-updated"; threadId: string; draft: string }
| { type: "ui/rename-auto-name-context-resolved"; threadId: string; context: ThreadTitleContext | null }
| { type: "ui/rename-cancelled"; threadId: string }
| { type: "ui/rename-save-started"; threadId: string; saveToken: number }
| { type: "ui/rename-save-failed"; threadId: string; saveToken: number }
| { type: "ui/rename-save-succeeded"; threadId: string; saveToken: number }
| { type: "ui/rename-generation-started"; threadId: string; generationToken: number }
| { type: "ui/rename-generation-succeeded"; threadId: string; generationToken: number; draft: string }
| { type: "ui/rename-generation-finished"; threadId: string; generationToken: number }
@ -99,7 +110,11 @@ export function isUiAction(action: { type: string }): action is UiAction {
case "ui/archive-confirm-set":
case "ui/rename-started":
case "ui/rename-draft-updated":
case "ui/rename-auto-name-context-resolved":
case "ui/rename-cancelled":
case "ui/rename-save-started":
case "ui/rename-save-failed":
case "ui/rename-save-succeeded":
case "ui/rename-generation-started":
case "ui/rename-generation-succeeded":
case "ui/rename-generation-finished":
@ -123,7 +138,11 @@ export function reduceUiSlice(state: ChatUiState, action: UiAction): ChatUiState
return patchObject(state, { archiveConfirmThreadId: action.threadId });
case "ui/rename-started":
case "ui/rename-draft-updated":
case "ui/rename-auto-name-context-resolved":
case "ui/rename-cancelled":
case "ui/rename-save-started":
case "ui/rename-save-failed":
case "ui/rename-save-succeeded":
case "ui/rename-generation-started":
case "ui/rename-generation-succeeded":
case "ui/rename-generation-finished":
@ -172,6 +191,10 @@ export function renameGenerationStillActive(
return state.kind === "generating" && state.threadId === threadId && threadRenameGenerationStillActive(state, generationToken);
}
export function renameSaveStillActive(state: ChatRenameUiState, threadId: string, saveToken: number): state is ChatRenameSavingUiState {
return state.kind === "saving" && state.threadId === threadId && threadRenameSaveStillActive(state, saveToken);
}
export function clearAllRequestDisclosures(state: ChatUiState): ChatUiState {
if (state.disclosures.approvalDetails.size === 0) return state;
return patchObject(state, {
@ -253,11 +276,23 @@ function goalEditorDraftUpdated(state: ChatGoalEditorUiState, objective: string)
function transitionChatRenameUiState(state: ChatRenameUiState, action: ChatRenameUiAction): ChatRenameUiState {
switch (action.type) {
case "ui/rename-started":
return { kind: "editing", threadId: action.threadId, draft: action.draft };
if (state.kind === "saving") return state;
return { kind: "editing", threadId: action.threadId, draft: action.draft, autoName: { kind: "checking" } };
case "ui/rename-draft-updated":
return transitionScopedChatRenameUiState(state, action.threadId, { type: "draft-updated", draft: action.draft });
case "ui/rename-auto-name-context-resolved":
return transitionScopedChatRenameUiState(state, action.threadId, {
type: "auto-name-context-resolved",
context: action.context,
});
case "ui/rename-cancelled":
return transitionScopedChatRenameUiState(state, action.threadId, { type: "cancelled" });
case "ui/rename-save-started":
return transitionScopedChatRenameUiState(state, action.threadId, { type: "save-started", saveToken: action.saveToken });
case "ui/rename-save-failed":
return transitionScopedChatRenameUiState(state, action.threadId, { type: "save-failed", saveToken: action.saveToken });
case "ui/rename-save-succeeded":
return transitionScopedChatRenameUiState(state, action.threadId, { type: "save-succeeded", saveToken: action.saveToken });
case "ui/rename-generation-started":
return transitionScopedChatRenameUiState(state, action.threadId, {
type: "generation-started",
@ -304,12 +339,14 @@ function chatRenameLifecycleStateWithoutThreadId(state: ChatRenameUiState): Thre
function chatRenameActiveStateWithoutThreadId(state: Exclude<ChatRenameUiState, { kind: "idle" }>): ThreadRenameActiveState {
switch (state.kind) {
case "editing":
return { kind: "editing", draft: state.draft };
return { kind: "editing", draft: state.draft, autoName: state.autoName };
case "saving":
return { kind: "saving", draft: state.draft, autoName: state.autoName, saveToken: state.saveToken };
case "generating":
return {
kind: "generating",
draft: state.draft,
originalDraft: state.originalDraft,
autoName: state.autoName,
generationToken: state.generationToken,
};
}

View file

@ -24,7 +24,6 @@ export function createAutoTitleCoordinator(host: AutoTitleCoordinatorHost): Auto
let activeThreadHadTurns = false;
let generation = 0;
const attemptedThreadIds = new Set<string>();
const inFlightThreadIds = new Set<string>();
const thread = (threadId: string): Thread | undefined =>
host.stateStore.getState().threadList.listedThreads.find((item) => item.id === threadId);
@ -44,8 +43,6 @@ export function createAutoTitleCoordinator(host: AutoTitleCoordinatorHost): Auto
});
} catch {
// Auto-title is best-effort metadata. Leave the thread preview untouched on failure.
} finally {
if (operationGeneration === generation) inFlightThreadIds.delete(threadId);
}
};
@ -54,7 +51,6 @@ export function createAutoTitleCoordinator(host: AutoTitleCoordinatorHost): Auto
generation += 1;
activeThreadHadTurns = false;
attemptedThreadIds.clear();
inFlightThreadIds.clear();
},
resetThreadTurnPresence(hadTurns) {
@ -67,12 +63,11 @@ export function createAutoTitleCoordinator(host: AutoTitleCoordinatorHost): Auto
if (hadTurnsBeforeThisCompletion) return;
if (threadHasTitle(threadId)) return;
if (attemptedThreadIds.has(threadId) || inFlightThreadIds.has(threadId)) return;
if (attemptedThreadIds.has(threadId)) return;
const context = host.completedTurnTitleContext(turnId, completedTurnTranscriptSummary);
if (!context) return;
attemptedThreadIds.add(threadId);
inFlightThreadIds.add(threadId);
void generateAndSetTitle(threadId, context, generation);
},
};

View file

@ -1,4 +1,6 @@
import { effectCompletedInCurrentContext } from "../effect-outcome";
import { ephemeralThreadActivatedAction } from "../state/actions";
import { capturePanelTargetLease, panelTargetLeaseIsCurrent } from "../state/panel-target";
import { activeThreadState } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
import { activeTurnId, chatTurnBusy } from "../turns/turn-state";
@ -30,6 +32,8 @@ export function createEphemeralThreadLifecycle(host: EphemeralThreadLifecycleHos
let disposed = false;
let openGeneration = 0;
const cleanupRequiredThreadIds = new Set<string>();
const openIsStale = (generation: number, panelTarget: ReturnType<typeof capturePanelTargetLease>): boolean =>
disposed || generation !== openGeneration || !panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget);
const unsubscribeActiveEphemeralThread = async (): Promise<boolean> => {
const active = activeThreadState(host.stateStore.getState());
if (active?.lifetime?.kind === "ephemeral") {
@ -41,16 +45,27 @@ export function createEphemeralThreadLifecycle(host: EphemeralThreadLifecycleHos
return {
async open(input): Promise<boolean> {
const generation = ++openGeneration;
const panelTarget = capturePanelTargetLease(host.stateStore.getState());
if (!(await host.ensureConnected())) return false;
const result = await host.transport.forkEphemeralThread(input.sourceThreadId);
if (!result) return false;
if (openIsStale(generation, panelTarget)) return false;
const effect = await host.transport.forkEphemeralThread(input.sourceThreadId);
if (!effectCompletedInCurrentContext(effect)) return false;
const result = effect.value;
if (result.kind === "cleanup-required") {
if (openIsStale(generation, panelTarget)) {
try {
await host.transport.unsubscribeEphemeralThread(result.threadId);
} catch {
// The connection close remains the final cleanup boundary.
}
return false;
}
cleanupRequiredThreadIds.add(result.threadId);
host.addSystemMessage("Could not prepare the side chat. Cleanup will be retried when this view closes.");
return false;
}
const snapshot = result;
if (disposed || generation !== openGeneration) {
if (generation !== openGeneration || !panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget)) {
try {
await host.transport.unsubscribeEphemeralThread(snapshot.activation.thread.id);
} catch {
@ -63,29 +78,36 @@ export function createEphemeralThreadLifecycle(host: EphemeralThreadLifecycleHos
response: snapshot.activation,
sourceThreadId: input.sourceThreadId,
sourceThreadTitle: input.sourceThreadTitle,
expectedPanelTargetRevision: panelTarget.revision,
}),
);
if (activeThreadState(host.stateStore.getState())?.id !== snapshot.activation.thread.id) return false;
host.notifyActiveThreadIdentityChanged();
return true;
},
async prepareForPersistentNavigation(): Promise<boolean> {
const state = host.stateStore.getState();
if (activeThreadState(state)?.lifetime?.kind !== "ephemeral") return true;
const active = activeThreadState(state);
if (active?.lifetime?.kind !== "ephemeral") return true;
const panelTarget = capturePanelTargetLease(state);
if (chatTurnBusy(state)) {
host.addSystemMessage("Finish or interrupt the current turn before switching threads.");
return false;
}
try {
if (!(await unsubscribeActiveEphemeralThread())) {
if (!panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget)) return false;
host.addSystemMessage("Could not discard the side chat. Try again before switching threads.");
return false;
}
} catch (error) {
if (!panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget)) return false;
host.addSystemMessage(error instanceof Error ? error.message : String(error));
return false;
}
host.stateStore.dispatch({ type: "active-thread/cleared" });
if (!panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget)) return false;
host.stateStore.dispatch({ type: "active-thread/cleared", expectedPanelTargetRevision: panelTarget.revision });
host.notifyActiveThreadIdentityChanged();
return true;
},

View file

@ -1,10 +1,11 @@
import type { ThreadActivationSnapshot } from "../../../../domain/threads/activation";
import type { EffectOutcome } from "../effect-outcome";
type EphemeralThreadForkResult =
export type EphemeralThreadForkResult =
| { kind: "ready"; activation: ThreadActivationSnapshot; sourceThreadId: string }
| { kind: "cleanup-required"; threadId: string };
export interface EphemeralThreadTransport {
forkEphemeralThread(sourceThreadId: string): Promise<EphemeralThreadForkResult | null>;
forkEphemeralThread(sourceThreadId: string): Promise<EffectOutcome<EphemeralThreadForkResult>>;
unsubscribeEphemeralThread(threadId: string): Promise<boolean>;
}

View file

@ -1,10 +1,15 @@
import type { ThreadGoal, ThreadGoalStatus, ThreadGoalUpdate } from "../../../../domain/threads/goal";
import { createKeyedOperationQueue, type KeyedOperationQueue } from "../../../../shared/runtime/keyed-operation-queue";
import { goalChangeItem } from "../../domain/thread-stream/factories/goal-items";
import type { GoalThreadStreamItem } from "../../domain/thread-stream/items";
import { effectCompletedInCurrentContext } from "../effect-outcome";
import type { LocalIdSource } from "../local-id-source";
import { activePanelOperationDecision } from "../panel-operation-policy";
import { capturePanelTargetLease, type PanelTargetLease, panelTargetLeaseIsCurrent } from "../state/panel-target";
import { activeThreadId, activeThreadState } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
import type { ThreadGoalReadTransport, ThreadGoalTransport } from "./goal-transport";
import type { ThreadStartOutcome } from "./thread-start-actions";
export interface ThreadGoalSyncHost {
stateStore: ChatStateStore;
@ -12,12 +17,23 @@ export interface ThreadGoalSyncHost {
localItemIds: LocalIdSource;
addSystemMessage: (text: string) => void;
addGoalEvent: (item: GoalThreadStreamItem) => void;
refreshLiveState: () => void;
}
export interface GoalActionsHost extends ThreadGoalSyncHost {
goalTransport: ThreadGoalTransport;
startThread: (preview?: string, options?: { syncGoal?: boolean }) => Promise<{ threadId: string } | null>;
startThread: (preview?: string, options?: { syncGoal?: boolean }) => Promise<ThreadStartOutcome>;
ensureRestoredThreadLoaded: () => Promise<boolean>;
}
interface GoalActionsContext extends GoalActionsHost {
goalOperations: ThreadGoalOperationCoordinator;
}
export interface ThreadGoalOperationCoordinator {
readonly goalMutations: KeyedOperationQueue<string>;
captureReadRevision(threadId: string): number;
readRevisionIsCurrent(threadId: string, revision: number): boolean;
markMutationCommitted(threadId: string): void;
}
export interface ThreadGoalSyncActions {
@ -44,24 +60,55 @@ type GoalObjectiveSavePlan =
type NormalizedGoalObjective = string & { readonly __brand: "NormalizedGoalObjective" };
const EMPTY_GOAL_OBJECTIVE_MESSAGE = "Goal objective cannot be empty.";
interface GoalMutationScope {
readonly panelTarget: PanelTargetLease;
}
export function createThreadGoalSyncActions(host: ThreadGoalSyncHost): ThreadGoalSyncActions {
interface GoalMutationOutcome {
readonly committed: boolean;
readonly presented: boolean;
}
const EMPTY_GOAL_OBJECTIVE_MESSAGE = "Goal objective cannot be empty.";
const GOAL_MUTATION_NOT_COMMITTED: GoalMutationOutcome = { committed: false, presented: false };
export function createThreadGoalOperationCoordinator(): ThreadGoalOperationCoordinator {
const readRevisions = new Map<string, number>();
const goalMutations = createKeyedOperationQueue<string>();
return {
syncThreadGoal: (threadId) => syncThreadGoal(host, threadId),
goalMutations,
captureReadRevision: (threadId) => readRevisions.get(threadId) ?? 0,
readRevisionIsCurrent: (threadId, revision) => (readRevisions.get(threadId) ?? 0) === revision,
markMutationCommitted: (threadId) => {
readRevisions.set(threadId, (readRevisions.get(threadId) ?? 0) + 1);
},
};
}
export function createGoalActions(host: GoalActionsHost): GoalActions {
export function createThreadGoalSyncActions(
host: ThreadGoalSyncHost,
goalOperations: ThreadGoalOperationCoordinator = createThreadGoalOperationCoordinator(),
): ThreadGoalSyncActions {
return {
syncThreadGoal: (threadId) => syncThreadGoal(host, threadId, goalOperations),
};
}
export function createGoalActions(
host: GoalActionsHost,
goalOperations: ThreadGoalOperationCoordinator = createThreadGoalOperationCoordinator(),
): GoalActions {
const context: GoalActionsContext = { ...host, goalOperations };
return {
activeGoal: () => activeThreadState(host.stateStore.getState())?.goal ?? null,
syncThreadGoal: (threadId) => syncThreadGoal(host, threadId),
saveObjective: (objective, tokenBudget) => saveObjective(host, objective, tokenBudget),
setObjective: (threadId, objective, tokenBudget) => setObjective(host, threadId, objective, tokenBudget),
setStatus: (threadId, status) => setGoalStatus(host, threadId, status),
clear: (threadId) => clearGoal(host, threadId),
syncThreadGoal: (threadId) => syncThreadGoal(host, threadId, goalOperations),
saveObjective: (objective, tokenBudget) => saveObjective(context, objective, tokenBudget),
setObjective: (threadId, objective, tokenBudget) =>
enqueueGoalMutation(context, threadId, (scope) => setObjective(context, threadId, objective, tokenBudget, scope)),
setStatus: (threadId, status) => enqueueGoalMutation(context, threadId, (scope) => setGoalStatus(context, threadId, status, scope)),
clear: (threadId) => enqueueGoalMutation(context, threadId, (scope) => clearGoal(context, threadId, scope)),
startEditingCurrent: () => {
startEditingCurrent(host);
void startEditingCurrent(context);
},
startEditing: (threadId, objective, tokenBudget) => {
startEditing(host, threadId, objective, tokenBudget);
@ -78,80 +125,117 @@ export function createGoalActions(host: GoalActionsHost): GoalActions {
};
}
async function syncThreadGoal(host: ThreadGoalSyncHost, threadId: string): Promise<void> {
async function syncThreadGoal(host: ThreadGoalSyncHost, threadId: string, goalOperations: ThreadGoalOperationCoordinator): Promise<void> {
const panelTarget = capturePanelTargetLease(host.stateStore.getState());
const readRevision = goalOperations.captureReadRevision(threadId);
try {
const goal = await host.goalTransport.readThreadGoal(threadId);
if (goal === undefined) return;
applyGoalIfActive(host, threadId, goal, { reportChange: false });
if (goal === undefined || !goalOperations.readRevisionIsCurrent(threadId, readRevision)) return;
applyGoalIfActive(host, threadId, goal, { reportChange: false, panelTarget });
} catch (error) {
addThreadScopedSystemMessage(host, threadId, `Could not load thread goal: ${errorMessage(error)}`);
if (!goalOperations.readRevisionIsCurrent(threadId, readRevision)) return;
addThreadScopedSystemMessage(host, threadId, `Could not load thread goal: ${errorMessage(error)}`, panelTarget);
}
}
async function setObjective(host: GoalActionsHost, threadId: string, objective: string, tokenBudget: number | null): Promise<boolean> {
async function setObjective(
host: GoalActionsContext,
threadId: string,
objective: string,
tokenBudget: number | null,
scope: GoalMutationScope,
): Promise<boolean> {
const normalized = normalizedGoalObjective(objective);
if (!normalized) {
host.addSystemMessage(EMPTY_GOAL_OBJECTIVE_MESSAGE);
return false;
}
return setNormalizedObjective(host, threadId, normalized, tokenBudget);
return setNormalizedObjective(host, threadId, normalized, tokenBudget, scope);
}
async function setNormalizedObjective(
host: GoalActionsHost,
host: GoalActionsContext,
threadId: string,
objective: NormalizedGoalObjective,
tokenBudget: number | null,
scope: GoalMutationScope,
): Promise<boolean> {
const current = activeThreadState(host.stateStore.getState())?.goal ?? null;
const isNewGoal = current === null;
const applied = await setGoal(host, threadId, {
objective,
status: current?.status ?? "active",
tokenBudget,
});
if (applied && isNewGoal) {
await recordGoalUserMessage(host, threadId, objective);
const outcome = await setGoal(
host,
threadId,
{
objective,
status: current?.status ?? "active",
tokenBudget,
},
scope,
);
if (outcome.committed && isNewGoal) {
await recordGoalUserMessage(host, threadId, objective, scope);
}
return applied;
return outcome.presented;
}
async function saveObjective(host: GoalActionsHost, objective: string, tokenBudget: number | null): Promise<boolean> {
async function saveObjective(host: GoalActionsContext, objective: string, tokenBudget: number | null): Promise<boolean> {
if (!(await prepareGoalMutation(host))) return false;
const plan = planGoalObjectiveSave(activeThreadId(host.stateStore.getState()), objective, tokenBudget);
switch (plan.kind) {
case "reject":
host.addSystemMessage(plan.message);
return false;
case "save-existing":
return setNormalizedObjective(host, plan.threadId, plan.objective, plan.tokenBudget);
return enqueueGoalMutation(host, plan.threadId, (scope) =>
setNormalizedObjective(host, plan.threadId, plan.objective, plan.tokenBudget, scope),
);
case "start-thread-and-save":
return startThreadAndSaveObjective(host, plan);
}
}
function setGoalStatus(host: GoalActionsHost, threadId: string, status: ThreadGoalStatus): Promise<boolean> {
return setGoal(host, threadId, { status });
async function setGoalStatus(
host: GoalActionsContext,
threadId: string,
status: ThreadGoalStatus,
scope: GoalMutationScope,
): Promise<boolean> {
return (await setGoal(host, threadId, { status }, scope)).presented;
}
async function clearGoal(host: GoalActionsHost, threadId: string): Promise<boolean> {
async function clearGoal(host: GoalActionsContext, threadId: string, scope: GoalMutationScope): Promise<boolean> {
try {
if (!(await host.goalTransport.clearThreadGoal(threadId))) return false;
applyGoalIfActive(host, threadId, null, { reportChange: true });
return true;
if (!(await host.goalTransport.ensureConnected()) || !goalMutationAdmissionIsCurrent(host, threadId, scope)) return false;
const effect = await host.goalTransport.clearThreadGoal(threadId);
if (!effectCompletedInCurrentContext(effect)) return false;
host.goalOperations.markMutationCommitted(threadId);
return applyGoalIfActive(host, threadId, null, { reportChange: true, panelTarget: scope.panelTarget });
} catch (error) {
addThreadScopedSystemMessage(host, threadId, errorMessage(error));
addThreadScopedSystemMessage(host, threadId, errorMessage(error), scope.panelTarget);
return false;
}
}
async function setGoal(host: GoalActionsHost, threadId: string, params: ThreadGoalUpdate): Promise<boolean> {
async function setGoal(
host: GoalActionsContext,
threadId: string,
params: ThreadGoalUpdate,
scope: GoalMutationScope,
): Promise<GoalMutationOutcome> {
try {
const goal = await host.goalTransport.setThreadGoal(threadId, params);
if (goal === undefined) return false;
return applyGoalIfActive(host, threadId, goal, { reportChange: true });
if (!(await host.goalTransport.ensureConnected()) || !goalMutationAdmissionIsCurrent(host, threadId, scope)) {
return GOAL_MUTATION_NOT_COMMITTED;
}
const effect = await host.goalTransport.setThreadGoal(threadId, params);
if (!effectCompletedInCurrentContext(effect)) return GOAL_MUTATION_NOT_COMMITTED;
host.goalOperations.markMutationCommitted(threadId);
return {
committed: true,
presented: applyGoalIfActive(host, threadId, effect.value, { reportChange: true, panelTarget: scope.panelTarget }),
};
} catch (error) {
addThreadScopedSystemMessage(host, threadId, errorMessage(error));
return false;
addThreadScopedSystemMessage(host, threadId, errorMessage(error), scope.panelTarget);
return GOAL_MUTATION_NOT_COMMITTED;
}
}
@ -159,24 +243,46 @@ function applyGoalIfActive(
host: ThreadGoalSyncHost,
threadId: string,
goal: ThreadGoal | null,
options: { reportChange: boolean },
options: { reportChange: boolean; panelTarget?: PanelTargetLease },
): boolean {
const state = host.stateStore.getState();
if (options.panelTarget && !panelTargetLeaseIsCurrent(state, options.panelTarget)) return false;
const activeThread = activeThreadState(state);
if (!activeThread || activeThread.id !== threadId) return false;
const item = options.reportChange ? goalChangeItem(host.localItemIds.next("goal"), activeThread.goal, goal) : null;
host.stateStore.dispatch({ type: "active-thread/goal-set", goal });
if (item) host.addGoalEvent(item);
host.refreshLiveState();
return true;
}
function startEditingCurrent(host: GoalActionsHost): void {
async function startEditingCurrent(host: GoalActionsContext): Promise<void> {
if (!(await prepareGoalMutation(host))) return;
host.stateStore.dispatch({ type: "ui/panel-set", panel: null });
const goal = activeThreadState(host.stateStore.getState())?.goal ?? null;
startEditing(host, goal?.threadId ?? null, goal?.objective ?? "", goal?.tokenBudget ?? null);
}
async function prepareGoalMutation(host: GoalActionsContext): Promise<boolean> {
const decision = activePanelOperationDecision(host.stateStore.getState(), "goal-mutation");
if (decision.kind === "allowed") return true;
if (decision.kind === "blocked") {
host.addSystemMessage(decision.message);
return false;
}
if (!(await host.ensureRestoredThreadLoaded())) return false;
const resumedDecision = activePanelOperationDecision(host.stateStore.getState(), "goal-mutation");
if (resumedDecision.kind === "allowed") return true;
if (resumedDecision.kind === "blocked") host.addSystemMessage(resumedDecision.message);
return false;
}
function goalMutationAllowedNow(host: GoalActionsHost): boolean {
const decision = activePanelOperationDecision(host.stateStore.getState(), "goal-mutation");
if (decision.kind === "allowed") return true;
if (decision.kind === "blocked") host.addSystemMessage(decision.message);
return false;
}
function startEditing(host: GoalActionsHost, threadId: string | null, objective: string, tokenBudget: number | null): void {
host.stateStore.dispatch({ type: "ui/goal-editor-started", threadId, objective, tokenBudget });
}
@ -195,33 +301,78 @@ function normalizedGoalObjective(objective: string): NormalizedGoalObjective | n
}
async function startThreadAndSaveObjective(
host: GoalActionsHost,
host: GoalActionsContext,
plan: Extract<GoalObjectiveSavePlan, { kind: "start-thread-and-save" }>,
): Promise<boolean> {
const panelTarget = capturePanelTargetLease(host.stateStore.getState());
try {
if (!(await host.goalTransport.ensureConnected())) return false;
const response = await host.startThread(plan.objective, { syncGoal: false });
const threadId = response?.threadId ?? null;
return threadId ? await setNormalizedObjective(host, threadId, plan.objective, plan.tokenBudget) : false;
if (!panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget) || !emptyPanelCanStartGoalThread(host)) return false;
const outcome = await host.startThread(plan.objective, { syncGoal: false });
if (outcome.kind === "created-not-activated") {
host.addSystemMessage(
`Created thread ${outcome.threadId}, but the connection changed before it could be opened. Resume it from history before setting its goal.`,
);
return false;
}
if (outcome.kind !== "created-activated") return false;
return await enqueueGoalMutation(host, outcome.threadId, (scope) =>
setNormalizedObjective(host, outcome.threadId, plan.objective, plan.tokenBudget, scope),
);
} catch (error) {
host.addSystemMessage(errorMessage(error));
if (panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget)) host.addSystemMessage(errorMessage(error));
return false;
}
}
async function recordGoalUserMessage(host: GoalActionsHost, threadId: string, objective: string): Promise<void> {
function emptyPanelCanStartGoalThread(host: GoalActionsHost): boolean {
const state = host.stateStore.getState();
return state.panelThread.kind === "empty" && activePanelOperationDecision(state, "goal-mutation").kind === "allowed";
}
async function recordGoalUserMessage(
host: GoalActionsContext,
threadId: string,
objective: string,
scope: GoalMutationScope,
): Promise<void> {
try {
await host.goalTransport.recordThreadGoalUserMessage(threadId, objective);
} catch (error) {
addThreadScopedSystemMessage(host, threadId, `Could not record goal message: ${errorMessage(error)}`);
addThreadScopedSystemMessage(host, threadId, `Could not record goal message: ${errorMessage(error)}`, scope.panelTarget);
}
}
function addThreadScopedSystemMessage(host: ThreadGoalSyncHost, threadId: string, text: string): void {
if (activeThreadId(host.stateStore.getState()) !== threadId) return;
function addThreadScopedSystemMessage(host: ThreadGoalSyncHost, threadId: string, text: string, panelTarget?: PanelTargetLease): void {
const state = host.stateStore.getState();
if ((panelTarget && !panelTargetLeaseIsCurrent(state, panelTarget)) || activeThreadId(state) !== threadId) return;
host.addSystemMessage(text);
}
function goalMutationPresentationIsCurrent(host: GoalActionsContext, threadId: string, scope: GoalMutationScope): boolean {
return (
panelTargetLeaseIsCurrent(host.stateStore.getState(), scope.panelTarget) && activeThreadId(host.stateStore.getState()) === threadId
);
}
function goalMutationAdmissionIsCurrent(host: GoalActionsContext, threadId: string, scope: GoalMutationScope): boolean {
return goalMutationPresentationIsCurrent(host, threadId, scope) && goalMutationAllowedNow(host);
}
function enqueueGoalMutation(
host: GoalActionsContext,
threadId: string,
operation: (scope: GoalMutationScope) => Promise<boolean>,
): Promise<boolean> {
const scope = {
panelTarget: capturePanelTargetLease(host.stateStore.getState()),
};
return host.goalOperations.goalMutations.run(threadId, async () => {
if (!goalMutationAdmissionIsCurrent(host, threadId, scope)) return false;
return operation(scope);
});
}
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}

View file

@ -1,12 +1,13 @@
import type { ThreadGoal, ThreadGoalUpdate } from "../../../../domain/threads/goal";
import type { EffectOutcome } from "../effect-outcome";
export interface ThreadGoalReadTransport {
readThreadGoal(threadId: string): Promise<ThreadGoal | null | undefined>;
}
export interface ThreadGoalTransport extends ThreadGoalReadTransport {
setThreadGoal(threadId: string, params: ThreadGoalUpdate): Promise<ThreadGoal | null | undefined>;
clearThreadGoal(threadId: string): Promise<boolean>;
setThreadGoal(threadId: string, params: ThreadGoalUpdate): Promise<EffectOutcome<ThreadGoal | null>>;
clearThreadGoal(threadId: string): Promise<EffectOutcome<void>>;
recordThreadGoalUserMessage(threadId: string, objective: string): Promise<boolean>;
ensureConnected(): Promise<boolean>;
}

View file

@ -0,0 +1,69 @@
import { activeThreadState } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
import { chatTurnBusy } from "../turns/turn-state";
import type { EphemeralThreadLifecycle } from "./ephemeral-thread-lifecycle";
import type { ThreadSubscriptionTransport } from "./thread-subscription-transport";
export interface PersistentNavigationLifecycle {
prepareForPersistentNavigation(targetThreadId: string | null): Promise<PersistentNavigationPreparation | null>;
commitPersistentNavigation(preparation: PersistentNavigationPreparation): void;
}
export type PersistentNavigationPreparation =
| { readonly kind: "ready" }
| { readonly kind: "unsubscribe-on-adoption"; readonly threadId: string };
interface PersistentNavigationLifecycleHost {
stateStore: ChatStateStore;
ephemeral: EphemeralThreadLifecycle;
subscriptions: ThreadSubscriptionTransport;
addSystemMessage(text: string): void;
}
export function createPersistentNavigationLifecycle(host: PersistentNavigationLifecycleHost): PersistentNavigationLifecycle {
const pendingUnsubscribes = new Set<string>();
const unsubscribeAttempts = new Map<string, Promise<void>>();
const attemptPendingUnsubscribe = (threadId: string): void => {
if (!pendingUnsubscribes.has(threadId) || unsubscribeAttempts.has(threadId)) return;
const attempt = (async (): Promise<void> => {
try {
if (await host.subscriptions.unsubscribeThread(threadId)) {
pendingUnsubscribes.delete(threadId);
return;
}
host.addSystemMessage("Could not unsubscribe from the previous subagent.");
} catch (error) {
host.addSystemMessage(error instanceof Error ? error.message : "Could not unsubscribe from the previous subagent.");
}
})().finally(() => {
unsubscribeAttempts.delete(threadId);
});
unsubscribeAttempts.set(threadId, attempt);
};
const retryPendingUnsubscribes = (): void => {
for (const threadId of pendingUnsubscribes) attemptPendingUnsubscribe(threadId);
};
return {
async prepareForPersistentNavigation(targetThreadId): Promise<PersistentNavigationPreparation | null> {
retryPendingUnsubscribes();
const state = host.stateStore.getState();
const active = activeThreadState(state);
if (!active || active.id === targetThreadId) return { kind: "ready" };
if (active.lifetime?.kind === "ephemeral") {
return (await host.ephemeral.prepareForPersistentNavigation()) ? { kind: "ready" } : null;
}
if (active.provenance?.kind !== "subagent" || !chatTurnBusy(state)) return { kind: "ready" };
return { kind: "unsubscribe-on-adoption", threadId: active.id };
},
commitPersistentNavigation(preparation): void {
if (preparation.kind !== "unsubscribe-on-adoption") return;
pendingUnsubscribes.add(preparation.threadId);
attemptPendingUnsubscribe(preparation.threadId);
},
};
}

View file

@ -3,7 +3,7 @@ import type { ThreadTitleContext } from "../../../../domain/threads/title-genera
import { activeThreadId, type ChatAction, type ChatState } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
import { threadStreamItems } from "../state/thread-stream";
import { type ChatRenameUiState, renameGenerationStillActive } from "../state/ui-state";
import { type ChatRenameUiState, renameGenerationStillActive, renameSaveStillActive } from "../state/ui-state";
import { firstThreadTitleContextFromThreadStreamItems } from "./title-context";
interface RenameEditState {
@ -16,7 +16,8 @@ export interface ThreadRenameEditorActionsHost {
ensureConnected: () => Promise<void>;
addSystemMessage: (text: string) => void;
renameThread(threadId: string, value: string): Promise<boolean>;
generateThreadTitle(threadId: string): Promise<string>;
resolveThreadTitleContext(threadId: string): Promise<ThreadTitleContext | null>;
generateThreadTitle(context: ThreadTitleContext, signal?: AbortSignal): Promise<string | null>;
}
export interface ThreadRenameEditorActions {
@ -26,15 +27,22 @@ export interface ThreadRenameEditorActions {
start(threadId: string): void;
updateDraft(threadId: string, value: string): void;
cancel(threadId: string): void;
cancelAutoName(threadId: string): void;
save(threadId: string, value: string): Promise<void>;
autoNameDraft(threadId: string): Promise<void>;
}
export function createThreadRenameEditorActions(host: ThreadRenameEditorActionsHost): ThreadRenameEditorActions {
let nextRenameGenerationToken = 1;
let nextRenameSaveToken = 1;
let activeGeneration: { threadId: string; generationToken: number; controller: AbortController } | null = null;
let activeContextPreparation: { threadId: string } | null = null;
const action = {
invalidate(): void {
activeGeneration?.controller.abort();
activeGeneration = null;
activeContextPreparation = null;
dispatch(host, { type: "ui/rename-cleared" });
},
@ -52,9 +60,13 @@ export function createThreadRenameEditorActions(host: ThreadRenameEditorActionsH
},
start(threadId: string): void {
const current = renameState(host);
if (current.kind === "saving") return;
const thread = host.stateStore.getState().threadList.listedThreads.find((item) => item.id === threadId);
if (!thread) return;
abortActiveGeneration();
dispatch(host, { type: "ui/rename-started", threadId, draft: threadRenameDraftTitle(thread) });
void prepareAutoName(threadId);
},
updateDraft(threadId: string, value: string): void {
@ -62,29 +74,38 @@ export function createThreadRenameEditorActions(host: ThreadRenameEditorActionsH
},
cancel(threadId: string): void {
const current = renameState(host);
if (current.kind === "saving" && current.threadId === threadId) return;
abortGeneration(threadId);
if (activeContextPreparation?.threadId === threadId) activeContextPreparation = null;
dispatch(host, { type: "ui/rename-cancelled", threadId });
},
cancelAutoName(threadId: string): void {
const current = renameState(host);
if (current.kind !== "generating" || current.threadId !== threadId) return;
abortGeneration(threadId);
finishAutoNameDraftGeneration(host, threadId, current.generationToken);
},
async save(threadId: string, value: string): Promise<void> {
const current = renameState(host);
if (current.kind === "idle" || current.threadId !== threadId || current.kind === "generating") return;
const editingState = current;
if (current.kind !== "editing" || current.threadId !== threadId) return;
const saveToken = nextRenameSaveToken;
dispatch(host, { type: "ui/rename-save-started", threadId, saveToken });
if (!renameSaveStillActive(renameState(host), threadId, saveToken)) return;
nextRenameSaveToken += 1;
try {
await host.ensureConnected();
if (renameState(host) !== editingState) return;
if (!renameSaveStillActive(renameState(host), threadId, saveToken)) return;
const result = await host.renameThread(threadId, value);
if (!result) {
if (renameState(host) === editingState) action.cancel(threadId);
return;
}
if (renameState(host) === editingState) {
dispatch(host, { type: "ui/rename-cleared" });
}
await host.renameThread(threadId, value);
dispatch(host, { type: "ui/rename-save-succeeded", threadId, saveToken });
} catch (error) {
if (renameState(host) !== editingState) return;
if (!renameSaveStillActive(renameState(host), threadId, saveToken)) return;
host.addSystemMessage(error instanceof Error ? error.message : String(error));
dispatch(host, { type: "ui/rename-save-failed", threadId, saveToken });
}
},
@ -92,34 +113,67 @@ export function createThreadRenameEditorActions(host: ThreadRenameEditorActionsH
const current = renameState(host);
if (current.kind !== "editing" || current.threadId !== threadId) return;
const editingState = current;
await host.ensureConnected();
if (renameState(host) !== editingState) return;
dispatch(host, {
type: "ui/rename-generation-started",
threadId,
generationToken: nextRenameGenerationToken,
});
const generationToken = nextRenameGenerationToken;
if (!renameGenerationStillActive(renameState(host), threadId, generationToken)) return;
const generating = renameState(host);
if (!renameGenerationStillActive(generating, threadId, generationToken)) return;
nextRenameGenerationToken += 1;
const controller = new AbortController();
activeGeneration = { threadId, generationToken, controller };
try {
const title = await host.generateThreadTitle(threadId);
const title = await host.generateThreadTitle(generating.autoName.context, controller.signal);
if (!title) throw new Error("Codex did not return a usable thread title.");
dispatch(host, { type: "ui/rename-generation-succeeded", threadId, generationToken, draft: title });
} catch (error) {
if (renameGenerationStillActive(renameState(host), threadId, generationToken)) {
host.addSystemMessage(error instanceof Error ? error.message : String(error));
}
} finally {
clearGeneration(generationToken);
finishAutoNameDraftGeneration(host, threadId, generationToken);
}
},
};
return action;
function abortGeneration(threadId: string): void {
if (activeGeneration?.threadId !== threadId) return;
abortActiveGeneration();
}
function abortActiveGeneration(): void {
if (!activeGeneration) return;
activeGeneration.controller.abort();
activeGeneration = null;
}
function clearGeneration(generationToken: number): void {
if (activeGeneration?.generationToken === generationToken) activeGeneration = null;
}
async function prepareAutoName(threadId: string): Promise<void> {
const preparation = { threadId };
activeContextPreparation = preparation;
let context: ThreadTitleContext | null = null;
try {
await host.ensureConnected();
if (activeContextPreparation !== preparation) return;
context = await host.resolveThreadTitleContext(threadId);
} catch {
// Auto-name availability is reflected by the disabled action.
}
if (activeContextPreparation !== preparation) return;
activeContextPreparation = null;
const current = renameState(host);
if ((current.kind !== "editing" && current.kind !== "saving") || current.threadId !== threadId) return;
dispatch(host, { type: "ui/rename-auto-name-context-resolved", threadId, context });
}
}
export function activeThreadRenameTitleContext(state: ChatState, threadId: string): ThreadTitleContext | null {

View file

@ -1,4 +1,5 @@
import { awaitingResumeThreadState } from "../state/root-reducer";
import { capturePanelTargetLease, type PanelTargetLease, panelTargetLeaseIsCurrent, panelTargetLeasesMatch } from "../state/panel-target";
import { activeThreadId, awaitingResumeThreadState } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
export interface RestorationControllerHost {
@ -8,7 +9,7 @@ export interface RestorationControllerHost {
export type RestoredThreadLoader = (threadId: string) => Promise<void>;
export class RestorationController {
private loading: { threadId: string; promise: Promise<void> } | null = null;
private loading: { threadId: string; panelTarget: PanelTargetLease; promise: Promise<void> } | null = null;
constructor(private readonly host: RestorationControllerHost) {}
@ -19,27 +20,30 @@ export class RestorationController {
async ensureLoaded(loadThread: RestoredThreadLoader): Promise<boolean> {
const restoredThread = awaitingResumeThreadState(this.host.stateStore.getState());
if (!restoredThread) return true;
if (this.loading?.threadId === restoredThread.threadId) {
await this.loading.promise;
return this.restorationLoaded();
const panelTarget = capturePanelTargetLease(this.host.stateStore.getState());
const activeLoading = this.loading;
if (activeLoading?.threadId === restoredThread.threadId && panelTargetLeasesMatch(activeLoading.panelTarget, panelTarget)) {
await activeLoading.promise;
return this.restorationLoaded(activeLoading.panelTarget, restoredThread.threadId);
}
const threadId = restoredThread.threadId;
const loading = { threadId, promise: loadThread(threadId) };
const loading = { threadId, panelTarget, promise: loadThread(threadId) };
this.loading = loading;
try {
await loading.promise;
} finally {
if (this.loading === loading) this.loading = null;
}
return this.restorationLoaded();
return this.restorationLoaded(loading.panelTarget, threadId);
}
isPending(threadId: string): boolean {
return awaitingResumeThreadState(this.host.stateStore.getState())?.threadId === threadId;
}
private restorationLoaded(): boolean {
return awaitingResumeThreadState(this.host.stateStore.getState()) === null;
private restorationLoaded(panelTarget: PanelTargetLease, threadId: string): boolean {
const state = this.host.stateStore.getState();
return panelTargetLeaseIsCurrent(state, panelTarget) && activeThreadId(state) === threadId;
}
}

View file

@ -1,5 +1,8 @@
import type { ThreadTokenUsage } from "../../../../domain/runtime/metrics";
import type { Thread } from "../../../../domain/threads/model";
import { effectCompletedInCurrentContext } from "../effect-outcome";
import { resumedThreadAction } from "../state/actions";
import { capturePanelTargetLease, type PanelTargetLease, panelTargetLeaseIsCurrent } from "../state/panel-target";
import { activeThreadState } from "../state/root-reducer";
import type { ChatStateStore } from "../state/store";
import { threadStreamIsEmpty } from "../state/thread-stream";
@ -16,81 +19,115 @@ export interface ResumeActionsHost {
closing: () => boolean;
resetThreadTurnPresence: (hadTurns: boolean) => void;
notifyActiveThreadIdentityChanged: () => void;
recordResumedThread: (thread: Thread) => void;
addSystemMessage: (text: string) => void;
refreshLiveState: () => void;
syncThreadGoal: (threadId: string) => Promise<void>;
recoverTokenUsageFromRollout?: (path: string) => Promise<ThreadTokenUsage | null>;
}
export interface ResumeActions {
resumeThread(threadId: string): Promise<void>;
resumeThread(threadId: string, intent?: ActiveChatResume, options?: ResumeThreadOptions): Promise<boolean>;
}
export interface ResumeThreadOptions {
onAdopted?: () => void;
}
export function createResumeActions(host: ResumeActionsHost): ResumeActions {
return {
resumeThread: (threadId) => resumeThread(host, threadId),
resumeThread: (threadId, intent, options) => resumeThread(host, threadId, intent, options),
};
}
async function resumeThread(host: ResumeActionsHost, threadId: string): Promise<void> {
async function resumeThread(
host: ResumeActionsHost,
threadId: string,
intent?: ActiveChatResume,
options?: ResumeThreadOptions,
): Promise<boolean> {
if (!canSwitchToThread(host.stateStore.getState(), threadId)) {
host.addSystemMessage("Finish or interrupt the current turn before switching threads.");
return;
return false;
}
const resume = host.resumeWork.begin(threadId);
const resume = intent ?? host.resumeWork.begin(threadId);
if (resume.threadId !== threadId || host.resumeWork.isStale(resume)) return false;
const initialPanelTarget = capturePanelTargetLease(host.stateStore.getState());
let currentPanelTarget = initialPanelTarget;
host.history.invalidate();
try {
const response = await host.resumeTransport.resumeThread(threadId);
if (!response) return;
if (isStaleResume(host, resume)) return;
applyResumedThread(host, response);
recoverResumedThreadTokenUsage(host, response.activation.thread.id, response.rolloutPath, resume);
if (response.initialHistoryPage) {
host.history.applyLatestPage(response.activation.thread.id, response.initialHistoryPage);
if (!(await host.resumeTransport.ensureConnected())) return false;
if (isStaleResume(host, resume, initialPanelTarget)) return false;
const effect = await host.resumeTransport.resumeThread(threadId);
if (!effectCompletedInCurrentContext(effect)) return false;
if (isStaleResume(host, resume, initialPanelTarget)) return false;
const adoptedPanelTarget = applyResumedThread(host, effect.value, initialPanelTarget.revision);
if (!adoptedPanelTarget) return false;
currentPanelTarget = adoptedPanelTarget;
host.recordResumedThread(effect.value.activation.thread);
options?.onAdopted?.();
recoverResumedThreadTokenUsage(host, effect.value.activation.thread.id, effect.value.rolloutPath, resume, adoptedPanelTarget);
if (effect.value.initialHistoryPage) {
host.history.applyLatestPage(effect.value.activation.thread.id, effect.value.initialHistoryPage);
} else {
await host.history.loadLatest(response.activation.thread.id);
await host.history.loadLatest(effect.value.activation.thread.id);
}
if (isStaleResume(host, resume)) return;
await host.syncThreadGoal(response.activation.thread.id);
if (isStaleResume(host, resume)) return;
if (isStaleResume(host, resume, adoptedPanelTarget)) return false;
await host.syncThreadGoal(effect.value.activation.thread.id);
if (isStaleResume(host, resume, adoptedPanelTarget)) return false;
const renderFallbackMessage = threadStreamIsEmpty(host.stateStore.getState().threadStream);
if (renderFallbackMessage) {
host.addSystemMessage(`Resumed thread ${response.activation.thread.id}`);
host.addSystemMessage(`Resumed thread ${effect.value.activation.thread.id}`);
}
host.refreshLiveState();
return true;
} catch (error) {
if (isStaleResume(host, resume)) return;
if (isStaleResume(host, resume, currentPanelTarget)) return false;
host.addSystemMessage(error instanceof Error ? error.message : String(error));
return false;
}
}
function applyResumedThread(host: ResumeActionsHost, response: ThreadResumeSnapshot): void {
host.stateStore.dispatch(
function applyResumedThread(
host: ResumeActionsHost,
response: ThreadResumeSnapshot,
expectedPanelTargetRevision: number,
): PanelTargetLease | null {
const state = host.stateStore.dispatch(
resumedThreadAction({
response: response.activation,
listedThreads: host.stateStore.getState().threadList.listedThreads,
expectedPanelTargetRevision,
}),
);
if (activeThreadState(state)?.id !== response.activation.thread.id) return null;
host.resetThreadTurnPresence(false);
host.notifyActiveThreadIdentityChanged();
return capturePanelTargetLease(state);
}
function recoverResumedThreadTokenUsage(host: ResumeActionsHost, threadId: string, path: string | null, resume: ActiveChatResume): void {
function recoverResumedThreadTokenUsage(
host: ResumeActionsHost,
threadId: string,
path: string | null,
resume: ActiveChatResume,
panelTarget: PanelTargetLease,
): void {
if (!path || !host.recoverTokenUsageFromRollout) return;
void host
.recoverTokenUsageFromRollout(path)
.then((tokenUsage) => {
if (!tokenUsage || isStaleResume(host, resume)) return;
if (!tokenUsage || isStaleResume(host, resume, panelTarget)) return;
const state = host.stateStore.getState();
const activeThread = activeThreadState(state);
if (!activeThread || activeThread.id !== threadId || activeThread.tokenUsage !== null) return;
host.stateStore.dispatch({ type: "active-thread/token-usage-set", tokenUsage });
host.refreshLiveState();
})
.catch(() => undefined);
}
function isStaleResume(host: ResumeActionsHost, resume: ActiveChatResume): boolean {
return host.resumeWork.isStale(resume) || host.closing();
function isStaleResume(host: ResumeActionsHost, resume: ActiveChatResume, panelTarget?: PanelTargetLease): boolean {
return (
host.resumeWork.isStale(resume) ||
host.closing() ||
Boolean(panelTarget && !panelTargetLeaseIsCurrent(host.stateStore.getState(), panelTarget))
);
}

View file

@ -1,11 +1,11 @@
export type ChatResumeLifecycleState = { kind: "idle" } | { kind: "resuming"; threadId: string };
export type ChatResumeLifecycleState = { kind: "idle" } | { kind: "resuming"; threadId: string | null };
export type ActiveChatResume = Extract<ChatResumeLifecycleState, { kind: "resuming" }>;
type ChatResumeLifecycleEvent = { type: "started"; resume: ActiveChatResume } | { type: "invalidated" };
export class ChatResumeWorkTracker {
private state: ChatResumeLifecycleState = { kind: "idle" };
begin(threadId: string): ActiveChatResume {
begin(threadId: string | null): ActiveChatResume {
const resume: ActiveChatResume = { kind: "resuming", threadId };
this.state = transitionChatResumeLifecycle(this.state, { type: "started", resume });
return resume;
@ -18,6 +18,10 @@ export class ChatResumeWorkTracker {
isStale(resume: ActiveChatResume): boolean {
return this.state !== resume;
}
isCurrent(resume: ActiveChatResume): boolean {
return this.state === resume;
}
}
function transitionChatResumeLifecycle(state: ChatResumeLifecycleState, event: ChatResumeLifecycleEvent): ChatResumeLifecycleState {

View file

@ -1,5 +1,6 @@
import type { ThreadActivationSnapshot } from "../../../../domain/threads/activation";
import type { ThreadStreamItem } from "../../domain/thread-stream/items";
import type { EffectOutcome } from "../effect-outcome";
export interface ThreadHistoryPage {
items: ThreadStreamItem[];
@ -18,5 +19,6 @@ export interface ThreadResumeSnapshot {
}
export interface ThreadResumeTransport {
resumeThread(threadId: string): Promise<ThreadResumeSnapshot | null>;
ensureConnected(): Promise<boolean>;
resumeThread(threadId: string): Promise<EffectOutcome<ThreadResumeSnapshot>>;
}

Some files were not shown because too many files have changed in this diff Show more