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
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Generated fixed launch domain from packages/pi-better-background-tasks/src/watch-fingerprint.ts. Do not edit directly.
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
/** Version tag of the watch outcome fingerprint. A value from another version is adopted, never compared. */
|
|
4
|
+
export const WATCH_FINGERPRINT_VERSION = "v1";
|
|
5
|
+
/** OSC and CSI control sequences are display markup, not content. */
|
|
6
|
+
const TERMINAL_ESCAPE = /\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)|\u001b\[[0-?]*[ -/]*[@-~]/g;
|
|
7
|
+
/**
|
|
8
|
+
* Text as the terminal would settle it. Redraw frames and control sequences are
|
|
9
|
+
* display churn: repainting the same visible line is not a new observation.
|
|
10
|
+
*/
|
|
11
|
+
function settledTerminalText(text) {
|
|
12
|
+
return text
|
|
13
|
+
.replace(TERMINAL_ESCAPE, "")
|
|
14
|
+
.split("\n")
|
|
15
|
+
.map((line) => {
|
|
16
|
+
// A CR immediately before LF ends the line; any other CR overwrites it in place.
|
|
17
|
+
const body = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
18
|
+
return (body.split("\r").at(-1) ?? "").replace(/[ \t]+$/, "");
|
|
19
|
+
})
|
|
20
|
+
.join("\n")
|
|
21
|
+
.replace(/\n+$/, "");
|
|
22
|
+
}
|
|
23
|
+
/** Objects compare by content regardless of key order, so reordered keys are not progress. */
|
|
24
|
+
function canonicalJsonText(value) {
|
|
25
|
+
if (Array.isArray(value))
|
|
26
|
+
return `[${value.map(canonicalJsonText).join(",")}]`;
|
|
27
|
+
if (value && typeof value === "object") {
|
|
28
|
+
const record = value;
|
|
29
|
+
const entries = Object.keys(record).sort()
|
|
30
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalJsonText(record[key])}`);
|
|
31
|
+
return `{${entries.join(",")}}`;
|
|
32
|
+
}
|
|
33
|
+
return JSON.stringify(value) ?? "null";
|
|
34
|
+
}
|
|
35
|
+
/** stdout compares as JSON content when it parses, and as settled text otherwise. */
|
|
36
|
+
function canonicalWatchStdout(stdout) {
|
|
37
|
+
const settled = settledTerminalText(stdout);
|
|
38
|
+
try {
|
|
39
|
+
return `json:${canonicalJsonText(JSON.parse(settled))}`;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return `text:${settled}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What a poll observed, independent of when it ran. Exit status, signal, error,
|
|
47
|
+
* stdout content, and stderr content are included; poll metadata (start time,
|
|
48
|
+
* end time, duration, check count) is deliberately not, so it can never hide a
|
|
49
|
+
* watch that reports the same outcome forever. Watch stdout may carry its own
|
|
50
|
+
* timestamps: projecting meaningful fields is the caller's job, not this
|
|
51
|
+
* fingerprint's guess.
|
|
52
|
+
*/
|
|
53
|
+
export function watchOutcomeFingerprint(result) {
|
|
54
|
+
// A runner may report why the check itself could not run; that is part of the
|
|
55
|
+
// observed outcome, not poll metadata.
|
|
56
|
+
const reported = result.error;
|
|
57
|
+
const error = typeof reported === "string" ? settledTerminalText(reported) : null;
|
|
58
|
+
const payload = JSON.stringify([
|
|
59
|
+
result.exitCode,
|
|
60
|
+
result.signal ?? null,
|
|
61
|
+
result.timedOut === true,
|
|
62
|
+
error,
|
|
63
|
+
canonicalWatchStdout(result.stdout),
|
|
64
|
+
settledTerminalText(result.stderr),
|
|
65
|
+
]);
|
|
66
|
+
return `${WATCH_FINGERPRINT_VERSION}:${createHash("sha256").update(payload).digest("hex")}`;
|
|
67
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { CommandResult } from "./types.js";
|
|
3
|
+
|
|
4
|
+
/** Version tag of the watch outcome fingerprint. A value from another version is adopted, never compared. */
|
|
5
|
+
export const WATCH_FINGERPRINT_VERSION = "v1";
|
|
6
|
+
/** OSC and CSI control sequences are display markup, not content. */
|
|
7
|
+
const TERMINAL_ESCAPE = /\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)|\u001b\[[0-?]*[ -/]*[@-~]/g;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Text as the terminal would settle it. Redraw frames and control sequences are
|
|
11
|
+
* display churn: repainting the same visible line is not a new observation.
|
|
12
|
+
*/
|
|
13
|
+
function settledTerminalText(text: string): string {
|
|
14
|
+
return text
|
|
15
|
+
.replace(TERMINAL_ESCAPE, "")
|
|
16
|
+
.split("\n")
|
|
17
|
+
.map((line) => {
|
|
18
|
+
// A CR immediately before LF ends the line; any other CR overwrites it in place.
|
|
19
|
+
const body = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
20
|
+
return (body.split("\r").at(-1) ?? "").replace(/[ \t]+$/, "");
|
|
21
|
+
})
|
|
22
|
+
.join("\n")
|
|
23
|
+
.replace(/\n+$/, "");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Objects compare by content regardless of key order, so reordered keys are not progress. */
|
|
27
|
+
function canonicalJsonText(value: unknown): string {
|
|
28
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJsonText).join(",")}]`;
|
|
29
|
+
if (value && typeof value === "object") {
|
|
30
|
+
const record = value as Record<string, unknown>;
|
|
31
|
+
const entries = Object.keys(record).sort()
|
|
32
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalJsonText(record[key])}`);
|
|
33
|
+
return `{${entries.join(",")}}`;
|
|
34
|
+
}
|
|
35
|
+
return JSON.stringify(value) ?? "null";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** stdout compares as JSON content when it parses, and as settled text otherwise. */
|
|
39
|
+
function canonicalWatchStdout(stdout: string): string {
|
|
40
|
+
const settled = settledTerminalText(stdout);
|
|
41
|
+
try {
|
|
42
|
+
return `json:${canonicalJsonText(JSON.parse(settled))}`;
|
|
43
|
+
} catch {
|
|
44
|
+
return `text:${settled}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* What a poll observed, independent of when it ran. Exit status, signal, error,
|
|
50
|
+
* stdout content, and stderr content are included; poll metadata (start time,
|
|
51
|
+
* end time, duration, check count) is deliberately not, so it can never hide a
|
|
52
|
+
* watch that reports the same outcome forever. Watch stdout may carry its own
|
|
53
|
+
* timestamps: projecting meaningful fields is the caller's job, not this
|
|
54
|
+
* fingerprint's guess.
|
|
55
|
+
*/
|
|
56
|
+
export function watchOutcomeFingerprint(result: CommandResult): string {
|
|
57
|
+
// A runner may report why the check itself could not run; that is part of the
|
|
58
|
+
// observed outcome, not poll metadata.
|
|
59
|
+
const reported = (result as CommandResult & { error?: unknown }).error;
|
|
60
|
+
const error = typeof reported === "string" ? settledTerminalText(reported) : null;
|
|
61
|
+
const payload = JSON.stringify([
|
|
62
|
+
result.exitCode,
|
|
63
|
+
result.signal ?? null,
|
|
64
|
+
result.timedOut === true,
|
|
65
|
+
error,
|
|
66
|
+
canonicalWatchStdout(result.stdout),
|
|
67
|
+
settledTerminalText(result.stderr),
|
|
68
|
+
]);
|
|
69
|
+
return `${WATCH_FINGERPRINT_VERSION}:${createHash("sha256").update(payload).digest("hex")}`;
|
|
70
|
+
}
|
|
@@ -3,10 +3,29 @@ import { mkdirSync, readFileSync, realpathSync } from "node:fs";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { setTimeout as delay } from "node:timers/promises";
|
|
6
|
+
import { cursorKind } from "./shared-log-utils.domain.mjs";
|
|
6
7
|
const hostedClients = new WeakSet();
|
|
8
|
+
const connectionAborts = new WeakMap();
|
|
7
9
|
const clients = new WeakMap();
|
|
8
10
|
const generations = new WeakMap();
|
|
9
11
|
const listeners = new WeakMap();
|
|
12
|
+
const subscribedClients = new WeakMap();
|
|
13
|
+
const activityScopes = new WeakMap();
|
|
14
|
+
/** Goal reads the subscribed observation, never a periodic full-state RPC. */
|
|
15
|
+
export async function backgroundGoalObservation(pi, ctx, cachedOnly = false) {
|
|
16
|
+
const scope = { background: true, cwd: ctx.cwd, sessionId: ctx.sessionManager?.getSessionId(), processId: process.pid };
|
|
17
|
+
const key = JSON.stringify(scope);
|
|
18
|
+
if (cachedOnly) {
|
|
19
|
+
const client = subscribedClients.get(pi);
|
|
20
|
+
return client && !client.closed && activityScopes.get(client) === key ? client.snapshot : undefined;
|
|
21
|
+
}
|
|
22
|
+
const client = await observationClient(pi, scope);
|
|
23
|
+
if (activityScopes.get(client) !== key) {
|
|
24
|
+
await client.subscribe(scope);
|
|
25
|
+
activityScopes.set(client, key);
|
|
26
|
+
}
|
|
27
|
+
return client.snapshot;
|
|
28
|
+
}
|
|
10
29
|
export function onBackgroundObservation(pi, notify) {
|
|
11
30
|
let set = listeners.get(pi);
|
|
12
31
|
if (!set) {
|
|
@@ -16,8 +35,12 @@ export function onBackgroundObservation(pi, notify) {
|
|
|
16
35
|
set.add(notify);
|
|
17
36
|
return () => { set.delete(notify); };
|
|
18
37
|
}
|
|
38
|
+
export function backgroundListNeedsHistory(params) {
|
|
39
|
+
return params.all === true || params.limit !== undefined || params.status !== undefined
|
|
40
|
+
|| typeof params.cursor === "string" && cursorKind(params.cursor) !== "s";
|
|
41
|
+
}
|
|
19
42
|
/** A query connection grants no supervisor, transcript writer or retention authority. */
|
|
20
|
-
export function backgroundObservation(pi, ctx, history = false, all = false, page) {
|
|
43
|
+
export function backgroundObservation(pi, ctx, history = false, all = false, page, wait) {
|
|
21
44
|
const scope = { background: true, cwd: ctx.cwd, sessionId: ctx.sessionManager?.getSessionId(), processId: process.pid };
|
|
22
45
|
const generation = generations.get(pi) ?? 0;
|
|
23
46
|
const read = async (retry) => {
|
|
@@ -28,13 +51,14 @@ export function backgroundObservation(pi, ctx, history = false, all = false, pag
|
|
|
28
51
|
if ((generations.get(pi) ?? 0) !== generation)
|
|
29
52
|
throw new Error("runtime-unavailable: client closed");
|
|
30
53
|
try {
|
|
31
|
-
const deadline = Date.now() + 20000;
|
|
54
|
+
const deadline = Date.now() + (wait?.timeoutMs ?? 20000);
|
|
32
55
|
for (;;) {
|
|
56
|
+
wait?.signal?.throwIfAborted();
|
|
33
57
|
checkGeneration();
|
|
34
58
|
const snapshot = await client.get({ ...scope, backgroundHistory: history, backgroundAll: all, ...page });
|
|
35
59
|
if (!history || snapshot.status !== "rebuilding" || Date.now() >= deadline)
|
|
36
60
|
return snapshot;
|
|
37
|
-
await delay(20);
|
|
61
|
+
await delay(wait ? 100 : 20, undefined, { signal: wait?.signal });
|
|
38
62
|
}
|
|
39
63
|
}
|
|
40
64
|
catch (error) {
|
|
@@ -57,6 +81,8 @@ function observationClient(pi, scope) {
|
|
|
57
81
|
};
|
|
58
82
|
let pending = clients.get(pi);
|
|
59
83
|
if (!pending) {
|
|
84
|
+
const controller = new AbortController();
|
|
85
|
+
connectionAborts.set(pi, controller);
|
|
60
86
|
pending = (async () => {
|
|
61
87
|
let hosted;
|
|
62
88
|
let rejected;
|
|
@@ -71,7 +97,9 @@ function observationClient(pi, scope) {
|
|
|
71
97
|
notify(); });
|
|
72
98
|
try {
|
|
73
99
|
await hosted.subscribe(scope);
|
|
100
|
+
activityScopes.set(hosted, JSON.stringify({ ...scope, processId: process.pid }));
|
|
74
101
|
checkOpen();
|
|
102
|
+
subscribedClients.set(pi, hosted);
|
|
75
103
|
return hosted;
|
|
76
104
|
}
|
|
77
105
|
catch (error) {
|
|
@@ -82,12 +110,10 @@ function observationClient(pi, scope) {
|
|
|
82
110
|
const requestedRoot = process.env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent");
|
|
83
111
|
mkdirSync(requestedRoot, { recursive: true, mode: 0o700 });
|
|
84
112
|
const root = realpathSync(requestedRoot);
|
|
85
|
-
const ensure = async () => {
|
|
86
|
-
checkOpen();
|
|
113
|
+
const ensure = async (timeoutMs, signal) => {
|
|
87
114
|
const installer = new URL("../runtime-client/src/runtime/client.mjs", import.meta.url).href;
|
|
88
115
|
const { ensureRuntime } = await import(installer);
|
|
89
|
-
|
|
90
|
-
await ensureRuntime({ agentDir: root, serverDir: process.env.PI_SERVER_DIR });
|
|
116
|
+
await ensureRuntime({ agentDir: root, serverDir: process.env.PI_SERVER_DIR, timeoutMs, signal });
|
|
91
117
|
};
|
|
92
118
|
let registration;
|
|
93
119
|
try {
|
|
@@ -96,16 +122,18 @@ function observationClient(pi, scope) {
|
|
|
96
122
|
catch (error) {
|
|
97
123
|
if (error.code !== "ENOENT")
|
|
98
124
|
throw error;
|
|
99
|
-
await ensure();
|
|
125
|
+
await ensure(undefined, controller.signal);
|
|
100
126
|
registration = JSON.parse(readFileSync(join(root, "server-registration-v1.json"), "utf8"));
|
|
101
127
|
}
|
|
102
128
|
if (registration.version !== 1 || registration.agentDir !== root)
|
|
103
129
|
throw new Error("Invalid canonical runtime registration");
|
|
104
130
|
const module = new URL("./shared-work-client.mjs", import.meta.url).href;
|
|
105
|
-
const {
|
|
131
|
+
const { connectWorkClient } = await import(module);
|
|
106
132
|
const connect = async () => {
|
|
107
133
|
checkOpen();
|
|
108
|
-
const client =
|
|
134
|
+
const client = await connectWorkClient(join(registration.serverDir, "pi-server.sock"), root, ["runtime-background-state-v1"], {
|
|
135
|
+
signal: controller.signal, recover: ({ timeoutMs, signal }) => ensure(timeoutMs, signal),
|
|
136
|
+
});
|
|
109
137
|
let subscribed = false;
|
|
110
138
|
client.onChange(() => {
|
|
111
139
|
if (!subscribed || clients.get(pi) !== pending)
|
|
@@ -116,25 +144,20 @@ function observationClient(pi, scope) {
|
|
|
116
144
|
notify();
|
|
117
145
|
});
|
|
118
146
|
try {
|
|
147
|
+
checkOpen();
|
|
119
148
|
await client.subscribe(scope);
|
|
149
|
+
checkOpen();
|
|
120
150
|
}
|
|
121
151
|
catch (error) {
|
|
122
152
|
client.close();
|
|
123
153
|
throw error;
|
|
124
154
|
}
|
|
155
|
+
subscribedClients.set(pi, client);
|
|
125
156
|
subscribed = true;
|
|
157
|
+
activityScopes.set(client, JSON.stringify({ ...scope, processId: process.pid }));
|
|
126
158
|
return client;
|
|
127
159
|
};
|
|
128
|
-
|
|
129
|
-
return await connect();
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
if (!["ENOENT", "ECONNREFUSED"].includes(error.code ?? ""))
|
|
133
|
-
throw error;
|
|
134
|
-
// Registration chooses transport; only the owner-safe installer may recover it.
|
|
135
|
-
await ensure();
|
|
136
|
-
return connect();
|
|
137
|
-
}
|
|
160
|
+
return connect();
|
|
138
161
|
})();
|
|
139
162
|
clients.set(pi, pending);
|
|
140
163
|
pending.catch(() => { if (clients.get(pi) === pending)
|
|
@@ -143,7 +166,10 @@ function observationClient(pi, scope) {
|
|
|
143
166
|
return pending;
|
|
144
167
|
}
|
|
145
168
|
export function closeBackgroundObservation(pi) {
|
|
169
|
+
subscribedClients.delete(pi);
|
|
146
170
|
generations.set(pi, (generations.get(pi) ?? 0) + 1);
|
|
171
|
+
connectionAborts.get(pi)?.abort();
|
|
172
|
+
connectionAborts.delete(pi);
|
|
147
173
|
const pending = clients.get(pi);
|
|
148
174
|
clients.delete(pi);
|
|
149
175
|
void pending?.then(client => client.close(), () => { });
|
|
@@ -4,18 +4,44 @@ import { join } from "node:path";
|
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { setTimeout as delay } from "node:timers/promises";
|
|
6
6
|
|
|
7
|
+
import { cursorKind } from "./shared-log-utils.js";
|
|
8
|
+
|
|
7
9
|
const hostedClients = new WeakSet<object>();
|
|
10
|
+
const connectionAborts = new WeakMap<object, AbortController>();
|
|
8
11
|
const clients = new WeakMap<object, Promise<any>>();
|
|
9
12
|
const generations = new WeakMap<object, number>();
|
|
10
13
|
const listeners = new WeakMap<object, Set<() => void>>();
|
|
14
|
+
const subscribedClients = new WeakMap<object, any>();
|
|
15
|
+
const activityScopes = new WeakMap<object, string>();
|
|
16
|
+
|
|
17
|
+
/** Goal reads the subscribed observation, never a periodic full-state RPC. */
|
|
18
|
+
export async function backgroundGoalObservation(pi: ExtensionAPI, ctx: ExtensionContext, cachedOnly = false): Promise<any> {
|
|
19
|
+
const scope = { background: true, cwd: ctx.cwd, sessionId: ctx.sessionManager?.getSessionId(), processId: process.pid };
|
|
20
|
+
const key = JSON.stringify(scope);
|
|
21
|
+
if (cachedOnly) {
|
|
22
|
+
const client = subscribedClients.get(pi);
|
|
23
|
+
return client && !client.closed && activityScopes.get(client) === key ? client.snapshot : undefined;
|
|
24
|
+
}
|
|
25
|
+
const client = await observationClient(pi, scope);
|
|
26
|
+
if (activityScopes.get(client) !== key) {
|
|
27
|
+
await client.subscribe(scope);
|
|
28
|
+
activityScopes.set(client, key);
|
|
29
|
+
}
|
|
30
|
+
return client.snapshot;
|
|
31
|
+
}
|
|
11
32
|
export function onBackgroundObservation(pi: ExtensionAPI, notify: () => void): () => void {
|
|
12
33
|
let set = listeners.get(pi);
|
|
13
34
|
if (!set) { set = new Set(); listeners.set(pi, set); }
|
|
14
35
|
set.add(notify); return () => { set.delete(notify); };
|
|
15
36
|
}
|
|
16
37
|
|
|
38
|
+
export function backgroundListNeedsHistory(params: Record<string, unknown>): boolean {
|
|
39
|
+
return params.all === true || params.limit !== undefined || params.status !== undefined
|
|
40
|
+
|| typeof params.cursor === "string" && cursorKind(params.cursor) !== "s";
|
|
41
|
+
}
|
|
42
|
+
|
|
17
43
|
/** A query connection grants no supervisor, transcript writer or retention authority. */
|
|
18
|
-
export function backgroundObservation(pi: ExtensionAPI, ctx: ExtensionContext, history = false, all = false, page?: { limit?: number; statuses?: string[]; cursor?: string }): Promise<any> {
|
|
44
|
+
export function backgroundObservation(pi: ExtensionAPI, ctx: ExtensionContext, history = false, all = false, page?: { limit?: number; statuses?: string[]; cursor?: string }, wait?: { timeoutMs: number; signal?: AbortSignal }): Promise<any> {
|
|
19
45
|
const scope = { background: true, cwd: ctx.cwd, sessionId: ctx.sessionManager?.getSessionId(), processId: process.pid };
|
|
20
46
|
const generation = generations.get(pi) ?? 0;
|
|
21
47
|
const read = async (retry: boolean): Promise<any> => {
|
|
@@ -24,12 +50,13 @@ export function backgroundObservation(pi: ExtensionAPI, ctx: ExtensionContext, h
|
|
|
24
50
|
const client = await connection;
|
|
25
51
|
if ((generations.get(pi) ?? 0) !== generation) throw new Error("runtime-unavailable: client closed");
|
|
26
52
|
try {
|
|
27
|
-
const deadline = Date.now() + 20000;
|
|
53
|
+
const deadline = Date.now() + (wait?.timeoutMs ?? 20000);
|
|
28
54
|
for (;;) {
|
|
55
|
+
wait?.signal?.throwIfAborted();
|
|
29
56
|
checkGeneration();
|
|
30
57
|
const snapshot = await client.get({ ...scope, backgroundHistory: history, backgroundAll: all, ...page });
|
|
31
58
|
if (!history || snapshot.status !== "rebuilding" || Date.now() >= deadline) return snapshot;
|
|
32
|
-
await delay(20);
|
|
59
|
+
await delay(wait ? 100 : 20, undefined, { signal: wait?.signal });
|
|
33
60
|
}
|
|
34
61
|
}
|
|
35
62
|
catch (error) {
|
|
@@ -49,6 +76,8 @@ function observationClient(pi: ExtensionAPI, scope: { background: boolean; cwd:
|
|
|
49
76
|
};
|
|
50
77
|
let pending = clients.get(pi);
|
|
51
78
|
if (!pending) {
|
|
79
|
+
const controller = new AbortController();
|
|
80
|
+
connectionAborts.set(pi, controller);
|
|
52
81
|
pending = (async () => {
|
|
53
82
|
let hosted: any;
|
|
54
83
|
let rejected: unknown;
|
|
@@ -59,50 +88,46 @@ function observationClient(pi: ExtensionAPI, scope: { background: boolean; cwd:
|
|
|
59
88
|
if (hosted) {
|
|
60
89
|
hostedClients.add(hosted);
|
|
61
90
|
hosted.onChange(() => { for (const notify of listeners.get(pi) ?? []) notify(); });
|
|
62
|
-
try { await hosted.subscribe(scope); checkOpen(); return hosted; }
|
|
91
|
+
try { await hosted.subscribe(scope); activityScopes.set(hosted, JSON.stringify({ ...scope, processId: process.pid })); checkOpen(); subscribedClients.set(pi, hosted); return hosted; }
|
|
63
92
|
catch (error) { hosted.close(); throw error; }
|
|
64
93
|
}
|
|
65
94
|
const requestedRoot = process.env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent");
|
|
66
95
|
mkdirSync(requestedRoot, { recursive: true, mode: 0o700 });
|
|
67
96
|
const root = realpathSync(requestedRoot);
|
|
68
|
-
const ensure = async () => {
|
|
69
|
-
checkOpen();
|
|
97
|
+
const ensure = async (timeoutMs?: number, signal?: AbortSignal) => {
|
|
70
98
|
const installer = new URL("../runtime-client/src/runtime/client.mjs", import.meta.url).href;
|
|
71
99
|
const { ensureRuntime } = await import(installer);
|
|
72
|
-
|
|
73
|
-
await ensureRuntime({ agentDir: root, serverDir: process.env.PI_SERVER_DIR });
|
|
100
|
+
await ensureRuntime({ agentDir: root, serverDir: process.env.PI_SERVER_DIR, timeoutMs, signal });
|
|
74
101
|
};
|
|
75
102
|
let registration;
|
|
76
103
|
try { registration = JSON.parse(readFileSync(join(root, "server-registration-v1.json"), "utf8")); }
|
|
77
104
|
catch (error) {
|
|
78
105
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
79
|
-
await ensure();
|
|
106
|
+
await ensure(undefined, controller.signal);
|
|
80
107
|
registration = JSON.parse(readFileSync(join(root, "server-registration-v1.json"), "utf8"));
|
|
81
108
|
}
|
|
82
109
|
if (registration.version !== 1 || registration.agentDir !== root) throw new Error("Invalid canonical runtime registration");
|
|
83
110
|
const module = new URL("./shared-work-client.mjs", import.meta.url).href;
|
|
84
|
-
const {
|
|
111
|
+
const { connectWorkClient } = await import(module);
|
|
85
112
|
const connect = async () => {
|
|
86
113
|
checkOpen();
|
|
87
|
-
const client =
|
|
114
|
+
const client = await connectWorkClient(join(registration.serverDir, "pi-server.sock"), root, ["runtime-background-state-v1"], {
|
|
115
|
+
signal: controller.signal, recover: ({ timeoutMs, signal }: { timeoutMs: number; signal: AbortSignal }) => ensure(timeoutMs, signal),
|
|
116
|
+
});
|
|
88
117
|
let subscribed = false;
|
|
89
118
|
client.onChange(() => {
|
|
90
119
|
if (!subscribed || clients.get(pi) !== pending) return;
|
|
91
120
|
if (client.closed) clients.delete(pi);
|
|
92
121
|
for (const notify of listeners.get(pi) ?? []) notify();
|
|
93
122
|
});
|
|
94
|
-
try { await client.subscribe(scope); }
|
|
123
|
+
try { checkOpen(); await client.subscribe(scope); checkOpen(); }
|
|
95
124
|
catch (error) { client.close(); throw error; }
|
|
125
|
+
subscribedClients.set(pi, client);
|
|
96
126
|
subscribed = true;
|
|
127
|
+
activityScopes.set(client, JSON.stringify({ ...scope, processId: process.pid }));
|
|
97
128
|
return client;
|
|
98
129
|
};
|
|
99
|
-
|
|
100
|
-
catch (error) {
|
|
101
|
-
if (!["ENOENT", "ECONNREFUSED"].includes((error as NodeJS.ErrnoException).code ?? "")) throw error;
|
|
102
|
-
// Registration chooses transport; only the owner-safe installer may recover it.
|
|
103
|
-
await ensure();
|
|
104
|
-
return connect();
|
|
105
|
-
}
|
|
130
|
+
return connect();
|
|
106
131
|
})();
|
|
107
132
|
clients.set(pi, pending);
|
|
108
133
|
pending.catch(() => { if (clients.get(pi) === pending) clients.delete(pi); });
|
|
@@ -111,7 +136,9 @@ function observationClient(pi: ExtensionAPI, scope: { background: boolean; cwd:
|
|
|
111
136
|
}
|
|
112
137
|
|
|
113
138
|
export function closeBackgroundObservation(pi: ExtensionAPI): void {
|
|
139
|
+
subscribedClients.delete(pi);
|
|
114
140
|
generations.set(pi, (generations.get(pi) ?? 0) + 1);
|
|
141
|
+
connectionAborts.get(pi)?.abort(); connectionAborts.delete(pi);
|
|
115
142
|
const pending = clients.get(pi); clients.delete(pi);
|
|
116
143
|
void pending?.then(client => client.close(), () => {});
|
|
117
144
|
}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import type { AgentSessionOverrides } from "./agent-session-settings.mjs";
|
|
2
|
+
import { readCatalogPreferences } from "./catalog-preferences.mjs";
|
|
2
3
|
import { CATALOG_SCHEMA_VERSION, type CatalogDefinition, type AgentDefinition, type CatalogKind, type CatalogScope, type Diagnostic, type ParseResult } from "./catalog-schema.mjs";
|
|
3
4
|
export type { CatalogScope, CatalogKind, CatalogDefinition, Diagnostic, ParseResult };
|
|
4
5
|
export declare const DEFAULT_PROJECT_CONFIG_DIR_NAME = ".pi";
|
|
5
6
|
export declare const SOURCE_PRECEDENCE: readonly ["project", "user", "bundled"];
|
|
6
7
|
export interface LoadCatalogInput {
|
|
8
|
+
/** Server-owned, bounded source reuse. Admission and discovery still run first. */
|
|
9
|
+
sources?: {
|
|
10
|
+
read(path: string): string;
|
|
11
|
+
parse(raw: string, path: string): ParseResult;
|
|
12
|
+
entry?(entry: CatalogEntry): CatalogEntry;
|
|
13
|
+
preferences?(root: string): ReturnType<typeof readCatalogPreferences>;
|
|
14
|
+
};
|
|
7
15
|
cwd: string;
|
|
8
16
|
/** Host trust decision. Untrusted projects do not contribute definitions. */
|
|
9
17
|
projectTrusted: boolean;
|
|
@@ -103,7 +111,7 @@ export declare function listScopedAgents(input: LoadCatalogInput, scope: "user"
|
|
|
103
111
|
}[];
|
|
104
112
|
diagnostics: Diagnostic[];
|
|
105
113
|
};
|
|
106
|
-
/**
|
|
114
|
+
/** Discover authorized sources. Identical bytes produce the same content digest. */
|
|
107
115
|
export declare function loadCatalog(input: LoadCatalogInput): CatalogSnapshot;
|
|
108
116
|
export declare function refreshCatalog(input: LoadCatalogInput): CatalogSnapshot;
|
|
109
117
|
export declare function createDefinition(input: SaveDefinitionInput): SaveResult;
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Filesystem catalog: project, personal, and bundled sources.
|
|
4
4
|
*
|
|
5
|
-
* Discovery is deterministic
|
|
6
|
-
*
|
|
5
|
+
* Discovery is deterministic. Direct callers read disk on every call; the server
|
|
6
|
+
* can supply bounded source reuse after admission. Callers launching a batch retain the snapshot
|
|
7
7
|
* and reuse that object for every job.
|
|
8
8
|
*
|
|
9
9
|
* Project files are read only when `projectTrusted` is true. Pass Pi's
|
|
@@ -69,12 +69,12 @@ export function listScopedAgents(input, scope) {
|
|
|
69
69
|
throw new Error("Unsafe project config directory");
|
|
70
70
|
const directory = scope === "project" ? projectCatalogPaths(input.cwd, config).agents : userCatalogPaths(input.userRoot).agents;
|
|
71
71
|
const occurrences = new Map();
|
|
72
|
-
collectDirectory(directory, scope, "agent", occurrences, diagnostics);
|
|
72
|
+
collectDirectory(directory, scope, "agent", occurrences, diagnostics, input.sources);
|
|
73
73
|
const agents = [...occurrences.values()].flat().flatMap(item => item.parse.definition?.kind === "agent"
|
|
74
74
|
? [{ path: item.path, definition: item.parse.definition }] : []);
|
|
75
75
|
return { agents, diagnostics };
|
|
76
76
|
}
|
|
77
|
-
/**
|
|
77
|
+
/** Discover authorized sources. Identical bytes produce the same content digest. */
|
|
78
78
|
export function loadCatalog(input) {
|
|
79
79
|
const loadedAt = new Date().toISOString();
|
|
80
80
|
const diagnostics = [];
|
|
@@ -91,8 +91,8 @@ export function loadCatalog(input) {
|
|
|
91
91
|
groups.set(scope, new Map());
|
|
92
92
|
if (input.projectTrusted) {
|
|
93
93
|
const project = projectCatalogPaths(input.cwd, configDirName);
|
|
94
|
-
collectDirectory(project.roles, "project", "role", groups.get("project"), diagnostics);
|
|
95
|
-
collectDirectory(project.agents, "project", "agent", groups.get("project"), diagnostics);
|
|
94
|
+
collectDirectory(project.roles, "project", "role", groups.get("project"), diagnostics, input.sources);
|
|
95
|
+
collectDirectory(project.agents, "project", "agent", groups.get("project"), diagnostics, input.sources);
|
|
96
96
|
}
|
|
97
97
|
else {
|
|
98
98
|
const project = projectCatalogPaths(input.cwd, configDirName);
|
|
@@ -101,8 +101,8 @@ export function loadCatalog(input) {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
const user = userCatalogPaths(input.userRoot);
|
|
104
|
-
collectDirectory(user.roles, "user", "role", groups.get("user"), diagnostics);
|
|
105
|
-
collectDirectory(user.agents, "user", "agent", groups.get("user"), diagnostics);
|
|
104
|
+
collectDirectory(user.roles, "user", "role", groups.get("user"), diagnostics, input.sources);
|
|
105
|
+
collectDirectory(user.agents, "user", "agent", groups.get("user"), diagnostics, input.sources);
|
|
106
106
|
const bundledRoles = input.bundledRoot ?? bundledRolesRoot();
|
|
107
107
|
if (!existsSync(bundledRoles)) {
|
|
108
108
|
diagnostics.push(diagnostic(DiagnosticCodes.bundledMissing, `Bundled role directory ${bundledRoles} is missing.`, {
|
|
@@ -114,23 +114,21 @@ export function loadCatalog(input) {
|
|
|
114
114
|
}));
|
|
115
115
|
}
|
|
116
116
|
else {
|
|
117
|
-
collectDirectory(bundledRoles, "bundled", "role", groups.get("bundled"), diagnostics);
|
|
117
|
+
collectDirectory(bundledRoles, "bundled", "role", groups.get("bundled"), diagnostics, input.sources);
|
|
118
118
|
}
|
|
119
119
|
if (input.bundledAgentsRoot) {
|
|
120
|
-
collectDirectory(input.bundledAgentsRoot, "bundled", "agent", groups.get("bundled"), diagnostics);
|
|
120
|
+
collectDirectory(input.bundledAgentsRoot, "bundled", "agent", groups.get("bundled"), diagnostics, input.sources);
|
|
121
121
|
}
|
|
122
122
|
const { entries, blocked } = selectIdentities(groups, diagnostics);
|
|
123
123
|
const savedSettings = {};
|
|
124
124
|
const preferenceSources = {};
|
|
125
|
-
const preferenceDigests = [];
|
|
126
125
|
const scopes = [
|
|
127
126
|
{ scope: "user", root: user.root },
|
|
128
127
|
...(input.projectTrusted ? [{ scope: "project", root: projectCatalogPaths(input.cwd, configDirName).root }] : []),
|
|
129
128
|
];
|
|
130
129
|
for (const { scope, root } of scopes) {
|
|
131
130
|
try {
|
|
132
|
-
const preferences = readCatalogPreferences(root);
|
|
133
|
-
preferenceDigests.push(preferences.digest);
|
|
131
|
+
const preferences = input.sources?.preferences?.(root) ?? readCatalogPreferences(root);
|
|
134
132
|
for (const entry of entries) {
|
|
135
133
|
if (scope === "user" && entry.scope === "project")
|
|
136
134
|
continue;
|
|
@@ -141,8 +139,8 @@ export function loadCatalog(input) {
|
|
|
141
139
|
if (own.bundled) {
|
|
142
140
|
const fallbacks = entry.scope === "bundled" ? [entry] : entry.unused.filter(source => source.scope === "bundled");
|
|
143
141
|
const fallback = fallbacks.length === 1 ? fallbacks[0] : undefined;
|
|
144
|
-
const raw = fallback ? readFileSync(fallback.path, "utf8") : undefined;
|
|
145
|
-
const parsed = raw === undefined ? undefined : parseDefinition(raw, fallback.path);
|
|
142
|
+
const raw = fallback ? (input.sources?.read(fallback.path) ?? readFileSync(fallback.path, "utf8")) : undefined;
|
|
143
|
+
const parsed = raw === undefined ? undefined : (input.sources?.parse(raw, fallback.path) ?? parseDefinition(raw, fallback.path));
|
|
146
144
|
if (!fallback || !parsed?.ok || parsed.definition?.id !== entry.id || parsed.diagnostics.some(item => item.blocking)
|
|
147
145
|
|| contentDigest(raw) !== fallback.contentDigest) {
|
|
148
146
|
const failure = diagnostic("catalog-preferences", `Cannot inherit bundled runtime defaults for ${entry.id}: a single valid bundled definition is required.`, {
|
|
@@ -182,8 +180,8 @@ export function loadCatalog(input) {
|
|
|
182
180
|
}
|
|
183
181
|
}
|
|
184
182
|
}
|
|
185
|
-
const snapshot = finishSnapshot(entries, blocked, diagnostics, loadedAt);
|
|
186
|
-
const digest = contentDigest(JSON.stringify([snapshot.digest,
|
|
183
|
+
const snapshot = finishSnapshot(entries, blocked, diagnostics, loadedAt, input.sources);
|
|
184
|
+
const digest = contentDigest(JSON.stringify([snapshot.digest, savedSettings, preferenceSources]));
|
|
187
185
|
return deepFreeze({ ...snapshot, digest, revision: digest, savedSettings, preferenceSources });
|
|
188
186
|
}
|
|
189
187
|
export function refreshCatalog(input) {
|
|
@@ -372,7 +370,7 @@ export function saveDefinition(input) {
|
|
|
372
370
|
export function isSafeConfigDirName(name) {
|
|
373
371
|
return /^\.[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(name) || /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(name);
|
|
374
372
|
}
|
|
375
|
-
function collectDirectory(directory, scope, directoryKind, into, diagnostics) {
|
|
373
|
+
function collectDirectory(directory, scope, directoryKind, into, diagnostics, sources) {
|
|
376
374
|
let stats;
|
|
377
375
|
try {
|
|
378
376
|
stats = lstatSync(directory);
|
|
@@ -449,7 +447,7 @@ function collectDirectory(directory, scope, directoryKind, into, diagnostics) {
|
|
|
449
447
|
continue;
|
|
450
448
|
let raw;
|
|
451
449
|
try {
|
|
452
|
-
raw = readFileSync(path, "utf8");
|
|
450
|
+
raw = sources?.read(path) ?? readFileSync(path, "utf8");
|
|
453
451
|
}
|
|
454
452
|
catch (error) {
|
|
455
453
|
diagnostics.push(diagnostic(DiagnosticCodes.ioError, `Could not read ${path}: ${error.message}`, {
|
|
@@ -462,7 +460,7 @@ function collectDirectory(directory, scope, directoryKind, into, diagnostics) {
|
|
|
462
460
|
}
|
|
463
461
|
let parse;
|
|
464
462
|
try {
|
|
465
|
-
parse = parseDefinition(raw, path);
|
|
463
|
+
parse = sources?.parse(raw, path) ?? parseDefinition(raw, path);
|
|
466
464
|
}
|
|
467
465
|
catch (error) {
|
|
468
466
|
const detail = error instanceof Error ? error.message : String(error);
|
|
@@ -586,11 +584,11 @@ function unusedSource(occurrence, reason) {
|
|
|
586
584
|
reason,
|
|
587
585
|
};
|
|
588
586
|
}
|
|
589
|
-
function finishSnapshot(entries, blocked, diagnostics, loadedAt) {
|
|
587
|
+
function finishSnapshot(entries, blocked, diagnostics, loadedAt, sources) {
|
|
590
588
|
const roleEntries = [];
|
|
591
589
|
const agentEntries = [];
|
|
592
590
|
for (const entry of entries) {
|
|
593
|
-
const frozen = deepFreeze(cloneEntry(entry));
|
|
591
|
+
const frozen = sources?.entry?.(entry) ?? deepFreeze(cloneEntry(entry));
|
|
594
592
|
if (frozen.kind === "role")
|
|
595
593
|
roleEntries.push([frozen.id, frozen]);
|
|
596
594
|
else
|