hippo-memory 3.1.0 → 3.2.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.
Files changed (78) hide show
  1. package/dist/api/context/context-plan.d.ts +2 -0
  2. package/dist/api/context/context-plan.js +10 -6
  3. package/dist/api/context/context-pools.d.ts +3 -0
  4. package/dist/api/context/context-pools.js +12 -0
  5. package/dist/api/context/context-render.d.ts +6 -0
  6. package/dist/api/context/context-render.js +24 -9
  7. package/dist/api/context/context-select.js +12 -7
  8. package/dist/api/context/context-types.d.ts +6 -3
  9. package/dist/api/context/context.js +5 -4
  10. package/dist/api/context/user-rule.d.ts +12 -0
  11. package/dist/api/context/user-rule.js +24 -0
  12. package/dist/api/context/user-rules-get.d.ts +10 -0
  13. package/dist/api/context/user-rules-get.js +25 -0
  14. package/dist/api/index.d.ts +1 -0
  15. package/dist/api/index.js +1 -0
  16. package/dist/api/memories/remember.js +2 -2
  17. package/dist/api/prompt-hook.d.ts +11 -0
  18. package/dist/api/prompt-hook.js +26 -10
  19. package/dist/api/recall/rank-stages.js +2 -2
  20. package/dist/api/session-end.d.ts +1 -1
  21. package/dist/api/session-end.js +5 -3
  22. package/dist/base/memory/entry.d.ts +5 -0
  23. package/dist/base/memory/entry.js +10 -3
  24. package/dist/base/memory/markdown-delimiters.d.ts +15 -0
  25. package/dist/base/memory/markdown-delimiters.js +94 -0
  26. package/dist/base/memory/memory-audit.js +4 -4
  27. package/dist/base/memory/memory-quality.d.ts +12 -0
  28. package/dist/base/memory/memory-quality.js +92 -3
  29. package/dist/base/ports/store/ledger.d.ts +2 -2
  30. package/dist/base/ports/store/types.d.ts +2 -1
  31. package/dist/base/rank/prompt-recall.d.ts +16 -3
  32. package/dist/base/rank/prompt-recall.js +47 -7
  33. package/dist/base/util/tokenize.d.ts +29 -0
  34. package/dist/base/util/tokenize.js +68 -5
  35. package/dist/base/util/version.d.ts +1 -1
  36. package/dist/base/util/version.js +1 -1
  37. package/dist/db/meta.d.ts +1 -0
  38. package/dist/db/meta.js +1 -0
  39. package/dist/domain/consolidate/conflicts.js +2 -2
  40. package/dist/domain/learn/forward-claim-detector.js +4 -2
  41. package/dist/domain/learn/invalidation.js +5 -2
  42. package/dist/domain/search/bm25.js +3 -3
  43. package/dist/store/rules/windows.d.ts +3 -0
  44. package/dist/store/rules/windows.js +3 -0
  45. package/dist/store/sqlite/audit/audit-maintenance.js +16 -13
  46. package/dist/store/sqlite/ledger/token-ledger.js +7 -1
  47. package/dist/store/sqlite/recall/candidates.js +1 -0
  48. package/dist/store/sqlite/recall/search-rows.d.ts +2 -2
  49. package/dist/store/sqlite/recall/search-rows.js +62 -26
  50. package/dist/store/sqlite/sharing/copy-rows.d.ts +1 -0
  51. package/dist/store/sqlite/sharing/copy-rows.js +2 -1
  52. package/dist/surface/capture/command.d.ts +11 -1
  53. package/dist/surface/capture/command.js +48 -17
  54. package/dist/surface/capture/extract.d.ts +13 -2
  55. package/dist/surface/capture/extract.js +251 -33
  56. package/dist/surface/capture/session-texts.js +8 -6
  57. package/dist/surface/capture/transcript.js +6 -1
  58. package/dist/surface/cli/context.d.ts +7 -0
  59. package/dist/surface/cli/context.js +17 -10
  60. package/dist/surface/cli/hook.js +3 -0
  61. package/dist/surface/cli/hooks/session-end-worker.js +5 -3
  62. package/dist/surface/cli/init.js +3 -0
  63. package/dist/surface/cli/memory-import.js +2 -1
  64. package/dist/surface/cli/session-rules.d.ts +3 -0
  65. package/dist/surface/cli/session-rules.js +45 -0
  66. package/dist/surface/cli/setup.js +2 -0
  67. package/dist/surface/cli/verbs/sessions.d.ts +1 -0
  68. package/dist/surface/cli/verbs/sessions.js +10 -2
  69. package/dist/surface/cli/verbs.d.ts +1 -0
  70. package/dist/surface/hooks-install/rules/claude-hooks.js +11 -3
  71. package/dist/surface/hooks-install/rules/install-plan.js +1 -0
  72. package/dist/surface/hooks-install/rules/json-hooks.d.ts +2 -0
  73. package/dist/surface/hooks-install/rules/shared.d.ts +2 -0
  74. package/dist/surface/hooks-install/rules/shared.js +2 -0
  75. package/extensions/openclaw-plugin/openclaw.plugin.json +1 -1
  76. package/extensions/openclaw-plugin/package.json +1 -1
  77. package/openclaw.plugin.json +1 -1
  78. package/package.json +1 -1
@@ -1,6 +1,7 @@
1
1
  import { duplicateKey } from '../../base/util/same-text.js';
