@howaboua/pi-notes-compaction 0.0.9 → 0.0.10
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/CHANGELOG.md +23 -5
- package/PCC-INTEGRATION.md +7 -98
- package/README.md +21 -13
- package/package.json +7 -3
- package/src/activation.ts +152 -0
- package/src/agent-identity.ts +119 -0
- package/src/attachment-snapshot.ts +103 -0
- package/src/binding-startup.ts +30 -0
- package/src/bridge.ts +1 -43
- package/src/compaction-projection.ts +88 -0
- package/src/context-owner.ts +103 -0
- package/src/context-sharing.ts +144 -0
- package/src/extension-hooks.ts +198 -0
- package/src/extension-sharing.ts +118 -0
- package/src/fresh-checkpoint.ts +117 -0
- package/src/fresh-notes.ts +141 -26
- package/src/history-search-protocol.ts +39 -0
- package/src/history-search-pruning.mjs +120 -0
- package/src/history-search-worker.mjs +256 -0
- package/src/history-search.ts +269 -0
- package/src/history.ts +212 -40
- package/src/idle-admission.ts +201 -0
- package/src/index.ts +71 -401
- package/src/integrations.ts +159 -0
- package/src/lifecycle.ts +63 -448
- package/src/note-save-marker.ts +112 -0
- package/src/notes-lookup.ts +112 -0
- package/src/notes.ts +24 -28
- package/src/query-executor.ts +190 -0
- package/src/remote.ts +357 -0
- package/src/rollover.ts +212 -0
- package/src/saved-session.ts +44 -7
- package/src/session-pruning-content.ts +237 -0
- package/src/session-pruning-plan.ts +243 -0
- package/src/session-pruning-policy.ts +20 -0
- package/src/session-pruning-screen.ts +130 -0
- package/src/session-pruning-source.ts +44 -0
- package/src/session-pruning-transaction.ts +141 -0
- package/src/session-pruning.ts +142 -0
- package/src/settings-command.ts +77 -0
- package/src/settings-screen.ts +84 -19
- package/src/settings.ts +143 -11
- package/src/shared-notes.ts +37 -14
- package/src/sharing-service.ts +348 -0
- package/src/store.ts +31 -0
- package/src/task-boundary.ts +6 -1
- package/src/tool-contracts.ts +279 -0
- package/src/tools.ts +19 -560
- package/src/tree-archive.ts +149 -0
- package/src/tree-checkpoint.ts +170 -0
- package/src/tree-handoff-read.ts +192 -0
- package/src/window-checkpoint.ts +105 -0
- package/src/windows.ts +25 -9
package/src/remote.ts
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentToolResult,
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import {
|
|
7
|
+
bindProtectedOutputScope,
|
|
8
|
+
canDeliverProtectedOutput,
|
|
9
|
+
protectedAccountScope,
|
|
10
|
+
resolveActiveCodexProvider,
|
|
11
|
+
resolveProtectedOutputScope,
|
|
12
|
+
} from "@howaboua/pi-codex-conversion/code-mode";
|
|
13
|
+
import {
|
|
14
|
+
type ContextAgentIdentity,
|
|
15
|
+
contextAgentIdentity,
|
|
16
|
+
contextTargetAgent,
|
|
17
|
+
} from "./agent-identity.js";
|
|
18
|
+
import type { CachedResponse, LookupQuery } from "./bridge.js";
|
|
19
|
+
import type {
|
|
20
|
+
RemoteNoteReference,
|
|
21
|
+
SharedContextRequest,
|
|
22
|
+
SharedContextResult,
|
|
23
|
+
} from "./context-sharing.js";
|
|
24
|
+
|
|
25
|
+
const ENDPOINTS = {
|
|
26
|
+
list_files_by_prefix: [
|
|
27
|
+
"file_order",
|
|
28
|
+
"file_order_by",
|
|
29
|
+
"max_results",
|
|
30
|
+
"prefix",
|
|
31
|
+
],
|
|
32
|
+
read_file: ["path", "start_line", "stop_line"],
|
|
33
|
+
search_contents: [
|
|
34
|
+
"max_files",
|
|
35
|
+
"max_matches_per_file",
|
|
36
|
+
"path_prefix",
|
|
37
|
+
"query",
|
|
38
|
+
"recent_file_first",
|
|
39
|
+
],
|
|
40
|
+
} as const;
|
|
41
|
+
|
|
42
|
+
function readQuery(query: LookupQuery): {
|
|
43
|
+
action: string;
|
|
44
|
+
args: Record<string, unknown>;
|
|
45
|
+
} {
|
|
46
|
+
if (query.namespace !== "notes")
|
|
47
|
+
throw new Error(
|
|
48
|
+
"History is available only from saved Pi JSONL sessions. Restore access to the session on this machine, then retry.",
|
|
49
|
+
);
|
|
50
|
+
const action = query.params["action"];
|
|
51
|
+
const actions: Record<string, readonly string[]> = ENDPOINTS;
|
|
52
|
+
if (typeof action !== "string" || !Object.hasOwn(actions, action))
|
|
53
|
+
throw new Error("Remote lookup requires a supported read action");
|
|
54
|
+
const fields = actions[action]!;
|
|
55
|
+
for (const [field, value] of Object.entries(query.params)) {
|
|
56
|
+
if (field === "action") continue;
|
|
57
|
+
if (!fields.includes(field))
|
|
58
|
+
throw new Error(`${query.namespace} ${action} does not accept ${field}`);
|
|
59
|
+
if (value === undefined || value === null) continue;
|
|
60
|
+
if (
|
|
61
|
+
[
|
|
62
|
+
"max_results",
|
|
63
|
+
"max_files",
|
|
64
|
+
"max_matches_per_file",
|
|
65
|
+
"start_line",
|
|
66
|
+
"stop_line",
|
|
67
|
+
].includes(field)
|
|
68
|
+
) {
|
|
69
|
+
if (
|
|
70
|
+
typeof value !== "number" ||
|
|
71
|
+
!Number.isSafeInteger(value) ||
|
|
72
|
+
(!["start_line", "stop_line"].includes(field) && value < 1)
|
|
73
|
+
)
|
|
74
|
+
throw new Error(`Invalid ${field}`);
|
|
75
|
+
} else if (field === "recent_file_first") {
|
|
76
|
+
if (typeof value !== "boolean") throw new Error(`Invalid ${field}`);
|
|
77
|
+
} else if (typeof value !== "string") throw new Error(`Invalid ${field}`);
|
|
78
|
+
}
|
|
79
|
+
const required =
|
|
80
|
+
action === "read_file"
|
|
81
|
+
? ["path"]
|
|
82
|
+
: action === "search_contents"
|
|
83
|
+
? ["query"]
|
|
84
|
+
: [];
|
|
85
|
+
for (const field of required)
|
|
86
|
+
if (typeof query.params[field] !== "string" || !query.params[field])
|
|
87
|
+
throw new Error(`${query.namespace} ${action} requires ${field}`);
|
|
88
|
+
const { action: _action, ...args } = query.params;
|
|
89
|
+
return { action, args };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function scope(
|
|
93
|
+
identity: ContextAgentIdentity,
|
|
94
|
+
accountId: string,
|
|
95
|
+
baseUrl: string,
|
|
96
|
+
): string {
|
|
97
|
+
return JSON.stringify({
|
|
98
|
+
threadId: identity.threadId,
|
|
99
|
+
sessionId: identity.sessionId,
|
|
100
|
+
agentName: identity.agentName,
|
|
101
|
+
accountScope: protectedAccountScope(accountId),
|
|
102
|
+
baseUrl,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function authenticated(ctx: ExtensionContext) {
|
|
107
|
+
const identity = contextAgentIdentity(ctx);
|
|
108
|
+
const before = JSON.stringify([identity, ctx.model]);
|
|
109
|
+
const provider = await resolveActiveCodexProvider(ctx);
|
|
110
|
+
const unchanged = () =>
|
|
111
|
+
before === JSON.stringify([contextAgentIdentity(ctx), ctx.model]);
|
|
112
|
+
if (!unchanged())
|
|
113
|
+
throw new Error(
|
|
114
|
+
"Context changed during authentication; retry in the original session",
|
|
115
|
+
);
|
|
116
|
+
if (
|
|
117
|
+
identity.accountScope &&
|
|
118
|
+
identity.accountScope !== protectedAccountScope(provider.accountId)
|
|
119
|
+
)
|
|
120
|
+
throw new Error("Remote contents belong to a different account");
|
|
121
|
+
if (identity.backendUrl && identity.backendUrl !== provider.baseUrl)
|
|
122
|
+
throw new Error("Remote contents belong to a different backend");
|
|
123
|
+
return {
|
|
124
|
+
identity,
|
|
125
|
+
provider,
|
|
126
|
+
unchanged,
|
|
127
|
+
source: scope(identity, provider.accountId, provider.baseUrl),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Automatic hybrid reads only. Writes and encrypted argument dispatch are deliberately absent. */
|
|
132
|
+
export function createNotesRemoteService(pi: ExtensionAPI) {
|
|
133
|
+
return {
|
|
134
|
+
canProject: (ctx: ExtensionContext) =>
|
|
135
|
+
ctx.model?.api === "openai-codex-responses" &&
|
|
136
|
+
canDeliverProtectedOutput(pi, ctx),
|
|
137
|
+
lookup: lookupRemoteNotes,
|
|
138
|
+
projectResult: projectNotesResponses,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function lookupRemoteNotes(
|
|
143
|
+
query: LookupQuery,
|
|
144
|
+
ctx: ExtensionContext,
|
|
145
|
+
signal?: AbortSignal,
|
|
146
|
+
): Promise<readonly CachedResponse[]> {
|
|
147
|
+
return lookup(query, ctx, signal);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function lookup(
|
|
151
|
+
query: LookupQuery,
|
|
152
|
+
ctx: ExtensionContext,
|
|
153
|
+
signal?: AbortSignal,
|
|
154
|
+
target?: RemoteNoteReference,
|
|
155
|
+
): Promise<readonly CachedResponse[]> {
|
|
156
|
+
const { action, args } = readQuery(query);
|
|
157
|
+
const auth = await authenticated(ctx);
|
|
158
|
+
if (target) checkReference(target, auth.provider);
|
|
159
|
+
const headers = auth.provider.headers;
|
|
160
|
+
headers.set(
|
|
161
|
+
"x-openai-tool-output-truncation-policy",
|
|
162
|
+
JSON.stringify({ mode: "tokens", limit: 10_000 }),
|
|
163
|
+
);
|
|
164
|
+
const response = await fetch(
|
|
165
|
+
`${auth.provider.baseUrl.replace(/\/+$/, "")}/alpha/notes/v2/${action}`,
|
|
166
|
+
{
|
|
167
|
+
method: "POST",
|
|
168
|
+
headers,
|
|
169
|
+
signal: signal
|
|
170
|
+
? AbortSignal.any([signal, AbortSignal.timeout(35_000)])
|
|
171
|
+
: AbortSignal.timeout(35_000),
|
|
172
|
+
body: JSON.stringify({
|
|
173
|
+
...args,
|
|
174
|
+
context: {
|
|
175
|
+
session_id: target?.identity.sessionId ?? auth.identity.sessionId,
|
|
176
|
+
current_agent_name:
|
|
177
|
+
target?.identity.agentName ?? auth.identity.agentName,
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
if (!response.ok)
|
|
183
|
+
throw new Error(`Remote lookup failed (${response.status})`);
|
|
184
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
185
|
+
const envelope: unknown = JSON.parse(new TextDecoder().decode(bytes));
|
|
186
|
+
if (!envelope || typeof envelope !== "object" || Array.isArray(envelope))
|
|
187
|
+
throw new Error("Remote lookup returned invalid data");
|
|
188
|
+
if (!auth.unchanged())
|
|
189
|
+
throw new Error(
|
|
190
|
+
"Context changed during lookup; retry in the original session",
|
|
191
|
+
);
|
|
192
|
+
return [
|
|
193
|
+
{
|
|
194
|
+
query,
|
|
195
|
+
bytes,
|
|
196
|
+
encoding: response.headers.get("content-type") ?? "application/json",
|
|
197
|
+
source: target
|
|
198
|
+
? scope(target.identity, auth.provider.accountId, auth.provider.baseUrl)
|
|
199
|
+
: auth.source,
|
|
200
|
+
fetchedAt: Date.now(),
|
|
201
|
+
coverage: "unknown",
|
|
202
|
+
},
|
|
203
|
+
];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function checkReference(
|
|
207
|
+
reference: RemoteNoteReference,
|
|
208
|
+
provider: { accountId: string; baseUrl: string },
|
|
209
|
+
): void {
|
|
210
|
+
if (
|
|
211
|
+
reference.identity.accountScope !==
|
|
212
|
+
protectedAccountScope(provider.accountId)
|
|
213
|
+
)
|
|
214
|
+
throw new Error("Saved remote contents belong to a different account");
|
|
215
|
+
if (
|
|
216
|
+
reference.baseUrl !== provider.baseUrl ||
|
|
217
|
+
reference.identity.backendUrl !== provider.baseUrl
|
|
218
|
+
)
|
|
219
|
+
throw new Error("Saved remote contents belong to a different backend");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export async function verifyRemoteReference(
|
|
223
|
+
ctx: ExtensionContext,
|
|
224
|
+
reference: RemoteNoteReference,
|
|
225
|
+
): Promise<void> {
|
|
226
|
+
checkReference(reference, (await authenticated(ctx)).provider);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Saved references authorise reads against their original owner, never writes. */
|
|
230
|
+
export async function executeRemoteAttachment(
|
|
231
|
+
ctx: ExtensionContext,
|
|
232
|
+
reference: RemoteNoteReference,
|
|
233
|
+
request: SharedContextRequest,
|
|
234
|
+
signal?: AbortSignal,
|
|
235
|
+
): Promise<SharedContextResult> {
|
|
236
|
+
if (request.namespace === "history")
|
|
237
|
+
throw new Error(
|
|
238
|
+
"History is unavailable from a saved remote notes attachment. Open its saved Pi JSONL session on this machine, then retry.",
|
|
239
|
+
);
|
|
240
|
+
if (
|
|
241
|
+
request.sessionId !== reference.identity.sessionId ||
|
|
242
|
+
(request.agentName !== reference.identity.agentName &&
|
|
243
|
+
!request.agentName.startsWith(`${reference.identity.agentName}/`)) ||
|
|
244
|
+
contextTargetAgent(
|
|
245
|
+
request.namespace,
|
|
246
|
+
request.params,
|
|
247
|
+
reference.identity.agentName,
|
|
248
|
+
) !== request.agentName
|
|
249
|
+
)
|
|
250
|
+
throw new Error(
|
|
251
|
+
"Saved remote request does not belong to its checkpoint owner",
|
|
252
|
+
);
|
|
253
|
+
if (request.encryptedArguments)
|
|
254
|
+
throw new Error("Saved remote reads require ordinary query arguments");
|
|
255
|
+
const responses = await lookup(
|
|
256
|
+
{ namespace: request.namespace, params: request.params },
|
|
257
|
+
ctx,
|
|
258
|
+
signal,
|
|
259
|
+
reference,
|
|
260
|
+
);
|
|
261
|
+
return {
|
|
262
|
+
content: [{ type: "text", text: "Saved remote read completed" }],
|
|
263
|
+
details: {
|
|
264
|
+
codexHistoryNotes: {},
|
|
265
|
+
externalNotesResponses: responses.map(({ bytes, ...response }) => ({
|
|
266
|
+
...response,
|
|
267
|
+
bytesBase64: Buffer.from(bytes).toString("base64"),
|
|
268
|
+
})),
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function projectNotesResponses(
|
|
274
|
+
result: AgentToolResult<unknown>,
|
|
275
|
+
responses: readonly CachedResponse[],
|
|
276
|
+
callId: string,
|
|
277
|
+
ctx: ExtensionContext,
|
|
278
|
+
signal?: AbortSignal,
|
|
279
|
+
relay = false,
|
|
280
|
+
): Promise<AgentToolResult<unknown>> {
|
|
281
|
+
signal?.throwIfAborted();
|
|
282
|
+
if (!callId)
|
|
283
|
+
throw new Error("Protected delivery requires a current tool call");
|
|
284
|
+
if (!responses.length)
|
|
285
|
+
return { ...result, content: result.content.map((part) => ({ ...part })) };
|
|
286
|
+
const auth = await authenticated(ctx);
|
|
287
|
+
const source = responses[0]!.source;
|
|
288
|
+
if (relay) {
|
|
289
|
+
const provenance: unknown = JSON.parse(source);
|
|
290
|
+
if (
|
|
291
|
+
!provenance ||
|
|
292
|
+
typeof provenance !== "object" ||
|
|
293
|
+
!("accountScope" in provenance) ||
|
|
294
|
+
provenance.accountScope !==
|
|
295
|
+
protectedAccountScope(auth.provider.accountId) ||
|
|
296
|
+
!("baseUrl" in provenance) ||
|
|
297
|
+
provenance.baseUrl !== auth.provider.baseUrl ||
|
|
298
|
+
!("threadId" in provenance) ||
|
|
299
|
+
typeof provenance.threadId !== "string" ||
|
|
300
|
+
!provenance.threadId ||
|
|
301
|
+
!("sessionId" in provenance) ||
|
|
302
|
+
typeof provenance.sessionId !== "string" ||
|
|
303
|
+
!provenance.sessionId ||
|
|
304
|
+
!("agentName" in provenance) ||
|
|
305
|
+
typeof provenance.agentName !== "string" ||
|
|
306
|
+
!/^\/root(?:\/[a-zA-Z0-9_-]+)*$/.test(provenance.agentName)
|
|
307
|
+
)
|
|
308
|
+
throw new Error(
|
|
309
|
+
"Shared protected contents belong to a different account or backend",
|
|
310
|
+
);
|
|
311
|
+
} else if (source !== auth.source)
|
|
312
|
+
throw new Error("Cached response belongs to a different context");
|
|
313
|
+
const protectedOutputs = responses.map((response, index) => {
|
|
314
|
+
if (response.source !== source)
|
|
315
|
+
throw new Error("Protected responses have different owners");
|
|
316
|
+
readQuery(response.query);
|
|
317
|
+
const envelope: unknown = JSON.parse(
|
|
318
|
+
new TextDecoder().decode(response.bytes),
|
|
319
|
+
);
|
|
320
|
+
if (
|
|
321
|
+
!envelope ||
|
|
322
|
+
typeof envelope !== "object" ||
|
|
323
|
+
!("encrypted_output" in envelope) ||
|
|
324
|
+
typeof envelope.encrypted_output !== "string" ||
|
|
325
|
+
!envelope.encrypted_output.trim()
|
|
326
|
+
)
|
|
327
|
+
throw new Error("Cached response contains no protected output");
|
|
328
|
+
const hint =
|
|
329
|
+
"attachment_hint" in envelope &&
|
|
330
|
+
typeof envelope.attachment_hint === "string"
|
|
331
|
+
? envelope.attachment_hint
|
|
332
|
+
: undefined;
|
|
333
|
+
return {
|
|
334
|
+
resultId: `${callId}:${index}`,
|
|
335
|
+
name: response.query.namespace + (hint ? `; ${hint}` : ""),
|
|
336
|
+
encryptedOutput: envelope.encrypted_output,
|
|
337
|
+
};
|
|
338
|
+
});
|
|
339
|
+
const deliveryScope = await resolveProtectedOutputScope(ctx);
|
|
340
|
+
if (!auth.unchanged())
|
|
341
|
+
throw new Error(
|
|
342
|
+
"Context changed during protected delivery; retry in the original session",
|
|
343
|
+
);
|
|
344
|
+
signal?.throwIfAborted();
|
|
345
|
+
const details = {
|
|
346
|
+
...(result.details && typeof result.details === "object"
|
|
347
|
+
? result.details
|
|
348
|
+
: {}),
|
|
349
|
+
protectedOutputs,
|
|
350
|
+
};
|
|
351
|
+
bindProtectedOutputScope(details, deliveryScope);
|
|
352
|
+
return {
|
|
353
|
+
...result,
|
|
354
|
+
content: result.content.map((part) => ({ ...part })),
|
|
355
|
+
details,
|
|
356
|
+
};
|
|
357
|
+
}
|
package/src/rollover.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CustomMessageEntryDraft,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
SessionBeforeCompactEvent,
|
|
5
|
+
SessionCompactFailedEvent,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { prepareCompactionProjection } from "./compaction-projection.js";
|
|
8
|
+
import type { NotesLifecycle } from "./lifecycle.js";
|
|
9
|
+
|
|
10
|
+
type Rollover = {
|
|
11
|
+
sessionId: string;
|
|
12
|
+
windowId: string | undefined;
|
|
13
|
+
triggerTurn: boolean;
|
|
14
|
+
} & (
|
|
15
|
+
| { phase: "scheduled" | "compacting" }
|
|
16
|
+
| { phase: "checking"; accepted?: AbortSignal }
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
// Owns scheduled rollover, compaction callback identity and manual cancellation receipts.
|
|
20
|
+
export class RolloverRequests {
|
|
21
|
+
private rollover: Rollover | undefined;
|
|
22
|
+
private cancelledCompact:
|
|
23
|
+
| {
|
|
24
|
+
sessionId: string;
|
|
25
|
+
signal: AbortSignal;
|
|
26
|
+
instructions: string | undefined;
|
|
27
|
+
}
|
|
28
|
+
| undefined;
|
|
29
|
+
private readonly host: Pick<
|
|
30
|
+
NotesLifecycle,
|
|
31
|
+
"active" | "windows" | "normalCompaction" | "newWindowRequests" | "fresh"
|
|
32
|
+
>;
|
|
33
|
+
private readonly openWindow: (
|
|
34
|
+
ctx: ExtensionContext,
|
|
35
|
+
triggerTurn: boolean,
|
|
36
|
+
trim: boolean,
|
|
37
|
+
) => Promise<void>;
|
|
38
|
+
private readonly sendMessage: (
|
|
39
|
+
message: Omit<CustomMessageEntryDraft, "type">,
|
|
40
|
+
options: { triggerTurn: boolean },
|
|
41
|
+
) => void;
|
|
42
|
+
private readonly startCheckpoint: (
|
|
43
|
+
ctx: ExtensionContext,
|
|
44
|
+
instructions?: string,
|
|
45
|
+
) => void;
|
|
46
|
+
constructor(
|
|
47
|
+
host: RolloverRequests["host"],
|
|
48
|
+
openWindow: RolloverRequests["openWindow"],
|
|
49
|
+
sendMessage: RolloverRequests["sendMessage"],
|
|
50
|
+
startCheckpoint: RolloverRequests["startCheckpoint"],
|
|
51
|
+
) {
|
|
52
|
+
this.host = host;
|
|
53
|
+
this.openWindow = openWindow;
|
|
54
|
+
this.sendMessage = sendMessage;
|
|
55
|
+
this.startCheckpoint = startCheckpoint;
|
|
56
|
+
}
|
|
57
|
+
get pending(): boolean {
|
|
58
|
+
return Boolean(this.rollover);
|
|
59
|
+
}
|
|
60
|
+
reset(): void {
|
|
61
|
+
this.rollover = undefined;
|
|
62
|
+
this.cancelledCompact = undefined;
|
|
63
|
+
}
|
|
64
|
+
clear(): void {
|
|
65
|
+
this.rollover = undefined;
|
|
66
|
+
}
|
|
67
|
+
schedule(ctx: ExtensionContext): void {
|
|
68
|
+
this.rollover = {
|
|
69
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
70
|
+
windowId: this.host.windows.current?.currentWindowId,
|
|
71
|
+
phase: "scheduled",
|
|
72
|
+
triggerTurn: true,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
settled(ctx: ExtensionContext): Promise<void> | undefined {
|
|
76
|
+
const rollover = this.rollover;
|
|
77
|
+
if (
|
|
78
|
+
rollover?.phase === "scheduled" &&
|
|
79
|
+
rollover.sessionId === ctx.sessionManager.getSessionId() &&
|
|
80
|
+
rollover.windowId === this.host.windows.current?.currentWindowId
|
|
81
|
+
) {
|
|
82
|
+
this.rollover = undefined;
|
|
83
|
+
return this.roll(ctx, rollover.triggerTurn);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async roll(ctx: ExtensionContext, triggerTurn: boolean): Promise<void> {
|
|
88
|
+
const policy = triggerTurn ? this.host.newWindowRequests : undefined;
|
|
89
|
+
const checkOnly = !this.host.normalCompaction && policy === "pi-limits";
|
|
90
|
+
if (!this.host.normalCompaction && !checkOnly) {
|
|
91
|
+
await this.openWindow(ctx, triggerTurn, true);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const pending: Rollover = {
|
|
95
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
96
|
+
windowId: this.host.windows.current?.currentWindowId,
|
|
97
|
+
phase: checkOnly ? "checking" : "compacting",
|
|
98
|
+
triggerTurn,
|
|
99
|
+
};
|
|
100
|
+
this.rollover = pending;
|
|
101
|
+
await new Promise<void>((resolve, reject) => {
|
|
102
|
+
const current = () =>
|
|
103
|
+
this.host.active &&
|
|
104
|
+
this.rollover === pending &&
|
|
105
|
+
pending.sessionId === ctx.sessionManager.getSessionId() &&
|
|
106
|
+
pending.windowId === this.host.windows.current?.currentWindowId;
|
|
107
|
+
const finish = (trim: boolean) => {
|
|
108
|
+
this.rollover = undefined;
|
|
109
|
+
void this.openWindow(ctx, triggerTurn, trim).then(resolve, reject);
|
|
110
|
+
};
|
|
111
|
+
ctx.compact({
|
|
112
|
+
onComplete: () => {
|
|
113
|
+
if (!current()) {
|
|
114
|
+
reject(new Error("Session changed during compaction"));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
finish(false);
|
|
118
|
+
},
|
|
119
|
+
onError: (error) => {
|
|
120
|
+
if (current()) {
|
|
121
|
+
// Only our own accepted gate cancels a summary in favour of notes-only rollover.
|
|
122
|
+
if (
|
|
123
|
+
pending.phase === "checking" &&
|
|
124
|
+
pending.accepted &&
|
|
125
|
+
!pending.accepted.aborted &&
|
|
126
|
+
error.message === "Compaction cancelled"
|
|
127
|
+
) {
|
|
128
|
+
finish(true);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (
|
|
132
|
+
error.message === "Nothing to compact (session too small)" ||
|
|
133
|
+
error.message === "Already compacted"
|
|
134
|
+
) {
|
|
135
|
+
if (policy === "unrestricted") {
|
|
136
|
+
// A new identity need not retire history when Pi has no summary work.
|
|
137
|
+
finish(false);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
this.rollover = undefined;
|
|
141
|
+
try {
|
|
142
|
+
if (triggerTurn)
|
|
143
|
+
this.sendMessage(
|
|
144
|
+
{
|
|
145
|
+
customType: "notes-compaction:continuation",
|
|
146
|
+
content:
|
|
147
|
+
"Not enough history to compact. Continue here; wait for more conversation before calling new_context again.",
|
|
148
|
+
display: true,
|
|
149
|
+
},
|
|
150
|
+
{ triggerTurn: true },
|
|
151
|
+
);
|
|
152
|
+
resolve();
|
|
153
|
+
} catch (error) {
|
|
154
|
+
reject(error);
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (this.rollover === pending) this.rollover = undefined;
|
|
160
|
+
reject(error);
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
beforeCompact(
|
|
166
|
+
event: SessionBeforeCompactEvent,
|
|
167
|
+
ctx: ExtensionContext,
|
|
168
|
+
): { cancel: true } | undefined {
|
|
169
|
+
if (this.rollover?.phase === "checking" && event.reason === "manual") {
|
|
170
|
+
this.rollover.accepted = event.signal;
|
|
171
|
+
return { cancel: true };
|
|
172
|
+
}
|
|
173
|
+
if (this.allowsCompaction(event.reason)) {
|
|
174
|
+
return prepareCompactionProjection(event, ctx, this.host.windows);
|
|
175
|
+
}
|
|
176
|
+
if (event.reason === "manual")
|
|
177
|
+
this.cancelledCompact = {
|
|
178
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
179
|
+
signal: event.signal,
|
|
180
|
+
instructions: event.customInstructions,
|
|
181
|
+
};
|
|
182
|
+
return { cancel: true };
|
|
183
|
+
}
|
|
184
|
+
allowsCompaction(reason: SessionBeforeCompactEvent["reason"]): boolean {
|
|
185
|
+
if (this.rollover?.phase === "checking" && reason === "manual")
|
|
186
|
+
return false;
|
|
187
|
+
return (
|
|
188
|
+
!this.host.active ||
|
|
189
|
+
this.host.normalCompaction ||
|
|
190
|
+
this.rollover?.phase === "compacting" ||
|
|
191
|
+
reason === "overflow"
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
async compactFailed(
|
|
195
|
+
event: SessionCompactFailedEvent,
|
|
196
|
+
ctx: ExtensionContext,
|
|
197
|
+
): Promise<void> {
|
|
198
|
+
const pending = this.cancelledCompact;
|
|
199
|
+
this.cancelledCompact = undefined;
|
|
200
|
+
if (
|
|
201
|
+
!pending ||
|
|
202
|
+
event.reason !== "manual" ||
|
|
203
|
+
!event.aborted ||
|
|
204
|
+
pending.signal.aborted ||
|
|
205
|
+
pending.sessionId !== ctx.sessionManager.getSessionId()
|
|
206
|
+
)
|
|
207
|
+
return;
|
|
208
|
+
if (!pending.instructions?.trim() && this.host.fresh(ctx))
|
|
209
|
+
await this.roll(ctx, false);
|
|
210
|
+
else this.startCheckpoint(ctx, pending.instructions);
|
|
211
|
+
}
|
|
212
|
+
}
|
package/src/saved-session.ts
CHANGED
|
@@ -2,6 +2,11 @@ import { type FileHandle, open } from "node:fs/promises";
|
|
|
2
2
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { Type } from "typebox";
|
|
4
4
|
import { Check } from "typebox/value";
|
|
5
|
+
import {
|
|
6
|
+
isTreeArchiveManifestData,
|
|
7
|
+
MAX_ARCHIVE_DEPTH,
|
|
8
|
+
TREE_ARCHIVE_ENTRY_TYPE,
|
|
9
|
+
} from "./tree-archive.js";
|
|
5
10
|
|
|
6
11
|
const SessionHeader = Type.Object({
|
|
7
12
|
type: Type.Literal("session"),
|
|
@@ -78,6 +83,10 @@ export async function readSavedNotesSession(
|
|
|
78
83
|
const before = await file.stat();
|
|
79
84
|
const entries: SessionEntry[] = [];
|
|
80
85
|
const branch: SessionEntry[] = [];
|
|
86
|
+
const archived = new Map<
|
|
87
|
+
string,
|
|
88
|
+
{ base: string | null; depth: number }[]
|
|
89
|
+
>();
|
|
81
90
|
let threadId: string | undefined;
|
|
82
91
|
let target: string | null | undefined;
|
|
83
92
|
for await (const line of reverseLines(file, before.size)) {
|
|
@@ -100,18 +109,46 @@ export async function readSavedNotesSession(
|
|
|
100
109
|
if (target === undefined) target = entry.id;
|
|
101
110
|
const active = entry.id === target;
|
|
102
111
|
if (active) target = entry.parentId;
|
|
112
|
+
if (history) {
|
|
113
|
+
// Pi appends descendants after their parents, including Tree manifests
|
|
114
|
+
// after their archived leaves. Follow only active manifests' ancestry.
|
|
115
|
+
const paths = archived.get(entry.id) ?? [];
|
|
116
|
+
archived.delete(entry.id);
|
|
117
|
+
for (const path of paths) {
|
|
118
|
+
if (++path.depth >= MAX_ARCHIVE_DEPTH || entry.parentId === path.base)
|
|
119
|
+
continue;
|
|
120
|
+
if (entry.parentId !== null) {
|
|
121
|
+
const parents = archived.get(entry.parentId) ?? [];
|
|
122
|
+
parents.push(path);
|
|
123
|
+
archived.set(entry.parentId, parents);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (
|
|
127
|
+
active &&
|
|
128
|
+
Check(Custom, entry) &&
|
|
129
|
+
entry.customType === TREE_ARCHIVE_ENTRY_TYPE &&
|
|
130
|
+
isTreeArchiveManifestData(entry.data) &&
|
|
131
|
+
entry.data.archivedLeafId !== entry.data.branchBaseId
|
|
132
|
+
) {
|
|
133
|
+
const paths = archived.get(entry.data.archivedLeafId) ?? [];
|
|
134
|
+
paths.push({ base: entry.data.branchBaseId, depth: 0 });
|
|
135
|
+
archived.set(entry.data.archivedLeafId, paths);
|
|
136
|
+
}
|
|
137
|
+
if (
|
|
138
|
+
active ||
|
|
139
|
+
paths.length ||
|
|
140
|
+
(Check(Custom, entry) && SHARING_ENTRIES.has(entry.customType))
|
|
141
|
+
)
|
|
142
|
+
entries.push(entry as SessionEntry);
|
|
143
|
+
if (active) branch.push(entry as SessionEntry);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
103
146
|
if (Check(Custom, entry)) {
|
|
104
147
|
if (SHARING_ENTRIES.has(entry.customType)) entries.push(entry);
|
|
105
148
|
else if (active && NOTE_ENTRIES.has(entry.customType))
|
|
106
149
|
branch.push(entry);
|
|
107
150
|
else continue;
|
|
108
|
-
} else if (
|
|
109
|
-
active &&
|
|
110
|
-
history &&
|
|
111
|
-
(entry.type === "message" || entry.type === "custom_message")
|
|
112
|
-
)
|
|
113
|
-
branch.push(entry as SessionEntry);
|
|
114
|
-
else if (active && Check(Handoff, entry)) branch.push(entry);
|
|
151
|
+
} else if (active && Check(Handoff, entry)) branch.push(entry);
|
|
115
152
|
else continue;
|
|
116
153
|
}
|
|
117
154
|
const after = await file.stat();
|