counterparts 0.3.2 → 0.3.4

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 (153) hide show
  1. package/CHANGELOG.md +153 -0
  2. package/docs/module-map.md +3 -2
  3. package/package.json +1 -1
  4. package/src/adapters/claude-code/NOTES.md +28 -0
  5. package/src/adapters/claude-code/bin/hook.ts +85 -22
  6. package/src/adapters/claude-code/config.ts +7 -0
  7. package/src/adapters/claude-code/doctor.ts +166 -4
  8. package/src/adapters/claude-code/hooks.ts +185 -6
  9. package/src/adapters/claude-code/index.ts +3 -0
  10. package/src/adapters/claude-code/transcript.ts +7 -0
  11. package/src/adapters/cli/CONTRACT.md +11 -0
  12. package/src/adapters/cli/INTERFACE-GAPS.md +16 -0
  13. package/src/adapters/cli/NOTES.md +25 -0
  14. package/src/adapters/cli/commands.ts +184 -4
  15. package/src/adapters/cli/dream-core.ts +126 -0
  16. package/src/adapters/cli/help.ts +17 -0
  17. package/src/adapters/cli/mechanisms.ts +199 -106
  18. package/src/adapters/dashboard/CONTRACT.md +5 -0
  19. package/src/adapters/dashboard/INTERFACE-GAPS.md +12 -0
  20. package/src/adapters/dashboard/NOTES.md +38 -0
  21. package/src/adapters/dashboard/registries.ts +35 -1
  22. package/src/adapters/dashboard/web/README.md +63 -26
  23. package/src/adapters/dashboard/web/app.html +2 -0
  24. package/src/adapters/dashboard/web/flow.ts +14 -0
  25. package/src/adapters/dashboard/web/lanes.ts +141 -0
  26. package/src/adapters/dashboard/web/mechanisms/association/index.js +1 -2
  27. package/src/adapters/dashboard/web/mechanisms/consolidation/index.js +8 -5
  28. package/src/adapters/dashboard/web/mechanisms/consolidation/panel.js +7 -7
  29. package/src/adapters/dashboard/web/mechanisms/decay/index.js +1 -2
  30. package/src/adapters/dashboard/web/mechanisms/dreaming/index.js +19 -0
  31. package/src/adapters/dashboard/web/mechanisms/emotional/index.js +9 -6
  32. package/src/adapters/dashboard/web/mechanisms/episodic-semantic/index.js +4 -5
  33. package/src/adapters/dashboard/web/mechanisms/index.js +5 -3
  34. package/src/adapters/dashboard/web/mechanisms/interference/index.js +1 -2
  35. package/src/adapters/dashboard/web/mechanisms/prospective/index.js +7 -5
  36. package/src/adapters/dashboard/web/mechanisms/reconsolidation/index.js +1 -2
  37. package/src/adapters/dashboard/web/mechanisms/regions.js +1 -1
  38. package/src/adapters/dashboard/web/mechanisms/retrieval/index.js +1 -2
  39. package/src/adapters/dashboard/web/mechanisms/salience/index.js +1 -2
  40. package/src/adapters/dashboard/web/mechanisms/schema/index.js +1 -2
  41. package/src/adapters/dashboard/web/narrate.ts +76 -13
  42. package/src/adapters/dashboard/web/pages/health/sections/checks.js +17 -1
  43. package/src/adapters/dashboard/web/pages/home/home.css +29 -13
  44. package/src/adapters/dashboard/web/pages/home/index.js +2 -0
  45. package/src/adapters/dashboard/web/pages/home/sections/explorer.js +22 -7
  46. package/src/adapters/dashboard/web/pages/home/sections/hero.js +15 -3
  47. package/src/adapters/dashboard/web/pages/home/sections/live-activity.js +15 -9
  48. package/src/adapters/dashboard/web/pages/home/sections/recent-chapters.js +6 -2
  49. package/src/adapters/dashboard/web/pages/home/sections/tiles.js +35 -18
  50. package/src/adapters/dashboard/web/pages/memories/index.js +30 -36
  51. package/src/adapters/dashboard/web/pages/memories/memories.css +97 -72
  52. package/src/adapters/dashboard/web/pages/memories/row.js +51 -0
  53. package/src/adapters/dashboard/web/pages/memories/sections/feel.js +176 -0
  54. package/src/adapters/dashboard/web/pages/memories/sections/hold.js +67 -0
  55. package/src/adapters/dashboard/web/pages/memories/sections/list.js +65 -43
  56. package/src/adapters/dashboard/web/pages/memories/sections/search.js +26 -23
  57. package/src/adapters/dashboard/web/pages/memories/state.js +12 -5
  58. package/src/adapters/dashboard/web/pages/self/index.js +57 -16
  59. package/src/adapters/dashboard/web/pages/self/sections/dreams.js +76 -0
  60. package/src/adapters/dashboard/web/pages/self/sections/journal.js +75 -19
  61. package/src/adapters/dashboard/web/pages/self/sections/page.js +108 -65
  62. package/src/adapters/dashboard/web/pages/self/sections/settling.js +129 -104
  63. package/src/adapters/dashboard/web/pages/self/sections/wake.js +31 -15
  64. package/src/adapters/dashboard/web/pages/self/self.css +122 -59
  65. package/src/adapters/dashboard/web/pages/self/state.js +21 -0
  66. package/src/adapters/dashboard/web/server.ts +9 -0
  67. package/src/adapters/dashboard/web/shared/memory-marks.css +17 -0
  68. package/src/adapters/dashboard/web/shared/memory-marks.js +81 -0
  69. package/src/adapters/dashboard/web/shared/memory-modal.js +240 -53
  70. package/src/adapters/dashboard/web/shared/modal.css +67 -0
  71. package/src/adapters/dashboard/web/shared/tip.js +1 -1
  72. package/src/adapters/dashboard/web/shared/widgets/feed.css +4 -0
  73. package/src/adapters/dashboard/web/shared/widgets/feed.js +56 -14
  74. package/src/adapters/dashboard/web/shared/widgets/light.css +1 -0
  75. package/src/adapters/dashboard/web/shared/widgets/light.js +7 -5
  76. package/src/adapters/dashboard/web/shared/widgets/tips.css +19 -0
  77. package/src/adapters/dashboard/web/shared/widgets/tips.js +56 -0
  78. package/src/adapters/dashboard/web/views/activity.ts +18 -2
  79. package/src/adapters/dashboard/web/views/archive-words.ts +27 -10
  80. package/src/adapters/dashboard/web/views/dreams.ts +199 -0
  81. package/src/adapters/dashboard/web/views/mechanism-panel.ts +116 -35
  82. package/src/adapters/dashboard/web/views/mechanisms.ts +81 -255
  83. package/src/adapters/dashboard/web/views/memories.ts +245 -43
  84. package/src/adapters/dashboard/web/views/memory-words.ts +133 -0
  85. package/src/adapters/dashboard/web/views/memory.ts +226 -4
  86. package/src/adapters/dashboard/web/views/mind.ts +261 -61
  87. package/src/adapters/dashboard/web/views/overview.ts +80 -42
  88. package/src/adapters/dashboard/web/views/search.ts +35 -1
  89. package/src/adapters/dashboard/web/views/shared.ts +14 -0
  90. package/src/adapters/dashboard/web/views.ts +2 -0
  91. package/src/adapters/feelings-line.ts +74 -0
  92. package/src/adapters/fired.ts +163 -23
  93. package/src/adapters/mcp/CONTRACT.md +8 -0
  94. package/src/adapters/mcp/NOTES.md +56 -0
  95. package/src/adapters/mcp/server.ts +254 -6
  96. package/src/adapters/mcp/tools.ts +180 -5
  97. package/src/adapters/mechanism-evidence.ts +465 -0
  98. package/src/core/bridge.ts +21 -16
  99. package/src/core/counterpart.ts +422 -10
  100. package/src/core/dream/CONTRACT.md +143 -0
  101. package/src/core/dream/INTERFACE-GAPS.md +24 -0
  102. package/src/core/dream/NOTES.md +84 -0
  103. package/src/core/dream/index.ts +1067 -0
  104. package/src/core/dream/mark.ts +14 -0
  105. package/src/core/dream/tunables.ts +56 -0
  106. package/src/core/encode/CONTRACT.md +7 -1
  107. package/src/core/feelings-wheel.ts +171 -13
  108. package/src/core/mint.ts +9 -0
  109. package/src/core/physics/CONTRACT.md +132 -21
  110. package/src/core/physics/NOTES.md +233 -0
  111. package/src/core/physics/index.ts +419 -50
  112. package/src/core/prospective/CONTRACT.md +35 -6
  113. package/src/core/prospective/INTERFACE-GAPS.md +10 -5
  114. package/src/core/prospective/NOTES.md +175 -4
  115. package/src/core/prospective/derive.ts +32 -3
  116. package/src/core/prospective/index.ts +362 -39
  117. package/src/core/prospective/tunables.ts +18 -1
  118. package/src/core/prospective/windows.ts +104 -46
  119. package/src/core/recall/CONTRACT.md +28 -0
  120. package/src/core/recall/NOTES.md +55 -0
  121. package/src/core/recall/activate.ts +32 -5
  122. package/src/core/recall/gate.ts +7 -1
  123. package/src/core/recall/index.ts +28 -2
  124. package/src/core/recall/mood.ts +106 -0
  125. package/src/core/recall/tunables.ts +26 -0
  126. package/src/core/remember/CONTRACT.md +5 -0
  127. package/src/core/remember/index.ts +1 -0
  128. package/src/core/remember/proposals.ts +86 -3
  129. package/src/core/remember/spans.ts +11 -1
  130. package/src/core/self/CONTRACT.md +3 -0
  131. package/src/core/self/INTERFACE-GAPS.md +7 -0
  132. package/src/core/self/NOTES.md +30 -0
  133. package/src/core/self/identity.ts +115 -6
  134. package/src/core/self/index.ts +17 -0
  135. package/src/core/self/tunables.ts +17 -0
  136. package/src/core/sleep/CONTRACT.md +17 -1
  137. package/src/core/sleep/INTERFACE-GAPS.md +19 -0
  138. package/src/core/sleep/NOTES.md +36 -0
  139. package/src/core/sleep/consolidate.ts +149 -96
  140. package/src/core/sleep/decay.ts +11 -0
  141. package/src/core/sleep/index.ts +4 -1
  142. package/src/core/sleep/tunables.ts +9 -0
  143. package/src/core/sleep/types.ts +17 -0
  144. package/src/core/sleep/upgrade.ts +168 -0
  145. package/src/core/store/CONTRACT.md +21 -2
  146. package/src/core/store/NOTES.md +68 -0
  147. package/src/core/store/feelings.ts +23 -6
  148. package/src/core/store/index.ts +716 -17
  149. package/src/core/store/operational.ts +329 -4
  150. package/src/core/store/owner-op-seam.ts +79 -1
  151. package/src/core/types.ts +37 -0
  152. package/src/adapters/dashboard/web/pages/memories/sections/constellation.js +0 -178
  153. package/src/adapters/dashboard/web/pages/memories/sections/kinds.js +0 -42
