counterparts 0.3.0 → 0.3.2

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 (207) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/docs/module-map.md +2 -0
  3. package/package.json +2 -1
  4. package/src/adapters/claude-code/CONTRACT.md +2 -1
  5. package/src/adapters/claude-code/NOTES.md +90 -2
  6. package/src/adapters/claude-code/bin/hook.ts +17 -2
  7. package/src/adapters/claude-code/bin/runner.ts +4 -1
  8. package/src/adapters/claude-code/config.ts +23 -0
  9. package/src/adapters/claude-code/doctor.ts +148 -26
  10. package/src/adapters/claude-code/hooks.ts +37 -12
  11. package/src/adapters/claude-code/index.ts +2 -0
  12. package/src/adapters/claude-code/standdown.ts +12 -1
  13. package/src/adapters/claude-code/transcript.ts +79 -6
  14. package/src/adapters/cli/INTERFACE-GAPS.md +4 -0
  15. package/src/adapters/cli/commands.ts +209 -61
  16. package/src/adapters/cli/export.ts +27 -3
  17. package/src/adapters/cli/help.ts +22 -8
  18. package/src/adapters/cli/install.ts +3 -5
  19. package/src/adapters/cli/mechanisms.ts +295 -0
  20. package/src/adapters/cli/repair-dates.ts +3 -1
  21. package/src/adapters/cli/snapshot.ts +4 -27
  22. package/src/adapters/dashboard/CONTRACT.md +13 -9
  23. package/src/adapters/dashboard/INTERFACE-GAPS.md +4 -0
  24. package/src/adapters/dashboard/NOTES.md +3 -2
  25. package/src/adapters/dashboard/activity.ts +2 -1
  26. package/src/adapters/dashboard/bin/dashboard.ts +18 -3
  27. package/src/adapters/dashboard/upgrade.ts +23 -0
  28. package/src/adapters/dashboard/web/README.md +153 -0
  29. package/src/adapters/dashboard/web/actions.ts +631 -0
  30. package/src/adapters/dashboard/web/app.html +40 -1703
  31. package/src/adapters/dashboard/web/app.js +55 -0
  32. package/src/adapters/dashboard/web/mechanisms/association/index.js +15 -0
  33. package/src/adapters/dashboard/web/mechanisms/association/panel.js +17 -0
  34. package/src/adapters/dashboard/web/mechanisms/consolidation/index.js +15 -0
  35. package/src/adapters/dashboard/web/mechanisms/consolidation/panel.js +23 -0
  36. package/src/adapters/dashboard/web/mechanisms/decay/index.js +15 -0
  37. package/src/adapters/dashboard/web/mechanisms/decay/panel.js +45 -0
  38. package/src/adapters/dashboard/web/mechanisms/emotional/index.js +15 -0
  39. package/src/adapters/dashboard/web/mechanisms/episodic-semantic/index.js +15 -0
  40. package/src/adapters/dashboard/web/mechanisms/index.js +60 -0
  41. package/src/adapters/dashboard/web/mechanisms/interference/index.js +15 -0
  42. package/src/adapters/dashboard/web/mechanisms/picture.js +28 -0
  43. package/src/adapters/dashboard/web/mechanisms/prospective/index.js +15 -0
  44. package/src/adapters/dashboard/web/mechanisms/reconsolidation/index.js +15 -0
  45. package/src/adapters/dashboard/web/mechanisms/reconsolidation/panel.js +15 -0
  46. package/src/adapters/dashboard/web/mechanisms/regions.js +30 -0
  47. package/src/adapters/dashboard/web/mechanisms/retrieval/index.js +15 -0
  48. package/src/adapters/dashboard/web/mechanisms/retrieval/panel.js +20 -0
  49. package/src/adapters/dashboard/web/mechanisms/salience/index.js +15 -0
  50. package/src/adapters/dashboard/web/mechanisms/salience/panel.js +14 -0
  51. package/src/adapters/dashboard/web/mechanisms/schema/index.js +15 -0
  52. package/src/adapters/dashboard/web/narrate.ts +10 -10
  53. package/src/adapters/dashboard/web/pages/flow/flow.css +38 -0
  54. package/src/adapters/dashboard/web/pages/flow/index.js +117 -0
  55. package/src/adapters/dashboard/web/pages/flow/sections/blind.js +12 -0
  56. package/src/adapters/dashboard/web/pages/flow/sections/detail.js +45 -0
  57. package/src/adapters/dashboard/web/pages/flow/sections/diagram.js +291 -0
  58. package/src/adapters/dashboard/web/pages/flow/sections/feed.js +11 -0
  59. package/src/adapters/dashboard/web/pages/flow/sections/fired.js +41 -0
  60. package/src/adapters/dashboard/web/pages/flow/sections/heatmap.js +25 -0
  61. package/src/adapters/dashboard/web/pages/flow/sections/records.js +24 -0
  62. package/src/adapters/dashboard/web/pages/flow/sections/symmetry.js +17 -0
  63. package/src/adapters/dashboard/web/pages/flow/state.js +3 -0
  64. package/src/adapters/dashboard/web/pages/health/health.css +93 -0
  65. package/src/adapters/dashboard/web/pages/health/index.js +41 -0
  66. package/src/adapters/dashboard/web/pages/health/sections/archive.js +84 -0
  67. package/src/adapters/dashboard/web/pages/health/sections/checks.js +172 -0
  68. package/src/adapters/dashboard/web/pages/health/sections/cycle.js +37 -0
  69. package/src/adapters/dashboard/web/pages/health/sections/verify.js +54 -0
  70. package/src/adapters/dashboard/web/pages/home/brain.js +375 -0
  71. package/src/adapters/dashboard/web/pages/home/home.css +134 -0
  72. package/src/adapters/dashboard/web/pages/home/index.js +84 -0
  73. package/src/adapters/dashboard/web/pages/home/sections/explorer.js +206 -0
  74. package/src/adapters/dashboard/web/pages/home/sections/hero.js +22 -0
  75. package/src/adapters/dashboard/web/pages/home/sections/live-activity.js +25 -0
  76. package/src/adapters/dashboard/web/pages/home/sections/recent-chapters.js +13 -0
  77. package/src/adapters/dashboard/web/pages/home/sections/tiles.js +52 -0
  78. package/src/adapters/dashboard/web/pages/memories/index.js +75 -0
  79. package/src/adapters/dashboard/web/pages/memories/memories.css +143 -0
  80. package/src/adapters/dashboard/web/pages/memories/sections/constellation.js +178 -0
  81. package/src/adapters/dashboard/web/pages/memories/sections/kinds.js +42 -0
  82. package/src/adapters/dashboard/web/pages/memories/sections/list.js +114 -0
  83. package/src/adapters/dashboard/web/pages/memories/sections/search.js +102 -0
  84. package/src/adapters/dashboard/web/pages/memories/sections/tools.js +155 -0
  85. package/src/adapters/dashboard/web/pages/memories/state.js +22 -0
  86. package/src/adapters/dashboard/web/pages/self/diff.js +79 -0
  87. package/src/adapters/dashboard/web/pages/self/index.js +43 -0
  88. package/src/adapters/dashboard/web/pages/self/markdown.js +61 -0
  89. package/src/adapters/dashboard/web/pages/self/sections/journal.js +32 -0
  90. package/src/adapters/dashboard/web/pages/self/sections/page.js +149 -0
  91. package/src/adapters/dashboard/web/pages/self/sections/settling.js +150 -0
  92. package/src/adapters/dashboard/web/pages/self/sections/stories.js +47 -0
  93. package/src/adapters/dashboard/web/pages/self/sections/wake.js +74 -0
  94. package/src/adapters/dashboard/web/pages/self/self.css +182 -0
  95. package/src/adapters/dashboard/web/server.ts +305 -28
  96. package/src/adapters/dashboard/web/shared/absence.js +14 -0
  97. package/src/adapters/dashboard/web/shared/actions.js +48 -0
  98. package/src/adapters/dashboard/web/shared/api.js +17 -0
  99. package/src/adapters/dashboard/web/shared/base.css +34 -0
  100. package/src/adapters/dashboard/web/shared/canvas.js +68 -0
  101. package/src/adapters/dashboard/web/shared/colors.js +6 -0
  102. package/src/adapters/dashboard/web/shared/dom.js +4 -0
  103. package/src/adapters/dashboard/web/shared/event-modal.js +25 -0
  104. package/src/adapters/dashboard/web/shared/fonts/OFL-courier-prime.txt +93 -0
  105. package/src/adapters/dashboard/web/shared/fonts/OFL-outfit.txt +93 -0
  106. package/src/adapters/dashboard/web/shared/fonts/courier-prime-latin-400.woff2 +0 -0
  107. package/src/adapters/dashboard/web/shared/fonts/courier-prime-latin-700.woff2 +0 -0
  108. package/src/adapters/dashboard/web/shared/fonts/outfit-latin-wght.woff2 +0 -0
  109. package/src/adapters/dashboard/web/shared/format.js +31 -0
  110. package/src/adapters/dashboard/web/shared/layout.css +32 -0
  111. package/src/adapters/dashboard/web/shared/memory-modal.js +124 -0
  112. package/src/adapters/dashboard/web/shared/modal.css +24 -0
  113. package/src/adapters/dashboard/web/shared/modal.js +22 -0
  114. package/src/adapters/dashboard/web/shared/state.js +12 -0
  115. package/src/adapters/dashboard/web/shared/tip.css +4 -0
  116. package/src/adapters/dashboard/web/shared/tip.js +12 -0
  117. package/src/adapters/dashboard/web/shared/tokens.css +50 -0
  118. package/src/adapters/dashboard/web/shared/type.css +52 -0
  119. package/src/adapters/dashboard/web/shared/utilities.css +5 -0
  120. package/src/adapters/dashboard/web/shared/vendor/MIT-three.txt +21 -0
  121. package/src/adapters/dashboard/web/shared/vendor/three.module.min.js +6 -0
  122. package/src/adapters/dashboard/web/shared/widgets/bar.css +9 -0
  123. package/src/adapters/dashboard/web/shared/widgets/card.css +6 -0
  124. package/src/adapters/dashboard/web/shared/widgets/chapters.js +15 -0
  125. package/src/adapters/dashboard/web/shared/widgets/chart.css +4 -0
  126. package/src/adapters/dashboard/web/shared/widgets/confirm.css +31 -0
  127. package/src/adapters/dashboard/web/shared/widgets/confirm.js +52 -0
  128. package/src/adapters/dashboard/web/shared/widgets/feed.css +13 -0
  129. package/src/adapters/dashboard/web/shared/widgets/feed.js +41 -0
  130. package/src/adapters/dashboard/web/shared/widgets/light.css +4 -0
  131. package/src/adapters/dashboard/web/shared/widgets/light.js +15 -0
  132. package/src/adapters/dashboard/web/shared/widgets/rows.css +7 -0
  133. package/src/adapters/dashboard/web/shared/widgets/table.css +17 -0
  134. package/src/adapters/dashboard/web/shared/widgets/tiles.css +14 -0
  135. package/src/adapters/dashboard/web/shell/pages.js +15 -0
  136. package/src/adapters/dashboard/web/shell/pulse.js +86 -0
  137. package/src/adapters/dashboard/web/shell/tabs.js +49 -0
  138. package/src/adapters/dashboard/web/static.ts +76 -0
  139. package/src/adapters/dashboard/web/views/activity.ts +80 -0
  140. package/src/adapters/dashboard/web/views/archive-words.ts +57 -0
  141. package/src/adapters/dashboard/web/views/flow-view.ts +217 -0
  142. package/src/adapters/dashboard/web/views/health.ts +328 -0
  143. package/src/adapters/dashboard/web/views/mechanism-panel.ts +370 -0
  144. package/src/adapters/dashboard/web/views/mechanisms.ts +302 -0
  145. package/src/adapters/dashboard/web/views/memories.ts +342 -0
  146. package/src/adapters/dashboard/web/views/memory.ts +210 -0
  147. package/src/adapters/dashboard/web/views/meta.ts +64 -0
  148. package/src/adapters/dashboard/web/views/mind.ts +729 -0
  149. package/src/adapters/dashboard/web/views/overview.ts +282 -0
  150. package/src/adapters/dashboard/web/views/pulse.ts +19 -0
  151. package/src/adapters/dashboard/web/views/rows.ts +207 -0
  152. package/src/adapters/dashboard/web/views/search.ts +62 -0
  153. package/src/adapters/dashboard/web/views/shared.ts +161 -0
  154. package/src/adapters/dashboard/web/views.ts +34 -1724
  155. package/src/adapters/fired.ts +138 -31
  156. package/src/adapters/mcp/INTERFACE-GAPS.md +4 -1
  157. package/src/adapters/mcp/NOTES.md +43 -0
  158. package/src/adapters/mcp/bin/serve.ts +9 -1
  159. package/src/adapters/mcp/index.ts +7 -0
  160. package/src/adapters/mcp/server.ts +167 -6
  161. package/src/adapters/mcp/tools.ts +42 -0
  162. package/src/adapters/mcp/write-up.ts +1 -1
  163. package/src/adapters/sessions.ts +18 -1
  164. package/src/adapters/snapshots.ts +71 -39
  165. package/src/core/associate/INTERFACE-GAPS.md +8 -0
  166. package/src/core/counterpart.ts +86 -7
  167. package/src/core/encode/INTERFACE-GAPS.md +12 -0
  168. package/src/core/feelings-wheel.ts +218 -0
  169. package/src/core/handoff/INTERFACE-GAPS.md +2 -0
  170. package/src/core/mint.ts +14 -2
  171. package/src/core/prospective/INTERFACE-GAPS.md +14 -0
  172. package/src/core/prospective/windows.ts +10 -48
  173. package/src/core/recall/INTERFACE-GAPS.md +6 -0
  174. package/src/core/remember/INTERFACE-GAPS.md +10 -0
  175. package/src/core/schemas/INTERFACE-GAPS.md +8 -0
  176. package/src/core/schemas/NOTES.md +118 -1
  177. package/src/core/schemas/index.ts +330 -40
  178. package/src/core/schemas/tunables.ts +23 -0
  179. package/src/core/schemas/types.ts +64 -1
  180. package/src/core/self/CONTRACT.md +3 -0
  181. package/src/core/self/INTERFACE-GAPS.md +10 -0
  182. package/src/core/self/NOTES.md +33 -2
  183. package/src/core/self/calendar.ts +7 -78
  184. package/src/core/self/episodes.ts +52 -12
  185. package/src/core/self/index.ts +40 -12
  186. package/src/core/self/writer.ts +22 -22
  187. package/src/core/sleep/NOTES.md +36 -0
  188. package/src/core/sleep/cycle.ts +38 -3
  189. package/src/core/sleep/fade.ts +76 -0
  190. package/src/core/sleep/index.ts +4 -1
  191. package/src/core/sleep/prune.ts +8 -1
  192. package/src/core/sleep/tunables.ts +5 -0
  193. package/src/core/sleep/types.ts +24 -0
  194. package/src/core/store/CONTRACT.md +36 -7
  195. package/src/core/store/NOTES.md +197 -0
  196. package/src/core/store/errors.ts +22 -0
  197. package/src/core/store/feelings.ts +182 -0
  198. package/src/core/store/index.ts +339 -45
  199. package/src/core/store/operational.ts +240 -22
  200. package/src/core/store/owner-op-seam.ts +13 -3
  201. package/src/core/store/paths.ts +15 -1
  202. package/src/core/store/pre-migration.ts +258 -0
  203. package/src/core/store/prose.ts +8 -1
  204. package/src/core/store/render.ts +3 -0
  205. package/src/core/time.ts +374 -0
  206. package/src/core/types.ts +13 -0
  207. package/src/adapters/dashboard/web/brain.html +0 -463
