@blackbelt-technology/pi-agent-dashboard 0.11.0 → 0.12.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/AGENTS.md +2 -0
- package/README.md +17 -12
- package/docs/architecture.md +973 -224
- package/package.json +29 -18
- package/packages/extension/package.json +16 -9
- package/packages/extension/src/bridge-context.ts +6 -0
- package/packages/extension/src/bridge-handlers/dispatch.ts +54 -69
- package/packages/extension/src/bridge-handlers/forwarders.ts +19 -42
- package/packages/extension/src/bridge-handlers/instance.ts +21 -12
- package/packages/extension/src/bridge-handlers/lifecycle.ts +84 -40
- package/packages/extension/src/bridge-shutdown.ts +44 -0
- package/packages/extension/src/bridge.ts +272 -260
- package/packages/extension/src/command-handler.ts +26 -31
- package/packages/extension/src/connection.ts +123 -31
- package/packages/extension/src/credential-guard.ts +300 -0
- package/packages/extension/src/custom-entry-forward.ts +27 -18
- package/packages/extension/src/default-model-apply.ts +83 -0
- package/packages/extension/src/flow-event-wiring.ts +19 -95
- package/packages/extension/src/followup-drain.ts +19 -5
- package/packages/extension/src/path-gate/decide.ts +68 -5
- package/packages/extension/src/path-gate/handler.ts +16 -8
- package/packages/extension/src/path-gate/index.ts +2 -2
- package/packages/extension/src/path-gate/roots.ts +15 -3
- package/packages/extension/src/path-gate/suppression.ts +3 -0
- package/packages/extension/src/pi-env.d.ts +8 -1
- package/packages/extension/src/pi-retry-settings.ts +11 -2
- package/packages/extension/src/plugin-event-forward-registry.ts +90 -14
- package/packages/extension/src/preview-path-link.ts +151 -0
- package/packages/extension/src/project-init/scaffold.ts +98 -35
- package/packages/extension/src/project-trust.ts +24 -15
- package/packages/extension/src/provider-model-cache.ts +118 -0
- package/packages/extension/src/provider-register.ts +428 -28
- package/packages/extension/src/retry-tracker.ts +90 -7
- package/packages/extension/src/role-manager.ts +4 -2
- package/packages/extension/src/role-model-tools.ts +21 -4
- package/packages/extension/src/server-auto-start.ts +68 -2
- package/packages/extension/src/server-launcher.ts +26 -8
- package/packages/extension/src/session-sync.ts +11 -12
- package/packages/extension/src/spawn-new-session.ts +22 -0
- package/packages/extension/src/steer-buffer.ts +164 -0
- package/packages/extension/src/stream-forward-buffer.ts +64 -15
- package/packages/extension/src/sudo-askpass/config-reader.ts +32 -0
- package/packages/extension/src/sudo-askpass/controller.ts +405 -0
- package/packages/extension/src/sudo-askpass/helper-script.ts +69 -0
- package/packages/extension/src/sudo-askpass/in-flight.ts +79 -0
- package/packages/extension/src/sudo-askpass/index.ts +11 -0
- package/packages/extension/src/sudo-askpass/install.ts +143 -0
- package/packages/extension/src/sudo-askpass/password-cache.ts +118 -0
- package/packages/extension/src/sudo-askpass/prompt-text.ts +55 -0
- package/packages/extension/src/sudo-askpass/protocol.ts +18 -0
- package/packages/extension/src/sudo-askpass/sudo-mask-registry.ts +52 -0
- package/packages/extension/src/terminal-reload.ts +19 -5
- package/packages/extension/src/tool-update-filter-registry.ts +210 -0
- package/packages/extension/src/vault-redaction.ts +4 -3
- package/packages/server/package.json +17 -14
- package/packages/server/src/access/access-denials.ts +8 -0
- package/packages/server/src/access/access-grants.ts +244 -58
- package/packages/server/src/access/access-plane.ts +7 -0
- package/packages/server/src/access/containment-gate.ts +66 -21
- package/packages/server/src/access/denial-hold.ts +3 -0
- package/packages/server/src/access/grant-coordinator.ts +17 -1
- package/packages/server/src/access/named-grant.ts +3 -0
- package/packages/server/src/access/pending-grant-registry.ts +32 -5
- package/packages/server/src/access/pi-trust-mirror-service.ts +7 -1
- package/packages/server/src/access/planes.ts +1 -0
- package/packages/server/src/access/verified-read.ts +24 -7
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +10 -2
- package/packages/server/src/acp-bridge/server-requests.ts +10 -4
- package/packages/server/src/agent-events/agent-event-hub.ts +1 -1
- package/packages/server/src/auth/audit-log.ts +28 -0
- package/packages/server/src/auth/auth-plugin.ts +188 -15
- package/packages/server/src/auth/auth.ts +80 -0
- package/packages/server/src/auth/bridge-ticket-eligibility.ts +53 -10
- package/packages/server/src/auth/cors-origin.ts +14 -1
- package/packages/server/src/auth/credential-envelope.ts +233 -0
- package/packages/server/src/auth/credential-key-worker.d.mts +16 -0
- package/packages/server/src/auth/credential-key-worker.mjs +108 -0
- package/packages/server/src/auth/credential-key.ts +479 -0
- package/packages/server/src/auth/credential-migration.ts +162 -0
- package/packages/server/src/auth/csp-report-paths.ts +0 -5
- package/packages/server/src/auth/csp.ts +74 -36
- package/packages/server/src/auth/device-proof.ts +202 -0
- package/packages/server/src/auth/duplicate-cookie-gate.ts +271 -0
- package/packages/server/src/auth/host-admission.ts +7 -0
- package/packages/server/src/auth/host-gate.ts +13 -2
- package/packages/server/src/auth/localhost-guard.ts +50 -23
- package/packages/server/src/auth/locked-json-file.ts +68 -6
- package/packages/server/src/auth/models-json-store.ts +269 -0
- package/packages/server/src/auth/mutation-origin-gate.ts +5 -2
- package/packages/server/src/auth/node-guard.ts +36 -49
- package/packages/server/src/auth/plugin-credential-store.ts +81 -10
- package/packages/server/src/auth/provider-auth-storage.ts +208 -11
- package/packages/server/src/auth/provider-block-checks.ts +321 -0
- package/packages/server/src/auth/provider-host-detect.ts +151 -0
- package/packages/server/src/auth/provider-key-file.ts +71 -0
- package/packages/server/src/auth/provider-readiness.ts +91 -0
- package/packages/server/src/auth/route-tier-gate.ts +3 -2
- package/packages/server/src/auth/vertex-key-check.ts +140 -0
- package/packages/server/src/auth/ws-ticket.ts +1 -1
- package/packages/server/src/browse.ts +73 -20
- package/packages/server/src/browser-handlers/acp-guard.ts +1 -4
- package/packages/server/src/browser-handlers/directory-handler.ts +11 -24
- package/packages/server/src/browser-handlers/session-action-handler.ts +158 -62
- package/packages/server/src/browser-handlers/spawn-choice.ts +138 -1
- package/packages/server/src/browser-handlers/subscription-handler.ts +46 -1
- package/packages/server/src/build-plugin-send-prompt.ts +18 -0
- package/packages/server/src/cli/credentials-command.ts +555 -0
- package/packages/server/src/cli.ts +21 -16
- package/packages/server/src/config-api.ts +34 -38
- package/packages/server/src/config-snapshot.ts +149 -0
- package/packages/server/src/directory-service.ts +33 -14
- package/packages/server/src/embed-lifecycle/idle-reaper.ts +13 -3
- package/packages/server/src/event-wiring.ts +181 -101
- package/packages/server/src/git-worktree/ensure-worktree.ts +185 -0
- package/packages/server/src/git-worktree/git-operations.ts +1 -1
- package/packages/server/src/git-worktree/repo-lock.ts +42 -0
- package/packages/server/src/git-worktree/start-worktree-init.ts +151 -0
- package/packages/server/src/git-worktree/worktree-init.ts +5 -1
- package/packages/server/src/identity/http-road-classification.ts +5 -1
- package/packages/server/src/identity/identity-floor.ts +5 -2
- package/packages/server/src/identity/ws-message-scope.ts +10 -3
- package/packages/server/src/identity/ws-road-classification.ts +2 -1
- package/packages/server/src/lib/browse-roots.ts +13 -2
- package/packages/server/src/lib/content-disposition.ts +20 -0
- package/packages/server/src/lib/dashboard-state-guard.ts +304 -21
- package/packages/server/src/lib/eml.ts +2 -2
- package/packages/server/src/lib/frame-policy-probe.ts +308 -0
- package/packages/server/src/lib/frame-policy-test-seam.ts +99 -0
- package/packages/server/src/lib/frame-policy-verdict.ts +125 -0
- package/packages/server/src/lib/grep.ts +4 -1
- package/packages/server/src/lib/index-html.ts +2 -2
- package/packages/server/src/lib/mention-fuzzy-index.ts +263 -0
- package/packages/server/src/lib/office-preview.ts +47 -10
- package/packages/server/src/lib/path-containment.ts +45 -12
- package/packages/server/src/lib/resolve-file-mention.ts +230 -38
- package/packages/server/src/lib/resolve-mentions-batch.ts +143 -0
- package/packages/server/src/lib/writable-handler-target.ts +4 -0
- package/packages/server/src/lib/writable-md-target.ts +22 -0
- package/packages/server/src/lifecycle/boot-parent-liveness.ts +10 -25
- package/packages/server/src/lifecycle/credential-exit-barrier.ts +207 -0
- package/packages/server/src/lifecycle/exit-signal.ts +43 -0
- package/packages/server/src/live-server/capability-redact.ts +21 -5
- package/packages/server/src/live-server/live-capability.ts +416 -52
- package/packages/server/src/live-server/live-csp-report.ts +1 -1
- package/packages/server/src/live-server/live-csp.ts +64 -16
- package/packages/server/src/live-server/live-log.ts +5 -1
- package/packages/server/src/live-server/live-server-manager.ts +11 -0
- package/packages/server/src/live-server/live-server-proxy.ts +268 -52
- package/packages/server/src/live-server/live-static.ts +4 -1
- package/packages/server/src/live-server/preview-dispatch.ts +286 -0
- package/packages/server/src/live-server/preview-observe.ts +53 -0
- package/packages/server/src/live-server/preview-origin.ts +182 -0
- package/packages/server/src/live-server/preview-path-bridge.ts +70 -0
- package/packages/server/src/live-server/preview-path-store.ts +420 -0
- package/packages/server/src/live-server/preview-path.ts +40 -0
- package/packages/server/src/live-server/preview-pool.ts +385 -0
- package/packages/server/src/live-server/preview-preflight.ts +41 -0
- package/packages/server/src/live-server/preview-reserve.ts +44 -0
- package/packages/server/src/live-server/preview-tier.ts +217 -0
- package/packages/server/src/live-server/preview-viewers.ts +54 -0
- package/packages/server/src/live-server/preview-ws-registry.ts +43 -0
- package/packages/server/src/model-proxy/custom-provider-discovery.ts +4 -2
- package/packages/server/src/model-proxy/internal-auth-storage.ts +5 -1
- package/packages/server/src/model-proxy/internal-registry.ts +283 -21
- package/packages/server/src/model-proxy/models-block-apply.ts +291 -0
- package/packages/server/src/model-proxy/pi-models-validator.ts +164 -0
- package/packages/server/src/model-proxy/registry-singleton.ts +44 -9
- package/packages/server/src/model-proxy/server-model-runtime.ts +17 -0
- package/packages/server/src/openspec/readiness.ts +1 -1
- package/packages/server/src/package/provider-probe.ts +194 -5
- package/packages/server/src/pairing/browser-gateway.ts +140 -42
- package/packages/server/src/pairing/device-key-intake.ts +120 -0
- package/packages/server/src/pairing/paired-devices.ts +370 -8
- package/packages/server/src/pairing/pairing.ts +69 -7
- package/packages/server/src/pairing/{subagent-resync-routing.ts → resync-requester-registry.ts} +6 -10
- package/packages/server/src/pairing/ws-tier-gate.ts +74 -0
- package/packages/server/src/pending/pending-attach-registry.ts +15 -0
- package/packages/server/src/pending/pending-initial-prompt-registry.ts +15 -0
- package/packages/server/src/persistence/memory-event-store.ts +251 -296
- package/packages/server/src/persistence/preferences-store.ts +52 -2
- package/packages/server/src/pi/bridge-contention.ts +84 -2
- package/packages/server/src/pi/host-project-trust.ts +15 -9
- package/packages/server/src/pi/pi-gateway.ts +208 -21
- package/packages/server/src/pi/provisional-registration.ts +4 -1
- package/packages/server/src/pi/resource-toggle-trust.ts +17 -6
- package/packages/server/src/pi-agent-settings.ts +95 -301
- package/packages/server/src/pi-default-remirror.ts +89 -0
- package/packages/server/src/plugin-append-event.ts +35 -0
- package/packages/server/src/plugin-entry-stats.ts +61 -0
- package/packages/server/src/plugin-local-request.ts +207 -0
- package/packages/server/src/plugin-ownership.ts +111 -0
- package/packages/server/src/plugin-policy-registry.ts +297 -0
- package/packages/server/src/plugin-request-lane.ts +92 -16
- package/packages/server/src/preferences-relay.ts +20 -0
- package/packages/server/src/project-seed.ts +347 -0
- package/packages/server/src/push/push-transports/webhook-url.ts +8 -5
- package/packages/server/src/questions/question-store.ts +3 -2
- package/packages/server/src/routes/access-routes.ts +23 -6
- package/packages/server/src/routes/acme-routes.ts +14 -4
- package/packages/server/src/routes/agent-event-stream-routes.ts +1 -1
- package/packages/server/src/routes/file-routes.ts +327 -86
- package/packages/server/src/routes/file-write-binary-routes.ts +20 -4
- package/packages/server/src/routes/frame-policy-routes.ts +215 -0
- package/packages/server/src/routes/git-routes.ts +29 -78
- package/packages/server/src/routes/grep-routes.ts +26 -0
- package/packages/server/src/routes/live-server-routes.ts +198 -4
- package/packages/server/src/routes/models-introspection-routes.ts +1 -1
- package/packages/server/src/routes/openspec-routes.ts +17 -3
- package/packages/server/src/routes/pairing-routes.ts +200 -11
- package/packages/server/src/routes/pi-settings-routes.ts +2 -0
- package/packages/server/src/routes/preferences-auto-name-routes.ts +5 -2
- package/packages/server/src/routes/preferences-claude-compat-routes.ts +49 -0
- package/packages/server/src/routes/provider-auth-routes.ts +167 -9
- package/packages/server/src/routes/provider-override-routes.ts +158 -0
- package/packages/server/src/routes/provider-routes.ts +348 -86
- package/packages/server/src/routes/session-routes.ts +25 -22
- package/packages/server/src/routes/system-routes.ts +87 -39
- package/packages/server/src/rpc-keeper/keeper-manager.ts +196 -25
- package/packages/server/src/rpc-keeper/keeper.cjs +37 -23
- package/packages/server/src/runtime-overlay/extension-reload.ts +16 -10
- package/packages/server/src/runtime-overlay/runtime-stager.ts +4 -2
- package/packages/server/src/server.ts +512 -175
- package/packages/server/src/services/cli-service.ts +59 -10
- package/packages/server/src/services/secrets-resolver.ts +46 -30
- package/packages/server/src/session/custom-event-group-annotation.ts +3 -3
- package/packages/server/src/session/custom-event-group-resolver.ts +10 -5
- package/packages/server/src/session/death-reason.ts +11 -7
- package/packages/server/src/session/event-status-extraction.ts +5 -1
- package/packages/server/src/session/memory-session-manager.ts +24 -2
- package/packages/server/src/session/open-tool-calls.ts +81 -47
- package/packages/server/src/session/pid-target.ts +127 -0
- package/packages/server/src/session/replay-compaction.ts +18 -5
- package/packages/server/src/session/seed-discovered-history.ts +85 -0
- package/packages/server/src/session/server-pid-context.ts +14 -0
- package/packages/server/src/session/session-api.ts +78 -29
- package/packages/server/src/session/session-archive.ts +7 -0
- package/packages/server/src/session/session-bootstrap.ts +9 -54
- package/packages/server/src/session/session-load-worker-pool.ts +11 -1
- package/packages/server/src/session/session-load-worker.ts +22 -3
- package/packages/server/src/spawn-process/abort-spawned-run.ts +28 -9
- package/packages/server/src/spawn-process/acp-spawn.ts +8 -2
- package/packages/server/src/spawn-process/headless-pid-registry.ts +383 -72
- package/packages/server/src/spawn-process/link-register.ts +42 -0
- package/packages/server/src/spawn-process/plugin-spawn-choice.ts +48 -0
- package/packages/server/src/spawn-process/plugin-spawn-worktree.ts +290 -0
- package/packages/server/src/spawn-process/process-classifier.ts +8 -3
- package/packages/server/src/spawn-process/process-manager.ts +61 -6
- package/packages/server/src/spawn-process/restored-identity.ts +112 -0
- package/packages/server/src/spawn-process/session-terminator.ts +179 -18
- package/packages/server/src/spawn-process/spawn-env-contributors.ts +2 -1
- package/packages/server/src/spawn-process/spawn-failure-log.ts +9 -0
- package/packages/server/src/spawn-process/spawn-register-watchdog.ts +44 -19
- package/packages/server/src/sudo-credential/relay.ts +337 -0
- package/packages/server/src/terminal/terminal-gateway.ts +10 -0
- package/packages/server/src/tls/sources/acme/credentials.ts +3 -2
- package/packages/server/src/tls/sources/acme/runtime.ts +2 -1
- package/packages/server/src/vault/vault-delivery-hub.ts +17 -1
- package/packages/server/src/vault/vault-routes.ts +9 -2
- package/packages/server/src/vault/vault-store.ts +87 -24
- package/packages/server/vendor/xlsx/LICENSE +201 -0
- package/packages/server/vendor/xlsx/PROVENANCE.json +13 -0
- package/packages/server/vendor/xlsx/xlsx.d.mts +1065 -0
- package/packages/server/vendor/xlsx/xlsx.mjs +28228 -0
- package/packages/shared/package.json +4 -2
- package/packages/shared/src/agent-credential-files.ts +32 -0
- package/packages/shared/src/browser-protocol.ts +137 -57
- package/packages/shared/src/canvas-detect.ts +1 -1
- package/packages/shared/src/canvas-types.ts +2 -2
- package/packages/shared/src/config.ts +73 -136
- package/packages/shared/src/containment-guard.ts +14 -4
- package/packages/shared/src/credential-encryption.ts +87 -0
- package/packages/shared/src/custom-event-groups.ts +2 -0
- package/packages/shared/src/dashboard-plugin/claude-compat.ts +159 -0
- package/packages/shared/src/dashboard-plugin/host-api.ts +1 -1
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +46 -0
- package/packages/shared/src/dashboard-plugin/plugin-status.ts +4 -3
- package/packages/shared/src/dashboard-plugin/replay-entry-types.ts +46 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +2 -0
- package/packages/shared/src/dashboard-plugin/slot-types.ts +1 -1
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +11 -3
- package/packages/shared/src/device-key-proof.ts +309 -0
- package/packages/shared/src/event-forward-declaration.ts +85 -12
- package/packages/shared/src/forbidden-subjects.ts +126 -14
- package/packages/shared/src/heap-limits.ts +0 -30
- package/packages/shared/src/host-admission.ts +2 -1
- package/packages/shared/src/index.ts +7 -6
- package/packages/shared/src/live-server.ts +20 -0
- package/packages/shared/src/mcp/verb-tiers.generated.ts +1 -3
- package/packages/shared/src/models-json-reader.ts +41 -0
- package/packages/shared/src/network-denial-signal.ts +15 -0
- package/packages/shared/src/node-version.ts +63 -60
- package/packages/shared/src/pairing-knock-client.ts +92 -8
- package/packages/shared/src/pi-agent-settings-lock.ts +311 -0
- package/packages/shared/src/pi-path-compare.ts +31 -0
- package/packages/shared/src/pi-settings-schema.ts +52 -9
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +16 -4
- package/packages/shared/src/pi-trust-mirror-types.ts +2 -0
- package/packages/shared/src/pi-trust-mirror.ts +18 -8
- package/packages/shared/src/pi-trust-resource-digest.ts +18 -12
- package/packages/shared/src/pid-namespace.ts +86 -0
- package/packages/shared/src/platform/native-ts-register.mjs +1 -1
- package/packages/shared/src/platform/process.ts +404 -1
- package/packages/shared/src/platform/resolve-pi-shell.ts +38 -9
- package/packages/shared/src/platform/spawn-runtime.ts +193 -7
- package/packages/shared/src/preview-egress.ts +56 -2
- package/packages/shared/src/preview-origins.ts +398 -0
- package/packages/shared/src/prompt-answer.ts +9 -5
- package/packages/shared/src/protocol.ts +147 -85
- package/packages/shared/src/provider-env-catalog.ts +413 -0
- package/packages/shared/src/provider-overrides.ts +112 -0
- package/packages/shared/src/question-types.ts +6 -0
- package/packages/shared/src/recommended-extensions.ts +79 -26
- package/packages/shared/src/renderer-by-ext.ts +5 -2
- package/packages/shared/src/rest-api.ts +93 -3
- package/packages/shared/src/role-schema.ts +24 -0
- package/packages/shared/src/route-tiers.ts +94 -3
- package/packages/shared/src/runtime-overlay/compat.ts +70 -6
- package/packages/shared/src/secret-patterns.ts +49 -0
- package/packages/shared/src/source-matching.ts +2 -2
- package/packages/shared/src/state-replay.ts +40 -24
- package/packages/shared/src/sudo-credential.ts +71 -0
- package/packages/shared/src/tool-path-resolve.ts +16 -1
- package/packages/shared/src/tool-registry/strategies.ts +1 -1
- package/packages/shared/src/tool-workdir.ts +56 -0
- package/packages/shared/src/trusted-types-mode.ts +72 -0
- package/packages/shared/src/types.ts +68 -109
- package/packages/shared/src/vertex-regions.ts +81 -0
- package/packages/shared/src/ws-message-tiers.ts +10 -3
- package/packages/extension/src/subagent-fanout-admission.ts +0 -234
- package/packages/extension/src/subagent-forward-sites.ts +0 -69
- package/packages/extension/src/subagent-frame-buffer.ts +0 -245
- package/packages/extension/src/subagent-frame-strip.ts +0 -109
- package/packages/extension/src/subagent-saturation.ts +0 -196
- package/packages/extension/src/subagent-tick-throttle.ts +0 -252
- package/packages/shared/src/anthropic-messages-source.ts +0 -8
- package/packages/shared/src/dashboard-plugin/anthropic-peer-names.ts +0 -9
- package/packages/shared/src/dashboard-plugin/subagent-types.ts +0 -219
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Subagent fan-out admission — the pure decision function plus the stateful
|
|
3
|
-
* gate the bridge wires onto `tool_call` / `tool_execution_end`.
|
|
4
|
-
*
|
|
5
|
-
* The capability: keep a parent session alive through a wide `Agent` fan-out on
|
|
6
|
-
* a loaded host by bounding how many children are in flight at once, refusing
|
|
7
|
-
* the excess visibly and terminally instead of letting the parent stall and be
|
|
8
|
-
* reaped. See change: bound-subagent-fanout-under-host-pressure.
|
|
9
|
-
*
|
|
10
|
-
* Three properties are load-bearing and easy to "simplify" away:
|
|
11
|
-
*
|
|
12
|
-
* - **The bound is in-flight concurrency, not batch width.** The extension
|
|
13
|
-
* `tool_call` event carries only `{type, toolCallId, toolName, input}` — the
|
|
14
|
-
* assistant message is dropped before the extension sees it — so batch size
|
|
15
|
-
* and identity are not knowable at decision time. The count rises at
|
|
16
|
-
* ADMISSION, not execution: siblings preflight before any executes, so a
|
|
17
|
-
* count keyed on execution would read zero across a whole batch and admit it.
|
|
18
|
-
* - **Release on `tool_execution_end`, never `tool_result`.** An aborted call
|
|
19
|
-
* (Esc) finalizes with an error result and `tool_execution_end` but SKIPS the
|
|
20
|
-
* path that produces the extension's `tool_result`; a counter released on
|
|
21
|
-
* `tool_result` would leak a permit on every abort and eventually refuse all
|
|
22
|
-
* subagent work for the session.
|
|
23
|
-
* - **Never wait on a child.** Siblings are preflighted sequentially and only
|
|
24
|
-
* then executed concurrently, so awaiting a child deadlocks by construction.
|
|
25
|
-
* Admission is synchronous, reject-only; it never delays an admitted call.
|
|
26
|
-
*
|
|
27
|
-
* See change: bound-subagent-fanout-under-host-pressure (D2/D3/D4/D6/D8).
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import {
|
|
31
|
-
resolveMaxConcurrentSubagents,
|
|
32
|
-
parseSubagentSaturation,
|
|
33
|
-
} from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
34
|
-
import type { SaturationReading, SaturationThresholds } from "./subagent-saturation.js";
|
|
35
|
-
|
|
36
|
-
/** Resolved admission configuration. */
|
|
37
|
-
export interface AdmissionConfig {
|
|
38
|
-
/** Effective cap: `0` = disabled; `Infinity` = fail-open/uncapped; else ≥ 1. */
|
|
39
|
-
maxConcurrentSubagents: number;
|
|
40
|
-
thresholds: SaturationThresholds;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Resolve admission config from a raw/dashboard-shaped object. Malformed admits. */
|
|
44
|
-
export function resolveAdmissionConfig(
|
|
45
|
-
raw:
|
|
46
|
-
| {
|
|
47
|
-
maxConcurrentSubagents?: unknown;
|
|
48
|
-
subagentSaturation?: unknown;
|
|
49
|
-
}
|
|
50
|
-
| undefined,
|
|
51
|
-
): AdmissionConfig {
|
|
52
|
-
return {
|
|
53
|
-
maxConcurrentSubagents: resolveMaxConcurrentSubagents(raw?.maxConcurrentSubagents),
|
|
54
|
-
thresholds: parseSubagentSaturation(raw?.subagentSaturation) ?? {},
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export type AdmissionCause = "cap" | "saturation";
|
|
59
|
-
|
|
60
|
-
export type AdmissionDecision =
|
|
61
|
-
| { action: "admit" }
|
|
62
|
-
| { action: "refuse"; cause: AdmissionCause; reason: string };
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The effective cap for one decision. Disabled / malformed / non-finite → the
|
|
66
|
-
* uncapped sentinel. Saturation narrows to a SINGLE child, never to zero, so a
|
|
67
|
-
* saturated session still makes progress.
|
|
68
|
-
*/
|
|
69
|
-
export function effectiveCap(maxConcurrentSubagents: number, saturated: boolean): number {
|
|
70
|
-
if (!Number.isFinite(maxConcurrentSubagents) || maxConcurrentSubagents < 1) {
|
|
71
|
-
return Number.POSITIVE_INFINITY;
|
|
72
|
-
}
|
|
73
|
-
return saturated ? Math.min(maxConcurrentSubagents, 1) : maxConcurrentSubagents;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Model-facing refusal text: cause, in-flight count, and "re-issue after". */
|
|
77
|
-
export function refusalReason(cause: AdmissionCause, inFlight: number, cap: number): string {
|
|
78
|
-
const running = `${inFlight} subagent${inFlight === 1 ? "" : "s"} already running`;
|
|
79
|
-
const tail = "Re-issue after the running subagents finish.";
|
|
80
|
-
if (cause === "saturation") {
|
|
81
|
-
return `Refused by host admission: resource saturation (${running}). ${tail}`;
|
|
82
|
-
}
|
|
83
|
-
return `Refused by host admission: at the concurrency cap of ${cap} (${running}). ${tail}`;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* The decision. Pure and synchronous: in-flight count + config + saturation →
|
|
88
|
-
* admit or refuse. It consults no child state and awaits nothing.
|
|
89
|
-
*/
|
|
90
|
-
export function decideAdmission(input: {
|
|
91
|
-
inFlight: number;
|
|
92
|
-
config: AdmissionConfig;
|
|
93
|
-
saturated: boolean;
|
|
94
|
-
}): AdmissionDecision {
|
|
95
|
-
const cap = effectiveCap(input.config.maxConcurrentSubagents, input.saturated);
|
|
96
|
-
if (input.inFlight < cap) return { action: "admit" };
|
|
97
|
-
const cause: AdmissionCause = input.saturated ? "saturation" : "cap";
|
|
98
|
-
return { action: "refuse", cause, reason: refusalReason(cause, input.inFlight, cap) };
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface FanoutAdmissionCounters {
|
|
102
|
-
/** Calls admitted (a permit was taken). */
|
|
103
|
-
fanoutAdmitted: number;
|
|
104
|
-
/** Calls refused by the gate (any cause). */
|
|
105
|
-
fanoutRefused: number;
|
|
106
|
-
/** Refusals whose cause was resource saturation. */
|
|
107
|
-
fanoutSaturationRefused: number;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** A refusal, as written to the durable session record. */
|
|
111
|
-
export interface FanoutRefusalRecord {
|
|
112
|
-
toolCallId: string;
|
|
113
|
-
cause: AdmissionCause;
|
|
114
|
-
inFlight: number;
|
|
115
|
-
cap: number;
|
|
116
|
-
reason: string;
|
|
117
|
-
at: number;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export interface RefusalSaturationSource {
|
|
121
|
-
read(thresholds: SaturationThresholds): { saturated: boolean; reading: SaturationReading };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export interface FanoutAdmissionGateOptions {
|
|
125
|
-
/** Resolve the current config for each decision. */
|
|
126
|
-
resolveConfig: () => AdmissionConfig;
|
|
127
|
-
/** Synchronous saturation source (the sampler). */
|
|
128
|
-
saturation: RefusalSaturationSource;
|
|
129
|
-
/**
|
|
130
|
-
* Durable refusal recorder. Called ONLY on refusal (the admit path stays free
|
|
131
|
-
* of filesystem I/O). Its failure must never change the verdict.
|
|
132
|
-
*/
|
|
133
|
-
recordRefusal?: (record: FanoutRefusalRecord) => void;
|
|
134
|
-
/** Injectable clock for the durable record's `at`. */
|
|
135
|
-
now?: () => number;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* The stateful gate: owns the admitted-id set (permits), the counters, and the
|
|
140
|
-
* fail-open wrapping. Every public method is synchronous and non-throwing.
|
|
141
|
-
*/
|
|
142
|
-
export class FanoutAdmissionGate {
|
|
143
|
-
private readonly admitted = new Set<string>();
|
|
144
|
-
private readonly resolveConfig: () => AdmissionConfig;
|
|
145
|
-
private readonly saturation: RefusalSaturationSource;
|
|
146
|
-
private readonly recordRefusal?: (record: FanoutRefusalRecord) => void;
|
|
147
|
-
private readonly now: () => number;
|
|
148
|
-
|
|
149
|
-
readonly counters: FanoutAdmissionCounters = {
|
|
150
|
-
fanoutAdmitted: 0,
|
|
151
|
-
fanoutRefused: 0,
|
|
152
|
-
fanoutSaturationRefused: 0,
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
constructor(opts: FanoutAdmissionGateOptions) {
|
|
156
|
-
this.resolveConfig = opts.resolveConfig;
|
|
157
|
-
this.saturation = opts.saturation;
|
|
158
|
-
this.recordRefusal = opts.recordRefusal;
|
|
159
|
-
this.now = opts.now ?? Date.now;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** Current in-flight children (permits held). */
|
|
163
|
-
get inFlight(): number {
|
|
164
|
-
return this.admitted.size;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Decide one `Agent` tool call. Returns `{block:true, reason}` to refuse, or
|
|
169
|
-
* `undefined` to admit unchanged. Any failure in the decision path admits.
|
|
170
|
-
*/
|
|
171
|
-
onToolCall(event: {
|
|
172
|
-
toolName?: unknown;
|
|
173
|
-
toolCallId?: unknown;
|
|
174
|
-
}): { block: true; reason: string } | undefined {
|
|
175
|
-
try {
|
|
176
|
-
if (!event || event.toolName !== "Agent" || typeof event.toolCallId !== "string") {
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
179
|
-
const config = this.resolveConfig();
|
|
180
|
-
const cap = config.maxConcurrentSubagents;
|
|
181
|
-
// Disabled / malformed / fail-open: exact no-op — no sampler read, no
|
|
182
|
-
// permit, no durable write, no delay.
|
|
183
|
-
if (!Number.isFinite(cap) || cap < 1) return undefined;
|
|
184
|
-
|
|
185
|
-
const { saturated } = this.saturation.read(config.thresholds);
|
|
186
|
-
const decision = decideAdmission({ inFlight: this.admitted.size, config, saturated });
|
|
187
|
-
|
|
188
|
-
if (decision.action === "admit") {
|
|
189
|
-
this.admitted.add(event.toolCallId);
|
|
190
|
-
this.counters.fanoutAdmitted += 1;
|
|
191
|
-
return undefined;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
this.counters.fanoutRefused += 1;
|
|
195
|
-
if (decision.cause === "saturation") this.counters.fanoutSaturationRefused += 1;
|
|
196
|
-
if (this.recordRefusal) {
|
|
197
|
-
try {
|
|
198
|
-
this.recordRefusal({
|
|
199
|
-
toolCallId: event.toolCallId,
|
|
200
|
-
cause: decision.cause,
|
|
201
|
-
inFlight: this.admitted.size,
|
|
202
|
-
// Record the EFFECTIVE cap (1 under saturation), matching the reason
|
|
203
|
-
// string — a post-mortem reader must not see "cap 2" for a refusal
|
|
204
|
-
// that actually happened at cap 1.
|
|
205
|
-
cap: effectiveCap(config.maxConcurrentSubagents, saturated),
|
|
206
|
-
reason: decision.reason,
|
|
207
|
-
at: this.now(),
|
|
208
|
-
});
|
|
209
|
-
} catch {
|
|
210
|
-
// Observability must never change the verdict.
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
return { block: true, reason: decision.reason };
|
|
214
|
-
} catch {
|
|
215
|
-
// Fail open: a throwing handler would make pi refuse every Agent call.
|
|
216
|
-
return undefined;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Release a permit on `tool_execution_end` — the signal pi emits on the
|
|
222
|
-
* normal, blocked AND aborted paths. Keyed to ids this gate ADMITTED, so a
|
|
223
|
-
* refused (or foreign) id returns nothing.
|
|
224
|
-
*/
|
|
225
|
-
onExecutionEnd(event: { toolCallId?: unknown }): void {
|
|
226
|
-
if (!event || typeof event.toolCallId !== "string") return;
|
|
227
|
-
this.admitted.delete(event.toolCallId);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/** Session change / shutdown: drop every held permit and the counters' basis. */
|
|
231
|
-
reset(): void {
|
|
232
|
-
this.admitted.clear();
|
|
233
|
-
}
|
|
234
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* subagent-forward-sites — the two subagent forward paths that call
|
|
3
|
-
* `sendEventForward` DIRECTLY, extracted from the bridge closure so the strip
|
|
4
|
-
* placement is testable.
|
|
5
|
-
*
|
|
6
|
-
* Placement is a decision, not a detail (D2). There is no single chokepoint:
|
|
7
|
-
* - a strip inside `sendEventForward` would strip the RESYNC REPLY, which is
|
|
8
|
-
* the one frame the whole pull model depends on staying fat;
|
|
9
|
-
* - a strip on the EventBus path alone would leak every frame drained by the
|
|
10
|
-
* buffered-frame flush.
|
|
11
|
-
*
|
|
12
|
-
* So the strip is applied here, per call site, and never in the transport.
|
|
13
|
-
*
|
|
14
|
-
* See change: reduce-subagent-details-payload.
|
|
15
|
-
*/
|
|
16
|
-
import type { SubagentFrameBuffer } from "./subagent-frame-buffer.js";
|
|
17
|
-
import { SubagentFrameBuffer as Buffer } from "./subagent-frame-buffer.js";
|
|
18
|
-
import { stripForForward } from "./subagent-frame-strip.js";
|
|
19
|
-
|
|
20
|
-
/** The bridge's `sendEventForward(channel, data)` transport hook. */
|
|
21
|
-
export type SendEventForward = (channel: string, data: Record<string, unknown>) => void;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Drain frames buffered while the bridge was not ready and forward them, each
|
|
25
|
-
* STRIPPED — they are intermediate frames of a still-running subagent, and the
|
|
26
|
-
* retained snapshot behind them stays fat for resync. Returns how many were
|
|
27
|
-
* drained. A send failure never stops the drain.
|
|
28
|
-
*/
|
|
29
|
-
export function flushBufferedSubagentFrames(
|
|
30
|
-
buffer: SubagentFrameBuffer,
|
|
31
|
-
send: SendEventForward,
|
|
32
|
-
): number {
|
|
33
|
-
const drained = buffer.drain();
|
|
34
|
-
for (const { channel, data } of drained) {
|
|
35
|
-
try {
|
|
36
|
-
send(channel, stripForForward(data, channel));
|
|
37
|
-
} catch {
|
|
38
|
-
/* keep flushing */
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return drained.length;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Answer a `subagent_resync_request` with the latest retained snapshot, sent
|
|
46
|
-
* UNSTRIPPED as a synthetic `subagents:started` frame. Returns the resolved
|
|
47
|
-
* `agentId`, or undefined when the reply was a no-op — the bridge was not
|
|
48
|
-
* serving (`canServe` false: retryable), or the agent is unknown / finished /
|
|
49
|
-
* evicted from the bounded snapshot map, in which case the client keeps its
|
|
50
|
-
* last rendered state rather than blanking (C3).
|
|
51
|
-
*/
|
|
52
|
-
export function serveSubagentResync(
|
|
53
|
-
buffer: SubagentFrameBuffer,
|
|
54
|
-
requestedId: string,
|
|
55
|
-
send: SendEventForward,
|
|
56
|
-
canServe: () => boolean,
|
|
57
|
-
requestId?: string,
|
|
58
|
-
reason?: "open" | "cadence",
|
|
59
|
-
): string | undefined {
|
|
60
|
-
if (!canServe()) return undefined;
|
|
61
|
-
const snap = buffer.resync(requestedId, reason);
|
|
62
|
-
if (!snap) return undefined;
|
|
63
|
-
// Echo the requester's correlation token so the server can deliver this reply
|
|
64
|
-
// to that one connection instead of every subscriber of the session. Copied,
|
|
65
|
-
// never assigned onto the retained snapshot.
|
|
66
|
-
// See change: reduce-subagent-details-payload (C5).
|
|
67
|
-
send("subagents:started", requestId ? { ...snap.data, __resyncRequestId: requestId } : snap.data);
|
|
68
|
-
return Buffer.agentIdOf(snap.data) ?? requestedId;
|
|
69
|
-
}
|
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SubagentFrameBuffer — makes the running-subagent timeline reconcilable
|
|
3
|
-
* instead of fire-and-forget.
|
|
4
|
-
*
|
|
5
|
-
* Background: the bridge's per-channel subscription
|
|
6
|
-
* (`registerEventBusForwarding` → `forwardBusEvent`) forwards subagent
|
|
7
|
-
* lifecycle frames (`subagents:created/started/completed/failed`) as
|
|
8
|
-
* `event_forward` messages, but only while `sessionReady && isActive()`.
|
|
9
|
-
* Frames emitted during a not-ready window (reconnect / discovery / `/reload`
|
|
10
|
-
* / bridge takeover) were silently dropped, so a running subagent's detail
|
|
11
|
-
* panel stayed empty or stale until the next tick — or until completion, when
|
|
12
|
-
* the durable reducer backfill kicks in. See change:
|
|
13
|
-
* fix-subagent-live-detail-reliability.
|
|
14
|
-
*
|
|
15
|
-
* This buffer closes that gap two ways:
|
|
16
|
-
* - D1 buffer-and-flush: while not ready, retain the latest frame per
|
|
17
|
-
* `agentId` (each frame carries a FULL snapshot, so latest supersedes
|
|
18
|
-
* older ones) in a bounded map, and drain it on the next re-register.
|
|
19
|
-
* - D2 resync: keep the latest snapshot of each RUNNING subagent so the
|
|
20
|
-
* client can pull current state after a gap that outlived the buffer
|
|
21
|
-
* (long disconnect) or when opening detail for a running subagent whose
|
|
22
|
-
* `entries[]` is empty. Finished subagents are dropped from the snapshot
|
|
23
|
-
* map — the durable completed-case backfill already covers them, so a
|
|
24
|
-
* resync for an unknown/finished agent is a no-op.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/** A subagent EventBus frame: the raw channel name plus its data payload. */
|
|
28
|
-
export interface SubagentFrame {
|
|
29
|
-
channel: string;
|
|
30
|
-
data: Record<string, unknown>;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** EventBus channels that carry subagent lifecycle frames. */
|
|
34
|
-
export const SUBAGENT_CHANNELS = new Set<string>([
|
|
35
|
-
"subagents:created",
|
|
36
|
-
"subagents:started",
|
|
37
|
-
"subagents:completed",
|
|
38
|
-
"subagents:failed",
|
|
39
|
-
]);
|
|
40
|
-
|
|
41
|
-
/** Channels that terminate a subagent run (drop it from the resync snapshots). */
|
|
42
|
-
export const TERMINAL_CHANNELS = new Set<string>(["subagents:completed", "subagents:failed"]);
|
|
43
|
-
|
|
44
|
-
export interface SubagentFrameStats {
|
|
45
|
-
/** Frames forwarded live (ready path). */
|
|
46
|
-
forwarded: number;
|
|
47
|
-
/** Frames buffered because the bridge was not ready. */
|
|
48
|
-
buffered: number;
|
|
49
|
-
/** Frames drained + forwarded on re-register. */
|
|
50
|
-
flushed: number;
|
|
51
|
-
/** Not-ready frames that could not be buffered (no `agentId`). */
|
|
52
|
-
droppedNoAgentId: number;
|
|
53
|
-
/** Frames/snapshots evicted because the 64-agent bound was exceeded. */
|
|
54
|
-
overflowEvicted: number;
|
|
55
|
-
/** Resync requests received. */
|
|
56
|
-
resyncRequests: number;
|
|
57
|
-
/** Resync requests answered with a snapshot. */
|
|
58
|
-
resyncServed: number;
|
|
59
|
-
/** Resync requests served via the fast `agentId` key. */
|
|
60
|
-
resyncByAgentId: number;
|
|
61
|
-
/** Resync requests served via the derived `agentSessionId` values-scan. */
|
|
62
|
-
resyncByAgentSessionId: number;
|
|
63
|
-
/** Resync requests for an unknown/finished agent (no-op). */
|
|
64
|
-
resyncNoop: number;
|
|
65
|
-
/**
|
|
66
|
-
* Resync requests issued by the open-inspector PULL LOOP rather than by a
|
|
67
|
-
* user opening a view. Sits beside the counters above so the D4 v1 cadence is
|
|
68
|
-
* provably not a new firehose: `resyncCadence / resyncRequests` is the share
|
|
69
|
-
* of the pull traffic the loop is responsible for.
|
|
70
|
-
* See change: reduce-subagent-details-payload (D6, task 9.4).
|
|
71
|
-
*/
|
|
72
|
-
resyncCadence: number;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export class SubagentFrameBuffer {
|
|
76
|
-
/** agentId → latest buffered frame (retained across a not-ready window). */
|
|
77
|
-
private readonly pending = new Map<string, SubagentFrame>();
|
|
78
|
-
/** agentId → latest snapshot of a still-running subagent (for resync). */
|
|
79
|
-
private readonly snapshots = new Map<string, SubagentFrame>();
|
|
80
|
-
|
|
81
|
-
readonly stats: SubagentFrameStats = {
|
|
82
|
-
forwarded: 0,
|
|
83
|
-
buffered: 0,
|
|
84
|
-
flushed: 0,
|
|
85
|
-
droppedNoAgentId: 0,
|
|
86
|
-
overflowEvicted: 0,
|
|
87
|
-
resyncRequests: 0,
|
|
88
|
-
resyncServed: 0,
|
|
89
|
-
resyncByAgentId: 0,
|
|
90
|
-
resyncByAgentSessionId: 0,
|
|
91
|
-
resyncNoop: 0,
|
|
92
|
-
resyncCadence: 0,
|
|
93
|
-
};
|
|
94
|
-
private readonly maxAgents: number;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @param maxAgents bound on distinct buffered agents (drop-oldest beyond it).
|
|
98
|
-
*
|
|
99
|
-
* The bound stays 64 now that resync is the ONLY mid-run timeline source
|
|
100
|
-
* (task 7.1). Raising it was considered and rejected: 64 concurrent RUNNING
|
|
101
|
-
* subagents in one session is already far past any observed workload, and a
|
|
102
|
-
* higher ceiling would trade a bounded, counted loss for unbounded retention
|
|
103
|
-
* on the bridge. The ceiling is instead made observable — `overflowEvicted`
|
|
104
|
-
* counts every eviction, and an evicted still-running agent answers with an
|
|
105
|
-
* explicit `resyncNoop` so the client keeps its last rendered state rather
|
|
106
|
-
* than blanking (C3). See change: reduce-subagent-details-payload.
|
|
107
|
-
*/
|
|
108
|
-
constructor(maxAgents = 64) {
|
|
109
|
-
this.maxAgents = maxAgents;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
static isSubagentChannel(channel: string): boolean {
|
|
113
|
-
return SUBAGENT_CHANNELS.has(channel);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
static agentIdOf(data: Record<string, unknown> | undefined): string | undefined {
|
|
117
|
-
return data && typeof data.id === "string" ? data.id : undefined;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* The runner session id (v7) a frame carries on its `details`, if any. The
|
|
122
|
-
* producer (>= 0.2.3) sets `agentSessionId` on `AgentDetails` = the frame
|
|
123
|
-
* `data.details` payload. Absent → undefined (older producer).
|
|
124
|
-
*/
|
|
125
|
-
static agentSessionIdOf(data: Record<string, unknown> | undefined): string | undefined {
|
|
126
|
-
const details = data?.details as Record<string, unknown> | undefined;
|
|
127
|
-
return details && typeof details.agentSessionId === "string" ? details.agentSessionId : undefined;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Evict the oldest-inserted keys from `map` until it fits `maxAgents`.
|
|
132
|
-
* Each eviction is real data loss, so it bumps the overflow counter.
|
|
133
|
-
*/
|
|
134
|
-
private evictToBound(map: Map<string, SubagentFrame>): void {
|
|
135
|
-
while (map.size > this.maxAgents) {
|
|
136
|
-
const oldest = map.keys().next().value;
|
|
137
|
-
if (oldest === undefined) break;
|
|
138
|
-
map.delete(oldest);
|
|
139
|
-
this.stats.overflowEvicted += 1;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Update the running-subagent snapshot map. Called for every subagent frame
|
|
145
|
-
* regardless of ready state so a resync can always return current state.
|
|
146
|
-
* Bounded to `maxAgents` with the same latest-wins / drop-oldest policy as
|
|
147
|
-
* `pending`, so retained resync snapshots cannot grow unbounded.
|
|
148
|
-
*/
|
|
149
|
-
private track(channel: string, agentId: string, data: Record<string, unknown>): void {
|
|
150
|
-
if (TERMINAL_CHANNELS.has(channel)) {
|
|
151
|
-
this.snapshots.delete(agentId);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
// Re-insert to move this agent to the most-recent position, then bound.
|
|
155
|
-
this.snapshots.delete(agentId);
|
|
156
|
-
this.snapshots.set(agentId, { channel, data });
|
|
157
|
-
this.evictToBound(this.snapshots);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Record a subagent frame that IS being forwarded live. Updates the resync
|
|
162
|
-
* snapshot and the forwarded counter. Call on the ready path.
|
|
163
|
-
*/
|
|
164
|
-
markForwarded(channel: string, data: Record<string, unknown>): void {
|
|
165
|
-
const agentId = SubagentFrameBuffer.agentIdOf(data);
|
|
166
|
-
if (agentId) this.track(channel, agentId, data);
|
|
167
|
-
this.stats.forwarded += 1;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Buffer a subagent frame emitted while the bridge is NOT ready. Retains the
|
|
172
|
-
* latest frame per `agentId`; when the distinct-agent bound is exceeded,
|
|
173
|
-
* evicts the oldest-inserted agent. Returns true if buffered, false if it
|
|
174
|
-
* could not be buffered (no `agentId` on the frame).
|
|
175
|
-
*/
|
|
176
|
-
buffer(channel: string, data: Record<string, unknown>): boolean {
|
|
177
|
-
const agentId = SubagentFrameBuffer.agentIdOf(data);
|
|
178
|
-
if (!agentId) {
|
|
179
|
-
this.stats.droppedNoAgentId += 1;
|
|
180
|
-
return false;
|
|
181
|
-
}
|
|
182
|
-
this.track(channel, agentId, data);
|
|
183
|
-
// Re-insert to move this agent to the most-recent position (emission order).
|
|
184
|
-
this.pending.delete(agentId);
|
|
185
|
-
this.pending.set(agentId, { channel, data });
|
|
186
|
-
this.evictToBound(this.pending);
|
|
187
|
-
this.stats.buffered += 1;
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Drain the buffered frames in emission order (oldest-inserted agent first).
|
|
193
|
-
* Clears the buffer; the caller forwards each returned frame. The resync
|
|
194
|
-
* snapshots are intentionally NOT cleared — a subagent can still be running.
|
|
195
|
-
*/
|
|
196
|
-
drain(): SubagentFrame[] {
|
|
197
|
-
const out = [...this.pending.values()];
|
|
198
|
-
this.pending.clear();
|
|
199
|
-
this.stats.flushed += out.length;
|
|
200
|
-
return out;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Resync (D2): the latest retained snapshot for a running subagent, or
|
|
205
|
-
* undefined for an unknown/finished agent (caller replies with nothing).
|
|
206
|
-
*
|
|
207
|
-
* The incoming `id` may be EITHER the v4 `agentId` (fast-path key lookup) or
|
|
208
|
-
* the v7 runner `agentSessionId` carried on a frame's `details` (derived
|
|
209
|
-
* values-scan fallback). The mapping is a pure function of the already-bounded
|
|
210
|
-
* (≤ maxAgents) `snapshots` map — no separate alias index, no `finished` set —
|
|
211
|
-
* so it cannot leak or diverge. The scan is O(≤ maxAgents) on a rare,
|
|
212
|
-
* user-initiated cold path. Terminal/evicted runs retain no snapshot, so both
|
|
213
|
-
* ids resolve to nothing. See change: resolve-subagent-inspector-by-session-id (D3).
|
|
214
|
-
*/
|
|
215
|
-
resync(id: string, reason?: "open" | "cadence"): SubagentFrame | undefined {
|
|
216
|
-
this.stats.resyncRequests += 1;
|
|
217
|
-
if (reason === "cadence") this.stats.resyncCadence += 1;
|
|
218
|
-
const byAgentId = this.snapshots.get(id);
|
|
219
|
-
if (byAgentId) {
|
|
220
|
-
this.stats.resyncServed += 1;
|
|
221
|
-
this.stats.resyncByAgentId += 1;
|
|
222
|
-
return byAgentId;
|
|
223
|
-
}
|
|
224
|
-
for (const snap of this.snapshots.values()) {
|
|
225
|
-
if (SubagentFrameBuffer.agentSessionIdOf(snap.data) === id) {
|
|
226
|
-
this.stats.resyncServed += 1;
|
|
227
|
-
this.stats.resyncByAgentSessionId += 1;
|
|
228
|
-
return snap;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
this.stats.resyncNoop += 1;
|
|
232
|
-
return undefined;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/** Drop all retained state (session change / shutdown). */
|
|
236
|
-
reset(): void {
|
|
237
|
-
this.pending.clear();
|
|
238
|
-
this.snapshots.clear();
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/** Number of distinct agents currently buffered. */
|
|
242
|
-
get pendingSize(): number {
|
|
243
|
-
return this.pending.size;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* subagent-frame-strip — drop the cumulative `details.entries[]` timeline from
|
|
3
|
-
* frames describing a NON-terminal subagent, on the FORWARD path only.
|
|
4
|
-
*
|
|
5
|
-
* Why here (D2): the producer builds one `snapshotDetails()` object and hands
|
|
6
|
-
* it to BOTH carriers (the `subagents:*` EventBus frame and the pi-core
|
|
7
|
-
* `tool_execution_update`), so per-tick bytes grow linearly with run length on
|
|
8
|
-
* both wire segments and in every stored event. The bridge is downstream of
|
|
9
|
-
* every producer version that exists, is our code, and already retains the fat
|
|
10
|
-
* snapshot for resync — so stripping here needs no producer release, no wire
|
|
11
|
-
* key, no version negotiation.
|
|
12
|
-
*
|
|
13
|
-
* Two invariants this module exists to hold:
|
|
14
|
-
* - **Allowlist, never a negation.** `AgentStatus` also carries `"stopped"`
|
|
15
|
-
* (unemitted today); a `!terminal` test would silently strip it and lose
|
|
16
|
-
* that run's timeline forever. Only `queued`/`running` are stripped, and an
|
|
17
|
-
* absent/unknown status is never stripped.
|
|
18
|
-
* - **Clone, never mutate.** `SubagentFrameBuffer` retains frames BY
|
|
19
|
-
* REFERENCE, so an in-place strip would corrupt the very snapshot the pull
|
|
20
|
-
* path serves.
|
|
21
|
-
*
|
|
22
|
-
* NOT applied to terminal frames (the durable record behind `tool_execution_end`
|
|
23
|
-
* backfill) nor to resync replies (the pull model's own answer).
|
|
24
|
-
*
|
|
25
|
-
* See change: reduce-subagent-details-payload.
|
|
26
|
-
*/
|
|
27
|
-
import { TERMINAL_CHANNELS } from "./subagent-frame-buffer.js";
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Statuses whose frames are stripped. An ALLOWLIST on purpose — a new
|
|
31
|
-
* `AgentStatus` defaults to "keep the timeline", which is the safe direction.
|
|
32
|
-
*/
|
|
33
|
-
export const NON_TERMINAL_STATUSES: ReadonlySet<string> = new Set(["queued", "running"]);
|
|
34
|
-
|
|
35
|
-
/** The two places a subagent `details` object sits on a frame payload. */
|
|
36
|
-
function detailsPathOf(
|
|
37
|
-
data: Record<string, unknown>,
|
|
38
|
-
): { container: Record<string, unknown>; key: string } | undefined {
|
|
39
|
-
const pr = data.partialResult;
|
|
40
|
-
if (pr && typeof pr === "object" && !Array.isArray(pr)) {
|
|
41
|
-
const prObj = pr as Record<string, unknown>;
|
|
42
|
-
if (prObj.details && typeof prObj.details === "object") {
|
|
43
|
-
return { container: prObj, key: "partialResult" };
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (data.details && typeof data.details === "object" && !Array.isArray(data.details)) {
|
|
47
|
-
return { container: data, key: "details" };
|
|
48
|
-
}
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Return a frame payload with `details.entries` removed when the frame
|
|
54
|
-
* describes a `queued`/`running` subagent. Every other frame — terminal,
|
|
55
|
-
* status-less, timeline-less — is returned unchanged (same reference).
|
|
56
|
-
*
|
|
57
|
-
* Pure: the input is never mutated. Only the objects on the mutated path are
|
|
58
|
-
* copied; untouched sub-objects are shared.
|
|
59
|
-
*/
|
|
60
|
-
export function stripSubagentEntries(data: Record<string, unknown>): Record<string, unknown> {
|
|
61
|
-
const path = detailsPathOf(data);
|
|
62
|
-
if (!path) return data;
|
|
63
|
-
const isNested = path.key === "partialResult";
|
|
64
|
-
const details = (isNested ? path.container.details : data.details) as Record<string, unknown>;
|
|
65
|
-
if (!Array.isArray(details.entries)) return data;
|
|
66
|
-
if (typeof details.status !== "string" || !NON_TERMINAL_STATUSES.has(details.status)) return data;
|
|
67
|
-
|
|
68
|
-
const { entries: _dropped, ...thinDetails } = details;
|
|
69
|
-
if (isNested) {
|
|
70
|
-
return { ...data, partialResult: { ...path.container, details: thinDetails } };
|
|
71
|
-
}
|
|
72
|
-
return { ...data, details: thinDetails };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Rollback switch (D2), PARTIAL by design. `PI_DASHBOARD_SUBAGENT_STRIP=0`
|
|
77
|
-
* forwards frames unstripped, so the WIRE PAYLOAD returns to its pre-change
|
|
78
|
-
* shape. It does not revert the rest of the change: the `subagent_*` truncation
|
|
79
|
-
* gate, the resync cadence, requester-scoped delivery, and the additive
|
|
80
|
-
* `storeTrim` counters stay active — each is a bug fix or additive telemetry
|
|
81
|
-
* that does not depend on the strip. Read per call so a session does not have
|
|
82
|
-
* to restart to flip it.
|
|
83
|
-
*/
|
|
84
|
-
function isSubagentStripEnabled(): boolean {
|
|
85
|
-
return process.env.PI_DASHBOARD_SUBAGENT_STRIP !== "0";
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* The forward-path entry point: strip unless the rollback flag is off. Applied
|
|
90
|
-
* at an explicit allowlist of CALL SITES (bus forward, buffered-frame flush,
|
|
91
|
-
* the `tool_execution_update` carrier) rather than inside `sendEventForward` —
|
|
92
|
-
* the resync reply calls that directly for a RUNNING agent and MUST stay fat.
|
|
93
|
-
*
|
|
94
|
-
* `channel`, when known, is a SECOND independent terminal guard: a frame on
|
|
95
|
-
* `subagents:completed`/`subagents:failed` is never stripped whatever its
|
|
96
|
-
* `details.status` says. The status allowlist alone would trust the producer to
|
|
97
|
-
* have flipped `status` before emitting on a terminal channel; a producer that
|
|
98
|
-
* emitted its last `running` snapshot there would lose that timeline forever —
|
|
99
|
-
* the single highest-severity failure mode in this design. Both signals have to
|
|
100
|
-
* be wrong for that to happen now.
|
|
101
|
-
*/
|
|
102
|
-
export function stripForForward(
|
|
103
|
-
data: Record<string, unknown>,
|
|
104
|
-
channel?: string,
|
|
105
|
-
): Record<string, unknown> {
|
|
106
|
-
if (!isSubagentStripEnabled()) return data;
|
|
107
|
-
if (channel !== undefined && TERMINAL_CHANNELS.has(channel)) return data;
|
|
108
|
-
return stripSubagentEntries(data);
|
|
109
|
-
}
|