fix: register background tools, session replay, background persistence, and tests/docs for Task 5 spec-review

This commit is contained in:
pi
2026-04-12 14:24:00 +01:00
parent 4919833b4e
commit 71cfb97456
4 changed files with 82 additions and 26 deletions

View File

@@ -3,7 +3,7 @@ import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { createRunArtifacts } from "./src/artifacts.ts";
import { loadSubagentsConfig } from "./src/config.ts";
import { monitorRun } from "./src/monitor.ts";
import { monitorRun as defaultMonitorRun } from "./src/monitor.ts";
import { listAvailableModelReferences } from "./src/models.ts";
import { createProcessSingleRunner } from "./src/process-runner.ts";
import { createConfiguredRunSingleTask } from "./src/runner.ts";
@@ -18,10 +18,17 @@ import {
isInsideTmux,
} from "./src/tmux.ts";
// background imports
import { createBackgroundRegistry } from "./src/background-registry.ts";
import { createBackgroundAgentTool } from "./src/background-tool.ts";
import { createBackgroundStatusTool } from "./src/background-status-tool.ts";
import { createBackgroundAgentSchema } from "./src/background-schema.ts";
import { discoverSubagentPresets } from "./src/presets.ts";
const packageRoot = dirname(fileURLToPath(import.meta.url));
const wrapperPath = join(packageRoot, "src", "wrapper", "cli.mjs");
export default function subagentsExtension(pi: ExtensionAPI) {
export default function subagentsExtension(pi: ExtensionAPI, deps: any = {}) {
if (process.env.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR === "agent") {
pi.registerProvider("github-copilot", {
headers: { "X-Initiator": "agent" },
@@ -37,6 +44,8 @@ export default function subagentsExtension(pi: ExtensionAPI) {
let lastRegisteredModelsKey: string | undefined;
const monitorRun = deps.monitorRun ?? defaultMonitorRun;
const tmuxRunner = createTmuxSingleRunner({
assertInsideTmux() {
if (!isInsideTmux()) throw new Error('tmux runner requires pi to be running inside tmux.');
@@ -68,11 +77,10 @@ export default function subagentsExtension(pi: ExtensionAPI) {
});
// background registry and helpers
const registry = (typeof createBackgroundRegistry === 'function') ? createBackgroundRegistry() : undefined;
const registry = createBackgroundRegistry();
let latestUi: { notify(message: string, type?: "info" | "warning" | "error"): void; setStatus(key: string, text: string | undefined): void } | undefined;
function renderCounts() {
if (!registry) return undefined;
const counts = registry.getCounts();
return counts.total === 0 ? undefined : `bg: ${counts.running} running / ${counts.total} total`;
}
@@ -82,7 +90,6 @@ export default function subagentsExtension(pi: ExtensionAPI) {
}
async function watchBackgroundRun(runId: string) {
if (!registry) return;
const run = registry.getRun(runId);
if (!run || !run.paths || !run.paths.eventsPath || !run.paths.resultPath) return;
try {
@@ -147,8 +154,12 @@ export default function subagentsExtension(pi: ExtensionAPI) {
discoverSubagentPresets,
launchDetachedTask: processRunner.launchDetachedTask,
registerBackgroundRun(entry: any) {
if (!registry) return { running: 0, completed: 0, failed: 0, aborted: 0, total: 0 };
registry.recordLaunch({ runId: entry.runId, preset: entry.preset, task: entry.task, requestedModel: entry.requestedModel, resolvedModel: entry.resolvedModel, paths: entry.paths, meta: entry.meta });
const run = registry.getRun(entry.runId);
try {
// persist initial run record so session manager can rebuild later
pi.appendEntry("pi-subagents:bg-run", { run });
} catch (e) {}
return registry.getCounts();
},
watchBackgroundRun(runId: string) {
@@ -167,20 +178,26 @@ export default function subagentsExtension(pi: ExtensionAPI) {
// replay persisted runs if session manager provides entries
try {
const entries = ctx.sessionManager?.getEntries?.() ?? [];
const bgEntries = entries.filter((e: any) => e.type === "pi-subagents:bg-run" || e.type === "pi-subagents:bg-update");
// convert to registry runs if possible
const runs = bgEntries.map((be: any) => be.data?.run).filter(Boolean);
if (registry && runs.length) registry.replay(runs);
// clear existing registry and rebuild from session entries
registry.replay([]);
for (const e of entries) {
const type = (e.type ?? e.customType) as string | undefined;
if (type === "pi-subagents:bg-run") {
const run = e.data?.run ?? e.data;
if (run && run.runId) registry.recordLaunch(run as any);
} else if (type === "pi-subagents:bg-update") {
const data = e.data;
if (data && data.runId) registry.recordUpdate(data.runId, data as any);
}
}
} catch (e) {}
updateStatus();
// reattach watchers for running runs
try {
if (registry) {
for (const r of registry.getSnapshot({ includeCompleted: true })) {
if (r.status === "running") void watchBackgroundRun(r.runId);
}
for (const r of registry.getSnapshot({ includeCompleted: true })) {
if (r.status === "running") void watchBackgroundRun(r.runId);
}
} catch (e) {}