@astrosheep/pi-context 0.20.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 (53) hide show
  1. package/dist/src/budget.js +10 -8
  2. package/dist/src/dream/cli.js +9 -8
  3. package/dist/src/dream/gates.js +2 -1
  4. package/dist/src/dream/git.js +28 -0
  5. package/dist/src/dream/runner.js +84 -25
  6. package/dist/src/history-tools.js +5 -5
  7. package/dist/src/history.js +11 -6
  8. package/dist/src/index.js +14 -15
  9. package/dist/src/notes/address.js +31 -0
  10. package/dist/src/{memory → notes}/frontmatter.js +5 -3
  11. package/dist/src/{notes.js → notes/model.js} +1 -1
  12. package/dist/src/{memory → notes}/paths.js +5 -1
  13. package/dist/src/{memory → notes}/store.js +45 -72
  14. package/dist/src/notes/tools.js +153 -0
  15. package/dist/src/prompts.js +31 -29
  16. package/dist/src/protocol.js +8 -4
  17. package/dist/src/thresholds.js +4 -1
  18. package/dist/src/tool-output.js +4 -1
  19. package/dist/src/warning.js +3 -3
  20. package/dist/test/agent-loop.test.js +6 -4
  21. package/dist/test/coherence.test.js +5 -1
  22. package/dist/test/dream.test.js +133 -34
  23. package/dist/test/history.test.js +6 -1
  24. package/dist/test/integration.test.js +84 -34
  25. package/dist/test/{memory.test.js → notes.test.js} +138 -34
  26. package/dist/test/pagination.property.test.js +1 -1
  27. package/package.json +5 -5
  28. package/playbook.md +30 -3
  29. package/src/budget.ts +11 -9
  30. package/src/dream/cli.ts +8 -8
  31. package/src/dream/gates.ts +2 -1
  32. package/src/dream/git.ts +27 -0
  33. package/src/dream/runner.ts +81 -23
  34. package/src/history-tools.ts +5 -5
  35. package/src/history.ts +12 -7
  36. package/src/index.ts +13 -14
  37. package/src/notes/address.ts +33 -0
  38. package/src/{memory → notes}/frontmatter.ts +5 -3
  39. package/src/{notes.ts → notes/model.ts} +2 -2
  40. package/src/{memory → notes}/paths.ts +6 -1
  41. package/src/{memory → notes}/store.ts +47 -77
  42. package/src/notes/tools.ts +132 -0
  43. package/src/prompts.ts +31 -29
  44. package/src/protocol.ts +8 -4
  45. package/src/thresholds.ts +4 -1
  46. package/src/tool-output.ts +4 -1
  47. package/src/warning.ts +3 -3
  48. package/dist/src/dream/apply.js +0 -87
  49. package/dist/src/dream/manifest.js +0 -16
  50. package/dist/src/memory/tools.js +0 -175
  51. package/src/dream/apply.ts +0 -47
  52. package/src/dream/manifest.ts +0 -21
  53. package/src/memory/tools.ts +0 -175
