@rolemodel/pi-clef-compact0.1.0
Verbatim decision-based context compaction for pi, driven by a Clef decision model
Untested on omp
This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.
Reports 0
No reports yet.
Readme
@rolemodel/pi-clef-compact — Verbatim decision-based compaction
Replaces pi's summary-based compaction with verbatim, decision-based compaction. Instead of rewriting old history into a summary, it asks a Clef decision model — for each completed tool call — keep the call? keep the result verbatim?
Anything kept stays verbatim. Only stale tool activity is dropped or truncated. No summarizer, no paraphrase, no lossy rewrite.
Install
pi install npm:@rolemodel/pi-clef-compact
The extension loads immediately and is inactive until an API key is configured (see below). With no key it notifies on session start and no-ops, so it is safe to install with nothing else in place.
Requirements
A running Clef decision-model endpoint speaking the System One format:
POST {baseUrl}/v1/systemone
Authorization: Bearer {apiKey}
{ "model": "...", "state": { "context", "goal", "instructions?", "history" }, "questions": {...} }
→ { "answers": { "<name>": { "type": "noul", "noul": 0.0-1.0 } } }
Each question gets a noul score in [0, 1]. The extension asks two questions per tool call (keep call / keep result) and applies keepThreshold to the scores.
The command
/clef-compact [focus instructions]
Runs a decision-based compaction immediately, optionally with focus instructions for the model (e.g. /clef-compact focus on the auth fixes). Auto-compaction (threshold or overflow) also goes through the decision model, and falls back to pi's default summary compaction when the reduction is below minReduction or the endpoint fails.
Configure
Everything is configurable. Settings live under the clefCompaction key in ~/.pi/agent/settings.json (or the project settings.json in .pi/agent/); the PI_CODING_AGENT_DIR environment variable overrides the agent directory.
{
"clefCompaction": {
"baseUrl": "http://localhost:8000",
"model": "clef-flash",
"apiKey": "your-key"
}
}
Any subset is fine — unspecified fields use the defaults. Invalid or out-of-range values are ignored (the default is used), so a bad setting can never break compaction.
Connection
| Key | Default | Description |
|---|---|---|
enabled |
true |
Master switch. false loads the extension but never compacts. |
baseUrl |
http://localhost:8000 |
Endpoint base URL. /v1/systemone is appended. |
model |
clef-flash |
Model id sent to the endpoint. Must match a model it serves. |
apiKey |
"" |
Bearer token. Empty means the extension stays inactive. |
The four connection fields can also be set via environment variables, which take precedence over settings.json — handy for keeping the key out of the file:
| Variable | Overrides |
|---|---|
CLEF_COMPACT_ENABLED |
enabled (true/false, 1/0) |
CLEF_COMPACT_BASE_URL |
baseUrl |
CLEF_COMPACT_MODEL |
model |
CLEF_COMPACT_API_KEY |
apiKey |
Decisions
| Key | Default | Description |
|---|---|---|
keepThreshold |
0.5 |
noul score at/above which a call or result is kept. Higher keeps more. |
preserveRecentCalls |
6 |
The most recent tool calls are always kept (never scored). |
minReduction |
0.2 |
For auto-compaction: if the verbatim compaction shrinks history by less than this ratio, fall back to pi's default summary compaction. Manual /clef-compact always applies the decisions. |
goalMessages |
3 |
How many recent user messages make up the goal sent to the model. |
maxGoalChars |
1000 |
Hard cap on the goal text length. |
Token budget & batching
History is split into batches that each fit under maxRequestTokens, so any context size works — cost grows linearly in requests, not in one oversized request.
| Key | Default | Description |
|---|---|---|
maxRequestTokens |
10000 |
Hard per-request token budget. |
stateShare |
0.7 |
Share of the budget reserved for state (the rest is for questions). |
contextShare |
0.4 |
Share of the state budget reserved for the shared conversation context. |
maxResultChars |
1200 |
Max chars of a tool result shown to the model inside state. |
maxInputChars |
300 |
Max chars of a tool input shown to the model inside state. |
maxContextCallChars |
200 |
Max chars of a tool-call line in the shared context. |
Output
| Key | Default | Description |
|---|---|---|
truncateHeadChars |
300 |
Chars of a dropped result kept in the output (rest replaced with a truncation marker). |
Tuning the estimator (advanced)
The batcher estimates token costs with these heuristics. Defaults reproduce the reference behavior; tune if your model's token size differs.
| Key | Default | Description |
|---|---|---|
charsPerToken |
3.2 |
Approx chars per token used by the estimator. |
questionCost |
90 |
Per-question overhead (tokens) assumed by the batcher. |
callOverheadTokens |
30 |
Per-call envelope overhead (tokens). |
requestOverheadTokens |
200 |
Fixed request-state overhead subtracted from the batch budget. |
minCallBudget |
200 |
Minimum batch budget (tokens); batches never go below this. |
Retries
| Key | Default | Description |
|---|---|---|
retries |
3 |
Total API attempts per batch before giving up. |
retryBackoffMs |
400 |
Base backoff between retries (ms); grows linearly per attempt. |
How it works
- When compaction is due (manual, threshold, or overflow), the extension serializes the messages to be summarized and extracts every completed tool call/result pair.
- The most recent
preserveRecentCallscalls are pinned (kept unconditionally). - The rest are batched under the token budget. Each batch carries the condensed conversation context, the goal, any focus instructions, and only that batch's tool calls.
- The endpoint scores each call and result with a
noulin[0, 1]:keepResult ≥ keepThreshold→ keep (verbatim)- otherwise
keepCall ≥ keepThreshold→ drop_result (keep the call, truncate the result totruncateHeadChars) - otherwise → drop_call (remove the call and its result)
- The conversation is rebuilt verbatim from the decisions and returned as the compaction summary. If the endpoint fails — or the reduction is below
minReductionon auto-compaction — pi's default summary compaction runs instead.
A missing answer for a call is treated as keep (safe side).
Notes
- The endpoint is only ever contacted during compaction.
- Compaction never touches the pinned recent calls or the messages that are kept anyway — only tool activity in the summarized region is scored.
- The extension is inert without an API key and can be disabled with
enabled: falseorCLEF_COMPACT_ENABLED=0.