mirror of
https://github.com/dtzp555-max/olp.git
synced 2026-07-19 09:45:07 +00:00
* feat+test+docs: D64+D65+D66+D67 — olp Node CLI + olp doctor framework + per-provider doctor checks + ADR 0002 Amendment 7 Second substantive Phase 4 implementation. 4 D-days bundled per Iron Rule 11 IDR — CLI dispatches to doctor; doctor calls into provider plugins via the new contract method; ADR amendment authorizes the contract change. Single PR is the minimum reviewable unit for "does plugin amendment + plugin impl + doctor consumer line up?" ## D64 — bin/olp.mjs Node CLI scaffold Operator surface for OLP. Node not bash (per ADR 0010 § Notes — bash with python3 JSON parsing is a known fragile point; OLP standardizes on Node). Subcommands: - status / health / usage / models / cache — HTTP calls to existing endpoints - providers — local: cross-references models-registry.json + config.json - chain show [<model>] — local: prints routing.chains from ~/.olp/config.json - logs [N] [--level X] — reads ~/.olp/logs/audit.ndjson via audit-query - restart — launchctl (macOS) / systemctl --user (Linux), best-effort - keys ... — delegates to bin/olp-keys.mjs runCli (no logic duplicated) - doctor [--check <id|category>] [--json] — D65 framework - help / --help / -h Token / URL resolution: - OLP_PROXY_URL env → OLP_PORT env → http://127.0.0.1:4567 (D60 default) - OLP_API_KEY env → OLP_OWNER_TOKEN env (filesystem manifest tokens are one-way SHA-256 per ADR 0007 § 5 — not recoverable; CLI surfaces helpful 401 message pointing at olp-keys keygen) Output: - Default: human-readable ANSI-colored text (no chalk dep, auto-suppressed under --json) - --json: raw JSON for scripting - Exit codes: 0=ok / 1=usage / 2=network|HTTP / 3=auth No npm deps. Built-ins only. Installed via package.json bin entry so `npx olp <subcommand>` works. ## D65 — lib/doctor.mjs framework Ports OCP scripts/doctor.mjs (the bedrock of AI-driven self-repair per the OCP audit's #2 inheritance candidate). Machine-readable next_action so a Claude Code / Cursor / etc. agent can self-repair OLP. Check shape: { id, category, async run(): { status: 'ok'|'fail'|'warn', message, evidence? } } Built-in checks: server.running, server.version, config.exists, config.providers_enabled, config.chains_configured, auth.owner_key_exists, system.node_version. Per-provider checks collected dynamically via provider.doctorChecks() per D67. --json output: { checks: [...], kind: noop|update|fix_oauth|fix_config|fresh_install| fix_server|fix_provider, next_action: { ai_executable: [], human_required: [], verify: 'olp doctor' }, summary } --check <id-or-category> for tight repair-loop fast paths. ## D66 — Per-provider doctorChecks() implementations Each shipped plugin contributes its own checks (lives in plugin file so the provider's maintainer updates it naturally): - anthropic.mjs: cli_available (claude --version) + oauth_token_present (~/.claude/.credentials.json OR ANTHROPIC_OAUTH_TOKEN env) - codex.mjs: cli_available (codex --version) + auth_present (~/.codex/config.json) - mistral.mjs: cli_available (vibe --version) + api_key_present (MISTRAL_API_KEY env OR ~/.vibe/.env) Each fail returns evidence.fix_commands (for ai_executable[]) or evidence.human_required (e.g., 'run: claude auth login'). ## D67 — ADR 0002 Amendment 7 New amendment adds OPTIONAL provider.doctorChecks(): DoctorCheck[] to the Provider contract. Backwards compatible — plugins without doctorChecks() contribute no provider checks (default behavior). Validator extended in lib/providers/base.mjs validateProvider. ## Test count 636 → 658 (+22 tests across Suites 32, 33). - Suite 32 — bin/olp.mjs CLI scaffold (10 tests): parseArgv, USAGE, unknown-subcommand, providers local + --json, chain show, status via ephemeral server with owner token, ECONNREFUSED → exit 2, resolveBearerToken precedence - Suite 33 — lib/doctor.mjs framework (12 tests): all kind branches (noop / fresh_install / fix_server / fix_oauth / fix_provider), collectProviderChecks reads doctorChecks(), throwing plugin captured, --check filter, built-in checks against temp HOME, anthropic plugin probe set, resolveProxyUrl precedence, deriveKind/deriveNextAction units ## Scope discipline server.mjs UNTOUCHED. All HTTP subcommands consume EXISTING endpoints. No new endpoints. No /health.anonymousKey. No olp-connect. No Telegram plugin. No IDE docs bundle. No CHANGELOG / package.json version bump (Phase 4 close handles versioning; only package.json bin entries updated). ## Known limitations (flagged for reviewer) - olp restart not unit-tested (would require mocking child_process.spawn in invasive way; manual smoke-test only at this D-day) - olp logs --level filtering matches optional level field if present in audit-event objects; appendAuditEvent already populates it where meaningful — no schema change needed in this bundle - olp usage panel shape inferred from lib/audit-query.mjs exports; if /v0/management/dashboard-data wire shape differs in subtle ways, formatter degrades to '?' but --json always works ## Authority - ADR 0010 § Phase 4 D-day plan D64-D67 line - ADR 0002 Amendment 7 (this commit — new amendment) - OCP ocp bash wrapper /Users/taodeng/ocp/ocp (subcommand reference, translated to Node) - OCP scripts/doctor.mjs /Users/taodeng/ocp/scripts/doctor.mjs (framework reference) - 2026-05-26 brainstorm (Top 5 OCP inheritance candidates, item 2: olp doctor machine-readable next_action) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix: D64-D67 reviewer P2 fold-in — shell-quote ai_executable paths + launchctl kickstart caveat Reviewer APPROVE — 0 P0/P1, 2 P2 hardening notes folded in. P2-1 — Shell-quote interpolated paths in fix_commands. lib/doctor.mjs config.exists fix_commands previously interpolated ${olpHome} / ${configPath} unquoted into the printf template. A malicious OLP_HOME env value containing shell metacharacters could inject commands into the suggested-fix string an AI agent (or human) pastes back into a terminal. Added _shellQuote(s) helper (POSIX single-quote-wrap with escape for embedded single quotes per POSIX shell rules). Risk surface is narrow at family scale (operator local env, single-user proxy), but hardening cost is one helper. P2-2 — Document launchctl kickstart -k env-stale pitfall. cmdRestart header now carries an explicit caveat that `launchctl kickstart -k` does NOT re-read the plist EnvironmentVariables block — launchd uses cached env from the most recent bootstrap. This is a known OCP institutional lesson (PIT INDEX in cc-rules MEMORY.md). The comment documents the bootout/bootstrap dance for env reloads and notes that the Phase 4 installer (post-D73) will expose `olp restart --full` for the safer reload path. 658/658 tests still pass; the _shellQuote change is invisible to existing tests because the test fixtures use safe paths. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: dtzp555 <dtzp555@gmail.com> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
273 lines
11 KiB
JavaScript
273 lines
11 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} DoctorCheck
|
|
* @property {string} id - unique per check, e.g. 'anthropic.cli_available'
|
|
* @property {'provider'} category - fixed for plugin-contributed checks (ADR 0002 Amendment 7)
|
|
* @property {function} run - async () => { status: 'ok'|'fail'|'warn', message: string, evidence?: { fix_commands?: string[], human_steps?: string[], reference?: string } }
|
|
*/
|
|
|
|
/**
|
|
* @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 {function} [doctorChecks] - OPTIONAL () => DoctorCheck[] (ADR 0002 Amendment 7, D67)
|
|
* @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');
|
|
}
|
|
|
|
// ADR 0002 Amendment 7 (D67): doctorChecks() is optional. When present it must be
|
|
// a function; absence is allowed (plugin contributes no provider-tier checks to
|
|
// `olp doctor` — built-in server/system/auth checks still run).
|
|
if (p.doctorChecks !== undefined && typeof p.doctorChecks !== 'function') {
|
|
errors.push('doctorChecks must be a function or omitted');
|
|
}
|
|
|
|
if (!p.hints || typeof p.hints !== 'object') {
|
|
errors.push('hints must be an object with { requiresTTY, concurrentSpawnSafe, maxConcurrent } + optional { maxSpawnTimeMs, cacheable }');
|
|
} 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.
|
|
*
|
|
* v0.1 live codes (per ADR 0004 Amendment 3, D34 F7 and Amendment 4, D38):
|
|
* SPAWN_FAILED, CLI_NOT_FOUND, AUTH_MISSING, SPAWN_TIMEOUT, CONCURRENCY_LIMIT
|
|
*
|
|
* CONCURRENCY_LIMIT (D38, issue #1): synthesized by the orchestration layer
|
|
* (NOT thrown by provider plugins themselves) when a spawn is attempted
|
|
* against a provider already at its hints.maxConcurrent limit. The fallback
|
|
* engine treats this as a hard trigger so the chain advances to the next hop
|
|
* rather than queueing. See ADR 0002 Amendment 6 (runtime enforcement) and
|
|
* ADR 0004 Amendment 4 (CONCURRENCY_LIMIT in hard-trigger taxonomy).
|
|
*
|
|
* QUOTA_EXHAUSTED and RATE_LIMITED were removed (D34 F7): no plugin parses
|
|
* underlying-API HTTP status codes at v0.1, so these codes are never emitted.
|
|
* Re-add via ADR 0004 amendment when a plugin gains HTTP-status parsing.
|
|
* (Previously removed: OUTPUT_PARSE_ERROR, D32 F4.)
|
|
*/
|
|
export const PROVIDER_ERROR_CODES = /** @type {const} */ ([
|
|
'AUTH_MISSING',
|
|
'CLI_NOT_FOUND',
|
|
'SPAWN_FAILED',
|
|
'SPAWN_TIMEOUT', // ADR 0004 § Trigger taxonomy bullet 4: spawn timeout is a hard trigger
|
|
'CONCURRENCY_LIMIT', // ADR 0002 Amendment 6 / ADR 0004 Amendment 4 (D38, issue #1)
|
|
/* ADR 0005 Amendment 8 §8 (D57): per-client streaming queue overflow.
|
|
* NOT a hard trigger — the source spawned successfully and other attached
|
|
* clients continue to receive chunks; only this client's queue exceeded
|
|
* PER_CLIENT_QUEUE_CAP. The affected client receives a synthetic
|
|
* { type: 'stop', finish_reason: 'length' } + [DONE] terminator.
|
|
* D58 wires server-side header/log surface; HARD_TRIGGER_CODES in
|
|
* lib/fallback/engine.mjs is a whitelist so absence here gives the
|
|
* correct default (no fallback advancement). */
|
|
'STREAM_BACKPRESSURE',
|
|
]);
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|