@@ -1,175 +0,0 @@
1
- import { Type } from "@earendil-works/pi-ai";
2
- import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
3
- import { localIso } from "../notes.js";
4
- import { characterWindowHeader, middleTruncate, output, outputRaw, page, prefixFit, readCharacterWindow, withinTextBudget } from "../tool-output.js";
5
- import { cursor, nullableString, positiveInteger, searchQueries, searchQuery } from "../tool-schema.js";
6
- import { serializeNote, stripLeadingFrontmatter, type NoteMeta, type Origin } from "./frontmatter.js";
7
- import { NoteError, editNote, listNotes, readNote, searchNotes, writeNote } from "./store.js";
8
- import type { Scope } from "./paths.js";
9
-
10
- const SCOPE = Type.Optional(
11
- Type.Union([Type.Literal("session"), Type.Literal("project"), Type.Literal("global")], {
12
- description:
13
- "The note's reach — which root it lives under. session: only this session needs it (checkpoints, scratch state, worker rosters); dies with the session. project: tied to the current working directory — design decisions and repo facts that future sessions here still need. global: follows you everywhere — user laws, preferences, cross-project maps. On write, picks the destination root (default: session). Omit on read/list/search to cover all three; a read resolves session → project → global and returns the first existing file.",
14
- }),
15
- );
16
- const ORIGIN = Type.Optional(
17
- Type.Union([Type.Literal("user"), Type.Literal("self"), Type.Literal("external")], {
18
- 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.",
19
- }),
20
- );
21
-
22
- /** Render epoch-ms metadata as the same local ISO timestamps the frontmatter carries. */
23
- function wireMeta(meta: NoteMeta): Record<string, unknown> {
24
- return { ...meta, created_at: localIso(meta.created_at), updated_at: localIso(meta.updated_at), last_accessed: localIso(meta.last_accessed) };
25
- }
26
-
27
- /** Turn a typed store refusal into the pinned error arm; unknown errors stay thrown. */
28
- function failure(error: unknown) {
29
- if (error instanceof NoteError) {
30
- const payload: Record<string, unknown> = { error: error.message };
31
- if (error.line_numbers) payload.line_numbers = error.line_numbers;
32
- if (error.edit_index !== undefined) payload.edit_index = error.edit_index;
33
- return output(payload);
34
- }
35
- throw error;
36
- }
37
-
38
- export function registerMemoryTools(pi: ExtensionAPI) {
39
- pi.registerTool(defineTool({
40
- name: "notes_write",
41
- label: "Notes write",
42
- description: "Create or replace a note as a real markdown file under the session, project, or global note root. 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.",
43
- parameters: Type.Object({ path: Type.String(), content: Type.String(), scope: SCOPE, origin: ORIGIN, stale: Type.Optional(Type.Boolean()) }, { additionalProperties: false }),
44
- // A batch containing write or edit runs one call at a time, so note read-modify-write cannot race.
45
- executionMode: "sequential",
46
- async execute(_id, params, _signal, _update, ctx) {
47
- const content = params.content;
48
- try {
49
- const { meta } = writeNote(ctx, params.path, content, { scope: (params.scope ?? "session") as Scope, origin: (params.origin ?? "self") as Origin, stale: params.stale });
50
- return output({ path: params.path, scope: meta.scope, size_bytes: Buffer.byteLength(stripLeadingFrontmatter(content), "utf8"), meta: wireMeta(meta) });
51
- } catch (error) {
52
- return failure(error);
53
- }
54
- },
55
- }));
56
-
57
- pi.registerTool(defineTool({
58
- name: "notes_edit",
59
- label: "Notes edit",
60
- description: "Edit a note body by exact-text replacement; frontmatter is never editable this way. 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 scope/origin/stale. scope/origin/stale are setters: scope moves the file, refusing when the target already exists. The success return carries resolved_scope and a diff of what changed.",
61
- parameters: Type.Object({ path: Type.String(), edits: Type.Optional(Type.Array(Type.Object({ oldText: Type.String(), newText: Type.String() }, { additionalProperties: false }))), scope: SCOPE, origin: ORIGIN, stale: Type.Optional(Type.Boolean()), replace_all: Type.Optional(Type.Boolean()) }, { additionalProperties: false }),
62
- executionMode: "sequential",
63
- async execute(_id, params, _signal, _update, ctx) {
64
- try {
65
- const { meta, applied, resolved_scope, diff } = editNote(ctx, params.path, params.edits, { scope: params.scope as Scope | undefined, origin: params.origin as Origin | undefined, stale: params.stale, replaceAll: params.replace_all });
66
- return output({ path: params.path, applied, resolved_scope, diff, meta: wireMeta(meta) });
67
- } catch (error) {
68
- return failure(error);
69
- }
70
- },
71
- }));
72
-
73
- pi.registerTool(defineTool({
74
- name: "notes_read",
75
- label: "Notes read",
76
- description: "Read a character window of a note file, frontmatter included: 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 12000, max 50000). 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 file, the resolved offset, the delivered char range, and the resume cursor.",
77
- parameters: Type.Object({ path: Type.String(), scope: SCOPE, 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: 50000, description: "Largest requested window in code points (default 12000). A window too large for the wire budget is cut short; next_offset_chars names where the next read resumes." })) }, { additionalProperties: false }),
78
- async execute(_id, params, _signal, _update, ctx) {
79
- let note: ReturnType<typeof readNote>;
80
- try {
81
- note = readNote(ctx, params.path, { scope: params.scope as Scope | undefined });
82
- } catch (error) {
83
- return failure(error);
84
- }
85
- if (!note) return output({ error: "note not found", path: params.path });
86
- const text = serializeNote(note.meta, note.body);
87
- const totalChars = Array.from(text).length;
88
- // A positive offset past the end is an addressing error, not an empty page.
89
- if (typeof params.offset_chars === "number" && params.offset_chars > totalChars) {
90
- 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)`, path: params.path, offset_chars: params.offset_chars, total_chars: totalChars });
91
- }
92
- const created_at = localIso(note.meta.created_at);
93
- const updated_at = localIso(note.meta.updated_at);
94
- const limit_chars = Math.min(params.limit_chars ?? 12000, 50000);
95
- return readCharacterWindow(text, params.offset_chars, params.limit_chars, (window) => {
96
- const { content, ...rest } = window;
97
- return outputRaw(characterWindowHeader(params.path, window, ` · ${note.resolvedScope} · created ${created_at} · updated ${updated_at}`), content, { path: params.path, scope: note.resolvedScope, ...rest, limit_chars, created_at, updated_at });
98
- }, (result) => withinTextBudget(result.content[0].text));
99
- },
100
- }));
101
-
102
- pi.registerTool(defineTool({
103
- name: "notes_list",
104
- label: "Notes list",
105
- description: "List note files as rows carrying path, scope, origin, status, stale, size_bytes, created_at, and updated_at, most recently updated first. Without scope, all three scopes are merged; a glob pattern (* within a path segment, ** across segments) filters the virtual paths.",
106
- parameters: Type.Object({ scope: SCOPE, pattern: nullableString(), cursor: cursor(), max_results: positiveInteger() }, { additionalProperties: false }),
107
- async execute(_id, params, _signal, _update, ctx) {
108
- let rows: ReturnType<typeof listNotes>;
109
- try {
110
- rows = listNotes(ctx, { scope: params.scope as Scope | undefined, pattern: params.pattern ?? undefined });
111
- } catch (error) {
112
- return failure(error);
113
- }
114
- const files: Array<{ path: string; scope: Scope; origin: Origin; status: string; stale: boolean; size_bytes: number; created_at: string; updated_at: string; path_truncated?: boolean }> = rows.map((row) => ({
115
- path: row.path,
116
- scope: row.meta.scope,
117
- origin: row.meta.origin,
118
- status: row.meta.status,
119
- stale: row.meta.stale,
120
- size_bytes: row.sizeBytes,
121
- created_at: localIso(row.meta.created_at),
122
- updated_at: localIso(row.meta.updated_at),
123
- }));
124
- return output(page(files, params.cursor ?? 0, "files", params.max_results, (file, fits) => {
125
- if (fits(file)) return file;
126
- const path = middleTruncate(file.path, (candidate) => fits({ ...file, path: candidate, path_truncated: true }));
127
- return { ...file, path, path_truncated: true };
128
- }));
129
- },
130
- }));
131
-
132
- pi.registerTool(defineTool({
133
- name: "notes_search",
134
- label: "Notes search",
135
- description: "Case-sensitive literal substring search over note bodies; query is one string or several (OR), each matched line appears once. Without scope, all three scopes are merged and every entry carries its scope. 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).",
136
- parameters: Type.Object({ query: searchQuery(), scope: SCOPE, pattern: nullableString(), cursor: cursor(), max_matches_per_file: positiveInteger(), max_files: positiveInteger() }, { additionalProperties: false }),
137
- async execute(_id, params, _signal, _update, ctx) {
138
- const queries = searchQueries(params.query);
139
- let rows: ReturnType<typeof searchNotes>;
140
- try {
141
- rows = searchNotes(ctx, queries, { scope: params.scope as Scope | undefined, pattern: params.pattern ?? undefined });
142
- } catch (error) {
143
- return failure(error);
144
- }
145
- const maxPerFile = params.max_matches_per_file ?? Number.POSITIVE_INFINITY;
146
- const result: Array<{ path: string; scope: Scope; created_at: string; updated_at: string; matches_total: number; matches: Array<{ line: number; text: string; truncated: boolean; total_chars: number; offset_chars: number }>; path_truncated?: boolean }> = rows.map((row) => {
147
- 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 }));
148
- return { path: row.path, 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) };
149
- });
150
- // Trailing matches are dropped to fit the budget, named by matches_total; a single
151
- // over-budget line is delivered as a flagged prefix; only a pathological path is
152
- // middle-truncated, and then only with a visible path_truncated flag.
153
- const fitFile = (file: (typeof result)[number], fits: (candidate: (typeof result)[number]) => boolean) => {
154
- if (fits(file)) return file;
155
- const matches = file.matches;
156
- let low = 0;
157
- let high = matches.length;
158
- while (low < high) {
159
- const mid = Math.ceil((low + high) / 2);
160
- if (mid >= 1 && fits({ ...file, matches: matches.slice(0, mid) })) low = mid;
161
- else high = mid - 1;
162
- }
163
- if (low >= 1) return { ...file, matches: matches.slice(0, low) };
164
- const first = matches[0]!;
165
- const fitted = (text: string): (typeof result)[number] => ({ ...file, matches: [{ ...first, text, truncated: true }] });
166
- const text = prefixFit(first.text, (candidate) => fits(fitted(candidate)));
167
- const prefix: (typeof result)[number] = fitted(text);
168
- if (fits(prefix)) return prefix;
169
- const path = middleTruncate(prefix.path, (candidate) => fits({ ...prefix, path: candidate, path_truncated: true }));
170
- return { ...prefix, path, path_truncated: true };
171
- };
172
- return output(page(result, params.cursor ?? 0, "files", params.max_files, fitFile));
173
- },
174
- }));
175
- }