@hviana/sema 0.8.3 → 0.8.5

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.
Files changed (85) hide show
  1. package/AGENTS.md +11 -10
  2. package/README.md +17 -38
  3. package/dist/example/demo.js +85 -34
  4. package/dist/src/geometry.d.ts +0 -10
  5. package/dist/src/geometry.js +0 -12
  6. package/dist/src/meter.d.ts +11 -0
  7. package/dist/src/meter.js +11 -0
  8. package/dist/src/mind/articulation.js +1 -1
  9. package/dist/src/mind/attention.js +2 -1
  10. package/dist/src/mind/derivation.d.ts +201 -0
  11. package/dist/src/mind/derivation.js +327 -0
  12. package/dist/src/mind/graph-search.d.ts +2 -1
  13. package/dist/src/mind/graph-search.js +37 -15
  14. package/dist/src/mind/match.d.ts +2 -0
  15. package/dist/src/mind/match.js +2 -0
  16. package/dist/src/mind/mechanisms/alu.js +0 -2
  17. package/dist/src/mind/mechanisms/cast.d.ts +1 -5
  18. package/dist/src/mind/mechanisms/cast.js +15 -18
  19. package/dist/src/mind/mechanisms/confluence.d.ts +0 -3
  20. package/dist/src/mind/mechanisms/confluence.js +3 -9
  21. package/dist/src/mind/mechanisms/cover.js +17 -20
  22. package/dist/src/mind/mechanisms/extraction.d.ts +0 -1
  23. package/dist/src/mind/mechanisms/extraction.js +13 -8
  24. package/dist/src/mind/mechanisms/prefix-completion.js +0 -1
  25. package/dist/src/mind/mechanisms/recall.d.ts +0 -1
  26. package/dist/src/mind/mechanisms/recall.js +8 -9
  27. package/dist/src/mind/mechanisms/reference.js +3 -4
  28. package/dist/src/mind/pipeline-mechanism.d.ts +1 -4
  29. package/dist/src/mind/pipeline.js +106 -41
  30. package/dist/src/mind/rationale.d.ts +0 -11
  31. package/dist/src/mind/rationale.js +6 -32
  32. package/dist/src/mind/reasoning.d.ts +4 -30
  33. package/dist/src/mind/reasoning.js +183 -151
  34. package/dist/src/mind/types.js +6 -3
  35. package/docs/INDEX.md +23 -24
  36. package/docs/INVARIANTS.md +16 -17
  37. package/docs/architecture/bounded-reads.md +4 -4
  38. package/docs/architecture/closure.md +65 -0
  39. package/docs/architecture/commonality.md +27 -18
  40. package/docs/architecture/cost-model.md +5 -5
  41. package/docs/architecture/exact-vs-approximate.md +4 -4
  42. package/docs/architecture/factored-machinery.md +14 -14
  43. package/docs/architecture/mechanism-market.md +9 -9
  44. package/docs/architecture/meter.md +4 -5
  45. package/docs/architecture/store.md +2 -2
  46. package/docs/architecture/thresholds.md +1 -1
  47. package/docs/failures/tempting-but-wrong.md +11 -1
  48. package/docs/harness/gates.md +6 -6
  49. package/docs/mechanisms/cover.md +2 -2
  50. package/example/demo.ts +90 -37
  51. package/jsr.json +1 -1
  52. package/package.json +1 -1
  53. package/src/geometry.ts +0 -13
  54. package/src/meter.ts +11 -0
  55. package/src/mind/articulation.ts +0 -1
  56. package/src/mind/attention.ts +2 -1
  57. package/src/mind/derivation.ts +473 -0
  58. package/src/mind/graph-search.ts +37 -20
  59. package/src/mind/match.ts +2 -0
  60. package/src/mind/mechanisms/alu.ts +0 -2
  61. package/src/mind/mechanisms/cast.ts +17 -21
  62. package/src/mind/mechanisms/confluence.ts +3 -13
  63. package/src/mind/mechanisms/cover.ts +17 -20
  64. package/src/mind/mechanisms/extraction.ts +13 -9
  65. package/src/mind/mechanisms/prefix-completion.ts +0 -1
  66. package/src/mind/mechanisms/recall.ts +7 -9
  67. package/src/mind/mechanisms/reference.ts +2 -3
  68. package/src/mind/pipeline-mechanism.ts +1 -4
  69. package/src/mind/pipeline.ts +121 -46
  70. package/src/mind/rationale.ts +6 -36
  71. package/src/mind/reasoning.ts +208 -178
  72. package/src/mind/types.ts +5 -2
  73. package/test/133-the-decision-point-renders-the-state.test.mjs +204 -0
  74. package/test/134-the-law-explains-the-engines-own-refusal.test.mjs +237 -0
  75. package/test/135-one-law-any-producer.test.mjs +289 -0
  76. package/test/136-the-two-named-limits.test.mjs +205 -0
  77. package/test/137-the-law-lives-once-and-below.test.mjs +400 -0
  78. package/test/138-the-remainder-drains-only-what-a-move-declares.test.mjs +62 -0
  79. package/test/139-the-witness-is-engagement-not-explanation.test.mjs +51 -0
  80. package/test/140-irrelevant-supply-does-not-change-an-answer.test.mjs +48 -0
  81. package/test/141-the-question-is-paid-at-construction.test.mjs +98 -0
  82. package/test/36-already-answered-fusion.test.mjs +20 -2
  83. package/test/37-cluster-dispersion-fusion.test.mjs +30 -3
  84. package/test/38-reason-restate-guard.test.mjs +22 -2
  85. package/test/55-cost-meter.test.mjs +4 -1
