jeo-code 0.7.27 → 0.7.28

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.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  The README mirrors the latest 5 entries — regenerate with `bun run changelog:sync`.
8
8
 
9
+ ## [0.7.28] - 2026-07-02
10
+ _gajae-code 0.7.9–0.7.10 OAuth + model-execution parity: generic long rate-limit windows are retried (never fatal), retry waits are abort-cancellable, Codex prompt caching via session correlation, and the Kimi Code device-flow OAuth is completed end-to-end (catalog, gating, discovery, tests)._
11
+
12
+ ### Fixed
13
+ - **Generic 429 rate limits with a long server `Retry-After` were treated as FATAL — gajae 0.7.10 (#1370) keeps them retryable, and now jeo does too.** The old behavior failed fast on any 429 whose server-directed delay exceeded a hard-coded 5-minute budget, and silently compressed every honored `Retry-After` to 30s (retrying into a still-closed window). gjc's contract (verified against its `agent-session-retry-cap` tests): a transient rate limit honors the provider's wait IN FULL — even a ~3.1h `retry-after-ms=11180000` — while usage/quota/model-limit errors keep failing fast via `isUsageLimitError` → `defaultRetryable`. `withRetry` (`src/util/retry.ts`) now honors server-directed delays uncapped (the 30s `RATE_LIMIT_FLOOR_CAP_MS` only caps the SYNTHETIC escalating floor for server-silent 429s), and `resolveRetryOptions` no longer injects a default `rateLimitMaxServerDelayMs` — it remains available as explicit opt-in config (`retry.rateLimitMaxServerDelayMs`, added to the Config type + zod schema).
14
+ - **A long honored rate-limit wait is now user-cancellable.** `RetryOptions.signal` (new) aborts an in-progress backoff sleep: `resolveCall` threads the caller's ORIGINAL abort signal (Ctrl-C / turn abort) into the retry layer — distinct from the per-attempt fetch timeout, so one attempt's timeout can never cut short the next attempt's backoff, while Esc/Ctrl-C interrupts a multi-hour wait immediately instead of after the sleep elapses.
15
+ - **`isUsageLimitError` missed gajae's usage-exhaustion phrasings.** Added `model.?limit` / `message.?limit` / `limit for this model` / `out_of_credits` / Anthropic's account-level `request would exceed your account's rate limit` and the qualified `Resource has been exhausted (… quota/limit)` form (bare gRPC `resource exhausted` stays transient — deliberate jeo choice for Gemini windows that clear quickly). Without these, an out-of-credits 429 was retried on a pointless backoff ladder instead of failing fast to the model/credential-switch hint.
16
+ - **Anthropic 429s now carry their rate-limit response headers in the error detail** (gjc `getSafeAnthropicHeaderEvidence` parity): `retry-after`, `retry-after-ms`, and every `anthropic-ratelimit-*` header — including the decisive `anthropic-ratelimit-unified-overage-disabled-reason=out_of_credits`, which often arrives ONLY as a header. Without it an out-of-credits 429 read like a generic per-minute rate limit.
17
+
18
+ ### Added
19
+ - **Kimi Code (Moonshot coding subscription) OAuth — completed end-to-end.** The device-code flow (`src/auth/flows/kimi.ts`, RFC 8628: `auth.kimi.com` device_authorization → poll with `authorization_pending`/`slow_down`/`expired_token`/`access_denied` semantics, X-Msh-* device headers, persisted `kimi-device-id`) now has: catalog entries for the Kimi Code models (`kimi-for-coding`, `kimi-k2.7-code`, `kimi/kimi-k2.5`, `kimi/kimi-k2-turbo-preview`, `kimi/kimi-k2` — 262k context, provider-qualified where canonicals collide with tencent's), a `KIMI_CODE_MODELS` export, OAuth gating (`oauthServesModel`: kimi OAuth serves ONLY the Kimi Code set; Moonshot API ids fall back to `KIMI_API_KEY` via `effectiveCredentialForProvider`, with an actionable error when no key exists), and an OAuth-only discovery fallback that lists exactly the Kimi Code models (`catalogOr`) instead of faking moonshot ids. The `kimiAdapter` dispatches on credential kind — OAuth → Anthropic-compatible `api.kimi.com/coding/v1/messages` with plain Bearer + X-Msh headers (no Claude-Code cloaking; gated by `shouldUseClaudeCodeOAuthShape`'s baseUrl check), api_key → the unchanged OpenAI-compatible `api.moonshot.ai/v1`.
20
+ - **Codex/Responses provider-side prompt caching (gjc parity — closes the `ponytail` note in `openai-responses.ts`).** New `CallOptions.sessionKey` threads a stable per-conversation key from `launch.ts` (the session id), `team.ts`, and `task-tool.ts` (per-run UUIDs) through the engine loop into every model call. `codexResponsesRequest` stamps it as `prompt_cache_key` in the body (both the OAuth Codex backend and the public `api.openai.com/v1/responses`) plus `session_id`/`conversation_id`/`x-client-request-id` correlation headers on the Codex backend (`normalizePromptCacheKey`: ≤64 chars verbatim, longer keys hashed `pc_*` — gjc `normalizeOpenAIResponsesPromptCacheKey` parity). An agent loop replays nearly-identical history each step, so cache hits cut both latency and billed input tokens.
21
+
22
+ ### Verified
23
+ - `bun run typecheck` clean; full suite green — 2142 pass / 0 fail across 244 files (before the codex/kimi test additions), then targeted suites re-run green: `retry` (26), `rate-limit-handling` (5), `provider-errors`, `round-b`, `kimi-provider` (7 new), `kimi-oauth` (7 new), `codex-responses` (+3 new). The corrected rate-limit contract is pinned by tests that mirror gjc's own fixtures (3.1h retry-after honored + recovered; model-limit fails fast; mid-wait abort interrupts the sleep).
24
+
9
25
  ## [0.7.27] - 2026-07-02
10
26
  _Extended-keyboard decode layer: Esc/Ctrl+C work again on kitty-protocol terminals, Shift+Enter breaks lines on every terminal incl. Windows, row/draft-aware Cmd+←→/↑↓ + Home/End/Ctrl+Home/End, Ctrl+V text-paste fallback. Plus Anthropic 5th-gen omp-parity: catalog-derived output budgets end thinking-vs-reply starvation; refusals fail fast at the transport and the recovery ladder strips replayed thinking blocks._
11
27
 
package/README.ja.md CHANGED
@@ -205,11 +205,11 @@ CI は `.github/workflows/npm-publish.yml` で公開します — GitHub リリ
205
205
  ## 変更履歴 (Changelog)
206
206
 
207
207
  <!-- CHANGELOG:START (auto-generated from CHANGELOG.md — run `bun run changelog:sync`) -->
208
+ - **[0.7.28]** (2026-07-02) — gajae-code 0.7.9–0.7.10 OAuth + model-execution parity: generic long rate-limit windows are retried (never fatal), retry waits are abort-cancellable, Codex prompt caching via session correlation, and the Kimi Code device-flow OAuth is completed end-to-end (catalog, gating, discovery, tests).
208
209
  - **[0.7.27]** (2026-07-02) — Extended-keyboard decode layer: Esc/Ctrl+C work again on kitty-protocol terminals, Shift+Enter breaks lines on every terminal incl. Windows, row/draft-aware Cmd+←→/↑↓ + Home/End/Ctrl+Home/End, Ctrl+V text-paste fallback. Plus Anthropic 5th-gen omp-parity: catalog-derived output budgets end thinking-vs-reply starvation; refusals fail fast at the transport and the recovery ladder strips replayed thinking blocks.
209
210
  - **[0.7.26]** (2026-07-02) — Esc/Ctrl+C reliably clears the input box (fixes a gate the earlier reorder missed), and un-bracketed multi-line pastes no longer shred into premature per-line submits.
210
211
  - **[0.7.25]** (2026-07-02) — Shift+Enter now reliably distinguishable from Enter on kitty/WezTerm/iTerm2/xterm via modifyOtherKeys + kitty keyboard protocol; row-aware Home/End and Cmd+Left/Right in multi-row drafts.
211
212
  - **[0.7.24]** (2026-07-02) — OAuth callback page redesign with embedded jeo wordmark + Close button; memory budget O(n²)→O(n) incremental tracking fix.
212
- - **[0.7.23]** (2026-07-01) — Anthropic 5th-generation model catalog: adds Sonnet 5, Fable 5, and Mythos 5, promotes the 4.6 generation to 1M context / 128k output, and retires the superseded 4.5/3.5/4.1 ids. Bumps the default model to Sonnet 4.6, prices the new tiers, and fixes the thinking transport for Haiku 4.5 and every 5th-gen model.
213
213
 
214
214
  See [CHANGELOG.md](CHANGELOG.md) for the full history.
215
215
  <!-- CHANGELOG:END -->
package/README.ko.md CHANGED
@@ -205,11 +205,11 @@ CI는 `.github/workflows/npm-publish.yml`로 배포합니다 — GitHub 릴리
205
205
  ## 변경 이력 (Changelog)
206
206
 
207
207
  <!-- CHANGELOG:START (auto-generated from CHANGELOG.md — run `bun run changelog:sync`) -->
208
+ - **[0.7.28]** (2026-07-02) — gajae-code 0.7.9–0.7.10 OAuth + model-execution parity: generic long rate-limit windows are retried (never fatal), retry waits are abort-cancellable, Codex prompt caching via session correlation, and the Kimi Code device-flow OAuth is completed end-to-end (catalog, gating, discovery, tests).
208
209
  - **[0.7.27]** (2026-07-02) — Extended-keyboard decode layer: Esc/Ctrl+C work again on kitty-protocol terminals, Shift+Enter breaks lines on every terminal incl. Windows, row/draft-aware Cmd+←→/↑↓ + Home/End/Ctrl+Home/End, Ctrl+V text-paste fallback. Plus Anthropic 5th-gen omp-parity: catalog-derived output budgets end thinking-vs-reply starvation; refusals fail fast at the transport and the recovery ladder strips replayed thinking blocks.
209
210
  - **[0.7.26]** (2026-07-02) — Esc/Ctrl+C reliably clears the input box (fixes a gate the earlier reorder missed), and un-bracketed multi-line pastes no longer shred into premature per-line submits.
210
211
  - **[0.7.25]** (2026-07-02) — Shift+Enter now reliably distinguishable from Enter on kitty/WezTerm/iTerm2/xterm via modifyOtherKeys + kitty keyboard protocol; row-aware Home/End and Cmd+Left/Right in multi-row drafts.
211
212
  - **[0.7.24]** (2026-07-02) — OAuth callback page redesign with embedded jeo wordmark + Close button; memory budget O(n²)→O(n) incremental tracking fix.
212
- - **[0.7.23]** (2026-07-01) — Anthropic 5th-generation model catalog: adds Sonnet 5, Fable 5, and Mythos 5, promotes the 4.6 generation to 1M context / 128k output, and retires the superseded 4.5/3.5/4.1 ids. Bumps the default model to Sonnet 4.6, prices the new tiers, and fixes the thinking transport for Haiku 4.5 and every 5th-gen model.
213
213
 
214
214
  See [CHANGELOG.md](CHANGELOG.md) for the full history.
215
215
  <!-- CHANGELOG:END -->
package/README.md CHANGED
@@ -232,11 +232,11 @@ Huge thanks to [gajae-code](https://github.com/Yeachan-Heo/gajae-code) for the i
232
232
  ## Changelog
233
233
 
234
234
  <!-- CHANGELOG:START (auto-generated from CHANGELOG.md — run `bun run changelog:sync`) -->
235
+ - **[0.7.28]** (2026-07-02) — gajae-code 0.7.9–0.7.10 OAuth + model-execution parity: generic long rate-limit windows are retried (never fatal), retry waits are abort-cancellable, Codex prompt caching via session correlation, and the Kimi Code device-flow OAuth is completed end-to-end (catalog, gating, discovery, tests).
235
236
  - **[0.7.27]** (2026-07-02) — Extended-keyboard decode layer: Esc/Ctrl+C work again on kitty-protocol terminals, Shift+Enter breaks lines on every terminal incl. Windows, row/draft-aware Cmd+←→/↑↓ + Home/End/Ctrl+Home/End, Ctrl+V text-paste fallback. Plus Anthropic 5th-gen omp-parity: catalog-derived output budgets end thinking-vs-reply starvation; refusals fail fast at the transport and the recovery ladder strips replayed thinking blocks.
236
237
  - **[0.7.26]** (2026-07-02) — Esc/Ctrl+C reliably clears the input box (fixes a gate the earlier reorder missed), and un-bracketed multi-line pastes no longer shred into premature per-line submits.
237
238
  - **[0.7.25]** (2026-07-02) — Shift+Enter now reliably distinguishable from Enter on kitty/WezTerm/iTerm2/xterm via modifyOtherKeys + kitty keyboard protocol; row-aware Home/End and Cmd+Left/Right in multi-row drafts.
238
239
  - **[0.7.24]** (2026-07-02) — OAuth callback page redesign with embedded jeo wordmark + Close button; memory budget O(n²)→O(n) incremental tracking fix.
239
- - **[0.7.23]** (2026-07-01) — Anthropic 5th-generation model catalog: adds Sonnet 5, Fable 5, and Mythos 5, promotes the 4.6 generation to 1M context / 128k output, and retires the superseded 4.5/3.5/4.1 ids. Bumps the default model to Sonnet 4.6, prices the new tiers, and fixes the thinking transport for Haiku 4.5 and every 5th-gen model.
240
240
 
241
241
  See [CHANGELOG.md](CHANGELOG.md) for the full history.
242
242
  <!-- CHANGELOG:END -->
package/README.zh.md CHANGED
@@ -205,11 +205,11 @@ CI 通过 `.github/workflows/npm-publish.yml` 发布 — GitHub 发布 release
205
205
  ## 更新日志 (Changelog)
206
206
 
207
207
  <!-- CHANGELOG:START (auto-generated from CHANGELOG.md — run `bun run changelog:sync`) -->
208
+ - **[0.7.28]** (2026-07-02) — gajae-code 0.7.9–0.7.10 OAuth + model-execution parity: generic long rate-limit windows are retried (never fatal), retry waits are abort-cancellable, Codex prompt caching via session correlation, and the Kimi Code device-flow OAuth is completed end-to-end (catalog, gating, discovery, tests).
208
209
  - **[0.7.27]** (2026-07-02) — Extended-keyboard decode layer: Esc/Ctrl+C work again on kitty-protocol terminals, Shift+Enter breaks lines on every terminal incl. Windows, row/draft-aware Cmd+←→/↑↓ + Home/End/Ctrl+Home/End, Ctrl+V text-paste fallback. Plus Anthropic 5th-gen omp-parity: catalog-derived output budgets end thinking-vs-reply starvation; refusals fail fast at the transport and the recovery ladder strips replayed thinking blocks.
209
210
  - **[0.7.26]** (2026-07-02) — Esc/Ctrl+C reliably clears the input box (fixes a gate the earlier reorder missed), and un-bracketed multi-line pastes no longer shred into premature per-line submits.
210
211
  - **[0.7.25]** (2026-07-02) — Shift+Enter now reliably distinguishable from Enter on kitty/WezTerm/iTerm2/xterm via modifyOtherKeys + kitty keyboard protocol; row-aware Home/End and Cmd+Left/Right in multi-row drafts.
211
212
  - **[0.7.24]** (2026-07-02) — OAuth callback page redesign with embedded jeo wordmark + Close button; memory budget O(n²)→O(n) incremental tracking fix.
212
- - **[0.7.23]** (2026-07-01) — Anthropic 5th-generation model catalog: adds Sonnet 5, Fable 5, and Mythos 5, promotes the 4.6 generation to 1M context / 128k output, and retires the superseded 4.5/3.5/4.1 ids. Bumps the default model to Sonnet 4.6, prices the new tiers, and fixes the thinking transport for Haiku 4.5 and every 5th-gen model.
213
213
 
214
214
  See [CHANGELOG.md](CHANGELOG.md) for the full history.
215
215
  <!-- CHANGELOG:END -->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jeo-code",
3
- "version": "0.7.27",
3
+ "version": "0.7.28",
4
4
 
5
5
  "description": "Clean, highly optimized AI coding agent using spec-first loop",
6
6
  "type": "module",
@@ -105,6 +105,7 @@ export const ConfigSchema = z
105
105
  maxDelayMs: z.number().int().min(0).optional(),
106
106
  rateLimitRetries: z.number().int().min(0).optional(),
107
107
  rateLimitMinDelayMs: z.number().int().min(0).optional(),
108
+ rateLimitMaxServerDelayMs: z.number().int().min(0).optional(),
108
109
  })
