diff --git a/docs/run-engine/SPEC.md b/docs/run-engine/SPEC.md index e87fab913d..411a809979 100644 --- a/docs/run-engine/SPEC.md +++ b/docs/run-engine/SPEC.md @@ -472,7 +472,7 @@ Deleting a thread is a Stop of it (§14.7, `clearQueue` included): its delegatio - `read_only_needs_folder`: the caller's thread has no folder, so nothing could hold a read-only child read-only. A child whose binding says read-only but which has no folder is refused at every turn with the same code. - `shared_unsupported_for_provider` ("leave readOnly out"): the target cannot run a read-only child. `subagentTargetRefusal` refuses an adapter whose `childPolicy` is `isolated_only` (Antigravity), and `ProviderAdapter.childPreflight` reads the user's setup (Codex, below). -The same target check runs again before every turn of a read-only child (first, continued, resumed or queued), against the provider and setup in effect then. A child whose provider was switched to one that cannot be read-only, or whose Codex config gained an MCP server, has that turn refused with the same message (as a failed turn on its thread); it never runs unconstrained. +The same target check runs again before every turn of a read-only child (first, continued, resumed or queued), against the provider and setup in effect then. A child whose provider was switched to one that cannot be read-only, or whose Codex config can no longer be read or have its MCP servers switched off, has that turn refused (as a failed turn on its thread); it never runs unconstrained. Nesting is allowed: a subagent, read-only or not, gets the delegation tools and may start subagents. Per provider: @@ -480,7 +480,7 @@ Nesting is allowed: a subagent, read-only or not, gets the delegation tools and |---|---|---| | pi | `toolAccess` allows read tools and the delegation tools | — | | Claude | `permissionMode: 'default'`; the PreToolUse hook answers `ask` for every call, so allow rules in the user's settings cannot skip the callback (and denies the rest itself); the hook and `canUseTool` allow only Read, Glob, Grep, WebSearch, WebFetch, AskUserQuestion, ToolSearch, Claude's own subagent tool (`Agent`/`Task`) and Mousse's read and delegation tools; every write tool, shell and external MCP tool is denied, and so is every tool call a native Claude subagent makes beyond those, because the hook sees them too; the user's MCP servers stay configured | No restriction: native `Agent`/`Task` and Mousse's delegation tools are available | -| Codex | `read-only` sandbox on thread open and every `turn/start`; approvals stay `on-request`, and Mousse answers command, file, permission and MCP approvals with a denial itself, never asking the user. Refused (`shared_unsupported_for_provider`) while the user's effective Codex config (read through the app-server: home config, system config, the trusted project's config, active profile) has any enabled MCP server, a Mousse-managed one included, or cannot be read, because Mousse cannot deny a Codex MCP tool call | Codex's own subagents are left on, as in a terminal. The app-server completes the parent's turn while its helpers keep running and does not stop them when the parent is interrupted, so Mousse reads the helper tree back from the app-server (`thread/list` with `ancestorThreadId` lists every descendant at any depth with its status; `thread/read` places a helper that is not listed yet). The child's run stays open until two snapshots of that tree, at least 250 ms apart, agree that the thread and all its helpers are idle. Stop, a failed turn or a cancel interrupts the thread's turn and every busy helper at any depth, and repeats that each round, for up to 10 s, until the tree is at rest; the chat ends at once and the thread's next message waits for the sweep. The direct helpers named by spawn items (`subAgentActivity`, `collabAgentToolCall` `spawnAgent`) stay tracked as a floor under the tree. A listed helper whose parent is missing from the list (its own row is not written yet, or it was archived) is placed by walking `thread/read` up its ancestors; the process falls back only when that walk proves the row belongs to another root thread, or when an answer is malformed. A thread is interrupted only when its ancestry is established in the current snapshot: its parent link, as listed or as found when it was placed, names the thread or a thread established the same way. Any busy thread whose chain does not lead back (a refused read, an archived ancestor) still holds the run open but is never interrupted until a later snapshot establishes it; a placed thread whose parent has left the listing is walked up again each snapshot. A process that cannot list descendants (an older Codex, or no state database) is detected once at start and keeps the item-based tracking alone: helpers spawned by helpers are then not tracked, and a spawn reported after Stop or after the release is not interrupted. One timing assumption remains: a helper spawned during a turn that was already running when a check began is seen only once the server lists it or it has run, and its first turn normally starts before the spawner's next model call; a helper missed that way is picked up by the next run on the thread. A helper that ignores its interrupt keeps running past the sweep. A steer that arrives while only helpers are running starts a turn on the Codex thread, and a turn the server starts on its own meanwhile (a helper's message) joins the run. When neither the thread nor a helper under it has started or ended a turn or changed status for 30 minutes (a nested helper counts only through those notifications, not through its output), or the helpers hold the run for 4 hours in total, they are interrupted and the run ends `done` with its own text plus "Some helpers were abandoned after N minutes without activity" | +| Codex | `read-only` sandbox on thread open and every `turn/start`; approvals stay `on-request`, and Mousse answers command, file, permission and MCP approvals with a denial itself, never asking the user. Mousse cannot deny a Codex MCP tool call, so the user's enabled MCP servers in the effective Codex config (read through the app-server: home config, system config, the trusted project's config, active profile) are switched off for the child's thread (`mcp_servers..enabled = false`), and a caller on this computer is told which. Refused (`shared_unsupported_for_provider`) when that config cannot be read, an enabled server there uses a name Mousse reserves for its own, or a server's name cannot be switched off by key | Codex's own subagents are left on, as in a terminal. The app-server completes the parent's turn while its helpers keep running and does not stop them when the parent is interrupted, so Mousse reads the helper tree back from the app-server (`thread/list` with `ancestorThreadId` lists every descendant at any depth with its status; `thread/read` places a helper that is not listed yet). The child's run stays open until two snapshots of that tree, at least 250 ms apart, agree that the thread and all its helpers are idle. Stop, a failed turn or a cancel interrupts the thread's turn and every busy helper at any depth, and repeats that each round, for up to 10 s, until the tree is at rest; the chat ends at once and the thread's next message waits for the sweep. The direct helpers named by spawn items (`subAgentActivity`, `collabAgentToolCall` `spawnAgent`) stay tracked as a floor under the tree. A listed helper whose parent is missing from the list (its own row is not written yet, or it was archived) is placed by walking `thread/read` up its ancestors; the process falls back only when that walk proves the row belongs to another root thread, or when an answer is malformed. A thread is interrupted only when its ancestry is established in the current snapshot: its parent link, as listed or as found when it was placed, names the thread or a thread established the same way. Any busy thread whose chain does not lead back (a refused read, an archived ancestor) still holds the run open but is never interrupted until a later snapshot establishes it; a placed thread whose parent has left the listing is walked up again each snapshot. A process that cannot list descendants (an older Codex, or no state database) is detected once at start and keeps the item-based tracking alone: helpers spawned by helpers are then not tracked, and a spawn reported after Stop or after the release is not interrupted. One timing assumption remains: a helper spawned during a turn that was already running when a check began is seen only once the server lists it or it has run, and its first turn normally starts before the spawner's next model call; a helper missed that way is picked up by the next run on the thread. A helper that ignores its interrupt keeps running past the sweep. A steer that arrives while only helpers are running starts a turn on the Codex thread, and a turn the server starts on its own meanwhile (a helper's message) joins the run. When neither the thread nor a helper under it has started or ended a turn or changed status for 30 minutes (a nested helper counts only through those notifications, not through its output), or the helpers hold the run for 4 hours in total, they are interrupted and the run ends `done` with its own text plus "Some helpers were abandoned after N minutes without activity" | | Antigravity | refused (`shared_unsupported_for_provider`); it cannot be held to read-only | allowed; its native `start_subagent` is only observed | The Mousse MCP server also refuses a read-only child every tool that is not a `reads` tool or marked `readOnlySafe` (`question_ask` and the delegation tools `delegate_task`, `continue_subagent`, `request_answer`, `task_status`, `task_cancel`, `list_subagents`), with `mousse_read_only`: no queue-changing tool (`queue_enqueue`, `queue_update`), `thread_fork`, `thread_merge_back` or task tools. Runtimes that decide per tool name use `READ_ONLY_MOUSSE_TOOLS`, which a test keeps equal to that set. @@ -542,7 +542,7 @@ Every runtime except the pi loop reaches Mousse's own tools through one MCP serv - `src/mms/run/mcp/MousseMcpServer.ts`: one in-process MCP server (streamable HTTP on `127.0.0.1`, random port, plus a stdio bridge binary `mousse-cli engine mcp-stdio` for runtimes that only speak stdio). Tools are the app-tool registry (`src/mms/run/mcp/appTools/`), the same modules the pi loop calls in process: the delegation tools, `question_ask`, `task_create`/`task_update`/`task_list`, `thread_read`, `thread_list`, `thread_search`, `thread_send`, `thread_wait`, `thread_interrupt`, `thread_steer`, `request_answer`, `thread_create`, `queue_*`, `thread_fork`, `thread_merge_back`, the browser tools, `publish_applet`/`read_applet`, `skill_list`/`skill_load`, quick actions, `web_search`/`web_fetch`, and the dev GUI tools in development builds. A tool's old name (`src/shared/toolAliases.ts`) is not listed but still calls it: the registry renames the call before any check, so it has the new name's checks, ledger row and receipts. The list a credential sees depends on its thread (its read-only status, never the turn's mode); what a turn may use (mode, Settings, browser binding) is checked at call time and refused with a tool error that says why. The browser rights are separate: `browser.read` (observe, find, extract, screenshot, wait, listing tabs) and `browser.act` (open, act, opening, switching or closing a tab); a read-only turn lacks `browser.act` and is refused `lacks_capability` with `lacks: browser.act`. - **Monitors and background work** (#1605): `monitor_start`, `background_run`, `background_stop` and `keep_working` (`src/mms/run/mcp/appTools/monitors.ts`, service `src/mms/monitors/ThreadMonitors.ts`). A monitor is a script in the thread's folder whose printed lines are events, or a `wss://` socket (`ws://` only on loopback) whose messages are events; events within `MONITOR_BATCH_MS` reach the thread as one `reconcile_notice` row with payload `monitor_event`, delivered as an agent note is (an idle thread wakes, a native-steer run takes a steer, an interrupt_restart runtime gets it at the next Mousse tool boundary; held while the thread waits on a person's answer). Its output is quoted. A monitor past `MONITOR_CHATTY_EVENTS_PER_MINUTE` or `MONITOR_CHATTY_NOTICES_PER_MINUTE` is stopped. Scripts run with the daemon's environment plus the login shell's PATH (`sharedShellPath`), as the agent's own commands find programs; on macOS and Linux a script's whole process group ends with it, whatever ended it. Every end is told once: exit with its code, socket close with code and reason, failure, timeout, over-chatty, thread Stop (`attention: none`, told with the next turn; a deleted thread is told nothing) and daemon restart (a per-profile record, `monitors.json`, is read on start and its leftovers told after ready). The agent's own `background_stop` posts no notice: its result carries what was not reported yet. While any run, the thread shows Working (`ThreadRuntimeManager.setPendingMonitors`) and is not unloaded; `running_list` and the Running now note list them. Starting a script needs the shell's authority (`editsWorkspace`; asks unless Full access; refused in a read-only mode and in read-only subagents). No cap on how many run at once. - **Credentials**: `credentials(token_hash PK, thread_id, run_id, provider, mode_ceiling, access_level, created_at, revoked_at, last_seen_at)` (migration 4, `access_level` added by migration 11; `mode_ceiling` is retired and always `default`; durable rather than in-memory; only the SHA-256 of a token is stored). A credential belongs to one runtime epoch of (thread, instance) and acts for one run; a call never re-points it. The epoch and turn binding live in daemon memory with the token. The harness gateway gives each turn its bearer (`bearerForTurn`, PR 2 §2 `TurnBridge`) under the driver's turn binding: `per_call` runtimes (Claude Code: Mousse's `PreToolUse` hook stamps `_mousse_turn` into each Mousse call; the Pi RPC engine tags calls by attempt) keep their bearer, re-bound to each run, and the registry refuses a call without the active turn's token; `drained_turn` runtimes (Codex, ACP agents) keep it only after a turn proven drained (completed, no call still running, and the runtime's account of its Mousse calls matching what the server received), and rotate otherwise; `rotate` gets a new bearer every turn. A new runtime (driver, home, account generation or config revision), a failed turn or a rotation bumps the epoch, which revokes the epoch's credentials at once. A bumped epoch does not always mean a new process: Claude Code keeps its process and gets the new bearer over its input (`setMcpServers`, §15.2, #358) when only the bearer changed (a failed turn's bump), since it reconnects a server whose header changed; it starts a new process when the tool list changes with it (the caller's policy, `McpAttachment.tools`), because Claude Code adds the tools a reconnected server gains but keeps offering the model the ones it lost (checked with the real CLI). A call needs a live credential of the current epoch whose run is the thread's active run and has a registered app-tool turn; otherwise it fails `mousse_caller_not_live` before any handler runs. A turn's end settles the bearer, then aborts its in-flight calls. Revoked when the thread is deleted or trashed, when the thread switches provider, and after 24 h idle (`pruneExpired`, once per reconciler pass). Tokens live in daemon memory only, so boot (`reconcileOnBoot`, §12.5 step 3) revokes every live stored row and the next turn mints a fresh token. -- **Scoping**: a token acts as its run. The delegation rules (a read-only caller starts only read-only children, and a read-only child runs on this computer only) live in the orchestrator's shared delegation dispatch, so the in-process pi loop and the MCP server refuse the same calls the same way. Agents read and manage every thread (below). The **lineage view** of a thread is its `run.*`, `attempt.*`, `command.ignored`, `restart.note` and `thread.lineage` events plus `delegation.status` rows whose `result` is redacted unless the delegation's child is the caller (inbox, effect and tool events carry prompts and sibling results). `writesThreads` and `editsWorkspace` tools act on the caller's own thread only. +- **Scoping**: a token acts as its run. The delegation rules (a read-only caller starts only read-only children, and a read-only child on another computer runs read-only there, in its own project folder; a computer from before `threads.create` version 7 is refused with `update_needed`) live in the orchestrator's shared delegation dispatch, so the in-process pi loop and the MCP server refuse the same calls the same way. Agents read and manage every thread (below). The **lineage view** of a thread is its `run.*`, `attempt.*`, `command.ignored`, `restart.note` and `thread.lineage` events plus `delegation.status` rows whose `result` is redacted unless the delegation's child is the caller (inbox, effect and tool events carry prompts and sibling results). `writesThreads` and `editsWorkspace` tools act on the caller's own thread only. - Every tool handler declares an access class (`reads | readsOthers | actsAsCaller | writesThreads | startsThreads | managesOthers`); a handler without one fails to register. - **Agent access** (no setting): every agent reads and manages every thread, in every project, whatever started its turn (a person, a subagent's delegation, a bot, a launched or forked thread, a remote send, a scheduled run or a continuation). The per-profile level (`lineage`, `read`, `manage`), its project list and the per-thread caps older versions had are gone. New credentials, delegations and runs record `manage` in the columns kept for them (`credentials.access_level`, `delegations.access_level`, `runs.access_grant`); stored values from older versions, a stored `Thread.accessCeiling` and a job's `accessCap` are ignored, and a settings patch carrying the old `level`, `projectIds` or `projectsUnreadable` is accepted and dropped. What still holds is independent of it: permission modes and approvals, read-only subagents (only `reads`, `readsOthers` and `readOnlySafe` tools), the private-thread line (below), a bot thread's own dispatch rule (`bot_thread_read_only`) and its grant, and the limits on `thread_create`, `thread_send`, scheduled jobs and agent messages in a row. - **A bot's threads.** A personal bot works in two lanes. Its chat lane runs on its home thread (`bot:`): each message the owner sends with `bots.chat.send` starts a chat activation, whose first dispatch clears the thread's conversation context (`clearConversationContext`; the transcript stays), so the turn gets a context pack built from `bots.db` instead of the thread's history. Its run lane runs each scheduled or manual run on a child thread of its own (`bot::act:`). Only the bot's own dispatches start turns on these threads: an ordinary send, queued message, steer or Continue is refused (`bot_thread_read_only`). See `docs/bots.md`. diff --git a/tests/contractVersions.test.ts b/tests/contractVersions.test.ts index 47520f661c..a0634981b3 100644 --- a/tests/contractVersions.test.ts +++ b/tests/contractVersions.test.ts @@ -10,7 +10,7 @@ describe('contract versions', () => { daemonOwnerLease: 1, netProtocol: '1.0', netEnvelope: 1, - runDatabase: 32, + runDatabase: 33, botsDb: 17, mousseConf: 1, installation: 2, diff --git a/tests/run/botThreadParity.test.ts b/tests/run/botThreadParity.test.ts index b88fb605c3..afc5d2ef26 100644 --- a/tests/run/botThreadParity.test.ts +++ b/tests/run/botThreadParity.test.ts @@ -243,13 +243,16 @@ describe('a bot uses what a thread\'s agent can', () => { it('reaches a computer that lets agents work there, and what it reads there is framed as data', async () => { const remoteThreads = { - computerFor: vi.fn(() => ({ ok: true as const, node: 'n2', computer: 'Mac mini', online: true })), + computerFor: vi.fn(() => ({ ok: true as const, node: 'n2', computer: 'Mac mini', status: 'online' as const })), list: vi.fn(async () => ({ threads: [{ id: 'n2:t1', name: 'Build', updatedAt: NOW, startedAt: NOW }], projects: [], activity: {} })) } as unknown as RemoteThreadsHost const { call } = await world({ host: { remoteThreads } }) const listed = await call('thread_list', { computer: 'Mac mini' }) expect(listed.error).toBe(false) expect(listed.text).toMatch(/^\[The threads on Mac mini\. Data from your owner's threads/) + // The remote thread itself is listed, under its qualified id. + expect(listed.text).toContain('n2:t1') + expect(listed.text).toContain('Build') expect(remoteThreads.computerFor).toHaveBeenCalledWith('work', 'Mac mini') }) diff --git a/tests/run/codexAdapter.test.ts b/tests/run/codexAdapter.test.ts index 29d50b5c69..5540f988a1 100644 --- a/tests/run/codexAdapter.test.ts +++ b/tests/run/codexAdapter.test.ts @@ -860,8 +860,11 @@ describe('CodexAdapter', () => { it('runs a read-only child read-only and declines approvals without asking', async () => { const replay = transport([ ...handshake(), - ...threadStart(4, threadParams({ approvalPolicy: 'on-request', sandbox: 'read-only' })), - ...turnStart(5, 'check it', 'turn-1', { approvalPolicy: 'on-request', sandboxPolicy: { type: 'readOnly', networkAccess: false } }), + // The user's own MCP servers are read first, to switch them off for the read-only child (none here). + out({ id: 4, method: 'config/read', params: { includeLayers: true } }), + inn({ id: 4, result: { config: { mcp_servers: {} }, layers: [] } }), + ...threadStart(5, threadParams({ approvalPolicy: 'on-request', sandbox: 'read-only' })), + ...turnStart(6, 'check it', 'turn-1', { approvalPolicy: 'on-request', sandboxPolicy: { type: 'readOnly', networkAccess: false } }), inn({ id: 'srv-1', method: 'item/commandExecution/requestApproval', params: { kind: 'command', threadId: T, turnId: 'turn-1', itemId: 'cmd-1', startedAtMs: 0, environmentId: null, command: 'touch x' } }), out({ id: 'srv-1', result: { decision: 'decline' } }), ...message('msg-1', ['Checked']),