pi-posthorse 0.10.35 → 0.10.36
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/recovery-coordination.ts +281 -0
- package/src/recovery-inputs.ts +10 -2
- package/src/recovery-notes.ts +80 -11
- package/src/recovery-records.ts +30 -10
- package/src/recovery.ts +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { contextStart } from "./boundary.ts";
|
|
2
|
+
import {
|
|
3
|
+
isRecord,
|
|
4
|
+
optionalString,
|
|
5
|
+
presentText,
|
|
6
|
+
unknownArray,
|
|
7
|
+
type EntryLike,
|
|
8
|
+
type ProjectedEntry,
|
|
9
|
+
type ToolCallLike,
|
|
10
|
+
} from "./contracts.ts";
|
|
11
|
+
import { contextEdits } from "./history-entries.ts";
|
|
12
|
+
import { toolCalls } from "./message-text.ts";
|
|
13
|
+
|
|
14
|
+
export type Supersession = { readonly byEntryId: string; readonly inRetainedTail: boolean };
|
|
15
|
+
|
|
16
|
+
type ActiveEntry = {
|
|
17
|
+
readonly index: number;
|
|
18
|
+
readonly effectiveId: string;
|
|
19
|
+
readonly inRetainedTail: boolean;
|
|
20
|
+
readonly entry: EntryLike;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type SupersederIndex = {
|
|
24
|
+
readonly jobsByIndex: Map<number, readonly string[]>;
|
|
25
|
+
readonly jobLatest: Map<string, ActiveEntry>;
|
|
26
|
+
readonly escalationLatest: Map<string, ActiveEntry>;
|
|
27
|
+
readonly completionLatest: Map<string, ActiveEntry>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const TERMINAL_JOB_STATUSES = new Set(["succeeded", "failed", "cancelled", "timed_out", "unknown"]);
|
|
31
|
+
const ESCALATION_ACTIONS = new Set(["continue", "review", "stop"]);
|
|
32
|
+
|
|
33
|
+
function isPlainToolResult(entry: EntryLike, toolName: string): boolean {
|
|
34
|
+
return (
|
|
35
|
+
entry.type === "message" &&
|
|
36
|
+
entry.message?.role === "toolResult" &&
|
|
37
|
+
entry.message.toolName === toolName &&
|
|
38
|
+
entry.message.namespace === undefined &&
|
|
39
|
+
entry.message.isError !== true
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function callArgs(
|
|
44
|
+
entry: EntryLike,
|
|
45
|
+
calls: ReadonlyMap<string, ToolCallLike>,
|
|
46
|
+
): Readonly<Record<string, unknown>> | undefined {
|
|
47
|
+
const callId = optionalString(entry.message?.toolCallId);
|
|
48
|
+
const call = callId === undefined ? undefined : calls.get(callId);
|
|
49
|
+
return call?.namespace === undefined && isRecord(call?.arguments) ? call.arguments : undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function backgroundCompleteJobIds(entry: EntryLike): readonly string[] {
|
|
53
|
+
if (!isRecord(entry.details)) {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
const ids = new Set<string>();
|
|
57
|
+
for (const item of unknownArray(entry.details.jobs) ?? []) {
|
|
58
|
+
const id = isRecord(item) ? presentText(item.id) : undefined;
|
|
59
|
+
if (id !== undefined) {
|
|
60
|
+
ids.add(id);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const item of unknownArray(entry.details.jobIds) ?? []) {
|
|
64
|
+
const id = presentText(item);
|
|
65
|
+
if (id !== undefined) {
|
|
66
|
+
ids.add(id);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return [...ids];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function singleJobAction(
|
|
73
|
+
args: Readonly<Record<string, unknown>> | undefined,
|
|
74
|
+
details: Readonly<Record<string, unknown>>,
|
|
75
|
+
): string | undefined {
|
|
76
|
+
const callAction = optionalString(args?.action);
|
|
77
|
+
if (callAction === "status" && presentText(args?.id) === undefined) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
return callAction ?? optionalString(details.action);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function singleJobSupersederId(
|
|
84
|
+
entry: EntryLike,
|
|
85
|
+
calls: ReadonlyMap<string, ToolCallLike>,
|
|
86
|
+
): string | undefined {
|
|
87
|
+
const details = entry.message?.details;
|
|
88
|
+
if (!isRecord(details) || Array.isArray(details.jobs)) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const action = singleJobAction(callArgs(entry, calls), details);
|
|
92
|
+
if (action !== "status" && action !== "cancel") {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const id = presentText(details.id);
|
|
96
|
+
const status = optionalString(details.status);
|
|
97
|
+
return id !== undefined && status !== undefined && TERMINAL_JOB_STATUSES.has(status)
|
|
98
|
+
? id
|
|
99
|
+
: undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function recordAgentRunsSuperseder(
|
|
103
|
+
item: ActiveEntry,
|
|
104
|
+
calls: ReadonlyMap<string, ToolCallLike>,
|
|
105
|
+
index: SupersederIndex,
|
|
106
|
+
): void {
|
|
107
|
+
const args = callArgs(item.entry, calls);
|
|
108
|
+
const runId = presentText(args?.id);
|
|
109
|
+
const action = optionalString(args?.action);
|
|
110
|
+
if (runId === undefined || action === undefined) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (ESCALATION_ACTIONS.has(action)) {
|
|
114
|
+
index.escalationLatest.set(runId, item);
|
|
115
|
+
}
|
|
116
|
+
if (action === "continue" || (action === "review" && args?.verdict === "changes_requested")) {
|
|
117
|
+
index.completionLatest.set(runId, item);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function recordCustomSuperseder(item: ActiveEntry, index: SupersederIndex): void {
|
|
122
|
+
const { entry } = item;
|
|
123
|
+
if (entry.customType === "background-command-complete") {
|
|
124
|
+
const jobIds = backgroundCompleteJobIds(entry);
|
|
125
|
+
index.jobsByIndex.set(item.index, jobIds);
|
|
126
|
+
for (const jobId of jobIds) {
|
|
127
|
+
index.jobLatest.set(jobId, item);
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const runId = isRecord(entry.details) ? presentText(entry.details.id) : undefined;
|
|
132
|
+
if (runId === undefined) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (entry.customType === "agent-completion") {
|
|
136
|
+
index.escalationLatest.set(runId, item);
|
|
137
|
+
index.completionLatest.set(runId, item);
|
|
138
|
+
} else if (entry.customType === "agent-escalation") {
|
|
139
|
+
index.escalationLatest.set(runId, item);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function latestSupersederForJobs(
|
|
144
|
+
item: ActiveEntry,
|
|
145
|
+
jobIds: readonly string[],
|
|
146
|
+
jobLatest: ReadonlyMap<string, ActiveEntry>,
|
|
147
|
+
): ActiveEntry | undefined {
|
|
148
|
+
if (jobIds.length === 0) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
let winner: ActiveEntry | undefined;
|
|
152
|
+
for (const jobId of jobIds) {
|
|
153
|
+
const candidate = jobLatest.get(jobId);
|
|
154
|
+
if (candidate === undefined || candidate.index <= item.index) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
if (winner === undefined || candidate.index > winner.index) {
|
|
158
|
+
winner = candidate;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return winner;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function latestRunSuperseder(entry: EntryLike, index: SupersederIndex): ActiveEntry | undefined {
|
|
165
|
+
const runId = isRecord(entry.details) ? presentText(entry.details.id) : undefined;
|
|
166
|
+
if (runId === undefined) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
if (entry.customType === "agent-escalation") {
|
|
170
|
+
return index.escalationLatest.get(runId);
|
|
171
|
+
}
|
|
172
|
+
return entry.customType === "agent-completion" ? index.completionLatest.get(runId) : undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function supersederForEntry(item: ActiveEntry, index: SupersederIndex): ActiveEntry | undefined {
|
|
176
|
+
const { entry } = item;
|
|
177
|
+
if (entry.type !== "custom_message" || entry.display !== true) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
if (entry.customType === "background-command-complete") {
|
|
181
|
+
return latestSupersederForJobs(
|
|
182
|
+
item,
|
|
183
|
+
index.jobsByIndex.get(item.index) ?? [],
|
|
184
|
+
index.jobLatest,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
const candidate = latestRunSuperseder(entry, index);
|
|
188
|
+
return candidate !== undefined && candidate.index > item.index ? candidate : undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function indexActive(
|
|
192
|
+
active: readonly ActiveEntry[],
|
|
193
|
+
calls: ReadonlyMap<string, ToolCallLike>,
|
|
194
|
+
): SupersederIndex {
|
|
195
|
+
const index: SupersederIndex = {
|
|
196
|
+
jobsByIndex: new Map(),
|
|
197
|
+
jobLatest: new Map(),
|
|
198
|
+
escalationLatest: new Map(),
|
|
199
|
+
completionLatest: new Map(),
|
|
200
|
+
};
|
|
201
|
+
for (const item of active) {
|
|
202
|
+
if (item.entry.type === "custom_message") {
|
|
203
|
+
recordCustomSuperseder(item, index);
|
|
204
|
+
} else if (isPlainToolResult(item.entry, "background_command")) {
|
|
205
|
+
const jobId = singleJobSupersederId(item.entry, calls);
|
|
206
|
+
if (jobId !== undefined) {
|
|
207
|
+
index.jobLatest.set(jobId, item);
|
|
208
|
+
}
|
|
209
|
+
} else if (isPlainToolResult(item.entry, "agent_runs")) {
|
|
210
|
+
recordAgentRunsSuperseder(item, calls, index);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return index;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function activeCalls(
|
|
217
|
+
entries: readonly EntryLike[],
|
|
218
|
+
excluded: ReadonlySet<string>,
|
|
219
|
+
edits: ReadonlyMap<string, EntryLike>,
|
|
220
|
+
): Map<string, ToolCallLike> {
|
|
221
|
+
return new Map(
|
|
222
|
+
entries.flatMap((entry) => {
|
|
223
|
+
const id = entry.id ?? "";
|
|
224
|
+
if (
|
|
225
|
+
excluded.has(id) ||
|
|
226
|
+
entry.type !== "message" ||
|
|
227
|
+
entry.message?.role !== "assistant"
|
|
228
|
+
) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
const content = edits.get(id)?.replacement?.content ?? entry.message.content;
|
|
232
|
+
return toolCalls(content).flatMap((call) =>
|
|
233
|
+
call.id !== undefined && call.id !== "" ? [[call.id, call] as const] : [],
|
|
234
|
+
);
|
|
235
|
+
}),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function supersededCoordinationMap(
|
|
240
|
+
entries: readonly EntryLike[],
|
|
241
|
+
projected: readonly ProjectedEntry[],
|
|
242
|
+
options: {
|
|
243
|
+
readonly retained: ReadonlySet<string>;
|
|
244
|
+
readonly currentWindow?: readonly EntryLike[] | undefined;
|
|
245
|
+
},
|
|
246
|
+
): ReadonlyMap<string, Supersession> {
|
|
247
|
+
const edits = contextEdits(entries);
|
|
248
|
+
const excluded = new Set([
|
|
249
|
+
...projected
|
|
250
|
+
.filter(({ messages }) => messages.length === 0)
|
|
251
|
+
.map(({ sourceEntry }) => sourceEntry.id ?? ""),
|
|
252
|
+
...[...edits].filter(([, edit]) => edit.replacement === null).map(([id]) => id),
|
|
253
|
+
]);
|
|
254
|
+
excluded.delete("");
|
|
255
|
+
const calls = activeCalls(entries, excluded, edits);
|
|
256
|
+
const currentWindow = options.currentWindow ?? entries.slice(contextStart(entries).start);
|
|
257
|
+
const active = currentWindow
|
|
258
|
+
.filter((entry) => entry.id !== undefined && entry.id !== "" && !excluded.has(entry.id))
|
|
259
|
+
.map((entry, index): ActiveEntry => {
|
|
260
|
+
const sourceId = entry.id ?? "";
|
|
261
|
+
const edit = edits.get(sourceId);
|
|
262
|
+
return {
|
|
263
|
+
index,
|
|
264
|
+
effectiveId: edit?.replacement !== undefined ? (edit.id ?? sourceId) : sourceId,
|
|
265
|
+
inRetainedTail: options.retained.has(sourceId),
|
|
266
|
+
entry,
|
|
267
|
+
};
|
|
268
|
+
});
|
|
269
|
+
const index = indexActive(active, calls);
|
|
270
|
+
const result = new Map<string, Supersession>();
|
|
271
|
+
for (const item of active) {
|
|
272
|
+
const winner = supersederForEntry(item, index);
|
|
273
|
+
if (winner !== undefined) {
|
|
274
|
+
result.set(item.effectiveId, {
|
|
275
|
+
byEntryId: winner.effectiveId.slice(0, 120),
|
|
276
|
+
inRetainedTail: winner.inRetainedTail,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
}
|
package/src/recovery-inputs.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { OWNER_QUESTION_TOOL, type EntryLike, type ProjectedEntry } from "./cont
|
|
|
2
2
|
import { chainsAncestry, contextStart } from "./boundary.ts";
|
|
3
3
|
import { contextEdits } from "./history-entries.ts";
|
|
4
4
|
import { toolCalls } from "./message-text.ts";
|
|
5
|
+
import { supersededCoordinationMap, type Supersession } from "./recovery-coordination.ts";
|
|
5
6
|
import {
|
|
6
7
|
formatRecord,
|
|
7
8
|
MAX_RECOVERY_RECORD_CHARS,
|
|
@@ -55,6 +56,7 @@ function ownerRecords(
|
|
|
55
56
|
entries: readonly EntryLike[],
|
|
56
57
|
projected: readonly ProjectedEntry[],
|
|
57
58
|
ownerQuestionRegistered: boolean,
|
|
59
|
+
superseded?: ReadonlyMap<string, Supersession>,
|
|
58
60
|
): RecoveryRecord[] {
|
|
59
61
|
const calls = new Map(
|
|
60
62
|
projected.flatMap(({ messages }) =>
|
|
@@ -72,6 +74,7 @@ function ownerRecords(
|
|
|
72
74
|
ownerQuestionRegistered &&
|
|
73
75
|
call?.name === OWNER_QUESTION_TOOL &&
|
|
74
76
|
call.namespace === undefined,
|
|
77
|
+
entry.id === undefined ? undefined : superseded?.get(entry.id),
|
|
75
78
|
);
|
|
76
79
|
return record === undefined ? [] : [record];
|
|
77
80
|
});
|
|
@@ -80,9 +83,12 @@ function anchors(records: readonly RecoveryRecord[]): {
|
|
|
80
83
|
fixed: RecoveryRecord[];
|
|
81
84
|
others: RecoveryRecord[];
|
|
82
85
|
} {
|
|
83
|
-
const firstOwner =
|
|
86
|
+
const firstOwner =
|
|
87
|
+
records.find((record) => record.label === "owner input") ??
|
|
88
|
+
records.find((record) => record.supersededBy === undefined) ??
|
|
89
|
+
records[0];
|
|
84
90
|
const latestOwner = records.findLast((record) => record.kind === "owner");
|
|
85
|
-
const latestOverall = records.
|
|
91
|
+
const latestOverall = records.findLast((record) => record.supersededBy === undefined);
|
|
86
92
|
const selected = new Set([firstOwner, latestOwner, latestOverall]);
|
|
87
93
|
return {
|
|
88
94
|
fixed: records.filter((record) => selected.has(record)),
|
|
@@ -234,11 +240,13 @@ export function recoveryInputs(
|
|
|
234
240
|
maxChars,
|
|
235
241
|
},
|
|
236
242
|
);
|
|
243
|
+
const superseded = supersededCoordinationMap(entries, projected, { retained });
|
|
237
244
|
const { fixed, others } = anchors(
|
|
238
245
|
ownerRecords(
|
|
239
246
|
projectedCurrent(entries, projected, undefined, retained),
|
|
240
247
|
projected,
|
|
241
248
|
ownerQuestionRegistered,
|
|
249
|
+
superseded,
|
|
242
250
|
),
|
|
243
251
|
);
|
|
244
252
|
return { fixed: [...carried, ...fixed], others, checkpoint, skipped };
|
package/src/recovery-notes.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
|
+
import { contextStart } from "./boundary.ts";
|
|
2
3
|
import type { EntryLike, ProjectedEntry } from "./contracts.ts";
|
|
3
4
|
import { sessionNoteTouches, scopeRecoveryTouches, type NotesRecovery } from "./note-evidence.ts";
|
|
4
5
|
import { scopeDirectory } from "./note-scopes.ts";
|
|
@@ -96,33 +97,89 @@ function formatNoteLabel(note: TrackedNote, elsewhere: string | undefined): stri
|
|
|
96
97
|
? `${note.notePath} (${note.absPath})`
|
|
97
98
|
: note.notePath;
|
|
98
99
|
}
|
|
99
|
-
|
|
100
|
+
type WrittenNoteOptions = {
|
|
101
|
+
readonly span: RetainedSpan | undefined;
|
|
102
|
+
readonly notesDirectory: string | undefined;
|
|
103
|
+
readonly isPrimary: boolean;
|
|
104
|
+
readonly inCurrentWindow: boolean;
|
|
105
|
+
};
|
|
106
|
+
function formatPrimaryNoteProof(
|
|
100
107
|
note: WrittenNote,
|
|
101
108
|
span: RetainedSpan | undefined,
|
|
102
|
-
|
|
109
|
+
how: string,
|
|
103
110
|
): string {
|
|
104
|
-
const elsewhere = pathElsewhere(note, notesDirectory);
|
|
105
|
-
const label = formatNoteLabel(note, elsewhere);
|
|
106
|
-
const how = elsewhere === undefined ? "" : " on its path";
|
|
107
111
|
const { proof } = note;
|
|
108
112
|
if (proof.kind === "effect-only") {
|
|
109
|
-
return
|
|
113
|
+
return `[verified effect at entry ${note.entryId}; full content is not retained — read this note${how} now]`;
|
|
110
114
|
}
|
|
111
115
|
if (span === undefined) {
|
|
112
|
-
return
|
|
116
|
+
return `[compacted at entry ${note.entryId} — read this note${how} now]`;
|
|
113
117
|
}
|
|
114
118
|
switch (proof.kind) {
|
|
115
119
|
case "full": {
|
|
116
120
|
const updated =
|
|
117
121
|
proof.updatedAt === undefined ? "" : `, updated at entry ${proof.updatedAt}`;
|
|
118
|
-
return
|
|
122
|
+
return `[latest full write is in the retained tail above at entry ${proof.entryId}${updated}]`;
|
|
119
123
|
}
|
|
120
124
|
case "partial":
|
|
121
|
-
return
|
|
125
|
+
return `[only partial edits are in the retained tail above at entry ${proof.entryId} — read full note${how} now]`;
|
|
122
126
|
case "compacted":
|
|
123
|
-
return
|
|
127
|
+
return `[compacted before entry ${span.firstId} — read this note${how} now]`;
|
|
124
128
|
}
|
|
125
129
|
}
|
|
130
|
+
function formatSecondaryCompactedProof(
|
|
131
|
+
note: WrittenNote,
|
|
132
|
+
span: RetainedSpan | undefined,
|
|
133
|
+
inCurrentWindow: boolean,
|
|
134
|
+
how: string,
|
|
135
|
+
): string {
|
|
136
|
+
if (!inCurrentWindow) {
|
|
137
|
+
return `[compacted in an earlier window at entry ${note.entryId}; not in window — read${how} only if still relevant]`;
|
|
138
|
+
}
|
|
139
|
+
if (span !== undefined) {
|
|
140
|
+
return `[compacted before entry ${span.firstId}; not in window — read${how} only if still relevant]`;
|
|
141
|
+
}
|
|
142
|
+
return `[compacted at entry ${note.entryId}; not in window — read${how} only if still relevant]`;
|
|
143
|
+
}
|
|
144
|
+
function formatSecondaryNoteProof(
|
|
145
|
+
note: WrittenNote,
|
|
146
|
+
options: {
|
|
147
|
+
readonly span: RetainedSpan | undefined;
|
|
148
|
+
readonly inCurrentWindow: boolean;
|
|
149
|
+
readonly how: string;
|
|
150
|
+
},
|
|
151
|
+
): string {
|
|
152
|
+
const { span, inCurrentWindow, how } = options;
|
|
153
|
+
const { proof } = note;
|
|
154
|
+
if (proof.kind === "effect-only") {
|
|
155
|
+
return `[verified effect at entry ${note.entryId}; full content is not in window — read${how} only if still relevant]`;
|
|
156
|
+
}
|
|
157
|
+
if (span === undefined || proof.kind === "compacted") {
|
|
158
|
+
return formatSecondaryCompactedProof(note, span, inCurrentWindow, how);
|
|
159
|
+
}
|
|
160
|
+
if (proof.kind === "full") {
|
|
161
|
+
const updated =
|
|
162
|
+
proof.updatedAt === undefined ? "" : `, updated at entry ${proof.updatedAt}`;
|
|
163
|
+
return `[latest full write is in the retained tail above at entry ${proof.entryId}${updated}]`;
|
|
164
|
+
}
|
|
165
|
+
return `[only partial edits are in the retained tail above at entry ${proof.entryId} — read full note${how} only if still relevant]`;
|
|
166
|
+
}
|
|
167
|
+
function formatWrittenNoteLine(note: WrittenNote, options: WrittenNoteOptions): string {
|
|
168
|
+
const elsewhere = pathElsewhere(note, options.notesDirectory);
|
|
169
|
+
const label = formatNoteLabel(note, elsewhere);
|
|
170
|
+
const how = elsewhere === undefined ? "" : " on its path";
|
|
171
|
+
if (options.isPrimary) {
|
|
172
|
+
return `- Active note (written): ${label} ${formatPrimaryNoteProof(note, options.span, how)}`;
|
|
173
|
+
}
|
|
174
|
+
const prefix = options.inCurrentWindow
|
|
175
|
+
? "- Active note (written):"
|
|
176
|
+
: "- Earlier note (written):";
|
|
177
|
+
return `${prefix} ${label} ${formatSecondaryNoteProof(note, {
|
|
178
|
+
span: options.span,
|
|
179
|
+
inCurrentWindow: options.inCurrentWindow,
|
|
180
|
+
how,
|
|
181
|
+
})}`;
|
|
182
|
+
}
|
|
126
183
|
function formatReadNoteLine(
|
|
127
184
|
note: ReadNote,
|
|
128
185
|
span: RetainedSpan | undefined,
|
|
@@ -215,8 +272,20 @@ export function sessionNotesEvidence(
|
|
|
215
272
|
const written = all.filter((note) => note.kind === "written").slice(0, MAX_WRITTEN_NOTES);
|
|
216
273
|
const primary = written[0];
|
|
217
274
|
const readOnly = all.filter((note) => note.kind === "read").slice(0, MAX_READ_NOTES);
|
|
275
|
+
const currentWindowIds = new Set(
|
|
276
|
+
chainEntries
|
|
277
|
+
.slice(contextStart(chainEntries).start)
|
|
278
|
+
.flatMap((entry) => (entry.id !== undefined && entry.id !== "" ? [entry.id] : [])),
|
|
279
|
+
);
|
|
218
280
|
const lines = [
|
|
219
|
-
...written.map((note) =>
|
|
281
|
+
...written.map((note, index) =>
|
|
282
|
+
formatWrittenNoteLine(note, {
|
|
283
|
+
span,
|
|
284
|
+
notesDirectory,
|
|
285
|
+
isPrimary: index === 0,
|
|
286
|
+
inCurrentWindow: currentWindowIds.has(note.entryId),
|
|
287
|
+
}),
|
|
288
|
+
),
|
|
220
289
|
...readOnly.map((note) => formatReadNoteLine(note, span, notesDirectory)),
|
|
221
290
|
];
|
|
222
291
|
const header = "Task notes in this session:";
|
package/src/recovery-records.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isReminderType, OWNER_QUESTION_TOOL, type EntryLike } from "./contracts.ts";
|
|
2
2
|
import { boundedBlock, contentText, imageSummary, imagesOf } from "./message-text.ts";
|
|
3
|
+
import type { Supersession } from "./recovery-coordination.ts";
|
|
3
4
|
|
|
4
5
|
export const MAX_RECOVERY_RECORD_CHARS = 4_000;
|
|
5
6
|
export type RecoveryRecord = {
|
|
@@ -8,6 +9,7 @@ export type RecoveryRecord = {
|
|
|
8
9
|
readonly kind: "owner" | "coordination";
|
|
9
10
|
readonly label: string;
|
|
10
11
|
readonly text: string;
|
|
12
|
+
readonly supersededBy?: Supersession | undefined;
|
|
11
13
|
};
|
|
12
14
|
type RecordSource = {
|
|
13
15
|
readonly kind: RecoveryRecord["kind"];
|
|
@@ -49,29 +51,47 @@ function recordSource(entry: EntryLike, ownerQuestion: boolean): RecordSource |
|
|
|
49
51
|
}
|
|
50
52
|
return;
|
|
51
53
|
}
|
|
54
|
+
function supersessionSuffix(supersededBy: Supersession): string {
|
|
55
|
+
return supersededBy.inRetainedTail
|
|
56
|
+
? `; superseded by entry ${supersededBy.byEntryId} in retained tail`
|
|
57
|
+
: `; superseded by later entry ${supersededBy.byEntryId}`;
|
|
58
|
+
}
|
|
59
|
+
function recordLabel(source: RecordSource, supersededBy: Supersession | undefined): string {
|
|
60
|
+
return supersededBy === undefined
|
|
61
|
+
? source.label
|
|
62
|
+
: `${source.label}${supersessionSuffix(supersededBy)}`;
|
|
63
|
+
}
|
|
64
|
+
function recordBody(source: RecordSource, id: string): string {
|
|
65
|
+
const text = contentText(source.content).trim();
|
|
66
|
+
const summary =
|
|
67
|
+
source.label === "owner answer via ask_question"
|
|
68
|
+
? ""
|
|
69
|
+
: imageSummary(imagesOf(source.content));
|
|
70
|
+
const body = [text, summary === "" ? "" : `${summary} — recover with history read id ${id}`]
|
|
71
|
+
.filter((part) => part !== "")
|
|
72
|
+
.join("\n");
|
|
73
|
+
return body === "" ? "(non-text content; recover the entry from history)" : body;
|
|
74
|
+
}
|
|
52
75
|
export function recoveryRecord(
|
|
53
76
|
entry: EntryLike,
|
|
54
77
|
ownerQuestion: boolean,
|
|
78
|
+
supersededBy?: Supersession,
|
|
55
79
|
): RecoveryRecord | undefined {
|
|
56
80
|
const source = recordSource(entry, ownerQuestion);
|
|
57
81
|
if (source === undefined) {
|
|
58
82
|
return undefined;
|
|
59
83
|
}
|
|
60
84
|
const id = entry.id?.slice(0, 120) ?? "unknown";
|
|
61
|
-
const
|
|
62
|
-
const summary =
|
|
63
|
-
source.label === "owner answer via ask_question"
|
|
64
|
-
? ""
|
|
65
|
-
: imageSummary(imagesOf(source.content));
|
|
66
|
-
const body = [text, summary === "" ? "" : `${summary} — recover with history read id ${id}`]
|
|
67
|
-
.filter((part) => part !== "")
|
|
68
|
-
.join("\n");
|
|
85
|
+
const coordinationSupersession = source.kind === "coordination" ? supersededBy : undefined;
|
|
69
86
|
return {
|
|
70
87
|
id,
|
|
71
88
|
timestamp: entry.timestamp?.slice(0, 80) ?? "unknown time",
|
|
72
89
|
kind: source.kind,
|
|
73
|
-
label: source
|
|
74
|
-
text:
|
|
90
|
+
label: recordLabel(source, coordinationSupersession),
|
|
91
|
+
text: recordBody(source, id),
|
|
92
|
+
...(coordinationSupersession === undefined
|
|
93
|
+
? {}
|
|
94
|
+
: { supersededBy: coordinationSupersession }),
|
|
75
95
|
};
|
|
76
96
|
}
|
|
77
97
|
export function formatRecord(record: RecoveryRecord, limit: number): string {
|
package/src/recovery.ts
CHANGED
|
@@ -41,7 +41,7 @@ function buildRolloverPreamble(span: RetainedSpan | undefined): string {
|
|
|
41
41
|
span.firstId === span.lastId
|
|
42
42
|
? `entry ${span.firstId} above is the carried-over recent turn`
|
|
43
43
|
: `entries ${span.firstId} through ${span.lastId} above are the carried-over recent tail`;
|
|
44
|
-
return `${PREAMBLE}\n${RETAINED_MARKER_PREFIX}${span.lastId}. Earlier turns before entry ${span.firstId} were compacted into session history; ${range} from before this rollover.
|
|
44
|
+
return `${PREAMBLE}\n${RETAINED_MARKER_PREFIX}${span.lastId}. Earlier turns before entry ${span.firstId} were compacted into session history; ${range} from before this rollover. Their inputs and tool evidence are not repeated in this record; new messages follow below. Start the new context window from here.`;
|
|
45
45
|
}
|
|
46
46
|
function priorCheckpoint(entry: EntryLike | undefined, limit: number): string | undefined {
|
|
47
47
|
const handoff = windowHandoff(entry)?.trim();
|