Files
oh-my-pi/packages/coding-agent/src/task/output-manager.ts
T
can1357 9d99ae1af0 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.
2026-06-10 17:54:47 +02:00

89 lines
2.9 KiB
TypeScript

/**
* Session-scoped manager for agent output IDs.
*
* Keeps every subagent output id unique within a session without polluting the
* common case with bookkeeping. A requested name is used verbatim the first
* time it appears; only a *repeated* name gets a numeric suffix to disambiguate
* it (e.g. "Anna", "Anna-2", "Anna-3"). When a parent prefix is configured, ids
* are nested under it (e.g. "Anna.Bob") so hierarchical outputs stay grouped.
*
* This enables reliable agent:// URL resolution and prevents artifact
* collisions across repeated or nested task invocations.
*/
import * as fs from "node:fs/promises";
/**
* Manages agent output ID allocation to ensure uniqueness.
*
* The first allocation of a given name keeps the name as-is; subsequent
* allocations of the same name get a `-2`, `-3`, … suffix. On resume, scans
* existing output files so previously written outputs are never overwritten.
*/
export class AgentOutputManager {
#initialized = false;
/** Final ids already handed out, relative to this manager's scope. */
readonly #taken = new Set<string>();
readonly #getArtifactsDir: () => string | null;
readonly #parentPrefix: string | undefined;
constructor(getArtifactsDir: () => string | null, options?: { parentPrefix?: string }) {
this.#getArtifactsDir = getArtifactsDir;
this.#parentPrefix = options?.parentPrefix;
}
/**
* Seed the taken-id set from output files already on disk so a resumed
* session never reuses a name that would clobber a prior subagent's output.
*/
async #ensureInitialized(): Promise<void> {
if (this.#initialized) return;
this.#initialized = true;
const dir = this.#getArtifactsDir();
if (!dir) return;
let files: string[];
try {
files = await fs.readdir(dir);
} catch {
return; // Directory doesn't exist yet
}
const prefix = this.#parentPrefix ? `${this.#parentPrefix}.` : "";
for (const file of files) {
if (!file.endsWith(".md")) continue;
let rest = file.slice(0, -3); // drop ".md"
if (prefix) {
if (!rest.startsWith(prefix)) continue;
rest = rest.slice(prefix.length);
}
// Requested ids never contain "."; a dot marks a nested child, so this
// manager only owns the first segment of whatever remains.
const dot = rest.indexOf(".");
const segment = dot === -1 ? rest : rest.slice(0, dot);
if (segment) this.#taken.add(segment);
}
}
/** Pick the first free name (base, then `base-2`, `base-3`, …) and reserve it. */
#allocateUnique(id: string): string {
let candidate = id;
for (let n = 2; this.#taken.has(candidate); n++) {
candidate = `${id}-${n}`;
}
this.#taken.add(candidate);
return this.#parentPrefix ? `${this.#parentPrefix}.${candidate}` : candidate;
}
/**
* Allocate a unique ID.
*
* @param id Requested ID (e.g., "Anna")
* @returns Unique ID ("Anna" first, then "Anna-2", "Anna-3", …)
*/
async allocate(id: string): Promise<string> {
await this.#ensureInitialized();
return this.#allocateUnique(id);
}
}