fix: allow research runs to restart after stop; improve FTS search, agent limits, and timeouts
- AgentTree.restart() revives a stopped run in the same session: resets the root, archives old branches, and restores spawnability (stop no longer cancels the root). - ResearchLoop.start() restarts from stopped/completed and resets iteration counters; setGoal is allowed in terminal states; supervisor 'start' revives the tree when the root is not spawnable. - Cancelled/failed/archived children no longer count toward the per-agent child limit and archived agents are ignored by the duplicate-task guard. - cancel() no longer overwrites an already recorded agent result. - Add agent_timeout_seconds (default 1800) so hung agent sessions fail instead of blocking wait() forever. - FTS search now prefix-matches tokens (tolerates inflections) and safely handles punctuation/FTS5 metacharacters instead of returning false negatives or throwing. - Add tests for restart semantics, child limits, result preservation, agent timeouts, and FTS morphology/special characters (38/38 passing).
This commit is contained in:
@@ -18,4 +18,8 @@ describe("AgentTree", () => {
|
||||
it("restores relationships and marks active work interrupted", async () => { const { store, tree } = setup(); const child = await tree.spawn(request(tree.rootId, "Crash", "Investigate recovery behavior after process crash")); const path = store.manifestPath(tree.runId); const manifest = JSON.parse(readFileSync(path, "utf8")); manifest.agents[child.id].status = "running"; writeFileSync(path, JSON.stringify(manifest)); const restored = AgentTree.restore(store, new FakeRuntimeFactory(), DEFAULT_CONFIG, tree.runId); expect(restored.inspect(child.id).status).toBe("interrupted"); expect(restored.inspect(child.id).parentId).toBe(tree.rootId); });
|
||||
it("rejects vague tasks", async () => { const { tree } = setup(); await expect(tree.spawn(request(tree.rootId, "Vague", "look"))).rejects.toThrow(AgentTreeError); });
|
||||
it("bridges partial child results to the Supervisor root", async () => { const dir = mkdtempSync(resolve(tmpdir(), "hm-tree-message-")); const updates: string[] = []; const tree = new AgentTree(new RunStore(dir), new FakeRuntimeFactory(), DEFAULT_CONFIG, { goal: "Receive upward results", onRootMessage: (from, message) => updates.push(`${from}:${message}`) }); await tree.message(tree.rootId, "partial evidence", "child-1"); expect(updates).toEqual(["child-1:partial evidence"]); });
|
||||
it("keeps the root spawnable after stop, and restart revives the run", async () => { const { tree } = setup(); const child = await tree.spawn(request(tree.rootId, "First", "First sufficiently concrete research assignment")); await tree.start(child.id); await tree.stop(); expect(tree.status).toBe("stopped"); expect(tree.inspect(tree.rootId).status).not.toBe("cancelled"); tree.restart("Entirely new research question to investigate"); expect(tree.status).toBe("active"); expect(tree.inspect(tree.rootId).status).toBe("created"); expect(tree.inspect(child.id).status).toBe("archived"); expect(tree.inspect(tree.rootId).children).toEqual([]); await expect(tree.spawn(request(tree.rootId, "Next", "Next sufficiently concrete research assignment"))).resolves.toBeTruthy(); });
|
||||
it("does not count cancelled or failed children toward the child limit", async () => { const { tree } = setup({ ...DEFAULT_CONFIG, max_children_per_agent: 2 }); const first = await tree.spawn(request(tree.rootId, "Alpha", "First concrete assignment in a limited tree")); await tree.cancel(first.id); const second = await tree.spawn(request(tree.rootId, "Beta", "Second concrete assignment in a limited tree")); await tree.cancel(second.id); await expect(tree.spawn(request(tree.rootId, "Gamma", "Third concrete assignment in a limited tree"))).resolves.toBeTruthy(); });
|
||||
it("cancel does not clobber an already recorded result", async () => { const { tree } = setup(); const child = await tree.spawn(request(tree.rootId, "Done", "Complete a concrete assignment and return")); const result = await tree.start(child.id); expect(result.status).toBe("completed"); await tree.cancel(child.id); expect(tree.inspect(child.id).status).toBe("cancelled"); expect(tree.inspect(child.id).result?.status).toBe("completed"); });
|
||||
it("fails agents that exceed the configured timeout", async () => { const { tree } = setup({ ...DEFAULT_CONFIG, agent_timeout_seconds: 0.05 }, 200); const child = await tree.spawn(request(tree.rootId, "Slow", "Run a deliberately slow concrete assignment")); const result = await tree.start(child.id); expect(result.status).toBe("failed"); expect(result.summary).toMatch(/timed out/); expect(tree.inspect(child.id).error).toMatch(/timed out/); });
|
||||
});
|
||||
|
||||
@@ -8,4 +8,6 @@ describe("ResearchMemory", () => {
|
||||
it("persists findings and performs rebuildable full-text search", () => { const memory = new ResearchMemory(mkdtempSync(resolve(tmpdir(), "hm-memory-"))); const id = memory.save({ type: "fact", status: "corroborated", createdBy: "verifier", runId: "run-1", title: "Catalyst result", statement: "Catalyst alpha improves the measured yield.", evidence: "Independent measurements agree.", sources: ["source-a", "source-b"], limitations: "Small sample" }); expect(memory.search("catalyst")[0]?.id).toBe(id); memory.close(); expect(memory.rebuildIndex()).toBe(1); expect(memory.search("yield")[0]?.id).toBe(id); memory.close(); });
|
||||
it("does not allow unsourced claims to become corroborated facts", () => { const memory = new ResearchMemory(mkdtempSync(resolve(tmpdir(), "hm-memory-"))); expect(() => memory.save({ type: "fact", status: "corroborated", createdBy: "agent", runId: "run", title: "Claim", statement: "Unsupported" })).toThrow(/requires sources/); memory.close(); });
|
||||
it("keeps negative results searchable", () => { const memory = new ResearchMemory(mkdtempSync(resolve(tmpdir(), "hm-memory-"))); memory.save({ type: "experiment_result", status: "rejected", createdBy: "runner", runId: "run", title: "Null replication", statement: "No measurable effect", negativeResult: true }); expect(memory.search("replication")).toHaveLength(1); memory.close(); });
|
||||
it("prefixes tokens so inflected forms of the same word match", () => { const memory = new ResearchMemory(mkdtempSync(resolve(tmpdir(), "hm-memory-"))); const id = memory.save({ type: "fact", status: "observed", createdBy: "agent", runId: "run", title: "Ribosome dynamics", statement: "Рибосомами управляют рибосомные белки в рибосоме." }); const hits = memory.search("рибосома"); expect(hits.some((row) => row.id === id)).toBe(true); expect(hits[0]?.title).toBe("Ribosome dynamics"); memory.close(); });
|
||||
it("survives queries with punctuation and FTS metacharacters", () => { const memory = new ResearchMemory(mkdtempSync(resolve(tmpdir(), "hm-memory-"))); memory.save({ type: "fact", status: "observed", createdBy: "agent", runId: "run", title: "Code expansion", statement: "non-AUG starts and C++ style operators are searched." }); expect(memory.search("non-AUG").some((row) => row.title === "Code expansion")).toBe(true); expect(memory.search("C++").some((row) => row.title === "Code expansion")).toBe(true); expect(memory.search("NOT")).toHaveLength(0); memory.close(); });
|
||||
});
|
||||
|
||||
@@ -9,4 +9,7 @@ const report = (newFindings = 0) => ({ goal: "goal", tasks: ["task"], activeAgen
|
||||
describe("ResearchLoop", () => {
|
||||
it("stops after configured iterations without information gain", () => { const loop = new ResearchLoop(mkdtempSync(resolve(tmpdir(), "hm-loop-")), "run", "goal", { ...DEFAULT_CONFIG, max_iterations_without_progress: 2 }); loop.start(); loop.record(report()); expect(loop.record(report()).status).toBe("completed"); expect(loop.snapshot().stopReason).toMatch(/without information gain/); });
|
||||
it("resets no-progress counter and handles pause/resume", () => { const loop = new ResearchLoop(mkdtempSync(resolve(tmpdir(), "hm-loop-")), "run", "goal", DEFAULT_CONFIG); loop.start(); loop.record(report()); loop.record(report(1)); expect(loop.snapshot().noProgressIterations).toBe(0); loop.pause(); expect(loop.snapshot().status).toBe("paused"); loop.resume(); expect(loop.snapshot().status).toBe("running"); });
|
||||
it("restarts from stopped and resets counters and reports", () => { const loop = new ResearchLoop(mkdtempSync(resolve(tmpdir(), "hm-loop-")), "run", "goal", DEFAULT_CONFIG); loop.start(); loop.record(report(1)); expect(loop.snapshot().iteration).toBe(1); loop.stop(); expect(loop.snapshot().status).toBe("stopped"); loop.start(); expect(loop.snapshot().status).toBe("running"); expect(loop.snapshot().iteration).toBe(0); expect(loop.snapshot().reports).toHaveLength(0); expect(loop.snapshot().stopReason).toBeUndefined(); });
|
||||
it("allows changing the goal after stop, then restarting", () => { const loop = new ResearchLoop(mkdtempSync(resolve(tmpdir(), "hm-loop-")), "run", "old goal", DEFAULT_CONFIG); loop.start(); loop.record(report()); loop.stop(); loop.setGoal("new goal"); expect(loop.snapshot().goal).toBe("new goal"); loop.start(); expect(loop.snapshot().status).toBe("running"); });
|
||||
it("rejects changing the goal mid-run after iterations", () => { const loop = new ResearchLoop(mkdtempSync(resolve(tmpdir(), "hm-loop-")), "run", "goal", DEFAULT_CONFIG); loop.start(); loop.record(report()); expect(() => loop.setGoal("different")).toThrow(/new research run/); });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user