diff --git a/AGENTS.md b/AGENTS.md index 22bc335..ff6665d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -161,7 +161,7 @@ Lint has two established optional-chain warnings; do not add new warnings. Workf ## Limits -- 25 active loops +- 25 loops, excluding completed or cancelled orchestrations (100 of those may be retained) - 25 running monitors - 32 orchestration work items - 8 local orchestration workers under fixed pacing; `pacing: "aimd"` (the default unless `concurrency` or `PI_LOOP_PACING=fixed` is given) is bounded by its feedback window and the batch size, or by 8 unless every item declares `verify` (protocol-v2 pi-subagents queues beyond its own pool; npm pi-subagents has no queue and a launch starts or fails) diff --git a/docs/REFERENCE.md b/docs/REFERENCE.md index d1591a6..4bc68d9 100644 --- a/docs/REFERENCE.md +++ b/docs/REFERENCE.md @@ -231,7 +231,8 @@ External consumers import only `@trevonistrevon/pi-loop/api`. Deep `src/` import ## Limits and recovery gaps -- 25 active loops +- 25 loops, not counting completed or cancelled orchestrations retained for inspection +- 100 retained completed or cancelled orchestrations - 25 running monitors - 32 orchestration work items, 8 local workers, and 3 attempts per item - 8,192 result characters and 2,048 error characters per orchestration dispatch diff --git a/docs/USAGE_GUIDE.md b/docs/USAGE_GUIDE.md index a7dbd19..be65084 100644 --- a/docs/USAGE_GUIDE.md +++ b/docs/USAGE_GUIDE.md @@ -364,4 +364,4 @@ Workflow tool cards, `LoopList`, and `TaskList` stay at two lines while collapse On terminals at least 110 columns wide, a narrow progress panel docks at the top-right while a workflow, orchestration, running monitor, or native task is in progress. It never takes keyboard focus and disappears when nothing is in progress. Each workflow is a vertical state trail (`✓` visited, `▶` current with derived activity and attempt, `○` not yet entered) followed by its available outcomes; workflows that are paused or unclaimed are listed first. A swarm started from a workflow phase is listed under that phase's row instead of on its own; a finished phase shows its latest swarm's `done/total`, and a `∑` line under the workflow title totals agents and reported tokens across its swarms. A swarm whose workflow is gone, or whose phase row is not on screen, is listed on its own. Each orchestration (at most two) is a header with its progress and in-flight count, followed by up to five work items named by `label` or prompt: `●` in flight with its last reported dispatch state, elapsed time and attempt; `✗` failed; `?` uncertain; `○` pending; `✓` complete with duration and token count when the provider reported them; `–` cancelled. In-flight items are listed first and finished ones last, and a final `… N more` line counts the items not shown. The reported state is the last one received, not proof a worker is running. Running monitors (at most three, oldest first) show id, elapsed time, description or command, and their last output line, with a `quiet` flag after five minutes without output; monitor state is process-local, so nothing is shown for monitors from a previous pi process. Native tasks show `done/total`, in-progress work first, and flag a claim whose lease has expired. The panel is presentation only: it reads the same LoopStore and TaskStore snapshots as the status line and never claims work, resumes a controller, or schedules a wake. Task rows come only from standalone tasks, monitor rows only from this process's MonitorManager, and workflow rows only from LoopStore. Monitor output is untrusted and stripped of control bytes before display. Durations refresh on state changes and the existing 30-second heartbeat, and every minute while a monitor runs. Because it overlays the transcript's right edge, hide it with `/loop panel off` (or `/loop panel` to toggle), or start pi with `PI_LOOP_PANEL=off`. -The runtime allows at most 25 active loops and 25 running monitors. Each orchestration batch allows up to 32 work items, 8 local workers under fixed pacing (under `aimd`, the batch size when every item declares `verify`, otherwise 8), and 3 attempts per item, plus up to 3 overload re-sends per item that do not count as attempts. +The runtime allows at most 25 loops and 25 running monitors. Completed and cancelled orchestrations kept for inspection do not count toward the 25; up to 100 of them are retained, after which a new orchestration is refused until some are deleted with `LoopDelete`. Each orchestration batch allows up to 32 work items, 8 local workers under fixed pacing (under `aimd`, the batch size when every item declares `verify`, otherwise 8), and 3 attempts per item, plus up to 3 overload re-sends per item that do not count as attempts. diff --git a/src/store.ts b/src/store.ts index 1bdd2a7..7879d96 100644 --- a/src/store.ts +++ b/src/store.ts @@ -181,6 +181,12 @@ function workflowTransitionPausePolicy( } const MAX_LOOPS = 25; +/** Finished batches are kept only for their results, so they get a storage bound of their own. */ +const MAX_FINISHED_ORCHESTRATIONS = 100; + +function isFinishedOrchestration(entry: LoopEntry): boolean { + return entry.orchestration?.status === "completed" || entry.orchestration?.status === "cancelled"; +} const TOMBSTONE_TTL_MS = 10 * 60 * 1000; export class LoopStore extends ReducerBackedStore { @@ -205,9 +211,15 @@ export class LoopStore extends ReducerBackedStore; workflow?: WorkflowDefinition; actor?: WorkflowRuntimeActor; orchestration?: { definition: OrchestrationDefinitionInput; owner: OrchestrationActor } }): LoopEntry { return this.withLock(() => { - if (this.entries.size >= MAX_LOOPS) { + // The limit bounds controllers that can still act; a finished batch never dispatches or wakes again. + let finished = 0; + for (const entry of this.entries.values()) if (isFinishedOrchestration(entry)) finished++; + if (this.entries.size - finished >= MAX_LOOPS) { throw new Error(`Maximum of ${MAX_LOOPS} loops reached. Delete some before creating new ones.`); } + if (opts.orchestration && finished >= MAX_FINISHED_ORCHESTRATIONS) { + throw new Error(`Maximum of ${MAX_FINISHED_ORCHESTRATIONS} finished orchestrations retained. Delete some with LoopDelete before creating new ones.`); + } validateControllerDomain({ trigger, ...opts }); if (opts.workflow) { const validationError = validateWorkflowDefinition(opts.workflow); diff --git a/test/store.test.ts b/test/store.test.ts index e16c43c..ba3ff20 100644 --- a/test/store.test.ts +++ b/test/store.test.ts @@ -483,6 +483,43 @@ describe("LoopStore (in-memory)", () => { expect(s.get("2")!.status).toBe("active"); }); + describe("finished swarms and the loop limit", () => { + const swarm = (target: LoopStore = store) => target.create({ type: "dynamic" }, "Swarm", { + recurring: true, + orchestration: { owner: { sessionId: "s", runtimeId: "r", generation: 1 }, definition: { goal: "Swarm", work: [{ prompt: "A" }] } }, + }); + const finish = (entry: ReturnType, status: "completed" | "cancelled" | "needs_attention") => { + entry.orchestration!.status = status; + store.pause(entry.id, "orchestration_settlement", "batch settled"); + }; + + it("L1: completed and cancelled swarms kept for inspection do not use up the loop limit", () => { + for (let i = 0; i < 30; i++) finish(swarm(), i % 2 === 0 ? "completed" : "cancelled"); + for (let i = 0; i < 25; i++) store.create(cronTrigger, `loop ${i}`, { recurring: true }); + expect(store.list()).toHaveLength(55); + expect(() => store.create(cronTrigger, "overflow", { recurring: true })).toThrow("Maximum of 25 loops"); + }); + + it("L2: a running swarm, and one that needs attention, still count until it finishes", () => { + for (let i = 0; i < 24; i++) store.create(cronTrigger, `loop ${i}`, { recurring: true }); + const running = swarm(); + expect(() => swarm()).toThrow("Maximum of 25 loops"); + finish(running, "needs_attention"); + expect(() => swarm()).toThrow("Maximum of 25 loops"); + finish(running, "completed"); + expect(swarm().orchestration?.status).toBe("active"); + }); + + it("L3: finished swarms are themselves bounded, and the refusal says what to delete", () => { + for (let i = 0; i < 100; i++) finish(swarm(), "completed"); + expect(() => swarm()).toThrow("Maximum of 100 finished orchestrations retained. Delete some with LoopDelete before creating new ones."); + // Control: other controllers are not blocked by retained swarm results. + expect(store.create(cronTrigger, "still fine", { recurring: true }).status).toBe("active"); + store.delete("1"); + expect(swarm().orchestration?.status).toBe("active"); + }); + }); + it("enforces max loop limit", () => { for (let i = 0; i < 25; i++) { store.create(cronTrigger, `loop ${i}`, { recurring: true });