@@ -538,6 +538,100 @@ const pollProtocolMethods = (events: Queue.Queue<EffectAcpProtocol.AcpProtocolLo
538538 return methods;
539539 });
540540
541+ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
542+ environment: Readonly<Record<string, string>>,
543+ ) {
544+ const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
545+ const path = yield* Path.Path;
546+ const selfInvocation = yield* resolveSelfInvocation();
547+ const mockAgentPath = yield* path.fromFileUrl(
548+ new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
549+ );
550+ const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
551+ const instanceId = ProviderInstanceId.make("acp-test-instruction-compaction");
552+ const threadId = ThreadId.make("thread-acp-instruction-compaction");
553+ yield* (yield* McpProviderSessions.McpProviderSessions).set({
554+ environmentId: EnvironmentId.make("environment-acp-instruction-compaction"),
555+ threadId,
556+ providerSessionId: "mcp-session-acp-instruction-compaction",
557+ providerInstanceId: instanceId,
558+ endpoint: "http://127.0.0.1:43123/mcp",
559+ authorizationHeader: "Bearer instruction-compaction-token",
560+ browserToolsAvailable: true,
561+ });
562+ const adapter = yield* makeAcpAdapterV2({
563+ instanceId,
564+ flavor: {
565+ driver: ACP_TEST_DRIVER,
566+ capabilities: AcpProviderCapabilitiesV2,
567+ supportsCompaction: true,
568+ makeRuntime: makeMockRuntime({
569+ childProcessSpawner,
570+ mockAgentPath,
571+ protocolEvents,
572+ environment,
573+ wrapRuntime: (runtime) => ({
574+ ...runtime,
575+ // Make compaction during prompt completion deterministic at the adapter boundary.
576+ prompt: (payload, options) =>
577+ runtime.prompt(payload, options).pipe(Effect.tap(() => runtime.drainEvents)),
578+ }),
579+ }),
580+ },
581+ selfInvocation,
582+ });
583+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
584+ runtimeMode: "full-access",
585+ interactionMode: "default",
586+ cwd: process.cwd(),
587+ });
588+ const modelSelection = { instanceId, model: "default" } as const;
589+ const runtime = yield* adapter.openSession({
590+ threadId,
591+ providerSessionId: ProviderSessionId.make("provider-session-acp-instruction-compaction"),
592+ modelSelection,
593+ runtimePolicy,
594+ });
595+ const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
596+ const now = yield* DateTime.now;
597+ let ordinal = 0;
598+ return Effect.fnUntraced(function* (messageText: string, expectedStatus = "completed") {
599+ yield* runtime.startTurn(
600+ makeTurnInput({
601+ threadId,
602+ providerThread,
603+ instanceId,
604+ runtimePolicy,
605+ now,
606+ ordinal: ++ordinal,
607+ messageText,
608+ }),
609+ );
610+ const terminal = yield* runtime.events.pipe(
611+ Stream.filter((event) => event.type === "turn.terminal"),
612+ Stream.runHead,
613+ );
614+ assert.equal(Option.getOrThrow(terminal).status, expectedStatus);
615+ while (true) {
616+ const event = yield* Queue.take(protocolEvents);
617+ if (event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt") {
618+ return rawProtocolPromptTextBlocks(event);
619+ }
620+ }
621+ });
622+ });
623+
624+ function assertReinjectedInstructions(
625+ blocks: Array<{ type: "text"; text: string }>,
626+ request: string,
627+ ) {
628+ const text = blocks[0]?.text ?? "";
629+ assert.include(text, "T3 Code interaction mode: Default");
630+ assert.include(text, "T3 Code collaborative browser");
631+ assert.include(text, "T3 Code orchestration");
632+ assert.include(text, `<user_request>\n${request}\n</user_request>`);
633+ }
634+
541635describe("AcpAdapterV2", () => {
542636 it.live.each(["failed", "recovered", "completed", "cancelled"] as const)(
543637 "projects Mistral retry notices and their %s outcome",
@@ -827,6 +921,52 @@ describe("AcpAdapterV2", () => {
827921 }).pipe(Effect.provide(layerTest), Effect.scoped),
828922 );
829923
924+ it.live(
925+ "reinjects ACP instructions after protocol compaction, including during reinjection",
926+ () =>
927+ Effect.gen(function* () {
928+ const runTurn = yield* makeInstructionCompactionHarness({
929+ T3_ACP_COMPACTION_PROMPT_ORDINALS: "2,4",
930+ });
931+ yield* runTurn("Start work.");
932+ yield* runTurn("Continue until context compacts.");
933+ assert.deepEqual(yield* runTurn(" /repro-skill"), [
934+ { type: "text", text: " /repro-skill" },
935+ ]);
936+ assertReinjectedInstructions(
937+ yield* runTurn("Continue after compaction."),
938+ "Continue after compaction.",
939+ );
940+ assertReinjectedInstructions(
941+ yield* runTurn("Continue after the second compaction."),
942+ "Continue after the second compaction.",
943+ );
944+ assert.equal((yield* runTurn("Finish work."))[0]?.text, "Finish work.");
945+ }).pipe(Effect.provide(layerTest), Effect.scoped),
946+ );
947+
948+ it.live(
949+ "reinjects ACP instructions after successful native compact, but not failed compact",
950+ () =>
951+ Effect.gen(function* () {
952+ const runTurn = yield* makeInstructionCompactionHarness({
953+ T3_ACP_FAIL_PROMPT_ORDINAL: "2",
954+ });
955+ yield* runTurn("Start work.");
956+ assert.deepEqual(yield* runTurn("/compact", "failed"), [
957+ { type: "text", text: "/compact" },
958+ ]);
959+ assert.equal(
960+ (yield* runTurn("Continue after failure."))[0]?.text,
961+ "Continue after failure.",
962+ );
963+ assert.deepEqual(yield* runTurn("/compact"), [{ type: "text", text: "/compact" }]);
964+ assert.deepEqual(yield* runTurn("/repro-skill"), [{ type: "text", text: "/repro-skill" }]);
965+ assertReinjectedInstructions(yield* runTurn("Continue work."), "Continue work.");
966+ assert.equal((yield* runTurn("Finish work."))[0]?.text, "Finish work.");
967+ }).pipe(Effect.provide(layerTest), Effect.scoped),
968+ );
969+
830970 it.live("reapplies a selection after a switch away from it failed partway", () =>
831971 Effect.gen(function* () {
832972 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
0 commit comments