WIP: Add branch summarization abort support with loader and escape handler

This commit is contained in:
Mario Zechner 2025-12-29 19:36:03 +01:00
parent 159e19a010
commit 9dac0a1423
3 changed files with 112 additions and 66 deletions

View file

@ -146,6 +146,9 @@ export class AgentSession {
private _compactionAbortController: AbortController | undefined = undefined;
private _autoCompactionAbortController: AbortController | undefined = undefined;
// Branch summarization state
private _branchSummaryAbortController: AbortController | undefined = undefined;
// Retry state
private _retryAbortController: AbortController | undefined = undefined;
private _retryAttempt = 0;
@ -998,6 +1001,13 @@ export class AgentSession {
this._autoCompactionAbortController?.abort();
}
/**
* Cancel in-progress branch summarization.
*/
abortBranchSummary(): void {
this._branchSummaryAbortController?.abort();
}
/**
* Check if compaction is needed and run it.
* Called after agent_end and before prompt submission.
@ -1572,7 +1582,7 @@ export class AgentSession {
async navigateTree(
targetId: string,
options: { summarize?: boolean; customInstructions?: string } = {},
): Promise<{ editorText?: string; cancelled: boolean }> {
): Promise<{ editorText?: string; cancelled: boolean; aborted?: boolean }> {
const oldLeafId = this.sessionManager.getLeafUuid();
// No-op if already at target
@ -1625,7 +1635,7 @@ export class AgentSession {
};
// Set up abort controller for summarization
const abortController = new AbortController();
this._branchSummaryAbortController = new AbortController();
let hookSummary: { summary: string; details?: unknown } | undefined;
let fromHook = false;
@ -1635,7 +1645,7 @@ export class AgentSession {
type: "session_before_tree",
preparation,
model: this.model!, // Checked above if summarize is true
signal: abortController.signal,
signal: this._branchSummaryAbortController.signal,
})) as SessionBeforeTreeResult | undefined;
if (result?.cancel) {
@ -1655,11 +1665,16 @@ export class AgentSession {
summaryText = await this._generateBranchSummary(
entriesToSummarize,
options.customInstructions,
abortController.signal,
this._branchSummaryAbortController.signal,
);
} catch {
// Summarization failed - cancel navigation
return { cancelled: true };
} catch (error) {
this._branchSummaryAbortController = undefined;
// Check if aborted
if (error instanceof Error && (error.name === "AbortError" || error.message === "aborted")) {
return { cancelled: true, aborted: true };
}
// Re-throw actual errors so UI can display them
throw error;
}
} else if (hookSummary) {
summaryText = hookSummary.summary;
@ -1718,6 +1733,7 @@ export class AgentSession {
// Emit to custom tools
await this._emitToolSessionEvent("tree", this.sessionFile);
this._branchSummaryAbortController = undefined;
return { editorText, cancelled: false };
}

View file

@ -1637,8 +1637,32 @@ export class InteractiveMode {
"Create a summary of the branch you're leaving?",
);
// Set up escape handler and loader if summarizing
let summaryLoader: Loader | undefined;
const originalOnEscape = this.editor.onEscape;
if (wantsSummary) {
this.editor.onEscape = () => {
this.session.abortBranchSummary();
};
this.chatContainer.addChild(new Spacer(1));
summaryLoader = new Loader(
this.ui,
(spinner) => theme.fg("accent", spinner),
(text) => theme.fg("muted", text),
"Summarizing branch... (esc to cancel)",
);
this.statusContainer.addChild(summaryLoader);
this.ui.requestRender();
}
try {
const result = await this.session.navigateTree(entryId, { summarize: wantsSummary });
if (result.aborted) {
this.showStatus("Branch summarization cancelled");
return;
}
if (result.cancelled) {
this.showStatus("Navigation cancelled");
return;
@ -1653,6 +1677,12 @@ export class InteractiveMode {
this.showStatus("Navigated to selected point");
} catch (error) {
this.showError(error instanceof Error ? error.message : String(error));
} finally {
if (summaryLoader) {
summaryLoader.stop();
this.statusContainer.clear();
}
this.editor.onEscape = originalOnEscape;
}
},
() => {