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, 13:30 UTC
fix(claude): rename the native session when the thread title changes (#34775)
Co-authored-by: kumar-karan <kumar-karan@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Karan Kumar committed
Nov 15, 2026 at 00:04 UTC
f5c2ceb4d16404d33a4eb6b76359dd2ca1475df1
4 files changed
+403
-22
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+259
-1
@@ -213,13 +213,14 @@ function makeClaudeTestAppThread(input: {
213
readonly threadId: ThreadId;
214
readonly providerThread: OrchestrationV2ProviderThread;
215
readonly now: DateTime.Utc;
216
+ readonly title?: string;
217
}): OrchestrationV2AppThread {
218
return {
219
createdBy: "user",
220
creationSource: "web",
221
id: input.threadId,
222
projectId: ProjectId.make(`project-${input.threadId}`),
222
- title: "Claude attachment test",
223
+ title: input.title ?? "Claude attachment test",
224
providerInstanceId: ProviderInstanceId.make(ClaudeAdapterV2.CLAUDE_PROVIDER),
225
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
226
runtimeMode: "full-access",
@@ -252,6 +253,7 @@ function makeClaudeTestTurnInput(input: {
253
readonly attachments: ProviderAdapter.ProviderAdapterV2TurnInput["message"]["attachments"];
254
readonly providerTurnOrdinal?: number;
255
readonly nativeThreadHasTurns?: boolean;
256
+ readonly title?: string;
257
readonly messageCreatedBy?: ProviderAdapter.ProviderAdapterV2TurnInput["message"]["createdBy"];
258
readonly messageCreationSource?: ProviderAdapter.ProviderAdapterV2TurnInput["message"]["creationSource"];
259
readonly modelSelection?: ModelSelection;
@@ -1078,6 +1080,7 @@ describe("ClaudeAdapterV2 Auto-accept edits", () => {
1080
setModel: () => Effect.void,
1081
applyFlagSettings: () => Effect.void,
1082
setPermissionMode: () => Effect.void,
1083
+ renameSession: () => Effect.void,
1084
backgroundTasks: () => Effect.succeed(false),
1085
mcpServerStatus: Effect.succeed([]),
1086
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -1200,6 +1203,7 @@ describe("ClaudeAdapterV2 append-system-prompt-file launch argument", () => {
1203
setModel: () => Effect.void,
1204
applyFlagSettings: () => Effect.void,
1205
setPermissionMode: () => Effect.void,
1206
+ renameSession: () => Effect.void,
1207
backgroundTasks: () => Effect.succeed(false),
1208
mcpServerStatus: Effect.succeed([]),
1209
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -1458,6 +1462,7 @@ describe("ClaudeAdapterV2 provider installation gate", () => {
1462
setModel: () => Effect.void,
1463
applyFlagSettings: () => Effect.void,
1464
setPermissionMode: () => Effect.void,
1465
+ renameSession: () => Effect.void,
1466
backgroundTasks: () => Effect.succeed(false),
1467
mcpServerStatus: Effect.succeed([]),
1468
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -1645,6 +1650,7 @@ const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(functio
1650
setModel: () => Effect.void,
1651
applyFlagSettings: () => Effect.void,
1652
setPermissionMode: () => Effect.void,
1653
+ renameSession: () => Effect.void,
1654
backgroundTasks: () => Effect.succeed(false),
1655
mcpServerStatus: Effect.succeed([]),
1656
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -1838,6 +1844,7 @@ describe("ClaudeAdapterV2 resume compaction", () => {
1844
setModel: () => Effect.void,
1845
applyFlagSettings: () => Effect.void,
1846
setPermissionMode: () => Effect.void,
1847
+ renameSession: () => Effect.void,
1848
backgroundTasks: () => Effect.succeed(false),
1849
mcpServerStatus: Effect.succeed([]),
1850
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -2222,6 +2229,7 @@ describe("ClaudeAdapterV2 attachments", () => {
2229
setModel: () => Effect.void,
2230
applyFlagSettings: () => Effect.void,
2231
setPermissionMode: () => Effect.void,
2232
+ renameSession: () => Effect.void,
2233
backgroundTasks: () => Effect.succeed(false),
2234
mcpServerStatus: Effect.succeed([]),
2235
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -2763,6 +2771,7 @@ describe("ClaudeAdapterV2 native fork", () => {
2771
setModel: () => Effect.void,
2772
applyFlagSettings: () => Effect.void,
2773
setPermissionMode: () => Effect.void,
2774
+ renameSession: () => Effect.void,
2775
backgroundTasks: () => Effect.succeed(false),
2776
mcpServerStatus: Effect.succeed([]),
2777
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -3111,6 +3120,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
3120
setModel: () => Effect.void,
3121
applyFlagSettings: () => Effect.void,
3122
setPermissionMode: () => Effect.void,
3123
+ renameSession: () => Effect.void,
3124
backgroundTasks: () => Effect.succeed(false),
3125
mcpServerStatus: Effect.succeed([]),
3126
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -3443,6 +3453,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3453
readonly close?: (sdkMessages: Queue.Queue<SDKMessage>) => Effect.Effect<void>;
3454
readonly interrupt?: Effect.Effect<void, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError>;
3455
readonly setPermissionMode?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["setPermissionMode"];
3456
+ readonly renameSession?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["renameSession"];
3457
readonly applyFlagSettings?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["applyFlagSettings"];
3458
readonly stopTask?: (
3459
taskId: string,
@@ -3490,6 +3501,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3501
const changes = yield* makeChangeSignal;
3502
const offeredMessages: Array<SDKUserMessage> = [];
3503
const permissionModeChanges: Array<string> = [];
3504
+ const renameCalls: Array<string> = [];
3505
const liveSelectionChanges: Array<unknown> = [];
3506
const continuationRequests: Array<ProviderContinuationRequests.ProviderContinuationRequest> =
3507
[];
@@ -3583,6 +3595,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3595
}).pipe(
3596
Effect.andThen(() => options?.setPermissionMode?.(mode) ?? Effect.void),
3597
),
3598
+ renameSession: (title) =>
3599
+ Effect.sync(() => {
3600
+ renameCalls.push(title);
3601
+ }).pipe(Effect.andThen(() => options?.renameSession?.(title) ?? Effect.void)),
3602
backgroundTasks: options?.backgroundTasks ?? (() => Effect.succeed(false)),
3603
mcpServerStatus: Effect.succeed([]),
3604
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -3617,6 +3633,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3633
Effect.sync(() => {
3634
permissionModeChanges.push(mode);
3635
}).pipe(Effect.andThen(() => options?.setPermissionMode?.(mode) ?? Effect.void)),
3636
+ renameSession: (title) =>
3637
+ Effect.sync(() => {
3638
+ renameCalls.push(title);
3639
+ }).pipe(Effect.andThen(() => options?.renameSession?.(title) ?? Effect.void)),
3640
backgroundTasks: options?.backgroundTasks ?? (() => Effect.succeed(false)),
3641
mcpServerStatus: options?.mcp?.mcpServerStatus ?? Effect.succeed([]),
3642
readMcpResource:
@@ -3689,6 +3709,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3709
event.type === "turn.terminal",
3710
);
3711
return {
3712
+ adapter,
3713
attachmentsDir,
3714
runtime,
3715
providerThread,
@@ -3703,6 +3724,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3724
return uuid;
3725
},
3726
permissionModeChanges,
3727
+ renameCalls,
3728
liveSelectionChanges,
3729
continuationRequests,
3730
continuationReceipts,
@@ -3724,6 +3746,232 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3746
});
3747
const makeWakeHarness = makeWakeHarnessWithOptions();
3748
3749
+ describe("ClaudeAdapterV2 session title sync", () => {
3750
+ type TitleSyncHarness = Effect.Success<ReturnType<typeof makeWakeHarnessWithOptions>>;
3751
+ const startTurn = Effect.fnUntraced(function* (
3752
+ harness: TitleSyncHarness,
3753
+ title: string,
3754
+ ordinal: number,
3755
+ nativeThreadHasTurns?: boolean,
3756
+ runtimeOverride?: TitleSyncHarness["runtime"],
3757
+ providerThreadOverride?: OrchestrationV2ProviderThread,
3758
+ ) {
3759
+ const runtime = runtimeOverride ?? harness.runtime;
3760
+ const providerThread = providerThreadOverride ?? harness.providerThread;
3761
+ yield* runtime.startTurn(
3762
+ makeClaudeTestTurnInput({
3763
+ threadId: harness.threadId,
3764
+ providerThread,
3765
+ now: yield* DateTime.now,
3766
+ attemptId: RunAttemptId.make(`attempt-title-sync-${ordinal}`),
3767
+ providerTurnOrdinal: ordinal,
3768
+ title,
3769
+ text: "What is the sentinel?",
3770
+ attachments: [],
3771
+ ...(nativeThreadHasTurns === undefined ? {} : { nativeThreadHasTurns }),
3772
+ }),
3773
+ );
3774
+ });
3775
+ const finishTurn = Effect.fnUntraced(function* (harness: TitleSyncHarness, ordinal: number) {
3776
+ yield* harness.offerAndWait(
3777
+ makeResultFrame({ uuid: `title-result-${ordinal}`, result: "Done." }),
3778
+ );
3779
+ yield* Queue.take(harness.terminalReceipts);
3780
+ });
3781
+
3782
+ it.effect("renames the native session only when the thread title changed", () =>
3783
+ Effect.gen(function* () {
3784
+ const harness = yield* makeWakeHarnessWithOptions();
3785
+ yield* startTurn(harness, "First title", 1);
3786
+ assert.deepEqual(harness.renameCalls, []);
3787
+ yield* finishTurn(harness, 1);
3788
+
3789
+ yield* startTurn(harness, "First title", 2);
3790
+ assert.deepEqual(harness.renameCalls, []);
3791
+ yield* finishTurn(harness, 2);
3792
+
3793
+ yield* startTurn(harness, "Renamed title", 3);
3794
+ assert.deepEqual(harness.renameCalls, ["Renamed title"]);
3795
+ yield* finishTurn(harness, 3);
3796
+
3797
+ yield* startTurn(harness, "Renamed title", 4);
3798
+ assert.deepEqual(harness.renameCalls, ["Renamed title"]);
3799
+ yield* finishTurn(harness, 4);
3800
+ }).pipe(
3801
+ Effect.provide(
3802
+ Layer.mergeAll(
3803
+ IdAllocator.layer,
3804
+ McpProviderSessions.layer,
3805
+ NodeServices.layer,
3806
+ NodeCrypto.layer,
3807
+ ),
3808
+ ),
3809
+ ),
3810
+ );
3811
+
3812
+ it.effect("renames a resumed session whose stored title is stale", () =>
3813
+ Effect.gen(function* () {
3814
+ const harness = yield* makeWakeHarnessWithOptions();
3815
+ yield* startTurn(harness, "First title", 1, true);
3816
+ assert.deepEqual(harness.renameCalls, ["First title"]);
3817
+ yield* finishTurn(harness, 1);
3818
+
3819
+ yield* startTurn(harness, "First title", 2, true);
3820
+ assert.deepEqual(harness.renameCalls, ["First title"]);
3821
+ yield* finishTurn(harness, 2);
3822
+ }).pipe(
3823
+ Effect.provide(
3824
+ Layer.mergeAll(
3825
+ IdAllocator.layer,
3826
+ McpProviderSessions.layer,
3827
+ NodeServices.layer,
3828
+ NodeCrypto.layer,
3829
+ ),
3830
+ ),
3831
+ ),
3832
+ );
3833
+
3834
+ it.effect(
3835
+ "a rename blocked past the deadline still offers the prompt and retries next turn",
3836
+ () =>
3837
+ Effect.gen(function* () {
3838
+ const entered = yield* Deferred.make<void>();
3839
+ let attempts = 0;
3840
+ const harness = yield* makeWakeHarnessWithOptions({
3841
+ renameSession: () =>
3842
+ ++attempts === 1
3843
+ ? Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never))
3844
+ : Effect.void,
3845
+ });
3846
+ const firstTurn = yield* Effect.forkScoped(startTurn(harness, "First title", 1, true));
3847
+ yield* Deferred.await(entered);
3848
+ yield* TestClock.adjust("10 seconds");
3849
+ yield* Fiber.join(firstTurn);
3850
+ assert.deepEqual(harness.renameCalls, ["First title"]);
3851
+ assert.equal(harness.offeredMessages.length, 1);
3852
+ assert.equal(attempts, 1);
3853
+
3854
+ yield* finishTurn(harness, 1);
3855
+ assert.deepEqual(harness.renameCalls, ["First title"]);
3856
+
3857
+ yield* startTurn(harness, "First title", 2, true);
3858
+ assert.deepEqual(harness.renameCalls, ["First title", "First title"]);
3859
+ assert.equal(attempts, 2);
3860
+ yield* finishTurn(harness, 2);
3861
+ }).pipe(
3862
+ Effect.provide(
3863
+ Layer.mergeAll(
3864
+ IdAllocator.layer,
3865
+ McpProviderSessions.layer,
3866
+ NodeServices.layer,
3867
+ NodeCrypto.layer,
3868
+ ),
3869
+ ),
3870
+ ),
3871
+ );
3872
+
3873
+ it.effect("renames when reverting to an earlier title after a failed or timed out update", () =>
3874
+ Effect.gen(function* () {
3875
+ const harness = yield* makeWakeHarnessWithOptions({
3876
+ renameSession: (title) =>
3877
+ title === "Title B"
3878
+ ? Effect.fail(
3879
+ new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
3880
+ method: "renameSession",
3881
+ cause: new Error("timed out or refused"),
3882
+ }),
3883
+ )
3884
+ : Effect.void,
3885
+ });
3886
+ yield* startTurn(harness, "Title A", 1, true);
3887
+ yield* finishTurn(harness, 1);
3888
+ assert.deepEqual(harness.renameCalls, ["Title A"]);
3889
+
3890
+ yield* startTurn(harness, "Title B", 2, true);
3891
+ yield* finishTurn(harness, 2);
3892
+ assert.deepEqual(harness.renameCalls, ["Title A", "Title B"]);
3893
+
3894
+ yield* startTurn(harness, "Title A", 3, true);
3895
+ yield* finishTurn(harness, 3);
3896
+ assert.deepEqual(harness.renameCalls, ["Title A", "Title B", "Title A"]);
3897
+
3898
+ yield* startTurn(harness, "Title A", 4, true);
3899
+ yield* finishTurn(harness, 4);
3900
+ assert.deepEqual(harness.renameCalls, ["Title A", "Title B", "Title A"]);
3901
+ }).pipe(
3902
+ Effect.provide(
3903
+ Layer.mergeAll(
3904
+ IdAllocator.layer,
3905
+ McpProviderSessions.layer,
3906
+ NodeServices.layer,
3907
+ NodeCrypto.layer,
3908
+ ),
3909
+ ),
3910
+ ),
3911
+ );
3912
+
3913
+ it.effect(
3914
+ "preserves the last-applied title across provider-session recreation so a native /rename survives",
3915
+ () =>
3916
+ Effect.gen(function* () {
3917
+ const harness = yield* makeWakeHarnessWithOptions({ freshQueueOnReopen: true });
3918
+ yield* startTurn(harness, "First title", 1);
3919
+ assert.deepEqual(harness.renameCalls, []);
3920
+ yield* finishTurn(harness, 1);
3921
+
3922
+ const lastEvent = harness.events.findLast(
3923
+ (
3924
+ e,
3925
+ ): e is Extract<
3926
+ ProviderAdapter.ProviderAdapterV2Event,
3927
+ { type: "provider_thread.updated" }
3928
+ > => e.type === "provider_thread.updated",
3929
+ );
3930
+ assert.equal(lastEvent?.providerThread.nativeMetadata?.title, "First title");
3931
+ const threadAfterTurn1 = lastEvent!.providerThread;
3932
+
3933
+ const runtime2 = yield* harness.adapter.openSession({
3934
+ threadId: harness.threadId,
3935
+ providerSessionId: ProviderSessionId.make("provider-session-claude-wake-2"),
3936
+ modelSelection: CLAUDE_TEST_MODEL_SELECTION,
3937
+ runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
3938
+ });
3939
+ yield* runtime2.events.pipe(
3940
+ Stream.runForEach((event) =>
3941
+ Effect.gen(function* () {
3942
+ harness.events.push(event);
3943
+ if (event.type === "turn.terminal") {
3944
+ yield* Queue.offer(harness.terminalReceipts, event);
3945
+ }
3946
+ }),
3947
+ ),
3948
+ Effect.forkScoped,
3949
+ );
3950
+ const resumedThread = yield* runtime2.resumeThread({
3951
+ providerThread: threadAfterTurn1,
3952
+ });
3953
+ assert.equal(resumedThread.nativeMetadata?.title, "First title");
3954
+
3955
+ yield* startTurn(harness, "First title", 2, true, runtime2, resumedThread);
3956
+ assert.deepEqual(harness.renameCalls, []);
3957
+ yield* finishTurn(harness, 2);
3958
+
3959
+ yield* startTurn(harness, "Second title", 3, true, runtime2, resumedThread);
3960
+ assert.deepEqual(harness.renameCalls, ["Second title"]);
3961
+ yield* finishTurn(harness, 3);
3962
+ }).pipe(
3963
+ Effect.provide(
3964
+ Layer.mergeAll(
3965
+ IdAllocator.layer,
3966
+ McpProviderSessions.layer,
3967
+ NodeServices.layer,
3968
+ NodeCrypto.layer,
3969
+ ),
3970
+ ),
3971
+ ),
3972
+ );
3973
+ });
3974
+
3975
it.effect.each([false, true])(
3976
"resumes a stopped query with empty SDK content and a fresh native process, buffered: %s",
3977
(buffered) =>
@@ -13313,6 +13561,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
13561
setModel: () => Effect.void,
13562
applyFlagSettings: () => Effect.void,
13563
setPermissionMode: () => Effect.void,
13564
+ renameSession: () => Effect.void,
13565
backgroundTasks: () => Effect.succeed(false),
13566
mcpServerStatus: Effect.succeed([]),
13567
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -13508,6 +13757,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
13757
setModel: () => Effect.void,
13758
applyFlagSettings: () => Effect.void,
13759
setPermissionMode: () => Effect.void,
13760
+ renameSession: () => Effect.void,
13761
backgroundTasks: () => Effect.succeed(false),
13762
mcpServerStatus: Effect.succeed([]),
13763
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -13638,6 +13888,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
13888
setModel: () => Effect.void,
13889
applyFlagSettings: () => Effect.void,
13890
setPermissionMode: () => Effect.void,
13891
+ renameSession: () => Effect.void,
13892
backgroundTasks: () => Effect.succeed(false),
13893
mcpServerStatus: Effect.succeed([]),
13894
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -16838,6 +17089,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
17089
setModel: () => Effect.void,
17090
applyFlagSettings: () => Effect.void,
17091
setPermissionMode: () => Effect.void,
17092
+ renameSession: () => Effect.void,
17093
backgroundTasks: () => Effect.succeed(false),
17094
stopTask: () => Effect.void,
17095
mcpServerStatus: Effect.die("unexpected MCP server status"),
@@ -20096,6 +20348,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
20348
setModel: () => Effect.void,
20349
applyFlagSettings: () => Effect.void,
20350
setPermissionMode: () => Effect.void,
20351
+ renameSession: () => Effect.void,
20352
backgroundTasks: () => Effect.succeed(false),
20353
mcpServerStatus: Effect.succeed([]),
20354
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -20273,6 +20526,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
20526
setModel: () => Effect.void,
20527
applyFlagSettings: () => Effect.void,
20528
setPermissionMode: () => Effect.void,
20529
+ renameSession: () => Effect.void,
20530
backgroundTasks: () => Effect.succeed(false),
20531
mcpServerStatus: Effect.succeed([]),
20532
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -20450,6 +20704,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
20704
setModel: () => Effect.void,
20705
applyFlagSettings: () => Effect.void,
20706
setPermissionMode: () => Effect.void,
20707
+ renameSession: () => Effect.void,
20708
backgroundTasks: () => Effect.succeed(false),
20709
mcpServerStatus: Effect.succeed([]),
20710
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -20648,6 +20903,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
20903
setModel: () => Effect.void,
20904
applyFlagSettings: () => Effect.void,
20905
setPermissionMode: () => Effect.void,
20906
+ renameSession: () => Effect.void,
20907
backgroundTasks: () => Effect.succeed(false),
20908
mcpServerStatus: Effect.succeed([]),
20909
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -20794,6 +21050,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21050
setModel: () => Effect.void,
21051
applyFlagSettings: () => Effect.void,
21052
setPermissionMode: () => Effect.void,
21053
+ renameSession: () => Effect.void,
21054
backgroundTasks: () => Effect.succeed(false),
21055
mcpServerStatus: Effect.succeed([]),
21056
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -20915,6 +21172,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21172
setModel: () => Effect.void,
21173
applyFlagSettings: () => Effect.void,
21174
setPermissionMode: () => Effect.void,
21175
+ renameSession: () => Effect.void,
21176
backgroundTasks: () => Effect.succeed(false),
21177
mcpServerStatus: Effect.succeed([]),
21178
readMcpResource: () => Effect.die("unexpected MCP resource read"),
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts
+2
@@ -838,6 +838,8 @@ function makeReplayQueryRunner(
838
mode,
839
});
840
}),
841
+ // Recorded sessions already hold their title, and no replay asserts rename frames.
842
+ renameSession: () => Effect.void,
843
stopTask: (taskId) =>
844
replayEffect(() => {
845
assertNextOutboundFrame({ type: "query.stop_task", taskId });
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+139
-21
@@ -667,6 +667,7 @@ export interface ClaudeAgentSdkQuerySession {
667
readonly setPermissionMode: (
668
mode: PermissionMode,
669
) => Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
670
+ readonly renameSession: (title: string) => Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
671
readonly stopTask: (taskId: string) => Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
672
readonly backgroundTasks: (
673
toolUseId: string,
@@ -861,7 +862,7 @@ function closeClaudeQuery(queryRuntime: ClaudeQuery) {
862
async function sendClaudeControlRequest(
863
queryRuntime: ClaudeQuery,
864
request: {
864
- readonly subtype: "mcp_call" | "list_permission_rules";
865
+ readonly subtype: "mcp_call" | "list_permission_rules" | "rename_session";
866
readonly [key: string]: unknown;
867
},
868
): Promise<unknown> {
@@ -1082,6 +1083,14 @@ export type ClaudeAgentSdkProtocolLogEvent =
1083
readonly mode: PermissionMode;
1084
};
1085
}
1086
+ | {
1087
+ readonly direction: "outgoing";
1088
+ readonly stage: "decoded";
1089
+ readonly payload: {
1090
+ readonly type: "query.rename_session";
1091
+ readonly title: string;
1092
+ };
1093
+ }
1094
| {
1095
readonly direction: "outgoing";
1096
readonly stage: "decoded";
@@ -1746,6 +1755,28 @@ export const layerQueryRunner: Layer.Layer<
1755
}),
1756
),
1757
),
1758
+ renameSession: (title) =>
1759
+ Effect.tryPromise({
1760
+ try: () =>
1761
+ sendClaudeControlRequest(queryRuntime, {
1762
+ subtype: "rename_session",
1763
+ title,
1764
+ source: "host",
1765
+ }),
1766
+ catch: (cause) => queryRunnerError(cause, "renameSession"),
1767
+ }).pipe(
1768
+ Effect.asVoid,
1769
+ Effect.tap(() =>
1770
+ logProtocolEvent({
1771
+ direction: "outgoing",
1772
+ stage: "decoded",
1773
+ payload: {
1774
+ type: "query.rename_session",
1775
+ title,
1776
+ },
1777
+ }),
1778
+ ),
1779
+ ),
1780
stopTask: (taskId) =>
1781
Effect.tryPromise({
1782
try: () => queryRuntime.stopTask(taskId),
@@ -2533,6 +2564,12 @@ function resultTextFromSdkMessage(
2564
};
2565
}
2566
2567
+function normalizeClaudeSessionTitle(title: string | null | undefined): string | null {
2568
+ if (title === null || title === undefined) return null;
2569
+ const trimmed = title.trim();
2570
+ return trimmed.length > 0 ? trimmed : null;
2571
+}
2572
+
2573
function makeProviderThread(input: {
2574
readonly idAllocator: IdAllocator.IdAllocatorV2["Service"];
2575
readonly providerInstanceId: ProviderInstanceId;
@@ -5782,6 +5819,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5819
new Map<string, OrchestrationV2ProviderThread>(),
5820
);
5821
const goalsByNativeThread = new Map<string, OrchestrationV2ProviderGoal | null>();
5822
+ // The title T3 last applied to each native session, so a turn renames it only when the
5823
+ // thread title changed. A native `/rename` T3 never applied survives later turns.
5824
+ const appliedSessionTitles = new Map<string, string>();
5825
const goalCheckedTurns = new Set<string>();
5826
// Background subagents can finish after root settlement; retain their registry for wake evidence and hydration.
5827
const sessionSubagentsByTaskId = yield* Ref.make(new Map<string, ActiveClaudeSubagent>());
@@ -6070,27 +6110,47 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
6110
const runtimeContext = yield* Effect.context<never>();
6111
const runPromise = Effect.runPromiseWith(runtimeContext);
6112
6073
- const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) => {
6074
- if (event.type === "provider_thread.updated") {
6075
- const budget = pdfBudgets.get(event.providerThread.nativeThreadRef?.nativeId ?? "");
6076
- if (budget !== undefined) {
6077
- event = {
6078
- ...event,
6079
- providerThread: {
6080
- ...event.providerThread,
6081
- nativeMetadata: {
6082
- ...event.providerThread.nativeMetadata,
6113
+ // Overlays state the adapter owns per native session onto persisted provider metadata.
6114
+ const withAdapterNativeMetadata = (
6115
+ providerThread: OrchestrationV2ProviderThread,
6116
+ nativeThreadId: string,
6117
+ ): OrchestrationV2ProviderThread => {
6118
+ const budget = pdfBudgets.get(nativeThreadId);
6119
+ const hasAppliedTitle = appliedSessionTitles.has(nativeThreadId);
6120
+ const recordedTitle = providerThread.nativeMetadata?.title;
6121
+ if (budget === undefined && !hasAppliedTitle && recordedTitle === undefined) {
6122
+ return providerThread;
6123
+ }
6124
+ return {
6125
+ ...providerThread,
6126
+ nativeMetadata: {
6127
+ ...providerThread.nativeMetadata,
6128
+ ...(budget === undefined
6129
+ ? {}
6130
+ : {
6131
claudeAttachmentBudget: {
6132
bytesUsed: budget.committed.bytes,
6133
pagesUsed: budget.committed.pages,
6134
},
6087
- },
6088
- },
6089
- };
6090
- }
6091
- const nativeSessionId = clearedNativeSessions.get(
6092
- event.providerThread.nativeThreadRef?.nativeId ?? "",
6093
- );
6135
+ }),
6136
+ // A cleared title makes the next turn after a restart retry a rename that failed.
6137
+ ...(hasAppliedTitle
6138
+ ? { title: normalizeClaudeSessionTitle(appliedSessionTitles.get(nativeThreadId)) }
6139
+ : recordedTitle === undefined
6140
+ ? {}
6141
+ : { title: null }),
6142
+ },
6143
+ };
6144
+ };
6145
+
6146
+ const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) => {
6147
+ if (event.type === "provider_thread.updated") {
6148
+ const nativeThreadId = event.providerThread.nativeThreadRef?.nativeId ?? "";
6149
+ event = {
6150
+ ...event,
6151
+ providerThread: withAdapterNativeMetadata(event.providerThread, nativeThreadId),
6152
+ };
6153
+ const nativeSessionId = clearedNativeSessions.get(nativeThreadId);
6154
if (nativeSessionId !== undefined) {
6155
event = {
6156
...event,
@@ -6270,8 +6330,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
6330
if (nativeThreadId === undefined || nativeThreadId === null) {
6331
return;
6332
}
6333
+ const enriched = withAdapterNativeMetadata(providerThread, nativeThreadId);
6334
yield* Ref.update(lastProviderThreadByNativeThread, (current) =>
6274
- new Map(current).set(nativeThreadId, providerThread),
6335
+ new Map(current).set(nativeThreadId, enriched),
6336
);
6337
});
6338
@@ -12587,6 +12648,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12648
updated.add(nativeThreadId);
12649
return updated;
12650
});
12651
+ // A new session's spawn applied this turn's title. A resume keeps the session's stored
12652
+ // title, so the last applied one stays authoritative until a turn compares again.
12653
+ if (!shouldResume) {
12654
+ appliedSessionTitles.set(nativeThreadId, turnInput.appThread.title);
12655
+ }
12656
yield* resetBackgroundTaskStateForNativeThreadProcess(nativeThreadId, {
12657
status: "active",
12658
});
@@ -12786,6 +12852,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12852
const used = turnInput.providerThread.nativeMetadata?.claudeAttachmentBudget;
12853
pdfBudgets.set(nativeThreadId, makeClaudeAttachmentBudget(used));
12854
}
12855
+ if (!appliedSessionTitles.has(nativeThreadId)) {
12856
+ const recordedTitle = turnInput.providerThread.nativeMetadata?.title;
12857
+ if (recordedTitle !== undefined && recordedTitle !== null) {
12858
+ appliedSessionTitles.set(nativeThreadId, recordedTitle);
12859
+ }
12860
+ }
12861
const [skills, queryPolicy] = yield* Effect.all(
12862
[
12863
promptUuid === null
@@ -12835,6 +12907,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12907
Effect.provideService(Path.Path, path),
12908
);
12909
const querySession = yield* openQuery(turnInput, nativeThreadId, queryPolicy);
12910
+ // `claude --resume` shows the native title. A failed or timed-out rename forgets the
12911
+ // applied title so the next turn, even after a restart, sends it again.
12912
+ if (appliedSessionTitles.get(nativeThreadId) !== turnInput.appThread.title) {
12913
+ const renamed = yield* querySession.query
12914
+ .renameSession(turnInput.appThread.title)
12915
+ .pipe(Effect.timeout("10 seconds"), Effect.isSuccess);
12916
+ if (renamed) {
12917
+ appliedSessionTitles.set(nativeThreadId, turnInput.appThread.title);
12918
+ } else {
12919
+ appliedSessionTitles.delete(nativeThreadId);
12920
+ }
12921
+ const remembered = (yield* Ref.get(lastProviderThreadByNativeThread)).get(
12922
+ nativeThreadId,
12923
+ );
12924
+ if (remembered !== undefined) {
12925
+ yield* rememberProviderThread(remembered);
12926
+ }
12927
+ }
12928
const autoCompactWindow = yield* resolveAutoCompactWindow(
12929
querySession.settingsCwd,
12930
turnInput.modelSelection,
@@ -13598,14 +13688,22 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
13688
function* (threadInput: ProviderAdapter.ProviderAdapterV2EnsureThreadInput) {
13689
const createdAt = yield* DateTime.now;
13690
const nativeThreadId = yield* queryRunner.allocateSessionId;
13601
- return makeProviderThread({
13691
+ if (threadInput.title !== undefined) {
13692
+ appliedSessionTitles.set(nativeThreadId, threadInput.title);
13693
+ }
13694
+ const providerThread = makeProviderThread({
13695
idAllocator,
13696
providerInstanceId: adapterOptions.instanceId,
13697
appThreadId: threadInput.threadId,
13698
providerSessionId: input.providerSessionId,
13699
nativeThreadId,
13700
+ ...(threadInput.title !== undefined
13701
+ ? { nativeMetadata: { title: normalizeClaudeSessionTitle(threadInput.title) } }
13702
+ : {}),
13703
now: createdAt,
13704
});
13705
+ yield* rememberProviderThread(providerThread);
13706
+ return providerThread;
13707
},
13708
(effect, threadInput) =>
13709
effect.pipe(
@@ -13622,6 +13720,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
13720
resumeThread: Effect.fn("ClaudeAdapterV2.resumeThread")(
13721
function* (threadInput: { readonly providerThread: OrchestrationV2ProviderThread }) {
13722
const nativeThreadId = threadInput.providerThread.nativeThreadRef?.nativeId;
13723
+ if (nativeThreadId !== undefined && nativeThreadId !== null) {
13724
+ const recordedTitle = threadInput.providerThread.nativeMetadata?.title;
13725
+ if (recordedTitle !== undefined && recordedTitle !== null) {
13726
+ appliedSessionTitles.set(nativeThreadId, recordedTitle);
13727
+ }
13728
+ }
13729
if (
13730
nativeThreadId !== undefined &&
13731
nativeThreadId !== null &&
@@ -13647,12 +13751,26 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
13751
}
13752
}
13753
const updatedAt = yield* DateTime.now;
13650
- return {
13754
+ const appliedTitle =
13755
+ nativeThreadId !== undefined && nativeThreadId !== null
13756
+ ? appliedSessionTitles.get(nativeThreadId)
13757
+ : undefined;
13758
+ const providerThread: OrchestrationV2ProviderThread = {
13759
...threadInput.providerThread,
13760
providerSessionId: input.providerSessionId,
13761
status: "idle" as const,
13762
+ ...(appliedTitle !== undefined
13763
+ ? {
13764
+ nativeMetadata: {
13765
+ ...threadInput.providerThread.nativeMetadata,
13766
+ title: normalizeClaudeSessionTitle(appliedTitle),
13767
+ },
13768
+ }
13769
+ : {}),
13770
updatedAt,
13771
};
13772
+ yield* rememberProviderThread(providerThread);
13773
+ return providerThread;
13774
},
13775
(effect, threadInput) =>
13776
effect.pipe(
apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts
+3
@@ -169,6 +169,7 @@ it.effect.each(deliveries)(
169
setModel: () => Effect.void,
170
applyFlagSettings: () => Effect.void,
171
setPermissionMode: () => Effect.void,
172
+ renameSession: () => Effect.void,
173
backgroundTasks: () => Effect.succeed(false),
174
mcpServerStatus: Effect.succeed([]),
175
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -518,6 +519,7 @@ it.effect("applies an effort change to the running Claude query without waiting
519
flagSettings.push(flags);
520
}),
521
setPermissionMode: () => Effect.void,
522
+ renameSession: () => Effect.void,
523
backgroundTasks: () => Effect.succeed(false),
524
mcpServerStatus: Effect.succeed([]),
525
readMcpResource: () => Effect.die("unexpected MCP resource read"),
@@ -712,6 +714,7 @@ it.effect("offers a continuation for a later wake after a queued continuation is
714
setModel: () => Effect.void,
715
applyFlagSettings: () => Effect.void,
716
setPermissionMode: () => Effect.void,
717
+ renameSession: () => Effect.void,
718
backgroundTasks: () => Effect.succeed(false),
719
mcpServerStatus: Effect.succeed([]),
720
readMcpResource: () => Effect.die("unexpected MCP resource read"),