tmux-ide 2.8.0 → 2.9.0-beta.14
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/README.md +88 -125
- package/bin/cli.js +75949 -12803
- package/package.json +74 -37
- package/packages/contracts/package.json +9 -3
- package/packages/contracts/src/__tests__/actions-contract.test.ts +119 -0
- package/packages/contracts/src/__tests__/agent-graph-overlay.test.ts +428 -0
- package/packages/contracts/src/__tests__/app-window-mutation.test.ts +51 -0
- package/packages/contracts/src/__tests__/app-window-state.test.ts +34 -0
- package/packages/contracts/src/__tests__/application-shell.test.ts +635 -0
- package/packages/contracts/src/__tests__/causal-cell.test.ts +47 -0
- package/packages/contracts/src/__tests__/client-view-state.test.ts +115 -0
- package/packages/contracts/src/__tests__/cohesion-fixture.test.ts +106 -0
- package/packages/contracts/src/__tests__/commands.test.ts +86 -0
- package/packages/contracts/src/__tests__/daemon-events.test.ts +400 -0
- package/packages/contracts/src/__tests__/daemon-resources.test.ts +143 -0
- package/packages/contracts/src/__tests__/daemon-wire.test.ts +117 -0
- package/packages/contracts/src/__tests__/desktop-host.test.ts +269 -0
- package/packages/contracts/src/__tests__/desktop-missions.test.ts +211 -0
- package/packages/contracts/src/__tests__/experience-conformance.test.ts +153 -0
- package/packages/contracts/src/__tests__/experience-kernel-boundary.test.ts +109 -0
- package/packages/contracts/src/__tests__/experience-shell.test.ts +65 -0
- package/packages/contracts/src/__tests__/fixtures/daemon-wire-v2.json +35 -0
- package/packages/contracts/src/__tests__/fleet-agent-graph.test.ts +231 -0
- package/packages/contracts/src/__tests__/fleet-catalog.test.ts +268 -0
- package/packages/contracts/src/__tests__/fleet-lifecycle.test.ts +62 -0
- package/packages/contracts/src/__tests__/focus-overlay.test.ts +110 -0
- package/packages/contracts/src/__tests__/ide-config.test.ts +18 -0
- package/packages/contracts/src/__tests__/issue-error.test.ts +78 -0
- package/packages/contracts/src/__tests__/mission-projections.test.ts +186 -0
- package/packages/contracts/src/__tests__/missions.test.ts +272 -0
- package/packages/contracts/src/__tests__/multiplexer-verbs.test.ts +240 -0
- package/packages/contracts/src/__tests__/pane-appearance.test.ts +146 -0
- package/packages/contracts/src/__tests__/pane-stream.test.ts +488 -0
- package/packages/contracts/src/__tests__/pane-widget-marker.test.ts +327 -0
- package/packages/contracts/src/__tests__/performance-metrics.test.ts +73 -0
- package/packages/contracts/src/__tests__/performance-qualification.test.ts +137 -0
- package/packages/contracts/src/__tests__/rich-card-widget.test.ts +29 -0
- package/packages/contracts/src/__tests__/session-runtime.test.ts +504 -0
- package/packages/contracts/src/__tests__/startup-readiness.test.ts +409 -0
- package/packages/contracts/src/__tests__/terminal-attachment-stream.test.ts +72 -0
- package/packages/contracts/src/__tests__/terminal-attachments.test.ts +329 -0
- package/packages/contracts/src/__tests__/terminal-delivery.test.ts +88 -0
- package/packages/contracts/src/__tests__/terminal-runtime-inventory.test.ts +68 -0
- package/packages/contracts/src/__tests__/transport-contract-loading.test.ts +91 -0
- package/packages/contracts/src/__tests__/visual-tokens.test.ts +224 -0
- package/packages/contracts/src/__tests__/workspace-changes-resource.test.ts +317 -0
- package/packages/contracts/src/__tests__/workspace-changes-view.test.ts +122 -0
- package/packages/contracts/src/__tests__/workspace-config.test.ts +294 -0
- package/packages/contracts/src/__tests__/workspace-files-resource.test.ts +261 -0
- package/packages/contracts/src/__tests__/workspace-files-tree.test.ts +147 -0
- package/packages/contracts/src/__tests__/workspace-pane-creation.test.ts +249 -0
- package/packages/contracts/src/__tests__/workspace-promotion.test.ts +94 -0
- package/packages/contracts/src/__tests__/workspace-resource-identity.test.ts +90 -0
- package/packages/contracts/src/actions-contract.ts +205 -0
- package/packages/contracts/src/actions-errors.ts +20 -0
- package/packages/contracts/src/agent-graph-overlay.ts +425 -0
- package/packages/contracts/src/app-window-mutation.ts +101 -0
- package/packages/contracts/src/app-window-state.ts +446 -0
- package/packages/contracts/src/application-shell-resource.ts +54 -0
- package/packages/contracts/src/application-shell.ts +1094 -0
- package/packages/contracts/src/causal-cell.ts +168 -0
- package/packages/contracts/src/client-view-state.ts +156 -0
- package/packages/contracts/src/cohesion-fixture.ts +647 -0
- package/packages/contracts/src/commands.ts +153 -0
- package/packages/contracts/src/daemon-events.ts +374 -0
- package/packages/contracts/src/daemon-resource-request.test.ts +192 -0
- package/packages/contracts/src/daemon-resource-request.ts +397 -0
- package/packages/contracts/src/daemon-resources.ts +108 -0
- package/packages/contracts/src/daemon-wire.ts +100 -0
- package/packages/contracts/src/desktop-daemon-capability-error.ts +31 -0
- package/packages/contracts/src/desktop-daemon-issue.ts +83 -0
- package/packages/contracts/src/desktop-host.ts +728 -0
- package/packages/contracts/src/desktop-missions.ts +159 -0
- package/packages/contracts/src/desktop-update.ts +48 -0
- package/packages/contracts/src/desktop-workspace-name.ts +18 -0
- package/packages/contracts/src/domain.ts +493 -0
- package/packages/contracts/src/experience-identifiers.ts +53 -0
- package/packages/contracts/src/experience-shell.ts +249 -0
- package/packages/contracts/src/fleet-agent-graph.ts +171 -0
- package/packages/contracts/src/fleet-catalog.ts +190 -0
- package/packages/contracts/src/fleet-lifecycle.ts +136 -0
- package/packages/contracts/src/focus-overlay.ts +175 -0
- package/packages/contracts/src/ide-config.ts +40 -20
- package/packages/contracts/src/index.ts +70 -0
- package/packages/contracts/src/interaction-receipts.ts +438 -0
- package/packages/contracts/src/issue-error.ts +107 -0
- package/packages/contracts/src/mission-projections.ts +269 -0
- package/packages/contracts/src/multiplexer-verbs.ts +447 -0
- package/packages/contracts/src/pane-appearance.ts +248 -0
- package/packages/contracts/src/pane-stream.ts +711 -0
- package/packages/contracts/src/pane-widget-descriptor.ts +62 -0
- package/packages/contracts/src/pane-widget-marker.ts +301 -0
- package/packages/contracts/src/performance-metrics.ts +128 -0
- package/packages/contracts/src/performance-qualification.ts +276 -0
- package/packages/contracts/src/rich-card-widget.ts +72 -0
- package/packages/contracts/src/saved-machines.ts +50 -0
- package/packages/contracts/src/semantic-identity.ts +39 -0
- package/packages/contracts/src/session-runtime.ts +364 -0
- package/packages/contracts/src/shared-monotonic-clock.ts +18 -0
- package/packages/contracts/src/startup-readiness.ts +432 -0
- package/packages/contracts/src/terminal-attachment-stream.ts +119 -0
- package/packages/contracts/src/terminal-attachments.ts +347 -0
- package/packages/contracts/src/terminal-delivery.ts +245 -0
- package/packages/contracts/src/terminal-replica.ts +133 -0
- package/packages/contracts/src/terminal-runtime-inventory.ts +42 -0
- package/packages/contracts/src/visual-recipes.ts +126 -0
- package/packages/contracts/src/visual-theme-presets.ts +499 -0
- package/packages/contracts/src/visual-tokens.ts +883 -0
- package/packages/contracts/src/widget-asset.ts +43 -0
- package/packages/contracts/src/workspace-catalog-resource.test.ts +146 -0
- package/packages/contracts/src/workspace-catalog-resource.ts +180 -0
- package/packages/contracts/src/workspace-changes-resource.ts +405 -0
- package/packages/contracts/src/workspace-changes-view.ts +195 -0
- package/packages/contracts/src/workspace-config.ts +303 -0
- package/packages/contracts/src/workspace-files-resource.ts +310 -0
- package/packages/contracts/src/workspace-files-tree.ts +167 -0
- package/packages/contracts/src/workspace-missions-resource.ts +32 -0
- package/packages/contracts/src/workspace-multiplexer.ts +373 -0
- package/packages/contracts/src/workspace-open-handoff.ts +99 -0
- package/packages/contracts/src/workspace-open.ts +62 -0
- package/packages/contracts/src/workspace-pane-creation.ts +221 -0
- package/packages/contracts/src/workspace-promotion.ts +107 -0
- package/packages/contracts/src/workspace-resource-identity.ts +81 -0
- package/packages/contracts/src/workspace-state.ts +530 -0
- package/packages/contracts/src/workspace.ts +14 -2
- package/packages/core/package.json +35 -0
- package/packages/core/src/agent-provisioning.test.ts +42 -0
- package/packages/core/src/agent-provisioning.ts +112 -0
- package/packages/core/src/app-window-identity.ts +33 -0
- package/packages/core/src/application-shell-session.test.ts +105 -0
- package/packages/core/src/application-shell-session.ts +195 -0
- package/packages/core/src/index.ts +17 -0
- package/packages/core/src/interaction-flow.test.ts +66 -0
- package/packages/core/src/interaction-flow.ts +159 -0
- package/packages/core/src/interaction-receipts.test.ts +361 -0
- package/packages/core/src/interaction-receipts.ts +336 -0
- package/packages/core/src/navigator.test.ts +34 -0
- package/packages/core/src/navigator.ts +74 -0
- package/packages/core/src/optimistic-projection-conformance.test.ts +75 -0
- package/packages/core/src/optimistic-projection-conformance.ts +61 -0
- package/packages/core/src/optimistic-projection-store.ts +71 -0
- package/packages/core/src/optimistic-projection.test.ts +118 -0
- package/packages/core/src/optimistic-projection.ts +194 -0
- package/packages/core/src/performance-metrics.test.ts +121 -0
- package/packages/core/src/performance-metrics.ts +241 -0
- package/packages/core/src/performance-qualification.test.ts +283 -0
- package/packages/core/src/performance-qualification.ts +360 -0
- package/packages/core/src/saved-machines.test.ts +80 -0
- package/packages/core/src/saved-machines.ts +53 -0
- package/packages/core/src/terminal-compact-capability.ts +31 -0
- package/packages/core/src/terminal-compact-cold.test.ts +87 -0
- package/packages/core/src/terminal-conformance.test.ts +48 -0
- package/packages/core/src/terminal-conformance.ts +405 -0
- package/packages/core/src/terminal-delivery-cache-lifetime.test.ts +167 -0
- package/packages/core/src/terminal-delivery-encode-cooperative.test.ts +140 -0
- package/packages/core/src/terminal-delivery.test.ts +1471 -0
- package/packages/core/src/terminal-delivery.ts +3154 -0
- package/packages/core/src/terminal-fnv64-wasm-bytes.ts +20 -0
- package/packages/core/src/terminal-fnv64-wasm.test.ts +61 -0
- package/packages/core/src/terminal-fnv64-wasm.ts +110 -0
- package/packages/core/src/terminal-replica-cold.test.ts +31 -0
- package/packages/core/src/terminal-replica-cooperative.test.ts +170 -0
- package/packages/core/src/terminal-replica-hash-cache.test.ts +283 -0
- package/packages/core/src/terminal-replica-hash-cache.ts +399 -0
- package/packages/core/src/terminal-replica.test.ts +347 -0
- package/packages/core/src/terminal-replica.ts +1149 -0
- package/packages/core/src/workspace-selection.test.ts +63 -0
- package/packages/core/src/workspace-selection.ts +97 -0
- package/packages/daemon/dist/attach.js +2 -2
- package/packages/daemon/dist/canonical.js +1 -1
- package/packages/daemon/dist/cli.js +38 -37
- package/packages/daemon/dist/command-center/actions/command-definitions.js +70 -0
- package/packages/daemon/dist/command-center/actions/dispatcher.js +228 -10
- package/packages/daemon/dist/command-center/actions/handlers/_project-context.js +3 -4
- package/packages/daemon/dist/command-center/actions/handlers/app-set-remote-access.js +6 -2
- package/packages/daemon/dist/command-center/actions/handlers/app-window-mutate.js +39 -0
- package/packages/daemon/dist/command-center/actions/handlers/config-actions.js +44 -20
- package/packages/daemon/dist/command-center/actions/handlers/daemon-shutdown.js +11 -1
- package/packages/daemon/dist/command-center/actions/handlers/fleet-lifecycle.js +56 -0
- package/packages/daemon/dist/command-center/actions/handlers/project-launch.js +16 -4
- package/packages/daemon/dist/command-center/actions/handlers/workspace-multiplexer.js +55 -0
- package/packages/daemon/dist/command-center/actions/handlers/workspace-open.js +89 -0
- package/packages/daemon/dist/command-center/actions/handlers/workspace-pane-create.js +34 -0
- package/packages/daemon/dist/command-center/actions/handlers/workspace-promote.js +34 -0
- package/packages/daemon/dist/command-center/actions/registry.js +126 -0
- package/packages/daemon/dist/command-center/actions/semantic-multiplexer-actions.js +16 -0
- package/packages/daemon/dist/command-center/agent-status-watch.js +170 -0
- package/packages/daemon/dist/command-center/daemon-fleet-facts-observer.js +395 -0
- package/packages/daemon/dist/command-center/discovery.js +262 -3
- package/packages/daemon/dist/command-center/owner-authority.js +46 -0
- package/packages/daemon/dist/command-center/pane-stream-issue.js +185 -0
- package/packages/daemon/dist/command-center/resources/agent-graph-overlay.js +234 -0
- package/packages/daemon/dist/command-center/resources/application-shell.js +641 -0
- package/packages/daemon/dist/command-center/resources/desktop-missions.js +132 -0
- package/packages/daemon/dist/command-center/resources/fleet-catalog.js +163 -0
- package/packages/daemon/dist/command-center/resources/fleet-resource-route.js +48 -0
- package/packages/daemon/dist/command-center/resources/startup-readiness-route.js +121 -0
- package/packages/daemon/dist/command-center/resources/startup-readiness.js +183 -0
- package/packages/daemon/dist/command-center/resources/terminal-native-backing-route.js +92 -0
- package/packages/daemon/dist/command-center/resources/terminal-runtime-inventory.js +24 -0
- package/packages/daemon/dist/command-center/resources/workspace-changes-authority.js +451 -0
- package/packages/daemon/dist/command-center/resources/workspace-changes-git.js +222 -0
- package/packages/daemon/dist/command-center/resources/workspace-files-authority.js +493 -0
- package/packages/daemon/dist/command-center/resources/workspace-missions-route.js +75 -0
- package/packages/daemon/dist/command-center/resources/workspace-resource-ids.js +69 -0
- package/packages/daemon/dist/command-center/resources/workspace-resource-routes.js +116 -0
- package/packages/daemon/dist/command-center/server.js +540 -51
- package/packages/daemon/dist/command-center/terminal-attachment-issue.js +191 -0
- package/packages/daemon/dist/command-center/workspace-resource-observer.js +453 -0
- package/packages/daemon/dist/command-center/ws-events.js +681 -63
- package/packages/daemon/dist/config.js +31 -25
- package/packages/daemon/dist/control/lifecycle.js +12 -2
- package/packages/daemon/dist/control/server.js +3 -3
- package/packages/daemon/dist/detect.js +23 -20
- package/packages/daemon/dist/doctor.js +25 -8
- package/packages/daemon/dist/index.js +20 -1
- package/packages/daemon/dist/init.js +24 -22
- package/packages/daemon/dist/inspect.js +33 -8
- package/packages/daemon/dist/launch.js +38 -26
- package/packages/daemon/dist/lib/__tests__/fixtures/workspace-state-writer.js +58 -0
- package/packages/daemon/dist/lib/agent-kind.js +24 -0
- package/packages/daemon/dist/lib/app-config.js +29 -1
- package/packages/daemon/dist/lib/app-window-kernel.js +556 -0
- package/packages/daemon/dist/lib/app-window-mutation.js +139 -0
- package/packages/daemon/dist/lib/app-window-repository.js +368 -0
- package/packages/daemon/dist/lib/app-window-state.js +415 -0
- package/packages/daemon/dist/lib/application-shell-app-windows.js +285 -0
- package/packages/daemon/dist/lib/auth/middleware.js +7 -3
- package/packages/daemon/dist/lib/bundled-tmux.js +93 -0
- package/packages/daemon/dist/lib/canonical-daemon-bootstrap.js +276 -0
- package/packages/daemon/dist/lib/canonical-daemon.js +507 -70
- package/packages/daemon/dist/lib/chrome-front-door.js +13 -0
- package/packages/daemon/dist/lib/cli-action-bridge.js +112 -82
- package/packages/daemon/dist/lib/command-registry.js +107 -0
- package/packages/daemon/dist/lib/config-context.js +34 -0
- package/packages/daemon/dist/lib/daemon-embed.js +1072 -363
- package/packages/daemon/dist/lib/daemon-session-monitor.js +194 -0
- package/packages/daemon/dist/lib/daemon.js +3 -47
- package/packages/daemon/dist/lib/directory-watcher.js +118 -0
- package/packages/daemon/dist/lib/embedded-daemon-lifecycle.js +102 -0
- package/packages/daemon/dist/lib/environment-identity.js +79 -0
- package/packages/daemon/dist/lib/fleet-agent-lifecycle.js +65 -0
- package/packages/daemon/dist/lib/fleet-lifecycle-authority.js +437 -0
- package/packages/daemon/dist/lib/headless-daemon.js +353 -0
- package/packages/daemon/dist/lib/launch-plan.js +96 -1
- package/packages/daemon/dist/lib/legacy-config-adapter.js +19 -0
- package/packages/daemon/dist/lib/legacy-config-migration.js +110 -0
- package/packages/daemon/dist/lib/legacy-theme-compat.js +25 -0
- package/packages/daemon/dist/lib/mission-projections.js +615 -0
- package/packages/daemon/dist/lib/mission-repository.js +775 -0
- package/packages/daemon/dist/lib/onboarding-marker.js +51 -0
- package/packages/daemon/dist/lib/pane-chrome.js +12 -0
- package/packages/daemon/dist/lib/pane-source-credentials.js +178 -0
- package/packages/daemon/dist/lib/pane-widget.js +126 -0
- package/packages/daemon/dist/lib/project-inspect.js +4 -0
- package/packages/daemon/dist/lib/project-onboard.js +17 -21
- package/packages/daemon/dist/lib/project-probe.js +28 -22
- package/packages/daemon/dist/lib/project-readiness-probe.js +425 -0
- package/packages/daemon/dist/lib/project-readiness.js +680 -0
- package/packages/daemon/dist/lib/project-registry.js +42 -9
- package/packages/daemon/dist/lib/project-resolver.js +245 -0
- package/packages/daemon/dist/lib/project-runtime-errors.js +21 -0
- package/packages/daemon/dist/lib/project-runtime-repository.js +876 -0
- package/packages/daemon/dist/lib/remote-daemon-info.js +21 -0
- package/packages/daemon/dist/lib/resolved-config.js +318 -0
- package/packages/daemon/dist/lib/runtime-namespace.js +136 -0
- package/packages/daemon/dist/lib/saved-machines.js +53 -0
- package/packages/daemon/dist/lib/semantic-resource-id.js +14 -0
- package/packages/daemon/dist/lib/session-monitor.js +29 -23
- package/packages/daemon/dist/lib/session-options.js +3 -10
- package/packages/daemon/dist/lib/ssh-daemon-transport.js +280 -0
- package/packages/daemon/dist/lib/state-home.js +2 -3
- package/packages/daemon/dist/lib/theme-mode.js +9 -0
- package/packages/daemon/dist/lib/tmux-authority-replacement.js +102 -0
- package/packages/daemon/dist/lib/tmux-external-interaction-observer.js +534 -0
- package/packages/daemon/dist/lib/tmux-interaction-options.js +126 -0
- package/packages/daemon/dist/lib/tmux-named-socket-fence.js +61 -0
- package/packages/daemon/dist/lib/tmux-terminal-color.js +37 -0
- package/packages/daemon/dist/lib/tui-binary.js +291 -28
- package/packages/daemon/dist/lib/unix-socket-authority.js +48 -0
- package/packages/daemon/dist/lib/widget-asset-policy.js +3 -0
- package/packages/daemon/dist/lib/widget-asset-store.js +166 -0
- package/packages/daemon/dist/lib/workspace-config-loader.js +280 -0
- package/packages/daemon/dist/lib/workspace-multiplexer-verbs.js +1138 -0
- package/packages/daemon/dist/lib/workspace-open-handoff.js +219 -0
- package/packages/daemon/dist/lib/workspace-open.js +740 -0
- package/packages/daemon/dist/lib/workspace-pane-creation.js +1137 -0
- package/packages/daemon/dist/lib/workspace-promotion.js +809 -0
- package/packages/daemon/dist/lib/workspace-registry.js +150 -23
- package/packages/daemon/dist/lib/workspace-state-repository.js +790 -0
- package/packages/daemon/dist/lib/workspace-state.js +765 -0
- package/packages/daemon/dist/lib/yaml-io.js +4 -18
- package/packages/daemon/dist/migrate.js +105 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/COPYING +13 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/lib/libevent_core-2.1.7.dylib +0 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/lib/libncursesw.6.dylib +0 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/lib/libutf8proc.3.2.3.dylib +0 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/licenses/libevent_core-2.1.7.dylib.txt +99 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/licenses/libncursesw.6.dylib.txt +29 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/licenses/libutf8proc.3.2.3.dylib.txt +93 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/manifest.json +21 -0
- package/packages/daemon/dist/native/tmux/darwin-arm64/tmux +0 -0
- package/packages/daemon/dist/restart.js +2 -2
- package/packages/daemon/dist/restore.js +10 -6
- package/packages/daemon/dist/schemas/inspect.js +9 -1
- package/packages/daemon/dist/schemas/registry.js +5 -19
- package/packages/daemon/dist/schemas/ws-events.js +4 -107
- package/packages/daemon/dist/send.js +106 -3
- package/packages/daemon/dist/server/index.js +4 -2
- package/packages/daemon/dist/server/pane-stream-upgrade.js +117 -0
- package/packages/daemon/dist/server/terminal-attachment-upgrade.js +121 -0
- package/packages/daemon/dist/status.js +11 -8
- package/packages/daemon/dist/stop.js +2 -2
- package/packages/daemon/dist/terminal/MonotonicPtyInput.js +130 -0
- package/packages/daemon/dist/terminal/NodePtyAdapter.js +37 -10
- package/packages/daemon/dist/terminal/PtyAdapter.js +18 -0
- package/packages/daemon/dist/terminal/__tests__/MockPtyAdapter.js +33 -6
- package/packages/daemon/dist/terminal/attachments/admission-util.js +87 -0
- package/packages/daemon/dist/terminal/attachments/agent-status-probe.js +325 -0
- package/packages/daemon/dist/terminal/attachments/direct-websocket.js +1069 -0
- package/packages/daemon/dist/terminal/attachments/grouped-tmux.js +213 -0
- package/packages/daemon/dist/terminal/attachments/lease-manager.js +655 -0
- package/packages/daemon/dist/terminal/attachments/native-runtime.js +1597 -0
- package/packages/daemon/dist/terminal/attachments/pty-tmux-attachment-launcher.js +607 -0
- package/packages/daemon/dist/terminal/attachments/semantic-pane-catalog.js +208 -0
- package/packages/daemon/dist/terminal/attachments/tmux-view-executor.js +825 -0
- package/packages/daemon/dist/terminal/mirror/__tests__/scripted-channel.js +90 -0
- package/packages/daemon/dist/terminal/mirror/__tests__/simulated-channel.js +147 -0
- package/packages/daemon/dist/terminal/mirror/control-channel.js +768 -0
- package/packages/daemon/dist/terminal/mirror/control-mode-ownership.js +30 -0
- package/packages/daemon/dist/terminal/mirror/flow-ledger.js +56 -0
- package/packages/daemon/dist/terminal/mirror/mirror-service.js +338 -0
- package/packages/daemon/dist/terminal/mirror/native-frozen-grid.js +128 -0
- package/packages/daemon/dist/terminal/mirror/native-grid-capture.js +179 -0
- package/packages/daemon/dist/terminal/mirror/native-grid-projection.js +99 -0
- package/packages/daemon/dist/terminal/mirror/native-grid-reader.js +81 -0
- package/packages/daemon/dist/terminal/mirror/native-grid-reflow.js +207 -0
- package/packages/daemon/dist/terminal/mirror/pane-feed.js +209 -0
- package/packages/daemon/dist/terminal/mirror/session-channel.js +2774 -0
- package/packages/daemon/dist/terminal/pane-stream/lease-manager.js +245 -0
- package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +2067 -0
- package/packages/daemon/dist/terminal/pane-stream/runtime.js +53 -0
- package/packages/daemon/dist/terminal/pane-stream/wire-ledger.js +110 -0
- package/packages/daemon/dist/terminal/protocol/chunk-bytes.js +21 -0
- package/packages/daemon/dist/terminal/protocol/control.js +111 -0
- package/packages/daemon/dist/terminal/protocol/input-coalescer.js +125 -0
- package/packages/daemon/dist/terminal/protocol/layout-parse.js +127 -0
- package/packages/daemon/dist/terminal/protocol/native-backing-client.js +44 -0
- package/packages/daemon/dist/terminal/protocol/pane-display-name.js +155 -0
- package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +294 -0
- package/packages/daemon/dist/terminal/protocol/workspace-tmux-adapter.js +213 -0
- package/packages/daemon/dist/terminal/session-runtime/authority-arbiter.js +266 -0
- package/packages/daemon/dist/terminal/session-runtime/causal-cell-ledger.js +278 -0
- package/packages/daemon/dist/terminal/session-runtime/interaction-receipt-facts.js +147 -0
- package/packages/daemon/dist/terminal/session-runtime/multiplexer-backend.js +116 -0
- package/packages/daemon/dist/terminal/session-runtime/native-seed-backing.js +41 -0
- package/packages/daemon/dist/terminal/session-runtime/registry.js +1390 -0
- package/packages/daemon/dist/terminal/session-runtime/runtime-observability.js +72 -0
- package/packages/daemon/dist/terminal/session-runtime/runtime-scheduler.js +16 -0
- package/packages/daemon/dist/terminal/session-runtime/runtime-trace-correlator.js +48 -0
- package/packages/daemon/dist/terminal/session-runtime/semantic-mutation-executor.js +353 -0
- package/packages/daemon/dist/terminal/session-runtime/semantic-mutation-resource-changes.js +32 -0
- package/packages/daemon/dist/terminal/session-runtime/terminal-delivery-hub.js +1770 -0
- package/packages/daemon/dist/terminal/session-runtime/terminal-delivery-observation-identity.js +9 -0
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-interpreter.js +582 -0
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-owner.js +568 -0
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-shadow-projections.js +21 -0
- package/packages/daemon/dist/terminal/session-runtime/transport-binding.js +474 -0
- package/packages/daemon/dist/terminal/session-runtime/xterm-terminal-interpreter-backend.js +515 -0
- package/packages/daemon/dist/tui/async-disposable-slot.js +57 -0
- package/packages/daemon/dist/tui/chrome/cheatsheet.js +211 -0
- package/packages/daemon/dist/tui/chrome/chip.js +25 -0
- package/packages/daemon/dist/tui/chrome/events.js +94 -0
- package/packages/daemon/dist/tui/chrome/front-door.js +16 -0
- package/packages/daemon/dist/tui/chrome/kitty-keys.js +54 -0
- package/packages/daemon/dist/tui/chrome/menu.js +251 -0
- package/packages/daemon/dist/tui/chrome/notify-prefs.js +13 -0
- package/packages/daemon/dist/tui/chrome/notify-state.js +77 -0
- package/packages/daemon/dist/tui/chrome/notify.js +711 -0
- package/packages/daemon/dist/tui/chrome/panels.js +60 -0
- package/packages/daemon/dist/tui/chrome/sidebar.js +183 -0
- package/packages/daemon/dist/tui/chrome/snapshot.js +290 -0
- package/packages/daemon/dist/tui/chrome/statusline.js +552 -0
- package/packages/daemon/dist/tui/chrome/updater.js +651 -0
- package/packages/daemon/dist/tui/chrome/welcome.js +117 -0
- package/packages/daemon/dist/tui/compiled.js +194 -0
- package/packages/daemon/dist/tui/detect/agent-resolution.js +60 -0
- package/packages/daemon/dist/tui/detect/classify.js +184 -0
- package/packages/daemon/dist/tui/detect/manifest-loader.js +242 -0
- package/packages/daemon/dist/tui/detect/manifest.js +126 -0
- package/packages/daemon/dist/tui/detect/manifests.js +535 -0
- package/packages/daemon/dist/tui/detect/process-tree.js +194 -0
- package/packages/daemon/dist/tui/detect/session-id.js +414 -0
- package/packages/daemon/dist/tui/detect/snapshot.js +81 -0
- package/packages/daemon/dist/tui/integrations/claude.js +151 -0
- package/packages/daemon/dist/tui/integrations/offer.js +134 -0
- package/packages/daemon/dist/tui/integrations/opencode.js +117 -0
- package/packages/daemon/dist/tui/mirror/ack-writer.js +78 -0
- package/packages/daemon/dist/tui/mirror/activity-surface-view.jsx +69 -0
- package/packages/daemon/dist/tui/mirror/activity-surface.js +271 -0
- package/packages/daemon/dist/tui/mirror/agent-chip.js +84 -0
- package/packages/daemon/dist/tui/mirror/agent-lifecycle.js +372 -0
- package/packages/daemon/dist/tui/mirror/agent-provisioning-executor.js +111 -0
- package/packages/daemon/dist/tui/mirror/agent-rows.js +126 -0
- package/packages/daemon/dist/tui/mirror/ansi-palette.js +3 -0
- package/packages/daemon/dist/tui/mirror/app-state.js +294 -0
- package/packages/daemon/dist/tui/mirror/application-keybindings.js +33 -0
- package/packages/daemon/dist/tui/mirror/application-shell-daemon-connection.js +270 -0
- package/packages/daemon/dist/tui/mirror/application-shell-daemon-session.js +45 -0
- package/packages/daemon/dist/tui/mirror/attention.js +59 -0
- package/packages/daemon/dist/tui/mirror/blit.js +120 -0
- package/packages/daemon/dist/tui/mirror/canonical-workspace-routing.js +54 -0
- package/packages/daemon/dist/tui/mirror/changes-surface-view.jsx +92 -0
- package/packages/daemon/dist/tui/mirror/changes-surface.js +210 -0
- package/packages/daemon/dist/tui/mirror/composite-layout.js +539 -0
- package/packages/daemon/dist/tui/mirror/configless-session-bootstrap.js +134 -0
- package/packages/daemon/dist/tui/mirror/dialog-model.js +183 -0
- package/packages/daemon/dist/tui/mirror/dialog-stack-core.js +232 -0
- package/packages/daemon/dist/tui/mirror/dialog-stack.js +2 -0
- package/packages/daemon/dist/tui/mirror/diff-model.js +332 -0
- package/packages/daemon/dist/tui/mirror/editor-buffer.js +47 -0
- package/packages/daemon/dist/tui/mirror/editor-open-policy.js +9 -0
- package/packages/daemon/dist/tui/mirror/features/changes/controller.js +454 -0
- package/packages/daemon/dist/tui/mirror/features/changes/feature.jsx +9 -0
- package/packages/daemon/dist/tui/mirror/features/dialogs/feature.jsx +123 -0
- package/packages/daemon/dist/tui/mirror/features/dialogs/session.js +160 -0
- package/packages/daemon/dist/tui/mirror/features/files/feature.jsx +15 -0
- package/packages/daemon/dist/tui/mirror/features/files/session.js +617 -0
- package/packages/daemon/dist/tui/mirror/features/home/feature.jsx +122 -0
- package/packages/daemon/dist/tui/mirror/features/missions-activity/contract.js +3 -0
- package/packages/daemon/dist/tui/mirror/features/missions-activity/feature.jsx +5 -0
- package/packages/daemon/dist/tui/mirror/features/missions-activity/session.js +377 -0
- package/packages/daemon/dist/tui/mirror/features/palette/buffer-geometry.js +36 -0
- package/packages/daemon/dist/tui/mirror/features/palette/contract.js +9 -0
- package/packages/daemon/dist/tui/mirror/features/palette/feature.js +3 -0
- package/packages/daemon/dist/tui/mirror/features/palette/session.js +394 -0
- package/packages/daemon/dist/tui/mirror/features/palette/surface.jsx +50 -0
- package/packages/daemon/dist/tui/mirror/features/performance-hud/contract.js +1 -0
- package/packages/daemon/dist/tui/mirror/features/performance-hud/feature.jsx +2 -0
- package/packages/daemon/dist/tui/mirror/features/performance-hud/session.js +148 -0
- package/packages/daemon/dist/tui/mirror/features/performance-hud/surface.jsx +64 -0
- package/packages/daemon/dist/tui/mirror/features/rich-preview/asset-loader.js +125 -0
- package/packages/daemon/dist/tui/mirror/features/rich-preview/contract.js +1 -0
- package/packages/daemon/dist/tui/mirror/features/rich-preview/feature.jsx +103 -0
- package/packages/daemon/dist/tui/mirror/features/rich-preview/session.js +310 -0
- package/packages/daemon/dist/tui/mirror/features/settings/catalog.js +10 -0
- package/packages/daemon/dist/tui/mirror/features/settings/contract.js +1 -0
- package/packages/daemon/dist/tui/mirror/features/settings/feature.js +2 -0
- package/packages/daemon/dist/tui/mirror/features/settings/session.js +309 -0
- package/packages/daemon/dist/tui/mirror/file-tree.js +283 -0
- package/packages/daemon/dist/tui/mirror/files-surface-view.jsx +92 -0
- package/packages/daemon/dist/tui/mirror/files-surface.js +237 -0
- package/packages/daemon/dist/tui/mirror/focus-projection.js +80 -0
- package/packages/daemon/dist/tui/mirror/folder-picker.js +114 -0
- package/packages/daemon/dist/tui/mirror/frame-coalescer.js +74 -0
- package/packages/daemon/dist/tui/mirror/home-model.js +125 -0
- package/packages/daemon/dist/tui/mirror/home-surface-model.js +297 -0
- package/packages/daemon/dist/tui/mirror/home-surface.jsx +110 -0
- package/packages/daemon/dist/tui/mirror/hosted.js +239 -0
- package/packages/daemon/dist/tui/mirror/input-lifecycle.js +147 -0
- package/packages/daemon/dist/tui/mirror/interaction-flow.js +41 -0
- package/packages/daemon/dist/tui/mirror/legacy/missions-workspace-loader.js +84 -0
- package/packages/daemon/dist/tui/mirror/menu-model.js +143 -0
- package/packages/daemon/dist/tui/mirror/missions-dashboard.js +261 -0
- package/packages/daemon/dist/tui/mirror/missions-render-fixtures.js +330 -0
- package/packages/daemon/dist/tui/mirror/missions-surface-controller.js +314 -0
- package/packages/daemon/dist/tui/mirror/missions-surface.jsx +239 -0
- package/packages/daemon/dist/tui/mirror/missions-workspace.js +1399 -0
- package/packages/daemon/dist/tui/mirror/multiplexer-action-executor.js +252 -0
- package/packages/daemon/dist/tui/mirror/open-tui-pane-stream-socket.js +32 -0
- package/packages/daemon/dist/tui/mirror/open-tui-verified-routing.js +95 -0
- package/packages/daemon/dist/tui/mirror/open-tui-workspace-runtime-port.js +1182 -0
- package/packages/daemon/dist/tui/mirror/palette-surface-adapter.js +384 -0
- package/packages/daemon/dist/tui/mirror/palette.js +522 -0
- package/packages/daemon/dist/tui/mirror/pane-frame-state.js +124 -0
- package/packages/daemon/dist/tui/mirror/pane-mirror.js +1 -0
- package/packages/daemon/dist/tui/mirror/pane-surface.jsx +950 -0
- package/packages/daemon/dist/tui/mirror/panel-host-reactive.js +13 -0
- package/packages/daemon/dist/tui/mirror/panel-host.js +242 -0
- package/packages/daemon/dist/tui/mirror/perf-tap.js +32 -0
- package/packages/daemon/dist/tui/mirror/performance-events.js +46 -0
- package/packages/daemon/dist/tui/mirror/recipe-components.jsx +135 -0
- package/packages/daemon/dist/tui/mirror/recipes.js +395 -0
- package/packages/daemon/dist/tui/mirror/reference-performance-trace.js +598 -0
- package/packages/daemon/dist/tui/mirror/renderer-commands.js +250 -0
- package/packages/daemon/dist/tui/mirror/resize-model.js +105 -0
- package/packages/daemon/dist/tui/mirror/resize-transaction.js +221 -0
- package/packages/daemon/dist/tui/mirror/rich-placement-projection.js +80 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-appearance-owner.js +176 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-bootstrap.js +51 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-daemon-authority-owner.js +235 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-daemon-authority.js +8 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-generation-starter.js +90 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-navigation.js +86 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-observer.js +301 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-roster.jsx +170 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-selection.js +83 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-transport.js +199 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +221 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents.js +30 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-catalog-owner.js +104 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-catalog.js +329 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-host-focus-control-binding.js +111 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-host-focus-control-capability.js +25 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-host-focus-presentation.js +129 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-host-focus-test-control.js +242 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-input-readiness.js +20 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-lifecycle.js +297 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-agent-navigation.js +61 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-agents.js +158 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-authority.js +289 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-catalog.js +223 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-navigation.js +164 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-sidebar.jsx +215 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-startup.js +35 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-optional-features.js +12 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-palette-command-owner.js +132 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-palette-input.js +69 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-palette-search-owner.js +67 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-pane-activity-owner.js +61 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-pane-menu-owner.js +82 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-pane-rename-input.js +41 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-pane-rename-owner.js +46 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-pending-terminal-input.js +74 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-performance-log.js +126 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-post-render-runtime.js +117 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-root-configuration.js +21 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-root-renderer.js +160 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-session-focus-owner.js +65 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-shell-binding.js +313 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-shell-home.jsx +113 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-shell-viewport.js +14 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-frame-readiness-owner.js +20 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-host-focus-owner.js +25 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-input-ingress.js +102 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-interaction-controller.js +1360 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-palette-owner.js +217 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-paste.js +22 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-renderer-sources.js +31 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-selection-owner.js +217 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-workspace-policy.js +193 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-workspace.jsx +1286 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-theme-repaint.js +34 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-transient-note-owner.js +29 -0
- package/packages/daemon/dist/tui/mirror/runtime/bounded-performance-record-writer.js +67 -0
- package/packages/daemon/dist/tui/mirror/runtime/causal-cell-client-ledger.js +281 -0
- package/packages/daemon/dist/tui/mirror/runtime/causal-cell-input-fixture.js +43 -0
- package/packages/daemon/dist/tui/mirror/runtime/changes-deferred-identity.js +17 -0
- package/packages/daemon/dist/tui/mirror/runtime/daemon-authority-rebind.js +128 -0
- package/packages/daemon/dist/tui/mirror/runtime/deferred-intent-queue.js +40 -0
- package/packages/daemon/dist/tui/mirror/runtime/editor-primitives.js +39 -0
- package/packages/daemon/dist/tui/mirror/runtime/file-content-primitives.js +9 -0
- package/packages/daemon/dist/tui/mirror/runtime/fleet-lifecycle-client.js +30 -0
- package/packages/daemon/dist/tui/mirror/runtime/focus-framebuffer-projection.js +53 -0
- package/packages/daemon/dist/tui/mirror/runtime/generation-bound-slot.js +23 -0
- package/packages/daemon/dist/tui/mirror/runtime/host-local-tmux-adapter.js +203 -0
- package/packages/daemon/dist/tui/mirror/runtime/hosted-tty-size-bridge.js +166 -0
- package/packages/daemon/dist/tui/mirror/runtime/latest-intent-fence.js +14 -0
- package/packages/daemon/dist/tui/mirror/runtime/local-view-controller.js +58 -0
- package/packages/daemon/dist/tui/mirror/runtime/modal-admission-coordinator.js +118 -0
- package/packages/daemon/dist/tui/mirror/runtime/modal-pointer-capture.js +12 -0
- package/packages/daemon/dist/tui/mirror/runtime/open-tui-generation-host.js +833 -0
- package/packages/daemon/dist/tui/mirror/runtime/open-tui-session-owner.js +178 -0
- package/packages/daemon/dist/tui/mirror/runtime/optional-feature-registry.js +178 -0
- package/packages/daemon/dist/tui/mirror/runtime/optional-feature-registry.typecheck.js +1 -0
- package/packages/daemon/dist/tui/mirror/runtime/palette-production-controller.js +126 -0
- package/packages/daemon/dist/tui/mirror/runtime/pane-resize-geometry.js +17 -0
- package/packages/daemon/dist/tui/mirror/runtime/pane-scoped-terminal-surface.jsx +67 -0
- package/packages/daemon/dist/tui/mirror/runtime/product-surface-policy.js +12 -0
- package/packages/daemon/dist/tui/mirror/runtime/renderer-cadence.js +7 -0
- package/packages/daemon/dist/tui/mirror/runtime/renderer-frame-optimizer.js +88 -0
- package/packages/daemon/dist/tui/mirror/runtime/renderer-output-transport.js +56 -0
- package/packages/daemon/dist/tui/mirror/runtime/resize-terminal-pane.js +91 -0
- package/packages/daemon/dist/tui/mirror/runtime/rich-preview-demand.js +14 -0
- package/packages/daemon/dist/tui/mirror/runtime/runtime-layout-presentation.js +111 -0
- package/packages/daemon/dist/tui/mirror/runtime/select-terminal-pane.js +90 -0
- package/packages/daemon/dist/tui/mirror/runtime/semantic-pane-publication.js +14 -0
- package/packages/daemon/dist/tui/mirror/runtime/semantic-shell-viewport-resize.js +56 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-copy-cursor.js +122 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-copy-selection.js +65 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-dimensions-owner.js +9 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-fast-lane-renderer-adapter.js +911 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-frame-readiness.js +84 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-host-focus.js +347 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-host-frame-diagnostics.js +37 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-input-adapter.js +50 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-layout-projection.js +105 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-link-opener.js +23 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-links.js +81 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-pane-input-router.js +83 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-row-projection-cache.js +88 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-selection-units.js +123 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-selection.js +202 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-session-handoff.js +89 -0
- package/packages/daemon/dist/tui/mirror/runtime/terminal-tool-readiness.js +64 -0
- package/packages/daemon/dist/tui/mirror/runtime/tool-resource-controller.js +636 -0
- package/packages/daemon/dist/tui/mirror/runtime/tool-resource-projection.js +169 -0
- package/packages/daemon/dist/tui/mirror/runtime/viewport-fit-coordinator.js +7 -0
- package/packages/daemon/dist/tui/mirror/runtime/workspace-terminal-fast-lane.js +498 -0
- package/packages/daemon/dist/tui/mirror/scrollbar-model.js +64 -0
- package/packages/daemon/dist/tui/mirror/search-model.js +62 -0
- package/packages/daemon/dist/tui/mirror/selection.js +264 -0
- package/packages/daemon/dist/tui/mirror/semantic-pane-render-source.js +581 -0
- package/packages/daemon/dist/tui/mirror/semantic-session-view.js +269 -0
- package/packages/daemon/dist/tui/mirror/settings-model.js +304 -0
- package/packages/daemon/dist/tui/mirror/shell-chrome.js +313 -0
- package/packages/daemon/dist/tui/mirror/size-truth.js +104 -0
- package/packages/daemon/dist/tui/mirror/spans.js +31 -0
- package/packages/daemon/dist/tui/mirror/status-grammar.js +29 -0
- package/packages/daemon/dist/tui/mirror/terminal-native-reflow.js +1 -0
- package/packages/daemon/dist/tui/mirror/terminal-retained-row.js +10 -0
- package/packages/daemon/dist/tui/mirror/terminal-surface.js +92 -0
- package/packages/daemon/dist/tui/mirror/terminal-text.js +52 -0
- package/packages/daemon/dist/tui/mirror/terminal-viewport.js +462 -0
- package/packages/daemon/dist/tui/mirror/theme.js +765 -0
- package/packages/daemon/dist/tui/mirror/ui/agent-badge.jsx +14 -0
- package/packages/daemon/dist/tui/mirror/ui/badge.jsx +15 -0
- package/packages/daemon/dist/tui/mirror/ui/button.jsx +66 -0
- package/packages/daemon/dist/tui/mirror/ui/dialog.jsx +8 -0
- package/packages/daemon/dist/tui/mirror/ui/index.js +16 -0
- package/packages/daemon/dist/tui/mirror/ui/key-hint.jsx +39 -0
- package/packages/daemon/dist/tui/mirror/ui/keyboard-router.jsx +46 -0
- package/packages/daemon/dist/tui/mirror/ui/menu.jsx +24 -0
- package/packages/daemon/dist/tui/mirror/ui/navigation-row.jsx +62 -0
- package/packages/daemon/dist/tui/mirror/ui/overlay-frame.jsx +54 -0
- package/packages/daemon/dist/tui/mirror/ui/overlay-host.jsx +33 -0
- package/packages/daemon/dist/tui/mirror/ui/overlay-list-row.jsx +29 -0
- package/packages/daemon/dist/tui/mirror/ui/overlay-model.js +45 -0
- package/packages/daemon/dist/tui/mirror/ui/state.js +171 -0
- package/packages/daemon/dist/tui/mirror/ui/status-bar.jsx +102 -0
- package/packages/daemon/dist/tui/mirror/ui/surface.jsx +45 -0
- package/packages/daemon/dist/tui/mirror/ui/tabs.jsx +88 -0
- package/packages/daemon/dist/tui/mirror/widget-fallback.js +52 -0
- package/packages/daemon/dist/tui/mirror/widget-surface-model.js +145 -0
- package/packages/daemon/dist/tui/mirror/widget-surface.jsx +31 -0
- package/packages/daemon/dist/tui/mirror/workspace/agent-terminal-canvas.js +77 -0
- package/packages/daemon/dist/tui/mirror/workspace/application-command-description.js +44 -0
- package/packages/daemon/dist/tui/mirror/workspace/application-root-controller.js +99 -0
- package/packages/daemon/dist/tui/mirror/workspace/application-shell-controller.js +292 -0
- package/packages/daemon/dist/tui/mirror/workspace/application-shell-surface-commands.js +28 -0
- package/packages/daemon/dist/tui/mirror/workspace/application-shell.js +97 -0
- package/packages/daemon/dist/tui/mirror/workspace/command-palette-surface-view.jsx +158 -0
- package/packages/daemon/dist/tui/mirror/workspace/command-palette-surface.js +353 -0
- package/packages/daemon/dist/tui/mirror/workspace/connection-feedback.js +99 -0
- package/packages/daemon/dist/tui/mirror/workspace/icons.js +43 -0
- package/packages/daemon/dist/tui/mirror/workspace/pane-action-menu-model.js +8 -0
- package/packages/daemon/dist/tui/mirror/workspace/pane-action-menu.jsx +15 -0
- package/packages/daemon/dist/tui/mirror/workspace/pane-frame.js +452 -0
- package/packages/daemon/dist/tui/mirror/workspace/terminal-pane-chrome.js +555 -0
- package/packages/daemon/dist/tui/mirror/workspace/terminal-pane-header.jsx +159 -0
- package/packages/daemon/dist/tui/mirror/workspace/terminal-scrollback.js +183 -0
- package/packages/daemon/dist/tui/mirror/workspace/terminal-window-strip.jsx +253 -0
- package/packages/daemon/dist/tui/mirror/workspace/text.js +27 -0
- package/packages/daemon/dist/tui/mirror/workspace/workbench-controller.js +34 -0
- package/packages/daemon/dist/tui/mirror/workspace/workbench-shell.js +286 -0
- package/packages/daemon/dist/tui/mirror/workspace-ui-state.js +885 -0
- package/packages/daemon/dist/tui/readiness.js +34 -0
- package/packages/daemon/dist/tui/team/entry.js +20 -0
- package/packages/daemon/dist/tui/team/fuzzy.js +141 -0
- package/packages/daemon/dist/tui/team/home.js +115 -0
- package/packages/daemon/dist/tui/team/input.js +21 -0
- package/packages/daemon/dist/tui/team/keymap.js +99 -0
- package/packages/daemon/dist/tui/team/mouse.js +17 -0
- package/packages/daemon/dist/tui/team/nav.js +33 -0
- package/packages/daemon/dist/tui/team/preview.js +35 -0
- package/packages/daemon/dist/tui/team/projects.js +159 -0
- package/packages/daemon/dist/tui/team/report.js +37 -0
- package/packages/daemon/dist/tui/team/sessions.js +292 -0
- package/packages/daemon/dist/tui/team/tree.js +42 -0
- package/packages/daemon/dist/tui/team/wait-receipts.js +153 -0
- package/packages/daemon/dist/tui/team/wait.js +90 -0
- package/packages/daemon/dist/validate.js +16 -10
- package/packages/daemon/dist/widgets/lib/watcher.js +5 -92
- package/packages/daemon/dist/widgets/resolve.js +12 -9
- package/packages/daemon/dist/widgets/setup/setup-model.js +7 -9
- package/packages/daemon/src/attach.ts +2 -2
- package/packages/daemon/src/canonical.ts +19 -2
- package/packages/daemon/src/cli.ts +39 -37
- package/packages/daemon/src/command-center/actions/command-definitions.ts +91 -0
- package/packages/daemon/src/command-center/actions/dispatcher.ts +291 -10
- package/packages/daemon/src/command-center/actions/errors.ts +40 -0
- package/packages/daemon/src/command-center/actions/handlers/_project-context.ts +4 -5
- package/packages/daemon/src/command-center/actions/handlers/app-set-remote-access.ts +9 -1
- package/packages/daemon/src/command-center/actions/handlers/app-window-mutate.ts +54 -0
- package/packages/daemon/src/command-center/actions/handlers/config-actions.ts +59 -29
- package/packages/daemon/src/command-center/actions/handlers/daemon-shutdown.ts +12 -0
- package/packages/daemon/src/command-center/actions/handlers/fleet-lifecycle.ts +104 -0
- package/packages/daemon/src/command-center/actions/handlers/project-launch.ts +20 -4
- package/packages/daemon/src/command-center/actions/handlers/workspace-multiplexer.ts +96 -0
- package/packages/daemon/src/command-center/actions/handlers/workspace-open.ts +141 -0
- package/packages/daemon/src/command-center/actions/handlers/workspace-pane-create.ts +48 -0
- package/packages/daemon/src/command-center/actions/handlers/workspace-promote.ts +48 -0
- package/packages/daemon/src/command-center/actions/registry.ts +200 -1
- package/packages/daemon/src/command-center/actions/semantic-multiplexer-actions.ts +26 -0
- package/packages/daemon/src/command-center/agent-status-watch.ts +222 -0
- package/packages/daemon/src/command-center/daemon-fleet-facts-observer.ts +465 -0
- package/packages/daemon/src/command-center/discovery.ts +310 -5
- package/packages/daemon/src/command-center/owner-authority.ts +118 -0
- package/packages/daemon/src/command-center/pane-stream-issue.ts +247 -0
- package/packages/daemon/src/command-center/resources/agent-graph-overlay.ts +270 -0
- package/packages/daemon/src/command-center/resources/application-shell.ts +909 -0
- package/packages/daemon/src/command-center/resources/desktop-missions.ts +171 -0
- package/packages/daemon/src/command-center/resources/fleet-catalog.ts +200 -0
- package/packages/daemon/src/command-center/resources/fleet-resource-route.ts +69 -0
- package/packages/daemon/src/command-center/resources/startup-readiness-route.ts +172 -0
- package/packages/daemon/src/command-center/resources/startup-readiness.ts +250 -0
- package/packages/daemon/src/command-center/resources/terminal-native-backing-route.ts +104 -0
- package/packages/daemon/src/command-center/resources/terminal-runtime-inventory.ts +45 -0
- package/packages/daemon/src/command-center/resources/workspace-changes-authority.ts +594 -0
- package/packages/daemon/src/command-center/resources/workspace-changes-git.ts +278 -0
- package/packages/daemon/src/command-center/resources/workspace-files-authority.ts +583 -0
- package/packages/daemon/src/command-center/resources/workspace-missions-route.ts +125 -0
- package/packages/daemon/src/command-center/resources/workspace-resource-ids.ts +81 -0
- package/packages/daemon/src/command-center/resources/workspace-resource-routes.ts +147 -0
- package/packages/daemon/src/command-center/server.ts +732 -59
- package/packages/daemon/src/command-center/terminal-attachment-issue.ts +257 -0
- package/packages/daemon/src/command-center/workspace-resource-observer.ts +560 -0
- package/packages/daemon/src/command-center/ws-events.ts +853 -66
- package/packages/daemon/src/config.ts +35 -25
- package/packages/daemon/src/control/lifecycle.ts +14 -2
- package/packages/daemon/src/control/server.ts +3 -3
- package/packages/daemon/src/detect.ts +25 -20
- package/packages/daemon/src/doctor.ts +24 -7
- package/packages/daemon/src/index.ts +244 -2
- package/packages/daemon/src/init.ts +23 -22
- package/packages/daemon/src/inspect.ts +41 -8
- package/packages/daemon/src/launch.ts +56 -28
- package/packages/daemon/src/lib/agent-kind.ts +23 -0
- package/packages/daemon/src/lib/app-config.ts +44 -5
- package/packages/daemon/src/lib/app-window-kernel.ts +777 -0
- package/packages/daemon/src/lib/app-window-mutation.ts +178 -0
- package/packages/daemon/src/lib/app-window-repository.ts +650 -0
- package/packages/daemon/src/lib/app-window-state.ts +599 -0
- package/packages/daemon/src/lib/application-shell-app-windows.ts +357 -0
- package/packages/daemon/src/lib/auth/middleware.ts +9 -3
- package/packages/daemon/src/lib/bundled-tmux.ts +103 -0
- package/packages/daemon/src/lib/canonical-daemon-bootstrap.ts +392 -0
- package/packages/daemon/src/lib/canonical-daemon.ts +695 -73
- package/packages/daemon/src/lib/chrome-front-door.ts +17 -0
- package/packages/daemon/src/lib/cli-action-bridge.ts +127 -91
- package/packages/daemon/src/lib/command-registry.ts +152 -0
- package/packages/daemon/src/lib/config-context.ts +61 -0
- package/packages/daemon/src/lib/daemon-embed.ts +1374 -390
- package/packages/daemon/src/lib/daemon-session-monitor.ts +274 -0
- package/packages/daemon/src/lib/daemon.ts +3 -46
- package/packages/daemon/src/lib/directory-watcher.ts +166 -0
- package/packages/daemon/src/lib/embedded-daemon-lifecycle.ts +106 -0
- package/packages/daemon/src/lib/environment-identity.ts +82 -0
- package/packages/daemon/src/lib/errors.ts +7 -1
- package/packages/daemon/src/lib/fleet-agent-lifecycle.ts +74 -0
- package/packages/daemon/src/lib/fleet-lifecycle-authority.ts +535 -0
- package/packages/daemon/src/lib/headless-daemon.ts +459 -0
- package/packages/daemon/src/lib/launch-plan.ts +123 -4
- package/packages/daemon/src/lib/legacy-config-adapter.ts +28 -0
- package/packages/daemon/src/lib/legacy-config-migration.ts +145 -0
- package/packages/daemon/src/lib/legacy-theme-compat.ts +40 -0
- package/packages/daemon/src/lib/mission-projections.ts +775 -0
- package/packages/daemon/src/lib/mission-repository.ts +1311 -0
- package/packages/daemon/src/lib/onboarding-marker.ts +51 -0
- package/packages/daemon/src/lib/pane-chrome.ts +14 -0
- package/packages/daemon/src/lib/pane-source-credentials.ts +200 -0
- package/packages/daemon/src/lib/pane-widget.ts +182 -0
- package/packages/daemon/src/lib/project-inspect.ts +4 -0
- package/packages/daemon/src/lib/project-onboard.ts +19 -24
- package/packages/daemon/src/lib/project-probe.ts +38 -30
- package/packages/daemon/src/lib/project-readiness-probe.ts +578 -0
- package/packages/daemon/src/lib/project-readiness.ts +1034 -0
- package/packages/daemon/src/lib/project-registry.ts +45 -9
- package/packages/daemon/src/lib/project-resolver.ts +350 -0
- package/packages/daemon/src/lib/project-runtime-errors.ts +41 -0
- package/packages/daemon/src/lib/project-runtime-repository.ts +1194 -0
- package/packages/daemon/src/lib/remote-daemon-info.ts +42 -0
- package/packages/daemon/src/lib/resolved-config.ts +425 -0
- package/packages/daemon/src/lib/runtime-namespace.ts +186 -0
- package/packages/daemon/src/lib/saved-machines.ts +63 -0
- package/packages/daemon/src/lib/semantic-resource-id.ts +17 -0
- package/packages/daemon/src/lib/session-monitor.ts +28 -20
- package/packages/daemon/src/lib/session-options.ts +3 -10
- package/packages/daemon/src/lib/ssh-daemon-transport.ts +303 -0
- package/packages/daemon/src/lib/state-home.ts +2 -3
- package/packages/daemon/src/lib/theme-mode.ts +11 -0
- package/packages/daemon/src/lib/tmux-authority-replacement.ts +119 -0
- package/packages/daemon/src/lib/tmux-external-interaction-observer.ts +632 -0
- package/packages/daemon/src/lib/tmux-interaction-options.ts +171 -0
- package/packages/daemon/src/lib/tmux-named-socket-fence.ts +66 -0
- package/packages/daemon/src/lib/tmux-terminal-color.ts +44 -0
- package/packages/daemon/src/lib/tui-binary.ts +354 -34
- package/packages/daemon/src/lib/unix-socket-authority.ts +65 -0
- package/packages/daemon/src/lib/widget-asset-policy.ts +3 -0
- package/packages/daemon/src/lib/widget-asset-store.ts +218 -0
- package/packages/daemon/src/lib/workspace-config-loader.ts +378 -0
- package/packages/daemon/src/lib/workspace-multiplexer-verbs.ts +1362 -0
- package/packages/daemon/src/lib/workspace-open-handoff.ts +340 -0
- package/packages/daemon/src/lib/workspace-open.ts +983 -0
- package/packages/daemon/src/lib/workspace-pane-creation.ts +1434 -0
- package/packages/daemon/src/lib/workspace-promotion.ts +1035 -0
- package/packages/daemon/src/lib/workspace-registry.ts +193 -25
- package/packages/daemon/src/lib/workspace-state-repository.ts +1204 -0
- package/packages/daemon/src/lib/workspace-state.ts +960 -0
- package/packages/daemon/src/lib/yaml-io.ts +8 -17
- package/packages/daemon/src/migrate.ts +134 -0
- package/packages/daemon/src/restart.ts +2 -2
- package/packages/daemon/src/restore.ts +14 -9
- package/packages/daemon/src/schemas/inspect.ts +9 -1
- package/packages/daemon/src/schemas/registry.ts +12 -22
- package/packages/daemon/src/schemas/ws-events.ts +14 -131
- package/packages/daemon/src/send.ts +138 -3
- package/packages/daemon/src/server/README.md +1 -1
- package/packages/daemon/src/server/index.ts +6 -2
- package/packages/daemon/src/server/pane-stream-upgrade.ts +141 -0
- package/packages/daemon/src/server/terminal-attachment-upgrade.ts +153 -0
- package/packages/daemon/src/status.ts +13 -8
- package/packages/daemon/src/stop.ts +2 -2
- package/packages/daemon/src/terminal/MonotonicPtyInput.ts +153 -0
- package/packages/daemon/src/terminal/NodePtyAdapter.ts +53 -11
- package/packages/daemon/src/terminal/PtyAdapter.ts +111 -3
- package/packages/daemon/src/terminal/README.md +34 -16
- package/packages/daemon/src/terminal/attachments/admission-util.ts +97 -0
- package/packages/daemon/src/terminal/attachments/agent-status-probe.ts +474 -0
- package/packages/daemon/src/terminal/attachments/direct-websocket.ts +1482 -0
- package/packages/daemon/src/terminal/attachments/grouped-tmux.ts +293 -0
- package/packages/daemon/src/terminal/attachments/lease-manager.ts +1042 -0
- package/packages/daemon/src/terminal/attachments/native-runtime.ts +2246 -0
- package/packages/daemon/src/terminal/attachments/pty-tmux-attachment-launcher.ts +750 -0
- package/packages/daemon/src/terminal/attachments/semantic-pane-catalog.ts +365 -0
- package/packages/daemon/src/terminal/attachments/tmux-view-executor.ts +1095 -0
- package/packages/daemon/src/terminal/mirror/control-channel.ts +1031 -0
- package/packages/daemon/src/terminal/mirror/control-mode-ownership.ts +34 -0
- package/packages/daemon/src/terminal/mirror/events.ts +164 -0
- package/packages/daemon/src/terminal/mirror/flow-ledger.ts +65 -0
- package/packages/daemon/src/terminal/mirror/mirror-service.ts +503 -0
- package/packages/daemon/src/terminal/mirror/native-frozen-grid.ts +154 -0
- package/packages/daemon/src/terminal/mirror/native-grid-capture.ts +235 -0
- package/packages/daemon/src/terminal/mirror/native-grid-projection.ts +125 -0
- package/packages/daemon/src/terminal/mirror/native-grid-reader.ts +109 -0
- package/packages/daemon/src/terminal/mirror/native-grid-reflow.ts +241 -0
- package/packages/daemon/src/terminal/mirror/pane-feed.ts +272 -0
- package/packages/daemon/src/terminal/mirror/session-channel.ts +3378 -0
- package/packages/daemon/src/terminal/mirror/trusted-inventory.ts +29 -0
- package/packages/daemon/src/terminal/pane-stream/lease-manager.ts +375 -0
- package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +2625 -0
- package/packages/daemon/src/terminal/pane-stream/runtime.ts +78 -0
- package/packages/daemon/src/terminal/pane-stream/wire-ledger.ts +155 -0
- package/packages/daemon/src/terminal/protocol/README.md +14 -0
- package/packages/daemon/src/terminal/protocol/chunk-bytes.ts +20 -0
- package/packages/daemon/src/terminal/protocol/control.ts +130 -0
- package/packages/daemon/src/terminal/protocol/input-coalescer.ts +131 -0
- package/packages/daemon/src/terminal/protocol/native-backing-client.ts +65 -0
- package/packages/daemon/src/terminal/protocol/pane-display-name.ts +192 -0
- package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +382 -0
- package/packages/daemon/src/terminal/protocol/workspace-tmux-adapter.ts +470 -0
- package/packages/daemon/src/terminal/session-runtime/authority-arbiter.ts +318 -0
- package/packages/daemon/src/terminal/session-runtime/causal-cell-ledger.ts +359 -0
- package/packages/daemon/src/terminal/session-runtime/interaction-receipt-facts.ts +173 -0
- package/packages/daemon/src/terminal/session-runtime/multiplexer-backend.ts +194 -0
- package/packages/daemon/src/terminal/session-runtime/native-seed-backing.ts +80 -0
- package/packages/daemon/src/terminal/session-runtime/registry.ts +2084 -0
- package/packages/daemon/src/terminal/session-runtime/runtime-observability.ts +267 -0
- package/packages/daemon/src/terminal/session-runtime/runtime-scheduler.ts +30 -0
- package/packages/daemon/src/terminal/session-runtime/runtime-trace-correlator.ts +58 -0
- package/packages/daemon/src/terminal/session-runtime/semantic-mutation-executor.ts +555 -0
- package/packages/daemon/src/terminal/session-runtime/semantic-mutation-resource-changes.ts +48 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-delivery-hub.ts +2324 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-delivery-observation-identity.ts +18 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-interpreter-backend.ts +78 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-interpreter.ts +765 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-owner.ts +698 -0
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-shadow-projections.ts +36 -0
- package/packages/daemon/src/terminal/session-runtime/transport-binding.ts +682 -0
- package/packages/daemon/src/terminal/session-runtime/xterm-headless-provenance.json +51 -0
- package/packages/daemon/src/terminal/session-runtime/xterm-terminal-interpreter-backend.ts +697 -0
- package/packages/daemon/src/tui/async-disposable-slot.ts +60 -0
- package/packages/daemon/src/tui/chrome/front-door.ts +11 -23
- package/packages/daemon/src/tui/chrome/sidebar.ts +11 -5
- package/packages/daemon/src/tui/chrome/statusline.ts +15 -6
- package/packages/daemon/src/tui/chrome/updater.ts +100 -3
- package/packages/daemon/src/tui/compiled.ts +132 -6
- package/packages/daemon/src/tui/detect/agent-resolution.ts +85 -0
- package/packages/daemon/src/tui/detect/process-tree.ts +17 -1
- package/packages/daemon/src/tui/detect/snapshot.ts +38 -3
- package/packages/daemon/src/tui/integrations/claude.ts +1 -0
- package/packages/daemon/src/tui/main.ts +56 -0
- package/packages/daemon/src/tui/mirror/RECIPES.md +59 -0
- package/packages/daemon/src/tui/mirror/SHELL_CHROME.md +39 -0
- package/packages/daemon/src/tui/mirror/THEME.md +48 -0
- package/packages/daemon/src/tui/mirror/__snapshots__/activity-surface-renderer.test.tsx.snap +42 -0
- package/packages/daemon/src/tui/mirror/__snapshots__/changes-terminal-surface-renderer.test.tsx.snap +267 -0
- package/packages/daemon/src/tui/mirror/__snapshots__/home-files-surface-renderer.test.tsx.snap +267 -0
- package/packages/daemon/src/tui/mirror/__snapshots__/missions-surface-renderer.test.tsx.snap +134 -0
- package/packages/daemon/src/tui/mirror/__snapshots__/recipes-gallery-renderer.test.tsx.snap +141 -0
- package/packages/daemon/src/tui/mirror/__snapshots__/shell-chrome-renderer.test.tsx.snap +134 -0
- package/packages/daemon/src/tui/mirror/activity-surface-view.tsx +169 -0
- package/packages/daemon/src/tui/mirror/activity-surface.ts +430 -0
- package/packages/daemon/src/tui/mirror/agent-lifecycle.ts +36 -5
- package/packages/daemon/src/tui/mirror/agent-provisioning-executor.ts +175 -0
- package/packages/daemon/src/tui/mirror/agent-rows.ts +34 -0
- package/packages/daemon/src/tui/mirror/ansi-palette.ts +3 -0
- package/packages/daemon/src/tui/mirror/app-state.ts +14 -1
- package/packages/daemon/src/tui/mirror/app.tsx +2 -7047
- package/packages/daemon/src/tui/mirror/application-keybindings.ts +54 -0
- package/packages/daemon/src/tui/mirror/application-shell-daemon-connection.ts +370 -0
- package/packages/daemon/src/tui/mirror/application-shell-daemon-session.ts +74 -0
- package/packages/daemon/src/tui/mirror/canonical-workspace-routing.ts +90 -0
- package/packages/daemon/src/tui/mirror/changes-surface-view.tsx +263 -0
- package/packages/daemon/src/tui/mirror/changes-surface.ts +338 -0
- package/packages/daemon/src/tui/mirror/composite-layout.ts +710 -0
- package/packages/daemon/src/tui/mirror/configless-session-bootstrap.ts +192 -0
- package/packages/daemon/src/tui/mirror/dialog-model.ts +2 -2
- package/packages/daemon/src/tui/mirror/dialog-stack-core.ts +264 -0
- package/packages/daemon/src/tui/mirror/dialog-stack.ts +2 -367
- package/packages/daemon/src/tui/mirror/editor-buffer.ts +9 -70
- package/packages/daemon/src/tui/mirror/editor-open-policy.ts +16 -0
- package/packages/daemon/src/tui/mirror/features/changes/contract.ts +84 -0
- package/packages/daemon/src/tui/mirror/features/changes/controller.ts +470 -0
- package/packages/daemon/src/tui/mirror/features/changes/feature.tsx +19 -0
- package/packages/daemon/src/tui/mirror/features/dialogs/contract.ts +64 -0
- package/packages/daemon/src/tui/mirror/features/dialogs/feature.tsx +227 -0
- package/packages/daemon/src/tui/mirror/features/dialogs/session.ts +186 -0
- package/packages/daemon/src/tui/mirror/features/files/feature.tsx +54 -0
- package/packages/daemon/src/tui/mirror/features/files/session.ts +694 -0
- package/packages/daemon/src/tui/mirror/features/home/feature.tsx +172 -0
- package/packages/daemon/src/tui/mirror/features/missions-activity/contract.ts +106 -0
- package/packages/daemon/src/tui/mirror/features/missions-activity/feature.tsx +19 -0
- package/packages/daemon/src/tui/mirror/features/missions-activity/session.ts +470 -0
- package/packages/daemon/src/tui/mirror/features/palette/buffer-geometry.ts +46 -0
- package/packages/daemon/src/tui/mirror/features/palette/contract.ts +121 -0
- package/packages/daemon/src/tui/mirror/features/palette/feature.ts +14 -0
- package/packages/daemon/src/tui/mirror/features/palette/session.ts +443 -0
- package/packages/daemon/src/tui/mirror/features/palette/surface.tsx +133 -0
- package/packages/daemon/src/tui/mirror/features/performance-hud/contract.ts +20 -0
- package/packages/daemon/src/tui/mirror/features/performance-hud/feature.tsx +8 -0
- package/packages/daemon/src/tui/mirror/features/performance-hud/session.ts +152 -0
- package/packages/daemon/src/tui/mirror/features/performance-hud/surface.tsx +137 -0
- package/packages/daemon/src/tui/mirror/features/rich-preview/asset-loader.ts +153 -0
- package/packages/daemon/src/tui/mirror/features/rich-preview/contract.ts +94 -0
- package/packages/daemon/src/tui/mirror/features/rich-preview/feature.tsx +141 -0
- package/packages/daemon/src/tui/mirror/features/rich-preview/session.ts +387 -0
- package/packages/daemon/src/tui/mirror/features/settings/catalog.ts +26 -0
- package/packages/daemon/src/tui/mirror/features/settings/contract.ts +41 -0
- package/packages/daemon/src/tui/mirror/features/settings/feature.ts +3 -0
- package/packages/daemon/src/tui/mirror/features/settings/session.ts +375 -0
- package/packages/daemon/src/tui/mirror/files-surface-view.tsx +242 -0
- package/packages/daemon/src/tui/mirror/files-surface.ts +371 -0
- package/packages/daemon/src/tui/mirror/focus-projection.ts +113 -0
- package/packages/daemon/src/tui/mirror/frame-coalescer.ts +87 -0
- package/packages/daemon/src/tui/mirror/home-model.ts +1 -1
- package/packages/daemon/src/tui/mirror/home-surface-model.ts +440 -0
- package/packages/daemon/src/tui/mirror/home-surface.tsx +254 -0
- package/packages/daemon/src/tui/mirror/hosted.ts +87 -6
- package/packages/daemon/src/tui/mirror/input-lifecycle.ts +248 -0
- package/packages/daemon/src/tui/mirror/interaction-flow.ts +83 -0
- package/packages/daemon/src/tui/mirror/legacy/missions-workspace-loader.ts +117 -0
- package/packages/daemon/src/tui/mirror/menu-model.ts +1 -14
- package/packages/daemon/src/tui/mirror/missions-dashboard.ts +451 -0
- package/packages/daemon/src/tui/mirror/missions-render-fixtures.ts +371 -0
- package/packages/daemon/src/tui/mirror/missions-surface-controller.ts +530 -0
- package/packages/daemon/src/tui/mirror/missions-surface.tsx +434 -0
- package/packages/daemon/src/tui/mirror/missions-workspace.ts +1996 -0
- package/packages/daemon/src/tui/mirror/multiplexer-action-executor.ts +393 -0
- package/packages/daemon/src/tui/mirror/open-tui-pane-stream-socket.ts +47 -0
- package/packages/daemon/src/tui/mirror/open-tui-verified-routing.ts +152 -0
- package/packages/daemon/src/tui/mirror/open-tui-workspace-runtime-port.ts +1451 -0
- package/packages/daemon/src/tui/mirror/palette-surface-adapter.ts +465 -0
- package/packages/daemon/src/tui/mirror/palette.ts +189 -28
- package/packages/daemon/src/tui/mirror/pane-frame-state.ts +209 -0
- package/packages/daemon/src/tui/mirror/pane-mirror.ts +11 -593
- package/packages/daemon/src/tui/mirror/pane-surface.tsx +858 -53
- package/packages/daemon/src/tui/mirror/panel-host-reactive.ts +17 -0
- package/packages/daemon/src/tui/mirror/panel-host.ts +384 -0
- package/packages/daemon/src/tui/mirror/perf-tap.ts +1 -148
- package/packages/daemon/src/tui/mirror/performance-events.ts +494 -0
- package/packages/daemon/src/tui/mirror/recipe-components.tsx +310 -0
- package/packages/daemon/src/tui/mirror/recipes-gallery.tsx +256 -0
- package/packages/daemon/src/tui/mirror/recipes.ts +543 -0
- package/packages/daemon/src/tui/mirror/reference-performance-trace.ts +727 -0
- package/packages/daemon/src/tui/mirror/renderer-commands.ts +344 -0
- package/packages/daemon/src/tui/mirror/resize-model.ts +74 -9
- package/packages/daemon/src/tui/mirror/resize-transaction.ts +342 -0
- package/packages/daemon/src/tui/mirror/rich-placement-projection.ts +173 -0
- package/packages/daemon/src/tui/mirror/runtime/__snapshots__/application-shell-view-renderer.test.tsx.snap +87 -0
- package/packages/daemon/src/tui/mirror/runtime/application-add-machine-dialog.tsx +88 -0
- package/packages/daemon/src/tui/mirror/runtime/application-appearance-owner.ts +229 -0
- package/packages/daemon/src/tui/mirror/runtime/application-bootstrap.ts +118 -0
- package/packages/daemon/src/tui/mirror/runtime/application-daemon-authority-owner.ts +259 -0
- package/packages/daemon/src/tui/mirror/runtime/application-daemon-authority.ts +16 -0
- package/packages/daemon/src/tui/mirror/runtime/application-entry.ts +226 -0
- package/packages/daemon/src/tui/mirror/runtime/application-generation-starter.ts +130 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-navigation.ts +174 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-observer.ts +363 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-roster.tsx +276 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-selection.ts +99 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-transport.ts +232 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +295 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents.ts +70 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-catalog-owner.ts +130 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-catalog.ts +451 -0
- package/packages/daemon/src/tui/mirror/runtime/application-host-focus-control-binding.ts +143 -0
- package/packages/daemon/src/tui/mirror/runtime/application-host-focus-control-capability.ts +46 -0
- package/packages/daemon/src/tui/mirror/runtime/application-host-focus-presentation.ts +176 -0
- package/packages/daemon/src/tui/mirror/runtime/application-host-focus-test-control.ts +313 -0
- package/packages/daemon/src/tui/mirror/runtime/application-input-readiness.ts +28 -0
- package/packages/daemon/src/tui/mirror/runtime/application-lifecycle.ts +374 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-agent-navigation.ts +91 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-agents.ts +199 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-authority.ts +337 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-catalog.ts +256 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-navigation.ts +177 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-sidebar.tsx +356 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-startup.ts +39 -0
- package/packages/daemon/src/tui/mirror/runtime/application-optional-features.ts +25 -0
- package/packages/daemon/src/tui/mirror/runtime/application-palette-command-owner.ts +162 -0
- package/packages/daemon/src/tui/mirror/runtime/application-palette-input.ts +108 -0
- package/packages/daemon/src/tui/mirror/runtime/application-palette-search-owner.ts +78 -0
- package/packages/daemon/src/tui/mirror/runtime/application-pane-activity-owner.ts +79 -0
- package/packages/daemon/src/tui/mirror/runtime/application-pane-menu-owner.ts +95 -0
- package/packages/daemon/src/tui/mirror/runtime/application-pane-rename-input.ts +61 -0
- package/packages/daemon/src/tui/mirror/runtime/application-pane-rename-owner.ts +50 -0
- package/packages/daemon/src/tui/mirror/runtime/application-pending-terminal-input.ts +117 -0
- package/packages/daemon/src/tui/mirror/runtime/application-performance-log.ts +166 -0
- package/packages/daemon/src/tui/mirror/runtime/application-post-render-runtime.ts +163 -0
- package/packages/daemon/src/tui/mirror/runtime/application-root-configuration.ts +47 -0
- package/packages/daemon/src/tui/mirror/runtime/application-root-renderer.ts +165 -0
- package/packages/daemon/src/tui/mirror/runtime/application-root-v2.tsx +632 -0
- package/packages/daemon/src/tui/mirror/runtime/application-session-focus-owner.ts +80 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-binding.ts +375 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-catalog.tsx +448 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-home.tsx +252 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-overlay-stack.tsx +37 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-overlays.tsx +381 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-sidebar.tsx +188 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-view.tsx +537 -0
- package/packages/daemon/src/tui/mirror/runtime/application-shell-viewport.ts +18 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-frame-readiness-owner.ts +33 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-host-focus-owner.ts +36 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-input-ingress.ts +139 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-interaction-controller.ts +1673 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-palette-owner.ts +302 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-paste.ts +33 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-renderer-sources.ts +33 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-selection-owner.ts +277 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-workspace-policy.ts +311 -0
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-workspace.tsx +1921 -0
- package/packages/daemon/src/tui/mirror/runtime/application-theme-repaint.ts +45 -0
- package/packages/daemon/src/tui/mirror/runtime/application-transient-note-owner.ts +42 -0
- package/packages/daemon/src/tui/mirror/runtime/bounded-performance-record-writer.ts +75 -0
- package/packages/daemon/src/tui/mirror/runtime/causal-cell-client-ledger.ts +406 -0
- package/packages/daemon/src/tui/mirror/runtime/causal-cell-input-fixture.ts +55 -0
- package/packages/daemon/src/tui/mirror/runtime/changes-deferred-identity.ts +34 -0
- package/packages/daemon/src/tui/mirror/runtime/daemon-authority-rebind.ts +163 -0
- package/packages/daemon/src/tui/mirror/runtime/deferred-intent-queue.ts +45 -0
- package/packages/daemon/src/tui/mirror/runtime/editor-primitives.ts +56 -0
- package/packages/daemon/src/tui/mirror/runtime/file-content-primitives.ts +8 -0
- package/packages/daemon/src/tui/mirror/runtime/fleet-lifecycle-client.ts +67 -0
- package/packages/daemon/src/tui/mirror/runtime/focus-framebuffer-projection.ts +71 -0
- package/packages/daemon/src/tui/mirror/runtime/generation-bound-slot.ts +23 -0
- package/packages/daemon/src/tui/mirror/runtime/host-local-tmux-adapter.ts +233 -0
- package/packages/daemon/src/tui/mirror/runtime/hosted-tty-size-bridge.ts +213 -0
- package/packages/daemon/src/tui/mirror/runtime/latest-intent-fence.ts +22 -0
- package/packages/daemon/src/tui/mirror/runtime/local-view-controller.ts +95 -0
- package/packages/daemon/src/tui/mirror/runtime/modal-admission-coordinator.ts +158 -0
- package/packages/daemon/src/tui/mirror/runtime/modal-pointer-capture.ts +23 -0
- package/packages/daemon/src/tui/mirror/runtime/open-tui-generation-host.ts +1003 -0
- package/packages/daemon/src/tui/mirror/runtime/open-tui-session-owner.ts +229 -0
- package/packages/daemon/src/tui/mirror/runtime/optional-feature-registry.ts +234 -0
- package/packages/daemon/src/tui/mirror/runtime/optional-feature-registry.typecheck.ts +14 -0
- package/packages/daemon/src/tui/mirror/runtime/palette-production-controller.ts +170 -0
- package/packages/daemon/src/tui/mirror/runtime/palette-production-overlay.tsx +65 -0
- package/packages/daemon/src/tui/mirror/runtime/pane-resize-geometry.ts +29 -0
- package/packages/daemon/src/tui/mirror/runtime/pane-scoped-terminal-surface.tsx +156 -0
- package/packages/daemon/src/tui/mirror/runtime/product-surface-policy.ts +16 -0
- package/packages/daemon/src/tui/mirror/runtime/renderer-cadence.ts +7 -0
- package/packages/daemon/src/tui/mirror/runtime/renderer-frame-optimizer.ts +92 -0
- package/packages/daemon/src/tui/mirror/runtime/renderer-output-transport.ts +57 -0
- package/packages/daemon/src/tui/mirror/runtime/resize-terminal-pane.ts +134 -0
- package/packages/daemon/src/tui/mirror/runtime/rich-preview-demand.ts +26 -0
- package/packages/daemon/src/tui/mirror/runtime/rich-preview-overlay.tsx +54 -0
- package/packages/daemon/src/tui/mirror/runtime/runtime-layout-presentation.ts +119 -0
- package/packages/daemon/src/tui/mirror/runtime/select-terminal-pane.ts +127 -0
- package/packages/daemon/src/tui/mirror/runtime/semantic-pane-publication.ts +26 -0
- package/packages/daemon/src/tui/mirror/runtime/semantic-shell-viewport-resize.ts +88 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-copy-cursor.ts +169 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-copy-selection.ts +79 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-dimensions-owner.ts +20 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-fast-lane-renderer-adapter.ts +1085 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-frame-readiness.ts +100 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-host-focus.ts +519 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-host-frame-diagnostics.ts +47 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-input-adapter.ts +69 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-layout-projection.ts +145 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-link-opener.ts +30 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-links.ts +97 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-pane-input-router.ts +95 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-row-projection-cache.ts +115 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-selection-units.ts +128 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-selection.ts +285 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-session-handoff.ts +143 -0
- package/packages/daemon/src/tui/mirror/runtime/terminal-tool-readiness.ts +72 -0
- package/packages/daemon/src/tui/mirror/runtime/tool-resource-controller.ts +799 -0
- package/packages/daemon/src/tui/mirror/runtime/tool-resource-projection.ts +223 -0
- package/packages/daemon/src/tui/mirror/runtime/viewport-fit-coordinator.ts +12 -0
- package/packages/daemon/src/tui/mirror/runtime/workspace-terminal-fast-lane.ts +597 -0
- package/packages/daemon/src/tui/mirror/selection.ts +0 -21
- package/packages/daemon/src/tui/mirror/semantic-pane-render-source.ts +786 -0
- package/packages/daemon/src/tui/mirror/semantic-session-view.ts +366 -0
- package/packages/daemon/src/tui/mirror/settings-model.ts +24 -66
- package/packages/daemon/src/tui/mirror/shell-chrome-view.tsx +346 -0
- package/packages/daemon/src/tui/mirror/shell-chrome.ts +509 -0
- package/packages/daemon/src/tui/mirror/sidebar.tsx +138 -35
- package/packages/daemon/src/tui/mirror/size-truth.ts +1 -1
- package/packages/daemon/src/tui/mirror/terminal-native-reflow.ts +1 -0
- package/packages/daemon/src/tui/mirror/terminal-retained-row.ts +24 -0
- package/packages/daemon/src/tui/mirror/terminal-surface.ts +157 -0
- package/packages/daemon/src/tui/mirror/terminal-surface.tsx +84 -0
- package/packages/daemon/src/tui/mirror/terminal-text.ts +54 -0
- package/packages/daemon/src/tui/mirror/terminal-viewport.ts +544 -0
- package/packages/daemon/src/tui/mirror/theme.ts +1069 -33
- package/packages/daemon/src/tui/mirror/ui/agent-badge.tsx +41 -0
- package/packages/daemon/src/tui/mirror/ui/badge.tsx +42 -0
- package/packages/daemon/src/tui/mirror/ui/button.tsx +127 -0
- package/packages/daemon/src/tui/mirror/ui/dialog.tsx +45 -0
- package/packages/daemon/src/tui/mirror/ui/index.ts +59 -0
- package/packages/daemon/src/tui/mirror/ui/key-hint.tsx +63 -0
- package/packages/daemon/src/tui/mirror/ui/keyboard-router.tsx +72 -0
- package/packages/daemon/src/tui/mirror/ui/menu.tsx +86 -0
- package/packages/daemon/src/tui/mirror/ui/navigation-row.tsx +114 -0
- package/packages/daemon/src/tui/mirror/ui/overlay-frame.tsx +117 -0
- package/packages/daemon/src/tui/mirror/ui/overlay-host.tsx +71 -0
- package/packages/daemon/src/tui/mirror/ui/overlay-list-row.tsx +68 -0
- package/packages/daemon/src/tui/mirror/ui/overlay-model.ts +75 -0
- package/packages/daemon/src/tui/mirror/ui/state.ts +218 -0
- package/packages/daemon/src/tui/mirror/ui/status-bar.tsx +208 -0
- package/packages/daemon/src/tui/mirror/ui/surface.tsx +71 -0
- package/packages/daemon/src/tui/mirror/ui/tabs.tsx +168 -0
- package/packages/daemon/src/tui/mirror/widget-fallback.ts +66 -0
- package/packages/daemon/src/tui/mirror/widget-surface-model.ts +170 -0
- package/packages/daemon/src/tui/mirror/widget-surface.tsx +79 -0
- package/packages/daemon/src/tui/mirror/workspace/README.md +79 -0
- package/packages/daemon/src/tui/mirror/workspace/__snapshots__/agent-terminal-canvas-renderer.test.tsx.snap +348 -0
- package/packages/daemon/src/tui/mirror/workspace/__snapshots__/application-shell-renderer.test.tsx.snap +134 -0
- package/packages/daemon/src/tui/mirror/workspace/__snapshots__/command-palette-surface-renderer.test.tsx.snap +255 -0
- package/packages/daemon/src/tui/mirror/workspace/__snapshots__/pane-frame-renderer.test.tsx.snap +525 -0
- package/packages/daemon/src/tui/mirror/workspace/__snapshots__/terminal-pane-header-polish-renderer.test.tsx.snap +31 -0
- package/packages/daemon/src/tui/mirror/workspace/__snapshots__/workbench-shell-renderer.test.tsx.snap +134 -0
- package/packages/daemon/src/tui/mirror/workspace/agent-terminal-canvas-view.tsx +61 -0
- package/packages/daemon/src/tui/mirror/workspace/agent-terminal-canvas.ts +131 -0
- package/packages/daemon/src/tui/mirror/workspace/application-command-description.ts +77 -0
- package/packages/daemon/src/tui/mirror/workspace/application-root-controller.ts +169 -0
- package/packages/daemon/src/tui/mirror/workspace/application-shell-controller.ts +430 -0
- package/packages/daemon/src/tui/mirror/workspace/application-shell-surface-commands.ts +64 -0
- package/packages/daemon/src/tui/mirror/workspace/application-shell-view.tsx +169 -0
- package/packages/daemon/src/tui/mirror/workspace/application-shell.ts +179 -0
- package/packages/daemon/src/tui/mirror/workspace/command-palette-surface-view.tsx +356 -0
- package/packages/daemon/src/tui/mirror/workspace/command-palette-surface.ts +557 -0
- package/packages/daemon/src/tui/mirror/workspace/connection-feedback.ts +105 -0
- package/packages/daemon/src/tui/mirror/workspace/icons.ts +56 -0
- package/packages/daemon/src/tui/mirror/workspace/pane-action-menu-model.ts +33 -0
- package/packages/daemon/src/tui/mirror/workspace/pane-action-menu.tsx +54 -0
- package/packages/daemon/src/tui/mirror/workspace/pane-frame.ts +675 -0
- package/packages/daemon/src/tui/mirror/workspace/pane-frame.tsx +490 -0
- package/packages/daemon/src/tui/mirror/workspace/terminal-pane-chrome-view.tsx +125 -0
- package/packages/daemon/src/tui/mirror/workspace/terminal-pane-chrome.ts +833 -0
- package/packages/daemon/src/tui/mirror/workspace/terminal-pane-header.tsx +326 -0
- package/packages/daemon/src/tui/mirror/workspace/terminal-scrollback.ts +220 -0
- package/packages/daemon/src/tui/mirror/workspace/terminal-window-strip.tsx +466 -0
- package/packages/daemon/src/tui/mirror/workspace/text.ts +28 -0
- package/packages/daemon/src/tui/mirror/workspace/workbench-controller.ts +75 -0
- package/packages/daemon/src/tui/mirror/workspace/workbench-dock-opentui.tsx +224 -0
- package/packages/daemon/src/tui/mirror/workspace/workbench-shell.ts +486 -0
- package/packages/daemon/src/tui/mirror/workspace/workbench-shell.tsx +95 -0
- package/packages/daemon/src/tui/mirror/workspace-ui-state.ts +1278 -0
- package/packages/daemon/src/tui/readiness.ts +52 -0
- package/packages/daemon/src/tui/team/entry.ts +23 -13
- package/packages/daemon/src/tui/team/home.ts +1 -1
- package/packages/daemon/src/tui/team/index.tsx +27 -8
- package/packages/daemon/src/tui/team/projects.ts +12 -0
- package/packages/daemon/src/tui/team/sessions.ts +43 -48
- package/packages/daemon/src/tui/team/wait-receipts.ts +192 -0
- package/packages/daemon/src/ui/pane-frame/fixture.ts +43 -0
- package/packages/daemon/src/ui/workbench-dock/fixture.ts +61 -0
- package/packages/daemon/src/validate.ts +21 -9
- package/packages/daemon/src/widgets/config/README.md +1 -1
- package/packages/daemon/src/widgets/config/index.tsx +15 -115
- package/packages/daemon/src/widgets/lib/watcher.ts +3 -130
- package/packages/daemon/src/widgets/resolve.ts +18 -9
- package/packages/daemon/src/widgets/setup/footer.tsx +0 -7
- package/packages/daemon/src/widgets/setup/index.tsx +18 -20
- package/packages/daemon/src/widgets/setup/review-panel.tsx +15 -10
- package/packages/daemon/src/widgets/setup/setup-model.ts +7 -10
- package/packages/daemon-client/package.json +42 -0
- package/packages/daemon-client/src/application-shell-session.test.ts +196 -0
- package/packages/daemon-client/src/application-shell-session.ts +450 -0
- package/packages/daemon-client/src/bootstrap-coordinator.test.ts +122 -0
- package/packages/daemon-client/src/bootstrap-coordinator.ts +293 -0
- package/packages/daemon-client/src/connection-supervisor.test.ts +109 -0
- package/packages/daemon-client/src/connection-supervisor.ts +216 -0
- package/packages/daemon-client/src/direct-application-shell-transport.test.ts +939 -0
- package/packages/daemon-client/src/direct-application-shell-transport.ts +1144 -0
- package/packages/daemon-client/src/first-latest-coordinator.test.ts +175 -0
- package/packages/daemon-client/src/first-latest-coordinator.ts +82 -0
- package/packages/daemon-client/src/generation-bound-store-timers.test.ts +74 -0
- package/packages/daemon-client/src/generation-bound-store.ts +908 -0
- package/packages/daemon-client/src/index.ts +25 -0
- package/packages/daemon-client/src/owner-action-client.test.ts +180 -0
- package/packages/daemon-client/src/owner-action-client.ts +113 -0
- package/packages/daemon-client/src/pane-stream-client.test.ts +2771 -0
- package/packages/daemon-client/src/pane-stream-client.ts +1665 -0
- package/packages/daemon-client/src/pane-stream-clock-calibration.test.ts +207 -0
- package/packages/daemon-client/src/pane-stream-clock-calibration.ts +161 -0
- package/packages/daemon-client/src/pane-stream-loopback-benchmark.test.ts +51 -0
- package/packages/daemon-client/src/push-resource-session.test.ts +509 -0
- package/packages/daemon-client/src/push-resource-session.ts +797 -0
- package/packages/daemon-client/src/resource-replica.test.ts +186 -0
- package/packages/daemon-client/src/resource-replica.ts +280 -0
- package/packages/daemon-client/src/runtime-resource-ledger.test.ts +18 -0
- package/packages/daemon-client/src/runtime-resource-ledger.ts +69 -0
- package/packages/daemon-client/src/terminal-fast-lane.test.ts +817 -0
- package/packages/daemon-client/src/terminal-fast-lane.ts +817 -0
- package/packages/daemon-client/src/terminal-fast-lane.typecheck.ts +16 -0
- package/packages/daemon-client/src/workspace-catalog-v2.test.ts +109 -0
- package/packages/daemon-client/src/workspace-catalog-v2.ts +66 -0
- package/packages/daemon-client/src/workspace-client-boundaries.test.ts +59 -0
- package/packages/daemon-client/src/workspace-client-conformance.ts +67 -0
- package/packages/daemon-client/src/workspace-client-operations.test.ts +47 -0
- package/packages/daemon-client/src/workspace-client-operations.ts +220 -0
- package/packages/daemon-client/src/workspace-client-types.ts +302 -0
- package/packages/daemon-client/src/workspace-client.test.ts +2752 -0
- package/packages/daemon-client/src/workspace-client.ts +1452 -0
- package/packages/daemon-client/src/workspace-event-supervisor.test.ts +1086 -0
- package/packages/daemon-client/src/workspace-event-supervisor.ts +719 -0
- package/packages/daemon-client/src/workspace-pane-client.test.ts +80 -0
- package/packages/daemon-client/src/workspace-pane-client.ts +63 -0
- package/packages/presentation/package.json +28 -0
- package/packages/presentation/src/index.ts +2 -0
- package/packages/presentation/src/pane-frame/action-state.ts +72 -0
- package/packages/presentation/src/pane-frame/index.ts +3 -0
- package/packages/presentation/src/pane-frame/model.ts +517 -0
- package/packages/presentation/src/pane-frame/presenter.tsx +335 -0
- package/packages/presentation/src/pane-frame/web-entry.tsx +22 -0
- package/packages/presentation/src/pane-frame/web-host-unstyled.tsx +280 -0
- package/packages/presentation/src/pane-frame/web-host.css +388 -0
- package/packages/presentation/src/pane-frame/web-host.tsx +3 -0
- package/packages/presentation/src/presentation-contract.test.ts +63 -0
- package/packages/presentation/src/workbench-dock/index.ts +2 -0
- package/packages/presentation/src/workbench-dock/navigation.ts +54 -0
- package/packages/presentation/src/workbench-dock/presenter.tsx +252 -0
- package/packages/presentation/src/workbench-dock/web-entry.tsx +19 -0
- package/packages/presentation/src/workbench-dock/web-host-unstyled.tsx +198 -0
- package/packages/presentation/src/workbench-dock/web-host.css +204 -0
- package/packages/presentation/src/workbench-dock/web-host.tsx +3 -0
- package/packages/tmux-bridge/src/index.ts +9 -1
- package/packages/tmux-bridge/src/panes.ts +4 -0
- package/packages/tmux-bridge/src/runner.test.ts +72 -0
- package/packages/tmux-bridge/src/runner.ts +43 -2
- package/packages/tmux-bridge/src/sessions.ts +8 -1
- package/scripts/postinstall.js +57 -6
- package/skill/SKILL.md +27 -28
- package/templates/agent-team-monorepo.yml +28 -30
- package/templates/agent-team-nextjs.yml +24 -26
- package/templates/agent-team.yml +24 -24
- package/templates/convex.yml +24 -16
- package/templates/default.yml +26 -21
- package/templates/go.yml +21 -14
- package/templates/missions.yml +25 -40
- package/templates/nextjs.yml +24 -16
- package/templates/python.yml +21 -14
- package/templates/vite.yml +21 -14
- package/bin/cli.ts +0 -1773
- package/packages/daemon/dist/__tests__/support.d.ts +0 -7
- package/packages/daemon/dist/agent-explain.d.ts +0 -52
- package/packages/daemon/dist/attach.d.ts +0 -3
- package/packages/daemon/dist/bin.d.ts +0 -2
- package/packages/daemon/dist/canonical.d.ts +0 -2
- package/packages/daemon/dist/cli.d.ts +0 -1
- package/packages/daemon/dist/command-center/actions/contract.d.ts +0 -1
- package/packages/daemon/dist/command-center/actions/dispatcher.d.ts +0 -27
- package/packages/daemon/dist/command-center/actions/errors.d.ts +0 -38
- package/packages/daemon/dist/command-center/actions/handlers/_project-context.d.ts +0 -12
- package/packages/daemon/dist/command-center/actions/handlers/_resolve-project.d.ts +0 -42
- package/packages/daemon/dist/command-center/actions/handlers/app-set-remote-access.d.ts +0 -23
- package/packages/daemon/dist/command-center/actions/handlers/config-actions.d.ts +0 -12
- package/packages/daemon/dist/command-center/actions/handlers/daemon-shutdown.d.ts +0 -6
- package/packages/daemon/dist/command-center/actions/handlers/project-activate.d.ts +0 -7
- package/packages/daemon/dist/command-center/actions/handlers/project-launch.d.ts +0 -18
- package/packages/daemon/dist/command-center/actions/handlers/project-open-terminal.d.ts +0 -32
- package/packages/daemon/dist/command-center/actions/handlers/project-restart.d.ts +0 -17
- package/packages/daemon/dist/command-center/actions/handlers/project-stop.d.ts +0 -24
- package/packages/daemon/dist/command-center/actions/handlers/terminal-respawn.d.ts +0 -24
- package/packages/daemon/dist/command-center/actions/handlers/terminal-stop.d.ts +0 -14
- package/packages/daemon/dist/command-center/actions/registry.d.ts +0 -35
- package/packages/daemon/dist/command-center/discovery.d.ts +0 -23
- package/packages/daemon/dist/command-center/index.d.ts +0 -10
- package/packages/daemon/dist/command-center/schemas.d.ts +0 -76
- package/packages/daemon/dist/command-center/server.d.ts +0 -26
- package/packages/daemon/dist/command-center/ws-events.d.ts +0 -59
- package/packages/daemon/dist/config.d.ts +0 -19
- package/packages/daemon/dist/control/client.d.ts +0 -23
- package/packages/daemon/dist/control/dispatch.d.ts +0 -34
- package/packages/daemon/dist/control/fanout.d.ts +0 -19
- package/packages/daemon/dist/control/frames.d.ts +0 -23
- package/packages/daemon/dist/control/lifecycle.d.ts +0 -45
- package/packages/daemon/dist/control/server.d.ts +0 -16
- package/packages/daemon/dist/control/verbs.d.ts +0 -11
- package/packages/daemon/dist/detect.d.ts +0 -15
- package/packages/daemon/dist/doctor.d.ts +0 -39
- package/packages/daemon/dist/embed.d.ts +0 -1
- package/packages/daemon/dist/index.d.ts +0 -3
- package/packages/daemon/dist/init.d.ts +0 -4
- package/packages/daemon/dist/inspect.d.ts +0 -54
- package/packages/daemon/dist/launch.d.ts +0 -23
- package/packages/daemon/dist/lib/active-projects.d.ts +0 -12
- package/packages/daemon/dist/lib/agent-discovery.d.ts +0 -68
- package/packages/daemon/dist/lib/app-config.d.ts +0 -253
- package/packages/daemon/dist/lib/app-settings.d.ts +0 -9
- package/packages/daemon/dist/lib/auth/auth-service.d.ts +0 -33
- package/packages/daemon/dist/lib/auth/middleware.d.ts +0 -16
- package/packages/daemon/dist/lib/auth/types.d.ts +0 -1
- package/packages/daemon/dist/lib/auth-token.d.ts +0 -1
- package/packages/daemon/dist/lib/authorship.d.ts +0 -62
- package/packages/daemon/dist/lib/canonical-daemon.d.ts +0 -21
- package/packages/daemon/dist/lib/cli-action-bridge.d.ts +0 -27
- package/packages/daemon/dist/lib/daemon-embed.d.ts +0 -31
- package/packages/daemon/dist/lib/daemon-watchdog.d.ts +0 -16
- package/packages/daemon/dist/lib/daemon.d.ts +0 -8
- package/packages/daemon/dist/lib/dot-path.d.ts +0 -2
- package/packages/daemon/dist/lib/errors.d.ts +0 -37
- package/packages/daemon/dist/lib/filesystem-browser.d.ts +0 -85
- package/packages/daemon/dist/lib/launch-plan.d.ts +0 -6
- package/packages/daemon/dist/lib/log.d.ts +0 -40
- package/packages/daemon/dist/lib/manifest-pack.d.ts +0 -79
- package/packages/daemon/dist/lib/output.d.ts +0 -9
- package/packages/daemon/dist/lib/project-init-runner.d.ts +0 -46
- package/packages/daemon/dist/lib/project-inspect.d.ts +0 -28
- package/packages/daemon/dist/lib/project-onboard.d.ts +0 -58
- package/packages/daemon/dist/lib/project-probe.d.ts +0 -35
- package/packages/daemon/dist/lib/project-registry.d.ts +0 -76
- package/packages/daemon/dist/lib/session-monitor.d.ts +0 -15
- package/packages/daemon/dist/lib/session-options.d.ts +0 -13
- package/packages/daemon/dist/lib/shell.d.ts +0 -5
- package/packages/daemon/dist/lib/sizes.d.ts +0 -16
- package/packages/daemon/dist/lib/skill-sync.d.ts +0 -60
- package/packages/daemon/dist/lib/slugify.d.ts +0 -4
- package/packages/daemon/dist/lib/state-home.d.ts +0 -2
- package/packages/daemon/dist/lib/terminals-store.d.ts +0 -27
- package/packages/daemon/dist/lib/tui-binary.d.ts +0 -57
- package/packages/daemon/dist/lib/update-check.d.ts +0 -118
- package/packages/daemon/dist/lib/update.d.ts +0 -65
- package/packages/daemon/dist/lib/workspace-registry.d.ts +0 -84
- package/packages/daemon/dist/lib/worktree.d.ts +0 -76
- package/packages/daemon/dist/lib/yaml-io.d.ts +0 -10
- package/packages/daemon/dist/ls.d.ts +0 -3
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +0 -34
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/PkgInfo +0 -1
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/AppIcon.icns +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +0 -139
- package/packages/daemon/dist/restart.d.ts +0 -4
- package/packages/daemon/dist/restore.d.ts +0 -113
- package/packages/daemon/dist/schemas/domain.d.ts +0 -1
- package/packages/daemon/dist/schemas/filesystem.d.ts +0 -29
- package/packages/daemon/dist/schemas/ide-config.d.ts +0 -1
- package/packages/daemon/dist/schemas/index.d.ts +0 -12
- package/packages/daemon/dist/schemas/inspect.d.ts +0 -53
- package/packages/daemon/dist/schemas/registry.d.ts +0 -35
- package/packages/daemon/dist/schemas/ws-events.d.ts +0 -72
- package/packages/daemon/dist/send.d.ts +0 -59
- package/packages/daemon/dist/server/index.d.ts +0 -10
- package/packages/daemon/dist/server/pty-bridge.d.ts +0 -174
- package/packages/daemon/dist/server/ws-route.d.ts +0 -74
- package/packages/daemon/dist/status.d.ts +0 -3
- package/packages/daemon/dist/stop.d.ts +0 -3
- package/packages/daemon/dist/terminal/NodePtyAdapter.d.ts +0 -44
- package/packages/daemon/dist/terminal/PtyAdapter.d.ts +0 -124
- package/packages/daemon/dist/terminal/__tests__/MockPtyAdapter.d.ts +0 -77
- package/packages/daemon/dist/types.d.ts +0 -2
- package/packages/daemon/dist/validate.d.ts +0 -4
- package/packages/daemon/dist/widgets/changes/index.jsx +0 -575
- package/packages/daemon/dist/widgets/config/index.d.ts +0 -1
- package/packages/daemon/dist/widgets/config/index.jsx +0 -395
- package/packages/daemon/dist/widgets/explorer/breadcrumbs.d.ts +0 -10
- package/packages/daemon/dist/widgets/explorer/breadcrumbs.jsx +0 -51
- package/packages/daemon/dist/widgets/explorer/footer.d.ts +0 -6
- package/packages/daemon/dist/widgets/explorer/footer.jsx +0 -11
- package/packages/daemon/dist/widgets/explorer/index.jsx +0 -393
- package/packages/daemon/dist/widgets/explorer/tree-model.d.ts +0 -13
- package/packages/daemon/dist/widgets/explorer/tree.d.ts +0 -13
- package/packages/daemon/dist/widgets/explorer/tree.jsx +0 -108
- package/packages/daemon/dist/widgets/lib/config-model.d.ts +0 -22
- package/packages/daemon/dist/widgets/lib/files.d.ts +0 -11
- package/packages/daemon/dist/widgets/lib/git.d.ts +0 -11
- package/packages/daemon/dist/widgets/lib/grammar.d.ts +0 -79
- package/packages/daemon/dist/widgets/lib/help-overlay.d.ts +0 -19
- package/packages/daemon/dist/widgets/lib/help-overlay.jsx +0 -69
- package/packages/daemon/dist/widgets/lib/pane-comms.d.ts +0 -28
- package/packages/daemon/dist/widgets/lib/theme.d.ts +0 -44
- package/packages/daemon/dist/widgets/lib/watcher.d.ts +0 -9
- package/packages/daemon/dist/widgets/preview/index.jsx +0 -371
- package/packages/daemon/dist/widgets/resolve.d.ts +0 -20
- package/packages/daemon/dist/widgets/setup/agent-naming.d.ts +0 -9
- package/packages/daemon/dist/widgets/setup/agent-naming.jsx +0 -81
- package/packages/daemon/dist/widgets/setup/config-tree.d.ts +0 -13
- package/packages/daemon/dist/widgets/setup/config-tree.jsx +0 -203
- package/packages/daemon/dist/widgets/setup/detect-panel.d.ts +0 -14
- package/packages/daemon/dist/widgets/setup/detect-panel.jsx +0 -48
- package/packages/daemon/dist/widgets/setup/field-editor.d.ts +0 -12
- package/packages/daemon/dist/widgets/setup/field-editor.jsx +0 -163
- package/packages/daemon/dist/widgets/setup/footer.d.ts +0 -17
- package/packages/daemon/dist/widgets/setup/footer.jsx +0 -64
- package/packages/daemon/dist/widgets/setup/index.d.ts +0 -1
- package/packages/daemon/dist/widgets/setup/index.jsx +0 -261
- package/packages/daemon/dist/widgets/setup/layout-picker.d.ts +0 -9
- package/packages/daemon/dist/widgets/setup/layout-picker.jsx +0 -69
- package/packages/daemon/dist/widgets/setup/setup-model.d.ts +0 -15
- package/packages/daemon/dist/widgets/sidebar/index.jsx +0 -462
- package/packages/daemon/src/tui/mirror/control-client.ts +0 -214
- package/packages/daemon/src/tui/mirror/control.ts +0 -97
- package/packages/daemon/src/tui/mirror/focus-border.ts +0 -57
- package/packages/daemon/src/tui/mirror/host-terminal.ts +0 -49
- package/packages/daemon/src/tui/mirror/input-coalescer.ts +0 -105
- package/packages/daemon/src/tui/mirror/session-mirror.ts +0 -724
- package/packages/daemon/src/widgets/setup/orchestrator-panel.tsx +0 -200
- package/scripts/build-cli.mjs +0 -101
- package/scripts/build-macos-notifier.mjs +0 -160
- package/scripts/build-tui.mjs +0 -65
- package/scripts/capture-codex-traffic.mjs +0 -195
- package/scripts/check-src-additions.sh +0 -74
- package/scripts/coverage-summary.mjs +0 -68
- package/scripts/install-git-hooks.js +0 -53
- package/scripts/merge-update-manifests.mjs +0 -58
- package/scripts/merge-update-manifests.test.mjs +0 -82
- package/scripts/pack-check-run.mjs +0 -83
- package/scripts/perf-mirror.mjs +0 -313
- package/scripts/prepublish-check.mjs +0 -80
- package/scripts/publish-tap.sh +0 -55
- package/scripts/stress-test.ts +0 -141
- package/scripts/verify-endpoints.sh +0 -90
- /package/packages/daemon/dist/{server/standalone.d.ts → terminal/mirror/events.js} +0 -0
- /package/packages/daemon/dist/{widgets/changes/index.d.ts → terminal/mirror/trusted-inventory.js} +0 -0
- /package/packages/daemon/dist/{widgets/explorer/index.d.ts → terminal/session-runtime/terminal-interpreter-backend.js} +0 -0
- /package/packages/daemon/dist/{widgets/preview/index.d.ts → tui/mirror/features/changes/contract.js} +0 -0
- /package/packages/daemon/dist/{widgets/sidebar/index.d.ts → tui/mirror/features/dialogs/contract.js} +0 -0
- /package/packages/daemon/src/{tui/mirror → terminal/protocol}/layout-parse.ts +0 -0
|
@@ -0,0 +1,3154 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TERMINAL_DELIVERY_CHUNK_BYTES,
|
|
3
|
+
TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES,
|
|
4
|
+
TERMINAL_DELIVERY_PROTOCOL_VERSION,
|
|
5
|
+
TerminalDeliveryEnvelopeSchemaZ,
|
|
6
|
+
TerminalDeliveryChunkSchemaZ,
|
|
7
|
+
TerminalDeliveryNegotiatedSchemaZ,
|
|
8
|
+
TerminalDeliveryNackSchemaZ,
|
|
9
|
+
TerminalSemanticDeliveryPayloadSchemaZ,
|
|
10
|
+
TerminalReplicaSnapshotSchemaZ,
|
|
11
|
+
TerminalReplicaRowSchemaZ,
|
|
12
|
+
TerminalReplicaCellSchemaZ,
|
|
13
|
+
TerminalReplicaPlacementSchemaZ,
|
|
14
|
+
type TerminalDeliveryAck,
|
|
15
|
+
type TerminalDeliveryChunk,
|
|
16
|
+
type TerminalDeliveryEnvelope,
|
|
17
|
+
type TerminalDeliveryNegotiated,
|
|
18
|
+
type TerminalDeliveryNegotiationResult,
|
|
19
|
+
type TerminalDeliveryNack,
|
|
20
|
+
type TerminalDeliveryOffer,
|
|
21
|
+
type TerminalReplicaPatchPayload,
|
|
22
|
+
type TerminalReplicaCell,
|
|
23
|
+
type TerminalReplicaColor,
|
|
24
|
+
type TerminalReplicaRow,
|
|
25
|
+
type TerminalReplicaSnapshot,
|
|
26
|
+
type TerminalSemanticDeliveryPayload,
|
|
27
|
+
} from "@tmux-ide/contracts";
|
|
28
|
+
import {
|
|
29
|
+
applyTerminalReplicaPatch,
|
|
30
|
+
freezeTerminalReplicaSnapshot,
|
|
31
|
+
hashTerminalReplicaSnapshot,
|
|
32
|
+
hashTerminalReplicaTombstone,
|
|
33
|
+
hashTerminalReplicaSnapshotCooperatively,
|
|
34
|
+
registerTerminalReplicaRowsDeltaHash,
|
|
35
|
+
terminalReplicaRowsEqual,
|
|
36
|
+
} from "./terminal-replica.ts";
|
|
37
|
+
import { grantCompactReplicaCapability } from "./terminal-compact-capability.ts";
|
|
38
|
+
import {
|
|
39
|
+
hashTerminalReplicaRowCooperatively,
|
|
40
|
+
hashTerminalReplicaRowRunsCooperatively,
|
|
41
|
+
isTerminalReplicaRowDeeplyFrozen,
|
|
42
|
+
primeTerminalReplicaRowHash,
|
|
43
|
+
TerminalReplicaRunEncodingCache,
|
|
44
|
+
} from "./terminal-replica-hash-cache.ts";
|
|
45
|
+
|
|
46
|
+
export class TerminalDeliveryStateTooLargeError extends Error {
|
|
47
|
+
readonly bytes: number;
|
|
48
|
+
|
|
49
|
+
constructor(bytes: number) {
|
|
50
|
+
super(
|
|
51
|
+
`Terminal delivery representation is ${bytes} bytes; maximum is ${TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES}`,
|
|
52
|
+
);
|
|
53
|
+
this.name = "TerminalDeliveryStateTooLargeError";
|
|
54
|
+
this.bytes = bytes;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function negotiateTerminalDelivery(
|
|
59
|
+
offer: TerminalDeliveryOffer,
|
|
60
|
+
generation: string,
|
|
61
|
+
deliveryNonce: string,
|
|
62
|
+
): TerminalDeliveryNegotiationResult {
|
|
63
|
+
if (!offer.protocolVersions.includes(TERMINAL_DELIVERY_PROTOCOL_VERSION))
|
|
64
|
+
return { accepted: false, reason: "protocol-version-mismatch" };
|
|
65
|
+
const encoding = (
|
|
66
|
+
["semantic-compact-v1", "semantic-v1", "ansi-diff-v1", "ansi-raw-v1"] as const
|
|
67
|
+
).find((value) => offer.encodings.includes(value));
|
|
68
|
+
if (!encoding) return { accepted: false, reason: "encoding-mismatch" };
|
|
69
|
+
if (offer.richPlacements && encoding !== "semantic-v1" && encoding !== "semantic-compact-v1")
|
|
70
|
+
return { accepted: false, reason: "unsupported-capability-combination" };
|
|
71
|
+
return {
|
|
72
|
+
accepted: true,
|
|
73
|
+
negotiated: {
|
|
74
|
+
protocolVersion: TERMINAL_DELIVERY_PROTOCOL_VERSION,
|
|
75
|
+
encoding,
|
|
76
|
+
...(encoding === "semantic-compact-v1" && offer.encodings.includes("semantic-v1")
|
|
77
|
+
? { fallbackEncoding: "semantic-v1" as const }
|
|
78
|
+
: {}),
|
|
79
|
+
richPlacements:
|
|
80
|
+
offer.richPlacements && (encoding === "semantic-v1" || encoding === "semantic-compact-v1"),
|
|
81
|
+
generation,
|
|
82
|
+
deliveryNonce,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function encodeSemanticTerminalUpdate(update: TerminalSemanticDeliveryPayload): Uint8Array {
|
|
88
|
+
const bytes = new TextEncoder().encode(
|
|
89
|
+
canonicalJson(TerminalSemanticDeliveryPayloadSchemaZ.parse(update)),
|
|
90
|
+
);
|
|
91
|
+
assertRepresentationSize(bytes);
|
|
92
|
+
return bytes;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Non-materializing exact counter for the legacy semantic JSON wire. It stops
|
|
97
|
+
* at cap + 1 without building the canonical JSON string or UTF-8 buffer; a
|
|
98
|
+
* candidate within the cap is subsequently encoded and cross-checked.
|
|
99
|
+
*/
|
|
100
|
+
export function preaccountSemanticTerminalUpdateBytes(
|
|
101
|
+
input: TerminalSemanticDeliveryPayload,
|
|
102
|
+
maximum = TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES,
|
|
103
|
+
): Readonly<{ exact: true; bytes: number } | { exact: false; atLeastBytes: number }> {
|
|
104
|
+
let bytes = 0;
|
|
105
|
+
const saturated = {};
|
|
106
|
+
const add = (amount: number) => {
|
|
107
|
+
bytes += amount;
|
|
108
|
+
if (bytes > maximum) throw saturated;
|
|
109
|
+
};
|
|
110
|
+
const stringBytes = (value: string) => {
|
|
111
|
+
add(2);
|
|
112
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
113
|
+
const code = value.charCodeAt(index);
|
|
114
|
+
if (
|
|
115
|
+
code === 0x22 ||
|
|
116
|
+
code === 0x5c ||
|
|
117
|
+
code === 0x08 ||
|
|
118
|
+
code === 0x0c ||
|
|
119
|
+
code === 0x0a ||
|
|
120
|
+
code === 0x0d ||
|
|
121
|
+
code === 0x09
|
|
122
|
+
)
|
|
123
|
+
add(2);
|
|
124
|
+
else if (code <= 0x1f) add(6);
|
|
125
|
+
else if (code <= 0x7f) add(1);
|
|
126
|
+
else if (code <= 0x7ff) add(2);
|
|
127
|
+
else if (code >= 0xd800 && code <= 0xdbff) {
|
|
128
|
+
const next = value.charCodeAt(index + 1);
|
|
129
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
130
|
+
add(4);
|
|
131
|
+
index += 1;
|
|
132
|
+
} else add(6);
|
|
133
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) add(6);
|
|
134
|
+
else add(3);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const visit = (value: unknown): void => {
|
|
138
|
+
if (value === null) {
|
|
139
|
+
add(4);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (typeof value === "string") {
|
|
143
|
+
stringBytes(value);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
147
|
+
add(String(value).length);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (Array.isArray(value)) {
|
|
151
|
+
add(1);
|
|
152
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
153
|
+
if (index > 0) add(1);
|
|
154
|
+
visit(value[index]);
|
|
155
|
+
}
|
|
156
|
+
add(1);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (typeof value !== "object" || value === undefined)
|
|
160
|
+
throw new TypeError("Unsupported canonical JSON value");
|
|
161
|
+
add(1);
|
|
162
|
+
const record = value as Record<string, unknown>;
|
|
163
|
+
const keys = Object.keys(record).sort();
|
|
164
|
+
let emitted = 0;
|
|
165
|
+
for (const key of keys) {
|
|
166
|
+
const member = record[key];
|
|
167
|
+
if (member === undefined) continue;
|
|
168
|
+
if (emitted > 0) add(1);
|
|
169
|
+
stringBytes(key);
|
|
170
|
+
add(1);
|
|
171
|
+
visit(member);
|
|
172
|
+
emitted += 1;
|
|
173
|
+
}
|
|
174
|
+
add(1);
|
|
175
|
+
};
|
|
176
|
+
try {
|
|
177
|
+
visit(input);
|
|
178
|
+
return Object.freeze({ exact: true, bytes });
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (error !== saturated) throw error;
|
|
181
|
+
return Object.freeze({ exact: false, atLeastBytes: maximum + 1 });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function decodeSemanticTerminalUpdate(bytes: Uint8Array): TerminalSemanticDeliveryPayload {
|
|
186
|
+
assertRepresentationSize(bytes);
|
|
187
|
+
return TerminalSemanticDeliveryPayloadSchemaZ.parse(
|
|
188
|
+
JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)),
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const COMPACT_SEMANTIC_KIND = "terminal-semantic-compact";
|
|
193
|
+
const COMPACT_SEMANTIC_VERSION = 1;
|
|
194
|
+
const COMPACT_MAX_DIMENSION = 4_096;
|
|
195
|
+
const COMPACT_MAX_ROWS = 10_000;
|
|
196
|
+
const COMPACT_MAX_RUNS = 1_000_000;
|
|
197
|
+
const COMPACT_MAX_EXPANDED_CELLS = 1_000_000;
|
|
198
|
+
const COMPACT_MAX_EXPANSION_RATIO = 64;
|
|
199
|
+
const COMPACT_MAX_PLACEMENTS = 4_096;
|
|
200
|
+
const COMPACT_MAX_STRING_BYTES = 4_096;
|
|
201
|
+
const UTF8_ENCODER = new TextEncoder();
|
|
202
|
+
const COMPACT_DEFAULT_COLOR = Object.freeze({ kind: "default" as const });
|
|
203
|
+
const compactCommitCapabilities = new WeakMap<
|
|
204
|
+
TerminalSemanticDeliveryPayload,
|
|
205
|
+
Readonly<{ snapshot: TerminalReplicaSnapshot | null; hash: string }>
|
|
206
|
+
>();
|
|
207
|
+
|
|
208
|
+
type CompactColor = 0 | readonly [1 | 2, number];
|
|
209
|
+
type CompactCellRun = [number, string, 0 | 1 | 2, CompactColor, CompactColor, number];
|
|
210
|
+
type CompactRow = readonly [0 | 1, readonly CompactCellRun[]];
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A negotiated exact semantic representation. It changes only the wire shape:
|
|
214
|
+
* decode always returns the same strict canonical payload and hash authority.
|
|
215
|
+
*/
|
|
216
|
+
export function encodeCompactSemanticTerminalUpdate(
|
|
217
|
+
input: TerminalSemanticDeliveryPayload,
|
|
218
|
+
): Uint8Array {
|
|
219
|
+
const update = TerminalSemanticDeliveryPayloadSchemaZ.parse(input);
|
|
220
|
+
// Compact payloads contain only arrays and primitives below this object.
|
|
221
|
+
// Insert root keys in canonical order so the native serializer emits exactly
|
|
222
|
+
// the same representation without recursively allocating JSON fragments.
|
|
223
|
+
const wire =
|
|
224
|
+
update.frame === "seed"
|
|
225
|
+
? {
|
|
226
|
+
f: "s",
|
|
227
|
+
k: COMPACT_SEMANTIC_KIND,
|
|
228
|
+
r: update.revision,
|
|
229
|
+
s: compactSnapshot(update.snapshot),
|
|
230
|
+
v: 1,
|
|
231
|
+
}
|
|
232
|
+
: update.frame === "patch"
|
|
233
|
+
? {
|
|
234
|
+
b: update.baseRevision,
|
|
235
|
+
f: "p",
|
|
236
|
+
k: COMPACT_SEMANTIC_KIND,
|
|
237
|
+
p: compactPatch(update.patch),
|
|
238
|
+
r: update.revision,
|
|
239
|
+
v: 1,
|
|
240
|
+
}
|
|
241
|
+
: {
|
|
242
|
+
b: update.baseRevision,
|
|
243
|
+
f: "t",
|
|
244
|
+
k: COMPACT_SEMANTIC_KIND,
|
|
245
|
+
r: update.revision,
|
|
246
|
+
t: update.tombstone.reason,
|
|
247
|
+
v: 1,
|
|
248
|
+
};
|
|
249
|
+
const bytes = UTF8_ENCODER.encode(JSON.stringify(wire));
|
|
250
|
+
assertRepresentationSize(bytes);
|
|
251
|
+
return bytes;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Keep the ordinary small-patch encoder unchanged. Large seeds are validated
|
|
255
|
+
// and emitted in256-cell slices, not parsed or stringified as one giant object.
|
|
256
|
+
export const TERMINAL_COMPACT_COOPERATIVE_SEED_CELLS = 8192;
|
|
257
|
+
const CompactSeedHeaderSchema =
|
|
258
|
+
/* @__PURE__ */ TerminalSemanticDeliveryPayloadSchemaZ.options[0].omit({ snapshot: true });
|
|
259
|
+
const CompactSeedMetadataSchema = /* @__PURE__ */ TerminalReplicaSnapshotSchemaZ.omit({
|
|
260
|
+
grid: true,
|
|
261
|
+
history: true,
|
|
262
|
+
placements: true,
|
|
263
|
+
});
|
|
264
|
+
const CompactRowHeaderSchema = /* @__PURE__ */ TerminalReplicaRowSchemaZ.omit({ cells: true });
|
|
265
|
+
const CompactCellSliceSchema = /* @__PURE__ */ TerminalReplicaCellSchemaZ.array();
|
|
266
|
+
|
|
267
|
+
export function terminalSemanticUpdateNeedsCooperativeEncoding(
|
|
268
|
+
input: TerminalSemanticDeliveryPayload,
|
|
269
|
+
): boolean {
|
|
270
|
+
return (
|
|
271
|
+
input?.frame === "seed" &&
|
|
272
|
+
Array.isArray(input.snapshot?.grid) &&
|
|
273
|
+
Array.isArray(input.snapshot?.history) &&
|
|
274
|
+
(input.snapshot.grid.length + input.snapshot.history.length) * input.snapshot.cols >=
|
|
275
|
+
TERMINAL_COMPACT_COOPERATIVE_SEED_CELLS
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Exact compact encoding with task checkpoints for large seeds. No validation
|
|
280
|
+
* bypass or producer capability: every header, row, cell and placement uses the
|
|
281
|
+
* same strict contract schemas as the synchronous encoder. Inputs must remain
|
|
282
|
+
* stable until completion (daemon canonical snapshots are immutable).
|
|
283
|
+
*/
|
|
284
|
+
export async function encodeCompactSemanticTerminalUpdateCooperatively(
|
|
285
|
+
input: TerminalSemanticDeliveryPayload,
|
|
286
|
+
options: { readonly yieldControl: () => Promise<void>; readonly signal?: AbortSignal },
|
|
287
|
+
): Promise<Uint8Array> {
|
|
288
|
+
const check = () => options.signal?.throwIfAborted();
|
|
289
|
+
check();
|
|
290
|
+
if (input.frame !== "seed" || !terminalSemanticUpdateNeedsCooperativeEncoding(input))
|
|
291
|
+
return encodeCompactSemanticTerminalUpdate(input);
|
|
292
|
+
const yieldControl = async () => {
|
|
293
|
+
check();
|
|
294
|
+
await options.yieldControl();
|
|
295
|
+
check();
|
|
296
|
+
};
|
|
297
|
+
const { snapshot: source, ...headerInput } = input;
|
|
298
|
+
const header = CompactSeedHeaderSchema.parse(headerInput);
|
|
299
|
+
const { grid, history, placements, ...metadataInput } = source;
|
|
300
|
+
const metadata = CompactSeedMetadataSchema.parse(metadataInput);
|
|
301
|
+
if (!Array.isArray(grid) || !Array.isArray(history) || !Array.isArray(placements))
|
|
302
|
+
throw new TypeError("Invalid compact seed arrays");
|
|
303
|
+
if (
|
|
304
|
+
metadata.cols > COMPACT_MAX_DIMENSION ||
|
|
305
|
+
metadata.rows > COMPACT_MAX_DIMENSION ||
|
|
306
|
+
grid.length !== metadata.rows
|
|
307
|
+
)
|
|
308
|
+
compactEncodingLimit();
|
|
309
|
+
let rowCount = 0,
|
|
310
|
+
cellCount = 0,
|
|
311
|
+
runCount = 0,
|
|
312
|
+
work = 0,
|
|
313
|
+
encodedWork = 0;
|
|
314
|
+
const chunks: Uint8Array[] = [];
|
|
315
|
+
let fragments: string[] = [],
|
|
316
|
+
characters = 0,
|
|
317
|
+
total = 0;
|
|
318
|
+
const flush = () => {
|
|
319
|
+
if (fragments.length === 0) return;
|
|
320
|
+
const chunk = UTF8_ENCODER.encode(fragments.join(""));
|
|
321
|
+
total += chunk.byteLength;
|
|
322
|
+
if (total > TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES)
|
|
323
|
+
throw new TerminalDeliveryStateTooLargeError(total);
|
|
324
|
+
chunks.push(chunk);
|
|
325
|
+
fragments = [];
|
|
326
|
+
characters = 0;
|
|
327
|
+
};
|
|
328
|
+
const write = (fragment: string) => {
|
|
329
|
+
fragments.push(fragment);
|
|
330
|
+
characters += fragment.length;
|
|
331
|
+
if (characters >= 32 * 1024) flush();
|
|
332
|
+
};
|
|
333
|
+
write(
|
|
334
|
+
'{"f":"s","k":' +
|
|
335
|
+
JSON.stringify(COMPACT_SEMANTIC_KIND) +
|
|
336
|
+
',"r":' +
|
|
337
|
+
header.revision +
|
|
338
|
+
',"s":[' +
|
|
339
|
+
metadata.cols +
|
|
340
|
+
"," +
|
|
341
|
+
metadata.rows +
|
|
342
|
+
",",
|
|
343
|
+
);
|
|
344
|
+
for (const rows of [grid, history]) {
|
|
345
|
+
write("[");
|
|
346
|
+
for (let index = 0; index < rows.length; index++) {
|
|
347
|
+
const { cells, ...rowHeaderInput } = rows[index]!;
|
|
348
|
+
const rowHeader = CompactRowHeaderSchema.parse(rowHeaderInput);
|
|
349
|
+
if (++rowCount > COMPACT_MAX_ROWS || !Array.isArray(cells) || cells.length !== metadata.cols)
|
|
350
|
+
compactEncodingLimit();
|
|
351
|
+
cellCount += cells.length;
|
|
352
|
+
if (cellCount > COMPACT_MAX_EXPANDED_CELLS) compactEncodingLimit();
|
|
353
|
+
if (index) write(",");
|
|
354
|
+
write("[" + (rowHeader.wrapped ? 1 : 0) + ",[");
|
|
355
|
+
let prior: CompactCellRun | null = null,
|
|
356
|
+
wroteRun = false;
|
|
357
|
+
const emitRun = () => {
|
|
358
|
+
if (!prior) return;
|
|
359
|
+
if (wroteRun) write(",");
|
|
360
|
+
write(JSON.stringify(prior));
|
|
361
|
+
wroteRun = true;
|
|
362
|
+
};
|
|
363
|
+
for (let offset = 0; offset < cells.length; offset += 256) {
|
|
364
|
+
const validated = CompactCellSliceSchema.parse(cells.slice(offset, offset + 256));
|
|
365
|
+
for (const cell of validated) {
|
|
366
|
+
const encoded = compactCell(cell);
|
|
367
|
+
if (prior && compactRunCellEqual(prior, encoded)) prior[0]++;
|
|
368
|
+
else {
|
|
369
|
+
emitRun();
|
|
370
|
+
if (++runCount > COMPACT_MAX_RUNS) compactEncodingLimit();
|
|
371
|
+
prior = encoded;
|
|
372
|
+
}
|
|
373
|
+
work++;
|
|
374
|
+
encodedWork += cell.grapheme.length + 64;
|
|
375
|
+
if (work >= 256 || encodedWork >= 32 * 1024) {
|
|
376
|
+
work = 0;
|
|
377
|
+
encodedWork = 0;
|
|
378
|
+
await yieldControl();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
emitRun();
|
|
383
|
+
write("]]");
|
|
384
|
+
}
|
|
385
|
+
write("],");
|
|
386
|
+
}
|
|
387
|
+
write(
|
|
388
|
+
JSON.stringify(compactCursor(metadata.cursor)) +
|
|
389
|
+
"," +
|
|
390
|
+
JSON.stringify(compactModes(metadata.modes)) +
|
|
391
|
+
",[",
|
|
392
|
+
);
|
|
393
|
+
if (placements.length > COMPACT_MAX_PLACEMENTS) compactEncodingLimit();
|
|
394
|
+
for (let index = 0; index < placements.length; index++) {
|
|
395
|
+
const placement = TerminalReplicaPlacementSchemaZ.parse(placements[index]);
|
|
396
|
+
if (index) write(",");
|
|
397
|
+
const encoded = JSON.stringify(compactPlacement(placement));
|
|
398
|
+
write(encoded);
|
|
399
|
+
encodedWork += encoded.length;
|
|
400
|
+
if (++work >= 256 || encodedWork >= 32 * 1024) {
|
|
401
|
+
work = 0;
|
|
402
|
+
encodedWork = 0;
|
|
403
|
+
await yieldControl();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
write("]," + JSON.stringify(compactBootstrap(metadata.bootstrap)) + '],"v":1}');
|
|
407
|
+
flush();
|
|
408
|
+
// No full-size JSON string or final synchronous whole-object serialization.
|
|
409
|
+
const bytes = new Uint8Array(total);
|
|
410
|
+
let offset = 0,
|
|
411
|
+
copied = 0;
|
|
412
|
+
for (const chunk of chunks) {
|
|
413
|
+
bytes.set(chunk, offset);
|
|
414
|
+
offset += chunk.byteLength;
|
|
415
|
+
copied += chunk.byteLength;
|
|
416
|
+
if (copied >= 256 * 1024) {
|
|
417
|
+
copied = 0;
|
|
418
|
+
await yieldControl();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
check();
|
|
422
|
+
return bytes;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export function decodeCompactSemanticTerminalUpdate(
|
|
426
|
+
bytes: Uint8Array,
|
|
427
|
+
): TerminalSemanticDeliveryPayload {
|
|
428
|
+
return decodeCompactSemanticTerminalUpdateInternal(bytes);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function decodeCompactSemanticTerminalUpdateInternal(
|
|
432
|
+
bytes: Uint8Array,
|
|
433
|
+
suppliedBudget?: CompactDecodeBudget,
|
|
434
|
+
): TerminalSemanticDeliveryPayload {
|
|
435
|
+
assertRepresentationSize(bytes);
|
|
436
|
+
const wire: unknown = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
437
|
+
const frame =
|
|
438
|
+
wire !== null && typeof wire === "object" && !Array.isArray(wire)
|
|
439
|
+
? (wire as Record<string, unknown>).f
|
|
440
|
+
: null;
|
|
441
|
+
if (
|
|
442
|
+
!recordWithKeys(
|
|
443
|
+
wire,
|
|
444
|
+
frame === "s"
|
|
445
|
+
? ["v", "k", "f", "r", "s"]
|
|
446
|
+
: ["v", "k", "f", "b", "r", frame === "p" ? "p" : "t"],
|
|
447
|
+
)
|
|
448
|
+
)
|
|
449
|
+
throw new TypeError("Invalid compact semantic envelope");
|
|
450
|
+
if (wire.v !== COMPACT_SEMANTIC_VERSION || wire.k !== COMPACT_SEMANTIC_KIND)
|
|
451
|
+
throw new TypeError("Unsupported compact semantic version");
|
|
452
|
+
const revision = compactInteger(wire.r, 0, Number.MAX_SAFE_INTEGER, "revision");
|
|
453
|
+
const budget = suppliedBudget ?? compactDecodeBudget(bytes.byteLength);
|
|
454
|
+
let update: TerminalSemanticDeliveryPayload;
|
|
455
|
+
if (wire.f === "s") {
|
|
456
|
+
update = { frame: "seed", revision, snapshot: expandSnapshot(wire.s, budget) };
|
|
457
|
+
} else {
|
|
458
|
+
const baseRevision = compactInteger(wire.b, 0, Number.MAX_SAFE_INTEGER, "base revision");
|
|
459
|
+
if (revision <= baseRevision) throw new TypeError("Compact semantic revision did not advance");
|
|
460
|
+
if (wire.f === "p")
|
|
461
|
+
update = {
|
|
462
|
+
frame: "patch",
|
|
463
|
+
baseRevision,
|
|
464
|
+
revision,
|
|
465
|
+
patch: expandPatch(wire.p, budget),
|
|
466
|
+
};
|
|
467
|
+
else if (
|
|
468
|
+
wire.f === "t" &&
|
|
469
|
+
["pane-closed", "session-restarted", "runtime-disposed"].includes(String(wire.t))
|
|
470
|
+
)
|
|
471
|
+
update = {
|
|
472
|
+
frame: "tombstone",
|
|
473
|
+
baseRevision,
|
|
474
|
+
revision,
|
|
475
|
+
tombstone: Object.freeze({
|
|
476
|
+
reason: wire.t as "pane-closed" | "session-restarted" | "runtime-disposed",
|
|
477
|
+
}),
|
|
478
|
+
};
|
|
479
|
+
else throw new TypeError("Invalid compact semantic frame");
|
|
480
|
+
}
|
|
481
|
+
return Object.freeze(update);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function compactDecodeBudget(bytes: number): CompactDecodeBudget {
|
|
485
|
+
return {
|
|
486
|
+
rows: 0,
|
|
487
|
+
runs: 0,
|
|
488
|
+
cells: 0,
|
|
489
|
+
placements: 0,
|
|
490
|
+
maxCells: Math.min(COMPACT_MAX_EXPANDED_CELLS, bytes * COMPACT_MAX_EXPANSION_RATIO),
|
|
491
|
+
rowCache: new Map(),
|
|
492
|
+
reusedRows: 0,
|
|
493
|
+
allocatedCells: 0,
|
|
494
|
+
canonicalUtf8Allocations: 0,
|
|
495
|
+
canonicalUtf8Bytes: 0,
|
|
496
|
+
validatedCellAllocations: 0,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function compactSnapshot(snapshot: TerminalReplicaSnapshot): readonly unknown[] {
|
|
501
|
+
const budget: CompactDecodeBudget = {
|
|
502
|
+
rows: 0,
|
|
503
|
+
runs: 0,
|
|
504
|
+
cells: 0,
|
|
505
|
+
placements: 0,
|
|
506
|
+
maxCells: COMPACT_MAX_EXPANDED_CELLS,
|
|
507
|
+
reusedRows: 0,
|
|
508
|
+
allocatedCells: 0,
|
|
509
|
+
canonicalUtf8Allocations: 0,
|
|
510
|
+
canonicalUtf8Bytes: 0,
|
|
511
|
+
validatedCellAllocations: 0,
|
|
512
|
+
};
|
|
513
|
+
if (
|
|
514
|
+
snapshot.cols > COMPACT_MAX_DIMENSION ||
|
|
515
|
+
snapshot.rows > COMPACT_MAX_DIMENSION ||
|
|
516
|
+
snapshot.grid.length !== snapshot.rows
|
|
517
|
+
)
|
|
518
|
+
compactEncodingLimit();
|
|
519
|
+
return [
|
|
520
|
+
snapshot.cols,
|
|
521
|
+
snapshot.rows,
|
|
522
|
+
compactRows(snapshot.grid, snapshot.cols, budget),
|
|
523
|
+
compactRows(snapshot.history, snapshot.cols, budget),
|
|
524
|
+
compactCursor(snapshot.cursor),
|
|
525
|
+
compactModes(snapshot.modes),
|
|
526
|
+
compactPlacements(snapshot.placements, budget),
|
|
527
|
+
compactBootstrap(snapshot.bootstrap),
|
|
528
|
+
];
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function compactPatch(patch: TerminalReplicaPatchPayload): readonly unknown[] {
|
|
532
|
+
const budget: CompactDecodeBudget = {
|
|
533
|
+
rows: 0,
|
|
534
|
+
runs: 0,
|
|
535
|
+
cells: 0,
|
|
536
|
+
placements: 0,
|
|
537
|
+
maxCells: COMPACT_MAX_EXPANDED_CELLS,
|
|
538
|
+
reusedRows: 0,
|
|
539
|
+
allocatedCells: 0,
|
|
540
|
+
canonicalUtf8Allocations: 0,
|
|
541
|
+
canonicalUtf8Bytes: 0,
|
|
542
|
+
validatedCellAllocations: 0,
|
|
543
|
+
};
|
|
544
|
+
const cols = patch.dimensions?.cols ?? null;
|
|
545
|
+
if (
|
|
546
|
+
patch.dimensions &&
|
|
547
|
+
(patch.dimensions.cols > COMPACT_MAX_DIMENSION || patch.dimensions.rows > COMPACT_MAX_DIMENSION)
|
|
548
|
+
)
|
|
549
|
+
compactEncodingLimit();
|
|
550
|
+
if (patch.rows.length > COMPACT_MAX_DIMENSION) compactEncodingLimit();
|
|
551
|
+
return [
|
|
552
|
+
patch.dimensions ? [patch.dimensions.cols, patch.dimensions.rows] : null,
|
|
553
|
+
patch.rows.map(({ index, row }) => [index, compactRow(row, cols, budget)]),
|
|
554
|
+
patch.history ? compactRows(patch.history, cols, budget) : null,
|
|
555
|
+
patch.historyDelta
|
|
556
|
+
? [patch.historyDelta.trim, compactRows(patch.historyDelta.append, cols, budget)]
|
|
557
|
+
: null,
|
|
558
|
+
patch.cursor ? compactCursor(patch.cursor) : null,
|
|
559
|
+
patch.modes ? compactModes(patch.modes) : null,
|
|
560
|
+
patch.placements ? compactPlacements(patch.placements, budget) : null,
|
|
561
|
+
patch.bootstrap ? compactBootstrap(patch.bootstrap) : null,
|
|
562
|
+
];
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function compactRows(
|
|
566
|
+
rows: readonly TerminalReplicaRow[],
|
|
567
|
+
cols: number | null,
|
|
568
|
+
budget: CompactDecodeBudget,
|
|
569
|
+
): CompactRow[] {
|
|
570
|
+
return rows.map((row) => compactRow(row, cols, budget));
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function compactRow(
|
|
574
|
+
row: TerminalReplicaRow,
|
|
575
|
+
cols: number | null,
|
|
576
|
+
budget: CompactDecodeBudget,
|
|
577
|
+
): CompactRow {
|
|
578
|
+
if (++budget.rows > COMPACT_MAX_ROWS) compactEncodingLimit();
|
|
579
|
+
if (row.cells.length > COMPACT_MAX_EXPANDED_CELLS || (cols !== null && row.cells.length !== cols))
|
|
580
|
+
compactEncodingLimit();
|
|
581
|
+
budget.cells += row.cells.length;
|
|
582
|
+
if (budget.cells > COMPACT_MAX_EXPANDED_CELLS) compactEncodingLimit();
|
|
583
|
+
const runs: CompactCellRun[] = [];
|
|
584
|
+
for (const cell of row.cells) {
|
|
585
|
+
const prior = runs.at(-1);
|
|
586
|
+
const encoded = compactCell(cell);
|
|
587
|
+
if (prior && compactRunCellEqual(prior, encoded)) prior[0] += 1;
|
|
588
|
+
else {
|
|
589
|
+
if (++budget.runs > COMPACT_MAX_RUNS) compactEncodingLimit();
|
|
590
|
+
runs.push(encoded);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return [row.wrapped ? 1 : 0, runs];
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function compactCell(
|
|
597
|
+
cell: TerminalReplicaCell,
|
|
598
|
+
): [number, string, 0 | 1 | 2, CompactColor, CompactColor, number] {
|
|
599
|
+
return [
|
|
600
|
+
1,
|
|
601
|
+
compactEncodeString(cell.grapheme),
|
|
602
|
+
cell.width,
|
|
603
|
+
compactColor(cell.foreground),
|
|
604
|
+
compactColor(cell.background),
|
|
605
|
+
cell.attributes,
|
|
606
|
+
];
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function compactRunCellEqual(a: CompactCellRun, b: CompactCellRun): boolean {
|
|
610
|
+
return (
|
|
611
|
+
a[1] === b[1] &&
|
|
612
|
+
a[2] === b[2] &&
|
|
613
|
+
compactColorEqual(a[3], b[3]) &&
|
|
614
|
+
compactColorEqual(a[4], b[4]) &&
|
|
615
|
+
a[5] === b[5]
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
function compactColor(color: TerminalReplicaColor): CompactColor {
|
|
620
|
+
return color.kind === "default"
|
|
621
|
+
? 0
|
|
622
|
+
: [color.kind === "indexed" ? 1 : 2, color.kind === "indexed" ? color.index : color.value];
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
function compactColorEqual(a: CompactColor, b: CompactColor): boolean {
|
|
626
|
+
return a === 0 ? b === 0 : b !== 0 && a[0] === b[0] && a[1] === b[1];
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function compactCursor(cursor: TerminalReplicaSnapshot["cursor"]): readonly unknown[] {
|
|
630
|
+
return [cursor.x, cursor.y, cursor.hidden ? 1 : 0, cursor.style, cursor.blink ? 1 : 0];
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function compactModes(modes: TerminalReplicaSnapshot["modes"]): readonly unknown[] {
|
|
634
|
+
return [
|
|
635
|
+
modes.alternateScreen ? 1 : 0,
|
|
636
|
+
modes.applicationCursor ? 1 : 0,
|
|
637
|
+
modes.applicationKeypad ? 1 : 0,
|
|
638
|
+
modes.bracketedPaste ? 1 : 0,
|
|
639
|
+
modes.insert ? 1 : 0,
|
|
640
|
+
modes.origin ? 1 : 0,
|
|
641
|
+
modes.wraparound ? 1 : 0,
|
|
642
|
+
modes.mouseTracking ? 1 : 0,
|
|
643
|
+
modes.mouseProtocol ?? null,
|
|
644
|
+
modes.mouseEncoding ?? null,
|
|
645
|
+
modes.synchronizedOutput ? 1 : 0,
|
|
646
|
+
];
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function compactPlacement(
|
|
650
|
+
placement: TerminalReplicaSnapshot["placements"][number],
|
|
651
|
+
): readonly unknown[] {
|
|
652
|
+
return [
|
|
653
|
+
compactEncodeString(placement.id),
|
|
654
|
+
compactEncodeString(placement.kind),
|
|
655
|
+
placement.row,
|
|
656
|
+
placement.column,
|
|
657
|
+
placement.columns,
|
|
658
|
+
placement.rows,
|
|
659
|
+
compactEncodeString(placement.contentDigest),
|
|
660
|
+
];
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function compactPlacements(
|
|
664
|
+
placements: TerminalReplicaSnapshot["placements"],
|
|
665
|
+
budget: CompactDecodeBudget,
|
|
666
|
+
): readonly unknown[] {
|
|
667
|
+
budget.placements += placements.length;
|
|
668
|
+
if (budget.placements > COMPACT_MAX_PLACEMENTS) compactEncodingLimit();
|
|
669
|
+
return placements.map(compactPlacement);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function compactBootstrap(bootstrap: TerminalReplicaSnapshot["bootstrap"]): readonly unknown[] {
|
|
673
|
+
return [bootstrap.kind, bootstrap.hiddenState];
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function expandSnapshot(value: unknown, budget: CompactDecodeBudget): TerminalReplicaSnapshot {
|
|
677
|
+
const input = compactArray(value, 8, "snapshot");
|
|
678
|
+
const cols = compactInteger(input[0], 1, COMPACT_MAX_DIMENSION, "columns");
|
|
679
|
+
const rows = compactInteger(input[1], 1, COMPACT_MAX_DIMENSION, "rows");
|
|
680
|
+
const grid = expandRows(input[2], budget, cols, rows, "grid");
|
|
681
|
+
const history = expandRows(input[3], budget, cols, COMPACT_MAX_ROWS, "history");
|
|
682
|
+
const snapshot = Object.freeze({
|
|
683
|
+
cols,
|
|
684
|
+
rows,
|
|
685
|
+
grid,
|
|
686
|
+
history,
|
|
687
|
+
cursor: expandCursor(input[4], cols, rows),
|
|
688
|
+
modes: expandModes(input[5]),
|
|
689
|
+
placements: expandPlacements(input[6], budget),
|
|
690
|
+
bootstrap: expandBootstrap(input[7]),
|
|
691
|
+
});
|
|
692
|
+
if (
|
|
693
|
+
snapshot.placements.some(
|
|
694
|
+
(placement) =>
|
|
695
|
+
placement.row + placement.rows > rows || placement.column + placement.columns > cols,
|
|
696
|
+
)
|
|
697
|
+
)
|
|
698
|
+
throw new TypeError("Compact placement is outside the viewport");
|
|
699
|
+
return snapshot as unknown as TerminalReplicaSnapshot;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function expandPatch(value: unknown, budget: CompactDecodeBudget): TerminalReplicaPatchPayload {
|
|
703
|
+
const input = compactArray(value, 8, "patch");
|
|
704
|
+
const dimensions =
|
|
705
|
+
input[0] === null
|
|
706
|
+
? undefined
|
|
707
|
+
: (() => {
|
|
708
|
+
const pair = compactArray(input[0], 2, "dimensions");
|
|
709
|
+
return Object.freeze({
|
|
710
|
+
cols: compactInteger(pair[0], 1, COMPACT_MAX_DIMENSION, "columns"),
|
|
711
|
+
rows: compactInteger(pair[1], 1, COMPACT_MAX_DIMENSION, "rows"),
|
|
712
|
+
});
|
|
713
|
+
})();
|
|
714
|
+
const rowInputs = compactArrayAtMost(input[1], COMPACT_MAX_DIMENSION, "dirty rows");
|
|
715
|
+
const rows = rowInputs.map((entry) => {
|
|
716
|
+
const pair = compactArray(entry, 2, "dirty row");
|
|
717
|
+
return Object.freeze({
|
|
718
|
+
index: compactInteger(pair[0], 0, COMPACT_MAX_DIMENSION - 1, "row index"),
|
|
719
|
+
row: expandRow(pair[1], budget, dimensions?.cols ?? null),
|
|
720
|
+
});
|
|
721
|
+
});
|
|
722
|
+
const history =
|
|
723
|
+
input[2] === null ? undefined : expandRows(input[2], budget, null, COMPACT_MAX_ROWS, "history");
|
|
724
|
+
const historyDelta =
|
|
725
|
+
input[3] === null
|
|
726
|
+
? undefined
|
|
727
|
+
: (() => {
|
|
728
|
+
const pair = compactArray(input[3], 2, "history delta");
|
|
729
|
+
return Object.freeze({
|
|
730
|
+
trim: compactInteger(pair[0], 0, COMPACT_MAX_ROWS, "history trim"),
|
|
731
|
+
append: expandRows(pair[1], budget, null, COMPACT_MAX_ROWS, "history append"),
|
|
732
|
+
});
|
|
733
|
+
})();
|
|
734
|
+
if (history && historyDelta) throw new TypeError("Compact patch has two history representations");
|
|
735
|
+
return Object.freeze({
|
|
736
|
+
...(dimensions ? { dimensions } : {}),
|
|
737
|
+
rows: Object.freeze(rows.map((row) => Object.freeze(row))),
|
|
738
|
+
...(history ? { history } : {}),
|
|
739
|
+
...(historyDelta ? { historyDelta } : {}),
|
|
740
|
+
...(input[4] === null
|
|
741
|
+
? {}
|
|
742
|
+
: { cursor: expandCursor(input[4], dimensions?.cols ?? null, dimensions?.rows ?? null) }),
|
|
743
|
+
...(input[5] === null ? {} : { modes: expandModes(input[5]) }),
|
|
744
|
+
...(input[6] === null ? {} : { placements: expandPlacements(input[6], budget) }),
|
|
745
|
+
...(input[7] === null ? {} : { bootstrap: expandBootstrap(input[7]) }),
|
|
746
|
+
}) as unknown as TerminalReplicaPatchPayload;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
interface CompactDecodeBudget {
|
|
750
|
+
rows: number;
|
|
751
|
+
runs: number;
|
|
752
|
+
cells: number;
|
|
753
|
+
placements: number;
|
|
754
|
+
maxCells: number;
|
|
755
|
+
rowCache?: Map<string, Readonly<{ row: TerminalReplicaRow; runs: number; cells: number }>>;
|
|
756
|
+
rowReuseIndex?: ReadonlyMap<string, TerminalReplicaRow | readonly TerminalReplicaRow[]>;
|
|
757
|
+
rawRowReuseIndexes?: readonly ValidatedCompactRawRowIndex[];
|
|
758
|
+
transactionRows?: Map<number, ValidatedCompactRawRowCandidate>;
|
|
759
|
+
transactionRowBytes?: number;
|
|
760
|
+
transactionRowCount?: number;
|
|
761
|
+
reusedRows: number;
|
|
762
|
+
allocatedCells: number;
|
|
763
|
+
canonicalUtf8Allocations: number;
|
|
764
|
+
canonicalUtf8Bytes: number;
|
|
765
|
+
runEncodingCache?: TerminalReplicaRunEncodingCache;
|
|
766
|
+
decodedCellCache?: Map<
|
|
767
|
+
string,
|
|
768
|
+
Map<number, Map<number, Map<number, Map<number, TerminalReplicaCell>>>>
|
|
769
|
+
>;
|
|
770
|
+
validatedCellAllocations: number;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
const VALIDATED_COMPACT_RAW_ROW = new WeakMap<
|
|
774
|
+
TerminalReplicaRow,
|
|
775
|
+
Readonly<{
|
|
776
|
+
row: TerminalReplicaRow;
|
|
777
|
+
raw: Uint8Array;
|
|
778
|
+
rawHash: number;
|
|
779
|
+
runs: number;
|
|
780
|
+
cells: number;
|
|
781
|
+
}>
|
|
782
|
+
>();
|
|
783
|
+
interface ValidatedCompactRawRowCandidate {
|
|
784
|
+
readonly row: TerminalReplicaRow;
|
|
785
|
+
readonly raw: Uint8Array;
|
|
786
|
+
readonly runs: number;
|
|
787
|
+
readonly cells: number;
|
|
788
|
+
readonly collisions?: readonly ValidatedCompactRawRowCandidate[];
|
|
789
|
+
}
|
|
790
|
+
type ValidatedCompactRawRowIndex = ReadonlyMap<number, ValidatedCompactRawRowCandidate>;
|
|
791
|
+
// Each index owns only rows from its exact immutable array. Shared rows never
|
|
792
|
+
// anchor other collections, and a changed grid does not invalidate history.
|
|
793
|
+
const VALIDATED_COMPACT_RAW_ROW_INDEX = new WeakMap<
|
|
794
|
+
readonly TerminalReplicaRow[],
|
|
795
|
+
ValidatedCompactRawRowIndex
|
|
796
|
+
>();
|
|
797
|
+
|
|
798
|
+
function expandRows(
|
|
799
|
+
value: unknown,
|
|
800
|
+
budget: CompactDecodeBudget,
|
|
801
|
+
cols: number | null,
|
|
802
|
+
maximum: number,
|
|
803
|
+
label: string,
|
|
804
|
+
): TerminalReplicaRow[] {
|
|
805
|
+
const inputs = compactArrayAtMost(value, maximum, label);
|
|
806
|
+
if (label === "grid" && cols !== null && inputs.length !== maximum)
|
|
807
|
+
throw new TypeError("Compact grid cardinality mismatch");
|
|
808
|
+
return Object.freeze(inputs.map((row) => expandRow(row, budget, cols))) as TerminalReplicaRow[];
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
function expandRow(
|
|
812
|
+
value: unknown,
|
|
813
|
+
budget: CompactDecodeBudget,
|
|
814
|
+
cols: number | null,
|
|
815
|
+
): TerminalReplicaRow {
|
|
816
|
+
if (++budget.rows > COMPACT_MAX_ROWS) throw new TypeError("Compact semantic row budget exceeded");
|
|
817
|
+
const input = compactArray(value, 2, "row");
|
|
818
|
+
const wrapped = compactBit(input[0], "wrapped");
|
|
819
|
+
const runs = compactArrayAtMost(input[1], COMPACT_MAX_RUNS, "cell runs");
|
|
820
|
+
const cacheKey = budget.rowCache ? JSON.stringify(input) : null;
|
|
821
|
+
const cached = cacheKey === null ? undefined : budget.rowCache?.get(cacheKey);
|
|
822
|
+
if (cached) {
|
|
823
|
+
budget.runs += cached.runs;
|
|
824
|
+
budget.cells += cached.cells;
|
|
825
|
+
if (budget.runs > COMPACT_MAX_RUNS) throw new TypeError("Compact semantic run budget exceeded");
|
|
826
|
+
if (budget.cells > budget.maxCells)
|
|
827
|
+
throw new TypeError("Compact semantic expanded cell budget exceeded");
|
|
828
|
+
budget.reusedRows += 1;
|
|
829
|
+
return cached.row;
|
|
830
|
+
}
|
|
831
|
+
const runsBefore = budget.runs;
|
|
832
|
+
const cellsBefore = budget.cells;
|
|
833
|
+
const cells: TerminalReplicaCell[] = [];
|
|
834
|
+
for (const valueRun of runs) {
|
|
835
|
+
if (++budget.runs > COMPACT_MAX_RUNS)
|
|
836
|
+
throw new TypeError("Compact semantic run budget exceeded");
|
|
837
|
+
const run = compactArray(valueRun, 6, "cell run");
|
|
838
|
+
const count = compactInteger(run[0], 1, COMPACT_MAX_DIMENSION, "cell run count");
|
|
839
|
+
budget.cells += count;
|
|
840
|
+
if (budget.cells > budget.maxCells)
|
|
841
|
+
throw new TypeError("Compact semantic expanded cell budget exceeded");
|
|
842
|
+
const grapheme = compactString(run[1], "grapheme");
|
|
843
|
+
const width = compactInteger(run[2], 0, 2, "cell width") as 0 | 1 | 2;
|
|
844
|
+
const cell = Object.freeze({
|
|
845
|
+
grapheme,
|
|
846
|
+
width,
|
|
847
|
+
foreground: expandColor(run[3]),
|
|
848
|
+
background: expandColor(run[4]),
|
|
849
|
+
attributes: compactInteger(run[5], 0, 0xff, "cell attributes"),
|
|
850
|
+
});
|
|
851
|
+
for (let index = 0; index < count; index += 1) cells.push(cell);
|
|
852
|
+
}
|
|
853
|
+
if (cols !== null && cells.length !== cols) throw new TypeError("Compact row width mismatch");
|
|
854
|
+
if (cells.length < 1 || cells.length > COMPACT_MAX_DIMENSION)
|
|
855
|
+
throw new TypeError("Compact row width is out of bounds");
|
|
856
|
+
for (let index = 0; index < cells.length; index += 1) {
|
|
857
|
+
if (cells[index]!.width === 2 && cells[index + 1]?.width !== 0)
|
|
858
|
+
throw new TypeError("Malformed compact wide cell");
|
|
859
|
+
if (cells[index]!.width === 0 && (index === 0 || cells[index - 1]?.width !== 2))
|
|
860
|
+
throw new TypeError("Malformed compact continuation cell");
|
|
861
|
+
}
|
|
862
|
+
const row = Object.freeze({
|
|
863
|
+
cells: Object.freeze(cells),
|
|
864
|
+
wrapped,
|
|
865
|
+
}) as unknown as TerminalReplicaRow;
|
|
866
|
+
budget.allocatedCells += cells.length;
|
|
867
|
+
if (cacheKey !== null)
|
|
868
|
+
budget.rowCache?.set(
|
|
869
|
+
cacheKey,
|
|
870
|
+
Object.freeze({
|
|
871
|
+
row,
|
|
872
|
+
runs: budget.runs - runsBefore,
|
|
873
|
+
cells: budget.cells - cellsBefore,
|
|
874
|
+
}),
|
|
875
|
+
);
|
|
876
|
+
return row;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function expandColor(value: unknown): TerminalReplicaColor {
|
|
880
|
+
if (value === 0) return COMPACT_DEFAULT_COLOR;
|
|
881
|
+
const pair = compactArray(value, 2, "color");
|
|
882
|
+
if (pair[0] === 1)
|
|
883
|
+
return Object.freeze({
|
|
884
|
+
kind: "indexed",
|
|
885
|
+
index: compactInteger(pair[1], 0, 255, "indexed color"),
|
|
886
|
+
});
|
|
887
|
+
if (pair[0] === 2)
|
|
888
|
+
return Object.freeze({
|
|
889
|
+
kind: "rgb",
|
|
890
|
+
value: compactInteger(pair[1], 0, 0xffffff, "rgb color"),
|
|
891
|
+
});
|
|
892
|
+
throw new TypeError("Invalid compact color kind");
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
function expandCursor(
|
|
896
|
+
value: unknown,
|
|
897
|
+
cols: number | null,
|
|
898
|
+
rows: number | null,
|
|
899
|
+
): TerminalReplicaSnapshot["cursor"] {
|
|
900
|
+
const input = compactArray(value, 5, "cursor");
|
|
901
|
+
const x = compactInteger(input[0], 0, COMPACT_MAX_DIMENSION - 1, "cursor x");
|
|
902
|
+
const y = compactInteger(input[1], 0, COMPACT_MAX_DIMENSION - 1, "cursor y");
|
|
903
|
+
if ((cols !== null && x >= cols) || (rows !== null && y >= rows))
|
|
904
|
+
throw new TypeError("Compact cursor is outside the viewport");
|
|
905
|
+
if (!(["block", "underline", "bar"] as unknown[]).includes(input[3]))
|
|
906
|
+
throw new TypeError("Invalid compact cursor style");
|
|
907
|
+
return Object.freeze({
|
|
908
|
+
x,
|
|
909
|
+
y,
|
|
910
|
+
hidden: compactBit(input[2], "cursor hidden"),
|
|
911
|
+
style: input[3] as "block" | "underline" | "bar",
|
|
912
|
+
blink: compactBit(input[4], "cursor blink"),
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
function expandModes(value: unknown): TerminalReplicaSnapshot["modes"] {
|
|
917
|
+
const input = compactArray(value, 11, "modes");
|
|
918
|
+
const mouseProtocol = input[8];
|
|
919
|
+
const mouseEncoding = input[9];
|
|
920
|
+
if (
|
|
921
|
+
mouseProtocol !== null &&
|
|
922
|
+
!["none", "x10", "vt200", "drag", "any"].includes(String(mouseProtocol))
|
|
923
|
+
)
|
|
924
|
+
throw new TypeError("Invalid compact mouse protocol");
|
|
925
|
+
if (
|
|
926
|
+
mouseEncoding !== null &&
|
|
927
|
+
!["default", "utf8", "sgr", "sgr-pixels"].includes(String(mouseEncoding))
|
|
928
|
+
)
|
|
929
|
+
throw new TypeError("Invalid compact mouse encoding");
|
|
930
|
+
return Object.freeze({
|
|
931
|
+
alternateScreen: compactBit(input[0], "alternate screen"),
|
|
932
|
+
applicationCursor: compactBit(input[1], "application cursor"),
|
|
933
|
+
applicationKeypad: compactBit(input[2], "application keypad"),
|
|
934
|
+
bracketedPaste: compactBit(input[3], "bracketed paste"),
|
|
935
|
+
insert: compactBit(input[4], "insert"),
|
|
936
|
+
origin: compactBit(input[5], "origin"),
|
|
937
|
+
wraparound: compactBit(input[6], "wraparound"),
|
|
938
|
+
mouseTracking: compactBit(input[7], "mouse tracking"),
|
|
939
|
+
...(mouseProtocol === null
|
|
940
|
+
? {}
|
|
941
|
+
: { mouseProtocol: mouseProtocol as TerminalReplicaSnapshot["modes"]["mouseProtocol"] }),
|
|
942
|
+
...(mouseEncoding === null
|
|
943
|
+
? {}
|
|
944
|
+
: { mouseEncoding: mouseEncoding as TerminalReplicaSnapshot["modes"]["mouseEncoding"] }),
|
|
945
|
+
synchronizedOutput: compactBit(input[10], "synchronized output"),
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
function expandPlacements(
|
|
950
|
+
value: unknown,
|
|
951
|
+
budget: CompactDecodeBudget,
|
|
952
|
+
): TerminalReplicaSnapshot["placements"] {
|
|
953
|
+
const inputs = compactArrayAtMost(value, COMPACT_MAX_PLACEMENTS, "placements");
|
|
954
|
+
budget.placements += inputs.length;
|
|
955
|
+
if (budget.placements > COMPACT_MAX_PLACEMENTS)
|
|
956
|
+
throw new TypeError("Compact placement budget exceeded");
|
|
957
|
+
return Object.freeze(
|
|
958
|
+
inputs.map((value) => {
|
|
959
|
+
const input = compactArray(value, 7, "placement");
|
|
960
|
+
return Object.freeze({
|
|
961
|
+
id: compactString(input[0], "placement id", true),
|
|
962
|
+
kind: compactString(input[1], "placement kind", true),
|
|
963
|
+
row: compactInteger(input[2], 0, COMPACT_MAX_DIMENSION - 1, "placement row"),
|
|
964
|
+
column: compactInteger(input[3], 0, COMPACT_MAX_DIMENSION - 1, "placement column"),
|
|
965
|
+
columns: compactInteger(input[4], 1, COMPACT_MAX_DIMENSION, "placement columns"),
|
|
966
|
+
rows: compactInteger(input[5], 1, COMPACT_MAX_DIMENSION, "placement rows"),
|
|
967
|
+
contentDigest: compactString(input[6], "placement digest", true),
|
|
968
|
+
});
|
|
969
|
+
}),
|
|
970
|
+
) as unknown as TerminalReplicaSnapshot["placements"];
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
async function expandPlacementsCooperatively(
|
|
974
|
+
value: unknown,
|
|
975
|
+
budget: CompactDecodeBudget,
|
|
976
|
+
control: CompactCooperativeControl,
|
|
977
|
+
): Promise<TerminalReplicaSnapshot["placements"]> {
|
|
978
|
+
const inputs = compactArrayAtMost(value, COMPACT_MAX_PLACEMENTS, "placements");
|
|
979
|
+
budget.placements += inputs.length;
|
|
980
|
+
if (budget.placements > COMPACT_MAX_PLACEMENTS)
|
|
981
|
+
throw new TypeError("Compact placement budget exceeded");
|
|
982
|
+
const placements: TerminalReplicaSnapshot["placements"][number][] = [];
|
|
983
|
+
for (let index = 0; index < inputs.length; index += 1) {
|
|
984
|
+
const input = compactArray(inputs[index], 7, "placement");
|
|
985
|
+
placements.push(
|
|
986
|
+
Object.freeze({
|
|
987
|
+
id: compactString(input[0], "placement id", true),
|
|
988
|
+
kind: compactString(input[1], "placement kind", true),
|
|
989
|
+
row: compactInteger(input[2], 0, COMPACT_MAX_DIMENSION - 1, "placement row"),
|
|
990
|
+
column: compactInteger(input[3], 0, COMPACT_MAX_DIMENSION - 1, "placement column"),
|
|
991
|
+
columns: compactInteger(input[4], 1, COMPACT_MAX_DIMENSION, "placement columns"),
|
|
992
|
+
rows: compactInteger(input[5], 1, COMPACT_MAX_DIMENSION, "placement rows"),
|
|
993
|
+
contentDigest: compactString(input[6], "placement digest", true),
|
|
994
|
+
}),
|
|
995
|
+
);
|
|
996
|
+
// The parsed wire container is private and single-use. Drop each expanded
|
|
997
|
+
// placement immediately so a large validated prefix cannot coexist with
|
|
998
|
+
// its canonical replacement until the complete envelope finishes.
|
|
999
|
+
inputs[index] = null;
|
|
1000
|
+
if ((index + 1) % 16 === 0) await control.yieldControl();
|
|
1001
|
+
}
|
|
1002
|
+
return Object.freeze(placements) as unknown as TerminalReplicaSnapshot["placements"];
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
function expandBootstrap(value: unknown): TerminalReplicaSnapshot["bootstrap"] {
|
|
1006
|
+
const input = compactArray(value, 2, "bootstrap");
|
|
1007
|
+
if (
|
|
1008
|
+
!["painted-capture", "authoritative-stream"].includes(String(input[0])) ||
|
|
1009
|
+
!["unknown", "observed-from-start"].includes(String(input[1]))
|
|
1010
|
+
)
|
|
1011
|
+
throw new TypeError("Invalid compact bootstrap");
|
|
1012
|
+
return Object.freeze({
|
|
1013
|
+
kind: input[0] as "painted-capture" | "authoritative-stream",
|
|
1014
|
+
hiddenState: input[1] as "unknown" | "observed-from-start",
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
function compactArray(value: unknown, length: number, label: string): unknown[] {
|
|
1019
|
+
if (!Array.isArray(value) || value.length !== length)
|
|
1020
|
+
throw new TypeError(`Invalid compact ${label}`);
|
|
1021
|
+
return value;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
function compactArrayAtMost(value: unknown, maximum: number, label: string): unknown[] {
|
|
1025
|
+
if (!Array.isArray(value) || value.length > maximum)
|
|
1026
|
+
throw new TypeError(`Invalid compact ${label}`);
|
|
1027
|
+
return value;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
function compactInteger(value: unknown, minimum: number, maximum: number, label: string): number {
|
|
1031
|
+
if (!Number.isSafeInteger(value) || (value as number) < minimum || (value as number) > maximum)
|
|
1032
|
+
throw new TypeError(`Invalid compact ${label}`);
|
|
1033
|
+
return value as number;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
function compactBit(value: unknown, label: string): boolean {
|
|
1037
|
+
if (value !== 0 && value !== 1) throw new TypeError(`Invalid compact ${label}`);
|
|
1038
|
+
return value === 1;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function compactString(value: unknown, label: string, nonempty = false): string {
|
|
1042
|
+
if (
|
|
1043
|
+
typeof value !== "string" ||
|
|
1044
|
+
(nonempty && value.length === 0) ||
|
|
1045
|
+
UTF8_ENCODER.encode(value).byteLength > COMPACT_MAX_STRING_BYTES
|
|
1046
|
+
)
|
|
1047
|
+
throw new TypeError(`Invalid compact ${label}`);
|
|
1048
|
+
return value;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
function compactEncodeString(value: string): string {
|
|
1052
|
+
if (
|
|
1053
|
+
value.length > COMPACT_MAX_STRING_BYTES / 4 &&
|
|
1054
|
+
UTF8_ENCODER.encode(value).byteLength > COMPACT_MAX_STRING_BYTES
|
|
1055
|
+
)
|
|
1056
|
+
compactEncodingLimit();
|
|
1057
|
+
return value;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
function compactEncodingLimit(): never {
|
|
1061
|
+
throw new TerminalDeliveryStateTooLargeError(TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES + 1);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
function applyDecodedCompactPatch(
|
|
1065
|
+
current: TerminalReplicaSnapshot,
|
|
1066
|
+
patch: TerminalReplicaPatchPayload,
|
|
1067
|
+
): TerminalReplicaSnapshot {
|
|
1068
|
+
const cols = patch.dimensions?.cols ?? current.cols;
|
|
1069
|
+
const rows = patch.dimensions?.rows ?? current.rows;
|
|
1070
|
+
if (patch.history !== undefined && patch.historyDelta !== undefined)
|
|
1071
|
+
throw new TypeError("Compact patch has two history representations");
|
|
1072
|
+
if ((patch.historyDelta?.trim ?? 0) > current.history.length)
|
|
1073
|
+
throw new TypeError("Compact history trim is out of bounds");
|
|
1074
|
+
const resizedBlank =
|
|
1075
|
+
cols === current.cols && rows === current.rows ? null : compactBlankRow(cols);
|
|
1076
|
+
const nextGrid: TerminalReplicaRow[] =
|
|
1077
|
+
cols === current.cols && rows === current.rows && patch.rows.length === 0
|
|
1078
|
+
? current.grid
|
|
1079
|
+
: cols === current.cols && rows === current.rows
|
|
1080
|
+
? [...current.grid]
|
|
1081
|
+
: Array.from({ length: rows }, (_, index) => {
|
|
1082
|
+
const prior = current.grid[index];
|
|
1083
|
+
return prior?.cells.length === cols ? prior : resizedBlank!;
|
|
1084
|
+
});
|
|
1085
|
+
const seen = new Set<number>();
|
|
1086
|
+
for (const change of patch.rows) {
|
|
1087
|
+
if (
|
|
1088
|
+
change.index < 0 ||
|
|
1089
|
+
change.index >= rows ||
|
|
1090
|
+
seen.has(change.index) ||
|
|
1091
|
+
change.row.cells.length !== cols
|
|
1092
|
+
)
|
|
1093
|
+
throw new TypeError("Malformed compact terminal row patch");
|
|
1094
|
+
seen.add(change.index);
|
|
1095
|
+
nextGrid[change.index] = change.row;
|
|
1096
|
+
}
|
|
1097
|
+
const history = patch.history
|
|
1098
|
+
? patch.history
|
|
1099
|
+
: patch.historyDelta
|
|
1100
|
+
? (() => {
|
|
1101
|
+
const retained = current.history.length - patch.historyDelta.trim;
|
|
1102
|
+
const nextLength = retained + patch.historyDelta.append.length;
|
|
1103
|
+
const unchanged =
|
|
1104
|
+
nextLength === current.history.length &&
|
|
1105
|
+
current.history.every((row, index) =>
|
|
1106
|
+
index < retained
|
|
1107
|
+
? row === current.history[index + patch.historyDelta!.trim]
|
|
1108
|
+
: row === patch.historyDelta!.append[index - retained],
|
|
1109
|
+
);
|
|
1110
|
+
return unchanged
|
|
1111
|
+
? current.history
|
|
1112
|
+
: Object.freeze([
|
|
1113
|
+
...current.history.slice(patch.historyDelta.trim),
|
|
1114
|
+
...patch.historyDelta.append,
|
|
1115
|
+
]);
|
|
1116
|
+
})()
|
|
1117
|
+
: current.history;
|
|
1118
|
+
if (patch.historyDelta && history !== current.history)
|
|
1119
|
+
registerTerminalReplicaRowsDeltaHash(
|
|
1120
|
+
current.history,
|
|
1121
|
+
history,
|
|
1122
|
+
patch.historyDelta.trim,
|
|
1123
|
+
patch.historyDelta.append,
|
|
1124
|
+
);
|
|
1125
|
+
if (history.length > COMPACT_MAX_ROWS || history.some((row) => row.cells.length !== cols))
|
|
1126
|
+
throw new TypeError("Compact patch retained wrong-width history");
|
|
1127
|
+
const cursor = patch.cursor ?? current.cursor;
|
|
1128
|
+
if (cursor.x >= cols || cursor.y >= rows) throw new TypeError("Compact cursor is out of bounds");
|
|
1129
|
+
const placements = patch.placements ?? current.placements;
|
|
1130
|
+
if (
|
|
1131
|
+
placements.some(
|
|
1132
|
+
(placement) =>
|
|
1133
|
+
placement.row + placement.rows > rows || placement.column + placement.columns > cols,
|
|
1134
|
+
)
|
|
1135
|
+
)
|
|
1136
|
+
throw new TypeError("Compact placement is outside the viewport");
|
|
1137
|
+
return Object.freeze({
|
|
1138
|
+
cols,
|
|
1139
|
+
rows,
|
|
1140
|
+
grid: Object.freeze(nextGrid) as TerminalReplicaRow[],
|
|
1141
|
+
history,
|
|
1142
|
+
cursor,
|
|
1143
|
+
modes: patch.modes ?? current.modes,
|
|
1144
|
+
placements,
|
|
1145
|
+
bootstrap: patch.bootstrap ?? current.bootstrap,
|
|
1146
|
+
}) as unknown as TerminalReplicaSnapshot;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
function compactBlankRow(cols: number): TerminalReplicaRow {
|
|
1150
|
+
const cell = Object.freeze({
|
|
1151
|
+
grapheme: " ",
|
|
1152
|
+
width: 1 as const,
|
|
1153
|
+
foreground: COMPACT_DEFAULT_COLOR,
|
|
1154
|
+
background: COMPACT_DEFAULT_COLOR,
|
|
1155
|
+
attributes: 0,
|
|
1156
|
+
});
|
|
1157
|
+
return Object.freeze({
|
|
1158
|
+
cells: Object.freeze(Array.from({ length: cols }, () => cell)),
|
|
1159
|
+
wrapped: false,
|
|
1160
|
+
}) as unknown as TerminalReplicaRow;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
export interface CompactSemanticCommitProfile {
|
|
1164
|
+
readonly reusedCompactPayload: boolean;
|
|
1165
|
+
readonly expandedRows: number;
|
|
1166
|
+
readonly expandedRuns: number;
|
|
1167
|
+
readonly expandedCells: number;
|
|
1168
|
+
readonly reusedRows: number;
|
|
1169
|
+
readonly allocatedCells: number;
|
|
1170
|
+
readonly canonicalUtf8Allocations: number;
|
|
1171
|
+
readonly canonicalUtf8Bytes: number;
|
|
1172
|
+
readonly validatedCellAllocations: number;
|
|
1173
|
+
readonly placements: number;
|
|
1174
|
+
readonly schemaTraversals: 1;
|
|
1175
|
+
readonly hashTraversals: 1;
|
|
1176
|
+
readonly applyTraversals: 0 | 1;
|
|
1177
|
+
readonly trustedAdoption: true;
|
|
1178
|
+
readonly retainedSnapshots: 0;
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
export function decodeVerifiedCompactSemanticTerminalUpdate(
|
|
1182
|
+
bytes: Uint8Array,
|
|
1183
|
+
baseline: TerminalReplicaSnapshot | null,
|
|
1184
|
+
expectedHash: string,
|
|
1185
|
+
options?: {
|
|
1186
|
+
readonly grantReducerAdoption?: boolean;
|
|
1187
|
+
readonly onComplete?: (profile: CompactSemanticCommitProfile) => void;
|
|
1188
|
+
},
|
|
1189
|
+
): Readonly<{
|
|
1190
|
+
payload: TerminalSemanticDeliveryPayload;
|
|
1191
|
+
canonicalSnapshot: TerminalReplicaSnapshot | null;
|
|
1192
|
+
}> {
|
|
1193
|
+
const decodeBudget = options?.onComplete ? compactDecodeBudget(bytes.byteLength) : undefined;
|
|
1194
|
+
const payload = decodeCompactSemanticTerminalUpdateInternal(bytes, decodeBudget);
|
|
1195
|
+
const snapshot =
|
|
1196
|
+
payload.frame === "seed"
|
|
1197
|
+
? payload.snapshot
|
|
1198
|
+
: payload.frame === "patch"
|
|
1199
|
+
? baseline
|
|
1200
|
+
? applyDecodedCompactPatch(baseline, payload.patch)
|
|
1201
|
+
: (() => {
|
|
1202
|
+
throw new TypeError("Semantic delivery baseline gap");
|
|
1203
|
+
})()
|
|
1204
|
+
: null;
|
|
1205
|
+
const hash = snapshot
|
|
1206
|
+
? hashTerminalReplicaSnapshot(snapshot)
|
|
1207
|
+
: hashTerminalReplicaTombstone(
|
|
1208
|
+
payload.frame === "tombstone" ? payload.tombstone.reason : "protocol",
|
|
1209
|
+
);
|
|
1210
|
+
if (hash !== expectedHash) throw new TypeError("Canonical state hash mismatch");
|
|
1211
|
+
compactCommitCapabilities.set(payload, Object.freeze({ snapshot, hash }));
|
|
1212
|
+
if (options?.grantReducerAdoption)
|
|
1213
|
+
grantCompactReplicaCapability(
|
|
1214
|
+
payload.frame === "seed"
|
|
1215
|
+
? payload.snapshot
|
|
1216
|
+
: payload.frame === "patch"
|
|
1217
|
+
? payload.patch
|
|
1218
|
+
: payload.tombstone,
|
|
1219
|
+
baseline,
|
|
1220
|
+
snapshot,
|
|
1221
|
+
hash,
|
|
1222
|
+
);
|
|
1223
|
+
if (options?.onComplete && decodeBudget) {
|
|
1224
|
+
try {
|
|
1225
|
+
options.onComplete(
|
|
1226
|
+
Object.freeze({
|
|
1227
|
+
reusedCompactPayload: false,
|
|
1228
|
+
expandedRows: decodeBudget.rows,
|
|
1229
|
+
expandedRuns: decodeBudget.runs,
|
|
1230
|
+
expandedCells: decodeBudget.cells,
|
|
1231
|
+
reusedRows: decodeBudget.reusedRows,
|
|
1232
|
+
allocatedCells: decodeBudget.allocatedCells,
|
|
1233
|
+
canonicalUtf8Allocations: decodeBudget.canonicalUtf8Allocations,
|
|
1234
|
+
canonicalUtf8Bytes: decodeBudget.canonicalUtf8Bytes,
|
|
1235
|
+
validatedCellAllocations: decodeBudget.validatedCellAllocations,
|
|
1236
|
+
placements: decodeBudget.placements,
|
|
1237
|
+
schemaTraversals: 1,
|
|
1238
|
+
hashTraversals: 1,
|
|
1239
|
+
applyTraversals: payload.frame === "patch" ? 1 : 0,
|
|
1240
|
+
trustedAdoption: true,
|
|
1241
|
+
retainedSnapshots: 0,
|
|
1242
|
+
}),
|
|
1243
|
+
);
|
|
1244
|
+
} catch {
|
|
1245
|
+
// Detailed observers never own canonical delivery.
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
return Object.freeze({ payload, canonicalSnapshot: snapshot });
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Large compact states are expanded a bounded row slice at a time. Nothing is
|
|
1253
|
+
* published or capability-branded until the complete canonical hash matches.
|
|
1254
|
+
*/
|
|
1255
|
+
export async function decodeVerifiedCompactSemanticTerminalUpdateCooperatively(
|
|
1256
|
+
bytes: Uint8Array,
|
|
1257
|
+
baseline: TerminalReplicaSnapshot | null,
|
|
1258
|
+
expectedHash: string,
|
|
1259
|
+
options: {
|
|
1260
|
+
readonly yieldControl: () => Promise<void>;
|
|
1261
|
+
readonly rowsPerSlice?: number;
|
|
1262
|
+
readonly grantReducerAdoption?: boolean;
|
|
1263
|
+
readonly onComplete?: (profile: CompactSemanticCommitProfile) => void;
|
|
1264
|
+
},
|
|
1265
|
+
): Promise<
|
|
1266
|
+
Readonly<{
|
|
1267
|
+
payload: TerminalSemanticDeliveryPayload;
|
|
1268
|
+
canonicalSnapshot: TerminalReplicaSnapshot | null;
|
|
1269
|
+
}>
|
|
1270
|
+
> {
|
|
1271
|
+
assertRepresentationSize(bytes);
|
|
1272
|
+
const wire = await parseCompactJsonCooperatively(bytes, options.yieldControl);
|
|
1273
|
+
const frame =
|
|
1274
|
+
wire !== null && typeof wire === "object" && !Array.isArray(wire)
|
|
1275
|
+
? (wire as Record<string, unknown>).f
|
|
1276
|
+
: null;
|
|
1277
|
+
if (
|
|
1278
|
+
!recordWithKeys(
|
|
1279
|
+
wire,
|
|
1280
|
+
frame === "s"
|
|
1281
|
+
? ["v", "k", "f", "r", "s"]
|
|
1282
|
+
: ["v", "k", "f", "b", "r", frame === "p" ? "p" : "t"],
|
|
1283
|
+
)
|
|
1284
|
+
)
|
|
1285
|
+
throw new TypeError("Invalid compact semantic envelope");
|
|
1286
|
+
const qualifiedWire = wire as Record<string, unknown>;
|
|
1287
|
+
if (qualifiedWire.v !== COMPACT_SEMANTIC_VERSION || qualifiedWire.k !== COMPACT_SEMANTIC_KIND)
|
|
1288
|
+
throw new TypeError("Unsupported compact semantic version");
|
|
1289
|
+
const revision = compactInteger(qualifiedWire.r, 0, Number.MAX_SAFE_INTEGER, "revision");
|
|
1290
|
+
const budget = compactDecodeBudget(bytes.byteLength);
|
|
1291
|
+
// Cooperative validation never serializes a whole row to form an interning
|
|
1292
|
+
// key; that would recreate an unbounded synchronous phase.
|
|
1293
|
+
budget.rowCache = undefined;
|
|
1294
|
+
budget.transactionRows = new Map();
|
|
1295
|
+
budget.transactionRowBytes = 0;
|
|
1296
|
+
budget.transactionRowCount = 0;
|
|
1297
|
+
budget.runEncodingCache = new TerminalReplicaRunEncodingCache();
|
|
1298
|
+
budget.decodedCellCache = new Map();
|
|
1299
|
+
const control = {
|
|
1300
|
+
yieldControl: options.yieldControl,
|
|
1301
|
+
rowsPerSlice: Math.min(Math.max(options.rowsPerSlice ?? 64, 1), 64),
|
|
1302
|
+
rowsSinceYield: 0,
|
|
1303
|
+
};
|
|
1304
|
+
// Canonical rows have exactly their snapshot's width. A full resized seed
|
|
1305
|
+
// cannot reuse any old-width row, so avoid scanning/indexing old history.
|
|
1306
|
+
// This only removes an optimization candidate: normal seed validation and
|
|
1307
|
+
// canonical hashing still run below, with the original adoption baseline.
|
|
1308
|
+
const seedChangesWidth =
|
|
1309
|
+
qualifiedWire.f === "s" &&
|
|
1310
|
+
Array.isArray(qualifiedWire.s) &&
|
|
1311
|
+
qualifiedWire.s[0] !== baseline?.cols;
|
|
1312
|
+
if (baseline && !seedChangesWidth) {
|
|
1313
|
+
budget.rowReuseIndex = await compactBaselineRowReuseIndex(baseline, control);
|
|
1314
|
+
budget.rawRowReuseIndexes = [
|
|
1315
|
+
await compactRawRowReuseIndex(baseline.grid, control),
|
|
1316
|
+
await compactRawRowReuseIndex(baseline.history, control),
|
|
1317
|
+
];
|
|
1318
|
+
}
|
|
1319
|
+
let payload: TerminalSemanticDeliveryPayload;
|
|
1320
|
+
if (qualifiedWire.f === "s") {
|
|
1321
|
+
const encodedSnapshot = qualifiedWire.s;
|
|
1322
|
+
qualifiedWire.s = null;
|
|
1323
|
+
payload = Object.freeze({
|
|
1324
|
+
frame: "seed" as const,
|
|
1325
|
+
revision,
|
|
1326
|
+
snapshot: await expandSnapshotCooperatively(encodedSnapshot, budget, control),
|
|
1327
|
+
});
|
|
1328
|
+
} else {
|
|
1329
|
+
const baseRevision = compactInteger(
|
|
1330
|
+
qualifiedWire.b,
|
|
1331
|
+
0,
|
|
1332
|
+
Number.MAX_SAFE_INTEGER,
|
|
1333
|
+
"base revision",
|
|
1334
|
+
);
|
|
1335
|
+
if (revision <= baseRevision) throw new TypeError("Compact semantic revision did not advance");
|
|
1336
|
+
if (qualifiedWire.f === "p") {
|
|
1337
|
+
const encodedPatch = qualifiedWire.p;
|
|
1338
|
+
qualifiedWire.p = null;
|
|
1339
|
+
payload = Object.freeze({
|
|
1340
|
+
frame: "patch" as const,
|
|
1341
|
+
baseRevision,
|
|
1342
|
+
revision,
|
|
1343
|
+
patch: await expandPatchCooperatively(encodedPatch, budget, control),
|
|
1344
|
+
});
|
|
1345
|
+
} else if (
|
|
1346
|
+
qualifiedWire.f === "t" &&
|
|
1347
|
+
["pane-closed", "session-restarted", "runtime-disposed"].includes(String(qualifiedWire.t))
|
|
1348
|
+
)
|
|
1349
|
+
payload = Object.freeze({
|
|
1350
|
+
frame: "tombstone" as const,
|
|
1351
|
+
baseRevision,
|
|
1352
|
+
revision,
|
|
1353
|
+
tombstone: Object.freeze({
|
|
1354
|
+
reason: qualifiedWire.t as "pane-closed" | "session-restarted" | "runtime-disposed",
|
|
1355
|
+
}),
|
|
1356
|
+
});
|
|
1357
|
+
else throw new TypeError("Invalid compact semantic frame");
|
|
1358
|
+
}
|
|
1359
|
+
const snapshot =
|
|
1360
|
+
payload.frame === "seed"
|
|
1361
|
+
? payload.snapshot
|
|
1362
|
+
: payload.frame === "patch"
|
|
1363
|
+
? baseline
|
|
1364
|
+
? applyDecodedCompactPatch(baseline, payload.patch)
|
|
1365
|
+
: (() => {
|
|
1366
|
+
throw new TypeError("Semantic delivery baseline gap");
|
|
1367
|
+
})()
|
|
1368
|
+
: null;
|
|
1369
|
+
// Expansion has either adopted immutable baseline rows or constructed the
|
|
1370
|
+
// bounded changed rows. Parser/reuse scratch cannot affect canonical hashing
|
|
1371
|
+
// or publication, so release it before the longest cooperative phase.
|
|
1372
|
+
budget.rowReuseIndex = undefined;
|
|
1373
|
+
budget.rawRowReuseIndexes = undefined;
|
|
1374
|
+
budget.transactionRows = undefined;
|
|
1375
|
+
budget.transactionRowBytes = undefined;
|
|
1376
|
+
budget.transactionRowCount = undefined;
|
|
1377
|
+
budget.runEncodingCache = undefined;
|
|
1378
|
+
budget.decodedCellCache = undefined;
|
|
1379
|
+
const hash = snapshot
|
|
1380
|
+
? await hashTerminalReplicaSnapshotCooperatively(snapshot, options.yieldControl)
|
|
1381
|
+
: hashTerminalReplicaTombstone(
|
|
1382
|
+
payload.frame === "tombstone" ? payload.tombstone.reason : "protocol",
|
|
1383
|
+
);
|
|
1384
|
+
if (hash !== expectedHash) throw new TypeError("Canonical state hash mismatch");
|
|
1385
|
+
if (options.grantReducerAdoption)
|
|
1386
|
+
grantCompactReplicaCapability(
|
|
1387
|
+
payload.frame === "seed"
|
|
1388
|
+
? payload.snapshot
|
|
1389
|
+
: payload.frame === "patch"
|
|
1390
|
+
? payload.patch
|
|
1391
|
+
: payload.tombstone,
|
|
1392
|
+
baseline,
|
|
1393
|
+
snapshot,
|
|
1394
|
+
hash,
|
|
1395
|
+
);
|
|
1396
|
+
if (options.onComplete) {
|
|
1397
|
+
try {
|
|
1398
|
+
options.onComplete(
|
|
1399
|
+
Object.freeze({
|
|
1400
|
+
reusedCompactPayload: false,
|
|
1401
|
+
expandedRows: budget.rows,
|
|
1402
|
+
expandedRuns: budget.runs,
|
|
1403
|
+
expandedCells: budget.cells,
|
|
1404
|
+
reusedRows: budget.reusedRows,
|
|
1405
|
+
allocatedCells: budget.allocatedCells,
|
|
1406
|
+
canonicalUtf8Allocations: budget.canonicalUtf8Allocations,
|
|
1407
|
+
canonicalUtf8Bytes: budget.canonicalUtf8Bytes,
|
|
1408
|
+
validatedCellAllocations: budget.validatedCellAllocations,
|
|
1409
|
+
placements: budget.placements,
|
|
1410
|
+
schemaTraversals: 1,
|
|
1411
|
+
hashTraversals: 1,
|
|
1412
|
+
applyTraversals: payload.frame === "patch" ? 1 : 0,
|
|
1413
|
+
trustedAdoption: true,
|
|
1414
|
+
retainedSnapshots: 0,
|
|
1415
|
+
}),
|
|
1416
|
+
);
|
|
1417
|
+
} catch {
|
|
1418
|
+
// Detailed observers never own canonical delivery.
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return Object.freeze({ payload, canonicalSnapshot: snapshot });
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
const COOPERATIVE_JSON_WORK_CHARS = 16 * 1_024;
|
|
1425
|
+
const COOPERATIVE_JSON_MAX_STRING_CHARS = COMPACT_MAX_STRING_BYTES * 6 + 2;
|
|
1426
|
+
const COOPERATIVE_CELL_WORK = 256;
|
|
1427
|
+
|
|
1428
|
+
async function parseCompactJsonCooperatively(
|
|
1429
|
+
bytes: Uint8Array,
|
|
1430
|
+
yieldControl: () => Promise<void>,
|
|
1431
|
+
): Promise<unknown> {
|
|
1432
|
+
const parser = new CooperativeJsonParser(new CooperativeJsonSource(bytes), yieldControl, true);
|
|
1433
|
+
return parser.parse();
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
const COMPACT_PARSED_ROW_SLICE = Symbol("compact-parsed-row-slice");
|
|
1437
|
+
interface CompactParsedRowSlice {
|
|
1438
|
+
readonly kind: typeof COMPACT_PARSED_ROW_SLICE;
|
|
1439
|
+
readonly source: CooperativeJsonSource;
|
|
1440
|
+
readonly start: number;
|
|
1441
|
+
readonly end: number;
|
|
1442
|
+
}
|
|
1443
|
+
const COMPACT_PARSED_ROWS_SLICE = Symbol("compact-parsed-rows-slice");
|
|
1444
|
+
interface CompactParsedRowsSlice {
|
|
1445
|
+
readonly kind: typeof COMPACT_PARSED_ROWS_SLICE;
|
|
1446
|
+
readonly source: CooperativeJsonSource;
|
|
1447
|
+
readonly start: number;
|
|
1448
|
+
readonly end: number;
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
class CooperativeJsonSource {
|
|
1452
|
+
readonly #bytes: Uint8Array;
|
|
1453
|
+
readonly length: number;
|
|
1454
|
+
|
|
1455
|
+
constructor(bytes: Uint8Array) {
|
|
1456
|
+
this.#bytes = bytes;
|
|
1457
|
+
this.length = bytes.byteLength;
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
byteAt(index: number): number {
|
|
1461
|
+
return this.#bytes[index] ?? -1;
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
startsWithAscii(value: string, index: number): boolean {
|
|
1465
|
+
for (let offset = 0; offset < value.length; offset += 1)
|
|
1466
|
+
if (this.byteAt(index + offset) !== value.charCodeAt(offset)) return false;
|
|
1467
|
+
return true;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
slice(start: number, end: number): string {
|
|
1471
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(this.#bytes.subarray(start, end));
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
bytes(start: number, end: number): Uint8Array {
|
|
1475
|
+
return this.#bytes.subarray(start, end);
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
hash(start: number, end: number): number {
|
|
1479
|
+
let hash = 0x811c9dc5;
|
|
1480
|
+
for (let index = start; index < end; index += 1) {
|
|
1481
|
+
hash ^= this.#bytes[index]!;
|
|
1482
|
+
hash = Math.imul(hash, 0x01000193);
|
|
1483
|
+
}
|
|
1484
|
+
return hash >>> 0;
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
equals(start: number, end: number, expected: Uint8Array): boolean {
|
|
1488
|
+
if (end - start !== expected.byteLength) return false;
|
|
1489
|
+
for (let index = 0; index < expected.byteLength; index += 1)
|
|
1490
|
+
if (this.#bytes[start + index] !== expected[index]) return false;
|
|
1491
|
+
return true;
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
const compactJsonWhitespaceByte = (byte: number): boolean =>
|
|
1496
|
+
byte === 0x20 || byte === 0x09 || byte === 0x0a || byte === 0x0d;
|
|
1497
|
+
|
|
1498
|
+
const compactJsonNumberByte = (byte: number): boolean =>
|
|
1499
|
+
(byte >= 0x30 && byte <= 0x39) ||
|
|
1500
|
+
byte === 0x65 ||
|
|
1501
|
+
byte === 0x45 ||
|
|
1502
|
+
byte === 0x2b ||
|
|
1503
|
+
byte === 0x2d ||
|
|
1504
|
+
byte === 0x2e;
|
|
1505
|
+
|
|
1506
|
+
class CooperativeJsonParser {
|
|
1507
|
+
readonly #source: CooperativeJsonSource;
|
|
1508
|
+
readonly #yieldControl: () => Promise<void>;
|
|
1509
|
+
#index = 0;
|
|
1510
|
+
#work = 0;
|
|
1511
|
+
readonly #retainRowsAsSlices: boolean;
|
|
1512
|
+
|
|
1513
|
+
constructor(
|
|
1514
|
+
source: CooperativeJsonSource,
|
|
1515
|
+
yieldControl: () => Promise<void>,
|
|
1516
|
+
retainRowsAsSlices = false,
|
|
1517
|
+
) {
|
|
1518
|
+
this.#source = source;
|
|
1519
|
+
this.#yieldControl = yieldControl;
|
|
1520
|
+
this.#retainRowsAsSlices = retainRowsAsSlices;
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
async parse(): Promise<unknown> {
|
|
1524
|
+
await this.#space();
|
|
1525
|
+
const value = await this.#value(0);
|
|
1526
|
+
await this.#space();
|
|
1527
|
+
if (this.#index !== this.#source.length) throw new SyntaxError("Trailing compact JSON input");
|
|
1528
|
+
return value;
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
#checkpoint(amount = 1): Promise<void> | null {
|
|
1532
|
+
this.#work += amount;
|
|
1533
|
+
if (this.#work < COOPERATIVE_JSON_WORK_CHARS) return null;
|
|
1534
|
+
this.#work = 0;
|
|
1535
|
+
return this.#yieldControl();
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
async #space(): Promise<void> {
|
|
1539
|
+
while (compactJsonWhitespaceByte(this.#source.byteAt(this.#index))) {
|
|
1540
|
+
this.#index += 1;
|
|
1541
|
+
const checkpoint = this.#checkpoint();
|
|
1542
|
+
if (checkpoint) await checkpoint;
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
async #value(depth: number): Promise<unknown> {
|
|
1547
|
+
if (depth > 64) throw new SyntaxError("Compact JSON nesting exceeded");
|
|
1548
|
+
await this.#space();
|
|
1549
|
+
const head = this.#source.byteAt(this.#index);
|
|
1550
|
+
if (head === 0x22) return this.#string();
|
|
1551
|
+
if (head === 0x5b) return this.#array(depth + 1);
|
|
1552
|
+
if (head === 0x7b) return this.#object(depth + 1);
|
|
1553
|
+
if (head === 0x74 && this.#source.startsWithAscii("true", this.#index)) {
|
|
1554
|
+
this.#index += 4;
|
|
1555
|
+
const checkpoint = this.#checkpoint(4);
|
|
1556
|
+
if (checkpoint) await checkpoint;
|
|
1557
|
+
return true;
|
|
1558
|
+
}
|
|
1559
|
+
if (head === 0x66 && this.#source.startsWithAscii("false", this.#index)) {
|
|
1560
|
+
this.#index += 5;
|
|
1561
|
+
const checkpoint = this.#checkpoint(5);
|
|
1562
|
+
if (checkpoint) await checkpoint;
|
|
1563
|
+
return false;
|
|
1564
|
+
}
|
|
1565
|
+
if (head === 0x6e && this.#source.startsWithAscii("null", this.#index)) {
|
|
1566
|
+
this.#index += 4;
|
|
1567
|
+
const checkpoint = this.#checkpoint(4);
|
|
1568
|
+
if (checkpoint) await checkpoint;
|
|
1569
|
+
return null;
|
|
1570
|
+
}
|
|
1571
|
+
return this.#number();
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
async #string(): Promise<string> {
|
|
1575
|
+
const start = this.#index;
|
|
1576
|
+
this.#index += 1;
|
|
1577
|
+
let checkpoint = this.#checkpoint();
|
|
1578
|
+
if (checkpoint) await checkpoint;
|
|
1579
|
+
let escaped = false;
|
|
1580
|
+
while (this.#index < this.#source.length) {
|
|
1581
|
+
const character = this.#source.byteAt(this.#index);
|
|
1582
|
+
this.#index += 1;
|
|
1583
|
+
checkpoint = this.#checkpoint();
|
|
1584
|
+
if (checkpoint) await checkpoint;
|
|
1585
|
+
if (!escaped && character === 0x22) {
|
|
1586
|
+
const token = this.#source.slice(start, this.#index);
|
|
1587
|
+
return JSON.parse(token) as string;
|
|
1588
|
+
}
|
|
1589
|
+
escaped = !escaped && character === 0x5c;
|
|
1590
|
+
if (character !== 0x5c) escaped = false;
|
|
1591
|
+
if (this.#index - start > COOPERATIVE_JSON_MAX_STRING_CHARS)
|
|
1592
|
+
throw new SyntaxError("Compact JSON string exceeded");
|
|
1593
|
+
}
|
|
1594
|
+
throw new SyntaxError("Unterminated compact JSON string");
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
async #number(): Promise<number> {
|
|
1598
|
+
const start = this.#index;
|
|
1599
|
+
while (compactJsonNumberByte(this.#source.byteAt(this.#index))) {
|
|
1600
|
+
this.#index += 1;
|
|
1601
|
+
const checkpoint = this.#checkpoint();
|
|
1602
|
+
if (checkpoint) await checkpoint;
|
|
1603
|
+
if (this.#index - start > 64) throw new SyntaxError("Compact JSON number exceeded");
|
|
1604
|
+
}
|
|
1605
|
+
if (this.#index === start) throw new SyntaxError("Invalid compact JSON value");
|
|
1606
|
+
const token = this.#source.slice(start, this.#index);
|
|
1607
|
+
const value = Number(token);
|
|
1608
|
+
if (!Number.isFinite(value) || JSON.parse(token) !== value)
|
|
1609
|
+
throw new SyntaxError("Invalid compact JSON number");
|
|
1610
|
+
return value;
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
async #array(depth: number): Promise<unknown> {
|
|
1614
|
+
const start = this.#index;
|
|
1615
|
+
if (
|
|
1616
|
+
this.#retainRowsAsSlices &&
|
|
1617
|
+
(this.#source.startsWithAscii("[[0,[[", start) ||
|
|
1618
|
+
this.#source.startsWithAscii("[[1,[[", start))
|
|
1619
|
+
)
|
|
1620
|
+
return this.#rowsSlice(start);
|
|
1621
|
+
if (this.#retainRowsAsSlices && this.#looksLikeCompactRow(start)) return this.#rowSlice(start);
|
|
1622
|
+
this.#index += 1;
|
|
1623
|
+
let checkpoint = this.#checkpoint();
|
|
1624
|
+
if (checkpoint) await checkpoint;
|
|
1625
|
+
const values: unknown[] = [];
|
|
1626
|
+
await this.#space();
|
|
1627
|
+
if (this.#source.byteAt(this.#index) === 0x5d) {
|
|
1628
|
+
this.#index += 1;
|
|
1629
|
+
checkpoint = this.#checkpoint();
|
|
1630
|
+
if (checkpoint) await checkpoint;
|
|
1631
|
+
return values;
|
|
1632
|
+
}
|
|
1633
|
+
while (true) {
|
|
1634
|
+
values.push(await this.#value(depth));
|
|
1635
|
+
await this.#space();
|
|
1636
|
+
const separator = this.#source.byteAt(this.#index);
|
|
1637
|
+
this.#index += 1;
|
|
1638
|
+
checkpoint = this.#checkpoint();
|
|
1639
|
+
if (checkpoint) await checkpoint;
|
|
1640
|
+
if (separator === 0x5d) {
|
|
1641
|
+
if (
|
|
1642
|
+
this.#retainRowsAsSlices &&
|
|
1643
|
+
values.length === 2 &&
|
|
1644
|
+
(values[0] === 0 || values[0] === 1) &&
|
|
1645
|
+
Array.isArray(values[1]) &&
|
|
1646
|
+
values[1].every(
|
|
1647
|
+
(run) =>
|
|
1648
|
+
Array.isArray(run) &&
|
|
1649
|
+
run.length === 6 &&
|
|
1650
|
+
typeof run[0] === "number" &&
|
|
1651
|
+
typeof run[1] === "string",
|
|
1652
|
+
)
|
|
1653
|
+
)
|
|
1654
|
+
return Object.freeze({
|
|
1655
|
+
kind: COMPACT_PARSED_ROW_SLICE,
|
|
1656
|
+
source: this.#source,
|
|
1657
|
+
start,
|
|
1658
|
+
end: this.#index,
|
|
1659
|
+
}) satisfies CompactParsedRowSlice;
|
|
1660
|
+
return values;
|
|
1661
|
+
}
|
|
1662
|
+
if (separator !== 0x2c) throw new SyntaxError("Invalid compact JSON array");
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
#looksLikeCompactRow(start: number): boolean {
|
|
1667
|
+
if (
|
|
1668
|
+
!this.#source.startsWithAscii("[0,[[", start) &&
|
|
1669
|
+
!this.#source.startsWithAscii("[1,[[", start)
|
|
1670
|
+
)
|
|
1671
|
+
return false;
|
|
1672
|
+
let index = start + 5;
|
|
1673
|
+
const countStart = index;
|
|
1674
|
+
while (this.#source.byteAt(index) >= 0x30 && this.#source.byteAt(index) <= 0x39) index += 1;
|
|
1675
|
+
return (
|
|
1676
|
+
index > countStart &&
|
|
1677
|
+
this.#source.byteAt(index) === 0x2c &&
|
|
1678
|
+
this.#source.byteAt(index + 1) === 0x22
|
|
1679
|
+
);
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
async #rowSlice(start: number): Promise<CompactParsedRowSlice> {
|
|
1683
|
+
const slice = await this.#balancedArraySlice(start);
|
|
1684
|
+
return Object.freeze({ kind: COMPACT_PARSED_ROW_SLICE, ...slice });
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
async #rowsSlice(start: number): Promise<CompactParsedRowsSlice> {
|
|
1688
|
+
const slice = await this.#balancedArraySlice(start);
|
|
1689
|
+
return Object.freeze({ kind: COMPACT_PARSED_ROWS_SLICE, ...slice });
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
async #balancedArraySlice(
|
|
1693
|
+
start: number,
|
|
1694
|
+
): Promise<{ source: CooperativeJsonSource; start: number; end: number }> {
|
|
1695
|
+
let arrayDepth = 0;
|
|
1696
|
+
let inString = false;
|
|
1697
|
+
let escaped = false;
|
|
1698
|
+
while (this.#index < this.#source.length) {
|
|
1699
|
+
const character = this.#source.byteAt(this.#index);
|
|
1700
|
+
this.#index += 1;
|
|
1701
|
+
const checkpoint = this.#checkpoint();
|
|
1702
|
+
if (checkpoint) await checkpoint;
|
|
1703
|
+
if (inString) {
|
|
1704
|
+
if (!escaped && character === 0x22) inString = false;
|
|
1705
|
+
escaped = !escaped && character === 0x5c;
|
|
1706
|
+
if (character !== 0x5c) escaped = false;
|
|
1707
|
+
continue;
|
|
1708
|
+
}
|
|
1709
|
+
if (character === 0x22) {
|
|
1710
|
+
inString = true;
|
|
1711
|
+
continue;
|
|
1712
|
+
}
|
|
1713
|
+
if (character === 0x5b) arrayDepth += 1;
|
|
1714
|
+
else if (character === 0x5d) {
|
|
1715
|
+
arrayDepth -= 1;
|
|
1716
|
+
if (arrayDepth === 0) return { source: this.#source, start, end: this.#index };
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
throw new SyntaxError("Unterminated compact JSON array");
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
async #object(depth: number): Promise<Record<string, unknown>> {
|
|
1723
|
+
this.#index += 1;
|
|
1724
|
+
let checkpoint = this.#checkpoint();
|
|
1725
|
+
if (checkpoint) await checkpoint;
|
|
1726
|
+
const value: Record<string, unknown> = {};
|
|
1727
|
+
await this.#space();
|
|
1728
|
+
if (this.#source.byteAt(this.#index) === 0x7d) {
|
|
1729
|
+
this.#index += 1;
|
|
1730
|
+
checkpoint = this.#checkpoint();
|
|
1731
|
+
if (checkpoint) await checkpoint;
|
|
1732
|
+
return value;
|
|
1733
|
+
}
|
|
1734
|
+
while (true) {
|
|
1735
|
+
if (this.#source.byteAt(this.#index) !== 0x22)
|
|
1736
|
+
throw new SyntaxError("Invalid compact JSON key");
|
|
1737
|
+
const key = await this.#string();
|
|
1738
|
+
await this.#space();
|
|
1739
|
+
if (this.#source.byteAt(this.#index) !== 0x3a)
|
|
1740
|
+
throw new SyntaxError("Invalid compact JSON object");
|
|
1741
|
+
this.#index += 1;
|
|
1742
|
+
checkpoint = this.#checkpoint();
|
|
1743
|
+
if (checkpoint) await checkpoint;
|
|
1744
|
+
Object.defineProperty(value, key, {
|
|
1745
|
+
configurable: true,
|
|
1746
|
+
enumerable: true,
|
|
1747
|
+
value: await this.#value(depth),
|
|
1748
|
+
writable: true,
|
|
1749
|
+
});
|
|
1750
|
+
await this.#space();
|
|
1751
|
+
const separator = this.#source.byteAt(this.#index);
|
|
1752
|
+
this.#index += 1;
|
|
1753
|
+
checkpoint = this.#checkpoint();
|
|
1754
|
+
if (checkpoint) await checkpoint;
|
|
1755
|
+
if (separator === 0x7d) return value;
|
|
1756
|
+
if (separator !== 0x2c) throw new SyntaxError("Invalid compact JSON object");
|
|
1757
|
+
await this.#space();
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
interface CompactCooperativeControl {
|
|
1763
|
+
readonly yieldControl: () => Promise<void>;
|
|
1764
|
+
readonly rowsPerSlice: number;
|
|
1765
|
+
rowsSinceYield: number;
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
type ValidatedCompactRun = readonly [number, TerminalReplicaCell];
|
|
1769
|
+
|
|
1770
|
+
const compactDecodedColorKey = (color: TerminalReplicaColor): number =>
|
|
1771
|
+
color.kind === "default" ? -1 : color.kind === "indexed" ? color.index : 256 + color.value;
|
|
1772
|
+
|
|
1773
|
+
function compactInternDecodedCell(
|
|
1774
|
+
budget: CompactDecodeBudget,
|
|
1775
|
+
grapheme: string,
|
|
1776
|
+
width: 0 | 1 | 2,
|
|
1777
|
+
foreground: TerminalReplicaColor,
|
|
1778
|
+
background: TerminalReplicaColor,
|
|
1779
|
+
attributesValue: number,
|
|
1780
|
+
): TerminalReplicaCell {
|
|
1781
|
+
const cache = budget.decodedCellCache;
|
|
1782
|
+
if (!cache)
|
|
1783
|
+
return Object.freeze({ grapheme, width, foreground, background, attributes: attributesValue });
|
|
1784
|
+
const widths = cache.get(grapheme) ?? new Map();
|
|
1785
|
+
cache.set(grapheme, widths);
|
|
1786
|
+
const foregroundKey = compactDecodedColorKey(foreground);
|
|
1787
|
+
const foregrounds = widths.get(width) ?? new Map();
|
|
1788
|
+
widths.set(width, foregrounds);
|
|
1789
|
+
const backgroundKey = compactDecodedColorKey(background);
|
|
1790
|
+
const backgrounds = foregrounds.get(foregroundKey) ?? new Map();
|
|
1791
|
+
foregrounds.set(foregroundKey, backgrounds);
|
|
1792
|
+
const attributes = backgrounds.get(backgroundKey) ?? new Map();
|
|
1793
|
+
backgrounds.set(backgroundKey, attributes);
|
|
1794
|
+
const cached = attributes.get(attributesValue);
|
|
1795
|
+
if (cached) return cached;
|
|
1796
|
+
const cell = Object.freeze({
|
|
1797
|
+
grapheme,
|
|
1798
|
+
width,
|
|
1799
|
+
foreground,
|
|
1800
|
+
background,
|
|
1801
|
+
attributes: attributesValue,
|
|
1802
|
+
});
|
|
1803
|
+
attributes.set(attributesValue, cell);
|
|
1804
|
+
budget.validatedCellAllocations += 1;
|
|
1805
|
+
return cell;
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
function compactRowReuseCollision(
|
|
1809
|
+
value: TerminalReplicaRow | readonly TerminalReplicaRow[],
|
|
1810
|
+
): value is readonly TerminalReplicaRow[] {
|
|
1811
|
+
return Array.isArray(value);
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
async function compactRawRowReuseIndex(
|
|
1815
|
+
rows: readonly TerminalReplicaRow[],
|
|
1816
|
+
control: CompactCooperativeControl,
|
|
1817
|
+
): Promise<ValidatedCompactRawRowIndex> {
|
|
1818
|
+
const cacheable = Object.isFrozen(rows);
|
|
1819
|
+
const cachedIndex = cacheable ? VALIDATED_COMPACT_RAW_ROW_INDEX.get(rows) : undefined;
|
|
1820
|
+
if (cachedIndex) return cachedIndex;
|
|
1821
|
+
const index = new Map<number, ValidatedCompactRawRowCandidate>();
|
|
1822
|
+
let indexedRows = 0;
|
|
1823
|
+
for (const row of rows) {
|
|
1824
|
+
const cached = VALIDATED_COMPACT_RAW_ROW.get(row);
|
|
1825
|
+
if (cached) {
|
|
1826
|
+
const existing = index.get(cached.rawHash);
|
|
1827
|
+
if (!existing) index.set(cached.rawHash, cached);
|
|
1828
|
+
else if (existing.row !== row && !existing.collisions?.some((entry) => entry.row === row))
|
|
1829
|
+
index.set(
|
|
1830
|
+
cached.rawHash,
|
|
1831
|
+
Object.freeze({
|
|
1832
|
+
...existing,
|
|
1833
|
+
collisions: Object.freeze([...(existing.collisions ?? []), cached]),
|
|
1834
|
+
}),
|
|
1835
|
+
);
|
|
1836
|
+
}
|
|
1837
|
+
indexedRows += 1;
|
|
1838
|
+
if (indexedRows % control.rowsPerSlice === 0) await control.yieldControl();
|
|
1839
|
+
}
|
|
1840
|
+
if (cacheable) VALIDATED_COMPACT_RAW_ROW_INDEX.set(rows, index);
|
|
1841
|
+
return index;
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
async function compactBaselineRowReuseIndex(
|
|
1845
|
+
baseline: TerminalReplicaSnapshot,
|
|
1846
|
+
control: CompactCooperativeControl,
|
|
1847
|
+
): Promise<ReadonlyMap<string, TerminalReplicaRow | readonly TerminalReplicaRow[]>> {
|
|
1848
|
+
const index = new Map<string, TerminalReplicaRow | readonly TerminalReplicaRow[]>();
|
|
1849
|
+
let rowsSinceYield = 0;
|
|
1850
|
+
for (const rows of [baseline.grid, baseline.history]) {
|
|
1851
|
+
for (const row of rows) {
|
|
1852
|
+
if (VALIDATED_COMPACT_RAW_ROW.has(row)) continue;
|
|
1853
|
+
if (!isTerminalReplicaRowDeeplyFrozen(row)) continue;
|
|
1854
|
+
const digest = await hashTerminalReplicaRowCooperatively(row, control.yieldControl);
|
|
1855
|
+
const existing = index.get(digest);
|
|
1856
|
+
if (!existing) index.set(digest, row);
|
|
1857
|
+
else if (compactRowReuseCollision(existing)) {
|
|
1858
|
+
if (!existing.includes(row)) index.set(digest, Object.freeze([...existing, row]));
|
|
1859
|
+
} else if (existing !== row) index.set(digest, Object.freeze([existing, row]));
|
|
1860
|
+
rowsSinceYield += 1;
|
|
1861
|
+
if (rowsSinceYield >= control.rowsPerSlice) {
|
|
1862
|
+
rowsSinceYield = 0;
|
|
1863
|
+
await control.yieldControl();
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
return index;
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
function compactColorsEqual(left: TerminalReplicaColor, right: TerminalReplicaColor): boolean {
|
|
1871
|
+
return (
|
|
1872
|
+
left.kind === right.kind &&
|
|
1873
|
+
(left.kind === "default" ||
|
|
1874
|
+
(left.kind === "indexed" && right.kind === "indexed" && left.index === right.index) ||
|
|
1875
|
+
(left.kind === "rgb" && right.kind === "rgb" && left.value === right.value))
|
|
1876
|
+
);
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
async function compactRowMatchesRuns(
|
|
1880
|
+
row: TerminalReplicaRow,
|
|
1881
|
+
wrapped: boolean,
|
|
1882
|
+
runs: readonly ValidatedCompactRun[],
|
|
1883
|
+
cellCount: number,
|
|
1884
|
+
control: CompactCooperativeControl,
|
|
1885
|
+
): Promise<boolean> {
|
|
1886
|
+
if (row.wrapped !== wrapped || row.cells.length !== cellCount) return false;
|
|
1887
|
+
let offset = 0;
|
|
1888
|
+
let work = 0;
|
|
1889
|
+
for (const [count, cell] of runs) {
|
|
1890
|
+
for (let index = 0; index < count; index += 1) {
|
|
1891
|
+
const candidate = row.cells[offset++];
|
|
1892
|
+
if (
|
|
1893
|
+
!candidate ||
|
|
1894
|
+
candidate.grapheme !== cell.grapheme ||
|
|
1895
|
+
candidate.width !== cell.width ||
|
|
1896
|
+
candidate.attributes !== cell.attributes ||
|
|
1897
|
+
!compactColorsEqual(candidate.foreground, cell.foreground) ||
|
|
1898
|
+
!compactColorsEqual(candidate.background, cell.background)
|
|
1899
|
+
)
|
|
1900
|
+
return false;
|
|
1901
|
+
work += 1;
|
|
1902
|
+
if (work >= COOPERATIVE_CELL_WORK) {
|
|
1903
|
+
work = 0;
|
|
1904
|
+
await control.yieldControl();
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
return offset === cellCount;
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
async function expandRowsCooperatively(
|
|
1912
|
+
value: unknown,
|
|
1913
|
+
budget: CompactDecodeBudget,
|
|
1914
|
+
cols: number | null,
|
|
1915
|
+
maximum: number,
|
|
1916
|
+
label: string,
|
|
1917
|
+
control: CompactCooperativeControl,
|
|
1918
|
+
): Promise<TerminalReplicaRow[]> {
|
|
1919
|
+
if (
|
|
1920
|
+
value !== null &&
|
|
1921
|
+
typeof value === "object" &&
|
|
1922
|
+
(value as Partial<CompactParsedRowsSlice>).kind === COMPACT_PARSED_ROWS_SLICE
|
|
1923
|
+
)
|
|
1924
|
+
return expandRowsSliceCooperatively(
|
|
1925
|
+
value as CompactParsedRowsSlice,
|
|
1926
|
+
budget,
|
|
1927
|
+
cols,
|
|
1928
|
+
maximum,
|
|
1929
|
+
label,
|
|
1930
|
+
control,
|
|
1931
|
+
);
|
|
1932
|
+
const inputs = compactArrayAtMost(value, maximum, label);
|
|
1933
|
+
if (label === "grid" && cols !== null && inputs.length !== maximum)
|
|
1934
|
+
throw new TypeError("Compact grid cardinality mismatch");
|
|
1935
|
+
const rows: TerminalReplicaRow[] = [];
|
|
1936
|
+
for (let index = 0; index < inputs.length; index += 1) {
|
|
1937
|
+
const input = inputs[index];
|
|
1938
|
+
const row = await expandRowCooperatively(input, budget, cols, control);
|
|
1939
|
+
// Prime only immutable, structurally validated rows. A failed envelope
|
|
1940
|
+
// cannot expose these WeakMap keys or mint a reducer capability.
|
|
1941
|
+
rows.push(row);
|
|
1942
|
+
// Release the parsed run/container graph as soon as its immutable row has
|
|
1943
|
+
// either been reused or constructed. The wire tree is never published.
|
|
1944
|
+
inputs[index] = null;
|
|
1945
|
+
control.rowsSinceYield += 1;
|
|
1946
|
+
if (control.rowsSinceYield >= control.rowsPerSlice) {
|
|
1947
|
+
control.rowsSinceYield = 0;
|
|
1948
|
+
await control.yieldControl();
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
return Object.freeze(rows) as TerminalReplicaRow[];
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
async function expandRowsSliceCooperatively(
|
|
1955
|
+
slice: CompactParsedRowsSlice,
|
|
1956
|
+
budget: CompactDecodeBudget,
|
|
1957
|
+
cols: number | null,
|
|
1958
|
+
maximum: number,
|
|
1959
|
+
label: string,
|
|
1960
|
+
control: CompactCooperativeControl,
|
|
1961
|
+
): Promise<TerminalReplicaRow[]> {
|
|
1962
|
+
const rows: TerminalReplicaRow[] = [];
|
|
1963
|
+
let index = slice.start + 1;
|
|
1964
|
+
const rowSlice = {
|
|
1965
|
+
kind: COMPACT_PARSED_ROW_SLICE,
|
|
1966
|
+
source: slice.source,
|
|
1967
|
+
start: index,
|
|
1968
|
+
end: index,
|
|
1969
|
+
};
|
|
1970
|
+
while (index < slice.end - 1) {
|
|
1971
|
+
if (rows.length >= maximum) throw new TypeError(`Invalid compact ${label}`);
|
|
1972
|
+
const start = index;
|
|
1973
|
+
let depth = 0;
|
|
1974
|
+
let inString = false;
|
|
1975
|
+
let escaped = false;
|
|
1976
|
+
let work = 0;
|
|
1977
|
+
while (index < slice.end) {
|
|
1978
|
+
const character = slice.source.byteAt(index++);
|
|
1979
|
+
work += 1;
|
|
1980
|
+
if (work >= COOPERATIVE_JSON_WORK_CHARS) {
|
|
1981
|
+
work = 0;
|
|
1982
|
+
await control.yieldControl();
|
|
1983
|
+
}
|
|
1984
|
+
if (inString) {
|
|
1985
|
+
if (!escaped && character === 0x22) inString = false;
|
|
1986
|
+
escaped = !escaped && character === 0x5c;
|
|
1987
|
+
if (character !== 0x5c) escaped = false;
|
|
1988
|
+
continue;
|
|
1989
|
+
}
|
|
1990
|
+
if (character === 0x22) inString = true;
|
|
1991
|
+
else if (character === 0x5b) depth += 1;
|
|
1992
|
+
else if (character === 0x5d) {
|
|
1993
|
+
depth -= 1;
|
|
1994
|
+
if (depth === 0) break;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
if (depth !== 0) throw new SyntaxError("Unterminated compact JSON row");
|
|
1998
|
+
rowSlice.start = start;
|
|
1999
|
+
rowSlice.end = index;
|
|
2000
|
+
rows.push(await expandRowCooperatively(rowSlice, budget, cols, control));
|
|
2001
|
+
control.rowsSinceYield += 1;
|
|
2002
|
+
if (control.rowsSinceYield >= control.rowsPerSlice) {
|
|
2003
|
+
control.rowsSinceYield = 0;
|
|
2004
|
+
await control.yieldControl();
|
|
2005
|
+
}
|
|
2006
|
+
const separator = slice.source.byteAt(index++);
|
|
2007
|
+
if (separator === 0x5d) break;
|
|
2008
|
+
if (separator !== 0x2c) throw new SyntaxError("Invalid compact JSON rows");
|
|
2009
|
+
}
|
|
2010
|
+
if (label === "grid" && cols !== null && rows.length !== maximum)
|
|
2011
|
+
throw new TypeError("Compact grid cardinality mismatch");
|
|
2012
|
+
return Object.freeze(rows) as TerminalReplicaRow[];
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
async function expandRowCooperatively(
|
|
2016
|
+
value: unknown,
|
|
2017
|
+
budget: CompactDecodeBudget,
|
|
2018
|
+
cols: number | null,
|
|
2019
|
+
control: CompactCooperativeControl,
|
|
2020
|
+
): Promise<TerminalReplicaRow> {
|
|
2021
|
+
if (++budget.rows > COMPACT_MAX_ROWS) throw new TypeError("Compact semantic row budget exceeded");
|
|
2022
|
+
const parsedSlice =
|
|
2023
|
+
value !== null &&
|
|
2024
|
+
typeof value === "object" &&
|
|
2025
|
+
(value as Partial<CompactParsedRowSlice>).kind === COMPACT_PARSED_ROW_SLICE
|
|
2026
|
+
? (value as CompactParsedRowSlice)
|
|
2027
|
+
: null;
|
|
2028
|
+
const rawHash = parsedSlice ? parsedSlice.source.hash(parsedSlice.start, parsedSlice.end) : null;
|
|
2029
|
+
const transactionIndexed = rawHash === null ? undefined : budget.transactionRows?.get(rawHash);
|
|
2030
|
+
const match = (candidate: ValidatedCompactRawRowCandidate | undefined) =>
|
|
2031
|
+
!candidate || !parsedSlice
|
|
2032
|
+
? undefined
|
|
2033
|
+
: parsedSlice.source.equals(parsedSlice.start, parsedSlice.end, candidate.raw)
|
|
2034
|
+
? candidate
|
|
2035
|
+
: candidate.collisions?.find((entry) =>
|
|
2036
|
+
parsedSlice.source.equals(parsedSlice.start, parsedSlice.end, entry.raw),
|
|
2037
|
+
);
|
|
2038
|
+
let rawReuse = match(transactionIndexed);
|
|
2039
|
+
if (!rawReuse && rawHash !== null) {
|
|
2040
|
+
for (const index of budget.rawRowReuseIndexes ?? []) {
|
|
2041
|
+
rawReuse = match(index.get(rawHash));
|
|
2042
|
+
if (rawReuse) break;
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
if (rawReuse) {
|
|
2046
|
+
if (cols !== null && rawReuse.cells !== cols) throw new TypeError("Compact row width mismatch");
|
|
2047
|
+
budget.runs += rawReuse.runs;
|
|
2048
|
+
budget.cells += rawReuse.cells;
|
|
2049
|
+
if (budget.runs > COMPACT_MAX_RUNS) throw new TypeError("Compact semantic run budget exceeded");
|
|
2050
|
+
if (budget.cells > budget.maxCells)
|
|
2051
|
+
throw new TypeError("Compact semantic expanded cell budget exceeded");
|
|
2052
|
+
budget.reusedRows += 1;
|
|
2053
|
+
return rawReuse.row;
|
|
2054
|
+
}
|
|
2055
|
+
const parsedValue = await expandParsedRowSlice(value, control);
|
|
2056
|
+
const input = compactArray(parsedValue, 2, "row");
|
|
2057
|
+
const wrapped = compactBit(input[0], "wrapped");
|
|
2058
|
+
const runs = compactArrayAtMost(input[1], COMPACT_MAX_RUNS, "cell runs");
|
|
2059
|
+
const validatedRuns: ValidatedCompactRun[] = [];
|
|
2060
|
+
let cellCount = 0;
|
|
2061
|
+
let cellWork = 0;
|
|
2062
|
+
for (const valueRun of runs) {
|
|
2063
|
+
if (++budget.runs > COMPACT_MAX_RUNS)
|
|
2064
|
+
throw new TypeError("Compact semantic run budget exceeded");
|
|
2065
|
+
const run = compactArray(valueRun, 6, "cell run");
|
|
2066
|
+
const count = compactInteger(run[0], 1, COMPACT_MAX_DIMENSION, "cell run count");
|
|
2067
|
+
budget.cells += count;
|
|
2068
|
+
cellCount += count;
|
|
2069
|
+
if (budget.cells > budget.maxCells)
|
|
2070
|
+
throw new TypeError("Compact semantic expanded cell budget exceeded");
|
|
2071
|
+
const cell = compactInternDecodedCell(
|
|
2072
|
+
budget,
|
|
2073
|
+
compactString(run[1], "grapheme"),
|
|
2074
|
+
compactInteger(run[2], 0, 2, "cell width") as 0 | 1 | 2,
|
|
2075
|
+
expandColor(run[3]),
|
|
2076
|
+
expandColor(run[4]),
|
|
2077
|
+
compactInteger(run[5], 0, 0xff, "cell attributes"),
|
|
2078
|
+
);
|
|
2079
|
+
run.length = 2;
|
|
2080
|
+
run[0] = count;
|
|
2081
|
+
run[1] = cell;
|
|
2082
|
+
validatedRuns.push(run as unknown as ValidatedCompactRun);
|
|
2083
|
+
for (let index = 0; index < count; index += 1) {
|
|
2084
|
+
cellWork += 1;
|
|
2085
|
+
if (cellWork >= COOPERATIVE_CELL_WORK) {
|
|
2086
|
+
cellWork = 0;
|
|
2087
|
+
await control.yieldControl();
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
if (cols !== null && cellCount !== cols) throw new TypeError("Compact row width mismatch");
|
|
2092
|
+
if (cellCount < 1 || cellCount > COMPACT_MAX_DIMENSION)
|
|
2093
|
+
throw new TypeError("Compact row width is out of bounds");
|
|
2094
|
+
let previousWidth: 0 | 1 | 2 | null = null;
|
|
2095
|
+
let validatedCells = 0;
|
|
2096
|
+
for (const [count, cell] of validatedRuns) {
|
|
2097
|
+
for (let index = 0; index < count; index += 1) {
|
|
2098
|
+
if (previousWidth === 2 && cell.width !== 0)
|
|
2099
|
+
throw new TypeError("Malformed compact wide cell");
|
|
2100
|
+
if (cell.width === 0 && previousWidth !== 2)
|
|
2101
|
+
throw new TypeError("Malformed compact continuation cell");
|
|
2102
|
+
previousWidth = cell.width;
|
|
2103
|
+
validatedCells += 1;
|
|
2104
|
+
if (validatedCells % COOPERATIVE_CELL_WORK === 0) await control.yieldControl();
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
if (previousWidth === 2) throw new TypeError("Malformed compact wide cell");
|
|
2108
|
+
const digest = await hashTerminalReplicaRowRunsCooperatively(
|
|
2109
|
+
wrapped,
|
|
2110
|
+
cellCount,
|
|
2111
|
+
validatedRuns,
|
|
2112
|
+
control.yieldControl,
|
|
2113
|
+
64,
|
|
2114
|
+
(bytes) => {
|
|
2115
|
+
budget.canonicalUtf8Allocations += 1;
|
|
2116
|
+
budget.canonicalUtf8Bytes += bytes;
|
|
2117
|
+
},
|
|
2118
|
+
budget.runEncodingCache,
|
|
2119
|
+
);
|
|
2120
|
+
const indexed = budget.rowReuseIndex?.get(digest);
|
|
2121
|
+
if (
|
|
2122
|
+
indexed &&
|
|
2123
|
+
!compactRowReuseCollision(indexed) &&
|
|
2124
|
+
(await compactRowMatchesRuns(indexed, wrapped, validatedRuns, cellCount, control))
|
|
2125
|
+
) {
|
|
2126
|
+
budget.reusedRows += 1;
|
|
2127
|
+
if (parsedSlice !== null && rawHash !== null)
|
|
2128
|
+
VALIDATED_COMPACT_RAW_ROW.set(
|
|
2129
|
+
indexed,
|
|
2130
|
+
Object.freeze({
|
|
2131
|
+
row: indexed,
|
|
2132
|
+
raw: parsedSlice.source.bytes(parsedSlice.start, parsedSlice.end).slice(),
|
|
2133
|
+
rawHash,
|
|
2134
|
+
runs: validatedRuns.length,
|
|
2135
|
+
cells: cellCount,
|
|
2136
|
+
}),
|
|
2137
|
+
);
|
|
2138
|
+
rememberTransactionRow(budget, indexed);
|
|
2139
|
+
return indexed;
|
|
2140
|
+
}
|
|
2141
|
+
if (indexed && compactRowReuseCollision(indexed)) {
|
|
2142
|
+
for (const candidate of indexed) {
|
|
2143
|
+
if (!(await compactRowMatchesRuns(candidate, wrapped, validatedRuns, cellCount, control)))
|
|
2144
|
+
continue;
|
|
2145
|
+
budget.reusedRows += 1;
|
|
2146
|
+
if (parsedSlice !== null && rawHash !== null)
|
|
2147
|
+
VALIDATED_COMPACT_RAW_ROW.set(
|
|
2148
|
+
candidate,
|
|
2149
|
+
Object.freeze({
|
|
2150
|
+
row: candidate,
|
|
2151
|
+
raw: parsedSlice.source.bytes(parsedSlice.start, parsedSlice.end).slice(),
|
|
2152
|
+
rawHash,
|
|
2153
|
+
runs: validatedRuns.length,
|
|
2154
|
+
cells: cellCount,
|
|
2155
|
+
}),
|
|
2156
|
+
);
|
|
2157
|
+
rememberTransactionRow(budget, candidate);
|
|
2158
|
+
return candidate;
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
const cells: TerminalReplicaCell[] = [];
|
|
2162
|
+
let allocatedWork = 0;
|
|
2163
|
+
for (const [count, cell] of validatedRuns) {
|
|
2164
|
+
for (let index = 0; index < count; index += 1) {
|
|
2165
|
+
cells.push(cell);
|
|
2166
|
+
allocatedWork += 1;
|
|
2167
|
+
if (allocatedWork >= COOPERATIVE_CELL_WORK) {
|
|
2168
|
+
allocatedWork = 0;
|
|
2169
|
+
await control.yieldControl();
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
const row = Object.freeze({
|
|
2174
|
+
cells: Object.freeze(cells),
|
|
2175
|
+
wrapped,
|
|
2176
|
+
}) as unknown as TerminalReplicaRow;
|
|
2177
|
+
budget.allocatedCells += cells.length;
|
|
2178
|
+
primeTerminalReplicaRowHash(row, digest);
|
|
2179
|
+
if (parsedSlice !== null && rawHash !== null)
|
|
2180
|
+
VALIDATED_COMPACT_RAW_ROW.set(
|
|
2181
|
+
row,
|
|
2182
|
+
Object.freeze({
|
|
2183
|
+
row,
|
|
2184
|
+
raw: parsedSlice.source.bytes(parsedSlice.start, parsedSlice.end).slice(),
|
|
2185
|
+
rawHash,
|
|
2186
|
+
runs: validatedRuns.length,
|
|
2187
|
+
cells: cellCount,
|
|
2188
|
+
}),
|
|
2189
|
+
);
|
|
2190
|
+
rememberTransactionRow(budget, row);
|
|
2191
|
+
return row;
|
|
2192
|
+
}
|
|
2193
|
+
|
|
2194
|
+
// Keep current-message reuse separate from the immutable baseline index. Cache
|
|
2195
|
+
// saturation only forfeits an optimization; it never changes validation.
|
|
2196
|
+
function rememberTransactionRow(budget: CompactDecodeBudget, row: TerminalReplicaRow): void {
|
|
2197
|
+
const cache = budget.transactionRows;
|
|
2198
|
+
const entry = VALIDATED_COMPACT_RAW_ROW.get(row);
|
|
2199
|
+
if (!cache || !entry) return;
|
|
2200
|
+
const bucket = cache.get(entry.rawHash);
|
|
2201
|
+
if (
|
|
2202
|
+
(bucket ? 1 + (bucket.collisions?.length ?? 0) : 0) >= 4 ||
|
|
2203
|
+
(budget.transactionRowCount ?? 0) >= 1024 ||
|
|
2204
|
+
(budget.transactionRowBytes ?? 0) + entry.raw.byteLength > 256 * 1024
|
|
2205
|
+
)
|
|
2206
|
+
return;
|
|
2207
|
+
if (bucket)
|
|
2208
|
+
cache.set(entry.rawHash, {
|
|
2209
|
+
...bucket,
|
|
2210
|
+
collisions: [...(bucket.collisions ?? []), entry],
|
|
2211
|
+
});
|
|
2212
|
+
else cache.set(entry.rawHash, entry);
|
|
2213
|
+
budget.transactionRowBytes = (budget.transactionRowBytes ?? 0) + entry.raw.byteLength;
|
|
2214
|
+
budget.transactionRowCount = (budget.transactionRowCount ?? 0) + 1;
|
|
2215
|
+
}
|
|
2216
|
+
|
|
2217
|
+
async function expandParsedRowSlice(
|
|
2218
|
+
value: unknown,
|
|
2219
|
+
control: CompactCooperativeControl,
|
|
2220
|
+
): Promise<unknown> {
|
|
2221
|
+
if (
|
|
2222
|
+
value === null ||
|
|
2223
|
+
typeof value !== "object" ||
|
|
2224
|
+
(value as Partial<CompactParsedRowSlice>).kind !== COMPACT_PARSED_ROW_SLICE
|
|
2225
|
+
)
|
|
2226
|
+
return value;
|
|
2227
|
+
const slice = value as CompactParsedRowSlice;
|
|
2228
|
+
// Ordinary terminal rows are small. Parsing their individual JSON tokens
|
|
2229
|
+
// through async methods adds thousands of microtasks to a full redraw.
|
|
2230
|
+
// Bound native parsing by encoded bytes; all row/run/cell validation and
|
|
2231
|
+
// expansion budgets still run below, and larger rows remain cooperative.
|
|
2232
|
+
if (slice.end - slice.start <= 8 * 1_024)
|
|
2233
|
+
return JSON.parse(slice.source.slice(slice.start, slice.end));
|
|
2234
|
+
const rowSource = new CooperativeJsonSource(slice.source.bytes(slice.start, slice.end));
|
|
2235
|
+
return new CooperativeJsonParser(rowSource, control.yieldControl).parse();
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
async function expandSnapshotCooperatively(
|
|
2239
|
+
value: unknown,
|
|
2240
|
+
budget: CompactDecodeBudget,
|
|
2241
|
+
control: CompactCooperativeControl,
|
|
2242
|
+
): Promise<TerminalReplicaSnapshot> {
|
|
2243
|
+
const input = compactArray(value, 8, "snapshot");
|
|
2244
|
+
const cols = compactInteger(input[0], 1, COMPACT_MAX_DIMENSION, "columns");
|
|
2245
|
+
const rows = compactInteger(input[1], 1, COMPACT_MAX_DIMENSION, "rows");
|
|
2246
|
+
const grid = await expandRowsCooperatively(input[2], budget, cols, rows, "grid", control);
|
|
2247
|
+
input[2] = null;
|
|
2248
|
+
const history = await expandRowsCooperatively(
|
|
2249
|
+
input[3],
|
|
2250
|
+
budget,
|
|
2251
|
+
cols,
|
|
2252
|
+
COMPACT_MAX_ROWS,
|
|
2253
|
+
"history",
|
|
2254
|
+
control,
|
|
2255
|
+
);
|
|
2256
|
+
input[3] = null;
|
|
2257
|
+
const snapshot = Object.freeze({
|
|
2258
|
+
cols,
|
|
2259
|
+
rows,
|
|
2260
|
+
grid,
|
|
2261
|
+
history,
|
|
2262
|
+
cursor: expandCursor(input[4], cols, rows),
|
|
2263
|
+
modes: expandModes(input[5]),
|
|
2264
|
+
placements: await expandPlacementsCooperatively(input[6], budget, control),
|
|
2265
|
+
bootstrap: expandBootstrap(input[7]),
|
|
2266
|
+
});
|
|
2267
|
+
if (
|
|
2268
|
+
snapshot.placements.some(
|
|
2269
|
+
(placement) =>
|
|
2270
|
+
placement.row + placement.rows > rows || placement.column + placement.columns > cols,
|
|
2271
|
+
)
|
|
2272
|
+
)
|
|
2273
|
+
throw new TypeError("Compact placement is outside the viewport");
|
|
2274
|
+
return snapshot as unknown as TerminalReplicaSnapshot;
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
async function expandPatchCooperatively(
|
|
2278
|
+
value: unknown,
|
|
2279
|
+
budget: CompactDecodeBudget,
|
|
2280
|
+
control: CompactCooperativeControl,
|
|
2281
|
+
): Promise<TerminalReplicaPatchPayload> {
|
|
2282
|
+
const input = compactArray(value, 8, "patch");
|
|
2283
|
+
const dimensions =
|
|
2284
|
+
input[0] === null
|
|
2285
|
+
? undefined
|
|
2286
|
+
: (() => {
|
|
2287
|
+
const pair = compactArray(input[0], 2, "dimensions");
|
|
2288
|
+
return Object.freeze({
|
|
2289
|
+
cols: compactInteger(pair[0], 1, COMPACT_MAX_DIMENSION, "columns"),
|
|
2290
|
+
rows: compactInteger(pair[1], 1, COMPACT_MAX_DIMENSION, "rows"),
|
|
2291
|
+
});
|
|
2292
|
+
})();
|
|
2293
|
+
const rowInputs = compactArrayAtMost(input[1], COMPACT_MAX_DIMENSION, "dirty rows");
|
|
2294
|
+
const rows = [];
|
|
2295
|
+
for (let index = 0; index < rowInputs.length; index += 1) {
|
|
2296
|
+
const entry = rowInputs[index];
|
|
2297
|
+
const pair = compactArray(entry, 2, "dirty row");
|
|
2298
|
+
const row = await expandRowCooperatively(pair[1], budget, dimensions?.cols ?? null, control);
|
|
2299
|
+
rows.push(
|
|
2300
|
+
Object.freeze({
|
|
2301
|
+
index: compactInteger(pair[0], 0, COMPACT_MAX_DIMENSION - 1, "row index"),
|
|
2302
|
+
row,
|
|
2303
|
+
}),
|
|
2304
|
+
);
|
|
2305
|
+
rowInputs[index] = null;
|
|
2306
|
+
control.rowsSinceYield += 1;
|
|
2307
|
+
if (control.rowsSinceYield >= control.rowsPerSlice) {
|
|
2308
|
+
control.rowsSinceYield = 0;
|
|
2309
|
+
await control.yieldControl();
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
const history =
|
|
2313
|
+
input[2] === null
|
|
2314
|
+
? undefined
|
|
2315
|
+
: await expandRowsCooperatively(input[2], budget, null, COMPACT_MAX_ROWS, "history", control);
|
|
2316
|
+
const historyDelta =
|
|
2317
|
+
input[3] === null
|
|
2318
|
+
? undefined
|
|
2319
|
+
: await (async () => {
|
|
2320
|
+
const pair = compactArray(input[3], 2, "history delta");
|
|
2321
|
+
return Object.freeze({
|
|
2322
|
+
trim: compactInteger(pair[0], 0, COMPACT_MAX_ROWS, "history trim"),
|
|
2323
|
+
append: await expandRowsCooperatively(
|
|
2324
|
+
pair[1],
|
|
2325
|
+
budget,
|
|
2326
|
+
null,
|
|
2327
|
+
COMPACT_MAX_ROWS,
|
|
2328
|
+
"history append",
|
|
2329
|
+
control,
|
|
2330
|
+
),
|
|
2331
|
+
});
|
|
2332
|
+
})();
|
|
2333
|
+
if (history && historyDelta) throw new TypeError("Compact patch has two history representations");
|
|
2334
|
+
return Object.freeze({
|
|
2335
|
+
...(dimensions ? { dimensions } : {}),
|
|
2336
|
+
rows: Object.freeze(rows),
|
|
2337
|
+
...(history ? { history } : {}),
|
|
2338
|
+
...(historyDelta ? { historyDelta } : {}),
|
|
2339
|
+
...(input[4] === null
|
|
2340
|
+
? {}
|
|
2341
|
+
: { cursor: expandCursor(input[4], dimensions?.cols ?? null, dimensions?.rows ?? null) }),
|
|
2342
|
+
...(input[5] === null ? {} : { modes: expandModes(input[5]) }),
|
|
2343
|
+
...(input[6] === null
|
|
2344
|
+
? {}
|
|
2345
|
+
: { placements: await expandPlacementsCooperatively(input[6], budget, control) }),
|
|
2346
|
+
...(input[7] === null ? {} : { bootstrap: expandBootstrap(input[7]) }),
|
|
2347
|
+
}) as unknown as TerminalReplicaPatchPayload;
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
/**
|
|
2351
|
+
* OpenTUI fallback seam. Legacy JSON still pays its full Zod decode, deep
|
|
2352
|
+
* freeze/apply, and canonical hash verification before receiving a one-shot
|
|
2353
|
+
* reducer grant; the reducer never trusts an ordinary legacy object.
|
|
2354
|
+
*/
|
|
2355
|
+
export function decodeVerifiedLegacySemanticTerminalUpdate(
|
|
2356
|
+
bytes: Uint8Array,
|
|
2357
|
+
baseline: TerminalReplicaSnapshot | null,
|
|
2358
|
+
expectedHash: string,
|
|
2359
|
+
): Readonly<{
|
|
2360
|
+
payload: TerminalSemanticDeliveryPayload;
|
|
2361
|
+
canonicalSnapshot: TerminalReplicaSnapshot | null;
|
|
2362
|
+
}> {
|
|
2363
|
+
const payload = decodeSemanticTerminalUpdate(bytes);
|
|
2364
|
+
const snapshot =
|
|
2365
|
+
payload.frame === "seed"
|
|
2366
|
+
? freezeTerminalReplicaSnapshot(payload.snapshot)
|
|
2367
|
+
: payload.frame === "patch"
|
|
2368
|
+
? baseline
|
|
2369
|
+
? applyTerminalReplicaPatch(baseline, payload.patch)
|
|
2370
|
+
: (() => {
|
|
2371
|
+
throw new TypeError("Semantic delivery baseline gap");
|
|
2372
|
+
})()
|
|
2373
|
+
: null;
|
|
2374
|
+
const hash = snapshot
|
|
2375
|
+
? hashTerminalReplicaSnapshot(snapshot)
|
|
2376
|
+
: hashTerminalReplicaTombstone(
|
|
2377
|
+
payload.frame === "tombstone" ? payload.tombstone.reason : "protocol",
|
|
2378
|
+
);
|
|
2379
|
+
if (hash !== expectedHash) throw new TypeError("Canonical state hash mismatch");
|
|
2380
|
+
grantCompactReplicaCapability(
|
|
2381
|
+
payload.frame === "seed"
|
|
2382
|
+
? snapshot!
|
|
2383
|
+
: payload.frame === "patch"
|
|
2384
|
+
? payload.patch
|
|
2385
|
+
: payload.tombstone,
|
|
2386
|
+
baseline,
|
|
2387
|
+
snapshot,
|
|
2388
|
+
hash,
|
|
2389
|
+
);
|
|
2390
|
+
return Object.freeze({ payload, canonicalSnapshot: snapshot });
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
function recordWithKeys(value: unknown, keys: readonly string[]): value is Record<string, unknown> {
|
|
2394
|
+
return (
|
|
2395
|
+
value !== null &&
|
|
2396
|
+
typeof value === "object" &&
|
|
2397
|
+
!Array.isArray(value) &&
|
|
2398
|
+
Object.keys(value).sort().join("\0") === [...keys].sort().join("\0")
|
|
2399
|
+
);
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
export function splitTerminalDeliveryChunks(
|
|
2403
|
+
transactionId: string,
|
|
2404
|
+
bytes: Uint8Array,
|
|
2405
|
+
): TerminalDeliveryChunk[] {
|
|
2406
|
+
assertRepresentationSize(bytes);
|
|
2407
|
+
const chunks: TerminalDeliveryChunk[] = [];
|
|
2408
|
+
const count = Math.max(1, Math.ceil(bytes.byteLength / TERMINAL_DELIVERY_CHUNK_BYTES));
|
|
2409
|
+
for (let index = 0; index < count; index += 1) {
|
|
2410
|
+
chunks.push({
|
|
2411
|
+
type: "terminal.delivery.chunk",
|
|
2412
|
+
transactionId,
|
|
2413
|
+
index,
|
|
2414
|
+
bytes: bytes.slice(
|
|
2415
|
+
index * TERMINAL_DELIVERY_CHUNK_BYTES,
|
|
2416
|
+
Math.min(bytes.byteLength, (index + 1) * TERMINAL_DELIVERY_CHUNK_BYTES),
|
|
2417
|
+
),
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
return chunks;
|
|
2421
|
+
}
|
|
2422
|
+
|
|
2423
|
+
class TerminalDeliveryRepresentationHasher {
|
|
2424
|
+
// Two independent 32-bit lanes avoid BigInt's per-byte allocation cost while
|
|
2425
|
+
// retaining a deterministic 64-bit wire fingerprint in browser and daemon.
|
|
2426
|
+
#high = 0x811c9dc5;
|
|
2427
|
+
#low = 0x9e3779b9;
|
|
2428
|
+
|
|
2429
|
+
write(bytes: Uint8Array): void {
|
|
2430
|
+
for (const byte of bytes) {
|
|
2431
|
+
this.#high = Math.imul(this.#high ^ byte, 0x01000193) >>> 0;
|
|
2432
|
+
this.#low = Math.imul(this.#low ^ byte, 0x85ebca6b) >>> 0;
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
|
|
2436
|
+
digest(): string {
|
|
2437
|
+
return this.#high.toString(16).padStart(8, "0") + this.#low.toString(16).padStart(8, "0");
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
export function hashTerminalDeliveryRepresentation(bytes: Uint8Array): string {
|
|
2442
|
+
const hasher = new TerminalDeliveryRepresentationHasher();
|
|
2443
|
+
hasher.write(bytes);
|
|
2444
|
+
return hasher.digest();
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
export function diffTerminalReplicaSnapshots(
|
|
2448
|
+
baseline: TerminalReplicaSnapshot,
|
|
2449
|
+
target: TerminalReplicaSnapshot,
|
|
2450
|
+
): TerminalReplicaPatchPayload {
|
|
2451
|
+
const dimensionsChanged = baseline.cols !== target.cols || baseline.rows !== target.rows;
|
|
2452
|
+
const rows = target.grid.flatMap((row, index) =>
|
|
2453
|
+
dimensionsChanged || !terminalReplicaRowsEqual(baseline.grid[index], row)
|
|
2454
|
+
? [{ index, row }]
|
|
2455
|
+
: [],
|
|
2456
|
+
);
|
|
2457
|
+
return {
|
|
2458
|
+
...(dimensionsChanged ? { dimensions: { cols: target.cols, rows: target.rows } } : {}),
|
|
2459
|
+
rows,
|
|
2460
|
+
...(historyEqual(baseline.history, target.history) ? {} : { history: target.history }),
|
|
2461
|
+
cursor: target.cursor,
|
|
2462
|
+
modes: target.modes,
|
|
2463
|
+
placements: target.placements,
|
|
2464
|
+
bootstrap: target.bootstrap,
|
|
2465
|
+
};
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
export function encodeAnsiTerminalRepresentation(
|
|
2469
|
+
baseline: TerminalReplicaSnapshot | null,
|
|
2470
|
+
target: TerminalReplicaSnapshot,
|
|
2471
|
+
): Uint8Array {
|
|
2472
|
+
let output = "";
|
|
2473
|
+
// A seed replaces an unknown renderer, which may still be sitting in either
|
|
2474
|
+
// DEC buffer. Emit the target buffer explicitly in both directions before
|
|
2475
|
+
// clearing/repainting so reconnecting while alternate is active cannot paint
|
|
2476
|
+
// alternate cells into normal (and a normal reseed cannot remain in 1049).
|
|
2477
|
+
const alternateScreenChanged =
|
|
2478
|
+
baseline === null || baseline.modes.alternateScreen !== target.modes.alternateScreen;
|
|
2479
|
+
if (alternateScreenChanged)
|
|
2480
|
+
output += target.modes.alternateScreen ? "\u001b[?1049h" : "\u001b[?1049l";
|
|
2481
|
+
if (
|
|
2482
|
+
!baseline ||
|
|
2483
|
+
baseline.cols !== target.cols ||
|
|
2484
|
+
baseline.rows !== target.rows ||
|
|
2485
|
+
alternateScreenChanged ||
|
|
2486
|
+
!historyEqual(baseline.history, target.history)
|
|
2487
|
+
) {
|
|
2488
|
+
output += "\u001b[0m\u001b[2J\u001b[3J\u001b[H";
|
|
2489
|
+
if (target.history.length > 0) output += renderAnsiSeedRows(target);
|
|
2490
|
+
else {
|
|
2491
|
+
output += prepareAnsiTopWrappedRow(target);
|
|
2492
|
+
output += renderAnsiRowChains(
|
|
2493
|
+
target,
|
|
2494
|
+
target.grid.map((_, index) => index),
|
|
2495
|
+
false,
|
|
2496
|
+
);
|
|
2497
|
+
}
|
|
2498
|
+
} else {
|
|
2499
|
+
const changedRows = new Map<number, TerminalReplicaRow>();
|
|
2500
|
+
for (let row = 0; row < target.rows; row += 1) {
|
|
2501
|
+
if (terminalReplicaRowsEqual(baseline.grid[row], target.grid[row]!)) continue;
|
|
2502
|
+
changedRows.set(row, target.grid[row]!);
|
|
2503
|
+
}
|
|
2504
|
+
if (
|
|
2505
|
+
[...changedRows].some(([index, row]) => row.wrapped || baseline.grid[index]?.wrapped === true)
|
|
2506
|
+
)
|
|
2507
|
+
return encodeAnsiTerminalRepresentation(null, target);
|
|
2508
|
+
output += renderAnsiPatchRows(target, changedRows);
|
|
2509
|
+
if (changedRows.size === 0 && baseline.modes.wraparound !== target.modes.wraparound)
|
|
2510
|
+
output += ansiWraparoundPresentation(target);
|
|
2511
|
+
}
|
|
2512
|
+
output += ansiInputModesPresentation(target, baseline);
|
|
2513
|
+
output += ansiCursorPresentation(target);
|
|
2514
|
+
const bytes = new TextEncoder().encode(output);
|
|
2515
|
+
assertRepresentationSize(bytes);
|
|
2516
|
+
return bytes;
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
/**
|
|
2520
|
+
* Reconstruct an unknown emulator from canonical scrollback plus viewport.
|
|
2521
|
+
* Writing the rows in wire order lets xterm itself create scrollback and soft
|
|
2522
|
+
* wraps; repainting only addressable viewport rows would silently discard the
|
|
2523
|
+
* selection/search identity carried by `snapshot.history`.
|
|
2524
|
+
*/
|
|
2525
|
+
function renderAnsiSeedRows(target: TerminalReplicaSnapshot): string {
|
|
2526
|
+
const rows = [...target.history, ...target.grid];
|
|
2527
|
+
let output = "\u001b[?7h";
|
|
2528
|
+
rows.forEach((row, index) => {
|
|
2529
|
+
if (index > 0 && !row.wrapped) output += "\r\n";
|
|
2530
|
+
output += renderAnsiRow(row);
|
|
2531
|
+
});
|
|
2532
|
+
output += ansiWraparoundPresentation(target);
|
|
2533
|
+
return output;
|
|
2534
|
+
}
|
|
2535
|
+
|
|
2536
|
+
/**
|
|
2537
|
+
* Encode an admitted semantic patch from its explicit dirty rows.
|
|
2538
|
+
*
|
|
2539
|
+
* The patch is the canonical change set; comparing every immutable grid row
|
|
2540
|
+
* again in the renderer turns a one-line terminal update into O(screen rows)
|
|
2541
|
+
* work. Dimension changes remain a full repaint, while ordinary deltas touch
|
|
2542
|
+
* only `patch.rows` plus the cursor.
|
|
2543
|
+
*/
|
|
2544
|
+
export function encodeAnsiTerminalPatchRepresentation(
|
|
2545
|
+
patch: TerminalReplicaPatchPayload,
|
|
2546
|
+
target: TerminalReplicaSnapshot,
|
|
2547
|
+
baseline: TerminalReplicaSnapshot,
|
|
2548
|
+
): Uint8Array {
|
|
2549
|
+
if (patch.dimensions || baseline.modes.alternateScreen !== target.modes.alternateScreen)
|
|
2550
|
+
return encodeAnsiTerminalRepresentation(baseline, target);
|
|
2551
|
+
const patchedRows = new Map(patch.rows.map(({ index, row }) => [index, row] as const));
|
|
2552
|
+
// xterm's `isWrapped` is line topology, not cell content. Clearing/repainting
|
|
2553
|
+
// a line is not a portable way to change that bit after an unrelated prior
|
|
2554
|
+
// frame (notably workload -> baseline -> rich). Whenever a dirty row touches
|
|
2555
|
+
// either side of a soft-wrap transition, rebuild the exact buffer so stale
|
|
2556
|
+
// topology cannot survive or be manufactured by a partial repaint.
|
|
2557
|
+
if (
|
|
2558
|
+
[...patchedRows].some(([index, row]) => row.wrapped || baseline.grid[index]?.wrapped === true)
|
|
2559
|
+
)
|
|
2560
|
+
return encodeAnsiTerminalRepresentation(null, target);
|
|
2561
|
+
let output = "";
|
|
2562
|
+
const historyDelta = patch.historyDelta;
|
|
2563
|
+
const appendedRows = historyDelta?.append.length ?? 0;
|
|
2564
|
+
const incrementalScroll =
|
|
2565
|
+
historyDelta &&
|
|
2566
|
+
historyDelta.trim === 0 &&
|
|
2567
|
+
appendedRows > 0 &&
|
|
2568
|
+
appendedRows <= baseline.rows &&
|
|
2569
|
+
patchedRows.size === patch.rows.length &&
|
|
2570
|
+
[...patchedRows].every(
|
|
2571
|
+
([index, row]) =>
|
|
2572
|
+
index >= 0 && index < target.rows && terminalReplicaRowsEqual(row, target.grid[index]!),
|
|
2573
|
+
) &&
|
|
2574
|
+
historyDelta.append.every((row, index) =>
|
|
2575
|
+
terminalReplicaRowsEqual(row, baseline.grid[index]!),
|
|
2576
|
+
) &&
|
|
2577
|
+
target.grid.every((row, index) => {
|
|
2578
|
+
const shiftedIndex = index + appendedRows;
|
|
2579
|
+
if (shiftedIndex < baseline.rows) {
|
|
2580
|
+
return (
|
|
2581
|
+
terminalReplicaRowsEqual(row, baseline.grid[shiftedIndex]!) || patchedRows.has(index)
|
|
2582
|
+
);
|
|
2583
|
+
}
|
|
2584
|
+
// LF creates emulator-owned blank bottom rows. Requiring an exact dirty
|
|
2585
|
+
// replacement avoids depending on an implicit blank-row representation.
|
|
2586
|
+
return patchedRows.has(index);
|
|
2587
|
+
});
|
|
2588
|
+
if (incrementalScroll) {
|
|
2589
|
+
// A line-feed at the viewport bottom asks xterm to perform the same scroll
|
|
2590
|
+
// that moved these canonical rows into history. This preserves existing
|
|
2591
|
+
// selection/viewport state and lets xterm enforce its own scrollback cap;
|
|
2592
|
+
// replaying all 5k history rows on every append would be destructive and
|
|
2593
|
+
// quadratic over a sustained terminal stream.
|
|
2594
|
+
output += `\u001b[${target.rows};1H`;
|
|
2595
|
+
output += "\n".repeat(appendedRows);
|
|
2596
|
+
} else if (
|
|
2597
|
+
(patch.history !== undefined || patch.historyDelta !== undefined) &&
|
|
2598
|
+
!historyEqual(baseline.history, target.history)
|
|
2599
|
+
) {
|
|
2600
|
+
// Full history replacement has no incremental authority. It is rare and
|
|
2601
|
+
// must remain exact rather than synthesizing an unsafe delta.
|
|
2602
|
+
return encodeAnsiTerminalRepresentation(null, target);
|
|
2603
|
+
}
|
|
2604
|
+
output += renderAnsiPatchRows(target, patchedRows);
|
|
2605
|
+
if (patchedRows.size === 0 && baseline.modes.wraparound !== target.modes.wraparound)
|
|
2606
|
+
output += ansiWraparoundPresentation(target);
|
|
2607
|
+
output += ansiInputModesPresentation(target, baseline);
|
|
2608
|
+
output += ansiCursorPresentation(target);
|
|
2609
|
+
const bytes = new TextEncoder().encode(output);
|
|
2610
|
+
assertRepresentationSize(bytes);
|
|
2611
|
+
return bytes;
|
|
2612
|
+
}
|
|
2613
|
+
|
|
2614
|
+
function renderAnsiPatchRows(
|
|
2615
|
+
target: TerminalReplicaSnapshot,
|
|
2616
|
+
patchedRows: ReadonlyMap<number, TerminalReplicaRow>,
|
|
2617
|
+
): string {
|
|
2618
|
+
const affectedRows = new Set(patchedRows.keys());
|
|
2619
|
+
for (const index of patchedRows.keys()) {
|
|
2620
|
+
let dependency = index;
|
|
2621
|
+
while (dependency > 0 && (patchedRows.get(dependency) ?? target.grid[dependency])?.wrapped) {
|
|
2622
|
+
dependency -= 1;
|
|
2623
|
+
affectedRows.add(dependency);
|
|
2624
|
+
}
|
|
2625
|
+
}
|
|
2626
|
+
return renderAnsiRowChains(
|
|
2627
|
+
target,
|
|
2628
|
+
[...affectedRows].sort((left, right) => left - right),
|
|
2629
|
+
true,
|
|
2630
|
+
patchedRows,
|
|
2631
|
+
);
|
|
2632
|
+
}
|
|
2633
|
+
|
|
2634
|
+
function ansiCursorPresentation(target: TerminalReplicaSnapshot): string {
|
|
2635
|
+
const cursorShape =
|
|
2636
|
+
target.cursor.style === "underline"
|
|
2637
|
+
? target.cursor.blink
|
|
2638
|
+
? 3
|
|
2639
|
+
: 4
|
|
2640
|
+
: target.cursor.style === "bar"
|
|
2641
|
+
? target.cursor.blink
|
|
2642
|
+
? 5
|
|
2643
|
+
: 6
|
|
2644
|
+
: target.cursor.blink
|
|
2645
|
+
? 1
|
|
2646
|
+
: 2;
|
|
2647
|
+
return (
|
|
2648
|
+
`\u001b[${target.cursor.y + 1};${target.cursor.x + 1}H` +
|
|
2649
|
+
`\u001b[${cursorShape} q` +
|
|
2650
|
+
(target.cursor.hidden ? "\u001b[?25l" : "\u001b[?25h")
|
|
2651
|
+
);
|
|
2652
|
+
}
|
|
2653
|
+
|
|
2654
|
+
function ansiWraparoundPresentation(target: TerminalReplicaSnapshot): string {
|
|
2655
|
+
return target.modes.wraparound ? "\u001b[?7h" : "\u001b[?7l";
|
|
2656
|
+
}
|
|
2657
|
+
|
|
2658
|
+
/**
|
|
2659
|
+
* Restore the emulator modes that affect subsequent input and parsing. These
|
|
2660
|
+
* are deliberately emitted after painting/cursor placement: origin, insert,
|
|
2661
|
+
* and synchronized-output modes must describe the resulting terminal without
|
|
2662
|
+
* changing how the reconstruction itself is interpreted.
|
|
2663
|
+
*/
|
|
2664
|
+
function ansiInputModesPresentation(
|
|
2665
|
+
target: TerminalReplicaSnapshot,
|
|
2666
|
+
baseline: TerminalReplicaSnapshot | null,
|
|
2667
|
+
): string {
|
|
2668
|
+
const prior = baseline?.modes;
|
|
2669
|
+
const modes = target.modes;
|
|
2670
|
+
let output = "";
|
|
2671
|
+
const decMode = (code: number, enabled: boolean): string =>
|
|
2672
|
+
`\u001b[?${code}${enabled ? "h" : "l"}`;
|
|
2673
|
+
const ansiMode = (code: number, enabled: boolean): string =>
|
|
2674
|
+
`\u001b[${code}${enabled ? "h" : "l"}`;
|
|
2675
|
+
if (!prior || prior.applicationCursor !== modes.applicationCursor)
|
|
2676
|
+
output += decMode(1, modes.applicationCursor);
|
|
2677
|
+
if (!prior || prior.applicationKeypad !== modes.applicationKeypad)
|
|
2678
|
+
output += modes.applicationKeypad ? "\u001b=" : "\u001b>";
|
|
2679
|
+
if (!prior || prior.bracketedPaste !== modes.bracketedPaste)
|
|
2680
|
+
output += decMode(2004, modes.bracketedPaste);
|
|
2681
|
+
if (!prior || prior.insert !== modes.insert) output += ansiMode(4, modes.insert);
|
|
2682
|
+
if (!prior || prior.origin !== modes.origin) output += decMode(6, modes.origin);
|
|
2683
|
+
const mouseChanged =
|
|
2684
|
+
!prior ||
|
|
2685
|
+
prior.mouseTracking !== modes.mouseTracking ||
|
|
2686
|
+
prior.mouseProtocol !== modes.mouseProtocol ||
|
|
2687
|
+
prior.mouseEncoding !== modes.mouseEncoding;
|
|
2688
|
+
if (mouseChanged) {
|
|
2689
|
+
output += decMode(9, false);
|
|
2690
|
+
output += decMode(1000, false);
|
|
2691
|
+
output += decMode(1002, false);
|
|
2692
|
+
output += decMode(1003, false);
|
|
2693
|
+
output += decMode(1005, false);
|
|
2694
|
+
output += decMode(1006, false);
|
|
2695
|
+
output += decMode(1016, false);
|
|
2696
|
+
if (modes.mouseTracking) {
|
|
2697
|
+
const protocol = modes.mouseProtocol ?? "vt200";
|
|
2698
|
+
if (protocol === "x10") output += decMode(9, true);
|
|
2699
|
+
else if (protocol === "drag") output += decMode(1002, true);
|
|
2700
|
+
else if (protocol === "any") output += decMode(1003, true);
|
|
2701
|
+
else if (protocol !== "none") output += decMode(1000, true);
|
|
2702
|
+
}
|
|
2703
|
+
if (modes.mouseEncoding === "utf8") output += decMode(1005, true);
|
|
2704
|
+
else if (modes.mouseEncoding === "sgr") output += decMode(1006, true);
|
|
2705
|
+
else if (modes.mouseEncoding === "sgr-pixels") output += decMode(1016, true);
|
|
2706
|
+
}
|
|
2707
|
+
if (!prior || prior.synchronizedOutput !== modes.synchronizedOutput)
|
|
2708
|
+
output += decMode(2026, modes.synchronizedOutput);
|
|
2709
|
+
return output;
|
|
2710
|
+
}
|
|
2711
|
+
|
|
2712
|
+
export interface TerminalDeliveryClientState {
|
|
2713
|
+
readonly negotiated: TerminalDeliveryNegotiated;
|
|
2714
|
+
readonly workspaceName: string;
|
|
2715
|
+
readonly semanticPaneId: string;
|
|
2716
|
+
readonly incarnation: string | null;
|
|
2717
|
+
readonly appliedRevision: number;
|
|
2718
|
+
readonly appliedHash: string | null;
|
|
2719
|
+
readonly canonicalSnapshot: TerminalReplicaSnapshot | null;
|
|
2720
|
+
readonly inFlight: TerminalDeliveryEnvelope | null;
|
|
2721
|
+
readonly nextChunk: number;
|
|
2722
|
+
readonly receivedBytes: number;
|
|
2723
|
+
readonly reseedRequired: boolean;
|
|
2724
|
+
readonly failed: boolean;
|
|
2725
|
+
readonly tombstoned: boolean;
|
|
2726
|
+
}
|
|
2727
|
+
|
|
2728
|
+
export function createTerminalDeliveryClientState(
|
|
2729
|
+
negotiated: TerminalDeliveryNegotiated,
|
|
2730
|
+
workspaceName: string,
|
|
2731
|
+
semanticPaneId: string,
|
|
2732
|
+
): TerminalDeliveryClientState {
|
|
2733
|
+
return Object.freeze({
|
|
2734
|
+
negotiated: Object.freeze(TerminalDeliveryNegotiatedSchemaZ.parse(negotiated)),
|
|
2735
|
+
workspaceName,
|
|
2736
|
+
semanticPaneId,
|
|
2737
|
+
incarnation: null,
|
|
2738
|
+
appliedRevision: -1,
|
|
2739
|
+
appliedHash: null,
|
|
2740
|
+
canonicalSnapshot: null,
|
|
2741
|
+
inFlight: null,
|
|
2742
|
+
nextChunk: 0,
|
|
2743
|
+
receivedBytes: 0,
|
|
2744
|
+
reseedRequired: false,
|
|
2745
|
+
failed: false,
|
|
2746
|
+
tombstoned: false,
|
|
2747
|
+
});
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
export function admitTerminalDeliveryEnvelope(
|
|
2751
|
+
state: TerminalDeliveryClientState,
|
|
2752
|
+
envelope: TerminalDeliveryEnvelope,
|
|
2753
|
+
): TerminalDeliveryClientState {
|
|
2754
|
+
envelope = Object.freeze(TerminalDeliveryEnvelopeSchemaZ.parse(envelope));
|
|
2755
|
+
if (
|
|
2756
|
+
state.failed ||
|
|
2757
|
+
state.inFlight ||
|
|
2758
|
+
!terminalDeliveryEncodingAccepted(state.negotiated, envelope.encoding) ||
|
|
2759
|
+
envelope.generation !== state.negotiated.generation ||
|
|
2760
|
+
envelope.deliveryNonce !== state.negotiated.deliveryNonce ||
|
|
2761
|
+
envelope.protocolVersion !== state.negotiated.protocolVersion ||
|
|
2762
|
+
envelope.richPlacements !== state.negotiated.richPlacements ||
|
|
2763
|
+
state.workspaceName !== envelope.workspaceName ||
|
|
2764
|
+
state.semanticPaneId !== envelope.semanticPaneId
|
|
2765
|
+
)
|
|
2766
|
+
return failClient(state);
|
|
2767
|
+
if (envelope.frame === "seed") {
|
|
2768
|
+
if (state.tombstoned && state.incarnation === envelope.incarnation) return failClient(state);
|
|
2769
|
+
if (
|
|
2770
|
+
state.incarnation === envelope.incarnation &&
|
|
2771
|
+
state.appliedRevision >= 0 &&
|
|
2772
|
+
envelope.canonicalRevision <= state.appliedRevision
|
|
2773
|
+
)
|
|
2774
|
+
return failClient(state);
|
|
2775
|
+
if (
|
|
2776
|
+
state.incarnation !== null &&
|
|
2777
|
+
state.incarnation !== envelope.incarnation &&
|
|
2778
|
+
(!isNewerIncarnation(state.incarnation, envelope.incarnation) ||
|
|
2779
|
+
envelope.canonicalRevision <= state.appliedRevision)
|
|
2780
|
+
)
|
|
2781
|
+
return failClient(state);
|
|
2782
|
+
} else if (
|
|
2783
|
+
state.incarnation !== envelope.incarnation ||
|
|
2784
|
+
envelope.baseRevision !== state.appliedRevision ||
|
|
2785
|
+
state.reseedRequired
|
|
2786
|
+
) {
|
|
2787
|
+
return failClient(state);
|
|
2788
|
+
}
|
|
2789
|
+
if (envelope.frame !== "seed" && state.appliedRevision < 0) return failClient(state);
|
|
2790
|
+
return Object.freeze({ ...state, inFlight: envelope, nextChunk: 0, receivedBytes: 0 });
|
|
2791
|
+
}
|
|
2792
|
+
|
|
2793
|
+
export function terminalDeliveryEncodingAccepted(
|
|
2794
|
+
negotiated: TerminalDeliveryNegotiated,
|
|
2795
|
+
encoding: TerminalDeliveryEnvelope["encoding"],
|
|
2796
|
+
): boolean {
|
|
2797
|
+
return (
|
|
2798
|
+
encoding === negotiated.encoding ||
|
|
2799
|
+
(negotiated.encoding === "semantic-compact-v1" &&
|
|
2800
|
+
negotiated.fallbackEncoding === "semantic-v1" &&
|
|
2801
|
+
encoding === "semantic-v1")
|
|
2802
|
+
);
|
|
2803
|
+
}
|
|
2804
|
+
|
|
2805
|
+
export function admitTerminalDeliveryChunk(
|
|
2806
|
+
state: TerminalDeliveryClientState,
|
|
2807
|
+
chunk: TerminalDeliveryChunk,
|
|
2808
|
+
): TerminalDeliveryClientState {
|
|
2809
|
+
chunk = TerminalDeliveryChunkSchemaZ.parse(chunk);
|
|
2810
|
+
const envelope = state.inFlight;
|
|
2811
|
+
const expectedBytes = envelope
|
|
2812
|
+
? Math.min(
|
|
2813
|
+
TERMINAL_DELIVERY_CHUNK_BYTES,
|
|
2814
|
+
Math.max(0, envelope.representationBytes - chunk.index * TERMINAL_DELIVERY_CHUNK_BYTES),
|
|
2815
|
+
)
|
|
2816
|
+
: -1;
|
|
2817
|
+
if (
|
|
2818
|
+
state.failed ||
|
|
2819
|
+
!state.inFlight ||
|
|
2820
|
+
chunk.transactionId !== state.inFlight.transactionId ||
|
|
2821
|
+
chunk.index !== state.nextChunk ||
|
|
2822
|
+
chunk.index >= state.inFlight.chunkCount ||
|
|
2823
|
+
chunk.bytes.byteLength !== expectedBytes ||
|
|
2824
|
+
state.receivedBytes + chunk.bytes.byteLength > state.inFlight.representationBytes
|
|
2825
|
+
)
|
|
2826
|
+
return failClient(state);
|
|
2827
|
+
return Object.freeze({
|
|
2828
|
+
...state,
|
|
2829
|
+
nextChunk: state.nextChunk + 1,
|
|
2830
|
+
receivedBytes: state.receivedBytes + chunk.bytes.byteLength,
|
|
2831
|
+
});
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
export class TerminalDeliveryAssembler {
|
|
2835
|
+
readonly #storage: Uint8Array;
|
|
2836
|
+
readonly #buffer: Uint8Array;
|
|
2837
|
+
readonly #representationHasher = new TerminalDeliveryRepresentationHasher();
|
|
2838
|
+
readonly envelope: TerminalDeliveryEnvelope;
|
|
2839
|
+
#nextChunk = 0;
|
|
2840
|
+
#offset = 0;
|
|
2841
|
+
constructor(envelope: TerminalDeliveryEnvelope, reusableStorage?: Uint8Array) {
|
|
2842
|
+
this.envelope = Object.freeze(TerminalDeliveryEnvelopeSchemaZ.parse(envelope));
|
|
2843
|
+
this.#storage =
|
|
2844
|
+
reusableStorage && reusableStorage.byteLength >= this.envelope.representationBytes
|
|
2845
|
+
? reusableStorage
|
|
2846
|
+
: new Uint8Array(this.envelope.representationBytes);
|
|
2847
|
+
this.#buffer = this.#storage.subarray(0, this.envelope.representationBytes);
|
|
2848
|
+
}
|
|
2849
|
+
write(chunk: TerminalDeliveryChunk): void {
|
|
2850
|
+
chunk = TerminalDeliveryChunkSchemaZ.parse(chunk);
|
|
2851
|
+
const expected = Math.min(
|
|
2852
|
+
TERMINAL_DELIVERY_CHUNK_BYTES,
|
|
2853
|
+
this.envelope.representationBytes - this.#offset,
|
|
2854
|
+
);
|
|
2855
|
+
if (
|
|
2856
|
+
chunk.transactionId !== this.envelope.transactionId ||
|
|
2857
|
+
chunk.index !== this.#nextChunk ||
|
|
2858
|
+
chunk.bytes.byteLength !== expected
|
|
2859
|
+
)
|
|
2860
|
+
throw new TypeError("Invalid terminal delivery chunk");
|
|
2861
|
+
this.#buffer.set(chunk.bytes, this.#offset);
|
|
2862
|
+
this.#representationHasher.write(chunk.bytes);
|
|
2863
|
+
this.#offset += chunk.bytes.byteLength;
|
|
2864
|
+
this.#nextChunk += 1;
|
|
2865
|
+
}
|
|
2866
|
+
complete(): Uint8Array {
|
|
2867
|
+
if (
|
|
2868
|
+
this.#offset !== this.envelope.representationBytes ||
|
|
2869
|
+
this.#nextChunk !== this.envelope.chunkCount
|
|
2870
|
+
)
|
|
2871
|
+
throw new TypeError("Terminal delivery transaction is incomplete");
|
|
2872
|
+
if (this.#representationHasher.digest() !== this.envelope.representationHash)
|
|
2873
|
+
throw new TypeError("Terminal delivery representation hash mismatch");
|
|
2874
|
+
return this.#buffer;
|
|
2875
|
+
}
|
|
2876
|
+
|
|
2877
|
+
releaseStorage(): Uint8Array {
|
|
2878
|
+
if (
|
|
2879
|
+
this.#offset !== this.envelope.representationBytes ||
|
|
2880
|
+
this.#nextChunk !== this.envelope.chunkCount
|
|
2881
|
+
)
|
|
2882
|
+
throw new TypeError("Terminal delivery transaction is incomplete");
|
|
2883
|
+
return this.#storage;
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
|
|
2887
|
+
export interface StagedTerminalDelivery {
|
|
2888
|
+
readonly envelope: TerminalDeliveryEnvelope;
|
|
2889
|
+
readonly bytes: Uint8Array;
|
|
2890
|
+
}
|
|
2891
|
+
|
|
2892
|
+
export function completeTerminalDelivery(
|
|
2893
|
+
state: TerminalDeliveryClientState,
|
|
2894
|
+
assembler: TerminalDeliveryAssembler,
|
|
2895
|
+
): StagedTerminalDelivery {
|
|
2896
|
+
const envelope = state.inFlight;
|
|
2897
|
+
if (
|
|
2898
|
+
!envelope ||
|
|
2899
|
+
state.failed ||
|
|
2900
|
+
!sameEnvelope(assembler.envelope, envelope) ||
|
|
2901
|
+
state.nextChunk !== envelope.chunkCount ||
|
|
2902
|
+
state.receivedBytes !== envelope.representationBytes
|
|
2903
|
+
)
|
|
2904
|
+
throw new TypeError("Terminal delivery transaction is incomplete");
|
|
2905
|
+
return { envelope, bytes: assembler.complete() };
|
|
2906
|
+
}
|
|
2907
|
+
|
|
2908
|
+
export function commitTerminalDelivery(
|
|
2909
|
+
state: TerminalDeliveryClientState,
|
|
2910
|
+
staged: StagedTerminalDelivery,
|
|
2911
|
+
options: { readonly presentationApplied?: boolean } = {},
|
|
2912
|
+
): {
|
|
2913
|
+
readonly state: TerminalDeliveryClientState;
|
|
2914
|
+
readonly ack: TerminalDeliveryAck;
|
|
2915
|
+
/** The one authenticated semantic decode used to produce `state`. */
|
|
2916
|
+
readonly semanticUpdate: TerminalSemanticDeliveryPayload | null;
|
|
2917
|
+
} {
|
|
2918
|
+
const envelope = state.inFlight;
|
|
2919
|
+
if (!envelope || !sameEnvelope(envelope, staged.envelope))
|
|
2920
|
+
throw new TypeError("Delivery was not staged from this state");
|
|
2921
|
+
if (hashTerminalDeliveryRepresentation(staged.bytes) !== envelope.representationHash)
|
|
2922
|
+
throw new TypeError("Terminal delivery representation hash mismatch");
|
|
2923
|
+
let canonicalSnapshot = state.canonicalSnapshot;
|
|
2924
|
+
let semanticUpdate: TerminalSemanticDeliveryPayload | null = null;
|
|
2925
|
+
if (envelope.encoding === "semantic-v1" || envelope.encoding === "semantic-compact-v1") {
|
|
2926
|
+
const payload =
|
|
2927
|
+
envelope.encoding === "semantic-compact-v1"
|
|
2928
|
+
? decodeVerifiedCompactSemanticTerminalUpdate(
|
|
2929
|
+
staged.bytes,
|
|
2930
|
+
canonicalSnapshot,
|
|
2931
|
+
envelope.canonicalStateHash,
|
|
2932
|
+
).payload
|
|
2933
|
+
: decodeSemanticTerminalUpdate(staged.bytes);
|
|
2934
|
+
semanticUpdate = payload;
|
|
2935
|
+
if (payload.frame !== envelope.frame || payload.revision !== envelope.canonicalRevision)
|
|
2936
|
+
throw new TypeError("Semantic frame or revision mismatch");
|
|
2937
|
+
if (
|
|
2938
|
+
payload.frame !== "seed" &&
|
|
2939
|
+
(payload.baseRevision !== envelope.baseRevision ||
|
|
2940
|
+
payload.baseRevision !== state.appliedRevision)
|
|
2941
|
+
)
|
|
2942
|
+
throw new TypeError("Semantic delivery baseline gap");
|
|
2943
|
+
const compactCapability = compactCommitCapabilities.get(payload);
|
|
2944
|
+
if (envelope.encoding === "semantic-compact-v1") {
|
|
2945
|
+
if (!compactCapability || compactCapability.hash !== envelope.canonicalStateHash)
|
|
2946
|
+
throw new TypeError("Compact semantic capability was unavailable");
|
|
2947
|
+
canonicalSnapshot = compactCapability.snapshot;
|
|
2948
|
+
compactCommitCapabilities.delete(payload);
|
|
2949
|
+
} else {
|
|
2950
|
+
if (payload.frame === "seed")
|
|
2951
|
+
canonicalSnapshot = freezeTerminalReplicaSnapshot(payload.snapshot);
|
|
2952
|
+
else if (payload.frame === "patch") {
|
|
2953
|
+
if (!canonicalSnapshot) throw new TypeError("Semantic delivery baseline gap");
|
|
2954
|
+
canonicalSnapshot = applyTerminalReplicaPatch(canonicalSnapshot, payload.patch);
|
|
2955
|
+
} else canonicalSnapshot = null;
|
|
2956
|
+
const hash = canonicalSnapshot
|
|
2957
|
+
? hashTerminalReplicaSnapshot(canonicalSnapshot)
|
|
2958
|
+
: hashTerminalReplicaTombstone(
|
|
2959
|
+
payload.frame === "tombstone" ? payload.tombstone.reason : "protocol",
|
|
2960
|
+
);
|
|
2961
|
+
if (hash !== envelope.canonicalStateHash)
|
|
2962
|
+
throw new TypeError("Canonical state hash mismatch");
|
|
2963
|
+
}
|
|
2964
|
+
} else if (!options.presentationApplied) {
|
|
2965
|
+
throw new TypeError("ANSI presentation must be applied before ACK");
|
|
2966
|
+
}
|
|
2967
|
+
const next = Object.freeze({
|
|
2968
|
+
...state,
|
|
2969
|
+
workspaceName: envelope.workspaceName,
|
|
2970
|
+
semanticPaneId: envelope.semanticPaneId,
|
|
2971
|
+
incarnation: envelope.incarnation,
|
|
2972
|
+
appliedRevision: envelope.canonicalRevision,
|
|
2973
|
+
appliedHash: envelope.canonicalStateHash,
|
|
2974
|
+
canonicalSnapshot,
|
|
2975
|
+
tombstoned: envelope.frame === "tombstone",
|
|
2976
|
+
inFlight: null,
|
|
2977
|
+
nextChunk: 0,
|
|
2978
|
+
receivedBytes: 0,
|
|
2979
|
+
reseedRequired: false,
|
|
2980
|
+
});
|
|
2981
|
+
return {
|
|
2982
|
+
state: next,
|
|
2983
|
+
semanticUpdate,
|
|
2984
|
+
ack: {
|
|
2985
|
+
type: "terminal.delivery.ack",
|
|
2986
|
+
workspaceName: envelope.workspaceName,
|
|
2987
|
+
semanticPaneId: envelope.semanticPaneId,
|
|
2988
|
+
generation: envelope.generation,
|
|
2989
|
+
incarnation: envelope.incarnation,
|
|
2990
|
+
deliveryNonce: envelope.deliveryNonce,
|
|
2991
|
+
transactionId: envelope.transactionId,
|
|
2992
|
+
canonicalRevision: envelope.canonicalRevision,
|
|
2993
|
+
canonicalStateHash: envelope.canonicalStateHash,
|
|
2994
|
+
representationHash: envelope.representationHash,
|
|
2995
|
+
},
|
|
2996
|
+
};
|
|
2997
|
+
}
|
|
2998
|
+
|
|
2999
|
+
export function nackTerminalDelivery(
|
|
3000
|
+
state: TerminalDeliveryClientState,
|
|
3001
|
+
nack: TerminalDeliveryNack,
|
|
3002
|
+
): TerminalDeliveryClientState {
|
|
3003
|
+
nack = TerminalDeliveryNackSchemaZ.parse(nack);
|
|
3004
|
+
if (
|
|
3005
|
+
nack.generation !== state.negotiated.generation ||
|
|
3006
|
+
nack.deliveryNonce !== state.negotiated.deliveryNonce ||
|
|
3007
|
+
nack.workspaceName !== state.workspaceName ||
|
|
3008
|
+
nack.semanticPaneId !== state.semanticPaneId
|
|
3009
|
+
)
|
|
3010
|
+
return failClient(state);
|
|
3011
|
+
if (state.inFlight && nack.transactionId !== state.inFlight.transactionId)
|
|
3012
|
+
return failClient(state);
|
|
3013
|
+
return Object.freeze({
|
|
3014
|
+
...state,
|
|
3015
|
+
inFlight: null,
|
|
3016
|
+
nextChunk: 0,
|
|
3017
|
+
receivedBytes: 0,
|
|
3018
|
+
reseedRequired: true,
|
|
3019
|
+
failed: false,
|
|
3020
|
+
});
|
|
3021
|
+
}
|
|
3022
|
+
|
|
3023
|
+
function failClient(state: TerminalDeliveryClientState): TerminalDeliveryClientState {
|
|
3024
|
+
return Object.freeze({ ...state, failed: true, inFlight: null, receivedBytes: 0 });
|
|
3025
|
+
}
|
|
3026
|
+
|
|
3027
|
+
function sameEnvelope(left: TerminalDeliveryEnvelope, right: TerminalDeliveryEnvelope): boolean {
|
|
3028
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
3029
|
+
}
|
|
3030
|
+
|
|
3031
|
+
function historyEqual(
|
|
3032
|
+
left: readonly TerminalReplicaRow[],
|
|
3033
|
+
right: readonly TerminalReplicaRow[],
|
|
3034
|
+
): boolean {
|
|
3035
|
+
return (
|
|
3036
|
+
left === right ||
|
|
3037
|
+
(left.length === right.length &&
|
|
3038
|
+
left.every((row, index) => terminalReplicaRowsEqual(row, right[index]!)))
|
|
3039
|
+
);
|
|
3040
|
+
}
|
|
3041
|
+
|
|
3042
|
+
function renderAnsiRow(row: TerminalReplicaRow): string {
|
|
3043
|
+
let output = "\u001b[0m";
|
|
3044
|
+
for (const cell of row.cells) {
|
|
3045
|
+
if (cell.width === 0) continue;
|
|
3046
|
+
output += ansiCellStyle(cell);
|
|
3047
|
+
output += cell.grapheme || " ";
|
|
3048
|
+
}
|
|
3049
|
+
return `${output}\u001b[0m`;
|
|
3050
|
+
}
|
|
3051
|
+
|
|
3052
|
+
/**
|
|
3053
|
+
* Repaint rows while preserving the emulator's soft-wrap topology. A wrapped
|
|
3054
|
+
* row can only be created by letting a printable cell cross the preceding
|
|
3055
|
+
* row's right margin; CUP/CRLF reconstruction loses that bit even when every
|
|
3056
|
+
* cell is otherwise exact. Patch callers therefore include the unchanged
|
|
3057
|
+
* predecessor of a dirty wrapped row and repaint the whole wrapped chain.
|
|
3058
|
+
*/
|
|
3059
|
+
function renderAnsiRowChains(
|
|
3060
|
+
target: TerminalReplicaSnapshot,
|
|
3061
|
+
indices: readonly number[],
|
|
3062
|
+
clearRows: boolean,
|
|
3063
|
+
patchedRows: ReadonlyMap<number, TerminalReplicaRow> = new Map(),
|
|
3064
|
+
): string {
|
|
3065
|
+
if (indices.length === 0) return "";
|
|
3066
|
+
const rowAt = (index: number): TerminalReplicaRow =>
|
|
3067
|
+
patchedRows.get(index) ?? target.grid[index]!;
|
|
3068
|
+
let output = "\u001b[?7h";
|
|
3069
|
+
if (clearRows) {
|
|
3070
|
+
for (const index of indices) output += `\u001b[${index + 1};1H\u001b[2K`;
|
|
3071
|
+
}
|
|
3072
|
+
let previous = -2;
|
|
3073
|
+
for (const index of indices) {
|
|
3074
|
+
const row = rowAt(index);
|
|
3075
|
+
const continuesPrevious = index === previous + 1 && row.wrapped;
|
|
3076
|
+
if (!continuesPrevious) output += `\u001b[${index + 1};1H`;
|
|
3077
|
+
output += renderAnsiRow(row);
|
|
3078
|
+
previous = index;
|
|
3079
|
+
}
|
|
3080
|
+
output += ansiWraparoundPresentation(target);
|
|
3081
|
+
return output;
|
|
3082
|
+
}
|
|
3083
|
+
|
|
3084
|
+
/**
|
|
3085
|
+
* CUP cannot manufacture a wrapped first viewport row because its predecessor
|
|
3086
|
+
* lives above the addressable viewport. Build that bit once through a bounded
|
|
3087
|
+
* bottom-row wrap, move the line to the top, and discard the synthetic
|
|
3088
|
+
* scrollback before the ordinary exact repaint. Patch callers fall back to
|
|
3089
|
+
* this seed whenever row zero itself must be repainted as wrapped.
|
|
3090
|
+
*/
|
|
3091
|
+
function prepareAnsiTopWrappedRow(target: TerminalReplicaSnapshot): string {
|
|
3092
|
+
const first = target.grid[0];
|
|
3093
|
+
if (!first?.wrapped) return "";
|
|
3094
|
+
let output = `\u001b[?7h\u001b[${target.rows};1H\u001b[2K`;
|
|
3095
|
+
output += renderAnsiRow(first);
|
|
3096
|
+
output += renderAnsiRow(first);
|
|
3097
|
+
if (target.rows > 1) output += `\u001b[${target.rows - 1}S`;
|
|
3098
|
+
return `${output}\u001b[3J`;
|
|
3099
|
+
}
|
|
3100
|
+
|
|
3101
|
+
function ansiCellStyle(cell: TerminalReplicaRow["cells"][number]): string {
|
|
3102
|
+
const codes: number[] = [0];
|
|
3103
|
+
if (cell.attributes & 1) codes.push(1);
|
|
3104
|
+
if (cell.attributes & 2) codes.push(2);
|
|
3105
|
+
if (cell.attributes & 4) codes.push(3);
|
|
3106
|
+
if (cell.attributes & 8) codes.push(4);
|
|
3107
|
+
if (cell.attributes & 32) codes.push(7);
|
|
3108
|
+
if (cell.attributes & 128) codes.push(9);
|
|
3109
|
+
if (cell.foreground.kind === "indexed") codes.push(38, 5, cell.foreground.index);
|
|
3110
|
+
if (cell.foreground.kind === "rgb")
|
|
3111
|
+
codes.push(
|
|
3112
|
+
38,
|
|
3113
|
+
2,
|
|
3114
|
+
(cell.foreground.value >> 16) & 255,
|
|
3115
|
+
(cell.foreground.value >> 8) & 255,
|
|
3116
|
+
cell.foreground.value & 255,
|
|
3117
|
+
);
|
|
3118
|
+
if (cell.background.kind === "indexed") codes.push(48, 5, cell.background.index);
|
|
3119
|
+
if (cell.background.kind === "rgb")
|
|
3120
|
+
codes.push(
|
|
3121
|
+
48,
|
|
3122
|
+
2,
|
|
3123
|
+
(cell.background.value >> 16) & 255,
|
|
3124
|
+
(cell.background.value >> 8) & 255,
|
|
3125
|
+
cell.background.value & 255,
|
|
3126
|
+
);
|
|
3127
|
+
return `\u001b[${codes.join(";")}m`;
|
|
3128
|
+
}
|
|
3129
|
+
|
|
3130
|
+
function canonicalJson(value: unknown): string {
|
|
3131
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
3132
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
3133
|
+
const record = value as Record<string, unknown>;
|
|
3134
|
+
return `{${Object.keys(record)
|
|
3135
|
+
.sort()
|
|
3136
|
+
.filter((key) => record[key] !== undefined)
|
|
3137
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`)
|
|
3138
|
+
.join(",")}}`;
|
|
3139
|
+
}
|
|
3140
|
+
|
|
3141
|
+
function assertRepresentationSize(bytes: Uint8Array): void {
|
|
3142
|
+
if (bytes.byteLength > TERMINAL_DELIVERY_MAX_REPRESENTATION_BYTES)
|
|
3143
|
+
throw new TerminalDeliveryStateTooLargeError(bytes.byteLength);
|
|
3144
|
+
}
|
|
3145
|
+
|
|
3146
|
+
function isNewerIncarnation(current: string, candidate: string): boolean {
|
|
3147
|
+
const currentEpoch = /:([0-9]+)$/u.exec(current)?.[1];
|
|
3148
|
+
const candidateEpoch = /:([0-9]+)$/u.exec(candidate)?.[1];
|
|
3149
|
+
return (
|
|
3150
|
+
currentEpoch !== undefined &&
|
|
3151
|
+
candidateEpoch !== undefined &&
|
|
3152
|
+
Number(candidateEpoch) > Number(currentEpoch)
|
|
3153
|
+
);
|
|
3154
|
+
}
|