agent-recall-core 3.4.41 → 3.4.43
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/host-profile.d.ts +35 -0
- package/dist/host-profile.d.ts.map +1 -1
- package/dist/host-profile.js +37 -0
- package/dist/host-profile.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/storage/archive-write.d.ts.map +1 -1
- package/dist/storage/archive-write.js +13 -2
- package/dist/storage/archive-write.js.map +1 -1
- package/dist/storage/consolidation-queue.d.ts.map +1 -1
- package/dist/storage/consolidation-queue.js +48 -6
- package/dist/storage/consolidation-queue.js.map +1 -1
- package/dist/storage/recency-index.d.ts +20 -0
- package/dist/storage/recency-index.d.ts.map +1 -1
- package/dist/storage/recency-index.js +66 -2
- package/dist/storage/recency-index.js.map +1 -1
- package/dist/storage/session-card.d.ts +20 -4
- package/dist/storage/session-card.d.ts.map +1 -1
- package/dist/storage/session-card.js +19 -5
- package/dist/storage/session-card.js.map +1 -1
- package/dist/storage/working-memory.d.ts +257 -0
- package/dist/storage/working-memory.d.ts.map +1 -0
- package/dist/storage/working-memory.js +615 -0
- package/dist/storage/working-memory.js.map +1 -0
- package/dist/tools-logic/resurrect.d.ts +9 -2
- package/dist/tools-logic/resurrect.d.ts.map +1 -1
- package/dist/tools-logic/resurrect.js +43 -2
- package/dist/tools-logic/resurrect.js.map +1 -1
- package/dist/tools-logic/session-end.d.ts.map +1 -1
- package/dist/tools-logic/session-end.js +118 -15
- package/dist/tools-logic/session-end.js.map +1 -1
- package/dist/tools-logic/session-start.d.ts +12 -0
- package/dist/tools-logic/session-start.d.ts.map +1 -1
- package/dist/tools-logic/session-start.js +79 -1
- package/dist/tools-logic/session-start.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -1
- package/package.json +1 -1
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
import * as fs from "node:fs";
|
|
32
32
|
import * as path from "node:path";
|
|
33
33
|
import { getRoot } from "../types.js";
|
|
34
|
+
import { recordHookFailure } from "./hook-health.js";
|
|
34
35
|
const RECENCY_FILENAME = "recent-sessions.jsonl";
|
|
35
36
|
const MAX_LINES = 500;
|
|
36
37
|
/**
|
|
@@ -89,6 +90,10 @@ function rollIfNeeded(filePath) {
|
|
|
89
90
|
catch {
|
|
90
91
|
// Lock already held. If it's stale (a crashed writer, >ROLL_LOCK_STALE_MS
|
|
91
92
|
// old), force-break it and take it; otherwise skip this roll entirely.
|
|
93
|
+
// F5 depth (2026-08-12, followups wave): intentionally left UNWIRED —
|
|
94
|
+
// lock contention is EXPECTED/normal concurrent-access behavior, not a
|
|
95
|
+
// failure; reporting every contended roll would spam hook-health on
|
|
96
|
+
// ordinary concurrent hook-end runs. See report.
|
|
92
97
|
try {
|
|
93
98
|
const stat = fs.statSync(lockPath);
|
|
94
99
|
if (Date.now() - stat.mtimeMs <= ROLL_LOCK_STALE_MS)
|
|
@@ -97,6 +102,11 @@ function rollIfNeeded(filePath) {
|
|
|
97
102
|
fd = fs.openSync(lockPath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_WRONLY);
|
|
98
103
|
}
|
|
99
104
|
catch {
|
|
105
|
+
// F5 depth (2026-08-12, followups wave): intentionally left UNWIRED —
|
|
106
|
+
// rollIfNeeded is pure maintenance (trims an already-successfully-
|
|
107
|
+
// appended file); worst case the file grows a bit larger until the
|
|
108
|
+
// next append retries. No data loss, same "expected contention" class
|
|
109
|
+
// as the outer catch above.
|
|
100
110
|
return; // lost the race to break the stale lock, or another fs error — skip, never throw
|
|
101
111
|
}
|
|
102
112
|
}
|
|
@@ -106,6 +116,9 @@ function rollIfNeeded(filePath) {
|
|
|
106
116
|
fs.renameSync(tmpPath, filePath);
|
|
107
117
|
}
|
|
108
118
|
finally {
|
|
119
|
+
// F5 depth (2026-08-12, followups wave): intentionally left UNWIRED —
|
|
120
|
+
// idempotent cleanup no-ops ("already closed"/"already removed" are
|
|
121
|
+
// literally not error conditions).
|
|
109
122
|
try {
|
|
110
123
|
fs.closeSync(fd);
|
|
111
124
|
}
|
|
@@ -131,8 +144,17 @@ export function appendRecentSession(entry) {
|
|
|
131
144
|
fs.appendFileSync(filePath, JSON.stringify(entry) + "\n", "utf-8");
|
|
132
145
|
rollIfNeeded(filePath);
|
|
133
146
|
}
|
|
134
|
-
catch {
|
|
147
|
+
catch (err) {
|
|
135
148
|
// Best-effort — never throw from an append-only telemetry ledger.
|
|
149
|
+
// F5 depth (2026-08-12, followups wave): was invisible on failure — the
|
|
150
|
+
// continuity card would just silently be missing this session forever.
|
|
151
|
+
// Read side (readRecentSessions): the OUTER read-failure catch is now
|
|
152
|
+
// wired too (see below) — a total read failure there would otherwise
|
|
153
|
+
// masquerade as "append never persisted" to the CLI's hook-start
|
|
154
|
+
// wm-rescue verification (index.ts ~line 1197), misattributing a read
|
|
155
|
+
// bug as a write bug. The per-line corrupt-entry skip inside the read is
|
|
156
|
+
// left unwired — see report.
|
|
157
|
+
recordHookFailure("recency-append", err);
|
|
136
158
|
}
|
|
137
159
|
}
|
|
138
160
|
/**
|
|
@@ -144,6 +166,26 @@ export function appendRecentSession(entry) {
|
|
|
144
166
|
* write from a crash mid-append) are skipped individually rather than
|
|
145
167
|
* aborting the whole read. Returns `[]` when the index does not exist yet,
|
|
146
168
|
* when `n <= 0`, or on any read failure — never throws.
|
|
169
|
+
*
|
|
170
|
+
* M1 fix (Train C review, 2026-08-12 wave) — dedupe by `sid` at READ time,
|
|
171
|
+
* keeping the newest occurrence (we're already iterating the file
|
|
172
|
+
* newest-first, so the FIRST time a given sid is seen IS its newest entry).
|
|
173
|
+
* Root cause: two independent sweep callers can both append a recency entry
|
|
174
|
+
* for the SAME sid across a narrow cross-process TOCTOU window — the CLI's
|
|
175
|
+
* `hook-start` sweep and core's own `sessionStart()` can each observe "no
|
|
176
|
+
* recency entry yet for this sid" and both append, landing two lines for one
|
|
177
|
+
* logical session. Rather than chase every present-and-future write-side
|
|
178
|
+
* race that could produce a duplicate append (a per-caller fix only ever
|
|
179
|
+
* covers the callers audited today), this is the CLASS fix: collapsing
|
|
180
|
+
* duplicates at the single read path every consumer (the continuity card,
|
|
181
|
+
* `rescueOrphanedWorkingMemory`'s/`distillSessionToCard`'s own
|
|
182
|
+
* already-rescued check) shares makes ANY duplicate-append path — known or
|
|
183
|
+
* not-yet-written — structurally harmless. The write side
|
|
184
|
+
* (`appendRecentSession`) is intentionally left as a plain best-effort
|
|
185
|
+
* append, unchanged — see this module's own header for why duplicate
|
|
186
|
+
* detection belongs here, not there (a write-side lock would turn a
|
|
187
|
+
* per-session append into a cross-process contention point for a
|
|
188
|
+
* consistency property the read side can already guarantee for free).
|
|
147
189
|
*/
|
|
148
190
|
export function readRecentSessions(n) {
|
|
149
191
|
if (n <= 0)
|
|
@@ -154,20 +196,42 @@ export function readRecentSessions(n) {
|
|
|
154
196
|
return [];
|
|
155
197
|
const lines = fs.readFileSync(filePath, "utf-8").split("\n").filter(Boolean);
|
|
156
198
|
const out = [];
|
|
199
|
+
const seenSids = new Set();
|
|
157
200
|
for (let i = lines.length - 1; i >= 0 && out.length < n; i--) {
|
|
158
201
|
try {
|
|
159
202
|
const parsed = JSON.parse(lines[i]);
|
|
160
203
|
if (parsed && typeof parsed.slug === "string" && typeof parsed.title === "string" && typeof parsed.ts === "string") {
|
|
204
|
+
// Dedupe only when `sid` is a genuine, non-empty string — an entry
|
|
205
|
+
// with a missing/malformed sid (pre-existing data, or a caller
|
|
206
|
+
// that never set one) has no reliable identity to collapse on, so
|
|
207
|
+
// it is kept as-is rather than risking an over-aggressive merge.
|
|
208
|
+
if (typeof parsed.sid === "string" && parsed.sid.length > 0) {
|
|
209
|
+
if (seenSids.has(parsed.sid))
|
|
210
|
+
continue; // older duplicate of an sid already kept — drop it
|
|
211
|
+
seenSids.add(parsed.sid);
|
|
212
|
+
}
|
|
161
213
|
out.push(parsed);
|
|
162
214
|
}
|
|
163
215
|
}
|
|
164
216
|
catch {
|
|
165
217
|
// Skip a corrupt/partial line rather than aborting the whole read.
|
|
218
|
+
// F5 depth (2026-08-12, followups wave): intentionally left UNWIRED —
|
|
219
|
+
// one torn line among potentially hundreds is routine (documented
|
|
220
|
+
// above as an expected crash-mid-append artifact); the outer catch
|
|
221
|
+
// below covers "the whole read is broken," the meaningful signal.
|
|
166
222
|
}
|
|
167
223
|
}
|
|
168
224
|
return out;
|
|
169
225
|
}
|
|
170
|
-
catch {
|
|
226
|
+
catch (err) {
|
|
227
|
+
// F5 depth (2026-08-12, followups wave): a total read failure here
|
|
228
|
+
// (corrupt file, permission) silently returns [] — indistinguishable
|
|
229
|
+
// from "no continuity history yet" to the session_start renderer AND to
|
|
230
|
+
// the hook-start wm-rescue verification (index.ts ~line 1197) that reads
|
|
231
|
+
// this list to confirm appendRecentSession's write actually landed. That
|
|
232
|
+
// verification would misreport a read-side failure as a write-side one
|
|
233
|
+
// without this wire.
|
|
234
|
+
recordHookFailure("recency-read", err);
|
|
171
235
|
return [];
|
|
172
236
|
}
|
|
173
237
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recency-index.js","sourceRoot":"","sources":["../../src/storage/recency-index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"recency-index.js","sourceRoot":"","sources":["../../src/storage/recency-index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,C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AI,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;IACpC,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;QAAE,OAAO,cAAc,CAAC;IAE9C,MAAM,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC;IAC1B,IAAI,MAAM,GAAG,SAAS;QAAE,OAAO,UAAU,CAAC,CAAC,8CAA8C;IACzF,IAAI,MAAM,GAAG,OAAO;QAAE,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC,OAAO,CAAC;IACtE,IAAI,MAAM,GAAG,MAAM;QAAE,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IACnE,IAAI,MAAM,GAAG,OAAO;QAAE,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IACnE,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -52,6 +52,25 @@ export interface SessionCardResult {
|
|
|
52
52
|
slug: string;
|
|
53
53
|
date: string;
|
|
54
54
|
}
|
|
55
|
+
export interface WriteSessionCardResult {
|
|
56
|
+
path: string;
|
|
57
|
+
bytes: number;
|
|
58
|
+
/**
|
|
59
|
+
* The ACTUAL on-disk project slug this card was filed under, after
|
|
60
|
+
* `journalDir`'s case-fold EXISTING-DIR-reuse resolution (paths.ts's
|
|
61
|
+
* `resolveProjectDirName`, itself lowercasing via `sanitizeName`). This can
|
|
62
|
+
* — and, for a raw cwd-captured candidate like "AgentRecall", WILL —
|
|
63
|
+
* differ from the caller-supplied `card.slug`. Any caller that persists a
|
|
64
|
+
* SEPARATE record of "where this session lives" (e.g. the recency ledger's
|
|
65
|
+
* `slug` field) must key that record off THIS value, never off `card.slug`
|
|
66
|
+
* directly — using the un-normalized input there is exactly the ledger-vs-
|
|
67
|
+
* disk mismatch fixed in `distillOneSession` (working-memory.ts, Train C,
|
|
68
|
+
* 2026-08-13: "rescue ledger slug must match card's normalized on-disk
|
|
69
|
+
* slug"). Empty string only in the failure branch, where the write never
|
|
70
|
+
* got far enough to resolve a slug at all.
|
|
71
|
+
*/
|
|
72
|
+
slug: string;
|
|
73
|
+
}
|
|
55
74
|
/**
|
|
56
75
|
* Build a session card (F3) — pure-mechanical distillation of a session's
|
|
57
76
|
* head+tail transcript sample. Never throws: any internal failure degrades
|
|
@@ -67,8 +86,5 @@ export declare function buildSessionCard(raw: SessionCardInput): SessionCardResu
|
|
|
67
86
|
* archive-write.ts's convention) and never throws — a failed write must not
|
|
68
87
|
* break the hook-end / Stop turn.
|
|
69
88
|
*/
|
|
70
|
-
export declare function writeSessionCard(card: SessionCardResult):
|
|
71
|
-
path: string;
|
|
72
|
-
bytes: number;
|
|
73
|
-
};
|
|
89
|
+
export declare function writeSessionCard(card: SessionCardResult): WriteSessionCardResult;
|
|
74
90
|
//# sourceMappingURL=session-card.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-card.d.ts","sourceRoot":"","sources":["../../src/storage/session-card.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;
|
|
1
|
+
{"version":3,"file":"session-card.d.ts","sourceRoot":"","sources":["../../src/storage/session-card.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAuBH,MAAM,WAAW,eAAe;IAC9B,gEAAgE;IAChE,GAAG,EAAE,MAAM,CAAC;IACZ,6CAA6C;IAC7C,IAAI,EAAE,MAAM,CAAC;IACb,uEAAuE;IACvE,cAAc,EAAE,MAAM,CAAC;IACvB,wFAAwF;IACxF,cAAc,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACvD,2DAA2D;IAC3D,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,6FAA6F;IAC7F,OAAO,EAAE,MAAM,CAAC;IAChB,6FAA6F;IAC7F,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,eAAe,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;;;OAaG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAwED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,gBAAgB,GAAG,iBAAiB,CAoHzE;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,iBAAiB,GAAG,sBAAsB,CA4BhF"}
|
|
@@ -24,6 +24,7 @@ import * as path from "node:path";
|
|
|
24
24
|
import { journalDir, sanitizeSlug } from "./paths.js";
|
|
25
25
|
import { ensureDir, todayISO, truncateUtf8Bytes } from "./fs-utils.js";
|
|
26
26
|
import { generateFrontmatter } from "../palace/obsidian.js";
|
|
27
|
+
import { recordHookFailure } from "./hook-health.js";
|
|
27
28
|
import { DECISION_LINE_RE, NEXT_STEP_LINE_RE, parseJsonlLenient, extractArtifactPathsFromRecords, extractLinearRefsFromRecords, extractLinesMatching, extractFirstUserTextFromRecords, extractFinalRecordText, } from "./extraction.js";
|
|
28
29
|
// ---------------------------------------------------------------------------
|
|
29
30
|
// Field size budget (Card <= ~2KB, enforced in BYTES — this repo's content is
|
|
@@ -164,8 +165,13 @@ export function buildSessionCard(raw) {
|
|
|
164
165
|
const markdown = frontmatter + truncateBytes(body, bodyBudget);
|
|
165
166
|
return { markdown, title, artifacts, linearRefs, decisions, nextStep, sid, slug, date };
|
|
166
167
|
}
|
|
167
|
-
catch {
|
|
168
|
+
catch (err) {
|
|
168
169
|
// Never throw into the hook-end path — degrade to a minimal, valid card.
|
|
170
|
+
// F5 depth (2026-08-12, followups wave): degrading silently means the
|
|
171
|
+
// hook-end path reports success (a card WAS written) while the card is
|
|
172
|
+
// actually a content-free stub — recordHookFailure makes that visible
|
|
173
|
+
// without changing the degrade-to-stub behavior itself.
|
|
174
|
+
recordHookFailure("session-card-build", err);
|
|
169
175
|
const frontmatter = generateFrontmatter({
|
|
170
176
|
sid,
|
|
171
177
|
date,
|
|
@@ -202,18 +208,26 @@ export function writeSessionCard(card) {
|
|
|
202
208
|
const slug = sanitizeSlug(card.slug); // slug is caller-controlled; harden before path.join
|
|
203
209
|
const sid = sanitizeSlug(card.sid); // sid is UNTRUSTED (from hook stdin) — sanitize first
|
|
204
210
|
const dir = journalDir(slug);
|
|
211
|
+
// The ACTUAL on-disk slug directory name, AFTER journalDir's case-fold
|
|
212
|
+
// EXISTING-DIR-reuse resolution — see WriteSessionCardResult.slug's doc
|
|
213
|
+
// comment above for why this (not `slug`/`card.slug`) is the value
|
|
214
|
+
// callers must record wherever "where this card lives" needs persisting.
|
|
215
|
+
const resolvedSlug = path.basename(path.dirname(dir));
|
|
205
216
|
ensureDir(dir);
|
|
206
217
|
const dest = path.join(dir, `${card.date}--card--${sid}.md`);
|
|
207
218
|
if (fs.existsSync(dest)) {
|
|
208
|
-
return { path: dest, bytes: 0 };
|
|
219
|
+
return { path: dest, bytes: 0, slug: resolvedSlug };
|
|
209
220
|
}
|
|
210
221
|
const tmp = dest + ".tmp." + process.pid;
|
|
211
222
|
fs.writeFileSync(tmp, card.markdown, "utf-8");
|
|
212
223
|
fs.renameSync(tmp, dest);
|
|
213
|
-
return { path: dest, bytes: Buffer.byteLength(card.markdown, "utf-8") };
|
|
224
|
+
return { path: dest, bytes: Buffer.byteLength(card.markdown, "utf-8"), slug: resolvedSlug };
|
|
214
225
|
}
|
|
215
|
-
catch {
|
|
216
|
-
|
|
226
|
+
catch (err) {
|
|
227
|
+
// F5 depth (2026-08-12, followups wave): silent disk-write failure —
|
|
228
|
+
// same visibility fix as archiveSession/buildSessionCard above.
|
|
229
|
+
recordHookFailure("session-card-write", err);
|
|
230
|
+
return { path: "", bytes: 0, slug: "" };
|
|
217
231
|
}
|
|
218
232
|
}
|
|
219
233
|
//# sourceMappingURL=session-card.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-card.js","sourceRoot":"","sources":["../../src/storage/session-card.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,+BAA+B,EAC/B,4BAA4B,EAC5B,oBAAoB,EACpB,+BAA+B,EAC/B,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"session-card.js","sourceRoot":"","sources":["../../src/storage/session-card.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,UA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I,EAAE,YAAY,EAAE,CAAC;IAC9F,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,qEAAqE;QACrE,gEAAgE;QAChE,iBAAiB,CAAC,oBAAoB,EAAE,GAAG,CAAC,CAAC;QAC7C,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;IAC1C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* working-memory.ts — minutes-level, crash-proof capture tier
|
|
3
|
+
* (v3.4.42 working-memory wave, design doc
|
|
4
|
+
* reports/2026-08-04-working-memory-design.md).
|
|
5
|
+
*
|
|
6
|
+
* WHY: through v3.4.41, AR writes nothing until the Stop hook fires — a
|
|
7
|
+
* crash, `kill -9`, or a context-compact vaporizes the entire live session,
|
|
8
|
+
* and two concurrent Claude Code windows are mutually blind to each other.
|
|
9
|
+
* Owner intent (2026-07-31, verbatim spirit): "like a human brain — you can
|
|
10
|
+
* forget, you can decay, but you need to know what happened 10 minutes
|
|
11
|
+
* before, 5 minutes before." This module is the ONLY new storage primitive
|
|
12
|
+
* for that tier: one JSONL file per session id, appended to on every
|
|
13
|
+
* UserPromptSubmit (hook-ambient), read at hook-start for the cross-window
|
|
14
|
+
* "live" signal and for orphan rescue, and deleted once a session reaches a
|
|
15
|
+
* normal, successful hook-end. WM is NEVER archived — natural forgetting by
|
|
16
|
+
* design; it is a minutes-level cache, not a permanent record.
|
|
17
|
+
*
|
|
18
|
+
* Storage shape: `<AR_ROOT>/working-memory/<sid>.jsonl`, one JSON line per
|
|
19
|
+
* prompt, PLUS a small sidecar counter file `<sid>.jsonl.count` (a bare
|
|
20
|
+
* decimal integer) used ONLY to enforce the per-file line cap in O(1) —
|
|
21
|
+
* see `wmAppend`'s doc comment for why a sidecar counter, not a directory
|
|
22
|
+
* scan or a full-file read, is the right tool here. Per-sid files are a
|
|
23
|
+
* DELIBERATE choice (design doc, non-negotiable): a shared cross-session
|
|
24
|
+
* ledger would need a write-lock on every single prompt across every open
|
|
25
|
+
* window, turning a per-prompt hot path into a cross-process contention
|
|
26
|
+
* point. Per-sid files make that race structurally impossible — two windows
|
|
27
|
+
* literally never touch the same file.
|
|
28
|
+
*/
|
|
29
|
+
/** Per-sid line cap (design doc §Mechanism) — bounds disk, not memory. */
|
|
30
|
+
export declare const WM_LINE_CAP = 2000;
|
|
31
|
+
/** Prompt field byte cap (UTF-8-safe) applied at append time. */
|
|
32
|
+
export declare const WM_PROMPT_BYTE_CAP = 300;
|
|
33
|
+
/**
|
|
34
|
+
* Cross-window "live" line window (session-start.ts's continuity assembly):
|
|
35
|
+
* a WM file with mtime younger than this is treated as "another session is
|
|
36
|
+
* (or very recently was) active elsewhere". Exported so session-start.ts and
|
|
37
|
+
* this module share ONE threshold instead of two independently-tuned magic
|
|
38
|
+
* numbers.
|
|
39
|
+
*/
|
|
40
|
+
export declare const WM_LIVE_WINDOW_MS: number;
|
|
41
|
+
/**
|
|
42
|
+
* Orphan-rescue window (CLI hook-start): a WM file OLDER than this with no
|
|
43
|
+
* session card and no recency-index entry yet is presumed to belong to a
|
|
44
|
+
* session that crashed/was killed without ever reaching hook-end. Exported
|
|
45
|
+
* for the same single-source-of-truth reason as `WM_LIVE_WINDOW_MS`.
|
|
46
|
+
*/
|
|
47
|
+
export declare const WM_ORPHAN_WINDOW_MS: number;
|
|
48
|
+
export interface WorkingMemoryLine {
|
|
49
|
+
/** ISO-8601 timestamp of the prompt. */
|
|
50
|
+
ts: string;
|
|
51
|
+
/** Boilerplate-excluded, UTF-8-safe byte-capped prompt text. */
|
|
52
|
+
prompt: string;
|
|
53
|
+
/** Working directory at the time of the prompt, when known. */
|
|
54
|
+
cwd?: string;
|
|
55
|
+
}
|
|
56
|
+
export interface WorkingMemoryFileInfo {
|
|
57
|
+
/** Session id (the sanitized on-disk basename, minus the .jsonl extension). */
|
|
58
|
+
sid: string;
|
|
59
|
+
/** Last-modified time of the .jsonl file, as epoch milliseconds. */
|
|
60
|
+
mtimeMs: number;
|
|
61
|
+
/** Line count — read from the sidecar counter when present, else counted. */
|
|
62
|
+
lines: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Append one prompt to a session's working-memory file. Never throws — this
|
|
66
|
+
* runs on the hook-ambient hot path (every UserPromptSubmit) and a failure
|
|
67
|
+
* here must never delay or deny the ambient injection output that follows
|
|
68
|
+
* it in the caller. Any failure is reported via `recordHookFailure` instead.
|
|
69
|
+
*
|
|
70
|
+
* Boilerplate exclusion: a prompt that IS boilerplate (matches
|
|
71
|
+
* `isSystemText` — harness scaffolding, `<system-reminder>` blocks, etc.)
|
|
72
|
+
* appends NOTHING, not even an empty line, and does not consume any of the
|
|
73
|
+
* per-file line-cap budget.
|
|
74
|
+
*
|
|
75
|
+
* Line-cap strategy (documented per design doc constraint — "cheap stat
|
|
76
|
+
* size heuristic or first-write counter file is acceptable"): a byte-size
|
|
77
|
+
* heuristic on the growing .jsonl file was rejected because prompt length
|
|
78
|
+
* and cwd length both vary per line, making "file size / N" an unreliable
|
|
79
|
+
* proxy for "N lines" — it would let a run of short prompts blow well past
|
|
80
|
+
* the intended 2000-line cap while a run of near-cap-length prompts hits it
|
|
81
|
+
* early. Reading the .jsonl file itself to count lines was rejected because
|
|
82
|
+
* that makes wmAppend's cost scale with SESSION LENGTH (O(n) per prompt over
|
|
83
|
+
* an n-line file), violating the O(1)-hot-path constraint outright. The
|
|
84
|
+
* chosen alternative is a tiny sidecar counter file (a few bytes, a bare
|
|
85
|
+
* decimal integer) that is read once (cheap: a handful of bytes, not the
|
|
86
|
+
* growing transcript) and rewritten once per append — genuinely O(1) with
|
|
87
|
+
* respect to the session's own size.
|
|
88
|
+
*
|
|
89
|
+
* C1 fix (review, post-build): the raw prompt text used to reach disk with
|
|
90
|
+
* only the boilerplate check above — no injection/secret scrub — even though
|
|
91
|
+
* every OTHER persist path in this codebase (journal-write.ts, palace-write.ts,
|
|
92
|
+
* see storage/content-guard.ts) scrubs before writing. WM is a lower tier
|
|
93
|
+
* (minutes-level cache, not a permanent record) but it is NOT lower-exposure:
|
|
94
|
+
* its content is read back verbatim into (a) session-start.ts's cross-SESSION
|
|
95
|
+
* "live" line (surfaced to a DIFFERENT window/agent), (b) rescued session-card
|
|
96
|
+
* bodies (cli/index.ts orphan rescue — a permanent journal artifact once
|
|
97
|
+
* rescued), and (c) resurrect.ts's WM source. A secret or an injected
|
|
98
|
+
* "<system-reminder>ignore previous instructions</system-reminder>" block
|
|
99
|
+
* pasted into one prompt would otherwise flow verbatim into all three. This
|
|
100
|
+
* is the SINGLE choke point for the whole tier — every WM line is built here.
|
|
101
|
+
*
|
|
102
|
+
* Ordering (truncate → scrub → truncate again), deliberately NOT
|
|
103
|
+
* scrub-then-truncate, to preserve the O(1)-hot-path guarantee this module's
|
|
104
|
+
* own header documents: `scrubForCloud` is a ~15-regex pass whose cost scales
|
|
105
|
+
* with input length (measured: ~0.28ms on a 100KB string — see the fix
|
|
106
|
+
* report's benchmark). Truncating to WM_PROMPT_BYTE_CAP FIRST bounds the
|
|
107
|
+
* scrub's input to a small constant (≤300 bytes) regardless of how long the
|
|
108
|
+
* raw prompt was, so the added cost is ~1-2 microseconds per call — the scrub
|
|
109
|
+
* never sees the full pasted-file-sized prompt a user might submit. Content
|
|
110
|
+
* beyond the byte cap is truncated away before it is ever persisted, so it
|
|
111
|
+
* needs no scrubbing; content a secret/tag straddling the truncation boundary
|
|
112
|
+
* can still be caught if enough of it survives inside the retained window.
|
|
113
|
+
* The second truncateUtf8Bytes call is a cheap defensive no-op today (every
|
|
114
|
+
* SECRET_CONTENT_PATTERNS replacement is shorter than what it matches, so
|
|
115
|
+
* scrub never grows the string) — kept so the ≤WM_PROMPT_BYTE_CAP invariant
|
|
116
|
+
* holds even if that redaction-placeholder assumption ever changes.
|
|
117
|
+
*/
|
|
118
|
+
export declare function wmAppend(sid: string, entry: {
|
|
119
|
+
ts: string;
|
|
120
|
+
prompt: string;
|
|
121
|
+
cwd?: string;
|
|
122
|
+
}): void;
|
|
123
|
+
/**
|
|
124
|
+
* List every working-memory file currently on disk. Never throws — returns
|
|
125
|
+
* `[]` on any read error (missing directory, permissions, etc.). Used by
|
|
126
|
+
* BOTH the cross-window "live" line (session-start.ts, filters mtime <
|
|
127
|
+
* `WM_LIVE_WINDOW_MS`) and orphan rescue (CLI hook-start, filters mtime >
|
|
128
|
+
* `WM_ORPHAN_WINDOW_MS`) — not on any per-prompt hot path, so a full
|
|
129
|
+
* directory read here is acceptable (unlike `wmAppend`).
|
|
130
|
+
*/
|
|
131
|
+
export declare function wmList(): WorkingMemoryFileInfo[];
|
|
132
|
+
/**
|
|
133
|
+
* Read every parsed line of a session's working-memory file, oldest-first.
|
|
134
|
+
* Never throws — returns `[]` when the file is missing, empty, or every line
|
|
135
|
+
* fails to parse/validate.
|
|
136
|
+
*/
|
|
137
|
+
export declare function wmRead(sid: string): WorkingMemoryLine[];
|
|
138
|
+
/**
|
|
139
|
+
* Delete a session's working-memory file and its counter sidecar. Idempotent
|
|
140
|
+
* (calling twice, or on a sid with no file, is a silent no-op) and never
|
|
141
|
+
* throws — this runs on the hook-end "sleep consolidation" path (after a
|
|
142
|
+
* successful session-card write) and on the orphan-rescue path, neither of
|
|
143
|
+
* which may fail the hook over a best-effort cleanup step.
|
|
144
|
+
*/
|
|
145
|
+
export declare function wmDelete(sid: string): void;
|
|
146
|
+
/**
|
|
147
|
+
* Cheap, LOCAL cwd-majority slug guess for the two working-memory consumers
|
|
148
|
+
* that live in the CORE package (session-start.ts's live line, resurrect.ts's
|
|
149
|
+
* WM source) and therefore cannot import the CLI package's
|
|
150
|
+
* `resolveSessionProject` (packages/cli/src/utils/transcript-reader.ts) — the
|
|
151
|
+
* core package is a DEPENDENCY of the cli package, never the reverse, so a
|
|
152
|
+
* core module importing cli code would be a layering violation. Uses the
|
|
153
|
+
* SAME regex family as that function's Signal 1 (cwd frequency restricted to
|
|
154
|
+
* paths under `~/Projects/<name>`), deliberately WITHOUT its full three-signal
|
|
155
|
+
* claim-not-generate policy (no existing-slug preference, no content signal —
|
|
156
|
+
* working-memory lines carry only prompts + cwd, not the transcript's
|
|
157
|
+
* user/assistant message records that signal 2 scans).
|
|
158
|
+
*
|
|
159
|
+
* DELIBERATE choice, used by ALL THREE working-memory consumers (this core
|
|
160
|
+
* module's two call sites AND the CLI's orphan rescue, even though the
|
|
161
|
+
* latter COULD reach the real `resolveSessionProject`): F1's own
|
|
162
|
+
* claim-not-generate gate can only mint a BRAND-NEW slug when its content
|
|
163
|
+
* signal (from user/assistant transcript records) sees the name mentioned
|
|
164
|
+
* >=3 times — a signal that structurally never fires from cwd-only
|
|
165
|
+
* working-memory data. Routing orphan rescue through the real F1 function
|
|
166
|
+
* would therefore make it unable to attribute a crashed session's FIRST-EVER
|
|
167
|
+
* interaction with AR to its real project (exactly the case a crash-rescue
|
|
168
|
+
* mechanism most needs to handle) unless that project already has an
|
|
169
|
+
* AR_ROOT/projects/<slug> directory — so all three consumers share this
|
|
170
|
+
* lighter, uniform heuristic instead. Every candidate is still checked
|
|
171
|
+
* against `isValidProjectSlug` (the SAME gate F1 itself applies) so a
|
|
172
|
+
* deny-listed/UUID-shaped/otherwise-invalid cwd segment is never selected.
|
|
173
|
+
* Returns `null` when no line's `cwd` matches the pattern, or when every
|
|
174
|
+
* match was invalid (caller falls back to a literal `"auto"`, the existing
|
|
175
|
+
* convention for an unresolved slug elsewhere in this codebase).
|
|
176
|
+
*
|
|
177
|
+
* H1 fix (review, post-build): F1 (`resolveSessionProject`, transcript-reader.ts)
|
|
178
|
+
* scans its FULL ranked candidate list and lets the first one that already
|
|
179
|
+
* has an on-disk project home win outright ("prefer an existing slug" —
|
|
180
|
+
* Signal 3), even over a noisier top-ranked candidate. The original version
|
|
181
|
+
* of this function had no such tie-break — pure cwd-count majority — so a WM
|
|
182
|
+
* session that happened to visit an unrelated-but-more-frequent cwd (e.g. a
|
|
183
|
+
* quick `cd` into a sibling checkout mid-session) could out-vote the project
|
|
184
|
+
* the session actually belongs to, where F1 itself would have picked the
|
|
185
|
+
* established one. Mirrors F1's tie-break using core's OWN project listing
|
|
186
|
+
* (`listAllProjects`, this same module — no cli import, preserving the
|
|
187
|
+
* core→cli layering direction documented above) instead of duplicating a raw
|
|
188
|
+
* `fs.readdirSync` scan. Wrapped in its own try/catch: this function must
|
|
189
|
+
* never throw (same contract as every other WM helper), and `listAllProjects`
|
|
190
|
+
* is a filesystem read this module does not otherwise need to guard.
|
|
191
|
+
*/
|
|
192
|
+
export declare function guessSlugFromWmLines(lines: WorkingMemoryLine[]): string | null;
|
|
193
|
+
/**
|
|
194
|
+
* C-2 (Train C, 2026-08-12 wave, design doc reports/2026-08-12-trainc-design.md)
|
|
195
|
+
* — the working-memory orphan-rescue sweep, single-sourced.
|
|
196
|
+
*
|
|
197
|
+
* WHY: through the v3.4.42 working-memory wave this sweep lived ONLY inside
|
|
198
|
+
* the CLI's `hook-start` case (packages/cli/src/index.ts) — a session that
|
|
199
|
+
* crashed on a host with no Claude Code hooks (Codex/Cursor/raw MCP) had no
|
|
200
|
+
* path back to a card at all, because nothing but that one CLI command ever
|
|
201
|
+
* ran the sweep. Doctrine (owner, 2026-07-26): the customer's only action is
|
|
202
|
+
* describing intent — the MCP server process itself must carry the memory
|
|
203
|
+
* lifecycle on hook-less hosts. Moved here, byte-for-byte behavior preserved,
|
|
204
|
+
* so BOTH callers — the CLI's `hook-start` case AND core's own `sessionStart()`
|
|
205
|
+
* (tools-logic/session-start.ts, called by the MCP `session_start` tool on
|
|
206
|
+
* every host) — invoke this ONE function. Neither re-implements any part of
|
|
207
|
+
* the sweep; there is exactly one place this logic can drift.
|
|
208
|
+
*
|
|
209
|
+
* Mechanism (unchanged from the original CLI-only version): a working-memory
|
|
210
|
+
* file older than `WM_ORPHAN_WINDOW_MS` with no session card and no
|
|
211
|
+
* recency-index entry yet is presumed to belong to a session that crashed or
|
|
212
|
+
* was `kill -9`'d without ever reaching hook-end (or, on a non-hook host,
|
|
213
|
+
* without the agent ever calling `session_end`). Rescues it into a
|
|
214
|
+
* searchable session card + recency entry, then deletes the WM file.
|
|
215
|
+
* Idempotent (card-exists / recency-exists guards — safe to call on every
|
|
216
|
+
* hook-start AND every session_start, from every open window/process),
|
|
217
|
+
* per-file fault-isolated (one bad orphan can never abort the sweep for its
|
|
218
|
+
* siblings — M1 fix, see the recency-append verification below), and NEVER
|
|
219
|
+
* throws: every failure is reported via `recordHookFailure` instead of
|
|
220
|
+
* propagating, matching every other WM helper's contract in this module.
|
|
221
|
+
*
|
|
222
|
+
* Renamed failure-record hook keys from the CLI original ("hook-start-wm-rescue"
|
|
223
|
+
* -> "wm-orphan-rescue", "hook-start-wm-rescue-recency" -> "wm-orphan-rescue-
|
|
224
|
+
* recency") — cosmetic only, no test or renderer asserts the literal string —
|
|
225
|
+
* because the sweep is no longer CLI-hook-specific; the old names would be
|
|
226
|
+
* actively misleading once an MCP `session_start` call is what triggered a
|
|
227
|
+
* given failure row.
|
|
228
|
+
*
|
|
229
|
+
* The per-file WM→card mechanism itself lives in `distillOneSession` below —
|
|
230
|
+
* factored out so C-3's graceful-exit handler (mcp-server/src/lib/
|
|
231
|
+
* lifecycle-exit.ts, via `distillSessionToCard`) can distill its OWN,
|
|
232
|
+
* still-fresh session immediately (no age gate) through the exact same
|
|
233
|
+
* mechanism this sweep uses for aged orphans, instead of a third
|
|
234
|
+
* reimplementation of "WM lines -> card + recency entry -> delete WM".
|
|
235
|
+
*/
|
|
236
|
+
export declare function rescueOrphanedWorkingMemory(): void;
|
|
237
|
+
/**
|
|
238
|
+
* C-3 (Train C, 2026-08-12 wave) — best-effort freshness for a graceful
|
|
239
|
+
* client close. `mcp-server/src/lib/lifecycle-exit.ts` calls this for the
|
|
240
|
+
* CURRENT process's own `sid` (core's `getSessionId()`) on `stdin` "end"/
|
|
241
|
+
* "close" and on SIGTERM/SIGINT, so a graceful disconnect gets a card
|
|
242
|
+
* immediately instead of waiting up to `WM_ORPHAN_WINDOW_MS` for the NEXT
|
|
243
|
+
* session's orphan sweep to find it. Reuses `distillOneSession` — the SAME
|
|
244
|
+
* WM→card mechanism `rescueOrphanedWorkingMemory` uses for aged orphans —
|
|
245
|
+
* with no age gate, since this is called precisely because the session is
|
|
246
|
+
* ending RIGHT NOW, not because it went stale. A `kill -9` bypasses this
|
|
247
|
+
* entirely by construction (no signal, no stdin event ever fires) and falls
|
|
248
|
+
* through to `rescueOrphanedWorkingMemory` on a LATER session_start/hook-start,
|
|
249
|
+
* exactly as the design doc specifies.
|
|
250
|
+
*
|
|
251
|
+
* No-op (silent) when this session has no working-memory file at all — a
|
|
252
|
+
* session that never had a tool call captured (or whose WM was already
|
|
253
|
+
* cleaned up by a normal `session_end`) has nothing to distill. Never
|
|
254
|
+
* throws: reported via `recordHookFailure` like every other WM helper.
|
|
255
|
+
*/
|
|
256
|
+
export declare function distillSessionToCard(sid: string): void;
|
|
257
|
+
//# sourceMappingURL=working-memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"working-memory.d.ts","sourceRoot":"","sources":["../../src/storage/working-memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAmBH,0EAA0E;AAC1E,eAAO,MAAM,WAAW,OAAO,CAAC;AAChC,iEAAiE;AACjE,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,QAAqB,CAAC;AAEpD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,QAAiB,CAAC;AAElD,MAAM,WAAW,iBAAiB;IAChC,wCAAwC;IACxC,EAAE,EAAE,MAAM,CAAC;IACX,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,qBAAqB;IACpC,+EAA+E;IAC/E,GAAG,EAAE,MAAM,CAAC;IACZ,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