fix(coding-agent): handle stale extension contexts

fixes #3606
This commit is contained in:
Mario Zechner
2026-04-23 22:06:55 +02:00
parent 0cb8ff3843
commit f0cf8a59d2
9 changed files with 77 additions and 17 deletions

View File

@@ -66,6 +66,7 @@ function extractUserMessageText(content: string | Array<{ type: string; text?: s
*/
export class AgentSessionRuntime {
private rebindSession?: (session: AgentSession) => Promise<void>;
private beforeSessionInvalidate?: () => void;
constructor(
private _session: AgentSession,
@@ -99,6 +100,18 @@ export class AgentSessionRuntime {
this.rebindSession = rebindSession;
}
/**
* Set a synchronous callback that runs after `session_shutdown` handlers finish
* but before the current session is invalidated.
*
* This is for host-owned UI teardown that must not yield to the event loop,
* such as detaching extension-provided TUI components before the old extension
* context becomes stale.
*/
setBeforeSessionInvalidate(beforeSessionInvalidate?: () => void): void {
this.beforeSessionInvalidate = beforeSessionInvalidate;
}
private async emitBeforeSwitch(
reason: "new" | "resume",
targetSessionFile?: string,
@@ -139,6 +152,7 @@ export class AgentSessionRuntime {
reason,
targetSessionFile,
});
this.beforeSessionInvalidate?.();
this.session.dispose();
}
@@ -354,6 +368,7 @@ export class AgentSessionRuntime {
type: "session_shutdown",
reason: "quit",
});
this.beforeSessionInvalidate?.();
this.session.dispose();
}
}