@howaboua/pi-notes-compaction 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,11 +1,16 @@
1
1
  # @howaboua/pi-notes-compaction
2
2
 
3
+ ## 0.0.15
4
+
5
+ - Shared local checkpoints remain readable after workers close. Saved family lookup no longer depends on a live parent when the saved session is accessible locally.
6
+ - Saved Notes and History reads are faster in sessions with large messages or tool results.
7
+ - Shared saved-session reads no longer fail when the active session appends new entries.
8
+
3
9
  ## 0.0.14
4
10
 
5
11
  - Requires Pi 1.5 or newer.
6
12
 
7
13
  Older checkpoints may require a fresh note save before rollover.
8
-
9
14
  - Notes results in Pi's built-in codemode are now objects, matching Code and Notebook. Remove `JSON.parse` around those results.
10
15
  - Agent references now derive from Pi session IDs, with existing shared-note paths retained.
11
16
  - Fixed rollover rejecting successful large saves and saves in parallel tool groups. The saved-notes indicator uses the same readiness check.
package/README.md CHANGED
@@ -24,7 +24,9 @@ Ranked search is off by default. It indexes user messages and visible assistant
24
24
 
25
25
  Explicit searches of tool history, a window containing live context or another agent's history use literal substring matching. Search results identify the matching mode. Listing and reading history still includes the full transcript, including ongoing context and tool activity.
26
26
 
27
- Previously shared local notes and history remain readable after the other agent closes or Shepherdr is removed. Notes follows the saved family relationships and attachment aliases without requiring PCC. Reads use the other session's saved branch and preserve its history IDs. When only a retained attachment snapshot is available, notes identify it as detached or potentially stale. Snapshots do not contain history. Cross-agent writes still require their live routing service. History reads Pi JSONL sessions locally or through a live shared agent, including agents on another machine. Without live access, make the saved session available locally.
27
+ Previously shared local notes and history remain readable from accessible saved sessions after the other agent closes or Shepherdr is removed. Notes follows saved family relationships and attachment aliases without requiring PCC or a live parent. Reads use the other session's selected branch and preserve its history IDs. When only a retained attachment snapshot is available, notes identify it as detached or potentially stale. Snapshots do not contain history. Cross-agent writes still require live routing.
28
+
29
+ Shepherdr 0.2.27 remains supported for live sharing, including across connected machines. Reading a closed agent's saved session on another machine requires a saved-session transport that this Shepherdr version does not provide. Resume the shared agent and reconnect its machine, or make its saved Pi JSONL session available locally. Saved reads do not replace hybrid lookup or its encrypted cache with local-only results. History uses Pi JSONL, never remote notes or cached responses.
28
30
 
29
31
  In Code Mode and Notebook, plaintext notes and history return ordinary JavaScript values. Encrypted notes return delivery receipts instead of exposing their contents to JavaScript.
30
32
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@howaboua/pi-notes-compaction",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -37,7 +37,7 @@
37
37
  "better-sqlite3": "13.0.3"
38
38
  },
39
39
  "devDependencies": {
40
- "@howaboua/pi-codex-conversion": "4.0.8",
40
+ "@howaboua/pi-codex-conversion": "4.0.9",
41
41
  "@types/better-sqlite3": "9.6.0"
42
42
  },
43
43
  "peerDependencies": {
@@ -45,7 +45,7 @@
45
45
  "@earendil-works/pi-agent-core": ">=1.0.4",
46
46
  "@earendil-works/pi-ai": ">=1.0.4",
47
47
  "@earendil-works/pi-tui": ">=1.0.4",
48
- "@howaboua/pi-codex-conversion": ">=4.0.8",
48
+ "@howaboua/pi-codex-conversion": ">=4.0.9",
49
49
  "typebox": "*"
50
50
  },
51
51
  "peerDependenciesMeta": {
@@ -55,6 +55,8 @@
55
55
  },
56
56
  "exports": {
57
57
  ".": "./src/index.ts",
58
- "./context-sharing": "./src/context-sharing.ts"
58
+ "./context-sharing": "./src/context-sharing.ts",
59
+ "./saved-session": "./src/saved-session.mjs",
60
+ "./saved-session-index": "./src/saved-session-index.mjs"
59
61
  }
60
62
  }
