@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,1126 @@
|
|
|
1
|
+
// Repeaters: "for each" on a subgraph pane. The pane's graph (or, per
|
|
2
|
+
// item, a library or package pane: `templateBy`) is a template, and each item
|
|
3
|
+
// of an array input gets its own live instance of it: an inner canvas with its
|
|
4
|
+
// own state panes, timers and run history, namespaced `<pane>/@<key>/` in the
|
|
5
|
+
// sandbox. It is Noodl's Repeater without its flaws:
|
|
6
|
+
//
|
|
7
|
+
// - keyed by `each.key` (the rule for-each and keyed setters use,
|
|
8
|
+
// src/runtime/keys.js), so a reorder moves instances and never recreates
|
|
9
|
+
// one; added keys create an instance, removed keys dispose theirs, kept
|
|
10
|
+
// keys stay alive;
|
|
11
|
+
// - an instance reruns only when its own item changed (per-item port
|
|
12
|
+
// stamps: a content hash of the item, or the keyed-setter hint);
|
|
13
|
+
// - items without a key, or with a duplicate one, fall back to positions,
|
|
14
|
+
// with a note (never random ids);
|
|
15
|
+
// - the template is picked by a field (`templateBy`), never by code.
|
|
16
|
+
//
|
|
17
|
+
// A subgraph pane's `each` (saved with the canvas):
|
|
18
|
+
// { on: true,
|
|
19
|
+
// input: "" the input to repeat over ("" the first array input),
|
|
20
|
+
// key: "id" what names an item (a field, path or key expression;
|
|
21
|
+
// "" by position),
|
|
22
|
+
// max: 0 at most this many instances (0: all),
|
|
23
|
+
// batch: { size, delayMs } create new instances this many at a time,
|
|
24
|
+
// each batch settled before the next, delayMs between,
|
|
25
|
+
// templateBy: "kind" a field or dotted path of the item naming its template,
|
|
26
|
+
// templates: { <name>: { name, ref, graph } } a library or package pane
|
|
27
|
+
// per name (a linked subgraph's ref and cached graph);
|
|
28
|
+
// any other value uses the pane's own graph,
|
|
29
|
+
// removal: "drop" | { keepMinutes } what a removed key's instance does
|
|
30
|
+
// after its grace period: go (its onCleanup runs), or
|
|
31
|
+
// wait, paused, that long for the key to come back,
|
|
32
|
+
// graceMs: 0 how long a removed instance keeps running (its
|
|
33
|
+
// `removing` port is true) before that,
|
|
34
|
+
// collect: "array" | "keyed" the pane's value: the instances' values in
|
|
35
|
+
// item order, or an object by key,
|
|
36
|
+
// expose: [{ name, pane, output? }] } a pane of the template read from
|
|
37
|
+
// outside (below).
|
|
38
|
+
//
|
|
39
|
+
// Ports inside an instance (input panes of its template, by name): the
|
|
40
|
+
// repeated input's name gets the instance's item; another wired input gets
|
|
41
|
+
// that input's whole value (every instance reruns when it changes); an
|
|
42
|
+
// unwired one its parameter (`params`) or its own widget. Unwired input panes
|
|
43
|
+
// named `key`, `index` and `removing` get the item's key, its position (it
|
|
44
|
+
// changes when items move, so reading it reruns a moved instance) and whether
|
|
45
|
+
// the instance is on its way out.
|
|
46
|
+
//
|
|
47
|
+
// Item events: every event an instance's panes send on their event
|
|
48
|
+
// outputs (emit.event in any pane, a value keyed by one, a timer's tick, an
|
|
49
|
+
// html submit) comes out of the repeater pane's own event output
|
|
50
|
+
// `item` as { key, name, payload, index, pane, removing?, value? } (`value`:
|
|
51
|
+
// the instance's value, when it has one). Each is queued in order and never
|
|
52
|
+
// coalesced: two instances firing in one frame are two events (Noodl's Item
|
|
53
|
+
// Signals keep one slot and lose the first). They pass the transport inside
|
|
54
|
+
// the instance, then the repeater pane's budget and its readers' queue limits.
|
|
55
|
+
// An instance in its grace period still sends; a parked one (kept) doesn't.
|
|
56
|
+
// They go out only while something reads `item`.
|
|
57
|
+
//
|
|
58
|
+
// Exposed panes: `each.expose` names a pane of the template (by name,
|
|
59
|
+
// as ports are matched, so it holds across templates and template updates; a
|
|
60
|
+
// pane inside a subgraph pane of the template by the names down to it,
|
|
61
|
+
// `sub/name`, `output` picks one of its named outputs) and gives the repeater an
|
|
62
|
+
// output of its own, `name`, whose value is an object by item key (keyName) of
|
|
63
|
+
// that pane's value in each live instance. Panes outside read it with a wire
|
|
64
|
+
// to that output: `{ source: <repeater>, output: name }`. Instances without a
|
|
65
|
+
// pane of that name, or where it skipped, are left out; when no template has
|
|
66
|
+
// one the output skips, saying so. A pane inside an instance is otherwise
|
|
67
|
+
// never an outer pane's input: locateInner skips repeaters.
|
|
68
|
+
//
|
|
69
|
+
// Pure model code: no DOM. It runs under node --test and in `mallory run`.
|
|
70
|
+
|
|
71
|
+
import { graphPanes, outputIds, namespacedRuntime, bindPorts, exposedPath, SEP } from "./subgraph.js";
|
|
72
|
+
import { portId, cleanExpose, outputNameProblem, outputsOf, ERROR_OUTPUT } from "./ports.js";
|
|
73
|
+
import { planBatches } from "./foreach.js";
|
|
74
|
+
import { skip } from "@johnhenry/dataflow";
|
|
75
|
+
import { linkHash } from "./links.js";
|
|
76
|
+
import { showKey, approxBytes } from "../runtime/keys.js";
|
|
77
|
+
import { typesWhere, byType, inDataflow } from "../panes/registry.js";
|
|
78
|
+
import { bindContexts } from "./context.js";
|
|
79
|
+
import { RunLog, canvasFacts, retentionOf, configPolicy, MAX_INSTANCES } from "./runlog.js";
|
|
80
|
+
import { LazyMap } from "./lazy-map.js";
|
|
81
|
+
import { keepRemovedHolds } from "./wait-clock.js";
|
|
82
|
+
import { MAX_BATCH_KEY } from "./batches.js";
|
|
83
|
+
|
|
84
|
+
/** Input panes inside a template that the repeater fills itself (when nothing is wired to them). */
|
|
85
|
+
export const LIFECYCLE_PORTS = ["key", "index", "removing"];
|
|
86
|
+
export const MAX_GRACE_MS = 60000;
|
|
87
|
+
export const MAX_KEEP_MINUTES = 24 * 60;
|
|
88
|
+
export const COLLECT = ["array", "keyed"];
|
|
89
|
+
/** The repeater pane's event output for its instances' events (the subgraph record's `events`). */
|
|
90
|
+
export const ITEM_EVENTS = "item";
|
|
91
|
+
|
|
92
|
+
const DEFAULTS = byType((r) => r.defaults);
|
|
93
|
+
|
|
94
|
+
/** Panes whose errors a repeater doesn't surface: they need to be on screen to run (as in a subgraph). */
|
|
95
|
+
const NEEDS_SCREEN = new Set(typesWhere((r) => r.needsScreen));
|
|
96
|
+
|
|
97
|
+
// Per-item port stamps (on by default). Off, every instance's item port reruns whenever the list
|
|
98
|
+
// changes, as a subgraph's ports do (bindPorts stamps a whole input): for measuring (test/repeater.test.mjs, "measured").
|
|
99
|
+
let perItemStamps = true;
|
|
100
|
+
export function setPerItemStamps(on) {
|
|
101
|
+
perItemStamps = Boolean(on);
|
|
102
|
+
return perItemStamps;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Is this pane a repeater (a subgraph pane with for each on)? */
|
|
106
|
+
export const repeatOn = (pane) => pane?.type === "subgraph" && Boolean(pane.each?.on);
|
|
107
|
+
|
|
108
|
+
const whole = (v, lo, hi) => Math.min(hi, Math.max(lo, Math.floor(Number(v) || 0)));
|
|
109
|
+
|
|
110
|
+
/** A subgraph pane's `each` with defaults filled in (`key`, `batch`, `templateBy`, `templates` only when set). */
|
|
111
|
+
export function normalizeRepeat(each) {
|
|
112
|
+
const e = each && typeof each === "object" ? each : {};
|
|
113
|
+
const out = { on: Boolean(e.on), input: typeof e.input === "string" ? e.input : "", max: whole(e.max, 0, 1e9) };
|
|
114
|
+
const key = typeof e.key === "string" ? e.key.trim() : "";
|
|
115
|
+
if (key) out.key = key;
|
|
116
|
+
const size = whole(e.batch?.size, 0, 1e9);
|
|
117
|
+
if (size) out.batch = { size, delayMs: whole(e.batch?.delayMs, 0, 600000) };
|
|
118
|
+
const by = typeof e.templateBy === "string" ? e.templateBy.trim() : "";
|
|
119
|
+
if (by) out.templateBy = by;
|
|
120
|
+
if (e.templates && typeof e.templates === "object" && !Array.isArray(e.templates)) {
|
|
121
|
+
const templates = Object.fromEntries(Object.entries(e.templates).filter(([, t]) => t && typeof t === "object" && Array.isArray(t.graph?.panes)));
|
|
122
|
+
if (Object.keys(templates).length) out.templates = templates;
|
|
123
|
+
}
|
|
124
|
+
const keep = e.removal && typeof e.removal === "object" ? Number(e.removal.keepMinutes) : 0;
|
|
125
|
+
out.removal = keep > 0 ? { keepMinutes: Math.min(MAX_KEEP_MINUTES, keep) } : "drop";
|
|
126
|
+
out.graceMs = whole(e.graceMs, 0, MAX_GRACE_MS);
|
|
127
|
+
out.collect = e.collect === "keyed" ? "keyed" : "array";
|
|
128
|
+
const expose = cleanExpose(e.expose);
|
|
129
|
+
if (expose) out.expose = expose;
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Why a repeater setting is malformed, or null (the WebMCP tools refuse it with this). */
|
|
134
|
+
export function repeatProblem(each) {
|
|
135
|
+
if (!each || typeof each !== "object" || Array.isArray(each)) return "each is an object";
|
|
136
|
+
if (each.input === "*") return 'a repeater repeats over one input: "" (the first array) or its name, not "*"';
|
|
137
|
+
if (each.templateBy !== undefined && each.templateBy !== "" && !/^(?:[A-Za-z_$][\w$]*|\d+)(?:\.(?:[A-Za-z_$][\w$]*|\d+))*$/.test(String(each.templateBy))) return `templateBy is a field name or dotted path, like "kind" or "meta.kind" (got ${JSON.stringify(each.templateBy)})`;
|
|
138
|
+
if (each.collect !== undefined && !COLLECT.includes(each.collect)) return `collect is "array" or "keyed" (got ${JSON.stringify(each.collect)})`;
|
|
139
|
+
const r = each.removal;
|
|
140
|
+
if (r !== undefined && r !== "drop" && !(r && typeof r === "object" && Number(r.keepMinutes) > 0)) return 'removal is "drop" or { keepMinutes: N } (N > 0)';
|
|
141
|
+
return exposeProblem(each.expose);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Why a repeater's `expose` is malformed, or null: [{ name, pane, output?, description? }], each name an output name of its own. */
|
|
145
|
+
export function exposeProblem(expose) {
|
|
146
|
+
if (expose === undefined) return null;
|
|
147
|
+
if (!Array.isArray(expose)) return 'expose is a list of { name, pane, output? }: name the output, pane the pane inside the template (by name)';
|
|
148
|
+
const seen = new Set();
|
|
149
|
+
for (const e of expose) {
|
|
150
|
+
if (!e || typeof e !== "object" || Array.isArray(e)) return "expose: each entry is { name, pane, output? }";
|
|
151
|
+
const problem = outputNameProblem(e.name);
|
|
152
|
+
if (problem) return `expose: ${problem}`;
|
|
153
|
+
if ([ITEM_EVENTS, ERROR_OUTPUT].includes(e.name)) return `expose: "${e.name}" is the repeater's own ${e.name === ITEM_EVENTS ? "item events" : "error"} output; pick another name`;
|
|
154
|
+
if (seen.has(e.name)) return `expose: two entries are named "${e.name}"`;
|
|
155
|
+
seen.add(e.name);
|
|
156
|
+
if (typeof e.pane !== "string" || !e.pane.trim()) return `expose "${e.name}": pane is the name of a pane inside the template`;
|
|
157
|
+
if (e.output !== undefined && (typeof e.output !== "string" || outputNameProblem(e.output))) return `expose "${e.name}": output is one of that pane's named outputs`;
|
|
158
|
+
if (e.description !== undefined && typeof e.description !== "string") return `expose "${e.name}": a description is text`;
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ── keys and namespaces ──────────────────────────────────────────────────
|
|
164
|
+
|
|
165
|
+
/** FNV-1a, base 36: a short, stable hash for a key that can't be a namespace as it is. */
|
|
166
|
+
function shortHash(text) {
|
|
167
|
+
let h = 0x811c9dc5;
|
|
168
|
+
for (let i = 0; i < text.length; i++) h = Math.imul(h ^ text.charCodeAt(i), 16777619);
|
|
169
|
+
return (h >>> 0).toString(36);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The namespace segment of an instance: `@` and its key when the key is
|
|
174
|
+
* plain (letters, digits, `_`, `-`; a whole number for a number key), else a
|
|
175
|
+
* readable part and a hash of the key (`@buy_milk.h1x2y3`); by position:
|
|
176
|
+
* `@.3`. `@` never starts a pane id, so `<pane>/@<key>/<inner id>` can't be
|
|
177
|
+
* mistaken for a subgraph inside the template.
|
|
178
|
+
*/
|
|
179
|
+
export function segmentOf(keyText, index) {
|
|
180
|
+
if (keyText == null) return `@.${index}`;
|
|
181
|
+
const type = keyText[0];
|
|
182
|
+
const body = keyText.slice(1);
|
|
183
|
+
if (type === "s" && /^[A-Za-z0-9_-]{1,40}$/.test(body) && !/^\d+$/.test(body)) return `@${body}`;
|
|
184
|
+
if (type === "n" && /^\d{1,15}$/.test(body)) return `@${body}`;
|
|
185
|
+
const readable = showKey(keyText).replace(/[^A-Za-z0-9_-]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 24);
|
|
186
|
+
return `@${readable}.h${shortHash(keyText)}`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** An item's key as a value again (from keys.js keyText): "a", 3, true, ["en", 1]; by position, the index. */
|
|
190
|
+
export function keyValue(keyText, index) {
|
|
191
|
+
if (keyText == null) return index;
|
|
192
|
+
const body = keyText.slice(1);
|
|
193
|
+
if (keyText[0] === "s") return body;
|
|
194
|
+
if (keyText[0] === "n") return Number(body);
|
|
195
|
+
if (keyText[0] === "b") return body === "true";
|
|
196
|
+
try {
|
|
197
|
+
return JSON.parse(body);
|
|
198
|
+
} catch {
|
|
199
|
+
return body;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** An item's key as an object key (collect: "keyed"): the string, the number's digits, else JSON. */
|
|
204
|
+
export const keyName = (keyText, index) => {
|
|
205
|
+
const v = keyValue(keyText, index);
|
|
206
|
+
return typeof v === "string" ? v : typeof v === "number" || typeof v === "boolean" ? String(v) : JSON.stringify(v);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/** A key in words, for notes and the pane ("a", 3, item 2 by position). */
|
|
210
|
+
export const keyLabel = (keyText, index) => (keyText == null ? `#${index}` : showKey(keyText));
|
|
211
|
+
|
|
212
|
+
// ── the repeater of a pane ──────────────────────────────────────────────
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The live repeater of pane `id` on `outer`, made on first use:
|
|
216
|
+
* `{ instances: Map<segment, instance>, subgraphs: Map<segment, Canvas>, last, … }`.
|
|
217
|
+
* `subgraphs` (with `attached`, empty) lets canvasFor walk `g/@k/x` down to an instance.
|
|
218
|
+
*/
|
|
219
|
+
export function repeaterOf(outer, id) {
|
|
220
|
+
outer.repeaters ??= new Map();
|
|
221
|
+
let rep = outer.repeaters.get(id);
|
|
222
|
+
if (!rep) {
|
|
223
|
+
rep = { outer, id, instances: new Map(), subgraphs: new Map(), attached: new Set(), held: new Map(), last: null, stamp: "", settling: 0, disposed: false, runLog: null, traced: new Set(), tracedMore: false };
|
|
224
|
+
// Its instances' calls made in one task go to the sandbox as one (runtime.js batchUnder).
|
|
225
|
+
rep.unbatch = outer.runtime.batchUnder?.(`${id}${SEP}@`);
|
|
226
|
+
outer.repeaters.set(id, rep);
|
|
227
|
+
}
|
|
228
|
+
return rep;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Every instance canvas of every repeater on `canvas` (kept ones too). */
|
|
232
|
+
export function* instanceCanvases(canvas) {
|
|
233
|
+
for (const rep of canvas.repeaters?.values() ?? []) yield* rep.subgraphs.values();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Dispose pane `id`'s repeater: every instance goes (its panes' onCleanup runs). */
|
|
237
|
+
export function disposeRepeater(outer, id) {
|
|
238
|
+
const rep = outer.repeaters?.get(id);
|
|
239
|
+
if (!rep) return;
|
|
240
|
+
rep.disposed = true;
|
|
241
|
+
for (const inst of [...rep.instances.values()]) disposeInstance(rep, inst);
|
|
242
|
+
rep.unbatch?.();
|
|
243
|
+
outer.repeaters.delete(id);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Keep instance `seg` alive while something shows it (its view); release() lets a removal that came meanwhile finish. */
|
|
247
|
+
export function holdInstance(outer, id, seg) {
|
|
248
|
+
const rep = outer.repeaters?.get(id);
|
|
249
|
+
const inst = rep?.instances.get(seg);
|
|
250
|
+
if (!inst) return null;
|
|
251
|
+
rep.held.set(seg, (rep.held.get(seg) ?? 0) + 1);
|
|
252
|
+
inst.canvas.setDirtyTracking(true); // its view marks dirty panes
|
|
253
|
+
inst.canvas.keepHistory(); // and keeps its run history and run log from now on
|
|
254
|
+
return inst.canvas;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function releaseInstance(outer, id, seg) {
|
|
258
|
+
const rep = outer.repeaters?.get(id);
|
|
259
|
+
if (!rep || !rep.held.has(seg)) return;
|
|
260
|
+
const n = rep.held.get(seg) - 1;
|
|
261
|
+
if (n > 0) return void rep.held.set(seg, n);
|
|
262
|
+
rep.held.delete(seg);
|
|
263
|
+
const inst = rep.instances.get(seg);
|
|
264
|
+
inst?.canvas.setDirtyTracking(false);
|
|
265
|
+
if (inst?.pendingRemoval) finishRemoval(rep, inst);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* The instances of pane `id`, for its view and for agents, in item order
|
|
270
|
+
* (live ones), then those on their way out and the kept ones:
|
|
271
|
+
* [{ seg, key, label, index, state: "live" | "leaving" | "kept", template, status, canvas }].
|
|
272
|
+
* `status`: the instance's worst pane state (error, running, skipped, done, idle),
|
|
273
|
+
* worked out when read (the pane's strip reads it for the cells it draws).
|
|
274
|
+
*/
|
|
275
|
+
export function instancesOf(outer, id) {
|
|
276
|
+
const rep = outer.repeaters?.get(id);
|
|
277
|
+
if (!rep) return [];
|
|
278
|
+
const rank = { live: 0, leaving: 1, kept: 2 };
|
|
279
|
+
return [...rep.instances.values()]
|
|
280
|
+
.sort((a, b) => rank[a.state] - rank[b.state] || a.index - b.index)
|
|
281
|
+
.map((inst) => ({
|
|
282
|
+
seg: inst.seg,
|
|
283
|
+
key: keyValue(inst.key, inst.index),
|
|
284
|
+
label: keyLabel(inst.key, inst.index),
|
|
285
|
+
index: inst.index,
|
|
286
|
+
state: inst.state,
|
|
287
|
+
template: inst.template,
|
|
288
|
+
get status() {
|
|
289
|
+
return canvasStatus(inst.canvas);
|
|
290
|
+
},
|
|
291
|
+
canvas: inst.canvas,
|
|
292
|
+
}));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function canvasStatus(c) {
|
|
296
|
+
if (c.parked) return "kept";
|
|
297
|
+
const states = [...c.attached].filter((pid) => !NEEDS_SCREEN.has(c.panes.get(pid)?.type)).map((pid) => c.stateValue(pid).status);
|
|
298
|
+
for (const s of ["error", "running", "waiting"]) if (states.includes(s)) return s;
|
|
299
|
+
if (states.includes("done")) return "done";
|
|
300
|
+
return states.includes("skipped") ? "skipped" : "idle";
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// ── templates ────────────────────────────────────────────────────────────
|
|
304
|
+
|
|
305
|
+
const hashes = new WeakMap(); // panes array -> its definition hash
|
|
306
|
+
|
|
307
|
+
function hashOf(panes, groups) {
|
|
308
|
+
let h = hashes.get(panes);
|
|
309
|
+
if (h === undefined) hashes.set(panes, (h = linkHash(panes, groups)));
|
|
310
|
+
return h;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** The template an item's template name picks: `{ name, panes, hash }` ("" is the pane's own graph). */
|
|
314
|
+
function templateOf(pane, each, name) {
|
|
315
|
+
const t = name ? each.templates?.[name] : undefined;
|
|
316
|
+
if (t) return { name, panes: graphPanes(t), hash: hashOf(graphPanes(t), t.graph?.groups), ref: t.ref };
|
|
317
|
+
return { name: "", panes: graphPanes(pane), hash: hashOf(graphPanes(pane), pane.graph?.groups), ref: pane.ref };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Bring a live instance canvas in line with a changed template, in place:
|
|
322
|
+
* panes the template dropped go, new ones come, changed ones are updated.
|
|
323
|
+
* Panes that didn't change keep their values and state (a state pane's value,
|
|
324
|
+
* a timer's ticks), so editing the template doesn't reset every instance.
|
|
325
|
+
*/
|
|
326
|
+
export function applyTemplate(c, panes) {
|
|
327
|
+
const want = new Map(panes.map((p) => [p.id, p]));
|
|
328
|
+
const gesture = `template-${Math.random()}`;
|
|
329
|
+
for (const pid of [...c.attached]) if (!want.has(pid) || c.panes.get(pid)?.type !== want.get(pid).type) c.deletePane(pid, gesture);
|
|
330
|
+
for (const p of panes) {
|
|
331
|
+
const { rect = {}, ...content } = structuredClone(p);
|
|
332
|
+
if (!c.attached.has(p.id)) {
|
|
333
|
+
c.createPane(p.type, rect, content, gesture, { reuse: true });
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
const current = c.panes.get(p.id);
|
|
337
|
+
// Against the template pane with its type's defaults filled in, as the canvas model has it: a
|
|
338
|
+
// setting it leaves out (autorun, view) isn't a change, or every pane would rerun and lose its state.
|
|
339
|
+
const full = { inputs: [], name: "", ...(DEFAULTS[p.type]?.() ?? {}), ...content };
|
|
340
|
+
const patch = {};
|
|
341
|
+
for (const k of new Set([...Object.keys(current), ...Object.keys(full)])) {
|
|
342
|
+
if (k === "id" || k === "type" || k === "width") continue;
|
|
343
|
+
if (JSON.stringify(current[k]) !== JSON.stringify(full[k])) patch[k] = full[k];
|
|
344
|
+
}
|
|
345
|
+
if (Object.keys(patch).length) c.update(p.id, patch);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ── instances ────────────────────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* What each port (an input pane) of an instance reads, from its canvas's input
|
|
353
|
+
* panes: `{ kind: "item" | "outer" | "param" | "key" | "index" | "removing" | "own", key?, value? }`.
|
|
354
|
+
*/
|
|
355
|
+
function bindingsOf(rep, inst, pane) {
|
|
356
|
+
const c = inst.canvas;
|
|
357
|
+
const bound = new Map(pane.inputs.map((i) => [i.name, i]));
|
|
358
|
+
const params = pane.params && typeof pane.params === "object" ? pane.params : {};
|
|
359
|
+
const out = new Map();
|
|
360
|
+
for (const pid of c.attached) {
|
|
361
|
+
const port = c.panes.get(pid);
|
|
362
|
+
if (port?.type !== "input") continue;
|
|
363
|
+
const input = bound.get(port.name);
|
|
364
|
+
if (port.name === rep.listName && input) out.set(pid, { kind: "item" });
|
|
365
|
+
else if (input) out.set(pid, { kind: "outer", input });
|
|
366
|
+
else if (LIFECYCLE_PORTS.includes(port.name)) out.set(pid, { kind: port.name });
|
|
367
|
+
else if (Object.hasOwn(params, port.name)) out.set(pid, { kind: "param", value: params[port.name] });
|
|
368
|
+
else out.set(pid, { kind: "own" });
|
|
369
|
+
}
|
|
370
|
+
return out;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* What instance canvas `c`'s port `pid` reads on the repeater's canvas (to decide whether it is put back
|
|
375
|
+
* after a restart): the node of the input it copies (the repeated one, for its item; another, whole), or nothing
|
|
376
|
+
* (its key, index or removing, a parameter: values the repeater itself gives it).
|
|
377
|
+
*/
|
|
378
|
+
export function instancePortReads(c, pid) {
|
|
379
|
+
const outer = c.parent;
|
|
380
|
+
const rep = outer?.repeaters?.get(c.instanceOf?.pane);
|
|
381
|
+
const b = rep?.instances.get(c.instanceOf.seg)?.bindings?.get(pid);
|
|
382
|
+
if (!b) return [];
|
|
383
|
+
const input = b.kind === "item" ? outer.panes.get(rep.id)?.inputs?.find((i) => i.name === rep.listName) : b.kind === "outer" ? b.input : undefined;
|
|
384
|
+
return input ? [outer.events.depOf(rep.id, input)] : [];
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** The runtime port an instance's input pane `pid` has (namespacedRuntime's `portKey`), or undefined for its own widget. */
|
|
388
|
+
function portOf(rep, inst, pid) {
|
|
389
|
+
const b = inst.bindings?.get(pid);
|
|
390
|
+
if (!b) return undefined;
|
|
391
|
+
if (b.kind === "item") return { source: rep.listKey, index: inst.index, key: inst.key, spec: inst.key == null ? "" : rep.keySpec };
|
|
392
|
+
if (b.kind === "outer") return rep.outer.sourceKey(rep.id, b.input);
|
|
393
|
+
if (b.kind === "key") return { value: keyValue(inst.key, inst.index) };
|
|
394
|
+
if (b.kind === "index") return { value: inst.index };
|
|
395
|
+
if (b.kind === "removing") return { value: inst.state !== "live" };
|
|
396
|
+
return undefined; // a parameter (portParams) or its own widget
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** Each port's stamp: when it changes, the port runs again (and what reads it). */
|
|
400
|
+
function stampOf(rep, inst, b) {
|
|
401
|
+
if (b.kind === "item") {
|
|
402
|
+
if (!perItemStamps) return `${rep.listName}@${rep.listVersion}`; // (measuring) the whole list's version, as a subgraph's port
|
|
403
|
+
return `${rep.listName}:${inst.fp ?? `!${rep.runSeq}`}`; // an item that isn't JSON-like always counts as changed
|
|
404
|
+
}
|
|
405
|
+
if (b.kind === "outer") {
|
|
406
|
+
const home = rep.outer.inputHome(b.input);
|
|
407
|
+
return `${home.key}@${rep.outer.flow.get(home.dep)?.version ?? 0}`;
|
|
408
|
+
}
|
|
409
|
+
if (b.kind === "param") return `=${JSON.stringify(b.value)}`;
|
|
410
|
+
if (b.kind === "key") return `k${inst.key}`;
|
|
411
|
+
if (b.kind === "index") return `i${inst.index}`;
|
|
412
|
+
if (b.kind === "removing") return inst.state !== "live" ? "leaving" : "live";
|
|
413
|
+
return "";
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Point an instance's ports at what they read and rerun the ones whose stamp changed; returns how many reran.
|
|
418
|
+
* `rerun: false`: only note the stamps (the repeater pane put back after a restart, its instances' ports too).
|
|
419
|
+
*/
|
|
420
|
+
function bindInstance(rep, inst, pane, { rerun = true } = {}) {
|
|
421
|
+
inst.bindings = bindingsOf(rep, inst, pane);
|
|
422
|
+
const c = inst.canvas;
|
|
423
|
+
let reran = 0;
|
|
424
|
+
for (const [pid, b] of inst.bindings) {
|
|
425
|
+
if (b.kind === "param") c.portParams.set(pid, b.value);
|
|
426
|
+
else c.portParams.delete(pid);
|
|
427
|
+
if (b.kind === "own") c.portKeys.delete(pid);
|
|
428
|
+
else c.portKeys.set(pid, true); // a port: its pin is set with setValue (see Canvas#pinned)
|
|
429
|
+
const stamp = stampOf(rep, inst, b);
|
|
430
|
+
if (inst.stamps.get(pid) === stamp) continue;
|
|
431
|
+
const first = !inst.stamps.has(pid);
|
|
432
|
+
inst.stamps.set(pid, stamp);
|
|
433
|
+
if (!first && rerun) {
|
|
434
|
+
c.flow.invalidate(pid);
|
|
435
|
+
reran++;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
for (const pid of [...inst.stamps.keys()]) if (!inst.bindings.has(pid)) inst.stamps.delete(pid);
|
|
439
|
+
return reran;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const layouts = new WeakMap(); // a template's panes -> the window layout its instances share (Canvas.layoutOf)
|
|
443
|
+
|
|
444
|
+
function createInstance(rep, pane, want, template) {
|
|
445
|
+
const { outer, id } = rep;
|
|
446
|
+
let layout = layouts.get(template.panes);
|
|
447
|
+
if (!layout && typeof outer.constructor.layoutOf === "function") layouts.set(template.panes, (layout = outer.constructor.layoutOf({ panes: template.panes })));
|
|
448
|
+
const inst = { seg: want.seg, key: want.key, index: want.index, fp: want.fp, template: template.name, hash: template.hash, state: "live", stamps: new Map(), bindings: new Map(), timer: null, born: outer.clock.now() };
|
|
449
|
+
const prefix = `${id}${SEP}${want.seg}${SEP}`;
|
|
450
|
+
const name = `${pane.name || "repeater"} · ${keyLabel(want.key, want.index)}`;
|
|
451
|
+
const c = new outer.constructor({
|
|
452
|
+
runtime: namespacedRuntime(outer.runtime, prefix, (pid) => portOf(rep, inst, pid)),
|
|
453
|
+
// The same id as the outer canvas: network permissions are per canvas, and persisted state is keyed by the root's id and this prefix.
|
|
454
|
+
doc: { id: outer.id, name, viewport: pane.graph?.viewport, panes: structuredClone(template.panes) },
|
|
455
|
+
// Trusted like its canvas: its panes start a task later (dataflow), once its ports are bound below.
|
|
456
|
+
trusted: outer.trusted.value,
|
|
457
|
+
checkUrl: outer.checkUrl,
|
|
458
|
+
resolveImport: outer.resolveImport,
|
|
459
|
+
clock: outer.clockOption,
|
|
460
|
+
packageAssets: outer.packageAssets, // a package pane inside an instance reads its own package's files
|
|
461
|
+
layout,
|
|
462
|
+
history: false, // its steps go to the repeater's run log (instanceLog); its own once its view opens (holdInstance)
|
|
463
|
+
});
|
|
464
|
+
Object.assign(c, { parent: outer, subgraphId: id, instanceOf: { pane: id, seg: want.seg }, prefix: `${outer.prefix ?? ""}${prefix}`, portKeys: new Map(), portParams: new LazyMap(), portStamps: new LazyMap(), settling: 0 });
|
|
465
|
+
inst.canvas = c;
|
|
466
|
+
c.setDirtyTracking(false); // a lighter instance: which panes are dirty is worked out only while its view is open (holdInstance)
|
|
467
|
+
// A pane that keeps a recovery checkpoint is retried on its instance's own log: only a template with one pays for it.
|
|
468
|
+
if (template.panes.some((p) => p.each?.recover)) c.keepHistory();
|
|
469
|
+
// Changes inside that the repeater didn't start (a state setter, a timer, an edit in its view) rerun the repeater pane.
|
|
470
|
+
const rerun = () => {
|
|
471
|
+
if (rep.settling || rep.disposed || inst.state !== "live" || !outer.attached.has(id)) return;
|
|
472
|
+
outer.flow.invalidate(id);
|
|
473
|
+
};
|
|
474
|
+
// Heard directly, not as DOM events: an instance makes no event objects while nothing else listens.
|
|
475
|
+
c.onEmit = (type, detail) => {
|
|
476
|
+
if (type === "state") {
|
|
477
|
+
if (c.events.feeds.has(detail.id)) return; // an event output settling is an event going out (the outlet relays it), not a change of value: the repeater need not run again
|
|
478
|
+
const s = c.stateValue(detail.id);
|
|
479
|
+
if ((s.status === "done" || s.status === "error") && !s.result?.restored) rerun(); // a value put back after a restart is no change
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (type === "structure" || type === "content" || type === "geometry") inst.shape = null; // its panes, their order or their wires changed (instanceOutputs)
|
|
483
|
+
if (type === "structure") rerun();
|
|
484
|
+
};
|
|
485
|
+
c.runSink = instanceLog(rep, inst);
|
|
486
|
+
inst.off = () => {
|
|
487
|
+
c.onEmit = null;
|
|
488
|
+
c.runSink = null;
|
|
489
|
+
};
|
|
490
|
+
// Its panes' events also leave as the repeater's item events.
|
|
491
|
+
c.events.outlet = { wants: () => wantsItemEvents(rep, inst), out: (pid, name, item) => relayItemEvent(rep, inst, pid, name, item), close: (pid, kind, reason) => closeInstanceBatches(rep, inst, kind, reason, pid) };
|
|
492
|
+
rep.instances.set(want.seg, inst);
|
|
493
|
+
rep.subgraphs.set(want.seg, c);
|
|
494
|
+
syncInstanceAssets(rep, inst, template.ref);
|
|
495
|
+
bindInstance(rep, inst, pane);
|
|
496
|
+
return inst;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Tell the sandbox which files the panes of an instance read as `assets` (#646): those of the installed package
|
|
501
|
+
* its template's `ref` names (a repeater over a package pane, or a package pane as a template), by the same rule
|
|
502
|
+
* as a linked package pane's (`syncAssets` in src/model/subgraph.js: that version and copy, never a flagged one).
|
|
503
|
+
* An instance of a template that isn't a package's registers nothing, so it reads what the canvas around it has.
|
|
504
|
+
* `force`: again even when the ref is the same (a package was installed, updated or removed).
|
|
505
|
+
*/
|
|
506
|
+
function syncInstanceAssets(rep, inst, ref, { force = false } = {}) {
|
|
507
|
+
const key = ref?.package ? JSON.stringify([ref.package, ref.version ?? null, ref.integrity ?? null, ref.pane ?? null]) : "";
|
|
508
|
+
if (!force && key === (inst.assetKey ?? "")) return;
|
|
509
|
+
inst.assetKey = key;
|
|
510
|
+
const files = key ? rep.outer.packageAssets?.(ref) : undefined;
|
|
511
|
+
inst.canvas.runtime.setAssets?.("", files ?? null);
|
|
512
|
+
inst.canvas.runtime.setModule?.("", (key ? rep.outer.packageModuleFor?.(ref) : undefined) ?? null); // and its shared module (src/packages/module.js)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** The files the instances of every repeater on `canvas` read as `assets` again, and inside them (a package was installed, updated or removed). */
|
|
516
|
+
export function refreshInstanceAssets(canvas) {
|
|
517
|
+
for (const rep of canvas.repeaters?.values() ?? []) {
|
|
518
|
+
const pane = canvas.panes.get(rep.id);
|
|
519
|
+
for (const inst of rep.instances.values()) {
|
|
520
|
+
if (pane?.each) syncInstanceAssets(rep, inst, templateOf(pane, pane.each, inst.template).ref, { force: true });
|
|
521
|
+
inst.canvas.refreshPackageAssets();
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function disposeInstance(rep, inst) {
|
|
527
|
+
if (inst.timer) rep.outer.clock.clearTimer(inst.timer);
|
|
528
|
+
inst.timer = null;
|
|
529
|
+
inst.off?.();
|
|
530
|
+
rep.instances.delete(inst.seg);
|
|
531
|
+
rep.subgraphs.delete(inst.seg);
|
|
532
|
+
rep.held.delete(inst.seg);
|
|
533
|
+
inst.state = "gone";
|
|
534
|
+
closeInstanceBatches(rep, inst, "cancel", "its instance was removed"); // the readers of `item` are waiting for these batches to end
|
|
535
|
+
keepRemovedHolds(rep, inst.canvas); // what its waits and gates waited stays in the cost report
|
|
536
|
+
if (inst.assetKey) {
|
|
537
|
+
inst.canvas.runtime.setAssets?.("", null); // its package's files leave the sandbox with it
|
|
538
|
+
inst.canvas.runtime.setModule?.("", null); // and its shared module
|
|
539
|
+
}
|
|
540
|
+
inst.canvas.dispose(); // its values leave the sandbox, and its panes' onCleanup runs
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** Pause a kept instance: nothing in it runs, its timers stop, its values stay. */
|
|
544
|
+
function park(inst) {
|
|
545
|
+
inst.canvas.parked = true;
|
|
546
|
+
inst.canvas.events.dropQueued(); // a parked instance sends nothing, not even what waited for the transport
|
|
547
|
+
inst.canvas.flow.pause();
|
|
548
|
+
inst.canvas.timerClock.sync();
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function unpark(inst) {
|
|
552
|
+
if (!inst.canvas.parked) return;
|
|
553
|
+
inst.canvas.parked = false;
|
|
554
|
+
inst.canvas.flow.resume();
|
|
555
|
+
inst.canvas.timerClock.sync();
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/** A key went away: the instance leaves the value now, keeps running for the grace period (`removing` true), then finishes. */
|
|
559
|
+
function beginRemoval(rep, inst, each) {
|
|
560
|
+
if (inst.state !== "live") return;
|
|
561
|
+
inst.state = "leaving";
|
|
562
|
+
inst.policy = each.removal;
|
|
563
|
+
const pane = rep.outer.panes.get(rep.id);
|
|
564
|
+
if (pane) bindInstance(rep, inst, pane); // its `removing` port turns true
|
|
565
|
+
if (each.graceMs > 0) inst.timer = rep.outer.clock.setTimer(() => finishRemoval(rep, inst), each.graceMs);
|
|
566
|
+
else finishRemoval(rep, inst);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function finishRemoval(rep, inst) {
|
|
570
|
+
if (inst.timer) rep.outer.clock.clearTimer(inst.timer);
|
|
571
|
+
inst.timer = null;
|
|
572
|
+
if (inst.state === "gone" || inst.state === "live") return;
|
|
573
|
+
if (rep.held.has(inst.seg)) {
|
|
574
|
+
inst.pendingRemoval = true; // someone is looking at it: it goes when its view closes
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
inst.pendingRemoval = false;
|
|
578
|
+
const keep = inst.policy?.keepMinutes;
|
|
579
|
+
if (!keep) return disposeInstance(rep, inst);
|
|
580
|
+
inst.state = "kept";
|
|
581
|
+
park(inst);
|
|
582
|
+
inst.timer = rep.outer.clock.setTimer(() => {
|
|
583
|
+
inst.timer = null;
|
|
584
|
+
if (inst.state === "kept") disposeInstance(rep, inst);
|
|
585
|
+
}, keep * 60000);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** A removed key came back (while leaving or kept): its instance carries on, with its state. */
|
|
589
|
+
function revive(rep, inst) {
|
|
590
|
+
if (inst.timer) rep.outer.clock.clearTimer(inst.timer);
|
|
591
|
+
inst.timer = null;
|
|
592
|
+
inst.pendingRemoval = false;
|
|
593
|
+
inst.state = "live";
|
|
594
|
+
unpark(inst);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// ── the repeater's run log ─────────────────────────────────────────
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* The repeater's one run log of its instances' steps (a RunLog whose pane ids
|
|
601
|
+
* are `<seg>/<inner id>`), made on first use: the outer run log's step for the
|
|
602
|
+
* repeater's run lists the steps of the instances that ran from it (runlog.js
|
|
603
|
+
* canvasFacts). An instance keeps no run log of its own until its view opens.
|
|
604
|
+
*/
|
|
605
|
+
export function repeaterLog(rep) {
|
|
606
|
+
if (rep.runLog) return rep.runLog;
|
|
607
|
+
const at = (key) => {
|
|
608
|
+
const cut = key.indexOf(SEP);
|
|
609
|
+
const inst = rep.instances.get(key.slice(0, cut));
|
|
610
|
+
return inst ? { inst, id: key.slice(cut + 1) } : undefined;
|
|
611
|
+
};
|
|
612
|
+
const facts = (inst) => (inst.facts ??= canvasFacts(inst.canvas));
|
|
613
|
+
const { outer } = rep;
|
|
614
|
+
rep.runLog = new RunLog({
|
|
615
|
+
...(outer.clockOption ? { now: outer.clock.now, clock: outer.clock.now } : {}),
|
|
616
|
+
// What each instance's pane may keep is its own pane's policy (the canvas's, the repeater's, the template pane's):
|
|
617
|
+
// the key is the instance's, so a step is cut as it is recorded, not only once the outer log copies it.
|
|
618
|
+
retention: (key) => {
|
|
619
|
+
const a = at(key);
|
|
620
|
+
return a ? retentionOf(a.inst.canvas, a.id) : configPolicy();
|
|
621
|
+
},
|
|
622
|
+
facts: {
|
|
623
|
+
start: (key, state) => {
|
|
624
|
+
const a = at(key);
|
|
625
|
+
return a ? facts(a.inst).start(a.id, state) : {};
|
|
626
|
+
},
|
|
627
|
+
settle: (key, state, step) => {
|
|
628
|
+
const a = at(key);
|
|
629
|
+
return a ? facts(a.inst).settle(a.id, state, step) : {};
|
|
630
|
+
},
|
|
631
|
+
capture: async (key, state, step) => {
|
|
632
|
+
const a = at(key);
|
|
633
|
+
return a ? await facts(a.inst).capture(a.id, state, step) : undefined;
|
|
634
|
+
},
|
|
635
|
+
},
|
|
636
|
+
isRunning: (key, runId) => {
|
|
637
|
+
const a = at(key);
|
|
638
|
+
const s = a?.inst.canvas.flow.get(a.id);
|
|
639
|
+
return s?.status === "running" && s.runId === runId;
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
return rep.runLog;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Instance `inst`'s steps, into its repeater's run log (Canvas#runSink): those
|
|
647
|
+
* of the first MAX_INSTANCES instances that start running after the repeater's
|
|
648
|
+
* run starts (traceInstances), as the outer run log keeps; the others' cost nothing.
|
|
649
|
+
*/
|
|
650
|
+
function instanceLog(rep, inst) {
|
|
651
|
+
const key = (id) => `${inst.seg}${SEP}${id}`;
|
|
652
|
+
const traced = (state) => {
|
|
653
|
+
if (rep.traced.has(inst.seg)) return true;
|
|
654
|
+
if (state?.status !== "running") return false;
|
|
655
|
+
if (rep.traced.size >= MAX_INSTANCES) {
|
|
656
|
+
rep.tracedMore = true;
|
|
657
|
+
return false;
|
|
658
|
+
}
|
|
659
|
+
rep.traced.add(inst.seg);
|
|
660
|
+
return true;
|
|
661
|
+
};
|
|
662
|
+
return {
|
|
663
|
+
observe: (id, state) => traced(state) && repeaterLog(rep).observe(key(id), state),
|
|
664
|
+
hidden: (id, state) => traced(state) && repeaterLog(rep).hidden(key(id), state),
|
|
665
|
+
noteWait: (id, runId, kind, ms) => rep.traced.has(inst.seg) && repeaterLog(rep).noteWait(key(id), runId, kind, ms),
|
|
666
|
+
forget: (id) => rep.runLog?.forget(key(id)),
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** A run of the repeater starts: the instances it runs are the ones its run log keeps (an instance still running from before stays). */
|
|
671
|
+
function traceInstances(rep) {
|
|
672
|
+
const open = new Set([...(rep.runLog?.open.keys() ?? [])].map((key) => key.slice(0, key.indexOf(SEP))));
|
|
673
|
+
rep.traced = new Set([...(rep.traced ?? [])].filter((seg) => open.has(seg)));
|
|
674
|
+
rep.tracedMore = false;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Every canvas in `list` settled: nothing queued or running inside (a pane
|
|
679
|
+
* may queue another, a state setter, so look twice). Each canvas's idle() is
|
|
680
|
+
* woken by its own dataflow's changes (src/model/flow-idle.js): no timer per
|
|
681
|
+
* instance per round, and none at all, which a hidden tab would hold
|
|
682
|
+
* back.
|
|
683
|
+
*/
|
|
684
|
+
export async function settledAll(list) {
|
|
685
|
+
for (;;) {
|
|
686
|
+
// A parked instance's scheduler is paused, but a run it had started still counts.
|
|
687
|
+
await Promise.all(list.map((c) => c.flow.until(undefined, { paused: true })));
|
|
688
|
+
if (!list.some((c) => !c.disposed && c.hasRunning())) return;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const pause = (clock, ms, signal) =>
|
|
693
|
+
new Promise((resolve) => {
|
|
694
|
+
if (!(ms > 0) || signal?.aborted) return resolve();
|
|
695
|
+
const timer = clock.setTimer(done, ms);
|
|
696
|
+
function done() {
|
|
697
|
+
clock.clearTimer(timer);
|
|
698
|
+
signal?.removeEventListener("abort", done);
|
|
699
|
+
resolve();
|
|
700
|
+
}
|
|
701
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
// ── item events ───────────────────────────────────────────────────
|
|
705
|
+
|
|
706
|
+
/** Does anything read repeater `rep`'s item events, and may instance `inst` send them (live, or in its grace period)? */
|
|
707
|
+
function wantsItemEvents(rep, inst) {
|
|
708
|
+
if (rep.disposed || (inst.state !== "live" && inst.state !== "leaving") || inst.canvas.parked) return false;
|
|
709
|
+
return rep.outer.events.hasReaders(rep.id, ITEM_EVENTS);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* An instance's panes in order (`order`), its output panes (`outs`) and the
|
|
714
|
+
* sandbox keys of their values in the outer canvas's id space (`keys`). Kept
|
|
715
|
+
* until its panes change (every run of the repeater reads it for every
|
|
716
|
+
* instance).
|
|
717
|
+
*/
|
|
718
|
+
function instanceOutputs(rep, pane, inst) {
|
|
719
|
+
const output = inst.template ? null : (pane.output ?? null);
|
|
720
|
+
if (inst.shape?.output === output) return inst.shape;
|
|
721
|
+
const c = inst.canvas;
|
|
722
|
+
const order = c.order();
|
|
723
|
+
const outs = outputIds({ ...pane, output, graph: { panes: order.map((pid) => c.panes.get(pid)) } });
|
|
724
|
+
inst.shape = { output, order, outs, keys: outs.map((pid) => `${rep.id}${SEP}${inst.seg}${SEP}${c.homeOf(pid).key}`) };
|
|
725
|
+
return inst.shape;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// ── after a restart ──────────────────────────────────────────────────
|
|
729
|
+
//
|
|
730
|
+
// A repeater's instances keep their values across a restart of the sandbox
|
|
731
|
+
// under the rules any pane does (src/model/canvas.js #restorePlan): their
|
|
732
|
+
// ports are put back when what they copy on the repeater's canvas is (the
|
|
733
|
+
// repeated input, for the item; another input, whole), and their panes when
|
|
734
|
+
// everything they read is. The repeater pane is put back when every live
|
|
735
|
+
// instance's outputs and copying ports are, and runs again otherwise (it plans
|
|
736
|
+
// in the sandbox and gathers its instances' values).
|
|
737
|
+
|
|
738
|
+
/** Are all of repeater `id`'s live instances' outputs, and the ports that copy from outside, kept (`planOf(canvas)`: each instance's plan)? */
|
|
739
|
+
export function instancesKept(outer, id, planOf) {
|
|
740
|
+
const rep = outer.repeaters?.get(id);
|
|
741
|
+
const pane = outer.panes.get(id);
|
|
742
|
+
if (!rep?.last || !pane) return false;
|
|
743
|
+
for (const inst of rep.instances.values()) {
|
|
744
|
+
if (inst.state !== "live") continue;
|
|
745
|
+
const plan = planOf(inst.canvas);
|
|
746
|
+
if (!plan) return false;
|
|
747
|
+
const copying = [...inst.bindings.keys()].filter((pid) => instancePortReads(inst.canvas, pid).length);
|
|
748
|
+
if (![...instanceOutputs(rep, pane, inst).outs, ...copying].every((pid) => plan.same.has(pid))) return false;
|
|
749
|
+
}
|
|
750
|
+
return true;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Instance canvas `c` restarts keeping `keep`: its ports that copy a value from outside that isn't kept
|
|
755
|
+
* wait for the repeater pane, which runs again after that value and then reruns them (their stamps are marked
|
|
756
|
+
* stale here; an item's stamp is its content, which a restart doesn't change). Run now, they would find nothing
|
|
757
|
+
* to copy yet. Returns them, for the canvas to leave out of its run.
|
|
758
|
+
*/
|
|
759
|
+
export function portsAfterRestart(c, keep) {
|
|
760
|
+
const rep = c.parent?.repeaters?.get(c.instanceOf?.pane);
|
|
761
|
+
const inst = rep?.instances.get(c.instanceOf.seg);
|
|
762
|
+
const held = new Set();
|
|
763
|
+
if (!inst) return held;
|
|
764
|
+
for (const pid of inst.bindings.keys()) {
|
|
765
|
+
if (keep.has(pid) || !instancePortReads(c, pid).length) continue;
|
|
766
|
+
held.add(pid);
|
|
767
|
+
if (inst.stamps.has(pid)) inst.stamps.set(pid, "");
|
|
768
|
+
}
|
|
769
|
+
return held;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** Repeater pane `id` was put back after a restart: its live instances' ports note what they copy now, so its next run reruns only what changes. */
|
|
773
|
+
export function restampInstances(outer, id) {
|
|
774
|
+
const rep = outer.repeaters?.get(id);
|
|
775
|
+
const pane = outer.panes.get(id);
|
|
776
|
+
if (!rep || !pane) return;
|
|
777
|
+
for (const inst of rep.instances.values()) if (inst.state === "live") bindInstance(rep, inst, pane, { rerun: false });
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Instance `inst`'s batch `batch` as the repeater's `item` output sees it: its own, so two instances that use the
|
|
782
|
+
* same key never mix on one wire (the key is at most MAX_BATCH_KEY characters: past it, a hash of the whole).
|
|
783
|
+
*/
|
|
784
|
+
export function itemBatchKey(inst, batch) {
|
|
785
|
+
const key = `${inst.seg}:${batch}`;
|
|
786
|
+
return key.length <= MAX_BATCH_KEY ? key : `@${shortHash(key)}`;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Close the batches instance `inst` still has open on `item` (those of pane `only`, when given) with a `kind` marker
|
|
791
|
+
* (error or cancel): the readers' operators apply the policy the batch's start declared. Nothing else would end them: the
|
|
792
|
+
* instance's source is gone or failed, and the repeater's readers hold state for them.
|
|
793
|
+
*/
|
|
794
|
+
function closeInstanceBatches(rep, inst, kind, reason, only) {
|
|
795
|
+
if (!inst.openBatches?.size || rep.disposed) return;
|
|
796
|
+
for (const [key, open] of [...inst.openBatches]) {
|
|
797
|
+
if (only !== undefined && open.pane !== only) continue;
|
|
798
|
+
inst.openBatches.delete(key);
|
|
799
|
+
rep.outer.events.sendMarker(rep.id, ITEM_EVENTS, { kind, batch: key, reason }, { source: open.source });
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/** Event `name` of pane `pid` inside instance `inst` went out: the repeater's item event, in order. */
|
|
804
|
+
function relayItemEvent(rep, inst, pid, name, item) {
|
|
805
|
+
if (!wantsItemEvents(rep, inst)) return;
|
|
806
|
+
const { outer, id } = rep;
|
|
807
|
+
const pane = outer.panes.get(id);
|
|
808
|
+
if (!pane) return;
|
|
809
|
+
const source = `${pane.name || id} · ${keyLabel(inst.key, inst.index)}`;
|
|
810
|
+
if (item.marker) {
|
|
811
|
+
// A batch marker: the same marker for this instance's batch, in order with its events.
|
|
812
|
+
const { kind, batch, reason, onError, onCancel } = item.marker;
|
|
813
|
+
const key = itemBatchKey(inst, batch);
|
|
814
|
+
if (kind === "start") (inst.openBatches ??= new Map()).set(key, { pane: pid, source });
|
|
815
|
+
else inst.openBatches?.delete(key);
|
|
816
|
+
outer.events.sendMarker(id, ITEM_EVENTS, { kind, batch: key, ...(reason === undefined ? {} : { reason }), ...(kind === "start" ? { onError, onCancel } : {}) }, { source });
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
const batch = item.batch === undefined ? undefined : itemBatchKey(inst, item.batch);
|
|
820
|
+
if (batch !== undefined && !inst.openBatches?.has(batch)) (inst.openBatches ??= new Map()).set(batch, { pane: pid, source }); // an event opens its batch
|
|
821
|
+
const c = inst.canvas;
|
|
822
|
+
const fields = { key: keyValue(inst.key, inst.index), name, index: inst.index, pane: c.panes.get(pid)?.name || pid, ...(inst.state !== "live" ? { removing: true } : {}) };
|
|
823
|
+
// The payload: in the Worker when pane code sent it, or a repeater inside relayed it (its key there; the Worker
|
|
824
|
+
// stores in order, so it is there first), else the page's.
|
|
825
|
+
const from = item.result || item.pending ? `${id}${SEP}${inst.seg}${SEP}${item.key}` : null;
|
|
826
|
+
// The instance's value, when every output pane has one (a running instance keeps its last).
|
|
827
|
+
const { outs, keys } = instanceOutputs(rep, pane, inst);
|
|
828
|
+
const value = outs.length && outs.every((o) => c.stateValue(o).status === "done") ? { s: keys, ...(outs.length > 1 ? { l: outs.map((o) => c.panes.get(o)?.name || o) } : {}) } : null;
|
|
829
|
+
// Its size for the readers' byte budgets: the fields, and the payload as the inner event was measured (the Worker holds it) or as the page has it.
|
|
830
|
+
const bytes = approxBytes(fields) + (typeof item.bytes === "number" ? item.bytes : approxBytes(item.payload ?? null));
|
|
831
|
+
outer.events.relay(id, ITEM_EVENTS, ({ key, at }) => outer.runtime.storeItemEvent({ node: portId(id, ITEM_EVENTS), key, name: ITEM_EVENTS, fields, from, payload: from ? null : (item.payload ?? null), value, at, source, keep: outer.events.keepFor(portId(id, ITEM_EVENTS)), ...(batch === undefined ? {} : { batch }) }), { source, bytes, batch });
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// ── running ──────────────────────────────────────────────────────────────
|
|
835
|
+
|
|
836
|
+
/** The inputs' identities and versions, the settings and the templates: when none changed, the plan can't have either. */
|
|
837
|
+
function stampOfRun(outer, id, pane, each) {
|
|
838
|
+
const inputs = pane.inputs.map((i) => {
|
|
839
|
+
const home = outer.inputHome(i);
|
|
840
|
+
return `${i.name}=${home.key}@${outer.flow.get(outer.events.depOf(id, i))?.version ?? 0}`;
|
|
841
|
+
});
|
|
842
|
+
const templates = Object.keys(each.templates ?? {}).map((name) => `${name}:${templateOf(pane, each, name).hash}`);
|
|
843
|
+
return JSON.stringify([inputs, each, pane.params ?? null, templateOf(pane, each, "").hash, templates]);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Run repeater pane `id` (see the header): plan the items in the sandbox,
|
|
848
|
+
* create, keep, move and remove instances, rerun the ports of the items that
|
|
849
|
+
* changed, wait for every instance to settle, and gather their values. A
|
|
850
|
+
* failure inside an instance (that no catch pane inside caught) fails the
|
|
851
|
+
* pane, naming the instances. Resolves with the value's result plus
|
|
852
|
+
* `repeat` (what this change did), or `{ superseded: true }`.
|
|
853
|
+
*/
|
|
854
|
+
export async function runRepeater(outer, id, { runId, signal, fresh = false, retry = false }) {
|
|
855
|
+
const pane = outer.panes.get(id);
|
|
856
|
+
const each = normalizeRepeat(pane.each);
|
|
857
|
+
const rep = repeaterOf(outer, id);
|
|
858
|
+
rep.disposed = false;
|
|
859
|
+
rep.runSeq = (rep.runSeq ?? 0) + 1;
|
|
860
|
+
traceInstances(rep);
|
|
861
|
+
const label = pane.name || id;
|
|
862
|
+
const progress = (p) => (outer.progressOf(id).value = { runId, repeat: true, ...p });
|
|
863
|
+
rep.settling++;
|
|
864
|
+
try {
|
|
865
|
+
const stamp = stampOfRun(outer, id, pane, each);
|
|
866
|
+
let summary = rep.last;
|
|
867
|
+
if (fresh || stamp !== rep.stamp || !summary) {
|
|
868
|
+
summary = await plan(outer, rep, pane, each, { runId, signal, label, progress });
|
|
869
|
+
if (summary?.superseded) return summary;
|
|
870
|
+
}
|
|
871
|
+
// Wait for every live instance (the ones this run made, and the ones whose ports it reran).
|
|
872
|
+
const live = [...rep.instances.values()].filter((i) => i.state === "live").sort((a, b) => a.index - b.index);
|
|
873
|
+
for (const i of live) bindContexts(i.canvas); // a context its panes read changed (src/model/context.js)
|
|
874
|
+
// Another try (the pane's error policy): the panes inside that failed run again.
|
|
875
|
+
if (retry) for (const i of live) for (const pid of i.canvas.attached) if (i.canvas.stateValue(pid).status === "error") i.canvas.flow.run(pid);
|
|
876
|
+
await settledAll(live.map((i) => i.canvas));
|
|
877
|
+
if (signal?.aborted) return { superseded: true };
|
|
878
|
+
rep.stamp = stamp;
|
|
879
|
+
rep.last = summary;
|
|
880
|
+
return await collect(outer, rep, pane, each, live, { runId, label, summary });
|
|
881
|
+
} finally {
|
|
882
|
+
rep.settling--;
|
|
883
|
+
if (outer.progress.get(id)?.value?.repeat) outer.progressOf(id).value = null;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/** The plan and the instance changes: returns this change's summary, or `{ superseded: true }`. */
|
|
888
|
+
async function plan(outer, rep, pane, each, { runId, signal, label, progress }) {
|
|
889
|
+
const names = pane.inputs.map((i) => i.name);
|
|
890
|
+
const sources = pane.inputs.map((i) => outer.sourceKey(rep.id, i));
|
|
891
|
+
if (typeof outer.runtime.repeatPlan !== "function") throw new Error("repeaters aren't available in this sandbox");
|
|
892
|
+
const answer = await outer.runtime.repeatPlan({ id: rep.id, runId, sources, names, input: each.input, key: each.key ?? "", templateBy: each.templateBy ?? "", max: each.max, label });
|
|
893
|
+
if (signal?.aborted || answer?.superseded) return { superseded: true };
|
|
894
|
+
rep.listKey = sources[answer.over];
|
|
895
|
+
rep.listName = answer.name;
|
|
896
|
+
const listInput = pane.inputs[answer.over];
|
|
897
|
+
rep.listVersion = outer.flow.get(outer.events.depOf(rep.id, listInput))?.version ?? 0;
|
|
898
|
+
rep.keySpec = answer.keyed ? each.key : "";
|
|
899
|
+
const notes = answer.note ? [`key: ${answer.note}`] : [];
|
|
900
|
+
// The template each item gets; a template name with no template uses the pane's own graph.
|
|
901
|
+
const unknown = new Set();
|
|
902
|
+
const wanted = [];
|
|
903
|
+
for (let i = 0; i < answer.count; i++) {
|
|
904
|
+
const key = answer.keyed ? answer.keys[i] : null;
|
|
905
|
+
const name = answer.templates?.[i] ?? "";
|
|
906
|
+
if (name && !each.templates?.[name]) unknown.add(name);
|
|
907
|
+
wanted.push({ seg: segmentOf(key, i), key, index: i, fp: answer.fps[i], template: name && each.templates?.[name] ? name : "" });
|
|
908
|
+
}
|
|
909
|
+
if (unknown.size) notes.push(`${each.templateBy}: ${[...unknown].slice(0, 5).map((n) => JSON.stringify(n)).join(", ")}${unknown.size > 5 ? ", …" : ""} ${unknown.size === 1 ? "has" : "have"} no template, so ${unknown.size === 1 ? "it uses" : "they use"} the pane's own`);
|
|
910
|
+
let want = new Map(wanted.map((w) => [w.seg, w]));
|
|
911
|
+
if (want.size !== wanted.length) {
|
|
912
|
+
// Two keys with one namespace (a hash collision): by position, never two items in one instance.
|
|
913
|
+
wanted.forEach((w, i) => Object.assign(w, { seg: segmentOf(null, i), key: null }));
|
|
914
|
+
want = new Map(wanted.map((w) => [w.seg, w]));
|
|
915
|
+
rep.keySpec = "";
|
|
916
|
+
notes.push("two keys share a namespace, so the instances follow positions");
|
|
917
|
+
}
|
|
918
|
+
const counts = { created: 0, kept: 0, removed: 0, moved: 0, reran: 0, revived: 0, recreated: 0, updated: 0 };
|
|
919
|
+
// Removed keys: their instances leave (grace, then drop or keep).
|
|
920
|
+
for (const inst of [...rep.instances.values()]) {
|
|
921
|
+
if (want.has(inst.seg) || inst.state !== "live") continue;
|
|
922
|
+
beginRemoval(rep, inst, each);
|
|
923
|
+
counts.removed++;
|
|
924
|
+
}
|
|
925
|
+
// Kept (and revived) keys: a new position, a new item, a changed template.
|
|
926
|
+
const toCreate = [];
|
|
927
|
+
for (const w of wanted) {
|
|
928
|
+
let inst = rep.instances.get(w.seg);
|
|
929
|
+
const template = templateOf(pane, each, w.template);
|
|
930
|
+
if (inst && inst.template !== template.name) {
|
|
931
|
+
disposeInstance(rep, inst); // another template: another canvas
|
|
932
|
+
inst = undefined;
|
|
933
|
+
counts.recreated++;
|
|
934
|
+
}
|
|
935
|
+
if (!inst) {
|
|
936
|
+
toCreate.push([w, template]);
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
939
|
+
if (inst.state !== "live") {
|
|
940
|
+
revive(rep, inst);
|
|
941
|
+
counts.revived++;
|
|
942
|
+
} else counts.kept++;
|
|
943
|
+
if (inst.index !== w.index) counts.moved++;
|
|
944
|
+
if (inst.hash !== template.hash) {
|
|
945
|
+
syncInstanceAssets(rep, inst, template.ref); // updated to another version of its package: that version's files, before its panes rerun
|
|
946
|
+
applyTemplate(inst.canvas, template.panes);
|
|
947
|
+
inst.hash = template.hash;
|
|
948
|
+
counts.updated++;
|
|
949
|
+
}
|
|
950
|
+
Object.assign(inst, { index: w.index, fp: w.fp, key: w.key });
|
|
951
|
+
if (bindInstance(rep, inst, pane)) counts.reran++;
|
|
952
|
+
}
|
|
953
|
+
// New keys: created in batches (each batch settles before the next starts).
|
|
954
|
+
const batches = planBatches(toCreate.length, each.batch);
|
|
955
|
+
let made = 0;
|
|
956
|
+
for (const { start, end, delayMs } of batches) {
|
|
957
|
+
if (signal?.aborted) return { superseded: true };
|
|
958
|
+
if (delayMs) {
|
|
959
|
+
progress({ count: answer.count, total: answer.total, done: made, failed: 0, pausedUntil: outer.clock.now() + delayMs });
|
|
960
|
+
await pause(outer.clock, delayMs, signal);
|
|
961
|
+
if (signal?.aborted) return { superseded: true };
|
|
962
|
+
}
|
|
963
|
+
const batch = toCreate.slice(start, end).map(([w, template]) => createInstance(rep, pane, w, template));
|
|
964
|
+
counts.created += batch.length;
|
|
965
|
+
if (batches.length > 1) {
|
|
966
|
+
progress({ count: answer.count, total: answer.total, done: made, failed: 0 });
|
|
967
|
+
await settledAll(batch.map((i) => i.canvas));
|
|
968
|
+
made += batch.length;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
// The template editor (the pane opened), when it is: its ports read the first item (src/model/subgraph.js bindPorts).
|
|
972
|
+
rep.firstKey = wanted[0] ? keyValue(wanted[0].key, 0) : undefined;
|
|
973
|
+
const editor = outer.subgraphs.get(rep.id);
|
|
974
|
+
if (editor) bindPorts(outer, rep.id, editor);
|
|
975
|
+
const leaving = [...rep.instances.values()].filter((i) => i.state === "leaving").length;
|
|
976
|
+
const kept = [...rep.instances.values()].filter((i) => i.state === "kept").length;
|
|
977
|
+
return {
|
|
978
|
+
instances: answer.count,
|
|
979
|
+
total: answer.total,
|
|
980
|
+
keyed: answer.keyed,
|
|
981
|
+
...counts,
|
|
982
|
+
...(leaving ? { leaving } : {}),
|
|
983
|
+
...(kept ? { parked: kept } : {}),
|
|
984
|
+
...(answer.hinted !== undefined ? { hinted: answer.hinted } : {}),
|
|
985
|
+
...(notes.length ? { note: notes.join("; ") } : {}),
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/** The pane's value from its live instances (in item order), or its error. */
|
|
990
|
+
async function collect(outer, rep, pane, each, live, { runId, label, summary }) {
|
|
991
|
+
const failed = [];
|
|
992
|
+
const items = [];
|
|
993
|
+
let skipped = 0;
|
|
994
|
+
for (const inst of live) {
|
|
995
|
+
const c = inst.canvas;
|
|
996
|
+
const name = (pid) => c.panes.get(pid)?.name || pid;
|
|
997
|
+
const who = keyLabel(inst.key, inst.index);
|
|
998
|
+
const shape = instanceOutputs(rep, pane, inst);
|
|
999
|
+
const bad = shape.order.find((pid) => {
|
|
1000
|
+
const s = c.stateValue(pid);
|
|
1001
|
+
return s.status === "error" && !NEEDS_SCREEN.has(c.panes.get(pid)?.type) && !c.caughtFailures.has(pid);
|
|
1002
|
+
});
|
|
1003
|
+
if (bad) {
|
|
1004
|
+
const s = c.stateValue(bad);
|
|
1005
|
+
failed.push(`${who}: ${name(bad)}: ${s.error?.message ?? (s.cycle ? "a dependency cycle" : "failed")}`);
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
let { outs } = shape;
|
|
1009
|
+
const skippedOuts = outs.filter((pid) => c.stateValue(pid).status === "skipped");
|
|
1010
|
+
if (outs.length && skippedOuts.length === outs.length) {
|
|
1011
|
+
skipped++; // a skipped instance is left out of the value (as a for-each item that skips)
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
outs = outs.filter((pid) => !skippedOuts.includes(pid));
|
|
1015
|
+
const notDone = outs.find((pid) => c.stateValue(pid).status !== "done");
|
|
1016
|
+
if (notDone) {
|
|
1017
|
+
const s = c.stateValue(notDone);
|
|
1018
|
+
failed.push(`${who}: ${name(notDone)} is ${s.stale ? "stale" : s.status === "idle" ? "waiting to run" : s.status}`);
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
const sources = outs.length === shape.outs.length ? shape.keys : outs.map((pid) => `${rep.id}${SEP}${inst.seg}${SEP}${c.homeOf(pid).key}`);
|
|
1022
|
+
items.push({ s: sources, i: inst.index, ...(outs.length > 1 ? { l: outs.map(name) } : {}), ...(each.collect === "keyed" ? { k: keyName(inst.key, inst.index) } : {}) });
|
|
1023
|
+
}
|
|
1024
|
+
const repeat = { ...summary, ...(skipped ? { skipped } : {}), ...(failed.length ? { failed: failed.length } : {}) };
|
|
1025
|
+
if (failed.length) {
|
|
1026
|
+
const shown = failed.slice(0, 3).join("; ");
|
|
1027
|
+
const error = new Error(`${failed.length} of ${live.length} instance${live.length === 1 ? "" : "s"} failed: ${shown}${failed.length > 3 ? `; and ${failed.length - 3} more` : ""}`);
|
|
1028
|
+
error.name = "RepeaterError";
|
|
1029
|
+
error.repeat = repeat;
|
|
1030
|
+
throw error;
|
|
1031
|
+
}
|
|
1032
|
+
let result;
|
|
1033
|
+
if (typeof outer.runtime.gather === "function") {
|
|
1034
|
+
result = await outer.runtime.gather({ id: rep.id, runId, items, keyed: each.collect === "keyed", view: pane.view ?? "auto" });
|
|
1035
|
+
} else {
|
|
1036
|
+
// A runtime without gather (an older test double): build the value with an expression.
|
|
1037
|
+
const flat = items.flatMap((it) => it.s);
|
|
1038
|
+
const names = flat.map((_, i) => `v${i}`);
|
|
1039
|
+
let n = 0;
|
|
1040
|
+
const one = (it) => (it.l ? `({ ${it.l.map((l) => `${JSON.stringify(l)}: v${n++}`).join(", ")} })` : `v${n++}`);
|
|
1041
|
+
const code = each.collect === "keyed" ? `({ ${items.map((it) => `${JSON.stringify(it.k)}: ${one(it)}`).join(", ")} })` : `[${items.map(one).join(", ")}]`;
|
|
1042
|
+
result = await outer.runtime.exec({ id: rep.id, runId, sources: flat, names, code, mode: "expression", view: pane.view ?? "auto", label });
|
|
1043
|
+
}
|
|
1044
|
+
if (result?.superseded) return result;
|
|
1045
|
+
return { ...result, repeat };
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// ── exposed panes ─────────────────────────────────────────────────
|
|
1049
|
+
|
|
1050
|
+
/** Does any template of repeater pane `pane` (its own graph, or one of `templates`) have the pane `target` names (exposedPath)? */
|
|
1051
|
+
function templatesHave(pane, target) {
|
|
1052
|
+
const each = normalizeRepeat(pane.each);
|
|
1053
|
+
return [pane, ...Object.values(each.templates ?? {})].some((t) => exposedPath(graphPanes(t), target) !== null);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Where exposed pane `target` (exposedPath) is in instance `inst`: its value's
|
|
1058
|
+
* sandbox key below the instance (`x`, `g/x`, with `#output`) and its run
|
|
1059
|
+
* state now (the canvas inside a subgraph pane holds a pane in it), or null.
|
|
1060
|
+
*/
|
|
1061
|
+
function exposedIn(inst, target, port) {
|
|
1062
|
+
const c = inst.canvas;
|
|
1063
|
+
const path = exposedPath(c.order().map((pid) => c.panes.get(pid)), target);
|
|
1064
|
+
if (!path) return null;
|
|
1065
|
+
const last = path.at(-1);
|
|
1066
|
+
const node = port ? portId(last, port) : last;
|
|
1067
|
+
let holder = c;
|
|
1068
|
+
for (const gid of path.slice(0, -1)) holder = holder?.subgraphs.get(gid);
|
|
1069
|
+
return { key: [...path.slice(0, -1), node].join(SEP), state: holder?.stateValue(node) };
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* What "+ expose…" offers for repeater pane content `pane`:
|
|
1074
|
+
* `[{ pane, output?, type, depth }]`, the panes of its templates (its own
|
|
1075
|
+
* graph, and each linked template) and their named outputs, with the panes
|
|
1076
|
+
* inside subgraph panes in them as `sub/name` (not inside a repeater: its
|
|
1077
|
+
* panes are its instances'). Panes are matched by name, so the same name in
|
|
1078
|
+
* several templates is one choice. Input panes are left out: their value is
|
|
1079
|
+
* the item, its key, a value from outside or a port's, already at hand.
|
|
1080
|
+
*/
|
|
1081
|
+
export function exposeChoices(pane) {
|
|
1082
|
+
const each = normalizeRepeat(pane.each);
|
|
1083
|
+
const seen = new Set();
|
|
1084
|
+
const choices = [];
|
|
1085
|
+
const walk = (panes, prefix, depth) => {
|
|
1086
|
+
for (const p of panes) {
|
|
1087
|
+
const name = `${prefix}${p.name || p.id}`;
|
|
1088
|
+
if (!inDataflow(p.type) || p.type === "input") continue; // a note has no value; an input pane's is the item, its key, a value from outside or a port's
|
|
1089
|
+
if (!seen.has(name)) {
|
|
1090
|
+
seen.add(name);
|
|
1091
|
+
choices.push({ pane: name, type: p.type, depth });
|
|
1092
|
+
for (const o of outputsOf(p)) if (o.type !== "event" && !o.error && !o.setter) choices.push({ pane: name, output: o.name, type: p.type, depth });
|
|
1093
|
+
}
|
|
1094
|
+
if (p.type === "subgraph" && !p.each?.on) walk(graphPanes(p), `${name}/`, depth + 1);
|
|
1095
|
+
}
|
|
1096
|
+
};
|
|
1097
|
+
for (const t of [pane, ...Object.values(each.templates ?? {})]) walk(graphPanes(t), "", 0);
|
|
1098
|
+
return choices;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* Run exposed output `output` (outputsOf: `{ name, exposed: { pane, output? } }`)
|
|
1103
|
+
* of repeater pane `id`, once the repeater is done: an object by item key of
|
|
1104
|
+
* that pane's value (or its named output's) in each live instance, in item
|
|
1105
|
+
* order, stored as the output's value (`<id>#<name>`). An instance without
|
|
1106
|
+
* the pane, or where it didn't make a value this time (skipped, or a pane
|
|
1107
|
+
* that needs the screen), is left out. Resolves with the result, or a skip
|
|
1108
|
+
* when no template has a pane of that name.
|
|
1109
|
+
*/
|
|
1110
|
+
export async function runExposed(outer, id, output, { runId }) {
|
|
1111
|
+
const pane = outer.panes.get(id);
|
|
1112
|
+
const { pane: target, output: port } = output.exposed;
|
|
1113
|
+
const rep = outer.repeaters?.get(id);
|
|
1114
|
+
const live = [...(rep?.instances.values() ?? [])].filter((i) => i.state === "live").sort((a, b) => a.index - b.index);
|
|
1115
|
+
const items = [];
|
|
1116
|
+
let found = false;
|
|
1117
|
+
for (const inst of live) {
|
|
1118
|
+
const at = exposedIn(inst, target, port);
|
|
1119
|
+
if (!at) continue;
|
|
1120
|
+
found = true;
|
|
1121
|
+
if (at.state?.status !== "done") continue;
|
|
1122
|
+
items.push({ s: [`${id}${SEP}${inst.seg}${SEP}${at.key}`], k: keyName(inst.key, inst.index) });
|
|
1123
|
+
}
|
|
1124
|
+
if (!found && !templatesHave(pane, target)) return skip(`no pane named "${target}" in ${pane.name || id}'s template`);
|
|
1125
|
+
return await outer.runtime.gather({ id: portId(id, output.name), runId, items, keyed: true, view: "auto" });
|
|
1126
|
+
}
|