@howaboua/pi-notes-compaction 0.0.13 → 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.
Files changed (43) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/PCC-INTEGRATION.md +3 -1
  3. package/README.md +3 -1
  4. package/package.json +7 -7
  5. package/src/agent-identity.ts +112 -10
  6. package/src/attachment-sharing.ts +37 -6
  7. package/src/attachment-snapshot.ts +16 -6
  8. package/src/bridge.ts +1 -0
  9. package/src/context-sharing.ts +30 -1
  10. package/src/extension-hooks.ts +15 -2
  11. package/src/extension-sharing.ts +26 -11
  12. package/src/fresh-checkpoint.ts +30 -4
  13. package/src/fresh-notes.ts +44 -33
  14. package/src/history-search.ts +1 -1
  15. package/src/history-windows.ts +12 -2
  16. package/src/history.ts +7 -1
  17. package/src/index.ts +3 -0
  18. package/src/integrations.ts +24 -24
  19. package/src/model-roles.ts +28 -4
  20. package/src/note-result.ts +70 -0
  21. package/src/note-save-marker.ts +23 -9
  22. package/src/notes-lookup.ts +5 -2
  23. package/src/notes.ts +8 -3
  24. package/src/query-executor.ts +58 -19
  25. package/src/query-input.ts +12 -1
  26. package/src/remote.ts +26 -7
  27. package/src/saved-session-access.ts +117 -0
  28. package/src/saved-session-index.mjs +83 -0
  29. package/src/saved-session.mjs +241 -0
  30. package/src/saved-session.ts +37 -154
  31. package/src/saved-sharing-records.ts +41 -9
  32. package/src/shared-notes.ts +219 -69
  33. package/src/shared-session-reader.ts +35 -19
  34. package/src/sharing-service.ts +57 -9
  35. package/src/store.ts +10 -0
  36. package/src/task-boundary.ts +1 -1
  37. package/src/tool-contracts.ts +40 -27
  38. package/src/tools.ts +21 -7
  39. package/src/tree-archive-manifest.mjs +27 -0
  40. package/src/tree-archive.ts +6 -26
  41. package/src/window-message.ts +19 -8
  42. package/src/windows.ts +2 -2
  43. package/changelog.ts +0 -337
@@ -3,9 +3,15 @@ import type {
3
3
  ExtensionAPI,
4
4
  ExtensionContext,
5
5
  } from "@earendil-works/pi-coding-agent";
6
+ import {
7
+ contextAgentIdentity,
8
+ contextAgentNames,
9
+ contextStorageAgentName,
10
+ } from "./agent-identity.js";
6
11
  import { useHistory } from "./history.js";
7
12
  import type { HistorySearch } from "./history-search.js";
8
13
  import type { NotesLifecycle } from "./lifecycle.js";
14
+ import { noteResult } from "./note-result.js";
9
15
  import { collectNotes, normalizeNoteParams, useNotes } from "./notes.js";
10
16
  import { lookupNotes } from "./notes-lookup.js";
