Skip to content

Commit f68d312

Browse files
fix(server): keep conversation when a stopped thread switches provider instance
Switching a thread to another instance of the same driver only carried the resume cursor while the session was live. Once the session had stopped or errored, startSession discarded the persisted cursor because it belonged to a different instance id, and the provider opened a fresh thread. Reuse the persisted cursor and cwd whenever the two instances share resume state, which the continuation-key check directly above already establishes. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent ae53072 commit f68d312

2 files changed

Lines changed: 92 additions & 11 deletions

File tree

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,89 @@ it.effect("ProviderServiceLive rejects new sessions for disabled custom instance
986986
}).pipe(Effect.provide(NodeServices.layer)),
987987
);
988988

989+
it.effect(
990+
"ProviderServiceLive resumes a stopped session when switching to an instance with shared resume state",
991+
() =>
992+
Effect.gen(function* () {
993+
const primaryInstanceId = ProviderInstanceId.make("codex");
994+
const overlayInstanceId = ProviderInstanceId.make("codex_b");
995+
const codex = makeFakeCodexAdapter();
996+
const unsupported = () =>
997+
new ProviderUnsupportedError({
998+
provider: CODEX_DRIVER,
999+
});
1000+
const instanceIds = new Set<ProviderInstanceId>([primaryInstanceId, overlayInstanceId]);
1001+
const registry: ProviderAdapterRegistry.ProviderAdapterRegistry["Service"] = {
1002+
getByInstance: (requestedInstanceId) =>
1003+
instanceIds.has(requestedInstanceId)
1004+
? Effect.succeed(codex.adapter)
1005+
: Effect.fail(unsupported()),
1006+
getInstanceInfo: (requestedInstanceId) =>
1007+
instanceIds.has(requestedInstanceId)
1008+
? Effect.succeed({
1009+
instanceId: requestedInstanceId,
1010+
driverKind: CODEX_DRIVER,
1011+
displayName: undefined,
1012+
enabled: true,
1013+
continuationIdentity: {
1014+
driverKind: CODEX_DRIVER,
1015+
continuationKey: "codex:home:/Users/example/.codex",
1016+
},
1017+
})
1018+
: Effect.fail(unsupported()),
1019+
listInstances: () => Effect.succeed(Array.from(instanceIds)),
1020+
subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), (pubsub) =>
1021+
PubSub.subscribe(pubsub),
1022+
),
1023+
};
1024+
const runtimeRepositoryLayer = ProviderSessionRuntime.layer.pipe(
1025+
Layer.provide(SqlitePersistenceMemory),
1026+
);
1027+
const providerLayer = makeProviderServiceLive().pipe(
1028+
Layer.provide(NodeServices.layer),
1029+
Layer.provide(Layer.succeed(ProviderAdapterRegistry.ProviderAdapterRegistry, registry)),
1030+
Layer.provide(ProviderSessionDirectoryLive.pipe(Layer.provide(runtimeRepositoryLayer))),
1031+
Layer.provide(defaultServerSettingsLayer),
1032+
Layer.provide(serverConfigTestLayer),
1033+
Layer.provide(AnalyticsService.layerTest),
1034+
Layer.provide(
1035+
Layer.succeed(
1036+
ProviderEventLoggers.ProviderEventLoggers,
1037+
ProviderEventLoggers.NoOpProviderEventLoggers,
1038+
),
1039+
),
1040+
);
1041+
1042+
const threadId = asThreadId("thread-instance-switch");
1043+
const cwd = fixtureCwd("project-instance-switch");
1044+
yield* Effect.gen(function* () {
1045+
const provider = yield* ProviderService.ProviderService;
1046+
const initial = yield* provider.startSession(threadId, {
1047+
provider: CODEX_DRIVER,
1048+
providerInstanceId: primaryInstanceId,
1049+
threadId,
1050+
cwd,
1051+
runtimeMode: "full-access",
1052+
});
1053+
yield* provider.stopSession({ threadId });
1054+
codex.startSession.mockClear();
1055+
1056+
const resumed = yield* provider.startSession(threadId, {
1057+
provider: CODEX_DRIVER,
1058+
providerInstanceId: overlayInstanceId,
1059+
threadId,
1060+
runtimeMode: "full-access",
1061+
});
1062+
1063+
assert.equal(resumed.providerInstanceId, overlayInstanceId);
1064+
assert.equal(codex.startSession.mock.calls.length, 1);
1065+
const startPayload = codex.startSession.mock.calls[0]?.[0];
1066+
assert.deepEqual(startPayload?.resumeCursor, initial.resumeCursor);
1067+
assert.equal(startPayload?.cwd, cwd);
1068+
}).pipe(Effect.provide(providerLayer));
1069+
}).pipe(Effect.provide(NodeServices.layer)),
1070+
);
1071+
9891072
const routing = makeProviderServiceLayer();
9901073

9911074
const customCompactionDriver = ProviderDriverKind.make("custom-compaction-provider");

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
14351435
);
14361436
}
14371437
const persistedBinding = Option.getOrUndefined(yield* directory.getBinding(threadId));
1438+
let reusePersistedState = persistedBinding?.providerInstanceId === resolvedInstanceId;
14381439
if (
14391440
persistedBinding?.provider === resolvedProvider &&
14401441
persistedBinding.providerInstanceId !== resolvedInstanceId &&
@@ -1454,32 +1455,29 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
14541455
`Thread '${threadId}' cannot switch from instance '${previousInstanceId}' to '${resolvedInstanceId}' because their provider resume state is incompatible.`,
14551456
);
14561457
}
1458+
// Instances that share resume state (e.g. Codex auth overlays over one
1459+
// home) continue the same provider thread, so a stopped session keeps
1460+
// its conversation when the thread moves between them.
1461+
reusePersistedState = true;
14571462
}
14581463
const effectiveResumeCursor =
1459-
input.resumeCursor ??
1460-
(persistedBinding?.providerInstanceId === resolvedInstanceId
1461-
? persistedBinding.resumeCursor
1462-
: undefined);
1464+
input.resumeCursor ?? (reusePersistedState ? persistedBinding?.resumeCursor : undefined);
14631465
const effectiveCwd =
14641466
input.cwd ??
1465-
(persistedBinding?.providerInstanceId === resolvedInstanceId
1466-
? readPersistedCwd(persistedBinding.runtimePayload)
1467-
: undefined);
1467+
(reusePersistedState ? readPersistedCwd(persistedBinding?.runtimePayload) : undefined);
14681468
yield* Effect.annotateCurrentSpan({
14691469
"provider.kind": resolvedProvider,
14701470
"provider.resume_cursor.source":
14711471
input.resumeCursor !== undefined
14721472
? "request"
1473-
: effectiveResumeCursor !== undefined &&
1474-
persistedBinding?.providerInstanceId === resolvedInstanceId
1473+
: effectiveResumeCursor !== undefined && reusePersistedState
14751474
? "persisted"
14761475
: "none",
14771476
"provider.resume_cursor.present": effectiveResumeCursor !== undefined,
14781477
"provider.cwd.source":
14791478
input.cwd !== undefined
14801479
? "request"
1481-
: effectiveCwd !== undefined &&
1482-
persistedBinding?.providerInstanceId === resolvedInstanceId
1480+
: effectiveCwd !== undefined && reusePersistedState
14831481
? "persisted"
14841482
: "none",
14851483
"provider.cwd.effective": effectiveCwd ?? "",

0 commit comments

Comments
 (0)