@hviana/sema 0.8.9 → 0.9.1

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 (111) hide show
  1. package/AGENTS.md +7 -7
  2. package/dist/src/alu/src/index.d.ts +1 -1
  3. package/dist/src/alu/src/index.js +1 -1
  4. package/dist/src/alu/src/parser.js +2 -6
  5. package/dist/src/alu/src/resonance.d.ts +13 -0
  6. package/dist/src/alu/src/resonance.js +41 -0
  7. package/dist/src/alu/test/alu.test.js +39 -0
  8. package/dist/src/bytes.d.ts +6 -2
  9. package/dist/src/bytes.js +10 -4
  10. package/dist/src/canon.js +44 -0
  11. package/dist/src/geometry.d.ts +19 -1
  12. package/dist/src/geometry.js +125 -141
  13. package/dist/src/meter.d.ts +27 -0
  14. package/dist/src/meter.js +28 -1
  15. package/dist/src/mind/articulation.js +14 -1
  16. package/dist/src/mind/attention.d.ts +12 -0
  17. package/dist/src/mind/attention.js +44 -16
  18. package/dist/src/mind/bridge.js +3 -3
  19. package/dist/src/mind/derivation.d.ts +40 -0
  20. package/dist/src/mind/derivation.js +34 -0
  21. package/dist/src/mind/graph-search.d.ts +89 -15
  22. package/dist/src/mind/graph-search.js +345 -174
  23. package/dist/src/mind/learning.js +1 -1
  24. package/dist/src/mind/mechanisms/cover.d.ts +19 -3
  25. package/dist/src/mind/mechanisms/cover.js +101 -58
  26. package/dist/src/mind/mechanisms/recall.js +0 -1
  27. package/dist/src/mind/mind.js +2 -2
  28. package/dist/src/mind/pipeline.d.ts +5 -1
  29. package/dist/src/mind/pipeline.js +175 -87
  30. package/dist/src/mind/primitives.d.ts +25 -5
  31. package/dist/src/mind/primitives.js +107 -44
  32. package/dist/src/mind/reasoning.d.ts +18 -4
  33. package/dist/src/mind/reasoning.js +445 -321
  34. package/dist/src/mind/recognition.js +55 -73
  35. package/dist/src/mind/resonance.js +1 -11
  36. package/dist/src/mind/traverse.d.ts +3 -3
  37. package/dist/src/mind/traverse.js +3 -3
  38. package/dist/src/mind/types.d.ts +7 -1
  39. package/dist/src/store-sqlite.d.ts +25 -0
  40. package/dist/src/store-sqlite.js +89 -1
  41. package/dist/src/store.d.ts +48 -4
  42. package/dist/src/store.js +86 -6
  43. package/docs/INDEX.md +18 -18
  44. package/docs/INVARIANTS.md +16 -16
  45. package/docs/architecture/bounded-reads.md +1 -1
  46. package/docs/architecture/caches.md +5 -4
  47. package/docs/architecture/closure.md +45 -5
  48. package/docs/architecture/cost-model.md +16 -0
  49. package/docs/architecture/factored-machinery.md +14 -13
  50. package/docs/architecture/fold-contract.md +51 -1
  51. package/docs/architecture/mechanism-market.md +21 -0
  52. package/docs/architecture/memoization.md +3 -3
  53. package/docs/architecture/meter.md +2 -1
  54. package/docs/architecture/saturation.md +12 -0
  55. package/docs/architecture/store.md +25 -2
  56. package/docs/failures/tempting-but-wrong.md +13 -2
  57. package/docs/harness/gates.md +12 -10
  58. package/docs/mechanisms/cover.md +23 -6
  59. package/jsr.json +1 -1
  60. package/package.json +1 -1
  61. package/src/alu/README.md +10 -2
  62. package/src/alu/src/index.ts +1 -0
  63. package/src/alu/src/parser.ts +6 -6
  64. package/src/alu/src/resonance.ts +42 -0
  65. package/src/alu/test/alu.test.ts +40 -0
  66. package/src/bytes.ts +13 -3
  67. package/src/canon.ts +40 -0
  68. package/src/geometry.ts +183 -154
  69. package/src/meter.ts +28 -1
  70. package/src/mind/articulation.ts +14 -2
  71. package/src/mind/attention.ts +47 -25
  72. package/src/mind/bridge.ts +3 -3
  73. package/src/mind/derivation.ts +77 -0
  74. package/src/mind/graph-search.ts +449 -221
  75. package/src/mind/learning.ts +1 -7
  76. package/src/mind/match.ts +1 -2
  77. package/src/mind/mechanisms/cast.ts +1 -2
  78. package/src/mind/mechanisms/cover.ts +149 -84
  79. package/src/mind/mechanisms/extraction.ts +1 -2
  80. package/src/mind/mechanisms/prefix-completion.ts +1 -1
  81. package/src/mind/mechanisms/recall.ts +1 -3
  82. package/src/mind/mechanisms/reference.ts +1 -1
  83. package/src/mind/mind.ts +5 -30
  84. package/src/mind/pipeline.ts +206 -102
  85. package/src/mind/primitives.ts +119 -43
  86. package/src/mind/reasoning.ts +558 -413
  87. package/src/mind/recognition.ts +49 -65
  88. package/src/mind/resonance.ts +2 -16
  89. package/src/mind/trace.ts +1 -1
  90. package/src/mind/traverse.ts +3 -3
  91. package/src/mind/types.ts +9 -11
  92. package/src/store-sqlite.ts +92 -1
  93. package/src/store.ts +113 -7
  94. package/test/105-derive-through-reports-its-refusal.test.mjs +8 -5
  95. package/test/106-the-join-fires.test.mjs +21 -0
  96. package/test/111-the-cover-assembly-is-counted.test.mjs +8 -5
  97. package/test/128-the-leads-somewhere-pair-agrees.test.mjs +18 -12
  98. package/test/136-the-two-named-limits.test.mjs +3 -2
  99. package/test/137-the-law-lives-once-and-below.test.mjs +21 -0
  100. package/test/148-exact-shortcuts-agree.test.mjs +188 -0
  101. package/test/149-the-closure-engine.test.mjs +138 -0
  102. package/test/150-the-join-is-output-sensitive.test.mjs +66 -0
  103. package/test/151-the-cover-pays-for-what-it-reaches.test.mjs +142 -0
  104. package/test/152-the-read-side-names-as-the-write-side.test.mjs +146 -0
  105. package/test/153-a-cheaper-bound-is-looked-at-first.test.mjs +155 -0
  106. package/test/24-generalization.test.mjs +32 -0
  107. package/test/36-bloom.test.mjs +53 -0
  108. package/test/37-cluster-dispersion-fusion.test.mjs +75 -0
  109. package/test/48-recognise-turn-connective.test.mjs +3 -2
  110. package/test/55-cost-meter.test.mjs +4 -4
  111. package/test/90-connector-read-cap.test.mjs +7 -7
@@ -3,15 +3,31 @@
3
3
  // reason — extend an answer forward across facts (multi-hop)
4
4
  // fuseAttention — fuse independent points of attention (multi-topic)
5
5
  import { rItem, rNode } from "./trace.js";
