From 294b292545454bab650a7acd40f04f6571a1be3b Mon Sep 17 00:00:00 2001 From: Daniel Pardo Date: Mon, 5 Oct 2026 06:29:41 +0200 Subject: [PATCH] feat: make timeoutMs file-configurable like every other knob MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit timeoutMs was the only knob read from the environment (JEV_TIMEOUT_MS) instead of the 7 config layers, so route-A/B users — and GUI users who can only edit jev.yaml — could not tune the latency knob at all. - loadConfig: pick timeoutMs from the layers first via pickNumber with a > 0 per-layer validator; non-positive/bogus counts as unset and falls through lower layers -> env JEV_TIMEOUT_MS -> default 2000. - pickNumber gains an optional validate predicate (minConfidence keeps the unfiltered behaviour, 0 being valid there). - Tests 21/22: file timeoutMs beats env; bogus (0 / "abc") falls back to default and routes normally without hang or throw. - Docs: README config section, jev.yaml.example, DEVELOPMENT.md §2/§5 (also fixed a pre-existing staleness: JEV_MODEL was already a config-layer field, not env-only), HOW_IT_WORKS.md timeout refs. Fixes #10 --- README.md | 9 +++++++-- docs/DEVELOPMENT.md | 13 +++++++----- docs/HOW_IT_WORKS.md | 5 +++-- jev.yaml.example | 5 +++++ src/index.ts | 20 ++++++++++++++---- test/index.test.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f11f1d5..495e525 100644 --- a/README.md +++ b/README.md @@ -113,10 +113,15 @@ Config sources — first defined value wins per field: 5. plugin options (directory-package registrations only) 6. env `TYPESAFE_API_KEY` / `JEV_API_URL` / `JEV_MIN_CONFIDENCE` +Config-file fields: `apiKey`, `apiUrl`, `minConfidence`, `model`, `timeoutMs` +(milliseconds; non-positive or bogus values count as unset) — see +[jev.yaml.example](./jev.yaml.example). `JEV_MODEL` / `JEV_TIMEOUT_MS` are env +fallbacks for `model` / `timeoutMs`. + | Env var | Default | Meaning | | --- | --- | --- | -| `JEV_MODEL` | `jev-latest` | Jev model id | -| `JEV_TIMEOUT_MS` | `2000` | Jev round-trip timeout | +| `JEV_MODEL` | `jev-latest` | Jev model id (fallback for `model`) | +| `JEV_TIMEOUT_MS` | `2000` | Jev round-trip timeout (fallback for `timeoutMs`) | | `JEV_DEBUG_FILE` | `/tmp/opencode-jev.log` | decision log path | | `JEV_DEBUG` | — | `1` echoes the log to stdout | | `JEV_DEBUG_MAX_BYTES` | `262144` | log rotation cap (one `.1` backup) | diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index e7a5771..ae8be45 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -74,7 +74,8 @@ inspection` → `Jev client` → `interception core` → `transport` → `entryp `./.opencode/jev.{yaml,json}` → `~/.config/jev/config.{yaml,json}` → plugin `options` (directory-package registrations only) → env (`TYPESAFE_API_KEY` / `JEV_API_URL` / `JEV_MIN_CONFIDENCE`). -`JEV_MODEL` / `JEV_TIMEOUT_MS` are env-only extras. Constants like +`JEV_MODEL` / `JEV_TIMEOUT_MS` are env fallbacks for the `model` / `timeoutMs` +fields (non-positive or bogus `timeoutMs` counts as unset). Constants like `JEV_DEBUG_FILE` are captured at **module load**, not at setup. ## 3. Dev setup @@ -204,10 +205,12 @@ export TYPESAFE_API_KEY="***" # also: JEV_API_URL, JEV_MIN_CONFIDENCE Env vars must be visible to the **OpenCode server process** — export them in the shell you launch `opencode` from (a macOS GUI-launched app won't see your -zsh exports; use route A or B there). The debug/behavior knobs -`JEV_MODEL`, `JEV_TIMEOUT_MS`, `JEV_DEBUG`, `JEV_DEBUG_FILE`, -`JEV_DEBUG_MAX_BYTES` are **env-only**, read once at module load — put them -in the shell you start OpenCode from. +zsh exports; use route A or B there). The debug knobs `JEV_DEBUG`, +`JEV_DEBUG_FILE`, `JEV_DEBUG_MAX_BYTES` are **env-only**, read once at module +load — put them in the shell you start OpenCode from. `JEV_MODEL` / +`JEV_TIMEOUT_MS` are env fallbacks for the `model` / `timeoutMs` config +fields — a config-file value wins over them; they are read at setup, not +module load. ### Watch it work diff --git a/docs/HOW_IT_WORKS.md b/docs/HOW_IT_WORKS.md index 68f37d2..6c730fe 100644 --- a/docs/HOW_IT_WORKS.md +++ b/docs/HOW_IT_WORKS.md @@ -130,7 +130,8 @@ with N tool iterations and T tool schemas saves roughly `(N_applied) × - **Smaller request = faster prefill.** Fewer input tokens means less prefill work at the provider, every iteration. - **The Jev round-trip is the only added latency** — and it's bounded: - `JEV_TIMEOUT_MS` caps it (default 2000 ms worst case), and a Jev miss + `timeoutMs` caps it (env `JEV_TIMEOUT_MS` fallback; default 2000 ms worst + case), and a Jev miss (timeout/error/low confidence) costs at most that one round-trip, never a retry storm. - **After a bad API key (401/403) the plugin latches off** — zero added @@ -155,7 +156,7 @@ session*. The request is sent untouched when: - Jev's pick isn't in this request's tool list - The request already pins a tool (`tool_choice` is an object or `"none"`) or carries no usable tools -- Jev times out (≤ `JEV_TIMEOUT_MS`, default 2 s), errors, returns malformed +- Jev times out (≤ `timeoutMs`, default 2 s), errors, returns malformed JSON, or the key is invalid - The traffic isn't the agent loop (`event.kind !== "primary"` — title generation, compaction) diff --git a/jev.yaml.example b/jev.yaml.example index 07d49bf..c8c533b 100644 --- a/jev.yaml.example +++ b/jev.yaml.example @@ -10,3 +10,8 @@ apiKey: "apikey_REPLACE_ME" # Optional. Jev model. Default "jev-latest". # model: "jev-latest" + +# Optional. Jev round-trip timeout, milliseconds. Default 2000. +# Non-positive or non-numeric values count as unset (lower layer, then env +# JEV_TIMEOUT_MS, then default). +# timeoutMs: 2000 diff --git a/src/index.ts b/src/index.ts index 54b91c3..b97f5fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,7 +43,8 @@ import { Plugin } from "@opencode/plugin"; * 6. plugin options (ctx.options; only delivered when registered as a * directory package under "plugins", not for auto-discovered files) * 7. env TYPESAFE_API_KEY / JEV_API_URL / JEV_MIN_CONFIDENCE - * Extras: JEV_MODEL (default "jev-latest"), JEV_TIMEOUT_MS (default 2000). + * Env fallbacks: JEV_MODEL (for `model`), JEV_TIMEOUT_MS (for `timeoutMs`, + * default 2000). Non-positive/bogus `timeoutMs` in a layer counts as unset. * * Logging: OpenCode's background service discards plugin console output, so * every decision is appended to /tmp/opencode-jev.log (override with @@ -199,11 +200,20 @@ function pickString(layers: RawConfig[], key: string): string | undefined { return undefined; } -function pickNumber(layers: RawConfig[], key: string): number | undefined { +function pickNumber( + layers: RawConfig[], + key: string, + validate: (value: number) => boolean = () => true, +): number | undefined { for (const layer of layers) { const raw = layer[key]; const value = typeof raw === "string" ? Number(raw) : raw; - if (typeof value === "number" && Number.isFinite(value)) return value; + if ( + typeof value === "number" && + Number.isFinite(value) && + validate(value) + ) + return value; } return undefined; } @@ -226,7 +236,9 @@ async function loadConfig( apiKey, apiUrl: pickString(layers, "apiUrl") ?? DEFAULT_API_URL, minConfidence: Math.min(Math.max(confidence, 0), 1), - timeoutMs: positiveIntFromEnv("JEV_TIMEOUT_MS", DEFAULT_TIMEOUT_MS), + timeoutMs: + pickNumber(layers, "timeoutMs", (v) => v > 0) ?? + positiveIntFromEnv("JEV_TIMEOUT_MS", DEFAULT_TIMEOUT_MS), model: pickString(layers, "model") ?? process.env.JEV_MODEL ?? DEFAULT_MODEL, }; diff --git a/test/index.test.ts b/test/index.test.ts index b0e507c..d46b888 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -516,3 +516,51 @@ test("20. single function tool: passthrough, no fetch", async () => { assert.deepEqual(await bodyJson(event), payload); assert.equal(calls.length, 0); // nothing for jev to decide, no round-trip }); + +test("21. config file timeoutMs beats env JEV_TIMEOUT_MS", async () => { + const cfgPath = path.join(tempDir(), "jev.yaml"); + writeFileSync(cfgPath, "timeoutMs: 50\n"); + const { calls, holder } = await setupPlugin( + { + TYPESAFE_API_KEY: "test-key", + JEV_CONFIG_PATH: cfgPath, + JEV_TIMEOUT_MS: "5000", + }, + (_url, init) => + new Promise((_resolve, reject) => { + init.signal.addEventListener("abort", () => { + reject(init.signal.reason ?? new Error("aborted")); + }); + }), + ); + assert.ok(holder.captured); + const payload = anthropicPayload(); + const event = jsonEvent(ANTHROPIC_URL, payload); + const t0 = Date.now(); + await holder.captured!(event); // must abort at ~50ms, not hang to env's 5000 + const elapsed = Date.now() - t0; + assert.ok(elapsed < 1000, `expected ~50ms file timeout, took ${elapsed}ms`); + assert.deepEqual(await bodyJson(event), payload); // passthrough + assert.equal(calls.length, 1); +}); + +test("22. bogus config timeoutMs (0 / non-numeric): default used, routes normally", async () => { + for (const bogus of ["0", '"abc"']) { + const cfgPath = path.join(tempDir(), "jev.yaml"); + writeFileSync(cfgPath, `timeoutMs: ${bogus}\n`); + const { calls, holder } = await setupPlugin( + { TYPESAFE_API_KEY: "test-key", JEV_CONFIG_PATH: cfgPath }, + () => jevResponse(choice("bash", 0.95)), + ); + assert.ok(holder.captured); + const payload = anthropicPayload(); + const event = jsonEvent(ANTHROPIC_URL, payload); + await holder.captured!(event); // no hang, no throw + // Non-positive/NaN counts as unset -> default 2000ms applies, so the + // instant stub answer arrives and the trim happens (a 0ms timeout would + // abort before the response and passthrough instead). + const body = await bodyJson(event); + assert.deepEqual(body.tools, [{ name: "bash", input_schema: {} }]); + assert.equal(calls.length, 1); + } +});