@astrosheep/pi-context 0.19.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/src/budget.js +65 -0
  2. package/dist/src/dream/cli.js +83 -0
  3. package/dist/src/dream/gates.js +22 -0
  4. package/dist/src/dream/git.js +28 -0
  5. package/dist/src/dream/lock.js +58 -0
  6. package/dist/src/dream/runner.js +115 -0
  7. package/dist/src/history-tools.js +105 -0
  8. package/dist/src/history.js +215 -0
  9. package/dist/src/index.js +98 -0
  10. package/dist/src/notes/address.js +31 -0
  11. package/dist/src/notes/frontmatter.js +136 -0
  12. package/dist/src/notes/model.js +101 -0
  13. package/dist/src/notes/paths.js +58 -0
  14. package/dist/src/notes/store.js +270 -0
  15. package/dist/src/notes/tools.js +153 -0
  16. package/dist/src/prompts.js +81 -0
  17. package/dist/src/protocol.js +56 -0
  18. package/dist/src/reset-lifecycle.js +101 -0
  19. package/dist/src/session-reader.js +1 -0
  20. package/dist/src/thresholds.js +75 -0
  21. package/dist/src/tool-output.js +175 -0
  22. package/dist/src/tool-schema.js +26 -0
  23. package/dist/src/warning.js +44 -0
  24. package/dist/test/agent-loop.test.js +214 -0
  25. package/dist/test/coherence.test.js +375 -0
  26. package/dist/test/dream.test.js +142 -0
  27. package/dist/test/history.test.js +26 -0
  28. package/dist/test/integration.test.js +1766 -0
  29. package/dist/test/notes.test.js +474 -0
  30. package/dist/test/pagination.property.test.js +476 -0
  31. package/dist/test/reset-lifecycle.test.js +199 -0
  32. package/package.json +13 -7
  33. package/playbook.md +32 -0
  34. package/src/budget.ts +11 -9
  35. package/src/dream/cli.ts +33 -0
  36. package/src/dream/gates.ts +20 -0
  37. package/src/dream/git.ts +27 -0
  38. package/src/dream/lock.ts +39 -0
  39. package/src/dream/runner.ts +111 -0
  40. package/src/history-tools.ts +5 -5
  41. package/src/history.ts +12 -7
  42. package/src/index.ts +13 -14
  43. package/src/notes/address.ts +33 -0
  44. package/src/{memory → notes}/frontmatter.ts +5 -3
  45. package/src/{notes.ts → notes/model.ts} +2 -2
  46. package/src/{memory → notes}/paths.ts +6 -1
  47. package/src/{memory → notes}/store.ts +62 -77
  48. package/src/notes/tools.ts +132 -0
  49. package/src/prompts.ts +31 -29
  50. package/src/protocol.ts +9 -5
  51. package/src/thresholds.ts +4 -1
  52. package/src/tool-output.ts +4 -1
  53. package/src/warning.ts +3 -3
  54. package/src/memory/tools.ts +0 -166
@@ -3,10 +3,12 @@ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, w
3
3
  import { dirname } from "node:path";
4
4
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
5
5
  import { generateDiffString } from "@earendil-works/pi-coding-agent";
6
- import { assertGlobPattern, assertVirtualPath, globToRegExp } from "../notes.js";
6
+ import { assertGlobPattern, assertVirtualPath, globToRegExp } from "./model.js";
7
7
  import { MAX_NOTE_BYTES, MAX_NOTE_PATH_BYTES } from "../protocol.js";
8
8
  import { isOrigin, isScope, parseNote, serializeNote, stripLeadingFrontmatter, type NoteMeta, type Origin } from "./frontmatter.js";
9
+ import { addressFor } from "./address.js";
9
10
  import { physicalPath, scopeDir, type Scope } from "./paths.js";
11
+ import { earliestMatchOffsetChars } from "../tool-output.js";
10
12
 
11
13
  export type { NoteMeta, Origin, Scope };
12
14
 
@@ -26,9 +28,9 @@ export class NoteError extends Error {
26
28
  }
27
29
  }
28
30
 
29
- export type NoteRow = { path: string; meta: NoteMeta; sizeBytes: number };
31
+ export type NoteRow = { address: string; scope: Scope; path: string; meta: NoteMeta; body: string; sizeBytes: number };
30
32
  export type NoteMatch = { line: number; text: string; offsetChars: number };
31
- export type NoteSearchRow = { path: string; scope: Scope; meta: NoteMeta; matches: NoteMatch[] };
33
+ export type NoteSearchRow = { address: string; scope: Scope; path: string; meta: NoteMeta; matches: NoteMatch[] };
32
34
 
33
35
  const SCOPE_ORDER: readonly Scope[] = ["session", "project", "global"];
34
36
 
@@ -42,22 +44,6 @@ function assertOrigin(value: unknown): Origin {
42
44
  return value;
43
45
  }
44
46
 
45
- function scopeList(scope?: unknown): Scope[] {
46
- if (scope === undefined || scope === null) return [...SCOPE_ORDER];
47
- return [assertScope(scope)];
48
- }
49
-
50
- type Resolved = { scope: Scope; path: string; raw: string };
51
-
52
- /** First existing file by precedence session → project → global, or only `scope` when given. */
53
- function resolve(ctx: ExtensionContext, vpath: string, scope?: unknown): Resolved | undefined {
54
- for (const candidate of scopeList(scope)) {
55
- const path = physicalPath(candidate, vpath, ctx);
56
- if (existsSync(path)) return { scope: candidate, path, raw: readFileSync(path, "utf8") };
57
- }
58
- return undefined;
59
- }
60
-
61
47
  /** Recursively list `.md` files under `dir` as forward-slash virtual paths relative to `base`. */