@@ -1,20 +1,24 @@
1
1
  /**
2
- * `/api/memories` — the constellation and the kinds; `/api/memories/list` — every memory.
2
+ * `/api/memories` — how firmly what I hold is held, and how it feels (the two
3
+ * pictures above the list), plus the census the other tabs' tests lean on;
4
+ * `/api/memories/list` — every memory.
3
5
  *
4
6
  * Split out of `web/views.ts`, which re-exports every public name from here;
5
7
  * the four rules in that file's header apply to every line below.
6
8
  */
9
+ import { CORE_EMOTIONS } from "../../../../core/feelings-wheel.js";
7
10
  import { TUNABLES as PHYSICS, band as bandOf, strength as strengthOf } from "../../../../core/physics/index.js";
8
- import { isJournal } from "../../../../core/sleep/index.js";
11
+ import { isEntityCard, isJournal } from "../../../../core/sleep/index.js";
9
12
  import { rowToPhysics } from "../../../../core/store/index.js";
10
- import type { Band, Kind } from "../../../../core/types.js";
13
+ import type { Band, Kind, MemoryPhysics } from "../../../../core/types.js";
11
14
  import { NEVER, NONE } from "../../layout.js";
12
15
  import { BANDS, KINDS } from "../../registries.js";
13
16
  import type { DashboardSource } from "../../source.js";