6
- import { bytesEqual, indexOf } from "../bytes.js";
7
- import { resolve } from "./primitives.js";
6
+ import { bytesEqual, indexOf, latin1 } from "../bytes.js";
7
+ import { read, resolve } from "./primitives.js";
8
+ import { countClusters } from "./attention.js";
8
9
  import { hubBound } from "./traverse.js";
9
10
  import { containsSpan, follow, haloSiblings, project } from "./match.js";
10
11
  import { joinWithBridge, pivotInto } from "./resonance.js";
11
- import { admissible, advance } from "./derivation.js";
12
- import { closure, restates, unaccountedBytes, } from "./derivation.js";
12
+ import { closeOver, restates, unaccountedBytes, } from "./derivation.js";
13
13
  import { STEP } from "./graph-search.js";
14
- /** Extend a grounded answer forward across facts (multi-hop reasoning).
14
+ /** What one ADMITTED transition did to the question, published on the meter:
15
+ * the bytes its witnesses CARRIED and the remainder the move DRAINED. The two
16
+ * transitions that go through the law — the walk's steps and the fusion —
17
+ * report through this one function, so the two counters have one spelling.
18
+ * Write-only (meter.ts contract 1). */
19
+ function meterTransition(ctx, before, after, witnesses) {
20
+ if (!ctx.meter)
21
+ return;
22
+ for (const w of witnesses) {
23
+ if (w.window)
24
+ ctx.meter.reasonCarriedBytes += w.window[1] - w.window[0];
25
+ }
26
+ ctx.meter.closureDrainedBytes += unaccountedBytes(before.remainder) -
27
+ unaccountedBytes(after.remainder);
28
+ }
29
+ /** Extend a grounded answer forward across facts (multi-hop reasoning) — the
30
+ * WALK, run as one layer of the closure engine ({@link walkLayer}).
15
31
  * Pivots on the longest unconsumed learnt context each answer contains,
16
32
  * then follows the pivot's continuation to the next fact. **The chain ends
17
33
  * when it STOPS, never when a count runs out**: every exit is a refusal (no
@@ -26,28 +42,24 @@ import { STEP } from "./graph-search.js";
26
42
  * when it declared one — see the pivot's own containment rule. `pre` is the
27
43
  * response's shared pre-computation — the post-grounding stages read the
28
44
  * same container the mechanisms did. */
