@agimon-ai/po-autocompact 0.0.0-alpha.0
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/LICENSE +21 -0
- package/README.md +114 -0
- package/dist/apiContracts.cjs +17 -0
- package/dist/apiContracts.cjs.map +1 -0
- package/dist/apiContracts.d.mts +53 -0
- package/dist/apiContracts.d.mts.map +1 -0
- package/dist/apiContracts.mjs +13 -0
- package/dist/apiContracts.mjs.map +1 -0
- package/dist/compactionPolicy-BIJJHcBP.cjs +459 -0
- package/dist/compactionPolicy-BIJJHcBP.cjs.map +1 -0
- package/dist/compactionPolicy-CDCYTj2b.mjs +328 -0
- package/dist/compactionPolicy-CDCYTj2b.mjs.map +1 -0
- package/dist/extensions/server.cjs +855 -0
- package/dist/extensions/server.cjs.map +1 -0
- package/dist/extensions/server.d.mts +5 -0
- package/dist/extensions/server.d.mts.map +1 -0
- package/dist/extensions/server.mjs +850 -0
- package/dist/extensions/server.mjs.map +1 -0
- package/dist/extensions/web.mjs +161 -0
- package/dist/extensions/web.mjs.map +1 -0
- package/dist/index.cjs +3 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/package.json +111 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compactionPolicy-BIJJHcBP.cjs","names":["DEFAULT_COMPACTION_SETTINGS"],"sources":["../src/constants/autocompact.ts","../src/services/compactionPolicy/index.ts"],"sourcesContent":["export const STATE_CUSTOM_TYPE = 'doom-autocompact-state';\nexport const CHECKPOINT_MESSAGE_TYPE = 'doom-autocompact-checkpoint';\nexport const CONTEXT_MESSAGE_TYPE = 'doom-autocompact-context';\nexport const RESUME_MESSAGE_TYPE = 'doom-autocompact-resume';\nexport const RUNTIME_STATE_MESSAGE_TYPE = 'doom-autocompact-runtime-state';\nexport const STATE_VERSION = 2;\n/** Retrying a malformed checkpoint costs a full summarization call per turn, so a pass that\n * keeps failing is abandoned instead of retried for the rest of the session. */\nexport const MAX_INVALID_CHECKPOINT_ATTEMPTS = 3;\n\nexport const COMPACTION_THRESHOLDS = {\n 1: { ratio: 0.5, capTokens: 200_000 },\n 2: { ratio: 0.75, capTokens: 500_000 },\n 3: { ratio: 0.95, capTokens: 800_000 },\n} as const;\n\nexport const CHECKPOINT_HEADINGS = [\n '## Goal',\n '## Constraints & Preferences',\n '## Progress',\n '### Done',\n '### In Progress',\n '### Blocked',\n '## Key Decisions',\n '## Next Steps',\n '## Critical Context',\n] as const;\n","import {\n DEFAULT_COMPACTION_SETTINGS,\n type FileOperations,\n type SessionEntry,\n sessionEntryToContextMessages,\n} from '@earendil-works/pi-coding-agent';\n\nimport {\n CHECKPOINT_HEADINGS,\n CHECKPOINT_MESSAGE_TYPE,\n COMPACTION_THRESHOLDS,\n CONTEXT_MESSAGE_TYPE,\n STATE_VERSION,\n} from '../../constants/autocompact';\nimport type {\n AutocompactContextDetails,\n AutocompactFileDetails,\n AutocompactRatioOverrides,\n AutocompactTokenOverrides,\n AutocompactMessage,\n AutocompactPass,\n AutocompactState,\n CheckpointDecision,\n CheckpointRequestDetails,\n ContextProjection,\n} from '../../types/autocompact';\n\n/** V4 projections have numeric timestamps and store custom messages as message entries. */\nexport type NativePolicyEntry = { id: string; timestamp: number } & (\n | { type: 'message'; message: AutocompactMessage }\n | { type: 'custom'; customType: string; data?: unknown }\n | { type: 'compaction'; summary: string; tokensBefore: number; retainedTail: AutocompactMessage[] }\n | { type: 'branch_summary'; summary: string; fromId?: string | null }\n);\nexport type CompactionPolicyEntry = SessionEntry | NativePolicyEntry;\n\nfunction customMessage(entry: CompactionPolicyEntry) {\n if (entry.type === 'custom_message') return entry;\n if (entry.type === 'message' && entry.message.role === 'custom') return entry.message;\n return undefined;\n}\n\n/** Native entries are already projected messages, not Pi journal entries. */\nexport function policyEntryMessages(entry: CompactionPolicyEntry): AutocompactMessage[] {\n if (typeof entry.timestamp === 'string') return sessionEntryToContextMessages(entry as SessionEntry);\n if (entry.type === 'message') return [entry.message];\n if (entry.type === 'compaction' && 'retainedTail' in entry) {\n return [\n {\n role: 'compactionSummary',\n summary: entry.summary,\n tokensBefore: entry.tokensBefore,\n timestamp: entry.timestamp,\n },\n ...entry.retainedTail,\n ];\n }\n if (entry.type === 'branch_summary') {\n return [{ role: 'branchSummary', summary: entry.summary, fromId: entry.fromId ?? '', timestamp: entry.timestamp }];\n }\n return [];\n}\n\nconst SHOULD_COMPACT_PATTERN = /<shouldCompact>\\s*(true|false)\\s*<\\/shouldCompact>/i;\nconst FILE_SECTION_PATTERN = /\\n*<(?:read-files|modified-files)>[\\s\\S]*?<\\/(?:read-files|modified-files)>/g;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nfunction isStringArray(value: unknown): value is string[] {\n return Array.isArray(value) && value.every((item) => typeof item === 'string');\n}\n\nfunction isPass(value: unknown): value is AutocompactPass {\n return value === 1 || value === 2 || value === 3;\n}\n\nfunction assistantText(entry: CompactionPolicyEntry): string | undefined {\n if (entry.type !== 'message' || entry.message.role !== 'assistant') return undefined;\n const text = entry.message.content\n .filter((block) => block.type === 'text')\n .map((block) => block.text)\n .join('\\n')\n .trim();\n return text || undefined;\n}\n\nfunction isAutocompactMessage(value: unknown): value is AutocompactMessage {\n return isRecord(value) && typeof value.role === 'string';\n}\n\nexport function contextDetailsFromUnknown(value: unknown): AutocompactContextDetails | undefined {\n if (!isRecord(value) || !isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return undefined;\n if (!Array.isArray(value.retainedMessages) || !value.retainedMessages.every(isAutocompactMessage)) return undefined;\n if (!isRecord(value.doomAutocompact)) return undefined;\n const marker = value.doomAutocompact;\n if (\n marker.version !== STATE_VERSION ||\n (marker.pass !== 2 && marker.pass !== 3) ||\n typeof marker.cycle !== 'number' ||\n typeof marker.requestId !== 'string' ||\n typeof marker.snapshotLeafId !== 'string' ||\n typeof marker.tokensBefore !== 'number'\n )\n return undefined;\n return {\n readFiles: [...value.readFiles],\n modifiedFiles: [...value.modifiedFiles],\n retainedMessages: [...value.retainedMessages],\n doomAutocompact: {\n version: STATE_VERSION,\n cycle: marker.cycle,\n pass: marker.pass,\n requestId: marker.requestId,\n snapshotLeafId: marker.snapshotLeafId,\n tokensBefore: marker.tokensBefore,\n },\n };\n}\n\nexport function createInitialState(): AutocompactState {\n return {\n version: STATE_VERSION,\n cycle: 1,\n pass: 1,\n phase: 'waiting',\n checkpointQueue: [],\n exhaustedPasses: [],\n invalidAttempts: 0,\n baselineTokens: 0,\n baselinePending: false,\n readFiles: [],\n modifiedFiles: [],\n };\n}\n\n/**\n * The ratios each pass fires at, with configured values folded in.\n *\n * The ladder only works if it climbs, so a configured pass never sits below the\n * one before it: a file that says pass 2 fires earlier than pass 1 gets pass 1's\n * ratio for pass 2 rather than a pass that can never contribute.\n */\nexport function compactionRatios(configured: AutocompactRatioOverrides = {}): Record<AutocompactPass, number> {\n let previous = 0;\n const ratios = {} as Record<AutocompactPass, number>;\n for (const pass of [1, 2, 3] as const) {\n const ratio = Math.max(configured[pass] ?? COMPACTION_THRESHOLDS[pass].ratio, previous);\n ratios[pass] = ratio;\n previous = ratio;\n }\n return ratios;\n}\n\nexport function thresholdTokens(\n pass: AutocompactPass,\n contextWindow: number,\n baselineTokens = 0,\n configured: AutocompactRatioOverrides = {},\n): number {\n const threshold = COMPACTION_THRESHOLDS[pass];\n const ratio = compactionRatios(configured)[pass];\n const baseline = Math.max(0, Math.min(baselineTokens, contextWindow));\n const remaining = contextWindow - baseline;\n const staged = baseline + Math.min(Math.floor(remaining * ratio), threshold.capTokens);\n // Pi compacts natively above `contextWindow - reserveTokens`. A staged threshold above that\n // point can never fire, so the ladder would silently degrade into native compaction: on a\n // 200k window the raw pass 3 ratio lands at 190_000, while Pi already compacted at 183_616.\n const nativeTrigger = contextWindow - DEFAULT_COMPACTION_SETTINGS.reserveTokens;\n return nativeTrigger > baseline ? Math.min(staged, nativeTrigger) : staged;\n}\n\n/**\n * Where every pass fires, with a matching model's absolute token checkpoints folded in.\n *\n * A configured token count replaces the ratio only when it fires earlier, so an\n * override can bring a checkpoint forward on a very large window without ever\n * pushing one past the point Pi compacts natively.\n *\n * Two rules keep the ladder usable. A token count at or below the baseline is\n * ignored, because a compaction that lands above it would otherwise satisfy that\n * pass the moment the next cycle opens and re-fire it every turn. And a pass never\n * sits below the one before it, for the same reason {@link compactionRatios}\n * climbs: `evaluateUsage` skips any pass below the one already reached, so a later\n * pass that fires first does not merely reorder the ladder, it locks the earlier\n * passes out of the cycle and forces compaction with no summary ever written.\n */\nexport function effectiveThresholds(\n contextWindow: number,\n baselineTokens = 0,\n configured: AutocompactRatioOverrides = {},\n tokenOverrides: AutocompactTokenOverrides = {},\n): Record<AutocompactPass, number> {\n let previous = 0;\n const thresholds = {} as Record<AutocompactPass, number>;\n for (const pass of [1, 2, 3] as const) {\n const ratioTokens = thresholdTokens(pass, contextWindow, baselineTokens, configured);\n const absolute = tokenOverrides[pass];\n const staged = absolute !== undefined && absolute > baselineTokens ? Math.min(ratioTokens, absolute) : ratioTokens;\n thresholds[pass] = Math.max(staged, previous);\n previous = thresholds[pass];\n }\n return thresholds;\n}\nexport function latestCheckpointArtifactEntry(\n branchEntries: readonly CompactionPolicyEntry[],\n): CompactionPolicyEntry | undefined {\n return branchEntries.findLast(\n (entry) =>\n entry.type === 'compaction' ||\n // Staged checkpoints are plain entries so they stay out of the agent's LLM context.\n (entry.type === 'custom' && entry.customType === CHECKPOINT_MESSAGE_TYPE) ||\n // Legacy sessions staged checkpoints as custom messages; committed markers still are.\n customMessage(entry)?.customType === CHECKPOINT_MESSAGE_TYPE ||\n customMessage(entry)?.customType === CONTEXT_MESSAGE_TYPE,\n );\n}\n\nexport function baselineUsageIsSettled(branchEntries: readonly CompactionPolicyEntry[]): boolean {\n const boundaryIndex = branchEntries.findLastIndex(\n (entry) => entry.type === 'compaction' || customMessage(entry)?.customType === CONTEXT_MESSAGE_TYPE,\n );\n return branchEntries.slice(boundaryIndex + 1).some((entry) => {\n if (entry.type !== 'message' || entry.message.role !== 'assistant') return false;\n const { stopReason, usage } = entry.message;\n if (stopReason === 'aborted' || stopReason === 'error' || !usage) return false;\n return (usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite) > 0;\n });\n}\n\nexport function retainedMessagesAfterSnapshot(\n branchEntries: readonly CompactionPolicyEntry[],\n snapshotLeafId: string,\n): AutocompactMessage[] {\n const snapshotIndex = branchEntries.findIndex((entry) => entry.id === snapshotLeafId);\n if (snapshotIndex === -1) return [];\n return branchEntries\n .slice(snapshotIndex + 1)\n .filter((entry) => customMessage(entry)?.customType !== CHECKPOINT_MESSAGE_TYPE)\n .flatMap(policyEntryMessages);\n}\n\nexport function projectContextMessages(\n messages: AutocompactMessage[],\n branchEntries: readonly CompactionPolicyEntry[],\n): ContextProjection {\n const nativeCompactionIndex = branchEntries.findLastIndex((entry) => entry.type === 'compaction');\n const markerEntry = branchEntries\n .slice(nativeCompactionIndex + 1)\n .findLast((entry) => customMessage(entry)?.customType === CONTEXT_MESSAGE_TYPE);\n const markerMessage = markerEntry && customMessage(markerEntry);\n if (!markerEntry || !markerMessage) {\n return { messages, retainedMessageCount: 0 };\n }\n\n const details = contextDetailsFromUnknown(markerMessage.details);\n const summary = checkpointSummaryFromEntry(markerEntry);\n if (!details || !summary) return { messages, invalidMarker: true, retainedMessageCount: 0 };\n\n const markerMessageIndex = messages.findLastIndex(\n (message) =>\n message.role === 'custom' &&\n message.customType === CONTEXT_MESSAGE_TYPE &&\n contextDetailsFromUnknown(message.details)?.doomAutocompact.requestId === details.doomAutocompact.requestId,\n );\n if (markerMessageIndex === -1) return { messages, invalidMarker: true, retainedMessageCount: 0 };\n\n const summaryMessage: AutocompactMessage = {\n role: 'compactionSummary',\n summary,\n tokensBefore: details.doomAutocompact.tokensBefore,\n timestamp: messages[markerMessageIndex]?.timestamp ?? Date.now(),\n };\n return {\n messages: [summaryMessage, ...details.retainedMessages, ...messages.slice(markerMessageIndex + 1)],\n marker: details.doomAutocompact,\n retainedMessageCount: details.retainedMessages.length,\n };\n}\n\nexport function fileOperationsFromMessages(messages: AutocompactMessage[]): FileOperations {\n const fileOps: FileOperations = { read: new Set(), written: new Set(), edited: new Set() };\n for (const message of messages) {\n if (message.role !== 'assistant') continue;\n for (const block of message.content) {\n if (block.type !== 'toolCall') continue;\n const path = typeof block.arguments.path === 'string' ? block.arguments.path : undefined;\n if (!path) continue;\n if (block.name === 'read') fileOps.read.add(path);\n else if (block.name === 'write') fileOps.written.add(path);\n else if (block.name === 'edit') fileOps.edited.add(path);\n }\n }\n return fileOps;\n}\n\nexport function parseState(value: unknown): AutocompactState | undefined {\n if (!isRecord(value)) return undefined;\n if (value.version !== STATE_VERSION || typeof value.cycle !== 'number' || !isPass(value.pass)) return undefined;\n if (\n value.phase !== 'waiting' &&\n value.phase !== 'checkpoint_pending' &&\n value.phase !== 'checkpoint_ready' &&\n value.phase !== 'compacting'\n )\n return undefined;\n const baselineTokens = value.baselineTokens ?? 0;\n if (typeof baselineTokens !== 'number' || baselineTokens < 0) return undefined;\n const baselinePending = value.baselinePending ?? false;\n if (typeof baselinePending !== 'boolean') return undefined;\n const checkpointQueue = value.checkpointQueue ?? [];\n if (\n !Array.isArray(checkpointQueue) ||\n !checkpointQueue.every(isPass) ||\n checkpointQueue.some((pass, index) => index > 0 && pass <= checkpointQueue[index - 1]!)\n )\n return undefined;\n // Sessions persisted before bounded retry landed simply have no attempt history to restore.\n const exhaustedPasses = value.exhaustedPasses ?? [];\n if (!Array.isArray(exhaustedPasses) || !exhaustedPasses.every(isPass)) return undefined;\n const invalidAttempts = value.invalidAttempts ?? 0;\n if (typeof invalidAttempts !== 'number' || invalidAttempts < 0) return undefined;\n if (!isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return undefined;\n if (value.requestId !== undefined && typeof value.requestId !== 'string') return undefined;\n if (value.compactionPass !== undefined && value.compactionPass !== 2 && value.compactionPass !== 3) return undefined;\n if ('latestCheckpointEntryId' in value) return undefined;\n if (value.latestCheckpointSnapshotLeafId !== undefined && typeof value.latestCheckpointSnapshotLeafId !== 'string')\n return undefined;\n if (value.lastAttemptLeafId !== undefined && typeof value.lastAttemptLeafId !== 'string') return undefined;\n if (value.snapshotLeafId !== undefined && typeof value.snapshotLeafId !== 'string') return undefined;\n if (value.snapshotTokens !== undefined && (typeof value.snapshotTokens !== 'number' || value.snapshotTokens < 0))\n return undefined;\n if (value.pendingCheckpoint !== undefined && typeof value.pendingCheckpoint !== 'string') return undefined;\n\n return {\n version: STATE_VERSION,\n cycle: value.cycle,\n pass: value.pass,\n phase: value.phase,\n checkpointQueue: [...checkpointQueue],\n exhaustedPasses: [...exhaustedPasses],\n invalidAttempts,\n baselineTokens,\n baselinePending,\n readFiles: [...value.readFiles],\n modifiedFiles: [...value.modifiedFiles],\n ...(value.requestId ? { requestId: value.requestId } : {}),\n ...(value.compactionPass ? { compactionPass: value.compactionPass } : {}),\n ...(value.latestCheckpointSnapshotLeafId\n ? { latestCheckpointSnapshotLeafId: value.latestCheckpointSnapshotLeafId }\n : {}),\n ...(value.lastAttemptLeafId ? { lastAttemptLeafId: value.lastAttemptLeafId } : {}),\n ...(value.snapshotLeafId ? { snapshotLeafId: value.snapshotLeafId } : {}),\n ...(value.snapshotTokens !== undefined ? { snapshotTokens: value.snapshotTokens } : {}),\n ...(value.pendingCheckpoint ? { pendingCheckpoint: value.pendingCheckpoint } : {}),\n };\n}\n\nexport function checkpointSummaryFromEntry(entry: CompactionPolicyEntry | undefined): string | undefined {\n if (!entry) return undefined;\n if (entry.type === 'compaction') return entry.summary.trim() || undefined;\n if (entry.type === 'custom') {\n return isRecord(entry.data) && typeof entry.data.summary === 'string'\n ? entry.data.summary.trim() || undefined\n : undefined;\n }\n const marker = customMessage(entry);\n if (marker) {\n const content =\n typeof marker.content === 'string'\n ? marker.content\n : marker.content\n .filter((block) => block.type === 'text')\n .map((block) => block.text)\n .join('\\n');\n return content.trim() || undefined;\n }\n return assistantText(entry);\n}\n\nexport function parseCheckpointDecision(response: string): CheckpointDecision {\n const match = response.match(SHOULD_COMPACT_PATTERN);\n const summary = response.replace(SHOULD_COMPACT_PATTERN, '').trim();\n if (!match) return { summary };\n return { summary, shouldCompact: match[1]?.toLowerCase() === 'true' };\n}\n\nexport function isStructuredCheckpoint(summary: string): boolean {\n return CHECKPOINT_HEADINGS.every((heading) => summary.includes(heading));\n}\n\nexport function createRequestId(\n sessionId: string,\n cycle: number,\n pass: AutocompactPass,\n leafId: string | null,\n): string {\n return `${sessionId}:${cycle}:${pass}:${leafId ?? 'root'}`;\n}\n\nexport function checkpointRequestDetails(\n cycle: number,\n pass: AutocompactPass,\n requestId: string,\n): CheckpointRequestDetails {\n return { version: STATE_VERSION, cycle, pass, requestId };\n}\n\nexport function mergeFileDetails(\n current: AutocompactFileDetails,\n fileOps?: FileOperations,\n additional?: AutocompactFileDetails,\n): AutocompactFileDetails {\n const modified = new Set([\n ...current.modifiedFiles,\n ...(additional?.modifiedFiles ?? []),\n ...(fileOps ? [...fileOps.edited, ...fileOps.written] : []),\n ]);\n const read = new Set([...current.readFiles, ...(additional?.readFiles ?? []), ...(fileOps ? [...fileOps.read] : [])]);\n for (const path of modified) read.delete(path);\n return { readFiles: [...read].sort(), modifiedFiles: [...modified].sort() };\n}\n\nexport function fileDetailsFromUnknown(value: unknown): AutocompactFileDetails | undefined {\n if (!isRecord(value) || !isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return undefined;\n return { readFiles: value.readFiles, modifiedFiles: value.modifiedFiles };\n}\n\nexport function withCanonicalFileSections(summary: string, details: AutocompactFileDetails): string {\n const sections: string[] = [];\n if (details.readFiles.length > 0) sections.push(`<read-files>\\n${details.readFiles.join('\\n')}\\n</read-files>`);\n if (details.modifiedFiles.length > 0) {\n sections.push(`<modified-files>\\n${details.modifiedFiles.join('\\n')}\\n</modified-files>`);\n }\n const normalized = summary.replace(FILE_SECTION_PATTERN, '').trim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
|
1
|
+
import { DEFAULT_COMPACTION_SETTINGS } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
//#region src/constants/autocompact.ts
|
|
3
|
+
const STATE_CUSTOM_TYPE = "doom-autocompact-state";
|
|
4
|
+
const CHECKPOINT_MESSAGE_TYPE = "doom-autocompact-checkpoint";
|
|
5
|
+
const CONTEXT_MESSAGE_TYPE = "doom-autocompact-context";
|
|
6
|
+
const RESUME_MESSAGE_TYPE = "doom-autocompact-resume";
|
|
7
|
+
const RUNTIME_STATE_MESSAGE_TYPE = "doom-autocompact-runtime-state";
|
|
8
|
+
const COMPACTION_THRESHOLDS = {
|
|
9
|
+
1: {
|
|
10
|
+
ratio: .5,
|
|
11
|
+
capTokens: 2e5
|
|
12
|
+
},
|
|
13
|
+
2: {
|
|
14
|
+
ratio: .75,
|
|
15
|
+
capTokens: 5e5
|
|
16
|
+
},
|
|
17
|
+
3: {
|
|
18
|
+
ratio: .95,
|
|
19
|
+
capTokens: 8e5
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const CHECKPOINT_HEADINGS = [
|
|
23
|
+
"## Goal",
|
|
24
|
+
"## Constraints & Preferences",
|
|
25
|
+
"## Progress",
|
|
26
|
+
"### Done",
|
|
27
|
+
"### In Progress",
|
|
28
|
+
"### Blocked",
|
|
29
|
+
"## Key Decisions",
|
|
30
|
+
"## Next Steps",
|
|
31
|
+
"## Critical Context"
|
|
32
|
+
];
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/services/compactionPolicy/index.ts
|
|
35
|
+
function customMessage(entry) {
|
|
36
|
+
if (entry.type === "custom_message") return entry;
|
|
37
|
+
if (entry.type === "message" && entry.message.role === "custom") return entry.message;
|
|
38
|
+
}
|
|
39
|
+
const SHOULD_COMPACT_PATTERN = /<shouldCompact>\s*(true|false)\s*<\/shouldCompact>/i;
|
|
40
|
+
const FILE_SECTION_PATTERN = /\n*<(?:read-files|modified-files)>[\s\S]*?<\/(?:read-files|modified-files)>/g;
|
|
41
|
+
function isRecord(value) {
|
|
42
|
+
return typeof value === "object" && value !== null;
|
|
43
|
+
}
|
|
44
|
+
function isStringArray(value) {
|
|
45
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
46
|
+
}
|
|
47
|
+
function isPass(value) {
|
|
48
|
+
return value === 1 || value === 2 || value === 3;
|
|
49
|
+
}
|
|
50
|
+
function assistantText(entry) {
|
|
51
|
+
if (entry.type !== "message" || entry.message.role !== "assistant") return void 0;
|
|
52
|
+
return entry.message.content.filter((block) => block.type === "text").map((block) => block.text).join("\n").trim() || void 0;
|
|
53
|
+
}
|
|
54
|
+
function isAutocompactMessage(value) {
|
|
55
|
+
return isRecord(value) && typeof value.role === "string";
|
|
56
|
+
}
|
|
57
|
+
function contextDetailsFromUnknown(value) {
|
|
58
|
+
if (!isRecord(value) || !isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return void 0;
|
|
59
|
+
if (!Array.isArray(value.retainedMessages) || !value.retainedMessages.every(isAutocompactMessage)) return void 0;
|
|
60
|
+
if (!isRecord(value.doomAutocompact)) return void 0;
|
|
61
|
+
const marker = value.doomAutocompact;
|
|
62
|
+
if (marker.version !== 2 || marker.pass !== 2 && marker.pass !== 3 || typeof marker.cycle !== "number" || typeof marker.requestId !== "string" || typeof marker.snapshotLeafId !== "string" || typeof marker.tokensBefore !== "number") return void 0;
|
|
63
|
+
return {
|
|
64
|
+
readFiles: [...value.readFiles],
|
|
65
|
+
modifiedFiles: [...value.modifiedFiles],
|
|
66
|
+
retainedMessages: [...value.retainedMessages],
|
|
67
|
+
doomAutocompact: {
|
|
68
|
+
version: 2,
|
|
69
|
+
cycle: marker.cycle,
|
|
70
|
+
pass: marker.pass,
|
|
71
|
+
requestId: marker.requestId,
|
|
72
|
+
snapshotLeafId: marker.snapshotLeafId,
|
|
73
|
+
tokensBefore: marker.tokensBefore
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function createInitialState() {
|
|
78
|
+
return {
|
|
79
|
+
version: 2,
|
|
80
|
+
cycle: 1,
|
|
81
|
+
pass: 1,
|
|
82
|
+
phase: "waiting",
|
|
83
|
+
checkpointQueue: [],
|
|
84
|
+
exhaustedPasses: [],
|
|
85
|
+
invalidAttempts: 0,
|
|
86
|
+
baselineTokens: 0,
|
|
87
|
+
baselinePending: false,
|
|
88
|
+
readFiles: [],
|
|
89
|
+
modifiedFiles: []
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The ratios each pass fires at, with configured values folded in.
|
|
94
|
+
*
|
|
95
|
+
* The ladder only works if it climbs, so a configured pass never sits below the
|
|
96
|
+
* one before it: a file that says pass 2 fires earlier than pass 1 gets pass 1's
|
|
97
|
+
* ratio for pass 2 rather than a pass that can never contribute.
|
|
98
|
+
*/
|
|
99
|
+
function compactionRatios(configured = {}) {
|
|
100
|
+
let previous = 0;
|
|
101
|
+
const ratios = {};
|
|
102
|
+
for (const pass of [
|
|
103
|
+
1,
|
|
104
|
+
2,
|
|
105
|
+
3
|
|
106
|
+
]) {
|
|
107
|
+
const ratio = Math.max(configured[pass] ?? COMPACTION_THRESHOLDS[pass].ratio, previous);
|
|
108
|
+
ratios[pass] = ratio;
|
|
109
|
+
previous = ratio;
|
|
110
|
+
}
|
|
111
|
+
return ratios;
|
|
112
|
+
}
|
|
113
|
+
function thresholdTokens(pass, contextWindow, baselineTokens = 0, configured = {}) {
|
|
114
|
+
const threshold = COMPACTION_THRESHOLDS[pass];
|
|
115
|
+
const ratio = compactionRatios(configured)[pass];
|
|
116
|
+
const baseline = Math.max(0, Math.min(baselineTokens, contextWindow));
|
|
117
|
+
const remaining = contextWindow - baseline;
|
|
118
|
+
const staged = baseline + Math.min(Math.floor(remaining * ratio), threshold.capTokens);
|
|
119
|
+
const nativeTrigger = contextWindow - DEFAULT_COMPACTION_SETTINGS.reserveTokens;
|
|
120
|
+
return nativeTrigger > baseline ? Math.min(staged, nativeTrigger) : staged;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Where every pass fires, with a matching model's absolute token checkpoints folded in.
|
|
124
|
+
*
|
|
125
|
+
* A configured token count replaces the ratio only when it fires earlier, so an
|
|
126
|
+
* override can bring a checkpoint forward on a very large window without ever
|
|
127
|
+
* pushing one past the point Pi compacts natively.
|
|
128
|
+
*
|
|
129
|
+
* Two rules keep the ladder usable. A token count at or below the baseline is
|
|
130
|
+
* ignored, because a compaction that lands above it would otherwise satisfy that
|
|
131
|
+
* pass the moment the next cycle opens and re-fire it every turn. And a pass never
|
|
132
|
+
* sits below the one before it, for the same reason {@link compactionRatios}
|
|
133
|
+
* climbs: `evaluateUsage` skips any pass below the one already reached, so a later
|
|
134
|
+
* pass that fires first does not merely reorder the ladder, it locks the earlier
|
|
135
|
+
* passes out of the cycle and forces compaction with no summary ever written.
|
|
136
|
+
*/
|
|
137
|
+
function effectiveThresholds(contextWindow, baselineTokens = 0, configured = {}, tokenOverrides = {}) {
|
|
138
|
+
let previous = 0;
|
|
139
|
+
const thresholds = {};
|
|
140
|
+
for (const pass of [
|
|
141
|
+
1,
|
|
142
|
+
2,
|
|
143
|
+
3
|
|
144
|
+
]) {
|
|
145
|
+
const ratioTokens = thresholdTokens(pass, contextWindow, baselineTokens, configured);
|
|
146
|
+
const absolute = tokenOverrides[pass];
|
|
147
|
+
thresholds[pass] = Math.max(absolute !== void 0 && absolute > baselineTokens ? Math.min(ratioTokens, absolute) : ratioTokens, previous);
|
|
148
|
+
previous = thresholds[pass];
|
|
149
|
+
}
|
|
150
|
+
return thresholds;
|
|
151
|
+
}
|
|
152
|
+
function latestCheckpointArtifactEntry(branchEntries) {
|
|
153
|
+
return branchEntries.findLast((entry) => entry.type === "compaction" || entry.type === "custom" && entry.customType === "doom-autocompact-checkpoint" || customMessage(entry)?.customType === "doom-autocompact-checkpoint" || customMessage(entry)?.customType === "doom-autocompact-context");
|
|
154
|
+
}
|
|
155
|
+
function baselineUsageIsSettled(branchEntries) {
|
|
156
|
+
const boundaryIndex = branchEntries.findLastIndex((entry) => entry.type === "compaction" || customMessage(entry)?.customType === "doom-autocompact-context");
|
|
157
|
+
return branchEntries.slice(boundaryIndex + 1).some((entry) => {
|
|
158
|
+
if (entry.type !== "message" || entry.message.role !== "assistant") return false;
|
|
159
|
+
const { stopReason, usage } = entry.message;
|
|
160
|
+
if (stopReason === "aborted" || stopReason === "error" || !usage) return false;
|
|
161
|
+
return (usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite) > 0;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
function projectContextMessages(messages, branchEntries) {
|
|
165
|
+
const nativeCompactionIndex = branchEntries.findLastIndex((entry) => entry.type === "compaction");
|
|
166
|
+
const markerEntry = branchEntries.slice(nativeCompactionIndex + 1).findLast((entry) => customMessage(entry)?.customType === CONTEXT_MESSAGE_TYPE);
|
|
167
|
+
const markerMessage = markerEntry && customMessage(markerEntry);
|
|
168
|
+
if (!markerEntry || !markerMessage) return {
|
|
169
|
+
messages,
|
|
170
|
+
retainedMessageCount: 0
|
|
171
|
+
};
|
|
172
|
+
const details = contextDetailsFromUnknown(markerMessage.details);
|
|
173
|
+
const summary = checkpointSummaryFromEntry(markerEntry);
|
|
174
|
+
if (!details || !summary) return {
|
|
175
|
+
messages,
|
|
176
|
+
invalidMarker: true,
|
|
177
|
+
retainedMessageCount: 0
|
|
178
|
+
};
|
|
179
|
+
const markerMessageIndex = messages.findLastIndex((message) => message.role === "custom" && message.customType === "doom-autocompact-context" && contextDetailsFromUnknown(message.details)?.doomAutocompact.requestId === details.doomAutocompact.requestId);
|
|
180
|
+
if (markerMessageIndex === -1) return {
|
|
181
|
+
messages,
|
|
182
|
+
invalidMarker: true,
|
|
183
|
+
retainedMessageCount: 0
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
messages: [
|
|
187
|
+
{
|
|
188
|
+
role: "compactionSummary",
|
|
189
|
+
summary,
|
|
190
|
+
tokensBefore: details.doomAutocompact.tokensBefore,
|
|
191
|
+
timestamp: messages[markerMessageIndex]?.timestamp ?? Date.now()
|
|
192
|
+
},
|
|
193
|
+
...details.retainedMessages,
|
|
194
|
+
...messages.slice(markerMessageIndex + 1)
|
|
195
|
+
],
|
|
196
|
+
marker: details.doomAutocompact,
|
|
197
|
+
retainedMessageCount: details.retainedMessages.length
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function fileOperationsFromMessages(messages) {
|
|
201
|
+
const fileOps = {
|
|
202
|
+
read: /* @__PURE__ */ new Set(),
|
|
203
|
+
written: /* @__PURE__ */ new Set(),
|
|
204
|
+
edited: /* @__PURE__ */ new Set()
|
|
205
|
+
};
|
|
206
|
+
for (const message of messages) {
|
|
207
|
+
if (message.role !== "assistant") continue;
|
|
208
|
+
for (const block of message.content) {
|
|
209
|
+
if (block.type !== "toolCall") continue;
|
|
210
|
+
const path = typeof block.arguments.path === "string" ? block.arguments.path : void 0;
|
|
211
|
+
if (!path) continue;
|
|
212
|
+
if (block.name === "read") fileOps.read.add(path);
|
|
213
|
+
else if (block.name === "write") fileOps.written.add(path);
|
|
214
|
+
else if (block.name === "edit") fileOps.edited.add(path);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return fileOps;
|
|
218
|
+
}
|
|
219
|
+
function parseState(value) {
|
|
220
|
+
if (!isRecord(value)) return void 0;
|
|
221
|
+
if (value.version !== 2 || typeof value.cycle !== "number" || !isPass(value.pass)) return void 0;
|
|
222
|
+
if (value.phase !== "waiting" && value.phase !== "checkpoint_pending" && value.phase !== "checkpoint_ready" && value.phase !== "compacting") return void 0;
|
|
223
|
+
const baselineTokens = value.baselineTokens ?? 0;
|
|
224
|
+
if (typeof baselineTokens !== "number" || baselineTokens < 0) return void 0;
|
|
225
|
+
const baselinePending = value.baselinePending ?? false;
|
|
226
|
+
if (typeof baselinePending !== "boolean") return void 0;
|
|
227
|
+
const checkpointQueue = value.checkpointQueue ?? [];
|
|
228
|
+
if (!Array.isArray(checkpointQueue) || !checkpointQueue.every(isPass) || checkpointQueue.some((pass, index) => index > 0 && pass <= checkpointQueue[index - 1])) return void 0;
|
|
229
|
+
const exhaustedPasses = value.exhaustedPasses ?? [];
|
|
230
|
+
if (!Array.isArray(exhaustedPasses) || !exhaustedPasses.every(isPass)) return void 0;
|
|
231
|
+
const invalidAttempts = value.invalidAttempts ?? 0;
|
|
232
|
+
if (typeof invalidAttempts !== "number" || invalidAttempts < 0) return void 0;
|
|
233
|
+
if (!isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return void 0;
|
|
234
|
+
if (value.requestId !== void 0 && typeof value.requestId !== "string") return void 0;
|
|
235
|
+
if (value.compactionPass !== void 0 && value.compactionPass !== 2 && value.compactionPass !== 3) return void 0;
|
|
236
|
+
if ("latestCheckpointEntryId" in value) return void 0;
|
|
237
|
+
if (value.latestCheckpointSnapshotLeafId !== void 0 && typeof value.latestCheckpointSnapshotLeafId !== "string") return void 0;
|
|
238
|
+
if (value.lastAttemptLeafId !== void 0 && typeof value.lastAttemptLeafId !== "string") return void 0;
|
|
239
|
+
if (value.snapshotLeafId !== void 0 && typeof value.snapshotLeafId !== "string") return void 0;
|
|
240
|
+
if (value.snapshotTokens !== void 0 && (typeof value.snapshotTokens !== "number" || value.snapshotTokens < 0)) return void 0;
|
|
241
|
+
if (value.pendingCheckpoint !== void 0 && typeof value.pendingCheckpoint !== "string") return void 0;
|
|
242
|
+
return {
|
|
243
|
+
version: 2,
|
|
244
|
+
cycle: value.cycle,
|
|
245
|
+
pass: value.pass,
|
|
246
|
+
phase: value.phase,
|
|
247
|
+
checkpointQueue: [...checkpointQueue],
|
|
248
|
+
exhaustedPasses: [...exhaustedPasses],
|
|
249
|
+
invalidAttempts,
|
|
250
|
+
baselineTokens,
|
|
251
|
+
baselinePending,
|
|
252
|
+
readFiles: [...value.readFiles],
|
|
253
|
+
modifiedFiles: [...value.modifiedFiles],
|
|
254
|
+
...value.requestId ? { requestId: value.requestId } : {},
|
|
255
|
+
...value.compactionPass ? { compactionPass: value.compactionPass } : {},
|
|
256
|
+
...value.latestCheckpointSnapshotLeafId ? { latestCheckpointSnapshotLeafId: value.latestCheckpointSnapshotLeafId } : {},
|
|
257
|
+
...value.lastAttemptLeafId ? { lastAttemptLeafId: value.lastAttemptLeafId } : {},
|
|
258
|
+
...value.snapshotLeafId ? { snapshotLeafId: value.snapshotLeafId } : {},
|
|
259
|
+
...value.snapshotTokens !== void 0 ? { snapshotTokens: value.snapshotTokens } : {},
|
|
260
|
+
...value.pendingCheckpoint ? { pendingCheckpoint: value.pendingCheckpoint } : {}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
function checkpointSummaryFromEntry(entry) {
|
|
264
|
+
if (!entry) return void 0;
|
|
265
|
+
if (entry.type === "compaction") return entry.summary.trim() || void 0;
|
|
266
|
+
if (entry.type === "custom") return isRecord(entry.data) && typeof entry.data.summary === "string" ? entry.data.summary.trim() || void 0 : void 0;
|
|
267
|
+
const marker = customMessage(entry);
|
|
268
|
+
if (marker) return (typeof marker.content === "string" ? marker.content : marker.content.filter((block) => block.type === "text").map((block) => block.text).join("\n")).trim() || void 0;
|
|
269
|
+
return assistantText(entry);
|
|
270
|
+
}
|
|
271
|
+
function parseCheckpointDecision(response) {
|
|
272
|
+
const match = response.match(SHOULD_COMPACT_PATTERN);
|
|
273
|
+
const summary = response.replace(SHOULD_COMPACT_PATTERN, "").trim();
|
|
274
|
+
if (!match) return { summary };
|
|
275
|
+
return {
|
|
276
|
+
summary,
|
|
277
|
+
shouldCompact: match[1]?.toLowerCase() === "true"
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function isStructuredCheckpoint(summary) {
|
|
281
|
+
return CHECKPOINT_HEADINGS.every((heading) => summary.includes(heading));
|
|
282
|
+
}
|
|
283
|
+
function createRequestId(sessionId, cycle, pass, leafId) {
|
|
284
|
+
return `${sessionId}:${cycle}:${pass}:${leafId ?? "root"}`;
|
|
285
|
+
}
|
|
286
|
+
function checkpointRequestDetails(cycle, pass, requestId) {
|
|
287
|
+
return {
|
|
288
|
+
version: 2,
|
|
289
|
+
cycle,
|
|
290
|
+
pass,
|
|
291
|
+
requestId
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function mergeFileDetails(current, fileOps, additional) {
|
|
295
|
+
const modified = /* @__PURE__ */ new Set([
|
|
296
|
+
...current.modifiedFiles,
|
|
297
|
+
...additional?.modifiedFiles ?? [],
|
|
298
|
+
...fileOps ? [...fileOps.edited, ...fileOps.written] : []
|
|
299
|
+
]);
|
|
300
|
+
const read = /* @__PURE__ */ new Set([
|
|
301
|
+
...current.readFiles,
|
|
302
|
+
...additional?.readFiles ?? [],
|
|
303
|
+
...fileOps ? [...fileOps.read] : []
|
|
304
|
+
]);
|
|
305
|
+
for (const path of modified) read.delete(path);
|
|
306
|
+
return {
|
|
307
|
+
readFiles: [...read].sort(),
|
|
308
|
+
modifiedFiles: [...modified].sort()
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function fileDetailsFromUnknown(value) {
|
|
312
|
+
if (!isRecord(value) || !isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return void 0;
|
|
313
|
+
return {
|
|
314
|
+
readFiles: value.readFiles,
|
|
315
|
+
modifiedFiles: value.modifiedFiles
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function withCanonicalFileSections(summary, details) {
|
|
319
|
+
const sections = [];
|
|
320
|
+
if (details.readFiles.length > 0) sections.push(`<read-files>\n${details.readFiles.join("\n")}\n</read-files>`);
|
|
321
|
+
if (details.modifiedFiles.length > 0) sections.push(`<modified-files>\n${details.modifiedFiles.join("\n")}\n</modified-files>`);
|
|
322
|
+
const normalized = summary.replace(FILE_SECTION_PATTERN, "").trim();
|
|
323
|
+
return sections.length > 0 ? `${normalized}\n\n${sections.join("\n\n")}` : normalized;
|
|
324
|
+
}
|
|
325
|
+
//#endregion
|
|
326
|
+
export { STATE_CUSTOM_TYPE as S, withCanonicalFileSections as _, createInitialState as a, RESUME_MESSAGE_TYPE as b, fileDetailsFromUnknown as c, latestCheckpointArtifactEntry as d, mergeFileDetails as f, thresholdTokens as g, projectContextMessages as h, contextDetailsFromUnknown as i, fileOperationsFromMessages as l, parseState as m, checkpointRequestDetails as n, createRequestId as o, parseCheckpointDecision as p, checkpointSummaryFromEntry as r, effectiveThresholds as s, baselineUsageIsSettled as t, isStructuredCheckpoint as u, CHECKPOINT_MESSAGE_TYPE as v, RUNTIME_STATE_MESSAGE_TYPE as x, CONTEXT_MESSAGE_TYPE as y };
|
|
327
|
+
|
|
328
|
+
//# sourceMappingURL=compactionPolicy-CDCYTj2b.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compactionPolicy-CDCYTj2b.mjs","names":[],"sources":["../src/constants/autocompact.ts","../src/services/compactionPolicy/index.ts"],"sourcesContent":["export const STATE_CUSTOM_TYPE = 'doom-autocompact-state';\nexport const CHECKPOINT_MESSAGE_TYPE = 'doom-autocompact-checkpoint';\nexport const CONTEXT_MESSAGE_TYPE = 'doom-autocompact-context';\nexport const RESUME_MESSAGE_TYPE = 'doom-autocompact-resume';\nexport const RUNTIME_STATE_MESSAGE_TYPE = 'doom-autocompact-runtime-state';\nexport const STATE_VERSION = 2;\n/** Retrying a malformed checkpoint costs a full summarization call per turn, so a pass that\n * keeps failing is abandoned instead of retried for the rest of the session. */\nexport const MAX_INVALID_CHECKPOINT_ATTEMPTS = 3;\n\nexport const COMPACTION_THRESHOLDS = {\n 1: { ratio: 0.5, capTokens: 200_000 },\n 2: { ratio: 0.75, capTokens: 500_000 },\n 3: { ratio: 0.95, capTokens: 800_000 },\n} as const;\n\nexport const CHECKPOINT_HEADINGS = [\n '## Goal',\n '## Constraints & Preferences',\n '## Progress',\n '### Done',\n '### In Progress',\n '### Blocked',\n '## Key Decisions',\n '## Next Steps',\n '## Critical Context',\n] as const;\n","import {\n DEFAULT_COMPACTION_SETTINGS,\n type FileOperations,\n type SessionEntry,\n sessionEntryToContextMessages,\n} from '@earendil-works/pi-coding-agent';\n\nimport {\n CHECKPOINT_HEADINGS,\n CHECKPOINT_MESSAGE_TYPE,\n COMPACTION_THRESHOLDS,\n CONTEXT_MESSAGE_TYPE,\n STATE_VERSION,\n} from '../../constants/autocompact';\nimport type {\n AutocompactContextDetails,\n AutocompactFileDetails,\n AutocompactRatioOverrides,\n AutocompactTokenOverrides,\n AutocompactMessage,\n AutocompactPass,\n AutocompactState,\n CheckpointDecision,\n CheckpointRequestDetails,\n ContextProjection,\n} from '../../types/autocompact';\n\n/** V4 projections have numeric timestamps and store custom messages as message entries. */\nexport type NativePolicyEntry = { id: string; timestamp: number } & (\n | { type: 'message'; message: AutocompactMessage }\n | { type: 'custom'; customType: string; data?: unknown }\n | { type: 'compaction'; summary: string; tokensBefore: number; retainedTail: AutocompactMessage[] }\n | { type: 'branch_summary'; summary: string; fromId?: string | null }\n);\nexport type CompactionPolicyEntry = SessionEntry | NativePolicyEntry;\n\nfunction customMessage(entry: CompactionPolicyEntry) {\n if (entry.type === 'custom_message') return entry;\n if (entry.type === 'message' && entry.message.role === 'custom') return entry.message;\n return undefined;\n}\n\n/** Native entries are already projected messages, not Pi journal entries. */\nexport function policyEntryMessages(entry: CompactionPolicyEntry): AutocompactMessage[] {\n if (typeof entry.timestamp === 'string') return sessionEntryToContextMessages(entry as SessionEntry);\n if (entry.type === 'message') return [entry.message];\n if (entry.type === 'compaction' && 'retainedTail' in entry) {\n return [\n {\n role: 'compactionSummary',\n summary: entry.summary,\n tokensBefore: entry.tokensBefore,\n timestamp: entry.timestamp,\n },\n ...entry.retainedTail,\n ];\n }\n if (entry.type === 'branch_summary') {\n return [{ role: 'branchSummary', summary: entry.summary, fromId: entry.fromId ?? '', timestamp: entry.timestamp }];\n }\n return [];\n}\n\nconst SHOULD_COMPACT_PATTERN = /<shouldCompact>\\s*(true|false)\\s*<\\/shouldCompact>/i;\nconst FILE_SECTION_PATTERN = /\\n*<(?:read-files|modified-files)>[\\s\\S]*?<\\/(?:read-files|modified-files)>/g;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nfunction isStringArray(value: unknown): value is string[] {\n return Array.isArray(value) && value.every((item) => typeof item === 'string');\n}\n\nfunction isPass(value: unknown): value is AutocompactPass {\n return value === 1 || value === 2 || value === 3;\n}\n\nfunction assistantText(entry: CompactionPolicyEntry): string | undefined {\n if (entry.type !== 'message' || entry.message.role !== 'assistant') return undefined;\n const text = entry.message.content\n .filter((block) => block.type === 'text')\n .map((block) => block.text)\n .join('\\n')\n .trim();\n return text || undefined;\n}\n\nfunction isAutocompactMessage(value: unknown): value is AutocompactMessage {\n return isRecord(value) && typeof value.role === 'string';\n}\n\nexport function contextDetailsFromUnknown(value: unknown): AutocompactContextDetails | undefined {\n if (!isRecord(value) || !isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return undefined;\n if (!Array.isArray(value.retainedMessages) || !value.retainedMessages.every(isAutocompactMessage)) return undefined;\n if (!isRecord(value.doomAutocompact)) return undefined;\n const marker = value.doomAutocompact;\n if (\n marker.version !== STATE_VERSION ||\n (marker.pass !== 2 && marker.pass !== 3) ||\n typeof marker.cycle !== 'number' ||\n typeof marker.requestId !== 'string' ||\n typeof marker.snapshotLeafId !== 'string' ||\n typeof marker.tokensBefore !== 'number'\n )\n return undefined;\n return {\n readFiles: [...value.readFiles],\n modifiedFiles: [...value.modifiedFiles],\n retainedMessages: [...value.retainedMessages],\n doomAutocompact: {\n version: STATE_VERSION,\n cycle: marker.cycle,\n pass: marker.pass,\n requestId: marker.requestId,\n snapshotLeafId: marker.snapshotLeafId,\n tokensBefore: marker.tokensBefore,\n },\n };\n}\n\nexport function createInitialState(): AutocompactState {\n return {\n version: STATE_VERSION,\n cycle: 1,\n pass: 1,\n phase: 'waiting',\n checkpointQueue: [],\n exhaustedPasses: [],\n invalidAttempts: 0,\n baselineTokens: 0,\n baselinePending: false,\n readFiles: [],\n modifiedFiles: [],\n };\n}\n\n/**\n * The ratios each pass fires at, with configured values folded in.\n *\n * The ladder only works if it climbs, so a configured pass never sits below the\n * one before it: a file that says pass 2 fires earlier than pass 1 gets pass 1's\n * ratio for pass 2 rather than a pass that can never contribute.\n */\nexport function compactionRatios(configured: AutocompactRatioOverrides = {}): Record<AutocompactPass, number> {\n let previous = 0;\n const ratios = {} as Record<AutocompactPass, number>;\n for (const pass of [1, 2, 3] as const) {\n const ratio = Math.max(configured[pass] ?? COMPACTION_THRESHOLDS[pass].ratio, previous);\n ratios[pass] = ratio;\n previous = ratio;\n }\n return ratios;\n}\n\nexport function thresholdTokens(\n pass: AutocompactPass,\n contextWindow: number,\n baselineTokens = 0,\n configured: AutocompactRatioOverrides = {},\n): number {\n const threshold = COMPACTION_THRESHOLDS[pass];\n const ratio = compactionRatios(configured)[pass];\n const baseline = Math.max(0, Math.min(baselineTokens, contextWindow));\n const remaining = contextWindow - baseline;\n const staged = baseline + Math.min(Math.floor(remaining * ratio), threshold.capTokens);\n // Pi compacts natively above `contextWindow - reserveTokens`. A staged threshold above that\n // point can never fire, so the ladder would silently degrade into native compaction: on a\n // 200k window the raw pass 3 ratio lands at 190_000, while Pi already compacted at 183_616.\n const nativeTrigger = contextWindow - DEFAULT_COMPACTION_SETTINGS.reserveTokens;\n return nativeTrigger > baseline ? Math.min(staged, nativeTrigger) : staged;\n}\n\n/**\n * Where every pass fires, with a matching model's absolute token checkpoints folded in.\n *\n * A configured token count replaces the ratio only when it fires earlier, so an\n * override can bring a checkpoint forward on a very large window without ever\n * pushing one past the point Pi compacts natively.\n *\n * Two rules keep the ladder usable. A token count at or below the baseline is\n * ignored, because a compaction that lands above it would otherwise satisfy that\n * pass the moment the next cycle opens and re-fire it every turn. And a pass never\n * sits below the one before it, for the same reason {@link compactionRatios}\n * climbs: `evaluateUsage` skips any pass below the one already reached, so a later\n * pass that fires first does not merely reorder the ladder, it locks the earlier\n * passes out of the cycle and forces compaction with no summary ever written.\n */\nexport function effectiveThresholds(\n contextWindow: number,\n baselineTokens = 0,\n configured: AutocompactRatioOverrides = {},\n tokenOverrides: AutocompactTokenOverrides = {},\n): Record<AutocompactPass, number> {\n let previous = 0;\n const thresholds = {} as Record<AutocompactPass, number>;\n for (const pass of [1, 2, 3] as const) {\n const ratioTokens = thresholdTokens(pass, contextWindow, baselineTokens, configured);\n const absolute = tokenOverrides[pass];\n const staged = absolute !== undefined && absolute > baselineTokens ? Math.min(ratioTokens, absolute) : ratioTokens;\n thresholds[pass] = Math.max(staged, previous);\n previous = thresholds[pass];\n }\n return thresholds;\n}\nexport function latestCheckpointArtifactEntry(\n branchEntries: readonly CompactionPolicyEntry[],\n): CompactionPolicyEntry | undefined {\n return branchEntries.findLast(\n (entry) =>\n entry.type === 'compaction' ||\n // Staged checkpoints are plain entries so they stay out of the agent's LLM context.\n (entry.type === 'custom' && entry.customType === CHECKPOINT_MESSAGE_TYPE) ||\n // Legacy sessions staged checkpoints as custom messages; committed markers still are.\n customMessage(entry)?.customType === CHECKPOINT_MESSAGE_TYPE ||\n customMessage(entry)?.customType === CONTEXT_MESSAGE_TYPE,\n );\n}\n\nexport function baselineUsageIsSettled(branchEntries: readonly CompactionPolicyEntry[]): boolean {\n const boundaryIndex = branchEntries.findLastIndex(\n (entry) => entry.type === 'compaction' || customMessage(entry)?.customType === CONTEXT_MESSAGE_TYPE,\n );\n return branchEntries.slice(boundaryIndex + 1).some((entry) => {\n if (entry.type !== 'message' || entry.message.role !== 'assistant') return false;\n const { stopReason, usage } = entry.message;\n if (stopReason === 'aborted' || stopReason === 'error' || !usage) return false;\n return (usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite) > 0;\n });\n}\n\nexport function retainedMessagesAfterSnapshot(\n branchEntries: readonly CompactionPolicyEntry[],\n snapshotLeafId: string,\n): AutocompactMessage[] {\n const snapshotIndex = branchEntries.findIndex((entry) => entry.id === snapshotLeafId);\n if (snapshotIndex === -1) return [];\n return branchEntries\n .slice(snapshotIndex + 1)\n .filter((entry) => customMessage(entry)?.customType !== CHECKPOINT_MESSAGE_TYPE)\n .flatMap(policyEntryMessages);\n}\n\nexport function projectContextMessages(\n messages: AutocompactMessage[],\n branchEntries: readonly CompactionPolicyEntry[],\n): ContextProjection {\n const nativeCompactionIndex = branchEntries.findLastIndex((entry) => entry.type === 'compaction');\n const markerEntry = branchEntries\n .slice(nativeCompactionIndex + 1)\n .findLast((entry) => customMessage(entry)?.customType === CONTEXT_MESSAGE_TYPE);\n const markerMessage = markerEntry && customMessage(markerEntry);\n if (!markerEntry || !markerMessage) {\n return { messages, retainedMessageCount: 0 };\n }\n\n const details = contextDetailsFromUnknown(markerMessage.details);\n const summary = checkpointSummaryFromEntry(markerEntry);\n if (!details || !summary) return { messages, invalidMarker: true, retainedMessageCount: 0 };\n\n const markerMessageIndex = messages.findLastIndex(\n (message) =>\n message.role === 'custom' &&\n message.customType === CONTEXT_MESSAGE_TYPE &&\n contextDetailsFromUnknown(message.details)?.doomAutocompact.requestId === details.doomAutocompact.requestId,\n );\n if (markerMessageIndex === -1) return { messages, invalidMarker: true, retainedMessageCount: 0 };\n\n const summaryMessage: AutocompactMessage = {\n role: 'compactionSummary',\n summary,\n tokensBefore: details.doomAutocompact.tokensBefore,\n timestamp: messages[markerMessageIndex]?.timestamp ?? Date.now(),\n };\n return {\n messages: [summaryMessage, ...details.retainedMessages, ...messages.slice(markerMessageIndex + 1)],\n marker: details.doomAutocompact,\n retainedMessageCount: details.retainedMessages.length,\n };\n}\n\nexport function fileOperationsFromMessages(messages: AutocompactMessage[]): FileOperations {\n const fileOps: FileOperations = { read: new Set(), written: new Set(), edited: new Set() };\n for (const message of messages) {\n if (message.role !== 'assistant') continue;\n for (const block of message.content) {\n if (block.type !== 'toolCall') continue;\n const path = typeof block.arguments.path === 'string' ? block.arguments.path : undefined;\n if (!path) continue;\n if (block.name === 'read') fileOps.read.add(path);\n else if (block.name === 'write') fileOps.written.add(path);\n else if (block.name === 'edit') fileOps.edited.add(path);\n }\n }\n return fileOps;\n}\n\nexport function parseState(value: unknown): AutocompactState | undefined {\n if (!isRecord(value)) return undefined;\n if (value.version !== STATE_VERSION || typeof value.cycle !== 'number' || !isPass(value.pass)) return undefined;\n if (\n value.phase !== 'waiting' &&\n value.phase !== 'checkpoint_pending' &&\n value.phase !== 'checkpoint_ready' &&\n value.phase !== 'compacting'\n )\n return undefined;\n const baselineTokens = value.baselineTokens ?? 0;\n if (typeof baselineTokens !== 'number' || baselineTokens < 0) return undefined;\n const baselinePending = value.baselinePending ?? false;\n if (typeof baselinePending !== 'boolean') return undefined;\n const checkpointQueue = value.checkpointQueue ?? [];\n if (\n !Array.isArray(checkpointQueue) ||\n !checkpointQueue.every(isPass) ||\n checkpointQueue.some((pass, index) => index > 0 && pass <= checkpointQueue[index - 1]!)\n )\n return undefined;\n // Sessions persisted before bounded retry landed simply have no attempt history to restore.\n const exhaustedPasses = value.exhaustedPasses ?? [];\n if (!Array.isArray(exhaustedPasses) || !exhaustedPasses.every(isPass)) return undefined;\n const invalidAttempts = value.invalidAttempts ?? 0;\n if (typeof invalidAttempts !== 'number' || invalidAttempts < 0) return undefined;\n if (!isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return undefined;\n if (value.requestId !== undefined && typeof value.requestId !== 'string') return undefined;\n if (value.compactionPass !== undefined && value.compactionPass !== 2 && value.compactionPass !== 3) return undefined;\n if ('latestCheckpointEntryId' in value) return undefined;\n if (value.latestCheckpointSnapshotLeafId !== undefined && typeof value.latestCheckpointSnapshotLeafId !== 'string')\n return undefined;\n if (value.lastAttemptLeafId !== undefined && typeof value.lastAttemptLeafId !== 'string') return undefined;\n if (value.snapshotLeafId !== undefined && typeof value.snapshotLeafId !== 'string') return undefined;\n if (value.snapshotTokens !== undefined && (typeof value.snapshotTokens !== 'number' || value.snapshotTokens < 0))\n return undefined;\n if (value.pendingCheckpoint !== undefined && typeof value.pendingCheckpoint !== 'string') return undefined;\n\n return {\n version: STATE_VERSION,\n cycle: value.cycle,\n pass: value.pass,\n phase: value.phase,\n checkpointQueue: [...checkpointQueue],\n exhaustedPasses: [...exhaustedPasses],\n invalidAttempts,\n baselineTokens,\n baselinePending,\n readFiles: [...value.readFiles],\n modifiedFiles: [...value.modifiedFiles],\n ...(value.requestId ? { requestId: value.requestId } : {}),\n ...(value.compactionPass ? { compactionPass: value.compactionPass } : {}),\n ...(value.latestCheckpointSnapshotLeafId\n ? { latestCheckpointSnapshotLeafId: value.latestCheckpointSnapshotLeafId }\n : {}),\n ...(value.lastAttemptLeafId ? { lastAttemptLeafId: value.lastAttemptLeafId } : {}),\n ...(value.snapshotLeafId ? { snapshotLeafId: value.snapshotLeafId } : {}),\n ...(value.snapshotTokens !== undefined ? { snapshotTokens: value.snapshotTokens } : {}),\n ...(value.pendingCheckpoint ? { pendingCheckpoint: value.pendingCheckpoint } : {}),\n };\n}\n\nexport function checkpointSummaryFromEntry(entry: CompactionPolicyEntry | undefined): string | undefined {\n if (!entry) return undefined;\n if (entry.type === 'compaction') return entry.summary.trim() || undefined;\n if (entry.type === 'custom') {\n return isRecord(entry.data) && typeof entry.data.summary === 'string'\n ? entry.data.summary.trim() || undefined\n : undefined;\n }\n const marker = customMessage(entry);\n if (marker) {\n const content =\n typeof marker.content === 'string'\n ? marker.content\n : marker.content\n .filter((block) => block.type === 'text')\n .map((block) => block.text)\n .join('\\n');\n return content.trim() || undefined;\n }\n return assistantText(entry);\n}\n\nexport function parseCheckpointDecision(response: string): CheckpointDecision {\n const match = response.match(SHOULD_COMPACT_PATTERN);\n const summary = response.replace(SHOULD_COMPACT_PATTERN, '').trim();\n if (!match) return { summary };\n return { summary, shouldCompact: match[1]?.toLowerCase() === 'true' };\n}\n\nexport function isStructuredCheckpoint(summary: string): boolean {\n return CHECKPOINT_HEADINGS.every((heading) => summary.includes(heading));\n}\n\nexport function createRequestId(\n sessionId: string,\n cycle: number,\n pass: AutocompactPass,\n leafId: string | null,\n): string {\n return `${sessionId}:${cycle}:${pass}:${leafId ?? 'root'}`;\n}\n\nexport function checkpointRequestDetails(\n cycle: number,\n pass: AutocompactPass,\n requestId: string,\n): CheckpointRequestDetails {\n return { version: STATE_VERSION, cycle, pass, requestId };\n}\n\nexport function mergeFileDetails(\n current: AutocompactFileDetails,\n fileOps?: FileOperations,\n additional?: AutocompactFileDetails,\n): AutocompactFileDetails {\n const modified = new Set([\n ...current.modifiedFiles,\n ...(additional?.modifiedFiles ?? []),\n ...(fileOps ? [...fileOps.edited, ...fileOps.written] : []),\n ]);\n const read = new Set([...current.readFiles, ...(additional?.readFiles ?? []), ...(fileOps ? [...fileOps.read] : [])]);\n for (const path of modified) read.delete(path);\n return { readFiles: [...read].sort(), modifiedFiles: [...modified].sort() };\n}\n\nexport function fileDetailsFromUnknown(value: unknown): AutocompactFileDetails | undefined {\n if (!isRecord(value) || !isStringArray(value.readFiles) || !isStringArray(value.modifiedFiles)) return undefined;\n return { readFiles: value.readFiles, modifiedFiles: value.modifiedFiles };\n}\n\nexport function withCanonicalFileSections(summary: string, details: AutocompactFileDetails): string {\n const sections: string[] = [];\n if (details.readFiles.length > 0) sections.push(`<read-files>\\n${details.readFiles.join('\\n')}\\n</read-files>`);\n if (details.modifiedFiles.length > 0) {\n sections.push(`<modified-files>\\n${details.modifiedFiles.join('\\n')}\\n</modified-files>`);\n }\n const normalized = summary.replace(FILE_SECTION_PATTERN, '').trim();\n return sections.length > 0 ? `${normalized}\\n\\n${sections.join('\\n\\n')}` : normalized;\n}\n"],"mappings":";;AAAA,MAAa,oBAAoB;AACjC,MAAa,0BAA0B;AACvC,MAAa,uBAAuB;AACpC,MAAa,sBAAsB;AACnC,MAAa,6BAA6B;AAM1C,MAAa,wBAAwB;CACnC,GAAG;EAAE,OAAO;EAAK,WAAW;CAAQ;CACpC,GAAG;EAAE,OAAO;EAAM,WAAW;CAAQ;CACrC,GAAG;EAAE,OAAO;EAAM,WAAW;CAAQ;AACvC;AAEA,MAAa,sBAAsB;CACjC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;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