From 3fc6d32d0cf8351ce71880fc411228b04d926b67 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 08:13:29 +0000 Subject: [PATCH 1/6] test(server): Kimi prompts that end before Kimi's work do not settle as completed Failing repro for #36163. Kimi Code 2.6.1 answers session/prompt with end_turn when a steered notice ends the prompt's turn, and answers a prompt it only queued at once; the outcome watch reports both as clean completions. Co-Authored-By: Claude Opus 5.5 --- .../src/server/kimiAcp.test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/packages/provider-acp-registry/src/server/kimiAcp.test.ts b/packages/provider-acp-registry/src/server/kimiAcp.test.ts index e5c96750e5dae..2aa5339876711 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.test.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.test.ts @@ -31,7 +31,66 @@ const failedTurn = (turnId: number) => const completedTurn = (turnId: number) => JSON.stringify({ type: "turn.ended", agentId: "main", turnId, reason: "completed" }) + "\n"; +// 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 = + [ + { + 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" }, + { + type: "turn.prompt", + agentId: "main", + input: [{ type: "text", text: '' }], + }, + ] + .map((record) => JSON.stringify(record)) + .join("\n") + "\n"; + it.layer(NodeServices.layer)("makeKimiPromptOutcomeWatch", (it) => { + // regression: #36163 Kimi answers session/prompt with end_turn while it keeps working + it.effect.each([ + { case: "the prompt's turn handed off to a steered turn", appended: steeredTurn, polls: 1 }, + // Sent while that steered turn runs: Kimi queues it and answers at once, writing nothing. + { case: "the prompt was only queued behind Kimi's own turn", appended: "", polls: 41 }, + ])("does not settle as completed when $case", ({ appended, polls: pollCount }) => + 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"); + yield* fileSystem.writeFileString(wire, completedTurn(0)); + 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); + yield* fileSystem.writeFileString(wire, appended, { flag: "a" }); + const outcome = yield* check(endTurn).pipe(Effect.forkChild); + for (let poll = 1; poll < pollCount; poll++) { + yield* Queue.take(polls); + yield* TestClock.adjust("50 millis"); + } + assert.isDefined(yield* Fiber.join(outcome)); + }), + ); + it.effect("fails only the turn this prompt ended, once Kimi writes its outcome", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; From 861a3516e5af9b97da96a00330dbf33b58a66f24 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 08:20:03 +0000 Subject: [PATCH 2/6] fix(server): Kimi turns that end before Kimi's work no longer settle as completed Kimi Code 2.6.1 answers session/prompt with end_turn when a steered background-task notice ends the prompt's turn, and at once for a prompt it only queues, then runs that work without sending session/update. The outcome watch now reports both as a provider failure instead of a clean completion. Fixes #36163 Co-Authored-By: Claude Opus 5.5 --- .../src/server/kimiAcp.ts | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/provider-acp-registry/src/server/kimiAcp.ts b/packages/provider-acp-registry/src/server/kimiAcp.ts index 70b59082feab6..a76d6582165d7 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.ts @@ -28,13 +28,27 @@ const KimiTurnEnded = Schema.fromJsonString( ); const decodeTurnEnded = Schema.decodeUnknownOption(KimiTurnEnded); type KimiTurnEnded = typeof KimiTurnEnded.Type; +const decodePromptSteered = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ type: Schema.Literal("prompt.steered"), agentId: Schema.Literal("main") }), + ), +); class KimiTurnNotEnded extends Schema.TaggedError()("KimiTurnNotEnded", {}) {} const noOutcome = (_result: EffectAcpSchema.PromptResponse) => Effect.succeed(undefined); -function turnFailure(ended: KimiTurnEnded): OrchestrationV2ProviderFailure | undefined { +// Kimi runs that work in turns it never reports over ACP, so T3 cannot show it. +const hiddenWorkFailure = (message: string) => + makeProviderFailure({ message, code: null, class: "provider_error", retryable: false }); + +function turnFailure(ended: KimiTurnEnded, steered: boolean) { + if (steered && ended.reason === "completed") { + return hiddenWorkFailure( + "Kimi ended this turn early to handle a background task and kept working out of view. Its further output can't be shown here.", + ); + } if (ended.reason !== "failed") return undefined; const code = ended.error?.code; return makeProviderFailure({ @@ -53,7 +67,8 @@ 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. + * this prompt appended. Kimi 2.6.1 also answers `end_turn` when a steered notice ends the + * turn, and at once for a prompt it only queues; neither is a completed turn. */ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(function* ( configuredHome: string | undefined, @@ -91,6 +106,7 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func if (Option.isNone(wire)) return undefined; let offset = startOffset; let ended: KimiTurnEnded | undefined; + let steered = false; // Each poll decodes only the complete lines appended since the last one. const readAppended = Effect.scoped( Effect.gen(function* () { @@ -107,9 +123,12 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func const complete = bytes.lastIndexOf(0x0a) + 1; offset += BigInt(complete); for (const line of bytes.subarray(0, complete).toString("utf8").split("\n")) { + if (ended === undefined && line.includes('"prompt.steered"')) { + steered ||= Option.isSome(decodePromptSteered(line)); + } if (!line.includes('"turn.ended"')) continue; const record = decodeTurnEnded(line); - if (Option.isSome(record)) ended = record.value; + if (Option.isSome(record)) ended ??= record.value; } if (ended === undefined) return yield* new KimiTurnNotEnded(); return ended; @@ -122,7 +141,15 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func schedule: Schedule.spaced("50 millis"), while: (error) => error._tag === "KimiTurnNotEnded", }), - Effect.map(turnFailure), + Effect.map((ended) => turnFailure(ended, steered)), + Effect.catchTags({ + KimiTurnNotEnded: () => + Effect.succeed( + hiddenWorkFailure( + "Kimi queued this message behind work it was still doing and answered before running it. Its reply can't be shown here.", + ), + ), + }), Effect.catch(() => Effect.logWarning("Kimi's session log has no outcome for this turn.").pipe( Effect.as(undefined), From f97600b4050bfb172a9bbc0e43585862182b8b6e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 08:33:04 +0000 Subject: [PATCH 3/6] fix(server): Kimi slash commands no longer report a queued message Kimi answers its own slash commands (/compact, /status, /help, /usage) with end_turn and no turn.ended. The outcome watch reported every such answer as a message Kimi only queued, which also restarted the Kimi runtime and could kill a /compact it had just started. Report a queued message only when Kimi's log shows its own turn still open when the prompt was sent, and while that turn is open, count only a turn.ended after this prompt's own turn.prompt. Co-Authored-By: Claude Opus 5.5 --- .../src/server/kimiAcp.test.ts | 99 +++++++++++++------ .../src/server/kimiAcp.ts | 62 ++++++++++-- 2 files changed, 122 insertions(+), 39 deletions(-) diff --git a/packages/provider-acp-registry/src/server/kimiAcp.test.ts b/packages/provider-acp-registry/src/server/kimiAcp.test.ts index 2aa5339876711..91765e3063351 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.test.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.test.ts @@ -57,38 +57,77 @@ const steeredTurn = it.layer(NodeServices.layer)("makeKimiPromptOutcomeWatch", (it) => { // regression: #36163 Kimi answers session/prompt with end_turn while it keeps working it.effect.each([ - { case: "the prompt's turn handed off to a steered turn", appended: steeredTurn, polls: 1 }, + { + case: "the prompt's turn handed off to a steered turn", + before: completedTurn(0), + appended: steeredTurn, + polls: 1, + settles: false, + }, // Sent while that steered turn runs: Kimi queues it and answers at once, writing nothing. - { case: "the prompt was only queued behind Kimi's own turn", appended: "", polls: 41 }, - ])("does not settle as completed when $case", ({ appended, polls: pollCount }) => - 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"); - yield* fileSystem.writeFileString(wire, completedTurn(0)); - 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)), - ), - }), - ); + { + case: "the prompt was only queued behind Kimi's own turn", + before: steeredTurn, + appended: "", + polls: 41, + settles: false, + }, + // Its own turn ends only after the reply; this prompt's turn has not started. + { + case: "Kimi's own turn ends after a queued prompt is answered", + before: steeredTurn, + appended: completedTurn(1), + polls: 41, + settles: false, + }, + // Slash commands such as /compact answer without starting a turn. + { + case: "a slash command answers with no turn", + before: completedTurn(0), + appended: "", + polls: 41, + settles: true, + }, + ])( + "settles as completed: $settles when $case", + ({ before, appended, polls: pollCount, settles }) => + 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"); + yield* fileSystem.writeFileString(wire, before); + 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); - yield* fileSystem.writeFileString(wire, appended, { flag: "a" }); - const outcome = yield* check(endTurn).pipe(Effect.forkChild); - for (let poll = 1; poll < pollCount; poll++) { + 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); - yield* TestClock.adjust("50 millis"); - } - assert.isDefined(yield* Fiber.join(outcome)); - }), + yield* fileSystem.writeFileString(wire, appended, { flag: "a" }); + const outcome = yield* check(endTurn).pipe(Effect.forkChild); + for (let poll = 1; poll < pollCount; poll++) { + yield* Queue.take(polls); + yield* TestClock.adjust("50 millis"); + } + assert.strictEqual((yield* Fiber.join(outcome)) === undefined, settles); + }), ); it.effect("fails only the turn this prompt ended, once Kimi writes its outcome", () => @@ -115,12 +154,14 @@ it.layer(NodeServices.layer)("makeKimiPromptOutcomeWatch", (it) => { ); const completed = yield* watch(SESSION_ID); + yield* Queue.take(polls); yield* append(completedTurn(1)); assert.isUndefined(yield* completed(endTurn)); yield* Queue.take(polls); // Kimi answers the prompt before `turn.ended` reaches disk. const failed = yield* watch(SESSION_ID); + yield* Queue.take(polls); yield* append(failedTurn(2).slice(0, 40)); const pending = yield* failed(endTurn).pipe(Effect.forkChild); yield* Queue.take(polls); diff --git a/packages/provider-acp-registry/src/server/kimiAcp.ts b/packages/provider-acp-registry/src/server/kimiAcp.ts index a76d6582165d7..4acec7cd4cac5 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.ts @@ -28,6 +28,14 @@ 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") }), @@ -65,10 +73,9 @@ function turnFailure(ended: KimiTurnEnded, steered: boolean) { } /** - * 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 2.6.1 also answers `end_turn` when a steered notice ends the - * turn, and at once for a prompt it only queues; neither is a completed turn. + * Kimi Code answers failed, steered-away and queued prompts with `end_turn`, and runs that + * work without ACP updates. Its main agent's `wire.jsonl` shows what happened. 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, @@ -98,6 +105,31 @@ 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) { if (result.stopReason !== "end_turn") return undefined; @@ -107,6 +139,8 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func let offset = startOffset; let ended: KimiTurnEnded | undefined; let steered = false; + // Kimi's own unfinished turn ends first; this prompt's outcome follows its `turn.prompt`. + let started = !turnOpen; // Each poll decodes only the complete lines appended since the last one. const readAppended = Effect.scoped( Effect.gen(function* () { @@ -123,6 +157,12 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func const complete = bytes.lastIndexOf(0x0a) + 1; offset += BigInt(complete); for (const line of bytes.subarray(0, complete).toString("utf8").split("\n")) { + if (!started) { + started = + line.includes('"turn.prompt"') && + Option.exists(decodeTurnBoundary(line), (record) => record.type === "turn.prompt"); + continue; + } if (ended === undefined && line.includes('"prompt.steered"')) { steered ||= Option.isSome(decodePromptSteered(line)); } @@ -143,12 +183,14 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func }), Effect.map((ended) => turnFailure(ended, steered)), Effect.catchTags({ - KimiTurnNotEnded: () => - Effect.succeed( - hiddenWorkFailure( - "Kimi queued this message behind work it was still doing and answered before running it. Its reply can't be shown here.", - ), - ), + KimiTurnNotEnded: (error) => + turnOpen + ? Effect.succeed( + hiddenWorkFailure( + "Kimi queued this message behind work it was still doing and answered before running it. Its reply can't be shown here.", + ), + ) + : Effect.fail(error), }), Effect.catch(() => Effect.logWarning("Kimi's session log has no outcome for this turn.").pipe( From abf0ec370f8e2541ab7a53d5b86e5622ab241128 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 09:06:52 +0000 Subject: [PATCH 4/6] fix(server): Kimi runs stay open and show the work Kimi does out of view Reporting a steered or queued Kimi turn as failed still left the run closed and Kimi's reply missing. Kimi runs that work without ACP updates but records it in its session log. The outcome check now follows that log until Kimi is idle and returns the reply, which the ACP adapter streams into the open run before completing it. The check runs outside the runtime callback permit so the wait blocks nothing, and it stops when the turn is stopped. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 14 +- .../src/server/adapter.ts | 13 +- .../src/server/kimiAcp.test.ts | 224 ++++++++------- .../src/server/kimiAcp.ts | 149 ++++++---- packages/provider-acp/src/server/adapter.ts | 254 ++++++++++-------- 5 files changed, 363 insertions(+), 291 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 32956a0d495ff..141edd805d8c4 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] ?? "", ""); 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 91765e3063351..14f379be61525 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.test.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.test.ts @@ -31,10 +31,21 @@ 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", @@ -45,133 +56,116 @@ const steeredTurn = }, { type: "turn.ended", agentId: "main", turnId: 0, reason: "completed" }, { type: "prompt.completed", agentId: "main", promptId: "msg_prompt", reason: "completed" }, - { - type: "turn.prompt", - agentId: "main", - input: [{ type: "text", text: '' }], - }, - ] - .map((record) => JSON.stringify(record)) - .join("\n") + "\n"; + ) + 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) => { // regression: #36163 Kimi answers session/prompt with end_turn while it keeps working - it.effect.each([ - { - case: "the prompt's turn handed off to a steered turn", - before: completedTurn(0), - appended: steeredTurn, - polls: 1, - settles: false, - }, - // Sent while that steered turn runs: Kimi queues it and answers at once, writing nothing. - { - case: "the prompt was only queued behind Kimi's own turn", - before: steeredTurn, - appended: "", - polls: 41, - settles: false, - }, - // Its own turn ends only after the reply; this prompt's turn has not started. - { - case: "Kimi's own turn ends after a queued prompt is answered", - before: steeredTurn, - appended: completedTurn(1), - polls: 41, - settles: false, - }, - // Slash commands such as /compact answer without starting a turn. - { - case: "a slash command answers with no turn", - before: completedTurn(0), - appended: "", - polls: 41, - settles: true, - }, - ])( - "settles as completed: $settles when $case", - ({ before, appended, polls: pollCount, settles }) => - 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"); - yield* fileSystem.writeFileString(wire, before); - 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); - yield* fileSystem.writeFileString(wire, appended, { flag: "a" }); - const outcome = yield* check(endTurn).pipe(Effect.forkChild); - for (let poll = 1; poll < pollCount; poll++) { - yield* Queue.take(polls); - yield* TestClock.adjust("50 millis"); - } - assert.strictEqual((yield* Fiber.join(outcome)) === undefined, settles); - }), + it.effect("follows a steered hand-off until Kimi is idle and returns its reply", () => + Effect.gen(function* () { + 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", + }); + }), ); - it.effect("fails only the turn this prompt ended, once Kimi writes its outcome", () => + // 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", () => 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, 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) + + 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: "FINAL: all steps done\n\nPONG", + }); + }), + ); - const completed = yield* watch(SESSION_ID); - yield* Queue.take(polls); - yield* append(completedTurn(1)); - assert.isUndefined(yield* completed(endTurn)); + 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* Queue.take(polls); - 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 4acec7cd4cac5..1e35ae5453b2f 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({ @@ -42,21 +46,28 @@ const decodePromptSteered = Schema.decodeUnknownOption( ), ); -class KimiTurnNotEnded extends Schema.TaggedError()("KimiTurnNotEnded", {}) {} +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 }), + }), + }), + ), +); -const noOutcome = (_result: EffectAcpSchema.PromptResponse) => - Effect.succeed(undefined); +/** 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 }; -// Kimi runs that work in turns it never reports over ACP, so T3 cannot show it. -const hiddenWorkFailure = (message: string) => - makeProviderFailure({ message, code: null, class: "provider_error", retryable: false }); +const noOutcome = (_result: EffectAcpSchema.PromptResponse, _stopped: Effect.Effect) => + Effect.succeed(undefined); -function turnFailure(ended: KimiTurnEnded, steered: boolean) { - if (steered && ended.reason === "completed") { - return hiddenWorkFailure( - "Kimi ended this turn early to handle a background task and kept working out of view. Its further output can't be shown here.", - ); - } +function turnFailure(ended: KimiTurnEnded): OrchestrationV2ProviderFailure | undefined { if (ended.reason !== "failed") return undefined; const code = ended.error?.code; return makeProviderFailure({ @@ -73,9 +84,10 @@ function turnFailure(ended: KimiTurnEnded, steered: boolean) { } /** - * Kimi Code answers failed, steered-away and queued prompts with `end_turn`, and runs that - * work without ACP updates. Its main agent's `wire.jsonl` shows what happened. Slash commands - * answer without a turn, so only a prompt sent while a turn is open counts as queued. + * 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, @@ -131,17 +143,16 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func }), ).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; - let steered = false; - // Kimi's own unfinished turn ends first; this prompt's outcome follows its `turn.prompt`. - let started = !turnOpen; - // 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" }); @@ -156,44 +167,68 @@ 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 (!started) { - started = - line.includes('"turn.prompt"') && - Option.exists(decodeTurnBoundary(line), (record) => record.type === "turn.prompt"); - continue; - } - if (ended === undefined && line.includes('"prompt.steered"')) { - steered ||= Option.isSome(decodePromptSteered(line)); - } - 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((ended) => turnFailure(ended, steered)), - Effect.catchTags({ - KimiTurnNotEnded: (error) => - turnOpen - ? Effect.succeed( - hiddenWorkFailure( - "Kimi queued this message behind work it was still doing and answered before running it. Its reply can't be shown here.", - ), - ) - : Effect.fail(error), - }), + + let ended: KimiTurnEnded | undefined; + let steered = false; + let open = turnOpen; + 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; + open = boundary.value.type === "turn.prompt"; + if (open) continue; + const record = decodeTurnEnded(line); + if (Option.isNone(record)) continue; + lastEnded = record.value; + ended ??= record.value; + } else if (line.includes('"content.part"') && (turnOpen || 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. + 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); + quiet = open || 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.logWarning("Kimi's session log could not be read for this turn.").pipe( Effect.as(undefined), ), ), diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index e2dc68fc13fdb..5ce3c613ffa11 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; /** @@ -9166,108 +9170,138 @@ 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"; + 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( From 88c03d838eccba596b4b17c898b06c5b234ff037 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 09:29:44 +0000 Subject: [PATCH 5/6] fix(server): Kimi's hidden-work wait ends with the agent and its own prompt - A run whose outcome check is following the agent's work fails when the agent's runtime terminates, instead of staying open. - A prompt Kimi only queued gets only its own turn's reply and outcome, not the earlier turn's. - A session log that becomes unreadable while following that work fails the run instead of reporting success. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 75 +++++++++++++++++++ .../src/server/kimiAcp.test.ts | 44 +++++------ .../src/server/kimiAcp.ts | 31 +++++++- packages/provider-acp/src/server/adapter.ts | 12 +++ 4 files changed, 137 insertions(+), 25 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 141edd805d8c4..71d35d0fa0bab 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -1518,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/kimiAcp.test.ts b/packages/provider-acp-registry/src/server/kimiAcp.test.ts index 14f379be61525..09d339642ff47 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.test.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.test.ts @@ -104,27 +104,29 @@ it.layer(NodeServices.layer)("makeKimiPromptOutcomeWatch", (it) => { ); // 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", () => - 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) + - 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: "FINAL: all steps done\n\nPONG", - }); - }), + 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) + + 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", () => diff --git a/packages/provider-acp-registry/src/server/kimiAcp.ts b/packages/provider-acp-registry/src/server/kimiAcp.ts index 1e35ae5453b2f..694a80ae21174 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.ts @@ -174,6 +174,9 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func 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) => { @@ -184,13 +187,20 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func 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) continue; + if (open) { + queuedStarted ||= turnOpen && !wasOpen; + continue; + } const record = decodeTurnEnded(line); if (Option.isNone(record)) continue; - lastEnded = record.value; + if (!turnOpen || queuedStarted) lastEnded = record.value; ended ??= record.value; - } else if (line.includes('"content.part"') && (turnOpen || ended !== undefined)) { + } 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); } @@ -215,6 +225,7 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func 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); @@ -229,7 +240,19 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func }).pipe( Effect.catch(() => Effect.logWarning("Kimi's session log could not be read for this turn.").pipe( - Effect.as(undefined), + 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 5ce3c613ffa11..68200f59e0630 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -1624,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. */ @@ -2652,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 || @@ -8965,6 +8975,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( finalizedStatus: null, promptSettled: false, promptSettledStatus: null, + checkingPromptOutcome: false, workAccepted: false, promptWireSettled, promptDispatched, @@ -9180,6 +9191,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( !context.interrupted && result.stopReason !== "error" && result.stopReason !== "cancelled"; + context.checkingPromptOutcome = checksPromptOutcome; const promptOutcome = checksPromptOutcome ? yield* checkPromptOutcome( result, From 8437ac41b18604a0642037d1738c5cb0c2838ed5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 09:47:03 +0000 Subject: [PATCH 6/6] fix(server): a queued Kimi message waits for its own turn to run Co-Authored-By: Claude Opus 5.5 --- .../provider-acp-registry/src/server/kimiAcp.test.ts | 11 ++++++----- packages/provider-acp-registry/src/server/kimiAcp.ts | 4 +++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/provider-acp-registry/src/server/kimiAcp.test.ts b/packages/provider-acp-registry/src/server/kimiAcp.test.ts index 09d339642ff47..fb1eb7d753b50 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.test.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.test.ts @@ -113,12 +113,13 @@ it.layer(NodeServices.layer)("makeKimiPromptOutcomeWatch", (it) => { 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( - replyText("FINAL: all steps done") + - completedTurn(1) + - turnPrompt("Reply with the single word PONG.") + - replyText("PONG") + - completedTurn(2), + turnPrompt("Reply with the single word PONG.") + replyText("PONG") + completedTurn(2), ); yield* nextPoll("250 millis"); yield* nextPoll("250 millis"); diff --git a/packages/provider-acp-registry/src/server/kimiAcp.ts b/packages/provider-acp-registry/src/server/kimiAcp.ts index 694a80ae21174..c6cbbe9523029 100644 --- a/packages/provider-acp-registry/src/server/kimiAcp.ts +++ b/packages/provider-acp-registry/src/server/kimiAcp.ts @@ -231,7 +231,9 @@ export const makeKimiPromptOutcomeWatch = Effect.fn("KimiTurnOutcome.make")(func yield* Effect.sleep(HIDDEN_WORK_POLL_INTERVAL); const lines = yield* readAppended; follow(lines); - quiet = open || lines.length > 0 ? 0 : quiet + 1; + // 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 };