@hviana/sema 0.9.2 → 0.9.3

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 (40) hide show
  1. package/dist/src/meter.d.ts +6 -0
  2. package/dist/src/meter.js +6 -0
  3. package/dist/src/mind/evidence.d.ts +6 -0
  4. package/dist/src/mind/evidence.js +19 -7
  5. package/dist/src/mind/mechanisms/cast.d.ts +1 -1
  6. package/dist/src/mind/mechanisms/cast.js +73 -12
  7. package/dist/src/mind/mechanisms/cover.js +4 -20
  8. package/dist/src/mind/mechanisms/recall.js +84 -8
  9. package/dist/src/mind/mechanisms/reference.js +25 -0
  10. package/dist/src/mind/mind.d.ts +1 -0
  11. package/dist/src/mind/pipeline-mechanism.js +15 -1
  12. package/dist/src/mind/pipeline.js +5 -4
  13. package/dist/src/mind/reasoning.js +28 -16
  14. package/dist/src/mind/resonance.d.ts +3 -2
  15. package/dist/src/mind/resonance.js +3 -2
  16. package/dist/src/mind/traverse.d.ts +52 -0
  17. package/dist/src/mind/traverse.js +334 -7
  18. package/dist/src/mind/types.d.ts +4 -1
  19. package/docs/INDEX.md +17 -17
  20. package/docs/INVARIANTS.md +1 -1
  21. package/docs/architecture/evidence.md +172 -29
  22. package/jsr.json +1 -1
  23. package/package.json +1 -1
  24. package/src/meter.ts +6 -0
  25. package/src/mind/evidence.ts +22 -4
  26. package/src/mind/mechanisms/cast.ts +77 -3
  27. package/src/mind/mechanisms/cover.ts +6 -22
  28. package/src/mind/mechanisms/recall.ts +107 -9
  29. package/src/mind/mechanisms/reference.ts +24 -1
  30. package/src/mind/mind.ts +3 -1
  31. package/src/mind/pipeline-mechanism.ts +15 -1
  32. package/src/mind/pipeline.ts +5 -4
  33. package/src/mind/reasoning.ts +33 -15
  34. package/src/mind/resonance.ts +3 -2
  35. package/src/mind/traverse.ts +338 -6
  36. package/src/mind/types.ts +6 -2
  37. package/test/154-the-question-names-the-step.test.mjs +88 -1
  38. package/test/155-the-relation-read-off-another-instance.test.mjs +376 -0
  39. package/test/29-counterfactual.test.mjs +21 -11
  40. package/test/76-reference-binding.test.mjs +32 -0
@@ -17,10 +17,14 @@ import { gistOf, read, resolve } from "../primitives.js";
17
17
  import { indexOf } from "../../bytes.js";
18
18
  import {
19
19
  allWindowsAreScaffolding,
20
+ answersOtherQuestions,
20
21
  askedEvidence,
22
+ canonHeldPoints,
23
+ coInstanceFiller,
21
24
  corpusN,
22
25
  guidedFirst,
23
26
  hubBound,
27
+ namedContinuations,
24
28
  } from "../traverse.js";
