@turing-machine-js/visuals 7.0.0-alpha.6 → 7.0.0-alpha.7

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.
@@ -1,191 +0,0 @@
1
- import { bareIdOf, highlightExpand } from './graphUtils';
2
- /**
3
- * Pure highlight-rule evaluator. Given the current `highlight` (from
4
- * `MachineView`'s `$derived`), the engine `graph`, derived `indexes`,
5
- * and the previous strong-id (for pause-revisit pulse detection), emit
6
- * a sequence of `ops` calls describing the resulting visual state.
7
- *
8
- * Strictly additive — the caller is expected to clear previously-applied
9
- * highlight classes / edge marks / cluster activations BEFORE invoking
10
- * this function. The function never reads back from the consumer.
11
- *
12
- * Returns the new prev-strong-id to thread into the next call. Pulse
13
- * comparison uses the RAW strong id (not canonical), so wrapper-pause
14
- * and bare-pause register as different positions and don't pulse each
15
- * other. Updates only when `highlight.paused === true`; non-paused
16
- * events (idle / RUNNING_AUTO ticks) leave it untouched. Null highlight
17
- * resets it to null.
18
- *
19
- * See `docs/graph-highlight-and-breakpoints.md` for the 16 rules
20
- * enumerated.
21
- */
22
- export function applyHighlight(highlight, graph, indexes, prevStrongId, ops) {
23
- if (!highlight || !graph) {
24
- return { nextPrevStrongId: null };
25
- }
26
- // §5 Halt-target retargeting: real halt (id 0) reached from an in-frame
27
- // state retargets to the frame's halt marker (id = -frameId), so the
28
- // visible edge lands inside the cluster.
29
- let toId = highlight.toId;
30
- if (toId === 0 && typeof highlight.fromId === 'number') {
31
- const fromFrameId = indexes.nodeFrameMap.get(highlight.fromId);
32
- if (fromFrameId !== undefined)
33
- toId = -fromFrameId;
34
- }
35
- // §2 Equivalence-class expansion (asymmetric, via highlightExpand):
36
- // wrapper → [wrapper, bare] (joined visual pair for wrapper-entry pause)
37
- // bare → [bare] (engine genuinely on the bare; no wrapper sync)
38
- // From-side expansion only fires for positive numeric ids; the 'idle'
39
- // sentinel is handled directly below. Halt markers / singleton fall
40
- // through the direct-lookup branches.
41
- const fromEqIds = typeof highlight.fromId === 'number'
42
- ? highlightExpand(highlight.fromId, graph)
43
- : [];
44
- const toEqIds = toId !== null && toId > 0
45
- ? highlightExpand(toId, graph)
46
- : [];
47
- // §3 Class application — from side.
48
- if (highlight.fromId === 'idle') {
49
- ops.addNodeClass('idle', 'mg-highlight-from');
50
- if (highlight.strong === 'from')
51
- ops.addNodeClass('idle', 'mg-highlight-strong');
52
- }
53
- for (const id of fromEqIds) {
54
- ops.addNodeClass(id, 'mg-highlight-from');
55
- if (highlight.strong === 'from')
56
- ops.addNodeClass(id, 'mg-highlight-strong');
57
- }
58
- // §3 + §8 Class application — to side. Halt markers (toId < 0) and the
59
- // real halt singleton (toId === 0; only possible when §5 didn't retarget)
60
- // bypass the equivalence-class expansion via direct lookup.
61
- if (toId !== null && toId <= 0) {
62
- ops.addNodeClass(toId, 'mg-highlight-to');
63
- if (highlight.strong === 'to')
64
- ops.addNodeClass(toId, 'mg-highlight-strong');
65
- }
66
- for (const id of toEqIds) {
67
- ops.addNodeClass(id, 'mg-highlight-to');
68
- if (highlight.strong === 'to')
69
- ops.addNodeClass(id, 'mg-highlight-strong');
70
- }
71
- // Edge highlight: the data-id token form mermaid emits.
72
- const fromKey = highlight.fromId === 'idle' ? 'idle' : `s${highlight.fromId}`;
73
- const toKey = toId === null ? null
74
- : toId < 0 ? `c${-toId}` // halt marker
75
- : `s${toId}`;
76
- if (toKey !== null)
77
- ops.highlightEdge(fromKey, toKey);
78
- // §10 Wrapper-entry "call" edge: when to-side expanded to [wrapper, bare],
79
- // light up the wrapper→bare connector so the joined pair has a visible link.
80
- if (toEqIds.length > 1) {
81
- const wrapperId = toEqIds.find((id) => graph.nodes[id]?.isWrapper);
82
- const bareId = toEqIds.find((id) => !graph.nodes[id]?.isWrapper);
83
- if (wrapperId !== undefined && bareId !== undefined) {
84
- ops.highlightEdge(`s${wrapperId}`, `s${bareId}`);
85
- }
86
- }
87
- // §6 Source return chain: just-fired transition landed on a frame's
88
- // halt marker. Light up the post-pop trajectory before the next iter
89
- // moves the strong node.
90
- if (toId !== null && toId < 0) {
91
- const frameId = -toId;
92
- const wrappers = indexes.frameWrappersMap.get(frameId) ?? [];
93
- for (const { wrapperId, overrideId } of wrappers) {
94
- ops.highlightEdge(`w_${frameId}`, `s${wrapperId}`);
95
- ops.addNodeClass(wrapperId, 'mg-highlight-to');
96
- if (overrideId !== null) {
97
- ops.highlightEdge(`s${wrapperId}`, `s${overrideId}`);
98
- ops.addNodeClass(overrideId, 'mg-highlight-to');
99
- }
100
- }
101
- }
102
- // §7 Destination return chain: paused at a positive toId that's some
103
- // wrapper W's override AND fromId is in W's frame — the engine just
104
- // popped. The straight bare→override edge doesn't exist in the graph;
105
- // light up the actual visible path bare → halt-marker → return →
106
- // wrapper → override, plus the frame cluster.
107
- if (typeof highlight.fromId === 'number' && toId !== null && toId > 0) {
108
- const fromFrameId = indexes.nodeFrameMap.get(highlight.fromId);
109
- if (fromFrameId !== undefined) {
110
- const wrappers = indexes.frameWrappersMap.get(fromFrameId) ?? [];
111
- const matching = wrappers.filter((w) => w.overrideId === toId);
112
- if (matching.length > 0) {
113
- ops.addNodeClass(-fromFrameId, 'mg-highlight-to');
114
- ops.highlightEdge(`s${highlight.fromId}`, `c${fromFrameId}`);
115
- for (const { wrapperId } of matching) {
116
- ops.highlightEdge(`w_${fromFrameId}`, `s${wrapperId}`);
117
- ops.addNodeClass(wrapperId, 'mg-highlight-to');
118
- ops.highlightEdge(`s${wrapperId}`, `s${toId}`);
119
- }
120
- ops.markFrameActive(fromFrameId);
121
- }
122
- }
123
- }
124
- // §9 Frame-active for the strong node. Wrappers are outside any frame
125
- // so canonicalize via bareIdOf so the wrapper-entry pause still lights
126
- // up the bare's enclosing cluster.
127
- const strongId = highlight.strong === 'from' ? highlight.fromId : highlight.toId;
128
- const strongIdCanonical = typeof strongId === 'number'
129
- ? bareIdOf(strongId, graph)
130
- : strongId;
131
- if (typeof strongIdCanonical === 'number') {
132
- const frameId = indexes.nodeFrameMap.get(strongIdCanonical);
133
- if (frameId !== undefined)
134
- ops.markFrameActive(frameId);
135
- }
136
- // §11 Pulse on same-state revisit. Uses RAW strongId — wrapper-pause
137
- // and bare-pause are visually distinct positions even though they
138
- // share #debugRef; pausing at wrapper then continuing into bare must
139
- // not pulse. Idles never pulse and never update prevStrongId.
140
- if (highlight.paused
141
- && strongId !== null
142
- && strongId === prevStrongId
143
- && strongId !== undefined) {
144
- ops.pulse(strongId);
145
- }
146
- // Scroll-into-view target: for wrapper-entry pauses, scroll to the
147
- // BARE (not the wrapper) so the focus matches the displayed state
148
- // name. The worker's `resolveDisplayName` returns the bare's name
149
- // for wrapper iters (so the log reads "paused at walkToBlank ..."),
150
- // but `toId` is the wrapper's id and `highlightExpand` lights up
151
- // both nodes as strong. Without this canonicalization the scroll
152
- // lands on the wrapper while the log line and user's mental focus
153
- // are on the bare. Halt-related ids (≤ 0) are scrolled to as-is —
154
- // `bareIdOf` would collapse them all to the halt singleton, which
155
- // is structurally separate from the in-frame halt marker the user
156
- // is paused near.
157
- if (strongId !== null) {
158
- let scrollTarget = strongId;
159
- if (typeof strongId === 'number' && strongId > 0) {
160
- const node = graph.nodes[strongId];
161
- if (node?.isWrapper && node.bareStateId !== null) {
162
- scrollTarget = node.bareStateId;
163
- }
164
- }
165
- ops.scrollIntoView(scrollTarget);
166
- }
167
- const nextPrevStrongId = highlight.paused ? strongId : prevStrongId;
168
- return { nextPrevStrongId };
169
- }
170
- /**
171
- * Pure breakpoint-indicator rule evaluator. For each cached node key,
172
- * emit `ops.setBreakpoint(key, on)` reflecting whether the node's
173
- * canonical bare-id is in the `breakpoints` set.
174
- *
175
- * The 'idle' string sentinel never carries a breakpoint. All numeric
176
- * keys are valid BP-class members:
177
- * - positive id → regular state; canonical via bareIdOf (wrappers
178
- * collapse to bare)
179
- * - 0 → haltState singleton (engine-wide; canonical = 0)
180
- * - negative id → halt marker (per-frame visualization sentinel;
181
- * bareIdOf maps to 0 — same class as the singleton)
182
- * Consumers pass their iterable of cached node keys (e.g. `nodeCache.keys()`).
183
- */
184
- export function applyIndicator(breakpoints, graph, nodeIds, ops) {
185
- for (const key of nodeIds) {
186
- const on = typeof key === 'number'
187
- && graph !== null
188
- && breakpoints.has(bareIdOf(key, graph));
189
- ops.setBreakpoint(key, on);
190
- }
191
- }
package/dist/format.js DELETED
@@ -1,46 +0,0 @@
1
- import { movements, symbolCommands } from '@turing-machine-js/machine';
2
- export const MOVEMENT_LETTER = new Map([
3
- [movements.left, 'L'],
4
- [movements.right, 'R'],
5
- [movements.stay, 'S'],
6
- ]);
7
- /**
8
- * Render a single tape command in `WRITE/MOVE` form.
9
- * - Write: `'X'` (literal symbol) | `K` (keep) | `E` (erase = write blank).
10
- * - Move: `L` / `R` / `S` from `movements.*`.
11
- *
12
- * Matches the engine's edge-label vocabulary so formatted commands line up
13
- * with the write/move cells in `toMermaid`-emitted edge labels.
14
- */
15
- export function formatCommand(tapeCommand) {
16
- let write;
17
- if (tapeCommand.symbol === symbolCommands.keep) {
18
- write = 'K';
19
- }
20
- else if (tapeCommand.symbol === symbolCommands.erase) {
21
- write = 'E';
22
- }
23
- else {
24
- write = `'${tapeCommand.symbol}'`;
25
- }
26
- const move = MOVEMENT_LETTER.get(tapeCommand.movement) ?? '?';
27
- return `${write}/${move}`;
28
- }
29
- /**
30
- * Render one step's edge-label notation: `[reads] → [writes]/[moves]`.
31
- * Each role is wrapped in a single `[…]`; multi-tape entries are
32
- * comma-separated inside the brackets.
33
- *
34
- * Matches the engine's `toMermaid` emit so logged steps line up with
35
- * graph edge labels. Note: `nextSymbols` in `MachineState` is already
36
- * resolved (keep → current symbol, erase → blank) — `K` is inferred
37
- * by comparing `nextSymbols[i] === currentSymbols[i]`.
38
- */
39
- export function formatStep(m) {
40
- const reads = m.currentSymbols.map((s) => `'${s}'`).join(',');
41
- const writes = m.nextSymbols
42
- .map((s, i) => (s === m.currentSymbols[i] ? 'K' : `'${s}'`))
43
- .join(',');
44
- const moves = m.movements.map((mv) => MOVEMENT_LETTER.get(mv) ?? '?').join(',');
45
- return `[${reads}] → [${writes}]/[${moves}]`;
46
- }
@@ -1,58 +0,0 @@
1
- /**
2
- * Walk the engine graph once and build all derived lookups. Cheap;
3
- * intended to run on every Build (graph identity changes per build).
4
- */
5
- export function indexGraph(graph) {
6
- const nodeFrameMap = new Map();
7
- const frameWrappersMap = new Map();
8
- const frameLabelToId = new Map();
9
- if (!graph)
10
- return { nodeFrameMap, frameWrappersMap, frameLabelToId };
11
- for (const node of Object.values(graph.nodes)) {
12
- if (node.frameId !== null)
13
- nodeFrameMap.set(node.id, node.frameId);
14
- }
15
- // For each wrapper, append to its bare's frame entry. Multiple wrappers
16
- // can share the same bare with different overrides; we record them all
17
- // so the return-chain passes can highlight every candidate.
18
- for (const node of Object.values(graph.nodes)) {
19
- if (!node.isWrapper || node.bareStateId === null)
20
- continue;
21
- const bare = graph.nodes[node.bareStateId];
22
- if (!bare || bare.frameId === null)
23
- continue;
24
- const entry = { wrapperId: node.id, overrideId: node.overriddenHaltStateId };
25
- const arr = frameWrappersMap.get(bare.frameId);
26
- if (arr)
27
- arr.push(entry);
28
- else
29
- frameWrappersMap.set(bare.frameId, [entry]);
30
- }
31
- // Cluster label reconstruction: mirrors the engine's `toMermaid` emit
32
- // (`callable subtree of NAME` for single-bare frames, `callable scope:
33
- // A ∪ B ∪ …` for union frames; bare names sorted by id). Consumers
34
- // need this to map mermaid's rendered cluster (whose own SVG id is the
35
- // useless literal `[object Object]`) back to a frameId.
36
- const bareIds = new Set();
37
- for (const n of Object.values(graph.nodes)) {
38
- if (n.isWrapper && n.bareStateId !== null)
39
- bareIds.add(n.bareStateId);
40
- }
41
- const frameToBareNames = new Map();
42
- for (const n of Object.values(graph.nodes).sort((a, b) => a.id - b.id)) {
43
- if (n.isWrapper || n.isHaltMarker || n.frameId === null)
44
- continue;
45
- if (!bareIds.has(n.id))
46
- continue;
47
- const arr = frameToBareNames.get(n.frameId) ?? [];
48
- arr.push(n.name);
49
- frameToBareNames.set(n.frameId, arr);
50
- }
51
- for (const [frameId, names] of frameToBareNames) {
52
- const label = names.length > 1
53
- ? `callable scope: ${names.join(' ∪ ')}`
54
- : `callable subtree of ${names[0] ?? frameId}`;
55
- frameLabelToId.set(label, frameId);
56
- }
57
- return { nodeFrameMap, frameWrappersMap, frameLabelToId };
58
- }
@@ -1,76 +0,0 @@
1
- /**
2
- * Normalize an engine `GraphNode.id` to its canonical representative for
3
- * breakpoint-class lookups (machines-demo#37). Wrappers produced by
4
- * `State.withOverriddenHaltState` share `#debugRef` with their bare state
5
- * engine-side (turing-machine-js v7 `State.ts`: `state.#debugRef =
6
- * bare.#debugRef`), so they form a single breakpoint from the user's POV.
7
- * This collapses any wrapper id to its bare's id; non-wrapper ids return
8
- * self. Used so the demo can store ONE canonical id per equivalence class
9
- * in its breakpoint set, and expand to all class members for indicator
10
- * rendering — keeping the worker-side toggle count to one per class
11
- * (multiple toggles on the shared ref would double-flip).
12
- */
13
- export function bareIdOf(id, graph) {
14
- if (!graph)
15
- return id;
16
- // Halt markers (negative ids, one per frame) are visualization sentinels;
17
- // at runtime they all collapse to the haltState singleton (id 0). For
18
- // breakpoint purposes they're a single class — setting BP on any
19
- // halt-related node sets it on the global haltState.
20
- if (id < 0)
21
- return 0;
22
- const node = graph.nodes[id];
23
- if (node && node.isWrapper && node.bareStateId !== null) {
24
- return node.bareStateId;
25
- }
26
- return id;
27
- }
28
- /**
29
- * Asymmetric expansion for the highlight effect (machines-demo#37).
30
- * Wrapper → `[wrapper, bare]` (the wrapper-entry pause is visually joined
31
- * to its bare, since the user thinks of them as one call site).
32
- * Bare → `[bare]` only (when the engine is genuinely on the bare — e.g. a
33
- * loop iter — the wrapper is not the "active" state and shouldn't get the
34
- * strong highlight).
35
- * Non-wrapper / non-bare ids return `[id]`.
36
- */
37
- export function highlightExpand(id, graph) {
38
- if (!graph)
39
- return [id];
40
- const node = graph.nodes[id];
41
- if (node?.isWrapper && node.bareStateId !== null) {
42
- return [id, node.bareStateId];
43
- }
44
- return [id];
45
- }
46
- /**
47
- * All GraphNode ids in the same breakpoint equivalence class as `id`.
48
- * Symmetric — gives consumers the full list of nodes that share an engine
49
- * breakpoint, regardless of which class member is the input. Used by the
50
- * context-menu's "Shared with" info line so the user can see at a glance
51
- * which other nodes flip together.
52
- *
53
- * Halt class (canonical id 0): the halt singleton + every halt marker in
54
- * the graph. Wrapper/bare class: the bare + every wrapper pointing at it.
55
- * Singleton classes (regular states, idle sentinel proxies) return just
56
- * the input id.
57
- */
58
- export function equivalentIds(id, graph) {
59
- if (!graph)
60
- return [id];
61
- const canonical = bareIdOf(id, graph);
62
- if (canonical === 0) {
63
- const ids = [0];
64
- for (const node of Object.values(graph.nodes)) {
65
- if (node.isHaltMarker)
66
- ids.push(node.id);
67
- }
68
- return ids;
69
- }
70
- const result = new Set([canonical]);
71
- for (const node of Object.values(graph.nodes)) {
72
- if (node.isWrapper && node.bareStateId === canonical)
73
- result.add(node.id);
74
- }
75
- return [...result];
76
- }
@@ -1,36 +0,0 @@
1
- /**
2
- * Contract between the pure highlight logic (`applyHighlight`,
3
- * `applyIndicator`) and any consumer that actually renders the graph
4
- * (Svelte component, vanilla embed, server-side snapshot, etc.).
5
- *
6
- * The pure functions decide *what* should happen (which node gets a class,
7
- * which edge lights up, where to pulse); the consumer's `HighlightOps`
8
- * implementation decides *how* (DOM mutation, recording for tests, etc.).
9
- *
10
- * See `docs/graph-highlight-and-breakpoints.md` for the rules each
11
- * implementation must respect.
12
- */
13
- /**
14
- * Build a recording `HighlightOps` + `IndicatorOps` pair plus the shared
15
- * `record` array of calls in invocation order. Used by tests to assert
16
- * what the pure logic would have done without running a real DOM.
17
- *
18
- * Snapshot-friendly: the record contains only plain JSON-serializable
19
- * values (no DOM nodes, no function refs).
20
- */
21
- export function recordingOps() {
22
- const record = [];
23
- return {
24
- record,
25
- highlight: {
26
- addNodeClass(id, cls) { record.push({ op: 'addNodeClass', id, cls }); },
27
- highlightEdge(fromKey, toKey) { record.push({ op: 'highlightEdge', fromKey, toKey }); },
28
- markFrameActive(frameId) { record.push({ op: 'markFrameActive', frameId }); },
29
- pulse(id) { record.push({ op: 'pulse', id }); },
30
- scrollIntoView(id) { record.push({ op: 'scrollIntoView', id }); },
31
- },
32
- indicator: {
33
- setBreakpoint(id, on) { record.push({ op: 'setBreakpoint', id, on }); },
34
- },
35
- };
36
- }
package/dist/index.js DELETED
@@ -1,6 +0,0 @@
1
- export { recordingOps } from './highlightOps';
2
- export { bareIdOf, highlightExpand, equivalentIds } from './graphUtils';
3
- export { indexGraph } from './graphIndexes';
4
- export { applyHighlight, applyIndicator } from './applyHighlight';
5
- export { formatCommand, formatStep } from './format';
6
- export { recordSnippet } from './recordSnippet';
@@ -1,92 +0,0 @@
1
- import { haltState, } from '@turing-machine-js/machine';
2
- import { MOVEMENT_LETTER } from './format';
3
- import { bareIdOf } from './graphUtils';
4
- const DEFAULT_MAX_STEPS = 1000;
5
- function snapshotTapes(machine) {
6
- return machine.tapeBlock.tapes.map((t) => ({
7
- symbols: [...t.symbols],
8
- position: t.position,
9
- }));
10
- }
11
- function deriveCommands(m) {
12
- return m.movements.map((mv, i) => ({
13
- movement: MOVEMENT_LETTER.get(mv) ?? 'S',
14
- read: m.currentSymbols[i],
15
- // nextSymbols is already resolved (keep → current symbol, erase → blank);
16
- // when write === read the command was a keep (UI suppresses the flash).
17
- write: m.nextSymbols[i],
18
- }));
19
- }
20
- function deriveHighlight(m, graph) {
21
- return {
22
- fromId: bareIdOf(m.state.id, graph),
23
- toId: m.nextState === haltState ? 0 : m.nextState.id,
24
- strong: 'from',
25
- paused: false,
26
- };
27
- }
28
- /**
29
- * Record a full machine run into a `Snippet` — a self-contained playback
30
- * artifact suitable for embeds, articles, or landing-page panels.
31
- *
32
- * The returned snippet contains one frame per iteration plus a frame-0
33
- * initial-state snapshot. Recording stops when the machine halts or when
34
- * `maxSteps` iterations have been consumed (default 1000).
35
- *
36
- * Tape-timing note: `runStepByStep` yields BEFORE applying its command
37
- * (the command is applied after the yield resumes). The recorder uses a
38
- * one-step-delayed snapshot so each frame's `tape` reflects the
39
- * post-command state for that frame's iter.
40
- */
41
- export function recordSnippet(opts) {
42
- const { machine, initialState, graph, alphabets, name, maxSteps = DEFAULT_MAX_STEPS, log, } = opts;
43
- const frames = [
44
- { step: 0, tape: snapshotTapes(machine), highlight: null },
45
- ];
46
- // pending holds everything for the frame whose tape snapshot is not yet
47
- // available (because applyCommand hasn't fired yet). It is flushed at the
48
- // start of the NEXT iter (when the tape reflects the previous command) and
49
- // after the loop (when the final command has been applied).
50
- let pending = null;
51
- let prev = null;
52
- try {
53
- for (const m of machine.runStepByStep({ initialState, stepsLimit: maxSteps })) {
54
- // At this point applyCommand for the PREVIOUS iter has already run
55
- // (the generator called applyCommand before looping back to yield).
56
- // So the current tape state = post-command of the previous iter.
57
- if (pending !== null) {
58
- frames.push({ ...pending, tape: snapshotTapes(machine) });
59
- }
60
- const commands = deriveCommands(m);
61
- const highlight = deriveHighlight(m, graph);
62
- const logLine = log ? log(m, prev) : undefined;
63
- pending = {
64
- step: m.step,
65
- commands,
66
- highlight,
67
- ...(logLine !== undefined ? { log: logLine } : {}),
68
- };
69
- prev = m;
70
- }
71
- }
72
- catch (e) {
73
- // runStepByStep throws 'Long execution' when stepsLimit is hit.
74
- // At that point applyCommand for the last yielded iter has run, so the
75
- // tape is in the post-command state we want for the pending frame.
76
- if (!(e instanceof Error) || e.message !== 'Long execution') {
77
- throw e;
78
- }
79
- }
80
- // Flush the last pending frame. After the loop (or after the catch), the
81
- // tape reflects the post-command state of the final yielded iter.
82
- if (pending !== null) {
83
- frames.push({ ...pending, tape: snapshotTapes(machine) });
84
- }
85
- return {
86
- version: 1,
87
- ...(name !== undefined ? { name } : {}),
88
- graph,
89
- alphabets,
90
- frames,
91
- };
92
- }
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};