@archships/dim-plugin-auto-compact
v0.0.20
Published
Official auto compaction plugin for dim-agent-sdk.
Readme
@archships/dim-plugin-auto-compact
Official supported auto compaction plugin for dim-agent-sdk.
What it does
- exposes
SessionCompactorController.compact(...) - summarizes older history into one canonical compaction segment
- optionally uses a dedicated
summaryModelinstead of the session model - accepts an SDK-owned
CompactionExecutionRequest - normalizes summary output back into one canonical
<continuation_summary>...</continuation_summary>block - includes assistant tool calls and tool result metadata in the summary input; with
compaction.prune: true, older tool outputs are omitted with character counts while recent/error outputs render bounded previews - keeps full
session.messagesintact for UI and restore - stores plugin-owned provenance in
pluginState['auto-compact'], including semanticlastResultdiagnostics for the controller call only
Requirements
- enable SDK core compaction (
compaction: {}or an explicitsafetyRatio) on the agent - publish
ModelCapabilities.contextWindowon the model adapter so the gate can derivethreshold = contextWindow − max(effectiveO, contextWindow × safetyRatio)(whereeffectiveOcapsplannedOutputatmax(DEFAULT_OUTPUT_CAP_TOKENS, contextWindow × safetyRatio)and falls back to the safety floor whenplannedOutput / contextWindow > 0.6) - configure
compaction.compactorPluginId: 'auto-compact' - grant model permission to the plugin runtime
Usage
import { createAgent, createModel } from '@archships/dim-agent-sdk'
import { createAutoCompactPlugin } from '@archships/dim-plugin-auto-compact'
const agent = createAgent({
model: createModel(adapter),
plugins: [
createAutoCompactPlugin({
summaryModel: { provider: 'openai', modelId: 'gpt-4.1-mini' },
retainMessages: 6,
compaction: {
auto: true,
prune: true,
reserved: 10_000,
},
}),
],
compaction: {
safetyRatio: 0.2,
compactorPluginId: 'auto-compact',
},
})Notes
summaryModelis optional; the plugin defaults tocontext.status.modelmaxSummaryTokensis optional; the plugin defaults summary requests to1024- SDK core owns planning, cursor movement, checkpoints, typed notifications, and threshold fallback orchestration
- threshold fallback order is fixed in SDK core: one summary attempt, then SDK-owned last-message fallback
- the plugin only returns typed controller results; SDK-owned last-message fallback does not write into plugin state
- summary model calls now opt into the SDK usage ledger with semantic request kinds: threshold-triggered passes record
auto_compaction, andsession.compact()summary calls recordmanual_compaction compaction.prunedefaults totrue; set it tofalseonly when the summary model should receive full tool text and structured content before chunking/truncation- the default summary prompt asks the model for plain-text summary body only; runtime normalizes accepted output back into one canonical
<continuation_summary>...</continuation_summary>block before replay, including wrapped blocks, merged multi-block outputs, and malformed-tag fragments that still yield a non-empty semantic body - when normalization yields an empty body, runtime preserves the previous saved continuation summary when one already exists; only empty output without a previous summary fails with
invalid_summary_contract lastResultis updated on every controller call withcompacted/skipped/failed, plus summary normalization and summary-call diagnostics when available- runtime notifications expose typed fallback metadata:
context.compacted.metadata.resolutioncan besummaryorlast_message_fallback; controller failures usecontext.compaction.failed.metadata.attemptStage = 'default_summary' - persisted plugin state keeps only semantic provenance fields; summary-call counters and per-message id arrays stay out of
pluginState context.compactednotifications now include compacted / retained message counts plus estimator-based before / after / saved token fields when budgeting is enabledretainMessagesremains a preferred target, but the planner adjusts to legal message boundaries and can use terminal compaction when the visible history ends with assistant/tool messages- the runtime keeps leading system messages first, appends runtime prompt/context next, then uses synthetic
usermessages for the auto-compact environment note and the compaction summary before retained messages - run
pnpm run demo:auto-compactin the repo for the scripted walkthrough