29
- export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
30
- const answer = d0.product;
31
- /** The query material the GROUNDING left uncovered — the state's own
32
- * remainder. Only the reasoner's OWN extensions are judged against it; a
33
- * mechanism carrying its own `used` set owns its shape. */
34
- const uncovered = d0.remainder;
35
- // Echo guard: a query that is ITSELF a learnt continuation (some context's
36
- // answer) is being asked back at the system — hopping forward from it would
37
- // chain through the very fact that produced it and echo the conversation
38
- // back. The grounded answer alone is the honest read-out. Deliberately a
39
- // broad structural gate; pinned by test/31-audit.
40
- const qId = pre.queryResolved;
41
- if (qId !== null && ctx.store.prevCount(qId) > 0) {
42
- // THE ECHO GUARD, NAMED — the audit's point 1 could not attribute two real
43
- // walks that stopped with no note at all, and this is where one of them went:
44
- // the query IS a learnt continuation, so the reasoner returns the grounded
45
- // read-out and never offers anything. Without this note the walk looks like
46
- // an unexplained stop; with it, "no note" can only mean the offer was never
47
- // asked. No decision changes: the guard returns exactly where it did.
48
- ctx.trace?.step("echoGuard", [rItem(query, "query")], [], "the query is itself a learnt continuation — answering the grounded read-out without walking");
49
- return d0;
50
- }
45
+ export function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
46
+ return closeOver(d0, query, ctx.space.maxGroup, [
47
+ walkLayer(ctx, query, preConsumed, pre, voiced),
48
+ ]);
49
+ }
50
+ /** The multi-hop walk as a {@link ClosureLayer}: it OFFERS a forward absorb or a
51
+ * pivot, the law admits. Its preamble — the two guards that say the question
52
+ * was already answered by the query's own position in the graph, and the
53
+ * pre-consumption of what the grounding spoke for — runs on the FIRST offer,
54
+ * against the state the engine hands it; a guard that fires makes that first
55
+ * offer `null`, so the walk ends where it begins and reports nothing more. */
56
+ export function walkLayer(ctx, query, preConsumed, pre, voiced = []) {
57
+ /** The preamble ran (on the first offer), and whether a guard stopped it. */
58
+ let begun = false;
59
+ let stopped = false;
60
+ let groundedId = null;
61
+ let groundedPrev = null;
62
+ let startedFrom = new Uint8Array(0);
51
63
  // Consume a node and its neighbours for pivot-cycle prevention — CAPPED at
52
64
  // the hub bound, via the store's LIMITed edge reads: a common continuation's
53
65
  // reverse fan-in (and a hub context's forward fan-out) is corpus-sized, and
@@ -56,59 +68,9 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
56
68
  // read order); a pivot suppressed only by a beyond-cap neighbour may now
57
69
  // fire — the same visibility trade chooseNext documents.
58
70
  const bound = hubBound(ctx);
59
- // ANSWERED DIRECTLY — the echo guard's other half, and the same principle:
60
- // the QUERY's own position in the graph, not the answer's content, says the
61
- // read-out is complete. Above: the query is itself a learnt CONTINUATION.
62
- // Here: the query is a learnt CONTEXT and the grounded answer is one of ITS
63
- // OWN continuations. Either way the question was answered directly and there
64
- // is nothing left to chain for.
65
- //
66
- // Every stopping condition in the loop below judges the ANSWER (`consumed` /
67
- // the law's `restates` / `bytesEqual`); none asks whether the QUESTION was
68
- // satisfied. So a single-hop question whose answer happens to name another
69
- // learnt context extends past a correct answer and REPLACES it:
70
- //
71
- // asked "<subj> father"
72
- // hop 1 "The father of <subj> is Ernest I of Anhalt-Dessau." <- correct
73
- // pivot "Ernest I of Anhalt-Dessau" <- a learnt context too
74
- // got "The date of death of Ernest I of Anhalt-Dessau is 12 June 1516."
75
- //
76
- // Any store holding a bare-entity context alongside a relation fact has that
77
- // shape; it is not exotic.
78
- //
79
- // Checked ONCE, before the loop, and ahead of BOTH extension branches:
80
- // `absorbForward` extends the answer too, and nothing about the defect is
81
- // specific to pivoting, so a guard between them would gate one arbitrary half
82
- // of the same step. Hop 0 is also the only hop at which the question can be
83
- // answered directly at all — after a hop, `cur` is no longer the query's own
84
- // continuation, so re-testing per hop could only cost reads.
85
- //
86
- // Read from the ANSWER's side (`prevFirst`) rather than the query's
87
- // (`nextFirst`). Same relation, but a CONTEXT's fan-out is hub-sized while
88
- // this is one answer's establishing-context fan-in. Both the resolve and the
89
- // reverse read are exactly what hop 0 of the loop below would perform, so
90
- // they are computed ONCE here and handed down (`groundedId`, `groundedPrev`)
91
- // — the guard then costs nothing when it does not fire. Stated because the
92
- // naive placement does NOT: `resolve` re-folds the answer bytes on every call
93
- // (no memo) and `prevFirst` is a direct read (no memo), so a guard that
94
- // recomputed them would add one fold plus one √N-bounded read per ask.
95
- // The √N cap carries the file-wide visibility trade, and fails SAFE in the
96
- // direction that matters: a missed guard costs an over-extended answer, never
97
- // a suppressed chain.
98
- //
99
- // A genuine multi-hop query is not a deposited context at all ("What is the
100
- // capital of the country of Eiffel Tower?" resolves to nothing), so this can
101
- // never gate a real chain.
102
- const groundedId = resolve(ctx, answer);
103
- const groundedPrev = groundedId === null
104
- ? null
105
- : ctx.store.prevFirst(groundedId, bound);
106
- if (qId !== null && groundedPrev !== null && groundedPrev.includes(qId)) {
107
- return d0;
108
- }
109
71
  const consumed = new Set();
110
72
  /** `prev` lets a caller hand in an already-read reverse-edge list — hop 0
111
- * reuses the guard's, above, instead of re-reading it. */
73
+ * reuses the guard's, below, instead of re-reading it. */
112
74
  const consumeNode = (id, prev) => {
113
75
  if (id === null)
114
76
  return;
@@ -123,31 +85,103 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
123
85
  for (const n of ctx.store.nextFirst(id, bound))
124
86
  consumed.add(n);
125
87
  };
126
- // Pre-consume whatever the grounding stage already spoke for. The halo
127
- // sweep is one ANN query per node — cap it at haloQueryK sweeps (cover
128
- // grounding can pre-consume one node per recognised site, O(query length));
129
- // nodes past the cap are still consumed directly, they just skip the
130
- // synonym expansion.
131
- const preconsume = async () => {
132
- let haloSweeps = 0;
133
- for (const id of preConsumed) {
134
- consumeNode(id);
135
- if (haloSweeps >= ctx.cfg.haloQueryK)
136
- continue;
137
- const h = ctx.store.halo(id);
138
- if (!h)
139
- continue;
140
- haloSweeps++;
141
- for (const sib of await haloSiblings(ctx, id, h))
142
- consumeNode(sib.id);
88
+ /** THE PREAMBLE — true when the walk may proceed. */
89
+ const begin = async (d0) => {
90
+ const answer = d0.product;
91
+ startedFrom = answer;
92
+ // Echo guard: a query that is ITSELF a learnt continuation (some context's
93
+ // answer) is being asked back at the system — hopping forward from it would
94
+ // chain through the very fact that produced it and echo the conversation
95
+ // back. The grounded answer alone is the honest read-out. Deliberately a
96
+ // broad structural gate; pinned by test/31-audit.
97
+ const qId = pre.queryResolved;
98
+ if (qId !== null && ctx.store.prevCount(qId) > 0) {
99
+ // THE ECHO GUARD, NAMED — the audit's point 1 could not attribute two real
100
+ // walks that stopped with no note at all, and this is where one of them went:
101
+ // the query IS a learnt continuation, so the reasoner returns the grounded
102
+ // read-out and never offers anything. Without this note the walk looks like
103
+ // an unexplained stop; with it, "no note" can only mean the offer was never
104
+ // asked. No decision changes: the guard returns exactly where it did.
105
+ ctx.trace?.step("echoGuard", [rItem(query, "query")], [], "the query is itself a learnt continuation — answering the grounded read-out without walking");
106
+ return false;
107
+ }
108
+ // ANSWERED DIRECTLY — the echo guard's other half, and the same principle:
109
+ // the QUERY's own position in the graph, not the answer's content, says the
110
+ // read-out is complete. Above: the query is itself a learnt CONTINUATION.
111
+ // Here: the query is a learnt CONTEXT and the grounded answer is one of ITS
112
+ // OWN continuations. Either way the question was answered directly and there
113
+ // is nothing left to chain for.
114
+ //
115
+ // Every stopping condition in the loop below judges the ANSWER (`consumed` /
116
+ // the law's `restates` / `bytesEqual`); none asks whether the QUESTION was
117
+ // satisfied. So a single-hop question whose answer happens to name another
118
+ // learnt context extends past a correct answer and REPLACES it:
119
+ //
120
+ // asked "<subj> father"
121
+ // hop 1 "The father of <subj> is Ernest I of Anhalt-Dessau." <- correct
122
+ // pivot "Ernest I of Anhalt-Dessau" <- a learnt context too
123
+ // got "The date of death of Ernest I of Anhalt-Dessau is 12 June 1516."
124
+ //
125
+ // Any store holding a bare-entity context alongside a relation fact has that
126
+ // shape; it is not exotic.
127
+ //
128
+ // Checked ONCE, before the loop, and ahead of BOTH extension branches:
129
+ // `absorbForward` extends the answer too, and nothing about the defect is
130
+ // specific to pivoting, so a guard between them would gate one arbitrary half
131
+ // of the same step. Hop 0 is also the only hop at which the question can be
132
+ // answered directly at all — after a hop, `cur` is no longer the query's own
133
+ // continuation, so re-testing per hop could only cost reads.
134
+ //
135
+ // Read from the ANSWER's side (`prevFirst`) rather than the query's
136
+ // (`nextFirst`). Same relation, but a CONTEXT's fan-out is hub-sized while
137
+ // this is one answer's establishing-context fan-in. Both the resolve and the
138
+ // reverse read are exactly what hop 0 of the loop below would perform, so
139
+ // they are computed ONCE here and handed down (`groundedId`, `groundedPrev`)
140
+ // — the guard then costs nothing when it does not fire. Stated because the
141
+ // naive placement does NOT: `resolve` re-folds the answer bytes on every call
142
+ // (no memo) and `prevFirst` is a direct read (no memo), so a guard that
143
+ // recomputed them would add one fold plus one √N-bounded read per ask.
144
+ // The √N cap carries the file-wide visibility trade, and fails SAFE in the
145
+ // direction that matters: a missed guard costs an over-extended answer, never
146
+ // a suppressed chain.
147
+ //
148
+ // A genuine multi-hop query is not a deposited context at all ("What is the
149
+ // capital of the country of Eiffel Tower?" resolves to nothing), so this can
150
+ // never gate a real chain.
151
+ groundedId = resolve(ctx, answer);
152
+ groundedPrev = groundedId === null
153
+ ? null
154
+ : ctx.store.prevFirst(groundedId, bound);
155
+ if (qId !== null && groundedPrev !== null && groundedPrev.includes(qId)) {
156
+ return false;
143
157
  }
158
+ // Pre-consume whatever the grounding stage already spoke for. The halo
159
+ // sweep is one ANN query per node — cap it at haloQueryK sweeps (cover
160
+ // grounding can pre-consume one node per recognised site, O(query length));
161
+ // nodes past the cap are still consumed directly, they just skip the
162
+ // synonym expansion.
163
+ const preconsume = async () => {
164
+ let haloSweeps = 0;
165
+ for (const id of preConsumed) {
166
+ consumeNode(id);
167
+ if (haloSweeps >= ctx.cfg.haloQueryK)
168
+ continue;
169
+ const h = ctx.store.halo(id);
170
+ if (!h)
171
+ continue;
172
+ haloSweeps++;
173
+ for (const sib of await haloSiblings(ctx, id, h))
174
+ consumeNode(sib.id);
175
+ }
176
+ };
177
+ if (ctx.meter) {
178
+ await ctx.meter.time("reason.preconsumeHalos", preconsume);
179
+ }
180
+ else {
181
+ await preconsume();
182
+ }
183
+ return true;
144
184
  };
145
- if (ctx.meter) {
146
- await ctx.meter.time("reason.preconsumeHalos", preconsume);
147
- }
148
- else {
149
- await preconsume();
150
- }
151
185
  // ── THE WALK, AS THE LAW'S ───────────────────────────────────────────
152
186
  //
153
187
  // This function no longer decides whether a step may be taken: it OFFERS a
@@ -170,7 +204,6 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
170
204
  // 30 s); and raising it from 12 to 200 changed neither the answer nor
171
205
  // `pivotSteps` on the chain fixtures.
172
206
  const qv = pre.guide; // the response-wide guide IS the query's gist
173
- const W = ctx.space.maxGroup;
174
207
  // WHOSE EXTENSION IS THIS? `voiced` is what the mechanism WITHHELD (the
175
208
  // pipeline sends the used anchors' CONTINUATIONS, not their bytes), so a
176
209
  // non-empty `voiced` means exactly what that note says: the grounding came
@@ -181,7 +214,6 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
181
214
  // expresses as the identity species of progress — `moves` — declared by the
182
215
  // reporter and never inferred from the mechanism's name.
183
216
  const producerOwnsShape = voiced.length > 0;
184
- const startedFrom = answer;
185
217
  let hop = 0;
186
218
  let t;
187
219
  // WHAT THE OFFERED STEP WOULD BE. The law decides, so the accepted step is
@@ -190,6 +222,13 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
190
222
  // admitted.
191
223
  let pending = null;
192
224
  const offer = async (d) => {
225
+ if (!begun) {
226
+ begun = true;
227
+ if (!(await begin(d))) {
228
+ stopped = true;
229
+ return null;
230
+ }
231
+ }
193
232
  if (ctx.meter)
194
233
  ctx.meter.offerRuns += 1;
195
234
  const cur = d.product;
@@ -302,12 +341,8 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
302
341
  cost: STEP,
303
342
  };
304
343
  };
305
- const closed_ = await closure(d0, query, W, offer, (before, after, witnesses) => {
306
- if (ctx.meter) {
307
- ctx.meter.reasonCarriedBytes += witnesses.reduce((n, w) => n + (w.window ? w.window[1] - w.window[0] : 0), 0);
308
- ctx.meter.closureDrainedBytes += unaccountedBytes(before.remainder) -
309
- unaccountedBytes(after.remainder);
310
- }
344
+ const onTaken = (before, after, witnesses) => {
345
+ meterTransition(ctx, before, after, witnesses);
311
346
  const p = pending;
312
347
  pending = null;
313
348
  if (p === null)
@@ -321,7 +356,8 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
321
356
  ctx.meter.pivotSteps++;
322
357
  ctx.trace?.step("pivotStep", [rItem(p.cur, "answer"), rNode(ctx, p.pivot, "pivot")], [rItem(p.fc, "answer", resolve(ctx, p.fc) ?? undefined)], "pivot on the shared span this answer contains, then step forward across that fact");
323
358
  }
324
- }, (at) => {
359
+ };
360
+ const onRefused = (at) => {
325
361
  // THE LAW REFUSED — counted where it happened, before the bookkeeping
326
362
  // below decides whether there is a step to report.
327
363
  if (ctx.meter)
@@ -340,48 +376,115 @@ export async function reason(ctx, query, d0, preConsumed, pre, voiced = []) {
340
376
  const left = unaccountedBytes(at.remainder);
341
377
  ctx.trace?.step("pivotRefused", [rItem(p.cur, "answer"), rItem(query, "query")], at.remainder.map(([a, b]) => rItem(query.subarray(a, b), "uncovered")), `the step carries none of the question material the grounding left ` +
342
378
  `unaccounted (${left} byte(s) in ${at.remainder.length} span(s)) — refused`);
343
- });
379
+ };
344
380
  // THE WALK'S OWN ENDING, NAMED — the audit's point 1. Every stop INSIDE the
345
381
  // offer now has a note, but a walk can also end without the offer ever being
346
382
  // asked, or after a step the law took and then found nothing to follow: those
347
383
  // ended as "no note at all", indistinguishable from a mechanism that never ran.
348
384
  // Two endings cover every remaining case, and together they make a silent stop
349
385
  // impossible — which is what the `Offer` contract needs, because `null` is read
350
- // as exhaustion and exhaustion must be a statement, not an absence.
351
- t ??= ctx.trace?.enter("reason", [rItem(startedFrom, "grounded")]);
352
- if (closed_.remainder.length === 0) {
353
- ctx.trace?.step("walkClosed", [rItem(closed_.product, "answer")], [], "the derivation is closed — nothing is left unaccounted for");
386
+ // as exhaustion and exhaustion must be a statement, not an absence. A walk a
387
+ // guard stopped reports nothing here: the guard already said why (or, for the
388
+ // answered-directly guard, the answer IS the read-out), exactly as before the
389
+ // walk became a layer.
390
+ const onEnd = (d0, closed_) => {
391
+ if (stopped)
392
+ return;
393
+ t ??= ctx.trace?.enter("reason", [rItem(startedFrom, "grounded")]);
394
+ if (closed_.remainder.length === 0) {
395
+ ctx.trace?.step("walkClosed", [rItem(closed_.product, "answer")], [], "the derivation is closed — nothing is left unaccounted for");
396
+ }
397
+ else {
398
+ ctx.trace?.step("walkEndedWithoutOffer", [rItem(closed_.product, "answer")], closed_.remainder.map(([a, b]) => rItem(query.subarray(a, b), "uncovered")), `the walk ended with ${unaccountedBytes(closed_.remainder)} byte(s) unaccounted and no further offer — no structural continuation`);
399
+ }
400
+ // INSTRUMENTATION ONLY — the extension's two facts, untraced (meter.ts
401
+ // contract 1: a counter never reaches a decision). They are what a caller
402
+ // needs to PRICE the extension instead of taking it unconditionally, and both
403
+ // are now read off the state the law advanced rather than accumulated beside
404
+ // it: the work it did is the cost it accumulated, and the material it carried
405
+ // is the accounting the law's witnesses added.
406
+ const steps = closed_.cost - d0.cost;
407
+ if (ctx.meter) {
408
+ ctx.meter.reasonSteps += steps;
409
+ // The accounting the law's witnesses added, summed by the law's own function
410
+ // over the tail of the state's list — and computed ONLY when a meter is
411
+ // attached: this used to slice and MAP a fresh array on every response, for a
412
+ // counter that usually does not exist. One allocation, under the meter.
413
+ ctx.meter.reasonAccountedBytes += unaccountedBytes(closed_.accounted.slice(d0.accounted.length));
414
+ }
415
+ t?.done([rItem(closed_.product, "answer", resolve(ctx, closed_.product) ?? undefined)],
416
+ // A FIXPOINT: no further step was offered, or the law refused the one that
417
+ // was. There is no allowance to exhaust, so the note is true by
418
+ // construction.
419
+ "the multi-hop chain's fixpoint");
420
+ };
421
+ return { name: "reason", offer, onTaken, onRefused, onEnd };
422
+ }
423
+ /** In how many separate PLACES of the query a stored context shares content at
424
+ * window scale — the query's W-byte windows the context's bytes contain,
425
+ * clustered by {@link countClusters} at the same quantum. Below one window
426
+ * byte identity is chance (identityBar), so only whole windows count.
427
+ *
428
+ * It is the dispersion question asked of the CONTEXT rather than of the votes
429
+ * that happened to land on it. A region votes once, for its top anchor, so
430
+ * where a root's votes come from is a lossy witness of where its context and
431
+ * the query agree — measured on test/24's gap-3.1 fixture: the second topic's
432
+ * second cluster was the region `ace and `, in the OTHER topic's words,
433
+ * voting through a 2-byte sub-window anchor `e ` (attention.ts
434
+ * canonicalChunkId); once that stopped voting, the genuine topic read as one
435
+ * cluster. Its context still shares the system prompt AND its own wording
436
+ * with the query — two places, the two test/37 documents — while a
437
+ * coincidental echo (one closing phrase byte-identical to an unrelated
438
+ * conversation's end, test/37) shares one. */
439
+ function sharedPlaces(context, query, quantum) {
440
+ if (context.length < quantum || query.length < quantum)
441
+ return 0;
442
+ const windows = new Set();
443
+ for (let o = 0; o + quantum <= context.length; o++) {
444
+ windows.add(latin1(context.subarray(o, o + quantum)));
354
445
  }
355
- else {
356
- ctx.trace?.step("walkEndedWithoutOffer", [rItem(closed_.product, "answer")], closed_.remainder.map(([a, b]) => rItem(query.subarray(a, b), "uncovered")), `the walk ended with ${unaccountedBytes(closed_.remainder)} byte(s) unaccounted and no further offer — no structural continuation`);
446
+ const spans = [];
447
+ for (let o = 0; o + quantum <= query.length; o++) {
448
+ if (windows.has(latin1(query.subarray(o, o + quantum)))) {
449
+ spans.push([o, o + quantum]);
450
+ }
357
451
  }
358
- // INSTRUMENTATION ONLY — the extension's two facts, untraced (meter.ts
359
- // contract 1: a counter never reaches a decision). They are what a caller
360
- // needs to PRICE the extension instead of taking it unconditionally, and both
361
- // are now read off the state the law advanced rather than accumulated beside
362
- // it: the work it did is the cost it accumulated, and the material it carried
363
- // is the accounting the law's witnesses added.
364
- const steps = closed_.cost - d0.cost;
365
- if (ctx.meter) {
366
- ctx.meter.reasonSteps += steps;
367
- // The accounting the law's witnesses added, summed by the law's own function
368
- // over the tail of the state's list — and computed ONLY when a meter is
369
- // attached: this used to slice and MAP a fresh array on every response, for a
370
- // counter that usually does not exist. One allocation, under the meter.
371
- ctx.meter.reasonAccountedBytes += unaccountedBytes(closed_.accounted.slice(d0.accounted.length));
452
+ return countClusters(spans, quantum);
453
+ }
454
+ /** Whether any query byte lies at least `quantum` away from every span — the
455
+ * only place a root independent of those spans could stand. The spans,
456
+ * widened by the quantum on each side, either cover [0, len) or leave a gap. */
457
+ function roomBeyond(len, spans, quantum) {
458
+ if (spans.length === 0)
459
+ return len > 0;
460
+ const widened = spans
461
+ .map(([s, e]) => [s - quantum, e + quantum])
462
+ .sort((a, b) => a[0] - b[0]);
463
+ let reach = 0; // every byte below `reach` is within a quantum of a span
464
+ for (const [s, e] of widened) {
465
+ if (s > reach)
466
+ return true;
467
+ reach = Math.max(reach, e);
468
+ if (reach >= len)
469
+ return false;
372
470
  }
373
- t?.done([rItem(closed_.product, "answer", resolve(ctx, closed_.product) ?? undefined)],
374
- // A FIXPOINT: no further step was offered, or the law refused the one that
375
- // was. There is no allowance to exhaust, so the note is true by
376
- // construction.
377
- "the multi-hop chain's fixpoint");
378
- return closed_;
471
+ return reach < len;
379
472
  }
380
473
  /** Fuse independent points of attention into one answer (multi-topic).
381
474
  * When the consensus climb finds more than one dominant point, each
382
475
  * independent point grounds its own answer; they are bridged together
383
476
  * by any learnt connector the graph holds between them. */
384
- export async function fuseAttention(ctx, query, state, pre,
477
+ export function fuseAttention(ctx, query, state, pre, unclimbed = false, primarySpans = []) {
478
+ return closeOver(state, query, ctx.space.maxGroup, [
479
+ fusionLayer(ctx, query, pre, unclimbed, primarySpans),
480
+ ]);
481
+ }
482
+ /** Fusion as a {@link ClosureLayer}: ONE transition, offered once — the fused
483
+ * product of the independent points of attention, or nothing. The law admits
484
+ * it (`reaches`: the fusion composed structure this derivation had not stood
485
+ * on); the layer's own structural gates below decide whether there is a
486
+ * fusion to offer at all. */
487
+ export function fusionLayer(ctx, query, pre,
385
488
  /** True when `primary` never touched the consensus climb at all — e.g. a
386
489
  * pure ALU computation, which has no anchor of its own. commitVotes
387
490
  * ALWAYS admits the dominant root regardless of its vote (attention.ts:
@@ -396,191 +499,212 @@ unclimbed = false,
396
499
  * fuseAttention just reads a position from it. Empty or absent preserves
397
500
  * the original behaviour exactly. */
398
501
  primarySpans = []) {
399
- // THE STATE, NOT BARE BYTES: fusion is one more transition of the derivation
400
- // the walk returned, so it reads that state's product and hands back a state.
401
- // Its own structural gates stay here — this is the layer that can resolve
402
- // those witnesses, and the law never re-derives one.
403
- const primary = state.product;
404
- // When the answer is structurally drawn from the query itself
405
- // (extraction), it already spans all the query's pieces — fusion
406
- // would only add noise from unrelated stored contexts. The gate is
407
- // STRICT containment (resolved node in the query's tree, or a contiguous
408
- // byte run): the old sparse-subsequence test was trivially satisfied by
409
- // short answers over long queries, silently starving multi-topic queries
410
- // of fusion.
411
- if (containsSpan(ctx, query, primary))
412
- return state;
413
- // The committed points of attention ARE the shared climb's roots (same
414
- // query, same k, same DF mode) — read them from Precomputed instead of
415
- // re-climbing, so even a traced response pays for the climb once.
416
- const forest = (await pre.attention()).roots;
417
- // A LONE root is ordinarily primary's own source — nothing to fuse. But
418
- // when primary is unclimbed, the lone root was never checked against
419
- // anything: it is admitted by commitVotes unconditionally, so it may be
420
- // genuine consensus (Attention.breadth dominates — most of the query's
421
- // OWN regions corroborate it) or a coincidental echo (breadth does not
422
- // dominate — see test/35-attention-confidence). breadth is the SCALE-
423
- // INVARIANT read of exactly this question: the raw IDF vote cannot serve
424
- // here, since it is an absolute ln(N)-scaled quantity (a genuine root on
425
- // a large store can score BELOW its own floor while a coincidental echo
426
- // on a small one scores comfortably above its own, smaller, floor).
427
- //
428
- // Breadth alone is not enough when primary is a pure COMPUTATION. The ALU
429
- // answers "2+2 equals what?" with 4, and the store's own arithmetic table
430
- // then supplies a lone root — an exemplar like "1+2" — whose breadth
431
- // dominates because it is corroborated by the computation's OWN bytes.
432
- // Fusing it projected that exemplar's continuation and the bridge voiced
433
- // "4+3" (test/11 seed 99). A second point of attention must stand on
434
- // evidence that is structurally SEPARATE from primary's: at least one
435
- // perceptual quantum of query between them, the same separation
436
- // countClusters uses to tell independent evidence neighbourhoods apart.
437
- // Not a score, and not a tuned bar — the fold's own quantum.
438
- //
439
- // With no primarySpans (the caller did not resolve them) every span
440
- // vacuously qualifies, preserving the original behaviour exactly.
441
- const quantum = ctx.space.maxGroup;
442
- const independentOfPrimary = (root) => primarySpans.every(([s, e]) => {
443
- const gap = root.end <= s
444
- ? s - root.end
445
- : e <= root.start
446
- ? root.start - e
447
- : 0;
448
- return gap >= quantum;
449
- });
450
- const lonePromotes = unclimbed && forest.length === 1 &&
451
- forest[0].breadth > 0.5 && independentOfPrimary(forest[0]);
452
- if (forest.length === 0 || (forest.length <= 1 && !lonePromotes)) {
453
- return state;
454
- }
455
- // WHERE THE QUERY ASKED FOR IT. The sort below orders the fused pieces by
456
- // query position, which is the whole point of the `start` field: a
457
- // multi-topic answer should read in the order the question posed its
458
- // topics. Every ROOT carries its own start. `primary` did not — it was
459
- // given forest[0].start, the FIRST attention root's position, which is
460
- // primary's own source only when primary happens to come from that root.
461
- // When it does not, primary is sorted to a position it never occupied.
462
- //
463
- // Observed live: "What is the capital of France? And what is 2 + 2?"
464
- // answered "4The capital city of France is Paris." — the ALU result, whose
465
- // evidence is the "2 + 2" span near the END of the query, inherited the
466
- // France root's start of 0 and sorted ahead of the France answer. Both
467
- // pieces were right; only the order was.
468
- //
469
- // primary's own position is the earliest query byte its grounding stands
470
- // on. `accounted` is the cost-ladder read of that and is authoritative
471
- // when non-empty; when it is empty the grounding is a pure COMPUTATION,
472
- // whose evidence is its computed span — the same cost-ladder-vs-coverage
473
- // distinction think() already draws for the fusion remainder ("`accounted`
474
- // alone undercounts this ... cover prices its computed spans at near-zero
475
- // and deliberately leaves them out"), read here for position instead of
476
- // for coverage. With neither, nothing is known and the old behaviour
477
- // (forest[0].start) stands.
478
- const primaryStart = primarySpans.length > 0
479
- ? primarySpans.reduce((m, [s]) => Math.min(m, s), Infinity)
480
- : forest[0].start;
481
- const pieces = [
482
- { start: primaryStart, bytes: primary },
483
- ];
484
- const qv = pre.guide; // once, not per root
485
- const rest = lonePromotes ? forest : forest.slice(1);
486
- const t = ctx.trace?.enter("fuseAttention", [
487
- rItem(primary, "primary"),
488
- ...rest.map((r) => rNode(ctx, r.anchor, "point", r.vote)),
489
- ]);
490
- for (const root of rest) {
491
- // DISPERSION: this root's contributing regions are confined to a
492
- // single cluster (see Attention.clusters) — one local neighbourhood of
493
- // the query, not several separate places. Raw region count already
494
- // failed to discriminate a coincidental match from a genuine further
495
- // topic (test/24 gap 3.1 vs test/35's echo); dispersion is a different
496
- // question — not how MUCH evidence, but how many separate PLACES in the
497
- // query corroborate it — and a coincidental match is structurally
498
- // confined to one cluster no matter how strongly it resonates.
502
+ let offered = false;
503
+ const offer = async (state) => {
504
+ if (offered)
505
+ return null;
506
+ offered = true;
507
+ // THE STATE, NOT BARE BYTES: fusion is one more transition of the derivation
508
+ // the walk returned, so it reads that state's product and hands back a state.
509
+ // Its own structural gates stay here — this is the layer that can resolve
510
+ // those witnesses, and the law never re-derives one.
511
+ const primary = state.product;
512
+ // When the answer is structurally drawn from the query itself
513
+ // (extraction), it already spans all the query's pieces — fusion
514
+ // would only add noise from unrelated stored contexts. The gate is
515
+ // STRICT containment (resolved node in the query's tree, or a contiguous
516
+ // byte run): the old sparse-subsequence test was trivially satisfied by
517
+ // short answers over long queries, silently starving multi-topic queries
518
+ // of fusion.
519
+ if (containsSpan(ctx, query, primary))
520
+ return null;
521
+ // A SECOND POINT OF ATTENTION MUST STAND ON EVIDENCE STRUCTURALLY SEPARATE
522
+ // FROM PRIMARY'S: at least one perceptual quantum of query between them,
523
+ // the same separation countClusters uses to tell independent evidence
524
+ // neighbourhoods apart. Not a score, and not a tuned bar — the fold's own
525
+ // quantum. A root standing on primary's own evidence is primary's topic
526
+ // read again, never a further one, so it is not fused (measured on the
527
+ // 31.7M-node store: an exact dialogue turn grounded whole, `[0,79]`, paid a
528
+ // 3.7 s consensus climb whose only root lay inside that span).
529
+ //
530
+ // With no primarySpans (the caller did not resolve them) every span
531
+ // vacuously qualifies, preserving the original behaviour exactly.
532
+ const quantum = ctx.space.maxGroup;
533
+ const independentOfPrimary = (root) => primarySpans.every(([s, e]) => {
534
+ const gap = root.end <= s
535
+ ? s - root.end
536
+ : e <= root.start
537
+ ? root.start - e
538
+ : 0;
539
+ return gap >= quantum;
540
+ });
541
+ // Decided BEFORE the climb when it is already decided: a root spans at
542
+ // least one query byte, so where every byte lies within a quantum of
543
+ // primary's evidence no root can be independent, and there is nothing the
544
+ // climb could hand this layer.
545
+ if (!roomBeyond(query.length, primarySpans, quantum))
546
+ return null;
547
+ // The committed points of attention ARE the shared climb's roots (same
548
+ // query, same k, same DF mode) — read them from Precomputed instead of
549
+ // re-climbing, so even a traced response pays for the climb once.
550
+ const forest = (await pre.attention()).roots;
551
+ // A LONE root is ordinarily primary's own source — nothing to fuse. But
552
+ // when primary is unclimbed, the lone root was never checked against
553
+ // anything: it is admitted by commitVotes unconditionally, so it may be
554
+ // genuine consensus (Attention.breadth dominates — most of the query's
555
+ // OWN regions corroborate it) or a coincidental echo (breadth does not
556
+ // dominate — see test/35-attention-confidence). breadth is the SCALE-
557
+ // INVARIANT read of exactly this question: the raw IDF vote cannot serve
558
+ // here, since it is an absolute ln(N)-scaled quantity (a genuine root on
559
+ // a large store can score BELOW its own floor while a coincidental echo
560
+ // on a small one scores comfortably above its own, smaller, floor).
561
+ //
562
+ // Breadth alone is not enough when primary is a pure COMPUTATION. The ALU
563
+ // answers "2+2 equals what?" with 4, and the store's own arithmetic table
564
+ // then supplies a lone root — an exemplar like "1+2" — whose breadth
565
+ // dominates because it is corroborated by the computation's OWN bytes.
566
+ // Fusing it projected that exemplar's continuation and the bridge voiced
567
+ // "4+3" (test/11 seed 99) — the separation above is what refuses it.
568
+ const lonePromotes = unclimbed && forest.length === 1 &&
569
+ forest[0].breadth > 0.5 && independentOfPrimary(forest[0]);
570
+ if (forest.length === 0 || (forest.length <= 1 && !lonePromotes)) {
571
+ return null;
572
+ }
573
+ // WHERE THE QUERY ASKED FOR IT. The sort below orders the fused pieces by
574
+ // query position, which is the whole point of the `start` field: a
575
+ // multi-topic answer should read in the order the question posed its
576
+ // topics. Every ROOT carries its own start. `primary` did not — it was
577
+ // given forest[0].start, the FIRST attention root's position, which is
578
+ // primary's own source only when primary happens to come from that root.
579
+ // When it does not, primary is sorted to a position it never occupied.
499
580
  //
500
- // EXCEPTION: crossRegionVotes' own joint conclusions (a query naming
501
- // two attributes that were only ever learnt TOGETHER — test/34's own
502
- // binding corpus) are inherently ONE fused context and are pooled from
503
- // a single synthetic region, so they always read as one cluster even
504
- // though they already, by construction, account for both original
505
- // mentions. `breadth` (dominates — the same > half-the-query bar used
506
- // everywhere else) still correctly recognises these: a genuine joint
507
- // binding explains the MAJORITY of the query's regions on its own,
508
- // which a coincidental echo never does (verified: test/35's echo tops
509
- // out at 0.40). So a root is trusted when EITHER measure alone
510
- // indicates real signal — excluded only when BOTH are weak. Cheap and
511
- // synchronous — checked before the async already-answered walk below.
512
- if (root.clusters < 2 && root.breadth <= 0.5) {
513
- ctx.trace?.step("singleCluster", [rNode(ctx, root.anchor, "point", root.vote)], [], "this point's evidence is confined to one local neighbourhood of the query — not trusted as an independent topic");
514
- continue;
581
+ // Observed live: "What is the capital of France? And what is 2 + 2?"
582
+ // answered "4The capital city of France is Paris." — the ALU result, whose
583
+ // evidence is the "2 + 2" span near the END of the query, inherited the
584
+ // France root's start of 0 and sorted ahead of the France answer. Both
585
+ // pieces were right; only the order was.
586
+ //
587
+ // primary's own position is the earliest query byte its grounding stands
588
+ // on. `accounted` is the cost-ladder read of that and is authoritative
589
+ // when non-empty; when it is empty the grounding is a pure COMPUTATION,
590
+ // whose evidence is its computed span — the same cost-ladder-vs-coverage
591
+ // distinction think() already draws for the fusion remainder ("`accounted`
592
+ // alone undercounts this ... cover prices its computed spans at near-zero
593
+ // and deliberately leaves them out"), read here for position instead of
594
+ // for coverage. With neither, nothing is known and the old behaviour
595
+ // (forest[0].start) stands.
596
+ const primaryStart = primarySpans.length > 0
597
+ ? primarySpans.reduce((m, [s]) => Math.min(m, s), Infinity)
598
+ : forest[0].start;
599
+ const pieces = [
600
+ { start: primaryStart, bytes: primary },
601
+ ];
602
+ const qv = pre.guide; // once, not per root
603
+ const rest = (lonePromotes ? forest : forest.slice(1)).filter(independentOfPrimary);
604
+ if (rest.length === 0)
605
+ return null;
606
+ const t = ctx.trace?.enter("fuseAttention", [
607
+ rItem(primary, "primary"),
608
+ ...rest.map((r) => rNode(ctx, r.anchor, "point", r.vote)),
609
+ ]);
610
+ for (const root of rest) {
611
+ // DISPERSION: this root's contributing regions are confined to a
612
+ // single cluster (see Attention.clusters) — one local neighbourhood of
613
+ // the query, not several separate places. Raw region count already
614
+ // failed to discriminate a coincidental match from a genuine further
615
+ // topic (test/24 gap 3.1 vs test/35's echo); dispersion is a different
616
+ // question — not how MUCH evidence, but how many separate PLACES in the
617
+ // query corroborate it — and a coincidental match is structurally
618
+ // confined to one cluster no matter how strongly it resonates.
619
+ //
620
+ // EXCEPTION: crossRegionVotes' own joint conclusions (a query naming
621
+ // two attributes that were only ever learnt TOGETHER — test/34's own
622
+ // binding corpus) are inherently ONE fused context and are pooled from
623
+ // a single synthetic region, so they always read as one cluster even
624
+ // though they already, by construction, account for both original
625
+ // mentions. `breadth` (dominates — the same > half-the-query bar used
626
+ // everywhere else) still correctly recognises these: a genuine joint
627
+ // binding explains the MAJORITY of the query's regions on its own,
628
+ // which a coincidental echo never does (verified: test/35's echo tops
629
+ // out at 0.40). So a root is trusted when EITHER measure alone
630
+ // indicates real signal — excluded only when BOTH are weak. Cheap and
631
+ // synchronous — checked before the async already-answered walk below.
632
+ if (root.clusters < 2 && root.breadth <= 0.5 &&
633
+ sharedPlaces(read(ctx, root.anchor), query, quantum) < 2) {
634
+ ctx.trace?.step("singleCluster", [rNode(ctx, root.anchor, "point", root.vote)], [], "this point's evidence is confined to one local neighbourhood of the query — not trusted as an independent topic");
635
+ continue;
636
+ }
637
+ // ALREADY ANSWERED: this root's own learnt continuation — the same
638
+ // content-addressed walk reason()'s echo guard already trusts
639
+ // (`ctx.store.prevCount(qId) > 0`), here applied per-candidate instead
640
+ // of to the whole query — is VERBATIM present later in the query. A
641
+ // query that embeds both an exchange's ask and its own already-given
642
+ // reply (a conversation's turn plus its own prior answer, concatenated
643
+ // raw by addTurn — or any caller pasting a transcript into one
644
+ // respond() call; the check is Mind-bookkeeping-free, so it treats both
645
+ // identically) has already spoken this root's answer — fusing it in
646
+ // again would only restate it. Deliberately NOT a magnitude measure:
647
+ // it fires on exact content-addressed recurrence, not on how strongly
648
+ // the root resonates.
649
+ const cont = await follow(ctx, root.anchor, qv);
650
+ if (cont !== null && cont.length > 0 &&
651
+ // The law's positional reading: the caller knows the material it is
652
+ // looking for lies AT OR AFTER this root, which is the witness; the law
653
+ // owns the containment test itself. The `cont.length > 0` guard stays
654
+ // here because an EMPTY continuation indexes at every offset — the law
655
+ // has no opinion about whether "nothing" counts as said.
656
+ restates(query, cont, 0, { from: root.end })) {
657
+ ctx.trace?.step("alreadyAnswered", [rNode(ctx, root.anchor, "point", root.vote)], [rItem(cont, "continuation")], "this point's own learnt continuation already appears later in the query — already answered, not fused");
658
+ continue;
659
+ }
660
+ const g = await project(ctx, root.anchor, qv);
661
+ if (g === null || g.length === 0)
662
+ continue;
663
+ if (pieces.some((p) => indexOf(p.bytes, g, 0) >= 0))
664
+ continue;
665
+ pieces.push({ start: root.start, bytes: g });
515
666
  }
516
- // ALREADY ANSWERED: this root's own learnt continuation — the same
517
- // content-addressed walk reason()'s echo guard already trusts
518
- // (`ctx.store.prevCount(qId) > 0`), here applied per-candidate instead
519
- // of to the whole query — is VERBATIM present later in the query. A
520
- // query that embeds both an exchange's ask and its own already-given
521
- // reply (a conversation's turn plus its own prior answer, concatenated
522
- // raw by addTurn — or any caller pasting a transcript into one
523
- // respond() call; the check is Mind-bookkeeping-free, so it treats both
524
- // identically) has already spoken this root's answer — fusing it in
525
- // again would only restate it. Deliberately NOT a magnitude measure:
526
- // it fires on exact content-addressed recurrence, not on how strongly
527
- // the root resonates.
528
- const cont = await follow(ctx, root.anchor, qv);
529
- if (cont !== null && cont.length > 0 &&
530
- // The law's positional reading: the caller knows the material it is
531
- // looking for lies AT OR AFTER this root, which is the witness; the law
532
- // owns the containment test itself. The `cont.length > 0` guard stays
533
- // here because an EMPTY continuation indexes at every offset — the law
534
- // has no opinion about whether "nothing" counts as said.
535
- restates(query, cont, 0, { from: root.end })) {
536
- ctx.trace?.step("alreadyAnswered", [rNode(ctx, root.anchor, "point", root.vote)], [rItem(cont, "continuation")], "this point's own learnt continuation already appears later in the query — already answered, not fused");
537
- continue;
667
+ if (pieces.length === 1) {
668
+ t?.done([rItem(primary, "answer")], "no further independent point grounded");
669
+ return null;
538
670
  }
539
- const g = await project(ctx, root.anchor, qv);
540
- if (g === null || g.length === 0)
541
- continue;
542
- if (pieces.some((p) => indexOf(p.bytes, g, 0) >= 0))
543
- continue;
544
- pieces.push({ start: root.start, bytes: g });
545
- }
546
- if (pieces.length === 1) {
547
- t?.done([rItem(primary, "answer")], "no further independent point grounded");
548
- return state;
549
- }
550
- pieces.sort((a, b) => a.start - b.start);
551
- let out = pieces[0].bytes;
552
- for (let i = 1; i < pieces.length; i++) {
553
- // An approximate-resonance miss (or a genuinely unlearnt junction) joins
554
- // the pieces bare — joinWithBridge surfaces it as a bridgeMiss step.
555
- out = await joinWithBridge(ctx, out, pieces[i].bytes);
556
- }
557
- t?.done([rItem(out, "answer", resolve(ctx, out) ?? undefined)], `fused ${pieces.length} independent points of attention into one answer`);
558
- // THE FACT IS THE FUSED ANSWER, not the call: every early return above hands
559
- // back `primary` untouched. Untraced on purpose (meter.ts contract 1).
560
- if (ctx.meter)
561
- ctx.meter.fuseRuns++;
562
- // A FUSION IS A TRANSITION, and the only one in the engine that can splice the
563
- // QUESTION'S OWN material into the product. So it is OFFERED to the law rather
564
- // than built by hand: the law reads the window the fused product holds — the
565
- // same reading the carries admission uses, one definition — accounts for the
566
- // span it carried, and lets the question's remainder drain on EVIDENCE. A
567
- // fusion that carries nothing consumes nothing, because the reading finds no
568
- // window; and `reaches` is declared only when the fusion composed another root,
569
- // which is structure this derivation had not stood on.
570
- const fusedT = {
571
- product: out,
572
- contains: true,
573
- reaches: rest.length > 0,
574
- cost: STEP,
671
+ pieces.sort((a, b) => a.start - b.start);
672
+ let out = pieces[0].bytes;
673
+ for (let i = 1; i < pieces.length; i++) {
674
+ // An approximate-resonance miss (or a genuinely unlearnt junction) joins
675
+ // the pieces bare — joinWithBridge surfaces it as a bridgeMiss step.
676
+ out = await joinWithBridge(ctx, out, pieces[i].bytes);
677
+ }
678
+ t?.done([rItem(out, "answer", resolve(ctx, out) ?? undefined)], `fused ${pieces.length} independent points of attention into one answer`);
679
+ // THE FACT IS THE FUSED ANSWER, not the call: every early return above hands
680
+ // back `primary` untouched. Untraced on purpose (meter.ts contract 1).
681
+ if (ctx.meter)
682
+ ctx.meter.fuseRuns++;
683
+ // A FUSION IS A TRANSITION, and the only one in the engine that can splice the
684
+ // QUESTION'S OWN material into the product. So it is OFFERED to the law rather
685
+ // than built by hand: the law reads the window the fused product holds — the
686
+ // same reading the carries admission uses, one definition — accounts for the
687
+ // span it carried, and lets the question's remainder drain on EVIDENCE. A
688
+ // fusion that carries nothing consumes nothing, because the reading finds no
689
+ // window; and `reaches` is declared only when the fusion composed another root,
690
+ // which is structure this derivation had not stood on.
691
+ //
692
+ // THE LAW'S REFUSAL IS FINAL. The only state it refuses a fusion against is a
693
+ // supplied fixed point (`reaches` is always declared here), and the engine
694
+ // enters no layer for one — so there is no hand-built fallback: a fusion the
695
+ // law refuses is not taken (measured: no response in the suite reaches the
696
+ // refusal).
697
+ const fusedT = {
698
+ product: out,
699
+ contains: true,
700
+ reaches: rest.length > 0,
701
+ cost: STEP,
702
+ };
703
+ return fusedT;
704
+ };
705
+ return {
706
+ name: "fuse",
707
+ offer,
708
+ onTaken: (before, after, witnesses) => meterTransition(ctx, before, after, witnesses),
575
709
  };
576
- const fusedWitness = admissible(state, fusedT, query, ctx.space.maxGroup);
577
- if (fusedWitness === null)
578
- return { ...state, product: out };
579
- const fused = advance(state, fusedT, fusedWitness);
580
- if (ctx.meter) {
581
- ctx.meter.reasonCarriedBytes += fusedWitness.reduce((n, w) => n + (w.window ? w.window[1] - w.window[0] : 0), 0);
582
- ctx.meter.closureDrainedBytes += unaccountedBytes(state.remainder) -
583
- unaccountedBytes(fused.remainder);
584
- }
585
- return fused;
586
710
  }