@@ -0,0 +1,327 @@
1
+ // derivation.ts — the derivation unit and the one closure law it is governed by.
2
+ //
3
+ // THE LAW, in the quantities this engine already has:
4
+ //
5
+ // A derivation is CLOSED for a query when the structure it built accounts for
6
+ // the query's REMAINDER under the engine's own identity and admission rules,
7
+ // and every step is priced on the one ladder.
8
+ //
9
+ // It is written ONCE, here, because that is where a rule has to live — in the
10
+ // behaviour of the code and at the sites it governs (this repository's own
11
+ // history records the same conclusion: a law written as a document was deleted,
12
+ // and the reason given was that the rule belongs in the code). The tiers that
13
+ // can evaluate it ask it; the tiers that cannot say so and are named below.
14
+ //
15
+ // WHAT THE LAW READS, AND WHAT IT NEVER READS. It reads the state and the
16
+ // proposed continuation, and nothing else: no mechanism, no store, no context,
17
+ // no producer. Its whole purpose is that a product may be consumed by ANY
18
+ // transition the law admits, so that composition is the next transition of the
19
+ // same state rather than an operation with its own dispatch.
20
+ //
21
+ // THE TWO WITNESSES ARE THE LAYER'S. `contains` (does the transition's
22
+ // structure hold the product? a node in its tree, or one contiguous byte run)
23
+ // and `moves` (does it reach structure this derivation has not consumed?) are
24
+ // resolved by the layer that knows the structure, and handed in. The law never
25
+ // re-derives them, and never probes the store to ask: a predicate that would
26
+ // scan the store for every candidate would raise the cost of a step, which is
27
+ // why `traverse.ts`'s own admission predicate is LENT to the chart rather than
28
+ // called through it.
29
+ //
30
+ // THE UNIT: product, accounted, remainder, cost, and the two declarations a
31
+ // producer makes about its own result (`fixed`, `used`). No identity field
32
+ // (the product is content-addressed, so its identity is `resolve(product)` — a
33
+ // pure function, and a cache is not a field); no structure field (that is what
34
+ // identity is computed over); no frontier field (a continuation is computed by
35
+ // the layer that can offer one); no producer field (docs/architecture's own
36
+ // contract: `provenance` is observability and nothing compares it); and no
37
+ // count of any kind — no depth, hop, visited, once or cap.
38
+ //
39
+ // FIVE QUESTIONS, FIVE OWNERS — and no two of them may be answered by one fact:
40
+ //
41
+ // ENGAGEMENT does the step touch the question's material? `carries`: admits,
42
+ // consumes nothing
43
+ // PROGRESS does it reach structure not yet consumed? `reaches`: admits
44
+ // AND consumes what the
45
+ // step's window carries
46
+ // ACCOUNTING what does the step price? the span, into
47
+ // `accounted`
48
+ // CLOSURE is the question's material all accounted for? `closed` — the law
49
+ // TERMINATION why does the walk stop at all? the LAYER: its offer
50
+ // ends, or the law refuses
51
+ // one; never a depth
52
+ // CYCLES why is a loop not a walk? the LAYER's consumed
53
+ // set; the law only sees
54
+ // that a cycle never
55
+ // closes it (test/138.1)
56
+ //
57
+ // A step can progress without closing the derivation; a derivation can be closed
58
+ // with no further transition (the grounding built it closed); and neither of those
59
+ // is termination.
60
+ //
61
+ // THE REMAINDER TRAVELS UNCHANGED, and this was measured, not assumed: letting
62
+ // `advance` drain the witness made a step that engaged the gap CLOSE the
63
+ // derivation, the next step was admitted unconstrained, and test/110's
64
+ // three-link chain drifted one hop past its satisfying answer. A step ENGAGES
65
+ // the question's leftover; it does not consume it. Termination is therefore not
66
+ // the law's: it is the walker's cycle protection over a finite graph.
67
+ //
68
+ // TWO LIMITS ARE PROVED AND LEFT OUT (test/136 pins both, with the measurements):
69
+ // 1. the CHART cannot evaluate accounting — its interface has no parameter for
70
+ // it, and carrying it per chart item was measured and rejected. The chart
71
+ // evaluates the same law read off an item: identity is the item's `key`,
72
+ // continuation is the rule's existence, progress is the frontier advancing,
73
+ // closure is the goal test, and `fix` is `fixed`.
74
+ // 2. closure by the QUERY'S POSITION in the graph is not a term of the unit:
75
+ // `reason`'s echo guards stop a derivation although the remainder is not
76
+ // empty, and recall's reverse-recall tiers close it with an empty
77
+ // accounting. That is a fact about the asker's material in the store,
78
+ // available only to the layer holding it — recorded as a limit, not patched
79
+ // into the unit as a foreign key.
80
+ //
81
+ // LAYERING: this module imports `../bytes.js` only. It sits BELOW
82
+ // `graph-search.ts`, `match.ts`, `rationale.ts` and `pipeline.ts`, so each may
83
+ // ask it and none may be asked by it. The span algebra lives here too: it is
84
+ // the law's vocabulary — gap arithmetic over the asker's bytes — and not the
85
+ // tracer's, which is why it moved out of `rationale.ts`.
86
+ import { indexOf } from "../bytes.js";
87
+ // ── The span algebra: the law's vocabulary ──────────────────────────────────
88
+ /** The BYTE COUNT of a span list — what the currency calls `unaccounted` in
89
+ * `weight = moves + PASS·unaccounted`. ONE definition: this was four copies of
90
+ * the same `reduce` before the architecture audit collapsed them. */
91
+ export function unaccountedBytes(spans) {
92
+ let total = 0;
93
+ for (const [a, b] of spans)
94
+ total += b - a;
95
+ return total;
96
+ }
97
+ /** The `[start, end)` gaps of `[0, queryLen)` NOT covered by `accounted` — the
98
+ * union-of-spans reading the ladder prices at PASS per byte, and the raw
99
+ * material every closure decision measures. Clipped to the question, sorted
100
+ * and merged, so two overlapping spans account for their union once. */
101
+ export function unexplainedSpans(queryLen, accounted) {
102
+ const sorted = accounted
103
+ .map(([s, e]) => [Math.max(0, s), Math.min(queryLen, e)])
104
+ .filter(([s, e]) => e > s)
105
+ .sort((a, b) => a[0] - b[0]);
106
+ const gaps = [];
107
+ let reach = 0;
108
+ for (const [s, e] of sorted) {
109
+ if (s > reach)
110
+ gaps.push([reach, s]);
111
+ if (e > reach)
112
+ reach = e;
113
+ }
114
+ if (reach < queryLen)
115
+ gaps.push([reach, queryLen]);
116
+ return gaps;
117
+ }
118
+ /** THE REMAINDER: what no step has accounted for, with every span below one
119
+ * river-fold quantum dropped. `W` is the mind's own line between bridging
120
+ * punctuation and a substantive phrase — the same floor `liftedScaffolding`
121
+ * and the honesty-density bar use — so a remainder under it licenses nothing
122
+ * and blocks nothing. This is the law's measure. */
123
+ export function remainderOf(queryLen, explained, W) {
124
+ return unexplainedSpans(queryLen, explained).filter(([a, b]) => b - a >= W);
125
+ }
126
+ /** The window of `span` that `product` holds, or null: the ONE reading of
127
+ * coverage — used by {@link carries}, by the move branch, and by the GROUNDING
128
+ * when it decides what its answer has actually paid for. */
129
+ export function windowOf(span, product, query, W) {
130
+ const [a, b] = span;
131
+ for (let i = a; i + W <= b; i++) {
132
+ if (indexOf(product, query.subarray(i, i + W), 0) >= 0)
133
+ return [i, i + W];
134
+ }
135
+ return null;
136
+ }
137
+ /** PROGRESS, by coverage: the first member of `remainder` that `product` carries
138
+ * a whole quantum of, or null when it carries none. The window is taken from
139
+ * `query`, the asker's own bytes, so the test is "this product restates a
140
+ * quantum of what was left unaccounted", never a similarity score.
141
+ *
142
+ * One witness per step, deterministically the FIRST in remainder order: the
143
+ * measure stays a single member of a finite list, which is what makes the walk
144
+ * reviewable — and, because the remainder is not drained, it is the walker's
145
+ * cycle protection (not this) that terminates a chain. */
146
+ export function carries(remainder, product, query, W) {
147
+ for (const span of remainder) {
148
+ const window = windowOf(span, product, query, W);
149
+ if (window !== null)
150
+ return [{ span, window }];
151
+ }
152
+ return null;
153
+ }
154
+ // ── The restatement reading ─────────────────────────────────────────────────
155
+ /** Whether `bytes` RESTATES the question — says nothing the asker did not just
156
+ * say — and is therefore not an answer. This is a closure condition: a
157
+ * derivation whose product is already the question has added nothing, and the
158
+ * engine asks it in five places. ONE definition, asked everywhere, with the
159
+ * DIFFERENCES between those places supplied as WITNESSES by the caller — never
160
+ * as a mechanism or a producer. If this function ever needs to know who
161
+ * produced the bytes to decide, the right conclusion is that a witness is
162
+ * missing, not that it should dispatch.
163
+ *
164
+ * `floor` is one river-fold quantum: below it, byte overlap is chance, not
165
+ * evidence — the same line `identityBar`, the bridge's `attestedQ` and
166
+ * recognition's site floor all draw. `0` disables the floor, which is the
167
+ * reading the callers that ask before any structure exists use.
168
+ *
169
+ * THE THREE READINGS the callers need, and why each is a witness rather than a
170
+ * branch here:
171
+ *
172
+ * • `proper` — a PROPER part of the question (strictly shorter). This is the
173
+ * reading every tier that rejects a fragment uses.
174
+ * • `whole` — the EQUALITY reading: only "the answer IS the question" counts,
175
+ * because the caller has already handled a proper fragment elsewhere (a
176
+ * recall tier's own subspan tests).
177
+ * • `equate` — the response's own notion of "the same text" (whatever
178
+ * `src/canon.ts` equates: case, width, whitespace). A caller that has one
179
+ * passes it; a caller that does not gets the byte-exact reading. It is the
180
+ * same fallback `resolve` already makes when an exact lookup misses.
181
+ *
182
+ * The LITERAL EXEMPTION is deliberately NOT here: whether a span is the site's
183
+ * own bytes at its own position is the CALLER's knowledge, and a caller states
184
+ * it by not asking (see `segRestatesQuery` in types.ts, which returns false for
185
+ * a literal span before reaching this). */
186
+ export function restates(query, bytes, floor = 0, witnesses = {}) {
187
+ const equate = witnesses.equate ?? null;
188
+ const from = witnesses.from ?? 0;
189
+ const q = equate === null ? query : equate(query);
190
+ const b = equate === null ? bytes : equate(bytes);
191
+ if (b.length > q.length || b.length < floor)
192
+ return false;
193
+ if (witnesses.whole === true) {
194
+ return b.length === q.length && indexOf(q, b, from) >= 0;
195
+ }
196
+ if (witnesses.proper === true && b.length === q.length)
197
+ return false;
198
+ return indexOf(q, b, from) >= 0;
199
+ }
200
+ /** Whether the query span `[from, to)` lies inside a COMPLETED ASSISTANT TURN —
201
+ * material the engine has already produced, so it is context rather than
202
+ * something the asker is asserting. Recognition and attention still see the
203
+ * full transcript; what excludes these spans is the closure reading "this was
204
+ * already answered", and it is a closure reading rather than a budget: a window
205
+ * inside a prior reply is not a fresh constraint.
206
+ *
207
+ * ONE definition of it. `cursor` is the CALLER's own progress through `turns`
208
+ * (they are ascending and each caller scans its candidates in ascending order),
209
+ * so the amortised search is preserved exactly and a caller passes the same
210
+ * holder for a whole scan: extracting the reading must not cost the scan. */
211
+ export function insideAnsweredTurn(turns, cursor, from, to) {
212
+ while (cursor.at < turns.length && turns[cursor.at][1] <= from)
213
+ cursor.at++;
214
+ const turn = turns[cursor.at];
215
+ return turn !== undefined && turn[0] <= from && to <= turn[1];
216
+ }
217
+ /** CLOSED — nothing of the asker's material is left unaccounted. */
218
+ export function closed(d) {
219
+ return d.remainder.length === 0;
220
+ }
221
+ /** THE LAW, evaluated once.
222
+ *
223
+ * Returns the spans the transition accounts for — the witness that lets it be
224
+ * taken — or `null` when it is inadmissible. Evaluating it once and advancing
225
+ * the state with {@link advance} is the whole of a transition; asking twice for
226
+ * the same pair would repeat the scan, which this module must not make anyone
227
+ * do.
228
+ *
229
+ * ¬FIXED ∧ CONTAINS ∧ ( CLOSED ∨ CARRIES ∨ MOVES )
230
+ *
231
+ * Cost is not a term: it is the lattice order the market minimises over, and
232
+ * neither is any budget — a cap decides with a number of work, this decides
233
+ * with the remainder. */
234
+ export function admissible(d, t, query, W) {
235
+ if (d.fixed)
236
+ return null;
237
+ if (!t.contains)
238
+ return null;
239
+ if (closed(d))
240
+ return (t.explains ?? []).map((span) => ({ span }));
241
+ // MOVES BEFORE CARRIES, and that order is not arbitrary: a transition that
242
+ // DECLARES it moves (the walker's forward-absorb, and a pivot whose producing
243
+ // mechanism declared the anchors it speaks for) is admitted on that ground
244
+ // alone and records no coverage witness — which is exactly how the code it
245
+ // replaces behaved, where the brake was skipped whenever the producer owned
246
+ // the shape of its answer. Reading coverage first would attribute to such a
247
+ // step material it was never asked to account for.
248
+ if (t.reaches) {
249
+ // A MOVE IS ITS OWN GROUND — it needs no question material to be admitted.
250
+ // What it CARRIES is nonetheless the one thing the question's remainder may
251
+ // be consumed by, and it is read here by the SAME reading the carries
252
+ // admission uses: one definition, so the law cannot be told a span it does
253
+ // not hold. A move that carries nothing consumes nothing.
254
+ const carried = carries(d.remainder, t.product, query, W);
255
+ if (carried !== null)
256
+ return carried;
257
+ return (t.explains ?? []).map((span) => ({ span }));
258
+ }
259
+ return carries(d.remainder, t.product, query, W);
260
+ }
261
+ /** ADVANCE — the transition itself: the state that results from consuming `t`
262
+ * with the witness {@link admissible} returned. The product moves, the
263
+ * accounting accumulates what the step engaged, and the cost is added.
264
+ *
265
+ * THE REMAINDER DRAINS ONLY ON A DECLARED MOVE, and that is measured, not
266
+ * assumed: a step admitted by CARRIES holds question material, which any
267
+ * repetition also holds — draining on it let a cycle empty the remainder and
268
+ * close a derivation whose product never changed. A MOVE (structure this
269
+ * derivation has not consumed) cannot be produced by repetition, so it is the
270
+ * one evidence on which the question's leftover may be consumed. The result is
271
+ * no longer a supplied fixed point. */
272
+ function drain(remainder, explains) {
273
+ return remainder.flatMap(([a, b]) => {
274
+ const cut = explains.find(([x, y]) => x >= a && y <= b);
275
+ if (!cut)
276
+ return [[a, b]];
277
+ const [x, y] = cut;
278
+ const w = y - x;
279
+ const out = [];
280
+ if (x - a >= w)
281
+ out.push([a, x]);
282
+ if (b - y >= w)
283
+ out.push([y, b]);
284
+ return out;
285
+ });
286
+ }
287
+ export function advance(d, t, explains) {
288
+ const spans = explains.map((w) => w.span);
289
+ const carried = t.reaches === true
290
+ ? explains.flatMap((w) => (w.window ? [w.window] : []))
291
+ : [];
292
+ return {
293
+ product: t.product,
294
+ accounted: spans.length === 0 ? d.accounted : [...d.accounted, ...spans],
295
+ remainder: carried.length === 0 ? d.remainder : drain(d.remainder, carried),
296
+ cost: d.cost + t.cost,
297
+ used: d.used,
298
+ };
299
+ }
300
+ /** THE CLOSURE — the walk of {@link advance} over the continuations `offer`
301
+ * proposes, run until the layer has nothing further to offer or the law refuses
302
+ * the one it offered.
303
+ *
304
+ * ADMISSION is entirely the law's; TERMINATION is the layer's, and deliberately
305
+ * so. The remainder does not descend (draining it was implemented and refuted
306
+ * — see the module note), so a walk cannot run forever only because the layer
307
+ * offering continuations keeps its own cycle protection over a finite graph.
308
+ * Nothing here counts steps, and nothing here decides admissibility. */
309
+ export async function closure(d, query, W, offer,
310
+ /** Called for each step the law admits, with the state before and after. */
311
+ onTaken,
312
+ /** Called when the law refused the continuation the layer offered. */
313
+ onRefused) {
314
+ for (;;) {
315
+ const t = await offer(d);
316
+ if (t === null)
317
+ return d;
318
+ const explains = admissible(d, t, query, W);
319
+ if (explains === null) {
320
+ onRefused?.(d);
321
+ return d;
322
+ }
323
+ const next = advance(d, t, explains);
324
+ onTaken?.(d, next);
325
+ d = next;
326
+ }
327
+ }
@@ -186,7 +186,7 @@ export declare class GraphSearch {
186
186
  * Any learnt connector between two rewrites is spliced IN by the in-search
187
187
  * connector rule (see {@link outRules}), so the returned spans already carry
188
188
  * it — there is no post-pass. */
189
- cover(queryLen: number, sites: ReadonlyArray<Site>, conceptTarget: ReadonlyMap<number, number>, leaves: ReadonlyArray<Leaf>, splits: ReadonlySet<number>, starts: ReadonlySet<number>, substitutions?: ReadonlyMap<number, Uint8Array>, connectors?: ReadonlyMap<string, Uint8Array>, computedResults?: ReadonlyArray<ComputedResult>,
189
+ cover(queryLen: number, sites: ReadonlyArray<Site>, conceptTarget: ReadonlyMap<number, number>, leaves: ReadonlyArray<Leaf>, splits: ReadonlySet<number>, substitutions?: ReadonlyMap<number, Uint8Array>, connectors?: ReadonlyMap<string, Uint8Array>, computedResults?: ReadonlyArray<ComputedResult>,
190
190
  /** When given, receives each solved span's lightest derivation — the full
191
191
  * adapted A*LD proof tree as classified {@link DerivationStep}s — for the
192
192
  * TOP cover AND every nested completion the sink is threaded into (see
@@ -196,6 +196,7 @@ export declare class GraphSearch {
196
196
  onDerivation?: (steps: DerivationStep[]) => void): {
197
197
  segs: Seg[];
198
198
  cost: number;
199
+ moves: number;
199
200
  } | null;
200
201
  /** Build the deduction system for one span and return its lightest cover's
201
202
  * chosen spans — the SINGLE routine the query and every produced composite
@@ -51,6 +51,29 @@ function pushInto(mp, k, v) {
51
51
  /** Read the chosen spans back off a derivation: the goal is a chain of bridge
52
52
  * steps, each whose second premise is the `out` it crossed. Walk the chain to
53
53
  * the axiom and reverse into left-to-right order. */
54
+ /** The BYTE TERM of a derivation's cost: how many bytes its bridge rule charged
55
+ * at PASS each — read back off the rule that charged them (`bridgeRule`:
56
+ * `o.rec ? MICRO : PASS * (o.j - o.i)`), so the split exists in ONE place and
57
+ * the engine's generic accumulator is left alone.
58
+ *
59
+ * `derivation.cost - PASS * readBridgedBytes(derivation)` is therefore the
60
+ * derivation's DISCRETE work — the number a mechanism reports as `moves`, which
61
+ * the pipeline's one formula then prices together with `PASS * unaccounted`.
62
+ * The two readings of the byte term coincide by construction: the spans this
63
+ * sums are exactly the ones the chart could not recognise (`rec === false`),
64
+ * which are the spans the candidate leaves unaccounted. */
65
+ function readBridgedBytes(derivation) {
66
+ let bytes = 0;
67
+ let node = derivation;
68
+ while (node && node.rule) {
69
+ const o = node.premises[1]?.item;
70
+ if (o !== undefined && o.kind === "out" && o.rec === false) {
71
+ bytes += o.j - o.i;
72
+ }
73
+ node = node.premises[0];
74
+ }
75
+ return bytes;
76
+ }
54
77
  function readCover(derivation) {
55
78
  const segs = [];
56
79
  let node = derivation;
@@ -229,7 +252,7 @@ export class GraphSearch {
229
252
  * Any learnt connector between two rewrites is spliced IN by the in-search
230
253
  * connector rule (see {@link outRules}), so the returned spans already carry
231
254
  * it — there is no post-pass. */
232
- cover(queryLen, sites, conceptTarget, leaves, splits, starts, substitutions, connectors, computedResults,
255
+ cover(queryLen, sites, conceptTarget, leaves, splits, substitutions, connectors, computedResults,
233
256
  /** When given, receives each solved span's lightest derivation — the full
234
257
  * adapted A*LD proof tree as classified {@link DerivationStep}s — for the
235
258
  * TOP cover AND every nested completion the sink is threaded into (see
@@ -246,12 +269,7 @@ export class GraphSearch {
246
269
  // so the recompositions a produced form needs are reported in the same
247
270
  // trace instead of vanishing after the first layer.
248
271
  this.derivationSink = onDerivation;
249
- const solved = this.solve(queryLen, {
250
- sites,
251
- leaves,
252
- splits,
253
- starts,
254
- }, conceptTarget, substitutions, connectors, computedResults, onDerivation);
272
+ const solved = this.solve(queryLen, { sites, leaves, splits }, conceptTarget, substitutions, connectors, computedResults, onDerivation);
255
273
  // Deepening runs HERE, once, on the derivation the top cover CHOSE — never
256
274
  // inside the nested solve a completion runs. Nesting the deepening is what
257
275
  // made per-query cost track how densely the corpus interconnects the forms
@@ -259,9 +277,11 @@ export class GraphSearch {
259
277
  // continuations instead of following the chain the answer itself licensed.
260
278
  // With deepening only at the top, `recompleteNode` walks the accepted chain
261
279
  // one link at a time (its own memo and stack), so the work is the answer's.
262
- return solved === null
263
- ? null
264
- : { segs: this.deepen(solved.segs), cost: solved.cost };
280
+ return solved === null ? null : {
281
+ segs: this.deepen(solved.segs),
282
+ cost: solved.cost,
283
+ moves: solved.moves,
284
+ };
265
285
  }
266
286
  /** Build the deduction system for one span and return its lightest cover's
267
287
  * chosen spans — the SINGLE routine the query and every produced composite
@@ -277,7 +297,7 @@ export class GraphSearch {
277
297
  * decomposes, two recomposes, any mix — and stops only when it reaches a node
278
298
  * that leads nowhere new, never at an arbitrary count. */
279
299
  solve(spanLen, recognition, conceptTarget, substitutions, connectors, computedResults, onDerivation) {
280
- const system = this.buildSearch(spanLen, recognition.sites, conceptTarget, recognition.leaves, recognition.splits, recognition.starts, substitutions, connectors, computedResults);
300
+ const system = this.buildSearch(spanLen, recognition.sites, conceptTarget, recognition.leaves, recognition.splits, substitutions, connectors, computedResults);
281
301
  // Search-effort accounting (src/meter.ts): the chart's pops/pushes are
282
302
  // the cover's real cost, and a heuristic that stops being admissible
283
303
  // shows up as a pop count that explodes while the answer stays the same.
@@ -296,7 +316,11 @@ export class GraphSearch {
296
316
  onDerivation(readDerivation(derivation, substitutions !== undefined));
297
317
  }
298
318
  return derivation
299
- ? { segs: readCover(derivation), cost: derivation.cost }
319
+ ? {
320
+ segs: readCover(derivation),
321
+ cost: derivation.cost,
322
+ moves: derivation.cost - PASS * readBridgedBytes(derivation),
323
+ }
300
324
  : null;
301
325
  }
302
326
  /** Re-cover the CHOSEN fixpoint spans, in place.
@@ -330,7 +354,7 @@ export class GraphSearch {
330
354
  * adjacent fragments toward a known leaf (findLeaf) or branch (findBranch);
331
355
  * a completion fused with its neighbour may spell a deeper learned form the
332
356
  * flat probes can't name, recovered canonically by {@link resolve}. */
333
- buildSearch(queryLen, sites, conceptTarget, leaves, splits, starts, substitutions, connectors, computedResults) {
357
+ buildSearch(queryLen, sites, conceptTarget, leaves, splits, substitutions, connectors, computedResults) {
334
358
  const W = this.maxGroup; // fusible span ceiling (shortest composite bound)
335
359
  // Same corpus-scale hub floor {@link atomIsHub}/{@link atomReach} (traverse.ts)
336
360
  // derive for byte atoms — see {@link hubBound} below for why this module
@@ -473,7 +497,6 @@ export class GraphSearch {
473
497
  return this.outRules(it, {
474
498
  W,
475
499
  splits,
476
- starts,
477
500
  atomsAreHubs,
478
501
  coversDone,
479
502
  outsByStart,
@@ -914,7 +937,6 @@ export class GraphSearch {
914
937
  sites: [...recognised, ...structural],
915
938
  leaves: rec.leaves,
916
939
  splits: rec.splits,
917
- starts: rec.starts,
918
940
  }, new Map(), undefined, undefined, undefined, this.derivationSink);
919
941
  const answer = solved && concatBytes(solved.segs.map((s) => s.bytes));
920
942
  // ACCEPT, then CONTINUE THE CHAIN — but only along an accepted
@@ -294,6 +294,8 @@ export declare function haloSiblings(ctx: MindContext, id: number, halo?: Vec |
294
294
  * inside the composed phrase. Returns null when the corpus provides no
295
295
  * distributional evidence for the span. */
296
296
  export declare function spanHalo(ctx: MindContext, bytes: Uint8Array, from?: number, to?: number): Vec | null;
297
+ /** A TEST SURFACE: exported for the tests that pin the synonym strength ladder
298
+ * (they are its only consumers); nothing in `src/` calls it. */
297
299
  /** Distributional synonym evidence between arbitrary byte spans. Whole words
298
300
  * need not be independently interned: their stored W-window occurrences are
299
301
  * lifted to episode halos, bundled, and compared. The caller chooses the
@@ -814,6 +814,8 @@ export function spanHalo(ctx, bytes, from = 0, to = bytes.length) {
814
814
  }
815
815
  return found ? normalize(out) : null;
816
816
  }
817
+ /** A TEST SURFACE: exported for the tests that pin the synonym strength ladder
818
+ * (they are its only consumers); nothing in `src/` calls it. */
817
819
  /** Distributional synonym evidence between arbitrary byte spans. Whole words
818
820
  * need not be independently interned: their stored W-window occurrences are
819
821
  * lifted to episode halos, bundled, and compared. The caller chooses the
@@ -7,7 +7,6 @@
7
7
  // (`evalComputation`) are emitted inside its `parse()`. A user extension
8
8
  // joins the same way — see MindOptions.mechanismFactories.
9
9
  import { STEP } from "../graph-search.js";
10
- import { unexplainedLabel } from "../rationale.js";
11
10
  /** Wrap the ALU as a {@link PipelineMechanism}. */
12
11
  export function aluToMechanism(alu) {
13
12
  return {
@@ -31,7 +30,6 @@ export function aluToMechanism(alu) {
31
30
  bytes: u.bytes,
32
31
  accounted: [[u.i, u.j]],
33
32
  moves: STEP,
34
- unexplained: unexplainedLabel(query, [[u.i, u.j]]),
35
33
  }));
36
34
  },
37
35
  };
@@ -9,10 +9,6 @@ export interface CastResult {
9
9
  used: ReadonlySet<number>;
10
10
  accounted: Array<[number, number]>;
11
11
  moves: number;
12
- /** A human-readable label for the query bytes this schema left
13
- * unexplained — purely diagnostic, never priced (see the module's
14
- * Task 2 note in pipeline.ts's Candidate interface). */
15
- unexplained: string;
16
12
  }
17
13
  /** The seat that establishes a node's role in an analogical comparison:
18
14
  * the REVERSE context (what leads to it) when a predecessor genuinely
@@ -48,7 +44,7 @@ export interface CastResult {
48
44
  * describes id ("...painted by Leonardo da Vinci." contains "Leonardo da
49
45
  * Vinci"). An incidental adjacency predecessor never does — it merely
50
46
  * preceded id in some unrelated document without ever mentioning it. No
51
- * new tuned constant: containment is the same primitive `restatesQuery`
47
+ * new tuned constant: containment is the same primitive `restates`
52
48
  * and `dominates`-style checks already use throughout this codebase.
53
49
  *
54
50
  * `allowForward` (default true) gates the FORWARD branch specifically —
@@ -14,11 +14,10 @@ import { read } from "../primitives.js";
14
14
  import { argmaxBy, corpusN, edgeAncestors, hubBound, sharedReachMemo, } from "../traverse.js";
15
15
  import { analogyStrength, follow, project, reverseContext, sharedFrameStrengthOf, } from "../match.js";
16
16
  import { joinWithBridge } from "../resonance.js";
17
- import { restatesQuery } from "../reasoning.js";
18
17
  import { CONCEPT, STEP } from "../graph-search.js";
19
18
  import { indexOf } from "../../bytes.js";
20
19
  import { consensusFloor, dominates } from "../../geometry.js";
21
- import { unexplainedLabel, unexplainedSpans, } from "../rationale.js";
20
+ import { restates, unexplainedSpans } from "../derivation.js";
22
21
  import { rItem, rNode } from "../trace.js";
23
22
  import { dismissedKnownContent } from "../bridge.js";
24
23
  import { leafIdRun } from "../canonical.js";
@@ -89,7 +88,7 @@ const MIN_WEAVE = 2;
89
88
  * describes id ("...painted by Leonardo da Vinci." contains "Leonardo da
90
89
  * Vinci"). An incidental adjacency predecessor never does — it merely
91
90
  * preceded id in some unrelated document without ever mentioning it. No
92
- * new tuned constant: containment is the same primitive `restatesQuery`
91
+ * new tuned constant: containment is the same primitive `restates`
93
92
  * and `dominates`-style checks already use throughout this codebase.
94
93
  *
95
94
  * `allowForward` (default true) gates the FORWARD branch specifically —
@@ -421,7 +420,6 @@ export async function counterfactualTransfer(ctx, query, pre) {
421
420
  used: used ?? new Set(),
422
421
  accounted,
423
422
  moves,
424
- unexplained: unexplainedLabel(query, accounted),
425
423
  });
426
424
  };
427
425
  ctx.trace?.step("alignStructures", [rItem(query, "query")], points.map((p) => rNode(ctx, p.anchor, "structure", p.vote)), "the independent learnt structures the query weaves, by graded alignment");
@@ -522,7 +520,7 @@ export async function counterfactualTransfer(ctx, query, pre) {
522
520
  let answer = await joinWithBridge(ctx, filler, tail);
523
521
  const fwd = await follow(ctx, proj.anchor, qv);
524
522
  if (fwd !== null && indexOf(answer, fwd, 0) < 0 &&
525
- !restatesQuery(query, fwd)) {
523
+ !restates(query, fwd, 0, { proper: true })) {
526
524
  // THROUGH THE SHARED JOINER, not a bare concatenation.
527
525
  //
528
526
  // `joinWithBridge` is the composition step every out-of-search assembly
@@ -588,7 +586,7 @@ export async function counterfactualTransfer(ctx, query, pre) {
588
586
  // cap the test reads "none of the established continuations appears".
589
587
  const domNext = ctx.store.nextFirst(dominant.anchor, hubBound(ctx));
590
588
  const displaced = domNext
591
- .every((n) => indexOf(query, read(ctx, n), 0) < 0);
589
+ .every((n) => !restates(query, read(ctx, n), 0));
592
590
  // The SUBSTITUTE is what redirection speaks — the answer IS `project(last)`,
593
591
  // its own fact — so the same bar applies. The displaced structure is only
594
592
  // recognised as the slot being overridden and is never voiced, so it is
@@ -658,7 +656,7 @@ export async function counterfactualTransfer(ctx, query, pre) {
658
656
  if (queryScale(p.ctx.length) &&
659
657
  indexOf(dominant.ctx, p.ctx, 0) < 0 &&
660
658
  indexOf(p.ctx, dominant.ctx, 0) < 0 &&
661
- indexOf(query, p.ctx, 0) < 0) {
659
+ !restates(query, p.ctx, 0)) {
662
660
  analogs.push({ anchor: p.anchor, point: p, src: p });
663
661
  }
664
662
  // Reach through to the point's continuation targets regardless
@@ -675,7 +673,7 @@ export async function counterfactualTransfer(ctx, query, pre) {
675
673
  if (!queryScale(nctx.length) ||
676
674
  indexOf(dominant.ctx, nctx, 0) >= 0 ||
677
675
  indexOf(nctx, dominant.ctx, 0) >= 0 ||
678
- indexOf(query, nctx, 0) >= 0)
676
+ restates(query, nctx, 0))
679
677
  continue;
680
678
  analogs.push({ anchor: nid, point: null, src: p });
681
679
  }
@@ -1132,15 +1130,15 @@ export async function counterfactualTransfer(ctx, query, pre) {
1132
1130
  // consulting it here is the same fallback `resolve` already makes when an
1133
1131
  // exact content lookup misses, and it keeps this mechanism from carrying
1134
1132
  // any idea of its own about what a character is.
1135
- const echoesQuery = (x) => {
1136
- if (restatesQuery(query, x))
1137
- return true;
1138
- const canon = ctx.canon;
1139
- if (canon === null)
1140
- return false;
1141
- const cq = canon(query), cx = canon(x);
1142
- return cx.length < cq.length && indexOf(cq, cx, 0) >= 0;
1143
- };
1133
+ // TWO WITNESSES, ONE LAW: the byte reading, and — when the response carries
1134
+ // one — the same reading under the response's own equivalence. Asked twice
1135
+ // rather than branched on, because the equivalence is a property of the
1136
+ // injected canonicalizer (a substring-monotone function is the usual case,
1137
+ // not a guarantee), and the two readings are OR-ed here exactly as they were
1138
+ // before this became one definition.
1139
+ const echoesQuery = (x) => restates(query, x, 0, { proper: true }) ||
1140
+ (ctx.canon !== null &&
1141
+ restates(query, x, 0, { equate: ctx.canon, proper: true }));
1144
1142
  if (echoesQuery(b)) {
1145
1143
  const fwd = await follow(ctx, bestAnalog.anchor, qv);
1146
1144
  if (fwd !== null && fwd.length > 0 && !echoesQuery(fwd))
@@ -1240,7 +1238,6 @@ export const castMechanism = {
1240
1238
  accounted: c.accounted,
1241
1239
  moves: c.moves,
1242
1240
  used: c.used,
1243
- unexplained: c.unexplained,
1244
1241
  }));
1245
1242
  },
1246
1243
  };
@@ -10,9 +10,6 @@ export interface JoinResult {
10
10
  used: ReadonlySet<number>;
11
11
  accounted: Array<[number, number]>;
12
12
  moves: number;
13
- /** A human-readable label for the query bytes the meet left unexplained —
14
- * purely diagnostic, never priced. */
15
- unexplained: string;
16
13
  }
17
14
  /** The main confluence entry point. Given a query, detect whether it weaves
18
15
  * two or more INDEPENDENT constraints (ranked anchors supported by disjoint