pi-context 2.1.1 → 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 +29 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-context",
3
- "version": "2.1.1",
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();
@@ -463,8 +490,8 @@ export default function (pi: ExtensionAPI) {
463
490
  try {
464
491
  await commandCtx.waitForIdle();
465
492
 
466
- const currentLeaf = sm.getLeafId();
467
- if (currentLeaf !== compactTurnLeaf) {
493
+ const branch = sm.getBranch();
494
+ if (didConversationAdvance(branch, compactTurnLeaf)) {
468
495
  commandCtx.ui.notify("context_compact cancelled: conversation advanced before compaction completed.", "warning");
469
496
  pi.sendMessage({
470
497
  customType: PiContextCustomMessageType,