@sliday/tamp 0.8.5 → 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/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
@@ -336,7 +336,7 @@ return http.createServer(async (req, res) => {
336
336
  const parsed = JSON.parse(textBody.toString('utf-8'))
337
337
 
338
338
  const sessionKey = (config.stages?.includes('graph') || config.stages?.includes('read-diff'))
339
- ? deriveSessionKey(req.headers)
339
+ ? deriveSessionKey(req.headers, parsed)
340
340
  : null
341
341
  const sessionBucket = config.stages?.includes('graph') && sessionKey
342
342
  ? sessionStore.getBucket(sessionKey)
@@ -406,7 +406,7 @@ function probeSidecar(config) {
406
406
  req.on('error', () => {
407
407
  sidecarAvailable = false
408
408
  console.error('[tamp] \u26a0 llmlingua stage enabled but sidecar not running \u2014 text blocks won\u2019t compress')
409
- 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')
410
410
  })
411
411
  req.on('timeout', () => { req.destroy() })
412
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.5",
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
  }
@@ -251,7 +251,7 @@ const anthropic = {
251
251
  const OPENAI_COMPAT_SKIP_BLOCK_TYPES = new Set(['thinking', 'partial'])
252
252
 
253
253
  function extractOpenAIChatTargets(msg, i) {
254
- if (msg.role !== 'tool') return []
254
+ if (!msg || msg.role !== 'tool') return []
255
255
  // Some OpenAI-compat providers (Kimi, etc.) emit non-string tool content
256
256
  // with typed blocks like { type: 'thinking' }. Skip those.
257
257
  if (typeof msg.content !== 'string') {
@@ -393,11 +393,11 @@ const moonshot = {
393
393
 
394
394
  function extractGeminiContentTargets(content, ci) {
395
395
  const targets = []
396
- if (!content.parts?.length) return targets
396
+ if (!content || !content.parts?.length) return targets
397
397
 
398
398
  for (let pi = 0; pi < content.parts.length; pi++) {
399
399
  const part = content.parts[pi]
400
- if (!part.functionResponse?.response) continue
400
+ if (!part || !part.functionResponse?.response) continue
401
401
  const resp = part.functionResponse.response
402
402
  const text = typeof resp === 'string' ? resp : JSON.stringify(resp, null, 2)
403
403
  targets.push({
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 } = {}) {