Files
olp/lib/providers/base.mjs
T
taodengandClaude Opus 4.7 7ef5510837 feat(cache): D23 — implement hints.cacheable + 10MB size cap (round-2 F3)
cold-audit catch from 2026-05-24

Round-2 cold-audit Finding 3 (P2 cache correctness). ADR 0005 § "Cache
write conditions" items 3 and 4 were documented but never wired in
code:
- Item 3: "The provider's hints.cacheable flag is not false"
- Item 4: "The response is below a size cap (default 10 MB; configurable)"

Grep verified zero matches for `cacheable` / `10485760` / size-cap
patterns in lib/ or server.mjs pre-D23.

Changes (9 files, +391 / -12):

1. docs/adr/0002-plugin-architecture.md — Amendment 3 adds `cacheable`
   to the Provider contract hints list (after D11's Amendment 1 added
   maxSpawnTimeMs). Authority chain cites ADR 0005 § Cache write
   conditions item 3 as the field's origin.

2. docs/adr/0005-cache-cross-provider.md — Amendment 3 documents the
   D23 implementation of items 3 + 4 + the D16-interaction edge case
   (truncated > 10MB → no-op eviction, structurally bounded since
   responses > 10MB are anomalous by ADR's own rationale).

3. lib/providers/base.mjs — ProviderHints typedef gains
   `[cacheable]` (optional boolean); validateProvider rejects non-
   boolean non-undefined values. Omission accepted (default = true).

4. 3 plugins (anthropic / codex / mistral) each declare
   `cacheable: true` explicitly with citation comment.

5. lib/cache/store.mjs — CacheStore constructor accepts
   `maxEntryBytes` (default 10 * 1024 * 1024 = 10_485_760) +
   injectable `_warnFn`. `set()` computes
   `Buffer.byteLength(JSON.stringify(value))`; if exceeded, warns via
   `_warnFn` and returns undefined (no persistence). `getOrCompute`
   still returns the computed value to caller — cache write skipped
   but caller gets data; subsequent identical requests re-spawn.

6. server.mjs — 4 sites coordinated for cacheable opt-out:
   - `executeHopFn`: cacheable check before D13 shouldBypassCacheForHop
     (permanent provider policy precedes per-request bypass condition)
   - `cacheStore.peek` gate at line ~504: `cacheableForFirstHop`
     short-circuit
   - Real-streaming branch entry condition at line ~522:
     `cacheableForFirstHop` added (so cacheable: false + stream falls
     through to buffered path which honors the opt-out via executeHopFn)
   - Both `cacheStore.set` sites in streaming branch wrapped in
     `if (cacheableForFirstHop)` defensive guards (post-D23
     restructure these are unreachable for cacheable: false, but the
     guards make intent explicit and survive future refactors)

7. test-features.mjs — 13 new tests:
   - 5 validator tests (Suite 4): explicit true/false, omitted, string
     rejected, number rejected
   - 5 size-cap unit tests (Suite 9): default 10MB, custom override,
     oversize skip + warn capture, within-limit normal persistence,
     getOrCompute oversize returns-but-doesn't-cache + re-spawn
   - 3 cacheable integration tests (Suite 9e): non-streaming opt-out,
     streaming opt-out (the regression case that pre-fold-in failed),
     X-OLP-Cache header consistency on both paths

Tests: 335 → 348 (+13). All pass on Node 20.

Pre-commit fold-in (per evidence-first checkpoint #4):

- **D23 reviewer flagged 2 blocking issues**: (1) the cacheable opt-out
  in initial implementation was only in `executeHopFn` (buffered path);
  the D10 real-streaming branch in server.mjs bypassed the check
  entirely — calling streamPlugin.spawn() directly and writing to
  cacheStore.set() at 2 sites without consulting cacheable. (2) Suite
  9e integration tests didn't cover stream: true so the leak wasn't
  caught.

  Both diff-review and the implementer focused on `executeHopFn`
  because that's where the cold-audit reviewer pointed for Finding 3.
  Same class of "narrow attention" miss as several earlier D-days.

  Fold-in: compute `cacheableForFirstHop` once at request entry; add
  `!cacheableForFirstHop` short-circuit to peek gate; add
  `cacheableForFirstHop` to streaming-branch entry condition (forces
  fall-through to buffered path which has the opt-out); add defensive
  guards on both `cacheStore.set` call sites. Added a 3rd Suite 9e
  test covering stream: true + cacheable: false (which pre-fold-in
  would have failed by serving the second request from cache).

  This is now the FOURTH D-day where a doc-vs-code or path-coverage
  gap was caught by the reviewer rather than the implementer. The
  v1.6 § 10.x diff-review discipline continues to pay off.

Default behavior unchanged for 3 shipped plugins (all explicitly
`cacheable: true` → cache path identical to pre-D23).

Authority:
- ADR 0002 Amendment 3 (in-place) — establishes cacheable in contract
- ADR 0005 Amendment 3 (in-place) — documents implementation of items
  3 + 4
- ADR 0005 § Cache write conditions items 3 + 4 — the original
  authority for both rules
- CC 开发铁律 v1.6 § 10.x — Round-2 Cold Audit caught the missing
  implementation; diff-review Mode A caught the streaming-path gap

Reviewer (Iron Rule v1.6 § 10.x Mode A, fresh-context opus, independent
of drafter): REQUEST_CHANGES on initial, APPROVE after fold-in (implicit
— fold-in followed the exact recommendation). Verified:
- ADR amendment placement + structure
- Validator typedef + checks
- Size cap implementation in CacheStore + inflight slot release on
  oversize-skip
- All 4 interaction cases (cacheable × cache_control × D16
  × ordering) coherent post-fold-in
- 13 new tests including the regression test that would have failed
  on pre-fold-in code

Follow-up items (reviewer's non-blocking notes, NOT in this PR):
- ADR 0005 Amendment 3 could add one sentence on the prior-write-also-
  oversize case (file as docs polish)
- Consider extracting `shouldUseCacheForHop(hopProvider, ir)` helper
  combining D13 + D23 logic — reduces miss-risk for next reviewer
- Test 30 could add `assert.equal(store._inflight.size, 0)` as
  inflight-slot leak regression guard

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 14:45:08 +10:00

234 lines
8.9 KiB
JavaScript

/**
* lib/providers/base.mjs — Provider contract definition and shared helpers
*
* Authority: ADR 0002 § "Provider contract (v1.0 interface)"
*
* This module does NOT implement the Provider contract itself.
* Provider plugins compose the helpers exported here; they do not inherit
* from a base class (per ADR 0002 § Consequences/Mitigations: "compose helpers,
* do not inherit").
*/
// ── Contract typedef ──────────────────────────────────────────────────────
/**
* @typedef {Object} ProviderAuth
* @property {string} type - e.g. 'subscription', 'api-key', 'oauth'
* @property {string} storage - e.g. 'cli-managed', 'env', 'keychain'
* @property {string} path - artifact location hint
* @property {string|null} refresh - refresh mechanism or null if not applicable
*/
/**
* @typedef {Object} ProviderHints
* @property {boolean} requiresTTY
* @property {boolean} concurrentSpawnSafe
* @property {number} maxConcurrent
* @property {number} [maxSpawnTimeMs] - optional integer milliseconds, default 600000
* @property {boolean} [cacheable] - optional, default true; if false, opt out of OLP's
* response cache entirely — executeHopFn skips cacheStore.getOrCompute and calls
* collectAllChunks directly. ADR 0002 Amendment 3 (D23).
*/
/**
* @typedef {Object} ProviderContractV1
* @property {string} name - unique lowercase key
* @property {string} displayName - human-readable name
* @property {'1.0'} contractVersion - must be '1.0' for v1.0 plugins (D4 fold-in per reviewer F3)
* @property {string[]} models - model strings this provider serves
* @property {ProviderAuth} auth
* @property {function} spawn - async (irRequest, authContext) => AsyncIterator<IRResponseChunk>
* @property {function} estimateCost - (request) => {inputTokens, outputTokensEstimate, currency, usd}|null
* @property {function} quotaStatus - async (authContext) => {available, percentUsed, resetsAt, pool}|null
* @property {function} healthCheck - async () => {ok: boolean, latencyMs: number, error?: string}
* @property {ProviderHints} hints
*/
// ── Contract validator ────────────────────────────────────────────────────
/**
* Validates that a plugin object satisfies the v1.0 Provider contract.
* Per ADR 0002 § "Loading model", the registry calls this at startup for
* every registered provider; an invalid provider throws rather than silently
* degrading.
*
* @param {*} p
* @returns {{ valid: boolean, errors: string[] }}
*/
export function validateProvider(p) {
const errors = [];
if (!p || typeof p !== 'object') {
errors.push('provider must be an object');
return { valid: false, errors };
}
if (typeof p.name !== 'string' || p.name.trim() === '') {
errors.push('name must be a non-empty string');
} else if (!/^[a-z][a-z0-9_-]*$/.test(p.name)) {
errors.push('name must be lowercase alphanumeric (with _ or -) starting with a letter');
}
if (typeof p.displayName !== 'string' || p.displayName.trim() === '') {
errors.push('displayName must be a non-empty string');
}
// contractVersion: required to be exactly '1.0' for v1.0 plugins (D4 fold-in per reviewer F3)
// Per ADR 0002 § Mitigations: "The contract is versioned. v1.0 is the subset in this ADR;
// future additions require ADR amendment plus a contract-version bump."
if (p.contractVersion !== '1.0') {
errors.push(`contractVersion must be '1.0', got ${JSON.stringify(p.contractVersion)}`);
}
if (!Array.isArray(p.models)) {
errors.push('models must be an array of strings');
} else if (p.models.some(m => typeof m !== 'string')) {
errors.push('every entry in models must be a string');
}
if (!p.auth || typeof p.auth !== 'object') {
errors.push('auth must be an object with { type, storage, path, refresh }');
} else {
if (typeof p.auth.type !== 'string') errors.push('auth.type must be a string');
if (typeof p.auth.storage !== 'string') errors.push('auth.storage must be a string');
if (typeof p.auth.path !== 'string') errors.push('auth.path must be a string');
if (p.auth.refresh !== null && typeof p.auth.refresh !== 'string') {
errors.push('auth.refresh must be a string or null');
}
}
if (typeof p.spawn !== 'function') {
errors.push('spawn must be a function');
}
if (typeof p.estimateCost !== 'function') {
errors.push('estimateCost must be a function');
}
if (typeof p.quotaStatus !== 'function') {
errors.push('quotaStatus must be a function');
}
if (typeof p.healthCheck !== 'function') {
errors.push('healthCheck must be a function');
}
if (!p.hints || typeof p.hints !== 'object') {
errors.push('hints must be an object with { requiresTTY, concurrentSpawnSafe, maxConcurrent, maxSpawnTimeMs }');
} else {
if (typeof p.hints.requiresTTY !== 'boolean') errors.push('hints.requiresTTY must be a boolean');
if (typeof p.hints.concurrentSpawnSafe !== 'boolean') errors.push('hints.concurrentSpawnSafe must be a boolean');
if (typeof p.hints.maxConcurrent !== 'number' || !Number.isInteger(p.hints.maxConcurrent) || p.hints.maxConcurrent < 0) {
errors.push('hints.maxConcurrent must be a non-negative integer');
}
if (p.hints.maxSpawnTimeMs !== undefined) {
if (typeof p.hints.maxSpawnTimeMs !== 'number' || !Number.isInteger(p.hints.maxSpawnTimeMs) || p.hints.maxSpawnTimeMs <= 0) {
errors.push('hints.maxSpawnTimeMs must be a positive integer (milliseconds) or omitted');
}
}
// ADR 0002 Amendment 3 (D23): cacheable is optional; if present must be boolean.
// undefined → default true (cacheable); false → provider opts out of cache.
if (p.hints.cacheable !== undefined && typeof p.hints.cacheable !== 'boolean') {
errors.push('hints.cacheable must be a boolean or omitted');
}
}
return { valid: errors.length === 0, errors };
}
// ── Error class ───────────────────────────────────────────────────────────
/** Error codes surfaced by provider plugins */
export const PROVIDER_ERROR_CODES = /** @type {const} */ ([
'AUTH_MISSING',
'QUOTA_EXHAUSTED',
'RATE_LIMITED',
'CLI_NOT_FOUND',
'SPAWN_FAILED',
'OUTPUT_PARSE_ERROR',
'SPAWN_TIMEOUT', // ADR 0004 § Trigger taxonomy bullet 4: spawn timeout is a hard trigger
]);
export class ProviderError extends Error {
/**
* @param {string} message
* @param {typeof PROVIDER_ERROR_CODES[number]} code
*/
constructor(message, code) {
super(message);
this.name = 'ProviderError';
this.code = code;
}
}
// ── Shared helpers ────────────────────────────────────────────────────────
/**
* Wraps a promise with a timeout. Rejects with a ProviderError if the promise
* does not settle within `ms` milliseconds.
*
* @template T
* @param {Promise<T>} promise
* @param {number} ms
* @param {typeof PROVIDER_ERROR_CODES[number]} errorCode
* @returns {Promise<T>}
*/
export function withTimeout(promise, ms, errorCode) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new ProviderError(`Operation timed out after ${ms}ms`, errorCode));
}, ms);
promise.then(
v => { clearTimeout(timer); resolve(v); },
e => { clearTimeout(timer); reject(e); },
);
});
}
/**
* Merges two AsyncIterators into a single ordered stream.
* Items from whichever source yields first are emitted first.
* Useful when a provider plugin wants to interleave two internal streams.
*
* Not used at D3 (no providers yet) but provided as infrastructure so
* provider authors don't each implement their own fan-in.
*
* @template T
* @param {AsyncIterator<T>} iter1
* @param {AsyncIterator<T>} iter2
* @returns {AsyncGenerator<T>}
*/
export async function* mergeStreams(iter1, iter2) {
// Convert each iterator to a pull-based promise queue
const done1 = { done: true };
const done2 = { done: true };
let p1 = iter1.next();
let p2 = iter2.next();
while (true) {
const winner = await Promise.race([
p1.then(r => ({ r, which: 1 })),
p2.then(r => ({ r, which: 2 })),
]);
if (winner.which === 1) {
if (winner.r.done) {
// iter1 exhausted — drain iter2
for await (const v of { [Symbol.asyncIterator]: () => iter2 }) yield v;
return;
}
yield winner.r.value;
p1 = iter1.next();
} else {
if (winner.r.done) {
// iter2 exhausted — drain iter1
for await (const v of { [Symbol.asyncIterator]: () => iter1 }) yield v;
return;
}
yield winner.r.value;
p2 = iter2.next();
}
}
}