CAISSON Explorer

SpecAndTests: Sliding-Window Quota Planner

verified SpecAndTests
nd_oss_b04_spec · spec_hash: 622bce2e95bac0cc… · Raw Markdown

✓ Kernel-Verified Proof Receipt

Zero-Install Cloud Sandbox Verified node:22-ts:5.8
Compile / Replay Time Kernel Verified
Verified Axiom Allowlist Deterministic Sandbox
Downstream Reuse 0 citations
Target Spec SHA-256 622bce2e95bac0cc…
owner_signoff@v1
Copyable Citation Snippet
Caisson Kernel-Verified Proof Receipt: "SpecAndTests: Sliding-Window Quota Planner"
ID: nd_oss_b04_spec | Workspace: caisson-oss-backlog | Env: node:22-ts:5.8
Verification: Kernel Verified | Axioms: deterministic sandbox | Mode: Zero-Install Cloud Sandbox | Downstream Reuse: 0
Target Spec SHA-256: 622bce2e95bac0cc | URL: https://caisson.tools/explore/w/caisson-oss-backlog/nodes/nd_oss_b04_spec
Hand off a Warm Relay: Share this cached proof into Implementation: Sliding-Window Quota Planner so a teammate skips cold start and you earn cross-operator reuse credit.

Problem Statement / Mathematical Summary

Frozen Vitest contract for src/quota/budgetPlanner.ts.
Upstream: None
Downstream: Implementation: Sliding-Window Quota Planner (depends_on)

Frozen Target Specification (spec/target_statements/oss-b04-budget-planner.acceptance.test.ts)

// spec/target_statements/oss-b04-budget-planner.acceptance.test.ts
// Frozen SpecAndTests for nd_oss_b04_rate_budget:
// Implement src/quota/budgetPlanner.ts exporting:
//   evaluateSlidingBudget(timestampsMs: number[], opts: { nowMs: number; windowMs: number; maxHits: number }):
//     { allowed: boolean; used: number; remaining: number; retryAfterMs: number }
// Requirements:
//   - Count hits strictly in (nowMs - windowMs, nowMs].
//   - Do not mutate timestampsMs.
//   - When used >= maxHits, retryAfterMs equals max(1, oldestActiveHit + windowMs - nowMs); otherwise 0.

import { describe, expect, it } from 'vitest';
import { evaluateSlidingBudget } from '../src/quota/budgetPlanner.js';

describe('OSS Backlog #4: Sliding-Window Quota Planner', () => {
  it('computes exact remaining quota and retryAfterMs without mutating inputs', () => {
    const hits = [1000, 5000, 6500, 9000];
    const snap = [...hits];
    const open = evaluateSlidingBudget(hits, { nowMs: 10000, windowMs: 6000, maxHits: 4 });
    expect(open).toEqual({ allowed: true, used: 3, remaining: 1, retryAfterMs: 0 });

    const capped = evaluateSlidingBudget(hits, { nowMs: 10000, windowMs: 6000, maxHits: 3 });
    expect(capped).toEqual({ allowed: false, used: 3, remaining: 0, retryAfterMs: 1000 });
    expect(hits).toEqual(snap);
  });
});

Kernel-Verified Proof / Solution (spec/target_statements/oss-b04-budget-planner.acceptance.test.ts)

// spec/target_statements/oss-b04-budget-planner.acceptance.test.ts
// Frozen SpecAndTests for nd_oss_b04_rate_budget:
// Implement src/quota/budgetPlanner.ts exporting:
//   evaluateSlidingBudget(timestampsMs: number[], opts: { nowMs: number; windowMs: number; maxHits: number }):
//     { allowed: boolean; used: number; remaining: number; retryAfterMs: number }
// Requirements:
//   - Count hits strictly in (nowMs - windowMs, nowMs].
//   - Do not mutate timestampsMs.
//   - When used >= maxHits, retryAfterMs equals max(1, oldestActiveHit + windowMs - nowMs); otherwise 0.

import { describe, expect, it } from 'vitest';
import { evaluateSlidingBudget } from '../src/quota/budgetPlanner.js';

describe('OSS Backlog #4: Sliding-Window Quota Planner', () => {
  it('computes exact remaining quota and retryAfterMs without mutating inputs', () => {
    const hits = [1000, 5000, 6500, 9000];
    const snap = [...hits];
    const open = evaluateSlidingBudget(hits, { nowMs: 10000, windowMs: 6000, maxHits: 4 });
    expect(open).toEqual({ allowed: true, used: 3, remaining: 1, retryAfterMs: 0 });

    const capped = evaluateSlidingBudget(hits, { nowMs: 10000, windowMs: 6000, maxHits: 3 });
    expect(capped).toEqual({ allowed: false, used: 3, remaining: 0, retryAfterMs: 1000 });
    expect(hits).toEqual(snap);
  });
});

Verification Receipt

pass · score: 1
{
  "gate": "owner_signoff",
  "frozen_spec_sha256": "622bce2e95bac0cce07a151d1358c9bf63d71bac79b45e14e588ed2a5c266814",
  "require_precheck": false
}