Skip to content

Commit 8bf89eb

Browse files
committed
fix(acp): persist instruction invalidation across idle sessions
1 parent 206ef81 commit 8bf89eb

4 files changed

Lines changed: 217 additions & 26 deletions

File tree

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
392392
return Effect.fnUntraced(function* (
393393
messageText: string,
394394
expectedStatus = "completed",
395-
reopen: boolean | "rollback" | "compact" = false,
395+
reopen: boolean | "rollback" | "compact" | "resume-compact-reopen" = false,
396396
interactionMode: "default" | "plan" = "default",
397397
) {
398398
const turnRuntimePolicy = { ...runtimePolicy, interactionMode };
@@ -421,7 +421,7 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
421421
});
422422
providerThread = result.providerThread;
423423
}
424-
if (reopen === true) {
424+
const resume = Effect.fnUntraced(function* () {
425425
const nativeSessionId = providerThread.nativeThreadRef?.nativeId;
426426
if (nativeSessionId == null) return yield* Effect.die("Expected a persisted native session");
427427
yield* Scope.close(session.scope, Exit.void);
@@ -431,6 +431,27 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
431431
modelSelection,
432432
runtimePolicy: turnRuntimePolicy,
433433
});
434+
});
435+
if (reopen === true || reopen === "resume-compact-reopen") yield* resume();
436+
if (reopen === "resume-compact-reopen") {
437+
if (sessionUpdate === undefined) return yield* Effect.die("Missing session handler");
438+
const sessionId = providerThread.nativeThreadRef?.nativeId;
439+
if (sessionId == null) return yield* Effect.die("Missing native session");
440+
yield* sessionUpdate({
441+
sessionId,
442+
update: {
443+
sessionUpdate: "compaction_update",
444+
compactionId: "resumed-idle-compact",
445+
status: "completed",
446+
},
447+
});
448+
const updated = yield* session.runtime.events.pipe(
449+
Stream.filter((event) => event.type === "provider_thread.updated"),
450+
Stream.runHead,
451+
);
452+
providerThread = Option.getOrThrow(updated).providerThread;
453+
assert.isNull(providerThread.nativeMetadata?.acpInstructions);
454+
yield* resume();
434455
}
435456
const { runtime } = session;
436457
yield* runtime.startTurn(
@@ -1135,6 +1156,17 @@ describe("AcpAdapterV2", () => {
11351156
}).pipe(Effect.provide(layerTest), Effect.scoped),
11361157
);
11371158

1159+
it.live("persists idle compaction after resume before another turn", () =>
1160+
Effect.gen(function* () {
1161+
const runTurn = yield* makeInstructionCompactionHarness({});
1162+
yield* runTurn("Start work.");
1163+
assertReinjectedInstructions(
1164+
yield* runTurn("Continue after idle compaction.", "completed", "resume-compact-reopen"),
1165+
"Continue after idle compaction.",
1166+
);
1167+
}).pipe(Effect.provide(layerTest), Effect.scoped),
1168+
);
1169+
11381170
it.live.each(["native", "protocol"] as const)(
11391171
"retains ACP instructions across reopen but reinjects after %s compaction",
11401172
(compaction) =>

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
ProjectId,
1515
ProviderDriverKind,
1616
ProviderInstanceId,
17-
type ProviderSessionId,
17+
ProviderSessionId,
1818
ThreadId,
1919
} from "@t3tools/contracts";
2020
import * as Cause from "effect/Cause";
@@ -2900,6 +2900,90 @@ it.effect("ProviderSessionManagerV2 settles a request the event pump persists du
29002900
}),
29012901
);
29022902

