hippo-memory 3.1.1 → 3.2.1
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/dist/api/context/context-plan.d.ts +2 -0
- package/dist/api/context/context-plan.js +8 -2
- package/dist/api/context/context-pools.d.ts +3 -0
- package/dist/api/context/context-pools.js +12 -0
- package/dist/api/context/context-render.d.ts +6 -0
- package/dist/api/context/context-render.js +24 -9
- package/dist/api/context/context-types.d.ts +6 -3
- package/dist/api/context/context.js +5 -4
- package/dist/api/context/user-rule.d.ts +12 -0
- package/dist/api/context/user-rule.js +24 -0
- package/dist/api/context/user-rules-get.d.ts +10 -0
- package/dist/api/context/user-rules-get.js +25 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/api/memories/remember.js +2 -2
- package/dist/api/prompt-hook.d.ts +11 -0
- package/dist/api/prompt-hook.js +26 -10
- package/dist/api/session-end.d.ts +1 -1
- package/dist/api/session-end.js +5 -3
- package/dist/base/memory/entry.d.ts +5 -0
- package/dist/base/memory/entry.js +8 -1
- package/dist/base/ports/store/capture.d.ts +3 -0
- package/dist/base/ports/store/ledger.d.ts +2 -2
- package/dist/base/ports/store/types.d.ts +3 -1
- package/dist/base/util/same-text.d.ts +7 -0
- package/dist/base/util/same-text.js +12 -0
- package/dist/base/util/version.d.ts +1 -1
- package/dist/base/util/version.js +1 -1
- package/dist/store/sqlite/capture/captures.js +33 -8
- package/dist/store/sqlite/entries/entry-writes.d.ts +5 -0
- package/dist/store/sqlite/entries/entry-writes.js +13 -0
- package/dist/store/sqlite/ledger/token-ledger.js +7 -1
- package/dist/store/sqlite/recall/candidates.js +3 -0
- package/dist/store/sqlite/sharing/copy-rows.d.ts +2 -0
- package/dist/store/sqlite/sharing/copy-rows.js +2 -1
- package/dist/surface/capture/command.d.ts +9 -2
- package/dist/surface/capture/command.js +67 -48
- package/dist/surface/capture/extract.d.ts +9 -2
- package/dist/surface/capture/extract.js +80 -20
- package/dist/surface/capture/transcript.js +6 -1
- package/dist/surface/cli/context.d.ts +7 -0
- package/dist/surface/cli/context.js +17 -10
- package/dist/surface/cli/hook.js +3 -0
- package/dist/surface/cli/hooks/session-end-worker.js +5 -3
- package/dist/surface/cli/init.js +3 -0
- package/dist/surface/cli/memory-import.js +2 -1
- package/dist/surface/cli/session-rules.d.ts +3 -0
- package/dist/surface/cli/session-rules.js +45 -0
- package/dist/surface/cli/setup.js +2 -0
- package/dist/surface/cli/verbs/sessions.d.ts +1 -0
- package/dist/surface/cli/verbs/sessions.js +10 -2
- package/dist/surface/cli/verbs.d.ts +1 -0
- package/dist/surface/hooks-install/rules/claude-hooks.js +11 -3
- package/dist/surface/hooks-install/rules/install-plan.js +1 -0
- package/dist/surface/hooks-install/rules/json-hooks.d.ts +2 -0
- package/dist/surface/hooks-install/rules/shared.d.ts +2 -0
- package/dist/surface/hooks-install/rules/shared.js +2 -0
- package/dist-ui/assets/{index-DWo8WuPW.js → index-DvZmCHQ5.js} +2 -2
- package/dist-ui/assets/{react-BZPdts19.js → react-BpaniaUk.js} +2 -2
- package/dist-ui/index.html +2 -2
- package/extensions/openclaw-plugin/openclaw.plugin.json +1 -1
- package/extensions/openclaw-plugin/package.json +1 -1
- package/openclaw.plugin.json +1 -1
- package/package.json +2 -2
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// hippo.db's Captures: the whole batch on one handle, opened with openStore since a write sets up
|
|
2
2
|
// the mirror folders.
|
|
3
3
|
import { withWriteScope } from '../../../db/index.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { USER_RULE_TAG } from '../../../base/memory/entry.js';
|
|
5
|
+
import { duplicateKey, heldTextKeys, ruleReplaces, storedTextKeys, } from '../../../base/util/same-text.js';
|
|
6
|
+
import { CLI_ACTOR } from '../../../base/scope/actor.js';
|
|
7
|
+
import { supersedeInTx, writeEntryMirrors } from '../entries/entry-writes.js';
|
|
6
8
|
import { stampOriginProject } from '../entries/entry-row.js';
|
|
7
9
|
import { gatedWrite } from './gated-write.js';
|
|
8
10
|
import { updateStatsOn } from '../entries/index-and-stats.js';
|
|
@@ -10,29 +12,52 @@ import { listTextsHolding } from '../recall/candidates.js';
|
|
|
10
12
|
import { EVERY_SCOPE } from '../runtime/rule-sql.js';
|
|
11
13
|
import { openStore } from '../runtime/handles.js';
|
|
12
14
|
import { bound, skipEmpty } from '../runtime/bound.js';
|
|
13
|
-
/** The
|
|
14
|
-
function
|
|
15
|
+
/** The rows under `dedup` that may hold an entry's text. */
|
|
16
|
+
function heldRows(db, entries, dedup) {
|
|
15
17
|
const texts = entries.map((entry) => entry.content);
|
|
16
18
|
const admit = dedup.everyScope ? EVERY_SCOPE : undefined;
|
|
17
|
-
return
|
|
19
|
+
return listTextsHolding(db, { tenantId: dedup.tenantId, texts, project: dedup.project, admit });
|
|
20
|
+
}
|
|
21
|
+
/** The caller's rule project names when the stamped rule carries one of them, else none. */
|
|
22
|
+
function ruleProjectNames(rule, dedup) {
|
|
23
|
+
const origin = rule.originProject;
|
|
24
|
+
return origin && dedup.ruleProject?.includes(origin) ? dedup.ruleProject : [];
|
|
25
|
+
}
|
|
26
|
+
/** The ids of the held copies of the stamped rule's text that the rule replaces. */
|
|
27
|
+
function copiesTheRuleReplaces(rows, rule, project) {
|
|
28
|
+
const key = duplicateKey(rule.content);
|
|
29
|
+
return rows.flatMap(({ id, ...row }) => id !== undefined && ruleReplaces(row, project) && heldTextKeys(row).includes(key) ? [id] : []);
|
|
18
30
|
}
|
|
19
31
|
/** One outcome per entry, in order, in one transaction that reads the caller's dedup again
|
|
20
32
|
* first, so a text another writer stored since that read is skipped; mirrors and stats follow
|
|
21
33
|
* the commit. */
|
|
22
34
|
function writeCapturedEntries(db, files, entries, opts) {
|
|
23
35
|
const written = [];
|
|
36
|
+
const actor = opts.actor ?? CLI_ACTOR;
|
|
24
37
|
const outcomes = withWriteScope(db, 'capture_write', () => {
|
|
25
|
-
const
|
|
38
|
+
const rows = heldRows(db, entries, opts.dedup);
|
|
39
|
+
const held = storedTextKeys(rows);
|
|
26
40
|
return entries.map((entry) => {
|
|
27
|
-
|
|
41
|
+
const stamped = stampOriginProject(files, entry);
|
|
42
|
+
// A stated rule replaces an unpinned capture copy of its own project, which would hide it
|
|
43
|
+
// from the hooks.
|
|
44
|
+
const project = entry.tags.includes(USER_RULE_TAG)
|
|
45
|
+
? ruleProjectNames(stamped, opts.dedup)
|
|
46
|
+
: null;
|
|
47
|
+
const stops = project ? storedTextKeys(rows.filter((r) => !ruleReplaces(r, project))) : held;
|
|
48
|
+
if (stops.has(duplicateKey(entry.content)))
|
|
28
49
|
return 'skipped';
|
|
29
50
|
// One rejected entry must not abort the rest of this capture's entries.
|
|
30
|
-
const stamped = stampOriginProject(files, entry);
|
|
31
51
|
const outcome = gatedWrite(db, files, stamped, { actor: opts.actor });
|
|
32
52
|
if (outcome === 'skipped:rejected')
|
|
33
53
|
return 'rejected';
|
|
34
54
|
if (outcome !== 'written')
|
|
35
55
|
return 'skipped';
|
|
56
|
+
if (project) {
|
|
57
|
+
for (const id of copiesTheRuleReplaces(rows, stamped, project)) {
|
|
58
|
+
supersedeInTx(db, { tenantId: stamped.tenantId, id, newId: stamped.id, actor });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
36
61
|
written.push(stamped);
|
|
37
62
|
return 'captured';
|
|
38
63
|
});
|
|
@@ -106,6 +106,11 @@ interface SupersedeEntryOptions {
|
|
|
106
106
|
}
|
|
107
107
|
/** Marks `id` superseded by `newId`; false when another writer superseded it first. */
|
|
108
108
|
export declare function supersedeEntry(db: DatabaseSyncLike, { tenantId, id, newId }: SupersedeEntryOptions): boolean;
|
|
109
|
+
/** Marks `id` superseded by `newId` in the caller's transaction, with the supersede audit row and
|
|
110
|
+
* its summary parent's dirty mark; false when the row is gone or another writer got there first. */
|
|
111
|
+
export declare function supersedeInTx(db: DatabaseSyncLike, { tenantId, id, newId, actor }: SupersedeEntryOptions & {
|
|
112
|
+
actor: string;
|
|
113
|
+
}): boolean;
|
|
109
114
|
/** Each call on its own handle; mirrors and the forgotten counter follow the commit, so a
|
|
110
115
|
* rolled-back write leaves neither. */
|
|
111
116
|
export declare function sqliteEntryWrites({ on, files }: Handles): EntryWrites;
|
|
@@ -294,6 +294,19 @@ export function supersedeEntry(db, { tenantId, id, newId }) {
|
|
|
294
294
|
const result = cachedStatement(db, `UPDATE memories SET superseded_by = ? WHERE id = ? AND tenant_id = ? AND superseded_by IS NULL`).run(newId, id, tenantId);
|
|
295
295
|
return Number(result.changes ?? 0) !== 0;
|
|
296
296
|
}
|
|
297
|
+
/** Marks `id` superseded by `newId` in the caller's transaction, with the supersede audit row and
|
|
298
|
+
* its summary parent's dirty mark; false when the row is gone or another writer got there first. */
|
|
299
|
+
export function supersedeInTx(db, { tenantId, id, newId, actor }) {
|
|
300
|
+
const won = cachedStatement(db, 'UPDATE memories SET superseded_by = ? WHERE id = ? AND tenant_id = ? AND superseded_by IS NULL ' +
|
|
301
|
+
'RETURNING dag_parent_id').get(newId, id, tenantId);
|
|
302
|
+
if (!won)
|
|
303
|
+
return false;
|
|
304
|
+
if (won.dag_parent_id) {
|
|
305
|
+
markSummaryDirtyInTx(db, { summaryId: won.dag_parent_id, tenantId, actor });
|
|
306
|
+
}
|
|
307
|
+
appendAuditEvents(db, [{ tenantId, actor, op: 'supersede', targetId: id, metadata: { newId } }]);
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
297
310
|
/** Each call on its own handle; mirrors and the forgotten counter follow the commit, so a
|
|
298
311
|
* rolled-back write leaves neither. */
|
|
299
312
|
export function sqliteEntryWrites({ on, files }) {
|
|
@@ -14,6 +14,7 @@ const TOKEN_SURFACES = [
|
|
|
14
14
|
'hook',
|
|
15
15
|
'hook_recall',
|
|
16
16
|
'compact_resume',
|
|
17
|
+
'session_start',
|
|
17
18
|
'context',
|
|
18
19
|
'recall',
|
|
19
20
|
'mcp_recall',
|
|
@@ -24,7 +25,12 @@ const TOKEN_SURFACES = [
|
|
|
24
25
|
];
|
|
25
26
|
/** Surfaces whose re-reads are counted: only a hook payload tells a sub-agent's block from its
|
|
26
27
|
* parent's, as both carry one session id. */
|
|
27
|
-
const REREAD_SURFACES = [
|
|
28
|
+
const REREAD_SURFACES = [
|
|
29
|
+
'hook',
|
|
30
|
+
'hook_recall',
|
|
31
|
+
'compact_resume',
|
|
32
|
+
'session_start',
|
|
33
|
+
];
|
|
28
34
|
/** Rows older than this are pruned on write. */
|
|
29
35
|
const TOKEN_LEDGER_RETENTION_DAYS = 90;
|
|
30
36
|
/** The stamp a row written at `ts` prunes before: {@link TOKEN_LEDGER_RETENTION_DAYS} earlier. */
|
|
@@ -295,10 +295,13 @@ export function listTextsHolding(db, { tenantId, texts, project, admit = TEAM_VI
|
|
|
295
295
|
const byId = new Map();
|
|
296
296
|
for (const row of rowsHoldingTexts(db, { tenantId, filter }, texts)) {
|
|
297
297
|
byId.set(row.id, {
|
|
298
|
+
id: row.id,
|
|
298
299
|
content: row.content,
|
|
299
300
|
source: row.source,
|
|
300
301
|
originProject: row.origin_project,
|
|
301
302
|
pinned: row.pinned === 1,
|
|
303
|
+
supersededBy: row.superseded_by,
|
|
304
|
+
confidence: row.confidence,
|
|
302
305
|
});
|
|
303
306
|
}
|
|
304
307
|
return [...byId.values()];
|
|
@@ -10,6 +10,8 @@ export type CopyDbRow = {
|
|
|
10
10
|
source: string;
|
|
11
11
|
origin_project: string | null;
|
|
12
12
|
pinned: number;
|
|
13
|
+
superseded_by: string | null;
|
|
14
|
+
confidence: string;
|
|
13
15
|
};
|
|
14
16
|
/** Which rows of the tenant a copy read looks at: `filter` adds predicates, each written ` AND
|
|
15
17
|
* ...`. */
|
|
@@ -33,7 +33,8 @@ const MAY_HOLD_COPY_SQL = `(${bareTextSql('content')} IN (SELECT value FROM json
|
|
|
33
33
|
function copyChunk(db, read, texts, phrases) {
|
|
34
34
|
const tenant = tenantAnd(read.tenantId, phrases ? TENANT_IS : 'tenant_id = ?');
|
|
35
35
|
const bares = JSON.stringify([...new Set(texts.map(bareText))]);
|
|
36
|
-
return cachedStatement(db, `SELECT id, tenant_id, content, source, origin_project, pinned
|
|
36
|
+
return cachedStatement(db, `SELECT id, tenant_id, content, source, origin_project, pinned, superseded_by, confidence ` +
|
|
37
|
+
`FROM memories ` +
|
|
37
38
|
`WHERE ${phrases ? IN_PHRASE_HITS : '1'}` +
|
|
38
39
|
`${tenant.sql}${read.filter.sql} AND ${MAY_HOLD_COPY_SQL}`).all(...(phrases ? [phrases.join(' OR ')] : []), ...tenant.params, ...read.filter.params, bares, bares);
|
|
39
40
|
}
|
|
@@ -17,6 +17,9 @@ export interface CaptureOptions {
|
|
|
17
17
|
stdinText?: string;
|
|
18
18
|
stdinTimedOut?: boolean;
|
|
19
19
|
sessionTurns?: readonly SessionTurn[];
|
|
20
|
+
/** False when `sessionTurns` is a later slice of the session (a turn capture), so its first user
|
|
21
|
+
* turn answers the agent. */
|
|
22
|
+
fromStart?: boolean;
|
|
20
23
|
/** Tee stdout/stderr to this log file while capture runs, so SessionEnd output (lost in TUI teardown) shows via
|
|
21
24
|
* `hippo last-sleep`.
|
|
22
25
|
* Appends, because `hippo sleep` writes the same file first. */
|
|
@@ -43,11 +46,15 @@ export interface CaptureTarget {
|
|
|
43
46
|
}
|
|
44
47
|
export declare function runCapture(target: CaptureTarget, options: CaptureOptions): Promise<void>;
|
|
45
48
|
/** The items with `scrub` applied to each text, as a path stores them; scrubbing can make two
|
|
46
|
-
* items one, and the first stays. */
|
|
49
|
+
* items one, and the first stays unless a later one is the person's rule. */
|
|
47
50
|
export declare function scrubbedItems(items: readonly ExtractedItem[], scrub: (text: string) => string): ExtractedItem[];
|
|
48
51
|
/** Held keys, each twin's clean text too when a held row has its old text and repair keeps that
|
|
49
52
|
* row: a pinned row, or one whose text is no certain defect. */
|
|
50
53
|
export declare function heldKeysOf(rows: readonly HeldText[], twins: readonly BoldTwin[]): Set<string>;
|
|
54
|
+
/** The held keys that also stop a stated rule written for `ruleProject` (its names): every copy
|
|
55
|
+
* but one the store's write replaces, which holds the rule's exact text, so a bold twin still
|
|
56
|
+
* stops it. */
|
|
57
|
+
export declare function ruleHeldKeysOf(rows: readonly HeldText[], twins: readonly BoldTwin[], ruleProject: readonly string[]): Set<string>;
|
|
51
58
|
export interface CaptureTally {
|
|
52
59
|
captured: number;
|
|
53
60
|
skipped: number;
|
|
@@ -77,5 +84,5 @@ export interface ConfiguredStore {
|
|
|
77
84
|
}
|
|
78
85
|
/** Writes each new text once; a repeat of a captured text in the same batch counts as skipped,
|
|
79
86
|
* as a held one does. */
|
|
80
|
-
export declare function captureExtractedItems({ store, config, embed }: ConfiguredStore, options: CaptureWriteOptions, extracted: readonly ExtractedItem[], keys: Set<string>): Promise<CaptureTally>;
|
|
87
|
+
export declare function captureExtractedItems({ store, config, embed }: ConfiguredStore, options: CaptureWriteOptions, extracted: readonly ExtractedItem[], keys: Set<string>, ruleKeys?: ReadonlySet<string>): Promise<CaptureTally>;
|
|
81
88
|
//# sourceMappingURL=command.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fs from 'node:fs';
|
|
2
2
|
import * as path from 'node:path';
|
|
3
|
-
import { createMemory, Layer } from '../../base/memory/entry.js';
|
|
4
|
-
import { duplicateKey, storedTextKeys } from '../../base/util/same-text.js';
|
|
3
|
+
import { createMemory, Layer, USER_RULE_TAG } from '../../base/memory/entry.js';
|
|
4
|
+
import { duplicateKey, ruleReplaces, storedTextKeys } from '../../base/util/same-text.js';
|
|
5
5
|
import { isCertainReason, storedDefect } from '../../base/memory/memory-quality.js';
|
|
6
6
|
import { maskEmails, redactSecretsStrict } from '../../base/util/secret-detect.js';
|
|
7
7
|
import { projectId, projectNames } from '../../base/scope/project-identity.js';
|
|
@@ -9,7 +9,7 @@ import { isInitialized } from '../../host/project-identity.js';
|
|
|
9
9
|
import { errorMessage, log, errorFields } from '../../base/util/log.js';
|
|
10
10
|
import { teeStdStreams } from '../../host/stream-tee.js';
|
|
11
11
|
import { ALL_TENANTS } from '../../base/util/tenant.js';
|
|
12
|
-
import { boldTwins, extractFromTexts } from './extract.js';
|
|
12
|
+
import { boldTwins, extractFromTexts, isStatedRule, oncePerText, } from './extract.js';
|
|
13
13
|
import { collectSessionTurns, sessionTail, resolveLastSessionTranscript, } from './transcript.js';
|
|
14
14
|
import { PRIVATE_DIR } from '../../host/private-dir.js';
|
|
15
15
|
import { evalClock } from '../../infra/eval-switches.js';
|
|
@@ -67,7 +67,7 @@ async function runCaptureCore(target, options) {
|
|
|
67
67
|
const read = readCaptureTexts(options);
|
|
68
68
|
if (read === null)
|
|
69
69
|
return;
|
|
70
|
-
const { texts, userTexts } = read;
|
|
70
|
+
const { texts, userTexts, firstFollowUp } = read;
|
|
71
71
|
if (texts.every((text) => text.trim().length === 0)) {
|
|
72
72
|
console.log('No text to capture from.');
|
|
73
73
|
return;
|
|
@@ -76,71 +76,84 @@ async function runCaptureCore(target, options) {
|
|
|
76
76
|
// pass could not see (AGENTS.md: no secrets in memories).
|
|
77
77
|
const scrub = (text) => maskEmails(redactSecretsStrict(text));
|
|
78
78
|
const scrubbed = texts.map(scrub);
|
|
79
|
-
const extracted = scrubbedItems(extractFromTexts(scrubbed, userTexts), scrub);
|
|
79
|
+
const extracted = scrubbedItems(extractFromTexts(scrubbed, userTexts, firstFollowUp), scrub);
|
|
80
80
|
if (extracted.length === 0) {
|
|
81
81
|
console.log('No actionable items found in the input.');
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
84
|
-
const
|
|
85
|
-
|
|
84
|
+
const dedup = await captureDedup(target.store.folderFiles, options);
|
|
85
|
+
const held = await storedKeysInView(target.store, dedup, extracted, boldTwins(scrubbed, scrub));
|
|
86
|
+
printCaptureTally(options, await captureFromCli(target, options, dedup, extracted, held));
|
|
86
87
|
}
|
|
87
|
-
async function captureFromCli({ store, config, embed }, options, extracted, keys) {
|
|
88
|
+
async function captureFromCli({ store, config, embed }, options, dedup, extracted, { keys, ruleKeys }) {
|
|
88
89
|
const writeOpts = {
|
|
89
90
|
tenantId: options.global ? undefined : options.tenantId,
|
|
90
91
|
originProject: options.originProject,
|
|
91
92
|
lean: false,
|
|
92
|
-
dedup
|
|
93
|
+
dedup,
|
|
93
94
|
now: evalClock()(),
|
|
94
95
|
};
|
|
95
96
|
if (!options.dryRun) {
|
|
96
|
-
return captureExtractedItems({ store, config, embed }, writeOpts, extracted, keys);
|
|
97
|
+
return captureExtractedItems({ store, config, embed }, writeOpts, extracted, keys, ruleKeys);
|
|
97
98
|
}
|
|
98
|
-
return previewCapture({ store, config }, writeOpts, extracted, keys);
|
|
99
|
+
return previewCapture({ store, config }, writeOpts, extracted, keys, ruleKeys);
|
|
99
100
|
}
|
|
100
101
|
/** The items with `scrub` applied to each text, as a path stores them; scrubbing can make two
|
|
101
|
-
* items one, and the first stays. */
|
|
102
|
+
* items one, and the first stays unless a later one is the person's rule. */
|
|
102
103
|
export function scrubbedItems(items, scrub) {
|
|
103
|
-
|
|
104
|
-
return items
|
|
105
|
-
.map((item) => ({ ...item, content: scrub(item.content) }))
|
|
106
|
-
.filter((item) => {
|
|
107
|
-
const key = duplicateKey(item.content);
|
|
108
|
-
if (seen.has(key))
|
|
109
|
-
return false;
|
|
110
|
-
seen.add(key);
|
|
111
|
-
return true;
|
|
112
|
-
});
|
|
104
|
+
return oncePerText(items.map((item) => ({ ...item, content: scrub(item.content) })));
|
|
113
105
|
}
|
|
114
106
|
/** Held keys, each twin's clean text too when a held row has its old text and repair keeps that
|
|
115
107
|
* row: a pinned row, or one whose text is no certain defect. */
|
|
116
108
|
export function heldKeysOf(rows, twins) {
|
|
109
|
+
const keys = storedTextKeys(rows);
|
|
110
|
+
for (const key of twinKeys(rows, twins))
|
|
111
|
+
keys.add(key);
|
|
112
|
+
return keys;
|
|
113
|
+
}
|
|
114
|
+
/** The held keys that also stop a stated rule written for `ruleProject` (its names): every copy
|
|
115
|
+
* but one the store's write replaces, which holds the rule's exact text, so a bold twin still
|
|
116
|
+
* stops it. */
|
|
117
|
+
export function ruleHeldKeysOf(rows, twins, ruleProject) {
|
|
118
|
+
const keys = storedTextKeys(rows.filter((row) => !ruleReplaces(row, ruleProject)));
|
|
119
|
+
for (const key of twinKeys(rows, twins))
|
|
120
|
+
keys.add(key);
|
|
121
|
+
return keys;
|
|
122
|
+
}
|
|
123
|
+
function twinKeys(rows, twins) {
|
|
117
124
|
const keys = storedTextKeys(rows);
|
|
118
125
|
const pinned = storedTextKeys(rows.filter((row) => row.pinned));
|
|
119
|
-
|
|
126
|
+
return twins
|
|
120
127
|
.filter(({ old }) => (isCertainReason(storedDefect(old)) ? pinned : keys).has(duplicateKey(old)))
|
|
121
128
|
.map((twin) => duplicateKey(twin.clean));
|
|
122
|
-
for (const key of clean)
|
|
123
|
-
keys.add(key);
|
|
124
|
-
return keys;
|
|
125
129
|
}
|
|
126
130
|
// Dedup only against rows this capture's reader sees: another tenant's or project's rows are
|
|
127
131
|
// hidden, so they must not stop its own copy.
|
|
128
|
-
async function storedKeysInView(store,
|
|
129
|
-
const { tenantId, ...filter } = captureDedup(options);
|
|
132
|
+
async function storedKeysInView(store, { tenantId, project, everyScope, ruleProject = [] }, extracted, twins) {
|
|
130
133
|
// The lookup matches stored text exactly, so it also asks for the lines as capture once stored them.
|
|
131
134
|
const texts = [...extracted.map((item) => item.content), ...twins.map((twin) => twin.old)];
|
|
132
|
-
|
|
135
|
+
const rows = await store.captures.textsHolding(tenantId, texts, { project, everyScope });
|
|
136
|
+
return { keys: heldKeysOf(rows, twins), ruleKeys: ruleHeldKeysOf(rows, twins, ruleProject) };
|
|
133
137
|
}
|
|
134
138
|
/** The rows a capture's dedup reads, as data, so the store's write can read them again in its
|
|
135
|
-
* own transaction. */
|
|
136
|
-
function captureDedup(options) {
|
|
139
|
+
* own transaction, and the project a stated rule is written for. */
|
|
140
|
+
async function captureDedup(folderFiles, options) {
|
|
137
141
|
const tenantId = options.global ? ALL_TENANTS : options.tenantId;
|
|
138
142
|
const origin = options.originProject;
|
|
139
143
|
// classifyOriginProject in SQL: a project with no id sees every row, any other its own names and
|
|
140
144
|
// user-global rows.
|
|
141
|
-
if (origin
|
|
142
|
-
|
|
143
|
-
|
|
145
|
+
if (origin !== undefined && projectId(origin) !== '') {
|
|
146
|
+
const names = projectNames(origin);
|
|
147
|
+
return { tenantId, everyScope: true, project: names, ruleProject: names };
|
|
148
|
+
}
|
|
149
|
+
const ruleProject = origin === undefined ? await stampedProject(folderFiles) : [];
|
|
150
|
+
return { tenantId, everyScope: true, ruleProject };
|
|
151
|
+
}
|
|
152
|
+
/** The names of the project the store stamps on a row that names none: none for a shared or a
|
|
153
|
+
* user-global store. */
|
|
154
|
+
async function stampedProject(folderFiles) {
|
|
155
|
+
const { shared, project } = await folderFiles.home();
|
|
156
|
+
return shared || projectId(project) === '' ? [] : projectNames(project);
|
|
144
157
|
}
|
|
145
158
|
function printCaptureTally(options, { captured, skipped, rejected }) {
|
|
146
159
|
const prefix = options.dryRun ? '[dry-run] ' : '';
|
|
@@ -149,9 +162,6 @@ function printCaptureTally(options, { captured, skipped, rejected }) {
|
|
|
149
162
|
(rejected > 0 ? `, ${rejected} rejected` : '') +
|
|
150
163
|
')');
|
|
151
164
|
}
|
|
152
|
-
/** The raw texts for the chosen source, one per session turn, the person's turns first; null after printing why a
|
|
153
|
-
* last-session capture has nothing.
|
|
154
|
-
* Only a session says who spoke, so a piped or file capture counts no user texts. */
|
|
155
165
|
function readCaptureTexts(options) {
|
|
156
166
|
switch (options.source) {
|
|
157
167
|
case 'stdin': {
|
|
@@ -199,16 +209,19 @@ function readSessionTexts(options) {
|
|
|
199
209
|
console.log('Transcript had no user/assistant messages to summarise.');
|
|
200
210
|
return null;
|
|
201
211
|
}
|
|
202
|
-
|
|
212
|
+
// The first user turn is the request; a tail that cut it off, or a later slice, holds only replies.
|
|
213
|
+
const allUsers = turns.filter((t) => t.role === 'user').length;
|
|
214
|
+
const opener = options.fromStart !== false && users.length === allUsers ? 1 : 0;
|
|
215
|
+
return { texts: [...users, ...assistants], userTexts: users.length, firstFollowUp: opener };
|
|
203
216
|
}
|
|
204
217
|
/** One entry per new text: building can throw, so a held text or a repeat within the batch is
|
|
205
218
|
* never built. */
|
|
206
|
-
function planCapture(extracted,
|
|
219
|
+
function planCapture(extracted, isHeld, build) {
|
|
207
220
|
const plan = { entries: [], keys: [], slots: [] };
|
|
208
221
|
const firstSlot = new Map();
|
|
209
222
|
for (const item of extracted) {
|
|
210
223
|
const key = duplicateKey(item.content);
|
|
211
|
-
let slot =
|
|
224
|
+
let slot = isHeld(item, key) ? -1 : firstSlot.get(key);
|
|
212
225
|
if (slot === undefined) {
|
|
213
226
|
slot = plan.entries.push(build(item)) - 1;
|
|
214
227
|
plan.keys.push(key);
|
|
@@ -218,10 +231,14 @@ function planCapture(extracted, held, build) {
|
|
|
218
231
|
}
|
|
219
232
|
return plan;
|
|
220
233
|
}
|
|
234
|
+
/** Whether a held text stops `item`: any copy stops a note, only a `ruleKeys` copy a stated rule. */
|
|
235
|
+
function heldCheck(keys, ruleKeys) {
|
|
236
|
+
return (item, key) => (isStatedRule(item) ? ruleKeys : keys).has(key);
|
|
237
|
+
}
|
|
221
238
|
/** Writes each new text once; a repeat of a captured text in the same batch counts as skipped,
|
|
222
239
|
* as a held one does. */
|
|
223
|
-
export async function captureExtractedItems({ store, config, embed }, options, extracted, keys) {
|
|
224
|
-
const plan = planCapture(extracted, keys, (item) => captureEntry(item, options, config.defaultHalfLifeDays));
|
|
240
|
+
export async function captureExtractedItems({ store, config, embed }, options, extracted, keys, ruleKeys = keys) {
|
|
241
|
+
const plan = planCapture(extracted, heldCheck(keys, ruleKeys), (item) => captureEntry(item, options, config.defaultHalfLifeDays));
|
|
225
242
|
const { actor, lean, dedup } = options;
|
|
226
243
|
const outcomes = await store.captures.write(plan.entries, { actor, lean, dedup });
|
|
227
244
|
const tally = { captured: 0, skipped: 0, rejected: 0 };
|
|
@@ -242,16 +259,17 @@ export async function captureExtractedItems({ store, config, embed }, options, e
|
|
|
242
259
|
}
|
|
243
260
|
/** A dry run asks the store the same rejected-value question the write's gate does, so a
|
|
244
261
|
* tombstoned item previews as rejected rather than captured. */
|
|
245
|
-
async function previewCapture({ store: { rejections }, config }, options, extracted, keys) {
|
|
262
|
+
async function previewCapture({ store: { rejections }, config }, options, extracted, keys, ruleKeys) {
|
|
246
263
|
const tally = { captured: 0, skipped: 0, rejected: 0 };
|
|
247
|
-
const
|
|
264
|
+
const held = heldCheck(keys, ruleKeys);
|
|
265
|
+
const fresh = extracted.filter((item) => !held(item, duplicateKey(item.content)));
|
|
248
266
|
const refused = await rejections.refusedAmong(fresh.map((item) => {
|
|
249
267
|
const entry = captureEntry(item, options, config.defaultHalfLifeDays);
|
|
250
268
|
return { tenantId: entry.tenantId, entryId: entry.id, content: entry.content };
|
|
251
269
|
}));
|
|
252
270
|
let next = 0;
|
|
253
271
|
for (const item of extracted) {
|
|
254
|
-
if (
|
|
272
|
+
if (held(item, duplicateKey(item.content))) {
|
|
255
273
|
tally.skipped++;
|
|
256
274
|
console.log(` [skip] (${item.category}) ${item.content.slice(0, 80)}`);
|
|
257
275
|
}
|
|
@@ -271,13 +289,14 @@ function captureEntry(item, options, baseHalfLifeDays) {
|
|
|
271
289
|
// The write tenant must match the dedup read's, or scoped dedup passes and the row lands in
|
|
272
290
|
// 'default'. A rule the person stated is pinned, so it injects even when a later prompt shares
|
|
273
291
|
// none of its words. SHORTCUT: every stated rule pins; cap or age these pins if a store's pins
|
|
274
|
-
// outgrow the hook budget.
|
|
292
|
+
// outgrow the hook budget. The from-user tag marks the same pin as the person's rule for the hooks.
|
|
293
|
+
const stated = isStatedRule(item);
|
|
275
294
|
const created = createMemory(item.content, {
|
|
276
295
|
layer: Layer.Episodic,
|
|
277
|
-
tags: item.tags,
|
|
296
|
+
tags: stated ? [...item.tags, USER_RULE_TAG] : item.tags,
|
|
278
297
|
source: 'capture',
|
|
279
298
|
confidence: 'observed',
|
|
280
|
-
pinned:
|
|
299
|
+
pinned: stated,
|
|
281
300
|
tenantId: options.tenantId,
|
|
282
301
|
sourceSessionId: options.sessionId,
|
|
283
302
|
baseHalfLifeDays,
|
|
@@ -5,9 +5,16 @@ export interface ExtractedItem {
|
|
|
5
5
|
/** A person said it, not the agent: capture pins a rule that carries this. */
|
|
6
6
|
fromUser?: boolean;
|
|
7
7
|
}
|
|
8
|
+
/** A rule the person stated: capture pins it and tags it as theirs. */
|
|
9
|
+
export declare function isStatedRule(item: ExtractedItem): boolean;
|
|
10
|
+
/** Each text once, where its first copy stood; a later copy that is the person's rule replaces a
|
|
11
|
+
* first copy that is not, so a preference restated in a reply keeps its rule status. */
|
|
12
|
+
export declare function oncePerText(items: readonly ExtractedItem[], admit?: (item: ExtractedItem) => boolean): ExtractedItem[];
|
|
8
13
|
/** Captures complete supported statements, each text parsed alone so a fence or heading in one never reaches the next.
|
|
9
|
-
* The first `userTexts` texts are a person's own words; on a repeat their copy wins, so it keeps `fromUser
|
|
10
|
-
|
|
14
|
+
* The first `userTexts` texts are a person's own words; on a repeat their copy wins, so it keeps `fromUser`,
|
|
15
|
+
* and a copy that is their rule wins over one that is not.
|
|
16
|
+
* From `firstFollowUp` on, those words answer the agent's work, so a statement in them can be the person's rule. */
|
|
17
|
+
export declare function extractFromTexts(texts: readonly string[], userTexts?: number, firstFollowUp?: number): ExtractedItem[];
|
|
11
18
|
/** A text an older capture, which kept every bold marker, stored for a line, and what capture
|
|
12
19
|
* extracts from that line now. */
|
|
13
20
|
export interface BoldTwin {
|
|
@@ -2,6 +2,28 @@ import { duplicateKey } from '../../base/util/same-text.js';
|
|
|
2
2
|
import { assessAutomaticMemory, asideCloseOffset, QUOTE_MARK, } from '../../base/memory/memory-quality.js';
|
|
3
3
|
import { ERROR_TAG } from '../../base/memory/entry.js';
|
|
4
4
|
import { blankCodeSpans, strongMarkers } from '../../base/memory/markdown-delimiters.js';
|
|
5
|
+
/** A rule the person stated: capture pins it and tags it as theirs. */
|
|
6
|
+
export function isStatedRule(item) {
|
|
7
|
+
return item.fromUser === true && item.category === 'rule';
|
|
8
|
+
}
|
|
9
|
+
/** Each text once, where its first copy stood; a later copy that is the person's rule replaces a
|
|
10
|
+
* first copy that is not, so a preference restated in a reply keeps its rule status. */
|
|
11
|
+
export function oncePerText(items, admit = () => true) {
|
|
12
|
+
const kept = [];
|
|
13
|
+
const slot = new Map();
|
|
14
|
+
for (const item of items) {
|
|
15
|
+
const key = duplicateKey(item.content);
|
|
16
|
+
const at = slot.get(key);
|
|
17
|
+
if (at === undefined) {
|
|
18
|
+
if (admit(item))
|
|
19
|
+
slot.set(key, kept.push(item) - 1);
|
|
20
|
+
}
|
|
21
|
+
else if (isStatedRule(item) && !isStatedRule(kept[at]) && admit(item)) {
|
|
22
|
+
kept[at] = item;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return kept;
|
|
26
|
+
}
|
|
5
27
|
const DECISION_PATTERNS = [
|
|
6
28
|
/\b(?:we(?:'ve| have)?|i(?:'ve| have)?|let's)\s+decid(?:ed|e)\b/i,
|
|
7
29
|
/\b(?:let's|we(?:'ll| will| should)?)\s+(?:go with|do|use|try|build|implement|switch to)\b/i,
|
|
@@ -279,8 +301,55 @@ function extractSpecSections(prose, cuts) {
|
|
|
279
301
|
to,
|
|
280
302
|
}));
|
|
281
303
|
}
|
|
304
|
+
// A reply to the agent's work states how the person wants it done, rule word or not.
|
|
305
|
+
const ANY_STATEMENT = [/\S/];
|
|
306
|
+
// A kept reply names code or limits a choice; a thanks or a pasted test count does neither.
|
|
307
|
+
const CODE_CUE = /`|\w[\\/]\w|\w\.[A-Za-z]|\w_\w|\w\(|[<>{}[\]=]|(?:^|\s)--?[A-Za-z]/;
|
|
308
|
+
const LIMIT_CUE = /n't\b|\b(?:not|no longer|instead|rather|only|stop|unless|before|after|until|without|except)\b/i;
|
|
309
|
+
// Praise names code too ("Thanks, `src/cache.ts` looks good"); a thanks that goes on to direct is kept.
|
|
310
|
+
// SHORTCUT: word shapes, not meaning; the opt-in model capture is the upgrade path.
|
|
311
|
+
const ACK_LEAD = /^(?:thanks|thank you|ok(?:ay)?|great|nice|perfect|cool|lgtm)\b[\s,.!:-]*/i;
|
|
312
|
+
const NOT_A_DIRECTIVE = /^(?:for|on|to|so|the|a|an|this|that|it|its|you|your|i|we|all|really)\b/i;
|
|
313
|
+
const APPROVAL = /^(?:\S+\s+){0,3}(?:looks?|works?|worked)\s+(?:good|great|fine|right|now)\b/i;
|
|
314
|
+
// Pasted output opens on a file:line, a stack frame or an error word, or holds a file:line:col
|
|
315
|
+
// (a compiler arrow line) or an assertion message.
|
|
316
|
+
const LOG_CUE = new RegExp([
|
|
317
|
+
String.raw `^#\d+\s|^\S*\w\.[A-Za-z]+:\d+|\w\.[A-Za-z]+:\d+:\d+`,
|
|
318
|
+
String.raw `^(?:\w*Error|error(?:\[\w+\])?:|warning:|FAIL|Traceback)`,
|
|
319
|
+
String.raw `\bexpected\b.+\bto (?:be|equal|have)\b`,
|
|
320
|
+
].join('|'));
|
|
321
|
+
/** A thanks with no directive after it, or a short "X looks good". */
|
|
322
|
+
function isPraise(content) {
|
|
323
|
+
const lead = ACK_LEAD.exec(content);
|
|
324
|
+
const rest = lead ? content.slice(lead[0].length) : content;
|
|
325
|
+
if (APPROVAL.test(rest))
|
|
326
|
+
return true;
|
|
327
|
+
if (!lead)
|
|
328
|
+
return false;
|
|
329
|
+
return rest.split(/\s+/).filter(Boolean).length <= 3 || NOT_A_DIRECTIVE.test(rest);
|
|
330
|
+
}
|
|
331
|
+
/** A statement in a reply as the person's rule: 2 words when it names code, 4 with a limiting word. */
|
|
332
|
+
function followUpRule(sentence) {
|
|
333
|
+
const item = extractFromPatterns(sentence, ANY_STATEMENT, 'rule');
|
|
334
|
+
if (!item)
|
|
335
|
+
return null;
|
|
336
|
+
const { content } = item;
|
|
337
|
+
const limits = LIMIT_CUE.test(content);
|
|
338
|
+
if (LOG_CUE.test(content) || (isPraise(content) && !limits))
|
|
339
|
+
return null;
|
|
340
|
+
const words = content.split(/\s+/).length;
|
|
341
|
+
const kept = (words >= 2 && CODE_CUE.test(content)) || (words >= 4 && limits);
|
|
342
|
+
return kept ? { ...item, tags: [...item.tags, 'follow-up'] } : null;
|
|
343
|
+
}
|
|
344
|
+
/** What a sentence holds; in a reply a preference that names code or limits a choice is a rule. */
|
|
345
|
+
function sentenceItem(sentence, followUp) {
|
|
346
|
+
const hit = CATEGORIES.map(([category, patterns]) => extractFromPatterns(sentence, patterns, category)).find((found) => found !== null) ?? null;
|
|
347
|
+
if (!followUp || (hit !== null && hit.category !== 'preference'))
|
|
348
|
+
return hit;
|
|
349
|
+
return followUpRule(sentence) ?? hit;
|
|
350
|
+
}
|
|
282
351
|
// The items of the spec bullets first, then those of each prose block.
|
|
283
|
-
function extractSourced(text, cuts) {
|
|
352
|
+
function extractSourced(text, cuts, followUp = false) {
|
|
284
353
|
const prose = proseOnly(text);
|
|
285
354
|
const found = extractSpecSections(prose, cuts);
|
|
286
355
|
for (const block of proseBlocks(prose, cuts)) {
|
|
@@ -291,31 +360,22 @@ function extractSourced(text, cuts) {
|
|
|
291
360
|
continue;
|
|
292
361
|
if (cutByAside(sentence, at))
|
|
293
362
|
continue;
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
found.push({ item, from, to });
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
363
|
+
const item = sentenceItem(sentence, followUp);
|
|
364
|
+
if (item) {
|
|
365
|
+
const [from, to] = block.sourceOf(at, at + sentence.length);
|
|
366
|
+
found.push({ item, from, to });
|
|
301
367
|
}
|
|
302
368
|
}
|
|
303
369
|
}
|
|
304
370
|
return found;
|
|
305
371
|
}
|
|
306
372
|
/** Captures complete supported statements, each text parsed alone so a fence or heading in one never reaches the next.
|
|
307
|
-
* The first `userTexts` texts are a person's own words; on a repeat their copy wins, so it keeps `fromUser
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
const key = duplicateKey(item.content);
|
|
314
|
-
if (seen.has(key) || !assessAutomaticMemory(item.content).accepted)
|
|
315
|
-
return false;
|
|
316
|
-
seen.add(key);
|
|
317
|
-
return true;
|
|
318
|
-
});
|
|
373
|
+
* The first `userTexts` texts are a person's own words; on a repeat their copy wins, so it keeps `fromUser`,
|
|
374
|
+
* and a copy that is their rule wins over one that is not.
|
|
375
|
+
* From `firstFollowUp` on, those words answer the agent's work, so a statement in them can be the person's rule. */
|
|
376
|
+
export function extractFromTexts(texts, userTexts = 0, firstFollowUp = userTexts) {
|
|
377
|
+
const items = texts.flatMap((text, i) => extractSourced(text, boldPairs, i >= firstFollowUp && i < userTexts).map(({ item }) => i < userTexts ? { ...item, fromUser: true } : item));
|
|
378
|
+
return oncePerText(items, (item) => assessAutomaticMemory(item.content).accepted);
|
|
319
379
|
}
|
|
320
380
|
function trackOf(items) {
|
|
321
381
|
return { items, ends: items.map(({ to }) => to) };
|
|
@@ -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
|
|
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
|