xonecas.io
64875
Mc

mini-coder / commit 9d4e0e0

fix(agent): gracefully handle SIGINT during LLM turn

- Swap process.once for process.on to ensure process.listenerCount evaluates to >1 correctly.
- Add manual removal of the SIGINT listener to avoid duplicate executions.
- Wrap execution in try...finally block to guarantee cleanup and prevent leaking listeners.

Sean2026-02-23 9d4e0e022fcc97a36b48c4b8c8e1f224701e0cf4 parent 304aa97

1 file changed

src/agent/agent.ts+39−31
@@ -452,40 +452,48 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
452452 const systemPrompt = buildSystemPrompt(cwd);
453453
454454 const abortController = new AbortController();
455- const onSigInt = () => abortController.abort();
456- process.once("SIGINT", onSigInt);
457-
458- // Always push so the stack stays aligned 1:1 with turns. A null entry means
459- // the tree was clean at snapshot time — /undo will roll back conversation
460- // history but skip file restoration for that turn.
461- snapshotStack.push(snapped ? thisTurn : null);
462-
463- spinner.start("thinking");
464-
465- const events = runTurn({
466- model: llm,
467- messages: coreHistory,
468- tools: planMode ? [...buildReadOnlyToolSet({ cwd }), ...mcpTools] : tools,
469- systemPrompt,
470- signal: abortController.signal,
471- });
455+ const onSigInt = () => {
456+ abortController.abort();
457+ process.removeListener("SIGINT", onSigInt);
458+ };
459+ process.on("SIGINT", onSigInt);
472460
473- const { inputTokens, outputTokens, contextTokens, newMessages } =
474- await renderTurn(events, spinner);
475- process.removeListener("SIGINT", onSigInt);
461+ try {
462+ // Always push so the stack stays aligned 1:1 with turns. A null entry means
463+ // the tree was clean at snapshot time — /undo will roll back conversation
464+ // history but skip file restoration for that turn.
465+ snapshotStack.push(snapped ? thisTurn : null);
466+
467+ spinner.start("thinking");
468+
469+ const events = runTurn({
470+ model: llm,
471+ messages: coreHistory,
472+ tools: planMode
473+ ? [...buildReadOnlyToolSet({ cwd }), ...mcpTools]
474+ : tools,
475+ systemPrompt,
476+ signal: abortController.signal,
477+ });
478+
479+ const { inputTokens, outputTokens, contextTokens, newMessages } =
480+ await renderTurn(events, spinner);
481+
482+ // newMessages are raw ModelMessage objects — push directly into coreHistory
483+ // so subsequent turns have the correct input/output fields.
484+ if (newMessages.length > 0) {
485+ coreHistory.push(...newMessages);
486+ session.messages.push(...newMessages);
487+ saveMessages(session.id, newMessages, thisTurn);
488+ }
476489
477- // newMessages are raw ModelMessage objects — push directly into coreHistory
478- // so subsequent turns have the correct input/output fields.
479- if (newMessages.length > 0) {
480- coreHistory.push(...newMessages);
481- session.messages.push(...newMessages);
482- saveMessages(session.id, newMessages, thisTurn);
490+ totalIn += inputTokens;
491+ totalOut += outputTokens;
492+ lastContextTokens = contextTokens;
493+ touchActiveSession(session);
494+ } finally {
495+ process.removeListener("SIGINT", onSigInt);
483496 }
484-
485- totalIn += inputTokens;
486- totalOut += outputTokens;
487- lastContextTokens = contextTokens;
488- touchActiveSession(session);
489497 }
490498
491499 // ── Render status bar (called before each prompt) ──────────────────────────