diff --git a/apps/server/scripts/dev-replay-agent.ts b/apps/server/scripts/dev-replay-agent.ts index e3bb80f890833..2aa50e353f57a 100644 --- a/apps/server/scripts/dev-replay-agent.ts +++ b/apps/server/scripts/dev-replay-agent.ts @@ -1,5 +1,5 @@ #!/usr/bin/env node -// @effect-diagnostics nodeBuiltinImport:off globalTimers:off globalFetch:off +// @effect-diagnostics nodeBuiltinImport:off globalTimers:off /** * A free ACP agent for dev servers. It plays recorded ACP transcripts with simulated * streaming, so the real adapter, orchestration, and UI run without a paid model. @@ -69,16 +69,6 @@ export const SCENARIOS = [ name: "Command output", file: "./dev-replay/command_output_transcript.ndjson", }, - { - id: "delegate-remote", - name: "Delegate to a linked environment", - file: "./dev-replay/delegate_remote_transcript.ndjson", - }, - { - id: "remote-child", - name: "E2E suite run", - file: "./dev-replay/remote_child_transcript.ndjson", - }, ] as const; // Session setup and vendor state stay out: they would replace the replay agent's own models. @@ -94,11 +84,8 @@ const REPLAYED_UPDATES = new Set([ "usage_update", ]); -const MCP_CALL = "t3/mcp_call"; - // Only requests that ask the user. A replayed terminal/create would run a real command. -// `t3/mcp_call` is this agent's own: it calls a T3 tool over the session's MCP server. -const REPLAYED_REQUESTS = new Set(["session/request_permission", "elicitation/create", MCP_CALL]); +const REPLAYED_REQUESTS = new Set(["session/request_permission", "elicitation/create"]); const CHUNK_DELAY_MS = 30; const FRAME_DELAY_MS = 250; @@ -155,35 +142,12 @@ export function streamChunks(text: string): ReadonlyArray { return chunks.length === 0 ? [text] : chunks; } -/** - * Values a recording can name as ``, read per turn from the JSON file at - * T3_DEV_REPLAY_VARS, for ids that change every run such as another environment's. - */ -function readReplayVars(): Record { - const file = process.env.T3_DEV_REPLAY_VARS; - if (file === undefined || file === "") return {}; - try { - return JSON.parse(NodeFS.readFileSync(file, "utf8")) as Record; - } catch { - return {}; - } -} - -/** Fills the live session, workspace, vars, and per-prompt tool ids, so repeated turns add rows. */ +/** Fills the live session, workspace, and per-prompt tool ids, so repeated turns add rows. */ function materialize( value: unknown, - live: { - readonly sessionId: string; - readonly workspace: string; - readonly turnKey: string; - readonly vars: Record; - }, + live: { readonly sessionId: string; readonly workspace: string; readonly turnKey: string }, ): unknown { - if (typeof value === "string") { - return value - .replaceAll("", live.workspace) - .replace(//g, (match, name: string) => live.vars[name] ?? match); - } + if (typeof value === "string") return value.replaceAll("", live.workspace); if (Array.isArray(value)) return value.map((entry) => materialize(entry, live)); if (typeof value !== "object" || value === null) return value; return Object.fromEntries( @@ -212,58 +176,6 @@ function permissionRejectedToolCallId(request: LogicalFrame, answer: unknown): s return rejected ? params.toolCall?.toolCallId : undefined; } -interface McpServer { - readonly endpoint: string; - readonly authorization: string; -} - -/** The T3 MCP server T3 hands the agent in session/new, through the stdio bridge's env. */ -function t3McpServer(servers: unknown): McpServer | undefined { - if (!Array.isArray(servers)) return undefined; - for (const server of servers as ReadonlyArray<{ - env?: ReadonlyArray<{ name: string; value: string }>; - }>) { - const env = new Map((server.env ?? []).map((entry) => [entry.name, entry.value])); - const endpoint = env.get("T3_ACP_MCP_ENDPOINT"); - const authorization = env.get("T3_ACP_MCP_AUTHORIZATION"); - if (endpoint !== undefined && authorization !== undefined) return { endpoint, authorization }; - } - return undefined; -} - -/** One MCP tools/call over streamable HTTP: initialize, initialized, then the call. */ -async function callT3Tool(server: McpServer, name: string, args: unknown): Promise { - let sessionHeader: string | undefined; - const post = async (body: object) => { - const response = await fetch(server.endpoint, { - method: "POST", - headers: { - authorization: server.authorization, - accept: "application/json, text/event-stream", - "content-type": "application/json", - ...(sessionHeader === undefined ? {} : { "mcp-session-id": sessionHeader }), - }, - body: JSON.stringify({ jsonrpc: "2.0", ...body }), - }); - sessionHeader = response.headers.get("mcp-session-id") ?? sessionHeader; - const text = await response.text(); - // A streamed answer carries the JSON-RPC message on a `data:` line. - const data = text.split("\n").find((line) => line.startsWith("data:")); - return text.length === 0 ? undefined : JSON.parse(data === undefined ? text : data.slice(5)); - }; - await post({ - id: 1, - method: "initialize", - params: { - protocolVersion: "2025-06-18", - capabilities: {}, - clientInfo: { name: "t3-dev-replay", version: "1" }, - }, - }); - await post({ method: "notifications/initialized" }); - return post({ id: 2, method: "tools/call", params: { name, arguments: args } }); -} - function runAgent(): void { const turnsByScenario = new Map>(); const loadTurns = (scenarioId: string) => { @@ -279,13 +191,7 @@ function runAgent(): void { const sessions = new Map< string, - { - cwd: string; - model: string; - promptCount: number; - cancel: (() => void) | undefined; - mcp: McpServer | undefined; - } + { cwd: string; model: string; promptCount: number; cancel: (() => void) | undefined } >(); const pendingAgentRequests = new Map void>(); let nextAgentRequestId = 1; @@ -316,33 +222,9 @@ function runAgent(): void { ): Promise<"cancelled" | undefined> => { const session = sessions.get(sessionId)!; // Random, so a session loaded after a restart cannot reuse earlier tool rows. - const live = { - sessionId, - workspace: session.cwd, - turnKey: NodeCrypto.randomUUID(), - vars: readReplayVars(), - }; + const live = { sessionId, workspace: session.cwd, turnKey: NodeCrypto.randomUUID() }; for (const recorded of turn.frames) { const frame = materialize(recorded.frame, live) as LogicalFrame; - if (frame.kind === "request" && frame.method === MCP_CALL) { - await sleep(recorded.afterMs ?? FRAME_DELAY_MS, signal); - if (signal.aborted) return; - const call = frame.params as { name: string; arguments?: unknown }; - if (session.mcp === undefined) { - process.stderr.write(`dev replay agent has no T3 MCP server for ${call.name}\n`); - continue; - } - const result = await callT3Tool(session.mcp, call.name, call.arguments ?? {}).catch( - (error: unknown) => ({ error: String(error) }), - ); - const failed = result as { error?: unknown; result?: { isError?: boolean } } | undefined; - if (failed?.error !== undefined || failed?.result?.isError === true) { - process.stderr.write( - `dev replay agent: ${call.name} failed: ${JSON.stringify(result)}\n`, - ); - } - continue; - } if (frame.kind === "request") { await sleep(recorded.afterMs ?? FRAME_DELAY_MS, signal); if (signal.aborted) return; @@ -414,13 +296,7 @@ function runAgent(): void { const sessionId = message.method === "session/load" ? String(params.sessionId) : NodeCrypto.randomUUID(); const cwd = typeof params.cwd === "string" ? params.cwd : process.cwd(); - sessions.set(sessionId, { - cwd, - model: SCENARIOS[0].id, - promptCount: 0, - cancel: undefined, - mcp: t3McpServer(params.mcpServers), - }); + sessions.set(sessionId, { cwd, model: SCENARIOS[0].id, promptCount: 0, cancel: undefined }); return { ...(message.method === "session/new" ? { sessionId } : {}), models: { diff --git a/apps/server/scripts/dev-replay/delegate_remote_transcript.ndjson b/apps/server/scripts/dev-replay/delegate_remote_transcript.ndjson deleted file mode 100644 index 59903a2421a61..0000000000000 --- a/apps/server/scripts/dev-replay/delegate_remote_transcript.ndjson +++ /dev/null @@ -1,9 +0,0 @@ -{"type": "transcript_start", "provider": "acpRegistry", "protocol": "acp.ndjson-jsonrpc", "version": "1", "scenario": "dev_replay_delegate_remote", "metadata": {"generatedBy": "hand-written for the dev replay agent"}} -{"type": "expect_outbound", "frame": {"kind": "request", "method": "session/prompt", "params": ""}} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "The full suite takes a while on this laptop, so I'll run it on devbox."}}}}} -{"type": "emit_inbound", "frame": {"kind": "request", "method": "t3/mcp_call", "params": {"name": "delegate_task", "arguments": {"title": "E2E suite", "task": "Run the e2e suite on main and report each failing spec with its error.", "target": {"environmentId": "", "projectId": "", "model": "remote-child"}}}}, "afterMs": 600} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "\n\nStarted **E2E suite** on devbox. I'll tell you what fails when it finishes."}}}}, "afterMs": 400} -{"type": "emit_inbound", "frame": {"kind": "response", "method": "session/prompt", "result": {"stopReason": "end_turn"}}} -{"type": "expect_outbound", "frame": {"kind": "request", "method": "session/prompt", "params": ""}} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "The suite finished on devbox: 144 specs ran and 2 failed.\n\n- `checkout.spec.ts` applies a coupon: the 10% coupon is applied after tax, so it charges $20.00 instead of $18.00.\n- `avatar.spec.ts` crops on Safari: times out waiting for the crop preview.\n\nWant me to fix the coupon first?"}}}}, "afterMs": 400} -{"type": "emit_inbound", "frame": {"kind": "response", "method": "session/prompt", "result": {"stopReason": "end_turn"}}} diff --git a/apps/server/scripts/dev-replay/remote_child_transcript.ndjson b/apps/server/scripts/dev-replay/remote_child_transcript.ndjson deleted file mode 100644 index adae1d4ccee90..0000000000000 --- a/apps/server/scripts/dev-replay/remote_child_transcript.ndjson +++ /dev/null @@ -1,17 +0,0 @@ -{"type": "transcript_start", "provider": "acpRegistry", "protocol": "acp.ndjson-jsonrpc", "version": "1", "scenario": "dev_replay_remote_child", "metadata": {"generatedBy": "hand-written for the dev replay agent"}} -{"type": "expect_outbound", "frame": {"kind": "request", "method": "session/prompt", "params": ""}} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "Installing dependencies, then running all 144 specs on 8 workers."}}}}, "afterMs": 300} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call", "toolCallId": "install", "kind": "execute", "title": "pnpm install --frozen-lockfile", "status": "in_progress", "rawInput": {"command": "pnpm install --frozen-lockfile"}}}}, "afterMs": 600} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "install", "status": "completed", "rawOutput": "Packages: +1243\nDone in 18.4s\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call", "toolCallId": "build", "kind": "execute", "title": "pnpm build", "status": "in_progress", "rawInput": {"command": "pnpm build"}}}}, "afterMs": 400} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "build", "status": "completed", "rawOutput": "✓ built in 6.2s\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call", "toolCallId": "e2e", "kind": "execute", "title": "pnpm test:e2e", "status": "in_progress", "rawInput": {"command": "pnpm test:e2e"}}}}, "afterMs": 400} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "e2e", "status": "in_progress", "rawOutput": "Running 144 tests using 8 workers\n 24/144 done, 0 failed\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "e2e", "status": "in_progress", "rawOutput": "Running 144 tests using 8 workers\n 48/144 done, 1 failed\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "e2e", "status": "in_progress", "rawOutput": "Running 144 tests using 8 workers\n 72/144 done, 1 failed\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "e2e", "status": "in_progress", "rawOutput": "Running 144 tests using 8 workers\n 96/144 done, 1 failed\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "e2e", "status": "in_progress", "rawOutput": "Running 144 tests using 8 workers\n 120/144 done, 2 failed\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "e2e", "status": "in_progress", "rawOutput": "Running 144 tests using 8 workers\n 144/144 done, 2 failed\n"}}}, "afterMs": 2000} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "tool_call_update", "toolCallId": "e2e", "status": "completed", "rawOutput": "Running 144 tests using 8 workers\n ✘ checkout.spec.ts:42 › applies a coupon\n Expected: \"$18.00\"\n Received: \"$20.00\"\n ✘ avatar.spec.ts:17 › crops on Safari\n Test timeout of 30000ms exceeded.\n\n 2 failed\n 142 passed (1.9m)\n"}}}, "afterMs": 1500} -{"type": "emit_inbound", "frame": {"kind": "notification", "method": "session/update", "params": {"sessionId": "dev-replay-session", "update": {"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "\n\n144 specs ran and 2 failed:\n\n- `checkout.spec.ts` applies a coupon: expected **$18.00**, got **$20.00**. The 10% coupon is applied after tax instead of before.\n- `avatar.spec.ts` crops on Safari: timed out after 30s waiting for the crop preview.\n\nThe other 142 passed."}}}}, "afterMs": 1200} -{"type": "emit_inbound", "frame": {"kind": "response", "method": "session/prompt", "result": {"stopReason": "end_turn"}}} diff --git a/docs/operations/development.md b/docs/operations/development.md index 0492bab862e6c..2e22dd6d3649f 100644 --- a/docs/operations/development.md +++ b/docs/operations/development.md @@ -96,9 +96,7 @@ Dev servers add it back on start, so turn it off in Settings instead of removing To add a scenario, point a `SCENARIOS` entry at a generic ACP replay fixture (`registry_transcript.ndjson`) or a file in [dev-replay](../../apps/server/scripts/dev-replay). The agent replays session updates, permission requests, and questions. It drops session setup -and vendor frames, so Grok and fx recordings lose their vendor-only behavior. A `t3/mcp_call` -request frame (`{ name, arguments }`) makes the agent call that T3 tool itself over its MCP -session, and `` in any frame is filled from the JSON file at `T3_DEV_REPLAY_VARS`. +and vendor frames, so Grok and fx recordings lose their vendor-only behavior. ### Profiling dev startup