Repository navigation
fix(server): a thread follows Claude's EnterWorktree and ExitWorktree #35471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1967,6 +1967,7 @@ export function makeClaudeQueryOptions(input: { | |
| readonly supportedDialogKinds?: ClaudeQueryOptions["supportedDialogKinds"]; | ||
| readonly allowDangerouslySkipPermissions?: boolean; | ||
| readonly appendSystemPromptFileText?: string; | ||
| readonly hooks?: ClaudeQueryOptions["hooks"]; | ||
| // Flag settings, because a user's settings.json env beats the process env in Claude Code. | ||
| readonly toolSearchOverride?: "true"; | ||
| }): ClaudeAgentSdkQueryOptions { | ||
|
|
@@ -2104,9 +2105,16 @@ export function makeClaudeQueryOptions(input: { | |
| ...launchAppendSystemPrompt, | ||
| ].join("\n\n"), | ||
| }, | ||
| ...(deviceInstructions | ||
| ? { hooks: { SubagentStart: [{ hooks: [subagentDeviceInstructions] }] } } | ||
| : {}), | ||
| ...(input.hooks === undefined && !deviceInstructions | ||
| ? {} | ||
| : { | ||
| hooks: { | ||
| ...input.hooks, | ||
| ...(deviceInstructions | ||
| ? { SubagentStart: [{ hooks: [subagentDeviceInstructions] }] } | ||
| : {}), | ||
| }, | ||
| }), | ||
| ...(Object.keys(extraArgs).length === 0 ? {} : { extraArgs }), | ||
| }; | ||
| const additionalDirectories = [ | ||
|
|
@@ -5890,6 +5898,38 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| return Queue.offer(events, event).pipe(Effect.asVoid); | ||
| }; | ||
|
|
||
| // EnterWorktree/ExitWorktree move the live process without a T3 handoff. | ||
| let liveSession = session; | ||
| const moveSession = Effect.fn("ClaudeAdapterV2.moveSession")(function* ( | ||
| cwd: string, | ||
| cwdChangeSource: string | undefined, | ||
| ) { | ||
| if (cwd === liveSession.cwd && cwdChangeSource === liveSession.cwdChangeSource) return; | ||
| const { cwdChangeSource: _previousSource, ...rest } = liveSession; | ||
| liveSession = { | ||
| ...rest, | ||
| cwd, | ||
| ...(cwdChangeSource === undefined ? {} : { cwdChangeSource }), | ||
| updatedAt: yield* DateTime.now, | ||
| }; | ||
| yield* emitProviderEvent({ | ||
| type: "provider_session.updated", | ||
| driver: CLAUDE_PROVIDER, | ||
| providerSession: liveSession, | ||
| }); | ||
| }); | ||
| const onWorktreeTool: HookCallback = (hookInput) => | ||
| runPromise( | ||
| Effect.gen(function* () { | ||
| // Subagent worktrees do not move the root session. | ||
| if (hookInput.agent_id !== undefined || hookInput.cwd.length === 0) return {}; | ||
| const turn = yield* Ref.get(activeTurn); | ||
| const source = turn?.input.runtimePolicy.cwd ?? undefined; | ||
| if (source !== undefined) yield* moveSession(hookInput.cwd, source); | ||
| return {}; | ||
| }), | ||
| ); | ||
|
|
||
| const persistAttachmentBudget = Effect.fnUntraced(function* (nativeThreadId: string) { | ||
| const providerThread = (yield* Ref.get(lastProviderThreadByNativeThread)).get( | ||
| nativeThreadId, | ||
|
|
@@ -12083,6 +12123,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| supportedDialogKinds: ["resume_return"], | ||
| ...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }), | ||
| ...(toolSearchOverride === undefined ? {} : { toolSearchOverride }), | ||
| hooks: { | ||
| PostToolUse: [{ matcher: "^(EnterWorktree|ExitWorktree)$", hooks: [onWorktreeTool] }], | ||
| }, | ||
|
Comment on lines
+12126
to
+12128
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add adapter-level coverage for the worktree move hook. No test under Add a case that invokes the installed
|
||
| }); | ||
| const resolvedTurnCwd = yield* fileSystem | ||
| .realPath(turnInput.runtimePolicy.cwd ?? ".") | ||
|
|
@@ -12109,6 +12152,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| : Effect.void, | ||
| ), | ||
| ); | ||
| // A new process starts at its requested cwd, not where the previous one moved. | ||
| yield* moveSession(queryOptions.cwd ?? process.cwd(), undefined); | ||
| // Mark only successful opens; retries must not resume sessions that failed to create. | ||
| yield* Ref.update(openedNativeThreads, (current) => { | ||
| if (current.has(nativeThreadId)) { | ||
|
|
@@ -13012,7 +13057,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| instanceId: adapterOptions.instanceId, | ||
| driver: CLAUDE_PROVIDER, | ||
| providerSessionId: input.providerSessionId, | ||
| providerSession: session, | ||
| get providerSession() { | ||
| return liveSession; | ||
| }, | ||
| getModelContextWindow: (selection) => claudeContextWindow(selection) ?? undefined, | ||
| events: Stream.fromEffectRepeat(Queue.take(events)), | ||
| backgroundTasksEndWithProcess: true, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve caller
SubagentStarthooks.If
deviceInstructionsis true andinput.hooks.SubagentStartis set, Line 2114 replaces every caller matcher. Those callbacks never run. Append the device matcher to the caller’s list instead. (raw.githubusercontent.com)Proposed change
Committable suggestion