Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
refactor(daemon): derive busy-delivery/stdin caps from lifecycle (convergence #4)
Claudette#98731597f2a5b364
diff --git a/src/daemon/README.md b/src/daemon/README.mdindex e6e73b4cc..773c05f4b 100644--- a/src/daemon/README.md+++ b/src/daemon/README.md@@ -57,9 +57,15 @@ When a message arrives, what happens depends on the runtime's lifecycle:One long-lived process spans many turns. A new message is **written onto thestill-open input channel** — no restart. This is "steering."-- **`direct`** (codex, kimi, pi): write immediately; the runtime- tolerates injection any time.-- **`gated`** (claude): a raw write mid-stream could collide with an active+The busy-delivery mode is **derived from `lifecycle`** (`busyDeliveryModeOf` /+`supportsStdinNotificationOf` in `types.ts`) — `lifecycle.stdin` is the single+source of truth, so a driver's mode can't drift from it (it used to: a driver+could declare `lifecycle.stdin: "gated"` yet `busyDeliveryMode: "direct"`).+`persistent+direct → direct`, `persistent+gated → gated`, `per_turn → none`.++- **`direct`** (kimi, pi): write immediately; the runtime tolerates injection+ any time.+- **`gated`** (claude, codex): a raw write mid-stream could collide with an activesigned thinking block, so writes are **held until a safe boundary**,implemented by:- `apmStateMachine` (`src/runtime/apmStateMachine.ts`) — the policy reducers@@ -90,7 +96,7 @@ message and terminates the process on turn end.| Runtime | Lifecycle | Transport / protocol | Steering | Initial input | Output format ||---|---|---|---|---|---|| **claude** | persistent | child process, stream-json NDJSON | `gated` | `{type:"user",…}` line on stdin | stream-json |-| **codex** | persistent | child process, JSON-RPC 2.0 (`app-server --listen stdio://`) | `direct` (`turn/steer`) | `initialize` → `thread/start`/`resume` | JSON-RPC notifications |+| **codex** | persistent | child process, JSON-RPC 2.0 (`app-server --listen stdio://`) | `gated` | `initialize` → `thread/start`/`resume` | JSON-RPC notifications || **kimi** | persistent | child process, JSON-RPC "wire" | `direct` (`steer`) | `initialize` → `prompt` | JSON-RPC events || **pi** | persistent | in-process SDK (`@earendil-works/pi-coding-agent`), multi-provider | `direct` | `session.prompt()` | SDK event callback || **gemini** | per-turn | child process, stream-json | none | prompt on stdin, then close | stream-json |diff --git a/src/daemon/src/drivers/antigravity.ts b/src/daemon/src/drivers/antigravity.tsindex 76f9e2002..6db59714f 100644--- a/src/daemon/src/drivers/antigravity.ts+++ b/src/daemon/src/drivers/antigravity.ts@@ -49,8 +49,6 @@ export class AntigravityDriver implements Driver {toLaunchSpec: (_modelId: string) => ({ args: [] }),} as const;- readonly supportsStdinNotification = false;- readonly busyDeliveryMode = "none" as const;readonly capabilities = {reasoningEffort: false,diff --git a/src/daemon/src/drivers/claude.ts b/src/daemon/src/drivers/claude.tsindex 34a1dac23..08dc29d30 100644--- a/src/daemon/src/drivers/claude.ts+++ b/src/daemon/src/drivers/claude.ts@@ -25,8 +25,6 @@ export class ClaudeDriver implements Driver {toLaunchSpec: (modelId: string) => ({ args: ["--model", modelId] }),} as const;- readonly supportsStdinNotification = true;- readonly busyDeliveryMode = "gated" as const;readonly supportsNativeStandingPrompt = true;readonly capabilities = {diff --git a/src/daemon/src/drivers/codex.ts b/src/daemon/src/drivers/codex.tsindex 88bc6e1eb..88262f486 100644--- a/src/daemon/src/drivers/codex.ts+++ b/src/daemon/src/drivers/codex.ts@@ -55,8 +55,6 @@ export class CodexDriver implements Driver {toLaunchSpec: (modelId: string) => ({ params: { model: modelId } }),} as const;- readonly supportsStdinNotification = true;- readonly busyDeliveryMode = "gated" as const;readonly supportsNativeStandingPrompt = true;readonly capabilities = {diff --git a/src/daemon/src/drivers/copilot.ts b/src/daemon/src/drivers/copilot.tsindex 3597d50db..0bd6b2e1e 100644--- a/src/daemon/src/drivers/copilot.ts+++ b/src/daemon/src/drivers/copilot.ts@@ -26,8 +26,6 @@ export class CopilotDriver implements Driver {toLaunchSpec: (modelId: string) => ({ args: ["--model", modelId] }),} as const;- readonly supportsStdinNotification = false;- readonly busyDeliveryMode = "none" as const;readonly capabilities = {reasoningEffort: true,diff --git a/src/daemon/src/drivers/cursor.ts b/src/daemon/src/drivers/cursor.tsindex 8f463f4fe..19b26fa33 100644--- a/src/daemon/src/drivers/cursor.ts+++ b/src/daemon/src/drivers/cursor.ts@@ -26,8 +26,6 @@ export class CursorDriver implements Driver {toLaunchSpec: (modelId: string) => ({ args: ["--model", modelId] }),} as const;- readonly supportsStdinNotification = false;- readonly busyDeliveryMode = "none" as const;readonly capabilities = {reasoningEffort: false,diff --git a/src/daemon/src/drivers/gemini.ts b/src/daemon/src/drivers/gemini.tsindex d0b1a3f6b..526101904 100644--- a/src/daemon/src/drivers/gemini.ts+++ b/src/daemon/src/drivers/gemini.ts@@ -42,8 +42,6 @@ export class GeminiDriver implements Driver {modelId ? { args: ["--model", modelId] } : { args: [] },} as const;- readonly supportsStdinNotification = false;- readonly busyDeliveryMode = "none" as const;readonly capabilities = {reasoningEffort: false,diff --git a/src/daemon/src/drivers/kimi.ts b/src/daemon/src/drivers/kimi.tsindex 1405f68a8..271469881 100644--- a/src/daemon/src/drivers/kimi.ts+++ b/src/daemon/src/drivers/kimi.ts@@ -37,8 +37,6 @@ export class KimiDriver implements Driver {toLaunchSpec: (modelId: string) => ({ args: ["--model", modelId] }),} as const;- readonly supportsStdinNotification = true;- readonly busyDeliveryMode = "direct" as const;readonly capabilities = {reasoningEffort: false,diff --git a/src/daemon/src/drivers/opencode.ts b/src/daemon/src/drivers/opencode.tsindex 9893f43b7..d3fd595d4 100644--- a/src/daemon/src/drivers/opencode.ts+++ b/src/daemon/src/drivers/opencode.ts@@ -29,8 +29,6 @@ export class OpenCodeDriver implements Driver {toLaunchSpec: (modelId: string) => ({ args: ["--model", modelId] }),} as const;- readonly supportsStdinNotification = false;- readonly busyDeliveryMode = "none" as const;readonly terminateProcessOnTurnEnd = true;readonly deferSpawnUntilMessage = true;diff --git a/src/daemon/src/drivers/pi.ts b/src/daemon/src/drivers/pi.tsindex 8315bad8a..e49c5bdc1 100644--- a/src/daemon/src/drivers/pi.ts+++ b/src/daemon/src/drivers/pi.ts@@ -250,8 +250,6 @@ export class PiDriver implements Driver {toLaunchSpec: (modelId: string) => ({ params: { model: modelId } }),} as const;- readonly supportsStdinNotification = true;- readonly busyDeliveryMode = "direct" as const;readonly supportsNativeStandingPrompt = true;readonly capabilities = {diff --git a/src/daemon/src/manager/agentProfile.test.ts b/src/daemon/src/manager/agentProfile.test.tsindex 884277222..e93dbedb8 100644--- a/src/daemon/src/manager/agentProfile.test.ts+++ b/src/daemon/src/manager/agentProfile.test.ts@@ -18,9 +18,7 @@ function managerCapturingCtx(): {const mgr = new AgentProcessManager({driverFor: () =>({- lifecycle: { kind: "persistent" },- supportsStdinNotification: true,- busyDeliveryMode: "gated",+ lifecycle: { kind: "persistent", stdin: "gated", inFlightWake: "queue" },buildSystemPrompt: (config: { description?: string }) =>config.description ? `[system] ${config.description}` : "",}) as never,diff --git a/src/daemon/src/manager/managerCapabilityContract.test.ts b/src/daemon/src/manager/managerCapabilityContract.test.tsindex 65fa69337..5fedb9d02 100644--- a/src/daemon/src/manager/managerCapabilityContract.test.ts+++ b/src/daemon/src/manager/managerCapabilityContract.test.ts@@ -30,6 +30,7 @@ import { describe, it, expect } from "vitest";import { AgentProcessManager, type ManagedSession, type SessionFactory } from "./managerRuntime.js";import { listRuntimeIds, getDriver } from "../drivers/index.js";import type { Driver, LaunchContext } from "../types.js";+import { busyDeliveryModeOf, supportsStdinNotificationOf } from "../types.js";import type { Logger } from "../logger.js";interface CapabilityProfile {@@ -46,8 +47,10 @@ function profileOf(driver: Driver): CapabilityProfile {lifecycleKind: lifecycle.kind,stdin: lifecycle.kind === "persistent" ? lifecycle.stdin : undefined,inFlightWake: lifecycle.inFlightWake,- supportsStdinNotification: driver.supportsStdinNotification,- busyDeliveryMode: driver.busyDeliveryMode,+ // Derived from lifecycle (single source of truth) since #4 — the two fields+ // are no longer declared on Driver.+ supportsStdinNotification: supportsStdinNotificationOf(lifecycle),+ busyDeliveryMode: busyDeliveryModeOf(lifecycle),};}@@ -308,3 +311,38 @@ describe("AgentProcessManager capability contract — bucket sanity", () => {expect(directSteerBuckets.flatMap((b) => b.driverIds).sort()).toEqual(["kimi", "pi"]);});});++// #4 capability-unify equivalence table (executable). `busyDeliveryMode` and+// `supportsStdinNotification` are no longer declared on Driver — they derive+// from `lifecycle`. This pins that the DERIVED values equal the pre-unify+// literal declarations for EVERY registered driver, byte-for-byte. If a driver+// is added/changed such that the derivation would differ from its intended+// mode, this fails — which is the point: the derivation IS the contract now.+describe("AgentProcessManager capability contract — #4 lifecycle-derived equivalence", () => {+ // The authoritative pre-unify values (from daemon-capability-unify.md's table).+ const EXPECTED: Record<string, { busyDeliveryMode: "direct" | "gated" | "none"; supportsStdinNotification: boolean }> = {+ claude: { busyDeliveryMode: "gated", supportsStdinNotification: true },+ codex: { busyDeliveryMode: "gated", supportsStdinNotification: true },+ kimi: { busyDeliveryMode: "direct", supportsStdinNotification: true },+ pi: { busyDeliveryMode: "direct", supportsStdinNotification: true },+ gemini: { busyDeliveryMode: "none", supportsStdinNotification: false },+ copilot: { busyDeliveryMode: "none", supportsStdinNotification: false },+ cursor: { busyDeliveryMode: "none", supportsStdinNotification: false },+ opencode: { busyDeliveryMode: "none", supportsStdinNotification: false },+ antigravity: { busyDeliveryMode: "none", supportsStdinNotification: false },+ };++ it("every registered driver is in the equivalence table (guards against an unlisted new driver)", () => {+ expect(listRuntimeIds().slice().sort()).toEqual(Object.keys(EXPECTED).sort());+ });++ for (const id of listRuntimeIds()) {+ it(`${id}: derived busyDeliveryMode + supportsStdinNotification equal the pre-unify values`, () => {+ const lifecycle = getDriver(id).lifecycle;+ const expected = EXPECTED[id];+ expect(expected, `driver ${id} missing from EXPECTED table`).toBeDefined();+ expect(busyDeliveryModeOf(lifecycle)).toBe(expected!.busyDeliveryMode);+ expect(supportsStdinNotificationOf(lifecycle)).toBe(expected!.supportsStdinNotification);+ });+ }+});diff --git a/src/daemon/src/manager/managerRuntime.ts b/src/daemon/src/manager/managerRuntime.tsindex e5c08c4c8..14e8a5af7 100644--- a/src/daemon/src/manager/managerRuntime.ts+++ b/src/daemon/src/manager/managerRuntime.ts@@ -22,6 +22,7 @@ import {isActivelyWorking,} from "./managerPolicy.js";import type { Driver, LaunchContext, SdkDriverDeps } from "../types.js";+import { busyDeliveryModeOf, supportsStdinNotificationOf } from "../types.js";import { resolveLaunchFieldsOrDefault, type RuntimeConfig } from "../runtimeConfig.js";import { createChildProcessRuntimeSession, type ChildProcessRuntimeSession } from "../runtime/runtimeSession.js";import { SESSION_STOP_GRACE_MS } from "../runtime/killTree.js";@@ -590,8 +591,8 @@ export class AgentProcessManager {const driver = this.opts.driverFor(agentId, this.runtimeConfigs.get(agentId));const caps: AgentRuntimeCaps = {lifecycleKind: driver.lifecycle.kind,- supportsStdinNotification: driver.supportsStdinNotification,- busyDeliveryMode: driver.busyDeliveryMode,+ supportsStdinNotification: supportsStdinNotificationOf(driver.lifecycle),+ busyDeliveryMode: busyDeliveryModeOf(driver.lifecycle),};this.dispatch({ type: "register", agentId, caps });}diff --git a/src/daemon/src/manager/managerSwitchModel.test.ts b/src/daemon/src/manager/managerSwitchModel.test.tsindex 312e97c73..27d251440 100644--- a/src/daemon/src/manager/managerSwitchModel.test.ts+++ b/src/daemon/src/manager/managerSwitchModel.test.ts@@ -23,8 +23,6 @@ function fakeDriver(id: string): Driver {lifecycle: { kind: "per_turn", start: "immediate", exit: "natural", inFlightWake: "spawn_new" } as never,session: { recovery: "resume_or_fresh" } as never,model: { detectedModelsVerifiedAs: "launchable", toLaunchSpec: () => ({ args: [] }) } as never,- supportsStdinNotification: false,- busyDeliveryMode: "none",probe: () => ({ status: "healthy" as const, version: "test" }),spawn: async () => ({ process: {} as never }),parseLine: () => [],diff --git a/src/daemon/src/manager/managerTimeline.test.ts b/src/daemon/src/manager/managerTimeline.test.tsindex bf95b448d..79b53e77e 100644--- a/src/daemon/src/manager/managerTimeline.test.ts+++ b/src/daemon/src/manager/managerTimeline.test.ts@@ -18,7 +18,7 @@ function manager(rec: TimelineRecorder, capture: { ctx?: LaunchContext }) {const handlers: Record<string, ((arg?: unknown) => void)[]> = {};const mgr = new AgentProcessManager({driverFor: () =>- ({ lifecycle: { kind: "persistent" }, supportsStdinNotification: true, busyDeliveryMode: "gated" }) as never,+ ({ lifecycle: { kind: "persistent", stdin: "gated", inFlightWake: "queue" } }) as never,baseContextFor: (agentId) => ({ agentId, workingDirectory: "/tmp/x", standingPrompt: "", config: {} }),sessionFactory: ({ ctx }) => {capture.ctx = ctx;@@ -140,7 +140,7 @@ describe("manager.resetSession", () => {}> = [];const mgr = new AgentProcessManager({driverFor: () =>- ({ lifecycle: { kind: "persistent" }, supportsStdinNotification: true, busyDeliveryMode: "direct" }) as never,+ ({ lifecycle: { kind: "persistent", stdin: "direct", inFlightWake: "steer" } }) as never,baseContextFor: (agentId) => ({ agentId, workingDirectory: "/tmp/x", standingPrompt: "", config: {} }),sessionFactory: ({ ctx }) => {const entry: (typeof sessionsCreated)[number] = { ctx, handlers: {}, stopCalled: false, sends: [] };diff --git a/src/daemon/src/runtime/runtimeSession.ts b/src/daemon/src/runtime/runtimeSession.tsindex 88e0c1715..6e179980c 100644--- a/src/daemon/src/runtime/runtimeSession.ts+++ b/src/daemon/src/runtime/runtimeSession.ts@@ -12,6 +12,7 @@import { EventEmitter } from "events";import type { ChildProcess } from "child_process";import type { Driver, LaunchContext, StdinMode } from "../types.js";+import { busyDeliveryModeOf, supportsStdinNotificationOf } from "../types.js";import { killProcessTree, SESSION_STOP_GRACE_MS } from "./killTree.js";/**@@ -39,8 +40,10 @@ type DriverInFlightWake = "steer" | "queue" | "spawn_new" | "coalesce_into_pendiexport function descriptorFromDriver(driver: Driver): RuntimeSessionDescriptor {const lifecycle = driver.lifecycle.kind === "per_turn" ? "turn_based" : "persistent_stream";- const idle = driver.supportsStdinNotification ? "stdin" : "unsupported";- const busy = driver.supportsStdinNotification ? "stdin_steer" : "unsupported";+ // Derived from lifecycle (single source of truth) — see supportsStdinNotificationOf.+ const supportsStdin = supportsStdinNotificationOf(driver.lifecycle);+ const idle = supportsStdin ? "stdin" : "unsupported";+ const busy = supportsStdin ? "stdin_steer" : "unsupported";return {transport: "child_process",lifecycle,@@ -49,7 +52,7 @@ export function descriptorFromDriver(driver: Driver): RuntimeSessionDescriptor {turnBoundary: driver.lifecycle.kind === "per_turn" ? "process_exit" : "parsed_event",startPolicy: driver.lifecycle.kind === "per_turn" ? driver.lifecycle.start : "immediate",inFlightWake: driver.lifecycle.inFlightWake,- busyDelivery: driver.busyDeliveryMode,+ busyDelivery: busyDeliveryModeOf(driver.lifecycle),postTurn: driver.terminateProcessOnTurnEnd? "terminate_process": driver.endStdinOnTurnEnddiff --git a/src/daemon/src/runtime/sdkManagedSession.test.ts b/src/daemon/src/runtime/sdkManagedSession.test.tsindex 1e7cc297f..a269dc3a7 100644--- a/src/daemon/src/runtime/sdkManagedSession.test.ts+++ b/src/daemon/src/runtime/sdkManagedSession.test.ts@@ -48,8 +48,6 @@ function fakeSdkDriver(): { driver: Driver & { createSession: NonNullable<Driverlifecycle: { kind: "persistent", stdin: "direct", inFlightWake: "steer" } as const,session: { recovery: "resume_or_fresh" } as const,model: { detectedModelsVerifiedAs: "launchable", toLaunchSpec: () => ({ params: {} }) } as const,- supportsStdinNotification: true,- busyDeliveryMode: "direct" as const,probe: () => ({ status: "healthy" as const }),spawn: async () => {throw new Error("unsupported");@@ -220,8 +218,6 @@ describe("SdkManagedSession", () => {lifecycle: { kind: "persistent", stdin: "direct", inFlightWake: "steer" } as const,session: { recovery: "resume_or_fresh" } as const,model: { detectedModelsVerifiedAs: "launchable", toLaunchSpec: () => ({ params: {} }) } as const,- supportsStdinNotification: true,- busyDeliveryMode: "direct" as const,probe: () => ({ status: "healthy" as const }),spawn: async () => {throw new Error("unsupported");@@ -283,8 +279,6 @@ describe("SdkManagedSession", () => {lifecycle: { kind: "persistent", stdin: "direct", inFlightWake: "steer" } as const,session: { recovery: "resume_or_fresh" } as const,model: { detectedModelsVerifiedAs: "launchable", toLaunchSpec: () => ({ params: {} }) } as const,- supportsStdinNotification: true,- busyDeliveryMode: "direct" as const,probe: () => ({ status: "healthy" as const }),spawn: async () => {throw new Error("unsupported");@@ -358,8 +352,6 @@ describe("SdkManagedSession — start() does not block on the whole first turn",lifecycle: { kind: "persistent", stdin: "direct", inFlightWake: "steer" } as const,session: { recovery: "resume_or_fresh" } as const,model: { detectedModelsVerifiedAs: "launchable", toLaunchSpec: () => ({ params: {} }) } as const,- supportsStdinNotification: true,- busyDeliveryMode: "direct" as const,probe: () => ({ status: "healthy" as const }),spawn: async () => {throw new Error("unsupported");diff --git a/src/daemon/src/types.ts b/src/daemon/src/types.tsindex 232332dd7..05ca0f875 100644--- a/src/daemon/src/types.ts+++ b/src/daemon/src/types.ts@@ -86,6 +86,29 @@ export interface DriverModel {*/export type BusyDeliveryMode = "direct" | "gated" | "none";+/**+ * `busyDeliveryMode` and `supportsStdinNotification` are DERIVED from `lifecycle`+ * — `lifecycle` is the single source of truth for a driver's delivery model, so+ * the two facts can never drift from it (the pre-convergence duplicate `Driver`+ * fields could — e.g. codex once declared `busyDeliveryMode: "direct"` against a+ * `gated` lifecycle). Verified identity (not approximation) across all 9 drivers.+ *+ * These take `DriverLifecycle` (not the whole `Driver`) to stay a pure data+ * function with no `Driver`-interface cycle.+ *+ * If a future runtime genuinely needs a delivery mode its lifecycle doesn't+ * imply (e.g. persistent-but-poll), express it as a NEW `DriverLifecycle` shape+ * — the canonical source — not a side-channel override field. That keeps the+ * single-source-of-truth invariant this convergence (#4) established.+ */+export function busyDeliveryModeOf(lifecycle: DriverLifecycle): BusyDeliveryMode {+ return lifecycle.kind === "persistent" ? lifecycle.stdin : "none";+}++export function supportsStdinNotificationOf(lifecycle: DriverLifecycle): boolean {+ return lifecycle.kind === "persistent";+}+/*** Declarative description of which `RuntimeConfig` fields a driver actually* consumes at launch. Enables the platform to render accurate per-driver@@ -291,9 +314,10 @@ export interface Driver {readonly session: DriverSession;readonly model: DriverModel;- /** True if the runtime accepts mid-session input (steering / idle prompts). */- readonly supportsStdinNotification: boolean;- readonly busyDeliveryMode: BusyDeliveryMode;+ // `supportsStdinNotification` and `busyDeliveryMode` are NOT declared here —+ // they are derived from `lifecycle` via `supportsStdinNotificationOf` /+ // `busyDeliveryModeOf` (single source of truth; see those helpers).+/** True if the runtime takes the standing prompt natively (vs. inline). */readonly supportsNativeStandingPrompt?: boolean;