pi-better-server 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -1
- package/scripts/copy-pi-test-providers.mjs +10 -0
- package/scripts/event-publication/README.md +26 -0
- package/scripts/event-publication/commit.mjs +21 -0
- package/scripts/event-publication/measure.mjs +87 -0
- package/scripts/event-publication/writer.mjs +6 -0
- package/scripts/measure-full-runtime/frontend.ts +24 -0
- package/scripts/measure-full-runtime/hosted-extension.ts +28 -0
- package/scripts/measure-full-runtime/probe.mjs +29 -0
- package/scripts/measure-full-runtime/recovery-trace-loader.mjs +51 -0
- package/scripts/measure-full-runtime/recovery-trace.mjs +16 -0
- package/scripts/measure-full-runtime.mjs +146 -0
- package/scripts/measure-shared-reads/probe.mjs +26 -2
- package/scripts/measure-shared-reads.mjs +44 -9
- package/scripts/private-runtime/package-lock.json +66 -4
- package/scripts/private-runtime/package.json +10 -2
- package/scripts/source-provenance.mjs +31 -0
- package/scripts/test-on-pi.sh +3 -9
- package/src/handshake.ts +1 -1
- package/src/hosted-session.ts +32 -4
- package/src/maintenance.ts +35 -7
- package/src/registry.ts +28 -4
- package/src/runtime/attempt-invalidation.ts +54 -0
- package/src/runtime/background-history.ts +151 -45
- package/src/runtime/background-policy.ts +27 -0
- package/src/runtime/background-tools.ts +389 -0
- package/src/runtime/background.ts +410 -48
- package/src/runtime/cancellation.ts +28 -7
- package/src/runtime/catalog-sources.ts +199 -0
- package/src/runtime/catalog.ts +53 -21
- package/src/runtime/client.ts +22 -2
- package/src/runtime/domain/assignment-inbox.mjs +119 -0
- package/src/runtime/domain/assignment-observation.d.mts +1 -1
- package/src/runtime/domain/assignment-observation.mjs +71 -33
- package/src/runtime/domain/background/process-identity.mjs +20 -2
- package/src/runtime/domain/background/process.mjs +3 -3
- package/src/runtime/domain/background/registry.mjs +89 -4
- package/src/runtime/domain/background/types.mjs +1 -0
- package/src/runtime/domain/background-service/adapter-contract.domain.mjs +83 -0
- package/src/runtime/domain/background-service/adapter-contract.ts +70 -0
- package/src/runtime/domain/background-service/batch.domain.mjs +91 -0
- package/src/runtime/domain/background-service/batch.ts +111 -0
- package/src/runtime/domain/background-service/conditions.domain.mjs +94 -0
- package/src/runtime/domain/background-service/conditions.ts +99 -0
- package/src/runtime/domain/background-service/delivery-observer.domain.mjs +37 -0
- package/src/runtime/domain/background-service/delivery-observer.ts +42 -0
- package/src/runtime/domain/background-service/failures.domain.mjs +209 -0
- package/src/runtime/domain/background-service/failures.ts +212 -0
- package/src/runtime/domain/background-service/goal-provider.domain.mjs +193 -0
- package/src/runtime/domain/background-service/goal-provider.ts +209 -0
- package/src/runtime/domain/background-service/index.domain.mjs +54 -0
- package/src/runtime/domain/background-service/index.ts +55 -0
- package/src/runtime/domain/background-service/logs.domain.mjs +129 -0
- package/src/runtime/domain/background-service/logs.ts +134 -0
- package/src/runtime/domain/background-service/maintenance.domain.mjs +95 -0
- package/src/runtime/domain/background-service/maintenance.ts +98 -0
- package/src/runtime/domain/background-service/managed-authority.domain.mjs +32 -0
- package/src/runtime/domain/background-service/managed-authority.ts +30 -0
- package/src/runtime/domain/background-service/managed-client.domain.mjs +79 -0
- package/src/runtime/domain/background-service/managed-client.ts +73 -0
- package/src/runtime/domain/background-service/managed-frontend.domain.mjs +225 -0
- package/src/runtime/domain/background-service/managed-frontend.ts +176 -0
- package/src/runtime/domain/background-service/navigator-provider.domain.mjs +492 -0
- package/src/runtime/domain/background-service/navigator-provider.ts +485 -0
- package/src/runtime/domain/background-service/observed-wait-client.domain.mjs +6 -0
- package/src/runtime/domain/background-service/observed-wait-client.ts +10 -0
- package/src/runtime/domain/background-service/observed-waits.domain.mjs +4080 -0
- package/src/runtime/domain/background-service/observed-waits.ts +4513 -0
- package/src/runtime/domain/background-service/output.domain.mjs +867 -0
- package/src/runtime/domain/background-service/output.ts +952 -0
- package/src/runtime/domain/background-service/process-identity.domain.mjs +129 -0
- package/src/runtime/domain/background-service/process-identity.ts +134 -0
- package/src/runtime/domain/background-service/process.domain.mjs +512 -0
- package/src/runtime/domain/background-service/process.ts +532 -0
- package/src/runtime/domain/background-service/prompt.domain.mjs +58 -0
- package/src/runtime/domain/background-service/prompt.ts +72 -0
- package/src/runtime/domain/background-service/registry.domain.mjs +460 -0
- package/src/runtime/domain/background-service/registry.ts +450 -0
- package/src/runtime/domain/background-service/remote-task-preset.domain.mjs +67 -0
- package/src/runtime/domain/background-service/remote-task-preset.ts +127 -0
- package/src/runtime/domain/background-service/runtime.domain.mjs +1511 -0
- package/src/runtime/domain/background-service/runtime.ts +1636 -0
- package/src/runtime/domain/background-service/sandbox.domain.mjs +735 -0
- package/src/runtime/domain/background-service/sandbox.ts +913 -0
- package/src/runtime/domain/background-service/shared-callback-batcher.domain.mjs +777 -0
- package/src/runtime/domain/background-service/shared-callback-batcher.ts +956 -0
- package/src/runtime/domain/background-service/shared-execution-policy.domain.mjs +50 -0
- package/src/runtime/domain/background-service/shared-execution-policy.ts +61 -0
- package/src/runtime/domain/background-service/shared-failure-observations.domain.mjs +1078 -0
- package/src/runtime/domain/background-service/shared-failure-observations.ts +1166 -0
- package/src/runtime/domain/background-service/shared-harness-settings.domain.mjs +126 -0
- package/src/runtime/domain/background-service/shared-harness-settings.ts +116 -0
- package/src/runtime/domain/background-service/shared-log-utils.domain.mjs +1223 -0
- package/src/runtime/domain/background-service/shared-log-utils.ts +1492 -0
- package/src/runtime/domain/background-service/shared-navigator-panels.domain.mjs +289 -0
- package/src/runtime/domain/background-service/shared-navigator-panels.ts +272 -0
- package/src/runtime/domain/background-service/shared-navigator.domain.mjs +1930 -0
- package/src/runtime/domain/background-service/shared-navigator.ts +1962 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-inbox.mjs +144 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-observation.mjs +189 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/index.domain.mjs +51 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/index.ts +122 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.ts +73 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +163 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.ts +174 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-parent-callbacks.domain.mjs +66 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-parent-callbacks.ts +68 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.domain.mjs +87 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.ts +90 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.domain.mjs +97 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.ts +139 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/errors.domain.mjs +12 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/errors.ts +79 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.domain.mjs +342 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.ts +513 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.domain.mjs +283 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.ts +458 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/recovery.domain.mjs +52 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/recovery.ts +55 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.domain.mjs +114 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.ts +140 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.domain.mjs +280 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.ts +442 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.domain.mjs +420 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.ts +441 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/sqlite.domain.mjs +194 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/sqlite.ts +239 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +275 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +585 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.domain.mjs +120 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.ts +142 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-cancellation.domain.mjs +188 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-cancellation.ts +224 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.domain.mjs +25 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.ts +707 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/upgrade.domain.mjs +22 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/upgrade.ts +22 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/validate.domain.mjs +234 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/validate.ts +241 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.domain.mjs +851 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.ts +1424 -0
- package/src/runtime/domain/background-service/shared-permission-blocker.domain.mjs +40 -0
- package/src/runtime/domain/background-service/shared-permission-blocker.ts +48 -0
- package/src/runtime/domain/background-service/shared-render-scheduler.domain.mjs +41 -0
- package/src/runtime/domain/background-service/shared-render-scheduler.ts +47 -0
- package/src/runtime/domain/background-service/shared-sandbox-core.domain.mjs +1531 -0
- package/src/runtime/domain/background-service/shared-sandbox-core.ts +1757 -0
- package/src/runtime/domain/background-service/shared-sandbox-diagnostics.domain.mjs +591 -0
- package/src/runtime/domain/background-service/shared-sandbox-diagnostics.ts +491 -0
- package/src/runtime/domain/background-service/shared-ssh-core/index.domain.mjs +698 -0
- package/src/runtime/domain/background-service/shared-ssh-core/index.ts +979 -0
- package/src/runtime/domain/background-service/shared-stall-detector.domain.mjs +37 -0
- package/src/runtime/domain/background-service/shared-stall-detector.ts +69 -0
- package/src/runtime/domain/background-service/shared-work-client.mjs +303 -0
- package/src/runtime/domain/background-service/stall-attention.domain.mjs +29 -0
- package/src/runtime/domain/background-service/stall-attention.ts +31 -0
- package/src/runtime/domain/background-service/stall.domain.mjs +57 -0
- package/src/runtime/domain/background-service/stall.ts +57 -0
- package/src/runtime/domain/background-service/supervisor/README.md +111 -0
- package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +90 -0
- package/src/runtime/domain/background-service/supervisor/generated/conditions.mjs +96 -0
- package/src/runtime/domain/background-service/supervisor/generated/identity.mjs +95 -0
- package/src/runtime/domain/background-service/supervisor/generated/process-identity.mjs +131 -0
- package/src/runtime/domain/background-service/supervisor/generated/process.mjs +514 -0
- package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +872 -0
- package/src/runtime/domain/background-service/supervisor/generated/store.mjs +609 -0
- package/src/runtime/domain/background-service/supervisor/generated/types.mjs +50 -0
- package/src/runtime/domain/background-service/supervisor/identity.domain.mjs +93 -0
- package/src/runtime/domain/background-service/supervisor/identity.ts +99 -0
- package/src/runtime/domain/background-service/supervisor/index.domain.mjs +22 -0
- package/src/runtime/domain/background-service/supervisor/index.ts +37 -0
- package/src/runtime/domain/background-service/supervisor/invocation.domain.mjs +30 -0
- package/src/runtime/domain/background-service/supervisor/invocation.ts +44 -0
- package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +870 -0
- package/src/runtime/domain/background-service/supervisor/runner.ts +982 -0
- package/src/runtime/domain/background-service/supervisor/store.domain.mjs +605 -0
- package/src/runtime/domain/background-service/supervisor/store.ts +651 -0
- package/src/runtime/domain/background-service/supervisor/supervisor.domain.mjs +721 -0
- package/src/runtime/domain/background-service/supervisor/supervisor.ts +804 -0
- package/src/runtime/domain/background-service/supervisor/types.domain.mjs +48 -0
- package/src/runtime/domain/background-service/supervisor/types.ts +394 -0
- package/src/runtime/domain/background-service/task-diagnostics.domain.mjs +60 -0
- package/src/runtime/domain/background-service/task-diagnostics.ts +71 -0
- package/src/runtime/domain/background-service/tools.domain.mjs +1050 -0
- package/src/runtime/domain/background-service/tools.ts +1142 -0
- package/src/runtime/domain/background-service/types.domain.mjs +5 -0
- package/src/runtime/domain/background-service/types.ts +236 -0
- package/src/runtime/domain/background-service/work-observation.domain.mjs +176 -0
- package/src/runtime/domain/background-service/work-observation.ts +144 -0
- package/src/runtime/domain/catalog/catalog-store.d.mts +9 -1
- package/src/runtime/domain/catalog/catalog-store.mjs +20 -22
- package/src/runtime/domain/coordinator/src/assignment-changes.mjs +75 -0
- package/src/runtime/domain/coordinator/src/assignment-outcomes.mjs +2 -0
- package/src/runtime/domain/coordinator/src/background-parent-callbacks.mjs +65 -0
- package/src/runtime/domain/coordinator/src/background-requests.mjs +86 -0
- package/src/runtime/domain/coordinator/src/discovery.mjs +4 -2
- package/src/runtime/domain/coordinator/src/operations.mjs +44 -4
- package/src/runtime/domain/coordinator/src/result-supervision.mjs +12 -0
- package/src/runtime/domain/coordinator/src/rows.mjs +2 -0
- package/src/runtime/domain/coordinator/src/schema.mjs +73 -21
- package/src/runtime/domain/coordinator/src/store.mjs +32 -3
- package/src/runtime/domain/coordinator/src/subagent-launch.mjs +119 -0
- package/src/runtime/domain/coordinator/src/types.mjs +1 -1
- package/src/runtime/domain/coordinator/src/upgrade.mjs +22 -0
- package/src/runtime/domain/health-observation.d.mts +3 -1
- package/src/runtime/domain/health-observation.mjs +189 -190
- package/src/runtime/domain/log-cursor.d.mts +2 -0
- package/src/runtime/domain/log-cursor.mjs +85 -16
- package/src/runtime/domain/parse.d.mts +4 -1
- package/src/runtime/domain/parse.mjs +50 -5
- package/src/runtime/domain/sandbox-policy/README.md +633 -0
- package/src/runtime/domain/sandbox-policy/background-admission.domain.mjs +83 -0
- package/src/runtime/domain/sandbox-policy/background-admission.ts +75 -0
- package/src/runtime/domain/sandbox-policy/commands.domain.mjs +245 -0
- package/src/runtime/domain/sandbox-policy/commands.ts +336 -0
- package/src/runtime/domain/sandbox-policy/deny-rules.domain.mjs +440 -0
- package/src/runtime/domain/sandbox-policy/deny-rules.ts +620 -0
- package/src/runtime/domain/sandbox-policy/diagnostics-command.domain.mjs +30 -0
- package/src/runtime/domain/sandbox-policy/diagnostics-command.ts +27 -0
- package/src/runtime/domain/sandbox-policy/events.domain.mjs +49 -0
- package/src/runtime/domain/sandbox-policy/events.ts +85 -0
- package/src/runtime/domain/sandbox-policy/files.domain.mjs +153 -0
- package/src/runtime/domain/sandbox-policy/files.ts +224 -0
- package/src/runtime/domain/sandbox-policy/index.domain.mjs +289 -0
- package/src/runtime/domain/sandbox-policy/index.ts +386 -0
- package/src/runtime/domain/sandbox-policy/package.json +66 -0
- package/src/runtime/domain/sandbox-policy/permission-settings.domain.mjs +36 -0
- package/src/runtime/domain/sandbox-policy/permission-settings.ts +46 -0
- package/src/runtime/domain/sandbox-policy/permissions-page.domain.mjs +309 -0
- package/src/runtime/domain/sandbox-policy/permissions-page.ts +295 -0
- package/src/runtime/domain/sandbox-policy/permissions.domain.mjs +98 -0
- package/src/runtime/domain/sandbox-policy/permissions.ts +125 -0
- package/src/runtime/domain/sandbox-policy/policy.domain.mjs +80 -0
- package/src/runtime/domain/sandbox-policy/policy.ts +102 -0
- package/src/runtime/domain/sandbox-policy/preferences.domain.mjs +55 -0
- package/src/runtime/domain/sandbox-policy/preferences.ts +82 -0
- package/src/runtime/domain/sandbox-policy/rules-page.domain.mjs +138 -0
- package/src/runtime/domain/sandbox-policy/rules-page.ts +176 -0
- package/src/runtime/domain/sandbox-policy/session-permissions.domain.mjs +22 -0
- package/src/runtime/domain/sandbox-policy/session-permissions.ts +24 -0
- package/src/runtime/domain/sandbox-policy/shared-harness-settings.domain.mjs +126 -0
- package/src/runtime/domain/sandbox-policy/shared-harness-settings.ts +116 -0
- package/src/runtime/domain/sandbox-policy/shared-permission-blocker.domain.mjs +40 -0
- package/src/runtime/domain/sandbox-policy/shared-permission-blocker.ts +48 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-core.domain.mjs +1531 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-core.ts +1757 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.domain.mjs +591 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.ts +491 -0
- package/src/runtime/domain/sandbox-policy/shared-task-application.domain.mjs +185 -0
- package/src/runtime/domain/sandbox-policy/shared-task-application.ts +185 -0
- package/src/runtime/domain/sandbox-policy/shared-task-apply-patch.domain.mjs +416 -0
- package/src/runtime/domain/sandbox-policy/shared-task-apply-patch.ts +409 -0
- package/src/runtime/domain/sandbox-policy/shared-task-exec.domain.mjs +325 -0
- package/src/runtime/domain/sandbox-policy/shared-task-exec.ts +335 -0
- package/src/runtime/domain/sandbox-policy/shared-task-files.domain.mjs +346 -0
- package/src/runtime/domain/sandbox-policy/shared-task-files.ts +342 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-identity.domain.mjs +140 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-identity.ts +126 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-list.domain.mjs +220 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-list.ts +209 -0
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.domain.mjs +227 -0
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.ts +224 -0
- package/src/runtime/domain/sandbox-policy/shared-task-tools.domain.mjs +92 -0
- package/src/runtime/domain/sandbox-policy/shared-task-tools.ts +123 -0
- package/src/runtime/domain/sandbox-policy/shell.domain.mjs +93 -0
- package/src/runtime/domain/sandbox-policy/shell.ts +129 -0
- package/src/runtime/domain/sandbox-policy/state.domain.mjs +307 -0
- package/src/runtime/domain/sandbox-policy/state.ts +408 -0
- package/src/runtime/domain/sandbox-policy/status.domain.mjs +64 -0
- package/src/runtime/domain/sandbox-policy/status.ts +76 -0
- package/src/runtime/domain/sandbox-policy/subagent-admission.domain.mjs +69 -0
- package/src/runtime/domain/sandbox-policy/subagent-admission.ts +60 -0
- package/src/runtime/domain/subagents/CONTEXT.md +202 -0
- package/src/runtime/domain/subagents/PRODUCT.md +49 -0
- package/src/runtime/domain/subagents/README.md +201 -0
- package/src/runtime/domain/subagents/agent-catalog.mjs +64 -0
- package/src/runtime/domain/subagents/agent-commands.domain.mjs +822 -0
- package/src/runtime/domain/subagents/agent-commands.ts +905 -0
- package/src/runtime/domain/subagents/agent-inspection.domain.mjs +398 -0
- package/src/runtime/domain/subagents/agent-inspection.ts +574 -0
- package/src/runtime/domain/subagents/agent-operations.domain.mjs +40 -0
- package/src/runtime/domain/subagents/agent-operations.ts +61 -0
- package/src/runtime/domain/subagents/agent-session-settings.domain.mjs +112 -0
- package/src/runtime/domain/subagents/agent-session-settings.ts +118 -0
- package/src/runtime/domain/subagents/agents-catalog-tool.domain.mjs +84 -0
- package/src/runtime/domain/subagents/agents-catalog-tool.ts +118 -0
- package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +276 -0
- package/src/runtime/domain/subagents/assignment-outcomes.ts +212 -0
- package/src/runtime/domain/subagents/assignment-projection.mjs +261 -0
- package/src/runtime/domain/subagents/assignment-report.domain.mjs +174 -0
- package/src/runtime/domain/subagents/assignment-report.ts +156 -0
- package/src/runtime/domain/subagents/batch.mjs +275 -0
- package/src/runtime/domain/subagents/callback-fields.domain.mjs +72 -0
- package/src/runtime/domain/subagents/callback-fields.ts +95 -0
- package/src/runtime/domain/subagents/capacity.mjs +108 -0
- package/src/runtime/domain/subagents/catalog-editor.domain.mjs +98 -0
- package/src/runtime/domain/subagents/catalog-editor.ts +125 -0
- package/src/runtime/domain/subagents/catalog-host-admission.domain.mjs +104 -0
- package/src/runtime/domain/subagents/catalog-host-admission.ts +85 -0
- package/src/runtime/domain/subagents/catalog-identity.domain.mjs +222 -0
- package/src/runtime/domain/subagents/catalog-identity.ts +258 -0
- package/src/runtime/domain/subagents/catalog-overlay.domain.mjs +9 -0
- package/src/runtime/domain/subagents/catalog-overlay.ts +10 -0
- package/src/runtime/domain/subagents/catalog-preferences.domain.mjs +164 -0
- package/src/runtime/domain/subagents/catalog-preferences.ts +139 -0
- package/src/runtime/domain/subagents/catalog-resolver.domain.mjs +366 -0
- package/src/runtime/domain/subagents/catalog-resolver.ts +540 -0
- package/src/runtime/domain/subagents/catalog-runtime.domain.mjs +570 -0
- package/src/runtime/domain/subagents/catalog-runtime.ts +680 -0
- package/src/runtime/domain/subagents/catalog-schema.domain.mjs +1331 -0
- package/src/runtime/domain/subagents/catalog-schema.ts +1563 -0
- package/src/runtime/domain/subagents/catalog-skill-edits.domain.mjs +95 -0
- package/src/runtime/domain/subagents/catalog-skill-edits.ts +116 -0
- package/src/runtime/domain/subagents/catalog-store.domain.mjs +808 -0
- package/src/runtime/domain/subagents/catalog-store.ts +1006 -0
- package/src/runtime/domain/subagents/catalog-ui.domain.mjs +651 -0
- package/src/runtime/domain/subagents/catalog-ui.ts +611 -0
- package/src/runtime/domain/subagents/child-apply-patch.domain.mjs +13 -0
- package/src/runtime/domain/subagents/child-apply-patch.ts +13 -0
- package/src/runtime/domain/subagents/child-codemode.domain.mjs +16 -0
- package/src/runtime/domain/subagents/child-codemode.ts +17 -0
- package/src/runtime/domain/subagents/child-dependencies.mjs +193 -0
- package/src/runtime/domain/subagents/child-incidents.domain.mjs +170 -0
- package/src/runtime/domain/subagents/child-incidents.ts +169 -0
- package/src/runtime/domain/subagents/child-operations.domain.mjs +986 -0
- package/src/runtime/domain/subagents/child-operations.ts +1191 -0
- package/src/runtime/domain/subagents/child-ownership.domain.mjs +205 -0
- package/src/runtime/domain/subagents/child-ownership.ts +160 -0
- package/src/runtime/domain/subagents/child-preflight.mjs +38 -0
- package/src/runtime/domain/subagents/child-process-list.domain.mjs +7 -0
- package/src/runtime/domain/subagents/child-process-list.ts +8 -0
- package/src/runtime/domain/subagents/child-runtime.mjs +67 -0
- package/src/runtime/domain/subagents/child-steer.domain.mjs +109 -0
- package/src/runtime/domain/subagents/child-steer.ts +102 -0
- package/src/runtime/domain/subagents/cleanup.domain.mjs +818 -0
- package/src/runtime/domain/subagents/cleanup.ts +1017 -0
- package/src/runtime/domain/subagents/codex-import.domain.mjs +575 -0
- package/src/runtime/domain/subagents/codex-import.ts +717 -0
- package/src/runtime/domain/subagents/completion.domain.mjs +6 -0
- package/src/runtime/domain/subagents/completion.mjs +152 -0
- package/src/runtime/domain/subagents/completion.ts +11 -0
- package/src/runtime/domain/subagents/config.domain.mjs +248 -0
- package/src/runtime/domain/subagents/config.json +15 -0
- package/src/runtime/domain/subagents/config.ts +328 -0
- package/src/runtime/domain/subagents/continuation-authority.domain.mjs +38 -0
- package/src/runtime/domain/subagents/continuation-authority.ts +31 -0
- package/src/runtime/domain/subagents/continuation-runtime.domain.mjs +89 -0
- package/src/runtime/domain/subagents/continuation-runtime.ts +88 -0
- package/src/runtime/domain/subagents/continuation.domain.mjs +942 -0
- package/src/runtime/domain/subagents/continuation.ts +1051 -0
- package/src/runtime/domain/subagents/delegation.domain.mjs +19 -0
- package/src/runtime/domain/subagents/delegation.ts +24 -0
- package/src/runtime/domain/subagents/effective-settings.domain.mjs +130 -0
- package/src/runtime/domain/subagents/effective-settings.ts +184 -0
- package/src/runtime/domain/subagents/extensions.domain.mjs +6 -0
- package/src/runtime/domain/subagents/extensions.mjs +164 -0
- package/src/runtime/domain/subagents/extensions.ts +19 -0
- package/src/runtime/domain/subagents/failures.domain.mjs +362 -0
- package/src/runtime/domain/subagents/failures.ts +319 -0
- package/src/runtime/domain/subagents/finalization.domain.mjs +168 -0
- package/src/runtime/domain/subagents/finalization.ts +218 -0
- package/src/runtime/domain/subagents/git-remotes.domain.mjs +351 -0
- package/src/runtime/domain/subagents/git-remotes.ts +413 -0
- package/src/runtime/domain/subagents/git-workspace.domain.mjs +337 -0
- package/src/runtime/domain/subagents/git-workspace.ts +430 -0
- package/src/runtime/domain/subagents/goal-provider.domain.mjs +125 -0
- package/src/runtime/domain/subagents/goal-provider.ts +132 -0
- package/src/runtime/domain/subagents/health-observation.domain.mjs +617 -0
- package/src/runtime/domain/subagents/health-observation.ts +818 -0
- package/src/runtime/domain/subagents/health-surface.mjs +284 -0
- package/src/runtime/domain/subagents/health.domain.mjs +261 -0
- package/src/runtime/domain/subagents/health.ts +343 -0
- package/src/runtime/domain/subagents/historical-attention.domain.mjs +37 -0
- package/src/runtime/domain/subagents/historical-attention.ts +35 -0
- package/src/runtime/domain/subagents/incident-model.domain.mjs +322 -0
- package/src/runtime/domain/subagents/incident-model.ts +358 -0
- package/src/runtime/domain/subagents/index.domain.mjs +4731 -0
- package/src/runtime/domain/subagents/index.ts +4781 -0
- package/src/runtime/domain/subagents/launch-abort.domain.mjs +13 -0
- package/src/runtime/domain/subagents/launch-abort.ts +21 -0
- package/src/runtime/domain/subagents/launch-context.domain.mjs +24 -0
- package/src/runtime/domain/subagents/launch-context.ts +23 -0
- package/src/runtime/domain/subagents/launch-digest.mjs +6 -0
- package/src/runtime/domain/subagents/lifecycle.domain.mjs +265 -0
- package/src/runtime/domain/subagents/lifecycle.ts +360 -0
- package/src/runtime/domain/subagents/list.domain.mjs +2 -0
- package/src/runtime/domain/subagents/list.mjs +152 -0
- package/src/runtime/domain/subagents/list.ts +19 -0
- package/src/runtime/domain/subagents/log-cursor.domain.mjs +217 -0
- package/src/runtime/domain/subagents/log-cursor.ts +268 -0
- package/src/runtime/domain/subagents/managed-background-capability.domain.mjs +10 -0
- package/src/runtime/domain/subagents/managed-background-capability.ts +9 -0
- package/src/runtime/domain/subagents/managed-failure-worker.mjs +36 -0
- package/src/runtime/domain/subagents/managed-launch-bootstrap.mjs +48 -0
- package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +75 -0
- package/src/runtime/domain/subagents/managed-launch-client.ts +59 -0
- package/src/runtime/domain/subagents/managed-launch-host.domain.mjs +627 -0
- package/src/runtime/domain/subagents/managed-launch-host.ts +504 -0
- package/src/runtime/domain/subagents/managed-launch-journal.mjs +17 -0
- package/src/runtime/domain/subagents/managed-launch-ownership.domain.mjs +29 -0
- package/src/runtime/domain/subagents/managed-launch-ownership.ts +22 -0
- package/src/runtime/domain/subagents/managed-launch-port.domain.mjs +2 -0
- package/src/runtime/domain/subagents/managed-launch-port.ts +25 -0
- package/src/runtime/domain/subagents/managed-launch-recovery.domain.mjs +29 -0
- package/src/runtime/domain/subagents/managed-launch-recovery.ts +45 -0
- package/src/runtime/domain/subagents/managed-worker-bridge.domain.mjs +63 -0
- package/src/runtime/domain/subagents/managed-worker-bridge.ts +48 -0
- package/src/runtime/domain/subagents/managed-worker.mjs +13 -0
- package/src/runtime/domain/subagents/model-resolution-core.domain.mjs +822 -0
- package/src/runtime/domain/subagents/model-resolution-core.ts +1074 -0
- package/src/runtime/domain/subagents/model-resolution.domain.mjs +22 -0
- package/src/runtime/domain/subagents/model-resolution.ts +32 -0
- package/src/runtime/domain/subagents/navigator.domain.mjs +6 -0
- package/src/runtime/domain/subagents/navigator.mjs +1233 -0
- package/src/runtime/domain/subagents/navigator.ts +39 -0
- package/src/runtime/domain/subagents/operation-bridge-types.domain.mjs +1012 -0
- package/src/runtime/domain/subagents/operation-bridge-types.ts +1156 -0
- package/src/runtime/domain/subagents/outage-observations.domain.mjs +84 -0
- package/src/runtime/domain/subagents/outage-observations.ts +73 -0
- package/src/runtime/domain/subagents/output-payload.domain.mjs +767 -0
- package/src/runtime/domain/subagents/output-payload.ts +987 -0
- package/src/runtime/domain/subagents/owned-exit.domain.mjs +12 -0
- package/src/runtime/domain/subagents/owned-exit.ts +12 -0
- package/src/runtime/domain/subagents/package.json +86 -0
- package/src/runtime/domain/subagents/park-checkpoint.domain.mjs +55 -0
- package/src/runtime/domain/subagents/park-checkpoint.ts +61 -0
- package/src/runtime/domain/subagents/parse.domain.mjs +1218 -0
- package/src/runtime/domain/subagents/parse.ts +1451 -0
- package/src/runtime/domain/subagents/permission-policy.domain.mjs +232 -0
- package/src/runtime/domain/subagents/permission-policy.ts +291 -0
- package/src/runtime/domain/subagents/process-identity.mjs +33 -0
- package/src/runtime/domain/subagents/registry.domain.mjs +1584 -0
- package/src/runtime/domain/subagents/registry.ts +1904 -0
- package/src/runtime/domain/subagents/role-assignment.domain.mjs +104 -0
- package/src/runtime/domain/subagents/role-assignment.ts +141 -0
- package/src/runtime/domain/subagents/roles/role.architect.md +13 -0
- package/src/runtime/domain/subagents/roles/role.developer.md +16 -0
- package/src/runtime/domain/subagents/roles/role.explorer.md +13 -0
- package/src/runtime/domain/subagents/roles/role.product-manager.md +13 -0
- package/src/runtime/domain/subagents/roles/role.researcher.md +13 -0
- package/src/runtime/domain/subagents/roles/role.reviewer.md +13 -0
- package/src/runtime/domain/subagents/roles/role.writer.md +12 -0
- package/src/runtime/domain/subagents/sandbox.domain.mjs +64 -0
- package/src/runtime/domain/subagents/sandbox.ts +92 -0
- package/src/runtime/domain/subagents/session-selection.domain.mjs +32 -0
- package/src/runtime/domain/subagents/session-selection.ts +35 -0
- package/src/runtime/domain/subagents/session-store.domain.mjs +489 -0
- package/src/runtime/domain/subagents/session-store.ts +468 -0
- package/src/runtime/domain/subagents/settings-page.domain.mjs +313 -0
- package/src/runtime/domain/subagents/settings-page.ts +363 -0
- package/src/runtime/domain/subagents/shared-callback-batcher.domain.mjs +777 -0
- package/src/runtime/domain/subagents/shared-callback-batcher.ts +956 -0
- package/src/runtime/domain/subagents/shared-catalog.domain.mjs +70 -0
- package/src/runtime/domain/subagents/shared-catalog.ts +61 -0
- package/src/runtime/domain/subagents/shared-execution-policy.domain.mjs +50 -0
- package/src/runtime/domain/subagents/shared-execution-policy.ts +61 -0
- package/src/runtime/domain/subagents/shared-failure-observations.domain.mjs +1078 -0
- package/src/runtime/domain/subagents/shared-failure-observations.ts +1166 -0
- package/src/runtime/domain/subagents/shared-harness-settings.domain.mjs +126 -0
- package/src/runtime/domain/subagents/shared-harness-settings.ts +116 -0
- package/src/runtime/domain/subagents/shared-log-utils.domain.mjs +1223 -0
- package/src/runtime/domain/subagents/shared-log-utils.ts +1492 -0
- package/src/runtime/domain/subagents/shared-navigator-panels.domain.mjs +289 -0
- package/src/runtime/domain/subagents/shared-navigator-panels.ts +272 -0
- package/src/runtime/domain/subagents/shared-navigator.domain.mjs +1930 -0
- package/src/runtime/domain/subagents/shared-navigator.ts +1962 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-inbox.mjs +144 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-observation.mjs +189 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/index.domain.mjs +51 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/index.ts +122 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.ts +73 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +163 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.ts +174 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-parent-callbacks.domain.mjs +66 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-parent-callbacks.ts +68 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.domain.mjs +87 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.ts +90 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.domain.mjs +97 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.ts +139 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/errors.domain.mjs +12 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/errors.ts +79 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.domain.mjs +342 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.ts +513 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.domain.mjs +283 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.ts +458 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/recovery.domain.mjs +52 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/recovery.ts +55 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.domain.mjs +114 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.ts +140 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.domain.mjs +280 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.ts +442 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.domain.mjs +420 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.ts +441 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/sqlite.domain.mjs +194 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/sqlite.ts +239 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +275 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +585 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.domain.mjs +120 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.ts +142 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-cancellation.domain.mjs +188 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-cancellation.ts +224 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.domain.mjs +25 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.ts +707 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/upgrade.domain.mjs +22 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/upgrade.ts +22 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/validate.domain.mjs +234 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/validate.ts +241 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.domain.mjs +851 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.ts +1424 -0
- package/src/runtime/domain/subagents/shared-permission-blocker.domain.mjs +40 -0
- package/src/runtime/domain/subagents/shared-permission-blocker.ts +48 -0
- package/src/runtime/domain/subagents/shared-render-scheduler.domain.mjs +41 -0
- package/src/runtime/domain/subagents/shared-render-scheduler.ts +47 -0
- package/src/runtime/domain/subagents/shared-sandbox-core.domain.mjs +1531 -0
- package/src/runtime/domain/subagents/shared-sandbox-core.ts +1757 -0
- package/src/runtime/domain/subagents/shared-sandbox-diagnostics.domain.mjs +591 -0
- package/src/runtime/domain/subagents/shared-sandbox-diagnostics.ts +491 -0
- package/src/runtime/domain/subagents/shared-stall-detector.domain.mjs +37 -0
- package/src/runtime/domain/subagents/shared-stall-detector.ts +69 -0
- package/src/runtime/domain/subagents/shared-task-application.domain.mjs +185 -0
- package/src/runtime/domain/subagents/shared-task-application.ts +185 -0
- package/src/runtime/domain/subagents/shared-task-apply-patch.domain.mjs +416 -0
- package/src/runtime/domain/subagents/shared-task-apply-patch.ts +409 -0
- package/src/runtime/domain/subagents/shared-task-exec.domain.mjs +325 -0
- package/src/runtime/domain/subagents/shared-task-exec.ts +335 -0
- package/src/runtime/domain/subagents/shared-task-files.domain.mjs +346 -0
- package/src/runtime/domain/subagents/shared-task-files.ts +342 -0
- package/src/runtime/domain/subagents/shared-task-process-identity.domain.mjs +140 -0
- package/src/runtime/domain/subagents/shared-task-process-identity.ts +126 -0
- package/src/runtime/domain/subagents/shared-task-process-list.domain.mjs +220 -0
- package/src/runtime/domain/subagents/shared-task-process-list.ts +209 -0
- package/src/runtime/domain/subagents/shared-task-sandbox.domain.mjs +227 -0
- package/src/runtime/domain/subagents/shared-task-sandbox.ts +224 -0
- package/src/runtime/domain/subagents/shared-task-tools.domain.mjs +92 -0
- package/src/runtime/domain/subagents/shared-task-tools.ts +123 -0
- package/src/runtime/domain/subagents/shared-work-client.mjs +303 -0
- package/src/runtime/domain/subagents/shared-worktree-context.domain.mjs +142 -0
- package/src/runtime/domain/subagents/shared-worktree-context.ts +158 -0
- package/src/runtime/domain/subagents/skill-picker.domain.mjs +316 -0
- package/src/runtime/domain/subagents/skill-picker.ts +353 -0
- package/src/runtime/domain/subagents/skills.domain.mjs +138 -0
- package/src/runtime/domain/subagents/skills.ts +183 -0
- package/src/runtime/domain/subagents/spawn.domain.mjs +72 -0
- package/src/runtime/domain/subagents/spawn.ts +80 -0
- package/src/runtime/domain/subagents/stop.domain.mjs +184 -0
- package/src/runtime/domain/subagents/stop.ts +217 -0
- package/src/runtime/domain/subagents/subagent-tools.domain.mjs +101 -0
- package/src/runtime/domain/subagents/subagent-tools.ts +137 -0
- package/src/runtime/domain/subagents/task-guard.domain.mjs +163 -0
- package/src/runtime/domain/subagents/task-guard.ts +144 -0
- package/src/runtime/domain/subagents/task-policy.domain.mjs +201 -0
- package/src/runtime/domain/subagents/task-policy.ts +236 -0
- package/src/runtime/domain/subagents/task-runtime.mjs +53 -0
- package/src/runtime/domain/subagents/task-tool-owner.domain.mjs +18 -0
- package/src/runtime/domain/subagents/task-tool-owner.ts +21 -0
- package/src/runtime/domain/subagents/thinking.domain.mjs +22 -0
- package/src/runtime/domain/subagents/thinking.ts +28 -0
- package/src/runtime/domain/subagents/tier-policy.domain.mjs +75 -0
- package/src/runtime/domain/subagents/tier-policy.ts +126 -0
- package/src/runtime/domain/subagents/timing-notifications.domain.mjs +51 -0
- package/src/runtime/domain/subagents/timing-notifications.ts +37 -0
- package/src/runtime/domain/subagents/timing.domain.mjs +487 -0
- package/src/runtime/domain/subagents/timing.ts +634 -0
- package/src/runtime/domain/subagents/tools.domain.mjs +558 -0
- package/src/runtime/domain/subagents/tools.ts +629 -0
- package/src/runtime/domain/subagents/widget.domain.mjs +6 -0
- package/src/runtime/domain/subagents/widget.mjs +246 -0
- package/src/runtime/domain/subagents/widget.ts +30 -0
- package/src/runtime/domain/subagents/work-observation.domain.mjs +316 -0
- package/src/runtime/domain/subagents/work-observation.ts +248 -0
- package/src/runtime/domain/subagents/worker-catalog.domain.mjs +120 -0
- package/src/runtime/domain/subagents/worker-catalog.ts +162 -0
- package/src/runtime/domain/subagents/worker-operations-host.domain.mjs +1009 -0
- package/src/runtime/domain/subagents/worker-operations-host.ts +1238 -0
- package/src/runtime/domain/subagents/worker-waits.domain.mjs +2336 -0
- package/src/runtime/domain/subagents/worker-waits.ts +2878 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.mjs +52 -0
- package/src/runtime/domain/subagents/workspace-scope.ts +50 -0
- package/src/runtime/health-config.ts +27 -11
- package/src/runtime/log-checkpoints.ts +117 -0
- package/src/runtime/mutation-store.ts +5 -4
- package/src/runtime/queries.ts +27 -3
- package/src/runtime/query-executor.ts +178 -34
- package/src/runtime/query-worker.mjs +119 -22
- package/src/runtime/read-model.ts +939 -113
- package/src/runtime/read-watchdog-probe.ts +4 -0
- package/src/runtime/result-supervision.ts +188 -16
- package/src/runtime/source-watch.ts +175 -0
- package/src/runtime/subagent-launch.ts +73 -0
- package/src/runtime/work-budget.ts +130 -0
- package/src/runtime/work-client.d.mts +8 -0
- package/src/runtime/work-client.mjs +150 -7
- package/src/server.ts +185 -22
- package/src/session-activity.ts +26 -4
- package/src/version.ts +1 -1
|
@@ -0,0 +1,1584 @@
|
|
|
1
|
+
// Generated fixed launch domain from packages/pi-better-subagents/registry.ts. Do not edit directly.
|
|
2
|
+
/**
|
|
3
|
+
* Run registry — durable metadata for each spawned subagent.
|
|
4
|
+
*
|
|
5
|
+
* The authoritative record for every run is a `meta.json` sidecar on disk, so
|
|
6
|
+
* `list` / `output` / `result` keep working across foreground turns, `/reload`,
|
|
7
|
+
* and even a full pi restart. In-memory state holds only the live exit handlers
|
|
8
|
+
* for runs this process spawned.
|
|
9
|
+
*
|
|
10
|
+
* Catalog launches may attach an optional immutable `catalog` snapshot to that
|
|
11
|
+
* same record. Display-label reservations live under `{baseDir()}/labels`
|
|
12
|
+
* (see catalog-identity.ts). They are not a second registry, and removing a
|
|
13
|
+
* run directory does not delete them.
|
|
14
|
+
*/
|
|
15
|
+
import { assignmentRetainsEvidence } from "./assignment-outcomes.domain.mjs";
|
|
16
|
+
import { processStartToken } from "./process-identity.mjs";
|
|
17
|
+
export { processStartToken } from "./process-identity.mjs";
|
|
18
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
19
|
+
import { closeSync, existsSync, fsyncSync, linkSync, mkdirSync, openSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { createRequire } from "node:module";
|
|
21
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
22
|
+
import { tmpdir } from "node:os";
|
|
23
|
+
import { basename, dirname, join } from "node:path";
|
|
24
|
+
import { processExists } from "./spawn.domain.mjs";
|
|
25
|
+
import { archiveCompletedAttempt, durableSessionsDir, listArchivedAttempts, readArchivedAttempt, recordArchiveStatusTombstone, } from "./session-store.domain.mjs";
|
|
26
|
+
import { failureIdentity, observeFailures, readFailureState } from "./shared-failure-observations.domain.mjs";
|
|
27
|
+
/**
|
|
28
|
+
* True when coherent child-exit evidence may finalize a run in this status.
|
|
29
|
+
* `running` is the normal path; `orphaned`/`lost` are PROVISIONAL
|
|
30
|
+
* reconciliation verdicts (a health tick can observe the just-exited pid
|
|
31
|
+
* before the close handler runs) that the real exit — the stronger evidence —
|
|
32
|
+
* supersedes. True terminal records are never overwritten: finalization is
|
|
33
|
+
* idempotent (`completed`/`failed`) and a deliberate `subagent_stop` kill
|
|
34
|
+
* (`killed`) is not undone by the resulting exit.
|
|
35
|
+
*/
|
|
36
|
+
export function canExitFinalize(status) {
|
|
37
|
+
return status === "running" || status === "orphaned" || status === "lost";
|
|
38
|
+
}
|
|
39
|
+
/** Root of the VOLATILE runtime registry, deliberately OUTSIDE any repo. */
|
|
40
|
+
export function baseDir() {
|
|
41
|
+
return join(realpathSync(tmpdir()), "pi-better-subagents");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* `--session-dir` for NEW child launches: the durable Pi agent directory
|
|
45
|
+
* (`<agent dir>/subagents/sessions`), not the volatile registry. Saved
|
|
46
|
+
* conversations are ordinary Pi session files, so changing TMPDIR or a restart
|
|
47
|
+
* must not move them. Recipes recorded before this layout keep pointing at the
|
|
48
|
+
* old temporary directory and are read exactly as recorded.
|
|
49
|
+
*/
|
|
50
|
+
export function sessionsDir() {
|
|
51
|
+
return durableSessionsDir();
|
|
52
|
+
}
|
|
53
|
+
export function taskWorkspaceDir(id) {
|
|
54
|
+
if (!/^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id))
|
|
55
|
+
throw new Error("Invalid task workspace run ID.");
|
|
56
|
+
return join(realpathSync(tmpdir()), "pi-better-subagent-workspaces", id);
|
|
57
|
+
}
|
|
58
|
+
export function runDir(id) {
|
|
59
|
+
return join(baseDir(), "runs", id);
|
|
60
|
+
}
|
|
61
|
+
export function logPathFor(id) {
|
|
62
|
+
return join(runDir(id), "output.log");
|
|
63
|
+
}
|
|
64
|
+
export function promptPathFor(id) {
|
|
65
|
+
return join(runDir(id), "prompt.md");
|
|
66
|
+
}
|
|
67
|
+
function metaPathFor(id) {
|
|
68
|
+
return join(runDir(id), "meta.json");
|
|
69
|
+
}
|
|
70
|
+
export function parkRecordPath(id) {
|
|
71
|
+
return join(runDir(id), "park.json");
|
|
72
|
+
}
|
|
73
|
+
/** Verify the shape before trusting a record read from disk. */
|
|
74
|
+
function parseParkRecord(value) {
|
|
75
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
76
|
+
return undefined;
|
|
77
|
+
const record = value;
|
|
78
|
+
if (record.version !== 1)
|
|
79
|
+
return undefined;
|
|
80
|
+
if (typeof record.waitId !== "string" || !record.waitId)
|
|
81
|
+
return undefined;
|
|
82
|
+
if (!Number.isSafeInteger(record.generation) || (record.generation ?? 0) < 1)
|
|
83
|
+
return undefined;
|
|
84
|
+
if (typeof record.attemptId !== "string" || !record.attemptId)
|
|
85
|
+
return undefined;
|
|
86
|
+
if (typeof record.parkedAt !== "number" || !Number.isFinite(record.parkedAt))
|
|
87
|
+
return undefined;
|
|
88
|
+
const phases = ["parking", "waiting", "ready", "resuming", "held", "consumed", "cancelled"];
|
|
89
|
+
if (!phases.includes(record.phase))
|
|
90
|
+
return undefined;
|
|
91
|
+
return record;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Write the park disposition atomically and mirror it into the run's metadata.
|
|
95
|
+
* The mirror keeps listings, the Goal provider and cleanup from needing a second
|
|
96
|
+
* read; the file remains the authoritative copy for this attempt.
|
|
97
|
+
*/
|
|
98
|
+
export function writeParkRecord(id, record, options = {}) {
|
|
99
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id))
|
|
100
|
+
throw new Error("Invalid run ID.");
|
|
101
|
+
mkdirSync(runDir(id), { recursive: true });
|
|
102
|
+
const path = parkRecordPath(id);
|
|
103
|
+
const temp = join(dirname(path), `.park.${randomBytes(6).toString("hex")}.tmp`);
|
|
104
|
+
writeFileSync(temp, JSON.stringify(record), { mode: 0o600, flag: "wx" });
|
|
105
|
+
if (options.ifAbsent) {
|
|
106
|
+
try {
|
|
107
|
+
linkSync(temp, path);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
if (error.code === "EEXIST")
|
|
111
|
+
return;
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
unlinkSync(temp);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else
|
|
119
|
+
renameSync(temp, path);
|
|
120
|
+
const meta = readMeta(id);
|
|
121
|
+
if (meta && (meta.park?.phase !== record.phase
|
|
122
|
+
|| meta.park?.heldReason !== record.heldReason
|
|
123
|
+
|| meta.park?.resumedBy !== record.resumedBy
|
|
124
|
+
|| meta.park?.resumeExitReconciledClaimId !== record.resumeExitReconciledClaimId)) {
|
|
125
|
+
meta.park = record;
|
|
126
|
+
writeMeta(meta);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** Read the attempt's park disposition; absent for an ordinary run. */
|
|
130
|
+
export function readParkRecord(id) {
|
|
131
|
+
try {
|
|
132
|
+
return parseParkRecord(JSON.parse(readFileSync(parkRecordPath(id), "utf-8")));
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
if (error.code !== "ENOENT")
|
|
136
|
+
return undefined;
|
|
137
|
+
// The volatile run directory may already be reclaimed; the durable
|
|
138
|
+
// completed-attempt record carries the park snapshot taken at finalization.
|
|
139
|
+
return readMeta(id)?.park;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** Merge one phase/reason change into the park disposition. */
|
|
143
|
+
export function updateParkRecord(id, patch) {
|
|
144
|
+
const current = readParkRecord(id);
|
|
145
|
+
if (!current)
|
|
146
|
+
return undefined;
|
|
147
|
+
const next = { ...current, ...patch, updatedAt: Date.now() };
|
|
148
|
+
writeParkRecord(id, next);
|
|
149
|
+
return next;
|
|
150
|
+
}
|
|
151
|
+
/** A park still owns a pending continuation: `parking`, `waiting`, `ready`, `resuming` or `held`. */
|
|
152
|
+
export function isPendingPark(record) {
|
|
153
|
+
return record.phase !== "consumed" && record.phase !== "cancelled";
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Runs whose park disposition can still resume. Used by cleanup protection and
|
|
157
|
+
* by resume reconciliation; a consumed or cancelled park is not included.
|
|
158
|
+
*/
|
|
159
|
+
export function listPendingParkedRuns(metas = listMetas()) {
|
|
160
|
+
return listParkRecordCandidates(metas).filter((meta) => {
|
|
161
|
+
const record = meta.park ?? readParkRecord(meta.id);
|
|
162
|
+
return record !== undefined && isPendingPark(record);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/** Include a park file that was published just before its metadata mirror was lost. */
|
|
166
|
+
export function listParkRecordCandidates(metas = listMetas()) {
|
|
167
|
+
let volatileIds;
|
|
168
|
+
try {
|
|
169
|
+
volatileIds = new Set(readdirSync(join(baseDir(), "runs")));
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
volatileIds = new Set();
|
|
173
|
+
}
|
|
174
|
+
return metas.filter((meta) => meta.park !== undefined
|
|
175
|
+
|| (volatileIds.has(meta.id) && existsSync(parkRecordPath(meta.id))));
|
|
176
|
+
}
|
|
177
|
+
/** Read recovery sidecars in batches so a large registry does not block socket replies. */
|
|
178
|
+
export async function* scanParkRecords(metas = listMetas()) {
|
|
179
|
+
const runs = join(baseDir(), "runs");
|
|
180
|
+
const volatileIds = new Set(await readdir(runs).catch(() => []));
|
|
181
|
+
const batchSize = 32;
|
|
182
|
+
for (let offset = 0; offset < metas.length; offset += batchSize) {
|
|
183
|
+
const batch = await Promise.all(metas.slice(offset, offset + batchSize).map(async (meta) => {
|
|
184
|
+
if (!meta.park && !volatileIds.has(meta.id))
|
|
185
|
+
return;
|
|
186
|
+
try {
|
|
187
|
+
const record = parseParkRecord(JSON.parse(await readFile(join(runs, meta.id, "park.json"), "utf-8")));
|
|
188
|
+
if (record)
|
|
189
|
+
return { meta, record };
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (error.code === "ENOENT" && meta.park) {
|
|
193
|
+
const record = readMeta(meta.id)?.park;
|
|
194
|
+
if (record)
|
|
195
|
+
return { meta, record };
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}));
|
|
199
|
+
for (const entry of batch)
|
|
200
|
+
if (entry)
|
|
201
|
+
yield entry;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* How long a launch attempt that never produced a child is kept before a sweep
|
|
206
|
+
* reclaims it. Launch preparation takes seconds; the grace only exists so a
|
|
207
|
+
* paused or unusually slow launch in another process is never reclaimed under
|
|
208
|
+
* it. A record this old has no metadata and no log, so it can never become a run.
|
|
209
|
+
*/
|
|
210
|
+
export const ABANDONED_LAUNCH_GRACE_MS = 60 * 60 * 1000;
|
|
211
|
+
/**
|
|
212
|
+
* Parent-authored provenance for a run launched on the trusted task runtime (#325).
|
|
213
|
+
*
|
|
214
|
+
* `meta.taskRuntime` alone lives in the run directory. The parent only honours a child's
|
|
215
|
+
* structured intent and `failure_disposition` calls when this separate record, written by the
|
|
216
|
+
* parent before the child starts, also exists. It sits outside every run directory, under the
|
|
217
|
+
* registry root that the task runtime's policy denies to the child (`runtimeRoots`), so a future
|
|
218
|
+
* child-writable run directory cannot forge trust by rewriting its own metadata.
|
|
219
|
+
*/
|
|
220
|
+
export function taskRuntimeProvenancePath(id) {
|
|
221
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id))
|
|
222
|
+
throw new Error("Invalid run ID.");
|
|
223
|
+
return join(baseDir(), "task-runtime", `${id}.json`);
|
|
224
|
+
}
|
|
225
|
+
export function recordTaskRuntimeProvenance(id) {
|
|
226
|
+
const path = taskRuntimeProvenancePath(id);
|
|
227
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
228
|
+
writeFileSync(path, JSON.stringify({ version: 1, id }), { mode: 0o600 });
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Remove a run directory together with its provenance record (#325). Every run-removal path
|
|
232
|
+
* uses this, so no provenance outlives its run. Throws when the run directory cannot be removed.
|
|
233
|
+
*/
|
|
234
|
+
export function removeRunDirectory(id) {
|
|
235
|
+
const meta = readMeta(id);
|
|
236
|
+
if (meta && assignmentRetainsEvidence(meta))
|
|
237
|
+
throw new Error("Assignment outcome or uncertain writer still retains this run's evidence.");
|
|
238
|
+
rmSync(runDir(id), { recursive: true, force: true });
|
|
239
|
+
removeTaskRuntimeProvenance(id);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Undo everything a launch prepared before its child failed to spawn (#325, #332). Nothing references
|
|
243
|
+
* the run yet (no metadata), so its run directory and provenance go, plus the per-run session
|
|
244
|
+
* directory and disposable clone workspace of a sandboxed launch and the task scratch. Only the
|
|
245
|
+
* run's own paths are removed: a shared sessions directory or a caller-chosen sandbox_dir is kept.
|
|
246
|
+
*/
|
|
247
|
+
export function discardFailedLaunch(id, prepared = {}) {
|
|
248
|
+
try {
|
|
249
|
+
removeRunDirectory(id);
|
|
250
|
+
}
|
|
251
|
+
catch { /* best effort */ }
|
|
252
|
+
const remove = (path) => { try {
|
|
253
|
+
rmSync(path, { recursive: true, force: true });
|
|
254
|
+
}
|
|
255
|
+
catch { /* best effort */ } };
|
|
256
|
+
if (prepared.sessionDir && prepared.sessionDir === join(sessionsDir(), id))
|
|
257
|
+
remove(prepared.sessionDir);
|
|
258
|
+
if (prepared.workspaceDir && /^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id) && prepared.workspaceDir === taskWorkspaceDir(id))
|
|
259
|
+
remove(prepared.workspaceDir);
|
|
260
|
+
if (prepared.scratch)
|
|
261
|
+
remove(prepared.scratch);
|
|
262
|
+
}
|
|
263
|
+
/** Best-effort removal of a run's provenance record (e.g. after a failed launch). */
|
|
264
|
+
export function removeTaskRuntimeProvenance(id) {
|
|
265
|
+
try {
|
|
266
|
+
rmSync(taskRuntimeProvenancePath(id), { force: true });
|
|
267
|
+
}
|
|
268
|
+
catch { /* invalid id or already gone */ }
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Provenance records whose run directory no longer exists (e.g. removed by an older version). Only a
|
|
272
|
+
* definite ENOENT counts as gone: any other stat error (permissions, I/O) keeps the record, since
|
|
273
|
+
* deleting a live run's provenance would silently drop it to the exact-retry rule (#332).
|
|
274
|
+
*/
|
|
275
|
+
export function orphanedTaskRuntimeProvenance() {
|
|
276
|
+
let names;
|
|
277
|
+
try {
|
|
278
|
+
names = readdirSync(join(baseDir(), "task-runtime"));
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return [];
|
|
282
|
+
}
|
|
283
|
+
return names.filter((name) => name.endsWith(".json")).map((name) => name.slice(0, -5))
|
|
284
|
+
.filter((id) => { try {
|
|
285
|
+
statSync(runDir(id));
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
return error.code === "ENOENT";
|
|
290
|
+
} });
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Whether the parent may honour structured intent for this run. `unknown` means the answer could
|
|
294
|
+
* not be read right now (unreadable metadata or provenance); callers retry instead of caching it.
|
|
295
|
+
* A missing or mismatched provenance record is a definite `unconfined` (the exact-retry rule).
|
|
296
|
+
*/
|
|
297
|
+
export function taskRuntimeTrust(id) {
|
|
298
|
+
let raw;
|
|
299
|
+
try {
|
|
300
|
+
raw = readFileSync(taskRuntimeProvenancePath(id), "utf-8");
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
return error.code === "ENOENT" ? "unconfined" : "unknown";
|
|
304
|
+
}
|
|
305
|
+
let record;
|
|
306
|
+
try {
|
|
307
|
+
record = JSON.parse(raw);
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return "unconfined";
|
|
311
|
+
}
|
|
312
|
+
if (!record || typeof record !== "object" || record.id !== id)
|
|
313
|
+
return "unconfined";
|
|
314
|
+
const inspected = inspectRunMeta(id);
|
|
315
|
+
if (inspected.kind !== "ok")
|
|
316
|
+
return "unknown";
|
|
317
|
+
return inspected.meta.taskRuntime === true ? "trusted" : "unconfined";
|
|
318
|
+
}
|
|
319
|
+
function launchRecordPath(id) {
|
|
320
|
+
return join(runDir(id), ".launch.json");
|
|
321
|
+
}
|
|
322
|
+
function launchFromSource(source) {
|
|
323
|
+
const name = typeof source.name === "string" && source.name.length > 0 ? source.name : undefined;
|
|
324
|
+
const catalogKnown = Object.prototype.hasOwnProperty.call(source, "catalog") && source.catalog !== undefined;
|
|
325
|
+
return { name, catalogKnown, catalog: catalogKnown ? source.catalog : undefined };
|
|
326
|
+
}
|
|
327
|
+
function serializeLaunch(record) {
|
|
328
|
+
const payload = {};
|
|
329
|
+
if (record.name !== undefined)
|
|
330
|
+
payload.name = record.name;
|
|
331
|
+
if (record.catalogKnown)
|
|
332
|
+
payload.catalog = record.catalog ?? null;
|
|
333
|
+
return JSON.stringify(payload);
|
|
334
|
+
}
|
|
335
|
+
function parseLaunch(raw, id) {
|
|
336
|
+
let parsed;
|
|
337
|
+
try {
|
|
338
|
+
parsed = JSON.parse(raw);
|
|
339
|
+
}
|
|
340
|
+
catch (error) {
|
|
341
|
+
if (error instanceof SyntaxError)
|
|
342
|
+
throw new Error(`Unreadable launch record for ${id}`);
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
346
|
+
throw new Error(`Unreadable launch record for ${id}`);
|
|
347
|
+
}
|
|
348
|
+
return launchFromSource(parsed);
|
|
349
|
+
}
|
|
350
|
+
/** A committed meta.json is one historical launch. A truncated file is not. */
|
|
351
|
+
function readCommittedMetaLaunch(id) {
|
|
352
|
+
let raw;
|
|
353
|
+
try {
|
|
354
|
+
raw = readFileSync(metaPathFor(id), "utf-8");
|
|
355
|
+
}
|
|
356
|
+
catch (error) {
|
|
357
|
+
if (errnoOf(error) === "ENOENT")
|
|
358
|
+
return undefined;
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
const parsed = JSON.parse(raw);
|
|
363
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
364
|
+
return undefined;
|
|
365
|
+
return launchFromSource(parsed);
|
|
366
|
+
}
|
|
367
|
+
catch (error) {
|
|
368
|
+
if (error instanceof SyntaxError)
|
|
369
|
+
return undefined;
|
|
370
|
+
throw error;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function applyLaunchRecord(meta, record) {
|
|
374
|
+
if (record.name !== undefined)
|
|
375
|
+
meta.name = record.name;
|
|
376
|
+
else
|
|
377
|
+
delete meta.name;
|
|
378
|
+
if (record.catalogKnown)
|
|
379
|
+
meta.catalog = record.catalog;
|
|
380
|
+
else
|
|
381
|
+
delete meta.catalog;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Publish `contents` at `target` so the first complete file wins.
|
|
385
|
+
*
|
|
386
|
+
* `link` of an fsynced temp is atomic: callers either see the winner's
|
|
387
|
+
* bytes or no file. A later link gets EEXIST and leaves the winner in place.
|
|
388
|
+
* This is not another lock. Token recovery can still rename `.meta.lock`
|
|
389
|
+
* after a stale observation (the rename syscall moves whatever inode is at
|
|
390
|
+
* that path), so the lock pathname is not a safe place to remember the
|
|
391
|
+
* first snapshot.
|
|
392
|
+
*/
|
|
393
|
+
function publishImmutable(target, contents) {
|
|
394
|
+
try {
|
|
395
|
+
statSync(target);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
catch (error) {
|
|
399
|
+
if (errnoOf(error) !== "ENOENT")
|
|
400
|
+
throw error;
|
|
401
|
+
}
|
|
402
|
+
const temp = join(dirname(target), `.immutable.${randomBytes(8).toString("hex")}.tmp`);
|
|
403
|
+
const fd = openSync(temp, "wx");
|
|
404
|
+
try {
|
|
405
|
+
writeFileSync(fd, contents);
|
|
406
|
+
fsyncSync(fd);
|
|
407
|
+
}
|
|
408
|
+
catch (error) {
|
|
409
|
+
try {
|
|
410
|
+
closeSync(fd);
|
|
411
|
+
}
|
|
412
|
+
catch (closeError) {
|
|
413
|
+
if (errnoOf(closeError) !== "EBADF")
|
|
414
|
+
throw closeError;
|
|
415
|
+
}
|
|
416
|
+
try {
|
|
417
|
+
unlinkSync(temp);
|
|
418
|
+
}
|
|
419
|
+
catch (cleanup) {
|
|
420
|
+
if (errnoOf(cleanup) !== "ENOENT")
|
|
421
|
+
throw cleanup;
|
|
422
|
+
}
|
|
423
|
+
throw error;
|
|
424
|
+
}
|
|
425
|
+
closeSync(fd);
|
|
426
|
+
try {
|
|
427
|
+
linkSync(temp, target);
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
try {
|
|
431
|
+
unlinkSync(temp);
|
|
432
|
+
}
|
|
433
|
+
catch (cleanup) {
|
|
434
|
+
if (errnoOf(cleanup) !== "ENOENT")
|
|
435
|
+
throw cleanup;
|
|
436
|
+
}
|
|
437
|
+
if (errnoOf(error) !== "EEXIST")
|
|
438
|
+
throw error;
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
try {
|
|
442
|
+
unlinkSync(temp);
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
if (errnoOf(error) !== "ENOENT")
|
|
446
|
+
throw error;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function readOptionalUtf8(target) {
|
|
450
|
+
try {
|
|
451
|
+
return readFileSync(target, "utf-8");
|
|
452
|
+
}
|
|
453
|
+
catch (error) {
|
|
454
|
+
if (errnoOf(error) === "ENOENT")
|
|
455
|
+
return undefined;
|
|
456
|
+
throw error;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Freeze one coherent launch snapshot before `meta.json` is replaced.
|
|
461
|
+
*
|
|
462
|
+
* The exclusive link is the publication. A pause or crash before it leaves
|
|
463
|
+
* no snapshot; a pause or crash after it leaves the whole snapshot. Orphan
|
|
464
|
+
* split anchors are not read: pairing those files is the bug this replaces.
|
|
465
|
+
* A legacy `meta.json` is copied in as one record first, so its name and
|
|
466
|
+
* catalog (including absence or null) beat this writer without being
|
|
467
|
+
* reconciled field by field. The caller's object is then rewritten from
|
|
468
|
+
* whichever record won the link.
|
|
469
|
+
*/
|
|
470
|
+
function applyFrozenLaunch(meta) {
|
|
471
|
+
const path = launchRecordPath(meta.id);
|
|
472
|
+
let raw = readOptionalUtf8(path);
|
|
473
|
+
if (raw === undefined) {
|
|
474
|
+
const committed = readCommittedMetaLaunch(meta.id) ?? launchFromSource(meta);
|
|
475
|
+
publishImmutable(path, serializeLaunch(committed));
|
|
476
|
+
raw = readOptionalUtf8(path);
|
|
477
|
+
if (raw === undefined)
|
|
478
|
+
throw new Error(`Missing launch record for ${meta.id}`);
|
|
479
|
+
}
|
|
480
|
+
applyLaunchRecord(meta, parseLaunch(raw, meta.id));
|
|
481
|
+
}
|
|
482
|
+
const META_LOCK_WAIT_MS = 5_000;
|
|
483
|
+
const UNPARSABLE_LOCK_GRACE_MS = 1_000;
|
|
484
|
+
const nodeRequire = createRequire(import.meta.url);
|
|
485
|
+
const lockPause = new Int32Array(new SharedArrayBuffer(4));
|
|
486
|
+
const sqliteWarningFilter = Symbol.for("pi-better-subagents.sqlite-warning-filter");
|
|
487
|
+
class MetadataLockLost extends Error {
|
|
488
|
+
constructor() {
|
|
489
|
+
super("metadata lock lost");
|
|
490
|
+
this.name = "MetadataLockLost";
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function errnoOf(error) {
|
|
494
|
+
return error.code;
|
|
495
|
+
}
|
|
496
|
+
function sleepMs(ms) {
|
|
497
|
+
Atomics.wait(lockPause, 0, 0, ms);
|
|
498
|
+
}
|
|
499
|
+
function lockTimeout(id) {
|
|
500
|
+
return new Error(`Timed out writing metadata for ${id}`);
|
|
501
|
+
}
|
|
502
|
+
let ownStartKnown = false;
|
|
503
|
+
let ownStartCache;
|
|
504
|
+
const foreignStartCache = new Map();
|
|
505
|
+
function ownStartToken() {
|
|
506
|
+
if (ownStartKnown)
|
|
507
|
+
return ownStartCache;
|
|
508
|
+
ownStartCache = processStartToken(process.pid);
|
|
509
|
+
ownStartKnown = true;
|
|
510
|
+
return ownStartCache;
|
|
511
|
+
}
|
|
512
|
+
function cachedStartToken(pid) {
|
|
513
|
+
const now = Date.now();
|
|
514
|
+
const hit = foreignStartCache.get(pid);
|
|
515
|
+
if (hit && now - hit.at < 200)
|
|
516
|
+
return hit.token;
|
|
517
|
+
const token = processStartToken(pid);
|
|
518
|
+
foreignStartCache.set(pid, { token, at: now });
|
|
519
|
+
return token;
|
|
520
|
+
}
|
|
521
|
+
function makeOwner() {
|
|
522
|
+
return { pid: process.pid, token: randomBytes(16).toString("hex"), start: ownStartToken() };
|
|
523
|
+
}
|
|
524
|
+
function serializeOwner(owner) {
|
|
525
|
+
return JSON.stringify({ pid: owner.pid, token: owner.token, start: owner.start ?? null });
|
|
526
|
+
}
|
|
527
|
+
function parseOwner(raw) {
|
|
528
|
+
const trimmed = raw.trim();
|
|
529
|
+
if (/^[1-9]\d*$/.test(trimmed)) {
|
|
530
|
+
const pid = Number(trimmed);
|
|
531
|
+
return Number.isSafeInteger(pid) ? { pid, token: "" } : undefined;
|
|
532
|
+
}
|
|
533
|
+
try {
|
|
534
|
+
const parsed = JSON.parse(raw);
|
|
535
|
+
if (!parsed || typeof parsed !== "object")
|
|
536
|
+
return undefined;
|
|
537
|
+
if (typeof parsed.pid !== "number" || !Number.isInteger(parsed.pid) || parsed.pid <= 0)
|
|
538
|
+
return undefined;
|
|
539
|
+
if (typeof parsed.token !== "string")
|
|
540
|
+
return undefined;
|
|
541
|
+
const start = typeof parsed.start === "string" && parsed.start.length > 0 ? parsed.start : undefined;
|
|
542
|
+
return { pid: parsed.pid, token: parsed.token, start };
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
return undefined;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/** A live pid is never stale, including when that process is stopped or paused. */
|
|
549
|
+
function ownerIsLive(owner) {
|
|
550
|
+
if (owner.pid === process.pid)
|
|
551
|
+
return true;
|
|
552
|
+
if (!processExists(owner.pid))
|
|
553
|
+
return false;
|
|
554
|
+
if (!owner.start)
|
|
555
|
+
return true;
|
|
556
|
+
const current = cachedStartToken(owner.pid);
|
|
557
|
+
if (!current)
|
|
558
|
+
return true;
|
|
559
|
+
return current === owner.start;
|
|
560
|
+
}
|
|
561
|
+
function isStaleLock(lockPath, raw) {
|
|
562
|
+
const owner = parseOwner(raw);
|
|
563
|
+
if (!owner) {
|
|
564
|
+
try {
|
|
565
|
+
return Date.now() - statSync(lockPath).mtimeMs > UNPARSABLE_LOCK_GRACE_MS;
|
|
566
|
+
}
|
|
567
|
+
catch (error) {
|
|
568
|
+
if (errnoOf(error) === "ENOENT")
|
|
569
|
+
return false;
|
|
570
|
+
throw error;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return !ownerIsLive(owner);
|
|
574
|
+
}
|
|
575
|
+
function restoreDisplacedLock(grave, lockPath) {
|
|
576
|
+
try {
|
|
577
|
+
linkSync(grave, lockPath);
|
|
578
|
+
}
|
|
579
|
+
catch (error) {
|
|
580
|
+
if (errnoOf(error) !== "EEXIST")
|
|
581
|
+
throw error;
|
|
582
|
+
let displaced;
|
|
583
|
+
try {
|
|
584
|
+
displaced = readFileSync(grave, "utf-8");
|
|
585
|
+
}
|
|
586
|
+
catch (readError) {
|
|
587
|
+
if (errnoOf(readError) === "ENOENT")
|
|
588
|
+
return;
|
|
589
|
+
throw readError;
|
|
590
|
+
}
|
|
591
|
+
if (!isStaleLock(grave, displaced))
|
|
592
|
+
return;
|
|
593
|
+
try {
|
|
594
|
+
unlinkSync(grave);
|
|
595
|
+
}
|
|
596
|
+
catch (cleanup) {
|
|
597
|
+
if (errnoOf(cleanup) !== "ENOENT")
|
|
598
|
+
throw cleanup;
|
|
599
|
+
}
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
try {
|
|
603
|
+
unlinkSync(grave);
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
if (errnoOf(error) !== "ENOENT")
|
|
607
|
+
throw error;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/** Drop the lock file only when `predicate` still matches the inode we moved aside. */
|
|
611
|
+
function removeLockIf(lockPath, predicate) {
|
|
612
|
+
let observed;
|
|
613
|
+
try {
|
|
614
|
+
observed = readFileSync(lockPath, "utf-8");
|
|
615
|
+
}
|
|
616
|
+
catch (error) {
|
|
617
|
+
if (errnoOf(error) === "ENOENT")
|
|
618
|
+
return;
|
|
619
|
+
throw error;
|
|
620
|
+
}
|
|
621
|
+
if (!predicate(observed, lockPath))
|
|
622
|
+
return;
|
|
623
|
+
const grave = join(dirname(lockPath), `.meta.lock.${randomBytes(8).toString("hex")}.displaced`);
|
|
624
|
+
try {
|
|
625
|
+
renameSync(lockPath, grave);
|
|
626
|
+
}
|
|
627
|
+
catch (error) {
|
|
628
|
+
if (errnoOf(error) === "ENOENT")
|
|
629
|
+
return;
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
let moved;
|
|
633
|
+
try {
|
|
634
|
+
moved = readFileSync(grave, "utf-8");
|
|
635
|
+
}
|
|
636
|
+
catch (error) {
|
|
637
|
+
let restoreError;
|
|
638
|
+
try {
|
|
639
|
+
restoreDisplacedLock(grave, lockPath);
|
|
640
|
+
}
|
|
641
|
+
catch (restore) {
|
|
642
|
+
restoreError = restore;
|
|
643
|
+
}
|
|
644
|
+
if (restoreError)
|
|
645
|
+
throw restoreError;
|
|
646
|
+
throw error;
|
|
647
|
+
}
|
|
648
|
+
if (moved === observed && predicate(moved, grave)) {
|
|
649
|
+
try {
|
|
650
|
+
unlinkSync(grave);
|
|
651
|
+
}
|
|
652
|
+
catch (error) {
|
|
653
|
+
if (errnoOf(error) !== "ENOENT")
|
|
654
|
+
throw error;
|
|
655
|
+
}
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
restoreDisplacedLock(grave, lockPath);
|
|
659
|
+
}
|
|
660
|
+
function lockPayloadMatches(lockPath, payload) {
|
|
661
|
+
try {
|
|
662
|
+
return readFileSync(lockPath, "utf-8") === payload;
|
|
663
|
+
}
|
|
664
|
+
catch (error) {
|
|
665
|
+
if (errnoOf(error) === "ENOENT")
|
|
666
|
+
return false;
|
|
667
|
+
throw error;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
let databaseSyncCtor;
|
|
671
|
+
function databaseSync() {
|
|
672
|
+
if (databaseSyncCtor !== undefined)
|
|
673
|
+
return databaseSyncCtor;
|
|
674
|
+
installSqliteWarningFilter();
|
|
675
|
+
try {
|
|
676
|
+
const loaded = nodeRequire("node:sqlite");
|
|
677
|
+
databaseSyncCtor = loaded.DatabaseSync ?? null;
|
|
678
|
+
}
|
|
679
|
+
catch (error) {
|
|
680
|
+
const code = errnoOf(error);
|
|
681
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
682
|
+
const missing = code === "ERR_UNKNOWN_BUILTIN_MODULE"
|
|
683
|
+
|| message.includes("Cannot find package")
|
|
684
|
+
|| message.includes("Cannot find module")
|
|
685
|
+
|| message.includes("No such built-in module");
|
|
686
|
+
if (!missing)
|
|
687
|
+
throw error;
|
|
688
|
+
databaseSyncCtor = null;
|
|
689
|
+
}
|
|
690
|
+
return databaseSyncCtor;
|
|
691
|
+
}
|
|
692
|
+
function installSqliteWarningFilter() {
|
|
693
|
+
const marked = process;
|
|
694
|
+
if (marked[sqliteWarningFilter])
|
|
695
|
+
return;
|
|
696
|
+
marked[sqliteWarningFilter] = true;
|
|
697
|
+
const emit = process.emitWarning.bind(process);
|
|
698
|
+
process.emitWarning = ((warning, ...args) => {
|
|
699
|
+
const message = typeof warning === "string"
|
|
700
|
+
? warning
|
|
701
|
+
: warning?.message;
|
|
702
|
+
if (typeof message === "string" && message.includes("SQLite is an experimental feature"))
|
|
703
|
+
return;
|
|
704
|
+
return emit(warning, ...args);
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
function isSqliteBusy(error) {
|
|
708
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
709
|
+
return /database is locked|SQLITE_BUSY/i.test(message);
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Cross-process critical section. Node's built-in SQLite reserved lock is
|
|
713
|
+
* held for the whole section, including while this process is stopped, and
|
|
714
|
+
* the kernel drops it if the process dies. Waiters give up at 5s instead of
|
|
715
|
+
* blocking forever or stealing a live holder.
|
|
716
|
+
*/
|
|
717
|
+
function acquireProcessLock(directory) {
|
|
718
|
+
const Ctor = databaseSync();
|
|
719
|
+
if (!Ctor)
|
|
720
|
+
return () => { };
|
|
721
|
+
const db = new Ctor(join(directory, ".meta.lock.sqlite"));
|
|
722
|
+
try {
|
|
723
|
+
db.exec("PRAGMA busy_timeout = 5000");
|
|
724
|
+
db.exec("BEGIN IMMEDIATE");
|
|
725
|
+
}
|
|
726
|
+
catch (error) {
|
|
727
|
+
try {
|
|
728
|
+
db.close();
|
|
729
|
+
}
|
|
730
|
+
catch (closeError) {
|
|
731
|
+
if (!isSqliteBusy(error))
|
|
732
|
+
throw closeError;
|
|
733
|
+
}
|
|
734
|
+
if (isSqliteBusy(error))
|
|
735
|
+
throw lockTimeout(directory);
|
|
736
|
+
throw error;
|
|
737
|
+
}
|
|
738
|
+
return () => {
|
|
739
|
+
let rollbackError;
|
|
740
|
+
try {
|
|
741
|
+
db.exec("ROLLBACK");
|
|
742
|
+
}
|
|
743
|
+
catch (error) {
|
|
744
|
+
rollbackError = error;
|
|
745
|
+
}
|
|
746
|
+
try {
|
|
747
|
+
db.close();
|
|
748
|
+
}
|
|
749
|
+
catch (error) {
|
|
750
|
+
if (!rollbackError)
|
|
751
|
+
throw error;
|
|
752
|
+
}
|
|
753
|
+
if (rollbackError)
|
|
754
|
+
throw rollbackError;
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
function takeToken(lockPath, payload, deadline, id) {
|
|
758
|
+
for (;;) {
|
|
759
|
+
const creating = join(dirname(lockPath), `.meta.lock.${randomBytes(8).toString("hex")}.creating`);
|
|
760
|
+
writeFileSync(creating, payload, { flag: "wx" });
|
|
761
|
+
try {
|
|
762
|
+
linkSync(creating, lockPath);
|
|
763
|
+
}
|
|
764
|
+
catch (error) {
|
|
765
|
+
const code = errnoOf(error);
|
|
766
|
+
try {
|
|
767
|
+
unlinkSync(creating);
|
|
768
|
+
}
|
|
769
|
+
catch (cleanup) {
|
|
770
|
+
if (errnoOf(cleanup) !== "ENOENT")
|
|
771
|
+
throw cleanup;
|
|
772
|
+
}
|
|
773
|
+
if (code !== "EEXIST")
|
|
774
|
+
throw error;
|
|
775
|
+
removeLockIf(lockPath, (raw, path) => isStaleLock(path, raw));
|
|
776
|
+
if (Date.now() > deadline)
|
|
777
|
+
throw lockTimeout(id);
|
|
778
|
+
sleepMs(5);
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
try {
|
|
782
|
+
unlinkSync(creating);
|
|
783
|
+
}
|
|
784
|
+
catch (error) {
|
|
785
|
+
if (errnoOf(error) !== "ENOENT")
|
|
786
|
+
throw error;
|
|
787
|
+
}
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Serialize one run's read-modify-write. The owner token, not the lock's age,
|
|
793
|
+
* decides who may recover or release it. A live pid — even one paused well
|
|
794
|
+
* past two seconds — is not stolen. Release renames the lock aside and deletes
|
|
795
|
+
* it only when the moved bytes are still ours, so a newer owner's lock stays.
|
|
796
|
+
* The lock is per run and is not a second registry.
|
|
797
|
+
*/
|
|
798
|
+
function withRunMetaLock(id, body) {
|
|
799
|
+
const directory = runDir(id);
|
|
800
|
+
let releaseProcessLock;
|
|
801
|
+
try {
|
|
802
|
+
releaseProcessLock = acquireProcessLock(directory);
|
|
803
|
+
}
|
|
804
|
+
catch (error) {
|
|
805
|
+
if (isSqliteBusy(error) || (error instanceof Error && error.message === `Timed out writing metadata for ${directory}`)) {
|
|
806
|
+
throw lockTimeout(id);
|
|
807
|
+
}
|
|
808
|
+
throw error;
|
|
809
|
+
}
|
|
810
|
+
try {
|
|
811
|
+
const lockPath = join(directory, ".meta.lock");
|
|
812
|
+
const payload = serializeOwner(makeOwner());
|
|
813
|
+
const deadline = Date.now() + META_LOCK_WAIT_MS;
|
|
814
|
+
for (;;) {
|
|
815
|
+
let ownsLock = false;
|
|
816
|
+
try {
|
|
817
|
+
takeToken(lockPath, payload, deadline, id);
|
|
818
|
+
ownsLock = true;
|
|
819
|
+
const owns = () => {
|
|
820
|
+
if (!ownsLock)
|
|
821
|
+
return false;
|
|
822
|
+
if (!lockPayloadMatches(lockPath, payload)) {
|
|
823
|
+
ownsLock = false;
|
|
824
|
+
return false;
|
|
825
|
+
}
|
|
826
|
+
return true;
|
|
827
|
+
};
|
|
828
|
+
if (!owns())
|
|
829
|
+
continue;
|
|
830
|
+
body(owns);
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
catch (error) {
|
|
834
|
+
if (error instanceof MetadataLockLost) {
|
|
835
|
+
ownsLock = false;
|
|
836
|
+
if (Date.now() > deadline)
|
|
837
|
+
throw lockTimeout(id);
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
throw error;
|
|
841
|
+
}
|
|
842
|
+
finally {
|
|
843
|
+
if (ownsLock)
|
|
844
|
+
removeLockIf(lockPath, (raw) => raw === payload);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
finally {
|
|
849
|
+
releaseProcessLock();
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
function writeMetaFile(meta) {
|
|
853
|
+
applyFrozenLaunch(meta);
|
|
854
|
+
const target = metaPathFor(meta.id);
|
|
855
|
+
const json = JSON.stringify(meta, null, 2);
|
|
856
|
+
if (process.platform === "win32") {
|
|
857
|
+
writeFileSync(target, json);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
const temp = join(runDir(meta.id), `.meta.${randomBytes(8).toString("hex")}.tmp`);
|
|
861
|
+
const fd = openSync(temp, "wx");
|
|
862
|
+
try {
|
|
863
|
+
writeFileSync(fd, json);
|
|
864
|
+
fsyncSync(fd);
|
|
865
|
+
}
|
|
866
|
+
catch (error) {
|
|
867
|
+
try {
|
|
868
|
+
closeSync(fd);
|
|
869
|
+
}
|
|
870
|
+
catch (closeError) {
|
|
871
|
+
if (errnoOf(closeError) !== "EBADF")
|
|
872
|
+
throw closeError;
|
|
873
|
+
}
|
|
874
|
+
try {
|
|
875
|
+
unlinkSync(temp);
|
|
876
|
+
}
|
|
877
|
+
catch (cleanup) {
|
|
878
|
+
if (errnoOf(cleanup) !== "ENOENT")
|
|
879
|
+
throw cleanup;
|
|
880
|
+
}
|
|
881
|
+
throw error;
|
|
882
|
+
}
|
|
883
|
+
closeSync(fd);
|
|
884
|
+
try {
|
|
885
|
+
renameSync(temp, target);
|
|
886
|
+
}
|
|
887
|
+
catch (error) {
|
|
888
|
+
try {
|
|
889
|
+
unlinkSync(temp);
|
|
890
|
+
}
|
|
891
|
+
catch (cleanup) {
|
|
892
|
+
if (errnoOf(cleanup) !== "ENOENT")
|
|
893
|
+
throw cleanup;
|
|
894
|
+
}
|
|
895
|
+
throw error;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
let seq = 0;
|
|
899
|
+
const metaCache = new Map();
|
|
900
|
+
/** Test-only pause/check after the run lock is held and before `meta.json` is replaced. */
|
|
901
|
+
let metaWriteBarrier;
|
|
902
|
+
export function setMetaWriteBarrierForTests(barrier) {
|
|
903
|
+
metaWriteBarrier = barrier;
|
|
904
|
+
}
|
|
905
|
+
// Owned snapshots are process-resident. A cheap directory signature catches
|
|
906
|
+
// cross-process index changes before any cached IDs are reused.
|
|
907
|
+
const indexIdsCache = new Map();
|
|
908
|
+
const initializedIndexes = new Set();
|
|
909
|
+
const metaChangedListeners = new Set();
|
|
910
|
+
const registryIo = { fullDirectoryReads: 0, indexDirectoryReads: 0, metadataFileReads: 0, indexRevisionChecks: 0 };
|
|
911
|
+
export function getRegistryIoMetrics() {
|
|
912
|
+
return { ...registryIo };
|
|
913
|
+
}
|
|
914
|
+
export function resetRegistryIoMetrics() {
|
|
915
|
+
registryIo.fullDirectoryReads = 0;
|
|
916
|
+
registryIo.indexDirectoryReads = 0;
|
|
917
|
+
registryIo.metadataFileReads = 0;
|
|
918
|
+
registryIo.indexRevisionChecks = 0;
|
|
919
|
+
}
|
|
920
|
+
/** Monotonic, readable, collision-free run id: `sa_<base36-time>_<seq>`. */
|
|
921
|
+
export function nextRunId() {
|
|
922
|
+
seq += 1;
|
|
923
|
+
return `sa_${Date.now().toString(36)}_${seq}`;
|
|
924
|
+
}
|
|
925
|
+
/** Failure-journal operation for durable archive publication. */
|
|
926
|
+
const ARCHIVE_OPERATION = "durable-archive";
|
|
927
|
+
/**
|
|
928
|
+
* Journal the outcome of publishing one attempt's compact durable record in the
|
|
929
|
+
* run's existing failures journal (`failures.jsonl`, read by the output and
|
|
930
|
+
* result payloads). `undefined` reports a verified recovery.
|
|
931
|
+
*
|
|
932
|
+
* While publication keeps failing, the volatile run directory still holds the
|
|
933
|
+
* only copy of the recipe, so both sweeps retain it; this incident is what makes
|
|
934
|
+
* that state visible instead of silent. Best-effort: journaling never throws
|
|
935
|
+
* into finalization or a sweep, and an untouched run keeps no journal.
|
|
936
|
+
*/
|
|
937
|
+
export function observeArchivePublication(id, error) {
|
|
938
|
+
try {
|
|
939
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id))
|
|
940
|
+
return;
|
|
941
|
+
const path = join(runDir(id), "failures.jsonl");
|
|
942
|
+
const key = failureIdentity(ARCHIVE_OPERATION);
|
|
943
|
+
const open = readFailureState(path).observations[key];
|
|
944
|
+
const at = Date.now();
|
|
945
|
+
if (error === undefined) {
|
|
946
|
+
if (!open || open.status !== "unresolved")
|
|
947
|
+
return;
|
|
948
|
+
observeFailures(path, [{
|
|
949
|
+
id: `archive-published:${id}:${at}`,
|
|
950
|
+
operation: ARCHIVE_OPERATION,
|
|
951
|
+
kind: "recovered",
|
|
952
|
+
at,
|
|
953
|
+
incidents: [open.id],
|
|
954
|
+
summary: "the compact durable continuation record was published",
|
|
955
|
+
}]);
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
// One open incident per run: repeated sweeps retry the same publication and
|
|
959
|
+
// must not fill the journal with the same failure.
|
|
960
|
+
if (open && open.status === "unresolved")
|
|
961
|
+
return;
|
|
962
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
963
|
+
observeFailures(path, [{
|
|
964
|
+
id: `archive-failed:${id}:${at}`,
|
|
965
|
+
operation: ARCHIVE_OPERATION,
|
|
966
|
+
kind: "failure",
|
|
967
|
+
at,
|
|
968
|
+
category: "storage",
|
|
969
|
+
summary: `durable continuation record could not be published (${detail}); this attempt's log and recipe are retained until it is`,
|
|
970
|
+
evidence: `compact record for ${id}`,
|
|
971
|
+
}]);
|
|
972
|
+
}
|
|
973
|
+
catch { /* the journal is best-effort; retaining the run is the safety net */ }
|
|
974
|
+
}
|
|
975
|
+
export function writeMeta(meta, options = {}) {
|
|
976
|
+
if (options.patch && !readMeta(meta.id))
|
|
977
|
+
return;
|
|
978
|
+
mkdirSync(runDir(meta.id), { recursive: true });
|
|
979
|
+
let written = false;
|
|
980
|
+
withRunMetaLock(meta.id, (owns) => {
|
|
981
|
+
if (options.ifAbsent && existsSync(metaPathFor(meta.id)))
|
|
982
|
+
return;
|
|
983
|
+
if (options.patch) {
|
|
984
|
+
const current = readMeta(meta.id);
|
|
985
|
+
if (!current)
|
|
986
|
+
return;
|
|
987
|
+
const patch = options.patch(current);
|
|
988
|
+
if (!patch)
|
|
989
|
+
return;
|
|
990
|
+
meta = { ...current, ...patch, id: meta.id };
|
|
991
|
+
}
|
|
992
|
+
metaWriteBarrier?.();
|
|
993
|
+
if (!owns())
|
|
994
|
+
throw new MetadataLockLost();
|
|
995
|
+
writeMetaFile(meta);
|
|
996
|
+
metaCache.set(meta.id, meta);
|
|
997
|
+
indexMeta(meta);
|
|
998
|
+
// Publish the compact durable continuation record in the same critical
|
|
999
|
+
// section, so a completed attempt is never visible only in volatile
|
|
1000
|
+
// storage. Best-effort: a durable write failure must not break a launch
|
|
1001
|
+
// or finalization (the volatile record still exists). It is journaled
|
|
1002
|
+
// instead, and both sweeps retain the run until publication succeeds.
|
|
1003
|
+
if (meta.status === "completed") {
|
|
1004
|
+
try {
|
|
1005
|
+
if (archiveCompletedAttempt(meta))
|
|
1006
|
+
observeArchivePublication(meta.id);
|
|
1007
|
+
}
|
|
1008
|
+
catch (error) {
|
|
1009
|
+
observeArchivePublication(meta.id, error);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
owns();
|
|
1013
|
+
written = true;
|
|
1014
|
+
});
|
|
1015
|
+
if (options.patch && !written)
|
|
1016
|
+
return;
|
|
1017
|
+
for (const listener of metaChangedListeners) {
|
|
1018
|
+
try {
|
|
1019
|
+
listener(meta);
|
|
1020
|
+
}
|
|
1021
|
+
catch { /* best effort */ }
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
export function onMetaChanged(listener) {
|
|
1025
|
+
metaChangedListeners.add(listener);
|
|
1026
|
+
return () => metaChangedListeners.delete(listener);
|
|
1027
|
+
}
|
|
1028
|
+
export function readMeta(id) {
|
|
1029
|
+
try {
|
|
1030
|
+
registryIo.metadataFileReads += 1;
|
|
1031
|
+
const meta = JSON.parse(readFileSync(metaPathFor(id), "utf-8"));
|
|
1032
|
+
metaCache.set(id, meta);
|
|
1033
|
+
return meta;
|
|
1034
|
+
}
|
|
1035
|
+
catch (error) {
|
|
1036
|
+
metaCache.delete(id);
|
|
1037
|
+
// Only a MISSING volatile record falls back to the durable copy. An
|
|
1038
|
+
// unreadable or corrupt `meta.json` stays authoritative and is reported
|
|
1039
|
+
// as absent: a possibly stale archive must never replace it.
|
|
1040
|
+
if (error.code !== "ENOENT")
|
|
1041
|
+
return undefined;
|
|
1042
|
+
const archived = readArchivedAttempt(id);
|
|
1043
|
+
if (archived)
|
|
1044
|
+
metaCache.set(id, archived);
|
|
1045
|
+
return archived;
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
export function removeMetaArtifacts(meta, options = {}) {
|
|
1049
|
+
try {
|
|
1050
|
+
// A newer failed/killed attempt's volatile record is about to be the only
|
|
1051
|
+
// evidence that the conversation's newest attempt did not complete. Leave
|
|
1052
|
+
// a durable marker first, or an older completed archive would become the
|
|
1053
|
+
// conversation's visible last word and could be continued as if this
|
|
1054
|
+
// attempt had never happened. A marker that cannot be published retains
|
|
1055
|
+
// the run (the throw below): the unsafe direction is deleting both.
|
|
1056
|
+
recordArchiveStatusTombstone(meta);
|
|
1057
|
+
removeRunDirectory(meta.id);
|
|
1058
|
+
if (!options.keepWorkspace
|
|
1059
|
+
&& meta.taskRuntime && /^sa_[a-z0-9]+_[a-z0-9]+$/i.test(meta.id) && meta.cwd === taskWorkspaceDir(meta.id)) {
|
|
1060
|
+
rmSync(taskWorkspaceDir(meta.id), { recursive: true, force: true });
|
|
1061
|
+
}
|
|
1062
|
+
if (meta.taskRuntime && meta.taskScratch && dirname(meta.taskScratch) === realpathSync(tmpdir()) && /^pi-task-scratch-[a-z0-9]{6}$/i.test(basename(meta.taskScratch))) {
|
|
1063
|
+
rmSync(meta.taskScratch, { recursive: true, force: true });
|
|
1064
|
+
}
|
|
1065
|
+
metaCache.delete(meta.id);
|
|
1066
|
+
removeIndexEntry(join(baseDir(), "by-parent", String(meta.spawnPid)), meta.id);
|
|
1067
|
+
removeIndexEntry(join(baseDir(), "by-parent-active", String(meta.spawnPid)), meta.id);
|
|
1068
|
+
removeIndexEntry(join(baseDir(), "by-origin", originKey(originOf(meta))), meta.id);
|
|
1069
|
+
for (const listener of metaChangedListeners) {
|
|
1070
|
+
try {
|
|
1071
|
+
listener(meta);
|
|
1072
|
+
}
|
|
1073
|
+
catch { /* best effort */ }
|
|
1074
|
+
}
|
|
1075
|
+
return true;
|
|
1076
|
+
}
|
|
1077
|
+
catch {
|
|
1078
|
+
return false;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* All runs, newest first: the volatile registry plus durable archived attempts
|
|
1083
|
+
* whose raw log/run directory was already reclaimed. The volatile record wins
|
|
1084
|
+
* whenever both exist, so a reclaimed log never hides the live one.
|
|
1085
|
+
*/
|
|
1086
|
+
export function listMetas(options = {}) {
|
|
1087
|
+
let ids = [];
|
|
1088
|
+
try {
|
|
1089
|
+
registryIo.fullDirectoryReads += 1;
|
|
1090
|
+
ids = readdirSync(join(baseDir(), "runs"));
|
|
1091
|
+
}
|
|
1092
|
+
catch {
|
|
1093
|
+
ids = [];
|
|
1094
|
+
}
|
|
1095
|
+
const seen = new Set();
|
|
1096
|
+
const metas = [];
|
|
1097
|
+
for (const id of ids) {
|
|
1098
|
+
seen.add(id); // A corrupt volatile record must not be hidden by an older archive.
|
|
1099
|
+
const meta = readMetaForSweep(id);
|
|
1100
|
+
if (!meta)
|
|
1101
|
+
continue;
|
|
1102
|
+
metas.push(meta);
|
|
1103
|
+
}
|
|
1104
|
+
for (const meta of options.includeArchived === false ? [] : listArchivedAttempts()) {
|
|
1105
|
+
if (seen.has(meta.id))
|
|
1106
|
+
continue;
|
|
1107
|
+
seen.add(meta.id);
|
|
1108
|
+
metas.push(meta);
|
|
1109
|
+
}
|
|
1110
|
+
return metas.sort((a, b) => b.startedAt - a.startedAt);
|
|
1111
|
+
}
|
|
1112
|
+
/** Why parsed metadata is not a usable run record, or undefined when it is. */
|
|
1113
|
+
export function runMetaShapeProblem(meta, id) {
|
|
1114
|
+
if (!meta || typeof meta !== "object" || Array.isArray(meta))
|
|
1115
|
+
return "not an object";
|
|
1116
|
+
const value = meta;
|
|
1117
|
+
if (value.id !== id)
|
|
1118
|
+
return `id ${JSON.stringify(value.id ?? null)} does not match ${JSON.stringify(id)}`;
|
|
1119
|
+
if (typeof value.status !== "string" || !value.status)
|
|
1120
|
+
return "missing status";
|
|
1121
|
+
if (typeof value.startedAt !== "number" || !Number.isFinite(value.startedAt))
|
|
1122
|
+
return "missing startedAt";
|
|
1123
|
+
return undefined;
|
|
1124
|
+
}
|
|
1125
|
+
function launchAttemptClaimPath(id) {
|
|
1126
|
+
return join(runDir(id), ".launch-attempt.json");
|
|
1127
|
+
}
|
|
1128
|
+
/** Claim this run directory for a launch in progress. The directory must exist. */
|
|
1129
|
+
export function recordLaunchAttempt(id) {
|
|
1130
|
+
const claim = { version: 1, id, pid: process.pid, at: Date.now() };
|
|
1131
|
+
const start = processStartToken(process.pid);
|
|
1132
|
+
if (start)
|
|
1133
|
+
claim.start = start;
|
|
1134
|
+
writeFileSync(launchAttemptClaimPath(id), JSON.stringify(claim), { mode: 0o600 });
|
|
1135
|
+
}
|
|
1136
|
+
/** Drop the claim once `meta.json` is the run's record. Best effort. */
|
|
1137
|
+
export function clearLaunchAttempt(id) {
|
|
1138
|
+
try {
|
|
1139
|
+
unlinkSync(launchAttemptClaimPath(id));
|
|
1140
|
+
}
|
|
1141
|
+
catch { /* no claim, or the attempt's directory is already gone */ }
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Whether a launch claim is still owned by a live process. Conservative for one
|
|
1145
|
+
* direction only: an unreadable, malformed, or token-less claim counts as live,
|
|
1146
|
+
* so the attempt's artifacts are preserved. A dead pid, or a pid whose start
|
|
1147
|
+
* token no longer matches (recycled), is a dead attempt.
|
|
1148
|
+
*/
|
|
1149
|
+
function launchAttemptClaimIsLive(id) {
|
|
1150
|
+
let raw;
|
|
1151
|
+
try {
|
|
1152
|
+
raw = readFileSync(launchAttemptClaimPath(id), "utf-8");
|
|
1153
|
+
}
|
|
1154
|
+
catch (error) {
|
|
1155
|
+
return errnoOf(error) !== "ENOENT";
|
|
1156
|
+
}
|
|
1157
|
+
let parsed;
|
|
1158
|
+
try {
|
|
1159
|
+
parsed = JSON.parse(raw);
|
|
1160
|
+
}
|
|
1161
|
+
catch {
|
|
1162
|
+
return true;
|
|
1163
|
+
}
|
|
1164
|
+
if (!parsed || typeof parsed !== "object")
|
|
1165
|
+
return true;
|
|
1166
|
+
const pid = parsed.pid;
|
|
1167
|
+
if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0)
|
|
1168
|
+
return true;
|
|
1169
|
+
if (!processExists(pid))
|
|
1170
|
+
return false;
|
|
1171
|
+
const start = parsed.start;
|
|
1172
|
+
if (typeof start !== "string" || start.length === 0)
|
|
1173
|
+
return true;
|
|
1174
|
+
const current = cachedStartToken(pid);
|
|
1175
|
+
// An unavailable token does not prove the pid is dead, so the claim stays live.
|
|
1176
|
+
return current === undefined || current === start;
|
|
1177
|
+
}
|
|
1178
|
+
/**
|
|
1179
|
+
* True when a launch is preparing this run id right now, so its directory must
|
|
1180
|
+
* not be treated as debris. Unreadable state counts as in progress.
|
|
1181
|
+
*/
|
|
1182
|
+
export function launchAttemptInProgress(id) {
|
|
1183
|
+
if (!/^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id))
|
|
1184
|
+
return false;
|
|
1185
|
+
return launchAttemptClaimIsLive(id);
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* Read one run directory as an abandoned launch attempt, or `undefined`.
|
|
1189
|
+
*
|
|
1190
|
+
* A launch creates the directory, claims it, writes `prompt.md`, and prepares
|
|
1191
|
+
* the sandbox `control/` dir before the child starts and `meta.json` is
|
|
1192
|
+
* committed. When that preparation throws, or pi dies in that window, only
|
|
1193
|
+
* those artifacts remain: no metadata (nothing was committed) and no
|
|
1194
|
+
* `output.log` (the log is created at spawn and removed when the spawn itself
|
|
1195
|
+
* fails). Such a directory was never a run: it has no owner, no status, no log
|
|
1196
|
+
* and no result, and it can never gain any, so it must not be reported as
|
|
1197
|
+
* unreadable run evidence forever.
|
|
1198
|
+
*
|
|
1199
|
+
* The rule is deliberately narrow in the other direction too. A live launch
|
|
1200
|
+
* claim means the attempt is still being prepared and is never debris; a
|
|
1201
|
+
* directory WITH a log is not debris either, because a child may have started
|
|
1202
|
+
* and its output is real evidence; and a directory without the launch artifacts
|
|
1203
|
+
* is left to the age sweep rather than guessed at.
|
|
1204
|
+
*/
|
|
1205
|
+
export function abandonedLaunchRecord(id) {
|
|
1206
|
+
if (!/^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id))
|
|
1207
|
+
return undefined;
|
|
1208
|
+
let mtimeMs;
|
|
1209
|
+
try {
|
|
1210
|
+
mtimeMs = statSync(runDir(id)).mtimeMs;
|
|
1211
|
+
}
|
|
1212
|
+
catch {
|
|
1213
|
+
return undefined;
|
|
1214
|
+
}
|
|
1215
|
+
// Metadata absence is the claim of debris, so it is re-established as late as
|
|
1216
|
+
// possible: the launch shape first, then every piece of run evidence.
|
|
1217
|
+
if (!existsSync(promptPathFor(id)) && !existsSync(join(runDir(id), "control")))
|
|
1218
|
+
return undefined;
|
|
1219
|
+
if (launchAttemptClaimIsLive(id))
|
|
1220
|
+
return undefined;
|
|
1221
|
+
try {
|
|
1222
|
+
statSync(metaPathFor(id));
|
|
1223
|
+
return undefined;
|
|
1224
|
+
}
|
|
1225
|
+
catch (error) {
|
|
1226
|
+
if (errnoOf(error) !== "ENOENT")
|
|
1227
|
+
return undefined;
|
|
1228
|
+
}
|
|
1229
|
+
try {
|
|
1230
|
+
statSync(logPathFor(id));
|
|
1231
|
+
return undefined;
|
|
1232
|
+
}
|
|
1233
|
+
catch (error) {
|
|
1234
|
+
if (errnoOf(error) !== "ENOENT")
|
|
1235
|
+
return undefined;
|
|
1236
|
+
}
|
|
1237
|
+
return { id, mtimeMs };
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Reclaim one abandoned launch attempt: its run directory, the per-run session
|
|
1241
|
+
* directory a confined launch prepared, and the clone workspace it owned. Only
|
|
1242
|
+
* paths named by the attempt's own id are removed (shared sessions and a
|
|
1243
|
+
* caller-chosen `sandbox_dir` are never touched), exactly like a failed launch.
|
|
1244
|
+
*
|
|
1245
|
+
* Classification runs again inside the same per-run lock `writeMeta` publishes
|
|
1246
|
+
* under, and the removal happens there, so a launch that commits metadata while
|
|
1247
|
+
* the purger waits is preserved instead of deleted. A held or unavailable lock
|
|
1248
|
+
* is a live writer, so it is preserved too. This closes the race the earlier
|
|
1249
|
+
* shape-only rule lost (#623 review R1): no deletion of a live attempt's
|
|
1250
|
+
* metadata or log, and no orphaned child left behind by a purge.
|
|
1251
|
+
*/
|
|
1252
|
+
export function purgeAbandonedLaunch(id) {
|
|
1253
|
+
if (!abandonedLaunchRecord(id))
|
|
1254
|
+
return "not-debris";
|
|
1255
|
+
let outcome = "retained";
|
|
1256
|
+
try {
|
|
1257
|
+
withRunMetaLock(id, (owns) => {
|
|
1258
|
+
if (!owns())
|
|
1259
|
+
throw new MetadataLockLost();
|
|
1260
|
+
// Re-establish the negative evidence under the publication exclusion.
|
|
1261
|
+
if (!abandonedLaunchRecord(id)) {
|
|
1262
|
+
outcome = "not-debris";
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
discardFailedLaunch(id, { sessionDir: join(sessionsDir(), id), workspaceDir: taskWorkspaceDir(id) });
|
|
1266
|
+
outcome = "purged";
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
catch {
|
|
1270
|
+
// A live writer (or an unreadable run directory) is never safe to delete.
|
|
1271
|
+
// A directory that disappeared meanwhile is no longer debris.
|
|
1272
|
+
return abandonedLaunchRecord(id) ? "retained" : "not-debris";
|
|
1273
|
+
}
|
|
1274
|
+
if (outcome === "purged") {
|
|
1275
|
+
try {
|
|
1276
|
+
statSync(runDir(id));
|
|
1277
|
+
outcome = "retained";
|
|
1278
|
+
}
|
|
1279
|
+
catch (error) {
|
|
1280
|
+
if (errnoOf(error) !== "ENOENT")
|
|
1281
|
+
outcome = "retained";
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
return outcome;
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* Inspect one run's metadata without collapsing "missing" and "corrupt":
|
|
1288
|
+
* `missing` only when neither metadata nor the run directory exists.
|
|
1289
|
+
*/
|
|
1290
|
+
export function inspectRunMeta(id) {
|
|
1291
|
+
let raw;
|
|
1292
|
+
try {
|
|
1293
|
+
registryIo.metadataFileReads += 1;
|
|
1294
|
+
raw = readFileSync(metaPathFor(id), "utf-8");
|
|
1295
|
+
}
|
|
1296
|
+
catch (error) {
|
|
1297
|
+
metaCache.delete(id);
|
|
1298
|
+
const code = error.code;
|
|
1299
|
+
if (code === "ENOENT") {
|
|
1300
|
+
try {
|
|
1301
|
+
statSync(runDir(id));
|
|
1302
|
+
return { kind: "unreadable", detail: "metadata file is missing; the run directory and its evidence are retained" };
|
|
1303
|
+
}
|
|
1304
|
+
catch {
|
|
1305
|
+
const archived = readArchivedAttempt(id);
|
|
1306
|
+
return archived ? { kind: "ok", meta: archived } : { kind: "missing" };
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
return { kind: "unreadable", detail: error instanceof Error ? error.message : String(error) };
|
|
1310
|
+
}
|
|
1311
|
+
let meta;
|
|
1312
|
+
try {
|
|
1313
|
+
meta = JSON.parse(raw);
|
|
1314
|
+
}
|
|
1315
|
+
catch (error) {
|
|
1316
|
+
metaCache.delete(id);
|
|
1317
|
+
return { kind: "unreadable", detail: `invalid JSON: ${error instanceof Error ? error.message : String(error)}` };
|
|
1318
|
+
}
|
|
1319
|
+
const invalid = runMetaShapeProblem(meta, id);
|
|
1320
|
+
if (invalid) {
|
|
1321
|
+
metaCache.delete(id);
|
|
1322
|
+
return { kind: "unreadable", detail: `invalid metadata: ${invalid}` };
|
|
1323
|
+
}
|
|
1324
|
+
metaCache.set(id, meta);
|
|
1325
|
+
return { kind: "ok", meta };
|
|
1326
|
+
}
|
|
1327
|
+
/** Every run directory, counting unreadable records instead of dropping them. */
|
|
1328
|
+
export function listRunRecords() {
|
|
1329
|
+
let ids = [];
|
|
1330
|
+
try {
|
|
1331
|
+
registryIo.fullDirectoryReads += 1;
|
|
1332
|
+
ids = readdirSync(join(baseDir(), "runs"));
|
|
1333
|
+
}
|
|
1334
|
+
catch (error) {
|
|
1335
|
+
const code = error.code;
|
|
1336
|
+
if (code !== "ENOENT")
|
|
1337
|
+
return { metas: [], unreadable: [], abandoned: [], indexError: error instanceof Error ? error.message : String(error) };
|
|
1338
|
+
}
|
|
1339
|
+
const metas = [];
|
|
1340
|
+
const unreadable = [];
|
|
1341
|
+
const abandoned = [];
|
|
1342
|
+
for (const id of ids) {
|
|
1343
|
+
const cached = metaCache.get(id);
|
|
1344
|
+
if (cached && cached.status !== "running" && cached.status !== "orphaned") {
|
|
1345
|
+
metas.push(cached);
|
|
1346
|
+
continue;
|
|
1347
|
+
}
|
|
1348
|
+
const inspected = inspectRunMeta(id);
|
|
1349
|
+
if (inspected.kind === "ok")
|
|
1350
|
+
metas.push(inspected.meta);
|
|
1351
|
+
else if (inspected.kind === "unreadable") {
|
|
1352
|
+
if (abandonedLaunchRecord(id))
|
|
1353
|
+
abandoned.push(id);
|
|
1354
|
+
else
|
|
1355
|
+
unreadable.push({ id, detail: inspected.detail });
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
const volatileIds = new Set(ids);
|
|
1359
|
+
for (const archived of listArchivedAttempts()) {
|
|
1360
|
+
if (!volatileIds.has(archived.id))
|
|
1361
|
+
metas.push(archived);
|
|
1362
|
+
}
|
|
1363
|
+
metas.sort((a, b) => b.startedAt - a.startedAt);
|
|
1364
|
+
return { metas, unreadable, abandoned };
|
|
1365
|
+
}
|
|
1366
|
+
export function listMetasForParent(parentPid) {
|
|
1367
|
+
const directory = join(baseDir(), "by-parent", String(parentPid));
|
|
1368
|
+
ensureIndex(directory, (meta) => meta.spawnPid === parentPid);
|
|
1369
|
+
return readIndexedMetas(directory).filter((meta) => meta.spawnPid === parentPid);
|
|
1370
|
+
}
|
|
1371
|
+
export function listActiveMetasForParent(parentPid) {
|
|
1372
|
+
const directory = join(baseDir(), "by-parent-active", String(parentPid));
|
|
1373
|
+
ensureActiveParentIndex(directory, parentPid);
|
|
1374
|
+
return readIndexedMetas(directory)
|
|
1375
|
+
.filter((meta) => meta.spawnPid === parentPid && isActiveStatus(meta.status));
|
|
1376
|
+
}
|
|
1377
|
+
export function listMetasForOrigin(origin) {
|
|
1378
|
+
const directory = join(baseDir(), "by-origin", originKey(origin));
|
|
1379
|
+
ensureIndex(directory, (meta) => belongsToOrigin(meta, origin));
|
|
1380
|
+
return readIndexedMetas(directory).filter((meta) => belongsToOrigin(meta, origin));
|
|
1381
|
+
}
|
|
1382
|
+
function readMetaForSweep(id) {
|
|
1383
|
+
const cached = metaCache.get(id);
|
|
1384
|
+
if (cached && cached.status !== "running" && cached.status !== "orphaned")
|
|
1385
|
+
return cached;
|
|
1386
|
+
return readMeta(id);
|
|
1387
|
+
}
|
|
1388
|
+
function readIndexedMetas(directory) {
|
|
1389
|
+
return readIndexIds(directory)
|
|
1390
|
+
.map((id) => metaCache.get(id) ?? readMeta(id))
|
|
1391
|
+
.filter((meta) => meta !== undefined)
|
|
1392
|
+
.sort((a, b) => b.startedAt - a.startedAt);
|
|
1393
|
+
}
|
|
1394
|
+
function indexMeta(meta) {
|
|
1395
|
+
try {
|
|
1396
|
+
writeIndexEntry(join(baseDir(), "by-parent", String(meta.spawnPid)), meta.id);
|
|
1397
|
+
const activeDirectory = join(baseDir(), "by-parent-active", String(meta.spawnPid));
|
|
1398
|
+
if (isActiveStatus(meta.status))
|
|
1399
|
+
writeIndexEntry(activeDirectory, meta.id);
|
|
1400
|
+
else
|
|
1401
|
+
removeIndexEntry(activeDirectory, meta.id);
|
|
1402
|
+
writeIndexEntry(join(baseDir(), "by-origin", originKey(originOf(meta))), meta.id);
|
|
1403
|
+
}
|
|
1404
|
+
catch {
|
|
1405
|
+
// Indexes are accelerators; meta.json remains authoritative.
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
function writeIndexEntry(directory, id) {
|
|
1409
|
+
mkdirSync(directory, { recursive: true });
|
|
1410
|
+
const cached = indexIdsCache.get(directory);
|
|
1411
|
+
const cacheWasCurrent = cached ? cached.signature === indexDirectorySignature(directory) : false;
|
|
1412
|
+
try {
|
|
1413
|
+
writeFileSync(join(directory, id), "", { flag: "wx" });
|
|
1414
|
+
}
|
|
1415
|
+
catch (error) {
|
|
1416
|
+
if (error.code !== "EEXIST")
|
|
1417
|
+
throw error;
|
|
1418
|
+
}
|
|
1419
|
+
if (cached && cacheWasCurrent) {
|
|
1420
|
+
cached.ids.add(id);
|
|
1421
|
+
cached.signature = indexDirectorySignature(directory);
|
|
1422
|
+
}
|
|
1423
|
+
else if (cached) {
|
|
1424
|
+
indexIdsCache.delete(directory);
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
function removeIndexEntry(directory, id) {
|
|
1428
|
+
const cached = indexIdsCache.get(directory);
|
|
1429
|
+
const cacheWasCurrent = cached ? cached.signature === indexDirectorySignature(directory) : false;
|
|
1430
|
+
try {
|
|
1431
|
+
unlinkSync(join(directory, id));
|
|
1432
|
+
}
|
|
1433
|
+
catch { /* stale index entries are harmless */ }
|
|
1434
|
+
if (cached && cacheWasCurrent) {
|
|
1435
|
+
cached.ids.delete(id);
|
|
1436
|
+
cached.signature = indexDirectorySignature(directory);
|
|
1437
|
+
}
|
|
1438
|
+
else if (cached) {
|
|
1439
|
+
indexIdsCache.delete(directory);
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
function readIndexIds(directory) {
|
|
1443
|
+
const signature = indexDirectorySignature(directory);
|
|
1444
|
+
const cached = indexIdsCache.get(directory);
|
|
1445
|
+
if (cached && cached.signature === signature)
|
|
1446
|
+
return [...cached.ids];
|
|
1447
|
+
try {
|
|
1448
|
+
registryIo.indexDirectoryReads += 1;
|
|
1449
|
+
const ids = new Set(readdirSync(directory).filter((id) => id !== ".initialized"));
|
|
1450
|
+
indexIdsCache.set(directory, { ids, signature: indexDirectorySignature(directory) });
|
|
1451
|
+
return [...ids];
|
|
1452
|
+
}
|
|
1453
|
+
catch {
|
|
1454
|
+
indexIdsCache.delete(directory);
|
|
1455
|
+
return [];
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
function isActiveStatus(status) {
|
|
1459
|
+
return status === "running" || status === "orphaned";
|
|
1460
|
+
}
|
|
1461
|
+
function indexDirectorySignature(directory) {
|
|
1462
|
+
registryIo.indexRevisionChecks += 1;
|
|
1463
|
+
try {
|
|
1464
|
+
const stat = statSync(directory);
|
|
1465
|
+
return `${stat.dev}:${stat.ino}:${stat.mtimeMs}:${stat.ctimeMs}`;
|
|
1466
|
+
}
|
|
1467
|
+
catch {
|
|
1468
|
+
return "missing";
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
function ensureIndex(directory, matches) {
|
|
1472
|
+
if (initializedIndexes.has(directory))
|
|
1473
|
+
return;
|
|
1474
|
+
try {
|
|
1475
|
+
readFileSync(join(directory, ".initialized"));
|
|
1476
|
+
initializedIndexes.add(directory);
|
|
1477
|
+
return;
|
|
1478
|
+
}
|
|
1479
|
+
catch {
|
|
1480
|
+
// Existing registries are backfilled once for each owner.
|
|
1481
|
+
}
|
|
1482
|
+
indexIdsCache.delete(directory);
|
|
1483
|
+
const owned = listMetas().filter(matches);
|
|
1484
|
+
mkdirSync(directory, { recursive: true });
|
|
1485
|
+
for (const meta of owned)
|
|
1486
|
+
writeIndexEntry(directory, meta.id);
|
|
1487
|
+
writeFileSync(join(directory, ".initialized"), "1");
|
|
1488
|
+
initializedIndexes.add(directory);
|
|
1489
|
+
}
|
|
1490
|
+
function ensureActiveParentIndex(directory, parentPid) {
|
|
1491
|
+
if (initializedIndexes.has(directory))
|
|
1492
|
+
return;
|
|
1493
|
+
try {
|
|
1494
|
+
readFileSync(join(directory, ".initialized"));
|
|
1495
|
+
initializedIndexes.add(directory);
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
catch {
|
|
1499
|
+
// Build the active set from the already owner-scoped parent index.
|
|
1500
|
+
}
|
|
1501
|
+
indexIdsCache.delete(directory);
|
|
1502
|
+
const active = listMetasForParent(parentPid).filter((meta) => isActiveStatus(meta.status));
|
|
1503
|
+
mkdirSync(directory, { recursive: true });
|
|
1504
|
+
for (const meta of active)
|
|
1505
|
+
writeIndexEntry(directory, meta.id);
|
|
1506
|
+
writeFileSync(join(directory, ".initialized"), "1");
|
|
1507
|
+
initializedIndexes.add(directory);
|
|
1508
|
+
}
|
|
1509
|
+
export function originOf(meta) {
|
|
1510
|
+
return meta.callbackOrigin ?? { cwd: meta.cwd };
|
|
1511
|
+
}
|
|
1512
|
+
export function belongsToOrigin(meta, origin) {
|
|
1513
|
+
const candidate = originOf(meta);
|
|
1514
|
+
if (candidate.cwd !== origin.cwd)
|
|
1515
|
+
return false;
|
|
1516
|
+
if (candidate.sessionId || origin.sessionId)
|
|
1517
|
+
return candidate.sessionId === origin.sessionId;
|
|
1518
|
+
return true;
|
|
1519
|
+
}
|
|
1520
|
+
export function originKey(origin) {
|
|
1521
|
+
return createHash("sha256")
|
|
1522
|
+
.update(origin.cwd)
|
|
1523
|
+
.update("\0")
|
|
1524
|
+
.update(origin.sessionId ?? "")
|
|
1525
|
+
.digest("hex")
|
|
1526
|
+
.slice(0, 24);
|
|
1527
|
+
}
|
|
1528
|
+
/**
|
|
1529
|
+
* Reconcile the recorded status with reality for display. A run marked
|
|
1530
|
+
* "running" whose PID is no longer alive exited without our handler firing
|
|
1531
|
+
* (foreground pi was closed / restarted) — surface that as "exited".
|
|
1532
|
+
*/
|
|
1533
|
+
export function effectiveStatus(meta) {
|
|
1534
|
+
if (meta.status !== "running")
|
|
1535
|
+
return meta.status;
|
|
1536
|
+
if (processExists(meta.pid))
|
|
1537
|
+
return "running";
|
|
1538
|
+
return "exited";
|
|
1539
|
+
}
|
|
1540
|
+
/**
|
|
1541
|
+
* True when a status can yield a FINAL result: everything except `running`
|
|
1542
|
+
* and the non-terminal `orphaned`. `lost` is terminal (best-available
|
|
1543
|
+
* artifacts, never a completion); `exited` keeps its historic resultable
|
|
1544
|
+
* treatment. Used by subagent_result's gate.
|
|
1545
|
+
*/
|
|
1546
|
+
export function isFinalResultStatus(status) {
|
|
1547
|
+
return status !== "running" && status !== "orphaned";
|
|
1548
|
+
}
|
|
1549
|
+
/** True when this meta was spawned by the given parent pi PID (default: this process). */
|
|
1550
|
+
export function ownedByThisParent(meta, parentPid = process.pid) {
|
|
1551
|
+
return meta.spawnPid === parentPid;
|
|
1552
|
+
}
|
|
1553
|
+
/** True when the run has been dismissed from the human navigator. */
|
|
1554
|
+
export function isDismissed(meta) {
|
|
1555
|
+
return typeof meta.dismissedAt === "number";
|
|
1556
|
+
}
|
|
1557
|
+
/**
|
|
1558
|
+
* Mark a run dismissed from the navigator, durably. Idempotent: the first
|
|
1559
|
+
* dismissal timestamp wins. Returns the updated meta, or undefined for an
|
|
1560
|
+
* unknown id. Only the timestamp changes — all other metadata is preserved.
|
|
1561
|
+
*/
|
|
1562
|
+
export function dismissRun(id, at = Date.now()) {
|
|
1563
|
+
const meta = readMeta(id);
|
|
1564
|
+
if (!meta)
|
|
1565
|
+
return undefined;
|
|
1566
|
+
if (meta.dismissedAt === undefined) {
|
|
1567
|
+
meta.dismissedAt = at;
|
|
1568
|
+
writeMeta(meta);
|
|
1569
|
+
}
|
|
1570
|
+
return meta;
|
|
1571
|
+
}
|
|
1572
|
+
/**
|
|
1573
|
+
* Visible navigator runs: current-parent runs that are not dismissed. This is
|
|
1574
|
+
* the single visibility calculation shared by the navigator list and the
|
|
1575
|
+
* footer count, so they can never drift apart. `subagent_list` does NOT use
|
|
1576
|
+
* this — the model-facing list keeps showing dismissed runs.
|
|
1577
|
+
*/
|
|
1578
|
+
export function navigatorVisibleRuns(metas, parentPid = process.pid) {
|
|
1579
|
+
return metas.filter((m) => ownedByThisParent(m, parentPid) && !isDismissed(m));
|
|
1580
|
+
}
|
|
1581
|
+
/** Footer-count seam: how many runs the navigator affordance advertises. */
|
|
1582
|
+
export function navigatorVisibleCount(metas, parentPid = process.pid) {
|
|
1583
|
+
return navigatorVisibleRuns(metas, parentPid).length;
|
|
1584
|
+
}
|