@sliday/tamp 0.3.0 → 0.3.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Tamp
2
2
 
3
- **Token compression proxy for coding agents.** 32.0% fewer input tokens, zero code changes. Works with Claude Code, Aider, Cursor, Cline, Windsurf, and any OpenAI-compatible agent.
3
+ **Token compression proxy for coding agents.** 50.5% fewer input tokens, zero code changes. Works with Claude Code, Aider, Cursor, Cline, Windsurf, and any OpenAI-compatible agent.
4
4
 
5
5
  ```
6
6
  npx @sliday/tamp
@@ -232,9 +232,26 @@ test/fixtures/ Sample API payloads
232
232
 
233
233
  Claude Code sends the full conversation history on every API call. As a session progresses, tool results accumulate — file contents, directory listings, command outputs — all re-sent as input tokens on each request.
234
234
 
235
- At $3/million input tokens (Sonnet 4), a 200-request session consuming 3M input tokens costs $9. If 60% of tool results are compressible JSON, and compression removes 30-50% of those tokens, that's $1.60-2.70 saved per session.
235
+ At $3/million input tokens (Sonnet 4.6), a 200-request session consuming 3M input tokens costs $9. With 32% compression on 60% compressible traffic, that's ~63K tokens and $0.19 saved per session. For Opus 4.6 ($5/MTok), savings are $0.32/session.
236
236
 
237
- For teams with 5 developers doing 2 sessions/day, that's $500-800/month in savings.
237
+ ### Claude Max subscribers
238
+
239
+ Max plans have fixed token budgets. With 32% fewer input tokens per request, you get **47% more requests** from the same budget:
240
+
241
+ | Plan | Without Tamp | With Tamp |
242
+ |------|-------------|-----------|
243
+ | Max 5× ($100/mo) | 5× Pro | **7.4×** Pro |
244
+ | Max 20× ($200/mo) | 20× Pro | **29.4×** Pro |
245
+
246
+ ### API cost savings
247
+
248
+ | Model | Per dev/month | 10-person team/year |
249
+ |-------|-------------|-------------------|
250
+ | Sonnet 4.6 ($3/MTok) | $28 | $3,400 |
251
+ | Opus 4.6 ($5/MTok) | $48 | $5,760 |
252
+ | Opus 4.6 extended ($10/MTok) | $96 | $11,520 |
253
+
254
+ Based on 5 sessions/day, 200 requests/session, 60% compressible traffic.
238
255
 
239
256
  ## License
240
257
 
package/bin/tamp.js CHANGED
@@ -31,6 +31,10 @@ const STAGE_INFO = {
31
31
  'strip-lines':'Remove line-number prefixes from Read output',
32
32
  whitespace: 'Collapse blank lines, trim trailing spaces',
33
33
  llmlingua: 'Neural text compression via LLMLingua-2',
34
+ dedup: 'Replace duplicate tool_results with references',
35
+ diff: 'Replace similar re-reads with unified diffs',
36
+ prune: 'Strip lockfile hashes, registry URLs, npm metadata',
37
+ 'strip-comments': 'Remove code comments (lossy, opt-in)',
34
38
  }
35
39
 
36
40
  // --- Determine stages ---
@@ -40,7 +44,7 @@ let selectedStages
40
44
  if (process.env.TAMP_STAGES) {
41
45
  selectedStages = process.env.TAMP_STAGES.split(',').map(s => s.trim()).filter(Boolean)
42
46
  } else if (skipPrompt) {
43
- selectedStages = Object.keys(STAGE_INFO)
47
+ selectedStages = Object.keys(STAGE_INFO).filter(s => s !== 'strip-comments')
44
48
  } else {
45
49
  log('')
46
50
  log(` ${c.bold}${c.cyan}Tamp${c.reset} ${c.dim}v${pkg.version}${c.reset} — Token compression proxy`)
@@ -50,7 +54,7 @@ if (process.env.TAMP_STAGES) {
50
54
  choices: Object.entries(STAGE_INFO).map(([value, desc]) => ({
51
55
  name: `${value.padEnd(12)} ${c.dim}— ${desc}${c.reset}`,
52
56
  value,
53
- checked: true,
57
+ checked: value !== 'strip-comments',
54
58
  })),
55
59
  })
56
60
  if (selectedStages.length === 0) {
package/compress.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { encode } from '@toon-format/toon'
2
2
  import { countTokens } from '@anthropic-ai/tokenizer'
3
+ import { createPatch } from 'diff'
3
4
  import { tryParseJSON, classifyContent, stripLineNumbers } from './detect.js'
4
5
  import { anthropic } from './providers.js'
5
6
 
@@ -19,6 +20,100 @@ function normalizeWhitespace(text) {
19
20
  .replace(/\n{3,}/g, '\n\n')
20
21
  }
21
22
 
23
+ // --- Stage: dedup ---
24
+ function deduplicateTargets(targets) {
25
+ const seen = new Map()
26
+ for (const target of targets) {
27
+ if (target.skip) continue
28
+ const key = cacheKey(target.text)
29
+ const prev = seen.get(key)
30
+ if (prev && prev.text === target.text) {
31
+ const ref = `[see tool_result in message ${prev.path[1]}, block ${prev.index} — identical content]`
32
+ target.compressed = ref
33
+ target.dedup = true
34
+ } else {
35
+ seen.set(key, target)
36
+ }
37
+ }
38
+ }
39
+
40
+ // --- Stage: diff ---
41
+ function diffTargets(targets) {
42
+ const seen = []
43
+ for (const target of targets) {
44
+ if (target.skip || target.dedup || target.compressed) continue
45
+ if (target.text.length < 200) { seen.push(target); continue }
46
+
47
+ for (const prev of seen) {
48
+ const sim = quickSimilarity(prev.text, target.text)
49
+ if (sim > 0.5 && sim < 1.0) {
50
+ const patch = createPatch('file', prev.text, target.text, '', '', { context: 1 })
51
+ // Strip the patch header (first 4 lines: ---, +++, index, etc.)
52
+ const lines = patch.split('\n')
53
+ const bodyStart = lines.findIndex(l => l.startsWith('@@'))
54
+ if (bodyStart === -1) continue
55
+ const diffBody = lines.slice(bodyStart).join('\n')
56
+ if (diffBody.length < target.text.length * 0.5) {
57
+ target.compressed = `[diff from tool_result in message ${prev.path[1]}, block ${prev.index}]:\n${diffBody}`
58
+ target.diffed = true
59
+ break
60
+ }
61
+ }
62
+ }
63
+ seen.push(target)
64
+ }
65
+ }
66
+
67
+ function quickSimilarity(a, b) {
68
+ if (Math.abs(a.length - b.length) > Math.max(a.length, b.length) * 0.5) return 0
69
+ const setA = new Set(a.split('\n'))
70
+ const setB = new Set(b.split('\n'))
71
+ const intersection = [...setA].filter(x => setB.has(x)).length
72
+ const union = new Set([...setA, ...setB]).size
73
+ return union > 0 ? intersection / union : 0
74
+ }
75
+
76
+ // --- Stage: prune ---
77
+ const PRUNE_KEYS = new Set(['integrity', 'shasum', '_id', '_from', '_resolved', '_integrity', '_nodeVersion', '_npmVersion', '_phantomChildren', '_requiredBy'])
78
+
79
+ function shouldPrune(key, val) {
80
+ if (PRUNE_KEYS.has(key)) return true
81
+ if (key === 'resolved' && typeof val === 'string' && val.startsWith('https://registry.')) return true
82
+ return false
83
+ }
84
+
85
+ function deepPrune(obj) {
86
+ if (Array.isArray(obj)) return obj.map(deepPrune)
87
+ if (obj === null || typeof obj !== 'object') return obj
88
+ const result = {}
89
+ for (const [key, val] of Object.entries(obj)) {
90
+ if (shouldPrune(key, val)) continue
91
+ result[key] = deepPrune(val)
92
+ }
93
+ return result
94
+ }
95
+
96
+ function pruneJSON(text) {
97
+ const { ok, value } = tryParseJSON(text)
98
+ if (!ok) return null
99
+ const pruned = deepPrune(value)
100
+ const result = JSON.stringify(pruned, null, 2)
101
+ if (result.length >= text.length * 0.95) return null
102
+ return result
103
+ }
104
+
105
+ // --- Stage: strip-comments (opt-in, not in defaults) ---
106
+ function stripComments(text) {
107
+ return text
108
+ .replace(/\/\*\*[\s\S]*?\*\//g, '') // JSDoc /** ... */
109
+ .replace(/\/\*[\s\S]*?\*\//g, '') // block /* ... */
110
+ .replace(/\/\/.*$/gm, '') // line // ...
111
+ .replace(/^\s*#(?!!).*$/gm, '') // Python/shell # (not #!)
112
+ .replace(/<!--[\s\S]*?-->/g, '') // HTML <!-- ... -->
113
+ .replace(/\n\s*\n\s*\n/g, '\n\n') // collapse resulting blank lines
114
+ }
115
+
116
+ // --- Core compression ---
22
117
  export function compressText(text, config) {
23
118
  if (text.length < config.minSize) return null
24
119
  const cls = classifyContent(text)
@@ -33,6 +128,9 @@ export function compressText(text, config) {
33
128
  if (config.stages.includes('whitespace')) {
34
129
  processed = normalizeWhitespace(processed)
35
130
  }
131
+ if (config.stages.includes('strip-comments')) {
132
+ processed = stripComments(processed)
133
+ }
36
134
  if (processed.length < text.length * 0.9) {
37
135
  return { text: processed, method: 'normalize', originalLen: text.length, compressedLen: processed.length, originalTokens: countTokens(text), compressedTokens: countTokens(processed) }
38
136
  }
@@ -44,7 +142,14 @@ export function compressText(text, config) {
44
142
 
45
143
  if (cls !== 'json' && cls !== 'json-lined') return null
46
144
 
47
- const raw = cls === 'json-lined' ? stripLineNumbers(text) : text
145
+ let raw = cls === 'json-lined' ? stripLineNumbers(text) : text
146
+
147
+ // Prune low-value fields before minifying
148
+ if (config.stages.includes('prune')) {
149
+ const pruned = pruneJSON(raw)
150
+ if (pruned) raw = pruned
151
+ }
152
+
48
153
  const { ok, value } = tryParseJSON(raw)
49
154
  if (!ok) return null
50
155
 
@@ -106,9 +211,27 @@ async function compressBlock(text, config) {
106
211
 
107
212
  export async function compressRequest(body, config, provider) {
108
213
  const targets = provider.extract(body)
214
+
215
+ // Dedup: replace identical blocks with reference markers
216
+ if (config.stages.includes('dedup')) deduplicateTargets(targets)
217
+
218
+ // Diff: replace similar blocks with unified diffs
219
+ if (config.stages.includes('diff')) diffTargets(targets)
220
+
109
221
  const stats = []
110
222
  for (const target of targets) {
111
223
  if (target.skip) { stats.push({ index: target.index, skipped: target.skip }); continue }
224
+
225
+ // Dedup/diff already set .compressed — record stats and skip compression
226
+ if (target.dedup) {
227
+ stats.push({ index: target.index, method: 'dedup', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
228
+ continue
229
+ }
230
+ if (target.diffed) {
231
+ stats.push({ index: target.index, method: 'diff', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
232
+ continue
233
+ }
234
+
112
235
  const result = await compressBlock(target.text, config)
113
236
  if (result) {
114
237
  target.compressed = result.text
package/config.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export function loadConfig(env = process.env) {
2
- const stages = (env.TAMP_STAGES || 'minify,toon,strip-lines,whitespace,llmlingua').split(',').map(s => s.trim()).filter(Boolean)
2
+ const stages = (env.TAMP_STAGES || 'minify,toon,strip-lines,whitespace,llmlingua,dedup,diff,prune').split(',').map(s => s.trim()).filter(Boolean)
3
3
  return Object.freeze({
4
4
  port: parseInt(env.TAMP_PORT, 10) || 7778,
5
5
  upstream: env.TAMP_UPSTREAM || 'https://api.anthropic.com',
package/package.json CHANGED
@@ -9,8 +9,8 @@
9
9
  "providers.js",
10
10
  "stats.js"
11
11
  ],
12
- "version": "0.3.0",
13
- "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 32.0% fewer input tokens.",
12
+ "version": "0.3.2",
13
+ "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 50.5% fewer input tokens.",
14
14
  "type": "module",
15
15
  "main": "index.js",
16
16
  "bin": {
@@ -20,7 +20,8 @@
20
20
  "start": "node bin/tamp.js",
21
21
  "test": "node --test test/*.test.js",
22
22
  "test:sidecar": "node test/capture-golden.js --verify",
23
- "bench:semantic": "node bench/semantic-eval.js"
23
+ "bench:semantic": "node bench/semantic-eval.js",
24
+ "test:quality": "node bench/quality-eval.js"
24
25
  },
25
26
  "keywords": [
26
27
  "claude",
@@ -41,6 +42,7 @@
41
42
  "@anthropic-ai/tokenizer": "^0.0.4",
42
43
  "@inquirer/prompts": "^8.3.2",
43
44
  "@toon-format/toon": "^2.1.0",
45
+ "diff": "^8.0.4",
44
46
  "fzstd": "^0.1.1"
45
47
  }
46
48
  }