@sliday/tamp 0.8.4 → 0.8.7

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
@@ -247,7 +247,11 @@ Supported on all providers: Anthropic, OpenAI Chat, OpenAI Responses (Codex), Ge
247
247
  | Variable | Default | Description |
248
248
  |----------|---------|-------------|
249
249
  | `TAMP_PORT` | `7778` | Listen port |
250
- | `TAMP_UPSTREAM` | `https://api.anthropic.com` | Default upstream |
250
+ | `TAMP_UPSTREAM` | `https://api.anthropic.com` | Anthropic (default) upstream |
251
+ | `TAMP_UPSTREAM_OPENAI` | `https://api.openai.com` | Upstream for OpenAI-compatible routes (`/v1/chat/completions`, `/v1/responses`) |
252
+ | `TAMP_UPSTREAM_GEMINI` | `https://generativelanguage.googleapis.com` | Upstream for Gemini (`generateContent`) routes |
253
+ | `TAMP_UPSTREAM_KIMI` | `https://api.kimi.com` | Upstream for Kimi Code routes |
254
+ | `TAMP_UPSTREAM_MOONSHOT` | `https://api.moonshot.cn` | Upstream for Moonshot OpenAI-compatible routes |
251
255
  | `TAMP_MIN_SIZE` | `200` | Min content size (chars) |
252
256
  | `TAMP_LOG` | `true` | Enable logging |
253
257
  | `TAMP_CACHE_SAFE` | `true` | Compress newest only (prompt-cache safe) |
package/bin/tamp.js CHANGED
@@ -274,6 +274,11 @@ if (subcommand === 'uninstall-service') {
274
274
  process.exit(0)
275
275
  }
276
276
 
277
+ if (subcommand === 'version' || subcommand === '--version' || subcommand === '-v' || process.argv.includes('--version')) {
278
+ console.log(`Tamp v${VERSION}`)
279
+ process.exit(0)
280
+ }
281
+
277
282
  if (subcommand === 'status') {
278
283
  const port = process.env.TAMP_PORT || 7778
279
284
  const healthy = await checkPort(port)
package/compress.js CHANGED
@@ -180,11 +180,20 @@ function bm25TrimTargets(targets, body, provider) {
180
180
  }
181
181
 
182
182
  // --- Stage: prune ---
183
- const PRUNE_KEYS = new Set(['integrity', 'shasum', '_id', '_from', '_resolved', '_integrity', '_nodeVersion', '_npmVersion', '_phantomChildren', '_requiredBy'])
183
+ // Unambiguously npm-internal keys — safe to drop regardless of value.
184
+ const PRUNE_KEYS = new Set(['_id', '_from', '_resolved', '_integrity', '_nodeVersion', '_npmVersion', '_phantomChildren', '_requiredBy'])
185
+ // `integrity` / `shasum` are common field names; only drop them when the value
186
+ // is actually an npm hash, so a legitimately-named field (e.g. integrity:"high")
187
+ // in a non-npm tool_result is preserved.
188
+ const NPM_INTEGRITY = /^sha\d{1,3}-[A-Za-z0-9+/]+={0,2}$/
189
+ const NPM_SHASUM = /^[a-f0-9]{40}$/i
184
190
 
185
191
  function shouldPrune(key, val) {
186
192
  if (PRUNE_KEYS.has(key)) return true
187
- if (key === 'resolved' && typeof val === 'string' && val.startsWith('https://registry.')) return true
193
+ if (typeof val !== 'string') return false
194
+ if (key === 'resolved' && val.startsWith('https://registry.')) return true
195
+ if (key === 'integrity' && NPM_INTEGRITY.test(val)) return true
196
+ if (key === 'shasum' && NPM_SHASUM.test(val)) return true
188
197
  return false
189
198
  }
190
199
 
@@ -476,8 +485,15 @@ async function compressWithFoundationModels(text, config) {
476
485
  responseData += chunk.toString()
477
486
  })
478
487
 
