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.
This commit is contained in:
Simon Klee
2026-11-18 14:25:47 +01:00
Unverified
parent c352371bb0
commit 0b08d1cf62
6 changed files with 42 additions and 26 deletions
+16 -5
View File
@@ -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<Uint8Array>) => {
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(
+9 -9
View File
@@ -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<ReadableStreamDefaultController<Uint8Array>>()
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<NonNullable<typeof events>["emit"]>[0]) => events?.emit(event),
async stop() {
@@ -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],
})
@@ -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],
})
+5 -1
View File
@@ -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 },
})
-5
View File
@@ -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: {} } },