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,1142 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { advertiseBackgroundAdapter } from "./adapter-contract.js";
|
|
4
|
+
import { refreshBackgroundTasksNavigator } from "./navigator-provider.js";
|
|
5
|
+
import {
|
|
6
|
+
classifyOwnership,
|
|
7
|
+
formatLaunch,
|
|
8
|
+
formatList,
|
|
9
|
+
formatLog,
|
|
10
|
+
formatMissingTask,
|
|
11
|
+
formatMutationRefusal,
|
|
12
|
+
formatStatus,
|
|
13
|
+
formatDurableStatusMetadata,
|
|
14
|
+
formatStopResult,
|
|
15
|
+
type OutputOptions,
|
|
16
|
+
} from "./output.js";
|
|
17
|
+
import { cursorKind, readOutputControls } from "./shared-log-utils.js";
|
|
18
|
+
import { cancelCallbackBatch, getCallbackBatcher, setCallbackBatchContext } from "./shared-callback-batcher.js";
|
|
19
|
+
import { registerDeliveryObserver } from "./delivery-observer.js";
|
|
20
|
+
import { getObservedWaitHost } from "./observed-wait-client.js";
|
|
21
|
+
import type { DurableTaskKind, DurableLaunch, WaitView } from "./observed-waits.js";
|
|
22
|
+
import { backgroundListNeedsHistory, backgroundObservation } from "./work-observation.js";
|
|
23
|
+
import { inspectMeta, listMetasForOrigin, readMeta, writeMeta } from "./registry.js";
|
|
24
|
+
import type { WatchTaskParams, BackgroundTaskRuntime } from "./runtime.js";
|
|
25
|
+
import { backgroundProcessDiagnosticsAccess, ForegroundSandboxBlockedError } from "./sandbox.js";
|
|
26
|
+
import { inspectTaskProcess } from "./task-diagnostics.js";
|
|
27
|
+
import type { BackgroundTaskCallbackOrigin, BackgroundTaskMeta } from "./types.js";
|
|
28
|
+
import { FIRST_WATCH_CHECK_WAIT_MS, isTerminalStatus } from "./types.js";
|
|
29
|
+
|
|
30
|
+
export { formatLaunch } from "./output.js";
|
|
31
|
+
|
|
32
|
+
type ToolRuntime = Pick<BackgroundTaskRuntime, "awaitFirstWatchCheck" | "resumeRunningTask" | "spawnTask" | "startWatchTask" | "stopTask">;
|
|
33
|
+
|
|
34
|
+
const JsonPathSchema = Type.String({
|
|
35
|
+
pattern: "^\\$",
|
|
36
|
+
description: "Root-prefixed JSON path, e.g. $.status, $.terminalFailure, or $.steps[0].status. Bare keys and wildcards are unsupported.",
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const ConditionSchema = Type.Union([
|
|
40
|
+
Type.Object({ type: Type.Literal("exit_code"), equals: Type.Number() }),
|
|
41
|
+
Type.Object({ type: Type.Literal("stdout_contains"), value: Type.String() }),
|
|
42
|
+
Type.Object({ type: Type.Literal("stderr_contains"), value: Type.String() }),
|
|
43
|
+
Type.Object({ type: Type.Literal("json_path_equals"), path: JsonPathSchema, value: Type.Any() }),
|
|
44
|
+
Type.Object({ type: Type.Literal("json_path_exists"), path: JsonPathSchema }),
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* String-to-string map declared with additionalProperties. Equivalent to
|
|
49
|
+
* Type.Record(Type.String(), Type.String()) for accepted values; kept as the
|
|
50
|
+
* plainer JSON Schema form.
|
|
51
|
+
*/
|
|
52
|
+
function StringMap(description: string) {
|
|
53
|
+
return Type.Unsafe<Record<string, string>>({ type: "object", additionalProperties: { type: "string" }, description });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const SshSchema = Type.Object({
|
|
57
|
+
host: Type.String({ description: "SSH host. Required when ssh is set." }),
|
|
58
|
+
user: Type.Optional(Type.String({ description: "SSH user." })),
|
|
59
|
+
port: Type.Optional(Type.Integer({ minimum: 1, maximum: 65_535, description: "SSH port." })),
|
|
60
|
+
identity_file: Type.Optional(Type.String({ description: "SSH identity file path." })),
|
|
61
|
+
jump: Type.Optional(Type.String({ description: "SSH jump host passed with -J." })),
|
|
62
|
+
options: Type.Optional(StringMap("Additional SSH -o key/value options. Agent-safe defaults remain enforced.")),
|
|
63
|
+
}, {
|
|
64
|
+
description: "Structured SSH connection for remote background tasks. Set ssh instead of wrapping command in a hand-written ssh command; command is the remote command, and Pi keeps durable local logs, status, callbacks, and remote stop semantics.",
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const RemoteSchema = Type.Object({
|
|
68
|
+
session: Type.Optional(Type.Union([Type.Literal("tmux"), Type.Literal("direct")], { description: "Remote session mode. SSH spawn defaults to durable tmux. Watch always uses direct one-shot polls. Explicit direct spawn has weaker stop semantics." })),
|
|
69
|
+
install_tmux: Type.Optional(Type.Boolean({ description: "Allow SSH spawn to install tmux non-interactively when missing. Defaults true in tmux mode and is ignored for watch and direct spawn." })),
|
|
70
|
+
workdir: Type.Optional(Type.String({ description: "Remote working directory for the spawned command." })),
|
|
71
|
+
}, {
|
|
72
|
+
description: "Remote execution controls used with ssh. Omit session for SSH spawn to get the durable tmux default; SSH watch runs direct one-shot polls regardless of session and does not install tmux.",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Structured intent (#325). Validated in code before launch (no uniqueItems/pattern: some providers reject them).
|
|
76
|
+
// Each field also admits null, which means "not declared": models send optional fields as explicit
|
|
77
|
+
// null, and readTaskIntent treats null exactly like an omitted field on every Pi version. Plain
|
|
78
|
+
// JSON Schema `anyOf` (Type.Unsafe), not Type.Union: Pi 0.82 converts a null to "" or [] through a
|
|
79
|
+
// TypeBox union, and a Null-first union reports a malformed array as "must be null".
|
|
80
|
+
const IntentFields = {
|
|
81
|
+
operation_id: Type.Optional(Type.Unsafe<string | null>({ anyOf: [{ type: "null" }, { type: "string" }], description: "Optional stable id (letters, digits, . _ : / -; up to 64) for one logical operation. Reuse it when you retry with a changed command, scope, or timeout: when a later task with the same operation_id succeeds, earlier failures of that operation in this session are recovered." })),
|
|
82
|
+
expected_exit_codes: Type.Optional(Type.Unsafe<number[] | null>({ anyOf: [{ type: "array", items: { type: "integer", minimum: 0, maximum: 255 }, minItems: 1, maxItems: 16 }, { type: "null" }], description: "Optional distinct non-zero exit codes that are intentional for this command (e.g. [1] for a no-match probe). 0 is allowed and ignored: exit 0 is already success. Declared before launch; recorded as expected failures, not incidents needing action. Signals and timeouts are never expected." })),
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const CommandFields = {
|
|
86
|
+
name: Type.Optional(Type.String({ description: "Human-readable task label." })),
|
|
87
|
+
command: Type.Optional(Type.String({ description: "Shell command to run, or the remote command when ssh is set. Required unless shell:false with argv is used." })),
|
|
88
|
+
argv: Type.Optional(Type.Array(Type.String(), { description: "Argument vector. Use with shell:false to avoid shell parsing." })),
|
|
89
|
+
shell: Type.Optional(Type.Boolean({ description: "Run command through the package's bash-compatible shell. Default true. When the Background tasks sandbox profile turns Shell scripts off, a shell command and a direct shell executable are refused; pass argv with shell:false for a direct executable." })),
|
|
90
|
+
cwd: Type.Optional(Type.String({ description: "Working directory. Defaults to the current pi cwd." })),
|
|
91
|
+
env: Type.Optional(StringMap("Extra environment variables.")),
|
|
92
|
+
max_log_bytes: Type.Optional(Type.Number({ description: "Maximum retained raw-log bytes. Default 4194304 (4 MiB). Older output is compacted while the task runs." })),
|
|
93
|
+
callback: Type.Optional(Type.Boolean({ description: "Queue a follow-up when the task reaches a terminal state. Default true." })),
|
|
94
|
+
timeout_seconds: Type.Optional(Type.Number({ description: "Optional whole-task timeout in seconds. Command watchers default to 900 seconds when omitted; pass 0 to disable. Spawned processes have no default timeout. When the Background tasks sandbox profile sets a command time limit, that cap applies to each actual command (and each watch check) independently: a smaller timeout_seconds still wins, and 0 does not disable the policy cap." })),
|
|
95
|
+
ssh: Type.Optional(SshSchema),
|
|
96
|
+
remote: Type.Optional(RemoteSchema),
|
|
97
|
+
...IntentFields,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Opt-in step gate (#619). Chained shell commands report only the last command's
|
|
102
|
+
* status, so a failing test run followed by summary commands reads as success at
|
|
103
|
+
* the status surface. The gate runs the command under bash errexit so its first
|
|
104
|
+
* unhandled failing step decides the task's exit code.
|
|
105
|
+
*/
|
|
106
|
+
const StepGateField = Type.Optional(Type.Boolean({
|
|
107
|
+
description: "Spawn only: opt in to a step gate for a shell command. The command runs under bash errexit, so its first unhandled failing step ends the task with that step's exit code and a later command cannot mask it; status reports that exit with `(step gate)` and names the failing step. Handled failures (`|| true`, `if`/`while`/`until` conditions, `!`) are not gate failures. Not supported with shell:false or ssh. Default false (ordinary shell semantics).",
|
|
108
|
+
}));
|
|
109
|
+
|
|
110
|
+
const SpawnParams = Type.Object({ ...CommandFields, require_successful_steps: StepGateField });
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Batch launch. A job is exactly a spawn, so the job schema is the spawn schema;
|
|
114
|
+
* `shared` is that schema minus per-job identity, and a shared `name`/`command`/
|
|
115
|
+
* `argv` is refused here rather than silently applied to every job that omits it.
|
|
116
|
+
*/
|
|
117
|
+
const BatchJobParams = Type.Object({
|
|
118
|
+
...CommandFields,
|
|
119
|
+
require_successful_steps: StepGateField,
|
|
120
|
+
}, { description: "One independent background task. Supply its own command, or shell:false with argv; one invalid job refuses the whole batch before anything starts." });
|
|
121
|
+
|
|
122
|
+
const BatchSharedParams = Type.Omit(
|
|
123
|
+
Type.Object({ ...CommandFields, require_successful_steps: StepGateField }),
|
|
124
|
+
["name", "command", "argv"],
|
|
125
|
+
{
|
|
126
|
+
additionalProperties: false,
|
|
127
|
+
description: "Launch options applied to every job. A job's own value wins, and an explicit false, 0, or null overrides the shared value instead of inheriting it. name, command, and argv are per job: set them on each job, not here.",
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const BatchParams = Type.Object({
|
|
132
|
+
batchName: Type.Optional(Type.String({ description: "Optional label for the whole batch. Recorded on every launched task and shown in the launch result." })),
|
|
133
|
+
shared: Type.Optional(BatchSharedParams),
|
|
134
|
+
jobs: Type.Array(BatchJobParams, {
|
|
135
|
+
minItems: 1,
|
|
136
|
+
maxItems: 100,
|
|
137
|
+
description: "One to 100 independent jobs. The whole batch is refused before launch if any job is invalid.",
|
|
138
|
+
}),
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const BlindChecksField = Type.Optional(Type.Integer({ minimum: 0, description: "Flag the watch as possibly blind after this many checks in a row exit 0, write stderr, and match neither success_when nor failure_when: one incident that needs action, with the latest stderr line. The watch keeps running; a check with no stderr or a matched condition recovers it. Default 3; 0 turns it off." }));
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* How to write a watch check (#359): a check that swallows its own errors reports "pending"
|
|
145
|
+
* forever. Shared by bg_task_watch and the bg_task wrapper.
|
|
146
|
+
*/
|
|
147
|
+
const WATCH_CHECK_GUIDANCE = "Waits up to 15s for the first check and returns its exit code with stdout and stderr tails; if it is still running, says so. "
|
|
148
|
+
+ "Write the check so a broken check is visible: do not end it with `exit 0` or `|| true`, because a check that exits non-zero is recorded and escalates. "
|
|
149
|
+
+ "Map an unknown or unparseable state to failure (exit non-zero), not to pending. Prefer structured output, e.g. `--format=json | jq -er '.status'`, over fragile format strings. "
|
|
150
|
+
+ "A check that exits 0 but writes stderr without matching a condition for blind_checks (default 3) checks in a row is flagged as needing action; if that stderr is expected, redirect it (2>/dev/null) or set blind_checks:0.";
|
|
151
|
+
|
|
152
|
+
const WatchParams = Type.Object({
|
|
153
|
+
...CommandFields,
|
|
154
|
+
interval_seconds: Type.Optional(Type.Number({ description: "Polling interval in seconds. Default 30." })),
|
|
155
|
+
success_when: ConditionSchema,
|
|
156
|
+
failure_when: Type.Optional(ConditionSchema),
|
|
157
|
+
blind_checks: BlindChecksField,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const CursorFields = {
|
|
161
|
+
cursor: Type.Optional(Type.String({ description: "Caller-owned continuation cursor from a previous page. Replay returns the same page or a no-change/failure-only notice; nextCursor continues. Independent callers do not consume each other. Cursors are bound to the selected session scope." })),
|
|
162
|
+
max_bytes: Type.Optional(Type.Number({ description: "Optional UTF-8 byte budget. Defaults: status/log/list 1 KiB, raw evidence 16 KiB. Larger explicit pages are allowed up to the shared hard cap (status 2 KiB, log/list 4 KiB, raw 64 KiB). Hard caps are OUTPUT_BUDGET_MAX_BYTES." })),
|
|
163
|
+
maxBytes: Type.Optional(Type.Number({ description: "Deprecated alias for max_bytes. max_bytes wins when both are given." })),
|
|
164
|
+
all: Type.Optional(Type.Boolean({ description: "Inspect, list, stop, or clear-by-id tasks across every session. Default false is current-session only. Unknown ownership is reported as a gap, never as missing or healthy; stop and clear refuse tasks outside the current session." })),
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const IdParams = Type.Object({
|
|
168
|
+
id: Type.String({ description: "Background task id." }),
|
|
169
|
+
verbose: Type.Optional(Type.Boolean({ description: "Return raw metadata JSON with environment values omitted, bounded like raw evidence (16 KiB default, max_bytes up to 64 KiB, paged with cursor when larger). Default false returns the compact model-facing summary. Use true only for debugging or explicit recovery." })),
|
|
170
|
+
...CursorFields,
|
|
171
|
+
});
|
|
172
|
+
const HistoryField = {
|
|
173
|
+
diagnostics: Type.Optional(Type.Boolean({ description: "For status: sample registered task process identity, CPU time, state and wait evidence. Requires Process access Read in each enabled sandbox profile (Main and Background tasks); unavailable under Main confinement. No arguments, environment or remote probes; a snapshot cannot prove useful work or a hang." })),
|
|
174
|
+
history: Type.Optional(Type.Boolean({ description: "If true, return an incident page that also lists failure history: expected failures and recovered or superseded incidents. Default output lists only failures that need action and counts the rest." })),
|
|
175
|
+
};
|
|
176
|
+
const StatusParams = Type.Object({
|
|
177
|
+
id: Type.String({ description: "Background task id." }),
|
|
178
|
+
verbose: Type.Optional(Type.Boolean({ description: "Return raw metadata JSON with environment values omitted, bounded like raw evidence (16 KiB default, max_bytes up to 64 KiB, paged with cursor when larger). Default false returns the compact model-facing summary. Use true only for debugging or explicit recovery." })),
|
|
179
|
+
...HistoryField,
|
|
180
|
+
...CursorFields,
|
|
181
|
+
});
|
|
182
|
+
const ListParams = Type.Object({
|
|
183
|
+
status: Type.Optional(Type.Array(Type.String({ description: "Statuses to include." }))),
|
|
184
|
+
limit: Type.Optional(Type.Number({ description: "Maximum tasks to show. Default 10, max 100." })),
|
|
185
|
+
...CursorFields,
|
|
186
|
+
});
|
|
187
|
+
const LogParams = Type.Object({
|
|
188
|
+
id: Type.String({ description: "Background task id." }),
|
|
189
|
+
lines: Type.Optional(Type.Number({ description: "Number of trailing display rows. Default 10 for compact model ingestion. Set 0 to page retained raw bytes from the oldest retained offset." })),
|
|
190
|
+
tail_lines: Type.Optional(Type.Number({ description: "Deprecated alias for lines. lines wins when both are given." })),
|
|
191
|
+
raw: Type.Optional(Type.Boolean({ description: "Page retained raw log bytes (16 KiB default, 64 KiB hard cap) instead of the compact excerpt. Capture and retention loss are disclosed; this is not a full-history archive." })),
|
|
192
|
+
...CursorFields,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const ActionParams = Type.Object({
|
|
196
|
+
request_id: Type.Optional(Type.String({ description: "Stable cancellation request identity for action:stop. Reuse after disconnect." })),
|
|
197
|
+
action: Type.Union([
|
|
198
|
+
Type.Literal("spawn"),
|
|
199
|
+
Type.Literal("watch"),
|
|
200
|
+
Type.Literal("list"),
|
|
201
|
+
Type.Literal("status"),
|
|
202
|
+
Type.Literal("log"),
|
|
203
|
+
Type.Literal("stop"),
|
|
204
|
+
Type.Literal("clear"),
|
|
205
|
+
]),
|
|
206
|
+
id: Type.Optional(Type.String()),
|
|
207
|
+
status: Type.Optional(Type.Array(Type.String())),
|
|
208
|
+
limit: Type.Optional(Type.Number()),
|
|
209
|
+
lines: Type.Optional(Type.Number({ description: "action:log trailing display rows (default 10; 0 pages raw bytes)." })),
|
|
210
|
+
tail_lines: Type.Optional(Type.Number({ description: "Deprecated alias for lines." })),
|
|
211
|
+
verbose: Type.Optional(Type.Boolean()),
|
|
212
|
+
...CommandFields,
|
|
213
|
+
require_successful_steps: StepGateField,
|
|
214
|
+
interval_seconds: Type.Optional(Type.Number()),
|
|
215
|
+
success_when: Type.Optional(ConditionSchema),
|
|
216
|
+
failure_when: Type.Optional(ConditionSchema),
|
|
217
|
+
blind_checks: BlindChecksField,
|
|
218
|
+
raw: Type.Optional(Type.Boolean()),
|
|
219
|
+
...HistoryField,
|
|
220
|
+
...CursorFields,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const StatusActionParams = Type.Object({
|
|
224
|
+
request_id: Type.Optional(Type.String({ description: "Stable cancellation request identity for action:stop. Reuse after disconnect." })),
|
|
225
|
+
action: Type.Union([Type.Literal("list"), Type.Literal("status"), Type.Literal("log"), Type.Literal("stop"), Type.Literal("clear")]),
|
|
226
|
+
id: Type.Optional(Type.String()),
|
|
227
|
+
status: Type.Optional(Type.Array(Type.String())),
|
|
228
|
+
limit: Type.Optional(Type.Number()),
|
|
229
|
+
lines: Type.Optional(Type.Number({ description: "action:log trailing display rows (default 10; 0 pages raw bytes)." })),
|
|
230
|
+
tail_lines: Type.Optional(Type.Number({ description: "Deprecated alias for lines." })),
|
|
231
|
+
verbose: Type.Optional(Type.Boolean()),
|
|
232
|
+
raw: Type.Optional(Type.Boolean()),
|
|
233
|
+
...HistoryField,
|
|
234
|
+
...CursorFields,
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const WaitForOperationsParams = Type.Object({
|
|
238
|
+
task_ids: Type.Array(Type.String({ minLength: 1, maxLength: 128 }), {
|
|
239
|
+
minItems: 1,
|
|
240
|
+
maxItems: 100,
|
|
241
|
+
description: "Durable local task ids owned by this session. Every listed operation must reach a terminal outcome.",
|
|
242
|
+
}),
|
|
243
|
+
continuation_note: Type.String({
|
|
244
|
+
minLength: 1,
|
|
245
|
+
maxLength: 2_000,
|
|
246
|
+
description: "The next reasoning step after the result. This cannot change tools, model, role, permissions or authorization.",
|
|
247
|
+
}),
|
|
248
|
+
}, {
|
|
249
|
+
additionalProperties: false,
|
|
250
|
+
description: "Register an all-settled durable wait on owned background operations. Returns immediately; the parent stays responsive and is notified when every dependency is terminal. A final answer that says you are waiting is not a registration.",
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const TerminalRevisionSchema = Type.Object({
|
|
254
|
+
operation_id: Type.String({ minLength: 1, maxLength: 200 }),
|
|
255
|
+
task_id: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),
|
|
256
|
+
revision: Type.Integer({ minimum: 1 }),
|
|
257
|
+
}, { additionalProperties: false });
|
|
258
|
+
|
|
259
|
+
const AcknowledgeParams = Type.Object({
|
|
260
|
+
wait_id: Type.String({ minLength: 1, maxLength: 200 }),
|
|
261
|
+
generation: Type.Integer({ minimum: 1 }),
|
|
262
|
+
terminal_revisions: Type.Union([
|
|
263
|
+
Type.Array(TerminalRevisionSchema, { minItems: 1, maxItems: 100 }),
|
|
264
|
+
Type.Record(Type.String(), Type.Integer({ minimum: 1 })),
|
|
265
|
+
], { description: "The exact terminal revisions that were read. An object maps operation_id (or task_id) to revision." }),
|
|
266
|
+
disposition: Type.Union([Type.Literal("continue"), Type.Literal("blocked"), Type.Literal("assignment-complete")]),
|
|
267
|
+
note: Type.String({ maxLength: 4_000, description: "What the evidence means for the next step. This does not complete a Goal or assignment by itself." }),
|
|
268
|
+
}, {
|
|
269
|
+
additionalProperties: false,
|
|
270
|
+
description: "Interpret the exact terminal evidence of a wait and consume that generation. Requires a prior status/log read of each revision. Acknowledging does not complete the assignment.",
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const WaitCancelParams = Type.Object({
|
|
274
|
+
wait_id: Type.String({ minLength: 1, maxLength: 200 }),
|
|
275
|
+
generation: Type.Integer({ minimum: 1 }),
|
|
276
|
+
reason: Type.String({ minLength: 1, maxLength: 1_024 }),
|
|
277
|
+
}, {
|
|
278
|
+
additionalProperties: false,
|
|
279
|
+
description: "Cancel one durable wait generation. This detaches observation ownership; it does not stop the background task. Stopping the task is a separate explicit action.",
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const WaitListParams = Type.Object({
|
|
283
|
+
pending_only: Type.Optional(Type.Boolean({ description: "Only generations that can still accept a result. Default false lists consumed and cancelled history too." })),
|
|
284
|
+
}, { additionalProperties: false, description: "List durable operation waits owned by the current session and workspace." });
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
const BACKGROUND_ORCHESTRATION_GUIDELINES = [
|
|
288
|
+
"Follow the Harness execution policy when scheduling independent background operations; subagent delegation mode does not require background commands to run serially.",
|
|
289
|
+
"Use background tasks for genuinely long-running processes or repeated checks. Run short commands in the foreground.",
|
|
290
|
+
"When a structured plan is active, keep it as the coordinator ledger: launch relevant background work early, continue unblocked foreground work without polling, and update the plan after inspecting each terminal result or failure.",
|
|
291
|
+
"Do not treat launch as completion of the parent milestone; relevant background work must be terminal, inspected, and integrated before verification or completion.",
|
|
292
|
+
];
|
|
293
|
+
|
|
294
|
+
export function registerTools(pi: ExtensionAPI, runtime?: ToolRuntime, refreshNavigator = refreshBackgroundTasksNavigator, options: { resumeLocal?: boolean } = {}): void {
|
|
295
|
+
let activeSession: BackgroundTaskCallbackOrigin | undefined;
|
|
296
|
+
const getActiveSession = () => activeSession;
|
|
297
|
+
const schemas = new Map<string, object>();
|
|
298
|
+
const register: ExtensionAPI["registerTool"] = (definition) => {
|
|
299
|
+
// A reload must not share schema identity with the previous registration.
|
|
300
|
+
const parameters = { ...definition.parameters };
|
|
301
|
+
schemas.set(definition.name, parameters);
|
|
302
|
+
pi.registerTool({ ...definition, parameters });
|
|
303
|
+
};
|
|
304
|
+
advertiseBackgroundAdapter(pi, schemas);
|
|
305
|
+
|
|
306
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
307
|
+
setCallbackBatchContext(pi, ctx);
|
|
308
|
+
activeSession = getCallbackOrigin(ctx);
|
|
309
|
+
if (options.resumeLocal !== false) {
|
|
310
|
+
const { resumeRunningTask } = runtime ?? await import("./runtime.js");
|
|
311
|
+
const { runTaskMaintenance } = await import("./maintenance.js");
|
|
312
|
+
for (const meta of listMetasForOrigin(activeSession)) {
|
|
313
|
+
if (meta.status === "running" || (meta.callback !== false && !meta.callbackSentAt && !meta.callbackSuppressedAt)) {
|
|
314
|
+
resumeRunningTask(pi, meta, getActiveSession);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
runTaskMaintenance({ activeOrigin: activeSession });
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
321
|
+
setCallbackBatchContext(pi, ctx);
|
|
322
|
+
getCallbackBatcher(pi).setForegroundRunning(true);
|
|
323
|
+
});
|
|
324
|
+
pi.on("session_tree", (_event, ctx) => { setCallbackBatchContext(pi, ctx); });
|
|
325
|
+
pi.on("agent_end", () => { getCallbackBatcher(pi).setForegroundRunning(false); });
|
|
326
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
327
|
+
getCallbackBatcher(pi).setForegroundRunning(false);
|
|
328
|
+
setCallbackBatchContext(pi, ctx);
|
|
329
|
+
});
|
|
330
|
+
pi.on("tool_execution_start", (event) => { getCallbackBatcher(pi).toolStarted(event.toolCallId); });
|
|
331
|
+
pi.on("tool_execution_end", async (event) => { await getCallbackBatcher(pi).toolEnded(event.toolCallId); });
|
|
332
|
+
pi.on("session_before_switch", () => {
|
|
333
|
+
activeSession = undefined;
|
|
334
|
+
cancelCallbackBatch(pi);
|
|
335
|
+
});
|
|
336
|
+
pi.on("session_shutdown", () => {
|
|
337
|
+
activeSession = undefined;
|
|
338
|
+
cancelCallbackBatch(pi);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
register({
|
|
342
|
+
name: "bg_task_spawn",
|
|
343
|
+
label: "BG Spawn",
|
|
344
|
+
description: "Start a long-running background process and return immediately with its task id. For remote work, prefer structured ssh: pass ssh:{host,user} and put the remote command in command; spawn defaults to a remote tmux session with durable local logs and real remote stop. For short synchronous remote commands that should return output now, use remote_bash from pi-better-ssh. If tmux is missing, the preset attempts to install tmux non-interactively and fails closed with operator guidance when setup cannot proceed. Explicit remote.session=direct skips tmux, but direct mode has weaker stop semantics and may leave the remote process running. Pass require_successful_steps:true for a chained shell command whose earlier failing steps must not be masked by a later command's exit 0: it runs under bash errexit and ends at the first unhandled failing step. Never wait or poll in the foreground.",
|
|
345
|
+
promptGuidelines: BACKGROUND_ORCHESTRATION_GUIDELINES,
|
|
346
|
+
parameters: SpawnParams,
|
|
347
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
348
|
+
const origin = getCallbackOrigin(ctx);
|
|
349
|
+
activeSession = origin;
|
|
350
|
+
let receipt: DurableLaunch | undefined;
|
|
351
|
+
const durable = await durableLaunchText(pi, "process", params, ctx, launch => { receipt = launch; });
|
|
352
|
+
if (durable !== undefined) {
|
|
353
|
+
refreshNavigator(ctx);
|
|
354
|
+
return text(durable, launchReceipt(receipt));
|
|
355
|
+
}
|
|
356
|
+
const { spawnTask } = runtime ?? await import("./runtime.js");
|
|
357
|
+
const launched = reportLaunch(() => spawnTask(pi, params, ctx.cwd, origin, getActiveSession));
|
|
358
|
+
refreshNavigator(ctx);
|
|
359
|
+
return text(launched);
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
register({
|
|
364
|
+
name: "bg_task_spawn_batch",
|
|
365
|
+
label: "BG Spawn Batch",
|
|
366
|
+
description: "Start up to 100 independent background processes at once and return immediately with a batch id, one task id per launched job, and any job that was refused. Each job is a normal background task with its own process, log, and metadata, and records the batch id and batch name. Every job is preflighted first, so one invalid job refuses the batch before any process starts. Never wait or poll in the foreground; inspect, log, or stop each task by its id.",
|
|
367
|
+
promptGuidelines: BACKGROUND_ORCHESTRATION_GUIDELINES,
|
|
368
|
+
parameters: BatchParams,
|
|
369
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
370
|
+
activeSession = getCallbackOrigin(ctx);
|
|
371
|
+
const { formatBatchLaunch, spawnTaskBatch } = await import("./batch.js");
|
|
372
|
+
const launched = spawnTaskBatch(pi, params, ctx.cwd, activeSession, getActiveSession, {}, runtime);
|
|
373
|
+
refreshNavigator(ctx);
|
|
374
|
+
return text(formatBatchLaunch(launched), launched);
|
|
375
|
+
},
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
register({
|
|
379
|
+
name: "bg_task_watch",
|
|
380
|
+
label: "BG Watch",
|
|
381
|
+
description: `Poll a command in the background until success_when, failure_when, or timeout matches. For remote work, prefer structured ssh: pass ssh:{host,user} and provide the remote command in command; each interval opens a direct one-shot SSH poll without tmux installation. For short synchronous remote commands that should return output now, use remote_bash from pi-better-ssh. Returns its task id once the first check finishes. ${WATCH_CHECK_GUIDANCE} Default timeout 900 seconds; pass timeout_seconds:0 to disable.`,
|
|
382
|
+
promptGuidelines: BACKGROUND_ORCHESTRATION_GUIDELINES,
|
|
383
|
+
parameters: WatchParams,
|
|
384
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
385
|
+
const origin = getCallbackOrigin(ctx);
|
|
386
|
+
activeSession = origin;
|
|
387
|
+
let receipt: DurableLaunch | undefined;
|
|
388
|
+
const durable = await durableLaunchText(pi, "watch", params, ctx, launch => { receipt = launch; });
|
|
389
|
+
if (durable !== undefined) {
|
|
390
|
+
refreshNavigator(ctx);
|
|
391
|
+
return text(durable, launchReceipt(receipt));
|
|
392
|
+
}
|
|
393
|
+
const launched = await launchWatch(pi, params, ctx.cwd, origin, getActiveSession, FIRST_WATCH_CHECK_WAIT_MS, signal, runtime);
|
|
394
|
+
refreshNavigator(ctx);
|
|
395
|
+
return text(launched);
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
register({
|
|
400
|
+
name: "bg_task_list",
|
|
401
|
+
label: "BG List",
|
|
402
|
+
description: "List durable background tasks as compact rows under a 1 KiB UTF-8 budget (10 entries default), newest first. Current-session only unless all:true. Nonblocking. Pass the returned nextCursor as cursor to page further (every task is reachable), or statusCursor to get a small no-change response or failure-only updates; a higher limit/max_bytes gives a larger explicit page.",
|
|
403
|
+
parameters: ListParams,
|
|
404
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
405
|
+
activeSession = getCallbackOrigin(ctx);
|
|
406
|
+
return text(await durableAwareList(pi, params, activeSession, ctx));
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
register({
|
|
411
|
+
name: "bg_task_status",
|
|
412
|
+
label: "BG Status",
|
|
413
|
+
description: "Inspect one background task. Default output is a compact model-facing summary (1 KiB UTF-8) with matched condition, exit/signal, stop error, and failure counts before progress. Current-session only unless all:true; unknown ownership is a gap, not missing or healthy. Pass verbose:true only when full raw metadata is explicitly needed. Environment values are omitted. A raw log nextCursor passed as cursor continues the raw log page; an incidentCursor pages incidents that need action; history:true lists failure history too. After a terminal callback, call this first and call bg_task_log only if the summary is insufficient.",
|
|
414
|
+
parameters: StatusParams,
|
|
415
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
416
|
+
const origin = getCallbackOrigin(ctx);
|
|
417
|
+
activeSession = origin;
|
|
418
|
+
const durable = await durableStatusText(pi, params.id, ctx, params);
|
|
419
|
+
if (durable !== undefined) return text(durable);
|
|
420
|
+
return text(taskStatus(pi, params.id, params, statusOptions(params, origin)));
|
|
421
|
+
},
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
register({
|
|
425
|
+
name: "bg_task_log",
|
|
426
|
+
label: "BG Log",
|
|
427
|
+
description: "Read a background task log. Default output is a compact 10-line terminal-aware tail for model ingestion (1 KiB UTF-8). Current-session only unless all:true. Pass lines for a bounded tail (tail_lines is a deprecated alias); lines:0 pages the retained raw log from the oldest retained offset (16 KiB pages, 64 KiB hard cap) with a caller-owned cursor. Capture and retention loss are disclosed; this is not a full-history archive. Nonblocking.",
|
|
428
|
+
parameters: LogParams,
|
|
429
|
+
renderResult(result: unknown, options: unknown, theme: unknown) {
|
|
430
|
+
return renderBackgroundTaskLogDisplay(result, options, theme);
|
|
431
|
+
},
|
|
432
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
433
|
+
const origin = getCallbackOrigin(ctx);
|
|
434
|
+
activeSession = origin;
|
|
435
|
+
const durable = await durableLogText(pi, params.id, params, ctx);
|
|
436
|
+
if (durable !== undefined) return text(durable);
|
|
437
|
+
return logText(params.id, logOptions(params, origin));
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
register({
|
|
442
|
+
name: "bg_task_stop",
|
|
443
|
+
label: "BG Stop",
|
|
444
|
+
description: "Cancel a watcher or terminate a background task. Only a task owned by the current session stops; a foreign-session or unverifiable task is refused unless all:true. For a tmux-backed SSH task, stop kills its remote tmux session before marking it cancelled. Direct SSH stop only tears down the local client and may leave the remote process running.",
|
|
445
|
+
parameters: Type.Object({ ...IdParams.properties, request_id: Type.Optional(Type.String({ description: "Stable cancellation request identity. Reuse this value after a disconnect." })) }),
|
|
446
|
+
async execute(toolCallId, params, _signal, _onUpdate, ctx) {
|
|
447
|
+
const origin = getCallbackOrigin(ctx);
|
|
448
|
+
activeSession = origin;
|
|
449
|
+
const durable = await durableStopText(pi, params.id, ctx, params.request_id ?? toolCallId);
|
|
450
|
+
if (durable !== undefined) {
|
|
451
|
+
refreshNavigator(ctx);
|
|
452
|
+
return text(durable);
|
|
453
|
+
}
|
|
454
|
+
const result = await formatStop(pi, params.id, ctx, getActiveSession, statusOptions(params, origin), runtime ?? await import("./runtime.js"));
|
|
455
|
+
refreshNavigator(ctx);
|
|
456
|
+
return text(result);
|
|
457
|
+
},
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
register({
|
|
461
|
+
name: "bg_task",
|
|
462
|
+
label: "BG Task",
|
|
463
|
+
description: "Action wrapper for background tasks: spawn, watch, list, status, log, stop, or clear. For remote work, prefer structured ssh: pass ssh:{host,user} and provide the remote command in command. For action:spawn, require_successful_steps:true runs a chained shell command under bash errexit so it ends at its first unhandled failing step instead of only reporting its last command's exit. For short synchronous remote commands that should return output now, use remote_bash from pi-better-ssh. SSH spawn defaults to durable tmux; SSH watches use direct one-shot polls without tmux installation; remote.session=direct is a weaker-stop spawn escape hatch. Spawn returns immediately; do not poll in foreground. For action:watch: " + WATCH_CHECK_GUIDANCE + " For action:status, default compact output and use verbose:true only for full metadata. For action:log, default compact tail and use lines:0 to page retained raw bytes. Standalone tools and these wrappers share the same output assembler. List/status/log default to the current session; pass all:true to override. Stop and clear change only current-session tasks: clear dismisses every owned terminal task, or one task with id (all:true allows another session's task by id).",
|
|
464
|
+
promptGuidelines: BACKGROUND_ORCHESTRATION_GUIDELINES,
|
|
465
|
+
parameters: ActionParams,
|
|
466
|
+
renderResult(result: unknown, options: unknown, theme: unknown) {
|
|
467
|
+
return renderBackgroundTaskLogDisplay(result, options, theme);
|
|
468
|
+
},
|
|
469
|
+
async execute(toolCallId, params, signal, _onUpdate, ctx) {
|
|
470
|
+
activeSession = getCallbackOrigin(ctx);
|
|
471
|
+
return actionText(pi, { ...params, request_id: params.request_id ?? toolCallId }, ctx, activeSession, getActiveSession, signal, runtime ?? await import("./runtime.js"), refreshNavigator);
|
|
472
|
+
},
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
register({
|
|
476
|
+
name: "bg_status",
|
|
477
|
+
label: "BG Status",
|
|
478
|
+
description: "Action wrapper for inspecting background tasks: list, status, log, stop, or clear. Nonblocking. Status is compact by default; log returns a compact tail by default. Use verbose:true or lines:0 only for explicit full-data recovery. Shares the same output assembler as the standalone tools. Current-session default; pass all:true to override. Stop and clear change only current-session tasks (clear with id and all:true for another session's task).",
|
|
479
|
+
parameters: StatusActionParams,
|
|
480
|
+
renderResult(result: unknown, options: unknown, theme: unknown) {
|
|
481
|
+
return renderBackgroundTaskLogDisplay(result, options, theme);
|
|
482
|
+
},
|
|
483
|
+
async execute(toolCallId, params, _signal, _onUpdate, ctx) {
|
|
484
|
+
activeSession = getCallbackOrigin(ctx);
|
|
485
|
+
return actionText(pi, { ...params, request_id: params.request_id ?? toolCallId }, ctx, activeSession, getActiveSession, undefined, runtime ?? await import("./runtime.js"), refreshNavigator);
|
|
486
|
+
},
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
register({
|
|
490
|
+
name: "wait_for_operations",
|
|
491
|
+
label: "Wait for Operations",
|
|
492
|
+
description: "Register an all-settled durable wait on background operations owned by this session and return immediately. The parent stays responsive; a completion notification arrives when every listed operation is terminal. Reading the status/log and acknowledging the exact revisions is required before the wait is consumed. This never enqueues, merges, pushes, or resumes a model by itself.",
|
|
493
|
+
promptGuidelines: [
|
|
494
|
+
"Call wait_for_operations when no independent work remains; a final answer that says you are waiting is not a registration.",
|
|
495
|
+
"After a settled wait, inspect bg_task_status and bg_task_log, then acknowledge_operation_result with the exact revisions.",
|
|
496
|
+
],
|
|
497
|
+
parameters: WaitForOperationsParams,
|
|
498
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
499
|
+
const host = getObservedWaitHost(pi);
|
|
500
|
+
if (!host?.available) {
|
|
501
|
+
return text((await import("./observed-waits.js")).observedWaitServiceUnavailableReason(pi)
|
|
502
|
+
?? "Observed waits are unavailable in this session; inspect tasks directly and continue without a durable wait.");
|
|
503
|
+
}
|
|
504
|
+
try {
|
|
505
|
+
const wait = host.usesServerResults
|
|
506
|
+
? await host.superviseParentWait(ctx, params.task_ids, params.continuation_note)
|
|
507
|
+
: host.waitForParent(ctx, params.task_ids, params.continuation_note);
|
|
508
|
+
return text((await import("./observed-waits.js")).formatWaitView(wait), wait);
|
|
509
|
+
} catch (error) {
|
|
510
|
+
return text(`Wait was not registered: ${readableError(error)}`);
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
register({
|
|
516
|
+
name: "acknowledge_operation_result",
|
|
517
|
+
label: "Acknowledge Operation Result",
|
|
518
|
+
description: "Consume one ready wait generation after interpreting its exact terminal evidence. Requires a prior bg_task_status or bg_task_log read of every listed revision. The disposition (continue, blocked, assignment-complete) is recorded against the evidence; it does not complete a Goal or assignment by itself.",
|
|
519
|
+
parameters: AcknowledgeParams,
|
|
520
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
521
|
+
const host = getObservedWaitHost(pi);
|
|
522
|
+
if (!host?.available) {
|
|
523
|
+
return text((await import("./observed-waits.js")).observedWaitServiceUnavailableReason(pi)
|
|
524
|
+
?? "Observed waits are unavailable in this session; no wait was consumed.");
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
const ack = host.acknowledgeForParent(ctx, {
|
|
528
|
+
waitId: params.wait_id,
|
|
529
|
+
generation: params.generation,
|
|
530
|
+
terminalRevisions: params.terminal_revisions,
|
|
531
|
+
disposition: params.disposition,
|
|
532
|
+
note: params.note,
|
|
533
|
+
});
|
|
534
|
+
return text(`Acknowledged ${ack.waitId} generation ${ack.generation} as ${ack.disposition}; phase ${ack.phase}. Released ${ack.releasedPinIds.length} evidence pin(s).`, ack);
|
|
535
|
+
} catch (error) {
|
|
536
|
+
return text(`Wait was not acknowledged: ${readableError(error)}`);
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
register({
|
|
542
|
+
name: "bg_task_wait_cancel",
|
|
543
|
+
label: "BG Wait Cancel",
|
|
544
|
+
description: "Cancel one durable wait generation owned by this session. This detaches observation ownership and voids unconsumed deliveries; it does not stop the background task. Stop the task separately with bg_task_stop when that is the intent.",
|
|
545
|
+
parameters: WaitCancelParams,
|
|
546
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
547
|
+
const host = getObservedWaitHost(pi);
|
|
548
|
+
if (!host?.available) return text("Observed waits are unavailable; no wait was cancelled.");
|
|
549
|
+
try {
|
|
550
|
+
const wait = host.cancelWaitForParent(ctx, params.wait_id, params.generation, params.reason);
|
|
551
|
+
return text(`Cancelled ${wait.waitId} generation ${wait.generation}. The operation itself was not stopped.`, wait);
|
|
552
|
+
} catch (error) {
|
|
553
|
+
return text(`Wait was not cancelled: ${readableError(error)}`);
|
|
554
|
+
}
|
|
555
|
+
},
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
register({
|
|
559
|
+
name: "bg_task_wait_list",
|
|
560
|
+
label: "BG Wait List",
|
|
561
|
+
description: "List durable waits owned by the current session and workspace. Compact rows; consumed and cancelled generations are included unless pending_only is true.",
|
|
562
|
+
parameters: WaitListParams,
|
|
563
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
564
|
+
const host = getObservedWaitHost(pi);
|
|
565
|
+
if (!host?.available) return text("Observed waits are unavailable in this session.");
|
|
566
|
+
try {
|
|
567
|
+
const waits = host.listWaitsForParent(ctx, { pendingOnly: params.pending_only === true });
|
|
568
|
+
if (waits.length === 0) return text("No durable waits for this session.");
|
|
569
|
+
const { formatWaitView } = await import("./observed-waits.js");
|
|
570
|
+
return text(waits.map((wait: WaitView) => formatWaitView(wait)).join("\n\n"), waits);
|
|
571
|
+
} catch (error) {
|
|
572
|
+
return text(`Waits could not be listed: ${readableError(error)}`);
|
|
573
|
+
}
|
|
574
|
+
},
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
registerDeliveryObserver(pi, {
|
|
578
|
+
launch: async (params, ctx) => {
|
|
579
|
+
const host = getObservedWaitHost(pi);
|
|
580
|
+
if (!host?.available) {
|
|
581
|
+
throw new Error("Durable observed waits are unavailable; refusing to launch an unsupervised delivery watcher.");
|
|
582
|
+
}
|
|
583
|
+
const launched = await host.launchParent({
|
|
584
|
+
kind: "process",
|
|
585
|
+
params: params as unknown as Record<string, unknown>,
|
|
586
|
+
ctx,
|
|
587
|
+
});
|
|
588
|
+
return { id: launched.id };
|
|
589
|
+
},
|
|
590
|
+
wait: (taskIds, note, ctx) => {
|
|
591
|
+
const host = getObservedWaitHost(pi);
|
|
592
|
+
if (!host?.available) {
|
|
593
|
+
throw new Error("Durable observed waits are unavailable; the delivery wait was not registered.");
|
|
594
|
+
}
|
|
595
|
+
return host.waitForParent(ctx, taskIds, note);
|
|
596
|
+
},
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
export function text(textValue: string, details?: unknown) {
|
|
601
|
+
return { content: [{ type: "text" as const, text: textValue }], details };
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function launchReceipt(launch?: DurableLaunch) {
|
|
605
|
+
return launch ? { id: launch.id, operationId: launch.operationId, kind: launch.kind,
|
|
606
|
+
owner: launch.owner, started: launch.started, claimed: launch.claimed } : undefined;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** Requested byte budget: canonical `max_bytes`, else the deprecated `maxBytes` alias (#321). */
|
|
610
|
+
function requestedMaxBytes(params: Record<string, unknown>): number | undefined {
|
|
611
|
+
const value = readOutputControls(params).maxBytes;
|
|
612
|
+
return typeof value === "number" ? value : undefined;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function statusOptions(params: Record<string, unknown>, origin?: BackgroundTaskCallbackOrigin): OutputOptions {
|
|
616
|
+
return {
|
|
617
|
+
verbose: params.verbose === true,
|
|
618
|
+
history: params.history === true,
|
|
619
|
+
cursor: typeof params.cursor === "string" ? params.cursor : undefined,
|
|
620
|
+
maxBytes: requestedMaxBytes(params),
|
|
621
|
+
all: params.all === true,
|
|
622
|
+
...scopeOptions(origin),
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function taskStatus(pi: ExtensionAPI, id: string, params: Record<string, unknown>, options: OutputOptions): string {
|
|
627
|
+
const inspection = inspectMeta(id);
|
|
628
|
+
const enriched: OutputOptions = { ...options };
|
|
629
|
+
const kind = cursorKind(options.cursor);
|
|
630
|
+
if (params.diagnostics === true && !options.history && (!kind || kind === "s" || kind === "t") &&
|
|
631
|
+
inspection.meta && classifyOwnership(inspection.meta, options) === "allow") {
|
|
632
|
+
enriched.processDiagnostics = inspectTaskProcess(inspection.meta, backgroundProcessDiagnosticsAccess(pi));
|
|
633
|
+
// Fresh samples bypass status suppression, not raw or incident pagination.
|
|
634
|
+
if (kind === "s") enriched.cursor = undefined;
|
|
635
|
+
}
|
|
636
|
+
return formatStatus(inspection, enriched);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function logOptions(params: Record<string, unknown>, origin?: BackgroundTaskCallbackOrigin): OutputOptions {
|
|
640
|
+
// Canonical `lines`, else the deprecated `tail_lines` alias (#321).
|
|
641
|
+
const lines = readOutputControls(params).lines;
|
|
642
|
+
return {
|
|
643
|
+
tailLines: typeof lines === "number" ? lines : undefined,
|
|
644
|
+
raw: params.raw === true,
|
|
645
|
+
cursor: typeof params.cursor === "string" ? params.cursor : undefined,
|
|
646
|
+
maxBytes: requestedMaxBytes(params),
|
|
647
|
+
all: params.all === true,
|
|
648
|
+
...scopeOptions(origin),
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function listOptions(params: Record<string, unknown>, origin?: BackgroundTaskCallbackOrigin): OutputOptions {
|
|
653
|
+
return {
|
|
654
|
+
statuses: params.status as string[] | undefined,
|
|
655
|
+
limit: typeof params.limit === "number" ? params.limit : undefined,
|
|
656
|
+
cursor: typeof params.cursor === "string" ? params.cursor : undefined,
|
|
657
|
+
maxBytes: requestedMaxBytes(params),
|
|
658
|
+
all: params.all === true,
|
|
659
|
+
...scopeOptions(origin),
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
async function actionText(
|
|
664
|
+
pi: ExtensionAPI,
|
|
665
|
+
params: Record<string, unknown>,
|
|
666
|
+
ctx: ExtensionContext,
|
|
667
|
+
callbackOrigin: BackgroundTaskCallbackOrigin,
|
|
668
|
+
getActiveSession: () => BackgroundTaskCallbackOrigin | undefined,
|
|
669
|
+
signal: AbortSignal | undefined,
|
|
670
|
+
runtime: ToolRuntime,
|
|
671
|
+
refreshNavigator: typeof refreshBackgroundTasksNavigator,
|
|
672
|
+
) {
|
|
673
|
+
if (params.action === "log" && params.id) {
|
|
674
|
+
const durable = await durableLogText(pi, String(params.id), params, ctx);
|
|
675
|
+
if (durable !== undefined) return text(durable);
|
|
676
|
+
return logText(String(params.id), logOptions(params, callbackOrigin));
|
|
677
|
+
}
|
|
678
|
+
let receipt: DurableLaunch | undefined;
|
|
679
|
+
return text(await runAction(pi, params, ctx, callbackOrigin, getActiveSession, signal, runtime, refreshNavigator,
|
|
680
|
+
launch => { receipt = launch; }), launchReceipt(receipt));
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
async function runAction(
|
|
684
|
+
pi: ExtensionAPI,
|
|
685
|
+
params: Record<string, unknown>,
|
|
686
|
+
ctx: ExtensionContext,
|
|
687
|
+
callbackOrigin: BackgroundTaskCallbackOrigin,
|
|
688
|
+
getActiveSession: () => BackgroundTaskCallbackOrigin | undefined,
|
|
689
|
+
signal: AbortSignal | undefined,
|
|
690
|
+
runtime: ToolRuntime,
|
|
691
|
+
refreshNavigator: typeof refreshBackgroundTasksNavigator,
|
|
692
|
+
onLaunch?: (launch: DurableLaunch) => void,
|
|
693
|
+
): Promise<string> {
|
|
694
|
+
const { spawnTask } = runtime;
|
|
695
|
+
const withNavigatorRefresh = (ctx: ExtensionContext, result: string) => { refreshNavigator(ctx); return result; };
|
|
696
|
+
switch (params.action) {
|
|
697
|
+
case "spawn": {
|
|
698
|
+
const durable = await durableLaunchText(pi, "process", params, ctx, onLaunch);
|
|
699
|
+
if (durable !== undefined) return withNavigatorRefresh(ctx, durable);
|
|
700
|
+
return withNavigatorRefresh(ctx, reportLaunch(() => spawnTask(pi, params, ctx.cwd, callbackOrigin, getActiveSession)));
|
|
701
|
+
}
|
|
702
|
+
case "watch": {
|
|
703
|
+
if (!params.success_when) return "Invalid parameters: watch requires success_when.";
|
|
704
|
+
const durable = await durableLaunchText(pi, "watch", params, ctx, onLaunch);
|
|
705
|
+
if (durable !== undefined) return withNavigatorRefresh(ctx, durable);
|
|
706
|
+
return withNavigatorRefresh(ctx, await launchWatch(pi, params as unknown as WatchTaskParams, ctx.cwd, callbackOrigin, getActiveSession, FIRST_WATCH_CHECK_WAIT_MS, signal, runtime));
|
|
707
|
+
}
|
|
708
|
+
case "list":
|
|
709
|
+
return durableAwareList(pi, params, callbackOrigin, ctx);
|
|
710
|
+
case "status": {
|
|
711
|
+
if (!params.id) return "Invalid parameters: status requires id.";
|
|
712
|
+
const durable = await durableStatusText(pi, String(params.id), ctx, params);
|
|
713
|
+
if (durable !== undefined) return durable;
|
|
714
|
+
return taskStatus(pi, String(params.id), params, statusOptions(params, callbackOrigin));
|
|
715
|
+
}
|
|
716
|
+
case "log": {
|
|
717
|
+
if (!params.id) return "Invalid parameters: log requires id.";
|
|
718
|
+
const durable = await durableLogText(pi, String(params.id), params, ctx);
|
|
719
|
+
if (durable !== undefined) return durable;
|
|
720
|
+
return formatLog(String(params.id), logOptions(params, callbackOrigin));
|
|
721
|
+
}
|
|
722
|
+
case "stop": {
|
|
723
|
+
if (!params.id) return "Invalid parameters: stop requires id.";
|
|
724
|
+
const durable = await durableStopText(pi, String(params.id), ctx, typeof params.request_id === "string" ? params.request_id : undefined);
|
|
725
|
+
if (durable !== undefined) return withNavigatorRefresh(ctx, durable);
|
|
726
|
+
return withNavigatorRefresh(ctx, await formatStop(pi, String(params.id), ctx, getActiveSession, statusOptions(params, callbackOrigin), runtime));
|
|
727
|
+
}
|
|
728
|
+
case "clear": {
|
|
729
|
+
const host = getObservedWaitHost(pi);
|
|
730
|
+
await host?.refreshManagedResults();
|
|
731
|
+
const durableId = typeof params.id === "string" && params.id && host?.isDurableTask(params.id) === true;
|
|
732
|
+
if (durableId) {
|
|
733
|
+
return withNavigatorRefresh(ctx, dismissDurableForClear(pi, params, callbackOrigin)
|
|
734
|
+
?? `Durable operation ${String(params.id)} was not dismissed.`);
|
|
735
|
+
}
|
|
736
|
+
const legacy = formatClear(params, statusOptions(params, callbackOrigin));
|
|
737
|
+
const durable = dismissDurableForClear(pi, params, callbackOrigin);
|
|
738
|
+
return withNavigatorRefresh(ctx, durable === undefined ? legacy : `${legacy}\n${durable}`);
|
|
739
|
+
}
|
|
740
|
+
default:
|
|
741
|
+
return `Unknown action: ${String(params.action)}`;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/** Ordinary lists reuse retained activity; explicit filtered/paged lists open history. */
|
|
746
|
+
async function durableAwareList(
|
|
747
|
+
pi: ExtensionAPI,
|
|
748
|
+
params: Record<string, unknown>,
|
|
749
|
+
origin: BackgroundTaskCallbackOrigin | undefined,
|
|
750
|
+
ctx: ExtensionContext,
|
|
751
|
+
): Promise<string> {
|
|
752
|
+
const options = listOptions(params, origin);
|
|
753
|
+
const history = backgroundListNeedsHistory(params);
|
|
754
|
+
const snapshot = origin && !unavailableSessionOrigins.has(origin) ? await backgroundObservation(pi, ctx, history, params.all === true,
|
|
755
|
+
{ limit: options.limit, statuses: options.statuses, cursor: options.cursor }) : undefined;
|
|
756
|
+
if (snapshot && snapshot.status !== "ready") throw new Error(snapshot.reason ?? "Background activity is unverified");
|
|
757
|
+
const index = snapshot?.backgroundIndex ?? (snapshot ? { records: snapshot.rows
|
|
758
|
+
.map((row: any) => ({ found: true, meta: row.meta.durable ? { ...row.meta,
|
|
759
|
+
callbackOrigin: { cwd: row.meta.owner.workspace, sessionId: row.meta.owner.sessionId },
|
|
760
|
+
logPath: row.meta.logPaths?.stdout ?? "" } : row.meta }))
|
|
761
|
+
.sort((a: any, b: any) => b.meta.startedAt - a.meta.startedAt || b.meta.id.localeCompare(a.meta.id)) } : undefined);
|
|
762
|
+
const legacy = formatList(options, index);
|
|
763
|
+
if (snapshot) return legacy;
|
|
764
|
+
const host = getObservedWaitHost(pi);
|
|
765
|
+
if ((!snapshot && !host?.available) || !origin || typeof params.cursor === "string") return legacy;
|
|
766
|
+
const wanted = Array.isArray(params.status) && params.status.length > 0
|
|
767
|
+
? new Set(params.status.filter((status): status is string => typeof status === "string"))
|
|
768
|
+
: undefined;
|
|
769
|
+
const limit = Math.max(1, Math.min(Math.floor(typeof params.limit === "number" ? params.limit : 10), 100));
|
|
770
|
+
const durable = (snapshot ? snapshot.rows.filter((row: any) => row.meta.durable).map((row: any) => row.meta) as import("./observed-waits.js").DurableTaskProjection[] : host?.listDurableTasks(origin) ?? [])
|
|
771
|
+
.filter((task) => !wanted || (task.active ? wanted.has("running") : wanted.has(task.status)))
|
|
772
|
+
.slice(0, limit);
|
|
773
|
+
if (durable.length === 0) return legacy;
|
|
774
|
+
const rows = durable.map((task) => {
|
|
775
|
+
const elapsed = Math.max(0, Math.round(((task.endedAt ?? Date.now()) - task.startedAt) / 1000));
|
|
776
|
+
const label = task.name ? `${task.name} · ` : "";
|
|
777
|
+
const command = (task.command ?? task.argv?.join(" ") ?? "").replace(/\s+/g, " ").trim();
|
|
778
|
+
const exit = task.exitCode !== null ? ` exit=${task.exitCode}` : "";
|
|
779
|
+
const waits = task.waits.length ? ` waits=${task.waits.length}` : "";
|
|
780
|
+
return ` ${task.id} ${task.status} ${task.kind} ${elapsed}s ${label}${command}${exit}${waits}`.slice(0, 500);
|
|
781
|
+
});
|
|
782
|
+
return [
|
|
783
|
+
`durable background operations (supervised, current session): ${durable.length}`,
|
|
784
|
+
...rows,
|
|
785
|
+
"Inspect with bg_task_status/bg_task_log; stop with bg_task_stop; dismiss a terminal operation with clear.",
|
|
786
|
+
legacy,
|
|
787
|
+
].join("\n");
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/** Dismiss matching terminal durable operations without touching the legacy registry. */
|
|
791
|
+
function dismissDurableForClear(
|
|
792
|
+
pi: ExtensionAPI,
|
|
793
|
+
params: Record<string, unknown>,
|
|
794
|
+
origin: BackgroundTaskCallbackOrigin | undefined,
|
|
795
|
+
): string | undefined {
|
|
796
|
+
const host = getObservedWaitHost(pi);
|
|
797
|
+
if (!host?.available || !origin) return undefined;
|
|
798
|
+
const id = typeof params.id === "string" && params.id ? params.id : undefined;
|
|
799
|
+
if (id) {
|
|
800
|
+
if (!host.isDurableTask(id)) return undefined;
|
|
801
|
+
const task = host.readDurableTask(origin, id);
|
|
802
|
+
if (!task) return `Durable operation ${id} belongs to another session; it was not dismissed.`;
|
|
803
|
+
const dismissed = host.dismissDurableTask(origin, id);
|
|
804
|
+
if (!dismissed && !task.terminal) return `Durable operation ${id} is ${task.status}; stop it before dismissing it.`;
|
|
805
|
+
return dismissed
|
|
806
|
+
? `Dismissed durable operation ${id}. Its terminal evidence remains available through bg_task_status and bg_task_log.`
|
|
807
|
+
: `Durable operation ${id} was not dismissed.`;
|
|
808
|
+
}
|
|
809
|
+
const terminal = host.listDurableTasks(origin, { fresh: true }).filter((task) => task.terminal);
|
|
810
|
+
let dismissed = 0;
|
|
811
|
+
for (const task of terminal) if (host.dismissDurableTask(origin, task.id)) dismissed += 1;
|
|
812
|
+
return dismissed > 0
|
|
813
|
+
? `Dismissed ${dismissed} terminal durable operation${dismissed === 1 ? "" : "s"}.`
|
|
814
|
+
: undefined;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
function readableError(error: unknown): string {
|
|
818
|
+
return error instanceof Error ? error.message : String(error);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* Route a local launch through the durable supervisor when the observed-waits
|
|
823
|
+
* host is installed. `undefined` means "use the legacy task route": remote/SSH
|
|
824
|
+
* work, an extension without the durable host, or a session not yet activated.
|
|
825
|
+
* A failed activation is never permission to start a second legacy command.
|
|
826
|
+
* Admission and sandboxing happen inside the host, through the
|
|
827
|
+
* same `resolve*SandboxPlan` helpers the legacy route uses.
|
|
828
|
+
*/
|
|
829
|
+
async function durableLaunchText(
|
|
830
|
+
pi: ExtensionAPI,
|
|
831
|
+
kind: DurableTaskKind,
|
|
832
|
+
params: Record<string, unknown>,
|
|
833
|
+
ctx: ExtensionContext,
|
|
834
|
+
onLaunch?: (launch: DurableLaunch) => void,
|
|
835
|
+
): Promise<string | undefined> {
|
|
836
|
+
const host = getObservedWaitHost(pi);
|
|
837
|
+
if (!host || params.ssh || !host.available && !host.hasActivated) return undefined;
|
|
838
|
+
try {
|
|
839
|
+
const launched = await host.launchParent({ kind, params, ctx });
|
|
840
|
+
onLaunch?.(launched);
|
|
841
|
+
return host.formatLaunch(launched);
|
|
842
|
+
} catch (error) {
|
|
843
|
+
if (error instanceof ForegroundSandboxBlockedError) return error.message;
|
|
844
|
+
throw error;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
async function durableStatusText(pi: ExtensionAPI, id: string, ctx: ExtensionContext, params: Record<string, unknown>): Promise<string | undefined> {
|
|
849
|
+
const host = getObservedWaitHost(pi);
|
|
850
|
+
if (!host || !host.isDurableTask(id)) return undefined;
|
|
851
|
+
try {
|
|
852
|
+
const view = await host.settledStatusForParent(ctx, id);
|
|
853
|
+
if (params.verbose === true || cursorKind(typeof params.cursor === "string" ? params.cursor : undefined) === "t") {
|
|
854
|
+
const origin = { cwd: ctx.cwd, sessionId: ctx.sessionManager.getSessionId() };
|
|
855
|
+
const task = host.readDurableTask(origin, id);
|
|
856
|
+
if (!task) return `Background task ${id} is outside the current session scope.`;
|
|
857
|
+
const meta = {
|
|
858
|
+
...task, kind: task.kind === "watch" ? "command_watch" : "process", status: view.status,
|
|
859
|
+
argv: task.argv ? [...task.argv] : undefined,
|
|
860
|
+
logPath: task.logPaths.stdout, callbackOrigin: origin,
|
|
861
|
+
lastExitCode: view.exitCode, result: task.terminal ? { exitCode: view.exitCode, signal: view.signal } : undefined,
|
|
862
|
+
};
|
|
863
|
+
return formatDurableStatusMetadata(meta, statusOptions(params, origin));
|
|
864
|
+
}
|
|
865
|
+
return host.formatStatus(view);
|
|
866
|
+
} catch (error) {
|
|
867
|
+
return readableError(error);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
async function durableLogText(
|
|
872
|
+
pi: ExtensionAPI,
|
|
873
|
+
id: string,
|
|
874
|
+
params: Record<string, unknown>,
|
|
875
|
+
ctx: ExtensionContext,
|
|
876
|
+
): Promise<string | undefined> {
|
|
877
|
+
const host = getObservedWaitHost(pi);
|
|
878
|
+
if (!host || !host.isDurableTask(id)) return undefined;
|
|
879
|
+
try {
|
|
880
|
+
await host.settledStatusForParent(ctx, id);
|
|
881
|
+
const requested = typeof params.lines === "number" ? params.lines
|
|
882
|
+
: typeof params.tail_lines === "number" ? params.tail_lines : 40;
|
|
883
|
+
// `lines: 0` means the retained raw log on the legacy route; the durable
|
|
884
|
+
// store keeps the bounded tails, so show a wider tail instead of an empty page.
|
|
885
|
+
const lines = requested === 0 ? 200 : Math.max(1, Math.floor(requested));
|
|
886
|
+
return host.formatLog(host.logForParent(ctx, id, { lines }));
|
|
887
|
+
} catch (error) {
|
|
888
|
+
return readableError(error);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
async function durableStopText(pi: ExtensionAPI, id: string, ctx: ExtensionContext, requestId?: string): Promise<string | undefined> {
|
|
893
|
+
const host = getObservedWaitHost(pi);
|
|
894
|
+
if (!host || !host.isDurableTask(id)) return undefined;
|
|
895
|
+
try {
|
|
896
|
+
const status = host.formatStatus(await host.stopForParent(ctx, id, requestId));
|
|
897
|
+
return requestId ? `Cancellation request: ${requestId}\n${status}` : status;
|
|
898
|
+
} catch (error) {
|
|
899
|
+
return readableError(error);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* Report a launch, or why the foreground sandbox refused it.
|
|
905
|
+
*
|
|
906
|
+
* A blocked launch is an operator-facing answer, not a tool crash: the task was
|
|
907
|
+
* never started, and nothing about it is retried unconfined.
|
|
908
|
+
*/
|
|
909
|
+
function reportLaunch(launch: () => BackgroundTaskMeta): string {
|
|
910
|
+
try {
|
|
911
|
+
return formatLaunch(launch());
|
|
912
|
+
} catch (error) {
|
|
913
|
+
if (error instanceof ForegroundSandboxBlockedError) return error.message;
|
|
914
|
+
throw error;
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Start a watch and wait, bounded by `waitMs`, for its first check, so the launch result
|
|
920
|
+
* shows what the check really prints (#359). Local and SSH watches share this path.
|
|
921
|
+
*/
|
|
922
|
+
export async function launchWatch(
|
|
923
|
+
pi: ExtensionAPI,
|
|
924
|
+
params: WatchTaskParams,
|
|
925
|
+
cwd: string,
|
|
926
|
+
callbackOrigin: BackgroundTaskCallbackOrigin | undefined,
|
|
927
|
+
getActiveSession: () => BackgroundTaskCallbackOrigin | undefined,
|
|
928
|
+
waitMs = FIRST_WATCH_CHECK_WAIT_MS,
|
|
929
|
+
signal?: AbortSignal,
|
|
930
|
+
runtime?: ToolRuntime,
|
|
931
|
+
): Promise<string> {
|
|
932
|
+
const { startWatchTask, awaitFirstWatchCheck } = runtime ?? await import("./runtime.js");
|
|
933
|
+
let meta: BackgroundTaskMeta;
|
|
934
|
+
try {
|
|
935
|
+
meta = startWatchTask(pi, params, cwd, callbackOrigin, getActiveSession);
|
|
936
|
+
} catch (error) {
|
|
937
|
+
if (error instanceof ForegroundSandboxBlockedError) return error.message;
|
|
938
|
+
throw error;
|
|
939
|
+
}
|
|
940
|
+
// Esc (the tool's abort signal) ends the wait at once; the watch itself keeps running.
|
|
941
|
+
const outcome = await awaitFirstWatchCheck(meta.id, waitMs, signal);
|
|
942
|
+
const latest = readMeta(meta.id) ?? meta;
|
|
943
|
+
if (outcome && !("pending" in outcome && isTerminalStatus(latest.status))) return formatLaunch(latest, outcome);
|
|
944
|
+
return formatLaunch(latest);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/** Origins whose session identity could not be read (the host threw). */
|
|
948
|
+
const unavailableSessionOrigins = new WeakSet<BackgroundTaskCallbackOrigin>();
|
|
949
|
+
|
|
950
|
+
function getCallbackOrigin(ctx: ExtensionContext | undefined): BackgroundTaskCallbackOrigin {
|
|
951
|
+
let sessionId: string | undefined;
|
|
952
|
+
let unavailable = false;
|
|
953
|
+
try {
|
|
954
|
+
sessionId = ctx?.sessionManager?.getSessionId();
|
|
955
|
+
} catch {
|
|
956
|
+
sessionId = undefined;
|
|
957
|
+
unavailable = true;
|
|
958
|
+
}
|
|
959
|
+
const origin: BackgroundTaskCallbackOrigin = { cwd: ctx?.cwd ?? "", sessionId,
|
|
960
|
+
branchId: ctx?.sessionManager?.getLeafId?.() ?? null };
|
|
961
|
+
if (unavailable) unavailableSessionOrigins.add(origin);
|
|
962
|
+
return origin;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
function scopeOptions(origin?: BackgroundTaskCallbackOrigin): Pick<OutputOptions, "origin" | "sessionUnavailable"> {
|
|
966
|
+
return {
|
|
967
|
+
origin,
|
|
968
|
+
...(origin && unavailableSessionOrigins.has(origin) ? { sessionUnavailable: true } : {}),
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
function logText(id: string, options: OutputOptions) {
|
|
973
|
+
const body = formatLog(id, options);
|
|
974
|
+
if (!inspectMeta(id).meta) return text(body);
|
|
975
|
+
return text(body, buildBackgroundTaskLogDisplayDetails(body));
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
export function buildBackgroundTaskLogDisplayDetails(body: string) {
|
|
979
|
+
const fullLines = String(body ?? "").split(/\r?\n/);
|
|
980
|
+
const head = fullLines[0] || "bg_task_log";
|
|
981
|
+
const rest = fullLines.slice(1);
|
|
982
|
+
const compactLines = nonEmptyPreviewLines(rest);
|
|
983
|
+
return {
|
|
984
|
+
kind: "background-task-log-display",
|
|
985
|
+
head,
|
|
986
|
+
fullLineCount: fullLines.length,
|
|
987
|
+
compactLines,
|
|
988
|
+
foldedLineCount: Math.max(0, rest.length - compactLines.length),
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
export function renderBackgroundTaskLogDisplay(result: unknown, options: unknown = {}, theme: unknown = {}) {
|
|
993
|
+
const fullText = resultTextContent(result);
|
|
994
|
+
const details = ((result as { details?: unknown })?.details as ReturnType<typeof buildBackgroundTaskLogDisplayDetails> | undefined);
|
|
995
|
+
if (!details || details.kind !== "background-task-log-display") return renderLines(fullText.split(/\r?\n/));
|
|
996
|
+
const expanded = (options as { expanded?: boolean })?.expanded === true;
|
|
997
|
+
const meta = `${details.fullLineCount} lines`;
|
|
998
|
+
|
|
999
|
+
if (expanded) {
|
|
1000
|
+
return renderLines([
|
|
1001
|
+
`${themed(theme, "accent", "bg_task_log")} ${themed(theme, "dim", `· ${meta}`)}`,
|
|
1002
|
+
themed(theme, "dim", "Full displayed log. Click or collapse to fold."),
|
|
1003
|
+
"",
|
|
1004
|
+
...fullText.split(/\r?\n/),
|
|
1005
|
+
], "wrap");
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
const folded = details.foldedLineCount > 0
|
|
1009
|
+
? themed(theme, "dim", `Folded ${details.foldedLineCount} display lines. Click or expand for the requested log payload.`)
|
|
1010
|
+
: themed(theme, "dim", "Compact log. Expand for full display if needed.");
|
|
1011
|
+
return renderLines([
|
|
1012
|
+
`${themed(theme, "accent", "bg_task_log")} ${themed(theme, "dim", `· ${meta}`)}`,
|
|
1013
|
+
details.head,
|
|
1014
|
+
"",
|
|
1015
|
+
themed(theme, "dim", "preview"),
|
|
1016
|
+
...details.compactLines,
|
|
1017
|
+
folded,
|
|
1018
|
+
]);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
function resultTextContent(result: unknown): string {
|
|
1022
|
+
const content = (result as { content?: Array<{ text?: string }> })?.content;
|
|
1023
|
+
if (Array.isArray(content)) return content.map((part) => part.text ?? "").join("\n");
|
|
1024
|
+
return String(result ?? "");
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
function nonEmptyPreviewLines(lines: string[]): string[] {
|
|
1028
|
+
const nonEmpty = lines.filter((line) => line.trim().length > 0).slice(0, 8);
|
|
1029
|
+
return nonEmpty.length ? nonEmpty : lines.slice(0, 3);
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
function themed(theme: unknown, color: string, value: string): string {
|
|
1033
|
+
const fg = (theme as { fg?: (color: string, text: string) => string })?.fg;
|
|
1034
|
+
return typeof fg === "function" ? fg(color, value) : value;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
function renderLines(lines: string[], mode: "truncate" | "wrap" = "truncate") {
|
|
1038
|
+
return {
|
|
1039
|
+
render(width: number = 80) {
|
|
1040
|
+
return mode === "wrap"
|
|
1041
|
+
? lines.flatMap((line) => wrapLineToVisibleWidth(line, width))
|
|
1042
|
+
: lines.map((line) => truncateToVisibleWidth(line, width));
|
|
1043
|
+
},
|
|
1044
|
+
invalidate() { /* stateless */ },
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
function wrapLineToVisibleWidth(line: string, width: number): string[] {
|
|
1049
|
+
const str = String(line ?? "");
|
|
1050
|
+
const max = Math.max(1, Number(width) || 80);
|
|
1051
|
+
if (truncateToVisibleWidth(str, max) === str) return [str];
|
|
1052
|
+
const out: string[] = [];
|
|
1053
|
+
let current = "";
|
|
1054
|
+
let visible = 0;
|
|
1055
|
+
for (const char of str) {
|
|
1056
|
+
if (visible >= max) {
|
|
1057
|
+
out.push(current);
|
|
1058
|
+
current = "";
|
|
1059
|
+
visible = 0;
|
|
1060
|
+
}
|
|
1061
|
+
current += char;
|
|
1062
|
+
visible += 1;
|
|
1063
|
+
}
|
|
1064
|
+
out.push(current);
|
|
1065
|
+
return out;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
function truncateToVisibleWidth(value: string, width: number): string {
|
|
1069
|
+
const max = Math.max(0, Math.floor(width || 0));
|
|
1070
|
+
return String(value ?? "").slice(0, max);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
async function formatStop(
|
|
1074
|
+
pi: ExtensionAPI,
|
|
1075
|
+
id: string,
|
|
1076
|
+
_ctx: ExtensionContext,
|
|
1077
|
+
getActiveSession?: () => BackgroundTaskCallbackOrigin | undefined,
|
|
1078
|
+
options: OutputOptions = {},
|
|
1079
|
+
runtime?: Pick<BackgroundTaskRuntime, "stopTask">,
|
|
1080
|
+
): Promise<string> {
|
|
1081
|
+
const { stopTask } = runtime ?? await import("./runtime.js");
|
|
1082
|
+
const existing = inspectMeta(id);
|
|
1083
|
+
if (!existing.meta && !existing.found) return formatStopResult(existing, options);
|
|
1084
|
+
if (!existing.meta) return formatMissingTask(existing, options);
|
|
1085
|
+
// Same ownership rule as reads (#322): only a task owned by the current
|
|
1086
|
+
// session stops unless the caller passes all:true.
|
|
1087
|
+
const ownership = classifyOwnership(existing.meta, options);
|
|
1088
|
+
if (ownership !== "allow") return formatMutationRefusal(id, ownership, "stop", options);
|
|
1089
|
+
const meta = await stopTask(pi, id, getActiveSession);
|
|
1090
|
+
if (!meta) return formatStopResult({ id, found: existing.found, readable: false, error: existing.error }, options);
|
|
1091
|
+
return formatStopResult({ id: meta.id, meta, found: true, readable: true }, options);
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
/**
|
|
1095
|
+
* Dismiss terminal tasks with the same ownership rule as reads (#322). Bulk
|
|
1096
|
+
* clear never crosses the current session scope (stricter than reads, which
|
|
1097
|
+
* accept all:true for a list); clear with an id honours all:true like a read
|
|
1098
|
+
* by id. Tasks whose ownership cannot be verified are counted, not dismissed.
|
|
1099
|
+
*/
|
|
1100
|
+
function formatClear(params: Record<string, unknown>, options: OutputOptions): string {
|
|
1101
|
+
if (typeof params.id === "string" && params.id) return formatClearById(params.id, options);
|
|
1102
|
+
const statuses = params.status as string[] | undefined;
|
|
1103
|
+
const wanted = statuses && statuses.length > 0 ? new Set(statuses) : undefined;
|
|
1104
|
+
const statusLabel = wanted ? ` matching ${Array.from(wanted).join(",")}` : "";
|
|
1105
|
+
const origin = options.origin;
|
|
1106
|
+
if (!origin || options.sessionUnavailable) {
|
|
1107
|
+
return "Dismissed 0 terminal background tasks: the current session identity is unavailable, so no task's ownership can be verified. Clear one task with id and all:true.";
|
|
1108
|
+
}
|
|
1109
|
+
const scoped: OutputOptions = { ...options, all: false };
|
|
1110
|
+
const now = Date.now();
|
|
1111
|
+
let cleared = 0;
|
|
1112
|
+
let unverified = 0;
|
|
1113
|
+
for (const meta of listMetasForOrigin(origin)) {
|
|
1114
|
+
if (meta.dismissedAt !== undefined) continue;
|
|
1115
|
+
if (!isTerminalStatus(meta.status)) continue;
|
|
1116
|
+
if (wanted && !wanted.has(meta.status)) continue;
|
|
1117
|
+
if (classifyOwnership(meta, scoped) !== "allow") {
|
|
1118
|
+
unverified += 1;
|
|
1119
|
+
continue;
|
|
1120
|
+
}
|
|
1121
|
+
meta.dismissedAt = now;
|
|
1122
|
+
writeMeta(meta);
|
|
1123
|
+
cleared += 1;
|
|
1124
|
+
}
|
|
1125
|
+
const skipped = unverified
|
|
1126
|
+
? ` ${unverified} terminal task${unverified === 1 ? "" : "s"} with unverifiable ownership ${unverified === 1 ? "was" : "were"} not dismissed; clear one with id and all:true.`
|
|
1127
|
+
: "";
|
|
1128
|
+
return `Dismissed ${cleared} terminal background task${cleared === 1 ? "" : "s"}${statusLabel}.${skipped}`;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
function formatClearById(id: string, options: OutputOptions): string {
|
|
1132
|
+
const inspection = inspectMeta(id);
|
|
1133
|
+
if (!inspection.meta) return formatMissingTask(inspection, options);
|
|
1134
|
+
const meta = inspection.meta;
|
|
1135
|
+
const ownership = classifyOwnership(meta, options);
|
|
1136
|
+
if (ownership !== "allow") return formatMutationRefusal(id, ownership, "clear", options);
|
|
1137
|
+
if (!isTerminalStatus(meta.status)) return `Background task ${id} is ${meta.status}; only terminal tasks can be dismissed. Stop it first.`;
|
|
1138
|
+
if (meta.dismissedAt !== undefined) return `Background task ${id} was already dismissed.`;
|
|
1139
|
+
meta.dismissedAt = Date.now();
|
|
1140
|
+
writeMeta(meta);
|
|
1141
|
+
return `Dismissed terminal background task ${id} (${meta.status}).`;
|
|
1142
|
+
}
|