@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/src/windows.ts CHANGED
@@ -9,7 +9,7 @@ import type {
9
9
  } from "@earendil-works/pi-coding-agent";
10
10
  import { keyHint } from "@earendil-works/pi-coding-agent";
11
11
  import { Box, Text, TruncatedText } from "@earendil-works/pi-tui";
12
- import type { WindowIdentity } from "./bridge.js";
12
+ import type { NotesBridge, WindowIdentity } from "./bridge.js";
13
13
  import {
14
14
  renderTaskBoundaryReminder,
15
15
  taskBoundaryEligible,
@@ -24,9 +24,9 @@ export interface WindowDetails {
24
24
  trim: boolean;
25
25
  }
26
26
  const GUIDANCE = `<context_window_guidance>
27
- Keep one checkpoint per task at a stable path: request, constraints, decisions, progress, next steps, history IDs. Replace stale state; mark completion in place. Keep reusable findings and deferred ideas in separate topic notes; link, don't copy. Recording isn't permission to implement.
27
+ Notes persist across windows. Keep one checkpoint per task at a stable path, covering the request, constraints, decisions, progress, next steps and history IDs so you can resume without rereading the conversation. Replace stale state; mark completion in place. Keep reusable findings and deferred ideas in separate topic notes; link, don't copy. Recording isn't permission to implement.
28
28
 
29
- Save changed state after substantial work, before replying, and before new_context or handoff. Skip routine or unchanged state. Include checkpoint paths in handoffs. After rollover, read the checkpoint, then linked notes as needed; history only for missing details.
29
+ Save changed state after substantial work, before replying, and before new_context or handoff. Skip routine or unchanged state. Include checkpoint paths in handoffs. After rollover, read the checkpoint, then linked notes as needed. History retrieves earlier conversation; use it only for missing details.
30
30
  </context_window_guidance>`;
31
31
 
32
32
  export function registerWindowRenderer(pi: ExtensionAPI): void {
@@ -177,17 +177,34 @@ export function windowMessage(
177
177
 
178
178
  export class Windows {
179
179
  current: WindowIdentity | undefined;
180
+ private identityEntry:
181
+ | Extract<SessionEntry, { type: "custom_message" }>
182
+ | undefined;
183
+ private restoredIdentity:
184
+ | {
185
+ entryId: string;
186
+ timestamp: number;
187
+ prepare: NotesBridge["prepareReminder"];
188
+ message: AgentMessage;
189
+ }
190
+ | undefined;
180
191
  private compactedSinceBoundary = false;
181
192
  private reminders = new Set<string>();
182
193
  restore(entries: readonly SessionEntry[]): void {
183
194
  this.current = undefined;
195
+ this.identityEntry = undefined;
184
196
  this.compactedSinceBoundary = false;
185
197
  this.reminders.clear();
186
198
  for (const entry of entries) {
187
199
  const details = windowDetails(entry);
200
+ if (
201
+ entry.type === "custom_message" &&
202
+ (details?.kind === "window" || details?.kind === "identity")
203
+ )
204
+ this.identityEntry = entry;
188
205
  if (details?.kind === "window") {
189
206
  this.current = details.identity;
190
- this.compactedSinceBoundary = false;
207
+ if (details.trim) this.compactedSinceBoundary = false;
191
208
  } else if (
192
209
  details &&
193
210
  (details.kind === "reminder" ||
@@ -279,7 +296,7 @@ export class Windows {
279
296
  content:
280
297
  kind === "task-boundary"
281
298
  ? renderTaskBoundaryReminder(remainingPercent)
282
- : `<context_window_reminder>\n${kind === "urgent" ? "Urgent: " : ""}${remainingPercent}% remaining. Checkpoint the active request, state and known history IDs in notes, then call new_context ${kind === "urgent" ? "now, before other work" : "before continuing work"}.\n</context_window_reminder>`,
299
+ : `<context_window_reminder>\n${kind === "urgent" ? "Urgent: " : ""}${remainingPercent}% context remaining. Save the task state in notes, then call new_context ${kind === "urgent" ? "now, before other work" : "before continuing work"}.\n</context_window_reminder>`,
283
300
  display: true,
284
301
  details: {
285
302
  protocol: 1,
@@ -289,8 +306,74 @@ export class Windows {
289
306
  } satisfies WindowDetails,
290
307
  };
291
308
  }
292
- projectMessages(messages: readonly AgentMessage[]): AgentMessage[] {
293
- if (this.compactedSinceBoundary) return [...messages];
309
+ projectMessages(
310
+ messages: readonly AgentMessage[],
311
+ prepareIdentity?: NotesBridge["prepareReminder"],
312
+ ): AgentMessage[] {
313
+ if (this.compactedSinceBoundary) {
314
+ const summary = messages.findIndex(
315
+ (message) => message.role === "compactionSummary",
316
+ );
317
+ const entry = this.identityEntry;
318
+ const details = entry && windowDetails(entry);
319
+ const hasIdentity = messages.some((message) => {
320
+ const current =
321
+ message.role === "custom" &&
322
+ messageWindowDetails(message.customType, message.details);
323
+ return (
324
+ current &&
325
+ (current.kind === "window" || current.kind === "identity") &&
326
+ current.identity.currentWindowId === this.current?.currentWindowId
327
+ );
328
+ });
329
+ if (
330
+ summary < 0 ||
331
+ messages[0]?.role !== "system" ||
332
+ !entry ||
333
+ !details ||
334
+ typeof entry.content !== "string" ||
335
+ hasIdentity
336
+ )
337
+ return [...messages];
338
+ const timestamp = messages[summary]!.timestamp;
339
+ if (
340
+ this.restoredIdentity?.entryId !== entry.id ||
341
+ this.restoredIdentity.timestamp !== timestamp ||
342
+ this.restoredIdentity.prepare !== prepareIdentity
343
+ ) {
344
+ const draft: CustomMessageEntryDraft = {
345
+ type: "custom_message",
346
+ customType: WINDOW_MESSAGE,
347
+ content: `${entry.content}\n\nIf no checkpoint is available, use history to recover missing context.`,
348
+ display: false,
349
+ details: {
350
+ ...details,
351
+ kind: "identity",
352
+ trim: false,
353
+ } satisfies WindowDetails,
354
+ };
355
+ const prepared = prepareIdentity?.(draft) ?? draft;
356
+ this.restoredIdentity = {
357
+ entryId: entry.id,
358
+ timestamp,
359
+ prepare: prepareIdentity,
360
+ message: {
361
+ role: "custom",
362
+ customType: prepared.customType,
363
+ content: prepared.content,
364
+ display: prepared.display,
365
+ details: prepared.details,
366
+ timestamp,
367
+ },
368
+ };
369
+ }
370
+ // Carry the older identity ahead of its summary, outside opaque native replay.
371
+ return [
372
+ ...messages.slice(0, summary),
373
+ this.restoredIdentity.message,
374
+ ...messages.slice(summary),
375
+ ];
376
+ }
294
377
  let boundary = -1;
295
378
  for (let i = 0; i < messages.length; i++) {
296
379
  const message = messages[i]!;
@@ -300,7 +383,7 @@ export class Windows {
300
383
  throw new Error(
301
384
  "Invalid saved context window. Open an intact session branch to continue.",
302
385
  );
303
- if (details?.kind === "window") boundary = details.trim ? i : -1;
386
+ if (details?.kind === "window" && details.trim) boundary = i;
304
387
  }
305
388
  if (boundary < 0) return [...messages];
306
389
  const system = getCurrentSystemMessage(messages.slice(0, boundary));
@@ -312,7 +395,7 @@ export class Windows {
312
395
  let boundary = -1;
313
396
  for (let i = 0; i < entries.length; i++) {
314
397
  const details = windowDetails(entries[i]!);
315
- if (details?.kind === "window") boundary = details.trim ? i : -1;
398
+ if (details?.kind === "window" && details.trim) boundary = i;
316
399
  // Normal Pi/PCC compaction has already chosen its own checkpoint.
317
400
  if (entries[i]!.type === "compaction") boundary = -1;
318
401
  }