The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off - a plain Node server streams the fake OpenCode SSE feed. |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | import * as NodeHttp from "node:http"; |
| 4 | import type * as NodeNet from "node:net"; |
| 5 | import * as NodePath from "node:path"; |
| 6 | |
| 7 | import { expect } from "e2e"; |
| 8 | |
| 9 | import { T3_HOME } from "../e2e.config.ts"; |
| 10 | import { makeState, runPromise, test, WEB } from "./t3.ts"; |
| 11 | |
| 12 | // An OpenCode 1.19.5 server with background subagents on (OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS, |
| 13 | // which T3 never sets). Frames follow v1.19.5 source: tool/task.ts injects a finished background |
| 14 | // task's result as a synthetic prompt on the parent session; session/prompt.ts saves it, then |
| 15 | // reports busy; session/status.ts reports idle, then session.idle. |
| 16 | const ROOT = "ses_scenario_root"; |
| 17 | const CHILD = "ses_scenario_child"; |
| 18 | const MODEL = { providerID: "openai", modelID: "gpt-5.4-mini" }; |
| 19 | const WAKE_TEXT = "The background task returned CHILD_OK."; |
| 20 | |
| 21 | function fakeOpenCode() { |
| 22 | const streams = new Set<NodeHttp.ServerResponse>(); |
| 23 | let sequence = 0; |
| 24 | const emit = (type: string, properties: unknown) => { |
| 25 | const frame = `data: ${JSON.stringify({ id: `evt_${++sequence}`, type, properties })}\n\n`; |
| 26 | for (const stream of streams) stream.write(frame); |
| 27 | }; |
| 28 | const session = (id: string) => ({ |
| 29 | id, |
| 30 | slug: id, |
| 31 | version: "1.19.5", |
| 32 | projectID: "global", |
| 33 | directory: "/tmp", |
| 34 | title: id, |
| 35 | time: { created: 1, updated: 1 }, |
| 36 | ...(id === CHILD ? { parentID: ROOT } : {}), |
| 37 | }); |
| 38 | const user = (sessionID: string, id: string) => |
| 39 | emit("message.updated", { |
| 40 | sessionID, |
| 41 | info: { id, sessionID, role: "user", time: { created: 1 }, agent: "build", model: MODEL }, |
| 42 | }); |
| 43 | const answer = (sessionID: string, id: string, parentID: string, text: string) => { |
| 44 | const info = { |
| 45 | id, |
| 46 | parentID, |
| 47 | sessionID, |
| 48 | role: "assistant", |
| 49 | mode: "build", |
| 50 | agent: "build", |
| 51 | path: { cwd: "/tmp", root: "/" }, |
| 52 | cost: 0, |
| 53 | tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 54 | ...MODEL, |
| 55 | }; |
| 56 | emit("message.updated", { sessionID, info: { ...info, time: { created: 2 } } }); |
| 57 | emit("message.part.updated", { |
| 58 | sessionID, |
| 59 | part: { id: `prt_${id}`, messageID: id, sessionID, type: "text", text }, |
| 60 | time: 2, |
| 61 | }); |
| 62 | emit("message.updated", { |
| 63 | sessionID, |
| 64 | info: { ...info, time: { created: 2, completed: 3 }, finish: "stop" }, |
| 65 | }); |
| 66 | }; |
| 67 | const status = (sessionID: string, type: "busy" | "idle") => { |
| 68 | emit("session.status", { sessionID, status: { type } }); |
| 69 | if (type === "idle") emit("session.idle", { sessionID }); |
| 70 | }; |
| 71 | const task = (state: "running" | "completed") => |
| 72 | emit("message.part.updated", { |
| 73 | sessionID: ROOT, |
| 74 | part: { |
| 75 | id: "prt_task", |
| 76 | messageID: "msg_tool_step", |
| 77 | sessionID: ROOT, |
| 78 | type: "tool", |
| 79 | tool: "task", |
| 80 | callID: "call_task", |
| 81 | state: { |
| 82 | status: state, |
| 83 | title: "Return CHILD_OK", |
| 84 | input: { |
| 85 | description: "Return CHILD_OK", |
| 86 | prompt: "Respond exactly CHILD_OK.", |
| 87 | subagent_type: "general", |
| 88 | background: true, |
| 89 | }, |
| 90 | metadata: { sessionId: CHILD, background: true, jobId: CHILD }, |
| 91 | time: { start: 1, ...(state === "completed" ? { end: 2 } : {}) }, |
| 92 | ...(state === "completed" ? { output: "Background task started" } : {}), |
| 93 | }, |
| 94 | }, |
| 95 | time: 1, |
| 96 | }); |
| 97 | |
| 98 | // The foreground turn launches the task in the background and settles before it finishes. |
| 99 | const runPrompt = (messageID: string) => { |
| 100 | user(ROOT, messageID); |
| 101 | status(ROOT, "busy"); |
| 102 | emit("session.created", { sessionID: CHILD, info: session(CHILD) }); |
| 103 | task("running"); |
| 104 | user(CHILD, "msg_child_prompt"); |
| 105 | task("completed"); |
| 106 | emit("session.status", { sessionID: CHILD, status: { type: "busy" } }); |
| 107 | answer(ROOT, "msg_root_answer", messageID, "PARENT_OK"); |
| 108 | status(ROOT, "idle"); |
| 109 | }; |
| 110 | const finishBackgroundTask = () => { |
| 111 | answer(CHILD, "msg_child_answer", "msg_child_prompt", "CHILD_OK"); |
| 112 | status(CHILD, "idle"); |
| 113 | user(ROOT, "msg_background_result"); |
| 114 | emit("message.part.updated", { |
| 115 | sessionID: ROOT, |
| 116 | part: { |
| 117 | id: "prt_background_result", |
| 118 | messageID: "msg_background_result", |
| 119 | sessionID: ROOT, |
| 120 | type: "text", |
| 121 | synthetic: true, |
| 122 | text: `<task id="${CHILD}" state="completed">\n<summary>Background task completed: Return CHILD_OK</summary>\n<task_result>\nCHILD_OK\n</task_result>\n</task>`, |
| 123 | }, |
| 124 | time: 3, |
| 125 | }); |
| 126 | status(ROOT, "busy"); |
| 127 | answer(ROOT, "msg_wake_answer", "msg_background_result", WAKE_TEXT); |
| 128 | status(ROOT, "idle"); |
| 129 | }; |
| 130 | |
| 131 | const server = NodeHttp.createServer(async (request, response) => { |
| 132 | const path = new URL(request.url ?? "/", "http://opencode").pathname; |
| 133 | let body = ""; |
| 134 | for await (const chunk of request) body += chunk; |
| 135 | const json = (value: unknown) => { |
| 136 | response.writeHead(200, { "content-type": "application/json" }); |
| 137 | response.end(JSON.stringify(value)); |
| 138 | }; |
| 139 | if (path === "/event") { |
| 140 | response.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); |
| 141 | response.write(`data: ${JSON.stringify({ type: "server.connected", properties: {} })}\n\n`); |
| 142 | streams.add(response); |
| 143 | request.on("close", () => streams.delete(response)); |
| 144 | return; |
| 145 | } |
| 146 | if (path === "/api/info") { |
| 147 | response.writeHead(200, { "content-type": "text/html" }); |
| 148 | return response.end("<!doctype html>"); |
| 149 | } |
| 150 | if (path === "/global/health") return json({ healthy: true, version: "1.19.5" }); |
| 151 | if (path === "/provider") { |
| 152 | const model = { id: MODEL.modelID, name: "GPT-5.4 mini", release_date: "2026-01-01" }; |
| 153 | return json({ |
| 154 | all: [{ id: MODEL.providerID, name: "OpenAI", models: { [model.id]: model } }], |
| 155 | default: { [MODEL.providerID]: model.id }, |
| 156 | connected: [MODEL.providerID], |
| 157 | }); |
| 158 | } |
| 159 | if (path === "/agent") return json([{ name: "build", mode: "primary" }]); |
| 160 | if (path === "/command" || path === "/skill") return json([]); |
| 161 | if (path === "/session/status") return json({}); |
| 162 | if (path === "/session" && request.method === "POST") return json(session(ROOT)); |
| 163 | const match = /^\/session\/([^/]+)(?:\/([a-z_]+))?$/.exec(path); |
| 164 | if (match !== null) { |
| 165 | const [, id = ROOT, action] = match; |
| 166 | if (action === undefined) return json(session(id)); |
| 167 | if (action === "children") return json([]); |
| 168 | if (action === "abort") return json(true); |
| 169 | if (action === "prompt_async") { |
| 170 | response.writeHead(204).end(); |
| 171 | return runPrompt(JSON.parse(body).messageID); |
| 172 | } |
| 173 | } |
| 174 | response.writeHead(404, { "content-type": "application/json" }); |
| 175 | response.end("{}"); |
| 176 | }); |
| 177 | return { server, finishBackgroundTask }; |
| 178 | } |
| 179 | |
| 180 | test( |
| 181 | "An OpenCode 1.x background task's result wakes its settled thread", |
| 182 | WEB, |
| 183 | async ({ t3, app, screen }) => { |
| 184 | const opencode = fakeOpenCode(); |
| 185 | await new Promise<void>((resolve) => opencode.server.listen(0, "127.0.0.1", resolve)); |
| 186 | try { |
| 187 | const { port } = opencode.server.address() as NodeNet.AddressInfo; |
| 188 | const settingsPath = NodePath.join(T3_HOME, "userdata/settings.json"); |
| 189 | const settings = JSON.parse(await NodeFSP.readFile(settingsPath, "utf8")); |
| 190 | settings.providerInstances.opencode = { |
| 191 | driver: "opencode", |
| 192 | enabled: true, |
| 193 | config: { serverUrl: `http://127.0.0.1:${port}` }, |
| 194 | }; |
| 195 | await NodeFSP.writeFile(settingsPath, JSON.stringify(settings)); |
| 196 | |
| 197 | const project = await t3.project("demo"); |
| 198 | const { cli } = makeState(T3_HOME); |
| 199 | const start = [ |
| 200 | "thread", |
| 201 | "start", |
| 202 | project, |
| 203 | "Delegate a background task, then reply PARENT_OK.", |
| 204 | "--title", |
| 205 | "Background wake", |
| 206 | "--provider", |
| 207 | "opencode", |
| 208 | "--model", |
| 209 | `${MODEL.providerID}/${MODEL.modelID}`, |
| 210 | "--json", |
| 211 | ]; |
| 212 | // The settings watcher enables OpenCode a moment after the write. |
| 213 | let started = ""; |
| 214 | await expect |
| 215 | .poll( |
| 216 | async () => { |
| 217 | const result = await runPromise(cli(start, [0, 1])).catch(() => undefined); |
| 218 | if (result?.stdout.includes("threadId")) started = result.stdout; |
| 219 | return started.length > 0; |
| 220 | }, |
| 221 | { timeout: 60_000 }, |
| 222 | ) |
| 223 | .toBe(true); |
| 224 | const { threadId } = JSON.parse(started) as { threadId: string }; |
| 225 | await t3.open({ threadId }); |
| 226 | |
| 227 | await expect(screen.getByText("PARENT_OK")).toBeVisible({ timeout: 60_000 }); |
| 228 | opencode.finishBackgroundTask(); |
| 229 | await expect(screen.getByText(WAKE_TEXT)).toBeVisible({ timeout: 30_000 }); |
| 230 | // OpenCode's own turn is a second run, and it settles. |
| 231 | await expect |
| 232 | .poll( |
| 233 | async () => { |
| 234 | const shown = await runPromise(cli(["thread", "show", threadId, "--json"])); |
| 235 | const { thread } = JSON.parse(shown.stdout) as { |
| 236 | thread: { runCount: number; status: string }; |
| 237 | }; |
| 238 | return `${thread.runCount} runs, ${thread.status}`; |
| 239 | }, |
| 240 | { timeout: 30_000 }, |
| 241 | ) |
| 242 | .toBe("2 runs, completed"); |
| 243 | await app.screenshot("background-result"); |
| 244 | } finally { |
| 245 | opencode.server.closeAllConnections(); |
| 246 | opencode.server.close(); |
| 247 | } |
| 248 | }, |
| 249 | ); |