counterparts 0.1.0
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/CONSTITUTION.md +68 -0
- package/LICENSE +21 -0
- package/README.md +348 -0
- package/docs/ELI5.md +84 -0
- package/docs/QUICKSTART.md +1258 -0
- package/docs/module-map.md +81 -0
- package/package.json +63 -0
- package/src/adapters/claude-code/CONTRACT.md +606 -0
- package/src/adapters/claude-code/INTERFACE-GAPS.md +655 -0
- package/src/adapters/claude-code/NOTES.md +1055 -0
- package/src/adapters/claude-code/bin/hook.ts +820 -0
- package/src/adapters/claude-code/bin/runner.ts +522 -0
- package/src/adapters/claude-code/config.ts +778 -0
- package/src/adapters/claude-code/credentials.ts +199 -0
- package/src/adapters/claude-code/doctor.ts +2557 -0
- package/src/adapters/claude-code/embed-client.ts +617 -0
- package/src/adapters/claude-code/hooks.ts +2240 -0
- package/src/adapters/claude-code/index.ts +224 -0
- package/src/adapters/claude-code/interpret-client.ts +395 -0
- package/src/adapters/claude-code/page-writer.ts +462 -0
- package/src/adapters/claude-code/primacy.ts +162 -0
- package/src/adapters/claude-code/spawn.ts +241 -0
- package/src/adapters/claude-code/standdown.ts +536 -0
- package/src/adapters/claude-code/transcript.ts +661 -0
- package/src/adapters/claude-code/vectors.ts +415 -0
- package/src/adapters/cli/CONTRACT.md +255 -0
- package/src/adapters/cli/INTERFACE-GAPS.md +350 -0
- package/src/adapters/cli/NOTES.md +936 -0
- package/src/adapters/cli/bin/counterparts.ts +74 -0
- package/src/adapters/cli/commands.ts +6095 -0
- package/src/adapters/cli/export.ts +766 -0
- package/src/adapters/cli/index.ts +103 -0
- package/src/adapters/cli/install.ts +670 -0
- package/src/adapters/cli/removal.ts +1186 -0
- package/src/adapters/cli/repair-dates.ts +659 -0
- package/src/adapters/cli/self-page.ts +229 -0
- package/src/adapters/cli/snapshot.ts +279 -0
- package/src/adapters/cli/start-fresh.ts +1228 -0
- package/src/adapters/config-path.ts +342 -0
- package/src/adapters/dashboard/CONTRACT.md +67 -0
- package/src/adapters/dashboard/INTERFACE-GAPS.md +139 -0
- package/src/adapters/dashboard/NOTES.md +474 -0
- package/src/adapters/dashboard/activity.ts +121 -0
- package/src/adapters/dashboard/ansi.ts +82 -0
- package/src/adapters/dashboard/bin/dashboard.ts +479 -0
- package/src/adapters/dashboard/browse.ts +298 -0
- package/src/adapters/dashboard/identity.ts +182 -0
- package/src/adapters/dashboard/index.ts +183 -0
- package/src/adapters/dashboard/layout.ts +149 -0
- package/src/adapters/dashboard/registries.ts +291 -0
- package/src/adapters/dashboard/resolve.ts +206 -0
- package/src/adapters/dashboard/source.ts +48 -0
- package/src/adapters/dashboard/status.ts +327 -0
- package/src/adapters/dashboard/stories.ts +194 -0
- package/src/adapters/dashboard/web/app.html +1731 -0
- package/src/adapters/dashboard/web/brain.html +463 -0
- package/src/adapters/dashboard/web/fired.ts +31 -0
- package/src/adapters/dashboard/web/flow.ts +457 -0
- package/src/adapters/dashboard/web/narrate.ts +1042 -0
- package/src/adapters/dashboard/web/reveal.ts +135 -0
- package/src/adapters/dashboard/web/server.ts +287 -0
- package/src/adapters/dashboard/web/views.ts +1744 -0
- package/src/adapters/expansions.ts +569 -0
- package/src/adapters/fired.ts +1580 -0
- package/src/adapters/mcp/CONTRACT.md +240 -0
- package/src/adapters/mcp/INTERFACE-GAPS.md +271 -0
- package/src/adapters/mcp/NOTES.md +205 -0
- package/src/adapters/mcp/bin/serve.ts +349 -0
- package/src/adapters/mcp/deliberate.ts +619 -0
- package/src/adapters/mcp/index.ts +101 -0
- package/src/adapters/mcp/protocol.ts +170 -0
- package/src/adapters/mcp/server.ts +1740 -0
- package/src/adapters/mcp/stdio.ts +56 -0
- package/src/adapters/mcp/tools.ts +779 -0
- package/src/adapters/scopes.ts +657 -0
- package/src/adapters/sessions.ts +402 -0
- package/src/adapters/snapshots.ts +1069 -0
- package/src/adapters/stance-env.ts +137 -0
- package/src/core/associate/CONTRACT.md +118 -0
- package/src/core/associate/INTERFACE-GAPS.md +173 -0
- package/src/core/associate/NOTES.md +194 -0
- package/src/core/associate/buffer.ts +66 -0
- package/src/core/associate/edges.ts +188 -0
- package/src/core/associate/index.ts +570 -0
- package/src/core/associate/pending.ts +383 -0
- package/src/core/associate/spread.ts +149 -0
- package/src/core/associate/tunables.ts +123 -0
- package/src/core/bridge.ts +272 -0
- package/src/core/briefing.ts +67 -0
- package/src/core/counterpart.ts +3855 -0
- package/src/core/encode/CONTRACT.md +151 -0
- package/src/core/encode/INTERFACE-GAPS.md +135 -0
- package/src/core/encode/NOTES.md +146 -0
- package/src/core/encode/aliases.ts +71 -0
- package/src/core/encode/battery.ts +284 -0
- package/src/core/encode/chunk.ts +265 -0
- package/src/core/encode/emotion.ts +119 -0
- package/src/core/encode/floor.ts +76 -0
- package/src/core/encode/index.ts +100 -0
- package/src/core/encode/precision.ts +162 -0
- package/src/core/encode/preselect.ts +269 -0
- package/src/core/encode/salience.ts +108 -0
- package/src/core/encode/secrets.ts +265 -0
- package/src/core/encode/tunables.ts +99 -0
- package/src/core/encode/types.ts +286 -0
- package/src/core/encode/words.ts +75 -0
- package/src/core/handoff/CONTRACT.md +199 -0
- package/src/core/handoff/INTERFACE-GAPS.md +166 -0
- package/src/core/handoff/NOTES.md +206 -0
- package/src/core/handoff/index.ts +957 -0
- package/src/core/mint.ts +279 -0
- package/src/core/observer.ts +28 -0
- package/src/core/physics/CONTRACT.md +341 -0
- package/src/core/physics/NOTES.md +155 -0
- package/src/core/physics/clock.ts +51 -0
- package/src/core/physics/index.ts +1054 -0
- package/src/core/prospective/CONTRACT.md +137 -0
- package/src/core/prospective/INTERFACE-GAPS.md +112 -0
- package/src/core/prospective/NOTES.md +160 -0
- package/src/core/prospective/derive.ts +188 -0
- package/src/core/prospective/index.ts +1030 -0
- package/src/core/prospective/tunables.ts +72 -0
- package/src/core/prospective/windows.ts +187 -0
- package/src/core/recall/CONTRACT.md +326 -0
- package/src/core/recall/INTERFACE-GAPS.md +163 -0
- package/src/core/recall/NOTES.md +472 -0
- package/src/core/recall/activate.ts +521 -0
- package/src/core/recall/cues.ts +275 -0
- package/src/core/recall/gate.ts +441 -0
- package/src/core/recall/index.ts +692 -0
- package/src/core/recall/probe.ts +239 -0
- package/src/core/recall/reference.ts +201 -0
- package/src/core/recall/render.ts +285 -0
- package/src/core/recall/session.ts +403 -0
- package/src/core/recall/tunables.ts +329 -0
- package/src/core/remember/CONTRACT.md +293 -0
- package/src/core/remember/INTERFACE-GAPS.md +189 -0
- package/src/core/remember/NOTES.md +373 -0
- package/src/core/remember/fallback.ts +452 -0
- package/src/core/remember/index.ts +103 -0
- package/src/core/remember/owner-strike-seam.ts +479 -0
- package/src/core/remember/proposals.ts +609 -0
- package/src/core/remember/spans.ts +1310 -0
- package/src/core/remember/tunables.ts +111 -0
- package/src/core/remember/updates.ts +198 -0
- package/src/core/retrieval.ts +96 -0
- package/src/core/revision.ts +441 -0
- package/src/core/schemas/CONTRACT.md +190 -0
- package/src/core/schemas/INTERFACE-GAPS.md +172 -0
- package/src/core/schemas/NOTES.md +260 -0
- package/src/core/schemas/aliases.ts +160 -0
- package/src/core/schemas/index.ts +1459 -0
- package/src/core/schemas/tunables.ts +44 -0
- package/src/core/schemas/types.ts +387 -0
- package/src/core/self/CONTRACT.md +459 -0
- package/src/core/self/INTERFACE-GAPS.md +284 -0
- package/src/core/self/NOTES.md +1057 -0
- package/src/core/self/briefing.ts +980 -0
- package/src/core/self/episodes.ts +483 -0
- package/src/core/self/freeze.ts +103 -0
- package/src/core/self/identity.ts +604 -0
- package/src/core/self/index.ts +2130 -0
- package/src/core/self/journal-file.ts +760 -0
- package/src/core/self/page.ts +404 -0
- package/src/core/self/tunables.ts +244 -0
- package/src/core/self/writer.ts +714 -0
- package/src/core/sleep/CONTRACT.md +298 -0
- package/src/core/sleep/INTERFACE-GAPS.md +262 -0
- package/src/core/sleep/NOTES.md +770 -0
- package/src/core/sleep/briefing.ts +86 -0
- package/src/core/sleep/consolidate.ts +241 -0
- package/src/core/sleep/cycle.ts +598 -0
- package/src/core/sleep/decay.ts +237 -0
- package/src/core/sleep/dedup.ts +373 -0
- package/src/core/sleep/index.ts +112 -0
- package/src/core/sleep/log.ts +116 -0
- package/src/core/sleep/markers.ts +164 -0
- package/src/core/sleep/prune.ts +170 -0
- package/src/core/sleep/strength-cache.ts +176 -0
- package/src/core/sleep/tunables.ts +135 -0
- package/src/core/sleep/types.ts +542 -0
- package/src/core/store/CONTRACT.md +446 -0
- package/src/core/store/NOTES.md +1068 -0
- package/src/core/store/cache.ts +709 -0
- package/src/core/store/db.ts +275 -0
- package/src/core/store/errors.ts +134 -0
- package/src/core/store/index.ts +2450 -0
- package/src/core/store/operational.ts +610 -0
- package/src/core/store/owner-op-seam.ts +428 -0
- package/src/core/store/paths.ts +494 -0
- package/src/core/store/prose.ts +165 -0
- package/src/core/store/render.ts +87 -0
- package/src/core/store/walk-seam.ts +78 -0
- package/src/core/types.ts +68 -0
- package/tools/install-loop/run.sh +1063 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The owner-removal seam — the ONE place in `store/` that destroys anything.
|
|
3
|
+
*
|
|
4
|
+
* The store's public surface still exports no delete, no unlink, no rm, and no
|
|
5
|
+
* method that removes a memory (§5 G2, §16 G1 — "enforced by absence"). What
|
|
6
|
+
* changed on 2026-08-25 is that the chase's box-2 half now EXISTS, because
|
|
7
|
+
* "anything can be removed loudly" was only half-true while the `memories`,
|
|
8
|
+
* `edges` and `prospective` rows survived a removal as reported-but-dark state
|
|
9
|
+
* (BUILD-STATUS gap 3, cli/INTERFACE-GAPS §1).
|
|
10
|
+
*
|
|
11
|
+
* It lives in this file rather than on `Store` so that the enforcement stays
|
|
12
|
+
* structural: `Store` hands this module a capability at construction time (a
|
|
13
|
+
* WeakMap the rest of the program cannot see), and the caller-universality test
|
|
14
|
+
* in `test/cli.test.ts` pins who may import this file at all. Nothing reaches
|
|
15
|
+
* `chaseRemoved` by having a `Store`; you have to import the destruction path
|
|
16
|
+
* on purpose, from the one directory allowed to.
|
|
17
|
+
*
|
|
18
|
+
* How the pieces fit with `adapters/cli/`, which owns the other half:
|
|
19
|
+
*
|
|
20
|
+
* 1. The CLI (an owner-only, non-model-reachable path) implements
|
|
21
|
+
* `OwnerRemovalPort`. It is the ONLY implementor.
|
|
22
|
+
* 2. Before anything moves, the CLI calls `Store.appendRemovalRecord({stage:
|
|
23
|
+
* "requested"})`. A failed append is never reported as success — if the record
|
|
24
|
+
* cannot be written, nothing moves (§16 G10).
|
|
25
|
+
* 3. Everything that must READ the doomed content happens before any copy is
|
|
26
|
+
* chased (§16 G13): the contamination scan returns ids only.
|
|
27
|
+
* 4. Then `stage: "dark"` is appended. From that moment the store's deny-list
|
|
28
|
+
* refuses to load the id, at read and at rebuild — a restored backup or a
|
|
29
|
+
* stray copy cannot quietly resurrect it, and the stray is skipped and
|
|
30
|
+
* logged, never deleted (§16 G12).
|
|
31
|
+
* 5. Copies are chased — the words and every box-2 row by `chaseRemoved`
|
|
32
|
+
* here, in ONE transaction, and box 3 by a rebuild — then `stage: "chased"`
|
|
33
|
+
* (appended inside that same transaction) and `stage: "complete"` follow.
|
|
34
|
+
* Every crash point leaves the memory either fully alive, or dark AND
|
|
35
|
+
* recorded (§16 G11). Since the floor (schema v6) the CLI has no files left
|
|
36
|
+
* to chase: the words ARE the columns this blanks, so the erase is one
|
|
37
|
+
* commit rather than a file walk that could be interrupted halfway.
|
|
38
|
+
*
|
|
39
|
+
* Note kept from inside v1's released ceremony (contract §4): the cooling-off
|
|
40
|
+
* period was deliberately WALL-CLOCK, not active days — a week of not using the
|
|
41
|
+
* machine must still be a week of second thoughts.
|
|
42
|
+
*/
|
|
43
|
+
import { StoreError } from "./errors.js";
|
|
44
|
+
import type { Db } from "./db.js";
|
|
45
|
+
import type { MemoryRow } from "./operational.js";
|
|
46
|
+
import type { RemovalNote, Store } from "./index.js";
|
|
47
|
+
|
|
48
|
+
export interface OwnerRemovalRequest {
|
|
49
|
+
/** Memory-bearing roots only. The archive, backups, the graph, and the removal
|
|
50
|
+
* record itself are never nameable targets — they are chased as copies (§16 G16). */
|
|
51
|
+
targetId: string;
|
|
52
|
+
actor: "owner";
|
|
53
|
+
reason: string;
|
|
54
|
+
/** Wall-clock ms, not lived days. */
|
|
55
|
+
requestedAt: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface OwnerRemovalOutcome {
|
|
59
|
+
/** Ids only — printing matches would re-leak what is being erased (§16 G15). */
|
|
60
|
+
chased: string[];
|
|
61
|
+
unchased: string[];
|
|
62
|
+
/**
|
|
63
|
+
* Surfaces the removal deliberately does NOT take, in their own words. A
|
|
64
|
+
* third category on purpose: `unchased` means "could not", and reporting a
|
|
65
|
+
* conversation turn the removal is leaving on principle as a failure is a
|
|
66
|
+
* different lie from the silence §16 G15 forbids, not a smaller one.
|
|
67
|
+
*/
|
|
68
|
+
leftAlone: string[];
|
|
69
|
+
notes: RemovalNote[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface OwnerRemovalPort {
|
|
73
|
+
(request: OwnerRemovalRequest): OwnerRemovalOutcome;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ── the capability ──────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What a `Store` hands this module, and nothing else: a guarded transaction
|
|
80
|
+
* runner and the two reads the chase needs.
|
|
81
|
+
*
|
|
82
|
+
* `ownerMutate` is the store's own seam, not a naked database handle — an
|
|
83
|
+
* instrument that has stood down must refuse to destroy a memory exactly as it
|
|
84
|
+
* refuses to write one (observer-mode G3), and a chase that took the database
|
|
85
|
+
* directly would be the one write in the system that skipped the stance check.
|
|
86
|
+
*/
|
|
87
|
+
export type OwnerOpSite = "chaseRemoved" | "unarchiveMerged";
|
|
88
|
+
|
|
89
|
+
export interface OwnerOpAccess {
|
|
90
|
+
readonly dir: string;
|
|
91
|
+
ownerMutate<T>(site: OwnerOpSite, fn: (db: Db) => T): T;
|
|
92
|
+
rawRow(id: string): MemoryRow | undefined;
|
|
93
|
+
isDenied(id: string): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Re-index ONE row's tokens from its own prose, leaving its embedding alone.
|
|
96
|
+
* The seam's only route to box 3, and deliberately the lexical half only —
|
|
97
|
+
* a restore must never make a paid embedding call and must never drop a
|
|
98
|
+
* vector nothing here could recompute. See `store/index.ts`'s grant for why
|
|
99
|
+
* a restore has to touch box 3 at all.
|
|
100
|
+
*/
|
|
101
|
+
reindexLexical(id: string): void;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const GRANTS = new WeakMap<object, OwnerOpAccess>();
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Called once, by `Store`'s constructor. Holding a `Store` does not give you
|
|
108
|
+
* the capability; importing THIS file does, and that import is what the
|
|
109
|
+
* caller-universality test pins (§16 G2).
|
|
110
|
+
*/
|
|
111
|
+
export function grantOwnerOps(store: object, access: OwnerOpAccess): void {
|
|
112
|
+
GRANTS.set(store, access);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ── the chase ───────────────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
/** What the chase touched, by surface. Counts and verdicts — never contents. */
|
|
118
|
+
export interface ChaseReport {
|
|
119
|
+
readonly id: string;
|
|
120
|
+
/** Surfaces whose rows are GONE. */
|
|
121
|
+
readonly removed: { surface: string; count: number }[];
|
|
122
|
+
/** Surfaces stripped to a skeleton and kept, because a survivor points at them. */
|
|
123
|
+
readonly neutralized: { surface: string; count: number }[];
|
|
124
|
+
/** Surfaces this chase deliberately leaves whole, named so the list is honest. */
|
|
125
|
+
readonly survives: string[];
|
|
126
|
+
/** True when the chase found nothing left to do (a replayed or repeated chase). */
|
|
127
|
+
readonly noop: boolean;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The archived-reason a neutralized row carries, so raw SQL reads honestly too. */
|
|
131
|
+
export const REMOVED_REASON = "removed-by-owner";
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Remove one memory's box-2 state, in ONE transaction, and append the `chased`
|
|
135
|
+
* stage inside it — so the record and the removal land together or not at all.
|
|
136
|
+
*
|
|
137
|
+
* What DIES: the edges touching it (in both directions — an erased id left in
|
|
138
|
+
* the learned graph keeps CONDUCTING activation between its former neighbours,
|
|
139
|
+
* §16 G14), its prospective windows, the per-session gate rows that name it,
|
|
140
|
+
* and THE WORDS THEMSELVES — the body, the title, the meta and the content hash,
|
|
141
|
+
* on the memory row and on every one of its version rows. Before the floor
|
|
142
|
+
* these were pointers at files the CLI deleted separately; the columns are the
|
|
143
|
+
* content now, so blanking them IS the erase and it happens inside the same
|
|
144
|
+
* transaction as the record.
|
|
145
|
+
*
|
|
146
|
+
* What SURVIVES, on purpose:
|
|
147
|
+
* - the removal record (canonical, append-only) and therefore the deny-list;
|
|
148
|
+
* - a `removal_tombstone` row: what the memory WAS, in flags and counts, so a
|
|
149
|
+
* removed protected element still appears in the permanent enumeration as
|
|
150
|
+
* `[removed]` rather than vanishing (scar §2.19 from the other side);
|
|
151
|
+
* - the memory's own row and its version rows, STRIPPED — because box 2's
|
|
152
|
+
* foreign keys make them the lineage: a survivor whose `superseded_by` or
|
|
153
|
+
* `successor_id` names this id must resolve to a named removal, never to a
|
|
154
|
+
* dangling pointer. `Store.resolve` stops at a denied id and `Store.read`
|
|
155
|
+
* refuses it by name, so the skeleton reads as `[removed by the owner]`
|
|
156
|
+
* everywhere an id is rendered, and as nothing at all anywhere else.
|
|
157
|
+
*
|
|
158
|
+
* It refuses outright unless the id is already dark: a chase without a record
|
|
159
|
+
* is the one outcome worse than a chase that did not happen (§16 G10).
|
|
160
|
+
*/
|
|
161
|
+
export function chaseRemoved(store: Store, id: string): ChaseReport {
|
|
162
|
+
const access = GRANTS.get(store);
|
|
163
|
+
if (access === undefined) {
|
|
164
|
+
throw new StoreError("OWNER_OP_UNGRANTED", { id, site: "chaseRemoved" });
|
|
165
|
+
}
|
|
166
|
+
if (!access.isDenied(id)) {
|
|
167
|
+
// Not "unknown id": the id may well exist. The refusal is about ORDER —
|
|
168
|
+
// the record comes first, always.
|
|
169
|
+
throw new StoreError("REMOVAL_NOT_DARK", { id, site: "chaseRemoved" });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return access.ownerMutate("chaseRemoved", (db) => {
|
|
173
|
+
const row = access.rawRow(id);
|
|
174
|
+
const already = db.get<{ n: number }>(
|
|
175
|
+
"SELECT COUNT(*) AS n FROM removal_tombstone WHERE memory_id = ?",
|
|
176
|
+
id,
|
|
177
|
+
);
|
|
178
|
+
const noop = (already?.n ?? 0) > 0;
|
|
179
|
+
|
|
180
|
+
const edges =
|
|
181
|
+
db.get<{ n: number }>("SELECT COUNT(*) AS n FROM edges WHERE src = ? OR dst = ?", id, id)?.n ??
|
|
182
|
+
0;
|
|
183
|
+
const prospective =
|
|
184
|
+
db.get<{ n: number }>("SELECT COUNT(*) AS n FROM prospective WHERE memory_id = ?", id)?.n ?? 0;
|
|
185
|
+
const gateRows =
|
|
186
|
+
db.get<{ n: number }>("SELECT COUNT(*) AS n FROM gate_session WHERE ref = ?", id)?.n ?? 0;
|
|
187
|
+
const versions =
|
|
188
|
+
db.get<{ n: number }>("SELECT COUNT(*) AS n FROM versions WHERE memory_id = ?", id)?.n ?? 0;
|
|
189
|
+
|
|
190
|
+
// Rows that carry nothing anyone else points at: they simply go.
|
|
191
|
+
db.run("DELETE FROM edges WHERE src = ? OR dst = ?", id, id);
|
|
192
|
+
db.run("DELETE FROM prospective WHERE memory_id = ?", id);
|
|
193
|
+
db.run("DELETE FROM gate_session WHERE ref = ?", id);
|
|
194
|
+
|
|
195
|
+
// Version rows stay (a successor's predecessor pointer lives here) and lose
|
|
196
|
+
// every word they held. `reason`, `version_day` and `successor_id` are
|
|
197
|
+
// lineage metadata the removal has no cause to destroy, so they are left
|
|
198
|
+
// alone; `learned_on` and `happened_on` go with the content, because a date
|
|
199
|
+
// a memory was learned on is a fact ABOUT the removed memory.
|
|
200
|
+
db.run(
|
|
201
|
+
`UPDATE versions
|
|
202
|
+
SET title = NULL, body = '', meta = '{}', content_hash = '',
|
|
203
|
+
learned_on = '', happened_on = NULL
|
|
204
|
+
WHERE memory_id = ?`,
|
|
205
|
+
id,
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
if (row !== undefined) {
|
|
209
|
+
// The skeleton: an address, its family, and nothing else. Physics is
|
|
210
|
+
// zeroed rather than kept — a removed memory is not a protected one, and
|
|
211
|
+
// must not go on conducting, ranking or resisting anything.
|
|
212
|
+
// THE TOMBSTONE SHAPE. `body = ''` AND `content_hash = ''` together are
|
|
213
|
+
// what `rowTombstoned` reads, and both halves have to land: a blank body
|
|
214
|
+
// beside a hash that still names words would read as a row whose words
|
|
215
|
+
// went missing (`MEMORY_BODY_MISSING`) rather than as a removal, and
|
|
216
|
+
// `Schemas.load`'s skip — the thing that keeps the next session starting
|
|
217
|
+
// after a removal — is keyed on the pair.
|
|
218
|
+
db.run(
|
|
219
|
+
`UPDATE memories
|
|
220
|
+
SET novelty = NULL, relevance = 0, emotional = 0, predictive = 0, claimed = NULL,
|
|
221
|
+
uses = 0, reinforced_days = 0, consolidated = 0, promoted_identity = 0,
|
|
222
|
+
protected = 0, pressure = 0, last_challenged_day = NULL,
|
|
223
|
+
archived = 1, archived_reason = ?, content_hash = '',
|
|
224
|
+
title = NULL, body = '', meta = '{}', confidential = 0,
|
|
225
|
+
learned_on = '', happened_on = NULL
|
|
226
|
+
WHERE id = ?`,
|
|
227
|
+
REMOVED_REASON,
|
|
228
|
+
id,
|
|
229
|
+
);
|
|
230
|
+
if (!noop) {
|
|
231
|
+
db.run(
|
|
232
|
+
`INSERT INTO removal_tombstone
|
|
233
|
+
(memory_id, type, kind, band, protected, promoted_identity, superseded_by,
|
|
234
|
+
versions, edges, prospective, gate_rows, at)
|
|
235
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
236
|
+
id,
|
|
237
|
+
row.type,
|
|
238
|
+
row.kind,
|
|
239
|
+
row.band,
|
|
240
|
+
row.protected,
|
|
241
|
+
row.promoted_identity,
|
|
242
|
+
row.superseded_by,
|
|
243
|
+
versions,
|
|
244
|
+
edges,
|
|
245
|
+
prospective,
|
|
246
|
+
gateRows,
|
|
247
|
+
Date.now(),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const note: RemovalNote = { memoryId: id, stage: "chased", actor: "owner" };
|
|
253
|
+
store.appendRemovalRecord(note);
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
id,
|
|
257
|
+
removed: [
|
|
258
|
+
{ surface: "operational.edges", count: edges },
|
|
259
|
+
{ surface: "operational.prospective", count: prospective },
|
|
260
|
+
{ surface: "operational.gate_session", count: gateRows },
|
|
261
|
+
],
|
|
262
|
+
neutralized: [
|
|
263
|
+
{ surface: "operational.memories", count: row === undefined ? 0 : 1 },
|
|
264
|
+
{ surface: "operational.versions", count: versions },
|
|
265
|
+
],
|
|
266
|
+
survives: ["removal_record", "deny-list", "removal_tombstone"],
|
|
267
|
+
noop,
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ── the repair ──────────────────────────────────────────────────────────────
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The archive reason `sleep/dedup.ts` writes when it merges a duplicate. It is
|
|
276
|
+
* spelled here rather than imported because `store/` sits BELOW `sleep/` and
|
|
277
|
+
* must not depend on it; `test/store.test.ts` pins the two spellings equal, so
|
|
278
|
+
* a rename cannot silently make this door match nothing.
|
|
279
|
+
*/
|
|
280
|
+
export const MERGED_ARCHIVE_REASON = "merged";
|
|
281
|
+
|
|
282
|
+
/** The durable name a restore appends. Registered in the dashboard's vocabulary. */
|
|
283
|
+
export const UNMERGE_EVENT = "memory.unmerged";
|
|
284
|
+
|
|
285
|
+
/** What a restore did, in ids and counts. Never a body (§5 G10). */
|
|
286
|
+
export interface UnmergeRecord {
|
|
287
|
+
readonly event: typeof UNMERGE_EVENT;
|
|
288
|
+
readonly day: number;
|
|
289
|
+
/** The row put back on its feet. */
|
|
290
|
+
readonly candidateId: string;
|
|
291
|
+
/** What it had been merged into, read from the durable merge record. Null
|
|
292
|
+
* when no merge event survives the retention window — the archive reason
|
|
293
|
+
* still says `merged`, and the restore is still the right answer. */
|
|
294
|
+
readonly originalId: string | null;
|
|
295
|
+
/** The lived day the merge was recorded on. */
|
|
296
|
+
readonly mergedOnDay: number | null;
|
|
297
|
+
/**
|
|
298
|
+
* The use the merge credited to the original — and which this restore
|
|
299
|
+
* deliberately LEAVES STANDING. Taking it back would rewrite a physics count
|
|
300
|
+
* whose band may already have been materialized and whose crossing may
|
|
301
|
+
* already be a durable row, in order to undo a single use. Recording the
|
|
302
|
+
* number here is what makes the credit auditable without touching it.
|
|
303
|
+
*/
|
|
304
|
+
readonly usesDelta: number | null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export interface UnmergeReport {
|
|
308
|
+
readonly id: string;
|
|
309
|
+
readonly record: UnmergeRecord;
|
|
310
|
+
/** True when the row was already live: a repeated repair changes nothing. */
|
|
311
|
+
readonly noop: boolean;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Put back a row the dedup pass archived as a duplicate — the repair half of
|
|
316
|
+
* the finding in `sleep/NOTES.md` §12 (probe H).
|
|
317
|
+
*
|
|
318
|
+
* It lives on this seam, beside the chase, for the same structural reason:
|
|
319
|
+
* holding a `Store` gives you no way to un-archive anything, and the
|
|
320
|
+
* caller-universality test pins who may import this file at all. The store's
|
|
321
|
+
* public surface has no general `unarchive`, and it must not grow one — a
|
|
322
|
+
* pruned row, a revised predecessor and a removed row are all archived, and
|
|
323
|
+
* each is archived for a reason a repair tool has no business reversing. This
|
|
324
|
+
* door undoes exactly ONE reason and refuses every other by name.
|
|
325
|
+
*
|
|
326
|
+
* The refusals, in order: no capability; a removed id (the deny-list answers
|
|
327
|
+
* before anything else); an unknown id; a row with a successor (restoring it
|
|
328
|
+
* would put two live versions in one revision chain); an archive reason that
|
|
329
|
+
* is not the merge's.
|
|
330
|
+
*
|
|
331
|
+
* **Box 3 is re-indexed, and the reason is a moving target.** On master as this
|
|
332
|
+
* was written, `archive()` left box 3 alone — no deindex, no vector drop — so a
|
|
333
|
+
* restore needed nothing. That stops being true the moment `archive()` starts
|
|
334
|
+
* DEINDEXING, which PR #64 (`overnight/df-live-rows`) adds so document frequency
|
|
335
|
+
* is counted over live rows; whichever of the two lands second would otherwise
|
|
336
|
+
* leave this door restoring a row that is live, listed in `beliefs(entity)`, and
|
|
337
|
+
* invisible to lexical recall. So the restore calls `access.reindexLexical`
|
|
338
|
+
* unconditionally: on today's master that rewrites the same `doc_tokens` rows
|
|
339
|
+
* the id already had, and after #64 it is the only thing standing between the
|
|
340
|
+
* repair and a half-restored belief. The EMBEDDING is untouched in both worlds —
|
|
341
|
+
* `indexDoc` writes a vector only when handed one, and it is not handed one —
|
|
342
|
+
* because a repair that made a paid embedding call, or dropped a vector nothing
|
|
343
|
+
* here can recompute, would be a worse bug than the one it is fixing.
|
|
344
|
+
*
|
|
345
|
+
* What the restore does NOT do is erase the merge: the `sleep.merged.<id>` meta record and the `memory.merged`
|
|
346
|
+
* event both stay exactly where they are, and `memory.unmerged` is appended
|
|
347
|
+
* beside them. Constitution 7: the history is the point, and a repair that
|
|
348
|
+
* tidied away the evidence of the bug would be the same class of mistake as
|
|
349
|
+
* the bug.
|
|
350
|
+
*/
|
|
351
|
+
export function unarchiveMerged(store: Store, id: string): UnmergeReport {
|
|
352
|
+
const access = GRANTS.get(store);
|
|
353
|
+
if (access === undefined) {
|
|
354
|
+
throw new StoreError("OWNER_OP_UNGRANTED", { id, site: "unarchiveMerged" });
|
|
355
|
+
}
|
|
356
|
+
if (access.isDenied(id)) throw new StoreError("REMOVED", { id, by: "owner" });
|
|
357
|
+
const row = access.rawRow(id);
|
|
358
|
+
if (row === undefined) throw new StoreError("ID_UNKNOWN", { id });
|
|
359
|
+
if (row.superseded_by !== null) {
|
|
360
|
+
throw new StoreError("UNMERGE_SUPERSEDED", { id, successor: row.superseded_by });
|
|
361
|
+
}
|
|
362
|
+
if (row.archived === 1 && row.archived_reason !== MERGED_ARCHIVE_REASON) {
|
|
363
|
+
throw new StoreError("UNMERGE_NOT_A_MERGE", { id, reason: row.archived_reason });
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const report = access.ownerMutate("unarchiveMerged", (db) => {
|
|
367
|
+
const noop = row.archived === 0;
|
|
368
|
+
const merge = db.get<{ day: number; payload: string | null }>(
|
|
369
|
+
`SELECT day, payload FROM events
|
|
370
|
+
WHERE name = 'memory.merged' AND ref = ?
|
|
371
|
+
ORDER BY seq DESC LIMIT 1`,
|
|
372
|
+
id,
|
|
373
|
+
);
|
|
374
|
+
const payload = readMergePayload(merge?.payload ?? null);
|
|
375
|
+
const record: UnmergeRecord = {
|
|
376
|
+
event: UNMERGE_EVENT,
|
|
377
|
+
day: store.livedDay(),
|
|
378
|
+
candidateId: id,
|
|
379
|
+
originalId: payload.originalId,
|
|
380
|
+
mergedOnDay: merge?.day ?? null,
|
|
381
|
+
usesDelta: payload.usesDelta,
|
|
382
|
+
};
|
|
383
|
+
// ALREADY LIVE: say so, and write NOTHING. A `memory.unmerged` row appended
|
|
384
|
+
// here would read "the owner put this back" about a restore that never
|
|
385
|
+
// happened — a durable lie in the one log the owner is asked to trust. The
|
|
386
|
+
// CLI's own loop skips live rows, and that is not a substitute for this
|
|
387
|
+
// door being honest by itself.
|
|
388
|
+
if (noop) return { id, record, noop };
|
|
389
|
+
|
|
390
|
+
db.run("UPDATE memories SET archived = 0, archived_reason = NULL WHERE id = ?", id);
|
|
391
|
+
// Recorded inside the same transaction, and latched on the id, so a second
|
|
392
|
+
// `--apply` over the same store writes nothing at all (§5 G3).
|
|
393
|
+
store.appendEvent({
|
|
394
|
+
name: UNMERGE_EVENT,
|
|
395
|
+
day: record.day,
|
|
396
|
+
ref: id,
|
|
397
|
+
dedupKey: `${UNMERGE_EVENT}:${id}`,
|
|
398
|
+
payload: { ...record },
|
|
399
|
+
});
|
|
400
|
+
return { id, record, noop };
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// Box 2 first, box 3 after the commit — the same order `Store.put`,
|
|
404
|
+
// `revise` and `supersede` use (`index.ts`: `this.mutate(...)` then
|
|
405
|
+
// `indexOne`). Box 3 is the rebuildable box; indexing inside box 2's
|
|
406
|
+
// transaction would be the one write in the system that inverted that order,
|
|
407
|
+
// and a re-index that ran and then rolled back would leave the index ahead of
|
|
408
|
+
// the row rather than behind it.
|
|
409
|
+
if (!report.noop) access.reindexLexical(id);
|
|
410
|
+
return report;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Ids and numbers out of the merge record's JSON, or nulls. Never throws. */
|
|
414
|
+
function readMergePayload(raw: string | null): {
|
|
415
|
+
originalId: string | null;
|
|
416
|
+
usesDelta: number | null;
|
|
417
|
+
} {
|
|
418
|
+
if (raw === null) return { originalId: null, usesDelta: null };
|
|
419
|
+
try {
|
|
420
|
+
const parsed = JSON.parse(raw) as Record<string, unknown>;
|
|
421
|
+
return {
|
|
422
|
+
originalId: typeof parsed["originalId"] === "string" ? parsed["originalId"] : null,
|
|
423
|
+
usesDelta: typeof parsed["usesDelta"] === "number" ? parsed["usesDelta"] : null,
|
|
424
|
+
};
|
|
425
|
+
} catch {
|
|
426
|
+
return { originalId: null, usesDelta: null };
|
|
427
|
+
}
|
|
428
|
+
}
|