// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2026 avtc /** * agent_settled event router — delegate to the agent-lifecycle domain method. * * Fires once per user prompt when pi has no pending retry/compaction/followUp * continuation (pi will not continue running automatically). Used to deliver the * staged phase-transition followUp (deferred) so it does not race a concurrently * typed user message. */ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { log } from "../../log.js"; import type { IAgentLifecycle } from "../../shared/workflow-types.js"; export function registerAgentSettled(pi: ExtensionAPI, lifecycle: IAgentLifecycle): void { pi.on("agent_settled", async (event) => { // pi 1.1.0+ carries `aborted` on agent_settled: true when the run ended cancelled // (user Esc, or an operational abort such as ctx.compact() aborting the in-flight // run). In both cases pi's own machinery owns the follow-through — the user stopped // the run, or the compaction handler delivers its own re-injection at compaction_end. // Draining the staged followUp here would restart a turn the user killed, or race // the compaction handler's re-inject (double delivery). Keep the slot staged; the // next non-aborted settle drains it. On pi 1.0.0 hosts `aborted` is undefined → // falsy → previous behavior. if (event.aborted === true) { log.info("[workflow] agent_settled aborted=true — skipping post-turn drain (staged followUp kept)"); return; } await lifecycle.onAgentSettled(); }); }