@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.
- package/README.md +5 -1
- package/compress.js +211 -6
- package/config.js +22 -0
- package/index.js +36 -3
- package/lib/path-extract.js +31 -0
- package/lib/px-render.js +216 -0
- package/lib/replay-memo.js +73 -0
- package/lib/stale-images.js +81 -0
- package/lib/stale-inputs.js +96 -0
- package/lib/usage-tap.js +360 -0
- package/metadata.js +23 -0
- package/package.json +6 -3
- package/providers.js +14 -6
package/README.md
CHANGED
|
@@ -54,7 +54,9 @@ Auto-detects API format, compresses tool output, forwards upstream. Error result
|
|
|
54
54
|
| `diff` | Replace similar re-reads with diffs |
|
|
55
55
|
| `prune` | Remove low-value metadata |
|
|
56
56
|
|
|
57
|
-
Opt-in stages: `strip-comments`, `textpress` (LLM semantic compression), `graph` (session-scoped dedup — works on any coding agent: Codex, Claude Code, Aider — anywhere the same file is read twice, up to -99% per repeat block)
|
|
57
|
+
Opt-in stages: `strip-comments`, `textpress` (LLM semantic compression), `graph` (session-scoped dedup — works on any coding agent: Codex, Claude Code, Aider — anywhere the same file is read twice, up to -99% per repeat block), `px-render` (renders dense tool_results as PNG image blocks via [pxpipe](https://github.com/teamchong/pxpipe) — image tokens are priced by pixel area, so 4-64KB grep/code blocks drop ~80% on vision-legible models like Fable 5; see `docs/notes-px-render.md`), `stale-inputs` (prunes payloads from `tool_use.input` in older turns, keeping the call header — measured 18% on realistic requests), `stale-images` (evicts base64 images from older turns, keeping the newest 2 — ~4%)
|
|
58
|
+
|
|
59
|
+
`stale-inputs` and `stale-images` rewrite older turns, which invalidates the prompt cache. Both stay opt-in for that reason; weigh the token saving against cache-read pricing. See `reports/magic-compact-review.md`.
|
|
58
60
|
|
|
59
61
|
### LLMLingua sidecar
|
|
60
62
|
|
|
@@ -299,6 +301,8 @@ Supported on all providers: Anthropic, OpenAI Chat, OpenAI Responses (Codex), Ge
|
|
|
299
301
|
| `TAMP_LOG` | `true` | Enable logging |
|
|
300
302
|
| `TAMP_LOG_FILE` | *(none)* | Append logs to this file path, in addition to stderr |
|
|
301
303
|
| `TAMP_CACHE_SAFE` | `true` | Compress newest only (prompt-cache safe) |
|
|
304
|
+
| `TAMP_REPLAY` | `true` | Re-send older tool results exactly as Tamp first sent them (savings persist, cache prefix stays stable, secrets stay masked) |
|
|
305
|
+
| `TAMP_REPLAY_MAX_BYTES` | `67108864` | Memory bound for the replay memo (bytes, 64 MiB). Least recently used entries go first |
|
|
302
306
|
| `TAMP_REDACT` | `true` | Mask secrets in tool output before anything leaves the machine |
|
|
303
307
|
| `TAMP_REDACT_MODE` | `mask` | `mask` (replace with marker) or `remove` (delete value) |
|
|
304
308
|
| `TAMP_LLMLINGUA_URL` | *(none)* | LLMLingua sidecar URL. Set to `http://localhost:8788` to skip the auto-probe (avoids startup race if sidecar is still loading the model) |
|
package/compress.js
CHANGED
|
@@ -9,9 +9,12 @@ import { redactText } from './lib/redact.js'
|
|
|
9
9
|
import { anthropic } from './providers.js'
|
|
10
10
|
import { graphDeduplicateTargets } from './session-graph.js'
|
|
11
11
|
import { detectTaskType, generateOutputRules, isDangerousTask } from './lib/rules-generator.js'
|
|
12
|
-
import { extractTargetPaths } from './lib/path-extract.js'
|
|
12
|
+
import { extractTargetPaths, extractTargetToolNames } from './lib/path-extract.js'
|
|
13
|
+
import { evictStaleImages } from './lib/stale-images.js'
|
|
14
|
+
import { pruneStaleInputs } from './lib/stale-inputs.js'
|
|
13
15
|
import { summarize, rehydrateReferences } from './lib/disclosure.js'
|
|
14
16
|
import { trimLinesByRelevance } from './lib/bm25.js'
|
|
17
|
+
import { pxRenderTargets, pxModelAllowed } from './lib/px-render.js'
|
|
15
18
|
|
|
16
19
|
const cache = new Map()
|
|
17
20
|
const MAX_CACHE = 500
|
|
@@ -50,6 +53,7 @@ function deduplicateTargets(targets) {
|
|
|
50
53
|
if (ref.length < target.text.length) {
|
|
51
54
|
target.compressed = ref
|
|
52
55
|
target.dedup = true
|
|
56
|
+
target.refTarget = prev
|
|
53
57
|
}
|
|
54
58
|
} else {
|
|
55
59
|
seen.set(key, target)
|
|
@@ -76,6 +80,7 @@ function diffTargets(targets) {
|
|
|
76
80
|
if (diffBody.length < target.text.length * 0.5) {
|
|
77
81
|
target.compressed = `[diff from tool_result in message ${prev.path[1]}, block ${prev.index}]:\n${diffBody}`
|
|
78
82
|
target.diffed = true
|
|
83
|
+
target.refTarget = prev
|
|
79
84
|
break
|
|
80
85
|
}
|
|
81
86
|
}
|
|
@@ -98,7 +103,137 @@ function quickSimilarity(a, b) {
|
|
|
98
103
|
// can be confidently attributed (via the preceding tool_use), compare against
|
|
99
104
|
// the last-seen body for the same (session, path) pair. If the patch is small
|
|
100
105
|
// enough, substitute it for the body. Always cache the fresh text afterwards.
|
|
101
|
-
|
|
106
|
+
// Tool results that must reach the model verbatim. Unlike a Read or a Bash
|
|
107
|
+
// run, re-invoking these cannot reproduce the content — it would re-prompt the
|
|
108
|
+
// human. Tool attribution is Anthropic-only (see lib/path-extract.js).
|
|
109
|
+
const PROTECTED_TOOLS = new Set(['AskUserQuestion'])
|
|
110
|
+
|
|
111
|
+
function exemptProtectedTools(targets, body, provider) {
|
|
112
|
+
if (provider?.name !== 'anthropic' || !targets.length) return 0
|
|
113
|
+
const names = extractTargetToolNames(body, targets)
|
|
114
|
+
let exempted = 0
|
|
115
|
+
for (let i = 0; i < targets.length; i++) {
|
|
116
|
+
const target = targets[i]
|
|
117
|
+
if (!target || target.skip) continue
|
|
118
|
+
if (!PROTECTED_TOOLS.has(names[i])) continue
|
|
119
|
+
target.skip = 'user-decision'
|
|
120
|
+
exempted += 1
|
|
121
|
+
}
|
|
122
|
+
return exempted
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// --- Replay memo (see lib/replay-memo.js) ---
|
|
126
|
+
// Key = provider + conversation + position + tool id + hash of the RAW text.
|
|
127
|
+
// Position and conversation keep deterministic tool ids (`functions.read:0`)
|
|
128
|
+
// and identical bodies from sharing an entry; the hash catches a client that
|
|
129
|
+
// compacted or edited the body since.
|
|
130
|
+
function sha(text) {
|
|
131
|
+
return createHash('sha256').update(text).digest('base64')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function conversationScope(body) {
|
|
135
|
+
const turns = body?.messages || body?.input || body?.contents
|
|
136
|
+
if (!Array.isArray(turns) || !turns.length) return ''
|
|
137
|
+
// The first turn can be a system prompt every conversation shares (OpenAI
|
|
138
|
+
// chat, Kimi), so the first user turn is part of the identity too.
|
|
139
|
+
const seed = [turns[0], turns.find(t => t?.role === 'user') ?? null]
|
|
140
|
+
// cache_control markers move between requests; they are not identity.
|
|
141
|
+
try { return sha(JSON.stringify(seed, (k, v) => (k === 'cache_control' ? undefined : v))) } catch { return '' }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function replayKey(provider, scope, target) {
|
|
145
|
+
return `${provider.name}\0${scope}\0${target.path.join('/')}\0${target.id || ''}\0${target.rawHash}`
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// px-render values are block arrays; never share one object between the memo
|
|
149
|
+
// and a body that later stages (stale-images) may mutate.
|
|
150
|
+
function cloneReplayValue(value) {
|
|
151
|
+
return Array.isArray(value) ? structuredClone(value) : value
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function replayWrongModel(entry, body, config) {
|
|
155
|
+
return Array.isArray(entry.value) && !pxModelAllowed(body?.model, config.pxModels)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// A memo entry is usable only if what it points at is still in this request:
|
|
159
|
+
// image blocks need a model that reads them, and a dedup/diff/read-diff marker
|
|
160
|
+
// needs its referent (a client can clear or drop the block it refers to).
|
|
161
|
+
function replayUsable(entry, body, config, hashByPath, hashes) {
|
|
162
|
+
if (replayWrongModel(entry, body, config)) return false
|
|
163
|
+
const ref = entry.meta.ref
|
|
164
|
+
if (!ref) return true
|
|
165
|
+
return ref.path ? hashByPath.get(ref.path) === ref.hash : hashes.has(ref.hash)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function applyReplay(body, provider, target, entry, replay) {
|
|
169
|
+
// Through provider.apply so Gemini's wasObject JSON handling still runs.
|
|
170
|
+
// A null entry means "went out unchanged": nothing to write, not a hit.
|
|
171
|
+
if (entry.value === null) return
|
|
172
|
+
provider.apply(body, [{ ...target, skip: undefined, compressed: cloneReplayValue(entry.value) }])
|
|
173
|
+
replay.hits += 1
|
|
174
|
+
replay.tokensHeld += (entry.meta.originalTokens || 0) - (entry.meta.compressedTokens || 0)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Makes history byte-stable and keeps it redacted. Newest-group targets with a
|
|
178
|
+
// memo hit are replayed and skipped. Older turns (cacheSafe) are never
|
|
179
|
+
// compressed fresh: they get their memo entry, or failing that (restart,
|
|
180
|
+
// eviction, replay off) deterministic redaction only, so a masked secret
|
|
181
|
+
// cannot go out in clear one request later.
|
|
182
|
+
function stabilizeHistory(body, targets, config, provider, memo, replay) {
|
|
183
|
+
// Collect older turns before touching the body: a replayed px-render array
|
|
184
|
+
// would otherwise surface as new sub-text targets.
|
|
185
|
+
let older = []
|
|
186
|
+
if (config.cacheSafe !== false) {
|
|
187
|
+
const latest = new Set(targets.filter(t => t.path).map(t => t.path.join('/')))
|
|
188
|
+
older = provider.extract(body, { ...config, cacheSafe: false })
|
|
189
|
+
exemptProtectedTools(older, body, provider)
|
|
190
|
+
older = older.filter(t => !t.skip && typeof t.text === 'string' && !latest.has(t.path.join('/')))
|
|
191
|
+
}
|
|
192
|
+
if (!memo && (config.redact === false || !older.length)) return
|
|
193
|
+
|
|
194
|
+
const scope = memo ? conversationScope(body) : ''
|
|
195
|
+
const hashByPath = new Map()
|
|
196
|
+
if (memo) {
|
|
197
|
+
for (const t of [...older, ...targets]) {
|
|
198
|
+
if (t.skip || typeof t.text !== 'string') continue
|
|
199
|
+
t.rawHash = sha(t.text)
|
|
200
|
+
hashByPath.set(t.path.join('/'), t.rawHash)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const hashes = new Set(hashByPath.values())
|
|
204
|
+
|
|
205
|
+
if (memo) {
|
|
206
|
+
for (const target of targets) {
|
|
207
|
+
if (!target.rawHash) continue
|
|
208
|
+
target.replayKey = replayKey(provider, scope, target)
|
|
209
|
+
const entry = memo.get(target.replayKey)
|
|
210
|
+
if (!entry || !replayUsable(entry, body, config, hashByPath, hashes)) {
|
|
211
|
+
// Another model's request must not overwrite images stored for this one.
|
|
212
|
+
if (entry && replayWrongModel(entry, body, config)) target.replayKey = null
|
|
213
|
+
replay.misses += 1
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
applyReplay(body, provider, target, entry, replay)
|
|
217
|
+
target.skip = 'replayed'
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (const target of older) {
|
|
222
|
+
const key = memo && replayKey(provider, scope, target)
|
|
223
|
+
const entry = memo?.get(key)
|
|
224
|
+
if (entry && replayUsable(entry, body, config, hashByPath, hashes)) { applyReplay(body, provider, target, entry, replay); continue }
|
|
225
|
+
if (memo) replay.misses += 1
|
|
226
|
+
let value = null
|
|
227
|
+
if (config.redact !== false) {
|
|
228
|
+
const r = redactText(target.text, config.redactMode)
|
|
229
|
+
if (r.count > 0) { value = r.text; provider.apply(body, [{ ...target, compressed: value }]) }
|
|
230
|
+
}
|
|
231
|
+
// Remembering the outcome (deterministic) spares the regex pass next time.
|
|
232
|
+
if (memo && !(entry && replayWrongModel(entry, body, config))) memo.put(key, value, {})
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function readDiffTargets(targets, body, sessionKey, readCache, memo) {
|
|
102
237
|
if (!readCache || !sessionKey) return
|
|
103
238
|
const paths = extractTargetPaths(body, targets)
|
|
104
239
|
for (let i = 0; i < targets.length; i++) {
|
|
@@ -108,7 +243,10 @@ function readDiffTargets(targets, body, sessionKey, readCache) {
|
|
|
108
243
|
if (target.skip || target.dedup || target.diffed || target.compressed) {
|
|
109
244
|
// Even for already-compressed targets, refresh the cache with the
|
|
110
245
|
// original text so the next request can diff against the latest copy.
|
|
111
|
-
if (typeof target.text === 'string')
|
|
246
|
+
if (typeof target.text === 'string') {
|
|
247
|
+
readCache.put(sessionKey, path, target.text)
|
|
248
|
+
memo?.setAnchor?.(`${sessionKey}:${path}`, target.rawHash)
|
|
249
|
+
}
|
|
112
250
|
continue
|
|
113
251
|
}
|
|
114
252
|
if (typeof target.text !== 'string') continue
|
|
@@ -119,9 +257,11 @@ function readDiffTargets(targets, body, sessionKey, readCache) {
|
|
|
119
257
|
if (patch.length < target.text.length * 0.5) {
|
|
120
258
|
target.compressed = `[read-diff from prior read of ${path}]:\n${patch}`
|
|
121
259
|
target.readDiffed = true
|
|
260
|
+
target.refHash = memo?.getAnchor?.(`${sessionKey}:${path}`)
|
|
122
261
|
}
|
|
123
262
|
}
|
|
124
263
|
readCache.put(sessionKey, path, target.text)
|
|
264
|
+
memo?.setAnchor?.(`${sessionKey}:${path}`, target.rawHash)
|
|
125
265
|
}
|
|
126
266
|
}
|
|
127
267
|
|
|
@@ -681,6 +821,18 @@ export async function compressRequest(body, config, provider) {
|
|
|
681
821
|
|
|
682
822
|
const targets = provider.extract(body, { ...config, cacheSafe: config.cacheSafe !== false })
|
|
683
823
|
|
|
824
|
+
// Exempt recorded user decisions before any stage sees them. Re-running
|
|
825
|
+
// AskUserQuestion would re-prompt the human, so its result is the one body
|
|
826
|
+
// in a transcript that cannot be regenerated — the same reasoning that
|
|
827
|
+
// already exempts errored results at extract time. These bodies are tiny,
|
|
828
|
+
// so the forgone compression costs nothing.
|
|
829
|
+
exemptProtectedTools(targets, body, provider)
|
|
830
|
+
|
|
831
|
+
// Before redaction, so memo keys hash the raw text the client sent.
|
|
832
|
+
const memo = config.replayMemo || null
|
|
833
|
+
const replay = memo ? { hits: 0, misses: 0, tokensHeld: 0 } : null
|
|
834
|
+
stabilizeHistory(body, targets, config, provider, memo, replay)
|
|
835
|
+
|
|
684
836
|
// Redaction runs FIRST (security, not compression): mask secrets in every
|
|
685
837
|
// target's text before any stage — dedup/diff/cache/llmlingua/textpress —
|
|
686
838
|
// sees it, so secrets never enter a disk cache or get shipped to a third
|
|
@@ -689,7 +841,9 @@ export async function compressRequest(body, config, provider) {
|
|
|
689
841
|
let redactedCount = 0
|
|
690
842
|
if (config.redact !== false) {
|
|
691
843
|
for (const target of targets) {
|
|
692
|
-
|
|
844
|
+
// Replayed targets are already masked on the wire, but read-diff still
|
|
845
|
+
// caches their text; a raw prior there would leak via a later patch.
|
|
846
|
+
if ((target.skip && target.skip !== 'replayed') || typeof target.text !== 'string') continue
|
|
693
847
|
const r = redactText(target.text, config.redactMode)
|
|
694
848
|
if (r.count > 0) { target.text = r.text; target.redacted = true; redactedCount += r.count }
|
|
695
849
|
}
|
|
@@ -697,7 +851,7 @@ export async function compressRequest(body, config, provider) {
|
|
|
697
851
|
|
|
698
852
|
// Read-diff: session-scoped unified diff for re-reads (lossless, opt-in)
|
|
699
853
|
if (config.stages.includes('read-diff') && config.readCache && config.sessionKey && provider.name === 'anthropic') {
|
|
700
|
-
readDiffTargets(targets, body, config.sessionKey, config.readCache)
|
|
854
|
+
readDiffTargets(targets, body, config.sessionKey, config.readCache, config.replayMemo)
|
|
701
855
|
}
|
|
702
856
|
|
|
703
857
|
// Dedup: replace identical blocks with reference markers
|
|
@@ -725,6 +879,18 @@ export async function compressRequest(body, config, provider) {
|
|
|
725
879
|
bm25TrimTargets(targets, body, provider)
|
|
726
880
|
}
|
|
727
881
|
|
|
882
|
+
// px-render: image large dense tool_results for legible models (opt-in, lossy).
|
|
883
|
+
// Runs after bm25-trim so query-aware trimming keeps first claim on huge
|
|
884
|
+
// blocks; px-render picks up dense blocks bm25 declined or can't see.
|
|
885
|
+
if (config.stages.includes('px-render')) {
|
|
886
|
+
await pxRenderTargets(targets, body, provider, {
|
|
887
|
+
pxModels: config.pxModels,
|
|
888
|
+
pxMinChars: config.pxMinChars,
|
|
889
|
+
pxMaxImages: config.pxMaxImages,
|
|
890
|
+
isDangerousTask,
|
|
891
|
+
})
|
|
892
|
+
}
|
|
893
|
+
|
|
728
894
|
const stats = []
|
|
729
895
|
for (const target of targets) {
|
|
730
896
|
if (target.skip) { stats.push({ index: target.index, skipped: target.skip }); continue }
|
|
@@ -754,6 +920,12 @@ export async function compressRequest(body, config, provider) {
|
|
|
754
920
|
stats.push({ index: target.index, method: 'bm25-trim', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
|
|
755
921
|
continue
|
|
756
922
|
}
|
|
923
|
+
if (target.pxRendered) {
|
|
924
|
+
// compressed is a block array (images + factsheet); token counts were
|
|
925
|
+
// computed at render time from pixel area, not text length.
|
|
926
|
+
stats.push({ index: target.index, method: 'px-render', originalLen: target.text.length, compressedLen: JSON.stringify(target.compressed).length, originalTokens: target.pxOriginalTokens, compressedTokens: target.pxCompressedTokens, images: target.pxImageCount })
|
|
927
|
+
continue
|
|
928
|
+
}
|
|
757
929
|
|
|
758
930
|
const result = await compressBlock(target.text, config)
|
|
759
931
|
if (result) {
|
|
@@ -769,7 +941,40 @@ export async function compressRequest(body, config, provider) {
|
|
|
769
941
|
}
|
|
770
942
|
}
|
|
771
943
|
provider.apply(body, targets)
|
|
772
|
-
|
|
944
|
+
|
|
945
|
+
// Remember what went out. stats[i] belongs to targets[i]: the loop above
|
|
946
|
+
// pushes exactly one entry per target.
|
|
947
|
+
if (memo) {
|
|
948
|
+
for (let i = 0; i < targets.length; i++) {
|
|
949
|
+
const target = targets[i]
|
|
950
|
+
if (!target.replayKey || target.skip) continue
|
|
951
|
+
// A marker is replayable only while its referent can be re-checked.
|
|
952
|
+
// graph refs point into session state the memo cannot see: not stored.
|
|
953
|
+
let ref
|
|
954
|
+
if (target.graphed) continue
|
|
955
|
+
if (target.dedup || target.diffed) ref = { path: target.refTarget.path.join('/'), hash: target.refTarget.rawHash }
|
|
956
|
+
else if (target.readDiffed) { if (!target.refHash) continue; ref = { hash: target.refHash } }
|
|
957
|
+
const value = target.compressed ? cloneReplayValue(target.compressed) : null
|
|
958
|
+
memo.put(target.replayKey, value, { originalTokens: stats[i].originalTokens, compressedTokens: stats[i].compressedTokens, ref })
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// stale-images: drop base64 images from older turns (opt-in, lossy). Runs
|
|
963
|
+
// after apply so the extractor never sees the notices it leaves behind.
|
|
964
|
+
let staleImages = null
|
|
965
|
+
if (config.stages.includes('stale-images') && provider.name === 'anthropic') {
|
|
966
|
+
const r = evictStaleImages(body, { keepRecent: config.staleImageKeepRecent })
|
|
967
|
+
if (r.evicted > 0) staleImages = r
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
// stale-inputs: drop payloads from older tool_use blocks (opt-in, lossy).
|
|
971
|
+
let staleInputs = null
|
|
972
|
+
if (config.stages.includes('stale-inputs') && provider.name === 'anthropic') {
|
|
973
|
+
const r = pruneStaleInputs(body, { keepRecent: config.staleInputKeepRecent })
|
|
974
|
+
if (r.pruned > 0) staleInputs = r
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
return { body, stats, targetCount: targets.length, outputHint, disclosure: disclosureStats, redacted: redactedCount, staleImages, staleInputs, replay }
|
|
773
978
|
}
|
|
774
979
|
|
|
775
980
|
export async function compressMessages(body, config) {
|
package/config.js
CHANGED
|
@@ -242,6 +242,10 @@ export function loadConfig(env = process.env, options = {}) {
|
|
|
242
242
|
logFile: get('TAMP_LOG_FILE') || null,
|
|
243
243
|
maxBody: parseInt(get('TAMP_MAX_BODY'), 10) || 10_485_760,
|
|
244
244
|
cacheSafe: parseBoolean(get('TAMP_CACHE_SAFE'), true),
|
|
245
|
+
// Replay memo: re-send each tool_result exactly as tamp first emitted it,
|
|
246
|
+
// so savings, prompt-cache prefixes and redaction survive later requests.
|
|
247
|
+
replay: parseBoolean(get('TAMP_REPLAY'), true),
|
|
248
|
+
replayMaxBytes: parseInt(get('TAMP_REPLAY_MAX_BYTES'), 10) || 67108864,
|
|
245
249
|
// Secret redaction is a security pass, not a compression stage: on by
|
|
246
250
|
// default at every level, masks secrets before any stage (incl. ones that
|
|
247
251
|
// ship text to third parties) sees the body. TAMP_REDACT=false disables it.
|
|
@@ -255,6 +259,13 @@ export function loadConfig(env = process.env, options = {}) {
|
|
|
255
259
|
foundationModelsPath: get('TAMP_FOUNDATION_MODELS_PATH') || 'apfel',
|
|
256
260
|
foundationModelsTimeout: parseInt(get('TAMP_FOUNDATION_MODELS_TIMEOUT'), 10) || 10000,
|
|
257
261
|
foundationModelsSystemPrompt: get('TAMP_FOUNDATION_MODELS_SYSTEM_PROMPT') || 'Compress this text to 50% length while preserving all key information and meaning. Return only the compressed text without explanation.',
|
|
262
|
+
// px-render (image compression via pxpipe): allowlist defaults to the
|
|
263
|
+
// models MEASURED legible at reading dense rendered text (pxpipe FINDINGS
|
|
264
|
+
// 2026-06-16), not the expensive tier per se — opus-class models read
|
|
265
|
+
// dense identifiers off images poorly and stay opt-in.
|
|
266
|
+
pxModels: get('TAMP_PX_MODELS') || 'claude-fable-5',
|
|
267
|
+
pxMinChars: parseInt(get('TAMP_PX_MIN_CHARS'), 10) || 4096,
|
|
268
|
+
pxMaxImages: parseInt(get('TAMP_PX_MAX_IMAGES'), 10) || 6,
|
|
258
269
|
tokenCost: parseFloat(get('TAMP_TOKEN_COST')) || 3,
|
|
259
270
|
})
|
|
260
271
|
}
|
|
@@ -300,6 +311,17 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
|
|
|
300
311
|
# (e.g. codex, cursor, cline, aider, claude-code)
|
|
301
312
|
# TAMP_AGENT=
|
|
302
313
|
|
|
314
|
+
# px-render: image large dense tool_results (needs pxpipe-proxy installed).
|
|
315
|
+
# Opt in via TAMP_STAGES=...,px-render. Allowlist = vision-legible models.
|
|
316
|
+
# TAMP_PX_MODELS=claude-fable-5
|
|
317
|
+
# TAMP_PX_MIN_CHARS=4096
|
|
318
|
+
# TAMP_PX_MAX_IMAGES=6
|
|
319
|
+
|
|
320
|
+
# Replay memo: older tool_results go out exactly as first sent (stable cache
|
|
321
|
+
# prefix, secrets stay masked). In-memory, bounded by TAMP_REPLAY_MAX_BYTES.
|
|
322
|
+
# TAMP_REPLAY=true
|
|
323
|
+
# TAMP_REPLAY_MAX_BYTES=67108864
|
|
324
|
+
|
|
303
325
|
# TAMP_MIN_SIZE=200
|
|
304
326
|
# TAMP_LOG=true
|
|
305
327
|
# TAMP_LOG_FILE=
|
package/index.js
CHANGED
|
@@ -10,6 +10,8 @@ import { createSession, formatRequestLog, redactUrl } from './stats.js'
|
|
|
10
10
|
import { createSessionStore, deriveSessionKey } from './session-graph.js'
|
|
11
11
|
import { createReadCache } from './lib/read-cache.js'
|
|
12
12
|
import { createBrCache } from './lib/br-cache.js'
|
|
13
|
+
import { createReplayMemo } from './lib/replay-memo.js'
|
|
14
|
+
import { createUsageTap, createUsageTotals } from './lib/usage-tap.js'
|
|
13
15
|
import { generateOutputRules, PER_AGENT_OVERRIDES } from './lib/rules-generator.js'
|
|
14
16
|
|
|
15
17
|
// Regex source strings used by the task classifier — surfaced via
|
|
@@ -92,11 +94,12 @@ export function createProxy(overrides = {}, loadOpts = {}) {
|
|
|
92
94
|
: null
|
|
93
95
|
const sessionStore = createSessionStore({ brCache })
|
|
94
96
|
const readCache = createReadCache()
|
|
97
|
+
const replayMemo = config.replay ? createReplayMemo({ maxBytes: config.replayMaxBytes }) : null
|
|
95
98
|
// `tamp -y` starts the sidecar itself and hands the URL down via env, so the
|
|
96
99
|
// isMain probe below never runs and /health kept reporting sidecar:false
|
|
97
100
|
// while llmlingua was in fact wired up. Probe here too, quietly.
|
|
98
101
|
if (config.stages?.includes('llmlingua') && config.llmLinguaUrl) probeSidecar(config, true)
|
|
99
|
-
return { config, session, sessionStore, readCache, brCache, server: _createServer(config, session, sessionStore, readCache, brCache) }
|
|
102
|
+
return { config, session, sessionStore, readCache, brCache, replayMemo, server: _createServer(config, session, sessionStore, readCache, brCache, replayMemo) }
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
// Stream-decompress a zstd body, aborting as soon as the running output size
|
|
@@ -134,9 +137,11 @@ export function sendUpstreamError(res) {
|
|
|
134
137
|
return true
|
|
135
138
|
}
|
|
136
139
|
|
|
137
|
-
function _createServer(config, session, sessionStore, readCache, brCache) {
|
|
140
|
+
function _createServer(config, session, sessionStore, readCache, brCache, replayMemo) {
|
|
138
141
|
const log = createRequestLogger(config)
|
|
139
142
|
const disclosureTotals = { rehydrated: 0, missed: 0, disclosed: 0 }
|
|
143
|
+
const replayTotals = { hits: 0, misses: 0, tokensHeld: 0 }
|
|
144
|
+
const usageTotals = createUsageTotals()
|
|
140
145
|
|
|
141
146
|
function openUpstream(method, upstreamUrl, headers, res) {
|
|
142
147
|
const mod = upstreamUrl.protocol === 'https:' ? https : http
|
|
@@ -149,6 +154,26 @@ function openUpstream(method, upstreamUrl, headers, res) {
|
|
|
149
154
|
}, (upstreamRes) => {
|
|
150
155
|
res.writeHead(upstreamRes.statusCode, upstreamRes.headers)
|
|
151
156
|
upstreamRes.pipe(res)
|
|
157
|
+
// Passive read of provider usage (incl. prompt-cache counters) off the
|
|
158
|
+
// response as it streams past; never alters what the client receives.
|
|
159
|
+
const tap = createUsageTap({
|
|
160
|
+
contentType: upstreamRes.headers['content-type'],
|
|
161
|
+
contentEncoding: upstreamRes.headers['content-encoding'],
|
|
162
|
+
onUsage: (u) => {
|
|
163
|
+
usageTotals.record(u)
|
|
164
|
+
log(`[tamp] usage: in=${u.inputTokens} cache_read=${u.cacheReadTokens} cache_write=${u.cacheCreationTokens} out=${u.outputTokens}`)
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
const onData = (chunk) => tap.write(chunk)
|
|
168
|
+
upstreamRes.on('data', onData)
|
|
169
|
+
// 'close' also fires on an aborted upstream, so the tap always gets ended.
|
|
170
|
+
upstreamRes.on('close', () => tap.end())
|
|
171
|
+
// A 'data' listener keeps the source flowing after pipe() unpipes a dead
|
|
172
|
+
// client; detach and pause so an abandoned stream is not drained.
|
|
173
|
+
res.on('close', () => {
|
|
174
|
+
upstreamRes.off('data', onData)
|
|
175
|
+
if (!upstreamRes.readableEnded) upstreamRes.pause()
|
|
176
|
+
})
|
|
152
177
|
upstreamRes.on('error', (err) => {
|
|
153
178
|
log(`[tamp] response stream error: ${err.code || ''} ${err.message}`)
|
|
154
179
|
res.destroy()
|
|
@@ -236,6 +261,8 @@ return http.createServer(async (req, res) => {
|
|
|
236
261
|
blocksCompressed: totals.compressionCount,
|
|
237
262
|
},
|
|
238
263
|
disclosure: disclosureTotals,
|
|
264
|
+
replay: replayMemo ? { ...replayTotals, ...replayMemo.stats() } : null,
|
|
265
|
+
usage: usageTotals.snapshot(),
|
|
239
266
|
})
|
|
240
267
|
res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) })
|
|
241
268
|
return res.end(req.method === 'HEAD' ? undefined : body)
|
|
@@ -363,7 +390,7 @@ return http.createServer(async (req, res) => {
|
|
|
363
390
|
const sessionBucket = config.stages?.includes('graph') && sessionKey
|
|
364
391
|
? sessionStore.getBucket(sessionKey)
|
|
365
392
|
: null
|
|
366
|
-
const { body, stats, disclosure } = await compressRequest(parsed, { ...config, sessionBucket, sessionKey, readCache, brCache }, provider)
|
|
393
|
+
const { body, stats, disclosure, replay } = await compressRequest(parsed, { ...config, sessionBucket, sessionKey, readCache, brCache, replayMemo }, provider)
|
|
367
394
|
finalBody = Buffer.from(JSON.stringify(body), 'utf-8')
|
|
368
395
|
// Send uncompressed — simpler and content-length is accurate
|
|
369
396
|
if (decompressed) delete headers['content-encoding']
|
|
@@ -375,6 +402,12 @@ return http.createServer(async (req, res) => {
|
|
|
375
402
|
disclosureTotals.disclosed += disclosure.disclosed || 0
|
|
376
403
|
}
|
|
377
404
|
log(formatRequestLog(stats, session, provider.name, req.url, textBody.length, config.tokenCost, sessionBucket))
|
|
405
|
+
if (replay) {
|
|
406
|
+
replayTotals.hits += replay.hits
|
|
407
|
+
replayTotals.misses += replay.misses
|
|
408
|
+
replayTotals.tokensHeld += replay.tokensHeld
|
|
409
|
+
if (replay.hits > 0) log(`[tamp] replay: ${replay.hits} block${replay.hits !== 1 ? 's' : ''} re-sent as before, ${replay.tokensHeld} tok held`)
|
|
410
|
+
}
|
|
378
411
|
config.onCompress?.(stats, session.getTotals(), { provider: provider.name, url: req.url, bodySize: textBody.length })
|
|
379
412
|
} catch (err) {
|
|
380
413
|
log(`[tamp] passthrough (parse error): ${err.message}`)
|
package/lib/path-extract.js
CHANGED
|
@@ -47,3 +47,34 @@ export function extractTargetPaths(body, targets) {
|
|
|
47
47
|
return index.get(toolUseId) || null
|
|
48
48
|
})
|
|
49
49
|
}
|
|
50
|
+
|
|
51
|
+
// Map<tool_use_id, toolName>. Same walk as buildToolUseIndex, but keyed on the
|
|
52
|
+
// tool NAME rather than its file path — per-tool rules need to know that a
|
|
53
|
+
// body came from Read (reloadable) vs AskUserQuestion (a recorded decision).
|
|
54
|
+
function buildToolNameIndex(messages) {
|
|
55
|
+
const index = new Map()
|
|
56
|
+
if (!Array.isArray(messages)) return index
|
|
57
|
+
|
|
58
|
+
for (const msg of messages) {
|
|
59
|
+
if (msg?.role !== 'assistant' || !Array.isArray(msg.content)) continue
|
|
60
|
+
for (const block of msg.content) {
|
|
61
|
+
if (block?.type !== 'tool_use' || !block.id) continue
|
|
62
|
+
if (typeof block.name === 'string' && block.name.length > 0) {
|
|
63
|
+
index.set(block.id, block.name)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return index
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Parallel to `targets`: the tool name that produced each target, or null when
|
|
71
|
+
// it can't be attributed. Never reorders — callers index by position.
|
|
72
|
+
export function extractTargetToolNames(body, targets) {
|
|
73
|
+
const index = buildToolNameIndex(body?.messages)
|
|
74
|
+
return targets.map(target => {
|
|
75
|
+
if (!target || target.skip) return null
|
|
76
|
+
const toolUseId = findToolUseIdForTarget(body, target)
|
|
77
|
+
if (!toolUseId) return null
|
|
78
|
+
return index.get(toolUseId) || null
|
|
79
|
+
})
|
|
80
|
+
}
|