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.
Files changed (195) hide show
  1. package/CONSTITUTION.md +68 -0
  2. package/LICENSE +21 -0
  3. package/README.md +348 -0
  4. package/docs/ELI5.md +84 -0
  5. package/docs/QUICKSTART.md +1258 -0
  6. package/docs/module-map.md +81 -0
  7. package/package.json +63 -0
  8. package/src/adapters/claude-code/CONTRACT.md +606 -0
  9. package/src/adapters/claude-code/INTERFACE-GAPS.md +655 -0
  10. package/src/adapters/claude-code/NOTES.md +1055 -0
  11. package/src/adapters/claude-code/bin/hook.ts +820 -0
  12. package/src/adapters/claude-code/bin/runner.ts +522 -0
  13. package/src/adapters/claude-code/config.ts +778 -0
  14. package/src/adapters/claude-code/credentials.ts +199 -0
  15. package/src/adapters/claude-code/doctor.ts +2557 -0
  16. package/src/adapters/claude-code/embed-client.ts +617 -0
  17. package/src/adapters/claude-code/hooks.ts +2240 -0
  18. package/src/adapters/claude-code/index.ts +224 -0
  19. package/src/adapters/claude-code/interpret-client.ts +395 -0
  20. package/src/adapters/claude-code/page-writer.ts +462 -0
  21. package/src/adapters/claude-code/primacy.ts +162 -0
  22. package/src/adapters/claude-code/spawn.ts +241 -0
  23. package/src/adapters/claude-code/standdown.ts +536 -0
  24. package/src/adapters/claude-code/transcript.ts +661 -0
  25. package/src/adapters/claude-code/vectors.ts +415 -0
  26. package/src/adapters/cli/CONTRACT.md +255 -0
  27. package/src/adapters/cli/INTERFACE-GAPS.md +350 -0
  28. package/src/adapters/cli/NOTES.md +936 -0
  29. package/src/adapters/cli/bin/counterparts.ts +74 -0
  30. package/src/adapters/cli/commands.ts +6095 -0
  31. package/src/adapters/cli/export.ts +766 -0
  32. package/src/adapters/cli/index.ts +103 -0
  33. package/src/adapters/cli/install.ts +670 -0
  34. package/src/adapters/cli/removal.ts +1186 -0
  35. package/src/adapters/cli/repair-dates.ts +659 -0
  36. package/src/adapters/cli/self-page.ts +229 -0
  37. package/src/adapters/cli/snapshot.ts +279 -0
  38. package/src/adapters/cli/start-fresh.ts +1228 -0
  39. package/src/adapters/config-path.ts +342 -0
  40. package/src/adapters/dashboard/CONTRACT.md +67 -0
  41. package/src/adapters/dashboard/INTERFACE-GAPS.md +139 -0
  42. package/src/adapters/dashboard/NOTES.md +474 -0
  43. package/src/adapters/dashboard/activity.ts +121 -0
  44. package/src/adapters/dashboard/ansi.ts +82 -0
  45. package/src/adapters/dashboard/bin/dashboard.ts +479 -0
  46. package/src/adapters/dashboard/browse.ts +298 -0
  47. package/src/adapters/dashboard/identity.ts +182 -0
  48. package/src/adapters/dashboard/index.ts +183 -0
  49. package/src/adapters/dashboard/layout.ts +149 -0
  50. package/src/adapters/dashboard/registries.ts +291 -0
  51. package/src/adapters/dashboard/resolve.ts +206 -0
  52. package/src/adapters/dashboard/source.ts +48 -0
  53. package/src/adapters/dashboard/status.ts +327 -0
  54. package/src/adapters/dashboard/stories.ts +194 -0
  55. package/src/adapters/dashboard/web/app.html +1731 -0
  56. package/src/adapters/dashboard/web/brain.html +463 -0
  57. package/src/adapters/dashboard/web/fired.ts +31 -0
  58. package/src/adapters/dashboard/web/flow.ts +457 -0
  59. package/src/adapters/dashboard/web/narrate.ts +1042 -0
  60. package/src/adapters/dashboard/web/reveal.ts +135 -0
  61. package/src/adapters/dashboard/web/server.ts +287 -0
  62. package/src/adapters/dashboard/web/views.ts +1744 -0
  63. package/src/adapters/expansions.ts +569 -0
  64. package/src/adapters/fired.ts +1580 -0
  65. package/src/adapters/mcp/CONTRACT.md +240 -0
  66. package/src/adapters/mcp/INTERFACE-GAPS.md +271 -0
  67. package/src/adapters/mcp/NOTES.md +205 -0
  68. package/src/adapters/mcp/bin/serve.ts +349 -0
  69. package/src/adapters/mcp/deliberate.ts +619 -0
  70. package/src/adapters/mcp/index.ts +101 -0
  71. package/src/adapters/mcp/protocol.ts +170 -0
  72. package/src/adapters/mcp/server.ts +1740 -0
  73. package/src/adapters/mcp/stdio.ts +56 -0
  74. package/src/adapters/mcp/tools.ts +779 -0
  75. package/src/adapters/scopes.ts +657 -0
  76. package/src/adapters/sessions.ts +402 -0
  77. package/src/adapters/snapshots.ts +1069 -0
  78. package/src/adapters/stance-env.ts +137 -0
  79. package/src/core/associate/CONTRACT.md +118 -0
  80. package/src/core/associate/INTERFACE-GAPS.md +173 -0
  81. package/src/core/associate/NOTES.md +194 -0
  82. package/src/core/associate/buffer.ts +66 -0
  83. package/src/core/associate/edges.ts +188 -0
  84. package/src/core/associate/index.ts +570 -0
  85. package/src/core/associate/pending.ts +383 -0
  86. package/src/core/associate/spread.ts +149 -0
  87. package/src/core/associate/tunables.ts +123 -0
  88. package/src/core/bridge.ts +272 -0
  89. package/src/core/briefing.ts +67 -0
  90. package/src/core/counterpart.ts +3855 -0
  91. package/src/core/encode/CONTRACT.md +151 -0
  92. package/src/core/encode/INTERFACE-GAPS.md +135 -0
  93. package/src/core/encode/NOTES.md +146 -0
  94. package/src/core/encode/aliases.ts +71 -0
  95. package/src/core/encode/battery.ts +284 -0
  96. package/src/core/encode/chunk.ts +265 -0
  97. package/src/core/encode/emotion.ts +119 -0
  98. package/src/core/encode/floor.ts +76 -0
  99. package/src/core/encode/index.ts +100 -0
  100. package/src/core/encode/precision.ts +162 -0
  101. package/src/core/encode/preselect.ts +269 -0
  102. package/src/core/encode/salience.ts +108 -0
  103. package/src/core/encode/secrets.ts +265 -0
  104. package/src/core/encode/tunables.ts +99 -0
  105. package/src/core/encode/types.ts +286 -0
  106. package/src/core/encode/words.ts +75 -0
  107. package/src/core/handoff/CONTRACT.md +199 -0
  108. package/src/core/handoff/INTERFACE-GAPS.md +166 -0
  109. package/src/core/handoff/NOTES.md +206 -0
  110. package/src/core/handoff/index.ts +957 -0
  111. package/src/core/mint.ts +279 -0
  112. package/src/core/observer.ts +28 -0
  113. package/src/core/physics/CONTRACT.md +341 -0
  114. package/src/core/physics/NOTES.md +155 -0
  115. package/src/core/physics/clock.ts +51 -0
  116. package/src/core/physics/index.ts +1054 -0
  117. package/src/core/prospective/CONTRACT.md +137 -0
  118. package/src/core/prospective/INTERFACE-GAPS.md +112 -0
  119. package/src/core/prospective/NOTES.md +160 -0
  120. package/src/core/prospective/derive.ts +188 -0
  121. package/src/core/prospective/index.ts +1030 -0
  122. package/src/core/prospective/tunables.ts +72 -0
  123. package/src/core/prospective/windows.ts +187 -0
  124. package/src/core/recall/CONTRACT.md +326 -0
  125. package/src/core/recall/INTERFACE-GAPS.md +163 -0
  126. package/src/core/recall/NOTES.md +472 -0
  127. package/src/core/recall/activate.ts +521 -0
  128. package/src/core/recall/cues.ts +275 -0
  129. package/src/core/recall/gate.ts +441 -0
  130. package/src/core/recall/index.ts +692 -0
  131. package/src/core/recall/probe.ts +239 -0
  132. package/src/core/recall/reference.ts +201 -0
  133. package/src/core/recall/render.ts +285 -0
  134. package/src/core/recall/session.ts +403 -0
  135. package/src/core/recall/tunables.ts +329 -0
  136. package/src/core/remember/CONTRACT.md +293 -0
  137. package/src/core/remember/INTERFACE-GAPS.md +189 -0
  138. package/src/core/remember/NOTES.md +373 -0
  139. package/src/core/remember/fallback.ts +452 -0
  140. package/src/core/remember/index.ts +103 -0
  141. package/src/core/remember/owner-strike-seam.ts +479 -0
  142. package/src/core/remember/proposals.ts +609 -0
  143. package/src/core/remember/spans.ts +1310 -0
  144. package/src/core/remember/tunables.ts +111 -0
  145. package/src/core/remember/updates.ts +198 -0
  146. package/src/core/retrieval.ts +96 -0
  147. package/src/core/revision.ts +441 -0
  148. package/src/core/schemas/CONTRACT.md +190 -0
  149. package/src/core/schemas/INTERFACE-GAPS.md +172 -0
  150. package/src/core/schemas/NOTES.md +260 -0
  151. package/src/core/schemas/aliases.ts +160 -0
  152. package/src/core/schemas/index.ts +1459 -0
  153. package/src/core/schemas/tunables.ts +44 -0
  154. package/src/core/schemas/types.ts +387 -0
  155. package/src/core/self/CONTRACT.md +459 -0
  156. package/src/core/self/INTERFACE-GAPS.md +284 -0
  157. package/src/core/self/NOTES.md +1057 -0
  158. package/src/core/self/briefing.ts +980 -0
  159. package/src/core/self/episodes.ts +483 -0
  160. package/src/core/self/freeze.ts +103 -0
  161. package/src/core/self/identity.ts +604 -0
  162. package/src/core/self/index.ts +2130 -0
  163. package/src/core/self/journal-file.ts +760 -0
  164. package/src/core/self/page.ts +404 -0
  165. package/src/core/self/tunables.ts +244 -0
  166. package/src/core/self/writer.ts +714 -0
  167. package/src/core/sleep/CONTRACT.md +298 -0
  168. package/src/core/sleep/INTERFACE-GAPS.md +262 -0
  169. package/src/core/sleep/NOTES.md +770 -0
  170. package/src/core/sleep/briefing.ts +86 -0
  171. package/src/core/sleep/consolidate.ts +241 -0
  172. package/src/core/sleep/cycle.ts +598 -0
  173. package/src/core/sleep/decay.ts +237 -0
  174. package/src/core/sleep/dedup.ts +373 -0
  175. package/src/core/sleep/index.ts +112 -0
  176. package/src/core/sleep/log.ts +116 -0
  177. package/src/core/sleep/markers.ts +164 -0
  178. package/src/core/sleep/prune.ts +170 -0
  179. package/src/core/sleep/strength-cache.ts +176 -0
  180. package/src/core/sleep/tunables.ts +135 -0
  181. package/src/core/sleep/types.ts +542 -0
  182. package/src/core/store/CONTRACT.md +446 -0
  183. package/src/core/store/NOTES.md +1068 -0
  184. package/src/core/store/cache.ts +709 -0
  185. package/src/core/store/db.ts +275 -0
  186. package/src/core/store/errors.ts +134 -0
  187. package/src/core/store/index.ts +2450 -0
  188. package/src/core/store/operational.ts +610 -0
  189. package/src/core/store/owner-op-seam.ts +428 -0
  190. package/src/core/store/paths.ts +494 -0
  191. package/src/core/store/prose.ts +165 -0
  192. package/src/core/store/render.ts +87 -0
  193. package/src/core/store/walk-seam.ts +78 -0
  194. package/src/core/types.ts +68 -0
  195. package/tools/install-loop/run.sh +1063 -0
