counterparts 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +153 -0
- package/docs/module-map.md +3 -2
- package/package.json +1 -1
- package/src/adapters/claude-code/NOTES.md +28 -0
- package/src/adapters/claude-code/bin/hook.ts +85 -22
- package/src/adapters/claude-code/config.ts +7 -0
- package/src/adapters/claude-code/doctor.ts +166 -4
- package/src/adapters/claude-code/hooks.ts +185 -6
- package/src/adapters/claude-code/index.ts +3 -0
- package/src/adapters/claude-code/transcript.ts +7 -0
- package/src/adapters/cli/CONTRACT.md +11 -0
- package/src/adapters/cli/INTERFACE-GAPS.md +16 -0
- package/src/adapters/cli/NOTES.md +25 -0
- package/src/adapters/cli/commands.ts +184 -4
- package/src/adapters/cli/dream-core.ts +126 -0
- package/src/adapters/cli/help.ts +17 -0
- package/src/adapters/cli/mechanisms.ts +199 -106
- package/src/adapters/dashboard/CONTRACT.md +5 -0
- package/src/adapters/dashboard/INTERFACE-GAPS.md +12 -0
- package/src/adapters/dashboard/NOTES.md +38 -0
- package/src/adapters/dashboard/registries.ts +35 -1
- package/src/adapters/dashboard/web/README.md +63 -26
- package/src/adapters/dashboard/web/app.html +2 -0
- package/src/adapters/dashboard/web/flow.ts +14 -0
- package/src/adapters/dashboard/web/lanes.ts +141 -0
- package/src/adapters/dashboard/web/mechanisms/association/index.js +1 -2
- package/src/adapters/dashboard/web/mechanisms/consolidation/index.js +8 -5
- package/src/adapters/dashboard/web/mechanisms/consolidation/panel.js +7 -7
- package/src/adapters/dashboard/web/mechanisms/decay/index.js +1 -2
- package/src/adapters/dashboard/web/mechanisms/dreaming/index.js +19 -0
- package/src/adapters/dashboard/web/mechanisms/emotional/index.js +9 -6
- package/src/adapters/dashboard/web/mechanisms/episodic-semantic/index.js +4 -5
- package/src/adapters/dashboard/web/mechanisms/index.js +5 -3
- package/src/adapters/dashboard/web/mechanisms/interference/index.js +1 -2
- package/src/adapters/dashboard/web/mechanisms/prospective/index.js +7 -5
- package/src/adapters/dashboard/web/mechanisms/reconsolidation/index.js +1 -2
- package/src/adapters/dashboard/web/mechanisms/regions.js +1 -1
- package/src/adapters/dashboard/web/mechanisms/retrieval/index.js +1 -2
- package/src/adapters/dashboard/web/mechanisms/salience/index.js +1 -2
- package/src/adapters/dashboard/web/mechanisms/schema/index.js +1 -2
- package/src/adapters/dashboard/web/narrate.ts +76 -13
- package/src/adapters/dashboard/web/pages/health/sections/checks.js +17 -1
- package/src/adapters/dashboard/web/pages/home/home.css +29 -13
- package/src/adapters/dashboard/web/pages/home/index.js +2 -0
- package/src/adapters/dashboard/web/pages/home/sections/explorer.js +22 -7
- package/src/adapters/dashboard/web/pages/home/sections/hero.js +15 -3
- package/src/adapters/dashboard/web/pages/home/sections/live-activity.js +15 -9
- package/src/adapters/dashboard/web/pages/home/sections/recent-chapters.js +6 -2
- package/src/adapters/dashboard/web/pages/home/sections/tiles.js +35 -18
- package/src/adapters/dashboard/web/pages/memories/index.js +30 -36
- package/src/adapters/dashboard/web/pages/memories/memories.css +97 -72
- package/src/adapters/dashboard/web/pages/memories/row.js +51 -0
- package/src/adapters/dashboard/web/pages/memories/sections/feel.js +176 -0
- package/src/adapters/dashboard/web/pages/memories/sections/hold.js +67 -0
- package/src/adapters/dashboard/web/pages/memories/sections/list.js +65 -43
- package/src/adapters/dashboard/web/pages/memories/sections/search.js +26 -23
- package/src/adapters/dashboard/web/pages/memories/state.js +12 -5
- package/src/adapters/dashboard/web/pages/self/index.js +57 -16
- package/src/adapters/dashboard/web/pages/self/sections/dreams.js +76 -0
- package/src/adapters/dashboard/web/pages/self/sections/journal.js +75 -19
- package/src/adapters/dashboard/web/pages/self/sections/page.js +108 -65
- package/src/adapters/dashboard/web/pages/self/sections/settling.js +129 -104
- package/src/adapters/dashboard/web/pages/self/sections/wake.js +31 -15
- package/src/adapters/dashboard/web/pages/self/self.css +122 -59
- package/src/adapters/dashboard/web/pages/self/state.js +21 -0
- package/src/adapters/dashboard/web/server.ts +9 -0
- package/src/adapters/dashboard/web/shared/memory-marks.css +17 -0
- package/src/adapters/dashboard/web/shared/memory-marks.js +81 -0
- package/src/adapters/dashboard/web/shared/memory-modal.js +240 -53
- package/src/adapters/dashboard/web/shared/modal.css +67 -0
- package/src/adapters/dashboard/web/shared/tip.js +1 -1
- package/src/adapters/dashboard/web/shared/widgets/feed.css +4 -0
- package/src/adapters/dashboard/web/shared/widgets/feed.js +56 -14
- package/src/adapters/dashboard/web/shared/widgets/light.css +1 -0
- package/src/adapters/dashboard/web/shared/widgets/light.js +7 -5
- package/src/adapters/dashboard/web/shared/widgets/tips.css +19 -0
- package/src/adapters/dashboard/web/shared/widgets/tips.js +56 -0
- package/src/adapters/dashboard/web/views/activity.ts +18 -2
- package/src/adapters/dashboard/web/views/archive-words.ts +27 -10
- package/src/adapters/dashboard/web/views/dreams.ts +199 -0
- package/src/adapters/dashboard/web/views/mechanism-panel.ts +116 -35
- package/src/adapters/dashboard/web/views/mechanisms.ts +81 -255
- package/src/adapters/dashboard/web/views/memories.ts +245 -43
- package/src/adapters/dashboard/web/views/memory-words.ts +133 -0
- package/src/adapters/dashboard/web/views/memory.ts +226 -4
- package/src/adapters/dashboard/web/views/mind.ts +261 -61
- package/src/adapters/dashboard/web/views/overview.ts +80 -42
- package/src/adapters/dashboard/web/views/search.ts +35 -1
- package/src/adapters/dashboard/web/views/shared.ts +14 -0
- package/src/adapters/dashboard/web/views.ts +2 -0
- package/src/adapters/feelings-line.ts +74 -0
- package/src/adapters/fired.ts +163 -23
- package/src/adapters/mcp/CONTRACT.md +8 -0
- package/src/adapters/mcp/NOTES.md +56 -0
- package/src/adapters/mcp/server.ts +254 -6
- package/src/adapters/mcp/tools.ts +180 -5
- package/src/adapters/mechanism-evidence.ts +465 -0
- package/src/core/bridge.ts +21 -16
- package/src/core/counterpart.ts +422 -10
- package/src/core/dream/CONTRACT.md +143 -0
- package/src/core/dream/INTERFACE-GAPS.md +24 -0
- package/src/core/dream/NOTES.md +84 -0
- package/src/core/dream/index.ts +1067 -0
- package/src/core/dream/mark.ts +14 -0
- package/src/core/dream/tunables.ts +56 -0
- package/src/core/encode/CONTRACT.md +7 -1
- package/src/core/feelings-wheel.ts +171 -13
- package/src/core/mint.ts +9 -0
- package/src/core/physics/CONTRACT.md +132 -21
- package/src/core/physics/NOTES.md +233 -0
- package/src/core/physics/index.ts +419 -50
- package/src/core/prospective/CONTRACT.md +35 -6
- package/src/core/prospective/INTERFACE-GAPS.md +10 -5
- 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 +104 -46
- package/src/core/recall/CONTRACT.md +28 -0
- package/src/core/recall/NOTES.md +55 -0
- package/src/core/recall/activate.ts +32 -5
- package/src/core/recall/gate.ts +7 -1
- package/src/core/recall/index.ts +28 -2
- package/src/core/recall/mood.ts +106 -0
- package/src/core/recall/tunables.ts +26 -0
- package/src/core/remember/CONTRACT.md +5 -0
- package/src/core/remember/index.ts +1 -0
- package/src/core/remember/proposals.ts +86 -3
- package/src/core/remember/spans.ts +11 -1
- package/src/core/self/CONTRACT.md +3 -0
- package/src/core/self/INTERFACE-GAPS.md +7 -0
- package/src/core/self/NOTES.md +30 -0
- package/src/core/self/identity.ts +115 -6
- package/src/core/self/index.ts +17 -0
- package/src/core/self/tunables.ts +17 -0
- package/src/core/sleep/CONTRACT.md +17 -1
- package/src/core/sleep/INTERFACE-GAPS.md +19 -0
- package/src/core/sleep/NOTES.md +36 -0
- package/src/core/sleep/consolidate.ts +149 -96
- package/src/core/sleep/decay.ts +11 -0
- package/src/core/sleep/index.ts +4 -1
- package/src/core/sleep/tunables.ts +9 -0
- package/src/core/sleep/types.ts +17 -0
- package/src/core/sleep/upgrade.ts +168 -0
- package/src/core/store/CONTRACT.md +21 -2
- package/src/core/store/NOTES.md +68 -0
- package/src/core/store/feelings.ts +23 -6
- package/src/core/store/index.ts +716 -17
- package/src/core/store/operational.ts +329 -4
- package/src/core/store/owner-op-seam.ts +79 -1
- package/src/core/types.ts +37 -0
- package/src/adapters/dashboard/web/pages/memories/sections/constellation.js +0 -178
- package/src/adapters/dashboard/web/pages/memories/sections/kinds.js +0 -42
package/src/core/store/index.ts
CHANGED
|
@@ -54,8 +54,8 @@ import {
|
|
|
54
54
|
} from "../time.js";
|
|
55
55
|
import { checkFeelings } from "./feelings.js";
|
|
56
56
|
import type { AddFeelingsResult, FeelingInput, FeelingRow } from "./feelings.js";
|
|
57
|
-
import { creditUse } from "../physics/index.js";
|
|
58
|
-
import type { CreditOutcome, UseTier } from "../physics/index.js";
|
|
57
|
+
import { creditReturn, creditUse } from "../physics/index.js";
|
|
58
|
+
import type { CreditOutcome, ReturnOutcome, UseTier } from "../physics/index.js";
|
|
59
59
|
import type { Db, Statement, WalFold } from "./db.js";
|
|
60
60
|
import { foldWal, isLocked, wroteOn } from "./db.js";
|
|
61
61
|
import { StoreError } from "./errors.js";
|
|
@@ -69,15 +69,22 @@ import {
|
|
|
69
69
|
rowTombstoned,
|
|
70
70
|
} from "./operational.js";
|
|
71
71
|
import type {
|
|
72
|
+
CoreEventRow,
|
|
73
|
+
DreamAskRow,
|
|
74
|
+
DreamChangeRow,
|
|
75
|
+
DreamRow,
|
|
72
76
|
EdgeRow,
|
|
73
77
|
EventRow,
|
|
78
|
+
FeelingPeak,
|
|
74
79
|
GateSessionRow,
|
|
75
80
|
MemoryRow,
|
|
76
81
|
MigrationNote,
|
|
77
82
|
ProspectiveRow,
|
|
78
83
|
RemovalRow,
|
|
84
|
+
ReturnRow,
|
|
79
85
|
TombstoneRow,
|
|
80
86
|
VersionRow,
|
|
87
|
+
WakeDisplayRow,
|
|
81
88
|
} from "./operational.js";
|
|
82
89
|
import { grantOwnerOps } from "./owner-op-seam.js";
|
|
83
90
|
import {
|
|
@@ -102,6 +109,7 @@ import {
|
|
|
102
109
|
import type { ProseDoc, ProseType } from "./prose.js";
|
|
103
110
|
import {
|
|
104
111
|
DEFAULT_LENGTH_NORM,
|
|
112
|
+
decodeVector,
|
|
105
113
|
deindexDoc,
|
|
106
114
|
docFrequency,
|
|
107
115
|
embeddingCount,
|
|
@@ -129,7 +137,14 @@ export * from "./render.js";
|
|
|
129
137
|
export * from "./feelings.js";
|
|
130
138
|
export type { Db, Statement, WalFold } from "./db.js";
|
|
131
139
|
export type {
|
|
140
|
+
CoreEventRow,
|
|
141
|
+
DreamAskRow,
|
|
142
|
+
DreamChangeRow,
|
|
143
|
+
DreamRow,
|
|
144
|
+
ReturnRow,
|
|
145
|
+
WakeDisplayRow,
|
|
132
146
|
MemoryRow,
|
|
147
|
+
FeelingPeak,
|
|
133
148
|
VersionRow,
|
|
134
149
|
EdgeRow,
|
|
135
150
|
EventRow,
|
|
@@ -156,6 +171,7 @@ export {
|
|
|
156
171
|
OBSERVER_READ_FLOOR,
|
|
157
172
|
pendingMigration,
|
|
158
173
|
SCHEMA_VERSION,
|
|
174
|
+
V8_UPGRADE_KEY,
|
|
159
175
|
rowToPhysics,
|
|
160
176
|
rowTombstoned,
|
|
161
177
|
} from "./operational.js";
|
|
@@ -472,7 +488,9 @@ export interface ProspectiveInput {
|
|
|
472
488
|
memoryId: string;
|
|
473
489
|
windowKey: string;
|
|
474
490
|
eventDate: string;
|
|
475
|
-
|
|
491
|
+
/** `range` since 2026-09-26 (prospective windows.ts) — the column is TEXT, so
|
|
492
|
+
* this widens the type and nothing on disk. */
|
|
493
|
+
precision: "day" | "month" | "year" | "range";
|
|
476
494
|
state: "armed" | "fired" | "suppressed" | "expired";
|
|
477
495
|
fires?: number;
|
|
478
496
|
lastFiredDay?: number | null;
|
|
@@ -649,6 +667,21 @@ export const WRITE_METHODS = [
|
|
|
649
667
|
"rebuildCache",
|
|
650
668
|
"pruneDeadIndex",
|
|
651
669
|
"embedOne",
|
|
670
|
+
// v8 (2026-09-26, dreaming + consolidation).
|
|
671
|
+
"replayReturn",
|
|
672
|
+
"recordHintDisplay",
|
|
673
|
+
"supersedeInto",
|
|
674
|
+
"restoreSuperseded",
|
|
675
|
+
"restoreEdge",
|
|
676
|
+
"retractFeelings",
|
|
677
|
+
"retractDreamReturns",
|
|
678
|
+
"retractDreamNominations",
|
|
679
|
+
"appendCoreEvent",
|
|
680
|
+
"openDream",
|
|
681
|
+
"updateDream",
|
|
682
|
+
"recordDreamChange",
|
|
683
|
+
"markDreamChangeUndone",
|
|
684
|
+
"setDreamAsk",
|
|
652
685
|
] as const;
|
|
653
686
|
|
|
654
687
|
export type WriteMethod = (typeof WRITE_METHODS)[number];
|
|
@@ -1567,11 +1600,34 @@ export class Store {
|
|
|
1567
1600
|
* verdict absolutely and adds no opinion of its own. One rule, one owner:
|
|
1568
1601
|
* a second implementation of this arithmetic is exactly the divergence that
|
|
1569
1602
|
* produced v1's "two clocks" fiction.
|
|
1603
|
+
*
|
|
1604
|
+
* v8 (2026-09-26): a credited use is ALSO asked whether it is a RETURN
|
|
1605
|
+
* (`physics.creditReturn`, §5.11) — in the same transaction, with the one fact
|
|
1606
|
+
* only the store holds: whether the memory was SHOWING in the wake's hints
|
|
1607
|
+
* lane on this day (`wake_display`). A return is recorded as a `returns` row
|
|
1608
|
+
* and the aggregate columns are recomputed from the table. The use itself is
|
|
1609
|
+
* credited exactly as before either way.
|
|
1570
1610
|
*/
|
|
1571
|
-
reinforce(
|
|
1611
|
+
reinforce(
|
|
1612
|
+
id: string,
|
|
1613
|
+
day: number,
|
|
1614
|
+
tier: UseTier = "referenced",
|
|
1615
|
+
opts: {
|
|
1616
|
+
/**
|
|
1617
|
+
* The use came from recall SURFACING the memory on this turn's own cue
|
|
1618
|
+
* (a quoted loud candidate), not from an id the model could have read
|
|
1619
|
+
* off the wake: organic whatever the hints lane was showing, so the
|
|
1620
|
+
* display check is skipped. Absent — every other caller — the display
|
|
1621
|
+
* decides.
|
|
1622
|
+
*/
|
|
1623
|
+
cued?: boolean;
|
|
1624
|
+
} = {},
|
|
1625
|
+
): CreditOutcome & { ret: ReturnOutcome | null } {
|
|
1572
1626
|
const outcome = this.mutate("reinforce", () => {
|
|
1573
1627
|
const row = this.requireRow(id);
|
|
1574
|
-
const
|
|
1628
|
+
const physics = rowToPhysics(row);
|
|
1629
|
+
const verdict = creditUse(physics, day, tier);
|
|
1630
|
+
let ret: ReturnOutcome | null = null;
|
|
1575
1631
|
if (verdict.credited) {
|
|
1576
1632
|
this.ops.run(
|
|
1577
1633
|
`UPDATE memories
|
|
@@ -1582,18 +1638,565 @@ export class Store {
|
|
|
1582
1638
|
verdict.next.reinforcedDays,
|
|
1583
1639
|
id,
|
|
1584
1640
|
);
|
|
1641
|
+
ret = creditReturn(physics, day, {
|
|
1642
|
+
source: "awake",
|
|
1643
|
+
tierWeight: verdict.w,
|
|
1644
|
+
onDisplay: opts.cued !== true && this.shownInHints(id, day),
|
|
1645
|
+
since: this.legacyLastReturn(id),
|
|
1646
|
+
});
|
|
1647
|
+
if (ret.counted) this.writeReturn(id, day, ret, null);
|
|
1585
1648
|
}
|
|
1586
|
-
return verdict;
|
|
1649
|
+
return { ...verdict, ret };
|
|
1587
1650
|
});
|
|
1588
1651
|
this.emit("store.reinforce", id, {
|
|
1589
1652
|
credited: outcome.credited,
|
|
1590
1653
|
reason: outcome.reason,
|
|
1591
1654
|
day,
|
|
1592
1655
|
tier,
|
|
1656
|
+
returned: outcome.ret?.counted ?? false,
|
|
1657
|
+
returnReason: outcome.ret?.reason ?? null,
|
|
1593
1658
|
});
|
|
1594
1659
|
return outcome;
|
|
1595
1660
|
}
|
|
1596
1661
|
|
|
1662
|
+
/**
|
|
1663
|
+
* A DREAM REPLAY (physics §5.11): a return worth `DREAM_RETURN_WEIGHT` of an
|
|
1664
|
+
* awake one, spacing-scaled, at most once per dream per memory — and never a
|
|
1665
|
+
* USE: `uses`, `lastUsedDay` and the rep arm are untouched, and the core
|
|
1666
|
+
* lanes do not count it. Tagged with the dream's id so undoing the dream
|
|
1667
|
+
* removes it (`retractDreamReturns`).
|
|
1668
|
+
*/
|
|
1669
|
+
replayReturn(id: string, day: number, dreamId: string): ReturnOutcome {
|
|
1670
|
+
const outcome = this.mutate("replayReturn", () => {
|
|
1671
|
+
const row = this.requireRow(id);
|
|
1672
|
+
const ret = creditReturn(rowToPhysics(row), day, { source: "dream", since: this.legacyLastReturn(id) });
|
|
1673
|
+
if (ret.counted) this.writeReturn(id, day, ret, dreamId);
|
|
1674
|
+
return ret;
|
|
1675
|
+
});
|
|
1676
|
+
this.emit("store.replay", id, { counted: outcome.counted, reason: outcome.reason, day, dream: dreamId });
|
|
1677
|
+
return outcome;
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
/** The last day of a LEGACY return the upgrade credited (spacing reads it; the lanes never do). */
|
|
1681
|
+
private legacyLastReturn(id: string): number | null {
|
|
1682
|
+
return (
|
|
1683
|
+
this.ops.get<{ d: number | null }>("SELECT MAX(day) AS d FROM returns WHERE memory_id = ? AND source = 'legacy'", id)?.d ??
|
|
1684
|
+
null
|
|
1685
|
+
);
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
/** Insert one `returns` row and bring the aggregate columns in line with the table. */
|
|
1689
|
+
private writeReturn(id: string, day: number, ret: ReturnOutcome, dreamId: string | null): void {
|
|
1690
|
+
this.ops.run(
|
|
1691
|
+
`INSERT OR IGNORE INTO returns (memory_id, day, source, weight, gap, dream_id, at)
|
|
1692
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
1693
|
+
id,
|
|
1694
|
+
day,
|
|
1695
|
+
ret.source,
|
|
1696
|
+
ret.weight,
|
|
1697
|
+
Number.isFinite(ret.gap) ? ret.gap : 0,
|
|
1698
|
+
dreamId,
|
|
1699
|
+
this.nowFn(),
|
|
1700
|
+
);
|
|
1701
|
+
this.recomputeReturns(id);
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
/**
|
|
1705
|
+
* The aggregate columns FROM the `returns` table — the one place they are
|
|
1706
|
+
* written, so a removed dream's replays or a carried history can never leave
|
|
1707
|
+
* the physics disagreeing with its own evidence.
|
|
1708
|
+
*/
|
|
1709
|
+
private recomputeReturns(id: string): void {
|
|
1710
|
+
const agg = this.ops.get<{
|
|
1711
|
+
total: number | null;
|
|
1712
|
+
days: number | null;
|
|
1713
|
+
first: number | null;
|
|
1714
|
+
last: number | null;
|
|
1715
|
+
dream: number | null;
|
|
1716
|
+
}>(
|
|
1717
|
+
`SELECT SUM(weight) AS total,
|
|
1718
|
+
COUNT(DISTINCT CASE WHEN source = 'awake' THEN day END) AS days,
|
|
1719
|
+
MIN(CASE WHEN source = 'awake' THEN day END) AS first,
|
|
1720
|
+
MAX(CASE WHEN source = 'awake' THEN day END) AS last,
|
|
1721
|
+
MAX(CASE WHEN source = 'dream' THEN day END) AS dream
|
|
1722
|
+
FROM returns WHERE memory_id = ?`,
|
|
1723
|
+
id,
|
|
1724
|
+
);
|
|
1725
|
+
this.ops.run(
|
|
1726
|
+
`UPDATE memories
|
|
1727
|
+
SET returns = ?, return_days = ?, first_return_day = ?, last_return_day = ?, last_dream_day = ?
|
|
1728
|
+
WHERE id = ?`,
|
|
1729
|
+
agg?.total ?? 0,
|
|
1730
|
+
agg?.days ?? 0,
|
|
1731
|
+
agg?.first ?? null,
|
|
1732
|
+
agg?.last ?? null,
|
|
1733
|
+
agg?.dream ?? null,
|
|
1734
|
+
id,
|
|
1735
|
+
);
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
/** One memory's counted returns, oldest first. A read. */
|
|
1739
|
+
returnsOf(id: string): ReturnRow[] {
|
|
1740
|
+
return this.ops.all<ReturnRow>("SELECT * FROM returns WHERE memory_id = ? ORDER BY day, source", id);
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
/**
|
|
1744
|
+
* Returns counted since a moment (UTC ms) or a lived day, by source — for
|
|
1745
|
+
* `fired`, the mechanism lights and the dashboard. With neither, all of them.
|
|
1746
|
+
*/
|
|
1747
|
+
returnCounts(filter: { sinceAt?: number; sinceDay?: number } = {}): { awake: number; dream: number; memories: number } {
|
|
1748
|
+
const r = this.ops.get<{ awake: number | null; dream: number | null; memories: number | null }>(
|
|
1749
|
+
`SELECT SUM(CASE WHEN source = 'awake' THEN 1 ELSE 0 END) AS awake,
|
|
1750
|
+
SUM(CASE WHEN source = 'dream' THEN 1 ELSE 0 END) AS dream,
|
|
1751
|
+
COUNT(DISTINCT memory_id) AS memories
|
|
1752
|
+
FROM returns WHERE at >= ? AND day >= ?`,
|
|
1753
|
+
filter.sinceAt ?? 0,
|
|
1754
|
+
filter.sinceDay ?? -2_147_483_648,
|
|
1755
|
+
);
|
|
1756
|
+
return { awake: r?.awake ?? 0, dream: r?.dream ?? 0, memories: r?.memories ?? 0 };
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
// ── what the wake's hints lane showed (v8) ─────────────────────────────────
|
|
1760
|
+
|
|
1761
|
+
/**
|
|
1762
|
+
* Was this memory SHOWING in the wake's hints lane on lived day `day`? True
|
|
1763
|
+
* from the first day of its current showing through the day a publish
|
|
1764
|
+
* dropped it: the day it is dropped is ambiguous (a session opened earlier
|
|
1765
|
+
* that day still read the old bundle), and #238's reading of that ambiguity —
|
|
1766
|
+
* count it as shown — is kept.
|
|
1767
|
+
*/
|
|
1768
|
+
shownInHints(id: string, day: number): boolean {
|
|
1769
|
+
const r = this.ops.get<WakeDisplayRow>("SELECT * FROM wake_display WHERE memory_id = ?", id);
|
|
1770
|
+
if (r === undefined || r.lane !== "hints") return false;
|
|
1771
|
+
if (day < r.first_day) return false;
|
|
1772
|
+
return r.closed_day === null || day <= r.closed_day;
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
/** Every display row, by memory id. A read (`self/`'s habituation reads it). */
|
|
1776
|
+
wakeDisplays(): Map<string, WakeDisplayRow> {
|
|
1777
|
+
const out = new Map<string, WakeDisplayRow>();
|
|
1778
|
+
for (const r of this.ops.all<WakeDisplayRow>("SELECT * FROM wake_display")) out.set(r.memory_id, r);
|
|
1779
|
+
return out;
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
/**
|
|
1783
|
+
* Record what a PUBLISHED bundle kept in its hints lane on lived day `day`:
|
|
1784
|
+
* each kept id with the habituation load `self/` computed for it. A kept id
|
|
1785
|
+
* already showing extends its showing; a new one opens one; every OTHER
|
|
1786
|
+
* showing row still open is closed on `day`. Ids that no longer resolve to a
|
|
1787
|
+
* row are skipped. One transaction.
|
|
1788
|
+
*/
|
|
1789
|
+
recordHintDisplay(day: number, kept: readonly { id: string; load: number }[]): void {
|
|
1790
|
+
this.mutate("recordHintDisplay", () => {
|
|
1791
|
+
const at = this.nowFn();
|
|
1792
|
+
const keptIds = new Set<string>();
|
|
1793
|
+
for (const k of kept) {
|
|
1794
|
+
const row = this.row(k.id);
|
|
1795
|
+
if (row === undefined) continue;
|
|
1796
|
+
keptIds.add(k.id);
|
|
1797
|
+
const prior = this.ops.get<WakeDisplayRow>("SELECT * FROM wake_display WHERE memory_id = ?", k.id);
|
|
1798
|
+
const open = prior !== undefined && prior.closed_day === null;
|
|
1799
|
+
this.ops.run(
|
|
1800
|
+
`INSERT INTO wake_display
|
|
1801
|
+
(memory_id, lane, first_day, shown_day, closed_day, load, ever_day, ever_uses, ever_last_used, updated_at)
|
|
1802
|
+
VALUES (?, 'hints', ?, ?, NULL, ?, ?, ?, ?, ?)
|
|
1803
|
+
ON CONFLICT (memory_id) DO UPDATE SET
|
|
1804
|
+
lane = 'hints', first_day = excluded.first_day, shown_day = excluded.shown_day,
|
|
1805
|
+
closed_day = NULL, load = excluded.load, updated_at = excluded.updated_at`,
|
|
1806
|
+
k.id,
|
|
1807
|
+
open ? prior.first_day : day,
|
|
1808
|
+
day,
|
|
1809
|
+
k.load,
|
|
1810
|
+
day,
|
|
1811
|
+
row.uses,
|
|
1812
|
+
row.last_used_day,
|
|
1813
|
+
at,
|
|
1814
|
+
);
|
|
1815
|
+
}
|
|
1816
|
+
for (const r of this.ops.all<WakeDisplayRow>("SELECT * FROM wake_display WHERE closed_day IS NULL")) {
|
|
1817
|
+
if (keptIds.has(r.memory_id)) continue;
|
|
1818
|
+
this.ops.run("UPDATE wake_display SET closed_day = ?, updated_at = ? WHERE memory_id = ?", day, at, r.memory_id);
|
|
1819
|
+
}
|
|
1820
|
+
});
|
|
1821
|
+
this.emit("store.display", undefined, { day, kept: kept.length });
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
// ── merges, and undoing them (v8, `core/dream/`) ───────────────────────────
|
|
1825
|
+
|
|
1826
|
+
/**
|
|
1827
|
+
* One original FOLDED INTO an already-written successor — a dream's merge of
|
|
1828
|
+
* two or three near-copies. The original keeps its id, its words (a version
|
|
1829
|
+
* row carries them, with the successor named) and a forwarding address, and
|
|
1830
|
+
* is archived `reason`; nothing is deleted. With `carryReturns`, its return
|
|
1831
|
+
* history is copied onto the successor (a return day both had is kept once),
|
|
1832
|
+
* so the merged memory is at least as durable as what it was made from.
|
|
1833
|
+
*/
|
|
1834
|
+
supersedeInto(oldId: string, successorId: string, reason: string, opts: { carryReturns?: boolean } = {}): void {
|
|
1835
|
+
this.mutate("supersedeInto", () => {
|
|
1836
|
+
const row = this.requireRow(oldId);
|
|
1837
|
+
this.requireRow(successorId);
|
|
1838
|
+
const at = this.nowFn();
|
|
1839
|
+
this.ops.run(
|
|
1840
|
+
`INSERT INTO versions
|
|
1841
|
+
(memory_id, seq, reason, version_day, archived_at, content_hash, successor_id,
|
|
1842
|
+
title, body, meta, learned_on, happened_on, created_at, model, event_date)
|
|
1843
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1844
|
+
oldId,
|
|
1845
|
+
row.revision + 1,
|
|
1846
|
+
reason,
|
|
1847
|
+
this.livedDay(),
|
|
1848
|
+
at,
|
|
1849
|
+
row.content_hash,
|
|
1850
|
+
successorId,
|
|
1851
|
+
row.title,
|
|
1852
|
+
row.body,
|
|
1853
|
+
row.meta,
|
|
1854
|
+
row.learned_on,
|
|
1855
|
+
row.happened_on,
|
|
1856
|
+
row.updated_at ?? row.created_at,
|
|
1857
|
+
row.model,
|
|
1858
|
+
row.event_date,
|
|
1859
|
+
);
|
|
1860
|
+
this.ops.run(
|
|
1861
|
+
`UPDATE memories SET superseded_by = ?, archived = 1, archived_reason = ?, revision = ?, updated_at = ?
|
|
1862
|
+
WHERE id = ?`,
|
|
1863
|
+
successorId,
|
|
1864
|
+
reason,
|
|
1865
|
+
row.revision + 1,
|
|
1866
|
+
at,
|
|
1867
|
+
oldId,
|
|
1868
|
+
);
|
|
1869
|
+
if (opts.carryReturns === true) {
|
|
1870
|
+
this.ops.run(
|
|
1871
|
+
`INSERT OR IGNORE INTO returns (memory_id, day, source, weight, gap, dream_id, at)
|
|
1872
|
+
SELECT ?, day, source, weight, gap, dream_id, at FROM returns WHERE memory_id = ?`,
|
|
1873
|
+
successorId,
|
|
1874
|
+
oldId,
|
|
1875
|
+
);
|
|
1876
|
+
this.recomputeReturns(successorId);
|
|
1877
|
+
}
|
|
1878
|
+
});
|
|
1879
|
+
deindexDoc(this.cache, oldId);
|
|
1880
|
+
this.emit("store.supersede", oldId, { successor: successorId, reason });
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
/**
|
|
1884
|
+
* The undo of `supersedeInto`: the original is live again — no forwarding
|
|
1885
|
+
* address, not archived — and back in the index. Its version row stays, as
|
|
1886
|
+
* history. Refused (a no-op) for a row that is not archived with `reason`.
|
|
1887
|
+
*/
|
|
1888
|
+
restoreSuperseded(oldId: string, reason: string): boolean {
|
|
1889
|
+
const restored = this.mutate("restoreSuperseded", () => {
|
|
1890
|
+
const row = this.requireRow(oldId);
|
|
1891
|
+
if (row.archived !== 1 || row.archived_reason !== reason) return false;
|
|
1892
|
+
this.ops.run(
|
|
1893
|
+
`UPDATE memories SET superseded_by = NULL, archived = 0, archived_reason = NULL, updated_at = ?
|
|
1894
|
+
WHERE id = ?`,
|
|
1895
|
+
this.nowFn(),
|
|
1896
|
+
oldId,
|
|
1897
|
+
);
|
|
1898
|
+
return true;
|
|
1899
|
+
});
|
|
1900
|
+
if (restored) {
|
|
1901
|
+
try {
|
|
1902
|
+
this.indexOne(this.readProse(oldId));
|
|
1903
|
+
} catch {
|
|
1904
|
+
/* a row that will not read stays out of the index; the restore stands */
|
|
1905
|
+
}
|
|
1906
|
+
this.emit("store.restore", oldId, { reason });
|
|
1907
|
+
}
|
|
1908
|
+
return restored;
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
/** Remove one edge, or put back the weight it had before (a dream's link, undone). */
|
|
1912
|
+
restoreEdge(src: string, dst: string, restore: { weight: number; day: number } | null = null): void {
|
|
1913
|
+
this.mutate("restoreEdge", () => {
|
|
1914
|
+
if (restore === null) {
|
|
1915
|
+
this.ops.run("DELETE FROM edges WHERE src = ? AND dst = ?", src, dst);
|
|
1916
|
+
} else {
|
|
1917
|
+
this.ops.run(
|
|
1918
|
+
"UPDATE edges SET weight = ?, last_day = ?, updated_at = ? WHERE src = ? AND dst = ?",
|
|
1919
|
+
restore.weight,
|
|
1920
|
+
restore.day,
|
|
1921
|
+
this.nowFn(),
|
|
1922
|
+
src,
|
|
1923
|
+
dst,
|
|
1924
|
+
);
|
|
1925
|
+
}
|
|
1926
|
+
});
|
|
1927
|
+
this.emit("store.edge.restored", src, { dst, restored: restore !== null });
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
/** Delete feelings by id (a dream's feeling-now, undone). */
|
|
1931
|
+
retractFeelings(ids: readonly string[]): number {
|
|
1932
|
+
const n = this.mutate("retractFeelings", () => {
|
|
1933
|
+
let count = 0;
|
|
1934
|
+
for (const id of ids) {
|
|
1935
|
+
this.ops.run("UPDATE feelings SET beneath_id = NULL WHERE beneath_id = ?", id);
|
|
1936
|
+
this.ops.run("DELETE FROM feelings WHERE id = ?", id);
|
|
1937
|
+
count += this.ops.get<{ n: number }>("SELECT changes() AS n")?.n ?? 0;
|
|
1938
|
+
}
|
|
1939
|
+
return count;
|
|
1940
|
+
});
|
|
1941
|
+
this.emit("store.feelings.retracted", undefined, { count: n });
|
|
1942
|
+
return n;
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
/** Delete a dream's replays and recompute each memory's returns from what is left. */
|
|
1946
|
+
retractDreamReturns(dreamId: string): number {
|
|
1947
|
+
const n = this.mutate("retractDreamReturns", () => {
|
|
1948
|
+
const ids = this.ops
|
|
1949
|
+
.all<{ memory_id: string }>("SELECT DISTINCT memory_id FROM returns WHERE dream_id = ?", dreamId)
|
|
1950
|
+
.map((r) => r.memory_id);
|
|
1951
|
+
this.ops.run("DELETE FROM returns WHERE dream_id = ?", dreamId);
|
|
1952
|
+
for (const id of ids) if (this.row(id) !== undefined) this.recomputeReturns(id);
|
|
1953
|
+
return ids.length;
|
|
1954
|
+
});
|
|
1955
|
+
this.emit("store.replay.retracted", undefined, { dream: dreamId, memories: n });
|
|
1956
|
+
return n;
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
// ── the core's history (v8) ────────────────────────────────────────────────
|
|
1960
|
+
|
|
1961
|
+
/** Append one core event: a promotion (with its lane), a demotion, a nomination. */
|
|
1962
|
+
appendCoreEvent(input: {
|
|
1963
|
+
memoryId: string;
|
|
1964
|
+
action: "promoted" | "demoted" | "nominated";
|
|
1965
|
+
day: number;
|
|
1966
|
+
lane?: string | null;
|
|
1967
|
+
reason?: string | null;
|
|
1968
|
+
dreamId?: string | null;
|
|
1969
|
+
actor?: string | null;
|
|
1970
|
+
}): number {
|
|
1971
|
+
const seq = this.mutate("appendCoreEvent", () => {
|
|
1972
|
+
this.ops.run(
|
|
1973
|
+
`INSERT INTO core_events (memory_id, action, day, at, lane, reason, dream_id, actor)
|
|
1974
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
1975
|
+
input.memoryId,
|
|
1976
|
+
input.action,
|
|
1977
|
+
input.day,
|
|
1978
|
+
this.nowFn(),
|
|
1979
|
+
input.lane ?? null,
|
|
1980
|
+
input.reason ?? null,
|
|
1981
|
+
input.dreamId ?? null,
|
|
1982
|
+
input.actor ?? null,
|
|
1983
|
+
);
|
|
1984
|
+
return this.ops.get<{ seq: number }>("SELECT last_insert_rowid() AS seq")?.seq ?? 0;
|
|
1985
|
+
});
|
|
1986
|
+
this.emit("store.core", input.memoryId, { action: input.action });
|
|
1987
|
+
return seq;
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
/** Core events, newest first, optionally for one memory / one action / since a moment. */
|
|
1991
|
+
coreEvents(filter: { memoryId?: string; action?: string; sinceAt?: number; limit?: number } = {}): CoreEventRow[] {
|
|
1992
|
+
const where: string[] = [];
|
|
1993
|
+
const args: (string | number)[] = [];
|
|
1994
|
+
if (filter.memoryId !== undefined) {
|
|
1995
|
+
where.push("memory_id = ?");
|
|
1996
|
+
args.push(filter.memoryId);
|
|
1997
|
+
}
|
|
1998
|
+
if (filter.action !== undefined) {
|
|
1999
|
+
where.push("action = ?");
|
|
2000
|
+
args.push(filter.action);
|
|
2001
|
+
}
|
|
2002
|
+
if (filter.sinceAt !== undefined) {
|
|
2003
|
+
where.push("at >= ?");
|
|
2004
|
+
args.push(filter.sinceAt);
|
|
2005
|
+
}
|
|
2006
|
+
const sql = `SELECT * FROM core_events${where.length > 0 ? ` WHERE ${where.join(" AND ")}` : ""} ORDER BY seq DESC LIMIT ?`;
|
|
2007
|
+
args.push(filter.limit ?? 1_000);
|
|
2008
|
+
return this.ops.all<CoreEventRow>(sql, ...args);
|
|
2009
|
+
}
|
|
2010
|
+
|
|
2011
|
+
/** Delete a dream's nominations (the undo of its `nominate-core` changes). */
|
|
2012
|
+
retractDreamNominations(dreamId: string): number {
|
|
2013
|
+
const n = this.mutate("retractDreamNominations", () => {
|
|
2014
|
+
this.ops.run("DELETE FROM core_events WHERE dream_id = ? AND action = 'nominated'", dreamId);
|
|
2015
|
+
return this.ops.get<{ n: number }>("SELECT changes() AS n")?.n ?? 0;
|
|
2016
|
+
});
|
|
2017
|
+
this.emit("store.core.retracted", undefined, { dream: dreamId, count: n });
|
|
2018
|
+
return n;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
/** True when the owner's latest word on this memory's core membership is a demotion. */
|
|
2022
|
+
coreDemoted(id: string): boolean {
|
|
2023
|
+
const last = this.ops.get<{ action: string }>(
|
|
2024
|
+
"SELECT action FROM core_events WHERE memory_id = ? AND action IN ('promoted', 'demoted') ORDER BY seq DESC LIMIT 1",
|
|
2025
|
+
id,
|
|
2026
|
+
);
|
|
2027
|
+
return last?.action === "demoted";
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
// ── dreams (v8, `core/dream/`) ─────────────────────────────────────────────
|
|
2031
|
+
|
|
2032
|
+
/** Open a dream: its row, state `begun`. */
|
|
2033
|
+
openDream(input: {
|
|
2034
|
+
id: string;
|
|
2035
|
+
session?: string | null;
|
|
2036
|
+
scope?: string | null;
|
|
2037
|
+
day: number;
|
|
2038
|
+
date?: string | null;
|
|
2039
|
+
model?: string | null;
|
|
2040
|
+
shown?: readonly string[];
|
|
2041
|
+
}): void {
|
|
2042
|
+
this.mutate("openDream", () => {
|
|
2043
|
+
this.ops.run(
|
|
2044
|
+
`INSERT INTO dreams (id, session, scope, day, date, state, started_at, model, shown)
|
|
2045
|
+
VALUES (?, ?, ?, ?, ?, 'begun', ?, ?, ?)`,
|
|
2046
|
+
input.id,
|
|
2047
|
+
input.session ?? null,
|
|
2048
|
+
input.scope ?? null,
|
|
2049
|
+
input.day,
|
|
2050
|
+
input.date ?? null,
|
|
2051
|
+
this.nowFn(),
|
|
2052
|
+
modelOrNull(input.model ?? undefined),
|
|
2053
|
+
JSON.stringify(input.shown ?? []),
|
|
2054
|
+
);
|
|
2055
|
+
});
|
|
2056
|
+
this.emit("store.dream", input.id, { state: "begun", day: input.day });
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
/** Change a dream's state, journal or title. `journaled` / `undone` stamp their moment. */
|
|
2060
|
+
updateDream(id: string, patch: { state?: "begun" | "journaled" | "undone"; title?: string | null; journal?: string | null }): void {
|
|
2061
|
+
this.mutate("updateDream", () => {
|
|
2062
|
+
const row = this.ops.get<DreamRow>("SELECT * FROM dreams WHERE id = ?", id);
|
|
2063
|
+
if (row === undefined) throw new StoreError("ID_UNKNOWN", { id });
|
|
2064
|
+
const at = this.nowFn();
|
|
2065
|
+
const state = patch.state ?? row.state;
|
|
2066
|
+
this.ops.run(
|
|
2067
|
+
`UPDATE dreams SET state = ?, title = ?, journal = ?, finished_at = ?, undone_at = ? WHERE id = ?`,
|
|
2068
|
+
state,
|
|
2069
|
+
patch.title === undefined ? row.title : patch.title,
|
|
2070
|
+
patch.journal === undefined ? row.journal : patch.journal,
|
|
2071
|
+
patch.state === "journaled" ? at : row.finished_at,
|
|
2072
|
+
patch.state === "undone" ? at : row.undone_at,
|
|
2073
|
+
id,
|
|
2074
|
+
);
|
|
2075
|
+
});
|
|
2076
|
+
this.emit("store.dream", id, { state: patch.state ?? null });
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
/** Record one change a dream made; returns its sequence number within the dream. */
|
|
2080
|
+
recordDreamChange(dreamId: string, change: { action: string; ref?: string | null; ref2?: string | null; detail?: Record<string, unknown> }): number {
|
|
2081
|
+
const seq = this.mutate("recordDreamChange", () => {
|
|
2082
|
+
const next =
|
|
2083
|
+
(this.ops.get<{ n: number | null }>("SELECT MAX(seq) AS n FROM dream_changes WHERE dream_id = ?", dreamId)?.n ?? 0) + 1;
|
|
2084
|
+
this.ops.run(
|
|
2085
|
+
`INSERT INTO dream_changes (dream_id, seq, action, ref, ref2, detail, at, undone)
|
|
2086
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 0)`,
|
|
2087
|
+
dreamId,
|
|
2088
|
+
next,
|
|
2089
|
+
change.action,
|
|
2090
|
+
change.ref ?? null,
|
|
2091
|
+
change.ref2 ?? null,
|
|
2092
|
+
JSON.stringify(change.detail ?? {}),
|
|
2093
|
+
this.nowFn(),
|
|
2094
|
+
);
|
|
2095
|
+
return next;
|
|
2096
|
+
});
|
|
2097
|
+
return seq;
|
|
2098
|
+
}
|
|
2099
|
+
|
|
2100
|
+
/** Mark one recorded change as reversed. */
|
|
2101
|
+
markDreamChangeUndone(dreamId: string, seq: number): void {
|
|
2102
|
+
this.mutate("markDreamChangeUndone", () => {
|
|
2103
|
+
this.ops.run("UPDATE dream_changes SET undone = 1 WHERE dream_id = ? AND seq = ?", dreamId, seq);
|
|
2104
|
+
});
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
/** Raise or snooze the day's dream ask: one row per calendar date. */
|
|
2108
|
+
setDreamAsk(input: { date: string; state: "offered" | "declined"; session?: string | null; day: number }): boolean {
|
|
2109
|
+
const wrote = this.mutate("setDreamAsk", () => {
|
|
2110
|
+
if (input.state === "offered") {
|
|
2111
|
+
this.ops.run(
|
|
2112
|
+
"INSERT OR IGNORE INTO dream_asks (date, state, session, day, at) VALUES (?, 'offered', ?, ?, ?)",
|
|
2113
|
+
input.date,
|
|
2114
|
+
input.session ?? null,
|
|
2115
|
+
input.day,
|
|
2116
|
+
this.nowFn(),
|
|
2117
|
+
);
|
|
2118
|
+
return (this.ops.get<{ n: number }>("SELECT changes() AS n")?.n ?? 0) > 0;
|
|
2119
|
+
}
|
|
2120
|
+
this.ops.run(
|
|
2121
|
+
`INSERT INTO dream_asks (date, state, session, day, at) VALUES (?, 'declined', ?, ?, ?)
|
|
2122
|
+
ON CONFLICT (date) DO UPDATE SET state = 'declined', at = excluded.at`,
|
|
2123
|
+
input.date,
|
|
2124
|
+
input.session ?? null,
|
|
2125
|
+
input.day,
|
|
2126
|
+
this.nowFn(),
|
|
2127
|
+
);
|
|
2128
|
+
return true;
|
|
2129
|
+
});
|
|
2130
|
+
this.emit("store.dream.ask", undefined, { date: input.date, state: input.state, wrote });
|
|
2131
|
+
return wrote;
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
/**
|
|
2135
|
+
* Live memories made since a dream — one bounded read, for the ask (which
|
|
2136
|
+
* runs on every prompt) and a dream's bundle. Since a MOMENT when there was a
|
|
2137
|
+
* dream (`created_at`; a pre-v7 row with no moment falls back to its lived
|
|
2138
|
+
* birth day), else since a lived day. Newest first. Only the columns decide:
|
|
2139
|
+
* `type` memory, not archived, not superseded, not protected, not a dream's
|
|
2140
|
+
* own words. The caller applies the rest of recall's gates (the deny-list,
|
|
2141
|
+
* confidentiality) to what comes back.
|
|
2142
|
+
*/
|
|
2143
|
+
newMemoryIds(filter: { sinceAt: number | null; sinceDay: number; limit: number }): string[] {
|
|
2144
|
+
// Nor a dream's own MERGES (origin `dream:<id>`): a merged memory is a
|
|
2145
|
+
// rewording of what a dream already saw, and counting it as new would let
|
|
2146
|
+
// one dream's output raise the next day's ask by itself (review of #251).
|
|
2147
|
+
const base = `SELECT id FROM memories
|
|
2148
|
+
WHERE type = 'memory' AND archived = 0 AND superseded_by IS NULL AND protected = 0
|
|
2149
|
+
AND (source IS NULL OR source != 'dreamed')
|
|
2150
|
+
AND (origin_ref IS NULL OR origin_ref NOT LIKE 'dream:%')`;
|
|
2151
|
+
const order = "ORDER BY COALESCE(created_at, 0) DESC, birth_day DESC, id LIMIT ?";
|
|
2152
|
+
const rows =
|
|
2153
|
+
filter.sinceAt === null
|
|
2154
|
+
? this.ops.all<{ id: string }>(`${base} AND birth_day >= ? ${order}`, filter.sinceDay, filter.limit)
|
|
2155
|
+
: this.ops.all<{ id: string }>(
|
|
2156
|
+
`${base} AND (created_at > ? OR (created_at IS NULL AND birth_day > ?)) ${order}`,
|
|
2157
|
+
filter.sinceAt,
|
|
2158
|
+
filter.sinceDay,
|
|
2159
|
+
filter.limit,
|
|
2160
|
+
);
|
|
2161
|
+
return rows.map((r) => r.id);
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
/** One dream's row. */
|
|
2165
|
+
dream(id: string): DreamRow | undefined {
|
|
2166
|
+
return this.ops.get<DreamRow>("SELECT * FROM dreams WHERE id = ?", id);
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
/** Dreams, newest first. */
|
|
2170
|
+
dreams(filter: { limit?: number; sinceAt?: number } = {}): DreamRow[] {
|
|
2171
|
+
return filter.sinceAt === undefined
|
|
2172
|
+
? this.ops.all<DreamRow>("SELECT * FROM dreams ORDER BY started_at DESC, id DESC LIMIT ?", filter.limit ?? 100)
|
|
2173
|
+
: this.ops.all<DreamRow>(
|
|
2174
|
+
"SELECT * FROM dreams WHERE started_at >= ? ORDER BY started_at DESC, id DESC LIMIT ?",
|
|
2175
|
+
filter.sinceAt,
|
|
2176
|
+
filter.limit ?? 100,
|
|
2177
|
+
);
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
/** One dream's changes, in the order they were made. */
|
|
2181
|
+
dreamChanges(dreamId: string): DreamChangeRow[] {
|
|
2182
|
+
return this.ops.all<DreamChangeRow>("SELECT * FROM dream_changes WHERE dream_id = ? ORDER BY seq", dreamId);
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
/** The day's ask row, when one was raised. */
|
|
2186
|
+
dreamAsk(date: string): DreamAskRow | undefined {
|
|
2187
|
+
return this.ops.get<DreamAskRow>("SELECT * FROM dream_asks WHERE date = ?", date);
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
/** The stored vector of one memory, or null (no embedder, or not embedded). A read of box 3. */
|
|
2191
|
+
vectorOf(id: string): number[] | null {
|
|
2192
|
+
try {
|
|
2193
|
+
const r = this.cache.get<{ vec: unknown }>("SELECT vec FROM embeddings WHERE memory_id = ?", id);
|
|
2194
|
+
if (r === undefined || r.vec === null || r.vec === undefined) return null;
|
|
2195
|
+
return Array.from(decodeVector(r.vec as never));
|
|
2196
|
+
} catch {
|
|
2197
|
+
return null;
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
1597
2200
|
setBand(id: string, band: Band, day: number): void {
|
|
1598
2201
|
this.mutate("setBand", () => {
|
|
1599
2202
|
this.requireRow(id);
|
|
@@ -1656,7 +2259,8 @@ export class Store {
|
|
|
1656
2259
|
* Record feelings on a memory (schema v7; `store/feelings.ts`). All or none:
|
|
1657
2260
|
* one bad input refuses the call `FEELING_INVALID` and writes nothing. An
|
|
1658
2261
|
* emotion not on the wheel is stored as `other` with the word kept, and the
|
|
1659
|
-
* result's `notices` name
|
|
2262
|
+
* result's `notices` name a wheel key only when one is a near misspelling (and
|
|
2263
|
+
* an alias read as a wheel word carries `readAs`).
|
|
1660
2264
|
* `beneath` is an existing feeling's id ON THIS MEMORY or another input's
|
|
1661
2265
|
* index. `model` is the writer's model id, screened like `PutInput.model`.
|
|
1662
2266
|
*/
|
|
@@ -1692,7 +2296,7 @@ export class Store {
|
|
|
1692
2296
|
this.ops.run("UPDATE feelings SET beneath_id = ? WHERE id = ?", under, ids[i] as string);
|
|
1693
2297
|
});
|
|
1694
2298
|
});
|
|
1695
|
-
this.emit("store.feelings", memoryId, { count: rows.length, other: notices.length });
|
|
2299
|
+
this.emit("store.feelings", memoryId, { count: rows.length, other: notices.filter((n) => n.readAs === undefined).length });
|
|
1696
2300
|
return { ids, notices };
|
|
1697
2301
|
}
|
|
1698
2302
|
|
|
@@ -2495,8 +3099,19 @@ export class Store {
|
|
|
2495
3099
|
return this.row(id) !== undefined;
|
|
2496
3100
|
}
|
|
2497
3101
|
|
|
2498
|
-
|
|
2499
|
-
|
|
3102
|
+
/**
|
|
3103
|
+
* One memory's row — plus `feeling_peak`, the strongest feeling recorded on
|
|
3104
|
+
* it (physics §5.10, emotion part A). Every physics read goes through here
|
|
3105
|
+
* (`read`, `physicsOf`, sleep, recall, the dashboard), so this one subquery
|
|
3106
|
+
* is what makes a feeling weigh the same everywhere. `feelings_memory` is
|
|
3107
|
+
* indexed, so it is one index probe per row.
|
|
3108
|
+
*/
|
|
3109
|
+
row(id: string): (MemoryRow & FeelingPeak) | undefined {
|
|
3110
|
+
return this.ops.get<MemoryRow & FeelingPeak>(
|
|
3111
|
+
`SELECT m.*, (SELECT MAX(f.strength) FROM feelings f WHERE f.memory_id = m.id) AS feeling_peak
|
|
3112
|
+
FROM memories m WHERE m.id = ?`,
|
|
3113
|
+
id,
|
|
3114
|
+
);
|
|
2500
3115
|
}
|
|
2501
3116
|
|
|
2502
3117
|
read(id: string): StoredMemory {
|
|
@@ -2587,14 +3202,18 @@ export class Store {
|
|
|
2587
3202
|
* month `2026-10` sorts BEFORE `2026-10-01` as text and a range's text runs
|
|
2588
3203
|
* past its first day, so a plain `BETWEEN` would miss both. The index answers
|
|
2589
3204
|
* "starts no later than `to`" (`'~'` sorts after every digit and `..`), and
|
|
2590
|
-
* `calendarOverlaps` decides the rest.
|
|
2591
|
-
*
|
|
2592
|
-
*
|
|
3205
|
+
* `calendarOverlaps` decides the rest. The older meta convention
|
|
3206
|
+
* (`meta.eventDate`) is read by nothing since 2026-09-26: this column is the
|
|
3207
|
+
* one place a reminder date lives.
|
|
3208
|
+
*
|
|
3209
|
+
* `archived: true` includes archived memories too — for `prospective/`'s exit
|
|
3210
|
+
* accounting, where a dated memory archived before its window is the `faded`
|
|
3211
|
+
* exit and has to be found to be counted.
|
|
2593
3212
|
*/
|
|
2594
|
-
datedMemories(from: string, to: string): DatedMemory[] {
|
|
3213
|
+
datedMemories(from: string, to: string, opts: { archived?: boolean } = {}): DatedMemory[] {
|
|
2595
3214
|
const rows = this.ops.all<{ id: string; event_date: string }>(
|
|
2596
3215
|
`SELECT id, event_date FROM memories
|
|
2597
|
-
WHERE event_date IS NOT NULL AND event_date <= ? AND archived = 0`,
|
|
3216
|
+
WHERE event_date IS NOT NULL AND event_date <= ?${opts.archived === true ? "" : " AND archived = 0"}`,
|
|
2598
3217
|
`${to}~`,
|
|
2599
3218
|
);
|
|
2600
3219
|
return rows
|
|
@@ -2611,6 +3230,71 @@ export class Store {
|
|
|
2611
3230
|
);
|
|
2612
3231
|
}
|
|
2613
3232
|
|
|
3233
|
+
/**
|
|
3234
|
+
* Feelings recorded at or after `sinceMs` (the store's clock, UTC ms) — the
|
|
3235
|
+
* raw material of "how each person feels right now" (recall G18). Newest
|
|
3236
|
+
* first. Only feelings on a memory that is still live: an archived or
|
|
3237
|
+
* superseded memory's feelings no longer speak for the moment.
|
|
3238
|
+
*/
|
|
3239
|
+
feelingsSince(sinceMs: number): FeelingRow[] {
|
|
3240
|
+
return this.ops.all<FeelingRow>(
|
|
3241
|
+
`SELECT f.* FROM feelings f JOIN memories m ON m.id = f.memory_id
|
|
3242
|
+
WHERE f.created_at >= ? AND m.archived = 0 AND m.superseded_by IS NULL
|
|
3243
|
+
ORDER BY f.created_at DESC, f.rowid DESC`,
|
|
3244
|
+
sinceMs,
|
|
3245
|
+
);
|
|
3246
|
+
}
|
|
3247
|
+
|
|
3248
|
+
/**
|
|
3249
|
+
* The feelings on several memories in ONE query, with each memory's birth
|
|
3250
|
+
* day beside them (a feeling softens from the day its memory was born —
|
|
3251
|
+
* physics §5.10). Recall's mood-matching reads this for its candidates only,
|
|
3252
|
+
* inside its latency budget. Ids with no feelings are simply absent.
|
|
3253
|
+
*/
|
|
3254
|
+
feelingsOn(ids: readonly string[]): Map<string, (FeelingRow & { birth_day: number })[]> {
|
|
3255
|
+
const out = new Map<string, (FeelingRow & { birth_day: number })[]>();
|
|
3256
|
+
if (ids.length === 0) return out;
|
|
3257
|
+
const unique = [...new Set(ids)];
|
|
3258
|
+
// SQLite's default bound-variable ceiling is far above recall's candidate
|
|
3259
|
+
// cap; chunk anyway so no caller can walk into it.
|
|
3260
|
+
for (let at = 0; at < unique.length; at += 500) {
|
|
3261
|
+
const part = unique.slice(at, at + 500);
|
|
3262
|
+
const rows = this.ops.all<FeelingRow & { birth_day: number }>(
|
|
3263
|
+
`SELECT f.*, m.birth_day AS birth_day FROM feelings f JOIN memories m ON m.id = f.memory_id
|
|
3264
|
+
WHERE f.memory_id IN (${part.map(() => "?").join(",")})
|
|
3265
|
+
ORDER BY f.created_at, f.rowid`,
|
|
3266
|
+
...part,
|
|
3267
|
+
);
|
|
3268
|
+
for (const r of rows) {
|
|
3269
|
+
const list = out.get(r.memory_id) ?? [];
|
|
3270
|
+
list.push(r);
|
|
3271
|
+
out.set(r.memory_id, list);
|
|
3272
|
+
}
|
|
3273
|
+
}
|
|
3274
|
+
return out;
|
|
3275
|
+
}
|
|
3276
|
+
|
|
3277
|
+
/**
|
|
3278
|
+
* THE EMOTION CENSUS — how many live memories carry feeling, and how many of
|
|
3279
|
+
* those the feeling actually weighs on (intensity > 0: held higher at birth
|
|
3280
|
+
* and fading slower, physics §5.10). `sinceDay` narrows both to memories born
|
|
3281
|
+
* on or after that lived day. Counts only; no word of any feeling leaves.
|
|
3282
|
+
*/
|
|
3283
|
+
emotionCensus(opts: { sinceDay?: number } = {}): { withFeelings: number; weighted: number } {
|
|
3284
|
+
const since = opts.sinceDay ?? Number.MIN_SAFE_INTEGER;
|
|
3285
|
+
const row = this.ops.get<{ with_feelings: number | null; weighted: number | null }>(
|
|
3286
|
+
`SELECT
|
|
3287
|
+
SUM(CASE WHEN EXISTS (SELECT 1 FROM feelings f WHERE f.memory_id = m.id) THEN 1 ELSE 0 END) AS with_feelings,
|
|
3288
|
+
SUM(CASE WHEN m.emotional > 0
|
|
3289
|
+
OR EXISTS (SELECT 1 FROM feelings f WHERE f.memory_id = m.id AND f.strength > 0)
|
|
3290
|
+
THEN 1 ELSE 0 END) AS weighted
|
|
3291
|
+
FROM memories m
|
|
3292
|
+
WHERE m.archived = 0 AND m.superseded_by IS NULL AND m.birth_day >= ?`,
|
|
3293
|
+
since,
|
|
3294
|
+
);
|
|
3295
|
+
return { withFeelings: row?.with_feelings ?? 0, weighted: row?.weighted ?? 0 };
|
|
3296
|
+
}
|
|
3297
|
+
|
|
2614
3298
|
/**
|
|
2615
3299
|
* How many feelings, by `whose` and by `core` or `emotion` (an `other` counts
|
|
2616
3300
|
* under `other`), recorded from `from` to `to` — the person's calendar days,
|
|
@@ -2697,6 +3381,18 @@ export class Store {
|
|
|
2697
3381
|
);
|
|
2698
3382
|
}
|
|
2699
3383
|
|
|
3384
|
+
/**
|
|
3385
|
+
* Every memory id that has at least one firing-state row, sorted. The exit
|
|
3386
|
+
* accounting's other half (prospective §5 G12): a row can outlive its
|
|
3387
|
+
* memory's date — a date cleared by `revise`, a reschedule's retired window —
|
|
3388
|
+
* and `datedMemories` alone would never find it.
|
|
3389
|
+
*/
|
|
3390
|
+
prospectiveMemoryIds(): string[] {
|
|
3391
|
+
return this.ops
|
|
3392
|
+
.all<{ memory_id: string }>("SELECT DISTINCT memory_id FROM prospective ORDER BY memory_id")
|
|
3393
|
+
.map((r) => r.memory_id);
|
|
3394
|
+
}
|
|
3395
|
+
|
|
2700
3396
|
removalRecord(id?: string): RemovalRow[] {
|
|
2701
3397
|
const rows =
|
|
2702
3398
|
id === undefined
|
|
@@ -2946,8 +3642,8 @@ export class Store {
|
|
|
2946
3642
|
promoted_identity, protected, pressure, last_challenged_day, archived,
|
|
2947
3643
|
archived_reason, superseded_by, revision, content_hash,
|
|
2948
3644
|
learned_on, happened_on, source, origin_session, origin_scope, origin_ref,
|
|
2949
|
-
title, body, meta, confidential, created_at, updated_at, model, event_date)
|
|
2950
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, NULL, NULL, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
3645
|
+
title, body, meta, confidential, created_at, updated_at, model, event_date, legacy)
|
|
3646
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, NULL, NULL, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
2951
3647
|
id,
|
|
2952
3648
|
input.type,
|
|
2953
3649
|
input.kind,
|
|
@@ -2982,6 +3678,9 @@ export class Store {
|
|
|
2982
3678
|
at,
|
|
2983
3679
|
modelOrNull(input.model),
|
|
2984
3680
|
doc.eventDate ?? null,
|
|
3681
|
+
// v8: only a caller carrying a pre-upgrade memory's physics forward (a
|
|
3682
|
+
// dream merge of legacy rows) sets it; every new memory is post-upgrade.
|
|
3683
|
+
input.physics?.legacy === true ? 1 : 0,
|
|
2985
3684
|
);
|
|
2986
3685
|
return doc;
|
|
2987
3686
|
}
|