Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit bcd640b

Browse files
[codex] prevent ACP assistant ID collisions after restarts (pingdotgg#3932)
Co-authored-by: Julius Marminge <[email protected]>
1 parent b49a2e6 commit bcd640b

14 files changed

Lines changed: 90 additions & 14 deletions

‎apps/server/src/provider/Drivers/CursorDriver.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
101101
defaultConfig: (): CursorSettings => decodeCursorSettings({}),
102102
create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
103103
Effect.gen(function* () {
104+
const crypto = yield* Crypto.Crypto;
104105
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
105106
const fileSystem = yield* FileSystem.FileSystem;
106107
const path = yield* Path.Path;
@@ -133,6 +134,7 @@ export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
133134

134135
const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
135136
Effect.map(stampIdentity),
137+
Effect.provideService(Crypto.Crypto, crypto),
136138
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
137139
Effect.provideService(FileSystem.FileSystem, fileSystem),
138140
Effect.provideService(Path.Path, path),

‎apps/server/src/provider/Drivers/GrokDriver.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
8484
defaultConfig: (): GrokSettings => decodeGrokSettings({}),
8585
create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
8686
Effect.gen(function* () {
87+
const crypto = yield* Crypto.Crypto;
8788
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
8889
const httpClient = yield* HttpClient.HttpClient;
8990
const serverSettings = yield* ServerSettingsService;
@@ -114,6 +115,7 @@ export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
114115

115116
const checkProvider = checkGrokProviderStatus(effectiveConfig, processEnv).pipe(
116117
Effect.map(stampIdentity),
118+
Effect.provideService(Crypto.Crypto, crypto),
117119
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
118120
);
119121

‎apps/server/src/provider/Layers/CursorAdapter.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
228228
assert.isDefined(delta);
229229
if (delta?.type === "content.delta") {
230230
assert.equal(delta.payload.delta, "hello from mock");
231-
assert.match(String(delta.itemId), /^assistant:mock-session-1:segment:0$/);
231+
assert.match(String(delta.itemId), /^assistant:mock-session-1:runtime:[^:]+:segment:0$/);
232232
}
233233

234234
const assistantCompleted = runtimeEvents.find(
@@ -687,7 +687,10 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
687687
if (contentDelta?.type === "content.delta") {
688688
assert.equal(String(contentDelta.turnId), String(turn.turnId));
689689
assert.equal(contentDelta.payload.delta, "hello from mock");
690-
assert.equal(String(contentDelta.itemId), "assistant:mock-session-1:segment:0");
690+
assert.match(
691+
String(contentDelta.itemId),
692+
/^assistant:mock-session-1:runtime:[^:]+:segment:0$/,
693+
);
691694
}
692695
});
693696

‎apps/server/src/provider/Layers/CursorAdapter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ export function makeCursorAdapter(
558558
: {}),
559559
...acpNativeLoggers,
560560
}).pipe(
561+
Effect.provideService(Crypto.Crypto, crypto),
561562
Effect.provideService(Scope.Scope, sessionScope),
562563
Effect.mapError(
563564
(cause) =>

‎apps/server/src/provider/Layers/CursorProvider.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as NodeOS from "node:os";
22

33
import * as NodeServices from "@effect/platform-node/NodeServices";
4+
import type * as Crypto from "effect/Crypto";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Path from "effect/Path";
@@ -28,7 +29,7 @@ const runNode = <A, E>(
2829
effect: Effect.Effect<
2930
A,
3031
E,
31-
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
32+
ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto | FileSystem.FileSystem | Path.Path
3233
>,
3334
): Promise<A> => Effect.runPromise(effect.pipe(Effect.provide(NodeServices.layer)));
3435

‎apps/server/src/provider/Layers/CursorProvider.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
import { ProviderDriverKind } from "@t3tools/contracts";
1212
import type * as EffectAcpSchema from "effect-acp/schema";
1313
import { causeErrorTag } from "@t3tools/shared/observability";
14+
import * as Crypto from "effect/Crypto";
1415
import * as DateTime from "effect/DateTime";
1516
import * as Effect from "effect/Effect";
1617
import * as Exit from "effect/Exit";
@@ -992,7 +993,7 @@ export const checkCursorProviderStatus = Effect.fn("checkCursorProviderStatus")(
992993
): Effect.fn.Return<
993994
ServerProviderDraft,
994995
never,
995-
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
996+
ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto | FileSystem.FileSystem | Path.Path
996997
> {
997998
const checkedAt = DateTime.formatIso(yield* DateTime.now);
998999
const fallbackModels = getCursorFallbackModels(cursorSettings);

‎apps/server/src/provider/Layers/GrokAdapter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
596596
: {}),
597597
...acpNativeLoggers,
598598
}).pipe(
599+
Effect.provideService(Crypto.Crypto, crypto),
599600
Effect.provideService(Scope.Scope, sessionScope),
600601
Effect.mapError(
601602
(cause) =>

‎apps/server/src/provider/Layers/GrokProvider.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@t3tools/contracts";
88
import type * as EffectAcpSchema from "effect-acp/schema";
99
import { causeErrorTag } from "@t3tools/shared/observability";
10+
import * as Crypto from "effect/Crypto";
1011
import * as DateTime from "effect/DateTime";
1112
import * as Effect from "effect/Effect";
1213
import * as Exit from "effect/Exit";
@@ -167,7 +168,11 @@ const runGrokVersionCommand = (
167168
export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(function* (
168169
grokSettings: GrokSettings,
169170
environment: NodeJS.ProcessEnv = process.env,
170-
): Effect.fn.Return<ServerProviderDraft, never, ChildProcessSpawner.ChildProcessSpawner> {
171+
): Effect.fn.Return<
172+
ServerProviderDraft,
173+
never,
174+
ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto
175+
> {
171176
const checkedAt = DateTime.formatIso(yield* DateTime.now);
172177
const fallbackModels = grokModelsFromSettings(grokSettings.customModels);
173178

‎apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,43 @@ describe("AcpSessionRuntime", () => {
116116
),
117117
);
118118

119+
it.effect("keeps assistant item IDs unique when a provider session restarts", () => {
120+
const collectFirstAssistantItemId = Effect.gen(function* () {
121+
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;
122+
const started = yield* runtime.start();
123+
expect(started.sessionId).toBe("mock-session-1");
124+
125+
yield* runtime.prompt({
126+
prompt: [{ type: "text", text: "hi" }],
127+
});
128+
129+
const events = Array.from(yield* Stream.runCollect(Stream.take(runtime.getEvents(), 4)));
130+
const assistantStart = events.find((event) => event._tag === "AssistantItemStarted");
131+
expect(assistantStart?._tag).toBe("AssistantItemStarted");
132+
return assistantStart?._tag === "AssistantItemStarted" ? assistantStart.itemId : "";
133+
}).pipe(
134+
Effect.provide(
135+
AcpSessionRuntime.layer({
136+
spawn: {
137+
command: mockAgentCommand,
138+
args: mockAgentArgs,
139+
},
140+
cwd: process.cwd(),
141+
clientInfo: { name: "t3-test", version: "0.0.0" },
142+
authMethodId: "test",
143+
}),
144+
),
145+
Effect.scoped,
146+
);
147+
148+
return Effect.gen(function* () {
149+
const beforeRestart = yield* collectFirstAssistantItemId;
150+
const afterRestart = yield* collectFirstAssistantItemId;
151+
152+
expect(afterRestart).not.toBe(beforeRestart);
153+
}).pipe(Effect.provide(NodeServices.layer));
154+
});
155+
119156
it.effect("drops session updates emitted for a child ACP session", () =>
120157
Effect.gen(function* () {
121158
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;

‎apps/server/src/provider/acp/AcpSessionRuntime.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Cause from "effect/Cause";
22
import * as Clock from "effect/Clock";
33
import * as Context from "effect/Context";
4+
import * as Crypto from "effect/Crypto";
45
import * as Deferred from "effect/Deferred";
56
import * as Duration from "effect/Duration";
67
import * as Effect from "effect/Effect";
@@ -270,14 +271,24 @@ export const make = (
270271
): Effect.Effect<
271272
AcpSessionRuntime["Service"],
272273
EffectAcpErrors.AcpError,
273-
ChildProcessSpawner.ChildProcessSpawner | Scope.Scope
274+
ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto | Scope.Scope
274275
> =>
275276
Effect.gen(function* () {
277+
const crypto = yield* Crypto.Crypto;
276278
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
277279
const runtimeScope = yield* Scope.Scope;
278280
const eventQueue = yield* Queue.unbounded<AcpSessionRuntimeEvent>();
279281
const modeStateRef = yield* Ref.make<AcpSessionModeState | undefined>(undefined);
280282
const toolCallsRef = yield* Ref.make(new Map<string, AcpToolCallState>());
283+
const assistantItemRuntimeId = yield* crypto.randomUUIDv4.pipe(
284+
Effect.mapError(
285+
(cause) =>
286+
new EffectAcpErrors.AcpTransportError({
287+
detail: "Failed to generate an ACP assistant item runtime identifier.",
288+
cause,
289+
}),
290+
),
291+
);
281292
const assistantSegmentRef = yield* Ref.make<AcpAssistantSegmentState>({ nextSegmentIndex: 0 });
282293
const configOptionsRef = yield* Ref.make(sessionConfigOptionsFromSetup(undefined));
283294
const startStateRef = yield* Ref.make<AcpStartState>({ _tag: "NotStarted" });
@@ -387,6 +398,7 @@ export const make = (
387398
modeStateRef,
388399
toolCallsRef,
389400
assistantSegmentRef,
401+
assistantItemRuntimeId,
390402
params: notification,
391403
});
392404
}),
@@ -802,7 +814,7 @@ export const layer = (
802814
): Layer.Layer<
803815
AcpSessionRuntime,
804816
EffectAcpErrors.AcpError,
805-
ChildProcessSpawner.ChildProcessSpawner
817+
ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto
806818
> => Layer.effect(AcpSessionRuntime, make(options));
807819

808820
function sessionConfigOptionsFromSetup(
@@ -834,12 +846,14 @@ const handleSessionUpdate = ({
834846
modeStateRef,
835847
toolCallsRef,
836848
assistantSegmentRef,
849+
assistantItemRuntimeId,
837850
params,
838851
}: {
839852
readonly queue: Queue.Queue<AcpSessionRuntimeEvent>;
840853
readonly modeStateRef: Ref.Ref<AcpSessionModeState | undefined>;
841854
readonly toolCallsRef: Ref.Ref<Map<string, AcpToolCallState>>;
842855
readonly assistantSegmentRef: Ref.Ref<AcpAssistantSegmentState>;
856+
readonly assistantItemRuntimeId: string;
843857
readonly params: EffectAcpSchema.SessionNotification;
844858
}): Effect.Effect<void> =>
845859
Effect.gen(function* () {
@@ -887,6 +901,7 @@ const handleSessionUpdate = ({
887901
queue,
888902
assistantSegmentRef,
889903
sessionId: params.sessionId,
904+
assistantItemRuntimeId,
890905
});
891906
yield* Queue.offer(queue, {
892907
...event,
@@ -924,25 +939,27 @@ function shouldEmitToolCallUpdate(
924939
return previous === undefined || previous.title !== next.title || previous.detail !== next.detail;
925940
}
926941

927-
const assistantItemId = (sessionId: string, segmentIndex: number) =>
928-
`assistant:${sessionId}:segment:${segmentIndex}`;
942+
const assistantItemId = (sessionId: string, runtimeId: string, segmentIndex: number) =>
943+
`assistant:${sessionId}:runtime:${runtimeId}:segment:${segmentIndex}`;
929944

930945
const ensureActiveAssistantSegment = ({
931946
queue,
932947
assistantSegmentRef,
933948
sessionId,
949+
assistantItemRuntimeId,
934950
}: {
935951
readonly queue: Queue.Queue<AcpSessionRuntimeEvent>;
936952
readonly assistantSegmentRef: Ref.Ref<AcpAssistantSegmentState>;
937953
readonly sessionId: string;
954+
readonly assistantItemRuntimeId: string;
938955
}) =>
939956
Ref.modify<AcpAssistantSegmentState, EnsureActiveAssistantSegmentResult>(
940957
assistantSegmentRef,
941958
(current) => {
942959
if (current.activeItemId) {
943960
return [{ itemId: current.activeItemId }, current] as const;
944961
}
945-
const itemId = assistantItemId(sessionId, current.nextSegmentIndex);
962+
const itemId = assistantItemId(sessionId, assistantItemRuntimeId, current.nextSegmentIndex);
946963
return [
947964
{
948965
itemId,

0 commit comments

Comments
 (0)