@pi-agents/orchid 0.1.0-beta.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 +41 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/agents/AGENTS-MANIFEST.md +42 -0
- package/agents/brain.md +42 -0
- package/agents/context-builder.md +46 -0
- package/agents/delegate.md +12 -0
- package/agents/dev-1.md +42 -0
- package/agents/oracle.md +73 -0
- package/agents/planner.md +55 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/tester.md +45 -0
- package/agents/worker.md +55 -0
- package/extensions/ralph.ts +1 -0
- package/extensions/reviewer-extension.ts +125 -0
- package/extensions/task-orchestrator.ts +28 -0
- package/package.json +63 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-context-build.md +53 -0
- package/prompts/parallel-handoff-plan.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +41 -0
- package/skills/orchid/SKILL.md +214 -0
- package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
- package/skills/orchid/orchid-converge/SKILL.md +124 -0
- package/skills/orchid/orchid-decompose/SKILL.md +201 -0
- package/skills/orchid/orchid-doctor/SKILL.md +162 -0
- package/skills/orchid/orchid-investigate/SKILL.md +102 -0
- package/skills/orchid/orchid-launch/SKILL.md +147 -0
- package/skills/ralph/SKILL.md +73 -0
- package/skills/subagents/pi-subagents/SKILL.md +813 -0
- package/src/index.ts +7 -0
- package/src/orchestrator/abort.ts +534 -0
- package/src/orchestrator/agent-bridge-extension.ts +1020 -0
- package/src/orchestrator/agent-host.ts +954 -0
- package/src/orchestrator/cleanup.ts +776 -0
- package/src/orchestrator/config-loader.ts +1412 -0
- package/src/orchestrator/config-schema.ts +690 -0
- package/src/orchestrator/config.ts +81 -0
- package/src/orchestrator/context-window.ts +66 -0
- package/src/orchestrator/diagnostic-reports.ts +475 -0
- package/src/orchestrator/diagnostics.ts +394 -0
- package/src/orchestrator/discovery.ts +1833 -0
- package/src/orchestrator/engine-worker.ts +415 -0
- package/src/orchestrator/engine.ts +5940 -0
- package/src/orchestrator/execution.ts +3104 -0
- package/src/orchestrator/extension.ts +5934 -0
- package/src/orchestrator/formatting.ts +785 -0
- package/src/orchestrator/git.ts +88 -0
- package/src/orchestrator/index.ts +28 -0
- package/src/orchestrator/lane-runner.ts +1787 -0
- package/src/orchestrator/mailbox.ts +780 -0
- package/src/orchestrator/merge.ts +3414 -0
- package/src/orchestrator/messages.ts +1062 -0
- package/src/orchestrator/migrations.ts +278 -0
- package/src/orchestrator/naming.ts +117 -0
- package/src/orchestrator/path-resolver.ts +275 -0
- package/src/orchestrator/persistence.ts +2625 -0
- package/src/orchestrator/process-registry.ts +452 -0
- package/src/orchestrator/quality-gate.ts +1085 -0
- package/src/orchestrator/resume.ts +3488 -0
- package/src/orchestrator/sessions.ts +57 -0
- package/src/orchestrator/settings-loader.ts +136 -0
- package/src/orchestrator/settings-tui.ts +2208 -0
- package/src/orchestrator/sidecar-telemetry.ts +267 -0
- package/src/orchestrator/supervisor.ts +4548 -0
- package/src/orchestrator/task-executor-core.ts +675 -0
- package/src/orchestrator/tmux-compat.ts +37 -0
- package/src/orchestrator/tool-allowlist-constants.ts +37 -0
- package/src/orchestrator/types.ts +4465 -0
- package/src/orchestrator/verification.ts +547 -0
- package/src/orchestrator/waves.ts +1564 -0
- package/src/orchestrator/workspace.ts +707 -0
- package/src/orchestrator/worktree.ts +2725 -0
- package/src/ralph/index.ts +825 -0
- package/src/subagents/agents/agent-management.ts +648 -0
- package/src/subagents/agents/agent-scope.ts +6 -0
- package/src/subagents/agents/agent-selection.ts +23 -0
- package/src/subagents/agents/agent-serializer.ts +86 -0
- package/src/subagents/agents/agents.ts +832 -0
- package/src/subagents/agents/chain-serializer.ts +137 -0
- package/src/subagents/agents/frontmatter.ts +29 -0
- package/src/subagents/agents/identity.ts +30 -0
- package/src/subagents/agents/skills.ts +632 -0
- package/src/subagents/extension/config.ts +16 -0
- package/src/subagents/extension/control-notices.ts +92 -0
- package/src/subagents/extension/doctor.ts +199 -0
- package/src/subagents/extension/fanout-child.ts +170 -0
- package/src/subagents/extension/index.ts +573 -0
- package/src/subagents/extension/schemas.ts +168 -0
- package/src/subagents/intercom/intercom-bridge.ts +379 -0
- package/src/subagents/intercom/result-intercom.ts +377 -0
- package/src/subagents/runs/background/async-execution.ts +712 -0
- package/src/subagents/runs/background/async-job-tracker.ts +310 -0
- package/src/subagents/runs/background/async-resume.ts +345 -0
- package/src/subagents/runs/background/async-status.ts +325 -0
- package/src/subagents/runs/background/completion-dedupe.ts +63 -0
- package/src/subagents/runs/background/notify.ts +108 -0
- package/src/subagents/runs/background/parallel-groups.ts +45 -0
- package/src/subagents/runs/background/result-watcher.ts +307 -0
- package/src/subagents/runs/background/run-id-resolver.ts +83 -0
- package/src/subagents/runs/background/run-status.ts +269 -0
- package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
- package/src/subagents/runs/background/subagent-runner.ts +1808 -0
- package/src/subagents/runs/background/top-level-async.ts +13 -0
- package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
- package/src/subagents/runs/foreground/chain-execution.ts +938 -0
- package/src/subagents/runs/foreground/execution.ts +918 -0
- package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
- package/src/subagents/runs/shared/completion-guard.ts +147 -0
- package/src/subagents/runs/shared/long-running-guard.ts +175 -0
- package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/src/subagents/runs/shared/model-fallback.ts +103 -0
- package/src/subagents/runs/shared/nested-events.ts +819 -0
- package/src/subagents/runs/shared/nested-path.ts +52 -0
- package/src/subagents/runs/shared/nested-render.ts +115 -0
- package/src/subagents/runs/shared/parallel-utils.ts +109 -0
- package/src/subagents/runs/shared/pi-args.ts +220 -0
- package/src/subagents/runs/shared/pi-spawn.ts +115 -0
- package/src/subagents/runs/shared/run-history.ts +60 -0
- package/src/subagents/runs/shared/single-output.ts +164 -0
- package/src/subagents/runs/shared/subagent-control.ts +226 -0
- package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
- package/src/subagents/runs/shared/worktree.ts +577 -0
- package/src/subagents/shared/artifacts.ts +98 -0
- package/src/subagents/shared/atomic-json.ts +16 -0
- package/src/subagents/shared/file-coalescer.ts +40 -0
- package/src/subagents/shared/fork-context.ts +76 -0
- package/src/subagents/shared/formatters.ts +133 -0
- package/src/subagents/shared/jsonl-writer.ts +81 -0
- package/src/subagents/shared/model-info.ts +78 -0
- package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
- package/src/subagents/shared/session-identity.ts +10 -0
- package/src/subagents/shared/session-tokens.ts +44 -0
- package/src/subagents/shared/settings.ts +397 -0
- package/src/subagents/shared/status-format.ts +49 -0
- package/src/subagents/shared/types.ts +822 -0
- package/src/subagents/shared/utils.ts +450 -0
- package/src/subagents/slash/prompt-template-bridge.ts +397 -0
- package/src/subagents/slash/slash-bridge.ts +174 -0
- package/src/subagents/slash/slash-commands.ts +528 -0
- package/src/subagents/slash/slash-live-state.ts +292 -0
- package/src/subagents/tui/render-helpers.ts +80 -0
- package/src/subagents/tui/render.ts +1358 -0
- package/templates/agents/local/supervisor.md +33 -0
- package/templates/agents/local/task-merger.md +27 -0
- package/templates/agents/local/task-reviewer.md +30 -0
- package/templates/agents/local/task-worker.md +34 -0
- package/templates/agents/supervisor-routing.md +92 -0
- package/templates/agents/supervisor.md +229 -0
- package/templates/agents/task-merger.md +214 -0
- package/templates/agents/task-reviewer.md +260 -0
- package/templates/agents/task-worker-segment.md +44 -0
- package/templates/agents/task-worker.md +557 -0
- package/templates/tasks/CONTEXT.md +30 -0
- package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
- package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { controlNotificationKey, formatControlNoticeMessage } from "../runs/shared/subagent-control.ts";
|
|
3
|
+
import type { ControlEvent, SubagentState } from "../shared/types.ts";
|
|
4
|
+
|
|
5
|
+
export const SUBAGENT_CONTROL_MESSAGE_TYPE = "subagent_control_notice";
|
|
6
|
+
|
|
7
|
+
export interface SubagentControlMessageDetails {
|
|
8
|
+
event: ControlEvent;
|
|
9
|
+
source?: "foreground" | "async";
|
|
10
|
+
asyncDir?: string;
|
|
11
|
+
childIntercomTarget?: string;
|
|
12
|
+
noticeText?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function controlNoticeTarget(details: SubagentControlMessageDetails): string | undefined {
|
|
16
|
+
return details.childIntercomTarget;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function formatSubagentControlNotice(details: SubagentControlMessageDetails, content?: string): string {
|
|
20
|
+
return details.noticeText ?? content ?? formatControlNoticeMessage(details.event, controlNoticeTarget(details));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function noticeTimerKey(details: SubagentControlMessageDetails): string {
|
|
24
|
+
const childIntercomTarget = controlNoticeTarget(details);
|
|
25
|
+
return `${details.event.runId}:${controlNotificationKey(details.event, childIntercomTarget)}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function clearPendingForegroundControlNotices(state: SubagentState, runId?: string): void {
|
|
29
|
+
const pending = state.pendingForegroundControlNotices;
|
|
30
|
+
if (!pending) return;
|
|
31
|
+
for (const [key, timer] of pending) {
|
|
32
|
+
if (runId !== undefined && !key.startsWith(`${runId}:`)) continue;
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
pending.delete(key);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function deliverControlNotice(input: {
|
|
39
|
+
pi: Pick<ExtensionAPI, "sendMessage">;
|
|
40
|
+
visibleControlNotices: Set<string>;
|
|
41
|
+
details: SubagentControlMessageDetails;
|
|
42
|
+
}): void {
|
|
43
|
+
const childIntercomTarget = controlNoticeTarget(input.details);
|
|
44
|
+
const key = controlNotificationKey(input.details.event, childIntercomTarget);
|
|
45
|
+
if (input.visibleControlNotices.has(key)) return;
|
|
46
|
+
input.visibleControlNotices.add(key);
|
|
47
|
+
const noticeText = input.details.noticeText ?? formatControlNoticeMessage(input.details.event, childIntercomTarget);
|
|
48
|
+
input.pi.sendMessage(
|
|
49
|
+
{
|
|
50
|
+
customType: SUBAGENT_CONTROL_MESSAGE_TYPE,
|
|
51
|
+
content: noticeText,
|
|
52
|
+
display: true,
|
|
53
|
+
details: { ...input.details, childIntercomTarget, noticeText },
|
|
54
|
+
},
|
|
55
|
+
{ triggerTurn: input.details.source !== "foreground" },
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function isForegroundNoticeStillActionable(state: SubagentState, details: SubagentControlMessageDetails): boolean {
|
|
60
|
+
const control = state.foregroundControls.get(details.event.runId);
|
|
61
|
+
if (!control) return false;
|
|
62
|
+
if (control.currentAgent && control.currentAgent !== details.event.agent) return false;
|
|
63
|
+
if (details.event.index !== undefined && control.currentIndex !== details.event.index) return false;
|
|
64
|
+
return control.currentActivityState === "needs_attention";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function handleSubagentControlNotice(input: {
|
|
68
|
+
pi: Pick<ExtensionAPI, "sendMessage">;
|
|
69
|
+
state: SubagentState;
|
|
70
|
+
visibleControlNotices: Set<string>;
|
|
71
|
+
details: SubagentControlMessageDetails;
|
|
72
|
+
foregroundDelayMs?: number;
|
|
73
|
+
}): void {
|
|
74
|
+
if (!input.details?.event || input.details.event.type === "active_long_running") return;
|
|
75
|
+
if (input.details.source !== "foreground") {
|
|
76
|
+
deliverControlNotice(input);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const pending = input.state.pendingForegroundControlNotices ?? new Map<string, ReturnType<typeof setTimeout>>();
|
|
81
|
+
input.state.pendingForegroundControlNotices = pending;
|
|
82
|
+
const timerKey = noticeTimerKey(input.details);
|
|
83
|
+
const existing = pending.get(timerKey);
|
|
84
|
+
if (existing) clearTimeout(existing);
|
|
85
|
+
const timer = setTimeout(() => {
|
|
86
|
+
pending.delete(timerKey);
|
|
87
|
+
if (!isForegroundNoticeStillActionable(input.state, input.details)) return;
|
|
88
|
+
deliverControlNotice(input);
|
|
89
|
+
}, input.foregroundDelayMs ?? 1000);
|
|
90
|
+
timer.unref?.();
|
|
91
|
+
pending.set(timerKey, timer);
|
|
92
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { discoverAgentsAll, type AgentSource } from "../agents/agents.ts";
|
|
4
|
+
import { isAsyncAvailable } from "../runs/background/async-execution.ts";
|
|
5
|
+
import { diagnoseIntercomBridge, type IntercomBridgeDiagnostic } from "../intercom/intercom-bridge.ts";
|
|
6
|
+
import { discoverAvailableSkills, type SkillSource } from "../agents/skills.ts";
|
|
7
|
+
import {
|
|
8
|
+
ASYNC_DIR,
|
|
9
|
+
CHAIN_RUNS_DIR,
|
|
10
|
+
RESULTS_DIR,
|
|
11
|
+
TEMP_ROOT_DIR,
|
|
12
|
+
type ExtensionConfig,
|
|
13
|
+
type SubagentState,
|
|
14
|
+
} from "../shared/types.ts";
|
|
15
|
+
|
|
16
|
+
interface DoctorPaths {
|
|
17
|
+
tempRootDir: string;
|
|
18
|
+
asyncDir: string;
|
|
19
|
+
resultsDir: string;
|
|
20
|
+
chainRunsDir: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface DoctorDeps {
|
|
24
|
+
isAsyncAvailable: () => boolean;
|
|
25
|
+
discoverAgentsAll: typeof discoverAgentsAll;
|
|
26
|
+
discoverAvailableSkills: typeof discoverAvailableSkills;
|
|
27
|
+
diagnoseIntercomBridge: typeof diagnoseIntercomBridge;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface DoctorReportInput {
|
|
31
|
+
cwd: string;
|
|
32
|
+
config: ExtensionConfig;
|
|
33
|
+
state: SubagentState;
|
|
34
|
+
context?: "fresh" | "fork";
|
|
35
|
+
requestedSessionDir?: string;
|
|
36
|
+
currentSessionFile?: string | null;
|
|
37
|
+
currentSessionId?: string | null;
|
|
38
|
+
orchestratorTarget?: string;
|
|
39
|
+
sessionError?: string;
|
|
40
|
+
expandTilde?: (value: string) => string;
|
|
41
|
+
paths?: DoctorPaths;
|
|
42
|
+
deps?: Partial<DoctorDeps>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const DEFAULT_PATHS: DoctorPaths = {
|
|
46
|
+
tempRootDir: TEMP_ROOT_DIR,
|
|
47
|
+
asyncDir: ASYNC_DIR,
|
|
48
|
+
resultsDir: RESULTS_DIR,
|
|
49
|
+
chainRunsDir: CHAIN_RUNS_DIR,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const DEFAULT_DEPS: DoctorDeps = {
|
|
53
|
+
isAsyncAvailable,
|
|
54
|
+
discoverAgentsAll,
|
|
55
|
+
discoverAvailableSkills,
|
|
56
|
+
diagnoseIntercomBridge,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function errorText(error: unknown): string {
|
|
60
|
+
return error instanceof Error ? `${error.name}: ${error.message}` : String(error);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function lineFromCheck(label: string, check: () => string): string {
|
|
64
|
+
try {
|
|
65
|
+
return check();
|
|
66
|
+
} catch (error) {
|
|
67
|
+
return `- ${label}: failed — ${errorText(error)}`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function formatExistingDirectory(label: string, dirPath: string): string {
|
|
72
|
+
try {
|
|
73
|
+
if (!fs.existsSync(dirPath)) return `- ${label}: missing (${dirPath})`;
|
|
74
|
+
const stats = fs.statSync(dirPath);
|
|
75
|
+
if (!stats.isDirectory()) throw new Error(`not a directory: ${dirPath}`);
|
|
76
|
+
fs.accessSync(dirPath, fs.constants.R_OK | fs.constants.W_OK);
|
|
77
|
+
return `- ${label}: ok (${dirPath})`;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return `- ${label}: failed (${dirPath}) — ${errorText(error)}`;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function formatSourceCounts(counts: Record<AgentSource, number>): string {
|
|
84
|
+
return `builtin ${counts.builtin}, user ${counts.user}, project ${counts.project}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function formatSkillSourceCounts(skills: Array<{ source: SkillSource }>): string {
|
|
88
|
+
const counts = new Map<SkillSource, number>();
|
|
89
|
+
for (const skill of skills) counts.set(skill.source, (counts.get(skill.source) ?? 0) + 1);
|
|
90
|
+
const ordered: SkillSource[] = [
|
|
91
|
+
"project",
|
|
92
|
+
"project-settings",
|
|
93
|
+
"project-package",
|
|
94
|
+
"user",
|
|
95
|
+
"user-settings",
|
|
96
|
+
"user-package",
|
|
97
|
+
"extension",
|
|
98
|
+
"builtin",
|
|
99
|
+
"unknown",
|
|
100
|
+
];
|
|
101
|
+
const parts = ordered
|
|
102
|
+
.map((source) => `${source} ${counts.get(source) ?? 0}`)
|
|
103
|
+
.filter((part) => !part.endsWith(" 0"));
|
|
104
|
+
return parts.length > 0 ? parts.join(", ") : "none";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function formatConfiguredSessionDir(input: DoctorReportInput): string {
|
|
108
|
+
if (input.requestedSessionDir) {
|
|
109
|
+
return path.resolve(input.expandTilde?.(input.requestedSessionDir) ?? input.requestedSessionDir);
|
|
110
|
+
}
|
|
111
|
+
if (input.config.defaultSessionDir) {
|
|
112
|
+
return path.resolve(input.expandTilde?.(input.config.defaultSessionDir) ?? input.config.defaultSessionDir);
|
|
113
|
+
}
|
|
114
|
+
return "not configured";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function formatSessionLines(input: DoctorReportInput): string[] {
|
|
118
|
+
const sessionFile = input.currentSessionFile ?? null;
|
|
119
|
+
const lines = [
|
|
120
|
+
lineFromCheck("configured session dir", () => `- configured session dir: ${formatConfiguredSessionDir(input)}`),
|
|
121
|
+
`- current session file: ${sessionFile ?? "not available"}`,
|
|
122
|
+
`- current session dir: ${sessionFile ? path.dirname(sessionFile) : "not available"}`,
|
|
123
|
+
`- current session id: ${input.currentSessionId ?? input.state.currentSessionId ?? "not available"}`,
|
|
124
|
+
];
|
|
125
|
+
if (input.sessionError) lines.push(`- session manager: failed — ${input.sessionError}`);
|
|
126
|
+
return lines;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function formatDiscovery(input: DoctorReportInput, deps: DoctorDeps): string[] {
|
|
130
|
+
return [
|
|
131
|
+
lineFromCheck("agents/chains", () => {
|
|
132
|
+
const discovered = deps.discoverAgentsAll(input.cwd);
|
|
133
|
+
const agentCounts = {
|
|
134
|
+
builtin: discovered.builtin.length,
|
|
135
|
+
user: discovered.user.length,
|
|
136
|
+
project: discovered.project.length,
|
|
137
|
+
};
|
|
138
|
+
const chainCounts = discovered.chains.reduce<Record<AgentSource, number>>((counts, chain) => {
|
|
139
|
+
counts[chain.source] += 1;
|
|
140
|
+
return counts;
|
|
141
|
+
}, { builtin: 0, user: 0, project: 0 });
|
|
142
|
+
return [
|
|
143
|
+
`- agents: total ${agentCounts.builtin + agentCounts.user + agentCounts.project} (${formatSourceCounts(agentCounts)})`,
|
|
144
|
+
`- chains: total ${discovered.chains.length} (${formatSourceCounts(chainCounts)})`,
|
|
145
|
+
].join("\n");
|
|
146
|
+
}),
|
|
147
|
+
lineFromCheck("skills", () => {
|
|
148
|
+
const skills = deps.discoverAvailableSkills(input.cwd);
|
|
149
|
+
return `- skills: total ${skills.length} (${formatSkillSourceCounts(skills)})`;
|
|
150
|
+
}),
|
|
151
|
+
];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function formatIntercomDiagnostic(diagnostic: IntercomBridgeDiagnostic, context: "fresh" | "fork" | undefined): string[] {
|
|
155
|
+
const lines = [
|
|
156
|
+
`- bridge: ${diagnostic.active ? "active" : "inactive"}${diagnostic.reason ? ` (${diagnostic.reason})` : ""}`,
|
|
157
|
+
`- mode: ${diagnostic.mode}; context: ${context ?? "unspecified"}`,
|
|
158
|
+
`- orchestrator target: ${diagnostic.orchestratorTarget ?? "not available"}`,
|
|
159
|
+
`- pi-intercom: ${diagnostic.piIntercomAvailable ? "available" : "unavailable"} at ${diagnostic.extensionDir}`,
|
|
160
|
+
];
|
|
161
|
+
if (diagnostic.configPath && diagnostic.intercomConfigEnabled !== undefined) {
|
|
162
|
+
lines.push(`- intercom config: ${diagnostic.intercomConfigEnabled === false ? "disabled" : "enabled or absent"} (${diagnostic.configPath})`);
|
|
163
|
+
}
|
|
164
|
+
if (diagnostic.intercomConfigError) {
|
|
165
|
+
lines.push(`- intercom config warning: ${diagnostic.intercomConfigError}; runtime assumes enabled`);
|
|
166
|
+
}
|
|
167
|
+
return lines;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function buildDoctorReport(input: DoctorReportInput): string {
|
|
171
|
+
const paths = input.paths ?? DEFAULT_PATHS;
|
|
172
|
+
const deps = { ...DEFAULT_DEPS, ...input.deps };
|
|
173
|
+
const lines = [
|
|
174
|
+
"Subagents doctor report",
|
|
175
|
+
"",
|
|
176
|
+
"Runtime",
|
|
177
|
+
`- cwd: ${input.cwd}`,
|
|
178
|
+
lineFromCheck("async support", () => `- async support: ${deps.isAsyncAvailable() ? "available" : "unavailable"}`),
|
|
179
|
+
...formatSessionLines(input),
|
|
180
|
+
"",
|
|
181
|
+
"Filesystem",
|
|
182
|
+
formatExistingDirectory("temp root", paths.tempRootDir),
|
|
183
|
+
formatExistingDirectory("async runs", paths.asyncDir),
|
|
184
|
+
formatExistingDirectory("results", paths.resultsDir),
|
|
185
|
+
formatExistingDirectory("chain runs", paths.chainRunsDir),
|
|
186
|
+
"",
|
|
187
|
+
"Discovery",
|
|
188
|
+
...formatDiscovery(input, deps),
|
|
189
|
+
"",
|
|
190
|
+
"Intercom bridge",
|
|
191
|
+
...lineFromCheck("intercom bridge", () => formatIntercomDiagnostic(deps.diagnoseIntercomBridge({
|
|
192
|
+
config: input.config.intercomBridge,
|
|
193
|
+
context: input.context,
|
|
194
|
+
orchestratorTarget: input.orchestratorTarget,
|
|
195
|
+
cwd: input.cwd,
|
|
196
|
+
}), input.context).join("\n")).split("\n"),
|
|
197
|
+
];
|
|
198
|
+
return lines.join("\n");
|
|
199
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import type { ExtensionAPI, ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { discoverAgents } from "../agents/agents.ts";
|
|
6
|
+
import { getArtifactsDir } from "../shared/artifacts.ts";
|
|
7
|
+
import { createSubagentExecutor, type SubagentParamsLike } from "../runs/foreground/subagent-executor.ts";
|
|
8
|
+
import { SUBAGENT_CHILD_ENV, SUBAGENT_FANOUT_CHILD_ENV } from "../runs/shared/pi-args.ts";
|
|
9
|
+
import { readNestedControlRequests, resolveNestedRouteFromEnv, writeNestedControlResult } from "../runs/shared/nested-events.ts";
|
|
10
|
+
import { deliverSubagentIntercomMessageEvent } from "../intercom/result-intercom.ts";
|
|
11
|
+
import { resolveSubagentIntercomTarget } from "../intercom/intercom-bridge.ts";
|
|
12
|
+
import { SubagentParams } from "./schemas.ts";
|
|
13
|
+
import { loadConfig } from "./config.ts";
|
|
14
|
+
import { type Details, type SubagentState } from "../shared/types.ts";
|
|
15
|
+
|
|
16
|
+
function getSubagentSessionRoot(parentSessionFile: string | null): string {
|
|
17
|
+
if (parentSessionFile) {
|
|
18
|
+
const baseName = path.basename(parentSessionFile, ".jsonl");
|
|
19
|
+
const sessionsDir = path.dirname(parentSessionFile);
|
|
20
|
+
return path.join(sessionsDir, baseName);
|
|
21
|
+
}
|
|
22
|
+
return fs.mkdtempSync(path.join(os.tmpdir(), "pi-subagent-session-"));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function expandTilde(p: string): string {
|
|
26
|
+
return p.startsWith("~/") ? path.join(os.homedir(), p.slice(2)) : p;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function createChildSafeState(): SubagentState {
|
|
30
|
+
return {
|
|
31
|
+
baseCwd: "",
|
|
32
|
+
currentSessionId: null,
|
|
33
|
+
asyncJobs: new Map(),
|
|
34
|
+
foregroundRuns: new Map(),
|
|
35
|
+
foregroundControls: new Map(),
|
|
36
|
+
lastForegroundControlId: null,
|
|
37
|
+
pendingForegroundControlNotices: new Map(),
|
|
38
|
+
cleanupTimers: new Map(),
|
|
39
|
+
lastUiContext: null,
|
|
40
|
+
poller: null,
|
|
41
|
+
completionSeen: new Map(),
|
|
42
|
+
watcher: null,
|
|
43
|
+
watcherRestartTimer: null,
|
|
44
|
+
resultFileCoalescer: {
|
|
45
|
+
schedule: () => false,
|
|
46
|
+
clear: () => {},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function startNestedControlInboxListener(pi: ExtensionAPI, state: SubagentState): NodeJS.Timeout | undefined {
|
|
52
|
+
let route;
|
|
53
|
+
try {
|
|
54
|
+
route = resolveNestedRouteFromEnv();
|
|
55
|
+
} catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
if (!route) return undefined;
|
|
59
|
+
const seen = new Set<string>();
|
|
60
|
+
const inFlight = new Set<string>();
|
|
61
|
+
const pendingResults = new Map<string, Parameters<typeof writeNestedControlResult>[1]>();
|
|
62
|
+
const timer = setInterval(() => {
|
|
63
|
+
try {
|
|
64
|
+
for (const request of readNestedControlRequests(route)) {
|
|
65
|
+
if (seen.has(request.requestId) || inFlight.has(request.requestId)) continue;
|
|
66
|
+
inFlight.add(request.requestId);
|
|
67
|
+
void (async () => {
|
|
68
|
+
try {
|
|
69
|
+
let result = pendingResults.get(request.requestId);
|
|
70
|
+
if (!result) {
|
|
71
|
+
let ok = false;
|
|
72
|
+
let message = "Control request failed.";
|
|
73
|
+
try {
|
|
74
|
+
const control = state.foregroundControls.get(request.targetRunId);
|
|
75
|
+
if (!control) {
|
|
76
|
+
message = `Nested run ${request.targetRunId} is not active in this fanout child.`;
|
|
77
|
+
} else if (request.action === "interrupt") {
|
|
78
|
+
ok = control.interrupt?.() === true;
|
|
79
|
+
message = ok
|
|
80
|
+
? `Interrupt requested for nested run ${request.targetRunId}.`
|
|
81
|
+
: `Nested run ${request.targetRunId} has no active child step to interrupt.`;
|
|
82
|
+
} else if (!request.message?.trim()) {
|
|
83
|
+
message = "Nested resume requires message.";
|
|
84
|
+
} else if (!control.currentAgent) {
|
|
85
|
+
message = `Nested run ${request.targetRunId} has no active child message route.`;
|
|
86
|
+
} else {
|
|
87
|
+
const index = control.currentIndex ?? 0;
|
|
88
|
+
const target = resolveSubagentIntercomTarget(request.targetRunId, control.currentAgent, index);
|
|
89
|
+
ok = await deliverSubagentIntercomMessageEvent(
|
|
90
|
+
pi.events,
|
|
91
|
+
target,
|
|
92
|
+
`Follow-up for nested run ${request.targetRunId} (${control.currentAgent}):\n\n${request.message.trim()}`,
|
|
93
|
+
500,
|
|
94
|
+
{ source: "nested-resume", runId: request.targetRunId, agent: control.currentAgent, index },
|
|
95
|
+
);
|
|
96
|
+
message = ok
|
|
97
|
+
? `Delivered follow-up to live nested run ${request.targetRunId}.`
|
|
98
|
+
: `Nested child intercom target is not registered: ${target}`;
|
|
99
|
+
}
|
|
100
|
+
} catch (error) {
|
|
101
|
+
message = error instanceof Error ? error.message : String(error);
|
|
102
|
+
}
|
|
103
|
+
result = { ts: Date.now(), requestId: request.requestId, targetRunId: request.targetRunId, ok, message };
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
writeNestedControlResult(route, result);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
pendingResults.set(request.requestId, result);
|
|
109
|
+
console.error(`Failed to write nested control result for request '${request.requestId}' targeting '${request.targetRunId}' via inbox '${route.controlInbox}'; keeping request for retry:`, error);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
pendingResults.delete(request.requestId);
|
|
113
|
+
seen.add(request.requestId);
|
|
114
|
+
try { fs.unlinkSync(request.filePath); } catch {}
|
|
115
|
+
} finally {
|
|
116
|
+
inFlight.delete(request.requestId);
|
|
117
|
+
}
|
|
118
|
+
})();
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.error(`Failed to poll nested control inbox '${route.controlInbox}' for root '${route.rootRunId}':`, error);
|
|
122
|
+
}
|
|
123
|
+
}, 200);
|
|
124
|
+
timer.unref?.();
|
|
125
|
+
return timer;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export default function registerFanoutChildSubagentExtension(pi: ExtensionAPI): void {
|
|
129
|
+
if (process.env[SUBAGENT_CHILD_ENV] !== "1" || process.env[SUBAGENT_FANOUT_CHILD_ENV] !== "1") return;
|
|
130
|
+
|
|
131
|
+
const globalStore = globalThis as Record<string, unknown>;
|
|
132
|
+
const registeredKey = "__piSubagentFanoutChildRegisteredApis";
|
|
133
|
+
const registeredApis = globalStore[registeredKey] instanceof WeakSet
|
|
134
|
+
? globalStore[registeredKey] as WeakSet<ExtensionAPI>
|
|
135
|
+
: new WeakSet<ExtensionAPI>();
|
|
136
|
+
globalStore[registeredKey] = registeredApis;
|
|
137
|
+
if (registeredApis.has(pi)) return;
|
|
138
|
+
registeredApis.add(pi);
|
|
139
|
+
|
|
140
|
+
const config = loadConfig();
|
|
141
|
+
const state = createChildSafeState();
|
|
142
|
+
const executor = createSubagentExecutor({
|
|
143
|
+
pi,
|
|
144
|
+
state,
|
|
145
|
+
config,
|
|
146
|
+
asyncByDefault: config.asyncByDefault === true,
|
|
147
|
+
tempArtifactsDir: getArtifactsDir(null),
|
|
148
|
+
getSubagentSessionRoot,
|
|
149
|
+
expandTilde,
|
|
150
|
+
discoverAgents,
|
|
151
|
+
allowMutatingManagementActions: false,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const tool: ToolDefinition<typeof SubagentParams, Details> = {
|
|
155
|
+
name: "subagent",
|
|
156
|
+
label: "Subagent",
|
|
157
|
+
description: [
|
|
158
|
+
"Delegate to subagents from child-safe fanout mode.",
|
|
159
|
+
"Allowed management/control actions: list, get, status, interrupt, resume, doctor.",
|
|
160
|
+
"Agent config mutation actions create, update, and delete are blocked in this mode.",
|
|
161
|
+
].join("\n"),
|
|
162
|
+
parameters: SubagentParams,
|
|
163
|
+
execute(id, params, signal, onUpdate, ctx) {
|
|
164
|
+
return executor.execute(id, params as SubagentParamsLike, signal, onUpdate, ctx);
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
pi.registerTool(tool);
|
|
169
|
+
startNestedControlInboxListener(pi, state);
|
|
170
|
+
}
|