package/src/bridge.ts CHANGED
@@ -85,6 +85,7 @@ export interface NotesOwner {
85
85
 
86
86
  export interface NotesBridge {
87
87
  protocol: 1;
88
+ loadSavedSession?: import("./context-sharing.js").SavedSessionLoader;
88
89
  /** Preserve the owner's chronological entry while enabling developer-role delivery. */
89
90
  prepareReminder?(entry: CustomMessageEntryDraft): CustomMessageEntryDraft;
90
91
  lookup?(
@@ -46,6 +46,20 @@ export type ContextRouter = ((
46
46
  requiresRemoteScope?(ctx: ExtensionContext): boolean;
47
47
  };
48
48
 
49
+ export interface SavedSessionLocator {
50
+ machine: string;
51
+ sessionFile: string;
52
+ }
53
+
54
+ export type SavedSessionLoader = ((
55
+ locator: SavedSessionLocator,
56
+ history: boolean,
57
+ signal?: AbortSignal,
58
+ ) => Promise<unknown>) & {
59
+ /** Exact native-thread discovery only; returned locators are untrusted hints. */
60
+ find?(threadId: string, signal?: AbortSignal): Promise<unknown>;
61
+ };
62
+
49
63
  /** Remote checkpoints retain an authenticated address, never decrypted note contents. */
50
64
  export interface RemoteNoteReference {
51
65
  protocol: 1;
@@ -118,6 +132,7 @@ export interface ContextSharingService {
118
132
  signal?: AbortSignal,
119
133
  ): Promise<SharedContextResult>;
120
134
  registerRouter(router: ContextRouter): () => void;
135
+ registerSavedSessionLoader?(loader: SavedSessionLoader): () => void;
121
136
  }
122
137
 
123
138
  export const CONTEXT_SHARING_AVAILABLE =
@@ -48,6 +48,7 @@ export async function registerNotesSharing(
48
48
  const family = {
49
49
  ...remote,
50
50
  protocol: 1 as const,
51
+ loadSavedSession: route.loadSavedSession,
51
52
  agentName: (ctx: ExtensionContext) => contextAgentIdentity(ctx).agentName,
52
53
  route: async (
53
54
  query: import("./bridge.js").LookupQuery,
@@ -53,6 +53,21 @@ export function createQueryExecutor(
53
53
  assertActive();
54
54
  const raw = queryArguments("notes", input);
55
55
  if (!shared) {
56
+ let unavailable: unknown;
57
+ try {
58
+ const saved = await readSharedNotes(
59
+ ctx,
60
+ store,
61
+ raw,
62
+ signal,
63
+ lifecycle.bridge?.loadSavedSession,
64
+ );
65
+ if (saved) return noteResult(saved);
66
+ } catch (error) {
67
+ signal?.throwIfAborted();
68
+ if (!sharedNotesUnavailable(error)) throw error;
69
+ unavailable = error;
70
+ }
56
71
  try {
57
72
  const routed = await lifecycle.bridge?.route?.(
58
73
  { namespace: "notes", params: raw },
@@ -63,13 +78,10 @@ export function createQueryExecutor(
63
78
  if (routed) return routed;
64
79
  } catch (error) {
65
80
  signal?.throwIfAborted();
66
- if (!sharedNotesUnavailable(error)) throw error;
67
- const saved = await readSharedNotes(ctx, store, raw, signal);
68
- if (saved) return noteResult(saved);
69
- throw error;
81
+ if (!unavailable || !sharedNotesUnavailable(error)) throw error;
82
+ throw unavailable;
70
83
  }
71
- const saved = await readSharedNotes(ctx, store, raw, signal);
72
- if (saved) return noteResult(saved);
84
+ if (unavailable) throw unavailable;
73
85
  }
74
86
  const identity = contextAgentIdentity(ctx);
75
87
  const storageAgent = contextStorageAgentName(identity);
@@ -184,11 +196,19 @@ export function createQueryExecutor(
184
196
  search,
185
197
  params,
186
198
  signal,
199
+ lifecycle.bridge?.loadSavedSession,
187
200
  );
188
201
  if (saved) return result(saved);
189
202
  throw error;
190
203
  }
191
- const saved = await readSharedHistory(ctx, store, search, params, signal);
204
+ const saved = await readSharedHistory(
205
+ ctx,
206
+ store,
207
+ search,
208
+ params,
209
+ signal,
210
+ lifecycle.bridge?.loadSavedSession,
211
+ );
192
212
  if (saved) return result(saved);
193
213
  }
194
214
  if (
@@ -0,0 +1,117 @@
1
+ import { Type } from "typebox";
2
+ import { Check } from "typebox/value";
3
+ import type {
4
+ SavedSessionLoader,
5
+ SavedSessionLocator,
6
+ } from "./context-sharing.js";
7
+ import {
8
+ readSavedNotesSession,
9
+ type SavedNotesSession,
10
+ validateSavedNotesSession,
11
+ } from "./saved-session.js";
12
+ import { savedFamilyIdentity } from "./saved-sharing-records.js";
13
+ import type { NotesStore } from "./store.js";
14
+
15
+ const Locations = Type.Array(
16
+ Type.Object({
17
+ machine: Type.String({ minLength: 1 }),
18
+ sessionFile: Type.String({ minLength: 1 }),
19
+ }),
20
+ );
21
+
22
+ /** Local is relative to the session carrying the edge, not the machine running this reader. */
23
+ export function createSavedSessionAccess(
24
+ store: Pick<NotesStore, "sessionFile">,
25
+ namespace: "notes" | "history",
26
+ loader: SavedSessionLoader | undefined,
27
+ signal: AbortSignal | undefined,
28
+ ) {
29
+ const locations = new WeakMap<SavedNotesSession, SavedSessionLocator>();
30
+ const containingMachine = (session: SavedNotesSession) =>
31
+ locations.get(session)?.machine ?? "local";
32
+ function locator(
33
+ session: SavedNotesSession,
34
+ machine: string,
35
+ sessionFile: string,
36
+ ): SavedSessionLocator {
37
+ return {
38
+ machine: machine === "local" ? containingMachine(session) : machine,
39
+ sessionFile,
40
+ };
41
+ }
42
+ async function load(
43
+ location: SavedSessionLocator,
44
+ ): Promise<SavedNotesSession> {
45
+ signal?.throwIfAborted();
46
+ const session =
47
+ location.machine === "local"
48
+ ? await readSavedNotesSession(
49
+ location.sessionFile,
50
+ signal,
51
+ namespace === "history",
52
+ )
53
+ : loader
54
+ ? validateSavedNotesSession(
55
+ await loader(location, namespace === "history", signal),
56
+ )
57
+ : (() => {
58
+ throw Object.assign(
59
+ new Error(
60
+ "Saved shared sessions on that machine are unavailable. Resume the shared agent and reconnect its machine, or make its saved Pi JSONL session available locally.",
61
+ ),
62
+ { code: "SAVED_SESSION_UNAVAILABLE" },
63
+ );
64
+ })();
65
+ locations.set(session, location);
66
+ return session;
67
+ }
68
+
69
+ async function indexed(
70
+ current: SavedNotesSession,
71
+ target: string,
72
+ ): Promise<SavedNotesSession | undefined> {
73
+ // An exact local hint avoids dead relay parents. Index rows never establish membership.
74
+ const family = savedFamilyIdentity(current);
75
+ const threadId = target.slice(1);
76
+ if (family && !threadId.includes("/") && target !== "/root") {
77
+ const file = store.sessionFile(threadId);
78
+ if (file) {
79
+ const saved = await load({ machine: "local", sessionFile: file });
80
+ const member = savedFamilyIdentity(saved);
81
+ if (
82
+ saved.threadId !== threadId ||
83
+ !member ||
84
+ member.storage !== "session" ||
85
+ member.sessionId !== family.sessionId ||
86
+ member.agentName !== target
87
+ )
88
+ throw new Error(
89
+ "Saved shared notes belong to a different agent family",
90
+ );
91
+ return saved;
92
+ }
93
+ }
94
+ return;
95
+ }
96
+ async function find(target: string): Promise<SavedSessionLocator[]> {
97
+ const threadId = target.slice(1);
98
+ if (!loader?.find || target === "/root" || threadId.includes("/"))
99
+ return [];
100
+ signal?.throwIfAborted();
101
+ const value = await loader.find(threadId, signal);
102
+ signal?.throwIfAborted();
103
+ if (!Check(Locations, value))
104
+ throw new Error(
105
+ "Saved session discovery response is invalid. Update Notes on the other machine and retry.",
106
+ );
107
+ return [
108
+ ...new Map(
109
+ value.map((location) => [
110
+ `${location.machine}\0${location.sessionFile}`,
111
+ location,
112
+ ]),
113
+ ).values(),
114
+ ];
115
+ }
116
+ return { containingMachine, locator, load, indexed, find };
117
+ }
@@ -0,0 +1,83 @@
1
+ import { readdir, stat } from "node:fs/promises";
2
+ import { createRequire } from "node:module";
3
+ import { homedir } from "node:os";
4
+ import { join, resolve } from "node:path";
5
+
6
+ /** Exact path hints only. Neither the index nor a filename establishes Notes membership.
7
+ * Missing indexes are normal for old sessions; other index failures are reported.
8
+ * @param {string} threadId
9
+ * @param {string} [agentDir]
10
+ * @param {AbortSignal} [signal]
11
+ * @returns {Promise<string[]>}
12
+ */
13
+ export async function findSavedNotesSessions(threadId, agentDir, signal) {
14
+ signal?.throwIfAborted();
15
+ if (
16
+ !/^[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$/.test(threadId) ||
17
+ threadId === "root"
18
+ )
19
+ throw new Error("Invalid saved session thread ID");
20
+ const configured =
21
+ agentDir ??
22
+ process.env["PI_CODING_AGENT_DIR"] ??
23
+ join(homedir(), ".pi", "agent");
24
+ const dir =
25
+ configured === "~" || configured.startsWith("~/")
26
+ ? join(homedir(), configured.slice(2))
27
+ : resolve(configured);
28
+ const paths = new Set();
29
+ const index = join(dir, "notes-compaction", "notes.sqlite");
30
+ try {
31
+ await stat(index);
32
+ /** @type {import("node:sqlite").DatabaseSync} */
33
+ const db = process.versions["bun"]
34
+ ? new (createRequire(import.meta.url)("bun:sqlite").Database)(index, {
35
+ readonly: true,
36
+ })
37
+ : new (await import("node:sqlite")).DatabaseSync(index, {
38
+ readOnly: true,
39
+ });
40
+ try {
41
+ signal?.throwIfAborted();
42
+ const table = db
43
+ .prepare(
44
+ "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'sessions'",
45
+ )
46
+ .get();
47
+ if (table) {
48
+ const row = db
49
+ .prepare("SELECT file_path FROM sessions WHERE id = ?")
50
+ .get(threadId);
51
+ if (row && typeof row["file_path"] === "string" && row["file_path"])
52
+ paths.add(row["file_path"]);
53
+ }
54
+ } finally {
55
+ db.close();
56
+ }
57
+ } catch (error) {
58
+ signal?.throwIfAborted();
59
+ if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
60
+ throw new Error(
61
+ "Saved session index is unavailable. Restore access to the Notes index and retry.",
62
+ { cause: error },
63
+ );
64
+ }
65
+ const sessions = join(dir, "sessions");
66
+ try {
67
+ for (const cwd of await readdir(sessions, { withFileTypes: true })) {
68
+ signal?.throwIfAborted();
69
+ if (!cwd.isDirectory()) continue;
70
+ const directory = join(sessions, cwd.name);
71
+ for (const file of await readdir(directory, { withFileTypes: true })) {
72
+ signal?.throwIfAborted();
73
+ if (file.isFile() && file.name.endsWith(`_${threadId}.jsonl`))
74
+ paths.add(join(directory, file.name));
75
+ }
76
+ }
77
+ } catch (error) {
78
+ signal?.throwIfAborted();
79
+ if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
80
+ throw error;
81
+ }
82
+ return [...paths].sort();
83
+ }
@@ -0,0 +1,241 @@
1
+ import { createHash } from "node:crypto";
2
+ import { open, stat } from "node:fs/promises";
3
+ import {
4
+ isTreeArchiveManifestData,
5
+ MAX_ARCHIVE_DEPTH,
6
+ TREE_ARCHIVE_ENTRY_TYPE,
7
+ } from "./tree-archive-manifest.mjs";
8
+
9
+ /** @param {unknown} value @returns {value is Record<string, unknown>} */
10
+ function object(value) {
11
+ return value !== null && typeof value === "object" && !Array.isArray(value);
12
+ }
13
+ /** @param {unknown} value @returns {value is {type:"session",id:string}} */
14
+ function isSessionHeader(value) {
15
+ return (
16
+ object(value) &&
17
+ value["type"] === "session" &&
18
+ typeof value["id"] === "string"
19
+ );
20
+ }
21
+ /** @param {unknown} value @returns {value is import("@earendil-works/pi-coding-agent").SessionEntry} */
22
+ function isRecord(value) {
23
+ return (
24
+ object(value) &&
25
+ typeof value["type"] === "string" &&
26
+ typeof value["id"] === "string" &&
27
+ (value["parentId"] === null || typeof value["parentId"] === "string") &&
28
+ typeof value["timestamp"] === "string"
29
+ );
30
+ }
31
+ /** @param {unknown} value @returns {value is import("@earendil-works/pi-coding-agent").CustomEntry} */
32
+ function isCustom(value) {
33
+ return (
34
+ isRecord(value) &&
35
+ value.type === "custom" &&
36
+ typeof value.customType === "string" &&
37
+ "data" in value
38
+ );
39
+ }
40
+ /** @param {unknown} value @returns {value is import("@earendil-works/pi-coding-agent").BranchSummaryEntry} */
41
+ function isHandoff(value) {
42
+ return (
43
+ isRecord(value) &&
44
+ value.type === "branch_summary" &&
45
+ typeof value.fromId === "string" &&
46
+ typeof value.summary === "string" &&
47
+ object(value.details) &&
48
+ "codexContextNoteHandoff" in value.details
49
+ );
50
+ }
51
+ const NOTE_ENTRIES = new Set([
52
+ "notes-compaction:note:v1",
53
+ "notes-compaction:cache:v1",
54
+ "codex-context-note",
55
+ "codex-context-note-snapshot",
56
+ ]);
57
+ const SHARING_ENTRIES = new Set([
58
+ "codex-context-agent",
59
+ "codex-context-backend",
60
+ "herdr-context-child",
61
+ "shepherdr-attachment-route",
62
+ "shepherdr-attachment-owner",
63
+ "shepherdr-attachment-detached",
64
+ ]);
65
+
66
+ /** @param {import("node:fs/promises").FileHandle} file @param {number} size @param {AbortSignal} [signal] */
67
+ async function* reverseChunks(file, size, signal) {
68
+ let position = size;
69
+ while (position > 0) {
70
+ signal?.throwIfAborted();
71
+ const length = Math.min(64 * 1024, position);
72
+ position -= length;
73
+ const chunk = Buffer.allocUnsafe(length);
74
+ let offset = 0;
75
+ while (offset < length) {
76
+ const { bytesRead } = await file.read(
77
+ chunk,
78
+ offset,
79
+ length - offset,
80
+ position + offset,
81
+ );
82
+ if (!bytesRead)
83
+ throw new Error(
84
+ "Saved notes changed during the read. Retry the lookup.",
85
+ );
86
+ offset += bytesRead;
87
+ }
88
+ yield chunk;
89
+ }
90
+ }
91
+
92
+ /** @param {import("node:fs/promises").FileHandle} file @param {number} size @param {import("node:crypto").Hash} hash @param {AbortSignal} [signal] */
93
+ async function* reverseLines(file, size, hash, signal) {
94
+ /** @type {Buffer[]} */
95
+ let parts = [];
96
+ for await (const chunk of reverseChunks(file, size, signal)) {
97
+ hash.update(chunk);
98
+ let end = chunk.length;
99
+ for (let index = chunk.length - 1; index >= 0; index--) {
100
+ if (chunk[index] !== 10) continue;
101
+ const tail = chunk.subarray(index + 1, end);
102
+ // Long JSONL records span many chunks. Scan each byte once and join
103
+ // fragments only at the newline, rather than recopying a growing line.
104
+ if (parts.length) {
105
+ parts.push(tail);
106
+ yield Buffer.concat(parts.reverse());
107
+ parts = [];
108
+ } else yield tail;
109
+ end = index;
110
+ }
111
+ if (end) parts.push(chunk.subarray(0, end));
112
+ }
113
+ yield Buffer.concat(parts.reverse());
114
+ }
115
+
116
+ /** Read only recorded session paths. Identity is session-wide; notes follow the active parent chain. */
117
+ /** @param {string} path @param {AbortSignal} [signal] @param {boolean} [history] @returns {Promise<import("./saved-session.js").SavedNotesSession>} */
118
+ export async function readSavedNotesSession(path, signal, history = false) {
119
+ signal?.throwIfAborted();
120
+ const file = await open(path, "r");
121
+ try {
122
+ const before = await file.stat();
123
+ const hash = createHash("sha256");
124
+ /** @type {import("@earendil-works/pi-coding-agent").SessionEntry[]} */
125
+ const entries = [];
126
+ /** @type {import("@earendil-works/pi-coding-agent").SessionEntry[]} */
127
+ const branch = [];
128
+ /** @type {Map<string, {base:string|null,depth:number}[]>} */
129
+ const archived = new Map();
130
+ /** @type {string | undefined} */
131
+ let threadId;
132
+ /** @type {string | null | undefined} */
133
+ let target;
134
+ for await (const line of reverseLines(file, before.size, hash, signal)) {
135
+ signal?.throwIfAborted();
136
+ if (!line.length) continue;
137
+ /** @type {unknown} */
138
+ let entry;
139
+ try {
140
+ entry = JSON.parse(line.toString("utf8"));
141
+ } catch {
142
+ throw new Error(
143
+ "Saved notes are being updated. Retry after the session settles.",
144
+ );
145
+ }
146
+ if (isSessionHeader(entry)) {
147
+ threadId = entry.id;
148
+ continue;
149
+ }
150
+ if (!isRecord(entry)) throw new Error("Invalid saved session record");
151
+ if (target === undefined) target = entry.id;
152
+ const active = entry.id === target;
153
+ if (active) target = entry.parentId;
154
+ if (history) {
155
+ // Pi appends descendants after their parents, including Tree manifests
156
+ // after their archived leaves. Follow only active manifests' ancestry.
157
+ const paths = archived.get(entry.id) ?? [];
158
+ archived.delete(entry.id);
159
+ for (const path of paths) {
160
+ if (++path.depth >= MAX_ARCHIVE_DEPTH || entry.parentId === path.base)
161
+ continue;
162
+ if (entry.parentId !== null) {
163
+ const parents = archived.get(entry.parentId) ?? [];
164
+ parents.push(path);
165
+ archived.set(entry.parentId, parents);
166
+ }
167
+ }
168
+ if (
169
+ active &&
170
+ isCustom(entry) &&
171
+ entry.customType === TREE_ARCHIVE_ENTRY_TYPE &&
172
+ isTreeArchiveManifestData(entry.data) &&
173
+ entry.data.archivedLeafId !== entry.data.branchBaseId
174
+ ) {
175
+ const paths = archived.get(entry.data.archivedLeafId) ?? [];
176
+ paths.push({ base: entry.data.branchBaseId, depth: 0 });
177
+ archived.set(entry.data.archivedLeafId, paths);
178
+ }
179
+ if (
180
+ active ||
181
+ paths.length ||
182
+ (isCustom(entry) && SHARING_ENTRIES.has(entry.customType))
183
+ )
184
+ entries.push(entry);
185
+ if (active) branch.push(entry);
186
+ continue;
187
+ }
188
+ if (isCustom(entry)) {
189
+ if (SHARING_ENTRIES.has(entry.customType)) entries.push(entry);
190
+ else if (active && NOTE_ENTRIES.has(entry.customType))
191
+ branch.push(entry);
192
+ else continue;
193
+ } else if (active && isHandoff(entry)) branch.push(entry);
194
+ else continue;
195
+ }
196
+ // Pruning replaces the path. An open descriptor alone still sees the old inode.
197
+ const after = await stat(path);
198
+ if (
199
+ after.dev !== before.dev ||
200
+ after.ino !== before.ino ||
201
+ after.size < before.size ||
202
+ (after.size === before.size &&
203
+ (after.mtimeMs !== before.mtimeMs || after.ctimeMs !== before.ctimeMs))
204
+ )
205
+ throw new Error("Saved notes changed during the read. Retry the lookup.");
206
+ if (after.size > before.size) {
207
+ // Flushed Pi sessions append. Keep the original leaf/byte boundary,
208
+ // but prove its prefix did not change, including abandoned identities.
209
+ // Hash the same reverse chunks, without retaining the whole JSONL.
210
+ const verified = createHash("sha256");
211
+ for await (const chunk of reverseChunks(file, before.size, signal))
212
+ verified.update(chunk);
213
+ if (hash.digest("hex") !== verified.digest("hex"))
214
+ throw new Error(
215
+ "Saved notes changed during the read. Retry the lookup.",
216
+ );
217
+ const current = await stat(path);
218
+ if (
219
+ current.dev !== before.dev ||
220
+ current.ino !== before.ino ||
221
+ current.size < after.size ||
222
+ (current.size === after.size &&
223
+ (current.mtimeMs !== after.mtimeMs ||
224
+ current.ctimeMs !== after.ctimeMs))
225
+ )
226
+ throw new Error(
227
+ "Saved notes changed during the read. Retry the lookup.",
228
+ );
229
+ }
230
+ if (!threadId || (target !== undefined && target !== null))
231
+ throw new Error("Saved notes have an incomplete session branch");
232
+ return {
233
+ threadId,
234
+ entries: entries.reverse(),
235
+ branch: branch.reverse(),
236
+ savedAt: before.mtimeMs,
237
+ };
238
+ } finally {
239
+ await file.close();
240
+ }
241
+ }