diff --git a/.github/workflows/ci-e2e-native.yml b/.github/workflows/ci-e2e-native.yml index aac1897c88..637a898eb0 100644 --- a/.github/workflows/ci-e2e-native.yml +++ b/.github/workflows/ci-e2e-native.yml @@ -41,16 +41,16 @@ jobs: - name: Build run: npm run build - - name: Run real-pi native-lane compression E2E + - name: Run real-pi native-lane E2E (compression + delegate notification timing) env: E2E_PI_BIN: pi - run: ACP_TEST_E2E_NATIVE=1 node --import tsx --test tests/e2e/e2e-native-pi.test.ts + run: ACP_TEST_E2E_NATIVE=1 node --import tsx --test tests/e2e/e2e-native-pi.test.ts tests/e2e/e2e-delegate-notify.test.ts - name: Collect E2E artifacts if: failure() run: | mkdir -p tmp/e2e-artifacts - for d in tmp/e2e-native-pi-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done + for d in tmp/e2e-native-pi-* tmp/e2e-delegate-notify-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done - uses: actions/upload-artifact@v4 if: failure() diff --git a/package-lock.json b/package-lock.json index 8c32ad4a86..88db3bc9b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8779,7 +8779,7 @@ }, "pi-subagents": { "name": "billion-context-pi-subagents", - "version": "0.1.2", + "version": "0.1.3", "dev": true, "license": "MIT", "devDependencies": { diff --git a/pi-subagents/package.json b/pi-subagents/package.json index 98a5736cb6..78d11c1103 100644 --- a/pi-subagents/package.json +++ b/pi-subagents/package.json @@ -1,6 +1,6 @@ { "name": "billion-context-pi-subagents", - "version": "0.1.2", + "version": "0.1.3", "description": "acp_delegate sub-agent tools for the Pi coding agent — split out of billion-context-pi (#612); vendored in-repo (#2384).", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/pi-subagents/src/delegate-tool.ts b/pi-subagents/src/delegate-tool.ts index 1e4e981e06..d939a0e06c 100644 --- a/pi-subagents/src/delegate-tool.ts +++ b/pi-subagents/src/delegate-tool.ts @@ -11,6 +11,7 @@ import { Type, type Static } from "typebox"; import { delegateStatusWidget } from "./fleet-widget.js"; import type { AgentToolResult, + ContextEvent, ExtensionAPI, ExtensionContext, ToolDefinition, @@ -26,12 +27,15 @@ const SETTLED_GRACE_MS = 10_000; const KILL_GRACE_MS = 10_000; const RESULT_SUMMARY_CHARS = 500; export const OUT_DIR = join(tmpdir(), "acp-delegate"); -// Completion notifications are committed when the host agent settles (goes -// idle), not on a fixed timer (#2301): a fixed delay can fire before the model -// has read the result file, committing an irrevocable follow-up that a later -// read cannot recall. Finishes that land before the next settle still merge -// into ONE message (issue #157) because they share the same settle-triggered -// flush. +// Completion notifications are committed at commit boundaries, never on a +// fixed timer (#2301): while the host is BUSY, pending runs stay revocable +// until the `context` event (fires before every model call) re-checks them and +// merges survivors into ONE message appended to the very context about to hit +// the provider — no async gap between check and commit, so a result the model +// already read/waited cannot be double-delivered. While IDLE (no model call +// coming) ONE merged steering message starts a turn instead (#2320: a follow- +// up parked there would wait out the whole task). Finishes landing between +// boundaries merge into that same message (#157). const SESSION_EXT = ".session.jsonl"; const ACTIVITY_TAIL_CHARS = 400; @@ -694,7 +698,7 @@ const agentListLine = (name: string): string => { }; export function makeDelegateTool(pi: ExtensionAPI): ToolDefinition { - ensureHostSettleHandlers(pi); + ensureHostNotificationHandlers(pi); const maxConcurrent = delegatePolicy.maxConcurrent; const concurrencyNote = Number.isFinite(maxConcurrent) ? `\n• Concurrency limit: at most ${maxConcurrent} background delegate(s) run at once; extra launches stay QUEUED and start automatically as slots free. Set delegate.maxConcurrent in acp.json (or PI_ACP_DELEGATE_MAX_CONCURRENT) to change it.` @@ -814,93 +818,190 @@ function undeliveredNotice(excludeRunId?: string): string { return undeliveredNoticeFrom(Array.from(runs.values()), excludeRunId); } -// ─── Settle-gated completion notifications (#157, #2301) ─────────────────── -// Each injected notification is a follow-up turn for the model; delegates that -// finish close together must share ONE message instead of piling up in the -// queue. Committing is gated on the host agent settling (going idle) rather -// than a fixed timer: at the settle boundary every read performed this round -// has already been observed, so the flush can drop result files the model -// already read (notifyIfRead "skip") and only deliver what it did not. A fixed -// delay would commit an irrevocable follow-up before a later read lands, with -// no API to recall it (#2301). Runs finishing while the host is idle are -// delivered promptly so the async "launch and wait" pattern still wakes the -// model without waiting for unrelated activity. - -let notifyPi: ExtensionAPI | undefined; -const notifyQueue: DelegateRun[] = []; -let flushScheduled = false; -/** Host agent runs currently in flight (agent_start .. agent_settled). Zero - * means the host is idle, which is when it is safe to commit pending - * notifications. */ -let hostAgentRuns = 0; - -const settleHandlerPis = new WeakSet(); - -/** Register the host-lifecycle handlers that drive notification commits. - * Called from makeDelegateTool, which every delegate owner (the standalone - * factory AND each embedder) invokes at setup, so the settle hook reaches all - * wiring paths without any embedder-side change. Guarded per pi instance so - * repeated tool registration does not double-count host runs. */ -function ensureHostSettleHandlers(pi: ExtensionAPI): void { +// ─── Completion notifications: boundary commits (#157, #2301, #2320) ──────── +// Two delivery tiers, both read-checked AT COMMIT time (never earlier): +// 1. BUSY host — the `context` event fires before every model call of this +// host (the agent loop's transformContext); the pending set is re-filtered +// there (waiter/consumed/read-suppression) and ONE merged notification is +// appended to the exact message list about to hit the provider. No await +// separates the final check from the real context commit, so nothing ever +// sits in an irrevocable host queue waiting out the task (#2301's +// post-commit race closed at the source; #2320's stalled follow-ups gone). +// Hosts whose extension surface lacks the `context` event never fire it — +// their pending set waits for tier 2 instead. +// 2. IDLE host — no model call is coming, so there is no context to append +// to; ONE merged message goes out via sendUserMessage(steer), which starts +// a turn immediately. +// Delegates finishing close together share ONE message in either tier (#157). +// Failed runs are never read-suppressed (their file holds no failure marker); +// failures stay loud. State is scoped PER HOST INSTANCE so a session switch can +// never deliver one session's notifications into another's context. + +interface HostNotificationState { + /** Terminal runs awaiting their next commit boundary (revocable until then). */ + queue: DelegateRun[]; + /** agent_start..agent_settled runs in flight on THIS host. */ + activeRuns: number; + /** Idle-tier flush already armed for this host. */ + flushScheduled: boolean; +} + +const hostNotificationStates = new WeakMap(); +/** Strong refs so a target-less flush (tests/embedders) can enumerate hosts. + * A process holds one live host (+ short-lived ones across session switches), + * so this cannot grow unboundedly. */ +const knownHosts = new Set(); + +function hostNotificationState(pi: ExtensionAPI): HostNotificationState { + let st = hostNotificationStates.get(pi); + if (!st) { + st = { queue: [], activeRuns: 0, flushScheduled: false }; + hostNotificationStates.set(pi, st); + knownHosts.add(pi); + } + return st; +} + +/** Register the host hooks that drive notification commits. Called from + * makeDelegateTool, which every delegate owner (the standalone factory AND + * each embedder) invokes at setup, so the hooks reach all wiring paths without + * any embedder-side change. Guarded per pi instance so repeated tool + * registration does not double-count host runs or double-register handlers. */ +function ensureHostNotificationHandlers(pi: ExtensionAPI): void { // Tolerate a partial extension surface (some embedders/tests stub only the - // methods they use): without lifecycle events we still deliver via the - // idle-at-finish path, so degrade instead of failing tool registration. + // methods they use): without lifecycle events we still deliver via the idle + // tier or a later carrier recovery, so degrade instead of failing tool + // registration. if (typeof pi.on !== "function") return; - if (settleHandlerPis.has(pi)) return; - settleHandlerPis.add(pi); + if (hostNotificationStates.has(pi)) return; + const st = hostNotificationState(pi); pi.on("agent_start", () => { - hostAgentRuns += 1; + st.activeRuns += 1; }); pi.on("agent_settled", () => { - if (hostAgentRuns > 0) hostAgentRuns -= 1; - maybeFlushNotifications(); + if (st.activeRuns > 0) st.activeRuns -= 1; + maybeFlushNotifications(pi); }); + // Busy-tier commit point: fires before every LLM call of this host. Inert on + // hosts that lack the event (registration stays a harmless no-op then). + pi.on("context", (event: ContextEvent) => commitAtContextBoundary(st, event)); } -/** Schedule a read-checked flush on the next macrotask if the host is idle. +/** Schedule the idle-tier flush on the next macrotask if THIS host is idle. * Deferred (setTimeout 0) so the sendUserMessage-driven turn starts only after * the settling finally has fully unwound, avoiding two overlapping agent runs. - * At most one flush is ever pending, so same-tick finishes coalesce into one. */ -function maybeFlushNotifications(): void { - if (hostAgentRuns > 0) return; - if (flushScheduled) return; - flushScheduled = true; + * At most one flush is ever pending per host, so same-tick finishes coalesce + * into one. No-op while the host is busy: the `context` handler commits at the + * next model call instead. */ +function maybeFlushNotifications(pi: ExtensionAPI): void { + const st = hostNotificationState(pi); + if (st.activeRuns > 0) return; + if (st.flushScheduled) return; + st.flushScheduled = true; const t = setTimeout(() => { - flushScheduled = false; - flushDelegateNotifications(); + st.flushScheduled = false; + flushHostNotifications(pi); }, 0); t.unref?.(); } -/** Queue a finished run's completion notification for settle-gated delivery. - * Runs that finish before the next settle share one batched message (#157). If - * the host is already idle the flush is scheduled immediately; otherwise it - * waits for the next agent_settled, which re-checks reads before committing. */ +/** Queue a finished run's completion notification. While the host is busy the + * entry stays REVOCABLE until the next `context` boundary re-checks it; when + * the host is idle an immediate deferred flush is armed (idle tier). */ export function scheduleRunNotification(pi: ExtensionAPI, run: DelegateRun): void { - if (!notifyQueue.includes(run)) notifyQueue.push(run); + const st = hostNotificationState(pi); + if (!st.queue.includes(run)) st.queue.push(run); run.notifyQueued = true; - notifyPi = pi; - maybeFlushNotifications(); -} - -/** Deliver every undelivered terminal run (queued + any earlier lost ones) as - * a single injected message. Runs that gained a waiter or were consumed - * while queued are skipped — their result is owned by the wait/cancel path. - * On send failure nothing is marked delivered, so a later carrier recovers - * the batch via the normal undelivered-notice mechanism. */ -export function flushDelegateNotifications(): void { - const queued = new Set(notifyQueue.splice(0)); + maybeFlushNotifications(pi); +} + +/** Busy-tier commit: re-check ownership of queued runs EXACTLY where the model + * context is being assembled (the `context` event) and, if anything survives, + * append ONE merged notification user message to the outgoing message list. + * Between the read/wait checks below and the returned array there is no await, + * so a result the model already claimed cannot be double-delivered (#2301). + * Runs dropped here reached the model another way (waiter/consumed/read-after- + * finish suppression). Gate: commit only when the conversation already holds an + * assistant message — a fresh first-turn context ([system?, user]) cannot carry + * pending results yet (the child was spawned by an earlier response), and + * refusing non-turn contexts keeps auxiliary calls from consuming the batch. + * When the gate fails the queue is left intact for the next boundary. */ +function commitAtContextBoundary(st: HostNotificationState, event: ContextEvent) { + if (st.queue.length === 0) return undefined; + if (!event.messages.some((m) => m.role === "assistant")) return undefined; + const deliverable: DelegateRun[] = []; + for (const r of st.queue) { + r.notifyQueued = false; + if (r.waiter || r.consumed || r.injected) continue; + if (r.status !== "completed" && r.status !== "failed") continue; + // Read-check at commit time (#2301): a completed run whose result file the + // model already read (at/after finish) is dropped here, not delivered. + // Failed runs are never suppressed (failures stay loud). + if (r.status === "completed" && shouldSuppressRead(r, delegateNotifyIfRead)) { + applyReadSuppression(r, r.runId); + continue; + } + deliverable.push(r); + } + if (deliverable.length === 0) { + st.queue.length = 0; + return undefined; + } + const mode = delegateDisplayUsage; + let text: string; + let covered: DelegateRun[] = []; + if (deliverable.length === 1) { + const r = deliverable[0]!; + const built = buildInjectText( + r.agent, r.runId, r.task, r.status, + r.result?.code ?? null, r.result?.file ?? "", r.timedOut, + r.usage, mode, r.usageReported, + r.status === "failed" ? r.result?.body : undefined, + r.status === "failed" ? r.activityFile : undefined, + r.exitSignal, + ); + text = built.text; + covered = built.covered; + } else { + for (const r of deliverable) { + if (mode === "separate" && r.usage && !r.usageReported) addDelegateUsage(r.usage); + } + text = buildBatchText(deliverable, mode, () => true); + } + for (const r of deliverable) { + r.injected = true; // committed synchronously below — no async window + if (r.usage && !r.usageReported) r.usageReported = true; + } + for (const c of covered) c.injected = true; + st.queue.length = 0; + debug.event("delegate-notify-context-commit", { count: deliverable.length, failed: deliverable.filter((r) => r.status === "failed").length, runIds: deliverable.map((r) => r.runId).join(",") }); + logInfo("delegate", { event: "notify-context-commit", count: deliverable.length }); + return { messages: [...event.messages, { role: "user" as const, content: [{ type: "text" as const, text }], timestamp: Date.now() }] }; +} + +/** Idle-tier delivery: every undelivered terminal run of ONE host (queued + + * any earlier lost ones) as a single injected message. Called with no argument + * (tests/embedders) to flush every known host. Runs that gained a waiter or + * were consumed while queued are skipped — their result is owned by the + * wait/cancel path. On send failure nothing is marked delivered, so a later + * carrier recovers the batch via the normal undelivered-notice mechanism. */ +export function flushDelegateNotifications(target?: ExtensionAPI): void { + const hosts = target ? [target] : Array.from(knownHosts); + for (const h of hosts) flushHostNotifications(h); +} + +function flushHostNotifications(pi: ExtensionAPI): void { + const st = hostNotificationState(pi); + const queued = new Set(st.queue.splice(0)); for (const r of queued) r.notifyQueued = false; - const pi = notifyPi; - if (!pi) return; const owned = (r: DelegateRun): boolean => { if (r.waiter || r.consumed || r.injected) return false; if (r.status !== "completed" && r.status !== "failed") return false; - // Read-check at commit time (#2301): delivery is decided HERE, at the settle + // Read-check at commit time (#2301): delivery is decided HERE, at the commit // boundary, so a completed run whose result file the model already read // (at/after finish) is dropped instead of committed — not just reads that - // preceded some earlier fixed timer. Failed runs are never suppressed (their - // file holds no failure marker; failures stay loud). + // preceded some earlier check. Failed runs are never suppressed (their file + // holds no failure marker; failures stay loud). if (r.status === "completed" && shouldSuppressRead(r, delegateNotifyIfRead)) { applyReadSuppression(r, r.runId); return false; @@ -922,49 +1023,36 @@ export function flushDelegateNotifications(): void { } } if (deliverable.length === 0) return; + const mode = delegateDisplayUsage; + let text: string; + let covered: DelegateRun[] = []; if (deliverable.length === 1) { const r = deliverable[0]!; - const mode = delegateDisplayUsage; - const injected = injectResult( - pi, - r.agent, - r.runId, - r.task, - r.status, - r.result?.code ?? null, - r.result?.file ?? "", - r.timedOut, - r.usage, - mode, - r.usageReported, + const built = buildInjectText( + r.agent, r.runId, r.task, r.status, + r.result?.code ?? null, r.result?.file ?? "", r.timedOut, + r.usage, mode, r.usageReported, r.status === "failed" ? r.result?.body : undefined, r.status === "failed" ? r.activityFile : undefined, r.exitSignal, ); - if (r.usage && !r.usageReported && (mode === "separate" || injected)) r.usageReported = true; - r.injected = injected; - return; - } - const mode = delegateDisplayUsage; - const failedCount = deliverable.filter((r) => r.status === "failed").length; - const okCount = deliverable.length - failedCount; - const lost = deliverable.filter((r) => !queued.has(r)); - const parts: string[] = []; - if (lost.length > 0) { - parts.push(`⚠️ Recovery notice: ${lost.length} earlier delegate result${lost.length === 1 ? "" : "s"} never reached you (notification delivery failed); included below.`); - } - parts.push(`[acp_delegate] ${deliverable.length} delegates finished (${okCount} completed${failedCount > 0 ? `, ${failedCount} FAILED` : ""}).`); - for (const r of deliverable) parts.push(formatBatchRunSection(r)); - for (const r of deliverable) { - if (mode === "separate" && r.usage && !r.usageReported) addDelegateUsage(r.usage); + text = built.text; + covered = built.covered; + } else { + for (const r of deliverable) { + if (mode === "separate" && r.usage && !r.usageReported) addDelegateUsage(r.usage); + } + text = buildBatchText(deliverable, mode, (r) => queued.has(r)); } - parts.push(buildBatchTrailer(deliverable, failedCount > 0, mode)); - const text = parts.join("\n\n"); const send = pi.sendUserMessage; let sent = false; if (typeof send === "function") { try { - send.call(pi, text, { deliverAs: "followUp" }); + // Steering, not follow-up (#2320): a follow-up parked here would wait for + // the WHOLE task to end before reaching the model; steering is consumed at + // the next safe boundary of the current task — which, at the idle tier, is + // immediately (an idle host starts a turn right away). + send.call(pi, text, { deliverAs: "steer" }); sent = true; } catch (err) { logError("delegate", { event: "notify-batch-error", error: String(err), runIds: deliverable.map((r) => r.runId).join(",") }); @@ -972,12 +1060,15 @@ export function flushDelegateNotifications(): void { } else { logWarn("delegate", { event: "notify-batch-skipped", reason: "sendUserMessage unavailable" }); } + if (sent) { + for (const r of deliverable) r.injected = true; + for (const c of covered) c.injected = true; + } for (const r of deliverable) { - if (sent) r.injected = true; if (r.usage && !r.usageReported && (mode === "separate" || sent)) r.usageReported = true; } - debug.event("delegate-notify-batch", { count: deliverable.length, failed: failedCount, sent, runIds: deliverable.map((r) => r.runId).join(",") }); - logInfo("delegate", { event: "notify-batch", count: deliverable.length, failed: failedCount, sent }); + debug.event("delegate-notify-batch", { count: deliverable.length, failed: deliverable.filter((r) => r.status === "failed").length, sent, runIds: deliverable.map((r) => r.runId).join(",") }); + logInfo("delegate", { event: "notify-batch", count: deliverable.length, failed: deliverable.filter((r) => r.status === "failed").length, sent }); } /** One per-run section of a batched notification: status header + task + @@ -990,6 +1081,25 @@ export function formatBatchRunSection(run: DelegateRun): string { return formatPayload(header, run.result?.file ?? "", run.task, failed ? run.result?.body : undefined, failed ? run.activityFile : undefined); } +/** Build the merged multi-run notification text. `fromQueue` distinguishes + * freshly queued runs from earlier undelivered ones folded in for recovery + * (those get the recovery header). Usage accounting stays with the caller, + * which must accumulate BEFORE calling (the trailer shows the cumulative total + * including this batch). */ +function buildBatchText(deliverable: DelegateRun[], mode: "merged" | "separate", fromQueue: (r: DelegateRun) => boolean): string { + const failedCount = deliverable.filter((r) => r.status === "failed").length; + const okCount = deliverable.length - failedCount; + const lost = deliverable.filter((r) => !fromQueue(r)); + const parts: string[] = []; + if (lost.length > 0) { + parts.push(`⚠️ Recovery notice: ${lost.length} earlier delegate result${lost.length === 1 ? "" : "s"} never reached you (notification delivery failed); included below.`); + } + parts.push(`[acp_delegate] ${deliverable.length} delegates finished (${okCount} completed${failedCount > 0 ? `, ${failedCount} FAILED` : ""}).`); + for (const r of deliverable) parts.push(formatBatchRunSection(r)); + parts.push(buildBatchTrailer(deliverable, failedCount > 0, mode)); + return parts.join("\n\n"); +} + function buildBatchTrailer(batch: DelegateRun[], anyFailed: boolean, mode: "merged" | "separate"): string { const remaining = Array.from(runs.values()).filter((r) => (r.status === "running" || r.status === "queued")).length; const remainingLine = remaining > 0 @@ -1898,11 +2008,15 @@ function suppressIfReadNow(run: DelegateRun): void { if (run.status === "completed" && shouldSuppressRead(run, delegateNotifyIfRead)) applyReadSuppression(run, run.runId); } -/** status (set by finalize from the effective exit code) is the authority for +/** Build the single-run notification text WITHOUT sending: status header + + * remaining-count line + usage note + payload, plus a trailing recovery notice + * for earlier undelivered runs (returned as `covered` so the caller commits + * their marking only once ITS delivery succeeds). Separate-mode usage + * accumulation happens here because the cumulative line must include this run. + * status (set by finalize from the effective exit code) is the authority for * the FAILED/completed decision; the raw code is diagnostic display only * ("exit ?"), so the notification can never disagree with run.status. */ -export function injectResult( - pi: ExtensionAPI, +function buildInjectText( agent: string, runId: string, task: string, @@ -1916,13 +2030,7 @@ export function injectResult( body?: string, activityFile?: string, signal?: NodeJS.Signals | null, -): boolean { - const send = pi.sendUserMessage; - if (typeof send !== "function") { - debug.event("delegate-inject-skipped", { runId, reason: "sendUserMessage unavailable" }); - logWarn("delegate", { event: "inject-skipped", runId, reason: "sendUserMessage unavailable" }); - return false; - } +): { text: string; covered: DelegateRun[] } { const failed = status === "failed"; const statusLabel = failed ? "FAILED ⚠️" : "completed"; // Tell the model how many other delegates are still running, so it doesn't @@ -1974,11 +2082,40 @@ export function injectResult( const header = `[acp_delegate ${statusLabel}] **${agent}** (runId \`${runId}\`, ${exitLabel(code, signal)})${timeoutNote}${remainingLine}${usageNote} ${closing}`; const { text: recoveryText, covered } = buildRecoveryNotice(Array.from(runs.values()), runId); const text = formatPayload(header, file, task, failed ? body : undefined, failed ? activityFile : undefined) + (recoveryText ? `\n\n${recoveryText}` : ""); + return { text, covered }; +} + +/** Send a single-run notification via the host's message channel (idle tier / + * direct callers). Returns true only once the host accepted the message; on + * failure nothing is marked delivered so a later carrier recovers it. */ +export function injectResult( + pi: ExtensionAPI, + agent: string, + runId: string, + task: string, + status: RunStatus, + code: number | null, + file: string, + timedOut?: string, + usage?: Usage, + mode: "merged" | "separate" = "separate", + usageAlreadyReported?: boolean, + body?: string, + activityFile?: string, + signal?: NodeJS.Signals | null, +): boolean { + const send = pi.sendUserMessage; + if (typeof send !== "function") { + debug.event("delegate-inject-skipped", { runId, reason: "sendUserMessage unavailable" }); + logWarn("delegate", { event: "inject-skipped", runId, reason: "sendUserMessage unavailable" }); + return false; + } + const { text, covered } = buildInjectText(agent, runId, task, status, code, file, timedOut, usage, mode, usageAlreadyReported, body, activityFile, signal); try { - // sendUserMessage is fire-and-forget (returns void): it enqueues a - // follow-up turn. Interactive/rpc sessions consume it via their main loop; - // injection at shutdown is best-effort (no API to await a turn). - send.call(pi, text, { deliverAs: "followUp" }); + // Steering, not follow-up (#2320): a follow-up parked here would wait out + // the whole task before reaching the model; steering is consumed at the + // next safe boundary (immediately when the host is idle). + send.call(pi, text, { deliverAs: "steer" }); // Commit the recovery marking only now: a thrown send above must leave the // covered runs undelivered so a later carrier can still recover them. for (const r of covered) r.injected = true; diff --git a/pi-subagents/tests/delegate-context-notify.test.ts b/pi-subagents/tests/delegate-context-notify.test.ts new file mode 100644 index 0000000000..078952aac9 --- /dev/null +++ b/pi-subagents/tests/delegate-context-notify.test.ts @@ -0,0 +1,232 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + makeDelegateTool, + scheduleRunNotification, + setDelegateNotifyIfRead, +} from "../src/delegate-tool.js"; + +// #2320 busy-tier contract: while the host is mid-task, a finished delegate's +// notification must be committed at the NEXT MODEL CALL (the `context` event), +// not parked until the task ends. These tests drive the host lifecycle events +// deterministically through the same mock surface as +// delegate-settle-notify.test.ts, plus the `context` event that carries the +// outgoing message list. + +type PiLike = Parameters[0]; + +function mkRun(runId: string, status: "completed" | "failed", over: Record = {}): any { + return { + runId, + agent: "reviewer", + task: "review X", + cwd: "/tmp", + startedAt: 0, + finishedAt: 1000, + status, + result: { code: status === "completed" ? 0 : 1, file: `/tmp/${runId}.out`, body: "boom" }, + ...over, + }; +} + +const assistantMsg = { role: "assistant", content: [{ type: "text", text: "working" }] } as any; +const userMsg = { role: "user", content: [{ type: "text", text: "go" }] } as any; + +type SentEntry = { t: string; o?: { deliverAs?: string } }; + +/** Mock ExtensionAPI capturing sends WITH options and able to fire the `context` + * event with a chosen outgoing message list. */ +function mockPi() { + const sent: SentEntry[] = []; + const handlers = new Map void>>(); + const pi = { + sendUserMessage: (t: string, o?: { deliverAs?: string }) => void sent.push({ t, o }), + on: (event: string, handler: (...a: unknown[]) => void) => { + const list = handlers.get(event) ?? []; + list.push(handler); + handlers.set(event, list); + }, + } as unknown as PiLike; + const emit = (event: string) => { + for (const h of handlers.get(event) ?? []) h({}); + }; + // Fire the context boundary; returns the handler's ContextEventResult (if any). + const context = (messages: unknown[]): unknown => { + let res: unknown; + for (const h of handlers.get("context") ?? []) { + const r = h({ type: "context", messages }); + if (r !== undefined) res = r; + } + return res; + }; + return { pi, sent, emit, context }; +} + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); +async function waitFor(cond: () => boolean, what: string, ms = 1000): Promise { + const deadline = Date.now() + ms; + while (!cond()) { + if (Date.now() > deadline) throw new Error(`timed out waiting: ${what}`); + await sleep(5); + } +} + +// ─── busy tier: commit at the next model call, mid-task ───────────────────── + +test("busy host: notification commits into the next model call's context (#2320)", async () => { + setDelegateNotifyIfRead("skip"); + const { pi, sent, emit, context } = mockPi(); + makeDelegateTool(pi); + emit("agent_start"); // host is mid-task + const run = mkRun("del_busy", "completed"); + scheduleRunNotification(pi, run); + await sleep(30); + assert.equal(sent.length, 0, "nothing sent while the host is busy"); + + // The next LLM call assembles the context — the notification must ride along. + const base = [assistantMsg, userMsg]; + const res = context(base) as { messages: unknown[] } | undefined; + assert.ok(res, "context handler returns modified messages"); + assert.equal(res.messages.length, base.length + 1, "exactly ONE message appended"); + const appended = res.messages[base.length]! as any; + assert.equal(appended.role, "user"); + assert.ok(String(appended.content[0]!.text).includes("`del_busy`"), "names the run"); + assert.equal(run.injected, true, "marked committed synchronously"); + assert.equal(run.notifyQueued, false); + + // Task ends shortly after: nothing may be delivered AGAIN. + emit("agent_settled"); + await sleep(30); + assert.equal(sent.length, 0, "no double delivery at the settle boundary"); +}); + +test("busy host: read-before-boundary drops the notification at commit time (#2301)", async () => { + setDelegateNotifyIfRead("skip"); + const { pi, sent, emit, context } = mockPi(); + makeDelegateTool(pi); + emit("agent_start"); + const run = mkRun("del_ctx_read", "completed"); + scheduleRunNotification(pi, run); + run.readAt = run.finishedAt; // model claims the result before the boundary + const res = context([assistantMsg, userMsg]); + assert.equal(res, undefined, "nothing survives the re-check -> unmodified context"); + assert.equal(run.readSuppressed, true, "recorded as suppressed"); + assert.equal(run.injected, true, "suppression marks it handled so no tier can deliver it"); + emit("agent_settled"); + await sleep(30); + assert.equal(sent.length, 0, "suppressed run never reaches the idle tier either"); +}); + +test("busy host: failed run commits even when its output was read", async () => { + setDelegateNotifyIfRead("skip"); + const { pi, emit, context } = mockPi(); + makeDelegateTool(pi); + emit("agent_start"); + const run = mkRun("del_ctx_fail", "failed"); + scheduleRunNotification(pi, run); + run.readAt = run.finishedAt; + const res = context([assistantMsg, userMsg]) as { messages: unknown[] }; + const appended = res.messages[res.messages.length - 1] as any; + assert.match(String(appended.content[0]!.text), /FAILED/, "failures stay loud"); + assert.equal(run.readSuppressed, undefined); +}); + +test("busy host: close-together finishes merge into ONE appended message", async () => { + const { pi, emit, context } = mockPi(); + makeDelegateTool(pi); + emit("agent_start"); + const a = mkRun("del_m1", "completed"); + const b = mkRun("del_m2", "failed"); + scheduleRunNotification(pi, a); + scheduleRunNotification(pi, b); + const base = [assistantMsg, userMsg]; + const res = context(base) as { messages: unknown[] }; + assert.equal(res.messages.length, base.length + 1, "one merged message, not two"); + const text = String((res.messages[base.length] as any).content[0]!.text); + assert.ok(text.includes("`del_m1`") && text.includes("`del_m2`"), "both runs named"); + assert.ok(text.includes("2 delegates finished"), "batch header"); + assert.equal(a.injected, true); + assert.equal(b.injected, true); +}); + +test("busy host: non-turn contexts (no assistant yet) defer the commit", async () => { + const { pi, emit, context } = mockPi(); + makeDelegateTool(pi); + emit("agent_start"); + const run = mkRun("del_gate", "completed"); + scheduleRunNotification(pi, run); + const res = context([userMsg]); // e.g. an auxiliary/first-turn context + assert.equal(res, undefined, "gate refuses a context with no assistant turn"); + assert.equal(run.notifyQueued, true, "still pending for the next boundary"); + const res2 = context([assistantMsg, userMsg]) as { messages: unknown[] }; + assert.ok(res2, "the real turn's context picks it up"); + assert.equal(run.injected, true); +}); + +test("busy host: waiter-owned runs are excluded from the boundary commit", async () => { + const { pi, emit, context } = mockPi(); + makeDelegateTool(pi); + emit("agent_start"); + const parked = mkRun("del_parked", "completed", { waiter: {} }); + const free = mkRun("del_free", "completed"); + scheduleRunNotification(pi, parked); + scheduleRunNotification(pi, free); + const res = context([assistantMsg, userMsg]) as { messages: unknown[] }; + const text = String((res.messages[res.messages.length - 1] as any).content[0]!.text); + assert.ok(text.includes("`del_free`")); + assert.ok(!text.includes("`del_parked`"), "wait-owned result stays with the wait path"); + assert.ok(!parked.injected, "waiter run untouched — the wait path owns its delivery"); +}); + +// ─── per-host isolation: sessions never leak notifications across hosts ───── + +test("two hosts: each boundary commits only its own queued runs", async () => { + const A = mockPi(); + const B = mockPi(); + makeDelegateTool(A.pi); + makeDelegateTool(B.pi); + A.emit("agent_start"); + B.emit("agent_start"); + const ra = mkRun("del_hostA", "completed"); + const rb = mkRun("del_hostB", "completed"); + scheduleRunNotification(A.pi, ra); + scheduleRunNotification(B.pi, rb); + await sleep(30); + assert.equal(A.sent.length, 0); + assert.equal(B.sent.length, 0); + + const resA = A.context([assistantMsg, userMsg]) as { messages: unknown[] }; + const textA = String((resA.messages[resA.messages.length - 1] as any).content[0]!.text); + assert.ok(textA.includes("`del_hostA`")); + assert.ok(!textA.includes("`del_hostB`"), "host A's context never sees host B's run"); + assert.equal(rb.notifyQueued, true, "host B's run still pending"); + + const resB = B.context([assistantMsg, userMsg]) as { messages: unknown[] }; + const textB = String((resB.messages[resB.messages.length - 1] as any).content[0]!.text); + assert.ok(textB.includes("`del_hostB`")); + assert.ok(!textB.includes("`del_hostA`")); +}); + +// ─── idle tier: ONE merged steering message starts the follow-up turn ─────── + +test("idle host: single finish wakes with ONE steering message (#2320)", async () => { + const { pi, sent } = mockPi(); + makeDelegateTool(pi); + const run = mkRun("del_idle_steer", "completed"); + scheduleRunNotification(pi, run); + await waitFor(() => sent.length === 1, "idle wake"); + assert.ok(sent[0]!.t.includes("`del_idle_steer`")); + assert.deepEqual(sent[0]!.o, { deliverAs: "steer" }, "steering, not follow-up"); +}); + +test("idle host: multiple finishes coalesce into ONE steering message", async () => { + const { pi, sent } = mockPi(); + makeDelegateTool(pi); + const a = mkRun("del_ib1", "completed"); + const b = mkRun("del_ib2", "failed"); + scheduleRunNotification(pi, a); + scheduleRunNotification(pi, b); + await waitFor(() => sent.length === 1, "coalesced idle wake"); + assert.ok(sent[0]!.t.includes("`del_ib1`") && sent[0]!.t.includes("`del_ib2`")); + assert.deepEqual(sent[0]!.o, { deliverAs: "steer" }); +}); diff --git a/pi-subagents/tests/delegate-settle-notify.test.ts b/pi-subagents/tests/delegate-settle-notify.test.ts index 79372ee31f..3fcf5bc7be 100644 --- a/pi-subagents/tests/delegate-settle-notify.test.ts +++ b/pi-subagents/tests/delegate-settle-notify.test.ts @@ -94,7 +94,7 @@ test("finish while the host is idle delivers without waiting for a settle", asyn const { pi, sent } = mockPi(); makeDelegateTool(pi); const run = mkRun("del_idle", "completed"); - scheduleRunNotification(pi, run); // hostAgentRuns === 0 -> immediate deferred flush + scheduleRunNotification(pi, run); // activeRuns === 0 -> immediate deferred flush await waitFor(() => sent.length === 1, "idle wake"); assert.ok(sent[0]!.includes("`del_idle`")); }); diff --git a/pi-subagents/tests/delegate-tool.test.ts b/pi-subagents/tests/delegate-tool.test.ts index c0cd0fe8f2..16720b340f 100644 --- a/pi-subagents/tests/delegate-tool.test.ts +++ b/pi-subagents/tests/delegate-tool.test.ts @@ -950,6 +950,28 @@ test("single undelivered run flushes through the legacy single-run format", () = assert.equal(a.injected, true); }); +test("completion notifications are injected as steering on both paths (#2320)", async () => { + const entries: Array<{ t: string; o?: { deliverAs?: string } }> = []; + const handlers = new Map void>>(); + const pi = { + sendUserMessage: (t: string, o?: { deliverAs?: string }) => void entries.push({ t, o }), + on: (ev: string, h: (...a: unknown[]) => void) => { + const list = handlers.get(ev) ?? []; + list.push(h); + handlers.set(ev, list); + }, + } as any; + makeDelegateTool(pi); + for (const h of handlers.get("agent_start") ?? []) h({}); // busy: no idle-flush timer can fire + const ok = injectResult(pi, "reviewer", "del_steer", "review X", "completed", 0, "/tmp/del_steer.out", undefined, undefined, "separate", false); + assert.equal(ok, true, "single-path injection succeeds"); + scheduleRunNotification(pi, mkRun("del_steer_a", "completed")); + scheduleRunNotification(pi, mkRun("del_steer_b", "failed")); + flushDelegateNotifications(pi); + assert.equal(entries.length, 2, "one single-path send + one merged batch send"); + for (const e of entries) assert.deepEqual(e.o, { deliverAs: "steer" }, "steering delivery, not follow-up"); +}); + test("findUndeliveredRuns excludes queued runs (scheduled is not lost)", () => { const queued = mkRun("del_q", "completed", { notifyQueued: true }); const lost = mkRun("del_lost", "failed"); diff --git a/tests/e2e/e2e-delegate-notify.test.ts b/tests/e2e/e2e-delegate-notify.test.ts new file mode 100644 index 0000000000..e502df7382 --- /dev/null +++ b/tests/e2e/e2e-delegate-notify.test.ts @@ -0,0 +1,381 @@ +// E2E: acp_delegate completion-notification DELIVERY TIMING (#2320) through the +// REAL pi RPC lane and bili's embedded pi-subagents wiring (vendored in-repo, +// #2384). Deterministic fake chat upstream scripts the model; zero tokens. +// +// What this discriminates, from the per-request oracle (FAKE_REQLOG JSONL): +// FIXED => the notification user message appears as the FRESH last user +// message of a request whose directive queue still had work left +// (queueIdx < N) — consumed mid-task at the next safe boundary — +// and the run continues afterwards with further tool rounds. +// BUGGY => the notification only ever appears after the queue is exhausted +// (queueIdx == N): parked in the host follow-up queue until the +// whole main task ends (the #2320 defect). +// Under the two-tier commit design (busy tier appends into the `context` event +// before the next LLM call; idle tier sends ONE steering message when settled) +// NOTHING may park in the host follow-up/steer queue during the task, so the +// sampler additionally asserts pendingMessageCount stays 0 while busy. +// Scenario B proves the no-further-input property: after the scripted prompt is +// fully consumed, a late completion must still reach the model — either by +// committing into the initial run's final context or by steer auto-starting a +// new turn from idle. +// +// Gated by ACP_TEST_E2E_NATIVE=1 (same gate as e2e-native-pi.test.ts; needs +// `npm run build` first — the pi package loads dist/agent/pi-native.js, which +// inlines the vendored pi-subagents source). Skips when the pi CLI is absent. + +import { spawn, spawnSync } from "node:child_process"; +import fs from "node:fs"; +import http from "node:http"; +import net from "node:net"; +import os from "node:os"; +import path from "node:path"; +import { strict as assert } from "node:assert"; +import { test } from "node:test"; +import { assertPortDead } from "../port-race.js"; + +const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; +const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const FAKE_UPSTREAM = path.join(import.meta.dirname, "fake-upstream-chat.mjs"); +const TMO_MS = Number(process.env.E2E_TMO ?? 180_000); +const QUIET_MS = 12_000; // sustained quiescence before declaring the scenario done +const SAMPLE_MS = 400; +const WORK_ROOT = path.join(process.cwd(), "tmp"); +fs.mkdirSync(WORK_ROOT, { recursive: true }); +// pi walks up from its cwd for AGENTS.md too (#815): stay outside the repo. +const CWD_ROOT = path.join(os.tmpdir(), "billion-context-e2e-delegate"); +fs.mkdirSync(CWD_ROOT, { recursive: true }); + +function piAvailable(): boolean { + try { + return spawnSync(PI_BIN, ["--version"], { timeout: 15_000 }).status === 0; + } catch { + return false; + } +} + +/** Resolve the pi CLI entry for delegate CHILD processes. The global install + * ships under an aliased package dir (pi-stable), so the in-package global + * probes can miss it; derive the entry from the binary itself (symlinks + * resolve to dist/cli.js) and hand it over via PI_CLI_PATH, which the + * resolver honors first. Returns undefined when undeterminable — the package + * then falls back to its own probing. */ +function findPiCliEntry(bin: string): string | undefined { + const candidates: string[] = []; + try { + const w = spawnSync("sh", ["-c", `command -v ${JSON.stringify(bin)}`], { timeout: 5_000 }); + const abs = w.stdout.toString().trim(); + if (w.status === 0 && abs) candidates.push(abs); + } catch {} + candidates.push(bin); + for (const c of candidates) { + try { + const real = fs.realpathSync(c); + if (/[/\\]dist[/\\]cli\.js$/.test(real)) return real; + } catch {} + } + return undefined; +} + +function cleanEnv(extra: Record): NodeJS.ProcessEnv { + const env = { ...process.env }; + for (const key of Object.keys(env)) { + if (key.startsWith("BILI") || key.startsWith("BILLION_CONTEXT") || key.startsWith("ACP_")) delete env[key]; + } + for (const key of ["NODE_EXTRA_CA_CERTS", "PI_SESSION_FILE", "PI_PROVIDER", "PI_MODEL", "NODE_OPTIONS", "NODE_TEST_CONTEXT"]) delete env[key]; + return { ...env, ...extra } as NodeJS.ProcessEnv; +} + +function freePort(): Promise { + return new Promise((resolve, reject) => { + const srv = net.createServer(); + srv.listen(0, "127.0.0.1", () => { + const addr = srv.address(); + if (addr && typeof addr !== "string") srv.close(() => resolve(addr.port)); + else reject(new Error("freePort: no address")); + }); + srv.on("error", reject); + }); +} + +function waitForHttp(url: string, ms: number): Promise { + const deadline = Date.now() + ms; + return new Promise((resolve, reject) => { + const tick = () => { + const req = http.get(url, (res) => { res.resume(); res.on("end", resolve); }); + req.on("error", () => { + if (Date.now() > deadline) reject(new Error(`waitForHttp timeout: ${url}`)); + else setTimeout(tick, 200); + }); + }; + tick(); + }); +} + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); + +interface Ctx { + work: string; + piCwd: string; + piAgentDir: string; + xdg: { config: string; cache: string; state: string; data: string }; + reqLog: string; +} + +function makeCtx(work: string): Ctx { + const ctx: Ctx = { + work, + piCwd: fs.mkdtempSync(path.join(CWD_ROOT, "cwd-")), + piAgentDir: path.join(work, "pi-agent"), + xdg: { + config: path.join(work, "xdg-config"), + cache: path.join(work, "xdg-cache"), + state: path.join(work, "xdg-state"), + data: path.join(work, "xdg-data"), + }, + reqLog: path.join(work, "fake-chat-requests.jsonl"), + }; + for (const d of [ctx.piAgentDir, ctx.piCwd, ctx.xdg.config, ctx.xdg.cache, ctx.xdg.state, ctx.xdg.data]) { + fs.mkdirSync(d, { recursive: true }); + } + return ctx; +} + +function writeHostConfig(ctx: Ctx, fakePort: number): void { + fs.writeFileSync(path.join(ctx.piAgentDir, "settings.json"), JSON.stringify({ packages: [REPO_ROOT] }, null, 2)); + fs.writeFileSync(path.join(ctx.piAgentDir, "models.json"), JSON.stringify({ + providers: { + fake: { + baseUrl: `http://127.0.0.1:${fakePort}/v1`, + api: "openai-completions", + apiKey: "e2e-fake-key", + models: [{ id: "fake-model", name: "Fake", input: ["text"], contextWindow: 60000, maxTokens: 4096 }], + }, + }, + }, null, 2)); +} + +class RpcClient { + proc: ReturnType; + buf = ""; + responses = new Map(); + events: any[] = []; + states: Array<{ t: number; isStreaming?: boolean; pendingMessageCount?: number }> = []; + _idn = 0; + stderr = ""; + + constructor(cwd: string, env: NodeJS.ProcessEnv) { + this.proc = spawn(PI_BIN, ["--mode", "rpc", "--model", "fake/fake-model"], { + cwd, env, stdio: ["pipe", "pipe", "pipe"], + }); + if (!this.proc.stdout || !this.proc.stderr) throw new Error("rpc stdio unavailable"); + this.proc.stdout.on("data", (c: Buffer) => { + this.buf += c.toString(); + let i: number; + while ((i = this.buf.indexOf("\n")) >= 0) { + const line = this.buf.slice(0, i).trim(); + this.buf = this.buf.slice(i + 1); + if (!line) continue; + let obj: any; + try { obj = JSON.parse(line); } catch { this.events.push({ raw: line.slice(0, 300) }); continue; } + if (obj.type === "response" && obj.id != null && !this.responses.has(obj.id)) { + this.responses.set(obj.id, obj); + if (obj.command === "get_state" && obj.success) { + this.states.push({ t: Date.now(), ...obj.data }); + } + } else { + this.events.push(obj); + } + } + }); + this.proc.stderr.on("data", (c: Buffer) => { this.stderr += c.toString(); }); + } + + send(cmd: Record): string { + const id = `cmd_${++this._idn}`; + if (!this.proc.stdin) throw new Error("rpc stdin unavailable"); + this.proc.stdin.write(JSON.stringify({ id, ...cmd }) + "\n"); + return id; + } + + async waitResponse(id: string, ms: number): Promise { + const deadline = Date.now() + ms; + while (!this.responses.has(id)) { + if (Date.now() > deadline) throw new Error(`rpc response timeout: ${id}`); + await sleep(50); + } + return this.responses.get(id); + } + + kill(): void { + try { this.proc.kill("SIGTERM"); } catch {} + setTimeout(() => { try { this.proc.kill("SIGKILL"); } catch {} }, 2000).unref?.(); + } +} + +// Oracle heads carry ACP tag spans; strip them before content matching. +const stripAcps = (str: unknown) => String(str ?? "").replace(/\x3cacp\b[^>]*\x3e[\s\S]*?\x3c\/acp\x3e/g, "").trimStart(); + +function readOracle(file: string): any[] { + try { + return fs.readFileSync(file, "utf8").trim().split("\n").filter(Boolean).map((l) => JSON.parse(l)); + } catch { return []; } +} + +function killBiliInstances(ctx: Ctx): void { + const instancesDir = path.join(ctx.xdg.state, "billion-context", "instances"); + try { + for (const f of fs.readdirSync(instancesDir)) { + try { + const rec = JSON.parse(fs.readFileSync(path.join(instancesDir, f), "utf8")); + if (typeof rec.pid === "number") { try { process.kill(rec.pid, "SIGKILL"); } catch {} } + } catch {} + } + } catch {} +} + +async function runScenario(name: string, prompt: string, totalDirectives: number): Promise { + const work = fs.mkdtempSync(path.join(WORK_ROOT, `e2e-delegate-notify-${name}-`)); + const ctx = makeCtx(work); + const fakePort = await freePort(); + const fake = spawn(process.execPath, [FAKE_UPSTREAM], { + env: { ...process.env, FAKE_PORT: String(fakePort), FAKE_HOST: "127.0.0.1", FAKE_REQLOG: ctx.reqLog }, + stdio: ["ignore", "ignore", "ignore"], + }); + const failures: string[] = []; + const check = (label: string, ok: boolean, detail?: unknown): void => { + if (!ok) failures.push(`${label}${detail != null ? ` — ${String(detail).slice(0, 400)}` : ""}`); + }; + let pi: RpcClient | null = null; + try { + await waitForHttp(`http://127.0.0.1:${fakePort}/v1/models`, 15_000); + writeHostConfig(ctx, fakePort); + const cliEntry = findPiCliEntry(PI_BIN); + const env = cleanEnv({ + PI_CODING_AGENT_DIR: ctx.piAgentDir, + XDG_CONFIG_HOME: ctx.xdg.config, + XDG_CACHE_HOME: ctx.xdg.cache, + XDG_STATE_HOME: ctx.xdg.state, + XDG_DATA_HOME: ctx.xdg.data, + ...(cliEntry ? { PI_CLI_PATH: cliEntry } : {}), + }); + pi = new RpcClient(ctx.piCwd, env); + const pid = pi.send({ type: "prompt", message: prompt }); + let samplerAlive = true; + const sampler = setInterval(() => { + if (!samplerAlive) return; + try { pi!.send({ type: "get_state" }); } catch {} + }, SAMPLE_MS); + // Quiescence: isStreaming false && pendingMessageCount 0 sustained QUIET_MS. + let lastActiveT = Date.now(); + const deadline = Date.now() + TMO_MS; + let sawTimeout = false; + while (Date.now() < deadline) { + await sleep(SAMPLE_MS); + const st = pi.states.at(-1); + if (st?.isStreaming) lastActiveT = Date.now(); + if (st && (st.pendingMessageCount ?? 0) > 0) lastActiveT = Date.now(); + if (pi.states.length > 3 && Date.now() - lastActiveT >= QUIET_MS) break; + if (pi.proc.exitCode !== null) break; + } + if (Date.now() >= deadline) sawTimeout = true; + samplerAlive = false; + clearInterval(sampler); + await pi.waitResponse(pid, 5_000).catch(() => null); + const promptResp = pi.responses.get(pid); + check("prompt command accepted", promptResp?.success === true, JSON.stringify(promptResp ?? {}).slice(0, 200)); + check("no hard timeout", !sawTimeout, `elapsed=${Math.round((Date.now() - (lastActiveT - QUIET_MS)) / 1000)}s`); + pi.kill(); + await sleep(800); + try { fake.kill("SIGKILL"); } catch {} + await sleep(200); + + fs.writeFileSync(path.join(work, "events.jsonl"), pi.events.map((e) => JSON.stringify(e)).join("\n") + "\n"); + fs.writeFileSync(path.join(work, "states.jsonl"), pi.states.map((s) => JSON.stringify(s)).join("\n") + "\n"); + fs.writeFileSync(path.join(work, "pi.stderr.txt"), pi.stderr); + try { + fs.copyFileSync(path.join(ctx.xdg.state, "billion-context", "bili.log"), path.join(work, "bili.log")); + } catch {} + + const rows = readOracle(ctx.reqLog); + const delegateRow = rows.find((r) => r.toolName === "acp_delegate"); + check("parent dispatched acp_delegate (scripted)", !!delegateRow, delegateRow ? `conv=${delegateRow.conv}` : "no acp_delegate tool row"); + if (delegateRow) { + const parentConv = delegateRow.conv; + const prows = rows.filter((r) => r.conv === parentConv); + const notifFresh = prows.filter((r) => stripAcps(r.lastUserHead).startsWith("[acp_delegate") && String(r.roles ?? "").endsWith("user")); + check("exactly one fresh notification delivery", notifFresh.length === 1, `freshDeliveries=${notifFresh.length}, heads=${prows.map((r) => stripAcps(r.lastUserHead).slice(0, 40)).join(" | ")}`); + const R = notifFresh[0]; + if (R) { + if (name === "midtask") { + check(`notification consumed with work remaining (queueIdx < ${totalDirectives})`, R.queueIdx < totalDirectives, `queueIdx=${R.queueIdx}/${totalDirectives}, toolName=${R.toolName}`); + const laterTool = prows.some((r) => r.t > R.t && r.toolName === "bash"); + const needsLater = R.queueIdx < totalDirectives - 1; + check("run continued AFTER notification (later bash round)", needsLater ? laterTool : true, needsLater ? `laterBash=${laterTool}` : "notification landed on final directive — continuation n/a"); + const nearStreaming = pi.states.some((s) => s.isStreaming && Math.abs(s.t - R.t) <= 2500); + check("main task streaming around consumption", nearStreaming, `samplesNear=${JSON.stringify(pi.states.filter((s) => Math.abs(s.t - R.t) <= 2500).map((s) => ({ isStreaming: s.isStreaming, pending: s.pendingMessageCount })))}`); + // The busy tier commits straight into the model context: nothing may + // park in the host follow-up/steer queue during the task (parking is + // exactly the #2320 defect). + const maxPending = Math.max(0, ...pi.states.map((s) => s.pendingMessageCount ?? 0)); + check("no follow-up queue parking during the task (maxPending == 0)", maxPending === 0, `maxPending=${maxPending}`); + } else { + // Two valid delivery shapes, both proving "no further user input was + // needed": (a) IDLE tier — host already settled, steer auto-starts a + // new turn (nothing streaming before arrival); (b) BUSY tier — child + // finished before the initial run's final response, committed into + // that run's last model call (streaming still active around arrival). + const preArrival = pi.states.filter((s) => s.t >= R.t - 2500 && s.t <= R.t - 400); + const idleAround = preArrival.length > 0 && preArrival.every((s) => !s.isStreaming); + const busyCommit = preArrival.some((s) => s.isStreaming); + check("delivered with no further input (idle auto-start or in-run commit)", idleAround || busyCommit, `preArrivalSamples=${JSON.stringify(preArrival.map((s) => ({ isStreaming: s.isStreaming, pending: s.pendingMessageCount })))}`); + } + } + if (name === "idle") { + check("notification arrived after initial run ended (no re-prompting)", R ? prows.indexOf(R) >= totalDirectives : false, `rowOrder=${prows.map((r) => `q${r.queueIdx}:${r.toolName ?? "txt"}${stripAcps(r.lastUserHead).startsWith("[acp_delegate") ? ":NOTIF" : ""}`).join(",")}`); + } + } + await assertPortDead(fakePort); + } catch (e) { + failures.push(`scenario threw: ${e instanceof Error ? e.stack : String(e)}`); + } finally { + try { killBiliInstances(ctx); } catch {} + try { fake.kill("SIGKILL"); } catch {} + pi?.kill(); + } + if (failures.length > 0) { + throw new Error(`scenario "${name}" failed:\n ${failures.join("\n ")}\nartifacts: ${work}`); + } +} + +const SLEEP = '请调用bash {"command":"sleep 2"}'; +const DELEGATE = '请调用acp_delegate {"agent":"worker","task":"quick probe","async":true}'; + +// warmup bash + async delegate + 8 x sleep-2 rounds: the completion notice must +// land DURING the sleep rounds (queue not yet exhausted) and the run keeps going. +const PROMPT_MIDTASK = `任务:先热身,然后派发一个异步子任务并继续工作。 +${SLEEP};${DELEGATE};${SLEEP};${SLEEP};${SLEEP};${SLEEP};${SLEEP};${SLEEP};${SLEEP};${SLEEP};完成。`; +const TOTAL_MIDTASK = 10; + +// warmup bash + async delegate only: the main run ends, the agent goes idle; the +// late completion must still reach the model without any further input. +const PROMPT_IDLE = `任务:热身后派发一个异步子任务,然后结束本轮。 +${SLEEP};${DELEGATE};完成。`; +const TOTAL_IDLE = 2; + +function scenarioSkip(): string | false { + if (process.env.ACP_TEST_E2E_NATIVE !== "1") { + return "set ACP_TEST_E2E_NATIVE=1 (real pi RPC lane + local fake upstream; deterministic, zero tokens)"; + } + if (!fs.existsSync(PI_NATIVE_ENTRY)) return "dist/agent/pi-native.js missing — run `npm run build` first"; + if (!piAvailable()) return `${PI_BIN} CLI not available`; + return false; +} + +test("delegate completion reaches the model mid-task at the next safe boundary (#2320)", { skip: scenarioSkip(), timeout: TMO_MS + 60_000 }, async () => { + await runScenario("midtask", PROMPT_MIDTASK, TOTAL_MIDTASK); +}); + +test("late delegate completion reaches an idle model without further input (#2320)", { skip: scenarioSkip(), timeout: TMO_MS + 60_000 }, async () => { + await runScenario("idle", PROMPT_IDLE, TOTAL_IDLE); +});