/** * The recorder that turns pi's tool events into Iteration records (ADR-0002). * * The extension cannot see the turn itself, only the events pi emits around it, * so this class keeps the small amount of state those events need: which fire is * still open, in which session. Three rules follow from the event order and are * the whole point of the class: * * - a fire opens a pending iteration; only once its wake is handed to pi does it * bind, and only then does it capture tool calls (ADR-0009); * - `agent_settled` closes the bound iterations, never a pending one; * - a call is recorded into the newest bound iteration of its session, because * the events carry no loop id, so an unbound iteration — a wake still waiting * for an idle agent — must not take the calls of a turn it did not wake; * - `tool_execution_end` pairs by tool call id across every iteration, so a late * result still lands on the call that produced it. * * Every write is persisted with the loop record, because the record only exists * on disk: a reader sees an iteration only after it has been written out. * * @author mystery4f <40482524+mystery4f@users.noreply.github.com> */ import type { TaskScope } from "../base/runtime-state.js"; import type { TaskSessions } from "../base/task-sessions.js"; import type { LoopFire } from "./loop-tasks.js"; /** The start event's fields the recorder needs; pi passes the same ones. */ export interface ToolCallStartEvent { readonly toolCallId: string; readonly toolName: string; readonly args: unknown; } /** The end event's fields the recorder needs. */ export interface ToolCallEndEvent { readonly toolCallId: string; readonly toolName: string; readonly result: unknown; readonly isError: boolean; readonly durationMs?: number; } export interface IterationsOptions { readonly sessions: TaskSessions; /** Clock seam for tests; defaults to `Date.now`. */ readonly now?: () => number; /** Tool calls kept per iteration; defaults to `ITERATION_CALL_LIMIT`. */ readonly callLimit?: number; /** Iterations kept per loop; defaults to `ITERATION_HISTORY_LIMIT`. */ readonly historyLimit?: number; } /** Both caps are positive counts: a zero would record nothing and say nothing. */ export declare function positiveLimit(value: number | undefined, fallback: number, name: string): number; export declare class Iterations { private readonly sessions; private readonly now; private readonly callLimit; private readonly historyLimit; constructor(options: IterationsOptions); /** * Open the iteration for one fire and persist it, so a wake that is still * waiting for an idle agent — or a turn that ends before its first tool call — * still leaves a record. The iteration stays pending: it takes no calls until * `bind` marks the wake as sent. */ open(fire: LoopFire): Promise; /** * Bind the iteration to the turn its wake produced: called right after the * wake is handed to pi. From here on the turn's tool calls are its own. */ bind(fire: LoopFire): void; /** * Forget a fire that never woke the model — its loop was paused, completed or * deleted while the wake waited. The iteration goes with it: a pending record * that stays `running` forever would claim a turn that never happened. A bound * iteration is left alone: its wake already left for pi, so the turn is real * and `settle` owns the record from here on. */ drop(fire: LoopFire): Promise; /** Record one tool call's start into the newest open iteration of the session. */ recordStart(scope: TaskScope, start: ToolCallStartEvent): Promise; /** * Record how one tool call ended, found by its id. A call the recorder never * saw is ignored: a turn outside a fire is not an iteration, and its calls * must not appear in this loop's record. */ recordEnd(scope: TaskScope, end: ToolCallEndEvent): Promise; /** * Close every bound iteration still open in the session, because one run * settles at a time and a bound fire's turn is that run. Calls still running * at that point cannot end any more: `aborted` when the run was aborted, * `interrupted` otherwise. Pending iterations stay pending: their wake has not * produced a turn yet, so this settle is not theirs. */ settle(scope: TaskScope, aborted: boolean): Promise; /** Drop the oldest iterations until the history cap holds. */ private dropOverflow; /** * The iteration a start belongs to: the newest open iteration its wake already * produced. Read from the session's current records every time, because a loop * update replaces the record object and a deleted loop must stop capturing * calls. A pending iteration is skipped, not pruned: its turn has not begun, * and the current turn belongs to whoever started it. */ private newestOpenIteration; private resolve; }