@xzzpig/pi-subagents 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -4
- package/README.md +1 -1
- package/agents/reviewer.md +12 -2
- package/docs/agents.md +4 -4
- package/docs/configuration.md +6 -6
- package/docs/extension-api.md +14 -4
- package/docs/models.md +47 -6
- package/docs/observability.md +14 -3
- package/docs/tool-reference.md +21 -6
- package/docs/workflows.md +7 -1
- package/index.ts +10 -1
- package/package.json +3 -3
- package/prompts/council.md +19 -7
- package/prompts/parallel-review.md +5 -1
- package/prompts/review-loop.md +9 -5
- package/skills/council-mode/SKILL.md +50 -26
- package/skills/pi-subagents/SKILL.md +5 -1
- package/skills/pi-subagents/references/constraints-and-recipes.md +16 -1
- package/skills/pi-subagents/references/execution-controls.md +13 -4
- package/skills/pi-subagents/references/multi-lane-orchestration.md +3 -3
- package/skills/pi-subagents/references/prompting-and-roles.md +14 -7
- package/src/agents/agent-management.ts +115 -14
- package/src/agents/agent-serializer.ts +4 -2
- package/src/agents/agents.ts +226 -48
- package/src/api/external-job-provider.ts +10 -1
- package/src/api/preflight.ts +52 -19
- package/src/api/project-panes.ts +2 -0
- package/src/extension/doctor.ts +10 -0
- package/src/extension/fanout-child.ts +3 -2
- package/src/extension/index.ts +24 -4
- package/src/extension/public-execution.ts +12 -9
- package/src/extension/rpc.ts +77 -3
- package/src/extension/schemas.ts +7 -1
- package/src/extension/tool-description.ts +9 -6
- package/src/extension/tool-result.ts +19 -0
- package/src/inspectors/herdr/client.ts +3 -3
- package/src/inspectors/herdr/focus.ts +55 -0
- package/src/inspectors/herdr/project-panes.ts +228 -44
- package/src/integrations/herdr-status.ts +26 -4
- package/src/runs/background/async-execution.ts +146 -17
- package/src/runs/background/async-job-tracker.ts +33 -14
- package/src/runs/background/async-resume.ts +23 -2
- package/src/runs/background/async-retention.ts +1 -1
- package/src/runs/background/async-status.ts +10 -0
- package/src/runs/background/chain-root-attachment.ts +5 -0
- package/src/runs/background/control-channel.ts +98 -10
- package/src/runs/background/notify.ts +62 -1
- package/src/runs/background/result-watcher.ts +4 -3
- package/src/runs/background/run-status.ts +15 -1
- package/src/runs/background/stale-run-reconciler.ts +3 -0
- package/src/runs/background/subagent-runner.ts +333 -48
- package/src/runs/background/wait-completions.ts +2 -0
- package/src/runs/background/wait-tool.ts +4 -3
- package/src/runs/foreground/async-stop-action.ts +23 -2
- package/src/runs/foreground/execution.ts +148 -13
- package/src/runs/foreground/foreground-history.ts +3 -2
- package/src/runs/foreground/subagent-executor.ts +417 -50
- package/src/runs/foreground/workflow-detach-reconcile.ts +18 -6
- package/src/runs/shared/acceptance.ts +11 -5
- package/src/runs/shared/agent-contract.ts +1 -1
- package/src/runs/shared/child-identity.ts +36 -0
- package/src/runs/shared/completion-guard.ts +52 -2
- package/src/runs/shared/dynamic-fanout.ts +1 -1
- package/src/runs/shared/extension-bindings.ts +78 -0
- package/src/runs/shared/external-cli-runner.ts +2 -0
- package/src/runs/shared/external-job-bridge.ts +53 -37
- package/src/runs/shared/external-job-runner.ts +126 -23
- package/src/runs/shared/fast-mode-extension.ts +10 -0
- package/src/runs/shared/model-exclusions.ts +2 -1
- package/src/runs/shared/model-fallback.ts +48 -22
- package/src/runs/shared/model-scope.ts +106 -39
- package/src/runs/shared/mutation-evidence.ts +145 -0
- package/src/runs/shared/orca-progress-tabs.ts +71 -9
- package/src/runs/shared/parallel-utils.ts +15 -0
- package/src/runs/shared/pi-args.ts +78 -1
- package/src/runs/shared/structured-output.ts +18 -4
- package/src/runs/shared/subagent-prompt-runtime.ts +48 -15
- package/src/runs/shared/tool-availability.ts +1 -3
- package/src/shared/launch-contract.ts +12 -5
- package/src/shared/settings.ts +6 -1
- package/src/shared/thinking-ceiling.ts +52 -0
- package/src/shared/types.ts +120 -5
- package/src/tui/fleet-status.ts +66 -13
- package/src/tui/render.ts +5 -4
- package/src/watchdog/permission-arbiter.ts +59 -52
- package/src/workflows/scripted-workflow.ts +156 -12
package/src/tui/fleet-status.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
|
2
2
|
import { type EditorComponent, isKeyRelease, Key, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
3
|
import { snapshotExternalRuns } from "../api/external-runs.ts";
|
|
4
4
|
import { formatModelThinking } from "../shared/formatters.ts";
|
|
5
|
-
import type { AsyncJobStep, FleetViewPlacement, NestedRunSummary, NestedStepSummary, SubagentState } from "../shared/types.ts";
|
|
5
|
+
import type { AsyncJobState, AsyncJobStep, FleetViewPlacement, HerdrProjectPaneSnapshot, NestedRunSummary, NestedStepSummary, SubagentState } from "../shared/types.ts";
|
|
6
6
|
import { formatWorkflowJsonPreview } from "../workflows/scripted-workflow.ts";
|
|
7
7
|
|
|
8
8
|
export const FLEET_STATUS_WIDGET_KEY = "subagent-fleet-status";
|
|
@@ -17,6 +17,7 @@ type FleetStatusTui = {
|
|
|
17
17
|
};
|
|
18
18
|
type FleetStatusEntry = {
|
|
19
19
|
key: string;
|
|
20
|
+
surface?: "project-pane";
|
|
20
21
|
parentKey?: string;
|
|
21
22
|
workflowWrapper?: boolean;
|
|
22
23
|
agent: string;
|
|
@@ -26,6 +27,7 @@ type FleetStatusEntry = {
|
|
|
26
27
|
tokens: number;
|
|
27
28
|
state: string;
|
|
28
29
|
external?: true;
|
|
30
|
+
projectPane?: HerdrProjectPaneSnapshot;
|
|
29
31
|
nestedChildren?: NestedRunSummary[];
|
|
30
32
|
workflowRows?: FleetWorkflowRow[];
|
|
31
33
|
};
|
|
@@ -284,7 +286,32 @@ function foregroundDescription(control: { parentWorkflowRunId?: string; workflow
|
|
|
284
286
|
}
|
|
285
287
|
|
|
286
288
|
function activeLeafAgentCount(entries: FleetStatusEntry[]): number {
|
|
287
|
-
return entries.filter((entry) => !entry.workflowWrapper).length;
|
|
289
|
+
return entries.filter((entry) => !entry.workflowWrapper && !entry.surface).length;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function projectPaneNeedsAttention(pane: HerdrProjectPaneSnapshot): boolean {
|
|
293
|
+
return ["attention", "blocked", "paused", "failed", "error"].some((status) => pane.agentStatus.includes(status))
|
|
294
|
+
|| pane.summary?.includes("⚠") === true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function projectName(projectRoot: string): string {
|
|
298
|
+
return projectRoot.split(/[\\/]/).filter(Boolean).at(-1) ?? projectRoot;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function projectPaneEntries(state: SubagentState): FleetStatusEntry[] {
|
|
302
|
+
return [...(state.herdrProjectPanes?.values() ?? [])]
|
|
303
|
+
.filter((pane) => pane.state === "open")
|
|
304
|
+
.sort((left, right) => left.openedAt.localeCompare(right.openedAt) || left.projectRoot.localeCompare(right.projectRoot))
|
|
305
|
+
.map((pane) => ({
|
|
306
|
+
key: `project-pane:${pane.projectRoot}`,
|
|
307
|
+
surface: "project-pane" as const,
|
|
308
|
+
agent: `${projectName(pane.projectRoot)} · ${pane.paneId}`,
|
|
309
|
+
description: pane.summary,
|
|
310
|
+
startedAt: Date.parse(pane.openedAt) || pane.refreshedAt,
|
|
311
|
+
tokens: 0,
|
|
312
|
+
state: pane.agentStatus || "unknown",
|
|
313
|
+
projectPane: pane,
|
|
314
|
+
}));
|
|
288
315
|
}
|
|
289
316
|
|
|
290
317
|
export function collectFleetStatusEntries(state: SubagentState): FleetStatusEntry[] {
|
|
@@ -406,6 +433,7 @@ export function collectFleetStatusEntries(state: SubagentState): FleetStatusEntr
|
|
|
406
433
|
}
|
|
407
434
|
}
|
|
408
435
|
|
|
436
|
+
entries.push(...projectPaneEntries(state));
|
|
409
437
|
return entries.sort((left, right) => left.startedAt - right.startedAt || left.key.localeCompare(right.key));
|
|
410
438
|
}
|
|
411
439
|
|
|
@@ -483,7 +511,7 @@ export class SubagentFleetStatus {
|
|
|
483
511
|
this.clearWidget();
|
|
484
512
|
return;
|
|
485
513
|
}
|
|
486
|
-
if (this.
|
|
514
|
+
if (!this.hasInlineSurface()) {
|
|
487
515
|
this.active = false;
|
|
488
516
|
this.selectedKey = "main";
|
|
489
517
|
this.lastRenderKey = "";
|
|
@@ -578,26 +606,31 @@ export class SubagentFleetStatus {
|
|
|
578
606
|
}
|
|
579
607
|
|
|
580
608
|
render(width: number, theme: Theme): string[] {
|
|
581
|
-
if (this.
|
|
609
|
+
if (!this.hasInlineSurface()) return [];
|
|
582
610
|
if (!this.active) {
|
|
583
|
-
const
|
|
611
|
+
const workEntries = this.entries.filter((entry) => !entry.surface);
|
|
612
|
+
const projectEntries = this.entries.filter((entry) => entry.surface === "project-pane");
|
|
613
|
+
const tokens = workEntries.reduce((total, entry) => total + entry.tokens, 0);
|
|
584
614
|
const capacity = this.state.activeAsyncCapacity;
|
|
585
|
-
const hasNativeRows =
|
|
615
|
+
const hasNativeRows = workEntries.some((entry) => !entry.external);
|
|
586
616
|
const showNativeSummary = hasNativeRows || Boolean(capacity?.used);
|
|
587
617
|
const asyncRuns = capacity && showNativeSummary ? `Async runs ${capacity.used}/${capacity.limit || "∞"}` : "";
|
|
588
|
-
const activeEntries = activeLeafAgentCount(
|
|
589
|
-
const noun =
|
|
618
|
+
const activeEntries = activeLeafAgentCount(workEntries);
|
|
619
|
+
const noun = workEntries.some((entry) => entry.external) ? "job" : "agent";
|
|
590
620
|
const agents = activeEntries > 0 ? `${activeEntries} active ${noun}${activeEntries === 1 ? "" : "s"}` : "";
|
|
591
|
-
const
|
|
621
|
+
const paneAttention = projectEntries.filter((entry) => entry.projectPane && projectPaneNeedsAttention(entry.projectPane)).length;
|
|
622
|
+
const panes = projectEntries.length > 0 ? `${projectEntries.length} pane${projectEntries.length === 1 ? "" : "s"}${paneAttention ? ` (${paneAttention} ⚠)` : ""}` : "";
|
|
623
|
+
const label = [agents, asyncRuns, panes].filter(Boolean).join(" · ");
|
|
592
624
|
const detail = [showNativeSummary ? formatFleetTokens(tokens) : undefined, "↓/← to inspect"].filter(Boolean).join(" · ");
|
|
593
|
-
return [truncateToWidth(` ${theme.fg("muted", label)} · ${theme.fg("dim", detail)}`, width)];
|
|
625
|
+
return [truncateToWidth(` ${theme.fg("muted", label)}${label && detail ? " · " : ""}${theme.fg("dim", detail)}`, width)];
|
|
594
626
|
}
|
|
595
627
|
const roster = this.rosterKeys();
|
|
596
628
|
const selectedIndex = Math.max(0, roster.indexOf(this.selectedKey));
|
|
597
629
|
const lines = [truncateToWidth(` ${theme.fg("dim", "↑↓/jk select · enter inspect · esc back")}`, width), ""];
|
|
598
630
|
lines.push(truncateToWidth(` ${this.bullet(0, selectedIndex, theme)} main`, width));
|
|
599
631
|
|
|
600
|
-
const
|
|
632
|
+
const workEntries = this.entries.filter((entry) => !entry.surface);
|
|
633
|
+
const tree = fleetTreeRows(workEntries);
|
|
601
634
|
const selectedTreeIndex = Math.max(0, tree.findIndex((row) => (row.kind === "owner" || row.kind === "child") && row.entry.key === this.selectedKey));
|
|
602
635
|
const visibleCount = Math.min(this.maxAgentRows, tree.length);
|
|
603
636
|
const start = selectedTreeIndex < visibleCount ? 0 : selectedTreeIndex - visibleCount + 1;
|
|
@@ -615,15 +648,30 @@ export class SubagentFleetStatus {
|
|
|
615
648
|
}
|
|
616
649
|
}
|
|
617
650
|
if (hiddenBelow > 0) lines.push(rightAlign("", theme.fg("dim", `↓ ${hiddenBelow} more`), width));
|
|
651
|
+
this.renderProjectPaneSection(lines, selectedIndex, width, theme);
|
|
618
652
|
return lines;
|
|
619
653
|
}
|
|
620
654
|
|
|
655
|
+
private renderProjectPaneSection(lines: string[], selectedIndex: number, width: number, theme: Theme): void {
|
|
656
|
+
const entries = this.entries.filter((entry) => entry.surface === "project-pane");
|
|
657
|
+
if (!entries.length) return;
|
|
658
|
+
lines.push("", truncateToWidth(` ${theme.fg("dim", "project panes")}`, width));
|
|
659
|
+
for (const entry of entries) {
|
|
660
|
+
const rosterIndex = this.entries.findIndex((candidate) => candidate.key === entry.key) + 1;
|
|
661
|
+
lines.push(this.renderEntry(rosterIndex, selectedIndex, entry, width, theme));
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
|
|
621
666
|
private renderEntry(rosterIndex: number, selectedIndex: number, entry: FleetStatusEntry, width: number, theme: Theme, branch?: string): string {
|
|
622
667
|
const agent = entry.modelThinking ? `${entry.agent} (${entry.modelThinking})` : entry.agent;
|
|
623
668
|
const prefix = branch ? ` ${branch}` : " ";
|
|
624
669
|
const left = `${prefix} ${this.bullet(rosterIndex, selectedIndex, theme)} ${theme.fg("muted", agent)} · ${entry.state}`;
|
|
625
670
|
const elapsed = Date.now() - entry.startedAt;
|
|
626
|
-
const
|
|
671
|
+
const rightText = entry.projectPane
|
|
672
|
+
? `${entry.projectPane.summary ?? "—"} · ${formatFleetElapsed(Date.now() - entry.projectPane.refreshedAt)} ago`
|
|
673
|
+
: entry.external ? formatFleetElapsed(elapsed) : `${formatFleetElapsed(elapsed)} · ${formatFleetTokens(entry.tokens)}`;
|
|
674
|
+
const right = theme.fg("dim", rightText);
|
|
627
675
|
return rightAlign(left, right, width);
|
|
628
676
|
}
|
|
629
677
|
|
|
@@ -692,6 +740,7 @@ export class SubagentFleetStatus {
|
|
|
692
740
|
entries: this.entries.map((entry) => this.active
|
|
693
741
|
? [
|
|
694
742
|
entry.key,
|
|
743
|
+
entry.surface,
|
|
695
744
|
entry.parentKey,
|
|
696
745
|
entry.agent,
|
|
697
746
|
entry.state,
|
|
@@ -719,10 +768,14 @@ export class SubagentFleetStatus {
|
|
|
719
768
|
row.overflow,
|
|
720
769
|
]),
|
|
721
770
|
]
|
|
722
|
-
: [entry.key, entry.state, entry.external, entry.tokens]),
|
|
771
|
+
: [entry.key, entry.state, entry.external, entry.surface, entry.tokens, entry.projectPane?.refreshedAt, entry.projectPane?.summary]),
|
|
723
772
|
});
|
|
724
773
|
}
|
|
725
774
|
|
|
775
|
+
private hasInlineSurface(): boolean {
|
|
776
|
+
return this.entries.length > 0 || Boolean(this.state.activeAsyncCapacity?.used);
|
|
777
|
+
}
|
|
778
|
+
|
|
726
779
|
private getActiveUiContext(): ExtensionContext | undefined {
|
|
727
780
|
const ctx = this.ctx;
|
|
728
781
|
if (!ctx) return undefined;
|
package/src/tui/render.ts
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
type WorkflowNodeStatus,
|
|
19
19
|
type MainWindowRendererConfig,
|
|
20
20
|
MAX_WIDGET_JOBS,
|
|
21
|
-
|
|
21
|
+
WIDGET_ANIMATION_INTERVAL_MS,
|
|
22
22
|
WIDGET_KEY,
|
|
23
23
|
} from "../shared/types.ts";
|
|
24
24
|
import { sanitizeDisplayText, truncateDisplayText } from "../shared/display-text.ts";
|
|
@@ -1580,7 +1580,7 @@ function buildWidgetComponent(jobs: AsyncJobState[], expanded: boolean): (_tui:
|
|
|
1580
1580
|
const container = new Container();
|
|
1581
1581
|
container.render = (renderWidth: number): string[] => {
|
|
1582
1582
|
const width = Math.max(0, renderWidth - 2);
|
|
1583
|
-
const frame = Math.floor(Date.now() /
|
|
1583
|
+
const frame = Math.floor(Date.now() / WIDGET_ANIMATION_INTERVAL_MS);
|
|
1584
1584
|
const buildLines = (): string[] => expanded
|
|
1585
1585
|
? buildWidgetLines(jobs, theme, width, true, frame)
|
|
1586
1586
|
: jobs.length === 1
|
|
@@ -1746,8 +1746,9 @@ function workflowRowStateLabel(row: WorkflowChatProgressRow, theme: Theme): stri
|
|
|
1746
1746
|
}
|
|
1747
1747
|
|
|
1748
1748
|
function workflowOverallState(rows: WorkflowChatProgressRow[], hasTerminalValue: boolean, isError?: boolean): "running" | "complete" | "failed" | "paused" {
|
|
1749
|
-
if (
|
|
1749
|
+
if (rows.some((row) => row.state === "failed")) return "failed";
|
|
1750
1750
|
if (rows.some((row) => row.state === "detached")) return "paused";
|
|
1751
|
+
if (isError) return "failed";
|
|
1751
1752
|
if ((rows.length > 0 && rows.every((row) => row.state === "complete")) || hasTerminalValue) return "complete";
|
|
1752
1753
|
return "running";
|
|
1753
1754
|
}
|
|
@@ -1948,7 +1949,7 @@ export function renderSubagentResult(
|
|
|
1948
1949
|
const layout = resolveMainWindowRenderLayout(rendererConfig);
|
|
1949
1950
|
const compact = (component: Component): Component => capCompactMainWindowResult(component, layout, theme, !options.expanded);
|
|
1950
1951
|
const d = result.details;
|
|
1951
|
-
if (d?.mode === "workflow" && d.chatProgress?.mode === "live-card" && !result.isError
|
|
1952
|
+
if (d?.mode === "workflow" && d.chatProgress?.mode === "live-card" && d.workflow?.value === undefined && (!result.isError || (d.workflow?.trace.length ?? 0) > 0)) {
|
|
1952
1953
|
return compact(renderWorkflowChatProgress(d, result, theme, options.expanded ? resolveMainWindowRenderLayout() : layout, frame));
|
|
1953
1954
|
}
|
|
1954
1955
|
if (!d || !d.results.length) {
|
|
@@ -3,7 +3,6 @@ import { convertToLlm, type ExtensionContext } from "@earendil-works/pi-coding-a
|
|
|
3
3
|
import { streamSimple } from "@earendil-works/pi-ai/compat";
|
|
4
4
|
import { Type, type Static } from "typebox";
|
|
5
5
|
import { appendPermissionAudit, permissionArgsPreview } from "../runs/shared/permissions.ts";
|
|
6
|
-
import { agentStreamOptions } from "../shared/agent-stream-options.ts";
|
|
7
6
|
import { decodeChildWatchdogConfig } from "./child-status.ts";
|
|
8
7
|
import { childResolvedConfig } from "./register-child.ts";
|
|
9
8
|
import { resolveWatchdogReviewModel } from "./review.ts";
|
|
@@ -45,7 +44,11 @@ export function createWatchdogPermissionArbiter(options: WatchdogPermissionArbit
|
|
|
45
44
|
const createdAt = Date.now();
|
|
46
45
|
const auditBase = { type: "permission.request", createdAt, toolName: request.toolName, preview, matchedRule: "ask", decisionSource: "watchdog" };
|
|
47
46
|
appendPermissionAudit(request.auditPath, auditBase);
|
|
47
|
+
let completed = false;
|
|
48
48
|
const finish = (approved: boolean, reason: string, decision: string): WatchdogPermissionResult => {
|
|
49
|
+
const concise = conciseReason(reason);
|
|
50
|
+
if (completed) return { approved, reason: concise, source: "watchdog" };
|
|
51
|
+
completed = true;
|
|
49
52
|
appendPermissionAudit(request.auditPath, {
|
|
50
53
|
type: "permission.decision",
|
|
51
54
|
createdAt: Date.now(),
|
|
@@ -54,9 +57,9 @@ export function createWatchdogPermissionArbiter(options: WatchdogPermissionArbit
|
|
|
54
57
|
decision,
|
|
55
58
|
approved,
|
|
56
59
|
decisionSource: "watchdog",
|
|
57
|
-
reason:
|
|
60
|
+
reason: concise,
|
|
58
61
|
});
|
|
59
|
-
return { approved, reason:
|
|
62
|
+
return { approved, reason: concise, source: "watchdog" };
|
|
60
63
|
};
|
|
61
64
|
|
|
62
65
|
let childConfig;
|
|
@@ -84,61 +87,65 @@ export function createWatchdogPermissionArbiter(options: WatchdogPermissionArbit
|
|
|
84
87
|
|
|
85
88
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
86
89
|
let agent: Agent | undefined;
|
|
90
|
+
let abort: (() => void) | undefined;
|
|
87
91
|
try {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return finish(approved, decision.reason, decision.decision);
|
|
92
|
+
const run = async (): Promise<WatchdogPermissionResult> => {
|
|
93
|
+
const config = childResolvedConfig(childConfig);
|
|
94
|
+
const selection = await resolveWatchdogReviewModel(request.ctx, config);
|
|
95
|
+
const auth = selection.auth;
|
|
96
|
+
const registeredProvider = (request.ctx.modelRegistry as {
|
|
97
|
+
getRegisteredProviderConfig?: (provider: string) => { api?: string; streamSimple?: StreamFn } | undefined;
|
|
98
|
+
}).getRegisteredProviderConfig?.(selection.model.provider);
|
|
99
|
+
const baseStreamFn = options.streamFn ?? (registeredProvider?.streamSimple && registeredProvider.api === selection.model.api
|
|
100
|
+
? registeredProvider.streamSimple
|
|
101
|
+
: streamSimple);
|
|
102
|
+
const streamFn: StreamFn = (model, context, streamOptions) => baseStreamFn(model, context, {
|
|
103
|
+
...streamOptions,
|
|
104
|
+
...(auth.apiKey ? { apiKey: auth.apiKey } : {}),
|
|
105
|
+
env: auth.env || streamOptions?.env ? { ...(auth.env ?? {}), ...(streamOptions?.env ?? {}) } : undefined,
|
|
106
|
+
headers: { ...(streamOptions?.headers ?? {}), ...(auth.headers ?? {}) },
|
|
107
|
+
});
|
|
108
|
+
agent = new Agent({
|
|
109
|
+
initialState: {
|
|
110
|
+
systemPrompt: [
|
|
111
|
+
"You are the pi-subagents watchdog permission arbiter.",
|
|
112
|
+
"Decide only whether this exact non-bash child tool call should proceed.",
|
|
113
|
+
"Call watchdog_permission_decision exactly once with approve or deny and a concise reason.",
|
|
114
|
+
"Deny when uncertain. Do not produce freeform advice or ask the parent orchestrator.",
|
|
115
|
+
].join("\n"),
|
|
116
|
+
model: selection.model,
|
|
117
|
+
thinkingLevel: selection.thinkingLevel,
|
|
118
|
+
tools: [tool],
|
|
119
|
+
},
|
|
120
|
+
convertToLlm,
|
|
121
|
+
streamFn: streamFn,
|
|
122
|
+
getApiKey: (providerName) => providerName === selection.model.provider ? auth.apiKey : undefined,
|
|
123
|
+
beforeToolCall: async ({ toolCall }) => toolCall.name === tool.name ? undefined : { block: true, reason: `Permission arbiter tool '${toolCall.name}' is not allowed.` },
|
|
124
|
+
toolExecution: "sequential",
|
|
125
|
+
});
|
|
126
|
+
await agent.prompt(`Tool: ${request.toolName}\nRedacted arguments: ${preview}`);
|
|
127
|
+
if (!decision) return finish(false, "Watchdog permission arbiter returned no decision.", "malformed");
|
|
128
|
+
const approved = decision.decision === "approve";
|
|
129
|
+
return finish(approved, decision.reason, decision.decision);
|
|
130
|
+
};
|
|
131
|
+
return await Promise.race([
|
|
132
|
+
run(),
|
|
133
|
+
new Promise<WatchdogPermissionResult>((resolve) => { timeout = setTimeout(() => { agent?.abort(); resolve(finish(false, "Watchdog permission decision timed out.", "timeout")); }, childConfig.agentEndTimeoutMs); }),
|
|
134
|
+
new Promise<WatchdogPermissionResult>((resolve) => {
|
|
135
|
+
abort = () => { agent?.abort(); resolve(finish(false, "Watchdog permission decision was cancelled.", "cancelled")); };
|
|
136
|
+
request.signal?.addEventListener("abort", abort, { once: true });
|
|
137
|
+
request.ctx.signal?.addEventListener("abort", abort, { once: true });
|
|
138
|
+
}),
|
|
139
|
+
]);
|
|
137
140
|
} catch (error) {
|
|
138
141
|
const reason = error instanceof Error ? error.message : String(error);
|
|
139
142
|
return finish(false, `Watchdog permission arbiter failed closed: ${reason}`, reason.includes("timed out") ? "timeout" : "error");
|
|
140
143
|
} finally {
|
|
141
144
|
if (timeout) clearTimeout(timeout);
|
|
145
|
+
if (abort) {
|
|
146
|
+
request.signal?.removeEventListener("abort", abort);
|
|
147
|
+
request.ctx.signal?.removeEventListener("abort", abort);
|
|
148
|
+
}
|
|
142
149
|
}
|
|
143
150
|
};
|
|
144
151
|
}
|