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
|
@@ -5,6 +5,8 @@ import { isAbsolute, join, relative, resolve } from "node:path";
|
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
6
|
import { runDir, writeMeta } from "./registry.domain.mjs";
|
|
7
7
|
import { credentialFilePaths } from "./shared-sandbox-core.domain.mjs";
|
|
8
|
+
import { OperationBridgeError, redactSecret } from "./operation-bridge-types.domain.mjs";
|
|
9
|
+
import { requireText } from "./shared-operation-coordinator/src/validate.domain.mjs";
|
|
8
10
|
const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
9
11
|
const inside = (root, path) => { const rel = relative(root, path); return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel)); };
|
|
10
12
|
function retainedBytes(path, limit) {
|
|
@@ -22,13 +24,57 @@ function retainedBytes(path, limit) {
|
|
|
22
24
|
closeSync(fd);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
27
|
+
export function assignmentReportOutcomeReason(report) {
|
|
28
|
+
if (report.status === "complete")
|
|
29
|
+
return report.reason;
|
|
30
|
+
return (`Assignment incomplete: ${report.reason}`
|
|
31
|
+
+ (report.blocker ? ` Blocked (${report.blocker.kind}): ${report.blocker.reason}.` : "")
|
|
32
|
+
+ (report.remainingWork ? ` Remaining work: ${report.remainingWork.join("; ")}.` : "")).slice(0, 8192);
|
|
33
|
+
}
|
|
34
|
+
function validateReportText(report) {
|
|
35
|
+
requireText("report.reason", report.reason, 2048);
|
|
36
|
+
for (const item of report.remainingWork ?? [])
|
|
37
|
+
requireText("report.remaining_work", item, 1024);
|
|
38
|
+
if (report.blocker)
|
|
39
|
+
requireText("report.blocker.reason", report.blocker.reason, 1024);
|
|
40
|
+
for (const evidence of report.evidence)
|
|
41
|
+
requireText("report.evidence.explanation", evidence.explanation, 1024);
|
|
42
|
+
requireText("report.outcome_reason", assignmentReportOutcomeReason(report), 8192);
|
|
43
|
+
}
|
|
25
44
|
/** Runs synchronously under the authenticated handler: no yield between authority and persistence. */
|
|
26
45
|
export function recordAssignmentReport(id, input, assertAuthority, current) {
|
|
27
46
|
const meta = assertAuthority();
|
|
47
|
+
try {
|
|
48
|
+
return retainAssignmentReport(id, input, meta, assertAuthority, current);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
// Refused input is not a committed report: the same writer may correct it.
|
|
52
|
+
// Recheck the live writer before retaining even diagnostic metadata.
|
|
53
|
+
recordAssignmentReportRejection(error instanceof Error ? error.message : String(error), assertAuthority);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Also used for authenticated bridge refusals that never enter the report handler. */
|
|
58
|
+
export function recordAssignmentReportRejection(reason, assertAuthority) {
|
|
59
|
+
const latest = assertAuthority();
|
|
60
|
+
if (!latest.assignmentReport)
|
|
61
|
+
writeMeta({ ...latest, assignmentReportRejection: {
|
|
62
|
+
reason: redactSecret(reason), rejectedAt: Date.now(),
|
|
63
|
+
} });
|
|
64
|
+
}
|
|
65
|
+
function retainAssignmentReport(id, input, meta, assertAuthority, current) {
|
|
28
66
|
const report = input;
|
|
29
67
|
if (!["complete", "incomplete"].includes(report.status) || !report.reason?.trim() || report.reason.length > 2048
|
|
30
68
|
|| !Array.isArray(report.evidence) || report.evidence.length < 1 || report.evidence.length > 4)
|
|
31
69
|
throw new Error("Invalid assignment report.");
|
|
70
|
+
if (report.remaining_work !== undefined && (!Array.isArray(report.remaining_work) || report.remaining_work.length < 1 || report.remaining_work.length > 8
|
|
71
|
+
|| report.remaining_work.some((item) => typeof item !== "string" || !item.trim() || item.length > 1024)))
|
|
72
|
+
throw new Error("Remaining work needs 1 to 8 bounded descriptions.");
|
|
73
|
+
if (report.blocker && (!["permission", "external-input"].includes(report.blocker.kind) || !report.blocker.reason?.trim()
|
|
74
|
+
|| report.blocker.reason.length > 1024))
|
|
75
|
+
throw new Error("A blocker needs its permission or external-input kind and a concrete reason.");
|
|
76
|
+
if (report.status === "complete" && (report.remaining_work || report.blocker))
|
|
77
|
+
throw new Error("A complete assignment cannot have remaining work or a blocker.");
|
|
32
78
|
const root = realpathSync(meta.cwd);
|
|
33
79
|
const directory = join(runDir(id), "assignment-report-v1");
|
|
34
80
|
const captured = meta.operations?.policy.sandbox?.permissions;
|
|
@@ -40,36 +86,25 @@ export function recordAssignmentReport(id, input, assertAuthority, current) {
|
|
|
40
86
|
}
|
|
41
87
|
if (meta.operations?.policy.sandbox && !current?.permissions)
|
|
42
88
|
throw new Error("Current assignment evidence read authority unavailable.");
|
|
43
|
-
|
|
89
|
+
// Keep the legacy digest for whole-file reports without the additive fields.
|
|
90
|
+
const request = [report.status, report.reason, report.evidence.map((e) => e.offset === undefined && e.max_bytes === undefined
|
|
91
|
+
? [e.path, e.explanation] : [e.path, e.explanation, e.offset ?? 0, e.max_bytes])];
|
|
92
|
+
if (report.remaining_work || report.blocker)
|
|
93
|
+
request.push([report.remaining_work ?? null,
|
|
94
|
+
report.blocker ? [report.blocker.kind, report.blocker.reason] : null]);
|
|
95
|
+
const requestDigest = hash(JSON.stringify(request));
|
|
44
96
|
if (meta.assignmentReport) {
|
|
45
97
|
const accepted = readAssignmentReport(meta);
|
|
46
98
|
if (accepted.requestDigest !== requestDigest)
|
|
47
99
|
throw new Error("Conflicting assignment report refused.");
|
|
48
100
|
return { reportId: meta.assignmentReport.digest, status: accepted.status, note: "Report retained. Assignment terminalization after exit and exact parent disposition remain separate." };
|
|
49
101
|
}
|
|
50
|
-
const snapshots = report.evidence.map((e) =>
|
|
51
|
-
if (!e.path || !e.explanation?.trim() || e.explanation.length > 1024)
|
|
52
|
-
throw new Error("Evidence needs a bounded explanation.");
|
|
53
|
-
const source = realpathSync(resolve(root, e.path));
|
|
54
|
-
if (!inside(root, source) || credentialFilePaths(homedir()).some((p) => inside(p, source)))
|
|
55
|
-
throw new Error("Foreign or credential assignment evidence refused.");
|
|
56
|
-
const fd = openSync(source, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
57
|
-
try {
|
|
58
|
-
const stat = fstatSync(fd);
|
|
59
|
-
if (!stat.isFile() || stat.size > 65536)
|
|
60
|
-
throw new Error("Assignment evidence must be a file of at most 65536 bytes.");
|
|
61
|
-
const bytes = Buffer.alloc(stat.size);
|
|
62
|
-
if (readSync(fd, bytes, 0, bytes.length, 0) !== bytes.length)
|
|
63
|
-
throw new Error("Assignment evidence changed during capture.");
|
|
64
|
-
return { bytes, digest: hash(bytes), source, explanation: e.explanation };
|
|
65
|
-
}
|
|
66
|
-
finally {
|
|
67
|
-
closeSync(fd);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
102
|
+
const snapshots = report.evidence.map((e) => captureEvidence(root, e));
|
|
70
103
|
const retained = { version: 1, assignmentId: meta.assignment.assignmentId, attemptId: id, requestDigest,
|
|
71
104
|
status: report.status, reason: report.reason,
|
|
72
|
-
|
|
105
|
+
...(report.remaining_work ? { remainingWork: report.remaining_work } : {}), ...(report.blocker ? { blocker: report.blocker } : {}),
|
|
106
|
+
evidence: snapshots.map(({ digest, source, explanation, range }) => ({ path: join(directory, digest), digest, source, explanation, ...(range ? { range } : {}) })) };
|
|
107
|
+
validateReportText(retained);
|
|
73
108
|
const text = JSON.stringify(retained);
|
|
74
109
|
if (Buffer.byteLength(text) > 65536)
|
|
75
110
|
throw new Error("Assignment report exceeds its retained metadata bound.");
|
|
@@ -86,10 +121,52 @@ export function recordAssignmentReport(id, input, assertAuthority, current) {
|
|
|
86
121
|
writeFileSync(join(directory, snapshot.digest), snapshot.bytes, { mode: 0o600 });
|
|
87
122
|
const path = join(directory, `${digest}.json`);
|
|
88
123
|
writeFileSync(path, text, { mode: 0o600 });
|
|
89
|
-
|
|
124
|
+
const updated = { ...latest, assignmentReport: { version: 1, assignmentId: retained.assignmentId, attemptId: id, digest, path } };
|
|
125
|
+
delete updated.assignmentReportRejection;
|
|
126
|
+
writeMeta(updated);
|
|
90
127
|
}
|
|
91
128
|
return { reportId: digest, status: report.status, note: "Report retained. Assignment terminalization after exit and exact parent disposition remain separate." };
|
|
92
129
|
}
|
|
130
|
+
function captureEvidence(root, e) {
|
|
131
|
+
try {
|
|
132
|
+
if (!e.path || !e.explanation?.trim() || e.explanation.length > 1024)
|
|
133
|
+
throw new Error("Evidence needs a bounded explanation.");
|
|
134
|
+
const source = realpathSync(resolve(root, e.path));
|
|
135
|
+
if (credentialFilePaths(homedir()).some((p) => inside(p, source)))
|
|
136
|
+
throw new Error("Foreign or credential assignment evidence refused.");
|
|
137
|
+
if (!inside(root, source))
|
|
138
|
+
throw new Error(`Foreign assignment evidence refused: the source must resolve inside the assigned workspace ${JSON.stringify(root)}. A sibling directory is outside this boundary. Use an existing workspace file or place the reportable artifact inside the workspace, then retry.`);
|
|
139
|
+
const fd = openSync(source, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
140
|
+
try {
|
|
141
|
+
const stat = fstatSync(fd);
|
|
142
|
+
if (!stat.isFile())
|
|
143
|
+
throw new Error(`Assignment evidence ${JSON.stringify(e.path)} must be a regular file.`);
|
|
144
|
+
const offset = e.offset ?? 0;
|
|
145
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset > stat.size || (offset === stat.size && offset !== 0)
|
|
146
|
+
|| (e.offset !== undefined && e.max_bytes === undefined)
|
|
147
|
+
|| (e.max_bytes !== undefined && (!Number.isSafeInteger(e.max_bytes) || e.max_bytes < 1 || e.max_bytes > 65536))) {
|
|
148
|
+
throw new Error(`Invalid assignment evidence range for ${JSON.stringify(e.path)} (${stat.size} bytes). Use offset within the file and max_bytes from 1 through 65536.`);
|
|
149
|
+
}
|
|
150
|
+
if (e.max_bytes === undefined && stat.size > 65536) {
|
|
151
|
+
throw new Error(`Assignment evidence ${JSON.stringify(e.path)} is ${stat.size} bytes; the retained limit is 65536 bytes. Retry with offset and max_bytes (at most 65536) for the relevant excerpt.`);
|
|
152
|
+
}
|
|
153
|
+
const bytes = Buffer.alloc(Math.min(stat.size - offset, e.max_bytes ?? stat.size));
|
|
154
|
+
if (readSync(fd, bytes, 0, bytes.length, offset) !== bytes.length)
|
|
155
|
+
throw new Error("Assignment evidence changed during capture.");
|
|
156
|
+
const after = fstatSync(fd);
|
|
157
|
+
if (after.size !== stat.size || after.mtimeMs !== stat.mtimeMs || after.ctimeMs !== stat.ctimeMs)
|
|
158
|
+
throw new Error("Assignment evidence changed during capture.");
|
|
159
|
+
return { bytes, digest: hash(bytes), source, explanation: e.explanation,
|
|
160
|
+
...(e.max_bytes === undefined ? {} : { range: { offset, bytes: bytes.length, sourceBytes: stat.size } }) };
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
closeSync(fd);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
throw new OperationBridgeError("tool-error", error instanceof Error ? error.message : String(error), true);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
93
170
|
export function readAssignmentReport(meta) {
|
|
94
171
|
const ref = meta.assignmentReport;
|
|
95
172
|
if (!ref)
|
|
@@ -104,8 +181,16 @@ export function readAssignmentReport(meta) {
|
|
|
104
181
|
if (report.version !== 1 || report.assignmentId !== ref.assignmentId || report.attemptId !== meta.id)
|
|
105
182
|
throw new Error("Foreign assignment report.");
|
|
106
183
|
for (const e of report.evidence) {
|
|
107
|
-
if (!/^[a-f0-9]{64}$/.test(e.digest) || e.path !== join(runDir(meta.id), "assignment-report-v1", e.digest)
|
|
184
|
+
if (!/^[a-f0-9]{64}$/.test(e.digest) || e.path !== join(runDir(meta.id), "assignment-report-v1", e.digest))
|
|
185
|
+
throw new Error("Retained assignment evidence changed.");
|
|
186
|
+
const bytes = retainedBytes(e.path, 65536);
|
|
187
|
+
if (hash(bytes) !== e.digest || (e.range && (!Number.isSafeInteger(e.range.offset) || e.range.offset < 0
|
|
188
|
+
|| !Number.isSafeInteger(e.range.sourceBytes) || e.range.sourceBytes < 0 || e.range.bytes !== bytes.length
|
|
189
|
+
|| e.range.offset + e.range.bytes > e.range.sourceBytes)))
|
|
108
190
|
throw new Error("Retained assignment evidence changed.");
|
|
109
191
|
}
|
|
192
|
+
// Older accepted reports may contain prose the terminal store cannot retain.
|
|
193
|
+
// Treat those as unverified evidence rather than throwing during finalization.
|
|
194
|
+
validateReportText(report);
|
|
110
195
|
return report;
|
|
111
196
|
}
|
|
@@ -5,6 +5,8 @@ import { homedir } from "node:os";
|
|
|
5
5
|
import { runDir, writeMeta, type RunMeta } from "./registry.ts";
|
|
6
6
|
import { credentialFilePaths } from "./shared-sandbox-core.ts";
|
|
7
7
|
import type { PermissionSnapshot, PermissionProfile } from "./permission-policy.ts";
|
|
8
|
+
import { OperationBridgeError, redactSecret } from "./operation-bridge-types.ts";
|
|
9
|
+
import { requireText } from "./shared-operation-coordinator/src/validate.ts";
|
|
8
10
|
|
|
9
11
|
const hash = (bytes: string | Buffer) => createHash("sha256").update(bytes).digest("hex");
|
|
10
12
|
const inside = (root: string, path: string) => { const rel = relative(root, path); return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel)); };
|
|
@@ -20,15 +22,62 @@ function retainedBytes(path: string, limit: number): Buffer {
|
|
|
20
22
|
}
|
|
21
23
|
export interface AssignmentReport {
|
|
22
24
|
version: 1; assignmentId: string; attemptId: string; requestDigest: string; status: "complete" | "incomplete"; reason: string;
|
|
23
|
-
|
|
25
|
+
remainingWork?: string[];
|
|
26
|
+
blocker?: { kind: "permission" | "external-input"; reason: string };
|
|
27
|
+
evidence: Array<{ path: string; digest: string; source: string; explanation: string; range?: { offset: number; bytes: number; sourceBytes: number } }>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface ReportInput {
|
|
31
|
+
status: string; reason: string;
|
|
32
|
+
remaining_work?: string[];
|
|
33
|
+
blocker?: AssignmentReport["blocker"];
|
|
34
|
+
evidence: Array<{ path: string; explanation: string; offset?: number; max_bytes?: number }>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function assignmentReportOutcomeReason(report: AssignmentReport): string {
|
|
38
|
+
if (report.status === "complete") return report.reason;
|
|
39
|
+
return (`Assignment incomplete: ${report.reason}`
|
|
40
|
+
+ (report.blocker ? ` Blocked (${report.blocker.kind}): ${report.blocker.reason}.` : "")
|
|
41
|
+
+ (report.remainingWork ? ` Remaining work: ${report.remainingWork.join("; ")}.` : "")).slice(0, 8192);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function validateReportText(report: AssignmentReport): void {
|
|
45
|
+
requireText("report.reason", report.reason, 2048);
|
|
46
|
+
for (const item of report.remainingWork ?? []) requireText("report.remaining_work", item, 1024);
|
|
47
|
+
if (report.blocker) requireText("report.blocker.reason", report.blocker.reason, 1024);
|
|
48
|
+
for (const evidence of report.evidence) requireText("report.evidence.explanation", evidence.explanation, 1024);
|
|
49
|
+
requireText("report.outcome_reason", assignmentReportOutcomeReason(report), 8192);
|
|
24
50
|
}
|
|
25
51
|
|
|
26
52
|
/** Runs synchronously under the authenticated handler: no yield between authority and persistence. */
|
|
27
53
|
export function recordAssignmentReport(id: string, input: unknown, assertAuthority: () => RunMeta, current?: PermissionSnapshot) {
|
|
28
54
|
const meta = assertAuthority();
|
|
29
|
-
|
|
55
|
+
try { return retainAssignmentReport(id, input, meta, assertAuthority, current); }
|
|
56
|
+
catch (error) {
|
|
57
|
+
// Refused input is not a committed report: the same writer may correct it.
|
|
58
|
+
// Recheck the live writer before retaining even diagnostic metadata.
|
|
59
|
+
recordAssignmentReportRejection(error instanceof Error ? error.message : String(error), assertAuthority);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Also used for authenticated bridge refusals that never enter the report handler. */
|
|
65
|
+
export function recordAssignmentReportRejection(reason: string, assertAuthority: () => RunMeta): void {
|
|
66
|
+
const latest = assertAuthority();
|
|
67
|
+
if (!latest.assignmentReport) writeMeta({ ...latest, assignmentReportRejection: {
|
|
68
|
+
reason: redactSecret(reason), rejectedAt: Date.now(),
|
|
69
|
+
} });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function retainAssignmentReport(id: string, input: unknown, meta: RunMeta, assertAuthority: () => RunMeta, current?: PermissionSnapshot) {
|
|
73
|
+
const report = input as ReportInput;
|
|
30
74
|
if (!["complete", "incomplete"].includes(report.status) || !report.reason?.trim() || report.reason.length > 2048
|
|
31
75
|
|| !Array.isArray(report.evidence) || report.evidence.length < 1 || report.evidence.length > 4) throw new Error("Invalid assignment report.");
|
|
76
|
+
if (report.remaining_work !== undefined && (!Array.isArray(report.remaining_work) || report.remaining_work.length < 1 || report.remaining_work.length > 8
|
|
77
|
+
|| report.remaining_work.some((item) => typeof item !== "string" || !item.trim() || item.length > 1024))) throw new Error("Remaining work needs 1 to 8 bounded descriptions.");
|
|
78
|
+
if (report.blocker && (!["permission", "external-input"].includes(report.blocker.kind) || !report.blocker.reason?.trim()
|
|
79
|
+
|| report.blocker.reason.length > 1024)) throw new Error("A blocker needs its permission or external-input kind and a concrete reason.");
|
|
80
|
+
if (report.status === "complete" && (report.remaining_work || report.blocker)) throw new Error("A complete assignment cannot have remaining work or a blocker.");
|
|
32
81
|
const root = realpathSync(meta.cwd);
|
|
33
82
|
const directory = join(runDir(id), "assignment-report-v1");
|
|
34
83
|
const captured = (meta.operations?.policy.sandbox as { permissions?: Partial<PermissionProfile> } | null)?.permissions;
|
|
@@ -37,28 +86,23 @@ export function recordAssignmentReport(id: string, input: unknown, assertAuthori
|
|
|
37
86
|
if (policy?.enabled && policy.projectFiles !== "read" && policy.projectFiles !== "read-write") throw new Error("Current assignment evidence read permission refused.");
|
|
38
87
|
}
|
|
39
88
|
if (meta.operations?.policy.sandbox && !current?.permissions) throw new Error("Current assignment evidence read authority unavailable.");
|
|
40
|
-
|
|
89
|
+
// Keep the legacy digest for whole-file reports without the additive fields.
|
|
90
|
+
const request: unknown[] = [report.status, report.reason, report.evidence.map((e) => e.offset === undefined && e.max_bytes === undefined
|
|
91
|
+
? [e.path, e.explanation] : [e.path, e.explanation, e.offset ?? 0, e.max_bytes])];
|
|
92
|
+
if (report.remaining_work || report.blocker) request.push([report.remaining_work ?? null,
|
|
93
|
+
report.blocker ? [report.blocker.kind, report.blocker.reason] : null]);
|
|
94
|
+
const requestDigest = hash(JSON.stringify(request));
|
|
41
95
|
if (meta.assignmentReport) {
|
|
42
96
|
const accepted = readAssignmentReport(meta)!;
|
|
43
97
|
if (accepted.requestDigest !== requestDigest) throw new Error("Conflicting assignment report refused.");
|
|
44
98
|
return { reportId: meta.assignmentReport.digest, status: accepted.status, note: "Report retained. Assignment terminalization after exit and exact parent disposition remain separate." };
|
|
45
99
|
}
|
|
46
|
-
const snapshots = report.evidence.map((e) =>
|
|
47
|
-
if (!e.path || !e.explanation?.trim() || e.explanation.length > 1024) throw new Error("Evidence needs a bounded explanation.");
|
|
48
|
-
const source = realpathSync(resolve(root, e.path));
|
|
49
|
-
if (!inside(root, source) || credentialFilePaths(homedir()).some((p) => inside(p, source))) throw new Error("Foreign or credential assignment evidence refused.");
|
|
50
|
-
const fd = openSync(source, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
51
|
-
try {
|
|
52
|
-
const stat = fstatSync(fd);
|
|
53
|
-
if (!stat.isFile() || stat.size > 65536) throw new Error("Assignment evidence must be a file of at most 65536 bytes.");
|
|
54
|
-
const bytes = Buffer.alloc(stat.size);
|
|
55
|
-
if (readSync(fd, bytes, 0, bytes.length, 0) !== bytes.length) throw new Error("Assignment evidence changed during capture.");
|
|
56
|
-
return { bytes, digest: hash(bytes), source, explanation: e.explanation };
|
|
57
|
-
} finally { closeSync(fd); }
|
|
58
|
-
});
|
|
100
|
+
const snapshots = report.evidence.map((e) => captureEvidence(root, e));
|
|
59
101
|
const retained: AssignmentReport = { version: 1, assignmentId: meta.assignment!.assignmentId, attemptId: id, requestDigest,
|
|
60
102
|
status: report.status as AssignmentReport["status"], reason: report.reason,
|
|
61
|
-
|
|
103
|
+
...(report.remaining_work ? { remainingWork: report.remaining_work } : {}), ...(report.blocker ? { blocker: report.blocker } : {}),
|
|
104
|
+
evidence: snapshots.map(({ digest, source, explanation, range }) => ({ path: join(directory, digest), digest, source, explanation, ...(range ? { range } : {}) })) };
|
|
105
|
+
validateReportText(retained);
|
|
62
106
|
const text = JSON.stringify(retained);
|
|
63
107
|
if (Buffer.byteLength(text) > 65536) throw new Error("Assignment report exceeds its retained metadata bound.");
|
|
64
108
|
const digest = hash(text);
|
|
@@ -71,11 +115,44 @@ export function recordAssignmentReport(id: string, input: unknown, assertAuthori
|
|
|
71
115
|
for (const snapshot of snapshots) writeFileSync(join(directory, snapshot.digest), snapshot.bytes, { mode: 0o600 });
|
|
72
116
|
const path = join(directory, `${digest}.json`);
|
|
73
117
|
writeFileSync(path, text, { mode: 0o600 });
|
|
74
|
-
|
|
118
|
+
const updated = { ...latest, assignmentReport: { version: 1 as const, assignmentId: retained.assignmentId, attemptId: id, digest, path } };
|
|
119
|
+
delete updated.assignmentReportRejection;
|
|
120
|
+
writeMeta(updated);
|
|
75
121
|
}
|
|
76
122
|
return { reportId: digest, status: report.status, note: "Report retained. Assignment terminalization after exit and exact parent disposition remain separate." };
|
|
77
123
|
}
|
|
78
124
|
|
|
125
|
+
function captureEvidence(root: string, e: ReportInput["evidence"][number]) {
|
|
126
|
+
try {
|
|
127
|
+
if (!e.path || !e.explanation?.trim() || e.explanation.length > 1024) throw new Error("Evidence needs a bounded explanation.");
|
|
128
|
+
const source = realpathSync(resolve(root, e.path));
|
|
129
|
+
if (credentialFilePaths(homedir()).some((p) => inside(p, source))) throw new Error("Foreign or credential assignment evidence refused.");
|
|
130
|
+
if (!inside(root, source)) throw new Error(`Foreign assignment evidence refused: the source must resolve inside the assigned workspace ${JSON.stringify(root)}. A sibling directory is outside this boundary. Use an existing workspace file or place the reportable artifact inside the workspace, then retry.`);
|
|
131
|
+
const fd = openSync(source, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
132
|
+
try {
|
|
133
|
+
const stat = fstatSync(fd);
|
|
134
|
+
if (!stat.isFile()) throw new Error(`Assignment evidence ${JSON.stringify(e.path)} must be a regular file.`);
|
|
135
|
+
const offset = e.offset ?? 0;
|
|
136
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset > stat.size || (offset === stat.size && offset !== 0)
|
|
137
|
+
|| (e.offset !== undefined && e.max_bytes === undefined)
|
|
138
|
+
|| (e.max_bytes !== undefined && (!Number.isSafeInteger(e.max_bytes) || e.max_bytes < 1 || e.max_bytes > 65536))) {
|
|
139
|
+
throw new Error(`Invalid assignment evidence range for ${JSON.stringify(e.path)} (${stat.size} bytes). Use offset within the file and max_bytes from 1 through 65536.`);
|
|
140
|
+
}
|
|
141
|
+
if (e.max_bytes === undefined && stat.size > 65536) {
|
|
142
|
+
throw new Error(`Assignment evidence ${JSON.stringify(e.path)} is ${stat.size} bytes; the retained limit is 65536 bytes. Retry with offset and max_bytes (at most 65536) for the relevant excerpt.`);
|
|
143
|
+
}
|
|
144
|
+
const bytes = Buffer.alloc(Math.min(stat.size - offset, e.max_bytes ?? stat.size));
|
|
145
|
+
if (readSync(fd, bytes, 0, bytes.length, offset) !== bytes.length) throw new Error("Assignment evidence changed during capture.");
|
|
146
|
+
const after = fstatSync(fd);
|
|
147
|
+
if (after.size !== stat.size || after.mtimeMs !== stat.mtimeMs || after.ctimeMs !== stat.ctimeMs) throw new Error("Assignment evidence changed during capture.");
|
|
148
|
+
return { bytes, digest: hash(bytes), source, explanation: e.explanation,
|
|
149
|
+
...(e.max_bytes === undefined ? {} : { range: { offset, bytes: bytes.length, sourceBytes: stat.size } }) };
|
|
150
|
+
} finally { closeSync(fd); }
|
|
151
|
+
} catch (error) {
|
|
152
|
+
throw new OperationBridgeError("tool-error", error instanceof Error ? error.message : String(error), true);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
79
156
|
export function readAssignmentReport(meta: RunMeta): AssignmentReport | null {
|
|
80
157
|
const ref = meta.assignmentReport;
|
|
81
158
|
if (!ref) return null;
|
|
@@ -86,7 +163,14 @@ export function readAssignmentReport(meta: RunMeta): AssignmentReport | null {
|
|
|
86
163
|
const report = JSON.parse(text) as AssignmentReport;
|
|
87
164
|
if (report.version !== 1 || report.assignmentId !== ref.assignmentId || report.attemptId !== meta.id) throw new Error("Foreign assignment report.");
|
|
88
165
|
for (const e of report.evidence) {
|
|
89
|
-
if (!/^[a-f0-9]{64}$/.test(e.digest) || e.path !== join(runDir(meta.id), "assignment-report-v1", e.digest)
|
|
166
|
+
if (!/^[a-f0-9]{64}$/.test(e.digest) || e.path !== join(runDir(meta.id), "assignment-report-v1", e.digest)) throw new Error("Retained assignment evidence changed.");
|
|
167
|
+
const bytes = retainedBytes(e.path, 65536);
|
|
168
|
+
if (hash(bytes) !== e.digest || (e.range && (!Number.isSafeInteger(e.range.offset) || e.range.offset < 0
|
|
169
|
+
|| !Number.isSafeInteger(e.range.sourceBytes) || e.range.sourceBytes < 0 || e.range.bytes !== bytes.length
|
|
170
|
+
|| e.range.offset + e.range.bytes > e.range.sourceBytes))) throw new Error("Retained assignment evidence changed.");
|
|
90
171
|
}
|
|
172
|
+
// Older accepted reports may contain prose the terminal store cannot retain.
|
|
173
|
+
// Treat those as unverified evidence rather than throwing during finalization.
|
|
174
|
+
validateReportText(report);
|
|
91
175
|
return report;
|
|
92
176
|
}
|
|
@@ -90,13 +90,15 @@ export function mergeJobOptions(shared, job) {
|
|
|
90
90
|
cwd: job.cwd ?? shared?.cwd,
|
|
91
91
|
// Same semantics as subagent_spawn: shared and per-job values must reach
|
|
92
92
|
// spawnSubagentRun so batch jobs can request disposable clone workspaces.
|
|
93
|
+
isolation: job.isolation ?? shared?.isolation,
|
|
94
|
+
recover_in_clone: job.recover_in_clone,
|
|
93
95
|
git_clone_workspace: job.git_clone_workspace ?? shared?.git_clone_workspace,
|
|
94
96
|
approve: job.approve ?? shared?.approve,
|
|
95
97
|
allow_nested: job.allow_nested ?? shared?.allow_nested,
|
|
96
98
|
// Native session id selected for this JOB only. A shared value would make
|
|
97
99
|
// every job contend for the same conversation writer, so it is not inherited.
|
|
98
|
-
session_id: typeof job.session_id === "string" ? job.session_id : undefined,
|
|
99
|
-
session_name: typeof job.session_name === "string" ? job.session_name : undefined,
|
|
100
|
+
session_id: typeof job.session_id === "string" ? normalizeSessionSelector(job.session_id) : undefined,
|
|
101
|
+
session_name: typeof job.session_name === "string" ? normalizeSessionSelector(job.session_name) : undefined,
|
|
100
102
|
// Harness timing; an explicit null means "not set", so it falls through to shared.
|
|
101
103
|
deadline_minutes: job.deadline_minutes ?? shared?.deadline_minutes,
|
|
102
104
|
grace_minutes: job.grace_minutes ?? shared?.grace_minutes,
|
|
@@ -108,6 +110,10 @@ export function mergeJobOptions(shared, job) {
|
|
|
108
110
|
...catalogSelector(shared, job),
|
|
109
111
|
};
|
|
110
112
|
}
|
|
113
|
+
/** Keep nonblank selectors exact. Only a blank string is equivalent to omission. */
|
|
114
|
+
export function normalizeSessionSelector(value) {
|
|
115
|
+
return typeof value === "string" && value.trim() === "" ? undefined : value;
|
|
116
|
+
}
|
|
111
117
|
function catalogText(value) {
|
|
112
118
|
return typeof value === "string" && value.trim() !== "" ? value : undefined;
|
|
113
119
|
}
|
|
@@ -148,11 +154,13 @@ export function validateBatchPlan({ shared, jobs, onCapacity, config }) {
|
|
|
148
154
|
if (typeof job.prompt !== "string" || job.prompt.trim() === "") {
|
|
149
155
|
throw new Error(`job ${i + 1} is missing a non-empty prompt.`);
|
|
150
156
|
}
|
|
151
|
-
|
|
157
|
+
const sessionId = normalizeSessionSelector(job.session_id);
|
|
158
|
+
const sessionName = normalizeSessionSelector(job.session_name);
|
|
159
|
+
if (sessionId !== undefined && sessionName !== undefined)
|
|
152
160
|
throw new Error(`job ${i + 1}: pass either session_id or session_name, not both.`);
|
|
153
|
-
if (
|
|
161
|
+
if (sessionName !== undefined && typeof sessionName !== "string")
|
|
154
162
|
throw new Error(`job ${i + 1} session_name must be a non-empty name.`);
|
|
155
|
-
if (
|
|
163
|
+
if (sessionId !== undefined && typeof sessionId !== "string") {
|
|
156
164
|
throw new Error(`job ${i + 1} session_id must be a non-empty native session id.`);
|
|
157
165
|
}
|
|
158
166
|
if (seenPrompts.has(job.prompt)) {
|
|
@@ -3,7 +3,7 @@ import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
|
3
3
|
import { lstatSync, mkdirSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { processStartToken } from "./registry.domain.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { connectWorkClient } from "./shared-work-client.mjs";
|
|
7
7
|
import { overlayDigest } from "./catalog-overlay.domain.mjs";
|
|
8
8
|
export { overlayDigest } from "./catalog-overlay.domain.mjs";
|
|
9
9
|
export function createCatalogHostAdapter() {
|
|
@@ -11,6 +11,8 @@ export function createCatalogHostAdapter() {
|
|
|
11
11
|
let live;
|
|
12
12
|
let timer;
|
|
13
13
|
let epoch = 0;
|
|
14
|
+
let pendingConnection;
|
|
15
|
+
let refreshPending = false;
|
|
14
16
|
let admitting = Promise.resolve(undefined);
|
|
15
17
|
/** Fixed Pi adapter only. Never exposed as a tool or socket issuance operation. */
|
|
16
18
|
function noteCatalogHostContext(ctx, overlay) {
|
|
@@ -18,6 +20,8 @@ export function createCatalogHostAdapter() {
|
|
|
18
20
|
}
|
|
19
21
|
function closeCatalogHost() {
|
|
20
22
|
epoch++;
|
|
23
|
+
pendingConnection?.abort();
|
|
24
|
+
pendingConnection = undefined;
|
|
21
25
|
clearInterval(timer);
|
|
22
26
|
timer = undefined;
|
|
23
27
|
if (live) {
|
|
@@ -55,9 +59,15 @@ export function createCatalogHostAdapter() {
|
|
|
55
59
|
const id = randomUUID(), secret = randomBytes(32).toString("hex"), generation = randomUUID();
|
|
56
60
|
const path = join(root, `${id}.json`), staging = join(root, `.${id}`);
|
|
57
61
|
const authority = { id, secret, generation, cwd: scope.cwd, sessionId: scope.sessionId, branchId: scope.branchId };
|
|
58
|
-
const
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
pendingConnection = controller;
|
|
64
|
+
const client = await connectWorkClient(socketPath, agentDir, ["runtime-catalog-v1"], { signal: controller.signal })
|
|
65
|
+
.finally(() => { if (pendingConnection === controller)
|
|
66
|
+
pendingConnection = undefined; });
|
|
59
67
|
try {
|
|
60
68
|
const hello = await client.ready;
|
|
69
|
+
if (epoch !== admittedEpoch || !current)
|
|
70
|
+
throw new Error("catalog-host-revoked");
|
|
61
71
|
const pidStartTime = processStartToken(process.pid);
|
|
62
72
|
if (!pidStartTime)
|
|
63
73
|
throw new Error("catalog-host-identity-unavailable");
|
|
@@ -68,7 +78,15 @@ export function createCatalogHostAdapter() {
|
|
|
68
78
|
throw new Error("catalog-host-revoked");
|
|
69
79
|
live = { client, path, key, authority };
|
|
70
80
|
if (!timer) {
|
|
71
|
-
timer = setInterval(() => {
|
|
81
|
+
timer = setInterval(() => {
|
|
82
|
+
if (refreshPending)
|
|
83
|
+
return;
|
|
84
|
+
refreshPending = true;
|
|
85
|
+
void catalogHostAuthority(agentDir, current?.ctx.cwd ?? cwd, socketPath)
|
|
86
|
+
.catch(() => { if (epoch === admittedEpoch)
|
|
87
|
+
closeCatalogHost(); })
|
|
88
|
+
.finally(() => { refreshPending = false; });
|
|
89
|
+
}, 500);
|
|
72
90
|
timer.unref();
|
|
73
91
|
}
|
|
74
92
|
return authority;
|
|
@@ -2,7 +2,7 @@ import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
|
2
2
|
import { lstatSync, mkdirSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { processStartToken } from "./registry.ts";
|
|
5
|
-
import { WorkClient } from "./shared-work-client.mjs";
|
|
5
|
+
import { WorkClient, connectWorkClient } from "./shared-work-client.mjs";
|
|
6
6
|
import type { AgentSessionOverrides } from "./agent-session-settings.ts";
|
|
7
7
|
import { overlayDigest } from "./catalog-overlay.ts";
|
|
8
8
|
export { overlayDigest } from "./catalog-overlay.ts";
|
|
@@ -13,6 +13,8 @@ export function createCatalogHostAdapter() {
|
|
|
13
13
|
let live: { client: WorkClient; path: string; key: string; authority: Record<string, unknown> } | undefined;
|
|
14
14
|
let timer: ReturnType<typeof setInterval> | undefined;
|
|
15
15
|
let epoch = 0;
|
|
16
|
+
let pendingConnection: AbortController | undefined;
|
|
17
|
+
let refreshPending = false;
|
|
16
18
|
let admitting: Promise<Record<string, unknown> | undefined> = Promise.resolve(undefined);
|
|
17
19
|
|
|
18
20
|
/** Fixed Pi adapter only. Never exposed as a tool or socket issuance operation. */
|
|
@@ -22,6 +24,7 @@ export function createCatalogHostAdapter() {
|
|
|
22
24
|
|
|
23
25
|
function closeCatalogHost() {
|
|
24
26
|
epoch++;
|
|
27
|
+
pendingConnection?.abort(); pendingConnection = undefined;
|
|
25
28
|
clearInterval(timer); timer = undefined;
|
|
26
29
|
if (live) { live.client.close(); rmSync(live.path, { force: true }); live = undefined; }
|
|
27
30
|
current = undefined;
|
|
@@ -48,9 +51,13 @@ export function createCatalogHostAdapter() {
|
|
|
48
51
|
const id = randomUUID(), secret = randomBytes(32).toString("hex"), generation = randomUUID();
|
|
49
52
|
const path = join(root, `${id}.json`), staging = join(root, `.${id}`);
|
|
50
53
|
const authority = { id, secret, generation, cwd: scope.cwd, sessionId: scope.sessionId, branchId: scope.branchId };
|
|
51
|
-
const
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
pendingConnection = controller;
|
|
56
|
+
const client = await connectWorkClient(socketPath, agentDir, ["runtime-catalog-v1"], { signal: controller.signal })
|
|
57
|
+
.finally(() => { if (pendingConnection === controller) pendingConnection = undefined; });
|
|
52
58
|
try {
|
|
53
59
|
const hello = await client.ready;
|
|
60
|
+
if (epoch !== admittedEpoch || !current) throw new Error("catalog-host-revoked");
|
|
54
61
|
const pidStartTime = processStartToken(process.pid);
|
|
55
62
|
if (!pidStartTime) throw new Error("catalog-host-identity-unavailable");
|
|
56
63
|
writeFileSync(staging, JSON.stringify({ version: 1, id, generation, serverInstance: hello.instanceId, pid: process.pid, pidStartTime, secretHash: hash(secret), ...scope }), { mode: 0o600, flag: "wx" });
|
|
@@ -58,7 +65,16 @@ export function createCatalogHostAdapter() {
|
|
|
58
65
|
await client.request("runtime_bind_catalog_host", { authority });
|
|
59
66
|
if (epoch !== admittedEpoch || !current) throw new Error("catalog-host-revoked");
|
|
60
67
|
live = { client, path, key, authority };
|
|
61
|
-
if (!timer) {
|
|
68
|
+
if (!timer) {
|
|
69
|
+
timer = setInterval(() => {
|
|
70
|
+
if (refreshPending) return;
|
|
71
|
+
refreshPending = true;
|
|
72
|
+
void catalogHostAuthority(agentDir, current?.ctx.cwd ?? cwd, socketPath)
|
|
73
|
+
.catch(() => { if (epoch === admittedEpoch) closeCatalogHost(); })
|
|
74
|
+
.finally(() => { refreshPending = false; });
|
|
75
|
+
}, 500);
|
|
76
|
+
timer.unref();
|
|
77
|
+
}
|
|
62
78
|
return authority;
|
|
63
79
|
} catch (error) { client.close(); rmSync(path, { force: true }); rmSync(staging, { force: true }); throw error; }
|
|
64
80
|
}
|
|
@@ -32,7 +32,7 @@ function preferences(id: string, value: unknown): CatalogPreference {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
function snapshot(values: CatalogPreferences): { settings: CatalogPreferences; digest: string } {
|
|
35
|
-
const settings = Object.freeze(Object.fromEntries(Object.keys(values).sort().map(id => [id, values[id]])));
|
|
35
|
+
const settings = Object.freeze(Object.fromEntries(Object.keys(values).sort().map(id => [id, values[id]!])));
|
|
36
36
|
return { settings, digest: createHash("sha256").update(JSON.stringify(settings)).digest("hex") };
|
|
37
37
|
}
|
|
38
38
|
|
|
@@ -99,7 +99,41 @@ export function inspectCatalog(snapshot, id) {
|
|
|
99
99
|
snapshotDigest: snapshot.digest,
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
|
+
export const catalogResolutionMetrics = { assemblies: 0, hits: 0 };
|
|
103
|
+
const resolutions = new Map();
|
|
104
|
+
function resolveCached(snapshot, id, compute) {
|
|
105
|
+
const entry = findEntry(snapshot, id);
|
|
106
|
+
if (!entry)
|
|
107
|
+
return compute();
|
|
108
|
+
const roleId = entry.definition?.kind === "agent" ? entry.definition.roleId : undefined;
|
|
109
|
+
const role = roleId ? findEntry(snapshot, roleId) : undefined;
|
|
110
|
+
// Only the selected definition, its base role and their own preferences contribute.
|
|
111
|
+
const key = JSON.stringify([{ ...entry, unused: undefined }, role && { ...role, unused: undefined }, snapshot.savedSettings?.[id], roleId && snapshot.savedSettings?.[roleId],
|
|
112
|
+
snapshot.sessionSettings?.[id], roleId && snapshot.sessionSettings?.[roleId]]);
|
|
113
|
+
const cached = resolutions.get(key);
|
|
114
|
+
if (cached) {
|
|
115
|
+
catalogResolutionMetrics.hits++;
|
|
116
|
+
const result = structuredClone(cached);
|
|
117
|
+
result.entry = entry;
|
|
118
|
+
if (result.effective)
|
|
119
|
+
result.effective.snapshotDigest = snapshot.digest;
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
catalogResolutionMetrics.assemblies++;
|
|
123
|
+
const result = compute();
|
|
124
|
+
if (new TextEncoder().encode(key).byteLength <= 64 * 1024)
|
|
125
|
+
resolutions.set(key, structuredClone(result));
|
|
126
|
+
if (resolutions.size > 512)
|
|
127
|
+
resolutions.delete(resolutions.keys().next().value);
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
102
130
|
function resolveRole(snapshot, roleId) {
|
|
131
|
+
return resolveCached(snapshot, roleId, () => resolveRoleUncached(snapshot, roleId));
|
|
132
|
+
}
|
|
133
|
+
function resolveAgent(snapshot, agentId) {
|
|
134
|
+
return resolveCached(snapshot, agentId, () => resolveAgentUncached(snapshot, agentId));
|
|
135
|
+
}
|
|
136
|
+
function resolveRoleUncached(snapshot, roleId) {
|
|
103
137
|
const entry = findEntry(snapshot, roleId);
|
|
104
138
|
if (!entry || entry.kind !== "role") {
|
|
105
139
|
return {
|
|
@@ -142,7 +176,7 @@ function resolveRole(snapshot, roleId) {
|
|
|
142
176
|
},
|
|
143
177
|
};
|
|
144
178
|
}
|
|
145
|
-
function
|
|
179
|
+
function resolveAgentUncached(snapshot, agentId) {
|
|
146
180
|
const entry = findEntry(snapshot, agentId);
|
|
147
181
|
if (!entry || entry.kind !== "agent") {
|
|
148
182
|
return {
|
|
@@ -218,7 +218,41 @@ export function inspectCatalog(snapshot: CatalogSnapshot, id: string): CatalogIn
|
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
+
export const catalogResolutionMetrics = { assemblies: 0, hits: 0 };
|
|
222
|
+
const resolutions = new Map<string, Resolution>();
|
|
223
|
+
|
|
224
|
+
function resolveCached(snapshot: CatalogSnapshot, id: string, compute: () => Resolution): Resolution {
|
|
225
|
+
const entry = findEntry(snapshot, id);
|
|
226
|
+
if (!entry) return compute();
|
|
227
|
+
const roleId = entry.definition?.kind === "agent" ? entry.definition.roleId : undefined;
|
|
228
|
+
const role = roleId ? findEntry(snapshot, roleId) : undefined;
|
|
229
|
+
// Only the selected definition, its base role and their own preferences contribute.
|
|
230
|
+
const key = JSON.stringify([{ ...entry, unused: undefined }, role && { ...role, unused: undefined }, snapshot.savedSettings?.[id], roleId && snapshot.savedSettings?.[roleId],
|
|
231
|
+
snapshot.sessionSettings?.[id], roleId && snapshot.sessionSettings?.[roleId]]);
|
|
232
|
+
const cached = resolutions.get(key);
|
|
233
|
+
if (cached) {
|
|
234
|
+
catalogResolutionMetrics.hits++;
|
|
235
|
+
const result = structuredClone(cached);
|
|
236
|
+
result.entry = entry;
|
|
237
|
+
if (result.effective) result.effective.snapshotDigest = snapshot.digest;
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
catalogResolutionMetrics.assemblies++;
|
|
241
|
+
const result = compute();
|
|
242
|
+
if (new TextEncoder().encode(key).byteLength <= 64 * 1024) resolutions.set(key, structuredClone(result));
|
|
243
|
+
if (resolutions.size > 512) resolutions.delete(resolutions.keys().next().value!);
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
|
|
221
247
|
function resolveRole(snapshot: CatalogSnapshot, roleId: string): Resolution {
|
|
248
|
+
return resolveCached(snapshot, roleId, () => resolveRoleUncached(snapshot, roleId));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function resolveAgent(snapshot: CatalogSnapshot, agentId: string): Resolution {
|
|
252
|
+
return resolveCached(snapshot, agentId, () => resolveAgentUncached(snapshot, agentId));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function resolveRoleUncached(snapshot: CatalogSnapshot, roleId: string): Resolution {
|
|
222
256
|
const entry = findEntry(snapshot, roleId);
|
|
223
257
|
if (!entry || entry.kind !== "role") {
|
|
224
258
|
return {
|
|
@@ -268,7 +302,7 @@ function resolveRole(snapshot: CatalogSnapshot, roleId: string): Resolution {
|
|
|
268
302
|
};
|
|
269
303
|
}
|
|
270
304
|
|
|
271
|
-
function
|
|
305
|
+
function resolveAgentUncached(snapshot: CatalogSnapshot, agentId: string): Resolution {
|
|
272
306
|
const entry = findEntry(snapshot, agentId);
|
|
273
307
|
if (!entry || entry.kind !== "agent") {
|
|
274
308
|
return {
|