Skip to content
89 changes: 86 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();
const releaseCheck = yield* Deferred.make<void>();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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] ?? "", "<runtime_info>");
Expand Down Expand Up @@ -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<void>();
const releaseCheck = yield* Deferred.make<void>();
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");
Expand Down
13 changes: 7 additions & 6 deletions packages/provider-acp-registry/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>) =>
Effect.map(check(result, stopped), (outcome) =>
outcome === undefined || !("failure" in outcome)
? outcome
: { failure: outcome.failure, threadDisposition: "reusable" as const },
),
),
}),
...(registryAgentId === "mistral-vibe"
Expand Down
163 changes: 130 additions & 33 deletions packages/provider-acp-registry/src/server/kimiAcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>) =>
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: '<notification id="task:bash-1:completed">' }],
},
{ type: "turn.ended", agentId: "main", turnId: 0, reason: "completed" },
{ type: "prompt.completed", agentId: "main", promptId: "msg_prompt", reason: "completed" },
) + turnPrompt('<notification id="task:bash-1:completed">');
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<void>();
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<void>();
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,
},
});
}),
);
Expand Down
Loading
Loading