Skip to content

Commit 3c990ef

Browse files
Radu028claude
andcommitted
fix(acp): resend T3's notes after idle and Copilot compactions
A compaction_update that completed while no turn was active, or after the turn settled, never set the compaction marker, so the next prompt went out without T3's instructions or runtime notes. Mark a completed root-session compaction where session updates arrive, before turn projection. Copilot reports compaction only in its raw session-event feed. Subscribe to session.compaction_complete and, for an applied root compaction, mark the session compacted through a new markSessionCompacted extension hook. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eb7f45e commit 3c990ef

3 files changed

Lines changed: 121 additions & 38 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,36 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
345345
authorizationHeader: "Bearer instruction-compaction-token",
346346
browserToolsAvailable: true,
347347
});
348+
type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
349+
let sessionUpdate: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
350+
let copilotSessionEvent:
351+
| ((event: unknown) => Effect.Effect<void, EffectAcpErrors.AcpError>)
352+
| undefined;
348353
const adapter = yield* makeAcpAdapterV2({
349354
instanceId,
350355
flavor: {
351356
driver: ACP_TEST_DRIVER,
352357
capabilities: AcpProviderCapabilitiesV2,
353358
supportsCompaction: true,
359+
registerExtensions: makeCopilotSubagentRouting().registerExtensions,
354360
makeRuntime: makeMockRuntime({
355361
childProcessSpawner,
356362
mockAgentPath,
357363
protocolEvents,
358364
environment,
359365
wrapRuntime: (runtime) => ({
360366
...runtime,
367+
// The captured handlers deliver frames between turns, while no prompt is in flight.
368+
handleSessionUpdate: (next) =>
369+
Effect.sync(() => {
370+
sessionUpdate = next;
371+
}).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
372+
handleExtNotification: (method, payload, next) =>
373+
Effect.sync(() => {
374+
if (method === "github.com/copilot/sessionEvent") {
375+
copilotSessionEvent = next as typeof copilotSessionEvent;
376+
}
377+
}).pipe(Effect.andThen(runtime.handleExtNotification(method, payload, next))),
361378
// Make compaction during prompt completion deterministic at the adapter boundary.
362379
prompt: (payload, options) =>
363380
runtime.prompt(payload, options).pipe(Effect.tap(() => runtime.drainEvents)),
@@ -381,30 +398,45 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
381398
const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
382399
const now = yield* DateTime.now;
383400
let ordinal = 0;
384-
return Effect.fnUntraced(function* (messageText: string, expectedStatus = "completed") {
385-
yield* runtime.startTurn(
386-
makeTurnInput({
387-
threadId,
388-
providerThread,
389-
instanceId,
390-
runtimePolicy,
391-
now,
392-
ordinal: ++ordinal,
393-
messageText,
394-
}),
395-
);
396-
const terminal = yield* runtime.events.pipe(
397-
Stream.filter((event) => event.type === "turn.terminal"),
398-
Stream.runHead,
399-
);
400-
assert.equal(Option.getOrThrow(terminal).status, expectedStatus);
401-
while (true) {
402-
const event = yield* Queue.take(protocolEvents);
403-
if (event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt") {
404-
return rawProtocolPromptTextBlocks(event);
401+
const sessionId = "mock-session-1";
402+
return {
403+
runTurn: Effect.fnUntraced(function* (messageText: string, expectedStatus = "completed") {
404+
yield* runtime.startTurn(
405+
makeTurnInput({
406+
threadId,
407+
providerThread,
408+
instanceId,
409+
runtimePolicy,
410+
now,
411+
ordinal: ++ordinal,
412+
messageText,
413+
}),
414+
);
415+
const terminal = yield* runtime.events.pipe(
416+
Stream.filter((event) => event.type === "turn.terminal"),
417+
Stream.runHead,
418+
);
419+
assert.equal(Option.getOrThrow(terminal).status, expectedStatus);
420+
while (true) {
421+
const event = yield* Queue.take(protocolEvents);
422+
if (event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt") {
423+
return rawProtocolPromptTextBlocks(event);
424+
}
405425
}
406-
}
407-
});
426+
}),
427+
idleSessionUpdate: Effect.fnUntraced(function* (update: EffectAcpSchema.SessionUpdate) {
428+
assert.isDefined(sessionUpdate);
429+
yield* sessionUpdate({ sessionId, update });
430+
}),
431+
idleCopilotSessionEvent: Effect.fnUntraced(function* (event: {
432+
readonly type: string;
433+
readonly agentId?: string;
434+
readonly data: Record<string, unknown>;
435+
}) {
436+
assert.isDefined(copilotSessionEvent);
437+
yield* copilotSessionEvent({ sessionId, ...event });
438+
}),
439+
};
408440
});
409441

410442
function assertReinjectedInstructions(
@@ -1044,12 +1076,13 @@ describe("AcpAdapterV2", () => {
10441076
);
10451077

10461078
it.live(
1047-
"reinjects ACP instructions after protocol compaction, including during reinjection",
1079+
"reinjects ACP instructions after protocol compaction, during reinjection or while idle",
10481080
() =>
10491081
Effect.gen(function* () {
1050-
const runTurn = yield* makeInstructionCompactionHarness({
1051-
T3_ACP_COMPACTION_PROMPT_ORDINALS: "2,4",
1052-
});
1082+
const { runTurn, idleSessionUpdate, idleCopilotSessionEvent } =
1083+
yield* makeInstructionCompactionHarness({
1084+
T3_ACP_COMPACTION_PROMPT_ORDINALS: "2,4",
1085+
});
10531086
yield* runTurn("Start work.");
10541087
yield* runTurn("Continue until context compacts.");
10551088
assert.deepEqual(yield* runTurn(" /repro-skill"), [
@@ -1064,14 +1097,41 @@ describe("AcpAdapterV2", () => {
10641097
"Continue after the second compaction.",
10651098
);
10661099
assert.deepEqual(yield* runTurn("Finish work."), [{ type: "text", text: "Finish work." }]);
1100+
1101+
yield* idleSessionUpdate({
1102+
sessionUpdate: "compaction_update",
1103+
compactionId: "compact-idle",
1104+
status: "completed",
1105+
});
1106+
assertReinjectedInstructions(
1107+
yield* runTurn("Continue after idle compaction."),
1108+
"Continue after idle compaction.",
1109+
);
1110+
const copilotCompaction = { success: true, outcome: "applied", trigger: "threshold" };
1111+
yield* idleCopilotSessionEvent({
1112+
type: "session.compaction_complete",
1113+
agentId: "agent-a",
1114+
data: copilotCompaction,
1115+
});
1116+
assert.deepEqual(yield* runTurn("Continue after subagent compaction."), [
1117+
{ type: "text", text: "Continue after subagent compaction." },
1118+
]);
1119+
yield* idleCopilotSessionEvent({
1120+
type: "session.compaction_complete",
1121+
data: copilotCompaction,
1122+
});
1123+
assertReinjectedInstructions(
1124+
yield* runTurn("Continue after Copilot compaction."),
1125+
"Continue after Copilot compaction.",
1126+
);
10671127
}).pipe(Effect.provide(layerTest), Effect.scoped),
10681128
);
10691129

10701130
it.live(
10711131
"reinjects ACP instructions after successful native compact, but not failed compact",
10721132
() =>
10731133
Effect.gen(function* () {
1074-
const runTurn = yield* makeInstructionCompactionHarness({
1134+
const { runTurn } = yield* makeInstructionCompactionHarness({
10751135
T3_ACP_FAIL_PROMPT_ORDINAL: "2",
10761136
});
10771137
yield* runTurn("Start work.");

‎packages/provider-acp-registry/src/server/copilotAcp.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const copilotClientCapabilitiesMeta = {
2020
"subagent.failed",
2121
"assistant.message_delta",
2222
"assistant.reasoning_delta",
23+
"session.compaction_complete",
2324
],
2425
},
2526
};
@@ -97,6 +98,13 @@ export function makeCopilotSubagentRouting() {
9798
result: typeof data.error === "string" ? data.error : null,
9899
});
99100
}
101+
// Copilot sends no ACP compaction_update; agentId marks a subagent's own compaction.
102+
case "session.compaction_complete": {
103+
if (event.agentId !== undefined || data.success !== true || data.outcome !== "applied") {
104+
return Effect.void;
105+
}
106+
return context.markSessionCompacted(event.sessionId);
107+
}
100108
default:
101109
return Effect.void;
102110
}

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ export interface AcpAdapterV2ExtensionContext {
185185
readonly output?: string;
186186
readonly report?: BackgroundWork;
187187
}) => Effect.Effect<void>;
188+
/** For compactions reported outside ACP `compaction_update`; ignores non-root sessions. */
189+
readonly markSessionCompacted: (sessionId: string) => Effect.Effect<void>;
188190
readonly requestUserInput: (
189191
input: AcpAdapterV2UserInputRequest,
190192
requestContext: EffectAcpProtocol.AcpRequestContext,
@@ -1597,6 +1599,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
15971599
const promptRuntimeInstructions = yield* Ref.make(new Map<string, string>());
15981600
// Unique markers keep delayed prompt completions from clearing later compactions.
15991601
const compactionMarkers = yield* Ref.make(new Map<string, symbol>());
1602+
// The next prompt in a compacted root session resends T3's instructions and runtime notes.
1603+
const markSessionCompacted = Effect.fn("AcpAdapterV2.markSessionCompacted")(function* (
1604+
sessionId: string,
1605+
) {
1606+
if (sessionId !== (yield* Ref.get(activeSessionId))) return;
1607+
yield* Ref.update(compactionMarkers, (current) =>
1608+
new Map(current).set(sessionId, Symbol()),
1609+
);
1610+
});
16001611
const runtimeRestartRequired = yield* Ref.make(false);
16011612
const runtimeTeardownState = yield* Ref.make<AcpRuntimeTeardownState>({ _tag: "Idle" });
16021613
const runtimeCallbackGeneration = yield* Ref.make(0);
@@ -3670,11 +3681,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
36703681
updated.delete(context.nativeThreadId);
36713682
return updated;
36723683
});
3673-
yield* Ref.update(compactionMarkers, (current) => {
3674-
const updated = new Map(current);
3675-
updated.set(context.nativeThreadId, Symbol());
3676-
return updated;
3677-
});
3684+
yield* markSessionCompacted(context.nativeThreadId);
36783685
}
36793686
} else if (mcpIdentity !== undefined) {
36803687
const toolName = `${mcpIdentity.server}.${mcpIdentity.tool}`;
@@ -5826,6 +5833,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
58265833
) ?? Effect.void
58275834
);
58285835
}
5836+
// Turn projection misses compactions that finish while idle or after settlement.
5837+
if (
5838+
notification.update.sessionUpdate === "compaction_update" &&
5839+
notification.update.status === "completed"
5840+
) {
5841+
yield* markSessionCompacted(notification.sessionId);
5842+
}
58295843
yield* handleSessionUpdate(
58305844
flavor.normalizeSessionUpdate?.(notification) ?? notification,
58315845
);
@@ -6289,6 +6303,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
62896303
yield* finishSubagentFromNotice(notice);
62906304
}),
62916305
).pipe(Effect.asVoid),
6306+
markSessionCompacted: (sessionId) =>
6307+
runRuntimeCallbackAtGeneration(
6308+
handlerGeneration,
6309+
markSessionCompacted(sessionId),
6310+
).pipe(Effect.asVoid),
62926311
applyBackgroundTaskMutation: (incoming) =>
62936312
runRuntimeCallbackAtGeneration(
62946313
handlerGeneration,
@@ -7019,11 +7038,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
70197038
settledStatus === "completed"
70207039
) {
70217040
const nativeItemId = `${context.nativeTurnId}:compaction`;
7022-
yield* Ref.update(compactionMarkers, (current) => {
7023-
const updated = new Map(current);
7024-
updated.set(context.nativeThreadId, Symbol());
7025-
return updated;
7026-
});
7041+
yield* markSessionCompacted(context.nativeThreadId);
70277042
yield* emitProviderEvent({
70287043
type: "turn_item.updated",
70297044
driver,

0 commit comments

Comments
 (0)