feat(coding-agent): rewrote the task tool to spawn one persistent subagent per call

The task tool now takes a single { agent, assignment, description, ... } and always runs the subagent in the background — the batch tasks[] array and shared context parameter are gone. Fan-out is parallel task calls; shared background flows through a '/Users/can/.omp/agent/sessions/-Projects-.tree-pi-commit/2026-06-10T15-36-32-782Z_019eb22d-970e-7000-8964-72c98becf3e8/local' file referenced in each assignment.\n\nIntroduces a persistent subagent lifecycle: finished subagents stay live as idle, the lifecycle manager parks them to disk after task.agentIdleTtlMs (default 7 minutes; 0 keeps them live until exit), and they revive automatically when prompted from the Agent Hub, messaged on IRC, or resumed via task. New task(resume: "<id>") revives an idle or parked subagent and runs a follow-up assignment in its existing session.\n\nAdds soft request budgets (explore/quick_task 40, others 90, configurable via task.softRequestBudget, 0 disables): crossing the budget injects a one-time wrap-up steer into the child; crossing 1.5× aborts the run gracefully. Cancelled/aborted subagent salvage replaces the old (no output) with the child's last activity snippet plus request/token stats; SingleResult tracks a per-child requests counter (assistant message_end events) used to sort agent lists in runtime-ascending order in both the live progress view (finished agents above pending/running) and the finalized result view, so rows no longer reshuffle on finalize. Adds a task gallery fixture variant for the resume path (renderer key separated from fixture key).\n\nAll task tests are reshaped around the single-call contract; tests for the discarded shared-context flow are removed, and new task-guards/task-resume/task-schema tests pin the new contract surface.
This commit is contained in:
can1357
2026-06-10 17:54:47 +02:00
parent 9613076e96
commit 9d99ae1af0
34 changed files with 2996 additions and 1945 deletions
@@ -0,0 +1,83 @@
import { afterEach, describe, expect, it, vi } from "bun:test";
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import { TaskTool, taskSchema } from "@oh-my-pi/pi-coding-agent/task";
import * as discoveryModule from "@oh-my-pi/pi-coding-agent/task/discovery";
import type { ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
// Contract (rework-contracts.md §3): the task tool spawns ONE agent per call.
// `tasks[]` and `context` are gone; `resume` continues an existing agent.
describe("task schema (single-spawn)", () => {
it("accepts {agent, assignment}", () => {
const parsed = taskSchema.safeParse({ agent: "explore", assignment: "Map the auth module." });
expect(parsed.success).toBe(true);
});
it("accepts {resume, assignment}", () => {
const parsed = taskSchema.safeParse({ resume: "AuthLoader", assignment: "Also check refresh tokens." });
expect(parsed.success).toBe(true);
});
it("requires assignment", () => {
const parsed = taskSchema.safeParse({ agent: "explore" });
expect(parsed.success).toBe(false);
});
it("carries no tasks/context fields", () => {
const parsed = taskSchema.safeParse({
agent: "explore",
assignment: "Map the auth module.",
context: "shared background",
tasks: [{ id: "A", assignment: "..." }],
});
expect(parsed.success).toBe(true);
if (parsed.success) {
// Unknown keys are stripped: the batch/context shape no longer exists.
expect("tasks" in parsed.data).toBe(false);
expect("context" in parsed.data).toBe(false);
}
});
});
describe("task spawn/resume validation", () => {
afterEach(() => {
vi.restoreAllMocks();
});
function createSession(): ToolSession {
return {
cwd: "/tmp",
hasUI: false,
settings: Settings.isolated({ "task.isolation.mode": "none" }),
getSessionFile: () => null,
getSessionSpawns: () => "*",
} as unknown as ToolSession;
}
async function executeText(params: unknown): Promise<string> {
vi.spyOn(discoveryModule, "discoverAgents").mockResolvedValue({ agents: [], projectAgentsDir: null });
const tool = await TaskTool.create(createSession());
const result = await tool.execute("tool-call", params);
return result.content.find(part => part.type === "text")?.text ?? "";
}
it("rejects resume + agent together", async () => {
const text = await executeText({ agent: "explore", resume: "AuthLoader", assignment: "..." });
expect(text).toContain("not both");
});
it("rejects neither resume nor agent", async () => {
const text = await executeText({ assignment: "..." });
expect(text).toContain("Missing `agent`");
});
it("rejects resume + isolated", async () => {
const text = await executeText({ resume: "AuthLoader", isolated: true, assignment: "..." });
expect(text).toContain("not resumable");
});
it("rejects a missing assignment", async () => {
const text = await executeText({ agent: "explore" });
expect(text).toContain("Missing `assignment`");
});
});