mirror of
https://github.com/dtzp555-max/olp.git
synced 2026-07-19 09:45:07 +00:00
cold-audit catch from 2026-05-24 (round 4) Round-4 cold-audit cleanup batch. 7 items grouped per IDR cleanup-batch convention (D19/D20/D25/D26/D30/D31 precedent). 2 P2 + 3 ADR amendments + 2 small docs/code cleanups. F10 (P3 semantic edge case) filed separately as GitHub issue #8. Changes (8 files, +200 / -38): **P2 fixes** 1. **F3 — README missing 6 provider auth env vars** (README.md): D30 fixed the `OLP_*` table but missed the auth-bearing env vars actually read by plugin code: - `CLAUDE_CODE_OAUTH_TOKEN` (anthropic.mjs:84) — highest-precedence override; bypasses keychain + .credentials.json file lookup - `OPENAI_CODEX_AUTH_PATH` (codex.mjs:163) — overrides full auth file path; when set, no other path tried - `CODEX_HOME` (codex.mjs:176) — overrides base dir; default auth path becomes `$CODEX_HOME/auth.json` - `MISTRAL_API_KEY` (mistral.mjs:260) — directly supplies API key; highest precedence per Mistral DOCS-2 - `MISTRAL_VIBE_AUTH_PATH` (mistral.mjs:265) — overrides full .env path; evaluated only when MISTRAL_API_KEY absent - `VIBE_HOME` (mistral.mjs:277) — overrides Vibe base dir; default auth path becomes `$VIBE_HOME/.env` Real onboarding-blocker fix: new users couldn't run OLP without knowing about these env vars; README now documents them in a "Per-provider auth env vars" subsection. 2. **F8 — Early-return error paths missing X-OLP-* headers** (server.mjs): ADR 0004 § Observability + README claim "every response carries the 5 X-OLP-* headers" but 503 (no-enabled-providers), 415 (wrong Content-Type), and early 400s (bad JSON, IR validation) emitted only Content-Type + Content-Length + X-OLP-Latency-Ms (D18 only wired the chain-exhausted path). Operators debugging these paths got less info than the ADR promised. New helper `olpErrorHeaders({ startMs, model })` emits the canonical "no provider attempted" defaults: X-OLP-Provider-Used: 'none' X-OLP-Model-Used: model ?? 'unknown' X-OLP-Fallback-Hops: '0' X-OLP-Cache: 'bypass' X-OLP-Latency-Ms: <delta> 6 sendError call sites updated: 415 / 400-bad-JSON / 400-IR-parse (model: undefined → 'unknown') + 503-no-providers / 503-provider- disappeared / 500-engine-programming (model: ir.model). The 502 streaming-error-before-first-chunk path correctly stays on `olpHeaders(...)` since a provider WAS attempted. **ADR amendments** 3. **F2 — ADR 0003 model-mapping example correction** (docs/adr/0003, Amendment 2): the § Required fields example claimed `claude-sonnet-4-6` → `claude-sonnet-4-6-20260301` mapping happens in the provider plugin. This was wrong: per D17 SPOT decision (commitcb86807), `irRequest.model` is passed verbatim to the CLI; each provider CLI accepts its own aliases natively. Both inline correction (in § Decision) AND new Amendment 2 (in § Amendments) added. 4. **F4 — OUTPUT_PARSE_ERROR dead code removal** (base.mjs, engine.mjs): `PROVIDER_ERROR_CODES.OUTPUT_PARSE_ERROR` and `HARD_TRIGGER_CODES.OUTPUT_PARSE_ERROR = true` were registered but ZERO plugins emit OUTPUT_PARSE_ERROR. ADR 0004 § Trigger taxonomy enumerates 4 hard-trigger categories (5xx, quota 4xx, exit-code, spawn-timeout) — OUTPUT_PARSE_ERROR was a 5th never authorized by ADR. Removed from both enums with removal-reason comments for auditability. Re-add via ADR 0004 amendment if a future plugin surfaces parse failures (cheaper than authoring an amendment for dead code now). 5. **F5 — ADR 0002 Amendment 4 ratifying contractVersion** (docs/adr/0002): `lib/providers/base.mjs:76-81` enforces `p.contractVersion === '1.0'` and all 3 plugins declare it, but ADR 0002 § Provider contract field list never named it. Same governance-violation class as D11's `maxSpawnTimeMs` retroactive sync (Amendment 1). Amendment 4 ratifies contractVersion as a required v1.0 contract field; § Provider contract field list updated to 10 fields (was 9). **Small cleanups** 6. **F7 — README API Endpoints table 📋 markers** (README.md): added Status column with ✅ Shipped (3 rows: /v1/chat/completions, /v1/models, /health) and 📋 Planned (3 rows: /cache/stats Phase 5, /v0/management/ quota Phase 6, /dashboard Phase 6). Matches D20's Implementation Status table convention. 7. **F9 — Codex parser inline assumption labels** (codex.mjs): added 5 inline `// A4:` comments on each defensive branch in `codexChunkToIR`. Pre-D32 these branches had only the top-of-function block comment; ALIGNMENT.md Speculative-Candidate condition 5 promises future implementers can grep assumption labels to find every speculative branch — D32 honors that promise for codex.mjs (mistral.mjs already had this pattern from D8). **Tests** (test-features.mjs): - 17g retitled + assertion expanded to full 5-header set (was partial) - 17h new: 415 wrong Content-Type → 5 X-OLP-* headers with 'unknown' model - 17i new: 503 no-enabled-providers → 5 X-OLP-* headers with ir.model - OUTPUT_PARSE_ERROR test removed (replaced with comment for audit trail) Test count: 400 → 401 (-1 OUTPUT_PARSE_ERROR + 2 new F8 + 1 retitled existing = net +1). **F10 filed as issue, NOT in D32**: https://github.com/dtzp555-max/olp/issues/8 — "Soft-skip + chain- exhausted: X-OLP-Provider-Used semantics ambiguity". Semantic edge case requiring soft trigger reactivation (deferred to v1.x per D22 Amendment 2); not a v0.1 user-affecting issue. Pre-commit fold-in (per evidence-first checkpoint #4): - **D32 reviewer caught a doc-accuracy bug**: README's CLAUDE_CODE_OAUTH_TOKEN row described the no-env fallback chain as "Searches keychain first, then `~/.claude/.credentials.json`" — but the actual code in anthropic.mjs:82-112 checks file FIRST, then keychain (darwin-only). Order was reversed. Folded in: corrected to "Searches `~/.claude/.credentials.json` first, then macOS keychain (darwin only)". Same class of doc-accuracy mistake as the D25 → D31 / D27 → D31 pattern: D-batch reviewer catches docs that contradict source. Reviewer (Iron Rule v1.6 § 10.x Mode A, fresh-context opus, independent of drafter): APPROVE_WITH_MINOR. Independently verified: - All 6 F3 env vars: precedence chains traced through plugin source (caught the anthropic reversed-order — folded in) - F8 olpErrorHeaders helper correctly distinguishes "no provider" defaults from olpHeaders' "provider attempted" defaults - F8: all 6 sendError call sites use the right model arg (undefined pre-IR-parse → 'unknown'; ir.model post-IR-parse) - F8: 502 streaming-error stays on olpHeaders (provider was attempted) - F4: OUTPUT_PARSE_ERROR removed from both enums, zero plugin emit references remain, test cleanup is auditable - F5: ADR 0002 Amendment 4 matches Amendment 1's retroactive-sync structure; contract field list now 10 items - F2: D17 commitcb86807verified to match the SPOT decision claim - F7/F9: docs/code cleanups verified - F10 issue #8 verified OPEN with correct title - 401/401 tests pass 3 non-blocking suggestions (anthropic precedence column folded; F2 inline mistral `--model` caveat could be added; F4 wording asymmetry between base.mjs and engine.mjs comments) — minor polish, not folded. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
235 lines
9.0 KiB
JavaScript
235 lines
9.0 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 removed (D32 F4): no plugin emits it; dead code.
|
|
// Re-add via ADR 0004 amendment if a future plugin surfaces parse failures.
|
|
'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();
|
|
}
|
|
}
|
|
}
|