dsh-layers-hippocampus 1.7.2 → 1.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,157 @@
1
+ /**
2
+ * v1.7.3 (module 2): the raw-layer tombstone OVERLAY.
3
+ *
4
+ * The raw log is the one append-only source of truth, and it used to carry
5
+ * bookkeeping lines inside it:
6
+ *
7
+ * {"type":"raw_tombstone","hash":"…","timestamp":…}
8
+ *
9
+ * That polluted the log: every reader had to special-case a line that is not a
10
+ * turn, `deep_raw` cursors had to skip it, and the file no longer meant "turns in
11
+ * the order they happened". Worse, the marker lived in the SAME file as the data
12
+ * it was hiding, so "purge" could never be verified by looking at the log alone.
13
+ *
14
+ * This module moves that overlay OUT of the log:
15
+ *
16
+ * - `state/raw-tombstones.json` holds the keys, the log holds nothing but turns;
17
+ * - matching is two-layered, because two different things get deleted:
18
+ * * `turnIds` — a whole captured turn (`turn_<seq>_<role>`), and
19
+ * * `contentHashes` — a FRAGMENT, i.e. an episode distilled out of a turn.
20
+ * Deleting an episode must not have to delete the whole turn, and deleting a
21
+ * turn must not silently keep its fragments readable.
22
+ *
23
+ * Everything here is a pure load/mutate/write object: no raw shard is touched.
24
+ */
25
+
26
+ import path from 'node:path';
27
+ import { ensureDir, readJson, sha256, writeJsonAtomic } from './util.js';
28
+
29
+ export const RAW_TOMBSTONE_SCHEMA = 1;
30
+ export const RAW_TOMBSTONE_FILE = 'raw-tombstones.json';
31
+ /** How many keys we keep before the oldest are dropped (a tombstone must outlive the log anyway). */
32
+ export const MAX_TOMBSTONE_KEYS = 200_000;
33
+
34
+ /**
35
+ * A tombstone store is deliberately dumb: two string→timestamp maps and a file.
36
+ * `has()` must stay O(1) because `deep_raw` calls it once per scanned row.
37
+ */
38
+ export class RawTombstoneStore {
39
+ constructor({ file, clock } = {}) {
40
+ this.file = file ?? path.join('state', RAW_TOMBSTONE_FILE);
41
+ this.clock = clock ?? { now: () => Date.now() };
42
+ this._loaded = null;
43
+ }
44
+
45
+ /** Load (and normalise) the overlay. Never throws: a broken file means "no tombstones". */
46
+ load() {
47
+ if (this._loaded) return this._loaded;
48
+ const raw = readJson(this.file, null);
49
+ const turnIds = raw && typeof raw.turnIds === 'object' && raw.turnIds !== null ? { ...raw.turnIds } : {};
50
+ const contentHashes =
51
+ raw && typeof raw.contentHashes === 'object' && raw.contentHashes !== null ? { ...raw.contentHashes } : {};
52
+ this._loaded = { schema: RAW_TOMBSTONE_SCHEMA, turnIds, contentHashes, updatedAt: raw?.updatedAt ?? null };
53
+ return this._loaded;
54
+ }
55
+
56
+ /** True when EITHER key matches. Both arguments are optional. */
57
+ has({ turnId = null, contentHash = null } = {}) {
58
+ const state = this.load();
59
+ if (typeof turnId === 'string' && turnId.length > 0 && state.turnIds[turnId] !== undefined) return true;
60
+ if (typeof contentHash === 'string' && contentHash.length > 0 && state.contentHashes[contentHash] !== undefined) return true;
61
+ return false;
62
+ }
63
+
64
+ /** True when the literal text is tombstoned by content hash (used by readers that only have the text). */
65
+ hasContent(content) {
66
+ if (typeof content !== 'string') return false;
67
+ return this.has({ contentHash: sha256(content) });
68
+ }
69
+
70
+ /**
71
+ * Record keys. Returns `{added, total}`; `added` counts keys that were NOT already present,
72
+ * so a repeat purge is a no-op instead of growing the file.
73
+ */
74
+ add({ turnIds = [], contentHashes = [], at = null } = {}) {
75
+ const state = this.load();
76
+ const stamp = Number.isFinite(at) ? at : this.clock.now();
77
+ let added = 0;
78
+ for (const turnId of turnIds) {
79
+ if (typeof turnId !== 'string' || turnId.length === 0) continue;
80
+ if (state.turnIds[turnId] !== undefined) continue;
81
+ state.turnIds[turnId] = stamp;
82
+ added += 1;
83
+ }
84
+ for (const hash of contentHashes) {
85
+ if (typeof hash !== 'string' || hash.length === 0) continue;
86
+ if (state.contentHashes[hash] !== undefined) continue;
87
+ state.contentHashes[hash] = stamp;
88
+ added += 1;
89
+ }
90
+ if (added > 0) {
91
+ state.updatedAt = stamp;
92
+ this._trim(state);
93
+ this.write();
94
+ }
95
+ return { added, total: this.size() };
96
+ }
97
+
98
+ /** Remove one key (used by `restore`). Returns the number of keys actually removed. */
99
+ remove({ turnIds = [], contentHashes = [] } = {}) {
100
+ const state = this.load();
101
+ let removed = 0;
102
+ for (const turnId of turnIds) {
103
+ if (state.turnIds[turnId] !== undefined) {
104
+ delete state.turnIds[turnId];
105
+ removed += 1;
106
+ }
107
+ }
108
+ for (const hash of contentHashes) {
109
+ if (state.contentHashes[hash] !== undefined) {
110
+ delete state.contentHashes[hash];
111
+ removed += 1;
112
+ }
113
+ }
114
+ if (removed > 0) {
115
+ state.updatedAt = this.clock.now();
116
+ this.write();
117
+ }
118
+ return removed;
119
+ }
120
+
121
+ size() {
122
+ const state = this.load();
123
+ return Object.keys(state.turnIds).length + Object.keys(state.contentHashes).length;
124
+ }
125
+
126
+ list() {
127
+ const state = this.load();
128
+ return { turnIds: Object.keys(state.turnIds), contentHashes: Object.keys(state.contentHashes) };
129
+ }
130
+
131
+ status() {
132
+ const state = this.load();
133
+ return {
134
+ schema: RAW_TOMBSTONE_SCHEMA,
135
+ file: this.file,
136
+ turnIds: Object.keys(state.turnIds).length,
137
+ contentHashes: Object.keys(state.contentHashes).length,
138
+ updatedAt: state.updatedAt,
139
+ };
140
+ }
141
+
142
+ write() {
143
+ ensureDir(path.dirname(this.file));
144
+ writeJsonAtomic(this.file, this._loaded);
145
+ return this._loaded;
146
+ }
147
+
148
+ /** Oldest-first eviction; keys are inserted in time order, so `Object.keys` order is the age order. */
149
+ _trim(state) {
150
+ const limit = Math.floor(MAX_TOMBSTONE_KEYS / 2);
151
+ for (const bucket of ['turnIds', 'contentHashes']) {
152
+ const keys = Object.keys(state[bucket]);
153
+ if (keys.length <= limit) continue;
154
+ for (const key of keys.slice(0, keys.length - limit)) delete state[bucket][key];
155
+ }
156
+ }
157
+ }
package/src/webRoutes.js CHANGED
@@ -71,6 +71,7 @@ export const EDITABLE_CONFIG_KEYS = Object.freeze([
71
71
  // only move data between spaces and track the native context window.
72
72
  'context.autoSpaceFollow',
73
73
  'context.autoRolloverEnabled',
74
+ 'context.handoffClearRatio',
74
75
  'context.autoRolloverThreshold',
75
76
  'context.nativeTokenLimit',
76
77
  // Security red line (v1.5.1): the operator decides whether model-initiated
@@ -102,6 +103,12 @@ export const EDITABLE_CONFIG_KEYS = Object.freeze([
102
103
  'context.mode',
103
104
  'context.hintsIdleSilenceAfter',
104
105
  'context.statePointer.enabled',
106
+ // v1.7.3 physical pruning (module 1).
107
+ 'context.smartPrune.enabled',
108
+ 'context.smartPrune.threshold',
109
+ 'context.smartPrune.keepRecentTurns',
110
+ 'context.smartPrune.minChars',
111
+ 'context.smartPrune.maxNodes',
105
112
  'context.statePointer.maxChars',
106
113
  'context.coldStartInjectSummary',
107
114
  'context.coldStartInjectRecentTurns',