109
110
  .optional(),
110
111
  /**
@@ -40,6 +40,7 @@ async function invokeCallLlm(history: Message[], options: {
40
40
  onReasoningStart?: () => void;
41
41
  onReasoningArtifact?: (artifact: import("../ai/types").ReasoningArtifact) => void;
42
42
  tools?: import("../ai/types").NativeToolSchema[];
43
+ sessionKey?: string;
43
44
  }): Promise<string> {
44
45
  const mod = await import("./loop");
45
46
  return mod.callLlm(history, options);
@@ -265,6 +266,11 @@ export interface AgentLoopOptions {
265
266
  * so an additional query typed while the turn runs steers the live turn instead of
266
267
  * waiting for the next prompt. Return [] when nothing is pending. */
267
268
  steer?: () => string[];
269
+ /** Stable per-conversation key (the session id): threaded to every model call for
270
+ * provider-side prompt caching (gjc parity — Codex prompt_cache_key/session_id).
271
+ * An agent loop replays nearly-identical history each step, so cache hits here cut
272
+ * both latency and billed input tokens. */
273
+ sessionKey?: string;
268
274
  }
269
275
 
270
276
  export interface AgentLoopResult {
@@ -388,7 +394,7 @@ export async function runAgentLoop(history: Message[], opts: AgentLoopOptions):
388
394
  "with what you have already found this turn, and state explicitly anything that is still uncertain.",
389
395
  },
