diff --git a/.github/workflows/performance-contracts.yml b/.github/workflows/performance-contracts.yml index 2af8bb94c6f..c2c330ea00f 100644 --- a/.github/workflows/performance-contracts.yml +++ b/.github/workflows/performance-contracts.yml @@ -33,7 +33,6 @@ on: - 'src/renderer/src/lib/pane-manager/pane-terminal-output-scheduler-queue-retention.test.ts' - 'src/main/claude-usage/scanner-incremental-append.test.ts' - 'src/main/claude-usage/store-checkpoint-persistence.test.ts' - - 'src/renderer/src/components/native-chat/surfaces/integration/NativeChatResolvedView.streaming-input.test.tsx' permissions: contents: read diff --git a/config/reliability-gates.jsonc b/config/reliability-gates.jsonc index 744d4f96f31..28b0a2371fb 100644 --- a/config/reliability-gates.jsonc +++ b/config/reliability-gates.jsonc @@ -6086,6 +6086,7 @@ "Keep missing-proof mixed-version behavior identical to the incumbent client path." ], "knownGaps": [ + "Audit3037 removes seven supplied-input baseline cases from the terminal-input-gating registrar. Input-and-repaint wrapper selectors and unrelated assertions remain; historical receipts do not validate the reduced source bytes. Pane authority, acknowledgment refusal and quarantine recovery cases remain, without native or RPC execution claims.", "Audit2145 accepts removal of canned runtime metadata/guard/forwarding, fake in-memory persistence, private work-count and wrapper-duplication tests from the construction/orca-runtime.test.ts aggregate, plus the two automation run-context guards from runtime-workspace-launch-contracts.test.ts. Retained race, framing, physical completion and ownership callbacks remain byte-identical. Existing commands remain valid selectors; historical receipts stay unchanged and provide no fresh validation or replacement coverage for the reduced suites.", "Audit2133 current-parent preparation (988 authored removals; independently reviewed2099/2107/2115/2116 decisions; upstream clear-buffer and recovery-image regressions retained): Intentionally retire these low-value alarms without replacement: src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-hidden-replay.test.ts: structural support changed only; no new authored removal credit. src/main/runtime/construction/orca-runtime.test.ts: remove only literal registration import/call pairs await import('../orca-runtime-tests/worktree-setup-and-startup.spec'), await import('../orca-runtime-tests/gitlab-and-pr-bases.spec'); no additional authored credit. src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-input-and-repaint.test.ts: remove only literal registration import/call pairs import { registerPtyConnectionFreshNativeFollowResetTests } from '../output/pty-connection-fresh-native-follow-reset.spec', registerPtyConnectionFreshNativeFollowResetTests(); no additional authored credit. src/main/runtime/orca-runtime-tests/targeting-and-resilience.spec.ts: routes tab switch and capture start by explicit page id; accepts focus on tab switch without altering bridge args (focus is main-side concern); keeps terminal cwd resolution fail-soft when the provider is unavailable. src/main/runtime/orca-runtime-tests/worktree-removal-and-reconciliation-part-04.spec.ts: drops the bounded scan cache when orphan-cleanup removal completes; clears optimistic reconcile tokens when a CLI worktree removal succeeds; emits drift without mutating when the fetched base fast-forwards created HEAD; emits base_changed without mutation when the fetched base rewrote history; skips stale-token reconciles without mutating or emitting stale status; emits unknown without mutation when fetch fails or the base ref is missing. src/main/runtime/orca-runtime-tests/worktree-removal-and-reconciliation-part-03.spec.ts: routes runtime preserved-branch force-delete through the selected WSL project runtime; replies to a waiting client with the error of a delete that fails after acceptance; treats forced runtime deletion of an already-missing unregistered worktree as cleanup; treats normal runtime deletion of an already-missing unregistered worktree as cleanup. src/main/runtime/orca-runtime-tests/worktree-removal-and-reconciliation-part-02.spec.ts: warns that a missing-repo removal only forgot the workspace; restores runtime watchers when CLI worktree deletion fails after teardown; does not re-spend the drain budget on a removal after a wedged install was abandoned; routes runtime worktree removal through the selected WSL project runtime; force-deletes a branch that was preserved by runtime worktree removal. src/main/runtime/orca-runtime-tests/gitlab-and-pr-bases-part-02.spec.ts: records GitLab pasted-project recents only after successful runtime lookup; threads explicit origin preference through runtime WSL PR base resolution; resolves local GitLab fork MR bases from the target project MR head ref; captures the fork MR head from a dedicated ref, not the shared FETCH_HEAD; keeps the durable MR head when the head fetch fails but the local ref resolves; routes runtime GitLab fork MR base git calls through the selected WSL project runtime. src/main/runtime/orca-runtime-tests/gitlab-and-pr-bases.spec.ts: passes SSH connection ids through GitLab task operations; routes runtime GitLab issue, MR, work-item, and todo actions through the selected WSL project runtime; routes runtime GitLab MR details, review-management, job, and pasted URL actions through the selected WSL project runtime; normalizes runtime GitLab issue list arguments like the desktop IPC path. src/main/runtime/orca-runtime-tests/worktree-setup-and-startup-part-03.spec.ts: does not warn when setup is explicitly skipped for CLI-created worktrees; puts the agent a create starts in the first default tab, as the window does; keeps the 8s main-runtime startup readiness budget for agents without an override; gives OpenCode the 20s main-runtime startup readiness budget; launches explicit startup agents with prompts for CLI-created worktrees. src/main/runtime/orca-runtime-tests/worktree-setup-and-startup.spec.ts: returns a setup launch payload for CLI-created worktrees when hooks are explicitly enabled; does not hand the window a setup main never tried to start; passes the selected Windows setup shell into runtime runner generation; follows normal setup policy for CLI-created worktrees without activating them; uses returned WSL setup shell metadata when runtime spawns setup; uses the shell-aware setup runner for windowless creates without a startup command; reports the in-process setup hook as running when nothing can launch the runner. src/main/runtime/orca-runtime-tests/terminal-sleep-and-teardown-part-02.spec.ts: coalesces two clients sleeping the same host worktree; stops exactly the expected live PTYs for a worktree; explains a refused workspace close instead of throwing the raw refusal enum. src/main/runtime/orca-runtime-tests/mobile-summaries-part-03.spec.ts: marks the desktop-active worktree as isActive; includes SSH-backed worktrees in the mobile worktree summary; caches repeated malformed path misses before normalizing them again. src/main/runtime/orca-runtime-tests/mobile-summaries-part-02.spec.ts: keeps title-only foreground work ahead of monitoring in another pane; keeps a local hook row while a connected PTY still backs its pane; keeps an OSC row via its connected PTY after the pane binding is cleared. src/main/runtime/orca-runtime-tests/mobile-summaries.spec.ts: resolves files through a source-visible linked-checkout worktree; marks saved session tabs with live PTYs as host sidebar activity; attributes live legacy PTYs from saved layout bindings when their panes are hidden; marks saved browser tabs as host sidebar activity like desktop; falls back to the path-keyed GitHub cache entry; includes folder workspaces in compact worktree summaries for mobile; attaches inline agent rows from the latest OSC 9999 status; projects monitoring over a title-derived working status. src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-03.spec.ts: pushes to an idle pane when the only live waiter filters out the message type; resolves a registered waiter without PTY-injecting when the leaf is already idle; keeps already-idle status after tui-idle wait for immediate message delivery; resolves message waiters when notifyMessageArrived is called; does not resolve type-filtered message waiters for unrelated message types; removes message waiter abort listeners after message arrival; resolves message waiters on timeout when no message arrives. src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-02.spec.ts: delivers pending mail via notifyMessageArrived when the recipient is already idle; does not keep retrying when every pending row was already pointed; repoints pending rows restored with a live-idle coordinator. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-13.spec.ts: rolls back a half-created terminal whose surface never publishes. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-12.spec.ts: rejects startup prompts for agents that require post-ready stdin; uses portable Unix quoting for mobile agent launch commands in WSL project runtimes; keeps PowerShell quoting for mobile agent launch commands in Windows host runtimes; uses cmd.exe quoting for mobile agent launch commands in local Windows host runtimes; publishes headless mobile session agent identity with synthesized PTY status; rejects disabled mobile session agent launches before spawning; validates mobile terminal insertion anchors before resolving agent launch commands. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-11.spec.ts: coalesces a reconnect close burst onto one authoritative PTY inventory; keeps an explicit user close destructive while the PTY is live; builds mobile session agent launch commands on the runtime host; injects mobile quick-command prompts into the host-built agent startup command. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-10.spec.ts: closes persisted headless terminal parents before any prior list call; tears down a serve-owned headless tab on close while a renderer is attached so it cannot resurrect; defers a renderer-published pending tab to the renderer instead of tearing it down; returns a delegated close outcome with no selection tombstone when the renderer owns the outcome; tears down a runtime pending shell the renderer never adopted on close. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-08.spec.ts: materializes a plain shell when the pending tab has no launch agent; hydrates legacy persisted terminal tabs without layout entries; reads and indexes the full agent-status snapshot once per mobile projection; kills persisted SSH PTYs when closing hydrated headless tabs before pane metadata is restored; materializes hydrated pending headless terminals with the persisted session identity. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-07.spec.ts: hydrates a persisted SSH-owned pane before an attached renderer publishes its graph. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-05.spec.ts: publishes the desktop's tab name over the live agent title, and the live title without one; uses fresh neutral PTY titles over stale mobile snapshot and OSC titles; operates PTY-backed mobile session terminals without a renderer graph; reuses pane close for live PTYs that do not own a renderer tab; lists PTY-backed mobile session terminals without a renderer graph; shows and resolves active PTY-backed mobile session terminals without a renderer graph. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-04.spec.ts: creates mobile session terminals for folder workspaces in a headless runtime server; spawns fresh headless SSH mobile session terminals instead of reattaching synthetic local ids; hydrates headless mobile session terminals from the host workspace session; keeps live headless mobile session terminals when a desktop renderer publishes without them; publishes laptop-created remote runtime split terminals to phone session tabs. src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-02.spec.ts: does not report headless wake degradation without sleeping records; does not wake slept agents for non-mobile session-only activation; does not rewrite unread metadata when a mobile activation finds the worktree already read; returns unread:false from worktree.ps after a mobile activation clears the flag; materializes pending mobile session terminals without focusing desktop clients; closes browser mobile session tabs when addressed by browser workspace id. src/main/runtime/orca-runtime-tests/mobile-session-tabs.spec.ts: resolves mobile terminal surfaces by exact split leaf; closes the matching mobile terminal UUID leaf without closing the whole tab; closes the whole mobile terminal tab when addressed by parent tab id; activates the active split leaf when addressed by parent tab id; clears unread metadata on mobile worktree activation without focusing desktop clients; wakes slept agents on the host renderer when a phone activates a worktree. src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-05.spec.ts: publishes the hook provider session on a headless mobile tab so native chat can address the transcript; recovers the agent type from the hook row when the pane was launched without an agent hint; reads one agent-status snapshot per projection, not one per terminal tab; waits for unknown-launch foreground owner before publishing Pi-compatible mobile status; keeps Pi-compatible mobile session status as Pi for an unknown-launch foreground pi PTY; keeps renderer-vetted mobile agent status for custom-titled terminals. src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-04.spec.ts: publishes terminal surface agent status for paired web clients; preserves authoritative OMP identity for Pi-compatible remote terminal snapshots; normalizes a remote OMP title without republishing omitted launch identity; skips the foreground-process probe when the PTY launch agent is already known; probes the foreground process only on a status transition for unknown launch agents; passes the client signal and the slept-worktree refusal to the spawn. src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-03.spec.ts: does not use stale runtime-created PTY status when a neutral PTY title exists; recognizes runtime-created Codex PTY handles from the ready prompt; recognizes runtime-created Antigravity PTY handles from the ready prompt; recognizes live leaf Antigravity terminals from the ready prompt; recognizes a later Antigravity header with a prompt but no model line; keeps mobile terminal surfaces visible while their leaf handle is pending. src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-02.spec.ts: lets Claude agents management titles clear stale runtime-created title status; does not recognize live Claude agents panes from a Claude foreground process; does not let stale tab-level agent titles override current neutral pane titles; lists live terminals with fresh pane titles over stale tab titles; lets adopted neutral pane titles use non-shell foreground fallback; lets adopted neutral pane titles retry wrapper foregrounds until recognized; authorizes settled CLI prompts only after positive %s foreground identity; does not recognize arbitrary foreground TUIs as running agents; lets live neutral pane titles retry wrapper foregrounds until recognized; lets current Claude agents PTY titles override stale runtime-created OSC titles; does not let stale Claude agents OSC titles suppress current PTY title activity. src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status.spec.ts: keeps preallocated terminal handles valid across renderer reloads; preserves runtime-created PTY process identity after graph unavailable; keeps prompt bindings fenced across runtime restarts without provider incarnation; recognizes runtime-created PTY handles with agent launch titles; does not treat a bare Cursor Agent native title as a running agent session; uses stale runtime-created PTY status when there is no title or foreground evidence. src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-07.spec.ts: keeps orphaned list and show writability aligned with the send gate. src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-04.spec.ts: cancels a coalesced SSH worker recovery retry when its provider disconnects. src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-03.spec.ts: retries renderer reveal before clearing an adopted legacy worker sleeping record. src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-02.spec.ts: recovers exported ORCA_TERMINAL_HANDLE from discovered live PTY sessions. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-12.spec.ts: applies ANSI erase-from-start line controls in retained previews; applies ANSI stripping for private or intermediate CSI line controls; applies ANSI stripping for unsupported erase-line modes; reveals an agent terminal under the pane the caller reserved; keeps a fresh %s pane host-owned when a desktop window is available; keeps desktop publication %s separate from the reserved host spawn; mints its own pane when the reserved leaf is malformed, and reports the one it used; starts an agent terminal with the model the caller picked; starts an agent terminal without a model flag when no pick was made; attributes a startup-agent launch named %s as %s; attributes a fresh agent session the host builds as unknown; leaves a bare agent command the user typed out of launch attribution. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-11.spec.ts: trims oversized terminal output bursts without per-line array shifts; builds terminal previews without mapping the full retained tail; keeps recent PTY replay output capped without needing previous data for large chunks; applies terminal redraw controls before retaining previews; applies ANSI terminal redraw controls before retaining previews; retains multi-line ANSI footer redraws instead of appending old frames; keeps the cursor column when erasing full lines in multi-line redraws; applies ANSI split redraw controls without leaking raw params. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-10.spec.ts: returns renderer visible screen lines through terminal.read RPC JSON result; separates composer drafts from provider-owned terminal screens. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-09.spec.ts: reports an in-flight PTY focus as not navigated when its notifier disappears; reports graph-leaf focus as not navigated without a host notifier; grounds the provider mode tracker before a later chunk, in the emulator order; grounds an in-flight provider snapshot capture so it cannot publish the pre-reset screen; waits for terminal exit and resolves with the exit status; keeps terminal read payloads bounded while retained output remains pageable; trims terminal read preview character budget without per-line array shifts. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-08.spec.ts: submits a silent Claude composer once after the bounded render fallback; chunks large terminal.send text before provider writes; yields chunked terminal input through immediates between writes; yields while validating accepted large terminal.send text before provider writes; reveals a background terminal session when focusing its handle; replays captured launch config when focusing a background agent session; reveals background terminal sessions with the freshest PTY title; renames background terminal handles without requiring a visible tab; keeps a background terminal handle stable while reveal adoption is racing. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-07.spec.ts: resolves tui-idle when a stale Codex prompt is followed by Antigravity readiness; returns an agent-neutral blocked wait result for update prompts; returns an agent-neutral blocked wait result for workspace trust prompts; returns an agent-neutral blocked wait result for cwd selection prompts; returns a blocked wait result for Codex model migration prompts; returns a blocked wait result for Codex startup hook review prompts; returns an agent-neutral blocked wait result for generic interactive prompts; splits text and enter writes for background terminal handles; types the lead line for %s: %s; submits through the agent-specific PTY timing policy for %s. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-06.spec.ts: waits for exit on background terminal handles; keeps retained PTY transcript memory when controller refresh fails; resolves tui-idle from an Antigravity ready prompt preview; resolves Antigravity ready prompts with newline-heavy pasted tails without splitting; resolves live-leaf tui-idle from an Antigravity ready prompt preview. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-03.spec.ts: spawns the available Qoder command for a captured same-session resume: %s; does not use the local Windows shell setting for remote Windows bare agent creates; matches canonical bare agent commands when a command override is configured; keeps non-bare agent command terminal creates unchanged; keeps disabled bare agent command terminal creates unchanged; injects runtime hook receiver env into terminal sessions; creates background terminal sessions for a $label; creates background terminal sessions for a $label; enables Claude Agent Teams only for direct Claude launches when configured in-process; reveals Claude Agent Teams launches with the rewritten launch config; preserves Claude Agent Teams for sequenced Claude launches. src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness.spec.ts: creates visible terminal sessions without asking the renderer to focus a tab; asks the pty controller for the requested shell instead of a startup command; knows the agent a flagged launch command names before any process read; retires a flagged command pane identity when the command exits; retires only receipted restored PTY authority on command completion and exit; forgets synthetic handles when disconnected PTY records are pruned. src/main/runtime/orca-runtime-tests/headless-snapshots.spec.ts: returns OSC titles from headless main terminal snapshots; adopts renderer-seeded titles into headless main terminal snapshots; returns cwd metadata from seeded headless main terminal snapshots; adopts OSC7 host metadata from seeded headless terminal scrollback; falls back to the renderer snapshot for hidden-output recovery without headless state; keeps an empty headless snapshot authoritative for hidden-output recovery; advances the absolute output sequence across a daemon stream gap; emits explicit OSC 9999 agent status from runtime PTY data. src/main/runtime/orca-runtime-tests/terminal-side-effect-facts-part-02.spec.ts: falls back to the provider snapshot for a restored PTY with no mounted renderer; falls back to the available mirror when authoritative provider history is unavailable; keeps an empty renderer snapshot when the provider has no retained content; keeps restored provider history authoritative after later live output; uses provider alternate-screen state while a partial model is unsafe; prefers the tracked title over the headless emulator lastTitle; keeps the cursor-agent literal in record-fallback snapshots only without a tracker title; touches mobile snapshots once for decorative spinner ticks, again on idle. src/main/runtime/orca-runtime-tests/local-worktree-creation-part-02.spec.ts: creates a same-repo PR branch override from a resolved head SHA and matching push target; skips broad remote fetch for an existing full-SHA PR base; creates a selected Bitbucket PR branch override from a matching remote branch; suffixes an existing PR when a matching push target lacks selected PR metadata; suffixes a matching push target branch when selected PR metadata has no PR number; suffixes a matching push target branch when the existing PR is different; suffixes a selected PR remote conflict when the PR lookup fails; checks out an unused runtime PR branch only when it is at the resolved head SHA; suffixes only the runtime worktree path when an exact PR branch checkout path exists. src/main/runtime/orca-runtime-tests/local-worktree-creation.spec.ts: returns runtime local base update suggestions from addWorktree; creates a runtime local worktree from a usable persisted local branch base; does not create a runtime local worktree when the refresh fails and no local base ref exists; creates a branchNameOverride worktree from the selected matching remote base ref. src/renderer/src/components/terminal-pane/pty-connection/output-restore/pty-connection-dropped-output-snapshot-healing.spec.ts: repaints from the main-owned snapshot when main drops pending output at the cap. src/renderer/src/components/terminal-pane/pty-connection/emulation/pty-connection-mode2031-raw-subscription.spec.ts: records a mode 2031 subscribe from the raw chunk boundary without answering. src/renderer/src/components/terminal-pane/pty-connection/output/pty-connection-fresh-native-follow-reset.spec.ts: retries a fresh-spawn native follow reset when renderer dimensions return. Current changed source bytes, wrapper registrations and reduced selectors have no fresh execution or red/green credit. Unrelated surviving receipt objects keep their original narrow scope; current literal graph reachability does not establish historical import closure or equivalent replacement coverage. Current conservative command/import graph reaches src/main/runtime/construction/orca-runtime.test.ts, src/main/runtime/orca-runtime-tests/targeting-and-resilience.spec.ts, src/main/runtime/orca-runtime-tests/worktree-removal-and-reconciliation-part-04.spec.ts, src/main/runtime/orca-runtime-tests/worktree-removal-and-reconciliation-part-03.spec.ts, src/main/runtime/orca-runtime-tests/worktree-removal-and-reconciliation-part-02.spec.ts, src/main/runtime/orca-runtime-tests/gitlab-and-pr-bases-part-02.spec.ts, src/main/runtime/orca-runtime-tests/gitlab-and-pr-bases.spec.ts, src/main/runtime/orca-runtime-tests/worktree-setup-and-startup-part-03.spec.ts, src/main/runtime/orca-runtime-tests/worktree-setup-and-startup.spec.ts, src/main/runtime/orca-runtime-tests/terminal-sleep-and-teardown-part-02.spec.ts, src/main/runtime/orca-runtime-tests/mobile-summaries-part-03.spec.ts, src/main/runtime/orca-runtime-tests/mobile-summaries-part-02.spec.ts, src/main/runtime/orca-runtime-tests/mobile-summaries.spec.ts, src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-03.spec.ts, src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-02.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-13.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-12.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-11.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-10.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-08.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-07.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-05.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-04.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs-part-02.spec.ts, src/main/runtime/orca-runtime-tests/mobile-session-tabs.spec.ts, src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-05.spec.ts, src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-04.spec.ts, src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-03.spec.ts, src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status-part-02.spec.ts, src/main/runtime/orca-runtime-tests/terminal-handles-and-agent-status.spec.ts, src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-07.spec.ts, src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-04.spec.ts, src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-03.spec.ts, src/main/runtime/orca-runtime-tests/terminal-output-and-worker-recovery-part-02.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-12.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-11.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-10.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-09.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-08.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-07.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-06.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-03.spec.ts, src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness.spec.ts, src/main/runtime/orca-runtime-tests/headless-snapshots.spec.ts, src/main/runtime/orca-runtime-tests/terminal-side-effect-facts-part-02.spec.ts, src/main/runtime/orca-runtime-tests/local-worktree-creation-part-02.spec.ts, src/main/runtime/orca-runtime-tests/local-worktree-creation.spec.ts, src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-hidden-replay.test.ts, src/renderer/src/components/terminal-pane/pty-connection/output-restore/pty-connection-dropped-output-snapshot-healing.spec.ts, src/renderer/src/components/terminal-pane/pty-connection/emulation/pty-connection-mode2031-raw-subscription.spec.ts, src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-input-and-repaint.test.ts, src/renderer/src/components/terminal-pane/pty-connection/output/pty-connection-fresh-native-follow-reset.spec.ts. Unresolved local/computed edges remain explicit debt.", "Audit2095 current-parent preparation2126 (approved2092/2093 removals; source refresh2109/independent2112): Intentionally retire these low-value alarms without replacement: src/main/runtime/orca-runtime-tests/terminal-sleep-and-teardown-part-03.spec.ts: rejects exact terminal stop for multiple expected PTYs before stopping anything; omits stale renderer leaves when fresh PTY liveness is required; omits unbound renderer placeholders when fresh PTY liveness is required; captures the event method before the nested sleep receipt changes it; returns the close receipt without requiring a PTY stop; uses the same stop result as an empty explicit stop. src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-04.spec.ts: builds a compact worktree summary from persisted and live runtime state; reads the linked-PR state from the renderer repoId-keyed GitHub cache; carries persisted worktree host ownership in mobile summaries; emits only instance- and boundary-validated lineage parents in mobile summaries; resolves WSL platforms only for repos represented in mobile summaries; omits worktrees hidden by the host visibility policy from mobile summaries; applies linked-checkout source visibility to mobile summaries. src/main/runtime/orca-runtime-tests/hooks-and-hosted-review-part-02.spec.ts: routes runtime GitHub PR details and actions through the selected WSL project runtime; passes SSH connection context through hosted review creation flows; routes local WSL project hosted review flows through runtime git options; treats SSH worktree drift as unknown without local git probes. src/main/runtime/orca-runtime-tests/terminal-listing.spec.ts: emits one mobile session terminal tab per live PTY even if two tabs resolve to it; resolves projected split authority from the parent layout PTY binding; keeps explicit-id terminal lists from resolving all worktrees; keeps explicit-id cold-cache terminal lists from classifying unrelated same-repo worktrees; keeps unknown bare terminal-list ids on the no-scan exact-id path; matches explicit-id cwd PTYs for folder workspace instance IDs; routes PTY output through the PTY leaf index in large terminal graphs. src/main/runtime/orca-runtime-tests/window-authority.spec.ts: claims the first window as authoritative and ignores later windows; transfers authority from the headless sentinel to the first real window; relays terminal browser launches only while headless owns the graph; marks live headless PTYs for renderer reattach before desktop promotion; reports the activation gate state while no desktop window is available; bumps the epoch and enters reloading when the authoritative window reloads; can mark the graph ready for the authoritative window; drops back to unavailable and clears authority when the window disappears; hands the graph back to headless when a promoted serve window closes (#27292); keeps a crashed promoted serve renderer on its own window while the window is open; stays unavailable during initial loads before a graph is published. Current changed source bytes, wrapper registrations and reduced selectors have no fresh execution or red/green credit. Unrelated surviving receipt objects keep their original narrow scope; current literal graph reachability does not establish historical import closure or equivalent replacement coverage. Current conservative command/import graph reaches src/main/runtime/orca-runtime-tests/terminal-sleep-and-teardown-part-03.spec.ts, src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-04.spec.ts, src/main/runtime/orca-runtime-tests/hooks-and-hosted-review-part-02.spec.ts, src/main/runtime/orca-runtime-tests/terminal-listing.spec.ts, src/main/runtime/orca-runtime-tests/window-authority.spec.ts. Unresolved local/computed edges remain explicit debt.", @@ -6817,8 +6818,7 @@ "file": "src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-spawn-and-startup.test.ts", "assertions": [ "fresh local IPC worktree spawns are marked with cwdFallback worktree", - "startup cwd fallback metadata prints a generic in-terminal notice", - "remote-runtime worktree spawns are not marked with cwdFallback" + "startup cwd fallback metadata prints a generic in-terminal notice" ] } ], @@ -6856,6 +6856,7 @@ "Add WSL/mobile/API spawn provider-contract coverage or explicitly narrow their risk scope." ], "knownGaps": [ + "Audit3037 retires the runtime-owner spawn-routing suite and its remote cwdFallback-absence assertion claim. Actual routing-composition alarms are intentionally lost; no replacement or renewed execution credit. Other historical claims and receipts retain their original scope.", "Audit2133 current-parent preparation (988 authored removals; independently reviewed2099/2107/2115/2116 decisions; upstream clear-buffer and recovery-image regressions retained): Intentionally retire these low-value alarms without replacement: src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-spawn-and-startup.test.ts: remove only literal registration import/call pairs import { registerPtyConnectionTerminalBellAttentionTests } from '../notifications/pty-connection-terminal-bell-attention.spec', registerPtyConnectionTerminalBellAttentionTests(); no additional authored credit. src/renderer/src/components/terminal-pane/pty-connection/notifications/pty-connection-terminal-bell-attention.spec.ts: forwards onBell to pane attention and notification policy; hands the bell to the attention module with its pane even when pane attention is off. Current changed source bytes, wrapper registrations and reduced selectors have no fresh execution or red/green credit. Unrelated surviving receipt objects keep their original narrow scope; current literal graph reachability does not establish historical import closure or equivalent replacement coverage. Current conservative command/import graph reaches src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-spawn-and-startup.test.ts, src/renderer/src/components/terminal-pane/pty-connection/notifications/pty-connection-terminal-bell-attention.spec.ts. Unresolved local/computed edges remain explicit debt.", "Audit2065 (sources2063/2064): Intentionally retire these low-value alarms without replacement: src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-spawn-and-startup.test.ts: ordinary registration cleanup removes only import { registerPtyConnectionStartupCwdFallbackNoticeTests } from '../startup/pty-connection-startup-cwd-fallback-notice.spec'; registerPtyConnectionStartupCwdFallbackNoticeTests(). src/renderer/src/components/terminal-pane/pty-connection/startup/pty-connection-startup-cwd-fallback-notice.spec.ts: prints a terminal notice when the startup cwd fell back to the workspace root. Existing unrelated evidenceRuns retain their original scope; unchanged dated receipt objects do not validate changed source bytes or reduced selectors. No fresh gate execution or historical import-closure equivalence is claimed. Frozen current literal-import graph reaches these changed test bytes; unresolved local and computed edges remain debt, and current reachability is not historical execution or equivalent replacement evidence.", "Audit1683 removes low-value test projections, mock wiring, private-state assertions, inventories and duplicate examples without equivalent replacement coverage. Per-source reviews record retained public behavior and refusal boundaries with injected/native/remote limits. Affected historical receipts are retired; gates without active current evidence are downgraded.", @@ -12401,6 +12402,7 @@ "Add Linux and Windows WebGL visibility evidence before claiming cross-platform visual coverage." ], "knownGaps": [ + "Audit3037 removes seven supplied-input baseline cases from the terminal-input-gating registrar. Input-and-repaint wrapper selectors and unrelated assertions remain; historical receipts do not validate the reduced source bytes. No renewed atlas, renderer, native or performance evidence is claimed.", "Audit2133 current-parent preparation (988 authored removals; independently reviewed2099/2107/2115/2116 decisions; upstream clear-buffer and recovery-image regressions retained): Intentionally retire these low-value alarms without replacement: src/renderer/src/components/terminal-pane/render-health/terminal-webgl-atlas-recovery.test.ts: refreshes after each scheduled atlas reset; falls back to a timeout when animation frames are unavailable; continues recovery when a manager throws after scheduling. src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-hidden-replay.test.ts: structural support changed only; no new authored removal credit. src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-input-and-repaint.test.ts: remove only literal registration import/call pairs import { registerPtyConnectionFreshNativeFollowResetTests } from '../output/pty-connection-fresh-native-follow-reset.spec', registerPtyConnectionFreshNativeFollowResetTests(); no additional authored credit. src/renderer/src/components/terminal-pane/pty-connection/output-restore/pty-connection-dropped-output-snapshot-healing.spec.ts: repaints from the main-owned snapshot when main drops pending output at the cap. src/renderer/src/components/terminal-pane/pty-connection/emulation/pty-connection-mode2031-raw-subscription.spec.ts: records a mode 2031 subscribe from the raw chunk boundary without answering. src/renderer/src/components/terminal-pane/pty-connection/output/pty-connection-fresh-native-follow-reset.spec.ts: retries a fresh-spawn native follow reset when renderer dimensions return. Current changed source bytes, wrapper registrations and reduced selectors have no fresh execution or red/green credit. Unrelated surviving receipt objects keep their original narrow scope; current literal graph reachability does not establish historical import closure or equivalent replacement coverage. Current conservative command/import graph reaches src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-hidden-replay.test.ts, src/renderer/src/components/terminal-pane/pty-connection/output-restore/pty-connection-dropped-output-snapshot-healing.spec.ts, src/renderer/src/components/terminal-pane/pty-connection/emulation/pty-connection-mode2031-raw-subscription.spec.ts, src/renderer/src/components/terminal-pane/pty-connection/tests/pty-connection-input-and-repaint.test.ts, src/renderer/src/components/terminal-pane/pty-connection/output/pty-connection-fresh-native-follow-reset.spec.ts, src/renderer/src/components/terminal-pane/render-health/terminal-webgl-atlas-recovery.test.ts. Unresolved local/computed edges remain explicit debt. Retired active assertionRefs (raw original objects retained as historical only): {\n \"file\": \"src/renderer/src/components/terminal-pane/render-health/terminal-webgl-atlas-recovery.test.ts\",\n \"assertions\": [\"limits atlas recovery to explicit renderer lifecycle events\"]\n }", "The Electron harness used a deterministic visibility-event fallback because BrowserWindow.hide did not change document.visibilityState.", "No isolated live paired-server candidate run is recorded; remote-runtime coverage is a provider-agnostic renderer contract plus production incident evidence.", diff --git a/config/scripts/ci/sharding/ci-shard-timings.json b/config/scripts/ci/sharding/ci-shard-timings.json index f913749d8a0..50cf3747f19 100644 --- a/config/scripts/ci/sharding/ci-shard-timings.json +++ b/config/scripts/ci/sharding/ci-shard-timings.json @@ -5199,9 +5199,7 @@ "src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.host-outage.test.tsx": 3552, "src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.notice-card.test.tsx": 2911, "src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.refusal-notices.test.tsx": 3014, - "src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.retained-draft.test.tsx": 3736, "src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test.tsx": 4459, - "src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.wsl-host-stopped.test.tsx": 3657, "src/renderer/src/components/native-chat/tools/tasks/NativeChatStructuredSessionStatus.test.tsx": 583, "src/renderer/src/components/native-chat/tools/lines/NativeChatToolLine.test.tsx": 3544, "src/renderer/src/components/native-chat/tools/lines/NativeChatToolOutput.test.tsx": 410, @@ -5275,7 +5273,6 @@ "src/renderer/src/components/native-chat/pane/routing/native-chat-split-flow.test.ts": 1916, "src/renderer/src/components/native-chat/composer/submit/native-chat-structured-send-composition-clear.test.tsx": 2098, "src/renderer/src/components/native-chat/subagents/native-chat-subagent-handoff-copy-budget.test.ts": 362, - "src/renderer/src/components/native-chat/subagents/native-chat-subagent-section-work-runs.test.ts": 278, "src/renderer/src/components/native-chat/pane/routing/native-chat-tab-directory.test.ts": 304, "src/renderer/src/components/native-chat/native-chat-task-list-state.test.ts": 62, "src/renderer/src/components/native-chat/native-chat-thread-goal-rows.test.ts": 50, diff --git a/config/vitest.performance.config.ts b/config/vitest.performance.config.ts index a29424675e5..f05c41e95f5 100644 --- a/config/vitest.performance.config.ts +++ b/config/vitest.performance.config.ts @@ -22,8 +22,7 @@ const contracts = [ 'config/scripts/static-analysis/oxlint-contracts/app-store-performance-plugin.test.mjs', 'config/scripts/static-analysis/oxlint-contracts/quadratic-buffer-concat-plugin.test.mjs', 'src/main/claude-usage/scanner-incremental-append.test.ts', - 'src/main/claude-usage/store-checkpoint-persistence.test.ts', - 'src/renderer/src/components/native-chat/surfaces/integration/NativeChatResolvedView.streaming-input.test.tsx' + 'src/main/claude-usage/store-checkpoint-persistence.test.ts' ] for (const contract of contracts) { if (!existsSync(resolve(contract))) { diff --git a/docs/styleguide/native-chat.md b/docs/styleguide/native-chat.md index 69600d8d006..4d367ca879e 100644 --- a/docs/styleguide/native-chat.md +++ b/docs/styleguide/native-chat.md @@ -27,4 +27,4 @@ The chat root (`.native-chat-appearance`) redefines these steps as ratios of the - **Focus rings:** full-width transcript rows use the inset ring (`focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring`) so it isn't clipped by the scroll container; standalone controls use `focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring`. Inside the chat `--ring` is `--chat-focus-ring`, which clears 3:1 on the canvas; don't lower its opacity. - **Theme overrides:** plugin themes can set `--chat-bold-foreground`, `--chat-muted-foreground`, `--chat-inline-code`, `--chat-inline-code-foreground`, `--chat-code-block`, `--chat-selection` and `--chat-selection-foreground`. They feed `text-chat-foreground-faint`, `bg-chat-inline-code-surface`, `bg-chat-code-surface` and the chat root's `::selection`, so use those rather than reading the theme tokens directly. - **Selection** inside the chat is the link hue mixed into the canvas (close to the system highlight), with selected text in the full foreground so every role stays 4.5:1. Don't add per-component `selection:` colors in transcript text. -- **State colours:** warnings use `text-chat-warning`; diff lines use `text-chat-diff-added` / `text-chat-diff-removed`. They are mixed toward the foreground so they pass text contrast in light and dark. Don't use raw palette colours (`text-yellow-500`) or the git decoration tokens for text on a tinted row. +- **State colours:** warnings use `text-chat-warning`; diff lines use `text-chat-diff-added` / `text-chat-diff-removed`; what the reader has not seen yet uses `text-chat-unread`. They are mixed toward the foreground so they pass text contrast in light and dark. Don't use raw palette colours (`text-yellow-500`) or the git decoration tokens for text on a tinted row. diff --git a/skill-guides/orchestration/references/messaging-and-gates.md b/skill-guides/orchestration/references/messaging-and-gates.md index d59db8e4bb8..6840971af67 100644 --- a/skill-guides/orchestration/references/messaging-and-gates.md +++ b/skill-guides/orchestration/references/messaging-and-gates.md @@ -5,3 +5,7 @@ Handle a whole `check` batch before the next `check`. Groups (`@all`, `@idle`, `@codex`, ...) reach the live workers of your Run; `@worktree:` also reaches coordinators in that workspace. Use groups only for deliberate fan-out. + +To reach an agent on a paired server, use `send --on `; a reply to mail +from another server needs no flag. See +[Placement and remote execution](placement-and-remote.md). diff --git a/skill-guides/orchestration/references/placement-and-remote.md b/skill-guides/orchestration/references/placement-and-remote.md index 1ea5a060559..f68b146b18a 100644 --- a/skill-guides/orchestration/references/placement-and-remote.md +++ b/skill-guides/orchestration/references/placement-and-remote.md @@ -35,3 +35,24 @@ workspace, and the start output names the workspace it created. After start, address a remote worker only by Dispatch ID, never by a terminal handle. + +## Agents on another server + +To message an agent that already runs on a paired server, send with `--on`, +using the same server names as `worker-start --on`: + +```bash +ORCA orchestration send --on --to --subject "" --body "" +``` + +Your own Orca carries the mail and keeps the conversation. Replies come back to +your own `check`, and they wake you like local mail. The other agent answers +with a plain `reply --id --body `; no flag is needed on either +side. Mail from another server names its sender as ` on `. + +`--environment ` runs one command on that server and delivers nothing back +to you, so an agent never mails with it. Your mailbox stays on your own Orca: +`check` takes no `--environment`. Read-only commands such as `inbox` or +`worker-list` with `--environment` show that server's records. A server whose Orca +is too old answers `server_mail_unsupported` and nothing is sent. Pair servers +with `orca pair` or in Settings. diff --git a/src/cli/bundled-skill-guides.ts b/src/cli/bundled-skill-guides.ts index 37c0fe741c2..6beb516019a 100644 --- a/src/cli/bundled-skill-guides.ts +++ b/src/cli/bundled-skill-guides.ts @@ -4101,6 +4101,10 @@ const ORCHESTRATION_MESSAGING_AND_GATES_REFERENCE_MARKDOWN = [ "`@worktree:` also reaches coordinators in that workspace. Use groups only", "for deliberate fan-out.", "", + "To reach an agent on a paired server, use `send --on `; a reply to mail", + "from another server needs no flag. See", + "[Placement and remote execution](placement-and-remote.md).", + "", ].join('\n') // oxfmt-ignore @@ -4143,6 +4147,27 @@ const ORCHESTRATION_PLACEMENT_AND_REMOTE_REFERENCE_MARKDOWN = [ "After start, address a remote worker only by Dispatch ID, never by a terminal", "handle.", "", + "## Agents on another server", + "", + "To message an agent that already runs on a paired server, send with `--on`,", + "using the same server names as `worker-start --on`:", + "", + "```bash", + "ORCA orchestration send --on --to --subject \"\" --body \"\"", + "```", + "", + "Your own Orca carries the mail and keeps the conversation. Replies come back to", + "your own `check`, and they wake you like local mail. The other agent answers", + "with a plain `reply --id --body `; no flag is needed on either", + "side. Mail from another server names its sender as ` on `.", + "", + "`--environment ` runs one command on that server and delivers nothing back", + "to you, so an agent never mails with it. Your mailbox stays on your own Orca:", + "`check` takes no `--environment`. Read-only commands such as `inbox` or", + "`worker-list` with `--environment` show that server's records. A server whose Orca", + "is too old answers `server_mail_unsupported` and nothing is sent. Pair servers", + "with `orca pair` or in Settings.", + "", ].join('\n') // oxfmt-ignore diff --git a/src/cli/handlers/orchestration/agent-server-route.test.ts b/src/cli/handlers/orchestration/agent-server-route.test.ts new file mode 100644 index 00000000000..b84bae4609c --- /dev/null +++ b/src/cli/handlers/orchestration/agent-server-route.test.ts @@ -0,0 +1,105 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type * as RuntimeClientModule from '../../runtime-client' +import { refuseAgentServerRoute, requirePeerMailCarrier } from './agent-server-route' + +const { callerShow } = vi.hoisted(() => ({ callerShow: vi.fn() })) +vi.mock('../../runtime-client', async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + RuntimeClient: class { + call = callerShow + } + } +}) + +function ctx(command: string[], flags: [string, string | boolean][]) { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a handler context with only the fields this function reads. + return { + flags: new Map(flags), + client: { isRemote: true }, + cwd: '/', + json: false, + rawArgs: [...command, ...flags.flatMap(([name, value]) => [`--${name}`, String(value)])] + } as unknown as Parameters[1] +} + +describe('an agent pointing mail at another server', () => { + beforeEach(() => { + vi.stubEnv('ORCA_AGENT_LAUNCH_TOKEN', 'launch-token') + vi.stubEnv('ORCA_AGENT_SESSION_ID', '') + vi.stubEnv('ORCA_CLI_COMMAND', 'orca') + callerShow.mockResolvedValue({ result: { caller: null, terminal: true } }) + }) + + afterEach(() => { + vi.unstubAllEnvs() + callerShow.mockReset() + }) + + it('is refused with the --on command to run instead', async () => { + await expect( + refuseAgentServerRoute( + 'send', + ctx( + ['orchestration', 'send'], + [ + ['environment', 'devB'], + ['to', 'term_b'], + ['subject', 'hi'] + ] + ) + ) + ).rejects.toMatchObject({ + code: 'agent_server_route_refused', + message: + 'send with --environment devB runs on devB and nothing comes back to you. To message an agent on devB, use: orca orchestration send --on devB --to term_b --subject hi' + }) + }) + + it('refuses a check against another server: the mailbox is local', async () => { + await expect( + refuseAgentServerRoute('check', ctx(['orchestration', 'check'], [['environment', 'devB']])) + ).rejects.toMatchObject({ + code: 'mailbox_is_local', + message: + 'Your mailbox is on this Orca; replies from devB arrive here. Drop --environment devB.' + }) + }) + + it('leaves a person in a shell running the command on that server', async () => { + vi.stubEnv('ORCA_AGENT_LAUNCH_TOKEN', '') + await expect( + refuseAgentServerRoute('send', ctx(['orchestration', 'send'], [['environment', 'devB']])) + ).resolves.toBeUndefined() + expect(callerShow).not.toHaveBeenCalled() + }) + + it('leaves a launch its own Orca does not verify running on that server', async () => { + callerShow.mockResolvedValue({ result: { caller: null, terminal: false } }) + await expect( + refuseAgentServerRoute('send', ctx(['orchestration', 'send'], [['environment', 'devB']])) + ).resolves.toBeUndefined() + }) +}) + +describe('send --on from an older Orca', () => { + it('is refused before anything is sent', async () => { + const call = vi + .fn() + .mockResolvedValue({ result: { capabilities: ['orchestration.contract.v1'] } }) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a handler context with only the fields this function reads. + const context = { + flags: new Map([['on', 'devB']]), + client: { call }, + cwd: '/', + json: false + } as unknown as Parameters[0] + + await expect(requirePeerMailCarrier(context)).rejects.toMatchObject({ + code: 'server_mail_unsupported' + }) + expect(call).toHaveBeenCalledTimes(1) + expect(call).toHaveBeenCalledWith('status.get') + }) +}) diff --git a/src/cli/handlers/orchestration/agent-server-route.ts b/src/cli/handlers/orchestration/agent-server-route.ts new file mode 100644 index 00000000000..3112b6d7d84 --- /dev/null +++ b/src/cli/handlers/orchestration/agent-server-route.ts @@ -0,0 +1,95 @@ +import type { HandlerContext } from '../../dispatch' +import { RuntimeClient, RuntimeClientError } from '../../runtime-client' +import { readInjectedAgentSessionId } from '../../../shared/agent-session/lifecycle/agent-session-caller-env' +import type { OrchestrationCallerShowResult } from '../../../shared/orchestration/orchestration-caller-status' +import { renderOrchestrationArgv } from './agent-view' +import type { RuntimeStatus } from '../../../shared/runtime-types' +import { ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY } from '../../../shared/orchestration/orchestration-runtime-capabilities' + +type AgentServerRoutedCommand = 'send' | 'worker-start' | 'check' + +/** The server a remote client was pointed at, as the caller spelled it. */ +function serverSelector( + flags: HandlerContext['flags'] +): { name: string; route: string } | undefined { + const explicit = flags.get('environment') + if (typeof explicit === 'string') { + return { name: explicit, route: `--environment ${explicit}` } + } + const inherited = process.env.ORCA_ENVIRONMENT?.trim() + return inherited ? { name: inherited, route: `ORCA_ENVIRONMENT=${inherited}` } : undefined +} + +/** Only an agent its own Orca verifies counts; a person's shell keeps --environment = run it there. */ +async function isOrcaVerifiedAgent(): Promise { + if (!readInjectedAgentSessionId() && !process.env.ORCA_AGENT_LAUNCH_TOKEN?.trim()) { + return false + } + try { + const local = new RuntimeClient(undefined, 10_000, null, null) + const { result } = await local.call('orchestration.callerShow') + return result.caller !== null || result.terminal === true + } catch { + return false + } +} + +/** The same command with the server moved from --environment to --on. */ +function withOn(rawArgs: readonly string[] | undefined, server: string): string { + const args = rawArgs ?? [] + const kept: string[] = [] + for (let index = 0; index < args.length; index += 1) { + const arg = args[index]! + const [name] = arg.split('=', 1) + if (name === '--environment') { + if (!arg.includes('=')) { + index += 1 + } + continue + } + kept.push(arg) + } + const at = kept.findIndex((arg) => arg === 'send' || arg === 'worker-start') + kept.splice(at + 1, 0, '--on', server) + return renderOrchestrationArgv(kept) ?? '' +} + +/** An agent's mail and workers live on its own Orca; pointing them at another server reaches nobody. */ +export async function refuseAgentServerRoute( + command: AgentServerRoutedCommand, + ctx: HandlerContext +): Promise { + if (!ctx.client.isRemote || ctx.flags.has('on')) { + return + } + const server = serverSelector(ctx.flags) + if (!server || !(await isOrcaVerifiedAgent())) { + return + } + const { name, route } = server + if (command === 'check') { + throw new RuntimeClientError( + 'mailbox_is_local', + `Your mailbox is on this Orca; replies from ${name} arrive here. Drop ${route}.` + ) + } + const verb = command === 'send' ? 'message an agent' : 'start a worker' + throw new RuntimeClientError( + 'agent_server_route_refused', + `${command} with ${route} runs on ${name} and nothing comes back to you. To ${verb} on ${name}, use: ${withOn(ctx.rawArgs, name)}` + ) +} + +/** An older Orca would drop --on and send the mail to an agent here, so it must carry mail between servers first. */ +export async function requirePeerMailCarrier(ctx: HandlerContext): Promise { + if (!ctx.flags.has('on')) { + return + } + const { result } = await ctx.client.call('status.get') + if (!result.capabilities?.includes(ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY)) { + throw new RuntimeClientError( + 'server_mail_unsupported', + 'The Orca running here is too old to carry mail to another server. Update or restart Orca here, then send again. Nothing was sent.' + ) + } +} diff --git a/src/cli/handlers/orchestration/message-check-handler.ts b/src/cli/handlers/orchestration/message-check-handler.ts index bf707cfb64b..aa3869fa383 100644 --- a/src/cli/handlers/orchestration/message-check-handler.ts +++ b/src/cli/handlers/orchestration/message-check-handler.ts @@ -1,4 +1,5 @@ import type { CommandHandler } from '../../dispatch' +import { refuseAgentServerRoute } from './agent-server-route' import { printResult } from '../../format' import { withReceiptNext } from './receipt-next-text' import { printOrchestrationResult } from './agent-view' @@ -32,7 +33,9 @@ type CheckResult = { } export const ORCHESTRATION_CHECK_HANDLER: Record = { - 'orchestration check': async ({ flags, client, cwd, json }) => { + 'orchestration check': async (ctx) => { + const { flags, client, cwd, json } = ctx + await refuseAgentServerRoute('check', ctx) const wait = flags.has('wait') const peek = flags.has('peek') // Older runtimes ignore peek, making --unread --peek destructive. diff --git a/src/cli/handlers/orchestration/message-inbox-handlers.ts b/src/cli/handlers/orchestration/message-inbox-handlers.ts index 32d35acfb4b..66ba2a23a13 100644 --- a/src/cli/handlers/orchestration/message-inbox-handlers.ts +++ b/src/cli/handlers/orchestration/message-inbox-handlers.ts @@ -11,13 +11,16 @@ import { type OrchestrationMessageSummary as MessageSummary } from '../../../shared/orchestration/mailbox/orchestration-check-output' import { callOrchestrationMutation } from './mutation-request' +import { requirePeerMailCarrier } from './agent-server-route' import { resolveOrchestrationTerminalHandle } from './terminal-identity' import { withReceiptNext } from './receipt-next-text' import { printOrchestrationResult } from './agent-view' import { formatMessageViewText, toMessageView, type MessageView } from './message-agent-views' export const ORCHESTRATION_INBOX_HANDLERS: Record = { - 'orchestration reply': async ({ flags, client, cwd, json }) => { + 'orchestration reply': async (ctx) => { + const { flags, client, cwd, json } = ctx + await requirePeerMailCarrier(ctx) const from = await resolveOrchestrationTerminalHandle(flags, cwd, client, 'from') const result = await callOrchestrationMutation<{ message: { id: string } @@ -27,6 +30,7 @@ export const ORCHESTRATION_INBOX_HANDLERS: Record = { id: getRequiredStringFlag(flags, 'id'), body: getRequiredStringFlag(flags, 'body'), run: getOptionalStringFlag(flags, 'run'), + on: getOptionalStringFlag(flags, 'on'), from }) if (!flags.has('full')) { diff --git a/src/cli/handlers/orchestration/message-send-handler.ts b/src/cli/handlers/orchestration/message-send-handler.ts index 9288dd50d87..3fb76f51562 100644 --- a/src/cli/handlers/orchestration/message-send-handler.ts +++ b/src/cli/handlers/orchestration/message-send-handler.ts @@ -12,6 +12,7 @@ import { requireWorkerDoneSettlement } from '../orchestration-worker-settlement' import { getOptionalStructuredMessagePayload } from './message-payload' import { callOrchestrationMutation } from './mutation-request' import { isDevCliInvocation } from './runtime-compatibility' +import { refuseAgentServerRoute, requirePeerMailCarrier } from './agent-server-route' import { resolveOrchestrationTerminalHandle, throwNoActiveSenderTerminal @@ -70,7 +71,10 @@ function rejectLifecycleGroupRecipient(type: string | undefined, to: string): vo } export const ORCHESTRATION_SEND_HANDLER: Record = { - 'orchestration send': async ({ flags, client, cwd, json }) => { + 'orchestration send': async (ctx) => { + const { flags, client, cwd, json } = ctx + await refuseAgentServerRoute('send', ctx) + await requirePeerMailCarrier(ctx) const to = getOptionalStringFlag(flags, 'to') const type = getOptionalStringFlag(flags, 'type') if (to) { @@ -99,6 +103,7 @@ export const ORCHESTRATION_SEND_HANDLER: Record = { from, to, run: getOptionalStringFlag(flags, 'run'), + on: getOptionalStringFlag(flags, 'on'), subject: getRequiredStringFlag(flags, 'subject'), body: getOptionalStringFlag(flags, 'body'), type, diff --git a/src/cli/handlers/orchestration/worker-launch-handler.ts b/src/cli/handlers/orchestration/worker-launch-handler.ts index e03c4a523fa..d26ab6a669a 100644 --- a/src/cli/handlers/orchestration/worker-launch-handler.ts +++ b/src/cli/handlers/orchestration/worker-launch-handler.ts @@ -19,9 +19,12 @@ import { import { explainOlderRuntimeRunRequired } from './older-runtime-run-binding' import { withResolvedNextCommands } from '../../orchestration-mutation-recovery' import { readTextOrFileFlag } from '../../command-body-input' +import { refuseAgentServerRoute } from './agent-server-route' export const ORCHESTRATION_WORKER_LAUNCH_HANDLER: Record = { - 'orchestration worker-start': async ({ flags, client, cwd, json }) => { + 'orchestration worker-start': async (ctx) => { + const { flags, client, cwd, json } = ctx + await refuseAgentServerRoute('worker-start', ctx) const terminal = getPresentStringFlag(flags, 'terminal') const model = getOptionalStringFlag(flags, 'model') const effort = getOptionalStringFlag(flags, 'effort') diff --git a/src/cli/index-environment-local.test.ts b/src/cli/index-environment-local.test.ts index 5444e653a18..8a3ad0eef90 100644 --- a/src/cli/index-environment-local.test.ts +++ b/src/cli/index-environment-local.test.ts @@ -103,6 +103,29 @@ describe('--environment local and workspace addresses', () => { } }) + it('runs worker-start --on on this Orca despite an inherited ORCA_ENVIRONMENT', async () => { + const args = [ + 'orchestration', + 'worker-start', + '--on', + 'build-box', + '--spec', + 'Go', + '--agent', + 'claude' + ] + vi.stubEnv('ORCA_ENVIRONMENT', 'srv-b') + runtimeClientConstructorMock.mockClear() + listEnvironmentsMock.mockReturnValue(SAVED) + callMock.mockRejectedValue(new RuntimeClientError('runtime_unavailable', 'stop here')) + await run([...args, '--json']) + expect(runtimeClientConstructorMock).toHaveBeenCalled() + expect( + runtimeClientConstructorMock.mock.calls.every(([, environment]) => environment === null) + ).toBe(true) + vi.unstubAllEnvs() + }) + it('refuses current against another environment with an inspection command', async () => { listEnvironmentsMock.mockReturnValue(SAVED) const { exitCode, output } = await run([ diff --git a/src/cli/index.ts b/src/cli/index.ts index 10ce7052964..5e6e016eb52 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -105,7 +105,20 @@ export async function main( refuseConflictingSessionCallerFlags(commandSpec, parsed.flags) await refuseLocalManagementSelector(parsed.commandPath, parsed.flags) const RuntimeClientClass = await loadRuntimeClientClass() - const ignoreRemoteSelection = ignoresRemoteSelection(parsed.commandPath) + // `--on` runs on the caller's own Orca, which reaches that server; an inherited selection never reroutes it. + const onCallersOrca = runsOnCallersOrca(parsed.commandPath, parsed.flags) + if ( + onCallersOrca && + (parsed.flags.has('pairing-code') || + (parsed.flags.has('environment') && + !isLocalEnvironmentSelector(parsed.flags.get('environment')))) + ) { + throw new RuntimeClientError( + 'invalid_argument', + '--on already names the server; drop --environment. Your own Orca reaches it.' + ) + } + const ignoreRemoteSelection = onCallersOrca || ignoresRemoteSelection(parsed.commandPath) const pairingCode = ignoreRemoteSelection ? null : parsed.flags.get('pairing-code') const environmentSelector = ignoreRemoteSelection ? null : parsed.flags.get('environment') // An explicit local route suppresses ambient ORCA_ENVIRONMENT and pairing codes. @@ -192,6 +205,19 @@ export async function main( } } +const ON_CALLERS_ORCA_COMMANDS: ReadonlySet = new Set(['send', 'reply', 'worker-start']) + +function runsOnCallersOrca( + commandPath: readonly string[], + flags: ReadonlyMap +): boolean { + return ( + commandPath[0] === 'orchestration' && + ON_CALLERS_ORCA_COMMANDS.has(commandPath[1] ?? '') && + flags.has('on') + ) +} + async function runClaudeTeams(argv: string[], cwd: string): Promise { try { // Forward Claude Code arguments verbatim so new flags work without Orca changes. diff --git a/src/cli/runtime/client.ts b/src/cli/runtime/client.ts index 1425d23af36..40b8c360b35 100644 --- a/src/cli/runtime/client.ts +++ b/src/cli/runtime/client.ts @@ -38,10 +38,7 @@ import { ORCHESTRATION_CONTRACT_VERSION } from '../../shared/orchestration/orchestration-runtime-capabilities' import { RemoteRuntimeCompatGate } from './remote-runtime-compat-gate' -import { - createAgentLaunchCallerEvidence, - createOrchestrationCompatibilityEnvelope -} from './orchestration-compatibility-envelope' +import { OrchestrationRequestEvidence } from './orchestration-compatibility-envelope' import { getTimeoutMsParam, isWaitingCheck } from './runtime-request-timeout' import { isWorkerStartTimeoutWithinTimerLimit, @@ -56,7 +53,6 @@ import { const LONG_POLL_CLIENT_GRACE_MS = 10_000 // The host records the launched agent in the caller's chat; older hosts ignore the evidence. -const AGENT_LAUNCH_METHODS: ReadonlySet = new Set(['agent.launch', 'agent.launchReplay']) const loadWebSocketTransport = async () => await import('./websocket-transport.js') @@ -68,10 +64,7 @@ export class RuntimeClient { private readonly originalArgs: readonly string[] | undefined private readonly remoteCompat: RemoteRuntimeCompatGate private orchestrationContractCheck: Promise | null = null - private readonly orchestrationCompatibility = createOrchestrationCompatibilityEnvelope( - process.env - ) - private readonly agentLaunchCaller = createAgentLaunchCallerEvidence(process.env) + private readonly requestEvidence = new OrchestrationRequestEvidence(process.env) private readonly runtimeSourceFence: RuntimeSourceFence // Browser session cold starts can take 15–30 seconds; allow time for initialization. @@ -139,15 +132,11 @@ export class RuntimeClient { : error } } - const compatibilityEnvelope = method.startsWith('orchestration.') - ? { - ...this.orchestrationCompatibility, - compatibilityInvocationId: - orchestrationRequestId ?? this.orchestrationCompatibility.compatibilityInvocationId - } - : AGENT_LAUNCH_METHODS.has(method) && !this.remote && this.agentLaunchCaller - ? { orchestrationCompatibilityEvidence: this.agentLaunchCaller } - : {} + const compatibilityEnvelope = this.requestEvidence.envelopeFor(method, { + remote: this.remote !== null, + mutation: orchestrationMutation, + requestId: orchestrationRequestId + }) const envelope = { orchestrationCapability: options?.orchestrationCapability, orchestrationContractVersion: method.startsWith('orchestration.') diff --git a/src/cli/runtime/orchestration-compatibility-envelope.ts b/src/cli/runtime/orchestration-compatibility-envelope.ts index a40fe6868ab..be78e8dee2f 100644 --- a/src/cli/runtime/orchestration-compatibility-envelope.ts +++ b/src/cli/runtime/orchestration-compatibility-envelope.ts @@ -26,3 +26,40 @@ export function createAgentLaunchCallerEvidence( } return { agentSessionId } } + +const AGENT_LAUNCH_METHODS: ReadonlySet = new Set(['agent.launch', 'agent.launchReplay']) + +/** The caller evidence one request carries, read from this process's environment once. */ +export class OrchestrationRequestEvidence { + private readonly orchestration: RuntimeOrchestrationEnvelope + // A chat's session id means nothing on another server, so reads there go like a shell's. + private readonly remoteRead: RuntimeOrchestrationEnvelope + private readonly agentLaunchCaller: RuntimeOrchestrationEnvelope['orchestrationCompatibilityEvidence'] + + constructor(env: NodeJS.ProcessEnv) { + this.orchestration = createOrchestrationCompatibilityEnvelope(env) + const { agentSessionId: _session, ...shell } = + this.orchestration.orchestrationCompatibilityEvidence ?? {} + this.remoteRead = { + ...this.orchestration, + orchestrationCompatibilityEvidence: Object.keys(shell).length > 0 ? shell : undefined + } + this.agentLaunchCaller = createAgentLaunchCallerEvidence(env) + } + + envelopeFor( + method: string, + request: { remote: boolean; mutation: boolean; requestId: string | undefined } + ): RuntimeOrchestrationEnvelope { + if (method.startsWith('orchestration.')) { + const base = request.remote && !request.mutation ? this.remoteRead : this.orchestration + return { + ...base, + compatibilityInvocationId: request.requestId ?? base.compatibilityInvocationId + } + } + return AGENT_LAUNCH_METHODS.has(method) && !request.remote && this.agentLaunchCaller + ? { orchestrationCompatibilityEvidence: this.agentLaunchCaller } + : {} + } +} diff --git a/src/cli/runtime/orchestration-request-evidence.test.ts b/src/cli/runtime/orchestration-request-evidence.test.ts new file mode 100644 index 00000000000..aceef390bc2 --- /dev/null +++ b/src/cli/runtime/orchestration-request-evidence.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from 'vitest' +import { OrchestrationRequestEvidence } from './orchestration-compatibility-envelope' + +const CHAT = { ORCA_AGENT_SESSION_ID: '11111111-1111-4111-8111-111111111111' } + +describe('caller evidence on requests to another server', () => { + it('sends a chat read there like a shell, with no session id', () => { + const evidence = new OrchestrationRequestEvidence(CHAT) + expect( + evidence.envelopeFor('orchestration.inbox', { + remote: true, + mutation: false, + requestId: undefined + }).orchestrationCompatibilityEvidence + ).toBeUndefined() + }) + + it('keeps the session id on writes there, so that server still refuses them', () => { + const evidence = new OrchestrationRequestEvidence(CHAT) + expect( + evidence.envelopeFor('orchestration.taskCreate', { + remote: true, + mutation: true, + requestId: 'request_1' + }).orchestrationCompatibilityEvidence + ).toEqual({ agentSessionId: '11111111-1111-4111-8111-111111111111' }) + }) +}) diff --git a/src/cli/specs/orchestration.ts b/src/cli/specs/orchestration.ts index 5de2e60ef2a..44f8f118418 100644 --- a/src/cli/specs/orchestration.ts +++ b/src/cli/specs/orchestration.ts @@ -49,10 +49,11 @@ export const ORCHESTRATION_COMMAND_SPECS: CommandSpec[] = [ path: ['orchestration', 'send'], summary: 'Send an inter-agent message', usage: - 'orca orchestration send --subject [--to ] [--run ] [--from ] [--body ] [--type ] [--priority ] [--thread-id ] [--payload ] [--task-id ] [--dispatch-id ] [--outcome ] [--files-modified ] [--report-path ] [--phase ] [--retry-request ] [--json]', + 'orca orchestration send --subject [--to ] [--on ] [--run ] [--from ] [--body ] [--type ] [--priority ] [--thread-id ] [--payload ] [--task-id ] [--dispatch-id ] [--outcome ] [--files-modified ] [--report-path ] [--phase ] [--retry-request ] [--json]', allowedFlags: [ ...GLOBAL_FLAGS, 'to', + 'on', 'run', 'from', 'subject', @@ -82,7 +83,8 @@ export const ORCHESTRATION_COMMAND_SPECS: CommandSpec[] = [ 'From a worker, an omitted recipient defaults to its coordinator, also after it finished; mail sent after finishing, worker_done included, leaves the finished task unchanged.', 'Use --to dispatch: for attempt-specific coordinator guidance; Orca durably relays it to a connected worker server.', 'A worker_done with the active task/dispatch IDs completes that task only from the dispatched pane. When stable pane identity is unavailable, the sender handle must exactly match the dispatch assignee; injected preambles include the correct --from value.', - 'Prefer --task-id/--dispatch-id/etc. over raw --payload JSON in worker commands; PowerShell strips JSON quotes easily.' + 'Prefer --task-id/--dispatch-id/etc. over raw --payload JSON in worker commands; PowerShell strips JSON quotes easily.', + '--on mails a terminal or chat agent on a paired server (--to term_… or orca_session_id:…). Your own Orca carries it, and replies come back to your own check. From an agent, --environment runs the command on that server and delivers no mail.' ] }, { @@ -126,8 +128,12 @@ export const ORCHESTRATION_COMMAND_SPECS: CommandSpec[] = [ summary: 'Reply to a message', usage: 'orca orchestration reply --id --body [--run ] [--from ] [--retry-request ] [--json]', - allowedFlags: [...GLOBAL_FLAGS, 'id', 'body', 'run', 'from', 'retry-request'], - identityFlagRoles: { from: 'caller' } + // --on is accepted only to refuse it with the fix; a reply finds its server by itself. + allowedFlags: [...GLOBAL_FLAGS, 'id', 'body', 'run', 'from', 'on', 'retry-request'], + identityFlagRoles: { from: 'caller' }, + notes: [ + 'A reply to mail from an agent on another server goes back to that server by itself; no flag is needed.' + ] }, { path: ['orchestration', 'inbox'], diff --git a/src/main/acp/timeline/acp-subagents.ts b/src/main/acp/timeline/acp-subagents.ts index acc10e1e1be..5c73b363631 100644 --- a/src/main/acp/timeline/acp-subagents.ts +++ b/src/main/acp/timeline/acp-subagents.ts @@ -88,7 +88,7 @@ export class AcpSubagents { this.known.set(update.id, subagent) if (update.restart && subagent.ended !== undefined) { subagent.ended = undefined - subagent.reply = undefined + subagent.replyDigest = undefined } const ending = update.state !== undefined && acpSubagentOutcome(update.state) !== undefined // Late live reports must not resurrect an ended run after the host retires its record. diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index 367e601fd51..aeebd8bad02 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -61,6 +61,7 @@ import { HEADLESS_RUNTIME_WINDOW_ID } from '../../shared/runtime-session-contrac import { getHostDisplayLabelOverrides } from '../../shared/host-setting-overrides' import type { OrchestrationDb } from './orchestration/db/orchestration-db' import { continueRestoredWorkerMailSoon } from './rpc/methods/orchestration/messaging/settled-worker-follow-through' +import { PeerMailRelay } from './orchestration/federation/peer-mail/peer-mail-relay' const OrcaRuntimeSidebarBase: typeof OrcaRuntimeWithAgentTransitions = ORCA_SIDEBAR_INPUTS_IS_MAIN ? OrcaRuntimeWithSidebarTemporal @@ -68,6 +69,20 @@ const OrcaRuntimeSidebarBase: typeof OrcaRuntimeWithAgentTransitions = ORCA_SIDE // oxlint-disable-next-line typescript/no-unsafe-declaration-merging -- SAFETY: The existing Linear installer supplies the canonical prototype surface. class OrcaRuntimeService extends OrcaRuntimeSidebarBase { + // Lazy: the database can open, and arm relays, while the runtime is still constructing. + declare private peerMailRelay: PeerMailRelay | undefined + + override ensureOrchestrationFederationRelay(runId?: string): void { + super.ensureOrchestrationFederationRelay(runId) + this.peerMailRelay ??= new PeerMailRelay(this, () => this.orchestrationEnvironmentTransport) + this.peerMailRelay.ensure() + } + + override stopOrchestrationFederationRelay(): void { + super.stopOrchestrationFederationRelay() + this.peerMailRelay?.stop() + } + /** Sidebar facts are absent unless ORCA_SIDEBAR_INPUTS=main. */ get sidebarFacts() { return sidebarFactsRuntime(this) diff --git a/src/main/runtime/orchestration/db/attach-orchestration-db-methods.ts b/src/main/runtime/orchestration/db/attach-orchestration-db-methods.ts index f15cb9c577a..a8d353e63a1 100644 --- a/src/main/runtime/orchestration/db/attach-orchestration-db-methods.ts +++ b/src/main/runtime/orchestration/db/attach-orchestration-db-methods.ts @@ -18,6 +18,7 @@ import { attachRemoteDispatchAttachmentCreate } from './federation/remote-dispat import { attachRemoteDispatchAttachmentRelease } from './federation/remote-dispatch-attachment-release' import { attachRemoteDispatchAttachmentStop } from './federation/remote-dispatch-attachment-stop' import { attachRemoteQuestionStore } from './federation/remote-question-store' +import { attachPeerMailStore } from './federation/peer-mail-store' import { attachLegacyAskOperation } from './legacy/legacy-ask-operation' import { attachLegacyCompatibilityCandidates } from './legacy/legacy-compatibility-candidates' import { attachLegacyCompatibilityPrincipals } from './legacy/legacy-compatibility-principals' @@ -122,6 +123,7 @@ export function attachOrchestrationDbMethods(ctor: { prototype: object }): void attachFederationRelayAck(ctor) attachFederationRelayImport(ctor) attachRemoteQuestionStore(ctor) + attachPeerMailStore(ctor) attachFederationRelayItem(ctor) attachWorkerTerminalResourceStore(ctor) attachWorkerTerminalTransfer(ctor) diff --git a/src/main/runtime/orchestration/db/federation/peer-mail-store.ts b/src/main/runtime/orchestration/db/federation/peer-mail-store.ts new file mode 100644 index 00000000000..da829b40e5e --- /dev/null +++ b/src/main/runtime/orchestration/db/federation/peer-mail-store.ts @@ -0,0 +1,182 @@ +import type { MessageRow } from '../orchestration-records' +import type { MessageInsert } from '../messages/message-insert' +import { OrchestrationError } from '../../orchestration-error' +import { generateId } from '../generated-id' +import type { OrchestrationDb } from '../orchestration-db' + +export type PeerMailConversationRow = { + conversation_id: string + role: 'home' | 'peer' + environment_id: string | null + peer_fingerprint: string + local_party: string | null + local_pane_key: string | null + created_at: string +} + +export type PeerMailMessageRow = { + message_id: string + conversation_id: string + sender_label: string + remote_party: string | null +} + +/** The conversation this Orca opened for one local agent with one paired server's current pairing; reused for every send. */ +export function openHomePeerMailConversation( + this: OrchestrationDb, + params: { + environmentId: string + peerFingerprint: string + localParty: string + localPaneKey: string | null + } +): PeerMailConversationRow { + this.db + .prepare( + `INSERT INTO peer_mail_conversations ( + conversation_id, role, environment_id, peer_fingerprint, local_party, local_pane_key + ) VALUES (?, 'home', ?, ?, ?, ?) + ON CONFLICT(environment_id, local_party, peer_fingerprint) WHERE role = 'home' + DO UPDATE SET local_pane_key = COALESCE(excluded.local_pane_key, local_pane_key)` + ) + .run( + generateId('peerconv'), + params.environmentId, + params.peerFingerprint, + params.localParty, + params.localPaneKey + ) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly its row shape. + return this.db + .prepare( + `SELECT * FROM peer_mail_conversations + WHERE role = 'home' AND environment_id = ? AND local_party = ? AND peer_fingerprint = ?` + ) + .get(params.environmentId, params.localParty, params.peerFingerprint) as PeerMailConversationRow +} + +export function getPeerMailConversation( + this: OrchestrationDb, + conversationId: string +): PeerMailConversationRow | undefined { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly its row shape. + return this.db + .prepare('SELECT * FROM peer_mail_conversations WHERE conversation_id = ?') + .get(conversationId) as PeerMailConversationRow | undefined +} + +export function listHomePeerMailEnvironments(this: OrchestrationDb): string[] { + const statement = this.db.prepare( + `SELECT DISTINCT environment_id FROM peer_mail_conversations + WHERE role = 'home' AND environment_id IS NOT NULL` + ) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly these columns. + const rows = statement.all() as { environment_id: string }[] + return rows.map((row) => row.environment_id) +} + +export function listPeerConversationIds(this: OrchestrationDb, peerFingerprint: string): string[] { + const statement = this.db.prepare( + `SELECT conversation_id FROM peer_mail_conversations + WHERE role = 'peer' AND peer_fingerprint = ?` + ) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly these columns. + const rows = statement.all(peerFingerprint) as { conversation_id: string }[] + return rows.map((row) => row.conversation_id) +} + +export function getPeerMailMessage( + this: OrchestrationDb, + messageId: string +): PeerMailMessageRow | undefined { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: SELECT over this table returns exactly its row shape. + return this.db.prepare('SELECT * FROM peer_mail_messages WHERE message_id = ?').get(messageId) as + | PeerMailMessageRow + | undefined +} + +export function isPeerMailSenderLabel(this: OrchestrationDb, label: string): boolean { + return ( + this.db + .prepare('SELECT 1 FROM peer_mail_messages WHERE sender_label = ? LIMIT 1') + .get(label) !== undefined + ) +} + +/** Stores mail that crossed from a paired Orca once; a replayed id is a duplicate, even after a reset removed its row. */ +export function insertPeerMailMessage( + this: OrchestrationDb, + params: { + conversationId: string + /** Only a 'peer' conversation is created here, by the paired Orca that opened it. */ + peerFingerprint?: string + remoteParty: string | null + message: MessageInsert & { id: string; from: string } + } +): + | { message: MessageRow; duplicate: false } + | { message: MessageRow | undefined; duplicate: true } { + this.db.exec('SAVEPOINT peer_mail_insert') + try { + if (params.peerFingerprint !== undefined) { + const conversation = this.getPeerMailConversation(params.conversationId) + if ( + conversation && + (conversation.role !== 'peer' || conversation.peer_fingerprint !== params.peerFingerprint) + ) { + throw new OrchestrationError( + 'request_mismatch', + `Conversation ${params.conversationId} belongs to another server. Nothing was delivered.`, + { effectsApplied: false } + ) + } + if (!conversation) { + this.db + .prepare( + `INSERT INTO peer_mail_conversations (conversation_id, role, peer_fingerprint) + VALUES (?, 'peer', ?)` + ) + .run(params.conversationId, params.peerFingerprint) + } + } + if (this.getPeerMailMessage(params.message.id)) { + this.db.exec('RELEASE peer_mail_insert') + return { message: this.getMessageById(params.message.id), duplicate: true } + } + const message = this.insertMessage(params.message) + this.db + .prepare( + `INSERT INTO peer_mail_messages (message_id, conversation_id, sender_label, remote_party) + VALUES (?, ?, ?, ?)` + ) + .run(message.id, params.conversationId, params.message.from, params.remoteParty) + this.db.exec('RELEASE peer_mail_insert') + return { message, duplicate: false } + } catch (error) { + this.db.exec('ROLLBACK TO peer_mail_insert') + this.db.exec('RELEASE peer_mail_insert') + throw error + } +} + +export type PeerMailStoreMethods = { + openHomePeerMailConversation: typeof openHomePeerMailConversation + getPeerMailConversation: typeof getPeerMailConversation + listHomePeerMailEnvironments: typeof listHomePeerMailEnvironments + listPeerConversationIds: typeof listPeerConversationIds + getPeerMailMessage: typeof getPeerMailMessage + isPeerMailSenderLabel: typeof isPeerMailSenderLabel + insertPeerMailMessage: typeof insertPeerMailMessage +} + +export function attachPeerMailStore(ctor: { prototype: object }): void { + Object.assign(ctor.prototype, { + openHomePeerMailConversation, + getPeerMailConversation, + listHomePeerMailEnvironments, + listPeerConversationIds, + getPeerMailMessage, + isPeerMailSenderLabel, + insertPeerMailMessage + }) +} diff --git a/src/main/runtime/orchestration/db/orchestration-db-methods.ts b/src/main/runtime/orchestration/db/orchestration-db-methods.ts index 1de24dd9a41..824b031a2ee 100644 --- a/src/main/runtime/orchestration/db/orchestration-db-methods.ts +++ b/src/main/runtime/orchestration/db/orchestration-db-methods.ts @@ -17,6 +17,7 @@ import type { RemoteDispatchAttachmentCreateMethods } from './federation/remote- import type { RemoteDispatchAttachmentReleaseMethods } from './federation/remote-dispatch-attachment-release' import type { RemoteDispatchAttachmentStopMethods } from './federation/remote-dispatch-attachment-stop' import type { RemoteQuestionStoreMethods } from './federation/remote-question-store' +import type { PeerMailStoreMethods } from './federation/peer-mail-store' import type { LegacyAskOperationMethods } from './legacy/legacy-ask-operation' import type { LegacyCompatibilityCandidatesMethods } from './legacy/legacy-compatibility-candidates' import type { LegacyCompatibilityPrincipalsMethods } from './legacy/legacy-compatibility-principals' @@ -119,6 +120,7 @@ export type OrchestrationDbMethods = AttemptObservationStoreMethods & FederationRelayAckMethods & FederationRelayImportMethods & RemoteQuestionStoreMethods & + PeerMailStoreMethods & FederationRelayItemMethods & StructuredMailSendStoreMethods & WorkerTerminalResourceStoreMethods & diff --git a/src/main/runtime/orchestration/db/reset/orchestration-reset.ts b/src/main/runtime/orchestration/db/reset/orchestration-reset.ts index cd13b00ff8b..ac68ba65767 100644 --- a/src/main/runtime/orchestration/db/reset/orchestration-reset.ts +++ b/src/main/runtime/orchestration/db/reset/orchestration-reset.ts @@ -58,7 +58,7 @@ export function resetAll(this: OrchestrationDb): void { } export function resetTasks(this: OrchestrationDb): ResetRemovedWork | undefined { - // Messages survive; close threads so orphaned questions cannot masquerade as generic replies. + // Messages survive, and so do replies waiting for a paired Orca to pull them; close threads so orphaned questions cannot masquerade as generic replies. const watermark = this.runResetTransaction(` DELETE FROM coordinator_runs; DELETE FROM decision_gates; @@ -70,7 +70,7 @@ export function resetTasks(this: OrchestrationDb): ResetRemovedWork | undefined DELETE FROM legacy_operation_receipts; DELETE FROM legacy_compatibility_principals; DELETE FROM legacy_adoptions; - DELETE FROM federation_relay_items; + DELETE FROM federation_relay_items WHERE kind <> 'peer_mail'; DELETE FROM remote_dispatch_attachments; DELETE FROM federated_dispatches; DELETE FROM worker_terminal_archives; diff --git a/src/main/runtime/orchestration/db/schema/create-graph-tables-sql.ts b/src/main/runtime/orchestration/db/schema/create-graph-tables-sql.ts index 9d0faa09527..4eb92b8eef8 100644 --- a/src/main/runtime/orchestration/db/schema/create-graph-tables-sql.ts +++ b/src/main/runtime/orchestration/db/schema/create-graph-tables-sql.ts @@ -115,6 +115,34 @@ CREATE TABLE IF NOT EXISTS remote_questions ( CREATE INDEX IF NOT EXISTS idx_remote_questions_dispatch_status ON remote_questions(dispatch_id, status); +-- Agent mail between paired Orcas. 'home': this Orca opened it for local_party and pulls replies +-- from environment_id over the pairing credential peer_fingerprint, so a re-pair opens a new one. 'peer': the paired Orca peer_fingerprint opened it here; its replies wait in +-- federation_relay_items under dispatch_id = conversation_id until that Orca pulls them. +CREATE TABLE IF NOT EXISTS peer_mail_conversations ( + conversation_id TEXT PRIMARY KEY, + role TEXT NOT NULL CHECK(role IN ('home', 'peer')), + environment_id TEXT, + peer_fingerprint TEXT NOT NULL, + local_party TEXT, + local_pane_key TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')) +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_peer_mail_home_pairing + ON peer_mail_conversations(environment_id, local_party, peer_fingerprint) WHERE role = 'home'; +CREATE INDEX IF NOT EXISTS idx_peer_mail_peer_fingerprint + ON peer_mail_conversations(peer_fingerprint, role); + +-- A message that crossed between paired Orcas: its reply goes back over its conversation, to remote_party. +CREATE TABLE IF NOT EXISTS peer_mail_messages ( + message_id TEXT PRIMARY KEY, + conversation_id TEXT NOT NULL, + sender_label TEXT NOT NULL, + remote_party TEXT +); + +CREATE INDEX IF NOT EXISTS idx_peer_mail_messages_sender ON peer_mail_messages(sender_label); + -- Deletes from tasks, dispatch_contexts and worker tables are recorded in removed_orchestration_work -- (removed-orchestration-work-store.ts). CREATE TABLE IF NOT EXISTS tasks ( diff --git a/src/main/runtime/orchestration/federation/peer-mail/peer-mail-delivery.ts b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-delivery.ts new file mode 100644 index 00000000000..80cd096e51f --- /dev/null +++ b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-delivery.ts @@ -0,0 +1,122 @@ +import { createHash } from 'node:crypto' +import { ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY } from '../../../../../shared/orchestration/orchestration-runtime-capabilities' +import { z } from 'zod' +import type { OrcaRuntimeService } from '../../../orca-runtime' +import type { PeerMailConversationRow } from '../../db/federation/peer-mail-store' +import { generateId } from '../../db/generated-id' +import { OrchestrationError } from '../../orchestration-error' +import type { OrchestrationWorkerServer } from '../environment-transport' + +export type PeerMailRuntime = Pick< + OrcaRuntimeService, + | 'getOrchestrationDb' + | 'resolveOrchestrationWorkerServer' + | 'callOrchestrationWorkerServer' + | 'notifyMessageArrived' + | 'readMachineName' + | 'getTerminalHandleForPaneKey' +> + +const NOTHING_SENT = { effectsApplied: false } as const +const RuntimeStatusCapabilities = z.object({ capabilities: z.array(z.string()).optional() }) +const DELIVER_TIMEOUT_MS = 15_000 + +/** "term_9f3c on Jinwoo's Mac": who sent it, and the Orca server it came from. */ +export function peerMailSenderLabel(runtime: PeerMailRuntime, sender: string): string { + return `${sender} on ${runtime.readMachineName()}` +} + +/** A retried mutation reuses its message id, so the server stores the mail once. */ +export function peerMailMessageId( + mutation: { callerFingerprint: string; requestId: string } | undefined +): string { + if (!mutation) { + return generateId('msg') + } + const digest = createHash('sha256') + .update(`${mutation.callerFingerprint}\0${mutation.requestId}`) + .digest('hex') + return `msg_${digest.slice(0, 12)}` +} + +/** Refuses before any effect when the server's Orca cannot receive mail or queue its replies. */ +export async function requirePeerMailServer( + runtime: PeerMailRuntime, + selector: string +): Promise { + const server = runtime.resolveOrchestrationWorkerServer(selector) + const status = RuntimeStatusCapabilities.safeParse( + await runtime.callOrchestrationWorkerServer( + server.environmentId, + 'status.get', + undefined, + DELIVER_TIMEOUT_MS, + undefined, + { expectedEnvironmentPairingRevision: server.pairingRevision } + ) + ) + if ( + !status.success || + !status.data.capabilities?.includes(ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY) + ) { + throw new OrchestrationError( + 'server_mail_unsupported', + `Server ${server.name} runs an older Orca that cannot receive agent mail from another server. Update Orca on ${server.name}, then send again. Nothing was sent.`, + { ...NOTHING_SENT, server: server.name } + ) + } + return server +} + +/** The pairing credential the server knows this Orca by; a re-pair starts new conversations under it. */ +export function peerMailPairing(server: OrchestrationWorkerServer): string { + return server.homePeerFingerprint ?? server.peerFingerprint +} + +/** Hands one message to the paired server; it stores it once and delivers it with its own mail code. */ +export async function deliverPeerMail( + runtime: PeerMailRuntime, + server: OrchestrationWorkerServer, + conversation: PeerMailConversationRow, + message: { + id: string + to: string + senderLabel: string + subject: string + body?: string + type?: 'status' | 'handoff' + priority?: 'normal' | 'high' | 'urgent' + threadId?: string + } +): Promise { + await runtime.callOrchestrationWorkerServer( + server.environmentId, + 'orchestration.peerMailDeliver', + { + conversationId: conversation.conversation_id, + messageId: message.id, + to: message.to, + senderLabel: message.senderLabel, + subject: message.subject, + ...(message.body !== undefined ? { body: message.body } : {}), + ...(message.type ? { type: message.type } : {}), + ...(message.priority ? { priority: message.priority } : {}), + ...(message.threadId ? { threadId: message.threadId } : {}) + }, + DELIVER_TIMEOUT_MS, + undefined, + { expectedEnvironmentPairingRevision: server.pairingRevision } + ) +} + +/** Where a pulled reply lands: the opener's current mailbox, following a terminal's remint by pane. */ +export function peerMailLocalRecipient( + runtime: PeerMailRuntime, + conversation: PeerMailConversationRow +): string { + const party = conversation.local_party ?? '' + const reminted = conversation.local_pane_key + ? runtime.getTerminalHandleForPaneKey(conversation.local_pane_key) + : null + return reminted ?? party +} diff --git a/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.ts b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.ts new file mode 100644 index 00000000000..e2ac481adc8 --- /dev/null +++ b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-relay.ts @@ -0,0 +1,183 @@ +import { z } from 'zod' +import type { OrcaRuntimeService } from '../../../orca-runtime' +import { PeerMailRelayPayload } from '../../../../../shared/rpc-contract/orchestration-peer-mail-params' +import { resolveBareOrchestrationRecipient } from '../../../rpc/methods/orchestration/messaging/recipient-routing' +import type { OrchestrationDb } from '../../db/orchestration-db' +import type { MessageRow } from '../../db/orchestration-records' +import { federationRelayRetryDelayMs } from '../relay/federation-relay-scheduler' +import type { OrchestrationEnvironmentTransport } from '../environment-transport' +import { peerMailLocalRecipient } from './peer-mail-delivery' + +// Under the relay edge's idle cutoff, so an idle pull ends by itself instead of being dropped. +const PULL_WAIT_MS = 20_000 +const PULL_TIMEOUT_MS = PULL_WAIT_MS + 15_000 + +const PulledPeerMail = z.object({ + items: z.array( + z.object({ + conversationId: z.string(), + sequence: z.number().int().positive(), + messageId: z.string(), + payload: z.string() + }) + ) +}) + +type Loop = { db: OrchestrationDb; stopped: boolean } + +/** One pull loop per paired server this Orca opened conversations with. The server never dials + * back: each pull waits there for a reply, stores it as local mail, then acknowledges it. */ +export class PeerMailRelay { + private readonly loops = new Map() + + constructor( + private readonly runtime: OrcaRuntimeService, + private readonly transport: () => OrchestrationEnvironmentTransport | null + ) {} + + ensure(): void { + if (!this.transport()) { + return + } + const db = this.runtime.getOrchestrationDb() + for (const environmentId of db.listHomePeerMailEnvironments()) { + const current = this.loops.get(environmentId) + if (current && !current.stopped && current.db === db) { + continue + } + const loop: Loop = { db, stopped: false } + this.loops.set(environmentId, loop) + void this.run(environmentId, loop) + } + } + + stop(): void { + for (const loop of this.loops.values()) { + loop.stopped = true + } + this.loops.clear() + } + + private isCurrent(environmentId: string, loop: Loop): boolean { + return ( + !loop.stopped && + this.loops.get(environmentId) === loop && + this.runtime.getOrchestrationDb() === loop.db + ) + } + + private async run(environmentId: string, loop: Loop): Promise { + let failures = 0 + while (this.isCurrent(environmentId, loop)) { + try { + await this.pullOnce(environmentId, loop) + failures = 0 + } catch { + if (this.isEnvironmentRemoved(environmentId)) { + // A removed server cannot answer; the next ensure re-arms it if it is added back. + break + } + failures += 1 + await new Promise((resolve) => { + setTimeout(resolve, federationRelayRetryDelayMs(failures)).unref?.() + }) + } + } + if (this.loops.get(environmentId) === loop) { + this.loops.delete(environmentId) + } + } + + /** A store read error keeps the loop retrying rather than ending it. */ + private isEnvironmentRemoved(environmentId: string): boolean { + try { + return this.transport()?.hasEnvironment?.(environmentId) === false + } catch { + return false + } + } + + private async pullOnce(environmentId: string, loop: Loop): Promise { + const response = await this.runtime.callOrchestrationWorkerServer( + environmentId, + 'orchestration.peerMailPull', + { waitMs: PULL_WAIT_MS }, + PULL_TIMEOUT_MS + ) + if (!this.isCurrent(environmentId, loop)) { + return + } + const { items } = PulledPeerMail.parse(response) + if (items.length === 0) { + return + } + const through = new Map() + for (const item of items) { + const stored = this.importReply(loop.db, environmentId, item) + // Wake before the ack: a replay after a failed ack finds the row stored and stays quiet. + if (stored) { + this.runtime.notifyMessageArrived(stored.to_handle, stored.type) + } + through.set( + item.conversationId, + Math.max(through.get(item.conversationId) ?? 0, item.sequence) + ) + } + await this.runtime.callOrchestrationWorkerServer( + environmentId, + 'orchestration.peerMailAck', + { + acknowledgments: [...through].map(([conversationId, throughSequence]) => ({ + conversationId, + throughSequence + })) + }, + 15_000 + ) + } + + /** Stores a pulled reply once; a replay after a crash before the ack finds the stored row. */ + private importReply( + db: OrchestrationDb, + environmentId: string, + item: z.infer['items'][number] + ): MessageRow | undefined { + const conversation = db.getPeerMailConversation(item.conversationId) + const payload = PeerMailRelayPayload.safeParse(safeJson(item.payload)) + if ( + conversation?.role !== 'home' || + conversation.environment_id !== environmentId || + !payload.success + ) { + // Nothing here can read it; acknowledging keeps the server's queue moving. + console.warn( + `[orchestration] Dropped unreadable reply ${item.messageId} from ${environmentId}` + ) + return undefined + } + const handle = peerMailLocalRecipient(this.runtime, conversation) + const recipient = resolveBareOrchestrationRecipient({ runtime: this.runtime, db, handle }) + const stored = db.insertPeerMailMessage({ + conversationId: conversation.conversation_id, + remoteParty: payload.data.fromAddress, + message: { + id: item.messageId, + from: payload.data.senderLabel, + to: recipient.ok ? recipient.to : handle, + subject: payload.data.subject, + body: payload.data.body, + threadId: payload.data.threadId ?? undefined, + runId: recipient.ok ? recipient.runId : undefined + } + }) + return stored.duplicate ? undefined : stored.message + } +} + +function safeJson(value: string): unknown { + try { + return JSON.parse(value) + } catch { + return null + } +} diff --git a/src/main/runtime/orchestration/federation/peer-mail/peer-mail-waiters.ts b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-waiters.ts new file mode 100644 index 00000000000..0e2b13c329d --- /dev/null +++ b/src/main/runtime/orchestration/federation/peer-mail/peer-mail-waiters.ts @@ -0,0 +1,32 @@ +import type { OrchestrationDb } from '../../db/orchestration-db' + +const waiters = new WeakMap void>>() + +/** Wakes every pull waiting on this store; each re-reads its own conversations. */ +export function notifyPeerMailQueued(db: OrchestrationDb): void { + for (const wake of waiters.get(db) ?? []) { + wake() + } +} + +/** Resolves when a reply is queued, the wait ends, or the caller goes away. */ +export function waitForPeerMail( + db: OrchestrationDb, + timeoutMs: number, + signal?: AbortSignal +): Promise { + return new Promise((resolve) => { + const set = waiters.get(db) ?? new Set<() => void>() + waiters.set(db, set) + const done = (): void => { + clearTimeout(timer) + set.delete(done) + signal?.removeEventListener('abort', done) + resolve() + } + const timer = setTimeout(done, timeoutMs) + timer.unref?.() + set.add(done) + signal?.addEventListener('abort', done, { once: true }) + }) +} diff --git a/src/main/runtime/orchestration/mailbox/senders/agent-message-sender.ts b/src/main/runtime/orchestration/mailbox/senders/agent-message-sender.ts index 119c42b73cf..117f61bab8a 100644 --- a/src/main/runtime/orchestration/mailbox/senders/agent-message-sender.ts +++ b/src/main/runtime/orchestration/mailbox/senders/agent-message-sender.ts @@ -43,9 +43,11 @@ export function agentMessageSender( ): AgentMessageSender { const party = senderParty(address, db) const snapshot = snapshotSender(senderSnapshot, party, reportedDispatchId) + // Mail from another server is named by its label: "term_9f3c on Jinwoo's Mac". + const peerLabel = !snapshot.name && db?.isPeerMailSenderLabel(address) ? address : null return { party, - name: agentMessageSenderName(snapshot.name), + name: agentMessageSenderName(snapshot.name ?? peerLabel), ...(snapshot.agent ? { agent: snapshot.agent } : {}) } } diff --git a/src/main/runtime/rpc/errors.ts b/src/main/runtime/rpc/errors.ts index 9331daefc63..ec158eadf27 100644 --- a/src/main/runtime/rpc/errors.ts +++ b/src/main/runtime/rpc/errors.ts @@ -15,6 +15,7 @@ import { import type { RpcEnvelopeMeta, RpcFailure, RpcSuccess } from './core' import { CONNECTOR_REPLY_ERROR_CODES } from '../../../shared/rpc-contract/connector-params' import { ORCHESTRATION_SESSION_CALLER_ERROR_CODES } from '../../../shared/orchestration/orchestration-session-caller-codes' +import { ORCHESTRATION_FEDERATION_ERROR_CODES } from '../../../shared/orchestration/orchestration-federation-error-codes' import { computerUseErrorRecoveryData } from '../../../shared/computer-use-error-recovery' import { COMPUTER_ERROR_CODES } from '../../../shared/computer-use-runtime-types' import { TerminalSplitOutcomeError } from '../../../shared/workspace-layout/terminal-split-outcome' @@ -144,15 +145,7 @@ const STRUCTURED_RUNTIME_PASSTHROUGH_CODES: ReadonlySet = new Set([ 'cursor_dispatch_mismatch', 'source_changed', 'transcript_required', - 'server_required', - 'worktree_not_found_on_server', - 'resource_server_mismatch', - 'peer_changed', - 'remote_runtime_unavailable', - 'runtime_timeout', - 'invalid_runtime_response', - 'capability_unsupported', - 'relay_quota_exceeded', + ...ORCHESTRATION_FEDERATION_ERROR_CODES, 'dispatch_capability_invalid', 'agent_unconfigured', 'worker_prompt_too_large', diff --git a/src/main/runtime/rpc/methods/orchestration/caller-show.ts b/src/main/runtime/rpc/methods/orchestration/caller-show.ts index 0d5f0856807..1b34a65bf3c 100644 --- a/src/main/runtime/rpc/methods/orchestration/caller-show.ts +++ b/src/main/runtime/rpc/methods/orchestration/caller-show.ts @@ -69,10 +69,19 @@ export const ORCHESTRATION_CALLER_METHODS = [ permission: 'workspace', params: null, // Dispatch already admitted the environment's session; do not resolve another caller from params. - handler: (_params, { orchestrationCaller }): OrchestrationCallerShowResult => ({ + handler: ( + _params, + { orchestrationCaller, orchestrationCompatibilityEvidence, runtime } + ): OrchestrationCallerShowResult => ({ caller: orchestrationCaller ? { orcaSessionId: formatOrcaSessionAddress(orchestrationCaller.orcaSessionId), live: true } - : null + : null, + // An Orca-launched agent's own pane and launch secret prove it is one; no hook needs to have fired. + terminal: + !orchestrationCaller && + runtime.verifyOrchestrationCompatibilityCaller(orchestrationCompatibilityEvidence, { + currentRuntimeLaunchSufficient: true + }) !== null }) }), defineMethod({ diff --git a/src/main/runtime/rpc/methods/orchestration/federation/federation-methods.ts b/src/main/runtime/rpc/methods/orchestration/federation/federation-methods.ts index 589b69f3934..39c02e08332 100644 --- a/src/main/runtime/rpc/methods/orchestration/federation/federation-methods.ts +++ b/src/main/runtime/rpc/methods/orchestration/federation/federation-methods.ts @@ -1,9 +1,11 @@ import { ORCHESTRATION_FEDERATION_CONTROL_METHODS } from './federation-control' import { ORCHESTRATION_FEDERATION_RELAY_METHODS } from './federation-relay' import { ORCHESTRATION_FEDERATION_ATTACH_METHODS } from './federation' +import { ORCHESTRATION_PEER_MAIL_METHODS } from './peer-mail-methods' export const ORCHESTRATION_FEDERATION_METHODS = [ ...ORCHESTRATION_FEDERATION_ATTACH_METHODS, ...ORCHESTRATION_FEDERATION_RELAY_METHODS, - ...ORCHESTRATION_FEDERATION_CONTROL_METHODS + ...ORCHESTRATION_FEDERATION_CONTROL_METHODS, + ...ORCHESTRATION_PEER_MAIL_METHODS ] diff --git a/src/main/runtime/rpc/methods/orchestration/federation/peer-mail-methods.ts b/src/main/runtime/rpc/methods/orchestration/federation/peer-mail-methods.ts new file mode 100644 index 00000000000..da5ac7c2209 --- /dev/null +++ b/src/main/runtime/rpc/methods/orchestration/federation/peer-mail-methods.ts @@ -0,0 +1,181 @@ +import { defineMethod, type RpcContext } from '../../../core' +import { OrchestrationError } from '../../../../orchestration/orchestration-error' +import { isGeneratedId } from '../../../../orchestration/db/generated-id' +import type { OrchestrationDb } from '../../../../orchestration/db/orchestration-db' +import type { FederationRelayItemRow } from '../../../../orchestration/db/orchestration-records' +import { resolveBareOrchestrationRecipient } from '../messaging/recipient-routing' +import { mayNameSession } from '../messaging/session-recipient' +import { waitForPeerMail } from '../../../../orchestration/federation/peer-mail/peer-mail-waiters' +import { + PeerMailAckParams, + PeerMailDeliverParams, + PeerMailPullParams +} from '../../../../../../shared/rpc-contract/orchestration-peer-mail-params' + +const NOTHING_DELIVERED = { effectsApplied: false } as const +const MAX_PULL_WAIT_MS = 25_000 +const PULL_PAGE_SIZE = 50 + +/** Mail between servers comes only from a paired computer's Orca, never a phone or this Orca's own clients. */ +function requireRuntimePairing( + ctx: Pick +): string { + if ( + ctx.clientKind !== 'runtime' || + ctx.pairedDeviceId === undefined || + !ctx.authenticatedCallerFingerprint + ) { + throw new OrchestrationError( + 'mail_requires_server_pairing', + 'Agent mail between Orca servers comes only from a paired computer. Nothing was delivered.', + NOTHING_DELIVERED + ) + } + return ctx.authenticatedCallerFingerprint +} + +function pendingReplies(db: OrchestrationDb, peerFingerprint: string): FederationRelayItemRow[] { + const items: FederationRelayItemRow[] = [] + for (const conversationId of db.listPeerConversationIds(peerFingerprint)) { + if (items.length >= PULL_PAGE_SIZE) { + break + } + items.push( + ...db.listPendingFederationRelay(conversationId, 'to_home', PULL_PAGE_SIZE - items.length) + ) + } + return items +} + +export const ORCHESTRATION_PEER_MAIL_METHODS = [ + defineMethod({ + name: 'orchestration.peerMailDeliver', + permission: 'workspace', + params: PeerMailDeliverParams, + handler: async (params, ctx) => { + const peerFingerprint = requireRuntimePairing(ctx) + const { runtime } = ctx + const db = runtime.getOrchestrationDb() + if (!isGeneratedId(params.messageId, 'msg')) { + throw new OrchestrationError('invalid_argument', 'Mail between servers needs a message ID.') + } + // Replies wait in the relay keyed by conversation; any other id could name another Orca's dispatch there. + if (!isGeneratedId(params.conversationId, 'peerconv')) { + throw new OrchestrationError( + 'invalid_argument', + 'Mail between servers needs a conversation ID.', + NOTHING_DELIVERED + ) + } + const to = params.to + if (/^(run|dispatch|task):/.test(to) || to.startsWith('@')) { + throw new OrchestrationError( + 'invalid_argument', + `--on sends to one terminal or chat agent on this server; ${to} is a mailbox. Nothing was delivered.`, + NOTHING_DELIVERED + ) + } + // A retry after a lost answer is acknowledged even if the recipient has since gone. + const delivered = db.getPeerMailMessage(params.messageId) + if (delivered) { + const conversation = db.getPeerMailConversation(delivered.conversation_id) + if ( + delivered.conversation_id !== params.conversationId || + conversation?.peer_fingerprint !== peerFingerprint + ) { + throw new OrchestrationError( + 'request_mismatch', + `Message ${params.messageId} already belongs to another conversation. Nothing was delivered.`, + NOTHING_DELIVERED + ) + } + return { message: { id: params.messageId, to } } + } + if (mayNameSession(to)) { + // Recipient routing reads the session record store, which the host opens lazily. + await runtime.ensureStructuredAgentSessionHost().catch(() => undefined) + } + const recipient = resolveBareOrchestrationRecipient({ runtime, db, handle: to }) + if (!recipient.ok) { + throw new OrchestrationError( + recipient.code, + `${recipient.message} Nothing was delivered.`, + NOTHING_DELIVERED + ) + } + const paneKey = runtime.getLiveTerminalPaneKey(recipient.to) + if (paneKey && db.findActiveRemoteAttachmentForPane(paneKey)) { + // Its mailbox is its Dispatch, which only its Run home writes to. + throw new OrchestrationError( + 'invalid_argument', + `${to} is a worker for another Orca's Run; message it through that Run's coordinator. Nothing was delivered.`, + NOTHING_DELIVERED + ) + } + const stored = db.insertPeerMailMessage({ + conversationId: params.conversationId, + peerFingerprint, + remoteParty: null, + message: { + id: params.messageId, + from: params.senderLabel, + to: recipient.to, + subject: params.subject, + body: params.body, + type: params.type, + priority: params.priority, + threadId: params.threadId, + runId: recipient.runId + } + }) + if (!stored.duplicate) { + runtime.notifyMessageArrived(stored.message.to_handle, stored.message.type) + } + return { message: { id: params.messageId, to } } + } + }), + defineMethod({ + name: 'orchestration.peerMailPull', + permission: 'workspace', + params: PeerMailPullParams, + handler: async (params, ctx) => { + const peerFingerprint = requireRuntimePairing(ctx) + const db = ctx.runtime.getOrchestrationDb() + let items = pendingReplies(db, peerFingerprint) + const waitMs = Math.min(Math.max(params.waitMs ?? 0, 0), MAX_PULL_WAIT_MS) + if (items.length === 0 && waitMs > 0) { + await waitForPeerMail(db, waitMs, ctx.signal) + items = pendingReplies(ctx.runtime.getOrchestrationDb(), peerFingerprint) + } + return { + items: items.map((item) => ({ + conversationId: item.dispatch_id, + sequence: item.sequence, + messageId: item.message_id, + payload: item.payload + })) + } + } + }), + defineMethod({ + name: 'orchestration.peerMailAck', + permission: 'workspace', + params: PeerMailAckParams, + handler: (params, ctx) => { + const peerFingerprint = requireRuntimePairing(ctx) + const db = ctx.runtime.getOrchestrationDb() + for (const ack of params.acknowledgments) { + const conversation = db.getPeerMailConversation(ack.conversationId) + if (conversation?.role !== 'peer' || conversation.peer_fingerprint !== peerFingerprint) { + continue + } + db.acknowledgeFederationRelay({ + dispatchId: ack.conversationId, + direction: 'to_home', + throughSequence: ack.throughSequence + }) + } + return { acknowledged: params.acknowledgments.length } + } + }) +] diff --git a/src/main/runtime/rpc/methods/orchestration/federation/peer-mail.test.ts b/src/main/runtime/rpc/methods/orchestration/federation/peer-mail.test.ts new file mode 100644 index 00000000000..c624b2989fa --- /dev/null +++ b/src/main/runtime/rpc/methods/orchestration/federation/peer-mail.test.ts @@ -0,0 +1,439 @@ +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { + ORCHESTRATION_CONTRACT_VERSION, + ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY +} from '../../../../../../shared/orchestration/orchestration-runtime-capabilities' +import type { RuntimeRpcResponse } from '../../../../../../shared/runtime-rpc-envelope' +import { OrcaRuntimeService } from '../../../../orca-runtime' +import { OrchestrationDb } from '../../../../orchestration/db/orchestration-db' +import type { OrchestrationEnvironmentTransport } from '../../../../orchestration/federation/environment-transport' +import type { OrchestrationSessionCaller } from '../../../../orchestration/addressing/orchestration-caller-identity' +import { RpcDispatcher } from '../../../dispatcher' +import type { RpcContext } from '../../../core' +import { ORCHESTRATION_METHODS } from '../orchestration' +import { ORCHESTRATION_REPLY_METHOD } from '../messaging/reply-method' +import { notifyPeerMailQueued } from '../../../../orchestration/federation/peer-mail/peer-mail-waiters' + +// Retries run at once so a recovering pull loop is observable without waiting out its backoff. +vi.mock( + '../../../../orchestration/federation/relay/federation-relay-scheduler', + async (original) => ({ + ...(await original()), + federationRelayRetryDelayMs: () => 0 + }) +) + +const A_PANE = 'tab_a:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa' +const B_PANE = 'tab_b:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb' +const CHAT_ON_A = 'orca_session_id:11111111-1111-4111-8111-111111111111' +const CHAT_ON_B = 'orca_session_id:22222222-2222-4222-8222-222222222222' + +type Side = { db: OrchestrationDb; runtime: OrcaRuntimeService; dispatcher: RpcDispatcher } + +describe('agent mail between paired Orca servers', () => { + let dir: string + let a: Side + let b: Side + let bCapabilities: (capabilities: string[]) => string[] + let bClientKind: 'runtime' | 'mobile' + let aCredential: string + let hasEnvironment: () => boolean + + function openSide(name: string, machine: string, transport?: OrchestrationEnvironmentTransport) { + const db = new OrchestrationDb(join(dir, `${name}.db`)) + const runtime = new OrcaRuntimeService(null, undefined, { + orchestrationEnvironmentTransport: transport + }) + vi.spyOn(runtime, 'readMachineName').mockReturnValue(machine) + vi.spyOn(runtime, 'getTerminalHandleForPaneKey').mockReturnValue(null) + const panes: Record = { term_a: A_PANE, term_b: B_PANE } + vi.spyOn(runtime, 'getLiveTerminalPaneKey').mockImplementation( + (handle) => panes[handle] ?? null + ) + vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) => panes[handle] ?? null) + runtime.setOrchestrationDb(db) + return { + db, + runtime, + dispatcher: new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS }) + } + } + + const transport: OrchestrationEnvironmentTransport = { + resolve: () => ({ + environmentId: 'env_b', + name: 'devB', + peerFingerprint: 'fingerprint_b', + homePeerFingerprint: aCredential + }), + hasEnvironment: () => hasEnvironment(), + call: async (_selector, method, params) => { + if (method === 'status.get') { + const status = b.runtime.getStatus() + return { + id: 'status', + ok: true, + result: { ...status, capabilities: bCapabilities(status.capabilities ?? []) }, + _meta: { runtimeId: b.runtime.getRuntimeId() } + } + } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads. + return (await b.dispatcher.dispatch( + { + id: `from_a_${method}`, + authToken: 'a-device-token', + method, + params, + orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION + }, + { + authenticatedCallerFingerprint: aCredential, + pairedDeviceId: 'device_a', + clientKind: bClientKind + } + )) as RuntimeRpcResponse + } + } + + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'orca-peer-mail-')) + bCapabilities = (capabilities) => capabilities + bClientKind = 'runtime' + aCredential = 'fingerprint_a' + hasEnvironment = () => true + b = openSide('b', 'Build Box') + a = openSide('a', "Jinwoo's Mac", transport) + }) + + afterEach(() => { + a.runtime.stopOrchestrationFederationRelay() + b.runtime.stopOrchestrationFederationRelay() + a.db.close() + b.db.close() + rmSync(dir, { recursive: true, force: true }) + }) + + let requestCount = 0 + function mutation(side: Side, method: string, params: Record) { + requestCount += 1 + return side.dispatcher.dispatch({ + id: `${method}_${requestCount}`, + authToken: 'local', + orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION, + orchestrationRequestId: `request_${requestCount}`, + method, + params + }) + } + + function chatReply(side: Side, caller: string, params: { id: string; body: string }) { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads. + const orchestrationCaller = { + address: caller, + terminalHandle: null, + orcaSessionId: caller.slice('orca_session_id:'.length), + paneKey: null + } as unknown as OrchestrationSessionCaller + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads. + return ORCHESTRATION_REPLY_METHOD.handler(params, { + runtime: side.runtime, + orchestrationCaller + } as RpcContext) + } + + function mailFor(side: Side, handle: string) { + return side.db.getUnreadMessages(handle) + } + + it('carries a terminal agent mail to the paired server and its flagless reply back', async () => { + const sent = await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Check the build', + body: 'Is main green?' + }) + expect(sent).toMatchObject({ ok: true, result: { server: 'devB', message: { to: 'term_b' } } }) + + const [onB] = mailFor(b, 'term_b') + expect(onB).toMatchObject({ from_handle: "term_a on Jinwoo's Mac", subject: 'Check the build' }) + + const replied = await mutation(b, 'orchestration.reply', { + id: onB!.id, + body: 'Green.', + from: 'term_b' + }) + expect(replied).toMatchObject({ + ok: true, + result: { message: { subject: 'Re: Check the build' } } + }) + + a.runtime.ensureOrchestrationFederationRelay() + await vi.waitFor(() => expect(mailFor(a, 'term_a')).toHaveLength(1)) + const [back] = mailFor(a, 'term_a') + expect(back).toMatchObject({ + from_handle: 'term_b on Build Box', + subject: 'Re: Check the build', + body: 'Green.', + thread_id: onB!.id + }) + + await expect( + mutation(a, 'orchestration.reply', { id: back!.id, body: 'x', from: 'term_a', on: 'devB' }) + ).resolves.toMatchObject({ ok: false, error: { code: 'request_mismatch' } }) + + // The opener can keep answering, and the reply reaches the same agent again. + await expect( + mutation(a, 'orchestration.reply', { id: back!.id, body: 'Thanks.', from: 'term_a' }) + ).resolves.toMatchObject({ ok: true }) + expect(mailFor(b, 'term_b').map((message) => message.body)).toEqual(['Thanks.']) + }) + + it('returns a chat agent reply to the chat on the opening server', async () => { + const send = ORCHESTRATION_METHODS.find((method) => method.name === 'orchestration.send')! + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads. + await send.handler({ from: CHAT_ON_A, on: 'devB', to: 'term_b', subject: 'Plan?' }, { + runtime: a.runtime, + orchestrationCaller: { + address: CHAT_ON_A, + terminalHandle: null, + orcaSessionId: CHAT_ON_A.slice(16), + paneKey: null + } + } as unknown as RpcContext) + const [onB] = mailFor(b, 'term_b') + expect(onB!.from_handle).toBe(`${CHAT_ON_A} on Jinwoo's Mac`) + + await chatReply(b, CHAT_ON_B, { id: onB!.id, body: 'Plan attached.' }) + + a.runtime.ensureOrchestrationFederationRelay() + await vi.waitFor(() => expect(mailFor(a, CHAT_ON_A)).toHaveLength(1)) + expect(mailFor(a, CHAT_ON_A)[0]).toMatchObject({ + from_handle: `${CHAT_ON_B} on Build Box`, + body: 'Plan attached.' + }) + expect(a.db.getPeerMailMessage(mailFor(a, CHAT_ON_A)[0]!.id)?.remote_party).toBe(CHAT_ON_B) + }) + + it('delivers a reply queued before both servers restart exactly once', async () => { + await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Long job' + }) + const [onB] = mailFor(b, 'term_b') + await mutation(b, 'orchestration.reply', { id: onB!.id, body: 'Done.', from: 'term_b' }) + + a.runtime.stopOrchestrationFederationRelay() + a.db.close() + b.db.close() + b = openSide('b', 'Build Box') + a = openSide('a', "Jinwoo's Mac", transport) + + a.runtime.ensureOrchestrationFederationRelay() + await vi.waitFor(() => expect(mailFor(a, 'term_a')).toHaveLength(1)) + const [conversationId] = b.db.listPeerConversationIds('fingerprint_a') + expect(b.db.listPendingFederationRelay(conversationId!, 'to_home')).toEqual([]) + }) + + it('refuses an older server before sending anything', async () => { + bCapabilities = (capabilities) => + capabilities.filter((capability) => capability !== ORCHESTRATION_PEER_MAIL_RUNTIME_CAPABILITY) + + const sent = await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Hello' + }) + + expect(sent).toMatchObject({ + ok: false, + error: { code: 'server_mail_unsupported', data: { effectsApplied: false } } + }) + expect(b.db.getInbox(10)).toEqual([]) + }) + + it('takes mail between servers only from a paired computer, not a phone', async () => { + bClientKind = 'mobile' + + const sent = await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Hello' + }) + + expect(sent).toMatchObject({ + ok: false, + error: { code: 'mail_requires_server_pairing' } + }) + expect(b.db.getInbox(10)).toEqual([]) + }) + + it('acknowledges a retried delivery after its recipient is gone, storing it once', async () => { + const deliver = { + conversationId: 'peerconv_aaaaaaaaaaaa', + messageId: 'msg_aaaaaaaaaaaa', + to: 'term_b', + senderLabel: "term_a on Jinwoo's Mac", + subject: 'Once' + } + const first = await transport.call('devB', 'orchestration.peerMailDeliver', deliver) + vi.mocked(b.runtime.getLiveTerminalPaneKey).mockReturnValue(null) + + const retried = await transport.call('devB', 'orchestration.peerMailDeliver', deliver) + + expect(first).toMatchObject({ ok: true }) + expect(retried).toMatchObject({ ok: true, result: { message: { id: 'msg_aaaaaaaaaaaa' } } }) + expect(b.db.getInbox(10).filter((message) => message.subject === 'Once')).toHaveLength(1) + }) + + it('refuses payload fields before sending anything', async () => { + const sent = await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Report', + payload: '{"taskId":"task_1"}' + }) + + expect(sent).toMatchObject({ ok: false, error: { code: 'invalid_argument' } }) + expect(b.db.getInbox(10)).toEqual([]) + }) + + it('refuses mail to a worker another Orca coordinates', async () => { + vi.spyOn(b.db, 'findActiveRemoteAttachmentForPane').mockImplementation((paneKey) => + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a test double carrying only the fields the code under test reads. + paneKey === B_PANE ? ({ dispatch_id: 'ctx_remote' } as never) : undefined + ) + + const sent = await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Side mail' + }) + + expect(sent).toMatchObject({ ok: false, error: { code: 'invalid_argument' } }) + expect(b.db.getInbox(10)).toEqual([]) + }) + + it('refuses a conversation ID that is not a peer conversation', async () => { + const delivered = await transport.call('devB', 'orchestration.peerMailDeliver', { + conversationId: 'ctx_aaaaaaaaaaaa', + messageId: 'msg_aaaaaaaaaaaa', + to: 'term_b', + senderLabel: "term_a on Jinwoo's Mac", + subject: 'Into a dispatch' + }) + + expect(delivered).toMatchObject({ ok: false, error: { code: 'invalid_argument' } }) + expect(b.db.getInbox(10)).toEqual([]) + }) + + it('keeps conversing with a server after it is re-paired', async () => { + await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'First' + }) + const [first] = mailFor(b, 'term_b') + await mutation(b, 'orchestration.reply', { id: first!.id, body: 'One.', from: 'term_b' }) + a.runtime.ensureOrchestrationFederationRelay() + await vi.waitFor(() => expect(mailFor(a, 'term_a')).toHaveLength(1)) + const [answer] = mailFor(a, 'term_a') + b.db.markAsRead([first!.id]) + + aCredential = 'fingerprint_a_repaired' + await expect( + mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Second' + }) + ).resolves.toMatchObject({ ok: true }) + await expect( + mutation(a, 'orchestration.reply', { id: answer!.id, body: 'Thanks.', from: 'term_a' }) + ).resolves.toMatchObject({ ok: true }) + + expect(mailFor(b, 'term_b').map((message) => message.subject)).toEqual(['Second', 'Re: First']) + }) + + it('acknowledges a reply replayed after this Orca reset its mail', async () => { + await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Ask' + }) + const [onB] = mailFor(b, 'term_b') + await mutation(b, 'orchestration.reply', { id: onB!.id, body: 'Answer.', from: 'term_b' }) + a.runtime.ensureOrchestrationFederationRelay() + await vi.waitFor(() => expect(mailFor(a, 'term_a')).toHaveLength(1)) + const [conversationId] = b.db.listPeerConversationIds('fingerprint_a') + await vi.waitFor(() => + expect(b.db.listPendingFederationRelay(conversationId!, 'to_home')).toEqual([]) + ) + + a.db.resetMessages() + // The ack was lost: the server serves the stored reply again. + b.db['db'].prepare('UPDATE federation_relay_items SET acked_at = NULL').run() + notifyPeerMailQueued(b.db) + + await vi.waitFor(() => + expect(b.db.listPendingFederationRelay(conversationId!, 'to_home')).toEqual([]) + ) + expect(mailFor(a, 'term_a')).toEqual([]) + }) + + it('keeps a queued reply when the server resets its tasks', async () => { + await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Ask' + }) + a.runtime.stopOrchestrationFederationRelay() + const [onB] = mailFor(b, 'term_b') + await mutation(b, 'orchestration.reply', { id: onB!.id, body: 'Kept.', from: 'term_b' }) + + b.db.resetTasks() + + a.runtime.ensureOrchestrationFederationRelay() + await vi.waitFor(() => + expect(mailFor(a, 'term_a').map((message) => message.body)).toEqual(['Kept.']) + ) + }) + + it('keeps pulling after a failed pull when the server list cannot be read', async () => { + await mutation(a, 'orchestration.send', { + from: 'term_a', + on: 'devB', + to: 'term_b', + subject: 'Ask' + }) + a.runtime.stopOrchestrationFederationRelay() + const [onB] = mailFor(b, 'term_b') + await mutation(b, 'orchestration.reply', { id: onB!.id, body: 'Later.', from: 'term_b' }) + const call = vi.spyOn(transport, 'call') + call.mockRejectedValueOnce(new Error('offline')) + hasEnvironment = () => { + hasEnvironment = () => true + throw new Error('registry unreadable') + } + + a.runtime.ensureOrchestrationFederationRelay() + + await vi.waitFor(() => + expect(mailFor(a, 'term_a').map((message) => message.body)).toEqual(['Later.']) + ) + call.mockRestore() + }) +}) diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/peer-mail-reply.ts b/src/main/runtime/rpc/methods/orchestration/messaging/peer-mail-reply.ts new file mode 100644 index 00000000000..e78a9abda6a --- /dev/null +++ b/src/main/runtime/rpc/methods/orchestration/messaging/peer-mail-reply.ts @@ -0,0 +1,106 @@ +import type { OrcaRuntimeService } from '../../../../orca-runtime' +import type { OrchestrationSessionCaller } from '../../../../orchestration/addressing/orchestration-caller-identity' +import type { PeerMailMessageRow } from '../../../../orchestration/db/federation/peer-mail-store' +import type { MessageRow } from '../../../../orchestration/db/orchestration-records' +import { OrchestrationError } from '../../../../orchestration/orchestration-error' +import { + deliverPeerMail, + peerMailMessageId, + peerMailPairing, + peerMailSenderLabel, + requirePeerMailServer +} from '../../../../orchestration/federation/peer-mail/peer-mail-delivery' +import { notifyPeerMailQueued } from '../../../../orchestration/federation/peer-mail/peer-mail-waiters' +import { recordReceiptBeforeNudge } from './mutation-replay-nudge' + +const NOTHING_SENT = { effectsApplied: false } as const + +/** A reply to mail that came from another Orca goes back over that mail's conversation. */ +export async function replyOverPeerMail(args: { + runtime: OrcaRuntimeService + original: MessageRow + route: PeerMailMessageRow + params: { body: string; from?: string; on?: string } + orchestrationCaller: OrchestrationSessionCaller | undefined + orchestrationMutation: { callerFingerprint: string; requestId: string } | undefined + recordMutationReceipt: ((receipt: unknown) => void) | undefined +}): Promise { + const { runtime, original, route, params } = args + const db = runtime.getOrchestrationDb() + const conversation = db.getPeerMailConversation(route.conversation_id) + if (!conversation) { + throw new OrchestrationError( + 'request_mismatch', + `The conversation ${original.id} came on is gone from this Orca. No reply was sent.`, + NOTHING_SENT + ) + } + const replier = args.orchestrationCaller?.address ?? params.from ?? original.to_handle + const id = peerMailMessageId(args.orchestrationMutation) + const subject = original.subject.startsWith('Re: ') ? original.subject : `Re: ${original.subject}` + const threadId = original.thread_id ?? original.id + const senderLabel = peerMailSenderLabel(runtime, replier) + + if (params.on !== undefined) { + throw new OrchestrationError( + 'request_mismatch', + `${original.id} came from ${original.from_handle}; its reply goes back there by itself. Drop --on. No reply was sent.`, + NOTHING_SENT + ) + } + + if (conversation.role === 'peer') { + db.enqueueFederationRelay({ + dispatchId: conversation.conversation_id, + direction: 'to_home', + kind: 'peer_mail', + messageId: id, + payload: JSON.stringify({ + senderLabel, + fromAddress: replier, + subject, + body: params.body, + threadId + }) + }) + db.markAsRead([original.id]) + return recordReceiptBeforeNudge( + args.recordMutationReceipt, + { message: { id, subject }, to: original.from_handle }, + () => notifyPeerMailQueued(db), + { kind: 'messages', targets: [] } + ) + } + + const environmentId = conversation.environment_id ?? '' + if (!route.remote_party) { + throw new OrchestrationError( + 'request_mismatch', + `${original.id} does not say who sent it. No reply was sent.`, + NOTHING_SENT + ) + } + const server = await requirePeerMailServer(runtime, environmentId) + // After a re-pair the server no longer knows the old conversation; continue it under the new pairing. + const current = db.openHomePeerMailConversation({ + environmentId, + peerFingerprint: peerMailPairing(server), + localParty: conversation.local_party ?? '', + localPaneKey: conversation.local_pane_key + }) + await deliverPeerMail(runtime, server, current, { + id, + to: route.remote_party, + senderLabel, + subject, + body: params.body, + threadId + }) + db.markAsRead([original.id]) + return recordReceiptBeforeNudge( + args.recordMutationReceipt, + { message: { id, subject }, to: original.from_handle, server: server.name }, + () => runtime.ensureOrchestrationFederationRelay(), + { kind: 'federation' } + ) +} diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/peer-mail-send.ts b/src/main/runtime/rpc/methods/orchestration/messaging/peer-mail-send.ts new file mode 100644 index 00000000000..9001cb57a7d --- /dev/null +++ b/src/main/runtime/rpc/methods/orchestration/messaging/peer-mail-send.ts @@ -0,0 +1,133 @@ +import type { z } from 'zod' +import type { OrcaRuntimeService } from '../../../../orca-runtime' +import type { OrchestrationSessionCaller } from '../../../../orchestration/addressing/orchestration-caller-identity' +import type { OrchestrationCompatibilityCallerAuthority } from '../../../../orchestration/terminal-authority/orchestration-compatibility-authority' +import { OrchestrationError } from '../../../../orchestration/orchestration-error' +import { + deliverPeerMail, + peerMailMessageId, + peerMailPairing, + peerMailSenderLabel, + requirePeerMailServer +} from '../../../../orchestration/federation/peer-mail/peer-mail-delivery' +import type { RpcContext } from '../../../core' +import { recordReceiptBeforeNudge } from './mutation-replay-nudge' +import type { SendParams } from './send-params' +import type { ORCHESTRATION_SEND_METHODS } from './send-methods' + +const NOTHING_SENT = { effectsApplied: false } as const + +/** The agent on this Orca that mail sent elsewhere answers to: a chat by its session, a terminal by its live pane. */ +export function peerMailLocalCaller(args: { + runtime: OrcaRuntimeService + from: string | undefined + orchestrationCaller: OrchestrationSessionCaller | undefined + attestedCaller: OrchestrationCompatibilityCallerAuthority | undefined +}): { address: string; paneKey: string | null } { + if (args.orchestrationCaller) { + return { address: args.orchestrationCaller.address, paneKey: null } + } + const paneKey = + args.from === undefined + ? null + : (args.attestedCaller?.paneKey ?? args.runtime.getLiveTerminalPaneKey(args.from)) + if (!args.from || !paneKey) { + throw new OrchestrationError( + 'invalid_argument', + '--on needs a terminal or chat agent on this Orca to receive the replies. Run it from that agent. Nothing was sent.', + NOTHING_SENT + ) + } + if (args.runtime.getOrchestrationDb().findActiveRemoteAttachmentForPane(paneKey)) { + throw new OrchestrationError( + 'invalid_argument', + "This terminal is a worker for another Orca's Run; its mail goes to that Run's coordinator, not to other servers. Nothing was sent.", + NOTHING_SENT + ) + } + return { address: args.from, paneKey } +} + +/** `send --on `: this Orca carries the mail and keeps the conversation the replies come back on. */ +export async function sendPeerMail(args: { + params: z.infer & { on: string } + runtime: OrcaRuntimeService + orchestrationCaller: OrchestrationSessionCaller | undefined + attestedCaller: OrchestrationCompatibilityCallerAuthority | undefined + orchestrationMutation: { callerFingerprint: string; requestId: string } | undefined + recordMutationReceipt: ((receipt: unknown) => void) | undefined +}): Promise { + const { params, runtime } = args + const to = params.to + if (!to || /^(run|dispatch|task):/.test(to) || to.startsWith('@')) { + throw new OrchestrationError( + 'invalid_argument', + `--on sends to one terminal or chat agent on that server: --to term_… or --to orca_session_id:…${to ? `, not ${to}` : ''}. Nothing was sent.`, + NOTHING_SENT + ) + } + const type = params.type ?? 'status' + if (type !== 'status' && type !== 'handoff') { + throw new OrchestrationError( + 'invalid_argument', + `--on carries --type status or handoff, not ${type}. Nothing was sent.`, + NOTHING_SENT + ) + } + if (params.payload !== undefined) { + throw new OrchestrationError( + 'invalid_argument', + '--on carries a subject and body; payload fields such as --task-id or --report-path name work on this Orca only. Nothing was sent.', + NOTHING_SENT + ) + } + const caller = peerMailLocalCaller({ ...args, from: params.from }) + const server = await requirePeerMailServer(runtime, params.on) + const conversation = runtime.getOrchestrationDb().openHomePeerMailConversation({ + environmentId: server.environmentId, + peerFingerprint: peerMailPairing(server), + localParty: caller.address, + localPaneKey: caller.paneKey + }) + const id = peerMailMessageId(args.orchestrationMutation) + await deliverPeerMail(runtime, server, conversation, { + id, + to, + senderLabel: peerMailSenderLabel(runtime, caller.address), + subject: params.subject, + body: params.body, + type, + priority: params.priority, + threadId: params.threadId + }) + return recordReceiptBeforeNudge( + args.recordMutationReceipt, + { message: { id, to }, server: server.name }, + () => runtime.ensureOrchestrationFederationRelay(), + { kind: 'federation' } + ) +} + +/** `orchestration.send` with `on` set goes to the paired server; every other send runs unchanged. */ +export function withPeerMailSend( + methods: typeof ORCHESTRATION_SEND_METHODS +): typeof ORCHESTRATION_SEND_METHODS { + return methods.map((method) => ({ + ...method, + handler: (params: Parameters[0], ctx: RpcContext) => { + const on = params.on + if (on === undefined || ctx.replayedMutationReceipt !== undefined) { + return method.handler(params, ctx) + } + const attested = ctx.orchestrationCompatibilityCallerAuthority + return sendPeerMail({ + params: { ...params, on }, + runtime: ctx.runtime, + orchestrationCaller: ctx.orchestrationCaller, + attestedCaller: attested?.terminalHandle === params.from ? attested : undefined, + orchestrationMutation: ctx.orchestrationMutation, + recordMutationReceipt: ctx.recordMutationReceipt + }) + } + })) +} diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/reply-method.ts b/src/main/runtime/rpc/methods/orchestration/messaging/reply-method.ts index b0bd4ec708f..f0ea51d87e7 100644 --- a/src/main/runtime/rpc/methods/orchestration/messaging/reply-method.ts +++ b/src/main/runtime/rpc/methods/orchestration/messaging/reply-method.ts @@ -14,6 +14,7 @@ import { recordReceiptBeforeNudge, replayMutationNudge } from './mutation-replay import { resolveReplyRecipient } from './recipient-routing' import { helperQuestionAnswerer } from './helper-question-answerer' import { replyToRemoteRunHome } from './reply-remote' +import { replyOverPeerMail } from './peer-mail-reply' import { withAnswerNext } from '../receipt-next' import { ReplyParams } from '../../../../../../shared/rpc-contract/orchestration-params' import { @@ -35,6 +36,7 @@ export const ORCHESTRATION_REPLY_METHOD = defineMethod({ legacyCoordinatorRunId, recordMutationReceipt, replayedMutationReceipt, + orchestrationMutation, signal } ) => { @@ -157,6 +159,25 @@ export const ORCHESTRATION_REPLY_METHOD = defineMethod({ ) } + const peerRoute = db.getPeerMailMessage(original.id) + if (peerRoute) { + return await replyOverPeerMail({ + runtime, + original, + route: peerRoute, + params, + orchestrationCaller, + orchestrationMutation, + recordMutationReceipt + }) + } + if (params.on !== undefined) { + throw new OrchestrationError( + 'invalid_argument', + `${params.id} did not come from another server, so its reply stays on this Orca. Drop --on. No reply was sent.`, + { effectsApplied: false } + ) + } if (db.getRun(original.run_id)?.home_database === 'remote') { return await replyToRemoteRunHome({ runtime, db, original, params, signal }) } diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/send-methods.ts b/src/main/runtime/rpc/methods/orchestration/messaging/send-methods.ts index c9ba10ffe59..4c40eac6996 100644 --- a/src/main/runtime/rpc/methods/orchestration/messaging/send-methods.ts +++ b/src/main/runtime/rpc/methods/orchestration/messaging/send-methods.ts @@ -38,6 +38,7 @@ import { settledDispatchId } from './settled-worker-follow-through' +// `send --on ` is carried by withPeerMailSend (peer-mail-send.ts) before this handler runs. export const ORCHESTRATION_SEND_METHODS = [ defineMethod({ name: 'orchestration.send', diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/send-params.ts b/src/main/runtime/rpc/methods/orchestration/messaging/send-params.ts index 27bed5c0e56..cce983c33e3 100644 --- a/src/main/runtime/rpc/methods/orchestration/messaging/send-params.ts +++ b/src/main/runtime/rpc/methods/orchestration/messaging/send-params.ts @@ -33,6 +33,8 @@ export const SendParams = z // Use remint-stable pane keys to attest worker_done/heartbeat; from handles only route. senderPaneKey: OptionalString, run: OptionalString, + // A paired server: this Orca carries the mail there and the replies back (`send --on`). + on: OptionalString, waitForLifecycleSettlement: OptionalBoolean, devMode: OptionalBoolean }) diff --git a/src/main/runtime/rpc/methods/orchestration/orchestration.ts b/src/main/runtime/rpc/methods/orchestration/orchestration.ts index 57ab816b30d..a9d551e8555 100644 --- a/src/main/runtime/rpc/methods/orchestration/orchestration.ts +++ b/src/main/runtime/rpc/methods/orchestration/orchestration.ts @@ -5,6 +5,7 @@ import { ORCHESTRATION_WORKER_METHODS } from './worker/worker-methods' import { ORCHESTRATION_FEDERATION_METHODS } from './federation/federation-methods' import { ORCHESTRATION_MUTATION_REQUEST_METHODS } from './runs/mutation-request-show' import { ORCHESTRATION_SEND_METHODS } from './messaging/send-methods' +import { withPeerMailSend } from './messaging/peer-mail-send' import { ORCHESTRATION_CHECK_METHODS } from './messaging/check-methods' import { ORCHESTRATION_MESSAGE_METHODS } from './messaging/message-methods' import { ORCHESTRATION_DISPATCH_METHODS } from './runs/dispatch-methods' @@ -20,7 +21,7 @@ export const ORCHESTRATION_METHODS = requiringOrcadFeature('orchestration', [ ...ORCHESTRATION_WORKER_METHODS, ...ORCHESTRATION_FEDERATION_METHODS, ...ORCHESTRATION_MUTATION_REQUEST_METHODS, - ...ORCHESTRATION_SEND_METHODS, + ...withPeerMailSend(ORCHESTRATION_SEND_METHODS), ...ORCHESTRATION_CHECK_METHODS, ...ORCHESTRATION_MESSAGE_METHODS, ...ORCHESTRATION_DISPATCH_METHODS, diff --git a/src/main/speech/stt-recognizer.ts b/src/main/speech/stt-recognizer.ts index 1998151130c..fb440a87295 100644 --- a/src/main/speech/stt-recognizer.ts +++ b/src/main/speech/stt-recognizer.ts @@ -29,4 +29,6 @@ export type SttRecognizer = { finish(streamId: string): Promise /** Drop the stream's audio undecoded. */ discard(streamId: string): void + /** Called on a cancel's arrival, ahead of the queue: skip the stream's remaining chunks; the running decode finishes. */ + cancel(streamId: string): void } diff --git a/src/main/speech/stt-service.test.ts b/src/main/speech/stt-service.test.ts index f397c55b819..e3864b1e8a0 100644 --- a/src/main/speech/stt-service.test.ts +++ b/src/main/speech/stt-service.test.ts @@ -13,6 +13,8 @@ const { MockWorker, getCreatedWorkerCount, getLastWorker, resetWorkers } = vi.ho terminated = false emitStoppedOnStop = true answerDiscard = true + /** False: a decode for the canceled stream never ends, so the queue never reaches its discard. */ + reachDiscard = true emitReadyOnInit = HoistedMockWorker.emitReadyOnInit emitExitOnTerminate = true terminatePromise: Promise | null = null @@ -64,6 +66,11 @@ const { MockWorker, getCreatedWorkerCount, getLastWorker, resetWorkers } = vi.ho ) { queueMicrotask(() => this.emit('message', { type: 'stopped', streamId: message.streamId })) } + if (message.type === 'discard' && this.answerDiscard && this.reachDiscard) { + queueMicrotask(() => + this.emit('message', { type: 'discarded', streamId: message.streamId }) + ) + } } attachAudioPort(streamId: string, port: unknown): boolean { @@ -464,6 +471,41 @@ describe('SttService', () => { expect(worker!.messages.filter((message) => message.type === 'feed')).toHaveLength(0) }) + it('ends a finishing dictation at once when it is canceled, without waiting for its decode', async () => { + const service = new SttService(readyModels(), spawnMockWorker) + const sink = vi.fn() + await service.startDictation('model-a', sink, undefined, 'desktop') + const worker = getLastWorker()! + worker.emitStoppedOnStop = false + + const finishing = service.stopDictation('desktop') + await service.stopDictation('desktop', { discard: true }) + await finishing + + expect(worker.terminated).toBe(false) + expect(sink).not.toHaveBeenCalledWith(expect.objectContaining({ type: 'error' })) + }) + + it('restarts the speech process when a canceled decode is still running as the next dictation starts', async () => { + vi.useFakeTimers() + try { + const service = new SttService(readyModels(), spawnMockWorker) + await service.startDictation('model-a', vi.fn(), undefined, 'desktop') + const hung = getLastWorker()! + hung.reachDiscard = false + await service.stopDictation('desktop', { discard: true }) + + const next = service.startDictation('model-a', vi.fn(), undefined, 'desktop') + await vi.advanceTimersByTimeAsync(2_000) + await next + + expect(hung.terminated).toBe(true) + expect(getLastWorker()).not.toBe(hung) + } finally { + vi.useRealTimers() + } + }) + it('drops in-flight audio while stop is waiting for the worker', async () => { const service = new SttService(readyModels(), spawnMockWorker) diff --git a/src/main/speech/stt-session-start.ts b/src/main/speech/stt-session-start.ts index ee7bd5b1833..fad17af1f9d 100644 --- a/src/main/speech/stt-session-start.ts +++ b/src/main/speech/stt-session-start.ts @@ -154,6 +154,10 @@ async function acquireSttEngine( const current = state.engine // Bound in the same step it is chosen, so an idle teardown never sees it unused. if (current && sameSttRecognizerConfig(current, config)) { + if (current.draining) { + await current.draining + continue + } clearSttIdleTeardownTimer(state) return bindSttEngine(session, current) } @@ -212,6 +216,7 @@ export function spawnSttEngine( retired: false, vadModelPath, onUnloaded: [], + draining: null, ready: readyPromise.catch((error: unknown) => { failSttEngine(state, engine, STT_WORKER_EXITED_ERROR) throw error diff --git a/src/main/speech/stt-session-state.ts b/src/main/speech/stt-session-state.ts index c54d4dd5eed..2bdd8a022f9 100644 --- a/src/main/speech/stt-session-state.ts +++ b/src/main/speech/stt-session-state.ts @@ -23,6 +23,8 @@ export type SttEngine = SttRecognizerConfig & { vadModelPath: string | undefined /** Called once this process is gone; set when it must not outlive its last dictation. */ onUnloaded: (() => void)[] + /** Set while a canceled stream's last decode finishes; a new dictation waits for it before reusing this process. */ + draining: Promise | null } export type SttSession = { diff --git a/src/main/speech/stt-session-stop.ts b/src/main/speech/stt-session-stop.ts index 325665138c5..12d5c77976f 100644 --- a/src/main/speech/stt-session-stop.ts +++ b/src/main/speech/stt-session-stop.ts @@ -27,9 +27,12 @@ export function stopSttDictation( return Promise.resolve() } if (session.phase === 'stopping') { - // A cancel after Finish still aborts the cloud upload whose text it would throw away. + // A cancel after Finish still drops the work whose text it would throw away. if (options.discard) { session.cloud?.cancel() + if (session.engine) { + discardLocalStream(state, session.engine, session.streamId) + } } return session.stopPromise ?? Promise.resolve() } @@ -78,10 +81,69 @@ function stopLocalSession( discard, finish: (outcome) => finishLocalStop(state, session, engine, outcome, discard) }) - engine.worker.postMessage({ type: discard ? 'discard' : 'stop', streamId: session.streamId }) + if (discard) { + discardLocalStream(state, engine, session.streamId) + } else { + engine.worker.postMessage({ type: 'stop', streamId: session.streamId }) + } return stopped } +/** + * The speech process answers a cancel at once and skips the stream's queued work, but a decode already running + * finishes first. One that outlasts the grace (hung in native code, or just long) restarts the process, as a + * cancel always did, so the next dictation isn't stuck behind it. + */ +function discardLocalStream(state: SttSessionState, engine: SttEngine, streamId: string): void { + const reached = waitForSttWorkerMessage( + engine.worker, + (message) => message.type === 'discarded' && message.streamId === streamId, + DISCARD_DICTATION_TIMEOUT_MS + ) + engine.worker.postMessage({ type: 'discard', streamId }) + const draining = reached.then(async (inTime) => { + // Another device's decode may hold the queue; never kill its dictation. + if (!inTime && state.engine === engine && sttEngineUsers(state, engine).length === 0) { + // A failed kill must not fail the dictation waiting on this; it starts on a fresh process either way. + await teardownSttEngine(state, engine).catch((error: unknown) => + console.warn('[speech] Could not stop the busy speech process after a cancel:', error) + ) + } + }) + engine.draining = draining + void draining.finally(() => { + if (engine.draining === draining) { + engine.draining = null + } + }) +} + +/** Resolves true when the worker sends a matching message (or exits), false after `timeoutMs`. */ +function waitForSttWorkerMessage( + worker: SttEngine['worker'], + matches: (message: { type: string; streamId?: string }) => boolean, + timeoutMs: number +): Promise { + return new Promise((resolve) => { + const settle = (inTime: boolean): void => { + clearTimeout(timer) + worker.off('message', onMessage) + worker.off('exit', onExit) + resolve(inTime) + } + const onMessage = (message: { type: string; streamId?: string }): void => { + if (matches(message)) { + settle(true) + } + } + const onExit = (): void => settle(true) + const timer = setTimeout(() => settle(false), timeoutMs) + timer.unref?.() + worker.on('message', onMessage) + worker.on('exit', onExit) + }) +} + function finishLocalStop( state: SttSessionState, session: SttSession, diff --git a/src/main/speech/stt-transcribe-cpp-recognizer.ts b/src/main/speech/stt-transcribe-cpp-recognizer.ts index 9d5b51f0852..f292db59403 100644 --- a/src/main/speech/stt-transcribe-cpp-recognizer.ts +++ b/src/main/speech/stt-transcribe-cpp-recognizer.ts @@ -174,9 +174,15 @@ export async function createTranscribeCppRecognizer( }) } + // Canceled streams, until their queued discard runs; also covers a finish that already closed its stream. + const canceled = new Set() + const decode = async (streamId: string, stream: StreamCoverage, chunks: Float32Array[]) => { let undecoded = chunks.reduce((total, chunk) => total + chunk.length, 0) for (const chunk of chunks) { + if (canceled.has(streamId)) { + return + } const startedAt = performance.now() const text = await transcribeChunk(chunk, stream.options) const decodeMs = performance.now() - startedAt @@ -254,7 +260,11 @@ export async function createTranscribeCppRecognizer( } }, discard(streamId) { + canceled.delete(streamId) closeStream(streamId)?.vad?.dispose() + }, + cancel(streamId) { + canceled.add(streamId) } } } diff --git a/src/main/speech/stt-worker-cpu-fallback.ts b/src/main/speech/stt-worker-cpu-fallback.ts index d86c3d1b15b..ede1d976006 100644 --- a/src/main/speech/stt-worker-cpu-fallback.ts +++ b/src/main/speech/stt-worker-cpu-fallback.ts @@ -17,7 +17,7 @@ type ChildMessage = | { type: 'chunk'; streamId: string; text: string; samples: number } | { type: 'speech-pending'; streamId: string; pending: boolean } | { - type: 'ready' | 'loading' | 'stopped' | 'error' | 'partial' | 'final' + type: 'ready' | 'loading' | 'stopped' | 'discarded' | 'error' | 'partial' | 'final' streamId?: string text?: string error?: string diff --git a/src/main/speech/stt-worker.ts b/src/main/speech/stt-worker.ts index 0eb2d2d8c64..ebcbdcc64b4 100644 --- a/src/main/speech/stt-worker.ts +++ b/src/main/speech/stt-worker.ts @@ -16,6 +16,8 @@ type AudioPortInput = { const parentPort = connectSttWorkerParentPort() let recognizer: SttRecognizer | null = null const audioPorts = new Map() +// Canceled streams whose queued work is skipped instead of decoded; cleared when their discard runs. +const discarded = new Set() // transcribe-cpp decodes off the event loop; one queue keeps feeds behind init and stop behind feeds. let queue: Promise = Promise.resolve() @@ -37,6 +39,9 @@ async function handleInit(init: SttRecognizerInit): Promise { } async function handleFeed(msg: Extract): Promise { + if (discarded.has(msg.streamId)) { + return + } try { await recognizer?.feed(msg.streamId, msg.samples, msg.sampleRate) } catch (err) { @@ -48,6 +53,11 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v if (!port) { return } + // A cancel that arrived while this attach waited in line must not let the port's frames reopen the stream. + if (discarded.has(streamId)) { + port.close() + return + } const input: AudioPortInput = { port, onEnded: null, ending: false } const end = (): void => { if (audioPorts.get(streamId) !== input) { @@ -83,6 +93,9 @@ function attachAudioPort(streamId: string, port: MessagePortMain | undefined): v } async function handleStop(streamId: string): Promise { + if (discarded.has(streamId)) { + return + } const input = audioPorts.get(streamId) // Main's stop can overtake the renderer's last frames on the port; finish only after them. if (input) { @@ -98,15 +111,23 @@ async function handleStop(streamId: string): Promise { } } -// A canceled dictation drops its audio undecoded. -function handleDiscard(streamId: string): void { +// A cancel answers at once, even during Finish, and the stream's queued audio is skipped rather than decoded. +function cancelStream(streamId: string): void { + discarded.add(streamId) + recognizer?.cancel(streamId) const input = audioPorts.get(streamId) audioPorts.delete(streamId) input?.port.close() - recognizer?.discard(streamId) parentPort.post({ type: 'stopped', streamId }) } +// Runs in queue order, after everything queued for the stream before the cancel; the host waits for it. +function handleDiscard(streamId: string): void { + discarded.delete(streamId) + recognizer?.discard(streamId) + parentPort.post({ type: 'discarded', streamId }) +} + function handleMessage( msg: SttWorkerRequest, ports: readonly MessagePortMain[] @@ -130,5 +151,8 @@ function handleMessage( parentPort.onMessage((message, ports) => { // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only the session host posts here, and it sends SttWorkerRequest. const msg = message as SttWorkerRequest + if (msg.type === 'discard') { + cancelStream(msg.streamId) + } enqueue(() => handleMessage(msg, ports)) }) diff --git a/src/renderer/src/assets/main.css b/src/renderer/src/assets/main.css index f5412aa291f..59c6fd15fb5 100644 --- a/src/renderer/src/assets/main.css +++ b/src/renderer/src/assets/main.css @@ -61,6 +61,7 @@ --color-chat-foreground: var(--chat-foreground); --color-chat-foreground-strong: var(--chat-foreground-strong); --color-chat-foreground-faint: var(--chat-foreground-faint); + --color-chat-unread: var(--chat-unread); --color-chat-user-surface: var(--chat-user-surface); --color-chat-user-border: var(--chat-user-border); --color-chat-code-foreground: var(--chat-code-foreground); @@ -741,6 +742,8 @@ body[data-modal-overlay-open] /* Mixed toward the foreground so each clears text contrast on the canvas in either scheme. */ --chat-focus-ring: color-mix(in srgb, var(--chat-source-foreground) 50%, var(--chat-canvas)); --chat-warning: color-mix(in srgb, var(--status-warning) 70%, var(--chat-source-foreground)); + /* What the reader has not seen yet: the transcript's "New" marker, in the note hue. */ + --chat-unread: color-mix(in srgb, var(--callout-note) 70%, var(--chat-source-foreground)); --chat-diff-added: color-mix(in srgb, var(--diff-added) 70%, var(--chat-source-foreground)); --chat-diff-removed: color-mix(in srgb, var(--diff-removed) 70%, var(--chat-source-foreground)); --chat-link: color-mix( diff --git a/src/renderer/src/components/native-chat/NativeChatMessageList.tsx b/src/renderer/src/components/native-chat/NativeChatMessageList.tsx index 13599631a53..c4fc8c4b958 100644 --- a/src/renderer/src/components/native-chat/NativeChatMessageList.tsx +++ b/src/renderer/src/components/native-chat/NativeChatMessageList.tsx @@ -4,13 +4,14 @@ import { NATIVE_CHAT_TRANSCRIPT_COLUMN_CLASS } from './appearance/native-chat-appearance-style' import { NativeChatJumpToLatest } from './viewport/scroll/NativeChatJumpToLatest' +import { NativeChatNewRowsAnnouncement } from './viewport/new-rows/NativeChatNewRowsMarker' +import { useNativeChatNewRowsMarker } from './viewport/new-rows/native-chat-new-rows-marker' import { withoutPullRequestEventRows } from '../../../../shared/native-chat/native-chat-pull-request-event' import { useNativeChatRowTypography } from './viewport/virtualization/use-native-chat-row-typography' import { useCallback, useMemo, useRef, useState } from 'react' import { useTranslate } from '@/i18n/use-translate' import { useAppStore } from '@/store' import { - nativeChatScrollsToLatestOnSend, nativeChatShowsCiResultLines, nativeChatShowsTurnDiffSummary } from '../../../../shared/native-chat/native-chat-appearance-settings' @@ -243,12 +244,18 @@ export function NativeChatMessageList({ [allSlots, journalItems, stopping, journalSubmissions, latestTurn, hostWork] ) const navigationRequest = useNativeChatNavigationRequest(slots, messages, subagentSections) + const newRows = useNativeChatNewRowsMarker( + slots, + waitingSlots, + isVisible && session.readCurrent === true + ) const transcriptWindow = useNativeChatTranscriptWindow({ scrollRef, slots, isVisible, // Rail jumps, diff reveals and subagent reveals share one mutually exclusive pin. revealIndex: navigationRequest.revealIndex, + newRowsMarkerIndex: newRows.index, typography }) const transcriptScroll = useNativeChatTranscriptScroll({ @@ -326,25 +333,15 @@ export function NativeChatMessageList({ onLeaveEnd: readerLeavesEnd, onScrollGesture: transcriptScroll.forwardedGesture }) - // Off keeps a reader who scrolled up in place; one at the end still follows the reply. - const revealSent = useCallback( - (readerWasAtEnd = readerAtEnd()) => { - if (!nativeChatScrollsToLatestOnSend(useAppStore.getState().settings) && !readerWasAtEnd) { - return - } - beginNavigation() - scrollToBottom('send') - }, - [beginNavigation, readerAtEnd, scrollToBottom] - ) - useNativeChatMessageListHandle( - ref, - revealSent, - readerInput.revealFindMatch, - allSlots, + useNativeChatMessageListHandle(ref, { readerAtEnd, - navigation.revealSubagent - ) + beginNavigation, + scrollToBottom, + onSent: newRows.dismiss, + revealFindMatch: readerInput.revealFindMatch, + slots: allSlots, + revealSubagent: navigation.revealSubagent + }) const rowContext = useNativeChatTranscriptRowContext({ agent: session.agent, @@ -397,6 +394,7 @@ export function NativeChatMessageList({ loadedMessageIds={loadedMessageIds} context={rowContext} window={transcriptWindow} + newRowsMarker={newRows.marker} forwardGesture={readerInput.forwardedGesture} /> {liveLine ? ( @@ -417,6 +415,7 @@ export function NativeChatMessageList({ pendingId={navigation.pendingRailId} /> + diff --git a/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts b/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts index 19b8e0e927f..5db0cd89263 100644 --- a/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts +++ b/src/renderer/src/components/native-chat/messages/actions/use-native-chat-reveal-latest.ts @@ -1,5 +1,7 @@ import { useCallback, useImperativeHandle, useMemo, useRef } from 'react' +import { useAppStore } from '@/store' import { hasAskAnswer } from '../../../../../../shared/native-chat/input/native-chat-ask-answer' +import { nativeChatScrollsToLatestOnSend } from '../../../../../../shared/native-chat/native-chat-appearance-settings' import { nativeChatSlotCopyText } from './native-chat-message-copy-text' import type { NativeChatTranscriptSlot } from '../../conversation/slots/native-chat-transcript-slots' import type { NativeChatInteractiveSend } from '../../session/terminal/send/use-native-chat-interactive-send' @@ -86,22 +88,36 @@ export function useNativeChatRevealLatest(): { return { messageListRef, revealLatest, captureRevealLatest } } -function ignoreFindMatch(): void {} - -function alwaysAtEnd(): boolean { - return true -} - -function ignoreSubagent(): void {} - +/** Own sends follow the latest; with that setting off, only a reader who was at the end does. */ export function useNativeChatMessageListHandle( ref: React.Ref | undefined, - revealLatest: NativeChatMessageListHandle['revealLatest'], - revealFindMatch: NativeChatMessageListHandle['revealFindMatch'] = ignoreFindMatch, - slots: readonly NativeChatTranscriptSlot[] = [], - readerAtEnd: NativeChatMessageListHandle['readerAtEnd'] = alwaysAtEnd, - revealSubagent: NativeChatMessageListHandle['revealSubagent'] = ignoreSubagent + { + readerAtEnd, + beginNavigation, + scrollToBottom, + onSent, + revealFindMatch, + slots, + revealSubagent + }: Pick & { + beginNavigation: () => void + scrollToBottom: (reason: 'send') => void + /** The reader answered, whether or not the view moves. */ + onSent: () => void + slots: readonly NativeChatTranscriptSlot[] + } ): void { + const revealLatest = useCallback( + (readerWasAtEnd = readerAtEnd()) => { + onSent() + if (!nativeChatScrollsToLatestOnSend(useAppStore.getState().settings) && !readerWasAtEnd) { + return + } + beginNavigation() + scrollToBottom('send') + }, + [beginNavigation, onSent, readerAtEnd, scrollToBottom] + ) useImperativeHandle( ref, () => ({ diff --git a/src/renderer/src/components/native-chat/messages/rows/NativeChatTranscriptRow.tsx b/src/renderer/src/components/native-chat/messages/rows/NativeChatTranscriptRow.tsx index 2a2d3f5090e..3bfadce54b5 100644 --- a/src/renderer/src/components/native-chat/messages/rows/NativeChatTranscriptRow.tsx +++ b/src/renderer/src/components/native-chat/messages/rows/NativeChatTranscriptRow.tsx @@ -20,6 +20,8 @@ import { nativeChatRowOffersRewind } from '../../../../../../shared/native-chat/ import { NativeChatForkContext } from '../actions/native-chat-fork-context' import { nativeChatSlotOffersFork } from '../actions/native-chat-side-chat-fork-point' import { isActionableStructuredAgentSessionPrompt } from '../../../../../../shared/agent-session/view/structured-agent-session-live-turn' +import { NativeChatNewRowsMarker } from '../../viewport/new-rows/NativeChatNewRowsMarker' +import type { NativeChatNewRowsMarkerState } from '../../viewport/new-rows/native-chat-new-rows-marker' /** Memoize shared context so unchanged rows keep their props during transcript updates. */ export type NativeChatTranscriptRowContext = { @@ -45,10 +47,13 @@ export type NativeChatTranscriptRowContext = { /** Wrap message, status and rollup for one virtualized measurement; internal spacing is the row’s, inter-row gaps the window’s. */ export const NativeChatTranscriptRow = memo(function NativeChatTranscriptRow({ slot, - context + context, + newRowsMarker }: { slot: NativeChatTranscriptSlot context: NativeChatTranscriptRowContext + /** The "New" marker, when it draws in this row. */ + newRowsMarker?: NativeChatNewRowsMarkerState }): React.JSX.Element { // Offered only on the conversation's own turn-opening prompts (`nativeChatRowOffersRewind`). const rewind = useContext(NativeChatRewindContext) @@ -84,6 +89,9 @@ export const NativeChatTranscriptRow = memo(function NativeChatTranscriptRow({ const deliveryNotice = context.deliveryNotices?.get(message.id) const predecessors = context.taskListPredecessors.get(message.id) const expanded = turnKey ? context.expandedTurnIds.has(turnKey) : undefined + const marker = newRowsMarker ? ( + + ) : null const statusRow = status ? ( + {newRowsMarker?.place === 'above-row' ? marker : null} {/* A turn with no user bubble carries its bar above its first row. */} {slot.statusAbove ? statusRow : null} {!slot.drawsMessage ? null : receipt ? ( @@ -154,6 +163,7 @@ export const NativeChatTranscriptRow = memo(function NativeChatTranscriptRow({ workRun={workRun} /> )} + {newRowsMarker?.place === 'above-turn-bar' ? marker : null} {slot.statusAbove ? null : statusRow} {turnDiff ? : null} {openedPullRequests ? ( diff --git a/src/renderer/src/components/native-chat/selection-quotes/NativeChatSelectionQuote.test.tsx b/src/renderer/src/components/native-chat/selection-quotes/NativeChatSelectionQuote.test.tsx index 3d6cf7f6ce9..6010d9e6127 100644 --- a/src/renderer/src/components/native-chat/selection-quotes/NativeChatSelectionQuote.test.tsx +++ b/src/renderer/src/components/native-chat/selection-quotes/NativeChatSelectionQuote.test.tsx @@ -4,7 +4,6 @@ import { useRef } from 'react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type * as I18n from '@/i18n/i18n' import { createGlobalSettingsFixture } from '../../../../../shared/global-settings-test-fixture' -import { getOverlayFocusOwners } from '@/lib/overlay-focus-ownership' import { useAppStore } from '../../../store/index' import type { NativeChatComposerHandle } from '../composer/native-chat-composer-types' import { NativeChatMarkdown } from '../markdown/NativeChatMarkdown' @@ -89,31 +88,6 @@ describe('NativeChatSelectionQuote', () => { vi.useRealTimers() }) - it('quotes a selection from an agent reply into the composer as a blockquote', () => { - render() - releaseSelection(screen.getByTestId('first')) - - const quote = screen.getByRole('button', { name: 'Add to chat' }) - fireEvent.pointerDown(quote, { button: 0 }) - fireEvent.click(quote) - - expect(appendText).toHaveBeenCalledWith('> Use the draft store.\n>\n> It already appends.\n\n') - expect(screen.queryByRole('button', { name: 'Add to chat' })).toBeNull() - }) - - it('closes the offer before the composer takes the quote and focus', () => { - let focusOwners: HTMLElement[] | undefined - appendText.mockImplementationOnce(() => { - focusOwners = getOverlayFocusOwners() - }) - render() - releaseSelection(screen.getByTestId('first')) - - fireEvent.click(screen.getByRole('button', { name: 'Add to chat' })) - - expect(focusOwners).toEqual([]) - }) - it('takes a selection that runs on over text that cannot be selected', () => { render() releaseSelection(screen.getByTestId('first'), screen.getByTestId('time')) @@ -126,28 +100,6 @@ describe('NativeChatSelectionQuote', () => { releaseSelection(screen.getByTestId('first'), screen.getByTestId('next')) expect(screen.queryByRole('button', { name: 'Add to chat' })).toBeNull() }) - - it('offers nothing when a press leaves the selection as it was', () => { - render() - releaseSelection(screen.getByTestId('first')) - fireEvent.pointerDown(screen.getByTestId('time'), { button: 0 }) - fireEvent.mouseUp(screen.getByTestId('time'), { button: 0 }) - settle() - - expect(screen.queryByRole('button', { name: 'Add to chat' })).toBeNull() - }) - - it('withdraws the offer once the selection is gone', () => { - render() - releaseSelection(screen.getByTestId('first')) - expect(screen.getByRole('button', { name: 'Add to chat' })).toBeTruthy() - - act(() => { - window.getSelection()!.removeAllRanges() - document.dispatchEvent(new Event('selectionchange')) - }) - expect(screen.queryByRole('button', { name: 'Add to chat' })).toBeNull() - }) }) describe('NativeChatSelectionQuote copy on select', () => { diff --git a/src/renderer/src/components/native-chat/subagents/NativeChatMessageList.subagent-sections.test.tsx b/src/renderer/src/components/native-chat/subagents/NativeChatMessageList.subagent-sections.test.tsx index e707460e927..78427ceae40 100644 --- a/src/renderer/src/components/native-chat/subagents/NativeChatMessageList.subagent-sections.test.tsx +++ b/src/renderer/src/components/native-chat/subagents/NativeChatMessageList.subagent-sections.test.tsx @@ -22,11 +22,6 @@ vi.mock('../media/visuals/NativeChatInlineVisual', () => ({ vi.mock('@/lib/open-agent-message-sender', () => ({ openAgentMessageSender: unusedChatLeaves.openSender })) -afterEach(() => { - expect(unusedChatLeaves.inlineVisual).not.toHaveBeenCalled() - expect(unusedChatLeaves.openSender).not.toHaveBeenCalled() -}) - import type { AgentJournalItemBody, AgentJournalProducerLinkage, @@ -132,60 +127,6 @@ import { session, stubLayout } from '../viewport/virtualization/native-chat-wind vi.restoreAllMocks() }) - it("draws an open agent's rows between its entry and the next one", () => { - const agents = [ - { id: 'task-a', label: 'lane a', state: 'completed' as const }, - { id: 'task-b', label: 'lane b', state: 'completed' as const }, - { id: 'task-c', label: 'lane c', state: 'completed' as const } - ] - const items = [ - itemsWith('completed', false)[0]!, - journalItem( - 'spawn', - { - kind: 'message', - role: 'system', - blocks: [{ type: 'subagent-group', groupId: 'group-1', agents }] - }, - 2 - ), - ...agents.map(({ id, label }, index) => - journalItem( - `${id}-said`, - { - kind: 'message', - role: 'assistant', - blocks: [{ type: 'text', text: `Read ${label}.` }] - }, - 3 + index, - { agentId: id, producerKind: 'agent' } - ) - ) - ] - render( - - ) - fireEvent.click(screen.getByRole('button', { name: /Ran 3 subagents/ })) - fireEvent.click(screen.getByRole('button', { name: /lane b/ })) - - const order = [ - screen.getByRole('button', { name: /lane a/, expanded: false }), - screen.getByRole('button', { name: /lane b/, expanded: true }), - screen.getByText('Read lane b.'), - screen.getByRole('button', { name: /lane c/, expanded: false }) - ] - for (const [index, node] of order.slice(1).entries()) { - expect(order[index]!.compareDocumentPosition(node)).toBe(Node.DOCUMENT_POSITION_FOLLOWING) - } - expect(screen.queryByText('Read lane a.')).toBeNull() - expect(screen.getAllByText(/^lane [abc]$/)).toHaveLength(3) - }) - // Journal turn records own subagent edits, even when a newer send is queued or the opener is unloaded. it("counts a subagent's edit in the turn the journal says it was made in", () => { const say = (itemId: string, role: 'user' | 'assistant', text: string, sequence: number) => @@ -275,16 +216,6 @@ import { session, stubLayout } from '../viewport/virtualization/native-chat-wind expect(screen.getByText('The PR is CLEAN.')).toBeInTheDocument() }) - it('opens a working subagent while the session waits on it, and closes it once the parent moves on', () => { - const { rerender } = render(listOf('working', true)) - expect(screen.getByText('The PR is CLEAN.')).toBeInTheDocument() - expect(screen.getByRole('button', { name: /explore the lane/, expanded: true })).toBeVisible() - expect(screen.getAllByText('explore the lane')).toHaveLength(1) - rerender(listOf('working')) - expect(screen.queryByText('The PR is CLEAN.')).toBeNull() - expect(screen.getByRole('button', { name: /Kicked off 1 subagent/ })).toBeInTheDocument() - }) - it("keeps the reader's choice over the frontier, in either direction", () => { const { rerender } = render(listOf('working', true)) fireEvent.click(screen.getByRole('button', { name: /explore the lane/, expanded: true })) diff --git a/src/renderer/src/components/native-chat/subagents/native-chat-subagent-section-work-runs.test.ts b/src/renderer/src/components/native-chat/subagents/native-chat-subagent-section-work-runs.test.ts deleted file mode 100644 index 217105afe20..00000000000 --- a/src/renderer/src/components/native-chat/subagents/native-chat-subagent-section-work-runs.test.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { describe, expect, it } from 'vitest' -import type { - NativeChatMessage, - NativeChatSubagentState -} from '../../../../../shared/native-chat/native-chat-types' -import { projectNativeChatTranscript } from '../../../../../shared/native-chat/transcript/native-chat-transcript-projection' -import { nativeChatTurnMembership } from '../../../../../shared/native-chat/transcript/native-chat-turn-membership' -import { compareNativeChatDisplayMessages as compareMessages } from '../../../../../shared/native-chat/transcript/native-chat-message-order' -import { nativeChatSubagentSections } from './native-chat-subagent-sections' -import { - buildNativeChatTranscriptSlots, - type NativeChatTranscriptSlot -} from '../conversation/slots/native-chat-transcript-slots' - -let sequence = 0 - -function row( - id: string, - blocks: NativeChatMessage['blocks'], - overrides: Partial = {} -): NativeChatMessage { - sequence += 1 - return { - id, - role: 'assistant', - blocks, - timestamp: 1, - source: 'transcript', - journalPosition: { sequence, index: 0 }, - ...overrides - } -} - -const say = (value: string) => [{ type: 'text' as const, text: value }] -const call = (name: string) => [{ type: 'tool-call' as const, name, input: { name } }] -const by = (agentId: string) => ({ agentId, producerKind: 'agent' as const }) -const thought = (id: string, agentId?: string, state: 'running' | 'completed' = 'completed') => - row(id, say(`thinking ${id}`), { - role: 'reasoning', - state, - ...(agentId ? by(agentId) : {}) - }) -const roster = (id: string, agentId: string, state: NativeChatSubagentState) => - row( - id, - [ - { - type: 'subagent-group', - groupId: `group-${id}`, - agents: [{ id: agentId, label: agentId, state }] - } - ], - { role: 'system' } - ) - -function slotsOf(rows: NativeChatMessage[], open: Record) { - const { conversation, subagentRows } = projectNativeChatTranscript(rows, compareMessages) - const { turnKeys, liveTurnKey } = nativeChatTurnMembership(conversation) - return buildNativeChatTranscriptSlots({ - messages: conversation, - turnKeys, - liveTurnKey, - receipts: new Map(), - turnStatuses: { active: null, completedByTurn: {} }, - turnDiffs: new Map(), - expandedTurnKeys: new Set(), - isWorking: false, - lifecycleWorking: false, - subagentSections: nativeChatSubagentSections(conversation, subagentRows), - subagentChoices: { sections: new Map(Object.entries(open)), rosters: new Map() } - }) -} - -/** `>` per section a row sits in, then its id, its run's member ids, or `[agent]` for a head. */ -function outline(slots: readonly NativeChatTranscriptSlot[]): string[] { - return slots.map((slot) => { - const indent = '>'.repeat(slot.depth) - if (slot.kind === 'subagent') { - return `${indent}[${slot.agentId}]` - } - if (slot.kind === 'subagent-entries') { - return `${indent}${slot.agents.map((agent) => agent.id).join()}` - } - return `${indent}${slot.workRun?.map((member) => member.id).join('+') ?? slot.message.id}` - }) -} - -describe('work runs and subagent sections', () => { - it('never runs across a section head', () => { - const rows = [ - row('ask', say('go'), { role: 'user' }), - row('a', call('Read')), - row('s-a', call('Bash'), by('helper')), - thought('r1'), - row('c', call('Grep')) - ] - expect(outline(slotsOf(rows, { helper: false }))).toEqual(['ask', 'a', '[helper]', 'r1+c']) - expect(outline(slotsOf(rows, { helper: true }))).toEqual([ - 'ask', - 'a', - '[helper]', - '>s-a', - 'r1+c' - ]) - }) - - it("keeps a working section's open thought out of its run until it ends", () => { - const rows = (state: NativeChatSubagentState, last: 'running' | 'completed') => { - sequence = 0 - return [ - row('ask', say('go'), { role: 'user' }), - roster('spawn', 'helper', state), - thought('s-r1', 'helper'), - row('s-a', call('Read'), by('helper')), - thought('s-r2', 'helper', last) - ] - } - expect(outline(slotsOf(rows('working', 'running'), { helper: true }))).toEqual([ - 'ask', - 'spawn', - '>s-r1+s-a', - '>s-r2' - ]) - expect(outline(slotsOf(rows('working', 'completed'), { helper: true }))).toEqual([ - 'ask', - 'spawn', - '>s-r1+s-a+s-r2' - ]) - // A thought its finished agent never closed is not still being thought. - expect(outline(slotsOf(rows('completed', 'running'), { helper: true }))).toEqual([ - 'ask', - 'spawn', - '>s-r1+s-a+s-r2' - ]) - }) -}) diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatResolvedView.streaming-input.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatResolvedView.streaming-input.test.tsx deleted file mode 100644 index f5769543dcb..00000000000 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatResolvedView.streaming-input.test.tsx +++ /dev/null @@ -1,163 +0,0 @@ -// @vitest-environment happy-dom - -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' -import type { NativeChatMessage } from '../../../../../../shared/native-chat/native-chat-types' -import type { NativeChatLiveSession } from '../../session/terminal/read/native-chat-live-session-contract' -import type { NativeChatComposerProps } from '../../composer/native-chat-composer-types' -import type { AgentStatusPayload } from '../../../../../../shared/agent-status/contracts/agent-status-types' -import type * as StreamingModule from '../../../../../../shared/native-chat/native-chat-streaming' -import { clearPendingSendCacheForTests } from '../../session/terminal/delivery/native-chat-pending' - -const probe = vi.hoisted<{ - session: NativeChatLiveSession | null - inputs: (readonly NativeChatMessage[])[] - shown: NativeChatMessage[] -}>(() => ({ session: null, inputs: [], shown: [] })) -vi.mock('../../session/terminal/read/use-native-chat-retained-session', () => ({ - useNativeChatRetainedSession: () => probe.session -})) -vi.mock('../../NativeChatMessageList', () => ({ - NativeChatMessageList: ({ session }: { session: NativeChatLiveSession }) => { - probe.shown = session.messages - return null - } -})) -vi.mock('../../composer/NativeChatComposer', () => ({ - NativeChatComposer: ({ onOptimisticSend }: NativeChatComposerProps) => ( - - ) -})) -vi.mock('../../../../../../shared/native-chat/native-chat-streaming', async (importOriginal) => { - const original = await importOriginal() - return { - ...original, - deriveNativeChatStreamingText: ( - args: Parameters[0] - ) => { - probe.inputs.push(args.messages) - return original.deriveNativeChatStreamingText(args) - } - } -}) - -const { NativeChatResolvedView } = await import('../../NativeChatResolvedView') -const { TooltipProvider } = await import('@/components/ui/tooltip') -const { useAppStore } = await import('../../../../store/index') -const paneKey = 'preview-perf:leaf' -const message = (id: string, role: 'user' | 'assistant', text: string): NativeChatMessage => ({ - id, - role, - blocks: [{ type: 'text', text }], - timestamp: 1, - source: 'transcript' -}) - -function setPreview(text: string, extra: Partial = {}): void { - useAppStore.getState().setAgentStatus(paneKey, { - prompt: 'Prompt', - agentType: 'claude', - state: 'working', - lastAssistantMessage: text, - ...extra - }) -} - -function previewTicks(expected: readonly NativeChatMessage[], extra = {}): void { - probe.inputs = [] - for (let tick = 0; tick < 20; tick += 1) { - act(() => setPreview(`Current reply ${tick}`, extra)) - } - expect(probe.inputs.length).toBeGreaterThanOrEqual(20) - expect(new Set(probe.inputs).size).toBe(1) - expect(probe.inputs[0]).toBe(expected) -} - -beforeEach(() => { - vi.useFakeTimers({ toFake: ['Date'] }) - vi.setSystemTime(1_000) - clearPendingSendCacheForTests() - probe.inputs = [] - probe.shown = [] - useAppStore.setState({ agentStatusByPaneKey: {}, nativeChatLaunchPromptByTabId: {} }) -}) -afterEach(() => { - cleanup() - vi.useRealTimers() - clearPendingSendCacheForTests() - useAppStore.setState({ agentStatusByPaneKey: {}, nativeChatLaunchPromptByTabId: {} }) -}) - -describe('preview input allocation at realistic history sizes', () => { - it.each([1_000, 10_000])('reuses input across ticks with %i loaded messages', (size) => { - const transcript = Array.from({ length: size }, (_, index) => - message(`history-${index}`, index % 2 ? 'assistant' : 'user', `History ${index}`) - ) - probe.session = { - messages: transcript, - status: 'working', - sessionId: 'preview-perf', - agent: 'claude', - hasMore: false, - loadingEarlier: false, - olderHistoryGeneration: 0, - loadEarlier: vi.fn(), - readPhase: 'ready' - } - setPreview('Current reply') - render( - - - - ) - previewTicks(transcript) - act(() => fireEvent.click(screen.getByRole('button', { name: 'send' }))) - const queued = probe.inputs.at(-1)! - expect(queued).toHaveLength(size + 1) - expect(queued.at(-1)?.pendingSend).toBe('queued') - previewTicks(queued) - expect(probe.shown.at(-2)?.id).toBe('streaming') - expect(probe.shown.at(-1)?.pendingSend).toBe('queued') - previewTicks(queued, { lastAssistantMessageIsToolOutput: true }) - - const caughtUp = [...transcript, message('reply', 'assistant', 'Current reply 19')] - act(() => { - probe.session = { ...probe.session!, messages: caughtUp } - setPreview('Current reply 19') - }) - const updated = probe.inputs.at(-1)! - expect(updated).not.toBe(queued) - expect(updated.at(-2)?.id).toBe('reply') - expect(updated.at(-1)?.pendingSend).toBe('queued') - previewTicks(updated) - - act(() => { - probe.session = { ...probe.session!, status: 'ready' } - setPreview('Idle', { state: 'done' }) - }) - const idle = probe.inputs.at(-1)! - expect(idle).not.toBe(updated) - previewTicks(idle, { state: 'done' }) - - const retired = [ - ...caughtUp, - { ...message('follow-up', 'user', 'Follow up'), timestamp: 1_001 }, - { ...message('next-reply', 'assistant', 'Done'), timestamp: 1_002 } - ] - act(() => { - probe.session = { ...probe.session!, messages: retired } - setPreview('Done', { state: 'done' }) - }) - previewTicks(retired, { state: 'done' }) - }) -}) diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.availability-notice.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.availability-notice.test.tsx index 3ff5042c5f5..361334ba5f8 100644 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.availability-notice.test.tsx +++ b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.availability-notice.test.tsx @@ -140,15 +140,6 @@ function turnRow(): AgentJournalRenderItem { } } -it('leaves an auth turn row as the explanation when the catalog also says signed out', () => { - mocks.unavailable = { reason: 'notSignedIn', account: 'system' } - const row = startFailureRow({ kind: 'notSignedIn', account: 'system' }) - mocks.journalItems = [turnRow(), { ...row, itemId: 'codex-unauthorized-error', sequence: 3 }] - render(pane()) - expect(screen.queryByText(CODEX_SIGNED_OUT)).toBeNull() - expect(mocks.composerProps?.structuredTransport).not.toHaveProperty('unavailable') -}) - it('stays dismissed for the same verdict, and shows again when it changes or comes back', () => { mocks.unavailable = { reason: 'notSignedIn', account: 'system' } const view = render(pane()) @@ -175,21 +166,6 @@ it('stays dismissed for the same verdict, and shows again when it changes or com expect(screen.getByText(CODEX_SIGNED_OUT)).toBeTruthy() }) -it('says a reason once when the failed start already says it, keeping its Retry', () => { - mocks.launchLifecycle = 'failed' - mocks.launchFailure = { - kind: 'refused', - code: 'agent_session_operation_invalid', - details: { reason: 'notSignedIn' } - } - mocks.unavailable = { reason: 'notSignedIn', account: 'system' } - render(pane()) - - expect(screen.getAllByText(new RegExp(CODEX_SIGNED_OUT.replace(/[.`]/g, '\\$&')))).toHaveLength(1) - expect(screen.getByText(`Chat could not be started. ${CODEX_SIGNED_OUT}`)).toBeTruthy() - expect(screen.getByRole('button', { name: 'Retry' })).toBeTruthy() -}) - it("leaves the reason to the transcript's start-failure row, but not a different one", () => { mocks.journalItems = [startFailureRow({ kind: 'notSignedIn' })] mocks.unavailable = { reason: 'notSignedIn', account: 'system' } diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.notice-card.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.notice-card.test.tsx index 5129b5dab76..1fc8490b19f 100644 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.notice-card.test.tsx +++ b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.notice-card.test.tsx @@ -61,7 +61,6 @@ vi.mock('../../prompts/NativeChatApprovalCard', () => moduleFactories.nativeChat vi.mock('../../prompts/NativeChatQuestionCard', () => moduleFactories.nativeChatQuestionCard()) import { NativeChatStructuredSession } from '../../NativeChatStructuredSession' -import type { AgentJournalRenderItem } from '../../../../../../shared/agent-session/journal/agent-session-journal-types' import type { NativeChatStructuredComposerTransport } from '../../composer/submit/native-chat-structured-composer-transport' const SESSION_ID = 'notice-card-session' @@ -118,29 +117,3 @@ it('keeps a send failure’s raw error apart and lets the user dismiss it', () = fireEvent.click(screen.getByRole('button', { name: 'Dismiss' })) expect(screen.queryByText('Your message was not sent.')).toBeNull() }) - -const APPROVAL: AgentJournalRenderItem = JSON.parse( - JSON.stringify({ - itemId: 'approval-item', - revision: 1, - sequence: 1, - observedAt: 1, - body: { - kind: 'approval', - title: 'Run the plan?', - detail: null, - subject: { kind: 'plan', text: 'x' }, - options: [{ id: 'allow', label: 'Approve' }], - resolution: { state: 'pending', selectedOptionId: null, resolvedBy: null, resolvedAt: null } - } - }) -) - -it("keeps the notices above an approval card that takes the composer's place", () => { - mocks.launchLifecycle = 'failed' - mocks.promptItems = [APPROVAL] - renderPane() - expect(screen.queryByTestId('structured-composer')).toBeNull() - expect(screen.getByText('Chat could not be started.')).toBeTruthy() - expect(screen.getByRole('button', { name: 'Retry' })).toBeTruthy() -}) diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.refusal-notices.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.refusal-notices.test.tsx index 5a35fcca991..446452ff451 100644 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.refusal-notices.test.tsx +++ b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.refusal-notices.test.tsx @@ -35,72 +35,6 @@ afterEach(() => { resetStructuredSessionMocks() }) -{ - function renderPane(): void { - render( - - ) - } - - function journalRefusal( - reason: 'journalCorrupt' | 'journalUnavailable' | 'journalWrittenByNewerOrca' - ) { - return { code: 'agent_session_journal_unreadable', details: { reason } } as const - } - - it('says a damaged history cannot load in one line, without claiming Orca keeps trying', () => { - mocks.status = 'error' - mocks.readRefusal = journalRefusal('journalCorrupt') - mocks.messages = [] - - renderPane() - - expect(screen.getAllByText('Unable to load this chat.')).toHaveLength(1) - expect(screen.queryByText('Could not load conversation')).toBeNull() - expect(screen.queryByText(/keeps trying/)).toBeNull() - expect(screen.queryByText(/agent_session_/)).toBeNull() - // Nothing to send into: a send would only be refused and say it again. - expect(mocks.composerProps).toBeNull() - }) - - it("names a history that couldn't open right now once, in its one line", () => { - mocks.status = 'error' - mocks.readRefusal = journalRefusal('journalUnavailable') - mocks.messages = [] - - renderPane() - - expect(screen.getAllByText("Orca couldn't open this chat's history right now.")).toHaveLength(1) - expect(screen.queryByText('Could not load conversation')).toBeNull() - expect(screen.queryByText(/keeps trying/)).toBeNull() - expect(screen.queryByText(/Try again/)).toBeNull() - // It can clear, so the composer stays and a send waits for the read. - expect(mocks.composerProps).not.toBeNull() - }) - - it('says once that a newer Orca saved the chat and only an update opens it, with no composer', () => { - mocks.status = 'error' - mocks.readRefusal = journalRefusal('journalWrittenByNewerOrca') - mocks.messages = [] - - renderPane() - - expect( - screen.getAllByText('This chat was saved by a newer Orca. Update Orca to open it.') - ).toHaveLength(1) - expect(screen.queryByText('Could not load conversation')).toBeNull() - expect(screen.queryByText(/keeps trying/)).toBeNull() - expect(mocks.composerProps).toBeNull() - }) -} - { const STILL_WORKING = "The agent is still working. Run /clear when it's done." const BACKGROUND = @@ -158,15 +92,4 @@ afterEach(() => { pane.rerender() expect(screen.queryByText(BACKGROUND)).toBeNull() }) - - it('a line naming nothing the chat shows stays until the next send, as before', () => { - mocks.turnId = 'turn-1' - const pane = renderPane() - composerSays('Remove attachments before using a chat-session command.') - mocks.turnId = null - pane.rerender() - expect(screen.getByText('Remove attachments before using a chat-session command.')).toBeTruthy() - composerSays(null) - expect(screen.queryByText('Remove attachments before using a chat-session command.')).toBeNull() - }) } diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.retained-draft.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.retained-draft.test.tsx deleted file mode 100644 index aeae740f719..00000000000 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.retained-draft.test.tsx +++ /dev/null @@ -1,164 +0,0 @@ -// @vitest-environment happy-dom - -import type * as LaunchRegistry from '@/lib/agent-session/launch/structured-agent-session-launch-registry' -import type * as LaunchOptions from '@/lib/agent-session/launch/structured-agent-session-launch-options' -import { act, cleanup, render, screen } from '@testing-library/react' -import { afterEach, expect, it, vi } from 'vitest' -import type { ManagedRuntimeInstallObservation } from '@/runtime/hosts/installation/use-managed-runtime-install' - -const { mocks, moduleFactories, resetStructuredSessionMocks, launchModule } = await vi.hoisted( - async () => { - const setup = ( - await import('./NativeChatStructuredSession.test-harness') - ).createStructuredSessionMocks() - return { - ...setup, - launchModule: setup.moduleFactories.structuredAgentSessionLaunch() - } - } -) -const install = vi.hoisted((): { observation: ManagedRuntimeInstallObservation } => ({ - observation: { kind: 'inactive' } -})) - -vi.mock('@/lib/agent-session/launch/structured-agent-session-launch', () => ({ - retryStructuredAgentSessionLaunch: launchModule.retryStructuredAgentSessionLaunch, - relaunchFailedStructuredAgentSessionForMessage: - launchModule.relaunchFailedStructuredAgentSessionForMessage -})) -vi.mock( - '@/lib/agent-session/launch/structured-agent-session-launch-registry', - async (importOriginal) => ({ - ...(await importOriginal()), - getStructuredAgentSessionLaunchLifecycle: launchModule.getStructuredAgentSessionLaunchLifecycle, - getStructuredAgentSessionLaunchResumes: launchModule.getStructuredAgentSessionLaunchResumes, - useStructuredAgentSessionLaunchLifecycle: launchModule.useStructuredAgentSessionLaunchLifecycle, - useStructuredAgentSessionLaunchFailure: launchModule.useStructuredAgentSessionLaunchFailure - }) -) -vi.mock( - '@/lib/agent-session/launch/structured-agent-session-launch-options', - async (importOriginal) => ({ - ...(await importOriginal()), - useStructuredAgentSessionLaunchSelection: launchModule.useStructuredAgentSessionLaunchSelection - }) -) -vi.mock('@/lib/agent-session/launch/structured-agent-session-launch-message', () => - moduleFactories.structuredAgentSessionLaunchMessage() -) -vi.mock('../../../../runtime/native-chat/requests/structured-agent-session-call', () => - moduleFactories.structuredAgentSessionCall() -) -vi.mock('../../../../runtime/native-chat/requests/structured-agent-session-subscriptions', () => - moduleFactories.structuredAgentSessionSubscriptions() -) -vi.mock('@/components/machine-identity/use-published-machine-name', () => ({ - usePublishedMachineName: () => null -})) -vi.mock('../../session/structured/use-structured-agent-session', () => - moduleFactories.useStructuredAgentSession() -) -vi.mock('../../appearance/use-native-chat-font-size', () => moduleFactories.useNativeChatFontSize()) -vi.mock('../../links/actions/use-native-chat-file-link-context', () => - moduleFactories.useNativeChatFileLinkContext() -) -vi.mock('../../pane/ownership/use-native-chat-tab-owner', () => - moduleFactories.useNativeChatTabOwner() -) -vi.mock('../../links/actions/use-native-chat-file-link-click', () => - moduleFactories.useNativeChatFileLinkClick() -) -vi.mock('../../NativeChatMessageList', () => moduleFactories.nativeChatMessageList()) -vi.mock('../../host-access/NativeChatEmptyState', () => moduleFactories.nativeChatEmptyState()) -vi.mock('../../prompts/NativeChatApprovalCard', () => moduleFactories.nativeChatApprovalCard()) -vi.mock('../../prompts/NativeChatQuestionCard', () => moduleFactories.nativeChatQuestionCard()) -vi.mock('@/runtime/hosts/installation/use-managed-runtime-install', () => ({ - useManagedRuntimeInstall: () => install.observation -})) - -import { NativeChatStructuredSession } from '../../NativeChatStructuredSession' -import { clearNativeChatComposerDraftsForTests } from '../../drafts/native-chat-draft-reset' -import { hydrateNativeChatComposerDrafts } from '../../drafts/native-chat-composer-draft-load' -import { - readNativeChatComposerDraft, - updateNativeChatComposerDraft -} from '../../drafts/native-chat-composer-draft-store' -import { structuredAgentSessionDraftScopeKey } from '../../drafts/native-chat-draft-scope' - -import { TooltipProvider } from '@/components/ui/tooltip' -import { promptEditor } from '../../composer/editor/native-chat-prompt-editor.test-support' -import { promptTextContent } from '../../composer/editor/native-chat-prompt-document-text' -import { - createMemoryNativeChatComposerDraftStorage, - setNativeChatComposerDraftStorageForTests -} from '../../drafts/native-chat-composer-draft-storage' - -afterEach(() => { - cleanup() - resetStructuredSessionMocks() - clearNativeChatComposerDraftsForTests() -}) -it('shows and copies the ordinary source of an unsupported installed launch', async () => { - const text = 'Hand Off context\nwith trailing bytes ' - clearNativeChatComposerDraftsForTests() - setNativeChatComposerDraftStorageForTests(createMemoryNativeChatComposerDraftStorage()) - await hydrateNativeChatComposerDrafts() - mocks.launchLifecycle = 'failed' - mocks.launchFailure = { kind: 'refused', code: 'structured_agent_session_unsupported' } - mocks.messages = [] - install.observation = { - kind: 'active', - subject: { target: { kind: 'local' }, runtimeId: null, agent: 'codex' }, - snapshot: { - kind: 'known', - state: { - agent: 'codex', - revision: 1, - available: true, - version: '1.3.0', - installedVersion: '1.3.0', - installedCurrent: true, - downloadBytes: 0, - source: 'external', - operation: null - } - }, - removable: false - } - updateNativeChatComposerDraft( - structuredAgentSessionDraftScopeKey('session-1'), - { text, document: promptTextContent(text) }, - 'immediate' - ) - render( - - - - ) - const field = await screen.findByRole('textbox') - expect(field.getAttribute('contenteditable')).toBe('true') - const editor = promptEditor(field) - act(() => { - editor.commands.selectAll() - }) - let copied: string | undefined - editor.view.someProp('clipboardTextSerializer', (serializer) => { - copied = serializer( - editor.state.doc.slice(editor.state.selection.from, editor.state.selection.to), - editor.view - ) - return true - }) - expect(copied).toBe(text) - expect(editor.state.doc.textBetween(0, editor.state.doc.content.size, '\n')).toBe(text) - expect(readNativeChatComposerDraft(structuredAgentSessionDraftScopeKey('session-1')).text).toBe( - text - ) -}) diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test.tsx index a87070dc4c9..a87e02f97b4 100644 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test.tsx +++ b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.test.tsx @@ -1,11 +1,8 @@ -import { getDefaultSettings } from '../../../../../../shared/constants' // @vitest-environment happy-dom import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' -import { useAppStore } from '@/store' - const { mocks, moduleFactories, resetStructuredSessionMocks } = await vi.hoisted(async () => (await import('./NativeChatStructuredSession.test-harness')).createStructuredSessionMocks() ) @@ -38,59 +35,6 @@ vi.mock('../../prompts/NativeChatQuestionCard', () => moduleFactories.nativeChat import { NativeChatStructuredSession } from '../../NativeChatStructuredSession' describe('NativeChatStructuredSession', () => { - it('isolates mounted chats from unrelated settings and renders their live follow-up choice', async () => { - const original = useAppStore.getState().settings - const settings = { ...getDefaultSettings('/tmp'), nativeChatQueueFollowUps: false } - useAppStore.setState({ settings }) - const view = render( - <> - {['local-chat', 'ssh-chat'].map((sessionId) => ( - - ))} - - ) - try { - await act(async () => {}) - mocks.controllerRender.mockClear() - act(() => - useAppStore.setState({ settings: { ...settings, nativeChatQueueFollowUps: false } }) - ) - expect(mocks.controllerRender).not.toHaveBeenCalled() - act(() => - useAppStore.setState({ - settings: { ...settings, showTasksButton: !settings.showTasksButton } - }) - ) - expect(mocks.controllerRender).not.toHaveBeenCalled() - act(() => useAppStore.setState({ settings: { ...settings, nativeChatQueueFollowUps: true } })) - expect(new Set(mocks.controllerRender.mock.calls.map(([sessionId]) => sessionId))).toEqual( - new Set(['local-chat', 'ssh-chat']) - ) - expect(mocks.controllerRender.mock.calls.every(([, queue]) => queue === true)).toBe(true) - mocks.controllerRender.mockClear() - act(() => useAppStore.setState({ settings })) - expect(new Set(mocks.controllerRender.mock.calls.map(([sessionId]) => sessionId))).toEqual( - new Set(['local-chat', 'ssh-chat']) - ) - expect(mocks.controllerRender.mock.calls.every(([, queue]) => queue === false)).toBe(true) - } finally { - view.unmount() - useAppStore.setState({ settings: original }) - } - }) - afterEach(() => { cleanup() resetStructuredSessionMocks() diff --git a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.wsl-host-stopped.test.tsx b/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.wsl-host-stopped.test.tsx deleted file mode 100644 index 0a02fc94b7e..00000000000 --- a/src/renderer/src/components/native-chat/surfaces/integration/NativeChatStructuredSession.wsl-host-stopped.test.tsx +++ /dev/null @@ -1,83 +0,0 @@ -// @vitest-environment happy-dom - -import '@testing-library/jest-dom/vitest' -import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' -import { afterEach, expect, it, vi } from 'vitest' -import type { WslHostStatus } from '../../../../../../shared/wsl-host-types' - -const { moduleFactories, resetStructuredSessionMocks } = await vi.hoisted(async () => - (await import('./NativeChatStructuredSession.test-harness')).createStructuredSessionMocks() -) -const reconnectHost = vi.hoisted(() => - vi.fn<(environmentId: string, failureMessage?: string) => Promise>() -) - -vi.mock('../../../../runtime/native-chat/requests/structured-agent-session-call', () => - moduleFactories.structuredAgentSessionCall() -) -vi.mock('../../../../runtime/native-chat/requests/structured-agent-session-subscriptions', () => - moduleFactories.structuredAgentSessionSubscriptions() -) -vi.mock('../../session/structured/use-structured-agent-session', () => - moduleFactories.useStructuredAgentSession() -) -vi.mock('../../appearance/use-native-chat-font-size', () => moduleFactories.useNativeChatFontSize()) -vi.mock('../../links/actions/use-native-chat-file-link-context', () => - moduleFactories.useNativeChatFileLinkContext() -) -vi.mock('../../links/actions/use-native-chat-file-link-click', () => - moduleFactories.useNativeChatFileLinkClick() -) -vi.mock('../../NativeChatMessageList', () => moduleFactories.nativeChatMessageList()) -vi.mock('../../composer/NativeChatComposer', () => moduleFactories.nativeChatComposer()) -vi.mock('../../prompts/NativeChatApprovalCard', () => moduleFactories.nativeChatApprovalCard()) -vi.mock('../../prompts/NativeChatQuestionCard', () => moduleFactories.nativeChatQuestionCard()) -vi.mock('@/components/status-bar/runtime-environment-explicit-connect', () => ({ - connectRuntimeHostAndReloadProjects: reconnectHost -})) - -import { useAppStore } from '@/store/index' -import { NativeChatStructuredSession } from '../../NativeChatStructuredSession' - -const initialState = useAppStore.getState() - -function setWslStatus(state: WslHostStatus['state']): void { - act(() => { - useAppStore.setState({ - wslHostStatuses: { - 'wsl:ubuntu': { environmentId: 'wsl:ubuntu', distro: 'Ubuntu', state, detail: null } - } - }) - }) -} - -function renderPane() { - return render( - - ) -} - -afterEach(() => { - cleanup() - resetStructuredSessionMocks() - reconnectHost.mockReset() - useAppStore.setState({ wslHostStatuses: initialState.wslHostStatuses }) -}) - -it('still offers Start after a start that failed', async () => { - renderPane() - setWslStatus('distro-stopped') - reconnectHost.mockResolvedValue(false) - await act(async () => { - fireEvent.click(screen.getByRole('button', { name: 'Start' })) - }) - expect(screen.getByText('Ubuntu is stopped')).toBeInTheDocument() - expect(screen.getByRole('button', { name: 'Start' })).toBeEnabled() -}) diff --git a/src/renderer/src/components/native-chat/tools/runs/native-chat-tool-fold.test.ts b/src/renderer/src/components/native-chat/tools/runs/native-chat-tool-fold.test.ts index a0773e8eb9f..1821a4904ca 100644 --- a/src/renderer/src/components/native-chat/tools/runs/native-chat-tool-fold.test.ts +++ b/src/renderer/src/components/native-chat/tools/runs/native-chat-tool-fold.test.ts @@ -16,42 +16,6 @@ function msg( } describe('foldToolMessages', () => { - // Invisible provider events must not split visible runs; rendered plan updates do. - it('keeps one run across a wordless provider event, but not across a plan update', () => { - const frameRow = (id: string, kind: string): NativeChatMessage => - msg({ - id, - role: 'system', - blocks: [ - { - type: 'text', - text: `codex · ${kind}`, - providerFrame: { - provider: 'codex', - kind, - payload: { head: '{}', byteLength: 2, digest: 'digest', truncated: false } - } - } - ] - }) - const run = (between: NativeChatMessage): string[] => - foldToolMessages([ - msg({ - id: 'a1', - blocks: [ - { type: 'text', text: 'go' }, - { type: 'tool-call', name: 'Bash', input: {} } - ] - }), - msg({ id: 'r1', role: 'tool', blocks: [{ type: 'tool-result', output: 'ok' }] }), - between, - msg({ id: 'a2', blocks: [{ type: 'tool-call', name: 'Bash', input: {} }] }), - msg({ id: 'r2', role: 'tool', blocks: [{ type: 'tool-result', output: 'ok' }] }) - ]).map((message) => message.id) - expect(run(frameRow('frame', 'notification:future/event'))).toEqual(['a1', 'frame']) - expect(run(frameRow('plan', 'notification:turn/plan/updated'))).toEqual(['a1', 'plan', 'a2']) - }) - it('drops a result after a user turn abandons the pending call', () => { const folded = foldToolMessages([ msg({ id: 'c', role: 'assistant', blocks: [{ type: 'tool-call', name: 'Bash', input: {} }] }), @@ -61,21 +25,6 @@ describe('foldToolMessages', () => { expect(folded.map((m) => m.id)).toEqual(['c', 'u']) }) - it('does not fold a message carrying prose alongside a tool block', () => { - const folded = foldToolMessages([ - msg({ id: 'a', role: 'assistant', blocks: [{ type: 'text', text: 'first' }] }), - msg({ - id: 'b', - role: 'assistant', - blocks: [ - { type: 'text', text: 'more' }, - { type: 'tool-call', name: 'Read', input: {} } - ] - }) - ]) - expect(folded.map((m) => m.id)).toEqual(['a', 'b']) - }) - it('attributes a Claude tool result carried with a harness sidecar', () => { const folded = foldToolMessages([ msg({ @@ -136,54 +85,4 @@ describe('foldToolMessages', () => { }) ]) }) - - it('folds through a harness noise boundary but not a real user turn', () => { - const folded = foldToolMessages([ - msg({ id: 'a', role: 'assistant', blocks: [{ type: 'tool-call', name: 'Read', input: {} }] }), - msg({ id: 'n', role: 'user', blocks: [{ type: 'text', text: 'done' }] }), - msg({ id: 'r', role: 'tool', blocks: [{ type: 'tool-result', output: 'ok' }] }) - ]) - - expect(folded.find((message) => message.id === 'a')?.blocks).toEqual([ - { type: 'tool-call', name: 'Read', input: {} }, - { type: 'tool-result', output: 'ok' } - ]) - }) -}) - -describe('spawn-group roster rows', () => { - const roster = msg({ - id: 'roster', - role: 'system', - blocks: [ - { type: 'text', text: 'Kicked off 1 subagent — 1 working' }, - { - type: 'subagent-group', - groupId: 'thread:turn-1', - agents: [{ id: 'child-1', label: 'read', state: 'working' }] - } - ] - }) - - it('does not end the assistant run the following tool messages fold into', () => { - const folded = foldToolMessages([ - msg({ - id: 'a', - role: 'assistant', - blocks: [ - { type: 'text', text: 'working' }, - { type: 'tool-call', name: 'Bash', input: {} } - ] - }), - roster, - msg({ id: 't', role: 'tool', blocks: [{ type: 'tool-result', output: 'done' }] }) - ]) - - expect(folded.map((message) => message.id)).toEqual(['a', 'roster']) - expect(folded[0]?.blocks.map((block) => block.type)).toEqual([ - 'text', - 'tool-call', - 'tool-result' - ]) - }) }) diff --git a/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find.test.tsx b/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find.test.tsx index 0e6b3efe4b9..fb14de901d8 100644 --- a/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find.test.tsx +++ b/src/renderer/src/components/native-chat/viewport/find/use-native-chat-find.test.tsx @@ -1,5 +1,5 @@ // @vitest-environment happy-dom -import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' +import { act, cleanup, render, screen } from '@testing-library/react' import { useRef, type ReactNode } from 'react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { KeybindingOverrides } from '../../../../../../shared/keybindings/types' @@ -193,18 +193,6 @@ function findInput(): HTMLInputElement | null { return inputs.find((input) => input === document.activeElement) ?? inputs[0] ?? null } -function typeQuery(value: string): void { - const input = findInput() - if (!input) { - throw new Error('find bar is not open') - } - fireEvent.change(input, { target: { value } }) -} - -function status(): string | null | undefined { - return findInput()?.parentElement?.querySelector('[aria-live]')?.textContent -} - const TRANSCRIPT = ( <>

alpha one

@@ -231,26 +219,6 @@ afterEach(() => { }) describe('native chat find', () => { - it('refocuses on Mod+F without moving the match, and keeps the query on reopen', () => { - const composer = composerHandle() - render() - pressModF(composer.element) - typeQuery('alpha') - press(findInput()!, 'Enter') - composer.element.focus() - - expect(pressModF(composer.element).defaultPrevented).toBe(true) - const input = findInput()! - expect(document.activeElement).toBe(input) - expect([input.selectionStart, input.selectionEnd]).toEqual([0, 5]) - expect(status()).toBe('2/2') - - press(input, 'Escape') - expect(findInput()).toBeNull() - pressModF(composer.element) - expect(findInput()?.value).toBe('alpha') - }) - it('falls back to the composer when the element find was opened from is gone', () => { const composer = composerHandle() const view = render( diff --git a/src/renderer/src/components/native-chat/viewport/history/NativeChatMessageList.older-history.test.tsx b/src/renderer/src/components/native-chat/viewport/history/NativeChatMessageList.older-history.test.tsx index 1bf00fb4bfb..14df9d4defa 100644 --- a/src/renderer/src/components/native-chat/viewport/history/NativeChatMessageList.older-history.test.tsx +++ b/src/renderer/src/components/native-chat/viewport/history/NativeChatMessageList.older-history.test.tsx @@ -134,7 +134,6 @@ function flowAboveSpacer(spacer: HTMLElement): number { } type LoadEarlier = () => Promise -const lands = (): LoadEarlier => vi.fn(async (): Promise => 'applied') function paging({ messages, @@ -359,23 +358,6 @@ describe('older history auto-load', () => { } }) - it('stops, and shows neither status nor button, once history runs out', async () => { - const loadEarlier = lands() - const base = markers(100, 150) - const { rerender } = render(paging({ messages: base, loadEarlier })) - sentinelInRange = true - deliverIntersections() - rerender(paging({ messages: base, loadEarlier, loadingEarlier: true })) - await settle() - rerender(paging({ messages: [...markers(90, 100), ...base], loadEarlier, hasMore: false })) - deliverIntersections() - - expect(loadEarlier).toHaveBeenCalledTimes(1) - expect(observations.size).toBe(0) - expect(screen.queryByRole('status')).not.toBeInTheDocument() - expect(screen.queryByRole('button', { name: /load earlier/i })).not.toBeInTheDocument() - }) - it('offers a manual load after a failed page, and resumes auto-loading once it succeeds', async () => { const loadEarlier = vi .fn() diff --git a/src/renderer/src/components/native-chat/viewport/new-rows/NativeChatMessageList.new-rows-marker.test.tsx b/src/renderer/src/components/native-chat/viewport/new-rows/NativeChatMessageList.new-rows-marker.test.tsx new file mode 100644 index 00000000000..ca44f3e5740 --- /dev/null +++ b/src/renderer/src/components/native-chat/viewport/new-rows/NativeChatMessageList.new-rows-marker.test.tsx @@ -0,0 +1,172 @@ +// @vitest-environment happy-dom + +import '@testing-library/jest-dom/vitest' + +import { act, cleanup, screen } from '@testing-library/react' +import { render } from '../../messages/actions/native-chat-tooltip-test-harness' +import { createRef } from 'react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { NativeChatMessage } from '../../../../../../shared/native-chat/native-chat-types' +import { getDefaultSettings } from '../../../../../../shared/constants' +import { useAppStore } from '@/store' +import { NativeChatMessageList } from '../../NativeChatMessageList' +import type { NativeChatMessageListHandle } from '../../messages/actions/use-native-chat-reveal-latest' +import { + deliverResizes, + layout, + marker, + ROW_PITCH_PX, + scrollTranscript, + session, + stubLayout, + stubResizeObserver, + TRANSCRIPT_LENGTH +} from '../virtualization/native-chat-windowing-test-harness' + +// Pacing is not this test's subject. +vi.mock('../../markdown/use-native-chat-paced-text', async (importOriginal) => + (await import('../../markdown/native-chat-unpaced-text-fixture')).unpacedTextModule( + importOriginal + ) +) + +afterEach(cleanup) + +function chat( + length: number, + isVisible: boolean, + { + ref, + readCurrent = true + }: { ref?: React.Ref; readCurrent?: boolean } = {} +): React.JSX.Element { + const messages: NativeChatMessage[] = Array.from({ length }, (_, index) => ({ + ...marker(index), + journalPosition: { sequence: index + 1, index: 0 } + })) + return ( + + ) +} + +describe('the "New" marker in the transcript', () => { + let paneShown = true + let restore: (() => void)[] = [] + beforeEach(() => { + paneShown = true + vi.spyOn(document, 'hasFocus').mockReturnValue(true) + restore = [ + stubLayout({ scrollGeometry: true, offsetChain: true, isVisible: () => paneShown }), + stubResizeObserver() + ] + }) + afterEach(() => { + for (const undo of restore.toReversed()) { + undo() + } + layout.measuredRowHeights = [] + vi.restoreAllMocks() + }) + + // The window mounts only rows near the view; the marker's row is far from the end it opens at. + it('is drawn above the first row that came while the chat was hidden, however far back', () => { + const { rerender } = render(chat(TRANSCRIPT_LENGTH, true)) + rerender(chat(TRANSCRIPT_LENGTH, false)) + rerender(chat(TRANSCRIPT_LENGTH + 60, true)) + + const row = screen.getByRole('separator', { name: 'New' }).closest('[data-index]') + expect(row?.dataset.index).toBe(String(TRANSCRIPT_LENGTH)) + }) + + it('tells a screen reader that new messages came, in a region that was already there', () => { + const { container, rerender } = render(chat(TRANSCRIPT_LENGTH, true)) + const region = container.querySelector('[data-native-chat-scroll] ~ [aria-live="polite"]') + expect(region).toBeEmptyDOMElement() + + rerender(chat(TRANSCRIPT_LENGTH, false)) + rerender(chat(TRANSCRIPT_LENGTH + 60, true)) + expect(region).toHaveTextContent('New messages arrived while you were away') + }) + + it('is neither drawn nor announced when the reader turned it off', () => { + const settings = useAppStore.getState().settings + useAppStore.setState({ + settings: { ...getDefaultSettings('/tmp'), nativeChatMarkNewMessages: false } + }) + try { + const { container, rerender } = render(chat(TRANSCRIPT_LENGTH, true)) + rerender(chat(TRANSCRIPT_LENGTH, false)) + rerender(chat(TRANSCRIPT_LENGTH + 60, true)) + + expect(screen.queryByRole('separator', { name: 'New' })).toBeNull() + expect( + container.querySelector('[data-native-chat-scroll] ~ [aria-live="polite"]') + ).toBeEmptyDOMElement() + } finally { + useAppStore.setState({ settings }) + } + }) + + it('goes once the reader sends something', () => { + const ref = createRef() + const { rerender } = render(chat(TRANSCRIPT_LENGTH, true, { ref })) + rerender(chat(TRANSCRIPT_LENGTH, false, { ref })) + rerender(chat(TRANSCRIPT_LENGTH + 60, true, { ref })) + expect(screen.getByRole('separator', { name: 'New' })).toBeInTheDocument() + + act(() => ref.current?.revealLatest()) + + expect(screen.queryByRole('separator', { name: 'New' })).toBeNull() + }) + + describe('when the reader comes back to new rows', () => { + /** Catch up after restoring the hidden pane’s scroll position. */ + function returnToNewRows(rerender: (ui: React.JSX.Element) => void, count = 60): void { + paneShown = false + rerender(chat(TRANSCRIPT_LENGTH, false, { readCurrent: false })) + paneShown = true + rerender(chat(TRANSCRIPT_LENGTH, true, { readCurrent: false })) + rerender(chat(TRANSCRIPT_LENGTH + count, true)) + } + const scrollTop = (container: HTMLElement): number | undefined => + container.querySelector('[data-native-chat-scroll]')?.scrollTop + + it('brings a reader who was at the end to where the new rows begin', () => { + const { container, rerender } = render(chat(TRANSCRIPT_LENGTH, true)) + returnToNewRows(rerender) + + expect(scrollTop(container)).toBe(TRANSCRIPT_LENGTH * ROW_PITCH_PX) + }) + + it('keeps the reader where the new rows begin while a reply is still being drawn', async () => { + const { container, rerender } = render(chat(TRANSCRIPT_LENGTH, true)) + returnToNewRows(rerender, 1) + + const drawn: number[] = [] + drawn[TRANSCRIPT_LENGTH] = 2_000 + layout.measuredRowHeights = drawn + act(() => void deliverResizes()) + await act(async () => { + await new Promise((resolve) => requestAnimationFrame(() => resolve())) + }) + + expect(scrollTop(container)).toBe(TRANSCRIPT_LENGTH * ROW_PITCH_PX) + }) + + it('leaves a reader who was parked further up where they were', () => { + const { container, rerender } = render(chat(TRANSCRIPT_LENGTH, true)) + const readingAt = 2_000 + scrollTranscript(container, readingAt) + returnToNewRows(rerender) + + expect(screen.getByRole('separator', { name: 'New' })).toBeInTheDocument() + expect(scrollTop(container)).toBe(readingAt) + }) + }) +}) diff --git a/src/renderer/src/components/native-chat/viewport/new-rows/NativeChatNewRowsMarker.test.tsx b/src/renderer/src/components/native-chat/viewport/new-rows/NativeChatNewRowsMarker.test.tsx new file mode 100644 index 00000000000..b90a1ddb8af --- /dev/null +++ b/src/renderer/src/components/native-chat/viewport/new-rows/NativeChatNewRowsMarker.test.tsx @@ -0,0 +1,92 @@ +// @vitest-environment happy-dom + +import { cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { NativeChatNewRowsMarker } from './NativeChatNewRowsMarker' +import type { NativeChatNewRowsMarkerState as Marker } from './native-chat-new-rows-marker' +import { overrideLayoutProperty } from '../virtualization/native-chat-windowing-test-harness' + +afterEach(cleanup) + +function marker(overrides: Partial = {}): Marker { + return { + key: 'answer', + place: 'above-row', + arriving: true, + onArrived: vi.fn(), + onDismiss: vi.fn(), + ...overrides + } +} + +const VIEWPORT_PX = 600 +const ROW_TOP_PX = 1_000 + +let restoreLayout: (() => void)[] = [] + +/** Place the view bottom aboveMarkerPx short of a marker positioned intoRowPx within its row. */ +function pane( + value: Marker, + { intoRowPx = 0, aboveMarkerPx = 0 }: { intoRowPx?: number; aboveMarkerPx?: number } = {} +) { + const isRow = (element: HTMLElement): boolean => element.hasAttribute('data-index') + const isMarker = (element: HTMLElement): boolean => element.getAttribute('role') === 'separator' + const layout = (name: string, get: (this: HTMLElement) => unknown) => + overrideLayoutProperty(name, { get }) + restoreLayout = [ + layout('clientHeight', () => VIEWPORT_PX), + layout('scrollTop', () => ROW_TOP_PX + intoRowPx - VIEWPORT_PX - aboveMarkerPx), + layout('offsetTop', function () { + return isRow(this) ? ROW_TOP_PX : isMarker(this) ? intoRowPx : 0 + }), + layout('offsetParent', function () { + return isRow(this) || isMarker(this) ? this.parentElement : null + }) + ] + const onReveal = vi.fn() + const view = render( +
+
+ +
+
+
+ +
+
+