@sliday/tamp 0.8.1 → 0.8.4

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/compress.js CHANGED
@@ -37,8 +37,11 @@ function deduplicateTargets(targets) {
37
37
  const prev = seen.get(key)
38
38
  if (prev && prev.text === target.text) {
39
39
  const ref = `[see tool_result in message ${prev.path[1]}, block ${prev.index} — identical content]`
40
- target.compressed = ref
41
- target.dedup = true
40
+ // Never expand: a tiny duplicate can be shorter than the reference itself.
41
+ if (ref.length < target.text.length) {
42
+ target.compressed = ref
43
+ target.dedup = true
44
+ }
42
45
  } else {
43
46
  seen.set(key, target)
44
47
  }
package/index.js CHANGED
@@ -6,7 +6,7 @@ import * as fzstd from 'fzstd'
6
6
  import { loadConfig } from './config.js'
7
7
  import { compressRequest } from './compress.js'
8
8
  import { detectProvider, detectOpenAIAuthMode, resolveOpenAIUpstream } from './providers.js'
9
- import { createSession, formatRequestLog } from './stats.js'
9
+ 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'
@@ -95,6 +95,41 @@ export function createProxy(overrides = {}, loadOpts = {}) {
95
95
  return { config, session, sessionStore, readCache, brCache, server: _createServer(config, session, sessionStore, readCache, brCache) }
96
96
  }
97
97
 
98
+ // Stream-decompress a zstd body, aborting as soon as the running output size
99
+ // crosses `limit`. fzstd's one-shot decompress() inflates the entire frame
100
+ // into memory before any size check, so a small "bomb" frame could OOM the
101
+ // proxy; streaming bounds peak memory to ~limit + one block. Throws on excess.
102
+ export function decompressZstd(bytes, limit) {
103
+ const input = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)
104
+ const parts = []
105
+ let total = 0
106
+ const dctx = new fzstd.Decompress((chunk) => {
107
+ total += chunk.length
108
+ if (total > limit) throw new Error('zstd decompression exceeded size limit')
109
+ parts.push(chunk)
110
+ })
111
+ dctx.push(input, true)
112
+ const out = Buffer.allocUnsafe(total)
113
+ let offset = 0
114
+ for (const p of parts) { out.set(p, offset); offset += p.length }
115
+ return out
116
+ }
117
+
118
+ // Surface an upstream connection error to the client. Before any response
119
+ // bytes are sent we can emit a clean 502 JSON body. Once the response has
120
+ // started streaming (headersSent) or finished (writableEnded), appending JSON
121
+ // would corrupt the (possibly SSE) stream the client is reading, so we tear
122
+ // the socket down instead. Returns true if a JSON error body was written.
123
+ export function sendUpstreamError(res) {
124
+ if (res.headersSent || res.writableEnded) {
125
+ res.destroy()
126
+ return false
127
+ }
128
+ res.writeHead(502, { 'Content-Type': 'application/json' })
129
+ res.end(JSON.stringify({ error: 'upstream_error', message: 'failed to connect to upstream' }))
130
+ return true
131
+ }
132
+
98
133
  function _createServer(config, session, sessionStore, readCache, brCache) {
99
134
  const log = createRequestLogger(config)
100
135
  const disclosureTotals = { rehydrated: 0, missed: 0, disclosed: 0 }
@@ -118,10 +153,7 @@ function openUpstream(method, upstreamUrl, headers, res) {
118
153
 
119
154
  upstream.on('error', (err) => {
120
155
  log(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
121
- if (!res.headersSent) {
122
- res.writeHead(502, { 'Content-Type': 'application/json' })
123
- }
124
- res.end(JSON.stringify({ error: 'upstream_error', message: 'failed to connect to upstream' }))
156
+ sendUpstreamError(res)
125
157
  })
126
158
 
127
159
  res.on('error', (err) => {
@@ -209,7 +241,7 @@ return http.createServer(async (req, res) => {
209
241
  const provider = detectProvider(req.method, req.url, req.headers)
210
242
 
211
243
  if (!provider) {
212
- log(`[tamp] ${req.method} ${req.url}`)
244
+ log(`[tamp] ${req.method} ${redactUrl(req.url)}`)
213
245
  const upstreamUrl = buildUpstreamUrl(req.url, config.upstream)
214
246
  return pipeRequest(req, res, upstreamUrl)
215
247
  }
@@ -224,7 +256,11 @@ return http.createServer(async (req, res) => {
224
256
  let size = 0
225
257
  let overflow = false
226
258
 
227
- for await (const chunk of req) {
259
+ // destroyOnReturn:false so breaking out on overflow does NOT destroy the
260
+ // request stream — pipeRequest() still needs to forward the unread remainder
261
+ // to the upstream. Without this, an over-maxBody body is truncated while its
262
+ // original content-length is preserved, hanging the upstream.
263
+ for await (const chunk of req.iterator({ destroyOnReturn: false })) {
228
264
  size += chunk.length
229
265
  chunks.push(chunk)
230
266
  if (size > config.maxBody) {
@@ -263,11 +299,7 @@ return http.createServer(async (req, res) => {
263
299
  textBody = zlib.brotliDecompressSync(rawBody, { maxOutputLength: MAX_DECOMPRESSED })
264
300
  decompressed = true
265
301
  } else if (encoding === 'zstd') {
266
- const decompressedBytes = fzstd.decompress(new Uint8Array(rawBody))
267
- if (decompressedBytes.length > MAX_DECOMPRESSED) {
268
- throw new Error('zstd decompression exceeded size limit')
269
- }
270
- textBody = Buffer.from(decompressedBytes)
302
+ textBody = decompressZstd(rawBody, MAX_DECOMPRESSED)
271
303
  decompressed = true
272
304
  } else if (encoding && encoding !== 'identity') {
273
305
  // Unknown encoding — can't decompress, passthrough as-is
package/lib/br-cache.js CHANGED
@@ -123,6 +123,10 @@ export function createBrCache({ cacheDir = defaultCacheDir(), maxBytes = DEFAULT
123
123
  }
124
124
 
125
125
  pruneInternal()
126
+ // If this entry alone exceeds maxBytes, pruneInternal() just evicted it.
127
+ // Don't claim persistence we can't honour — a phantom record would leave
128
+ // the caller with a brHash that get() can never resolve.
129
+ if (!existsSync(finalPath)) return null
126
130
  return { hash, storedBytes: byteLen, brotliBytes }
127
131
  }
128
132
 
@@ -144,9 +144,11 @@ export async function compressFile(filePath, options = {}) {
144
144
  }
145
145
  }
146
146
 
147
- // Create backup
147
+ // Create backup. Never clobber an existing backup — it holds the pristine
148
+ // original from a prior run, and overwriting it with already-compressed
149
+ // content would make the original unrecoverable.
148
150
  const backupPath = filePath + '.bak'
149
- if (!options.noBackup) {
151
+ if (!options.noBackup && !existsSync(backupPath)) {
150
152
  try {
151
153
  await copyFile(filePath, backupPath)
152
154
  } catch (error) {
@@ -34,7 +34,9 @@ export default {
34
34
  if (LEGACY_PROGRESS.test(line)) continue;
35
35
  // BuildKit "in-progress" duplicated step lines end with a fractional second
36
36
  // marker and are re-emitted as DONE later; strip the non-DONE variants.
37
- if (/^#\d+\s+\S.*\s\d+\.\d+s$/.test(line) && !/\sDONE\b/.test(line)) continue;
37
+ // Never strip a line carrying error/warning/failure context — a RUN step's
38
+ // stdout can legitimately end in a duration (e.g. "build failed in 2.3s").
39
+ if (/^#\d+\s+\S.*\s\d+\.\d+s$/.test(line) && !/\sDONE\b/.test(line) && !/\b(error|warning|fail)/i.test(line)) continue;
38
40
  out.push(line);
39
41
  }
40
42
 
@@ -6,7 +6,9 @@
6
6
  // the Tests/Suites/Time/Snapshots summary and any stack traces.
7
7
 
8
8
  const PASS_LINE = /^\s*PASS\s+\S+/;
9
- const FAIL_LINE = /^\s*FAIL\s+\S+/;
9
+ // `m` flag is required: the all-pass gate scans the whole multi-line output,
10
+ // and a FAIL line is rarely the first line (jest interleaves PASS suites).
11
+ const FAIL_LINE = /^\s*FAIL\s+\S+/m;
10
12
  const JEST_SIG = /(^|\n)(PASS\s+\S+|FAIL\s+\S+|Tests:\s+.*\d+\s+(passed|failed|total)|Test Suites:\s+\d)/;
11
13
 
12
14
  export default {
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "sidecar/server.py",
15
15
  "sidecar/requirements.txt"
16
16
  ],
17
- "version": "0.8.1",
17
+ "version": "0.8.4",
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/stats.js CHANGED
@@ -9,10 +9,30 @@ const c = {
9
9
  red: '\x1b[31m',
10
10
  }
11
11
 
12
+ // Strip secret values from a request URL before logging. Some providers
13
+ // (Gemini and OpenAI-compatible gateways) carry the API key in the query
14
+ // string (?key=, ?api_key=), which would otherwise land in stdout, log
15
+ // files, and the systemd journal. Path-relative URLs only — no base needed.
16
+ const SECRET_QUERY_PARAM = /^(key|api_key|apikey|access_token|token)$/i
17
+ export function redactUrl(url) {
18
+ if (typeof url !== 'string') return url
19
+ const q = url.indexOf('?')
20
+ if (q === -1) return url
21
+ const path = url.slice(0, q)
22
+ const query = url.slice(q + 1)
23
+ const redacted = query.split('&').map((pair) => {
24
+ const eq = pair.indexOf('=')
25
+ if (eq === -1) return pair
26
+ const name = pair.slice(0, eq)
27
+ return SECRET_QUERY_PARAM.test(name) ? `${name}=REDACTED` : pair
28
+ }).join('&')
29
+ return `${path}?${redacted}`
30
+ }
31
+
12
32
  export function formatRequestLog(stats, session, providerName, url, bodySize, tokenCost, sessionBucket) {
13
33
  const compressed = stats.filter(s => s.method)
14
34
  const label = providerName || 'anthropic'
15
- const path = url || '/v1/messages'
35
+ const path = redactUrl(url) || '/v1/messages'
16
36
  const sizeInfo = bodySize ? ` ${c.dim}${fmtSize(bodySize)}${c.reset}` : ''
17
37
 
18
38
  if (!compressed.length && !stats.length) {