390
396
  ],
391
- { jsonMode: false, model: opts.model, maxTokens: opts.maxTokens, signal: opts.signal },
397
+ { jsonMode: false, model: opts.model, maxTokens: opts.maxTokens, signal: opts.signal, sessionKey: opts.sessionKey },
392
398
  );
393
399
  const consolidated = wrapUp.trim();
394
400
  if (consolidated) {
@@ -557,6 +563,7 @@ export async function runAgentLoop(history: Message[], opts: AgentLoopOptions):
557
563
  maxTokens: opts.maxTokens,
558
564
  reasoningEffort: opts.reasoningEffort,
559
565
  signal: opts.signal,
566
+ sessionKey: opts.sessionKey,
560
567
  onUsage: u => { acc.inputTokens += u.inputTokens ?? 0; acc.outputTokens += u.outputTokens ?? 0; sawUsage = true; },
561
568
  onToken,
562
569
  onReasoning,
@@ -1132,7 +1139,7 @@ export async function runAgentLoop(history: Message[], opts: AgentLoopOptions):
1132
1139
  "the key findings/changes so far, and what remains to be done next.",
1133
1140
  },
1134
1141
  ],
1135
- { jsonMode: false, model: opts.model, maxTokens: opts.maxTokens, signal: opts.signal },
1142
+ { jsonMode: false, model: opts.model, maxTokens: opts.maxTokens, signal: opts.signal, sessionKey: opts.sessionKey },
1136
1143
  );
