@fitchmultz/pi-subagents 0.0.0-stage → 0.44.4
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 +1645 -0
- package/LICENSE +21 -0
- package/README.md +1529 -2
- package/agents/context-builder.md +97 -0
- package/agents/debugger.md +51 -0
- package/agents/delegate.md +13 -0
- package/agents/fixer.md +37 -0
- package/agents/oracle.md +71 -0
- package/agents/planner.md +72 -0
- package/agents/researcher.md +51 -0
- package/agents/reviewer-claude.md +69 -0
- package/agents/reviewer-gpt.md +68 -0
- package/agents/reviewer-ponytail.md +82 -0
- package/agents/reviewer-security.md +81 -0
- package/agents/reviewer.md +69 -0
- package/agents/scout.md +66 -0
- package/agents/ui-designer.md +51 -0
- package/agents/watcher.md +26 -0
- package/agents/worker.md +46 -0
- package/agents/writer.md +36 -0
- package/dist/agents/agent-management.js +923 -0
- package/dist/agents/agent-scope.js +7 -0
- package/dist/agents/agent-selection.js +20 -0
- package/dist/agents/agent-serializer.js +100 -0
- package/dist/agents/agents.js +752 -0
- package/dist/agents/chain-serializer.js +367 -0
- package/dist/agents/frontmatter.js +21 -0
- package/dist/agents/identity.js +29 -0
- package/dist/agents/skills.js +570 -0
- package/dist/extension/build-info.js +1 -0
- package/dist/extension/compact-tools.js +112 -0
- package/dist/extension/config.js +22 -0
- package/dist/extension/control-notices.js +29 -0
- package/dist/extension/doctor.js +143 -0
- package/dist/extension/fanout-child.js +326 -0
- package/dist/extension/index.js +577 -0
- package/dist/extension/schemas.js +416 -0
- package/dist/extension/tool-input.js +20 -0
- package/dist/extension/tool-result.js +14 -0
- package/dist/history/canonical-result.js +81 -0
- package/dist/history/database.js +72 -0
- package/dist/history/index.js +194 -0
- package/dist/history/ingest.js +447 -0
- package/dist/history/queries.js +350 -0
- package/dist/history/store.js +216 -0
- package/dist/history/types.js +4 -0
- package/dist/history/worker.js +461 -0
- package/dist/intercom/intercom-bridge.js +112 -0
- package/dist/intercom/live-intercom.js +94 -0
- package/dist/intercom/result-intercom.js +373 -0
- package/dist/pi-intercom/agent-dir.js +10 -0
- package/dist/pi-intercom/broker/broker.js +426 -0
- package/dist/pi-intercom/broker/client.js +492 -0
- package/dist/pi-intercom/broker/framing.js +65 -0
- package/dist/pi-intercom/broker/paths.js +42 -0
- package/dist/pi-intercom/broker/pid.js +66 -0
- package/dist/pi-intercom/broker/spawn.js +221 -0
- package/dist/pi-intercom/config.js +102 -0
- package/dist/pi-intercom/index.js +2942 -0
- package/dist/pi-intercom/reply-tracker.js +182 -0
- package/dist/pi-intercom/session-targets.js +128 -0
- package/dist/pi-intercom/subagent-live-events.js +132 -0
- package/dist/pi-intercom/topics.js +157 -0
- package/dist/pi-intercom/types.js +163 -0
- package/dist/pi-intercom/ui/compose.js +269 -0
- package/dist/pi-intercom/ui/inline-message.js +80 -0
- package/dist/pi-intercom/ui/session-list.js +124 -0
- package/dist/runs/background/async-control.js +95 -0
- package/dist/runs/background/async-execution.js +676 -0
- package/dist/runs/background/async-job-tracker.js +383 -0
- package/dist/runs/background/async-result-file.js +58 -0
- package/dist/runs/background/async-resume.js +305 -0
- package/dist/runs/background/async-status.js +442 -0
- package/dist/runs/background/completion-dedupe.js +69 -0
- package/dist/runs/background/completion-delivery.js +245 -0
- package/dist/runs/background/notify.js +71 -0
- package/dist/runs/background/parallel-groups.js +55 -0
- package/dist/runs/background/result-watcher.js +426 -0
- package/dist/runs/background/run-id-resolver.js +64 -0
- package/dist/runs/background/run-status.js +382 -0
- package/dist/runs/background/stale-run-reconciler.js +335 -0
- package/dist/runs/background/subagent-runner-launcher.js +55 -0
- package/dist/runs/background/subagent-runner.js +1872 -0
- package/dist/runs/background/top-level-async.js +5 -0
- package/dist/runs/foreground/chain-clarify.js +1152 -0
- package/dist/runs/foreground/clarify-invocation.js +72 -0
- package/dist/runs/foreground/execution-input.js +423 -0
- package/dist/runs/foreground/foreground-control.js +817 -0
- package/dist/runs/foreground/question-control.js +77 -0
- package/dist/runs/foreground/run-async-path.js +205 -0
- package/dist/runs/foreground/subagent-executor.js +442 -0
- package/dist/runs/foreground/subagent-params.js +165 -0
- package/dist/runs/foreground/wait-run.js +139 -0
- package/dist/runs/shared/acceptance-contract.js +253 -0
- package/dist/runs/shared/acceptance-evaluation.js +256 -0
- package/dist/runs/shared/acceptance-finalization.js +280 -0
- package/dist/runs/shared/acceptance-reports.js +147 -0
- package/dist/runs/shared/acceptance.js +4 -0
- package/dist/runs/shared/chain-outputs.js +110 -0
- package/dist/runs/shared/child-attempt.js +693 -0
- package/dist/runs/shared/child-execution-cwd.js +96 -0
- package/dist/runs/shared/child-observations.js +114 -0
- package/dist/runs/shared/claude-code.js +199 -0
- package/dist/runs/shared/completion-guard.js +31 -0
- package/dist/runs/shared/dynamic-fanout.js +274 -0
- package/dist/runs/shared/history-index.js +72 -0
- package/dist/runs/shared/mcp-direct-tool-allowlist.js +334 -0
- package/dist/runs/shared/model-fallback.js +185 -0
- package/dist/runs/shared/mutating-tool-guard.js +170 -0
- package/dist/runs/shared/native-finalization.js +101 -0
- package/dist/runs/shared/native-usage.js +153 -0
- package/dist/runs/shared/nested-events.js +700 -0
- package/dist/runs/shared/nested-path.js +48 -0
- package/dist/runs/shared/nested-render.js +108 -0
- package/dist/runs/shared/parallel-utils.js +61 -0
- package/dist/runs/shared/parent-receipts.js +68 -0
- package/dist/runs/shared/parent-usage.js +121 -0
- package/dist/runs/shared/pi-args.js +310 -0
- package/dist/runs/shared/pi-spawn.js +52 -0
- package/dist/runs/shared/run-history-queries.js +45 -0
- package/dist/runs/shared/run-history.js +103 -0
- package/dist/runs/shared/run-id-ambiguity.js +4 -0
- package/dist/runs/shared/run-records.js +679 -0
- package/dist/runs/shared/session-cwd-preload.js +33 -0
- package/dist/runs/shared/single-output.js +222 -0
- package/dist/runs/shared/streaming-text.js +15 -0
- package/dist/runs/shared/structured-output.js +55 -0
- package/dist/runs/shared/subagent-control.js +179 -0
- package/dist/runs/shared/subagent-prompt-runtime.js +197 -0
- package/dist/runs/shared/subagent-tool-loop-guard.js +66 -0
- package/dist/runs/shared/supervisor-questions.js +320 -0
- package/dist/runs/shared/workflow-graph.js +214 -0
- package/dist/runs/shared/workflow-policy.js +84 -0
- package/dist/runs/shared/worktree.js +525 -0
- package/dist/shared/agent-context-policy.js +126 -0
- package/dist/shared/artifacts.js +96 -0
- package/dist/shared/atomic-json.js +13 -0
- package/dist/shared/cooperative.js +20 -0
- package/dist/shared/execution-cwd.js +14 -0
- package/dist/shared/file-coalescer.js +21 -0
- package/dist/shared/fork-context.js +49 -0
- package/dist/shared/formatters.js +85 -0
- package/dist/shared/journal-reader.js +407 -0
- package/dist/shared/lazy-tools.js +32 -0
- package/dist/shared/model-info.js +68 -0
- package/dist/shared/native-session.js +15 -0
- package/dist/shared/native-tui.js +18 -0
- package/dist/shared/native-typebox.js +20 -0
- package/dist/shared/post-exit-stdio-guard.js +197 -0
- package/dist/shared/prompt-sections.js +8 -0
- package/dist/shared/session-entries.js +52 -0
- package/dist/shared/session-identity.js +14 -0
- package/dist/shared/settings.js +254 -0
- package/dist/shared/status-format.js +111 -0
- package/dist/shared/temp-root.js +114 -0
- package/dist/shared/types.js +207 -0
- package/dist/shared/ui-mode.js +3 -0
- package/dist/shared/utils.js +334 -0
- package/dist/slash/prompt-template-bridge.js +298 -0
- package/dist/slash/slash-bridge.js +116 -0
- package/dist/slash/slash-commands.js +607 -0
- package/dist/slash/slash-live-state.js +246 -0
- package/dist/tui/action-hints.js +75 -0
- package/dist/tui/agent-history.js +197 -0
- package/dist/tui/agent-view.js +1656 -0
- package/dist/tui/render.js +1515 -0
- package/docs/intercom.md +579 -0
- package/docs/performance.md +121 -0
- package/package.json +101 -4
- package/prompts/gather-context-and-clarify.md +14 -0
- package/prompts/parallel-cleanup.md +62 -0
- package/prompts/parallel-context-build.md +56 -0
- package/prompts/parallel-handoff-plan.md +62 -0
- package/prompts/parallel-research.md +51 -0
- package/prompts/parallel-review.md +59 -0
- package/prompts/review-loop.md +42 -0
- package/scripts/build.mjs +134 -0
- package/scripts/check-compat.mjs +48 -0
- package/scripts/compat-host.mjs +19 -0
- package/scripts/linux-smoke.sh +51 -0
- package/scripts/local-install-smoke.mjs +66 -0
- package/scripts/native-package-smoke.mjs +82 -0
- package/scripts/package-smoke.mjs +205 -0
- package/scripts/prepare.mjs +67 -0
- package/scripts/real-pi-smoke.mjs +415 -0
- package/scripts/run-tests.mjs +149 -0
- package/skills/pi-intercom/SKILL.md +113 -0
- package/skills/pi-intercom/evals/evals.json +29 -0
- package/skills/pi-intercom/evals/trigger-evals.json +47 -0
- package/skills/pi-intercom/references/peer-sessions.md +69 -0
- package/skills/pi-subagents/SKILL.md +76 -0
- package/skills/pi-subagents/evals/evals.json +29 -0
- package/skills/pi-subagents/evals/trigger-evals.json +42 -0
- package/skills/pi-subagents/references/full-orchestration-guide.md +863 -0
- package/src/agents/agent-management.ts +813 -0
- package/src/agents/agent-scope.ts +7 -0
- package/src/agents/agent-selection.ts +23 -0
- package/src/agents/agent-serializer.ts +99 -0
- package/src/agents/agents.ts +916 -0
- package/src/agents/chain-serializer.ts +329 -0
- package/src/agents/frontmatter.ts +21 -0
- package/src/agents/identity.ts +30 -0
- package/src/agents/skills.ts +637 -0
- package/src/extension/build-info.ts +2 -0
- package/src/extension/compact-tools.ts +114 -0
- package/src/extension/config.ts +23 -0
- package/src/extension/control-notices.ts +47 -0
- package/src/extension/doctor.ts +171 -0
- package/src/extension/fanout-child.ts +312 -0
- package/src/extension/index.ts +649 -0
- package/src/extension/schemas.ts +444 -0
- package/src/extension/tool-input.ts +19 -0
- package/src/extension/tool-result.ts +16 -0
- package/src/history/canonical-result.ts +62 -0
- package/src/history/database.ts +59 -0
- package/src/history/index.ts +152 -0
- package/src/history/ingest.ts +332 -0
- package/src/history/queries.ts +241 -0
- package/src/history/store.ts +203 -0
- package/src/history/types.ts +166 -0
- package/src/history/worker.ts +317 -0
- package/src/intercom/intercom-bridge.ts +123 -0
- package/src/intercom/live-intercom.ts +104 -0
- package/src/intercom/result-intercom.ts +429 -0
- package/src/pi-intercom/agent-dir.ts +9 -0
- package/src/pi-intercom/broker/broker.ts +463 -0
- package/src/pi-intercom/broker/client.ts +558 -0
- package/src/pi-intercom/broker/framing.ts +77 -0
- package/src/pi-intercom/broker/paths.ts +47 -0
- package/src/pi-intercom/broker/pid.ts +59 -0
- package/src/pi-intercom/broker/spawn.ts +248 -0
- package/src/pi-intercom/config.ts +142 -0
- package/src/pi-intercom/index.ts +2929 -0
- package/src/pi-intercom/reply-tracker.ts +211 -0
- package/src/pi-intercom/session-targets.ts +172 -0
- package/src/pi-intercom/subagent-live-events.ts +133 -0
- package/src/pi-intercom/topics.ts +125 -0
- package/src/pi-intercom/types.ts +271 -0
- package/src/pi-intercom/ui/compose.ts +289 -0
- package/src/pi-intercom/ui/inline-message.ts +92 -0
- package/src/pi-intercom/ui/session-list.ts +139 -0
- package/src/runs/background/async-control.ts +89 -0
- package/src/runs/background/async-execution.ts +850 -0
- package/src/runs/background/async-job-tracker.ts +385 -0
- package/src/runs/background/async-result-file.ts +62 -0
- package/src/runs/background/async-resume.ts +333 -0
- package/src/runs/background/async-status.ts +481 -0
- package/src/runs/background/completion-dedupe.ts +79 -0
- package/src/runs/background/completion-delivery.ts +195 -0
- package/src/runs/background/notify.ts +123 -0
- package/src/runs/background/parallel-groups.ts +53 -0
- package/src/runs/background/result-watcher.ts +423 -0
- package/src/runs/background/run-id-resolver.ts +69 -0
- package/src/runs/background/run-status.ts +384 -0
- package/src/runs/background/stale-run-reconciler.ts +369 -0
- package/src/runs/background/subagent-runner-launcher.ts +56 -0
- package/src/runs/background/subagent-runner.ts +2053 -0
- package/src/runs/background/top-level-async.ts +13 -0
- package/src/runs/foreground/chain-clarify.ts +1340 -0
- package/src/runs/foreground/clarify-invocation.ts +77 -0
- package/src/runs/foreground/execution-input.ts +467 -0
- package/src/runs/foreground/foreground-control.ts +821 -0
- package/src/runs/foreground/question-control.ts +70 -0
- package/src/runs/foreground/run-async-path.ts +243 -0
- package/src/runs/foreground/subagent-executor.ts +501 -0
- package/src/runs/foreground/subagent-params.ts +320 -0
- package/src/runs/foreground/wait-run.ts +121 -0
- package/src/runs/shared/acceptance-contract.ts +257 -0
- package/src/runs/shared/acceptance-evaluation.ts +275 -0
- package/src/runs/shared/acceptance-finalization.ts +353 -0
- package/src/runs/shared/acceptance-reports.ts +152 -0
- package/src/runs/shared/acceptance.ts +33 -0
- package/src/runs/shared/chain-outputs.ts +114 -0
- package/src/runs/shared/child-attempt.ts +623 -0
- package/src/runs/shared/child-execution-cwd.ts +91 -0
- package/src/runs/shared/child-observations.ts +99 -0
- package/src/runs/shared/claude-code.ts +243 -0
- package/src/runs/shared/completion-guard.ts +35 -0
- package/src/runs/shared/dynamic-fanout.ts +295 -0
- package/src/runs/shared/history-index.ts +69 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +376 -0
- package/src/runs/shared/model-fallback.ts +219 -0
- package/src/runs/shared/mutating-tool-guard.ts +194 -0
- package/src/runs/shared/native-finalization.ts +128 -0
- package/src/runs/shared/native-usage.ts +113 -0
- package/src/runs/shared/nested-events.ts +752 -0
- package/src/runs/shared/nested-path.ts +52 -0
- package/src/runs/shared/nested-render.ts +119 -0
- package/src/runs/shared/parallel-utils.ts +153 -0
- package/src/runs/shared/parent-receipts.ts +59 -0
- package/src/runs/shared/parent-usage.ts +121 -0
- package/src/runs/shared/pi-args.ts +350 -0
- package/src/runs/shared/pi-spawn.ts +52 -0
- package/src/runs/shared/run-history-queries.ts +44 -0
- package/src/runs/shared/run-history.ts +93 -0
- package/src/runs/shared/run-id-ambiguity.ts +4 -0
- package/src/runs/shared/run-records.ts +607 -0
- package/src/runs/shared/session-cwd-preload.ts +27 -0
- package/src/runs/shared/single-output.ts +267 -0
- package/src/runs/shared/streaming-text.ts +18 -0
- package/src/runs/shared/structured-output.ts +68 -0
- package/src/runs/shared/subagent-control.ts +219 -0
- package/src/runs/shared/subagent-prompt-runtime.ts +201 -0
- package/src/runs/shared/subagent-tool-loop-guard.ts +89 -0
- package/src/runs/shared/supervisor-questions.ts +372 -0
- package/src/runs/shared/workflow-graph.ts +220 -0
- package/src/runs/shared/workflow-policy.ts +125 -0
- package/src/runs/shared/worktree.ts +635 -0
- package/src/shared/agent-context-policy.ts +184 -0
- package/src/shared/artifacts.ts +95 -0
- package/src/shared/atomic-json.ts +16 -0
- package/src/shared/cooperative.ts +21 -0
- package/src/shared/execution-cwd.ts +14 -0
- package/src/shared/file-coalescer.ts +29 -0
- package/src/shared/fork-context.ts +68 -0
- package/src/shared/formatters.ts +84 -0
- package/src/shared/journal-reader.ts +333 -0
- package/src/shared/lazy-tools.ts +36 -0
- package/src/shared/model-info.ts +81 -0
- package/src/shared/native-session.ts +8 -0
- package/src/shared/native-tui.ts +10 -0
- package/src/shared/native-typebox.ts +14 -0
- package/src/shared/post-exit-stdio-guard.ts +204 -0
- package/src/shared/prompt-sections.ts +10 -0
- package/src/shared/session-entries.ts +49 -0
- package/src/shared/session-identity.ts +18 -0
- package/src/shared/settings.ts +384 -0
- package/src/shared/status-format.ts +123 -0
- package/src/shared/temp-root.ts +107 -0
- package/src/shared/types.ts +1259 -0
- package/src/shared/ui-mode.ts +5 -0
- package/src/shared/utils.ts +342 -0
- package/src/slash/prompt-template-bridge.ts +408 -0
- package/src/slash/slash-bridge.ts +178 -0
- package/src/slash/slash-commands.ts +617 -0
- package/src/slash/slash-live-state.ts +292 -0
- package/src/tui/action-hints.ts +69 -0
- package/src/tui/agent-history.ts +210 -0
- package/src/tui/agent-view.ts +1323 -0
- package/src/tui/render.ts +1550 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import { formatRunAction } from "../shared/status-format.ts";
|
|
4
|
+
import { SUBAGENT_CHILD_ENV, SUBAGENT_FANOUT_CHILD_ENV } from "../runs/shared/pi-args.ts";
|
|
5
|
+
import {
|
|
6
|
+
type Details,
|
|
7
|
+
type IntercomEventBus,
|
|
8
|
+
type NestedRunSummary,
|
|
9
|
+
type PublicNestedRunSummary,
|
|
10
|
+
type SingleResult,
|
|
11
|
+
type SubagentResultIntercomChild,
|
|
12
|
+
type SubagentResultIntercomPayload,
|
|
13
|
+
type SubagentResultStatus,
|
|
14
|
+
type SubagentRunMode,
|
|
15
|
+
SUBAGENT_RESULT_INTERCOM_DELIVERY_EVENT,
|
|
16
|
+
SUBAGENT_RESULT_INTERCOM_EVENT,
|
|
17
|
+
} from "../shared/types.ts";
|
|
18
|
+
|
|
19
|
+
export function resolveSubagentResultStatus(input: {
|
|
20
|
+
exitCode?: number;
|
|
21
|
+
success?: boolean;
|
|
22
|
+
state?: string;
|
|
23
|
+
interrupted?: boolean;
|
|
24
|
+
detached?: boolean;
|
|
25
|
+
timedOut?: boolean;
|
|
26
|
+
acceptance?: SingleResult["acceptance"];
|
|
27
|
+
}): SubagentResultStatus {
|
|
28
|
+
if (input.detached) return "detached";
|
|
29
|
+
if (input.timedOut || input.state === "timed-out") return "timed-out";
|
|
30
|
+
if (input.interrupted || input.state === "paused") return "paused";
|
|
31
|
+
if ((input.exitCode === undefined || input.exitCode === 0) && (input.acceptance?.status === "blocked" || input.state === "blocked")) return "blocked";
|
|
32
|
+
if (typeof input.success === "boolean") return input.success ? "completed" : "failed";
|
|
33
|
+
if (input.state === "complete") return "completed";
|
|
34
|
+
if (input.state === "failed") return "failed";
|
|
35
|
+
if (typeof input.exitCode === "number") return input.exitCode === 0 ? "completed" : "failed";
|
|
36
|
+
return "failed";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function countStatuses(children: SubagentResultIntercomChild[]): Record<SubagentResultStatus, number> {
|
|
40
|
+
const counts: Record<SubagentResultStatus, number> = {
|
|
41
|
+
completed: 0,
|
|
42
|
+
failed: 0,
|
|
43
|
+
blocked: 0,
|
|
44
|
+
paused: 0,
|
|
45
|
+
detached: 0,
|
|
46
|
+
"timed-out": 0,
|
|
47
|
+
};
|
|
48
|
+
for (const child of children) {
|
|
49
|
+
counts[child.status] += 1;
|
|
50
|
+
}
|
|
51
|
+
return counts;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function formatStatusCounts(counts: Record<SubagentResultStatus, number>): string {
|
|
55
|
+
const parts = [
|
|
56
|
+
counts.completed ? `${counts.completed} completed` : undefined,
|
|
57
|
+
counts.failed ? `${counts.failed} failed` : undefined,
|
|
58
|
+
counts.blocked ? `${counts.blocked} need human action` : undefined,
|
|
59
|
+
counts.paused ? `${counts.paused} paused` : undefined,
|
|
60
|
+
counts.detached ? `${counts.detached} detached` : undefined,
|
|
61
|
+
counts["timed-out"] ? `${counts["timed-out"]} timed out` : undefined,
|
|
62
|
+
].filter((part): part is string => Boolean(part));
|
|
63
|
+
return parts.length ? parts.join(", ") : "0 results";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function resolveGroupedStatus(children: SubagentResultIntercomChild[], workflowStatus?: SubagentResultStatus): SubagentResultStatus {
|
|
67
|
+
const counts = countStatuses(children);
|
|
68
|
+
if (workflowStatus) counts[workflowStatus] += 1;
|
|
69
|
+
if (counts.failed > 0) return "failed";
|
|
70
|
+
if (counts["timed-out"] > 0) return "timed-out";
|
|
71
|
+
if (counts.blocked > 0) return "blocked";
|
|
72
|
+
if (counts.paused > 0) return "paused";
|
|
73
|
+
if (counts.completed > 0) return "completed";
|
|
74
|
+
if (counts.detached > 0) return "detached";
|
|
75
|
+
return "failed";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function compactNestedRun(run: NestedRunSummary | PublicNestedRunSummary, depth = 0): PublicNestedRunSummary {
|
|
79
|
+
return {
|
|
80
|
+
id: run.id,
|
|
81
|
+
parentRunId: run.parentRunId,
|
|
82
|
+
...(run.parentStepIndex !== undefined ? { parentStepIndex: run.parentStepIndex } : {}),
|
|
83
|
+
...(run.parentAgent ? { parentAgent: run.parentAgent } : {}),
|
|
84
|
+
depth: run.depth,
|
|
85
|
+
path: run.path.slice(0, 4).map((part) => ({
|
|
86
|
+
runId: part.runId,
|
|
87
|
+
...(part.stepIndex !== undefined ? { stepIndex: part.stepIndex } : {}),
|
|
88
|
+
...(part.agent ? { agent: part.agent } : {}),
|
|
89
|
+
})),
|
|
90
|
+
...(run.asyncDir ? { asyncDir: run.asyncDir } : {}),
|
|
91
|
+
...(run.sessionId ? { sessionId: run.sessionId } : {}),
|
|
92
|
+
...(run.sessionFile ? { sessionFile: run.sessionFile } : {}),
|
|
93
|
+
...(run.intercomTarget ? { intercomTarget: run.intercomTarget } : {}),
|
|
94
|
+
...(run.ownerIntercomTarget ? { ownerIntercomTarget: run.ownerIntercomTarget } : {}),
|
|
95
|
+
...(run.leafIntercomTarget ? { leafIntercomTarget: run.leafIntercomTarget } : {}),
|
|
96
|
+
...(run.ownerState ? { ownerState: run.ownerState } : {}),
|
|
97
|
+
...(run.mode ? { mode: run.mode } : {}),
|
|
98
|
+
state: run.state,
|
|
99
|
+
...(run.agent ? { agent: run.agent } : {}),
|
|
100
|
+
...(run.agents?.length ? { agents: run.agents.slice(0, 12) } : {}),
|
|
101
|
+
...(run.currentStep !== undefined ? { currentStep: run.currentStep } : {}),
|
|
102
|
+
...(run.chainStepCount !== undefined ? { chainStepCount: run.chainStepCount } : {}),
|
|
103
|
+
...(run.parallelGroups?.length ? { parallelGroups: run.parallelGroups.slice(0, 8) } : {}),
|
|
104
|
+
...(run.activityState ? { activityState: run.activityState } : {}),
|
|
105
|
+
...(run.lastActivityAt !== undefined ? { lastActivityAt: run.lastActivityAt } : {}),
|
|
106
|
+
...(run.currentTool ? { currentTool: run.currentTool } : {}),
|
|
107
|
+
...(run.currentToolStartedAt !== undefined ? { currentToolStartedAt: run.currentToolStartedAt } : {}),
|
|
108
|
+
...(run.currentPath ? { currentPath: run.currentPath } : {}),
|
|
109
|
+
...(run.turnCount !== undefined ? { turnCount: run.turnCount } : {}),
|
|
110
|
+
...(run.toolCount !== undefined ? { toolCount: run.toolCount } : {}),
|
|
111
|
+
...(run.totalTokens ? { totalTokens: run.totalTokens } : {}),
|
|
112
|
+
...(run.startedAt !== undefined ? { startedAt: run.startedAt } : {}),
|
|
113
|
+
...(run.endedAt !== undefined ? { endedAt: run.endedAt } : {}),
|
|
114
|
+
...(run.lastUpdate !== undefined ? { lastUpdate: run.lastUpdate } : {}),
|
|
115
|
+
...(run.error ? { error: run.error } : {}),
|
|
116
|
+
...(run.steps?.length ? { steps: run.steps.slice(0, 12).map((step) => ({
|
|
117
|
+
agent: step.agent,
|
|
118
|
+
status: step.status,
|
|
119
|
+
...(step.sessionFile ? { sessionFile: step.sessionFile } : {}),
|
|
120
|
+
...(step.activityState ? { activityState: step.activityState } : {}),
|
|
121
|
+
...(step.lastActivityAt !== undefined ? { lastActivityAt: step.lastActivityAt } : {}),
|
|
122
|
+
...(step.currentTool ? { currentTool: step.currentTool } : {}),
|
|
123
|
+
...(step.currentToolStartedAt !== undefined ? { currentToolStartedAt: step.currentToolStartedAt } : {}),
|
|
124
|
+
...(step.currentPath ? { currentPath: step.currentPath } : {}),
|
|
125
|
+
...(step.turnCount !== undefined ? { turnCount: step.turnCount } : {}),
|
|
126
|
+
...(step.toolCount !== undefined ? { toolCount: step.toolCount } : {}),
|
|
127
|
+
...(step.startedAt !== undefined ? { startedAt: step.startedAt } : {}),
|
|
128
|
+
...(step.endedAt !== undefined ? { endedAt: step.endedAt } : {}),
|
|
129
|
+
...(step.error ? { error: step.error } : {}),
|
|
130
|
+
...(depth < 2 && step.children?.length ? { children: step.children.slice(0, 8).map((child) => compactNestedRun(child, depth + 1)) } : {}),
|
|
131
|
+
})) } : {}),
|
|
132
|
+
...(depth < 2 && run.children?.length ? { children: run.children.slice(0, 8).map((child) => compactNestedRun(child, depth + 1)) } : {}),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function compactNestedResultChildren(children: Array<NestedRunSummary | PublicNestedRunSummary> | undefined): PublicNestedRunSummary[] | undefined {
|
|
137
|
+
if (!children?.length) return undefined;
|
|
138
|
+
return children.slice(0, 16).map((child) => compactNestedRun(child));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function attachNestedChildrenToResultChildren(
|
|
142
|
+
runId: string,
|
|
143
|
+
children: SubagentResultIntercomChild[],
|
|
144
|
+
nestedChildren: NestedRunSummary[] | undefined,
|
|
145
|
+
): SubagentResultIntercomChild[] {
|
|
146
|
+
const compact = compactNestedResultChildren(nestedChildren);
|
|
147
|
+
if (!compact?.length) return children.map((child) => ({ ...child, children: compactNestedResultChildren(child.children) }));
|
|
148
|
+
return children.map((child, index) => {
|
|
149
|
+
const childIndex = child.index ?? index;
|
|
150
|
+
const alreadyAttachedIds = new Set(child.children?.map((nested) => nested.id) ?? []);
|
|
151
|
+
const attached = compact.filter((nested) => nested.parentRunId === runId && nested.parentStepIndex === childIndex && !alreadyAttachedIds.has(nested.id));
|
|
152
|
+
const fallbackAttached = children.length === 1
|
|
153
|
+
? compact.filter((nested) => nested.parentRunId === runId && nested.parentStepIndex === undefined && !alreadyAttachedIds.has(nested.id))
|
|
154
|
+
: [];
|
|
155
|
+
const merged = compactNestedResultChildren([...(child.children ?? []), ...attached, ...fallbackAttached]);
|
|
156
|
+
return merged?.length ? { ...child, children: merged } : { ...child, children: undefined };
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function formatNestedResultLines(children: PublicNestedRunSummary[] | undefined): string[] {
|
|
161
|
+
if (!children?.length) return [];
|
|
162
|
+
const lines = ["Nested subagents:"];
|
|
163
|
+
let remaining = 10;
|
|
164
|
+
const append = (runs: PublicNestedRunSummary[] | undefined, indent: string): void => {
|
|
165
|
+
for (const run of runs ?? []) {
|
|
166
|
+
if (remaining <= 0) {
|
|
167
|
+
lines.push(`${indent}↳ +more nested runs; inspect status for full tree`);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
remaining--;
|
|
171
|
+
const label = run.agent ?? run.agents?.join("+") ?? run.id;
|
|
172
|
+
lines.push(`${indent}↳ ${label} — ${run.state} [${run.id}]`);
|
|
173
|
+
if (run.sessionFile) lines.push(`${indent} Session: ${run.sessionFile}`);
|
|
174
|
+
append(run.children, `${indent} `);
|
|
175
|
+
for (const step of run.steps ?? []) append(step.children, `${indent} `);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
append(children, "");
|
|
179
|
+
return lines;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface GroupedResultIntercomMessageInput {
|
|
183
|
+
completionId?: string;
|
|
184
|
+
to: string;
|
|
185
|
+
runId: string;
|
|
186
|
+
mode: SubagentRunMode;
|
|
187
|
+
source: "foreground" | "async";
|
|
188
|
+
children: SubagentResultIntercomChild[];
|
|
189
|
+
status?: SubagentResultStatus;
|
|
190
|
+
error?: string;
|
|
191
|
+
resultPath?: string;
|
|
192
|
+
asyncId?: string;
|
|
193
|
+
asyncDir?: string;
|
|
194
|
+
chainSteps?: number;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function asyncResumeGuidance(input: {
|
|
198
|
+
source: "foreground" | "async";
|
|
199
|
+
children: SubagentResultIntercomChild[];
|
|
200
|
+
asyncId?: string;
|
|
201
|
+
}): string | undefined {
|
|
202
|
+
if (input.source !== "async" || !input.asyncId) return undefined;
|
|
203
|
+
const childSafe = process.env[SUBAGENT_CHILD_ENV] === "1" && process.env[SUBAGENT_FANOUT_CHILD_ENV] === "1";
|
|
204
|
+
const resumable = input.children.filter((child) => typeof child.sessionPath === "string" && fs.existsSync(child.sessionPath));
|
|
205
|
+
if (input.children.length === 1 && resumable.length === 1) {
|
|
206
|
+
return `Continue: ${formatRunAction("resume", input.asyncId, { message: "..." }, childSafe)}`;
|
|
207
|
+
}
|
|
208
|
+
if (resumable.length > 0) {
|
|
209
|
+
const firstIndex = resumable[0]?.index ?? input.children.indexOf(resumable[0]!);
|
|
210
|
+
return `Continue child: ${formatRunAction("resume", input.asyncId, { index: firstIndex, message: "..." }, childSafe)}`;
|
|
211
|
+
}
|
|
212
|
+
return "Resume: unavailable; no child session file was persisted.";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function formatSubagentResultIntercomMessage(input: {
|
|
216
|
+
runId: string;
|
|
217
|
+
mode: SubagentRunMode;
|
|
218
|
+
status: SubagentResultStatus;
|
|
219
|
+
error?: string;
|
|
220
|
+
source: "foreground" | "async";
|
|
221
|
+
children: SubagentResultIntercomChild[];
|
|
222
|
+
resultPath?: string;
|
|
223
|
+
asyncId?: string;
|
|
224
|
+
asyncDir?: string;
|
|
225
|
+
chainSteps?: number;
|
|
226
|
+
}): string {
|
|
227
|
+
const counts = countStatuses(input.children);
|
|
228
|
+
const lines: string[] = [
|
|
229
|
+
"subagent results",
|
|
230
|
+
"",
|
|
231
|
+
`Run: ${input.runId}`,
|
|
232
|
+
`Mode: ${input.mode}`,
|
|
233
|
+
`Status: ${input.status}`,
|
|
234
|
+
`Children: ${formatStatusCounts(counts)}`,
|
|
235
|
+
...(input.error ? [`Workflow ${input.status === "paused" ? "paused" : "error"}: ${input.error}`] : []),
|
|
236
|
+
];
|
|
237
|
+
if (input.source === "foreground" && input.status === "completed") {
|
|
238
|
+
lines.push("This completes the matching subagent call. Continue the parent task without relaunching the same call.");
|
|
239
|
+
}
|
|
240
|
+
if (input.mode === "chain" && typeof input.chainSteps === "number") {
|
|
241
|
+
lines.push(`Chain steps: ${input.chainSteps}`);
|
|
242
|
+
}
|
|
243
|
+
if (input.resultPath) lines.push(`Saved result (acceptance details when configured): ${input.resultPath}`);
|
|
244
|
+
if (input.asyncId) lines.push(`Async id: ${input.asyncId}`);
|
|
245
|
+
if (input.asyncDir) lines.push(`Async dir: ${input.asyncDir}`);
|
|
246
|
+
const resumeGuidance = asyncResumeGuidance(input);
|
|
247
|
+
if (resumeGuidance) lines.push(resumeGuidance);
|
|
248
|
+
if (input.children.some((child) => child.intercomTarget)) {
|
|
249
|
+
lines.push("");
|
|
250
|
+
lines.push(input.source === "async"
|
|
251
|
+
? "Previous intercom targets below identify child sessions used while they were running. Inspect artifacts or session logs if resume is unavailable."
|
|
252
|
+
: "Intercom targets below identify child sessions used while they were running; completed child sessions may no longer be reachable. Inspect artifacts or session logs for follow-up.");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
for (let index = 0; index < input.children.length; index++) {
|
|
256
|
+
const child = input.children[index]!;
|
|
257
|
+
lines.push("");
|
|
258
|
+
lines.push(`${index + 1}. ${child.agent} — ${child.status}`);
|
|
259
|
+
if (child.intercomTarget) lines.push(`${input.source === "async" ? "Previous intercom target" : "Run intercom target"}: ${child.intercomTarget}`);
|
|
260
|
+
if (child.artifactPath) lines.push(`Output artifact: ${child.artifactPath}`);
|
|
261
|
+
if (child.metadataPath) lines.push(`Result metadata (acceptance details when configured): ${child.metadataPath}${fs.existsSync(child.metadataPath) ? "" : " (missing)"}`);
|
|
262
|
+
if (child.sessionPath) lines.push(`Session: ${child.sessionPath}`);
|
|
263
|
+
lines.push(...formatNestedResultLines(child.children));
|
|
264
|
+
lines.push("Summary:");
|
|
265
|
+
lines.push(child.summary);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return lines.join("\n");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function buildSubagentResultIntercomPayload(input: GroupedResultIntercomMessageInput): SubagentResultIntercomPayload {
|
|
272
|
+
const children = input.children.map((child) => ({
|
|
273
|
+
...child,
|
|
274
|
+
summary: child.summary.trim() || "(no output)",
|
|
275
|
+
children: compactNestedResultChildren(child.children),
|
|
276
|
+
}));
|
|
277
|
+
const status = resolveGroupedStatus(children, input.status);
|
|
278
|
+
const summary = formatStatusCounts(countStatuses(children));
|
|
279
|
+
const firstChild = children[0];
|
|
280
|
+
const payload: SubagentResultIntercomPayload = {
|
|
281
|
+
...(input.completionId ? { completionId: input.completionId } : {}),
|
|
282
|
+
to: input.to,
|
|
283
|
+
runId: input.runId,
|
|
284
|
+
mode: input.mode,
|
|
285
|
+
status,
|
|
286
|
+
summary,
|
|
287
|
+
...(input.error ? { error: input.error } : {}),
|
|
288
|
+
source: input.source,
|
|
289
|
+
children,
|
|
290
|
+
...(input.resultPath ? { resultPath: input.resultPath } : {}),
|
|
291
|
+
...(input.asyncId ? { asyncId: input.asyncId } : {}),
|
|
292
|
+
...(input.asyncDir ? { asyncDir: input.asyncDir } : {}),
|
|
293
|
+
...(typeof input.chainSteps === "number" ? { chainSteps: input.chainSteps } : {}),
|
|
294
|
+
...(firstChild?.agent ? { agent: firstChild.agent } : {}),
|
|
295
|
+
...(firstChild?.index !== undefined ? { index: firstChild.index } : {}),
|
|
296
|
+
...(firstChild?.artifactPath ? { artifactPath: firstChild.artifactPath } : {}),
|
|
297
|
+
...(firstChild?.sessionPath ? { sessionPath: firstChild.sessionPath } : {}),
|
|
298
|
+
message: "",
|
|
299
|
+
};
|
|
300
|
+
payload.message = formatSubagentResultIntercomMessage(payload);
|
|
301
|
+
return payload;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export async function deliverSubagentResultIntercomEvent(
|
|
305
|
+
events: IntercomEventBus,
|
|
306
|
+
payload: SubagentResultIntercomPayload,
|
|
307
|
+
timeoutMs = 500,
|
|
308
|
+
): Promise<boolean> {
|
|
309
|
+
return deliverSubagentIntercomMessageEvent(events, payload.to, payload.message, timeoutMs, { ...payload });
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export async function deliverSubagentIntercomMessageEvent(
|
|
313
|
+
events: IntercomEventBus,
|
|
314
|
+
to: string,
|
|
315
|
+
message: string,
|
|
316
|
+
timeoutMs = 500,
|
|
317
|
+
extra: Record<string, unknown> = {},
|
|
318
|
+
): Promise<boolean> {
|
|
319
|
+
if (typeof events.on !== "function" || typeof events.emit !== "function") return false;
|
|
320
|
+
const requestId = typeof extra.requestId === "string" ? extra.requestId : randomUUID();
|
|
321
|
+
return new Promise((resolve) => {
|
|
322
|
+
let settled = false;
|
|
323
|
+
let unsubscribe: (() => void) | undefined;
|
|
324
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
325
|
+
const finish = (delivered: boolean) => {
|
|
326
|
+
if (settled) return;
|
|
327
|
+
settled = true;
|
|
328
|
+
if (timer) clearTimeout(timer);
|
|
329
|
+
unsubscribe?.();
|
|
330
|
+
resolve(delivered);
|
|
331
|
+
};
|
|
332
|
+
unsubscribe = events.on(SUBAGENT_RESULT_INTERCOM_DELIVERY_EVENT, (data) => {
|
|
333
|
+
if (!data || typeof data !== "object") return;
|
|
334
|
+
const delivery = data as { requestId?: unknown; delivered?: unknown };
|
|
335
|
+
if (delivery.requestId !== requestId) return;
|
|
336
|
+
finish(delivery.delivered === true);
|
|
337
|
+
});
|
|
338
|
+
timer = setTimeout(() => finish(false), timeoutMs);
|
|
339
|
+
try {
|
|
340
|
+
events.emit(SUBAGENT_RESULT_INTERCOM_EVENT, { ...extra, to, message, requestId });
|
|
341
|
+
} catch {
|
|
342
|
+
finish(false);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function stripSingleResultOutputs(result: SingleResult): SingleResult {
|
|
348
|
+
return {
|
|
349
|
+
...result,
|
|
350
|
+
messages: undefined,
|
|
351
|
+
finalOutput: undefined,
|
|
352
|
+
truncation: undefined,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export function stripDetailsOutputsForIntercomReceipt(details: Details, delivery?: Details["intercomDelivery"]): Details {
|
|
357
|
+
return {
|
|
358
|
+
...details,
|
|
359
|
+
results: details.results.map(stripSingleResultOutputs),
|
|
360
|
+
...(delivery ? { intercomDelivery: delivery } : {}),
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function compactReceiptSummary(summary: string): string {
|
|
365
|
+
const withoutOutput = summary.split(/\n\nOutput:\n|\sOutput:\s/)[0] ?? summary;
|
|
366
|
+
const normalized = withoutOutput.replace(/\s+/g, " ").trim();
|
|
367
|
+
return normalized.length > 240 ? `${normalized.slice(0, 239)}…` : normalized;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function formatSubagentResultReceipt(input: {
|
|
371
|
+
mode: SubagentRunMode;
|
|
372
|
+
runId: string;
|
|
373
|
+
payload: SubagentResultIntercomPayload;
|
|
374
|
+
}): string {
|
|
375
|
+
const counts = countStatuses(input.payload.children);
|
|
376
|
+
const modeLabel = input.mode === "single"
|
|
377
|
+
? "single subagent result"
|
|
378
|
+
: input.mode === "parallel"
|
|
379
|
+
? "parallel subagent results"
|
|
380
|
+
: "chain subagent results";
|
|
381
|
+
const lines = [
|
|
382
|
+
`Delivered ${modeLabel} via intercom.`,
|
|
383
|
+
"Delivery: succeeded",
|
|
384
|
+
`Run: ${input.runId}`,
|
|
385
|
+
`Child outcome: ${input.payload.status}`,
|
|
386
|
+
`Children: ${formatStatusCounts(counts)}`,
|
|
387
|
+
...(input.payload.error ? [`Workflow ${input.payload.status === "paused" ? "paused" : "error"}: ${input.payload.error}`] : []),
|
|
388
|
+
];
|
|
389
|
+
|
|
390
|
+
if (input.payload.resultPath) lines.push(`Saved result (acceptance details when configured): ${input.payload.resultPath}`);
|
|
391
|
+
for (const child of input.payload.children) {
|
|
392
|
+
if (child.metadataPath) lines.push(`Result metadata (${child.agent}; acceptance details when configured): ${child.metadataPath}${fs.existsSync(child.metadataPath) ? "" : " (missing)"}`);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const artifacts = input.payload.children.filter((child) => typeof child.artifactPath === "string");
|
|
396
|
+
if (artifacts.length > 0) {
|
|
397
|
+
lines.push("Artifacts:");
|
|
398
|
+
for (const child of artifacts) {
|
|
399
|
+
lines.push(`- ${child.agent} [${child.status}]: ${child.artifactPath}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const intercomTargets = input.payload.children.filter((child) => typeof child.intercomTarget === "string");
|
|
404
|
+
if (intercomTargets.length > 0) {
|
|
405
|
+
lines.push("Run intercom targets (may be inactive after completion):");
|
|
406
|
+
for (const child of intercomTargets) {
|
|
407
|
+
lines.push(`- ${child.agent} [${child.status}]: ${child.intercomTarget}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const sessions = input.payload.children.filter((child) => typeof child.sessionPath === "string");
|
|
412
|
+
if (sessions.length > 0) {
|
|
413
|
+
lines.push("Sessions:");
|
|
414
|
+
for (const child of sessions) {
|
|
415
|
+
lines.push(`- ${child.agent} [${child.status}]: ${child.sessionPath}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const nonCompleted = input.payload.children.filter((child) => child.status !== "completed");
|
|
420
|
+
if (nonCompleted.length > 0) {
|
|
421
|
+
lines.push("Non-completed children:");
|
|
422
|
+
for (const child of nonCompleted) {
|
|
423
|
+
lines.push(`- ${child.agent} [${child.status}]: ${compactReceiptSummary(child.summary)}`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
lines.push("Full grouped output was sent over intercom.");
|
|
428
|
+
return lines.join("\n");
|
|
429
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { homedir } from "os";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
|
|
4
|
+
export function getPiAgentDir(): string {
|
|
5
|
+
const configured = process.env.PI_CODING_AGENT_DIR?.trim();
|
|
6
|
+
if (configured === "~") return homedir();
|
|
7
|
+
if (configured?.startsWith("~/")) return join(homedir(), configured.slice(2));
|
|
8
|
+
return configured || join(homedir(), ".pi", "agent");
|
|
9
|
+
}
|