From 0b08d1cf620ffcf7ab2adf66bafdc242ad40625c Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Wed, 18 Nov 2026 14:25:47 +0100 Subject: [PATCH] test(cli-rs): push background wakes once the run says it waits The C.10 background scenarios pushed the wake events 300 ms after the session wait answered. In that window the client lists messages and jobs before it prints its wait notice. Under load the TS side ran past the window once (lane run suites-20261118-140807), printed the woken reply before background_wait, and background-subagent failed on the order of the two lines. P-CLI cases can now set late, which pushes events to the fake server's event stream once the client's stdout or stderr shows a given string. The three background scenarios use it on the wait notice, so the wake comes after the notice on both sides. --- packages/cli-rs/parity/cli/run.ts | 21 ++++++++++++++----- packages/cli-rs/parity/lib/fake-server.ts | 18 ++++++++-------- .../scenarios/C.10/background-json.cli.ts | 4 ++-- .../scenarios/C.10/background-subagent.cli.ts | 14 +++++++++---- .../parity/scenarios/C.10/background.cli.ts | 6 +++++- .../cli-rs/parity/scenarios/C.10/fixture.ts | 5 ----- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/packages/cli-rs/parity/cli/run.ts b/packages/cli-rs/parity/cli/run.ts index fe551b10cf8..d402f51b50c 100644 --- a/packages/cli-rs/parity/cli/run.ts +++ b/packages/cli-rs/parity/cli/run.ts @@ -48,6 +48,9 @@ export type CliCase = { masks?: { pattern: RegExp; replacement: string }[] // Sends `name` to the client once a request for `after` (a path) has reached the fake server. signal?: { after: string; name: NodeJS.Signals } + // Pushes `events` to the fake server's event stream once the client's stdout or stderr shows `after`, for + // events the client must see only after it has printed something (that it waits for a background job). + late?: { after: string; events: unknown[] } // P-FILE: files read back after the run into the record (`null` when missing), by path under the sandbox // root (`work/...` is the working directory, `home/.config/...` the XDG config). collect?: string[] @@ -87,11 +90,19 @@ export async function runCli(side: Side, value: CliCase) { proc.kill(signal.name) })() } - const [stdout, stderr, exit] = await Promise.all([ - new Response(proc.stdout).text(), - new Response(proc.stderr).text(), - proc.exited, - ]) + let late = value.late + const read = async (stream: ReadableStream) => { + const decoder = new TextDecoder() + let text = "" + for await (const chunk of stream) { + text += decoder.decode(chunk, { stream: true }) + if (!late || !text.includes(late.after)) continue + server?.push(late.events) + late = undefined + } + return text + decoder.decode() + } + const [stdout, stderr, exit] = await Promise.all([read(proc.stdout), read(proc.stderr), proc.exited]) await server?.stop() const files = value.collect ? Object.fromEntries( diff --git a/packages/cli-rs/parity/lib/fake-server.ts b/packages/cli-rs/parity/lib/fake-server.ts index 763e1cb873d..4b9aabce9bf 100644 --- a/packages/cli-rs/parity/lib/fake-server.ts +++ b/packages/cli-rs/parity/lib/fake-server.ts @@ -61,6 +61,12 @@ export async function startFakeServer(options: Options = {}) { const events = options.fallback === "tui" ? createEventStream() : undefined const fallback = events ? createFetch(undefined, events).fetch : undefined const streams = new Set>() + const push = (events: unknown[]) => + events.forEach((event) => { + if (event !== DROP) return streams.forEach((controller) => controller.enqueue(encode(event))) + streams.forEach((controller) => controller.close()) + streams.clear() + }) const server = Bun.serve({ hostname: "127.0.0.1", port: 0, @@ -80,15 +86,7 @@ export async function startFakeServer(options: Options = {}) { const response = respond(route, streams, entry, requests) if (route.then) { const events = route.then(entry, requests) - setTimeout( - () => - events.forEach((event) => { - if (event !== DROP) return streams.forEach((controller) => controller.enqueue(encode(event))) - streams.forEach((controller) => controller.close()) - streams.clear() - }), - route.thenDelay ?? 0, - ) + setTimeout(() => push(events), route.thenDelay ?? 0) } return response } @@ -104,6 +102,8 @@ export async function startFakeServer(options: Options = {}) { return { url: `http://127.0.0.1:${server.port}`, requests, + // Pushes events to every open `events` stream, as a route's `then` does. + push, // Pushes an event to the open `fallback: "tui"` stream. emit: (event: Parameters["emit"]>[0]) => events?.emit(event), async stop() { diff --git a/packages/cli-rs/parity/scenarios/C.10/background-json.cli.ts b/packages/cli-rs/parity/scenarios/C.10/background-json.cli.ts index f9b5c805ce6..7f226bddbc3 100644 --- a/packages/cli-rs/parity/scenarios/C.10/background-json.cli.ts +++ b/packages/cli-rs/parity/scenarios/C.10/background-json.cli.ts @@ -1,14 +1,14 @@ // `background` with `--format json`: a `background_wait` event lists the jobs. import { defineCli } from "../../cli/run" import { nowMask, server, textStep } from "./fixture" -import { job, jobs, old, woken } from "./background.cli" +import { job, jobs, old, waitingJson, woken } from "./background.cli" export default defineCli({ args: ["run", "--format", "json", "hello"], server: server({ events: () => textStep("Started the job."), routes: [jobs((count) => (count === 1 ? [old, job] : [old]))], - late: woken, }), + late: { after: waitingJson, events: woken }, masks: [nowMask], }) diff --git a/packages/cli-rs/parity/scenarios/C.10/background-subagent.cli.ts b/packages/cli-rs/parity/scenarios/C.10/background-subagent.cli.ts index f72e7e5e1aa..3d41b4bdd11 100644 --- a/packages/cli-rs/parity/scenarios/C.10/background-subagent.cli.ts +++ b/packages/cli-rs/parity/scenarios/C.10/background-subagent.cli.ts @@ -3,7 +3,7 @@ // turn succeeded (`noninteractive.test.ts`, "waits for a subagent's report and the parent turn it triggers"). import { defineCli } from "../../cli/run" import { event, nowMask, server, textStep } from "./fixture" -import { jobs } from "./background.cli" +import { jobs, waitingJson } from "./background.cli" const child = { kind: "subagent", @@ -21,12 +21,18 @@ export default defineCli({ server: server({ events: () => textStep("Started the subagent."), // The wait reads the child's inbox too. - routes: [jobs((count) => (count === 1 ? [child] : [])), { path: "/api/session/ses_child/inbox", json: { data: [] } }], - late: [ + routes: [ + jobs((count) => (count === 1 ? [child] : [])), + { path: "/api/session/ses_child/inbox", json: { data: [] } }, + ], + }), + late: { + after: waitingJson, + events: [ event("session.text.started", { assistantMessageID: "msg_b", ordinal: 0 }, 30), event("session.text.ended", { assistantMessageID: "msg_b", ordinal: 0, text: "Report handled." }, 31), event("session.execution.failed", { error: { type: "unknown", message: "report turn failed" } }, 32), ], - }), + }, masks: [nowMask], }) diff --git a/packages/cli-rs/parity/scenarios/C.10/background.cli.ts b/packages/cli-rs/parity/scenarios/C.10/background.cli.ts index 3e21fb20177..78c450a4900 100644 --- a/packages/cli-rs/parity/scenarios/C.10/background.cli.ts +++ b/packages/cli-rs/parity/scenarios/C.10/background.cli.ts @@ -30,11 +30,15 @@ export const woken = [ succeeded(), ] +// The wake arrives once the run has said it waits, so the wait notice comes first on both sides. Timed from +// the wait's answer instead, a client slow to list the jobs under load printed the woken reply first. +export const waitingJson = '"type":"background_wait"' + export default defineCli({ args: ["run", "hello"], server: server({ events: () => textStep("Started the job."), routes: [jobs((count) => (count === 1 ? [old, job] : [old]))], - late: woken, }), + late: { after: "waiting for 1 background job", events: woken }, }) diff --git a/packages/cli-rs/parity/scenarios/C.10/fixture.ts b/packages/cli-rs/parity/scenarios/C.10/fixture.ts index d7be84fb143..f0749d7fb0e 100644 --- a/packages/cli-rs/parity/scenarios/C.10/fixture.ts +++ b/packages/cli-rs/parity/scenarios/C.10/fixture.ts @@ -93,8 +93,6 @@ type Script = { listed?: boolean // The last event, `session.execution.succeeded` by default; `null` for none. end?: unknown - // Events pushed once the wait has answered. - late?: unknown[] } // The scripted events carry `ses_run`; a script for another session gets its ID. @@ -149,9 +147,6 @@ export function server(script: Script = {}): Options { path: `/api/experimental/session/${id}/wait`, status: 204, delay: 400, - then: () => retarget(script.late ?? [], id), - // After the run has started to reconcile. - thenDelay: 300, }, { path: "/api/experimental/job", json: { data: [] } }, { path: "/api/session/active", json: { data: {} } },