pi-posthorse 0.10.28 → 0.10.30
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/index.ts +8 -16
- package/package.json +1 -1
- package/src/boundary.ts +12 -10
- package/src/budget.ts +12 -8
- package/src/context-tools.ts +6 -12
- package/src/contracts.ts +27 -15
- package/src/files.ts +9 -11
- package/src/history-cursor.ts +1 -1
- package/src/history-entries.ts +7 -6
- package/src/history-pages.ts +1 -2
- package/src/history-search.ts +39 -11
- package/src/history.ts +2 -42
- package/src/host.ts +2 -9
- package/src/message-text.ts +11 -8
- package/src/note-discovery.ts +12 -13
- package/src/note-guards.ts +0 -3
- package/src/note-inventory.ts +3 -2
- package/src/note-provenance.ts +2 -2
- package/src/note-scopes.ts +4 -14
- package/src/note-snapshots.ts +11 -24
- package/src/note-store.ts +11 -2
- package/src/notes.ts +10 -23
- package/src/paging.ts +5 -8
- package/src/recovery-inputs.ts +3 -3
- package/src/recovery-note-touches.ts +22 -7
- package/src/recovery-notes.ts +24 -21
- package/src/recovery-records.ts +4 -4
- package/src/recovery.ts +23 -22
- package/src/reminders.ts +3 -89
- package/src/retained-tail.ts +75 -1
- package/src/retention.ts +4 -15
- package/src/rollover.ts +5 -32
- package/src/session-files.ts +6 -14
- package/src/ui/cards.ts +2 -3
- package/src/ui/display.ts +37 -75
- package/src/ui/text.ts +1 -3
- package/ui.ts +0 -1
- package/src/records.ts +0 -3
package/src/note-discovery.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readdir, stat } from "node:fs/promises";
|
|
2
2
|
import { basename, join, relative } from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import { errorCode, type EntryLike } from "./contracts.ts";
|
|
4
4
|
import { isPublicationTemporaryName } from "./files.ts";
|
|
5
5
|
import { sessionNoteTouches } from "./note-evidence.ts";
|
|
6
6
|
import { notesUnder, type NoteFile } from "./note-inventory.ts";
|
|
@@ -49,7 +49,7 @@ async function projectScopes(
|
|
|
49
49
|
];
|
|
50
50
|
const directory = join(session.binding.root, ".forks");
|
|
51
51
|
const entries = await readdir(directory, { withFileTypes: true }).catch((error: unknown) => {
|
|
52
|
-
if (
|
|
52
|
+
if (errorCode(error) === "ENOENT") {
|
|
53
53
|
return [];
|
|
54
54
|
}
|
|
55
55
|
throw error;
|
|
@@ -102,7 +102,7 @@ async function scopedFiles(
|
|
|
102
102
|
function noteKey(address: NoteAddress): string {
|
|
103
103
|
return JSON.stringify([address.root, address.scope, address.path]);
|
|
104
104
|
}
|
|
105
|
-
|
|
105
|
+
function noteReadTarget(note: DiscoveredNote): string {
|
|
106
106
|
return JSON.stringify({ scope: scopeDirectory(note.address), path: note.address.path });
|
|
107
107
|
}
|
|
108
108
|
export function noteLabel(note: DiscoveredNote, session: SessionNotes, labeled: boolean): string {
|
|
@@ -120,16 +120,17 @@ async function readReference(
|
|
|
120
120
|
session: SessionNotes,
|
|
121
121
|
path: string,
|
|
122
122
|
signal: AbortSignal | undefined,
|
|
123
|
+
sourceEntries: readonly EntryLike[],
|
|
123
124
|
): Promise<DiscoveredNote | undefined> {
|
|
124
125
|
if (isPublicationTemporaryName(basename(path))) {
|
|
125
126
|
return;
|
|
126
127
|
}
|
|
127
|
-
const target = await session.access(path, { signal });
|
|
128
|
+
const target = await session.access(path, { signal, sourceEntries });
|
|
128
129
|
if (target.kind === "own") {
|
|
129
130
|
return;
|
|
130
131
|
}
|
|
131
132
|
const info = await stat(target.path).catch((error: unknown) => {
|
|
132
|
-
if (
|
|
133
|
+
if (errorCode(error) === "ENOENT") {
|
|
133
134
|
return;
|
|
134
135
|
}
|
|
135
136
|
throw error;
|
|
@@ -193,13 +194,7 @@ export class NoteDiscovery {
|
|
|
193
194
|
session: SessionNotes,
|
|
194
195
|
options: ComparisonOptions,
|
|
195
196
|
): Promise<DiscoveredNote[]> {
|
|
196
|
-
const groups =
|
|
197
|
-
for (const file of files) {
|
|
198
|
-
const key = `${noteKey(file.lineage)}\0${file.size}`;
|
|
199
|
-
const group = groups.get(key) ?? [];
|
|
200
|
-
group.push(file);
|
|
201
|
-
groups.set(key, group);
|
|
202
|
-
}
|
|
197
|
+
const groups = Map.groupBy(files, (file) => `${noteKey(file.lineage)}\0${file.size}`);
|
|
203
198
|
const selected: DiscoveredNote[] = [];
|
|
204
199
|
for (const group of groups.values()) {
|
|
205
200
|
if (group.length === 1) {
|
|
@@ -258,8 +253,12 @@ export class NoteDiscovery {
|
|
|
258
253
|
path === options.path ||
|
|
259
254
|
path.startsWith(`${options.path}/`),
|
|
260
255
|
);
|
|
256
|
+
if (candidates.length === 0) {
|
|
257
|
+
return [];
|
|
258
|
+
}
|
|
259
|
+
const sourceEntries = await session.ancestorEntries(options.signal);
|
|
261
260
|
const resolved = await mapBatch(candidates, (path) =>
|
|
262
|
-
readReference(session, path, options.signal),
|
|
261
|
+
readReference(session, path, options.signal, sourceEntries),
|
|
263
262
|
);
|
|
264
263
|
return resolved.filter((file): file is DiscoveredNote => file !== undefined);
|
|
265
264
|
}
|
package/src/note-guards.ts
CHANGED
|
@@ -16,9 +16,6 @@ import type { NoteCall, NotesStore, SessionNotes } from "./note-store.ts";
|
|
|
16
16
|
/** Native core tools expand @/~ paths; pi-apply-edits deliberately treats them literally. */
|
|
17
17
|
function toolPath(tool: string, path: string, cwd: string): string {
|
|
18
18
|
if (tool !== "read" && tool !== "write" && tool !== "edit") {
|
|
19
|
-
if (process.platform === "win32") {
|
|
20
|
-
return resolve(cwd, path);
|
|
21
|
-
}
|
|
22
19
|
return isAbsolute(path) ? path : `${cwd}/${path}`;
|
|
23
20
|
}
|
|
24
21
|
let normalized = path
|
package/src/note-inventory.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readdir, realpath, stat } from "node:fs/promises";
|
|
2
2
|
import { basename, join } from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import { errorCode, presentText } from "./contracts.ts";
|
|
4
4
|
import { isPublicationTemporaryName } from "./files.ts";
|
|
5
5
|
import {
|
|
6
6
|
canonicalPath,
|
|
@@ -19,7 +19,8 @@ async function statIfPresent(path: string) {
|
|
|
19
19
|
try {
|
|
20
20
|
return await stat(path);
|
|
21
21
|
} catch (error) {
|
|
22
|
-
|
|
22
|
+
const code = errorCode(error);
|
|
23
|
+
if (code === "ENOENT" || code === "ENOTDIR") {
|
|
23
24
|
return;
|
|
24
25
|
}
|
|
25
26
|
throw error;
|
package/src/note-provenance.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { statSync } from "node:fs";
|
|
2
2
|
import { isAbsolute, join } from "node:path";
|
|
3
3
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import {
|
|
4
|
+
import { errorCode, type EntryLike } from "./contracts.ts";
|
|
5
5
|
import { sessionLines } from "./session-lines.ts";
|
|
6
6
|
import { parseSessionLine } from "./session-files.ts";
|
|
7
7
|
import { sessionNoteTouches } from "./note-evidence.ts";
|
|
@@ -51,7 +51,7 @@ async function journalMetadata(
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
} catch (error) {
|
|
54
|
-
if (
|
|
54
|
+
if (errorCode(error) !== "ENOENT") {
|
|
55
55
|
throw error;
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/note-scopes.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { realpathSync } from "node:fs";
|
|
|
2
2
|
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
3
|
import { Type, type Static } from "typebox";
|
|
4
4
|
import { Check } from "typebox/value";
|
|
5
|
-
import {
|
|
5
|
+
import { errorCode, type EntryLike, type Immutable } from "./contracts.ts";
|
|
6
6
|
|
|
7
7
|
export const NOTES_BINDING = "posthorse-notes-scope";
|
|
8
8
|
export const NOTES_TOUCH = "posthorse-note-touch";
|
|
@@ -50,21 +50,10 @@ const touchSchema = Type.Object({
|
|
|
50
50
|
Type.Object({
|
|
51
51
|
...addressSchema.properties,
|
|
52
52
|
seed: Type.Optional(seedSchema),
|
|
53
|
-
kind: Type.
|
|
54
|
-
Type.Literal("read"),
|
|
55
|
-
Type.Literal("write"),
|
|
56
|
-
Type.Literal("clear"),
|
|
57
|
-
Type.Literal("delete"),
|
|
58
|
-
Type.Literal("adopt"),
|
|
59
|
-
]),
|
|
53
|
+
kind: Type.Enum(["read", "write", "clear", "delete", "adopt"]),
|
|
60
54
|
}),
|
|
61
55
|
),
|
|
62
56
|
});
|
|
63
|
-
type Immutable<T> = T extends readonly (infer Item)[]
|
|
64
|
-
? readonly Immutable<Item>[]
|
|
65
|
-
: T extends object
|
|
66
|
-
? { readonly [Key in keyof T]: Immutable<T[Key]> }
|
|
67
|
-
: T;
|
|
68
57
|
export type NoteOwner = Immutable<Static<typeof ownerSchema>>;
|
|
69
58
|
export type NoteScope = Immutable<Static<typeof scopeSchema>>;
|
|
70
59
|
export type NoteAddress = Immutable<Static<typeof addressSchema>>;
|
|
@@ -149,7 +138,8 @@ export function canonicalPath(path: string): string {
|
|
|
149
138
|
try {
|
|
150
139
|
return realpathSync(absolute);
|
|
151
140
|
} catch (error) {
|
|
152
|
-
|
|
141
|
+
const code = errorCode(error);
|
|
142
|
+
if (code !== "ENOENT" && code !== "ENOTDIR") {
|
|
153
143
|
throw error;
|
|
154
144
|
}
|
|
155
145
|
const parent = dirname(absolute);
|
package/src/note-snapshots.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { constants } from "node:fs";
|
|
2
|
+
import { constants, createReadStream } from "node:fs";
|
|
3
3
|
import {
|
|
4
4
|
copyFile,
|
|
5
5
|
lstat,
|
|
6
6
|
mkdir,
|
|
7
7
|
mkdtemp,
|
|
8
|
-
open,
|
|
9
8
|
readFile,
|
|
10
9
|
rename,
|
|
11
10
|
rm,
|
|
@@ -14,8 +13,9 @@ import {
|
|
|
14
13
|
writeFile,
|
|
15
14
|
} from "node:fs/promises";
|
|
16
15
|
import { dirname, join } from "node:path";
|
|
16
|
+
import { pipeline } from "node:stream/promises";
|
|
17
17
|
import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
18
|
-
import {
|
|
18
|
+
import { errorCode } from "./contracts.ts";
|
|
19
19
|
import { publishFile } from "./files.ts";
|
|
20
20
|
import {
|
|
21
21
|
addressIn,
|
|
@@ -49,21 +49,10 @@ export function sameIdentity(left: NoteIdentity, right: NoteIdentity): boolean {
|
|
|
49
49
|
);
|
|
50
50
|
}
|
|
51
51
|
export async function fileDigest(path: string, signal?: AbortSignal): Promise<string> {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
signal?.throwIfAborted();
|
|
57
|
-
const bytes: unknown = chunk;
|
|
58
|
-
if (!(bytes instanceof Uint8Array)) {
|
|
59
|
-
throw new TypeError("Expected binary file data.");
|
|
60
|
-
}
|
|
61
|
-
hash.update(bytes);
|
|
62
|
-
}
|
|
63
|
-
return hash.digest("hex");
|
|
64
|
-
} finally {
|
|
65
|
-
await file.close();
|
|
66
|
-
}
|
|
52
|
+
signal?.throwIfAborted();
|
|
53
|
+
const hasher = createHash("sha256");
|
|
54
|
+
await pipeline(createReadStream(path, { signal }), hasher);
|
|
55
|
+
return hasher.digest("hex");
|
|
67
56
|
}
|
|
68
57
|
export async function readManifest(directory: string): Promise<ForkBinding | undefined> {
|
|
69
58
|
try {
|
|
@@ -75,10 +64,8 @@ export async function readManifest(directory: string): Promise<ForkBinding | und
|
|
|
75
64
|
? binding
|
|
76
65
|
: undefined;
|
|
77
66
|
} catch (error) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
(isRecord(error) && (error.code === "ENOENT" || error.code === "ENOTDIR"))
|
|
81
|
-
) {
|
|
67
|
+
const code = errorCode(error);
|
|
68
|
+
if (error instanceof SyntaxError || code === "ENOENT" || code === "ENOTDIR") {
|
|
82
69
|
return;
|
|
83
70
|
}
|
|
84
71
|
throw error;
|
|
@@ -165,7 +152,7 @@ async function prepareSnapshot(
|
|
|
165
152
|
const container = dirname(directory);
|
|
166
153
|
if (
|
|
167
154
|
await lstat(container).catch((error: unknown) => {
|
|
168
|
-
if (
|
|
155
|
+
if (errorCode(error) === "ENOENT") {
|
|
169
156
|
return;
|
|
170
157
|
}
|
|
171
158
|
throw error;
|
|
@@ -229,7 +216,7 @@ export async function ensureManifest(binding: NoteBinding): Promise<void> {
|
|
|
229
216
|
try {
|
|
230
217
|
await writeFile(path, JSON.stringify(binding), { flag: "wx" });
|
|
231
218
|
} catch (error) {
|
|
232
|
-
if (
|
|
219
|
+
if (errorCode(error) !== "EEXIST") {
|
|
233
220
|
throw error;
|
|
234
221
|
}
|
|
235
222
|
const existing = await readManifest(scopeDirectory(binding));
|
package/src/note-store.ts
CHANGED
|
@@ -145,26 +145,34 @@ export class SessionNotes {
|
|
|
145
145
|
options: {
|
|
146
146
|
readonly includeDirectories?: boolean | undefined;
|
|
147
147
|
readonly signal?: AbortSignal | undefined;
|
|
148
|
+
readonly sourceEntries?: readonly EntryLike[] | undefined;
|
|
148
149
|
} = {},
|
|
149
150
|
): Promise<NoteAddress | undefined> {
|
|
150
151
|
if (this.binding.kind !== "fork" || this.binding.source === null) {
|
|
151
152
|
return;
|
|
152
153
|
}
|
|
154
|
+
const live =
|
|
155
|
+
options.sourceEntries ?? (await sourceEntries(this.#source.file, options.signal));
|
|
153
156
|
const entries = [
|
|
154
157
|
...this.#manager.getEntries(),
|
|
155
|
-
...this.#source.entries,
|
|
156
|
-
...(await sourceEntries(this.#source.file, options.signal)),
|
|
158
|
+
...(live.length > 0 ? live : this.#source.entries),
|
|
157
159
|
];
|
|
158
160
|
return resolveReference(this.binding.source, path, entries, {
|
|
159
161
|
includeDirectories: options.includeDirectories,
|
|
160
162
|
});
|
|
161
163
|
}
|
|
164
|
+
async ancestorEntries(signal?: AbortSignal): Promise<readonly EntryLike[]> {
|
|
165
|
+
return this.binding.kind === "fork" && this.binding.source !== null
|
|
166
|
+
? sourceEntries(this.#source.file, signal)
|
|
167
|
+
: [];
|
|
168
|
+
}
|
|
162
169
|
async access(
|
|
163
170
|
path: string,
|
|
164
171
|
options: {
|
|
165
172
|
readonly scope?: string | undefined;
|
|
166
173
|
readonly includeDirectories?: boolean | undefined;
|
|
167
174
|
readonly signal?: AbortSignal | undefined;
|
|
175
|
+
readonly sourceEntries?: readonly EntryLike[] | undefined;
|
|
168
176
|
} = {},
|
|
169
177
|
): Promise<NoteAccess> {
|
|
170
178
|
if (options.scope !== undefined) {
|
|
@@ -187,6 +195,7 @@ export class SessionNotes {
|
|
|
187
195
|
const reference = await this.#reference(relative, {
|
|
188
196
|
includeDirectories: options.includeDirectories,
|
|
189
197
|
signal: options.signal,
|
|
198
|
+
sourceEntries: options.sourceEntries,
|
|
190
199
|
});
|
|
191
200
|
return reference === undefined
|
|
192
201
|
? {
|
package/src/notes.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
type ExtensionAPI,
|
|
8
8
|
type ExtensionContext,
|
|
9
9
|
} from "@earendil-works/pi-coding-agent";
|
|
10
|
-
import { Type } from "typebox";
|
|
10
|
+
import { Type, type Static } from "typebox";
|
|
11
11
|
import { toolCards } from "../ui.ts";
|
|
12
12
|
import {
|
|
13
13
|
MAX_PAGE_CHARS,
|
|
@@ -17,10 +17,10 @@ import {
|
|
|
17
17
|
notesDirectoryHeader,
|
|
18
18
|
presentText,
|
|
19
19
|
requireValue,
|
|
20
|
-
|
|
20
|
+
type Immutable,
|
|
21
21
|
type PolicyContext,
|
|
22
|
-
type TextResult,
|
|
23
22
|
} from "./contracts.ts";
|
|
23
|
+
import { textResult, type TextResult } from "./ui/display.ts";
|
|
24
24
|
import { isBinaryFile, notesDir, publishFile, readText } from "./files.ts";
|
|
25
25
|
import { notePath, scopeDirectory } from "./note-scopes.ts";
|
|
26
26
|
import type { NoteCall, NotesStore, SessionNotes } from "./note-store.ts";
|
|
@@ -28,14 +28,6 @@ import { NoteDiscovery, noteLabel } from "./note-discovery.ts";
|
|
|
28
28
|
import { excerpt, excerptAround } from "./message-text.ts";
|
|
29
29
|
import type { PageAccess } from "./paging.ts";
|
|
30
30
|
|
|
31
|
-
export type NotesParams = {
|
|
32
|
-
readonly op: "list" | "read" | "write" | "append" | "search";
|
|
33
|
-
readonly path?: string;
|
|
34
|
-
readonly content?: string;
|
|
35
|
-
readonly query?: string;
|
|
36
|
-
readonly offset?: number;
|
|
37
|
-
readonly scope?: string;
|
|
38
|
-
};
|
|
39
31
|
type NoteHits = { readonly rows: string[]; readonly more: boolean; readonly skipped: number };
|
|
40
32
|
/** Paged operations take a fresh policy snapshot after their filesystem awaits. */
|
|
41
33
|
type NotesAccess = {
|
|
@@ -297,16 +289,10 @@ function withoutEmptyScope(params: NotesParams): NotesParams {
|
|
|
297
289
|
return presentText(scope) === undefined ? unscoped : params;
|
|
298
290
|
}
|
|
299
291
|
const notesParameters = Type.Object({
|
|
300
|
-
op: Type.
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
Type.Literal("write"),
|
|
305
|
-
Type.Literal("append"),
|
|
306
|
-
Type.Literal("search"),
|
|
307
|
-
],
|
|
308
|
-
{ description: "Operation to perform" },
|
|
309
|
-
),
|
|
292
|
+
op: Type.Enum(["list", "read", "write", "append", "search"], {
|
|
293
|
+
type: "string",
|
|
294
|
+
description: "Operation to perform",
|
|
295
|
+
}),
|
|
310
296
|
path: Type.Optional(
|
|
311
297
|
Type.String({
|
|
312
298
|
description:
|
|
@@ -330,6 +316,7 @@ const notesParameters = Type.Object({
|
|
|
330
316
|
}),
|
|
331
317
|
),
|
|
332
318
|
});
|
|
319
|
+
export type NotesParams = Immutable<Static<typeof notesParameters>>;
|
|
333
320
|
export function registerNotes(
|
|
334
321
|
pi: ExtensionAPI,
|
|
335
322
|
policy: (ctx: ExtensionContext) => PolicyContext,
|
|
@@ -345,7 +332,7 @@ export function registerNotes(
|
|
|
345
332
|
"Durable task notes shared across Git worktrees and context resets; forks get private copies. Ops: list (newest first), read, search (case-insensitive), write (replace; empty clears), append (one record). list/search also cover other sessions' note scopes and show their selectors; pass scope to read, list, or search one of them. list/read/search are paged; continue with the returned offset. A read of a current note returns its file path.",
|
|
346
333
|
promptSnippet: "save and recall durable state with private notes for forks",
|
|
347
334
|
parameters: notesParameters,
|
|
348
|
-
async execute(...[
|
|
335
|
+
async execute(...[id, received, signal, , ctx]) {
|
|
349
336
|
const params = withoutEmptyScope(received);
|
|
350
337
|
// Migrate newly returned legacy package notes without changing this session’s binding.
|
|
351
338
|
notesDir(ctx.cwd);
|
|
@@ -359,7 +346,7 @@ export function registerNotes(
|
|
|
359
346
|
host: () => policy(ctx),
|
|
360
347
|
pages,
|
|
361
348
|
signal,
|
|
362
|
-
call: session.call(
|
|
349
|
+
call: session.call(id),
|
|
363
350
|
discovery,
|
|
364
351
|
};
|
|
365
352
|
switch (params.op) {
|
package/src/paging.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
+
import { estimateTokens } from "@earendil-works/pi-coding-agent";
|
|
1
2
|
import { pageCapacity } from "./budget.ts";
|
|
2
|
-
import { textOf } from "./message-text.ts";
|
|
3
3
|
import {
|
|
4
4
|
ESTIMATED_IMAGE_CHARS,
|
|
5
5
|
MAX_PAGE_CHARS,
|
|
6
6
|
MIN_PAGE_CHARS,
|
|
7
7
|
PAGE_MARGIN_TOKENS,
|
|
8
|
-
textResult,
|
|
9
8
|
type PolicyContext,
|
|
10
|
-
type TextResult,
|
|
11
9
|
} from "./contracts.ts";
|
|
10
|
+
import { textResult, type TextResult } from "./ui/display.ts";
|
|
12
11
|
|
|
13
12
|
/** What else the page carries: images share the budget, and the header comes out of the body. */
|
|
14
13
|
export type PageRequest = {
|
|
@@ -86,12 +85,10 @@ export class PageReservations {
|
|
|
86
85
|
private settle(ctx: PolicyContext): void {
|
|
87
86
|
this.accounting = reconcile(this.accounting, ctx.getContextUsage()?.tokens);
|
|
88
87
|
}
|
|
89
|
-
private reserve(
|
|
88
|
+
private reserve(content: TextResult["content"] | string): void {
|
|
90
89
|
this.accounting = withTokens(
|
|
91
90
|
this.accounting,
|
|
92
|
-
this.accounting.tokens +
|
|
93
|
-
Math.ceil(text.length / 4) +
|
|
94
|
-
imageCount * (ESTIMATED_IMAGE_CHARS / 4),
|
|
91
|
+
this.accounting.tokens + estimateTokens({ role: "user", content, timestamp: 0 }),
|
|
95
92
|
);
|
|
96
93
|
}
|
|
97
94
|
deliver(
|
|
@@ -100,7 +97,7 @@ export class PageReservations {
|
|
|
100
97
|
build: (chars: number) => TextResult,
|
|
101
98
|
): TextResult {
|
|
102
99
|
const result = build(this.size(ctx, request));
|
|
103
|
-
this.reserve(
|
|
100
|
+
this.reserve(result.content);
|
|
104
101
|
return result;
|
|
105
102
|
}
|
|
106
103
|
error(ctx: PolicyContext, message: string): never {
|
package/src/recovery-inputs.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { OWNER_QUESTION_TOOL, type EntryLike, type ProjectedEntry } from "./contracts.ts";
|
|
2
2
|
import { chainsAncestry, contextStart } from "./boundary.ts";
|
|
3
3
|
import { contextEdits } from "./history-entries.ts";
|
|
4
4
|
import { toolCalls } from "./message-text.ts";
|
|
@@ -70,7 +70,7 @@ function ownerRecords(
|
|
|
70
70
|
const record = recoveryRecord(
|
|
71
71
|
entry,
|
|
72
72
|
ownerQuestionRegistered &&
|
|
73
|
-
call?.name ===
|
|
73
|
+
call?.name === OWNER_QUESTION_TOOL &&
|
|
74
74
|
call.namespace === undefined,
|
|
75
75
|
);
|
|
76
76
|
return record === undefined ? [] : [record];
|
|
@@ -81,7 +81,7 @@ function anchors(records: readonly RecoveryRecord[]): {
|
|
|
81
81
|
others: RecoveryRecord[];
|
|
82
82
|
} {
|
|
83
83
|
const firstOwner = records.find((record) => record.label === "owner input") ?? records[0];
|
|
84
|
-
const latestOwner =
|
|
84
|
+
const latestOwner = records.findLast((record) => record.kind === "owner");
|
|
85
85
|
const latestOverall = records.at(-1);
|
|
86
86
|
const selected = new Set([firstOwner, latestOwner, latestOverall]);
|
|
87
87
|
return {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
+
import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent";
|
|
2
3
|
import {
|
|
3
4
|
NOTE_RECEIPT,
|
|
4
5
|
isRecord,
|
|
@@ -72,21 +73,35 @@ function normalizeRelPath(raw: string): string {
|
|
|
72
73
|
return invalid ? "" : cleaned;
|
|
73
74
|
}
|
|
74
75
|
|
|
76
|
+
const CONFIG_DIR_PATTERN = RegExp.escape(CONFIG_DIR_NAME);
|
|
77
|
+
const PACKAGE_NOTES_PATTERN = new RegExp(
|
|
78
|
+
`\\/${CONFIG_DIR_PATTERN}\\/package-notes\\/[0-9a-f]{64}\\/notes\\/(.+)$`,
|
|
79
|
+
"u",
|
|
80
|
+
);
|
|
81
|
+
const GLOBAL_NOTES_PATTERN = new RegExp(
|
|
82
|
+
`\\/${CONFIG_DIR_PATTERN}\\/(?:agent\\/)?notes\\/[^/]+\\/[^/]+\\/[^/]+\\/(.+)$`,
|
|
83
|
+
"u",
|
|
84
|
+
);
|
|
85
|
+
const RELATIVE_NOTES_PREFIX = new RegExp(`^(?:\\.\\/)?${CONFIG_DIR_PATTERN}\\/notes\\/`, "u");
|
|
86
|
+
|
|
75
87
|
function matchMarkerPath(slash: string): string | undefined {
|
|
76
|
-
for (const marker of [
|
|
88
|
+
for (const marker of [`/${CONFIG_DIR_NAME}/notes/`, "/posthorse-notes/"]) {
|
|
77
89
|
const idx = slash.lastIndexOf(marker);
|
|
78
90
|
if (idx >= 0) {
|
|
79
91
|
return slash.slice(idx + marker.length);
|
|
80
92
|
}
|
|
81
93
|
}
|
|
82
|
-
if (
|
|
83
|
-
|
|
94
|
+
if (
|
|
95
|
+
slash.startsWith(`${CONFIG_DIR_NAME}/notes/`) ||
|
|
96
|
+
slash.startsWith(`./${CONFIG_DIR_NAME}/notes/`)
|
|
97
|
+
) {
|
|
98
|
+
return slash.replace(RELATIVE_NOTES_PREFIX, "");
|
|
84
99
|
}
|
|
85
|
-
const pkgMatch =
|
|
100
|
+
const pkgMatch = PACKAGE_NOTES_PATTERN.exec(slash);
|
|
86
101
|
if (pkgMatch !== null) {
|
|
87
102
|
return pkgMatch[1];
|
|
88
103
|
}
|
|
89
|
-
return
|
|
104
|
+
return GLOBAL_NOTES_PATTERN.exec(slash)?.[1];
|
|
90
105
|
}
|
|
91
106
|
|
|
92
107
|
function absoluteSlashPath(slash: string): boolean {
|
|
@@ -95,7 +110,7 @@ function absoluteSlashPath(slash: string): boolean {
|
|
|
95
110
|
|
|
96
111
|
/** Receipt lines are a storage format: parse exactly what the notes tool emits. */
|
|
97
112
|
function receiptPattern(prefix: string): RegExp {
|
|
98
|
-
return new RegExp(`^${
|
|
113
|
+
return new RegExp(`^${RegExp.escape(prefix)}([^\\r\\n]+)$`, "mu");
|
|
99
114
|
}
|
|
100
115
|
const receiptPatterns: readonly (readonly [RegExp, ContentTouchKind])[] = [
|
|
101
116
|
[receiptPattern(NOTE_RECEIPT.wrote), "full-write"],
|
|
@@ -412,7 +427,7 @@ export function collectTouches(
|
|
|
412
427
|
readonly defaultDir?: string | undefined;
|
|
413
428
|
} {
|
|
414
429
|
const results = collectResultMap(entries);
|
|
415
|
-
const knownDirs = new Set(
|
|
430
|
+
const knownDirs = new Set(directories).union(learnNotesDirs(entries, results));
|
|
416
431
|
const handledResultIds = new Set<string>();
|
|
417
432
|
const touches: NoteTouch[] = [];
|
|
418
433
|
for (const item of entries) {
|
package/src/recovery-notes.ts
CHANGED
|
@@ -196,23 +196,7 @@ function sessionNotes(
|
|
|
196
196
|
sources.notes === undefined ? defaultDir : scopeDirectory(sources.notes.binding),
|
|
197
197
|
};
|
|
198
198
|
}
|
|
199
|
-
|
|
200
|
-
* The file path of the note that the record names first, when this session wrote one. The path is
|
|
201
|
-
* absolute whenever the notes directory is known, so the `read` tool opens it from any directory.
|
|
202
|
-
*/
|
|
203
|
-
export function primaryNotePath(
|
|
204
|
-
chainEntries: readonly EntryLike[],
|
|
205
|
-
allEntries: readonly EntryLike[],
|
|
206
|
-
projected: readonly ProjectedEntry[],
|
|
207
|
-
sources: NoteSources,
|
|
208
|
-
): string | undefined {
|
|
209
|
-
const { all, notesDirectory } = sessionNotes(chainEntries, allEntries, projected, sources);
|
|
210
|
-
const primary = all.find((note) => note.kind === "written");
|
|
211
|
-
return primary === undefined
|
|
212
|
-
? undefined
|
|
213
|
-
: (pathElsewhere(primary, notesDirectory) ?? primary.absPath ?? primary.notePath);
|
|
214
|
-
}
|
|
215
|
-
export function renderSessionNotes(
|
|
199
|
+
export function sessionNotesEvidence(
|
|
216
200
|
chainEntries: readonly EntryLike[],
|
|
217
201
|
allEntries: readonly EntryLike[],
|
|
218
202
|
projected: readonly ProjectedEntry[],
|
|
@@ -220,25 +204,44 @@ export function renderSessionNotes(
|
|
|
220
204
|
readonly span: RetainedSpan | undefined;
|
|
221
205
|
readonly maxChars: number;
|
|
222
206
|
},
|
|
223
|
-
): string | undefined {
|
|
207
|
+
): { readonly section: string | undefined; readonly primaryNotePath: string | undefined } {
|
|
224
208
|
const { span, maxChars } = options;
|
|
225
209
|
const { all, notesDirectory } = sessionNotes(chainEntries, allEntries, projected, options);
|
|
226
210
|
if (all.length === 0) {
|
|
227
|
-
return;
|
|
211
|
+
return { section: undefined, primaryNotePath: undefined };
|
|
228
212
|
}
|
|
229
213
|
const written = all.filter((note) => note.kind === "written").slice(0, MAX_WRITTEN_NOTES);
|
|
214
|
+
const primary = written[0];
|
|
215
|
+
const primaryNotePath =
|
|
216
|
+
primary === undefined
|
|
217
|
+
? undefined
|
|
218
|
+
: (pathElsewhere(primary, notesDirectory) ?? primary.absPath ?? primary.notePath);
|
|
230
219
|
const readOnly = all.filter((note) => note.kind === "read").slice(0, MAX_READ_NOTES);
|
|
231
220
|
const lines = [
|
|
232
221
|
...written.map((note) => formatWrittenNoteLine(note, span, notesDirectory)),
|
|
233
222
|
...readOnly.map((note) => formatReadNoteLine(note, span, notesDirectory)),
|
|
234
223
|
];
|
|
235
224
|
const header = "Task notes in this session:";
|
|
236
|
-
const action = formatNotesAction(
|
|
225
|
+
const action = formatNotesAction(primary, span, notesDirectory);
|
|
237
226
|
const selected: string[] = [];
|
|
238
227
|
for (const line of lines) {
|
|
239
228
|
if ([header, ...selected, line, action].join("\n").length <= maxChars) {
|
|
240
229
|
selected.push(line);
|
|
241
230
|
}
|
|
242
231
|
}
|
|
243
|
-
return
|
|
232
|
+
return {
|
|
233
|
+
section: selected.length === 0 ? undefined : [header, ...selected, action].join("\n"),
|
|
234
|
+
primaryNotePath,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
export function renderSessionNotes(
|
|
238
|
+
chainEntries: readonly EntryLike[],
|
|
239
|
+
allEntries: readonly EntryLike[],
|
|
240
|
+
projected: readonly ProjectedEntry[],
|
|
241
|
+
options: NoteSources & {
|
|
242
|
+
readonly span: RetainedSpan | undefined;
|
|
243
|
+
readonly maxChars: number;
|
|
244
|
+
},
|
|
245
|
+
): string | undefined {
|
|
246
|
+
return sessionNotesEvidence(chainEntries, allEntries, projected, options).section;
|
|
244
247
|
}
|
package/src/recovery-records.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isReminderType, type EntryLike } from "./contracts.ts";
|
|
2
|
-
import { boundedBlock, imageSummary, imagesOf
|
|
1
|
+
import { isReminderType, OWNER_QUESTION_TOOL, type EntryLike } from "./contracts.ts";
|
|
2
|
+
import { boundedBlock, contentText, imageSummary, imagesOf } from "./message-text.ts";
|
|
3
3
|
|
|
4
4
|
export const MAX_RECOVERY_RECORD_CHARS = 4_000;
|
|
5
5
|
export type RecoveryRecord = {
|
|
@@ -20,7 +20,7 @@ function ownerAnswer(entry: EntryLike, ownerQuestion: boolean): boolean {
|
|
|
20
20
|
ownerQuestion &&
|
|
21
21
|
entry.type === "message" &&
|
|
22
22
|
message?.role === "toolResult" &&
|
|
23
|
-
message.toolName ===
|
|
23
|
+
message.toolName === OWNER_QUESTION_TOOL &&
|
|
24
24
|
message.namespace === undefined &&
|
|
25
25
|
message.isError !== true
|
|
26
26
|
);
|
|
@@ -58,7 +58,7 @@ export function recoveryRecord(
|
|
|
58
58
|
return undefined;
|
|
59
59
|
}
|
|
60
60
|
const id = entry.id?.slice(0, 120) ?? "unknown";
|
|
61
|
-
const text =
|
|
61
|
+
const text = contentText(source.content).trim();
|
|
62
62
|
const summary =
|
|
63
63
|
source.label === "owner answer via ask_question"
|
|
64
64
|
? ""
|