dsh-layers-hippocampus 1.7.0 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1910 -1845
- package/README.md +24 -3
- package/docs/ARCHITECTURE.zh.md +2 -2
- package/index.js +180 -10
- package/package.json +1 -1
- package/src/config.js +41 -0
- package/src/hintFeedback.js +200 -0
- package/src/promptHygiene.js +144 -0
- package/src/utils/promptLayout.js +36 -1
- package/src/webRoutes.js +5 -0
- package/ui/client.js +1006 -675
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt hygiene for INJECTED summaries (v1.7.1).
|
|
3
|
+
*
|
|
4
|
+
* The problem this solves is concrete: a handoff/L3 summary written hours ago says
|
|
5
|
+
* "events 348 / reviews total 346 due 337 / version 1.6.9", and the model spends
|
|
6
|
+
* tokens reading a stale fact and then more tokens contradicting it. Counters are
|
|
7
|
+
* TRUE AT WRITE TIME and false almost immediately, so they must never enter a
|
|
8
|
+
* prompt as if they were knowledge.
|
|
9
|
+
*
|
|
10
|
+
* Two rules, both conservative:
|
|
11
|
+
*
|
|
12
|
+
* 1. **Drop bookkeeping counters** — version strings, `events: N`, `reviews total
|
|
13
|
+
* N/due M`, `gen-000123`, ISO timestamps, `N 条事件` etc. Numbers that carry a
|
|
14
|
+
* LESSON (e.g. "854,668 + 256,000 > 1,048,576") are NOT touched: only the
|
|
15
|
+
* known bookkeeping keys are stripped, never bare digits.
|
|
16
|
+
* 2. **Stamp the snapshot** so a reader knows the text is old even after cleaning.
|
|
17
|
+
*
|
|
18
|
+
* Pure and deterministic: no clock reads inside the transforms (the caller passes
|
|
19
|
+
* `at`), no IO, no regex backtracking traps (all patterns are anchored on literal
|
|
20
|
+
* key names and bounded quantifiers).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** Bookkeeping keys whose numeric value is stale by definition. */
|
|
24
|
+
const COUNTER_KEYS = [
|
|
25
|
+
'version',
|
|
26
|
+
'events',
|
|
27
|
+
'eventcount',
|
|
28
|
+
'activetags',
|
|
29
|
+
'semsummaries',
|
|
30
|
+
'semanticsummaries',
|
|
31
|
+
'summaries',
|
|
32
|
+
'reviewstotal',
|
|
33
|
+
'reviews',
|
|
34
|
+
'due',
|
|
35
|
+
'pinned',
|
|
36
|
+
'shards',
|
|
37
|
+
'rawshards',
|
|
38
|
+
'indexedshards',
|
|
39
|
+
'archives',
|
|
40
|
+
'archivecount',
|
|
41
|
+
'diskbytes',
|
|
42
|
+
'rawbytes',
|
|
43
|
+
'tokens',
|
|
44
|
+
'injected',
|
|
45
|
+
'pending',
|
|
46
|
+
'gen',
|
|
47
|
+
'indexgen',
|
|
48
|
+
'loggen',
|
|
49
|
+
'runs',
|
|
50
|
+
'groups',
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
/** `events: 1446` / `reviews total 346` → drop the clause. */
|
|
54
|
+
const COUNTER_RE = new RegExp(
|
|
55
|
+
`(?:^|[\\s((、,,;;|·])[^;;。\\n|]{0,24}?(?:${COUNTER_KEYS.join('|')})[^;;。\\n|]{0,12}?[::=≈]?\\s*\\d[\\d,._/]*`,
|
|
56
|
+
'i',
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
/** Semver-ish stamps: `1.6.9`, `v1.7.0`. Bounded so `854,668` is never matched. */
|
|
60
|
+
const VERSION_RE = /(?:\bv|\bversion\s*[::=]?\s*)?\b\d+\.\d+\.\d+\b/i;
|
|
61
|
+
|
|
62
|
+
/** `gen-000113`, `ev_ab12…`, ISO timestamps. */
|
|
63
|
+
const MACHINE_ID_RE = /\bgen-\d{3,}\b|\b\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?Z?\b/;
|
|
64
|
+
|
|
65
|
+
/** `348 条事件` / `9 个分片` / `85 个文件` — count + counting noun. */
|
|
66
|
+
const COUNT_NOUN_RE = /\b\d[\d,]*\s*(?:条|个|篇|次|files?|events?|shards?|tokens?|items?)\b/i;
|
|
67
|
+
|
|
68
|
+
/** Sentence/clause separators used when deciding what a "segment" is. */
|
|
69
|
+
const SEGMENT_SEP = /([;;。\n]|(?:\s·\s))/;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* True when this clause is bookkeeping rather than knowledge.
|
|
73
|
+
*
|
|
74
|
+
* Deliberately narrow: a clause is dropped only when it looks like a counter or a
|
|
75
|
+
* machine id. A clause mixing a lesson AND a counter keeps the lesson half only if
|
|
76
|
+
* the counter is a separate `;`-delimited segment — we never rewrite inside a clause.
|
|
77
|
+
*/
|
|
78
|
+
export function isVolatileClause(clause) {
|
|
79
|
+
const text = String(clause ?? '').trim();
|
|
80
|
+
if (text.length === 0) return false;
|
|
81
|
+
if (MACHINE_ID_RE.test(text)) return true;
|
|
82
|
+
if (COUNTER_RE.test(text)) return true;
|
|
83
|
+
if (COUNT_NOUN_RE.test(text) && !/[是为需要必须禁止不要]/.test(text)) return true;
|
|
84
|
+
// A bare version stamp is only volatile when it is the whole clause ("v1.6.9").
|
|
85
|
+
if (VERSION_RE.test(text) && text.replace(VERSION_RE, '').trim().length <= 6) return true;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Strip volatile clauses from a block of injected text.
|
|
91
|
+
*
|
|
92
|
+
* @param {string} text raw summary / persona / active-task text
|
|
93
|
+
* @returns {{text: string, removed: string[]}} cleaned text plus what was dropped
|
|
94
|
+
*/
|
|
95
|
+
export function stripVolatile(text) {
|
|
96
|
+
const raw = String(text ?? '');
|
|
97
|
+
if (raw.trim().length === 0) return { text: '', removed: [] };
|
|
98
|
+
const removed = [];
|
|
99
|
+
const kept = [];
|
|
100
|
+
for (const piece of raw.split(SEGMENT_SEP)) {
|
|
101
|
+
if (piece === undefined) continue;
|
|
102
|
+
if (/^[;;。\n]$/.test(piece) || /^\s·\s$/.test(piece)) {
|
|
103
|
+
kept.push(piece);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (isVolatileClause(piece)) {
|
|
107
|
+
const trimmed = piece.trim();
|
|
108
|
+
if (trimmed) removed.push(trimmed);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
kept.push(piece);
|
|
112
|
+
}
|
|
113
|
+
// Collapse the separators that lost their neighbour and normalise whitespace.
|
|
114
|
+
let out = kept.join('');
|
|
115
|
+
out = out.replace(/^\s*[;;。·]\s*/u, '').replace(/[;;·]\s*(?=[;;。]|$)/gu, '');
|
|
116
|
+
out = out.replace(/\s{2,}/g, ' ').trim();
|
|
117
|
+
if (out.length > 0 && !/[。.!?!?]$/.test(out)) out += '。';
|
|
118
|
+
return { text: out, removed };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `2026-11-24T14:31Z` — the moment the summary was produced, or "unknown". */
|
|
122
|
+
export function snapshotStamp(at) {
|
|
123
|
+
const value = Number(at);
|
|
124
|
+
if (!Number.isFinite(value) || value <= 0) return 'snapshot time unknown';
|
|
125
|
+
return `snapshot ${new Date(value).toISOString().replace(/\.\d{3}Z$/, 'Z')}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Clean a summary and stamp it. This is the ONE entry point every injection site
|
|
130
|
+
* should use, so "clean + dated" cannot be forgotten in one place and applied in
|
|
131
|
+
* another.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} text
|
|
134
|
+
* @param {{at?: number, maxChars?: number, label?: string}} options
|
|
135
|
+
*/
|
|
136
|
+
export function hygienicSummary(text, { at = null, maxChars = 800, label = null } = {}) {
|
|
137
|
+
const { text: cleaned, removed } = stripVolatile(text);
|
|
138
|
+
if (cleaned.length === 0) return { text: '', removed, dropped: true };
|
|
139
|
+
const head = `(${snapshotStamp(at)}${label ? ` · ${label}` : ''} · counters stripped as stale)`;
|
|
140
|
+
const body = cleaned.replace(/\s+/g, ' ').trim();
|
|
141
|
+
const budget = Math.max(0, Number(maxChars) || 0);
|
|
142
|
+
const text_ = `${head} ${body}`.slice(0, budget > 0 ? budget : undefined);
|
|
143
|
+
return { text: text_, removed, dropped: false };
|
|
144
|
+
}
|
|
@@ -16,8 +16,20 @@
|
|
|
16
16
|
export const BLOCK_MARKER = '[Memory Blocks]';
|
|
17
17
|
export const ACTIVE_TASK_MARKER = '[Active Task]';
|
|
18
18
|
export const POINTER_MARKER = '[Memory Index]';
|
|
19
|
+
// v1.7.1: the CURRENT-STATE line (task mode). It is the most volatile thing in the
|
|
20
|
+
// prompt (window pressure changes every turn), so it goes LAST — after every other
|
|
21
|
+
// volatile section — and never before the block marker.
|
|
22
|
+
export const STATE_MARKER = '[Task State]';
|
|
19
23
|
|
|
20
|
-
export function composePrompt({
|
|
24
|
+
export function composePrompt({
|
|
25
|
+
rules = '',
|
|
26
|
+
activeTask = '',
|
|
27
|
+
map = '',
|
|
28
|
+
hints = '',
|
|
29
|
+
blocks = '',
|
|
30
|
+
pointers = '',
|
|
31
|
+
stateLine = '',
|
|
32
|
+
} = {}) {
|
|
21
33
|
const parts = [String(rules).trim()];
|
|
22
34
|
// The active task sits at the very top of the memory section: a new session must
|
|
23
35
|
// know what it was doing before it reads anything else.
|
|
@@ -30,6 +42,9 @@ export function composePrompt({ rules = '', activeTask = '', map = '', hints = '
|
|
|
30
42
|
// v1.6.9 (module 3): index pointers sit UNDER the hints — both are per-turn and
|
|
31
43
|
// both must stay after the block marker so the cacheable prefix never moves.
|
|
32
44
|
if (String(pointers).trim()) parts.push(`${POINTER_MARKER}\n${String(pointers).trim()}`);
|
|
45
|
+
// v1.7.1: the task-state line changes on almost every turn, so it must be the very
|
|
46
|
+
// last section — everything above it stays byte-identical for the prefix cache.
|
|
47
|
+
if (String(stateLine).trim()) parts.push(`${STATE_MARKER}\n${String(stateLine).trim()}`);
|
|
33
48
|
return parts.filter(Boolean).join('\n\n');
|
|
34
49
|
}
|
|
35
50
|
|
|
@@ -70,6 +85,26 @@ export function renderPointers(entries = [], { maxChars = 300 } = {}) {
|
|
|
70
85
|
return lines.join('\n');
|
|
71
86
|
}
|
|
72
87
|
|
|
88
|
+
/**
|
|
89
|
+
* v1.7.1: render the `[Task State]` body.
|
|
90
|
+
*
|
|
91
|
+
* Contract: ONE line, `·`-separated, hard-capped by `maxChars`. This exists because a
|
|
92
|
+
* build/debug task needs "where am I right now" (repo HEAD, window pressure), while
|
|
93
|
+
* memory summarises where it has BEEN. Anything that does not fit the budget is cut,
|
|
94
|
+
* not wrapped — a status dump in the prompt tail is worse than a short line.
|
|
95
|
+
*/
|
|
96
|
+
export function renderStateLine(fields = {}, { maxChars = 300 } = {}) {
|
|
97
|
+
const budget = Math.max(0, Number(maxChars) || 0);
|
|
98
|
+
const parts = [];
|
|
99
|
+
for (const value of Object.values(fields ?? {})) {
|
|
100
|
+
const text = String(value ?? '').replace(/\s+/g, ' ').trim();
|
|
101
|
+
if (text) parts.push(text);
|
|
102
|
+
}
|
|
103
|
+
const line = parts.join(' · ');
|
|
104
|
+
if (budget > 0 && line.length > budget) return line.slice(0, Math.max(0, budget - 1)) + '…';
|
|
105
|
+
return line;
|
|
106
|
+
}
|
|
107
|
+
|
|
73
108
|
/** Map view: tag names + counts only, never event bodies. */
|
|
74
109
|
export function renderMemoryMap(entries = []) {
|
|
75
110
|
return entries.map((entry) => `${entry.tag}(${entry.count ?? entry.events ?? 0})`).join(' · ');
|
package/src/webRoutes.js
CHANGED
|
@@ -98,6 +98,11 @@ export const EDITABLE_CONFIG_KEYS = Object.freeze([
|
|
|
98
98
|
'context.autoInjectIndex.maxChars',
|
|
99
99
|
// v1.6.0 cold-start injection switches (independent; both default on since 1.6.3).
|
|
100
100
|
'context.handoffSummaryTurns',
|
|
101
|
+
// v1.7.1: the task/expert contract switch and the two task-mode behaviours it gates.
|
|
102
|
+
'context.mode',
|
|
103
|
+
'context.hintsIdleSilenceAfter',
|
|
104
|
+
'context.statePointer.enabled',
|
|
105
|
+
'context.statePointer.maxChars',
|
|
101
106
|
'context.coldStartInjectSummary',
|
|
102
107
|
'context.coldStartInjectRecentTurns',
|
|
103
108
|
// v1.5.6 L2 multi-merge tunables (offline maintain phase).
|