2
- import { assessAutomaticMemory } from '../../base/memory/memory-quality.js';
2
+ import { assessAutomaticMemory, asideCloseOffset, QUOTE_MARK, } from '../../base/memory/memory-quality.js';
3
3
  import { ERROR_TAG } from '../../base/memory/entry.js';
4
+ import { blankCodeSpans, strongMarkers } from '../../base/memory/markdown-delimiters.js';
4
5
  const DECISION_PATTERNS = [
5
6
  /\b(?:we(?:'ve| have)?|i(?:'ve| have)?|let's)\s+decid(?:ed|e)\b/i,
6
7
  /\b(?:let's|we(?:'ll| will| should)?)\s+(?:go with|do|use|try|build|implement|switch to)\b/i,
@@ -85,32 +86,139 @@ function continues(line, previous) {
85
86
  return false;
86
87
  return !SENTENCE_END.test(masked(`${previous} ${line}`).slice(0, previous.length));
87
88
  }
88
- function proseBlocks(text) {
89
+ /** What capture read before it removed bold markers. */
90
+ const keepAll = () => [];
91
+ /** Offsets of each `**` that pairs with the nearest opener before it. */
92
+ function pairedBold(line) {
93
+ const paired = [];
94
+ const openers = [];
95
+ // Blanked code spans keep every offset, so the markers found here are cut from the source.
96
+ for (const marker of strongMarkers(blankCodeSpans(line, '`'))) {
97
+ if (marker.inWord)
98
+ continue;
99
+ const opener = marker.close ? openers.pop() : undefined;
100
+ if (opener !== undefined)
101
+ paired.push(opener, marker.index);
102
+ else if (marker.open)
103
+ openers.push(marker.index);
104
+ }
105
+ return paired;
106
+ }
107
+ /** The segmenter splits after "**Label.**" and leaves the closing `**` on the next rule, so each
108
+ * real pair goes; a glob, `2**10` or `**kwargs` has no partner and stays as written. */
109
+ const boldPairs = (line) => pairedBold(line).sort((a, b) => a - b);
110
+ /** `line` without the two characters at each of the sorted `cuts`, built in one pass. */
111
+ function cutOut(line, cuts) {
112
+ let out = '';
113
+ let kept = 0;
114
+ for (const at of cuts) {
115
+ out += line.slice(kept, at);
116
+ kept = at + 2;
117
+ }
118
+ return out + line.slice(kept);
119
+ }
120
+ /** How many of the sorted `values` are at most `limit`. */
121
+ function countUpTo(values, limit) {
122
+ let low = 0;
123
+ let high = values.length;
124
+ while (low < high) {
125
+ const middle = (low + high) >> 1;
126
+ if (values[middle] <= limit)
127
+ low = middle + 1;
128
+ else
129
+ high = middle;
130
+ }
131
+ return low;
132
+ }
133
+ function blockOf(pieces, cuts) {
134
+ const raw = pieces.map((piece) => piece.text).join(' ');
135
+ const cutAt = cuts(raw);
136
+ // The sentences are those 3.1.0 split from the uncut text, so a removed `**` merges none: ICU
137
+ // breaks "**.Always" but not "n.Always". Masking keeps offsets.
138
+ const cutEnds = cutAt.map((at) => at + 2);
139
+ const sentenceStarts = [
140
+ ...new Intl.Segmenter('en', { granularity: 'sentence' }).segment(masked(raw)),
141
+ ].map(({ index }) => index - 2 * countUpTo(cutEnds, index));
142
+ // Where each cut sits in the cut text, and where each piece starts in the raw text.
143
+ const shifted = cutAt.map((at, k) => at - 2 * k);
144
+ const starts = [];
145
+ let sum = 0;
146
+ for (const piece of pieces) {
147
+ starts.push(sum);
148
+ sum += piece.text.length + 1;
149
+ }
150
+ const toProse = (rawIndex) => {
151
+ const k = Math.max(0, countUpTo(starts, rawIndex) - 1);
152
+ return pieces[k].at + Math.min(rawIndex - starts[k], pieces[k].text.length);
153
+ };
154
+ return {
155
+ text: cutOut(raw, cutAt),
156
+ sentenceStarts,
157
+ sourceOf: (from, to) => [
158
+ toProse(from + 2 * countUpTo(shifted, from)),
159
+ toProse(to + 2 * countUpTo(shifted, to - 1)),
160
+ ],
161
+ };
162
+ }
163
+ function proseBlocks(prose, cuts) {
89
164
  const blocks = [];
90
165
  let pending = [];
166
+ // Cut once the lines are joined, as a spec bullet is, so a bold pair that wraps a line goes whole.
91
167
  const flush = () => {
92
168
  if (pending.length > 0)
93
- blocks.push(pending.join(' '));
169
+ blocks.push(blockOf(pending, cuts));
94
170
  pending = [];
95
171
  };
96
- for (const line of text.split('\n')) {
172
+ let offset = 0;
173
+ for (const line of prose.split('\n')) {
97
174
  const trimmed = line.trim();
175
+ const end = offset + line.trimEnd().length;
176
+ offset += line.length + 1;
98
177
  if (!trimmed || /^#+\s/.test(trimmed)) {
99
178
  flush();
100
179
  continue;
101
180
  }
102
181
  const bullet = trimmed.match(BULLET);
103
- if (bullet || !continues(trimmed, pending.at(-1)))
182
+ if (bullet || !continues(trimmed, pending.at(-1)?.text))
104
183
  flush();
105
- pending.push(bullet?.[1] ?? trimmed);
184
+ const text = bullet?.[1] ?? trimmed;
185
+ pending.push({ text, at: end - text.length });
106
186
  }
107
187
  flush();
108
188
  return blocks;
109
189
  }
110
- function completeSentences(text) {
111
- // Masking keeps offsets, so each sentence is cut from the unchanged source.
112
- const segments = new Intl.Segmenter('en', { granularity: 'sentence' }).segment(masked(text));
113
- return [...segments].map(({ index, segment }) => text.slice(index, index + segment.length).trim());
190
+ /** Each sentence of a block, trimmed, with where it starts in the block text. */
191
+ function completeSentences({ text, sentenceStarts, }) {
192
+ return sentenceStarts.map((start, k) => {
193
+ const piece = text.slice(start, sentenceStarts[k + 1] ?? text.length);
194
+ return { sentence: piece.trim(), at: start + piece.length - piece.trimStart().length };
195
+ });
196
+ }
197
+ // A "(" after ":" or ";" is an emoticon, and one with a quote mark on both sides is quoted text.
198
+ function opensAside(text, at) {
199
+ const before = text[at - 1] ?? '';
200
+ const quoted = QUOTE_MARK.test(before) && QUOTE_MARK.test(text[at + 1] ?? '');
201
+ return text[at] === '(' && !/[:;]/.test(before) && !quoted;
202
+ }
203
+ // A sentence starts inside a cut aside: an unclosed "(" before it, an aside-closing ")" in it, and
204
+ // a word after that ")".
205
+ function asideCutter(block) {
206
+ const blanked = blankCodeSpans(block);
207
+ let depth = 0;
208
+ let seen = 0;
209
+ return (sentence, at) => {
210
+ while (seen < at) {
211
+ if (opensAside(blanked, seen))
212
+ depth++;
213
+ else if (blanked[seen] === ')')
214
+ depth = Math.max(0, depth - 1);
215
+ seen++;
216
+ }
217
+ if (depth === 0)
218
+ return false;
219
+ const close = asideCloseOffset(sentence);
220
+ return close >= 0 && /[\p{L}\p{N}]/u.test(sentence.slice(close + 1));
221
+ };
114
222
  }
115
223
  function insideBrackets(lead) {
116
224
  return (lead.match(/[([{]/g) ?? []).length > (lead.match(/[)\]}]/g) ?? []).length;
@@ -128,12 +236,15 @@ function extractFromPatterns(sentence, patterns, category) {
128
236
  .trim();
129
237
  return content.length <= MAX_CHARS ? { content, category, tags: [category, 'captured'] } : null;
130
238
  }
131
- function extractSpecSections(text) {
239
+ function extractSpecSections(prose, cuts) {
132
240
  const bullets = [];
133
241
  let inSpecSection = false;
134
242
  let open = false;
135
- for (const line of text.split('\n')) {
243
+ let offset = 0;
244
+ for (const line of prose.split('\n')) {
136
245
  const trimmed = line.trim();
246
+ const end = offset + line.trimEnd().length;
247
+ offset += line.length + 1;
137
248
  if (SPEC_HEADING_PATTERNS.some((pattern) => pattern.test(trimmed))) {
138
249
  inSpecSection = true;
139
250
  open = false;
@@ -142,46 +253,107 @@ function extractSpecSections(text) {
142
253
  if (/^#+\s/.test(trimmed) || /^[A-Z][a-z]+:$/.test(trimmed))
143
254
  inSpecSection = false;
144
255
  const bullet = trimmed.match(BULLET);
256
+ const last = bullets.at(-1);
145
257
  const wrapped = inSpecSection &&
146
258
  open &&
147
259
  !bullet &&
148
260
  trimmed !== '' &&
149
- (/^\s/.test(line) || continues(trimmed, bullets.at(-1)));
150
- if (inSpecSection && bullet)
151
- bullets.push(bullet[1].trim());
152
- else if (wrapped)
153
- bullets[bullets.length - 1] += ` ${trimmed}`;
261
+ (/^\s/.test(line) || continues(trimmed, last?.text));
262
+ if (inSpecSection && bullet) {
263
+ const text = bullet[1].trim();
264
+ bullets.push({ text, at: end - text.length, to: end });
265
+ }
266
+ else if (wrapped && last) {
267
+ last.text += ` ${trimmed}`;
268
+ last.to = end;
269
+ }
154
270
  open = inSpecSection && (bullet !== null || wrapped);
155
271
  }
272
+ // Cut once the lines are joined, so a pair that wraps a line goes whole.
156
273
  return bullets
157
- .filter((content) => content.length <= MAX_CHARS && !content.endsWith('?'))
158
- .map((content) => ({ content, category: 'spec', tags: ['spec', 'captured'] }));
274
+ .map(({ text, at, to }) => ({ content: cutOut(text, cuts(text)), from: at, to }))
275
+ .filter(({ content }) => content.length <= MAX_CHARS && !content.endsWith('?'))
276
+ .map(({ content, from, to }) => ({
277
+ item: { content, category: 'spec', tags: ['spec', 'captured'] },
278
+ from,
279
+ to,
280
+ }));
159
281
  }
160
- function extractOne(text) {
282
+ // A reply to the agent's work states how the person wants it done, rule word or not.
283
+ const ANY_STATEMENT = [/\S/];
284
+ // A kept reply names code or limits a choice; a thanks or a pasted test count does neither.
285
+ const CODE_CUE = /`|\w[\\/]\w|\w\.[A-Za-z]|\w_\w|\w\(|[<>{}[\]=]|(?:^|\s)--?[A-Za-z]/;
286
+ const LIMIT_CUE = /n't\b|\b(?:not|no longer|instead|rather|only|stop|unless|before|after|until|without|except)\b/i;
287
+ // Praise names code too ("Thanks, `src/cache.ts` looks good"); a thanks that goes on to direct is kept.
288
+ // SHORTCUT: word shapes, not meaning; the opt-in model capture is the upgrade path.
289
+ const ACK_LEAD = /^(?:thanks|thank you|ok(?:ay)?|great|nice|perfect|cool|lgtm)\b[\s,.!:-]*/i;
290
+ const NOT_A_DIRECTIVE = /^(?:for|on|to|so|the|a|an|this|that|it|its|you|your|i|we|all|really)\b/i;
291
+ const APPROVAL = /^(?:\S+\s+){0,3}(?:looks?|works?|worked)\s+(?:good|great|fine|right|now)\b/i;
292
+ // Pasted output opens on a file:line, a stack frame or an error word, or holds a file:line:col
293
+ // (a compiler arrow line) or an assertion message.
294
+ const LOG_CUE = new RegExp([
295
+ String.raw `^#\d+\s|^\S*\w\.[A-Za-z]+:\d+|\w\.[A-Za-z]+:\d+:\d+`,
296
+ String.raw `^(?:\w*Error|error(?:\[\w+\])?:|warning:|FAIL|Traceback)`,
297
+ String.raw `\bexpected\b.+\bto (?:be|equal|have)\b`,
298
+ ].join('|'));
299
+ /** A thanks with no directive after it, or a short "X looks good". */
300
+ function isPraise(content) {
301
+ const lead = ACK_LEAD.exec(content);
302
+ const rest = lead ? content.slice(lead[0].length) : content;
303
+ if (APPROVAL.test(rest))
304
+ return true;
305
+ if (!lead)
306
+ return false;
307
+ return rest.split(/\s+/).filter(Boolean).length <= 3 || NOT_A_DIRECTIVE.test(rest);
308
+ }
309
+ /** A statement in a reply as the person's rule: 2 words when it names code, 4 with a limiting word. */
310
+ function followUpRule(sentence) {
311
+ const item = extractFromPatterns(sentence, ANY_STATEMENT, 'rule');
312
+ if (!item)
313
+ return null;
314
+ const { content } = item;
315
+ const limits = LIMIT_CUE.test(content);
316
+ if (LOG_CUE.test(content) || (isPraise(content) && !limits))
317
+ return null;
318
+ const words = content.split(/\s+/).length;
319
+ const kept = (words >= 2 && CODE_CUE.test(content)) || (words >= 4 && limits);
320
+ return kept ? { ...item, tags: [...item.tags, 'follow-up'] } : null;
321
+ }
322
+ /** What a sentence holds; in a reply a preference that names code or limits a choice is a rule. */
323
+ function sentenceItem(sentence, followUp) {
324
+ const hit = CATEGORIES.map(([category, patterns]) => extractFromPatterns(sentence, patterns, category)).find((found) => found !== null) ?? null;
325
+ if (!followUp || (hit !== null && hit.category !== 'preference'))
326
+ return hit;
327
+ return followUpRule(sentence) ?? hit;
328
+ }
329
+ // The items of the spec bullets first, then those of each prose block.
330
+ function extractSourced(text, cuts, followUp = false) {
161
331
  const prose = proseOnly(text);
162
- const items = extractSpecSections(prose);
163
- for (const block of proseBlocks(prose)) {
164
- for (const sentence of completeSentences(block)) {
332
+ const found = extractSpecSections(prose, cuts);
333
+ for (const block of proseBlocks(prose, cuts)) {
334
+ const cutByAside = asideCutter(block.text);
335
+ for (const { sentence, at } of completeSentences(block)) {
165
336
  // A question asks rather than states; far over the bound, skip before any pattern runs.
166
337
  if (sentence.endsWith('?') || sentence.length > 2 * MAX_CHARS)
167
338
  continue;
168
- for (const [category, patterns] of CATEGORIES) {
169
- const item = extractFromPatterns(sentence, patterns, category);
170
- if (item) {
171
- items.push(item);
172
- break;
173
- }
339
+ if (cutByAside(sentence, at))
340
+ continue;
341
+ const item = sentenceItem(sentence, followUp);
342
+ if (item) {
343
+ const [from, to] = block.sourceOf(at, at + sentence.length);
344
+ found.push({ item, from, to });
174
345
  }
175
346
  }
176
347
  }
177
- return items;
348
+ return found;
178
349
  }
179
350
  /** Captures complete supported statements, each text parsed alone so a fence or heading in one never reaches the next.
180
- * The first `userTexts` texts are a person's own words; on a repeat their copy wins, so it keeps `fromUser`. */
181
- export function extractFromTexts(texts, userTexts = 0) {
351
+ * The first `userTexts` texts are a person's own words; on a repeat their copy wins, so it keeps `fromUser`.
352
+ * From `firstFollowUp` on, those words answer the agent's work, so a statement in them can be the person's rule. */
353
+ export function extractFromTexts(texts, userTexts = 0, firstFollowUp = userTexts) {
182
354
  const seen = new Set();
183
355
  return texts
184
- .flatMap((text, i) => extractOne(text).map((item) => (i < userTexts ? { ...item, fromUser: true } : item)))
356
+ .flatMap((text, i) => extractSourced(text, boldPairs, i >= firstFollowUp && i < userTexts).map(({ item }) => i < userTexts ? { ...item, fromUser: true } : item))
185
357
  .filter((item) => {
186
358
  const key = duplicateKey(item.content);
187
359
  if (seen.has(key) || !assessAutomaticMemory(item.content).accepted)
@@ -190,4 +362,50 @@ export function extractFromTexts(texts, userTexts = 0) {
190
362
  return true;
191
363
  });
192
364
  }
365
+ function trackOf(items) {
366
+ return { items, ends: items.map(({ to }) => to) };
367
+ }
368
+ /** The items of `track` whose range overlaps [from, to). */
369
+ function overlapping(track, from, to) {
370
+ const found = [];
371
+ for (let k = countUpTo(track.ends, from); k < track.items.length && track.items[k].from < to; k++) {
372
+ found.push(track.items[k]);
373
+ }
374
+ return found;
375
+ }
376
+ /** Whether each range covers the other, but for markers, spaces and sentence marks at the edges. */
377
+ function sameSpan(prose, a, b) {
378
+ const gap = (x, y) => prose.slice(Math.min(x, y), Math.max(x, y));
379
+ return /^[\s*.!?]*$/.test(gap(a.from, b.from)) && /^[\s*.!?]*$/.test(gap(a.to, b.to));
380
+ }
381
+ /** The twins of `texts`: each old item pairs, one to one, with the new item of the same span, the
382
+ * same category first and then the larger overlap; a pair whose texts differ. A new item that
383
+ * holds more or less than the old one is no twin, or a held row would block a rule it lacks. */
384
+ export function boldTwins(texts, scrub) {
385
+ const twins = [];
386
+ for (const text of texts.filter((t) => t.includes('**'))) {
387
+ const prose = proseOnly(text);
388
+ const fresh = extractSourced(text, boldPairs);
389
+ // Spec bullets and prose sentences overlap each other, so each kind keeps its own order.
390
+ const tracks = [trackOf(fresh.filter(({ item }) => item.category === 'spec'))];
391
+ tracks.push(trackOf(fresh.filter(({ item }) => item.category !== 'spec')));
392
+ const taken = new Set();
393
+ for (const sourced of extractSourced(text, keepAll)) {
394
+ const { item: old, from, to } = sourced;
395
+ const size = (found) => Math.min(found.to, to) - Math.max(found.from, from);
396
+ const same = (found) => Number(found.item.category === old.category);
397
+ const [match] = tracks
398
+ .flatMap((track) => overlapping(track, from, to))
399
+ .filter((found) => !taken.has(found) && sameSpan(prose, found, sourced))
400
+ .sort((a, b) => same(b) - same(a) || size(b) - size(a));
401
+ if (match === undefined)
402
+ continue;
403
+ taken.add(match);
404
+ if (old.content !== match.item.content) {
405
+ twins.push({ old: old.content, clean: scrub(match.item.content) });
406
+ }
407
+ }
408
+ }
409
+ return twins;
410
+ }
193
411
  //# sourceMappingURL=extract.js.map
@@ -3,11 +3,10 @@
3
3
  import { BadRequestError } from '../../base/errors/api-errors.js';
4
4
  import { assertCallerIds } from '../../api/prompt-hook.js';
5
5
  import { assertCallerProject, projectNames } from '../../base/scope/project-identity.js';
6
- import { storedTextKeys } from '../../base/util/same-text.js';
7
6
  import { scrubForSharing } from './share-scrub.js';
8
- import { captureExtractedItems } from './command.js';
7
+ import { captureExtractedItems, heldKeysOf, scrubbedItems } from './command.js';
9
8
  import { detachedEmbedder } from '../../api/embeddings.js';
10
- import { extractFromTexts } from './extract.js';
9
+ import { boldTwins, extractFromTexts } from './extract.js';
11
10
  import { withEntryInfra } from '../entry-infra.js';
12
11
  import { withDetached } from '../../api/detached.js';
13
12
  const MAX_TEXTS = 30;
@@ -40,7 +39,9 @@ function assertSessionCapture(req) {
40
39
  export async function captureSessionTexts(given, req) {
41
40
  const ctx = withEntryInfra(given);
42
41
  assertSessionCapture(req);
43
- const extracted = extractFromTexts(req.texts.map(scrubForSharing));
42
+ const scrubbed = req.texts.map(scrubForSharing);
43
+ // Scrubbed again after extraction: bold removal can rebuild an address or key.
44
+ const extracted = scrubbedItems(extractFromTexts(scrubbed), scrubForSharing);
44
45
  // The newest items win, since the end of a session states where it landed.
45
46
  const items = extracted.slice(-MAX_ITEMS);
46
47
  if (items.length === 0)
@@ -48,7 +49,8 @@ export async function captureSessionTexts(given, req) {
48
49
  // The project filter is classifyOriginProject's in SQL: the caller's rows and user-global rows
49
50
  // block a copy, other projects' rows do not.
50
51
  const dedup = { tenantId: ctx.tenantId, project: projectNames(req.project) };
51
- const held = await ctx.store.captures.textsHolding(dedup.tenantId, items.map((i) => i.content), { project: dedup.project });
52
+ const twins = boldTwins(scrubbed, scrubForSharing);
53
+ const held = await ctx.store.captures.textsHolding(dedup.tenantId, [...items.map((i) => i.content), ...twins.map((twin) => twin.old)], { project: dedup.project });
52
54
  const { store, config } = ctx;
53
55
  const folder = { store, config, embedderFor: ctx.infra.embedderFor };
54
56
  const tally = await withDetached(ctx, (detached) => captureExtractedItems({ store, config, embed: detachedEmbedder(folder, ctx.infra, detached) }, {
@@ -59,7 +61,7 @@ export async function captureSessionTexts(given, req) {
59
61
  lean: true,
60
62
  dedup,
61
63
  now: ctx.infra.clock(),
62
- }, items, storedTextKeys(held)));
64
+ }, items, heldKeysOf(held, twins)));
63
65
  return { ...tally, skipped: tally.skipped + extracted.length - items.length };
64
66
  }
65
67
  //# sourceMappingURL=session-texts.js.map
@@ -12,12 +12,17 @@ const RECENT_ASSISTANT_TURNS = 10;
12
12
  * Build a compact text summary from a Claude Code / OpenCode / Copilot JSONL transcript: plain
13
13
  * user messages and the final assistant text chunk, without thinking, tool_use and tool_result
14
14
  * noise. Exported for tests. */
15
- /** Leading markers of the command lines Claude Code writes with type 'user'. */
15
+ /** Leading markers of the lines Claude Code writes with type 'user' that no person typed: commands,
16
+ * `!` shell input and output, task notices. */
16
17
  const CLAUDE_CODE_COMMAND_PREFIXES = [
17
18
  '<local-command-',
18
19
  '<command-name>',
19
20
  '<command-message>',
20
21
  '<command-args>',
22
+ '<bash-input>',
23
+ '<bash-stdout>',
24
+ '<bash-stderr>',
25
+ '<task-notification>',
21
26
  ];
22
27
  function isNonHumanUserLine(entry, content) {
23
28
  if (entry.isMeta === true ||
@@ -1,3 +1,10 @@
1
1
  import { type CommandContext } from './flag-values.js';
2
+ export interface ContextSession {
3
+ readonly currentSessionId: string | undefined;
4
+ readonly ledgerSessionId: string | undefined;
5
+ readonly payloadSessionId: string | undefined;
6
+ readonly prompt: string | undefined;
7
+ }
8
+ export declare function resolveContextSession(stdinText: string | undefined): ContextSession;
2
9
  export declare function handleContext({ hippoRoot, tenantId, args, flags, }: CommandContext): Promise<void>;
3
10
  //# sourceMappingURL=context.d.ts.map
@@ -12,8 +12,8 @@ import { DEFAULT_CONTEXT_BUDGET } from '../../api/context/context.js';
12
12
  import { cliApiContext, cliHookRequest } from './api-context.js';
13
13
  import { renderAmbientSummary } from '../../base/memory/ambient.js';
14
14
  import { strengthOf } from '../../api/types.js';
15
- import { contextBlockLines, contextCost, crossProjectLines, settleTokens, } from '../../api/context/context-render.js';
16
- import { additionalContextOutput, hasContextData, sessionStartEnvelope, toRenderItems, } from '../../api/prompt-hook.js';
15
+ import { contextBlockLines, contextCost, crossProjectLines, settleTokens, userRuleLines, } from '../../api/context/context-render.js';
16
+ import { additionalContextOutput, hasContextData, sessionStartEnvelope, splitUserRules, toRenderItems, } from '../../api/prompt-hook.js';
17
17
  import { flushDeliveryRecorder, startDeliveryRecorder, takeDelivery } from '../../api/ledger-db.js';
18
18
  import { boolFlag, parseBudgetFlag, parseCountFlag, parseLimitFlag, stringFlagOrExit, } from './flag-values.js';
19
19
  import { requireInit, workingFolder } from './shared.js';
@@ -92,18 +92,21 @@ async function renderContext(options) {
92
92
  const framing = stringFlagOrExit(flags, 'framing') ?? 'observe';
93
93
  const now = ctx.infra.clock();
94
94
  const input = { query, budget, pinnedOnly, format, framing, session, rec, now };
95
- const result = await api.contextGet(ctx, buildContextOpts(flags, input, ctx));
95
+ const opts = buildContextOpts(flags, input, ctx);
96
+ const result = await api.contextGet(ctx, opts);
96
97
  if (!hasContextData(result)) {
97
98
  rec?.delivered({ state: 'empty' });
98
99
  return;
99
100
  }
100
101
  const envelope = format === 'copilot' ? sessionStartEnvelope(stdinText) : undefined;
101
- await renderContextView(contextView(ctx, input, result, envelope), format, query);
102
+ const view = contextView(ctx, input, result, envelope, opts.currentProject);
103
+ await renderContextView(view, format, query);
102
104
  }
103
- function contextView(ctx, input, result, envelope) {
105
+ function contextView(ctx, input, result, envelope, project) {
104
106
  return {
105
107
  config: ctx.config,
106
108
  tenantId: ctx.tenantId,
109
+ project,
107
110
  ledgerSessionId: input.session.ledgerSessionId,
108
111
  payloadSessionId: input.session.payloadSessionId,
109
112
  pinnedOnly: input.pinnedOnly,
@@ -133,7 +136,7 @@ async function renderContextView(view, format, query) {
133
136
  await renderContextMarkdown(view);
134
137
  }
135
138
  }
136
- function resolveContextSession(stdinText) {
139
+ export function resolveContextSession(stdinText) {
137
140
  // The session id bounds the active-task-snapshot read: the stdin hook payload wins, then
138
141
  // hostSessionId(); absent both, api.contextGet applies the pure freshness bound.
139
142
  const payload = readHookPayload(stdinText);
@@ -220,11 +223,15 @@ async function renderContextJson(view, query) {
220
223
  * ledger does. */
221
224
  async function renderContextMarkdown(view) {
222
225
  const { result, rec, framing } = view;
226
+ const { userRules, others } = splitUserRules(view);
223
227
  // Cross-project inclusions get their own section so they never masquerade as project memory.
224
- const renderItems = toRenderItems(result.entries.filter((r) => r.category !== 'cross-project'));
225
- const crossEntries = result.entries.filter((r) => r.category === 'cross-project');
228
+ const renderItems = toRenderItems(others.filter((r) => r.category !== 'cross-project'));
229
+ const crossEntries = others.filter((r) => r.category === 'cross-project');
226
230
  const text = settleTokens((t) => {
227
- const lines = continuityTexts(result.activeSnapshot, result.sessionHandoff, result.recentEvents, result);
231
+ const lines = [
232
+ ...userRuleLines(userRules),
233
+ ...continuityTexts(result.activeSnapshot, result.sessionHandoff, result.recentEvents, result),
234
+ ];
228
235
  if (renderItems.length > 0) {
229
236
  lines.push(...contextBlockLines(renderItems, t, framing, { now: view.now, strength: view.strength }));
230
237
  }
@@ -236,7 +243,7 @@ async function renderContextMarkdown(view) {
236
243
  if (text.length > 0)
237
244
  console.log(text);
238
245
  rec?.delivered(text.length > 0 ? { state: 'sent', emittedText: `${text}\n` } : { state: 'empty' });
239
- await bookContextInject(view, renderItems.length, estimateTokens(text));
246
+ await bookContextInject(view, userRules.length + renderItems.length, estimateTokens(text));
240
247
  }
241
248
  export async function handleContext({ hippoRoot, tenantId, args, flags, }) {
242
249
  // Bounded, not a TTY guard: the hot stdin path and a manual run share this one command.
@@ -104,6 +104,9 @@ function printClaudeHookInstall(result) {
104
104
  if (result.installedSessionStart) {
105
105
  console.log(`Installed hippo last-sleep SessionStart hook in ${result.target} settings`);
106
106
  }
107
+ if (result.installedSessionRules) {
108
+ console.log(`Installed hippo session-rules SessionStart hook in ${result.target} settings`);
109
+ }
107
110
  if (result.installedUserPromptSubmit) {
108
111
  console.log(`Installed hippo pinned-inject UserPromptSubmit hook in ${result.target} settings`);
109
112
  }
@@ -39,11 +39,12 @@ function runSessionEndWork({ hippoRoot, tenantId, flags, mode, held, }) {
39
39
  },
40
40
  currentProject: () => ctx.infra.machine.projectIdentity(workingFolder(ctx)),
41
41
  // The run writes to this folder's own store, else to the global one in its reach.
42
- capture: async (into, turns, originProject) => {
42
+ capture: async (into, turns, originProject, fromStart) => {
43
43
  const root = into === ctx ? hippoRoot : ctx.reach?.global?.root;
44
44
  if (root === undefined)
45
45
  return false;
46
- return captureEndedSession(root, { tenantId, flags, transcriptPath, turns, originProject });
46
+ const ended = { tenantId, flags, transcriptPath, turns, originProject, fromStart };
47
+ return captureEndedSession(root, ended);
47
48
  },
48
49
  digest: (scan, key, log) => recordSessionDigest(hippoRoot, ctx.store, scan, { key, tenantId, log, config: ctx.config }),
49
50
  workingState: (log) => (transcriptPath ? transcriptWorkingState(transcriptPath, log) : null),
@@ -62,7 +63,7 @@ async function sleepFolderStore(hippoRoot, tenantId, flags, closeLogFile) {
62
63
  }
63
64
  }
64
65
  /** True when capture ran to the end, so a turn close may move its cursor past these turns. */
65
- async function captureEndedSession(store, { tenantId, flags, transcriptPath, turns, originProject }) {
66
+ async function captureEndedSession(store, { tenantId, flags, transcriptPath, turns, originProject, fromStart }) {
66
67
  const logFile = stringFlag(flags, 'log-file') ?? null;
67
68
  try {
68
69
  await runCapture(captureTarget(store), {
@@ -74,6 +75,7 @@ async function captureEndedSession(store, { tenantId, flags, transcriptPath, tur
74
75
  tenantId,
75
76
  originProject,
76
77
  sessionTurns: turns,
78
+ fromStart,
77
79
  });
78
80
  return true;
79
81
  }
@@ -331,6 +331,9 @@ function installClaudeCodeSettingsHooks(hook) {
331
331
  if (result.installedSessionStart) {
332
332
  console.log(` Auto-installed hippo last-sleep SessionStart hook in ${hook} settings`);
333
333
  }
334
+ if (result.installedSessionRules) {
335
+ console.log(` Auto-installed hippo session-rules SessionStart hook in ${hook} settings`);
336
+ }
334
337
  if (result.installedUserPromptSubmit) {
335
338
  console.log(` Auto-installed hippo pinned-inject UserPromptSubmit hook in ${hook} settings`);
336
339
  }
@@ -6,6 +6,7 @@ import { embedCoverage, memoryEmbedMissing } from '../../api/embeddings.js';
6
6
  import { importAtSessionEnd, importForStore } from '../../api/agent-memories.js';
7
7
  import { currentMachine } from '../../infra/machine.js';
8
8
  import { detailLines } from '../../base/memory/agent-memory-report.js';
9
+ import { withoutUserTag } from '../../base/memory/entry.js';
9
10
  import { importChatGPTInto, importClaudeInto, importCursorInto, importGenericFileInto, } from '../importers/sources.js';
10
11
  import { importMarkdownInto } from '../importers/markdown.js';
11
12
  import { importVaultInto } from '../importers/vault.js';
@@ -50,7 +51,7 @@ function warnRedacted(count) {
50
51
  export async function handleImport({ hippoRoot, tenantId, args, flags, }) {
51
52
  const useGlobal = boolFlag(flags, 'global');
52
53
  const dryRun = boolFlag(flags, 'dry-run');
53
- const extraTags = stringListFlag(flags, 'tag') ?? [];
54
+ const extraTags = withoutUserTag(stringListFlag(flags, 'tag') ?? []);
54
55
  const targetRoot = useGlobal ? hippoHomeDir() : hippoRoot;
55
56
  if (boolFlag(flags, 'agents')) {
56
57
  return await importAgentMemories(hippoRoot, tenantId, { useGlobal, dryRun });
@@ -0,0 +1,3 @@
1
+ import type { CommandContext } from './flag-values.js';
2
+ export declare function handleSessionRules({ hippoRoot, tenantId }: CommandContext): Promise<void>;
3
+ //# sourceMappingURL=session-rules.d.ts.map
@@ -0,0 +1,45 @@
1
+ // `hippo session-rules`: the SessionStart hook that sends the rules the person taught, and nothing
2
+ // else.
3
+ import { estimateTokens } from '../../base/util/token-text.js';
4
+ import { userRulesGet } from '../../api/index.js';
5
+ import { userRuleLines } from '../../api/context/context-render.js';
6
+ import { bookTokenUse } from '../../api/ledger-db.js';
7
+ import { cliApiContext, cliHookRequest, cliLedgerReach } from './api-context.js';
8
+ import { resolveContextSession } from './context.js';
9
+ import { hookStoreRoot, inPilotHoldout } from './hook-runtime.js';
10
+ import { readHookStdin } from './stdin.js';
11
+ import { workingFolder } from './shared.js';
12
+ async function printSessionRules(hippoRoot, tenantId, stdinText) {
13
+ const { currentSessionId, ledgerSessionId, payloadSessionId } = resolveContextSession(stdinText);
14
+ // Booked at the first hook call, as the prompt hook books it, so the holdout sees no rules.
15
+ const held = await inPilotHoldout(hippoRoot, tenantId, currentSessionId, {
16
+ write: payloadSessionId !== undefined,
17
+ });
18
+ if (held)
19
+ return;
20
+ const ctx = cliApiContext(hippoRoot, tenantId);
21
+ const rules = await userRulesGet(ctx, {
22
+ currentProject: ctx.infra.machine.projectIdentity(workingFolder(ctx)),
23
+ currentSessionId,
24
+ });
25
+ const text = userRuleLines(rules).join('\n').trim();
26
+ if (text === '')
27
+ return;
28
+ process.stdout.write(JSON.stringify({
29
+ hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: text },
30
+ }));
31
+ await bookTokenUse({
32
+ tenantId,
33
+ sessionId: ledgerSessionId,
34
+ surface: 'session_start',
35
+ event: 'inject',
36
+ items: rules.length,
37
+ tokens: estimateTokens(text),
38
+ }, cliLedgerReach(hippoRoot));
39
+ }
40
+ export async function handleSessionRules({ hippoRoot, tenantId }) {
41
+ const { text: stdinText } = await readHookStdin();
42
+ // Fires in every folder, so it never requires init: a folder with no store reads the global one.
43
+ await cliHookRequest(() => printSessionRules(hookStoreRoot(hippoRoot), tenantId, stdinText));
44
+ }
45
+ //# sourceMappingURL=session-rules.js.map
@@ -104,6 +104,8 @@ function setupJsonTool(tool, dryRun) {
104
104
  bits.push('SessionEnd (session-end)');
105
105
  if (result.installedSessionStart)
106
106
  bits.push('SessionStart');
107
+ if (result.installedSessionRules)
108
+ bits.push('SessionStart (session-rules)');
107
109
  if (result.installedUserPromptSubmit)
108
110
  bits.push('UserPromptSubmit (pinned-inject)');
109
111
  if (result.installedPreCompact)
@@ -19,6 +19,7 @@ export declare const SESSION_VERBS: {
19
19
  capture: VerbSpec;
20
20
  setup: VerbSpec;
21
21
  'last-sleep': VerbSpec;
22
+ 'session-rules': VerbSpec;
22
23
  'session-end': VerbSpec;
23
24
  '__session-end-worker': VerbSpec;
24
25
  'pre-compact': VerbSpec;