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.
- package/CHANGELOG.md +76 -0
- package/docs/module-map.md +2 -0
- package/package.json +2 -1
- package/src/adapters/claude-code/CONTRACT.md +2 -1
- package/src/adapters/claude-code/NOTES.md +90 -2
- package/src/adapters/claude-code/bin/hook.ts +17 -2
- package/src/adapters/claude-code/bin/runner.ts +4 -1
- package/src/adapters/claude-code/config.ts +23 -0
- package/src/adapters/claude-code/doctor.ts +148 -26
- package/src/adapters/claude-code/hooks.ts +37 -12
- package/src/adapters/claude-code/index.ts +2 -0
- package/src/adapters/claude-code/standdown.ts +12 -1
- package/src/adapters/claude-code/transcript.ts +79 -6
- package/src/adapters/cli/INTERFACE-GAPS.md +4 -0
- package/src/adapters/cli/commands.ts +209 -61
- package/src/adapters/cli/export.ts +27 -3
- package/src/adapters/cli/help.ts +22 -8
- package/src/adapters/cli/install.ts +3 -5
- package/src/adapters/cli/mechanisms.ts +295 -0
- package/src/adapters/cli/repair-dates.ts +3 -1
- package/src/adapters/cli/snapshot.ts +4 -27
- package/src/adapters/dashboard/CONTRACT.md +13 -9
- package/src/adapters/dashboard/INTERFACE-GAPS.md +4 -0
- package/src/adapters/dashboard/NOTES.md +3 -2
- package/src/adapters/dashboard/activity.ts +2 -1
- package/src/adapters/dashboard/bin/dashboard.ts +18 -3
- package/src/adapters/dashboard/upgrade.ts +23 -0
- package/src/adapters/dashboard/web/README.md +153 -0
- package/src/adapters/dashboard/web/actions.ts +631 -0
- package/src/adapters/dashboard/web/app.html +40 -1703
- package/src/adapters/dashboard/web/app.js +55 -0
- package/src/adapters/dashboard/web/mechanisms/association/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/association/panel.js +17 -0
- package/src/adapters/dashboard/web/mechanisms/consolidation/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/consolidation/panel.js +23 -0
- package/src/adapters/dashboard/web/mechanisms/decay/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/decay/panel.js +45 -0
- package/src/adapters/dashboard/web/mechanisms/emotional/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/episodic-semantic/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/index.js +60 -0
- package/src/adapters/dashboard/web/mechanisms/interference/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/picture.js +28 -0
- package/src/adapters/dashboard/web/mechanisms/prospective/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/reconsolidation/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/reconsolidation/panel.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/regions.js +30 -0
- package/src/adapters/dashboard/web/mechanisms/retrieval/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/retrieval/panel.js +20 -0
- package/src/adapters/dashboard/web/mechanisms/salience/index.js +15 -0
- package/src/adapters/dashboard/web/mechanisms/salience/panel.js +14 -0
- package/src/adapters/dashboard/web/mechanisms/schema/index.js +15 -0
- package/src/adapters/dashboard/web/narrate.ts +10 -10
- package/src/adapters/dashboard/web/pages/flow/flow.css +38 -0
- package/src/adapters/dashboard/web/pages/flow/index.js +117 -0
- package/src/adapters/dashboard/web/pages/flow/sections/blind.js +12 -0
- package/src/adapters/dashboard/web/pages/flow/sections/detail.js +45 -0
- package/src/adapters/dashboard/web/pages/flow/sections/diagram.js +291 -0
- package/src/adapters/dashboard/web/pages/flow/sections/feed.js +11 -0
- package/src/adapters/dashboard/web/pages/flow/sections/fired.js +41 -0
- package/src/adapters/dashboard/web/pages/flow/sections/heatmap.js +25 -0
- package/src/adapters/dashboard/web/pages/flow/sections/records.js +24 -0
- package/src/adapters/dashboard/web/pages/flow/sections/symmetry.js +17 -0
- package/src/adapters/dashboard/web/pages/flow/state.js +3 -0
- package/src/adapters/dashboard/web/pages/health/health.css +93 -0
- package/src/adapters/dashboard/web/pages/health/index.js +41 -0
- package/src/adapters/dashboard/web/pages/health/sections/archive.js +84 -0
- package/src/adapters/dashboard/web/pages/health/sections/checks.js +172 -0
- package/src/adapters/dashboard/web/pages/health/sections/cycle.js +37 -0
- package/src/adapters/dashboard/web/pages/health/sections/verify.js +54 -0
- package/src/adapters/dashboard/web/pages/home/brain.js +375 -0
- package/src/adapters/dashboard/web/pages/home/home.css +134 -0
- package/src/adapters/dashboard/web/pages/home/index.js +84 -0
- package/src/adapters/dashboard/web/pages/home/sections/explorer.js +206 -0
- package/src/adapters/dashboard/web/pages/home/sections/hero.js +22 -0
- package/src/adapters/dashboard/web/pages/home/sections/live-activity.js +25 -0
- package/src/adapters/dashboard/web/pages/home/sections/recent-chapters.js +13 -0
- package/src/adapters/dashboard/web/pages/home/sections/tiles.js +52 -0
- package/src/adapters/dashboard/web/pages/memories/index.js +75 -0
- package/src/adapters/dashboard/web/pages/memories/memories.css +143 -0
- package/src/adapters/dashboard/web/pages/memories/sections/constellation.js +178 -0
- package/src/adapters/dashboard/web/pages/memories/sections/kinds.js +42 -0
- package/src/adapters/dashboard/web/pages/memories/sections/list.js +114 -0
- package/src/adapters/dashboard/web/pages/memories/sections/search.js +102 -0
- package/src/adapters/dashboard/web/pages/memories/sections/tools.js +155 -0
- package/src/adapters/dashboard/web/pages/memories/state.js +22 -0
- package/src/adapters/dashboard/web/pages/self/diff.js +79 -0
- package/src/adapters/dashboard/web/pages/self/index.js +43 -0
- package/src/adapters/dashboard/web/pages/self/markdown.js +61 -0
- package/src/adapters/dashboard/web/pages/self/sections/journal.js +32 -0
- package/src/adapters/dashboard/web/pages/self/sections/page.js +149 -0
- package/src/adapters/dashboard/web/pages/self/sections/settling.js +150 -0
- package/src/adapters/dashboard/web/pages/self/sections/stories.js +47 -0
- package/src/adapters/dashboard/web/pages/self/sections/wake.js +74 -0
- package/src/adapters/dashboard/web/pages/self/self.css +182 -0
- package/src/adapters/dashboard/web/server.ts +305 -28
- package/src/adapters/dashboard/web/shared/absence.js +14 -0
- package/src/adapters/dashboard/web/shared/actions.js +48 -0
- package/src/adapters/dashboard/web/shared/api.js +17 -0
- package/src/adapters/dashboard/web/shared/base.css +34 -0
- package/src/adapters/dashboard/web/shared/canvas.js +68 -0
- package/src/adapters/dashboard/web/shared/colors.js +6 -0
- package/src/adapters/dashboard/web/shared/dom.js +4 -0
- package/src/adapters/dashboard/web/shared/event-modal.js +25 -0
- package/src/adapters/dashboard/web/shared/fonts/OFL-courier-prime.txt +93 -0
- package/src/adapters/dashboard/web/shared/fonts/OFL-outfit.txt +93 -0
- package/src/adapters/dashboard/web/shared/fonts/courier-prime-latin-400.woff2 +0 -0
- package/src/adapters/dashboard/web/shared/fonts/courier-prime-latin-700.woff2 +0 -0
- package/src/adapters/dashboard/web/shared/fonts/outfit-latin-wght.woff2 +0 -0
- package/src/adapters/dashboard/web/shared/format.js +31 -0
- package/src/adapters/dashboard/web/shared/layout.css +32 -0
- package/src/adapters/dashboard/web/shared/memory-modal.js +124 -0
- package/src/adapters/dashboard/web/shared/modal.css +24 -0
- package/src/adapters/dashboard/web/shared/modal.js +22 -0
- package/src/adapters/dashboard/web/shared/state.js +12 -0
- package/src/adapters/dashboard/web/shared/tip.css +4 -0
- package/src/adapters/dashboard/web/shared/tip.js +12 -0
- package/src/adapters/dashboard/web/shared/tokens.css +50 -0
- package/src/adapters/dashboard/web/shared/type.css +52 -0
- package/src/adapters/dashboard/web/shared/utilities.css +5 -0
- package/src/adapters/dashboard/web/shared/vendor/MIT-three.txt +21 -0
- package/src/adapters/dashboard/web/shared/vendor/three.module.min.js +6 -0
- package/src/adapters/dashboard/web/shared/widgets/bar.css +9 -0
- package/src/adapters/dashboard/web/shared/widgets/card.css +6 -0
- package/src/adapters/dashboard/web/shared/widgets/chapters.js +15 -0
- package/src/adapters/dashboard/web/shared/widgets/chart.css +4 -0
- package/src/adapters/dashboard/web/shared/widgets/confirm.css +31 -0
- package/src/adapters/dashboard/web/shared/widgets/confirm.js +52 -0
- package/src/adapters/dashboard/web/shared/widgets/feed.css +13 -0
- package/src/adapters/dashboard/web/shared/widgets/feed.js +41 -0
- package/src/adapters/dashboard/web/shared/widgets/light.css +4 -0
- package/src/adapters/dashboard/web/shared/widgets/light.js +15 -0
- package/src/adapters/dashboard/web/shared/widgets/rows.css +7 -0
- package/src/adapters/dashboard/web/shared/widgets/table.css +17 -0
- package/src/adapters/dashboard/web/shared/widgets/tiles.css +14 -0
- package/src/adapters/dashboard/web/shell/pages.js +15 -0
- package/src/adapters/dashboard/web/shell/pulse.js +86 -0
- package/src/adapters/dashboard/web/shell/tabs.js +49 -0
- package/src/adapters/dashboard/web/static.ts +76 -0
- package/src/adapters/dashboard/web/views/activity.ts +80 -0
- package/src/adapters/dashboard/web/views/archive-words.ts +57 -0
- package/src/adapters/dashboard/web/views/flow-view.ts +217 -0
- package/src/adapters/dashboard/web/views/health.ts +328 -0
- package/src/adapters/dashboard/web/views/mechanism-panel.ts +370 -0
- package/src/adapters/dashboard/web/views/mechanisms.ts +302 -0
- package/src/adapters/dashboard/web/views/memories.ts +342 -0
- package/src/adapters/dashboard/web/views/memory.ts +210 -0
- package/src/adapters/dashboard/web/views/meta.ts +64 -0
- package/src/adapters/dashboard/web/views/mind.ts +729 -0
- package/src/adapters/dashboard/web/views/overview.ts +282 -0
- package/src/adapters/dashboard/web/views/pulse.ts +19 -0
- package/src/adapters/dashboard/web/views/rows.ts +207 -0
- package/src/adapters/dashboard/web/views/search.ts +62 -0
- package/src/adapters/dashboard/web/views/shared.ts +161 -0
- package/src/adapters/dashboard/web/views.ts +34 -1724
- package/src/adapters/fired.ts +138 -31
- package/src/adapters/mcp/INTERFACE-GAPS.md +4 -1
- package/src/adapters/mcp/NOTES.md +43 -0
- package/src/adapters/mcp/bin/serve.ts +9 -1
- package/src/adapters/mcp/index.ts +7 -0
- package/src/adapters/mcp/server.ts +167 -6
- package/src/adapters/mcp/tools.ts +42 -0
- package/src/adapters/mcp/write-up.ts +1 -1
- package/src/adapters/sessions.ts +18 -1
- package/src/adapters/snapshots.ts +71 -39
- package/src/core/associate/INTERFACE-GAPS.md +8 -0
- package/src/core/counterpart.ts +86 -7
- package/src/core/encode/INTERFACE-GAPS.md +12 -0
- package/src/core/feelings-wheel.ts +218 -0
- package/src/core/handoff/INTERFACE-GAPS.md +2 -0
- package/src/core/mint.ts +14 -2
- package/src/core/prospective/INTERFACE-GAPS.md +14 -0
- package/src/core/prospective/windows.ts +10 -48
- package/src/core/recall/INTERFACE-GAPS.md +6 -0
- package/src/core/remember/INTERFACE-GAPS.md +10 -0
- package/src/core/schemas/INTERFACE-GAPS.md +8 -0
- package/src/core/schemas/NOTES.md +118 -1
- package/src/core/schemas/index.ts +330 -40
- package/src/core/schemas/tunables.ts +23 -0
- package/src/core/schemas/types.ts +64 -1
- package/src/core/self/CONTRACT.md +3 -0
- package/src/core/self/INTERFACE-GAPS.md +10 -0
- package/src/core/self/NOTES.md +33 -2
- package/src/core/self/calendar.ts +7 -78
- package/src/core/self/episodes.ts +52 -12
- package/src/core/self/index.ts +40 -12
- package/src/core/self/writer.ts +22 -22
- package/src/core/sleep/NOTES.md +36 -0
- package/src/core/sleep/cycle.ts +38 -3
- package/src/core/sleep/fade.ts +76 -0
- package/src/core/sleep/index.ts +4 -1
- package/src/core/sleep/prune.ts +8 -1
- package/src/core/sleep/tunables.ts +5 -0
- package/src/core/sleep/types.ts +24 -0
- package/src/core/store/CONTRACT.md +36 -7
- package/src/core/store/NOTES.md +197 -0
- package/src/core/store/errors.ts +22 -0
- package/src/core/store/feelings.ts +182 -0
- package/src/core/store/index.ts +339 -45
- package/src/core/store/operational.ts +240 -22
- package/src/core/store/owner-op-seam.ts +13 -3
- package/src/core/store/paths.ts +15 -1
- package/src/core/store/pre-migration.ts +258 -0
- package/src/core/store/prose.ts +8 -1
- package/src/core/store/render.ts +3 -0
- package/src/core/time.ts +374 -0
- package/src/core/types.ts +13 -0
- package/src/adapters/dashboard/web/brain.html +0 -463
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/api/mechanism?id=<id>` — what the home page's mechanism panel shows for the
|
|
3
|
+
* picked mechanism: its last few firings, narrated, and a small picture of THIS
|
|
4
|
+
* store's real data where the site shows a demo.
|
|
5
|
+
*
|
|
6
|
+
* decay — the fade curves of a few real memories, from their last use
|
|
7
|
+
* retrieval — the last turns that brought memories to mind, and whether
|
|
8
|
+
* each one has been used since
|
|
9
|
+
* consolidation — memories climbing toward the core, and how far each has to go
|
|
10
|
+
* salience — recent memories and the score each was written with
|
|
11
|
+
* association — the graph hubs: what the most is wired to
|
|
12
|
+
* reconsolidation — recent corrections weighed against old memories
|
|
13
|
+
*
|
|
14
|
+
* A grey mechanism (not built) gets no picture and no activity: it has none.
|
|
15
|
+
* Which rows count as a firing is `MECHANISM_PROOFS`, in `mechanisms.ts`.
|
|
16
|
+
*
|
|
17
|
+
* Read-only, like everything in this directory. Memory words go through
|
|
18
|
+
* `reveal`, so a confidential row is withheld here exactly as everywhere else.
|
|
19
|
+
*/
|
|
20
|
+
import { TUNABLES, promotionEligibility, strength } from "../../../../core/physics/index.js";
|
|
21
|
+
import { isJournal } from "../../../../core/sleep/index.js";
|
|
22
|
+
import type { EventRow } from "../../../../core/store/index.js";
|
|
23
|
+
import type { Band, Kind } from "../../../../core/types.js";
|
|
24
|
+
import type { DashboardSource } from "../../source.js";
|
|
25
|
+
import { narrate } from "../narrate.js";
|
|
26
|
+
import type { NarratedEvent } from "../narrate.js";
|
|
27
|
+
import { reveal, revealHere } from "../reveal.js";
|
|
28
|
+
import { MECHANISM_PROOFS, amount, payloadOf } from "./mechanisms.js";
|
|
29
|
+
import { census } from "./shared.js";
|
|
30
|
+
import type { MemoryLine } from "./shared.js";
|
|
31
|
+
|
|
32
|
+
/** How many narrated firings the panel lists. */
|
|
33
|
+
export const PANEL_ACTIVITY = 4;
|
|
34
|
+
/** How far back, per event name, the panel looks for them. */
|
|
35
|
+
const ACTIVITY_LOOKBACK = 200;
|
|
36
|
+
/** How many lived days ahead a fade curve is drawn. */
|
|
37
|
+
export const FADE_AHEAD = 60;
|
|
38
|
+
/** How many lived days behind "now" a fade curve may start. */
|
|
39
|
+
export const FADE_BEHIND = 60;
|
|
40
|
+
|
|
41
|
+
interface Said {
|
|
42
|
+
readonly id: string;
|
|
43
|
+
readonly text: string;
|
|
44
|
+
readonly confidential: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function said(src: DashboardSource, id: string, width = 72): Said {
|
|
48
|
+
const r = reveal(src.store, id, width);
|
|
49
|
+
return { id, text: r.text ?? r.label, confidential: r.confidential };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function saidLine(m: MemoryLine): Said {
|
|
53
|
+
return { id: m.id, text: m.text, confidential: m.confidential };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface FadeCurve extends Said {
|
|
57
|
+
readonly kind: Kind;
|
|
58
|
+
readonly band: Band;
|
|
59
|
+
readonly lastUsedDay: number;
|
|
60
|
+
readonly now: number;
|
|
61
|
+
/** [lived day, strength] pairs, one per day, from the curve's start. */
|
|
62
|
+
readonly points: readonly (readonly [number, number])[];
|
|
63
|
+
/** The first lived day ahead on which it falls below the archive line, if unused. */
|
|
64
|
+
readonly archiveDay: number | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type Picture =
|
|
68
|
+
| {
|
|
69
|
+
readonly kind: "decay";
|
|
70
|
+
readonly day: number;
|
|
71
|
+
readonly from: number;
|
|
72
|
+
readonly to: number;
|
|
73
|
+
/** Below this a memory drops out of the semantic band. */
|
|
74
|
+
readonly semanticFloor: number;
|
|
75
|
+
/** Below this a memory can be archived. */
|
|
76
|
+
readonly archiveLine: number;
|
|
77
|
+
readonly curves: readonly FadeCurve[];
|
|
78
|
+
}
|
|
79
|
+
| {
|
|
80
|
+
readonly kind: "retrieval";
|
|
81
|
+
readonly turns: readonly {
|
|
82
|
+
readonly seq: number;
|
|
83
|
+
readonly day: number;
|
|
84
|
+
readonly turn: number;
|
|
85
|
+
readonly memories: readonly (Said & { readonly said: boolean; readonly usedSince: boolean })[];
|
|
86
|
+
}[];
|
|
87
|
+
}
|
|
88
|
+
| {
|
|
89
|
+
readonly kind: "consolidation";
|
|
90
|
+
readonly threshold: number;
|
|
91
|
+
readonly requiredDays: number;
|
|
92
|
+
readonly climbing: readonly (Said & { readonly base: number; readonly days: number })[];
|
|
93
|
+
readonly promoted: readonly (Said & { readonly day: number })[];
|
|
94
|
+
readonly core: number;
|
|
95
|
+
}
|
|
96
|
+
| {
|
|
97
|
+
readonly kind: "salience";
|
|
98
|
+
readonly memories: readonly (Said & { readonly salience: number; readonly bornDay: number; readonly memKind: Kind })[];
|
|
99
|
+
}
|
|
100
|
+
| {
|
|
101
|
+
readonly kind: "association";
|
|
102
|
+
readonly hubs: readonly (Said & { readonly weight: number; readonly degree: number })[];
|
|
103
|
+
readonly links: number;
|
|
104
|
+
}
|
|
105
|
+
| {
|
|
106
|
+
readonly kind: "reconsolidation";
|
|
107
|
+
readonly revisions: readonly {
|
|
108
|
+
readonly seq: number;
|
|
109
|
+
readonly day: number;
|
|
110
|
+
readonly target: Said;
|
|
111
|
+
readonly challenger: Said;
|
|
112
|
+
readonly pressure: number;
|
|
113
|
+
readonly bar: number;
|
|
114
|
+
readonly crossed: boolean;
|
|
115
|
+
}[];
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export interface MechanismPanelView {
|
|
119
|
+
readonly id: string;
|
|
120
|
+
readonly found: boolean;
|
|
121
|
+
readonly built: boolean;
|
|
122
|
+
readonly livedDay: number;
|
|
123
|
+
/** The newest few rows that count as this mechanism firing, narrated. */
|
|
124
|
+
readonly activity: readonly NarratedEvent[];
|
|
125
|
+
/** Null for a grey mechanism, and for a built one with no picture of its own. */
|
|
126
|
+
readonly picture: Picture | null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function mechanismPanel(src: DashboardSource, id: string): MechanismPanelView {
|
|
130
|
+
const store = src.store;
|
|
131
|
+
const livedDay = store.livedDay();
|
|
132
|
+
const proof = MECHANISM_PROOFS.find((m) => m.id === id);
|
|
133
|
+
if (proof === undefined) return { id, found: false, built: false, livedDay, activity: [], picture: null };
|
|
134
|
+
if (!proof.built) return { id, found: true, built: false, livedDay, activity: [], picture: null };
|
|
135
|
+
|
|
136
|
+
const backing: EventRow[] = [];
|
|
137
|
+
for (const p of proof.proofs) {
|
|
138
|
+
for (const row of store.eventLog({ name: p.event, order: "desc", limit: ACTIVITY_LOOKBACK })) {
|
|
139
|
+
if (amount(p, payloadOf(row)) > 0) backing.push(row);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const seen = new Set<number>();
|
|
143
|
+
const activity = backing
|
|
144
|
+
.sort((a, b) => b.seq - a.seq)
|
|
145
|
+
.filter((r) => (seen.has(r.seq) ? false : (seen.add(r.seq), true)))
|
|
146
|
+
.slice(0, PANEL_ACTIVITY)
|
|
147
|
+
.map((r) => narrate(store, r));
|
|
148
|
+
|
|
149
|
+
return { id, found: true, built: true, livedDay, activity, picture: pictureOf(src, id, livedDay) };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function pictureOf(src: DashboardSource, id: string, day: number): Picture | null {
|
|
153
|
+
switch (id) {
|
|
154
|
+
case "decay":
|
|
155
|
+
return decayPicture(src, day);
|
|
156
|
+
case "retrieval":
|
|
157
|
+
return retrievalPicture(src);
|
|
158
|
+
case "consolidation":
|
|
159
|
+
return consolidationPicture(src);
|
|
160
|
+
case "salience":
|
|
161
|
+
return saliencePicture(src);
|
|
162
|
+
case "association":
|
|
163
|
+
return associationPicture(src);
|
|
164
|
+
case "reconsolidation":
|
|
165
|
+
return reconsolidationPicture(src);
|
|
166
|
+
default:
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ── forgetting ──────────────────────────────────────────────────────────────
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Up to four real memories, spread across how strong they are today — the
|
|
175
|
+
* strongest one that can still fade, the faintest, and two between — each drawn
|
|
176
|
+
* from its last use to sixty lived days ahead, as the physics computes it. The
|
|
177
|
+
* core identity band does not fade, so it is left out rather than drawn flat.
|
|
178
|
+
*/
|
|
179
|
+
function decayPicture(src: DashboardSource, day: number): Picture {
|
|
180
|
+
const store = src.store;
|
|
181
|
+
const fading = census(src).filter((m) => !m.schema && !m.promoted && !m.unreadable);
|
|
182
|
+
const picks: MemoryLine[] = [];
|
|
183
|
+
if (fading.length <= 4) picks.push(...fading);
|
|
184
|
+
else {
|
|
185
|
+
for (const q of [0, 0.35, 0.7, 1]) {
|
|
186
|
+
const m = fading[Math.round(q * (fading.length - 1))];
|
|
187
|
+
if (m !== undefined && !picks.includes(m)) picks.push(m);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const to = day + FADE_AHEAD;
|
|
191
|
+
let from = day;
|
|
192
|
+
const curves: FadeCurve[] = picks.map((m) => {
|
|
193
|
+
const physics = store.physicsOf(m.id);
|
|
194
|
+
const start = Math.max(physics.lastUsedDay, day - FADE_BEHIND);
|
|
195
|
+
from = Math.min(from, start);
|
|
196
|
+
const points: [number, number][] = [];
|
|
197
|
+
let archiveDay: number | null = null;
|
|
198
|
+
for (let d = start; d <= to; d++) {
|
|
199
|
+
const s = strength(physics, d);
|
|
200
|
+
points.push([d, round(s)]);
|
|
201
|
+
if (archiveDay === null && d > day && s < TUNABLES.PHI_PRUNE) archiveDay = d;
|
|
202
|
+
}
|
|
203
|
+
return { ...saidLine(m), kind: m.kind, band: m.band, lastUsedDay: physics.lastUsedDay, now: round(m.strength), points, archiveDay };
|
|
204
|
+
});
|
|
205
|
+
return {
|
|
206
|
+
kind: "decay",
|
|
207
|
+
day,
|
|
208
|
+
from,
|
|
209
|
+
to,
|
|
210
|
+
semanticFloor: TUNABLES.THETA_SEM,
|
|
211
|
+
archiveLine: TUNABLES.PHI_PRUNE,
|
|
212
|
+
curves,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ── retrieval ───────────────────────────────────────────────────────────────
|
|
217
|
+
|
|
218
|
+
function idsOf(v: unknown): string[] {
|
|
219
|
+
if (!Array.isArray(v)) return [];
|
|
220
|
+
const out: string[] = [];
|
|
221
|
+
for (const item of v) {
|
|
222
|
+
if (typeof item === "string") out.push(item);
|
|
223
|
+
else if (item !== null && typeof item === "object" && typeof (item as { id?: unknown }).id === "string") {
|
|
224
|
+
out.push((item as { id: string }).id);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The last few turns that brought anything to mind: what was said out loud and
|
|
232
|
+
* what was kept as a footnote, and whether each memory has been USED since —
|
|
233
|
+
* read off the memory's own physics (its last credited use is on or after that
|
|
234
|
+
* turn's day, and after its birth), not guessed from the turn.
|
|
235
|
+
*/
|
|
236
|
+
function retrievalPicture(src: DashboardSource): Picture {
|
|
237
|
+
const store = src.store;
|
|
238
|
+
const turns: Extract<Picture, { kind: "retrieval" }>["turns"][number][] = [];
|
|
239
|
+
for (const row of store.eventLog({ name: "recall.decision", order: "desc", limit: ACTIVITY_LOOKBACK })) {
|
|
240
|
+
const p = payloadOf(row);
|
|
241
|
+
const surfaced = idsOf(p["surfaced"]);
|
|
242
|
+
const footnotes = idsOf(p["footnotes"]);
|
|
243
|
+
if (surfaced.length + footnotes.length === 0) continue;
|
|
244
|
+
const memories = [
|
|
245
|
+
...surfaced.map((id) => ({ id, said: true })),
|
|
246
|
+
...footnotes.map((id) => ({ id, said: false })),
|
|
247
|
+
].slice(0, 6).map(({ id, said: aloud }) => {
|
|
248
|
+
let usedSince = false;
|
|
249
|
+
try {
|
|
250
|
+
const physics = store.physicsOf(id);
|
|
251
|
+
usedSince = physics.lastUsedDay >= row.day && physics.lastUsedDay > physics.birthDay;
|
|
252
|
+
} catch {
|
|
253
|
+
usedSince = false;
|
|
254
|
+
}
|
|
255
|
+
return { ...said(src, id, 64), said: aloud, usedSince };
|
|
256
|
+
});
|
|
257
|
+
const turn = typeof p["turn"] === "number" ? p["turn"] : 0;
|
|
258
|
+
turns.push({ seq: row.seq, day: row.day, turn, memories });
|
|
259
|
+
if (turns.length >= 3) break;
|
|
260
|
+
}
|
|
261
|
+
return { kind: "retrieval", turns };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ── consolidation ───────────────────────────────────────────────────────────
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Becoming core takes two things at once: a strong enough base, and real use on
|
|
268
|
+
* three separate lived days. Each climber shows both, against the bar; the
|
|
269
|
+
* nearest six are listed, with the last few that made it.
|
|
270
|
+
*/
|
|
271
|
+
function consolidationPicture(src: DashboardSource): Picture {
|
|
272
|
+
const store = src.store;
|
|
273
|
+
const rows = census(src).filter((m) => !m.schema && !m.unreadable);
|
|
274
|
+
const core = rows.filter((m) => m.promoted).length;
|
|
275
|
+
const climbing = rows
|
|
276
|
+
.filter((m) => !m.promoted)
|
|
277
|
+
.map((m) => {
|
|
278
|
+
const v = promotionEligibility(store.physicsOf(m.id));
|
|
279
|
+
const progress = Math.min(1, v.base / v.threshold) + Math.min(1, v.reinforcedDays / v.requiredDays);
|
|
280
|
+
return { m, base: v.base, days: v.reinforcedDays, progress };
|
|
281
|
+
})
|
|
282
|
+
.sort((a, b) => b.progress - a.progress || (a.m.id < b.m.id ? -1 : 1))
|
|
283
|
+
.slice(0, 6)
|
|
284
|
+
.map((c) => ({ ...saidLine(c.m), base: round(c.base), days: c.days }));
|
|
285
|
+
const promoted = store
|
|
286
|
+
.eventLog({ name: "band.promoted", order: "desc", limit: 3 })
|
|
287
|
+
.filter((r) => r.ref !== null)
|
|
288
|
+
.map((r) => ({ ...said(src, r.ref as string), day: r.day }));
|
|
289
|
+
return {
|
|
290
|
+
kind: "consolidation",
|
|
291
|
+
threshold: TUNABLES.THETA_ID,
|
|
292
|
+
requiredDays: TUNABLES.N_PROMOTION_DAYS,
|
|
293
|
+
climbing,
|
|
294
|
+
promoted,
|
|
295
|
+
core,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// ── salience ────────────────────────────────────────────────────────────────
|
|
300
|
+
|
|
301
|
+
/** The most recent memories, newest first, with the score each was written with. */
|
|
302
|
+
function saliencePicture(src: DashboardSource): Picture {
|
|
303
|
+
const memories = census(src)
|
|
304
|
+
.filter((m) => !m.schema && !m.unreadable)
|
|
305
|
+
.sort((a, b) => b.bornDay - a.bornDay || (a.id < b.id ? -1 : 1))
|
|
306
|
+
.slice(0, 8)
|
|
307
|
+
.map((m) => ({ ...saidLine(m), salience: round(m.salience), bornDay: m.bornDay, memKind: m.kind }));
|
|
308
|
+
return { kind: "salience", memories };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ── association ─────────────────────────────────────────────────────────────
|
|
312
|
+
|
|
313
|
+
/** The graph hubs: what the most is wired to, by summed link weight. */
|
|
314
|
+
function associationPicture(src: DashboardSource): Picture {
|
|
315
|
+
const store = src.store;
|
|
316
|
+
const weight = new Map<string, { w: number; d: number }>();
|
|
317
|
+
let links = 0;
|
|
318
|
+
for (const id of store.list({ archived: false })) {
|
|
319
|
+
const row = store.row(id);
|
|
320
|
+
if (row === undefined || isJournal(row)) continue;
|
|
321
|
+
for (const edge of store.edgesFrom(id)) {
|
|
322
|
+
links += 1;
|
|
323
|
+
for (const end of [id, edge.dst]) {
|
|
324
|
+
const cur = weight.get(end) ?? { w: 0, d: 0 };
|
|
325
|
+
cur.w += edge.weight;
|
|
326
|
+
cur.d += 1;
|
|
327
|
+
weight.set(end, cur);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const hubs = [...weight.entries()]
|
|
332
|
+
.sort((a, b) => b[1].w - a[1].w || (a[0] < b[0] ? -1 : 1))
|
|
333
|
+
.slice(0, 6)
|
|
334
|
+
.map(([id, v]) => ({ ...said(src, id), weight: round(v.w), degree: v.d }));
|
|
335
|
+
return { kind: "association", hubs, links };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ── reconsolidation ─────────────────────────────────────────────────────────
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* The last few corrections weighed against an old memory: which memory argued,
|
|
342
|
+
* with which one, and how far the pressure is toward the bar it would take to
|
|
343
|
+
* change it. The target is read AS IT STOOD (unfollowed), like the feed does.
|
|
344
|
+
*/
|
|
345
|
+
function reconsolidationPicture(src: DashboardSource): Picture {
|
|
346
|
+
const store = src.store;
|
|
347
|
+
const revisions = store.eventLog({ name: "revision.pressure", order: "desc", limit: 5 }).map((row) => {
|
|
348
|
+
const p = payloadOf(row);
|
|
349
|
+
const targetId = typeof p["targetId"] === "string" ? p["targetId"] : (row.ref ?? "");
|
|
350
|
+
const challengerId = typeof p["challengerId"] === "string" ? p["challengerId"] : "";
|
|
351
|
+
const here = revealHere(store, targetId, 72);
|
|
352
|
+
const pressure = typeof p["pressureAfter"] === "number" ? p["pressureAfter"] : 0;
|
|
353
|
+
const bar = typeof p["bar"] === "number" ? p["bar"] : 0;
|
|
354
|
+
return {
|
|
355
|
+
seq: row.seq,
|
|
356
|
+
day: row.day,
|
|
357
|
+
target: { id: targetId, text: here.text ?? here.label, confidential: here.confidential },
|
|
358
|
+
challenger: said(src, challengerId),
|
|
359
|
+
pressure: round(pressure),
|
|
360
|
+
bar: round(bar),
|
|
361
|
+
crossed: bar > 0 && pressure >= bar,
|
|
362
|
+
};
|
|
363
|
+
});
|
|
364
|
+
return { kind: "reconsolidation", revisions };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function round(x: number): number {
|
|
368
|
+
return Number.isFinite(x) ? Math.round(x * 1000) / 1000 : 0;
|
|
369
|
+
}
|
|
370
|
+
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/api/mechanisms` — the eleven memory mechanisms the site names, each with a
|
|
3
|
+
* light: grey (not built), green (built, and fired in the last 7 LIVED days),
|
|
4
|
+
* amber (built, and not firing).
|
|
5
|
+
*
|
|
6
|
+
* The list, its order and its four families are the site's
|
|
7
|
+
* (`counterparts-site/features/home-v2/content/regions.ts`, minus the two it
|
|
8
|
+
* parks). What each one says about Counterparts lives beside the page, in
|
|
9
|
+
* `web/mechanisms/<id>/index.js`; this file owns only the one question the page
|
|
10
|
+
* cannot answer for itself: did it fire, and which rows say so.
|
|
11
|
+
*
|
|
12
|
+
* BUILT OR NOT comes from `docs/research/mechanism-audit-2026-09-24.md`, not
|
|
13
|
+
* from the site's "in development" badge — the two disagree on consolidation
|
|
14
|
+
* (site: in development; audit: built, narrow) and association (site: in
|
|
15
|
+
* development; audit: built but starved). The light answers "does the code
|
|
16
|
+
* run", so it takes the audit's word; the badge stays the site's, on the
|
|
17
|
+
* client module.
|
|
18
|
+
*
|
|
19
|
+
* Read-only, like everything in this directory: `eventLog` reads and nothing
|
|
20
|
+
* else. Rules 1–4 of `views.ts` apply; no row text is emitted, only counts and
|
|
21
|
+
* event `seq`s the page can open with `/api/event`.
|
|
22
|
+
*/
|
|
23
|
+
import type { EventRow } from "../../../../core/store/index.js";
|
|
24
|
+
import type { DurableEventName } from "../../registries.js";
|
|
25
|
+
import type { DashboardSource } from "../../source.js";
|
|
26
|
+
|
|
27
|
+
/** The window: today's lived day and the six before it. */
|
|
28
|
+
export const MECHANISM_DAYS = 7;
|
|
29
|
+
/** How many backing event seqs each mechanism hands the page. */
|
|
30
|
+
export const RECENT_IDS = 3;
|
|
31
|
+
/** Ceiling on one name's read inside the window — reported, never silent. */
|
|
32
|
+
const WINDOW_CEILING = 50_000;
|
|
33
|
+
/** How far back an amber row looks for "last seen", newest first. */
|
|
34
|
+
const LOOKBACK = 500;
|
|
35
|
+
|
|
36
|
+
export type Family = "encoding" | "storage" | "retrieval" | "transformation";
|
|
37
|
+
export const FAMILIES: readonly Family[] = ["encoding", "storage", "retrieval", "transformation"];
|
|
38
|
+
|
|
39
|
+
export type Payload = Record<string, unknown>;
|
|
40
|
+
|
|
41
|
+
/** One kind of row that proves a mechanism fired. */
|
|
42
|
+
export interface Proof {
|
|
43
|
+
readonly event: DurableEventName;
|
|
44
|
+
/** What a unit of it is, after the number — [one, many]:
|
|
45
|
+
* ["memory archived at the floor", "memories archived at the floor"]. */
|
|
46
|
+
readonly says: readonly [string, string];
|
|
47
|
+
/** Sum this numeric payload field instead of counting rows; a row whose
|
|
48
|
+
* field is not a number above zero does not count at all (the `positive`
|
|
49
|
+
* rule of `adapters/fired.ts`: a row that lands whether or not the
|
|
50
|
+
* mechanism did anything is not a firing). */
|
|
51
|
+
readonly sum?: string;
|
|
52
|
+
/** Only rows this accepts count. */
|
|
53
|
+
readonly where?: (p: Payload) => boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface MechanismProof {
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly family: Family;
|
|
59
|
+
/** Per the audit. A mechanism that is not built is grey and never counted. */
|
|
60
|
+
readonly built: boolean;
|
|
61
|
+
/** Empty when not built. */
|
|
62
|
+
readonly proofs: readonly Proof[];
|
|
63
|
+
/** For a grey light: the one plain line it shows instead of evidence. */
|
|
64
|
+
readonly grey?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const num = (v: unknown): number => (typeof v === "number" && Number.isFinite(v) ? v : 0);
|
|
68
|
+
|
|
69
|
+
// ── THE TABLE ───────────────────────────────────────────────────────────────
|
|
70
|
+
// One row per mechanism, in the site's order. Read it top to bottom: what
|
|
71
|
+
// counts as the mechanism firing, in the rows the store already keeps.
|
|
72
|
+
export const MECHANISM_PROOFS: readonly MechanismProof[] = [
|
|
73
|
+
// ── Encoding ──
|
|
74
|
+
{
|
|
75
|
+
// Salience writes no row of its own: the score is columns on the memory.
|
|
76
|
+
// Every memory is scored at the gate, so the gate's row IS the scoring.
|
|
77
|
+
id: "salience",
|
|
78
|
+
family: "encoding",
|
|
79
|
+
built: true,
|
|
80
|
+
proofs: [
|
|
81
|
+
{ event: "gate.deposit", sum: "accepted", says: ["memory scored as it was written", "memories scored as they were written"] },
|
|
82
|
+
{ event: "gate.chunk", sum: "accepted", says: ["memory scored from a crash write-up", "memories scored from a crash write-up"] },
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
// The quoted feeling is stored as a label with no weight, the classifier
|
|
87
|
+
// ships off, and the numeric emotional score rides the salience columns —
|
|
88
|
+
// there is no row that could say "feeling changed this memory".
|
|
89
|
+
id: "emotional",
|
|
90
|
+
family: "encoding",
|
|
91
|
+
built: false,
|
|
92
|
+
proofs: [],
|
|
93
|
+
grey: "In development: a feeling is stored as a label but does not yet weigh anything.",
|
|
94
|
+
},
|
|
95
|
+
// ── Storage ──
|
|
96
|
+
{
|
|
97
|
+
id: "decay",
|
|
98
|
+
family: "storage",
|
|
99
|
+
built: true,
|
|
100
|
+
proofs: [
|
|
101
|
+
// The decay site also records the CROSSING into identity (direction
|
|
102
|
+
// up), which is not a fade; only a move down counts here.
|
|
103
|
+
{ event: "band.transition", where: (p) => p["site"] === "decay" && p["direction"] !== "up", says: ["memory faded a band", "memories faded a band"] },
|
|
104
|
+
{ event: "memory.pruned", says: ["memory archived at the floor", "memories archived at the floor"] },
|
|
105
|
+
// The entity-card fade has no row of its own; it is a count on the
|
|
106
|
+
// nightly cycle row, so only nights that faded something count.
|
|
107
|
+
{ event: "sleep.cycle", sum: "faded", says: ["unused card faded", "unused cards faded"] },
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
// Only byte-identical bodies merge (that is consolidation's, below); two
|
|
112
|
+
// near-duplicates are left alone on purpose. Nothing competes.
|
|
113
|
+
id: "interference",
|
|
114
|
+
family: "storage",
|
|
115
|
+
built: false,
|
|
116
|
+
proofs: [],
|
|
117
|
+
grey: "In development: similar memories do not compete yet.",
|
|
118
|
+
},
|
|
119
|
+
// ── Retrieval ──
|
|
120
|
+
{
|
|
121
|
+
id: "retrieval",
|
|
122
|
+
family: "retrieval",
|
|
123
|
+
built: true,
|
|
124
|
+
proofs: [
|
|
125
|
+
{
|
|
126
|
+
event: "recall.decision",
|
|
127
|
+
where: (p) => num(p["surfacedCount"]) + num(p["footnoteCount"]) > 0,
|
|
128
|
+
says: ["turn brought memories to mind", "turns brought memories to mind"],
|
|
129
|
+
},
|
|
130
|
+
{ event: "mcp.recall", says: ["deliberate look-up", "deliberate look-ups"] },
|
|
131
|
+
{ event: "recall.credit", sum: "credited", says: ["memory strengthened by being used", "memories strengthened by being used"] },
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
// Built but starved (audit): links form only between memories used in
|
|
136
|
+
// the same turn, and a hop only boosts what the turn already reached. A
|
|
137
|
+
// flush that wrote no link rows is not a firing.
|
|
138
|
+
id: "association",
|
|
139
|
+
family: "retrieval",
|
|
140
|
+
built: true,
|
|
141
|
+
proofs: [{ event: "associate.flush", sum: "rows", says: ["link written", "links written"] }],
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
// `prospective.fire` exists and only the demo seeder calls `fire()`: no
|
|
145
|
+
// live path spends the budget, so the rows prove nothing about real use.
|
|
146
|
+
id: "prospective",
|
|
147
|
+
family: "retrieval",
|
|
148
|
+
built: false,
|
|
149
|
+
proofs: [],
|
|
150
|
+
grey: "In development: dated reminders can be stored, but nothing brings them back on the day yet.",
|
|
151
|
+
},
|
|
152
|
+
// ── Transformation ──
|
|
153
|
+
{
|
|
154
|
+
// Narrow (audit): only exact duplicates merge, promotion needs a high score
|
|
155
|
+
// and use on three separate days, and it runs every three lived days.
|
|
156
|
+
// The cycle row itself is NOT a proof: it lands every night regardless.
|
|
157
|
+
id: "consolidation",
|
|
158
|
+
family: "transformation",
|
|
159
|
+
built: true,
|
|
160
|
+
proofs: [
|
|
161
|
+
{ event: "band.promoted", says: ["memory became core", "memories became core"] },
|
|
162
|
+
{ event: "memory.merged", says: ["exact duplicate merged", "exact duplicates merged"] },
|
|
163
|
+
{ event: "band.transition", where: (p) => p["site"] === "consolidate", says: ["memory rose a band", "memories rose a band"] },
|
|
164
|
+
],
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
// Partial (audit): it revises only what a writer declared with `updates:`,
|
|
168
|
+
// for beliefs and identity lines, under pressure over several days. The
|
|
169
|
+
// supersede itself (`memory.superseded`) is not durable; the pressure row is.
|
|
170
|
+
id: "reconsolidation",
|
|
171
|
+
family: "transformation",
|
|
172
|
+
built: true,
|
|
173
|
+
proofs: [{ event: "revision.pressure", says: ["correction weighed against an old memory", "corrections weighed against old memories"] }],
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
// A band label only; nothing distils episodes into knowledge.
|
|
177
|
+
id: "episodic-semantic",
|
|
178
|
+
family: "transformation",
|
|
179
|
+
built: false,
|
|
180
|
+
proofs: [],
|
|
181
|
+
grey: "In development: sessions do not turn into general knowledge yet.",
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
// Beliefs have no live producer (`addBelief` is called only by the demo
|
|
185
|
+
// seeder). `revision.pressure` is shared with reconsolidation above and is
|
|
186
|
+
// counted there, not here.
|
|
187
|
+
id: "schema",
|
|
188
|
+
family: "transformation",
|
|
189
|
+
built: false,
|
|
190
|
+
proofs: [],
|
|
191
|
+
grey: "In development: beliefs about people and projects are not formed yet.",
|
|
192
|
+
},
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
export type MechanismStatus = "grey" | "green" | "amber";
|
|
196
|
+
|
|
197
|
+
export interface MechanismLight {
|
|
198
|
+
readonly id: string;
|
|
199
|
+
readonly family: Family;
|
|
200
|
+
readonly status: MechanismStatus;
|
|
201
|
+
/** One plain-English line. */
|
|
202
|
+
readonly evidence: string;
|
|
203
|
+
/** The newest few event `seq`s that back a green light. Empty otherwise. */
|
|
204
|
+
readonly events: readonly number[];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface MechanismsView {
|
|
208
|
+
readonly livedDay: number;
|
|
209
|
+
/** The first lived day inside the window. */
|
|
210
|
+
readonly fromDay: number;
|
|
211
|
+
readonly days: number;
|
|
212
|
+
readonly mechanisms: readonly MechanismLight[];
|
|
213
|
+
/** A read hit its ceiling, so some counts are floors. */
|
|
214
|
+
readonly truncated: boolean;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function payloadOf(row: EventRow): Payload {
|
|
218
|
+
if (row.payload === null) return {};
|
|
219
|
+
try {
|
|
220
|
+
const v: unknown = JSON.parse(row.payload);
|
|
221
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Payload) : {};
|
|
222
|
+
} catch {
|
|
223
|
+
return {};
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The amount this row contributes, or 0 when it does not count. */
|
|
228
|
+
export function amount(proof: Proof, p: Payload): number {
|
|
229
|
+
if (proof.where !== undefined && !proof.where(p)) return 0;
|
|
230
|
+
if (proof.sum === undefined) return 1;
|
|
231
|
+
const v = num(p[proof.sum]);
|
|
232
|
+
return v > 0 ? v : 0;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function mechanismsView(src: DashboardSource): MechanismsView {
|
|
236
|
+
const store = src.store;
|
|
237
|
+
let livedDay = 0;
|
|
238
|
+
try {
|
|
239
|
+
livedDay = store.livedDay();
|
|
240
|
+
} catch {
|
|
241
|
+
livedDay = 0;
|
|
242
|
+
}
|
|
243
|
+
const fromDay = Math.max(0, livedDay - (MECHANISM_DAYS - 1));
|
|
244
|
+
let truncated = false;
|
|
245
|
+
// One read per event name, shared by every proof that names it.
|
|
246
|
+
const windowRows = new Map<string, EventRow[]>();
|
|
247
|
+
const rowsFor = (name: string): EventRow[] => {
|
|
248
|
+
let rows = windowRows.get(name);
|
|
249
|
+
if (rows === undefined) {
|
|
250
|
+
rows = store.eventLog({ name, sinceDay: fromDay, limit: WINDOW_CEILING, order: "desc" });
|
|
251
|
+
if (rows.length >= WINDOW_CEILING) truncated = true;
|
|
252
|
+
windowRows.set(name, rows);
|
|
253
|
+
}
|
|
254
|
+
return rows;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const mechanisms = MECHANISM_PROOFS.map((m): MechanismLight => {
|
|
258
|
+
if (!m.built) {
|
|
259
|
+
return { id: m.id, family: m.family, status: "grey", evidence: m.grey ?? "Not built yet.", events: [] };
|
|
260
|
+
}
|
|
261
|
+
const parts: string[] = [];
|
|
262
|
+
const backing: EventRow[] = [];
|
|
263
|
+
for (const proof of m.proofs) {
|
|
264
|
+
let total = 0;
|
|
265
|
+
for (const row of rowsFor(proof.event)) {
|
|
266
|
+
const n = amount(proof, payloadOf(row));
|
|
267
|
+
if (n > 0) {
|
|
268
|
+
total += n;
|
|
269
|
+
backing.push(row);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (total > 0) parts.push(`${total} ${total === 1 ? proof.says[0] : proof.says[1]}`);
|
|
273
|
+
}
|
|
274
|
+
if (parts.length > 0) {
|
|
275
|
+
const events = backing
|
|
276
|
+
.sort((a, b) => b.seq - a.seq)
|
|
277
|
+
.slice(0, RECENT_IDS)
|
|
278
|
+
.map((r) => r.seq);
|
|
279
|
+
return { id: m.id, family: m.family, status: "green", evidence: `${parts.join(", ")} in the last ${MECHANISM_DAYS} lived days.`, events };
|
|
280
|
+
}
|
|
281
|
+
// Built, and nothing inside the window: when was it last seen at all? Only
|
|
282
|
+
// the newest LOOKBACK rows of each name are searched, so a name with that
|
|
283
|
+
// many non-counting rows since its last real firing reads "no record" —
|
|
284
|
+
// acceptable for a scaffold; a real "last fired" wants its own query.
|
|
285
|
+
let lastDay: number | null = null;
|
|
286
|
+
for (const proof of m.proofs) {
|
|
287
|
+
for (const row of store.eventLog({ name: proof.event, order: "desc", limit: LOOKBACK })) {
|
|
288
|
+
if (amount(proof, payloadOf(row)) > 0) {
|
|
289
|
+
if (lastDay === null || row.day > lastDay) lastDay = row.day;
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const evidence =
|
|
295
|
+
lastDay === null
|
|
296
|
+
? `Built, and no record of it firing yet.`
|
|
297
|
+
: `Built, but quiet for ${MECHANISM_DAYS} lived days (last fired on lived day ${lastDay}).`;
|
|
298
|
+
return { id: m.id, family: m.family, status: "amber", evidence, events: [] };
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
return { livedDay, fromDay, days: MECHANISM_DAYS, mechanisms, truncated };
|
|
302
|
+
}
|