@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.
Files changed (163) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +21 -0
  3. package/README.md +246 -0
  4. package/agents/AGENTS-MANIFEST.md +42 -0
  5. package/agents/brain.md +42 -0
  6. package/agents/context-builder.md +46 -0
  7. package/agents/delegate.md +12 -0
  8. package/agents/dev-1.md +42 -0
  9. package/agents/oracle.md +73 -0
  10. package/agents/planner.md +55 -0
  11. package/agents/researcher.md +52 -0
  12. package/agents/reviewer.md +79 -0
  13. package/agents/scout.md +50 -0
  14. package/agents/tester.md +45 -0
  15. package/agents/worker.md +55 -0
  16. package/extensions/ralph.ts +1 -0
  17. package/extensions/reviewer-extension.ts +125 -0
  18. package/extensions/task-orchestrator.ts +28 -0
  19. package/package.json +63 -0
  20. package/prompts/gather-context-and-clarify.md +13 -0
  21. package/prompts/parallel-cleanup.md +59 -0
  22. package/prompts/parallel-context-build.md +53 -0
  23. package/prompts/parallel-handoff-plan.md +59 -0
  24. package/prompts/parallel-research.md +50 -0
  25. package/prompts/parallel-review.md +54 -0
  26. package/prompts/review-loop.md +41 -0
  27. package/skills/orchid/SKILL.md +214 -0
  28. package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
  29. package/skills/orchid/orchid-converge/SKILL.md +124 -0
  30. package/skills/orchid/orchid-decompose/SKILL.md +201 -0
  31. package/skills/orchid/orchid-doctor/SKILL.md +162 -0
  32. package/skills/orchid/orchid-investigate/SKILL.md +102 -0
  33. package/skills/orchid/orchid-launch/SKILL.md +147 -0
  34. package/skills/ralph/SKILL.md +73 -0
  35. package/skills/subagents/pi-subagents/SKILL.md +813 -0
  36. package/src/index.ts +7 -0
  37. package/src/orchestrator/abort.ts +534 -0
  38. package/src/orchestrator/agent-bridge-extension.ts +1020 -0
  39. package/src/orchestrator/agent-host.ts +954 -0
  40. package/src/orchestrator/cleanup.ts +776 -0
  41. package/src/orchestrator/config-loader.ts +1412 -0
  42. package/src/orchestrator/config-schema.ts +690 -0
  43. package/src/orchestrator/config.ts +81 -0
  44. package/src/orchestrator/context-window.ts +66 -0
  45. package/src/orchestrator/diagnostic-reports.ts +475 -0
  46. package/src/orchestrator/diagnostics.ts +394 -0
  47. package/src/orchestrator/discovery.ts +1833 -0
  48. package/src/orchestrator/engine-worker.ts +415 -0
  49. package/src/orchestrator/engine.ts +5940 -0
  50. package/src/orchestrator/execution.ts +3104 -0
  51. package/src/orchestrator/extension.ts +5934 -0
  52. package/src/orchestrator/formatting.ts +785 -0
  53. package/src/orchestrator/git.ts +88 -0
  54. package/src/orchestrator/index.ts +28 -0
  55. package/src/orchestrator/lane-runner.ts +1787 -0
  56. package/src/orchestrator/mailbox.ts +780 -0
  57. package/src/orchestrator/merge.ts +3414 -0
  58. package/src/orchestrator/messages.ts +1062 -0
  59. package/src/orchestrator/migrations.ts +278 -0
  60. package/src/orchestrator/naming.ts +117 -0
  61. package/src/orchestrator/path-resolver.ts +275 -0
  62. package/src/orchestrator/persistence.ts +2625 -0
  63. package/src/orchestrator/process-registry.ts +452 -0
  64. package/src/orchestrator/quality-gate.ts +1085 -0
  65. package/src/orchestrator/resume.ts +3488 -0
  66. package/src/orchestrator/sessions.ts +57 -0
  67. package/src/orchestrator/settings-loader.ts +136 -0
  68. package/src/orchestrator/settings-tui.ts +2208 -0
  69. package/src/orchestrator/sidecar-telemetry.ts +267 -0
  70. package/src/orchestrator/supervisor.ts +4548 -0
  71. package/src/orchestrator/task-executor-core.ts +675 -0
  72. package/src/orchestrator/tmux-compat.ts +37 -0
  73. package/src/orchestrator/tool-allowlist-constants.ts +37 -0
  74. package/src/orchestrator/types.ts +4465 -0
  75. package/src/orchestrator/verification.ts +547 -0
  76. package/src/orchestrator/waves.ts +1564 -0
  77. package/src/orchestrator/workspace.ts +707 -0
  78. package/src/orchestrator/worktree.ts +2725 -0
  79. package/src/ralph/index.ts +825 -0
  80. package/src/subagents/agents/agent-management.ts +648 -0
  81. package/src/subagents/agents/agent-scope.ts +6 -0
  82. package/src/subagents/agents/agent-selection.ts +23 -0
  83. package/src/subagents/agents/agent-serializer.ts +86 -0
  84. package/src/subagents/agents/agents.ts +832 -0
  85. package/src/subagents/agents/chain-serializer.ts +137 -0
  86. package/src/subagents/agents/frontmatter.ts +29 -0
  87. package/src/subagents/agents/identity.ts +30 -0
  88. package/src/subagents/agents/skills.ts +632 -0
  89. package/src/subagents/extension/config.ts +16 -0
  90. package/src/subagents/extension/control-notices.ts +92 -0
  91. package/src/subagents/extension/doctor.ts +199 -0
  92. package/src/subagents/extension/fanout-child.ts +170 -0
  93. package/src/subagents/extension/index.ts +573 -0
  94. package/src/subagents/extension/schemas.ts +168 -0
  95. package/src/subagents/intercom/intercom-bridge.ts +379 -0
  96. package/src/subagents/intercom/result-intercom.ts +377 -0
  97. package/src/subagents/runs/background/async-execution.ts +712 -0
  98. package/src/subagents/runs/background/async-job-tracker.ts +310 -0
  99. package/src/subagents/runs/background/async-resume.ts +345 -0
  100. package/src/subagents/runs/background/async-status.ts +325 -0
  101. package/src/subagents/runs/background/completion-dedupe.ts +63 -0
  102. package/src/subagents/runs/background/notify.ts +108 -0
  103. package/src/subagents/runs/background/parallel-groups.ts +45 -0
  104. package/src/subagents/runs/background/result-watcher.ts +307 -0
  105. package/src/subagents/runs/background/run-id-resolver.ts +83 -0
  106. package/src/subagents/runs/background/run-status.ts +269 -0
  107. package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
  108. package/src/subagents/runs/background/subagent-runner.ts +1808 -0
  109. package/src/subagents/runs/background/top-level-async.ts +13 -0
  110. package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
  111. package/src/subagents/runs/foreground/chain-execution.ts +938 -0
  112. package/src/subagents/runs/foreground/execution.ts +918 -0
  113. package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
  114. package/src/subagents/runs/shared/completion-guard.ts +147 -0
  115. package/src/subagents/runs/shared/long-running-guard.ts +175 -0
  116. package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
  117. package/src/subagents/runs/shared/model-fallback.ts +103 -0
  118. package/src/subagents/runs/shared/nested-events.ts +819 -0
  119. package/src/subagents/runs/shared/nested-path.ts +52 -0
  120. package/src/subagents/runs/shared/nested-render.ts +115 -0
  121. package/src/subagents/runs/shared/parallel-utils.ts +109 -0
  122. package/src/subagents/runs/shared/pi-args.ts +220 -0
  123. package/src/subagents/runs/shared/pi-spawn.ts +115 -0
  124. package/src/subagents/runs/shared/run-history.ts +60 -0
  125. package/src/subagents/runs/shared/single-output.ts +164 -0
  126. package/src/subagents/runs/shared/subagent-control.ts +226 -0
  127. package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
  128. package/src/subagents/runs/shared/worktree.ts +577 -0
  129. package/src/subagents/shared/artifacts.ts +98 -0
  130. package/src/subagents/shared/atomic-json.ts +16 -0
  131. package/src/subagents/shared/file-coalescer.ts +40 -0
  132. package/src/subagents/shared/fork-context.ts +76 -0
  133. package/src/subagents/shared/formatters.ts +133 -0
  134. package/src/subagents/shared/jsonl-writer.ts +81 -0
  135. package/src/subagents/shared/model-info.ts +78 -0
  136. package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
  137. package/src/subagents/shared/session-identity.ts +10 -0
  138. package/src/subagents/shared/session-tokens.ts +44 -0
  139. package/src/subagents/shared/settings.ts +397 -0
  140. package/src/subagents/shared/status-format.ts +49 -0
  141. package/src/subagents/shared/types.ts +822 -0
  142. package/src/subagents/shared/utils.ts +450 -0
  143. package/src/subagents/slash/prompt-template-bridge.ts +397 -0
  144. package/src/subagents/slash/slash-bridge.ts +174 -0
  145. package/src/subagents/slash/slash-commands.ts +528 -0
  146. package/src/subagents/slash/slash-live-state.ts +292 -0
  147. package/src/subagents/tui/render-helpers.ts +80 -0
  148. package/src/subagents/tui/render.ts +1358 -0
  149. package/templates/agents/local/supervisor.md +33 -0
  150. package/templates/agents/local/task-merger.md +27 -0
  151. package/templates/agents/local/task-reviewer.md +30 -0
  152. package/templates/agents/local/task-worker.md +34 -0
  153. package/templates/agents/supervisor-routing.md +92 -0
  154. package/templates/agents/supervisor.md +229 -0
  155. package/templates/agents/task-merger.md +214 -0
  156. package/templates/agents/task-reviewer.md +260 -0
  157. package/templates/agents/task-worker-segment.md +44 -0
  158. package/templates/agents/task-worker.md +557 -0
  159. package/templates/tasks/CONTEXT.md +30 -0
  160. package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
  161. package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
  162. package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
  163. package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
