@pinet/model-aware-compaction 0.2.8 → 0.2.10

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/README.md CHANGED
@@ -37,7 +37,7 @@ Rules are evaluated in order. `*` wildcards are supported, such as `example-prox
37
37
 
38
38
  ## Behavior
39
39
 
40
- After each `agent_end`, the extension reads `ctx.getContextUsage()` and the active `ctx.model`. Waiting for `agent_end` is important: tool-using runs can emit several `turn_end` events, and Pi's `ctx.compact()` aborts an active agent operation before compacting. Triggering only after the complete model/tool loop has settled prevents compaction from discarding pending tool work. When usage first exceeds the matching rule's `activeContextTokens`, the extension calls `ctx.compact()`. It prevents duplicate calls while compaction is in flight and re-arms after usage drops below the threshold, the model changes, a session starts, or compaction fails.
40
+ After each `agent_settled`, the extension reads `ctx.getContextUsage()` and the active `ctx.model`. Pi can still auto-compact, retry, or process queued follow-ups after `agent_end`; waiting for `agent_settled` prevents proactive compaction from racing that automatic lifecycle. When usage first exceeds the matching rule's `activeContextTokens`, the extension calls `ctx.compact()`. It skips branches whose latest entry is already a compaction, prevents duplicate calls while compaction is in flight, and treats an `Already compacted` callback as an idempotent outcome. The threshold re-arms after usage drops below the limit, the model changes, a session starts, or another compaction failure occurs.
41
41
 
42
42
  Run `/model-aware-compaction-status` to inspect the active model, usage, matched threshold, state, and loaded rules.
43
43
 
package/dist/index.js CHANGED
@@ -10,9 +10,9 @@ export default function modelAwareCompaction(pi) {
10
10
  };
11
11
  pi.on("session_start", rearm);
12
12
  pi.on("model_select", rearm);
13
- // ctx.compact() aborts an active agent operation. Wait for the complete
14
- // model/tool loop to settle so compaction cannot discard pending tool work.
15
- pi.on("agent_end", (_event, rawCtx) => {
13
+ // agent_end may still be followed by Pi's automatic compaction and retry.
14
+ // Wait until the full operation settles so proactive compaction cannot race it.
15
+ pi.on("agent_settled", (_event, rawCtx) => {
16
16
  const ctx = rawCtx;
17
17
  const config = loadConfig(ctx.cwd);
18
18
  const usage = ctx.getContextUsage?.();
@@ -29,6 +29,10 @@ export default function modelAwareCompaction(pi) {
29
29
  }
30
30
  if (!decision.shouldCompact || !ctx.compact || !decision.modelKey || decision.limit === null)
31
31
  return;
32
+ if (ctx.sessionManager.getBranch().at(-1)?.type === "compaction") {
33
+ triggeredModelKey = decision.modelKey;
34
+ return;
35
+ }
32
36
  inFlight = true;
33
37
  triggeredModelKey = decision.modelKey;
34
38
  const details = `model=${decision.modelKey} tokens=${usage?.tokens ?? "unknown"} limit=${decision.limit}`;
@@ -49,6 +53,8 @@ export default function modelAwareCompaction(pi) {
49
53
  },
50
54
  onError: (error) => {
51
55
  inFlight = false;
56
+ if (error.message === "Already compacted")
57
+ return;
52
58
  triggeredModelKey = null;
53
59
  console.error(`${LOG_PREFIX} failed ${details}: ${error.message}`);
54
60
  if (config.debug && ctx.hasUI)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinet/model-aware-compaction",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "type": "module",
5
5
  "description": "Pi extension for proactive model-aware context compaction thresholds",
6
6
  "author": "Will Porcellini <5994936+gugu91@users.noreply.github.com>",