osborn 0.9.278 → 0.9.280

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,56 +0,0 @@
1
- /**
2
- * key-health.ts — a tiny, REAL "can this provider actually answer?" self-test for
3
- * the no-Claude fallback tiers (issue #87, gap 5).
4
- *
5
- * The bug this surfaces: applyAuthFallback() picks a tier because a key is
6
- * PRESENT, but presence is not validity — the observed test account had a z.ai
7
- * key that failed and an OpenRouter key that had expired, and nothing said so;
8
- * the session showed "Listening" and silently never replied (the #70 symptom).
9
- *
10
- * This fires ONE minimal /v1/messages request against the ACTIVE provider's
11
- * endpoint (the exact env applyAuthFallback already set) to confirm the
12
- * configured key can really answer. The result rides GET /health, so a dead key
13
- * becomes a visible "No working model" instead of a silent non-reply.
14
- *
15
- * Design rules (adversarial review of #87):
16
- * - NEVER on the Claude path (tier 1) and never on the connect critical path:
17
- * the caller runs it fire-and-forget AFTER the session starts, fallback-only,
18
- * so it adds zero boot latency to a normal Claude session.
19
- * - Conservative classification: only a DEFINITIVE auth/billing failure
20
- * (401/403/402) becomes a negative verdict. A 4xx that could be an endpoint
21
- * quirk (400/404), any 5xx, a timeout or a network error yields NO verdict
22
- * (null) — a transient blip must never fabricate "No working model".
23
- *
24
- * Shape mirrors the frontend's KeyHealthEntry (frontend/src/lib/agent-health.ts)
25
- * so resolveAgentHealth consumes it unchanged.
26
- */
27
- /** One fallback key's self-test result. Mirrors the frontend KeyHealthEntry. */
28
- export interface KeyHealthEntry {
29
- /** Provider key, e.g. 'zai' | 'openrouter' | 'anthropic'. */
30
- provider: string;
31
- /** Outcome of the tiny real request. */
32
- status: 'ok' | 'failed' | 'expired';
33
- /** ISO timestamp of the test. */
34
- checkedAt?: string;
35
- }
36
- /**
37
- * Classify a /v1/messages probe's HTTP status into a key-health verdict, or null
38
- * when the outcome is ambiguous (no honest verdict — see module header).
39
- * 2xx → ok (the key answered)
40
- * 429 → ok (valid key, just rate-limited)
41
- * 401, 403 → expired (bad / expired credential)
42
- * 402 → failed (billing / credit depleted)
43
- * anything else → null (endpoint quirk / 5xx / unknown — no verdict)
44
- */
45
- export declare function classifyProbeStatus(httpStatus: number): 'ok' | 'failed' | 'expired' | null;
46
- /**
47
- * Fire ONE minimal request against the ACTIVE provider endpoint to confirm the
48
- * configured key can really answer. Reads the env applyAuthFallback() already
49
- * set (ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN / ANTHROPIC_API_KEY /
50
- * ANTHROPIC_MODEL), so it tests exactly what query() will use. All fallback
51
- * tiers speak native Anthropic Messages, so one request shape covers them.
52
- *
53
- * Returns the verdict entry, or null when there was nothing to test or the
54
- * outcome was ambiguous (never a fabricated failure).
55
- */
56
- export declare function probeActiveProviderKey(provider: string, env?: NodeJS.ProcessEnv, timeoutMs?: number): Promise<KeyHealthEntry | null>;
@@ -1,93 +0,0 @@
1
- /**
2
- * key-health.ts — a tiny, REAL "can this provider actually answer?" self-test for
3
- * the no-Claude fallback tiers (issue #87, gap 5).
4
- *
5
- * The bug this surfaces: applyAuthFallback() picks a tier because a key is
6
- * PRESENT, but presence is not validity — the observed test account had a z.ai
7
- * key that failed and an OpenRouter key that had expired, and nothing said so;
8
- * the session showed "Listening" and silently never replied (the #70 symptom).
9
- *
10
- * This fires ONE minimal /v1/messages request against the ACTIVE provider's
11
- * endpoint (the exact env applyAuthFallback already set) to confirm the
12
- * configured key can really answer. The result rides GET /health, so a dead key
13
- * becomes a visible "No working model" instead of a silent non-reply.
14
- *
15
- * Design rules (adversarial review of #87):
16
- * - NEVER on the Claude path (tier 1) and never on the connect critical path:
17
- * the caller runs it fire-and-forget AFTER the session starts, fallback-only,
18
- * so it adds zero boot latency to a normal Claude session.
19
- * - Conservative classification: only a DEFINITIVE auth/billing failure
20
- * (401/403/402) becomes a negative verdict. A 4xx that could be an endpoint
21
- * quirk (400/404), any 5xx, a timeout or a network error yields NO verdict
22
- * (null) — a transient blip must never fabricate "No working model".
23
- *
24
- * Shape mirrors the frontend's KeyHealthEntry (frontend/src/lib/agent-health.ts)
25
- * so resolveAgentHealth consumes it unchanged.
26
- */
27
- import { cleanEnvKey } from './claude-auth.js';
28
- /**
29
- * Classify a /v1/messages probe's HTTP status into a key-health verdict, or null
30
- * when the outcome is ambiguous (no honest verdict — see module header).
31
- * 2xx → ok (the key answered)
32
- * 429 → ok (valid key, just rate-limited)
33
- * 401, 403 → expired (bad / expired credential)
34
- * 402 → failed (billing / credit depleted)
35
- * anything else → null (endpoint quirk / 5xx / unknown — no verdict)
36
- */
37
- export function classifyProbeStatus(httpStatus) {
38
- if (httpStatus >= 200 && httpStatus < 300)
39
- return 'ok';
40
- if (httpStatus === 429)
41
- return 'ok';
42
- if (httpStatus === 401 || httpStatus === 403)
43
- return 'expired';
44
- if (httpStatus === 402)
45
- return 'failed';
46
- return null;
47
- }
48
- /**
49
- * Fire ONE minimal request against the ACTIVE provider endpoint to confirm the
50
- * configured key can really answer. Reads the env applyAuthFallback() already
51
- * set (ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN / ANTHROPIC_API_KEY /
52
- * ANTHROPIC_MODEL), so it tests exactly what query() will use. All fallback
53
- * tiers speak native Anthropic Messages, so one request shape covers them.
54
- *
55
- * Returns the verdict entry, or null when there was nothing to test or the
56
- * outcome was ambiguous (never a fabricated failure).
57
- */
58
- export async function probeActiveProviderKey(provider, env = process.env, timeoutMs = 8000) {
59
- const base = (cleanEnvKey(env.ANTHROPIC_BASE_URL) || 'https://api.anthropic.com').replace(/\/$/, '');
60
- const authToken = cleanEnvKey(env.ANTHROPIC_AUTH_TOKEN);
61
- const apiKey = cleanEnvKey(env.ANTHROPIC_API_KEY);
62
- const model = cleanEnvKey(env.ANTHROPIC_MODEL) || 'claude-3-5-haiku-latest';
63
- // Nothing to test — no credential configured for the active endpoint.
64
- if (!authToken && !apiKey)
65
- return null;
66
- const headers = {
67
- 'content-type': 'application/json',
68
- 'anthropic-version': '2023-06-01',
69
- };
70
- // Fallback redirects (z.ai / OpenRouter) authenticate with a bearer AUTH_TOKEN;
71
- // a user's own Anthropic key uses x-api-key. applyAuthFallback sets exactly one.
72
- if (authToken)
73
- headers['authorization'] = `Bearer ${authToken}`;
74
- else
75
- headers['x-api-key'] = apiKey;
76
- const body = JSON.stringify({ model, max_tokens: 1, messages: [{ role: 'user', content: 'hi' }] });
77
- try {
78
- const r = await fetch(`${base}/v1/messages`, {
79
- method: 'POST',
80
- headers,
81
- body,
82
- signal: AbortSignal.timeout(timeoutMs),
83
- });
84
- const verdict = classifyProbeStatus(r.status);
85
- if (!verdict)
86
- return null;
87
- return { provider, status: verdict, checkedAt: new Date().toISOString() };
88
- }
89
- catch {
90
- // Network error / timeout — ambiguous, never a fabricated verdict.
91
- return null;
92
- }
93
- }