dsh-layers-hippocampus 1.7.3 → 1.7.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 +49 -0
- package/README.md +2 -2
- package/docs/ARCHITECTURE.zh.md +2 -2
- package/index.js +111 -9
- package/package.json +1 -1
- package/src/activity.js +220 -0
- package/src/config.js +75 -2
- package/src/graph.js +73 -0
- package/src/memory.js +10 -1
- package/src/webRoutes.js +33 -0
- package/ui/client.js +509 -45
package/src/config.js
CHANGED
|
@@ -43,6 +43,10 @@ export const FLAT_ALIASES = Object.freeze({
|
|
|
43
43
|
export const EVENT_TIME_POLICIES = Object.freeze(['content', 'explicit', 'ingest']);
|
|
44
44
|
/** Raw-turn retention modes: off / gated by value / everything. */
|
|
45
45
|
export const RAW_MODES = Object.freeze(['off', 'salience', 'all']);
|
|
46
|
+
/** v1.7.4 graph glow presets: named palettes the panel can render without a custom colour. */
|
|
47
|
+
export const GRAPH_GLOW_PRESETS = Object.freeze(['accent', 'vivid', 'warm']);
|
|
48
|
+
/** v1.7.4 canvas tint modes: follow the theme / force a tint / stay neutral. */
|
|
49
|
+
export const GRAPH_CANVAS_TINTS = Object.freeze(['auto', 'tint', 'none']);
|
|
46
50
|
|
|
47
51
|
/**
|
|
48
52
|
* Tool suffixes this plugin registers (`${toolPrefix}_<suffix>`). This is the
|
|
@@ -99,6 +103,9 @@ export const CONFIG_KEYS = Object.freeze([
|
|
|
99
103
|
'dream',
|
|
100
104
|
'enableReflection',
|
|
101
105
|
'graphMaxEdges',
|
|
106
|
+
// v1.7.4: read-time node ceiling, applied next to graphMaxEdges.
|
|
107
|
+
'graphMaxNodes',
|
|
108
|
+
'ui',
|
|
102
109
|
'storageBudgetMb',
|
|
103
110
|
'maxRecallTopK',
|
|
104
111
|
'retrieval',
|
|
@@ -181,6 +188,20 @@ export const DEFAULT_CONFIG = Object.freeze({
|
|
|
181
188
|
// Edges returned by /api/memory/graph. The offline cache may hold up to
|
|
182
189
|
// GRAPH_HARD_CAP; this is the read-time ceiling, so changing it is cheap.
|
|
183
190
|
graphMaxEdges: 500,
|
|
191
|
+
// Nodes returned by the same route (v1.7.4): trimming is a pure projection over
|
|
192
|
+
// the cached graph, so the ceiling is independent from graphMaxEdges.
|
|
193
|
+
graphMaxNodes: 80,
|
|
194
|
+
// v1.7.4 graph presentation only. None of these values changes what is stored;
|
|
195
|
+
// they are read by the Web panel (and the activity route) and never by recall.
|
|
196
|
+
ui: Object.freeze({
|
|
197
|
+
graphGlowEnabled: true,
|
|
198
|
+
graphGlowPreset: 'accent',
|
|
199
|
+
// `null` = follow the preset; `#rrggbb` = an explicit glow colour.
|
|
200
|
+
graphGlowCustom: null,
|
|
201
|
+
graphCanvasTint: 'auto',
|
|
202
|
+
// Opt-in persistence of the recent-activity ring (state/activity.json).
|
|
203
|
+
graphActivityPersist: false,
|
|
204
|
+
}),
|
|
184
205
|
// Display threshold for the storage ring (MB). Never evicts anything.
|
|
185
206
|
storageBudgetMb: 50,
|
|
186
207
|
maxRecallTopK: 100,
|
|
@@ -492,6 +513,23 @@ function requireEnum(value, key, allowed) {
|
|
|
492
513
|
return value;
|
|
493
514
|
}
|
|
494
515
|
|
|
516
|
+
/**
|
|
517
|
+
* v1.7.4: `null` (follow the preset) or an explicit `#rrggbb` colour. Anything
|
|
518
|
+
* else is a config error carrying the key path — a typo must not silently render
|
|
519
|
+
* an invisible glow.
|
|
520
|
+
*/
|
|
521
|
+
function requireHexColorOrNull(value, key) {
|
|
522
|
+
if (value === null || value === undefined) return null;
|
|
523
|
+
if (typeof value !== 'string' || !/^#[0-9a-fA-F]{6}$/.test(value)) {
|
|
524
|
+
throw configError(
|
|
525
|
+
ErrorCodes.CONFIG_INVALID_TYPE,
|
|
526
|
+
`config.${key} must be null or a #rrggbb colour, got ${JSON.stringify(value)}`,
|
|
527
|
+
{ key, expected: 'null | #rrggbb', got: value },
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
return value;
|
|
531
|
+
}
|
|
532
|
+
|
|
495
533
|
function requireStringArray(value, key) {
|
|
496
534
|
if (!Array.isArray(value)) {
|
|
497
535
|
throw configError(ErrorCodes.CONFIG_INVALID_TYPE, `config.${key} must be an array of strings`, {
|
|
@@ -539,7 +577,7 @@ function normaliseRaw(raw) {
|
|
|
539
577
|
}
|
|
540
578
|
|
|
541
579
|
const out = { ...nested };
|
|
542
|
-
for (const group of ['retrieval', 'context', 'index', 'temp', 'decay', 'activeSet', 'raw', 'delete', 'review', 'migration', 'identity', 'tools', 'dream', 'tagging', 'features', 'l2Merge']) {
|
|
580
|
+
for (const group of ['retrieval', 'context', 'index', 'temp', 'decay', 'activeSet', 'raw', 'delete', 'review', 'migration', 'identity', 'tools', 'dream', 'tagging', 'features', 'l2Merge', 'ui']) {
|
|
543
581
|
if (isPlainObject(nested[group])) out[group] = { ...nested[group] };
|
|
544
582
|
}
|
|
545
583
|
for (const [alias, path] of Object.entries(FLAT_ALIASES)) {
|
|
@@ -757,7 +795,7 @@ function attachConfigMeta(config, migration) {
|
|
|
757
795
|
function buildResolvedConfig(raw = {}, { env = process.env } = {}) {
|
|
758
796
|
const input = normaliseRaw(raw);
|
|
759
797
|
|
|
760
|
-
for (const group of ['retrieval', 'context', 'index', 'temp', 'decay', 'activeSet', 'raw', 'delete', 'review', 'migration', 'identity', 'tools', 'dream', 'tagging', 'features', 'l2Merge']) {
|
|
798
|
+
for (const group of ['retrieval', 'context', 'index', 'temp', 'decay', 'activeSet', 'raw', 'delete', 'review', 'migration', 'identity', 'tools', 'dream', 'tagging', 'features', 'l2Merge', 'ui']) {
|
|
761
799
|
if (group in input && input[group] !== undefined && !isPlainObject(input[group])) {
|
|
762
800
|
throw configError(ErrorCodes.CONFIG_INVALID_TYPE, `config.${group} must be an object`, { key: group });
|
|
763
801
|
}
|
|
@@ -1505,6 +1543,39 @@ function buildResolvedConfig(raw = {}, { env = process.env } = {}) {
|
|
|
1505
1543
|
'graphMaxEdges',
|
|
1506
1544
|
{ min: 100, max: 2000 },
|
|
1507
1545
|
);
|
|
1546
|
+
// v1.7.4 node ceiling: same fail-fast contract, independent from the edge limit.
|
|
1547
|
+
const graphMaxNodes = requireInteger(
|
|
1548
|
+
input.graphMaxNodes === undefined ? defaults.graphMaxNodes : input.graphMaxNodes,
|
|
1549
|
+
'graphMaxNodes',
|
|
1550
|
+
{ min: 10, max: 2000 },
|
|
1551
|
+
);
|
|
1552
|
+
// v1.7.4 UI-only presentation settings. They never change stored data, but a
|
|
1553
|
+
// typo must still fail fast instead of silently rendering an invisible graph.
|
|
1554
|
+
const uiIn = input.ui ?? {};
|
|
1555
|
+
const uiConfig = Object.freeze({
|
|
1556
|
+
graphGlowEnabled: requireBoolean(
|
|
1557
|
+
uiIn.graphGlowEnabled === undefined ? defaults.ui.graphGlowEnabled : uiIn.graphGlowEnabled,
|
|
1558
|
+
'ui.graphGlowEnabled',
|
|
1559
|
+
),
|
|
1560
|
+
graphGlowPreset: requireEnum(
|
|
1561
|
+
uiIn.graphGlowPreset === undefined ? defaults.ui.graphGlowPreset : uiIn.graphGlowPreset,
|
|
1562
|
+
'ui.graphGlowPreset',
|
|
1563
|
+
GRAPH_GLOW_PRESETS,
|
|
1564
|
+
),
|
|
1565
|
+
graphGlowCustom: requireHexColorOrNull(
|
|
1566
|
+
uiIn.graphGlowCustom === undefined ? defaults.ui.graphGlowCustom : uiIn.graphGlowCustom,
|
|
1567
|
+
'ui.graphGlowCustom',
|
|
1568
|
+
),
|
|
1569
|
+
graphCanvasTint: requireEnum(
|
|
1570
|
+
uiIn.graphCanvasTint === undefined ? defaults.ui.graphCanvasTint : uiIn.graphCanvasTint,
|
|
1571
|
+
'ui.graphCanvasTint',
|
|
1572
|
+
GRAPH_CANVAS_TINTS,
|
|
1573
|
+
),
|
|
1574
|
+
graphActivityPersist: requireBoolean(
|
|
1575
|
+
uiIn.graphActivityPersist === undefined ? defaults.ui.graphActivityPersist : uiIn.graphActivityPersist,
|
|
1576
|
+
'ui.graphActivityPersist',
|
|
1577
|
+
),
|
|
1578
|
+
});
|
|
1508
1579
|
// Ring budget: integer MB, fail-fast. The UI recomputes the ring from this
|
|
1509
1580
|
// value, so an out-of-range number is a config bug, not something to clamp.
|
|
1510
1581
|
const storageBudgetMb = requireInteger(
|
|
@@ -1533,6 +1604,8 @@ function buildResolvedConfig(raw = {}, { env = process.env } = {}) {
|
|
|
1533
1604
|
dream: dreamConfig,
|
|
1534
1605
|
enableReflection,
|
|
1535
1606
|
graphMaxEdges,
|
|
1607
|
+
graphMaxNodes,
|
|
1608
|
+
ui: uiConfig,
|
|
1536
1609
|
storageBudgetMb,
|
|
1537
1610
|
maxRecallTopK,
|
|
1538
1611
|
retrieval: { topK: retrievalTopK },
|
package/src/graph.js
CHANGED
|
@@ -137,6 +137,79 @@ export function buildGraph(events = [], { maxEdges = GRAPH_HARD_CAP, topNeighbor
|
|
|
137
137
|
};
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
/** Lexicographic (code-unit) comparison, so trimming is locale-independent. */
|
|
141
|
+
function compareText(a, b) {
|
|
142
|
+
const left = String(a);
|
|
143
|
+
const right = String(b);
|
|
144
|
+
if (left < right) return -1;
|
|
145
|
+
if (left > right) return 1;
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Read-time trim: cap the cached graph to `maxNodes` nodes and `maxEdges` edges
|
|
151
|
+
* without touching the cache file.
|
|
152
|
+
*
|
|
153
|
+
* Eviction is deterministic and purely local:
|
|
154
|
+
* - nodes sort by `events` ASC, then `weight` ASC, then `id` ASC; the nodes with
|
|
155
|
+
* the fewest events are evicted first, so the tail is what survives;
|
|
156
|
+
* - edges keep only endpoints that survived, then sort by `weight` DESC, then
|
|
157
|
+
* `source`, then `target`, and are sliced to `maxEdges`.
|
|
158
|
+
*
|
|
159
|
+
* The input is never mutated: arrays and node/edge objects in the result are
|
|
160
|
+
* copies. Every other field of the source graph is carried over unchanged.
|
|
161
|
+
*
|
|
162
|
+
* @param {object} graph a `buildGraph()`/`GraphCache.view()` shaped object
|
|
163
|
+
* @param {{maxNodes?: number, maxEdges?: number}} [options]
|
|
164
|
+
* @returns {object} new graph object with trim accounting
|
|
165
|
+
*/
|
|
166
|
+
export function trimGraph(graph, { maxNodes = 80, maxEdges = 500 } = {}) {
|
|
167
|
+
const source = graph && typeof graph === 'object' ? graph : {};
|
|
168
|
+
const sourceNodes = Array.isArray(source.nodes) ? source.nodes : [];
|
|
169
|
+
const sourceEdges = Array.isArray(source.edges) ? source.edges : [];
|
|
170
|
+
const totalNodes = sourceNodes.length;
|
|
171
|
+
const totalEdges = sourceEdges.length;
|
|
172
|
+
const nodeLimit = Math.max(0, Math.trunc(Number(maxNodes)) || 0);
|
|
173
|
+
const edgeLimit = Math.max(0, Math.trunc(Number(maxEdges)) || 0);
|
|
174
|
+
|
|
175
|
+
const byEvictionPriority = [...sourceNodes].sort((a, b) => {
|
|
176
|
+
const eventsA = Number.isFinite(a?.events) ? a.events : 0;
|
|
177
|
+
const eventsB = Number.isFinite(b?.events) ? b.events : 0;
|
|
178
|
+
if (eventsA !== eventsB) return eventsA - eventsB;
|
|
179
|
+
const weightA = Number.isFinite(a?.weight) ? a.weight : 0;
|
|
180
|
+
const weightB = Number.isFinite(b?.weight) ? b.weight : 0;
|
|
181
|
+
if (weightA !== weightB) return weightA - weightB;
|
|
182
|
+
return compareText(a?.id, b?.id);
|
|
183
|
+
});
|
|
184
|
+
const dropCount = Math.max(0, totalNodes - nodeLimit);
|
|
185
|
+
const keptNodes = byEvictionPriority.slice(dropCount).map((node) => ({ ...node }));
|
|
186
|
+
const keptIds = new Set(keptNodes.map((node) => String(node.id)));
|
|
187
|
+
|
|
188
|
+
const keptEdges = sourceEdges
|
|
189
|
+
.filter((edge) => keptIds.has(String(edge?.source)) && keptIds.has(String(edge?.target)))
|
|
190
|
+
.sort((a, b) => {
|
|
191
|
+
const weightA = Number.isFinite(a?.weight) ? a.weight : 0;
|
|
192
|
+
const weightB = Number.isFinite(b?.weight) ? b.weight : 0;
|
|
193
|
+
if (weightA !== weightB) return weightB - weightA;
|
|
194
|
+
return compareText(a?.source, b?.source) || compareText(a?.target, b?.target);
|
|
195
|
+
})
|
|
196
|
+
.slice(0, edgeLimit)
|
|
197
|
+
.map((edge) => ({ ...edge }));
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
...source,
|
|
201
|
+
nodes: keptNodes,
|
|
202
|
+
edges: keptEdges,
|
|
203
|
+
nodeCount: keptNodes.length,
|
|
204
|
+
edgeCount: keptEdges.length,
|
|
205
|
+
totalNodes,
|
|
206
|
+
totalEdges,
|
|
207
|
+
droppedNodes: totalNodes - keptNodes.length,
|
|
208
|
+
droppedEdges: totalEdges - keptEdges.length,
|
|
209
|
+
truncated: totalNodes > keptNodes.length || totalEdges > keptEdges.length,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
140
213
|
/** Read-only view over the cached graph file. */
|
|
141
214
|
export class GraphCache {
|
|
142
215
|
constructor({ dataDir, clock }) {
|
package/src/memory.js
CHANGED
|
@@ -357,13 +357,17 @@ export class Memory {
|
|
|
357
357
|
* @param {object} [options.clock] Clock with now(); never defaulted in tests
|
|
358
358
|
* @param {object} [options.policy] partial policy overrides
|
|
359
359
|
* @param {object} [options.config] { maxSegmentRecords, context }
|
|
360
|
+
* @param {object|null} [options.activity] v1.7.4 activity ring (read-time hints only)
|
|
360
361
|
*/
|
|
361
|
-
constructor({ dataDir, clock = new SystemClock(), policy = {}, config = {}, spaces = null, spacesFactory = null, transfer = null, federation = null, dedupe = null, spaceId = DEFAULT_SPACE_ID }) {
|
|
362
|
+
constructor({ dataDir, clock = new SystemClock(), policy = {}, config = {}, spaces = null, spacesFactory = null, transfer = null, federation = null, dedupe = null, spaceId = DEFAULT_SPACE_ID, activity = null }) {
|
|
362
363
|
if (!dataDir) throw new MemoryError(ErrorCodes.INVALID_CONFIG, 'dataDir is required');
|
|
363
364
|
assertClock(clock);
|
|
364
365
|
this.dataDir = path.resolve(dataDir);
|
|
365
366
|
this.clock = clock;
|
|
366
367
|
this.config = config;
|
|
368
|
+
// v1.7.4: optional activity ring. `null` means "no hints" and every caller must
|
|
369
|
+
// behave identically; a store failure is swallowed at the use site.
|
|
370
|
+
this.activity = activity;
|
|
367
371
|
// v1.5.6 pre-ingest tagging: counters for the panel, and a warning hook the
|
|
368
372
|
// plugin adapter wires to the Host logger (a miss must never break a write).
|
|
369
373
|
this._autoTag = { applied: 0, misses: 0, lastMissAt: null };
|
|
@@ -1447,6 +1451,11 @@ export class Memory {
|
|
|
1447
1451
|
...(this._boot && this._boot.status !== 'ok' ? [`boot-check:${this._boot.status}`] : []),
|
|
1448
1452
|
],
|
|
1449
1453
|
};
|
|
1454
|
+
// v1.7.4 activity ring: recorded by the RUNTIME (`index.js#noteRecallActivity`),
|
|
1455
|
+
// not here. In a multi-space profile a recall is routed through `spaces.run()`
|
|
1456
|
+
// and cross-space reads fan out to per-space `Memory` instances, so a hook at
|
|
1457
|
+
// this level only fires for whichever instance happened to serve the call and
|
|
1458
|
+
// the graph's default ring stayed empty. See index.js for the single hook.
|
|
1450
1459
|
return this.applyPayloadBudget(payload);
|
|
1451
1460
|
}
|
|
1452
1461
|
|
package/src/webRoutes.js
CHANGED
|
@@ -30,6 +30,8 @@ export const MEMORY_API = Object.freeze({
|
|
|
30
30
|
stats: '/api/hippocampus/stats',
|
|
31
31
|
config: '/api/hippocampus/config',
|
|
32
32
|
graph: '/api/hippocampus/graph',
|
|
33
|
+
// v1.7.4: bounded recent recall/capture ring (tags only, never bodies).
|
|
34
|
+
activity: '/api/hippocampus/activity',
|
|
33
35
|
retry: '/api/hippocampus/retry',
|
|
34
36
|
transfer: '/api/hippocampus/transfer',
|
|
35
37
|
// Per-memory projection for the panel's human-initiated 分享/转移 button.
|
|
@@ -53,6 +55,13 @@ export const EDITABLE_CONFIG_KEYS = Object.freeze([
|
|
|
53
55
|
'enableReflection',
|
|
54
56
|
'dreamIdleMinutes',
|
|
55
57
|
'graphMaxEdges',
|
|
58
|
+
'graphMaxNodes',
|
|
59
|
+
// v1.7.4 graph presentation (glow / canvas tint) + activity-ring persistence.
|
|
60
|
+
'ui.graphGlowEnabled',
|
|
61
|
+
'ui.graphGlowPreset',
|
|
62
|
+
'ui.graphGlowCustom',
|
|
63
|
+
'ui.graphCanvasTint',
|
|
64
|
+
'ui.graphActivityPersist',
|
|
56
65
|
'storageBudgetMb',
|
|
57
66
|
'maxRecallTopK',
|
|
58
67
|
'retrieval.topK',
|
|
@@ -442,6 +451,29 @@ export function createMemoryRoutes({ runtime, persist = null, logger = null }) {
|
|
|
442
451
|
}
|
|
443
452
|
};
|
|
444
453
|
|
|
454
|
+
// Activity (v1.7.4): the recent recall/capture ring the panel flashes tags from.
|
|
455
|
+
// Read-only and independent of the memory phase: it is a hint, not stored truth.
|
|
456
|
+
const activityHandler = async (req, res) => {
|
|
457
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
458
|
+
sendState(res, 405, runtime, { ok: false, error: { code: 'MEM_E_INVALID_INPUT', message: 'use GET' } });
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
try {
|
|
462
|
+
const url = new URL(String(req.url ?? MEMORY_API.activity), 'http://localhost');
|
|
463
|
+
const sinceRaw = Number(url.searchParams.get('since'));
|
|
464
|
+
const since = Number.isFinite(sinceRaw) && sinceRaw > 0 ? Math.trunc(sinceRaw) : 0;
|
|
465
|
+
// A missing `limit` must mean 64, not 0 (Number(null) === 0 would clamp to 1).
|
|
466
|
+
const limitParam = url.searchParams.get('limit');
|
|
467
|
+
const limitRaw = Number(limitParam);
|
|
468
|
+
const limit = limitParam !== null && Number.isFinite(limitRaw) ? Math.min(Math.max(Math.trunc(limitRaw), 1), 64) : 64;
|
|
469
|
+
const payload = runtime.activityPayload({ spaceId: requestedSpace(req), since, limit });
|
|
470
|
+
sendState(res, 200, runtime, payload);
|
|
471
|
+
} catch (error) {
|
|
472
|
+
log(`activity failed: ${error?.message ?? error}`);
|
|
473
|
+
fail(res, 500, error, runtime);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
|
|
445
477
|
// Retry: only meaningful while the phase is `failed`; the runtime owns the
|
|
446
478
|
// state machine, so this handler never starts a second bootstrap by itself.
|
|
447
479
|
const retryHandler = async (req, res) => {
|
|
@@ -646,6 +678,7 @@ export function createMemoryRoutes({ runtime, persist = null, logger = null }) {
|
|
|
646
678
|
{ kind: 'exact', path: MEMORY_API.stats, handler: statsHandler },
|
|
647
679
|
{ kind: 'exact', path: MEMORY_API.config, handler: configHandler },
|
|
648
680
|
{ kind: 'exact', path: MEMORY_API.graph, handler: graphHandler },
|
|
681
|
+
{ kind: 'exact', path: MEMORY_API.activity, handler: activityHandler },
|
|
649
682
|
{ kind: 'exact', path: MEMORY_API.retry, handler: retryHandler },
|
|
650
683
|
{ kind: 'exact', path: MEMORY_API.transfer, handler: transferHandler },
|
|
651
684
|
{ kind: 'exact', path: MEMORY_API.memories, handler: memoriesHandler },
|