mirror of
https://github.com/dtzp555-max/olp.git
synced 2026-07-19 09:45:07 +00:00
Folds in three production-blocking defects from external Codex review
round-3 of Phase 1 (D5+D6+D8+D9 already merged). Without these, OLP
returns 503 on every request even when `npm start` succeeds, streams
arrive as a single buffered burst after the spawn completes, and hung
CLIs block the engine forever despite ADR 0004 declaring spawn-timeout
a hard trigger.
P1.1 providers.enabled config wiring (ADR 0002 § Disable model):
- loadFallbackConfigSync() returns tri-field {chains, soft_triggers,
providersEnabled}; server.mjs reads _startupConfig.providersEnabled
and passes to loadProviders() at startup
- Empty / missing config → 0 enabled providers → 503 no_enabled_provider
(matches v0.1 0-Enabled posture per ALIGNMENT.md § Provider Inventory)
- __setProvidersEnabled / __resetProvidersEnabled test seams; in-place
Map mutation preserves existing direct-mutation patterns in Suite 13
P1.2 Real SSE streaming on single-hop cache-miss (ADR 0003 entry adapter
pattern, OpenAI /v1/chat/completions stream=true contract):
- New handleChatCompletions branch when ir.stream === true && chain.length
=== 1 && !bypassCache && !preCheckHit
- for await (const irChunk of provider.spawn(...)) writes SSE per chunk
via res.write(irChunkToOpenAISSE(...)); accumulates streamedChunks for
cacheStore.set on stop
- First-chunk rule preserved: error-before-first-chunk → sendError(502);
error-after-first-chunk → truncated res.end(), no fallback
- Multi-hop chains (chain.length > 1) continue to use buffered
executeWithFallback to keep fallback safety semantics
P1.3 Spawn timeout hard trigger (ADR 0004 § Trigger taxonomy bullet 4):
- SPAWN_TIMEOUT added to PROVIDER_ERROR_CODES (lib/providers/base.mjs)
and HARD_TRIGGER_CODES (lib/fallback/engine.mjs)
- All three plugins (anthropic.mjs / codex.mjs / mistral.mjs) wrap drain
loop with setTimeout (default 600_000ms, configurable via
hints.maxSpawnTimeMs); on fire: proc.kill('SIGTERM') + reject pending
drain promise with ProviderError(..., 'SPAWN_TIMEOUT')
- Timer cleared in finally; resolveNext/rejectNext atomically nulled in
push() + timer-fire path to prevent late-fire double-settle
Tests 277 → 288 (+11). Suite 14 (4 providers.enabled), Suite 15
(3 streaming cache-miss real-time, including arrival-count >= 2
assertion that architecturally proves real streaming), Suite 16
(4 spawn timeout, including 2-hop chain advancement from timed-out
primary). 288/288 pass on Node 20.20.2 + Node 25.8.0.
Authorities:
- ADR 0002 § Disable model — config toggle, not plugin-removal
https://github.com/dtzp555-max/olp/blob/main/docs/adr/0002-plugin-architecture.md
- ADR 0003 § Translation direction model — entry adapter for await pattern
https://github.com/dtzp555-max/olp/blob/main/docs/adr/0003-intermediate-representation.md
- ADR 0004 § Trigger taxonomy + § Fallback safety (first-chunk rule)
https://github.com/dtzp555-max/olp/blob/main/docs/adr/0004-fallback-engine.md
- OpenAI /v1/chat/completions stream=true (server-sent events,
data: {chunk} per delta, terminator data: [DONE])
https://platform.openai.com/docs/api-reference/chat/streaming
Reviewer (Iron Rule 10): fresh-context opus, independent of drafter.
Verdict: APPROVE_WITH_MINOR. Folded the one cheap minor before commit
(Suite 15a arrival-count assertion strengthened from chunks.length > 0
to arrivalTimestamps.length >= 2 — the prior assertion would have
admitted a buffered impl). Two remaining non-blocking notes deferred:
optional writeHead deferral (low value; single-hop guard makes pre-
content 200 + empty body safe), and version bump (Phase 1 ships as
v0.1.0 aggregate when D11–D16 land).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
220 lines
8.0 KiB
JavaScript
220 lines
8.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
|
|
*/
|
|
|
|
/**
|
|
* @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 }');
|
|
} 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');
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|