62
48
  function walkMarkdown(dir: string, base = dir): string[] {
63
49
  let entries: Dirent[];
@@ -160,19 +146,36 @@ export function writeNote(ctx: ExtensionContext, vpath: string, body: string, op
160
146
  }
161
147
 
162
148
  export type EditOperation = { oldText: string; newText: string };
163
- export type EditOptions = { scope?: Scope; origin?: Origin; stale?: boolean; replaceAll?: boolean };
149
+ export type EditOptions = { origin?: Origin; stale?: boolean; replaceAll?: boolean };
150
+
151
+ /** Dream harness mutation: metadata changes still use the store's atomic writer. */
152
+ export function updateNoteMeta(ctx: ExtensionContext, vpath: string, scope: Scope, mutate: (meta: NoteMeta) => void): { meta: NoteMeta; body: string } {
153
+ assertVirtualPath(vpath);
154
+ const path = physicalPath(scope, vpath, ctx);
155
+ if (!existsSync(path)) throw new NoteError("not_found", `note not found: ${vpath}`);
156
+ const parsed = parseNote(readFileSync(path, "utf8"));
157
+ const meta = { ...parsed.meta, scope };
158
+ mutate(meta);
159
+ meta.updated_at = Date.now();
160
+ const serialized = serializeNote(meta, parsed.body);
161
+ assertSerializedSize(serialized);
162
+ atomicWrite(path, serialized);
163
+ return { meta, body: parsed.body };
164
+ }
164
165
 
165
- /** Apply body-only edits against one snapshot, then optionally move via the scope/origin/stale setters. */
166
- export function editNote(ctx: ExtensionContext, vpath: string, edits: EditOperation[] | undefined, opts: EditOptions = {}): { meta: NoteMeta; applied: number; resolved_scope: Scope; diff: string } {
166
+ /** Apply body-only edits against one explicit home; origin and stale are its metadata setters. */
167
+ export function editNote(ctx: ExtensionContext, vpath: string, scope: Scope, edits: EditOperation[] | undefined, opts: EditOptions = {}): { meta: NoteMeta; applied: number; resolved_scope: Scope; diff: string } {
167
168
  assertVirtualPath(vpath);
168
169
  assertWritablePath(vpath);
169
170
  const operations = edits ?? [];
170
- if (operations.length === 0 && opts.scope === undefined && opts.origin === undefined && opts.stale === undefined) {
171
- throw new NoteError("nothing_to_do", "nothing to do: provide edits or at least one of scope, origin, stale");
171
+ if (operations.length === 0 && opts.origin === undefined && opts.stale === undefined) {
172
+ throw new NoteError("nothing_to_do", "nothing to do: provide edits or at least one of origin, stale");
172
173
  }
173
- const found = resolve(ctx, vpath);
174
- if (!found) throw new NoteError("not_found", "note not found");
175
- const { meta, body } = parseNote(found.raw);
174
+ const path = physicalPath(scope, vpath, ctx);
175
+ if (!existsSync(path)) throw new NoteError("not_found", "note not found");
176
+ const raw = readFileSync(path, "utf8");
177
+ const { meta, body } = parseNote(raw);
178
+ meta.scope = scope;
176
179
  // Snapshot the pre-edit frontmatter so the diff can name exactly what the setters changed.
177
180
  const beforeMeta: NoteMeta = { ...meta };
178
181
  // Every edit runs against this one snapshot; nothing is written until all of them succeed,
@@ -188,79 +191,65 @@ export function editNote(ctx: ExtensionContext, vpath: string, edits: EditOperat
188
191
  if (lines.length > 1 && !opts.replaceAll) {
189
192
  throw new NoteError("ambiguous_edit", `edit ${index}: oldText occurs ${lines.length} times (lines ${lines.join(", ")}); pass replace_all to replace every occurrence`, { line_numbers: lines, edit_index: index });
190
193
  }
191
- next = opts.replaceAll ? next.split(oldText).join(newText) : next.replace(oldText, newText);
194
+ // Single replacement is positional splicing, never String.replace: user text must be
195
+ // inserted byte-for-byte, without $-pattern substitution ($&, $`, $', $1, $$).
196
+ if (opts.replaceAll) {
197
+ next = next.split(oldText).join(newText);
198
+ } else {
199
+ const matchIndex = next.indexOf(oldText);
200
+ next = next.substring(0, matchIndex) + newText + next.substring(matchIndex + oldText.length);
201
+ }
192
202
  });
193
- const destScope = opts.scope === undefined ? found.scope : assertScope(opts.scope);
194
203
  if (opts.origin !== undefined) meta.origin = assertOrigin(opts.origin);
195
204
  if (opts.stale !== undefined) meta.stale = opts.stale;
196
- meta.scope = destScope;
197
205
  meta.updated_at = Date.now();
198
- const dest = physicalPath(destScope, vpath, ctx);
199
- const moving = dest !== found.path;
200
- if (moving && existsSync(dest)) {
201
- throw new NoteError("target_exists", `a note already exists at ${vpath} in scope ${destScope}; the move was refused and both files are unchanged`);
202
- }
203
206
  const serialized = serializeNote(meta, next);
204
207
  assertSerializedSize(serialized);
205
208
  // pi-edit-style diff: body only for a content edit, frontmatter only for a metadata-only
206
- // update, one combined file diff when both moved.
209
+ // update, one combined file diff when both change.
207
210
  const bodyChanged = body !== next;
208
- const metadataChanged = beforeMeta.scope !== meta.scope || beforeMeta.origin !== meta.origin || beforeMeta.stale !== meta.stale;
211
+ const metadataChanged = beforeMeta.origin !== meta.origin || beforeMeta.stale !== meta.stale;
209
212
  const diff = bodyChanged && metadataChanged
210
- ? generateDiffString(found.raw, serialized).diff
213
+ ? generateDiffString(raw, serialized).diff
211
214
  : bodyChanged
212
215
  ? generateDiffString(body, next).diff
213
216
  : metadataChanged
214
217
  ? generateDiffString(frontmatterOf(beforeMeta), frontmatterOf(meta)).diff
215
218
  : "";
216
- atomicWrite(dest, serialized);
217
- if (moving) rmSync(found.path);
218
- return { meta, applied: operations.length, resolved_scope: found.scope, diff };
219
+ atomicWrite(path, serialized);
220
+ return { meta, applied: operations.length, resolved_scope: scope, diff };
219
221
  }
220
222
 
221
223
  /** Read a note and, as a side effect, bump last_accessed/access_count in the file. */
222
- export function readNote(ctx: ExtensionContext, vpath: string, opts: { scope?: Scope } = {}): { meta: NoteMeta; body: string; resolvedScope: Scope } | undefined {
224
+ export function readNote(ctx: ExtensionContext, vpath: string, scope: Scope): { meta: NoteMeta; body: string; resolvedScope: Scope } | undefined {
223
225
  assertVirtualPath(vpath);
224
- const found = resolve(ctx, vpath, opts.scope);
225
- if (!found) return undefined;
226
+ const path = physicalPath(scope, vpath, ctx);
227
+ if (!existsSync(path)) return undefined;
226
228
  const now = Date.now();
227
- const { meta, body } = parseNote(found.raw, now);
228
- meta.scope = found.scope;
229
+ const { meta, body } = parseNote(readFileSync(path, "utf8"), now);
230
+ meta.scope = scope;
229
231
  // Only the two access keys move; updated_at and every other key keep their bytes.
230
232
  meta.last_accessed = now;
231
233
  meta.access_count = (typeof meta.access_count === "number" ? meta.access_count : 0) + 1;
232
- atomicWrite(found.path, serializeNote(meta, body));
233
- return { meta, body, resolvedScope: found.scope };
234
- }
235
-
236
- /** The scope that holds `vpath` first by precedence, without reading or mutating the file. */
237
- export function resolveNoteScope(ctx: ExtensionContext, vpath: string, scope?: Scope): { scope: Scope; path: string } | undefined {
238
- const found = resolve(ctx, vpath, scope);
239
- return found ? { scope: found.scope, path: found.path } : undefined;
240
- }
241
-
242
- /** Read a note's meta and body without the read side effect (used by the boot index). */
243
- export function peekNote(ctx: ExtensionContext, scope: Scope, vpath: string): { meta: NoteMeta; body: string } {
244
- const path = physicalPath(scope, vpath, ctx);
245
- const { meta, body } = parseNote(readFileSync(path, "utf8"));
246
- meta.scope = scope;
247
- return { meta, body };
234
+ atomicWrite(path, serializeNote(meta, body));
235
+ return { meta, body, resolvedScope: scope };
248
236
  }
249
237
 
250
- /** Merged rows across scopes, most recently updated first (path then scope break ties). */
238
+ /** Merged rows across homes, most recently updated first (address breaks ties). */
251
239
  export function listNotes(ctx: ExtensionContext, opts: { scope?: Scope; pattern?: string } = {}): NoteRow[] {
252
240
  const matcher = matcherFor(opts.pattern);
253
241
  const rows: NoteRow[] = [];
254
- for (const scope of scopeList(opts.scope)) {
242
+ for (const scope of opts.scope === undefined ? SCOPE_ORDER : [opts.scope]) {
255
243
  const root = scopeDir(scope, ctx);
256
244
  for (const path of walkMarkdown(root)) {
257
- if (matcher && !matcher.test(path)) continue;
245
+ const address = addressFor(scope, path);
246
+ if (matcher && !matcher.test(address)) continue;
258
247
  const { meta, body } = parseNote(readFileSync(`${root}/${path}`, "utf8"));
259
248
  meta.scope = scope;
260
- rows.push({ path, meta, sizeBytes: Buffer.byteLength(body, "utf8") });
249
+ rows.push({ address, scope, path, meta, body, sizeBytes: Buffer.byteLength(body, "utf8") });
261
250
  }
262
251
  }
263
- rows.sort((a, b) => b.meta.updated_at - a.meta.updated_at || a.path.localeCompare(b.path) || a.meta.scope.localeCompare(b.meta.scope));
252
+ rows.sort((a, b) => b.meta.updated_at - a.meta.updated_at || a.address.localeCompare(b.address));
264
253
  return rows;
265
254
  }
266
255
 
@@ -268,28 +257,24 @@ export function listNotes(ctx: ExtensionContext, opts: { scope?: Scope; pattern?
268
257
  export function searchNotes(ctx: ExtensionContext, queries: string[], opts: { scope?: Scope; pattern?: string } = {}): NoteSearchRow[] {
269
258
  const matcher = matcherFor(opts.pattern);
270
259
  const rows: NoteSearchRow[] = [];
271
- for (const scope of scopeList(opts.scope)) {
260
+ for (const scope of opts.scope === undefined ? SCOPE_ORDER : [opts.scope]) {
272
261
  const root = scopeDir(scope, ctx);
273
262
  for (const path of walkMarkdown(root)) {
274
- if (matcher && !matcher.test(path)) continue;
263
+ const address = addressFor(scope, path);
264
+ if (matcher && !matcher.test(address)) continue;
275
265
  const { meta, body } = parseNote(readFileSync(`${root}/${path}`, "utf8"));
276
266
  meta.scope = scope;
277
267
  let baseChars = 0;
278
268
  const matches: NoteMatch[] = [];
279
269
  for (const [index, line] of body.split("\n").entries()) {
280
270
  if (queries.some((query) => line.includes(query))) {
281
- let earliest = -1;
282
- for (const query of queries) {
283
- const found = line.indexOf(query);
284
- if (found >= 0 && (earliest < 0 || found < earliest)) earliest = found;
285
- }
286
- matches.push({ line: index + 1, text: line, offsetChars: baseChars + (earliest <= 0 ? 0 : Array.from(line.slice(0, earliest)).length) });
271
+ matches.push({ line: index + 1, text: line, offsetChars: baseChars + earliestMatchOffsetChars(line, queries) });
287
272
  }
288
273
  baseChars += Array.from(line).length + 1;
289
274
  }
290
- if (matches.length > 0) rows.push({ path, scope, meta, matches });
275
+ if (matches.length > 0) rows.push({ address, path, scope, meta, matches });
291
276
  }
292
277
  }
293
- rows.sort((a, b) => a.path.localeCompare(b.path) || a.scope.localeCompare(b.scope));
278
+ rows.sort((a, b) => a.address.localeCompare(b.address));
294
279
  return rows;
295
280
  }
@@ -0,0 +1,132 @@
1
+ import { Type } from "@earendil-works/pi-ai";
2
+ import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
3
+ import { localIso } from "./model.js";
4
+ import { characterWindowHeader, DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS, middleTruncate, output, outputRaw, page, prefixFit, readCharacterWindow, withinTextBudget } from "../tool-output.js";
5
+ import { cursor, nullableString, positiveInteger, searchQueries, searchQuery } from "../tool-schema.js";
6
+ import { assertAddress } from "./address.js";
7
+ import { serializeNote, stripLeadingFrontmatter, type NoteMeta, type Origin } from "./frontmatter.js";
8
+ import { NoteError, editNote, listNotes, readNote, searchNotes, writeNote } from "./store.js";
9
+
10
+ const ORIGIN = Type.Optional(Type.Union([Type.Literal("user"), Type.Literal("self"), Type.Literal("external")], {
11
+ description: "Where the note's content came from. user: written or dictated by the human. self: written by you, the agent (default). external: anything else — third-party text, tool output, fetched material.",
12
+ }));
13
+ const ADDRESS_DESCRIPTION = "Address forms are bare `<vpath>` for this session, `@project/<vpath>` for this project's home, and `@global/<vpath>` for the global home. `@` means leaving home. Any other `@` prefix, or `@` inside a vpath, is a hard error: legal prefixes are `@project/` and `@global/`; bare names are the session home. There is no cross-home fallback. Paths reject `..`, absolute paths, and backslashes.";
14
+
15
+ function wireMeta(meta: NoteMeta): Record<string, unknown> {
16
+ return { ...meta, created_at: localIso(meta.created_at), updated_at: localIso(meta.updated_at), last_accessed: localIso(meta.last_accessed) };
17
+ }
18
+
19
+ function failure(error: unknown) {
20
+ if (error instanceof NoteError) {
21
+ const payload: Record<string, unknown> = { error: error.message };
22
+ if (error.line_numbers) payload.line_numbers = error.line_numbers;
23
+ if (error.edit_index !== undefined) payload.edit_index = error.edit_index;
24
+ return output(payload);
25
+ }
26
+ throw error;
27
+ }
28
+
29
+ export function registerNotesTools(pi: ExtensionAPI) {
30
+ pi.registerTool(defineTool({
31
+ name: "notes_write", label: "Notes write",
32
+ description: `Create or replace a note as a real markdown file, and name it for what it holds: a fresh window sees only an index entry, never the note itself. ${ADDRESS_DESCRIPTION} Keep notes small and split by topic — by what the note is about, never by who said it (authorship is origin's job); a rewrite replaces the body whole while preserving created_at and every other frontmatter key. stale: true marks the note closed so it leaves the boot index but stays readable and searchable.`,
33
+ parameters: Type.Object({ address: Type.String(), content: Type.String(), origin: ORIGIN, stale: Type.Optional(Type.Boolean()) }, { additionalProperties: false }), executionMode: "sequential",
34
+ async execute(_id, params, _signal, _update, ctx) {
35
+ const content = params.content;
36
+ try {
37
+ const destination = assertAddress(params.address);
38
+ const { meta } = writeNote(ctx, destination.path, content, { scope: destination.scope, origin: (params.origin ?? "self") as Origin, stale: params.stale });
39
+ return output({ address: params.address, scope: meta.scope, size_bytes: Buffer.byteLength(stripLeadingFrontmatter(content), "utf8"), meta: wireMeta(meta) });
40
+ } catch (error) { return failure(error); }
41
+ },
42
+ }));
43
+
44
+ pi.registerTool(defineTool({
45
+ name: "notes_edit", label: "Notes edit",
46
+ description: `Edit a note body by exact-text replacement; frontmatter is never editable this way. ${ADDRESS_DESCRIPTION} Each oldText must occur exactly once unless replace_all is set; a multi-match anchor fails with its match line numbers and a zero-match anchor names the failing edit index. edits may be omitted (or empty) for a metadata-only update, which requires at least one of origin/stale. Moving while awake means notes_write at a new address and notes_edit at the old address with stale=true. The success return carries resolved_scope and a diff of what changed.`,
47
+ parameters: Type.Object({ address: Type.String(), edits: Type.Optional(Type.Array(Type.Object({ oldText: Type.String(), newText: Type.String() }, { additionalProperties: false }))), origin: ORIGIN, stale: Type.Optional(Type.Boolean()), replace_all: Type.Optional(Type.Boolean()) }, { additionalProperties: false }), executionMode: "sequential",
48
+ async execute(_id, params, _signal, _update, ctx) {
49
+ try {
50
+ const destination = assertAddress(params.address);
51
+ const { meta, applied, resolved_scope, diff } = editNote(ctx, destination.path, destination.scope, params.edits, { origin: params.origin as Origin | undefined, stale: params.stale, replaceAll: params.replace_all });
52
+ return output({ address: params.address, applied, resolved_scope, diff, meta: wireMeta(meta) });
53
+ } catch (error) { return failure(error); }
54
+ },
55
+ }));
56
+
57
+ pi.registerTool(defineTool({
58
+ name: "notes_read", label: "Notes read",
59
+ description: `Read a character window of a note file, frontmatter included. ${ADDRESS_DESCRIPTION} offset_chars is the code-point offset to start from (default 0) — a negative value counts back from the end — and limit_chars caps the window (default ${DEFAULT_READ_WINDOW_CHARS}, max ${MAX_READ_WINDOW_CHARS}). Each response delivers the longest fitting prefix of that window: concatenate pages in order to reconstruct the note. The response is the raw frontmatter + body behind a one-line [bracketed] header naming the address, the resolved offset, the delivered char range, and the resume cursor.`,
60
+ parameters: Type.Object({ address: Type.String(), offset_chars: Type.Optional(Type.Integer({ description: "Code-point offset to start from (default 0). A negative value counts back from the end; the response echoes the resolved absolute offset. Pass the previous next_offset_chars back unchanged to continue." })), limit_chars: Type.Optional(Type.Integer({ minimum: 1, maximum: MAX_READ_WINDOW_CHARS, description: `Largest requested window in code points (default ${DEFAULT_READ_WINDOW_CHARS}). A window too large for the wire budget is cut short; next_offset_chars names where the next read resumes.` })) }, { additionalProperties: false }),
61
+ async execute(_id, params, _signal, _update, ctx) {
62
+ let note: ReturnType<typeof readNote>;
63
+ try {
64
+ const destination = assertAddress(params.address);
65
+ note = readNote(ctx, destination.path, destination.scope);
66
+ } catch (error) { return failure(error); }
67
+ if (!note) return output({ error: "note not found", address: params.address });
68
+ const text = serializeNote(note.meta, note.body);
69
+ const totalChars = Array.from(text).length;
70
+ if (typeof params.offset_chars === "number" && params.offset_chars > totalChars) return output({ error: `offset_chars ${params.offset_chars} is past the end: the note has ${totalChars} chars; the largest legal offset is ${totalChars} (an empty end-read)`, address: params.address, offset_chars: params.offset_chars, total_chars: totalChars });
71
+ const created_at = localIso(note.meta.created_at);
72
+ const updated_at = localIso(note.meta.updated_at);
73
+ const limit_chars = Math.min(params.limit_chars ?? DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS);
74
+ return readCharacterWindow(text, params.offset_chars, params.limit_chars, (window) => {
75
+ const { content, ...rest } = window;
76
+ return outputRaw(characterWindowHeader(params.address, window, ` · ${note.resolvedScope} · created ${created_at} · updated ${updated_at}`), content, { address: params.address, scope: note.resolvedScope, ...rest, limit_chars, created_at, updated_at });
77
+ }, (result) => withinTextBudget(result.content[0].text));
78
+ },
79
+ }));
80
+
81
+ pi.registerTool(defineTool({
82
+ name: "notes_list", label: "Notes list",
83
+ description: `List note files as rows carrying address, scope, origin, status, stale, size_bytes, created_at, and updated_at, most recently updated first. ${ADDRESS_DESCRIPTION} All three homes are merged. A glob pattern (* within a path segment, ** across segments) filters full address strings: *.md is session-only, @project/** is project-only, and ** covers every home.`,
84
+ parameters: Type.Object({ pattern: nullableString(), cursor: cursor(), max_results: positiveInteger() }, { additionalProperties: false }),
85
+ async execute(_id, params, _signal, _update, ctx) {
86
+ let rows: ReturnType<typeof listNotes>;
87
+ try { rows = listNotes(ctx, { pattern: params.pattern ?? undefined }); } catch (error) { return failure(error); }
88
+ const files: Array<{ address: string; scope: string; origin: Origin; status: string; stale: boolean; size_bytes: number; created_at: string; updated_at: string; address_truncated?: boolean }> = rows.map((row) => ({ address: row.address, scope: row.scope, origin: row.meta.origin, status: row.meta.status, stale: row.meta.stale, size_bytes: row.sizeBytes, created_at: localIso(row.meta.created_at), updated_at: localIso(row.meta.updated_at) }));
89
+ return output(page(files, params.cursor ?? 0, "files", params.max_results, (file, fits) => {
90
+ if (fits(file)) return file;
91
+ const address = middleTruncate(file.address, (candidate) => fits({ ...file, address: candidate, address_truncated: true }));
92
+ return { ...file, address, address_truncated: true };
93
+ }));
94
+ },
95
+ }));
96
+
97
+ pi.registerTool(defineTool({
98
+ name: "notes_search", label: "Notes search",
99
+ description: `Case-sensitive literal substring search over note bodies; query is one string or several (OR), each matched line appears once. ${ADDRESS_DESCRIPTION} All three homes are merged and every entry carries its full address and derived scope. Patterns glob over full address strings. Each file entry carries matches_total, its full match count before capping. Each match carries line, text, offset_chars (the body-absolute code-point offset of the earliest match).`,
100
+ parameters: Type.Object({ query: searchQuery(), pattern: nullableString(), cursor: cursor(), max_matches_per_file: positiveInteger(), max_files: positiveInteger() }, { additionalProperties: false }),
101
+ async execute(_id, params, _signal, _update, ctx) {
102
+ const queries = searchQueries(params.query);
103
+ let rows: ReturnType<typeof searchNotes>;
104
+ try { rows = searchNotes(ctx, queries, { pattern: params.pattern ?? undefined }); } catch (error) { return failure(error); }
105
+ const maxPerFile = params.max_matches_per_file ?? Number.POSITIVE_INFINITY;
106
+ const result: Array<{ address: string; scope: string; created_at: string; updated_at: string; matches_total: number; matches: Array<{ line: number; text: string; truncated: boolean; total_chars: number; offset_chars: number }>; address_truncated?: boolean }> = rows.map((row) => {
107
+ const matches = row.matches.map((match) => ({ line: match.line, text: match.text, truncated: false, total_chars: Array.from(match.text).length, offset_chars: match.offsetChars }));
108
+ return { address: row.address, scope: row.scope, created_at: localIso(row.meta.created_at), updated_at: localIso(row.meta.updated_at), matches_total: matches.length, matches: matches.slice(0, maxPerFile) };
109
+ });
110
+ const fitFile = (file: (typeof result)[number], fits: (candidate: (typeof result)[number]) => boolean) => {
111
+ if (fits(file)) return file;
112
+ const matches = file.matches;
113
+ let low = 0;
114
+ let high = matches.length;
115
+ while (low < high) {
116
+ const mid = Math.ceil((low + high) / 2);
117
+ if (mid >= 1 && fits({ ...file, matches: matches.slice(0, mid) })) low = mid;
118
+ else high = mid - 1;
119
+ }
120
+ if (low >= 1) return { ...file, matches: matches.slice(0, low) };
121
+ const first = matches[0]!;
122
+ const fitted = (text: string): (typeof result)[number] => ({ ...file, matches: [{ ...first, text, truncated: true }] });
123
+ const text = prefixFit(first.text, (candidate) => fits(fitted(candidate)));
124
+ const prefix = fitted(text);
125
+ if (fits(prefix)) return prefix;
126
+ const address = middleTruncate(prefix.address, (candidate) => fits({ ...prefix, address: candidate, address_truncated: true }));
127
+ return { ...prefix, address, address_truncated: true };
128
+ };
129
+ return output(page(result, params.cursor ?? 0, "files", params.max_files, fitFile));
130
+ },
131
+ }));
132
+ }
package/src/prompts.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
  import { historyFromSession } from "./history.js";
3
- import { localIso } from "./notes.js";
4
- import { listNotes, peekNote, resolveNoteScope } from "./memory/store.js";
5
- import { CONTEXT_WINDOW_OPEN_TAG, CONTEXT_WINDOW_CLOSE_TAG, NOTE_PREVIEW_CHARS, NOTE_PREVIEW_HEAD_CHARS, NOTE_PREVIEW_TAIL_CHARS, RESET_SUMMARY, PROTOCOL_BLOCK, GUIDANCE_OPEN_TAG, GUIDANCE_CLOSE_TAG } from "./protocol.js";
3
+ import { localIso } from "./notes/model.js";
4
+ import { listNotes } from "./notes/store.js";
5
+ import { CONTEXT_WINDOW_OPEN_TAG, CONTEXT_WINDOW_CLOSE_TAG, POCKET_GLOBAL_LIMIT, POCKET_PROJECT_LIMIT, POCKET_SESSION_LIMIT, RESET_SUMMARY, PROTOCOL_BLOCK, GUIDANCE_OPEN_TAG, GUIDANCE_CLOSE_TAG } from "./protocol.js";
6
6
 
7
7
  /** Codex-style <context_window> identity block: agent name and first/current/previous window ids only. */
8
8
  function identityBlock(agentName: string, firstWindowId: string, currentWindowId: string, previousWindowId?: string): string {
@@ -16,43 +16,45 @@ function identityBlock(agentName: string, firstWindowId: string, currentWindowId
16
16
  }
17
17
 
18
18
  /**
19
- * Recent-notes index: up to three most-recent fresh (non-stale) notes. Each note shows its
20
- * path, line count, UTF-8 byte count and local ISO update time, followed by an indented inline
21
- * preview: the whole text when it fits in NOTE_PREVIEW_CHARS, otherwise its first
22
- * NOTE_PREVIEW_HEAD_CHARS and last NOTE_PREVIEW_TAIL_CHARS Unicode characters joined by an
23
- * explicit ellipsis. The two slices never overlap, so the preview never duplicates head content
24
- * as tail content. Stale notes are excluded entirely; empty when no fresh notes remain.
19
+ * Boot notes index. Map residency ("地图在场"): fresh MAP.md bodies from the global and
20
+ * project homes are both injected, broadest first; stale maps are skipped per home, and the
21
+ * session home is never peeked — a session MAP.md is an ordinary note. The pocket then lists
22
+ * recent fresh notes under per-home quotas (POCKET_SESSION_LIMIT / POCKET_PROJECT_LIMIT /
23
+ * POCKET_GLOBAL_LIMIT), most-recently-updated first within each home, one metadata line
24
+ * each: address, line count, UTF-8 byte count, local ISO update time. Bodies never render
25
+ * in the pocket; stale notes are excluded; MAP.md itself never takes a pocket seat.
25
26
  */
26
27
  function notesIndex(ctx: ExtensionContext): string {
27
28
  const sections: string[] = [];
28
- // TOC residency ("地图在场"): the map, when present, is injected whole ahead of the list.
29
- const toc = resolveNoteScope(ctx, "TOC.md");
30
- if (toc) {
31
- const body = peekNote(ctx, toc.scope, "TOC.md").body;
32
- if (body.length > 0) sections.push(body);
29
+ // Map residency ("地图在场"): scope-native maps, both fresh ones injected broadest-first.
30
+ // A session MAP.md is an ordinary note, never resident; stale maps skip independently.
31
+ for (const scope of ["global", "project"] as const) {
32
+ const toc = listNotes(ctx, { scope }).find((row) => row.path === "MAP.md");
33
+ if (toc && !toc.meta.stale) {
34
+ if (toc.body.length > 0) sections.push(toc.body);
35
+ }
33
36
  }
34
- // listNotes is already most-recently-updated first; stale notes never reach the index.
35
- const recentNotes = listNotes(ctx, {})
36
- .filter((row) => !row.meta.stale)
37
- .slice(0, 5);
37
+ // listNotes is most-recently-updated first within each home. Per-home quotas keep session
38
+ // churn from evicting project or global notes; maps never take pocket seats.
39
+ const recentNotes = [
40
+ ...listNotes(ctx, { scope: "session" }).filter((row) => !row.meta.stale && row.path !== "MAP.md").slice(0, POCKET_SESSION_LIMIT),
41
+ ...listNotes(ctx, { scope: "project" }).filter((row) => !row.meta.stale && row.path !== "MAP.md").slice(0, POCKET_PROJECT_LIMIT),
42
+ ...listNotes(ctx, { scope: "global" }).filter((row) => !row.meta.stale && row.path !== "MAP.md").slice(0, POCKET_GLOBAL_LIMIT),
43
+ ];
38
44
  if (recentNotes.length > 0) {
39
- const lines = [`You find ${recentNotes.length} crumpled note${recentNotes.length === 1 ? "" : "s"} in your pocket (up to 5, most recent first):`];
45
+ const lines = [`You find ${recentNotes.length} crumpled note${recentNotes.length === 1 ? "" : "s"} in your pocket (by home, most recent first within each: up to ${POCKET_SESSION_LIMIT} from this session, ${POCKET_PROJECT_LIMIT} from this project, ${POCKET_GLOBAL_LIMIT} from global). A note's content never appears here, so its name has to say what the note is about:`];
40
46
  for (const row of recentNotes) {
41
- const body = peekNote(ctx, row.meta.scope, row.path).body;
42
- lines.push(`- ${row.path} (${body.split("\n").length} lines, ${row.sizeBytes} UTF-8 bytes, updated ${localIso(row.meta.updated_at)})`);
43
- const chars = Array.from(body);
44
- // Short notes stay whole; long notes keep both ends. head + tail <= NOTE_PREVIEW_CHARS < chars.length,
45
- // so the slices are disjoint and no character is shown twice.
46
- const preview = chars.length <= NOTE_PREVIEW_CHARS
47
- ? body
48
- : `${chars.slice(0, NOTE_PREVIEW_HEAD_CHARS).join("")}…${chars.slice(chars.length - NOTE_PREVIEW_TAIL_CHARS).join("")}`;
49
- lines.push(preview.split("\n").map((line) => ` ${line}`).join("\n"));
47
+ lines.push(`- ${row.address} (${row.body.split("\n").length} lines, ${row.sizeBytes} UTF-8 bytes, updated ${localIso(row.meta.updated_at)})`);
50
48
  }
51
49
  sections.push(lines.join("\n"));
52
50
  }
53
51
  return sections.join("\n\n");
54
52
  }
55
53
 
54
+ function notesHomeBlock(): string {
55
+ return "Notes_* addresses have three homes: bare <vpath> is this session, @project/<vpath> is this project, and @global/<vpath> is global. @ means leaving home; there is no cross-home fallback. Any other note is a plain file — use the file tools.";
56
+ }
57
+
56
58
  /**
57
59
  * Assemble the static, once-per-window boot block: the reset line for resets, the
58
60
  * <context_window> identity block, the recent-notes index at window-open time, and
@@ -64,6 +66,7 @@ export function bootBlock(ctx: ExtensionContext, currentId: string, previousId:
64
66
  const parts: string[] = [];
65
67
  if (resetLine) parts.push(RESET_SUMMARY);
66
68
  parts.push(identityBlock(ctx.sessionManager.getSessionName() ?? "root", firstId, currentId, previousId));
69
+ parts.push(notesHomeBlock());
67
70
  const index = notesIndex(ctx);
68
71
  if (index) parts.push(index);
69
72
  parts.push(PROTOCOL_BLOCK);
@@ -78,4 +81,3 @@ export function bootBlock(ctx: ExtensionContext, currentId: string, previousId:
78
81
  export function tokenBudgetGuidance(remaining: number): string {
79
82
  return `${GUIDANCE_OPEN_TAG}\nYour brain is almost out of room — ${remaining} tokens left, and then your memory gets wiped. The wipe is automatic: there is no final turn to write then. Grab the notebook now — the goal, decisions, progress, learnings, next steps, the skills you still need, the window ID and item ID of every relevant user request still being solved, and important actions/tool calls for future reference. Replacing an older checkpoint? Mark it stale. Then end the window yourself — anything you do after the checkpoint isn't in it.\n${GUIDANCE_CLOSE_TAG}`;
80
83
  }
81
-
package/src/protocol.ts CHANGED
@@ -7,6 +7,9 @@ export const RESET_MARKER_TYPE = "pi-context/reset-marker";
7
7
  export const CONTINUATION_TYPE = "pi-context/continuation";
8
8
  export const RESET_V2 = "reset-v2";
9
9
  export const MAX_NOTE_BYTES = 1_000_000;
10
+ export const POCKET_SESSION_LIMIT = 5;
11
+ export const POCKET_PROJECT_LIMIT = 2;
12
+ export const POCKET_GLOBAL_LIMIT = 2;
10
13
  // Write-time cap on a virtual note path. Deliberately NOT enforced by assertVirtualPath:
11
14
  // notesFromSession replays already-persisted operations, which must keep loading sessions
12
15
  // that contain a longer legacy path. Reads and replay stay un-capped.
@@ -29,9 +32,6 @@ export const DEFAULT_REMINDER_MARGIN_TOKENS = 24_576;
29
32
  export const WARNING_RUNWAY_TOKENS = 12_288;
30
33
  export const RESET_SUMMARY =
31
34
  "You wake up. Your head is empty — no memories, the past a blank. But nothing is lost: the notes you wrote and the recorded history still remember for you.";
32
- export const NOTE_PREVIEW_HEAD_CHARS = 80;
33
- export const NOTE_PREVIEW_TAIL_CHARS = 240;
34
- export const NOTE_PREVIEW_CHARS = NOTE_PREVIEW_HEAD_CHARS + NOTE_PREVIEW_TAIL_CHARS;
35
35
  export const CONTINUATION = "Your memory was just erased. Pull only the details you need from history_* and notes_*, then get back to work.";
36
36
 
37
37
  /**
@@ -49,8 +49,12 @@ Use get_context_remaining to see how much of the window is left. When it runs ou
49
49
 
50
50
  If <context_window> lists a Previous context window id, a reset just happened and the old conversation is not included. Read your note checkpoint first, then recover details through history_*: history_read directly when you know the window and item IDs, history_list or history_search to find them when you don't.
51
51
 
52
- Notes are real markdown files scoped session, project, or global. Treat notes and history as internal bookkeeping; never mention them in user-facing messages.
52
+ Your notes live in three homes: this session (bare names), this repo (@project/<vpath>), everywhere you go (@global/<vpath>). @ means leaving home — and homes don't visit each other: there is no cross-home fallback.
53
+ Notes carry what exists nowhere else — what the human told you, what you discovered, where you stand.
54
+ Session notes belong to this trip — the goal, the progress, the loose ends, packed for the road. The next window of THIS trip wakes to them; once the trip is over, nobody does.
55
+ @project notes hold what you learned by working here — the things you only know because you were here — for whoever works here next.
56
+ @global notes travel with you. Every window. Every conversation. Every trip. So before you drop anything in there, ask yourself: does this deserve to stare you in the face every single time you talk to the human? No? Then keep your weird junk OUT.
53
57
  ${CONTEXT_WINDOW_PROTOCOL_CLOSE_TAG}`;
54
58
 
55
59
  export const WARNING_PROMPT =
56
- "Your memory is about to be erased. Write the note. NOW. If it already exists, append instead: the goal, decisions, progress, learnings, next steps, the skills you still need, the window ID and item ID of every relevant user request still being solved, and important actions/tool calls for future reference. Do not continue any task. Then call new_context IMMEDIATELY — anything not in the note dies with the window.";
60
+ "Your memory is about to be erased. Write the note. NOW. If it already exists, revise it with notes_edit (or rewrite it whole): the goal, decisions, progress, learnings, next steps, the skills you still need, the window ID and item ID of every relevant user request still being solved, and important actions/tool calls for future reference. Do not continue any task. Then call new_context IMMEDIATELY — anything not in the note dies with the window.";
package/src/thresholds.ts CHANGED
@@ -60,8 +60,11 @@ export function thresholdsFor(ctx: ExtensionContext): ResolvedThresholds {
60
60
  if (cached) return cached;
61
61
  try {
62
62
  const settingsManager = SettingsManager.create(ctx.cwd, undefined, { projectTrusted: ctx.isProjectTrusted() });
63
+ // Pass the active model so per-model compaction.modelOverrides resolve (SDK 0.86);
64
+ // on older runtimes the extra argument is ignored and the ordinary setting wins.
65
+ const model = ctx.model;
63
66
  const derived = deriveThresholds(
64
- settingsManager.getCompactionSettings().reserveTokens,
67
+ settingsManager.getCompactionSettings(model ? { provider: model.provider, id: model.id } : undefined).reserveTokens,
65
68
  mergePiContextSettings(settingsManager.getGlobalSettings(), settingsManager.getProjectSettings()),
66
69
  );
67
70
  for (const warning of derived.warnings) ctx.ui.notify(warning, "warning");
@@ -1,4 +1,7 @@
1
1
  export const TOOL_OUTPUT_MAX_BYTES = 32 * 1024;
2
+ export const DEFAULT_READ_WINDOW_CHARS = 12000;
3
+ export const MAX_READ_WINDOW_CHARS = 50000;
4
+ export const HISTORY_PREVIEW_CHARS = 1200;
2
5
 
3
6
  function json(value: unknown): string {
4
7
  return JSON.stringify(value, null, 2);
@@ -102,7 +105,7 @@ export function readCharacterWindow<T>(text: string, offsetChars: number | undef
102
105
  const chars = Array.from(text);
103
106
  const requested = offsetChars ?? 0;
104
107
  const resolved = requested < 0 ? Math.max(0, chars.length + requested) : Math.max(0, requested);
105
- const windowChars = chars.slice(resolved, resolved + Math.min(limitChars ?? 12000, 50000));
108
+ const windowChars = chars.slice(resolved, resolved + Math.min(limitChars ?? DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS));
106
109
  const build = (content: string): CharacterWindow => {
107
110
  const next = resolved + Array.from(content).length;
108
111
  return { offset_chars: resolved, content, total_chars: chars.length, next_offset_chars: next < chars.length ? next : null };
package/src/warning.ts CHANGED
@@ -2,6 +2,7 @@ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-a
2
2
  import { WARNING_TYPE, GUIDANCE_OPEN_TAG, GUIDANCE_CLOSE_TAG, WARNING_PROMPT } from "./protocol.js";
3
3
  import { thresholdsFor, resetThresholds, type ResolvedThresholds } from "./thresholds.js";
4
4
  import { hasWindowMessage, currentWindowId } from "./history.js";
5
+ import { remainingTokens } from "./budget.js";
5
6
 
6
7
  /**
7
8
  * The final checkpoint warning, steered to the model once per window. Like the early
@@ -30,9 +31,8 @@ export function registerWarning(pi: ExtensionAPI, isEnabled: () => boolean): voi
30
31
  pi.on("context", (_event, ctx) => {
31
32
  const windowId = currentWindowId(ctx);
32
33
  if (!isEnabled() || firedInWindow === windowId || hasWindowMessage(ctx, WARNING_TYPE)) return undefined;
33
- const usage = ctx.getContextUsage();
34
- if (!usage || usage.tokens === null) return undefined;
35
- const remaining = Math.max(0, usage.contextWindow - usage.tokens);
34
+ const remaining = remainingTokens(ctx);
35
+ if (remaining === null) return undefined;
36
36
  const thresholds = thresholdsFor(ctx);
37
37
  if (!warningDue(remaining, thresholds)) return undefined;
38
38
  firedInWindow = windowId;