@duckmind/dm-darwin-x64 0.43.4 → 0.43.8
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/dm +0 -0
- package/extensions/.dm-extensions.json +241 -49
- package/extensions/dm-9router-ext/package.json +2 -19
- package/extensions/dm-9router-ext/src/index.js +5 -0
- package/extensions/dm-ask-user/index.js +10 -0
- package/extensions/dm-ask-user/package.json +1 -21
- package/extensions/dm-cli-anything/package.json +0 -6
- package/extensions/dm-context/package.json +2 -12
- package/extensions/dm-context/src/context.js +1 -0
- package/extensions/dm-context/src/index.js +7 -0
- package/extensions/dm-context/src/utils.js +1 -0
- package/extensions/dm-cua/package.json +0 -7
- package/extensions/dm-fff/package.json +1 -28
- package/extensions/dm-fff/src/index.js +15 -0
- package/extensions/dm-fff/src/query.js +1 -0
- package/extensions/dm-goal/package.json +2 -31
- package/extensions/dm-goal/src/goal.js +43 -0
- package/extensions/dm-grill-me/index.js +174 -0
- package/extensions/dm-grill-me/package.json +1 -26
- package/extensions/dm-image2/package.json +0 -7
- package/extensions/dm-subagents/package.json +1 -32
- package/extensions/dm-subagents/src/agents/agent-management.js +37 -0
- package/extensions/dm-subagents/src/agents/agent-memory.js +6 -0
- package/extensions/dm-subagents/src/agents/agent-scope.js +1 -0
- package/extensions/dm-subagents/src/agents/agent-selection.js +1 -0
- package/extensions/dm-subagents/src/agents/agent-serializer.js +7 -0
- package/extensions/dm-subagents/src/agents/agents.js +11 -0
- package/extensions/dm-subagents/src/agents/chain-serializer.js +12 -0
- package/extensions/dm-subagents/src/agents/frontmatter.js +6 -0
- package/extensions/dm-subagents/src/agents/identity.js +1 -0
- package/extensions/dm-subagents/src/agents/proactive-skills.js +1 -0
- package/extensions/dm-subagents/src/agents/skills.js +6 -0
- package/extensions/dm-subagents/src/extension/config.js +2 -0
- package/extensions/dm-subagents/src/extension/control-notices.js +4 -0
- package/extensions/dm-subagents/src/extension/doctor.js +16 -0
- package/extensions/dm-subagents/src/extension/fanout-child.js +231 -0
- package/extensions/dm-subagents/src/extension/index.js +367 -0
- package/extensions/dm-subagents/src/extension/rpc.js +8 -0
- package/extensions/dm-subagents/src/extension/schemas.js +1 -0
- package/extensions/dm-subagents/src/extension/{tool-description.ts → tool-description.js} +6 -126
- package/extensions/dm-subagents/src/intercom/intercom-bridge.js +13 -0
- package/extensions/dm-subagents/src/intercom/native-supervisor-channel.js +5 -0
- package/extensions/dm-subagents/src/intercom/result-intercom.js +3 -0
- package/extensions/dm-subagents/src/profiles/profiles.js +3 -0
- package/extensions/dm-subagents/src/runs/background/async-execution.js +46 -0
- package/extensions/dm-subagents/src/runs/background/async-job-tracker.js +14 -0
- package/extensions/dm-subagents/src/runs/background/async-resume.js +8 -0
- package/extensions/dm-subagents/src/runs/background/async-status.js +12 -0
- package/extensions/dm-subagents/src/runs/background/chain-append.js +2 -0
- package/extensions/dm-subagents/src/runs/background/chain-root-attachment.js +1 -0
- package/extensions/dm-subagents/src/runs/background/completion-batcher.js +1 -0
- package/extensions/dm-subagents/src/runs/background/completion-dedupe.js +1 -0
- package/extensions/dm-subagents/src/runs/background/control-channel.js +1 -0
- package/extensions/dm-subagents/src/runs/background/fleet-view.js +17 -0
- package/extensions/dm-subagents/src/runs/background/notify.js +3 -0
- package/extensions/dm-subagents/src/runs/background/parallel-groups.js +1 -0
- package/extensions/dm-subagents/src/runs/background/result-watcher.js +8 -0
- package/extensions/dm-subagents/src/runs/background/run-id-resolver.js +4 -0
- package/extensions/dm-subagents/src/runs/background/run-status.js +23 -0
- package/extensions/dm-subagents/src/runs/background/scheduled-runs.js +4 -0
- package/extensions/dm-subagents/src/runs/background/stale-run-reconciler.js +11 -0
- package/extensions/dm-subagents/src/runs/background/subagent-runner.js +78 -0
- package/extensions/dm-subagents/src/runs/background/top-level-async.js +1 -0
- package/extensions/dm-subagents/src/runs/background/wait.js +11 -0
- package/extensions/dm-subagents/src/runs/foreground/chain-clarify.js +12 -0
- package/extensions/dm-subagents/src/runs/foreground/chain-execution.js +102 -0
- package/extensions/dm-subagents/src/runs/foreground/execution.js +51 -0
- package/extensions/dm-subagents/src/runs/foreground/subagent-executor.js +228 -0
- package/extensions/dm-subagents/src/runs/shared/acceptance.js +3 -0
- package/extensions/dm-subagents/src/runs/shared/chain-outputs.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/completion-guard.js +3 -0
- package/extensions/dm-subagents/src/runs/shared/dm-args.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/dm-spawn.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/dynamic-fanout.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/long-running-guard.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/mcp-direct-tool-allowlist.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/model-fallback.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/model-scope.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/nested-events.js +8 -0
- package/extensions/dm-subagents/src/runs/shared/nested-path.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/nested-render.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/parallel-utils.js +5 -0
- package/extensions/dm-subagents/src/runs/shared/run-history.js +5 -0
- package/extensions/dm-subagents/src/runs/shared/single-output.js +14 -0
- package/extensions/dm-subagents/src/runs/shared/structured-output.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/subagent-control.js +5 -0
- package/extensions/dm-subagents/src/runs/shared/subagent-prompt-runtime.js +22 -0
- package/extensions/dm-subagents/src/runs/shared/tool-budget.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/turn-budget.js +7 -0
- package/extensions/dm-subagents/src/runs/shared/workflow-graph.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/worktree.js +3 -0
- package/extensions/dm-subagents/src/shared/artifacts.js +2 -0
- package/extensions/dm-subagents/src/shared/atomic-json.js +1 -0
- package/extensions/dm-subagents/src/shared/child-transcript.js +5 -0
- package/extensions/dm-subagents/src/shared/file-coalescer.js +1 -0
- package/extensions/dm-subagents/src/shared/fork-context.js +6 -0
- package/extensions/dm-subagents/src/shared/formatters.js +9 -0
- package/extensions/dm-subagents/src/shared/jsonl-writer.js +2 -0
- package/extensions/dm-subagents/src/shared/model-info.js +1 -0
- package/extensions/dm-subagents/src/shared/post-exit-stdio-guard.js +1 -0
- package/extensions/dm-subagents/src/shared/session-identity.js +1 -0
- package/extensions/dm-subagents/src/shared/session-tokens.js +2 -0
- package/extensions/dm-subagents/src/shared/settings.js +23 -0
- package/extensions/dm-subagents/src/shared/status-format.js +1 -0
- package/extensions/dm-subagents/src/shared/types.js +8 -0
- package/extensions/dm-subagents/src/shared/utils.js +2 -0
- package/extensions/dm-subagents/src/slash/prompt-template-bridge.js +1 -0
- package/extensions/dm-subagents/src/slash/prompt-workflows.js +7 -0
- package/extensions/dm-subagents/src/slash/slash-bridge.js +1 -0
- package/extensions/dm-subagents/src/slash/slash-commands.js +38 -0
- package/extensions/dm-subagents/src/slash/slash-live-state.js +6 -0
- package/extensions/dm-subagents/src/tui/render-helpers.js +1 -0
- package/extensions/dm-subagents/src/tui/render.js +4 -0
- package/extensions/dm-tasks/package.json +1 -30
- package/extensions/dm-tasks/src/auto-clear.js +1 -0
- package/extensions/dm-tasks/src/index.js +212 -0
- package/extensions/dm-tasks/src/process-tracker.js +1 -0
- package/extensions/dm-tasks/src/task-store.js +1 -0
- package/extensions/dm-tasks/src/tasks-config.js +1 -0
- package/extensions/dm-tasks/src/types.js +0 -0
- package/extensions/dm-tasks/src/ui/settings-menu.js +1 -0
- package/extensions/dm-tasks/src/ui/task-widget.js +1 -0
- package/extensions/dm-usage/index.js +9 -0
- package/extensions/dm-usage/package.json +1 -18
- package/extensions/greedysearch-dm/index.js +20 -0
- package/extensions/greedysearch-dm/package.json +1 -22
- package/extensions/greedysearch-dm/src/formatters/results.js +8 -0
- package/extensions/greedysearch-dm/src/formatters/sources.js +1 -0
- package/extensions/greedysearch-dm/src/formatters/synthesis.js +1 -0
- package/extensions/greedysearch-dm/src/tools/greedy-search-handler.js +20 -0
- package/extensions/greedysearch-dm/src/tools/shared.js +9 -0
- package/extensions/greedysearch-dm/src/types.js +0 -0
- package/extensions/greedysearch-dm/src/utils/helpers.js +1 -0
- package/package.json +1 -1
- package/extensions/dm-9router-ext/src/index.ts +0 -541
- package/extensions/dm-ask-user/index.ts +0 -857
- package/extensions/dm-context/src/context.ts +0 -158
- package/extensions/dm-context/src/index.ts +0 -439
- package/extensions/dm-context/src/utils.ts +0 -6
- package/extensions/dm-fff/src/index.ts +0 -1023
- package/extensions/dm-fff/src/query.ts +0 -87
- package/extensions/dm-goal/src/goal.ts +0 -1073
- package/extensions/dm-grill-me/index.ts +0 -1085
- package/extensions/dm-subagents/src/agents/agent-management.ts +0 -1052
- package/extensions/dm-subagents/src/agents/agent-memory.ts +0 -254
- package/extensions/dm-subagents/src/agents/agent-scope.ts +0 -6
- package/extensions/dm-subagents/src/agents/agent-selection.ts +0 -25
- package/extensions/dm-subagents/src/agents/agent-serializer.ts +0 -121
- package/extensions/dm-subagents/src/agents/agents.ts +0 -1553
- package/extensions/dm-subagents/src/agents/chain-serializer.ts +0 -277
- package/extensions/dm-subagents/src/agents/frontmatter.ts +0 -93
- package/extensions/dm-subagents/src/agents/identity.ts +0 -30
- package/extensions/dm-subagents/src/agents/proactive-skills.ts +0 -191
- package/extensions/dm-subagents/src/agents/skills.ts +0 -729
- package/extensions/dm-subagents/src/extension/config.ts +0 -39
- package/extensions/dm-subagents/src/extension/control-notices.ts +0 -92
- package/extensions/dm-subagents/src/extension/doctor.ts +0 -214
- package/extensions/dm-subagents/src/extension/fanout-child.ts +0 -172
- package/extensions/dm-subagents/src/extension/index.ts +0 -656
- package/extensions/dm-subagents/src/extension/rpc.ts +0 -369
- package/extensions/dm-subagents/src/extension/schemas.ts +0 -309
- package/extensions/dm-subagents/src/intercom/intercom-bridge.ts +0 -180
- package/extensions/dm-subagents/src/intercom/native-supervisor-channel.ts +0 -519
- package/extensions/dm-subagents/src/intercom/result-intercom.ts +0 -377
- package/extensions/dm-subagents/src/profiles/profiles.ts +0 -637
- package/extensions/dm-subagents/src/runs/background/async-execution.ts +0 -1065
- package/extensions/dm-subagents/src/runs/background/async-job-tracker.ts +0 -441
- package/extensions/dm-subagents/src/runs/background/async-resume.ts +0 -391
- package/extensions/dm-subagents/src/runs/background/async-status.ts +0 -395
- package/extensions/dm-subagents/src/runs/background/chain-append.ts +0 -282
- package/extensions/dm-subagents/src/runs/background/chain-root-attachment.ts +0 -191
- package/extensions/dm-subagents/src/runs/background/completion-batcher.ts +0 -166
- package/extensions/dm-subagents/src/runs/background/completion-dedupe.ts +0 -63
- package/extensions/dm-subagents/src/runs/background/control-channel.ts +0 -332
- package/extensions/dm-subagents/src/runs/background/fleet-view.ts +0 -515
- package/extensions/dm-subagents/src/runs/background/notify.ts +0 -225
- package/extensions/dm-subagents/src/runs/background/parallel-groups.ts +0 -45
- package/extensions/dm-subagents/src/runs/background/result-watcher.ts +0 -315
- package/extensions/dm-subagents/src/runs/background/run-id-resolver.ts +0 -84
- package/extensions/dm-subagents/src/runs/background/run-status.ts +0 -434
- package/extensions/dm-subagents/src/runs/background/scheduled-runs.ts +0 -514
- package/extensions/dm-subagents/src/runs/background/stale-run-reconciler.ts +0 -368
- package/extensions/dm-subagents/src/runs/background/subagent-runner.ts +0 -3171
- package/extensions/dm-subagents/src/runs/background/top-level-async.ts +0 -13
- package/extensions/dm-subagents/src/runs/background/wait.ts +0 -353
- package/extensions/dm-subagents/src/runs/foreground/chain-clarify.ts +0 -1333
- package/extensions/dm-subagents/src/runs/foreground/chain-execution.ts +0 -1313
- package/extensions/dm-subagents/src/runs/foreground/execution.ts +0 -1239
- package/extensions/dm-subagents/src/runs/foreground/subagent-executor.ts +0 -3613
- package/extensions/dm-subagents/src/runs/shared/acceptance.ts +0 -879
- package/extensions/dm-subagents/src/runs/shared/chain-outputs.ts +0 -116
- package/extensions/dm-subagents/src/runs/shared/completion-guard.ts +0 -147
- package/extensions/dm-subagents/src/runs/shared/dm-args.ts +0 -271
- package/extensions/dm-subagents/src/runs/shared/dm-spawn.ts +0 -147
- package/extensions/dm-subagents/src/runs/shared/dynamic-fanout.ts +0 -295
- package/extensions/dm-subagents/src/runs/shared/long-running-guard.ts +0 -175
- package/extensions/dm-subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +0 -365
- package/extensions/dm-subagents/src/runs/shared/model-fallback.ts +0 -292
- package/extensions/dm-subagents/src/runs/shared/model-scope.ts +0 -128
- package/extensions/dm-subagents/src/runs/shared/nested-events.ts +0 -908
- package/extensions/dm-subagents/src/runs/shared/nested-path.ts +0 -52
- package/extensions/dm-subagents/src/runs/shared/nested-render.ts +0 -115
- package/extensions/dm-subagents/src/runs/shared/parallel-utils.ts +0 -198
- package/extensions/dm-subagents/src/runs/shared/run-history.ts +0 -60
- package/extensions/dm-subagents/src/runs/shared/single-output.ts +0 -180
- package/extensions/dm-subagents/src/runs/shared/structured-output.ts +0 -77
- package/extensions/dm-subagents/src/runs/shared/subagent-control.ts +0 -223
- package/extensions/dm-subagents/src/runs/shared/subagent-prompt-runtime.ts +0 -342
- package/extensions/dm-subagents/src/runs/shared/tool-budget.ts +0 -74
- package/extensions/dm-subagents/src/runs/shared/turn-budget.ts +0 -52
- package/extensions/dm-subagents/src/runs/shared/workflow-graph.ts +0 -206
- package/extensions/dm-subagents/src/runs/shared/worktree.ts +0 -600
- package/extensions/dm-subagents/src/shared/artifacts.ts +0 -113
- package/extensions/dm-subagents/src/shared/atomic-json.ts +0 -86
- package/extensions/dm-subagents/src/shared/child-transcript.ts +0 -212
- package/extensions/dm-subagents/src/shared/file-coalescer.ts +0 -40
- package/extensions/dm-subagents/src/shared/fork-context.ts +0 -194
- package/extensions/dm-subagents/src/shared/formatters.ts +0 -133
- package/extensions/dm-subagents/src/shared/jsonl-writer.ts +0 -81
- package/extensions/dm-subagents/src/shared/model-info.ts +0 -78
- package/extensions/dm-subagents/src/shared/post-exit-stdio-guard.ts +0 -85
- package/extensions/dm-subagents/src/shared/session-identity.ts +0 -10
- package/extensions/dm-subagents/src/shared/session-tokens.ts +0 -44
- package/extensions/dm-subagents/src/shared/settings.ts +0 -450
- package/extensions/dm-subagents/src/shared/status-format.ts +0 -49
- package/extensions/dm-subagents/src/shared/types.ts +0 -1257
- package/extensions/dm-subagents/src/shared/utils.ts +0 -554
- package/extensions/dm-subagents/src/slash/prompt-template-bridge.ts +0 -420
- package/extensions/dm-subagents/src/slash/prompt-workflows.ts +0 -330
- package/extensions/dm-subagents/src/slash/slash-bridge.ts +0 -176
- package/extensions/dm-subagents/src/slash/slash-commands.ts +0 -1296
- package/extensions/dm-subagents/src/slash/slash-live-state.ts +0 -292
- package/extensions/dm-subagents/src/tui/render-helpers.ts +0 -80
- package/extensions/dm-subagents/src/tui/render.ts +0 -1748
- package/extensions/dm-tasks/src/auto-clear.ts +0 -91
- package/extensions/dm-tasks/src/index.ts +0 -1145
- package/extensions/dm-tasks/src/process-tracker.ts +0 -140
- package/extensions/dm-tasks/src/task-store.ts +0 -305
- package/extensions/dm-tasks/src/tasks-config.ts +0 -23
- package/extensions/dm-tasks/src/types.ts +0 -40
- package/extensions/dm-tasks/src/ui/settings-menu.ts +0 -100
- package/extensions/dm-tasks/src/ui/task-widget.ts +0 -265
- package/extensions/dm-usage/index.ts +0 -1718
- package/extensions/greedysearch-dm/index.ts +0 -177
- package/extensions/greedysearch-dm/src/formatters/results.ts +0 -163
- package/extensions/greedysearch-dm/src/formatters/sources.ts +0 -116
- package/extensions/greedysearch-dm/src/formatters/synthesis.ts +0 -87
- package/extensions/greedysearch-dm/src/tools/greedy-search-handler.ts +0 -370
- package/extensions/greedysearch-dm/src/tools/shared.ts +0 -187
- package/extensions/greedysearch-dm/src/types.ts +0 -110
- package/extensions/greedysearch-dm/src/utils/helpers.ts +0 -40
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Subagent completion notifications.
|
|
3
|
-
*
|
|
4
|
-
* Successful (completed) async results are held briefly and emitted as a
|
|
5
|
-
* single grouped message when sibling jobs finish within a short window (see
|
|
6
|
-
* `completion-batcher.ts`). Failed and paused results bypass grouping and fire
|
|
7
|
-
* immediately, flushing any held successes first, so failure and attention
|
|
8
|
-
* signals are never delayed.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { ExtensionAPI } from "@duckmind/dm-coding-agent";
|
|
12
|
-
import { buildCompletionKey, getGlobalSeenMap, markSeenWithTtl } from "./completion-dedupe.ts";
|
|
13
|
-
import {
|
|
14
|
-
type CompletionBatchConfig,
|
|
15
|
-
type CompletionBatcher,
|
|
16
|
-
createCompletionBatcher,
|
|
17
|
-
resolveCompletionBatchConfig,
|
|
18
|
-
} from "./completion-batcher.ts";
|
|
19
|
-
import { SUBAGENT_ASYNC_COMPLETE_EVENT, type SubagentState } from "../../shared/types.ts";
|
|
20
|
-
|
|
21
|
-
interface ChainStepResult {
|
|
22
|
-
agent: string;
|
|
23
|
-
output: string;
|
|
24
|
-
success: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface SubagentNotifyDetails {
|
|
28
|
-
agent: string;
|
|
29
|
-
status: "completed" | "failed" | "paused";
|
|
30
|
-
taskInfo?: string;
|
|
31
|
-
resultPreview: string;
|
|
32
|
-
durationMs?: number;
|
|
33
|
-
sessionLabel?: string;
|
|
34
|
-
sessionValue?: string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface SubagentResult {
|
|
38
|
-
id: string | null;
|
|
39
|
-
agent: string | null;
|
|
40
|
-
success: boolean;
|
|
41
|
-
summary: string;
|
|
42
|
-
exitCode?: number;
|
|
43
|
-
state?: string;
|
|
44
|
-
timestamp: number;
|
|
45
|
-
durationMs?: number;
|
|
46
|
-
cwd?: string;
|
|
47
|
-
sessionFile?: string;
|
|
48
|
-
shareUrl?: string;
|
|
49
|
-
gistUrl?: string;
|
|
50
|
-
shareError?: string;
|
|
51
|
-
results?: ChainStepResult[];
|
|
52
|
-
taskIndex?: number;
|
|
53
|
-
totalTasks?: number;
|
|
54
|
-
sessionId?: string | null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface NotifyTimerApi {
|
|
58
|
-
setTimeout(handler: () => void, delayMs: number): unknown;
|
|
59
|
-
clearTimeout(handle: unknown): void;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface RegisterSubagentNotifyOptions {
|
|
63
|
-
batchConfig?: CompletionBatchConfig;
|
|
64
|
-
timers?: NotifyTimerApi;
|
|
65
|
-
now?: () => number;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function formatSessionLine(details: SubagentNotifyDetails): string | undefined {
|
|
69
|
-
if (!details.sessionValue) return undefined;
|
|
70
|
-
return details.sessionLabel ? `${details.sessionLabel}: ${details.sessionValue}` : details.sessionValue;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function formatSingleCompletion(details: SubagentNotifyDetails): string {
|
|
74
|
-
const sessionLine = formatSessionLine(details);
|
|
75
|
-
return [
|
|
76
|
-
`Background task ${details.status}: **${details.agent}**${details.taskInfo ?? ""}`,
|
|
77
|
-
"",
|
|
78
|
-
details.resultPreview.trim() ? details.resultPreview : "(no output)",
|
|
79
|
-
sessionLine ? "" : undefined,
|
|
80
|
-
sessionLine,
|
|
81
|
-
]
|
|
82
|
-
.filter((line) => line !== undefined)
|
|
83
|
-
.join("\n");
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function formatGroupedCompletion(details: SubagentNotifyDetails[]): string {
|
|
87
|
-
const header = `Background tasks completed (${details.length}): ${details.map((d) => `**${d.agent}**${d.taskInfo ?? ""}`).join(", ")}`;
|
|
88
|
-
const blocks: string[] = [header, ""];
|
|
89
|
-
for (let index = 0; index < details.length; index++) {
|
|
90
|
-
const detail = details[index];
|
|
91
|
-
if (!detail) continue;
|
|
92
|
-
const sessionLine = formatSessionLine(detail);
|
|
93
|
-
blocks.push(`${index + 1}. ${detail.agent}${detail.taskInfo ?? ""}`);
|
|
94
|
-
blocks.push(detail.resultPreview.trim() ? detail.resultPreview : "(no output)");
|
|
95
|
-
if (sessionLine) blocks.push(sessionLine);
|
|
96
|
-
blocks.push("");
|
|
97
|
-
}
|
|
98
|
-
return blocks.join("\n").trimEnd();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function sendCompletion(pi: Pick<ExtensionAPI, "sendMessage">, details: SubagentNotifyDetails[]): void {
|
|
102
|
-
if (details.length === 0) return;
|
|
103
|
-
const content = details.length === 1
|
|
104
|
-
? formatSingleCompletion(details[0]!)
|
|
105
|
-
: formatGroupedCompletion(details);
|
|
106
|
-
pi.sendMessage(
|
|
107
|
-
{
|
|
108
|
-
customType: "subagent-notify",
|
|
109
|
-
content,
|
|
110
|
-
display: true,
|
|
111
|
-
},
|
|
112
|
-
{ triggerTurn: true },
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function completionBatchKey(result: SubagentResult): string {
|
|
117
|
-
const sessionId = typeof result.sessionId === "string" ? result.sessionId.trim() : "";
|
|
118
|
-
if (sessionId) return `session:${sessionId}`;
|
|
119
|
-
const cwd = typeof result.cwd === "string" ? result.cwd.trim() : "";
|
|
120
|
-
return cwd ? `cwd:${cwd}` : "unknown";
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function buildCompletionDetails(result: SubagentResult): SubagentNotifyDetails {
|
|
124
|
-
const agent = result.agent ?? "unknown";
|
|
125
|
-
const summary = typeof result.summary === "string" ? result.summary : "";
|
|
126
|
-
const paused = !result.success && (
|
|
127
|
-
result.exitCode === 0
|
|
128
|
-
|| result.state === "paused"
|
|
129
|
-
|| summary.startsWith("Paused after interrupt.")
|
|
130
|
-
);
|
|
131
|
-
const status = paused ? "paused" : result.success ? "completed" : "failed";
|
|
132
|
-
|
|
133
|
-
const taskInfo =
|
|
134
|
-
result.taskIndex !== undefined && result.totalTasks !== undefined
|
|
135
|
-
? ` (${result.taskIndex + 1}/${result.totalTasks})`
|
|
136
|
-
: undefined;
|
|
137
|
-
|
|
138
|
-
const session =
|
|
139
|
-
result.shareUrl
|
|
140
|
-
? { label: "Session", value: result.shareUrl }
|
|
141
|
-
: result.shareError
|
|
142
|
-
? { label: "Session share error", value: result.shareError }
|
|
143
|
-
: result.sessionFile
|
|
144
|
-
? { label: "Session file", value: result.sessionFile }
|
|
145
|
-
: undefined;
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
agent,
|
|
149
|
-
status,
|
|
150
|
-
...(taskInfo ? { taskInfo } : {}),
|
|
151
|
-
resultPreview: summary,
|
|
152
|
-
...(typeof result.durationMs === "number" ? { durationMs: result.durationMs } : {}),
|
|
153
|
-
...(session ? { sessionLabel: session.label, sessionValue: session.value } : {}),
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export default function registerSubagentNotify(
|
|
158
|
-
pi: ExtensionAPI,
|
|
159
|
-
state: Pick<SubagentState, "currentSessionId">,
|
|
160
|
-
options: RegisterSubagentNotifyOptions = {},
|
|
161
|
-
): void {
|
|
162
|
-
const unsubscribeStoreKey = "__pi_subagents_notify_unsubscribe__";
|
|
163
|
-
const batcherStoreKey = "__pi_subagents_notify_batcher__";
|
|
164
|
-
const globalStore = globalThis as Record<string, unknown>;
|
|
165
|
-
const previousUnsubscribe = globalStore[unsubscribeStoreKey];
|
|
166
|
-
if (typeof previousUnsubscribe === "function") {
|
|
167
|
-
try {
|
|
168
|
-
previousUnsubscribe();
|
|
169
|
-
} catch {
|
|
170
|
-
// Best effort cleanup for stale handlers from an older reload.
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
const previousBatcher = globalStore[batcherStoreKey];
|
|
174
|
-
if (previousBatcher && typeof (previousBatcher as { dispose?: () => void }).dispose === "function") {
|
|
175
|
-
try {
|
|
176
|
-
(previousBatcher as { dispose: () => void }).dispose();
|
|
177
|
-
} catch {
|
|
178
|
-
// Best effort cleanup for a stale batcher from an older reload.
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const seen = getGlobalSeenMap("__pi_subagents_notify_seen__");
|
|
183
|
-
const ttlMs = 10 * 60 * 1000;
|
|
184
|
-
const nowFn = options.now ?? Date.now;
|
|
185
|
-
const batchConfig = resolveCompletionBatchConfig(options.batchConfig);
|
|
186
|
-
const batchers = new Map<string, CompletionBatcher<SubagentNotifyDetails>>();
|
|
187
|
-
globalStore[batcherStoreKey] = {
|
|
188
|
-
dispose() {
|
|
189
|
-
for (const batcher of batchers.values()) batcher.dispose();
|
|
190
|
-
batchers.clear();
|
|
191
|
-
},
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
const handleComplete = (data: unknown) => {
|
|
195
|
-
const result = data as SubagentResult;
|
|
196
|
-
if (typeof result.sessionId !== "string" || result.sessionId !== state.currentSessionId) return;
|
|
197
|
-
const now = nowFn();
|
|
198
|
-
const key = buildCompletionKey(result, "notify");
|
|
199
|
-
if (markSeenWithTtl(seen, key, now, ttlMs)) return;
|
|
200
|
-
|
|
201
|
-
const details = buildCompletionDetails(result);
|
|
202
|
-
const batchKey = completionBatchKey(result);
|
|
203
|
-
let batcher = batchers.get(batchKey);
|
|
204
|
-
if (!batcher) {
|
|
205
|
-
batcher = createCompletionBatcher<SubagentNotifyDetails>({
|
|
206
|
-
config: batchConfig,
|
|
207
|
-
emit: (items) => sendCompletion(pi, items),
|
|
208
|
-
...(options.timers ? { timers: options.timers } : {}),
|
|
209
|
-
now: nowFn,
|
|
210
|
-
});
|
|
211
|
-
batchers.set(batchKey, batcher);
|
|
212
|
-
}
|
|
213
|
-
if (details.status !== "completed") {
|
|
214
|
-
// Failures and paused runs bypass grouping. Flush any held
|
|
215
|
-
// successes for the same owner first so they are not stranded
|
|
216
|
-
// behind this signal, then emit the non-completion result immediately.
|
|
217
|
-
batcher.flush();
|
|
218
|
-
sendCompletion(pi, [details]);
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
batcher.push(details);
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
globalStore[unsubscribeStoreKey] = pi.events.on(SUBAGENT_ASYNC_COMPLETE_EVENT, handleComplete);
|
|
225
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { AsyncParallelGroupStatus } from "../../shared/types.ts";
|
|
2
|
-
|
|
3
|
-
function isValidParallelGroup(group: unknown, stepCount: number, chainStepCount: number): group is AsyncParallelGroupStatus {
|
|
4
|
-
if (typeof group !== "object" || group === null) return false;
|
|
5
|
-
const { start, count, stepIndex } = group as Partial<AsyncParallelGroupStatus>;
|
|
6
|
-
return typeof start === "number"
|
|
7
|
-
&& typeof count === "number"
|
|
8
|
-
&& typeof stepIndex === "number"
|
|
9
|
-
&& Number.isInteger(start)
|
|
10
|
-
&& Number.isInteger(count)
|
|
11
|
-
&& Number.isInteger(stepIndex)
|
|
12
|
-
&& start >= 0
|
|
13
|
-
&& count > 0
|
|
14
|
-
&& stepIndex >= 0
|
|
15
|
-
&& stepIndex < chainStepCount
|
|
16
|
-
&& start + count <= stepCount;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function normalizeParallelGroups(groups: unknown, stepCount: number, chainStepCount: number): AsyncParallelGroupStatus[] {
|
|
20
|
-
if (!Array.isArray(groups)) return [];
|
|
21
|
-
return groups
|
|
22
|
-
.filter((group): group is AsyncParallelGroupStatus => isValidParallelGroup(group, stepCount, chainStepCount))
|
|
23
|
-
.sort((left, right) => left.stepIndex - right.stepIndex || left.start - right.start);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function flatToLogicalStepIndex(flatIndex: number, chainStepCount: number, groups: AsyncParallelGroupStatus[]): number {
|
|
27
|
-
let logicalIndex = 0;
|
|
28
|
-
let cursor = 0;
|
|
29
|
-
for (const group of groups) {
|
|
30
|
-
while (cursor < group.start && logicalIndex < chainStepCount) {
|
|
31
|
-
if (cursor === flatIndex) return logicalIndex;
|
|
32
|
-
cursor++;
|
|
33
|
-
logicalIndex++;
|
|
34
|
-
}
|
|
35
|
-
if (flatIndex >= group.start && flatIndex < group.start + group.count) return group.stepIndex;
|
|
36
|
-
cursor = group.start + group.count;
|
|
37
|
-
logicalIndex = group.stepIndex + 1;
|
|
38
|
-
}
|
|
39
|
-
while (cursor <= flatIndex && logicalIndex < chainStepCount) {
|
|
40
|
-
if (cursor === flatIndex) return logicalIndex;
|
|
41
|
-
cursor++;
|
|
42
|
-
logicalIndex++;
|
|
43
|
-
}
|
|
44
|
-
return Math.max(0, chainStepCount - 1);
|
|
45
|
-
}
|
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import { buildCompletionKey, markSeenWithTtl } from "./completion-dedupe.ts";
|
|
4
|
-
import { createFileCoalescer } from "../../shared/file-coalescer.ts";
|
|
5
|
-
import {
|
|
6
|
-
SUBAGENT_ASYNC_COMPLETE_EVENT,
|
|
7
|
-
type IntercomEventBus,
|
|
8
|
-
type NestedRunSummary,
|
|
9
|
-
type SubagentResultIntercomChild,
|
|
10
|
-
type SubagentState,
|
|
11
|
-
} from "../../shared/types.ts";
|
|
12
|
-
import {
|
|
13
|
-
attachNestedChildrenToResultChildren,
|
|
14
|
-
buildSubagentResultIntercomPayload,
|
|
15
|
-
compactNestedResultChildren,
|
|
16
|
-
deliverSubagentResultIntercomEvent,
|
|
17
|
-
resolveSubagentResultStatus,
|
|
18
|
-
} from "../../intercom/result-intercom.ts";
|
|
19
|
-
import { projectNestedRegistryForRoot, sanitizeSummary } from "../shared/nested-events.ts";
|
|
20
|
-
|
|
21
|
-
const WATCHER_RESTART_DELAY_MS = 3000;
|
|
22
|
-
const POLL_INTERVAL_MS = 3000;
|
|
23
|
-
|
|
24
|
-
type ResultWatcherFs = Pick<typeof fs, "existsSync" | "readFileSync" | "unlinkSync" | "readdirSync" | "mkdirSync" | "realpathSync" | "watch">;
|
|
25
|
-
|
|
26
|
-
type ResultWatcherTimers = {
|
|
27
|
-
setTimeout: typeof setTimeout;
|
|
28
|
-
clearTimeout: typeof clearTimeout;
|
|
29
|
-
setInterval: typeof setInterval;
|
|
30
|
-
clearInterval: typeof clearInterval;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type ResultWatcherDeps = {
|
|
34
|
-
fs?: ResultWatcherFs;
|
|
35
|
-
timers?: ResultWatcherTimers;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
type ResultFileChild = {
|
|
39
|
-
agent?: string;
|
|
40
|
-
output?: string;
|
|
41
|
-
error?: string;
|
|
42
|
-
success?: boolean;
|
|
43
|
-
sessionFile?: string;
|
|
44
|
-
artifactPaths?: { outputPath?: string };
|
|
45
|
-
intercomTarget?: string;
|
|
46
|
-
children?: unknown;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
type ResultFileData = {
|
|
50
|
-
id?: string;
|
|
51
|
-
runId?: string;
|
|
52
|
-
agent?: string;
|
|
53
|
-
success?: boolean;
|
|
54
|
-
state?: string;
|
|
55
|
-
mode?: string;
|
|
56
|
-
summary?: string;
|
|
57
|
-
results?: ResultFileChild[];
|
|
58
|
-
nestedChildren?: unknown;
|
|
59
|
-
sessionId?: string;
|
|
60
|
-
cwd?: string;
|
|
61
|
-
sessionFile?: string;
|
|
62
|
-
asyncDir?: string;
|
|
63
|
-
intercomTarget?: string;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
function sanitizeNestedResultChildren(value: unknown, resultPath: string, label: string): NestedRunSummary[] | undefined {
|
|
67
|
-
if (value === undefined) return undefined;
|
|
68
|
-
if (!Array.isArray(value)) {
|
|
69
|
-
console.error(`Ignoring invalid nested children in subagent result file '${resultPath}' at ${label}: expected an array.`);
|
|
70
|
-
return undefined;
|
|
71
|
-
}
|
|
72
|
-
const children = value.map((child) => sanitizeSummary(child)).filter((child): child is NestedRunSummary => Boolean(child));
|
|
73
|
-
if (children.length !== value.length) {
|
|
74
|
-
console.error(`Ignoring ${value.length - children.length} invalid nested child record(s) in subagent result file '${resultPath}' at ${label}.`);
|
|
75
|
-
}
|
|
76
|
-
return children.length ? children : undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function getErrorCode(error: unknown): string | undefined {
|
|
80
|
-
return typeof error === "object" && error !== null && "code" in error
|
|
81
|
-
? (error as NodeJS.ErrnoException).code
|
|
82
|
-
: undefined;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function isNotFoundError(error: unknown): boolean {
|
|
86
|
-
return getErrorCode(error) === "ENOENT";
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function shouldFallBackToPolling(error: unknown): boolean {
|
|
90
|
-
const code = getErrorCode(error);
|
|
91
|
-
return code === "EMFILE" || code === "ENOSPC";
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function resolveNativeWatchDir(fsApi: ResultWatcherFs, resultsDir: string): string {
|
|
95
|
-
try {
|
|
96
|
-
return fsApi.realpathSync.native(resultsDir);
|
|
97
|
-
} catch {
|
|
98
|
-
return resultsDir;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function createResultWatcher(
|
|
103
|
-
pi: { events: IntercomEventBus },
|
|
104
|
-
state: SubagentState,
|
|
105
|
-
resultsDir: string,
|
|
106
|
-
completionTtlMs: number,
|
|
107
|
-
deps: ResultWatcherDeps = {},
|
|
108
|
-
): {
|
|
109
|
-
startResultWatcher: () => void;
|
|
110
|
-
primeExistingResults: () => void;
|
|
111
|
-
stopResultWatcher: () => void;
|
|
112
|
-
} {
|
|
113
|
-
const fsApi = deps.fs ?? fs;
|
|
114
|
-
const timers = deps.timers ?? { setTimeout, clearTimeout, setInterval, clearInterval };
|
|
115
|
-
|
|
116
|
-
const handleResult = async (file: string) => {
|
|
117
|
-
const resultPath = path.join(resultsDir, file);
|
|
118
|
-
if (!fsApi.existsSync(resultPath)) return;
|
|
119
|
-
try {
|
|
120
|
-
const data = JSON.parse(fsApi.readFileSync(resultPath, "utf-8")) as ResultFileData;
|
|
121
|
-
if (typeof data.sessionId !== "string" || data.sessionId !== state.currentSessionId) return;
|
|
122
|
-
|
|
123
|
-
const runId = data.runId ?? data.id ?? file.replace(/\.json$/i, "");
|
|
124
|
-
const hasExplicitNestedChildren = data.nestedChildren !== undefined;
|
|
125
|
-
let nestedChildren = compactNestedResultChildren(sanitizeNestedResultChildren(data.nestedChildren, resultPath, "nestedChildren"));
|
|
126
|
-
if (!nestedChildren?.length && !hasExplicitNestedChildren) {
|
|
127
|
-
try {
|
|
128
|
-
nestedChildren = compactNestedResultChildren(projectNestedRegistryForRoot(runId)?.children);
|
|
129
|
-
} catch (error) {
|
|
130
|
-
console.error(`Failed to enrich subagent result file '${resultPath}' with nested registry children; will retry later:`, error);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
const now = Date.now();
|
|
135
|
-
const completionKey = buildCompletionKey(data, `result:${file}`);
|
|
136
|
-
if (markSeenWithTtl(state.completionSeen, completionKey, now, completionTtlMs)) {
|
|
137
|
-
fsApi.unlinkSync(resultPath);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const hasResultChildren = Array.isArray(data.results) && data.results.length > 0;
|
|
142
|
-
const resultChildren = hasResultChildren
|
|
143
|
-
? data.results!
|
|
144
|
-
: [{
|
|
145
|
-
agent: data.agent,
|
|
146
|
-
output: data.summary,
|
|
147
|
-
success: data.success,
|
|
148
|
-
}];
|
|
149
|
-
const normalizedChildren = attachNestedChildrenToResultChildren(runId, resultChildren.map((result = {}, index): SubagentResultIntercomChild => {
|
|
150
|
-
const baseOutput = result.output ?? data.summary;
|
|
151
|
-
const hasRealOutput = typeof baseOutput === "string" && baseOutput.trim().length > 0;
|
|
152
|
-
const output = hasRealOutput ? baseOutput : "(no output)";
|
|
153
|
-
const summary = result.success === false && result.error
|
|
154
|
-
? `${result.error}${hasRealOutput ? `\n\nOutput:\n${baseOutput}` : ""}`
|
|
155
|
-
: output;
|
|
156
|
-
const sessionPath = result.sessionFile ?? (resultChildren.length === 1 ? data.sessionFile : undefined);
|
|
157
|
-
const childNestedChildren = sanitizeNestedResultChildren(result.children, resultPath, `results[${index}].children`);
|
|
158
|
-
return {
|
|
159
|
-
agent: result.agent ?? data.agent ?? `step-${index + 1}`,
|
|
160
|
-
status: resolveSubagentResultStatus({
|
|
161
|
-
success: result.success,
|
|
162
|
-
state: data.state === "paused" || typeof result.success !== "boolean" ? data.state : undefined,
|
|
163
|
-
}),
|
|
164
|
-
summary,
|
|
165
|
-
index,
|
|
166
|
-
artifactPath: result.artifactPaths?.outputPath,
|
|
167
|
-
...(typeof sessionPath === "string" && fsApi.existsSync(sessionPath) ? { sessionPath } : {}),
|
|
168
|
-
...(result.intercomTarget ? { intercomTarget: result.intercomTarget } : {}),
|
|
169
|
-
...(childNestedChildren ? { children: childNestedChildren } : {}),
|
|
170
|
-
};
|
|
171
|
-
}), nestedChildren);
|
|
172
|
-
|
|
173
|
-
const intercomTarget = data.intercomTarget?.trim();
|
|
174
|
-
if (intercomTarget) {
|
|
175
|
-
const mode = data.mode === "single" || data.mode === "parallel" || data.mode === "chain"
|
|
176
|
-
? data.mode
|
|
177
|
-
: resultChildren.length > 1 ? "chain" : "single";
|
|
178
|
-
const payload = buildSubagentResultIntercomPayload({
|
|
179
|
-
to: intercomTarget,
|
|
180
|
-
runId,
|
|
181
|
-
mode,
|
|
182
|
-
source: "async",
|
|
183
|
-
children: normalizedChildren,
|
|
184
|
-
asyncId: data.id,
|
|
185
|
-
asyncDir: data.asyncDir,
|
|
186
|
-
});
|
|
187
|
-
const delivered = await deliverSubagentResultIntercomEvent(pi.events, payload);
|
|
188
|
-
if (!delivered) {
|
|
189
|
-
console.error(`Subagent async grouped result intercom delivery was not acknowledged for '${resultPath}'.`);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
pi.events.emit(SUBAGENT_ASYNC_COMPLETE_EVENT, {
|
|
194
|
-
...data,
|
|
195
|
-
runId,
|
|
196
|
-
...(nestedChildren?.length ? { nestedChildren } : {}),
|
|
197
|
-
...(Array.isArray(data.results) ? {
|
|
198
|
-
results: hasResultChildren
|
|
199
|
-
? normalizedChildren.map((child, index) => ({
|
|
200
|
-
...data.results![index],
|
|
201
|
-
agent: child.agent,
|
|
202
|
-
status: child.status,
|
|
203
|
-
summary: child.summary,
|
|
204
|
-
index: child.index,
|
|
205
|
-
artifactPath: child.artifactPath,
|
|
206
|
-
sessionPath: child.sessionPath,
|
|
207
|
-
children: child.children,
|
|
208
|
-
}))
|
|
209
|
-
: [],
|
|
210
|
-
} : {}),
|
|
211
|
-
});
|
|
212
|
-
fsApi.unlinkSync(resultPath);
|
|
213
|
-
} catch (error) {
|
|
214
|
-
if (isNotFoundError(error)) return;
|
|
215
|
-
console.error(`Failed to process subagent result file '${resultPath}':`, error);
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
state.resultFileCoalescer = createFileCoalescer((file) => {
|
|
220
|
-
void handleResult(file);
|
|
221
|
-
}, 50);
|
|
222
|
-
|
|
223
|
-
const primeExistingResults = () => {
|
|
224
|
-
try {
|
|
225
|
-
fsApi.readdirSync(resultsDir)
|
|
226
|
-
.filter((f) => f.endsWith(".json"))
|
|
227
|
-
.forEach((file) => state.resultFileCoalescer.schedule(file, 0));
|
|
228
|
-
} catch (error) {
|
|
229
|
-
if (isNotFoundError(error)) return;
|
|
230
|
-
console.error(`Failed to scan subagent result directory '${resultsDir}':`, error);
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
const startPollingFallback = (reason: unknown) => {
|
|
235
|
-
state.watcher?.close();
|
|
236
|
-
state.watcher = null;
|
|
237
|
-
if (state.watcherRestartTimer) return;
|
|
238
|
-
|
|
239
|
-
console.error(
|
|
240
|
-
`Subagent result watcher for '${resultsDir}' fell back to polling because native fs.watch is unavailable (${getErrorCode(reason) ?? "unknown error"}).`,
|
|
241
|
-
);
|
|
242
|
-
primeExistingResults();
|
|
243
|
-
state.watcherRestartTimer = timers.setInterval(primeExistingResults, POLL_INTERVAL_MS);
|
|
244
|
-
state.watcherRestartTimer.unref?.();
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
const scheduleRestart = () => {
|
|
248
|
-
if (state.watcherRestartTimer) return;
|
|
249
|
-
state.watcherRestartTimer = timers.setTimeout(() => {
|
|
250
|
-
state.watcherRestartTimer = null;
|
|
251
|
-
try {
|
|
252
|
-
fsApi.mkdirSync(resultsDir, { recursive: true });
|
|
253
|
-
startResultWatcher();
|
|
254
|
-
} catch (error) {
|
|
255
|
-
if (shouldFallBackToPolling(error)) {
|
|
256
|
-
startPollingFallback(error);
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
console.error(`Failed to restart subagent result watcher for '${resultsDir}':`, error);
|
|
260
|
-
scheduleRestart();
|
|
261
|
-
}
|
|
262
|
-
}, WATCHER_RESTART_DELAY_MS);
|
|
263
|
-
state.watcherRestartTimer.unref?.();
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
const startResultWatcher = () => {
|
|
267
|
-
if (state.watcher) return;
|
|
268
|
-
if (state.watcherRestartTimer) {
|
|
269
|
-
timers.clearTimeout(state.watcherRestartTimer);
|
|
270
|
-
timers.clearInterval(state.watcherRestartTimer);
|
|
271
|
-
state.watcherRestartTimer = null;
|
|
272
|
-
}
|
|
273
|
-
try {
|
|
274
|
-
const watchDir = resolveNativeWatchDir(fsApi, resultsDir);
|
|
275
|
-
state.watcher = fsApi.watch(watchDir, (ev, file) => {
|
|
276
|
-
if (ev !== "rename" || !file) return;
|
|
277
|
-
const fileName = file.toString();
|
|
278
|
-
if (!fileName.endsWith(".json")) return;
|
|
279
|
-
state.resultFileCoalescer.schedule(fileName);
|
|
280
|
-
});
|
|
281
|
-
state.watcher.on("error", (error) => {
|
|
282
|
-
if (shouldFallBackToPolling(error)) {
|
|
283
|
-
startPollingFallback(error);
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
console.error(`Subagent result watcher failed for '${resultsDir}':`, error);
|
|
287
|
-
state.watcher?.close();
|
|
288
|
-
state.watcher = null;
|
|
289
|
-
scheduleRestart();
|
|
290
|
-
});
|
|
291
|
-
state.watcher.unref?.();
|
|
292
|
-
} catch (error) {
|
|
293
|
-
if (shouldFallBackToPolling(error)) {
|
|
294
|
-
startPollingFallback(error);
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
console.error(`Failed to start subagent result watcher for '${resultsDir}':`, error);
|
|
298
|
-
state.watcher = null;
|
|
299
|
-
scheduleRestart();
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
const stopResultWatcher = () => {
|
|
304
|
-
state.watcher?.close();
|
|
305
|
-
state.watcher = null;
|
|
306
|
-
if (state.watcherRestartTimer) {
|
|
307
|
-
timers.clearTimeout(state.watcherRestartTimer);
|
|
308
|
-
timers.clearInterval(state.watcherRestartTimer);
|
|
309
|
-
}
|
|
310
|
-
state.watcherRestartTimer = null;
|
|
311
|
-
state.resultFileCoalescer.clear();
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
return { startResultWatcher, primeExistingResults, stopResultWatcher };
|
|
315
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import { ASYNC_DIR, RESULTS_DIR, type SubagentState } from "../../shared/types.ts";
|
|
4
|
-
import { findAsyncRunPrefixMatches, type AsyncRunLocation } from "./async-resume.ts";
|
|
5
|
-
import { assertSafeNestedId, findNestedRunMatchesById, type NestedRoute, type NestedRunMatch, type NestedRunResolutionScope } from "../shared/nested-events.ts";
|
|
6
|
-
|
|
7
|
-
export type ResolvedSubagentRunId =
|
|
8
|
-
| { kind: "foreground"; id: string }
|
|
9
|
-
| { kind: "async"; id: string; location: AsyncRunLocation }
|
|
10
|
-
| { kind: "nested"; id: string; match: NestedRunMatch };
|
|
11
|
-
|
|
12
|
-
export interface ResolveSubagentRunIdDeps {
|
|
13
|
-
state?: SubagentState;
|
|
14
|
-
asyncDirRoot?: string;
|
|
15
|
-
resultsDir?: string;
|
|
16
|
-
nested?: NestedRunResolutionScope;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function exactAsyncLocation(id: string, asyncDirRoot: string, resultsDir: string): AsyncRunLocation | undefined {
|
|
20
|
-
const asyncDir = path.join(asyncDirRoot, id);
|
|
21
|
-
const resultPath = path.join(resultsDir, `${id}.json`);
|
|
22
|
-
if (!fs.existsSync(asyncDir) && !fs.existsSync(resultPath)) return undefined;
|
|
23
|
-
return {
|
|
24
|
-
asyncDir: fs.existsSync(asyncDir) ? asyncDir : null,
|
|
25
|
-
resultPath: fs.existsSync(resultPath) ? resultPath : null,
|
|
26
|
-
resolvedId: id,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function foregroundIds(state: SubagentState | undefined): string[] {
|
|
31
|
-
if (!state) return [];
|
|
32
|
-
return [...new Set([...state.foregroundControls.keys(), ...(state.foregroundRuns?.keys() ?? [])])];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function nestedScopeFromState(state: SubagentState | undefined): NestedRunResolutionScope | undefined {
|
|
36
|
-
if (!state) return undefined;
|
|
37
|
-
const routes: NestedRoute[] = [];
|
|
38
|
-
const seen = new Set<string>();
|
|
39
|
-
const add = (route: NestedRoute | undefined) => {
|
|
40
|
-
if (!route) return;
|
|
41
|
-
const key = `${route.rootRunId}:${route.eventSink}:${route.controlInbox}`;
|
|
42
|
-
if (seen.has(key)) return;
|
|
43
|
-
seen.add(key);
|
|
44
|
-
routes.push(route);
|
|
45
|
-
};
|
|
46
|
-
for (const control of state.foregroundControls.values()) add(control.nestedRoute as NestedRoute | undefined);
|
|
47
|
-
for (const job of state.asyncJobs.values()) add(job.nestedRoute as NestedRoute | undefined);
|
|
48
|
-
return { routes };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function asyncPrefixMatches(prefix: string, asyncDirRoot: string, resultsDir: string): Array<{ id: string; location: AsyncRunLocation }> {
|
|
52
|
-
return findAsyncRunPrefixMatches(prefix, asyncDirRoot, resultsDir);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function resolveSubagentRunId(id: string, deps: ResolveSubagentRunIdDeps = {}): ResolvedSubagentRunId | undefined {
|
|
56
|
-
assertSafeNestedId("id", id);
|
|
57
|
-
const asyncDirRoot = deps.asyncDirRoot ?? ASYNC_DIR;
|
|
58
|
-
const resultsDir = deps.resultsDir ?? RESULTS_DIR;
|
|
59
|
-
|
|
60
|
-
const nestedScope = deps.nested ?? nestedScopeFromState(deps.state);
|
|
61
|
-
if (deps.state?.foregroundControls.has(id) || deps.state?.foregroundRuns?.has(id)) return { kind: "foreground", id };
|
|
62
|
-
const exactAsync = exactAsyncLocation(id, asyncDirRoot, resultsDir);
|
|
63
|
-
if (exactAsync) return { kind: "async", id, location: exactAsync };
|
|
64
|
-
const exactNested = findNestedRunMatchesById(id, nestedScope ? { scope: nestedScope } : {});
|
|
65
|
-
if (exactNested.length > 1) throw new Error(`Nested run id '${id}' is ambiguous across authorized registries. Provide the full id after stale registries are cleaned up.`);
|
|
66
|
-
if (exactNested[0]) return { kind: "nested", id, match: exactNested[0] };
|
|
67
|
-
|
|
68
|
-
const matches: ResolvedSubagentRunId[] = [];
|
|
69
|
-
for (const foregroundId of foregroundIds(deps.state).filter((candidate) => candidate.startsWith(id))) {
|
|
70
|
-
matches.push({ kind: "foreground", id: foregroundId });
|
|
71
|
-
}
|
|
72
|
-
for (const match of asyncPrefixMatches(id, asyncDirRoot, resultsDir)) {
|
|
73
|
-
matches.push({ kind: "async", id: match.id, location: match.location });
|
|
74
|
-
}
|
|
75
|
-
for (const match of findNestedRunMatchesById(id, nestedScope ? { prefix: true, scope: nestedScope } : { prefix: true })) {
|
|
76
|
-
matches.push({ kind: "nested", id: match.run.id, match });
|
|
77
|
-
}
|
|
78
|
-
const unique = new Map(matches.map((match) => [`${match.kind}:${match.id}`, match]));
|
|
79
|
-
const values = [...unique.values()];
|
|
80
|
-
if (values.length > 1) {
|
|
81
|
-
throw new Error(`Ambiguous subagent run id prefix '${id}' matched: ${values.map((match) => `${match.kind}:${match.id}`).join(", ")}. Provide a longer id.`);
|
|
82
|
-
}
|
|
83
|
-
return values[0];
|
|
84
|
-
}
|