MCPcopy
hub / github.com/codeaashu/claude-code / withTokenCountVCR

Function withTokenCountVCR

src/services/vcr.ts:382–406  ·  view source on GitHub ↗
(
  messages: unknown[],
  tools: unknown[],
  f: () => Promise<number | null>,
)

Source from the content-addressed store, hash-verified

380}
381
382export async function withTokenCountVCR(
383 messages: unknown[],
384 tools: unknown[],
385 f: () => Promise<number | null>,
386): Promise<number | null> {
387 // Dehydrate before hashing so fixture keys survive cwd/config-home/tempdir
388 // variation and message UUID/timestamp churn. System prompts embed the
389 // working directory (both raw and as a slash→dash project slug in the
390 // auto-memory path) and messages carry fresh UUIDs per run; without this,
391 // every test run produces a new hash and fixtures never hit in CI.
392 const cwdSlug = getCwd().replace(/[^a-zA-Z0-9]/g, '-')
393 const dehydrated = (
394 dehydrateValue(jsonStringify({ messages, tools })) as string
395 )
396 .replaceAll(cwdSlug, '[CWD_SLUG]')
397 .replace(
398 /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,
399 '[UUID]',
400 )
401 .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?/g, '[TIMESTAMP]')
402 const result = await withFixture(dehydrated, 'token-count', async () => ({
403 tokenCount: await f(),
404 }))
405 return result.tokenCount
406}
407

Callers 1

Calls 5

getCwdFunction · 0.85
dehydrateValueFunction · 0.85
jsonStringifyFunction · 0.85
withFixtureFunction · 0.85
fFunction · 0.50

Tested by

no test coverage detected