@bastani/atomic 0.9.12 → 0.9.13-alpha.1
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 +20 -0
- package/dist/builtin/intercom/CHANGELOG.md +6 -0
- package/dist/builtin/intercom/README.md +5 -12
- package/dist/builtin/intercom/broker/client.ts +11 -2
- package/dist/builtin/intercom/contact-supervisor-tool.ts +13 -3
- package/dist/builtin/intercom/index-heavy.ts +13 -3
- package/dist/builtin/intercom/index.ts +43 -19
- package/dist/builtin/intercom/intercom-utils.ts +15 -29
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/intercom/source-ownership.ts +9 -12
- package/dist/builtin/mcp/CHANGELOG.md +6 -0
- package/dist/builtin/mcp/README.md +3 -1
- package/dist/builtin/mcp/index.ts +10 -16
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/mcp/startup-warmup.ts +20 -8
- package/dist/builtin/subagents/CHANGELOG.md +30 -0
- package/dist/builtin/subagents/README.md +22 -30
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/src/extension/doctor.ts +1 -1
- package/dist/builtin/subagents/src/extension/fanout-child.ts +76 -56
- package/dist/builtin/subagents/src/extension/index.ts +23 -32
- package/dist/builtin/subagents/src/extension/startup-maintenance.ts +25 -44
- package/dist/builtin/subagents/src/intercom/result-intercom.ts +7 -7
- package/dist/builtin/subagents/src/runs/background/async-job-tracker.ts +166 -434
- package/dist/builtin/subagents/src/runs/background/notify.ts +29 -8
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-dynamic-step.ts +2 -2
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-parallel-runner.ts +4 -4
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-parallel-step.ts +2 -2
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-sequential-step.ts +2 -2
- package/dist/builtin/subagents/src/runs/foreground/execution-run-sync.ts +1 -281
- package/dist/builtin/subagents/src/runs/foreground/execution-structured-retries.ts +11 -75
- package/dist/builtin/subagents/src/runs/foreground/inprocess-run-sync.ts +457 -0
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-async.ts +119 -246
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-chain.ts +1 -1
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-context.ts +18 -9
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-parallel-task.ts +1 -1
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-parallel.ts +4 -4
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-resume.ts +1 -646
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-runtime.ts +2 -7
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-single.ts +5 -5
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-status.ts +7 -8
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-types.ts +14 -5
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor.ts +158 -93
- package/dist/builtin/subagents/src/runs/inprocess/background-single.ts +179 -0
- package/dist/builtin/subagents/src/runs/inprocess/background.ts +89 -0
- package/dist/builtin/subagents/src/runs/inprocess/child-policy.ts +27 -0
- package/dist/builtin/subagents/src/runs/inprocess/control-registry.ts +55 -0
- package/dist/builtin/subagents/src/runs/inprocess/control-status.ts +106 -0
- package/dist/builtin/subagents/src/runs/inprocess/index.ts +50 -0
- package/dist/builtin/subagents/src/runs/inprocess/nested-routing.ts +218 -0
- package/dist/builtin/subagents/src/runs/{shared/subagent-prompt-runtime.ts → inprocess/prompt-behavior.ts} +37 -75
- package/dist/builtin/subagents/src/runs/inprocess/runner.ts +1138 -0
- package/dist/builtin/subagents/src/runs/{shared/nested-events.ts → inprocess/runtime-support/nested-api.ts} +13 -7
- package/dist/builtin/subagents/src/runs/{shared/nested-events-control.ts → inprocess/runtime-support/nested-control.ts} +18 -17
- package/dist/builtin/subagents/src/runs/{shared/nested-events-core.ts → inprocess/runtime-support/nested-core.ts} +14 -61
- package/dist/builtin/subagents/src/runs/{shared/nested-events-projection.ts → inprocess/runtime-support/nested-projection.ts} +4 -10
- package/dist/builtin/subagents/src/runs/{shared/nested-events-registry.ts → inprocess/runtime-support/nested-registry.ts} +19 -4
- package/dist/builtin/subagents/src/runs/{shared/nested-render.ts → inprocess/runtime-support/nested-rendering.ts} +3 -3
- package/dist/builtin/subagents/src/runs/{shared/nested-events-sanitize.ts → inprocess/runtime-support/nested-sanitize.ts} +3 -3
- package/dist/builtin/subagents/src/runs/inprocess.ts +1 -0
- package/dist/builtin/subagents/src/runs/shared/dynamic-fanout.ts +10 -4
- package/dist/builtin/subagents/src/runs/shared/model-candidate-filter.ts +0 -1
- package/dist/builtin/subagents/src/runs/shared/model-fallback.ts +15 -492
- package/dist/builtin/subagents/src/runs/shared/parallel-utils.ts +13 -11
- package/dist/builtin/subagents/src/runs/shared/run-history.ts +3 -5
- package/dist/builtin/subagents/src/runs/shared/single-output.ts +4 -4
- package/dist/builtin/subagents/src/runs/shared/workflow-graph.ts +5 -5
- package/dist/builtin/subagents/src/shared/types-async.ts +0 -1
- package/dist/builtin/subagents/src/shared/types-config.ts +18 -0
- package/dist/builtin/subagents/src/shared/types-results.ts +9 -2
- package/dist/builtin/subagents/src/slash/prompt-template-bridge.ts +2 -3
- package/dist/builtin/subagents/src/slash/slash-live-state.ts +2 -2
- package/dist/builtin/subagents/src/tui/render-chain-graph.ts +10 -4
- package/dist/builtin/subagents/src/tui/render-event-formatting.ts +12 -11
- package/dist/builtin/subagents/src/tui/render-result-compact.ts +49 -12
- package/dist/builtin/subagents/src/tui/render-result.ts +46 -17
- package/dist/builtin/subagents/src/tui/render-stable-output.ts +1 -1
- package/dist/builtin/subagents/src/tui/render-status-progress.ts +8 -5
- package/dist/builtin/subagents/src/tui/render-widget-graph.ts +0 -8
- package/dist/builtin/subagents/src/tui/render-widget.ts +0 -24
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +25 -0
- package/dist/builtin/workflows/README.md +13 -3
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/durable/backend.ts +7 -0
- package/dist/builtin/workflows/src/durable/dbos-metadata.ts +11 -1
- package/dist/builtin/workflows/src/durable/resume-runtime.ts +1 -0
- package/dist/builtin/workflows/src/durable/tool-primitive.ts +4 -28
- package/dist/builtin/workflows/src/durable/types.ts +5 -0
- package/dist/builtin/workflows/src/engine/run.ts +17 -1
- package/dist/builtin/workflows/src/extension/atomic-stage-session.ts +12 -39
- package/dist/builtin/workflows/src/extension/config-file-loader.ts +10 -1
- package/dist/builtin/workflows/src/extension/config-loader.ts +1 -1
- package/dist/builtin/workflows/src/extension/dispatcher.ts +4 -0
- package/dist/builtin/workflows/src/extension/index.bundle.mjs +1151 -737
- package/dist/builtin/workflows/src/extension/lifecycle-notifications.ts +303 -38
- package/dist/builtin/workflows/src/extension/render-result.ts +4 -0
- package/dist/builtin/workflows/src/extension/runtime-durable-resume.ts +6 -3
- package/dist/builtin/workflows/src/extension/runtime.ts +7 -1
- package/dist/builtin/workflows/src/extension/wiring.ts +53 -11
- package/dist/builtin/workflows/src/extension/workflow-command-registration.ts +12 -3
- package/dist/builtin/workflows/src/extension/workflow-durable-resume-command.ts +5 -2
- package/dist/builtin/workflows/src/extension/workflow-run-control-command.ts +9 -7
- package/dist/builtin/workflows/src/extension/workflow-status-summary.ts +11 -0
- package/dist/builtin/workflows/src/extension/workflow-tool-content.ts +18 -1
- package/dist/builtin/workflows/src/extension/workflow-tool-control.ts +10 -8
- package/dist/builtin/workflows/src/extension/workflow-tool.ts +9 -4
- package/dist/builtin/workflows/src/runs/background/quit.ts +28 -4
- package/dist/builtin/workflows/src/runs/background/resume-acknowledgements.ts +2 -1
- package/dist/builtin/workflows/src/runs/background/status.ts +55 -8
- package/dist/builtin/workflows/src/runs/foreground/executor-direct-helpers.ts +4 -0
- package/dist/builtin/workflows/src/runs/foreground/executor-prompt-nodes.ts +3 -1
- package/dist/builtin/workflows/src/runs/foreground/executor-stage-control.ts +1 -1
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-context.ts +1 -1
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-controller.ts +688 -38
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-types.ts +3 -1
- package/dist/builtin/workflows/src/runs/shared/model-fallback-failures.ts +19 -514
- package/dist/builtin/workflows/src/runs/shared/retry.ts +44 -0
- package/dist/builtin/workflows/src/shared/authoring-contract-stage.d.ts +5 -0
- package/dist/builtin/workflows/src/shared/authoring-contract-stage.ts +6 -0
- package/dist/builtin/workflows/src/shared/authoring-contract-ui.d.ts +5 -1
- package/dist/builtin/workflows/src/shared/authoring-contract-ui.ts +5 -0
- package/dist/builtin/workflows/src/shared/persistence-restore-helpers.ts +5 -0
- package/dist/builtin/workflows/src/shared/persistence-restore.ts +3 -0
- package/dist/builtin/workflows/src/shared/persistence-session-entries.ts +4 -0
- package/dist/builtin/workflows/src/shared/run-indicator-status.ts +75 -0
- package/dist/builtin/workflows/src/shared/store-public-types.ts +43 -7
- package/dist/builtin/workflows/src/shared/store-run-methods.ts +42 -8
- package/dist/builtin/workflows/src/shared/store-stage-methods.ts +31 -8
- package/dist/builtin/workflows/src/shared/store-types.ts +34 -0
- package/dist/builtin/workflows/src/shared/timing.ts +10 -0
- package/dist/builtin/workflows/src/tui/chat-surface-message.ts +22 -2
- package/dist/builtin/workflows/src/tui/session-list.ts +1 -1
- package/dist/builtin/workflows/src/tui/session-overlays.ts +14 -10
- package/dist/builtin/workflows/src/tui/session-picker.ts +20 -4
- package/dist/builtin/workflows/src/tui/status-list.ts +31 -47
- package/dist/builtin/workflows/src/tui/store-widget-installer.ts +9 -1
- package/dist/builtin/workflows/src/tui/widget.ts +16 -46
- package/dist/core/agent-session-auto-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-auto-compaction.js +54 -11
- package/dist/core/agent-session-auto-compaction.js.map +1 -1
- package/dist/core/agent-session-events.d.ts.map +1 -1
- package/dist/core/agent-session-events.js +49 -14
- package/dist/core/agent-session-events.js.map +1 -1
- package/dist/core/agent-session-methods.d.ts +13 -0
- package/dist/core/agent-session-methods.d.ts.map +1 -1
- package/dist/core/agent-session-methods.js.map +1 -1
- package/dist/core/agent-session-models.d.ts.map +1 -1
- package/dist/core/agent-session-models.js +12 -1
- package/dist/core/agent-session-models.js.map +1 -1
- package/dist/core/agent-session-prompt.d.ts.map +1 -1
- package/dist/core/agent-session-prompt.js +4 -0
- package/dist/core/agent-session-prompt.js.map +1 -1
- package/dist/core/agent-session-retry.d.ts +21 -4
- package/dist/core/agent-session-retry.d.ts.map +1 -1
- package/dist/core/agent-session-retry.js +232 -106
- package/dist/core/agent-session-retry.js.map +1 -1
- package/dist/core/agent-session-state.d.ts.map +1 -1
- package/dist/core/agent-session-state.js +2 -1
- package/dist/core/agent-session-state.js.map +1 -1
- package/dist/core/agent-session-tool-hooks.d.ts.map +1 -1
- package/dist/core/agent-session-tool-hooks.js +12 -0
- package/dist/core/agent-session-tool-hooks.js.map +1 -1
- package/dist/core/agent-session-tool-registry.d.ts.map +1 -1
- package/dist/core/agent-session-tool-registry.js +1 -1
- package/dist/core/agent-session-tool-registry.js.map +1 -1
- package/dist/core/agent-session-types.d.ts +3 -1
- package/dist/core/agent-session-types.d.ts.map +1 -1
- package/dist/core/agent-session-types.js.map +1 -1
- package/dist/core/agent-session.d.ts +12 -1
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +7 -0
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/extensions/context-types.d.ts +25 -0
- package/dist/core/extensions/context-types.d.ts.map +1 -1
- package/dist/core/extensions/context-types.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/runner-context.d.ts +2 -1
- package/dist/core/extensions/runner-context.d.ts.map +1 -1
- package/dist/core/extensions/runner-context.js +4 -0
- package/dist/core/extensions/runner-context.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +3 -2
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +3 -1
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/model-fallback-failures.d.ts +30 -0
- package/dist/core/model-fallback-failures.d.ts.map +1 -0
- package/dist/core/model-fallback-failures.js +610 -0
- package/dist/core/model-fallback-failures.js.map +1 -0
- package/dist/core/retry-policy.d.ts +30 -0
- package/dist/core/retry-policy.d.ts.map +1 -0
- package/dist/core/retry-policy.js +25 -0
- package/dist/core/retry-policy.js.map +1 -0
- package/dist/core/sdk-types.d.ts +8 -2
- package/dist/core/sdk-types.d.ts.map +1 -1
- package/dist/core/sdk-types.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +6 -1
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager-core.d.ts +2 -0
- package/dist/core/session-manager-core.d.ts.map +1 -1
- package/dist/core/session-manager-core.js +7 -0
- package/dist/core/session-manager-core.js.map +1 -1
- package/dist/index-extensions.d.ts +1 -1
- package/dist/index-extensions.d.ts.map +1 -1
- package/dist/index-extensions.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/docs/intercom.md +5 -10
- package/docs/settings.md +7 -1
- package/docs/subagents.md +19 -19
- package/docs/workflows.md +21 -5
- package/examples/extensions/gondolin/package-lock.json +3 -3
- package/npm-shrinkwrap.json +29 -29
- package/package.json +2 -2
- package/dist/builtin/subagents/src/runs/background/async-event-journal.ts +0 -112
- package/dist/builtin/subagents/src/runs/background/async-execution-chain.ts +0 -543
- package/dist/builtin/subagents/src/runs/background/async-execution-common.ts +0 -140
- package/dist/builtin/subagents/src/runs/background/async-execution-single.ts +0 -279
- package/dist/builtin/subagents/src/runs/background/async-execution-types.ts +0 -99
- package/dist/builtin/subagents/src/runs/background/async-execution.ts +0 -17
- package/dist/builtin/subagents/src/runs/background/async-resume.ts +0 -402
- package/dist/builtin/subagents/src/runs/background/async-status.ts +0 -411
- package/dist/builtin/subagents/src/runs/background/completion-claims.ts +0 -196
- package/dist/builtin/subagents/src/runs/background/completion-dedupe.ts +0 -102
- package/dist/builtin/subagents/src/runs/background/parallel-groups.ts +0 -59
- package/dist/builtin/subagents/src/runs/background/result-delivery-processor.ts +0 -300
- package/dist/builtin/subagents/src/runs/background/result-file-claims.ts +0 -166
- package/dist/builtin/subagents/src/runs/background/result-quarantine.ts +0 -77
- package/dist/builtin/subagents/src/runs/background/result-retry-scheduler.ts +0 -48
- package/dist/builtin/subagents/src/runs/background/result-status.ts +0 -87
- package/dist/builtin/subagents/src/runs/background/result-watcher-data.ts +0 -72
- package/dist/builtin/subagents/src/runs/background/result-watcher.ts +0 -320
- package/dist/builtin/subagents/src/runs/background/run-id-resolver.ts +0 -105
- package/dist/builtin/subagents/src/runs/background/run-status.ts +0 -358
- package/dist/builtin/subagents/src/runs/background/stale-run-reconciler.ts +0 -457
- package/dist/builtin/subagents/src/runs/background/subagent-runner-dynamic.ts +0 -376
- package/dist/builtin/subagents/src/runs/background/subagent-runner-finalize.ts +0 -150
- package/dist/builtin/subagents/src/runs/background/subagent-runner-output.ts +0 -109
- package/dist/builtin/subagents/src/runs/background/subagent-runner-parallel-helpers.ts +0 -128
- package/dist/builtin/subagents/src/runs/background/subagent-runner-parallel.ts +0 -309
- package/dist/builtin/subagents/src/runs/background/subagent-runner-sequential.ts +0 -173
- package/dist/builtin/subagents/src/runs/background/subagent-runner-state.ts +0 -578
- package/dist/builtin/subagents/src/runs/background/subagent-runner-step.ts +0 -330
- package/dist/builtin/subagents/src/runs/background/subagent-runner-streaming.ts +0 -330
- package/dist/builtin/subagents/src/runs/background/subagent-runner-types.ts +0 -210
- package/dist/builtin/subagents/src/runs/background/subagent-runner-utils.ts +0 -69
- package/dist/builtin/subagents/src/runs/background/subagent-runner.ts +0 -90
- package/dist/builtin/subagents/src/runs/background/top-level-async.ts +0 -12
- package/dist/builtin/subagents/src/runs/foreground/execution-attempt-control.ts +0 -140
- package/dist/builtin/subagents/src/runs/foreground/execution-attempt-finalize.ts +0 -108
- package/dist/builtin/subagents/src/runs/foreground/execution-attempt-types.ts +0 -17
- package/dist/builtin/subagents/src/runs/foreground/execution-attempt.ts +0 -558
- package/dist/builtin/subagents/src/runs/shared/attempt-watchdog.ts +0 -126
- package/dist/builtin/subagents/src/runs/shared/final-drain.ts +0 -39
- package/dist/builtin/subagents/src/runs/shared/pi-args.ts +0 -339
- package/dist/builtin/subagents/src/runs/shared/pi-spawn.ts +0 -165
- package/dist/builtin/subagents/src/runs/shared/spawn-env.ts +0 -21
- package/dist/builtin/subagents/src/shared/post-exit-stdio-guard.ts +0 -86
- /package/dist/builtin/subagents/src/runs/{shared/nested-path.ts → inprocess/runtime-support/nested-paths.ts} +0 -0
|
@@ -1,578 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import { appendJsonl } from "../../shared/artifacts.ts";
|
|
4
|
-
import { writeAtomicJson } from "../../shared/atomic-json.ts";
|
|
5
|
-
import type { TokenUsage } from "../../shared/types.ts";
|
|
6
|
-
import { extractTextFromContent, extractToolArgsPreview } from "../../shared/utils.ts";
|
|
7
|
-
import {
|
|
8
|
-
createMutatingFailureState,
|
|
9
|
-
didMutatingToolFail,
|
|
10
|
-
isMutatingTool,
|
|
11
|
-
nextLongRunningTrigger,
|
|
12
|
-
recordMutatingFailure,
|
|
13
|
-
resetMutatingFailureState,
|
|
14
|
-
resolveCurrentPath,
|
|
15
|
-
shouldEscalateMutatingFailures,
|
|
16
|
-
summarizeRecentMutatingFailures,
|
|
17
|
-
} from "../shared/long-running-guard.ts";
|
|
18
|
-
import { nestedSummaryFromAsyncStatus, writeNestedEvent } from "../shared/nested-events.ts";
|
|
19
|
-
import { flattenSteps, isDynamicRunnerGroup, isParallelGroup } from "../shared/parallel-utils.ts";
|
|
20
|
-
import {
|
|
21
|
-
buildControlEvent,
|
|
22
|
-
claimControlNotification,
|
|
23
|
-
DEFAULT_CONTROL_CONFIG,
|
|
24
|
-
deriveActivityState,
|
|
25
|
-
formatControlIntercomMessage,
|
|
26
|
-
formatControlNoticeMessage,
|
|
27
|
-
} from "../shared/subagent-control.ts";
|
|
28
|
-
import type {
|
|
29
|
-
ChildEvent,
|
|
30
|
-
RunnerExecutionState,
|
|
31
|
-
RunnerStatusPayload,
|
|
32
|
-
RunnerStatusStep,
|
|
33
|
-
SubagentRunConfig,
|
|
34
|
-
} from "./subagent-runner-types.ts";
|
|
35
|
-
import { appendRecentStepOutput, resetStepLiveDetail } from "./subagent-runner-utils.ts";
|
|
36
|
-
|
|
37
|
-
const mutatingFailureWindowMs = 5 * 60_000;
|
|
38
|
-
|
|
39
|
-
function initialStepStatus(step: {
|
|
40
|
-
agent: string;
|
|
41
|
-
phase?: string;
|
|
42
|
-
label?: string;
|
|
43
|
-
outputName?: string;
|
|
44
|
-
structured?: boolean;
|
|
45
|
-
sessionFile?: string;
|
|
46
|
-
skills?: string[] | false;
|
|
47
|
-
model?: string;
|
|
48
|
-
thinking?: string;
|
|
49
|
-
fastMode?: boolean;
|
|
50
|
-
modelCandidates?: string[];
|
|
51
|
-
}): RunnerStatusStep {
|
|
52
|
-
return {
|
|
53
|
-
agent: step.agent,
|
|
54
|
-
phase: step.phase,
|
|
55
|
-
label: step.label,
|
|
56
|
-
outputName: step.outputName,
|
|
57
|
-
structured: step.structured,
|
|
58
|
-
status: "pending",
|
|
59
|
-
...(step.sessionFile ? { sessionFile: step.sessionFile } : {}),
|
|
60
|
-
skills: step.skills as RunnerStatusStep["skills"],
|
|
61
|
-
model: step.model,
|
|
62
|
-
thinking: step.thinking,
|
|
63
|
-
...(step.fastMode ? { fastMode: true } : {}),
|
|
64
|
-
attemptedModels:
|
|
65
|
-
step.modelCandidates && step.modelCandidates.length > 0
|
|
66
|
-
? step.modelCandidates
|
|
67
|
-
: step.model
|
|
68
|
-
? [step.model]
|
|
69
|
-
: undefined,
|
|
70
|
-
recentTools: [],
|
|
71
|
-
recentOutput: [],
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function createRunnerExecutionState(config: SubagentRunConfig): RunnerExecutionState {
|
|
76
|
-
const { id, steps, resultPath, cwd, placeholder, taskIndex, totalTasks, maxOutput, artifactsDir, artifactConfig } =
|
|
77
|
-
config;
|
|
78
|
-
const overallStartTime = Date.now();
|
|
79
|
-
const shareEnabled = config.share === true;
|
|
80
|
-
const asyncDir = config.asyncDir;
|
|
81
|
-
const statusPath = path.join(asyncDir, "status.json");
|
|
82
|
-
const eventsPath = path.join(asyncDir, "events.jsonl");
|
|
83
|
-
const logPath = path.join(asyncDir, `subagent-log-${id}.md`);
|
|
84
|
-
const controlConfig = config.controlConfig ?? DEFAULT_CONTROL_CONFIG;
|
|
85
|
-
const parallelGroups: Array<{ start: number; count: number; stepIndex: number }> = [];
|
|
86
|
-
const initialStatusSteps: RunnerStatusStep[] = [];
|
|
87
|
-
let flatStepCount = 0;
|
|
88
|
-
for (let stepIndex = 0; stepIndex < steps.length; stepIndex++) {
|
|
89
|
-
const step = steps[stepIndex]!;
|
|
90
|
-
if (isParallelGroup(step)) {
|
|
91
|
-
parallelGroups.push({ start: flatStepCount, count: step.parallel.length, stepIndex });
|
|
92
|
-
for (const task of step.parallel) initialStatusSteps.push(initialStepStatus(task));
|
|
93
|
-
flatStepCount += step.parallel.length;
|
|
94
|
-
} else if (isDynamicRunnerGroup(step)) {
|
|
95
|
-
parallelGroups.push({ start: flatStepCount, count: 1, stepIndex });
|
|
96
|
-
initialStatusSteps.push({
|
|
97
|
-
agent: `expand:${step.parallel.agent}`,
|
|
98
|
-
phase: step.phase ?? step.parallel.phase,
|
|
99
|
-
label: step.label ?? step.parallel.label ?? `Dynamic fanout (${step.collect.as})`,
|
|
100
|
-
outputName: step.collect.as,
|
|
101
|
-
structured: Boolean(step.collect.outputSchema),
|
|
102
|
-
status: "pending",
|
|
103
|
-
recentTools: [],
|
|
104
|
-
recentOutput: [],
|
|
105
|
-
});
|
|
106
|
-
flatStepCount++;
|
|
107
|
-
} else {
|
|
108
|
-
initialStatusSteps.push(initialStepStatus(step));
|
|
109
|
-
flatStepCount++;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
const flatSteps = flattenSteps(steps);
|
|
113
|
-
const sessionEnabled =
|
|
114
|
-
Boolean(config.sessionDir) || shareEnabled || flatSteps.some((step) => Boolean(step.sessionFile));
|
|
115
|
-
const statusPayload: RunnerStatusPayload = {
|
|
116
|
-
runId: id,
|
|
117
|
-
...(config.sessionId ? { sessionId: config.sessionId } : {}),
|
|
118
|
-
mode: config.resultMode ?? (flatSteps.length > 1 ? "chain" : "single"),
|
|
119
|
-
state: "running",
|
|
120
|
-
lastActivityAt: overallStartTime,
|
|
121
|
-
startedAt: overallStartTime,
|
|
122
|
-
lastUpdate: overallStartTime,
|
|
123
|
-
pid: process.pid,
|
|
124
|
-
cwd,
|
|
125
|
-
currentStep: 0,
|
|
126
|
-
chainStepCount: steps.length,
|
|
127
|
-
parallelGroups,
|
|
128
|
-
workflowGraph: config.workflowGraph,
|
|
129
|
-
steps: initialStatusSteps,
|
|
130
|
-
artifactsDir,
|
|
131
|
-
sessionDir: config.sessionDir,
|
|
132
|
-
outputFile: path.join(asyncDir, "output-0.log"),
|
|
133
|
-
};
|
|
134
|
-
fs.mkdirSync(asyncDir, { recursive: true });
|
|
135
|
-
const state: RunnerExecutionState = {
|
|
136
|
-
config,
|
|
137
|
-
id,
|
|
138
|
-
steps,
|
|
139
|
-
resultPath,
|
|
140
|
-
cwd,
|
|
141
|
-
placeholder,
|
|
142
|
-
taskIndex,
|
|
143
|
-
totalTasks,
|
|
144
|
-
maxOutput,
|
|
145
|
-
artifactsDir,
|
|
146
|
-
artifactConfig,
|
|
147
|
-
previousOutput: "",
|
|
148
|
-
outputs: {},
|
|
149
|
-
results: [],
|
|
150
|
-
overallStartTime,
|
|
151
|
-
shareEnabled,
|
|
152
|
-
asyncDir,
|
|
153
|
-
statusPath,
|
|
154
|
-
eventsPath,
|
|
155
|
-
logPath,
|
|
156
|
-
controlConfig,
|
|
157
|
-
interrupted: false,
|
|
158
|
-
previousCumulativeTokens: { input: 0, output: 0, total: 0 } satisfies TokenUsage,
|
|
159
|
-
flatSteps,
|
|
160
|
-
sessionEnabled,
|
|
161
|
-
statusPayload,
|
|
162
|
-
flatIndex: 0,
|
|
163
|
-
mutatingFailureStates: initialStatusSteps.map(() => createMutatingFailureState()),
|
|
164
|
-
pendingToolResults: initialStatusSteps.map(() => undefined),
|
|
165
|
-
emittedControlEventKeys: new Set(),
|
|
166
|
-
activeLongRunningSteps: new Set(),
|
|
167
|
-
};
|
|
168
|
-
writeStatusPayload(state);
|
|
169
|
-
return state;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function emitNestedSelfEvent(
|
|
173
|
-
state: RunnerExecutionState,
|
|
174
|
-
type: "subagent.nested.updated" | "subagent.nested.completed",
|
|
175
|
-
): void {
|
|
176
|
-
const { config, statusPayload, asyncDir, id } = state;
|
|
177
|
-
if (!config.nestedRoute || !config.nestedSelf) return;
|
|
178
|
-
try {
|
|
179
|
-
writeNestedEvent(config.nestedRoute, {
|
|
180
|
-
type,
|
|
181
|
-
ts: Date.now(),
|
|
182
|
-
parentRunId: config.nestedSelf.parentRunId,
|
|
183
|
-
parentStepIndex: config.nestedSelf.parentStepIndex,
|
|
184
|
-
child: nestedSummaryFromAsyncStatus(statusPayload, asyncDir, {
|
|
185
|
-
id,
|
|
186
|
-
parentRunId: config.nestedSelf.parentRunId,
|
|
187
|
-
parentStepIndex: config.nestedSelf.parentStepIndex,
|
|
188
|
-
depth: config.nestedSelf.depth,
|
|
189
|
-
path: config.nestedSelf.path,
|
|
190
|
-
mode: statusPayload.mode,
|
|
191
|
-
ts: Date.now(),
|
|
192
|
-
}),
|
|
193
|
-
});
|
|
194
|
-
} catch (error) {
|
|
195
|
-
console.error("Failed to emit nested async status event:", error);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function refreshWorkflowGraph(state: RunnerExecutionState): void {
|
|
200
|
-
const { config, statusPayload } = state;
|
|
201
|
-
if (!config.workflowGraph) return;
|
|
202
|
-
const graph = structuredClone(statusPayload.workflowGraph ?? config.workflowGraph);
|
|
203
|
-
const normalize = (
|
|
204
|
-
status: RunnerStatusStep["status"],
|
|
205
|
-
): "pending" | "running" | "completed" | "failed" | "paused" | "detached" => {
|
|
206
|
-
if (status === "complete" || status === "completed") return "completed";
|
|
207
|
-
if (status === "running" || status === "failed" || status === "paused" || status === "pending") return status;
|
|
208
|
-
return "pending";
|
|
209
|
-
};
|
|
210
|
-
const updateNode = (node: NonNullable<typeof graph.nodes>[number]): void => {
|
|
211
|
-
if (node.flatIndex !== undefined) {
|
|
212
|
-
const step = statusPayload.steps[node.flatIndex];
|
|
213
|
-
if (step) {
|
|
214
|
-
node.status = normalize(step.status);
|
|
215
|
-
node.error = step.error;
|
|
216
|
-
}
|
|
217
|
-
if (statusPayload.currentStep === node.flatIndex) graph.currentNodeId = node.id;
|
|
218
|
-
}
|
|
219
|
-
for (const child of node.children ?? []) updateNode(child);
|
|
220
|
-
if (node.children?.length) {
|
|
221
|
-
if (node.children.every((child) => child.status === "completed")) node.status = "completed";
|
|
222
|
-
else if (node.children.some((child) => child.status === "running")) node.status = "running";
|
|
223
|
-
else if (node.children.some((child) => child.status === "failed")) node.status = "failed";
|
|
224
|
-
else if (node.children.some((child) => child.status === "paused")) node.status = "paused";
|
|
225
|
-
}
|
|
226
|
-
if (node.error) node.status = "failed";
|
|
227
|
-
};
|
|
228
|
-
for (const node of graph.nodes) updateNode(node);
|
|
229
|
-
statusPayload.workflowGraph = graph;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export function writeStatusPayload(state: RunnerExecutionState): void {
|
|
233
|
-
refreshWorkflowGraph(state);
|
|
234
|
-
writeAtomicJson(state.statusPath, state.statusPayload);
|
|
235
|
-
emitNestedSelfEvent(
|
|
236
|
-
state,
|
|
237
|
-
state.statusPayload.state === "running" || state.statusPayload.state === "queued"
|
|
238
|
-
? "subagent.nested.updated"
|
|
239
|
-
: "subagent.nested.completed",
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export function markDynamicGraphGroup(
|
|
244
|
-
state: RunnerExecutionState,
|
|
245
|
-
stepIndex: number,
|
|
246
|
-
status: "completed" | "failed" | "running",
|
|
247
|
-
error?: string,
|
|
248
|
-
): void {
|
|
249
|
-
const groupNode = state.statusPayload.workflowGraph?.nodes.find((node) => node.id === `step-${stepIndex}`);
|
|
250
|
-
if (!groupNode) return;
|
|
251
|
-
groupNode.status = status;
|
|
252
|
-
groupNode.error = error;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function stepOutputActivityAt(state: RunnerExecutionState, index: number): number {
|
|
256
|
-
const step = state.statusPayload.steps[index];
|
|
257
|
-
let lastActivityAt = step?.lastActivityAt ?? step?.startedAt ?? state.overallStartTime;
|
|
258
|
-
const outputPath = path.join(state.asyncDir, `output-${index}.log`);
|
|
259
|
-
try {
|
|
260
|
-
lastActivityAt = Math.max(lastActivityAt, fs.statSync(outputPath).mtimeMs);
|
|
261
|
-
} catch (error) {
|
|
262
|
-
if ((error as NodeJS.ErrnoException).code !== "ENOENT")
|
|
263
|
-
console.error(`Failed to inspect async output file '${outputPath}':`, error);
|
|
264
|
-
}
|
|
265
|
-
return lastActivityAt;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function appendControlEvent(state: RunnerExecutionState, event: ReturnType<typeof buildControlEvent>) {
|
|
269
|
-
const { controlConfig, config, statusPayload, eventsPath } = state;
|
|
270
|
-
if (!controlConfig.enabled) return;
|
|
271
|
-
const childIntercomTarget = config.childIntercomTargets?.[event.index ?? statusPayload.currentStep];
|
|
272
|
-
const channels =
|
|
273
|
-
event.type === "active_long_running"
|
|
274
|
-
? controlConfig.notifyChannels.filter((channel) => channel !== "intercom")
|
|
275
|
-
: controlConfig.notifyChannels;
|
|
276
|
-
if (
|
|
277
|
-
channels.length === 0 ||
|
|
278
|
-
!claimControlNotification(controlConfig, event, state.emittedControlEventKeys, childIntercomTarget)
|
|
279
|
-
)
|
|
280
|
-
return;
|
|
281
|
-
appendJsonl(
|
|
282
|
-
eventsPath,
|
|
283
|
-
JSON.stringify({
|
|
284
|
-
type: "subagent.control",
|
|
285
|
-
event,
|
|
286
|
-
channels,
|
|
287
|
-
childIntercomTarget,
|
|
288
|
-
noticeText: formatControlNoticeMessage(event, childIntercomTarget),
|
|
289
|
-
...(config.controlIntercomTarget && channels.includes("intercom")
|
|
290
|
-
? {
|
|
291
|
-
intercom: {
|
|
292
|
-
to: config.controlIntercomTarget,
|
|
293
|
-
message: formatControlIntercomMessage(event, childIntercomTarget),
|
|
294
|
-
},
|
|
295
|
-
}
|
|
296
|
-
: {}),
|
|
297
|
-
}),
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function syncTopLevelCurrentTool(statusPayload: RunnerStatusPayload): void {
|
|
302
|
-
const activeStep = statusPayload.steps
|
|
303
|
-
.filter(
|
|
304
|
-
(step) => step.status === "running" && typeof step.currentTool === "string" && step.currentTool.length > 0,
|
|
305
|
-
)
|
|
306
|
-
.sort((left, right) => (right.currentToolStartedAt ?? 0) - (left.currentToolStartedAt ?? 0))[0];
|
|
307
|
-
statusPayload.currentTool = activeStep?.currentTool;
|
|
308
|
-
statusPayload.currentToolStartedAt = activeStep?.currentToolStartedAt;
|
|
309
|
-
statusPayload.currentPath = activeStep?.currentPath;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function maybeEmitActiveLongRunning(state: RunnerExecutionState, flatIndex: number, now: number): boolean {
|
|
313
|
-
const { controlConfig, statusPayload, id } = state;
|
|
314
|
-
if (!controlConfig.enabled || state.activeLongRunningSteps.has(flatIndex)) return false;
|
|
315
|
-
const step = statusPayload.steps[flatIndex];
|
|
316
|
-
if (step?.status !== "running" || step.activityState === "needs_attention") return false;
|
|
317
|
-
const reason = nextLongRunningTrigger(controlConfig, {
|
|
318
|
-
startedAt: step.startedAt ?? state.overallStartTime,
|
|
319
|
-
now,
|
|
320
|
-
turns: step.turnCount ?? 0,
|
|
321
|
-
tokens: step.tokens?.total ?? 0,
|
|
322
|
-
});
|
|
323
|
-
if (!reason) return false;
|
|
324
|
-
state.activeLongRunningSteps.add(flatIndex);
|
|
325
|
-
const previous = step.activityState;
|
|
326
|
-
step.activityState = "active_long_running";
|
|
327
|
-
statusPayload.activityState =
|
|
328
|
-
statusPayload.activityState === "needs_attention" ? "needs_attention" : "active_long_running";
|
|
329
|
-
appendControlEvent(
|
|
330
|
-
state,
|
|
331
|
-
buildControlEvent({
|
|
332
|
-
type: "active_long_running",
|
|
333
|
-
from: previous,
|
|
334
|
-
to: "active_long_running",
|
|
335
|
-
runId: id,
|
|
336
|
-
agent: step.agent,
|
|
337
|
-
index: flatIndex,
|
|
338
|
-
ts: now,
|
|
339
|
-
message: `${step.agent} is still active but long-running`,
|
|
340
|
-
reason,
|
|
341
|
-
turns: step.turnCount,
|
|
342
|
-
tokens: step.tokens?.total,
|
|
343
|
-
toolCount: step.toolCount,
|
|
344
|
-
currentTool: step.currentTool,
|
|
345
|
-
currentToolDurationMs: step.currentToolStartedAt ? Math.max(0, now - step.currentToolStartedAt) : undefined,
|
|
346
|
-
currentPath: step.currentPath,
|
|
347
|
-
elapsedMs: now - (step.startedAt ?? state.overallStartTime),
|
|
348
|
-
}),
|
|
349
|
-
);
|
|
350
|
-
return true;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
export function updateStepModel(
|
|
354
|
-
state: RunnerExecutionState,
|
|
355
|
-
flatIndex: number,
|
|
356
|
-
model: string | undefined,
|
|
357
|
-
thinking: string | undefined,
|
|
358
|
-
fastMode?: boolean,
|
|
359
|
-
now = Date.now(),
|
|
360
|
-
): void {
|
|
361
|
-
const step = state.statusPayload.steps[flatIndex];
|
|
362
|
-
if (!step) return;
|
|
363
|
-
step.model = model;
|
|
364
|
-
step.thinking = thinking;
|
|
365
|
-
step.fastMode = fastMode ? true : undefined;
|
|
366
|
-
state.statusPayload.lastUpdate = now;
|
|
367
|
-
writeStatusPayload(state);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
export function updateStepFromChildEvent(state: RunnerExecutionState, flatIndex: number, event: ChildEvent): void {
|
|
371
|
-
const step = state.statusPayload.steps[flatIndex];
|
|
372
|
-
if (!step) return;
|
|
373
|
-
const now = Date.now();
|
|
374
|
-
state.statusPayload.currentStep = flatIndex;
|
|
375
|
-
if (event.type === "tool_execution_start" && event.toolName) {
|
|
376
|
-
const mutates = isMutatingTool(event.toolName, event.args);
|
|
377
|
-
const currentPath = resolveCurrentPath(event.toolName, event.args);
|
|
378
|
-
step.toolCount = (step.toolCount ?? 0) + 1;
|
|
379
|
-
step.currentTool = event.toolName;
|
|
380
|
-
step.currentToolArgs = extractToolArgsPreview(event.args ?? {});
|
|
381
|
-
step.currentToolStartedAt = now;
|
|
382
|
-
step.currentPath = currentPath;
|
|
383
|
-
state.pendingToolResults[flatIndex] = { tool: event.toolName, path: currentPath, mutates, startedAt: now };
|
|
384
|
-
state.statusPayload.toolCount = (state.statusPayload.toolCount ?? 0) + 1;
|
|
385
|
-
syncTopLevelCurrentTool(state.statusPayload);
|
|
386
|
-
} else if (event.type === "tool_execution_end") {
|
|
387
|
-
if (step.currentTool) {
|
|
388
|
-
step.recentTools ??= [];
|
|
389
|
-
step.recentTools.push({ tool: step.currentTool, args: step.currentToolArgs || "", endMs: now });
|
|
390
|
-
}
|
|
391
|
-
step.currentTool = undefined;
|
|
392
|
-
step.currentToolArgs = undefined;
|
|
393
|
-
step.currentToolStartedAt = undefined;
|
|
394
|
-
step.currentPath = undefined;
|
|
395
|
-
syncTopLevelCurrentTool(state.statusPayload);
|
|
396
|
-
} else if (event.type === "tool_result_end" && event.message) {
|
|
397
|
-
const toolSnapshot = state.pendingToolResults[flatIndex];
|
|
398
|
-
state.pendingToolResults[flatIndex] = undefined;
|
|
399
|
-
const resultText = extractTextFromContent(event.message.content);
|
|
400
|
-
appendRecentStepOutput(step, resultText.split("\n").slice(-10));
|
|
401
|
-
if (toolSnapshot?.mutates && didMutatingToolFail(resultText)) {
|
|
402
|
-
const failureState = state.mutatingFailureStates[flatIndex]!;
|
|
403
|
-
recordMutatingFailure(
|
|
404
|
-
failureState,
|
|
405
|
-
{
|
|
406
|
-
tool: toolSnapshot.tool,
|
|
407
|
-
path: toolSnapshot.path,
|
|
408
|
-
error:
|
|
409
|
-
resultText
|
|
410
|
-
.split("\n")
|
|
411
|
-
.find((line) => line.trim())
|
|
412
|
-
?.trim()
|
|
413
|
-
.slice(0, 180) ?? "mutating tool failed",
|
|
414
|
-
ts: now,
|
|
415
|
-
},
|
|
416
|
-
mutatingFailureWindowMs,
|
|
417
|
-
);
|
|
418
|
-
if (
|
|
419
|
-
state.controlConfig.enabled &&
|
|
420
|
-
shouldEscalateMutatingFailures(failureState, state.controlConfig.failedToolAttemptsBeforeAttention) &&
|
|
421
|
-
step.activityState !== "needs_attention"
|
|
422
|
-
) {
|
|
423
|
-
const previous = step.activityState;
|
|
424
|
-
step.activityState = "needs_attention";
|
|
425
|
-
state.statusPayload.activityState = "needs_attention";
|
|
426
|
-
appendControlEvent(
|
|
427
|
-
state,
|
|
428
|
-
buildControlEvent({
|
|
429
|
-
type: "needs_attention",
|
|
430
|
-
from: previous,
|
|
431
|
-
to: "needs_attention",
|
|
432
|
-
runId: state.id,
|
|
433
|
-
agent: step.agent,
|
|
434
|
-
index: flatIndex,
|
|
435
|
-
ts: now,
|
|
436
|
-
message: `${step.agent} needs attention after repeated mutating tool failures`,
|
|
437
|
-
reason: "tool_failures",
|
|
438
|
-
turns: step.turnCount,
|
|
439
|
-
tokens: step.tokens?.total,
|
|
440
|
-
toolCount: step.toolCount,
|
|
441
|
-
currentTool: toolSnapshot.tool,
|
|
442
|
-
currentToolDurationMs: toolSnapshot.startedAt ? Math.max(0, now - toolSnapshot.startedAt) : undefined,
|
|
443
|
-
currentPath: toolSnapshot.path,
|
|
444
|
-
recentFailureSummary: summarizeRecentMutatingFailures(failureState),
|
|
445
|
-
}),
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
} else if (toolSnapshot?.mutates) {
|
|
449
|
-
resetMutatingFailureState(state.mutatingFailureStates[flatIndex]!);
|
|
450
|
-
}
|
|
451
|
-
} else if (event.type === "message_end" && event.message?.role === "assistant") {
|
|
452
|
-
appendRecentStepOutput(step, extractTextFromContent(event.message.content).split("\n").slice(-10));
|
|
453
|
-
step.turnCount = (step.turnCount ?? 0) + 1;
|
|
454
|
-
const usage = event.message.usage;
|
|
455
|
-
if (usage) {
|
|
456
|
-
const input = usage.input ?? usage.inputTokens ?? 0;
|
|
457
|
-
const output = usage.output ?? usage.outputTokens ?? 0;
|
|
458
|
-
const previousInput = step.tokens?.input ?? 0;
|
|
459
|
-
const previousOutput = step.tokens?.output ?? 0;
|
|
460
|
-
step.tokens = {
|
|
461
|
-
input: previousInput + input,
|
|
462
|
-
output: previousOutput + output,
|
|
463
|
-
total: previousInput + previousOutput + input + output,
|
|
464
|
-
};
|
|
465
|
-
const totalInput = state.statusPayload.totalTokens?.input ?? 0;
|
|
466
|
-
const totalOutput = state.statusPayload.totalTokens?.output ?? 0;
|
|
467
|
-
state.statusPayload.totalTokens = {
|
|
468
|
-
input: totalInput + input,
|
|
469
|
-
output: totalOutput + output,
|
|
470
|
-
total: totalInput + totalOutput + input + output,
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
state.statusPayload.turnCount = Math.max(state.statusPayload.turnCount ?? 0, step.turnCount);
|
|
474
|
-
}
|
|
475
|
-
syncTopLevelCurrentTool(state.statusPayload);
|
|
476
|
-
step.lastActivityAt = now;
|
|
477
|
-
state.statusPayload.lastActivityAt = now;
|
|
478
|
-
state.statusPayload.lastUpdate = now;
|
|
479
|
-
maybeEmitActiveLongRunning(state, flatIndex, now);
|
|
480
|
-
writeStatusPayload(state);
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
export function updateRunnerActivityState(state: RunnerExecutionState, now: number): boolean {
|
|
484
|
-
if (!state.controlConfig.enabled) return false;
|
|
485
|
-
let changed = false;
|
|
486
|
-
let runLastActivityAt = state.statusPayload.lastActivityAt ?? state.overallStartTime;
|
|
487
|
-
for (let index = 0; index < state.statusPayload.steps.length; index++) {
|
|
488
|
-
const step = state.statusPayload.steps[index]!;
|
|
489
|
-
if (step.status !== "running") continue;
|
|
490
|
-
const lastActivityAt = stepOutputActivityAt(state, index);
|
|
491
|
-
runLastActivityAt = Math.max(runLastActivityAt, lastActivityAt);
|
|
492
|
-
if (step.lastActivityAt !== lastActivityAt) {
|
|
493
|
-
step.lastActivityAt = lastActivityAt;
|
|
494
|
-
changed = true;
|
|
495
|
-
}
|
|
496
|
-
const idleState = deriveActivityState({
|
|
497
|
-
config: state.controlConfig,
|
|
498
|
-
startedAt: step.startedAt ?? state.overallStartTime,
|
|
499
|
-
lastActivityAt,
|
|
500
|
-
now,
|
|
501
|
-
});
|
|
502
|
-
if (idleState === "needs_attention") {
|
|
503
|
-
const previous = step.activityState;
|
|
504
|
-
step.activityState = "needs_attention";
|
|
505
|
-
if (previous !== "needs_attention") {
|
|
506
|
-
appendControlEvent(
|
|
507
|
-
state,
|
|
508
|
-
buildControlEvent({
|
|
509
|
-
from: previous,
|
|
510
|
-
to: "needs_attention",
|
|
511
|
-
runId: state.id,
|
|
512
|
-
agent: step.agent,
|
|
513
|
-
index,
|
|
514
|
-
ts: now,
|
|
515
|
-
lastActivityAt,
|
|
516
|
-
}),
|
|
517
|
-
);
|
|
518
|
-
changed = true;
|
|
519
|
-
}
|
|
520
|
-
} else if (maybeEmitActiveLongRunning(state, index, now)) changed = true;
|
|
521
|
-
}
|
|
522
|
-
if (state.statusPayload.lastActivityAt !== runLastActivityAt) {
|
|
523
|
-
state.statusPayload.lastActivityAt = runLastActivityAt;
|
|
524
|
-
changed = true;
|
|
525
|
-
}
|
|
526
|
-
const nextRunState = state.statusPayload.steps.some((step) => step.activityState === "needs_attention")
|
|
527
|
-
? "needs_attention"
|
|
528
|
-
: state.statusPayload.steps.some((step) => step.activityState === "active_long_running")
|
|
529
|
-
? "active_long_running"
|
|
530
|
-
: undefined;
|
|
531
|
-
if (nextRunState !== state.currentActivityState) {
|
|
532
|
-
state.currentActivityState = nextRunState;
|
|
533
|
-
state.statusPayload.activityState = nextRunState;
|
|
534
|
-
changed = true;
|
|
535
|
-
}
|
|
536
|
-
state.statusPayload.lastUpdate = now;
|
|
537
|
-
if (changed) writeStatusPayload(state);
|
|
538
|
-
return changed;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
export function startActivityTimer(state: RunnerExecutionState): void {
|
|
542
|
-
if (!state.controlConfig.enabled) return;
|
|
543
|
-
state.activityTimer = setInterval(() => {
|
|
544
|
-
if (state.statusPayload.state !== "running") return;
|
|
545
|
-
updateRunnerActivityState(state, Date.now());
|
|
546
|
-
}, 1000);
|
|
547
|
-
state.activityTimer.unref?.();
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
export function interruptRunner(state: RunnerExecutionState): void {
|
|
551
|
-
if (state.interrupted || state.statusPayload.state !== "running") return;
|
|
552
|
-
state.interrupted = true;
|
|
553
|
-
const now = Date.now();
|
|
554
|
-
state.statusPayload.state = "paused";
|
|
555
|
-
state.currentActivityState = undefined;
|
|
556
|
-
state.statusPayload.activityState = undefined;
|
|
557
|
-
state.statusPayload.lastUpdate = now;
|
|
558
|
-
for (const step of state.statusPayload.steps) {
|
|
559
|
-
if (step.status === "running") {
|
|
560
|
-
step.status = "paused";
|
|
561
|
-
step.activityState = undefined;
|
|
562
|
-
step.endedAt = now;
|
|
563
|
-
step.durationMs = step.startedAt ? now - step.startedAt : undefined;
|
|
564
|
-
step.lastActivityAt = now;
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
writeStatusPayload(state);
|
|
568
|
-
appendJsonl(state.eventsPath, JSON.stringify({ type: "subagent.run.paused", ts: now, runId: state.id }));
|
|
569
|
-
state.activeChildInterrupt?.();
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
export function cleanupActivityTimer(state: RunnerExecutionState): void {
|
|
573
|
-
if (!state.activityTimer) return;
|
|
574
|
-
clearInterval(state.activityTimer);
|
|
575
|
-
state.activityTimer = undefined;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
export { resetStepLiveDetail };
|