14
17
  import { WITHHELD, revealHere } from "../reveal.js";
15
18
  import { archiveWords } from "./archive-words.js";
16
- import { BAND_GLOSS } from "./rows.js";
17
- import type { BarRow } from "./rows.js";
19
+ import { letGoDay } from "./mechanism-panel.js";
20
+ import { feelingsShown, isChapterMemory, shownOf } from "./memory-words.js";
21
+ import type { DateFrom, FeelingShown } from "./memory-words.js";
18
22
  import { absenceFor, census, countMap } from "./shared.js";
19
23
  import type { MemoryLine } from "./shared.js";
20
24
 
@@ -47,15 +51,61 @@ export interface MemoriesView {
47
51
  readonly memories: number;
48
52
  /** Of `total`: entities and beliefs about them (schema rows). */
49
53
  readonly schemas: number;
54
+ /** How firmly what I hold is held: firm / settling / fading, over every live
55
+ * row but the journal chapters, which are counted apart (they aren't scored). */
56
+ readonly hold: { readonly firm: number; readonly settling: number; readonly fading: number; readonly journal: number };
57
+ /** How many lived days ahead "fading" looks (`NEAR_LET_GO_DAYS`). */
58
+ readonly nearLetGoDays: number;
59
+ /** How many lived days ahead "firm" looks (`FIRM_AHEAD_DAYS`). */
60
+ readonly firmAheadDays: number;
61
+ /** How it feels: the feelings on live memories, by the wheel's six cores. */
62
+ readonly feelings: FeelingsView;
63
+ /** The census, strongest first. Nothing on the page draws it now; kept for the
64
+ * callers and tests that read it. */
50
65
  readonly points: MemoryLine[];
51
- readonly distribution: { from: number; to: number; count: number }[];
52
- /** Strength in ten steps, each step split by today's band — the scatter's
53
- * right margin. Over every row, not only the plotted ones. */
54
66
  readonly strengthByBand: { from: number; to: number; bands: Record<string, number> }[];
55
- readonly bands: BarRow[];
56
67
  readonly kinds: KindRow[];
57
68
  readonly pointsAbsent: string | null;
58
- readonly note: string;
69
+ }
70
+
71
+ /** One person's side of one core feeling. */
72
+ export interface FeelingSide {
73
+ /** How many feelings are recorded under this core. */
74
+ readonly count: number;
75
+ /** Their recorded strengths, summed (each 0..1, as felt). */
76
+ readonly sum: number;
77
+ /** The specific feelings under it, most first: `proud 3, joyful 1`. */
78
+ readonly words: { readonly word: string; readonly count: number }[];
79
+ }
80
+
81
+ export interface FeelingsView {
82
+ /** Live memories carrying at least one feeling. */
83
+ readonly carrying: number;
84
+ /** In the wheel's order (`CORE_EMOTIONS`): yours (`whose = owner`) and mine (`whose = self`). */
85
+ readonly cores: { readonly core: string; readonly yours: FeelingSide; readonly mine: FeelingSide }[];
86
+ }
87
+
88
+ /** How firmly a memory is held, in three words. */
89
+ export type Hold = "firm" | "settling" | "fading";
90
+
91
+ /** "Fading": prune would take it within this many lived days if nobody used it. */
92
+ export const NEAR_LET_GO_DAYS = 14;
93
+ /** "Firm": unused, it is still above the semantic floor (`THETA_SEM`, settled
94
+ * into what I know) this many lived days from now — far from the let-go line. */
95
+ export const FIRM_AHEAD_DAYS = 30;
96
+
97
+ /**
98
+ * Firm, settling or fading. Core and protected memories are firm (neither is
99
+ * pruned). Fading is prune's own verdict within `NEAR_LET_GO_DAYS` (`letGoDay`);
100
+ * an entity card is faded by `schemas/`, not by prune, so it is never fading
101
+ * here. Firm is still settled `FIRM_AHEAD_DAYS` from now if unused. The rest
102
+ * are settling.
103
+ */
104
+ export function holdOf(physics: MemoryPhysics, day: number, opts: { prunable: boolean }): Hold {
105
+ if (physics.promotedIdentity === true || physics.protected === true) return "firm";
106
+ if (opts.prunable && letGoDay(physics, day, NEAR_LET_GO_DAYS) !== null) return "fading";
107
+ if (strengthOf(physics, day + FIRM_AHEAD_DAYS) >= PHYSICS.THETA_SEM) return "firm";
108
+ return "settling";
59
109
  }
