@sliday/tamp 0.5.8 → 0.7.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/metadata.js CHANGED
@@ -5,6 +5,7 @@ const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url),
5
5
  export const VERSION = pkg.version
6
6
 
7
7
  export const DEFAULT_STAGES = Object.freeze([
8
+ 'cmd-strip',
8
9
  'minify',
9
10
  'toon',
10
11
  'strip-lines',
@@ -12,6 +13,7 @@ export const DEFAULT_STAGES = Object.freeze([
12
13
  'llmlingua',
13
14
  'dedup',
14
15
  'diff',
16
+ 'read-diff',
15
17
  'prune',
16
18
  ])
17
19
 
@@ -20,6 +22,9 @@ export const EXTRA_STAGES = Object.freeze([
20
22
  'textpress',
21
23
  'foundation-models',
22
24
  'graph',
25
+ 'br-cache',
26
+ 'disclosure',
27
+ 'bm25-trim',
23
28
  ])
24
29
 
25
30
  export const ALL_STAGES = Object.freeze([
@@ -29,12 +34,18 @@ export const ALL_STAGES = Object.freeze([
29
34
 
30
35
  // Stages that can drop or paraphrase content (semantic/neural/comment removal).
31
36
  // `graph` is opt-in but fully lossless — it substitutes a reference marker for
32
- // content the model has already seen earlier in the same session.
37
+ // content the model has already seen earlier in the same session. `br-cache`
38
+ // is also lossless: it's a disk-backed store for large bodies, not a rewriter.
39
+ // `disclosure` IS lossy: it drops body content from the outgoing turn and
40
+ // relies on the model quoting the marker to trigger rehydration next turn.
41
+ // `bm25-trim` drops low-relevance lines from huge tool_results (lossy).
33
42
  export const LOSSY_STAGES = Object.freeze(new Set([
34
43
  'llmlingua',
35
44
  'foundation-models',
36
45
  'textpress',
37
46
  'strip-comments',
47
+ 'disclosure',
48
+ 'bm25-trim',
38
49
  ]))
39
50
 
40
51
  export function isLossy(stage) {
@@ -53,6 +64,10 @@ export const STAGE_HINTS = Object.freeze({
53
64
  summary: 'cross-request dedup, lossless, -99% on re-reads',
54
65
  setup: 'TAMP_STAGES=...,graph',
55
66
  },
67
+ 'read-diff': {
68
+ summary: 'replace re-reads with diffs, lossless, -80% on common flows',
69
+ setup: 'enabled by default in balanced preset',
70
+ },
56
71
  'strip-comments': {
57
72
  summary: 'remove code comments (lossy)',
58
73
  setup: null,
@@ -65,9 +80,22 @@ export const STAGE_HINTS = Object.freeze({
65
80
  summary: 'Apple Intelligence neural (macOS 15+, lossy)',
66
81
  setup: null,
67
82
  },
83
+ 'br-cache': {
84
+ summary: 'Brotli disk cache, persists across sessions, enables Phase 5 disclosure',
85
+ setup: 'TAMP_STAGES=...,br-cache',
86
+ },
87
+ disclosure: {
88
+ summary: 'progressive disclosure for >32KB tool_results, model can quote hash to expand',
89
+ setup: 'TAMP_STAGES=...,disclosure (requires br-cache)',
90
+ },
91
+ 'bm25-trim': {
92
+ summary: 'query-aware line ranking, preserves first+last, bypasses dangerous tasks',
93
+ setup: 'TAMP_STAGES=...,bm25-trim',
94
+ },
68
95
  })
69
96
 
70
97
  export const STAGE_DESCRIPTIONS = Object.freeze({
98
+ 'cmd-strip': 'Strip progress bars and spinners from command output (lossless)',
71
99
  minify: 'Strip JSON whitespace (lossless)',
72
100
  toon: 'Columnar array encoding (lossless)',
73
101
  'strip-lines': 'Remove line-number prefixes',
@@ -76,32 +104,36 @@ export const STAGE_DESCRIPTIONS = Object.freeze({
76
104
  'foundation-models': 'Apple Intelligence neural compression (macOS 15+, Apple Silicon, 100% local)',
77
105
  dedup: 'Deduplicate identical tool_results',
78
106
  diff: 'Replace similar re-reads with diffs',
107
+ 'read-diff': 'Session-scoped unified diff for re-reads (lossless, opt-in)',
79
108
  prune: 'Strip lockfile hashes & npm metadata',
80
109
  'strip-comments': 'Remove code comments (lossy)',
81
110
  textpress: 'LLM semantic compression (Ollama/OpenRouter)',
82
111
  graph: 'Session-scoped dedup across requests (lossless, opt-in)',
112
+ 'br-cache': 'Disk-backed Brotli store for large tool_results (lossless, opt-in)',
113
+ disclosure: '3-tier summary for huge tool_results with on-demand rehydration (lossy, aggressive-only)',
114
+ 'bm25-trim': 'Drop low-relevance lines from huge tool_results via BM25 (lossy, aggressive-only)',
83
115
  })
84
116
 
85
117
  export const COMPRESSION_PRESETS = Object.freeze({
86
118
  conservative: {
87
119
  name: 'Conservative',
88
120
  description: 'Safe, lossless compression only',
89
- stages: ['minify', 'toon', 'strip-lines', 'whitespace', 'dedup', 'diff'],
121
+ stages: ['cmd-strip', 'minify', 'toon', 'strip-lines', 'whitespace', 'dedup', 'diff'],
90
122
  expectedSavings: '45-50%',
91
123
  risk: 'None',
92
124
  },
93
125
  balanced: {
94
126
  name: 'Balanced',
95
127
  description: 'Default, includes semantic compression',
96
- stages: ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune'],
128
+ stages: ['cmd-strip', 'minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'read-diff', 'prune'],
97
129
  expectedSavings: '52-58%',
98
130
  risk: 'Low',
99
131
  },
100
132
  aggressive: {
101
133
  name: 'Aggressive',
102
134
  description: 'Maximum compression, lossy stages enabled',
103
- stages: ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune', 'strip-comments', 'textpress'],
104
- expectedSavings: '60-68%',
135
+ stages: ['cmd-strip', 'minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'read-diff', 'prune', 'strip-comments', 'textpress', 'br-cache', 'disclosure', 'bm25-trim'],
136
+ expectedSavings: '65-72%',
105
137
  risk: 'Medium (may lose comments, verbose text)',
106
138
  },
107
139
  })
package/package.json CHANGED
@@ -12,8 +12,8 @@
12
12
  "stats.js",
13
13
  "lib/"
14
14
  ],
15
- "version": "0.5.8",
16
- "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 60-70% combined input+output savings with Caveman-integrated presets.",
15
+ "version": "0.7.0",
16
+ "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 60-70% combined input+output savings with Caveman-integrated presets and per-command rewriters.",
17
17
  "type": "module",
18
18
  "main": "index.js",
19
19
  "bin": {
package/providers.js CHANGED
@@ -52,6 +52,9 @@ function extractAnthropicMessageTargets(msg, mi) {
52
52
 
53
53
  function getLastUserTextFromAnthropicMessages(messages) {
54
54
  if (!Array.isArray(messages)) return null
55
+ // Walk back across user messages until we find one with actual text
56
+ // content. Latest user messages in a Claude Code session are often
57
+ // pure tool_result blocks; the human's intent text is in an earlier turn.
55
58
  for (let i = messages.length - 1; i >= 0; i--) {
56
59
  const msg = messages[i]
57
60
  if (msg?.role !== 'user') continue
@@ -61,7 +64,7 @@ function getLastUserTextFromAnthropicMessages(messages) {
61
64
  if (b?.type === 'text' && typeof b.text === 'string') return b.text
62
65
  }
63
66
  }
64
- return null
67
+ // No text in this user message — keep walking back
65
68
  }
66
69
  return null
67
70
  }
@@ -91,6 +94,117 @@ function appendToLastUserMessageAnthropic(messages, text) {
91
94
  return false
92
95
  }
93
96
 
97
+ // ---- Disclosure: find and rehydrate <tamp-ref:v1:...> quotes (Phase 5) ----
98
+
99
+ const DISCLOSURE_MARKER = /<tamp-ref:v1:([a-f0-9]{64}):(\d+)>/g
100
+
101
+ function scanStringForRefs(str, pathDesc, out) {
102
+ if (typeof str !== 'string' || str.indexOf('<tamp-ref:v1:') === -1) return
103
+ const re = new RegExp(DISCLOSURE_MARKER.source, DISCLOSURE_MARKER.flags)
104
+ let m
105
+ while ((m = re.exec(str)) !== null) {
106
+ out.push({ hash: m[1], bytes: Number(m[2]), match: m[0], path: pathDesc.slice() })
107
+ }
108
+ }
109
+
110
+ function findAnthropicReferences(body) {
111
+ const out = []
112
+ if (!Array.isArray(body?.messages)) return out
113
+ for (let mi = 0; mi < body.messages.length; mi++) {
114
+ const msg = body.messages[mi]
115
+ if (!msg) continue
116
+ if (typeof msg.content === 'string') {
117
+ scanStringForRefs(msg.content, ['messages', mi, 'content'], out)
118
+ continue
119
+ }
120
+ if (!Array.isArray(msg.content)) continue
121
+ for (let bi = 0; bi < msg.content.length; bi++) {
122
+ const block = msg.content[bi]
123
+ if (!block) continue
124
+ if (block.type === 'text' && typeof block.text === 'string') {
125
+ scanStringForRefs(block.text, ['messages', mi, 'content', bi, 'text'], out)
126
+ } else if (block.type === 'tool_use') {
127
+ if (block.input && typeof block.input === 'object') {
128
+ const serialized = JSON.stringify(block.input)
129
+ scanStringForRefs(serialized, ['messages', mi, 'content', bi, 'input'], out)
130
+ } else if (typeof block.input === 'string') {
131
+ scanStringForRefs(block.input, ['messages', mi, 'content', bi, 'input'], out)
132
+ }
133
+ } else if (block.type === 'tool_result') {
134
+ if (typeof block.content === 'string') {
135
+ scanStringForRefs(block.content, ['messages', mi, 'content', bi, 'content'], out)
136
+ } else if (Array.isArray(block.content)) {
137
+ for (let ci = 0; ci < block.content.length; ci++) {
138
+ const sub = block.content[ci]
139
+ if (sub?.type === 'text' && typeof sub.text === 'string') {
140
+ scanStringForRefs(sub.text, ['messages', mi, 'content', bi, 'content', ci, 'text'], out)
141
+ }
142
+ }
143
+ }
144
+ }
145
+ }
146
+ }
147
+ return out
148
+ }
149
+
150
+ function getAtPath(body, path) {
151
+ let obj = body
152
+ for (let i = 0; i < path.length; i++) obj = obj?.[path[i]]
153
+ return obj
154
+ }
155
+
156
+ function setAtPath(body, path, value) {
157
+ let obj = body
158
+ for (let i = 0; i < path.length - 1; i++) obj = obj[path[i]]
159
+ obj[path[path.length - 1]] = value
160
+ }
161
+
162
+ // Replace marker(s) at each ref's path with the expanded block. For
163
+ // tool_use.input objects we round-trip via JSON. Idempotent: if the marker
164
+ // has already been replaced by an expansion (which contains "expanded -"
165
+ // sentinel for the same hash), we skip further replacement.
166
+ function applyAnthropicRehydration(body, refs) {
167
+ const groups = new Map()
168
+ for (const ref of refs) {
169
+ if (!ref.expansion) continue
170
+ const key = ref.path.join('|')
171
+ if (!groups.has(key)) groups.set(key, { path: ref.path, items: [] })
172
+ groups.get(key).items.push(ref)
173
+ }
174
+
175
+ for (const { path, items } of groups.values()) {
176
+ const isToolUseInput = path[path.length - 1] === 'input'
177
+ let current
178
+ if (isToolUseInput) {
179
+ const val = getAtPath(body, path)
180
+ current = typeof val === 'string' ? val : JSON.stringify(val)
181
+ } else {
182
+ current = getAtPath(body, path)
183
+ }
184
+ if (typeof current !== 'string') continue
185
+
186
+ let updated = current
187
+ for (const ref of items) {
188
+ const expandedSentinel = `<tamp-ref:v1:${ref.hash} expanded -`
189
+ if (updated.indexOf(expandedSentinel) !== -1) continue
190
+ const idx = updated.indexOf(ref.match)
191
+ if (idx === -1) continue
192
+ updated = updated.slice(0, idx) + ref.expansion + updated.slice(idx + ref.match.length)
193
+ }
194
+
195
+ if (updated === current) continue
196
+
197
+ if (isToolUseInput) {
198
+ try {
199
+ const parsed = JSON.parse(updated)
200
+ setAtPath(body, path, parsed)
201
+ continue
202
+ } catch { /* fall through to string */ }
203
+ }
204
+ setAtPath(body, path, updated)
205
+ }
206
+ }
207
+
94
208
  const anthropic = {
95
209
  name: 'anthropic',
96
210
  match(method, url) {
@@ -113,6 +227,12 @@ const anthropic = {
113
227
  injectOutputHint(body, text) {
114
228
  return appendToLastUserMessageAnthropic(body?.messages, text)
115
229
  },
230
+ findReferences(body) {
231
+ return findAnthropicReferences(body)
232
+ },
233
+ applyRehydration(body, refs) {
234
+ return applyAnthropicRehydration(body, refs)
235
+ },
116
236
  }
117
237
 
118
238
  function extractOpenAIChatTargets(msg, i) {
@@ -291,6 +411,7 @@ const openaiResponses = {
291
411
  },
292
412
  getLastUserText(body) {
293
413
  if (!Array.isArray(body?.input) || !body.input.length) return null
414
+ // Walk back across user items until we find one with actual text content
294
415
  for (let i = body.input.length - 1; i >= 0; i--) {
295
416
  const item = body.input[i]
296
417
  if (item?.role !== 'user' || !Array.isArray(item.content)) continue
@@ -302,6 +423,8 @@ const openaiResponses = {
302
423
  },
303
424
  injectOutputHint(body, text) {
304
425
  if (!Array.isArray(body?.input) || !body.input.length || !text) return false
426
+ // Target the LATEST user item (cache-safe). If it has no input_text
427
+ // block we push one, so injection always succeeds for any user item.
305
428
  for (let i = body.input.length - 1; i >= 0; i--) {
306
429
  const item = body.input[i]
307
430
  if (item?.role !== 'user' || !Array.isArray(item.content)) continue
package/session-graph.js CHANGED
@@ -4,7 +4,7 @@ const SESSION_TTL_MS = 2 * 60 * 60 * 1000
4
4
  const MAX_SESSIONS = 50
5
5
  const MIN_BYTES = 512
6
6
 
7
- export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_SESSIONS } = {}) {
7
+ export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_SESSIONS, brCache = null } = {}) {
8
8
  const store = new Map()
9
9
 
10
10
  function prune() {
@@ -26,7 +26,7 @@ export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_S
26
26
  const oldest = store.keys().next().value
27
27
  store.delete(oldest)
28
28
  }
29
- bucket = { refs: new Map(), nextId: 0, lastSeen: Date.now() }
29
+ bucket = createSessionBucket({ brCache })
30
30
  store.set(key, bucket)
31
31
  return bucket
32
32
  }
@@ -34,6 +34,27 @@ export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_S
34
34
  return { getBucket, prune, _store: store }
35
35
  }
36
36
 
37
+ // A bucket tracks per-session graph state. Optionally pipes large target
38
+ // bodies through a Brotli disk cache so the in-memory structure stays lean
39
+ // and content persists across proxy restarts for Phase 5 rehydration.
40
+ export function createSessionBucket({ brCache = null } = {}) {
41
+ const refs = new Map()
42
+ let brotliBytes = 0
43
+ let offloaded = 0
44
+ let hits = 0
45
+
46
+ return {
47
+ refs,
48
+ nextId: 0,
49
+ lastSeen: Date.now(),
50
+ brCache,
51
+ // Counters incremented by graphDeduplicateTargets when brCache is attached.
52
+ brStats() { return { brotliBytes, offloaded, hits } },
53
+ _recordOffload(bytes) { brotliBytes += bytes; offloaded += 1 },
54
+ _recordHit() { hits += 1 },
55
+ }
56
+ }
57
+
37
58
  export function deriveSessionKey(headers) {
38
59
  if (!headers) return null
39
60
  const auth = headers.authorization || headers['x-api-key'] || ''
@@ -43,6 +64,7 @@ export function deriveSessionKey(headers) {
43
64
 
44
65
  export function graphDeduplicateTargets(targets, bucket, { minBytes = MIN_BYTES } = {}) {
45
66
  if (!bucket) return
67
+ const brCache = bucket.brCache
46
68
  for (const target of targets) {
47
69
  if (target.skip || target.dedup || target.diffed || target.compressed) continue
48
70
  if (typeof target.text !== 'string' || target.text.length < minBytes) continue
@@ -52,9 +74,20 @@ export function graphDeduplicateTargets(targets, bucket, { minBytes = MIN_BYTES
52
74
  if (existing) {
53
75
  target.compressed = `<tamp-file-ref id="${existing.id}" sha="${hash}" bytes="${target.text.length}"/>`
54
76
  target.graphed = true
77
+ if (brCache && existing.brHash) bucket._recordHit()
55
78
  continue
56
79
  }
57
80
  bucket.nextId += 1
58
- bucket.refs.set(hash, { id: bucket.nextId, bytes: target.text.length })
81
+ const entry = { id: bucket.nextId, bytes: target.text.length }
82
+ // Opportunistic offload to the disk store — Phase 3 collects the hash
83
+ // for forward-compat with Phase 5 rehydration; no markers change yet.
84
+ if (brCache && typeof brCache.put === 'function' && target.text.length >= brCache.minSize) {
85
+ const stored = brCache.put(target.text)
86
+ if (stored) {
87
+ entry.brHash = stored.hash
88
+ bucket._recordOffload(stored.brotliBytes)
89
+ }
90
+ }
91
+ bucket.refs.set(hash, entry)
59
92
  }
60
93
  }
package/stats.js CHANGED
@@ -9,7 +9,7 @@ const c = {
9
9
  red: '\x1b[31m',
10
10
  }
11
11
 
12
- export function formatRequestLog(stats, session, providerName, url, bodySize, tokenCost) {
12
+ export function formatRequestLog(stats, session, providerName, url, bodySize, tokenCost, sessionBucket) {
13
13
  const compressed = stats.filter(s => s.method)
14
14
  const label = providerName || 'anthropic'
15
15
  const path = url || '/v1/messages'
@@ -58,6 +58,17 @@ export function formatRequestLog(stats, session, providerName, url, bodySize, to
58
58
  lines.push(`[tamp] ${c.magenta}session${c.reset} ${fmtSize(t.totalSaved)} chars, ${t.totalTokensSaved} tokens saved across ${t.compressionCount} blocks ${c.dim}(${sessionPct}% avg)${c.reset}${moneyInfo}`)
59
59
  }
60
60
 
61
+ // br-cache counters — only render when the bucket reports activity, so
62
+ // turning the stage off produces no extra rows.
63
+ if (sessionBucket && typeof sessionBucket.brStats === 'function') {
64
+ const br = sessionBucket.brStats()
65
+ if (br && (br.offloaded > 0 || br.hits > 0)) {
66
+ const totalLookups = br.offloaded + br.hits
67
+ const ratio = totalLookups > 0 ? ((br.hits / totalLookups) * 100).toFixed(1) : '0.0'
68
+ lines.push(`[tamp] ${c.cyan}br-cache${c.reset} ${fmtSize(br.brotliBytes)} stored, ${br.offloaded} offloaded, ${br.hits} hits ${c.dim}(${ratio}% hit ratio)${c.reset}`)
69
+ }
70
+ }
71
+
61
72
  return lines.join('\n')
62
73
  }
63
74