acp-kernel 0.0.73 → 0.0.74-pr.306.172
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/dist/{chunk-37CVFHFQ.js → chunk-QPVKTDYF.js} +23 -1
- package/dist/chunk-QPVKTDYF.js.map +1 -0
- package/dist/compress.d.ts.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/decompress.d.ts.map +1 -1
- package/dist/hide-consumed.d.ts.map +1 -1
- package/dist/index.js +120 -58
- package/dist/index.js.map +1 -1
- package/dist/panel/index.js +3 -2
- package/dist/panel/index.js.map +1 -1
- package/dist/panel/topic.d.ts +0 -3
- package/dist/panel/topic.d.ts.map +1 -1
- package/dist/parse-compress-input.d.ts.map +1 -1
- package/dist/persist/index.js +2 -1
- package/dist/persist/index.js.map +1 -1
- package/dist/persist/state-merge.d.ts.map +1 -1
- package/dist/pipeline.d.ts +2 -0
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/report.d.ts.map +1 -1
- package/dist/search/index.d.ts.map +1 -1
- package/dist/sync.d.ts.map +1 -1
- package/dist/truncate-tools.d.ts +7 -0
- package/dist/truncate-tools.d.ts.map +1 -1
- package/dist/truncate.d.ts +5 -0
- package/dist/truncate.d.ts.map +1 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-37CVFHFQ.js.map +0 -1
|
@@ -390,12 +390,34 @@ function viableRanges(ranges) {
|
|
|
390
390
|
return ranges.filter((r) => r.tokens >= VIABLE_RANGE_MIN_TOKENS);
|
|
391
391
|
}
|
|
392
392
|
|
|
393
|
+
// src/truncate.ts
|
|
394
|
+
function isHighSurrogate(c) {
|
|
395
|
+
return c >= 55296 && c <= 56319;
|
|
396
|
+
}
|
|
397
|
+
function isLowSurrogate(c) {
|
|
398
|
+
return c >= 56320 && c <= 57343;
|
|
399
|
+
}
|
|
400
|
+
function clampPrefix(text, maxUnits) {
|
|
401
|
+
const cut = Math.min(maxUnits, text.length);
|
|
402
|
+
if (cut > 0 && isHighSurrogate(text.charCodeAt(cut - 1))) return text.slice(0, cut - 1);
|
|
403
|
+
return text.slice(0, cut);
|
|
404
|
+
}
|
|
405
|
+
function clampWindow(text, start, end) {
|
|
406
|
+
let s = Math.max(0, Math.min(start, text.length));
|
|
407
|
+
let e = Math.min(text.length, Math.max(s, end));
|
|
408
|
+
if (s > 0 && isLowSurrogate(text.charCodeAt(s))) s += 1;
|
|
409
|
+
if (e > s && isHighSurrogate(text.charCodeAt(e - 1))) e -= 1;
|
|
410
|
+
return text.slice(s, Math.max(s, e));
|
|
411
|
+
}
|
|
412
|
+
|
|
393
413
|
export {
|
|
394
414
|
defaultCountTokens,
|
|
395
415
|
thinkingTokenValue,
|
|
396
416
|
countMessageTokens,
|
|
397
417
|
estimateTokensFast,
|
|
398
418
|
createBpeTokenizer,
|
|
419
|
+
clampPrefix,
|
|
420
|
+
clampWindow,
|
|
399
421
|
COMPRESS_PHILOSOPHY,
|
|
400
422
|
HOW_TO_COMPRESS_RULES,
|
|
401
423
|
TIER2_DISTILL_RULES,
|
|
@@ -407,4 +429,4 @@ export {
|
|
|
407
429
|
VIABLE_RANGE_MIN_TOKENS,
|
|
408
430
|
viableRanges
|
|
409
431
|
};
|
|
410
|
-
//# sourceMappingURL=chunk-
|
|
432
|
+
//# sourceMappingURL=chunk-QPVKTDYF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/tokenize.ts","../src/compression-rules.ts","../src/prompts.ts","../src/nudge-text.ts","../src/viable.ts","../src/truncate.ts"],"sourcesContent":["import { createRequire } from \"node:module\";\n\nconst require = createRequire(import.meta.url);\n\nexport function defaultCountTokens(text: string): number {\n if (!text) return 0;\n // CJK chars tokenize ~1:1 (chars/4 badly underestimates them). Count them\n // directly, then estimate the non-CJK remainder with chars/4 so digits,\n // punctuation, and symbols in code/JSON are not dropped to zero.\n const cjk = text.match(/[\\u4e00-\\u9fff\\u3040-\\u30ff\\uac00-\\ud7af]/g);\n const cjkCount = cjk?.length ?? 0;\n return cjkCount + Math.ceil((text.length - cjkCount) / 4);\n}\n\nexport function estimateMessageTokens(text: string | undefined): number {\n return defaultCountTokens(text ?? \"\");\n}\n\n/** Guarded contribution of a host-projected thinking payload to the metered\n * message size. Non-finite or non-positive values are treated as absent (0),\n * so a bad host value can never poison the accounting. */\nexport function thinkingTokenValue(thinking: number | undefined): number {\n return typeof thinking === \"number\" &&\n Number.isFinite(thinking) &&\n thinking > 0\n ? thinking\n : 0;\n}\n\n/** Total metered size of a core message: visible text plus any host-projected\n * thinking payload (CoreMessage.thinkingTokens). Every per-message counting\n * site (range recommendations, block compressedTokens, status reports, context\n * breakdown) goes through this so all surfaces share one caliber. */\nexport function countMessageTokens(\n message: { text?: string; thinkingTokens?: number },\n countTokens: TokenCountFn = defaultCountTokens,\n): number {\n return (\n countTokens(message.text ?? \"\") + thinkingTokenValue(message.thinkingTokens)\n );\n}\n\nexport function estimateTokensFast(text: string): number {\n if (!text) return 0;\n return Math.ceil(text.length / 4);\n}\n\nexport type TokenCountFn = (text: string) => number;\n\nconst BPE_SIZE_GUARD = 100_000;\n\nexport function createBpeTokenizer(): TokenCountFn {\n try {\n const mod = require(\"@anthropic-ai/tokenizer\");\n const bpeCount = mod.countTokens ?? mod.default?.countTokens;\n if (typeof bpeCount !== \"function\") return defaultCountTokens;\n return (text: string) => {\n if (text.length > BPE_SIZE_GUARD) return defaultCountTokens(text);\n try {\n return bpeCount(text);\n } catch {\n return defaultCountTokens(text);\n }\n };\n } catch {\n return defaultCountTokens;\n }\n}\n","/**\n * Compression rule texts — VERBATIM copy from context-compress-algorithms (MIT, ours).\n * These were tuned over months of production use.\n *\n * 2026-09-06 amendment (owner-approved; billion-context-pi#309 incident): write-side\n * quote-fidelity rules merged INTO the tuned text. Verbatim user quotes now require\n * a message ref; recorded task state is labeled as history; unicode written directly.\n * Motivation: session 01a071dc blocks b60/b61 stored a fabricated\n * `user verbatim '合并了 下一个'` as a live \"CURRENT TASK\", causing loop relapses.\n */\n\nexport const COMPRESS_PHILOSOPHY = `Compression Philosophy:\n- All compression serves the primary task, but be frugal.\n- Context capacity is precious. Save context by compressing consumed outputs, not by avoiding tools.\n- Compress by need, not by percentage.\n- Work from summaries, not raw tool outputs. All listed ranges (user prompts, tool outputs, code, logs, exploration, intermediate steps) should be compressed to summary format — the ONLY exceptions are protected content, content the current step is actively using, or critical content you cannot reconstruct.`;\n\nexport const HOW_TO_COMPRESS_RULES = `HOW TO COMPRESS\n\nWhen you call \\`compress\\`, the summary you write becomes the only record of the replaced conversation. Make it self-contained and complete: every user request, experiment purpose, and work task in the range must be accurately captured. A later reader (or you, after decompressing) should be able to continue the task WITHOUT needing the original. The summary records the PAST as of this block's creation: label recorded task state as history (\"TASK AS OF THIS BLOCK: ...\") — never as a live instruction, so a later reader treats it as settled context, not something to re-execute. Write plain text with real unicode characters; never copy \\\\uXXXX escape sequences or JSON-escaped fragments out of tool output.\n\nKEEP VERBATIM — never paraphrase or abbreviate these:\n- Full file paths with line numbers, directory prefix on every mention (\\`lib/hooks.ts:347\\`, \\`src/index.ts:12-18\\`, \\`gatenet_v3/model.py:45\\`). Never abbreviate to a bare filename (\\`hooks.ts\\`, \\`model.py\\`) — they are ambiguous and cannot be grepped or decompressed-to later.\n- Function, class, and type signatures (exact names, params, return types) AND critical code lines that encode logic — the line that IS the finding, not just the function name (e.g. \\`kv_keys += define_gate * a_key[i](emb)\\` is more useful than \"see model_kvnet.py\").\n- Error messages and stack traces (exact text — you need the literal string to grep for it later).\n- Key details from reports and analyses — not just the conclusion. Keep the comparison numbers and the mechanism, not \"X is worse\" alone (write \"1.76× PPL gap because KV store is static\", not \"KVNet underperforms\").\n- Decisions and their rationale (\"chose X over Y because Z\" — the \"because\" is load-bearing; without it the decision looks arbitrary).\n- Constraints discovered (\"must support Node 22\", \"no new dependencies\", \"AGENTS.md forbids \\`as any\\`\").\n- Exact values: versions, config keys, thresholds, magic numbers.\n- User intent — quote short user messages verbatim ONLY WITH their message ref, e.g. \\`User said (m00132): \"ship it tonight\"\\`. Without a verifiable ref, paraphrase (\\`user previously asked (paraphrased): ...\\`) — this is the one exception to the verbatim rule above; never present a reconstructed or half-remembered phrase as a verbatim quote. When the message is too long to quote, preserve intent with extra care: do not change scope, constraints, priorities, acceptance criteria, or requested outcomes. Quotes are historical records, never current directives. Losing these changes the task itself.\n- The user's overall goal and any changes to it — the big-picture objective plus how it evolved during the compressed range. Each summary must reflect the goal as it stood at the end of the range, including pivots (e.g., \"initially: fix bug X → pivoted to: refactor module Y after discovering root cause\"). Losing the goal or its evolution makes all subsequent work appear unmotivated.\n- Purpose behind each significant action — preserve not just what was done but why: the hypothesis behind each experiment, the question behind each exploration, the task goal behind each work action. Without purpose, the summary reads as disconnected technical steps with no through-line.\n- Open questions and unresolved TODOs — losing these changes what work appears to remain.\n- Message refs of key anchors (\\`m00420\\`, \\`m00510–m00520\\`) — they let you or a later reader jump back via decompress to the exact original.\n\nDROP — extract the signal, discard the vessel:\n- Verbose logs (build/test/\\`npm\\` output) once you have captured the error line or the result.\n- Duplicate file reads once the needed content is recorded.\n- Consumed exploration — search hits, agent return values, successful tool outputs — once you have extracted the facts you need (same rule as dead-ends, but nothing went wrong; the content is simply spent).\n- Dead-end exploration — but PRESERVE the lesson in one line: \"tried X, failed because Y\".\n- Back-and-forth discussion and self-corrections once the final position is captured (keep the outcome, drop the journey to it).\n- Repeated status checks (\\`git status\\`, \\`ls\\`) once state is known.\n\nFor each significant item you DROP (scripts, reports, large analyses, long tool outputs), add a one-line CONTENT description of what it covers — not where it lives. Bad: \"probe script at /path/probe_kvnet.py\". Good: \"probe_kvnet.py: tests n-gram baseline, generation quality, long-range dependency, position sensitivity, op pipeline, QUERY attention.\" This lets a later decompress target the right block by relevance, not by guessing locations.\n\nPRIORITY — when the summary must be compact, preserve in this order:\n1. User's overall goal, goal evolution, intent, and hard constraints (losing these changes the task).\n2. Decisions and rationale.\n3. Exact technical artifacts: paths, signatures, errors, values.\n4. Conclusions and key findings.\n5. Lessons learned: what failed and why.\n\nWrite dense, scannable bullets — not narrative prose. If the range spans distinct concerns (request → findings → decision), group bullets under short thematic headers so a reader can scan to the part they need. Every line must earn its place. Do not mimic the style of existing summaries in context; follow these rules.`;\n\nexport const TIER2_DISTILL_RULES = `TIER 2 COMPRESSION — DISTILLATION\n\nYou are compressing historical summaries (not raw conversation). These summaries have already captured the details. Your job is to DISTILL them: extract only what matters for future work, discard the process.\n\nKEEP — these are the only things that survive distillation:\n- Decisions and their rationale (\"chose X over Y because Z\" — the \"because\" is load-bearing).\n- Final outcomes: version numbers shipped, PR numbers merged/closed, bugs fixed or deferred.\n- Key lessons: what failed and why (\"tried X, failed because Y\"). These prevent repeating mistakes.\n- Critical constraints discovered (\"must support Node 22\", \"AGENTS.md forbids as any\").\n- Design decisions with architectural impact (\"chose compress-as-anchor over synthetic messages because prefix cache\").\n- User quotes and task state only as attributed history: keep the source ref with any user quote; never carry a tier-1 \"CURRENT TASK\" claim forward as a live directive — relabel it \"TASK AS OF THIS BLOCK\".\n- Whether content is OBSOLETE or SUPERSEDED — mark with one line: \"[SUPERSEDED by PR #NNN]\" or \"[OBSOLETE: deleted in vX.Y.Z]\". Do NOT keep the obsolete content's details — just the marker and reason.\n- Function/class/type names and module paths that are the SUBJECT of the work — e.g., \"fixed filterCompressedRanges in prune.ts\", \"added SessionStateRegistry in state.ts\". Not exact line numbers or full signatures — just enough to LOCATE the code without searching.\n- Exploration findings: if a block was exploratory with no decision, keep the CONCLUSION in one line (\"explored X, not viable because Y\"). Do not keep the exploration process.\n\nDROP — these were useful during the work but are no longer needed:\n- Exact line numbers, diffs, verbose function signatures, full code listings.\n- Build/deploy process details, test execution steps.\n- Review process details (who reviewed, what rounds, test counts).\n- Verbose logs, command output, intermediate debugging steps.\n\nFORMAT:\n- Start each distilled block with a source header line:\n \\`Source: bN+bM+... (XK→YK tok, Zx). [original topic]\\`\n Example: \\`Source: b5+b7 (56K+44K→268 tok, 375x). [Tool-result recap + publish]\\`\n- 3-5 bullet points per source block, each a self-contained fact.\n- Dense, scannable — no narrative prose.\n- Start with the outcome, not the process: \"v1.13.0 shipped (7 PRs bundled)\" not \"implemented 7 PRs then reviewed then merged\".\n- Cross-block synthesis: if multiple source blocks cover the same topic (same PR, same feature, same bug), MERGE them into a single group of bullets. Do not repeat the same fact from different blocks — keep it once under the most relevant source header.\n\nSIZE TARGET: 50-150 tokens per source block (excluding the header). If you can't fit it in 150 tokens, you're keeping too much process. If a block has nothing worth keeping (pure noise), output just the header followed by \"[no actionable content].\"`;\n\nexport const TIER3_CONDENSE_RULES = `TIER 3 COMPRESSION — ULTRA-CONDENSATION\n\nYou are compressing distilled summaries (Tier 2) into ultra-condensed facts (Tier 3). The distilled summaries already contain only decisions and outcomes. Your job is to reduce them to bare factual references.\n\nPRIORITY — when a source block has more facts than the size target allows, keep in this order:\n1. Shipped outcomes (versions released, PRs merged) — these are permanent record.\n2. Open work (PRs/issues still pending) — these may need follow-up.\n3. Key decisions with architectural impact (\"chose X over Y because Z\").\n4. Critical constraints (\"must support Node 22\").\nDrop everything else. Tier 3 is a lookup index, not a knowledge base.\n\nFORMAT:\n- Start with a source header line:\n \\`Source: bN+bM+... (XK→YK tok, Zx). [original topic]\\`\n- Output 1-3 facts per source block. Each fact is a single line: subject + outcome.\n- No explanations, no rationale, no process — just the fact.\n- Format: \"[PR/Issue/Version] — [outcome in ≤8 words]\"\n- Merge related facts from different source blocks if they concern the same topic.\n\nEXAMPLES:\n- \"v1.13.0 shipped — quality gate + GC fix (7 PRs)\"\n- \"PR #196 merged — preserve-first-user (supersedes #169)\"\n- \"Bug 1214 fixed — compress consumed all user messages\"\n- \"Chose compress-as-anchor — prefix cache benefit over synthetic injection\"\n- \"Constraint: AGENTS.md forbids as any — never suppress types\"\n\nDROP:\n- Multi-sentence context. If a fact needs >1 sentence, it's too detailed for Tier 3.\n- Lessons learned (\"tried X, failed because Y\") — drop UNLESS the failure is likely to recur and the block is <30 days old.\n- Design rationale details — keep the decision, drop the \"because\" unless it's a critical constraint.\n- Anything marked [OBSOLETE] or [SUPERSEDED] — drop entirely, note \"[N blocks obsolete]\" in the summary.\n\nSIZE TARGET: 30-60 tokens per source block (including header). For a batch of N source blocks, total output ≈ N × 40 tokens. If a source block has only one trivial fact, output just the header + one line.`;\n","import {\n COMPRESS_PHILOSOPHY,\n HOW_TO_COMPRESS_RULES,\n TIER2_DISTILL_RULES,\n TIER3_CONDENSE_RULES,\n} from \"./compression-rules.js\";\n\n/**\n * Overridable prompt text consumed by the kernel's nudge renderer and, via the\n * adapter, the system prompt. Every field here is LOAD-BEARING: these rules\n * were tuned over months of production use and are quality-critical. Overriding\n * them can degrade summary quality (loss of paths / signatures / decisions →\n * broken retrieval), so {@link resolvePrompts} requires `{ acknowledgeRisk: true }`.\n *\n * Surface-level text (summary section headers, status-report chrome, tool\n * descriptions) is intentionally NOT part of this interface — it is owned by\n * the adapter or a later \"prompt-set format\" layer and is safe to customize\n * freely. See DESIGN.md for the load-bearing vs surface classification.\n */\nexport interface Prompts {\n /** Core compression philosophy. Embedded in the system prompt + every nudge. */\n compressPhilosophy: string;\n /** Rules the model follows when writing a tier-1 summary. */\n howToCompressRules: string;\n /** Rules for tier-2 distillation of existing summaries. */\n tier2DistillRules: string;\n /** Rules for tier-3 ultra-condensation of distilled summaries. */\n tier3CondenseRules: string;\n}\n\n/**\n * The kernel's canonical prompt values (verbatim from compression-rules.ts).\n * Frozen so a buggy caller cannot mutate the shared singleton and corrupt\n * every other consumer of {@link defaultPrompts}.\n */\nexport const defaultPrompts: Prompts = Object.freeze({\n compressPhilosophy: COMPRESS_PHILOSOPHY,\n howToCompressRules: HOW_TO_COMPRESS_RULES,\n tier2DistillRules: TIER2_DISTILL_RULES,\n tier3CondenseRules: TIER3_CONDENSE_RULES,\n}) as Prompts;\n\nexport interface ResolvePromptsOptions {\n /**\n * Must be `true` to override any prompt field. Every {@link Prompts} field is\n * load-bearing; overriding without acknowledging the quality risk is a\n * programming error and throws.\n */\n acknowledgeRisk?: boolean;\n}\n\n/**\n * Merge prompt overrides onto the kernel defaults. All fields are load-bearing,\n * so ANY override requires `{ acknowledgeRisk: true }`.\n *\n * Only `string`-valued overrides take effect: an explicit `undefined`/`null` or\n * a wrong type is silently dropped (never clobbers a good default), so a\n * malformed partial never degrades the canonical rules. Resolve once at host\n * startup, then pass the resulting {@link Prompts} to {@link renderNudgeText}\n * and to the adapter's system-prompt composition so both layers stay consistent.\n */\nexport function resolvePrompts(\n overrides?: Partial<Prompts>,\n options: ResolvePromptsOptions = {},\n): Prompts {\n const clean: Partial<Prompts> = {};\n if (overrides) {\n for (const [key, value] of Object.entries(overrides)) {\n if (typeof value === \"string\") {\n (clean as Record<string, unknown>)[key] = value;\n }\n }\n }\n const keys = Object.keys(clean) as (keyof Prompts)[];\n if (keys.length > 0 && !options.acknowledgeRisk) {\n throw new Error(\n `resolvePrompts: overriding compression rules requires { acknowledgeRisk: true }. ` +\n `Overridden keys: ${keys.join(\", \")}. These rules are quality-critical (tuned over months of production use); ` +\n `changing them can degrade summary quality and break retrieval (summaries may lose paths, signatures, decisions).`,\n );\n }\n return { ...defaultPrompts, ...clean };\n}\n","import type { NudgeDecision, CompressibleRange, ProtectedRange, ContextBreakdown, CompressionBlock, BlockSpan } from \"./types.js\";\nimport { defaultPrompts } from \"./prompts.js\";\nimport type { Prompts } from \"./prompts.js\";\n\nexport type NudgeVoice = \"gentle\" | \"emergency\";\n\nexport interface NudgePromptSections {\n efficiencyNote?: string | null;\n emergencyHeader?: string | null;\n t2Guidance?: string | null;\n t3Guidance?: string | null;\n}\n\nexport interface RenderedNudge {\n voice: NudgeVoice;\n text: string;\n}\n\nfunction efficiencyNote(prompts: Prompts, sections: NudgePromptSections): string | null {\n if (sections.efficiencyNote !== undefined) return sections.efficiencyNote;\n return `This is an efficiency nudge to compress early and keep context lean — not an overflow warning. A separate, stronger alert will appear if the context is actually full.\\n\\n${prompts.compressPhilosophy}`;\n}\n\nfunction emergencyHeader(prompts: Prompts, sections: NudgePromptSections): string | null {\n if (sections.emergencyHeader !== undefined) return sections.emergencyHeader;\n return `⚠️ Context limit reached — compress now. Prioritize consumed tool outputs.\\n\\n${prompts.compressPhilosophy}`;\n}\n\nfunction formatK(n: number): string {\n if (n >= 1000) return `${(n / 1000).toFixed(1)}K`;\n return `${n}`;\n}\n\nfunction formatBreakdown(bd?: ContextBreakdown): string {\n if (!bd) return \"\";\n const parts: string[] = [];\n if (bd.system > 0) parts.push(`${formatK(bd.system)} system`);\n if (bd.tool > 0) parts.push(`${formatK(bd.tool)} tool`);\n if (bd.summaries > 0) parts.push(`${formatK(bd.summaries)} summaries`);\n if (bd.code > 0) parts.push(`${formatK(bd.code)} code`);\n if (bd.text > 0) parts.push(`${formatK(bd.text)} text`);\n const growth = bd.growth > 0 ? `\\n+${formatK(bd.growth)} since last nudge` : \"\";\n return `Context breakdown: ${parts.join(\" | \")}${growth}`;\n}\n\n\n\nfunction formatTierTargetBlocks(blocks: CompressionBlock[]): string {\n if (blocks.length === 0) {\n return \"Target blocks: (none — no tier blocks found)\";\n }\n const lines = blocks.map((b) => {\n const summaryTokens = Math.ceil((b.summary ?? \"\").length / 4);\n const topic = b.topic ? ` \"${b.topic}\"` : \"\";\n return ` ${b.blockId} ${b.effectiveMessageIds.length} msgs ${formatK(b.compressedTokens)}→${formatK(summaryTokens)}${topic}`;\n });\n return `Target ${blocks[0]!.tier === 1 ? \"tier-1\" : \"tier-2\"} blocks to distill (${blocks.length}):\\n${lines.join(\"\\n\")}`;\n}\n\nconst BLOCK_MAP_MAX_SHOWN = 8;\n\nfunction formatBlockMap(spans: BlockSpan[]): string {\n if (spans.length === 0) return \"\";\n const hidden = Math.max(0, spans.length - BLOCK_MAP_MAX_SHOWN);\n const shown = hidden > 0 ? spans.slice(-BLOCK_MAP_MAX_SHOWN) : spans;\n const items = shown.map(\n (s) => `${s.blockId}=${s.startRef}–${s.endRef}${s.tier > 1 ? ` t${s.tier}` : \"\"}`,\n );\n const prefix = hidden > 0 ? `…+${hidden} older · ` : \"\";\n return `Active blocks (${spans.length}): ${prefix}${items.join(\" · \")}`;\n}\n\nexport function formatRanges(compressible: CompressibleRange[], protectedRanges: ProtectedRange[]): string {\n if (compressible.length === 0 && protectedRanges.length === 0) {\n return \"[No specific ranges detected — compress any consumed content.]\";\n }\n\n // Merge compressible + protected into a single oldest-first list, mirroring\n // opencode-acp's formatCompressibleRanges. Splitting them into two sections\n // lost the time order and hid overlaps; a range can be partly compressible\n // and partly protected, which only the merged view shows correctly.\n interface Merged {\n startRef: string; endRef: string; startNum: number; endNum: number;\n count: number; tokens: number; userMsgs: number;\n compressibleTokens: number; compressibleCount: number;\n protectedTokens: number; protectedCount: number; protectedTools: string[];\n toolPct: number; textPct: number; dangerous: boolean;\n }\n const refNum = (ref: string): number => {\n const m = ref.match(/\\d+/);\n return m ? parseInt(m[0], 10) : 0;\n };\n const entries: Merged[] = [];\n for (const r of compressible) {\n entries.push({\n startRef: r.startRef, endRef: r.endRef, startNum: refNum(r.startRef), endNum: refNum(r.endRef),\n count: r.count, tokens: r.tokens, userMsgs: r.userMsgs ?? 0, toolPct: r.toolPct, textPct: r.textPct,\n compressibleTokens: r.tokens, compressibleCount: r.count,\n protectedTokens: 0, protectedCount: 0, protectedTools: [], dangerous: r.dangerous ?? false,\n });\n }\n for (const r of protectedRanges) {\n entries.push({\n startRef: r.startRef, endRef: r.endRef, startNum: refNum(r.startRef), endNum: refNum(r.endRef),\n count: r.count, tokens: r.tokens, userMsgs: 0, toolPct: 0, textPct: 0,\n compressibleTokens: 0, compressibleCount: 0,\n protectedTokens: r.tokens, protectedCount: r.count, protectedTools: [...r.tools], dangerous: false,\n });\n }\n entries.sort((a, b) => a.startNum - b.startNum);\n // Merge adjacent/overlapping ranges (gap ≤ 1 ref).\n const merged: Merged[] = [];\n for (const e of entries) {\n const last = merged[merged.length - 1];\n if (last && e.startNum <= last.endNum + 1) {\n last.endRef = e.endRef;\n last.endNum = Math.max(last.endNum, e.endNum);\n last.count += e.count;\n last.tokens += e.tokens;\n last.userMsgs += e.userMsgs;\n last.compressibleTokens += e.compressibleTokens;\n last.compressibleCount += e.compressibleCount;\n last.protectedTokens += e.protectedTokens;\n last.protectedCount += e.protectedCount;\n if (e.dangerous) last.dangerous = true;\n for (const t of e.protectedTools) {\n if (!last.protectedTools.includes(t)) last.protectedTools.push(t);\n }\n } else {\n merged.push({ ...e });\n }\n }\n const userNote = (n: number): string =>\n n > 0 ? ` · ${n} user msg${n > 1 ? \"s\" : \"\"}` : \"\";\n const lines = merged.map((e) => {\n const suffix = e.dangerous && e.compressibleTokens > 0 ? \" ⚠️ NOT recommended unless you are certain.\" : \"\";\n if (e.protectedTokens > 0 && e.compressibleTokens === 0) {\n return ` ${e.startRef}–${e.endRef} ${e.count} msgs ${formatK(e.tokens)} [PROTECTED: ${e.protectedTools.join(\", \")} — not compressible]${suffix}`;\n }\n if (e.protectedTokens > 0 && e.compressibleTokens > 0) {\n return ` ${e.startRef}–${e.endRef} ${e.count} msgs ${formatK(e.tokens)} [${formatK(e.compressibleTokens)} compressible | ${formatK(e.protectedTokens)} protected: ${e.protectedTools.join(\", \")}]${userNote(e.userMsgs)}${suffix}`;\n }\n return ` ${e.startRef}–${e.endRef} ${e.count} msgs ${formatK(e.tokens)} [tool ${e.toolPct}% | text ${e.textPct}%]${userNote(e.userMsgs)}${suffix}`;\n });\n return `Compressible ranges (${merged.length}, oldest first):\\n${lines.join(\"\\n\")}`;\n}\n\nconst DEFAULT_T2_GUIDANCE = `Your tier-1 compression summaries have accumulated. Distill them into a single denser tier-2 summary. Use block IDs as boundaries (startId and endId as bN). Any raw (uncompressed) messages sitting between the boundary blocks are absorbed into the tier-2 block as well — apply HOW TO COMPRESS to those raw messages and the TIER 2 distillation rules to the existing summaries, so the whole span is covered and nothing is lost.`;\n\nconst DEFAULT_T3_GUIDANCE = `Your tier-2 compression summaries have accumulated. Condense them further into a tier-3 ultra-condensed summary. Use block IDs as boundaries (startId and endId as bN). Any raw (uncompressed) messages sitting between the boundary blocks are absorbed into the tier-3 block as well — apply HOW TO COMPRESS to those raw messages and the TIER 3 condensation rules to the existing summaries, so the whole span is covered and nothing is lost.`;\n\nfunction tierGuidance(tier: 2 | 3, sections: NudgePromptSections): string | null {\n const value = tier === 2 ? sections.t2Guidance : sections.t3Guidance;\n if (value !== undefined) return value;\n return tier === 2 ? DEFAULT_T2_GUIDANCE : DEFAULT_T3_GUIDANCE;\n}\n\nfunction compact(parts: string[]): string[] {\n while (parts.length > 0 && parts[0] === \"\") parts.shift();\n return parts;\n}\n\nexport function renderNudgeText(decision: NudgeDecision, prompts: Prompts = defaultPrompts, sections: NudgePromptSections = {}): RenderedNudge {\n const breakdownStr = formatBreakdown(decision.contextBreakdown);\n const rangesStr = formatRanges(decision.compressibleRanges, decision.protectedRanges ?? []);\n const blockMapStr = formatBlockMap(decision.activeBlockSpans ?? []);\n const isEmergency = !!decision.breakdown?.emergencyOverride || !!decision.breakdown?.overLimit;\n\n if (decision.tier !== null && decision.tier >= 2) {\n const isT2 = decision.tier === 2;\n const targets = decision.tierTargetBlocks ?? [];\n const blockList = formatTierTargetBlocks(targets);\n const startId = targets[0]?.blockId ?? \"b1\";\n const endId = targets[targets.length - 1]?.blockId ?? \"b5\";\n const voice: NudgeVoice = isEmergency ? \"emergency\" : \"gentle\";\n const triggerLine = isEmergency\n ? `[EMERGENCY — TIER ${decision.tier} ${isT2 ? \"DISTILLATION\" : \"CONDENSATION\"}] Context limit reached — distill NOW into a denser summary to reclaim tokens.`\n : `[TIER ${decision.tier} ${isT2 ? \"DISTILLATION\" : \"CONDENSATION\"} TRIGGER]`;\n const guidance = tierGuidance(isT2 ? 2 : 3, sections);\n const head = efficiencyNote(prompts, sections);\n return {\n voice,\n text: compact([\n ...(head === null ? [] : [head]),\n \"\",\n breakdownStr,\n \"\",\n triggerLine,\n ...(guidance === null ? [] : [guidance]),\n blockList,\n `Example: compress({ content: [{ startId: \"${startId}\", endId: \"${endId}\", summary: \"...\" }] })`,\n \"\",\n prompts.howToCompressRules,\n \"\",\n isT2 ? prompts.tier2DistillRules : prompts.tier3CondenseRules,\n ]).join(\"\\n\"),\n };\n }\n\n if (isEmergency) {\n const head = emergencyHeader(prompts, sections);\n return {\n voice: \"emergency\",\n text: compact([\n ...(head === null ? [] : [head]),\n \"\",\n breakdownStr,\n \"\",\n prompts.howToCompressRules,\n \"\",\n `{ \"topic\": \"...\", \"content\": [{ \"startId\": \"<ID>\", \"endId\": \"<ID>\", \"summary\": \"...\" }] }`,\n \"Only use IDs from visible messages above. Compress older work first.\",\n \"\",\n rangesStr,\n ...(blockMapStr ? [\"\", blockMapStr] : []),\n ]).join(\"\\n\"),\n };\n }\n\n const gentleHead = efficiencyNote(prompts, sections);\n return {\n voice: \"gentle\",\n text: compact([\n ...(gentleHead === null ? [] : [gentleHead]),\n \"\",\n breakdownStr,\n \"\",\n prompts.howToCompressRules,\n \"\",\n rangesStr,\n ...(blockMapStr ? [\"\", blockMapStr] : []),\n \"\",\n `💡 Compress all ranges in one call (pass multiple content entries: \\`content: [{...}, {...}]\\`).`,\n ]).join(\"\\n\"),\n };\n}\n","/** Minimum size for a compressible range to be worth recommending. Ranges\n * below this are fragmented leftovers (a 16-token ack, a one-line tool\n * result): the model cannot write a meaningful >=50-char summary for them,\n * and a batched compress call that includes one gets atomically rejected\n * (the kernel validates the whole batch). Observed in the wild: a 14-range\n * recommendation list containing a 16-token range → every batch attempt\n * failed with \"Summary too short\". Apply on every surface that recommends\n * ranges: the injected nudge, acp_status, and the /acp panel. */\nexport const VIABLE_RANGE_MIN_TOKENS = 200;\n\nexport function viableRanges<T extends { tokens: number }>(ranges: T[]): T[] {\n return ranges.filter((r) => r.tokens >= VIABLE_RANGE_MIN_TOKENS);\n}\n","// UTF-16-safe truncation. A plain slice can land between the two code units\n// of an astral character (emoji, CJK ext-B+), stranding a lone surrogate:\n// JSON.stringify escapes it (\\ud83e), and gateways that re-encode request\n// bodies to strict UTF-8 then throw UnicodeEncodeError on every retry\n// (ranxianglei/billion-context#816) — a deterministic poison that never heals.\n\nfunction isHighSurrogate(c: number): boolean {\n return c >= 0xd800 && c <= 0xdbff;\n}\n\nfunction isLowSurrogate(c: number): boolean {\n return c >= 0xdc00 && c <= 0xdfff;\n}\n\n/** At most maxUnits code units; never ends on a stranded high surrogate. */\nexport function clampPrefix(text: string, maxUnits: number): string {\n const cut = Math.min(maxUnits, text.length);\n if (cut > 0 && isHighSurrogate(text.charCodeAt(cut - 1))) return text.slice(0, cut - 1);\n return text.slice(0, cut);\n}\n\n/** A [start, end) window with both edges snapped off surrogate pairs. */\nexport function clampWindow(text: string, start: number, end: number): string {\n let s = Math.max(0, Math.min(start, text.length));\n let e = Math.min(text.length, Math.max(s, end));\n if (s > 0 && isLowSurrogate(text.charCodeAt(s))) s += 1;\n if (e > s && isHighSurrogate(text.charCodeAt(e - 1))) e -= 1;\n return text.slice(s, Math.max(s, e));\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAE9B,IAAMA,WAAU,cAAc,YAAY,GAAG;AAEtC,SAAS,mBAAmB,MAAsB;AACvD,MAAI,CAAC,KAAM,QAAO;AAIlB,QAAM,MAAM,KAAK,MAAM,4CAA4C;AACnE,QAAM,WAAW,KAAK,UAAU;AAChC,SAAO,WAAW,KAAK,MAAM,KAAK,SAAS,YAAY,CAAC;AAC1D;AASO,SAAS,mBAAmB,UAAsC;AACvE,SAAO,OAAO,aAAa,YACzB,OAAO,SAAS,QAAQ,KACxB,WAAW,IACT,WACA;AACN;AAMO,SAAS,mBACd,SACA,cAA4B,oBACpB;AACR,SACE,YAAY,QAAQ,QAAQ,EAAE,IAAI,mBAAmB,QAAQ,cAAc;AAE/E;AAEO,SAAS,mBAAmB,MAAsB;AACvD,MAAI,CAAC,KAAM,QAAO;AAClB,SAAO,KAAK,KAAK,KAAK,SAAS,CAAC;AAClC;AAIA,IAAM,iBAAiB;AAEhB,SAAS,qBAAmC;AAC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,MACA;AAAA,MACA;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA,GAAI,cAAc,CAAC,IAAI,WAAW,IAAI,CAAC;AAAA,MACvC;AAAA,MACA;AAAA,IACF,CAAC,EAAE,KAAK,IAAI;AAAA,EACd;AACF;;;ACnOO,IAAM,0BAA0B;AAEhC,SAAS,aAA2C,QAAkB;AAC3E,SAAO,OAAO,OAAO,CAAC,MAAM,EAAE,UAAU,uBAAuB;AACjE;;;ACNA,SAAS,gBAAgB,GAAoB;AACzC,SAAO,KAAK,SAAU,KAAK;AAC/B;AAEA,SAAS,eAAe,GAAoB;AACxC,SAAO,KAAK,SAAU,KAAK;AAC/B;AAGO,SAAS,YAAY,MAAc,UAA0B;AAChE,QAAM,MAAM,KAAK,IAAI,UAAU,KAAK,MAAM;AAC1C,MAAI,MAAM,KAAK,gBAAgB,KAAK,WAAW,MAAM,CAAC,CAAC,EAAG,QAAO,KAAK,MAAM,GAAG,MAAM,CAAC;AACtF,SAAO,KAAK,MAAM,GAAG,GAAG;AAC5B;AAGO,SAAS,YAAY,MAAc,OAAe,KAAqB;AAC1E,MAAI,IAAI,KAAK,IAAI,GAAG,KAAK,IAAI,OAAO,KAAK,MAAM,CAAC;AAChD,MAAI,IAAI,KAAK,IAAI,KAAK,QAAQ,KAAK,IAAI,GAAG,GAAG,CAAC;AAC9C,MAAI,IAAI,KAAK,eAAe,KAAK,WAAW,CAAC,CAAC,EAAG,MAAK;AACtD,MAAI,IAAI,KAAK,gBAAgB,KAAK,WAAW,IAAI,CAAC,CAAC,EAAG,MAAK;AAC3D,SAAO,KAAK,MAAM,GAAG,KAAK,IAAI,GAAG,CAAC,CAAC;AACvC;","names":["require","require"]}
|
package/dist/compress.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compress.d.ts","sourceRoot":"","sources":["../src/compress.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkC,kBAAkB,EAAE,MAAM,YAAY,C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U,CAAC,EAAE,cAAc,CAAC;CAC7B;AAED,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE;QACN,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,EAAE,CAAC;IACJ,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,KAAK,EAAE,gBAAgB,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,mBAAmB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CACnC;AA2DD,wBAAgB,UAAU,CAAC,KAAK,GAAE,KAAU,GAAG,eAAe,CA+U7D;AA0lCD,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,wBAAgB,aAAa,CAC3B,iBAAiB,EAAE,MAAM,EACzB,SAAS,GAAE,OAAO,CAAC,MAAM,CAAM,GAC9B,MAAM,CA+CR;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,wBAAgB,aAAa,CAC3B,iBAAiB,EAAE,MAAM,EACzB,SAAS,GAAE,OAAO,CAAC,MAAM,CAAM,GAC9B,MAAM,CA+CR;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CA8DvD"}
|
package/dist/decompress.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decompress.d.ts","sourceRoot":"","sources":["../src/decompress.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"decompress.d.ts","sourceRoot":"","sources":["../src/decompress.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAElF,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAO1D;AAED,wBAAgB,6BAA6B,CACzC,KAAK,EAAE,gBAAgB,EACvB,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,GACxB,gBAAgB,EAAE,CAUpB;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiB1F;AAED,MAAM,WAAW,iBAAiB;IAC9B,IAAI,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,wBAAgB,eAAe,CAC3B,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,MAAM,EAAE,EAClB,OAAO,GAAE,iBAAsB,GAChC,gBAAgB,CAkClB;AAED,MAAM,WAAW,qBAAqB;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACzB;AAED,wBAAgB,2BAA2B,CACvC,QAAQ,EAAE,WAAW,EAAE,EACvB,sBAAsB,EAAE,GAAG,CAAC,MAAM,CAAC,EACnC,KAAK,EAAE,gBAAgB,GACxB,qBAAqB,CA8BvB;AAED,MAAM,WAAW,sBAAsB;IACnC,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAC;IACb,6GAA6G;IAC7G,KAAK,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,qBAAqB;IAClC,kKAAkK;IAClK,IAAI,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,mBAAmB,CAC/B,KAAK,EAAE,gBAAgB,EACvB,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,WAAW,EAAE,EACvB,OAAO,GAAE,qBAA0B,GACpC,sBAAsB,CAsCxB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hide-consumed.d.ts","sourceRoot":"","sources":["../src/hide-consumed.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hide-consumed.d.ts","sourceRoot":"","sources":["../src/hide-consumed.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAYhE,MAAM,WAAW,kBAAkB;IAC/B,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;CAClB;AA8FD,wBAAgB,yBAAyB,CACrC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,WAAW,EAAE,GACxB,kBAAkB,CAwFpB"}
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,8 @@ import {
|
|
|
4
4
|
TIER2_DISTILL_RULES,
|
|
5
5
|
TIER3_CONDENSE_RULES,
|
|
6
6
|
VIABLE_RANGE_MIN_TOKENS,
|
|
7
|
+
clampPrefix,
|
|
8
|
+
clampWindow,
|
|
7
9
|
countMessageTokens,
|
|
8
10
|
createBpeTokenizer,
|
|
9
11
|
defaultCountTokens,
|
|
@@ -14,7 +16,7 @@ import {
|
|
|
14
16
|
resolvePrompts,
|
|
15
17
|
thinkingTokenValue,
|
|
16
18
|
viableRanges
|
|
17
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-QPVKTDYF.js";
|
|
18
20
|
import {
|
|
19
21
|
activeBlocks,
|
|
20
22
|
advanceSurvival,
|
|
@@ -296,6 +298,7 @@ function syncBlocks(messages, state) {
|
|
|
296
298
|
nudge: { ...state.nudge, anchors: { ...state.nudge.anchors } },
|
|
297
299
|
stats: { ...state.stats },
|
|
298
300
|
absorbed: (state.absorbed ?? []).map((record) => ({ ...record })),
|
|
301
|
+
terminalStreak: state.terminalStreak,
|
|
299
302
|
nextBlockId: state.nextBlockId,
|
|
300
303
|
nextRunId: state.nextRunId
|
|
301
304
|
};
|
|
@@ -353,7 +356,7 @@ function defaultConfig(modelContextLimit, overrides = {}) {
|
|
|
353
356
|
tier2GrowthMultiplier: 1.5
|
|
354
357
|
},
|
|
355
358
|
promotionThreshold: 5,
|
|
356
|
-
truncate: { threshold: 0.95 },
|
|
359
|
+
truncate: { threshold: 0.95, terminalEscapeAfter: 3 },
|
|
357
360
|
compress: {
|
|
358
361
|
minCompressRange: 5e3,
|
|
359
362
|
maxSummaryLength: 2e4,
|
|
@@ -405,6 +408,9 @@ function validateConfig(config) {
|
|
|
405
408
|
if (config.truncate.threshold <= 0 || config.truncate.threshold > 1) {
|
|
406
409
|
errors.push("truncate.threshold must be in (0, 1]");
|
|
407
410
|
}
|
|
411
|
+
if (config.truncate.terminalEscapeAfter !== void 0 && (!Number.isInteger(config.truncate.terminalEscapeAfter) || config.truncate.terminalEscapeAfter < 0)) {
|
|
412
|
+
errors.push("truncate.terminalEscapeAfter must be an integer >= 0");
|
|
413
|
+
}
|
|
408
414
|
for (const tier of [config.tiers.tier2Trigger, config.tiers.tier3Trigger]) {
|
|
409
415
|
if (tier < 1) errors.push("tier triggers must be >= 1");
|
|
410
416
|
}
|
|
@@ -654,47 +660,69 @@ var DEFAULTS = {
|
|
|
654
660
|
};
|
|
655
661
|
function truncateLargeToolOutputs(messages, tokenCount, config, countTokens, options = {}) {
|
|
656
662
|
const opts = { ...DEFAULTS, ...options };
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
const protectedIndex = messages.length - opts.protectRecentMessages;
|
|
661
|
-
const candidates = [];
|
|
662
|
-
for (let index = 0; index < messages.length; index++) {
|
|
663
|
-
if (index >= protectedIndex) break;
|
|
664
|
-
const message = messages[index];
|
|
665
|
-
if (message.contentType !== "tool-result") continue;
|
|
666
|
-
const text = message.text ?? "";
|
|
667
|
-
if (text.length === 0 || text.includes(TRUNCATION_MARKER)) continue;
|
|
668
|
-
const tokens = countTokens(text);
|
|
669
|
-
if (tokens < opts.minOutputTokens) continue;
|
|
670
|
-
candidates.push({ index, tokens });
|
|
663
|
+
const limit = config.modelContextLimit;
|
|
664
|
+
if (limit <= 0 || tokenCount < config.truncate.threshold * limit) {
|
|
665
|
+
return { messages, truncatedCount: 0, savedTokens: 0, candidatesFound: 0 };
|
|
671
666
|
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
667
|
+
const protectedIndex = messages.length - opts.protectRecentMessages;
|
|
668
|
+
const findCandidates = (predicate) => {
|
|
669
|
+
const found = [];
|
|
670
|
+
for (let i = 0; i < protectedIndex; i++) {
|
|
671
|
+
const message = messages[i];
|
|
672
|
+
if (!predicate(message)) continue;
|
|
673
|
+
const text = message.text ?? "";
|
|
674
|
+
if (text.length === 0 || text.includes(TRUNCATION_MARKER)) continue;
|
|
675
|
+
if (countTokens(text) < opts.minOutputTokens) continue;
|
|
676
|
+
found.push(message);
|
|
677
|
+
}
|
|
678
|
+
return found.sort(
|
|
679
|
+
(a, b) => countTokens(b.text ?? "") - countTokens(a.text ?? "")
|
|
680
|
+
);
|
|
681
|
+
};
|
|
682
|
+
const toolResults = findCandidates((m) => m.contentType === "tool-result");
|
|
683
|
+
const textMessages = options.includeTextMessages ? findCandidates(
|
|
684
|
+
(m) => m.contentType === "text" && (m.role === "user" || m.role === "assistant") && !isRenderedSummaryMessage(m) && !m.text?.startsWith(SUMMARY_HEADER)
|
|
685
|
+
) : [];
|
|
686
|
+
const candidatesFound = toolResults.length + textMessages.length;
|
|
677
687
|
let truncatedCount = 0;
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
const
|
|
684
|
-
|
|
688
|
+
let savedTokens = 0;
|
|
689
|
+
let remaining = tokenCount;
|
|
690
|
+
const targetTokens = config.truncate.threshold * limit * 0.9;
|
|
691
|
+
const replacements = /* @__PURE__ */ new Map();
|
|
692
|
+
const applyTo = (candidates) => {
|
|
693
|
+
for (const candidate of candidates) {
|
|
694
|
+
if (remaining <= targetTokens) break;
|
|
695
|
+
const original = candidate.text ?? "";
|
|
696
|
+
const tokens = countTokens(original);
|
|
697
|
+
if (original.length <= opts.keepPrefixChars + opts.keepSuffixChars) {
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
const prefix = clampPrefix(original, opts.keepPrefixChars);
|
|
701
|
+
const suffix = clampWindow(original, original.length - opts.keepSuffixChars, original.length);
|
|
702
|
+
const replacement = `${prefix}
|
|
685
703
|
|
|
686
|
-
...${TRUNCATION_MARKER} \u2014 original ~${
|
|
704
|
+
...${TRUNCATION_MARKER} \u2014 original ~${tokens} tokens]...
|
|
687
705
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
);
|
|
697
|
-
|
|
706
|
+
${suffix}`;
|
|
707
|
+
replacements.set(candidate.id, replacement);
|
|
708
|
+
truncatedCount++;
|
|
709
|
+
remaining -= tokens - countTokens(replacement);
|
|
710
|
+
savedTokens += tokens - countTokens(replacement);
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
applyTo(toolResults);
|
|
714
|
+
applyTo(textMessages);
|
|
715
|
+
if (replacements.size === 0) {
|
|
716
|
+
return { messages, truncatedCount: 0, savedTokens: 0, candidatesFound };
|
|
717
|
+
}
|
|
718
|
+
return {
|
|
719
|
+
messages: messages.map(
|
|
720
|
+
(m) => replacements.has(m.id) ? { ...m, text: replacements.get(m.id) } : m
|
|
721
|
+
),
|
|
722
|
+
truncatedCount,
|
|
723
|
+
savedTokens,
|
|
724
|
+
candidatesFound
|
|
725
|
+
};
|
|
698
726
|
}
|
|
699
727
|
|
|
700
728
|
// src/hide-consumed.ts
|
|
@@ -749,7 +777,7 @@ function compactEntry(entry) {
|
|
|
749
777
|
if (!entry || typeof entry !== "object") return entry;
|
|
750
778
|
const e = entry;
|
|
751
779
|
if (typeof e.summary !== "string" || e.summary.length <= SUMMARY_STUB_CHARS) return entry;
|
|
752
|
-
return { ...e, summary: `${e.summary
|
|
780
|
+
return { ...e, summary: `${clampPrefix(e.summary, SUMMARY_STUB_CHARS - 1)}\u2026` };
|
|
753
781
|
}
|
|
754
782
|
function serializeCompacted(obj, content, contentWasString) {
|
|
755
783
|
let changed = false;
|
|
@@ -2323,6 +2351,7 @@ function createCore(ports = {}) {
|
|
|
2323
2351
|
state.nudge.lastPerMessageNudgeTokens = 0;
|
|
2324
2352
|
state.nudge.lastNudgeShownTokens = 0;
|
|
2325
2353
|
state.nudge.lastShownByTier = {};
|
|
2354
|
+
state.terminalStreak = 0;
|
|
2326
2355
|
}
|
|
2327
2356
|
return {
|
|
2328
2357
|
state,
|
|
@@ -2352,7 +2381,9 @@ function createCore(ports = {}) {
|
|
|
2352
2381
|
return {
|
|
2353
2382
|
messages: result.messages,
|
|
2354
2383
|
state: result.state,
|
|
2355
|
-
nudge: result.effects.nudge
|
|
2384
|
+
nudge: result.effects.nudge,
|
|
2385
|
+
terminalEscape: result.effects.terminalEscape,
|
|
2386
|
+
truncationSkipped: result.effects.truncationSkipped
|
|
2356
2387
|
};
|
|
2357
2388
|
}
|
|
2358
2389
|
function decompress(blockId, state) {
|
|
@@ -2545,18 +2576,49 @@ var emergencyTruncateNode = {
|
|
|
2545
2576
|
name: "emergency-truncate",
|
|
2546
2577
|
run(io, ctx) {
|
|
2547
2578
|
const usage = ctx.config.modelContextLimit > 0 ? ctx.tokenCount / ctx.config.modelContextLimit : 0;
|
|
2548
|
-
|
|
2579
|
+
const prevStreak = io.state.terminalStreak ?? 0;
|
|
2580
|
+
if (usage < ctx.config.truncate.threshold) {
|
|
2581
|
+
return prevStreak > 0 ? { ...io, state: { ...io.state, terminalStreak: 0 } } : io;
|
|
2582
|
+
}
|
|
2549
2583
|
const trunc = truncateLargeToolOutputs(
|
|
2550
2584
|
io.messages,
|
|
2551
2585
|
ctx.tokenCount,
|
|
2552
2586
|
ctx.config,
|
|
2553
2587
|
ctx.countTokens,
|
|
2554
|
-
{
|
|
2588
|
+
{
|
|
2589
|
+
protectRecentMessages: ctx.config.preserveRecentMessages,
|
|
2590
|
+
includeTextMessages: true
|
|
2591
|
+
}
|
|
2555
2592
|
);
|
|
2593
|
+
const nudge = io.effects.nudge;
|
|
2594
|
+
const minBenefit = nudge?.breakdown.minPressureBenefit ?? 0;
|
|
2595
|
+
const maxPending = nudge?.breakdown.maxPending ?? 0;
|
|
2596
|
+
const noViableCompression = nudge !== void 0 && (minBenefit > 0 ? maxPending < minBenefit : maxPending <= 0);
|
|
2597
|
+
const stuck = noViableCompression && trunc.savedTokens <= 0;
|
|
2598
|
+
const streak = stuck ? prevStreak + 1 : 0;
|
|
2599
|
+
const escapeAfter = ctx.config.truncate.terminalEscapeAfter ?? 3;
|
|
2600
|
+
const triggered = stuck && escapeAfter > 0 && streak >= escapeAfter;
|
|
2601
|
+
const effects = {
|
|
2602
|
+
...io.effects,
|
|
2603
|
+
truncatedCount: trunc.truncatedCount
|
|
2604
|
+
};
|
|
2605
|
+
if (trunc.savedTokens <= 0) {
|
|
2606
|
+
effects.truncationSkipped = trunc.candidatesFound === 0 ? `emergency-truncate ran at ${Math.round(usage * 100)}% usage but found no truncatable content (no oversized tool-result or text message outside the last ${ctx.config.preserveRecentMessages} messages)` : `emergency-truncate found ${trunc.candidatesFound} candidate(s) at ${Math.round(usage * 100)}% usage but none were large enough to save tokens`;
|
|
2607
|
+
}
|
|
2608
|
+
if (triggered) {
|
|
2609
|
+
effects.terminalEscape = {
|
|
2610
|
+
message: `Usage at ${Math.round(usage * 100)}% (${ctx.tokenCount}/${ctx.config.modelContextLimit} tokens) persists with nothing compressible above the benefit floor and no truncatable content: compression cannot reduce this context below the limit. Start a new session or use native compaction.`,
|
|
2611
|
+
usage,
|
|
2612
|
+
tokenCount: ctx.tokenCount,
|
|
2613
|
+
modelContextLimit: ctx.config.modelContextLimit,
|
|
2614
|
+
stuckEvents: streak
|
|
2615
|
+
};
|
|
2616
|
+
}
|
|
2556
2617
|
return {
|
|
2557
2618
|
...io,
|
|
2558
2619
|
messages: trunc.messages,
|
|
2559
|
-
|
|
2620
|
+
state: { ...io.state, terminalStreak: streak },
|
|
2621
|
+
effects
|
|
2560
2622
|
};
|
|
2561
2623
|
}
|
|
2562
2624
|
};
|
|
@@ -2649,7 +2711,8 @@ function applySingleRange(input) {
|
|
|
2649
2711
|
for (const m of input.messages) {
|
|
2650
2712
|
if (!m.id) continue;
|
|
2651
2713
|
if (m.contentType === "reasoning") reasoningIds.add(m.id);
|
|
2652
|
-
if (m.role === "assistant" && m.contentType === "tool-call")
|
|
2714
|
+
if (m.role === "assistant" && m.contentType === "tool-call")
|
|
2715
|
+
callIds.add(m.id);
|
|
2653
2716
|
}
|
|
2654
2717
|
const withdrawIds = /* @__PURE__ */ new Set();
|
|
2655
2718
|
let splitTurnCount = 0;
|
|
@@ -2920,6 +2983,7 @@ function decideNudge(input) {
|
|
|
2920
2983
|
const t1Eff = tiers[1]?.pending ?? 0;
|
|
2921
2984
|
const t2Pen = tiers[2]?.pending ?? 0;
|
|
2922
2985
|
const t3Pen = tiers[3]?.pending ?? 0;
|
|
2986
|
+
const maxPending = Math.max(0, t1Eff, t2Pen, t3Pen);
|
|
2923
2987
|
const firstSightMassReady = state.nudge.lastNudgeShownTokens === 0 && baseline === 0 && usage >= config.nudge.minContextLimitPct && Math.max(t1Eff, t2Pen, t3Pen) >= nudgeGrowthTokens;
|
|
2924
2988
|
const growthReady = firstSightMassReady || growthSinceReference >= growthFloor;
|
|
2925
2989
|
const t2Count = tiers[2]?.targetBlocks.length ?? 0;
|
|
@@ -2992,10 +3056,6 @@ function decideNudge(input) {
|
|
|
2992
3056
|
(t) => ((tiers[t]?.pending ?? 0) >= nudgeGrowthTokens || countReady(t)) && (state.nudge.lastShownByTier[t] ?? 0) > 0 && tokenCount - (state.nudge.lastShownByTier[t] ?? 0) < growthFloor
|
|
2993
3057
|
).map((t) => `T${t} (cadence)`);
|
|
2994
3058
|
const blockedHint = blocked.length > 0 ? `, blocked: ${blocked.join(", ")}` : "";
|
|
2995
|
-
const maxPending = Math.max(
|
|
2996
|
-
0,
|
|
2997
|
-
...Object.values(tiers).map((t) => t.pending)
|
|
2998
|
-
);
|
|
2999
3059
|
const pendingShort = maxPending < nudgeGrowthTokens;
|
|
3000
3060
|
const growthShort = growthSinceReference < growthFloor;
|
|
3001
3061
|
const parts = [];
|
|
@@ -3039,7 +3099,8 @@ function decideNudge(input) {
|
|
|
3039
3099
|
minPressureBenefit,
|
|
3040
3100
|
pendingT1: tiers[1].pending,
|
|
3041
3101
|
pendingT2: tiers[2].pending,
|
|
3042
|
-
pendingT3: tiers[3].pending
|
|
3102
|
+
pendingT3: tiers[3].pending,
|
|
3103
|
+
maxPending
|
|
3043
3104
|
},
|
|
3044
3105
|
contextBreakdown: ctxBreakdown
|
|
3045
3106
|
};
|
|
@@ -3079,6 +3140,7 @@ function cloneState(state) {
|
|
|
3079
3140
|
nudge: { ...state.nudge, anchors: { ...state.nudge.anchors } },
|
|
3080
3141
|
stats: { ...state.stats },
|
|
3081
3142
|
absorbed: (state.absorbed ?? []).map((record) => ({ ...record })),
|
|
3143
|
+
terminalStreak: state.terminalStreak,
|
|
3082
3144
|
nextBlockId: state.nextBlockId,
|
|
3083
3145
|
nextRunId: state.nextRunId
|
|
3084
3146
|
};
|
|
@@ -3437,7 +3499,7 @@ function buildRestoredContentPreview(messages, beforeActiveMessageIds, state) {
|
|
|
3437
3499
|
for (const message of restored) {
|
|
3438
3500
|
if (totalLength >= MAX_PREVIEW) break;
|
|
3439
3501
|
const text = message.text ?? "";
|
|
3440
|
-
const truncated = text.length > MAX_PER_MESSAGE ? text
|
|
3502
|
+
const truncated = text.length > MAX_PER_MESSAGE ? clampPrefix(text, MAX_PER_MESSAGE) + "..." : text;
|
|
3441
3503
|
const label = message.toolName && message.contentType !== "text" ? `${message.toolName}: ${truncated}` : `[${message.role}] ${truncated}`;
|
|
3442
3504
|
lines.push(label);
|
|
3443
3505
|
totalLength += label.length + 1;
|
|
@@ -3765,7 +3827,7 @@ ${block.summary}
|
|
|
3765
3827
|
const lines = [`Active compression blocks (${activeBlocks2.length}):`];
|
|
3766
3828
|
for (const block of activeBlocks2) {
|
|
3767
3829
|
const range = `${block.effectiveMessageIds.length} messages`;
|
|
3768
|
-
const preview = block.summary
|
|
3830
|
+
const preview = clampPrefix(block.summary, 200);
|
|
3769
3831
|
lines.push(`
|
|
3770
3832
|
${block.blockId} | ${range} | "${block.topic ?? "(none)"}"`);
|
|
3771
3833
|
lines.push(` ${preview}${block.summary.length > 200 ? "..." : ""}`);
|
|
@@ -3863,7 +3925,7 @@ function parseCompressArgs(input, opts) {
|
|
|
3863
3925
|
return parseObjectValue(input, callId, diag);
|
|
3864
3926
|
}
|
|
3865
3927
|
function parseStringInput(raw, callId, diag) {
|
|
3866
|
-
diag.rawPrefix = raw
|
|
3928
|
+
diag.rawPrefix = clampPrefix(raw, 800);
|
|
3867
3929
|
diag.length = raw.length;
|
|
3868
3930
|
const cleaned = stripFence(raw.trim());
|
|
3869
3931
|
const first = parseStringCore(cleaned, callId, diag);
|
|
@@ -4047,7 +4109,7 @@ function deriveTopicFromSummary(summary) {
|
|
|
4047
4109
|
const source = heading !== null ? heading[1] : summary.split("\n").find((l) => l.trim().length > 0) ?? "";
|
|
4048
4110
|
const text = source.trim().replace(/^[\u2022\-*]\s+/, "");
|
|
4049
4111
|
if (text.length === 0) return void 0;
|
|
4050
|
-
return text.length > 60 ? text
|
|
4112
|
+
return text.length > 60 ? clampPrefix(text, 60).trimEnd() : text;
|
|
4051
4113
|
}
|
|
4052
4114
|
function splitLineEntries(content) {
|
|
4053
4115
|
const parts = content.split(/\n(?=\s*(?:["']\s*,\s*)?["']?\s*[mb]\d{1,7}\s*(?:[-\u2013\u2014\u2026~]|\.\.\.|to)\s*[mb]\d{1,7}\b)/i).map((p) => p.trim().replace(/^(?:["']\s*,?\s*)+/, "").replace(/["']\s*$/, "")).filter((p) => p.length > 0);
|
|
@@ -4631,7 +4693,7 @@ function messageDocs(msgs) {
|
|
|
4631
4693
|
kind: "message",
|
|
4632
4694
|
ref: m.ref,
|
|
4633
4695
|
text: m.text,
|
|
4634
|
-
title: `${m.role}: ${m.text
|
|
4696
|
+
title: `${m.role}: ${clampPrefix(m.text, 60)}`,
|
|
4635
4697
|
role: m.role,
|
|
4636
4698
|
blockId: m.blockId,
|
|
4637
4699
|
tier: m.tier,
|
|
@@ -4696,7 +4758,7 @@ async function searchBlocksAsync(docs, query, options = {}) {
|
|
|
4696
4758
|
function makePreview(text, query, len) {
|
|
4697
4759
|
if (!text) return "";
|
|
4698
4760
|
const terms = query.toLowerCase().trim().split(/\s+/).filter((t) => t.length > 1);
|
|
4699
|
-
if (terms.length === 0) return text
|
|
4761
|
+
if (terms.length === 0) return clampPrefix(text, len);
|
|
4700
4762
|
const lower = text.toLowerCase();
|
|
4701
4763
|
let hitIdx = -1;
|
|
4702
4764
|
for (const term of terms) {
|
|
@@ -4706,13 +4768,13 @@ function makePreview(text, query, len) {
|
|
|
4706
4768
|
break;
|
|
4707
4769
|
}
|
|
4708
4770
|
}
|
|
4709
|
-
if (hitIdx < 0) return text
|
|
4771
|
+
if (hitIdx < 0) return clampPrefix(text, len);
|
|
4710
4772
|
const half = Math.max(0, Math.floor(len / 2) - 10);
|
|
4711
4773
|
const start = Math.max(0, hitIdx - half);
|
|
4712
4774
|
const end = Math.min(text.length, start + len);
|
|
4713
4775
|
const prefix = start > 0 ? "\u2026" : "";
|
|
4714
4776
|
const suffix = end < text.length ? "\u2026" : "";
|
|
4715
|
-
return prefix + text
|
|
4777
|
+
return prefix + clampWindow(text, start, end).trim() + suffix;
|
|
4716
4778
|
}
|
|
4717
4779
|
export {
|
|
4718
4780
|
ABSORB_PROMPT_MARKER,
|