counterparts 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONSTITUTION.md +68 -0
- package/LICENSE +21 -0
- package/README.md +348 -0
- package/docs/ELI5.md +84 -0
- package/docs/QUICKSTART.md +1258 -0
- package/docs/module-map.md +81 -0
- package/package.json +63 -0
- package/src/adapters/claude-code/CONTRACT.md +606 -0
- package/src/adapters/claude-code/INTERFACE-GAPS.md +655 -0
- package/src/adapters/claude-code/NOTES.md +1055 -0
- package/src/adapters/claude-code/bin/hook.ts +820 -0
- package/src/adapters/claude-code/bin/runner.ts +522 -0
- package/src/adapters/claude-code/config.ts +778 -0
- package/src/adapters/claude-code/credentials.ts +199 -0
- package/src/adapters/claude-code/doctor.ts +2557 -0
- package/src/adapters/claude-code/embed-client.ts +617 -0
- package/src/adapters/claude-code/hooks.ts +2240 -0
- package/src/adapters/claude-code/index.ts +224 -0
- package/src/adapters/claude-code/interpret-client.ts +395 -0
- package/src/adapters/claude-code/page-writer.ts +462 -0
- package/src/adapters/claude-code/primacy.ts +162 -0
- package/src/adapters/claude-code/spawn.ts +241 -0
- package/src/adapters/claude-code/standdown.ts +536 -0
- package/src/adapters/claude-code/transcript.ts +661 -0
- package/src/adapters/claude-code/vectors.ts +415 -0
- package/src/adapters/cli/CONTRACT.md +255 -0
- package/src/adapters/cli/INTERFACE-GAPS.md +350 -0
- package/src/adapters/cli/NOTES.md +936 -0
- package/src/adapters/cli/bin/counterparts.ts +74 -0
- package/src/adapters/cli/commands.ts +6095 -0
- package/src/adapters/cli/export.ts +766 -0
- package/src/adapters/cli/index.ts +103 -0
- package/src/adapters/cli/install.ts +670 -0
- package/src/adapters/cli/removal.ts +1186 -0
- package/src/adapters/cli/repair-dates.ts +659 -0
- package/src/adapters/cli/self-page.ts +229 -0
- package/src/adapters/cli/snapshot.ts +279 -0
- package/src/adapters/cli/start-fresh.ts +1228 -0
- package/src/adapters/config-path.ts +342 -0
- package/src/adapters/dashboard/CONTRACT.md +67 -0
- package/src/adapters/dashboard/INTERFACE-GAPS.md +139 -0
- package/src/adapters/dashboard/NOTES.md +474 -0
- package/src/adapters/dashboard/activity.ts +121 -0
- package/src/adapters/dashboard/ansi.ts +82 -0
- package/src/adapters/dashboard/bin/dashboard.ts +479 -0
- package/src/adapters/dashboard/browse.ts +298 -0
- package/src/adapters/dashboard/identity.ts +182 -0
- package/src/adapters/dashboard/index.ts +183 -0
- package/src/adapters/dashboard/layout.ts +149 -0
- package/src/adapters/dashboard/registries.ts +291 -0
- package/src/adapters/dashboard/resolve.ts +206 -0
- package/src/adapters/dashboard/source.ts +48 -0
- package/src/adapters/dashboard/status.ts +327 -0
- package/src/adapters/dashboard/stories.ts +194 -0
- package/src/adapters/dashboard/web/app.html +1731 -0
- package/src/adapters/dashboard/web/brain.html +463 -0
- package/src/adapters/dashboard/web/fired.ts +31 -0
- package/src/adapters/dashboard/web/flow.ts +457 -0
- package/src/adapters/dashboard/web/narrate.ts +1042 -0
- package/src/adapters/dashboard/web/reveal.ts +135 -0
- package/src/adapters/dashboard/web/server.ts +287 -0
- package/src/adapters/dashboard/web/views.ts +1744 -0
- package/src/adapters/expansions.ts +569 -0
- package/src/adapters/fired.ts +1580 -0
- package/src/adapters/mcp/CONTRACT.md +240 -0
- package/src/adapters/mcp/INTERFACE-GAPS.md +271 -0
- package/src/adapters/mcp/NOTES.md +205 -0
- package/src/adapters/mcp/bin/serve.ts +349 -0
- package/src/adapters/mcp/deliberate.ts +619 -0
- package/src/adapters/mcp/index.ts +101 -0
- package/src/adapters/mcp/protocol.ts +170 -0
- package/src/adapters/mcp/server.ts +1740 -0
- package/src/adapters/mcp/stdio.ts +56 -0
- package/src/adapters/mcp/tools.ts +779 -0
- package/src/adapters/scopes.ts +657 -0
- package/src/adapters/sessions.ts +402 -0
- package/src/adapters/snapshots.ts +1069 -0
- package/src/adapters/stance-env.ts +137 -0
- package/src/core/associate/CONTRACT.md +118 -0
- package/src/core/associate/INTERFACE-GAPS.md +173 -0
- package/src/core/associate/NOTES.md +194 -0
- package/src/core/associate/buffer.ts +66 -0
- package/src/core/associate/edges.ts +188 -0
- package/src/core/associate/index.ts +570 -0
- package/src/core/associate/pending.ts +383 -0
- package/src/core/associate/spread.ts +149 -0
- package/src/core/associate/tunables.ts +123 -0
- package/src/core/bridge.ts +272 -0
- package/src/core/briefing.ts +67 -0
- package/src/core/counterpart.ts +3855 -0
- package/src/core/encode/CONTRACT.md +151 -0
- package/src/core/encode/INTERFACE-GAPS.md +135 -0
- package/src/core/encode/NOTES.md +146 -0
- package/src/core/encode/aliases.ts +71 -0
- package/src/core/encode/battery.ts +284 -0
- package/src/core/encode/chunk.ts +265 -0
- package/src/core/encode/emotion.ts +119 -0
- package/src/core/encode/floor.ts +76 -0
- package/src/core/encode/index.ts +100 -0
- package/src/core/encode/precision.ts +162 -0
- package/src/core/encode/preselect.ts +269 -0
- package/src/core/encode/salience.ts +108 -0
- package/src/core/encode/secrets.ts +265 -0
- package/src/core/encode/tunables.ts +99 -0
- package/src/core/encode/types.ts +286 -0
- package/src/core/encode/words.ts +75 -0
- package/src/core/handoff/CONTRACT.md +199 -0
- package/src/core/handoff/INTERFACE-GAPS.md +166 -0
- package/src/core/handoff/NOTES.md +206 -0
- package/src/core/handoff/index.ts +957 -0
- package/src/core/mint.ts +279 -0
- package/src/core/observer.ts +28 -0
- package/src/core/physics/CONTRACT.md +341 -0
- package/src/core/physics/NOTES.md +155 -0
- package/src/core/physics/clock.ts +51 -0
- package/src/core/physics/index.ts +1054 -0
- package/src/core/prospective/CONTRACT.md +137 -0
- package/src/core/prospective/INTERFACE-GAPS.md +112 -0
- package/src/core/prospective/NOTES.md +160 -0
- package/src/core/prospective/derive.ts +188 -0
- package/src/core/prospective/index.ts +1030 -0
- package/src/core/prospective/tunables.ts +72 -0
- package/src/core/prospective/windows.ts +187 -0
- package/src/core/recall/CONTRACT.md +326 -0
- package/src/core/recall/INTERFACE-GAPS.md +163 -0
- package/src/core/recall/NOTES.md +472 -0
- package/src/core/recall/activate.ts +521 -0
- package/src/core/recall/cues.ts +275 -0
- package/src/core/recall/gate.ts +441 -0
- package/src/core/recall/index.ts +692 -0
- package/src/core/recall/probe.ts +239 -0
- package/src/core/recall/reference.ts +201 -0
- package/src/core/recall/render.ts +285 -0
- package/src/core/recall/session.ts +403 -0
- package/src/core/recall/tunables.ts +329 -0
- package/src/core/remember/CONTRACT.md +293 -0
- package/src/core/remember/INTERFACE-GAPS.md +189 -0
- package/src/core/remember/NOTES.md +373 -0
- package/src/core/remember/fallback.ts +452 -0
- package/src/core/remember/index.ts +103 -0
- package/src/core/remember/owner-strike-seam.ts +479 -0
- package/src/core/remember/proposals.ts +609 -0
- package/src/core/remember/spans.ts +1310 -0
- package/src/core/remember/tunables.ts +111 -0
- package/src/core/remember/updates.ts +198 -0
- package/src/core/retrieval.ts +96 -0
- package/src/core/revision.ts +441 -0
- package/src/core/schemas/CONTRACT.md +190 -0
- package/src/core/schemas/INTERFACE-GAPS.md +172 -0
- package/src/core/schemas/NOTES.md +260 -0
- package/src/core/schemas/aliases.ts +160 -0
- package/src/core/schemas/index.ts +1459 -0
- package/src/core/schemas/tunables.ts +44 -0
- package/src/core/schemas/types.ts +387 -0
- package/src/core/self/CONTRACT.md +459 -0
- package/src/core/self/INTERFACE-GAPS.md +284 -0
- package/src/core/self/NOTES.md +1057 -0
- package/src/core/self/briefing.ts +980 -0
- package/src/core/self/episodes.ts +483 -0
- package/src/core/self/freeze.ts +103 -0
- package/src/core/self/identity.ts +604 -0
- package/src/core/self/index.ts +2130 -0
- package/src/core/self/journal-file.ts +760 -0
- package/src/core/self/page.ts +404 -0
- package/src/core/self/tunables.ts +244 -0
- package/src/core/self/writer.ts +714 -0
- package/src/core/sleep/CONTRACT.md +298 -0
- package/src/core/sleep/INTERFACE-GAPS.md +262 -0
- package/src/core/sleep/NOTES.md +770 -0
- package/src/core/sleep/briefing.ts +86 -0
- package/src/core/sleep/consolidate.ts +241 -0
- package/src/core/sleep/cycle.ts +598 -0
- package/src/core/sleep/decay.ts +237 -0
- package/src/core/sleep/dedup.ts +373 -0
- package/src/core/sleep/index.ts +112 -0
- package/src/core/sleep/log.ts +116 -0
- package/src/core/sleep/markers.ts +164 -0
- package/src/core/sleep/prune.ts +170 -0
- package/src/core/sleep/strength-cache.ts +176 -0
- package/src/core/sleep/tunables.ts +135 -0
- package/src/core/sleep/types.ts +542 -0
- package/src/core/store/CONTRACT.md +446 -0
- package/src/core/store/NOTES.md +1068 -0
- package/src/core/store/cache.ts +709 -0
- package/src/core/store/db.ts +275 -0
- package/src/core/store/errors.ts +134 -0
- package/src/core/store/index.ts +2450 -0
- package/src/core/store/operational.ts +610 -0
- package/src/core/store/owner-op-seam.ts +428 -0
- package/src/core/store/paths.ts +494 -0
- package/src/core/store/prose.ts +165 -0
- package/src/core/store/render.ts +87 -0
- package/src/core/store/walk-seam.ts +78 -0
- package/src/core/types.ts +68 -0
- package/tools/install-loop/run.sh +1063 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# `schemas/` — interface gaps
|
|
2
|
+
|
|
3
|
+
*What this module needed from a neighbour and worked around instead of editing,
|
|
4
|
+
and what a neighbour will need from this module before the wiring can be right.
|
|
5
|
+
Each entry names the owner, the workaround now in the code, and the real fix. An
|
|
6
|
+
item stays listed until a test proves the wiring.*
|
|
7
|
+
|
|
8
|
+
## 0. Nothing was stubbed, and the shared rule is imported
|
|
9
|
+
|
|
10
|
+
`physics/` covered every arithmetic need — `applyChallenge`, `pruneVerdict`,
|
|
11
|
+
`successorSeed`, `supersedeRecord`, `revisionBar`, `band`, `strength`,
|
|
12
|
+
`clampSalienceAtSeam` — and `store/` covered every durability need. **No local
|
|
13
|
+
copy of a physics rule or a store operation exists in this directory.**
|
|
14
|
+
|
|
15
|
+
Three symbols are imported from `encode/`, all leaf files, no cycle:
|
|
16
|
+
|
|
17
|
+
- `occursAsWholeWord` from `encode/words.js` — **SEAMS §7, mandatory**. One
|
|
18
|
+
whole-word definition, shared with preselection; a second is forbidden. A test
|
|
19
|
+
imports both this module's alias lookup and `occursAsWholeWord` and proves they
|
|
20
|
+
agree term-for-term, and a source scan asserts no boundary regex is defined
|
|
21
|
+
here.
|
|
22
|
+
- `gateAliases` from `encode/aliases.js` — reuse, not obligation. It already
|
|
23
|
+
implements "verbatim in source, individually verified, dropped rather than
|
|
24
|
+
fatal" on top of the one whole-word rule, and it drops an alias that is itself
|
|
25
|
+
a credential. Re-implementing it would have been a second alias policy.
|
|
26
|
+
- `containsSecret` from `encode/secrets.js` — the ops rule: a credential must
|
|
27
|
+
never become an entity the store indexes. Birth refuses the name outright
|
|
28
|
+
(`name-is-secret`), because unlike an alias, a name has no drop-and-continue.
|
|
29
|
+
|
|
30
|
+
## 1. `store/` has no durable event log — the revision story is session-scoped
|
|
31
|
+
|
|
32
|
+
**Owner:** `store/` (box 2), with `dashboard/` as the consumer.
|
|
33
|
+
**Needed:** contract §5 says every pressure increment is logged and the
|
|
34
|
+
dashboard's story view reads them; constitution line 16 says the owner can see
|
|
35
|
+
what changed and why.
|
|
36
|
+
**Have:** `Store` emits `StoreEvent` to an in-memory ring plus an `onEvent`
|
|
37
|
+
callback. There is no events table and no append-only telemetry file.
|
|
38
|
+
**Workaround:** `Schemas` keeps its own ring and its own `increments[]`, and
|
|
39
|
+
`story(beliefId)` returns the durable half (the version rows the store retained,
|
|
40
|
+
the live pressure/bar on the head) plus the increments **this session** logged.
|
|
41
|
+
Restart the process and the increment history is gone; the pressure number
|
|
42
|
+
survives, the story of how it got there does not.
|
|
43
|
+
**Real fix:** an `events` table in box 2 with a bounded retention sweep beside
|
|
44
|
+
`pruneSupersededVersions`, or a documented obligation on every adapter to
|
|
45
|
+
persist `onEvent`. The second is cheaper and worse: it makes the telemetry an
|
|
46
|
+
adapter's discipline, which is exactly the shape scar §2.4 keeps punishing.
|
|
47
|
+
|
|
48
|
+
## 2. `store/` has no meta query — enumeration is a full scan at open
|
|
49
|
+
|
|
50
|
+
**Owner:** `store/` (box 2).
|
|
51
|
+
**Needed:** "every belief of entity X", "every entity row" — the module's most
|
|
52
|
+
common reads.
|
|
53
|
+
**Have:** `Store.list({ type, kind, band, archived })`. `meta` lives in prose,
|
|
54
|
+
not in a column, so no filter can reach `role` or `entityId`.
|
|
55
|
+
**Workaround:** one scan at open (`list({type:"schema"})` → read each prose file
|
|
56
|
+
→ index `role` / `entityId` / `name` / `aliases` in memory), and every subsequent
|
|
57
|
+
write updates the index. Operational state (archived, superseded, physics) is
|
|
58
|
+
always read live from box 2; only the prose-only fields are cached.
|
|
59
|
+
**Consequence:** open cost is O(schema rows), and a second `Schemas` instance
|
|
60
|
+
over the same store does not see the first's writes. Fine for one process; the
|
|
61
|
+
moment two adapters open the store concurrently it is stale-cache territory.
|
|
62
|
+
**Real fix:** either a `meta_index` table (key, value, memory_id) box 2 maintains
|
|
63
|
+
on write, or first-class `schema_element` columns. A `Store.listByMeta(key,
|
|
64
|
+
value)` is the smallest useful shape.
|
|
65
|
+
|
|
66
|
+
## 3. `recall/` should take its alias map from here — SEAMS §6 wiring
|
|
67
|
+
|
|
68
|
+
**Owner:** the coordinator. **Do not edit `recall/`.**
|
|
69
|
+
`recall/INTERFACE-GAPS.md` §2 records that `Turn.aliases?: ReadonlyMap<string,
|
|
70
|
+
readonly string[]>` is *borrowed from the caller*, and that a caller who forgets
|
|
71
|
+
it silently loses the ambiguous-handle safety half.
|
|
72
|
+
|
|
73
|
+
**This module now owns that map.** The wiring is one line at the call site:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
recall.turn({ ...turn, aliases: schemas.aliasMap() });
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`aliasMap()` returns handle (lower-cased) → sorted entity ids, for LIVE entities
|
|
80
|
+
only — a faded entity stops disambiguating because it stops surfacing.
|
|
81
|
+
`aliasIndex().ambiguous()` lists the handles that resolve to more than one
|
|
82
|
+
entity, if the caller wants the count rather than the map. Keys are lower-cased
|
|
83
|
+
because the shared whole-word matcher is case-insensitive without an option;
|
|
84
|
+
`handleKey()` is exported so recall never has to guess the folding.
|
|
85
|
+
|
|
86
|
+
**The gap stays open until a test in the composed pipeline proves the map is
|
|
87
|
+
actually passed** — the safety half is unreachable from inside either module.
|
|
88
|
+
|
|
89
|
+
## 4. `encode/`'s `SchemaSlice` has no elision count
|
|
90
|
+
|
|
91
|
+
**Owner:** `encode/`.
|
|
92
|
+
Contract §5 G8 requires elided items to be announced as a count, never silently
|
|
93
|
+
omitted. `encode/preselect.ts`'s `SchemaSlice` carries `beliefs`, `currentState`,
|
|
94
|
+
`identityCore` and no count field.
|
|
95
|
+
**Workaround:** `SchemaSliceOut` is `SchemaSlice`-shaped plus `elided`, and it is
|
|
96
|
+
structurally assignable to `SchemaSlice`, so `preselectSchemas({ schemas:
|
|
97
|
+
schemas.slices() })` type-checks today. `elided` is 0 in this build: nothing is
|
|
98
|
+
dropped, so nothing needs announcing.
|
|
99
|
+
**Real fix, when a slice ever elides:** `elided?: number` on `SchemaSlice`, read
|
|
100
|
+
by `renderSchemaContext` the way it already announces the identity-core
|
|
101
|
+
truncation. Until then the field is carried and ignored, which is honest but not
|
|
102
|
+
enforced anywhere.
|
|
103
|
+
|
|
104
|
+
## 5. `self/` must mint the identity core in this module's shape
|
|
105
|
+
|
|
106
|
+
**Owner:** `self/`.
|
|
107
|
+
Birth refuses `kind: "self"` in two independent layers, so the identity core
|
|
108
|
+
cannot be created through this module — correct, and deliberate. But the
|
|
109
|
+
placement rule (`status-on-identity-refused`) and the "a name resolving to the
|
|
110
|
+
identity core is not a birth site" layer both need to *see* the self schema.
|
|
111
|
+
|
|
112
|
+
**The obligation:** `self/` writes its core as `type: "schema"`, `kind: "self"`,
|
|
113
|
+
with `meta: { role: "entity", name: <the core's name>, aliases: [...] }`. Without
|
|
114
|
+
`meta.role` this module will not index it, and the ~72 KB refusal degrades to
|
|
115
|
+
"entity-unknown" — a refusal, but the wrong one, and a *silent* weakening of the
|
|
116
|
+
guarantee that matters most here.
|
|
117
|
+
**Test to write at the seam:** mint the core through `self/`, then assert
|
|
118
|
+
`schemas.addCurrentState({ entityId: coreId, ... }).reason ===
|
|
119
|
+
"status-on-identity-refused"`.
|
|
120
|
+
|
|
121
|
+
**Deliberate, and stated here so it is not mistaken for an oversight:**
|
|
122
|
+
`SchemaSliceOut` carries no `identityCore`. Encode's `SchemaSlice` has the field
|
|
123
|
+
and `renderSchemaContext` renders it under a byte budget — but *what the core
|
|
124
|
+
says* is `self/`'s to compose and to compress, not this module's to spill. The
|
|
125
|
+
slice from here is names, aliases, beliefs and current state; a caller that wants
|
|
126
|
+
the core in the same prompt merges `self/`'s rendering into the slice it passes
|
|
127
|
+
to `preselectSchemas`. If that merge never materialises, the symptom is a self
|
|
128
|
+
schema whose core is invisible at encoding — worth a seam test either way.
|
|
129
|
+
|
|
130
|
+
## 6. `sleep/` owns the fade cadence
|
|
131
|
+
|
|
132
|
+
`fadeSweep(day)` is pure arithmetic on a lived day and does no scheduling. Death
|
|
133
|
+
by decay only happens if something calls it — this is scar §2.17 in its most
|
|
134
|
+
literal form (*write paths ship, curation paths starve*), and `lifecycle()`'s
|
|
135
|
+
`noExitsYet` flag is the tripwire that fires if nobody ever does.
|
|
136
|
+
|
|
137
|
+
**The obligation:** `sleep/`'s consolidation cycle calls `schemas.fadeSweep(d)`
|
|
138
|
+
on the active-day clock, and reports the returned counts alongside its own prune
|
|
139
|
+
counts.
|
|
140
|
+
|
|
141
|
+
## 7. `pruneVerdict`'s `inLiveRevisionChain` is supplied as `false` for entities
|
|
142
|
+
|
|
143
|
+
**Owner:** shared with `sleep/`.
|
|
144
|
+
Entities are never superseded — only beliefs are — so an entity is never in a
|
|
145
|
+
live revision chain and the flag is a constant here. **Whoever prunes BELIEF rows
|
|
146
|
+
must not copy that constant:** a belief in an open supersede chain is exactly the
|
|
147
|
+
case the flag exists for, and the answer comes from `store.versions()` /
|
|
148
|
+
`superseded_by`, not from this module.
|
|
149
|
+
|
|
150
|
+
## 8. `remember/`'s `updates:` resolution overlaps this module's (SEAMS §11)
|
|
151
|
+
|
|
152
|
+
SEAMS §11 lists `CandidateSource` + `IdResolver` for `updates:` resolution as
|
|
153
|
+
coming "from store/schemas". `Schemas.challengeBelief` resolves the declared
|
|
154
|
+
target itself, through `store.resolve()`, and refuses `target-unresolvable`
|
|
155
|
+
rather than throwing. If `remember/` also resolves — with its own
|
|
156
|
+
`UPDATES_FLOOR` / `UPDATES_MARGIN`, both uncalibrated — then a declaration is
|
|
157
|
+
matched twice by two different rules.
|
|
158
|
+
|
|
159
|
+
**Recommendation for the coordinator:** `remember/` resolves *text to a candidate
|
|
160
|
+
id* (its matching problem); this module resolves *an id through the supersede
|
|
161
|
+
chain* (the store's problem). They compose in that order and must not be merged.
|
|
162
|
+
|
|
163
|
+
**CLOSED 2026-09-04 — `src/core/revision.ts` (SEAMS item O) is the composition,
|
|
164
|
+
in exactly that order.** The door hands the applier `resolved ?? declared`, and
|
|
165
|
+
the applier walks it through `store.resolve()` before anything else; no second
|
|
166
|
+
matcher was written, and `UPDATES_FLOOR` / `UPDATES_MARGIN` stayed where they
|
|
167
|
+
are. The applier also carries the matcher's `method` through
|
|
168
|
+
`mint.directionOf`, so the freeze seam and the pressure path read ONE verdict
|
|
169
|
+
about what a declaration meant — a `content` match is a restatement and never
|
|
170
|
+
becomes a challenge. Proof: `test/seams.test.ts` — "one apply per DECLARATION —
|
|
171
|
+
an address that resolves to nothing is counted, not silent", "a CONFIRMATION is
|
|
172
|
+
not a challenge — a matched restatement adds no pressure".
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# `schemas/` — implementation notes
|
|
2
|
+
|
|
3
|
+
*Where the CONTRACT was silent, what this module decided, and why. Decisions are
|
|
4
|
+
defaults (constitution line 13): every one of these is revisable, and the ones
|
|
5
|
+
carrying a measurement debt say so.*
|
|
6
|
+
|
|
7
|
+
## 1. One id family, three roles
|
|
8
|
+
|
|
9
|
+
The contract describes an entity schema as *sectioned prose* — core, current
|
|
10
|
+
state, beliefs, protected, lineage. This build stores the sections as
|
|
11
|
+
**separate rows in the `sch_` family**, discriminated by
|
|
12
|
+
`meta.role: "entity" | "belief" | "current-state"`, rather than as sections
|
|
13
|
+
inside one document.
|
|
14
|
+
|
|
15
|
+
Reason: physics operates on rows. A belief needs its own strength, its own
|
|
16
|
+
pressure field, its own supersede chain and its own decay — §5.6's pressure
|
|
17
|
+
accumulator is a *field on the target row*, and there is no target row if the
|
|
18
|
+
belief is a paragraph inside its entity's body. The sectioned-prose *view*
|
|
19
|
+
is reconstructible (`slices()`, `beliefs()`, `currentState()`); the reverse is
|
|
20
|
+
not. It also means `no silent destruction` and archive-on-overwrite apply to a
|
|
21
|
+
belief for free, because they already apply to every row.
|
|
22
|
+
|
|
23
|
+
Cost, recorded: an entity's own document is no longer the whole schema. An
|
|
24
|
+
owner reading one entity row sees the stub and its names, not the beliefs.
|
|
25
|
+
*(Written on the file floor, where that document was `prose/schema/sch_*.md` and
|
|
26
|
+
the cost was a file that did not say everything; since the floor — schema v6,
|
|
27
|
+
2026-09-20 — it is a row, and the cost is the same one.)*
|
|
28
|
+
The dashboard's schema view has to join. If that trade turns out wrong, the fix
|
|
29
|
+
is a render pass, not a storage change.
|
|
30
|
+
|
|
31
|
+
## 2. A belief's `kind` is its ENTITY's kind
|
|
32
|
+
|
|
33
|
+
Fixed at mint, never re-derived. This is what makes "revision bars differ by
|
|
34
|
+
kind" arithmetic instead of policy: a belief about a person inherits iota 0.8
|
|
35
|
+
and the slow-kind daily force cap; project status on an entity inherits 0.5 and
|
|
36
|
+
no cap — "a loosening chosen out loud" (§4.3), so world-state flips on one clear
|
|
37
|
+
correction while a model of a friend takes ~3 lived days.
|
|
38
|
+
|
|
39
|
+
## 3. What the successor belief is made of
|
|
40
|
+
|
|
41
|
+
On REVISE the contract says the old element is retained with lineage; it does not
|
|
42
|
+
say what the new one says or how strongly it is held. This build:
|
|
43
|
+
|
|
44
|
+
- **body** — `input.statement`, defaulting to the challenging memory's own prose.
|
|
45
|
+
The engine mints; the model never hands over a belief edit (§5 G1).
|
|
46
|
+
- **salience** — the challenger's. A belief is held as strongly as the evidence
|
|
47
|
+
that carried it, and inheriting the *old* belief's salience would make a
|
|
48
|
+
revised belief permanently as strong as the thing it replaced.
|
|
49
|
+
- **physics** — `physics.successorSeed(target)`: pressure resets to 0, identity
|
|
50
|
+
membership is inherited. Not this module's rule; physics owns it.
|
|
51
|
+
|
|
52
|
+
## 4. A re-mention credits at the `referenced` tier
|
|
53
|
+
|
|
54
|
+
Deliberate, and the arithmetic depends on it. `surfaced` (w = 0.25) leaves a
|
|
55
|
+
daily-mentioned stub at base 0.03, below `PHI_PRUNE` — a stub that is mentioned
|
|
56
|
+
every day would still fade, which is not the lifecycle the contract describes. A
|
|
57
|
+
mention is the entity *being used*, and physics already refuses the credit on the
|
|
58
|
+
birth day and on any day already credited, so "accumulates" means distinct lived
|
|
59
|
+
days by construction.
|
|
60
|
+
|
|
61
|
+
## 5. What kills an entity (contract open question 1)
|
|
62
|
+
|
|
63
|
+
`physics.pruneVerdict` **plus one blocker of this module's own**: an entity with
|
|
64
|
+
live beliefs or live current-state rows attached does not fade, however weak its
|
|
65
|
+
own row is. That is the "zero live attached memories" rule the open question
|
|
66
|
+
calls obvious, scoped to the elements this module can see.
|
|
67
|
+
|
|
68
|
+
Not answered, still open: *a person with one faded memory is not the same as a
|
|
69
|
+
project that ended.* Both fade here on the same five physics conditions. The
|
|
70
|
+
per-kind distinction, if it is real, belongs in `KINDS` as a per-kind
|
|
71
|
+
`D_FLOOR_DAYS`, not in a special case here.
|
|
72
|
+
|
|
73
|
+
## 6. A faded name mentioned again is a NEW birth
|
|
74
|
+
|
|
75
|
+
Death by decay archives the entity; `store` has no unarchive, and this build does
|
|
76
|
+
not want one. Re-mention mints a fresh stub — the old entity died, and the
|
|
77
|
+
mention is a new place for memories to attach. The old id stays resolvable and
|
|
78
|
+
keeps its exit in the lifecycle counts, and the birth emits
|
|
79
|
+
`schema.birth.after-fade` with the prior id, so born → faded → born again is a
|
|
80
|
+
countable fact rather than an argument (scar §2.17's shape: if the churn is a
|
|
81
|
+
defect, telemetry has to be able to say so).
|
|
82
|
+
|
|
83
|
+
The alternative — reviving the archived row — would need an unarchive verb on the
|
|
84
|
+
store and would make "died by decay" a lie the first time it happened.
|
|
85
|
+
|
|
86
|
+
## 7. Near collision is token containment, and it is CAL
|
|
87
|
+
|
|
88
|
+
"Mike" against "Mike Chen" refuses. The rule is token containment because that is
|
|
89
|
+
the exact shape §14.3 names, it needs no embedder, and it fails toward refusal
|
|
90
|
+
rather than toward a silent merge. **It is not a measured similarity bar**
|
|
91
|
+
(scar §2.8): v1's 0.60 embedding floor was intuited and inert, because arbitrary
|
|
92
|
+
same-corpus pairs already sat at 0.576. When the real name space is measured, the
|
|
93
|
+
measured rule replaces `collision()` — which is why it is one exported function
|
|
94
|
+
and a tunable, not an `if` in the birth path.
|
|
95
|
+
|
|
96
|
+
Known false negative: "Mike Chen" against "Michael Chen" reads as `none`. An
|
|
97
|
+
edit-distance or phonetic arm would catch it and would also start refusing real
|
|
98
|
+
distinct names; neither direction should be chosen without the measurement.
|
|
99
|
+
|
|
100
|
+
## 8. The per-chunk birth counter is process-local
|
|
101
|
+
|
|
102
|
+
`MAX_BIRTHS_PER_CHUNK` is enforced against an in-memory map keyed by `chunkRef`.
|
|
103
|
+
Two `Schemas` instances over one store — or one process restart mid-run — do not
|
|
104
|
+
share the count. Acceptable because the cap guards against *one confused chunk*
|
|
105
|
+
inventing five entities, which is a within-run phenomenon; a chunk is not
|
|
106
|
+
re-interpreted across processes. If interpretation ever becomes resumable, the
|
|
107
|
+
counter moves into box 2 with the run record.
|
|
108
|
+
|
|
109
|
+
## 9. Confidence, and the protected queue
|
|
110
|
+
|
|
111
|
+
Both contract §4 items marked **PROPOSED** are implemented as proposed:
|
|
112
|
+
|
|
113
|
+
- **No separate belief confidence.** Strength alone carries "weakly-held beliefs
|
|
114
|
+
fall easier", because the revision bar is `iota × strength(old)`. One number,
|
|
115
|
+
and it cannot go inert unnoticed.
|
|
116
|
+
- **No second-signature queue for protection.** `addBelief({ protected: true })`
|
|
117
|
+
sets the flag at the mint, and protected beliefs refuse every revision path.
|
|
118
|
+
The corrective that shipped and worked in v1 was legibility, not a cadence:
|
|
119
|
+
every protected element is enumerable at will (`beliefs()` carries the flag).
|
|
120
|
+
Scar §2.19's risk — permanent ink at the lowest write bar — is live and named.
|
|
121
|
+
|
|
122
|
+
## 10. Protection is checked BEFORE the arithmetic
|
|
123
|
+
|
|
124
|
+
A protected belief accrues no pressure at all, rather than accruing pressure that
|
|
125
|
+
can never cash. Permanence that quietly builds a case against itself is a
|
|
126
|
+
different and worse guarantee than permanence, and the pressure field would grow
|
|
127
|
+
without bound behind a refusal nobody sees.
|
|
128
|
+
|
|
129
|
+
## 11. Telemetry: names are hashed, bodies never appear
|
|
130
|
+
|
|
131
|
+
A schema name is author content that the store indexes, so events carry
|
|
132
|
+
`nameHash`, kinds, counts and reasons — never the name, never a statement. This
|
|
133
|
+
is the same rule `encode/` follows when it logs a dropped alias by index. The
|
|
134
|
+
event ring is in memory; durability is the adapter's through `onEvent`
|
|
135
|
+
(INTERFACE-GAPS §1).
|
|
136
|
+
|
|
137
|
+
## 12. Where status lives (contract open question 3)
|
|
138
|
+
|
|
139
|
+
**A timestamped `current-state` row on the entity** — the first of the two
|
|
140
|
+
options, not "an ordinary memory with an event date". Two reasons: a wake
|
|
141
|
+
briefing can render it (it is addressable by entity), and the placement rule
|
|
142
|
+
needs a *thing to refuse*, which an ordinary memory would not give it. The second
|
|
143
|
+
option remains more brain-faithful and cheaper to clean up; if current-state rows
|
|
144
|
+
turn out never to be re-read, that is the evidence to switch on.
|
|
145
|
+
|
|
146
|
+
**And what happens when a current-state row is CONTRADICTED** (owner ruling
|
|
147
|
+
2026-09-04, `replaceCurrentState`, SEAMS item O): it is replaced immediately,
|
|
148
|
+
with lineage, and no pressure is accumulated. Two things this build decided that
|
|
149
|
+
the ruling did not say:
|
|
150
|
+
|
|
151
|
+
- **The successor's `statedOn` is the replacing day, not the target's.** A
|
|
152
|
+
replaced status is a status as of the day that replaced it; carrying the old
|
|
153
|
+
date forward would make a fresh correction read as stale, which is the exact
|
|
154
|
+
failure the timestamp exists to prevent.
|
|
155
|
+
- **The versions row says `replaced-by-declaration`, not
|
|
156
|
+
`revised-by-pressure`.** They are different crossings and a reader of the
|
|
157
|
+
lineage must be able to tell a climbed bar from a fact that simply changed
|
|
158
|
+
(scar §2.4). Both strings live in `tunables.ts`, one spelling each.
|
|
159
|
+
|
|
160
|
+
The refusal itself — `status-on-identity-refused` — is the ~72 KB lesson
|
|
161
|
+
mechanized. It refuses on the *identity schema*, not on "status-shaped text",
|
|
162
|
+
because a text classifier here would be a second, softer gate with no admission
|
|
163
|
+
test (scar §2.16).
|
|
164
|
+
|
|
165
|
+
## 13. A removed schema row is skipped, not read (2026-09-18)
|
|
166
|
+
|
|
167
|
+
Found by the adversarial review of the F3 refactor, pre-existing since removal
|
|
168
|
+
shipped, and proved end to end through the real command rather than the seam.
|
|
169
|
+
|
|
170
|
+
`MEMORY_BEARING` permits a `schema` target, so an entity or a belief is a legal
|
|
171
|
+
`counterparts remove`. The chase KEEPS the row and blanks `prose_path`
|
|
172
|
+
(`store/owner-op-seam.ts`); `store.list({type: "schema"})` still returns it; and
|
|
173
|
+
`load` had no guard, so `readProseFile("")` threw `PROSE_FILE_MISSING` out of
|
|
174
|
+
`Schemas.open` and therefore out of `Counterpart.open`.
|
|
175
|
+
|
|
176
|
+
**What that looked like is the reason this could not wait for the floor work.**
|
|
177
|
+
Not a crash. The hook entry point catches everything out of `main()`, writes one
|
|
178
|
+
line to stderr and exits 0, so every session after the removal had no wake, no
|
|
179
|
+
recall and no capture — silently. The CLI and the MCP server fail loudly; the
|
|
180
|
+
hook is the path the owner actually lives in. One `remove` on a person would have
|
|
181
|
+
switched his memory off until someone read the stderr.
|
|
182
|
+
|
|
183
|
+
The second face of the same root cause: in the dark-only state (marked, chase not
|
|
184
|
+
yet run) `slices()` threw `REMOVED`, through `element` → `physicsOf` →
|
|
185
|
+
`requireRow`. `slices()` is a model path.
|
|
186
|
+
|
|
187
|
+
**The fix is to skip, and the CHASED half is one predicate — `removed()` — that
|
|
188
|
+
`load` and `entity` share.** It is free: `prose_path === ""`, and the row is
|
|
189
|
+
already in hand. The DARK half needs the deny-list, and who asks it differs by
|
|
190
|
+
what each caller is already paying for. `load` fetches `deniedIds()` ONCE for the
|
|
191
|
+
whole walk, because it runs at every open over every schema row and a removal
|
|
192
|
+
must cost the index one query, not one per row. `entity` asks per call — it has
|
|
193
|
+
no other store read to borrow from, which costs `entities()` about 2 ms at 120
|
|
194
|
+
entities, measured and accepted rather than paid for with a private view-builder.
|
|
195
|
+
`element` borrows the refusal from the `physicsOf` call it has to make anyway,
|
|
196
|
+
which is why `slices()` did not get slower (66.7 ms → 64.5 ms over 120 entities ×
|
|
197
|
+
6 beliefs; an earlier draft that asked `deniedIds()` per element measured 76.5 ms
|
|
198
|
+
and was rewritten). Either way the question is asked LIVE, not trusted from
|
|
199
|
+
`load`: an in-process `Schemas` can be older than a removal the owner has since
|
|
200
|
+
run in another process.
|
|
201
|
+
|
|
202
|
+
Skipping — rather than rendering a placeholder — is the honest answer:
|
|
203
|
+
`Store.read` still refuses the id BY NAME, so "removed" and "never existed" stay
|
|
204
|
+
distinguishable to anyone who asks for it directly.
|
|
205
|
+
|
|
206
|
+
It also turns a piece of luck into a check. `element` refused a denied id before
|
|
207
|
+
this only because `physicsOf` happened to be evaluated *after* `statement` in the
|
|
208
|
+
same object literal: reordering two lines would have lost the gate, and the
|
|
209
|
+
removed prose was read off disk into memory before the throw discarded it. It is
|
|
210
|
+
now a statement above the read, with its `catch` narrowed to `REMOVED` /
|
|
211
|
+
`ID_UNKNOWN` (`isAbsence`) so a broken database cannot present as a quietly empty
|
|
212
|
+
index.
|
|
213
|
+
|
|
214
|
+
**The two skips are counted apart.** A row the deny-list names was removed by the
|
|
215
|
+
owner, and its absence is the point. A row with a blanked `prose_path` and NO
|
|
216
|
+
removal record is something else — a half-written migration, a disk event — and
|
|
217
|
+
skipping that one buys uptime with data disappearing without a word. Both are
|
|
218
|
+
still skipped, because a session that cannot start helps nobody, but `load` keeps
|
|
219
|
+
the counts apart and `loadSkips()` returns them (a count for the removed, the ids
|
|
220
|
+
for the unaccounted, which is what a `doctor` or `verify` line would want to
|
|
221
|
+
print). Nothing prints them yet; that is the hook for the follow-up, and it was
|
|
222
|
+
left out of this change because those files are being edited elsewhere.
|
|
223
|
+
|
|
224
|
+
**Removal does not cascade, and the consequence is VISIBLE — the first draft of
|
|
225
|
+
this section was wrong about that.** Beliefs hanging off a removed entity keep
|
|
226
|
+
their rows: orphaned, not destroyed. They vanish from every path that starts at
|
|
227
|
+
an entity — slices, the alias index, preselection, the rendered schema context,
|
|
228
|
+
and so recall — and they remain ordinary rows to every path that does not.
|
|
229
|
+
|
|
230
|
+
The proved counter-example is the owner's own dashboard. `counterparts dashboard
|
|
231
|
+
identity` enumerates `store.list({ band: "identity", archived: false })` and
|
|
232
|
+
`store.list({ archived: false })` (`self/identity.ts#enumerate`) and reads the
|
|
233
|
+
prose itself (`adapters/dashboard/identity.ts`). It never asks an entity anything.
|
|
234
|
+
So after removing a person, a promoted belief ABOUT her still prints there in
|
|
235
|
+
full, under "Identity band", while `browse --id <her entity>` correctly says
|
|
236
|
+
`[removed by the owner]`. The protected list is the same loop and the same story.
|
|
237
|
+
`Schemas.element(id)` and `Store.read(id)` answer for the orphans too, by design —
|
|
238
|
+
they were not removed.
|
|
239
|
+
|
|
240
|
+
This is not a regression: before the fix the store could not be opened at all
|
|
241
|
+
after such a removal, so nothing rendered because nothing ran. It is what a
|
|
242
|
+
working session then shows, and "I removed her and her salary is still on the
|
|
243
|
+
screen" is the worst possible way for the owner to find out that removal does not
|
|
244
|
+
cascade. **So the cascade question goes to him, in words, rather than being
|
|
245
|
+
discovered.** Implementing it — chasing an entity's elements, or teaching
|
|
246
|
+
`enumerate` to skip elements whose entity is denied — is a behaviour change and
|
|
247
|
+
`cli/removal.ts`'s to make; naming the orphaned state is this file's.
|
|
248
|
+
|
|
249
|
+
**What this does NOT reach, because `Schemas` builds its index at open and no
|
|
250
|
+
process tells another one anything.** In a long-lived process — the MCP server —
|
|
251
|
+
a removal run from the CLI is invisible to `this.meta` and `this.index` until the
|
|
252
|
+
next start. So in that window `liveElementIds`'s `attached` count still counts a
|
|
253
|
+
dark-marked belief (a number, off by one; it renders nothing), and a dark-marked
|
|
254
|
+
ENTITY is still in the alias index, so `aliasIndex().lookup("ada")` returns it.
|
|
255
|
+
The stale alias cannot leak text: its only consumer is `retrieval.ts` →
|
|
256
|
+
`Turn.aliases` → recall, and `recall/activate.ts` filters denied ids before
|
|
257
|
+
candidates are formed, so such an entity can be MATCHED and never SURFACED.
|
|
258
|
+
Neither is new and neither is removal's fault: it is the same staleness the
|
|
259
|
+
server already has for every write another process makes. Worth closing when
|
|
260
|
+
something needs cross-process invalidation, not before.
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The alias index — this module's, and the one `recall/` borrows (SEAMS §6).
|
|
3
|
+
*
|
|
4
|
+
* ONE WHOLE-WORD RULE. `occursAsWholeWord` is imported from `encode/words.ts`
|
|
5
|
+
* and there is no second definition here, deliberately and by SEAMS §7 /
|
|
6
|
+
* behavioral-spec §8 G3: birth must test a proposed name by EXACTLY the rule
|
|
7
|
+
* preselection uses, or a name that "was in the span" for one is not for the
|
|
8
|
+
* other. The subtlety that matters is that hyphens and apostrophes are word
|
|
9
|
+
* characters — which is why `self` does not match `self-contained`, v1's
|
|
10
|
+
* measured 21% false-positive rate on the self schema. A copy of that rule here
|
|
11
|
+
* would drift from it in exactly one release.
|
|
12
|
+
*
|
|
13
|
+
* Aliases are first-class and change only through explicit alias operations,
|
|
14
|
+
* never as a side effect of editing prose (§4.2 G4) — which is why this index
|
|
15
|
+
* has `register` / `unregister` and no "sync from body text" path.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { occursAsWholeWord } from "../encode/words.js";
|
|
19
|
+
|
|
20
|
+
export interface AliasHit {
|
|
21
|
+
id: string;
|
|
22
|
+
/** The registered term that matched, verbatim as registered. */
|
|
23
|
+
term: string;
|
|
24
|
+
source: "name" | "alias";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface Registered {
|
|
28
|
+
name: string;
|
|
29
|
+
aliases: string[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Handles fold to lower case for LOOKUP because the shared whole-word matcher is
|
|
34
|
+
* case-insensitive without an option — an option is how two callers drift apart
|
|
35
|
+
* (words.ts). Keying any other way would make `lookup` and `matchesIn` disagree.
|
|
36
|
+
*/
|
|
37
|
+
export function handleKey(term: string): string {
|
|
38
|
+
return term.trim().toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class AliasIndex {
|
|
42
|
+
private readonly byKey = new Map<string, Set<string>>();
|
|
43
|
+
private readonly byId = new Map<string, Registered>();
|
|
44
|
+
|
|
45
|
+
/** Idempotent: re-registering an id replaces its terms wholesale. */
|
|
46
|
+
register(id: string, name: string, aliases: readonly string[] = []): void {
|
|
47
|
+
this.unregister(id);
|
|
48
|
+
const terms: Registered = { name, aliases: [...aliases] };
|
|
49
|
+
this.byId.set(id, terms);
|
|
50
|
+
for (const term of [name, ...terms.aliases]) {
|
|
51
|
+
const key = handleKey(term);
|
|
52
|
+
if (key.length === 0) continue;
|
|
53
|
+
const set = this.byKey.get(key) ?? new Set<string>();
|
|
54
|
+
set.add(id);
|
|
55
|
+
this.byKey.set(key, set);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
unregister(id: string): void {
|
|
60
|
+
const prior = this.byId.get(id);
|
|
61
|
+
if (prior === undefined) return;
|
|
62
|
+
for (const term of [prior.name, ...prior.aliases]) {
|
|
63
|
+
const key = handleKey(term);
|
|
64
|
+
const set = this.byKey.get(key);
|
|
65
|
+
if (set === undefined) continue;
|
|
66
|
+
set.delete(id);
|
|
67
|
+
if (set.size === 0) this.byKey.delete(key);
|
|
68
|
+
}
|
|
69
|
+
this.byId.delete(id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
has(id: string): boolean {
|
|
73
|
+
return this.byId.has(id);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
termsFor(id: string): { name: string; aliases: string[] } | undefined {
|
|
77
|
+
const r = this.byId.get(id);
|
|
78
|
+
return r === undefined ? undefined : { name: r.name, aliases: [...r.aliases] };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Exact handle resolution. Two ids means the handle is ambiguous (§9 G5). */
|
|
82
|
+
lookup(term: string): string[] {
|
|
83
|
+
return [...(this.byKey.get(handleKey(term)) ?? [])].sort();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Handles that resolve to more than one entity — the half recall enforces. */
|
|
87
|
+
ambiguous(): string[] {
|
|
88
|
+
return [...this.byKey.entries()]
|
|
89
|
+
.filter(([, ids]) => ids.size > 1)
|
|
90
|
+
.map(([key]) => key)
|
|
91
|
+
.sort();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The map `recall/`'s `Turn.aliases` wants, so the caller stops supplying it
|
|
96
|
+
* from thin air (recall/INTERFACE-GAPS §2). Handle -> ids, both halves live.
|
|
97
|
+
*/
|
|
98
|
+
map(): ReadonlyMap<string, readonly string[]> {
|
|
99
|
+
const out = new Map<string, readonly string[]>();
|
|
100
|
+
for (const [key, ids] of this.byKey) out.set(key, [...ids].sort());
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Every registered term occurring in `text` AS A WHOLE WORD, by the one shared
|
|
106
|
+
* definition. This is what birth tests a name with and what preselection's
|
|
107
|
+
* lexical channel tests a slice with — the same function, called twice.
|
|
108
|
+
*/
|
|
109
|
+
matchesIn(text: string): AliasHit[] {
|
|
110
|
+
const hits: AliasHit[] = [];
|
|
111
|
+
for (const [id, reg] of this.byId) {
|
|
112
|
+
if (occursAsWholeWord(text, reg.name)) hits.push({ id, term: reg.name, source: "name" });
|
|
113
|
+
for (const alias of reg.aliases) {
|
|
114
|
+
if (occursAsWholeWord(text, alias)) hits.push({ id, term: alias, source: "alias" });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return hits.sort((a, b) => a.id.localeCompare(b.id) || a.term.localeCompare(b.term));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
size(): number {
|
|
121
|
+
return this.byId.size;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
// Collision — exact, and near
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
|
|
129
|
+
/** Tokens for the near-collision test. Hyphens and apostrophes stay INSIDE a
|
|
130
|
+
* token, for the same reason they are word characters in words.ts. */
|
|
131
|
+
export function nameTokens(name: string): string[] {
|
|
132
|
+
return handleKey(name)
|
|
133
|
+
.replace(/[^\p{L}\p{N}'’-]+/gu, " ")
|
|
134
|
+
.trim()
|
|
135
|
+
.split(/\s+/)
|
|
136
|
+
.filter((t) => t.length > 0);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export type CollisionKind = "none" | "exact" | "near";
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* "Mike" against "Mike Chen" is a NEAR collision: one name's tokens are wholly
|
|
143
|
+
* contained in the other's. The engine has no evidence that they are the same
|
|
144
|
+
* thing, and silently deciding either way — a second schema, or a wrong merge —
|
|
145
|
+
* is worse than declining and saying so (§14.3). CAL: see tunables.
|
|
146
|
+
*/
|
|
147
|
+
export function collision(a: string, b: string): CollisionKind {
|
|
148
|
+
const ka = handleKey(a);
|
|
149
|
+
const kb = handleKey(b);
|
|
150
|
+
if (ka.length === 0 || kb.length === 0) return "none";
|
|
151
|
+
if (ka === kb) return "exact";
|
|
152
|
+
const ta = nameTokens(a);
|
|
153
|
+
const tb = nameTokens(b);
|
|
154
|
+
if (ta.length === 0 || tb.length === 0) return "none";
|
|
155
|
+
const sa = new Set(ta);
|
|
156
|
+
const sb = new Set(tb);
|
|
157
|
+
const aInB = ta.every((t) => sb.has(t));
|
|
158
|
+
const bInA = tb.every((t) => sa.has(t));
|
|
159
|
+
return aInB || bInA ? "near" : "none";
|
|
160
|
+
}
|