diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 32956a0d495ff..71d35d0fa0bab 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -1334,7 +1334,7 @@ describe("AcpAdapterV2", () => { assert.equal(promptOrdinal, 2, "continuation turn must not dispatch session/prompt"); }).pipe(Effect.provide(layerTest)), ); - it.live("fails an end_turn the outcome check rejects and restarts before the next turn", () => + it.live("applies what the outcome check finds after an end_turn", () => Effect.gen(function* () { const checking = yield* Deferred.make(); const releaseCheck = yield* Deferred.make(); @@ -1373,7 +1373,7 @@ describe("AcpAdapterV2", () => { ? { failure: quota, threadDisposition: "reusable" as const } : watchedSessions.length >= 3 ? { failure: poisoned, threadDisposition: "broken" as const } - : undefined; + : { hiddenReply: "FINAL: done out of view" }; const interrupted = watchedSessions.length === 4; return () => interrupted @@ -1445,8 +1445,16 @@ describe("AcpAdapterV2", () => { return assert.fail("expected a failed terminal"); } assert.deepEqual(failed.failure, quota); - const completed = (yield* runTurn(2)).at(-1); + // Work the agent did without ACP updates reaches the turn as its reply. + const completedEvents = yield* runTurn(2); + const completed = completedEvents.at(-1); assert.equal(completed?.type === "turn.terminal" ? completed.status : null, "completed"); + const replies = completedEvents.flatMap((event) => + event.type === "turn_item.updated" && event.turnItem.type === "assistant_message" + ? [event.turnItem.text] + : [], + ); + assert.equal(replies.at(-1), "FINAL: done out of view"); assert.deepEqual(promptRuntimes, [1, 2], "the next turn runs on a fresh runtime"); // The fresh runtime has no record of what reached the agent, so it sends T3's notes again. assert.include(promptTexts[1] ?? "", ""); @@ -1510,6 +1518,81 @@ describe("AcpAdapterV2", () => { ); }).pipe(Effect.provide(layerTest)), ); + it.live("fails a turn whose outcome check is pending when the runtime terminates", () => + Effect.gen(function* () { + const checking = yield* Deferred.make(); + const releaseCheck = yield* Deferred.make(); + const runtimeInputs: AcpAdapterV2RuntimeInput[] = []; + const baseMakeRuntime = makeMockRuntime({ + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner, + mockAgentPath: yield* bundledMockAgentPath, + wrapRuntime: (runtime) => ({ + ...runtime, + prompt: () => Effect.succeed({ stopReason: "end_turn" as const }), + }), + }); + const instanceId = ProviderInstanceId.make("acp-test-outcome-termination"); + const threadId = ThreadId.make("thread-acp-outcome-termination"); + const adapter = yield* makeAcpAdapterV2({ + instanceId, + selfInvocation: yield* resolveSelfInvocation(), + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + // Follows work the agent does without ACP updates, as Kimi's check does. + watchPromptOutcome: () => + Effect.succeed(() => + Deferred.succeed(checking, undefined).pipe( + Effect.andThen(Deferred.await(releaseCheck)), + Effect.as(undefined), + ), + ), + makeRuntime: (runtimeInput) => + Effect.sync(() => { + runtimeInputs.push(runtimeInput); + }).pipe(Effect.andThen(baseMakeRuntime(runtimeInput))), + }, + }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "default" } as const; + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("provider-session-acp-outcome-termination"), + modelSelection, + runtimePolicy, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread, + instanceId, + runtimePolicy, + now: yield* DateTime.now, + }), + ); + const terminal = yield* runtime.events.pipe( + Stream.filter((event) => event.type === "turn.terminal"), + Stream.runHead, + Effect.forkChild, + ); + yield* Deferred.await(checking); + yield* runtimeInputs.at(-1)!.onTermination!( + new EffectAcpErrors.AcpTransportError({ detail: "Injected agent exit", cause: "test" }), + ); + const ended = Option.getOrUndefined(yield* Fiber.join(terminal)); + yield* Deferred.succeed(releaseCheck, undefined); + assert.equal(ended?.type === "turn.terminal" ? ended.status : null, "failed"); + }).pipe(Effect.provide(layerTest)), + ); it.live("retains returned usage when Stop finalizes before the prompt callback", () => Effect.gen(function* () { const instanceId = ProviderInstanceId.make("acp-test-prompt-usage-stop"); diff --git a/packages/provider-acp-registry/src/server/adapter.ts b/packages/provider-acp-registry/src/server/adapter.ts index ce5a2b94531c4..423413952489f 100644 --- a/packages/provider-acp-registry/src/server/adapter.ts +++ b/packages/provider-acp-registry/src/server/adapter.ts @@ -272,12 +272,13 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu watchPromptOutcome: (sessionId: string) => Effect.map( watchKimiPromptOutcome(sessionId), - (check) => (result: EffectAcpSchema.PromptResponse) => - Effect.map(check(result), (failure) => - failure === undefined - ? undefined - : { failure, threadDisposition: "reusable" as const }, - ), + (check) => + (result: EffectAcpSchema.PromptResponse, stopped: Effect.Effect) => + Effect.map(check(result, stopped), (outcome) => + outcome === undefined || !("failure" in outcome) + ? outcome + : { failure: outcome.failure, threadDisposition: "reusable" as const }, + ), ), }), ...(registryAgentId === "mistral-vibe" diff --git a/packages/provider-acp-registry/src/server/kimiAcp.test.ts b/packages/provider-acp-registry/src/server/kimiAcp.test.ts index e5c96750e5dae..fb1eb7d753b50 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.test.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.test.ts @@ -31,47 +31,144 @@ const failedTurn = (turnId: number) => const completedTurn = (turnId: number) => JSON.stringify({ type: "turn.ended", agentId: "main", turnId, reason: "completed" }) + "\n"; +const records = (...items: ReadonlyArray) => + items.map((record) => JSON.stringify(record)).join("\n") + "\n"; +const replyText = (text: string) => + records({ + type: "context.append_loop_event", + agentId: "main", + event: { type: "content.part", part: { type: "text", text } }, + }); +const turnPrompt = (text: string) => + records({ type: "turn.prompt", agentId: "main", input: [{ type: "text", text }] }); + +// Trimmed from Kimi Code 2.6.1's wire.jsonl: a background task finished mid-turn, so Kimi +// steered its notice in, ended the prompt's turn, and ran the notice as its own next turn. +const steeredTurn = + records( + { + type: "prompt.steered", + agentId: "main", + activePromptId: "msg_prompt", + promptIds: ["task:bash-1:completed"], + messageId: "task:bash-1:completed", + content: [{ type: "text", text: '' }], + }, + { type: "turn.ended", agentId: "main", turnId: 0, reason: "completed" }, + { type: "prompt.completed", agentId: "main", promptId: "msg_prompt", reason: "completed" }, + ) + turnPrompt(''); +const notStopped = Effect.succeed(false); + +const makeWire = Effect.fn("makeWire")(function* (before: string) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const home = yield* fileSystem.makeTempDirectoryScoped(); + const agentDirectory = path.join(home, "sessions", "wd_proj_1", SESSION_ID, "agents", "main"); + yield* fileSystem.makeDirectory(agentDirectory, { recursive: true }); + const wire = path.join(agentDirectory, "wire.jsonl"); + yield* fileSystem.writeFileString(wire, before); + // Signals once a read has closed the log, so the check is already waiting for its next read. + const polls = yield* Queue.unbounded(); + const watch = yield* makeKimiPromptOutcomeWatch(home).pipe( + Effect.provideService(FileSystem.FileSystem, { + ...fileSystem, + open: (path, options) => + Effect.addFinalizer(() => Queue.offer(polls, undefined)).pipe( + Effect.andThen(fileSystem.open(path, options)), + ), + }), + ); + const check = yield* watch(SESSION_ID); + // The watch reads the log once to learn whether Kimi's own turn is still open. + yield* Queue.take(polls); + const append = (text: string) => fileSystem.writeFileString(wire, text, { flag: "a" }); + const nextPoll = (interval: "50 millis" | "250 millis") => + TestClock.adjust(interval).pipe(Effect.andThen(Queue.take(polls))); + return { check, append, polls, nextPoll }; +}); + it.layer(NodeServices.layer)("makeKimiPromptOutcomeWatch", (it) => { - it.effect("fails only the turn this prompt ended, once Kimi writes its outcome", () => + // regression: #36163 Kimi answers session/prompt with end_turn while it keeps working + it.effect("follows a steered hand-off until Kimi is idle and returns its reply", () => Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const home = yield* fileSystem.makeTempDirectoryScoped(); - const agentDirectory = path.join(home, "sessions", "wd_proj_1", SESSION_ID, "agents", "main"); - yield* fileSystem.makeDirectory(agentDirectory, { recursive: true }); - const wire = path.join(agentDirectory, "wire.jsonl"); - // An earlier turn's failure is history, not this prompt's outcome. - yield* fileSystem.writeFileString(wire, failedTurn(0)); - const append = (text: string) => fileSystem.writeFileString(wire, text, { flag: "a" }); - // Signals once a poll has closed the log, so the check is already waiting for its next poll. - const polls = yield* Queue.unbounded(); - const watch = yield* makeKimiPromptOutcomeWatch(home).pipe( - Effect.provideService(FileSystem.FileSystem, { - ...fileSystem, - open: (path, options) => - Effect.addFinalizer(() => Queue.offer(polls, undefined)).pipe( - Effect.andThen(fileSystem.open(path, options)), - ), - }), - ); + const { check, append, polls, nextPoll } = yield* makeWire(completedTurn(0)); + yield* append(steeredTurn + replyText("FINAL: all steps done") + completedTurn(1)); + const outcome = yield* check(endTurn, notStopped).pipe(Effect.forkChild); + yield* Queue.take(polls); + yield* nextPoll("250 millis"); + yield* nextPoll("250 millis"); + assert.deepStrictEqual(yield* Fiber.join(outcome), { + hiddenReply: "FINAL: all steps done", + }); + }), + ); - const completed = yield* watch(SESSION_ID); - yield* append(completedTurn(1)); - assert.isUndefined(yield* completed(endTurn)); + // Sent while that steered turn runs: Kimi queues it and answers at once, writing nothing. + it.effect( + "waits out Kimi's own turn for a prompt it only queued, and returns only its reply", + () => + Effect.gen(function* () { + const { check, append, nextPoll } = yield* makeWire(steeredTurn); + const outcome = yield* check(endTurn, notStopped).pipe(Effect.forkChild); + yield* nextPoll("250 millis"); + yield* nextPoll("250 millis"); + yield* nextPoll("250 millis"); + yield* append(replyText("FINAL: all steps done") + completedTurn(1)); + // The queued prompt's own turn can start well after Kimi's turn ends. + yield* nextPoll("250 millis"); + yield* nextPoll("250 millis"); + yield* nextPoll("250 millis"); + yield* append( + turnPrompt("Reply with the single word PONG.") + replyText("PONG") + completedTurn(2), + ); + yield* nextPoll("250 millis"); + yield* nextPoll("250 millis"); + yield* nextPoll("250 millis"); + assert.deepStrictEqual(yield* Fiber.join(outcome), { + hiddenReply: "PONG", + }); + }), + ); + + it.effect("stops following Kimi's work once the turn stops", () => + Effect.gen(function* () { + const { check } = yield* makeWire(steeredTurn); + assert.isUndefined(yield* check(endTurn, Effect.succeed(true))); + }), + ); + + // Slash commands such as /compact answer without starting a turn. + it.effect("settles a slash command that answers with no turn", () => + Effect.gen(function* () { + const { check, polls, nextPoll } = yield* makeWire(completedTurn(0)); + const outcome = yield* check(endTurn, notStopped).pipe(Effect.forkChild); yield* Queue.take(polls); + for (let poll = 2; poll <= 41; poll++) yield* nextPoll("50 millis"); + assert.isUndefined(yield* Fiber.join(outcome)); + }), + ); + + it.effect("fails only the turn this prompt ended, once Kimi writes its outcome", () => + Effect.gen(function* () { + // An earlier turn's failure is history, not this prompt's outcome. + const completed = yield* makeWire(failedTurn(0)); + yield* completed.append(completedTurn(1)); + assert.isUndefined(yield* completed.check(endTurn, notStopped)); // Kimi answers the prompt before `turn.ended` reaches disk. - const failed = yield* watch(SESSION_ID); - yield* append(failedTurn(2).slice(0, 40)); - const pending = yield* failed(endTurn).pipe(Effect.forkChild); - yield* Queue.take(polls); - yield* append(failedTurn(2).slice(40)); + const failed = yield* makeWire(failedTurn(0) + completedTurn(1)); + yield* failed.append(failedTurn(2).slice(0, 40)); + const pending = yield* failed.check(endTurn, notStopped).pipe(Effect.forkChild); + yield* Queue.take(failed.polls); + yield* failed.append(failedTurn(2).slice(40)); yield* TestClock.adjust("50 millis"); assert.deepStrictEqual(yield* Fiber.join(pending), { - class: "provider_error", - message: "upstream overloaded", - code: "provider.overloaded", - retryable: true, + failure: { + class: "provider_error", + message: "upstream overloaded", + code: "provider.overloaded", + retryable: true, + }, }); }), ); diff --git a/packages/provider-acp-registry/src/server/kimiAcp.ts b/packages/provider-acp-registry/src/server/kimiAcp.ts index 70b59082feab6..c6cbbe9523029 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.ts @@ -5,12 +5,16 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; -import * as Schedule from "effect/Schedule"; import * as Schema from "effect/Schema"; import type * as EffectAcpSchema from "effect-acp/compat"; const NATIVE_SESSION_ID = /^[\w-]+$/; const READ_CHUNK_BYTES = 65_536; +const OUTCOME_POLLS = 41; +const OUTCOME_POLL_INTERVAL = "50 millis"; +const HIDDEN_WORK_POLL_INTERVAL = "250 millis"; +// Kimi writes the next queued turn's `turn.prompt` right after the last one ends. +const HIDDEN_WORK_IDLE_POLLS = 2; const KimiTurnEnded = Schema.fromJsonString( Schema.Struct({ @@ -28,11 +32,40 @@ const KimiTurnEnded = Schema.fromJsonString( ); const decodeTurnEnded = Schema.decodeUnknownOption(KimiTurnEnded); type KimiTurnEnded = typeof KimiTurnEnded.Type; +const decodeTurnBoundary = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ + type: Schema.Literals(["turn.prompt", "turn.ended"]), + agentId: Schema.Literal("main"), + }), + ), +); +const decodePromptSteered = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ type: Schema.Literal("prompt.steered"), agentId: Schema.Literal("main") }), + ), +); + +const decodeTextPart = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ + type: Schema.Literal("context.append_loop_event"), + agentId: Schema.Literal("main"), + event: Schema.Struct({ + type: Schema.Literal("content.part"), + part: Schema.Struct({ type: Schema.Literal("text"), text: Schema.String }), + }), + }), + ), +); -class KimiTurnNotEnded extends Schema.TaggedError()("KimiTurnNotEnded", {}) {} +/** A failure Kimi's stop reason hid, or the reply of turns Kimi ran without ACP updates. */ +export type KimiPromptOutcome = + | { readonly failure: OrchestrationV2ProviderFailure } + | { readonly hiddenReply: string }; -const noOutcome = (_result: EffectAcpSchema.PromptResponse) => - Effect.succeed(undefined); +const noOutcome = (_result: EffectAcpSchema.PromptResponse, _stopped: Effect.Effect) => + Effect.succeed(undefined); function turnFailure(ended: KimiTurnEnded): OrchestrationV2ProviderFailure | undefined { if (ended.reason !== "failed") return undefined; @@ -51,9 +84,10 @@ function turnFailure(ended: KimiTurnEnded): OrchestrationV2ProviderFailure | und } /** - * Kimi Code (2.5.3, 2.6.1) answers a failed turn with `end_turn` and no error. Its main - * agent's `wire.jsonl` records the real outcome, so the check reads the `turn.ended` - * this prompt appended. + * Kimi Code answers failed, steered-away and queued prompts with `end_turn`, and runs the + * work after a steer or queue without ACP updates. Its main agent's `wire.jsonl` shows what + * happened, so the check follows it until Kimi is idle and returns that work's reply. Slash + * commands answer without a turn, so only a prompt sent while a turn is open counts as queued. */ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(function* ( configuredHome: string | undefined, @@ -83,15 +117,42 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func Effect.orElseSucceed(() => 0n), ) : 0n; + // Reads back from the end until the last main-agent `turn.prompt` or `turn.ended`. + const turnOpen = Option.isNone(before) + ? false + : yield* Effect.scoped( + Effect.gen(function* () { + const file = yield* fileSystem.open(before.value, { flag: "r" }); + let position = startOffset; + let carry = Buffer.alloc(0); + while (position > 0n) { + const start = + position > BigInt(READ_CHUNK_BYTES) ? position - BigInt(READ_CHUNK_BYTES) : 0n; + const chunk = yield* file.readAlloc(Number(position - start), { position: start }); + if (Option.isNone(chunk)) return false; + const lines = Buffer.concat([chunk.value, carry]).toString("utf8").split("\n"); + carry = start > 0n ? Buffer.from(lines.shift() ?? "", "utf8") : carry; + for (const line of lines.reverse()) { + if (!line.includes('"turn.prompt"') && !line.includes('"turn.ended"')) continue; + const record = decodeTurnBoundary(line); + if (Option.isSome(record)) return record.value.type === "turn.prompt"; + } + position = start; + } + return false; + }), + ).pipe(Effect.orElseSucceed(() => false)); - return Effect.fn("KimiTurnOutcome.check")(function* (result: EffectAcpSchema.PromptResponse) { + return Effect.fn("KimiTurnOutcome.check")(function* ( + result: EffectAcpSchema.PromptResponse, + stopped: Effect.Effect, + ) { if (result.stopReason !== "end_turn") return undefined; // A new session's log can first appear with its first prompt. const wire = Option.isSome(before) ? before : yield* findWire; if (Option.isNone(wire)) return undefined; let offset = startOffset; - let ended: KimiTurnEnded | undefined; - // Each poll decodes only the complete lines appended since the last one. + // Each read returns only the complete lines appended since the last one. const readAppended = Effect.scoped( Effect.gen(function* () { const file = yield* fileSystem.open(wire.value, { flag: "r" }); @@ -106,26 +167,94 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func const bytes = Buffer.concat(chunks); const complete = bytes.lastIndexOf(0x0a) + 1; offset += BigInt(complete); - for (const line of bytes.subarray(0, complete).toString("utf8").split("\n")) { - if (!line.includes('"turn.ended"')) continue; - const record = decodeTurnEnded(line); - if (Option.isSome(record)) ended = record.value; - } - if (ended === undefined) return yield* new KimiTurnNotEnded(); - return ended; + return bytes.subarray(0, complete).toString("utf8").split("\n").filter(Boolean); }), ); - // Kimi writes `turn.ended` on a microtask after it answers the prompt. - return yield* readAppended.pipe( - Effect.retry({ - times: 40, - schedule: Schedule.spaced("50 millis"), - while: (error) => error._tag === "KimiTurnNotEnded", - }), - Effect.map(turnFailure), + + let ended: KimiTurnEnded | undefined; + let steered = false; + let open = turnOpen; + // A queued prompt's work starts at its own `turn.prompt`, after Kimi's open turn ends. + let queuedStarted = false; + let followingHiddenWork = false; + let lastEnded: KimiTurnEnded | undefined; + const replies: Array = []; + const follow = (lines: ReadonlyArray) => { + for (const line of lines) { + if (ended === undefined && !turnOpen && line.includes('"prompt.steered"')) { + steered ||= Option.isSome(decodePromptSteered(line)); + } + if (line.includes('"turn.prompt"') || line.includes('"turn.ended"')) { + const boundary = decodeTurnBoundary(line); + if (Option.isNone(boundary)) continue; + const wasOpen = open; + open = boundary.value.type === "turn.prompt"; + if (open) { + queuedStarted ||= turnOpen && !wasOpen; + continue; + } + const record = decodeTurnEnded(line); + if (Option.isNone(record)) continue; + if (!turnOpen || queuedStarted) lastEnded = record.value; + ended ??= record.value; + } else if ( + line.includes('"content.part"') && + (turnOpen ? queuedStarted : ended !== undefined) + ) { + const part = decodeTextPart(line); + if (Option.isSome(part)) replies.push(part.value.event.part.text); + } + } + }; + + return yield* Effect.gen(function* () { + // Kimi writes `turn.ended` on a microtask after it answers the prompt. + if (!turnOpen) { + for (let poll = 1; poll <= OUTCOME_POLLS; poll++) { + if (poll > 1) yield* Effect.sleep(OUTCOME_POLL_INTERVAL); + follow(yield* readAppended); + if (ended !== undefined) break; + } + } + if (!turnOpen && (!steered || (ended !== undefined && ended.reason !== "completed"))) { + if (ended === undefined) { + yield* Effect.logWarning("Kimi's session log has no outcome for this turn."); + return undefined; + } + const failure = turnFailure(ended); + return failure === undefined ? undefined : { failure }; + } + // A steered notice or a queued prompt runs in turns Kimi reports only to its log. + followingHiddenWork = true; + for (let quiet = 0; quiet < HIDDEN_WORK_IDLE_POLLS;) { + if (yield* stopped) break; + yield* Effect.sleep(HIDDEN_WORK_POLL_INTERVAL); + const lines = yield* readAppended; + follow(lines); + // A queued prompt stays pending until its own turn has run. + const pending = open || (turnOpen && !queuedStarted); + quiet = pending || lines.length > 0 ? 0 : quiet + 1; + } + const failure = lastEnded === undefined ? undefined : turnFailure(lastEnded); + if (failure !== undefined) return { failure }; + const hiddenReply = replies.join("\n\n").trim(); + return hiddenReply.length === 0 ? undefined : { hiddenReply }; + }).pipe( Effect.catch(() => - Effect.logWarning("Kimi's session log has no outcome for this turn.").pipe( - Effect.as(undefined), + Effect.logWarning("Kimi's session log could not be read for this turn.").pipe( + Effect.as( + followingHiddenWork + ? { + failure: makeProviderFailure({ + class: "provider_error", + message: + "Kimi kept working after it answered, and T3 Code lost track of that work.", + code: null, + retryable: null, + }), + } + : undefined, + ), ), ), ); diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index e2dc68fc13fdb..68200f59e0630 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -341,10 +341,12 @@ export function acpPerTurnPromptUsage( }; } -interface AcpPromptOutcomeFailure { - readonly failure: OrchestrationV2ProviderFailure; - readonly threadDisposition: "reusable" | "broken"; -} +type AcpPromptOutcome = + | { + readonly failure: OrchestrationV2ProviderFailure; + readonly threadDisposition: "reusable" | "broken"; + } + | { readonly hiddenReply: string }; export interface AcpAdapterV2Flavor { readonly buildAttachmentPrompt?: (input: { @@ -358,15 +360,17 @@ export interface AcpAdapterV2Flavor { }) => Effect.Effect, EffectAcpErrors.AcpError>; readonly promptFailure?: (cause: unknown) => OrchestrationV2ProviderFailure; /** - * Runs before `session/prompt`. The returned check sees a prompt the agent ended - * without an error or cancel, and reports a failure its stop reason hid. A reported - * failure restarts the runtime before the next turn. A `broken` thread's native - * history is unusable, so the next turn falls back to a fresh session. + * Runs before `session/prompt`; the check sees an `end_turn` and reports a hidden failure + * (restarting the runtime; `broken` falls back to a fresh session), or the reply of work + * the agent did without ACP updates, which it may follow until `stopped`. */ readonly watchPromptOutcome?: ( sessionId: string, ) => Effect.Effect< - (result: EffectAcpSchema.PromptResponse) => Effect.Effect + ( + result: EffectAcpSchema.PromptResponse, + stopped: Effect.Effect, + ) => Effect.Effect >; readonly requiresSignIn?: (cause: unknown) => boolean; /** @@ -1620,6 +1624,8 @@ interface ActiveAcpTurn { finalizedStatus: "completed" | "interrupted" | "failed" | "cancelled" | null; promptSettled: boolean; promptSettledStatus: "completed" | "interrupted" | "failed" | "cancelled" | null; + /** The prompt returned and its outcome check may still be following the agent's work. */ + checkingPromptOutcome: boolean; workAccepted: boolean; readonly promptWireSettled: Deferred.Deferred; /** Completed once `session/prompt` is the runtime's active prompt. */ @@ -2648,6 +2654,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( // An open prompt fails with its transport. A turn held open for a running tool // has no prompt, so nothing else ends it. const held = yield* Ref.get(activeTurn); + if (held?.checkingPromptOutcome === true && !held.finalized) { + yield* finalizeTurn( + held, + "failed", + makeProviderFailure({ class: "transport_error" }), + ); + return; + } if ( flavor.toolOutlivesPrompt === undefined || held === null || @@ -8961,6 +8975,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( finalizedStatus: null, promptSettled: false, promptSettledStatus: null, + checkingPromptOutcome: false, workAccepted: false, promptWireSettled, promptDispatched, @@ -9166,108 +9181,139 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ), Effect.tap(() => options.testHooks?.afterPromptWireSettled?.() ?? Effect.void), Effect.flatMap((result) => - runRuntimeCallbackAtGeneration( - promptGeneration, - Effect.gen(function* () { - // Record delivery only for this runtime: a replacement can reuse the session id. - // Grok cancels locally, refusals discard the prompt, and errors need a retry. - // Native compaction sends no prompt, and the compacted session needs the notes. - if (compactSession !== undefined) { - yield* markSessionCompacted(requestedSessionId); - } else if ( - result.stopReason !== "cancelled" && - result.stopReason !== "refusal" && - result.stopReason !== "error" - ) { - yield* Ref.update(promptInstructionStates, (current) => { - if (promptParts?.instructionState === undefined) return current; - const updated = new Map(current); - updated.set(requestedSessionId, promptParts.instructionState); - return updated; - }); - yield* Ref.update(promptRuntimeInstructions, (current) => { - if (promptParts?.runtimeInstructions === undefined) return current; - const updated = new Map(current); - updated.set(requestedSessionId, promptParts.runtimeInstructions); - return updated; - }); - yield* Ref.update(compactionMarkers, (current) => { - if (promptParts?.instructionState === undefined) return current; - const observedMarker = promptParts.compactionMarker; - if ( - observedMarker === undefined || - current.get(requestedSessionId) !== observedMarker - ) - return current; - const updated = new Map(current); - updated.delete(requestedSessionId); - return updated; - }); - } - if (context.finalized || followsAgentWorkState) return; - // ACP v2 reports a failed prompt through its idle stop reason, not an RPC error. - const hiddenFailure = - checkPromptOutcome === undefined || - context.interrupted || - result.stopReason === "error" || - result.stopReason === "cancelled" - ? undefined - : yield* checkPromptOutcome(result); - if (hiddenFailure !== undefined) { - yield* Ref.set(runtimeRestartRequired, true); - } - const failure = - hiddenFailure?.failure ?? - (context.interrupted - ? undefined - : stopReasonFailure(result.stopReason, result.error)); - const status = - failure !== undefined - ? "failed" - : result.stopReason === "cancelled" - ? context.interrupted - ? "interrupted" - : "cancelled" - : "completed"; - if (defersFinalizeForBackgroundWork && !context.interrupted) { - context.promptSettled = true; - context.promptSettledStatus = status; - for (const [taskId, completion] of context.backgroundCommands) { - if (completion === null) continue; - yield* finishRegisteredBackgroundTool({ taskId, ...completion }); + Effect.gen(function* () { + // Outside the callback permit: the check can follow work the agent keeps + // doing without ACP updates until this turn stops. + const checksPromptOutcome = + checkPromptOutcome !== undefined && + !context.finalized && + !followsAgentWorkState && + !context.interrupted && + result.stopReason !== "error" && + result.stopReason !== "cancelled"; + context.checkingPromptOutcome = checksPromptOutcome; + const promptOutcome = checksPromptOutcome + ? yield* checkPromptOutcome( + result, + Effect.map( + Ref.get(runtimeCallbackGeneration), + (generation) => + generation !== promptGeneration || + context.finalized || + context.interrupted, + ), + ) + : undefined; + return yield* runRuntimeCallbackAtGeneration( + promptGeneration, + Effect.gen(function* () { + // Record delivery only for this runtime: a replacement can reuse the session id. + // Grok cancels locally, refusals discard the prompt, and errors need a retry. + // Native compaction sends no prompt, and the compacted session needs the notes. + if (compactSession !== undefined) { + yield* markSessionCompacted(requestedSessionId); + } else if ( + result.stopReason !== "cancelled" && + result.stopReason !== "refusal" && + result.stopReason !== "error" + ) { + yield* Ref.update(promptInstructionStates, (current) => { + if (promptParts?.instructionState === undefined) return current; + const updated = new Map(current); + updated.set(requestedSessionId, promptParts.instructionState); + return updated; + }); + yield* Ref.update(promptRuntimeInstructions, (current) => { + if (promptParts?.runtimeInstructions === undefined) return current; + const updated = new Map(current); + updated.set(requestedSessionId, promptParts.runtimeInstructions); + return updated; + }); + yield* Ref.update(compactionMarkers, (current) => { + if (promptParts?.instructionState === undefined) return current; + const observedMarker = promptParts.compactionMarker; + if ( + observedMarker === undefined || + current.get(requestedSessionId) !== observedMarker + ) + return current; + const updated = new Map(current); + updated.delete(requestedSessionId); + return updated; + }); } - } - if (failure !== undefined) { - yield* terminalizeOpenForegroundTools(context, "failed"); - // Unlike an RPC failure, the agent survives an idle error, so its - // background tools and subagents keep reporting to this run. - yield* finalizeTurn(context, "failed", failure, { - childTurnsContinue: hasSurvivingBackgroundWork(context), - ...(hiddenFailure === undefined - ? {} - : { threadDisposition: hiddenFailure.threadDisposition }), - }); - return; - } - yield* idleReportedSubagents(context); - if (context.finalized) return; - if ( - defersFinalizeForBackgroundWork && - !context.interrupted && - hasDeferredBackgroundWork(context) - ) { - yield* closeTextStreams(context); - yield* ( - options.testHooks?.afterPromptSettledWithBackgroundWork?.() ?? Effect.void - ); - return; - } - yield* finalizeTurn(context, status); - }), - ).pipe( - Effect.andThen(() => options.testHooks?.afterPromptCallback?.() ?? Effect.void), - Effect.asVoid, - ), + if (context.finalized || followsAgentWorkState) return; + // ACP v2 reports a failed prompt through its idle stop reason, not an RPC error. + const hiddenFailure = + promptOutcome !== undefined && "failure" in promptOutcome + ? promptOutcome + : undefined; + if (promptOutcome !== undefined && "hiddenReply" in promptOutcome) { + yield* handleSessionUpdate({ + sessionId: requestedSessionId, + update: { + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: promptOutcome.hiddenReply }, + }, + }); + } + const stoppedDuringCheck = checksPromptOutcome && context.interrupted; + if (hiddenFailure !== undefined) { + yield* Ref.set(runtimeRestartRequired, true); + } + const failure = + hiddenFailure?.failure ?? + (context.interrupted + ? undefined + : stopReasonFailure(result.stopReason, result.error)); + const status = + failure !== undefined + ? "failed" + : result.stopReason === "cancelled" || stoppedDuringCheck + ? context.interrupted + ? "interrupted" + : "cancelled" + : "completed"; + if (defersFinalizeForBackgroundWork && !context.interrupted) { + context.promptSettled = true; + context.promptSettledStatus = status; + for (const [taskId, completion] of context.backgroundCommands) { + if (completion === null) continue; + yield* finishRegisteredBackgroundTool({ taskId, ...completion }); + } + } + if (failure !== undefined) { + yield* terminalizeOpenForegroundTools(context, "failed"); + // Unlike an RPC failure, the agent survives an idle error, so its + // background tools and subagents keep reporting to this run. + yield* finalizeTurn(context, "failed", failure, { + childTurnsContinue: hasSurvivingBackgroundWork(context), + ...(hiddenFailure === undefined + ? {} + : { threadDisposition: hiddenFailure.threadDisposition }), + }); + return; + } + yield* idleReportedSubagents(context); + if (context.finalized) return; + if ( + defersFinalizeForBackgroundWork && + !context.interrupted && + hasDeferredBackgroundWork(context) + ) { + yield* closeTextStreams(context); + yield* ( + options.testHooks?.afterPromptSettledWithBackgroundWork?.() ?? Effect.void + ); + return; + } + yield* finalizeTurn(context, status); + }), + ).pipe( + Effect.andThen(() => options.testHooks?.afterPromptCallback?.() ?? Effect.void), + Effect.asVoid, + ); + }), ), Effect.catchCause((cause) => runRuntimeCallbackAtGeneration(