pi-posthorse 0.10.25 → 0.10.27
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 +1 -1
- package/src/boundary.ts +8 -2
- package/src/contracts.ts +45 -1
- package/src/files.ts +6 -0
- package/src/guidance.ts +46 -8
- package/src/history-entries.ts +5 -1
- package/src/history-pages.ts +62 -56
- package/src/history-search.ts +1 -0
- package/src/note-discovery.ts +12 -6
- package/src/note-evidence.ts +42 -15
- package/src/note-guards.ts +8 -5
- package/src/note-inventory.ts +18 -9
- package/src/note-provenance.ts +5 -5
- package/src/note-scopes.ts +13 -5
- package/src/note-snapshots.ts +1 -0
- package/src/note-store.ts +9 -5
- package/src/notes.ts +22 -19
- package/src/paging.ts +41 -36
- package/src/recovery-inputs.ts +4 -1
- package/src/recovery-note-touches.ts +39 -14
- package/src/recovery-notes.ts +2 -2
- package/src/recovery.ts +6 -3
- package/src/reminders.ts +30 -7
- package/src/reset-requests.ts +2 -0
- package/src/retained-tail.ts +85 -1
- package/src/rollover.ts +40 -11
- package/src/session-lines.ts +1 -0
- package/ui.ts +3 -2
package/package.json
CHANGED
package/src/boundary.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AUTO_HANDOFF_PREFIX,
|
|
3
|
+
POSTHORSE_COMPACTION,
|
|
4
|
+
isRecord,
|
|
5
|
+
type EntryLike,
|
|
6
|
+
type ProjectedEntry,
|
|
7
|
+
} from "./contracts.ts";
|
|
2
8
|
|
|
3
9
|
export type BoundaryKind = "fork" | "native" | "automatic" | "explicit" | "posthorse";
|
|
4
10
|
/** Everything the app derives from one persisted context boundary, decided in one place. */
|
|
@@ -50,7 +56,7 @@ export function classifyBoundary(entry?: EntryLike): Boundary | undefined {
|
|
|
50
56
|
}
|
|
51
57
|
const details = isRecord(entry.details) ? entry.details : undefined;
|
|
52
58
|
const freshGuidance = !providerSummaryBlock(entry);
|
|
53
|
-
if (details?.posthorse !==
|
|
59
|
+
if (details?.posthorse !== POSTHORSE_COMPACTION) {
|
|
54
60
|
return { kind: "native", window: false, chainsAncestry: false, freshGuidance };
|
|
55
61
|
}
|
|
56
62
|
const kind = compactionKind(details);
|
package/src/contracts.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { PosthorseDisplay } from "
|
|
2
|
+
import type { PosthorseDisplay } from "./ui/display.ts";
|
|
3
3
|
|
|
4
4
|
export { isRecord } from "./records.ts";
|
|
5
5
|
|
|
@@ -40,6 +40,8 @@ export type ToolCallLike = {
|
|
|
40
40
|
};
|
|
41
41
|
export type MessageLike = {
|
|
42
42
|
readonly role?: string | undefined;
|
|
43
|
+
readonly timestamp?: number | undefined;
|
|
44
|
+
readonly sections?: unknown;
|
|
43
45
|
readonly stopReason?: string | undefined;
|
|
44
46
|
readonly errorMessage?: string | undefined;
|
|
45
47
|
readonly content?: unknown;
|
|
@@ -112,6 +114,48 @@ export const REMINDER_TYPE = "posthorse-reminder";
|
|
|
112
114
|
export const AUTO_HANDOFF_PREFIX = "Automatic context rollover recovery record.";
|
|
113
115
|
export const EMPTY_HANDOFF = "Fresh context. Restore relevant notes and history before continuing.";
|
|
114
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Journal wording is a storage format: recovery reads sessions back, across releases, by
|
|
119
|
+
* matching this exact text. Producers and parsers share one vocabulary, so a wording change
|
|
120
|
+
* is a migration and must keep reading the previous release's journals.
|
|
121
|
+
*/
|
|
122
|
+
export const BOUNDARY_TYPE = "posthorse-boundary";
|
|
123
|
+
export const RESET_DEFERRED_TYPE = "posthorse-reset-deferred";
|
|
124
|
+
export const RETAINED_TAIL_TYPE = "posthorse-retained-tail";
|
|
125
|
+
export const RETAINED_MARKER_PREFIX = "Context rolled over after entry ";
|
|
126
|
+
/** Display marker the legacy fork's context-window extension projects into messages. */
|
|
127
|
+
export const LEGACY_WINDOW_TYPE = "context-window";
|
|
128
|
+
/** The details marker every compaction Posthorse drafts carries; readers reject other values. */
|
|
129
|
+
export const POSTHORSE_COMPACTION = 1;
|
|
130
|
+
|
|
131
|
+
export function posthorseDetails(reason: string): {
|
|
132
|
+
readonly posthorse: typeof POSTHORSE_COMPACTION;
|
|
133
|
+
readonly reason: string;
|
|
134
|
+
} {
|
|
135
|
+
return { posthorse: POSTHORSE_COMPACTION, reason };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Receipt lines the notes tool returns; recovery matches them to record note touches. */
|
|
139
|
+
export const NOTE_RECEIPT = {
|
|
140
|
+
wrote: "Wrote ",
|
|
141
|
+
appended: "Appended to ",
|
|
142
|
+
file: "File: ",
|
|
143
|
+
directory: "Notes directory: ",
|
|
144
|
+
} as const;
|
|
145
|
+
|
|
146
|
+
export function noteWriteReceipt(path: string): string {
|
|
147
|
+
return `${NOTE_RECEIPT.wrote}${path}`;
|
|
148
|
+
}
|
|
149
|
+
export function noteAppendReceipt(path: string): string {
|
|
150
|
+
return `${NOTE_RECEIPT.appended}${path}`;
|
|
151
|
+
}
|
|
152
|
+
export function noteReadHeader(path: string): string {
|
|
153
|
+
return `${NOTE_RECEIPT.file}${path}\n`;
|
|
154
|
+
}
|
|
155
|
+
export function notesDirectoryHeader(directory: string): string {
|
|
156
|
+
return `${NOTE_RECEIPT.directory}${directory}\n`;
|
|
157
|
+
}
|
|
158
|
+
|
|
115
159
|
export function unknownArray(value: unknown): readonly unknown[] | undefined {
|
|
116
160
|
return Array.isArray(value) ? value : undefined;
|
|
117
161
|
}
|
package/src/files.ts
CHANGED
|
@@ -86,6 +86,12 @@ async function existingNote(target: string) {
|
|
|
86
86
|
}
|
|
87
87
|
return previous;
|
|
88
88
|
}
|
|
89
|
+
/** Reserved by publishFile; inventories hide these names without deleting their contents. */
|
|
90
|
+
export function isPublicationTemporaryName(name: string): boolean {
|
|
91
|
+
return /^\.posthorse-[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\.tmp$/.test(
|
|
92
|
+
name,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
89
95
|
/** Same-directory replacement; pre-rename failures preserve the old note and its metadata. */
|
|
90
96
|
export async function publishFile(
|
|
91
97
|
path: string,
|
package/src/guidance.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ExtensionAPI
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { Type, type Static } from "typebox";
|
|
3
3
|
import { Check } from "typebox/value";
|
|
4
4
|
import {
|
|
@@ -9,8 +9,14 @@ import {
|
|
|
9
9
|
} from "./budget.ts";
|
|
10
10
|
import type { EntryLike, PolicyContext, SnapshotReader } from "./contracts.ts";
|
|
11
11
|
import { startsFreshGuidanceEpoch } from "./boundary.ts";
|
|
12
|
-
import { createEpochCache, type EpochValue } from "./epoch-cache.ts";
|
|
13
|
-
import {
|
|
12
|
+
import { createEpochCache, type EpochValue, type SessionContext } from "./epoch-cache.ts";
|
|
13
|
+
import {
|
|
14
|
+
sessionEntry,
|
|
15
|
+
toolFacts,
|
|
16
|
+
type SessionHost,
|
|
17
|
+
type SessionIdentity,
|
|
18
|
+
type ToolRegistry,
|
|
19
|
+
} from "./host.ts";
|
|
14
20
|
|
|
15
21
|
export const WINDOW_GUIDANCE_TYPE = "posthorse-window-guidance";
|
|
16
22
|
const guidanceRecordSchema = Type.Object({
|
|
@@ -33,18 +39,20 @@ type GuidanceEpoch = {
|
|
|
33
39
|
readonly tools: GuidanceTools | undefined;
|
|
34
40
|
};
|
|
35
41
|
export type WindowGuidance = {
|
|
36
|
-
|
|
37
|
-
readonly
|
|
42
|
+
/** Only the session manager is read; the same context the epoch cache accepts. */
|
|
43
|
+
readonly resolve: (ctx: SessionContext, host: PolicyContext) => string;
|
|
44
|
+
readonly deliver: (ctx: SessionContext, host: PolicyContext) => string;
|
|
38
45
|
readonly reset: () => void;
|
|
39
46
|
};
|
|
40
47
|
|
|
41
|
-
export function activeGuidanceTools(pi:
|
|
48
|
+
export function activeGuidanceTools(pi: ToolRegistry): GuidanceTools {
|
|
42
49
|
const { activeUnnamespaced } = toolFacts(pi);
|
|
43
50
|
return {
|
|
44
51
|
newContext: activeUnnamespaced.has("new_context"),
|
|
45
52
|
getContextRemaining: activeUnnamespaced.has("get_context_remaining"),
|
|
46
53
|
};
|
|
47
54
|
}
|
|
55
|
+
/** Replay oldest first: the last text per budget wins, while the first record fixes epoch tools. */
|
|
48
56
|
function collectGuidanceEntries(
|
|
49
57
|
entries: readonly EntryLike[],
|
|
50
58
|
baseByBudget?: ReadonlyMap<string, string>,
|
|
@@ -122,7 +130,10 @@ function scanGuidanceEpoch(
|
|
|
122
130
|
...withMemoryEpoch(collected, memoryByEpoch?.get(anchorKey)),
|
|
123
131
|
};
|
|
124
132
|
}
|
|
125
|
-
|
|
133
|
+
/** The journal and tool-registry members guidance borrows; the host's full API satisfies these. */
|
|
134
|
+
export type GuidanceRegistry = ToolRegistry & Pick<ExtensionAPI, "appendEntry">;
|
|
135
|
+
|
|
136
|
+
export function createWindowGuidance(pi: GuidanceRegistry): WindowGuidance {
|
|
126
137
|
let memoryByManager = new WeakMap<object, Map<string, MemoryEpoch>>();
|
|
127
138
|
const epochs = createEpochCache<GuidanceEpoch>((ctx, identity, previous) =>
|
|
128
139
|
scanGuidanceEpoch(
|
|
@@ -132,6 +143,21 @@ export function createWindowGuidance(pi: ExtensionAPI): WindowGuidance {
|
|
|
132
143
|
memoryByManager.get(ctx.sessionManager),
|
|
133
144
|
),
|
|
134
145
|
);
|
|
146
|
+
/**
|
|
147
|
+
* A fresh anchor retires every earlier one on this branch: nothing reads an old epoch again
|
|
148
|
+
* without /tree, which resets all memory. Keep only the current anchor's delivered text.
|
|
149
|
+
*/
|
|
150
|
+
const pruneRetiredEpochs = (manager: object, anchorKey: string): void => {
|
|
151
|
+
const byEpoch = memoryByManager.get(manager);
|
|
152
|
+
if (byEpoch === undefined) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
for (const key of byEpoch.keys()) {
|
|
156
|
+
if (key !== anchorKey) {
|
|
157
|
+
byEpoch.delete(key);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
135
161
|
return {
|
|
136
162
|
resolve(ctx, host) {
|
|
137
163
|
const epoch = epochs.read(ctx);
|
|
@@ -140,8 +166,19 @@ export function createWindowGuidance(pi: ExtensionAPI): WindowGuidance {
|
|
|
140
166
|
epoch.byBudget.get(key) ?? buildGuidance(host, epoch.tools ?? ALL_GUIDANCE_TOOLS)
|
|
141
167
|
);
|
|
142
168
|
},
|
|
169
|
+
/**
|
|
170
|
+
* Delivering guidance from inside the request hook must persist before the request: a
|
|
171
|
+
* restart after a rollover with a provider summary block has to reuse these exact bytes —
|
|
172
|
+
* the leading system message is sent unchanged, and in-memory delivery state does not
|
|
173
|
+
* survive. On the fork, the provider-summary request runs this hook mid-compaction. That
|
|
174
|
+
* is safe twice over: the request that reached the rollover threshold already delivered
|
|
175
|
+
* this budget's text, so the summary request reuses it and writes nothing; and a
|
|
176
|
+
* compaction with a summary block keeps the previous guidance anchor, so even a budget
|
|
177
|
+
* change mid-compaction appends inside the epoch the next window still reads.
|
|
178
|
+
*/
|
|
143
179
|
deliver(ctx, host) {
|
|
144
180
|
const epoch = epochs.read(ctx);
|
|
181
|
+
pruneRetiredEpochs(ctx.sessionManager, epoch.anchorKey);
|
|
145
182
|
const key = budgetGuidanceKey(host);
|
|
146
183
|
const existing = epoch.byBudget.get(key);
|
|
147
184
|
if (existing !== undefined) {
|
|
@@ -177,7 +214,7 @@ export function createWindowGuidance(pi: ExtensionAPI): WindowGuidance {
|
|
|
177
214
|
};
|
|
178
215
|
}
|
|
179
216
|
export function registerGuidance(
|
|
180
|
-
pi: ExtensionAPI,
|
|
217
|
+
pi: Pick<ExtensionAPI, "on">,
|
|
181
218
|
policy: SnapshotReader,
|
|
182
219
|
windowGuidance: WindowGuidance,
|
|
183
220
|
): void {
|
|
@@ -185,6 +222,7 @@ export function registerGuidance(
|
|
|
185
222
|
pi.on("before_agent_start", (event) => {
|
|
186
223
|
promptOptions = event.systemPromptOptions;
|
|
187
224
|
});
|
|
225
|
+
|
|
188
226
|
pi.on("agent_settled", () => {
|
|
189
227
|
// Prompt options belong to one run, never to later idle context evaluation.
|
|
190
228
|
promptOptions = undefined;
|
package/src/history-entries.ts
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
toolIdentity,
|
|
21
21
|
} from "./message-text.ts";
|
|
22
22
|
|
|
23
|
+
/** Native journal order is oldest first; the last edit of a target wins, regardless of timestamp. */
|
|
23
24
|
export function contextEdits(entries: readonly EntryLike[]): Map<string, EntryLike> {
|
|
24
25
|
return new Map(
|
|
25
26
|
entries.flatMap((entry) =>
|
|
@@ -186,7 +187,10 @@ export function toWindowedEntry(
|
|
|
186
187
|
images: imagesOf(entryContent(entry)),
|
|
187
188
|
};
|
|
188
189
|
}
|
|
189
|
-
/**
|
|
190
|
+
/**
|
|
191
|
+
* Feed parents before children, including entries that do not match a search. Labels follow
|
|
192
|
+
* ancestry, not timestamps. A fresh projection belongs to one forward journal traversal.
|
|
193
|
+
*/
|
|
190
194
|
export function windowProjection(): (entry: EntryLike) => string {
|
|
191
195
|
const windows = new Map<string, string>();
|
|
192
196
|
return (entry) => {
|
package/src/history-pages.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MIN_PAGE_CHARS,
|
|
3
3
|
ESTIMATED_IMAGE_CHARS,
|
|
4
|
+
textResult,
|
|
4
5
|
type HistoryHit,
|
|
5
6
|
type PolicyContext,
|
|
6
7
|
type TextResult,
|
|
@@ -80,34 +81,35 @@ export function searchPage(
|
|
|
80
81
|
host: PolicyContext,
|
|
81
82
|
pages: PageAccess,
|
|
82
83
|
): TextResult {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
84
|
+
return pages.deliver(host, {}, (chars) => {
|
|
85
|
+
const reserve = Math.max(
|
|
86
|
+
0,
|
|
87
|
+
...matches.hits.map(
|
|
88
|
+
(hit) => searchFooter(cursorAt(matches.key, hit, hit.text.length)).length,
|
|
89
|
+
),
|
|
90
|
+
);
|
|
91
|
+
if (reserve >= chars) {
|
|
92
|
+
pages.error(host, "History entry id is too large for pagination.");
|
|
93
|
+
}
|
|
94
|
+
const skippedNote =
|
|
95
|
+
matches.skipped > 0 && (params.cursor === undefined || params.cursor === "")
|
|
96
|
+
? `\n[Skipped ${matches.skipped} match${matches.skipped === 1 ? "" : "es"} already in your active context.]`
|
|
97
|
+
: "";
|
|
98
|
+
const page = searchParts(matches, chars - reserve - skippedNote.length);
|
|
99
|
+
const body =
|
|
100
|
+
page.parts.length > 0
|
|
101
|
+
? page.parts.join("\n")
|
|
102
|
+
: `No history matches "${excerpt(params.query ?? "", 200)}".`;
|
|
103
|
+
const tail =
|
|
104
|
+
skippedNote +
|
|
105
|
+
(page.continuation.kind === "more" ? searchFooter(page.continuation.next) : "");
|
|
106
|
+
return textResult(body + tail, [], {
|
|
107
|
+
kind: "history-search",
|
|
108
|
+
entries: [...page.spans],
|
|
109
|
+
footerLength: tail.length,
|
|
110
|
+
more: page.continuation.kind === "more",
|
|
111
|
+
skipped: matches.skipped,
|
|
112
|
+
});
|
|
111
113
|
});
|
|
112
114
|
}
|
|
113
115
|
function readOffsets(
|
|
@@ -144,33 +146,37 @@ export function historyReadPage(
|
|
|
144
146
|
const shownId = shownEntryId(item, params.id, source);
|
|
145
147
|
const { offset, imageOffset } = readOffsets(item, params);
|
|
146
148
|
const firstImage = imageOffset < item.images.length ? 1 : 0;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
offset,
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
149
|
+
return access.pages.deliver(access.host, { imageCount: firstImage }, (chars) => {
|
|
150
|
+
const imageEnd = Math.min(
|
|
151
|
+
item.images.length,
|
|
152
|
+
imageOffset + firstImage + Math.floor((chars - MIN_PAGE_CHARS) / ESTIMATED_IMAGE_CHARS),
|
|
153
|
+
);
|
|
154
|
+
const images = item.images.slice(imageOffset, imageEnd);
|
|
155
|
+
const end = Math.min(
|
|
156
|
+
item.text.length,
|
|
157
|
+
offset + chars - (images.length - firstImage) * ESTIMATED_IMAGE_CHARS,
|
|
158
|
+
);
|
|
159
|
+
const continuation: Continuation<{
|
|
160
|
+
readonly offset: number;
|
|
161
|
+
readonly imageOffset: number;
|
|
162
|
+
}> =
|
|
163
|
+
end < item.text.length || imageEnd < item.images.length
|
|
164
|
+
? { kind: "more", next: { offset: end, imageOffset: imageEnd } }
|
|
165
|
+
: { kind: "complete" };
|
|
166
|
+
const more =
|
|
167
|
+
continuation.kind === "more"
|
|
168
|
+
? `\nMore remains; call history read with id "${shownId}" and offset ${continuation.next.offset}${item.images.length > 0 ? ` and imageOffset ${continuation.next.imageOffset}` : ""}.`
|
|
169
|
+
: "";
|
|
170
|
+
const header = `${item.entry.timestamp ?? ""}${sourceTag(source)} [window ${item.windowId}] [${shownId}] [chars ${offset}-${end} of ${item.text.length}] `;
|
|
171
|
+
return textResult(`${header}${item.text.slice(offset, end)}${more}`, images, {
|
|
172
|
+
kind: "history-read",
|
|
173
|
+
headerLength: header.length,
|
|
174
|
+
offset,
|
|
175
|
+
end,
|
|
176
|
+
total: item.text.length,
|
|
177
|
+
imageOffset,
|
|
178
|
+
imageEnd,
|
|
179
|
+
imageTotal: item.images.length,
|
|
180
|
+
});
|
|
175
181
|
});
|
|
176
182
|
}
|
package/src/history-search.ts
CHANGED
|
@@ -31,6 +31,7 @@ export type HistorySource = {
|
|
|
31
31
|
readonly dir: string;
|
|
32
32
|
readonly cwd: string;
|
|
33
33
|
readonly currentFile?: string | undefined;
|
|
34
|
+
/** Pi's parent-before-child branch order, not message timestamp order. */
|
|
34
35
|
readonly branch: () => readonly EntryLike[];
|
|
35
36
|
readonly projection: () => readonly ProjectedEntry[];
|
|
36
37
|
};
|
package/src/note-discovery.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readdir, stat } from "node:fs/promises";
|
|
2
|
-
import { join, relative } from "node:path";
|
|
2
|
+
import { basename, join, relative } from "node:path";
|
|
3
3
|
import { isRecord } from "./contracts.ts";
|
|
4
|
+
import { isPublicationTemporaryName } from "./files.ts";
|
|
4
5
|
import { sessionNoteTouches } from "./note-evidence.ts";
|
|
5
6
|
import { notesUnder, type NoteFile } from "./note-inventory.ts";
|
|
6
7
|
import {
|
|
@@ -54,12 +55,14 @@ async function projectScopes(
|
|
|
54
55
|
throw error;
|
|
55
56
|
});
|
|
56
57
|
const candidates = entries.filter((entry) => !entry.name.startsWith(".staging-"));
|
|
57
|
-
const manifests = await mapBatch(candidates, async (entry) =>
|
|
58
|
-
entry
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
const manifests = await mapBatch(candidates, async (entry) => ({
|
|
59
|
+
entry,
|
|
60
|
+
manifest: entry.isDirectory()
|
|
61
|
+
? await readManifest(join(directory, entry.name, "notes"))
|
|
62
|
+
: undefined,
|
|
63
|
+
}));
|
|
64
|
+
for (const { entry, manifest } of manifests) {
|
|
61
65
|
const path = join(directory, entry.name);
|
|
62
|
-
const manifest = manifests[index];
|
|
63
66
|
if (manifest === undefined || manifest.root !== session.binding.root) {
|
|
64
67
|
notice(`Not indexed: unmanaged or incomplete notes data at ${path}.`);
|
|
65
68
|
} else {
|
|
@@ -118,6 +121,9 @@ async function readReference(
|
|
|
118
121
|
path: string,
|
|
119
122
|
signal: AbortSignal | undefined,
|
|
120
123
|
): Promise<DiscoveredNote | undefined> {
|
|
124
|
+
if (isPublicationTemporaryName(basename(path))) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
121
127
|
const target = await session.access(path, { signal });
|
|
122
128
|
if (target.kind === "own") {
|
|
123
129
|
return;
|
package/src/note-evidence.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import { isRecord, optionalString, type EntryLike, type ProjectedEntry } from "./contracts.ts";
|
|
3
3
|
import { contextEdits } from "./history-entries.ts";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
collectTouches,
|
|
6
|
+
noteTargetKey,
|
|
7
|
+
type NoteTouch,
|
|
8
|
+
type ScannedNoteEntry,
|
|
9
|
+
} from "./recovery-note-touches.ts";
|
|
5
10
|
import {
|
|
6
11
|
addressOf,
|
|
7
12
|
bindingsOf,
|
|
@@ -20,7 +25,9 @@ export type NotesRecovery = {
|
|
|
20
25
|
};
|
|
21
26
|
|
|
22
27
|
export type NoteEvidence = {
|
|
28
|
+
/** Active branch in native journal order; later edits and receipts supersede earlier ones. */
|
|
23
29
|
readonly branch: readonly EntryLike[];
|
|
30
|
+
/** Keep each source journal's effect order when combining durable provenance. */
|
|
24
31
|
readonly allEntries: readonly EntryLike[];
|
|
25
32
|
readonly projected: readonly ProjectedEntry[];
|
|
26
33
|
readonly retained: ReadonlySet<string>;
|
|
@@ -74,15 +81,12 @@ function recordedTouches(
|
|
|
74
81
|
): NoteTouch[] {
|
|
75
82
|
const receipts = effectReceipts(evidence.branch);
|
|
76
83
|
const shown = new Map(visible.map((item) => [item.entry.id, item]));
|
|
77
|
-
const
|
|
78
|
-
return touchRecords(evidence.allEntries).flatMap((record) => {
|
|
84
|
+
const touches = touchRecords(evidence.allEntries).flatMap((record) => {
|
|
79
85
|
const receipt = receipts.get(record.rootToolCallId);
|
|
80
86
|
const item = shown.get(receipt?.id);
|
|
81
|
-
|
|
82
|
-
if (item === undefined || seen.has(key)) {
|
|
87
|
+
if (item === undefined) {
|
|
83
88
|
return [];
|
|
84
89
|
}
|
|
85
|
-
seen.add(key);
|
|
86
90
|
return record.files.map((file): NoteTouch => {
|
|
87
91
|
let kind: NoteTouch["kind"] = "observed-write";
|
|
88
92
|
if (file.kind === "read") {
|
|
@@ -96,14 +100,39 @@ function recordedTouches(
|
|
|
96
100
|
kind,
|
|
97
101
|
entryId: item.entryId,
|
|
98
102
|
receiptId: item.entry.id ?? item.entryId,
|
|
103
|
+
toolCallId: record.toolCallId,
|
|
99
104
|
inRetained: false,
|
|
100
105
|
owner: record.owner,
|
|
101
106
|
};
|
|
102
107
|
});
|
|
103
108
|
});
|
|
109
|
+
const latest = new Map<string, NoteTouch>();
|
|
110
|
+
for (const touch of touches) {
|
|
111
|
+
const key = JSON.stringify([touch.owner?.id, touch.owner?.timestamp, touchKey(touch)]);
|
|
112
|
+
// Adoption and completion can record the same file in one call. Keep its final effect
|
|
113
|
+
// at its final position without discarding other files adopted by that call.
|
|
114
|
+
latest.delete(key);
|
|
115
|
+
latest.set(key, touch);
|
|
116
|
+
}
|
|
117
|
+
return [...latest.values()];
|
|
104
118
|
}
|
|
105
119
|
function touchKey(touch: NoteTouch): string {
|
|
106
|
-
return JSON.stringify([
|
|
120
|
+
return JSON.stringify([
|
|
121
|
+
touch.receiptId ?? touch.entryId,
|
|
122
|
+
touch.toolCallId,
|
|
123
|
+
noteTargetKey(touch),
|
|
124
|
+
]);
|
|
125
|
+
}
|
|
126
|
+
function reconcileTouch(recorded: NoteTouch, inferred: NoteTouch | undefined): NoteTouch {
|
|
127
|
+
if (
|
|
128
|
+
inferred !== undefined &&
|
|
129
|
+
((recorded.kind === "read" && inferred.kind === "read") ||
|
|
130
|
+
(recorded.kind === "observed-write" &&
|
|
131
|
+
(inferred.kind === "full-write" || inferred.kind === "partial-write")))
|
|
132
|
+
) {
|
|
133
|
+
return { ...inferred, owner: recorded.owner };
|
|
134
|
+
}
|
|
135
|
+
return recorded;
|
|
107
136
|
}
|
|
108
137
|
export function sessionNoteTouches(evidence: NoteEvidence): {
|
|
109
138
|
readonly touches: readonly NoteTouch[];
|
|
@@ -117,14 +146,12 @@ export function sessionNoteTouches(evidence: NoteEvidence): {
|
|
|
117
146
|
]);
|
|
118
147
|
const legacy = collectTouches(visible, directories);
|
|
119
148
|
const recorded = recordedTouches(evidence, visible);
|
|
120
|
-
const
|
|
121
|
-
const covered = new Set
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
});
|
|
127
|
-
const touches = [...identified, ...recorded.filter((touch) => !covered.has(touchKey(touch)))];
|
|
149
|
+
const inferredByCall = new Map(legacy.touches.map((touch) => [touchKey(touch), touch]));
|
|
150
|
+
const covered = new Set(recorded.map(touchKey));
|
|
151
|
+
const touches = [
|
|
152
|
+
...legacy.touches.filter((touch) => !covered.has(touchKey(touch))),
|
|
153
|
+
...recorded.map((touch) => reconcileTouch(touch, inferredByCall.get(touchKey(touch)))),
|
|
154
|
+
];
|
|
128
155
|
const order = new Map(visible.map((entry, index) => [entry.entry.id ?? entry.entryId, index]));
|
|
129
156
|
touches.sort(
|
|
130
157
|
(a, b) =>
|
package/src/note-guards.ts
CHANGED
|
@@ -89,12 +89,15 @@ async function recordedFiles(
|
|
|
89
89
|
captured: CapturedCall,
|
|
90
90
|
): Promise<NoteTouchRecord["files"]> {
|
|
91
91
|
const files: NoteTouchRecord["files"][number][] = [];
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
const addressed = await mapBatch(operations, async (operation) => ({
|
|
93
|
+
operation,
|
|
94
|
+
address: await noteAddress(
|
|
95
|
+
toolPath(captured.tool, operation.path, captured.cwd),
|
|
96
|
+
captured.session,
|
|
97
|
+
),
|
|
98
|
+
}));
|
|
99
|
+
for (const { operation, address } of addressed) {
|
|
96
100
|
// Each result belongs to the scope captured before execution, even after /tree.
|
|
97
|
-
const address = addresses[index];
|
|
98
101
|
if (address === undefined) {
|
|
99
102
|
continue;
|
|
100
103
|
}
|
package/src/note-inventory.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readdir, realpath, stat } from "node:fs/promises";
|
|
2
|
-
import { join } from "node:path";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
3
|
import { isRecord, presentText } from "./contracts.ts";
|
|
4
|
+
import { isPublicationTemporaryName } from "./files.ts";
|
|
4
5
|
import {
|
|
5
6
|
canonicalPath,
|
|
6
7
|
inside,
|
|
@@ -26,7 +27,9 @@ async function statIfPresent(path: string) {
|
|
|
26
27
|
}
|
|
27
28
|
/** Preserve depth-first listing order and recheck later entries after visiting each folder. */
|
|
28
29
|
async function* folderEntries(directory: string, signal: AbortSignal | undefined) {
|
|
29
|
-
const entries = await readdir(directory, { withFileTypes: true })
|
|
30
|
+
const entries = (await readdir(directory, { withFileTypes: true })).filter(
|
|
31
|
+
(entry) => !isPublicationTemporaryName(entry.name),
|
|
32
|
+
);
|
|
30
33
|
let start = 0;
|
|
31
34
|
while (start < entries.length) {
|
|
32
35
|
signal?.throwIfAborted();
|
|
@@ -34,15 +37,18 @@ async function* folderEntries(directory: string, signal: AbortSignal | undefined
|
|
|
34
37
|
const firstFolder = batch.findIndex(
|
|
35
38
|
(entry) => entry.isDirectory() || entry.isSymbolicLink(),
|
|
36
39
|
);
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const listed = await Promise.all(
|
|
41
|
+
batch
|
|
42
|
+
.slice(0, firstFolder === -1 ? batch.length : firstFolder + 1)
|
|
43
|
+
.map(async (entry) => {
|
|
44
|
+
const path = join(directory, entry.name);
|
|
45
|
+
return { path, info: await statIfPresent(path), alias: entry.isSymbolicLink() };
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
for (const { path, info, alias } of listed) {
|
|
42
49
|
start += 1;
|
|
43
|
-
const info = infos[index];
|
|
44
50
|
if (info !== undefined) {
|
|
45
|
-
yield { path, info, alias
|
|
51
|
+
yield { path, info, alias };
|
|
46
52
|
}
|
|
47
53
|
if (info?.isDirectory() === true) {
|
|
48
54
|
break;
|
|
@@ -99,6 +105,9 @@ export async function notesUnder(
|
|
|
99
105
|
requested === undefined || requested === "."
|
|
100
106
|
? scopeDirectory(scope)
|
|
101
107
|
: notePath(scope, requested);
|
|
108
|
+
if (isPublicationTemporaryName(basename(target))) {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
102
111
|
const info = await statIfPresent(target);
|
|
103
112
|
options.signal?.throwIfAborted();
|
|
104
113
|
if (info === undefined) {
|
package/src/note-provenance.ts
CHANGED
|
@@ -235,13 +235,13 @@ async function forkBinding(
|
|
|
235
235
|
const entries = [...manager.getEntries(), ...source.entries];
|
|
236
236
|
const files: SnapshotNote[] = [];
|
|
237
237
|
const absent: string[] = [];
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
238
|
+
const addressed = await mapBatch(candidates, async (candidate) => ({
|
|
239
|
+
candidate,
|
|
240
|
+
address: await resolveReference(scope ?? candidate.source, candidate.path, entries),
|
|
241
|
+
}));
|
|
242
|
+
for (const { candidate, address } of addressed) {
|
|
242
243
|
// Resolve against the immediate parent's files, not a historical ancestor's version.
|
|
243
244
|
// A missing source note is not copied; a known source scope still resolves it later.
|
|
244
|
-
const address = addresses[index];
|
|
245
245
|
if (address !== undefined) {
|
|
246
246
|
files.push({
|
|
247
247
|
path: candidate.path,
|