@xzzpig/pi-subagents 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +477 -0
- package/README.md +2 -2
- package/agents/claude-code-writer.md +15 -0
- package/agents/claude-code.md +15 -0
- package/agents/codex-exec-writer.md +15 -0
- package/agents/codex-exec.md +15 -0
- package/agents/cursor-agent-writer.md +14 -0
- package/agents/cursor-agent.md +14 -0
- package/agents/reviewer.md +1 -1
- package/agents/scout.md +1 -1
- package/docs/agents.md +195 -39
- package/docs/configuration.md +81 -22
- package/docs/extension-api.md +69 -14
- package/docs/missions.md +2 -0
- package/docs/models.md +32 -9
- package/docs/observability.md +22 -20
- package/docs/tool-reference.md +123 -14
- package/docs/watchdog.md +92 -114
- package/docs/workflows.md +142 -4
- package/install.mjs +1 -1
- package/package.json +7 -6
- package/prompts/review-loop.md +2 -2
- package/skills/council-mode/SKILL.md +48 -243
- package/skills/council-mode/references/pass-contracts.md +150 -0
- package/skills/pi-subagents/SKILL.md +89 -35
- package/skills/pi-subagents/references/constraints-and-recipes.md +30 -234
- package/skills/pi-subagents/references/execution-controls.md +91 -15
- package/skills/pi-subagents/references/management-authoring-rpc.md +5 -5
- package/skills/pi-subagents/references/multi-lane-orchestration.md +13 -1
- package/skills/pi-subagents/references/prompting-and-roles.md +59 -31
- package/skills/pi-subagents/references/review-and-validation.md +73 -0
- package/src/agents/agent-management.ts +384 -115
- package/src/agents/agent-refinements.ts +4 -4
- package/src/agents/agent-serializer.ts +14 -2
- package/src/agents/agents.ts +935 -372
- package/src/agents/builtin-names.ts +6 -0
- package/src/agents/runtime-agent-events.ts +70 -0
- package/src/agents/runtime-agent-registry.ts +42 -21
- package/src/api/agents.ts +10 -5
- package/src/api/background-work.ts +12 -3
- package/src/api/capability-ceiling.ts +0 -1
- package/src/api/{pi-args.ts → child-tool-plan.ts} +1 -1
- package/src/api/delegation.ts +0 -7
- package/src/api/external-runs.ts +67 -4
- package/src/api/preflight.ts +77 -21
- package/src/api/shared-types.ts +6 -0
- package/src/extension/config.ts +90 -0
- package/src/extension/doctor.ts +5 -13
- package/src/extension/fanout-child.ts +14 -14
- package/src/extension/index.ts +115 -37
- package/src/extension/public-execution.ts +103 -15
- package/src/extension/rpc.ts +120 -25
- package/src/extension/schemas.ts +65 -29
- package/src/extension/tool-description.ts +48 -26
- package/src/inspectors/herdr/actions.ts +2 -1
- package/src/inspectors/herdr/inspector-runner.ts +21 -23
- package/src/inspectors/herdr/session-roots-codec.ts +42 -0
- package/src/integrations/herdr-status.ts +51 -3
- package/src/intercom/native-supervisor-channel.ts +138 -60
- package/src/intercom/result-intercom.ts +7 -0
- package/src/intercom/supervisor-ui.ts +243 -0
- package/src/profiles/profiles.ts +5 -6
- package/src/runs/background/active-async-capacity.ts +103 -18
- package/src/runs/background/async-execution.ts +417 -133
- package/src/runs/background/async-job-tracker.ts +95 -15
- package/src/runs/background/async-resume.ts +60 -28
- package/src/runs/background/async-retention.ts +20 -3
- package/src/runs/background/async-status-snapshot.ts +23 -261
- package/src/runs/background/async-status.ts +204 -14
- package/src/runs/background/auto-drain.ts +1 -1
- package/src/runs/background/chain-append.ts +8 -3
- package/src/runs/background/chain-root-attachment.ts +74 -8
- package/src/runs/background/control-channel.ts +4 -205
- package/src/runs/background/fleet-view.ts +38 -22
- package/src/runs/background/inspect-rpc.ts +8 -8
- package/src/runs/background/notify.ts +238 -12
- package/src/runs/background/process-terminal.ts +16 -0
- package/src/runs/background/result-delivery-ownership.ts +45 -0
- package/src/runs/background/result-files.ts +28 -14
- package/src/runs/background/result-watcher.ts +39 -16
- package/src/runs/background/resume-guidance.ts +2 -2
- package/src/runs/background/retained-children.ts +1 -1
- package/src/runs/background/run-child-session.ts +613 -0
- package/src/runs/background/run-status.ts +84 -17
- package/src/runs/background/runner-aliases.ts +125 -0
- package/src/runs/background/runner-child-sessions.ts +31 -0
- package/src/runs/background/scheduled-runs.ts +174 -18
- package/src/runs/background/stale-run-reconciler.ts +10 -4
- package/src/runs/background/steering.ts +8 -15
- package/src/runs/background/subagent-runner.ts +718 -1161
- package/src/runs/background/subagent-wait.ts +86 -31
- package/src/runs/background/terminal-run-index.ts +1 -1
- package/src/runs/background/wait-completions.ts +43 -2
- package/src/runs/background/wait-config.ts +23 -9
- package/src/runs/background/wait-tool.ts +26 -19
- package/src/runs/foreground/async-steering-action.ts +3 -19
- package/src/runs/foreground/execution.ts +489 -499
- package/src/runs/foreground/foreground-control.ts +13 -0
- package/src/runs/foreground/foreground-history.ts +23 -1
- package/src/runs/foreground/subagent-executor.ts +1223 -402
- package/src/runs/foreground/workflow-detach-reconcile.ts +140 -115
- package/src/runs/foreground/workflow-foreground-steering.ts +24 -98
- package/src/runs/shared/abort-recovery.ts +119 -0
- package/src/runs/shared/acceptance.ts +138 -36
- package/src/runs/shared/async-status-projection.ts +565 -0
- package/src/runs/shared/background-process-options.ts +9 -0
- package/src/runs/shared/capability-ceiling.ts +2 -2
- package/src/runs/shared/child-hooks.ts +25 -0
- package/src/runs/shared/child-identity.ts +32 -6
- package/src/runs/shared/child-launch-plan.ts +151 -0
- package/src/runs/shared/child-launch.ts +364 -0
- package/src/runs/shared/child-lifecycle.ts +25 -0
- package/src/runs/shared/child-runtime-config.ts +126 -0
- package/src/runs/shared/child-session.ts +366 -0
- package/src/runs/shared/child-tool-plan.ts +632 -0
- package/src/runs/shared/claude-code-adapter.ts +133 -0
- package/src/runs/shared/codex-exec-adapter.ts +129 -0
- package/src/runs/shared/completion-evidence.ts +89 -0
- package/src/runs/shared/completion-guard.ts +6 -5
- package/src/runs/shared/cursor-agent-adapter.ts +114 -0
- package/src/runs/shared/dynamic-fanout.ts +3 -3
- package/src/runs/shared/external-cli-contract.ts +167 -0
- package/src/runs/shared/external-cli-preflight.ts +138 -0
- package/src/runs/shared/external-cli-runner.ts +348 -55
- package/src/runs/shared/fast-mode-extension.ts +5 -5
- package/src/runs/shared/host-step-status.ts +231 -0
- package/src/runs/shared/lane-metadata.ts +126 -0
- package/src/runs/shared/launch-cwd.ts +16 -0
- package/src/runs/shared/long-running-guard.ts +2 -1
- package/src/runs/shared/mcp-config-sources.ts +422 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +222 -158
- package/src/runs/shared/mcp-direct-tool-grant.ts +194 -0
- package/src/runs/shared/model-exclusions.ts +139 -9
- package/src/runs/shared/model-fallback.ts +146 -14
- package/src/runs/shared/mutation-evidence.ts +57 -3
- package/src/runs/shared/nested-events.ts +99 -77
- package/src/runs/shared/nested-path.ts +0 -14
- package/src/runs/shared/nested-render.ts +9 -5
- package/src/runs/shared/orca-progress-tabs.ts +12 -7
- package/src/runs/shared/parallel-handoff.ts +423 -7
- package/src/runs/shared/parallel-utils.ts +20 -7
- package/src/runs/shared/permissions.ts +1 -14
- package/src/runs/shared/pi-spawn.ts +69 -35
- package/src/runs/shared/process-signal.ts +16 -0
- package/src/runs/shared/run-fanout-budget.ts +0 -13
- package/src/runs/shared/run-history.ts +21 -1
- package/src/runs/shared/runtime-acknowledged-extensions.ts +0 -27
- package/src/runs/shared/sandbox-profile-guard.ts +85 -0
- package/src/runs/shared/sandbox-startup-diagnostics.ts +54 -0
- package/src/runs/shared/single-output.ts +89 -22
- package/src/runs/shared/structured-output.ts +49 -9
- package/src/runs/shared/subagent-control.ts +6 -2
- package/src/runs/shared/subagent-prompt-runtime.ts +188 -365
- package/src/runs/shared/task-intent.ts +21 -7
- package/src/runs/shared/tool-availability.ts +18 -62
- package/src/runs/shared/tool-budget.ts +0 -14
- package/src/runs/shared/tool-timeout.ts +1 -1
- package/src/runs/shared/workflow-graph.ts +16 -0
- package/src/runs/shared/worktree-cleanup-plan.ts +866 -0
- package/src/runs/shared/worktree.ts +571 -62
- package/src/shared/atomic-json.ts +3 -1
- package/src/shared/child-session-name.ts +46 -0
- package/src/shared/extension-context.ts +24 -0
- package/src/shared/fork-context.ts +21 -12
- package/src/shared/fork-session-cwd.ts +27 -0
- package/src/shared/formatters.ts +18 -3
- package/src/shared/jsonl-writer.ts +11 -0
- package/src/shared/launch-contract.ts +11 -1
- package/src/shared/pruned-fork.ts +450 -0
- package/src/shared/sandbox-profile.ts +23 -0
- package/src/shared/session-file-trust.ts +19 -0
- package/src/shared/session-tokens.ts +14 -3
- package/src/shared/settings.ts +19 -105
- package/src/shared/shortcuts.ts +17 -0
- package/src/shared/thinking-ceiling.ts +0 -6
- package/src/shared/types.ts +545 -70
- package/src/shared/utils.ts +60 -93
- package/src/shared/workflow-child-permit.ts +207 -0
- package/src/slash/delegation-adapters.ts +1 -9
- package/src/slash/delegation-request.ts +0 -4
- package/src/slash/prompt-template-bridge.ts +37 -1
- package/src/slash/slash-bridge.ts +1 -2
- package/src/slash/slash-commands.ts +383 -116
- package/src/slash/slash-live-state.ts +22 -11
- package/src/slash/subagents-admin.ts +29 -12
- package/src/tui/fleet-native-transcript.ts +4 -0
- package/src/tui/fleet-status.ts +217 -82
- package/src/tui/fleet-transcript.ts +11 -5
- package/src/tui/fleet.ts +54 -28
- package/src/tui/render-helpers.ts +31 -0
- package/src/tui/render.ts +1429 -163
- package/src/watchdog/change-signature.ts +40 -1
- package/src/watchdog/child-status.ts +54 -34
- package/src/watchdog/diff-tool.ts +77 -0
- package/src/watchdog/emission-guard.ts +5 -3
- package/src/watchdog/guidance.ts +20 -0
- package/src/watchdog/register-child.ts +28 -25
- package/src/watchdog/register-main.ts +10 -9
- package/src/watchdog/render.ts +4 -5
- package/src/watchdog/review.ts +15 -4
- package/src/watchdog/rules.ts +70 -0
- package/src/watchdog/runtime.ts +75 -92
- package/src/watchdog/scope.ts +0 -11
- package/src/watchdog/settings.ts +48 -104
- package/src/watchdog/turn-delta.ts +1 -1
- package/src/watchdog/types.ts +18 -32
- package/src/watchdog/warning-format.ts +0 -1
- package/src/workflows/chat-progress.ts +7 -3
- package/src/workflows/host-command.ts +235 -0
- package/src/workflows/scripted-workflow.ts +988 -59
- package/src/workflows/workflow-checklist.ts +439 -0
- package/src/workflows/workflow-child-summary.ts +121 -0
- package/src/workflows/workflow-preflight.ts +297 -0
- package/src/workflows/workflow-receipt.ts +232 -6
- package/src/workflows/workflow-resources.ts +150 -0
- package/src/workflows/workflow-settlement.ts +246 -0
- package/src/runs/shared/child-protocol.ts +0 -401
- package/src/runs/shared/pi-args.ts +0 -935
- package/src/runs/shared/subagent-startup-retry.ts +0 -116
- package/src/runs/shared/turn-budget.ts +0 -98
- package/src/shared/post-exit-stdio-guard.ts +0 -85
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import type { AsyncJobStep, HostStepNodeV1, WorkflowGraphNode, WorkflowGraphSnapshot, WorkflowPreflightLaneV1, WorkflowPreflightV1 } from "../shared/types.ts";
|
|
2
|
+
import { sanitizeDisplayText } from "../shared/display-text.ts";
|
|
3
|
+
import { workflowPreflightLaneForRuntimeKey } from "./workflow-preflight.ts";
|
|
4
|
+
|
|
5
|
+
export type WorkflowChecklistState = "complete" | "running" | "queued" | "blocked" | "failed" | "paused" | "stopped";
|
|
6
|
+
|
|
7
|
+
export interface WorkflowChecklistStep {
|
|
8
|
+
key?: string;
|
|
9
|
+
workflowKey?: string;
|
|
10
|
+
runId?: string;
|
|
11
|
+
label?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
phase?: string;
|
|
14
|
+
agent?: string;
|
|
15
|
+
status: string;
|
|
16
|
+
context?: "fresh" | "fork";
|
|
17
|
+
activityState?: string;
|
|
18
|
+
startedAt?: number;
|
|
19
|
+
endedAt?: number;
|
|
20
|
+
durationMs?: number;
|
|
21
|
+
currentTool?: string;
|
|
22
|
+
currentToolStartedAt?: number;
|
|
23
|
+
currentPath?: string;
|
|
24
|
+
turnCount?: number;
|
|
25
|
+
toolCount?: number;
|
|
26
|
+
outputName?: string;
|
|
27
|
+
error?: string;
|
|
28
|
+
toolBudgetBlocked?: boolean;
|
|
29
|
+
turnBudgetExceeded?: boolean;
|
|
30
|
+
timedOut?: boolean;
|
|
31
|
+
stopped?: boolean;
|
|
32
|
+
acceptance?: { status?: string; reviewResult?: { status?: string } };
|
|
33
|
+
review?: { status?: string };
|
|
34
|
+
watchdog?: { phase?: string };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface WorkflowChecklistTraceEntry {
|
|
38
|
+
operation?: string;
|
|
39
|
+
key: string;
|
|
40
|
+
state: string;
|
|
41
|
+
agent?: string;
|
|
42
|
+
runId?: string;
|
|
43
|
+
phase?: string;
|
|
44
|
+
label?: string;
|
|
45
|
+
generatedLaneKey?: string;
|
|
46
|
+
durationMs?: number;
|
|
47
|
+
error?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface WorkflowChecklistItem {
|
|
51
|
+
key: string;
|
|
52
|
+
label: string;
|
|
53
|
+
phase: string;
|
|
54
|
+
state: WorkflowChecklistState;
|
|
55
|
+
agent?: string;
|
|
56
|
+
context?: "fresh" | "fork";
|
|
57
|
+
startedAt?: number;
|
|
58
|
+
durationMs?: number;
|
|
59
|
+
currentTool?: string;
|
|
60
|
+
currentToolStartedAt?: number;
|
|
61
|
+
currentPath?: string;
|
|
62
|
+
toolCount?: number;
|
|
63
|
+
outputName?: string;
|
|
64
|
+
error?: string;
|
|
65
|
+
preflight?: WorkflowPreflightLaneV1;
|
|
66
|
+
kind?: "child" | "host";
|
|
67
|
+
monitorKind?: HostStepNodeV1["monitorKind"];
|
|
68
|
+
provider?: string;
|
|
69
|
+
role?: string;
|
|
70
|
+
verdict?: HostStepNodeV1["verdict"];
|
|
71
|
+
target?: string;
|
|
72
|
+
reasonCode?: string;
|
|
73
|
+
stale?: boolean;
|
|
74
|
+
reportPath?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface WorkflowChecklistPhase {
|
|
78
|
+
key: string;
|
|
79
|
+
label: string;
|
|
80
|
+
state: WorkflowChecklistState;
|
|
81
|
+
items: WorkflowChecklistItem[];
|
|
82
|
+
total: number;
|
|
83
|
+
done: number;
|
|
84
|
+
running: number;
|
|
85
|
+
queued: number;
|
|
86
|
+
blocked: number;
|
|
87
|
+
failed: number;
|
|
88
|
+
paused: number;
|
|
89
|
+
stopped: number;
|
|
90
|
+
parallel: boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface WorkflowChecklistProjection {
|
|
94
|
+
phases: WorkflowChecklistPhase[];
|
|
95
|
+
total: number;
|
|
96
|
+
done: number;
|
|
97
|
+
running: number;
|
|
98
|
+
queued: number;
|
|
99
|
+
blocked: number;
|
|
100
|
+
failed: number;
|
|
101
|
+
paused: number;
|
|
102
|
+
stopped: number;
|
|
103
|
+
bottleneck?: WorkflowChecklistItem;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface WorkflowChecklistInput {
|
|
107
|
+
graph?: WorkflowGraphSnapshot;
|
|
108
|
+
steps?: readonly WorkflowChecklistStep[] | readonly AsyncJobStep[];
|
|
109
|
+
hostSteps?: readonly HostStepNodeV1[];
|
|
110
|
+
preflight?: WorkflowPreflightV1;
|
|
111
|
+
trace?: readonly WorkflowChecklistTraceEntry[];
|
|
112
|
+
now?: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const MAX_TEXT = 160;
|
|
116
|
+
const TERMINAL_STATES = new Set<WorkflowChecklistState>(["complete", "blocked", "failed", "paused", "stopped"]);
|
|
117
|
+
|
|
118
|
+
function text(value: unknown, fallback?: string): string | undefined {
|
|
119
|
+
if (typeof value !== "string") return fallback;
|
|
120
|
+
const clean = sanitizeDisplayText(value).replace(/\s+/g, " ").trim();
|
|
121
|
+
return clean ? clean.slice(0, MAX_TEXT) : fallback;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function keyText(value: unknown, fallback: string): string {
|
|
125
|
+
return text(value, fallback) ?? fallback;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function normalizedStatus(value: unknown): string {
|
|
129
|
+
return typeof value === "string" ? value.toLowerCase() : "queued";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function finite(value: unknown): number | undefined {
|
|
133
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function count(value: unknown): number | undefined {
|
|
137
|
+
const number = finite(value);
|
|
138
|
+
return number !== undefined && number >= 0 ? Math.round(number) : undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function explicitBlocked(source: Pick<WorkflowChecklistStep, "activityState" | "toolBudgetBlocked" | "turnBudgetExceeded" | "timedOut" | "acceptance" | "review" | "watchdog"> & { verdict?: unknown; stale?: unknown }): boolean {
|
|
142
|
+
const acceptance = normalizedStatus(source.acceptance?.status);
|
|
143
|
+
const review = normalizedStatus(source.review?.status ?? source.acceptance?.reviewResult?.status);
|
|
144
|
+
return source.toolBudgetBlocked === true
|
|
145
|
+
|| source.turnBudgetExceeded === true
|
|
146
|
+
|| source.activityState === "needs_attention"
|
|
147
|
+
|| source.timedOut === true
|
|
148
|
+
|| source.watchdog?.phase === "stale"
|
|
149
|
+
|| source.stale === true
|
|
150
|
+
|| source.verdict === "inconclusive"
|
|
151
|
+
|| acceptance === "rejected"
|
|
152
|
+
|| acceptance === "blockers"
|
|
153
|
+
|| review === "blockers"
|
|
154
|
+
|| review === "review-required";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function checklistState(source: Pick<WorkflowChecklistStep, "status" | "activityState" | "toolBudgetBlocked" | "turnBudgetExceeded" | "timedOut" | "acceptance" | "review" | "watchdog"> & { verdict?: unknown; stale?: unknown }): WorkflowChecklistState {
|
|
158
|
+
if (explicitBlocked(source)) return "blocked";
|
|
159
|
+
switch (normalizedStatus(source.status)) {
|
|
160
|
+
case "complete":
|
|
161
|
+
case "completed":
|
|
162
|
+
case "done":
|
|
163
|
+
case "pass":
|
|
164
|
+
case "accepted": return "complete";
|
|
165
|
+
case "running":
|
|
166
|
+
case "started":
|
|
167
|
+
case "active": return "running";
|
|
168
|
+
case "failed":
|
|
169
|
+
case "error":
|
|
170
|
+
case "fail": return "failed";
|
|
171
|
+
case "blocked":
|
|
172
|
+
case "rejected":
|
|
173
|
+
case "partial":
|
|
174
|
+
case "needs_attention": return "blocked";
|
|
175
|
+
case "paused":
|
|
176
|
+
case "detached": return "paused";
|
|
177
|
+
case "stopped":
|
|
178
|
+
case "cancelled":
|
|
179
|
+
case "canceled": return "stopped";
|
|
180
|
+
default: return "queued";
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function duration(step: Pick<WorkflowChecklistStep, "durationMs" | "startedAt" | "endedAt">, now: number | undefined, state: WorkflowChecklistState): number | undefined {
|
|
185
|
+
const explicit = finite(step.durationMs);
|
|
186
|
+
const startedAt = finite(step.startedAt);
|
|
187
|
+
if (explicit !== undefined) return Math.max(0, explicit);
|
|
188
|
+
if (startedAt === undefined) return undefined;
|
|
189
|
+
const end = state === "running" ? now : finite(step.endedAt) ?? now;
|
|
190
|
+
return end === undefined ? undefined : Math.max(0, end - startedAt);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function laneFor(preflight: WorkflowPreflightV1 | undefined, key: string, preferredKeys: readonly (string | undefined)[] = []): WorkflowPreflightLaneV1 | undefined {
|
|
194
|
+
return workflowPreflightLaneForRuntimeKey(preflight, key, preferredKeys);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function stepKey(step: WorkflowChecklistStep): string | undefined {
|
|
198
|
+
return step.key ?? step.workflowKey ?? step.runId;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function stepItem(step: WorkflowChecklistStep, index: number, phase: string, key = stepKey(step) ?? `step-${index + 1}`, label = step.label ?? step.description ?? stepKey(step) ?? step.agent ?? key, preflight?: WorkflowPreflightLaneV1): WorkflowChecklistItem {
|
|
202
|
+
const state = checklistState(step);
|
|
203
|
+
return {
|
|
204
|
+
key: keyText(key, `step-${index + 1}`),
|
|
205
|
+
label: keyText(label, key),
|
|
206
|
+
phase,
|
|
207
|
+
state,
|
|
208
|
+
...(text(step.agent) ? { agent: text(step.agent) } : {}),
|
|
209
|
+
...(step.context ? { context: step.context } : {}),
|
|
210
|
+
...(finite(step.startedAt) !== undefined ? { startedAt: finite(step.startedAt) } : {}),
|
|
211
|
+
...(duration(step, undefined, state) !== undefined ? { durationMs: duration(step, undefined, state) } : {}),
|
|
212
|
+
...(text(step.currentTool) ? { currentTool: text(step.currentTool) } : {}),
|
|
213
|
+
...(finite(step.currentToolStartedAt) !== undefined ? { currentToolStartedAt: finite(step.currentToolStartedAt) } : {}),
|
|
214
|
+
...(text(step.currentPath) ? { currentPath: text(step.currentPath) } : {}),
|
|
215
|
+
...(count(step.toolCount) !== undefined ? { toolCount: count(step.toolCount) } : {}),
|
|
216
|
+
...(text(step.outputName) ? { outputName: text(step.outputName) } : {}),
|
|
217
|
+
...(text(step.error) ? { error: text(step.error) } : {}),
|
|
218
|
+
...(preflight ? { preflight } : {}),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function hostItem(host: HostStepNodeV1, phase: string, key = host.id): WorkflowChecklistItem {
|
|
223
|
+
const state = checklistState({ status: host.state, verdict: host.verdict, stale: host.freshness?.stale });
|
|
224
|
+
return {
|
|
225
|
+
key: keyText(key, "host-step"),
|
|
226
|
+
label: keyText(host.label, "host step"),
|
|
227
|
+
phase,
|
|
228
|
+
state,
|
|
229
|
+
kind: "host",
|
|
230
|
+
monitorKind: host.monitorKind,
|
|
231
|
+
...(text(host.provider) ? { provider: text(host.provider) } : {}),
|
|
232
|
+
...(text(host.role) ? { role: text(host.role) } : {}),
|
|
233
|
+
...(host.verdict ? { verdict: host.verdict } : {}),
|
|
234
|
+
...(text(host.target) ? { target: text(host.target) } : {}),
|
|
235
|
+
...(text(host.reasonCode) ? { reasonCode: text(host.reasonCode) } : {}),
|
|
236
|
+
...(text(host.detail) ? { error: text(host.detail) } : {}),
|
|
237
|
+
...(host.freshness?.stale !== undefined ? { stale: host.freshness.stale } : {}),
|
|
238
|
+
...(text(host.reportPath) ? { reportPath: text(host.reportPath) } : {}),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function graphNodes(graph: WorkflowGraphSnapshot | undefined): WorkflowGraphNode[] {
|
|
243
|
+
const out: WorkflowGraphNode[] = [];
|
|
244
|
+
const visit = (node: WorkflowGraphNode): void => {
|
|
245
|
+
if (node.kind === "parallel-group" || node.kind === "dynamic-parallel-group") {
|
|
246
|
+
for (const child of node.children ?? []) visit(child);
|
|
247
|
+
if (!node.children?.length) out.push(node);
|
|
248
|
+
} else {
|
|
249
|
+
out.push(node);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
for (const node of graph?.nodes ?? []) visit(node);
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function traceSources(trace: readonly WorkflowChecklistTraceEntry[] | undefined): WorkflowChecklistTraceEntry[] {
|
|
257
|
+
const latest = new Map<string, WorkflowChecklistTraceEntry>();
|
|
258
|
+
for (const entry of trace ?? []) {
|
|
259
|
+
const state = normalizedStatus(entry.state);
|
|
260
|
+
if ((entry.operation !== undefined && entry.operation !== "run" && entry.operation !== "host") || !entry.key || state === "delivered" || state === "missed") continue;
|
|
261
|
+
const existing = latest.get(entry.key);
|
|
262
|
+
latest.set(entry.key, state === "reused" && existing ? { ...existing, ...entry, state: existing.state } : { ...existing, ...entry });
|
|
263
|
+
}
|
|
264
|
+
return [...latest.values()];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function traceItem(entry: WorkflowChecklistTraceEntry, index: number, preflight: WorkflowPreflightLaneV1 | undefined): WorkflowChecklistItem {
|
|
268
|
+
const phase = keyText(preflight?.key ?? entry.generatedLaneKey ?? entry.phase, "Workflow");
|
|
269
|
+
const item = stepItem({ key: entry.key, label: entry.label, phase, agent: entry.agent, status: entry.state === "started" ? "running" : entry.state, durationMs: entry.durationMs, error: entry.error }, index, phase, entry.key, entry.label ?? entry.key, preflight);
|
|
270
|
+
if (entry.operation === "host") item.kind = "host";
|
|
271
|
+
return item;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function phaseFor(phases: Map<string, WorkflowChecklistPhase>, label: string): WorkflowChecklistPhase {
|
|
275
|
+
let phase = phases.get(label);
|
|
276
|
+
if (!phase) {
|
|
277
|
+
phase = { key: label, label, state: "queued", items: [], total: 0, done: 0, running: 0, queued: 0, blocked: 0, failed: 0, paused: 0, stopped: 0, parallel: false };
|
|
278
|
+
phases.set(label, phase);
|
|
279
|
+
}
|
|
280
|
+
return phase;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function add(phases: Map<string, WorkflowChecklistPhase>, phase: string, item: WorkflowChecklistItem): void {
|
|
284
|
+
phaseFor(phases, phase).items.push(item);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function mergeNodeStep(node: WorkflowGraphNode, step: WorkflowChecklistStep, phase: string, trace: WorkflowChecklistTraceEntry | undefined, preflight: WorkflowPreflightLaneV1 | undefined): WorkflowChecklistItem {
|
|
288
|
+
const state = checklistState(step);
|
|
289
|
+
const nodeState = checklistState({ status: node.status, acceptance: node.acceptanceStatus ? { status: node.acceptanceStatus } : undefined });
|
|
290
|
+
const status = TERMINAL_STATES.has(nodeState) && !TERMINAL_STATES.has(state)
|
|
291
|
+
? nodeState
|
|
292
|
+
: trace && !TERMINAL_STATES.has(state)
|
|
293
|
+
? (trace.state === "started" ? "running" : trace.state)
|
|
294
|
+
: normalizedStatus(step.status) === "pending" && normalizedStatus(node.status) !== "pending" ? node.status : step.status;
|
|
295
|
+
return stepItem({ ...step, status, key: node.id, label: step.label ?? node.label, phase: step.phase ?? phase, agent: step.agent ?? node.agent, outputName: step.outputName ?? node.outputName, error: step.error ?? trace?.error ?? node.error, acceptance: step.acceptance ?? (node.acceptanceStatus ? { status: node.acceptanceStatus } : undefined), durationMs: step.durationMs ?? trace?.durationMs }, node.flatIndex ?? 0, phase, node.id, step.label ?? node.label, preflight);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function priority(item: WorkflowChecklistItem): number {
|
|
299
|
+
return item.state === "blocked" ? 0 : item.state === "failed" ? 1 : item.state === "running" ? 2 : item.state === "paused" ? 3 : item.state === "stopped" ? 4 : item.state === "queued" ? 5 : 6;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function applyNow(item: WorkflowChecklistItem, now: number | undefined): WorkflowChecklistItem {
|
|
303
|
+
return item.durationMs === undefined && item.startedAt !== undefined && now !== undefined ? { ...item, durationMs: Math.max(0, now - item.startedAt) } : item;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function finalize(phase: WorkflowChecklistPhase): void {
|
|
307
|
+
phase.total = phase.items.length;
|
|
308
|
+
phase.done = phase.items.filter((item) => item.state === "complete").length;
|
|
309
|
+
phase.running = phase.items.filter((item) => item.state === "running").length;
|
|
310
|
+
phase.queued = phase.items.filter((item) => item.state === "queued").length;
|
|
311
|
+
phase.blocked = phase.items.filter((item) => item.state === "blocked").length;
|
|
312
|
+
phase.failed = phase.items.filter((item) => item.state === "failed").length;
|
|
313
|
+
phase.paused = phase.items.filter((item) => item.state === "paused").length;
|
|
314
|
+
phase.stopped = phase.items.filter((item) => item.state === "stopped").length;
|
|
315
|
+
phase.parallel = phase.total > 1;
|
|
316
|
+
phase.state = phase.blocked ? "blocked" : phase.failed ? "failed" : phase.running ? "running" : phase.paused ? "paused" : phase.stopped ? "stopped" : phase.queued ? "queued" : "complete";
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function projectWorkflowChecklist(input: WorkflowChecklistInput): WorkflowChecklistProjection {
|
|
320
|
+
const phases = new Map<string, WorkflowChecklistPhase>();
|
|
321
|
+
const steps = (input.steps ?? []) as readonly WorkflowChecklistStep[];
|
|
322
|
+
const nodes = graphNodes(input.graph);
|
|
323
|
+
const trace = traceSources(input.trace);
|
|
324
|
+
const traceByKey = new Map(trace.map((entry) => [entry.key, entry]));
|
|
325
|
+
const phaseByNode = new Map<string, string>();
|
|
326
|
+
for (const phase of input.graph?.phases ?? []) {
|
|
327
|
+
const title = keyText(phase.title, "Workflow");
|
|
328
|
+
phaseFor(phases, title);
|
|
329
|
+
for (const nodeId of phase.nodeIds) if (!phaseByNode.has(nodeId)) phaseByNode.set(nodeId, title);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const stepsByKey = new Map<string, Array<{ step: WorkflowChecklistStep; index: number }>>();
|
|
333
|
+
for (const [index, step] of steps.entries()) {
|
|
334
|
+
const key = stepKey(step);
|
|
335
|
+
if (!key) continue;
|
|
336
|
+
stepsByKey.set(key, [...(stepsByKey.get(key) ?? []), { step, index }]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const usedSteps = new Set<number>();
|
|
340
|
+
const hostById = new Map((input.hostSteps ?? []).map((host) => [host.id, host]));
|
|
341
|
+
const graphKeys = new Set(nodes.map((node) => node.id));
|
|
342
|
+
const stepKeys = new Set([...stepsByKey.keys()]);
|
|
343
|
+
const hostKeys = new Set(hostById.keys());
|
|
344
|
+
|
|
345
|
+
for (const node of nodes) {
|
|
346
|
+
const host = node.hostStep ?? hostById.get(node.id);
|
|
347
|
+
const phase = keyText(phaseByNode.get(node.id) ?? node.phase ?? (host ? host.label : node.label), "Workflow");
|
|
348
|
+
if (host) {
|
|
349
|
+
add(phases, phase, hostItem(host, phase, node.id));
|
|
350
|
+
hostById.delete(node.id);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const matches = (stepsByKey.get(node.id) ?? []).filter(({ index }) => !usedSteps.has(index));
|
|
354
|
+
if (matches.length) {
|
|
355
|
+
for (const match of matches) {
|
|
356
|
+
usedSteps.add(match.index);
|
|
357
|
+
add(phases, phase, applyNow(mergeNodeStep(node, match.step, phase, traceByKey.get(stepKey(match.step) ?? node.id), laneFor(input.preflight, node.id, [phase])), input.now));
|
|
358
|
+
}
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
add(phases, phase, applyNow(mergeNodeStep(node, { key: node.id, label: node.label, phase, agent: node.agent, status: node.status, outputName: node.outputName, error: node.error, acceptance: node.acceptanceStatus ? { status: node.acceptanceStatus } : undefined }, phase, traceByKey.get(node.id), laneFor(input.preflight, node.id, [phase])), input.now));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
for (const [index, step] of steps.entries()) {
|
|
365
|
+
if (usedSteps.has(index)) continue;
|
|
366
|
+
const key = keyText(stepKey(step), `step-${index + 1}`);
|
|
367
|
+
const traceEntry = traceByKey.get(key);
|
|
368
|
+
const lane = laneFor(input.preflight, key, [step.phase, traceEntry?.generatedLaneKey, traceEntry?.phase]);
|
|
369
|
+
const phase = keyText(lane?.key ?? step.phase ?? traceEntry?.generatedLaneKey ?? traceEntry?.phase, "Workflow");
|
|
370
|
+
add(phases, phase, applyNow(stepItem(step, index, phase, key, step.label ?? step.key ?? step.agent, lane), input.now));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
for (const host of hostById.values()) add(phases, keyText(host.label, "Host"), hostItem(host, keyText(host.label, "Host")));
|
|
374
|
+
for (const entry of trace) {
|
|
375
|
+
if (graphKeys.has(entry.key) || stepKeys.has(entry.key) || hostKeys.has(entry.key)) continue;
|
|
376
|
+
const item = traceItem(entry, phases.size, laneFor(input.preflight, entry.key, [entry.generatedLaneKey, entry.phase]));
|
|
377
|
+
add(phases, item.phase, item);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const finalized = [...phases.values()].filter((phase) => phase.items.length > 0);
|
|
381
|
+
for (const phase of finalized) {
|
|
382
|
+
phase.items = phase.items.map((item) => applyNow(item, input.now));
|
|
383
|
+
finalize(phase);
|
|
384
|
+
}
|
|
385
|
+
const all = finalized.flatMap((phase) => phase.items);
|
|
386
|
+
const counts = (state: WorkflowChecklistState): number => all.filter((item) => item.state === state).length;
|
|
387
|
+
const bottleneck = [...all].sort((left, right) => priority(left) - priority(right))[0];
|
|
388
|
+
return { phases: finalized, total: all.length, done: counts("complete"), running: counts("running"), queued: counts("queued"), blocked: counts("blocked"), failed: counts("failed"), paused: counts("paused"), stopped: counts("stopped"), ...(bottleneck && bottleneck.state !== "complete" ? { bottleneck } : {}) };
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function stateLabel(state: WorkflowChecklistState): string {
|
|
392
|
+
return state === "running" ? "active" : state;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function formatWorkflowChecklistSummary(projection: WorkflowChecklistProjection): string {
|
|
396
|
+
if (!projection.total) return "";
|
|
397
|
+
return [`${projection.done}/${projection.total} done`, projection.running ? `${projection.running} active` : undefined, projection.queued ? `${projection.queued} queued` : undefined, projection.blocked ? `${projection.blocked} blocked` : undefined, projection.failed ? `${projection.failed} failed` : undefined, projection.paused ? `${projection.paused} paused` : undefined, projection.stopped ? `${projection.stopped} stopped` : undefined].filter((value): value is string => Boolean(value)).join(" · ");
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
export function formatWorkflowChecklistPhase(phase: WorkflowChecklistPhase): string {
|
|
401
|
+
const counts = [phase.total > 1 && phase.done ? `${phase.done} done` : undefined, phase.running ? `${phase.running} active` : undefined, phase.queued ? `${phase.queued} queued` : undefined, phase.blocked ? `${phase.blocked} blocked` : undefined, phase.failed ? `${phase.failed} failed` : undefined, phase.paused ? `${phase.paused} paused` : undefined, phase.stopped ? `${phase.stopped} stopped` : undefined].filter((value): value is string => Boolean(value));
|
|
402
|
+
return counts.length ? `${phase.label} ${counts.join(" · ")}` : phase.label;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export function formatWorkflowChecklistBottleneck(item: WorkflowChecklistItem | undefined, options: { includeOutput?: boolean } = {}): string | undefined {
|
|
406
|
+
if (!item) return undefined;
|
|
407
|
+
const identity = [item.label, item.agent && item.agent !== item.label ? item.agent : undefined].filter((value): value is string => Boolean(value)).join(" · ") || item.key;
|
|
408
|
+
const includeOutput = options.includeOutput ?? true;
|
|
409
|
+
const details = [item.context ? `(${item.context})` : undefined, item.currentTool ? `${item.currentTool}${item.durationMs !== undefined ? ` ${formatDurationText(item.durationMs)}` : ""}` : undefined, !item.currentTool && item.currentPath ? item.currentPath : undefined, !item.currentTool && item.durationMs !== undefined ? formatDurationText(item.durationMs) : undefined, item.toolCount !== undefined ? `${item.toolCount} tools` : undefined, includeOutput && item.outputName ? `out:${item.outputName}` : undefined, item.error ? `error:${item.error.replace(/\bOutput:/g, "output:")}` : undefined].filter((value): value is string => Boolean(value));
|
|
410
|
+
return [identity, ...details].join(" · ");
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function formatWorkflowChecklistItem(item: WorkflowChecklistItem): string {
|
|
414
|
+
const identity = [item.label, item.agent && item.agent !== item.label ? item.agent : undefined].filter((value): value is string => Boolean(value)).join(" · ") || item.key;
|
|
415
|
+
const details = [item.kind && item.monitorKind ? item.monitorKind : undefined, item.provider ? `provider:${item.provider}` : undefined, item.role ? `role:${item.role}` : undefined, item.target, item.currentTool, !item.currentTool && item.currentPath ? item.currentPath : undefined, item.durationMs !== undefined ? formatDurationText(item.durationMs) : undefined, item.toolCount !== undefined ? `${item.toolCount} tools` : undefined, item.outputName ? `out:${item.outputName}` : undefined, item.reportPath ? `out:${item.reportPath}` : undefined, item.stale ? "stale" : undefined, item.reasonCode ? `reason:${item.reasonCode}` : undefined, item.error ? `error:${item.error.replace(/\bOutput:/g, "output:")}` : undefined].filter((value): value is string => Boolean(value));
|
|
416
|
+
return `${identity}${item.context ? ` (${item.context})` : ""}${item.state === "complete" ? "" : ` · ${stateLabel(item.state)}`}${details.length ? ` · ${details.join(" · ")}` : ""}`;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function formatDurationText(ms: number): string {
|
|
420
|
+
const seconds = Math.max(0, Math.round(ms / 1000));
|
|
421
|
+
return seconds < 60 ? `${seconds}s` : `${Math.floor(seconds / 60)}m ${seconds % 60}s`;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function formatWorkflowChecklistText(projection: WorkflowChecklistProjection, indent = "", options: { includeItems?: boolean } = {}): string[] {
|
|
425
|
+
if (!projection.total) return [];
|
|
426
|
+
const lines = [`${indent}Workflow checklist: ${formatWorkflowChecklistSummary(projection)}`];
|
|
427
|
+
for (const phase of projection.phases) {
|
|
428
|
+
const phaseMarker = phase.state === "complete" ? "✓" : phase.state === "running" ? "⠼" : phase.state === "blocked" ? "!" : phase.state === "failed" ? "✗" : phase.state === "paused" || phase.state === "stopped" ? "■" : "◦";
|
|
429
|
+
lines.push(`${indent} ${phaseMarker} ${formatWorkflowChecklistPhase(phase)}`);
|
|
430
|
+
if (options.includeItems === false) continue;
|
|
431
|
+
for (const item of phase.items) {
|
|
432
|
+
const marker = item.state === "complete" ? "✓" : item.state === "running" ? "⠼" : item.state === "blocked" ? "!" : item.state === "failed" ? "✗" : item.state === "paused" || item.state === "stopped" ? "■" : "◦";
|
|
433
|
+
lines.push(`${indent} ${marker} ${formatWorkflowChecklistItem(item)}`);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
const bottleneck = formatWorkflowChecklistBottleneck(projection.bottleneck);
|
|
437
|
+
if (bottleneck) lines.push(`${indent} bottleneck · ${bottleneck}`);
|
|
438
|
+
return lines;
|
|
439
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { AsyncStatus, WorkflowChildSummaryV1 } from "../shared/types.ts";
|
|
2
|
+
import type { WorkflowScriptChildResult, WorkflowScriptTraceEntry } from "./scripted-workflow.ts";
|
|
3
|
+
|
|
4
|
+
const KEY_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
5
|
+
const TERMINAL_STATES = new Set(["completed", "failed", "paused", "stopped", "rejected", "detached"]);
|
|
6
|
+
const MAX_REQUIRED_ID_BYTES = 4_096;
|
|
7
|
+
|
|
8
|
+
function bounded(value: unknown, maxBytes: number): string | undefined {
|
|
9
|
+
if (typeof value !== "string" || !value.trim() || Buffer.byteLength(value, "utf8") > maxBytes) return undefined;
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function requiredId(value: string, label: string): string {
|
|
14
|
+
const id = bounded(value, MAX_REQUIRED_ID_BYTES);
|
|
15
|
+
if (!id) throw new Error(`${label} must be a non-empty identifier of at most ${MAX_REQUIRED_ID_BYTES} UTF-8 bytes.`);
|
|
16
|
+
return id;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function workflowChildSummary(input: {
|
|
20
|
+
parentToolCallId: string;
|
|
21
|
+
workflowRunId: string;
|
|
22
|
+
workflowState: WorkflowChildSummaryV1["workflowState"];
|
|
23
|
+
inventoryComplete: boolean;
|
|
24
|
+
trace?: WorkflowScriptTraceEntry[];
|
|
25
|
+
children?: WorkflowScriptChildResult[];
|
|
26
|
+
steps?: NonNullable<AsyncStatus["steps"]>;
|
|
27
|
+
}): WorkflowChildSummaryV1 {
|
|
28
|
+
const rows = new Map<string, WorkflowChildSummaryV1["children"][number]>();
|
|
29
|
+
for (const entry of input.trace ?? []) {
|
|
30
|
+
if (entry.operation !== "run" || !KEY_PATTERN.test(entry.key)) continue;
|
|
31
|
+
const previous = rows.get(entry.key);
|
|
32
|
+
const state = entry.state === "completed" ? "completed"
|
|
33
|
+
: entry.state === "failed" ? "failed"
|
|
34
|
+
: entry.state === "stopped" ? "stopped"
|
|
35
|
+
: entry.state === "detached" ? "detached"
|
|
36
|
+
: previous?.state ?? "running";
|
|
37
|
+
rows.set(entry.key, {
|
|
38
|
+
childId: entry.key,
|
|
39
|
+
state,
|
|
40
|
+
...(bounded(entry.runId, 256) ? { runId: bounded(entry.runId, 256) } : {}),
|
|
41
|
+
...(previous?.agent ? { agent: previous.agent } : {}),
|
|
42
|
+
...(previous?.sessionName ? { sessionName: previous.sessionName } : {}),
|
|
43
|
+
...(previous?.model ? { model: previous.model } : {}),
|
|
44
|
+
...(previous?.thinking ? { thinking: previous.thinking } : {}),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
for (const step of input.steps ?? []) {
|
|
48
|
+
const key = step.workflowKey;
|
|
49
|
+
if (!key || !KEY_PATTERN.test(key)) continue;
|
|
50
|
+
const state = step.status === "complete" || step.status === "completed" ? "completed"
|
|
51
|
+
: step.status === "failed" ? "failed"
|
|
52
|
+
: step.status === "paused" ? "paused"
|
|
53
|
+
: step.status === "stopped" ? "stopped"
|
|
54
|
+
: step.status === "rejected" ? "rejected"
|
|
55
|
+
: step.status === "pending" ? "pending" : "running";
|
|
56
|
+
const launchResolved = step.async !== undefined || step.sessionFile !== undefined || step.runId !== undefined || step.model !== undefined;
|
|
57
|
+
rows.set(key, {
|
|
58
|
+
childId: key,
|
|
59
|
+
state,
|
|
60
|
+
...(bounded(step.runId, 256) ? { runId: bounded(step.runId, 256) } : {}),
|
|
61
|
+
...(launchResolved && bounded(step.agent, 256) ? { agent: bounded(step.agent, 256) } : {}),
|
|
62
|
+
...(bounded(step.sessionName, 256) ? { sessionName: bounded(step.sessionName, 256) } : {}),
|
|
63
|
+
...(bounded(step.model, 256) ? { model: bounded(step.model, 256) } : {}),
|
|
64
|
+
...(bounded(step.thinking, 32) ? { thinking: bounded(step.thinking, 32) } : {}),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
for (const child of input.children ?? []) {
|
|
68
|
+
if (!KEY_PATTERN.test(child.key)) continue;
|
|
69
|
+
const result = Array.isArray(child.results) ? child.results.find((value) => value && typeof value === "object") as Record<string, unknown> | undefined : undefined;
|
|
70
|
+
const state = child.detached ? "detached" : child.stopped ? "stopped" : child.interrupted ? "paused" : child.ok ? "completed" : result?.acceptance && typeof result.acceptance === "object" && (result.acceptance as { status?: unknown }).status === "rejected" ? "rejected" : "failed";
|
|
71
|
+
rows.set(child.key, {
|
|
72
|
+
childId: child.key,
|
|
73
|
+
state,
|
|
74
|
+
...(bounded(child.runId, 256) ? { runId: bounded(child.runId, 256) } : {}),
|
|
75
|
+
...(bounded(child.agent, 256) ? { agent: bounded(child.agent, 256) } : {}),
|
|
76
|
+
...(bounded(result?.sessionName, 256) ? { sessionName: bounded(result?.sessionName, 256) } : {}),
|
|
77
|
+
...(bounded(result?.model, 256) ? { model: bounded(result?.model, 256) } : {}),
|
|
78
|
+
...(bounded(result?.thinking, 32) ? { thinking: bounded(result?.thinking, 32) } : {}),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (input.inventoryComplete) {
|
|
82
|
+
for (const [key, row] of rows) {
|
|
83
|
+
if (!TERMINAL_STATES.has(row.state)) rows.set(key, { ...row, state: input.workflowState === "stopped" ? "stopped" : "failed" });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
version: 1,
|
|
88
|
+
parentToolCallId: requiredId(input.parentToolCallId, "parentToolCallId"),
|
|
89
|
+
workflowRunId: requiredId(input.workflowRunId, "workflowRunId"),
|
|
90
|
+
inventoryComplete: input.inventoryComplete,
|
|
91
|
+
workflowState: input.workflowState,
|
|
92
|
+
children: [...rows.values()],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function parseWorkflowChildSummary(value: unknown): WorkflowChildSummaryV1 | undefined {
|
|
97
|
+
if (value === undefined) return undefined;
|
|
98
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("workflowChildren must be an object.");
|
|
99
|
+
const input = value as Record<string, unknown>;
|
|
100
|
+
const allowed = new Set(["version", "parentToolCallId", "workflowRunId", "inventoryComplete", "workflowState", "children"]);
|
|
101
|
+
if (Object.keys(input).some((key) => !allowed.has(key))) throw new Error("workflowChildren has unsupported fields.");
|
|
102
|
+
if (input.version !== 1 || typeof input.inventoryComplete !== "boolean" || !Array.isArray(input.children)) throw new Error("workflowChildren is invalid.");
|
|
103
|
+
const workflowState = input.workflowState;
|
|
104
|
+
if (workflowState !== "queued" && workflowState !== "running" && workflowState !== "completed" && workflowState !== "failed" && workflowState !== "paused" && workflowState !== "stopped") throw new Error("workflowChildren.workflowState is invalid.");
|
|
105
|
+
const children = input.children.map((row): WorkflowChildSummaryV1["children"][number] => {
|
|
106
|
+
if (!row || typeof row !== "object" || Array.isArray(row)) throw new Error("workflowChildren child row is invalid.");
|
|
107
|
+
const child = row as Record<string, unknown>;
|
|
108
|
+
if (Object.keys(child).some((key) => !["childId", "runId", "agent", "sessionName", "model", "thinking", "state"].includes(key))) throw new Error("workflowChildren child row has unsupported fields.");
|
|
109
|
+
if (typeof child.childId !== "string" || !KEY_PATTERN.test(child.childId)) throw new Error("workflowChildren childId is invalid.");
|
|
110
|
+
const state = child.state;
|
|
111
|
+
if (state !== "pending" && state !== "running" && state !== "completed" && state !== "failed" && state !== "paused" && state !== "stopped" && state !== "rejected" && state !== "detached") throw new Error("workflowChildren child state is invalid.");
|
|
112
|
+
for (const [field, maxBytes] of [["runId", 256], ["agent", 256], ["sessionName", 256], ["model", 256], ["thinking", 32]] as const) {
|
|
113
|
+
if (child[field] !== undefined && bounded(child[field], maxBytes) === undefined) throw new Error(`workflowChildren child ${field} is invalid.`);
|
|
114
|
+
}
|
|
115
|
+
return { childId: child.childId, state, ...(child.runId ? { runId: child.runId as string } : {}), ...(child.agent ? { agent: child.agent as string } : {}), ...(child.sessionName ? { sessionName: child.sessionName as string } : {}), ...(child.model ? { model: child.model as string } : {}), ...(child.thinking ? { thinking: child.thinking as string } : {}) };
|
|
116
|
+
});
|
|
117
|
+
if (new Set(children.map((child) => child.childId)).size !== children.length) throw new Error("workflowChildren has duplicate childId values.");
|
|
118
|
+
if (typeof input.parentToolCallId !== "string") throw new Error("workflowChildren.parentToolCallId is invalid.");
|
|
119
|
+
if (typeof input.workflowRunId !== "string") throw new Error("workflowChildren.workflowRunId is invalid.");
|
|
120
|
+
return { version: 1, parentToolCallId: requiredId(input.parentToolCallId, "workflowChildren.parentToolCallId"), workflowRunId: requiredId(input.workflowRunId, "workflowChildren.workflowRunId"), inventoryComplete: input.inventoryComplete, workflowState, children };
|
|
121
|
+
}
|