@howaboua/pi-notes-compaction 0.0.4 → 0.0.6
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 +21 -0
- package/PCC-INTEGRATION.md +7 -3
- package/README.md +24 -3
- package/package.json +2 -2
- package/src/bridge.ts +8 -0
- package/src/fresh-notes.ts +95 -24
- package/src/index.ts +183 -32
- package/src/lifecycle.ts +147 -38
- package/src/notes.ts +10 -1
- package/src/saved-session.ts +124 -0
- package/src/settings-screen.ts +118 -19
- package/src/settings.ts +72 -1
- package/src/shared-notes.ts +377 -0
- package/src/task-boundary.ts +32 -0
- package/src/tools.ts +48 -24
- package/src/windows.ts +150 -28
package/src/lifecycle.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import type {
|
|
3
3
|
AgentSettledEvent,
|
|
4
|
+
BeforeAgentStartEvent,
|
|
4
5
|
ExtensionAPI,
|
|
5
6
|
ExtensionContext,
|
|
6
7
|
InputEvent,
|
|
@@ -13,9 +14,14 @@ import {
|
|
|
13
14
|
prepareBranchEntries,
|
|
14
15
|
} from "@earendil-works/pi-coding-agent";
|
|
15
16
|
import type { NotesBridge } from "./bridge.js";
|
|
16
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
hasFreshNotes,
|
|
19
|
+
hasPrecedingNoteWrite,
|
|
20
|
+
NOTES_RUN_ENTRY,
|
|
21
|
+
} from "./fresh-notes.js";
|
|
17
22
|
import { noteHints, sessionRef } from "./notes.js";
|
|
18
|
-
import type { IdleMinutes } from "./settings.js";
|
|
23
|
+
import type { IdleMinutes, NewWindowRequests } from "./settings.js";
|
|
24
|
+
import { savedNotesIdentity } from "./shared-notes.js";
|
|
19
25
|
import type { NotesStore } from "./store.js";
|
|
20
26
|
import {
|
|
21
27
|
SETTLEMENT_ENTRY,
|
|
@@ -28,8 +34,7 @@ import {
|
|
|
28
34
|
interface Checkpoint {
|
|
29
35
|
sessionId: string;
|
|
30
36
|
windowId: string;
|
|
31
|
-
|
|
32
|
-
admitted: boolean;
|
|
37
|
+
requestId: string;
|
|
33
38
|
runId: string | undefined;
|
|
34
39
|
outcome: "completed" | "aborted" | "error" | undefined;
|
|
35
40
|
}
|
|
@@ -39,22 +44,28 @@ interface HeldInput {
|
|
|
39
44
|
releasePrompt: string;
|
|
40
45
|
admissions: ((result: InputEventResult) => void)[];
|
|
41
46
|
}
|
|
42
|
-
|
|
47
|
+
type Rollover = {
|
|
43
48
|
sessionId: string;
|
|
44
|
-
|
|
49
|
+
windowId: string | undefined;
|
|
45
50
|
triggerTurn: boolean;
|
|
46
|
-
}
|
|
51
|
+
} & (
|
|
52
|
+
| { phase: "scheduled" | "compacting" }
|
|
53
|
+
| { phase: "checking"; accepted?: AbortSignal }
|
|
54
|
+
);
|
|
47
55
|
|
|
48
56
|
export class NotesLifecycle {
|
|
49
57
|
readonly windows = new Windows();
|
|
50
58
|
active = false;
|
|
51
59
|
normalCompaction = false;
|
|
60
|
+
newWindowRequests: NewWindowRequests = "unrestricted";
|
|
52
61
|
idleMinutes: IdleMinutes = 0;
|
|
62
|
+
taskBoundaryReminder = 0;
|
|
53
63
|
bridge: NotesBridge | undefined;
|
|
54
64
|
runId = "";
|
|
55
65
|
private checkpoint: Checkpoint | undefined;
|
|
56
66
|
private held: HeldInput | undefined;
|
|
57
67
|
private rollover: Rollover | undefined;
|
|
68
|
+
private customAdmission: Promise<void> | undefined;
|
|
58
69
|
private cancelledCompact:
|
|
59
70
|
| {
|
|
60
71
|
sessionId: string;
|
|
@@ -70,9 +81,16 @@ export class NotesLifecycle {
|
|
|
70
81
|
this.store = store;
|
|
71
82
|
}
|
|
72
83
|
agent(ctx: ExtensionContext): string {
|
|
73
|
-
return
|
|
84
|
+
return (
|
|
85
|
+
this.bridge?.agentName?.(ctx) ??
|
|
86
|
+
savedNotesIdentity({
|
|
87
|
+
threadId: ctx.sessionManager.getSessionId(),
|
|
88
|
+
entries: ctx.sessionManager.getEntries(),
|
|
89
|
+
}).agentName
|
|
90
|
+
);
|
|
74
91
|
}
|
|
75
92
|
reset(ctx?: ExtensionContext): void {
|
|
93
|
+
this.customAdmission = undefined;
|
|
76
94
|
this.checkpoint = undefined;
|
|
77
95
|
this.rollover = undefined;
|
|
78
96
|
this.cancelledCompact = undefined;
|
|
@@ -122,10 +140,34 @@ export class NotesLifecycle {
|
|
|
122
140
|
);
|
|
123
141
|
}
|
|
124
142
|
}
|
|
125
|
-
beforeStart(ctx: ExtensionContext): void {
|
|
143
|
+
beforeStart(ctx: ExtensionContext, event: BeforeAgentStartEvent): void {
|
|
126
144
|
this.initialize(ctx);
|
|
127
145
|
this.runId = randomUUID();
|
|
128
|
-
|
|
146
|
+
this.pi.appendEntry(NOTES_RUN_ENTRY, { runId: this.runId });
|
|
147
|
+
if (
|
|
148
|
+
this.checkpoint &&
|
|
149
|
+
event.messageType === "custom" &&
|
|
150
|
+
event.message.customType === "notes-compaction:checkpoint:v1" &&
|
|
151
|
+
(event.message.details as { requestId?: string } | undefined)
|
|
152
|
+
?.requestId === this.checkpoint.requestId
|
|
153
|
+
)
|
|
154
|
+
this.checkpoint.runId = this.runId;
|
|
155
|
+
}
|
|
156
|
+
prepareCustomTurn(ctx: ExtensionContext): Promise<void> | undefined {
|
|
157
|
+
if (this.customAdmission) return this.customAdmission;
|
|
158
|
+
if (!this.active || !this.idleDue(ctx) || !this.fresh(ctx)) return;
|
|
159
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
160
|
+
const preparation = this.roll(ctx, false)
|
|
161
|
+
.then(() => {
|
|
162
|
+
if (!this.active || sessionId !== ctx.sessionManager.getSessionId())
|
|
163
|
+
throw new Error("Session changed during custom kickoff admission");
|
|
164
|
+
})
|
|
165
|
+
.finally(() => {
|
|
166
|
+
if (this.customAdmission === preparation)
|
|
167
|
+
this.customAdmission = undefined;
|
|
168
|
+
});
|
|
169
|
+
this.customAdmission = preparation;
|
|
170
|
+
return preparation;
|
|
129
171
|
}
|
|
130
172
|
outcome(outcome: "completed" | "aborted" | "error"): void {
|
|
131
173
|
if (this.checkpoint) this.checkpoint.outcome = outcome;
|
|
@@ -137,11 +179,27 @@ export class NotesLifecycle {
|
|
|
137
179
|
runId,
|
|
138
180
|
);
|
|
139
181
|
}
|
|
140
|
-
requestRollover(ctx: ExtensionContext): boolean {
|
|
182
|
+
requestRollover(ctx: ExtensionContext, callId: string): boolean {
|
|
183
|
+
if (this.newWindowRequests === "off")
|
|
184
|
+
throw new Error(
|
|
185
|
+
"New window requests are disabled. Continue in the current window.",
|
|
186
|
+
);
|
|
141
187
|
if (this.checkpoint || this.held) return false;
|
|
142
188
|
if (this.rollover) return false;
|
|
189
|
+
if (
|
|
190
|
+
!hasPrecedingNoteWrite(
|
|
191
|
+
ctx.sessionManager.getBranch(),
|
|
192
|
+
this.windows.current?.currentWindowId,
|
|
193
|
+
this.runId,
|
|
194
|
+
callId,
|
|
195
|
+
)
|
|
196
|
+
)
|
|
197
|
+
throw new Error(
|
|
198
|
+
"Save your progress with notes, wait for the write to succeed, then call new_context next.",
|
|
199
|
+
);
|
|
143
200
|
this.rollover = {
|
|
144
201
|
sessionId: ctx.sessionManager.getSessionId(),
|
|
202
|
+
windowId: this.windows.current?.currentWindowId,
|
|
145
203
|
phase: "scheduled",
|
|
146
204
|
triggerTurn: true,
|
|
147
205
|
};
|
|
@@ -164,36 +222,91 @@ export class NotesLifecycle {
|
|
|
164
222
|
triggerTurn: false,
|
|
165
223
|
});
|
|
166
224
|
this.windows.current = ids;
|
|
167
|
-
if (triggerTurn)
|
|
225
|
+
if (triggerTurn)
|
|
226
|
+
this.pi.sendMessage(
|
|
227
|
+
{
|
|
228
|
+
customType: "notes-compaction:continuation",
|
|
229
|
+
content: "Read your saved notes and resume the task.",
|
|
230
|
+
display: false,
|
|
231
|
+
},
|
|
232
|
+
{ triggerTurn: true },
|
|
233
|
+
);
|
|
168
234
|
}
|
|
169
235
|
private async roll(
|
|
170
236
|
ctx: ExtensionContext,
|
|
171
237
|
triggerTurn: boolean,
|
|
172
238
|
): Promise<void> {
|
|
173
|
-
|
|
239
|
+
const policy = triggerTurn ? this.newWindowRequests : undefined;
|
|
240
|
+
const checkOnly = !this.normalCompaction && policy === "pi-limits";
|
|
241
|
+
if (!this.normalCompaction && !checkOnly) {
|
|
174
242
|
await this.openWindow(ctx, triggerTurn, true);
|
|
175
243
|
return;
|
|
176
244
|
}
|
|
177
245
|
const pending: Rollover = {
|
|
178
246
|
sessionId: ctx.sessionManager.getSessionId(),
|
|
179
|
-
|
|
247
|
+
windowId: this.windows.current?.currentWindowId,
|
|
248
|
+
phase: checkOnly ? "checking" : "compacting",
|
|
180
249
|
triggerTurn,
|
|
181
250
|
};
|
|
182
251
|
this.rollover = pending;
|
|
183
252
|
await new Promise<void>((resolve, reject) => {
|
|
253
|
+
const current = () =>
|
|
254
|
+
this.active &&
|
|
255
|
+
this.rollover === pending &&
|
|
256
|
+
pending.sessionId === ctx.sessionManager.getSessionId() &&
|
|
257
|
+
pending.windowId === this.windows.current?.currentWindowId;
|
|
258
|
+
const finish = (trim: boolean) => {
|
|
259
|
+
this.rollover = undefined;
|
|
260
|
+
void this.openWindow(ctx, triggerTurn, trim).then(resolve, reject);
|
|
261
|
+
};
|
|
184
262
|
ctx.compact({
|
|
185
263
|
onComplete: () => {
|
|
186
|
-
if (
|
|
187
|
-
this.rollover !== pending ||
|
|
188
|
-
pending.sessionId !== ctx.sessionManager.getSessionId()
|
|
189
|
-
) {
|
|
264
|
+
if (!current()) {
|
|
190
265
|
reject(new Error("Session changed during compaction"));
|
|
191
266
|
return;
|
|
192
267
|
}
|
|
193
|
-
|
|
194
|
-
void this.openWindow(ctx, triggerTurn, false).then(resolve, reject);
|
|
268
|
+
finish(false);
|
|
195
269
|
},
|
|
196
270
|
onError: (error) => {
|
|
271
|
+
if (current()) {
|
|
272
|
+
// Only our own accepted gate cancels a summary in favour of notes-only rollover.
|
|
273
|
+
if (
|
|
274
|
+
pending.phase === "checking" &&
|
|
275
|
+
pending.accepted &&
|
|
276
|
+
!pending.accepted.aborted &&
|
|
277
|
+
error.message === "Compaction cancelled"
|
|
278
|
+
) {
|
|
279
|
+
finish(true);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (
|
|
283
|
+
error.message === "Nothing to compact (session too small)" ||
|
|
284
|
+
error.message === "Already compacted"
|
|
285
|
+
) {
|
|
286
|
+
if (policy === "unrestricted") {
|
|
287
|
+
// A new identity need not retire history when Pi has no summary work.
|
|
288
|
+
finish(false);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
this.rollover = undefined;
|
|
292
|
+
try {
|
|
293
|
+
if (triggerTurn)
|
|
294
|
+
this.pi.sendMessage(
|
|
295
|
+
{
|
|
296
|
+
customType: "notes-compaction:continuation",
|
|
297
|
+
content:
|
|
298
|
+
"Not enough history to compact. Continue here; wait for more conversation before calling new_context again.",
|
|
299
|
+
display: true,
|
|
300
|
+
},
|
|
301
|
+
{ triggerTurn: true },
|
|
302
|
+
);
|
|
303
|
+
resolve();
|
|
304
|
+
} catch (error) {
|
|
305
|
+
reject(error);
|
|
306
|
+
}
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
197
310
|
if (this.rollover === pending) this.rollover = undefined;
|
|
198
311
|
reject(error);
|
|
199
312
|
},
|
|
@@ -203,12 +316,11 @@ export class NotesLifecycle {
|
|
|
203
316
|
private startCheckpoint(ctx: ExtensionContext, instructions?: string): void {
|
|
204
317
|
if (!ctx.isIdle() || !this.windows.current || this.checkpoint)
|
|
205
318
|
throw new Error("A checkpoint cannot start until the session settles");
|
|
206
|
-
const
|
|
319
|
+
const requestId = randomUUID();
|
|
207
320
|
this.checkpoint = {
|
|
208
321
|
sessionId: ctx.sessionManager.getSessionId(),
|
|
209
322
|
windowId: this.windows.current.currentWindowId,
|
|
210
|
-
|
|
211
|
-
admitted: false,
|
|
323
|
+
requestId,
|
|
212
324
|
runId: undefined,
|
|
213
325
|
outcome: undefined,
|
|
214
326
|
};
|
|
@@ -216,13 +328,12 @@ export class NotesLifecycle {
|
|
|
216
328
|
this.pi.sendMessage(
|
|
217
329
|
{
|
|
218
330
|
customType: "notes-compaction:checkpoint:v1",
|
|
219
|
-
|
|
331
|
+
details: { requestId },
|
|
332
|
+
content: `<context_window_reminder>\nSave the task state in notes, then finish your reply. A new window opens automatically after your reply; do not call new_context. If saving fails, report the error.\n</context_window_reminder>${instructions?.trim() ? `\n\nCheckpoint guidance:\n${instructions}` : ""}`,
|
|
220
333
|
display: true,
|
|
221
334
|
},
|
|
222
|
-
{ triggerTurn:
|
|
335
|
+
{ triggerTurn: true },
|
|
223
336
|
);
|
|
224
|
-
// A settled user kickoff runs ALL input and before_agent_start handlers.
|
|
225
|
-
this.pi.sendUserMessage(prompt);
|
|
226
337
|
} catch (error) {
|
|
227
338
|
this.checkpoint = undefined;
|
|
228
339
|
throw error;
|
|
@@ -286,16 +397,6 @@ export class NotesLifecycle {
|
|
|
286
397
|
admit?.({ action: "continue" });
|
|
287
398
|
return { action: "handled" };
|
|
288
399
|
}
|
|
289
|
-
const checkpoint = this.checkpoint;
|
|
290
|
-
if (
|
|
291
|
-
checkpoint &&
|
|
292
|
-
!checkpoint.admitted &&
|
|
293
|
-
event.source === "extension" &&
|
|
294
|
-
event.text === checkpoint.prompt
|
|
295
|
-
) {
|
|
296
|
-
checkpoint.admitted = true;
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
400
|
if (!this.held && (!this.idleDue(ctx) || !this.fresh(ctx))) return;
|
|
300
401
|
const held =
|
|
301
402
|
this.held ??
|
|
@@ -357,6 +458,10 @@ export class NotesLifecycle {
|
|
|
357
458
|
event: SessionBeforeCompactEvent,
|
|
358
459
|
ctx: ExtensionContext,
|
|
359
460
|
): { cancel: true } | undefined {
|
|
461
|
+
if (this.rollover?.phase === "checking" && event.reason === "manual") {
|
|
462
|
+
this.rollover.accepted = event.signal;
|
|
463
|
+
return { cancel: true };
|
|
464
|
+
}
|
|
360
465
|
if (this.allowsCompaction(event.reason)) {
|
|
361
466
|
// PCC owns preparation when bridged, including native checkpoint routes.
|
|
362
467
|
if (this.bridge) return;
|
|
@@ -432,7 +537,10 @@ export class NotesLifecycle {
|
|
|
432
537
|
return { cancel: true };
|
|
433
538
|
}
|
|
434
539
|
allowsCompaction(reason: SessionBeforeCompactEvent["reason"]): boolean {
|
|
540
|
+
if (this.rollover?.phase === "checking" && reason === "manual")
|
|
541
|
+
return false;
|
|
435
542
|
return (
|
|
543
|
+
!this.active ||
|
|
436
544
|
this.normalCompaction ||
|
|
437
545
|
this.rollover?.phase === "compacting" ||
|
|
438
546
|
reason === "overflow"
|
|
@@ -532,7 +640,8 @@ export class NotesLifecycle {
|
|
|
532
640
|
const rollover = this.rollover;
|
|
533
641
|
if (
|
|
534
642
|
rollover?.phase === "scheduled" &&
|
|
535
|
-
rollover.sessionId === ctx.sessionManager.getSessionId()
|
|
643
|
+
rollover.sessionId === ctx.sessionManager.getSessionId() &&
|
|
644
|
+
rollover.windowId === this.windows.current?.currentWindowId
|
|
536
645
|
) {
|
|
537
646
|
this.rollover = undefined;
|
|
538
647
|
await this.roll(ctx, rollover.triggerTurn);
|
package/src/notes.ts
CHANGED
|
@@ -327,7 +327,7 @@ export function useNotes(
|
|
|
327
327
|
} satisfies NoteReceipt);
|
|
328
328
|
} catch {
|
|
329
329
|
throw new Error(
|
|
330
|
-
`Note ${root}${path}
|
|
330
|
+
`Note ${root}${path} was stored but not attached to this session. Read it before retrying the write.`,
|
|
331
331
|
);
|
|
332
332
|
}
|
|
333
333
|
return {
|
|
@@ -335,7 +335,16 @@ export function useNotes(
|
|
|
335
335
|
file: noteMetadata(revision, root),
|
|
336
336
|
};
|
|
337
337
|
}
|
|
338
|
+
return readNotes(notes, params, agent);
|
|
339
|
+
}
|
|
338
340
|
|
|
341
|
+
export function readNotes(
|
|
342
|
+
notes: ReadonlyMap<string, NoteRevision>,
|
|
343
|
+
params: Record<string, unknown>,
|
|
344
|
+
agent: string,
|
|
345
|
+
): Record<string, unknown> {
|
|
346
|
+
const action = params["action"];
|
|
347
|
+
const root = `${agent}/notes/`;
|
|
339
348
|
if (action === "read_file") {
|
|
340
349
|
const path = params["path"] as string;
|
|
341
350
|
const note = notes.get(path);
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { type FileHandle, open } from "node:fs/promises";
|
|
2
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import { Check } from "typebox/value";
|
|
5
|
+
|
|
6
|
+
const SessionHeader = Type.Object({
|
|
7
|
+
type: Type.Literal("session"),
|
|
8
|
+
id: Type.String(),
|
|
9
|
+
});
|
|
10
|
+
const RecordHeader = Type.Object({
|
|
11
|
+
type: Type.String(),
|
|
12
|
+
id: Type.String(),
|
|
13
|
+
parentId: Type.Union([Type.String(), Type.Null()]),
|
|
14
|
+
timestamp: Type.String(),
|
|
15
|
+
});
|
|
16
|
+
const Custom = Type.Object({
|
|
17
|
+
...RecordHeader.properties,
|
|
18
|
+
type: Type.Literal("custom"),
|
|
19
|
+
customType: Type.String(),
|
|
20
|
+
data: Type.Unknown(),
|
|
21
|
+
});
|
|
22
|
+
const Handoff = Type.Object({
|
|
23
|
+
...RecordHeader.properties,
|
|
24
|
+
type: Type.Literal("branch_summary"),
|
|
25
|
+
fromId: Type.String(),
|
|
26
|
+
summary: Type.String(),
|
|
27
|
+
details: Type.Object({ codexContextNoteHandoff: Type.Unknown() }),
|
|
28
|
+
});
|
|
29
|
+
const NOTE_ENTRIES = new Set([
|
|
30
|
+
"notes-compaction:note:v1",
|
|
31
|
+
"codex-context-note",
|
|
32
|
+
"codex-context-note-snapshot",
|
|
33
|
+
]);
|
|
34
|
+
const SHARING_ENTRIES = new Set([
|
|
35
|
+
"codex-context-agent",
|
|
36
|
+
"herdr-context-child",
|
|
37
|
+
"shepherdr-attachment-route",
|
|
38
|
+
"shepherdr-attachment-owner",
|
|
39
|
+
"shepherdr-attachment-detached",
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
export interface SavedNotesSession {
|
|
43
|
+
threadId: string;
|
|
44
|
+
entries: readonly SessionEntry[];
|
|
45
|
+
branch: readonly SessionEntry[];
|
|
46
|
+
savedAt?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function* reverseLines(file: FileHandle, size: number) {
|
|
50
|
+
let position = size;
|
|
51
|
+
let partial = Buffer.alloc(0);
|
|
52
|
+
while (position > 0) {
|
|
53
|
+
const length = Math.min(64 * 1024, position);
|
|
54
|
+
position -= length;
|
|
55
|
+
const chunk = Buffer.allocUnsafe(length);
|
|
56
|
+
const { bytesRead } = await file.read(chunk, 0, length, position);
|
|
57
|
+
const data = Buffer.concat([chunk.subarray(0, bytesRead), partial]);
|
|
58
|
+
let end = data.length;
|
|
59
|
+
for (let index = data.length - 1; index >= 0; index--) {
|
|
60
|
+
if (data[index] !== 10) continue;
|
|
61
|
+
yield data.subarray(index + 1, end);
|
|
62
|
+
end = index;
|
|
63
|
+
}
|
|
64
|
+
partial = data.subarray(0, end);
|
|
65
|
+
}
|
|
66
|
+
yield partial;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Read only recorded session paths. Identity is session-wide; notes follow the active parent chain. */
|
|
70
|
+
export async function readSavedNotesSession(
|
|
71
|
+
path: string,
|
|
72
|
+
signal?: AbortSignal,
|
|
73
|
+
): Promise<SavedNotesSession> {
|
|
74
|
+
signal?.throwIfAborted();
|
|
75
|
+
const file = await open(path, "r");
|
|
76
|
+
try {
|
|
77
|
+
const before = await file.stat();
|
|
78
|
+
const entries: SessionEntry[] = [];
|
|
79
|
+
const branch: SessionEntry[] = [];
|
|
80
|
+
let threadId: string | undefined;
|
|
81
|
+
let target: string | null | undefined;
|
|
82
|
+
for await (const line of reverseLines(file, before.size)) {
|
|
83
|
+
signal?.throwIfAborted();
|
|
84
|
+
if (!line.length) continue;
|
|
85
|
+
let entry: unknown;
|
|
86
|
+
try {
|
|
87
|
+
entry = JSON.parse(line.toString("utf8"));
|
|
88
|
+
} catch {
|
|
89
|
+
throw new Error(
|
|
90
|
+
"Saved notes are being updated. Retry after the session settles.",
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
if (Check(SessionHeader, entry)) {
|
|
94
|
+
threadId = entry.id;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (!Check(RecordHeader, entry))
|
|
98
|
+
throw new Error("Invalid saved session record");
|
|
99
|
+
if (target === undefined) target = entry.id;
|
|
100
|
+
const active = entry.id === target;
|
|
101
|
+
if (active) target = entry.parentId;
|
|
102
|
+
if (Check(Custom, entry)) {
|
|
103
|
+
if (SHARING_ENTRIES.has(entry.customType)) entries.push(entry);
|
|
104
|
+
else if (active && NOTE_ENTRIES.has(entry.customType))
|
|
105
|
+
branch.push(entry);
|
|
106
|
+
else continue;
|
|
107
|
+
} else if (active && Check(Handoff, entry)) branch.push(entry);
|
|
108
|
+
else continue;
|
|
109
|
+
}
|
|
110
|
+
const after = await file.stat();
|
|
111
|
+
if (before.size !== after.size || before.mtimeMs !== after.mtimeMs)
|
|
112
|
+
throw new Error("Saved notes changed during the read. Retry the lookup.");
|
|
113
|
+
if (!threadId || (target !== undefined && target !== null))
|
|
114
|
+
throw new Error("Saved notes have an incomplete session branch");
|
|
115
|
+
return {
|
|
116
|
+
threadId,
|
|
117
|
+
entries: entries.reverse(),
|
|
118
|
+
branch: branch.reverse(),
|
|
119
|
+
savedAt: before.mtimeMs,
|
|
120
|
+
};
|
|
121
|
+
} finally {
|
|
122
|
+
await file.close();
|
|
123
|
+
}
|
|
124
|
+
}
|
package/src/settings-screen.ts
CHANGED
|
@@ -2,20 +2,29 @@ import {
|
|
|
2
2
|
type ExtensionContext,
|
|
3
3
|
getSettingsListTheme,
|
|
4
4
|
} from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
SettingsList,
|
|
7
|
+
truncateToWidth,
|
|
8
|
+
wrapTextWithAnsi,
|
|
9
|
+
} from "@earendil-works/pi-tui";
|
|
10
|
+
import {
|
|
11
|
+
IDLE_MINUTES,
|
|
12
|
+
NEW_WINDOW_REQUESTS,
|
|
13
|
+
type NotesSettings,
|
|
14
|
+
} from "./settings.js";
|
|
15
|
+
import { TASK_BOUNDARY_PRESETS, taskBoundaryLabel } from "./task-boundary.js";
|
|
7
16
|
|
|
8
17
|
export async function openNotesSettings(
|
|
9
18
|
ctx: ExtensionContext,
|
|
10
19
|
options: {
|
|
11
20
|
settings: () => NotesSettings;
|
|
21
|
+
settingsPath: string;
|
|
12
22
|
save: (settings: NotesSettings) => void;
|
|
13
23
|
status: () => string[];
|
|
14
24
|
prune: () => Promise<void>;
|
|
15
25
|
},
|
|
16
26
|
): Promise<void> {
|
|
17
27
|
while (true) {
|
|
18
|
-
const status = options.status();
|
|
19
28
|
const action = await ctx.ui.custom<"close" | "prune">(
|
|
20
29
|
(tui, theme, _kb, done) => {
|
|
21
30
|
const settings = options.settings();
|
|
@@ -23,6 +32,14 @@ export async function openNotesSettings(
|
|
|
23
32
|
minutes === 0 ? "off" : `${minutes} min`;
|
|
24
33
|
const list = new SettingsList(
|
|
25
34
|
[
|
|
35
|
+
{
|
|
36
|
+
id: "active",
|
|
37
|
+
label: "Active",
|
|
38
|
+
description: `Whitelist matches exact model IDs from any provider. Edit modelWhitelist in ${options.settingsPath}, then reload Pi. Example: ["gpt-6-luna", "gpt-6.1-sol"].`,
|
|
39
|
+
currentValue:
|
|
40
|
+
settings.active === "all" ? "All models" : "Whitelist",
|
|
41
|
+
values: ["All models", "Whitelist"],
|
|
42
|
+
},
|
|
26
43
|
{
|
|
27
44
|
id: "compact",
|
|
28
45
|
label: "Normal compaction",
|
|
@@ -31,6 +48,25 @@ export async function openNotesSettings(
|
|
|
31
48
|
currentValue: settings.normalCompaction ? "on" : "off",
|
|
32
49
|
values: ["off", "on"],
|
|
33
50
|
},
|
|
51
|
+
{
|
|
52
|
+
id: "newWindowRequests",
|
|
53
|
+
label: "New window requests",
|
|
54
|
+
description:
|
|
55
|
+
"Off disables new_context. Pi-limits keeps the current window when Pi finds too little history to compact. Unrestricted accepts requests at any size. Normal compaction still chooses how windows roll over. Manual /compact and idle rollover are separate.",
|
|
56
|
+
currentValue: settings.newWindowRequests,
|
|
57
|
+
values: [...NEW_WINDOW_REQUESTS],
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: "taskBoundaryReminder",
|
|
61
|
+
label: "Task-boundary reminder",
|
|
62
|
+
description:
|
|
63
|
+
"Suggest a separate-task rollover after this many used tokens, only with active capacity above 272k. Custom: edit taskBoundaryReminder in the notes settings JSON (whole tokens); 0 is off.",
|
|
64
|
+
currentValue: taskBoundaryLabel(settings.taskBoundaryReminder),
|
|
65
|
+
values: [
|
|
66
|
+
...TASK_BOUNDARY_PRESETS.map(taskBoundaryLabel),
|
|
67
|
+
"custom (edit JSON)",
|
|
68
|
+
],
|
|
69
|
+
},
|
|
34
70
|
{
|
|
35
71
|
id: "idle",
|
|
36
72
|
label: "Idle rollover",
|
|
@@ -56,9 +92,42 @@ export async function openNotesSettings(
|
|
|
56
92
|
return;
|
|
57
93
|
}
|
|
58
94
|
const current = options.settings();
|
|
95
|
+
if (
|
|
96
|
+
id === "taskBoundaryReminder" &&
|
|
97
|
+
value === "custom (edit JSON)"
|
|
98
|
+
) {
|
|
99
|
+
ctx.ui.notify(
|
|
100
|
+
`Set taskBoundaryReminder to a positive whole token count in ${options.settingsPath}, then reload Pi. 0 is off.`,
|
|
101
|
+
"info",
|
|
102
|
+
);
|
|
103
|
+
list.updateValue(
|
|
104
|
+
id,
|
|
105
|
+
taskBoundaryLabel(current.taskBoundaryReminder),
|
|
106
|
+
);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
59
109
|
try {
|
|
60
|
-
if (id === "
|
|
110
|
+
if (id === "active")
|
|
111
|
+
options.save({
|
|
112
|
+
...current,
|
|
113
|
+
active: value === "All models" ? "all" : "whitelist",
|
|
114
|
+
});
|
|
115
|
+
else if (id === "compact")
|
|
61
116
|
options.save({ ...current, normalCompaction: value === "on" });
|
|
117
|
+
else if (id === "newWindowRequests") {
|
|
118
|
+
const newWindowRequests = NEW_WINDOW_REQUESTS.find(
|
|
119
|
+
(mode) => mode === value,
|
|
120
|
+
);
|
|
121
|
+
if (newWindowRequests)
|
|
122
|
+
options.save({ ...current, newWindowRequests });
|
|
123
|
+
} else if (id === "taskBoundaryReminder")
|
|
124
|
+
options.save({
|
|
125
|
+
...current,
|
|
126
|
+
taskBoundaryReminder:
|
|
127
|
+
TASK_BOUNDARY_PRESETS.find(
|
|
128
|
+
(tokens) => taskBoundaryLabel(tokens) === value,
|
|
129
|
+
) ?? current.taskBoundaryReminder,
|
|
130
|
+
});
|
|
62
131
|
else {
|
|
63
132
|
const idleMinutes = IDLE_MINUTES.find(
|
|
64
133
|
(minutes) => idleValue(minutes) === value,
|
|
@@ -69,11 +138,19 @@ export async function openNotesSettings(
|
|
|
69
138
|
} catch (error) {
|
|
70
139
|
list.updateValue(
|
|
71
140
|
id,
|
|
72
|
-
id === "
|
|
73
|
-
? current.
|
|
74
|
-
? "
|
|
75
|
-
: "
|
|
76
|
-
:
|
|
141
|
+
id === "active"
|
|
142
|
+
? current.active === "all"
|
|
143
|
+
? "All models"
|
|
144
|
+
: "Whitelist"
|
|
145
|
+
: id === "taskBoundaryReminder"
|
|
146
|
+
? taskBoundaryLabel(current.taskBoundaryReminder)
|
|
147
|
+
: id === "newWindowRequests"
|
|
148
|
+
? current.newWindowRequests
|
|
149
|
+
: id === "compact"
|
|
150
|
+
? current.normalCompaction
|
|
151
|
+
? "on"
|
|
152
|
+
: "off"
|
|
153
|
+
: idleValue(current.idleMinutes),
|
|
77
154
|
);
|
|
78
155
|
ctx.ui.notify(
|
|
79
156
|
`Could not save notes settings: ${String(error)}`,
|
|
@@ -86,20 +163,42 @@ export async function openNotesSettings(
|
|
|
86
163
|
{ enableSearch: false },
|
|
87
164
|
);
|
|
88
165
|
return {
|
|
89
|
-
render: (width) =>
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
166
|
+
render: (width) => {
|
|
167
|
+
const wrap = (text: string) =>
|
|
168
|
+
wrapTextWithAnsi(text, Math.max(1, width - 2)).map(
|
|
169
|
+
(line) => ` ${line}`,
|
|
170
|
+
);
|
|
171
|
+
const rule = theme.fg("accent", "─".repeat(Math.max(0, width)));
|
|
172
|
+
return [
|
|
173
|
+
rule,
|
|
174
|
+
` ${theme.bold("Notes & context")}`,
|
|
93
175
|
"",
|
|
94
|
-
...
|
|
176
|
+
...options
|
|
177
|
+
.status()
|
|
178
|
+
.flatMap((line) =>
|
|
179
|
+
wrap(line).map((part) => theme.fg("dim", part)),
|
|
180
|
+
),
|
|
95
181
|
"",
|
|
96
|
-
|
|
97
|
-
"
|
|
98
|
-
"↑↓ navigate · Enter/Space change · Esc close · saved immediately",
|
|
182
|
+
...wrap(
|
|
183
|
+
"This extension works best on GPT6+ models. For other models we recommend switching the active option to whitelist or switching normal compaction on to work alongside the notes system.",
|
|
99
184
|
),
|
|
100
|
-
|
|
185
|
+
"",
|
|
186
|
+
...list.render(width).slice(0, -2),
|
|
187
|
+
"",
|
|
188
|
+
...wrap("↑/↓ select · Enter/Space change · Esc close").map(
|
|
189
|
+
(line) => theme.fg("dim", line),
|
|
190
|
+
),
|
|
191
|
+
...wrap("Settings saved immediately").map((line) =>
|
|
192
|
+
theme.fg("dim", line),
|
|
193
|
+
),
|
|
194
|
+
rule,
|
|
195
|
+
].map((line) => truncateToWidth(line, width, ""));
|
|
196
|
+
},
|
|
101
197
|
invalidate: () => list.invalidate(),
|
|
102
|
-
handleInput: (data) =>
|
|
198
|
+
handleInput: (data) => {
|
|
199
|
+
list.handleInput(data);
|
|
200
|
+
tui.requestRender();
|
|
201
|
+
},
|
|
103
202
|
};
|
|
104
203
|
},
|
|
105
204
|
);
|