60
110
 
61
111
  const KIND_GLOSS: Record<Kind, string> = {
@@ -91,8 +141,6 @@ export function memoriesView(src: DashboardSource, opts: { limit?: number } = {}
91
141
  const rows = census(src);
92
142
  const limit = opts.limit ?? 4000;
93
143
  const byKind = countMap<Kind>(rows, "kind");
94
- const byBand = countMap<Band>(rows, "band");
95
- const peak = Math.max(1, ...BANDS.map((b) => byBand.get(b) ?? 0));
96
144
  const everLived = day > 0 || rows.length > 0;
97
145
 
98
146
  const meanByKind = new Map<Kind, number[]>();
@@ -102,18 +150,6 @@ export function memoriesView(src: DashboardSource, opts: { limit?: number } = {}
102
150
  meanByKind.set(r.kind, list);
103
151
  }
104
152
 
105
- const buckets = 20;
106
- const distribution = Array.from({ length: buckets }, (_, i) => ({
107
- from: i / buckets,
108
- to: (i + 1) / buckets,
109
- count: 0,
110
- }));
111
- for (const r of rows) {
112
- const i = Math.min(buckets - 1, Math.max(0, Math.floor(r.strength * buckets)));
113
- const bucket = distribution[i];
114
- if (bucket !== undefined) bucket.count += 1;
115
- }
116
-
117
153
  const steps = 10;
118
154
  const strengthByBand = Array.from({ length: steps }, (_, i) => ({
119
155
  from: i / steps,
@@ -125,19 +161,52 @@ export function memoriesView(src: DashboardSource, opts: { limit?: number } = {}
125
161
  if (step !== undefined) step.bands[r.band] = (step.bands[r.band] ?? 0) + 1;
126
162
  }
127
163
 
164
+ const hold = { firm: 0, settling: 0, fading: 0, journal: 0 };
165
+ const sides = new Map<string, { yours: Side; mine: Side }>(
166
+ CORE_EMOTIONS.map((c) => [c, { yours: side(), mine: side() }]),
167
+ );
168
+ let carrying = 0;
169
+ for (const m of rows) {
170
+ const row = store.row(m.id);
171
+ if (row !== undefined && isChapterMemory(row)) hold.journal += 1;
172
+ else if (m.unreadable) hold.settling += 1;
173
+ else {
174
+ try {
175
+ hold[holdOf(store.physicsOf(m.id), day, { prunable: row === undefined || !isEntityCard(row) })] += 1;
176
+ } catch {
177
+ hold.settling += 1;
178
+ }
179
+ }
180
+ const felt = feelingsShown(store, m.id);
181
+ if (felt.length > 0) carrying += 1;
182
+ for (const f of felt) {
183
+ const at = sides.get(f.core);
184
+ const s = f.whose === "owner" ? at?.yours : f.whose === "self" ? at?.mine : undefined;
185
+ if (s === undefined) continue;
186
+ s.count += 1;
187
+ s.sum += Math.max(0, Math.min(1, f.strength));
188
+ s.words.set(f.word, (s.words.get(f.word) ?? 0) + 1);
189
+ }
190
+ }
191
+
128
192
  return {
129
193
  day,
130
194
  total: rows.length,
131
195
  memories: rows.filter((r) => !r.schema).length,
132
196
  schemas: rows.filter((r) => r.schema).length,
197
+ hold,
198
+ nearLetGoDays: NEAR_LET_GO_DAYS,
199
+ firmAheadDays: FIRM_AHEAD_DAYS,
200
+ feelings: {
201
+ carrying,
202
+ cores: CORE_EMOTIONS.map((core) => {
203
+ const at = sides.get(core) ?? { yours: side(), mine: side() };
204
+ return { core, yours: sideOut(at.yours), mine: sideOut(at.mine) };
205
+ }),
206
+ },
133
207
  strengthByBand,
134
208
  points: rows.slice(0, limit),
135
209
  pointsAbsent: rows.length === 0 ? (everLived ? NONE : NEVER) : null,
136
- distribution,
137
- bands: BANDS.map((b) => {
138
- const count = byBand.get(b) ?? 0;
139
- return { label: b, count, fraction: count / peak, note: BAND_GLOSS[b], absent: absenceFor(count, everLived) };
140
- }),
141
210
  kinds: KINDS.map((kind) => {
142
211
  const count = byKind.get(kind) ?? 0;
143
212
  const list = meanByKind.get(kind) ?? [];
@@ -156,7 +225,22 @@ export function memoriesView(src: DashboardSource, opts: { limit?: number } = {}
156
225
  fadeSpeed: fadeSpeed(tunables.kappa),
157
226
  };
158
227
  }),
159
- note: "Every dot is one memory — or one of the beliefs and entities the schemas hold, which decay and consolidate the same way and are counted apart only where a headline says memories. How many lived days old across, how strong up, how much it mattered at encoding as its size. Colour is the band it is in today, computed now — not the band it was born into.",
228
+ };
229
+ }
230
+
231
+ interface Side {
232
+ count: number;
233
+ sum: number;
234
+ words: Map<string, number>;
235
+ }
236
+ const side = (): Side => ({ count: 0, sum: 0, words: new Map() });
237
+ function sideOut(s: Side): FeelingSide {
238
+ return {
239
+ count: s.count,
240
+ sum: Math.round(s.sum * 1000) / 1000,
241
+ words: [...s.words.entries()]
242
+ .map(([word, count]) => ({ word, count }))
243
+ .sort((a, b) => b.count - a.count || (a.word < b.word ? -1 : 1)),
160
244
  };
161
245
  }
162
246
 
@@ -187,13 +271,37 @@ export interface ListRow {
187
271
  readonly schema: boolean;
188
272
  /** For a schema row: what it is — "entity", "belief", "current state". */
189
273
  readonly schemaRole: string | null;
274
+ /** The words to show: headings dropped, a date written at their front lifted off. */
275
+ readonly text: string;
276
+ /** The date shown beside the row (`YYYY-MM-DD`), and where it came from. */
277
+ readonly date: string | null;
278
+ readonly dateFrom: DateFrom | null;
279
+ /** In the core (promoted into identity). */
280
+ readonly core: boolean;
281
+ readonly protected: boolean;
282
+ /** A journal-chapter memory (`isChapterMemory`): not scored, shown as "journal". */
283
+ readonly journal: boolean;
284
+ readonly feelings: FeelingShown[];
285
+ /** v7 moment it was written (UTC ms); null on an older row. */
286
+ readonly createdAt: number | null;
287
+ /** How firmly it is held (`holdOf`); null for an archived row or a journal chapter. */
288
+ readonly hold: Hold | null;
190
289
  }
191
290
 
291
+ export type ListSort = "newest" | "oldest";
292
+
192
293
  export interface MemoryListView {
193
294
  readonly day: number;
194
295
  readonly state: ListState;
195
296
  readonly kind: Kind | null;
196
297
  readonly band: Band | null;
298
+ readonly core: boolean;
299
+ readonly journal: boolean;
300
+ readonly hold: Hold | null;
301
+ /** A feeling filter from the radar: one word from one side, or every feeling under one core. */
302
+ readonly feeling: { readonly word: string; readonly whose: "owner" | "self" } | null;
303
+ readonly feelingCore: string | null;
304
+ readonly sort: ListSort;
197
305
  readonly offset: number;
198
306
  readonly limit: number;
199
307
  /** Rows matching every filter — what the pager pages over. */
@@ -204,6 +312,11 @@ export interface MemoryListView {
204
312
  readonly archived: number;
205
313
  readonly kinds: Record<string, number>;
206
314
  readonly bands: Record<string, number>;
315
+ /** In the core, and journal chapters — each within the live/archived choice. */
316
+ readonly core: number;
317
+ readonly journal: number;
318
+ /** Firm / settling / fading — live rows only, journal chapters left out. */
319
+ readonly hold: Record<Hold, number>;
207
320
  };
208
321
  readonly rows: ListRow[];
209
322
  readonly absent: string | null;
@@ -242,7 +355,22 @@ function firstLine(body: string, width: number): string {
242
355
  */
243
356
  export function memoryListView(
244
357
  src: DashboardSource,
245
- opts: { state?: string | null; kind?: string | null; band?: string | null; offset?: number; limit?: number } = {},
358
+ opts: {
359
+ state?: string | null;
360
+ kind?: string | null;
361
+ band?: string | null;
362
+ core?: boolean;
363
+ journal?: boolean;
364
+ hold?: string | null;
365
+ /** A feeling's word as shown (`proud`, or an `other`'s own word), with `whose`. */
366
+ feeling?: string | null;
367
+ whose?: string | null;
368
+ /** Every feeling under this core, from either side. */
369
+ feelingCore?: string | null;
370
+ sort?: string | null;
371
+ offset?: number;
372
+ limit?: number;
373
+ } = {},
246
374
  ): MemoryListView {
247
375
  const store = src.store;
248
376
  const day = store.livedDay();
@@ -251,6 +379,21 @@ export function memoryListView(
251
379
  const band = BANDS.includes(opts.band as Band) ? (opts.band as Band) : null;
252
380
  const limit = Math.max(1, Math.min(LIST_MAX, Math.floor(opts.limit ?? 50)));
253
381
  const offset = Math.max(0, Math.floor(opts.offset ?? 0));
382
+ const onlyCore = opts.core === true;
383
+ const onlyJournal = opts.journal === true;
384
+ const sort: ListSort = opts.sort === "oldest" ? "oldest" : "newest";
385
+ const word = typeof opts.feeling === "string" ? opts.feeling.trim().toLowerCase() : "";
386
+ const feeling: MemoryListView["feeling"] =
387
+ word.length > 0 && (opts.whose === "owner" || opts.whose === "self") ? { word, whose: opts.whose } : null;
388
+ const feelingCore = feeling === null && (CORE_EMOTIONS as readonly string[]).includes(opts.feelingCore ?? "") ? (opts.feelingCore as string) : null;
389
+ // Read only when a feeling filter asks for it: one feelings read per row.
390
+ const felt = (id: string): boolean => {
391
+ if (feeling === null && feelingCore === null) return true;
392
+ return feelingsShown(store, id).some((f) =>
393
+ feeling !== null ? f.whose === feeling.whose && f.word.toLowerCase() === feeling.word : f.core === feelingCore,
394
+ );
395
+ };
396
+ const onlyHold: Hold | null = opts.hold === "firm" || opts.hold === "settling" || opts.hold === "fading" ? opts.hold : null;
254
397
 
255
398
  interface Slim {
256
399
  id: string;
@@ -259,7 +402,11 @@ export function memoryListView(
259
402
  strength: number;
260
403
  bornDay: number;
261
404
  learnedOn: string;
405
+ createdAt: number | null;
262
406
  archived: boolean;
407
+ core: boolean;
408
+ journal: boolean;
409
+ hold: Hold | null;
263
410
  }
264
411
  const all: Slim[] = [];
265
412
  let live = 0;
@@ -274,42 +421,82 @@ export function memoryListView(
274
421
  if (state === "archived" && !isArchived) continue;
275
422
  let b: Band = row.band;
276
423
  let s = 0;
424
+ const chapter = isChapterMemory(row);
425
+ let hold: Hold | null = null;
277
426
  try {
278
427
  const physics = rowToPhysics(row);
279
428
  b = bandOf(physics, day);
280
429
  s = strengthOf(physics, day);
430
+ if (!isArchived && !chapter) hold = holdOf(physics, day, { prunable: !isEntityCard(row) });
281
431
  } catch {
282
432
  /* a row whose physics will not compute still lists, at zero */
433
+ if (!isArchived && !chapter) hold = "settling";
283
434
  }
284
- all.push({ id, kind: row.kind, band: b, strength: s, bornDay: row.birth_day, learnedOn: row.learned_on, archived: isArchived });
435
+ all.push({
436
+ id,
437
+ kind: row.kind,
438
+ band: b,
439
+ strength: s,
440
+ bornDay: row.birth_day,
441
+ learnedOn: row.learned_on,
442
+ createdAt: row.created_at ?? null,
443
+ archived: isArchived,
444
+ core: row.promoted_identity === 1,
445
+ journal: chapter,
446
+ hold,
447
+ });
285
448
  }
286
449
  const kinds: Record<string, number> = Object.fromEntries(KINDS.map((k) => [k, 0]));
287
450
  const bands: Record<string, number> = Object.fromEntries(BANDS.map((b) => [b, 0]));
451
+ let core = 0;
452
+ let journal = 0;
453
+ const holds: Record<Hold, number> = { firm: 0, settling: 0, fading: 0 };
288
454
  for (const r of all) {
455
+ if (r.hold !== null) holds[r.hold] += 1;
289
456
  kinds[r.kind] = (kinds[r.kind] ?? 0) + 1;
290
457
  bands[r.band] = (bands[r.band] ?? 0) + 1;
458
+ if (r.core) core += 1;
459
+ if (r.journal) journal += 1;
291
460
  }
292
- const matching = all.filter((r) => (kind === null || r.kind === kind) && (band === null || r.band === band));
293
- // Newest first: the lived day it was born, then the calendar day it was
294
- // recorded, then the id — so the order is defined, not SQLite's.
295
- matching.sort(
296
- (a, b) =>
297
- b.bornDay - a.bornDay ||
298
- (a.learnedOn < b.learnedOn ? 1 : a.learnedOn > b.learnedOn ? -1 : 0) ||
299
- (a.id < b.id ? -1 : a.id > b.id ? 1 : 0),
461
+ const matching = all.filter(
462
+ (r) =>
463
+ (kind === null || r.kind === kind) &&
464
+ (band === null || r.band === band) &&
465
+ (!onlyCore || r.core) &&
466
+ (!onlyJournal || r.journal) &&
467
+ (onlyHold === null || r.hold === onlyHold) &&
468
+ felt(r.id),
300
469
  );
470
+ // Newest first: the lived day it was born, then the moment it was written
471
+ // (v7 `created_at`; a row from before v7 has none and counts as the older),
472
+ // then the calendar day it was recorded, then the id — so the order is
473
+ // defined, not SQLite's. Oldest first is exactly the reverse.
474
+ const newestFirst = (a: Slim, b: Slim): number =>
475
+ b.bornDay - a.bornDay ||
476
+ (b.createdAt ?? -Infinity) - (a.createdAt ?? -Infinity) ||
477
+ (a.learnedOn < b.learnedOn ? 1 : a.learnedOn > b.learnedOn ? -1 : 0) ||
478
+ (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
479
+ matching.sort(sort === "oldest" ? (a, b) => newestFirst(b, a) : newestFirst);
301
480
  const rows: ListRow[] = matching.slice(offset, offset + limit).map((r) => {
302
481
  const row = store.row(r.id);
303
482
  const here = revealHere(store, r.id, 160);
304
483
  let title: string | null = null;
305
484
  let line: string;
306
- if (!here.present) line = here.label;
307
- else if (here.confidential) line = WITHHELD;
485
+ let text: string;
486
+ if (!here.present) line = text = here.label;
487
+ else if (here.confidential) line = text = WITHHELD;
308
488
  else {
309
489
  const t = row?.title ?? null;
310
490
  title = t !== null && t.trim().length > 0 ? t.trim() : null;
311
491
  line = firstLine(row?.body ?? "", 160) || here.label;
492
+ text = "";
312
493
  }
494
+ const shown = shownOf(row?.body ?? "", {
495
+ chapter: r.journal,
496
+ learnedOn: r.learnedOn,
497
+ confidential: here.confidential || !here.present,
498
+ withheld: text,
499
+ });
313
500
  return {
314
501
  id: r.id,
315
502
  title,
@@ -324,6 +511,15 @@ export function memoryListView(
324
511
  archivedReason: r.archived ? (row?.archived_reason ?? null) : null,
325
512
  schema: row?.type === "schema",
326
513
  schemaRole: row?.type === "schema" ? schemaRole(row.meta) : null,
514
+ text: shown.text || line,
515
+ date: shown.date,
516
+ dateFrom: shown.dateFrom,
517
+ core: r.core,
518
+ protected: row?.protected === 1,
519
+ journal: r.journal,
520
+ feelings: feelingsShown(store, r.id),
521
+ createdAt: r.createdAt,
522
+ hold: r.hold,
327
523
  };
328
524
  });
329
525
  const everLived = day > 0 || live + archived > 0;
@@ -332,10 +528,16 @@ export function memoryListView(
332
528
  state,
333
529
  kind,
334
530
  band,
531
+ core: onlyCore,
532
+ journal: onlyJournal,
533
+ hold: onlyHold,
534
+ feeling,
535
+ feelingCore,
536
+ sort,
335
537
  offset,
336
538
  limit,
337
539
  total: matching.length,
338
- counts: { live, archived, kinds, bands },
540
+ counts: { live, archived, kinds, bands, core, journal, hold: holds },
339
541
  rows,
340
542
  absent: matching.length === 0 ? (everLived ? NONE : NEVER) : null,
341
543
  };
@@ -0,0 +1,133 @@
1
+ /**
2
+ * How a memory's words are SHOWN on the memories tab — the row's text, the date
3
+ * beside it, whether it is a journal chapter, and its feelings in words. Shared
4
+ * by the list, search and the memory card (2026-09-26: an experiment, not a
5
+ * rule).
6
+ *
7
+ * Display only. Nothing here writes, and a date lifted off the front of a
8
+ * memory's text is lifted from what is SHOWN; the stored body is untouched.
9
+ */
10
+ import { wheelEntry } from "../../../../core/feelings-wheel.js";
11
+ import { readChapterLead } from "../../../../core/self/index.js";
12
+ import type { MemoryRow, ReadOnlyStore } from "../../../../core/store/index.js";
13
+
14
+ /** Where the date shown beside a row came from. */
15
+ export type DateFrom = "text" | "chapter" | "recorded";
16
+
17
+ export interface Shown {
18
+ /** The words to show, a date written at their front lifted off. */
19
+ readonly text: string;
20
+ /** `YYYY-MM-DD`, or null when nothing dates it. */
21
+ readonly date: string | null;
22
+ readonly dateFrom: DateFrom | null;
23
+ }
24
+
25
+ export interface FeelingShown {
26
+ readonly core: string;
27
+ /** The wheel's word, or the person's own word for an `other`. */
28
+ readonly word: string;
29
+ readonly whose: string;
30
+ readonly strength: number;
31
+ }
32
+
33
+ /**
34
+ * A JOURNAL-CHAPTER MEMORY: a `memory` row minted from a chapter of the journal
35
+ * (`source = episode`, `meta.episodeId`). Its text is the chapter as written,
36
+ * with no salience of its own, so a strength beside it reads "0%" — which looks
37
+ * like something failing. It is shown as "journal" instead. (The journal ENTRY
38
+ * itself, `type = episode`, is `sleep#isJournal`, and is not in the list.)
39
+ */
40
+ export function isChapterMemory(row: Pick<MemoryRow, "type" | "source" | "meta">): boolean {
41
+ if (row.type !== "memory") return false;
42
+ if (row.source === "episode") return true;
43
+ if (!row.meta.includes("episodeId")) return false;
44
+ try {
45
+ return typeof (JSON.parse(row.meta) as Record<string, unknown>)["episodeId"] === "string";
46
+ } catch {
47
+ return false;
48
+ }
49
+ }
50
+
51
+ /**
52
+ * A full `YYYY-MM-DD` at the very start, then `:`, `,`, a dash, or a space.
53
+ * "2026 was…" and "2026-09 was…" are not dates to lift.
54
+ */
55
+ const LEAD_DATE = /^(\d{4})-(\d{2})-(\d{2})(?:[ \t]*[:,—–-][ \t]*|[ \t]+)(?=\S)/;
56
+
57
+ /** The date written at the front of `text`, and the text after it — or `text` untouched. */
58
+ export function liftDate(text: string): { date: string | null; rest: string } {
59
+ const m = LEAD_DATE.exec(text);
60
+ if (m === null) return { date: null, rest: text };
61
+ const [, y, mo, d] = m;
62
+ const month = Number(mo);
63
+ const day = Number(d);
64
+ if (month < 1 || month > 12 || day < 1 || day > 31) return { date: null, rest: text };
65
+ // "2026-07-08, the pass…" reads as a sentence once the date is beside it.
66
+ const rest = text.slice(m[0].length);
67
+ return { date: `${y}-${mo}-${d}`, rest: rest.charAt(0).toUpperCase() + rest.slice(1) };
68
+ }
69
+
70
+ const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
71
+
72
+ /** A chapter heading's date (`Fri 10 Jul 2026`) as `2026-07-10`, or null. */
73
+ export function chapterDate(written: string | null): string | null {
74
+ if (written === null) return null;
75
+ const m = /^[A-Za-z]{3} (\d{1,2}) ([A-Za-z]{3}) (\d{4})$/.exec(written.trim());
76
+ if (m === null) return null;
77
+ const month = MONTHS.indexOf((m[2] ?? "").toLowerCase());
78
+ if (month < 0) return null;
79
+ return `${m[3]}-${String(month + 1).padStart(2, "0")}-${String(Number(m[1])).padStart(2, "0")}`;
80
+ }
81
+
82
+ const DAY = /^\d{4}-\d{2}-\d{2}$/;
83
+
84
+ /**
85
+ * What a row shows: its words on one line (markdown headings dropped), the date
86
+ * lifted off their front, and the date beside them — the written one, else a
87
+ * chapter's own, else the day it was recorded. A confidential row lifts nothing
88
+ * from its words: it shows `withheld` and the recorded day.
89
+ */
90
+ export function shownOf(
91
+ body: string,
92
+ opts: { chapter: boolean; learnedOn: string; confidential: boolean; withheld?: string; width?: number },
93
+ ): Shown {
94
+ const recorded = DAY.test(opts.learnedOn) ? opts.learnedOn : null;
95
+ const fallback = { date: recorded, dateFrom: recorded === null ? null : ("recorded" as const) };
96
+ if (opts.confidential) return { text: opts.withheld ?? "", ...fallback };
97
+ let words = body;
98
+ let fromChapter: string | null = null;
99
+ if (opts.chapter) {
100
+ const lead = readChapterLead(body);
101
+ words = lead.rest;
102
+ fromChapter = chapterDate(lead.date);
103
+ }
104
+ const flat = words
105
+ .split("\n")
106
+ .map((l) => l.trim())
107
+ .filter((l) => l.length > 0 && !l.startsWith("#"))
108
+ .join(" ")
109
+ .replace(/\s+/g, " ")
110
+ .trim();
111
+ const lifted = liftDate(flat);
112
+ const width = opts.width ?? 320;
113
+ const text = lifted.rest.length > width ? `${lifted.rest.slice(0, width - 1).trimEnd()}…` : lifted.rest;
114
+ if (lifted.date !== null) return { text, date: lifted.date, dateFrom: "text" };
115
+ if (fromChapter !== null) return { text, date: fromChapter, dateFrom: "chapter" };
116
+ return { text, ...fallback };
117
+ }
118
+
119
+ /** A memory's feelings in words, oldest first. The `carried_by` words are not here. */
120
+ export function feelingsShown(store: ReadOnlyStore, id: string): FeelingShown[] {
121
+ let rows;
122
+ try {
123
+ rows = store.feelingsFor(id);
124
+ } catch {
125
+ return [];
126
+ }
127
+ return rows.map((f) => ({
128
+ core: f.core,
129
+ word: f.emotion === "other" ? (f.other_word ?? "other") : (wheelEntry(f.emotion)?.word ?? f.emotion),
130
+ whose: f.whose,
131
+ strength: f.strength,
132
+ }));
133
+ }