pi-better-server 0.5.0 → 0.7.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 +7 -3
- 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 +37 -5
- package/scripts/event-publication/writer.mjs +3 -8
- package/scripts/measure-full-runtime/frontend.ts +10 -2
- package/scripts/measure-full-runtime/probe.mjs +4 -2
- 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 +2 -0
- package/scripts/measure-shared-reads.mjs +2 -1
- package/scripts/private-runtime/package-lock.json +18 -0
- package/scripts/private-runtime/package.json +4 -1
- package/scripts/source-provenance.mjs +2 -2
- package/scripts/test-on-pi.sh +2 -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 +9 -4
- package/src/runtime/background-tools.ts +181 -27
- package/src/runtime/background.ts +365 -70
- 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 +43 -35
- package/src/runtime/domain/background/process-identity.mjs +20 -2
- package/src/runtime/domain/background/process.mjs +52 -14
- package/src/runtime/domain/background/registry.mjs +61 -3
- package/src/runtime/domain/background/supervisor/store.mjs +3 -3
- package/src/runtime/domain/background/supervisor/supervisor.mjs +9 -3
- package/src/runtime/domain/background/types.mjs +1 -0
- package/src/runtime/domain/background-service/adapter-contract.domain.mjs +1 -1
- package/src/runtime/domain/background-service/adapter-contract.ts +1 -1
- package/src/runtime/domain/background-service/goal-provider.domain.mjs +35 -16
- package/src/runtime/domain/background-service/goal-provider.ts +31 -16
- package/src/runtime/domain/background-service/index.domain.mjs +25 -9
- package/src/runtime/domain/background-service/index.ts +25 -9
- package/src/runtime/domain/background-service/managed-authority.domain.mjs +6 -3
- package/src/runtime/domain/background-service/managed-authority.ts +5 -3
- package/src/runtime/domain/background-service/managed-client.domain.mjs +21 -7
- package/src/runtime/domain/background-service/managed-client.ts +20 -7
- package/src/runtime/domain/background-service/managed-frontend.domain.mjs +159 -19
- package/src/runtime/domain/background-service/managed-frontend.ts +135 -19
- package/src/runtime/domain/background-service/navigator-provider.domain.mjs +21 -5
- package/src/runtime/domain/background-service/navigator-provider.ts +19 -5
- 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 +372 -57
- package/src/runtime/domain/background-service/observed-waits.ts +321 -53
- package/src/runtime/domain/background-service/process-identity.domain.mjs +20 -2
- package/src/runtime/domain/background-service/process-identity.ts +18 -2
- package/src/runtime/domain/background-service/process.domain.mjs +52 -14
- package/src/runtime/domain/background-service/process.ts +48 -14
- package/src/runtime/domain/background-service/registry.domain.mjs +61 -3
- package/src/runtime/domain/background-service/registry.ts +53 -5
- package/src/runtime/domain/background-service/review-settings.domain.mjs +16 -0
- package/src/runtime/domain/background-service/review-settings.ts +18 -0
- package/src/runtime/domain/background-service/runtime.domain.mjs +40 -86
- package/src/runtime/domain/background-service/runtime.ts +27 -82
- package/src/runtime/domain/background-service/sandbox.domain.mjs +14 -15
- package/src/runtime/domain/background-service/sandbox.ts +14 -11
- package/src/runtime/domain/background-service/shared-callback-batcher.domain.mjs +8 -1
- package/src/runtime/domain/background-service/shared-callback-batcher.ts +11 -2
- package/src/runtime/domain/background-service/shared-navigator-panels.domain.mjs +24 -2
- package/src/runtime/domain/background-service/shared-navigator-panels.ts +26 -4
- package/src/runtime/domain/background-service/shared-navigator.domain.mjs +273 -41
- package/src/runtime/domain/background-service/shared-navigator.ts +244 -40
- 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 +53 -41
- 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 +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.ts +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.domain.mjs +11 -2
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.ts +14 -3
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.domain.mjs +4 -2
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.ts +5 -3
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.domain.mjs +44 -4
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.ts +50 -2
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.ts +9 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.domain.mjs +12 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.ts +12 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.domain.mjs +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.ts +2 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.domain.mjs +42 -21
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.ts +40 -24
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +26 -4
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +40 -8
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.domain.mjs +23 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.ts +24 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.domain.mjs +1 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.ts +5 -1
- package/src/runtime/domain/background-service/shared-sandbox-core.ts +1 -1
- package/src/runtime/domain/background-service/shared-work-client.mjs +174 -7
- package/src/runtime/domain/background-service/stall.domain.mjs +5 -3
- package/src/runtime/domain/background-service/stall.ts +7 -4
- package/src/runtime/domain/background-service/supervised-progress.domain.mjs +42 -0
- package/src/runtime/domain/background-service/supervised-progress.ts +43 -0
- package/src/runtime/domain/background-service/supervisor/README.md +1 -4
- package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +1 -0
- package/src/runtime/domain/background-service/supervisor/generated/process-identity.mjs +20 -2
- package/src/runtime/domain/background-service/supervisor/generated/process.mjs +52 -14
- package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +47 -4
- package/src/runtime/domain/background-service/supervisor/generated/store.mjs +3 -3
- package/src/runtime/domain/background-service/supervisor/generated/watch-fingerprint.mjs +69 -0
- package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +47 -4
- package/src/runtime/domain/background-service/supervisor/runner.ts +38 -3
- package/src/runtime/domain/background-service/supervisor/store.domain.mjs +3 -3
- package/src/runtime/domain/background-service/supervisor/store.ts +3 -3
- package/src/runtime/domain/background-service/supervisor/supervisor.domain.mjs +9 -3
- package/src/runtime/domain/background-service/supervisor/supervisor.ts +6 -3
- package/src/runtime/domain/background-service/task-diagnostics.domain.mjs +1 -1
- package/src/runtime/domain/background-service/task-diagnostics.ts +1 -1
- package/src/runtime/domain/background-service/tools.domain.mjs +53 -40
- package/src/runtime/domain/background-service/tools.ts +67 -54
- package/src/runtime/domain/background-service/types.domain.mjs +1 -0
- package/src/runtime/domain/background-service/types.ts +7 -3
- package/src/runtime/domain/background-service/watch-fingerprint.domain.mjs +67 -0
- package/src/runtime/domain/background-service/watch-fingerprint.ts +70 -0
- package/src/runtime/domain/background-service/work-observation.domain.mjs +46 -20
- package/src/runtime/domain/background-service/work-observation.ts +46 -19
- 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-requests.mjs +11 -2
- 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/outbox.mjs +7 -0
- 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 +42 -21
- package/src/runtime/domain/coordinator/src/store.mjs +26 -4
- package/src/runtime/domain/coordinator/src/subagent-launch.mjs +23 -1
- package/src/runtime/domain/coordinator/src/types.mjs +1 -1
- 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/log-paths.mjs +8 -1
- package/src/runtime/domain/parse.d.mts +4 -1
- package/src/runtime/domain/parse.mjs +58 -9
- package/src/runtime/domain/sandbox-policy/README.md +63 -65
- package/src/runtime/domain/sandbox-policy/commands.domain.mjs +11 -8
- package/src/runtime/domain/sandbox-policy/commands.ts +12 -7
- package/src/runtime/domain/sandbox-policy/index.domain.mjs +53 -9
- package/src/runtime/domain/sandbox-policy/index.ts +52 -9
- package/src/runtime/domain/sandbox-policy/package.json +1 -1
- package/src/runtime/domain/sandbox-policy/permission-settings.domain.mjs +5 -2
- package/src/runtime/domain/sandbox-policy/permission-settings.ts +9 -2
- package/src/runtime/domain/sandbox-policy/permissions-page.domain.mjs +56 -137
- package/src/runtime/domain/sandbox-policy/permissions-page.ts +52 -130
- package/src/runtime/domain/sandbox-policy/permissions.domain.mjs +1 -6
- package/src/runtime/domain/sandbox-policy/permissions.ts +1 -6
- package/src/runtime/domain/sandbox-policy/shared-sandbox-core.ts +1 -1
- package/src/runtime/domain/sandbox-policy/shared-task-exec.domain.mjs +14 -1
- package/src/runtime/domain/sandbox-policy/shared-task-exec.ts +12 -1
- 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 +3 -2
- package/src/runtime/domain/sandbox-policy/shared-task-process-list.ts +8 -2
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.domain.mjs +4 -3
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.ts +5 -3
- package/src/runtime/domain/sandbox-policy/shared-task-tools.domain.mjs +8 -5
- package/src/runtime/domain/sandbox-policy/shared-task-tools.ts +8 -5
- package/src/runtime/domain/session-store.mjs +7 -0
- package/src/runtime/domain/subagents/README.md +13 -11
- package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +10 -7
- package/src/runtime/domain/subagents/assignment-outcomes.ts +9 -6
- package/src/runtime/domain/subagents/assignment-report.domain.mjs +109 -24
- package/src/runtime/domain/subagents/assignment-report.ts +103 -19
- package/src/runtime/domain/subagents/batch.mjs +13 -5
- package/src/runtime/domain/subagents/catalog-host-admission.domain.mjs +21 -3
- package/src/runtime/domain/subagents/catalog-host-admission.ts +19 -3
- package/src/runtime/domain/subagents/catalog-preferences.ts +1 -1
- package/src/runtime/domain/subagents/catalog-resolver.domain.mjs +35 -1
- package/src/runtime/domain/subagents/catalog-resolver.ts +35 -1
- package/src/runtime/domain/subagents/catalog-store.domain.mjs +20 -22
- package/src/runtime/domain/subagents/catalog-store.ts +27 -20
- 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-dependencies.mjs +27 -15
- package/src/runtime/domain/subagents/child-operations.domain.mjs +80 -2
- package/src/runtime/domain/subagents/child-operations.ts +80 -1
- package/src/runtime/domain/subagents/child-preflight.mjs +9 -2
- 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/cleanup.domain.mjs +11 -2
- package/src/runtime/domain/subagents/cleanup.ts +10 -2
- package/src/runtime/domain/subagents/config.domain.mjs +54 -2
- package/src/runtime/domain/subagents/config.ts +53 -3
- package/src/runtime/domain/subagents/continuation-authority.domain.mjs +5 -1
- package/src/runtime/domain/subagents/continuation-authority.ts +3 -1
- 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 +4 -2
- package/src/runtime/domain/subagents/continuation.ts +4 -2
- package/src/runtime/domain/subagents/extensions.mjs +11 -13
- package/src/runtime/domain/subagents/failures.domain.mjs +11 -2
- package/src/runtime/domain/subagents/failures.ts +10 -2
- package/src/runtime/domain/subagents/finalization.domain.mjs +16 -1
- package/src/runtime/domain/subagents/finalization.ts +21 -1
- package/src/runtime/domain/subagents/goal-provider.domain.mjs +44 -2
- package/src/runtime/domain/subagents/goal-provider.ts +44 -2
- package/src/runtime/domain/subagents/health-observation.domain.mjs +189 -190
- package/src/runtime/domain/subagents/health-observation.ts +43 -48
- package/src/runtime/domain/subagents/index.domain.mjs +457 -182
- package/src/runtime/domain/subagents/index.ts +417 -155
- package/src/runtime/domain/subagents/isolated-workspace.domain.mjs +515 -0
- package/src/runtime/domain/subagents/isolated-workspace.ts +557 -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/list.mjs +2 -2
- package/src/runtime/domain/subagents/log-cursor.domain.mjs +85 -16
- package/src/runtime/domain/subagents/log-cursor.ts +91 -15
- package/src/runtime/domain/subagents/managed-launch-bootstrap.mjs +2 -2
- package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +27 -7
- package/src/runtime/domain/subagents/managed-launch-client.ts +28 -8
- package/src/runtime/domain/subagents/managed-launch-host.domain.mjs +222 -14
- package/src/runtime/domain/subagents/managed-launch-host.ts +168 -15
- package/src/runtime/domain/subagents/managed-launch-journal.mjs +1 -5
- package/src/runtime/domain/subagents/managed-launch-port.ts +4 -0
- package/src/runtime/domain/subagents/operation-bridge-types.domain.mjs +27 -2
- package/src/runtime/domain/subagents/operation-bridge-types.ts +27 -2
- package/src/runtime/domain/subagents/output-payload.domain.mjs +24 -9
- package/src/runtime/domain/subagents/output-payload.ts +26 -12
- 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 +4 -2
- package/src/runtime/domain/subagents/parse.domain.mjs +58 -9
- package/src/runtime/domain/subagents/parse.ts +57 -10
- package/src/runtime/domain/subagents/permission-policy.domain.mjs +32 -8
- package/src/runtime/domain/subagents/permission-policy.ts +41 -8
- package/src/runtime/domain/subagents/process-identity.mjs +33 -0
- package/src/runtime/domain/subagents/registry.domain.mjs +71 -51
- package/src/runtime/domain/subagents/registry.ts +77 -38
- package/src/runtime/domain/subagents/retained-result.domain.mjs +56 -0
- package/src/runtime/domain/subagents/retained-result.ts +65 -0
- package/src/runtime/domain/subagents/session-selection.domain.mjs +3 -1
- package/src/runtime/domain/subagents/session-selection.ts +2 -1
- package/src/runtime/domain/subagents/session-store.domain.mjs +7 -0
- package/src/runtime/domain/subagents/session-store.ts +9 -0
- package/src/runtime/domain/subagents/settings-page.domain.mjs +151 -107
- package/src/runtime/domain/subagents/settings-page.ts +147 -108
- package/src/runtime/domain/subagents/shared-callback-batcher.domain.mjs +8 -1
- package/src/runtime/domain/subagents/shared-callback-batcher.ts +11 -2
- package/src/runtime/domain/subagents/shared-catalog.domain.mjs +28 -29
- package/src/runtime/domain/subagents/shared-catalog.ts +29 -21
- package/src/runtime/domain/subagents/shared-navigator-panels.domain.mjs +24 -2
- package/src/runtime/domain/subagents/shared-navigator-panels.ts +26 -4
- package/src/runtime/domain/subagents/shared-navigator.domain.mjs +273 -41
- package/src/runtime/domain/subagents/shared-navigator.ts +244 -40
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-inbox.mjs +144 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-observation.mjs +53 -41
- 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 +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.ts +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.domain.mjs +11 -2
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.ts +14 -3
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.domain.mjs +4 -2
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.ts +5 -3
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.domain.mjs +44 -4
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.ts +50 -2
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.ts +9 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.domain.mjs +12 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.ts +12 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.domain.mjs +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.ts +2 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.domain.mjs +42 -21
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.ts +40 -24
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +26 -4
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +40 -8
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.domain.mjs +23 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.ts +24 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.domain.mjs +1 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.ts +5 -1
- package/src/runtime/domain/subagents/shared-sandbox-core.ts +1 -1
- package/src/runtime/domain/subagents/shared-task-exec.domain.mjs +14 -1
- package/src/runtime/domain/subagents/shared-task-exec.ts +12 -1
- 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 +3 -2
- package/src/runtime/domain/subagents/shared-task-process-list.ts +8 -2
- package/src/runtime/domain/subagents/shared-task-sandbox.domain.mjs +4 -3
- package/src/runtime/domain/subagents/shared-task-sandbox.ts +5 -3
- package/src/runtime/domain/subagents/shared-task-tools.domain.mjs +8 -5
- package/src/runtime/domain/subagents/shared-task-tools.ts +8 -5
- package/src/runtime/domain/subagents/shared-work-client.mjs +174 -7
- package/src/runtime/domain/subagents/subagent-tools.domain.mjs +22 -38
- package/src/runtime/domain/subagents/subagent-tools.ts +28 -34
- package/src/runtime/domain/subagents/task-guard.domain.mjs +14 -1
- package/src/runtime/domain/subagents/task-guard.ts +13 -1
- package/src/runtime/domain/subagents/task-policy.domain.mjs +3 -2
- package/src/runtime/domain/subagents/task-policy.ts +8 -2
- package/src/runtime/domain/subagents/timing.domain.mjs +10 -1
- package/src/runtime/domain/subagents/timing.ts +10 -1
- package/src/runtime/domain/subagents/tools.domain.mjs +4 -8
- package/src/runtime/domain/subagents/tools.ts +4 -7
- package/src/runtime/domain/subagents/work-observation.domain.mjs +102 -17
- package/src/runtime/domain/subagents/work-observation.ts +91 -17
- package/src/runtime/domain/subagents/worker-operations-host.domain.mjs +9 -4
- package/src/runtime/domain/subagents/worker-operations-host.ts +12 -4
- package/src/runtime/domain/subagents/worker-waits.domain.mjs +89 -35
- package/src/runtime/domain/subagents/worker-waits.ts +93 -34
- package/src/runtime/domain/subagents/workspace-files.domain.mjs +103 -0
- package/src/runtime/domain/subagents/workspace-files.ts +114 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.d.mts +38 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.mjs +5 -0
- package/src/runtime/domain/subagents/workspace-scope.ts +6 -0
- package/src/runtime/domain/subagents/worktree-feedback.domain.mjs +55 -0
- package/src/runtime/domain/subagents/worktree-feedback.ts +77 -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 +25 -2
- package/src/runtime/query-executor.ts +149 -28
- package/src/runtime/query-worker.mjs +106 -21
- package/src/runtime/read-model.ts +849 -121
- package/src/runtime/read-watchdog-probe.ts +4 -0
- package/src/runtime/result-supervision.ts +188 -16
- package/src/runtime/root.ts +5 -2
- package/src/runtime/source-watch.ts +175 -0
- package/src/runtime/subagent-launch.ts +11 -4
- 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 +104 -23
- package/src/session-activity.ts +26 -4
- package/src/version.ts +1 -1
|
@@ -3,12 +3,17 @@ import { randomUUID } from 'node:crypto';
|
|
|
3
3
|
import { requestBackgroundTool } from "./managed-client.domain.mjs";
|
|
4
4
|
import { onBackgroundObservation } from "./work-observation.domain.mjs";
|
|
5
5
|
import { getCallbackBatcher } from "./shared-callback-batcher.domain.mjs";
|
|
6
|
-
import { registerObservedWaitClient } from "./observed-
|
|
6
|
+
import { registerObservedWaitClient } from "./observed-wait-client.domain.mjs";
|
|
7
|
+
import { observeNavigatorLaunch } from "./shared-navigator.domain.mjs";
|
|
7
8
|
/** Native transcript delivery and cached UI projections; no coordinator or supervisor. */
|
|
8
9
|
export function createManagedBackgroundFrontend(pi) {
|
|
9
10
|
let ctx, generation = 0, refresh, dirty = false;
|
|
10
11
|
let rows = [], legacyRows = [], details = new Map(), logs = new Map();
|
|
12
|
+
let pendingAttention = new Set();
|
|
13
|
+
const retainedTasks = new Map();
|
|
14
|
+
const cachedTask = (id) => rows.find(row => row.id === id) ?? retainedTasks.get(id);
|
|
11
15
|
let adoptedScope;
|
|
16
|
+
let observing = false;
|
|
12
17
|
const scopeOf = (context) => JSON.stringify([context.cwd, context.sessionManager.getSessionId()]);
|
|
13
18
|
const ownership = new Map();
|
|
14
19
|
const adoptContext = (context) => {
|
|
@@ -18,7 +23,9 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
18
23
|
legacyRows = [];
|
|
19
24
|
details.clear();
|
|
20
25
|
logs.clear();
|
|
26
|
+
retainedTasks.clear();
|
|
21
27
|
generation++;
|
|
28
|
+
pendingAttention.clear();
|
|
22
29
|
}
|
|
23
30
|
ctx = context;
|
|
24
31
|
adoptedScope = scopeOf(context);
|
|
@@ -34,6 +41,21 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
34
41
|
ownership.delete(ownership.keys().next().value);
|
|
35
42
|
};
|
|
36
43
|
const queued = new Set(), listeners = new Set();
|
|
44
|
+
const traceSeen = new Set();
|
|
45
|
+
const trace = (stage, facts = {}) => {
|
|
46
|
+
if (process.env.PI_BG_OBSERVED_WAITS_DEBUG !== '1')
|
|
47
|
+
return;
|
|
48
|
+
const full = JSON.stringify({ stage, sessionId: ctx?.sessionManager.getSessionId(), ...facts });
|
|
49
|
+
const line = full.length <= 4096 ? full : JSON.stringify({ stage, truncated: true });
|
|
50
|
+
if (traceSeen.has(line) || traceSeen.size >= 128)
|
|
51
|
+
return;
|
|
52
|
+
const output = traceSeen.size === 127 ? JSON.stringify({ stage: 'capture-limit', limit: 128 }) : line;
|
|
53
|
+
traceSeen.add(line);
|
|
54
|
+
try {
|
|
55
|
+
console.error('[background-handoff-frontend]', output);
|
|
56
|
+
}
|
|
57
|
+
catch { /* diagnostics never own delivery */ }
|
|
58
|
+
};
|
|
37
59
|
const active = (origin, branch) => Boolean(ctx && origin.cwd === ctx.cwd && origin.sessionId === ctx.sessionManager.getSessionId()
|
|
38
60
|
&& (!branch || ctx.sessionManager.getBranch().some(entry => entry.id === branch)));
|
|
39
61
|
const request = (tool, params) => {
|
|
@@ -45,6 +67,38 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
45
67
|
for (const listener of listeners)
|
|
46
68
|
listener({ workspace: ctx?.cwd, sessionId: ctx?.sessionManager.getSessionId() });
|
|
47
69
|
};
|
|
70
|
+
let handoffRetry;
|
|
71
|
+
let attentionRetry;
|
|
72
|
+
let retryAt = Infinity;
|
|
73
|
+
const scheduleHandoffPoll = (delayMs) => {
|
|
74
|
+
if (!ctx || retryAt <= Date.now() + delayMs)
|
|
75
|
+
return;
|
|
76
|
+
clearTimeout(handoffRetry);
|
|
77
|
+
retryAt = Date.now() + delayMs;
|
|
78
|
+
// Poll again before attempting delivery: the task or its recipient may have changed.
|
|
79
|
+
handoffRetry = setTimeout(() => {
|
|
80
|
+
handoffRetry = undefined;
|
|
81
|
+
retryAt = Infinity;
|
|
82
|
+
if (ctx)
|
|
83
|
+
void reconcile().catch(() => undefined);
|
|
84
|
+
}, delayMs);
|
|
85
|
+
handoffRetry.unref();
|
|
86
|
+
};
|
|
87
|
+
const waitForAttentionIdle = () => {
|
|
88
|
+
if (attentionRetry)
|
|
89
|
+
return;
|
|
90
|
+
attentionRetry = setTimeout(() => {
|
|
91
|
+
attentionRetry = undefined;
|
|
92
|
+
if (!ctx || !pendingAttention.size)
|
|
93
|
+
return;
|
|
94
|
+
if (!ctx.isIdle()) {
|
|
95
|
+
waitForAttentionIdle();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
void reconcile().catch(() => undefined);
|
|
99
|
+
}, 1000);
|
|
100
|
+
attentionRetry.unref();
|
|
101
|
+
};
|
|
48
102
|
const reconcile = () => {
|
|
49
103
|
if (!ctx)
|
|
50
104
|
return Promise.resolve();
|
|
@@ -55,21 +109,52 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
55
109
|
dirty = false;
|
|
56
110
|
const captured = ctx, version = generation;
|
|
57
111
|
refresh = (async () => {
|
|
58
|
-
const
|
|
112
|
+
const rechecking = pendingAttention.size && captured.isIdle() ? pendingAttention : undefined;
|
|
113
|
+
trace('poll-start');
|
|
114
|
+
const snapshot = await request('__background_poll', { knownActiveIds: rows.filter(row => row.active).map(row => row.id).slice(0, 200) });
|
|
115
|
+
trace('poll-received', { stale: version !== generation, total: snapshot.deliveries?.length ?? 0, deliveries: (snapshot.deliveries ?? []).slice(0, 8).map((item) => ({ taskId: item.notification.taskId, receiptId: item.receiptId })) });
|
|
59
116
|
if (version !== generation)
|
|
60
117
|
return;
|
|
118
|
+
if (Number.isFinite(snapshot.retryAfterMs))
|
|
119
|
+
scheduleHandoffPoll(snapshot.retryAfterMs);
|
|
61
120
|
if (snapshot.recoveryPending === true)
|
|
62
121
|
dirty = true;
|
|
63
|
-
|
|
64
|
-
|
|
122
|
+
// A rebuilding snapshot is not evidence that visible tasks vanished.
|
|
123
|
+
if (snapshot.status !== 'rebuilding' && snapshot.status !== 'degraded') {
|
|
124
|
+
// Older servers include compact callback pointers in the task list.
|
|
125
|
+
rows = (snapshot.tasks ?? []).filter((task) => !task.callbackPending);
|
|
126
|
+
// Detail remains inspectable while Navigator retains a terminal row.
|
|
127
|
+
for (const task of rows) {
|
|
128
|
+
retainedTasks.delete(task.id);
|
|
129
|
+
retainedTasks.set(task.id, task);
|
|
130
|
+
if (retainedTasks.size > 256)
|
|
131
|
+
retainedTasks.delete(retainedTasks.keys().next().value);
|
|
132
|
+
}
|
|
133
|
+
legacyRows = snapshot.legacyRows ?? [];
|
|
134
|
+
}
|
|
65
135
|
notify();
|
|
136
|
+
const deferredAttention = new Set();
|
|
66
137
|
for (const item of snapshot.deliveries ?? []) {
|
|
67
138
|
if (queued.has(item.token))
|
|
68
139
|
continue;
|
|
140
|
+
const n = item.notification;
|
|
141
|
+
const type = n.event?.customType;
|
|
142
|
+
if ((type === 'background-task-stall' || type === 'background-wait-review')
|
|
143
|
+
&& (!rechecking?.has(item.receiptId) || !captured.isIdle())) {
|
|
144
|
+
// Keep the candidate here while busy. Pi's followUp queue cannot
|
|
145
|
+
// revalidate it when the current turn eventually ends.
|
|
146
|
+
deferredAttention.add(item.receiptId);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
69
149
|
queued.add(item.token);
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
150
|
+
const facts = { taskId: item.notification.taskId, receiptId: item.receiptId };
|
|
151
|
+
const receipt = (delivered, reason) => {
|
|
152
|
+
trace('handoff', { ...facts, delivered, reason: reason?.slice(0, 256) });
|
|
153
|
+
void requestBackgroundTool(pi, captured, randomUUID(), '__background_receipt', { token: item.token, delivered, reason })
|
|
154
|
+
.then(() => trace('receipt-return', facts), error => trace('receipt-error', { ...facts, error: String(error).slice(0, 256) }))
|
|
155
|
+
.finally(() => { queued.delete(item.token); if (ctx === captured)
|
|
156
|
+
void reconcile().catch(() => undefined); });
|
|
157
|
+
};
|
|
73
158
|
const proof = captured.sessionManager.getBranch().some((entry) => {
|
|
74
159
|
const message = entry.type === 'custom_message' ? entry : entry.message;
|
|
75
160
|
return item.notification.urgent ? message?.customType === item.notification.event.customType && message.details?.urgentEventId === item.receiptId : message?.customType === 'background-completion-batch' && message.details?.completionEventIds?.includes(item.receiptId);
|
|
@@ -78,9 +163,15 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
78
163
|
receipt(true);
|
|
79
164
|
continue;
|
|
80
165
|
}
|
|
81
|
-
|
|
166
|
+
trace('queue', { ...facts, urgent: n.urgent === true, recipientActive: active(n.origin, n.branchId) });
|
|
82
167
|
if (n.urgent) {
|
|
83
|
-
|
|
168
|
+
const handoff = getCallbackBatcher(pi).deliverUrgent({ ...n.event, receiptId: item.receiptId,
|
|
169
|
+
getSuppressionReason: () => active(n.origin, n.branchId) ? undefined : 'The originating session or branch is no longer active',
|
|
170
|
+
onDelivered: () => receipt(true), onSuppressed: reason => receipt(false, reason) });
|
|
171
|
+
const retry = () => { queued.delete(item.token); if (version === generation)
|
|
172
|
+
scheduleHandoffPoll(1000); };
|
|
173
|
+
void Promise.resolve(handoff).then(ok => { if (!ok)
|
|
174
|
+
retry(); }, retry);
|
|
84
175
|
continue;
|
|
85
176
|
}
|
|
86
177
|
getCallbackBatcher(pi).enqueue({ ...n.event, source: 'background-task', id: n.taskId, label: n.taskId, status: n.waitId ? `${n.status}; wait ${n.waitId} ready` : n.status,
|
|
@@ -88,22 +179,66 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
88
179
|
getSuppressionReason: () => active(n.origin, n.branchId) ? undefined : 'The originating session or branch is no longer active',
|
|
89
180
|
onDelivered: () => receipt(true), onSuppressed: (reason) => receipt(false, reason) });
|
|
90
181
|
}
|
|
91
|
-
|
|
182
|
+
pendingAttention = deferredAttention;
|
|
183
|
+
if (pendingAttention.size) {
|
|
184
|
+
if (captured.isIdle())
|
|
185
|
+
dirty = true;
|
|
186
|
+
else
|
|
187
|
+
waitForAttentionIdle();
|
|
188
|
+
}
|
|
189
|
+
})().catch(error => {
|
|
190
|
+
trace('poll-error', { error: String(error).slice(0, 256) });
|
|
191
|
+
if (version === generation && pendingAttention.size)
|
|
192
|
+
waitForAttentionIdle();
|
|
193
|
+
throw error;
|
|
194
|
+
})
|
|
195
|
+
.finally(() => { refresh = undefined; if (dirty && ctx)
|
|
92
196
|
void reconcile().catch(() => undefined); });
|
|
93
197
|
return refresh;
|
|
94
198
|
};
|
|
199
|
+
const activate = () => {
|
|
200
|
+
if (observing || !ctx)
|
|
201
|
+
return;
|
|
202
|
+
observing = true;
|
|
203
|
+
const version = generation;
|
|
204
|
+
void reconcile().catch(() => { if (generation === version)
|
|
205
|
+
observing = false; });
|
|
206
|
+
};
|
|
95
207
|
const host = {
|
|
96
208
|
async reconcileNow() { await reconcile(); while (refresh)
|
|
97
209
|
await refresh; },
|
|
98
|
-
start(context
|
|
210
|
+
start(context, restore = true) {
|
|
211
|
+
adoptContext(context);
|
|
212
|
+
generation++;
|
|
213
|
+
observing = false;
|
|
214
|
+
if (restore)
|
|
215
|
+
activate();
|
|
216
|
+
},
|
|
99
217
|
noteParentContext(context) { adoptContext(context); generation++; void reconcile().catch(() => undefined); },
|
|
100
|
-
close() {
|
|
218
|
+
close() {
|
|
219
|
+
clearTimeout(handoffRetry);
|
|
220
|
+
clearTimeout(attentionRetry);
|
|
221
|
+
handoffRetry = attentionRetry = undefined;
|
|
222
|
+
retryAt = Infinity;
|
|
223
|
+
observing = false;
|
|
224
|
+
ctx = undefined;
|
|
225
|
+
adoptedScope = undefined;
|
|
226
|
+
generation++;
|
|
227
|
+
rows = [];
|
|
228
|
+
legacyRows = [];
|
|
229
|
+
details.clear();
|
|
230
|
+
logs.clear();
|
|
231
|
+
retainedTasks.clear();
|
|
232
|
+
queued.clear();
|
|
233
|
+
ownership.clear();
|
|
234
|
+
pendingAttention.clear();
|
|
235
|
+
},
|
|
101
236
|
isParentOriginActive: active,
|
|
102
237
|
onDurableTaskChange(listener) { listeners.add(listener); return () => listeners.delete(listener); },
|
|
103
|
-
listDurableTasks(origin,
|
|
104
|
-
readDurableTask(origin, id) { return active(origin) ?
|
|
238
|
+
listDurableTasks(origin, _options = {}) { return active(origin) ? rows : []; },
|
|
239
|
+
readDurableTask(origin, id) { const task = cachedTask(id); return active(origin, task?.parentRecipient?.branchId) ? task : undefined; },
|
|
105
240
|
ownsDurableTask(id) { if (!ctx || adoptedScope !== scopeOf(ctx))
|
|
106
|
-
return false; return ownership.has(id) || rows.some(row => row.id === id); },
|
|
241
|
+
return false; return ownership.has(id) || rows.some(row => row.id === id && !row.readOnly); },
|
|
107
242
|
readDurableTaskLog(origin, id, lines = 40) {
|
|
108
243
|
if (!active(origin))
|
|
109
244
|
return;
|
|
@@ -135,7 +270,10 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
135
270
|
throw new Error('Native Background context unavailable');
|
|
136
271
|
adoptContext(context);
|
|
137
272
|
const capturedGeneration = generation, capturedScope = adoptedScope;
|
|
138
|
-
const
|
|
273
|
+
const launch = () => requestBackgroundTool(pi, context, id, definition.name, params, definition.parameters, signal);
|
|
274
|
+
const spawning = ['bg_task_spawn', 'bg_task_spawn_batch', 'bg_task_watch'].includes(definition.name)
|
|
275
|
+
|| definition.name === 'bg_task' && ['spawn', 'watch'].includes(params?.action);
|
|
276
|
+
const result = spawning ? await observeNavigatorLaunch(context, 'background-tasks', id, params?.name || 'Background task launch', launch) : await launch();
|
|
139
277
|
if (ctx === context && generation === capturedGeneration && adoptedScope === capturedScope)
|
|
140
278
|
rememberOwnership(result.details?.backgroundOwnership, context);
|
|
141
279
|
await host.reconcileNow().catch(() => undefined);
|
|
@@ -146,10 +284,11 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
146
284
|
} });
|
|
147
285
|
registerObservedWaitClient(proxy, host);
|
|
148
286
|
const ui = {
|
|
149
|
-
|
|
287
|
+
onOpen: activate,
|
|
288
|
+
rows: () => legacyRows,
|
|
150
289
|
prepareDetail(id, options) {
|
|
151
290
|
const lines = options?.logTailLines ?? 40, key = `${id}:${lines}`;
|
|
152
|
-
const durable =
|
|
291
|
+
const durable = Boolean(cachedTask(id)), cache = durable ? logs : details;
|
|
153
292
|
const version = generation;
|
|
154
293
|
if (cache.has(key))
|
|
155
294
|
return;
|
|
@@ -161,7 +300,8 @@ export function createManagedBackgroundFrontend(pi) {
|
|
|
161
300
|
cache.delete(key); });
|
|
162
301
|
},
|
|
163
302
|
detail(id, now, options) { return details.get(`${id}:${options?.logTailLines ?? 40}`) ?? null; },
|
|
164
|
-
close(id) {
|
|
303
|
+
close(id) { if (cachedTask(id)?.readOnly)
|
|
304
|
+
return { action: 'not-closable', providerId: 'background-tasks', id }; void request('__background_action', { id, action: rows.find(row => row.id === id)?.active || legacyRows.find(row => row.id === id)?.status === 'running' ? 'stop' : 'dismiss' }).then(() => reconcile()).catch(() => undefined); return { action: 'requested', providerId: 'background-tasks', id }; }
|
|
165
305
|
};
|
|
166
306
|
return { host, proxy, ui };
|
|
167
307
|
}
|
|
@@ -4,17 +4,24 @@ import type { ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-a
|
|
|
4
4
|
import { requestBackgroundTool } from './managed-client.js';
|
|
5
5
|
import { onBackgroundObservation } from './work-observation.js';
|
|
6
6
|
import { getCallbackBatcher } from './shared-callback-batcher.js';
|
|
7
|
-
import { registerObservedWaitClient
|
|
7
|
+
import { registerObservedWaitClient } from "./observed-wait-client.js";
|
|
8
|
+
import type { ObservedWaitHost } from "./observed-waits.js";
|
|
9
|
+
import { observeNavigatorLaunch } from './shared-navigator.ts';
|
|
8
10
|
/** Native transcript delivery and cached UI projections; no coordinator or supervisor. */
|
|
9
11
|
export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
10
12
|
let ctx: ExtensionContext | undefined, generation = 0, refresh: Promise<void> | undefined, dirty=false;
|
|
11
13
|
let rows: any[] = [], legacyRows: any[] = [], details = new Map<string, any>(), logs = new Map<string, any>();
|
|
14
|
+
let pendingAttention = new Set<string>();
|
|
15
|
+
const retainedTasks = new Map<string, any>();
|
|
16
|
+
const cachedTask = (id: string) => rows.find(row => row.id === id) ?? retainedTasks.get(id);
|
|
12
17
|
let adoptedScope: string | undefined;
|
|
18
|
+
let observing = false;
|
|
13
19
|
const scopeOf = (context: ExtensionContext) => JSON.stringify([context.cwd, context.sessionManager.getSessionId()]);
|
|
14
20
|
const ownership = new Map<string, { operationId: string; owner: { kind: string; sessionId: string; workspace: string } }>();
|
|
15
21
|
const adoptContext = (context: ExtensionContext) => {
|
|
16
22
|
if (adoptedScope !== undefined && adoptedScope !== scopeOf(context)) {
|
|
17
|
-
ownership.clear(); rows = []; legacyRows = []; details.clear(); logs.clear(); generation++;
|
|
23
|
+
ownership.clear(); rows = []; legacyRows = []; details.clear(); logs.clear(); retainedTasks.clear(); generation++;
|
|
24
|
+
pendingAttention.clear();
|
|
18
25
|
}
|
|
19
26
|
ctx = context; adoptedScope = scopeOf(context);
|
|
20
27
|
};
|
|
@@ -26,6 +33,16 @@ export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
|
26
33
|
if (ownership.size > 256) ownership.delete(ownership.keys().next().value!);
|
|
27
34
|
};
|
|
28
35
|
const queued = new Set<string>(), listeners = new Set<(change: any) => void>();
|
|
36
|
+
const traceSeen = new Set<string>();
|
|
37
|
+
const trace = (stage: string, facts: Record<string, unknown> = {}) => {
|
|
38
|
+
if (process.env.PI_BG_OBSERVED_WAITS_DEBUG !== '1') return;
|
|
39
|
+
const full = JSON.stringify({ stage, sessionId: ctx?.sessionManager.getSessionId(), ...facts });
|
|
40
|
+
const line = full.length <= 4096 ? full : JSON.stringify({ stage, truncated: true });
|
|
41
|
+
if (traceSeen.has(line) || traceSeen.size >= 128) return;
|
|
42
|
+
const output = traceSeen.size === 127 ? JSON.stringify({ stage: 'capture-limit', limit: 128 }) : line;
|
|
43
|
+
traceSeen.add(line);
|
|
44
|
+
try { console.error('[background-handoff-frontend]', output); } catch { /* diagnostics never own delivery */ }
|
|
45
|
+
};
|
|
29
46
|
const active = (origin: any, branch?: string | null) => Boolean(ctx && origin.cwd === ctx.cwd && origin.sessionId === ctx.sessionManager.getSessionId()
|
|
30
47
|
&& (!branch || ctx.sessionManager.getBranch().some(entry => entry.id === branch)));
|
|
31
48
|
const request = (tool: string, params: any) => {
|
|
@@ -35,6 +52,30 @@ export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
|
35
52
|
};
|
|
36
53
|
const notify = () => { for (const listener of listeners)
|
|
37
54
|
listener({ workspace: ctx?.cwd, sessionId: ctx?.sessionManager.getSessionId() }); };
|
|
55
|
+
let handoffRetry: ReturnType<typeof setTimeout> | undefined;
|
|
56
|
+
let attentionRetry: ReturnType<typeof setTimeout> | undefined;
|
|
57
|
+
let retryAt = Infinity;
|
|
58
|
+
const scheduleHandoffPoll = (delayMs: number) => {
|
|
59
|
+
if (!ctx || retryAt <= Date.now() + delayMs) return;
|
|
60
|
+
clearTimeout(handoffRetry);
|
|
61
|
+
retryAt = Date.now() + delayMs;
|
|
62
|
+
// Poll again before attempting delivery: the task or its recipient may have changed.
|
|
63
|
+
handoffRetry = setTimeout(() => {
|
|
64
|
+
handoffRetry = undefined; retryAt = Infinity;
|
|
65
|
+
if (ctx) void reconcile().catch(() => undefined);
|
|
66
|
+
}, delayMs);
|
|
67
|
+
handoffRetry.unref();
|
|
68
|
+
};
|
|
69
|
+
const waitForAttentionIdle = () => {
|
|
70
|
+
if (attentionRetry) return;
|
|
71
|
+
attentionRetry = setTimeout(() => {
|
|
72
|
+
attentionRetry = undefined;
|
|
73
|
+
if (!ctx || !pendingAttention.size) return;
|
|
74
|
+
if (!ctx.isIdle()) { waitForAttentionIdle(); return; }
|
|
75
|
+
void reconcile().catch(() => undefined);
|
|
76
|
+
}, 1000);
|
|
77
|
+
attentionRetry.unref();
|
|
78
|
+
};
|
|
38
79
|
const reconcile = () => {
|
|
39
80
|
if (!ctx)
|
|
40
81
|
return Promise.resolve();
|
|
@@ -42,19 +83,47 @@ export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
|
42
83
|
dirty=false;
|
|
43
84
|
const captured = ctx, version = generation;
|
|
44
85
|
refresh = (async () => {
|
|
45
|
-
const
|
|
86
|
+
const rechecking = pendingAttention.size && captured.isIdle() ? pendingAttention : undefined;
|
|
87
|
+
trace('poll-start');
|
|
88
|
+
const snapshot = await request('__background_poll', { knownActiveIds: rows.filter(row => row.active).map(row => row.id).slice(0, 200) });
|
|
89
|
+
trace('poll-received', { stale: version !== generation, total: snapshot.deliveries?.length ?? 0, deliveries: (snapshot.deliveries ?? []).slice(0, 8).map((item: any) => ({ taskId: item.notification.taskId, receiptId: item.receiptId })) });
|
|
46
90
|
if (version !== generation)
|
|
47
91
|
return;
|
|
92
|
+
if (Number.isFinite(snapshot.retryAfterMs)) scheduleHandoffPoll(snapshot.retryAfterMs);
|
|
48
93
|
if (snapshot.recoveryPending === true) dirty = true;
|
|
49
|
-
|
|
50
|
-
|
|
94
|
+
// A rebuilding snapshot is not evidence that visible tasks vanished.
|
|
95
|
+
if (snapshot.status !== 'rebuilding' && snapshot.status !== 'degraded') {
|
|
96
|
+
// Older servers include compact callback pointers in the task list.
|
|
97
|
+
rows = (snapshot.tasks ?? []).filter((task: any) => !task.callbackPending);
|
|
98
|
+
// Detail remains inspectable while Navigator retains a terminal row.
|
|
99
|
+
for (const task of rows) {
|
|
100
|
+
retainedTasks.delete(task.id); retainedTasks.set(task.id, task);
|
|
101
|
+
if (retainedTasks.size > 256) retainedTasks.delete(retainedTasks.keys().next().value!);
|
|
102
|
+
}
|
|
103
|
+
legacyRows = snapshot.legacyRows ?? [];
|
|
104
|
+
}
|
|
51
105
|
notify();
|
|
106
|
+
const deferredAttention = new Set<string>();
|
|
52
107
|
for (const item of snapshot.deliveries ?? []) {
|
|
53
108
|
if (queued.has(item.token))
|
|
54
109
|
continue;
|
|
110
|
+
const n = item.notification;
|
|
111
|
+
const type = n.event?.customType;
|
|
112
|
+
if ((type === 'background-task-stall' || type === 'background-wait-review')
|
|
113
|
+
&& (!rechecking?.has(item.receiptId) || !captured.isIdle())) {
|
|
114
|
+
// Keep the candidate here while busy. Pi's followUp queue cannot
|
|
115
|
+
// revalidate it when the current turn eventually ends.
|
|
116
|
+
deferredAttention.add(item.receiptId);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
55
119
|
queued.add(item.token);
|
|
56
|
-
const
|
|
57
|
-
|
|
120
|
+
const facts = { taskId: item.notification.taskId, receiptId: item.receiptId };
|
|
121
|
+
const receipt = (delivered: boolean, reason?: string) => {
|
|
122
|
+
trace('handoff', { ...facts, delivered, reason: reason?.slice(0, 256) });
|
|
123
|
+
void requestBackgroundTool(pi, captured, randomUUID(), '__background_receipt', { token: item.token, delivered, reason })
|
|
124
|
+
.then(() => trace('receipt-return', facts), error => trace('receipt-error', { ...facts, error: String(error).slice(0, 256) }))
|
|
125
|
+
.finally(() => {queued.delete(item.token);if(ctx===captured)void reconcile().catch(()=>undefined);});
|
|
126
|
+
};
|
|
58
127
|
const proof = captured.sessionManager.getBranch().some((entry: any) => {
|
|
59
128
|
const message = entry.type === 'custom_message' ? entry : entry.message;
|
|
60
129
|
return item.notification.urgent ? message?.customType===item.notification.event.customType&&message.details?.urgentEventId===item.receiptId : message?.customType === 'background-completion-batch' && message.details?.completionEventIds?.includes(item.receiptId);
|
|
@@ -63,26 +132,69 @@ export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
|
63
132
|
receipt(true);
|
|
64
133
|
continue;
|
|
65
134
|
}
|
|
66
|
-
|
|
67
|
-
if(n.urgent)
|
|
135
|
+
trace('queue', { ...facts, urgent: n.urgent === true, recipientActive: active(n.origin, n.branchId) });
|
|
136
|
+
if (n.urgent) {
|
|
137
|
+
const handoff = getCallbackBatcher(pi).deliverUrgent({ ...n.event, receiptId: item.receiptId,
|
|
138
|
+
getSuppressionReason: () => active(n.origin, n.branchId) ? undefined : 'The originating session or branch is no longer active',
|
|
139
|
+
onDelivered: () => receipt(true), onSuppressed: reason => receipt(false, reason) });
|
|
140
|
+
const retry = () => { queued.delete(item.token); if (version === generation) scheduleHandoffPoll(1000); };
|
|
141
|
+
void Promise.resolve(handoff).then(ok => { if (!ok) retry(); }, retry);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
68
144
|
getCallbackBatcher(pi).enqueue({ ...n.event, source: 'background-task', id: n.taskId, label: n.taskId, status: n.waitId ? `${n.status}; wait ${n.waitId} ready` : n.status,
|
|
69
145
|
detailTool: 'bg_task_status', outcome: n.outcome, callback: true, receiptId: item.receiptId,
|
|
70
146
|
getSuppressionReason: () => active(n.origin, n.branchId) ? undefined : 'The originating session or branch is no longer active',
|
|
71
147
|
onDelivered: () => receipt(true), onSuppressed: (reason) => receipt(false, reason) });
|
|
72
148
|
}
|
|
73
|
-
|
|
149
|
+
pendingAttention = deferredAttention;
|
|
150
|
+
if (pendingAttention.size) {
|
|
151
|
+
if (captured.isIdle()) dirty = true;
|
|
152
|
+
else waitForAttentionIdle();
|
|
153
|
+
}
|
|
154
|
+
})().catch(error => {
|
|
155
|
+
trace('poll-error', { error: String(error).slice(0, 256) });
|
|
156
|
+
if (version === generation && pendingAttention.size) waitForAttentionIdle();
|
|
157
|
+
throw error;
|
|
158
|
+
})
|
|
159
|
+
.finally(() => { refresh = undefined;if(dirty&&ctx)void reconcile().catch(()=>undefined); });
|
|
74
160
|
return refresh;
|
|
75
161
|
};
|
|
162
|
+
const activate = () => {
|
|
163
|
+
if (observing || !ctx) return;
|
|
164
|
+
observing = true;
|
|
165
|
+
const version = generation;
|
|
166
|
+
void reconcile().catch(() => { if (generation === version) observing = false; });
|
|
167
|
+
};
|
|
76
168
|
const host = {
|
|
77
169
|
async reconcileNow() { await reconcile(); while (refresh) await refresh; },
|
|
78
|
-
start(context: ExtensionContext
|
|
170
|
+
start(context: ExtensionContext, restore = true) {
|
|
171
|
+
adoptContext(context); generation++; observing = false;
|
|
172
|
+
if (restore) activate();
|
|
173
|
+
},
|
|
79
174
|
noteParentContext(context: ExtensionContext) { adoptContext(context); generation++; void reconcile().catch(() => undefined); },
|
|
80
|
-
close() {
|
|
175
|
+
close() {
|
|
176
|
+
clearTimeout(handoffRetry);
|
|
177
|
+
clearTimeout(attentionRetry);
|
|
178
|
+
handoffRetry = attentionRetry = undefined;
|
|
179
|
+
retryAt = Infinity;
|
|
180
|
+
observing = false;
|
|
181
|
+
ctx = undefined;
|
|
182
|
+
adoptedScope = undefined;
|
|
183
|
+
generation++;
|
|
184
|
+
rows = [];
|
|
185
|
+
legacyRows = [];
|
|
186
|
+
details.clear();
|
|
187
|
+
logs.clear();
|
|
188
|
+
retainedTasks.clear();
|
|
189
|
+
queued.clear();
|
|
190
|
+
ownership.clear();
|
|
191
|
+
pendingAttention.clear();
|
|
192
|
+
},
|
|
81
193
|
isParentOriginActive: active,
|
|
82
194
|
onDurableTaskChange(listener: (change: any) => void) { listeners.add(listener); return () => listeners.delete(listener); },
|
|
83
|
-
listDurableTasks(origin: any,
|
|
84
|
-
readDurableTask(origin: any, id: string) { return active(origin) ?
|
|
85
|
-
ownsDurableTask(id: string) { if (!ctx || adoptedScope !== scopeOf(ctx)) return false; return ownership.has(id) || rows.some(row => row.id === id); },
|
|
195
|
+
listDurableTasks(origin: any, _options: any = {}) { return active(origin) ? rows : []; },
|
|
196
|
+
readDurableTask(origin: any, id: string) { const task = cachedTask(id); return active(origin, task?.parentRecipient?.branchId) ? task : undefined; },
|
|
197
|
+
ownsDurableTask(id: string) { if (!ctx || adoptedScope !== scopeOf(ctx)) return false; return ownership.has(id) || rows.some(row => row.id === id && !row.readOnly); },
|
|
86
198
|
readDurableTaskLog(origin: any, id: string, lines = 40) {
|
|
87
199
|
if (!active(origin))
|
|
88
200
|
return;
|
|
@@ -104,7 +216,10 @@ export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
|
104
216
|
throw new Error('Native Background context unavailable');
|
|
105
217
|
adoptContext(context);
|
|
106
218
|
const capturedGeneration = generation, capturedScope = adoptedScope;
|
|
107
|
-
const
|
|
219
|
+
const launch = () => requestBackgroundTool(pi, context, id, definition.name, params, definition.parameters, signal);
|
|
220
|
+
const spawning = ['bg_task_spawn', 'bg_task_spawn_batch', 'bg_task_watch'].includes(definition.name)
|
|
221
|
+
|| definition.name === 'bg_task' && ['spawn', 'watch'].includes(params?.action);
|
|
222
|
+
const result = spawning ? await observeNavigatorLaunch(context, 'background-tasks', id, params?.name || 'Background task launch', launch) : await launch();
|
|
108
223
|
if (ctx === context && generation === capturedGeneration && adoptedScope === capturedScope) rememberOwnership(result.details?.backgroundOwnership, context);
|
|
109
224
|
await host.reconcileNow().catch(() => undefined);
|
|
110
225
|
return result;
|
|
@@ -114,10 +229,11 @@ export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
|
114
229
|
} });
|
|
115
230
|
registerObservedWaitClient(proxy, host as unknown as ObservedWaitHost);
|
|
116
231
|
const ui: ManagedBackgroundNavigator = {
|
|
117
|
-
|
|
232
|
+
onOpen: activate,
|
|
233
|
+
rows: () => legacyRows,
|
|
118
234
|
prepareDetail(id, options) {
|
|
119
235
|
const lines = options?.logTailLines ?? 40, key = `${id}:${lines}`;
|
|
120
|
-
const durable =
|
|
236
|
+
const durable = Boolean(cachedTask(id)), cache = durable ? logs : details;
|
|
121
237
|
const version=generation;
|
|
122
238
|
if (cache.has(key))
|
|
123
239
|
return;
|
|
@@ -125,7 +241,7 @@ export function createManagedBackgroundFrontend(pi: ExtensionAPI) {
|
|
|
125
241
|
void request(durable ? '__background_log' : '__background_detail', { id, lines }).then(value => { if(version===generation){cache.set(key, value); notify();} }, () => {if(version===generation)cache.delete(key);});
|
|
126
242
|
},
|
|
127
243
|
detail(id, now, options) { return details.get(`${id}:${options?.logTailLines ?? 40}`) ?? null; },
|
|
128
|
-
close(id) { void request('__background_action', { id, action: rows.find(row => row.id === id)?.active || legacyRows.find(row => row.id === id)?.status === 'running' ? 'stop' : 'dismiss' }).then(() => reconcile()).catch(() => undefined); return { action: 'requested', providerId: 'background-tasks', id }; }
|
|
244
|
+
close(id) { if (cachedTask(id)?.readOnly) return {action: 'not-closable', providerId: 'background-tasks', id}; void request('__background_action', { id, action: rows.find(row => row.id === id)?.active || legacyRows.find(row => row.id === id)?.status === 'running' ? 'stop' : 'dismiss' }).then(() => reconcile()).catch(() => undefined); return { action: 'requested', providerId: 'background-tasks', id }; }
|
|
129
245
|
};
|
|
130
246
|
return { host, proxy, ui };
|
|
131
247
|
}
|
|
@@ -6,13 +6,13 @@ import { actionableFailures, failureLabel, readFailureState } from "./shared-fai
|
|
|
6
6
|
import { failurePath, failureView } from "./failures.domain.mjs";
|
|
7
7
|
import { readLog } from "./logs.domain.mjs";
|
|
8
8
|
import { listMetasForOrigin, onMetaChanged, readMeta, writeMeta } from "./registry.domain.mjs";
|
|
9
|
-
import { getObservedWaitHost } from "./observed-
|
|
10
|
-
import {
|
|
9
|
+
import { getObservedWaitHost } from "./observed-wait-client.domain.mjs";
|
|
10
|
+
import { describeSupervisedProgress } from "./supervised-progress.domain.mjs";
|
|
11
11
|
import { formatBackgroundTaskStall, observeBackgroundTaskStall } from "./stall.domain.mjs";
|
|
12
12
|
const TERMINAL_NAVIGATOR_RETENTION_MS = 30_000;
|
|
13
13
|
const DURABLE_ROW_CACHE_MS = 1_000;
|
|
14
14
|
let durableRowCache;
|
|
15
|
-
export function createBackgroundTasksNavigator(runtime
|
|
15
|
+
export function createBackgroundTasksNavigator(runtime, managed) {
|
|
16
16
|
const owner = Symbol("background-tasks navigator factory");
|
|
17
17
|
let unregister;
|
|
18
18
|
let navigatorContext;
|
|
@@ -74,6 +74,7 @@ export function createBackgroundTasksNavigator(runtime = { stopTask }, managed)
|
|
|
74
74
|
id: "background-tasks",
|
|
75
75
|
label: "Background Tasks",
|
|
76
76
|
priority: 20,
|
|
77
|
+
onOpen: managed?.onOpen,
|
|
77
78
|
visibleCount: () => {
|
|
78
79
|
const now = Date.now();
|
|
79
80
|
return (managed ? managed.rows().filter(row => row.status === "running").length : visibleMetas(now).filter((meta) => meta.status === "running").length)
|
|
@@ -92,9 +93,13 @@ export function createBackgroundTasksNavigator(runtime = { stopTask }, managed)
|
|
|
92
93
|
},
|
|
93
94
|
armCloseLabel: (row) => row.status === "running" || row.status === "starting" ? "x again to stop" : "x again to dismiss",
|
|
94
95
|
close: (id) => {
|
|
96
|
+
if (durableTask(id)?.readOnly)
|
|
97
|
+
return { action: "not-closable", providerId: "background-tasks", id };
|
|
95
98
|
if (managed)
|
|
96
99
|
return managed.close(id);
|
|
97
100
|
const durable = durableTask(id);
|
|
101
|
+
if (durable?.readOnly)
|
|
102
|
+
return { action: "not-closable", providerId: "background-tasks", id };
|
|
98
103
|
if (durable) {
|
|
99
104
|
const host = navigatorHost();
|
|
100
105
|
const origin = activeNavigatorOrigin;
|
|
@@ -113,7 +118,11 @@ export function createBackgroundTasksNavigator(runtime = { stopTask }, managed)
|
|
|
113
118
|
if (!meta)
|
|
114
119
|
return { action: "missing", providerId: "background-tasks", id };
|
|
115
120
|
if (meta.status === "running") {
|
|
116
|
-
|
|
121
|
+
const pi = piRef;
|
|
122
|
+
if (runtime)
|
|
123
|
+
void runtime.stopTask(pi, id);
|
|
124
|
+
else
|
|
125
|
+
void import("./runtime.domain.mjs").then(({ stopTask }) => stopTask(pi, id));
|
|
117
126
|
return { action: "stopped", providerId: "background-tasks", id, status: "stopping" };
|
|
118
127
|
}
|
|
119
128
|
meta.dismissedAt = Date.now();
|
|
@@ -167,6 +176,9 @@ export function createBackgroundTasksNavigator(runtime = { stopTask }, managed)
|
|
|
167
176
|
const command = durableCommandLabel(task);
|
|
168
177
|
const elapsed = formatDuration((task.endedAt ?? now) - task.startedAt);
|
|
169
178
|
const facts = [];
|
|
179
|
+
const progress = task.progress && describeSupervisedProgress(task.progress, now);
|
|
180
|
+
if (progress)
|
|
181
|
+
facts.push(progress);
|
|
170
182
|
if (task.attention && task.reason)
|
|
171
183
|
facts.push(task.reason);
|
|
172
184
|
else if (task.attention)
|
|
@@ -180,6 +192,8 @@ export function createBackgroundTasksNavigator(runtime = { stopTask }, managed)
|
|
|
180
192
|
return {
|
|
181
193
|
providerId: "background-tasks",
|
|
182
194
|
id: task.id,
|
|
195
|
+
parentAgentId: task.parentAgentId,
|
|
196
|
+
readOnly: task.readOnly,
|
|
183
197
|
name: task.name,
|
|
184
198
|
status: task.status,
|
|
185
199
|
statusTone: toneForDurableStatus(task.status),
|
|
@@ -204,7 +218,9 @@ export function createBackgroundTasksNavigator(runtime = { stopTask }, managed)
|
|
|
204
218
|
const metadata = [
|
|
205
219
|
{ label: "provider", value: "Background Tasks (durable)" },
|
|
206
220
|
{ label: "kind", value: task.kind },
|
|
221
|
+
...(task.parentAgentId ? [{ label: "agent", value: task.parentAgentId }] : []),
|
|
207
222
|
{ label: "operation", value: task.operationId },
|
|
223
|
+
...(task.progress ? [{ label: "last progress", value: new Date(task.progress.observedAt).toISOString() }] : []),
|
|
208
224
|
{ label: "status", value: `${task.status}${task.terminalOutcome ? ` (${task.terminalOutcome})` : ""}` },
|
|
209
225
|
{ label: "started", value: new Date(task.startedAt).toISOString() },
|
|
210
226
|
...(task.endedAt !== undefined ? [{ label: "ended", value: new Date(task.endedAt).toISOString() }] : []),
|
|
@@ -242,7 +258,7 @@ export function createBackgroundTasksNavigator(runtime = { stopTask }, managed)
|
|
|
242
258
|
label: log?.truncated ? "log tail" : "log",
|
|
243
259
|
text: evidenceText,
|
|
244
260
|
},
|
|
245
|
-
footerActions: [task.active ? "x stop" : "x dismiss"],
|
|
261
|
+
footerActions: task.readOnly ? ["Managed by owning agent"] : [task.active ? "x stop" : "x dismiss"],
|
|
246
262
|
};
|
|
247
263
|
}
|
|
248
264
|
function durableCommandLabel(task) {
|