import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; import type { RunEvent } from "pi-agents/src/run/events.js"; import type { Server } from "node:net"; import type { AgentResultRole } from "../context/result-contracts.js"; import type { UsdModelCost } from "../models/price-catalog.js"; import type { EventIdentity } from "../telemetry/events.js"; import type { BoardEvent, BoardRecord, CoordinationAction, WarRoom } from "../workflows/war-room.js"; import type { Worktree } from "../worktrees/manager.js"; import type { AgentFact, AttributionManifest, Policy, PriceCatalog, PriceRate, TaskEnvelope, TaskLedger, Topology, UltraConfig } from "../types.js"; import type { ControllerState } from "./state-machine.js"; /** * Everything the controller knows about one in-flight run. It is deliberately one record rather * than several: the lifecycle, the budget, the delegated runtime, and the War Room board all * have to settle together, and splitting them into parallel maps is how they drift apart. */ export interface DelegatedExecution { executionId: string; phase: "standard" | "warroom-round" | "warroom-specialist" | "writer"; plannedModels: Map; resultRoles: Map; nodeKinds: Map; boardAuthors: Map; usageIncomplete: boolean; } export interface WarRoomParticipant { runId: string; instance: string; credential: string; generation: number; state: "pending" | "live" | "completed"; } export interface WarRoomSignalCredentialBinding { parentRunId: string; author: string; instance: string; generation: number; delegatedRunId?: string; ready: Promise; settle: () => void; } export interface AcceptedWarRoomSignal { event: BoardEvent; record: BoardRecord; actions: readonly CoordinationAction[]; duplicate?: true; } export interface WarRoomWriterFact { id: string; claim: string; evidence: string[]; boardProvenance: { qualification: "direct-evidence" | "confirmed-hypothesis"; source: { cursor: number; author: string; eventType: "evidence" | "hypothesis" }; confirmation?: { cursor: number; author: string; eventType: "confirmation"; evidence: string[] }; }; } export type DelegatedRunEvent = Exclude; export interface QueuedPiAgentsEvent { sequence: number; event: DelegatedRunEvent; } export interface UsageAccountingState { credits: number; /** Whether the credits came from a declared catalogue entry or one derived from the registry. */ priceSource: "catalog" | "derived"; usdReported?: number; spent: boolean; modelCallAttempted: boolean; reservationReleased: boolean; ledgerPersisted: boolean; complete: boolean; agentCreditsApplied: boolean; } export interface ActiveRun { state: ControllerState; piAgentsRunId?: string; identity: EventIdentity; topology: Topology; cwd: string; acceptanceCommand?: string; informational?: boolean; presentationQueued?: boolean; presentationUsageRecorded?: boolean; envelopeMeasurementRecorded?: boolean; repairAttempts: number; fingerprints: Map; model: string; spentCredits: number; spentUsd: number; reservedCredits?: number; policy?: Policy; config?: UltraConfig; modelCosts?: Map; /** Rates derived from the Pi registry for models the catalogue does not name, and the memo built from them. */ derivedRates?: Map; resolvedCatalog?: PriceCatalog; modelRegistry?: ExtensionContext["modelRegistry"]; context?: ExtensionContext; envelope?: TaskEnvelope; worktree?: Worktree; writerScope?: string[]; integrated?: boolean; nodeKinds?: Map; handoffs?: TraceHandoff[]; ledger?: TaskLedger; ledgerRedactions?: string[]; plannedModels?: Map; resultRoles?: Map; agentFacts?: Map; agentCredits?: Map; accountedDelegatedNodes?: Set; delegatedUsage?: Map; delegatedUsageIncomplete?: boolean; usageAccounting?: Map; softLimitEmitted?: boolean; hardLimitEmitted?: boolean; attributionManifest?: AttributionManifest; protocolFailure?: boolean; warRoom?: WarRoom; warRoomBatches?: Map; warRoomPhase?: "rounds" | "writer"; warRoomRound?: 1 | 2 | 3; warRoomMaxRounds?: number; warRoomSpecialists?: Set; warRoomParticipants?: Map; warRoomGeneration?: number; warRoomSignalBindings?: Map; warRoomParticipantCredentials?: Map; warRoomSignalReceipts?: Map>; warRoomSignalDeliveries?: Set>; warRoomAcceptedSpecialistRuns?: Set; warRoomSpecialistAdmission?: Promise; warRoomSpecialistReservations?: WeakMap>; warRoomFinalizing?: boolean; warRoomRoundCompletion?: { runId: string }; warRoomSignalPath?: string; warRoomSignalServer?: Server; delegatedExecutionSequence?: number; delegatedExecutions?: Map; delegatedRunStates?: Map; inFlightStarts?: Set>; unboundDelegatedStarts?: number; terminalIntent?: ControllerState; terminalSettlement?: Promise; manualStopPending?: boolean; piAgentsEventQueue?: QueuedPiAgentsEvent[]; piAgentsEventGate?: boolean; piAgentsEventDrain?: Promise; piAgentsEventEffects?: Set; terminalResultRecorded?: boolean; blockerReason?: string; } export interface TraceHandoff { fromNodeId: string; toNodeId: string; kind: "task-envelope" | "result"; payloadChars: number; payloadEstimatedTokens: number; factIds: string[]; hypothesisIds: string[]; contentHash: string; traceStored: boolean; received: boolean; }