The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 11:00 UTC
fix(acp): recognize sessions already owned by a thread (#37244)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
DrStrangerUJN committed
Nov 15, 2026 at 23:40 UTC
c2e54e3ac9c8c0d5ceaf7df5acd4f4374fa19b6f
3 files changed
+136
-29
apps/server/src/provider/AcpRegistrySessions.ts
+43
-1
@@ -1,6 +1,7 @@
1
import {
2
AcpRegistryOperationError,
3
CommandId,
4
+ ThreadId,
5
type AcpRegistryImportSessionInput,
6
type AcpRegistryImportSessionResult,
7
type Project,
@@ -16,6 +17,7 @@ import * as SecureRandom from "effect/crypto/SecureRandom";
17
import * as Effect from "effect/Effect";
18
import * as Layer from "effect/Layer";
19
import * as Result from "effect/Result";
20
+import * as SqlClient from "effect/sql/SqlClient";
21
22
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
23
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
@@ -47,6 +49,11 @@ export class AcpRegistrySessions extends Context.Service<
49
},
50
AcpRegistryOperationError
51
>;
52
+ readonly findOwner: (input: {
53
+ readonly driver: ProviderDriverKind;
54
+ readonly instanceId: ProviderInstanceId;
55
+ readonly sessionId: string;
56
+ }) => Effect.Effect<ThreadId | null, AcpRegistryOperationError>;
57
/**
58
* Binds a native ACP session to a T3 thread and copies its history in. Importing a
59
* session whose thread still lacks its history repairs that thread instead.
@@ -67,6 +74,35 @@ const make = Effect.gen(function* () {
74
const startup = yield* ServerRuntimeStartup.ServerRuntimeStartup;
75
const agentSessionImporter = yield* AgentSessionImporter.AgentSessionImporter;
76
const random = yield* SecureRandom.SecureRandom;
77
+ const sql = yield* SqlClient.SqlClient;
78
+
79
+ const findOwner = Effect.fn("AcpRegistrySessions.findOwner")(function* (input: {
80
+ readonly driver: ProviderDriverKind;
81
+ readonly instanceId: ProviderInstanceId;
82
+ readonly sessionId: string;
83
+ }) {
84
+ const rows = yield* sql<{ readonly thread_id: string }>`
85
+ SELECT provider.thread_id
86
+ FROM orchestration_v2_projection_provider_threads AS provider
87
+ JOIN orchestration_v2_projection_threads AS thread ON thread.thread_id = provider.thread_id
88
+ WHERE provider.driver = ${input.driver}
89
+ AND provider.provider_instance_id = ${input.instanceId}
90
+ AND json_extract(provider.payload_json, '$.nativeThreadRef.nativeId') = ${input.sessionId}
91
+ AND thread.deleted_at IS NULL
92
+ ORDER BY thread.updated_at DESC
93
+ LIMIT 1
94
+ `.pipe(
95
+ Effect.mapError(
96
+ (cause) =>
97
+ new AcpRegistryOperationError({
98
+ reason: "session_import_failed",
99
+ message: "Could not inspect ACP session ownership.",
100
+ cause,
101
+ }),
102
+ ),
103
+ );
104
+ return rows[0] ? ThreadId.make(rows[0].thread_id) : null;
105
+ });
106
107
const project = Effect.fn("AcpRegistrySessions.project")(function* (projectId: ProjectId) {
108
const found = yield* projectService.getById(projectId).pipe(
@@ -128,6 +164,12 @@ const make = Effect.gen(function* () {
164
instanceId: input.instanceId,
165
sessionId: input.sessionId,
166
});
167
+ const owner = yield* findOwner({
168
+ driver: instance.driverKind,
169
+ instanceId: input.instanceId,
170
+ sessionId: input.sessionId,
171
+ });
172
+ if (owner !== null && owner !== threadId) return { threadId: owner, imported: false } as const;
173
const existing = yield* threadManagement.getThreadShell(threadId).pipe(
174
Effect.mapError(
175
(cause) =>
@@ -223,7 +265,7 @@ const make = Effect.gen(function* () {
265
return { threadId, imported: true } as const;
266
}, coordinator.withSessionMutation);
267
226
- return AcpRegistrySessions.of({ project, sessionManager, importSession });
268
+ return AcpRegistrySessions.of({ project, sessionManager, findOwner, importSession });
269
});
270
271
export const layer = Layer.effect(AcpRegistrySessions, make);
apps/server/src/ws.test.ts
+60
-3
@@ -24,6 +24,7 @@ import {
24
TurnItemId,
25
ProviderDriverKind,
26
ProviderInstanceId,
27
+ ProviderThreadId,
28
RunId,
29
SourceControlRepositoryError,
30
WS_METHODS,
@@ -2345,6 +2346,7 @@ const acpThreadId = (sessionId: string) =>
2346
const acpSessionTags = [
2347
WS_METHODS.serverListAcpRegistrySessions,
2348
WS_METHODS.serverImportAcpRegistrySession,
2349
+ WS_METHODS.serverDeleteAcpRegistrySession,
2350
] as const;
2351
const acpSessionRpcs = WsRpcGroup.omit(
2352
...[...WsRpcGroup.requests.keys()].filter(
@@ -2362,7 +2364,7 @@ const acpStores = Layer.mergeAll(EventStore.layer, ProjectionStore.layer).pipe(
2364
2365
const seedAcpThread = Effect.fn(function* (
2366
sessionId: string,
2365
- state: { readonly imported: boolean; readonly ran: boolean },
2367
+ state: { readonly imported: boolean; readonly ran: boolean; readonly nativeId?: string },
2368
) {
2369
const sink = yield* EventSink.EventSinkV2;
2370
const now = DateTime.makeUnsafe("2026-10-23T00:00:00.000Z");
@@ -2400,6 +2402,32 @@ const seedAcpThread = Effect.fn(function* (
2402
payload: thread,
2403
},
2404
];
2405
+ if (state.nativeId !== undefined) {
2406
+ events.push({
2407
+ id: EventId.make(`${threadId}:provider`),
2408
+ type: "provider-thread.updated",
2409
+ threadId,
2410
+ occurredAt: now,
2411
+ payload: {
2412
+ id: ProviderThreadId.make(`${threadId}:provider`),
2413
+ appThreadId: threadId,
2414
+ ownerNodeId: null,
2415
+ driver: acpDriver,
2416
+ providerInstanceId: acpInstanceId,
2417
+ providerSessionId: null,
2418
+ nativeThreadRef: { driver: acpDriver, nativeId: state.nativeId, strength: "strong" },
2419
+ nativeConversationHeadRef: null,
2420
+ status: "idle",
2421
+ firstRunOrdinal: null,
2422
+ lastRunOrdinal: null,
2423
+ handoffIds: [],
2424
+ forkedFrom: null,
2425
+ createdAt: now,
2426
+ updatedAt: now,
2427
+ pendingBackgroundTasks: [],
2428
+ },
2429
+ });
2430
+ }
2431
if (state.ran) {
2432
events.push({
2433
id: EventId.make(`${threadId}:run`),
@@ -2432,10 +2460,17 @@ const seedAcpThread = Effect.fn(function* (
2460
it.effect("ACP session RPCs list a thread missing its history and repair it on import", () =>
2461
Effect.gen(function* () {
2462
const projections = yield* ProjectionStore.ProjectionStoreV2;
2463
+ const sql = yield* SqlClient.SqlClient;
2464
const pending = yield* seedAcpThread("pending", { imported: false, ran: false });
2465
const complete = yield* seedAcpThread("complete", { imported: true, ran: false });
2466
const ran = yield* seedAcpThread("ran", { imported: false, ran: true });
2467
+ const owned = yield* seedAcpThread("existing-t3-thread", {
2468
+ imported: false,
2469
+ ran: true,
2470
+ nativeId: "owned-session",
2471
+ });
2472
const repaired: Array<ThreadId> = [];
2473
+ const deleted: Array<string> = [];
2474
const session = (sessionId: string) => ({
2475
sessionId,
2476
cwd: "/workspace/acp",
@@ -2446,16 +2481,22 @@ it.effect("ACP session RPCs list a thread missing its history and repair it on i
2481
});
2482
const instance = {
2483
driverKind: acpDriver,
2449
- snapshot: { getSnapshot: Effect.succeed({ nativeSessions: { canLoad: true } }) },
2484
+ snapshot: {
2485
+ getSnapshot: Effect.succeed({ nativeSessions: { canLoad: true, canDelete: true } }),
2486
+ },
2487
acpSessionManagement: {
2488
listSessions: () =>
2489
Effect.succeed({
2453
- sessions: ["new", "pending", "complete", "ran"].map(session),
2490
+ sessions: ["new", "pending", "complete", "ran", "owned-session"].map(session),
2491
nextCursor: null,
2492
canLoad: true,
2493
canResume: true,
2494
canDelete: true,
2495
}),
2496
+ deleteSession: ({ sessionId }: { readonly sessionId: string }) =>
2497
+ Effect.sync(() => {
2498
+ deleted.push(sessionId);
2499
+ }),
2500
},
2501
} as unknown as ProviderInstance;
2502
const dependencies = Layer.mergeAll(
@@ -2505,6 +2546,7 @@ it.effect("ACP session RPCs list a thread missing its history and repair it on i
2546
importNativeHistory: ({ threadId }) =>
2547
Effect.sync(() => repaired.push(threadId)).pipe(Effect.as(1)),
2548
}),
2549
+ Layer.succeed(SqlClient.SqlClient, sql),
2550
);
2551
const services = AcpRegistrySessions.layer.pipe(Layer.provideMerge(dependencies));
2552
const client = yield* RpcTest.makeClient(acpSessionRpcs).pipe(
@@ -2544,6 +2586,7 @@ it.effect("ACP session RPCs list a thread missing its history and repair it on i
2586
["pending", pending, true],
2587
["complete", complete, undefined],
2588
["ran", ran, undefined],
2589
+ ["owned-session", owned, undefined],
2590
],
2591
);
2592
@@ -2554,6 +2597,20 @@ it.effect("ACP session RPCs list a thread missing its history and repair it on i
2597
});
2598
assert.deepEqual(imported, { threadId: pending, imported: true });
2599
assert.deepEqual(repaired, [pending]);
2600
+ const reopened = yield* client[WS_METHODS.serverImportAcpRegistrySession]({
2601
+ instanceId: acpInstanceId,
2602
+ projectId: acpProjectId,
2603
+ sessionId: "owned-session",
2604
+ });
2605
+ assert.deepEqual(reopened, { threadId: owned, imported: false });
2606
+ assert.deepEqual(repaired, [pending]);
2607
+ const deletion = yield* client[WS_METHODS.serverDeleteAcpRegistrySession]({
2608
+ instanceId: acpInstanceId,
2609
+ projectId: acpProjectId,
2610
+ sessionId: "owned-session",
2611
+ }).pipe(Effect.exit);
2612
+ assert.isTrue(Exit.isFailure(deletion));
2613
+ assert.deepEqual(deleted, []);
2614
}).pipe(
2615
Effect.provide(
2616
Layer.mergeAll(
apps/server/src/ws.ts
+33
-25
@@ -1611,31 +1611,36 @@ export const layerWsRpc = (
1611
const sessions = yield* Effect.forEach(
1612
listed.sessions,
1613
(session) => {
1614
- const threadId = AcpRegistrySessions.importedThreadId({
1614
+ const idInput = {
1615
driver: instance.driverKind,
1616
instanceId: input.instanceId,
1617
sessionId: session.sessionId,
1618
- });
1619
- return threadManagement.getThreadShell(threadId).pipe(
1620
- Effect.map((thread) => ({
1621
- ...session,
1622
- importedThreadId: thread === null ? null : threadId,
1623
- ...(thread !== null &&
1624
- AgentSessionImporter.isNativeHistoryPending({
1625
- historyOrigin: thread.historyOrigin,
1626
- hasRun: thread.latestRunId !== null,
1627
- })
1628
- ? { nativeHistoryPending: true }
1629
- : {}),
1630
- })),
1631
- Effect.mapError(
1632
- (cause) =>
1633
- new AcpRegistryOperationError({
1634
- reason: "session_import_failed",
1635
- message: "Could not inspect existing imported ACP sessions.",
1636
- cause,
1637
- }),
1638
- ),
1618
+ };
1619
+ return acpRegistrySessions.findOwner(idInput).pipe(
1620
+ Effect.flatMap((owner) => {
1621
+ const threadId = owner ?? AcpRegistrySessions.importedThreadId(idInput);
1622
+ return threadManagement.getThreadShell(threadId).pipe(
1623
+ Effect.map((thread) => ({
1624
+ ...session,
1625
+ importedThreadId: thread === null ? null : threadId,
1626
+ ...(thread !== null &&
1627
+ AgentSessionImporter.isNativeHistoryPending({
1628
+ historyOrigin: thread.historyOrigin,
1629
+ hasRun: thread.latestRunId !== null,
1630
+ })
1631
+ ? { nativeHistoryPending: true }
1632
+ : {}),
1633
+ })),
1634
+ Effect.mapError(
1635
+ (cause) =>
1636
+ new AcpRegistryOperationError({
1637
+ reason: "session_import_failed",
1638
+ message: "Could not inspect existing imported ACP sessions.",
1639
+ cause,
1640
+ }),
1641
+ ),
1642
+ );
1643
+ }),
1644
);
1645
},
1646
{ concurrency: 16 },
@@ -1659,11 +1664,14 @@ export const layerWsRpc = (
1664
message: "The ACP agent does not advertise session deletion.",
1665
});
1666
}
1662
- const threadId = AcpRegistrySessions.importedThreadId({
1667
+ const idInput = {
1668
driver: instance.driverKind,
1669
instanceId: input.instanceId,
1670
sessionId: input.sessionId,
1666
- });
1671
+ };
1672
+ const threadId =
1673
+ (yield* acpRegistrySessions.findOwner(idInput)) ??
1674
+ AcpRegistrySessions.importedThreadId(idInput);
1675
const importedThread = yield* threadManagement.getThreadShell(threadId).pipe(
1676
Effect.mapError(
1677
(cause) =>
@@ -3642,8 +3650,8 @@ export const layer = Layer.unwrap(
3650
Layer.provideMerge(RpcSerialization.layerJson),
3651
// Handler-context reporters see only request defects, not server-wide defects.
3652
Layer.provide(DefectReporter.layer),
3645
- Layer.provide(Layer.succeed(SqlClient.SqlClient, sql)),
3653
Layer.provide(AcpRegistrySessions.layer),
3654
+ Layer.provide(Layer.succeed(SqlClient.SqlClient, sql)),
3655
Layer.provide(ProviderMaintenanceRunner.layer),
3656
Layer.provide(Layer.succeed(NpmRelease.NpmRelease, npmRelease)),
3657
Layer.provide(Layer.succeed(ServerSelfUpdate.ServerSelfUpdate, serverSelfUpdate)),