fix(pi): prevent oversized subagent result crashes
This commit is contained in:
@@ -140,6 +140,22 @@ test("activity exposes ordered transcript events while status and list keep only
|
||||
assert.doesNotMatch(JSON.stringify(listed), /private transcript body|secret file contents/u);
|
||||
});
|
||||
|
||||
test("status activity history keeps only the 100 most recent summaries", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const supervisor = new Supervisor(runner, "/tmp");
|
||||
const accepted = await spawnStarted(supervisor);
|
||||
|
||||
for (let index = 0; index < 150; index += 1) {
|
||||
runner.starts[0].events.running(`tick ${index}`);
|
||||
}
|
||||
|
||||
const history = supervisor.status(accepted.id).activityHistory;
|
||||
|
||||
assert.equal(history.length, 100);
|
||||
assert.equal(history[0].summary, "tick 50");
|
||||
assert.equal(history[99].summary, "tick 149");
|
||||
});
|
||||
|
||||
test("process failure reaches failed with diagnostics", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const supervisor = new Supervisor(runner, "/tmp");
|
||||
@@ -303,6 +319,22 @@ test("maxConcurrent preserves queued records", async () => {
|
||||
assert.equal(runner.starts.length, 2);
|
||||
});
|
||||
|
||||
test("clearTerminal returns only removed terminal ids", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const supervisor = new Supervisor(runner, "/tmp");
|
||||
const first = await spawnStarted(supervisor, "one");
|
||||
const second = await spawnStarted(supervisor, "two");
|
||||
const running = await spawnStarted(supervisor, "three");
|
||||
|
||||
runner.starts[0].events.completed("one done", "agent_settled");
|
||||
runner.starts[1].events.completed("two done", "agent_settled");
|
||||
|
||||
assert.deepEqual(supervisor.clearTerminal(), [first.id, second.id]);
|
||||
assert.throws(() => supervisor.status(first.id), /unknown subagent id/);
|
||||
assert.throws(() => supervisor.status(second.id), /unknown subagent id/);
|
||||
assert.equal(supervisor.status(running.id).state, "running");
|
||||
});
|
||||
|
||||
test("terminal records stay listed past ttl and remain retrievable until cleared", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const supervisor = new Supervisor(runner, "/tmp", { recentTerminalTtlMs: 5 });
|
||||
|
||||
@@ -44,6 +44,8 @@ const DEFAULT_TIMEOUTS = {
|
||||
runMs: 0,
|
||||
};
|
||||
|
||||
const MAX_ACTIVITY_HISTORY = 100;
|
||||
|
||||
export class Supervisor {
|
||||
private nextChild = 0;
|
||||
private readonly children = new Map<string, RunningChild>();
|
||||
@@ -90,14 +92,14 @@ export class Supervisor {
|
||||
return cloneResult(this.require(id).record);
|
||||
}
|
||||
|
||||
clearTerminal(ids?: string[]): SubagentStatus[] {
|
||||
clearTerminal(ids?: string[]): string[] {
|
||||
const selectedIds = ids ? [...new Set(ids.map((id) => id.trim()).filter(Boolean))] : undefined;
|
||||
if (selectedIds) for (const id of selectedIds) this.require(id);
|
||||
const cleared: SubagentStatus[] = [];
|
||||
const cleared: string[] = [];
|
||||
for (const [id, child] of this.children) {
|
||||
if (selectedIds && !selectedIds.includes(id)) continue;
|
||||
if (!isTerminal(child.record.status.state)) continue;
|
||||
cleared.push(cloneStatus(child.record.status));
|
||||
cleared.push(id);
|
||||
this.children.delete(id);
|
||||
}
|
||||
if (cleared.length > 0) this.emitChange();
|
||||
@@ -344,6 +346,9 @@ export class Supervisor {
|
||||
const summary = summarizeActivity(activity);
|
||||
record.status.currentActivity = summary;
|
||||
record.status.activityHistory.push(summary);
|
||||
if (record.status.activityHistory.length > MAX_ACTIVITY_HISTORY) {
|
||||
record.status.activityHistory.splice(0, record.status.activityHistory.length - MAX_ACTIVITY_HISTORY);
|
||||
}
|
||||
return activity;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user