@@ -0,0 +1,164 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { OutputMode, SavedOutputReference } from "../../shared/types.ts";
4
+
5
+ export interface SingleOutputSnapshot {
6
+ exists: boolean;
7
+ mtimeMs?: number;
8
+ size?: number;
9
+ }
10
+
11
+ export function normalizeSingleOutputOverride(
12
+ output: string | boolean | undefined,
13
+ defaultOutput: string | undefined,
14
+ ): string | false | undefined {
15
+ if (output === false || output === "false") return false;
16
+ if (output === true || output === "true") return defaultOutput;
17
+ if (typeof output === "string" && output.length > 0) return output;
18
+ return undefined;
19
+ }
20
+
21
+ export function resolveSingleOutputPath(
22
+ output: string | boolean | undefined,
23
+ runtimeCwd: string,
24
+ requestedCwd?: string,
25
+ ): string | undefined {
26
+ if (typeof output !== "string" || !output || output === "false" || output === "true") return undefined;
27
+ if (path.isAbsolute(output)) return output;
28
+ const baseCwd = requestedCwd
29
+ ? (path.isAbsolute(requestedCwd) ? requestedCwd : path.resolve(runtimeCwd, requestedCwd))
30
+ : runtimeCwd;
31
+ return path.resolve(baseCwd, output);
32
+ }
33
+
34
+ export function injectSingleOutputInstruction(task: string, outputPath: string | undefined): string {
35
+ if (!outputPath) return task;
36
+ return `${task}\n\n---\n**Output:** Write your findings to: ${outputPath}`;
37
+ }
38
+
39
+ function countLines(text: string): number {
40
+ if (!text) return 0;
41
+ const newlineMatches = text.match(/\r\n|\r|\n/g);
42
+ return (newlineMatches?.length ?? 0) + (/[\r\n]$/.test(text) ? 0 : 1);
43
+ }
44
+
45
+ function formatByteSize(bytes: number): string {
46
+ if (bytes < 1024) return `${bytes} B`;
47
+ const units = ["KB", "MB", "GB", "TB"];
48
+ let value = bytes / 1024;
49
+ let unitIndex = 0;
50
+ while (value >= 1024 && unitIndex < units.length - 1) {
51
+ value /= 1024;
52
+ unitIndex++;
53
+ }
54
+ return `${value.toFixed(1)} ${units[unitIndex]}`;
55
+ }
56
+
57
+ export function formatSavedOutputReference(savedPath: string, fullOutput: string): SavedOutputReference {
58
+ const absolutePath = path.resolve(savedPath);
59
+ const bytes = Buffer.byteLength(fullOutput, "utf-8");
60
+ const lines = countLines(fullOutput);
61
+ return {
62
+ path: absolutePath,
63
+ bytes,
64
+ lines,
65
+ message: `Output saved to: ${absolutePath} (${formatByteSize(bytes)}, ${lines} ${lines === 1 ? "line" : "lines"}). Read this file if needed.`,
66
+ };
67
+ }
68
+
69
+ export function validateFileOnlyOutputMode(outputMode: OutputMode | undefined, outputPath: string | undefined, context: string): string | undefined {
70
+ if (outputMode === "file-only" && !outputPath) {
71
+ return `${context} sets outputMode: "file-only" but does not configure an output file. Set output to a path or use outputMode: "inline".`;
72
+ }
73
+ return undefined;
74
+ }
75
+
76
+ export function captureSingleOutputSnapshot(outputPath: string | undefined): SingleOutputSnapshot | undefined {
77
+ if (!outputPath) return undefined;
78
+ try {
79
+ const stat = fs.statSync(outputPath);
80
+ return { exists: true, mtimeMs: stat.mtimeMs, size: stat.size };
81
+ } catch {
82
+ // The snapshot is advisory; resolveSingleOutput reports concrete read/write failures.
83
+ return { exists: false };
84
+ }
85
+ }
86
+
87
+ function persistSingleOutput(
88
+ outputPath: string | undefined,
89
+ fullOutput: string,
90
+ ): { savedPath?: string; error?: string } {
91
+ if (!outputPath) return {};
92
+ try {
93
+ fs.mkdirSync(path.dirname(outputPath), { recursive: true });
94
+ fs.writeFileSync(outputPath, fullOutput, "utf-8");
95
+ return { savedPath: outputPath };
96
+ } catch (err) {
97
+ return { error: err instanceof Error ? err.message : String(err) };
98
+ }
99
+ }
100
+
101
+ export function resolveSingleOutput(
102
+ outputPath: string | undefined,
103
+ fallbackOutput: string,
104
+ beforeRun: SingleOutputSnapshot | undefined,
105
+ ): { fullOutput: string; savedPath?: string; saveError?: string } {
106
+ if (!outputPath) return { fullOutput: fallbackOutput };
107
+
108
+ let changedSinceStart = false;
109
+ try {
110
+ const stat = fs.statSync(outputPath);
111
+ changedSinceStart = !beforeRun?.exists
112
+ || stat.mtimeMs !== beforeRun.mtimeMs
113
+ || stat.size !== beforeRun.size;
114
+ } catch (error) {
115
+ const code = error && typeof error === "object" && "code" in error ? (error as { code?: unknown }).code : undefined;
116
+ if (code !== "ENOENT" && code !== "ENOTDIR") {
117
+ return {
118
+ fullOutput: fallbackOutput,
119
+ saveError: `Failed to inspect output file: ${error instanceof Error ? error.message : String(error)}`,
120
+ };
121
+ }
122
+ }
123
+
124
+ if (changedSinceStart) {
125
+ try {
126
+ return { fullOutput: fs.readFileSync(outputPath, "utf-8"), savedPath: outputPath };
127
+ } catch (error) {
128
+ return {
129
+ fullOutput: fallbackOutput,
130
+ saveError: `Failed to read changed output file: ${error instanceof Error ? error.message : String(error)}`,
131
+ };
132
+ }
133
+ }
134
+
135
+ const save = persistSingleOutput(outputPath, fallbackOutput);
136
+ if (save.savedPath) return { fullOutput: fallbackOutput, savedPath: save.savedPath };
137
+ return { fullOutput: fallbackOutput, saveError: save.error };
138
+ }
139
+
140
+ export function finalizeSingleOutput(params: {
141
+ fullOutput: string;
142
+ truncatedOutput?: string;
143
+ outputPath?: string;
144
+ outputMode?: OutputMode;
145
+ exitCode: number;
146
+ savedPath?: string;
147
+ outputReference?: SavedOutputReference;
148
+ saveError?: string;
149
+ }): { displayOutput: string; savedPath?: string; outputReference?: SavedOutputReference; saveError?: string } {
150
+ let displayOutput = params.truncatedOutput || params.fullOutput;
151
+ if (params.exitCode === 0 && params.savedPath) {
152
+ const outputReference = params.outputReference ?? formatSavedOutputReference(params.savedPath, params.fullOutput);
153
+ if (params.outputMode === "file-only") {
154
+ return { displayOutput: outputReference.message, savedPath: params.savedPath, outputReference };
155
+ }
156
+ displayOutput += `\n\n${outputReference.message}`;
157
+ return { displayOutput, savedPath: params.savedPath, outputReference };
158
+ }
159
+ if (params.exitCode === 0 && params.saveError && params.outputPath) {
160
+ displayOutput += `\n\nOutput file error: ${params.outputPath}\n${params.saveError}`;
161
+ return { displayOutput, saveError: params.saveError };
162
+ }
163
+ return { displayOutput };
164
+ }
@@ -0,0 +1,226 @@
1
+ import {
2
+ type ActivityState,
3
+ type ControlConfig,
4
+ type ControlEvent,
5
+ type ControlEventType,
6
+ type ControlNotificationChannel,
7
+ type ResolvedControlConfig,
8
+ } from "../../shared/types.ts";
9
+
10
+ const CONTROL_EVENT_TYPES: ControlEventType[] = ["active_long_running", "needs_attention"];
11
+ const CONTROL_NOTIFICATION_CHANNELS: ControlNotificationChannel[] = ["event", "async", "intercom"];
12
+ const DEFAULT_NOTIFY_ON: ControlEventType[] = ["active_long_running", "needs_attention"];
13
+
14
+ export const DEFAULT_CONTROL_CONFIG: ResolvedControlConfig = {
15
+ enabled: true,
16
+ needsAttentionAfterMs: 60_000,
17
+ activeNoticeAfterMs: 240_000,
18
+ failedToolAttemptsBeforeAttention: 3,
19
+ notifyOn: DEFAULT_NOTIFY_ON,
20
+ notifyChannels: CONTROL_NOTIFICATION_CHANNELS,
21
+ };
22
+
23
+ function parsePositiveInt(value: unknown): number | undefined {
24
+ if (typeof value !== "number") return undefined;
25
+ if (!Number.isFinite(value) || !Number.isInteger(value) || value < 1) return undefined;
26
+ return value;
27
+ }
28
+
29
+ function parseControlList<T extends string>(value: unknown, allowed: readonly T[]): T[] | undefined {
30
+ if (!Array.isArray(value)) return undefined;
31
+ if (value.length === 0) return [];
32
+ const allowedSet = new Set(allowed);
33
+ const parsed = value.filter((entry): entry is T => typeof entry === "string" && allowedSet.has(entry as T));
34
+ return parsed.length > 0 ? Array.from(new Set(parsed)) : undefined;
35
+ }
36
+
37
+ export function resolveControlConfig(
38
+ globalConfig?: ControlConfig,
39
+ override?: ControlConfig,
40
+ ): ResolvedControlConfig {
41
+ const enabled = override?.enabled ?? globalConfig?.enabled ?? DEFAULT_CONTROL_CONFIG.enabled;
42
+ const needsAttentionAfterMs = parsePositiveInt(override?.needsAttentionAfterMs)
43
+ ?? parsePositiveInt(globalConfig?.needsAttentionAfterMs)
44
+ ?? DEFAULT_CONTROL_CONFIG.needsAttentionAfterMs;
45
+ const activeNoticeAfterMs = parsePositiveInt(override?.activeNoticeAfterMs)
46
+ ?? parsePositiveInt(globalConfig?.activeNoticeAfterMs)
47
+ ?? DEFAULT_CONTROL_CONFIG.activeNoticeAfterMs;
48
+ const activeNoticeAfterTurns = parsePositiveInt(override?.activeNoticeAfterTurns)
49
+ ?? parsePositiveInt(globalConfig?.activeNoticeAfterTurns);
50
+ const activeNoticeAfterTokens = parsePositiveInt(override?.activeNoticeAfterTokens)
51
+ ?? parsePositiveInt(globalConfig?.activeNoticeAfterTokens);
52
+ const failedToolAttemptsBeforeAttention = parsePositiveInt(override?.failedToolAttemptsBeforeAttention)
53
+ ?? parsePositiveInt(globalConfig?.failedToolAttemptsBeforeAttention)
54
+ ?? DEFAULT_CONTROL_CONFIG.failedToolAttemptsBeforeAttention;
55
+ const notifyOn = parseControlList(override?.notifyOn, CONTROL_EVENT_TYPES)
56
+ ?? parseControlList(globalConfig?.notifyOn, CONTROL_EVENT_TYPES)
57
+ ?? DEFAULT_CONTROL_CONFIG.notifyOn;
58
+ const notifyChannels = parseControlList(override?.notifyChannels, CONTROL_NOTIFICATION_CHANNELS)
59
+ ?? parseControlList(globalConfig?.notifyChannels, CONTROL_NOTIFICATION_CHANNELS)
60
+ ?? DEFAULT_CONTROL_CONFIG.notifyChannels;
61
+ return {
62
+ enabled,
63
+ needsAttentionAfterMs,
64
+ activeNoticeAfterMs,
65
+ activeNoticeAfterTurns,
66
+ activeNoticeAfterTokens,
67
+ failedToolAttemptsBeforeAttention,
68
+ notifyOn: [...notifyOn],
69
+ notifyChannels: [...notifyChannels],
70
+ };
71
+ }
72
+
73
+ export function deriveActivityState(input: {
74
+ config: ResolvedControlConfig;
75
+ startedAt: number;
76
+ lastActivityAt?: number;
77
+ now?: number;
78
+ }): ActivityState | undefined {
79
+ if (!input.config.enabled) return undefined;
80
+ const now = input.now ?? Date.now();
81
+ const lastActivity = input.lastActivityAt ?? input.startedAt;
82
+ const ageMs = Math.max(0, now - lastActivity);
83
+ return ageMs > input.config.needsAttentionAfterMs ? "needs_attention" : undefined;
84
+ }
85
+
86
+ export function buildControlEvent(input: {
87
+ type?: ControlEventType;
88
+ from?: ActivityState;
89
+ to: ActivityState;
90
+ runId: string;
91
+ agent: string;
92
+ index?: number;
93
+ ts?: number;
94
+ lastActivityAt?: number;
95
+ message?: string;
96
+ reason?: ControlEvent["reason"];
97
+ turns?: number;
98
+ tokens?: number;
99
+ toolCount?: number;
100
+ currentTool?: string;
101
+ currentToolDurationMs?: number;
102
+ currentPath?: string;
103
+ elapsedMs?: number;
104
+ recentFailureSummary?: string;
105
+ }): ControlEvent {
106
+ const ts = input.ts ?? Date.now();
107
+ const type = input.type ?? (input.to === "active_long_running" ? "active_long_running" : "needs_attention");
108
+ const elapsedMs = input.elapsedMs ?? (input.lastActivityAt ? Math.max(0, ts - input.lastActivityAt) : undefined);
109
+ const elapsedSeconds = elapsedMs !== undefined ? Math.floor(elapsedMs / 1000) : undefined;
110
+ const message = input.message ?? (type === "active_long_running"
111
+ ? `${input.agent} is still active but long-running`
112
+ : elapsedSeconds !== undefined
113
+ ? `${input.agent} needs attention (no observed activity for ${elapsedSeconds}s)`
114
+ : `${input.agent} needs attention`);
115
+ return {
116
+ type,
117
+ ...(input.from ? { from: input.from } : {}),
118
+ to: input.to,
119
+ ts,
120
+ runId: input.runId,
121
+ agent: input.agent,
122
+ ...(input.index !== undefined ? { index: input.index } : {}),
123
+ message,
124
+ reason: input.reason ?? (type === "active_long_running" ? "active_long_running" : "idle"),
125
+ ...(input.turns !== undefined ? { turns: input.turns } : {}),
126
+ ...(input.tokens !== undefined ? { tokens: input.tokens } : {}),
127
+ ...(input.toolCount !== undefined ? { toolCount: input.toolCount } : {}),
128
+ ...(input.currentTool ? { currentTool: input.currentTool } : {}),
129
+ ...(input.currentToolDurationMs !== undefined ? { currentToolDurationMs: input.currentToolDurationMs } : {}),
130
+ ...(input.currentPath ? { currentPath: input.currentPath } : {}),
131
+ ...(elapsedMs !== undefined ? { elapsedMs } : {}),
132
+ ...(input.recentFailureSummary ? { recentFailureSummary: input.recentFailureSummary } : {}),
133
+ };
134
+ }
135
+
136
+ export function shouldNotifyControlEvent(config: ResolvedControlConfig, event: ControlEvent): boolean {
137
+ return config.enabled && config.notifyOn.includes(event.type);
138
+ }
139
+
140
+ export function controlNotificationKey(event: ControlEvent, childIntercomTarget?: string): string {
141
+ const childKey = childIntercomTarget ?? (event.index !== undefined ? `${event.runId}:${event.index}` : event.runId);
142
+ return `${childKey}:${event.type}:${event.reason ?? "idle"}`;
143
+ }
144
+
145
+ export function claimControlNotification(config: ResolvedControlConfig, event: ControlEvent, seenKeys: Set<string>, childIntercomTarget?: string): boolean {
146
+ if (!shouldNotifyControlEvent(config, event)) return false;
147
+ const key = controlNotificationKey(event, childIntercomTarget);
148
+ if (seenKeys.has(key)) return false;
149
+ seenKeys.add(key);
150
+ return true;
151
+ }
152
+
153
+ function formatLongRunningFacts(event: ControlEvent): string | undefined {
154
+ const facts: string[] = [];
155
+ if (event.elapsedMs !== undefined) facts.push(`elapsed ${Math.floor(Math.max(0, event.elapsedMs) / 1000)}s`);
156
+ if (event.turns !== undefined) facts.push(`${event.turns} turns`);
157
+ if (event.tokens !== undefined) facts.push(`${event.tokens} tokens`);
158
+ if (event.toolCount !== undefined) facts.push(`${event.toolCount} tools`);
159
+ if (event.currentTool) facts.push(`tool ${event.currentTool}${event.currentToolDurationMs !== undefined ? ` ${Math.floor(Math.max(0, event.currentToolDurationMs) / 1000)}s` : ""}`);
160
+ if (event.currentPath) facts.push(`path ${event.currentPath}`);
161
+ return facts.length > 0 ? facts.join(" | ") : undefined;
162
+ }
163
+
164
+ export function formatControlNoticeMessage(event: ControlEvent, childIntercomTarget?: string): string {
165
+ const runTarget = event.runId;
166
+ if (event.reason === "completion_guard") {
167
+ return [
168
+ `Subagent failed: ${event.agent}`,
169
+ `Run: ${runTarget}${event.index !== undefined ? ` step ${event.index + 1}` : ""}`,
170
+ `Signal: ${event.message}`,
171
+ "Next: read the output artifact or session from the subagent result, then retry with a more explicit implementation prompt or handle the fix directly.",
172
+ childIntercomTarget ? `Run intercom target (may be inactive): ${childIntercomTarget}` : undefined,
173
+ ].filter((line): line is string => Boolean(line)).join("\n");
174
+ }
175
+
176
+ const nudgeCommand = childIntercomTarget
177
+ ? `intercom({ action: "send", to: "${childIntercomTarget}", message: "What are you blocked on? Reply with the smallest next step or ask for a decision." })`
178
+ : undefined;
179
+ if (event.type === "active_long_running") {
180
+ const facts = formatLongRunningFacts(event);
181
+ return [
182
+ `Subagent active but long-running: ${event.agent}`,
183
+ `Run: ${runTarget}${event.index !== undefined ? ` step ${event.index + 1}` : ""}`,
184
+ `Signal: ${event.message}`,
185
+ facts ? `Facts: ${facts}` : undefined,
186
+ "Hint: Inspect status, then nudge if the work seems stuck.",
187
+ childIntercomTarget
188
+ ? `Nudge: ${nudgeCommand}`
189
+ : "Nudge: no child message route registered",
190
+ `Status: subagent({ action: "status", id: "${runTarget}" })`,
191
+ `Interrupt: subagent({ action: "interrupt", id: "${runTarget}" })`,
192
+ ].filter((line): line is string => Boolean(line)).join("\n");
193
+ }
194
+
195
+ return [
196
+ `Subagent needs attention: ${event.agent}`,
197
+ `Run: ${runTarget}${event.index !== undefined ? ` step ${event.index + 1}` : ""}`,
198
+ `Signal: ${event.message}`,
199
+ event.recentFailureSummary ? `Recent failures: ${event.recentFailureSummary}` : undefined,
200
+ "Hint: Inspect status first unless the run is clearly blocked.",
201
+ childIntercomTarget
202
+ ? `Nudge: ${nudgeCommand}`
203
+ : "Nudge: no child message route registered",
204
+ `Status: subagent({ action: "status", id: "${runTarget}" })`,
205
+ `Interrupt: subagent({ action: "interrupt", id: "${runTarget}" })`,
206
+ ].filter((line): line is string => Boolean(line)).join("\n");
207
+ }
208
+
209
+ export function formatControlIntercomMessage(event: ControlEvent, childIntercomTarget?: string): string {
210
+ const statusLabel = event.reason === "completion_guard"
211
+ ? "subagent failed"
212
+ : event.type === "active_long_running"
213
+ ? "subagent active but long-running"
214
+ : "subagent needs attention";
215
+ return [
216
+ statusLabel,
217
+ "",
218
+ event.reason === "completion_guard"
219
+ ? `${event.agent} failed in run ${event.runId}.`
220
+ : event.type === "active_long_running"
221
+ ? `${event.agent} is still active but long-running in run ${event.runId}.`
222
+ : `${event.agent} needs attention in run ${event.runId}.`,
223
+ "",
224
+ formatControlNoticeMessage(event, childIntercomTarget),
225
+ ].join("\n");
226
+ }
@@ -0,0 +1,170 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { SUBAGENT_FANOUT_CHILD_ENV } from "./pi-args.ts";
3
+
4
+ const SUBAGENT_INHERIT_PROJECT_CONTEXT_ENV = "PI_SUBAGENT_INHERIT_PROJECT_CONTEXT";
5
+ const SUBAGENT_INHERIT_SKILLS_ENV = "PI_SUBAGENT_INHERIT_SKILLS";
6
+ export const SUBAGENT_INTERCOM_SESSION_NAME_ENV = "PI_SUBAGENT_INTERCOM_SESSION_NAME";
7
+
8
+ export const CHILD_SUBAGENT_BOUNDARY_INSTRUCTIONS = [
9
+ "You are a child subagent, not the parent orchestrator.",
10
+ "The parent session owns delegation, orchestration, review fanout, and follow-up worker launches.",
11
+ "Ignore prior parent-only orchestration instructions in inherited conversation history.",
12
+ "Do not propose or run subagents. Complete only your assigned role-specific task with the tools available to you.",
13
+ "If you need to edit files, call the actual edit/write tools. Do not print tool-call syntax, patches, or pseudo-tool calls as text.",
14
+ ].join("\n");
15
+
16
+ export const CHILD_FANOUT_BOUNDARY_INSTRUCTIONS = [
17
+ "You are a child subagent with explicit fanout responsibility for this assigned task.",
18
+ "The parent session owns final orchestration, acceptance, and follow-up implementation launches.",
19
+ "You may use the `subagent` tool only for the fanout work explicitly requested in this task.",
20
+ "Do not broaden yourself into general parent orchestration. Do not launch follow-up workers unless the task explicitly asks for that.",
21
+ "The maxSubagentDepth cap still applies and may block further fanout.",
22
+ "If you need to edit files, call the actual edit/write tools. Do not print tool-call syntax, patches, or pseudo-tool calls as text.",
23
+ ].join("\n");
24
+
25
+ const PARENT_ONLY_CUSTOM_MESSAGE_TYPES = new Set([
26
+ "subagent-orchestration-instructions",
27
+ "subagent-slash-result",
28
+ "subagent-notify",
29
+ "subagent_control_notice",
30
+ "subagent-control",
31
+ "subagent-control-notice",
32
+ ]);
33
+ const SUBAGENT_ORCHESTRATION_SKILL_NAME_PATTERN = /<name>\s*pi-subagents\s*<\/name>/;
34
+ const PROJECT_CONTEXT_HEADER = "\n\n# Project Context\n\nProject-specific instructions and guidelines:\n\n";
35
+ const SKILLS_HEADER = "\n\nThe following skills provide specialized instructions for specific tasks.";
36
+ const DATE_HEADER = "\nCurrent date:";
37
+
38
+ function readBooleanEnv(name: string): boolean | undefined {
39
+ const value = process.env[name];
40
+ if (value === undefined) return undefined;
41
+ return value !== "0";
42
+ }
43
+
44
+ function findSectionEnd(prompt: string, startIndex: number, nextHeaders: string[]): number {
45
+ let endIndex = prompt.length;
46
+ for (const header of nextHeaders) {
47
+ const index = prompt.indexOf(header, startIndex);
48
+ if (index !== -1 && index < endIndex) {
49
+ endIndex = index;
50
+ }
51
+ }
52
+ return endIndex;
53
+ }
54
+
55
+ export function stripProjectContext(prompt: string): string {
56
+ const startIndex = prompt.indexOf(PROJECT_CONTEXT_HEADER);
57
+ if (startIndex === -1) return prompt;
58
+ const endIndex = findSectionEnd(prompt, startIndex + PROJECT_CONTEXT_HEADER.length, [SKILLS_HEADER, DATE_HEADER]);
59
+ return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`;
60
+ }
61
+
62
+ export function stripInheritedSkills(prompt: string): string {
63
+ const startIndex = prompt.indexOf(SKILLS_HEADER);
64
+ if (startIndex === -1) return prompt;
65
+ const endIndex = findSectionEnd(prompt, startIndex + SKILLS_HEADER.length, [DATE_HEADER]);
66
+ return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`;
67
+ }
68
+
69
+ export function stripSubagentOrchestrationSkill(prompt: string): string {
70
+ return prompt
71
+ .replace(/\n{0,2}<skill\s+name=["']pi-subagents["'][^>]*>[\s\S]*?<\/skill>\n{0,2}/g, "\n\n")
72
+ .replace(/[ \t]*<skill>\s*[\s\S]*?<\/skill>\s*/g, (block) => SUBAGENT_ORCHESTRATION_SKILL_NAME_PATTERN.test(block) ? "" : block);
73
+ }
74
+
75
+ function stripChildBoundaryInstructions(prompt: string): string {
76
+ let rewritten = prompt;
77
+ for (const boundary of [CHILD_SUBAGENT_BOUNDARY_INSTRUCTIONS, CHILD_FANOUT_BOUNDARY_INSTRUCTIONS]) {
78
+ rewritten = rewritten.split(boundary).join("");
79
+ }
80
+ return rewritten.replace(/^(?:[ \t]*\r?\n)+/, "");
81
+ }
82
+
83
+ export function rewriteSubagentPrompt(
84
+ prompt: string,
85
+ options: { inheritProjectContext: boolean; inheritSkills: boolean; fanoutChild?: boolean },
86
+ ): string {
87
+ let rewritten = prompt;
88
+ if (!options.inheritProjectContext) {
89
+ rewritten = stripProjectContext(rewritten);
90
+ }
91
+ if (!options.inheritSkills) {
92
+ rewritten = stripInheritedSkills(rewritten);
93
+ }
94
+ rewritten = stripSubagentOrchestrationSkill(rewritten);
95
+ rewritten = stripChildBoundaryInstructions(rewritten);
96
+ const boundary = options.fanoutChild ? CHILD_FANOUT_BOUNDARY_INSTRUCTIONS : CHILD_SUBAGENT_BOUNDARY_INSTRUCTIONS;
97
+ return `${boundary}\n\n${rewritten}`;
98
+ }
99
+
100
+ function isParentOnlySubagentMessage(message: unknown): boolean {
101
+ const m = message as { role?: string; customType?: string };
102
+ return m?.role === "custom"
103
+ && typeof m.customType === "string"
104
+ && PARENT_ONLY_CUSTOM_MESSAGE_TYPES.has(m.customType);
105
+ }
106
+
107
+ function isSubagentToolResultMessage(message: unknown): boolean {
108
+ const m = message as { role?: string; toolName?: string };
109
+ return m?.role === "toolResult" && m.toolName === "subagent";
110
+ }
111
+
112
+ function isSubagentToolCallBlock(block: unknown): boolean {
113
+ const b = block as { type?: string; name?: string };
114
+ return b?.type === "toolCall" && b.name === "subagent";
115
+ }
116
+
117
+ function stripAssistantSubagentToolCallBlocks(message: unknown): unknown | undefined {
118
+ const m = message as { role?: string; content?: unknown };
119
+ if (m?.role !== "assistant" || !Array.isArray(m.content)) return message;
120
+ const filteredContent = m.content.filter((block) => !isSubagentToolCallBlock(block));
121
+ if (filteredContent.length === m.content.length) return message;
122
+ if (filteredContent.length === 0) return undefined;
123
+ return { ...m, content: filteredContent };
124
+ }
125
+
126
+ export function stripParentOnlySubagentMessages(messages: unknown[]): unknown[] {
127
+ let changed = false;
128
+ const filtered: unknown[] = [];
129
+ for (const message of messages) {
130
+ if (isParentOnlySubagentMessage(message) || isSubagentToolResultMessage(message)) {
131
+ changed = true;
132
+ continue;
133
+ }
134
+ const stripped = stripAssistantSubagentToolCallBlocks(message);
135
+ if (stripped === undefined) {
136
+ changed = true;
137
+ continue;
138
+ }
139
+ if (stripped !== message) changed = true;
140
+ filtered.push(stripped);
141
+ }
142
+ return changed ? filtered : messages;
143
+ }
144
+
145
+ export default function registerSubagentPromptRuntime(pi: ExtensionAPI): void {
146
+ pi.on("context", (event) => {
147
+ const messages = stripParentOnlySubagentMessages(event.messages);
148
+ if (messages === event.messages) return undefined;
149
+ return { messages };
150
+ });
151
+
152
+ pi.on("before_agent_start", async (event) => {
153
+ const intercomSessionName = process.env[SUBAGENT_INTERCOM_SESSION_NAME_ENV]?.trim();
154
+ if (intercomSessionName && typeof pi.setSessionName === "function") {
155
+ pi.setSessionName(intercomSessionName);
156
+ }
157
+
158
+ const inheritProjectContext = readBooleanEnv(SUBAGENT_INHERIT_PROJECT_CONTEXT_ENV);
159
+ const inheritSkills = readBooleanEnv(SUBAGENT_INHERIT_SKILLS_ENV);
160
+ const fanoutChild = readBooleanEnv(SUBAGENT_FANOUT_CHILD_ENV);
161
+ if (inheritProjectContext === undefined && inheritSkills === undefined && fanoutChild === undefined) return;
162
+ const rewritten = rewriteSubagentPrompt(event.systemPrompt, {
163
+ inheritProjectContext: inheritProjectContext ?? true,
164
+ inheritSkills: inheritSkills ?? true,
165
+ fanoutChild: fanoutChild === true,
166
+ });
167
+ if (rewritten === event.systemPrompt) return;
168
+ return { systemPrompt: rewritten };
169
+ });
170
+ }