@johnhenry/mallory-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -2
- package/bin/mallory.mjs +51 -0
- package/package.json +51 -4
- package/src/a2a/client.js +511 -0
- package/src/a2a/demo-agent.js +396 -0
- package/src/a2a/hub.js +578 -0
- package/src/a2a/settings.js +88 -0
- package/src/a2a/task.js +358 -0
- package/src/acp/client.js +573 -0
- package/src/acp/demo-agent.js +332 -0
- package/src/acp/hub.js +632 -0
- package/src/acp/settings.js +78 -0
- package/src/actions/map.js +524 -0
- package/src/agent/cloud.js +77 -0
- package/src/agent/compact.js +84 -0
- package/src/agent/constants.js +39 -0
- package/src/agent/diff.js +101 -0
- package/src/agent/fork.js +159 -0
- package/src/agent/grounding.js +73 -0
- package/src/agent/guards.js +129 -0
- package/src/agent/local-transport.js +152 -0
- package/src/agent/loop.js +479 -0
- package/src/agent/park-store.js +77 -0
- package/src/agent/park.js +76 -0
- package/src/agent/ports.js +28 -0
- package/src/agent/providers.js +591 -0
- package/src/agent/restart.js +51 -0
- package/src/agent/router.js +380 -0
- package/src/agent/run.js +861 -0
- package/src/agent/tools.js +247 -0
- package/src/agent-library/engine.js +43 -0
- package/src/ai/bridge.js +155 -0
- package/src/ai/policy.js +131 -0
- package/src/aimatey/vendor.js +18 -0
- package/src/audio/container-duration.js +35 -0
- package/src/audio/instruments.js +776 -0
- package/src/audio/lanes.js +230 -0
- package/src/audio/patch.js +1410 -0
- package/src/audio/plan.js +276 -0
- package/src/audio/rack.js +1006 -0
- package/src/audio/sample-trim.js +293 -0
- package/src/audio/soundfont.js +243 -0
- package/src/audio/take-store.js +87 -0
- package/src/audio/takes.js +129 -0
- package/src/audio/wav.js +170 -0
- package/src/audio/zzfx.js +333 -0
- package/src/autonomy/bundle.js +207 -0
- package/src/autonomy/confine.js +61 -0
- package/src/autonomy/enforce.js +155 -0
- package/src/autonomy/file-kv.js +88 -0
- package/src/autonomy/ledger.js +203 -0
- package/src/autonomy/pins.js +50 -0
- package/src/autonomy/psl/index.js +32 -0
- package/src/autonomy/psl/suffixes.json +1 -0
- package/src/autonomy/record.js +28 -0
- package/src/autonomy/routines.js +107 -0
- package/src/autonomy/server.js +175 -0
- package/src/autonomy/signer-node.js +167 -0
- package/src/autonomy/store.js +43 -0
- package/src/autonomy/unattended.js +13 -0
- package/src/bytes/bytes.js +378 -0
- package/src/calc/syntax.js +125 -0
- package/src/calc/tex.js +28 -0
- package/src/calendar/vendor.js +2 -0
- package/src/clipboard.js +130 -0
- package/src/clsh/clsh.js +3417 -0
- package/src/codec/capnp.js +1804 -0
- package/src/codec/core.js +2043 -0
- package/src/codec/vendor/avro.js +136 -0
- package/src/codec/vendor/cbor.js +25 -0
- package/src/codec/vendor/flatbuffers.js +206 -0
- package/src/codec/vendor/msgpack.js +9 -0
- package/src/codec/vendor/protobuf.js +53 -0
- package/src/delegation/caps.js +60 -0
- package/src/delegation/config.js +32 -0
- package/src/device/model.js +246 -0
- package/src/device/recording.js +198 -0
- package/src/device/stream-model.js +151 -0
- package/src/documents/documents.js +1224 -0
- package/src/effects/catalog.js +63 -0
- package/src/effects/extras.js +133 -0
- package/src/effects/parse.js +339 -0
- package/src/embed/protocol.js +131 -0
- package/src/env/env.js +159 -0
- package/src/exact/vendor.js +45 -0
- package/src/example-meta.js +164 -0
- package/src/examples.js +94 -0
- package/src/export/canvas-image.js +217 -0
- package/src/export/expr-latex-lazy.js +8 -0
- package/src/export/formats.js +322 -0
- package/src/export/latex.js +183 -0
- package/src/export/pane.js +184 -0
- package/src/export/value.js +452 -0
- package/src/export/zip.js +214 -0
- package/src/ext/access.js +58 -0
- package/src/ext/btasks.js +225 -0
- package/src/ext/client.js +209 -0
- package/src/ext/tools.js +69 -0
- package/src/ext/view.js +105 -0
- package/src/ext/webmcp-tabs.js +299 -0
- package/src/files/archive.js +232 -0
- package/src/files/files.js +317 -0
- package/src/formats/json.js +24 -0
- package/src/formats/jupyter.js +188 -0
- package/src/formats/markdown.js +254 -0
- package/src/formats/n8n.js +276 -0
- package/src/formats/node-red.js +363 -0
- package/src/formats/registry.js +177 -0
- package/src/formats/rivet.js +259 -0
- package/src/formats/shared.js +98 -0
- package/src/formats/structure.js +182 -0
- package/src/formats/yaml.js +127 -0
- package/src/frame/frame.js +962 -0
- package/src/frame/vendor.js +1484 -0
- package/src/gate/audit.js +116 -0
- package/src/gate/gate.js +365 -0
- package/src/gate/policy.js +295 -0
- package/src/gate/scripted.js +110 -0
- package/src/gate/seams.js +32 -0
- package/src/gate/store.js +66 -0
- package/src/geometry/model.js +533 -0
- package/src/grant-key.js +149 -0
- package/src/graph/model.js +381 -0
- package/src/headless/a2a-push.js +123 -0
- package/src/headless/channels/config.js +142 -0
- package/src/headless/channels/connectors.js +283 -0
- package/src/headless/channels/index.js +257 -0
- package/src/headless/channels/mail.js +197 -0
- package/src/headless/channels/mailnet.js +223 -0
- package/src/headless/channels/platforms.js +194 -0
- package/src/headless/channels/replay.js +49 -0
- package/src/headless/cli.js +1397 -0
- package/src/headless/companion-client.js +182 -0
- package/src/headless/cost-cli.js +177 -0
- package/src/headless/deprecations-cli.js +102 -0
- package/src/headless/examples.js +48 -0
- package/src/headless/ffmpeg-exec.js +217 -0
- package/src/headless/gateways/config.js +213 -0
- package/src/headless/gateways/homeassistant.js +159 -0
- package/src/headless/gateways/index.js +121 -0
- package/src/headless/gateways/matter.js +150 -0
- package/src/headless/gateways/mqtt-client.js +177 -0
- package/src/headless/gateways/mqtt.js +70 -0
- package/src/headless/gateways/netway.js +226 -0
- package/src/headless/gateways/serverpod-worker.mjs +16 -0
- package/src/headless/gateways/serverpod.js +71 -0
- package/src/headless/gateways/util.js +27 -0
- package/src/headless/grant-cli.js +208 -0
- package/src/headless/guard.js +125 -0
- package/src/headless/guards.js +124 -0
- package/src/headless/machine-cli.js +853 -0
- package/src/headless/mcp-endpoint.js +287 -0
- package/src/headless/mcp-roots.js +38 -0
- package/src/headless/model-endpoint.js +88 -0
- package/src/headless/net.js +151 -0
- package/src/headless/node.js +263 -0
- package/src/headless/npm-tarball.js +29 -0
- package/src/headless/operator-page.js +112 -0
- package/src/headless/operator.js +242 -0
- package/src/headless/people.js +34 -0
- package/src/headless/pkg-cli.js +1103 -0
- package/src/headless/pkg-serve.js +138 -0
- package/src/headless/process-worker.js +88 -0
- package/src/headless/runner-remote.js +477 -0
- package/src/headless/sandbox-child.mjs +21 -0
- package/src/headless/scheduled.js +557 -0
- package/src/headless/serve-stream.js +97 -0
- package/src/headless/serve.js +2238 -0
- package/src/headless/short-links.js +158 -0
- package/src/headless/static-dirs.js +207 -0
- package/src/headless/trust-file.js +51 -0
- package/src/headless/watch.js +291 -0
- package/src/headless/ws-connect.js +65 -0
- package/src/headless/ws-connections.js +44 -0
- package/src/headless/ws-echo.js +150 -0
- package/src/headless/ws-proxy.js +354 -0
- package/src/headless/ws-ticket.js +48 -0
- package/src/hooks/config.js +50 -0
- package/src/hooks/engine.js +189 -0
- package/src/input/log.js +613 -0
- package/src/input/player.js +217 -0
- package/src/input/replay.js +209 -0
- package/src/jth/vendor.js +28 -0
- package/src/ledger/tx.js +94 -0
- package/src/machine/apply.js +193 -0
- package/src/machine/diff.js +168 -0
- package/src/machine/drive.js +296 -0
- package/src/machine/engine.js +478 -0
- package/src/machine/files-tool.js +76 -0
- package/src/machine/lint.js +139 -0
- package/src/machine/llmtm-trace.js +117 -0
- package/src/machine/llmtm.js +475 -0
- package/src/machine/otel.js +80 -0
- package/src/machine/pane-provider.js +51 -0
- package/src/machine/propose.js +159 -0
- package/src/machine/random.js +52 -0
- package/src/machine/risk.js +107 -0
- package/src/machine/session.js +1017 -0
- package/src/machine/shell-tool.js +45 -0
- package/src/machine/tape.js +349 -0
- package/src/machine/templates.js +104 -0
- package/src/machine/trace.js +664 -0
- package/src/machine/view.js +73 -0
- package/src/machine/vm-tool.js +47 -0
- package/src/machine/watchdog.js +46 -0
- package/src/mcp/app-args.js +51 -0
- package/src/mcp/app-run.js +56 -0
- package/src/mcp/apps.js +468 -0
- package/src/mcp/cache.js +232 -0
- package/src/mcp/client.js +507 -0
- package/src/mcp/demo-app.js +183 -0
- package/src/mcp/demo-server.js +602 -0
- package/src/mcp/elicitation.js +116 -0
- package/src/mcp/hub.js +1326 -0
- package/src/mcp/roots.js +139 -0
- package/src/mcp/sampling.js +105 -0
- package/src/mcp/settings.js +125 -0
- package/src/mcp/tape.js +292 -0
- package/src/mcp/uri-template.js +156 -0
- package/src/mcp/wsh-client.js +164 -0
- package/src/media/animated-codec.js +7 -0
- package/src/media/animated.js +623 -0
- package/src/media/compare-agent.js +70 -0
- package/src/media/compare-job.js +63 -0
- package/src/media/compare-page.js +296 -0
- package/src/media/compare-worker.js +14 -0
- package/src/media/compare.js +600 -0
- package/src/media/contract.js +62 -0
- package/src/media/decode-frame.js +57 -0
- package/src/media/decode-pool.js +188 -0
- package/src/media/ffmpeg-edit.js +344 -0
- package/src/media/ffmpeg-wasm-client.js +121 -0
- package/src/media/ffmpeg-wasm-session.js +79 -0
- package/src/media/ffmpeg-wasm.js +139 -0
- package/src/media/ffmpeg-worker.js +23 -0
- package/src/media/ffmpeg.js +903 -0
- package/src/media/helpers.js +419 -0
- package/src/media/keep.js +108 -0
- package/src/media/metadata-worker.js +16 -0
- package/src/media/metadata.js +2518 -0
- package/src/media/mime.js +102 -0
- package/src/media/page-decode.js +74 -0
- package/src/media/persist.js +114 -0
- package/src/media/png.js +226 -0
- package/src/media/sample-input.js +67 -0
- package/src/media/store.js +86 -0
- package/src/media/wav.js +109 -0
- package/src/midi/mappings.js +250 -0
- package/src/midi/message.js +125 -0
- package/src/model/action-hub.js +143 -0
- package/src/model/adapters.js +284 -0
- package/src/model/agent-runs.js +12 -0
- package/src/model/arrange.js +308 -0
- package/src/model/automation-clock.js +494 -0
- package/src/model/automation.js +653 -0
- package/src/model/batches.js +78 -0
- package/src/model/binary-view.js +195 -0
- package/src/model/breakpoints.js +392 -0
- package/src/model/calc-settings.js +65 -0
- package/src/model/calendar.js +254 -0
- package/src/model/canvas-id.js +5 -0
- package/src/model/canvas.js +5048 -0
- package/src/model/channel-names.js +29 -0
- package/src/model/channels.js +692 -0
- package/src/model/chart-clock.js +631 -0
- package/src/model/chart-code.js +69 -0
- package/src/model/codecs.js +60 -0
- package/src/model/companion-access.js +271 -0
- package/src/model/companion-pane.js +50 -0
- package/src/model/composite.js +324 -0
- package/src/model/context.js +380 -0
- package/src/model/controls.js +231 -0
- package/src/model/converter-presets.js +368 -0
- package/src/model/cost.js +580 -0
- package/src/model/curve-transform.js +122 -0
- package/src/model/cycles.js +176 -0
- package/src/model/debug-log.js +168 -0
- package/src/model/decision-clock.js +100 -0
- package/src/model/decisions.js +265 -0
- package/src/model/desk.js +87 -0
- package/src/model/device-hub.js +400 -0
- package/src/model/diff.js +445 -0
- package/src/model/emit-events.js +179 -0
- package/src/model/errors.js +238 -0
- package/src/model/event-presets.js +297 -0
- package/src/model/events.js +1303 -0
- package/src/model/explicit-setters.js +139 -0
- package/src/model/field-mapping.js +623 -0
- package/src/model/files-pane.js +263 -0
- package/src/model/flow-idle.js +89 -0
- package/src/model/foreach.js +549 -0
- package/src/model/frame-loop.js +71 -0
- package/src/model/frame-pressure.js +180 -0
- package/src/model/function-scaffold.js +1000 -0
- package/src/model/geometry-settings.js +54 -0
- package/src/model/goals.js +176 -0
- package/src/model/graph-settings.js +76 -0
- package/src/model/grid-widget.js +175 -0
- package/src/model/groups.js +332 -0
- package/src/model/hash.js +21 -0
- package/src/model/history.js +259 -0
- package/src/model/http-copy.js +88 -0
- package/src/model/http-send-companion.js +110 -0
- package/src/model/http-send.js +163 -0
- package/src/model/http-tap.js +521 -0
- package/src/model/http-tools.js +1138 -0
- package/src/model/import-lock.js +78 -0
- package/src/model/imports.js +258 -0
- package/src/model/infer-outputs.js +822 -0
- package/src/model/ink-connect.js +150 -0
- package/src/model/ink-diff.js +75 -0
- package/src/model/ink-gone.js +71 -0
- package/src/model/ink-route.js +120 -0
- package/src/model/ink-svg.js +206 -0
- package/src/model/ink.js +739 -0
- package/src/model/input-log-hub.js +456 -0
- package/src/model/input-split.js +69 -0
- package/src/model/json-schema-host.js +9 -0
- package/src/model/json-schema.js +368 -0
- package/src/model/jth.js +78 -0
- package/src/model/latex-expr.js +528 -0
- package/src/model/layout-history.js +120 -0
- package/src/model/lazy-map.js +88 -0
- package/src/model/ledger-pane.js +101 -0
- package/src/model/legacy-viaframe.js +90 -0
- package/src/model/lessons.js +448 -0
- package/src/model/links.js +981 -0
- package/src/model/live.js +411 -0
- package/src/model/math-widget.js +53 -0
- package/src/model/memory-settings.js +33 -0
- package/src/model/midi-controls.js +350 -0
- package/src/model/nl-time.js +408 -0
- package/src/model/notebook.js +137 -0
- package/src/model/pane-view-store.js +15 -0
- package/src/model/partial.js +115 -0
- package/src/model/pause-clock.js +53 -0
- package/src/model/peer-pane.js +32 -0
- package/src/model/persist-state.js +290 -0
- package/src/model/person-only.js +122 -0
- package/src/model/pin.js +209 -0
- package/src/model/plot-3d.js +213 -0
- package/src/model/plot-series.js +157 -0
- package/src/model/plot-settings.js +531 -0
- package/src/model/ports.js +312 -0
- package/src/model/presentation.js +222 -0
- package/src/model/preset-apply.js +316 -0
- package/src/model/preset-banks.js +335 -0
- package/src/model/preset-morph.js +263 -0
- package/src/model/preset-ops.js +205 -0
- package/src/model/preset-player.js +283 -0
- package/src/model/preset-states.js +172 -0
- package/src/model/profile-settings.js +109 -0
- package/src/model/promote.js +103 -0
- package/src/model/protected.js +10 -0
- package/src/model/proxy-log.js +230 -0
- package/src/model/proxy.js +367 -0
- package/src/model/quantity-widget.js +61 -0
- package/src/model/queue-book.js +359 -0
- package/src/model/quick-add.js +224 -0
- package/src/model/rate-limit.js +313 -0
- package/src/model/rate.js +95 -0
- package/src/model/recovery.js +216 -0
- package/src/model/rename-follow.js +113 -0
- package/src/model/repeater.js +1126 -0
- package/src/model/retention.js +664 -0
- package/src/model/route-edit.js +132 -0
- package/src/model/router.js +155 -0
- package/src/model/runaway.js +232 -0
- package/src/model/runlog.js +1033 -0
- package/src/model/runners/a2a.js +2 -0
- package/src/model/runners/acp.js +2 -0
- package/src/model/runners/actions.js +4 -0
- package/src/model/runners/agent.js +4 -0
- package/src/model/runners/audio.js +21 -0
- package/src/model/runners/automation.js +2 -0
- package/src/model/runners/calc.js +23 -0
- package/src/model/runners/catch.js +10 -0
- package/src/model/runners/codec.js +24 -0
- package/src/model/runners/companion-run.js +2 -0
- package/src/model/runners/device.js +7 -0
- package/src/model/runners/dom.js +13 -0
- package/src/model/runners/effects.js +2 -0
- package/src/model/runners/embed.js +11 -0
- package/src/model/runners/eval.js +2 -0
- package/src/model/runners/files.js +19 -0
- package/src/model/runners/geometry.js +23 -0
- package/src/model/runners/goals.js +18 -0
- package/src/model/runners/graph.js +23 -0
- package/src/model/runners/html.js +2 -0
- package/src/model/runners/http-proxy.js +185 -0
- package/src/model/runners/http-send.js +61 -0
- package/src/model/runners/http-static.js +83 -0
- package/src/model/runners/http-tap.js +26 -0
- package/src/model/runners/http.js +40 -0
- package/src/model/runners/import.js +2 -0
- package/src/model/runners/index.js +74 -0
- package/src/model/runners/input.js +14 -0
- package/src/model/runners/inputlog.js +7 -0
- package/src/model/runners/ledger.js +15 -0
- package/src/model/runners/machine.js +4 -0
- package/src/model/runners/mcp.js +2 -0
- package/src/model/runners/mcpapp.js +2 -0
- package/src/model/runners/media.js +18 -0
- package/src/model/runners/memory.js +28 -0
- package/src/model/runners/mesh-inspect.js +13 -0
- package/src/model/runners/peer.js +14 -0
- package/src/model/runners/plot.js +22 -0
- package/src/model/runners/render.js +14 -0
- package/src/model/runners/respond.js +2 -0
- package/src/model/runners/sample-soundfont.js +85 -0
- package/src/model/runners/sample.js +159 -0
- package/src/model/runners/scene.js +8 -0
- package/src/model/runners/score.js +24 -0
- package/src/model/runners/secret.js +10 -0
- package/src/model/runners/select.js +2 -0
- package/src/model/runners/shared.js +4 -0
- package/src/model/runners/shares.js +15 -0
- package/src/model/runners/shell.js +7 -0
- package/src/model/runners/skill.js +26 -0
- package/src/model/runners/state.js +2 -0
- package/src/model/runners/statechart.js +4 -0
- package/src/model/runners/subgraph.js +9 -0
- package/src/model/runners/tabs.js +25 -0
- package/src/model/runners/terminal.js +5 -0
- package/src/model/runners/text.js +4 -0
- package/src/model/runners/timer.js +7 -0
- package/src/model/runners/traffic.js +65 -0
- package/src/model/runners/trigger.js +2 -0
- package/src/model/runners/verdict.js +29 -0
- package/src/model/runners/vm.js +7 -0
- package/src/model/runners/wait.js +4 -0
- package/src/model/runners/ws-client.js +57 -0
- package/src/model/safe-regex.js +719 -0
- package/src/model/sample-settings.js +43 -0
- package/src/model/sample-source.js +47 -0
- package/src/model/saved-mapping.js +153 -0
- package/src/model/score-controllers.js +87 -0
- package/src/model/score-edit.js +399 -0
- package/src/model/score-instruments.js +300 -0
- package/src/model/score-library.js +407 -0
- package/src/model/score-markup.js +235 -0
- package/src/model/score-mix.js +248 -0
- package/src/model/score-patterns.js +276 -0
- package/src/model/score-selection.js +34 -0
- package/src/model/score-time.js +139 -0
- package/src/model/score-transforms.js +336 -0
- package/src/model/score-transport.js +192 -0
- package/src/model/selection.js +351 -0
- package/src/model/serial-controls.js +328 -0
- package/src/model/shares-pane.js +17 -0
- package/src/model/shell-pane.js +106 -0
- package/src/model/signature.js +1122 -0
- package/src/model/size-hints.js +106 -0
- package/src/model/size-ratio.js +12 -0
- package/src/model/skill-settings.js +23 -0
- package/src/model/snapping.js +466 -0
- package/src/model/split.js +375 -0
- package/src/model/spring-player.js +222 -0
- package/src/model/statecharts.js +1369 -0
- package/src/model/states-preset.js +208 -0
- package/src/model/static-route.js +100 -0
- package/src/model/step-words.js +45 -0
- package/src/model/subgraph.js +1042 -0
- package/src/model/switch.js +98 -0
- package/src/model/tabs-store.js +10 -0
- package/src/model/tensor-view.js +146 -0
- package/src/model/timer-clock.js +361 -0
- package/src/model/timers.js +422 -0
- package/src/model/timing.js +212 -0
- package/src/model/traffic-agent.js +111 -0
- package/src/model/traffic-store.js +20 -0
- package/src/model/traffic.js +665 -0
- package/src/model/transport.js +364 -0
- package/src/model/turns.js +86 -0
- package/src/model/types.js +425 -0
- package/src/model/value-path.js +138 -0
- package/src/model/versions.js +535 -0
- package/src/model/view-outputs.js +144 -0
- package/src/model/viewport-bookmarks.js +194 -0
- package/src/model/virtual-clock.js +85 -0
- package/src/model/vm-pane.js +258 -0
- package/src/model/wait-clock.js +715 -0
- package/src/model/wait-persist.js +248 -0
- package/src/model/waits.js +431 -0
- package/src/model/watch.js +80 -0
- package/src/model/webhook.js +496 -0
- package/src/model/when.js +48 -0
- package/src/model/widget-values.js +392 -0
- package/src/model/window-commands.js +278 -0
- package/src/model/windows.js +348 -0
- package/src/model/wire-inspect.js +343 -0
- package/src/model/wire-ops.js +324 -0
- package/src/model/wire-pipeline.js +378 -0
- package/src/model/wire-route.js +637 -0
- package/src/model/workspaces.js +122 -0
- package/src/model/ws-client.js +155 -0
- package/src/model/ws-demo.js +31 -0
- package/src/model/ws-frames.js +141 -0
- package/src/model/ws-hub.js +219 -0
- package/src/model/ws-served.js +13 -0
- package/src/model/xstate.js +374 -0
- package/src/narrative/narrative.js +1557 -0
- package/src/network/auth.js +224 -0
- package/src/network/csp-frame.js +197 -0
- package/src/network/declarative-shadow.js +27 -0
- package/src/network/frame-net-shim.js +962 -0
- package/src/network/hosts.js +157 -0
- package/src/network/policy.js +520 -0
- package/src/network/rtc-shim.js +418 -0
- package/src/network/secret-gate.js +78 -0
- package/src/network/secrets.js +261 -0
- package/src/network/sockets.js +287 -0
- package/src/network/store.js +186 -0
- package/src/network/streams.js +99 -0
- package/src/packages/asset-values.js +50 -0
- package/src/packages/assets.js +99 -0
- package/src/packages/deprecation.js +203 -0
- package/src/packages/fetch.js +152 -0
- package/src/packages/function-canvas.js +73 -0
- package/src/packages/module.js +120 -0
- package/src/packages/package.js +474 -0
- package/src/packages/published.js +27 -0
- package/src/packages/requires.js +83 -0
- package/src/packages/signing.js +145 -0
- package/src/packages/trust.js +107 -0
- package/src/panes/a2a.js +51 -0
- package/src/panes/acp.js +60 -0
- package/src/panes/actions.js +54 -0
- package/src/panes/agent.js +83 -0
- package/src/panes/audio.js +103 -0
- package/src/panes/automation.js +112 -0
- package/src/panes/calc.js +67 -0
- package/src/panes/catch.js +52 -0
- package/src/panes/codec.js +59 -0
- package/src/panes/colors.js +12 -0
- package/src/panes/companion.js +54 -0
- package/src/panes/decision.js +65 -0
- package/src/panes/device.js +56 -0
- package/src/panes/dom.js +37 -0
- package/src/panes/effects.js +118 -0
- package/src/panes/embed.js +30 -0
- package/src/panes/eval.js +186 -0
- package/src/panes/fields.js +299 -0
- package/src/panes/files.js +45 -0
- package/src/panes/gate.js +70 -0
- package/src/panes/geometry.js +64 -0
- package/src/panes/goals.js +75 -0
- package/src/panes/graph.js +66 -0
- package/src/panes/html.js +54 -0
- package/src/panes/http.js +82 -0
- package/src/panes/import.js +38 -0
- package/src/panes/input.js +124 -0
- package/src/panes/inputlog.js +66 -0
- package/src/panes/ledger.js +56 -0
- package/src/panes/machine.js +138 -0
- package/src/panes/mcp.js +85 -0
- package/src/panes/mcpapp.js +64 -0
- package/src/panes/media.js +38 -0
- package/src/panes/memory.js +46 -0
- package/src/panes/mesh-inspect.js +49 -0
- package/src/panes/note.js +38 -0
- package/src/panes/peer.js +44 -0
- package/src/panes/plot.js +196 -0
- package/src/panes/registry.js +475 -0
- package/src/panes/render.js +87 -0
- package/src/panes/respond.js +44 -0
- package/src/panes/router.js +84 -0
- package/src/panes/sample.js +75 -0
- package/src/panes/scene.js +55 -0
- package/src/panes/score.js +87 -0
- package/src/panes/secret.js +38 -0
- package/src/panes/select.js +67 -0
- package/src/panes/shader.js +166 -0
- package/src/panes/shared.js +33 -0
- package/src/panes/shares.js +43 -0
- package/src/panes/shell.js +47 -0
- package/src/panes/skill.js +44 -0
- package/src/panes/state.js +91 -0
- package/src/panes/statechart.js +63 -0
- package/src/panes/story.js +33 -0
- package/src/panes/subgraph.js +94 -0
- package/src/panes/tabs.js +52 -0
- package/src/panes/terminal.js +57 -0
- package/src/panes/text.js +36 -0
- package/src/panes/timer.js +55 -0
- package/src/panes/traffic.js +59 -0
- package/src/panes/verdict.js +62 -0
- package/src/panes/vm.js +46 -0
- package/src/panes/wait.js +61 -0
- package/src/panes/watch.js +57 -0
- package/src/panes/webhook.js +67 -0
- package/src/panes/ws-client.js +67 -0
- package/src/persist.js +281 -0
- package/src/plot/tex.js +19 -0
- package/src/plot/vendor.js +59 -0
- package/src/plot/view-vendor.js +5 -0
- package/src/privacy/levels.js +108 -0
- package/src/protocol/devtools.js +116 -0
- package/src/protocol/inspector.js +368 -0
- package/src/render/time.js +381 -0
- package/src/rooms/mesh-metrics.js +162 -0
- package/src/run/companion.js +355 -0
- package/src/run/describe.js +114 -0
- package/src/run/local-model.js +150 -0
- package/src/run/monitor.js +208 -0
- package/src/run/publish.js +360 -0
- package/src/run/respond-outcome.js +12 -0
- package/src/run/run.js +820 -0
- package/src/run/runner-pairing.js +243 -0
- package/src/run/values.js +122 -0
- package/src/runtime/compile.js +122 -0
- package/src/runtime/console-tag.js +64 -0
- package/src/runtime/frame-signals.js +126 -0
- package/src/runtime/iteration-push.js +19 -0
- package/src/runtime/keys.js +474 -0
- package/src/runtime/midi-input.js +170 -0
- package/src/runtime/person-waits.js +140 -0
- package/src/runtime/resource-timing.js +56 -0
- package/src/runtime/runtime.js +2150 -0
- package/src/runtime/sandbox-modules.js +163 -0
- package/src/runtime/spring.js +246 -0
- package/src/runtime/strict.js +215 -0
- package/src/runtime/transfer.js +75 -0
- package/src/runtime/tween.js +158 -0
- package/src/runtime/wasm-fuel.js +596 -0
- package/src/runtime/worker/00-prelude.js +88 -0
- package/src/runtime/worker/10-helpers.js +295 -0
- package/src/runtime/worker/20-io.js +645 -0
- package/src/runtime/worker/30-previews.js +210 -0
- package/src/runtime/worker/40-profile-lineage.js +278 -0
- package/src/runtime/worker/50-own-worker.js +233 -0
- package/src/runtime/worker/60-timers.js +172 -0
- package/src/runtime/worker/70-state-keyed.js +193 -0
- package/src/runtime/worker/80-each-compile-generators.js +619 -0
- package/src/runtime/worker/90-events-operators.js +655 -0
- package/src/runtime/worker/95-agents-batches.js +1820 -0
- package/src/runtime/worker/96-plot.js +47 -0
- package/src/runtime/worker/97-calc.js +28 -0
- package/src/runtime/worker/98-geometry.js +24 -0
- package/src/runtime/worker/99-graph.js +30 -0
- package/src/runtime/worker/99-shell.js +30 -0
- package/src/safety/config.js +57 -0
- package/src/safety/engine.js +324 -0
- package/src/scene/pixi-scene.js +806 -0
- package/src/scene/scene-assets.js +241 -0
- package/src/scene/scene-edit.js +319 -0
- package/src/secret/settings.js +41 -0
- package/src/security/multipart.js +257 -0
- package/src/security/redact.js +261 -0
- package/src/serial/broker.js +473 -0
- package/src/serial/config.js +185 -0
- package/src/serial/framers.js +389 -0
- package/src/serial/packets.js +64 -0
- package/src/shader/audio-texture.js +87 -0
- package/src/shader/channels.js +496 -0
- package/src/shader/common.js +90 -0
- package/src/shader/model.js +867 -0
- package/src/shader/passes.js +150 -0
- package/src/shader/shadertoy.js +198 -0
- package/src/shell/hub.js +287 -0
- package/src/sim/checkpoints.js +850 -0
- package/src/sim/compile.js +37 -0
- package/src/sim/fixed.js +377 -0
- package/src/sim/headless.js +205 -0
- package/src/sim/physics.js +399 -0
- package/src/smf-helper/vendor.js +2 -0
- package/src/sources/config.js +435 -0
- package/src/storage.js +153 -0
- package/src/table/table-geo.js +2344 -0
- package/src/table/table-lineage.js +808 -0
- package/src/table/table-quality.js +355 -0
- package/src/table/table-schema.js +392 -0
- package/src/table/table-sql.js +1650 -0
- package/src/table/table-stats.js +480 -0
- package/src/table/table.js +2102 -0
- package/src/tensor/tensor.js +1153 -0
- package/src/terminal/hub.js +715 -0
- package/src/terminal/screen.js +516 -0
- package/src/tests/capture.js +88 -0
- package/src/tests/compare.js +480 -0
- package/src/tests/dataset.js +170 -0
- package/src/tests/evaluate.js +286 -0
- package/src/tests/from-runs.js +99 -0
- package/src/tests/judge.js +134 -0
- package/src/tests/matchers.js +263 -0
- package/src/tests/matrix-spec.js +87 -0
- package/src/tests/matrix.js +397 -0
- package/src/tests/metrics.js +369 -0
- package/src/tests/no-value.js +42 -0
- package/src/tests/pane-check.js +45 -0
- package/src/tests/report.js +124 -0
- package/src/tests/reporters.js +140 -0
- package/src/tests/run.js +155 -0
- package/src/tilemap/tilemap.js +1122 -0
- package/src/units/units.js +3 -0
- package/src/util/bytes-text.js +21 -0
- package/src/util/num.js +10 -0
- package/src/util/regexp.js +2 -0
- package/src/util/text.js +38 -0
- package/src/verdict/engine.js +154 -0
- package/src/vm/guest-wsh.js +108 -0
- package/src/vm/hub.js +457 -0
- package/src/vm/image-files.js +4 -0
- package/src/vm/image.js +33 -0
- package/src/vm/image.lock.json +13 -0
- package/src/vm/link.js +266 -0
- package/src/vm/mount.js +212 -0
- package/src/vm/net.js +85 -0
- package/src/vm/pipe-ws.js +187 -0
- package/src/vm/v86-loader.js +11 -0
- package/src/webmcp/args.js +212 -0
- package/src/webmcp/hidden.js +218 -0
- package/src/webmcp/text.js +250 -0
- package/src/webmcp/tools/bookmark-tools.js +106 -0
- package/src/webmcp/tools/breakpoint-tools.js +48 -0
- package/src/webmcp/tools/canvases.js +208 -0
- package/src/webmcp/tools/context-tools.js +84 -0
- package/src/webmcp/tools/core.js +170 -0
- package/src/webmcp/tools/devices.js +41 -0
- package/src/webmcp/tools/edit.js +357 -0
- package/src/webmcp/tools/event-tools.js +145 -0
- package/src/webmcp/tools/image-tools.js +173 -0
- package/src/webmcp/tools/ink-tools.js +247 -0
- package/src/webmcp/tools/input-log-tools.js +163 -0
- package/src/webmcp/tools/live.js +166 -0
- package/src/webmcp/tools/machine-tools.js +74 -0
- package/src/webmcp/tools/media.js +161 -0
- package/src/webmcp/tools/midi-tools.js +32 -0
- package/src/webmcp/tools/preset-tools.js +184 -0
- package/src/webmcp/tools/quick-add-tools.js +95 -0
- package/src/webmcp/tools/reshape.js +152 -0
- package/src/webmcp/tools/run-tools.js +309 -0
- package/src/webmcp/tools/runs.js +101 -0
- package/src/webmcp/tools/scaffold-tools.js +161 -0
- package/src/webmcp/tools/serial-tools.js +36 -0
- package/src/webmcp/tools/shared.js +338 -0
- package/src/webmcp/tools/subgraphs.js +360 -0
- package/src/webmcp/tools/tests.js +170 -0
- package/src/webmcp/tools/traffic-tools.js +70 -0
- package/src/webmcp/tools/transport-tools.js +65 -0
- package/src/webmcp/tools/version-tools.js +126 -0
- package/src/webmcp/tools/versions.js +11 -0
- package/src/webmcp/tools/wire-tools.js +90 -0
- package/src/webmcp/tools/workspace-tools.js +93 -0
- package/src/webmcp/tools.js +1323 -0
- package/src/wsh/connect.js +347 -0
- package/src/wsh/demo-host.js +361 -0
- package/src/wsh/e2e.js +27 -0
- package/src/wsh/keys.js +154 -0
- package/src/wsh/known-hosts.js +39 -0
- package/src/wsh/private-key.js +95 -0
- package/src/wsh/reverse.js +34 -0
- package/src/wsh/settings.js +118 -0
- package/src/wsh/transfer.js +62 -0
- package/src/wsh/vendor.js +5 -0
- package/src/x402/challenge.js +171 -0
- package/src/x402/demo-server.js +127 -0
- package/src/x402/pay.js +125 -0
- package/src/x402/retry.js +95 -0
- package/src/x402/store.js +76 -0
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
// The machine's trace as a file and as a replay (docs/design/machine-pane.md, "The trace"; LTM-TRACE-01 to 07, 09, 11, 12, 15): the event
|
|
2
|
+
// schema, the run header (what ran: a content hash of the compiled definition, the provider and settings identity, never a key), JSONL out and
|
|
3
|
+
// in, the redaction every export goes through, replay of a recorded trace into frames (the tape at each step, with no model called), the
|
|
4
|
+
// fork of a run at a frame, and the diff of two runs by step and by tape. Pure: no DOM, no clock, no storage.
|
|
5
|
+
// Written from the behaviour the llmtm parity matrix describes, not from llmtm's code (D3).
|
|
6
|
+
import { createRun, runValue } from "./engine.js";
|
|
7
|
+
import { applyAction, resolveMove, routeFor, contentProblem } from "./apply.js";
|
|
8
|
+
import { diffSequences, metricTable } from "../agent/diff.js";
|
|
9
|
+
import { SECRET_SHAPES, REDACTED, isCredentialField, isPlaceholderValue, redactBody, redactUrl } from "../security/redact.js";
|
|
10
|
+
import { matches as pathMatches, cleanPolicy, WITHHELD } from "../model/retention.js";
|
|
11
|
+
import { currentFormat } from "../model/legacy-viaframe.js";
|
|
12
|
+
|
|
13
|
+
export const TRACE_FORMAT = "mallory-machine-trace";
|
|
14
|
+
export const TRACE_VERSION = 1;
|
|
15
|
+
/** The most a trace file may hold, read or written: a longer one is refused, never cut (a cut trace would replay as something else). */
|
|
16
|
+
export const MAX_TRACE_LINES = 200_000;
|
|
17
|
+
export const MAX_TRACE_CHARS = 64 * 1024 * 1024;
|
|
18
|
+
|
|
19
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
20
|
+
|
|
21
|
+
// ── the event schema ──────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
const T = { string: (v) => typeof v === "string", number: (v) => Number.isFinite(v), boolean: (v) => typeof v === "boolean", object: isObject, array: Array.isArray, any: () => true };
|
|
24
|
+
/**
|
|
25
|
+
* The vocabulary, version 1: `type` -> { required, optional } (field -> JSON type). Fields not listed are allowed (a later version may add
|
|
26
|
+
* some) and kept; a `type` not listed is kept and shown as unknown, never dropped (LTM-TRACE-13), and replay steps over it.
|
|
27
|
+
*/
|
|
28
|
+
export const EVENT_SCHEMA = Object.freeze({
|
|
29
|
+
run_meta: { required: { format: "string", version: "number", machine: "string", definition: "object", maxSteps: "number" }, optional: { run: "string", provider: "object", settings: "object", maxCostUsd: "number", maxDurationMs: "number", inject: "object", parent: "object" } },
|
|
30
|
+
step_committed: { required: { step: "number", before: "object", after: "object", proposal: "object", cellBefore: "any", cellAfter: "any", attempts: "number", usage: "object" }, optional: { grew: "string", routedBy: "string", degraded: "string", dropped: "array", asked: "object", llm: "any", cost: "number", tool: "object", child: "object", window: "object", ref: "object" } },
|
|
31
|
+
submachine_run: { required: { step: "number", state: "string", machine: "string", outcome: "string", attempts: "number", steps: "number" }, optional: { halt: "string", error: "string" } },
|
|
32
|
+
tool_call: { required: { step: "number", state: "string", tool: "string", outcome: "string", attempts: "number" }, optional: { args: "any", error: "string", code: "string" } },
|
|
33
|
+
proposal_rejected: { required: { step: "number", tier: "string", reason: "string", policy: "string", attempt: "number" }, optional: { proposal: "object", waitMs: "number" } },
|
|
34
|
+
approval_requested: { required: { step: "number", state: "string", proposal: "object" }, optional: {} },
|
|
35
|
+
approval_decided: { required: { step: "number", decision: "string" }, optional: { proposed: "string", landed: "string", reason: "string", scripted: "boolean", auto: "string" } },
|
|
36
|
+
question_answered: { required: { step: "number", state: "string", action: "string" }, optional: { timedOut: "boolean" } },
|
|
37
|
+
risk_assessed: { required: { step: "number", state: "string", tier: "string", reasons: "array", route: "string" }, optional: {} },
|
|
38
|
+
halt: { required: { step: "number", kind: "string", reason: "string" }, optional: {} },
|
|
39
|
+
restart: { required: { n: "number", kind: "string" }, optional: { text: "string", delay: "number" } },
|
|
40
|
+
person_edit: { required: { step: "number", cell: "string" }, optional: { kind: "string", content: "string" } },
|
|
41
|
+
resume: { required: { step: "number", why: "string" }, optional: { request: "string", decision: "string", afterReload: "boolean", from: "string", at: "number" } },
|
|
42
|
+
fork: { required: { at: "number", step: "number", from: "string" }, optional: { edited: "array" } },
|
|
43
|
+
// A person's note at a step of a run (LTM-UI-18, LTM-HUB-03). The driver never writes one: they live in the person's own store and are added to a trace
|
|
44
|
+
// when it is exported (`withComments`), and read back by `commentsOf`. `frame` is the replay frame it was made at (the scrubber puts it there), `step` the run's step.
|
|
45
|
+
comment: { required: { id: "string", step: "number", text: "string", at: "number" }, optional: { frame: "number" } },
|
|
46
|
+
});
|
|
47
|
+
export const EVENT_TYPES = Object.freeze(Object.keys(EVENT_SCHEMA));
|
|
48
|
+
|
|
49
|
+
/** What is wrong with one event, or "" when it fits the schema (an unknown type fits: it is kept). */
|
|
50
|
+
export function eventProblem(e) {
|
|
51
|
+
if (!isObject(e)) return "an event is an object";
|
|
52
|
+
if (typeof e.type !== "string" || !e.type) return "an event has a type";
|
|
53
|
+
const spec = EVENT_SCHEMA[e.type];
|
|
54
|
+
if (!spec) return "";
|
|
55
|
+
for (const [k, t] of Object.entries(spec.required)) if (!Object.hasOwn(e, k) || !T[t](e[k])) return `${e.type}: ${k} must be present (${t})`;
|
|
56
|
+
for (const [k, t] of Object.entries(spec.optional)) if (Object.hasOwn(e, k) && e[k] !== undefined && !T[t](e[k])) return `${e.type}: ${k} must be a ${t}`;
|
|
57
|
+
return "";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ── identity: hashes ──────────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
const K = new Uint32Array([
|
|
63
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
64
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
65
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
66
|
+
]);
|
|
67
|
+
/** SHA-256 of a text as hex, synchronous (the header is written inside a step, which can't wait for crypto.subtle). */
|
|
68
|
+
export function sha256(text) {
|
|
69
|
+
const bytes = new TextEncoder().encode(String(text));
|
|
70
|
+
const total = Math.ceil((bytes.length + 9) / 64) * 64;
|
|
71
|
+
const data = new Uint8Array(total);
|
|
72
|
+
data.set(bytes);
|
|
73
|
+
data[bytes.length] = 0x80;
|
|
74
|
+
const view = new DataView(data.buffer);
|
|
75
|
+
view.setUint32(total - 8, Math.floor((bytes.length * 8) / 0x100000000));
|
|
76
|
+
view.setUint32(total - 4, (bytes.length * 8) >>> 0);
|
|
77
|
+
const H = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
|
|
78
|
+
const w = new Uint32Array(64);
|
|
79
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
80
|
+
for (let off = 0; off < total; off += 64) {
|
|
81
|
+
for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + i * 4);
|
|
82
|
+
for (let i = 16; i < 64; i++) {
|
|
83
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
84
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
85
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
86
|
+
}
|
|
87
|
+
let [a, b, c, d, e, f, g, h] = H;
|
|
88
|
+
for (let i = 0; i < 64; i++) {
|
|
89
|
+
const t1 = (h + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]) >>> 0;
|
|
90
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
|
|
91
|
+
h = g;
|
|
92
|
+
g = f;
|
|
93
|
+
f = e;
|
|
94
|
+
e = (d + t1) >>> 0;
|
|
95
|
+
d = c;
|
|
96
|
+
c = b;
|
|
97
|
+
b = a;
|
|
98
|
+
a = (t1 + t2) >>> 0;
|
|
99
|
+
}
|
|
100
|
+
H[0] += a; H[1] += b; H[2] += c; H[3] += d; H[4] += e; H[5] += f; H[6] += g; H[7] += h;
|
|
101
|
+
}
|
|
102
|
+
return [...H].map((x) => x.toString(16).padStart(8, "0")).join("");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** JSON with every object's keys in order: the same value always gives the same text. */
|
|
106
|
+
export function canonical(value) {
|
|
107
|
+
if (Array.isArray(value)) return `[${value.map((v) => canonical(v === undefined ? null : v)).join(",")}]`;
|
|
108
|
+
if (isObject(value)) return `{${Object.keys(value).filter((k) => value[k] !== undefined).sort().map((k) => `${JSON.stringify(k)}:${canonical(value[k])}`).join(",")}}`;
|
|
109
|
+
return JSON.stringify(value) ?? "null";
|
|
110
|
+
}
|
|
111
|
+
export const hashOf = (value) => `sha256:${sha256(canonical(value))}`;
|
|
112
|
+
|
|
113
|
+
/** The identity of a compiled definition: a hash of everything a run reads from it, locked cells included. */
|
|
114
|
+
export const definitionHash = (c) => hashOf(c);
|
|
115
|
+
|
|
116
|
+
/** A base URL as an identity: scheme and host only. A credential in it (user:password@, a query token, a path) is never copied. */
|
|
117
|
+
const hostOf = (url) => {
|
|
118
|
+
try {
|
|
119
|
+
const u = new URL(String(url));
|
|
120
|
+
return /^https?:$/.test(u.protocol) ? `${u.protocol}//${u.host}` : undefined;
|
|
121
|
+
} catch {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Who answered, with no key: the provider kind, the model, the host of a custom endpoint, and for a recording a hash of its replies.
|
|
128
|
+
* `llm` is the pane's llm setting, `replies` its recording.
|
|
129
|
+
*/
|
|
130
|
+
export function providerIdentity(llm = {}, replies) {
|
|
131
|
+
const kind = typeof llm.provider === "string" ? llm.provider : "recorded";
|
|
132
|
+
const host = llm.baseUrl ? hostOf(llm.baseUrl) : undefined;
|
|
133
|
+
return {
|
|
134
|
+
kind,
|
|
135
|
+
...(typeof llm.model === "string" && llm.model ? { model: llm.model } : {}),
|
|
136
|
+
...(host ? { host } : {}),
|
|
137
|
+
...(kind === "random" && Number.isInteger(llm.seed) ? { seed: llm.seed } : {}),
|
|
138
|
+
...(kind === "pane" && typeof llm.pane === "string" && llm.pane ? { pane: llm.pane } : {}),
|
|
139
|
+
...(kind === "recorded" ? { replies: Array.isArray(replies) ? replies.length : 0, repliesHash: hashOf(replies ?? []) } : {}),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The header of a run (`run_meta`): what ran, with nothing secret. `definition.hash` is the content hash of the compiled definition,
|
|
145
|
+
* `settings.hash` of the settings that decide what a run does (policy, gate, caps, watchdog, restart, price: no key, no name of one),
|
|
146
|
+
* `provider` as providerIdentity; `parent` is the lineage of a fork or a resumed run (`{ run, at, step, edited?, resumed? }`).
|
|
147
|
+
*/
|
|
148
|
+
export function runHeader(c, { run, maxSteps, maxCostUsd, maxDurationMs, inject, provider, settings, parent } = {}) {
|
|
149
|
+
return {
|
|
150
|
+
type: "run_meta",
|
|
151
|
+
format: TRACE_FORMAT,
|
|
152
|
+
version: TRACE_VERSION,
|
|
153
|
+
...(run ? { run: String(run) } : {}),
|
|
154
|
+
machine: c.id,
|
|
155
|
+
definition: { hash: definitionHash(c), states: Object.keys(c.states).length, cells: c.cells.length },
|
|
156
|
+
...(provider ? { provider } : {}),
|
|
157
|
+
...(settings ? { settings: { hash: hashOf(settings) } } : {}),
|
|
158
|
+
maxSteps,
|
|
159
|
+
...(maxCostUsd ? { maxCostUsd } : {}),
|
|
160
|
+
...(maxDurationMs ? { maxDurationMs } : {}),
|
|
161
|
+
...(inject ? { inject: { ...inject } } : {}),
|
|
162
|
+
...(parent ? { parent: { ...parent } } : {}),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ── JSONL ─────────────────────────────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Read a trace file. Fails closed: the first line must be a run header of this format and a version this reader knows, every line must be
|
|
170
|
+
* a JSON object, and a file over the limits is refused. Returns `{ events, header, problems, unknown }`; `problems` is empty for a sound
|
|
171
|
+
* trace (an event that does not fit the schema is listed, and replay stops there), `unknown` the types kept without being understood.
|
|
172
|
+
*/
|
|
173
|
+
export function parseTrace(text) {
|
|
174
|
+
const out = { events: [], header: null, problems: [], unknown: [] };
|
|
175
|
+
if (typeof text !== "string") return { ...out, problems: ["a trace is text (JSON Lines)"] };
|
|
176
|
+
if (text.length > MAX_TRACE_CHARS) return { ...out, problems: [`the trace is longer than ${MAX_TRACE_CHARS} characters`] };
|
|
177
|
+
const lines = text.split("\n").filter((l) => l.trim() !== "");
|
|
178
|
+
if (lines.length > MAX_TRACE_LINES) return { ...out, problems: [`the trace has more than ${MAX_TRACE_LINES} events`] };
|
|
179
|
+
if (!lines.length) return { ...out, problems: ["the trace is empty"] };
|
|
180
|
+
for (const [i, line] of lines.entries()) {
|
|
181
|
+
let e;
|
|
182
|
+
try {
|
|
183
|
+
e = JSON.parse(line);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
return { ...out, events: [], problems: [`line ${i + 1} is not JSON: ${error.message}`] };
|
|
186
|
+
}
|
|
187
|
+
if (!isObject(e)) return { ...out, events: [], problems: [`line ${i + 1} is not an event (an object)`] };
|
|
188
|
+
if (i === 0) {
|
|
189
|
+
if (e.type !== "run_meta" || currentFormat(e.format) !== TRACE_FORMAT) return { ...out, events: [], problems: [`the first line is not a ${TRACE_FORMAT} header`] };
|
|
190
|
+
if (e.version !== TRACE_VERSION) return { ...out, events: [], problems: [`this is trace version ${JSON.stringify(e.version)}; this reader knows version ${TRACE_VERSION}`] };
|
|
191
|
+
out.header = e;
|
|
192
|
+
}
|
|
193
|
+
const bad = eventProblem(e);
|
|
194
|
+
if (bad) out.problems.push(`line ${i + 1}: ${bad}`);
|
|
195
|
+
if (!EVENT_SCHEMA[e.type] && !out.unknown.includes(e.type)) out.unknown.push(e.type);
|
|
196
|
+
out.events.push(e);
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ── redaction: one policy for every export ───────────────────────────────────
|
|
202
|
+
|
|
203
|
+
/** The keys whose string values are structure (names of states, kinds, enums), not text a model or a person wrote: a summary keeps them. */
|
|
204
|
+
const STRUCTURE_KEYS = new Set(["type", "format", "tier", "kind", "move", "nextState", "state", "id", "cell", "status", "decision", "policy", "machine", "run", "hash", "cellKind", "degraded", "grew", "mode", "request", "from", "provider", "model", "host", "repliesHash", "why", "name", "dropped"]);
|
|
205
|
+
|
|
206
|
+
/** A URL anywhere in a text: its credential-named query and fragment parameters, userinfo and key-shaped path segments are taken out whatever the value looks like (S6). */
|
|
207
|
+
const URL_IN_TEXT = /\b[a-z][a-z0-9+.-]*:\/\/[^\s"'<>\\`]+/gi;
|
|
208
|
+
// A path segment that is a bare token (24 or more of letters, digits, `_` or `-`, with both a digit and a letter, or both cases) is a key under no known
|
|
209
|
+
// shape: `/v1/AbCd...32 chars/files`. A word, a slug or a version number is not caught (no digits or one case).
|
|
210
|
+
const TOKEN_SEGMENT = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z0-9_-]{24,}$|^(?=.*[a-z])(?=.*[A-Z])[A-Za-z0-9_-]{24,}$/;
|
|
211
|
+
function scrubUrl(u) {
|
|
212
|
+
const red = redactUrl(u).url;
|
|
213
|
+
const m = /^([a-z][a-z0-9+.-]*:\/\/[^/?#]*)([^?#]*)(.*)$/is.exec(red);
|
|
214
|
+
if (!m) return red;
|
|
215
|
+
return m[1] + m[2].split("/").map((seg) => (TOKEN_SEGMENT.test(seg) && !isPlaceholderValue(seg) ? REDACTED : seg)).join("/") + m[3];
|
|
216
|
+
}
|
|
217
|
+
const scrubUrls = (text) => text.replace(URL_IN_TEXT, scrubUrl);
|
|
218
|
+
|
|
219
|
+
function scrubString(s) {
|
|
220
|
+
let out = scrubUrls(s);
|
|
221
|
+
for (const r of SECRET_SHAPES) out = out.replace(new RegExp(r.pattern, r.flags.includes("g") ? r.flags : `${r.flags}g`), r.replacement);
|
|
222
|
+
return /^\s*[[{]/.test(out) ? redactBody(out, "application/json") : out;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function scrubValue(value, key, depth = 0) {
|
|
226
|
+
if (typeof value === "string") return key !== undefined && isCredentialField(key) && value !== "" && !isPlaceholderValue(value) ? REDACTED : scrubString(value);
|
|
227
|
+
if (value === null || typeof value !== "object") return value;
|
|
228
|
+
if (depth > 40) return REDACTED;
|
|
229
|
+
if (Array.isArray(value)) return value.map((v) => scrubValue(v, undefined, depth + 1));
|
|
230
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, isCredentialField(k) && typeof v === "string" && !isPlaceholderValue(v) && v !== "" ? REDACTED : scrubValue(v, k, depth + 1)]));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function withhold(value, key, depth = 0) {
|
|
234
|
+
if (typeof value === "string") return key !== undefined && STRUCTURE_KEYS.has(key) ? scrubString(value) : WITHHELD;
|
|
235
|
+
if (value === null || typeof value !== "object") return value;
|
|
236
|
+
if (depth > 40) return WITHHELD;
|
|
237
|
+
if (Array.isArray(value)) return value.map((v) => withhold(v, key, depth + 1)); // a list keeps its key's meaning (dropped: ["protected"])
|
|
238
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, withhold(v, k, depth + 1)]));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function redactAt(value, paths, path = [], depth = 0) {
|
|
242
|
+
if (paths.some((p) => pathMatches(p, path))) return REDACTED;
|
|
243
|
+
if (value === null || typeof value !== "object" || depth > 40) return value;
|
|
244
|
+
if (Array.isArray(value)) return value.map((v, i) => redactAt(v, paths, [...path, String(i)], depth + 1));
|
|
245
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, redactAt(v, paths, [...path, k], depth + 1)]));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The events as an export may carry them. Always: the shapes of keys and the values of credential-named fields are scrubbed (the one
|
|
250
|
+
* policy, src/security/redact.js). Then the pane's capture policy (src/model/retention.js): `summary` (or `dropText`) keeps the structure
|
|
251
|
+
* (states, heads, actions, kinds, counts) and withholds every text; `redact` paths (`**.content`) are hidden by path, on each event.
|
|
252
|
+
* Returns new objects; the live trace is never changed.
|
|
253
|
+
*/
|
|
254
|
+
export function redactEvents(events, policy) {
|
|
255
|
+
const p = cleanPolicy(policy);
|
|
256
|
+
const summary = p?.mode === "summary" || p?.dropText === true;
|
|
257
|
+
return events.map((e) => {
|
|
258
|
+
let out = scrubValue(structuredClone(e));
|
|
259
|
+
if (summary) out = withhold(out);
|
|
260
|
+
if (p?.redact?.length) out = redactAt(out, p.redact);
|
|
261
|
+
return out;
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** A trace as JSON Lines, redacted (a `policy` of undefined still scrubs keys). Refuses a trace longer than the limits. */
|
|
266
|
+
export function serializeTrace(events, { policy } = {}) {
|
|
267
|
+
if (events.length > MAX_TRACE_LINES) throw new Error(`The trace has ${events.length} events, more than a file may hold (${MAX_TRACE_LINES}).`);
|
|
268
|
+
return `${redactEvents(events, policy).map((e) => JSON.stringify(e)).join("\n")}\n`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// ── comments (LTM-UI-18, LTM-HUB-03) ───────────────────────────────────────────
|
|
272
|
+
|
|
273
|
+
/** The `comment` event for a comment record (`{ id, step, frame?, text, at }`). */
|
|
274
|
+
export const commentEvent = (c) => ({ type: "comment", id: String(c.id), step: c.step, ...(Number.isInteger(c.frame) ? { frame: c.frame } : {}), text: String(c.text), at: c.at });
|
|
275
|
+
const commentOrder = (a, b) => (a.frame ?? a.step) - (b.frame ?? b.step) || a.at - b.at || (a.id < b.id ? -1 : 1);
|
|
276
|
+
|
|
277
|
+
/** The comments in a trace's events, as records, in step order. */
|
|
278
|
+
export const commentsOf = (events) => (events ?? []).filter((e) => e?.type === "comment" && !eventProblem(e)).map(({ id, step, frame, text, at }) => ({ id, step, ...(Number.isInteger(frame) ? { frame } : {}), text, at })).sort(commentOrder);
|
|
279
|
+
|
|
280
|
+
/** `events` with the person's comments appended after the run, in step order. One already in the events (read from a file), or repeated, is kept once. */
|
|
281
|
+
export function withComments(events, comments) {
|
|
282
|
+
const have = new Set((events ?? []).filter((e) => e?.type === "comment").map((e) => e.id));
|
|
283
|
+
const fresh = [];
|
|
284
|
+
for (const c of [...(comments ?? [])].sort(commentOrder)) {
|
|
285
|
+
if (have.has(c.id)) continue;
|
|
286
|
+
have.add(c.id);
|
|
287
|
+
fresh.push(commentEvent(c));
|
|
288
|
+
}
|
|
289
|
+
return [...(events ?? []), ...fresh];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ── replay ────────────────────────────────────────────────────────────────────
|
|
293
|
+
|
|
294
|
+
const strip = (cell) => (cell ? { id: cell.id, kind: cell.kind, content: cell.content, meta: cell.meta, status: cell.status, protected: cell.protected } : null);
|
|
295
|
+
const same = (a, b) => canonical(a) === canonical(b);
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Replay a trace on its definition without asking anything: the run header seeds the tape (with the cells it injected), then each event is
|
|
299
|
+
* folded in order, a committed step by applying its recorded effective proposal with the machine's own apply function. Every step is
|
|
300
|
+
* checked against what the trace says it did (state, head, the cell before and after, the route): a trace that does not fit this
|
|
301
|
+
* definition stops the replay at that event with the reason, and the frames up to there stay. The model is never called; a recorded
|
|
302
|
+
* proposal is data.
|
|
303
|
+
* Returns `{ frames, problems, header, definitionMatches }`. Frame 0 is the tape before any step; a later frame follows each event that
|
|
304
|
+
* changes the run (a committed step, a person's edit, a restart): `{ index, event, eventIndex, label, state, head, step, tape, changed,
|
|
305
|
+
* notes, halt?, halted? }`. `notes` are the events that led to it (a rejection, an approval) and `halt` the halt event that ended the run
|
|
306
|
+
* there. `halted` is true when the run cannot go on from this frame (a terminal state or the model's own HALT).
|
|
307
|
+
*/
|
|
308
|
+
export function replayTrace(c, events) {
|
|
309
|
+
const problems = [];
|
|
310
|
+
const header = events?.[0];
|
|
311
|
+
if (!isObject(header) || header.type !== "run_meta") return { frames: [], problems: ["the trace has no run header"], header: null, definitionMatches: false };
|
|
312
|
+
const definitionMatches = header.definition?.hash === definitionHash(c);
|
|
313
|
+
let run;
|
|
314
|
+
try {
|
|
315
|
+
run = createRun(c, { inject: header.inject });
|
|
316
|
+
} catch (error) {
|
|
317
|
+
return { frames: [], problems: [`the header's injected cells do not fit this definition: ${error.message}`], header, definitionMatches };
|
|
318
|
+
}
|
|
319
|
+
const frameOf = (index, event, eventIndex, label, notes, changed = []) => ({ index, event, eventIndex, label, state: run.state, head: run.tape.head, step: run.step, tape: run.tape.cells.map(strip), changed, notes, halted: false });
|
|
320
|
+
const frames = [frameOf(0, null, 0, "start", [])];
|
|
321
|
+
let notes = [];
|
|
322
|
+
const stop = (i, why) => problems.push(`event ${i + 1}: ${why}`);
|
|
323
|
+
for (let i = 1; i < events.length; i++) {
|
|
324
|
+
const e = events[i];
|
|
325
|
+
if (eventProblem(e)) {
|
|
326
|
+
stop(i, eventProblem(e));
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
const last = frames[frames.length - 1];
|
|
330
|
+
if (e.type === "step_committed") {
|
|
331
|
+
if (e.before.state !== run.state || e.before.head !== run.tape.head) {
|
|
332
|
+
stop(i, `the step began in state ${e.before.state}, head ${e.before.head}, but the replay is in ${run.state}, head ${run.tape.head}`);
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
let tape = run.tape;
|
|
336
|
+
let cellAfter;
|
|
337
|
+
let cellBefore;
|
|
338
|
+
let next = e.after.state;
|
|
339
|
+
if (e.proposal.action?.kind === "HALT") {
|
|
340
|
+
cellBefore = cellAfter = tape.cells[tape.head];
|
|
341
|
+
run = { ...run, step: e.step };
|
|
342
|
+
} else {
|
|
343
|
+
const applied = applyAction(c, tape, e.proposal.action ?? {}, { step: run.step });
|
|
344
|
+
if (!applied.ok) {
|
|
345
|
+
stop(i, `the recorded action does not apply here: ${applied.reason}`);
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
const moved = resolveMove(c, applied.tape, e.proposal.move, { step: run.step });
|
|
349
|
+
if (!moved.ok) {
|
|
350
|
+
stop(i, `the recorded move does not apply here: ${moved.reason}`);
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
const route = routeFor(c, run.state, applied.cell);
|
|
354
|
+
if (route?.unanswered) {
|
|
355
|
+
stop(i, "a route could not be answered");
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
const expected = route ? route.to : e.proposal.nextState;
|
|
359
|
+
if (expected !== next || (route ? route.match : undefined) !== e.routedBy) {
|
|
360
|
+
stop(i, `the step went to ${next}${e.routedBy ? ` (routed by ${e.routedBy})` : ""}, but the definition sends it to ${expected}${route ? ` (routed by ${route.match})` : ""}`);
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
if (moved.tape.head !== e.after.head) {
|
|
364
|
+
stop(i, `the head ended at ${e.after.head}, the replay's at ${moved.tape.head}`);
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
cellBefore = applied.cellBefore;
|
|
368
|
+
cellAfter = applied.cellAfter;
|
|
369
|
+
tape = moved.tape;
|
|
370
|
+
run = { ...run, tape, step: e.step };
|
|
371
|
+
}
|
|
372
|
+
// A trace imported from llmtm may carry no cell snapshots (include_cell_diffs off): there is nothing to check the step against then.
|
|
373
|
+
const checkable = header.provider?.kind !== "imported" || (e.cellAfter !== null && e.cellBefore !== null);
|
|
374
|
+
if (checkable && (!same(strip(cellAfter), e.cellAfter) || !same(strip(cellBefore), e.cellBefore))) {
|
|
375
|
+
stop(i, "the cell after the step is not what the trace recorded");
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
const before = last.tape;
|
|
379
|
+
run = { ...run, state: next };
|
|
380
|
+
const changed = run.tape.cells.filter((x) => !before.some((y) => y.id === x.id && same(y, strip(x)))).map((x) => x.id);
|
|
381
|
+
const frame = frameOf(frames.length, e, i, `step ${e.step}: ${e.proposal.action?.kind}`, notes, changed);
|
|
382
|
+
frame.halted = c.states[next]?.terminal === true || e.proposal.action?.kind === "HALT";
|
|
383
|
+
frames.push(frame);
|
|
384
|
+
notes = [];
|
|
385
|
+
} else if (e.type === "person_edit") {
|
|
386
|
+
const at = run.tape.cells.findIndex((x) => x.id === e.cell);
|
|
387
|
+
if (at < 0) {
|
|
388
|
+
stop(i, `a person edited cell ${e.cell}, which is not on the replayed tape`);
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
const cell = { ...run.tape.cells[at], ...(e.kind !== undefined ? { kind: e.kind } : {}), ...(e.content !== undefined ? { content: e.content } : {}) };
|
|
392
|
+
run = { ...run, tape: { ...run.tape, cells: run.tape.cells.map((x, k) => (k === at ? cell : x)) } };
|
|
393
|
+
frames.push(frameOf(frames.length, e, i, `edit of ${e.cell} (you)`, notes, [e.cell]));
|
|
394
|
+
notes = [];
|
|
395
|
+
} else if (e.type === "restart") {
|
|
396
|
+
run = createRun(c, { inject: header.inject });
|
|
397
|
+
frames.push(frameOf(frames.length, e, i, `restart ${e.n}`, notes, run.tape.cells.map((x) => x.id)));
|
|
398
|
+
notes = [];
|
|
399
|
+
} else if (e.type === "halt") {
|
|
400
|
+
last.halt = { kind: e.kind, reason: e.reason };
|
|
401
|
+
} else if (e.type === "comment") {
|
|
402
|
+
// a person's note: it changes nothing about the run, so it is neither a frame nor a note (the scrubber shows it, src/ui/machine-replay.js)
|
|
403
|
+
} else if (e.type !== "run_meta") notes.push(e); // a rejection, an approval, a resume, a fork, or a type this reader does not know
|
|
404
|
+
}
|
|
405
|
+
return { frames, problems, header, definitionMatches };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** The pane's value (the same JSON `runValue` makes) for a frame, so a replayed tape reads like a live one. */
|
|
409
|
+
export const frameValue = (c, frame) => runValue(c, { state: frame.state, tape: { cells: frame.tape, head: frame.head }, step: frame.step, halted: Boolean(frame.halt) || frame.halted, ...(frame.halt ? { haltKind: frame.halt.kind, haltReason: frame.halt.reason } : {}) });
|
|
410
|
+
|
|
411
|
+
// ── inspect ──────────────────────────────────────────────────────────────────
|
|
412
|
+
|
|
413
|
+
/** A run in numbers (LTM-TRACE-11): steps, rejections, approvals, edits, resumes, restarts, calls, tokens, cost, how it ended. */
|
|
414
|
+
export function inspectTrace(events) {
|
|
415
|
+
const count = (type) => events.filter((e) => e.type === type).length;
|
|
416
|
+
const steps = events.filter((e) => e.type === "step_committed");
|
|
417
|
+
const halts = events.filter((e) => e.type === "halt");
|
|
418
|
+
const decided = events.filter((e) => e.type === "approval_decided");
|
|
419
|
+
const header = events[0]?.type === "run_meta" ? events[0] : null;
|
|
420
|
+
const last = halts.at(-1);
|
|
421
|
+
return {
|
|
422
|
+
run: header?.run,
|
|
423
|
+
machine: header?.machine,
|
|
424
|
+
definition: header?.definition?.hash,
|
|
425
|
+
...(header?.parent ? { parent: header.parent } : {}),
|
|
426
|
+
steps: steps.length,
|
|
427
|
+
rejections: count("proposal_rejected"),
|
|
428
|
+
approvals: decided.length,
|
|
429
|
+
amended: decided.filter((e) => e.decision === "amended").length,
|
|
430
|
+
denied: decided.filter((e) => e.decision === "denied").length,
|
|
431
|
+
personEdits: count("person_edit"),
|
|
432
|
+
resumes: count("resume"),
|
|
433
|
+
restarts: count("restart"),
|
|
434
|
+
modelCalls: steps.reduce((n, e) => n + (e.attempts || 0), 0),
|
|
435
|
+
inputTokens: steps.reduce((n, e) => n + (Number(e.usage?.input) || 0), 0),
|
|
436
|
+
outputTokens: steps.reduce((n, e) => n + (Number(e.usage?.output) || 0), 0),
|
|
437
|
+
cost: Math.round(steps.reduce((n, e) => n + (Number(e.cost) || 0), 0) * 1e9) / 1e9,
|
|
438
|
+
ms: steps.reduce((n, e) => n + (Number(e.usage?.ms) || 0), 0),
|
|
439
|
+
models: steps.reduce((m, e) => (typeof e.usage?.model === "string" && e.usage.model ? { ...m, [e.usage.model]: (m[e.usage.model] || 0) + 1 } : m), {}),
|
|
440
|
+
ended: last ? { kind: last.kind, reason: last.reason } : null,
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Each committed step's usage (LTM-PROV-09): `{ step, input, output, cost, ms?, model? }`, in order. */
|
|
445
|
+
export function stepUsage(events) {
|
|
446
|
+
return events
|
|
447
|
+
.filter((e) => e.type === "step_committed")
|
|
448
|
+
.map((e) => ({ step: e.step, input: Number(e.usage?.input) || 0, output: Number(e.usage?.output) || 0, ...(Number.isFinite(e.usage?.ms) ? { ms: e.usage.ms } : {}), ...(typeof e.usage?.model === "string" && e.usage.model ? { model: e.usage.model } : {}), cost: Number(e.cost) || 0 }));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// ── fork and resume ──────────────────────────────────────────────────────────
|
|
452
|
+
|
|
453
|
+
/** Why the run can't be forked at frame `at` of `replay`, or "". A frame that ended the run (a terminal state, the model's HALT) can't. */
|
|
454
|
+
export function forkProblem(replay, at) {
|
|
455
|
+
if (replay.problems.length) return `The trace doesn't replay on this definition (${replay.problems[0]}), so it can't be forked.`;
|
|
456
|
+
if (!replay.definitionMatches) return "The trace was recorded on a different definition than this pane's (the hashes differ), so it can't be continued here.";
|
|
457
|
+
if (!Number.isInteger(at) || at < 0 || at >= replay.frames.length) return `There is no frame ${Number.isInteger(at) ? at : "?"} in this trace (0 to ${replay.frames.length - 1}).`;
|
|
458
|
+
if (replay.frames[at].halted) return "The run ended at this step (a terminal state or the model's HALT): a terminal step can't be forked from.";
|
|
459
|
+
return "";
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const callsOf = (events) => {
|
|
463
|
+
let n = 0;
|
|
464
|
+
for (const e of events) {
|
|
465
|
+
if (e.type === "restart") n = 0;
|
|
466
|
+
else if (e.type === "step_committed") n += e.attempts || 0;
|
|
467
|
+
}
|
|
468
|
+
return n;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Continue a recorded run from frame `at` (0 is the start): the fork, or with `resume` a run that stopped for a reason a person has fixed.
|
|
473
|
+
* `edit`: `{ cell: id, content?, kind? }` changes one cell first (a person's edit, recorded as one), `proposal`: the proposal the next step
|
|
474
|
+
* takes instead of asking the model (checked like any other). Returns what a driver needs to carry on:
|
|
475
|
+
* `{ run, events, usage, header: { parent }, nextProposal?, replyOffset }`; `events` is the lineage to carry (the source's events
|
|
476
|
+
* up to the frame, then a `fork` or `resume` event and the edit). Throws an Error saying why when it can't be done.
|
|
477
|
+
*/
|
|
478
|
+
export function continueFrom(c, events, at, { edit, proposal, resume = false, why, run: runId } = {}) {
|
|
479
|
+
const replay = replayTrace(c, events);
|
|
480
|
+
const problem = forkProblem(replay, at);
|
|
481
|
+
if (problem) throw new Error(resume && replay.frames[at]?.halted ? "This run ended in a terminal state or with the model's HALT: there is nothing to resume." : problem);
|
|
482
|
+
const frame = replay.frames[at];
|
|
483
|
+
const header = replay.header;
|
|
484
|
+
const prefix = events.slice(1, frame.eventIndex + 1).filter((e) => e.type !== "comment"); // a comment belongs to the run it was made on
|
|
485
|
+
let tape = { cells: frame.tape.map((x) => ({ ...x, meta: structuredClone(x.meta) })), head: frame.head };
|
|
486
|
+
const out = [...prefix, resume ? { type: "resume", step: frame.step, why: why || "continued by a person", from: header.run ?? "", at } : { type: "fork", at, step: frame.step, from: header.run ?? "", ...(edit || proposal ? { edited: [...(edit ? [`cell:${edit.cell}`] : []), ...(proposal ? ["proposal"] : [])] } : {}) }];
|
|
487
|
+
if (edit) {
|
|
488
|
+
const i = tape.cells.findIndex((x) => x.id === edit.cell);
|
|
489
|
+
if (i < 0) throw new Error(`There is no cell ${JSON.stringify(edit.cell)} on the tape at this step.`);
|
|
490
|
+
const cell = { ...tape.cells[i] };
|
|
491
|
+
if (edit.kind !== undefined) {
|
|
492
|
+
if (!c.kinds.includes(edit.kind)) throw new Error(`${edit.kind} is not one of the machine's cell kinds.`);
|
|
493
|
+
cell.kind = edit.kind;
|
|
494
|
+
}
|
|
495
|
+
if (edit.content !== undefined) {
|
|
496
|
+
const bad = contentProblem(c, String(edit.content));
|
|
497
|
+
if (bad) throw new Error(`The edit isn't valid: ${bad}.`);
|
|
498
|
+
cell.content = String(edit.content);
|
|
499
|
+
}
|
|
500
|
+
tape = { ...tape, cells: tape.cells.map((x, k) => (k === i ? cell : x)) };
|
|
501
|
+
out.push({ type: "person_edit", step: frame.step, cell: edit.cell, ...(edit.kind !== undefined ? { kind: edit.kind } : {}), ...(edit.content !== undefined ? { content: String(edit.content) } : {}) });
|
|
502
|
+
}
|
|
503
|
+
if (proposal !== undefined && !isObject(proposal)) throw new Error("The next proposal is an object: { action, move, nextState }.");
|
|
504
|
+
if (proposal !== undefined && (c.states[frame.state]?.tool || c.states[frame.state]?.submachine || c.states[frame.state]?.elicit)) throw new Error(`The next step is a ${c.states[frame.state].tool ? "tool" : "submachine"} state (${frame.state}): it takes no proposal. Fork it with an edited cell instead.`);
|
|
505
|
+
const carried = prefix;
|
|
506
|
+
const used = callsOf(carried);
|
|
507
|
+
const next = events.slice(frame.eventIndex + 1).find((e) => e.type === "step_committed");
|
|
508
|
+
const spent = carried.filter((e) => e.type === "step_committed");
|
|
509
|
+
return {
|
|
510
|
+
run: { state: frame.state, tape, step: frame.step, halted: false },
|
|
511
|
+
events: out,
|
|
512
|
+
usage: { inputTokens: spent.reduce((n, e) => n + (Number(e.usage?.input) || 0), 0), outputTokens: spent.reduce((n, e) => n + (Number(e.usage?.output) || 0), 0), cost: Math.round(spent.reduce((n, e) => n + (Number(e.cost) || 0), 0) * 1e9) / 1e9 },
|
|
513
|
+
parent: { run: header.run ?? "", at, step: frame.step, ...(resume ? { resumed: true } : {}), ...(edit || proposal ? { edited: [...(edit ? [`cell:${edit.cell}`] : []), ...(proposal ? ["proposal"] : [])] } : {}) },
|
|
514
|
+
inject: header.inject,
|
|
515
|
+
...(proposal !== undefined ? { nextProposal: proposal } : {}),
|
|
516
|
+
// A recording answers in order: the fork goes on from the reply the source used next, and a replaced reply is spent.
|
|
517
|
+
replyOffset: used + (proposal !== undefined ? (next?.attempts || 1) : 0),
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// ── diff ─────────────────────────────────────────────────────────────────────
|
|
522
|
+
|
|
523
|
+
const stepSignature = (e) => canonical([e.before, e.proposal?.action, e.proposal?.move, e.after, e.cellAfter?.content, e.cellAfter?.kind, e.cellAfter?.status, e.routedBy ?? null]);
|
|
524
|
+
const stepLine = (e) => (e ? `step ${e.step}: ${e.proposal?.action?.kind}${e.proposal?.action?.text !== undefined ? ` ${JSON.stringify(e.proposal.action.text).slice(0, 60)}` : ""} → ${e.after.state}, head ${e.after.head}` : undefined);
|
|
525
|
+
const whyDiffers = (a, b) => {
|
|
526
|
+
if (!a) return "the second run took a step the first didn't";
|
|
527
|
+
if (!b) return "the first run took a step the second didn't";
|
|
528
|
+
if (a.before.state !== b.before.state || a.before.head !== b.before.head) return "the runs were in different places";
|
|
529
|
+
if (!same(a.proposal?.action, b.proposal?.action)) return "a different action";
|
|
530
|
+
if (a.proposal?.move !== b.proposal?.move) return "a different head move";
|
|
531
|
+
if (a.after.state !== b.after.state) return "a different next state";
|
|
532
|
+
return "the cell ended up different";
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
/** The tape of one frame against another, by cell id: `[{ id, kind: added | removed | changed, field?, before?, after? }]`. */
|
|
536
|
+
export function diffTapes(a, b) {
|
|
537
|
+
const out = [];
|
|
538
|
+
const mb = new Map(b.map((x) => [x.id, x]));
|
|
539
|
+
const ma = new Map(a.map((x) => [x.id, x]));
|
|
540
|
+
for (const x of a) {
|
|
541
|
+
const y = mb.get(x.id);
|
|
542
|
+
if (!y) out.push({ id: x.id, kind: "removed", before: x.content });
|
|
543
|
+
else for (const f of ["kind", "content", "status", "protected"]) if (x[f] !== y[f]) out.push({ id: x.id, kind: "changed", field: f, before: x[f], after: y[f] });
|
|
544
|
+
if (y && !same(x.meta, y.meta)) out.push({ id: x.id, kind: "changed", field: "meta", before: x.meta, after: y.meta });
|
|
545
|
+
}
|
|
546
|
+
for (const y of b) if (!ma.has(y.id)) out.push({ id: y.id, kind: "added", after: y.content });
|
|
547
|
+
return out;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Compare two runs of the same machine (LTM-TRACE-09), `a` the earlier or parent and `b` the other, each `{ events }`. By step: where the
|
|
552
|
+
* committed steps first differ and why; a table of what each did and cost; whether the definitions differ (a note, not an error). By tape:
|
|
553
|
+
* the tape at the first divergence and at the end, cell by cell. `{ identical, definitions: { same, a, b }, steps: { a, b }, shared,
|
|
554
|
+
* firstDivergent?, table, tape: { atDivergence?, final }, final: { a, b } }`.
|
|
555
|
+
*/
|
|
556
|
+
export function diffRuns(c, a, b) {
|
|
557
|
+
const ra = replayTrace(c, a.events);
|
|
558
|
+
const rb = replayTrace(c, b.events);
|
|
559
|
+
const sa = a.events.filter((e) => e.type === "step_committed");
|
|
560
|
+
const sb = b.events.filter((e) => e.type === "step_committed");
|
|
561
|
+
const { shared, firstDivergent } = diffSequences(sa, sb, { signature: stepSignature, why: whyDiffers, line: stepLine });
|
|
562
|
+
const ia = inspectTrace(a.events);
|
|
563
|
+
const ib = inspectTrace(b.events);
|
|
564
|
+
const table = metricTable([
|
|
565
|
+
["steps", ia.steps, ib.steps],
|
|
566
|
+
["model calls", ia.modelCalls, ib.modelCalls],
|
|
567
|
+
["rejections", ia.rejections, ib.rejections],
|
|
568
|
+
["approvals", ia.approvals, ib.approvals],
|
|
569
|
+
["answers edited", ia.amended, ib.amended],
|
|
570
|
+
["person edits", ia.personEdits, ib.personEdits],
|
|
571
|
+
["restarts", ia.restarts, ib.restarts],
|
|
572
|
+
["input tokens", ia.inputTokens, ib.inputTokens],
|
|
573
|
+
["output tokens", ia.outputTokens, ib.outputTokens],
|
|
574
|
+
["cost (USD)", ia.cost, ib.cost],
|
|
575
|
+
["ended", ia.ended?.kind ?? "not yet", ib.ended?.kind ?? "not yet"],
|
|
576
|
+
]);
|
|
577
|
+
const last = (r) => r.frames[r.frames.length - 1];
|
|
578
|
+
// The frame after the same number of committed steps, to show the tape where the runs part.
|
|
579
|
+
const at = (r, n) => r.frames.find((f) => f.event?.type === "step_committed" && f.step === n) ?? r.frames[0];
|
|
580
|
+
const divergedAt = firstDivergent ? firstDivergent.step : undefined;
|
|
581
|
+
const fa = last(ra);
|
|
582
|
+
const fb = last(rb);
|
|
583
|
+
const finalTape = fa && fb ? diffTapes(fa.tape, fb.tape) : [];
|
|
584
|
+
const haltOf = (f, events) => events.filter((e) => e.type === "halt").at(-1) ?? (f?.halt ? { kind: f.halt.kind } : null);
|
|
585
|
+
const sameDefinition = a.events[0]?.definition?.hash === b.events[0]?.definition?.hash;
|
|
586
|
+
const finalState = (f) => (f ? { state: f.state, step: f.step, head: f.head } : null);
|
|
587
|
+
return {
|
|
588
|
+
identical: !firstDivergent && !finalTape.length && same(finalState(fa), finalState(fb)),
|
|
589
|
+
definitions: { same: sameDefinition, a: a.events[0]?.definition?.hash, b: b.events[0]?.definition?.hash },
|
|
590
|
+
steps: { a: sa.length, b: sb.length },
|
|
591
|
+
shared,
|
|
592
|
+
...(firstDivergent ? { firstDivergent } : {}),
|
|
593
|
+
table,
|
|
594
|
+
tape: {
|
|
595
|
+
...(divergedAt && ra.frames.length && rb.frames.length ? { atDivergence: diffTapes(at(ra, divergedAt).tape, at(rb, divergedAt).tape), step: divergedAt } : {}),
|
|
596
|
+
final: finalTape,
|
|
597
|
+
},
|
|
598
|
+
final: { a: { ...finalState(fa), halt: haltOf(fa, a.events) }, b: { ...finalState(fb), halt: haltOf(fb, b.events) } },
|
|
599
|
+
problems: [...ra.problems.map((p) => `first run: ${p}`), ...rb.problems.map((p) => `second run: ${p}`)],
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
// ── per-cell history and the run forest (LTM-TRACE-10, LTM-TRACE-08) ─────────────────────────────────────────────────────────────────────────
|
|
605
|
+
|
|
606
|
+
const sameCell = (a, b) => a && b && a.kind === b.kind && a.status === b.status && a.content === b.content && JSON.stringify(a.meta) === JSON.stringify(b.meta);
|
|
607
|
+
const cellChanges = (a, b) => {
|
|
608
|
+
if (!a || !b) return [];
|
|
609
|
+
const out = [];
|
|
610
|
+
if (a.content !== b.content) out.push(`content ${JSON.stringify(a.content)} \u2192 ${JSON.stringify(b.content)}`);
|
|
611
|
+
if (a.kind !== b.kind) out.push(`kind ${a.kind} \u2192 ${b.kind}`);
|
|
612
|
+
if (a.status !== b.status) out.push(`status ${a.status} \u2192 ${b.status}`);
|
|
613
|
+
if (JSON.stringify(a.meta) !== JSON.stringify(b.meta)) out.push("meta changed");
|
|
614
|
+
return out;
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Which committed steps touched each cell, and how it changed: `{ cellId: [{ frame, step, label, before, after, changes }] }` from the frames of a
|
|
619
|
+
* replay (`replayTrace(...).frames`). `before` is null where the step added the cell and `after` where it removed it. A cell nothing touched is
|
|
620
|
+
* not a key. The frames carry the tape of every step, so this reads the trace and asks no one.
|
|
621
|
+
*/
|
|
622
|
+
export function cellHistory(frames) {
|
|
623
|
+
const out = {};
|
|
624
|
+
for (let i = 1; i < (frames?.length ?? 0); i++) {
|
|
625
|
+
const [prev, cur] = [frames[i - 1], frames[i]];
|
|
626
|
+
const [was, now] = [new Map(prev.tape.map((x) => [x.id, x])), new Map(cur.tape.map((x) => [x.id, x]))];
|
|
627
|
+
for (const id of new Set([...was.keys(), ...now.keys()])) {
|
|
628
|
+
const [a, b] = [was.get(id) ?? null, now.get(id) ?? null];
|
|
629
|
+
if (a && b && sameCell(a, b)) continue;
|
|
630
|
+
(out[id] ??= []).push({ frame: i, step: cur.step, label: cur.label, before: a, after: b, changes: cellChanges(a, b) });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return out;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** How many committed steps touched each cell, from the events of a run (the badge on a cell): `{ cellId: n }`. A step that changed nothing is not counted. */
|
|
637
|
+
export function touchCounts(events) {
|
|
638
|
+
const out = {};
|
|
639
|
+
for (const e of events ?? []) {
|
|
640
|
+
if (e?.type !== "step_committed" || !e.cellBefore || !e.cellAfter || e.proposal?.action?.kind === "HALT") continue;
|
|
641
|
+
if (e.cellBefore.id === e.cellAfter.id && sameCell(e.cellBefore, e.cellAfter)) continue;
|
|
642
|
+
out[e.cellAfter.id] = (out[e.cellAfter.id] ?? 0) + 1;
|
|
643
|
+
if (e.cellBefore.id !== e.cellAfter.id) out[e.cellBefore.id] = (out[e.cellBefore.id] ?? 0) + 1;
|
|
644
|
+
}
|
|
645
|
+
return out;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/** "forked at step 2 (edited)" / "resumed at step 2": what a run's parent link says. */
|
|
649
|
+
export const forkWords = (parent) => `${parent.resumed ? "resumed" : "forked"} at step ${parent.step ?? parent.at}${parent.edited?.length ? " (edited)" : ""}`;
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* The runs of a machine as a forest: each run that forked or resumed another under it, in the order the runs were made. `runs`: `{ id, parent?: { run, at,
|
|
653
|
+
* step, resumed?, edited? }, steps, halted, haltKind?, haltReason? }`. A run whose parent is not in the list is a root marked `orphan` (the source left the log).
|
|
654
|
+
*/
|
|
655
|
+
export function machineForest(runs) {
|
|
656
|
+
const nodes = new Map((runs ?? []).map((run) => [run.id, { run, children: [] }]));
|
|
657
|
+
const roots = [];
|
|
658
|
+
for (const node of nodes.values()) {
|
|
659
|
+
const parent = node.run.parent ? nodes.get(node.run.parent.run) : undefined;
|
|
660
|
+
if (parent && parent !== node) parent.children.push(node);
|
|
661
|
+
else roots.push(node.run.parent ? Object.assign(node, { orphan: true }) : node);
|
|
662
|
+
}
|
|
663
|
+
return roots;
|
|
664
|
+
}
|