@astrosheep/pi-context 0.22.0 → 0.23.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.
- package/README.md +5 -1
- package/dist/src/dream/cli.js +13 -1
- package/dist/src/dream/doctor.js +138 -0
- package/dist/src/history-tools.js +3 -4
- package/dist/src/notes/store.js +14 -8
- package/dist/src/notes/tools.js +14 -21
- package/dist/src/reset-lifecycle.js +82 -28
- package/dist/src/tool-output.js +8 -11
- package/dist/test/agent-loop.test.js +25 -9
- package/dist/test/coherence.test.js +32 -36
- package/dist/test/doctor.test.js +44 -0
- package/dist/test/integration.test.js +26 -27
- package/dist/test/notes.test.js +106 -20
- package/dist/test/pagination.property.test.js +24 -29
- package/dist/test/reset-lifecycle.test.js +99 -102
- package/docs/reset-lifecycle.md +4 -2
- package/package.json +1 -1
- package/src/dream/cli.ts +10 -1
- package/src/dream/doctor.ts +97 -0
- package/src/history-tools.ts +3 -4
- package/src/notes/store.ts +16 -9
- package/src/notes/tools.ts +16 -23
- package/src/reset-lifecycle.ts +89 -28
- package/src/tool-output.ts +8 -11
package/package.json
CHANGED
package/src/dream/cli.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { materialGate, timeGate } from "./gates.js";
|
|
|
7
7
|
import { loadPlaybook, runDreamer, type DreamerSessionFactory, type DreamResult, type DreamWrite } from "./runner.js";
|
|
8
8
|
import { gitCommit } from "./git.js";
|
|
9
9
|
import { readDreamerSettings, type DreamerSetting } from "../thresholds.js";
|
|
10
|
+
import { doctor } from "./doctor.js";
|
|
10
11
|
import { notesRoot } from "../notes/paths.js";
|
|
11
12
|
|
|
12
13
|
function args(argv: string[]) { const out: Record<string, string | boolean> = {}; for (let i=0;i<argv.length;i++) { const a=argv[i]!; if (a === "--force" || a === "--help") out[a.slice(2)] = true; else if (a.startsWith("--")) out[a.slice(2)] = argv[++i] ?? ""; } return out; }
|
|
@@ -55,7 +56,15 @@ function finishDream(home: string, stamp: string, reportPath: string, failed: bo
|
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
export async function main(argv = process.argv.slice(2), deps: DreamDependencies = {}): Promise<number> {
|
|
58
|
-
|
|
59
|
+
if (argv[0] === "doctor") {
|
|
60
|
+
const options = args(argv.slice(1));
|
|
61
|
+
if (options.help) { console.log("dream doctor [--notes-home <dir>] — read-only diagnostics; no model or repairs"); return 0; }
|
|
62
|
+
const home = resolve(String(options["notes-home"] ?? notesRoot()));
|
|
63
|
+
const issues = doctor(home);
|
|
64
|
+
console.log(issues.length ? issues.join("\n") : `dream doctor: OK (${home})`);
|
|
65
|
+
return issues.length ? 1 : 0;
|
|
66
|
+
}
|
|
67
|
+
const a = args(argv); if (a.help) { console.log("dream doctor [--notes-home <dir>] — read-only diagnostics\ndream --notes-home <dir> [--min-hours 24] [--min-sessions 3] [--force] [--dreamer <model pattern>] [--playbook <path>]\nDreamer model: --dreamer wins, else pi-context.dreamer from settings, else the automatic model. Default playbook: <installed package root>/playbook.md; --playbook overrides it."); return 0; }
|
|
59
68
|
const home = resolve(String(a["notes-home"] ?? notesRoot())); process.env.PI_NOTES_HOME = home; mkdirSync(home, { recursive: true });
|
|
60
69
|
const lockPath = join(home, ".dream.lock"); const stampPath = lastRunPath(lockPath);
|
|
61
70
|
const minHours = Number(a["min-hours"] ?? 24); const minSessions = Number(a["min-sessions"] ?? 3);
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import { basename, join, relative } from "node:path";
|
|
3
|
+
import { assertAddress } from "../notes/address.js";
|
|
4
|
+
|
|
5
|
+
/** Read-only diagnostics. Never follows symlinks or acquires/removes a dream lock. */
|
|
6
|
+
export function doctor(home: string): string[] {
|
|
7
|
+
const issues: string[] = [];
|
|
8
|
+
const report = (path: string, message: string) => issues.push(`${relative(home, path) || "."}: ${message}`);
|
|
9
|
+
const inspect = (path: string, action: () => void) => {
|
|
10
|
+
try { action(); } catch (error) { report(path, `cannot inspect: ${error instanceof Error ? error.message : String(error)}`); }
|
|
11
|
+
};
|
|
12
|
+
const directory = (path: string): boolean => {
|
|
13
|
+
const stat = lstatSync(path);
|
|
14
|
+
if (stat.isDirectory()) return true;
|
|
15
|
+
report(path, "expected a directory (symlinks are not followed); check its location/type");
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
18
|
+
const checkNote = (path: string, root: string, project?: string) => {
|
|
19
|
+
const raw = readFileSync(path, "utf8");
|
|
20
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/.exec(raw);
|
|
21
|
+
if (!match) { report(path, "missing or unclosed frontmatter; add a valid metadata block"); return; }
|
|
22
|
+
const fields = new Map<string, string>();
|
|
23
|
+
for (const line of match[1]!.split(/\r?\n/)) {
|
|
24
|
+
const field = /^([\w]+):\s*(.*?)\s*$/.exec(line);
|
|
25
|
+
if (!field) continue;
|
|
26
|
+
if (fields.has(field[1]!)) report(path, `duplicate metadata key ${field[1]}; keep one value`);
|
|
27
|
+
fields.set(field[1]!, field[2]!.replace(/^(["'])(.*)\1$/, "$2"));
|
|
28
|
+
}
|
|
29
|
+
for (const [key, valid] of Object.entries({ origin: /^(user|self|external)$/, status: /^(active|superseded|pending|archived)$/, stale: /^(true|false)$/, access_count: /^\d+$/ })) {
|
|
30
|
+
if (!valid.test(fields.get(key) ?? "")) report(path, `missing/invalid ${key}; repair frontmatter`);
|
|
31
|
+
}
|
|
32
|
+
for (const key of ["created_at", "updated_at", "last_accessed"]) {
|
|
33
|
+
const value = fields.get(key);
|
|
34
|
+
if (!value || !Number.isFinite(Date.parse(value))) report(path, `missing/invalid ${key}; use an ISO timestamp`);
|
|
35
|
+
}
|
|
36
|
+
if (fields.has("scope")) report(path, "obsolete scope field; remove it (home determines scope)");
|
|
37
|
+
// Check concrete, code-formatted addresses; examples/globs and prose are not links.
|
|
38
|
+
for (const link of raw.slice(match[0].length).matchAll(/`([^`\n]+)`/g)) {
|
|
39
|
+
const address = link[1]!;
|
|
40
|
+
if (!address.endsWith(".md") || /[<>*?\s]/.test(address)) continue;
|
|
41
|
+
if (!address.startsWith("@") && basename(path) !== "MAP.md") continue;
|
|
42
|
+
try {
|
|
43
|
+
const parsed = assertAddress(address);
|
|
44
|
+
const targetHome = parsed.scope === "personal" ? join(home, "personal") : parsed.scope === "project" ? project : root;
|
|
45
|
+
if (!targetHome) { report(path, `${address}: project context unavailable; use a resolvable reference`); continue; }
|
|
46
|
+
if (!existsSync(join(targetHome, parsed.path))) report(path, `${address}: target missing; update or remove the reference`);
|
|
47
|
+
} catch { report(path, `${address}: invalid address; use bare, @project/ or @personal/ addresses`); }
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const walk = (dir: string, root: string, project?: string) => {
|
|
51
|
+
for (const name of readdirSync(dir)) {
|
|
52
|
+
const path = join(dir, name);
|
|
53
|
+
inspect(path, () => {
|
|
54
|
+
const stat = lstatSync(path);
|
|
55
|
+
if (stat.isSymbolicLink()) report(path, "symlink not inspected; replace with a regular note/directory");
|
|
56
|
+
else if (stat.isDirectory()) walk(path, root, project);
|
|
57
|
+
else if (stat.isFile() && name.endsWith(".md")) checkNote(path, root, project);
|
|
58
|
+
else report(path, "unexpected file in note home; inspect and relocate it");
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
inspect(home, () => {
|
|
63
|
+
if (!directory(home)) return;
|
|
64
|
+
for (const name of readdirSync(home)) {
|
|
65
|
+
const path = join(home, name);
|
|
66
|
+
inspect(path, () => {
|
|
67
|
+
if (name === "global") { report(path, "legacy home; manually migrate to personal/ without overwriting existing files"); return; }
|
|
68
|
+
if (name === ".dream.lock") {
|
|
69
|
+
const valid = lstatSync(path).isFile() && /^[1-9]\d* [\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}\s*$/i.test(readFileSync(path, "utf8"));
|
|
70
|
+
report(path, `${valid ? "lock present" : "malformed lock"}; verify no dream is running before manual removal; liveness not inferred`);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if ([".git", "dreams", "snapshots", "trash", ".dream.lock.last-run"].includes(name)) return;
|
|
74
|
+
if (name === "personal") { if (directory(path)) walk(path, path); return; }
|
|
75
|
+
if (name === "project" || name === "pi") {
|
|
76
|
+
if (!directory(path)) return;
|
|
77
|
+
const homes = name === "pi" ? join(path, "session") : path;
|
|
78
|
+
if (name === "pi") {
|
|
79
|
+
for (const entry of readdirSync(path)) if (entry !== "session") report(join(path, entry), "unexpected directory; expected pi/session/<id>/");
|
|
80
|
+
if (!existsSync(homes) || !directory(homes)) return;
|
|
81
|
+
}
|
|
82
|
+
for (const id of readdirSync(homes)) {
|
|
83
|
+
const root = join(homes, id);
|
|
84
|
+
inspect(root, () => {
|
|
85
|
+
if (!directory(root)) return;
|
|
86
|
+
if (name === "project" && !/^.+-[\da-f]{8}$/.test(id)) report(root, "invalid project key; expected <name>-<8 hex>");
|
|
87
|
+
walk(root, root, name === "project" ? root : undefined);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
report(path, "unexpected root entry; expected personal/, project/, pi/session/ or dream artifacts");
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return issues;
|
|
97
|
+
}
|
package/src/history-tools.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Type } from "@earendil-works/pi-ai";
|
|
2
2
|
import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { output, outputRaw, page, middleTruncate, prefixFit, earliestMatchOffsetChars, readCharacterWindow,
|
|
3
|
+
import { output, outputRaw, page, middleTruncate, prefixFit, earliestMatchOffsetChars, readCharacterWindow, readWindowBlock, withinTextBudget, DEFAULT_READ_WINDOW_CHARS, HISTORY_PREVIEW_CHARS, MAX_READ_WINDOW_CHARS } from "./tool-output.js";
|
|
4
4
|
import { positiveInteger, recentFirst, nullableString, role, cursor, searchQuery, searchQueries } from "./tool-schema.js";
|
|
5
5
|
import { historyFromSession, filteredItems, visibleItem, allItems, vacuousRoleToolCombo, unknownWindowId } from "./history.js";
|
|
6
6
|
|
|
@@ -61,7 +61,7 @@ export function registerHistoryTools(pi: ExtensionAPI) {
|
|
|
61
61
|
pi.registerTool(defineTool({
|
|
62
62
|
name: "history_read",
|
|
63
63
|
label: "History read item",
|
|
64
|
-
description: "Read a bounded character range from one session item. Each response delivers the longest contiguous prefix of the requested window that fits the wire budget: follow the resume cursor to reconstruct the item exactly. A negative offset_chars counts back from the item's end. Offsets and counts are code points (an emoji or CJK character counts as one). The response
|
|
64
|
+
description: "Read a bounded character range from one session item. Each response delivers the longest contiguous prefix of the requested window that fits the wire budget: follow the resume cursor to reconstruct the item exactly. A negative offset_chars counts back from the item's end. Offsets and counts are code points (an emoji or CJK character counts as one). The response begins with the shared READ WINDOW block naming window_id and item_id; concatenate only the content after that block to reconstruct the item.",
|
|
65
65
|
parameters: Type.Object({ item_id: Type.String(), offset_chars: Type.Optional(Type.Integer({ description: "Code-point offset to start from. 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.` })), window_id: Type.String() }, { additionalProperties: false }),
|
|
66
66
|
async execute(_id, params, _signal, _update, ctx) {
|
|
67
67
|
const item = allItems(ctx).find((candidate) => candidate.windowId === params.window_id && candidate.itemId === params.item_id);
|
|
@@ -72,10 +72,9 @@ export function registerHistoryTools(pi: ExtensionAPI) {
|
|
|
72
72
|
if (typeof params.offset_chars === "number" && params.offset_chars > totalChars) {
|
|
73
73
|
return output({ error: `offset_chars ${params.offset_chars} is past the end: the item has ${totalChars} chars; the largest legal offset is ${totalChars} (an empty end-read)`, window_id: item.windowId, item_id: item.itemId, offset_chars: params.offset_chars, total_chars: totalChars });
|
|
74
74
|
}
|
|
75
|
-
const limit_chars = Math.min(params.limit_chars ?? DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS);
|
|
76
75
|
return readCharacterWindow(item.content, params.offset_chars, params.limit_chars, (window) => {
|
|
77
76
|
const { content, ...cursor } = window;
|
|
78
|
-
return outputRaw(
|
|
77
|
+
return outputRaw(readWindowBlock([["window_id", item.windowId], ["item_id", item.itemId]], window), content, { window_id: item.windowId, item_id: item.itemId, ...cursor });
|
|
79
78
|
}, (result) => withinTextBudget(result.content[0].text));
|
|
80
79
|
},
|
|
81
80
|
}));
|
package/src/notes/store.ts
CHANGED
|
@@ -220,19 +220,25 @@ export function editNote(ctx: ExtensionContext, vpath: string, scope: Scope, edi
|
|
|
220
220
|
return { meta, applied: operations.length, resolved_scope: scope, diff };
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
+
/** Normalize a parsed note exactly as a read does, including its access metadata mutation. */
|
|
224
|
+
function accessedMeta(meta: NoteMeta, scope: Scope, now: number): NoteMeta {
|
|
225
|
+
const next = { ...meta, scope };
|
|
226
|
+
next.last_accessed = now;
|
|
227
|
+
next.access_count = (typeof next.access_count === "number" ? next.access_count : 0) + 1;
|
|
228
|
+
return next;
|
|
229
|
+
}
|
|
230
|
+
|
|
223
231
|
/** Read a note and, as a side effect, bump last_accessed/access_count in the file. */
|
|
224
|
-
export function readNote(ctx: ExtensionContext, vpath: string, scope: Scope): { meta: NoteMeta; body: string; resolvedScope: Scope } | undefined {
|
|
232
|
+
export function readNote(ctx: ExtensionContext, vpath: string, scope: Scope): { meta: NoteMeta; body: string; text: string; resolvedScope: Scope } | undefined {
|
|
225
233
|
assertVirtualPath(vpath);
|
|
226
234
|
const path = physicalPath(scope, vpath, ctx);
|
|
227
235
|
if (!existsSync(path)) return undefined;
|
|
228
236
|
const now = Date.now();
|
|
229
|
-
const
|
|
230
|
-
meta
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
atomicWrite(path, serializeNote(meta, body));
|
|
235
|
-
return { meta, body, resolvedScope: scope };
|
|
237
|
+
const parsed = parseNote(readFileSync(path, "utf8"), now);
|
|
238
|
+
const meta = accessedMeta(parsed.meta, scope, now);
|
|
239
|
+
const text = serializeNote(meta, parsed.body);
|
|
240
|
+
atomicWrite(path, text);
|
|
241
|
+
return { meta, body: parsed.body, text, resolvedScope: scope };
|
|
236
242
|
}
|
|
237
243
|
|
|
238
244
|
/** Merged rows across homes, most recently updated first (address breaks ties). */
|
|
@@ -264,11 +270,12 @@ export function searchNotes(ctx: ExtensionContext, queries: string[], opts: { sc
|
|
|
264
270
|
if (matcher && !matcher.test(address)) continue;
|
|
265
271
|
const { meta, body } = parseNote(readFileSync(`${root}/${path}`, "utf8"));
|
|
266
272
|
meta.scope = scope;
|
|
273
|
+
const serializedBodyOffset = Array.from(serializeNote(accessedMeta(meta, scope, Date.now()), "")).length;
|
|
267
274
|
let baseChars = 0;
|
|
268
275
|
const matches: NoteMatch[] = [];
|
|
269
276
|
for (const [index, line] of body.split("\n").entries()) {
|
|
270
277
|
if (queries.some((query) => line.includes(query))) {
|
|
271
|
-
matches.push({ line: index + 1, text: line, offsetChars: baseChars + earliestMatchOffsetChars(line, queries) });
|
|
278
|
+
matches.push({ line: index + 1, text: line, offsetChars: serializedBodyOffset + baseChars + earliestMatchOffsetChars(line, queries) });
|
|
272
279
|
}
|
|
273
280
|
baseChars += Array.from(line).length + 1;
|
|
274
281
|
}
|
package/src/notes/tools.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Type } from "@earendil-works/pi-ai";
|
|
2
2
|
import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { localIso } from "./model.js";
|
|
4
|
-
import {
|
|
4
|
+
import { DEFAULT_READ_WINDOW_CHARS, MAX_READ_WINDOW_CHARS, middleTruncate, output, outputRaw, page, prefixFit, readCharacterWindow, readWindowBlock, withinTextBudget } from "../tool-output.js";
|
|
5
5
|
import { cursor, nullableString, positiveInteger, searchQueries, searchQuery } from "../tool-schema.js";
|
|
6
6
|
import { assertAddress } from "./address.js";
|
|
7
|
-
import {
|
|
7
|
+
import { type Origin } from "./frontmatter.js";
|
|
8
8
|
import { NoteError, editNote, listNotes, readNote, searchNotes, writeNote } from "./store.js";
|
|
9
9
|
|
|
10
10
|
const ORIGIN = Type.Optional(Type.Union([Type.Literal("user"), Type.Literal("self"), Type.Literal("external")], {
|
|
@@ -12,10 +12,6 @@ const ORIGIN = Type.Optional(Type.Union([Type.Literal("user"), Type.Literal("sel
|
|
|
12
12
|
}));
|
|
13
13
|
const ADDRESS_DESCRIPTION = "Address forms are bare `<vpath>` for this session, `@project/<vpath>` for this project's home, and `@personal/<vpath>` for the human's cross-project home. `@` means leaving home. Any other `@` prefix, or `@` inside a vpath, is a hard error: legal prefixes are `@project/` and `@personal/`; bare names are the session home. There is no cross-home fallback. Paths reject `..`, absolute paths, and backslashes.";
|
|
14
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
15
|
function failure(error: unknown) {
|
|
20
16
|
if (error instanceof NoteError) {
|
|
21
17
|
const payload: Record<string, unknown> = { error: error.message };
|
|
@@ -35,28 +31,28 @@ export function registerNotesTools(pi: ExtensionAPI) {
|
|
|
35
31
|
const content = params.content;
|
|
36
32
|
try {
|
|
37
33
|
const destination = assertAddress(params.address);
|
|
38
|
-
|
|
39
|
-
return output({ address: params.address,
|
|
34
|
+
writeNote(ctx, destination.path, content, { scope: destination.scope, origin: (params.origin ?? "self") as Origin, stale: params.stale });
|
|
35
|
+
return output({ address: params.address, written: true });
|
|
40
36
|
} catch (error) { return failure(error); }
|
|
41
37
|
},
|
|
42
38
|
}));
|
|
43
39
|
|
|
44
40
|
pi.registerTool(defineTool({
|
|
45
41
|
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
|
|
42
|
+
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 the address and a diff of what changed.`,
|
|
47
43
|
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
44
|
async execute(_id, params, _signal, _update, ctx) {
|
|
49
45
|
try {
|
|
50
46
|
const destination = assertAddress(params.address);
|
|
51
|
-
const {
|
|
52
|
-
return output({ address: params.address, applied,
|
|
47
|
+
const { applied, diff } = editNote(ctx, destination.path, destination.scope, params.edits, { origin: params.origin as Origin | undefined, stale: params.stale, replaceAll: params.replace_all });
|
|
48
|
+
return output({ address: params.address, applied, diff });
|
|
53
49
|
} catch (error) { return failure(error); }
|
|
54
50
|
},
|
|
55
51
|
}));
|
|
56
52
|
|
|
57
53
|
pi.registerTool(defineTool({
|
|
58
54
|
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
|
|
55
|
+
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 in the shared READ WINDOW block: concatenate only the content after the block to reconstruct the note.`,
|
|
60
56
|
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
57
|
async execute(_id, params, _signal, _update, ctx) {
|
|
62
58
|
let note: ReturnType<typeof readNote>;
|
|
@@ -65,27 +61,24 @@ export function registerNotesTools(pi: ExtensionAPI) {
|
|
|
65
61
|
note = readNote(ctx, destination.path, destination.scope);
|
|
66
62
|
} catch (error) { return failure(error); }
|
|
67
63
|
if (!note) return output({ error: "note not found", address: params.address });
|
|
68
|
-
const text =
|
|
64
|
+
const text = note.text;
|
|
69
65
|
const totalChars = Array.from(text).length;
|
|
70
66
|
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
67
|
return readCharacterWindow(text, params.offset_chars, params.limit_chars, (window) => {
|
|
75
68
|
const { content, ...rest } = window;
|
|
76
|
-
return outputRaw(
|
|
69
|
+
return outputRaw(readWindowBlock([["address", params.address]], window), content, { address: params.address, ...rest });
|
|
77
70
|
}, (result) => withinTextBudget(result.content[0].text));
|
|
78
71
|
},
|
|
79
72
|
}));
|
|
80
73
|
|
|
81
74
|
pi.registerTool(defineTool({
|
|
82
75
|
name: "notes_list", label: "Notes list",
|
|
83
|
-
description: `List note files as rows carrying address,
|
|
76
|
+
description: `List note files as rows carrying address, updated_at, and stale, 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
77
|
parameters: Type.Object({ pattern: nullableString(), cursor: cursor(), max_results: positiveInteger() }, { additionalProperties: false }),
|
|
85
78
|
async execute(_id, params, _signal, _update, ctx) {
|
|
86
79
|
let rows: ReturnType<typeof listNotes>;
|
|
87
80
|
try { rows = listNotes(ctx, { pattern: params.pattern ?? undefined }); } catch (error) { return failure(error); }
|
|
88
|
-
const files: Array<{ address: string;
|
|
81
|
+
const files: Array<{ address: string; stale: boolean; updated_at: string; address_truncated?: boolean }> = rows.map((row) => ({ address: row.address, stale: row.meta.stale, updated_at: localIso(row.meta.updated_at) }));
|
|
89
82
|
return output(page(files, params.cursor ?? 0, "files", params.max_results, (file, fits) => {
|
|
90
83
|
if (fits(file)) return file;
|
|
91
84
|
const address = middleTruncate(file.address, (candidate) => fits({ ...file, address: candidate, address_truncated: true }));
|
|
@@ -96,16 +89,16 @@ export function registerNotesTools(pi: ExtensionAPI) {
|
|
|
96
89
|
|
|
97
90
|
pi.registerTool(defineTool({
|
|
98
91
|
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
|
|
92
|
+
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. 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 (a code-point offset into the serialized note returned by notes_read, at the earliest query match), and truncated.`,
|
|
100
93
|
parameters: Type.Object({ query: searchQuery(), pattern: nullableString(), cursor: cursor(), max_matches_per_file: positiveInteger(), max_files: positiveInteger() }, { additionalProperties: false }),
|
|
101
94
|
async execute(_id, params, _signal, _update, ctx) {
|
|
102
95
|
const queries = searchQueries(params.query);
|
|
103
96
|
let rows: ReturnType<typeof searchNotes>;
|
|
104
97
|
try { rows = searchNotes(ctx, queries, { pattern: params.pattern ?? undefined }); } catch (error) { return failure(error); }
|
|
105
98
|
const maxPerFile = params.max_matches_per_file ?? Number.POSITIVE_INFINITY;
|
|
106
|
-
const result: Array<{ address: string;
|
|
107
|
-
const matches = row.matches.map((match) => ({ line: match.line, text: match.text, truncated: false,
|
|
108
|
-
return { address: row.address,
|
|
99
|
+
const result: Array<{ address: string; updated_at: string; stale: boolean; matches_total: number; matches: Array<{ line: number; text: string; truncated: boolean; offset_chars: number }>; address_truncated?: boolean }> = rows.map((row) => {
|
|
100
|
+
const matches = row.matches.map((match) => ({ line: match.line, text: match.text, truncated: false, offset_chars: match.offsetChars }));
|
|
101
|
+
return { address: row.address, updated_at: localIso(row.meta.updated_at), stale: row.meta.stale, matches_total: matches.length, matches: matches.slice(0, maxPerFile) };
|
|
109
102
|
});
|
|
110
103
|
const fitFile = (file: (typeof result)[number], fits: (candidate: (typeof result)[number]) => boolean) => {
|
|
111
104
|
if (fits(file)) return file;
|
package/src/reset-lifecycle.ts
CHANGED
|
@@ -12,7 +12,16 @@ export function registerResetLifecycle(pi: ExtensionAPI, options: {
|
|
|
12
12
|
isCurrentReset: (entryId: string, ctx: ExtensionContext) => boolean;
|
|
13
13
|
onReset: (entryId: string) => void;
|
|
14
14
|
}) {
|
|
15
|
-
type Attempt = {
|
|
15
|
+
type Attempt = {
|
|
16
|
+
completed: boolean;
|
|
17
|
+
explicit: boolean;
|
|
18
|
+
nextRequested: boolean;
|
|
19
|
+
continuationStarted: boolean;
|
|
20
|
+
sessionId: string;
|
|
21
|
+
settled: boolean;
|
|
22
|
+
wait: Promise<void>;
|
|
23
|
+
release: () => void;
|
|
24
|
+
};
|
|
16
25
|
type Request =
|
|
17
26
|
| { phase: "idle" }
|
|
18
27
|
| { phase: "requested" }
|
|
@@ -21,38 +30,39 @@ export function registerResetLifecycle(pi: ExtensionAPI, options: {
|
|
|
21
30
|
let handledEntry: string | undefined;
|
|
22
31
|
let active = true;
|
|
23
32
|
|
|
33
|
+
const release = (attempt: Attempt) => {
|
|
34
|
+
if (attempt.settled) return;
|
|
35
|
+
attempt.settled = true;
|
|
36
|
+
if (state.phase === "compacting" && state.attempt === attempt) {
|
|
37
|
+
state = { phase: "idle" };
|
|
38
|
+
handledEntry = undefined;
|
|
39
|
+
}
|
|
40
|
+
attempt.release();
|
|
41
|
+
};
|
|
24
42
|
const clear = () => {
|
|
43
|
+
if (state.phase === "compacting") release(state.attempt);
|
|
25
44
|
state = { phase: "idle" };
|
|
26
45
|
handledEntry = undefined;
|
|
27
46
|
};
|
|
28
47
|
const valid = (request: Attempt, ctx: ExtensionContext) =>
|
|
29
48
|
active && options.isEnabled() && state.phase === "compacting" && state.attempt === request && ctx.sessionManager.getSessionId() === request.sessionId;
|
|
30
49
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
pi.on("agent_settled", (_event, ctx) => {
|
|
47
|
-
if (!active || !options.isEnabled() || state.phase === "compacting" || !ctx.isIdle()) return;
|
|
48
|
-
if (state.phase !== "requested") return;
|
|
49
|
-
// One owner for requested resets. Consume the request before any external call;
|
|
50
|
-
// repeated settled events and reentrant callbacks are harmless.
|
|
51
|
-
const request: Attempt = { completed: false, sessionId: ctx.sessionManager.getSessionId(), explicit: true };
|
|
50
|
+
const begin = (ctx: ExtensionContext) => {
|
|
51
|
+
let releaseWait!: () => void;
|
|
52
|
+
const request: Attempt = {
|
|
53
|
+
completed: false,
|
|
54
|
+
explicit: true,
|
|
55
|
+
nextRequested: false,
|
|
56
|
+
continuationStarted: false,
|
|
57
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
58
|
+
settled: false,
|
|
59
|
+
wait: new Promise<void>((resolve) => { releaseWait = resolve; }),
|
|
60
|
+
release: () => releaseWait(),
|
|
61
|
+
};
|
|
52
62
|
state = { phase: "compacting", attempt: request };
|
|
53
63
|
const onError = (error: Error) => {
|
|
54
64
|
if (!valid(request, ctx)) return;
|
|
55
|
-
|
|
65
|
+
release(request);
|
|
56
66
|
// Do not retry from settled in a tight loop. A later prompt may trigger a
|
|
57
67
|
// native reset or explicitly request one.
|
|
58
68
|
ctx.ui.notify(`pi-context: reset did not complete (${error.message}). The conversation is retained; resume with another prompt.`, "warning");
|
|
@@ -61,19 +71,64 @@ export function registerResetLifecycle(pi: ExtensionAPI, options: {
|
|
|
61
71
|
ctx.compact({
|
|
62
72
|
onComplete: () => {
|
|
63
73
|
if (!valid(request, ctx)) return;
|
|
64
|
-
state = { phase: "idle" };
|
|
65
74
|
// session_compact only confirms the boundary. onComplete runs after
|
|
66
75
|
// Pi clears compaction state; sending inside the hook starts too early.
|
|
67
76
|
// A queued user prompt may already have started at compaction_end.
|
|
68
77
|
if (request.completed && ctx.isIdle() && !ctx.hasPendingMessages()) {
|
|
69
|
-
|
|
78
|
+
// The SDK detaches sendMessage, so own the next settled event before
|
|
79
|
+
// starting it. The originating agent_settled handler awaits wait.
|
|
80
|
+
if (request.continuationStarted) return;
|
|
81
|
+
request.continuationStarted = true;
|
|
82
|
+
try {
|
|
83
|
+
pi.sendMessage(options.continuation, { triggerTurn: true });
|
|
84
|
+
} catch (error) {
|
|
85
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
70
88
|
}
|
|
89
|
+
release(request);
|
|
71
90
|
},
|
|
72
91
|
onError,
|
|
73
92
|
});
|
|
74
93
|
} catch (error) {
|
|
75
94
|
onError(error instanceof Error ? error : new Error(String(error)));
|
|
76
95
|
}
|
|
96
|
+
return request;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// State is intentionally not resumed from a pending request: a loaded session must
|
|
100
|
+
// not execute work from a tool that belonged to a previous runtime or tree branch.
|
|
101
|
+
pi.on("session_start", () => { clear(); active = true; });
|
|
102
|
+
pi.on("session_shutdown", () => { clear(); active = false; });
|
|
103
|
+
pi.on("session_tree", clear);
|
|
104
|
+
|
|
105
|
+
pi.on("agent_end", (_event, ctx) => {
|
|
106
|
+
if (!active || !options.isEnabled()) return;
|
|
107
|
+
if (ctx.signal?.aborted) {
|
|
108
|
+
// Esc cancels the user's run. Do not reset or resurrect it at settled.
|
|
109
|
+
clear();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
114
|
+
if (!active || !options.isEnabled() || !ctx.isIdle()) return;
|
|
115
|
+
if (state.phase === "compacting" && state.attempt.continuationStarted) {
|
|
116
|
+
const preceding = state.attempt;
|
|
117
|
+
if (!preceding.nextRequested) {
|
|
118
|
+
release(preceding);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// This settled event belongs to the continuation started by preceding.
|
|
122
|
+
// If it requested another reset, retain preceding until that reset's own
|
|
123
|
+
// continuation settles. Its eventual nested handler only releases its own
|
|
124
|
+
// waiter, so it never awaits itself.
|
|
125
|
+
const next = begin(ctx);
|
|
126
|
+
return next.wait.then(() => release(preceding));
|
|
127
|
+
}
|
|
128
|
+
if (state.phase !== "requested") return;
|
|
129
|
+
// One owner for requested resets. Consume the request before any external call;
|
|
130
|
+
// repeated settled events and reentrant callbacks are harmless.
|
|
131
|
+
return begin(ctx).wait;
|
|
77
132
|
});
|
|
78
133
|
|
|
79
134
|
pi.on("session_before_compact", (event, ctx) => {
|
|
@@ -103,9 +158,15 @@ export function registerResetLifecycle(pi: ExtensionAPI, options: {
|
|
|
103
158
|
|
|
104
159
|
return {
|
|
105
160
|
request() {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
161
|
+
if (state.phase === "idle") {
|
|
162
|
+
state = { phase: "requested" };
|
|
163
|
+
return "rollover_requested";
|
|
164
|
+
}
|
|
165
|
+
if (state.phase === "compacting" && state.attempt.continuationStarted && !state.attempt.nextRequested) {
|
|
166
|
+
state.attempt.nextRequested = true;
|
|
167
|
+
return "rollover_requested";
|
|
168
|
+
}
|
|
169
|
+
return "rollover_already_pending";
|
|
109
170
|
},
|
|
110
171
|
clear,
|
|
111
172
|
};
|
package/src/tool-output.ts
CHANGED
|
@@ -115,16 +115,14 @@ export function readCharacterWindow<T>(text: string, offsetChars: number | undef
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* metadata (notes add their timestamps) inside the same brackets.
|
|
118
|
+
* Fixed metadata block preceding any raw character-window payload. Callers supply their
|
|
119
|
+
* source identity fields in wire order; range and continuation semantics are shared.
|
|
121
120
|
*/
|
|
122
|
-
export function
|
|
123
|
-
// The range end is offset + delivered count, never `total_chars`: a read resolved past the
|
|
124
|
-
// end delivers zero characters there, and the header must not render an inverted range.
|
|
121
|
+
export function readWindowBlock(identity: ReadonlyArray<readonly [string, string]>, window: CharacterWindow): string {
|
|
125
122
|
const end = window.offset_chars + Array.from(window.content).length;
|
|
126
|
-
const
|
|
127
|
-
|
|
123
|
+
const next = window.next_offset_chars === null ? "null" : String(window.next_offset_chars);
|
|
124
|
+
const fields = identity.map(([name, value]) => `${name}: ${value}`).join("\n");
|
|
125
|
+
return `--- READ WINDOW ---\n${fields}\nchars: [${window.offset_chars},${end}) of ${window.total_chars}\nnext_offset_chars: ${next}\n`;
|
|
128
126
|
}
|
|
129
127
|
|
|
130
128
|
/**
|
|
@@ -183,9 +181,8 @@ export function output(value: unknown, details?: unknown, terminate = false) {
|
|
|
183
181
|
}
|
|
184
182
|
|
|
185
183
|
/**
|
|
186
|
-
* Encode a prose payload as raw text:
|
|
187
|
-
* verbatim.
|
|
188
|
-
* `details` carries the slim metadata object and never duplicates the payload.
|
|
184
|
+
* Encode a prose payload as raw text: metadata prefix, a blank line, then the payload
|
|
185
|
+
* verbatim. `details` carries the slim metadata object and never duplicates the payload.
|
|
189
186
|
*/
|
|
190
187
|
export function outputRaw(header: string, content: string, details: unknown, terminate = false) {
|
|
191
188
|
return { content: [{ type: "text" as const, text: `${header}\n${content}` }], details, terminate };
|