@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,168 @@
1
+ /**
2
+ * TypeBox schemas for subagent tool parameters
3
+ */
4
+
5
+ import { Type } from "@sinclair/typebox";
6
+ import { SUBAGENT_ACTIONS } from "../shared/types.ts";
7
+
8
+ const SkillOverride = Type.Unsafe({
9
+ anyOf: [
10
+ { type: "array", items: { type: "string" } },
11
+ { type: "boolean" },
12
+ { type: "string" },
13
+ ],
14
+ description: "Skill name(s) to inject (comma-separated), array of strings, or boolean (false disables, true uses default)",
15
+ });
16
+
17
+ const OutputOverride = Type.Unsafe({
18
+ anyOf: [
19
+ { type: "string" },
20
+ { type: "boolean" },
21
+ ],
22
+ description: "Output filename/path (string), or false to disable file output",
23
+ });
24
+
25
+ const OutputModeOverride = Type.String({
26
+ enum: ["inline", "file-only"],
27
+ description: "Return saved output inline (default) or only a concise file reference. file-only requires output to be a path.",
28
+ });
29
+
30
+ const ReadsOverride = Type.Unsafe({
31
+ anyOf: [
32
+ { type: "array", items: { type: "string" } },
33
+ { type: "boolean" },
34
+ ],
35
+ description: "Files to read before running (array of filenames), or false to disable",
36
+ });
37
+
38
+ const TaskItem = Type.Object({
39
+ agent: Type.String(),
40
+ task: Type.String(),
41
+ cwd: Type.Optional(Type.String()),
42
+ count: Type.Optional(Type.Integer({ minimum: 1, description: "Repeat this parallel task N times with the same settings." })),
43
+ output: Type.Optional(OutputOverride),
44
+ outputMode: Type.Optional(OutputModeOverride),
45
+ reads: Type.Optional(ReadsOverride),
46
+ progress: Type.Optional(Type.Boolean({ description: "Enable progress.md tracking for this task" })),
47
+ model: Type.Optional(Type.String({ description: "Override model for this task (e.g. 'google/gemini-3-pro')" })),
48
+ skill: Type.Optional(SkillOverride),
49
+ });
50
+
51
+ // Parallel task item (within a parallel step)
52
+ const ParallelTaskSchema = Type.Object({
53
+ agent: Type.String(),
54
+ task: Type.Optional(Type.String({ description: "Task template with {task}, {previous}, {chain_dir} variables. Defaults to {previous}." })),
55
+ cwd: Type.Optional(Type.String()),
56
+ count: Type.Optional(Type.Integer({ minimum: 1, description: "Repeat this parallel task N times with the same settings." })),
57
+ output: Type.Optional(OutputOverride),
58
+ outputMode: Type.Optional(OutputModeOverride),
59
+ reads: Type.Optional(ReadsOverride),
60
+ progress: Type.Optional(Type.Boolean({ description: "Enable progress.md tracking in {chain_dir}" })),
61
+ skill: Type.Optional(SkillOverride),
62
+ model: Type.Optional(Type.String({ description: "Override model for this task" })),
63
+ });
64
+
65
+ // Flattened so chain steps do not need an object-shape anyOf/oneOf union.
66
+ const ChainItem = Type.Object({
67
+ agent: Type.Optional(Type.String({ description: "Sequential step agent name" })),
68
+ task: Type.Optional(Type.String({
69
+ description: "Task template with variables: {task}=original request, {previous}=prior step's text response, {chain_dir}=shared folder. Required for first step, defaults to '{previous}' for subsequent steps."
70
+ })),
71
+ cwd: Type.Optional(Type.String()),
72
+ output: Type.Optional(OutputOverride),
73
+ outputMode: Type.Optional(OutputModeOverride),
74
+ reads: Type.Optional(ReadsOverride),
75
+ progress: Type.Optional(Type.Boolean({ description: "Enable progress.md tracking in {chain_dir}" })),
76
+ skill: Type.Optional(SkillOverride),
77
+ model: Type.Optional(Type.String({ description: "Override model for this step" })),
78
+ parallel: Type.Optional(Type.Array(ParallelTaskSchema, { minItems: 1, description: "Tasks to run in parallel" })),
79
+ concurrency: Type.Optional(Type.Number({ description: "Max concurrent tasks (default: 4)" })),
80
+ failFast: Type.Optional(Type.Boolean({ description: "Stop on first failure (default: false)" })),
81
+ worktree: Type.Optional(Type.Boolean({
82
+ description: "Create isolated git worktrees for each parallel task."
83
+ })),
84
+ }, { description: "Chain step: use {agent, task?, ...} for sequential or {parallel: [...]} for concurrent execution" });
85
+
86
+ const ControlOverrides = Type.Object({
87
+ enabled: Type.Optional(Type.Boolean({ description: "Enable/disable subagent control attention tracking for this run" })),
88
+ needsAttentionAfterMs: Type.Optional(Type.Integer({ minimum: 1, description: "No-observed-activity window before a run needs attention" })),
89
+ activeNoticeAfterMs: Type.Optional(Type.Integer({ minimum: 1, description: "Active-long-running notice threshold by elapsed ms (default: 240000)" })),
90
+ activeNoticeAfterTurns: Type.Optional(Type.Integer({ minimum: 1, description: "Optional active-long-running notice threshold by assistant turns (disabled by default)" })),
91
+ activeNoticeAfterTokens: Type.Optional(Type.Integer({ minimum: 1, description: "Optional active-long-running notice threshold by total tokens (disabled by default)" })),
92
+ failedToolAttemptsBeforeAttention: Type.Optional(Type.Integer({ minimum: 1, description: "Consecutive mutating-tool failures before escalating to needs_attention (default: 3)" })),
93
+ notifyOn: Type.Optional(Type.Array(Type.String({ enum: ["active_long_running", "needs_attention"] }), {
94
+ description: "Control event types that should notify the parent/orchestrator. Defaults to active_long_running and needs_attention.",
95
+ })),
96
+ notifyChannels: Type.Optional(Type.Array(Type.String({ enum: ["event", "async", "intercom"] }), {
97
+ description: "Notification channels to use when available. Defaults to event, async, and intercom.",
98
+ })),
99
+ });
100
+
101
+ export const SubagentParams = Type.Object({
102
+ agent: Type.Optional(Type.String({ description: "Agent name (SINGLE mode) or target for management get/update/delete" })),
103
+ task: Type.Optional(Type.String({ description: "Task (SINGLE mode, optional for self-contained agents)" })),
104
+ // Management action (when present, tool operates in management mode)
105
+ action: Type.Optional(Type.String({
106
+ enum: [...SUBAGENT_ACTIONS],
107
+ description: "Management/control action. Omit for execution mode."
108
+ })),
109
+ id: Type.Optional(Type.String({
110
+ description: "Run id or prefix for action='status', action='interrupt', or action='resume'."
111
+ })),
112
+ runId: Type.Optional(Type.String({
113
+ description: "Target run ID for action='interrupt' or action='resume'. Defaults to the most recently active controllable run for interrupt. Prefer id for new calls."
114
+ })),
115
+ dir: Type.Optional(Type.String({
116
+ description: "Async run directory for action='status' or action='resume'."
117
+ })),
118
+ index: Type.Optional(Type.Integer({ minimum: 0, description: "Zero-based child index for actions that target a specific child." })),
119
+ message: Type.Optional(Type.String({ description: "Follow-up message for action='resume'. Use index to choose a child from multi-child runs." })),
120
+ // Chain identifier for management (can't reuse 'chain' — that's the execution array)
121
+ chainName: Type.Optional(Type.String({
122
+ description: "Chain name for get/update/delete management actions"
123
+ })),
124
+ // Agent/chain configuration for create/update (nested to avoid conflicts with execution fields)
125
+ config: Type.Optional(Type.Unsafe({
126
+ anyOf: [
127
+ { type: "object", additionalProperties: true },
128
+ { type: "string" },
129
+ ],
130
+ description: "Agent or chain config for create/update. Agent: name, package (optional namespace; runtime name becomes package.name), description, scope ('user'|'project', default 'user'), systemPrompt, systemPromptMode, inheritProjectContext, inheritSkills, defaultContext ('fresh'|'fork'), model, tools (comma-separated), extensions (comma-separated), skills (comma-separated), thinking, output, reads, progress, maxSubagentDepth. Chain: name, package, description, scope, steps (array of {agent, task?, output?, outputMode?, reads?, model?, skill?, progress?}). Presence of 'steps' creates a chain instead of an agent. String values must be valid JSON."
131
+ })),
132
+ tasks: Type.Optional(Type.Array(TaskItem, { description: "PARALLEL mode: [{agent, task, count?, output?, outputMode?, reads?, progress?}, ...]" })),
133
+ concurrency: Type.Optional(Type.Integer({ minimum: 1, description: "Top-level PARALLEL mode only: max concurrent tasks. Defaults to config.parallel.concurrency or 4." })),
134
+ worktree: Type.Optional(Type.Boolean({
135
+ description: "Create isolated git worktrees for each parallel task. " +
136
+ "Prevents filesystem conflicts. Requires clean git state. " +
137
+ "Per-worktree diffs included in output."
138
+ })),
139
+ chain: Type.Optional(Type.Array(ChainItem, { description: "CHAIN mode: sequential pipeline where each step's response becomes {previous} for the next. Use {task}, {previous}, {chain_dir} in task templates." })),
140
+ context: Type.Optional(Type.String({
141
+ enum: ["fresh", "fork"],
142
+ description: "'fresh' or 'fork' to branch from parent session. If omitted, any requested agent with defaultContext: 'fork' makes the whole invocation forked; otherwise the default is 'fresh'.",
143
+ })),
144
+ chainDir: Type.Optional(Type.String({ description: "Persistent directory for chain artifacts. Default: a user-scoped temp directory under <tmpdir>/ (auto-cleaned after 24h)" })),
145
+ async: Type.Optional(Type.Boolean({ description: "Run in background (default: false, or per config)" })),
146
+ agentScope: Type.Optional(Type.String({ description: "Agent discovery scope: 'user', 'project', or 'both' (default: 'both'; project wins on name collisions)" })),
147
+ cwd: Type.Optional(Type.String()),
148
+ artifacts: Type.Optional(Type.Boolean({ description: "Write debug artifacts (default: true)" })),
149
+ includeProgress: Type.Optional(Type.Boolean({ description: "Include full progress in result (default: false)" })),
150
+ share: Type.Optional(Type.Boolean({ description: "Upload session to GitHub Gist for sharing (default: false)" })),
151
+ sessionDir: Type.Optional(
152
+ Type.String({ description: "Directory to store session logs (default: temp; enables sessions even if share=false)" }),
153
+ ),
154
+ // Clarification TUI
155
+ clarify: Type.Optional(Type.Boolean({ description: "Show TUI to preview/edit before execution. Explicit clarify: true keeps the run foreground for the clarify UI; omitted clarify can still run in the background when async: true is set." })),
156
+ control: Type.Optional(ControlOverrides),
157
+ // Solo agent overrides
158
+ output: Type.Optional(Type.Unsafe({
159
+ anyOf: [
160
+ { type: "string" },
161
+ { type: "boolean" },
162
+ ],
163
+ description: "Output file for single agent (string), or false to disable. Relative paths resolve against cwd.",
164
+ })),
165
+ outputMode: Type.Optional(OutputModeOverride),
166
+ skill: Type.Optional(SkillOverride),
167
+ model: Type.Optional(Type.String({ description: "Override model for single agent (e.g. 'anthropic/claude-sonnet-4')" })),
168
+ });
@@ -0,0 +1,379 @@
1
+ import { execSync } from "node:child_process";
2
+ import * as fs from "node:fs";
3
+ import * as os from "node:os";
4
+ import * as path from "node:path";
5
+ import type { AgentConfig } from "../agents/agents.ts";
6
+ import type { ExtensionConfig, IntercomBridgeConfig, IntercomBridgeMode } from "../shared/types.ts";
7
+ import { getAgentDir } from "../shared/utils.ts";
8
+
9
+ const PI_INTERCOM_PACKAGE_NAME = "pi-intercom";
10
+ const CONFIG_DIR = ".pi";
11
+
12
+ function defaultAgentDir(): string {
13
+ return getAgentDir();
14
+ }
15
+
16
+ function defaultIntercomExtensionDir(agentDir = defaultAgentDir()): string {
17
+ return path.join(agentDir, "extensions", PI_INTERCOM_PACKAGE_NAME);
18
+ }
19
+
20
+ function defaultIntercomConfigPath(agentDir = defaultAgentDir()): string {
21
+ return path.join(agentDir, "intercom", "config.json");
22
+ }
23
+
24
+ function defaultSubagentConfigDir(agentDir = defaultAgentDir()): string {
25
+ return path.join(agentDir, "extensions", "subagent");
26
+ }
27
+
28
+ const DEFAULT_INTERCOM_TARGET_PREFIX = "subagent-chat";
29
+ export const INTERCOM_BRIDGE_MARKER = "Intercom orchestration channel:";
30
+ const DEFAULT_INTERCOM_BRIDGE_TEMPLATE = `The inherited thread is reference-only. Do not continue that conversation or send questions, status updates, or completion handoffs to the supervisor in normal assistant text.
31
+
32
+ Use contact_supervisor first. It resolves the supervisor session "{orchestratorTarget}" and run metadata automatically.
33
+ - Need a decision, blocked, approval, or product/API/scope ambiguity: contact_supervisor({ reason: "need_decision", message: "<question>" })
34
+ - After contact_supervisor with reason "need_decision", stay alive and continue only after the reply arrives. Do not finish your final response with a choose-one question.
35
+ - Do not ask for clarification when the only conflict is review-only/no-edit versus progress-writing or artifact-writing instructions. Review-only/no-edit wins; leave files unchanged and mention the conflict in your final result only if it matters.
36
+ - Meaningful progress or unexpected discoveries that change the plan: contact_supervisor({ reason: "progress_update", message: "UPDATE: <summary>" })
37
+ - Generic intercom is lower-level plumbing/fallback only: intercom({ action: "ask", to: "{orchestratorTarget}", message: "<question>" })
38
+
39
+ Do not use contact_supervisor or intercom for routine completion handoffs. If no coordination is needed, return a focused task result.`;
40
+
41
+ export interface IntercomBridgeState {
42
+ active: boolean;
43
+ mode: IntercomBridgeMode;
44
+ orchestratorTarget?: string;
45
+ extensionDir: string;
46
+ instruction: string;
47
+ }
48
+
49
+ export interface IntercomBridgeDiagnostic {
50
+ active: boolean;
51
+ mode: IntercomBridgeMode;
52
+ wantsIntercom: boolean;
53
+ piIntercomAvailable: boolean;
54
+ extensionDir: string;
55
+ configPath?: string;
56
+ orchestratorTarget?: string;
57
+ reason?: string;
58
+ intercomConfigEnabled?: boolean;
59
+ intercomConfigError?: string;
60
+ }
61
+
62
+ interface ResolveIntercomBridgeInput {
63
+ config: ExtensionConfig["intercomBridge"];
64
+ context: "fresh" | "fork" | undefined;
65
+ orchestratorTarget?: string;
66
+ extensionDir?: string;
67
+ configPath?: string;
68
+ settingsDir?: string;
69
+ cwd?: string;
70
+ agentDir?: string;
71
+ globalNpmRoot?: string | null;
72
+ }
73
+
74
+ export function resolveIntercomSessionTarget(sessionName: string | undefined, sessionId: string): string {
75
+ const trimmedName = sessionName?.trim();
76
+ if (trimmedName) return trimmedName;
77
+ const normalizedSessionId = sessionId.startsWith("session-") ? sessionId.slice("session-".length) : sessionId;
78
+ return `${DEFAULT_INTERCOM_TARGET_PREFIX}-${normalizedSessionId.slice(0, 8)}`;
79
+ }
80
+
81
+ function sanitizeIntercomTargetPart(value: string): string {
82
+ return value.trim().toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "") || "agent";
83
+ }
84
+
85
+ export function resolveSubagentIntercomTarget(runId: string, agent: string, index?: number): string {
86
+ const stepSuffix = index !== undefined ? `-${index + 1}` : "";
87
+ return `subagent-${sanitizeIntercomTargetPart(agent)}-${sanitizeIntercomTargetPart(runId)}${stepSuffix}`;
88
+ }
89
+
90
+ export function resolveIntercomBridgeMode(value: unknown): IntercomBridgeMode {
91
+ if (value === "off" || value === "always" || value === "fork-only") return value;
92
+ return "always";
93
+ }
94
+
95
+ function resolveIntercomBridgeConfig(value: ExtensionConfig["intercomBridge"]): Required<IntercomBridgeConfig> {
96
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
97
+ return {
98
+ mode: "always",
99
+ instructionFile: "",
100
+ };
101
+ }
102
+ return {
103
+ mode: resolveIntercomBridgeMode(value.mode),
104
+ instructionFile: typeof value.instructionFile === "string" ? value.instructionFile : "",
105
+ };
106
+ }
107
+
108
+ function intercomConfigStatus(configPath: string): { enabled: boolean; error?: unknown } {
109
+ if (!fs.existsSync(configPath)) return { enabled: true };
110
+ try {
111
+ const parsed = JSON.parse(fs.readFileSync(configPath, "utf-8")) as { enabled?: unknown };
112
+ return { enabled: parsed.enabled !== false };
113
+ } catch (error) {
114
+ return { enabled: true, error };
115
+ }
116
+ }
117
+
118
+ function readJsonBestEffort(filePath: string): unknown {
119
+ try {
120
+ return JSON.parse(fs.readFileSync(filePath, "utf-8"));
121
+ } catch (error) {
122
+ const code = error && typeof error === "object" && "code" in error ? (error as NodeJS.ErrnoException).code : undefined;
123
+ if (code !== "ENOENT") console.warn(`Failed to read JSON from '${filePath}'.`, error);
124
+ return null;
125
+ }
126
+ }
127
+
128
+ function packageHasPiExtension(packageRoot: string): boolean {
129
+ if (!fs.existsSync(packageRoot)) return false;
130
+ const pkg = readJsonBestEffort(path.join(packageRoot, "package.json"));
131
+ if (pkg && typeof pkg === "object" && !Array.isArray(pkg)) {
132
+ const pi = (pkg as { pi?: unknown }).pi;
133
+ if (pi && typeof pi === "object" && !Array.isArray(pi)) {
134
+ const extensions = (pi as { extensions?: unknown }).extensions;
135
+ return Array.isArray(extensions) && extensions.some((entry) => typeof entry === "string" && entry.trim() !== "");
136
+ }
137
+ }
138
+ return fs.existsSync(path.join(packageRoot, "extensions"));
139
+ }
140
+
141
+ function isSafePackagePath(value: string): boolean {
142
+ return value.length > 0
143
+ && !path.isAbsolute(value)
144
+ && value.split(/[\\/]/).every((part) => part.length > 0 && part !== "." && part !== "..");
145
+ }
146
+
147
+ function parseNpmPackageName(source: string): string | undefined {
148
+ const spec = source.slice(4).trim();
149
+ if (!spec) return undefined;
150
+ const match = spec.match(/^(@?[^@]+(?:\/[^@]+)?)(?:@(.+))?$/);
151
+ const packageName = match?.[1] ?? spec;
152
+ return isSafePackagePath(packageName) ? packageName : undefined;
153
+ }
154
+
155
+ function packageEntrySource(entry: unknown): string | undefined {
156
+ if (typeof entry === "string") return entry;
157
+ if (entry && typeof entry === "object" && !Array.isArray(entry) && typeof (entry as { source?: unknown }).source === "string") {
158
+ return (entry as { source: string }).source;
159
+ }
160
+ return undefined;
161
+ }
162
+
163
+ function packageEntryAllowsExtensions(entry: unknown): boolean {
164
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) return true;
165
+ const extensions = (entry as { extensions?: unknown }).extensions;
166
+ return !Array.isArray(extensions) || extensions.length > 0;
167
+ }
168
+
169
+ function findNearestProjectConfigDir(cwd: string): string | undefined {
170
+ let current = path.resolve(cwd);
171
+ while (true) {
172
+ const configDir = path.join(current, CONFIG_DIR);
173
+ if (fs.existsSync(path.join(configDir, "settings.json"))) return configDir;
174
+ const parent = path.dirname(current);
175
+ if (parent === current) return undefined;
176
+ current = parent;
177
+ }
178
+ }
179
+
180
+ let cachedGlobalNpmRoot: string | null | undefined;
181
+
182
+ function getGlobalNpmRoot(): string | null {
183
+ if (cachedGlobalNpmRoot !== undefined) return cachedGlobalNpmRoot;
184
+ try {
185
+ cachedGlobalNpmRoot = execSync("npm root -g", { encoding: "utf-8", timeout: 5000 }).trim();
186
+ return cachedGlobalNpmRoot;
187
+ } catch {
188
+ cachedGlobalNpmRoot = null;
189
+ return null;
190
+ }
191
+ }
192
+
193
+ function configuredPiIntercomPackageDir(input: ResolveIntercomBridgeInput, agentDir: string): string | undefined {
194
+ const projectConfigDir = input.cwd ? findNearestProjectConfigDir(path.resolve(input.cwd)) : undefined;
195
+ const settingsFiles = [
196
+ ...(projectConfigDir ? [{ file: path.join(projectConfigDir, "settings.json"), configDir: projectConfigDir, scope: "project" as const }] : []),
197
+ { file: path.join(agentDir, "settings.json"), configDir: agentDir, scope: "user" as const },
198
+ ];
199
+ const globalNpmRoot = input.globalNpmRoot === undefined ? getGlobalNpmRoot() : input.globalNpmRoot;
200
+
201
+ for (const { file, configDir, scope } of settingsFiles) {
202
+ const settings = readJsonBestEffort(file);
203
+ if (!settings || typeof settings !== "object" || Array.isArray(settings)) continue;
204
+ const packages = (settings as { packages?: unknown }).packages;
205
+ if (!Array.isArray(packages)) continue;
206
+
207
+ for (const entry of packages) {
208
+ if (!packageEntryAllowsExtensions(entry)) continue;
209
+ const source = packageEntrySource(entry)?.trim();
210
+ if (!source?.startsWith("npm:")) continue;
211
+ const packageName = parseNpmPackageName(source);
212
+ if (packageName !== PI_INTERCOM_PACKAGE_NAME) continue;
213
+ const candidates = scope === "project"
214
+ ? [path.join(configDir, "npm", "node_modules", packageName)]
215
+ : [
216
+ ...(globalNpmRoot ? [path.join(globalNpmRoot, packageName)] : []),
217
+ path.join(agentDir, "npm", "node_modules", packageName),
218
+ ];
219
+ const packageRoot = candidates.find(packageHasPiExtension);
220
+ if (packageRoot) return path.resolve(packageRoot);
221
+ }
222
+ }
223
+ return undefined;
224
+ }
225
+
226
+ function resolveIntercomExtensionDir(input: ResolveIntercomBridgeInput, agentDir: string): string {
227
+ const legacyDir = path.resolve(input.extensionDir ?? defaultIntercomExtensionDir(agentDir));
228
+ if (fs.existsSync(legacyDir)) return legacyDir;
229
+ return configuredPiIntercomPackageDir(input, agentDir) ?? legacyDir;
230
+ }
231
+
232
+ function extensionSandboxAllowsIntercom(extensions: string[] | undefined, extensionDir: string): boolean {
233
+ if (extensions === undefined) return true;
234
+
235
+ const intercomDir = path.resolve(extensionDir).replaceAll("\\", "/").toLowerCase();
236
+ for (const entry of extensions) {
237
+ const normalized = entry.trim().replaceAll("\\", "/").toLowerCase();
238
+ if (normalized === "pi-intercom") return true;
239
+ if (normalized === intercomDir) return true;
240
+ if (normalized.startsWith(`${intercomDir}/`)) return true;
241
+ if (normalized.endsWith("/pi-intercom")) return true;
242
+ if (normalized.includes("/pi-intercom/")) return true;
243
+ }
244
+ return false;
245
+ }
246
+
247
+ function expandTilde(filePath: string): string {
248
+ return filePath.startsWith("~/") ? path.join(os.homedir(), filePath.slice(2)) : filePath;
249
+ }
250
+
251
+ function resolveInstructionTemplate(instructionFile: string, settingsDir: string): string {
252
+ if (!instructionFile) return DEFAULT_INTERCOM_BRIDGE_TEMPLATE;
253
+ const expandedPath = expandTilde(instructionFile);
254
+ const resolvedPath = path.isAbsolute(expandedPath)
255
+ ? expandedPath
256
+ : path.resolve(settingsDir, expandedPath);
257
+ try {
258
+ return fs.readFileSync(resolvedPath, "utf-8");
259
+ } catch (error) {
260
+ console.warn(`Failed to read intercom bridge instructionFile at '${resolvedPath}'. Using default instructions.`, error);
261
+ return DEFAULT_INTERCOM_BRIDGE_TEMPLATE;
262
+ }
263
+ }
264
+
265
+ function buildIntercomBridgeInstruction(orchestratorTarget: string, template: string): string {
266
+ const instruction = template.replaceAll("{orchestratorTarget}", orchestratorTarget).trim();
267
+ if (instruction.startsWith(INTERCOM_BRIDGE_MARKER)) return instruction;
268
+ return `${INTERCOM_BRIDGE_MARKER}
269
+ ${instruction}`;
270
+ }
271
+
272
+ export function diagnoseIntercomBridge(input: ResolveIntercomBridgeInput): IntercomBridgeDiagnostic {
273
+ const config = resolveIntercomBridgeConfig(input.config);
274
+ const mode = config.mode;
275
+ const agentDir = path.resolve(input.agentDir ?? defaultAgentDir());
276
+ const extensionDir = resolveIntercomExtensionDir(input, agentDir);
277
+ const orchestratorTarget = input.orchestratorTarget?.trim();
278
+ const configPath = path.resolve(input.configPath ?? defaultIntercomConfigPath(agentDir));
279
+ const wantsIntercom = mode !== "off" && !(mode === "fork-only" && input.context !== "fork");
280
+ const piIntercomAvailable = fs.existsSync(extensionDir);
281
+ let configStatus: ReturnType<typeof intercomConfigStatus> | undefined;
282
+ let reason: string | undefined;
283
+ if (mode === "off") reason = "bridge mode is off";
284
+ else if (mode === "fork-only" && input.context !== "fork") reason = "bridge mode is fork-only and context is not fork";
285
+ else if (!orchestratorTarget) reason = "orchestrator target is not available";
286
+ else if (!piIntercomAvailable) reason = "pi-intercom extension was not found";
287
+ else {
288
+ configStatus = intercomConfigStatus(configPath);
289
+ if (!configStatus.enabled) reason = "intercom config is disabled";
290
+ }
291
+ let intercomConfigError: string | undefined;
292
+ if (configStatus?.error) {
293
+ const error = configStatus.error;
294
+ intercomConfigError = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
295
+ }
296
+
297
+ return {
298
+ active: reason === undefined,
299
+ mode,
300
+ wantsIntercom,
301
+ piIntercomAvailable,
302
+ extensionDir,
303
+ configPath,
304
+ ...(orchestratorTarget ? { orchestratorTarget } : {}),
305
+ ...(reason ? { reason } : {}),
306
+ ...(configStatus ? { intercomConfigEnabled: configStatus.enabled } : {}),
307
+ ...(intercomConfigError ? { intercomConfigError } : {}),
308
+ };
309
+ }
310
+
311
+ export function resolveIntercomBridge(input: ResolveIntercomBridgeInput): IntercomBridgeState {
312
+ const config = resolveIntercomBridgeConfig(input.config);
313
+ const mode = config.mode;
314
+ const agentDir = path.resolve(input.agentDir ?? defaultAgentDir());
315
+ const extensionDir = resolveIntercomExtensionDir(input, agentDir);
316
+ const orchestratorTarget = input.orchestratorTarget?.trim();
317
+ const settingsDir = path.resolve(input.settingsDir ?? defaultSubagentConfigDir(agentDir));
318
+ const defaultInstruction = buildIntercomBridgeInstruction(
319
+ orchestratorTarget || "{orchestratorTarget}",
320
+ DEFAULT_INTERCOM_BRIDGE_TEMPLATE,
321
+ );
322
+
323
+ if (mode === "off") {
324
+ return { active: false, mode, extensionDir, instruction: defaultInstruction };
325
+ }
326
+ if (mode === "fork-only" && input.context !== "fork") {
327
+ return { active: false, mode, extensionDir, instruction: defaultInstruction };
328
+ }
329
+ if (!orchestratorTarget) {
330
+ return { active: false, mode, extensionDir, instruction: defaultInstruction };
331
+ }
332
+ if (!fs.existsSync(extensionDir)) {
333
+ return { active: false, mode, extensionDir, instruction: defaultInstruction };
334
+ }
335
+
336
+ const configPath = path.resolve(input.configPath ?? defaultIntercomConfigPath(agentDir));
337
+ const intercomStatus = intercomConfigStatus(configPath);
338
+ if (intercomStatus.error) console.warn(`Failed to parse intercom config at '${configPath}'. Assuming enabled.`, intercomStatus.error);
339
+ if (!intercomStatus.enabled) {
340
+ return { active: false, mode, extensionDir, instruction: defaultInstruction };
341
+ }
342
+
343
+ const instruction = buildIntercomBridgeInstruction(
344
+ orchestratorTarget,
345
+ resolveInstructionTemplate(config.instructionFile, settingsDir),
346
+ );
347
+
348
+ return {
349
+ active: true,
350
+ mode,
351
+ orchestratorTarget,
352
+ extensionDir,
353
+ instruction,
354
+ };
355
+ }
356
+
357
+ export function applyIntercomBridgeToAgent(agent: AgentConfig, bridge: IntercomBridgeState): AgentConfig {
358
+ if (!bridge.active || !bridge.orchestratorTarget) return agent;
359
+ if (!extensionSandboxAllowsIntercom(agent.extensions, bridge.extensionDir)) return agent;
360
+
361
+ const bridgeTools = ["intercom", "contact_supervisor"];
362
+ const tools = agent.tools
363
+ ? [...agent.tools, ...bridgeTools.filter((tool) => !agent.tools?.includes(tool))]
364
+ : agent.tools;
365
+ const instruction = bridge.instruction;
366
+ const trimmedPrompt = agent.systemPrompt?.trim() || "";
367
+ const systemPrompt = trimmedPrompt.includes(INTERCOM_BRIDGE_MARKER)
368
+ ? trimmedPrompt
369
+ : trimmedPrompt
370
+ ? `${trimmedPrompt}\n\n${instruction}`
371
+ : instruction;
372
+
373
+ if (tools === agent.tools && systemPrompt === agent.systemPrompt) return agent;
374
+ return {
375
+ ...agent,
376
+ tools,
377
+ systemPrompt,
378
+ };
379
+ }