@@ -0,0 +1,217 @@
1
+ /**
2
+ * `/api/flow` and `/api/node` — the architecture diagram's live state. (Named
3
+ * `flow-view` so it is not confused with `web/flow.ts`, the diagram's static shape.)
4
+ *
5
+ * Split out of `web/views.ts`, which re-exports every public name from here;
6
+ * the four rules in that file's header apply to every line below.
7
+ */
8
+ import { band } from "../../../../core/physics/index.js";
9
+ import { NEVER, NONE } from "../../layout.js";
10
+ import type { DashboardSource } from "../../source.js";
11
+ import { FLOW_EDGES, FLOW_NODES, NO_EVENT_OF_ITS_OWN, UNLOGGED_PATH, eventsOfNode, findNode } from "../flow.js";
12
+ import type { NodeKey } from "../flow.js";
13
+ import { narrate } from "../narrate.js";
14
+ import type { NarratedEvent } from "../narrate.js";
15
+ import { activityView } from "./activity.js";
16
+ import { LOG_CEILING, census, memoriesHeld } from "./shared.js";
17
+
18
+ // ─────────────────────────────────────────────────────────────────────────────
19
+ // flow
20
+ // ─────────────────────────────────────────────────────────────────────────────
21
+
22
+ export interface FlowNodeState {
23
+ readonly key: NodeKey;
24
+ readonly label: string;
25
+ readonly sub: string;
26
+ readonly x: number;
27
+ readonly y: number;
28
+ readonly w: number;
29
+ readonly h: number;
30
+ readonly accent: string;
31
+ /** How many durable events this node has ever recorded, in the kept window. */
32
+ readonly count: number;
33
+ /** The honest state line drawn inside the box. */
34
+ readonly state: string;
35
+ /** True when the log has never carried a single event for this node. */
36
+ readonly silent: boolean;
37
+ }
38
+
39
+ export interface FlowView {
40
+ readonly nodes: FlowNodeState[];
41
+ readonly edges: typeof FLOW_EDGES;
42
+ readonly lastSeq: number;
43
+ readonly feed: NarratedEvent[];
44
+ readonly note: string;
45
+ }
46
+
47
+ export function flowView(src: DashboardSource, feedLimit = 24): FlowView {
48
+ const store = src.store;
49
+ // MEMORIES, not every row: the STORE node says "N memories held" and the
50
+ // overview's tile says the same words, so they had better be the same number.
51
+ const rows = census(src).filter((r) => !r.schema);
52
+ const day = store.livedDay();
53
+ const activity = activityView(src, { limit: feedLimit });
54
+ const counts = new Map<NodeKey, number>();
55
+ for (const v of activity.vocabulary) {
56
+ if (v.node === null) continue;
57
+ counts.set(v.node as NodeKey, (counts.get(v.node as NodeKey) ?? 0) + v.count);
58
+ }
59
+
60
+ const nodes: FlowNodeState[] = FLOW_NODES.map((node) => {
61
+ const count = counts.get(node.key) ?? 0;
62
+ const silent = eventsOfNode(node.key).length === 0 || count === 0;
63
+ return {
64
+ key: node.key,
65
+ label: node.label,
66
+ sub: node.sub,
67
+ x: node.x,
68
+ y: node.y,
69
+ w: node.w,
70
+ h: node.h,
71
+ accent: node.accent,
72
+ count,
73
+ state: nodeState(src, node.key, count, rows.length, day),
74
+ silent,
75
+ };
76
+ });
77
+
78
+ return {
79
+ nodes,
80
+ edges: FLOW_EDGES,
81
+ lastSeq: activity.lastSeq,
82
+ feed: activity.events,
83
+ note: "Only particles mean activity. A still diagram is a still machine — nothing here drifts for decoration.",
84
+ };
85
+ }
86
+
87
+ /** The one line inside a node's box: its own state, or the honest absence. */
88
+ function nodeState(src: DashboardSource, key: NodeKey, count: number, memories: number, day: number): string {
89
+ const store = src.store;
90
+ switch (key) {
91
+ case "store":
92
+ return memories === 0 ? NONE : `${memories} memories held`;
93
+ case "physics":
94
+ return day === 0 ? NEVER : `day ${day} on the clock`;
95
+ case "associate": {
96
+ let edges = 0;
97
+ for (const id of store.list({ archived: false })) edges += store.edgesFrom(id).length;
98
+ return edges === 0 ? NONE : `${edges} directed edges`;
99
+ }
100
+ case "prospective": {
101
+ let armed = 0;
102
+ let fired = 0;
103
+ for (const id of store.list({ archived: false })) {
104
+ for (const p of store.prospectiveFor(id)) {
105
+ if (p.state === "fired") fired += 1;
106
+ else armed += 1;
107
+ }
108
+ }
109
+ return armed + fired === 0 ? NONE : `${armed} waiting · ${fired} fired`;
110
+ }
111
+ case "self": {
112
+ const e = src.self.enumerate(day);
113
+ return e.identity.length === 0 && e.protected.length === 0
114
+ ? NONE
115
+ : `${e.identity.length} in the band · ${e.protected.length} permanent`;
116
+ }
117
+ case "wake": {
118
+ const wake = src.self.wake();
119
+ return wake.ok ? `${wake.bytes} bytes waiting` : NEVER;
120
+ }
121
+ case "schemas": {
122
+ const entities = src.schemas.entities().length;
123
+ return entities === 0 ? NONE : `${entities} entities`;
124
+ }
125
+ case "spans":
126
+ // Turn capture writes no durable event; see NO_EVENT_OF_ITS_OWN. On a
127
+ // store that has never lived a day the honest word is still `(never run)`
128
+ // — "silent by design" would claim a design decision was exercised.
129
+ return day === 0 ? NEVER : "silent by design";
130
+ case "session": {
131
+ const sessions = new Set<string>();
132
+ for (const name of eventsOfNode("session")) {
133
+ for (const row of store.eventLog({ name, limit: LOG_CEILING })) if (row.ref !== null) sessions.add(row.ref);
134
+ }
135
+ // The surfacing log names its session, so a store whose host wrote no
136
+ // boundary rows can still say how many conversations it has seen.
137
+ for (const row of store.eventLog({ name: "recall.decision", limit: LOG_CEILING })) {
138
+ if (row.ref !== null) sessions.add(row.ref);
139
+ }
140
+ return sessions.size === 0 ? (day === 0 ? NEVER : NONE) : `${sessions.size} sessions seen`;
141
+ }
142
+ case "remember":
143
+ // The door's own record is the ask's pacing row, which only a HOST
144
+ // writes. What the door actually did is the count that came through it.
145
+ return memories === 0 ? (day === 0 ? NEVER : NONE) : `${memories} came through`;
146
+ case "encode":
147
+ // NEITHER ABSENCE WORD IS TRUE HERE once anything is in the store: the
148
+ // battery ran on every one of those memories. `(never run)` beside
149
+ // STORE's `143 memories held` was a contradiction on the face of the
150
+ // diagram. Both doors record now (`gate.chunk`, `gate.deposit`), so the
151
+ // ordinary answer is a count — but a store holding memories that PREDATE
152
+ // the gate log, or that were seeded around the door, still has none, and
153
+ // that state gets its own sentence rather than a zero.
154
+ if (count > 0) return `${count} recorded`;
155
+ if (memories > 0) return `${memories} passed · no gate record for them`;
156
+ return day === 0 ? NEVER : NONE;
157
+ default:
158
+ return count === 0 ? NEVER : `${count} recorded`;
159
+ }
160
+ }
161
+
162
+ export interface NodeDetailView {
163
+ readonly found: boolean;
164
+ readonly key: string;
165
+ readonly label: string;
166
+ readonly what: string;
167
+ readonly analog: string;
168
+ readonly breaks: string;
169
+ readonly eventNames: string[];
170
+ readonly noEventOfItsOwn: string | null;
171
+ /** Work that is real and deliberately unrecorded. See `UNLOGGED_PATH`. */
172
+ readonly unloggedPath: string | null;
173
+ readonly recent: NarratedEvent[];
174
+ readonly recentAbsent: string | null;
175
+ readonly state: string;
176
+ }
177
+
178
+ export function nodeDetail(src: DashboardSource, key: string, limit = 8): NodeDetailView {
179
+ const node = findNode(key);
180
+ if (node === undefined) {
181
+ return {
182
+ found: false,
183
+ key,
184
+ label: "",
185
+ what: "",
186
+ analog: "",
187
+ breaks: "",
188
+ eventNames: [],
189
+ noEventOfItsOwn: null,
190
+ unloggedPath: null,
191
+ recent: [],
192
+ recentAbsent: NEVER,
193
+ state: "",
194
+ };
195
+ }
196
+ const store = src.store;
197
+ const names = eventsOfNode(node.key);
198
+ const rows = [];
199
+ for (const name of names) rows.push(...store.eventLog({ name, limit: LOG_CEILING }));
200
+ rows.sort((a, b) => a.seq - b.seq);
201
+ const recent = rows.slice(-limit).reverse().map((row) => narrate(store, row));
202
+ const everLived = store.livedDay() > 0 || store.list().length > 0;
203
+ return {
204
+ found: true,
205
+ key: node.key,
206
+ label: node.label,
207
+ what: node.what,
208
+ analog: node.analog,
209
+ breaks: node.breaks,
210
+ eventNames: names,
211
+ noEventOfItsOwn: NO_EVENT_OF_ITS_OWN[node.key] ?? null,
212
+ unloggedPath: UNLOGGED_PATH[node.key] ?? null,
213
+ recent,
214
+ recentAbsent: recent.length === 0 ? (names.length === 0 ? null : everLived ? NONE : NEVER) : null,
215
+ state: nodeState(src, node.key, rows.length, memoriesHeld(src), store.livedDay()),
216
+ };
217
+ }
@@ -0,0 +1,328 @@
1
+ /**
2
+ * `/api/health`.
3
+ *
4
+ * Split out of `web/views.ts`, which re-exports every public name from here;
5
+ * the four rules in that file's header apply to every line below.
6
+ */
7
+ import { symmetryCheck } from "../../../../core/physics/index.js";
8
+ import { MARKER_UNSET, MERGE_ARCHIVE_REASON, PRUNE_ARCHIVE_REASON, readMarker } from "../../../../core/sleep/index.js";
9
+ import type { Kind } from "../../../../core/types.js";
10
+ import { NEVER, NONE } from "../../layout.js";
11
+ import { CYCLE_PHASES, DURABLE_EVENTS, DURABLE_EVENT_NAMES, KINDS } from "../../registries.js";
12
+ import type { DurableEventName } from "../../registries.js";
13
+ import type { DashboardSource } from "../../source.js";
14
+ import { reveal, revealHere } from "../reveal.js";
15
+
16
+ import { ARCHIVE_PHRASES, REMOVED_BY_OWNER, unmappedArchiveWords } from "./archive-words.js";
17
+ import { LOG_CEILING, eventCountsByName } from "./shared.js";
18
+
19
+ // ─────────────────────────────────────────────────────────────────────────────
20
+ // health
21
+ // ─────────────────────────────────────────────────────────────────────────────
22
+
23
+ export interface HealthView {
24
+ readonly phases: { phase: string; day: number | null; ago: number | null; torn: boolean; absent: string | null }[];
25
+ readonly symmetry: { kind: Kind; up: number; down: number; reason: string; ok: boolean }[];
26
+ /**
27
+ * ONE TABLE, LED BY ENGLISH. There were two — "what a host told me" over the
28
+ * eleven `adapter.*` names, and "everything I can record durably" over all
29
+ * nineteen — so seven identifiers were listed twice on one screen with
30
+ * near-duplicate glosses, and the first thing the health tab showed a person
31
+ * was 23 dotted names against a column of `(never run)` (design review,
32
+ * 2026-09-04, §9 and ranked #4). The gloss each row already carried in its
33
+ * third column now LEADS it and the dotted name is the second line, which is
34
+ * the order a reader needs them in: what this records, then what it is
35
+ * called. `adapter` says whether a host wrote it or the machinery did — the
36
+ * only thing the split table was really saying — and `note` carries the
37
+ * adapter-specific caveat the old table had room for.
38
+ */
39
+ readonly records: {
40
+ name: DurableEventName;
41
+ gloss: string;
42
+ count: number;
43
+ absent: string | null;
44
+ adapter: boolean;
45
+ note: string | null;
46
+ }[];
47
+ readonly heatmap: { days: number[]; names: string[]; cells: { day: number; name: string; count: number }[] };
48
+ readonly removals: { id: string; label: string; stage: string; actor: string; reason: string | null; at: number }[];
49
+ readonly removalsAbsent: string | null;
50
+ readonly blind: readonly { readonly what: string; readonly why: string }[];
51
+ /**
52
+ * THE LAST CYCLE, as one line: the newest lived day any phase finished on,
53
+ * which phases finished that day, and when the newest `sleep.cycle` row was
54
+ * written (ms; null when none is held) so the page can say "today" only when
55
+ * it was.
56
+ */
57
+ readonly cycle: {
58
+ readonly day: number | null;
59
+ readonly at: number | null;
60
+ readonly ran: number;
61
+ readonly total: number;
62
+ readonly phases: { phase: string; gloss: string; state: "ran" | "behind" | "never" | "torn"; day: number | null }[];
63
+ };
64
+ /**
65
+ * WHERE ARCHIVED MEMORIES WENT — every archived row counted by its
66
+ * `archived_reason`, each reason in plain words, with the ids behind it so a
67
+ * segment can list them. "Removed by you" is the removal record's memories
68
+ * (a removed row keeps only a skeleton), with its stage and reason.
69
+ */
70
+ readonly archive: {
71
+ readonly total: number;
72
+ readonly reasons: {
73
+ reason: string;
74
+ phrase: string;
75
+ count: number;
76
+ known: boolean;
77
+ items: { id: string; label: string; note: string | null }[];
78
+ }[];
79
+ };
80
+ }
81
+
82
+ /** Each phase of the cycle, in a few plain words (hover text on its dot). */
83
+ const PHASE_GLOSS: Record<string, string> = {
84
+ clock: "move the lived day on",
85
+ decay: "let unused memories weaken",
86
+ consolidate: "strengthen and promote what was used",
87
+ prune: "let go of what fell to the floor",
88
+ fade: "fade cards nothing mentions any more",
89
+ dedup: "merge duplicates",
90
+ versions: "tidy old versions",
91
+ briefing: "write the next wake briefing",
92
+ log: "sweep old log rows",
93
+ };
94
+
95
+ /** Shown even at zero: the three ways out that are forgetting by design. */
96
+ const ALWAYS_SHOWN = new Set<string>([PRUNE_ARCHIVE_REASON, MERGE_ARCHIVE_REASON, REMOVED_BY_OWNER]);
97
+ /** How many ids one segment carries to the page. */
98
+ const ARCHIVE_ITEMS_CAP = 200;
99
+
100
+ /** The events an ADAPTER writes — the only ones that can tell the owner what a
101
+ * host actually did with what was composed for it. */
102
+ const ADAPTER_EVENTS: readonly DurableEventName[] = [
103
+ "adapter.ask",
104
+ "adapter.boundary",
105
+ "adapter.recall",
106
+ "adapter.wake.injected",
107
+ "adapter.wake.delivered",
108
+ "adapter.primacy.deliver",
109
+ "adapter.primacy.standdown",
110
+ "adapter.embed.backfill",
111
+ "adapter.semantic.lag",
112
+ "adapter.authorship.ask",
113
+ "adapter.episode.ask",
114
+ ];
115
+
116
+ const ADAPTER_NOTE: Partial<Record<DurableEventName, string>> = {
117
+ "adapter.wake.injected": "bytes handed to the host, never the text",
118
+ "adapter.wake.delivered": "whether the briefing was actually seen the next turn",
119
+ "adapter.recall": "how long recall took, when the adapter recorded it",
120
+ "adapter.ask": "asked, paced out, or capped for the day",
121
+ "adapter.authorship.ask": "historical — nothing writes this now",
122
+ "adapter.episode.ask": "historical — nothing writes this now",
123
+ };
124
+
125
+ export function healthView(src: DashboardSource): HealthView {
126
+ const store = src.store;
127
+ const day = store.livedDay();
128
+ const everLived = day > 0 || store.list().length > 0;
129
+
130
+ const phases = CYCLE_PHASES.map((phase) => {
131
+ const marker = readMarker(store, phase);
132
+ if (marker.health === "torn") {
133
+ return { phase, day: null, ago: null, torn: true, absent: "torn marker" };
134
+ }
135
+ if (marker.day === MARKER_UNSET) return { phase, day: null, ago: null, torn: false, absent: NEVER };
136
+ return { phase, day: marker.day, ago: Math.max(0, day - marker.day), torn: false, absent: null };
137
+ });
138
+
139
+ const transitions = store.eventLog({ name: "band.transition", limit: LOG_CEILING });
140
+ const symmetry = KINDS.map((kind) => {
141
+ let up = 0;
142
+ let down = 0;
143
+ for (const row of transitions) {
144
+ if (row.payload === null) continue;
145
+ try {
146
+ const p = JSON.parse(row.payload) as { kind?: string; direction?: string };
147
+ if (p.kind !== kind) continue;
148
+ if (p.direction === "up") up += 1;
149
+ else if (p.direction === "down") down += 1;
150
+ } catch {
151
+ continue;
152
+ }
153
+ }
154
+ const verdict = symmetryCheck(kind, { up, down });
155
+ return { kind, up, down, reason: verdict.reason, ok: verdict.reason === "within-expectation" };
156
+ });
157
+
158
+ // The merged record table. Every durable name appears exactly once — the
159
+ // totality rule is unchanged, and the test still walks `DURABLE_EVENT_NAMES`
160
+ // against it — but what HAS happened is ordered above what never has, so the
161
+ // first screen of this tab is the log's actual contents rather than eleven
162
+ // adapter names nobody has ever run. The order is stated on the page.
163
+ const counts = eventCountsByName(src);
164
+ const records = DURABLE_EVENT_NAMES.map((name) => {
165
+ const count = counts.get(name) ?? 0;
166
+ const adapter = ADAPTER_EVENTS.includes(name);
167
+ return {
168
+ name,
169
+ gloss: DURABLE_EVENTS[name],
170
+ count,
171
+ absent: count === 0 ? NEVER : null,
172
+ adapter,
173
+ note: adapter ? (ADAPTER_NOTE[name] ?? "counts and reasons; never text") : null,
174
+ };
175
+ }).sort((a, b) => (b.count === a.count ? 0 : b.count - a.count));
176
+
177
+ // The heatmap: lived day × event name. Bounded to the last 21 lived days so a
178
+ // long-lived store still fits on a screen without a scrollbar in two axes.
179
+ const span = 21;
180
+ const from = Math.max(0, day - span + 1);
181
+ const days = Array.from({ length: Math.max(1, Math.min(span, day + 1)) }, (_, i) => from + i);
182
+ const cells: { day: number; name: string; count: number }[] = [];
183
+ for (const name of DURABLE_EVENT_NAMES) {
184
+ const rows = store.eventLog({ name, sinceDay: from, limit: LOG_CEILING });
185
+ const per = new Map<number, number>();
186
+ for (const row of rows) per.set(row.day, (per.get(row.day) ?? 0) + 1);
187
+ for (const d of days) cells.push({ day: d, name, count: per.get(d) ?? 0 });
188
+ }
189
+
190
+ const removals = store.removalRecord().map((x) => ({
191
+ id: x.memory_id,
192
+ label: reveal(store, x.memory_id, 72).label,
193
+ stage: x.stage,
194
+ actor: x.actor,
195
+ reason: x.reason,
196
+ at: x.at,
197
+ }));
198
+
199
+ // The last cycle: the newest day any phase finished on is "the last cycle";
200
+ // a phase whose marker is older than that is behind.
201
+ const newest = phases.reduce<number | null>((m, p) => (p.day === null ? m : m === null ? p.day : Math.max(m, p.day)), null);
202
+ const lastCycleRow = store.eventLog({ name: "sleep.cycle", order: "desc", limit: 1 })[0];
203
+ const cyclePhases = phases.map((p) => ({
204
+ phase: p.phase,
205
+ gloss: PHASE_GLOSS[p.phase] ?? p.phase,
206
+ state: p.torn ? ("torn" as const) : p.day === null ? ("never" as const) : p.day === newest ? ("ran" as const) : ("behind" as const),
207
+ day: p.day,
208
+ }));
209
+ const cycle = {
210
+ day: newest,
211
+ at: lastCycleRow === undefined ? null : lastCycleRow.at,
212
+ ran: cyclePhases.filter((p) => p.state === "ran").length,
213
+ total: cyclePhases.length,
214
+ phases: cyclePhases,
215
+ };
216
+
217
+ // Where archived memories went. One pass over the rows, then the removal
218
+ // record for "removed by you" (a removed row keeps only a skeleton, so its
219
+ // words are gone and the record is what says what happened).
220
+ const byReason = new Map<string, string[]>();
221
+ for (const id of store.list()) {
222
+ const row = store.row(id);
223
+ if (row === undefined || row.archived !== 1) continue;
224
+ const reason = row.archived_reason ?? "";
225
+ if (reason === REMOVED_BY_OWNER) continue;
226
+ const ids = byReason.get(reason) ?? [];
227
+ ids.push(id);
228
+ byReason.set(reason, ids);
229
+ }
230
+ const removedLatest = new Map<string, (typeof removals)[number]>();
231
+ for (const r of removals) removedLatest.set(r.id, r);
232
+ const removedIds = new Set<string>(removedLatest.keys());
233
+ for (const id of store.list()) {
234
+ const row = store.row(id);
235
+ if (row !== undefined && row.archived === 1 && row.archived_reason === REMOVED_BY_OWNER) removedIds.add(id);
236
+ }
237
+ const reasons: HealthView["archive"]["reasons"] = [];
238
+ const itemsOf = (ids: readonly string[]) =>
239
+ ids.slice(0, ARCHIVE_ITEMS_CAP).map((id) => {
240
+ // The archived row's OWN words (not its successor's) when they can be
241
+ // shown; otherwise the named absence or the withholding.
242
+ const r = revealHere(store, id, 90);
243
+ return { id, label: r.text ?? r.label, note: null };
244
+ });
245
+ for (const [reason, phrase] of ARCHIVE_PHRASES) {
246
+ if (reason === REMOVED_BY_OWNER) {
247
+ const ids = [...removedIds];
248
+ if (ids.length === 0 && !ALWAYS_SHOWN.has(reason)) continue;
249
+ reasons.push({
250
+ reason,
251
+ phrase,
252
+ count: ids.length,
253
+ known: true,
254
+ items: ids.slice(0, ARCHIVE_ITEMS_CAP).map((id) => {
255
+ const rec = removedLatest.get(id);
256
+ return {
257
+ id,
258
+ label: rec?.label ?? reveal(store, id, 72).label,
259
+ note: rec === undefined ? null : `${rec.stage} · by ${rec.actor}${rec.reason ? ` · ${rec.reason}` : ""}`,
260
+ };
261
+ }),
262
+ });
263
+ continue;
264
+ }
265
+ const ids = byReason.get(reason) ?? [];
266
+ byReason.delete(reason);
267
+ if (ids.length === 0 && !ALWAYS_SHOWN.has(reason)) continue;
268
+ reasons.push({ reason, phrase, count: ids.length, known: true, items: itemsOf(ids) });
269
+ }
270
+ for (const [reason, ids] of byReason) {
271
+ reasons.push({
272
+ reason: reason === "" ? "(none)" : reason,
273
+ phrase: unmappedArchiveWords(reason),
274
+ count: ids.length,
275
+ known: false,
276
+ items: itemsOf(ids),
277
+ });
278
+ }
279
+ const archive = { total: reasons.reduce((n, r) => n + r.count, 0), reasons };
280
+
281
+ return {
282
+ cycle,
283
+ archive,
284
+ phases,
285
+ symmetry,
286
+ records,
287
+ heatmap: { days, names: [...DURABLE_EVENT_NAMES], cells },
288
+ removals,
289
+ removalsAbsent: removals.length === 0 ? (everLived ? NONE : NEVER) : null,
290
+ blind: BLIND_SPOTS,
291
+ };
292
+ }
293
+
294
+ /**
295
+ * WHAT I CANNOT SEE — the named absences, in the shape the terminal `status`
296
+ * view established: a number I do not durably hold is stated as a number I do
297
+ * not hold, never invented to fill a panel. Every line here is a real gap, and
298
+ * three of them are filed in `INTERFACE-GAPS.md`.
299
+ */
300
+ export const BLIND_SPOTS: readonly { readonly what: string; readonly why: string }[] = [
301
+ {
302
+ what: "What one cycle did.",
303
+ why: "There is no durable per-cycle outcome record — the report goes back to its caller and is gone. What survives is each phase's completion marker and each row's archived-with-reason state, so every exit count on this page is SINCE BIRTH, never 'this cycle'.",
304
+ },
305
+ {
306
+ what: "A protected element I can no longer read.",
307
+ why: "Protection is a flag on the row whose physics stopped reading, so a removed protected element simply leaves the list. The identity band is queryable and that half is recovered above; this half is not (INTERFACE-GAPS §3).",
308
+ },
309
+ {
310
+ what: "Recall latency, unless an adapter recorded it.",
311
+ why: "The core times its own surfacing race and reports it in the decision row. A turn whose adapter wrote no `adapter.recall` leaves no latency behind at all.",
312
+ },
313
+ {
314
+ what: "Anything older than the retention window.",
315
+ why: "The event log is bounded telemetry, not canonical memory. Rows past the window are swept unless a replay latch holds them, so every count on this page is 'what I still have', not 'what ever happened'.",
316
+ },
317
+ {
318
+ what: "Whether a memory was ever actually useful to you.",
319
+ why: "I record that the reply used it, which is the closest thing I have. Whether it helped is a judgement only you can make, and nothing here should be read as evidence of it.",
320
+ },
321
+ {
322
+ what: "Anything a host did not tell me.",
323
+ why: "The wake bundle's arrival, the ask's outcome, the bytes injected — all of it reaches me only if the adapter wrote a row. Silence on this page is sometimes the adapter's silence, not the machine's.",
324
+ },
325
+ ];
326
+
327
+ /** Everything the terminal views print, as one number each — the JSON the app's
328
+ * header badge reads without pulling a whole page. */