1137
1144
  const consolidated = wrapUp.trim();
1138
1145
  if (consolidated) {
package/src/agent/loop.ts CHANGED
@@ -31,6 +31,9 @@ export interface ChatOptions {
31
31
  onReasoningArtifact?: (artifact: import("../ai/types").ReasoningArtifact) => void;
32
32
  /** NATIVE tool-calling function declarations (forwarded to capable adapters). */
33
33
  tools?: import("../ai/types").NativeToolSchema[];
34
+ /** Stable per-conversation key (session id) for provider-side prompt caching
35
+ * (gjc parity: prompt_cache_key / session_id correlation on Codex/Responses). */
36
+ sessionKey?: string;
34
37
  }
35
38
 
36
39
  const manager = createModelManager();
@@ -78,6 +78,11 @@ export interface Config {
78
78
  rateLimitRetries?: number;
79
79
  /** Minimum backoff (ms) for a 429 when the server sends no Retry-After. */
80
80
  rateLimitMinDelayMs?: number;
81
+ /** Opt-in ceiling (ms) on a 429's server-directed Retry-After: a delay beyond it fails
82
+ * fast instead of sleeping. Unset (the default) honors any server-directed delay in
83
+ * full, however long — a generic rate limit is retried forever, not treated as fatal
84
+ * past some budget. Set this only if unbounded rate-limit waits are undesirable. */
85
+ rateLimitMaxServerDelayMs?: number;
81
86
  /** HTTP statuses to treat as NON-retryable even when defaultRetryable would
82
87
  * retry them (e.g. pin 503 to fail fast instead of riding the backoff ladder). */
83
88
  failFastStatuses?: number[];
@@ -385,14 +390,88 @@ export async function readRawGlobalConfig(): Promise<Config> {
385
390
  return salvageCredentials(clean, parsed);
386
391
  }
387
392
 
393
+ /**
394
+ * Global config write lock — HIGH-1 lost-update fix (gjc parity: auth-storage.ts
395
+ * SqliteAuthCredentialStore serializes credential writes inside SQLite). jeo keeps
396
+ * everything in one config.json, so EVERY read-modify-write must serialize through one
397
+ * lock file; otherwise two concurrent per-provider OAuth refreshes (e.g. doctor.ts
398
+ * Promise.all) read the same base config and the later write clobbers the earlier one's
399
+ * just-rotated refresh token → silent logout.
400
+ *
401
+ * Semantics mirror src/auth/storage.ts acquireLock (stale-lock detection + bounded wait
402
+ * + single steal at the deadline) but are implemented locally: storage.ts imports
403
+ * state.ts, so importing it back would create a cycle.
404
+ * // ponytail: extract a shared lockfile helper module if a third lockfile user appears.
405
+ */
406
+ const CONFIG_LOCK_STALE_MS = 5_000;
407
+
408
+ function configLockPath(): string {
409
+ return `${globalConfigPath()}.lock`;
410
+ }
411
+
412
+ async function acquireConfigLock(timeoutMs = CONFIG_LOCK_STALE_MS): Promise<void> {
413
+ const lockPath = configLockPath();
414
+ await fs.mkdir(path.dirname(lockPath), { recursive: true, mode: 0o700 });
415
+ // `timeoutMs` is the STALENESS threshold for a dead holder's lock file; the
416
+ // acquisition wait itself is bounded at 2× that (same policy as storage.ts).
417
+ const deadline = Date.now() + Math.max(timeoutMs * 2, 1_000);
418
+ while (true) {
419
+ try {
420
+ const handle = await fs.open(lockPath, "wx");
421
+ await handle.writeFile(JSON.stringify({ pid: process.pid, createdAt: Date.now() }), "utf-8");
422
+ await handle.close();
423
+ return;
424
+ } catch (err: any) {
425
+ if (err.code !== "EEXIST") throw err;
426
+ try {
427
+ const info = JSON.parse(await fs.readFile(lockPath, "utf-8"));
428
+ if (typeof info.createdAt === "number" && info.createdAt + timeoutMs < Date.now()) {
429
+ await fs.unlink(lockPath).catch(() => {});
430
+ }
431
+ } catch {
432
+ try {
433
+ const stat = await fs.stat(lockPath);
434
+ if (stat.mtimeMs + timeoutMs < Date.now()) {
435
+ await fs.unlink(lockPath).catch(() => {});
436
+ }
437
+ } catch {}
438
+ }
439
+ }
440
+ if (Date.now() >= deadline) {
441
+ // Deadline reached: the holder is dead or wedged. Steal once — the lock guards
442
+ // a short config read-modify-write, so waiting longer only hangs the caller.
443
+ await fs.unlink(lockPath).catch(() => {});
444
+ const handle = await fs.open(lockPath, "wx").catch(() => null);
445
+ if (handle) {
446
+ await handle.writeFile(JSON.stringify({ pid: process.pid, createdAt: Date.now(), stolen: true }), "utf-8");
447
+ await handle.close();
448
+ return;
449
+ }
450
+ throw new Error(`config lock could not be acquired within ${Math.max(timeoutMs * 2, 1_000)}ms (${lockPath})`);
451
+ }
452
+ await new Promise(resolve => setTimeout(resolve, 50));
453
+ }
454
+ }
455
+
456
+ async function releaseConfigLock(): Promise<void> {
457
+ await fs.unlink(configLockPath()).catch(() => {});
458
+ }
459
+
388
460
  /** Merge a patch onto the RAW on-disk config and persist. The `build` callback
389
461
  * receives the raw config so partial updates (subagents/roles maps) are derived
390
- * from on-disk state, never from the env-overlaid runtime config. */
462
+ * from on-disk state, never from the env-overlaid runtime config. The whole
463
+ * read→merge→write cycle holds the global config lock so concurrent patches
464
+ * (per-provider OAuth refreshes, /model save, doctor sweeps) never lose updates. */
391
465
  export async function saveConfigPatch(build: (raw: Config) => Partial<Config>): Promise<Config> {
392
- const raw = await readRawGlobalConfig();
393
- const next = { ...raw, ...build(raw) };
394
- await saveGlobalConfig(next);
395
- return next;
466
+ await acquireConfigLock();
467
+ try {
468
+ const raw = await readRawGlobalConfig();
469
+ const next = { ...raw, ...build(raw) };
470
+ await saveGlobalConfig(next);
471
+ return next;
472
+ } finally {
473
+ await releaseConfigLock();
474
+ }
396
475
  }
397
476
 
398
477
  export function getLocalJeoDir(cwd: string = process.cwd()): string {
@@ -220,6 +220,9 @@ export function createTaskTool(opts: TaskToolOptions): ToolHandler {
220
220
  model,
221
221
  maxSteps,
222
222
  maxTokens: resolveMaxOutputTokens(model, thinking),
223
+ // Per-run prompt-cache key: the subagent replays its own growing history each
224
+ // step, so a stable per-run key gets provider cache hits (gjc sub-session parity).
225
+ sessionKey: crypto.randomUUID(),
223
226
  // Bounded delegation: a subagent's step contract stays exact — the parent
224
227
  // owns any retry/extension decision, so the gjc retry flow is disabled here.
225
228
  budget: { maxExtensions: 0 },
@@ -93,6 +93,14 @@ export const MODEL_CATALOG: readonly CatalogModel[] = [
93
93
  { canonical: "kimi-thinking-preview", provider: "kimi", providerModel: "kimi-thinking-preview", contextTokens: 128_000, maxOutputTokens: 32_000, thinking: FULL, images: true },
94
94
  { canonical: "kimi-latest", provider: "kimi", providerModel: "kimi-latest", contextTokens: 128_000, maxOutputTokens: 16_384, thinking: [], images: true },
95
95
  { canonical: "moonshot-v1-128k", provider: "kimi", providerModel: "moonshot-v1-128k", contextTokens: 128_000, maxOutputTokens: 16_384, thinking: [], images: false },
96
+ // Kimi Code (Moonshot coding subscription, device-code OAuth) — Anthropic-compatible
97
+ // at https://api.kimi.com/coding. Provider-qualified (`kimi/…`) to avoid canonical
98
+ // collisions (kimi-k2.5 also exists under tencent). gjc catalog parity: kimi-code.
99
+ { canonical: "kimi-for-coding", provider: "kimi", providerModel: "kimi/kimi-for-coding", contextTokens: 262_144, maxOutputTokens: 32_000, thinking: FULL, images: true, company: "Moonshot (Kimi Code)" },
100
+ { canonical: "kimi-k2.7-code", provider: "kimi", providerModel: "kimi/kimi-k2.7-code", contextTokens: 262_144, maxOutputTokens: 65_536, thinking: FULL, images: true, company: "Moonshot (Kimi Code)" },
101
+ { canonical: "kimi/kimi-k2.5", provider: "kimi", providerModel: "kimi/kimi-k2.5", contextTokens: 262_144, maxOutputTokens: 65_536, thinking: FULL, images: true, company: "Moonshot (Kimi Code)" },
102
+ { canonical: "kimi/kimi-k2-turbo-preview", provider: "kimi", providerModel: "kimi/kimi-k2-turbo-preview", contextTokens: 262_144, maxOutputTokens: 32_000, thinking: FULL, images: false, company: "Moonshot (Kimi Code)" },
103
+ { canonical: "kimi/kimi-k2", provider: "kimi", providerModel: "kimi/kimi-k2", contextTokens: 262_144, maxOutputTokens: 262_144, thinking: [], images: false, company: "Moonshot (Kimi Code)" },
96
104
  // Google
97
105
  { canonical: "gemini-1.5-pro", provider: "gemini", providerModel: "gemini-1.5-pro", contextTokens: 1_000_000, maxOutputTokens: 8_192, thinking: [], images: true },
98
106
  { canonical: "gemini-2.0-flash", provider: "gemini", providerModel: "gemini-2.0-flash", contextTokens: 1_000_000, maxOutputTokens: 8_192, thinking: [], images: true },
@@ -144,6 +152,21 @@ export const MODEL_CATALOG: readonly CatalogModel[] = [
144
152
  */
145
153
  export const CODEX_MODELS: readonly string[] = ["gpt-5.5", "gpt-5.4"];
146
154
 
155
+ /**
156
+ * Model ids the Kimi Code OAuth backend (api.kimi.com/coding, Anthropic Messages
157
+ * format) actually serves. The subscription endpoint rejects Moonshot API-platform
158
+ * ids (kimi-latest, moonshot-v1-*, kimi-thinking-preview), so a Kimi OAuth login is
159
+ * limited to exactly these; an API key serves the api.moonshot.ai catalog instead.
160
+ * gjc parity: models.json `kimi-code` provider entries (wire ids, `kimi/` stripped).
161
+ */
162
+ export const KIMI_CODE_MODELS: readonly string[] = [
163
+ "kimi-for-coding",
164
+ "kimi-k2.7-code",
165
+ "kimi-k2.5",
166
+ "kimi-k2-turbo-preview",
167
+ "kimi-k2",
168
+ ];
169
+
147
170
  /** Format a token count compactly (1000 → 1K, 1_000_000 → 1M). */
148
171
  export function formatTokens(n: number): string {
149
172
  if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n % 1_000_000 === 0 ? 0 : 1)}M`;
@@ -11,7 +11,7 @@ import { readGlobalConfig, type Config } from "../agent/state";
11
11
  import { resolveCredential, type AuthProvider, type Credential } from "../auth";
12
12
  import type { ProviderName } from "./types";
13
13
  import { PROVIDER_NAMES } from "./provider-status";
14
- import { catalogByProvider, CODEX_MODELS } from "./model-catalog";
14
+ import { catalogByProvider, CODEX_MODELS, KIMI_CODE_MODELS } from "./model-catalog";
15
15
  import { extractChatgptAccountId } from "./providers/openai-responses";
16
16
  import { openaiCompatDef } from "./providers/openai-compatible-catalog";
17
17
 
@@ -291,7 +291,11 @@ export function catalogOr(result: ProviderModelsResult): ProviderModelsResult {
291
291
  if (!eligible) return result;
292
292
  const ids = result.provider === "openai" && result.source === "oauth"
293
293
  ? [...CODEX_MODELS]
294
- : catalogByProvider(result.provider).map(m => m.providerModel);
294
+ : result.provider === "kimi" && result.source === "oauth"
295
+ // Kimi OAuth (Kimi Code subscription) serves ONLY the Kimi Code catalog —
296
+ // moonshot API-platform ids would 404 against api.kimi.com/coding.
297
+ ? KIMI_CODE_MODELS.map(id => `kimi/${id}`)
298
+ : catalogByProvider(result.provider).map(m => m.providerModel);
295
299
  if (ids.length === 0) return result;
296
300
  return { ...result, models: ids, ok: true, fallback: true };
297
301
  }
@@ -6,7 +6,7 @@ import "./register-providers"; // side-effect: registers built-in adapters into
6
6
  import type { CallOptions, Message, ProviderAdapter, ProviderName } from "./types";
7
7
  import { expandAlias, resolveModelId, effectiveAliasesFor } from "./model-registry";
8
8
  import { findCatalogEntry, type ModelCatalogEntry } from "./model-catalog-compat";
9
- import { toProviderModel, CODEX_MODELS, findCatalogModel } from "./model-catalog";
9
+ import { toProviderModel, CODEX_MODELS, KIMI_CODE_MODELS, findCatalogModel } from "./model-catalog";
10
10
  import { xaiCredential } from "./providers/xai";
11
11
  import { OPENAI_COMPAT_NAMES, isOpenAICompatProvider } from "./providers/openai-compatible-catalog";
12
12
  import { withRetry, defaultRetryable, type RetryOptions } from "../util/retry";
@@ -194,19 +194,18 @@ const ALIAS_DEFAULTS = { fast: "ollama/qwen2.5:0.5b", local: "ollama/qwen2.5:0.5
194
194
  * requestMaxRetries + 1. When unset, the `withRetry` defaults apply (3 attempts),
195
195
  * but rate-limit (429) errors get a more generous budget + a backoff floor so a
196
196
  * transient per-minute window can clear instead of the very first 429 instantly
197
- * exhausting auto-retry. A server-directed retry delay above the five-minute
198
- * budget is surfaced immediately with its reset hint instead of being capped and
199
- * retried pointlessly. Explicit config (`requestMaxRetries`/`maxDelayMs`) always
200
- * wins and disables the matching rate-limit default.
201
- * `maxDelayMs` caps per-attempt backoff when provided.
197
+ * exhausting auto-retry. A server-directed retry delay is honored IN FULL — no
198
+ * default ceiling — because a generic 429 is retried forever, not treated as fatal
199
+ * past some arbitrary budget (gjc parity: even a multi-hour Anthropic rate-limit
200
+ * window is retried, not bailed on); `rateLimitMaxServerDelayMs` remains available
201
+ * as an explicit opt-in for a caller that truly cannot wait that long. Explicit
202
+ * config (`requestMaxRetries`/`maxDelayMs`) always wins and disables the matching
203
+ * rate-limit default. `maxDelayMs` caps per-attempt backoff when provided.
202
204
  */
203
205
  const DEFAULT_RATE_LIMIT_RETRIES = 6; // total attempts for 429 (initial + 5 retries)
204
206
  // 429 floor when the server sends no Retry-After. Escalates per attempt inside
205
207
  // withRetry (2s → 4s → 8s → 16s → 30s ≈ 60s total), spanning a per-minute window.
206
208
  const DEFAULT_RATE_LIMIT_MIN_DELAY_MS = 2000;
207
- // GJC parity for server-directed 429s: retry short windows, but do not hang a CLI
208
- // through long subscription/account resets.
209
- const DEFAULT_RATE_LIMIT_MAX_SERVER_DELAY_MS = 5 * 60 * 1000;
210
209
  export function resolveRetryOptions(retry: Config["retry"], kind: "request" | "stream" = "request"): RetryOptions {
211
210
  const opts: RetryOptions = { isRetryable: defaultRetryable };
212
211
 
@@ -244,7 +243,12 @@ export function resolveRetryOptions(retry: Config["retry"], kind: "request" | "s
244
243
  // 429 backoff floor: explicit wins; else default UNLESS the user pinned maxDelayMs.
245
244
  if (typeof retry?.rateLimitMinDelayMs === "number") opts.rateLimitMinDelayMs = retry.rateLimitMinDelayMs;
246
245
  else if (typeof retry?.maxDelayMs !== "number") opts.rateLimitMinDelayMs = DEFAULT_RATE_LIMIT_MIN_DELAY_MS;
247
- opts.rateLimitMaxServerDelayMs = DEFAULT_RATE_LIMIT_MAX_SERVER_DELAY_MS;
246
+ // No default `rateLimitMaxServerDelayMs` ceiling: gjc parity retries a generic 429
247
+ // forever, honoring however long the server asks (see the doc comment above). Usage
248
+ // limits fail fast on their own via isUsageLimitError → defaultRetryable, well before a
249
+ // wait is ever computed, so this is safe. An explicit config value is still honored for
250
+ // a caller that truly cannot wait an unbounded server-directed delay.
251
+ if (typeof retry?.rateLimitMaxServerDelayMs === "number") opts.rateLimitMaxServerDelayMs = retry.rateLimitMaxServerDelayMs;
248
252
 
249
253
  // Config-driven fail-fast overrides: a status in `failFastStatuses` or a message
250
254
  // matching any `failFastPattern` is forced non-retryable, layered on top of the
@@ -281,12 +285,18 @@ export function resolveRetryOptions(retry: Config["retry"], kind: "request" | "s
281
285
  /**
282
286
  * Whether the bundled adapter can serve this provider+model over OAuth end-to-end.
283
287
  * OpenAI OAuth (ChatGPT/Codex) only serves Codex models, so any other OpenAI model
284
- * must use an API key. A provider whose OAuth backend is not verified end-to-end
288
+ * must use an API key. Kimi OAuth (Kimi Code subscription) only serves the Kimi Code
289
+ * catalog (api.kimi.com/coding) — Moonshot API-platform ids (kimi-latest, moonshot-v1-*)
290
+ * need a KIMI_API_KEY. A provider whose OAuth backend is not verified end-to-end
285
291
  * cannot serve any model over OAuth. Everything else (Anthropic Messages, Gemini /
286
292
  * Antigravity Cloud Code Assist) is served end-to-end.
287
293
  */
288
294
  function oauthServesModel(provider: AuthProvider, model: string): boolean {
289
295
  if (provider === "openai") return CODEX_MODELS.includes(model);
296
+ if (provider === "kimi") {
297
+ const wire = model.startsWith("kimi/") ? model.slice(5) : model;
298
+ return KIMI_CODE_MODELS.includes(wire);
299
+ }
290
300
  if (isOAuthProvider(provider) && OAUTH_FLOW_REGISTRY[provider].verifiedEndToEnd === false) return false;
291
301
  return true;
292
302
  }
@@ -375,6 +385,7 @@ async function resolveCall(options: Partial<CallOptions>, kind: "request" | "str
375
385
  onReasoning: options.onReasoning,
376
386
  onReasoningArtifact: options.onReasoningArtifact,
377
387
  tools: options.tools,
388
+ sessionKey: options.sessionKey,
378
389
  };
379
390
  // Caller-supplied retry sink rides on the config-derived retry budget so the
380
391
  // engine/TUI can surface "rate limited — retrying in Ns" instead of a silent wait.
@@ -383,7 +394,12 @@ async function resolveCall(options: Partial<CallOptions>, kind: "request" | "str
383
394
  // never replays after the first emitted chunk). Both fall back to `maxRetries`,
384
395
  // and an unset stream budget keeps the conservative withRetry default — the
385
396
  // generous gjc default of 100 only applies when the user configures it.
386
- const retry: RetryOptions = { ...resolveRetryOptions(config.retry, kind), ...(options.onRetry ? { onRetry: options.onRetry } : {}) };
397
+ // `retry.signal` is the caller's ORIGINAL signal (Ctrl-C / turn abort) — not the
398
+ // per-attempt fetch timeout composed below — so a long, honored server-directed
399
+ // retry wait (see resolveRetryOptions) can still be cancelled by the user without
400
+ // waiting out the full delay, while a single attempt's timeout never cuts short
401
+ // the NEXT attempt's backoff sleep.
402
+ const retry: RetryOptions = { ...resolveRetryOptions(config.retry, kind), ...(options.onRetry ? { onRetry: options.onRetry } : {}), ...(callOptions.signal ? { signal: callOptions.signal } : {}) };
387
403
 
388
404
  if (provider === "ollama" || provider === "lmstudio") {
389
405
  return { adapter, callOptions, credential: { kind: "none", provider: "openai" }, retry };
@@ -416,6 +432,11 @@ async function resolveCall(options: Partial<CallOptions>, kind: "request" | "str
416
432
  "OpenAI OAuth 자격증명은 Codex 모델(gpt-5.5/gpt-5.4)만 지원. OPENAI_API_KEY를 설정하거나 모델을 변경하세요"
417
433
  );
418
434
  }
435
+ if (provider === "kimi" && effective.kind === "oauth" && !oauthServesModel("kimi", model)) {
436
+ throw new Error(
437
+ `Kimi OAuth (Kimi Code subscription) serves only the Kimi Code models (${KIMI_CODE_MODELS.join(", ")}). Set KIMI_API_KEY for Moonshot API models, or pick a Kimi Code model with /model.`,
438
+ );
439
+ }
419
440
  if (effective.kind === "none" && !isLocalOpenAi) {
420
441
  throw new Error(
421
442
  `No credential for provider '${provider}'. Run 'jeo setup', 'jeo auth login', or set ${provider.toUpperCase()}_API_KEY / ${provider.toUpperCase()}_OAUTH_TOKEN.`
@@ -12,9 +12,14 @@ const CLAUDE_CODE_VERSION = "2.1.63";
12
12
  const CLAUDE_CODE_SYSTEM_INSTRUCTION = "You are a Claude agent, built on Anthropic's Claude Agent SDK.";
13
13
  const CLAUDE_BILLING_HEADER_PREFIX = "x-anthropic-billing-header:";
14
14
  /** Betas needed for API-key requests: interleaved-thinking enables thinking+tools,
15
- * prompt-caching-scope gives scoped cache breakpoints. */
15
+ * context-management matches gjc's default beta set (sent on ALL requests),
16
+ * prompt-caching-scope gives scoped cache breakpoints.
17
+ * ponytail: fine-grained-tool-streaming-2025-05-14 — gjc sends it and repairs truncated
18
+ * tool-argument JSON; jeo parses accumulated tool JSON without that repair pass, so the
19
+ * beta stays off until a repair step exists. */
16
20
  const ANTHROPIC_API_KEY_BETA = [
17
21
  "interleaved-thinking-2025-05-14",
22
+ "context-management-2025-06-27",
18
23
  "prompt-caching-scope-2026-01-05",
19
24
  ];
20
25
  const ANTHROPIC_OAUTH_BETA = [
@@ -46,8 +51,10 @@ function stripAnthropicPrefix(model: string): string {
46
51
  return model.startsWith("anthropic/") ? model.slice(10) : model;
47
52
  }
48
53
 
49
- function shouldUseClaudeCodeOAuthShape(model: string, credential: Credential): boolean {
50
- return credential.kind === "oauth" && !model.startsWith("claude-3-5-haiku");
54
+ function shouldUseClaudeCodeOAuthShape(model: string, credential: Credential, baseUrl?: string): boolean {
55
+ // Claude-Code OAuth cloaking is for api.anthropic.com only. An OAuth credential
56
+ // against a custom base (e.g. Kimi Code at api.kimi.com/coding) is a plain bearer.
57
+ return credential.kind === "oauth" && !baseUrl && !model.startsWith("claude-3-5-haiku");
51
58
  }
52
59
 
53
60
  function createClaudeCloakingUserId(): string {
@@ -68,9 +75,10 @@ function anthropicSystemBlocks(
68
75
  model: string,
69
76
  credential: Credential,
70
77
  billingPayload: Record<string, unknown>,
78
+ baseUrl?: string,
71
79
  ): AnthropicSystemBlock[] | undefined {
72
80
  const blocks: AnthropicSystemBlock[] = [];
73
- if (shouldUseClaudeCodeOAuthShape(model, credential)) {
81
+ if (shouldUseClaudeCodeOAuthShape(model, credential, baseUrl)) {
74
82
  const billingSeed = systemPrompt ? { ...billingPayload, system: [systemPrompt] } : billingPayload;
75
83
  blocks.push(
76
84
  { type: "text", text: createClaudeBillingHeader(billingSeed) },
@@ -89,17 +97,19 @@ function anthropicSystemBlocks(
89
97
  return blocks;
90
98
  }
91
99
 
92
- /** Anthropic extended-thinking budget by reasoning effort (kept under max_tokens). Cross-provider
93
- * parity (matches Gemini's tiers): minimal/low/medium/high ALL enable thinking with scaling
100
+ /** Anthropic extended-thinking budget by reasoning effort (kept under max_tokens). Tiers match
101
+ * gjc's ANTHROPIC_THINKING table: minimal/low/medium/high ALL enable thinking with scaling
94
102
  * depth — reasoning works at every thinking level (gajae parity: Minimal is a real effort).
95
- * Only an UNSET effort stays non-thinking (the explicit /fast off path). */
96
- function anthropicThinkingBudget(effort: CallOptions["reasoningEffort"], maxTokens: number): number | undefined {
103
+ * Only an UNSET effort stays non-thinking (the explicit /fast off path). The "xhigh" row is
104
+ * correct-by-table even though upstream currently folds xhigh→high before it arrives. */
105
+ function anthropicThinkingBudget(effort: CallOptions["reasoningEffort"] | "xhigh", maxTokens: number): number | undefined {
97
106
  let budget: number;
98
107
  switch (effort) {
99
- case "minimal": budget = 2000; break;
100
- case "low": budget = 4000; break;
101
- case "medium": budget = 10000; break;
102
- case "high": budget = 24000; break;
108
+ case "minimal": budget = 1024; break;
109
+ case "low": budget = 4096; break;
110
+ case "medium": budget = 8192; break;
111
+ case "high": budget = 16384; break;
112
+ case "xhigh": budget = 32768; break;
103
113
  default: return undefined;
104
114
  }
105
115
  return Math.min(budget, Math.max(1024, maxTokens - 1024));
@@ -264,7 +274,7 @@ export function anthropicPayload(
264
274
  // Extended thinking requires max_tokens strictly above the thinking budget.
265
275
  max_tokens: thinkingBudget !== undefined ? Math.max(maxTokens, thinkingBudget + 1024) : maxTokens,
266
276
  };
267
- if (credential.kind === "oauth") payload.metadata = { user_id: createClaudeCloakingUserId() };
277
+ if (shouldUseClaudeCodeOAuthShape(model, credential, options.baseUrl)) payload.metadata = { user_id: createClaudeCloakingUserId() };
268
278
  if (effort !== undefined) {
269
279
  // Enable Claude extended thinking. Extended thinking forbids a custom temperature, so
270
280
  // temperature is only set on the non-thinking path.
@@ -294,7 +304,7 @@ export function anthropicPayload(
294
304
  payload.tool_choice = { type: "auto" };
295
305
  }
296
306
  if (stream) payload.stream = true;
297
- const system = anthropicSystemBlocks(systemPrompt, model, credential, payload);
307
+ const system = anthropicSystemBlocks(systemPrompt, model, credential, payload, options.baseUrl);
298
308
  if (system) payload.system = system;
299
309
  return JSON.stringify(payload);
300
310
  }
@@ -311,7 +321,7 @@ export function anthropicRequest(
311
321
  // Anthropic-compatible providers (z.ai, MiniMax, …) accept the Messages wire
312
322
  // format at their own host; an explicit baseUrl pins `${base}/v1/messages`.
313
323
  url: options.baseUrl ? `${options.baseUrl.replace(/\/$/, "")}/v1/messages` : ANTHROPIC_URL,
314
- headers: headersFor(credential, stream, stripAnthropicPrefix(options.model)),
324
+ headers: { ...headersFor(credential, stream, stripAnthropicPrefix(options.model), options.baseUrl), ...options.extraHeaders },
315
325
  body: anthropicPayload(messages, options, stream, includeTemperature, credential, stripArtifacts),
316
326
  };
317
327
  }
@@ -327,6 +337,22 @@ function isReasoningArtifactError(status: number, detail: string): boolean {
327
337
  return status === 400 && /thinking|signature|redacted_thinking/i.test(detail);
328
338
  }
329
339
 
340
+ /** gjc parity (getSafeAnthropicHeaderEvidence): fold Anthropic's rate-limit response
341
+ * headers into a 429's error detail so the retry classifier can see them. The decisive
342
+ * usage-exhaustion signal (`anthropic-ratelimit-unified-overage-disabled-reason:
343
+ * out_of_credits`) often arrives ONLY as a header — without this, an out-of-credits 429
344
+ * reads like a generic per-minute rate limit and is retried pointlessly. */
345
+ function anthropicRateLimitEvidence(headers: Headers): string {
346
+ const evidence: string[] = [];
347
+ for (const [name, value] of headers) {
348
+ const lower = name.toLowerCase();
349
+ if (lower === "retry-after" || lower === "retry-after-ms" || lower.startsWith("anthropic-ratelimit-")) {
350
+ evidence.push(`${lower}=${value}`);
351
+ }
352
+ }
353
+ return evidence.length > 0 ? ` Anthropic rate-limit evidence: ${evidence.sort().join(", ")}` : "";
354
+ }
355
+
330
356
  async function postAnthropic(
331
357
  messages: Message[],
332
358
  options: CallOptions,
@@ -354,10 +380,13 @@ async function postAnthropic(
354
380
  throw await providerHttpError("Anthropic", response, stream ? "(stream)" : undefined);
355
381
  }
356
382
 
383
+ // 429s carry their rate-limit headers in the DETAIL so isUsageLimitError /
384
+ // friendlyProviderError can classify on the full evidence (gjc parity).
385
+ const enrichedDetail = response.status === 429 ? `${detail}${anthropicRateLimitEvidence(response.headers)}` : detail;
357
386
  throw new ProviderHttpError(
358
387
  "Anthropic",
359
388
  response.status,
360
- detail,
389
+ enrichedDetail,
361
390
  stream ? "(stream)" : undefined,
362
391
  parseRetryAfter(response.headers.get("retry-after")) ?? parseRetryFromBody(detail),
363
392
  );
@@ -543,8 +572,18 @@ function claudeCodeOAuthHeaders(stream: boolean, model: string): Record<string,
543
572
  };
544
573
  }
545
574
 
546
- function headersFor(credential: Credential, stream: boolean, model: string): Record<string, string> {
575
+ function headersFor(credential: Credential, stream: boolean, model: string, baseUrl?: string): Record<string, string> {
547
576
  if (credential.kind === "oauth") {
577
+ // OAuth against a NON-Anthropic base (Kimi Code, …): plain bearer, no Claude-Code
578
+ // cloaking headers/betas. gjc parity: buildAnthropicHeaders' generic-bearer branch.
579
+ if (baseUrl) {
580
+ return {
581
+ accept: stream ? "text/event-stream" : "application/json",
582
+ "content-type": "application/json",
583
+ authorization: `Bearer ${credential.token}`,
584
+ "anthropic-version": "2023-06-01",
585
+ };
586
+ }
548
587
  return {
549
588
  "content-type": "application/json",
550
589
  authorization: `Bearer ${credential.token}`,