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
package/src/core/store/index.ts
CHANGED
|
@@ -41,6 +41,19 @@ import { existsSync, mkdirSync, readdirSync } from "node:fs";
|
|
|
41
41
|
import { join } from "node:path";
|
|
42
42
|
import { randomBytes } from "node:crypto";
|
|
43
43
|
import type { Band, Kind, MemoryPhysics, MemorySource, Salience } from "../types.js";
|
|
44
|
+
import { isModelId } from "../types.js";
|
|
45
|
+
import {
|
|
46
|
+
calendarOverlaps,
|
|
47
|
+
compareCalendarDates,
|
|
48
|
+
daysBetween,
|
|
49
|
+
isCalendarDate,
|
|
50
|
+
isDay,
|
|
51
|
+
localDate,
|
|
52
|
+
resolveZone,
|
|
53
|
+
utcDate,
|
|
54
|
+
} from "../time.js";
|
|
55
|
+
import { checkFeelings } from "./feelings.js";
|
|
56
|
+
import type { AddFeelingsResult, FeelingInput, FeelingRow } from "./feelings.js";
|
|
44
57
|
import { creditUse } from "../physics/index.js";
|
|
45
58
|
import type { CreditOutcome, UseTier } from "../physics/index.js";
|
|
46
59
|
import type { Db, Statement, WalFold } from "./db.js";
|
|
@@ -60,6 +73,7 @@ import type {
|
|
|
60
73
|
EventRow,
|
|
61
74
|
GateSessionRow,
|
|
62
75
|
MemoryRow,
|
|
76
|
+
MigrationNote,
|
|
63
77
|
ProspectiveRow,
|
|
64
78
|
RemovalRow,
|
|
65
79
|
TombstoneRow,
|
|
@@ -112,6 +126,7 @@ export * from "../observer.js";
|
|
|
112
126
|
export * from "./paths.js";
|
|
113
127
|
export * from "./prose.js";
|
|
114
128
|
export * from "./render.js";
|
|
129
|
+
export * from "./feelings.js";
|
|
115
130
|
export type { Db, Statement, WalFold } from "./db.js";
|
|
116
131
|
export type {
|
|
117
132
|
MemoryRow,
|
|
@@ -122,12 +137,24 @@ export type {
|
|
|
122
137
|
ProspectiveRow,
|
|
123
138
|
RemovalRow,
|
|
124
139
|
TombstoneRow,
|
|
140
|
+
MigrationNote,
|
|
125
141
|
} from "./operational.js";
|
|
142
|
+
export {
|
|
143
|
+
PRE_MIGRATION_NAME_RE,
|
|
144
|
+
PRE_MIGRATION_TAG,
|
|
145
|
+
assertPreMigrationTarget,
|
|
146
|
+
preMigrationDir,
|
|
147
|
+
preMigrationName,
|
|
148
|
+
preMigrationVersions,
|
|
149
|
+
realpathDeep,
|
|
150
|
+
vacuumInto,
|
|
151
|
+
} from "./pre-migration.js";
|
|
126
152
|
export {
|
|
127
153
|
ADDED_COLUMNS,
|
|
128
154
|
DEFAULT_RETENTION_DAYS,
|
|
129
155
|
isPreRowsDatabase,
|
|
130
156
|
OBSERVER_READ_FLOOR,
|
|
157
|
+
pendingMigration,
|
|
131
158
|
SCHEMA_VERSION,
|
|
132
159
|
rowToPhysics,
|
|
133
160
|
rowTombstoned,
|
|
@@ -220,7 +247,21 @@ export interface StoreOptions extends Stance {
|
|
|
220
247
|
* before. A seeder, a replay harness and a migration pass one.
|
|
221
248
|
*/
|
|
222
249
|
now?: () => number;
|
|
250
|
+
/**
|
|
251
|
+
* The zone a moment is read in when this store names a person's day —
|
|
252
|
+
* `learned_on`, `today()`, the day the store began (docs/time.md, 2026-09-25).
|
|
253
|
+
* An IANA name from the host's config (`timeZone`); absent or unknown, the
|
|
254
|
+
* machine's CURRENT zone, resolved at every call so a laptop that moves shows
|
|
255
|
+
* where it is now (`time.ts#resolveZone`).
|
|
256
|
+
*/
|
|
257
|
+
timeZone?: string;
|
|
223
258
|
onEvent?: (event: StoreEvent) => void;
|
|
259
|
+
/**
|
|
260
|
+
* Where the copy taken before a schema migration goes. Absent ⇒ beside the
|
|
261
|
+
* store (`<base>/snapshots`); a store outside that layout with none named
|
|
262
|
+
* refuses to migrate (`MIGRATION_SNAPSHOT_FAILED`).
|
|
263
|
+
*/
|
|
264
|
+
snapshotsDir?: string;
|
|
224
265
|
}
|
|
225
266
|
|
|
226
267
|
export interface PutInput {
|
|
@@ -232,6 +273,19 @@ export interface PutInput {
|
|
|
232
273
|
title?: string;
|
|
233
274
|
happenedOn?: string;
|
|
234
275
|
learnedOn?: string;
|
|
276
|
+
/**
|
|
277
|
+
* The calendar date this memory is ABOUT, as the person said it — `2026-10-15`,
|
|
278
|
+
* `2026-10`, `2026`, or `2026-10-20..2026-10-31` (`time.ts#parseCalendarDate`).
|
|
279
|
+
* A reminder's date. Refused by name when it is not one (`EVENT_DATE_INVALID`):
|
|
280
|
+
* a date that cannot be read is a reminder that will never come up.
|
|
281
|
+
*/
|
|
282
|
+
eventDate?: string;
|
|
283
|
+
/**
|
|
284
|
+
* The model id that wrote these words, as the host reported it (schema v7).
|
|
285
|
+
* Screened with `isModelId`; anything else — or absent — writes NULL, which
|
|
286
|
+
* reads "not a named model" (the sweep, the owner, an unknown host).
|
|
287
|
+
*/
|
|
288
|
+
model?: string;
|
|
235
289
|
meta?: Record<string, unknown>;
|
|
236
290
|
band?: Band;
|
|
237
291
|
salience?: Partial<Salience>;
|
|
@@ -284,6 +338,18 @@ export interface StoredMemory {
|
|
|
284
338
|
* `confidentialByMeta` at every write). Carried on the read so a caller gates
|
|
285
339
|
* on a boolean rather than on JSON it had to re-interpret. */
|
|
286
340
|
confidential: boolean;
|
|
341
|
+
/** v7 moments, UTC ms (docs/time.md). Null on a row written before v7. */
|
|
342
|
+
createdAt: number | null;
|
|
343
|
+
updatedAt: number | null;
|
|
344
|
+
/** v7: the model that wrote the current words; null = not a named model. */
|
|
345
|
+
model: string | null;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** One memory with a reminder date, as `Store.datedMemories` returns it. */
|
|
349
|
+
export interface DatedMemory {
|
|
350
|
+
readonly id: string;
|
|
351
|
+
/** As stated — never widened, never narrowed. */
|
|
352
|
+
readonly eventDate: string;
|
|
287
353
|
}
|
|
288
354
|
|
|
289
355
|
export interface PruneReport {
|
|
@@ -569,6 +635,7 @@ export const WRITE_METHODS = [
|
|
|
569
635
|
"link",
|
|
570
636
|
"linkMany",
|
|
571
637
|
"setProspective",
|
|
638
|
+
"addFeelings",
|
|
572
639
|
"advanceClock",
|
|
573
640
|
"setMeta",
|
|
574
641
|
"setMetaMany",
|
|
@@ -618,6 +685,9 @@ const EVENT_RING = 500;
|
|
|
618
685
|
*/
|
|
619
686
|
export const EMBEDDER_RECONCILED_EVENT = "store.embedder.reconciled";
|
|
620
687
|
|
|
688
|
+
/** The durable row an open writes after it migrated the schema: from, to, and the copy. */
|
|
689
|
+
export const STORE_MIGRATED_EVENT = "store.migrated";
|
|
690
|
+
|
|
621
691
|
/** Rows per box-3 transaction in the at-open inline refill. CAL. */
|
|
622
692
|
const REFILL_BATCH = 500;
|
|
623
693
|
/**
|
|
@@ -723,8 +793,12 @@ export class Store {
|
|
|
723
793
|
* as found.
|
|
724
794
|
*/
|
|
725
795
|
readonly embedderVerdict: EmbedderVerdict;
|
|
796
|
+
/** The migration this open ran, and the copy taken before it; null when none. */
|
|
797
|
+
readonly migration: MigrationNote | null;
|
|
726
798
|
/** The provenance clock (§I7). The ONE `Date.now` in this file is its default. */
|
|
727
799
|
private readonly nowFn: () => number;
|
|
800
|
+
/** The configured zone, as given; `zone()` resolves it per call. */
|
|
801
|
+
private readonly configuredZone: string | undefined;
|
|
728
802
|
private readonly onEvent: ((e: StoreEvent) => void) | undefined;
|
|
729
803
|
private readonly ring: StoreEvent[] = [];
|
|
730
804
|
|
|
@@ -738,6 +812,7 @@ export class Store {
|
|
|
738
812
|
this.retentionDays = opts.retentionDays ?? DEFAULT_RETENTION_DAYS;
|
|
739
813
|
this.embed = opts.embed;
|
|
740
814
|
this.nowFn = opts.now ?? Date.now;
|
|
815
|
+
this.configuredZone = opts.timeZone;
|
|
741
816
|
this.onEvent = opts.onEvent;
|
|
742
817
|
|
|
743
818
|
// ── THE PRE-ROWS REFUSAL, AND IT IS THE FIRST THING THAT HAPPENS ───────
|
|
@@ -811,10 +886,18 @@ export class Store {
|
|
|
811
886
|
// needs somewhere to open the cache, and materializing the cache's own
|
|
812
887
|
// directory changes no canonical state and takes no canonical lock.
|
|
813
888
|
mkdirSync(paths.cacheDir(this.dir), { recursive: true });
|
|
889
|
+
let migration = null as MigrationNote | null;
|
|
814
890
|
this.ops = openOperational(paths.operational(this.dir), {
|
|
815
891
|
initialize: !this.observer,
|
|
892
|
+
// For the v7 migration's clamp of `lastActiveDate` (review S1).
|
|
893
|
+
localToday: localDate(this.nowFn(), resolveZone(this.configuredZone)),
|
|
816
894
|
retentionDays: this.retentionDays,
|
|
895
|
+
...(opts.snapshotsDir === undefined ? {} : { snapshotsDir: opts.snapshotsDir }),
|
|
896
|
+
onMigrated: (note) => {
|
|
897
|
+
migration = note;
|
|
898
|
+
},
|
|
817
899
|
});
|
|
900
|
+
this.migration = migration;
|
|
818
901
|
this.cache = openCache(paths.cache(this.dir));
|
|
819
902
|
// THE AT-OPEN IDENTITY CHECK (cache v5, roadmap C1). Once per open, never
|
|
820
903
|
// per call, and never under observer: an instrument has no embedder, and
|
|
@@ -882,7 +965,8 @@ export class Store {
|
|
|
882
965
|
this.ops.run(
|
|
883
966
|
"INSERT OR IGNORE INTO meta (key, value) VALUES (?, ?)",
|
|
884
967
|
STORE_CREATED_KEY,
|
|
885
|
-
|
|
968
|
+
// The person's day (2026-09-25); a store made before this carries UTC.
|
|
969
|
+
this.today(),
|
|
886
970
|
);
|
|
887
971
|
}
|
|
888
972
|
// The owner-op capability. Handed to the seam module, never to a caller:
|
|
@@ -920,6 +1004,18 @@ export class Store {
|
|
|
920
1004
|
});
|
|
921
1005
|
this.assertLayout();
|
|
922
1006
|
this.afterReconcile(identity);
|
|
1007
|
+
if (migration !== null) this.noteMigration(migration);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/** The durable row for a migration this open ran, naming the copy taken first. */
|
|
1011
|
+
private noteMigration(note: MigrationNote): void {
|
|
1012
|
+
const data = { from: note.from, to: note.to, snapshot: note.snapshot, reused: note.reused, dir: note.dir };
|
|
1013
|
+
this.emit(STORE_MIGRATED_EVENT, undefined, data);
|
|
1014
|
+
try {
|
|
1015
|
+
this.appendEvent({ name: STORE_MIGRATED_EVENT, day: this.livedDay(), payload: data });
|
|
1016
|
+
} catch {
|
|
1017
|
+
// A lost lock costs the row, never the open; the copy is on disk either way.
|
|
1018
|
+
}
|
|
923
1019
|
}
|
|
924
1020
|
|
|
925
1021
|
/**
|
|
@@ -1145,16 +1241,23 @@ export class Store {
|
|
|
1145
1241
|
* thinks it is" asks here rather than reading the ambient clock, so a seeded,
|
|
1146
1242
|
* replayed or migrated store dates its rows by the run it is replaying.
|
|
1147
1243
|
*
|
|
1148
|
-
*
|
|
1149
|
-
* `
|
|
1150
|
-
*
|
|
1244
|
+
* `today()` is the person's calendar day — the LOCAL date of `now()` in
|
|
1245
|
+
* `zone()` (docs/time.md, 2026-09-25). It was UTC until then, on the reasoning
|
|
1246
|
+
* NOTES 2026-09-05 records: provenance and the hooks' dates on one calendar.
|
|
1247
|
+
* They moved together, so they are still on one — the local one.
|
|
1151
1248
|
*/
|
|
1152
1249
|
now(): number {
|
|
1153
1250
|
return this.nowFn();
|
|
1154
1251
|
}
|
|
1155
1252
|
|
|
1156
1253
|
today(): string {
|
|
1157
|
-
return
|
|
1254
|
+
return localDate(this.nowFn(), this.zone());
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/** The zone this store reads a person's day in: the configured one, else the
|
|
1258
|
+
* machine's current zone, resolved now (`time.ts#resolveZone`). */
|
|
1259
|
+
zone(): string {
|
|
1260
|
+
return resolveZone(this.configuredZone);
|
|
1158
1261
|
}
|
|
1159
1262
|
|
|
1160
1263
|
/** Copies, ordered oldest first. Optionally filtered by name. */
|
|
@@ -1247,6 +1350,12 @@ export class Store {
|
|
|
1247
1350
|
meta?: Record<string, unknown>;
|
|
1248
1351
|
learnedOn?: string;
|
|
1249
1352
|
happenedOn?: string;
|
|
1353
|
+
/** A new reminder date, or `null` to clear it (a reschedule, a cancel). */
|
|
1354
|
+
eventDate?: string | null;
|
|
1355
|
+
/** Who wrote the new words (see `PutInput.model`). A revise that changes
|
|
1356
|
+
* the BODY and names no model records NULL: the words are no longer the
|
|
1357
|
+
* last model's. A revise that leaves the body alone keeps the model. */
|
|
1358
|
+
model?: string;
|
|
1250
1359
|
reason?: string;
|
|
1251
1360
|
},
|
|
1252
1361
|
): number {
|
|
@@ -1259,22 +1368,29 @@ export class Store {
|
|
|
1259
1368
|
// a `wx` collision loop and a crash window between the two; it is now a
|
|
1260
1369
|
// row written beside the update, so "an overwrite keeps the prior version"
|
|
1261
1370
|
// is a property of the transaction rather than of the ordering (§5 G5).
|
|
1371
|
+
const at = this.nowFn();
|
|
1372
|
+
const eventDate =
|
|
1373
|
+
patch.eventDate === undefined || patch.eventDate === null ? patch.eventDate : assertEventDate(patch.eventDate, id);
|
|
1262
1374
|
this.ops.run(
|
|
1263
1375
|
`INSERT INTO versions
|
|
1264
1376
|
(memory_id, seq, reason, version_day, archived_at, content_hash, successor_id,
|
|
1265
|
-
title, body, meta, learned_on, happened_on)
|
|
1266
|
-
VALUES (?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?)`,
|
|
1377
|
+
title, body, meta, learned_on, happened_on, created_at, model, event_date)
|
|
1378
|
+
VALUES (?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1267
1379
|
id,
|
|
1268
1380
|
seq,
|
|
1269
1381
|
patch.reason ?? "revise",
|
|
1270
1382
|
this.livedDay(),
|
|
1271
|
-
|
|
1383
|
+
at,
|
|
1272
1384
|
row.content_hash,
|
|
1273
1385
|
row.title,
|
|
1274
1386
|
row.body,
|
|
1275
1387
|
row.meta,
|
|
1276
1388
|
row.learned_on,
|
|
1277
1389
|
row.happened_on,
|
|
1390
|
+
// The archived words' own moment: when they were last written.
|
|
1391
|
+
row.updated_at ?? row.created_at,
|
|
1392
|
+
row.model,
|
|
1393
|
+
row.event_date,
|
|
1278
1394
|
);
|
|
1279
1395
|
const next: ProseDoc = {
|
|
1280
1396
|
...prior,
|
|
@@ -1284,6 +1400,14 @@ export class Store {
|
|
|
1284
1400
|
if (patch.title !== undefined) next.title = patch.title;
|
|
1285
1401
|
if (patch.learnedOn !== undefined) next.learnedOn = patch.learnedOn;
|
|
1286
1402
|
if (patch.happenedOn !== undefined) next.happenedOn = patch.happenedOn;
|
|
1403
|
+
if (eventDate === null) delete next.eventDate;
|
|
1404
|
+
else if (eventDate !== undefined) next.eventDate = eventDate;
|
|
1405
|
+
const model =
|
|
1406
|
+
patch.model !== undefined
|
|
1407
|
+
? modelOrNull(patch.model)
|
|
1408
|
+
: patch.body !== undefined
|
|
1409
|
+
? null
|
|
1410
|
+
: row.model;
|
|
1287
1411
|
// THE SAME RULE AS `put`, through the same function. `patch.body ?? ...`
|
|
1288
1412
|
// accepts `""` happily, and on this floor that would write the one state
|
|
1289
1413
|
// no write path may produce — a row whose hash names words its body no
|
|
@@ -1295,7 +1419,8 @@ export class Store {
|
|
|
1295
1419
|
this.ops.run(
|
|
1296
1420
|
`UPDATE memories
|
|
1297
1421
|
SET content_hash = ?, revision = ?, title = ?, body = ?, meta = ?,
|
|
1298
|
-
confidential = ?, learned_on = ?, happened_on =
|
|
1422
|
+
confidential = ?, learned_on = ?, happened_on = ?,
|
|
1423
|
+
event_date = ?, model = ?, updated_at = ?
|
|
1299
1424
|
WHERE id = ?`,
|
|
1300
1425
|
nextHash,
|
|
1301
1426
|
seq,
|
|
@@ -1308,6 +1433,9 @@ export class Store {
|
|
|
1308
1433
|
confidentialByMeta(next.meta) ? 1 : 0,
|
|
1309
1434
|
next.learnedOn,
|
|
1310
1435
|
next.happenedOn ?? null,
|
|
1436
|
+
next.eventDate ?? null,
|
|
1437
|
+
model,
|
|
1438
|
+
at,
|
|
1311
1439
|
id,
|
|
1312
1440
|
);
|
|
1313
1441
|
return { doc: next, seq, hash: nextHash };
|
|
@@ -1331,16 +1459,17 @@ export class Store {
|
|
|
1331
1459
|
// body too, so the text is held twice for as long as the version row
|
|
1332
1460
|
// lives — the honest cost of rows over files, bounded by the 90-day
|
|
1333
1461
|
// version prune (owner ruling 1). See `store/NOTES.md`.
|
|
1462
|
+
const at = this.nowFn();
|
|
1334
1463
|
this.ops.run(
|
|
1335
1464
|
`INSERT INTO versions
|
|
1336
1465
|
(memory_id, seq, reason, version_day, archived_at, content_hash, successor_id,
|
|
1337
|
-
title, body, meta, learned_on, happened_on)
|
|
1338
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1466
|
+
title, body, meta, learned_on, happened_on, created_at, model, event_date)
|
|
1467
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1339
1468
|
oldId,
|
|
1340
1469
|
row.revision + 1,
|
|
1341
1470
|
reason,
|
|
1342
1471
|
this.livedDay(),
|
|
1343
|
-
|
|
1472
|
+
at,
|
|
1344
1473
|
row.content_hash,
|
|
1345
1474
|
created.id,
|
|
1346
1475
|
row.title,
|
|
@@ -1348,13 +1477,18 @@ export class Store {
|
|
|
1348
1477
|
row.meta,
|
|
1349
1478
|
row.learned_on,
|
|
1350
1479
|
row.happened_on,
|
|
1480
|
+
row.updated_at ?? row.created_at,
|
|
1481
|
+
row.model,
|
|
1482
|
+
row.event_date,
|
|
1351
1483
|
);
|
|
1352
1484
|
this.ops.run(
|
|
1353
|
-
`UPDATE memories SET superseded_by = ?, archived = 1, archived_reason = ?, revision =
|
|
1485
|
+
`UPDATE memories SET superseded_by = ?, archived = 1, archived_reason = ?, revision = ?,
|
|
1486
|
+
updated_at = ?
|
|
1354
1487
|
WHERE id = ?`,
|
|
1355
1488
|
created.id,
|
|
1356
1489
|
reason,
|
|
1357
1490
|
row.revision + 1,
|
|
1491
|
+
at,
|
|
1358
1492
|
oldId,
|
|
1359
1493
|
);
|
|
1360
1494
|
return { doc: created, newId: created.id };
|
|
@@ -1372,7 +1506,12 @@ export class Store {
|
|
|
1372
1506
|
archive(id: string, reason: string): void {
|
|
1373
1507
|
this.mutate("archive", () => {
|
|
1374
1508
|
this.requireRow(id);
|
|
1375
|
-
this.ops.run(
|
|
1509
|
+
this.ops.run(
|
|
1510
|
+
"UPDATE memories SET archived = 1, archived_reason = ?, updated_at = ? WHERE id = ?",
|
|
1511
|
+
reason,
|
|
1512
|
+
this.nowFn(),
|
|
1513
|
+
id,
|
|
1514
|
+
);
|
|
1376
1515
|
});
|
|
1377
1516
|
// Out of the index, not out of the store: `read`, `resolve` and the version
|
|
1378
1517
|
// chain are untouched. An archived row was never deliverable — `activate`
|
|
@@ -1465,13 +1604,8 @@ export class Store {
|
|
|
1465
1604
|
|
|
1466
1605
|
link(edge: EdgeInput): void {
|
|
1467
1606
|
this.mutate("link", () => {
|
|
1468
|
-
this.
|
|
1469
|
-
|
|
1470
|
-
edge.src,
|
|
1471
|
-
edge.dst,
|
|
1472
|
-
edge.weight,
|
|
1473
|
-
edge.day,
|
|
1474
|
-
);
|
|
1607
|
+
const at = this.nowFn();
|
|
1608
|
+
this.ops.run(EDGE_UPSERT, edge.src, edge.dst, edge.weight, edge.day, at, at);
|
|
1475
1609
|
});
|
|
1476
1610
|
this.emit("store.link", edge.src, { dst: edge.dst, weight: edge.weight });
|
|
1477
1611
|
}
|
|
@@ -1479,10 +1613,9 @@ export class Store {
|
|
|
1479
1613
|
/** Multi-row and foreign-keyed: one bad endpoint rolls the whole batch back. */
|
|
1480
1614
|
linkMany(edges: readonly EdgeInput[]): void {
|
|
1481
1615
|
this.mutate("linkMany", () => {
|
|
1482
|
-
const st = this.ops.prepare(
|
|
1483
|
-
|
|
1484
|
-
);
|
|
1485
|
-
for (const e of edges) st.run(e.src, e.dst, e.weight, e.day);
|
|
1616
|
+
const st = this.ops.prepare(EDGE_UPSERT);
|
|
1617
|
+
const at = this.nowFn();
|
|
1618
|
+
for (const e of edges) st.run(e.src, e.dst, e.weight, e.day, at, at);
|
|
1486
1619
|
});
|
|
1487
1620
|
this.emit("store.link", undefined, { count: edges.length });
|
|
1488
1621
|
}
|
|
@@ -1490,10 +1623,18 @@ export class Store {
|
|
|
1490
1623
|
setProspective(entry: ProspectiveInput): void {
|
|
1491
1624
|
this.mutate("setProspective", () => {
|
|
1492
1625
|
this.requireRow(entry.memoryId);
|
|
1626
|
+
// An UPSERT, not `INSERT OR REPLACE`: a replace deletes the row first and
|
|
1627
|
+
// would reset `created_at` on every state change (v7).
|
|
1628
|
+
const at = this.nowFn();
|
|
1493
1629
|
this.ops.run(
|
|
1494
|
-
`INSERT
|
|
1495
|
-
(memory_id, window_key, event_date, precision, state, fires, last_fired_day
|
|
1496
|
-
|
|
1630
|
+
`INSERT INTO prospective
|
|
1631
|
+
(memory_id, window_key, event_date, precision, state, fires, last_fired_day,
|
|
1632
|
+
created_at, updated_at)
|
|
1633
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1634
|
+
ON CONFLICT (memory_id, window_key) DO UPDATE SET
|
|
1635
|
+
event_date = excluded.event_date, precision = excluded.precision,
|
|
1636
|
+
state = excluded.state, fires = excluded.fires,
|
|
1637
|
+
last_fired_day = excluded.last_fired_day, updated_at = excluded.updated_at`,
|
|
1497
1638
|
entry.memoryId,
|
|
1498
1639
|
entry.windowKey,
|
|
1499
1640
|
entry.eventDate,
|
|
@@ -1501,6 +1642,8 @@ export class Store {
|
|
|
1501
1642
|
entry.state,
|
|
1502
1643
|
entry.fires ?? 0,
|
|
1503
1644
|
entry.lastFiredDay ?? null,
|
|
1645
|
+
at,
|
|
1646
|
+
at,
|
|
1504
1647
|
);
|
|
1505
1648
|
});
|
|
1506
1649
|
this.emit("store.prospective", entry.memoryId, {
|
|
@@ -1509,11 +1652,66 @@ export class Store {
|
|
|
1509
1652
|
});
|
|
1510
1653
|
}
|
|
1511
1654
|
|
|
1655
|
+
/**
|
|
1656
|
+
* Record feelings on a memory (schema v7; `store/feelings.ts`). All or none:
|
|
1657
|
+
* one bad input refuses the call `FEELING_INVALID` and writes nothing. An
|
|
1658
|
+
* emotion not on the wheel is stored as `other` with the word kept, and the
|
|
1659
|
+
* result's `notices` name the nearest wheel keys so the caller can rewrite.
|
|
1660
|
+
* `beneath` is an existing feeling's id ON THIS MEMORY or another input's
|
|
1661
|
+
* index. `model` is the writer's model id, screened like `PutInput.model`.
|
|
1662
|
+
*/
|
|
1663
|
+
addFeelings(memoryId: string, inputs: readonly FeelingInput[], opts: { model?: string } = {}): AddFeelingsResult {
|
|
1664
|
+
const { rows, notices } = checkFeelings(inputs);
|
|
1665
|
+
const ids = rows.map(() => `fel_${randomBytes(6).toString("hex")}`);
|
|
1666
|
+
this.mutate("addFeelings", () => {
|
|
1667
|
+
this.requireRow(memoryId);
|
|
1668
|
+
const at = this.nowFn();
|
|
1669
|
+
const model = modelOrNull(opts.model);
|
|
1670
|
+
const insert = this.ops.prepare(
|
|
1671
|
+
`INSERT INTO feelings
|
|
1672
|
+
(id, memory_id, whose, core, emotion, other_word, strength, beneath_id, carried_by, model,
|
|
1673
|
+
created_at, updated_at)
|
|
1674
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?)`,
|
|
1675
|
+
);
|
|
1676
|
+
rows.forEach((r, i) =>
|
|
1677
|
+
insert.run(ids[i] as string, memoryId, r.whose, r.core, r.emotion, r.otherWord, r.strength, r.carriedBy, model, at, at),
|
|
1678
|
+
);
|
|
1679
|
+
// THEN the links, so an input may sit on one listed after it.
|
|
1680
|
+
rows.forEach((r, i) => {
|
|
1681
|
+
if (r.beneath === null) return;
|
|
1682
|
+
let under: string;
|
|
1683
|
+
if (typeof r.beneath === "number") {
|
|
1684
|
+
under = ids[r.beneath] as string;
|
|
1685
|
+
} else {
|
|
1686
|
+
const found = this.ops.get<{ memory_id: string }>("SELECT memory_id FROM feelings WHERE id = ?", r.beneath);
|
|
1687
|
+
if (found === undefined || found.memory_id !== memoryId) {
|
|
1688
|
+
throw new StoreError("FEELING_INVALID", { index: i, reason: "beneath-not-on-this-memory" });
|
|
1689
|
+
}
|
|
1690
|
+
under = r.beneath;
|
|
1691
|
+
}
|
|
1692
|
+
this.ops.run("UPDATE feelings SET beneath_id = ? WHERE id = ?", under, ids[i] as string);
|
|
1693
|
+
});
|
|
1694
|
+
});
|
|
1695
|
+
this.emit("store.feelings", memoryId, { count: rows.length, other: notices.length });
|
|
1696
|
+
return { ids, notices };
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1512
1699
|
/** The active-day clock (scar E8): days actually lived, not calendar days. */
|
|
1513
1700
|
advanceClock(date: string): number {
|
|
1514
1701
|
const day = this.mutate("advanceClock", () => {
|
|
1515
1702
|
const last = this.getMeta("lastActiveDate") ?? "";
|
|
1516
1703
|
if (last !== "" && date < last) {
|
|
1704
|
+
// ONE CALENDAR DAY BACK IS THE SAME LIVED DAY (review S1, 2026-09-25).
|
|
1705
|
+
// Since the person's day became local, a date one day behind the last
|
|
1706
|
+
// one is ordinary: the upgrade evening west of UTC (the last 0.3.1
|
|
1707
|
+
// boundary stamped UTC's tomorrow), or a flight west across midnight.
|
|
1708
|
+
// It is lived time that already counted, so it holds the day — no
|
|
1709
|
+
// advance, no rewrite of `lastActiveDate`, no failure. A real jump back
|
|
1710
|
+
// of more than a day is still refused, as it always was.
|
|
1711
|
+
if (isDay(date) && isDay(last) && daysBetween(date, last) <= 1) {
|
|
1712
|
+
this.emit("store.clock.held", undefined, { date, last });
|
|
1713
|
+
return this.livedDay();
|
|
1714
|
+
}
|
|
1517
1715
|
throw new StoreError("CLOCK_BACKWARDS", { date, last });
|
|
1518
1716
|
}
|
|
1519
1717
|
if (date === last) return this.livedDay();
|
|
@@ -2320,6 +2518,9 @@ export class Store {
|
|
|
2320
2518
|
revision: row.revision,
|
|
2321
2519
|
contentHash: row.content_hash,
|
|
2322
2520
|
confidential: row.confidential === 1,
|
|
2521
|
+
createdAt: row.created_at,
|
|
2522
|
+
updatedAt: row.updated_at,
|
|
2523
|
+
model: row.model,
|
|
2323
2524
|
};
|
|
2324
2525
|
}
|
|
2325
2526
|
|
|
@@ -2377,6 +2578,69 @@ export class Store {
|
|
|
2377
2578
|
return this.ops.get<{ n: number }>(sql, ...args)?.n ?? 0;
|
|
2378
2579
|
}
|
|
2379
2580
|
|
|
2581
|
+
/**
|
|
2582
|
+
* LIVE memories whose reminder date (`event_date`) covers any day from `from`
|
|
2583
|
+
* to `to`, inclusive — `YYYY-MM-DD` both (prospective/INTERFACE-GAPS §2).
|
|
2584
|
+
* Ordered by the date, earliest first, then id.
|
|
2585
|
+
*
|
|
2586
|
+
* The SQL bound is loose on purpose and the exact test is `time.ts`'s: a
|
|
2587
|
+
* month `2026-10` sorts BEFORE `2026-10-01` as text and a range's text runs
|
|
2588
|
+
* past its first day, so a plain `BETWEEN` would miss both. The index answers
|
|
2589
|
+
* "starts no later than `to`" (`'~'` sorts after every digit and `..`), and
|
|
2590
|
+
* `calendarOverlaps` decides the rest. A memory with only the older meta
|
|
2591
|
+
* convention (`meta.eventDate`) is not here; `prospective/contentDates`
|
|
2592
|
+
* still reads that.
|
|
2593
|
+
*/
|
|
2594
|
+
datedMemories(from: string, to: string): DatedMemory[] {
|
|
2595
|
+
const rows = this.ops.all<{ id: string; event_date: string }>(
|
|
2596
|
+
`SELECT id, event_date FROM memories
|
|
2597
|
+
WHERE event_date IS NOT NULL AND event_date <= ? AND archived = 0`,
|
|
2598
|
+
`${to}~`,
|
|
2599
|
+
);
|
|
2600
|
+
return rows
|
|
2601
|
+
.filter((r) => calendarOverlaps(r.event_date, from, to))
|
|
2602
|
+
.sort((a, b) => compareCalendarDates(a.event_date, b.event_date) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
|
|
2603
|
+
.map((r) => ({ id: r.id, eventDate: r.event_date }));
|
|
2604
|
+
}
|
|
2605
|
+
|
|
2606
|
+
/** A memory's feelings, oldest first (then in the order written). A removed memory has none. */
|
|
2607
|
+
feelingsFor(memoryId: string): FeelingRow[] {
|
|
2608
|
+
return this.ops.all<FeelingRow>(
|
|
2609
|
+
"SELECT * FROM feelings WHERE memory_id = ? ORDER BY created_at, rowid",
|
|
2610
|
+
memoryId,
|
|
2611
|
+
);
|
|
2612
|
+
}
|
|
2613
|
+
|
|
2614
|
+
/**
|
|
2615
|
+
* How many feelings, by `whose` and by `core` or `emotion` (an `other` counts
|
|
2616
|
+
* under `other`), recorded from `from` to `to` — the person's calendar days,
|
|
2617
|
+
* inclusive, read in `zone()`; either end may be left open. Counts only: no
|
|
2618
|
+
* word of any feeling leaves here.
|
|
2619
|
+
*/
|
|
2620
|
+
feelingCounts(
|
|
2621
|
+
opts: { by: "core" | "emotion"; whose?: string; from?: string; to?: string },
|
|
2622
|
+
): { whose: string; key: string; count: number }[] {
|
|
2623
|
+
const col = opts.by === "core" ? "core" : "emotion";
|
|
2624
|
+
const rows = this.ops.all<{ whose: string; key: string; created_at: number }>(
|
|
2625
|
+
`SELECT whose, ${col} AS key, created_at FROM feelings ${opts.whose === undefined ? "" : "WHERE whose = ?"}`,
|
|
2626
|
+
...(opts.whose === undefined ? [] : [opts.whose]),
|
|
2627
|
+
);
|
|
2628
|
+
const zone = this.zone();
|
|
2629
|
+
const tally = new Map<string, { whose: string; key: string; count: number }>();
|
|
2630
|
+
for (const r of rows) {
|
|
2631
|
+
if (opts.from !== undefined || opts.to !== undefined) {
|
|
2632
|
+
const day = localDate(r.created_at, zone);
|
|
2633
|
+
if (opts.from !== undefined && day < opts.from) continue;
|
|
2634
|
+
if (opts.to !== undefined && day > opts.to) continue;
|
|
2635
|
+
}
|
|
2636
|
+
const k = `${r.whose}\u0000${r.key}`;
|
|
2637
|
+
const t = tally.get(k) ?? { whose: r.whose, key: r.key, count: 0 };
|
|
2638
|
+
t.count += 1;
|
|
2639
|
+
tally.set(k, t);
|
|
2640
|
+
}
|
|
2641
|
+
return [...tally.values()].sort((a, b) => b.count - a.count || (a.whose + a.key < b.whose + b.key ? -1 : 1));
|
|
2642
|
+
}
|
|
2643
|
+
|
|
2380
2644
|
versions(id: string): VersionRow[] {
|
|
2381
2645
|
return this.ops.all<VersionRow>(
|
|
2382
2646
|
"SELECT * FROM versions WHERE memory_id = ? ORDER BY seq",
|
|
@@ -2648,16 +2912,21 @@ export class Store {
|
|
|
2648
2912
|
if (input.origin.spanHash !== undefined) origin["spanHash"] = input.origin.spanHash;
|
|
2649
2913
|
if (Object.keys(origin).length > 0) meta["origin"] = origin;
|
|
2650
2914
|
}
|
|
2915
|
+
const eventDate = input.eventDate === undefined ? undefined : assertEventDate(input.eventDate, id);
|
|
2916
|
+
// ONE MOMENT for the row, and `learned_on` is its local date unless the
|
|
2917
|
+
// caller dated the memory itself (mint dates a deposit by its own instant).
|
|
2918
|
+
const at = this.nowFn();
|
|
2651
2919
|
const doc: ProseDoc = {
|
|
2652
2920
|
id,
|
|
2653
2921
|
type: input.type,
|
|
2654
|
-
learnedOn: input.learnedOn ?? this.
|
|
2922
|
+
learnedOn: input.learnedOn ?? localDate(at, this.zone()),
|
|
2655
2923
|
bornDay: input.physics?.birthDay ?? day,
|
|
2656
2924
|
meta,
|
|
2657
2925
|
body,
|
|
2658
2926
|
};
|
|
2659
2927
|
if (input.title !== undefined) doc.title = input.title;
|
|
2660
2928
|
if (input.happenedOn !== undefined) doc.happenedOn = input.happenedOn;
|
|
2929
|
+
if (eventDate !== undefined) doc.eventDate = eventDate;
|
|
2661
2930
|
// Serialized (and so G6-checked) BEFORE the INSERT, for the same reason:
|
|
2662
2931
|
// a function or a NaN in `meta` is silent data loss, and the refusal must
|
|
2663
2932
|
// land while nothing has been written.
|
|
@@ -2677,8 +2946,8 @@ export class Store {
|
|
|
2677
2946
|
promoted_identity, protected, pressure, last_challenged_day, archived,
|
|
2678
2947
|
archived_reason, superseded_by, revision, content_hash,
|
|
2679
2948
|
learned_on, happened_on, source, origin_session, origin_scope, origin_ref,
|
|
2680
|
-
title, body, meta, confidential)
|
|
2681
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, NULL, NULL, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
2949
|
+
title, body, meta, confidential, created_at, updated_at, model, event_date)
|
|
2950
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, NULL, NULL, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
2682
2951
|
id,
|
|
2683
2952
|
input.type,
|
|
2684
2953
|
input.kind,
|
|
@@ -2709,6 +2978,10 @@ export class Store {
|
|
|
2709
2978
|
doc.body,
|
|
2710
2979
|
metaJson,
|
|
2711
2980
|
confidentialByMeta(meta) ? 1 : 0,
|
|
2981
|
+
at,
|
|
2982
|
+
at,
|
|
2983
|
+
modelOrNull(input.model),
|
|
2984
|
+
doc.eventDate ?? null,
|
|
2712
2985
|
);
|
|
2713
2986
|
return doc;
|
|
2714
2987
|
}
|
|
@@ -2745,6 +3018,7 @@ export class Store {
|
|
|
2745
3018
|
};
|
|
2746
3019
|
if (row.title !== null) doc.title = row.title;
|
|
2747
3020
|
if (row.happened_on !== null) doc.happenedOn = row.happened_on;
|
|
3021
|
+
if (row.event_date !== null) doc.eventDate = row.event_date;
|
|
2748
3022
|
return doc;
|
|
2749
3023
|
}
|
|
2750
3024
|
|
|
@@ -2771,6 +3045,7 @@ export class Store {
|
|
|
2771
3045
|
};
|
|
2772
3046
|
if (version.title !== null) doc.title = version.title;
|
|
2773
3047
|
if (version.happened_on !== null) doc.happenedOn = version.happened_on;
|
|
3048
|
+
if (version.event_date !== null) doc.eventDate = version.event_date;
|
|
2774
3049
|
return doc;
|
|
2775
3050
|
}
|
|
2776
3051
|
|
|
@@ -3006,29 +3281,48 @@ export function newId(type: ProseType): string {
|
|
|
3006
3281
|
}
|
|
3007
3282
|
|
|
3008
3283
|
/**
|
|
3009
|
-
* The calendar date an INSTANT falls on
|
|
3284
|
+
* The UTC calendar date an INSTANT falls on.
|
|
3010
3285
|
*
|
|
3011
|
-
*
|
|
3012
|
-
*
|
|
3013
|
-
*
|
|
3014
|
-
*
|
|
3015
|
-
*
|
|
3016
|
-
*
|
|
3286
|
+
* This WAS the provenance clock's arithmetic, UTC by deliberate choice while
|
|
3287
|
+
* every other date in the codebase was UTC too (NOTES 2026-09-05). Since
|
|
3288
|
+
* docs/time.md (2026-09-25) a person's day is LOCAL and is `time.ts#localDate`
|
|
3289
|
+
* (a store: `store.today()`, `localDate(at, store.zone())`). What is left here
|
|
3290
|
+
* is the UTC reading, kept under its old name for the callers that mean UTC on
|
|
3291
|
+
* purpose — reading a pre-v7 row's `learned_on`, the tools under `tools/`, and
|
|
3292
|
+
* the dashboard's `todayUtc` — so none of them changes meaning silently.
|
|
3017
3293
|
*
|
|
3018
|
-
* NOT `physics/clock.ts#dayKey
|
|
3019
|
-
* is a physics idea — "which lived day does this activity belong to" — and a
|
|
3020
|
-
* memory taken at 01:30 was taken on the 14th no matter which lived day it counts
|
|
3021
|
-
* toward. Provenance does not get the boundary shift.
|
|
3294
|
+
* NOT `physics/clock.ts#dayKey`: that shifts by the BOUNDARY HOUR, a physics idea.
|
|
3022
3295
|
*/
|
|
3023
3296
|
export function dateOf(at: number): string {
|
|
3024
|
-
return
|
|
3297
|
+
return utcDate(at);
|
|
3025
3298
|
}
|
|
3026
3299
|
|
|
3027
|
-
/** The ambient date
|
|
3300
|
+
/** The ambient UTC date (see `dateOf`). A store's person's day is `store.today()`. */
|
|
3028
3301
|
export function today(): string {
|
|
3029
|
-
return
|
|
3302
|
+
return utcDate(Date.now());
|
|
3303
|
+
}
|
|
3304
|
+
|
|
3305
|
+
/** The v7 column's screen: a model id the host reported, or NULL. */
|
|
3306
|
+
function modelOrNull(model: string | undefined): string | null {
|
|
3307
|
+
return model !== undefined && isModelId(model) ? model : null;
|
|
3308
|
+
}
|
|
3309
|
+
|
|
3310
|
+
/** An event date that cannot be read is a reminder that never comes up: refused
|
|
3311
|
+
* by name. Returns the date as it will be STORED — trimmed (review N5). */
|
|
3312
|
+
function assertEventDate(date: string, id: string): string {
|
|
3313
|
+
if (!isCalendarDate(date)) throw new StoreError("EVENT_DATE_INVALID", { id, date: String(date).slice(0, 64) });
|
|
3314
|
+
return date.trim();
|
|
3030
3315
|
}
|
|
3031
3316
|
|
|
3317
|
+
/**
|
|
3318
|
+
* `link` / `linkMany`: an UPSERT, not `INSERT OR REPLACE` — a replace deletes
|
|
3319
|
+
* the row first and would reset `created_at` on every re-weighting (v7).
|
|
3320
|
+
*/
|
|
3321
|
+
const EDGE_UPSERT = `INSERT INTO edges (src, dst, weight, last_day, created_at, updated_at)
|
|
3322
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
3323
|
+
ON CONFLICT (src, dst) DO UPDATE SET
|
|
3324
|
+
weight = excluded.weight, last_day = excluded.last_day, updated_at = excluded.updated_at`;
|
|
3325
|
+
|
|
3032
3326
|
/**
|
|
3033
3327
|
* True when a data dir has already been initialized (used by adapters, not writes).
|
|
3034
3328
|
*
|