@blackbelt-technology/pi-agent-dashboard 0.10.0 → 0.11.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 -2
- package/README.md +47 -6
- package/docs/architecture.md +1369 -63
- package/package.json +14 -9
- package/packages/extension/package.json +5 -3
- package/packages/extension/src/bridge-handlers/dispatch.ts +57 -3
- package/packages/extension/src/bridge-handlers/instance.ts +10 -1
- package/packages/extension/src/bridge-handlers/lifecycle.ts +65 -26
- package/packages/extension/src/bridge-ticket-client.ts +61 -7
- package/packages/extension/src/bridge-tls-checks.ts +79 -0
- package/packages/extension/src/bridge.ts +41 -28
- package/packages/extension/src/canvas-tool.ts +2 -0
- package/packages/extension/src/command-handler.ts +80 -6
- package/packages/extension/src/connect-target.ts +23 -0
- package/packages/extension/src/connection.ts +30 -7
- package/packages/extension/src/dashboard-http-origin.ts +88 -0
- package/packages/extension/src/dev-build.ts +11 -1
- package/packages/extension/src/git-link-builder.ts +11 -2
- package/packages/extension/src/inbound-limits.ts +138 -0
- package/packages/extension/src/local-token-header.ts +77 -6
- package/packages/extension/src/model-refresh.ts +69 -0
- package/packages/extension/src/pinned-endpoint.ts +44 -0
- package/packages/extension/src/project-trust-live.ts +143 -0
- package/packages/extension/src/project-trust.ts +159 -20
- package/packages/extension/src/provider-register.ts +59 -18
- package/packages/extension/src/remote-registration-gate.ts +21 -21
- package/packages/extension/src/remote-upgrade-preparer.ts +104 -0
- package/packages/extension/src/role-model-tools.ts +47 -6
- package/packages/extension/src/server-auto-start.ts +1 -1
- package/packages/extension/src/server-proof-probe.ts +117 -0
- package/packages/extension/src/session-sync.ts +42 -12
- package/packages/extension/src/vault-delivery.ts +11 -1
- package/packages/server/package.json +8 -8
- package/packages/server/src/access/access-grants.ts +61 -8
- package/packages/server/src/access/access-health.ts +10 -0
- package/packages/server/src/access/access-plane.ts +5 -1
- package/packages/server/src/access/containment-gate.ts +55 -17
- package/packages/server/src/access/cwd-verdict.ts +3 -3
- package/packages/server/src/access/pi-trust-live.ts +244 -0
- package/packages/server/src/access/pi-trust-mirror-service.ts +44 -11
- package/packages/server/src/access/planes.ts +106 -1
- package/packages/server/src/access/preview-egress-store.ts +153 -0
- package/packages/server/src/access/prompt-channel.ts +5 -0
- package/packages/server/src/access/provisioning-wiring.ts +113 -0
- package/packages/server/src/access/provisioning.ts +763 -0
- package/packages/server/src/access/yolo-session.ts +54 -2
- package/packages/server/src/acp-bridge/acp-session-adapter.ts +8 -3
- package/packages/server/src/agent-events/agent-event-hub.ts +158 -0
- package/packages/server/src/auth/audit-log.ts +80 -0
- package/packages/server/src/auth/auth-plugin.ts +44 -25
- package/packages/server/src/auth/auth.ts +24 -4
- package/packages/server/src/auth/bearer-auth.ts +52 -12
- package/packages/server/src/auth/cors-origin.ts +76 -29
- package/packages/server/src/auth/csp-report-paths.ts +31 -0
- package/packages/server/src/auth/csp-report-sink.ts +302 -0
- package/packages/server/src/auth/csp-report-store.ts +406 -0
- package/packages/server/src/auth/csp.ts +105 -9
- package/packages/server/src/auth/dashboard-credential-store.ts +4 -0
- package/packages/server/src/auth/dashboard-state-caller.ts +29 -0
- package/packages/server/src/auth/local-proof-refusal-log.ts +89 -0
- package/packages/server/src/auth/local-proof.ts +69 -18
- package/packages/server/src/auth/localhost-guard.ts +48 -10
- package/packages/server/src/auth/nav-intent.ts +72 -0
- package/packages/server/src/auth/passkey/passkey-assets.ts +99 -0
- package/packages/server/src/auth/passkey/passkey-pages.ts +17 -81
- package/packages/server/src/auth/passkey/passkey-routes.ts +8 -0
- package/packages/server/src/auth/pi-legacy-credentials.ts +3 -0
- package/packages/server/src/auth/plugin-server-trust.ts +29 -0
- package/packages/server/src/auth/plugin-trust-store.ts +103 -0
- package/packages/server/src/auth/proven-tls.ts +45 -0
- package/packages/server/src/auth/provider-auth-storage.ts +6 -1
- package/packages/server/src/auth/radius-mcp.ts +1 -1
- package/packages/server/src/auth/request-principal.ts +32 -0
- package/packages/server/src/auth/secret-rotation.ts +246 -0
- package/packages/server/src/auth/secure-origin.ts +56 -0
- package/packages/server/src/auth/serve-app-host.ts +111 -11
- package/packages/server/src/auth/ws-ticket.ts +39 -1
- package/packages/server/src/auth/ws-upgrade-reject-log.ts +4 -1
- package/packages/server/src/browser-handlers/directory-handler.ts +23 -0
- package/packages/server/src/browser-handlers/folder-gate.ts +55 -0
- package/packages/server/src/browser-handlers/handler-context.ts +25 -2
- package/packages/server/src/browser-handlers/session-action-handler.ts +9 -1
- package/packages/server/src/browser-handlers/subscription-handler.ts +100 -2
- package/packages/server/src/browser-handlers/terminal-handler.ts +108 -10
- package/packages/server/src/canvas/canvas-accumulator.ts +35 -7
- package/packages/server/src/cli.ts +210 -39
- package/packages/server/src/config-api.ts +23 -1
- package/packages/server/src/config-snapshot.ts +48 -1
- package/packages/server/src/directory-service.ts +160 -54
- package/packages/server/src/event-wiring.ts +143 -108
- package/packages/server/src/git-worktree/git-operations.ts +44 -10
- package/packages/server/src/git-worktree/redact-git-text.ts +124 -0
- package/packages/server/src/git-worktree/ref-arg.ts +39 -0
- package/packages/server/src/git-worktree/worktree-init-trust.ts +43 -1
- package/packages/server/src/identity/http-road-classification.ts +13 -1
- package/packages/server/src/identity/ws-message-scope.ts +1 -0
- package/packages/server/src/identity/ws-road-classification.ts +1 -0
- package/packages/server/src/known-server-moves.ts +36 -15
- package/packages/server/src/lib/browse-roots.ts +118 -17
- package/packages/server/src/lib/client-dist.ts +36 -21
- package/packages/server/src/lib/dashboard-state-guard.ts +230 -0
- package/packages/server/src/lib/diagram-render.ts +4 -6
- package/packages/server/src/lib/eml.ts +3 -8
- package/packages/server/src/lib/file-cwd-admission.ts +145 -0
- package/packages/server/src/lib/grep-worker.ts +23 -0
- package/packages/server/src/lib/grep.ts +93 -7
- package/packages/server/src/lib/index-html.ts +81 -0
- package/packages/server/src/lib/office-preview.ts +2 -3
- package/packages/server/src/lib/path-containment.ts +7 -0
- package/packages/server/src/lib/resolve-file-mention.ts +11 -9
- package/packages/server/src/lib/route-admission.ts +80 -0
- package/packages/server/src/lib/writable-md-target.ts +4 -0
- package/packages/server/src/lifecycle/recovery-server.ts +65 -18
- package/packages/server/src/live-server/capability-redact.ts +15 -0
- package/packages/server/src/live-server/live-browser-gate.ts +22 -0
- package/packages/server/src/live-server/live-capability.ts +224 -17
- package/packages/server/src/live-server/live-csp-report.ts +63 -0
- package/packages/server/src/live-server/live-csp.ts +93 -0
- package/packages/server/src/live-server/live-egress.ts +435 -0
- package/packages/server/src/live-server/live-exposure.ts +149 -0
- package/packages/server/src/live-server/live-log.ts +21 -0
- package/packages/server/src/live-server/live-origin-scan.ts +298 -0
- package/packages/server/src/live-server/live-server-manager.ts +209 -33
- package/packages/server/src/live-server/live-server-proxy.ts +162 -17
- package/packages/server/src/live-server/live-static.ts +228 -0
- package/packages/server/src/openspec/dependencies-file.ts +122 -0
- package/packages/server/src/openspec/openspec-change-watcher.ts +3 -1
- package/packages/server/src/openspec/openspec-dependency-inputs.ts +281 -0
- package/packages/server/src/openspec/openspec-poll-fs-helpers.ts +72 -26
- package/packages/server/src/openspec/openspec-poll-worker-pool.ts +48 -12
- package/packages/server/src/openspec/openspec-poll-worker.ts +95 -9
- package/packages/server/src/openspec/workflow-schema-status.ts +379 -0
- package/packages/server/src/openspec/workflow-schema.ts +141 -267
- package/packages/server/src/package/npm-search-proxy.ts +5 -2
- package/packages/server/src/package/provider-probe.ts +8 -2
- package/packages/server/src/pairing/browser-gateway.ts +188 -12
- package/packages/server/src/pairing/device-token-services.ts +26 -0
- package/packages/server/src/pairing/paired-devices.ts +335 -22
- package/packages/server/src/pairing/pairing.ts +34 -4
- package/packages/server/src/pairing/socket-locality.ts +26 -0
- package/packages/server/src/persistence/preferences-store.ts +9 -0
- package/packages/server/src/pi/bridge-channel.ts +33 -0
- package/packages/server/src/pi/bridge-upgrade-auth.ts +18 -3
- package/packages/server/src/pi/gateway-listeners.ts +9 -3
- package/packages/server/src/pi/host-project-trust.ts +14 -6
- package/packages/server/src/pi/pi-gateway.ts +191 -21
- package/packages/server/src/pi/resource-toggle-trust.ts +1 -1
- package/packages/server/src/plugin-discovery-wiring.ts +142 -0
- package/packages/server/src/plugin-event-seam.ts +116 -4
- package/packages/server/src/plugin-runtime-clients.ts +89 -0
- package/packages/server/src/routes/access-routes.ts +159 -9
- package/packages/server/src/routes/agent-event-stream-routes.ts +137 -0
- package/packages/server/src/routes/canvas-types-routes.ts +20 -6
- package/packages/server/src/routes/csp-violations-routes.ts +162 -0
- package/packages/server/src/routes/file-routes.ts +329 -209
- package/packages/server/src/routes/file-write-binary-routes.ts +77 -20
- package/packages/server/src/routes/folder-project-routes.ts +18 -12
- package/packages/server/src/routes/git-routes.ts +319 -203
- package/packages/server/src/routes/grep-routes.ts +38 -14
- package/packages/server/src/routes/known-servers-routes.ts +189 -91
- package/packages/server/src/routes/live-server-routes.ts +378 -15
- package/packages/server/src/routes/local-proof-routes.ts +14 -0
- package/packages/server/src/routes/nav-intent-routes.ts +46 -0
- package/packages/server/src/routes/openspec-group-routes.ts +53 -44
- package/packages/server/src/routes/openspec-routes.ts +379 -28
- package/packages/server/src/routes/package-routes.ts +58 -10
- package/packages/server/src/routes/pairing-routes.ts +146 -10
- package/packages/server/src/routes/pi-trust-query-routes.ts +330 -0
- package/packages/server/src/routes/plugin-activation-routes.ts +59 -0
- package/packages/server/src/routes/plugin-runtime-client-routes.ts +149 -0
- package/packages/server/src/routes/provider-auth-routes.ts +15 -2
- package/packages/server/src/routes/resource-activation-routes.ts +18 -4
- package/packages/server/src/routes/session-routes.ts +51 -4
- package/packages/server/src/routes/system-routes.ts +84 -15
- package/packages/server/src/routes/ui-link-base.ts +88 -0
- package/packages/server/src/runtime-overlay/runtime-io.ts +2 -9
- package/packages/server/src/server.ts +648 -137
- package/packages/server/src/session/hydration-state.ts +148 -0
- package/packages/server/src/session/memory-session-manager.ts +7 -0
- package/packages/server/src/session/replay-exit-controller.ts +221 -0
- package/packages/server/src/session/retained-transcript.ts +9 -2
- package/packages/server/src/session/session-load-worker.ts +11 -2
- package/packages/server/src/spawn-process/headless-pid-registry.ts +14 -9
- package/packages/server/src/terminal/terminal-manager.ts +54 -46
- package/packages/server/src/tls/tls-bridge-route.ts +61 -0
- package/packages/server/src/tls/tls-config-validation.ts +2 -0
- package/packages/server/src/vault/device-expiry-sweep.ts +36 -0
- package/packages/server/src/vault/vault-delivery-hub.ts +155 -25
- package/packages/server/src/watch-files-handler.ts +66 -0
- package/packages/shared/package.json +1 -1
- package/packages/shared/src/browser-protocol.ts +148 -2
- package/packages/shared/src/canvas-declare.ts +27 -0
- package/packages/shared/src/config.ts +372 -53
- package/packages/shared/src/containment-guard.ts +35 -0
- package/packages/shared/src/dashboard-plugin/anthropic-peer-names.ts +9 -0
- package/packages/shared/src/dashboard-plugin/event-type-owners.ts +50 -0
- package/packages/shared/src/dashboard-plugin/file-handler.ts +2 -0
- package/packages/shared/src/dashboard-plugin/host-api.ts +61 -0
- package/packages/shared/src/dashboard-plugin/host-fx-classes.ts +23 -0
- package/packages/shared/src/dashboard-plugin/host-import-map.ts +38 -0
- package/packages/shared/src/dashboard-plugin/index.ts +2 -0
- package/packages/shared/src/dashboard-plugin/manifest-types.ts +54 -1
- package/packages/shared/src/dashboard-plugin/plugin-status.ts +54 -0
- package/packages/shared/src/dashboard-plugin/slot-props.ts +45 -3
- package/packages/shared/src/dashboard-plugin/slot-types.ts +8 -0
- package/packages/shared/src/dashboard-plugin/subagent-types.ts +219 -0
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +28 -0
- package/packages/shared/src/display-prefs.ts +13 -0
- package/packages/shared/src/doctor-core.ts +94 -27
- package/packages/shared/src/live-server.ts +141 -0
- package/packages/shared/src/local-proof-signal.ts +16 -0
- package/packages/shared/src/loopback-host.ts +26 -0
- package/packages/shared/src/mcp/verb-tiers.generated.ts +178 -0
- package/packages/shared/src/mdns-discovery.ts +25 -6
- package/packages/shared/src/nav-intent.ts +29 -0
- package/packages/shared/src/openspec-dependencies.ts +760 -0
- package/packages/shared/src/openspec-poller.ts +3 -2
- package/packages/shared/src/pairing-knock-client.ts +44 -4
- package/packages/shared/src/pi-trust-live-query.ts +93 -0
- package/packages/shared/src/pi-trust-mirror-snapshot.ts +5 -7
- package/packages/shared/src/pi-trust-mirror-types.ts +36 -3
- package/packages/shared/src/pi-trust-mirror.ts +29 -3
- package/packages/shared/src/pi-trust-resource-digest.ts +255 -0
- package/packages/shared/src/platform/ensure-bundled-git.ts +57 -34
- package/packages/shared/src/platform/git-source.ts +88 -26
- package/packages/shared/src/platform/git.ts +14 -8
- package/packages/shared/src/platform/process.ts +48 -4
- package/packages/shared/src/platform/resolve-pi-shell.ts +76 -0
- package/packages/shared/src/platform/select-git-source.ts +7 -0
- package/packages/shared/src/plugin-route-cwd.ts +95 -0
- package/packages/shared/src/preview-egress.ts +138 -0
- package/packages/shared/src/project-root.ts +16 -0
- package/packages/shared/src/protocol.ts +40 -1
- package/packages/shared/src/recommended-extensions.ts +18 -0
- package/packages/shared/src/recovery-timing.ts +27 -0
- package/packages/shared/src/remote-credentials.ts +63 -0
- package/packages/shared/src/rest-api.ts +55 -8
- package/packages/shared/src/route-tiers.ts +55 -1
- package/packages/shared/src/runtime-overlay/archive-safety.d.mts +2 -0
- package/packages/shared/src/runtime-overlay/archive-safety.mjs +33 -0
- package/packages/shared/src/runtime-overlay/manifest.d.mts +7 -0
- package/packages/shared/src/runtime-overlay/manifest.mjs +22 -2
- package/packages/shared/src/safe-local-path.ts +24 -0
- package/packages/shared/src/sanitize-profiles.ts +98 -0
- package/packages/shared/src/server-launcher.ts +9 -1
- package/packages/shared/src/server-pinning.ts +55 -2
- package/packages/shared/src/server-routes.ts +462 -0
- package/packages/shared/src/session-ui-path.ts +11 -0
- package/packages/shared/src/tls-only-token.ts +16 -0
- package/packages/shared/src/tool-registry/definitions.ts +5 -5
- package/packages/shared/src/tool-registry/strategies.ts +12 -11
- package/packages/shared/src/types.ts +66 -0
- package/packages/shared/src/url-policy.ts +96 -0
- package/packages/shared/src/vault-delivery.ts +15 -0
- package/packages/shared/src/ws-message-tiers.ts +1 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote folder gate. A non-local socket may only pin / add / move a folder that
|
|
3
|
+
* already lies inside the shared-root set; otherwise the command is dropped and
|
|
4
|
+
* ONLY that socket is told. A local socket is unchanged.
|
|
5
|
+
*
|
|
6
|
+
* Without this a remote caller could `pin_directory /opt/secret` and so widen
|
|
7
|
+
* its own browse roots. Consistency ("ask the host to pin it" stays true), not
|
|
8
|
+
* a hard boundary: see design.md threat model.
|
|
9
|
+
* See change: harden-server-capability-bounds (D1b).
|
|
10
|
+
*/
|
|
11
|
+
import { isWithinBrowseRoots } from "../lib/browse-roots.js";
|
|
12
|
+
import type { BrowserHandlerContext } from "./handler-context.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Run `proceed` for a local socket (`locallyTrusted === true`; synchronously, exactly as
|
|
16
|
+
* before) or for a remote (or unflagged) socket when `path` is inside the shared roots. Anything else, including
|
|
17
|
+
* a root computation failure or a missing root resolver, refuses (fail closed).
|
|
18
|
+
*/
|
|
19
|
+
export function gateRemoteFolder(
|
|
20
|
+
ctx: BrowserHandlerContext,
|
|
21
|
+
path: string,
|
|
22
|
+
command: string,
|
|
23
|
+
proceed: () => void,
|
|
24
|
+
): void {
|
|
25
|
+
// Default-deny: only an explicit `true` is local; an absent bit is treated as remote.
|
|
26
|
+
if (ctx.locallyTrusted === true) {
|
|
27
|
+
proceed();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const refuse = (why: string): void => {
|
|
31
|
+
// JSON.stringify: the path is caller-controlled (no newline may forge a log line).
|
|
32
|
+
console.warn(`[folder-gate] refused remote ${command} ${JSON.stringify(path)}: ${why}`);
|
|
33
|
+
ctx.sendTo(ctx.ws, { type: "folder_refused", path, reason: "outside_allowed_roots" });
|
|
34
|
+
};
|
|
35
|
+
const roots = ctx.browseRoots;
|
|
36
|
+
if (!roots) {
|
|
37
|
+
refuse("no root resolver");
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
roots()
|
|
41
|
+
// `null` = confinement is switched off (`browse.confineRemote: false`).
|
|
42
|
+
.then((allowed) => (allowed === null ? true : isWithinBrowseRoots(path, allowed)))
|
|
43
|
+
.then(
|
|
44
|
+
(ok) => {
|
|
45
|
+
if (!ok) return refuse("outside the shared roots");
|
|
46
|
+
try {
|
|
47
|
+
proceed();
|
|
48
|
+
} catch (err) {
|
|
49
|
+
// The check passed; a failing command is the handler's problem, not a refusal.
|
|
50
|
+
console.error(`[folder-gate] ${command} ${JSON.stringify(path)} failed after the root check:`, err);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
(err: unknown) => refuse(`root check failed: ${err instanceof Error ? err.message : String(err)}`),
|
|
54
|
+
);
|
|
55
|
+
}
|
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
import type { ServerToBrowserMessage } from "@blackbelt-technology/pi-dashboard-shared/browser-protocol.js";
|
|
7
7
|
import type { WebSocket } from "ws";
|
|
8
8
|
import type { DirectoryService } from "../directory-service.js";
|
|
9
|
-
import type { PendingAttachRegistry } from "../pending/pending-attach-registry.js";
|
|
10
9
|
import type { PendingArchiveIntentRegistry } from "../pending/pending-archive-intent-registry.js";
|
|
10
|
+
import type { PendingAttachRegistry } from "../pending/pending-attach-registry.js";
|
|
11
11
|
import type { PendingClientCorrelations } from "../pending/pending-client-correlations.js";
|
|
12
12
|
import type { PendingForkRegistry } from "../pending/pending-fork-registry.js";
|
|
13
13
|
import type { PendingInitialPromptRegistry } from "../pending/pending-initial-prompt-registry.js";
|
|
14
|
+
import type { PendingPrincipalOwnerRegistry } from "../pending/pending-principal-owner-registry.js";
|
|
14
15
|
import type { PendingResumeIntentRegistry } from "../pending/pending-resume-intent-registry.js";
|
|
15
16
|
import type { PendingResumeRegistry } from "../pending/pending-resume-registry.js";
|
|
16
|
-
import type { PendingPrincipalOwnerRegistry } from "../pending/pending-principal-owner-registry.js";
|
|
17
17
|
import type { PendingWorktreeBaseRegistry } from "../pending/pending-worktree-base-registry.js";
|
|
18
18
|
import type { EventStore } from "../persistence/memory-event-store.js";
|
|
19
19
|
import type { MetaPersistence } from "../persistence/meta-persistence.js";
|
|
@@ -26,6 +26,16 @@ import type { TerminalManager } from "../terminal/terminal-manager.js";
|
|
|
26
26
|
|
|
27
27
|
export interface BrowserHandlerContext {
|
|
28
28
|
ws: WebSocket;
|
|
29
|
+
/**
|
|
30
|
+
* Was this socket genuinely local at the `/ws` upgrade (`isLocallyTrusted`)?
|
|
31
|
+
* Only `true` is local: `false` OR absent is remote, so `pin_directory` /
|
|
32
|
+
* `add_folder_to_workspace` / `move_folder_to_workspace` are confined to the shared
|
|
33
|
+
* roots (default-deny). The gateway always sets it.
|
|
34
|
+
* See change: harden-server-capability-bounds (D1b).
|
|
35
|
+
*/
|
|
36
|
+
locallyTrusted?: boolean;
|
|
37
|
+
/** The shared-root set for the remote folder gate (`null` = confinement off); absent on a remote socket = refuse. */
|
|
38
|
+
browseRoots?: () => Promise<import("../lib/browse-roots.js").BrowseRoots | null>;
|
|
29
39
|
sessionManager: SessionManager;
|
|
30
40
|
eventStore: EventStore;
|
|
31
41
|
piGateway: PiGateway;
|
|
@@ -62,8 +72,21 @@ export interface BrowserHandlerContext {
|
|
|
62
72
|
* See change: add-tail-only-replay-window (D1).
|
|
63
73
|
*/
|
|
64
74
|
replayWindowMode?: import("@blackbelt-technology/pi-dashboard-shared/memory-limits.js").ReplayWindowMode;
|
|
75
|
+
/**
|
|
76
|
+
* Bridge replay-mode controller + hydration/register bookkeeping. Absent ⇒
|
|
77
|
+
* a subscribe never waits for a bridge replay and records no fingerprint.
|
|
78
|
+
* See change: fix-large-session-resume-replay (D5, D6).
|
|
79
|
+
*/
|
|
80
|
+
replayState?: import("../session/hydration-state.js").ReplayState;
|
|
65
81
|
directoryService?: DirectoryService;
|
|
66
82
|
terminalManager?: TerminalManager;
|
|
83
|
+
/**
|
|
84
|
+
* Cwd admission seam for standalone terminals (`handleCreateTerminal`): the live
|
|
85
|
+
* known-folder set + `admitCwd` options/test seams. Absent ⇒ nothing is known, so every
|
|
86
|
+
* standalone create is refused (fail closed).
|
|
87
|
+
* See change: harden-git-terminal-plugin-boundaries (D14).
|
|
88
|
+
*/
|
|
89
|
+
terminalAdmission?: import("../lib/route-admission.js").RouteAdmissionDeps;
|
|
67
90
|
headlessPidRegistry: HeadlessPidRegistry;
|
|
68
91
|
pendingResumeRegistry: PendingResumeRegistry;
|
|
69
92
|
pendingDashboardSpawns?: Map<string, number>;
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
findPidByMarker,
|
|
15
15
|
} from "@blackbelt-technology/pi-dashboard-shared/platform/process-identify.js";
|
|
16
16
|
import { mintSpawnToken } from "../auth/spawn-token.js";
|
|
17
|
+
import { isValidRefArg } from "../git-worktree/ref-arg.js";
|
|
17
18
|
import {
|
|
18
19
|
type DispatchReloadContext,
|
|
19
20
|
dispatchReload,
|
|
@@ -823,8 +824,15 @@ export async function handleSpawnSession(
|
|
|
823
824
|
// write `.meta.json#gitWorktreeBase` so the WORKSPACE-subcard pill
|
|
824
825
|
// can render `created from <base>` on later renders.
|
|
825
826
|
// See change: add-worktree-spawn-dialog.
|
|
827
|
+
// B1r: the hint is a client-supplied git ref — an option-like / control-character value is NOT
|
|
828
|
+
// stored (one log line, no client error: it is only a hint) and is re-validated where used.
|
|
829
|
+
// See change: harden-git-terminal-plugin-boundaries.
|
|
826
830
|
if (typeof msg.gitWorktreeBase === "string" && msg.gitWorktreeBase.length > 0) {
|
|
827
|
-
|
|
831
|
+
if (isValidRefArg(msg.gitWorktreeBase)) {
|
|
832
|
+
pendingWorktreeBaseRegistry?.enqueue(msg.cwd, msg.gitWorktreeBase);
|
|
833
|
+
} else {
|
|
834
|
+
console.warn(`[dashboard] spawn_session: ignoring invalid gitWorktreeBase hint (${JSON.stringify(msg.gitWorktreeBase).slice(0, 80)})`);
|
|
835
|
+
}
|
|
828
836
|
}
|
|
829
837
|
|
|
830
838
|
// Initial-prompt intent — same race-safe FIFO enqueue pattern. Consumed by
|
|
@@ -37,6 +37,50 @@ interface LoadResultLike {
|
|
|
37
37
|
success: boolean;
|
|
38
38
|
events: Array<{ eventType: string; timestamp: number; data: Record<string, unknown> }>;
|
|
39
39
|
error?: string;
|
|
40
|
+
/** Skip-wipe fingerprint source. See change: fix-large-session-resume-replay (D5). */
|
|
41
|
+
entryCount?: number;
|
|
42
|
+
leafEntryId?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A subscriber with nothing to read yet because a bridge is replaying the
|
|
47
|
+
* session: keepalive the client's replay-in-flight state until the replay exit
|
|
48
|
+
* flush (or the unregister terminal batch) ends it with `isLast: true`. The
|
|
49
|
+
* controller stops the keepalive BEFORE it flushes, so no empty marker can
|
|
50
|
+
* follow the terminal batch; a closed socket stops its own.
|
|
51
|
+
* See change: fix-large-session-resume-replay (D6).
|
|
52
|
+
*/
|
|
53
|
+
function waitForBridgeReplay(
|
|
54
|
+
ws: WebSocket,
|
|
55
|
+
sessionId: string,
|
|
56
|
+
ctx: BrowserHandlerContext,
|
|
57
|
+
opts: { prime: boolean },
|
|
58
|
+
): void {
|
|
59
|
+
const controller = ctx.replayState?.controller;
|
|
60
|
+
if (!controller) return;
|
|
61
|
+
const { sendTo } = ctx;
|
|
62
|
+
if (opts.prime) sendTo(ws, { type: "event_replay", sessionId, events: [], isLast: false });
|
|
63
|
+
let removeWaiter = (): void => {};
|
|
64
|
+
let timer: ReturnType<typeof setInterval> | null = setInterval(() => {
|
|
65
|
+
if (ws.readyState !== ws.OPEN) {
|
|
66
|
+
stop();
|
|
67
|
+
removeWaiter();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
sendTo(ws, { type: "event_replay", sessionId, events: [], isLast: false });
|
|
71
|
+
}, controller.keepaliveMs);
|
|
72
|
+
timer.unref?.();
|
|
73
|
+
function stop(): void {
|
|
74
|
+
if (timer !== null) {
|
|
75
|
+
clearInterval(timer);
|
|
76
|
+
timer = null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
removeWaiter = controller.addWaiter(sessionId, stop);
|
|
80
|
+
ws.once("close", () => {
|
|
81
|
+
stop();
|
|
82
|
+
removeWaiter();
|
|
83
|
+
});
|
|
40
84
|
}
|
|
41
85
|
|
|
42
86
|
/**
|
|
@@ -333,7 +377,21 @@ export async function sendEventBatches(
|
|
|
333
377
|
sendTo: (ws: WebSocket, msg: ServerToBrowserMessage) => void,
|
|
334
378
|
windowLimit?: number,
|
|
335
379
|
mode: ReplayWindowMode = "head-tail",
|
|
380
|
+
/**
|
|
381
|
+
* Superseded-send probe, checked before every batch (and once up front). When
|
|
382
|
+
* it answers true the send stops and returns 0; the caller unmutes the socket
|
|
383
|
+
* without a catch-up. Absent ⇒ never stale.
|
|
384
|
+
* See change: fix-large-session-resume-replay (D4).
|
|
385
|
+
*/
|
|
386
|
+
isStale?: () => boolean,
|
|
387
|
+
/**
|
|
388
|
+
* Send a `session_state_reset` when NO window applies, so the replay-exit
|
|
389
|
+
* flush always announces exactly one reset — the store may be head-trimmed
|
|
390
|
+
* (first seq > 1), where the client's seq-1 reset rule cannot be relied on.
|
|
391
|
+
*/
|
|
392
|
+
resetIfUnwindowed = false,
|
|
336
393
|
): Promise<number> {
|
|
394
|
+
if (isStale?.()) return 0;
|
|
337
395
|
// High-water mark is computed from the PRE-compaction window (D4).
|
|
338
396
|
const preCompactionMaxSeq = stored.length > 0 ? stored[stored.length - 1].seq : 0;
|
|
339
397
|
// Replay-only stream compaction: drop assistant `message_update` snapshots
|
|
@@ -424,6 +482,8 @@ export async function sendEventBatches(
|
|
|
424
482
|
if (ws.readyState === ws.OPEN) {
|
|
425
483
|
sendTo(ws, { type: "history_window", sessionId, headMaxSeq, tailMinSeq, gapCount, oldestGapSeq, windowShape: mode });
|
|
426
484
|
}
|
|
485
|
+
} else if (resetIfUnwindowed && ws.readyState === ws.OPEN) {
|
|
486
|
+
sendTo(ws, { type: "session_state_reset", sessionId });
|
|
427
487
|
}
|
|
428
488
|
// Terminate an empty payload explicitly. The loop below cannot run when there
|
|
429
489
|
// is nothing to batch, so without this a warm empty delta (or a cold session
|
|
@@ -439,6 +499,7 @@ export async function sendEventBatches(
|
|
|
439
499
|
}
|
|
440
500
|
for (let i = 0; i < compacted.length; i += REPLAY_BATCH_SIZE) {
|
|
441
501
|
if (ws.readyState !== ws.OPEN) return 0;
|
|
502
|
+
if (isStale?.()) return 0;
|
|
442
503
|
const batch = compacted.slice(i, i + REPLAY_BATCH_SIZE);
|
|
443
504
|
sendTo(ws, {
|
|
444
505
|
type: "event_replay",
|
|
@@ -889,6 +950,12 @@ export function handleSubscribe(
|
|
|
889
950
|
.catch(onReplayFailed);
|
|
890
951
|
}
|
|
891
952
|
}
|
|
953
|
+
} else if (ctx.replayState?.controller.replayingSessions.has(msg.sessionId)) {
|
|
954
|
+
// A bridge is replaying this session and nothing is stored yet: do NOT
|
|
955
|
+
// hydrate from disk (it would race the replay, and a session without a
|
|
956
|
+
// `sessionFile` would be reported unavailable). The exit flush serves us.
|
|
957
|
+
// See change: fix-large-session-resume-replay (D6).
|
|
958
|
+
waitForBridgeReplay(ws, msg.sessionId, ctx, { prime: true });
|
|
892
959
|
} else if (directoryService) {
|
|
893
960
|
const session = sessionManager.get(msg.sessionId);
|
|
894
961
|
// Archived sessions are non-resident: resolve the transcript file from the
|
|
@@ -984,6 +1051,11 @@ export function handleSubscribe(
|
|
|
984
1051
|
replayed: replayedSockets,
|
|
985
1052
|
});
|
|
986
1053
|
}
|
|
1054
|
+
// A register that lands while this load is running (or resolved but not
|
|
1055
|
+
// yet inserted) supersedes it. See change: fix-large-session-resume-replay (D6).
|
|
1056
|
+
const hydration = ctx.replayState?.hydration;
|
|
1057
|
+
const generation = hydration?.generation(msg.sessionId) ?? 0;
|
|
1058
|
+
const superseded = (): boolean => hydration !== undefined && hydration.generation(msg.sessionId) !== generation;
|
|
987
1059
|
// The retained read is async now, so it is handed to the same promise
|
|
988
1060
|
// chain the disk path uses rather than forking the ~60 lines of
|
|
989
1061
|
// ingest/broadcast below.
|
|
@@ -1003,11 +1075,21 @@ export function handleSubscribe(
|
|
|
1003
1075
|
// `retainedTranscript` after the last subscriber left.
|
|
1004
1076
|
if (read.cancelled) return { success: false, events: [], error: "cancelled" };
|
|
1005
1077
|
retainedState = read.state;
|
|
1006
|
-
return { success: true, events: read.events };
|
|
1078
|
+
return { success: true, events: read.events, entryCount: read.entryCount, leafEntryId: read.leafEntryId };
|
|
1007
1079
|
})
|
|
1008
1080
|
: directoryService.loadSessionEvents(msg.sessionId, sessionFile as string, session?.contextWindow);
|
|
1009
1081
|
loaded.then(async (result) => {
|
|
1010
1082
|
stopHeartbeat();
|
|
1083
|
+
if (superseded()) {
|
|
1084
|
+
// Drop the result before it is inserted. The bridge replay is now the
|
|
1085
|
+
// only source: keep this socket waiting for its exit flush.
|
|
1086
|
+
if (ctx.replayState?.controller.replayingSessions.has(msg.sessionId)) {
|
|
1087
|
+
if (subs.has(msg.sessionId)) waitForBridgeReplay(ws, msg.sessionId, ctx, { prime: false });
|
|
1088
|
+
} else if (!eventStore.hasEvents(msg.sessionId)) {
|
|
1089
|
+
sendTo(ws, { type: "event_replay", sessionId: msg.sessionId, events: [], isLast: true });
|
|
1090
|
+
}
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1011
1093
|
if (result.success) {
|
|
1012
1094
|
// Hydration admits full-resolution inline images straight from the
|
|
1013
1095
|
// transcript, so it needs the SAME two-phase strip as the live path.
|
|
@@ -1026,6 +1108,20 @@ export function handleSubscribe(
|
|
|
1026
1108
|
pendingAttachments.push(...prepared.pending);
|
|
1027
1109
|
eventStore.insertEvent(msg.sessionId, prepared.event);
|
|
1028
1110
|
}
|
|
1111
|
+
// Fingerprint of what was just stored, so a later register of the same
|
|
1112
|
+
// branch can skip the wipe. None when the leaf is unknown.
|
|
1113
|
+
if (hydration) {
|
|
1114
|
+
hydration.markHydrated(
|
|
1115
|
+
msg.sessionId,
|
|
1116
|
+
result.entryCount !== undefined && result.leafEntryId !== undefined
|
|
1117
|
+
? {
|
|
1118
|
+
entryCount: result.entryCount,
|
|
1119
|
+
leafEntryId: result.leafEntryId,
|
|
1120
|
+
storeMaxSeq: eventStore.getMaxSeq(msg.sessionId),
|
|
1121
|
+
}
|
|
1122
|
+
: undefined,
|
|
1123
|
+
);
|
|
1124
|
+
}
|
|
1029
1125
|
const statsUpdates = extractStatsFromEvents(result.events);
|
|
1030
1126
|
const metaUpdates: Record<string, unknown> = {
|
|
1031
1127
|
dataUnavailable: false,
|
|
@@ -1049,7 +1145,9 @@ export function handleSubscribe(
|
|
|
1049
1145
|
// Cold hydration is ALWAYS a full stream — window it (D1). The
|
|
1050
1146
|
// `history_window` message is emitted per subscriber inside this
|
|
1051
1147
|
// loop by `sendEventBatches` itself.
|
|
1052
|
-
await sendEventBatches(sub, msg.sessionId, stored, sendTo, maxReplayEvents, replayWindowMode);
|
|
1148
|
+
await sendEventBatches(sub, msg.sessionId, stored, sendTo, maxReplayEvents, replayWindowMode, superseded);
|
|
1149
|
+
// A register superseded the fan-out: stop; the exit flush serves everyone.
|
|
1150
|
+
if (superseded()) break;
|
|
1053
1151
|
replayPendingUiRequests(sub, msg.sessionId);
|
|
1054
1152
|
replayNotifyLog(sub, msg.sessionId);
|
|
1055
1153
|
replayUiState(sub, msg.sessionId, ctx);
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Terminal message handlers: create, kill, rename.
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
4
|
+
import { isAbsolute } from "node:path";
|
|
5
|
+
import type { BrowserToServerMessage, TerminalErrorMessage } from "@blackbelt-technology/pi-dashboard-shared/browser-protocol.js";
|
|
6
|
+
import { admissionAllows, releaseAdmission } from "@blackbelt-technology/pi-dashboard-shared/cwd-guard.js";
|
|
5
7
|
import { canAccessSession } from "../identity/session-access.js";
|
|
8
|
+
import { admitRouteCwd } from "../lib/route-admission.js";
|
|
9
|
+
import { TerminalLimitError } from "../terminal/terminal-manager.js";
|
|
6
10
|
import type { BrowserHandlerContext } from "./handler-context.js";
|
|
7
11
|
|
|
8
12
|
type Owner = { iss: string; sub: string };
|
|
@@ -43,16 +47,91 @@ function mayUseTerminal(ctx: BrowserHandlerContext, terminalId: string): boolean
|
|
|
43
47
|
return canAccessSession({ active: true, principal: principalOf(ctx), owner: t.principalOwner });
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
/**
|
|
51
|
+
* A cap refusal starts nothing and is told ONLY to the requesting socket: no
|
|
52
|
+
* broadcast, no session-order insert, no inline event. Anything else rethrows
|
|
53
|
+
* (the gateway logs it). See change: harden-server-capability-bounds (D3).
|
|
54
|
+
*/
|
|
55
|
+
function refuseAtCap(err: unknown, cwd: string, inline: boolean, ctx: BrowserHandlerContext): void {
|
|
56
|
+
if (!(err instanceof TerminalLimitError)) throw err;
|
|
57
|
+
console.warn(`[terminal] new terminal refused: ${err.scope} limit ${err.limit} reached (inline=${inline})`);
|
|
58
|
+
ctx.sendTo(ctx.ws, { type: "terminal_limit_reached", scope: err.scope, limit: err.limit, cwd, inline });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const REFUSAL_TEXT: Record<TerminalErrorMessage["code"], string> = {
|
|
62
|
+
cwd_not_admitted:
|
|
63
|
+
"A terminal was not opened: this folder is not one the dashboard knows about. Open a session in it or pin it first.",
|
|
64
|
+
cwd_admission_unavailable:
|
|
65
|
+
"A terminal was not opened: the folder could not be verified right now. Try again in a moment.",
|
|
66
|
+
session_not_found: "A terminal was not opened: the session for it no longer exists.",
|
|
67
|
+
};
|
|
68
|
+
const NO_SESSION_CWD_TEXT = "A terminal was not opened: this session has no recorded folder.";
|
|
69
|
+
|
|
70
|
+
/** `terminal_error` goes to the requesting socket ONLY; a failing send never escapes. */
|
|
71
|
+
function sendTerminalError(
|
|
48
72
|
ctx: BrowserHandlerContext,
|
|
73
|
+
code: TerminalErrorMessage["code"],
|
|
74
|
+
cwd: unknown,
|
|
75
|
+
inline: boolean,
|
|
76
|
+
message: string = REFUSAL_TEXT[code],
|
|
49
77
|
): void {
|
|
78
|
+
try {
|
|
79
|
+
ctx.sendTo(ctx.ws, { type: "terminal_error", code, cwd: typeof cwd === "string" ? cwd : "", inline, message });
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.error(`[terminal] could not deliver terminal_error (${code}): ${errText(err)}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const errText = (err: unknown): string => (err instanceof Error ? err.message : String(err));
|
|
86
|
+
|
|
87
|
+
/** The requesting socket is closing/closed (`ws` readyState CLOSING=2 / CLOSED=3). Absent ⇒ open. */
|
|
88
|
+
function requesterClosed(ctx: BrowserHandlerContext): boolean {
|
|
89
|
+
const state = (ctx.ws as { readyState?: number } | undefined)?.readyState;
|
|
90
|
+
return state === 2 || state === 3;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Standalone terminal. The cwd is admitted (shared tri-state `admitCwd`: known
|
|
95
|
+
* folders = session cwds ∪ pinned dirs ∪ workspace folders, plus a worktree of one)
|
|
96
|
+
* BEFORE any spawn; the cap is checked by `spawn` AFTER the await, in the same tick
|
|
97
|
+
* as the spawn, so concurrent requests cannot exceed it. Never rejects: every
|
|
98
|
+
* failure becomes `terminal_error{cwd_admission_unavailable}` and one log line.
|
|
99
|
+
* See change: harden-git-terminal-plugin-boundaries (D14).
|
|
100
|
+
*/
|
|
101
|
+
export async function handleCreateTerminal(
|
|
102
|
+
msg: Extract<BrowserToServerMessage, { type: "create_terminal" }>,
|
|
103
|
+
ctx: BrowserHandlerContext,
|
|
104
|
+
): Promise<void> {
|
|
50
105
|
const { terminalManager, sessionOrderManager, broadcast } = ctx;
|
|
51
|
-
if (terminalManager
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
106
|
+
if (!terminalManager || !sessionOrderManager) return;
|
|
107
|
+
const cwd: unknown = msg.cwd;
|
|
108
|
+
if (typeof cwd !== "string" || cwd === "" || !isAbsolute(cwd)) {
|
|
109
|
+
return sendTerminalError(ctx, "cwd_not_admitted", cwd, false);
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const admission = await admitRouteCwd(cwd, ctx.terminalAdmission);
|
|
113
|
+
try {
|
|
114
|
+
if (admission.kind === "indeterminate") {
|
|
115
|
+
console.warn(`[terminal] cwd admission undecidable for new terminal (${admission.failedProbes.join(",")})`);
|
|
116
|
+
return sendTerminalError(ctx, "cwd_admission_unavailable", cwd, false);
|
|
117
|
+
}
|
|
118
|
+
if (!admissionAllows(admission, cwd)) return sendTerminalError(ctx, "cwd_not_admitted", cwd, false);
|
|
119
|
+
if (requesterClosed(ctx)) return; // requester went away while admission was pending
|
|
120
|
+
let terminal: ReturnType<typeof terminalManager.spawn>;
|
|
121
|
+
try {
|
|
122
|
+
terminal = terminalManager.spawn(cwd, { owner: ownerForSpawn(ctx) });
|
|
123
|
+
} catch (err) {
|
|
124
|
+
return refuseAtCap(err, cwd, false, ctx);
|
|
125
|
+
}
|
|
126
|
+
sessionOrderManager.insert(cwd, terminal.id);
|
|
127
|
+
broadcast({ type: "terminal_added", terminal });
|
|
128
|
+
broadcast({ type: "sessions_reordered", cwd, sessionIds: sessionOrderManager.getOrder(cwd) });
|
|
129
|
+
} finally {
|
|
130
|
+
if (admission.kind === "admit") releaseAdmission(admission.token);
|
|
131
|
+
}
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error(`[terminal] create_terminal failed: ${errText(err)}`);
|
|
134
|
+
sendTerminalError(ctx, "cwd_admission_unavailable", cwd, false);
|
|
56
135
|
}
|
|
57
136
|
}
|
|
58
137
|
|
|
@@ -61,7 +140,9 @@ export function handleCreateTerminal(
|
|
|
61
140
|
* terminal_added (TerminalsView filters ephemeral from tabs), then insert and
|
|
62
141
|
* broadcast an `inline_terminal_open` event into the session's chat stream so
|
|
63
142
|
* the card is event-sourced and replays on reload.
|
|
64
|
-
*
|
|
143
|
+
* The PTY spawns at the SESSION's own recorded cwd; `msg.cwd` (kept in the
|
|
144
|
+
* protocol for old clients) is ignored. Unknown session → `session_not_found`.
|
|
145
|
+
* See change: add-inline-terminal-card, harden-git-terminal-plugin-boundaries (D14).
|
|
65
146
|
*/
|
|
66
147
|
export function handleOpenInlineTerminal(
|
|
67
148
|
msg: Extract<BrowserToServerMessage, { type: "open_inline_terminal" }>,
|
|
@@ -70,7 +151,24 @@ export function handleOpenInlineTerminal(
|
|
|
70
151
|
const { terminalManager, eventStore, broadcast, broadcastEvent } = ctx;
|
|
71
152
|
if (!terminalManager) return;
|
|
72
153
|
if (!mayWriteSession(ctx, msg.sessionId)) return;
|
|
73
|
-
|
|
154
|
+
let terminal: ReturnType<typeof terminalManager.spawn>;
|
|
155
|
+
try {
|
|
156
|
+
const sessionId: unknown = msg.sessionId;
|
|
157
|
+
const session = typeof sessionId === "string" && sessionId !== "" ? ctx.sessionManager.get(sessionId) : undefined;
|
|
158
|
+
if (!session) return sendTerminalError(ctx, "session_not_found", msg.cwd, true);
|
|
159
|
+
const cwd: unknown = session.cwd;
|
|
160
|
+
if (typeof cwd !== "string" || cwd === "") {
|
|
161
|
+
return sendTerminalError(ctx, "cwd_not_admitted", msg.cwd, true, NO_SESSION_CWD_TEXT);
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
terminal = terminalManager.spawn(cwd, { ephemeral: true, owner: ownerForSpawn(ctx) });
|
|
165
|
+
} catch (err) {
|
|
166
|
+
return refuseAtCap(err, cwd, true, ctx);
|
|
167
|
+
}
|
|
168
|
+
} catch (err) {
|
|
169
|
+
console.error(`[terminal] open_inline_terminal failed: ${errText(err)}`);
|
|
170
|
+
return sendTerminalError(ctx, "cwd_admission_unavailable", msg.cwd, true);
|
|
171
|
+
}
|
|
74
172
|
broadcast({ type: "terminal_added", terminal });
|
|
75
173
|
const seq = eventStore.insertEvent(msg.sessionId, {
|
|
76
174
|
eventType: "inline_terminal_open",
|
|
@@ -36,7 +36,7 @@ import type { ViewTarget } from "@blackbelt-technology/pi-dashboard-shared/types
|
|
|
36
36
|
/** Minimal forwarded-event shape the accumulator reads. */
|
|
37
37
|
export interface CanvasForwardedEvent {
|
|
38
38
|
eventType: string;
|
|
39
|
-
data?: { toolName?: unknown; args?: unknown };
|
|
39
|
+
data?: { toolName?: unknown; args?: unknown; toolCallId?: unknown };
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export interface CanvasAccumulatorDeps {
|
|
@@ -52,9 +52,16 @@ export interface CanvasAccumulatorDeps {
|
|
|
52
52
|
target: ViewTarget | null,
|
|
53
53
|
mode?: "replace" | "pin",
|
|
54
54
|
title?: string,
|
|
55
|
+
/** Tool call id that produced the target (last writer wins). */
|
|
56
|
+
sourceCallId?: string,
|
|
55
57
|
) => void;
|
|
56
58
|
/** Broadcast a `canvas_server_chip` for a declared server (Decision 4). */
|
|
57
|
-
broadcastServerChip: (
|
|
59
|
+
broadcastServerChip: (
|
|
60
|
+
sessionId: string,
|
|
61
|
+
port: number,
|
|
62
|
+
title?: string,
|
|
63
|
+
extra?: { path?: string; declarationId?: string },
|
|
64
|
+
) => void;
|
|
58
65
|
/**
|
|
59
66
|
* Broadcast a chip-expiry for a session's active server chip (S32): at the
|
|
60
67
|
* turn boundary (`agent_end`) or abort/termination the chip must become
|
|
@@ -87,6 +94,8 @@ export function createCanvasAccumulator(
|
|
|
87
94
|
): CanvasAccumulator {
|
|
88
95
|
// Per-session per-turn candidate buffer.
|
|
89
96
|
const buffers = new Map<string, CanvasCandidate[]>();
|
|
97
|
+
// Tool call id that produced each buffered candidate (parallel by identity).
|
|
98
|
+
const callIds = new WeakMap<CanvasCandidate, string>();
|
|
90
99
|
// Per-session active server-chip port (declared this turn), for expiry (S32).
|
|
91
100
|
const activeChips = new Map<string, number>();
|
|
92
101
|
|
|
@@ -102,7 +111,9 @@ export function createCanvasAccumulator(
|
|
|
102
111
|
candidate: CanvasCandidate,
|
|
103
112
|
mode?: "replace" | "pin",
|
|
104
113
|
title?: string,
|
|
114
|
+
callId?: string,
|
|
105
115
|
): void {
|
|
116
|
+
if (callId) callIds.set(candidate, callId);
|
|
106
117
|
const buf = buffers.get(sessionId) ?? [];
|
|
107
118
|
const wasEmpty = buf.length === 0;
|
|
108
119
|
buf.push(candidate);
|
|
@@ -111,13 +122,17 @@ export function createCanvasAccumulator(
|
|
|
111
122
|
// DECLARE re-crowns the eager target (S16). Non-first DOC candidates only
|
|
112
123
|
// accumulate — recency is resolved at settle.
|
|
113
124
|
if (wasEmpty || candidate.prio === "DECLARE") {
|
|
114
|
-
deps.broadcastIntent(sessionId, "eager", candidate.target, mode, title);
|
|
125
|
+
deps.broadcastIntent(sessionId, "eager", candidate.target, mode, title, callId);
|
|
115
126
|
}
|
|
116
127
|
}
|
|
117
128
|
|
|
118
129
|
function onToolStart(sessionId: string, event: CanvasForwardedEvent, cwd: string): void {
|
|
119
130
|
const toolName = typeof event.data?.toolName === "string" ? event.data.toolName : "";
|
|
120
131
|
if (!toolName) return;
|
|
132
|
+
const callId =
|
|
133
|
+
typeof event.data?.toolCallId === "string" && event.data.toolCallId
|
|
134
|
+
? event.data.toolCallId
|
|
135
|
+
: undefined;
|
|
121
136
|
const args = event.data?.args as Record<string, unknown> | undefined;
|
|
122
137
|
|
|
123
138
|
// `canvas()` declare-tool: normalized here with the session cwd; bypasses
|
|
@@ -131,17 +146,20 @@ export function createCanvasAccumulator(
|
|
|
131
146
|
if (!result.ok) return;
|
|
132
147
|
if ("chip" in result) {
|
|
133
148
|
activeChips.set(sessionId, result.chip.port);
|
|
134
|
-
deps.broadcastServerChip(sessionId, result.chip.port, result.chip.title
|
|
149
|
+
deps.broadcastServerChip(sessionId, result.chip.port, result.chip.title, {
|
|
150
|
+
path: result.chip.path,
|
|
151
|
+
declarationId: callId,
|
|
152
|
+
});
|
|
135
153
|
return;
|
|
136
154
|
}
|
|
137
|
-
pushCandidate(sessionId, result.candidate, intentMode(result.mode), result.title);
|
|
155
|
+
pushCandidate(sessionId, result.candidate, intentMode(result.mode), result.title, callId);
|
|
138
156
|
return;
|
|
139
157
|
}
|
|
140
158
|
|
|
141
159
|
// DOC detect: write/edit only, gated by the fresh-read type registry.
|
|
142
160
|
const canvasTypes = deps.readCanvasTypes(cwd);
|
|
143
161
|
const candidate = detectCanvasIntent(toolName, args, cwd, canvasTypes);
|
|
144
|
-
if (candidate) pushCandidate(sessionId, candidate);
|
|
162
|
+
if (candidate) pushCandidate(sessionId, candidate, undefined, undefined, callId);
|
|
145
163
|
}
|
|
146
164
|
|
|
147
165
|
function onAgentEnd(sessionId: string): void {
|
|
@@ -153,7 +171,17 @@ export function createCanvasAccumulator(
|
|
|
153
171
|
// No canvas activity this turn → nothing to settle (avoids closing a prior
|
|
154
172
|
// session-persisted canvas and avoids per-idle-turn spam).
|
|
155
173
|
if (!buf || buf.length === 0) return;
|
|
156
|
-
|
|
174
|
+
const winner = selectCanvasTarget(buf);
|
|
175
|
+
// The winning candidate is the one whose target `selectCanvasTarget` returned.
|
|
176
|
+
const source = winner ? buf.findLast((c) => c.target === winner) : undefined;
|
|
177
|
+
deps.broadcastIntent(
|
|
178
|
+
sessionId,
|
|
179
|
+
"settle",
|
|
180
|
+
winner,
|
|
181
|
+
undefined,
|
|
182
|
+
undefined,
|
|
183
|
+
source ? callIds.get(source) : undefined,
|
|
184
|
+
);
|
|
157
185
|
}
|
|
158
186
|
|
|
159
187
|
function resetTurn(sessionId: string): void {
|