@sliday/tamp 0.8.20 → 0.9.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.
@@ -0,0 +1,216 @@
1
+ // px-render stage: render large DENSE tool_results as PNG image blocks.
2
+ //
3
+ // Borrowed from teamchong/pxpipe (MIT): image input tokens are priced by pixel
4
+ // area (⌈w/28⌉×⌈h/28⌉ patches on Anthropic), not text volume, so content that tokenizes
5
+ // badly as text (paths, hashes, hex, grep output at ~1-2 chars/token) packs
6
+ // ~3x more chars per token as a rendered image. Prose tokenizes fine (~3.5+
7
+ // chars/token) and LOSES money imaged — the density gate below excludes it.
8
+ //
9
+ // Model-gated: vision legibility varies wildly per model (pxpipe FINDINGS:
10
+ // fable-5 reads dense hex 13/15, opus-tier 2/15), so the allowlist defaults to
11
+ // the models measured legible, not the expensive ones per se. Reading images
12
+ // is lossy-in-fidelity: a factsheet of precision-critical tokens rides beside
13
+ // the images as plain text so exact identifiers never depend on glyph reading.
14
+
15
+ import { countTokens } from '@anthropic-ai/tokenizer'
16
+
17
+ // Anthropic vision pricing: one token per 28×28-px patch, ⌈w/28⌉×⌈h/28⌉ per
18
+ // image (pxpipe 0.16 src/core/vision-cost.ts patchTokens; pixels/750 was the
19
+ // older continuous approximation). Fallback when the exported estimator is absent.
20
+ const PATCH_PX = 28
21
+ // Post-render profitability margin: images + factsheet must beat text by 20%.
22
+ const PROFIT_MARGIN = 0.8
23
+ // Density pre-gate: chars-per-text-token at or above this reads as clean
24
+ // prose — the content class with the worst measured image-legibility risk and
25
+ // the least token upside. This gate is a cheap CPU skip only; the exact
26
+ // profitability decision is the post-render check below (pixel area is known
27
+ // then). Grep/code/log content sits at ~2.5-3.5 chars/token and must pass.
28
+ const MAX_DENSITY = 4.0
29
+
30
+ let pxpipeModule // memoized dynamic import; null = tried and missing
31
+ async function loadPxpipe() {
32
+ if (pxpipeModule !== undefined) return pxpipeModule
33
+ try {
34
+ pxpipeModule = await import('pxpipe-proxy')
35
+ } catch {
36
+ pxpipeModule = null
37
+ }
38
+ return pxpipeModule
39
+ }
40
+
41
+ // Bracketed variant tags ([1m] etc.) stripped so base and variant gate identically.
42
+ export function pxModelAllowed(model, allowCsv) {
43
+ if (typeof model !== 'string' || !allowCsv) return false
44
+ const base = model.replace(/\[[^\]]*\]/g, '')
45
+ return allowCsv.split(',').map(s => s.trim()).filter(Boolean)
46
+ .some(b => base === b || base.startsWith(`${b}-`))
47
+ }
48
+
49
+ // --- Lean factsheet (adapted from pxpipe src/core/factsheet.ts, MIT) ---
50
+ // Extracts precision-critical tokens (paths, SHAs, versions, flags, numbers,
51
+ // CONST_IDS) so they ride next to the images as verbatim text. Deterministic
52
+ // by construction (fixed pattern order, total sort, no Date/random) so the
53
+ // emitted line is byte-stable and never busts the prompt cache.
54
+ // Synced with pxpipe 0.16 (LABEL=value, field=value, email, IBAN, currency,
55
+ // dir path, CLI flag, decimal, camelCase, ticket codes). Every pattern must
56
+ // stay linear-time on a whole 200k-char string, not just a 512-char chunk:
57
+ // the email and path patterns start only at the head of a run (lookbehind)
58
+ // instead of retrying at every offset, and the ticket pattern checks its digit
59
+ // after matching instead of with upstream's 120-char lookahead.
60
+ const FS_TICKET = /\b[A-Z][A-Z0-9]+(?:-[A-Z0-9]+)+\b/g
61
+ export const FS_PATTERNS = [
62
+ /\b[A-Z][A-Z0-9_]{2,}=[^\s)"'<>]+/g,
63
+ /\b[A-Za-z][A-Za-z0-9_]{2,}=[A-Za-z0-9_.:/+-]{1,64}/g,
64
+ /\bhttps?:\/\/[^\s)"'<>]+/g,
65
+ /(?<![\w.!#$%&'*+/=?^`{|}~-])[\w.!#$%&'*+/=?^`{|}~-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+\b/g,
66
+ /\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b/g,
67
+ /\b[A-Z]{2}\d{2}[A-Z0-9]{8,30}\b/g,
68
+ /(?:[$€£¥]|USD|EUR|GBP|CAD|AUD|CHF|JPY)\d(?:[\d,_]*\d)?(?:\.\d{2})?\b/g,
69
+ /(?<![\w@~+./-])[\w@~+.-]*(?:\/[\w.@+-]+)+\.[A-Za-z]\w{0,8}\b/g,
70
+ /\/[\w.@+-]+(?:\/[\w.@+-]+)+\/?/g,
71
+ /\b(?=[0-9a-f]*\d)[0-9a-f]{7,40}\b/g,
72
+ /\bv?\d+\.\d+(?:\.\d+)?(?:[-+][\w.]+)?\b/g,
73
+ /(?:^|[^\w-])(--?[A-Za-z][\w-]+)/g,
74
+ /\b\d[\d,_]{3,}\b/g,
75
+ /\b\d+\.\d+\b/g,
76
+ /\b[A-Z][A-Z0-9]{2,}(?:_[A-Z0-9]+)+\b/g,
77
+ /\b(?:[a-z]+|[A-Z][a-z0-9]+)(?:[A-Z][a-z0-9]*)+\b/g,
78
+ FS_TICKET,
79
+ ]
80
+ const FS_MAX_TOKENS = 96
81
+ const FS_MAX_URLS = 8
82
+ const FS_MAX_SCAN = 262_144
83
+ const FS_URL = /^https?:\/\//
84
+ // Short opaque identifiers (SHAs, UUIDs, numbers, CONST_IDS) outrank long URLs:
85
+ // length is anti-correlated with OCR-risk × consequence.
86
+ const FS_TIER0 = /^(?:(?=[0-9a-f]*\d)[0-9a-f]{7,40}|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}|[A-Z][A-Z0-9]{2,}(?:_[A-Z0-9]+)+|\d[\d,_]*|\d+\.\d+)$/
87
+ // pxpipe 0.16 tier-0 additions. Lowercase field=value stays tier 1: log lines
88
+ // carry one per row and would flood the top tier.
89
+ const FS_TIER0_SHAPES = [
90
+ /^[A-Z][A-Z0-9_]{2,}=\S+$/,
91
+ /^[^@\s]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+$/,
92
+ /^[A-Z]{2}\d{2}[A-Z0-9]{8,30}$/,
93
+ /^(?:[$€£¥]|USD|EUR|GBP|CAD|AUD|CHF|JPY)\d(?:[\d,_]*\d)?(?:\.\d{2})?$/,
94
+ /^--?[A-Za-z][\w-]+$/,
95
+ ]
96
+ const FS_TICKET_SHAPE = /^[A-Z][A-Z0-9]+(?:-[A-Z0-9]+)+$/
97
+ const FS_CAMEL = /^(?:[a-z]+|[A-Z][a-z0-9]+)(?:[A-Z][a-z0-9]*)+$/
98
+
99
+ function fsTier(tok) {
100
+ if (FS_TIER0.test(tok)) return 0
101
+ if (FS_TIER0_SHAPES.some(re => re.test(tok))) return 0
102
+ if (FS_TICKET_SHAPE.test(tok) && /\d/.test(tok)) return 0
103
+ if (tok.length >= 8 && FS_CAMEL.test(tok)) return 0
104
+ if (FS_URL.test(tok)) return 2
105
+ return 1
106
+ }
107
+
108
+ export function extractFactsheetTokens(text) {
109
+ const scan = text.length > FS_MAX_SCAN ? text.slice(0, FS_MAX_SCAN) : text
110
+ const seen = new Set()
111
+ for (const chunk of scan.split(/\s+/)) {
112
+ if (chunk.length < 3 || chunk.length > 512) continue
113
+ for (const re of FS_PATTERNS) {
114
+ for (const m of chunk.matchAll(re)) {
115
+ const tok = (m[1] ?? m[0]).trim().replace(/(?<![.,;:!?])[.,;:!?]+$/, '')
116
+ if (re === FS_TICKET && !/\d/.test(tok)) continue
117
+ if (tok.length >= 3 && tok.length <= 120) seen.add(tok)
118
+ }
119
+ }
120
+ if (seen.size >= 2048) break
121
+ }
122
+ // Substring collapse (length-desc, then lexical — total order) so partial
123
+ // forms fold into the most specific token.
124
+ const ordered = [...seen].sort((a, b) => b.length - a.length || (a < b ? -1 : a > b ? 1 : 0))
125
+ const specific = []
126
+ for (const t of ordered) {
127
+ if (!specific.some(k => k.includes(t))) specific.push(t)
128
+ }
129
+ const ranked = specific
130
+ .map(t => ({ t, tier: fsTier(t) }))
131
+ .sort((a, b) => a.tier - b.tier || b.t.length - a.t.length || (a.t < b.t ? -1 : a.t > b.t ? 1 : 0))
132
+ const kept = []
133
+ let urls = 0
134
+ for (const { t, tier } of ranked) {
135
+ if (kept.length >= FS_MAX_TOKENS) break
136
+ if (tier === 2 && urls++ >= FS_MAX_URLS) continue
137
+ kept.push(t)
138
+ }
139
+ return kept
140
+ }
141
+
142
+ export function factsheetText(text) {
143
+ const tokens = extractFactsheetTokens(text)
144
+ if (!tokens.length) return ''
145
+ return '[Exact identifiers from the rendered image above (paths, ids, versions, numbers) — quote these verbatim instead of transcribing them from the image: '
146
+ + tokens.join(' · ') + ']'
147
+ }
148
+
149
+ // --- The stage ---
150
+ // Claims eligible targets by setting target.compressed to a tool_result
151
+ // content ARRAY (image blocks + factsheet text). applyTargets writes it
152
+ // verbatim, and the Anthropic API accepts block arrays at tool_result.content.
153
+ // Only string-content tool_result targets are eligible (path depth 5 ending in
154
+ // 'content'): sub-text targets point at a `text` field where an array would be
155
+ // structurally invalid.
156
+ export async function pxRenderTargets(targets, body, provider, config) {
157
+ if (!provider || provider.name !== 'anthropic') return 0
158
+ if (!pxModelAllowed(body?.model, config.pxModels)) return 0
159
+ const userText = typeof provider.getLastUserText === 'function' ? (provider.getLastUserText(body) || '') : ''
160
+ if (userText && config.isDangerousTask?.(userText)) return 0
161
+
162
+ const px = await loadPxpipe()
163
+ if (!px?.renderTextToImages) return 0
164
+
165
+ // pxpipe 0.16 resolves a Claude id to its render profile (claude-model-profiles.ts):
166
+ // the original Fable 5 keeps bare 5×8 rows, Fable 5.1+ and other Claude ids get
167
+ // +2px row spacing. Only Claude ids are passed: anything else would resolve to a
168
+ // GPT/Gemini geometry, so it keeps the Anthropic defaults.
169
+ const base = body.model.replace(/\[[^\]]*\]/g, '')
170
+ const pxModel = /claude|anthropic/i.test(base) ? base : undefined
171
+ const imageTokensOf = pages => pages.reduce((sum, p) => sum + (pxModel && typeof px.openAIVisionTokens === 'function'
172
+ ? px.openAIVisionTokens(pxModel, p.width, p.height)
173
+ : Math.ceil(p.width / PATCH_PX) * Math.ceil(p.height / PATCH_PX)), 0)
174
+
175
+ let rendered = 0
176
+ for (const target of targets) {
177
+ if (target.skip || target.dedup || target.diffed || target.readDiffed || target.graphed || target.disclosed || target.bm25Trimmed || target.compressed) continue
178
+ if (typeof target.text !== 'string') continue
179
+ if (!Array.isArray(target.path) || target.path.length !== 5 || target.path[target.path.length - 1] !== 'content') continue
180
+ if (target.text.length < config.pxMinChars) continue
181
+
182
+ const textTokens = countTokens(target.text)
183
+ if (textTokens === 0 || target.text.length / textTokens >= MAX_DENSITY) continue
184
+
185
+ let result
186
+ try {
187
+ result = await px.renderTextToImages(target.text, pxModel ? { reflow: true, model: pxModel } : { reflow: true })
188
+ } catch {
189
+ continue
190
+ }
191
+ if (!result?.pages?.length || result.pages.length > config.pxMaxImages) continue
192
+ // Atlas gaps render as blank cells — silent loss, don't ship it.
193
+ if (result.droppedChars > 0) continue
194
+
195
+ const sheet = factsheetText(target.text)
196
+ // Priced from the real rendered page sizes (Σ w×h = result.pixels), so a
197
+ // spaced profile's taller pages are charged in full.
198
+ const imageTokens = imageTokensOf(result.pages)
199
+ const sheetTokens = sheet ? countTokens(sheet) : 0
200
+ if (imageTokens + sheetTokens >= textTokens * PROFIT_MARGIN) continue
201
+
202
+ const blocks = result.pages.map(p => ({
203
+ type: 'image',
204
+ source: { type: 'base64', media_type: 'image/png', data: Buffer.from(p.png).toString('base64') },
205
+ }))
206
+ if (sheet) blocks.push({ type: 'text', text: sheet })
207
+
208
+ target.compressed = blocks
209
+ target.pxRendered = true
210
+ target.pxOriginalTokens = textTokens
211
+ target.pxCompressedTokens = imageTokens + sheetTokens
212
+ target.pxImageCount = result.pages.length
213
+ rendered += 1
214
+ }
215
+ return rendered
216
+ }
@@ -0,0 +1,73 @@
1
+ // Replay memo: remembers what tamp emitted for each tool_result so the same
2
+ // raw tool_result, re-sent as history in a later request, goes out
3
+ // byte-identical. Without it cacheSafe compresses (and redacts) a turn only
4
+ // while it is newest, so request N+1 forwards that turn raw: savings vanish,
5
+ // the prompt-cache prefix breaks, and a masked secret leaves unmasked.
6
+ //
7
+ // In-memory LRU bounded by the bytes of stored values. A value is a string,
8
+ // an array of content blocks (px-render), or null ("was sent unchanged").
9
+ // Raw text never enters the memo; callers key on its hash.
10
+
11
+ export const DEFAULT_REPLAY_MAX_BYTES = 64 * 1024 * 1024
12
+ const MAX_ANCHORS = 20_000
13
+
14
+ function valueBytes(value) {
15
+ if (value === null || value === undefined) return 0
16
+ if (typeof value === 'string') return Buffer.byteLength(value, 'utf8')
17
+ return Buffer.byteLength(JSON.stringify(value), 'utf8')
18
+ }
19
+
20
+ export function createReplayMemo({ maxBytes = DEFAULT_REPLAY_MAX_BYTES } = {}) {
21
+ const store = new Map()
22
+ let bytes = 0
23
+ let evictions = 0
24
+
25
+ function drop(key) {
26
+ const entry = store.get(key)
27
+ if (!entry) return
28
+ bytes -= entry.size
29
+ store.delete(key)
30
+ }
31
+
32
+ function get(key) {
33
+ const entry = store.get(key)
34
+ if (!entry) return undefined
35
+ // Map insertion order is the LRU order: re-insert to mark as recent.
36
+ store.delete(key)
37
+ store.set(key, entry)
38
+ return entry
39
+ }
40
+
41
+ function put(key, value, meta = {}) {
42
+ drop(key)
43
+ // Count the key too, so a flood of null entries stays bounded.
44
+ const size = valueBytes(value) + key.length
45
+ if (size > maxBytes) return false
46
+ store.set(key, { value, meta, size })
47
+ bytes += size
48
+ while (bytes > maxBytes) {
49
+ drop(store.keys().next().value)
50
+ evictions += 1
51
+ }
52
+ return true
53
+ }
54
+
55
+ function stats() {
56
+ return { entries: store.size, bytes, maxBytes, evictions }
57
+ }
58
+
59
+ // read-diff anchors: raw hash of the read that each cached file copy came
60
+ // from, so a replayed diff can check its base is still in the request.
61
+ const anchors = new Map()
62
+ function setAnchor(key, hash) {
63
+ anchors.delete(key)
64
+ if (!hash) return
65
+ anchors.set(key, hash)
66
+ if (anchors.size > MAX_ANCHORS) anchors.delete(anchors.keys().next().value)
67
+ }
68
+ function getAnchor(key) {
69
+ return anchors.get(key)
70
+ }
71
+
72
+ return { get, put, stats, setAnchor, getAnchor }
73
+ }
@@ -0,0 +1,81 @@
1
+ // Stale image eviction (opt-in, lossy).
2
+ //
3
+ // Tamp's target extractor only collects `type: 'text'` sub-blocks, so base64
4
+ // image blocks inside tool_results pass through untouched. Measured across 403
5
+ // code-only sessions they carry 79% of all tokens: reading PNGs in graphics and
6
+ // game projects, not just browser screenshots.
7
+ //
8
+ // Images are also the safest thing to drop, because they are reloadable — the
9
+ // agent can re-Read the file or re-take the screenshot. What is not safe is
10
+ // dropping the image the agent just asked to see, so eviction is scoped by age:
11
+ // the newest `keepRecent` image-bearing turns keep their pixels.
12
+ //
13
+ // Cost: rewriting old turns invalidates the prompt cache, which is exactly what
14
+ // tamp's cacheSafe mode exists to avoid. That is why this stage is opt-in.
15
+ // Anthropic block shape only.
16
+
17
+ const DEFAULT_KEEP_RECENT = 2
18
+
19
+ function isImageBlock(block) {
20
+ return block?.type === 'image'
21
+ }
22
+
23
+ function hasImage(block) {
24
+ return block?.type === 'tool_result'
25
+ && Array.isArray(block.content)
26
+ && block.content.some(isImageBlock)
27
+ }
28
+
29
+ // Map<tool_use_id, file_path> so the notice can name what was dropped.
30
+ function buildPathIndex(messages) {
31
+ const index = new Map()
32
+ for (const msg of messages) {
33
+ if (msg?.role !== 'assistant' || !Array.isArray(msg.content)) continue
34
+ for (const block of msg.content) {
35
+ if (block?.type !== 'tool_use' || !block.id) continue
36
+ const path = block.input?.file_path || block.input?.path || block.input?.filePath
37
+ if (typeof path === 'string' && path.length > 0) index.set(block.id, path)
38
+ }
39
+ }
40
+ return index
41
+ }
42
+
43
+ // Named source beats a bare "an image": it tells the agent what to re-read.
44
+ function notice(path, chars) {
45
+ const subject = path ? `Image of ${path}` : 'Image'
46
+ return `<tamp-stale-image>\n${subject} dropped — it is older than the most recent turns and images are reloadable.\nIf you still need to see it, re-read the file (or re-take the screenshot).\nDropped: ${chars} base64 characters\n</tamp-stale-image>`
47
+ }
48
+
49
+ export function evictStaleImages(body, { keepRecent = DEFAULT_KEEP_RECENT } = {}) {
50
+ const messages = body?.messages
51
+ const none = { evicted: 0, chars: 0 }
52
+ if (!Array.isArray(messages)) return none
53
+
54
+ const imageTurns = []
55
+ for (let mi = 0; mi < messages.length; mi++) {
56
+ const msg = messages[mi]
57
+ if (msg?.role !== 'user' || !Array.isArray(msg.content)) continue
58
+ if (msg.content.some(hasImage)) imageTurns.push(mi)
59
+ }
60
+ if (imageTurns.length <= keepRecent) return none
61
+
62
+ const paths = buildPathIndex(messages)
63
+ const stale = imageTurns.slice(0, imageTurns.length - keepRecent)
64
+ let evicted = 0
65
+ let chars = 0
66
+
67
+ for (const mi of stale) {
68
+ for (const block of messages[mi].content) {
69
+ if (!hasImage(block)) continue
70
+ block.content = block.content.map(sub => {
71
+ if (!isImageBlock(sub)) return sub
72
+ const size = typeof sub.source?.data === 'string' ? sub.source.data.length : 0
73
+ evicted += 1
74
+ chars += size
75
+ return { type: 'text', text: notice(paths.get(block.tool_use_id), size) }
76
+ })
77
+ }
78
+ }
79
+
80
+ return { evicted, chars }
81
+ }
@@ -0,0 +1,96 @@
1
+ // Stale tool_use.input pruning (opt-in, lossy).
2
+ //
3
+ // Rule table adapted from magic-compact (https://github.com/aerovato/magic-compact,
4
+ // BSD-3-Clause, src/prune.ts). Thresholds and per-tool choices follow that work.
5
+ //
6
+ // tool_use.input carries 26% of billed tokens across 60 large sessions — the
7
+ // largest mass tamp has never touched. An old tool call still needs to show its
8
+ // SHAPE (which tool, against which file) because that is the record of what the
9
+ // agent did. Its payload does not: the written file is on disk, the edit already
10
+ // landed, the agent prompt already ran. So headers stay, payloads go.
11
+ //
12
+ // Age-scoped for the same reason as stale-images: pruning the call the agent is
13
+ // still working with would break it. Rewriting old turns invalidates the prompt
14
+ // cache, which is why this is opt-in. Anthropic block shape only.
15
+
16
+ const DEFAULT_KEEP_RECENT = 2
17
+ const LIMIT = { words: 128, chars: 1024 }
18
+ const BASH_LIMIT = 1024
19
+ const BASH_HEAD = 512
20
+
21
+ function tooSmall(text) {
22
+ if (typeof text !== 'string') return true
23
+ const trimmed = text.trim()
24
+ const words = trimmed ? trimmed.split(/\s+/).length : 0
25
+ return words <= LIMIT.words && text.length <= LIMIT.chars
26
+ }
27
+
28
+ function notice(what, chars) {
29
+ return `[tamp: ${what} omitted from this older turn (${chars} chars). Re-read the file if you need the current contents.]`
30
+ }
31
+
32
+ // field -> label. Each is a payload the agent can recover another way.
33
+ const OMIT_FIELDS = {
34
+ Write: [['content', 'file contents']],
35
+ NotebookEdit: [['new_source', 'notebook source']],
36
+ Agent: [['prompt', 'agent prompt']],
37
+ Workflow: [['script', 'workflow script']],
38
+ SendMessage: [['message', 'inter-agent message']],
39
+ }
40
+
41
+ function pruneInput(name, input) {
42
+ if (!input || typeof input !== 'object') return false
43
+ if (name === 'AskUserQuestion') return false
44
+
45
+ if (name === 'Bash') {
46
+ const command = input.command
47
+ if (typeof command !== 'string' || command.length <= BASH_LIMIT) return false
48
+ input.command = `${command.slice(0, BASH_HEAD)}\n[tamp: command truncated, ${command.length} chars total]`
49
+ return true
50
+ }
51
+
52
+ if (name === 'Edit') {
53
+ const oldStr = input.old_string
54
+ const newStr = input.new_string
55
+ if (typeof oldStr !== 'string' || typeof newStr !== 'string') return false
56
+ if (tooSmall(`${oldStr}\n${newStr}`)) return false
57
+ input.old_string = notice('edit old_string', oldStr.length)
58
+ input.new_string = notice('edit new_string', newStr.length)
59
+ return true
60
+ }
61
+
62
+ const fields = OMIT_FIELDS[name]
63
+ if (!fields) return false
64
+ let changed = false
65
+ for (const [field, label] of fields) {
66
+ const value = input[field]
67
+ if (typeof value !== 'string' || tooSmall(value)) continue
68
+ input[field] = notice(label, value.length)
69
+ changed = true
70
+ }
71
+ return changed
72
+ }
73
+
74
+ export function pruneStaleInputs(body, { keepRecent = DEFAULT_KEEP_RECENT } = {}) {
75
+ const messages = body?.messages
76
+ const none = { pruned: 0 }
77
+ if (!Array.isArray(messages)) return none
78
+
79
+ const callTurns = []
80
+ for (let mi = 0; mi < messages.length; mi++) {
81
+ const msg = messages[mi]
82
+ if (msg?.role !== 'assistant' || !Array.isArray(msg.content)) continue
83
+ if (msg.content.some(b => b?.type === 'tool_use')) callTurns.push(mi)
84
+ }
85
+ if (callTurns.length <= keepRecent) return none
86
+
87
+ let pruned = 0
88
+ for (const mi of callTurns.slice(0, callTurns.length - keepRecent)) {
89
+ for (const block of messages[mi].content) {
90
+ if (block?.type !== 'tool_use') continue
91
+ if (pruneInput(block.name, block.input)) pruned += 1
92
+ }
93
+ }
94
+
95
+ return { pruned }
96
+ }