@@ -0,0 +1,66 @@
1
+ /**
2
+ * The delta buffer — **the declared durability exemption** (§10 G6, contract G11).
3
+ *
4
+ * Per-turn co-activation deltas accumulate in fast mutable state and batch-flush
5
+ * at the boundary. A crash therefore loses at most one session's reinforcement,
6
+ * **never a memory**: everything in here is an increment to learned structure that
7
+ * the next co-activation will re-earn, and nothing in here is the only copy of
8
+ * anything. That is the whole content of the exemption, and this file plus
9
+ * `associate/CONTRACT.md` §5 G11 are the ONLY places it may be declared — a second
10
+ * module that buffers non-reconstructible state fails review.
11
+ *
12
+ * On a host whose credit pass and boundary are different processes the pass
13
+ * drains this buffer to a file instead of publishing from it (`pending.ts`), so
14
+ * what dies with that process is the pass in flight rather than the session's
15
+ * reinforcement. The exemption is unchanged: the file holds the same
16
+ * re-earnable increments, and the buffer is still where they accumulate.
17
+ *
18
+ * The drain is where at-most-once is decided, so it is a method with one job:
19
+ *
20
+ * **`drain()` SWAPS the map, it does not clear it in place.** The returned batch
21
+ * is exactly what was read, and a delta that arrives during the flush lands in
22
+ * the fresh map and survives to the next one (§10 G5 — a concurrent arrival is
23
+ * never deleted unflushed).
24
+ *
25
+ * The caller's ordering — drain FIRST, publish SECOND, and never restore on a
26
+ * failed publish — is what makes a crash drop one flush's deltas rather than
27
+ * double-apply them. See index.ts.
28
+ */
29
+ import { pairKey } from "./edges.js";
30
+ import type { PairDelta } from "./edges.js";
31
+
32
+ export class DeltaBuffer {
33
+ private pairs = new Map<string, PairDelta>();
34
+
35
+ /** Accumulate one unordered pair's delta. Ordering of (a, b) never matters. */
36
+ add(a: string, b: string, delta: number): void {
37
+ if (delta <= 0 || a === b) return;
38
+ const key = pairKey(a, b);
39
+ const have = this.pairs.get(key);
40
+ if (have === undefined) {
41
+ const [lo, hi] = a < b ? [a, b] : [b, a];
42
+ this.pairs.set(key, { a: lo, b: hi, delta });
43
+ return;
44
+ }
45
+ this.pairs.set(key, { a: have.a, b: have.b, delta: have.delta + delta });
46
+ }
47
+
48
+ get size(): number {
49
+ return this.pairs.size;
50
+ }
51
+
52
+ /** A read-only look at what is pending. Copies — the caller cannot mutate it. */
53
+ pending(): PairDelta[] {
54
+ return [...this.pairs.values()].map((p) => ({ ...p }));
55
+ }
56
+
57
+ /**
58
+ * Take everything buffered, atomically with respect to this process: the map is
59
+ * replaced, so what comes back is closed and what arrives next is separate.
60
+ */
61
+ drain(): PairDelta[] {
62
+ const taken = this.pairs;
63
+ this.pairs = new Map<string, PairDelta>();
64
+ return [...taken.values()];
65
+ }
66
+ }
@@ -0,0 +1,188 @@
1
+ /**
2
+ * The edge arithmetic — increment, cap, decay by lived day, homeostasis.
3
+ *
4
+ * It lives HERE rather than on `physics/`'s page by the module map's 2026-08-25
5
+ * ruling: edge-level arithmetic keeps `physics/` one page. What it does NOT do is
6
+ * define a second decay CURVE — `decayCurve` is imported from physics and given
7
+ * this module's own stability constant (`S_EDGE`). One curve family, two
8
+ * constants, which is what "a physics-family curve, own constants" means.
9
+ *
10
+ * Everything in this file is pure. Nothing reads a store, nothing writes one, and
11
+ * every state-changing operation returns the NEXT rows for the caller to persist —
12
+ * the same shape physics uses, for the same reason: the flush ordering that makes
13
+ * at-most-once true (index.ts) can only be reasoned about if the arithmetic has no
14
+ * say in when it lands.
15
+ */
16
+ import { decayCurve } from "../physics/index.js";
17
+ import type { AssociateTunables } from "./tunables.js";
18
+
19
+ /** One directed edge as this module sees it. Mirrors `store.EdgeRow`, without
20
+ * binding the arithmetic to the row shape. */
21
+ export interface EdgeState {
22
+ readonly src: string;
23
+ readonly dst: string;
24
+ readonly weight: number;
25
+ readonly lastDay: number;
26
+ }
27
+
28
+ /** An accumulated co-activation delta for one UNORDERED pair. */
29
+ export interface PairDelta {
30
+ readonly a: string;
31
+ readonly b: string;
32
+ readonly delta: number;
33
+ }
34
+
35
+ /** An edge pushed out by the per-node count cap (§10 G3, scar §2.17). */
36
+ export interface Eviction {
37
+ readonly src: string;
38
+ readonly dst: string;
39
+ /** The weight it held when it was evicted — the part that would otherwise be
40
+ * the only lost record. Carried in the flush report and the event. */
41
+ readonly priorWeight: number;
42
+ readonly reason: "count-cap" | "below-floor";
43
+ }
44
+
45
+ export interface FlushPlan {
46
+ /** Absolute next rows — never deltas. A partially-applied plan is impossible
47
+ * because the caller writes them in ONE transaction. */
48
+ readonly rows: readonly EdgeState[];
49
+ readonly evictions: readonly Eviction[];
50
+ /** Nodes whose outgoing total was scaled back by synaptic scaling. */
51
+ readonly renormalized: readonly string[];
52
+ readonly nodesTouched: number;
53
+ }
54
+
55
+ /** Canonical unordered-pair key. `a|b` with the ids sorted, so one pair has one
56
+ * buffer slot however the caller ordered it. */
57
+ export function pairKey(a: string, b: string): string {
58
+ return a < b ? `${a}|${b}` : `${b}|${a}`;
59
+ }
60
+
61
+ /**
62
+ * An edge's weight AT a lived day — the decay, applied lazily and idempotently.
63
+ * A pure function of (stored weight, stored day, asked day), so there is no step
64
+ * to run twice (physics guarantee 5's shape, scar E8).
65
+ */
66
+ export function edgeWeightAt(e: EdgeState, day: number, t: AssociateTunables): number {
67
+ const elapsed = day - e.lastDay;
68
+ if (elapsed <= 0) return e.weight;
69
+ return e.weight * decayCurve(elapsed, t.S_EDGE, t.EDGE_DECAY_SHAPE);
70
+ }
71
+
72
+ /** Dead edges conduct nothing. The floor is where an edge stops being an
73
+ * association and becomes a rounding error. */
74
+ export function conducts(weight: number, t: AssociateTunables): boolean {
75
+ return weight > t.EDGE_FLOOR;
76
+ }
77
+
78
+ /** Increment, capped. The cap is the per-edge half of homeostasis (§10 G2). */
79
+ export function strengthen(current: number, delta: number, t: AssociateTunables): number {
80
+ const next = current + Math.max(0, delta);
81
+ return next > t.EDGE_CAP ? t.EDGE_CAP : next;
82
+ }
83
+
84
+ /**
85
+ * One node's outgoing edges, after the deltas: cap the LIVE edge count (weakest
86
+ * evicted), then bound the total outgoing weight by PROPORTIONAL renormalization.
87
+ *
88
+ * Order matters and is deliberate: evict first, scale second. Scaling first would
89
+ * shrink edges that are about to be evicted anyway and let the survivors keep more
90
+ * weight than the bound allows.
91
+ */
92
+ export function homeostasis(
93
+ src: string,
94
+ weights: ReadonlyMap<string, number>,
95
+ t: AssociateTunables,
96
+ ): { next: Map<string, number>; evictions: Eviction[]; renormalized: boolean } {
97
+ const next = new Map(weights);
98
+ const evictions: Eviction[] = [];
99
+
100
+ // Live edges, strongest first; ties broken by id so the plan is deterministic.
101
+ const live = [...next.entries()]
102
+ .filter(([, w]) => conducts(w, t))
103
+ .sort((x, y) => y[1] - x[1] || (x[0] < y[0] ? -1 : 1));
104
+
105
+ for (const [dst, w] of live.slice(t.MAX_EDGES_PER_NODE)) {
106
+ // "Removal" here is a zeroed weight, not a deleted row: this module has no
107
+ // delete and the store exports none (no-silent-destruction). The eviction is
108
+ // reported and evented; the durable eviction ARCHIVE is a store gap
109
+ // (INTERFACE-GAPS.md §2).
110
+ next.set(dst, 0);
111
+ evictions.push({ src, dst, priorWeight: w, reason: "count-cap" });
112
+ }
113
+
114
+ const kept = live.slice(0, t.MAX_EDGES_PER_NODE);
115
+ const total = kept.reduce((sum, [, w]) => sum + w, 0);
116
+ let renormalized = false;
117
+ if (total > t.MAX_OUT_WEIGHT && total > 0) {
118
+ const factor = t.MAX_OUT_WEIGHT / total;
119
+ for (const [dst, w] of kept) next.set(dst, w * factor);
120
+ renormalized = true;
121
+ }
122
+ return { next, evictions, renormalized };
123
+ }
124
+
125
+ /**
126
+ * The whole flush, as arithmetic: current rows + buffered deltas + a lived day
127
+ * → the absolute next rows.
128
+ *
129
+ * Two properties the caller depends on:
130
+ *
131
+ * 1. **Symmetric by construction.** One unordered pair mints two directed rows,
132
+ * because the store's edge table is keyed (src, dst) and a traversal reads
133
+ * `edgesFrom` only. Renormalization can later make the two directions differ
134
+ * in magnitude — a hub's edge to a quiet node is scaled while the quiet node's
135
+ * edge back is not — and that asymmetry is real, not a bug (NOTES.md §5).
136
+ * 2. **Decay is REALIZED, not merely observed.** Every row a touched node writes
137
+ * carries its decayed-to-`day` weight and `lastDay = day`. Exact for the
138
+ * exponential family, which is why the shape is pinned (NOTES.md §4).
139
+ */
140
+ export function planFlush(
141
+ deltas: readonly PairDelta[],
142
+ day: number,
143
+ edgesFrom: (id: string) => readonly EdgeState[],
144
+ t: AssociateTunables,
145
+ ): FlushPlan {
146
+ /** node -> (dst -> weight at `day`, deltas applied). */
147
+ const nodes = new Map<string, Map<string, number>>();
148
+ /** node -> (dst -> the row as stored), so unchanged rows are not rewritten. */
149
+ const stored = new Map<string, Map<string, EdgeState>>();
150
+
151
+ const load = (id: string): Map<string, number> => {
152
+ const have = nodes.get(id);
153
+ if (have !== undefined) return have;
154
+ const decayed = new Map<string, number>();
155
+ const raw = new Map<string, EdgeState>();
156
+ for (const e of edgesFrom(id)) {
157
+ decayed.set(e.dst, edgeWeightAt(e, day, t));
158
+ raw.set(e.dst, e);
159
+ }
160
+ nodes.set(id, decayed);
161
+ stored.set(id, raw);
162
+ return decayed;
163
+ };
164
+
165
+ for (const d of deltas) {
166
+ if (d.a === d.b) continue;
167
+ const forward = load(d.a);
168
+ const back = load(d.b);
169
+ forward.set(d.b, strengthen(forward.get(d.b) ?? 0, d.delta, t));
170
+ back.set(d.a, strengthen(back.get(d.a) ?? 0, d.delta, t));
171
+ }
172
+
173
+ const rows: EdgeState[] = [];
174
+ const evictions: Eviction[] = [];
175
+ const renormalized: string[] = [];
176
+ for (const [src, weights] of [...nodes].sort((x, y) => (x[0] < y[0] ? -1 : 1))) {
177
+ const settled = homeostasis(src, weights, t);
178
+ evictions.push(...settled.evictions);
179
+ if (settled.renormalized) renormalized.push(src);
180
+ const raw = stored.get(src) ?? new Map<string, EdgeState>();
181
+ for (const [dst, weight] of [...settled.next].sort((x, y) => (x[0] < y[0] ? -1 : 1))) {
182
+ const before = raw.get(dst);
183
+ if (before !== undefined && before.weight === weight && before.lastDay === day) continue;
184
+ rows.push({ src, dst, weight, lastDay: day });
185
+ }
186
+ }
187
+ return { rows, evictions, renormalized, nodesTouched: nodes.size };
188
+ }