488
+ const timeoutMs = config.foundationModelsTimeout || 10000
479
489
  const response = await new Promise((resolve, reject) => {
490
+ // Bound the subprocess: a hung apfel must not hang the request forever.
491
+ const timer = setTimeout(() => {
492
+ try { proc.kill('SIGKILL') } catch { /* already gone */ }
493
+ reject(new Error(`apfel timed out after ${timeoutMs}ms`))
494
+ }, timeoutMs)
480
495
  proc.on('close', (code) => {
496
+ clearTimeout(timer)
481
497
  if (code === 5) {
482
498
  reject(new Error('FoundationModels not available (requires macOS 15+, Apple Silicon)'))
483
499
  return
@@ -495,6 +511,7 @@ async function compressWithFoundationModels(text, config) {
495
511
  })
496
512
 
497
513
  proc.on('error', (err) => {
514
+ clearTimeout(timer)
498
515
  reject(err)
499
516
  })
500
517
  })
package/config.js CHANGED
@@ -3,6 +3,7 @@ import { homedir } from 'node:os'
3
3
  import { join } from 'node:path'
4
4
  import {
5
5
  DEFAULT_STAGES,
6
+ ALL_STAGES,
6
7
  VERSION,
7
8
  COMPRESSION_PRESETS,
8
9
  COMPRESSION_LEVELS,
@@ -100,7 +101,13 @@ export function loadConfig(env = process.env, options = {}) {
100
101
 
101
102
  const explicitStagesStr = get('TAMP_STAGES')
102
103
  const explicitStages = explicitStagesStr
103
- ? explicitStagesStr.split(',').map(s => s.trim()).filter(Boolean)
104
+ ? explicitStagesStr.split(',').map(s => s.trim()).filter(Boolean).filter(s => {
105
+ if (ALL_STAGES.includes(s)) return true
106
+ process.stderr.write(
107
+ `[tamp] unknown stage in TAMP_STAGES: ${JSON.stringify(s)} — ignoring. Valid stages: ${ALL_STAGES.join(', ')}\n`
108
+ )
109
+ return false
110
+ })
104
111
  : []
105
112
 
106
113
  const presetNameRaw = get('TAMP_COMPRESSION_PRESET')
@@ -151,7 +158,10 @@ export function loadConfig(env = process.env, options = {}) {
151
158
  level = typeof preset.level === 'number' ? preset.level : null
152
159
  levelSource = 'preset-env'
153
160
  } else {
154
- // Unknown preset name — fall through to default balanced
161
+ // Unknown preset name — warn and fall through to default balanced
162
+ process.stderr.write(
163
+ `[tamp] unknown TAMP_COMPRESSION_PRESET=${JSON.stringify(presetNameRaw)} — expected one of: ${Object.keys(COMPRESSION_PRESETS).join(', ')}. Using balanced.\n`
164
+ )
155
165
  stages = [...COMPRESSION_PRESETS.balanced.stages]
156
166
  level = COMPRESSION_PRESETS.balanced.level
157
167
  levelSource = 'default'
package/detect.js CHANGED
@@ -1,18 +1,29 @@
1
1
  import { rewriteCommandOutput } from './lib/rewriters/index.js'
2
2
 
3
3
  const LINE_NUM_RE = /^ *\d+[\t→]/
4
+ // Genuine line-numbered output (cat -n, the Read tool) is right-aligned, so the
5
+ // number carries leading padding. Requiring padding distinguishes it from
6
+ // unpadded numeric first columns in TSV/CSV tool output.
7
+ const PADDED_LINE_NUM_RE = /^ +\d+[\t→]/
4
8
 
5
9
  export function stripLineNumbers(str) {
6
10
  if (typeof str !== 'string') return str
7
11
  const lines = str.split('\n')
8
12
  if (lines.length < 2) return str
9
- // Check first 3 non-empty lines for line number pattern
10
- let matches = 0
13
+ // Collect the leading numbers from the first few non-empty lines. Only treat
14
+ // them as line numbers when they are padded AND strictly consecutive — the
15
+ // invariants of real line numbering. This avoids silently dropping the first
16
+ // column of numeric tabular data (e.g. "1\tAlice\t30").
17
+ const nums = []
11
18
  for (const line of lines.slice(0, 5)) {
12
19
  if (line.length === 0) continue
13
- if (LINE_NUM_RE.test(line)) matches++
20
+ if (!PADDED_LINE_NUM_RE.test(line)) continue
21
+ nums.push(parseInt(line, 10))
22
+ }
23
+ if (nums.length < 2) return str
24
+ for (let i = 1; i < nums.length; i++) {
25
+ if (nums[i] !== nums[i - 1] + 1) return str
14
26
  }
15
- if (matches < 2) return str
16
27
  return lines.map(l => l.replace(LINE_NUM_RE, '')).join('\n')
17
28
  }
18
29
 
package/index.js CHANGED
@@ -260,13 +260,22 @@ return http.createServer(async (req, res) => {
260
260
  // request stream — pipeRequest() still needs to forward the unread remainder
261
261
  // to the upstream. Without this, an over-maxBody body is truncated while its
262
262
  // original content-length is preserved, hanging the upstream.
263
- for await (const chunk of req.iterator({ destroyOnReturn: false })) {
264
- size += chunk.length
265
- chunks.push(chunk)
266
- if (size > config.maxBody) {
267
- overflow = true
268
- break
263
+ try {
264
+ for await (const chunk of req.iterator({ destroyOnReturn: false })) {
265
+ size += chunk.length
266
+ chunks.push(chunk)
267
+ if (size > config.maxBody) {
268
+ overflow = true
269
+ break
270
+ }
269
271
  }
272
+ } catch (err) {
273
+ // Client aborted mid-upload (ECONNRESET / premature close). The client is
274
+ // gone, so there's nothing to forward — drop the request without letting
275
+ // the rejection bubble up and crash the proxy process.
276
+ log(`[tamp] client aborted during request body: ${err.code || err.message}`)
277
+ if (!res.writableEnded) res.destroy()
278
+ return
270
279
  }
271
280
 
272
281
  if (overflow) {
@@ -327,7 +336,7 @@ return http.createServer(async (req, res) => {
327
336
  const parsed = JSON.parse(textBody.toString('utf-8'))
328
337
 
329
338
  const sessionKey = (config.stages?.includes('graph') || config.stages?.includes('read-diff'))
330
- ? deriveSessionKey(req.headers)
339
+ ? deriveSessionKey(req.headers, parsed)
331
340
  : null
332
341
  const sessionBucket = config.stages?.includes('graph') && sessionKey
333
342
  ? sessionStore.getBucket(sessionKey)
@@ -397,7 +406,7 @@ function probeSidecar(config) {
397
406
  req.on('error', () => {
398
407
  sidecarAvailable = false
399
408
  console.error('[tamp] \u26a0 llmlingua stage enabled but sidecar not running \u2014 text blocks won\u2019t compress')
400
- console.error('[tamp] start with: uv run --with fastapi --with uvicorn --with llmlingua --with mlx uvicorn server:app --host 0.0.0.0 --port 8788 --app-dir sidecar')
409
+ console.error('[tamp] start with: uv run --with fastapi --with uvicorn --with llmlingua --with mlx uvicorn server:app --host 127.0.0.1 --port 8788 --app-dir sidecar')
401
410
  })
402
411
  req.on('timeout', () => { req.destroy() })
403
412
  }
package/lib/br-cache.js CHANGED
@@ -39,7 +39,9 @@ function filePathFor(cacheDir, hash) {
39
39
  }
40
40
 
41
41
  export function createBrCache({ cacheDir = defaultCacheDir(), maxBytes = DEFAULT_MAX_BYTES, minSize = DEFAULT_MIN_SIZE } = {}) {
42
- try { mkdirSync(cacheDir, { recursive: true }) } catch { /* ignore */ }
42
+ // 0o700: cached bodies are full tool_results (code, file contents, command
43
+ // output) that may contain secrets — keep them owner-only on shared machines.
44
+ try { mkdirSync(cacheDir, { recursive: true, mode: 0o700 }) } catch { /* ignore */ }
43
45
 
44
46
  let hits = 0
45
47
  let misses = 0
@@ -101,7 +103,7 @@ export function createBrCache({ cacheDir = defaultCacheDir(), maxBytes = DEFAULT
101
103
  } catch { /* fall through and rewrite */ }
102
104
  }
103
105
 
104
- try { mkdirSync(shardDir, { recursive: true }) } catch { /* ignore */ }
106
+ try { mkdirSync(shardDir, { recursive: true, mode: 0o700 }) } catch { /* ignore */ }
105
107
 
106
108
  let compressed
107
109
  try {
@@ -114,7 +116,7 @@ export function createBrCache({ cacheDir = defaultCacheDir(), maxBytes = DEFAULT
114
116
 
115
117
  const tmpPath = `${finalPath}.tmp`
116
118
  try {
117
- writeFileSync(tmpPath, compressed)
119
+ writeFileSync(tmpPath, compressed, { mode: 0o600 })
118
120
  renameSync(tmpPath, finalPath)
119
121
  brotliBytes = compressed.length
120
122
  } catch {
@@ -23,7 +23,11 @@ const STRATEGIES = Object.freeze({
23
23
  minSize: 200,
24
24
  },
25
25
  json: {
26
- stages: ['minify', 'toon', 'prune'],
26
+ // No 'toon' here: this strategy rewrites a file in place that keeps its
27
+ // .json extension, so the output must stay valid JSON. TOON is not JSON.
28
+ // minify (lossless) and prune (documented npm-metadata strip) keep it
29
+ // parseable.
30
+ stages: ['minify', 'prune'],
27
31
  preserveStructure: false,
28
32
  minSize: 100,
29
33
  },
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "sidecar/server.py",
15
15
  "sidecar/requirements.txt"
16
16
  ],
17
- "version": "0.8.4",
17
+ "version": "0.8.7",
18
18
  "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.",
19
19
  "type": "module",
20
20
  "main": "index.js",
package/providers.js CHANGED
@@ -21,14 +21,14 @@ function findLatestEligibleGroup(items, extractTargets) {
21
21
 
22
22
  function extractAnthropicMessageTargets(msg, mi) {
23
23
  const targets = []
24
- if (msg.role !== 'user') return targets
24
+ if (!msg || msg.role !== 'user') return targets
25
25
 
26
26
  if (typeof msg.content === 'string') {
27
27
  targets.push({ path: ['messages', mi, 'content'], text: msg.content, index: mi })
28
28
  } else if (Array.isArray(msg.content)) {
29
29
  for (let i = 0; i < msg.content.length; i++) {
30
30
  const block = msg.content[i]
31
- if (block.type !== 'tool_result') continue
31
+ if (!block || block.type !== 'tool_result') continue
32
32
  if (block.is_error) {
33
33
  targets.push({ skip: 'error', index: i })
34
34
  continue
@@ -39,7 +39,7 @@ function extractAnthropicMessageTargets(msg, mi) {
39
39
  } else if (Array.isArray(block.content)) {
40
40
  for (let j = 0; j < block.content.length; j++) {
41
41
  const sub = block.content[j]
42
- if (sub.type === 'text') {
42
+ if (sub && sub.type === 'text') {
43
43
  targets.push({ path: ['messages', mi, 'content', i, 'content', j, 'text'], text: sub.text, index: i })
44
44
  }
45
45
  }
@@ -203,11 +203,13 @@ function applyAnthropicRehydration(body, refs) {
203
203
  if (updated === current) continue
204
204
 
205
205
  if (isToolUseInput) {
206
+ // tool_use.input must stay a JSON object. If inserting the expansion made
207
+ // the serialized form invalid JSON, leave the original input untouched
208
+ // rather than writing a string Anthropic would reject.
206
209
  try {
207
- const parsed = JSON.parse(updated)
208
- setAtPath(body, path, parsed)
209
- continue
210
- } catch { /* fall through to string */ }
210
+ setAtPath(body, path, JSON.parse(updated))
211
+ } catch { /* not valid JSON: keep the original object input */ }
212
+ continue
211
213
  }
212
214
  setAtPath(body, path, updated)
213
215
  }
@@ -249,7 +251,7 @@ const anthropic = {
249
251
  const OPENAI_COMPAT_SKIP_BLOCK_TYPES = new Set(['thinking', 'partial'])
250
252
 
251
253
  function extractOpenAIChatTargets(msg, i) {
252
- if (msg.role !== 'tool') return []
254
+ if (!msg || msg.role !== 'tool') return []
253
255
  // Some OpenAI-compat providers (Kimi, etc.) emit non-string tool content
254
256
  // with typed blocks like { type: 'thinking' }. Skip those.
255
257
  if (typeof msg.content !== 'string') {
@@ -391,11 +393,11 @@ const moonshot = {
391
393
 
392
394
  function extractGeminiContentTargets(content, ci) {
393
395
  const targets = []
394
- if (!content.parts?.length) return targets
396
+ if (!content || !content.parts?.length) return targets
395
397
 
396
398
  for (let pi = 0; pi < content.parts.length; pi++) {
397
399
  const part = content.parts[pi]
398
- if (!part.functionResponse?.response) continue
400
+ if (!part || !part.functionResponse?.response) continue
399
401
  const resp = part.functionResponse.response
400
402
  const text = typeof resp === 'string' ? resp : JSON.stringify(resp, null, 2)
401
403
  targets.push({
@@ -459,10 +461,13 @@ const gemini = {
459
461
  const path = t.path
460
462
  for (let i = 0; i < path.length - 1; i++) obj = obj[path[i]]
461
463
  if (t.wasObject) {
464
+ // Gemini's functionResponse.response must be a JSON object. If the
465
+ // compressed form isn't valid JSON (e.g. TOON), writing it as a string
466
+ // produces a request the API rejects — keep the original object.
462
467
  try {
463
468
  obj[path[path.length - 1]] = JSON.parse(t.compressed)
464
- continue
465
- } catch { /* fall through to string */ }
469
+ } catch { /* not valid JSON: leave the original object untouched */ }
470
+ continue
466
471
  }
467
472
  obj[path[path.length - 1]] = t.compressed
468
473
  }
package/session-graph.js CHANGED
@@ -55,11 +55,35 @@ export function createSessionBucket({ brCache = null } = {}) {
55
55
  }
56
56
  }
57
57
 
58
- export function deriveSessionKey(headers) {
58
+ // A conversation-stable fingerprint: the system prompt plus the first turn.
59
+ // Claude Code (and peers) resend an immutable opening every turn, so this is
60
+ // constant within a conversation but differs across conversations — even when
61
+ // they share one API token. Empty when no body is available (callers that omit
62
+ // it get the legacy token-only key).
63
+ function conversationSeed(body) {
64
+ if (!body || typeof body !== 'object') return ''
65
+ const turns = body.messages || body.contents || body.input
66
+ const first = Array.isArray(turns) && turns.length ? turns[0] : null
67
+ if (first == null && body.system === undefined) return ''
68
+ try {
69
+ return JSON.stringify({ system: body.system ?? null, first }).slice(0, 4096)
70
+ } catch {
71
+ return ''
72
+ }
73
+ }
74
+
75
+ // Session key scopes the read-diff / graph caches. It MUST be conversation-
76
+ // specific: keying on the token alone let content from one conversation be
77
+ // referenced (read-diff, graph) in another that shares the token, emitting a
78
+ // diff/marker against a base the second conversation's model never saw. A
79
+ // too-unique seed merely disables cross-request dedup (full content is
80
+ // forwarded) — the safe failure direction.
81
+ export function deriveSessionKey(headers, body) {
59
82
  if (!headers) return null
60
83
  const auth = headers.authorization || headers['x-api-key'] || ''
61
84
  if (!auth) return null
62
- return createHash('sha256').update(auth).digest('hex').slice(0, 16)
85
+ const seed = conversationSeed(body)
86
+ return createHash('sha256').update(`${auth} ${seed}`).digest('hex').slice(0, 16)
63
87
  }
64
88
 
65
89
  export function graphDeduplicateTargets(targets, bucket, { minBytes = MIN_BYTES } = {}) {