pi-context 2.1.0 → 2.1.2

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/index.ts +59 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-context",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Agentic Context Management for Pi",
5
5
  "files": [
6
6
  "src",
@@ -8,7 +8,8 @@
8
8
  "README.md"
9
9
  ],
10
10
  "scripts": {
11
- "typecheck": "tsc --noEmit"
11
+ "typecheck": "tsc --noEmit",
12
+ "test": "npm run typecheck && rm -rf dist && tsc --declaration false && node --test test/*.test.mjs"
12
13
  },
13
14
  "keywords": [
14
15
  "pi-agent",
package/src/index.ts CHANGED
@@ -38,6 +38,33 @@ const formatContextUsage = (usage: ContextUsage | undefined, includeTokens = fal
38
38
  return `${percent} (${formatTokens(usage.tokens)}/${formatTokens(usage.contextWindow)})`;
39
39
  };
40
40
 
41
+ const PassiveCompactionEntryTypes = new Set<SessionEntry["type"]>([
42
+ "custom",
43
+ "label",
44
+ "session_info",
45
+ "model_change",
46
+ "thinking_level_change",
47
+ ]);
48
+
49
+ /**
50
+ * Detect whether session activity after the compact turn should cancel the
51
+ * requested compaction. Non-contextual session state is ignored; entries that
52
+ * participate in model context, plus unknown future behavior, fail closed.
53
+ */
54
+ export const didConversationAdvance = (
55
+ branch: readonly SessionEntry[],
56
+ compactTurnLeaf: string | null,
57
+ ): boolean => {
58
+ if (!compactTurnLeaf) return true;
59
+
60
+ const compactTurnIndex = branch.findIndex((entry) => entry.id === compactTurnLeaf);
61
+ if (compactTurnIndex === -1) return true;
62
+
63
+ return branch
64
+ .slice(compactTurnIndex + 1)
65
+ .some((entry) => !PassiveCompactionEntryTypes.has(entry.type));
66
+ };
67
+
41
68
  const resolveTargetId = (sm: SessionManager, target: string): string => {
42
69
  if (target.toLowerCase() === "root") {
43
70
  const tree = sm.getTree();
@@ -452,6 +479,7 @@ export default function (pi: ExtensionAPI) {
452
479
  const compactParams = CompactParams;
453
480
  const commandCtx = CommandCtx;
454
481
  CompactParams = null;
482
+ const compactTurnLeaf = sm.getLeafId();
455
483
 
456
484
  // `agent_end` is emitted before the core Agent is actually idle. If we
457
485
  // call pi.sendMessage({ triggerTurn: true }) inside this handler, pi still
@@ -461,6 +489,24 @@ export default function (pi: ExtensionAPI) {
461
489
  setTimeout(async () => {
462
490
  try {
463
491
  await commandCtx.waitForIdle();
492
+
493
+ const branch = sm.getBranch();
494
+ if (didConversationAdvance(branch, compactTurnLeaf)) {
495
+ commandCtx.ui.notify("context_compact cancelled: conversation advanced before compaction completed.", "warning");
496
+ pi.sendMessage({
497
+ customType: PiContextCustomMessageType,
498
+ content: [
499
+ "context_compact cancelled: conversation advanced before the summary branch was created.",
500
+ "No compaction was applied; continue from the current path. If still useful, inspect timeline and retry with an updated summary.",
501
+ ].join("\n"),
502
+ display: false,
503
+ }, {
504
+ triggerTurn: true,
505
+ deliverAs: "followUp",
506
+ });
507
+ return;
508
+ }
509
+
464
510
  const nid = sm.branchWithSummary(compactParams.tid, compactParams.enrichedMessage);
465
511
  compactParams.nid = nid;
466
512
  // branchWithSummary advances the leaf to the summary entry. Reset
@@ -488,7 +534,19 @@ export default function (pi: ExtensionAPI) {
488
534
  deliverAs: "followUp",
489
535
  });
490
536
  } catch (err) {
491
- commandCtx.ui.notify(`context_compact failed to continue: ${err instanceof Error ? err.message : String(err)}`, "error");
537
+ const message = err instanceof Error ? err.message : String(err);
538
+ commandCtx.ui.notify(`context_compact failed: ${message}`, "error");
539
+ pi.sendMessage({
540
+ customType: PiContextCustomMessageType,
541
+ content: [
542
+ `context_compact failed: ${message}`,
543
+ "No compaction was applied; continue from the current path. Retry only with a fresh timeline/summary.",
544
+ ].join("\n"),
545
+ display: false,
546
+ }, {
547
+ triggerTurn: true,
548
+ deliverAs: "followUp",
549
+ });
492
550
  }
493
551
  }, 0);
494
552
  });