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
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* and must not collapse into stale. Raw log mtime/size is diagnostic only.
|
|
12
12
|
*/
|
|
13
13
|
import { statSync } from "node:fs";
|
|
14
|
-
import { DEFAULT_MAX_READ_BYTES, readAppendedLines } from "./log-cursor.mjs";
|
|
14
|
+
import { DEFAULT_MAX_READ_BYTES, readAppendedLines, parseLogEvent, logReadMetrics } from "./log-cursor.mjs";
|
|
15
15
|
import { logPathFor } from "./log-paths.mjs";
|
|
16
16
|
import { observeStall } from "./shared-stall-detector.mjs";
|
|
17
17
|
/** Defaults tuned for coordinator workloads; override per call or via config. */
|
|
@@ -142,188 +142,206 @@ export function mayAffectChildEventFacts(line) {
|
|
|
142
142
|
* that already parsed NDJSON). Skips non-objects; ignores raw non-JSON noise.
|
|
143
143
|
*/
|
|
144
144
|
export function extractChildEventFacts(events) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
let
|
|
150
|
-
|
|
151
|
-
let
|
|
152
|
-
let
|
|
153
|
-
let
|
|
154
|
-
|
|
145
|
+
return createChildEventFactFold()(events);
|
|
146
|
+
}
|
|
147
|
+
function createChildEventFactFold(saved) {
|
|
148
|
+
const openTools = new Map(saved?.openTools);
|
|
149
|
+
let anonymous = saved?.anonymous ?? 0;
|
|
150
|
+
const meaningful = saved?.meaningful ?? {};
|
|
151
|
+
let lastToolAt = saved?.lastToolAt;
|
|
152
|
+
let compacting = saved?.compacting ?? false;
|
|
153
|
+
let compactionStartedAt = saved?.compactionStartedAt;
|
|
154
|
+
let lastCompaction = saved?.lastCompaction;
|
|
155
|
+
let sawAgentSettled = saved?.sawAgentSettled ?? false;
|
|
156
|
+
let retry = saved?.retry;
|
|
157
|
+
const recovery = saved?.recovery ?? {
|
|
155
158
|
recoveredAfterError: false,
|
|
156
159
|
retrying: false,
|
|
157
160
|
willRetry: undefined,
|
|
158
161
|
lastError: undefined,
|
|
159
162
|
errorHistory: [],
|
|
160
163
|
};
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
lastToolAt = at ?? lastToolAt;
|
|
181
|
-
bumpMeaningful(meaningful, at);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (type === "tool_execution_end") {
|
|
185
|
-
const toolCallId = typeof e.toolCallId === "string" ? e.toolCallId : undefined;
|
|
186
|
-
if (toolCallId) {
|
|
187
|
-
openTools.delete(toolCallId);
|
|
164
|
+
const fold = (events) => {
|
|
165
|
+
for (const raw of events) {
|
|
166
|
+
logReadMetrics.healthFolded++;
|
|
167
|
+
if (!raw || typeof raw !== "object")
|
|
168
|
+
continue;
|
|
169
|
+
const e = raw;
|
|
170
|
+
const type = typeof e.type === "string" ? e.type : undefined;
|
|
171
|
+
if (!type)
|
|
172
|
+
continue;
|
|
173
|
+
// Only parsed-event write provenance counts as wall-clock activity.
|
|
174
|
+
const at = eventTime(e);
|
|
175
|
+
if (type === "tool_execution_start") {
|
|
176
|
+
const toolName = typeof e.toolName === "string" ? e.toolName : "unknown";
|
|
177
|
+
const toolCallId = typeof e.toolCallId === "string" ? e.toolCallId : undefined;
|
|
178
|
+
const key = toolCallId ?? `anonymous:${anonymous++}`;
|
|
179
|
+
openTools.set(key, { toolCallId, toolName, startedAt: at });
|
|
180
|
+
lastToolAt = at ?? lastToolAt;
|
|
181
|
+
bumpMeaningful(meaningful, at);
|
|
182
|
+
continue;
|
|
188
183
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
184
|
+
if (type === "tool_execution_update") {
|
|
185
|
+
lastToolAt = at ?? lastToolAt;
|
|
186
|
+
bumpMeaningful(meaningful, at);
|
|
187
|
+
continue;
|
|
193
188
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
at
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const msg = typeof e.errorMessage === "string" ? e.errorMessage : "model retry";
|
|
230
|
-
recovery.lastError = { message: msg, at };
|
|
231
|
-
pushError(recovery.errorHistory, recovery.lastError);
|
|
232
|
-
bumpMeaningful(meaningful, at);
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
if (type === "auto_retry_end") {
|
|
236
|
-
recovery.retrying = false;
|
|
237
|
-
const success = e.success === true;
|
|
238
|
-
if (success) {
|
|
239
|
-
markRecovered(recovery);
|
|
189
|
+
if (type === "tool_execution_end") {
|
|
190
|
+
const toolCallId = typeof e.toolCallId === "string" ? e.toolCallId : undefined;
|
|
191
|
+
if (toolCallId) {
|
|
192
|
+
openTools.delete(toolCallId);
|
|
193
|
+
}
|
|
194
|
+
else if (typeof e.toolName === "string") {
|
|
195
|
+
const match = [...openTools].find(([, t]) => t.toolName === e.toolName);
|
|
196
|
+
if (match)
|
|
197
|
+
openTools.delete(match[0]);
|
|
198
|
+
}
|
|
199
|
+
lastToolAt = at ?? lastToolAt;
|
|
200
|
+
bumpMeaningful(meaningful, at);
|
|
201
|
+
// Successful tool completion supersedes prior retry intent (AC9).
|
|
202
|
+
if (e.isError !== true)
|
|
203
|
+
markRecovered(recovery);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (type === "compaction_start") {
|
|
207
|
+
compacting = true;
|
|
208
|
+
compactionStartedAt = at;
|
|
209
|
+
bumpMeaningful(meaningful, at);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (type === "compaction_end") {
|
|
213
|
+
compacting = false;
|
|
214
|
+
lastCompaction = {
|
|
215
|
+
reason: typeof e.reason === "string" ? e.reason : undefined,
|
|
216
|
+
aborted: typeof e.aborted === "boolean" ? e.aborted : undefined,
|
|
217
|
+
willRetry: typeof e.willRetry === "boolean" ? e.willRetry : undefined,
|
|
218
|
+
errorMessage: typeof e.errorMessage === "string" ? e.errorMessage : undefined,
|
|
219
|
+
at,
|
|
220
|
+
};
|
|
221
|
+
compactionStartedAt = undefined;
|
|
222
|
+
bumpMeaningful(meaningful, at);
|
|
223
|
+
continue;
|
|
240
224
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
225
|
+
if (type === "auto_retry_start") {
|
|
226
|
+
recovery.retrying = true;
|
|
227
|
+
recovery.recoveredAfterError = false;
|
|
228
|
+
recovery.willRetry = true;
|
|
229
|
+
retry = {
|
|
230
|
+
attempt: typeof e.attempt === "number" ? e.attempt : undefined,
|
|
231
|
+
maxAttempts: typeof e.maxAttempts === "number" ? e.maxAttempts : undefined,
|
|
232
|
+
startedAt: at,
|
|
233
|
+
};
|
|
234
|
+
const msg = typeof e.errorMessage === "string" ? e.errorMessage : "model retry";
|
|
245
235
|
recovery.lastError = { message: msg, at };
|
|
246
236
|
pushError(recovery.errorHistory, recovery.lastError);
|
|
247
|
-
|
|
237
|
+
bumpMeaningful(meaningful, at);
|
|
238
|
+
continue;
|
|
248
239
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
?
|
|
258
|
-
: "
|
|
259
|
-
recovery.lastError = { message, at };
|
|
240
|
+
if (type === "auto_retry_end") {
|
|
241
|
+
recovery.retrying = false;
|
|
242
|
+
const success = e.success === true;
|
|
243
|
+
if (success) {
|
|
244
|
+
markRecovered(recovery);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
const msg = typeof e.finalError === "string"
|
|
248
|
+
? e.finalError
|
|
249
|
+
: (recovery.lastError?.message ?? "model retry exhausted");
|
|
250
|
+
recovery.lastError = { message: msg, at };
|
|
260
251
|
pushError(recovery.errorHistory, recovery.lastError);
|
|
261
252
|
recovery.recoveredAfterError = false;
|
|
262
|
-
// Model-error evidence is meaningful for timing, not healthy progress.
|
|
263
|
-
bumpMeaningful(meaningful, at);
|
|
264
253
|
}
|
|
265
|
-
|
|
254
|
+
bumpMeaningful(meaningful, at);
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (type === "message_end") {
|
|
258
|
+
const msg = e.message;
|
|
259
|
+
if (isAssistantMessage(msg)) {
|
|
260
|
+
if (assistantIsError(msg)) {
|
|
261
|
+
const message = typeof msg.errorMessage === "string"
|
|
262
|
+
? msg.errorMessage
|
|
263
|
+
: "assistant stopReason=error";
|
|
264
|
+
recovery.lastError = { message, at };
|
|
265
|
+
pushError(recovery.errorHistory, recovery.lastError);
|
|
266
|
+
recovery.recoveredAfterError = false;
|
|
267
|
+
// Model-error evidence is meaningful for timing, not healthy progress.
|
|
268
|
+
bumpMeaningful(meaningful, at);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
bumpMeaningful(meaningful, at);
|
|
272
|
+
// Later successful assistant evidence clears retry/warning (AC9).
|
|
273
|
+
markRecovered(recovery);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
// user message_end is not child progress
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (type === "turn_end") {
|
|
280
|
+
const msg = e.message;
|
|
281
|
+
if (isAssistantMessage(msg) && !assistantIsError(msg)) {
|
|
266
282
|
bumpMeaningful(meaningful, at);
|
|
267
|
-
// Later successful assistant evidence clears retry/warning (AC9).
|
|
268
283
|
markRecovered(recovery);
|
|
269
284
|
}
|
|
285
|
+
continue;
|
|
270
286
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
287
|
+
if (type === "agent_end") {
|
|
288
|
+
if (typeof e.willRetry === "boolean")
|
|
289
|
+
recovery.willRetry = e.willRetry;
|
|
290
|
+
if (recovery.willRetry === true) {
|
|
291
|
+
recovery.retrying = true;
|
|
292
|
+
recovery.recoveredAfterError = false;
|
|
293
|
+
bumpMeaningful(meaningful, at);
|
|
294
|
+
}
|
|
295
|
+
continue;
|
|
279
296
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
recovery.willRetry
|
|
285
|
-
|
|
286
|
-
recovery.retrying = true;
|
|
287
|
-
recovery.recoveredAfterError = false;
|
|
297
|
+
if (type === "agent_settled") {
|
|
298
|
+
sawAgentSettled = true;
|
|
299
|
+
// Settled ends the active retry phase; retain error history for detail.
|
|
300
|
+
recovery.retrying = false;
|
|
301
|
+
if (recovery.willRetry === true)
|
|
302
|
+
recovery.willRetry = false;
|
|
288
303
|
bumpMeaningful(meaningful, at);
|
|
304
|
+
continue;
|
|
289
305
|
}
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
if (type === "agent_settled") {
|
|
293
|
-
sawAgentSettled = true;
|
|
294
|
-
// Settled ends the active retry phase; retain error history for detail.
|
|
295
|
-
recovery.retrying = false;
|
|
296
|
-
if (recovery.willRetry === true)
|
|
297
|
-
recovery.willRetry = false;
|
|
298
|
-
bumpMeaningful(meaningful, at);
|
|
299
|
-
continue;
|
|
306
|
+
// message_update is noisy; intentionally NOT meaningful for stale detection.
|
|
300
307
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
longModelCallSupported: false,
|
|
308
|
+
let modelState = "ok";
|
|
309
|
+
if (recovery.retrying || recovery.willRetry === true)
|
|
310
|
+
modelState = "retrying";
|
|
311
|
+
else if (recovery.lastError && !recovery.recoveredAfterError)
|
|
312
|
+
modelState = "error";
|
|
313
|
+
else
|
|
314
|
+
modelState = "ok";
|
|
315
|
+
return {
|
|
316
|
+
lastMeaningfulAt: meaningful.last,
|
|
317
|
+
activeTools: [...openTools.values()],
|
|
318
|
+
lastToolAt,
|
|
319
|
+
compacting,
|
|
320
|
+
compactionStartedAt,
|
|
321
|
+
lastCompaction,
|
|
322
|
+
model: {
|
|
323
|
+
state: modelState,
|
|
324
|
+
lastError: recovery.lastError,
|
|
325
|
+
errorHistory: [...recovery.errorHistory],
|
|
326
|
+
retry,
|
|
327
|
+
},
|
|
328
|
+
sawAgentSettled,
|
|
329
|
+
willRetry: recovery.willRetry,
|
|
330
|
+
longModelCallSupported: false,
|
|
331
|
+
};
|
|
326
332
|
};
|
|
333
|
+
return Object.assign(fold, { snapshot: () => ({ openTools: [...openTools], anonymous,
|
|
334
|
+
meaningful, lastToolAt, compacting, compactionStartedAt, lastCompaction,
|
|
335
|
+
sawAgentSettled, retry, recovery }) });
|
|
336
|
+
}
|
|
337
|
+
export function encodeHealthLogState(state) {
|
|
338
|
+
return { cursor: state.cursor, truncated: state.truncated, fold: state.fold.snapshot() };
|
|
339
|
+
}
|
|
340
|
+
export function decodeHealthLogState(saved) {
|
|
341
|
+
if (!saved?.fold?.recovery || !Array.isArray(saved.fold.openTools)
|
|
342
|
+
|| !Number.isSafeInteger(saved.cursor?.offset) || saved.cursor.offset < 0)
|
|
343
|
+
throw new Error("Invalid health checkpoint");
|
|
344
|
+
return { cursor: saved.cursor, truncated: saved.truncated, fold: createChildEventFactFold(saved.fold) };
|
|
327
345
|
}
|
|
328
346
|
/** Parse the fact-relevant events out of raw NDJSON lines. */
|
|
329
347
|
function parseFactEvents(lines) {
|
|
@@ -336,7 +354,7 @@ function parseFactEvents(lines) {
|
|
|
336
354
|
if (!mayAffectChildEventFacts(s))
|
|
337
355
|
continue;
|
|
338
356
|
try {
|
|
339
|
-
events.push(
|
|
357
|
+
events.push(parseLogEvent(s));
|
|
340
358
|
sizes.push(s.length);
|
|
341
359
|
}
|
|
342
360
|
catch {
|
|
@@ -347,14 +365,8 @@ function parseFactEvents(lines) {
|
|
|
347
365
|
}
|
|
348
366
|
// ---- incremental log reading ----------------------------------------------
|
|
349
367
|
//
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
// (see log-cursor.ts). Only the types the fold consumes are retained, which is
|
|
353
|
-
// ~2% of a log's lines; `message_update` alone is 98% of the volume and cannot
|
|
354
|
-
// move a fact. The retained list is capped, oldest-first, and a drop is
|
|
355
|
-
// reported as a diagnostic rather than silently changing an observation.
|
|
356
|
-
/** Memory budget for one run's retained fact-relevant events. */
|
|
357
|
-
const MAX_RETAINED_EVENT_BYTES = 8 * 1024 * 1024; // 8 MiB
|
|
368
|
+
// Retain structural facts, not source events. Only newly completed lines enter
|
|
369
|
+
// the fold; a skipped byte window remains explicitly reported in rawLog.
|
|
358
370
|
function maxHealthReadBytes() {
|
|
359
371
|
const raw = process.env.PI_SUBAGENT_MAX_HEALTH_READ_BYTES;
|
|
360
372
|
if (!raw)
|
|
@@ -389,15 +401,6 @@ function rememberState(id, state) {
|
|
|
389
401
|
factLogStates.delete(oldest.value);
|
|
390
402
|
}
|
|
391
403
|
}
|
|
392
|
-
function trimToBudget(state) {
|
|
393
|
-
while (state.bytes > MAX_RETAINED_EVENT_BYTES && state.events.length > 1) {
|
|
394
|
-
state.events.shift();
|
|
395
|
-
state.bytes -= state.sizes.shift() ?? 0;
|
|
396
|
-
state.dropped = true;
|
|
397
|
-
}
|
|
398
|
-
if (state.bytes < 0)
|
|
399
|
-
state.bytes = 0;
|
|
400
|
-
}
|
|
401
404
|
/**
|
|
402
405
|
* Read a run log for event facts + raw mtime/size diagnostics.
|
|
403
406
|
* Raw log write time never promotes activity health by itself.
|
|
@@ -428,7 +431,8 @@ export function extractChildEventFactsFromLog(id, opts = {}) {
|
|
|
428
431
|
return { facts: extractChildEventFacts(events), rawLog };
|
|
429
432
|
}
|
|
430
433
|
const key = opts.logPath ?? id;
|
|
431
|
-
const
|
|
434
|
+
const states = opts.states ?? factLogStates;
|
|
435
|
+
const previous = states.get(key);
|
|
432
436
|
const read = readAppendedLines(path, previous?.cursor, maxHealthReadBytes());
|
|
433
437
|
if (read.error !== undefined) {
|
|
434
438
|
rawLog.error = rawLog.error ?? read.error;
|
|
@@ -436,24 +440,19 @@ export function extractChildEventFactsFromLog(id, opts = {}) {
|
|
|
436
440
|
return { facts: emptyFacts(), rawLog };
|
|
437
441
|
}
|
|
438
442
|
const state = read.restarted || !previous
|
|
439
|
-
? { cursor: read.cursor,
|
|
443
|
+
? { cursor: read.cursor, fold: createChildEventFactFold(), truncated: false }
|
|
440
444
|
: previous;
|
|
441
445
|
state.cursor = read.cursor;
|
|
442
446
|
state.truncated = state.truncated || read.truncated;
|
|
443
447
|
const parsed = parseFactEvents(read.lines);
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
trimToBudget(state);
|
|
450
|
-
}
|
|
451
|
-
rememberState(key, state);
|
|
448
|
+
const facts = state.fold(parsed.events);
|
|
449
|
+
if (opts.states)
|
|
450
|
+
states.set(key, state);
|
|
451
|
+
else
|
|
452
|
+
rememberState(key, state);
|
|
452
453
|
if (state.truncated)
|
|
453
454
|
rawLog.windowTruncated = true;
|
|
454
|
-
|
|
455
|
-
rawLog.eventsDropped = true;
|
|
456
|
-
return { facts: extractChildEventFacts(state.events), rawLog };
|
|
455
|
+
return { facts, rawLog };
|
|
457
456
|
}
|
|
458
457
|
function processLiveness(status, supervised) {
|
|
459
458
|
if (status === "orphaned")
|
|
@@ -18,31 +18,68 @@
|
|
|
18
18
|
* cursor skips ahead rather than falling permanently behind, and
|
|
19
19
|
* `truncated` reports that bytes were never seen.
|
|
20
20
|
*/
|
|
21
|
-
import { closeSync, openSync, readSync, statSync } from "node:fs";
|
|
21
|
+
import { closeSync, openSync, readSync, statSync, fstatSync } from "node:fs";
|
|
22
22
|
/** Bytes of the log head kept to detect a rewrite behind our back. */
|
|
23
23
|
const HEAD_SAMPLE_BYTES = 256;
|
|
24
|
+
export const logReadMetrics = { bytes: 0, reads: 0, parsedEvents: 0, displayFolded: 0, healthFolded: 0 };
|
|
25
|
+
let batch;
|
|
26
|
+
/** Share identical ranges and decoding only during a synchronous observation. */
|
|
27
|
+
export function withLogReadBatch(observe) {
|
|
28
|
+
const previous = batch;
|
|
29
|
+
batch = { reads: new Map(), events: new Map(), ranges: new Map() };
|
|
30
|
+
try {
|
|
31
|
+
return observe();
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
batch = previous;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function parseLogEvent(line) {
|
|
38
|
+
if (batch?.events.has(line))
|
|
39
|
+
return batch.events.get(line);
|
|
40
|
+
const event = JSON.parse(line);
|
|
41
|
+
logReadMetrics.parsedEvents++;
|
|
42
|
+
batch?.events.set(line, event);
|
|
43
|
+
return event;
|
|
44
|
+
}
|
|
24
45
|
/** Default ceiling on a single read: also the cold-start tail window. */
|
|
25
46
|
export const DEFAULT_MAX_READ_BYTES = 32 * 1024 * 1024; // 32 MiB
|
|
26
47
|
function errText(err) {
|
|
27
48
|
return err instanceof Error ? err.message : String(err);
|
|
28
49
|
}
|
|
29
|
-
function readAt(fd, position, length) {
|
|
50
|
+
function readAt(fd, position, length, path) {
|
|
30
51
|
const buf = Buffer.allocUnsafe(length);
|
|
52
|
+
const ranges = batch?.ranges.get(path) ?? [];
|
|
31
53
|
let filled = 0;
|
|
32
54
|
while (filled < length) {
|
|
33
|
-
const
|
|
55
|
+
const at = position + filled;
|
|
56
|
+
const cached = ranges.find(range => range.start <= at && range.start + range.data.length > at);
|
|
57
|
+
if (cached) {
|
|
58
|
+
const offset = at - cached.start;
|
|
59
|
+
const n = Math.min(length - filled, cached.data.length - offset);
|
|
60
|
+
cached.data.copy(buf, filled, offset, offset + n);
|
|
61
|
+
filled += n;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const next = Math.min(position + length, ...ranges.filter(range => range.start > at).map(range => range.start));
|
|
65
|
+
const n = readSync(fd, buf, filled, next - at, at);
|
|
34
66
|
if (n <= 0)
|
|
35
67
|
break;
|
|
68
|
+
if (batch) {
|
|
69
|
+
ranges.push({ start: at, data: buf.subarray(filled, filled + n) });
|
|
70
|
+
batch.ranges.set(path, ranges);
|
|
71
|
+
}
|
|
36
72
|
filled += n;
|
|
73
|
+
logReadMetrics.bytes += n;
|
|
37
74
|
}
|
|
38
75
|
return filled === length ? buf : buf.subarray(0, filled);
|
|
39
76
|
}
|
|
40
77
|
/** Head sample as a byte-exact string, for rewrite detection. */
|
|
41
|
-
function readHead(fd, size) {
|
|
78
|
+
function readHead(fd, size, path) {
|
|
42
79
|
const length = Math.min(HEAD_SAMPLE_BYTES, size);
|
|
43
80
|
if (length <= 0)
|
|
44
81
|
return "";
|
|
45
|
-
return readAt(fd, 0, length).toString("latin1");
|
|
82
|
+
return readAt(fd, 0, length, path).toString("latin1");
|
|
46
83
|
}
|
|
47
84
|
/**
|
|
48
85
|
* Same log? Compare the prefix both samples cover, so a head that is still
|
|
@@ -62,7 +99,17 @@ function headsAgree(previous, current) {
|
|
|
62
99
|
* Passing `previous: undefined` starts cold: the last `maxBytes` of the log,
|
|
63
100
|
* with the leading partial line dropped.
|
|
64
101
|
*/
|
|
65
|
-
export function readAppendedLines(path, previous, maxBytes = DEFAULT_MAX_READ_BYTES) {
|
|
102
|
+
export function readAppendedLines(path, previous, maxBytes = DEFAULT_MAX_READ_BYTES, cumulative = false) {
|
|
103
|
+
const key = JSON.stringify([path, previous, maxBytes, cumulative]);
|
|
104
|
+
const cached = batch?.reads.get(key);
|
|
105
|
+
if (cached)
|
|
106
|
+
return cached;
|
|
107
|
+
const result = readLines(path, previous, maxBytes, cumulative);
|
|
108
|
+
batch?.reads.set(key, result);
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
function readLines(path, previous, maxBytes, cumulative) {
|
|
112
|
+
logReadMetrics.reads++;
|
|
66
113
|
const empty = (error) => ({
|
|
67
114
|
lines: [],
|
|
68
115
|
cursor: previous ?? { offset: 0, head: "" },
|
|
@@ -86,54 +133,76 @@ export function readAppendedLines(path, previous, maxBytes = DEFAULT_MAX_READ_BY
|
|
|
86
133
|
return empty(errText(err));
|
|
87
134
|
}
|
|
88
135
|
try {
|
|
89
|
-
const
|
|
136
|
+
const info = fstatSync(fd);
|
|
137
|
+
size = info.size;
|
|
138
|
+
const generation = `${info.dev}:${info.ino}:${info.birthtimeMs}`;
|
|
139
|
+
const head = readHead(fd, size, path);
|
|
90
140
|
// A log that shrank or whose head changed is a different log: the old
|
|
91
141
|
// offset would point into unrelated bytes. Compare only the prefix the
|
|
92
142
|
// two samples share, so a log still shorter than the sample window is
|
|
93
143
|
// not "rewritten" every time it grows.
|
|
94
144
|
const resumable = previous !== undefined
|
|
145
|
+
&& (previous.generation === undefined || previous.generation === generation)
|
|
146
|
+
&& !(previous.size === size && previous.modified !== undefined && previous.modified !== info.mtimeMs)
|
|
95
147
|
&& previous.offset <= size
|
|
96
148
|
&& headsAgree(previous.head, head);
|
|
97
149
|
const restarted = previous !== undefined && !resumable;
|
|
150
|
+
let coverageStart = resumable ? previous.coverageStart ?? 0 : 0;
|
|
151
|
+
let missing = resumable ? previous.missing : undefined;
|
|
152
|
+
let skipLeading = resumable ? previous.skipLeading : false;
|
|
153
|
+
const cursor = (offset) => ({ offset, head, generation, size, modified: info.mtimeMs, coverageStart, missing, skipLeading });
|
|
98
154
|
let start = resumable ? previous.offset : 0;
|
|
99
155
|
let truncated = false;
|
|
100
|
-
if (size - start > maxBytes) {
|
|
156
|
+
if (!cumulative && size - start > maxBytes) {
|
|
101
157
|
// Skip ahead instead of lagging: a chatty log must not push the
|
|
102
158
|
// cursor permanently into the past.
|
|
103
159
|
start = size - maxBytes;
|
|
160
|
+
coverageStart = start;
|
|
161
|
+
missing = [missing?.[0] ?? (resumable ? previous.offset : 0), start];
|
|
104
162
|
truncated = true;
|
|
105
163
|
}
|
|
106
164
|
// We begin mid-line whenever bytes were skipped.
|
|
107
|
-
const startedMidLine = truncated;
|
|
165
|
+
const startedMidLine = truncated || skipLeading;
|
|
108
166
|
if (size === start) {
|
|
109
|
-
return { lines: [], cursor:
|
|
167
|
+
return { lines: [], cursor: cursor(start), totalBytes: size, truncated, restarted };
|
|
110
168
|
}
|
|
111
|
-
const
|
|
169
|
+
const end = cumulative ? Math.min(size, start + maxBytes) : size;
|
|
170
|
+
const buf = readAt(fd, start, end - start, path);
|
|
112
171
|
const lastNewline = buf.lastIndexOf(0x0a);
|
|
113
172
|
if (lastNewline === -1) {
|
|
114
173
|
// No complete line yet. Hold position so the line can finish —
|
|
115
174
|
// unless it is an oversized partial we already skipped into, which
|
|
116
175
|
// would otherwise be re-read forever.
|
|
117
|
-
const
|
|
176
|
+
const oversized = cumulative && end < size;
|
|
177
|
+
if (oversized)
|
|
178
|
+
missing = [missing?.[0] ?? start, end];
|
|
179
|
+
if (oversized)
|
|
180
|
+
skipLeading = true;
|
|
181
|
+
const offset = startedMidLine || oversized ? start + buf.length : start;
|
|
118
182
|
const partial = startedMidLine ? undefined : buf.toString("utf-8");
|
|
119
|
-
return { lines: [], partial, cursor:
|
|
183
|
+
return { lines: [], partial: oversized ? undefined : partial, cursor: cursor(offset), totalBytes: size, truncated: truncated || oversized, restarted, more: end < size };
|
|
120
184
|
}
|
|
121
185
|
const text = buf.subarray(0, lastNewline + 1).toString("utf-8");
|
|
122
186
|
const lines = text.split("\n");
|
|
123
187
|
lines.pop(); // trailing "" after the final newline
|
|
124
|
-
if (startedMidLine)
|
|
188
|
+
if (startedMidLine) {
|
|
125
189
|
lines.shift(); // partial first line
|
|
190
|
+
missing = [missing?.[0] ?? 0, start + buf.indexOf(0x0a) + 1];
|
|
191
|
+
coverageStart = Math.max(coverageStart, missing[1]);
|
|
192
|
+
skipLeading = false;
|
|
193
|
+
}
|
|
126
194
|
// Anything after the final newline is unterminated: hand it over
|
|
127
195
|
// separately rather than pretending it is a line.
|
|
128
196
|
const trailing = buf.subarray(lastNewline + 1);
|
|
129
|
-
const partial = trailing.length > 0 ? trailing.toString("utf-8") : undefined;
|
|
197
|
+
const partial = end === size && trailing.length > 0 ? trailing.toString("utf-8") : undefined;
|
|
130
198
|
return {
|
|
131
199
|
lines,
|
|
132
200
|
partial,
|
|
133
|
-
cursor:
|
|
201
|
+
cursor: cursor(start + lastNewline + 1),
|
|
134
202
|
totalBytes: size,
|
|
135
203
|
truncated,
|
|
136
204
|
restarted,
|
|
205
|
+
more: end < size,
|
|
137
206
|
};
|
|
138
207
|
}
|
|
139
208
|
catch (err) {
|