pi-posthorse 0.10.39 → 0.10.40
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/package.json +4 -4
- package/src/files.ts +37 -5
- package/src/note-discovery.ts +20 -4
- package/src/note-guards.ts +15 -8
- package/src/note-provenance.ts +7 -3
- package/src/note-store.ts +42 -20
- package/src/notes.ts +13 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-posthorse",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.40",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Posthorse: fresh context, same journey. No-summary context rollover for Pi, with sparse budget reminders, handoffs, notes, and history.",
|
|
6
6
|
"keywords": [
|
|
@@ -71,9 +71,9 @@
|
|
|
71
71
|
}
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@earendil-works/pi-ai": "1.
|
|
75
|
-
"@earendil-works/pi-coding-agent": "1.
|
|
76
|
-
"@earendil-works/pi-tui": "1.
|
|
74
|
+
"@earendil-works/pi-ai": "1.7.0",
|
|
75
|
+
"@earendil-works/pi-coding-agent": "1.7.0",
|
|
76
|
+
"@earendil-works/pi-tui": "1.7.0",
|
|
77
77
|
"@types/node": "24.19.1",
|
|
78
78
|
"oxc-parser": "0.157.0",
|
|
79
79
|
"oxfmt": "0.79.0",
|
package/src/files.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
type FileHandle,
|
|
25
25
|
} from "node:fs/promises";
|
|
26
26
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
27
|
+
import { StringDecoder } from "node:string_decoder";
|
|
27
28
|
import { CONFIG_DIR_NAME, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
28
29
|
import { errorCode } from "./contracts.ts";
|
|
29
30
|
|
|
@@ -170,19 +171,50 @@ export async function* lfLines(
|
|
|
170
171
|
}
|
|
171
172
|
}
|
|
172
173
|
/** Git's heuristic: a NUL in the first 8,000 bytes marks binary content. */
|
|
173
|
-
|
|
174
|
+
const BINARY_SAMPLE_BYTES = 8_000;
|
|
175
|
+
/** Count UTF-16 code units, retaining only the requested page. Binary notes have no text window. */
|
|
176
|
+
export async function readNoteWindow(
|
|
177
|
+
path: string,
|
|
178
|
+
offset: number,
|
|
179
|
+
limit: number,
|
|
180
|
+
signal?: AbortSignal,
|
|
181
|
+
): Promise<{ readonly window: string; readonly total: number } | undefined> {
|
|
182
|
+
signal?.throwIfAborted();
|
|
174
183
|
const file = await open(path, "r");
|
|
175
184
|
try {
|
|
176
|
-
const
|
|
177
|
-
|
|
185
|
+
const buffer = Buffer.alloc(64 * 1024);
|
|
186
|
+
const decoder = new StringDecoder("utf8");
|
|
187
|
+
let bytesSeen = 0;
|
|
188
|
+
let total = 0;
|
|
189
|
+
let window = "";
|
|
190
|
+
while (true) {
|
|
191
|
+
const { bytesRead } = await file.read(buffer);
|
|
192
|
+
signal?.throwIfAborted();
|
|
193
|
+
const bytes = buffer.subarray(0, bytesRead);
|
|
194
|
+
if (
|
|
195
|
+
bytesSeen < BINARY_SAMPLE_BYTES &&
|
|
196
|
+
bytes.subarray(0, BINARY_SAMPLE_BYTES - bytesSeen).includes(0)
|
|
197
|
+
) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
bytesSeen += bytesRead;
|
|
201
|
+
const text = bytesRead === 0 ? decoder.end() : decoder.write(bytes);
|
|
202
|
+
if (total < offset + limit && total + text.length > offset) {
|
|
203
|
+
window += text.slice(Math.max(0, offset - total), offset + limit - total);
|
|
204
|
+
}
|
|
205
|
+
total += text.length;
|
|
206
|
+
if (bytesRead === 0) {
|
|
207
|
+
return { window, total };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
178
210
|
} finally {
|
|
179
211
|
await file.close();
|
|
180
212
|
}
|
|
181
213
|
}
|
|
182
|
-
/** A whole file as UTF-8 text, or undefined
|
|
214
|
+
/** A whole file as UTF-8 text, or undefined for binary content. */
|
|
183
215
|
export async function readText(path: string, signal?: AbortSignal): Promise<string | undefined> {
|
|
184
216
|
const bytes = await readFile(path, { signal });
|
|
185
|
-
return bytes.subarray(0,
|
|
217
|
+
return bytes.subarray(0, BINARY_SAMPLE_BYTES).includes(0) ? undefined : bytes.toString("utf8");
|
|
186
218
|
}
|
|
187
219
|
function gitRoot(dir: string, marker: string): string {
|
|
188
220
|
let target = marker;
|
package/src/note-discovery.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
sameIdentity,
|
|
13
13
|
} from "./note-snapshots.ts";
|
|
14
14
|
import {
|
|
15
|
+
bindingsOf,
|
|
15
16
|
sameScope,
|
|
16
17
|
scopeDirectory,
|
|
17
18
|
type ForkBinding,
|
|
@@ -234,17 +235,33 @@ export class NoteDiscovery {
|
|
|
234
235
|
}
|
|
235
236
|
return selected;
|
|
236
237
|
}
|
|
237
|
-
async #references(
|
|
238
|
+
async #references(
|
|
239
|
+
session: SessionNotes,
|
|
240
|
+
options: DiscoveryOptions,
|
|
241
|
+
notice: (message: string) => void,
|
|
242
|
+
): Promise<DiscoveredNote[]> {
|
|
238
243
|
if (session.binding.kind !== "fork" || options.scope !== undefined) {
|
|
239
244
|
return [];
|
|
240
245
|
}
|
|
241
246
|
const recovery = session.recovery;
|
|
247
|
+
let sourceEntries: readonly EntryLike[];
|
|
248
|
+
try {
|
|
249
|
+
sourceEntries = await session.ancestorEntries(options.signal);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
options.signal?.throwIfAborted();
|
|
252
|
+
notice(
|
|
253
|
+
`Ancestor note references unavailable: ${error instanceof Error ? error.message : String(error)}`,
|
|
254
|
+
);
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
242
257
|
const { touches } = sessionNoteTouches({
|
|
243
258
|
branch: session.branch,
|
|
244
259
|
allEntries: recovery.entries,
|
|
245
260
|
projected: session.projection,
|
|
246
261
|
retained: new Set(),
|
|
247
|
-
directories: new Set(
|
|
262
|
+
directories: new Set(
|
|
263
|
+
[...session.scopes, ...bindingsOf(sourceEntries)].map(scopeDirectory),
|
|
264
|
+
),
|
|
248
265
|
});
|
|
249
266
|
const paths = new Set(touches.map((touch) => touch.notePath));
|
|
250
267
|
const candidates = [...paths].filter(
|
|
@@ -256,7 +273,6 @@ export class NoteDiscovery {
|
|
|
256
273
|
if (candidates.length === 0) {
|
|
257
274
|
return [];
|
|
258
275
|
}
|
|
259
|
-
const sourceEntries = await session.ancestorEntries(options.signal);
|
|
260
276
|
const resolved = await mapBatch(candidates, (path) =>
|
|
261
277
|
readReference(session, path, options.signal, sourceEntries),
|
|
262
278
|
);
|
|
@@ -284,7 +300,7 @@ export class NoteDiscovery {
|
|
|
284
300
|
manifest: await readManifest(requestedScope),
|
|
285
301
|
},
|
|
286
302
|
])(),
|
|
287
|
-
this.#references(session, options),
|
|
303
|
+
this.#references(session, options, notice),
|
|
288
304
|
]);
|
|
289
305
|
const scoped = await mapBatch(scopes, async (scope) => {
|
|
290
306
|
const scopeNotices: string[] = [];
|
package/src/note-guards.ts
CHANGED
|
@@ -40,7 +40,13 @@ async function noteAddress(path: string, session: SessionNotes): Promise<NoteAdd
|
|
|
40
40
|
if (known !== undefined) {
|
|
41
41
|
return known;
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
// An unavailable ancestor cannot identify additional notes; ordinary file tools still run.
|
|
44
|
+
const scopes = await session.ancestorScopes().catch(() => []);
|
|
45
|
+
const ancestor = scopes.length === 0 ? undefined : addressOf(path, scopes);
|
|
46
|
+
if (ancestor !== undefined) {
|
|
47
|
+
return ancestor;
|
|
48
|
+
}
|
|
49
|
+
for (const { root } of [...session.scopes, ...scopes]) {
|
|
44
50
|
const parts = relative(root, canonicalPath(path)).split(sep);
|
|
45
51
|
if (
|
|
46
52
|
parts[0] !== ".forks" ||
|
|
@@ -121,6 +127,11 @@ async function recordedFiles(
|
|
|
121
127
|
export function registerNoteGuards(pi: ExtensionAPI, store: NotesStore): void {
|
|
122
128
|
const calls = new Map<string, CapturedCall>();
|
|
123
129
|
pi.on("tool_call", async (event, ctx) => {
|
|
130
|
+
const call = store.startCall(event.toolCallId, event.parentToolCallId);
|
|
131
|
+
const operations = noteOperations(event.toolName, event.input);
|
|
132
|
+
if (event.toolName === "notes" || operations.length === 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
124
135
|
let session: SessionNotes;
|
|
125
136
|
try {
|
|
126
137
|
session = await store.get(ctx);
|
|
@@ -128,19 +139,15 @@ export function registerNoteGuards(pi: ExtensionAPI, store: NotesStore): void {
|
|
|
128
139
|
// Failed notes initialization skips bookkeeping; the notes tool reports the error itself.
|
|
129
140
|
return;
|
|
130
141
|
}
|
|
131
|
-
const call = session.startCall(event.toolCallId, event.parentToolCallId);
|
|
132
142
|
const captured = { session, call, cwd: ctx.cwd, tool: event.toolName };
|
|
133
143
|
calls.set(event.toolCallId, captured);
|
|
134
|
-
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
for (const operation of noteOperations(event.toolName, event.input)) {
|
|
144
|
+
for (const operation of operations) {
|
|
138
145
|
await prepareOperation(operation, captured);
|
|
139
146
|
}
|
|
140
147
|
});
|
|
141
148
|
pi.on("tool_result", async (event) => {
|
|
142
149
|
const captured = calls.get(event.toolCallId);
|
|
143
|
-
if (captured === undefined
|
|
150
|
+
if (captured === undefined) {
|
|
144
151
|
return;
|
|
145
152
|
}
|
|
146
153
|
const details: unknown = event.details;
|
|
@@ -151,7 +158,7 @@ export function registerNoteGuards(pi: ExtensionAPI, store: NotesStore): void {
|
|
|
151
158
|
captured.session.record(await recordedFiles(operations, captured), captured.call);
|
|
152
159
|
});
|
|
153
160
|
pi.on("tool_execution_end", (event) => {
|
|
154
|
-
|
|
161
|
+
store.finishCall(event.toolCallId);
|
|
155
162
|
calls.delete(event.toolCallId);
|
|
156
163
|
});
|
|
157
164
|
}
|
package/src/note-provenance.ts
CHANGED
|
@@ -278,13 +278,17 @@ export async function loadNotesBinding(
|
|
|
278
278
|
const own = bindingsOf(manager.getEntries()).findLast((binding) =>
|
|
279
279
|
sameOwner(binding.owner, owner),
|
|
280
280
|
);
|
|
281
|
+
if (own !== undefined) {
|
|
282
|
+
return {
|
|
283
|
+
binding: own,
|
|
284
|
+
source: { entries: [], file: header.parentSession },
|
|
285
|
+
created: false,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
281
288
|
const source: NoteSource = {
|
|
282
289
|
entries: await sourceEntries(header.parentSession),
|
|
283
290
|
file: header.parentSession,
|
|
284
291
|
};
|
|
285
|
-
if (own !== undefined) {
|
|
286
|
-
return { binding: own, source, created: false };
|
|
287
|
-
}
|
|
288
292
|
const inherited = bindingsOf([...manager.getEntries(), ...source.entries]);
|
|
289
293
|
const fork = header.parentSession !== undefined || inherited.length > 0;
|
|
290
294
|
const binding: NoteBinding = fork
|
package/src/note-store.ts
CHANGED
|
@@ -50,7 +50,7 @@ export class SessionNotes {
|
|
|
50
50
|
readonly #source: NoteSource;
|
|
51
51
|
readonly #owned: Set<string>;
|
|
52
52
|
readonly scopes: readonly NoteScope[];
|
|
53
|
-
|
|
53
|
+
#ancestorScopes: Promise<readonly NoteScope[]> | undefined;
|
|
54
54
|
readonly #revisions = new Map<string, number>();
|
|
55
55
|
constructor(pi: NotesApi, binding: NoteBinding, manager: NotesManager, source: NoteSource) {
|
|
56
56
|
this.#pi = pi;
|
|
@@ -80,17 +80,6 @@ export class SessionNotes {
|
|
|
80
80
|
get recovery(): NotesRecovery {
|
|
81
81
|
return { binding: this.binding, entries: this.#manager.getEntries() };
|
|
82
82
|
}
|
|
83
|
-
startCall(id: string, parent: string | undefined): NoteCall {
|
|
84
|
-
const rootId = parent === undefined ? id : (this.#calls.get(parent) ?? parent);
|
|
85
|
-
this.#calls.set(id, rootId);
|
|
86
|
-
return { id, rootId };
|
|
87
|
-
}
|
|
88
|
-
finishCall(id: string): void {
|
|
89
|
-
this.#calls.delete(id);
|
|
90
|
-
}
|
|
91
|
-
call(id: string): NoteCall {
|
|
92
|
-
return { id, rootId: this.#calls.get(id) ?? id };
|
|
93
|
-
}
|
|
94
83
|
/** A project session owns its whole scope; a fork owns recorded paths and files it already has. */
|
|
95
84
|
#ownsPath(path: string): boolean {
|
|
96
85
|
return (
|
|
@@ -120,11 +109,22 @@ export class SessionNotes {
|
|
|
120
109
|
files: normalized,
|
|
121
110
|
} satisfies NoteTouchRecord);
|
|
122
111
|
}
|
|
112
|
+
async ancestorScopes(): Promise<readonly NoteScope[]> {
|
|
113
|
+
if (this.#source.file === undefined || this.#source.entries.length > 0) {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
this.#ancestorScopes ??= sourceEntries(this.#source.file)
|
|
117
|
+
.then(bindingsOf)
|
|
118
|
+
.catch((error: unknown) => {
|
|
119
|
+
this.#ancestorScopes = undefined;
|
|
120
|
+
throw error;
|
|
121
|
+
});
|
|
122
|
+
return this.#ancestorScopes;
|
|
123
|
+
}
|
|
123
124
|
async selectScope(selector: string): Promise<NoteScope> {
|
|
124
125
|
const directory = canonicalPath(selector);
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
);
|
|
126
|
+
const matches = (scope: NoteScope) => canonicalPath(scopeDirectory(scope)) === directory;
|
|
127
|
+
const known = this.scopes.find(matches);
|
|
128
128
|
if (known !== undefined) {
|
|
129
129
|
return known;
|
|
130
130
|
}
|
|
@@ -136,6 +136,10 @@ export class SessionNotes {
|
|
|
136
136
|
) {
|
|
137
137
|
return manifest;
|
|
138
138
|
}
|
|
139
|
+
const ancestor = (await this.ancestorScopes()).find(matches);
|
|
140
|
+
if (ancestor !== undefined) {
|
|
141
|
+
return ancestor;
|
|
142
|
+
}
|
|
139
143
|
throw new Error(
|
|
140
144
|
`Unknown notes scope ${JSON.stringify(selector)}. Omit scope for this session's notes, or copy a scope selector from notes list/search.`,
|
|
141
145
|
);
|
|
@@ -259,12 +263,16 @@ function sessionKey(ctx: ExtensionContext): string {
|
|
|
259
263
|
export type NotesStore = {
|
|
260
264
|
readonly get: (ctx: ExtensionContext) => Promise<SessionNotes>;
|
|
261
265
|
readonly recovery: (ctx: ExtensionContext) => NotesRecovery | undefined;
|
|
266
|
+
readonly startCall: (id: string, parent: string | undefined) => NoteCall;
|
|
267
|
+
readonly finishCall: (id: string) => void;
|
|
268
|
+
readonly call: (id: string) => NoteCall;
|
|
262
269
|
};
|
|
263
270
|
type SessionState =
|
|
264
271
|
| { readonly kind: "initializing"; readonly value: Promise<SessionNotes> }
|
|
265
272
|
| { readonly kind: "ready"; readonly value: SessionNotes };
|
|
266
273
|
export function createNotesStore(pi: NotesApi): NotesStore {
|
|
267
274
|
const sessions = new Map<string, SessionState>();
|
|
275
|
+
const calls = new Map<string, string>();
|
|
268
276
|
|
|
269
277
|
const get = (ctx: ExtensionContext): Promise<SessionNotes> => {
|
|
270
278
|
const key = sessionKey(ctx);
|
|
@@ -281,22 +289,36 @@ export function createNotesStore(pi: NotesApi): NotesStore {
|
|
|
281
289
|
?.root ?? canonicalPath(notesDir(ctx.cwd));
|
|
282
290
|
const initialized = loadNotesBinding(ctx.sessionManager, root)
|
|
283
291
|
.then(({ binding, source, created }) => {
|
|
284
|
-
if (created) {
|
|
285
|
-
pi.appendEntry(NOTES_BINDING, binding);
|
|
286
|
-
}
|
|
287
292
|
const session = new SessionNotes(pi, binding, ctx.sessionManager, source);
|
|
288
|
-
sessions.
|
|
293
|
+
if (sessions.get(key)?.value === initialized) {
|
|
294
|
+
if (created) {
|
|
295
|
+
pi.appendEntry(NOTES_BINDING, binding);
|
|
296
|
+
}
|
|
297
|
+
sessions.set(key, { kind: "ready", value: session });
|
|
298
|
+
}
|
|
289
299
|
return session;
|
|
290
300
|
})
|
|
291
301
|
.catch((error: unknown) => {
|
|
292
|
-
sessions.
|
|
302
|
+
if (sessions.get(key)?.value === initialized) {
|
|
303
|
+
sessions.delete(key);
|
|
304
|
+
}
|
|
293
305
|
throw error;
|
|
294
306
|
});
|
|
307
|
+
sessions.clear();
|
|
295
308
|
sessions.set(key, { kind: "initializing", value: initialized });
|
|
296
309
|
return initialized;
|
|
297
310
|
};
|
|
298
311
|
return {
|
|
299
312
|
get,
|
|
313
|
+
startCall(id, parent) {
|
|
314
|
+
const rootId = parent === undefined ? id : (calls.get(parent) ?? parent);
|
|
315
|
+
calls.set(id, rootId);
|
|
316
|
+
return { id, rootId };
|
|
317
|
+
},
|
|
318
|
+
finishCall(id) {
|
|
319
|
+
calls.delete(id);
|
|
320
|
+
},
|
|
321
|
+
call: (id) => ({ id, rootId: calls.get(id) ?? id }),
|
|
300
322
|
recovery: (ctx: ExtensionContext) => {
|
|
301
323
|
const state = sessions.get(sessionKey(ctx));
|
|
302
324
|
return state?.kind === "ready" ? state.value.recovery : undefined;
|
package/src/notes.ts
CHANGED
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
type PolicyContext,
|
|
22
22
|
} from "./contracts.ts";
|
|
23
23
|
import { textResult, type TextResult } from "./ui/display.ts";
|
|
24
|
-
import {
|
|
24
|
+
import { notesDir, publishFile, readNoteWindow, readText } from "./files.ts";
|
|
25
25
|
import { notePath, scopeDirectory } from "./note-scopes.ts";
|
|
26
26
|
import type { NoteCall, NotesStore, SessionNotes } from "./note-store.ts";
|
|
27
27
|
import { NoteDiscovery, noteLabel } from "./note-discovery.ts";
|
|
@@ -108,17 +108,18 @@ async function readNote(
|
|
|
108
108
|
access,
|
|
109
109
|
);
|
|
110
110
|
}
|
|
111
|
-
|
|
111
|
+
const offset = params.offset ?? 0;
|
|
112
|
+
const page = await readNoteWindow(path, offset, MAX_PAGE_CHARS, access.signal);
|
|
113
|
+
if (page === undefined) {
|
|
112
114
|
throw new Error(
|
|
113
115
|
`${relative} is a binary file (${formatSize(info.size)}); notes read returns text only.`,
|
|
114
116
|
);
|
|
115
117
|
}
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
if (offset > 0 && offset >= text.length) {
|
|
118
|
+
const { window, total } = page;
|
|
119
|
+
if (offset > 0 && offset >= total) {
|
|
119
120
|
return pages.deliver(access.host(), {}, () =>
|
|
120
121
|
textResult(
|
|
121
|
-
`Offset ${offset} is past the end of ${relative} (${
|
|
122
|
+
`Offset ${offset} is past the end of ${relative} (${total} chars). Restart with offset 0.`,
|
|
122
123
|
),
|
|
123
124
|
);
|
|
124
125
|
}
|
|
@@ -127,18 +128,18 @@ async function readNote(
|
|
|
127
128
|
? noteReadHeader(path)
|
|
128
129
|
: `Read-only live reference: notes read ${JSON.stringify({ scope: scopeDirectory(target.address), path: target.address.path })}\n`;
|
|
129
130
|
return pages.deliver(access.host(), { headerChars: header.length }, (chars) => {
|
|
130
|
-
const end = Math.min(
|
|
131
|
+
const end = Math.min(total, offset + chars);
|
|
131
132
|
const more =
|
|
132
|
-
end <
|
|
133
|
-
? `\n[chars ${offset}-${end} of ${
|
|
133
|
+
end < total
|
|
134
|
+
? `\n[chars ${offset}-${end} of ${total}; continue with offset ${end}]`
|
|
134
135
|
: "";
|
|
135
136
|
session.record([{ ...target.address, kind: "read" }], access.call);
|
|
136
|
-
return textResult(`${header}${
|
|
137
|
+
return textResult(`${header}${window.slice(0, chars)}${more}`, [], {
|
|
137
138
|
kind: "note-read",
|
|
138
139
|
headerLength: header.length,
|
|
139
140
|
offset,
|
|
140
141
|
end,
|
|
141
|
-
total
|
|
142
|
+
total,
|
|
142
143
|
});
|
|
143
144
|
});
|
|
144
145
|
}
|
|
@@ -353,7 +354,7 @@ export function registerNotes(
|
|
|
353
354
|
host: () => policy(ctx),
|
|
354
355
|
pages,
|
|
355
356
|
signal,
|
|
356
|
-
call:
|
|
357
|
+
call: store.call(id),
|
|
357
358
|
discovery,
|
|
358
359
|
};
|
|
359
360
|
switch (params.op) {
|