Skip to content

Commit fd2a787

Browse files
committed
fix(acp): 清理空闲压缩和回滚后的指令缓存
在空闲根会话完成压缩时同步失效持久指令记录和进程内缓存,保留原有后续任务处理。\n\n回滚替换会话时清除同名原生会话的旧投递记录,避免遗漏新会话的指令。\n\n补充提示词行为回归;72 项 ACP 测试、31 项 Registry/Grok 回放及 check:changed 均通过。
1 parent 10d2a22 commit fd2a787

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

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

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ const pollProtocolMethods = (events: Queue.Queue<EffectAcpProtocol.AcpProtocolLo
307307
const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
308308
environment: Readonly<Record<string, string>>,
309309
followAgentWorkState = false,
310+
drainPromptEvents = true,
310311
) {
311312
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
312313
const path = yield* Path.Path;
@@ -317,6 +318,9 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
317318
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
318319
const instanceId = ProviderInstanceId.make("acp-test-instruction-compaction");
319320
const threadId = ThreadId.make("thread-acp-instruction-compaction");
321+
let sessionUpdate:
322+
| Parameters<AcpSessionRuntime.AcpSessionRuntime["Service"]["handleSessionUpdate"]>[0]
323+
| undefined;
320324
yield* (yield* McpProviderSessions.McpProviderSessions).set({
321325
environmentId: EnvironmentId.make("environment-acp-instruction-compaction"),
322326
threadId,
@@ -340,9 +344,15 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
340344
environment,
341345
wrapRuntime: (runtime) => ({
342346
...runtime,
347+
handleSessionUpdate: (handler) => {
348+
sessionUpdate = handler;
349+
return runtime.handleSessionUpdate(handler);
350+
},
343351
// Make compaction during prompt completion deterministic at the adapter boundary.
344352
prompt: (payload, options) =>
345-
runtime.prompt(payload, options).pipe(Effect.tap(() => runtime.drainEvents)),
353+
runtime
354+
.prompt(payload, options)
355+
.pipe(Effect.tap(() => (drainPromptEvents ? runtime.drainEvents : Effect.void))),
346356
}),
347357
}),
348358
},
@@ -380,11 +390,36 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
380390
return Effect.fnUntraced(function* (
381391
messageText: string,
382392
expectedStatus = "completed",
383-
reopen = false,
393+
reopen: boolean | "rollback" | "compact" = false,
384394
interactionMode: "default" | "plan" = "default",
385395
) {
386396
const turnRuntimePolicy = { ...runtimePolicy, interactionMode };
387-
if (reopen) {
397+
if (reopen === "compact") {
398+
if (sessionUpdate === undefined) return yield* Effect.die("Missing session handler");
399+
const sessionId = providerThread.nativeThreadRef?.nativeId;
400+
if (sessionId == null) return yield* Effect.die("Missing native session");
401+
yield* sessionUpdate({
402+
sessionId,
403+
update: {
404+
sessionUpdate: "compaction_update",
405+
compactionId: "idle-compact",
406+
status: "completed",
407+
},
408+
});
409+
}
410+
if (reopen === "rollback") {
411+
const result = yield* session.runtime.rollbackThread({
412+
providerThread,
413+
providerThreadTurns: [],
414+
target: {
415+
type: "thread_start",
416+
checkpointId: CheckpointId.make("instruction-rollback"),
417+
appRunOrdinal: 0,
418+
},
419+
});
420+
providerThread = result.providerThread;
421+
}
422+
if (reopen === true) {
388423
const nativeSessionId = providerThread.nativeThreadRef?.nativeId;
389424
if (nativeSessionId == null) return yield* Effect.die("Expected a persisted native session");
390425
yield* Scope.close(session.scope, Exit.void);
@@ -438,6 +473,22 @@ function assertReinjectedInstructions(
438473
}
439474

440475
describe("AcpAdapterV2", () => {
476+
it.live.each(["rollback", "compact"] as const)(
477+
"reinjects instructions after idle %s with no continuation consumer",
478+
(transition) =>
479+
Effect.gen(function* () {
480+
const runTurn = yield* makeInstructionCompactionHarness(
481+
{},
482+
false,
483+
transition !== "rollback",
484+
);
485+
assertReinjectedInstructions(yield* runTurn("Start work."), "Start work.");
486+
assertReinjectedInstructions(
487+
yield* runTurn("Continue after reset.", "completed", transition),
488+
"Continue after reset.",
489+
);
490+
}).pipe(Effect.provide(layerTest), Effect.scoped),
491+
);
441492
it.live.each(["details", "none"] as const)(
442493
"fails the turn on an ACP v2 idle error with %s, settles its open tool, and accepts the next prompt",
443494
(details) =>

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
19291929
const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) =>
19301930
Queue.offer(events, event).pipe(Effect.asVoid);
19311931
const recordInstructions = Effect.fnUntraced(function* (
1932-
context: ActiveAcpTurn,
1932+
context: Pick<ActiveAcpTurn, "nativeThreadId" | "nativeMetadata">,
19331933
receipt: NonNullable<
19341934
OrchestrationV2ProviderThreadNativeMetadata["acpInstructions"]
19351935
> | null,
@@ -4507,6 +4507,34 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
45074507
) {
45084508
const context = yield* Ref.get(activeTurn);
45094509
const update = notification.update;
4510+
if (
4511+
(context === null || context.finalized) &&
4512+
notification.sessionId === (yield* Ref.get(activeSessionId)) &&
4513+
update.sessionUpdate === "compaction_update" &&
4514+
update.status === "completed"
4515+
) {
4516+
const known = (yield* Ref.get(providerThreadByNativeSessionId)).get(
4517+
notification.sessionId,
4518+
);
4519+
yield* recordInstructions(
4520+
{
4521+
nativeThreadId: notification.sessionId,
4522+
nativeMetadata:
4523+
(yield* Ref.get(nativeMetadataBySessionId)).get(notification.sessionId) ??
4524+
known?.nativeMetadata ??
4525+
null,
4526+
},
4527+
null,
4528+
);
4529+
yield* Ref.update(promptInstructionStates, (current) => {
4530+
const updated = new Map(current);
4531+
updated.delete(notification.sessionId);
4532+
return updated;
4533+
});
4534+
yield* Ref.update(compactionMarkers, (current) =>
4535+
new Map(current).set(notification.sessionId, Symbol()),
4536+
);
4537+
}
45104538
if (
45114539
followsAgentWorkState &&
45124540
flavor.extractSubagentSessionEvent !== undefined &&
@@ -8370,6 +8398,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
83708398
yield* Ref.set(activeSelection, null);
83718399
yield* Ref.set(activeInteractionMode, null);
83728400
yield* Ref.set(promptInstructionStates, new Map());
8401+
yield* Ref.update(nativeMetadataBySessionId, (current) =>
8402+
new Map(current).set(candidate.sessionId, {
8403+
...current.get(candidate.sessionId),
8404+
acpInstructions: null,
8405+
}),
8406+
);
83738407
yield* Ref.set(compactionMarkers, new Map());
83748408
yield* Ref.set(providerTurns, new Map());
83758409
yield* Ref.set(snapshot, {

0 commit comments

Comments
 (0)