@andorra-labs/code 0.1.12 → 0.1.14
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/dist/src/account-presentation.d.ts +1 -37
- package/dist/src/account-presentation.js +6 -43
- package/dist/src/activity.d.ts +0 -25
- package/dist/src/activity.js +1 -23
- package/dist/src/agent-definitions.d.ts +0 -26
- package/dist/src/agent-definitions.js +0 -43
- package/dist/src/agent-execution.d.ts +0 -2
- package/dist/src/agent-execution.js +347 -554
- package/dist/src/agent-mcp-catalog.d.ts +0 -8
- package/dist/src/agent-mcp-catalog.js +0 -2
- package/dist/src/agent-mcp.d.ts +0 -56
- package/dist/src/agent-mcp.js +11 -155
- package/dist/src/agent-modifiers.d.ts +0 -1
- package/dist/src/agent-modifiers.js +25 -68
- package/dist/src/analytics.d.ts +0 -66
- package/dist/src/analytics.js +0 -45
- package/dist/src/andorra/endpoint.d.ts +7 -0
- package/dist/src/andorra/endpoint.js +46 -0
- package/dist/src/andorra/entry.d.ts +21 -0
- package/dist/src/andorra/entry.js +100 -0
- package/dist/src/andorra/errors.d.ts +2 -0
- package/dist/src/andorra/errors.js +39 -0
- package/dist/src/andorra/hosted.d.ts +52 -0
- package/dist/src/andorra/hosted.js +126 -0
- package/dist/src/andorra/monitor.d.ts +151 -0
- package/dist/src/andorra/monitor.js +393 -0
- package/dist/src/andorra/notices.d.ts +7 -0
- package/dist/src/andorra/notices.js +15 -0
- package/dist/src/andorra/opt-in.d.ts +58 -0
- package/dist/src/andorra/opt-in.js +112 -0
- package/dist/src/andorra/request-watch.d.ts +6 -0
- package/dist/src/andorra/request-watch.js +10 -0
- package/dist/src/andorra/state.d.ts +86 -0
- package/dist/src/andorra/state.js +238 -0
- package/dist/src/andorra/terminal.d.ts +68 -0
- package/dist/src/andorra/terminal.js +125 -0
- package/dist/src/andorra/usage-format.d.ts +9 -0
- package/dist/src/andorra/usage-format.js +19 -0
- package/dist/src/anthropic-console-auth.d.ts +0 -24
- package/dist/src/anthropic-console-auth.js +0 -37
- package/dist/src/anthropic-credentials.d.ts +0 -25
- package/dist/src/anthropic-credentials.js +9 -29
- package/dist/src/app-server/protocol.d.ts +18 -246
- package/dist/src/app-server/protocol.js +1 -132
- package/dist/src/app-server/reasoning.d.ts +0 -31
- package/dist/src/app-server/reasoning.js +0 -54
- package/dist/src/app-server/terminal.d.ts +0 -36
- package/dist/src/app-server/terminal.js +3 -39
- package/dist/src/app-server/workspace.d.ts +0 -167
- package/dist/src/app-server/workspace.js +4 -406
- package/dist/src/app-server.d.ts +52 -925
- package/dist/src/app-server.js +828 -3076
- package/dist/src/artifacts.d.ts +0 -10
- package/dist/src/artifacts.js +1 -11
- package/dist/src/attachments.d.ts +4 -98
- package/dist/src/attachments.js +10 -130
- package/dist/src/auth/AuthManager.d.ts +9 -93
- package/dist/src/auth/AuthManager.js +164 -186
- package/dist/src/auth/callback-page.d.ts +0 -1
- package/dist/src/auth/callback-page.js +0 -4
- package/dist/src/auth/callback.d.ts +0 -25
- package/dist/src/auth/callback.js +1 -29
- package/dist/src/auth/deployment.d.ts +1 -16
- package/dist/src/auth/deployment.js +1 -16
- package/dist/src/auth/index.d.ts +1 -12
- package/dist/src/auth/index.js +1 -12
- package/dist/src/auth/lock.d.ts +0 -1
- package/dist/src/auth/lock.js +0 -1
- package/dist/src/auth/store.d.ts +33 -108
- package/dist/src/auth/store.js +55 -130
- package/dist/src/auth/types.d.ts +0 -8
- package/dist/src/auth-commands.d.ts +0 -5
- package/dist/src/auth-commands.js +0 -1
- package/dist/src/automation-runner.d.ts +0 -9
- package/dist/src/automation-runner.js +0 -11
- package/dist/src/automation-service.d.ts +0 -10
- package/dist/src/automation-service.js +3 -17
- package/dist/src/automation-tool.js +0 -5
- package/dist/src/automation-worker-shutdown.d.ts +0 -1
- package/dist/src/automation-worker-shutdown.js +0 -3
- package/dist/src/automations.d.ts +23 -2
- package/dist/src/automations.js +91 -12
- package/dist/src/background-tasks.d.ts +0 -23
- package/dist/src/background-tasks.js +0 -11
- package/dist/src/boot-trace.d.ts +0 -13
- package/dist/src/boot-trace.js +1 -14
- package/dist/src/browser-activity.js +0 -2
- package/dist/src/browser-bridge.d.ts +0 -2
- package/dist/src/browser-bridge.js +0 -27
- package/dist/src/browser-captcha-tool.d.ts +0 -13
- package/dist/src/browser-captcha-tool.js +3 -35
- package/dist/src/browser-extensions-tool.d.ts +24 -0
- package/dist/src/browser-extensions-tool.js +137 -0
- package/dist/src/browser-extensions.d.ts +127 -0
- package/dist/src/browser-extensions.js +729 -0
- package/dist/src/browser-observation.d.ts +0 -15
- package/dist/src/browser-observation.js +1 -22
- package/dist/src/browser-panel-tool.d.ts +1 -37
- package/dist/src/browser-panel-tool.js +3 -73
- package/dist/src/browser-runner.d.ts +0 -1
- package/dist/src/browser-runner.js +5 -46
- package/dist/src/browser-server.d.ts +0 -6
- package/dist/src/browser-server.js +6 -229
- package/dist/src/browser-session.d.ts +0 -25
- package/dist/src/browser-session.js +1 -71
- package/dist/src/browser-stealth.d.ts +19 -13
- package/dist/src/browser-stealth.js +22 -38
- package/dist/src/browser-ui/app.js +7 -174
- package/dist/src/browser-ui/style.css +0 -68
- package/dist/src/browser-ui/tokens.css +15 -97
- package/dist/src/browser-view.d.ts +106 -295
- package/dist/src/browser-view.js +551 -587
- package/dist/src/capabilities.d.ts +0 -8
- package/dist/src/capabilities.js +0 -29
- package/dist/src/capsolver.d.ts +0 -13
- package/dist/src/capsolver.js +0 -15
- package/dist/src/clarification-ui.d.ts +0 -7
- package/dist/src/clarification-ui.js +0 -8
- package/dist/src/claude-executable.d.ts +0 -1
- package/dist/src/claude-executable.js +1 -2
- package/dist/src/claude-provider.d.ts +6 -19
- package/dist/src/claude-provider.js +155 -63
- package/dist/src/cli-executable.d.ts +0 -16
- package/dist/src/cli-executable.js +0 -7
- package/dist/src/cli-process.d.ts +0 -26
- package/dist/src/cli-process.js +0 -18
- package/dist/src/cli-sign-in.d.ts +0 -20
- package/dist/src/cli-sign-in.js +1 -17
- package/dist/src/cli.d.ts +1 -55
- package/dist/src/cli.js +118 -742
- package/dist/src/code-intelligence/CodeIntelligenceManager.d.ts +4 -59
- package/dist/src/code-intelligence/CodeIntelligenceManager.js +26 -99
- package/dist/src/code-intelligence/ContextLedger.d.ts +0 -4
- package/dist/src/code-intelligence/ContextLedger.js +0 -10
- package/dist/src/code-intelligence/ContextRouter.d.ts +0 -6
- package/dist/src/code-intelligence/ContextRouter.js +0 -8
- package/dist/src/code-intelligence/codegraph/CodeGraphProjectSession.d.ts +0 -2
- package/dist/src/code-intelligence/codegraph/CodeGraphProjectSession.js +5 -12
- package/dist/src/code-intelligence/codegraph/CodeGraphProvider.d.ts +0 -6
- package/dist/src/code-intelligence/codegraph/CodeGraphProvider.js +0 -6
- package/dist/src/code-intelligence/codegraph/CodeGraphSyncBarrier.d.ts +0 -5
- package/dist/src/code-intelligence/codegraph/CodeGraphSyncBarrier.js +1 -17
- package/dist/src/code-intelligence/codegraph/repoExploreTool.d.ts +0 -5
- package/dist/src/code-intelligence/codegraph/repoExploreTool.js +0 -5
- package/dist/src/code-intelligence/repository.d.ts +0 -9
- package/dist/src/code-intelligence/repository.js +1 -11
- package/dist/src/code-intelligence/types.d.ts +0 -8
- package/dist/src/codex-auth.d.ts +0 -44
- package/dist/src/codex-auth.js +0 -45
- package/dist/src/codex-browser-auth.d.ts +0 -13
- package/dist/src/codex-browser-auth.js +3 -20
- package/dist/src/codex-errors.js +0 -2
- package/dist/src/codex-executable.d.ts +0 -2
- package/dist/src/codex-executable.js +1 -7
- package/dist/src/codex-provider.d.ts +0 -13
- package/dist/src/codex-provider.js +0 -28
- package/dist/src/codex-rpc.d.ts +0 -10
- package/dist/src/codex-rpc.js +0 -12
- package/dist/src/coding-tools.d.ts +4 -26
- package/dist/src/coding-tools.js +161 -181
- package/dist/src/command-metrics.d.ts +0 -4
- package/dist/src/command-output-tap.d.ts +0 -11
- package/dist/src/command-output-tap.js +0 -3
- package/dist/src/command-screens.d.ts +41 -1
- package/dist/src/command-screens.js +316 -113
- package/dist/src/commands.d.ts +12 -63
- package/dist/src/commands.js +98 -127
- package/dist/src/completion-gates.d.ts +0 -47
- package/dist/src/completion-gates.js +0 -14
- package/dist/src/completion.d.ts +0 -98
- package/dist/src/completion.js +2 -239
- package/dist/src/config.d.ts +15 -115
- package/dist/src/config.js +123 -172
- package/dist/src/context-budget.d.ts +0 -22
- package/dist/src/context-budget.js +0 -44
- package/dist/src/context-compaction.d.ts +29 -6
- package/dist/src/context-compaction.js +94 -25
- package/dist/src/context-selection.d.ts +0 -24
- package/dist/src/context-selection.js +0 -34
- package/dist/src/dashboard-view.d.ts +2 -2
- package/dist/src/dashboard-view.js +11 -37
- package/dist/src/dashboard.d.ts +0 -2
- package/dist/src/dashboard.js +0 -9
- package/dist/src/deadline.d.ts +0 -45
- package/dist/src/deadline.js +0 -54
- package/dist/src/decision/ContinuationAdapter.d.ts +0 -5
- package/dist/src/decision/ContinuationAdapter.js +0 -5
- package/dist/src/decision/DecisionEngine.d.ts +0 -4
- package/dist/src/decision/DecisionManager.d.ts +0 -1
- package/dist/src/decision/DecisionManager.js +2 -7
- package/dist/src/decision/DecisionPolicy.d.ts +0 -1
- package/dist/src/decision/DecisionPolicy.js +0 -1
- package/dist/src/decision/FactContinuations.d.ts +11 -43
- package/dist/src/decision/FactContinuations.js +126 -95
- package/dist/src/decision/FactServiceGate.d.ts +0 -1
- package/dist/src/decision/FactServiceGate.js +0 -4
- package/dist/src/decision/PreparedSteps.d.ts +0 -15
- package/dist/src/decision/PreparedSteps.js +1 -11
- package/dist/src/decision/applyToolMenu.d.ts +0 -1
- package/dist/src/decision/applyToolMenu.js +2 -6
- package/dist/src/decision/checks.d.ts +15 -28
- package/dist/src/decision/checks.js +118 -49
- package/dist/src/decision/facts.d.ts +0 -16
- package/dist/src/decision/facts.js +0 -30
- package/dist/src/decision/guidancePolicy.js +0 -1
- package/dist/src/decision/hostedPolicy.d.ts +0 -6
- package/dist/src/decision/hostedPolicy.js +0 -7
- package/dist/src/decision/hostedSkills.d.ts +0 -19
- package/dist/src/decision/hostedSkills.js +0 -19
- package/dist/src/decision/optimizationPolicy.d.ts +0 -3
- package/dist/src/decision/optimizationPolicy.js +1 -4
- package/dist/src/decision/providers/AndorraDecisionProvider.d.ts +0 -7
- package/dist/src/decision/providers/AndorraDecisionProvider.js +4 -27
- package/dist/src/decision/providers/HttpDecisionProvider.d.ts +0 -2
- package/dist/src/decision/providers/HttpDecisionProvider.js +0 -6
- package/dist/src/decision/routeTools.d.ts +0 -8
- package/dist/src/decision/routeTools.js +1 -8
- package/dist/src/decision/toolFamilies.d.ts +0 -1
- package/dist/src/decision/toolFamilies.js +1 -2
- package/dist/src/decision/toolMenu.d.ts +0 -3
- package/dist/src/decision/toolMenu.js +0 -4
- package/dist/src/decision/types.d.ts +1 -6
- package/dist/src/default-graph.d.ts +0 -9
- package/dist/src/default-graph.js +0 -9
- package/dist/src/delegation-changes.d.ts +0 -56
- package/dist/src/delegation-changes.js +4 -130
- package/dist/src/diff-preview.d.ts +0 -1
- package/dist/src/diff-preview.js +0 -17
- package/dist/src/duration.d.ts +0 -50
- package/dist/src/duration.js +0 -50
- package/dist/src/efficiency.d.ts +0 -39
- package/dist/src/efficiency.js +0 -16
- package/dist/src/engine/protocol.d.ts +0 -2
- package/dist/src/engine/protocol.js +0 -2
- package/dist/src/engine-streams.d.ts +0 -9
- package/dist/src/extensions.d.ts +3 -2
- package/dist/src/extensions.js +63 -35
- package/dist/src/favicons.d.ts +0 -26
- package/dist/src/favicons.js +0 -34
- package/dist/src/file-diff.d.ts +2 -58
- package/dist/src/file-diff.js +0 -32
- package/dist/src/file-read-key.d.ts +0 -7
- package/dist/src/file-read-key.js +0 -7
- package/dist/src/file-read-state.d.ts +0 -1
- package/dist/src/file-read-state.js +0 -4
- package/dist/src/firefox-cookies.d.ts +0 -34
- package/dist/src/firefox-cookies.js +1 -55
- package/dist/src/git-identity-audit.d.ts +27 -0
- package/dist/src/git-identity-audit.js +286 -0
- package/dist/src/git-identity-guard.d.ts +4 -0
- package/dist/src/git-identity-guard.js +998 -0
- package/dist/src/git-identity-hold.d.ts +12 -0
- package/dist/src/git-identity-hold.js +81 -0
- package/dist/src/git-summary.d.ts +0 -58
- package/dist/src/git-summary.js +2 -212
- package/dist/src/goal-completion-verification.d.ts +2 -2
- package/dist/src/goal-completion-verification.js +3 -6
- package/dist/src/graph-account.d.ts +0 -21
- package/dist/src/graph-account.js +0 -17
- package/dist/src/graph-editor-session.d.ts +0 -1
- package/dist/src/graph-editor-session.js +0 -3
- package/dist/src/graph-health.d.ts +9 -61
- package/dist/src/graph-health.js +34 -59
- package/dist/src/graph-manager-chat.d.ts +36 -153
- package/dist/src/graph-manager-chat.js +161 -182
- package/dist/src/graph-provider-portability.d.ts +1 -21
- package/dist/src/graph-provider-portability.js +6 -45
- package/dist/src/graph-quota.d.ts +0 -33
- package/dist/src/graph-quota.js +1 -44
- package/dist/src/graph-runner.d.ts +0 -1
- package/dist/src/graph-runner.js +58 -391
- package/dist/src/graph-selection.d.ts +0 -14
- package/dist/src/graph-selection.js +2 -15
- package/dist/src/graph-server.d.ts +2 -10
- package/dist/src/graph-server.js +39 -101
- package/dist/src/graph-studio-features.d.ts +0 -5
- package/dist/src/graph-studio-features.js +2 -42
- package/dist/src/graph-sync.d.ts +0 -78
- package/dist/src/graph-sync.js +5 -201
- package/dist/src/graph-tool.d.ts +0 -18
- package/dist/src/graph-tool.js +0 -27
- package/dist/src/graph-ui/app.js +176 -492
- package/dist/src/graph-ui/compat.js +0 -7
- package/dist/src/graph-ui/controls.css +0 -48
- package/dist/src/graph-ui/controls.js +0 -88
- package/dist/src/graph-ui/drafts.js +0 -12
- package/dist/src/graph-ui/edge-router.js +0 -62
- package/dist/src/graph-ui/embed.js +0 -59
- package/dist/src/graph-ui/icons.js +0 -8
- package/dist/src/graph-ui/library.css +0 -5
- package/dist/src/graph-ui/manager-markdown.js +0 -11
- package/dist/src/graph-ui/manager-state.js +1 -78
- package/dist/src/graph-ui/manager.css +0 -18
- package/dist/src/graph-ui/manager.js +43 -185
- package/dist/src/graph-ui/model-picker.js +0 -29
- package/dist/src/graph-ui/sidebar.js +2 -3
- package/dist/src/graph-ui/skill-picker.js +373 -39
- package/dist/src/graph-ui/studio-canvas.js +3 -38
- package/dist/src/graph-ui/studio.css +0 -18
- package/dist/src/graph-ui/studio.js +3 -71
- package/dist/src/graph-ui/style.css +16 -136
- package/dist/src/graph-ui/theme.js +1 -26
- package/dist/src/graph-ui/tokens.css +19 -118
- package/dist/src/graphs.d.ts +6 -157
- package/dist/src/graphs.js +66 -269
- package/dist/src/grep-regex-worker.js +0 -2
- package/dist/src/grep-regex.d.ts +1 -5
- package/dist/src/grep-regex.js +6 -8
- package/dist/src/grok-provider.d.ts +0 -17
- package/dist/src/grok-provider.js +1 -26
- package/dist/src/handoffs.d.ts +0 -11
- package/dist/src/handoffs.js +0 -13
- package/dist/src/hardened-runtime.d.ts +0 -10
- package/dist/src/hardened-runtime.js +9 -47
- package/dist/src/harness-home.d.ts +21 -0
- package/dist/src/harness-home.js +87 -0
- package/dist/src/host-process-guard.d.ts +0 -50
- package/dist/src/host-process-guard.js +0 -641
- package/dist/src/limits.d.ts +0 -64
- package/dist/src/limits.js +0 -62
- package/dist/src/managed-sign-in.d.ts +0 -12
- package/dist/src/managed-sign-in.js +0 -16
- package/dist/src/markdown.d.ts +0 -28
- package/dist/src/markdown.js +1 -175
- package/dist/src/mcp.d.ts +0 -99
- package/dist/src/mcp.js +4 -170
- package/dist/src/membership-errors.d.ts +0 -14
- package/dist/src/membership-errors.js +1 -14
- package/dist/src/membership.d.ts +9 -59
- package/dist/src/membership.js +20 -91
- package/dist/src/memory/LocalToolMemory.d.ts +0 -1
- package/dist/src/memory/LocalToolMemory.js +2 -7
- package/dist/src/memory/MemoryDistiller.js +0 -15
- package/dist/src/memory/MemoryManager.d.ts +0 -3
- package/dist/src/memory/MemoryManager.js +6 -18
- package/dist/src/memory/MemoryPolicy.d.ts +0 -2
- package/dist/src/memory/MemoryPolicy.js +0 -2
- package/dist/src/memory/MemoryProvider.d.ts +0 -6
- package/dist/src/memory/MemoryRetriever.js +0 -7
- package/dist/src/memory/PermissionMemoryProvider.d.ts +0 -1
- package/dist/src/memory/PermissionMemoryProvider.js +0 -1
- package/dist/src/memory/ProjectMemory.d.ts +5 -40
- package/dist/src/memory/ProjectMemory.js +22 -47
- package/dist/src/memory/ToolFailureLessons.d.ts +0 -3
- package/dist/src/memory/ToolFailureLessons.js +0 -6
- package/dist/src/memory/providers/NoOpMemoryProvider.d.ts +0 -1
- package/dist/src/memory/providers/NoOpMemoryProvider.js +0 -1
- package/dist/src/memory/redaction.d.ts +0 -5
- package/dist/src/memory/redaction.js +0 -11
- package/dist/src/memory/repository.d.ts +0 -6
- package/dist/src/memory/repository.js +2 -12
- package/dist/src/memory/types.d.ts +0 -1
- package/dist/src/model-context-windows.d.ts +2 -0
- package/dist/src/model-context-windows.js +24 -0
- package/dist/src/model-health-cache.d.ts +0 -38
- package/dist/src/model-health-cache.js +2 -50
- package/dist/src/model-metadata.d.ts +1 -18
- package/dist/src/model-metadata.js +42 -45
- package/dist/src/model-override.d.ts +0 -36
- package/dist/src/model-override.js +0 -43
- package/dist/src/model-tiers.d.ts +0 -13
- package/dist/src/model-tiers.js +0 -23
- package/dist/src/opencode-catalog.d.ts +0 -2
- package/dist/src/opencode-catalog.js +0 -11
- package/dist/src/opencode-cli-provider.d.ts +0 -35
- package/dist/src/opencode-cli-provider.js +1 -44
- package/dist/src/opencode-provider.d.ts +0 -2
- package/dist/src/opencode-provider.js +1 -16
- package/dist/src/package-name.d.ts +0 -2
- package/dist/src/package-name.js +0 -2
- package/dist/src/paged-transcript.d.ts +0 -11
- package/dist/src/paged-transcript.js +2 -20
- package/dist/src/palette.d.ts +1 -63
- package/dist/src/palette.js +1 -64
- package/dist/src/permissions.d.ts +0 -15
- package/dist/src/permissions.js +17 -21
- package/dist/src/powershell-pool.d.ts +0 -9
- package/dist/src/powershell-pool.js +3 -26
- package/dist/src/process-warnings.d.ts +4 -0
- package/dist/src/process-warnings.js +103 -0
- package/dist/src/profile.d.ts +0 -43
- package/dist/src/profile.js +0 -43
- package/dist/src/project-reservation.d.ts +11 -0
- package/dist/src/project-reservation.js +143 -0
- package/dist/src/provider-connection.d.ts +0 -18
- package/dist/src/provider-connection.js +15 -39
- package/dist/src/provider-engine.d.ts +0 -21
- package/dist/src/provider-engine.js +1 -96
- package/dist/src/provider-import.d.ts +0 -49
- package/dist/src/provider-import.js +0 -60
- package/dist/src/provider-keys.d.ts +1 -36
- package/dist/src/provider-keys.js +5 -48
- package/dist/src/provider-settings.d.ts +16 -3
- package/dist/src/provider-settings.js +36 -52
- package/dist/src/providers.d.ts +14 -219
- package/dist/src/providers.js +69 -326
- package/dist/src/pull-requests.d.ts +0 -173
- package/dist/src/pull-requests.js +5 -195
- package/dist/src/qa/browser/PlaywrightRunner.d.ts +0 -7
- package/dist/src/qa/browser/PlaywrightRunner.js +2 -16
- package/dist/src/qa/command-policy.d.ts +0 -23
- package/dist/src/qa/command-policy.js +0 -30
- package/dist/src/qa/guidance.d.ts +0 -21
- package/dist/src/qa/guidance.js +0 -20
- package/dist/src/qa/profile/ProjectInspector.d.ts +0 -4
- package/dist/src/qa/profile/ProjectInspector.js +0 -6
- package/dist/src/qa/profile/QaConfigLoader.d.ts +0 -7
- package/dist/src/qa/profile/QaConfigLoader.js +0 -6
- package/dist/src/qa/profile/QaProfile.d.ts +0 -7
- package/dist/src/qa/profile/QaProfile.js +0 -7
- package/dist/src/qa/profile/yaml.d.ts +0 -9
- package/dist/src/qa/profile/yaml.js +0 -13
- package/dist/src/qa/qa-authorize.d.ts +0 -19
- package/dist/src/qa/qa-authorize.js +4 -11
- package/dist/src/qa/runtime/ProcessManager.d.ts +0 -31
- package/dist/src/qa/runtime/ProcessManager.js +4 -41
- package/dist/src/qa/runtime/PtyManager.d.ts +0 -4
- package/dist/src/qa/runtime/PtyManager.js +1 -7
- package/dist/src/qa/runtime/changes.d.ts +0 -11
- package/dist/src/qa/runtime/changes.js +1 -3
- package/dist/src/qa/tools.d.ts +0 -10
- package/dist/src/qa/tools.js +0 -12
- package/dist/src/qa/types.d.ts +0 -13
- package/dist/src/react-runtime.d.ts +2 -21
- package/dist/src/react-runtime.js +107 -422
- package/dist/src/read-document-doc.d.ts +0 -6
- package/dist/src/read-document-doc.js +0 -55
- package/dist/src/read-document-docx.d.ts +0 -16
- package/dist/src/read-document-docx.js +2 -69
- package/dist/src/read-document-text.d.ts +0 -17
- package/dist/src/read-document-text.js +0 -22
- package/dist/src/read-document-tool.d.ts +0 -31
- package/dist/src/read-document-tool.js +0 -30
- package/dist/src/read-document.d.ts +0 -49
- package/dist/src/read-document.js +1 -67
- package/dist/src/read-image.d.ts +0 -32
- package/dist/src/read-image.js +0 -33
- package/dist/src/remote/cert.d.ts +0 -18
- package/dist/src/remote/cert.js +0 -44
- package/dist/src/remote/client.d.ts +0 -70
- package/dist/src/remote/client.js +1 -116
- package/dist/src/remote/command.js +0 -17
- package/dist/src/remote/engine-pool.d.ts +0 -152
- package/dist/src/remote/engine-pool.js +22 -339
- package/dist/src/remote/errors.d.ts +0 -15
- package/dist/src/remote/errors.js +0 -7
- package/dist/src/remote/network.d.ts +0 -26
- package/dist/src/remote/network.js +1 -34
- package/dist/src/remote/pairing.d.ts +16 -51
- package/dist/src/remote/pairing.js +50 -57
- package/dist/src/remote/pem.d.ts +0 -4
- package/dist/src/remote/pem.js +0 -4
- package/dist/src/remote/roots.d.ts +0 -20
- package/dist/src/remote/roots.js +0 -8
- package/dist/src/remote/serve.d.ts +0 -47
- package/dist/src/remote/serve.js +0 -30
- package/dist/src/remote/server.d.ts +0 -14
- package/dist/src/remote/server.js +1 -79
- package/dist/src/remote/types.d.ts +1 -32
- package/dist/src/remote/types.js +0 -10
- package/dist/src/run-control.d.ts +2 -9
- package/dist/src/run-control.js +9 -13
- package/dist/src/run-progress.d.ts +0 -170
- package/dist/src/run-progress.js +0 -285
- package/dist/src/run-records.d.ts +0 -443
- package/dist/src/run-records.js +14 -687
- package/dist/src/run-temp.d.ts +12 -0
- package/dist/src/run-temp.js +50 -0
- package/dist/src/runtime.d.ts +12 -228
- package/dist/src/runtime.js +2 -128
- package/dist/src/sandbox-child.js +0 -1
- package/dist/src/save-lock.d.ts +0 -12
- package/dist/src/save-lock.js +0 -28
- package/dist/src/screen-selection.d.ts +0 -23
- package/dist/src/screen-selection.js +0 -22
- package/dist/src/secret-redaction.d.ts +0 -12
- package/dist/src/secret-redaction.js +0 -45
- package/dist/src/session-changes.d.ts +4 -80
- package/dist/src/session-changes.js +25 -130
- package/dist/src/session-goal.d.ts +129 -0
- package/dist/src/session-goal.js +230 -0
- package/dist/src/session-monitor.d.ts +1 -10
- package/dist/src/session-monitor.js +2 -39
- package/dist/src/session-reaper.d.ts +0 -15
- package/dist/src/session-reaper.js +0 -14
- package/dist/src/session-skills.d.ts +15 -44
- package/dist/src/session-skills.js +75 -60
- package/dist/src/session-summary.d.ts +0 -26
- package/dist/src/session-summary.js +0 -32
- package/dist/src/session-title.d.ts +0 -1
- package/dist/src/session-title.js +0 -2
- package/dist/src/sessions.d.ts +3 -122
- package/dist/src/sessions.js +9 -178
- package/dist/src/shell.d.ts +3 -20
- package/dist/src/shell.js +8 -72
- package/dist/src/side-panel.d.ts +5 -99
- package/dist/src/side-panel.js +76 -133
- package/dist/src/sign-in-detection.d.ts +0 -39
- package/dist/src/sign-in-detection.js +0 -43
- package/dist/src/sign-in-offer.d.ts +1 -19
- package/dist/src/sign-in-offer.js +0 -13
- package/dist/src/skill-commands.d.ts +0 -1
- package/dist/src/skill-commands.js +1 -4
- package/dist/src/skills/bundled/control-browser/SKILL.md +1 -1
- package/dist/src/skills/bundled/graph-manager/errors.md +1 -1
- package/dist/src/skills/bundled/provenance.json +738 -738
- package/dist/src/skills/invocable-skills.d.ts +0 -18
- package/dist/src/skills/invocable-skills.js +0 -17
- package/dist/src/skills/project-skills.d.ts +0 -1
- package/dist/src/skills/project-skills.js +0 -3
- package/dist/src/skills/prompt-skills.js +0 -14
- package/dist/src/skills/runtime-skills.d.ts +0 -13
- package/dist/src/skills/runtime-skills.js +0 -37
- package/dist/src/stall-recovery.d.ts +0 -25
- package/dist/src/stall-recovery.js +0 -38
- package/dist/src/stray-files.d.ts +0 -9
- package/dist/src/stray-files.js +0 -16
- package/dist/src/structured-step.d.ts +0 -22
- package/dist/src/structured-step.js +0 -22
- package/dist/src/subagent-run.d.ts +2 -163
- package/dist/src/subagent-run.js +18 -376
- package/dist/src/subagent-tree.d.ts +32 -0
- package/dist/src/subagent-tree.js +128 -0
- package/dist/src/telemetry/aggregate.d.ts +0 -37
- package/dist/src/telemetry/aggregate.js +0 -40
- package/dist/src/telemetry/capture-core.d.ts +0 -87
- package/dist/src/telemetry/capture-core.js +0 -67
- package/dist/src/telemetry/capture.d.ts +0 -1
- package/dist/src/telemetry/capture.js +0 -3
- package/dist/src/telemetry/classify.d.ts +0 -58
- package/dist/src/telemetry/classify.js +0 -60
- package/dist/src/telemetry/decision-capture.d.ts +0 -1
- package/dist/src/telemetry/emitter.d.ts +0 -30
- package/dist/src/telemetry/emitter.js +1 -62
- package/dist/src/telemetry/gate.d.ts +0 -29
- package/dist/src/telemetry/gate.js +0 -22
- package/dist/src/telemetry/identity.d.ts +0 -13
- package/dist/src/telemetry/identity.js +0 -29
- package/dist/src/telemetry/outbox.d.ts +0 -64
- package/dist/src/telemetry/outbox.js +1 -108
- package/dist/src/telemetry/schema.d.ts +1 -49
- package/dist/src/telemetry/schema.js +3 -84
- package/dist/src/telemetry/state.d.ts +0 -53
- package/dist/src/telemetry/state.js +0 -64
- package/dist/src/telemetry/surface.d.ts +0 -31
- package/dist/src/telemetry/surface.js +1 -39
- package/dist/src/telemetry/uploader.d.ts +0 -11
- package/dist/src/telemetry/uploader.js +0 -46
- package/dist/src/telemetry/uploads.d.ts +0 -11
- package/dist/src/telemetry/uploads.js +0 -11
- package/dist/src/telemetry/version.d.ts +0 -4
- package/dist/src/telemetry/version.js +0 -14
- package/dist/src/telemetry/workspace.d.ts +0 -7
- package/dist/src/telemetry/workspace.js +1 -22
- package/dist/src/terminal-ui.d.ts +33 -521
- package/dist/src/terminal-ui.js +145 -884
- package/dist/src/test-edit-guard.d.ts +74 -0
- package/dist/src/test-edit-guard.js +269 -0
- package/dist/src/text.d.ts +0 -1
- package/dist/src/text.js +0 -1
- package/dist/src/time-limit.d.ts +0 -36
- package/dist/src/time-limit.js +0 -37
- package/dist/src/timers.d.ts +0 -161
- package/dist/src/timers.js +0 -126
- package/dist/src/todos.d.ts +0 -1
- package/dist/src/todos.js +0 -3
- package/dist/src/token-rate.d.ts +0 -94
- package/dist/src/token-rate.js +0 -154
- package/dist/src/tool-activity.d.ts +0 -6
- package/dist/src/tool-activity.js +0 -7
- package/dist/src/tool-availability.d.ts +0 -13
- package/dist/src/tool-availability.js +1 -14
- package/dist/src/tool-call-clock.d.ts +1 -4
- package/dist/src/tool-call-clock.js +0 -13
- package/dist/src/tool-catalog.d.ts +0 -11
- package/dist/src/tool-catalog.js +0 -23
- package/dist/src/tool-dependencies.d.ts +0 -5
- package/dist/src/tool-dependencies.js +0 -4
- package/dist/src/tool-diff-stats.d.ts +0 -23
- package/dist/src/tool-diff-stats.js +0 -49
- package/dist/src/tool-errors.d.ts +9 -7
- package/dist/src/tool-errors.js +13 -6
- package/dist/src/tool-failure-tracker.d.ts +3 -11
- package/dist/src/tool-failure-tracker.js +19 -27
- package/dist/src/tool-names.d.ts +0 -16
- package/dist/src/tool-names.js +0 -12
- package/dist/src/tool-profile.d.ts +6 -0
- package/dist/src/tool-profile.js +23 -0
- package/dist/src/tool-result-filter.d.ts +0 -19
- package/dist/src/tool-result-filter.js +1 -66
- package/dist/src/tool-schema-compatibility.d.ts +0 -6
- package/dist/src/tool-schema-compatibility.js +0 -9
- package/dist/src/tools.d.ts +0 -4
- package/dist/src/tools.js +11 -114
- package/dist/src/trajectory.d.ts +0 -13
- package/dist/src/trajectory.js +23 -79
- package/dist/src/transcript.d.ts +0 -41
- package/dist/src/transcript.js +5 -60
- package/dist/src/types.d.ts +4 -168
- package/dist/src/untracked-file-removal.js +0 -20
- package/dist/src/update-check.d.ts +0 -14
- package/dist/src/update-check.js +1 -15
- package/dist/src/update.d.ts +0 -35
- package/dist/src/update.js +0 -100
- package/dist/src/usage-analytics.d.ts +17 -17
- package/dist/src/usage-analytics.js +168 -209
- package/dist/src/web-search.d.ts +0 -23
- package/dist/src/web-search.js +1 -27
- package/dist/src/widgets.d.ts +0 -25
- package/dist/src/widgets.js +0 -24
- package/dist/src/workspace-facts.d.ts +0 -12
- package/dist/src/workspace-facts.js +1 -18
- package/dist/src/workspace-git-metadata.d.ts +0 -16
- package/dist/src/workspace-git-metadata.js +0 -21
- package/dist/src/workspace-instructions.d.ts +3 -15
- package/dist/src/workspace-instructions.js +6 -19
- package/package.json +13 -2
package/dist/src/run-records.js
CHANGED
|
@@ -6,25 +6,11 @@ import { configDirectory } from './config.js';
|
|
|
6
6
|
import { writeSessionSnapshot } from './sessions.js';
|
|
7
7
|
import { ToolFailureTracker } from './tool-failure-tracker.js';
|
|
8
8
|
import { stopReasonPhrase } from './runtime.js';
|
|
9
|
-
/**
|
|
10
|
-
* The directory holding one JSON Lines event log and one JSON result per run.
|
|
11
|
-
*
|
|
12
|
-
* `runs/` sits inside the installation directory, beside `config.json` and `artifacts/`, so the
|
|
13
|
-
* identity that names a record is also the last path segment of a file this process writes.
|
|
14
|
-
*/
|
|
15
9
|
export function runRecordsDirectory(directory = configDirectory()) {
|
|
16
10
|
return join(directory, 'runs');
|
|
17
11
|
}
|
|
18
|
-
/** The history entry a session carries while its attempt is in flight, replaced when the attempt lands. */
|
|
19
12
|
export const RUNNING_TURN_SUMMARY = 'This attempt was interrupted or is still running. Verify the workspace before continuing.';
|
|
20
|
-
/** The same entry after recovery classifies the attempt as one the engine never finished. */
|
|
21
13
|
export const INTERRUPTED_TURN_SUMMARY = 'This attempt stopped unexpectedly before it finished; its work may be incomplete.';
|
|
22
|
-
/**
|
|
23
|
-
* What a session marker should say when the run record finalized but the session never heard how
|
|
24
|
-
* the attempt ended. Stopped outcomes commonly carry no completion object at all — a cancel has
|
|
25
|
-
* nothing to summarize — and then the recorded status is the only account left: a sentence about
|
|
26
|
-
* the engine stopping unexpectedly would contradict a record that says the user cancelled.
|
|
27
|
-
*/
|
|
28
14
|
export function summaryForStoppedRecord(record) {
|
|
29
15
|
const summary = record.completion?.summary;
|
|
30
16
|
if (typeof summary === 'string')
|
|
@@ -34,10 +20,6 @@ export function summaryForStoppedRecord(record) {
|
|
|
34
20
|
if (record.status === 'interrupted')
|
|
35
21
|
return INTERRUPTED_TURN_SUMMARY;
|
|
36
22
|
const reason = typeof record.reason === 'string' && record.reason.length > 0 ? record.reason : undefined;
|
|
37
|
-
// A record's reason is read after the colon of a sentence about the attempt, so it goes through
|
|
38
|
-
// the same classification every other surface uses: an internal code such as
|
|
39
|
-
// `maxNodeVisits:qa` becomes the sentence a person can act on, and a failure message is used
|
|
40
|
-
// whole. Reading the code raw would leak the identifier this repository never shows a user.
|
|
41
23
|
return reason === undefined ? 'This attempt did not finish; its work may be incomplete.' : `This attempt did not finish: ${stopReasonPhrase(reason)}.`;
|
|
42
24
|
}
|
|
43
25
|
export async function writeStudioRunSummary(directory, record) {
|
|
@@ -53,56 +35,25 @@ export async function writeStudioRunSummary(directory, record) {
|
|
|
53
35
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 });
|
|
54
36
|
await writeSessionSnapshot(join(runRecordsDirectory(directory), `${record.id}.studio-summary`), JSON.stringify(projected) + '\n');
|
|
55
37
|
}
|
|
56
|
-
/**
|
|
57
|
-
* The claim naming a run open — required, unlike the supplemental studio summary.
|
|
58
|
-
*
|
|
59
|
-
* The claim is the answer `sessionHasOpenRun` gives every rail mutation, so a run whose claim
|
|
60
|
-
* failed to land would be invisible to the guard while its result record says the session is
|
|
61
|
-
* live: another process could then archive, rename, or delete the live session, and the run's
|
|
62
|
-
* final save would overwrite or recreate what that mutation did. `writeOpenRunRecord` therefore
|
|
63
|
-
* publishes the record only once this write has landed; the snapshot layer behind it already
|
|
64
|
-
* retries the transient rename failures (Windows AV locks) that a suppressed failure would
|
|
65
|
-
* otherwise turn into that state.
|
|
66
|
-
*/
|
|
67
38
|
async function writeOpenRunClaim(directory, claim) {
|
|
68
39
|
const body = { runId: claim.runId, sessionId: claim.sessionId, pid: claim.pid ?? process.pid, claimedAt: new Date().toISOString(), startedAt: processStartedAt(), nonce: PROCESS_NONCE };
|
|
69
40
|
await writeSessionSnapshot(runRecordPath(directory, claim.runId, 'open-run'), JSON.stringify(body) + '\n');
|
|
70
41
|
}
|
|
71
|
-
/** Drop a run's open claim. A run only ever owns its own claim, so this cannot clobber another's. */
|
|
72
42
|
export async function clearOpenRunClaim(directory, runId) {
|
|
73
43
|
await rm(runRecordPath(directory, runId, 'open-run'), { force: true }).catch(() => { });
|
|
74
44
|
}
|
|
75
|
-
/**
|
|
76
|
-
* Remove the record of an attempt that never began.
|
|
77
|
-
*
|
|
78
|
-
* The claim and the record answer one question — is a run live? — and a question with no run behind
|
|
79
|
-
* it has to have neither. A record left behind is an attempt the Runs view lists, and its live pid
|
|
80
|
-
* tells the recovery that another process owns it, so the attempt stays `running` until that
|
|
81
|
-
* process exits.
|
|
82
|
-
*/
|
|
83
45
|
export async function removeOpenRunRecord(directory, runId) {
|
|
84
|
-
// The claim is released only when the record is gone. A record that still says the run is live,
|
|
85
|
-
// with no claim to say so, is a history that contradicts itself — and when the file cannot be
|
|
86
|
-
// removed, the claim stays and the recovery classifies the attempt once its owner is gone.
|
|
87
46
|
const removed = await rm(runRecordPath(directory, runId, 'json'), { force: true }).then(() => true, () => false);
|
|
88
47
|
await rm(join(runRecordsDirectory(directory), `${runId}.studio-summary`), { force: true }).catch(() => { });
|
|
89
48
|
if (removed)
|
|
90
49
|
await clearOpenRunClaim(directory, runId);
|
|
91
50
|
}
|
|
92
|
-
/** The result file written at start; atomic, so a crash mid-write cannot tear the record. */
|
|
93
51
|
export async function writeOpenRunRecord(directory, record) {
|
|
94
52
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 });
|
|
95
|
-
// The claim lands before the record, and the record is not published without it: from the
|
|
96
|
-
// moment any engine can see the open record, the guard already has its index entry. The reverse
|
|
97
|
-
// order — or a suppressed claim failure — would leave the state this method's own guard cannot
|
|
98
|
-
// answer: the record says the run is live while the claim, the guard's fast answer, is missing,
|
|
99
|
-
// and a rail mutation of this session is accepted against a run that trusts the record.
|
|
100
53
|
await writeOpenRunClaim(directory, { runId: record.id, sessionId: record.sessionId, pid: record.pid });
|
|
101
54
|
await writeSessionSnapshot(runRecordPath(directory, record.id, 'json'), JSON.stringify(record, null, 2) + '\n');
|
|
102
|
-
// Best effort: failure of the supplemental index must not abort a real coding run.
|
|
103
55
|
await writeStudioRunSummary(directory, record).catch(() => { });
|
|
104
56
|
}
|
|
105
|
-
/** Whether a process is running. A live process owned by another user answers EPERM, not ESRCH. */
|
|
106
57
|
export function isPidAlive(pid) {
|
|
107
58
|
if (!Number.isSafeInteger(pid) || pid < 1)
|
|
108
59
|
return false;
|
|
@@ -114,70 +65,29 @@ export function isPidAlive(pid) {
|
|
|
114
65
|
return error.code === 'EPERM';
|
|
115
66
|
}
|
|
116
67
|
}
|
|
117
|
-
/** This process's own start time, as the claims it writes record it. */
|
|
118
68
|
export function processStartedAt() {
|
|
119
69
|
return new Date(Date.now() - Math.round(process.uptime() * 1000)).toISOString();
|
|
120
70
|
}
|
|
121
|
-
/**
|
|
122
|
-
* This process instance's identity, as the claims it writes record it.
|
|
123
|
-
*
|
|
124
|
-
* A pid is not an identity — the OS hands a dead process's number to the next one, and a
|
|
125
|
-
* container that restarts within seconds reuses the same number for its replacement. A start
|
|
126
|
-
* time compared with a tolerance cannot tell those apart either: a restarted PID-1 container
|
|
127
|
-
* starts within the tolerance of the one it replaces, so its predecessor's claim would look
|
|
128
|
-
* like this process's own. A value that exists only in this instance of the process answers it
|
|
129
|
-
* exactly: the claim carries the nonce of the process that wrote it, and a claim this process
|
|
130
|
-
* did not write is a claim of a different instance, whatever the number it carried.
|
|
131
|
-
*/
|
|
132
71
|
const PROCESS_NONCE = randomUUID();
|
|
133
|
-
/** This process instance's nonce, for a claim that carries one. */
|
|
134
72
|
export function processNonce() {
|
|
135
73
|
return PROCESS_NONCE;
|
|
136
74
|
}
|
|
137
|
-
/**
|
|
138
|
-
* The workspaces this process is working in.
|
|
139
|
-
*
|
|
140
|
-
* The reservation is the process's rather than an engine's: an app server and an MCP or CLI
|
|
141
|
-
* subagent in one process are two engines over one workspace, and two full-access runs in it
|
|
142
|
-
* would write the same files at once. The unit the reservation covers is the workspace, because
|
|
143
|
-
* that is what two overlapping runs would both write to.
|
|
144
|
-
*/
|
|
145
75
|
const processWorkspaces = new Map();
|
|
146
|
-
/**
|
|
147
|
-
* Reserve a workspace for an engine in this process.
|
|
148
|
-
*
|
|
149
|
-
* @returns whether the reservation was taken; a workspace another engine in this process is
|
|
150
|
-
* working in is not available.
|
|
151
|
-
*/
|
|
152
76
|
export function claimProcessWorkspace(workspace, owner) {
|
|
153
77
|
if (processWorkspaces.has(workspace))
|
|
154
78
|
return false;
|
|
155
79
|
processWorkspaces.set(workspace, owner);
|
|
156
80
|
return true;
|
|
157
81
|
}
|
|
158
|
-
/** Give a workspace back, when the engine releasing it is the one that reserved it. */
|
|
159
82
|
export function releaseProcessWorkspace(workspace, owner) {
|
|
160
83
|
if (processWorkspaces.get(workspace) === owner)
|
|
161
84
|
processWorkspaces.delete(workspace);
|
|
162
85
|
}
|
|
163
|
-
/**
|
|
164
|
-
* When a running process started, as the platform can report it, or `undefined` when it cannot.
|
|
165
|
-
*
|
|
166
|
-
* A pid alone is not an identity: the OS reuses it, so a claim whose holder was killed can appear
|
|
167
|
-
* to be held by an unrelated process that inherited the number, and that attempt would refuse to be
|
|
168
|
-
* resumed for as long as the impostor lives. Comparing the start time the claim recorded for its
|
|
169
|
-
* holder with the start time the OS reports for that pid is what tells the two apart. The probe is
|
|
170
|
-
* deliberately best-effort — a platform that cannot answer returns nothing, and the caller falls
|
|
171
|
-
* back to pid liveness — because being unable to check must never turn a live holder into a stale
|
|
172
|
-
* one (which is the direction that would let two engines resume the same attempt).
|
|
173
|
-
*/
|
|
174
86
|
async function processStartedAtFor(pid) {
|
|
175
87
|
if (!Number.isSafeInteger(pid) || pid < 1)
|
|
176
88
|
return undefined;
|
|
177
89
|
try {
|
|
178
90
|
if (process.platform === 'linux') {
|
|
179
|
-
// Field 22 of `/proc/<pid>/stat` is the start time in clock ticks since boot; the process name
|
|
180
|
-
// (field 2) may hold spaces and parentheses, so parsing starts after its last `)`.
|
|
181
91
|
const stat = await readFile(`/proc/${pid}/stat`, 'utf8');
|
|
182
92
|
const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
|
|
183
93
|
const ticks = Number(fields[19]);
|
|
@@ -200,20 +110,8 @@ async function processStartedAtFor(pid) {
|
|
|
200
110
|
return undefined;
|
|
201
111
|
}
|
|
202
112
|
}
|
|
203
|
-
/** How far a recorded start time may differ from the probed one and still be the same process. */
|
|
204
113
|
const PROCESS_START_TOLERANCE_MS = 5_000;
|
|
205
114
|
const validSessionFile = (id) => /^[a-f0-9-]{36}$/i.test(id);
|
|
206
|
-
/**
|
|
207
|
-
* Read many files at once and map each one's text through `pick`, in order; a file that cannot be
|
|
208
|
-
* read, or that `pick` maps to undefined, yields undefined.
|
|
209
|
-
*
|
|
210
|
-
* The startup recovery pass looks at every run result and every saved session, which on a long-used
|
|
211
|
-
* installation is well over a thousand files. Read one after another they kept the terminal from
|
|
212
|
-
* drawing for seconds — far longer on a cold disk, where each open waits on the device and on file
|
|
213
|
-
* scanning. A bounded number in flight overlaps those waits without exhausting file handles, and
|
|
214
|
-
* `pick` runs inside each worker, so a file's text is dropped as soon as it is read unless `pick`
|
|
215
|
-
* keeps it: memory stays bounded by the files in flight and what is kept, not by the whole history.
|
|
216
|
-
*/
|
|
217
115
|
async function scanFiles(paths, pick, concurrency = 32) {
|
|
218
116
|
const contents = new Array(paths.length).fill(undefined);
|
|
219
117
|
let next = 0;
|
|
@@ -233,25 +131,6 @@ async function scanFiles(paths, pick, concurrency = 32) {
|
|
|
233
131
|
await Promise.all(Array.from({ length: Math.min(concurrency, paths.length) }, worker));
|
|
234
132
|
return contents;
|
|
235
133
|
}
|
|
236
|
-
/**
|
|
237
|
-
* Classify the runs and session turns a previous process left unfinished.
|
|
238
|
-
*
|
|
239
|
-
* A run that finalizes replaces its open record and its marker turn together; anything that ends
|
|
240
|
-
* that way needs no help here. What this repairs is the other end: a process killed between the
|
|
241
|
-
* open record and the finalizer leaves a record stuck at `running` and a session turn claiming the
|
|
242
|
-
* attempt "is still running", and neither can tell the difference between live work and a crash on
|
|
243
|
-
* its own. The pid answers it — a process this home's engine cannot see is gone — and from there
|
|
244
|
-
* the record becomes `interrupted`, the marker entry is upgraded to say so, and a marker whose
|
|
245
|
-
* record never landed gets an interrupted record synthesized so the run is at least listable.
|
|
246
|
-
*
|
|
247
|
-
* A record whose pid is alive is left untouched: another engine on this home may genuinely be
|
|
248
|
-
* running it — and it is named in `live`, captured by the same walk, so a caller who wants to
|
|
249
|
-
* react when such a holder exits needs no second scan (and no gap between two). `onlyRunIds`
|
|
250
|
-
* bounds the rewrite to records the caller already knows its own earlier sweep left open: a
|
|
251
|
-
* delayed repair must never touch a record a live process still owns, including the calling
|
|
252
|
-
* engine's own active run. The whole pass is best-effort; one damaged record never hides the
|
|
253
|
-
* others.
|
|
254
|
-
*/
|
|
255
134
|
export async function reconcileInterruptedRuns(directory, onlyRunIds, options) {
|
|
256
135
|
const recovery = { runs: [], markers: 0, live: [], interrupted: [] };
|
|
257
136
|
const swept = await markOpenRunsInterrupted(directory, onlyRunIds, options?.skipOwnProcess === true, options?.includePending === true);
|
|
@@ -261,41 +140,13 @@ export async function reconcileInterruptedRuns(directory, onlyRunIds, options) {
|
|
|
261
140
|
recovery.markers = await upgradeSessionMarkers(directory);
|
|
262
141
|
return recovery;
|
|
263
142
|
}
|
|
264
|
-
/**
|
|
265
|
-
* The open records this process can no longer own, rewritten with status `interrupted`, plus the
|
|
266
|
-
* records the same walk left running because a live foreign process owns them.
|
|
267
|
-
*
|
|
268
|
-
* The live set is what lets a starting engine watch its predecessor without a second scan: the
|
|
269
|
-
* sweep either recovers a record whose holder is gone or names it here, so an exit at any moment
|
|
270
|
-
* is caught by one pass or the other. `onlyRunIds` bounds the rewrite to records the caller knows
|
|
271
|
-
* its own earlier sweep left open — a delayed repair must not touch a record a live process still
|
|
272
|
-
* owns, including the calling engine's own active run.
|
|
273
|
-
*/
|
|
274
|
-
/**
|
|
275
|
-
* Whether an open run belongs to a run this very process instance is executing.
|
|
276
|
-
*
|
|
277
|
-
* A pid is not an identity: the OS hands a dead process's number to the next one, and this
|
|
278
|
-
* process's own number is no different. The claim beside the record carries the start time its
|
|
279
|
-
* owner recorded, so a claim this instance wrote is told apart from one an earlier instance with
|
|
280
|
-
* the same number wrote. The first is a live run — an in-process host that starts a second engine
|
|
281
|
-
* must not terminalize the first engine's work, which is still executing — and the second is the
|
|
282
|
-
* debris of a crash.
|
|
283
|
-
*
|
|
284
|
-
* A claim with no recorded start time, or one that names another process, proves neither: the
|
|
285
|
-
* answer is the conservative one, and the run is classified as a crash.
|
|
286
|
-
*/
|
|
287
143
|
async function claimedByThisProcess(directory, runId) {
|
|
288
144
|
try {
|
|
289
145
|
const claim = JSON.parse(await readFile(runRecordPath(directory, runId, 'open-run'), 'utf8'));
|
|
290
146
|
if (claim.pid !== process.pid)
|
|
291
147
|
return false;
|
|
292
|
-
// The nonce is the exact answer: a claim this process instance wrote carries it, and one
|
|
293
|
-
// another instance wrote does not — however close its start time is to this one's.
|
|
294
148
|
if (typeof claim.nonce === 'string')
|
|
295
149
|
return claim.nonce === PROCESS_NONCE;
|
|
296
|
-
// A claim without one was written before the nonce existed, so the start time is all there
|
|
297
|
-
// is. The tolerance is the conservative direction: a claim that looks like this process's own
|
|
298
|
-
// is left alone rather than terminalized.
|
|
299
150
|
if (typeof claim.startedAt !== 'string')
|
|
300
151
|
return false;
|
|
301
152
|
const claimed = Date.parse(claim.startedAt);
|
|
@@ -321,11 +172,7 @@ async function markOpenRunsInterrupted(directory, onlyRunIds, skipOwnProcess = f
|
|
|
321
172
|
const ids = [];
|
|
322
173
|
const live = [];
|
|
323
174
|
const records = [];
|
|
324
|
-
// Only result records: `.json` is the result's extension, and `.checkpoint.json` names only
|
|
325
|
-
// ever existed as a checkpoint before the suffix was shortened to keep it out of the scans.
|
|
326
175
|
const results = names.filter(name => name.endsWith('.json') && !name.endsWith('.checkpoint.json'));
|
|
327
|
-
// A record this pass acts on is `running`, or `interrupted` with `telemetryPending`; a file
|
|
328
|
-
// naming neither cannot be one, and skipping its parse is most of the pass on a large history.
|
|
329
176
|
const texts = await scanFiles(results.map(name => join(runs, name)), text => text.includes('running') || text.includes('telemetryPending') ? text : undefined);
|
|
330
177
|
for (const [index, name] of results.entries()) {
|
|
331
178
|
const path = join(runs, name);
|
|
@@ -346,35 +193,19 @@ async function markOpenRunsInterrupted(directory, onlyRunIds, skipOwnProcess = f
|
|
|
346
193
|
const runId = typeof value.id === 'string' && value.id === name.slice(0, -'.json'.length) ? value.id : undefined;
|
|
347
194
|
if (runId === undefined)
|
|
348
195
|
continue;
|
|
349
|
-
// A record rewritten by an earlier recovery whose finish was never queued: the pair is still
|
|
350
|
-
// owed, and the derived event id makes re-reporting it idempotent.
|
|
351
196
|
if (value.status === 'interrupted') {
|
|
352
197
|
records.push(value);
|
|
353
198
|
continue;
|
|
354
199
|
}
|
|
355
200
|
const pid = typeof value.pid === 'number' ? value.pid : undefined;
|
|
356
|
-
// A live foreign pid is the only answer that keeps a record open — another engine's run, or
|
|
357
|
-
// the predecessor still shutting down, and it is named in `live` for a watcher to hold. This
|
|
358
|
-
// process's own pid keeps its active run open too, but it is nobody to watch: the engine is
|
|
359
|
-
// the one running this code. Anything else is a crash.
|
|
360
201
|
if (pid !== undefined && pid !== process.pid && isPidAlive(pid)) {
|
|
361
202
|
live.push({ runId, pid, sessionId: typeof value.sessionId === 'string' ? value.sessionId : undefined });
|
|
362
203
|
continue;
|
|
363
204
|
}
|
|
364
|
-
// A run this very process instance is executing is not debris either. A pid is not an
|
|
365
|
-
// identity — the OS hands a dead process's number to the next one, and this process's own
|
|
366
|
-
// number is no different — so the claim beside the record is what tells the two apart: it
|
|
367
|
-
// carries the start time its owner recorded, and a claim this instance wrote is a run that
|
|
368
|
-
// is still going. An in-process host that starts a second engine must not terminalize the
|
|
369
|
-
// first engine's live work: the claim it would remove is what holds the session against a
|
|
370
|
-
// mutation, and the run would save over that mutation when it finished.
|
|
371
205
|
if (skipOwnProcess && pid === process.pid && await claimedByThisProcess(directory, runId))
|
|
372
206
|
continue;
|
|
373
207
|
if (onlyRunIds !== undefined && !onlyRunIds.has(runId))
|
|
374
208
|
continue;
|
|
375
|
-
// Two coordinators can read the same open record before either rewrite lands. The lock makes
|
|
376
|
-
// the running-to-interrupted transition single-owner, so only one of them enqueues the finish;
|
|
377
|
-
// the loser has nothing to emit and leaves the record to the winner.
|
|
378
209
|
const lockDirectory = join(runs, `${runId}.recover`);
|
|
379
210
|
let release;
|
|
380
211
|
try {
|
|
@@ -387,13 +218,8 @@ async function markOpenRunsInterrupted(directory, onlyRunIds, skipOwnProcess = f
|
|
|
387
218
|
}
|
|
388
219
|
const unlock = async () => {
|
|
389
220
|
await release();
|
|
390
|
-
// The lock's own directory is an implementation detail; leaving one empty directory per
|
|
391
|
-
// recovered run would clutter `runs/` for no reason.
|
|
392
221
|
await rm(lockDirectory, { recursive: true, force: true }).catch(() => { });
|
|
393
222
|
};
|
|
394
|
-
// The record was read before the lock. A coordinator that read it, was descheduled, and then
|
|
395
|
-
// acquired the lock the winner just released would otherwise rewrite a record that already
|
|
396
|
-
// says `interrupted` and enqueue a second finish. Only the reading under the lock counts.
|
|
397
223
|
let current;
|
|
398
224
|
try {
|
|
399
225
|
current = JSON.parse(await readFile(path, 'utf8'));
|
|
@@ -406,8 +232,6 @@ async function markOpenRunsInterrupted(directory, onlyRunIds, skipOwnProcess = f
|
|
|
406
232
|
await unlock();
|
|
407
233
|
continue;
|
|
408
234
|
}
|
|
409
|
-
// `telemetryPending` is cleared by whoever queues the finish. A process that dies between the
|
|
410
|
-
// rewrite and the enqueue therefore leaves a record a later pass still knows to report.
|
|
411
235
|
const interrupted = { ...current, status: 'interrupted', telemetryPending: true, reason: 'the engine stopped while this run was active', endedAt: new Date().toISOString() };
|
|
412
236
|
try {
|
|
413
237
|
await writeSessionSnapshot(path, JSON.stringify(interrupted, null, 2) + '\n');
|
|
@@ -417,25 +241,16 @@ async function markOpenRunsInterrupted(directory, onlyRunIds, skipOwnProcess = f
|
|
|
417
241
|
continue;
|
|
418
242
|
}
|
|
419
243
|
await unlock();
|
|
420
|
-
// Startup recovery can delete the dead claim before MCP monitoring ever
|
|
421
|
-
// runs. Preserve the pre-snapshot crash signal from the recovered run record
|
|
422
|
-
// so a previous successful session turn cannot hide this interrupted attempt.
|
|
423
244
|
const crash = await rememberCrashedClaim(directory, {
|
|
424
245
|
runId, sessionId: value.sessionId, claimedAt: value.startedAt,
|
|
425
246
|
});
|
|
426
|
-
// A marker that could not be persisted leaves the claim as the only
|
|
427
|
-
// durable, retryable copy of the signal: removing it here would erase the
|
|
428
|
-
// crash from monitoring for good. A later monitor pass retries the write.
|
|
429
247
|
if (crash?.durable !== false)
|
|
430
|
-
// The record says the attempt is over, so its open claim must stop naming it to the guard: a
|
|
431
|
-
// claim whose holder later turns out to be alive would otherwise hold the session shut.
|
|
432
248
|
await rm(join(runs, `${runId}.open-run`), { force: true }).catch(() => { });
|
|
433
249
|
ids.push(runId);
|
|
434
250
|
records.push(interrupted);
|
|
435
251
|
}
|
|
436
252
|
return { ids, live, records };
|
|
437
253
|
}
|
|
438
|
-
/** The facts a session's own history carries, when a caller already holds the snapshot. */
|
|
439
254
|
function sessionCrashFacts(history) {
|
|
440
255
|
const turns = Array.isArray(history) ? history : [];
|
|
441
256
|
const runIds = new Set();
|
|
@@ -464,7 +279,6 @@ async function readSessionCrashFacts(directory, sessionId) {
|
|
|
464
279
|
return { kind: 'unreadable' };
|
|
465
280
|
}
|
|
466
281
|
}
|
|
467
|
-
/** Whether a run that started after `time` has persisted a turn in this session. */
|
|
468
282
|
async function persistedRunStartedAfter(directory, runId, time) {
|
|
469
283
|
if (runId === undefined)
|
|
470
284
|
return false;
|
|
@@ -472,23 +286,6 @@ async function persistedRunStartedAfter(directory, runId, time) {
|
|
|
472
286
|
const startedAt = typeof record?.startedAt === 'string' ? Date.parse(record.startedAt) : Number.NaN;
|
|
473
287
|
return Number.isFinite(startedAt) && startedAt > time;
|
|
474
288
|
}
|
|
475
|
-
/**
|
|
476
|
-
* A crashed run may die between writing its claim and saving the running turn.
|
|
477
|
-
* Keep a small, durable monitor-only signal before cleaning up the dead claim.
|
|
478
|
-
*
|
|
479
|
-
* The sidecar exists for one state only: an attempt the session snapshot never
|
|
480
|
-
* records, hidden behind an older turn. A session that has a turn naming this
|
|
481
|
-
* run already reports the attempt — a `running` marker turn included — so a
|
|
482
|
-
* finalizer's terminal record left behind by a failed claim removal must not
|
|
483
|
-
* become a `crashedAttempt`. A run that started after the claim proves a newer
|
|
484
|
-
* attempt was persisted, which supersedes the crash. Startup recovery calls
|
|
485
|
-
* this after rewriting its record, so the pre-snapshot test is the same whether
|
|
486
|
-
* recovery or a monitor observed the dead claim.
|
|
487
|
-
*
|
|
488
|
-
* @returns the marker and whether it reached the disk. `undefined` means no
|
|
489
|
-
* marker is needed; `durable: false` means the caller must keep its claim, the
|
|
490
|
-
* only retryable copy of the signal left.
|
|
491
|
-
*/
|
|
492
289
|
async function rememberCrashedClaim(directory, claim, factsFor = sessionId => readSessionCrashFacts(directory, sessionId)) {
|
|
493
290
|
const runId = claim.runId, sessionId = claim.sessionId, claimedAt = claim.claimedAt;
|
|
494
291
|
if (typeof runId !== 'string' || typeof sessionId !== 'string'
|
|
@@ -496,8 +293,6 @@ async function rememberCrashedClaim(directory, claim, factsFor = sessionId => re
|
|
|
496
293
|
|| typeof claimedAt !== 'string' || !Number.isFinite(Date.parse(claimedAt)))
|
|
497
294
|
return undefined;
|
|
498
295
|
const facts = await factsFor(sessionId);
|
|
499
|
-
// A deleted session has no monitor row to report. An unreadable snapshot is
|
|
500
|
-
// not proof the attempt was recorded, so only a readable one can answer.
|
|
501
296
|
if (facts.kind === 'missing')
|
|
502
297
|
return undefined;
|
|
503
298
|
if (facts.kind === 'present') {
|
|
@@ -512,8 +307,6 @@ async function rememberCrashedClaim(directory, claim, factsFor = sessionId => re
|
|
|
512
307
|
return { marker, durable: true };
|
|
513
308
|
}
|
|
514
309
|
catch {
|
|
515
|
-
// The session layer already retries transient Windows renames; a failure
|
|
516
|
-
// that survives that is reported so the caller keeps its claim.
|
|
517
310
|
return { marker, durable: false };
|
|
518
311
|
}
|
|
519
312
|
}
|
|
@@ -530,9 +323,6 @@ export async function sessionsWithOpenRunClaims(directory, sessions) {
|
|
|
530
323
|
}
|
|
531
324
|
const active = new Set();
|
|
532
325
|
const crashed = new Map();
|
|
533
|
-
// A query passes the same snapshot its views are built from, so pruning can
|
|
534
|
-
// never delete a marker using a save the answer does not reflect. Without one
|
|
535
|
-
// (no current caller) the files are the only source.
|
|
536
326
|
const snapshots = sessions === undefined ? undefined : new Map(sessions.map(session => [session.id, session]));
|
|
537
327
|
const fileFacts = new Map();
|
|
538
328
|
const absentFacts = new Map();
|
|
@@ -550,10 +340,6 @@ export async function sessionsWithOpenRunClaims(directory, sessions) {
|
|
|
550
340
|
const session = snapshots.get(sessionId);
|
|
551
341
|
if (session !== undefined)
|
|
552
342
|
return sessionCrashFacts(session.history);
|
|
553
|
-
// The store omits unreadable or invalid records from its listing, so
|
|
554
|
-
// absence from the snapshot is proof of deletion only when the file
|
|
555
|
-
// itself is gone. Anything still on disk stays an unknown state, keeping
|
|
556
|
-
// its marker alive for when the record is repaired.
|
|
557
343
|
let pending = absentFacts.get(sessionId);
|
|
558
344
|
if (pending === undefined) {
|
|
559
345
|
pending = confirmAbsent(sessionId);
|
|
@@ -568,15 +354,11 @@ export async function sessionsWithOpenRunClaims(directory, sessions) {
|
|
|
568
354
|
}
|
|
569
355
|
return pending;
|
|
570
356
|
};
|
|
571
|
-
// Only the newest unresolved crash per session is actionable. Older sidecars
|
|
572
|
-
// can never outlive it (a persisted run that supersedes the newest also
|
|
573
|
-
// supersedes them), so keeping them would make every poll reread them forever.
|
|
574
357
|
const kept = new Map();
|
|
575
358
|
const keepCrash = async (entry, file) => {
|
|
576
359
|
const existing = crashed.get(entry.sessionId);
|
|
577
360
|
const prior = kept.get(entry.sessionId);
|
|
578
361
|
if (existing !== undefined && Date.parse(existing.claimedAt) >= Date.parse(entry.claimedAt)) {
|
|
579
|
-
// The selected crash is at least as new; this sidecar is redundant.
|
|
580
362
|
if (file !== prior)
|
|
581
363
|
await rm(join(runs, file), { force: true }).catch(() => { });
|
|
582
364
|
return;
|
|
@@ -600,9 +382,6 @@ export async function sessionsWithOpenRunClaims(directory, sessions) {
|
|
|
600
382
|
|| name !== entry.runId + '.mcp-crash')
|
|
601
383
|
continue;
|
|
602
384
|
const known = await factsFor(entry.sessionId);
|
|
603
|
-
// Remove markers nothing can report any more, so the scan stays bounded:
|
|
604
|
-
// a deleted session is orphaned, and a session that already records the
|
|
605
|
-
// attempt, or a later run that supersedes it, makes the sidecar redundant.
|
|
606
385
|
if (known.kind === 'missing'
|
|
607
386
|
|| (known.kind === 'present' && (known.runIds.has(entry.runId)
|
|
608
387
|
|| await persistedRunStartedAfter(directory, known.lastRunId, Date.parse(entry.claimedAt))))) {
|
|
@@ -611,7 +390,7 @@ export async function sessionsWithOpenRunClaims(directory, sessions) {
|
|
|
611
390
|
}
|
|
612
391
|
await keepCrash({ runId: entry.runId, sessionId: entry.sessionId, claimedAt: entry.claimedAt }, name);
|
|
613
392
|
}
|
|
614
|
-
catch {
|
|
393
|
+
catch { }
|
|
615
394
|
}
|
|
616
395
|
for (const name of names) {
|
|
617
396
|
if (!name.endsWith('.open-run'))
|
|
@@ -621,7 +400,7 @@ export async function sessionsWithOpenRunClaims(directory, sessions) {
|
|
|
621
400
|
claim = JSON.parse(await readFile(join(runs, name), 'utf8'));
|
|
622
401
|
}
|
|
623
402
|
catch {
|
|
624
|
-
continue;
|
|
403
|
+
continue;
|
|
625
404
|
}
|
|
626
405
|
if (!claim || typeof claim !== 'object' || typeof claim.sessionId !== 'string')
|
|
627
406
|
continue;
|
|
@@ -634,22 +413,12 @@ export async function sessionsWithOpenRunClaims(directory, sessions) {
|
|
|
634
413
|
const outcome = await rememberCrashedClaim(directory, claim, factsFor);
|
|
635
414
|
if (outcome !== undefined)
|
|
636
415
|
await keepCrash({ ...outcome.marker, sessionId: claim.sessionId }, outcome.marker.runId + '.mcp-crash');
|
|
637
|
-
// Keep a claim whose crash marker could not be persisted: the claim is the
|
|
638
|
-
// only retryable copy, and removing it would permanently hide the attempt.
|
|
639
416
|
if (outcome?.durable === false)
|
|
640
417
|
continue;
|
|
641
418
|
await rm(join(runs, name), { force: true }).catch(() => { });
|
|
642
419
|
}
|
|
643
420
|
return { active, crashed };
|
|
644
421
|
}
|
|
645
|
-
/**
|
|
646
|
-
* Whether a live open-run claim still holds this session against mutations.
|
|
647
|
-
*
|
|
648
|
-
* `ignoreRunId` excuses one run from the answer: the engine that owns that run mutates the session
|
|
649
|
-
* safely beside it when its finalizer merges into the record it saves, so it names its own
|
|
650
|
-
* in-flight run here instead of being held by its own claim. Every other owner — a second window
|
|
651
|
-
* or the CLI sharing the home — answers exactly as before, crash-claim cleanup included.
|
|
652
|
-
*/
|
|
653
422
|
export async function sessionHasOpenRun(directory, sessionId, options = {}) {
|
|
654
423
|
const runs = runRecordsDirectory(directory);
|
|
655
424
|
let names;
|
|
@@ -670,24 +439,15 @@ export async function sessionHasOpenRun(directory, sessionId, options = {}) {
|
|
|
670
439
|
try {
|
|
671
440
|
claim = JSON.parse(await readFile(join(runs, name), 'utf8'));
|
|
672
441
|
}
|
|
673
|
-
// A claim that cannot be parsed proves no owner but hides what the records would say: the scan
|
|
674
|
-
// is the conservative answer for this directory, not a silent miss.
|
|
675
442
|
catch {
|
|
676
443
|
return scanRunsForOpenRun(directory, sessionId, options.ignoreRunId);
|
|
677
444
|
}
|
|
678
445
|
if (!claim || typeof claim !== 'object' || claim.sessionId !== sessionId)
|
|
679
446
|
continue;
|
|
680
|
-
// The named run is the caller's own work, not a stranger: its claim is excused outright, so
|
|
681
|
-
// nothing here reclaims or removes it.
|
|
682
447
|
if (options.ignoreRunId !== undefined && (claim.runId === options.ignoreRunId || name === `${options.ignoreRunId}.open-run`))
|
|
683
448
|
continue;
|
|
684
|
-
// A claim the finalizer failed to remove is not evidence of live work, however alive its owner
|
|
685
|
-
// looks: see `claimOutlivedItsRun`. Reclaiming it here is what keeps a transient removal failure
|
|
686
|
-
// from holding a session shut for the life of the process that wrote it.
|
|
687
449
|
if (await claimOutlivedItsRun(directory, claim)) {
|
|
688
450
|
const outcome = await rememberCrashedClaim(directory, claim);
|
|
689
|
-
// A marker that could not be written leaves the claim in place: it is the
|
|
690
|
-
// only retryable signal that this attempt never reached the session.
|
|
691
451
|
if (outcome?.durable !== false)
|
|
692
452
|
await rm(join(runs, name), { force: true }).catch(() => { });
|
|
693
453
|
continue;
|
|
@@ -701,17 +461,6 @@ export async function sessionHasOpenRun(directory, sessionId, options = {}) {
|
|
|
701
461
|
}
|
|
702
462
|
return false;
|
|
703
463
|
}
|
|
704
|
-
/**
|
|
705
|
-
* Whether an armed wake still names this session from any engine.
|
|
706
|
-
*
|
|
707
|
-
* A wake lives in the registry of the engine that armed it, and every engine sharing the
|
|
708
|
-
* installation runs a reaper pass over the same store: asked of its own registry alone, one
|
|
709
|
-
* engine's pass would file away a session another engine still owes a prompt. Each arm wrote a
|
|
710
|
-
* claim, so the claims are the answer. A claim whose owner is live holds the session; a claim
|
|
711
|
-
* whose owner is gone is swept here, because the wake it named was a promise its process held and
|
|
712
|
-
* nothing will deliver it now. A claim that cannot be read might name a live wake, so it holds
|
|
713
|
-
* the session too.
|
|
714
|
-
*/
|
|
715
464
|
export async function sessionHasArmedWake(directory, sessionId) {
|
|
716
465
|
const runs = runRecordsDirectory(directory);
|
|
717
466
|
let names;
|
|
@@ -740,47 +489,26 @@ export async function sessionHasArmedWake(directory, sessionId) {
|
|
|
740
489
|
armed = true;
|
|
741
490
|
continue;
|
|
742
491
|
}
|
|
743
|
-
// The owner is gone, and with it the wake: the claim it left cannot hold the session against
|
|
744
|
-
// every later pass, so the read that proved it dead clears it.
|
|
745
492
|
await rm(join(runs, name), { force: true }).catch(() => { });
|
|
746
493
|
}
|
|
747
494
|
return armed;
|
|
748
495
|
}
|
|
749
|
-
/** The path of one wake's claim, beside the open-run claims it is read with. */
|
|
750
496
|
function wakeClaimPath(directory, sessionId, timerId) {
|
|
751
497
|
if (!validSessionFile(sessionId) || !validSessionFile(timerId)) {
|
|
752
498
|
throw new Error('A wake claim needs a session identity and a timer identity');
|
|
753
499
|
}
|
|
754
500
|
return join(runRecordsDirectory(directory), `${sessionId}.${timerId}.wake-claim`);
|
|
755
501
|
}
|
|
756
|
-
/**
|
|
757
|
-
* Record that this engine armed a wake for a session.
|
|
758
|
-
*
|
|
759
|
-
* The wake itself lives in this process's registry, so a reaper pass in another engine cannot see
|
|
760
|
-
* it there. The claim is what makes it visible across the engines that share the installation: one
|
|
761
|
-
* small file per wake, carrying the writing process's identity, read by `sessionHasArmedWake`. Best
|
|
762
|
-
* effort — a claim that fails to land leaves the wake invisible to a foreign pass until its
|
|
763
|
-
* delivery lands and reverts the filing, the way the run that armed it would have.
|
|
764
|
-
*/
|
|
765
502
|
export async function writeWakeClaim(directory, sessionId, timerId) {
|
|
766
503
|
const body = { sessionId, timerId, pid: process.pid, claimedAt: new Date().toISOString(), startedAt: processStartedAt(), nonce: PROCESS_NONCE };
|
|
767
|
-
// A home with no runs yet has no directory for the claim: the write lands beside the open-run claims.
|
|
768
504
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 }).catch(() => { });
|
|
769
505
|
await writeSessionSnapshot(wakeClaimPath(directory, sessionId, timerId), JSON.stringify(body) + '\n');
|
|
770
506
|
}
|
|
771
|
-
/** Drop one wake's claim: the wake was delivered, spent, or canceled by the engine that armed it. */
|
|
772
507
|
export async function clearWakeClaim(directory, sessionId, timerId) {
|
|
773
508
|
if (!validSessionFile(sessionId) || !validSessionFile(timerId))
|
|
774
509
|
return;
|
|
775
510
|
await rm(wakeClaimPath(directory, sessionId, timerId), { force: true }).catch(() => { });
|
|
776
511
|
}
|
|
777
|
-
/**
|
|
778
|
-
* Drop every claim a session's wakes hold, when the wakes go with the session they were armed for.
|
|
779
|
-
*
|
|
780
|
-
* Deleting a session clears its wakes with the record; the claims have to go with them, or claims
|
|
781
|
-
* naming a session that no longer exists would outlive the session that could ever clear them.
|
|
782
|
-
* Only claims naming this session are removed: another session's wakes are nobody else's to drop.
|
|
783
|
-
*/
|
|
784
512
|
export async function clearWakeClaims(directory, sessionId) {
|
|
785
513
|
if (!validSessionFile(sessionId))
|
|
786
514
|
return;
|
|
@@ -800,21 +528,6 @@ export async function clearWakeClaims(directory, sessionId) {
|
|
|
800
528
|
await rm(join(runs, name), { force: true }).catch(() => { });
|
|
801
529
|
}
|
|
802
530
|
}
|
|
803
|
-
/**
|
|
804
|
-
* Whether this claim's own run record closed after the claim was made, which makes the claim failed
|
|
805
|
-
* cleanup rather than live work.
|
|
806
|
-
*
|
|
807
|
-
* A run finalizes by rewriting its result record and then removing its claim. If that removal fails — a
|
|
808
|
-
* scanner holding the file on Windows is enough, and the removal is deliberately best-effort so a
|
|
809
|
-
* transient lock cannot abort a finished run — the leftover goes on naming its owner, and nothing else
|
|
810
|
-
* sweeps it: the startup repair walks only records that are still `running`, so a claim whose record is
|
|
811
|
-
* closed survives every later restart and keeps `sessionHasOpenRun` answering `busy` for this session
|
|
812
|
-
* until the process it names exits. Comparing the two stamps is what tells this apart from the claim of
|
|
813
|
-
* a run in flight: a resume reuses its attempt's run id, so a record that closed *before* this claim was
|
|
814
|
-
* written belongs to that earlier attempt and says nothing about this one. A record that is absent,
|
|
815
|
-
* unreadable, or without a closing stamp proves nothing, and the claim stands — the conservative
|
|
816
|
-
* direction, because clearing a live claim is what lets a mutation land on a session a run still owns.
|
|
817
|
-
*/
|
|
818
531
|
async function claimOutlivedItsRun(directory, claim) {
|
|
819
532
|
const runId = typeof claim.runId === 'string' ? claim.runId : undefined;
|
|
820
533
|
if (runId === undefined)
|
|
@@ -837,18 +550,6 @@ async function claimOutlivedItsRun(directory, claim) {
|
|
|
837
550
|
const endedAt = typeof closed.endedAt === 'string' ? Date.parse(closed.endedAt) : Number.NaN;
|
|
838
551
|
return Number.isFinite(endedAt) && endedAt > claimedAt;
|
|
839
552
|
}
|
|
840
|
-
/**
|
|
841
|
-
* Whether the process an open-run claim names is the one that wrote it.
|
|
842
|
-
*
|
|
843
|
-
* `isPidAlive` alone is not an identity: the OS reuses numbers, so a claim whose engine crashed
|
|
844
|
-
* can come to name an unrelated process that inherited the pid, and treating that as the owner
|
|
845
|
-
* would hold the session busy — refusing every rename, archive, and delete — for as long as the
|
|
846
|
-
* impostor lives. The start time the owner recorded when it claimed the run is corroborated
|
|
847
|
-
* against the one the platform reports for that pid, which is how `inspectClaim` judges a resume
|
|
848
|
-
* claim. A claim written before that field existed, and a platform that cannot report a start
|
|
849
|
-
* time, fall back to pid liveness: the conservative direction, because a live owner read as
|
|
850
|
-
* stale is what lets a rail mutation land while a run still owns the session.
|
|
851
|
-
*/
|
|
852
553
|
async function claimOwnerIsLive(pid, startedAt) {
|
|
853
554
|
if (pid === undefined || !Number.isSafeInteger(pid) || pid < 1)
|
|
854
555
|
return true;
|
|
@@ -857,11 +558,9 @@ async function claimOwnerIsLive(pid, startedAt) {
|
|
|
857
558
|
const claimed = typeof startedAt === 'string' ? Date.parse(startedAt) : Number.NaN;
|
|
858
559
|
if (!Number.isFinite(claimed))
|
|
859
560
|
return true;
|
|
860
|
-
// Our own start time is known without a probe: the common case is this process's own run.
|
|
861
561
|
const actual = pid === process.pid ? Date.parse(processStartedAt()) : await processStartedAtFor(pid);
|
|
862
562
|
return actual === undefined || Math.abs(actual - claimed) <= PROCESS_START_TOLERANCE_MS;
|
|
863
563
|
}
|
|
864
|
-
/** The pre-index answer: read the result records themselves. Only the fallback paths reach it. */
|
|
865
564
|
async function scanRunsForOpenRun(directory, sessionId, ignoreRunId) {
|
|
866
565
|
const runs = runRecordsDirectory(directory);
|
|
867
566
|
let names;
|
|
@@ -885,7 +584,6 @@ async function scanRunsForOpenRun(directory, sessionId, ignoreRunId) {
|
|
|
885
584
|
}
|
|
886
585
|
if (!value || typeof value !== 'object' || value.status !== 'running' || value.sessionId !== sessionId)
|
|
887
586
|
continue;
|
|
888
|
-
// The caller's own in-flight run is excused from the answer, exactly as in the claim index.
|
|
889
587
|
if (ignoreRunId !== undefined && (value.id === ignoreRunId || name === `${ignoreRunId}.json`))
|
|
890
588
|
continue;
|
|
891
589
|
const pid = typeof value.pid === 'number' ? value.pid : undefined;
|
|
@@ -894,25 +592,13 @@ async function scanRunsForOpenRun(directory, sessionId, ignoreRunId) {
|
|
|
894
592
|
}
|
|
895
593
|
return false;
|
|
896
594
|
}
|
|
897
|
-
/** Whether a value is the plain object a completion's `output` is declared to be. */
|
|
898
595
|
const plainObject = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
|
|
899
|
-
/** Whether a value is the `{ type, ref }` pair a completion's artifact reference is declared to be. */
|
|
900
596
|
function isArtifactRef(value) {
|
|
901
597
|
if (!value || typeof value !== 'object')
|
|
902
598
|
return false;
|
|
903
599
|
const ref = value;
|
|
904
600
|
return typeof ref.type === 'string' && typeof ref.ref === 'string';
|
|
905
601
|
}
|
|
906
|
-
/**
|
|
907
|
-
* Bring every session's view of unfinished attempts in line with the run records.
|
|
908
|
-
*
|
|
909
|
-
* A marker turn whose record finalized means the final turn never reached the session file — the
|
|
910
|
-
* finalizer saved the record and the session save itself failed — so the marker takes the record's
|
|
911
|
-
* own account of the attempt. A marker with no record at all is a crash between the two writes: it
|
|
912
|
-
* becomes interrupted, and with a known run identity an interrupted record is synthesized so the
|
|
913
|
-
* Runs view shows the attempt instead of only the transcript. A marker whose record is still open
|
|
914
|
-
* under a live pid is real in-flight work and stays.
|
|
915
|
-
*/
|
|
916
602
|
async function upgradeSessionMarkers(directory, only) {
|
|
917
603
|
const sessions = join(directory, 'sessions');
|
|
918
604
|
let names;
|
|
@@ -926,10 +612,6 @@ async function upgradeSessionMarkers(directory, only) {
|
|
|
926
612
|
}
|
|
927
613
|
let markers = 0;
|
|
928
614
|
const candidates = names.filter(name => name.endsWith('.json') && (only === undefined || name === `${only}.json`));
|
|
929
|
-
// A marker turn's completion has status `running` or the running summary, and both contain the
|
|
930
|
-
// word; a session without it has nothing to repair. The scan only narrows the set: a candidate is
|
|
931
|
-
// read again below, so the repair never writes back a copy older than the file, and the scan
|
|
932
|
-
// keeps only the answer, not the text.
|
|
933
615
|
const mayHaveMarker = await scanFiles(candidates.map(name => join(sessions, name)), text => text.includes('running') || undefined);
|
|
934
616
|
for (const [index, name] of candidates.entries()) {
|
|
935
617
|
if (!mayHaveMarker[index])
|
|
@@ -956,32 +638,18 @@ async function upgradeSessionMarkers(directory, only) {
|
|
|
956
638
|
if (record && record.status === 'running') {
|
|
957
639
|
if (typeof record.pid !== 'number' || isPidAlive(record.pid))
|
|
958
640
|
continue;
|
|
959
|
-
// `markOpenRunsInterrupted` already landed, but a record this pass cannot rewrite (a
|
|
960
|
-
// locked file on Windows) must not leave the transcript claiming work in flight.
|
|
961
641
|
}
|
|
962
642
|
if (record && record.status !== 'running' && typeof record.status === 'string') {
|
|
963
|
-
// The attempt finished; only the session never heard how — or what it produced. The
|
|
964
|
-
// finalization persisted the whole completion (tests, outcome, artifacts) even when its
|
|
965
|
-
// session save failed, so the repaired turn takes it whole rather than only its summary.
|
|
966
643
|
const finalized = (record.completion && typeof record.completion === 'object' && !Array.isArray(record.completion))
|
|
967
644
|
? { ...record.completion }
|
|
968
645
|
: undefined;
|
|
969
646
|
if (finalized) {
|
|
970
647
|
if (record.status === 'succeeded') {
|
|
971
|
-
// A successful run: the turn is a normal completed turn, and the desktop labels any
|
|
972
|
-
// present non-running status as a stopped one, so it carries the record's completion
|
|
973
|
-
// with no status at all.
|
|
974
648
|
delete finalized.status;
|
|
975
649
|
}
|
|
976
650
|
else {
|
|
977
651
|
finalized.status = record.status;
|
|
978
652
|
}
|
|
979
|
-
// The saved session must stay loadable and renderable: session validation keeps only a
|
|
980
|
-
// string summary and a string-array tests list, and clients render the outcome beside
|
|
981
|
-
// them, so the fields a hand-edited or damaged record gets wrong are repaired to their
|
|
982
|
-
// declared shapes rather than written through verbatim. `output` and `artifacts` are not
|
|
983
|
-
// drawn but are serialized into the next run's model context, so they are held to their
|
|
984
|
-
// shapes too; a field that cannot be repaired is dropped.
|
|
985
653
|
if (typeof finalized.summary !== 'string' || finalized.summary.length === 0) {
|
|
986
654
|
finalized.summary = record.status === 'succeeded'
|
|
987
655
|
? (typeof completion.summary === 'string' && completion.summary.length > 0 ? completion.summary : summaryForStoppedRecord(record))
|
|
@@ -989,8 +657,6 @@ async function upgradeSessionMarkers(directory, only) {
|
|
|
989
657
|
}
|
|
990
658
|
if (!Array.isArray(finalized.tests) || !finalized.tests.every((test) => typeof test === 'string'))
|
|
991
659
|
finalized.tests = [];
|
|
992
|
-
// A React child must stringify: an object where the outcome belongs throws while the
|
|
993
|
-
// transcript renders, which would make the repaired session unusable in the desktop.
|
|
994
660
|
if (typeof finalized.outcome !== 'string')
|
|
995
661
|
delete finalized.outcome;
|
|
996
662
|
if (!plainObject(finalized.output))
|
|
@@ -1006,9 +672,6 @@ async function upgradeSessionMarkers(directory, only) {
|
|
|
1006
672
|
completion.summary = summaryForStoppedRecord(record);
|
|
1007
673
|
completion.status = record.status;
|
|
1008
674
|
}
|
|
1009
|
-
// The session totals are the pre-run snapshot the marker was saved with; the attempt's
|
|
1010
|
-
// usage lives only in the record. Fold it once per file even when the marker turn sits
|
|
1011
|
-
// in both history arrays, or the recovery would count the same run twice.
|
|
1012
675
|
if (known && !usageFolded.has(runId)) {
|
|
1013
676
|
usageFolded.add(runId);
|
|
1014
677
|
foldRecordUsage(session, record.usage);
|
|
@@ -1028,28 +691,13 @@ async function upgradeSessionMarkers(directory, only) {
|
|
|
1028
691
|
await writeSessionSnapshot(join(sessions, name), JSON.stringify(session) + '\n');
|
|
1029
692
|
markers += changed;
|
|
1030
693
|
}
|
|
1031
|
-
catch {
|
|
694
|
+
catch { }
|
|
1032
695
|
}
|
|
1033
696
|
return markers;
|
|
1034
697
|
}
|
|
1035
|
-
/**
|
|
1036
|
-
* Bring one session's unfinished turns in line with the run records.
|
|
1037
|
-
*
|
|
1038
|
-
* The same repair the startup pass makes, for one session: a run whose final session save failed
|
|
1039
|
-
* leaves its turn marked running, and a long-lived server would keep it that way until it was
|
|
1040
|
-
* restarted. The attempt's own record says how it ended, so the turn takes that account of it.
|
|
1041
|
-
*/
|
|
1042
698
|
export async function repairSessionMarkers(directory, sessionId) {
|
|
1043
699
|
return await upgradeSessionMarkers(directory, sessionId);
|
|
1044
700
|
}
|
|
1045
|
-
/**
|
|
1046
|
-
* Add a finished run's usage to the session totals during recovery.
|
|
1047
|
-
*
|
|
1048
|
-
* The saved session holds the totals as they were when the marker was written — before the run's
|
|
1049
|
-
* model calls — and after a failed final save the attempt's usage exists only on its record. The
|
|
1050
|
-
* fold matches the live one: the three required counters always add, a cache counter the provider
|
|
1051
|
-
* reported (even as zero) adds, and an unreported one stays absent.
|
|
1052
|
-
*/
|
|
1053
701
|
function foldRecordUsage(session, usage) {
|
|
1054
702
|
if (!usage || typeof usage !== 'object' || Array.isArray(usage))
|
|
1055
703
|
return;
|
|
@@ -1074,12 +722,9 @@ async function readRunRecord(directory, runId) {
|
|
|
1074
722
|
return undefined;
|
|
1075
723
|
}
|
|
1076
724
|
}
|
|
1077
|
-
/** The record a crashed run never wrote, so the attempt is listable and classifiable. */
|
|
1078
725
|
async function synthesizeInterruptedRecord(directory, runId, sessionId) {
|
|
1079
726
|
if (await readRunRecord(directory, runId) !== undefined)
|
|
1080
727
|
return;
|
|
1081
|
-
// The crash can land before anything ever created `runs/`: the session marker is written first,
|
|
1082
|
-
// and a home whose only run never opened a record has no directory to synthesize into.
|
|
1083
728
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 });
|
|
1084
729
|
let startedAt = new Date().toISOString();
|
|
1085
730
|
try {
|
|
@@ -1088,72 +733,34 @@ async function synthesizeInterruptedRecord(directory, runId, sessionId) {
|
|
|
1088
733
|
if (typeof time === 'string')
|
|
1089
734
|
startedAt = time;
|
|
1090
735
|
}
|
|
1091
|
-
catch {
|
|
736
|
+
catch { }
|
|
1092
737
|
try {
|
|
1093
738
|
await writeSessionSnapshot(runRecordPath(directory, runId, 'json'), JSON.stringify({
|
|
1094
739
|
id: runId, status: 'interrupted', reason: 'the engine stopped before the run was recorded', startedAt,
|
|
1095
740
|
...(sessionId === undefined ? {} : { sessionId }), usage: { inputTokens: 0, outputTokens: 0, requests: 0 }, toolCalls: 0,
|
|
1096
741
|
}, null, 2) + '\n');
|
|
1097
742
|
}
|
|
1098
|
-
catch {
|
|
743
|
+
catch { }
|
|
1099
744
|
}
|
|
1100
|
-
/**
|
|
1101
|
-
* The path a run's persisted record lives at.
|
|
1102
|
-
*
|
|
1103
|
-
* The run identity reaches this path from runtime events, where it is typed `unknown`, so the
|
|
1104
|
-
* alphabet is checked here at the sink rather than trusted from every caller. The identity is
|
|
1105
|
-
* always a UUID, and the guard admits only `[A-Za-z0-9_-]` -- the same alphabet `ArtifactStore`
|
|
1106
|
-
* uses for its own run-scoped directory. That rejects every spelling that could leave `runs/`:
|
|
1107
|
-
* `.` and `..` segments (the dot is not in the alphabet, so `..` is not merely resolved away but
|
|
1108
|
-
* refused), `/` and `\` separators, an absolute path, and a Windows drive-relative or
|
|
1109
|
-
* alternate-data-stream name (`:`).
|
|
1110
|
-
* @param directory - the installation directory holding `runs/`.
|
|
1111
|
-
* @param runId - the run identity, typically a generated UUID.
|
|
1112
|
-
* @param extension - which record per run this path names: the result, the journal, or the
|
|
1113
|
-
* checkpoint the resume work leaves behind until the run succeeds.
|
|
1114
|
-
* @returns the absolute or installation-relative path of the record.
|
|
1115
|
-
*/
|
|
1116
745
|
export function runRecordPath(directory, runId, extension) {
|
|
1117
|
-
// The alphabet alone is not the whole test: `$` also matches just before a trailing newline, so
|
|
1118
|
-
// `/^…$/` would admit "run\n" as readily as "run". Requiring the value to survive `trim()`
|
|
1119
|
-
// unchanged closes that and rejects padded input at the same time.
|
|
1120
746
|
if (typeof runId !== 'string' || runId.trim() !== runId || !/^[\w-]{1,64}$/.test(runId)) {
|
|
1121
|
-
// The rejected value is deliberately not echoed: it is untrusted text, and an error message
|
|
1122
|
-
// is rendered to a terminal.
|
|
1123
747
|
throw new Error('A persisted run record needs a run identity of up to 64 word characters');
|
|
1124
748
|
}
|
|
1125
749
|
return join(runRecordsDirectory(directory), `${runId}.${extension}`);
|
|
1126
750
|
}
|
|
1127
|
-
/**
|
|
1128
|
-
* Record that a run's journal is missing events, durably and separately from the sink that failed.
|
|
1129
|
-
*
|
|
1130
|
-
* The engine deliberately keeps running when an append fails (a full disk must not kill the user's
|
|
1131
|
-
* work), and the journal writer holds what it could not write and writes it once the file accepts
|
|
1132
|
-
* appends again. This marker covers the window in between and the cases that never close: the
|
|
1133
|
-
* process dying while events are still held, or the writer giving some up. The lines after such a
|
|
1134
|
-
* gap are present, so nothing in the file itself reveals it. The writer removes the marker once
|
|
1135
|
-
* everything it held has landed (best effort both ways: if the marker cannot be written, the
|
|
1136
|
-
* record's own flag is what stands), and `journalWriteFailed` is what a resume reads to warn that a
|
|
1137
|
-
* step may repeat.
|
|
1138
|
-
*/
|
|
1139
751
|
export async function noteJournalWriteFailure(directory, runId, detail) {
|
|
1140
752
|
try {
|
|
1141
753
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 });
|
|
1142
754
|
await appendFile(runRecordPath(directory, runId, 'journal-error'), `${new Date().toISOString()}\t${detail.replace(/[\r\n]+/g, ' ')}\n`, { mode: 0o600 });
|
|
1143
755
|
}
|
|
1144
|
-
catch {
|
|
756
|
+
catch { }
|
|
1145
757
|
}
|
|
1146
|
-
/**
|
|
1147
|
-
* Whether a run's journal is known to be incomplete: the marker the writer left, or the flag the
|
|
1148
|
-
* attempt's result record carries. A resume still continues such an attempt (a session must
|
|
1149
|
-
* always be resumable), but it tells the user that a step the missing events recorded may repeat.
|
|
1150
|
-
*/
|
|
1151
758
|
export async function journalWriteFailed(directory, runId) {
|
|
1152
759
|
try {
|
|
1153
760
|
await stat(runRecordPath(directory, runId, 'journal-error'));
|
|
1154
761
|
return true;
|
|
1155
762
|
}
|
|
1156
|
-
catch {
|
|
763
|
+
catch { }
|
|
1157
764
|
try {
|
|
1158
765
|
const value = JSON.parse(await readFile(runRecordPath(directory, runId, 'json'), 'utf8'));
|
|
1159
766
|
return value.journalIntegrity === 'failed';
|
|
@@ -1162,30 +769,7 @@ export async function journalWriteFailed(directory, runId) {
|
|
|
1162
769
|
return false;
|
|
1163
770
|
}
|
|
1164
771
|
}
|
|
1165
|
-
/** How much a journal writer holds in memory while its file refuses appends, before it gives up. */
|
|
1166
772
|
const MAX_HELD_JOURNAL_BYTES = 16 * 1024 * 1024;
|
|
1167
|
-
/**
|
|
1168
|
-
* The journal sink for one run: ordered appends that leave no silent hole.
|
|
1169
|
-
*
|
|
1170
|
-
* The filesystem does not finish concurrent appends in the order they were started, so events are
|
|
1171
|
-
* written one after another. An event the file refuses (a full disk, most often) is held rather
|
|
1172
|
-
* than dropped, and each later event first retries everything held, oldest first. A disk that
|
|
1173
|
-
* fills for a minute and is then freed leaves a complete journal behind, not a permanently
|
|
1174
|
-
* untrusted one.
|
|
1175
|
-
*
|
|
1176
|
-
* A failed append can still have written part of its line, so after any failed write the next one
|
|
1177
|
-
* measures the file before it appends anything. When the file grew past the length the writer last
|
|
1178
|
-
* knew to be whole lines, it is cut back first, so a retried event neither merges with its own torn
|
|
1179
|
-
* fragment nor appears twice. While the file cannot be measured (or is not a regular file), nothing
|
|
1180
|
-
* is appended: the events stay held, and the journal cannot be reported complete. When even the cut
|
|
1181
|
-
* fails, the next line starts on a fresh line and the journal is reported as no longer complete: the
|
|
1182
|
-
* fragment cannot be told apart from an event that really ran. The same holds when the file turns
|
|
1183
|
-
* out shorter than the lines already written to it: something else cut it, and those lines are gone.
|
|
1184
|
-
*
|
|
1185
|
-
* While anything is held, or after anything was given up, the run's `journal-error` marker says
|
|
1186
|
-
* so. Events are given up only when what is held passes `maxHeldBytes`. Once everything held has
|
|
1187
|
-
* landed and nothing was given up, the writer removes the marker again.
|
|
1188
|
-
*/
|
|
1189
773
|
export function createRunJournalWriter(directory, runId, options = {}) {
|
|
1190
774
|
const path = runRecordPath(directory, runId, 'jsonl');
|
|
1191
775
|
const files = {
|
|
@@ -1208,25 +792,14 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1208
792
|
const limit = options.maxHeldBytes ?? MAX_HELD_JOURNAL_BYTES;
|
|
1209
793
|
const held = [];
|
|
1210
794
|
let heldBytes = 0;
|
|
1211
|
-
// The file's length as far as this writer knows it: every byte before it is part of a whole line,
|
|
1212
|
-
// unless `separate` says a fragment the writer could not cut is at the end. Unknown until the
|
|
1213
|
-
// first write measures the file.
|
|
1214
795
|
let size;
|
|
1215
|
-
// The next line must start on a fresh line, because a torn fragment is still at the end of the file.
|
|
1216
796
|
let separate = false;
|
|
1217
|
-
// An event was given up, or a torn fragment could not be cut: the journal can no longer prove it is
|
|
1218
|
-
// a complete account, however many later appends succeed.
|
|
1219
797
|
let lost = false;
|
|
1220
|
-
// A write failed since the last time everything held reached the file.
|
|
1221
798
|
let failing = false;
|
|
1222
|
-
// The last write failed, so the file's end is not known to be where `size` says: it may hold a
|
|
1223
|
-
// fragment of the refused line, or have changed since. The next write measures it first.
|
|
1224
799
|
let unverified = false;
|
|
1225
800
|
let chain = Promise.resolve();
|
|
1226
801
|
const writeLine = async (line) => {
|
|
1227
802
|
if (size === undefined || unverified) {
|
|
1228
|
-
// A measurement that fails throws before anything is appended: the line stays held, and the
|
|
1229
|
-
// next write measures again.
|
|
1230
803
|
const actual = await files.size(path);
|
|
1231
804
|
if (actual === undefined) {
|
|
1232
805
|
throw Object.assign(new Error('The run journal path is not a regular file'), { code: 'EISDIR' });
|
|
@@ -1235,8 +808,6 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1235
808
|
size = actual;
|
|
1236
809
|
}
|
|
1237
810
|
else if (actual > size) {
|
|
1238
|
-
// `size` only grows when an append succeeds, so it is still the length before the refused
|
|
1239
|
-
// one: everything past it is a torn fragment of a line that is still held.
|
|
1240
811
|
try {
|
|
1241
812
|
await files.truncate(path, size);
|
|
1242
813
|
}
|
|
@@ -1247,7 +818,6 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1247
818
|
}
|
|
1248
819
|
}
|
|
1249
820
|
else if (actual < size) {
|
|
1250
|
-
// Something else cut the file, and lines this writer had written are gone.
|
|
1251
821
|
size = actual;
|
|
1252
822
|
separate = true;
|
|
1253
823
|
lost = true;
|
|
@@ -1259,8 +829,6 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1259
829
|
await files.appendFile(path, data);
|
|
1260
830
|
}
|
|
1261
831
|
catch (error) {
|
|
1262
|
-
// A configuration home that has never held a run has no `runs/` directory yet. Nothing was
|
|
1263
|
-
// written, so the append is simply repeated once the directory exists.
|
|
1264
832
|
if (error.code !== 'ENOENT')
|
|
1265
833
|
throw error;
|
|
1266
834
|
await files.mkdir(runRecordsDirectory(directory));
|
|
@@ -1276,8 +844,6 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1276
844
|
await writeLine(line);
|
|
1277
845
|
}
|
|
1278
846
|
catch (error) {
|
|
1279
|
-
// Whatever failed, the file is measured before the next append rather than here: a probe
|
|
1280
|
-
// that fails right after a torn append must not let a later retry write after the fragment.
|
|
1281
847
|
unverified = true;
|
|
1282
848
|
throw error;
|
|
1283
849
|
}
|
|
@@ -1289,8 +855,6 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1289
855
|
try {
|
|
1290
856
|
await drain();
|
|
1291
857
|
if (failing && !lost) {
|
|
1292
|
-
// Everything the file refused has now landed, in order: the journal is a complete account
|
|
1293
|
-
// again, and the marker that said otherwise goes.
|
|
1294
858
|
failing = false;
|
|
1295
859
|
await rm(runRecordPath(directory, runId, 'journal-error'), { force: true }).catch(() => { });
|
|
1296
860
|
}
|
|
@@ -1298,8 +862,6 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1298
862
|
catch (error) {
|
|
1299
863
|
failing = true;
|
|
1300
864
|
if (heldBytes > limit) {
|
|
1301
|
-
// Holding more would trade the user's memory for a journal that is already late. What is
|
|
1302
|
-
// held is given up, and the marker stays for good.
|
|
1303
865
|
held.length = 0;
|
|
1304
866
|
heldBytes = 0;
|
|
1305
867
|
lost = true;
|
|
@@ -1308,13 +870,11 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1308
870
|
try {
|
|
1309
871
|
options.onFailure?.(error);
|
|
1310
872
|
}
|
|
1311
|
-
catch {
|
|
873
|
+
catch { }
|
|
1312
874
|
}
|
|
1313
875
|
};
|
|
1314
876
|
return {
|
|
1315
877
|
append(event) {
|
|
1316
|
-
// Serialized now, not when the chain reaches it: the event is recorded as it was when it
|
|
1317
|
-
// happened, even if its object is changed while earlier appends are still landing.
|
|
1318
878
|
let line;
|
|
1319
879
|
let unserializable;
|
|
1320
880
|
try {
|
|
@@ -1325,14 +885,13 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1325
885
|
}
|
|
1326
886
|
chain = chain.then(async () => {
|
|
1327
887
|
if (line === undefined) {
|
|
1328
|
-
// An event that cannot be serialized can never be written, so it is a gap like any other.
|
|
1329
888
|
failing = true;
|
|
1330
889
|
lost = true;
|
|
1331
890
|
await noteJournalWriteFailure(directory, runId, unserializable instanceof Error ? unserializable.message : String(unserializable));
|
|
1332
891
|
try {
|
|
1333
892
|
options.onFailure?.(unserializable);
|
|
1334
893
|
}
|
|
1335
|
-
catch {
|
|
894
|
+
catch { }
|
|
1336
895
|
return;
|
|
1337
896
|
}
|
|
1338
897
|
held.push(line);
|
|
@@ -1350,19 +909,12 @@ export function createRunJournalWriter(directory, runId, options = {}) {
|
|
|
1350
909
|
get intact() { return held.length === 0 && !lost; },
|
|
1351
910
|
};
|
|
1352
911
|
}
|
|
1353
|
-
/** A visit as the record and the marker carry it: no `generation` key when the writer did not know it. */
|
|
1354
912
|
function checkpointVisit(nodeId, generation) {
|
|
1355
913
|
return generation === undefined ? { nodeId } : { nodeId, generation };
|
|
1356
914
|
}
|
|
1357
|
-
/**
|
|
1358
|
-
* One marker line: when, which visit (as JSON, so any node id survives the tab-separated format),
|
|
1359
|
-
* and what failed. Lines from builds that predate the visit column carry the detail in the second
|
|
1360
|
-
* field, which never parses as a visit, so their node is unknown.
|
|
1361
|
-
*/
|
|
1362
915
|
function checkpointFailureLine(visit, detail) {
|
|
1363
916
|
return `${new Date().toISOString()}\t${JSON.stringify(visit)}\t${detail.replace(/[\r\n\t]+/g, ' ')}\n`;
|
|
1364
917
|
}
|
|
1365
|
-
/** A visit read back from a record or a marker, or `undefined` when the value is not one. */
|
|
1366
918
|
function parseCheckpointVisit(value) {
|
|
1367
919
|
if (typeof value !== 'object' || value === null)
|
|
1368
920
|
return undefined;
|
|
@@ -1373,11 +925,6 @@ function parseCheckpointVisit(value) {
|
|
|
1373
925
|
return undefined;
|
|
1374
926
|
return checkpointVisit(nodeId, generation);
|
|
1375
927
|
}
|
|
1376
|
-
/**
|
|
1377
|
-
* Track one run's checkpoint writes. The caller reports every write's outcome in the order the
|
|
1378
|
-
* writes ran (the checkpoint writers already serialize them). The run's final record carries
|
|
1379
|
-
* `checkpointIntegrity: 'failed'` and the `failures` list while `intact` is false.
|
|
1380
|
-
*/
|
|
1381
928
|
export function createCheckpointFailureTracker(directory, runId) {
|
|
1382
929
|
const marker = runRecordPath(directory, runId, 'checkpoint-error');
|
|
1383
930
|
const failures = new Map();
|
|
@@ -1391,7 +938,7 @@ export function createCheckpointFailureTracker(directory, runId) {
|
|
|
1391
938
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 });
|
|
1392
939
|
await appendFile(marker, line, { mode: 0o600 });
|
|
1393
940
|
}
|
|
1394
|
-
catch {
|
|
941
|
+
catch { }
|
|
1395
942
|
},
|
|
1396
943
|
async succeeded(visit) {
|
|
1397
944
|
if (!failures.delete(key(visit)))
|
|
@@ -1402,33 +949,15 @@ export function createCheckpointFailureTracker(directory, runId) {
|
|
|
1402
949
|
else
|
|
1403
950
|
await writeFile(marker, [...failures.values()].map(entry => entry.line).join(''), { mode: 0o600 });
|
|
1404
951
|
}
|
|
1405
|
-
catch {
|
|
952
|
+
catch { }
|
|
1406
953
|
},
|
|
1407
954
|
get intact() { return failures.size === 0; },
|
|
1408
955
|
get failures() { return [...failures.values()].map(entry => ({ ...entry.visit })); },
|
|
1409
956
|
};
|
|
1410
957
|
}
|
|
1411
|
-
/**
|
|
1412
|
-
* Whether a run's checkpoints are known to be incomplete: the record's flag, or the marker the
|
|
1413
|
-
* tracker left. A resume still continues such an attempt, and tells the user that a visit whose
|
|
1414
|
-
* conversation was not saved may repeat work.
|
|
1415
|
-
*/
|
|
1416
958
|
export async function checkpointWriteFailed(directory, runId) {
|
|
1417
959
|
return (await checkpointFailure(directory, runId)) !== undefined;
|
|
1418
960
|
}
|
|
1419
|
-
/**
|
|
1420
|
-
* The checkpoint failure a run left unhealed: `undefined` when there is none.
|
|
1421
|
-
*
|
|
1422
|
-
* The final record comes first. It is written once every checkpoint write has settled, from the
|
|
1423
|
-
* tracker itself, so its list is the whole failure set — while the marker is appended as failures
|
|
1424
|
-
* happen, and the same full disk that failed a checkpoint can fail the marker line naming it. A
|
|
1425
|
-
* record that is flagged but carries no list (a build that predates the list) therefore names
|
|
1426
|
-
* nothing and dates nothing: its marker may be missing the very failure that matters. Only a run
|
|
1427
|
-
* with no final record (one that crashed) is read from its marker, best effort.
|
|
1428
|
-
*
|
|
1429
|
-
* `visits` is absent when the failures cannot be named. `at` lets a resume see that a checkpoint
|
|
1430
|
-
* saved after the newest failure already replaced whatever the failed write would have written.
|
|
1431
|
-
*/
|
|
1432
961
|
export async function checkpointFailure(directory, runId) {
|
|
1433
962
|
let record;
|
|
1434
963
|
try {
|
|
@@ -1447,7 +976,6 @@ export async function checkpointFailure(directory, runId) {
|
|
|
1447
976
|
text = await readFile(runRecordPath(directory, runId, 'checkpoint-error'), 'utf8');
|
|
1448
977
|
}
|
|
1449
978
|
catch (error) {
|
|
1450
|
-
// A marker that exists but cannot be read is still a failure, only an unnamed one.
|
|
1451
979
|
return error.code === 'ENOENT' ? undefined : {};
|
|
1452
980
|
}
|
|
1453
981
|
const visits = new Map();
|
|
@@ -1482,15 +1010,6 @@ export async function checkpointFailure(directory, runId) {
|
|
|
1482
1010
|
...(dated && at !== undefined ? { at } : {}),
|
|
1483
1011
|
};
|
|
1484
1012
|
}
|
|
1485
|
-
/**
|
|
1486
|
-
* The checkpoint file for one node of a run: `<runId>.checkpoint.<node-key>`.
|
|
1487
|
-
*
|
|
1488
|
-
* Parallel branches checkpoint concurrently, and one shared file per run meant every branch's
|
|
1489
|
-
* write overwrote the others' — a crash kept only whichever conversation landed last. Each node
|
|
1490
|
-
* owns a file instead, so every unfinished branch survives. Node ids carry arbitrary characters,
|
|
1491
|
-
* so the name gets a sanitized prefix for debuggability plus a hash of the exact id for
|
|
1492
|
-
* uniqueness: two ids that sanitize to the same prefix still get different files.
|
|
1493
|
-
*/
|
|
1494
1013
|
function runCheckpointPath(directory, runId, nodeId) {
|
|
1495
1014
|
const slug = nodeId.replace(/[^A-Za-z0-9_-]/g, '-').slice(0, 48);
|
|
1496
1015
|
let hash = 2166136261;
|
|
@@ -1500,34 +1019,10 @@ function runCheckpointPath(directory, runId, nodeId) {
|
|
|
1500
1019
|
}
|
|
1501
1020
|
return `${runRecordPath(directory, runId, 'checkpoint')}.${slug}-${(hash >>> 0).toString(36)}`;
|
|
1502
1021
|
}
|
|
1503
|
-
/**
|
|
1504
|
-
* Persist the in-flight conversation of the node a run is working on.
|
|
1505
|
-
*
|
|
1506
|
-
* The write is atomic (a staged rename, the same discipline as every durable record in this
|
|
1507
|
-
* directory) at mode 0600: the file carries the node's full message history — including tool
|
|
1508
|
-
* outputs — so it is as sensitive as the run journal, and a crash mid-write must never leave a
|
|
1509
|
-
* half-file a resume would read back. A new snapshot replaces the last; the runner already
|
|
1510
|
-
* sanitizes an interrupted conversation before publishing its checkpoint (stub tool messages are
|
|
1511
|
-
* marked as not replayed), so whatever the callback handed over is what belongs on disk.
|
|
1512
|
-
*/
|
|
1513
1022
|
export async function writeRunCheckpoint(directory, runId, checkpoint) {
|
|
1514
1023
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 });
|
|
1515
1024
|
await writeSessionSnapshot(runCheckpointPath(directory, runId, checkpoint.nodeId), JSON.stringify(checkpoint) + '\n');
|
|
1516
1025
|
}
|
|
1517
|
-
/**
|
|
1518
|
-
* Read back one node's checkpoint, or nothing when there is no usable one.
|
|
1519
|
-
*
|
|
1520
|
-
* Missing and unreadable both mean the same thing to the caller: resume without the prior
|
|
1521
|
-
* conversation (the safe restart the runner falls back to). Shape validation is deliberately
|
|
1522
|
-
* shallow — a checkpoint from an incompatible build would carry the wrong state fields, and the
|
|
1523
|
-
* node re-running from scratch is safer than a seeded conversation whose counters disagree with
|
|
1524
|
-
* the graph the run is being replayed onto.
|
|
1525
|
-
*
|
|
1526
|
-
* One field is rebuilt rather than trusted: the tool-failure tracker is a class whose behavior
|
|
1527
|
-
* (and whose Map) JSON does not carry, so a resumed node gets a fresh one. Losing the repeated-
|
|
1528
|
-
* failure strike counts is the conservative direction — the worst case is one more retry before
|
|
1529
|
-
* the tracker strikes out again, not a crash on a state object without its methods.
|
|
1530
|
-
*/
|
|
1531
1026
|
export async function readRunCheckpoint(directory, runId, nodeId) {
|
|
1532
1027
|
let raw;
|
|
1533
1028
|
try {
|
|
@@ -1538,11 +1033,6 @@ export async function readRunCheckpoint(directory, runId, nodeId) {
|
|
|
1538
1033
|
}
|
|
1539
1034
|
return parseRunCheckpoint(raw);
|
|
1540
1035
|
}
|
|
1541
|
-
/**
|
|
1542
|
-
* Read every checkpoint the run kept — one file per node, plus the legacy single-file shape an
|
|
1543
|
-
* older build may have left behind. Unreadable or invalid files are skipped individually: a
|
|
1544
|
-
* damaged branch costs that branch its conversation, nothing more.
|
|
1545
|
-
*/
|
|
1546
1036
|
export async function readRunCheckpoints(directory, runId) {
|
|
1547
1037
|
const root = runRecordsDirectory(directory);
|
|
1548
1038
|
const prefix = `${runId}.checkpoint`;
|
|
@@ -1555,8 +1045,6 @@ export async function readRunCheckpoints(directory, runId) {
|
|
|
1555
1045
|
}
|
|
1556
1046
|
const checkpoints = [];
|
|
1557
1047
|
for (const name of names.sort()) {
|
|
1558
|
-
// The bare `<runId>.checkpoint` is the pre-per-node shape; a per-node file is
|
|
1559
|
-
// `<runId>.checkpoint.<node-key>`. Nothing else the runs directory holds matches either.
|
|
1560
1048
|
if (name !== prefix && !name.startsWith(`${prefix}.`))
|
|
1561
1049
|
continue;
|
|
1562
1050
|
let raw;
|
|
@@ -1572,16 +1060,6 @@ export async function readRunCheckpoints(directory, runId) {
|
|
|
1572
1060
|
}
|
|
1573
1061
|
return checkpoints;
|
|
1574
1062
|
}
|
|
1575
|
-
/**
|
|
1576
|
-
* Rebuild every tool-failure tracker a persisted continuation state carries.
|
|
1577
|
-
*
|
|
1578
|
-
* The tracker is a class JSON cannot round-trip, so its strike counts live as plain numbers in the
|
|
1579
|
-
* file and its methods do not. A modified agent's state nests one continuation per copy
|
|
1580
|
-
* (`state.copies[i].state`), and the runtime reaches into those on the resumed copy's first tool
|
|
1581
|
-
* result — where a plain object has no `record`, so the failure lands after the tool already
|
|
1582
|
-
* executed and the effect can be repeated by the next resume. The rebuild is recursive for the same
|
|
1583
|
-
* reason: whatever state shape nests further, every tracker in it has to come back as a tracker.
|
|
1584
|
-
*/
|
|
1585
1063
|
function reviveFailures(state) {
|
|
1586
1064
|
const revived = { ...state, failures: new ToolFailureTracker() };
|
|
1587
1065
|
if (Array.isArray(state.copies))
|
|
@@ -1597,27 +1075,17 @@ function parseRunCheckpoint(raw) {
|
|
|
1597
1075
|
|| typeof value.state.directionRevision !== 'number'
|
|
1598
1076
|
|| typeof value.revision !== 'number' || typeof value.savedAt !== 'string')
|
|
1599
1077
|
return undefined;
|
|
1600
|
-
// A checkpoint whose conversation is not a conversation is worse than no checkpoint: the
|
|
1601
|
-
// resumed node REPLACES its freshly built system prompt, history and task with this array, so
|
|
1602
|
-
// an empty or malformed one would run the node with no task at all. Anything the current
|
|
1603
|
-
// runtime could not continue reads as absent — the compatibility contract is a fresh node.
|
|
1604
1078
|
if (value.messages.length === 0 || value.messages.some(message => !message || typeof message !== 'object'
|
|
1605
1079
|
|| !CHECKPOINT_MESSAGE_ROLES.has(message.role)
|
|
1606
1080
|
|| typeof message.content !== 'string'))
|
|
1607
1081
|
return undefined;
|
|
1608
|
-
// The continuation counters drive the resumed run's limits (iterations, repairs, todo calls);
|
|
1609
|
-
// a checkpoint missing them is from a build whose state shape this reader cannot trust.
|
|
1610
1082
|
if (![value.state.iterations, value.state.repairs, value.state.todoCalls, value.state.directionRevision]
|
|
1611
1083
|
.every(count => typeof count === 'number' && Number.isFinite(count)))
|
|
1612
1084
|
return undefined;
|
|
1613
|
-
// The model identity stamp is optional: garbage there degrades to "no stamp" (no predecessor-
|
|
1614
|
-
// conversation restore for a pending successor) rather than refusing a sound checkpoint file.
|
|
1615
1085
|
if (typeof value.provider !== 'string')
|
|
1616
1086
|
delete value.provider;
|
|
1617
1087
|
if (typeof value.model !== 'string')
|
|
1618
1088
|
delete value.model;
|
|
1619
|
-
// The delivery marker is optional and self-describing: garbage degrades to "no marker", which
|
|
1620
|
-
// only means the text fallback is used, never that a sound checkpoint is refused.
|
|
1621
1089
|
if (Array.isArray(value.steeringDelivered))
|
|
1622
1090
|
value.steeringDelivered = value.steeringDelivered.filter(id => typeof id === 'string');
|
|
1623
1091
|
else
|
|
@@ -1628,11 +1096,6 @@ function parseRunCheckpoint(raw) {
|
|
|
1628
1096
|
return undefined;
|
|
1629
1097
|
}
|
|
1630
1098
|
}
|
|
1631
|
-
/**
|
|
1632
|
-
* Discard a run's checkpoints — every per-node file and the legacy single file. The terminal
|
|
1633
|
-
* paths call this once the attempt has finished, so durable checkpoints exist only for attempts
|
|
1634
|
-
* a resume can actually pick up again.
|
|
1635
|
-
*/
|
|
1636
1099
|
export async function removeRunCheckpoint(directory, runId) {
|
|
1637
1100
|
const root = runRecordsDirectory(directory);
|
|
1638
1101
|
const prefix = `${runId}.checkpoint`;
|
|
@@ -1649,23 +1112,6 @@ export async function removeRunCheckpoint(directory, runId) {
|
|
|
1649
1112
|
await rm(join(root, name), { force: true }).catch(() => { });
|
|
1650
1113
|
}
|
|
1651
1114
|
}
|
|
1652
|
-
/**
|
|
1653
|
-
* Read a claim where it stands.
|
|
1654
|
-
*
|
|
1655
|
-
* An ordinary read never disturbs the lock path, so a live holder cannot lose its claim merely to
|
|
1656
|
-
* being inspected. The identity comes back with the content — and is deliberately read BEFORE the
|
|
1657
|
-
* content, so a file replaced underneath the read answers with the identity of the newer file
|
|
1658
|
-
* while the caller acts on the older content: a reclaimer re-reads both under its ticket, and that
|
|
1659
|
-
* mismatch is what refuses the reclamation instead of letting it delete a claim it never judged.
|
|
1660
|
-
* An unreadable file proves no live holder (it is stale); the holder's own pid is stale too,
|
|
1661
|
-
* because a crashed process's pid can be recycled to this one.
|
|
1662
|
-
*
|
|
1663
|
-
* A live pid is corroborated against the start time the claim recorded before it counts as a live
|
|
1664
|
-
* holder: a pid the OS handed to an unrelated process that started later cannot be the claim's
|
|
1665
|
-
* author, and treating it as one would refuse that attempt's resume for as long as the impostor
|
|
1666
|
-
* runs. Where the platform cannot report a start time, the pid stands on its own — the conservative
|
|
1667
|
-
* direction, since a live holder treated as stale is what lets two engines resume at once.
|
|
1668
|
-
*/
|
|
1669
1115
|
async function inspectClaim(path) {
|
|
1670
1116
|
let identity;
|
|
1671
1117
|
try {
|
|
@@ -1684,7 +1130,7 @@ async function inspectClaim(path) {
|
|
|
1684
1130
|
if (typeof value.startedAt === 'string')
|
|
1685
1131
|
startedAt = Date.parse(value.startedAt);
|
|
1686
1132
|
}
|
|
1687
|
-
catch {
|
|
1133
|
+
catch { }
|
|
1688
1134
|
let live = pid !== undefined && pid !== process.pid && isPidAlive(pid);
|
|
1689
1135
|
if (live && startedAt !== undefined && Number.isFinite(startedAt)) {
|
|
1690
1136
|
const actual = await processStartedAtFor(pid);
|
|
@@ -1693,24 +1139,6 @@ async function inspectClaim(path) {
|
|
|
1693
1139
|
}
|
|
1694
1140
|
return { ...identity, live };
|
|
1695
1141
|
}
|
|
1696
|
-
/**
|
|
1697
|
-
* Take the exclusive right to reclaim a stale claim, or refuse while another engine holds it.
|
|
1698
|
-
*
|
|
1699
|
-
* Reclaiming means deleting a file, and no filesystem offers a conditional delete: two processes
|
|
1700
|
-
* that both judge the same stale file can both proceed to remove it, and the one that removes
|
|
1701
|
-
* second can remove the claim the other has already created and started resuming under — leaving
|
|
1702
|
-
* two live resumers. The ticket turns the delete into a serialized step (taken by the same
|
|
1703
|
-
* create-exclusive link the claim itself uses), so only one reclaimer at a time may remove
|
|
1704
|
-
* anything, and every removal is additionally gated on re-reading the exact file it judged.
|
|
1705
|
-
*
|
|
1706
|
-
* A ticket left by a reclaimer that died mid-reclaim is taken over the way a stale claim is: its
|
|
1707
|
-
* holder is read where it stands, and only a dead one is discarded. Two processes taking over that
|
|
1708
|
-
* same dead ticket does not reopen the hole: the ticket guards only the delete, and the delete is
|
|
1709
|
-
* still conditional on the identity re-read, so a doubled ticket can at worst make both reclaimers
|
|
1710
|
-
* refuse.
|
|
1711
|
-
*
|
|
1712
|
-
* @returns whether this process holds the ticket, in which case the caller must remove it.
|
|
1713
|
-
*/
|
|
1714
1142
|
async function takeReclaimTicket(ticket) {
|
|
1715
1143
|
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
1716
1144
|
const temp = `${ticket}.tmp-${process.pid}-${randomUUID()}`;
|
|
@@ -1728,48 +1156,16 @@ async function takeReclaimTicket(ticket) {
|
|
|
1728
1156
|
const held = await inspectClaim(ticket);
|
|
1729
1157
|
if (held === undefined)
|
|
1730
1158
|
continue;
|
|
1731
|
-
// Another engine is reclaiming this run right now: refuse rather than removing anything it
|
|
1732
|
-
// may be in the middle of. Its own refusal-or-claim answer arrives for the caller to retry.
|
|
1733
1159
|
if (held.live)
|
|
1734
1160
|
return false;
|
|
1735
1161
|
await rm(ticket, { force: true }).catch(() => { });
|
|
1736
1162
|
}
|
|
1737
1163
|
return false;
|
|
1738
1164
|
}
|
|
1739
|
-
/**
|
|
1740
|
-
* Take (or refuse) the cross-process claim that this run id is being resumed.
|
|
1741
|
-
*
|
|
1742
|
-
* Two engines sharing one home can both be asked to resume the same stopped attempt, and the
|
|
1743
|
-
* per-process start guard cannot see the other one. The claim is a create-exclusive file: the
|
|
1744
|
-
* winner proceeds, and a file left by a process that is gone is stale and gets reclaimed. It is
|
|
1745
|
-
* deliberately a separate name from every record the Runs view scans, and it names the original
|
|
1746
|
-
* attempt — the thing two resumers would collide over.
|
|
1747
|
-
*
|
|
1748
|
-
* Reclamation may not be "read the file, then unlink the path", nor "rename whatever is in the
|
|
1749
|
-
* way, then read it": the first deletes a claim another engine may have created in between, and
|
|
1750
|
-
* the second vacates the canonical path — unlocking a live holder's claim for the rename window,
|
|
1751
|
-
* so a third engine linking into it takes the name while the original holder still believes it
|
|
1752
|
-
* owns it. What survives both is refusing to act on anything but verified evidence: the claim is
|
|
1753
|
-
* read in place (which never disturbs it), the reclamation takes the ticket above, the file is
|
|
1754
|
-
* re-read UNDER the ticket so a claim replaced in between refuses instead of being moved, and the
|
|
1755
|
-
* moved file's identity is checked before it is discarded. A live claim is never vacated, and the
|
|
1756
|
-
* only file ever deleted here is one this process read as stale twice and matched by identity.
|
|
1757
|
-
*
|
|
1758
|
-
* @returns whether the claim was taken; false means a live process holds it, or another engine is
|
|
1759
|
-
* reclaiming it right now.
|
|
1760
|
-
*/
|
|
1761
1165
|
export async function claimRunResume(directory, runId) {
|
|
1762
1166
|
await mkdir(runRecordsDirectory(directory), { recursive: true, mode: 0o700 });
|
|
1763
1167
|
return takeStaleableClaim(runRecordPath(directory, runId, 'claim'));
|
|
1764
1168
|
}
|
|
1765
|
-
/**
|
|
1766
|
-
* The generic cross-process exclusive claim over one path.
|
|
1767
|
-
*
|
|
1768
|
-
* One protocol serves every exclusive claim in this directory: the resume claim above and the
|
|
1769
|
-
* per-session mutation lock below. Create-exclusive first; a file left by a dead process is
|
|
1770
|
-
* reclaimed only under the reclaim ticket and only when the re-read under the ticket is still the
|
|
1771
|
-
* exact file judged stale.
|
|
1772
|
-
*/
|
|
1773
1169
|
async function takeStaleableClaim(path) {
|
|
1774
1170
|
const ticket = `${path}.reclaim`;
|
|
1775
1171
|
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
@@ -1785,7 +1181,6 @@ async function takeStaleableClaim(path) {
|
|
|
1785
1181
|
throw error;
|
|
1786
1182
|
await rm(temp, { force: true }).catch(() => { });
|
|
1787
1183
|
}
|
|
1788
|
-
// A file is in the way. A live holder answers refusal before anything touches the path.
|
|
1789
1184
|
const judged = await inspectClaim(path);
|
|
1790
1185
|
if (judged === undefined)
|
|
1791
1186
|
continue;
|
|
@@ -1794,9 +1189,6 @@ async function takeStaleableClaim(path) {
|
|
|
1794
1189
|
if (!await takeReclaimTicket(ticket))
|
|
1795
1190
|
return false;
|
|
1796
1191
|
try {
|
|
1797
|
-
// Re-read under the ticket: between the judgement above and here another engine may have
|
|
1798
|
-
// reclaimed the claim and started resuming under its own file. Only a file that is still
|
|
1799
|
-
// stale AND still the exact file judged is acted on; anything else is left untouched.
|
|
1800
1192
|
const current = await inspectClaim(path);
|
|
1801
1193
|
if (current === undefined || current.live || current.dev !== judged.dev || current.ino !== judged.ino)
|
|
1802
1194
|
continue;
|
|
@@ -1805,23 +1197,17 @@ async function takeStaleableClaim(path) {
|
|
|
1805
1197
|
await rename(path, aside);
|
|
1806
1198
|
}
|
|
1807
1199
|
catch (error) {
|
|
1808
|
-
// Gone already (a competing reclaim took it): retry the create against the new state.
|
|
1809
1200
|
if (error.code === 'ENOENT')
|
|
1810
1201
|
continue;
|
|
1811
1202
|
throw error;
|
|
1812
1203
|
}
|
|
1813
1204
|
const moved = await inspectClaim(aside);
|
|
1814
1205
|
if (moved === undefined || moved.dev !== judged.dev || moved.ino !== judged.ino) {
|
|
1815
|
-
// A file this process never judged stale — an out-of-protocol writer (an older build
|
|
1816
|
-
// sharing this home) replaced the claim between the re-read and the rename. It is put back
|
|
1817
|
-
// before anything else happens: an unverified file is never deleted. If a third contender
|
|
1818
|
-
// took the name in the meantime the displaced file stays under its aside name, intact,
|
|
1819
|
-
// rather than being destroyed to make room for this attempt.
|
|
1820
1206
|
try {
|
|
1821
1207
|
await link(aside, path);
|
|
1822
1208
|
await rm(aside, { force: true });
|
|
1823
1209
|
}
|
|
1824
|
-
catch {
|
|
1210
|
+
catch { }
|
|
1825
1211
|
return false;
|
|
1826
1212
|
}
|
|
1827
1213
|
await rm(aside, { force: true });
|
|
@@ -1829,17 +1215,9 @@ async function takeStaleableClaim(path) {
|
|
|
1829
1215
|
finally {
|
|
1830
1216
|
await rm(ticket, { force: true }).catch(() => { });
|
|
1831
1217
|
}
|
|
1832
|
-
// The verified stale file is gone. The next pass takes the name with the same create-exclusive
|
|
1833
|
-
// link: a fresh contender may win it, and then its live claim is what refuses this resume.
|
|
1834
1218
|
}
|
|
1835
1219
|
return false;
|
|
1836
1220
|
}
|
|
1837
|
-
/**
|
|
1838
|
-
* Give up a resume claim taken by a start that never reached its run — the claim itself, and the
|
|
1839
|
-
* reclaim ticket this process could be holding. A live reclaimer's ticket is left alone: it is
|
|
1840
|
-
* what serializes that engine's delete, and removing it early would hand the serialization to a
|
|
1841
|
-
* third contender mid-reclaim.
|
|
1842
|
-
*/
|
|
1843
1221
|
export async function releaseRunResumeClaim(directory, runId) {
|
|
1844
1222
|
const path = runRecordPath(directory, runId, 'claim');
|
|
1845
1223
|
await rm(path, { force: true }).catch(() => { });
|
|
@@ -1847,42 +1225,13 @@ export async function releaseRunResumeClaim(directory, runId) {
|
|
|
1847
1225
|
if (held !== undefined && !held.live)
|
|
1848
1226
|
await rm(`${path}.reclaim`, { force: true }).catch(() => { });
|
|
1849
1227
|
}
|
|
1850
|
-
/** Thrown when a session mutation lock is held elsewhere and does not free up within the wait. */
|
|
1851
1228
|
export class SessionMutationLockHeldError extends Error {
|
|
1852
1229
|
constructor(sessionId) {
|
|
1853
1230
|
super(`The session ${sessionId} is being changed by another writer`);
|
|
1854
1231
|
this.name = 'SessionMutationLockHeldError';
|
|
1855
1232
|
}
|
|
1856
1233
|
}
|
|
1857
|
-
/**
|
|
1858
|
-
* The in-process turnstiles, keyed by directory and session.
|
|
1859
|
-
*
|
|
1860
|
-
* A create-exclusive claim serializes processes, not the two awaits inside one process: two
|
|
1861
|
-
* concurrent requests of the same engine would each try to claim, and a holder's claim carries this
|
|
1862
|
-
* very pid, which `inspectClaim` must read as stale (a recycled pid cannot be trusted as ours). So
|
|
1863
|
-
* same-process contenders queue here and only the queue's head ever touches the claim.
|
|
1864
|
-
*/
|
|
1865
1234
|
const mutationTurns = new Map();
|
|
1866
|
-
/**
|
|
1867
|
-
* Serialize a session's cross-process read-modify-write mutation.
|
|
1868
|
-
*
|
|
1869
|
-
* Every rename, archive, and delete reads a session record, edits fields, and saves it back. Two
|
|
1870
|
-
* windows doing that to one session can interleave — each save overwrites the whole record, so one
|
|
1871
|
-
* loses the other's field, and a save landing after a delete recreates the session the user
|
|
1872
|
-
* removed. The answer is one lock per session, held across exactly that window: take it before the
|
|
1873
|
-
* busy check, do the check, the fresh re-read, and the save inside it, and release. A fresh read
|
|
1874
|
-
* under the lock is what makes two serialized mutations additive rather than lossy: each sees what
|
|
1875
|
-
* the last one wrote.
|
|
1876
|
-
*
|
|
1877
|
-
* The lock is the same claim protocol the resume claim uses — create-exclusive, with verified
|
|
1878
|
-
* reclamation of a file left by a dead process — so a holder that dies cannot strand the session:
|
|
1879
|
-
* the next contender reclaims and proceeds. It also carries an in-process turnstile, because the
|
|
1880
|
-
* app-server can have two concurrent requests for one session, and a request must never judge its
|
|
1881
|
-
* own process's live claim stale.
|
|
1882
|
-
*
|
|
1883
|
-
* @returns the release function. Throws `SessionMutationLockHeldError` when another writer keeps
|
|
1884
|
-
* the lock past the wait — the caller owes the user a busy error, not a mutation.
|
|
1885
|
-
*/
|
|
1886
1235
|
export async function acquireSessionMutationLock(directory, sessionId, options = {}) {
|
|
1887
1236
|
const turnKey = `${resolve(directory)}\u0000${sessionId}`;
|
|
1888
1237
|
const previous = mutationTurns.get(turnKey) ?? Promise.resolve();
|
|
@@ -1899,9 +1248,6 @@ export async function acquireSessionMutationLock(directory, sessionId, options =
|
|
|
1899
1248
|
mutationTurns.delete(turnKey);
|
|
1900
1249
|
releaseTurn();
|
|
1901
1250
|
};
|
|
1902
|
-
// A path-unsafe identity — never a real session, which is a UUID — cannot name a claim file. The
|
|
1903
|
-
// turnstile above is the only serialization such an id gets; the mutation it guards is itself
|
|
1904
|
-
// unreachable through the validated paths that construct these ids.
|
|
1905
1251
|
let path;
|
|
1906
1252
|
try {
|
|
1907
1253
|
path = runRecordPath(directory, sessionId, 'mutation-lock');
|
|
@@ -1925,16 +1271,10 @@ export async function acquireSessionMutationLock(directory, sessionId, options =
|
|
|
1925
1271
|
}
|
|
1926
1272
|
}
|
|
1927
1273
|
catch (error) {
|
|
1928
|
-
// Anything thrown on the way to holding the claim — the timeout above, or a transient
|
|
1929
|
-
// filesystem or permission error out of the directory write or the claim attempt — must give
|
|
1930
|
-
// the turnstile back before it propagates. The map would otherwise keep this unresolved turn,
|
|
1931
|
-
// and every later mutation of the session would wait behind a release that never comes: one
|
|
1932
|
-
// transient error would shut the session's rail actions for the process lifetime.
|
|
1933
1274
|
finish();
|
|
1934
1275
|
throw error;
|
|
1935
1276
|
}
|
|
1936
1277
|
}
|
|
1937
|
-
/** Remove the claim at `claimPath` and its dead reclaim ticket. False when the filesystem refused the unlink. */
|
|
1938
1278
|
const removeClaim = async (claimPath) => {
|
|
1939
1279
|
try {
|
|
1940
1280
|
await rm(claimPath, { force: true });
|
|
@@ -1942,8 +1282,6 @@ export async function acquireSessionMutationLock(directory, sessionId, options =
|
|
|
1942
1282
|
catch {
|
|
1943
1283
|
return false;
|
|
1944
1284
|
}
|
|
1945
|
-
// A reclaim ticket left by a holder that died would keep refusing reclamation for whoever
|
|
1946
|
-
// reads it next; drop one only when its holder is provably gone, as the resume release does.
|
|
1947
1285
|
const held = await inspectClaim(`${claimPath}.reclaim`).catch(() => undefined);
|
|
1948
1286
|
if (held !== undefined && !held.live)
|
|
1949
1287
|
await rm(`${claimPath}.reclaim`, { force: true }).catch(() => { });
|
|
@@ -1952,11 +1290,6 @@ export async function acquireSessionMutationLock(directory, sessionId, options =
|
|
|
1952
1290
|
return async () => {
|
|
1953
1291
|
const claimPath = path;
|
|
1954
1292
|
if (claimPath !== undefined && claimHeld) {
|
|
1955
|
-
// A release whose unlink is refused must not resolve as though the lock were gone: the claim
|
|
1956
|
-
// file names this live pid, so every other process keeps waiting out a mutation that is over.
|
|
1957
|
-
// Short retries first — a scanner or indexer holding the file is a matter of moments — and if
|
|
1958
|
-
// the filesystem keeps refusing, the in-process turn comes back at once while a detached pass
|
|
1959
|
-
// keeps removing the claim until it is gone. The writer is done; only the file owes anyone.
|
|
1960
1293
|
let removed = await removeClaim(claimPath);
|
|
1961
1294
|
for (const wait of [50, 100, 200]) {
|
|
1962
1295
|
if (removed)
|
|
@@ -1976,12 +1309,6 @@ export async function acquireSessionMutationLock(directory, sessionId, options =
|
|
|
1976
1309
|
finish();
|
|
1977
1310
|
};
|
|
1978
1311
|
}
|
|
1979
|
-
/**
|
|
1980
|
-
* Clear the recovery marker once a record's `interrupted` finish has been queued.
|
|
1981
|
-
*
|
|
1982
|
-
* Called by the reporter rather than the sweep: the sweep must not claim the work is done, because
|
|
1983
|
-
* the finish it returns may never be enqueued if this process dies first.
|
|
1984
|
-
*/
|
|
1985
1312
|
export async function clearInterruptedTelemetry(directory, runIds) {
|
|
1986
1313
|
for (const runId of runIds) {
|
|
1987
1314
|
const path = runRecordPath(directory, runId, 'json');
|
|
@@ -1992,6 +1319,6 @@ export async function clearInterruptedTelemetry(directory, runIds) {
|
|
|
1992
1319
|
delete value.telemetryPending;
|
|
1993
1320
|
await writeSessionSnapshot(path, JSON.stringify(value, null, 2) + '\n');
|
|
1994
1321
|
}
|
|
1995
|
-
catch {
|
|
1322
|
+
catch { }
|
|
1996
1323
|
}
|
|
1997
1324
|
}
|