counterparts 0.3.1 → 0.3.3
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 +121 -1
- package/docs/module-map.md +2 -0
- package/package.json +1 -1
- package/src/adapters/claude-code/NOTES.md +44 -0
- package/src/adapters/claude-code/bin/hook.ts +100 -24
- package/src/adapters/claude-code/bin/runner.ts +3 -1
- package/src/adapters/claude-code/config.ts +30 -0
- package/src/adapters/claude-code/doctor.ts +59 -24
- package/src/adapters/claude-code/hooks.ts +185 -14
- package/src/adapters/claude-code/index.ts +4 -0
- package/src/adapters/cli/commands.ts +189 -59
- 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 +320 -0
- package/src/adapters/cli/repair-dates.ts +3 -1
- package/src/adapters/dashboard/CONTRACT.md +13 -9
- 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/registries.ts +4 -0
- package/src/adapters/dashboard/upgrade.ts +23 -0
- package/src/adapters/dashboard/web/README.md +161 -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/flow.ts +2 -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 +19 -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 +18 -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 +18 -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 +188 -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 +82 -0
- package/src/adapters/dashboard/web/pages/self/markdown.js +61 -0
- package/src/adapters/dashboard/web/pages/self/sections/journal.js +88 -0
- package/src/adapters/dashboard/web/pages/self/sections/page.js +192 -0
- package/src/adapters/dashboard/web/pages/self/sections/settling.js +145 -0
- package/src/adapters/dashboard/web/pages/self/sections/stories.js +47 -0
- package/src/adapters/dashboard/web/pages/self/sections/wake.js +90 -0
- package/src/adapters/dashboard/web/pages/self/self.css +242 -0
- package/src/adapters/dashboard/web/pages/self/state.js +23 -0
- package/src/adapters/dashboard/web/pages/self/tips.js +50 -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 +133 -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 +373 -0
- package/src/adapters/dashboard/web/views/memories.ts +342 -0
- package/src/adapters/dashboard/web/views/memory.ts +224 -0
- package/src/adapters/dashboard/web/views/meta.ts +64 -0
- package/src/adapters/dashboard/web/views/mind.ts +853 -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/feelings-line.ts +74 -0
- package/src/adapters/fired.ts +106 -35
- package/src/adapters/mcp/CONTRACT.md +8 -0
- package/src/adapters/mcp/NOTES.md +88 -0
- package/src/adapters/mcp/bin/serve.ts +5 -1
- package/src/adapters/mcp/index.ts +3 -0
- package/src/adapters/mcp/server.ts +322 -6
- package/src/adapters/mcp/tools.ts +89 -0
- package/src/adapters/mcp/write-up.ts +1 -1
- package/src/core/bridge.ts +21 -16
- package/src/core/counterpart.ts +337 -5
- package/src/core/encode/CONTRACT.md +7 -1
- package/src/core/feelings-wheel.ts +376 -0
- package/src/core/mint.ts +23 -2
- package/src/core/physics/CONTRACT.md +43 -0
- package/src/core/physics/NOTES.md +94 -0
- package/src/core/physics/index.ts +98 -6
- package/src/core/prospective/CONTRACT.md +35 -6
- package/src/core/prospective/INTERFACE-GAPS.md +16 -1
- package/src/core/prospective/NOTES.md +175 -4
- package/src/core/prospective/derive.ts +32 -3
- package/src/core/prospective/index.ts +362 -39
- package/src/core/prospective/tunables.ts +18 -1
- package/src/core/prospective/windows.ts +110 -90
- package/src/core/recall/CONTRACT.md +28 -0
- package/src/core/recall/NOTES.md +39 -0
- package/src/core/recall/activate.ts +32 -5
- package/src/core/recall/gate.ts +7 -1
- package/src/core/recall/index.ts +19 -0
- package/src/core/recall/mood.ts +106 -0
- package/src/core/recall/tunables.ts +26 -0
- package/src/core/remember/index.ts +1 -0
- package/src/core/remember/proposals.ts +86 -3
- package/src/core/self/NOTES.md +17 -0
- package/src/core/self/calendar.ts +7 -78
- package/src/core/self/episodes.ts +7 -8
- package/src/core/self/identity.ts +20 -1
- package/src/core/self/index.ts +32 -11
- package/src/core/self/writer.ts +22 -22
- package/src/core/sleep/cycle.ts +8 -2
- package/src/core/store/CONTRACT.md +33 -7
- package/src/core/store/NOTES.md +162 -0
- package/src/core/store/errors.ts +15 -0
- package/src/core/store/feelings.ts +199 -0
- package/src/core/store/index.ts +397 -48
- package/src/core/store/operational.ts +148 -12
- package/src/core/store/owner-op-seam.ts +13 -3
- package/src/core/store/pre-migration.ts +3 -1
- 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 +22 -0
- package/src/adapters/dashboard/web/brain.html +0 -463
package/src/core/time.ts
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TIME — the one module that converts (docs/time.md, agreed with the owner
|
|
3
|
+
* 2026-09-25).
|
|
4
|
+
*
|
|
5
|
+
* Two kinds of time, and they are never mixed:
|
|
6
|
+
*
|
|
7
|
+
* - **A moment** — UTC milliseconds since the epoch, taken from the store's one
|
|
8
|
+
* clock (`store.now()`). `created_at`, an event's `at`, a version's
|
|
9
|
+
* `archived_at`. Shown in the person's zone, converted HERE, at display time.
|
|
10
|
+
* - **A calendar date a person says** — plain text as written: `2026-10-15`,
|
|
11
|
+
* `2026-10`, `2026`, or a range `2026-10-20..2026-10-31`. Never turned into a
|
|
12
|
+
* moment: "Oct 15" stored as midnight UTC is Oct 14 in Montana, and a month has
|
|
13
|
+
* no moment to pick at all.
|
|
14
|
+
*
|
|
15
|
+
* The third clock, the LIVED day (`store.livedDay()`, `physics/clock.ts`), is a
|
|
16
|
+
* counter of days actually used and is not a calendar; nothing here moves it.
|
|
17
|
+
*
|
|
18
|
+
* Every conversion between the two kinds lives in this file: a moment's local
|
|
19
|
+
* date, a moment's local clock line, "today" in a zone, and the arithmetic and
|
|
20
|
+
* comparison of calendar dates. `test/time.test.ts` scans `src/` for code that
|
|
21
|
+
* builds dates by hand (`toISOString().slice(0, 10)`, the `getUTC*` and local
|
|
22
|
+
* `Date` getters, `Date.UTC`) outside this file and a short allow-list.
|
|
23
|
+
*
|
|
24
|
+
* **The zone** is an IANA name (`America/Denver`), never an offset, so daylight
|
|
25
|
+
* saving is the zone rules' job. `resolveZone(configured)` is the config's
|
|
26
|
+
* `timeZone` when it names a real zone, else the machine's CURRENT zone, read
|
|
27
|
+
* per call: a laptop that flies to New York shows New York time the next time
|
|
28
|
+
* anything asks. Pure given `(at, zone)`: nothing here reads the ambient clock
|
|
29
|
+
* except `todayIn`'s default, and a store passes its own `now()`.
|
|
30
|
+
*
|
|
31
|
+
* Folded in 2026-09-25: `self/calendar.ts` (B3's local-day helper, 2026-09-23),
|
|
32
|
+
* which re-exports from here so its callers are unchanged, and the calendar
|
|
33
|
+
* arithmetic `prospective/windows.ts` did with the UTC getters.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
// ── zones ────────────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
/** The zone every conversion falls back to when the machine will not say. */
|
|
39
|
+
export const FALLBACK_ZONE = "UTC";
|
|
40
|
+
|
|
41
|
+
const formatters = new Map<string, Intl.DateTimeFormat>();
|
|
42
|
+
|
|
43
|
+
/** One formatter per (zone, shape), because building one costs ~50 µs. */
|
|
44
|
+
function formatter(zone: string, shape: "date" | "clock"): Intl.DateTimeFormat {
|
|
45
|
+
const key = `${shape}|${zone}`;
|
|
46
|
+
let f = formatters.get(key);
|
|
47
|
+
if (f === undefined) {
|
|
48
|
+
f =
|
|
49
|
+
shape === "date"
|
|
50
|
+
? new Intl.DateTimeFormat("en-US", {
|
|
51
|
+
timeZone: zone,
|
|
52
|
+
year: "numeric",
|
|
53
|
+
month: "2-digit",
|
|
54
|
+
day: "2-digit",
|
|
55
|
+
weekday: "short",
|
|
56
|
+
})
|
|
57
|
+
: new Intl.DateTimeFormat("en-US", {
|
|
58
|
+
timeZone: zone,
|
|
59
|
+
year: "numeric",
|
|
60
|
+
month: "2-digit",
|
|
61
|
+
day: "2-digit",
|
|
62
|
+
weekday: "short",
|
|
63
|
+
hour: "numeric",
|
|
64
|
+
minute: "2-digit",
|
|
65
|
+
hour12: true,
|
|
66
|
+
timeZoneName: "short",
|
|
67
|
+
});
|
|
68
|
+
formatters.set(key, f);
|
|
69
|
+
}
|
|
70
|
+
return f;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Is `name` an IANA zone this runtime knows? `"America/Denver"`, `"UTC"`. */
|
|
74
|
+
export function isZone(name: unknown): name is string {
|
|
75
|
+
if (typeof name !== "string" || name.trim().length === 0 || name.length > 64) return false;
|
|
76
|
+
// A NAME, never an offset (review N5): the runtime accepts "+05:30", but an
|
|
77
|
+
// offset has no daylight-saving rules, which is the reason zones are names.
|
|
78
|
+
if (!/^[A-Za-z]/.test(name)) return false;
|
|
79
|
+
try {
|
|
80
|
+
new Intl.DateTimeFormat("en-US", { timeZone: name });
|
|
81
|
+
return true;
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The machine's zone as it stands right now — what `TZ` sets for a process. */
|
|
88
|
+
export function machineZone(): string {
|
|
89
|
+
try {
|
|
90
|
+
const z = new Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
91
|
+
return isZone(z) ? z : FALLBACK_ZONE;
|
|
92
|
+
} catch {
|
|
93
|
+
return FALLBACK_ZONE;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The zone to convert in: the configured one when it is a real zone, else the
|
|
99
|
+
* machine's current one. A misspelt config value is a caller's mistake, and its
|
|
100
|
+
* safe reading is the machine's own day rather than a throw inside a hook — the
|
|
101
|
+
* doctor line names the zone in use, which is where the owner sees it.
|
|
102
|
+
*/
|
|
103
|
+
export function resolveZone(configured?: string | null): string {
|
|
104
|
+
return configured !== undefined && configured !== null && isZone(configured) ? configured : machineZone();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ── a moment, in a zone ──────────────────────────────────────────────────────
|
|
108
|
+
|
|
109
|
+
interface Parts {
|
|
110
|
+
year: string;
|
|
111
|
+
month: string;
|
|
112
|
+
day: string;
|
|
113
|
+
weekday: string;
|
|
114
|
+
hour: string;
|
|
115
|
+
minute: string;
|
|
116
|
+
dayPeriod: string;
|
|
117
|
+
timeZoneName: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function partsOf(at: number, zone: string, shape: "date" | "clock"): Parts | null {
|
|
121
|
+
const d = new Date(at);
|
|
122
|
+
if (!Number.isFinite(d.getTime())) return null;
|
|
123
|
+
let raw: Intl.DateTimeFormatPart[];
|
|
124
|
+
try {
|
|
125
|
+
raw = formatter(zone, shape).formatToParts(d);
|
|
126
|
+
} catch {
|
|
127
|
+
if (zone === FALLBACK_ZONE) return null;
|
|
128
|
+
return partsOf(at, FALLBACK_ZONE, shape);
|
|
129
|
+
}
|
|
130
|
+
const out: Record<string, string> = {};
|
|
131
|
+
for (const p of raw) out[p.type] = p.value;
|
|
132
|
+
return {
|
|
133
|
+
year: (out["year"] ?? "").padStart(4, "0"),
|
|
134
|
+
month: out["month"] ?? "",
|
|
135
|
+
day: out["day"] ?? "",
|
|
136
|
+
weekday: out["weekday"] ?? "",
|
|
137
|
+
hour: out["hour"] ?? "",
|
|
138
|
+
minute: out["minute"] ?? "",
|
|
139
|
+
dayPeriod: (out["dayPeriod"] ?? "").toLowerCase(),
|
|
140
|
+
timeZoneName: out["timeZoneName"] ?? "",
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* `YYYY-MM-DD` — the calendar day the moment `at` falls on in `zone` (default:
|
|
146
|
+
* the machine's current zone). Empty for a moment that is not a number. An
|
|
147
|
+
* unknown zone name reads as the machine's zone, as `resolveZone` would.
|
|
148
|
+
*/
|
|
149
|
+
export function localDate(at: number, zone?: string): string {
|
|
150
|
+
const z = zone === undefined || !isZoneCached(zone) ? machineZone() : zone;
|
|
151
|
+
const p = partsOf(at, z, "date");
|
|
152
|
+
return p === null ? "" : `${p.year}-${p.month}-${p.day}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The moment as a person reads it on a clock: `Thu 25 Sep 2026, 1:40 pm MDT`.
|
|
157
|
+
* The zone's own short name (`MDT`, `EDT`, or `GMT+2` where the runtime has no
|
|
158
|
+
* abbreviation), so a reader who travelled can see which clock it is.
|
|
159
|
+
*/
|
|
160
|
+
export function localClock(at: number, zone?: string): string {
|
|
161
|
+
const z = zone === undefined || !isZoneCached(zone) ? machineZone() : zone;
|
|
162
|
+
const p = partsOf(at, z, "clock");
|
|
163
|
+
if (p === null) return "";
|
|
164
|
+
const mo = MONTHS[Number(p.month) - 1] ?? p.month;
|
|
165
|
+
return `${p.weekday} ${String(Number(p.day))} ${mo} ${p.year}, ${p.hour}:${p.minute} ${p.dayPeriod} ${p.timeZoneName}`.trim();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Today's `YYYY-MM-DD` in `zone`. A store passes its own clock; the default is
|
|
169
|
+
* the ambient one, for an adapter with no store open. */
|
|
170
|
+
export function todayIn(zone?: string, at: number = Date.now()): string {
|
|
171
|
+
return localDate(at, zone);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The UTC calendar date of a moment. For the few places that still name things
|
|
176
|
+
* by the UTC day on purpose (a snapshot folder, a parked directory's suffix —
|
|
177
|
+
* each says why) and for reading rows written before 2026-09-25, whose
|
|
178
|
+
* `learned_on` was UTC. A person's day is `localDate`.
|
|
179
|
+
*/
|
|
180
|
+
export function utcDate(at: number): string {
|
|
181
|
+
const d = new Date(at);
|
|
182
|
+
return Number.isFinite(d.getTime()) ? d.toISOString().slice(0, 10) : "";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** A moment as an ISO-8601 UTC instant (`2026-09-25T19:40:00.000Z`) — a log
|
|
186
|
+
* line's or a file name's spelling of a moment, never a person's. */
|
|
187
|
+
export function isoInstant(at: number): string {
|
|
188
|
+
const d = new Date(at);
|
|
189
|
+
return Number.isFinite(d.getTime()) ? d.toISOString() : "";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const zoneOk = new Map<string, boolean>();
|
|
193
|
+
function isZoneCached(zone: string): boolean {
|
|
194
|
+
let ok = zoneOk.get(zone);
|
|
195
|
+
if (ok === undefined) {
|
|
196
|
+
ok = isZone(zone);
|
|
197
|
+
zoneOk.set(zone, ok);
|
|
198
|
+
}
|
|
199
|
+
return ok;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ── calendar dates, as a person says them ────────────────────────────────────
|
|
203
|
+
|
|
204
|
+
/** How much a stated date says. A range is two days, inclusive. */
|
|
205
|
+
export type CalendarPrecision = "day" | "month" | "year" | "range";
|
|
206
|
+
|
|
207
|
+
/** A stated calendar date, read: the text as written and the days it covers. */
|
|
208
|
+
export interface CalendarDate {
|
|
209
|
+
/** Exactly as stated — never widened, never narrowed (prospective §12 G4). */
|
|
210
|
+
readonly text: string;
|
|
211
|
+
readonly precision: CalendarPrecision;
|
|
212
|
+
/** First day covered, `YYYY-MM-DD`. */
|
|
213
|
+
readonly first: string;
|
|
214
|
+
/** Last day covered, inclusive. */
|
|
215
|
+
readonly last: string;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const DAY_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
219
|
+
const MONTH_RE = /^(\d{4})-(\d{2})$/;
|
|
220
|
+
const YEAR_RE = /^(\d{4})$/;
|
|
221
|
+
/** The range separator: `2026-10-20..2026-10-31`. */
|
|
222
|
+
export const RANGE_SEP = "..";
|
|
223
|
+
const DAY_MS = 86_400_000;
|
|
224
|
+
|
|
225
|
+
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] as const;
|
|
226
|
+
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] as const;
|
|
227
|
+
|
|
228
|
+
function pad2(n: number): string {
|
|
229
|
+
return String(n).padStart(2, "0");
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* A `YYYY-MM-DD` as a count of days, for arithmetic ONLY. The day is read as
|
|
234
|
+
* the label it is — never as a moment in any zone — so the arithmetic uses the
|
|
235
|
+
* proleptic Gregorian day count directly and needs no `Date` at all.
|
|
236
|
+
*/
|
|
237
|
+
function dayNumber(y: number, m: number, d: number): number {
|
|
238
|
+
// Days from civil, after Howard Hinnant's algorithm: exact for every year.
|
|
239
|
+
const yy = m <= 2 ? y - 1 : y;
|
|
240
|
+
const era = Math.floor(yy / 400);
|
|
241
|
+
const yoe = yy - era * 400;
|
|
242
|
+
const mp = (m + 9) % 12;
|
|
243
|
+
const doy = Math.floor((153 * mp + 2) / 5) + d - 1;
|
|
244
|
+
const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;
|
|
245
|
+
return era * 146097 + doe - 719468;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function fromDayNumber(z: number): string {
|
|
249
|
+
const zz = z + 719468;
|
|
250
|
+
const era = Math.floor(zz / 146097);
|
|
251
|
+
const doe = zz - era * 146097;
|
|
252
|
+
const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);
|
|
253
|
+
const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));
|
|
254
|
+
const mp = Math.floor((5 * doy + 2) / 153);
|
|
255
|
+
const d = doy - Math.floor((153 * mp + 2) / 5) + 1;
|
|
256
|
+
const m = mp < 10 ? mp + 3 : mp - 9;
|
|
257
|
+
const y = yoe + era * 400 + (m <= 2 ? 1 : 0);
|
|
258
|
+
return `${String(y).padStart(4, "0")}-${pad2(m)}-${pad2(d)}`;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function daysInMonth(y: number, m: number): number {
|
|
262
|
+
return dayNumber(m === 12 ? y + 1 : y, m === 12 ? 1 : m + 1, 1) - dayNumber(y, m, 1);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The day count of a real `YYYY-MM-DD`, or null (`2026-02-30` is not a day). */
|
|
266
|
+
function dayOf(ymd: string): number | null {
|
|
267
|
+
const m = DAY_RE.exec(ymd);
|
|
268
|
+
if (m === null) return null;
|
|
269
|
+
const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
270
|
+
if (y < 1 || mo < 1 || mo > 12 || d < 1 || d > daysInMonth(y, mo)) return null;
|
|
271
|
+
return dayNumber(y, mo, d);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Is `ymd` a whole, real calendar day? */
|
|
275
|
+
export function isDay(ymd: unknown): ymd is string {
|
|
276
|
+
return typeof ymd === "string" && dayOf(ymd) !== null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Read a stated calendar date: a day, a month, a year, or a range of two days
|
|
281
|
+
* (`2026-10-20..2026-10-31`, inclusive, first ≤ last). Null for anything else —
|
|
282
|
+
* a refusal with a shape, never a guess.
|
|
283
|
+
*/
|
|
284
|
+
export function parseCalendarDate(text: unknown): CalendarDate | null {
|
|
285
|
+
if (typeof text !== "string") return null;
|
|
286
|
+
const s = text.trim();
|
|
287
|
+
if (s.includes(RANGE_SEP)) {
|
|
288
|
+
const [a, b, ...rest] = s.split(RANGE_SEP);
|
|
289
|
+
if (rest.length > 0 || a === undefined || b === undefined) return null;
|
|
290
|
+
const x = dayOf(a);
|
|
291
|
+
const y = dayOf(b);
|
|
292
|
+
if (x === null || y === null || y < x) return null;
|
|
293
|
+
return { text: s, precision: "range", first: a, last: b };
|
|
294
|
+
}
|
|
295
|
+
if (dayOf(s) !== null) return { text: s, precision: "day", first: s, last: s };
|
|
296
|
+
const mm = MONTH_RE.exec(s);
|
|
297
|
+
if (mm !== null) {
|
|
298
|
+
const [y, mo] = [Number(mm[1]), Number(mm[2])];
|
|
299
|
+
if (y < 1 || mo < 1 || mo > 12) return null;
|
|
300
|
+
return { text: s, precision: "month", first: `${mm[1]}-${mm[2]}-01`, last: `${mm[1]}-${mm[2]}-${pad2(daysInMonth(y, mo))}` };
|
|
301
|
+
}
|
|
302
|
+
const yy = YEAR_RE.exec(s);
|
|
303
|
+
if (yy !== null && Number(yy[1]) >= 1) return { text: s, precision: "year", first: `${yy[1]}-01-01`, last: `${yy[1]}-12-31` };
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Is `text` a calendar date this module can read? */
|
|
308
|
+
export function isCalendarDate(text: unknown): text is string {
|
|
309
|
+
return parseCalendarDate(text) !== null;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** `ymd` moved by `n` days. Throws on a malformed day — a caller's bug. */
|
|
313
|
+
export function addDays(ymd: string, n: number): string {
|
|
314
|
+
const d = dayOf(ymd);
|
|
315
|
+
if (d === null) throw new Error(`addDays: not a calendar day: ${ymd}`);
|
|
316
|
+
return fromDayNumber(d + n);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Calendar days from `a` to `b`; negative when `b` is earlier. Throws on a malformed day. */
|
|
320
|
+
export function daysBetween(a: string, b: string): number {
|
|
321
|
+
const x = dayOf(a);
|
|
322
|
+
const y = dayOf(b);
|
|
323
|
+
if (x === null || y === null) throw new Error(`daysBetween: not calendar days: ${a}, ${b}`);
|
|
324
|
+
return y - x;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** First and last day of a stated `YYYY-MM`, or null. */
|
|
328
|
+
export function monthBounds(month: string): { first: string; last: string } | null {
|
|
329
|
+
const c = parseCalendarDate(month);
|
|
330
|
+
return c !== null && c.precision === "month" ? { first: c.first, last: c.last } : null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Order two stated dates by the first day each covers, then by the last — so a
|
|
335
|
+
* month sorts beside its first day rather than before every day of the year the
|
|
336
|
+
* way the raw strings would (`'2026-10' < '2026-01-05'` is false, and
|
|
337
|
+
* `'2026-10' < '2026-10-01'` is true). Unreadable text sorts last, by text.
|
|
338
|
+
*/
|
|
339
|
+
export function compareCalendarDates(a: string, b: string): number {
|
|
340
|
+
const x = parseCalendarDate(a);
|
|
341
|
+
const y = parseCalendarDate(b);
|
|
342
|
+
if (x === null || y === null) return x === null && y === null ? a.localeCompare(b) : x === null ? 1 : -1;
|
|
343
|
+
if (x.first !== y.first) return x.first < y.first ? -1 : 1;
|
|
344
|
+
if (x.last !== y.last) return x.last < y.last ? -1 : 1;
|
|
345
|
+
return 0;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Does the stated date cover any day from `from` to `to` (inclusive, `YYYY-MM-DD`)? */
|
|
349
|
+
export function calendarOverlaps(date: string, from: string, to: string): boolean {
|
|
350
|
+
const c = parseCalendarDate(date);
|
|
351
|
+
return c !== null && c.first <= to && c.last >= from;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** Does the stated date cover the day `ymd`? */
|
|
355
|
+
export function calendarCovers(date: string, ymd: string): boolean {
|
|
356
|
+
return calendarOverlaps(date, ymd, ymd);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* A `YYYY-MM-DD` as a person reads it: `Tue 23 Sep 2026`. Empty for anything
|
|
361
|
+
* that is not a whole, real day — a `2026-08` or a typo is not padded into a day
|
|
362
|
+
* it never named. The string is already a calendar day, so no zone is involved.
|
|
363
|
+
*/
|
|
364
|
+
export function readableDate(ymd: string): string {
|
|
365
|
+
const d = dayOf(ymd.trim());
|
|
366
|
+
if (d === null) return "";
|
|
367
|
+
const [y, mo, dd] = ymd.trim().split("-").map(Number) as [number, number, number];
|
|
368
|
+
// Day 0 of the count (1970-01-01) was a Thursday.
|
|
369
|
+
const weekday = WEEKDAYS[(((d + 4) % 7) + 7) % 7];
|
|
370
|
+
return `${weekday} ${String(dd)} ${MONTHS[mo - 1]} ${String(y)}`;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Legacy name (`self/calendar.ts`, 2026-09-23): the local date of a moment. */
|
|
374
|
+
export const calendarDate = localDate;
|
package/src/core/types.ts
CHANGED
|
@@ -29,6 +29,19 @@ export const MEMORY_SOURCES = [
|
|
|
29
29
|
] as const;
|
|
30
30
|
export type MemorySource = (typeof MEMORY_SOURCES)[number];
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* A model id as the host reports it (`claude-opus-5-5`, `claude-opus-5-5[1m]`),
|
|
34
|
+
* and nothing else: it is printed into a chapter heading and stored on a row
|
|
35
|
+
* (`memories.model`, schema v7), so no spaces, no `·`, no `<synthetic>`.
|
|
36
|
+
* Anything that fails this is treated as unknown.
|
|
37
|
+
*
|
|
38
|
+
* Moved here from `self/episodes.ts` on 2026-09-25 so `store/` can screen the
|
|
39
|
+
* column with the same test the chapter heading uses; `self/` re-exports it.
|
|
40
|
+
*/
|
|
41
|
+
export function isModelId(value: unknown): value is string {
|
|
42
|
+
return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9._:\[\]-]{0,63}$/.test(value);
|
|
43
|
+
}
|
|
44
|
+
|
|
32
45
|
/** Four dimensions, 0-1, fixed at encoding. novelty is null for a blind write
|
|
33
46
|
* (no schema context existed) — recorded, never defaulted (scar §2.9). */
|
|
34
47
|
export interface Salience {
|
|
@@ -58,6 +71,15 @@ export interface MemoryPhysics {
|
|
|
58
71
|
* Optional so this stays an extension, not a break; absent reads as 0, which
|
|
59
72
|
* is the promotion-blocking direction. */
|
|
60
73
|
reinforcedDays?: number;
|
|
74
|
+
/**
|
|
75
|
+
* The strongest RECORDED feeling on this memory (the `feelings` table's
|
|
76
|
+
* `MAX(strength)`, owner's or self's), or absent/null when it carries none.
|
|
77
|
+
* Read beside the row by `store.row()`; physics folds it into the emotional
|
|
78
|
+
* intensity (`physics.emotionalIntensity`, CONTRACT §5.10). As recorded, never
|
|
79
|
+
* softened: the height and slope a feeling gives a memory are set by how it
|
|
80
|
+
* felt, the way affect stamps a trace at encoding.
|
|
81
|
+
*/
|
|
82
|
+
feelingPeak?: number | null;
|
|
61
83
|
consolidated: boolean;
|
|
62
84
|
/** Set only by the explicit promotion crossing or revision inheritance (§5.3). */
|
|
63
85
|
promotedIdentity: boolean;
|