Repository navigation
fix(server): Claude threads resume the session /clear started #35054
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
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 |
|---|---|---|
|
|
@@ -5846,6 +5846,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| ); | ||
| const absorbedWakes = yield* Ref.make(new Set<string>()); | ||
| const heldProposedPlansByToolUseId = new Map<string, string>(); | ||
| // /clear moves a live process to a new native session; persist that one so a reopen resumes it. | ||
| const clearedNativeSessions = new Map<string, string>(); | ||
| const runtimeContext = yield* Effect.context<never>(); | ||
| const runPromise = Effect.runPromiseWith(runtimeContext); | ||
|
|
||
|
|
@@ -5867,6 +5869,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| }, | ||
| }; | ||
| } | ||
| const nativeSessionId = clearedNativeSessions.get( | ||
| event.providerThread.nativeThreadRef?.nativeId ?? "", | ||
| ); | ||
| if (nativeSessionId !== undefined) { | ||
| event = { | ||
| ...event, | ||
| providerThread: { | ||
| ...event.providerThread, | ||
| nativeThreadRef: { | ||
| driver: CLAUDE_PROVIDER, | ||
| nativeId: nativeSessionId, | ||
| strength: "strong", | ||
| }, | ||
| nativeConversationHeadRef: null, | ||
|
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. Preserve the native session associated with historical cursors. When Clearing the thread head does not correct these historical references. Preserve enough session identity to resolve each historical cursor against its original session. Otherwise, rollback and fork cannot recover the requested pre-clear conversation.
|
||
| }, | ||
| }; | ||
| } | ||
| } | ||
| return Queue.offer(events, event).pipe(Effect.asVoid); | ||
| }; | ||
|
|
@@ -11999,6 +12018,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| path, | ||
| }); | ||
| const closedExistingNativeThreadId = existing !== null ? existing.nativeThreadId : null; | ||
| clearedNativeSessions.delete(nativeThreadId); | ||
| if (existing !== null) { | ||
| yield* existing.query.close.pipe(Effect.ignore); | ||
| if (existing.nativeThreadId !== nativeThreadId) { | ||
|
|
@@ -12159,6 +12179,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| context.capabilities = new Set(message.capabilities ?? []); | ||
| context.claudeCodeVersion = message.claude_code_version; | ||
| } | ||
| if ( | ||
| message.type === "system" && | ||
| message.subtype === "init" && | ||
| message.session_id.length > 0 && | ||
| message.session_id !== context.nativeThreadId | ||
| ) { | ||
| clearedNativeSessions.set(context.nativeThreadId, message.session_id); | ||
| } | ||
| if ( | ||
| message.type === "system" && | ||
| (message.subtype === "init" || message.subtype === "status") && | ||
|
|
@@ -12903,7 +12931,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
|
|
||
| const closeLiveQueryForNativeThread = Effect.fnUntraced(function* (nativeThreadId: string) { | ||
| const existing = yield* Ref.get(queryContext); | ||
| if (existing === null || existing.nativeThreadId !== nativeThreadId) { | ||
| if ( | ||
| existing === null || | ||
| (existing.nativeThreadId !== nativeThreadId && | ||
| clearedNativeSessions.get(existing.nativeThreadId) !== nativeThreadId) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -12925,7 +12957,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | |
| ); | ||
| if (ownedLiveQuery) { | ||
| yield* finalizeActiveTurnAfterQueryExit(existing); | ||
| yield* endSubagentCallsOfClosedQuery(nativeThreadId, "interrupted"); | ||
| yield* endSubagentCallsOfClosedQuery(existing.nativeThreadId, "interrupted"); | ||
| } else { | ||
| yield* endSubagentsOfReplacedQuery(existing); | ||
| } | ||
|
|
||
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.
Resolve the replacement session ID in background-task lookups.
After session A changes to B, this update exposes B to callers. However, roster ingestion still stores tasks under
liveQuery.nativeThreadId, which remains A.Consequently,
awaitBackgroundTasksat Line 13287 looks under B and returnsmatched: []for a running task.hasPendingBackgroundWorkForThreadat Line 13065 also reports no roster. These operations remain incorrect until the process is replaced.Resolve B to the live process's registry key in these operations, or migrate the registries when adopting B. Extend the regression test with a running background task and an await request using the updated provider thread.