25
29
  import {
26
30
  follow,
@@ -108,13 +112,13 @@ export async function recallByResonance(
108
112
  // constituent bar is the same two-quanta (2W) reading confluence binds
109
113
  // under, nested recognitions collapse to their MAXIMAL span, and two
110
114
  // distinct maximal arguments mean the query asks about neither alone.
111
- if (qId === null) {
112
- const W2 = 2 * ctx.space.maxGroup;
113
- const args = pre.rec.sites.filter((s) =>
114
- s.end - s.start >= W2 &&
115
- s.end - s.start < query.length &&
116
- ctx.store.hasNext(s.payload)
117
- );
115
+ // The argument binding, given its candidate arguments (`args`) and the
116
+ // recognised fragments that answer other questions (`strangerIds`).
117
+ const W2 = 2 * ctx.space.maxGroup;
118
+ const bindArgument = async (
119
+ args: Array<{ start: number; end: number; payload: number }>,
120
+ strangerIds: ReadonlySet<number>,
121
+ ) => {
118
122
  // Maximal spans by one sorted sweep (starts ascending, ties longest
119
123
  // first): every earlier span starts at or before s, so s is contained
120
124
  // exactly when the running max end already covers it. O(m log m) — a
@@ -140,13 +144,25 @@ export async function recallByResonance(
140
144
  // binding to the argument's continuation would answer past content
141
145
  // the query itself already carries forward. Derived from the same W2
142
146
  // bar the argument itself is held to, never a separate tuned number.
147
+ // A fragment that answers other questions is not such a piece.
143
148
  const hasSubstantialOutside = maximal.length === 1 &&
144
149
  pre.rec.sites.some((s) =>
145
- s.end - s.start >= W2 &&
150
+ s.end - s.start >= W2 && !strangerIds.has(s.payload) &&
146
151
  (s.end <= maximal[0].start || s.start >= maximal[0].end)
147
152
  );
148
153
  if (maximal.length === 1 && !hasSubstantialOutside) {
149
154
  const arg = maximal[0];
155
+ // An argument whose several continuations the question names none of
156
+ // outright would be followed by popularity. Another instance of the
157
+ // question may name one (traverse.ts, `byCoInstance`), and its
158
+ // proposals are the consensus climb's points — so the climb is asked
159
+ // for first, exactly where the choice would otherwise be blind.
160
+ const asked = ctx._edgeAsked;
161
+ if (
162
+ asked !== null && asked.points === undefined &&
163
+ ctx.store.nextFirst(arg.payload, 2).length >= 2 &&
164
+ namedContinuations(ctx, arg.payload, asked) === null
165
+ ) await pre.attention();
150
166
  const g = await follow(ctx, arg.payload, queryGist);
151
167
  // The same "no restated fragment" guard tier 2 applies below (§ "the
152
168
  // anchor cleared the consensus floor..."): a followed continuation
@@ -176,6 +192,53 @@ export async function recallByResonance(
176
192
  );
177
193
  }
178
194
  }
195
+ return null;
196
+ };
197
+ let canonArgumentPending = false;
198
+ let strangers: ReadonlySet<number> = new Set();
199
+ if (qId === null) {
200
+ const recognised = pre.rec.sites.filter((s) =>
201
+ s.end - s.start >= W2 &&
202
+ s.end - s.start < query.length &&
203
+ ctx.store.hasNext(s.payload)
204
+ );
205
+ // An argument must lead somewhere FOR THIS QUESTION: a fragment whose
206
+ // continuations the question names none of answers other questions
207
+ // (traverse.ts) — `director`, inside `When was the director of film Jinpa
208
+ // born?`, bound the most-poured of fifty-five directors. Such a fragment
209
+ // is no argument, and no independent piece of the query either.
210
+ const strangerIds = new Set<number>();
211
+ for (const s of recognised) {
212
+ if (
213
+ answersOtherQuestions(ctx, s.payload, query.length, s.end - s.start)
214
+ ) strangerIds.add(s.payload);
215
+ }
216
+ const args = recognised.filter((s) => !strangerIds.has(s.payload));
217
+ if (args.length === 0 && strangerIds.size > 0) {
218
+ if (ctx.meter) ctx.meter.unaskedFragments += strangerIds.size;
219
+ ctx.trace?.step(
220
+ "unaskedFragments",
221
+ recognised.filter((s) => strangerIds.has(s.payload)).map((s) =>
222
+ rItem(query.subarray(s.start, s.end), "fragment", s.payload, [
223
+ s.start,
224
+ s.end,
225
+ ])
226
+ ),
227
+ [],
228
+ "the only arguments are pieces of other forms whose continuations the question names none of — they answer other questions",
229
+ );
230
+ }
231
+ const bound = await bindArgument(args, strangerIds);
232
+ if (bound !== null) return bound;
233
+ // NO ARGUMENT BY BYTES IS NOT NO ARGUMENT. Recognition matches bytes, so
234
+ // `Man At Bath` (2Wiki title-cases its questions) is no site of the stored
235
+ // `Man at Bath`. When nothing recognised can bind, the points the
236
+ // consensus climb reaches that the question holds under the response's
237
+ // equivalence are arguments too (traverse.ts, `canonHeldPoints`) — read
238
+ // after the clean-resonance tier, which answers a near-identical question
239
+ // without any climb.
240
+ canonArgumentPending = ctx.canon !== null && args.length === 0;
241
+ strangers = strangerIds;
179
242
  }
180
243
 
181
244
  // The response's ONE top-k read (Precomputed.resonance) — the same list the
@@ -272,6 +335,20 @@ export async function recallByResonance(
272
335
  Math.sqrt(otherLen / Math.max(1, query.length)),
273
336
  );
274
337
 
338
+ // The argument the question holds only under the response's equivalence
339
+ // (above). Not gated on the whole-query resonance: a question whose only
340
+ // argument is a case variant (`… film Man At Bath work at?`) resonates
341
+ // below the significance bar as a whole.
342
+ if (canonArgumentPending) {
343
+ await pre.attention();
344
+ const held = canonHeldPoints(ctx, W2).filter((s) =>
345
+ ctx.store.hasNext(s.payload) &&
346
+ !answersOtherQuestions(ctx, s.payload, query.length, s.end - s.start)
347
+ );
348
+ const bound = await bindArgument(held, strangers);
349
+ if (bound !== null) return bound;
350
+ }
351
+
275
352
  // 2. Scaffolding-dominated.
276
353
  if (top.score >= sig) {
277
354
  // The committed points of attention ARE the shared climb's roots (same
@@ -403,7 +480,28 @@ export async function recallByResonance(
403
480
  forest[0].anchor,
404
481
  query.length * ctx.space.maxGroup + 1,
405
482
  );
406
- if (g !== null && voicesDisplacedFiller(ctx, query, anchorBytes, g)) {
483
+ // …and the same refusal read from the anchor's side: an anchor the query
484
+ // witnesses in every byte but one filler of its own is ANOTHER INSTANCE
485
+ // of the question (traverse.ts, `coInstanceFiller`). The query-side
486
+ // reading above needs the shared frame to dominate the QUERY, which a
487
+ // long slot defeats: `Where was the performer of song God (John Lennon
488
+ // Song) born?` elected `Where was Nicki Minaj (Nicki Minaj Song) born?`
489
+ // and answered Nicki Minaj's birthplace.
490
+ const asked = ctx._edgeAsked;
491
+ const co = asked === null
492
+ ? null
493
+ : coInstanceFiller(ctx, anchorBytes, [asked.index], asked.bytes);
494
+ if (co !== null) {
495
+ ctx.trace?.step(
496
+ "coInstanceAnchor",
497
+ [rItem(query, "query"), rNode(ctx, forest[0].anchor, "anchor")],
498
+ [rItem(anchorBytes.subarray(co.span[0], co.span[1]), "filler")],
499
+ "refused — the anchor is another instance of the question's frame, " +
500
+ "and its continuation speaks of its own filler",
501
+ );
502
+ } else if (
503
+ g !== null && voicesDisplacedFiller(ctx, query, anchorBytes, g)
504
+ ) {
407
505
  ctx.trace?.step(
408
506
  "displacedFiller",
409
507
  [rItem(query, "query"), rNode(ctx, forest[0].anchor, "anchor")],
@@ -37,6 +37,8 @@
37
37
  import type { MindContext } from "../types.js";
38
38
  import type { FrameInstance } from "../match.js";
39
39
  import { carriesFillers, distinct, follow, substituteAll } from "../match.js";
40
+ import { resolve } from "../primitives.js";
41
+ import { hubBound } from "../traverse.js";
40
42
  import { dominates } from "../../geometry.js";
41
43
  import { bytesEqual } from "../../bytes.js";
42
44
  import { restates } from "../derivation.js";
@@ -186,7 +188,6 @@ export async function bindReference(
186
188
  if (!distinct(referents)) {
187
189
  return fail("two slots name the same bytes — the binding is ambiguous");
188
190
  }
189
-
190
191
  // ── THE LICENCE ────────────────────────────────────────────────────────
191
192
  // Every instance must agree, against the first, that its continuation is its
192
193
  // own fillers carried through one fixed form. Unanimity, exactly as the
@@ -199,11 +200,33 @@ export async function bindReference(
199
200
  // discard them. Each goes through the shared projection, so an ambiguous
200
201
  // instance is disambiguated exactly as it would be anywhere else.
201
202
  const leadsNowhere = "an instance of the frame leads nowhere";
203
+ // A FACT FILED UNDER ITS FILLER IS NOT A CARRIAGE. When an instance's
204
+ // filler is itself a stored context leading to that instance's answer, the
205
+ // answer is something the corpus knows ABOUT that filler (`The place of
206
+ // birth of Peter Jackson is Wellington.` is a continuation of `Peter
207
+ // Jackson`), not a form any occupant takes — and two such facts can agree
208
+ // by chance (two people born in Wellington). `Run gcc hello.c` is filed
209
+ // under the question alone.
210
+ const filedUnderFiller = (inst: FrameInstance, cont: Uint8Array): boolean => {
211
+ const c = resolve(ctx, cont);
212
+ if (c === null) return false;
213
+ return inst.slots.some((s) => {
214
+ const f = resolve(ctx, s.filler);
215
+ return f !== null &&
216
+ ctx.store.nextFirst(f, hubBound(ctx)).includes(c);
217
+ });
218
+ };
202
219
  const first = await follow(ctx, frame[0].id, pre.guide);
203
220
  if (first === null || first.length === 0) return fail(leadsNowhere);
221
+ if (filedUnderFiller(frame[0], first)) {
222
+ return fail("the frame's answer is a fact filed under its filler");
223
+ }
204
224
  for (let i = 1; i < frame.length; i++) {
205
225
  const cont = await follow(ctx, frame[i].id, pre.guide);
206
226
  if (cont === null || cont.length === 0) return fail(leadsNowhere);
227
+ if (filedUnderFiller(frame[i], cont)) {
228
+ return fail("the frame's answer is a fact filed under its filler");
229
+ }
207
230
  if (
208
231
  !carriesFillers(first, fillersOf(frame[0]), cont, fillersOf(frame[i]))
209
232
  ) {
package/src/mind/mind.ts CHANGED
@@ -335,7 +335,9 @@ export class Mind implements MindContext {
335
335
  * reverse-recall disambiguation via `chooseAmong`. */
336
336
  _edgeGuide: Vec | null = null;
337
337
  /** See {@link MindContext._edgeAsked}. */
338
- _edgeAsked: { bytes: Uint8Array; index: WindowIndex } | null = null;
338
+ _edgeAsked:
339
+ | { bytes: Uint8Array; index: WindowIndex; points?: readonly number[] }
340
+ | null = null;
339
341
  /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
340
342
  * of a single response follows the SAME continuation for each ambiguous
341
343
  * context node. */
@@ -225,7 +225,21 @@ export class Precomputed {
225
225
  return this._attention ??= this.shared(
226
226
  "attention",
227
227
  () => climbAttentionAll(this.ctx, this.query, this.k),
228
- );
228
+ ).then((read) => {
229
+ // The climb's scored anchors are what the exact tier reads co-instances
230
+ // of the question from (traverse.ts, `relationFrames`): the
231
+ // approximate climb proposes, witnessing decides.
232
+ // A pick made before the points existed read less evidence than one
233
+ // made after, so the response's pick memo starts over (traverse.ts,
234
+ // guidedNext: a pick is a function of the store, the guide AND the
235
+ // points — traced and untraced responses must agree).
236
+ const asked = this.ctx._edgeAsked;
237
+ if (asked !== null && asked.points === undefined) {
238
+ asked.points = read.ranked.map((a) => a.anchor);
239
+ this.ctx._edgeChoice.clear();
240
+ }
241
+ return read;
242
+ });
229
243
  }
230
244
 
231
245
  private _weave?: Promise<WeaveInfo>;
@@ -773,13 +773,14 @@ export async function think(
773
773
  // Reading the used anchors' OWN bytes here says something stronger and
774
774
  // wrong: that nothing INSIDE what was voiced may be pivoted through. A
775
775
  // comparison's seat sentence legitimately contains further terms with
776
- // their own unrelated facts, and C3 pins exactly that — `Mona Lisa`, inside
777
- // the voiced seat `The Mona Lisa was painted by Leonardo da Vinci.`, leads
778
- // on to `Mona Lisa hangs in the Louvre`, which is about neither analog.
776
+ // their own unrelated facts — `Mona Lisa`, inside the voiced seat `The Mona
777
+ // Lisa was painted by Leonardo da Vinci.`, leads on to `Mona Lisa hangs in
778
+ // the Louvre`, which is about neither analog (test/29 C3: a candidate the
779
+ // walk takes only when the question names it).
779
780
  // The withheld content is the used anchors' CONTINUATIONS, so that is what
780
781
  // the containment rule reads: `speare` is contained in `Shakespeare wrote
781
782
  // 39 plays` and stays refused, while `Mona Lisa` appears in no withheld
782
- // continuation and the genuine further hop fires.
783
+ // continuation and stays a candidate.
783
784
  //
784
785
  // Only a mechanism carrying its own `used` set (cast/join) gets this: there
785
786
  // `preConsumed` is a deliberate, short list of the anchors the answer
@@ -9,6 +9,7 @@ import type { Attention, MindContext } from "./types.js";
9
9
  import { read, resolve } from "./primitives.js";
10
10
  import { countClusters } from "./attention.js";
11
11
  import {
12
+ coInstanceFiller,
12
13
  guidedFirst,
13
14
  hubBound,
14
15
  namedContinuations,
@@ -255,16 +256,12 @@ export function walkLayer(
255
256
  // 30 s); and raising it from 12 to 200 changed neither the answer nor
256
257
  // `pivotSteps` on the chain fixtures.
257
258
  const qv = pre.guide; // the response-wide guide IS the query's gist
258
- // WHOSE EXTENSION IS THIS? `voiced` is what the mechanism WITHHELD (the
259
- // pipeline sends the used anchors' CONTINUATIONS, not their bytes), so a
260
- // non-empty `voiced` means exactly what that note says: the grounding came
261
- // from a mechanism that carries its own short `used` set (cast/join) and
262
- // therefore OWNS the shape of its answer. The further terms inside such a
263
- // seat are legitimately followable (test/29 C3's `Mona Lisa` lives inside the
264
- // voiced seat and leads on to a fact about neither analog), which the law
265
- // expresses as the identity species of progress — `moves` — declared by the
266
- // reporter and never inferred from the mechanism's name.
267
- const producerOwnsShape = voiced.length > 0;
259
+ // NO PRODUCER OWNS THE NEXT STEP. A grounding that declared what it voiced
260
+ // (cast/join) used to MOVE through any further term inside its answer,
261
+ // unasked — `Who is the director of The Jerk?` grounded by CAST walked on to
262
+ // `The country of citizenship of Carl Reiner is American.` What it voiced
263
+ // still bounds what may be pivoted through (`voiced`, pivotInto); whether a
264
+ // step is taken is the naming rule below, the same for every mechanism.
268
265
  let hop = 0;
269
266
  let t: ReturnType<Rationale["enter"]> | undefined;
270
267
  // WHAT THE OFFERED STEP WOULD BE. The law decides, so the accepted step is
@@ -439,7 +436,7 @@ export function walkLayer(
439
436
  const hop = guidedFirst(ctx, pivot);
440
437
  named = names !== null && hop !== undefined && names.includes(hop);
441
438
  }
442
- if (!named && !producerOwnsShape && closed(d)) {
439
+ if (!named && closed(d)) {
443
440
  ctx.trace?.step(
444
441
  "pivotUnasked",
445
442
  [rItem(cur, "answer"), rNode(ctx, pivot, "pivot")],
@@ -449,15 +446,14 @@ export function walkLayer(
449
446
  return null;
450
447
  }
451
448
  pending = { kind: "pivot", cur, pivot, fc };
452
- // Offered with the identity species when the grounding declared what it
453
- // speaks for, or when the question names the step; otherwise the law
454
- // requires this step to carry question material the grounding left
449
+ // Offered with the identity species when the question names the step;
450
+ // otherwise the law requires this step to carry question material the grounding left
455
451
  // unaccounted, which is the drift the extension tests pin — refused by the
456
452
  // law's own measure, not by a private window test.
457
453
  return {
458
454
  product: fc,
459
455
  contains: true,
460
- reaches: producerOwnsShape || named,
456
+ reaches: named,
461
457
  cost: STEP,
462
458
  };
463
459
  };
@@ -833,6 +829,28 @@ export function fusionLayer(
833
829
  // again would only restate it. Deliberately NOT a magnitude measure:
834
830
  // it fires on exact content-addressed recurrence, not on how strongly
835
831
  // the root resonates.
832
+ // ANOTHER INSTANCE IS NOT ANOTHER TOPIC. A root the question witnesses
833
+ // in every byte but one contiguous span is the question's own frame
834
+ // around a different filler (evidence.ts, `coInstanceFiller`): the climb
835
+ // committed `Where was Peter Jackson born?` as a second point of
836
+ // `Where was the director of film Beat Girl born?` on the frame windows
837
+ // alone, and fusing it voiced Peter Jackson's birthplace beside the
838
+ // answer. Such a root says what the question's relation IS (traverse.ts,
839
+ // `byCoInstance`), never what it asks about.
840
+ const asked = ctx._edgeAsked;
841
+ if (asked !== null) {
842
+ const form = read(ctx, root.anchor, 2 * query.length);
843
+ const co = coInstanceFiller(ctx, form, [asked.index], asked.bytes);
844
+ if (co !== null) {
845
+ ctx.trace?.step(
846
+ "coInstanceRoot",
847
+ [rNode(ctx, root.anchor, "point", root.vote)],
848
+ [rItem(form.subarray(co.span[0], co.span[1]), "filler")],
849
+ "the question witnesses this point in every byte but one filler of its own — another instance of the question's frame, not a further topic",
850
+ );
851
+ continue;
852
+ }
853
+ }
836
854
  const cont = await follow(ctx, root.anchor, qv);
837
855
  if (
838
856
  cont !== null && cont.length > 0 &&
@@ -307,8 +307,9 @@ export async function joinWithBridge(
307
307
  * Shakespeare" (id 676) and the pivot hopped through "speare" (id 606, not
308
308
  * in `consumed`) straight into that analog's own biography, which is exactly
309
309
  * what the comparison had refused to voice. The rule is CONTAINMENT, not
310
- * overlap, so C3's genuine further hop — "Mona Lisa", a term inside the seat
311
- * sentence but part of NEITHER analog — still fires. */
310
+ * overlap, so C3's further term — "Mona Lisa", inside the seat sentence but
311
+ * part of NEITHER analog — stays a candidate; whether the walk steps through
312
+ * it is the naming rule's decision (reasoning.ts, evidence.md). */
312
313
  export async function pivotInto(
313
314
  ctx: MindContext,
314
315
  answer: Uint8Array,