@pinet/model-aware-compaction

Pi extension for proactive model-aware context compaction thresholds

Packages

Package details

extension

Install @pinet/model-aware-compaction from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:@pinet/model-aware-compaction
Package
@pinet/model-aware-compaction
Version
0.2.4
Published
Jul 7, 2026
Downloads
488/mo · 194/wk
Author
tmustier
License
MIT
Types
extension
Size
14.1 KB
Dependencies
0 dependencies · 1 peer
Pi manifest JSON
{
  "extensions": [
    "./dist/index.js"
  ]
}

Security note

Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.

README

@pinet/model-aware-compaction

A Pi extension that triggers proactive compaction at different active-context token limits for different models. It adapts Pi's shipped trigger-compact.ts example to multi-model and subagent workloads.

Install

pi install npm:@pinet/model-aware-compaction

For local development from a clone of this repository, load the source directly:

pi --extension /path/to/extensions/model-aware-compaction/index.ts

Configure

The extension is disabled by default. Add this to project .pi/settings.json or global ~/.pi/agent/settings.json:

{
  "model-aware-compaction": {
    "enabled": true,
    "rules": [
      { "model": "openai/gpt-5-mini", "activeContextTokens": 100000 },
      { "model": "anthropic/claude-sonnet-4-6", "activeContextTokens": 100000 },
      { "model": "example-proxy/*", "activeContextTokens": 136000 }
    ],
    "customInstructions": "Preserve decisions, files changed, validation results, and next steps.",
    "debug": false
  }
}

Rules are evaluated in order. * wildcards are supported, such as example-proxy/*.

Behavior

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.

Run /model-aware-compaction-status to inspect the active model, usage, matched threshold, state, and loaded rules.

Limitation

Pi's extension API makes ctx.compact() fire-and-forget. This package is therefore proactive best effort, not an atomic compact-before-next-provider-request barrier. Debug logs make trigger/completion/failure visible so that race behavior can be measured. Upstream model-specific settings or an awaitable/deferred compaction seam would provide a stronger guarantee.

Development

pnpm --filter @pinet/model-aware-compaction lint
pnpm --filter @pinet/model-aware-compaction typecheck
pnpm --filter @pinet/model-aware-compaction test
pnpm --filter @pinet/model-aware-compaction build