11
17
  import {
@@ -42,11 +48,26 @@ export function createQueryExecutor(
42
48
  ctx: ExtensionContext,
43
49
  ): Promise<AgentToolResult<unknown>> {
44
50
  input = prepareNotesArguments(input);
45
- const help = queryHelp("notes", input, false, lifecycle.gptPromptMode());
51
+ const help = queryHelp("notes", input, lifecycle.gptPromptMode());
46
52
  if (help) return help;
47
53
  assertActive();
48
54
  const raw = queryArguments("notes", input);
49
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
+ }
50
71
  try {
51
72
  const routed = await lifecycle.bridge?.route?.(
52
73
  { namespace: "notes", params: raw },
@@ -57,17 +78,27 @@ export function createQueryExecutor(
57
78
  if (routed) return routed;
58
79
  } catch (error) {
59
80
  signal?.throwIfAborted();
60
- if (!sharedNotesUnavailable(error)) throw error;
61
- const saved = await readSharedNotes(ctx, store, raw, signal);
62
- if (saved) return result(saved);
63
- throw error;
81
+ if (!unavailable || !sharedNotesUnavailable(error)) throw error;
82
+ throw unavailable;
64
83
  }
65
- const saved = await readSharedNotes(ctx, store, raw, signal);
66
- if (saved) return result(saved);
84
+ if (unavailable) throw unavailable;
85
+ }
86
+ const identity = contextAgentIdentity(ctx);
87
+ const storageAgent = contextStorageAgentName(identity);
88
+ const localRaw = { ...raw };
89
+ for (const key of ["path", "prefix", "path_prefix"]) {
90
+ const value = localRaw[key];
91
+ if (typeof value !== "string") continue;
92
+ const alias = contextAgentNames(identity).find(
93
+ (name) =>
94
+ value === `${name}/notes` || value.startsWith(`${name}/notes/`),
95
+ );
96
+ if (alias)
97
+ localRaw[key] = `${identity.agentName}${value.slice(alias.length)}`;
67
98
  }
68
99
  const params = queryArguments(
69
100
  "notes",
70
- normalizeNoteParams(raw, lifecycle.agent(ctx)),
101
+ normalizeNoteParams(localRaw, lifecycle.agent(ctx)),
71
102
  );
72
103
  if (signal?.aborted) throw new Error("Note operation cancelled");
73
104
  const local = useNotes(
@@ -85,7 +116,7 @@ export function createQueryExecutor(
85
116
  params["action"] === "append_to_file"
86
117
  )
87
118
  return {
88
- ...result(local),
119
+ ...noteResult(local),
89
120
  details: {
90
121
  ...(shared
91
122
  ? { codexHistoryNotes: {}, notesCompactionRouted: true }
@@ -103,7 +134,7 @@ export function createQueryExecutor(
103
134
  params["action"] === "read_file" &&
104
135
  notes.get(params["path"] as string)?.mode === "replace"
105
136
  )
106
- return result(local);
137
+ return noteResult(local);
107
138
  const prefix = String(params["prefix"] ?? params["path_prefix"] ?? "");
108
139
  const replacementsInScope =
109
140
  params["action"] !== "read_file" &&
@@ -114,7 +145,7 @@ export function createQueryExecutor(
114
145
  for (const field of ["path", "prefix", "path_prefix"])
115
146
  if (typeof remoteParams[field] === "string")
116
147
  remoteParams[field] =
117
- `${lifecycle.agent(ctx)}/notes${remoteParams[field] ? `/${remoteParams[field]}` : ""}`;
148
+ `${storageAgent}/notes${remoteParams[field] ? `/${remoteParams[field]}` : ""}`;
118
149
  return lookupNotes(
119
150
  pi,
120
151
  store,
@@ -135,12 +166,7 @@ export function createQueryExecutor(
135
166
  ctx: ExtensionContext,
136
167
  ): Promise<AgentToolResult<unknown>> {
137
168
  input = prepareHistoryArguments(input);
138
- const help = queryHelp(
139
- "history",
140
- input,
141
- search.isEnabled(ctx.sessionManager.getSessionId()),
142
- lifecycle.gptPromptMode(),
143
- );
169
+ const help = queryHelp("history", input, lifecycle.gptPromptMode());
144
170
  if (help) return help;
145
171
  assertActive();
146
172
  const params = queryArguments("history", input);
@@ -170,14 +196,27 @@ export function createQueryExecutor(
170
196
  search,
171
197
  params,
172
198
  signal,
199
+ lifecycle.bridge?.loadSavedSession,
173
200
  );
174
201
  if (saved) return result(saved);
175
202
  throw error;
176
203
  }
177
- 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
+ );
178
212
  if (saved) return result(saved);
179
213
  }
180
- if (params["agent_name"] && params["agent_name"] !== lifecycle.agent(ctx))
214
+ if (
215
+ params["agent_name"] &&
216
+ !contextAgentNames(contextAgentIdentity(ctx)).includes(
217
+ String(params["agent_name"]),
218
+ )
219
+ )
181
220
  throw new Error(
182
221
  "That agent's Pi session history is unavailable on this machine. Restore access to its saved JSONL session, or omit agent_name to search this session.",
183
222
  );
@@ -58,12 +58,21 @@ export function normalizeQueryInput(
58
58
  if (!input || typeof input !== "object" || Array.isArray(input))
59
59
  throw new Error("Supply an action object; use {} for help");
60
60
  const params = { ...input } as Record<string, unknown>;
61
+ if (
62
+ namespace === "history" &&
63
+ params["action"] === undefined &&
64
+ "query" in params
65
+ )
66
+ params["action"] = "search";
61
67
  const action =
62
68
  ACTIONS[namespace][String(params["action"])] ?? params["action"];
63
69
  if (action !== undefined) params["action"] = action;
64
70
  if (namespace === "notes") {
65
- if (action === "write" || action === "append")
71
+ alias(params, "address", "path");
72
+ if (action === "write" || action === "append") {
66
73
  alias(params, "content", "text");
74
+ alias(params, "body", "text");
75
+ }
67
76
  if (action === "list" || action === "search") {
68
77
  alias(params, "prefix", "path");
69
78
  alias(params, "path_prefix", "path");
@@ -76,6 +85,8 @@ export function normalizeQueryInput(
76
85
  alias(params, "max_results", "limit");
77
86
  }
78
87
  const used = PRESENTATION[`${namespace}:${String(action)}`] ?? [];
88
+ if (namespace === "history" && action !== "read")
89
+ delete params["offset_chars"];
79
90
  for (const hint of HINTS) if (!used.includes(hint)) delete params[hint];
80
91
  return params;
81
92
  }
package/src/remote.ts CHANGED
@@ -12,8 +12,11 @@ import {
12
12
  } from "@howaboua/pi-codex-conversion/code-mode";
13
13
  import {
14
14
  type ContextAgentIdentity,
15
+ canonicalContextIdentity,
15
16
  contextAgentIdentity,
17
+ contextStorageAgentName,
16
18
  contextTargetAgent,
19
+ isContextAgentName,
17
20
  } from "./agent-identity.js";
18
21
  import type { CachedResponse, LookupQuery } from "./bridge.js";
19
22
  import type {
@@ -21,6 +24,7 @@ import type {
21
24
  SharedContextRequest,
22
25
  SharedContextResult,
23
26
  } from "./context-sharing.js";
27
+ import { noteResult } from "./note-result.js";
24
28
 
25
29
  const ENDPOINTS = {
26
30
  list_files_by_prefix: [
@@ -97,7 +101,7 @@ function scope(
97
101
  return JSON.stringify({
98
102
  threadId: identity.threadId,
99
103
  sessionId: identity.sessionId,
100
- agentName: identity.agentName,
104
+ agentName: contextStorageAgentName(identity),
101
105
  accountScope: protectedAccountScope(accountId),
102
106
  baseUrl,
103
107
  });
@@ -154,6 +158,19 @@ async function lookup(
154
158
  target?: RemoteNoteReference,
155
159
  ): Promise<readonly CachedResponse[]> {
156
160
  const { action, args } = readQuery(query);
161
+ if (target) {
162
+ const identity = canonicalContextIdentity(target.identity);
163
+ for (const field of ["path", "prefix", "path_prefix"]) {
164
+ const value = args[field];
165
+ if (
166
+ typeof value === "string" &&
167
+ (value === `${identity.agentName}/notes` ||
168
+ value.startsWith(`${identity.agentName}/notes/`))
169
+ )
170
+ args[field] =
171
+ `${contextStorageAgentName(identity)}${value.slice(identity.agentName.length)}`;
172
+ }
173
+ }
157
174
  const auth = await authenticated(ctx);
158
175
  if (target) checkReference(target, auth.provider);
159
176
  const headers = auth.provider.headers;
@@ -173,8 +190,9 @@ async function lookup(
173
190
  ...args,
174
191
  context: {
175
192
  session_id: target?.identity.sessionId ?? auth.identity.sessionId,
176
- current_agent_name:
177
- target?.identity.agentName ?? auth.identity.agentName,
193
+ current_agent_name: contextStorageAgentName(
194
+ target?.identity ?? auth.identity,
195
+ ),
178
196
  },
179
197
  }),
180
198
  },
@@ -237,10 +255,11 @@ export async function executeRemoteAttachment(
237
255
  throw new Error(
238
256
  "History is unavailable from a saved remote notes attachment. Open its saved Pi JSONL session on this machine, then retry.",
239
257
  );
258
+ const identity = canonicalContextIdentity(reference.identity);
240
259
  if (
241
260
  request.sessionId !== reference.identity.sessionId ||
242
- (request.agentName !== reference.identity.agentName &&
243
- !request.agentName.startsWith(`${reference.identity.agentName}/`)) ||
261
+ (request.agentName !== identity.agentName &&
262
+ request.agentName !== contextStorageAgentName(identity)) ||
244
263
  contextTargetAgent(
245
264
  request.namespace,
246
265
  request.params,
@@ -259,7 +278,7 @@ export async function executeRemoteAttachment(
259
278
  reference,
260
279
  );
261
280
  return {
262
- content: [{ type: "text", text: "Saved remote read completed" }],
281
+ ...noteResult({ usage: "Saved remote read completed" }),
263
282
  details: {
264
283
  codexHistoryNotes: {},
265
284
  externalNotesResponses: responses.map(({ bytes, ...response }) => ({
@@ -303,7 +322,7 @@ async function projectNotesResponses(
303
322
  !provenance.sessionId ||
304
323
  !("agentName" in provenance) ||
305
324
  typeof provenance.agentName !== "string" ||
306
- !/^\/root(?:\/[a-zA-Z0-9_-]+)*$/.test(provenance.agentName)
325
+ !isContextAgentName(provenance.agentName)
307
326
  )
308
327
  throw new Error(
309
328
  "Shared protected contents belong to a different account or backend",
@@ -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
+ }