@xzzpig/pi-subagents 0.1.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 +1621 -0
- package/LICENSE +21 -0
- package/README.md +118 -0
- package/agents/delegate.md +14 -0
- package/agents/oracle.md +74 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/worker.md +58 -0
- package/index.ts +1 -0
- package/inspector-runner.mjs +11 -0
- package/install.mjs +93 -0
- package/package.json +105 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +43 -0
- package/skills/pi-subagents/SKILL.md +41 -0
- package/skills/pi-subagents/references/constraints-and-recipes.md +251 -0
- package/skills/pi-subagents/references/execution-controls.md +428 -0
- package/skills/pi-subagents/references/management-authoring-rpc.md +161 -0
- package/skills/pi-subagents/references/prompting-and-roles.md +242 -0
- package/src/agents/agent-management.ts +1256 -0
- package/src/agents/agent-memory.ts +254 -0
- package/src/agents/agent-refinements.ts +624 -0
- package/src/agents/agent-scope.ts +6 -0
- package/src/agents/agent-selection.ts +25 -0
- package/src/agents/agent-serializer.ts +163 -0
- package/src/agents/agents.ts +1889 -0
- package/src/agents/chain-serializer.ts +280 -0
- package/src/agents/frontmatter.ts +149 -0
- package/src/agents/identity.ts +30 -0
- package/src/agents/proactive-skills.ts +194 -0
- package/src/agents/skills.ts +761 -0
- package/src/api/background-work.ts +197 -0
- package/src/api/capability-ceiling.ts +17 -0
- package/src/api/control-channel.ts +4 -0
- package/src/api/delegation.ts +119 -0
- package/src/api/external-runs.ts +129 -0
- package/src/api/intercom-bridge.ts +3 -0
- package/src/api/pi-args.ts +5 -0
- package/src/api/preflight.ts +403 -0
- package/src/api/shared-types.ts +19 -0
- package/src/extension/config.ts +76 -0
- package/src/extension/control-notices.ts +58 -0
- package/src/extension/doctor.ts +230 -0
- package/src/extension/fanout-child.ts +201 -0
- package/src/extension/index.ts +805 -0
- package/src/extension/public-execution.ts +71 -0
- package/src/extension/rpc.ts +653 -0
- package/src/extension/schemas.ts +375 -0
- package/src/extension/steering-notices.ts +35 -0
- package/src/extension/tool-description.ts +164 -0
- package/src/inspectors/herdr/actions.ts +229 -0
- package/src/inspectors/herdr/client.ts +130 -0
- package/src/inspectors/herdr/inspector-runner.ts +141 -0
- package/src/inspectors/herdr/project-panes.ts +154 -0
- package/src/integrations/herdr-status.ts +330 -0
- package/src/intercom/intercom-bridge.ts +187 -0
- package/src/intercom/native-supervisor-channel.ts +715 -0
- package/src/intercom/result-intercom.ts +421 -0
- package/src/missions/actions.ts +410 -0
- package/src/missions/goal-driver.ts +162 -0
- package/src/missions/lifecycle.ts +346 -0
- package/src/missions/store.ts +507 -0
- package/src/missions/types.ts +157 -0
- package/src/missions/workflow-state.ts +250 -0
- package/src/policy/authority.ts +46 -0
- package/src/profiles/profiles.ts +661 -0
- package/src/runs/background/async-execution.ts +1574 -0
- package/src/runs/background/async-job-tracker.ts +511 -0
- package/src/runs/background/async-resume.ts +539 -0
- package/src/runs/background/async-status.ts +542 -0
- package/src/runs/background/auto-drain.ts +67 -0
- package/src/runs/background/chain-append.ts +328 -0
- package/src/runs/background/chain-root-attachment.ts +199 -0
- package/src/runs/background/completion-batcher.ts +168 -0
- package/src/runs/background/completion-dedupe.ts +54 -0
- package/src/runs/background/control-channel.ts +692 -0
- package/src/runs/background/fleet-view.ts +540 -0
- package/src/runs/background/notify.ts +332 -0
- package/src/runs/background/parallel-groups.ts +45 -0
- package/src/runs/background/process-terminal.ts +280 -0
- package/src/runs/background/result-watcher.ts +414 -0
- package/src/runs/background/retained-children.ts +68 -0
- package/src/runs/background/run-id-resolver.ts +167 -0
- package/src/runs/background/run-status.ts +504 -0
- package/src/runs/background/scheduled-runs.ts +753 -0
- package/src/runs/background/stale-run-reconciler.ts +396 -0
- package/src/runs/background/steering.ts +239 -0
- package/src/runs/background/subagent-runner.ts +4718 -0
- package/src/runs/background/subagent-wait.ts +661 -0
- package/src/runs/background/top-level-async.ts +14 -0
- package/src/runs/background/wait-completions.ts +112 -0
- package/src/runs/background/wait-config.ts +36 -0
- package/src/runs/background/wait-subscriptions.ts +253 -0
- package/src/runs/background/wait-tool.ts +34 -0
- package/src/runs/foreground/async-steering-action.ts +256 -0
- package/src/runs/foreground/async-stop-action.ts +65 -0
- package/src/runs/foreground/chain-clarify.ts +1354 -0
- package/src/runs/foreground/chain-execution.ts +1527 -0
- package/src/runs/foreground/execution.ts +1868 -0
- package/src/runs/foreground/foreground-control.ts +132 -0
- package/src/runs/foreground/subagent-executor.ts +5532 -0
- package/src/runs/shared/acceptance.ts +1365 -0
- package/src/runs/shared/agent-contract.ts +38 -0
- package/src/runs/shared/capability-ceiling.ts +209 -0
- package/src/runs/shared/chain-outputs.ts +118 -0
- package/src/runs/shared/child-protocol.ts +401 -0
- package/src/runs/shared/completion-guard.ts +95 -0
- package/src/runs/shared/context-mode.ts +44 -0
- package/src/runs/shared/dynamic-fanout.ts +297 -0
- package/src/runs/shared/external-cli-runner.ts +130 -0
- package/src/runs/shared/long-running-guard.ts +220 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +406 -0
- package/src/runs/shared/model-fallback.ts +336 -0
- package/src/runs/shared/model-scope.ts +128 -0
- package/src/runs/shared/nested-events.ts +1076 -0
- package/src/runs/shared/nested-path.ts +52 -0
- package/src/runs/shared/nested-render.ts +123 -0
- package/src/runs/shared/parallel-handoff.ts +238 -0
- package/src/runs/shared/parallel-utils.ts +256 -0
- package/src/runs/shared/permissions.ts +95 -0
- package/src/runs/shared/pi-args.ts +808 -0
- package/src/runs/shared/pi-spawn.ts +163 -0
- package/src/runs/shared/process-signal.ts +19 -0
- package/src/runs/shared/run-history.ts +181 -0
- package/src/runs/shared/runtime-acknowledged-extensions.ts +71 -0
- package/src/runs/shared/session-lease.ts +299 -0
- package/src/runs/shared/single-output.ts +235 -0
- package/src/runs/shared/spawn-budget.ts +128 -0
- package/src/runs/shared/structured-output.ts +182 -0
- package/src/runs/shared/subagent-control.ts +231 -0
- package/src/runs/shared/subagent-prompt-runtime.ts +588 -0
- package/src/runs/shared/subagent-startup-retry.ts +104 -0
- package/src/runs/shared/task-intent.ts +181 -0
- package/src/runs/shared/tool-availability.ts +83 -0
- package/src/runs/shared/tool-budget.ts +80 -0
- package/src/runs/shared/turn-budget.ts +98 -0
- package/src/runs/shared/usage-budget.ts +65 -0
- package/src/runs/shared/workflow-graph.ts +231 -0
- package/src/runs/shared/worktree.ts +759 -0
- package/src/shared/accessible-dir.ts +47 -0
- package/src/shared/artifacts.ts +285 -0
- package/src/shared/atomic-json.ts +61 -0
- package/src/shared/child-transcript.ts +264 -0
- package/src/shared/file-coalescer.ts +40 -0
- package/src/shared/file-system-retry.ts +47 -0
- package/src/shared/fork-context.ts +213 -0
- package/src/shared/formatters.ts +133 -0
- package/src/shared/jsonl-writer.ts +81 -0
- package/src/shared/launch-contract.ts +124 -0
- package/src/shared/model-info.ts +81 -0
- package/src/shared/post-exit-stdio-guard.ts +85 -0
- package/src/shared/session-identity.ts +10 -0
- package/src/shared/session-tokens.ts +44 -0
- package/src/shared/settings.ts +501 -0
- package/src/shared/status-format.ts +55 -0
- package/src/shared/types.ts +2069 -0
- package/src/shared/utils.ts +613 -0
- package/src/slash/delegation-adapters.ts +434 -0
- package/src/slash/delegation-json.ts +108 -0
- package/src/slash/delegation-request.ts +166 -0
- package/src/slash/prompt-template-bridge.ts +383 -0
- package/src/slash/prompt-workflows.ts +300 -0
- package/src/slash/selector.ts +147 -0
- package/src/slash/slash-bridge.ts +192 -0
- package/src/slash/slash-commands.ts +994 -0
- package/src/slash/slash-live-state.ts +296 -0
- package/src/slash/subagents-admin.ts +432 -0
- package/src/tui/fleet-status.ts +564 -0
- package/src/tui/fleet-transcript.ts +577 -0
- package/src/tui/fleet.ts +879 -0
- package/src/tui/render-helpers.ts +80 -0
- package/src/tui/render.ts +2029 -0
- package/src/types/pi-runtime-compat.d.ts +8 -0
- package/src/watchdog/change-signature.ts +220 -0
- package/src/watchdog/child-status.ts +205 -0
- package/src/watchdog/emission-guard.ts +123 -0
- package/src/watchdog/lsp-diagnostics.ts +537 -0
- package/src/watchdog/model-selection.ts +167 -0
- package/src/watchdog/permission-arbiter.ts +145 -0
- package/src/watchdog/register-child.ts +117 -0
- package/src/watchdog/register-main.ts +440 -0
- package/src/watchdog/render.ts +54 -0
- package/src/watchdog/review.ts +302 -0
- package/src/watchdog/runtime.ts +868 -0
- package/src/watchdog/scope.ts +62 -0
- package/src/watchdog/settings.ts +568 -0
- package/src/watchdog/tool-actions.ts +155 -0
- package/src/watchdog/turn-delta.ts +161 -0
- package/src/watchdog/types.ts +198 -0
- package/src/watchdog/warning-format.ts +73 -0
- package/src/workflows/chat-progress.ts +140 -0
- package/src/workflows/scripted-workflow.ts +506 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Agent, type AgentTool, type StreamFn } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { convertToLlm, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { streamSimple } from "@earendil-works/pi-ai/compat";
|
|
4
|
+
import { Type, type Static } from "typebox";
|
|
5
|
+
import { appendPermissionAudit, permissionArgsPreview } from "../runs/shared/permissions.ts";
|
|
6
|
+
import { decodeChildWatchdogConfig } from "./child-status.ts";
|
|
7
|
+
import { childResolvedConfig } from "./register-child.ts";
|
|
8
|
+
import { resolveWatchdogReviewModel } from "./review.ts";
|
|
9
|
+
|
|
10
|
+
const PermissionDecisionParams = Type.Object({
|
|
11
|
+
decision: Type.String({ enum: ["approve", "deny"] }),
|
|
12
|
+
reason: Type.String({ description: "One concise reason for this exact decision." }),
|
|
13
|
+
}, { additionalProperties: false });
|
|
14
|
+
|
|
15
|
+
type PermissionDecisionParams = Static<typeof PermissionDecisionParams>;
|
|
16
|
+
|
|
17
|
+
export interface WatchdogPermissionResult {
|
|
18
|
+
approved: boolean;
|
|
19
|
+
reason: string;
|
|
20
|
+
source: "watchdog";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface WatchdogPermissionRequest {
|
|
24
|
+
ctx: ExtensionContext;
|
|
25
|
+
toolName: string;
|
|
26
|
+
args: unknown;
|
|
27
|
+
rawWatchdogConfig?: string;
|
|
28
|
+
auditPath?: string;
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface WatchdogPermissionArbiterOptions {
|
|
33
|
+
streamFn?: StreamFn;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function conciseReason(value: string): string {
|
|
37
|
+
const trimmed = value.trim();
|
|
38
|
+
return trimmed ? trimmed.slice(0, 500) : "Watchdog returned an empty reason.";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createWatchdogPermissionArbiter(options: WatchdogPermissionArbiterOptions = {}) {
|
|
42
|
+
return async (request: WatchdogPermissionRequest): Promise<WatchdogPermissionResult> => {
|
|
43
|
+
const preview = permissionArgsPreview(request.args);
|
|
44
|
+
const createdAt = Date.now();
|
|
45
|
+
const auditBase = { type: "permission.request", createdAt, toolName: request.toolName, preview, matchedRule: "ask", decisionSource: "watchdog" };
|
|
46
|
+
appendPermissionAudit(request.auditPath, auditBase);
|
|
47
|
+
const finish = (approved: boolean, reason: string, decision: string): WatchdogPermissionResult => {
|
|
48
|
+
appendPermissionAudit(request.auditPath, {
|
|
49
|
+
type: "permission.decision",
|
|
50
|
+
createdAt: Date.now(),
|
|
51
|
+
requestCreatedAt: createdAt,
|
|
52
|
+
toolName: request.toolName,
|
|
53
|
+
decision,
|
|
54
|
+
approved,
|
|
55
|
+
decisionSource: "watchdog",
|
|
56
|
+
reason: conciseReason(reason),
|
|
57
|
+
});
|
|
58
|
+
return { approved, reason: conciseReason(reason), source: "watchdog" };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
let childConfig;
|
|
62
|
+
try {
|
|
63
|
+
childConfig = decodeChildWatchdogConfig(request.rawWatchdogConfig);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
66
|
+
return finish(false, `Watchdog permission arbiter configuration is invalid: ${reason}`, "unavailable");
|
|
67
|
+
}
|
|
68
|
+
if (!childConfig) return finish(false, "Watchdog permission arbiter is unavailable because the child watchdog is disabled.", "unavailable");
|
|
69
|
+
if (request.signal?.aborted || request.ctx.signal?.aborted) return finish(false, "Watchdog permission decision was cancelled.", "cancelled");
|
|
70
|
+
|
|
71
|
+
let decision: PermissionDecisionParams | undefined;
|
|
72
|
+
const tool: AgentTool<typeof PermissionDecisionParams, { recorded: boolean }> = {
|
|
73
|
+
name: "watchdog_permission_decision",
|
|
74
|
+
label: "Watchdog permission decision",
|
|
75
|
+
description: "Approve or deny this exact child tool call. Call exactly once.",
|
|
76
|
+
parameters: PermissionDecisionParams,
|
|
77
|
+
executionMode: "sequential",
|
|
78
|
+
async execute(_toolCallId, params) {
|
|
79
|
+
if (!decision) decision = params;
|
|
80
|
+
return { content: [{ type: "text", text: "Permission decision recorded." }], details: { recorded: true } };
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
85
|
+
let agent: Agent | undefined;
|
|
86
|
+
try {
|
|
87
|
+
const config = childResolvedConfig(childConfig);
|
|
88
|
+
const selection = await resolveWatchdogReviewModel(request.ctx, config);
|
|
89
|
+
const auth = selection.auth;
|
|
90
|
+
const registeredProvider = (request.ctx.modelRegistry as {
|
|
91
|
+
getRegisteredProviderConfig?: (provider: string) => { api?: string; streamSimple?: StreamFn } | undefined;
|
|
92
|
+
}).getRegisteredProviderConfig?.(selection.model.provider);
|
|
93
|
+
const baseStreamFn = options.streamFn ?? (registeredProvider?.streamSimple && registeredProvider.api === selection.model.api
|
|
94
|
+
? registeredProvider.streamSimple
|
|
95
|
+
: streamSimple);
|
|
96
|
+
const streamFn: StreamFn = (model, context, streamOptions) => baseStreamFn(model, context, {
|
|
97
|
+
...streamOptions,
|
|
98
|
+
...(auth.apiKey ? { apiKey: auth.apiKey } : {}),
|
|
99
|
+
env: auth.env || streamOptions?.env ? { ...(auth.env ?? {}), ...(streamOptions?.env ?? {}) } : undefined,
|
|
100
|
+
headers: { ...(streamOptions?.headers ?? {}), ...(auth.headers ?? {}) },
|
|
101
|
+
});
|
|
102
|
+
agent = new Agent({
|
|
103
|
+
initialState: {
|
|
104
|
+
systemPrompt: [
|
|
105
|
+
"You are the pi-subagents watchdog permission arbiter.",
|
|
106
|
+
"Decide only whether this exact non-bash child tool call should proceed.",
|
|
107
|
+
"Call watchdog_permission_decision exactly once with approve or deny and a concise reason.",
|
|
108
|
+
"Deny when uncertain. Do not produce freeform advice or ask the parent orchestrator.",
|
|
109
|
+
].join("\n"),
|
|
110
|
+
model: selection.model,
|
|
111
|
+
thinkingLevel: selection.thinkingLevel,
|
|
112
|
+
tools: [tool],
|
|
113
|
+
},
|
|
114
|
+
convertToLlm,
|
|
115
|
+
streamFn: streamFn,
|
|
116
|
+
getApiKey: (providerName) => providerName === selection.model.provider ? auth.apiKey : undefined,
|
|
117
|
+
beforeToolCall: async ({ toolCall }) => toolCall.name === tool.name ? undefined : { block: true, reason: `Permission arbiter tool '${toolCall.name}' is not allowed.` },
|
|
118
|
+
toolExecution: "sequential",
|
|
119
|
+
});
|
|
120
|
+
const abort = () => agent?.abort();
|
|
121
|
+
request.signal?.addEventListener("abort", abort, { once: true });
|
|
122
|
+
request.ctx.signal?.addEventListener("abort", abort, { once: true });
|
|
123
|
+
try {
|
|
124
|
+
const prompt = `Tool: ${request.toolName}\nRedacted arguments: ${preview}`;
|
|
125
|
+
await Promise.race([
|
|
126
|
+
agent.prompt(prompt),
|
|
127
|
+
new Promise<never>((_, reject) => { timeout = setTimeout(() => { agent?.abort(); reject(new Error("Watchdog permission decision timed out.")); }, childConfig.agentEndTimeoutMs); }),
|
|
128
|
+
]);
|
|
129
|
+
} finally {
|
|
130
|
+
request.signal?.removeEventListener("abort", abort);
|
|
131
|
+
request.ctx.signal?.removeEventListener("abort", abort);
|
|
132
|
+
}
|
|
133
|
+
if (!decision) return finish(false, "Watchdog permission arbiter returned no decision.", "malformed");
|
|
134
|
+
const approved = decision.decision === "approve";
|
|
135
|
+
return finish(approved, decision.reason, decision.decision);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
138
|
+
return finish(false, `Watchdog permission arbiter failed closed: ${reason}`, reason.includes("timed out") ? "timeout" : "error");
|
|
139
|
+
} finally {
|
|
140
|
+
if (timeout) clearTimeout(timeout);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export const requestWatchdogPermission = createWatchdogPermissionArbiter();
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { MainWatchdogRuntime } from "./runtime.ts";
|
|
3
|
+
import { createMainWatchdogReview } from "./review.ts";
|
|
4
|
+
import { DEFAULT_WATCHDOG_CONFIG } from "./settings.ts";
|
|
5
|
+
import { createWatchdogWarningMessage } from "./warning-format.ts";
|
|
6
|
+
import {
|
|
7
|
+
CHILD_WATCHDOG_CONFIG_ENV,
|
|
8
|
+
CHILD_WATCHDOG_STATUS_EVENT,
|
|
9
|
+
decodeChildWatchdogConfig,
|
|
10
|
+
type ChildWatchdogConfig,
|
|
11
|
+
type ChildWatchdogPhase,
|
|
12
|
+
} from "./child-status.ts";
|
|
13
|
+
import type { ResolvedWatchdogConfig, WatchdogWarningDetails } from "./types.ts";
|
|
14
|
+
|
|
15
|
+
export function childResolvedConfig(config: ChildWatchdogConfig): ResolvedWatchdogConfig {
|
|
16
|
+
return {
|
|
17
|
+
...DEFAULT_WATCHDOG_CONFIG,
|
|
18
|
+
enabled: true,
|
|
19
|
+
agentEndTimeoutMs: config.agentEndTimeoutMs,
|
|
20
|
+
maxWarnings: config.maxWarnings,
|
|
21
|
+
main: {
|
|
22
|
+
enabled: true,
|
|
23
|
+
...(config.model ? { model: config.model } : {}),
|
|
24
|
+
...(config.thinking !== undefined ? { thinking: config.thinking } : {}),
|
|
25
|
+
},
|
|
26
|
+
autoFollow: {
|
|
27
|
+
blockers: config.autoFollowBlockers,
|
|
28
|
+
maxAttempts: config.autoFollowMaxAttempts,
|
|
29
|
+
stalemateRepeats: config.stalemateRepeats,
|
|
30
|
+
},
|
|
31
|
+
children: {
|
|
32
|
+
...DEFAULT_WATCHDOG_CONFIG.children,
|
|
33
|
+
watchdogTailTimeoutMs: config.watchdogTailTimeoutMs,
|
|
34
|
+
},
|
|
35
|
+
lsp: { ...config.lsp },
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function childWarningDetails(details: WatchdogWarningDetails, config: ChildWatchdogConfig): WatchdogWarningDetails {
|
|
40
|
+
return {
|
|
41
|
+
...details,
|
|
42
|
+
source: details.source === "lsp" ? "lsp" : "child",
|
|
43
|
+
...(config.agent ? { agent: config.agent } : {}),
|
|
44
|
+
...(config.runId ? { runId: config.runId } : {}),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function writeStatus(event: unknown): void {
|
|
49
|
+
try {
|
|
50
|
+
process.stdout.write(`${JSON.stringify(event)}\n`);
|
|
51
|
+
} catch {
|
|
52
|
+
// Child watchdog status is advisory; stdout failures are handled by the child process itself.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function registerChildWatchdog(pi: ExtensionAPI, rawConfig = process.env[CHILD_WATCHDOG_CONFIG_ENV]): MainWatchdogRuntime | undefined {
|
|
57
|
+
const childConfig = decodeChildWatchdogConfig(rawConfig);
|
|
58
|
+
if (!childConfig?.enabled) return undefined;
|
|
59
|
+
let currentContext: ExtensionContext | undefined;
|
|
60
|
+
let seq = 0;
|
|
61
|
+
const emitStatus = (phase: ChildWatchdogPhase, followUpPending = false, reason?: string): void => {
|
|
62
|
+
writeStatus({
|
|
63
|
+
type: CHILD_WATCHDOG_STATUS_EVENT,
|
|
64
|
+
...(childConfig.runId ? { runId: childConfig.runId } : {}),
|
|
65
|
+
...(childConfig.agent ? { agent: childConfig.agent } : {}),
|
|
66
|
+
...(childConfig.childIndex !== undefined ? { childIndex: childConfig.childIndex, stepIndex: childConfig.childIndex } : {}),
|
|
67
|
+
seq: ++seq,
|
|
68
|
+
phase,
|
|
69
|
+
ts: Date.now(),
|
|
70
|
+
followUpPending,
|
|
71
|
+
...(reason ? { reason } : {}),
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
const resolved = childResolvedConfig(childConfig);
|
|
75
|
+
const runtime = new MainWatchdogRuntime({
|
|
76
|
+
resolveConfig: () => ({ ok: true, config: resolved, errors: [], sources: [{ scope: "session", exists: true }] }),
|
|
77
|
+
review: createMainWatchdogReview(() => currentContext, { getThinkingLevel: () => pi.getThinkingLevel() }),
|
|
78
|
+
reviewDescription: "child model review",
|
|
79
|
+
reviewChangesOnly: true,
|
|
80
|
+
displayWarning: (details) => {
|
|
81
|
+
const childDetails = childWarningDetails(details, childConfig);
|
|
82
|
+
pi.sendMessage(createWatchdogWarningMessage(childDetails, { display: true, details: childDetails }));
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const rememberContext = (ctx: ExtensionContext) => {
|
|
86
|
+
currentContext = ctx;
|
|
87
|
+
};
|
|
88
|
+
const onRuntimeEvent = pi.on as unknown as (event: string, handler: (event: unknown, ctx: ExtensionContext) => unknown) => void;
|
|
89
|
+
onRuntimeEvent("session_start", (_event, ctx) => {
|
|
90
|
+
rememberContext(ctx);
|
|
91
|
+
runtime.bindSession(ctx);
|
|
92
|
+
emitStatus("idle");
|
|
93
|
+
});
|
|
94
|
+
onRuntimeEvent("before_agent_start", (event, ctx) => {
|
|
95
|
+
rememberContext(ctx);
|
|
96
|
+
runtime.handleBeforeAgentStart(event, ctx);
|
|
97
|
+
});
|
|
98
|
+
onRuntimeEvent("turn_end", (event, ctx) => {
|
|
99
|
+
rememberContext(ctx);
|
|
100
|
+
runtime.handleTurnEnd(event, ctx);
|
|
101
|
+
});
|
|
102
|
+
onRuntimeEvent("agent_end", async (event, ctx) => {
|
|
103
|
+
rememberContext(ctx);
|
|
104
|
+
emitStatus("reviewing");
|
|
105
|
+
await runtime.handleAgentEnd(event, ctx);
|
|
106
|
+
const snapshot = runtime.getSnapshot(ctx.cwd);
|
|
107
|
+
if (snapshot.status === "failed") emitStatus("failed", false, snapshot.lastError);
|
|
108
|
+
else if (snapshot.status === "stale") emitStatus("stale", false, "review stale");
|
|
109
|
+
else emitStatus("idle");
|
|
110
|
+
});
|
|
111
|
+
onRuntimeEvent("session_shutdown", () => {
|
|
112
|
+
currentContext = undefined;
|
|
113
|
+
runtime.dispose();
|
|
114
|
+
emitStatus("idle");
|
|
115
|
+
});
|
|
116
|
+
return runtime;
|
|
117
|
+
}
|