@@ -92,7 +92,7 @@ import {
9292 type AcpAdapterV2SubagentUpdate,
9393} from "@t3tools/provider-acp/server/adapter";
9494
95- import { makeGrokAdapterV2 } from "@t3tools/provider-grok/testing";
95+ import { makeGrokAdapterV2, makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing";
9696import {
9797 acpRegistryPromptFailure,
9898 registerMistralVibeAcpExtensions,
@@ -11899,6 +11899,227 @@ describe("AcpAdapterV2", () => {
1189911899 }).pipe(Effect.provide(layerTest), Effect.scoped),
1190011900 );
1190111901
11902+ it.effect.each([
11903+ { status: "completed" as const, exitCode: 0 },
11904+ { status: "failed" as const, exitCode: 1 },
11905+ ])(
11906+ "retains early Grok $status command completion across sparse notifications and replayed ACKs",
11907+ ({ status, exitCode }) =>
11908+ Effect.gen(function* () {
11909+ const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
11910+ const path = yield* Path.Path;
11911+ const selfInvocation = yield* resolveSelfInvocation();
11912+ const mockAgentPath = yield* path.fromFileUrl(
11913+ new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
11914+ );
11915+ type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
11916+ let sessionUpdateHandler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
11917+ let taskCompleted:
11918+ | ((payload: unknown) => Effect.Effect<void, EffectAcpErrors.AcpError>)
11919+ | undefined;
11920+ const promptStarted = yield* Deferred.make<void>();
11921+ const promptGate = yield* Deferred.make<EffectAcpSchema.PromptResponse>();
11922+ const promptHeld = yield* Deferred.make<void>();
11923+ const finalizeScheduled = yield* Deferred.make<Duration.Input>();
11924+ const continuationOffered = yield* Deferred.make<void>();
11925+ const continuationRequests: Array<ProviderContinuationRequests.ProviderContinuationRequest> =
11926+ [];
11927+ const instanceId = ProviderInstanceId.make("acp-test");
11928+ const grokFlavor = makeGrokAcpAdapterFlavor(
11929+ {
11930+ instanceId,
11931+ settings: DEFAULT_GROK_SETTINGS,
11932+ environment: {},
11933+ hostPlatform: yield* HostProcess.Platform,
11934+ selfInvocation,
11935+ },
11936+ yield* HostProcess.HomeDirectory,
11937+ );
11938+ const adapter = yield* makeAcpAdapterV2({
11939+ instanceId,
11940+ selfInvocation,
11941+ testHooks: {
11942+ afterPromptSettledWithBackgroundWork: () =>
11943+ Deferred.succeed(promptHeld, undefined).pipe(Effect.asVoid),
11944+ onDeferredFinalizeScheduled: (debounce) =>
11945+ Deferred.succeed(finalizeScheduled, debounce).pipe(Effect.asVoid),
11946+ },
11947+ flavor: {
11948+ driver: ACP_TEST_DRIVER,
11949+ capabilities: AcpProviderCapabilitiesV2,
11950+ enablePostSettleContinuation: true,
11951+ deferFinalizeForBackgroundWork: true,
11952+ normalizeToolCall: grokFlavor.normalizeToolCall!,
11953+ extractBackgroundTaskId: grokFlavor.extractBackgroundTaskId!,
11954+ isBackgroundCommandTool: grokFlavor.isBackgroundCommandTool!,
11955+ registerExtensions: ({ runtime, applyBackgroundTaskMutation }) =>
11956+ registerXAiBackgroundTaskTracking(runtime, applyBackgroundTaskMutation),
11957+ makeRuntime: makeMockRuntime({
11958+ childProcessSpawner,
11959+ mockAgentPath,
11960+ wrapRuntime: (runtime) => ({
11961+ ...runtime,
11962+ handleSessionUpdate: (handler) =>
11963+ Effect.sync(() => {
11964+ sessionUpdateHandler = handler;
11965+ }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
11966+ handleExtNotification: (method, schema, handler) =>
11967+ Effect.sync(() => {
11968+ if (method === "x.ai/task_completed") {
11969+ taskCompleted = (payload) =>
11970+ handler(Schema.decodeUnknownSync(schema)(payload));
11971+ }
11972+ }).pipe(Effect.andThen(runtime.handleExtNotification(method, schema, handler))),
11973+ prompt: () =>
11974+ Deferred.succeed(promptStarted, undefined).pipe(
11975+ Effect.andThen(Deferred.await(promptGate)),
11976+ ),
11977+ }),
11978+ }),
11979+ },
11980+ continuationRequests: {
11981+ offer: (request) =>
11982+ Effect.sync(() => continuationRequests.push(request)).pipe(
11983+ Effect.andThen(Deferred.succeed(continuationOffered, undefined)),
11984+ Effect.asVoid,
11985+ ),
11986+ },
11987+ });
11988+ const threadId = ThreadId.make("thread-acp-early-command");
11989+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
11990+ runtimeMode: "full-access",
11991+ interactionMode: "default",
11992+ cwd: process.cwd(),
11993+ });
11994+ const modelSelection = { instanceId, model: "default" } as const;
11995+ const runtime = yield* adapter.openSession({
11996+ threadId,
11997+ providerSessionId: ProviderSessionId.make("provider-session-acp-early-command"),
11998+ modelSelection,
11999+ runtimePolicy,
12000+ });
12001+ const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
12002+ yield* runtime.events.pipe(
12003+ Stream.runForEach((event) => Queue.offer(events, event)),
12004+ Effect.forkScoped,
12005+ );
12006+ const providerThread = yield* runtime.ensureThread({
12007+ threadId,
12008+ modelSelection,
12009+ runtimePolicy,
12010+ });
12011+ const now = yield* DateTime.now;
12012+ yield* runtime
12013+ .startTurn(
12014+ makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal: 1 }),
12015+ )
12016+ .pipe(Effect.forkScoped);
12017+ yield* Deferred.await(promptStarted);
12018+ assert.isDefined(sessionUpdateHandler);
12019+ assert.isDefined(taskCompleted);
12020+ const update = (update: EffectAcpSchema.SessionUpdate) =>
12021+ sessionUpdateHandler!({ sessionId: "mock-session-1", update });
12022+ const complete = (taskId: string, snapshot?: { output?: string; exit_code?: number }) =>
12023+ taskCompleted!({
12024+ sessionId: "mock-session-1",
12025+ update: { sessionUpdate: "task_completed", task_id: taskId, task_snapshot: snapshot },
12026+ });
12027+ for (const taskId of ["early", "blocker"]) {
12028+ yield* update({
12029+ sessionUpdate: "tool_call",
12030+ toolCallId: taskId,
12031+ title: `Run ${taskId}`,
12032+ kind: "execute",
12033+ status: "completed",
12034+ rawInput: { command: `echo ${taskId}` },
12035+ rawOutput: { type: "BackgroundTaskStarted", task_id: taskId },
12036+ });
12037+ }
12038+ // Progress removes the ACK carrying the task identity before a reportless completion.
12039+ yield* update({
12040+ sessionUpdate: "tool_call_update",
12041+ toolCallId: "early",
12042+ status: "in_progress",
12043+ rawOutput: { type: "Text", text: "Command progress" },
12044+ });
12045+ yield* complete("early", { exit_code: exitCode, output: "EARLY_BG_DONE\n" });
12046+ // The real decoder treats this exit-code-free duplicate as completed, even after failure.
12047+ yield* complete("early");
12048+ assert.lengthOf(continuationRequests, 0);
12049+
12050+ let backgroundItem: ProviderAdapter.ProviderAdapterV2Event | undefined;
12051+ let checkpoint = 0;
12052+ const assertCommand = Effect.fnUntraced(function* (expectedStatus: string, output: string) {
12053+ const title = `Checkpoint ${++checkpoint}`;
12054+ // A later tool event fences the emitted updates without waiting for a terminal that may never come.
12055+ yield* update({ sessionUpdate: "tool_call_update", toolCallId: "blocker", title });
12056+ while (true) {
12057+ const event = yield* Queue.take(events);
12058+ if (event.type !== "turn_item.updated") continue;
12059+ if (event.turnItem.nativeItemRef?.nativeId === "early") backgroundItem = event;
12060+ if (
12061+ event.turnItem.nativeItemRef?.nativeId === "blocker" &&
12062+ event.turnItem.title === title
12063+ )
12064+ break;
12065+ }
12066+ assert.equal(backgroundItem?.type, "turn_item.updated");
12067+ if (backgroundItem?.type !== "turn_item.updated") return;
12068+ assert.equal(backgroundItem.turnItem.status, expectedStatus);
12069+ assert.equal(backgroundItem.turnItem.type, "command_execution");
12070+ if (backgroundItem.turnItem.type === "command_execution") {
12071+ assert.equal(backgroundItem.turnItem.output ?? "", output);
12072+ }
12073+ });
12074+ yield* Deferred.succeed(promptGate, { stopReason: "end_turn" });
12075+ yield* Deferred.await(promptHeld);
12076+ yield* assertCommand(status, "EARLY_BG_DONE\n");
12077+ assert.lengthOf(continuationRequests, 0, "the other command still holds the root open");
12078+
12079+ const replayAck = () =>
12080+ update({
12081+ sessionUpdate: "tool_call_update",
12082+ toolCallId: "early",
12083+ status: "completed",
12084+ rawOutput: { type: "BackgroundTaskStarted", task_id: "early" },
12085+ });
12086+ yield* replayAck();
12087+ yield* assertCommand(status, "EARLY_BG_DONE\n");
12088+ // Sparse ACP updates must retain the command result after projection replaces the ACK.
12089+ for (const sparseStatus of ["completed", "in_progress"] as const) {
12090+ yield* update({
12091+ sessionUpdate: "tool_call_update",
12092+ toolCallId: "early",
12093+ status: sparseStatus,
12094+ });
12095+ yield* assertCommand(status, "EARLY_BG_DONE\n");
12096+ }
12097+ if (status === "completed") {
12098+ for (const output of ["", "UPDATED_OUTPUT\n"]) {
12099+ yield* complete("early", { output });
12100+ yield* replayAck();
12101+ yield* assertCommand(status, output);
12102+ }
12103+ }
12104+ yield* complete("blocker", { exit_code: 0, output: "BLOCKER_DONE\n" });
12105+ yield* TestClock.adjust(yield* Deferred.await(finalizeScheduled));
12106+ while (true) {
12107+ const event = yield* Queue.take(events);
12108+ if (event.type !== "turn.terminal") continue;
12109+ assert.equal(event.status, "completed");
12110+ break;
12111+ }
12112+ yield* Deferred.await(continuationOffered);
12113+ yield* complete("early");
12114+ yield* complete("blocker");
12115+ assert.lengthOf(
12116+ continuationRequests,
12117+ 1,
12118+ "duplicate completions must not offer another continuation",
12119+ );
12120+ }).pipe(Effect.provide(layerTest), Effect.scoped),
12121+ );
12122+
1190212123 it.effect("empty-drain continuation turn waits the quiet window so late frames can attach", () =>
1190312124 Effect.gen(function* () {
1190412125 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
0 commit comments