2903+
it.effect.each(["current", "newer-run", "newer-session"] as const)(
2904+
"ProviderSessionManagerV2 persists instruction invalidation only for its owner (%s)",
2905+
(owner) =>
2906+
Effect.gen(function* () {
2907+
const state = yield* Ref.make(emptyState);
2908+
yield* Effect.gen(function* () {
2909+
const eventSink = yield* EventSink.EventSinkV2;
2910+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2911+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
2912+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2913+
const now = yield* DateTime.now;
2914+
const threadId = ThreadId.make("instruction-invalidation");
2915+
const providerSessionId = yield* idAllocator.allocate.providerSession({
2916+
providerInstanceId: modelSelection.instanceId,
2917+
threadId,
2918+
});
2919+
const providerThread = {
2920+
...makeProviderThread({ idAllocator, threadId, providerSessionId, now }),
2921+
lastRunOrdinal: 1,
2922+
nativeMetadata: {
2923+
title: "current title",
2924+
acpInstructions: {
2925+
nativeSessionId: "native-thread",
2926+
digest: "delivered-instructions",
2927+
},
2928+
},
2929+
};
2930+
const stored = {
2931+
...providerThread,
2932+
...(owner === "newer-run" ? { lastRunOrdinal: 2 } : {}),
2933+
...(owner === "newer-session"
2934+
? { providerSessionId: ProviderSessionId.make("newer-session") }
2935+
: {}),
2936+
};
2937+
yield* eventSink.write({
2938+
events: [
2939+
yield* makeThreadCreatedEvent({ idAllocator, threadId, now }),
2940+
{
2941+
id: yield* idAllocator.allocate.event({ threadId, providerSessionId }),
2942+
type: "provider-thread.updated",
2943+
threadId,
2944+
driver: CODEX_DRIVER,
2945+
occurredAt: now,
2946+
payload: stored,
2947+
},
2948+
],
2949+
});
2950+
const before = (yield* projectionStore.getThreadRecords(threadId, ["providerThreads"]))
2951+
.providerThreads[0]!;
2952+
const runtime = yield* manager.open({
2953+
threadId,
2954+
providerSessionId,
2955+
modelSelection,
2956+
runtimePolicy,
2957+
});
2958+
const subscription = yield* runtime.subscribeEvents!;
2959+
const queue = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId));
2960+
assert.isDefined(queue);
2961+
yield* Queue.offer(queue!, {
2962+
type: "provider_thread.updated",
2963+
driver: CODEX_DRIVER,
2964+
providerThread: {
2965+
...providerThread,
2966+
nativeMetadata: { title: "stale title", acpInstructions: null },
2967+
},
2968+
});
2969+
yield* subscription.events.pipe(Stream.runHead);
2970+
const projection = yield* projectionStore.getThreadRecords(threadId, ["providerThreads"]);
2971+
const updated = projection.providerThreads.find(
2972+
(candidate) => candidate.id === providerThread.id,
2973+
);
2974+
assert.deepEqual(
2975+
updated,
2976+
owner === "current"
2977+
? {
2978+
...before,
2979+
nativeMetadata: { ...before.nativeMetadata, acpInstructions: null },
2980+
}
2981+
: before,
2982+
);
2983+
}).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1000 })));
2984+
}),
2985+
);
2986+
29032987
it.effect("ProviderSessionManagerV2 persists session-scoped runtime requests without a run", () =>
29042988
Effect.gen(function* () {
29052989
const state = yield* Ref.make(emptyState);

‎apps/server/src/orchestration-v2/ProviderSessionManager.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,48 @@ export const layerWithOptions = (
18761876
),
18771877
);
18781878

1879+
const persistInstructionInvalidation = Effect.fn(
1880+
"ProviderSessionManager.persistInstructionInvalidation",
1881+
)(function* (entry: LiveSessionEntry, offered: OrchestrationV2ProviderThread) {
1882+
const threadId = offered.appThreadId;
1883+
if (threadId === null || offered.nativeMetadata?.acpInstructions !== null) return;
1884+
// Idle updates outlive run subscribers; invalidate only the receipt, not newer thread state.
1885+
yield* Effect.gen(function* () {
1886+
const current = (yield* Ref.get(sessions)).get(
1887+
sessionKey(entry.runtime.providerSessionId),
1888+
);
1889+
if (current?.runtime !== entry.runtime || !current.attachedThreadIds.has(threadId))
1890+
return true;
1891+
const projection = yield* projectionStore.getThreadRecords(threadId, ["providerThreads"]);
1892+
const stored = projection.providerThreads.find(
1893+
(candidate) => candidate.id === offered.id,
1894+
);
1895+
if (
1896+
stored === undefined ||
1897+
stored.providerSessionId !== entry.runtime.providerSessionId ||
1898+
stored.lastRunOrdinal !== offered.lastRunOrdinal ||
1899+
stored.nativeThreadRef?.nativeId !== offered.nativeThreadRef?.nativeId ||
1900+
stored.nativeMetadata?.acpInstructions == null
1901+
)
1902+
return true;
1903+
const events = yield* providerEventIngestor.normalize({
1904+
providerSessionId: entry.runtime.providerSessionId,
1905+
providerInstanceId: entry.runtime.instanceId,
1906+
threadId,
1907+
event: {
1908+
type: "provider_thread.updated",
1909+
driver: entry.runtime.driver,
1910+
providerThread: {
1911+
...stored,
1912+
nativeMetadata: { ...stored.nativeMetadata, acpInstructions: null },
1913+
},
1914+
},
1915+
});
1916+
return (yield* eventSink.writeIfProviderThreadUnchanged({ expected: stored, events }))
1917+
.committed;
1918+
}).pipe(Effect.repeat({ until: (committed) => committed }));
1919+
});
1920+
18791921
const startEventPump = (entry: LiveSessionEntry) => {
18801922
let stoppedByProvider = false;
18811923
return entry.runtime.events.pipe(
@@ -1917,6 +1959,18 @@ export const layerWithOptions = (
19171959
),
19181960
Effect.andThen(
19191961
Effect.gen(function* () {
1962+
if (event.type === "provider_thread.updated") {
1963+
yield* persistInstructionInvalidation(entry, event.providerThread).pipe(
1964+
Effect.mapError(
1965+
(cause) =>
1966+
new ProviderAdapter.ProviderAdapterEventStreamError({
1967+
driver: entry.runtime.driver,
1968+
providerSessionId: entry.runtime.providerSessionId,
1969+
cause,
1970+
}),
1971+
),
1972+
);
1973+
}
19201974
if (
19211975
event.type === "provider_thread.updated" &&
19221976
event.providerThread.status === "error"

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

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,13 +1933,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
19331933

19341934
const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) =>
19351935
Queue.offer(events, event).pipe(Effect.asVoid);
1936-
const recordInstructions = Effect.fnUntraced(function* (
1936+
const invalidateInstructions = Effect.fn("AcpAdapterV2.invalidateInstructions")(function* (
19371937
context: Pick<ActiveAcpTurn, "nativeThreadId" | "nativeMetadata">,
1938-
receipt: NonNullable<
1939-
OrchestrationV2ProviderThreadNativeMetadata["acpInstructions"]
1940-
> | null,
19411938
) {
1942-
const metadata = { ...context.nativeMetadata, acpInstructions: receipt };
1939+
const metadata = { ...context.nativeMetadata, acpInstructions: null };
19431940
context.nativeMetadata = metadata;
19441941
yield* Ref.update(nativeMetadataBySessionId, (current) =>
19451942
new Map(current).set(context.nativeThreadId, metadata),
@@ -3686,7 +3683,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
36863683
: { beforeTokenCount: context.contextUsage.usedTokens }),
36873684
};
36883685
if (status === "completed") {
3689-
yield* recordInstructions(context, null);
3686+
yield* invalidateInstructions(context);
36903687
context.contextUsage = null;
36913688
yield* Ref.update(contextUsageBySessionId, (current) => {
36923689
const updated = new Map(current);
@@ -4521,16 +4518,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
45214518
const known = (yield* Ref.get(providerThreadByNativeSessionId)).get(
45224519
notification.sessionId,
45234520
);
4524-
yield* recordInstructions(
4525-
{
4526-
nativeThreadId: notification.sessionId,
4527-
nativeMetadata:
4528-
(yield* Ref.get(nativeMetadataBySessionId)).get(notification.sessionId) ??
4529-
known?.nativeMetadata ??
4530-
null,
4531-
},
4532-
null,
4533-
);
4521+
yield* invalidateInstructions({
4522+
nativeThreadId: notification.sessionId,
4523+
nativeMetadata:
4524+
(yield* Ref.get(nativeMetadataBySessionId)).get(notification.sessionId) ??
4525+
known?.nativeMetadata ??
4526+
null,
4527+
});
45344528
yield* Ref.update(promptInstructionStates, (current) => {
45354529
const updated = new Map(current);
45364530
updated.delete(notification.sessionId);
@@ -6982,10 +6976,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
69826976
(yield* Ref.get(compactionMarkers)).get(context.nativeThreadId) ===
69836977
delivery.compactionMarker
69846978
) {
6985-
yield* recordInstructions(context, {
6986-
nativeSessionId: context.nativeThreadId,
6987-
digest: delivery.digest,
6988-
});
6979+
context.nativeMetadata = {
6980+
...context.nativeMetadata,
6981+
acpInstructions: { nativeSessionId: context.nativeThreadId, digest: delivery.digest },
6982+
};
69896983
yield* Ref.update(promptInstructionStates, (current) =>
69906984
new Map(current).set(context.nativeThreadId, delivery.state),
69916985
);
@@ -7002,7 +6996,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
70026996
settledStatus === "completed"
70036997
) {
70046998
const nativeItemId = `${context.nativeTurnId}:compaction`;
7005-
yield* recordInstructions(context, null);
6999+
yield* invalidateInstructions(context);
70067000
yield* Ref.update(compactionMarkers, (current) => {
70077001
const updated = new Map(current);
70087002
updated.set(context.nativeThreadId, Symbol());
@@ -7043,6 +7037,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
70437037
threadId: context.input.threadId,
70447038
providerTurn: turn,
70457039
});
7040+
const nativeMetadata = context.nativeMetadata;
7041+
if (nativeMetadata !== null) {
7042+
yield* Ref.update(nativeMetadataBySessionId, (current) =>
7043+
new Map(current).set(context.nativeThreadId, nativeMetadata),
7044+
);
7045+
}
70467046
const updatedProviderThread: OrchestrationV2ProviderThread = {
70477047
...context.input.providerThread,
70487048
providerSessionId: input.providerSessionId,
@@ -7610,7 +7610,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
76107610
}
76117611
const promptGeneration = yield* Ref.get(runtimeCallbackGeneration);
76127612
if (promptParts?.instructionState !== undefined) {
7613-
yield* recordInstructions(context, null);
7613+
yield* invalidateInstructions(context);
76147614
yield* Ref.update(promptInstructionStates, (current) => {
76157615
const updated = new Map(current);
76167616
updated.delete(requestedSessionId);
@@ -7890,6 +7890,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
78907890
Effect.gen(function* () {
78917891
yield* awaitRuntimeTeardown();
78927892
const sessionId = yield* nativeThreadId(driver, threadInput.providerThread);
7893+
yield* Ref.update(providerThreadByNativeSessionId, (current) =>
7894+
new Map(current).set(sessionId, {
7895+
...threadInput.providerThread,
7896+
providerSessionId: input.providerSessionId,
7897+
}),
7898+
);
78937899
const previousItemIdentityVersion = itemIdentityVersion;
78947900
useProviderThreadIdentity(threadInput.providerThread);
78957901
const restorePreviousItemIdentity = Effect.sync(() => {
@@ -7919,12 +7925,27 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
79197925
yield* Ref.set(activeInteractionMode, nextRuntimePolicy.interactionMode);
79207926
}
79217927
const now = yield* DateTime.now;
7922-
return {
7928+
const rememberedMetadata = (yield* Ref.get(nativeMetadataBySessionId)).get(
7929+
sessionId,
7930+
);
7931+
const providerThread = {
79237932
...threadInput.providerThread,
79247933
providerSessionId: input.providerSessionId,
7934+
...(rememberedMetadata === undefined
7935+
? {}
7936+
: {
7937+
nativeMetadata: {
7938+
...threadInput.providerThread.nativeMetadata,
7939+
...rememberedMetadata,
7940+
},
7941+
}),
79257942
status: "idle" as const,
79267943
updatedAt: now,
79277944
};
7945+
yield* Ref.update(providerThreadByNativeSessionId, (current) =>
7946+
new Map(current).set(sessionId, providerThread),
7947+
);
7948+
return providerThread;
79287949
}),
79297950
);
79307951
},

0 commit comments

Comments
 (0)