CodeSampleX

Sample

toad-cache 3.7.4: HitStatisticsRecord

Verified sample for npm toad-cache 3.7.4: HitStatisticsRecord. The contract ran on node 22 · linux debian/x64 · docker and passed.

sha256:9ef29f25556ee033d781a8ad0ed972e6473aa612c0904b7d14193cc07f968c56

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Verification-run environments

Environment Contract Stages Run
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-11-23

Case

HOW
Goal
verify toad-cache.HitStatisticsRecord in pkg:npm/toad-cache@3.7.4
Packages
Symbols
  • toad-cache.HitStatisticsRecord
Environment
node 22.23.2
Created
2026-11-23T15:51:08Z

Contract

  1. new HitStatisticsRecord() initializes an empty records map and getStatistics() returns the live records object
  2. initForCache(cacheId, timestamp) creates a zeroed 10-field statistics bucket and replaces prior timestamps for that cacheId on re-initialization
  3. resetForCache(cacheId) is a no-op for unknown cacheIds and zeroes all timestamp buckets for an existing cacheId without altering other caches
  4. sharing a HitStatisticsRecord across multiple LruObjectHitStatistics instances isolates per-cache counters under one getStatistics() map and supports per-cache resetForCache

Files

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.cjs
  • test/contract.cjs

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify toad-cache.HitStatisticsRecord in pkg:npm/toad-cache@3.7.4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/toad-cache@3.7.4
Demonstrate these symbols/APIs:
  - toad-cache.HitStatisticsRecord
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:95f071e592a17cff8059866c5b1a5a8d89613482fc23d447838151907fa1dfee","constraints":{"executionContext":"node"},"contract":["new HitStatisticsRecord() initializes an empty records map and getStatistics() returns the live records object","initForCache(cacheId, timestamp) creates a zeroed 10-field statistics bucket and replaces prior timestamps for that cacheId on re-initialization","resetForCache(cacheId) is a no-op for unknown cacheIds and zeroes all timestamp buckets for an existing cacheId without altering other caches","sharing a HitStatisticsRecord across multiple LruObjectHitStatistics instances isolates per-cache counters under one getStatistics() map and supports per-cache resetForCache"],"goal":"verify toad-cache.HitStatisticsRecord in pkg:npm/toad-cache@3.7.4","kind":"HOW","packages":["pkg:npm/toad-cache@3.7.4"],"schemaVersion":1,"symbols":["toad-cache.HitStatisticsRecord"]},"contractCommand":["node","test/contract.cjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/toad-cache@3.7.4"],"schemaVersion":1,"subject":"pkg:npm/toad-cache@3.7.4","symbols":["toad-cache.HitStatisticsRecord"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "toad-cache-hit-statistics-record-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "toad-cache-hit-statistics-record-sample",
      "version": "1.0.0",
      "dependencies": {
        "toad-cache": "3.7.4"
      }
    },
    "node_modules/toad-cache": {
      "version": "3.7.4",
      "resolved": "https://registry.npmjs.org/toad-cache/-/toad-cache-3.7.4.tgz",
      "integrity": "sha512-m1TdR/rvT7kgGJZhspNtXdsdYk0fddFpJJFlG5s+UkPFo6lkLoZ3YLOaovPYjq1R75NP5JfeTlSHaOsE09peCg==",
      "license": "MIT",
      "engines": {
        "node": ">=20"
      }
    }
  }
}
package.json
{
  "name": "toad-cache-hit-statistics-record-sample",
  "version": "1.0.0",
  "private": true,
  "description": "Minimal sample verifying toad-cache HitStatisticsRecord initialization, shared cache statistics aggregation, and per-cache reset",
  "main": "src/index.cjs",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "toad-cache": "3.7.4"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify toad-cache.HitStatisticsRecord in pkg:npm/toad-cache@3.7.4",
  "kind": "HOW",
  "packages": [
    "pkg:npm/toad-cache@3.7.4"
  ],
  "symbols": [
    "toad-cache.HitStatisticsRecord"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.cjs
'use strict';

const { HitStatisticsRecord, LruObjectHitStatistics } = require('toad-cache');

function createSharedStatisticsRecord() {
  return new HitStatisticsRecord();
}

function createTrackedCache(cacheId, sharedRecord, options = {}) {
  const {
    max = 100,
    ttlInMsecs = 0,
    statisticTtlInHours = 24,
  } = options;

  return new LruObjectHitStatistics(
    max,
    ttlInMsecs,
    cacheId,
    sharedRecord,
    statisticTtlInHours,
  );
}

module.exports = {
  HitStatisticsRecord,
  createSharedStatisticsRecord,
  createTrackedCache,
};
test/contract.cjs
'use strict';

const assert = require('node:assert/strict');
const {
  HitStatisticsRecord,
  createSharedStatisticsRecord,
  createTrackedCache,
} = require('../src/index.cjs');

const ZERO_STATS = {
  cacheSize: 0,
  hits: 0,
  falsyHits: 0,
  emptyHits: 0,
  misses: 0,
  expirations: 0,
  evictions: 0,
  invalidateOne: 0,
  invalidateAll: 0,
  sets: 0,
};

// 1. Fresh HitStatisticsRecord starts empty and getStatistics() returns live records
const directRecord = new HitStatisticsRecord();
assert.deepEqual(directRecord.records, {});
assert.equal(directRecord.getStatistics(), directRecord.records);

// 2. initForCache creates a zeroed bucket and replaces prior timestamps on re-init
directRecord.initForCache('alpha', '2026-01-01');
assert.deepEqual(directRecord.getStatistics(), {
  alpha: {
    '2026-01-01': ZERO_STATS,
  },
});

directRecord.records.alpha['2026-01-01'].hits = 4;
directRecord.initForCache('alpha', '2026-01-02');
assert.deepEqual(directRecord.getStatistics(), {
  alpha: {
    '2026-01-02': ZERO_STATS,
  },
});

// 3. resetForCache is a safe no-op for unknown cacheIds
directRecord.resetForCache('nonexistent');
assert.equal('nonexistent' in directRecord.getStatistics(), false);

// 4. resetForCache zeroes all timestamp buckets for the target cacheId only
directRecord.initForCache('beta', '2026-01-02');
directRecord.records.alpha['2026-01-02'].hits = 3;
directRecord.records.alpha['2026-01-02'].misses = 2;
directRecord.records.alpha['2026-01-03'] = { ...ZERO_STATS, sets: 5 };
directRecord.records.beta['2026-01-02'].hits = 7;

directRecord.resetForCache('alpha');
assert.deepEqual(directRecord.getStatistics().alpha, {
  '2026-01-02': ZERO_STATS,
  '2026-01-03': ZERO_STATS,
});
assert.equal(directRecord.getStatistics().beta['2026-01-02'].hits, 7);

// 5. Shared HitStatisticsRecord across multiple LruObjectHitStatistics caches
const sharedRecord = createSharedStatisticsRecord();
const cacheA = createTrackedCache('cache-a', sharedRecord, { max: 2, ttlInMsecs: 0 });
const cacheB = createTrackedCache('cache-b', sharedRecord, { max: 5, ttlInMsecs: 0 });

assert.equal(cacheA.getStatistics(), sharedRecord.getStatistics());
assert.equal(cacheB.getStatistics(), sharedRecord.getStatistics());

cacheA.set('k1', 'v1');
cacheA.set('k2', '');
cacheA.set('k3', 0); // triggers eviction of k1 (max = 2)
assert.equal(cacheA.get('k2'), ''); // hit + falsyHit + emptyHit
assert.equal(cacheA.get('k3'), 0);  // hit + falsyHit
assert.equal(cacheA.get('missing'), undefined); // miss
cacheA.delete('k3'); // invalidateOne
cacheA.clear();      // invalidateAll

cacheB.set('item', 'ok');
assert.equal(cacheB.get('item'), 'ok');

const stats = sharedRecord.getStatistics();
const [stampA] = Object.keys(stats['cache-a']);
const [stampB] = Object.keys(stats['cache-b']);
assert.ok(stampA, 'expected timestamp key for cache-a');
assert.ok(stampB, 'expected timestamp key for cache-b');

assert.deepEqual(stats['cache-a'][stampA], {
  cacheSize: 0,
  hits: 2,
  falsyHits: 2,
  emptyHits: 1,
  misses: 1,
  expirations: 0,
  evictions: 1,
  invalidateOne: 1,
  invalidateAll: 1,
  sets: 3,
});

assert.deepEqual(stats['cache-b'][stampB], {
  ...ZERO_STATS,
  cacheSize: 1,
  hits: 1,
  sets: 1,
});

// Resetting cache-a clears its counters while leaving cache-b intact
sharedRecord.resetForCache('cache-a');
assert.deepEqual(sharedRecord.getStatistics()['cache-a'][stampA], ZERO_STATS);
assert.equal(sharedRecord.getStatistics()['cache-b'][stampB].hits, 1);
assert.equal(sharedRecord.getStatistics()['cache-b'][stampB].sets, 1);

console.log('PASS: toad-cache.HitStatisticsRecord contract verified');

Origin Seeder

anonymous