@howaboua/pi-notes-compaction 0.0.5 → 0.0.7
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 +14 -0
- package/PCC-INTEGRATION.md +7 -3
- package/README.md +24 -3
- package/package.json +2 -2
- package/src/bridge.ts +5 -0
- package/src/fresh-notes.ts +95 -24
- package/src/index.ts +151 -30
- package/src/lifecycle.ts +146 -38
- package/src/notes.ts +10 -1
- package/src/saved-session.ts +131 -0
- package/src/settings-screen.ts +55 -13
- package/src/settings.ts +55 -0
- package/src/shared-notes.ts +421 -0
- package/src/task-boundary.ts +1 -1
- package/src/tools.ts +69 -26
- package/src/windows.ts +92 -9
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,16 +44,20 @@ 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;
|
|
53
62
|
taskBoundaryReminder = 0;
|
|
54
63
|
bridge: NotesBridge | undefined;
|
|
@@ -56,6 +65,7 @@ export class NotesLifecycle {
|
|
|
56
65
|
private checkpoint: Checkpoint | undefined;
|
|
57
66
|
private held: HeldInput | undefined;
|
|
58
67
|
private rollover: Rollover | undefined;
|
|
68
|
+
private customAdmission: Promise<void> | undefined;
|
|
59
69
|
private cancelledCompact:
|
|
60
70
|
| {
|
|
61
71
|
sessionId: string;
|
|
@@ -71,9 +81,16 @@ export class NotesLifecycle {
|
|
|
71
81
|
this.store = store;
|
|
72
82
|
}
|
|
73
83
|
agent(ctx: ExtensionContext): string {
|
|
74
|
-
return
|
|
84
|
+
return (
|
|
85
|
+
this.bridge?.agentName?.(ctx) ??
|
|
86
|
+
savedNotesIdentity({
|
|
87
|
+
threadId: ctx.sessionManager.getSessionId(),
|
|
88
|
+
entries: ctx.sessionManager.getEntries(),
|
|
89
|
+
}).agentName
|
|
90
|
+
);
|
|
75
91
|
}
|
|
76
92
|
reset(ctx?: ExtensionContext): void {
|
|
93
|
+
this.customAdmission = undefined;
|
|
77
94
|
this.checkpoint = undefined;
|
|
78
95
|
this.rollover = undefined;
|
|
79
96
|
this.cancelledCompact = undefined;
|
|
@@ -123,10 +140,34 @@ export class NotesLifecycle {
|
|
|
123
140
|
);
|
|
124
141
|
}
|
|
125
142
|
}
|
|
126
|
-
beforeStart(ctx: ExtensionContext): void {
|
|
143
|
+
beforeStart(ctx: ExtensionContext, event: BeforeAgentStartEvent): void {
|
|
127
144
|
this.initialize(ctx);
|
|
128
145
|
this.runId = randomUUID();
|
|
129
|
-
|
|
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;
|
|
130
171
|
}
|
|
131
172
|
outcome(outcome: "completed" | "aborted" | "error"): void {
|
|
132
173
|
if (this.checkpoint) this.checkpoint.outcome = outcome;
|
|
@@ -138,11 +179,27 @@ export class NotesLifecycle {
|
|
|
138
179
|
runId,
|
|
139
180
|
);
|
|
140
181
|
}
|
|
141
|
-
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
|
+
);
|
|
142
187
|
if (this.checkpoint || this.held) return false;
|
|
143
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
|
+
);
|
|
144
200
|
this.rollover = {
|
|
145
201
|
sessionId: ctx.sessionManager.getSessionId(),
|
|
202
|
+
windowId: this.windows.current?.currentWindowId,
|
|
146
203
|
phase: "scheduled",
|
|
147
204
|
triggerTurn: true,
|
|
148
205
|
};
|
|
@@ -165,36 +222,91 @@ export class NotesLifecycle {
|
|
|
165
222
|
triggerTurn: false,
|
|
166
223
|
});
|
|
167
224
|
this.windows.current = ids;
|
|
168
|
-
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
|
+
);
|
|
169
234
|
}
|
|
170
235
|
private async roll(
|
|
171
236
|
ctx: ExtensionContext,
|
|
172
237
|
triggerTurn: boolean,
|
|
173
238
|
): Promise<void> {
|
|
174
|
-
|
|
239
|
+
const policy = triggerTurn ? this.newWindowRequests : undefined;
|
|
240
|
+
const checkOnly = !this.normalCompaction && policy === "pi-limits";
|
|
241
|
+
if (!this.normalCompaction && !checkOnly) {
|
|
175
242
|
await this.openWindow(ctx, triggerTurn, true);
|
|
176
243
|
return;
|
|
177
244
|
}
|
|
178
245
|
const pending: Rollover = {
|
|
179
246
|
sessionId: ctx.sessionManager.getSessionId(),
|
|
180
|
-
|
|
247
|
+
windowId: this.windows.current?.currentWindowId,
|
|
248
|
+
phase: checkOnly ? "checking" : "compacting",
|
|
181
249
|
triggerTurn,
|
|
182
250
|
};
|
|
183
251
|
this.rollover = pending;
|
|
184
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
|
+
};
|
|
185
262
|
ctx.compact({
|
|
186
263
|
onComplete: () => {
|
|
187
|
-
if (
|
|
188
|
-
this.rollover !== pending ||
|
|
189
|
-
pending.sessionId !== ctx.sessionManager.getSessionId()
|
|
190
|
-
) {
|
|
264
|
+
if (!current()) {
|
|
191
265
|
reject(new Error("Session changed during compaction"));
|
|
192
266
|
return;
|
|
193
267
|
}
|
|
194
|
-
|
|
195
|
-
void this.openWindow(ctx, triggerTurn, false).then(resolve, reject);
|
|
268
|
+
finish(false);
|
|
196
269
|
},
|
|
197
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
|
+
}
|
|
198
310
|
if (this.rollover === pending) this.rollover = undefined;
|
|
199
311
|
reject(error);
|
|
200
312
|
},
|
|
@@ -204,12 +316,11 @@ export class NotesLifecycle {
|
|
|
204
316
|
private startCheckpoint(ctx: ExtensionContext, instructions?: string): void {
|
|
205
317
|
if (!ctx.isIdle() || !this.windows.current || this.checkpoint)
|
|
206
318
|
throw new Error("A checkpoint cannot start until the session settles");
|
|
207
|
-
const
|
|
319
|
+
const requestId = randomUUID();
|
|
208
320
|
this.checkpoint = {
|
|
209
321
|
sessionId: ctx.sessionManager.getSessionId(),
|
|
210
322
|
windowId: this.windows.current.currentWindowId,
|
|
211
|
-
|
|
212
|
-
admitted: false,
|
|
323
|
+
requestId,
|
|
213
324
|
runId: undefined,
|
|
214
325
|
outcome: undefined,
|
|
215
326
|
};
|
|
@@ -217,13 +328,12 @@ export class NotesLifecycle {
|
|
|
217
328
|
this.pi.sendMessage(
|
|
218
329
|
{
|
|
219
330
|
customType: "notes-compaction:checkpoint:v1",
|
|
220
|
-
|
|
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}` : ""}`,
|
|
221
333
|
display: true,
|
|
222
334
|
},
|
|
223
|
-
{ triggerTurn:
|
|
335
|
+
{ triggerTurn: true },
|
|
224
336
|
);
|
|
225
|
-
// A settled user kickoff runs ALL input and before_agent_start handlers.
|
|
226
|
-
this.pi.sendUserMessage(prompt);
|
|
227
337
|
} catch (error) {
|
|
228
338
|
this.checkpoint = undefined;
|
|
229
339
|
throw error;
|
|
@@ -287,16 +397,6 @@ export class NotesLifecycle {
|
|
|
287
397
|
admit?.({ action: "continue" });
|
|
288
398
|
return { action: "handled" };
|
|
289
399
|
}
|
|
290
|
-
const checkpoint = this.checkpoint;
|
|
291
|
-
if (
|
|
292
|
-
checkpoint &&
|
|
293
|
-
!checkpoint.admitted &&
|
|
294
|
-
event.source === "extension" &&
|
|
295
|
-
event.text === checkpoint.prompt
|
|
296
|
-
) {
|
|
297
|
-
checkpoint.admitted = true;
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
400
|
if (!this.held && (!this.idleDue(ctx) || !this.fresh(ctx))) return;
|
|
301
401
|
const held =
|
|
302
402
|
this.held ??
|
|
@@ -358,6 +458,10 @@ export class NotesLifecycle {
|
|
|
358
458
|
event: SessionBeforeCompactEvent,
|
|
359
459
|
ctx: ExtensionContext,
|
|
360
460
|
): { cancel: true } | undefined {
|
|
461
|
+
if (this.rollover?.phase === "checking" && event.reason === "manual") {
|
|
462
|
+
this.rollover.accepted = event.signal;
|
|
463
|
+
return { cancel: true };
|
|
464
|
+
}
|
|
361
465
|
if (this.allowsCompaction(event.reason)) {
|
|
362
466
|
// PCC owns preparation when bridged, including native checkpoint routes.
|
|
363
467
|
if (this.bridge) return;
|
|
@@ -433,7 +537,10 @@ export class NotesLifecycle {
|
|
|
433
537
|
return { cancel: true };
|
|
434
538
|
}
|
|
435
539
|
allowsCompaction(reason: SessionBeforeCompactEvent["reason"]): boolean {
|
|
540
|
+
if (this.rollover?.phase === "checking" && reason === "manual")
|
|
541
|
+
return false;
|
|
436
542
|
return (
|
|
543
|
+
!this.active ||
|
|
437
544
|
this.normalCompaction ||
|
|
438
545
|
this.rollover?.phase === "compacting" ||
|
|
439
546
|
reason === "overflow"
|
|
@@ -533,7 +640,8 @@ export class NotesLifecycle {
|
|
|
533
640
|
const rollover = this.rollover;
|
|
534
641
|
if (
|
|
535
642
|
rollover?.phase === "scheduled" &&
|
|
536
|
-
rollover.sessionId === ctx.sessionManager.getSessionId()
|
|
643
|
+
rollover.sessionId === ctx.sessionManager.getSessionId() &&
|
|
644
|
+
rollover.windowId === this.windows.current?.currentWindowId
|
|
537
645
|
) {
|
|
538
646
|
this.rollover = undefined;
|
|
539
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,131 @@
|
|
|
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
|
+
history = false,
|
|
74
|
+
): Promise<SavedNotesSession> {
|
|
75
|
+
signal?.throwIfAborted();
|
|
76
|
+
const file = await open(path, "r");
|
|
77
|
+
try {
|
|
78
|
+
const before = await file.stat();
|
|
79
|
+
const entries: SessionEntry[] = [];
|
|
80
|
+
const branch: SessionEntry[] = [];
|
|
81
|
+
let threadId: string | undefined;
|
|
82
|
+
let target: string | null | undefined;
|
|
83
|
+
for await (const line of reverseLines(file, before.size)) {
|
|
84
|
+
signal?.throwIfAborted();
|
|
85
|
+
if (!line.length) continue;
|
|
86
|
+
let entry: unknown;
|
|
87
|
+
try {
|
|
88
|
+
entry = JSON.parse(line.toString("utf8"));
|
|
89
|
+
} catch {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"Saved notes are being updated. Retry after the session settles.",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (Check(SessionHeader, entry)) {
|
|
95
|
+
threadId = entry.id;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (!Check(RecordHeader, entry))
|
|
99
|
+
throw new Error("Invalid saved session record");
|
|
100
|
+
if (target === undefined) target = entry.id;
|
|
101
|
+
const active = entry.id === target;
|
|
102
|
+
if (active) target = entry.parentId;
|
|
103
|
+
if (Check(Custom, entry)) {
|
|
104
|
+
if (SHARING_ENTRIES.has(entry.customType)) entries.push(entry);
|
|
105
|
+
else if (active && NOTE_ENTRIES.has(entry.customType))
|
|
106
|
+
branch.push(entry);
|
|
107
|
+
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);
|
|
115
|
+
else continue;
|
|
116
|
+
}
|
|
117
|
+
const after = await file.stat();
|
|
118
|
+
if (before.size !== after.size || before.mtimeMs !== after.mtimeMs)
|
|
119
|
+
throw new Error("Saved notes changed during the read. Retry the lookup.");
|
|
120
|
+
if (!threadId || (target !== undefined && target !== null))
|
|
121
|
+
throw new Error("Saved notes have an incomplete session branch");
|
|
122
|
+
return {
|
|
123
|
+
threadId,
|
|
124
|
+
entries: entries.reverse(),
|
|
125
|
+
branch: branch.reverse(),
|
|
126
|
+
savedAt: before.mtimeMs,
|
|
127
|
+
};
|
|
128
|
+
} finally {
|
|
129
|
+
await file.close();
|
|
130
|
+
}
|
|
131
|
+
}
|
package/src/settings-screen.ts
CHANGED
|
@@ -7,7 +7,11 @@ import {
|
|
|
7
7
|
truncateToWidth,
|
|
8
8
|
wrapTextWithAnsi,
|
|
9
9
|
} from "@earendil-works/pi-tui";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
IDLE_MINUTES,
|
|
12
|
+
NEW_WINDOW_REQUESTS,
|
|
13
|
+
type NotesSettings,
|
|
14
|
+
} from "./settings.js";
|
|
11
15
|
import { TASK_BOUNDARY_PRESETS, taskBoundaryLabel } from "./task-boundary.js";
|
|
12
16
|
|
|
13
17
|
export async function openNotesSettings(
|
|
@@ -21,7 +25,6 @@ export async function openNotesSettings(
|
|
|
21
25
|
},
|
|
22
26
|
): Promise<void> {
|
|
23
27
|
while (true) {
|
|
24
|
-
const status = options.status();
|
|
25
28
|
const action = await ctx.ui.custom<"close" | "prune">(
|
|
26
29
|
(tui, theme, _kb, done) => {
|
|
27
30
|
const settings = options.settings();
|
|
@@ -29,6 +32,14 @@ export async function openNotesSettings(
|
|
|
29
32
|
minutes === 0 ? "off" : `${minutes} min`;
|
|
30
33
|
const list = new SettingsList(
|
|
31
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
|
+
},
|
|
32
43
|
{
|
|
33
44
|
id: "compact",
|
|
34
45
|
label: "Normal compaction",
|
|
@@ -37,6 +48,14 @@ export async function openNotesSettings(
|
|
|
37
48
|
currentValue: settings.normalCompaction ? "on" : "off",
|
|
38
49
|
values: ["off", "on"],
|
|
39
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
|
+
},
|
|
40
59
|
{
|
|
41
60
|
id: "taskBoundaryReminder",
|
|
42
61
|
label: "Task-boundary reminder",
|
|
@@ -88,9 +107,20 @@ export async function openNotesSettings(
|
|
|
88
107
|
return;
|
|
89
108
|
}
|
|
90
109
|
try {
|
|
91
|
-
if (id === "
|
|
110
|
+
if (id === "active")
|
|
111
|
+
options.save({
|
|
112
|
+
...current,
|
|
113
|
+
active: value === "All models" ? "all" : "whitelist",
|
|
114
|
+
});
|
|
115
|
+
else if (id === "compact")
|
|
92
116
|
options.save({ ...current, normalCompaction: value === "on" });
|
|
93
|
-
else if (id === "
|
|
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")
|
|
94
124
|
options.save({
|
|
95
125
|
...current,
|
|
96
126
|
taskBoundaryReminder:
|
|
@@ -108,13 +138,19 @@ export async function openNotesSettings(
|
|
|
108
138
|
} catch (error) {
|
|
109
139
|
list.updateValue(
|
|
110
140
|
id,
|
|
111
|
-
id === "
|
|
112
|
-
?
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
:
|
|
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),
|
|
118
154
|
);
|
|
119
155
|
ctx.ui.notify(
|
|
120
156
|
`Could not save notes settings: ${String(error)}`,
|
|
@@ -137,8 +173,14 @@ export async function openNotesSettings(
|
|
|
137
173
|
rule,
|
|
138
174
|
` ${theme.bold("Notes & context")}`,
|
|
139
175
|
"",
|
|
140
|
-
...
|
|
141
|
-
|
|
176
|
+
...options
|
|
177
|
+
.status()
|
|
178
|
+
.flatMap((line) =>
|
|
179
|
+
wrap(line).map((part) => theme.fg("dim", part)),
|
|
180
|
+
),
|
|
181
|
+
"",
|
|
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.",
|
|
142
184
|
),
|
|
143
185
|
"",
|
|
144
186
|
...list.render(width).slice(0, -2),
|
package/src/settings.ts
CHANGED
|
@@ -3,8 +3,23 @@ import { dirname } from "node:path";
|
|
|
3
3
|
|
|
4
4
|
export const IDLE_MINUTES = [0, 5, 15, 25, 55] as const;
|
|
5
5
|
export type IdleMinutes = (typeof IDLE_MINUTES)[number];
|
|
6
|
+
export const NEW_WINDOW_REQUESTS = [
|
|
7
|
+
"off",
|
|
8
|
+
"pi-limits",
|
|
9
|
+
"unrestricted",
|
|
10
|
+
] as const;
|
|
11
|
+
export type NewWindowRequests = (typeof NEW_WINDOW_REQUESTS)[number];
|
|
12
|
+
export const DEFAULT_MODEL_WHITELIST = [
|
|
13
|
+
"gpt-6-astra",
|
|
14
|
+
"gpt-6-sol",
|
|
15
|
+
"gpt-6-luna",
|
|
16
|
+
"gpt-6.1-sol",
|
|
17
|
+
];
|
|
6
18
|
export interface NotesSettings {
|
|
19
|
+
active: "all" | "whitelist";
|
|
20
|
+
modelWhitelist: string[];
|
|
7
21
|
normalCompaction: boolean;
|
|
22
|
+
newWindowRequests: NewWindowRequests;
|
|
8
23
|
idleMinutes: IdleMinutes;
|
|
9
24
|
taskBoundaryReminder: number;
|
|
10
25
|
}
|
|
@@ -16,7 +31,10 @@ export function readSettings(path: string): NotesSettings {
|
|
|
16
31
|
} catch (error) {
|
|
17
32
|
if ((error as NodeJS.ErrnoException).code === "ENOENT")
|
|
18
33
|
return {
|
|
34
|
+
active: "all",
|
|
35
|
+
modelWhitelist: [...DEFAULT_MODEL_WHITELIST],
|
|
19
36
|
normalCompaction: false,
|
|
37
|
+
newWindowRequests: "unrestricted",
|
|
20
38
|
idleMinutes: 0,
|
|
21
39
|
taskBoundaryReminder: 0,
|
|
22
40
|
};
|
|
@@ -32,7 +50,31 @@ export function readSettings(path: string): NotesSettings {
|
|
|
32
50
|
throw new Error(
|
|
33
51
|
"Invalid notes settings. Save a setting in /notes, then reload Pi.",
|
|
34
52
|
);
|
|
53
|
+
const active = "active" in settings ? settings.active : "all";
|
|
54
|
+
if (active !== "all" && active !== "whitelist")
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Invalid active setting in ${path}. Use "all" or "whitelist", then reload Pi.`,
|
|
57
|
+
);
|
|
58
|
+
const modelWhitelist =
|
|
59
|
+
"modelWhitelist" in settings
|
|
60
|
+
? settings.modelWhitelist
|
|
61
|
+
: [...DEFAULT_MODEL_WHITELIST];
|
|
62
|
+
if (
|
|
63
|
+
!Array.isArray(modelWhitelist) ||
|
|
64
|
+
!modelWhitelist.every((id): id is string => typeof id === "string")
|
|
65
|
+
)
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Invalid modelWhitelist in ${path}. Use an array of model ID strings, then reload Pi.`,
|
|
68
|
+
);
|
|
35
69
|
const idleMinutes = "idleMinutes" in settings ? settings.idleMinutes : 0;
|
|
70
|
+
const newWindowRequests =
|
|
71
|
+
"newWindowRequests" in settings
|
|
72
|
+
? settings.newWindowRequests
|
|
73
|
+
: "unrestricted";
|
|
74
|
+
if (!NEW_WINDOW_REQUESTS.some((value) => value === newWindowRequests))
|
|
75
|
+
throw new Error(
|
|
76
|
+
"Invalid new window requests setting. Choose off, pi-limits or unrestricted in /notes.",
|
|
77
|
+
);
|
|
36
78
|
const taskBoundaryReminder =
|
|
37
79
|
"taskBoundaryReminder" in settings ? settings.taskBoundaryReminder : 0;
|
|
38
80
|
if (
|
|
@@ -48,12 +90,25 @@ export function readSettings(path: string): NotesSettings {
|
|
|
48
90
|
"Invalid idle rollover interval. Choose off, 5, 15, 25 or 55 minutes.",
|
|
49
91
|
);
|
|
50
92
|
return {
|
|
93
|
+
active,
|
|
94
|
+
modelWhitelist,
|
|
51
95
|
normalCompaction: settings.normalCompaction,
|
|
96
|
+
newWindowRequests: newWindowRequests as NewWindowRequests,
|
|
52
97
|
idleMinutes: idleMinutes as IdleMinutes,
|
|
53
98
|
taskBoundaryReminder,
|
|
54
99
|
};
|
|
55
100
|
}
|
|
56
101
|
|
|
102
|
+
export function modelIsActive(
|
|
103
|
+
settings: NotesSettings,
|
|
104
|
+
model?: { id: string },
|
|
105
|
+
): boolean {
|
|
106
|
+
return (
|
|
107
|
+
settings.active === "all" ||
|
|
108
|
+
Boolean(model && settings.modelWhitelist.includes(model.id))
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
57
112
|
export function writeSettings(path: string, settings: NotesSettings): void {
|
|
58
113
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
59
114
|
const temporary = `${path}.${process.pid}.tmp`;
|