senpi icon indicating copy to clipboard operation
senpi copied to clipboard

handoff-hint re-arms its timer after session_shutdown and keeps senpi -p alive

Open hhgyu opened this issue 2 weeks ago • 0 comments

What happened?

senpi -p can stay alive for about 5 minutes after the run is done.

The builtin handoff-hint extension clears its prompt-cache timer in its session_shutdown handler, but it keeps handling agent_end / agent_settled afterwards. If a turn starts and settles while the remaining session_shutdown handlers are still awaited (for example an extension's late sendMessage(..., { triggerTurn: true })), agent_settled calls arm() again. arm() uses a plain setTimeout (not unref'd) for the cache TTL, and no later shutdown reaches it, so the process stays up until that timer fires.

Source on main (unchanged since v2026.11.12, still the same in v2026.11.15-4): packages/coding-agent/src/core/extensions/builtin/handoff-hint/index.ts, arm() and the agent_settled / session_shutdown handlers. Builtin shutdown handlers run before extension handlers (emitSessionShutdownEvent awaits them in load order), so handoff-hint has already disarmed when the late turn settles.

Related: #5753 (same lifecycle ordering: work starting after session_shutdown).

Steps to reproduce

  1. Put this in <agentDir>/extensions/late-wake.js:
export default function (pi) {
  pi.on("session_shutdown", async () => {
    pi.sendMessage({ customType: "late-wake", content: "background work finished", display: true }, { triggerTurn: true })
    await new Promise((resolve) => setTimeout(resolve, 1000))
  })
}
  1. Run senpi -p --provider <p> --model <m> "say hello" with a model that has a prompt-cache TTL.

Observed (Windows 11, isolated agent dir, scripted mock provider):

Variant Result
late-wake.js does not send exits at 16.4 s
late-wake.js sends still alive at 60 s
sends, with {"handoffHint":{"enabled":false}} exits at 15.0 s

The late turn needs an extension to trigger it, but the timer that keeps the process alive is the builtin's.

Expected behavior

The process exits once the run and shutdown are done. Suggested fix: ignore lifecycle events in handoff-hint after session_shutdown (a shutDown flag checked in agent_end, agent_settled, session_compact, model_select, the cache-warm listener and arm()), and unref() the timer, since the hint is advisory UI.

Version

2026.11.12 (handoff-hint unchanged on main through v2026.11.15-4)

hhgyu avatar Nov 17 '26 09:11 hhgyu