warden-agent 0.4.0 → 0.4.1

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/README.md CHANGED
@@ -6,7 +6,7 @@ You chat with it in your project folder. It reads your code, edits files, runs c
6
6
 
7
7
  It also includes the original Warden supervisor tools: record and review Claude Code sessions, and run the same task on several models to compare them.
8
8
 
9
- > **Status: early (0.4.0).** Tested live with Gemini 2.5 Flash. Read [What is verified](#what-is-verified).
9
+ > **Status: early (0.4.1).** Tested live with Gemini 2.5 Flash. Read [What is verified](#what-is-verified).
10
10
 
11
11
  ## Install
12
12
 
@@ -75,6 +75,8 @@ Then type what you want, e.g. `the tests in calc_test.py fail, fix them`. Warden
75
75
  ? Allow this change? [y] yes [a] always allow edit_file this session [n] no, or type what to do instead
76
76
  ```
77
77
 
78
+ **You can keep typing while it works.** Press Enter and the message is queued: the model reads it at its next step (after the tool it is running), so you can add details or change course without waiting. What you type shows in the status line.
79
+
78
80
  Inside the app:
79
81
 
80
82
  | | |
@@ -89,8 +91,9 @@ Inside the app:
89
91
  | `/resume` | continue an earlier conversation |
90
92
  | `/init` | the model writes a `WARDEN.md` with notes about your project |
91
93
  | `/usage` | tokens used |
94
+ | `/jobs` | servers and watchers the model started in the background; `/jobs stop <id>` or `/jobs stop all` |
92
95
  | `!command` | run a shell command yourself; the model sees the output |
93
- | Ctrl+C | interrupt the model; twice at an empty prompt quits |
96
+ | Esc or Ctrl+C | interrupt the model (the conversation is kept); Ctrl+C twice at an empty prompt quits |
94
97
 
95
98
  Other ways to start:
96
99
 
@@ -105,7 +108,9 @@ If a `WARDEN.md`, `AGENTS.md` or `CLAUDE.md` exists in the folder, its contents
105
108
 
106
109
  ## Tools the model can use
107
110
 
108
- `read_file`, `write_file`, `edit_file` (exact text replacement), `list_dir`, `glob`, `grep`, and `bash`. On Windows, `bash` uses Git Bash if installed, otherwise PowerShell.
111
+ `read_file`, `write_file` (with `append` for writing big files in parts), `edit_file` (exact text replacement), `list_dir`, `glob`, `grep`, `bash`, and `job`. On Windows, `bash` uses Git Bash if installed, otherwise PowerShell.
112
+
113
+ Commands that keep running (dev servers, watchers) are started with `background=true`: Warden returns after their first output, the `job` tool reads their latest output or stops them, and Warden stops them all when it exits. A command that runs too long is stopped together with everything it started, so Warden never hangs on it.
109
114
 
110
115
  ## Safety
111
116
 
@@ -133,6 +138,9 @@ If a `WARDEN.md`, `AGENTS.md` or `CLAUDE.md` exists in the folder, its contents
133
138
  "mode": "ask",
134
139
  "maxSteps": 60,
135
140
  "contextTokens": 120000,
141
+ "requestTimeoutSec": 180,
142
+ "idleTimeoutSec": 120,
143
+ "stallTimeoutSec": 300,
136
144
  "providers": {
137
145
  "deepseek": { "api": "openai", "baseUrl": "https://api.deepseek.com/v1", "keyEnv": ["DEEPSEEK_API_KEY"] }
138
146
  }
@@ -142,6 +150,8 @@ If a `WARDEN.md`, `AGENTS.md` or `CLAUDE.md` exists in the folder, its contents
142
150
 
143
151
  Any OpenAI-compatible server can be added under `providers` and used as `name/model`.
144
152
 
153
+ The timeouts stop a model request that hangs: no response at all after `requestTimeoutSec` (tried twice), a reply that goes silent for `idleTimeoutSec`, or a connection that sends only keep-alive pings for `stallTimeoutSec`. Your conversation is kept; send the message again.
154
+
145
155
  ## Supervisor tools (from Warden 0.1)
146
156
 
147
157
  Record Claude Code sessions, review them with other models, and compare models on the same task:
@@ -156,7 +166,8 @@ See [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md) for setup, hook configuration and
156
166
 
157
167
  ## What is verified
158
168
 
159
- - `npm test` runs offline: a mock model server that streams tool calls, the real tools on a temp project, permissions, rate-limit retries, interrupts, saved sessions, the terminal app driven through a pipe, and the supervisor tests.
169
+ - `npm test` runs offline: a mock model server that streams tool calls, the real tools on a temp project, permissions, rate-limit retries, interrupts, timeouts (no response, silent stream, keep-alive only), reasoning streams, cut-off replies, saved sessions, background jobs, the terminal app driven through a pipe, and the supervisor tests.
170
+ - The interactive app is also tested on a simulated terminal with real keystrokes: chatting turn after turn, typing while the model works, Esc and Ctrl+C (also during an approval question), a model that stops responding, and continuing after each.
160
171
  - **Live:** Gemini 2.5 Flash through Google's OpenAI-compatible endpoint. It found, read and fixed a bug, streamed its answer, asked for approval, and respected a denial. The `/add` tool test identified it as native tool calling. Its free daily quota (20 requests) ran out during testing, which is how the quota handling was found and fixed.
161
172
  - **Live model lists:** OpenRouter (466 models, 16 free), Hugging Face (126, free monthly credits rather than free models) and NVIDIA (65 chat models) were listed through Warden's own code without keys.
162
173
  - **Not tested live:** chatting through OpenRouter, Hugging Face, NVIDIA, Groq, Cerebras, Mistral, DeepSeek, Ollama, LM Studio, OpenAI and Anthropic (no keys or local servers were available). The text tool format was tested against a mock model only. They use the same code path as Gemini (except Anthropic, which has its own).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "warden-agent",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "A terminal coding agent for any model (Gemini, OpenRouter, Groq, Ollama, OpenAI, Anthropic), plus tools to review and compare coding agents.",
5
5
  "keywords": [
6
6
  "ai",
@@ -36,7 +36,7 @@
36
36
  "scripts": {
37
37
  "daemon": "node src/cli.mjs daemon",
38
38
  "watch": "node src/cli.mjs watch",
39
- "test": "node test/meta.mock.mjs && node test/agent.mock.mjs && node test/models.mock.mjs && node test/providers.mock.mjs && node test/arena.mock.mjs && node test/harnesses.mock.mjs",
39
+ "test": "node test/meta.mock.mjs && node test/agent.mock.mjs && node test/tui.mock.mjs && node test/models.mock.mjs && node test/providers.mock.mjs && node test/arena.mock.mjs && node test/harnesses.mock.mjs",
40
40
  "build:standalone": "node scripts/build-standalone.mjs"
41
41
  },
42
42
  "engines": {
@@ -13,6 +13,9 @@ export const AGENT_DEFAULTS = {
13
13
  maxOutputTokens: 8192,
14
14
  maxSteps: 60, // model calls per user message before Warden stops and asks you
15
15
  contextTokens: 120000, // rough budget; old tool output is trimmed above this
16
+ requestTimeoutSec: 180, // a model that has not started answering after this long is given up on (retried once)
17
+ idleTimeoutSec: 120, // a reply that goes completely silent this long is stopped
18
+ stallTimeoutSec: 300, // a connection kept open with no reply at all (only keep-alive pings) this long is stopped
16
19
  providers: {}, // extra or overridden providers: { "name": { "api": "openai", "baseUrl": "...", "keyEnv": ["ENV"] } }
17
20
  recentModels: [],
18
21
  };
@@ -1,9 +1,8 @@
1
- // The agent loop: send the conversation to the model, run the tools it asks for (with permission), repeat until
2
- // it answers without tool calls. The UI is injected so the same loop runs in the terminal app and in headless mode.
1
+ // The agent loop: send the conversation to the model, run the tools it asks for (with approvals), repeat until it answers.
3
2
  import { chat, toolsUnsupported } from "./providers.mjs";
4
- import { findModel, toolModeFor, setToolMode } from "./models.mjs";
5
3
  import { TOOLS, toolByName, needsApproval, describeCall } from "./tools.mjs";
6
4
  import { systemPrompt } from "./prompt.mjs";
5
+ import { findModel, toolModeFor, setToolMode } from "./models.mjs";
7
6
 
8
7
  const isAbort = (e) => e?.name === "AbortError" || /aborted/i.test(e?.message || "");
9
8
 
@@ -19,20 +18,56 @@ export function trimContext(messages, budgetTokens) {
19
18
  return trimmed;
20
19
  }
21
20
 
21
+ // File contents the model already wrote are on disk; keeping every full copy in the conversation makes each later
22
+ // request bigger and slower (and burns free-tier token limits). Older write_file / edit_file arguments are replaced
23
+ // by a short note; the model reads the file again when it needs it. The latest assistant message is left alone.
24
+ const BIG = 1500;
25
+ export function compactWrites(messages) {
26
+ let lastAssistant = -1;
27
+ for (let i = messages.length - 1; i >= 0; i--) if (messages[i].role === "assistant") { lastAssistant = i; break; }
28
+ let n = 0;
29
+ for (let i = 0; i < lastAssistant; i++) {
30
+ const m = messages[i];
31
+ if (m.role !== "assistant" || !m.toolCalls?.length) continue;
32
+ for (const c of m.toolCalls) {
33
+ const a = c.args;
34
+ if (!a || c.compacted || typeof a !== "object") continue;
35
+ if (c.name === "write_file" && String(a.content ?? "").length > BIG) {
36
+ const lines = String(a.content).split("\n").length;
37
+ c.args = { ...a, content: `[${lines} lines ${a.append ? "appended" : "written"} earlier; removed from the conversation to save space. Read ${a.path} to see the current contents.]` };
38
+ c.compacted = true; n++;
39
+ } else if (c.name === "edit_file" && String(a.old_string ?? "").length + String(a.new_string ?? "").length > BIG * 2) {
40
+ c.args = { ...a, old_string: `[${String(a.old_string).split("\n").length} lines, removed to save space]`, new_string: `[${String(a.new_string).split("\n").length} lines, removed to save space; read ${a.path} for the current contents]` };
41
+ c.compacted = true; n++;
42
+ }
43
+ }
44
+ }
45
+ return n;
46
+ }
47
+
48
+ const TRUNCATED_ARGS = (name, limit) =>
49
+ `Your reply hit the output limit (${limit} tokens) while you were still writing the arguments for ${name}, so the call was cut off and nothing was done.` +
50
+ (name === "write_file" ? " Write big files in parts: call write_file with the first part (under about 300 lines), then write_file with append=true for each further part." : " Send a shorter call, splitting the work into several smaller calls.");
51
+
22
52
  /**
23
53
  * Runs one user message to completion.
24
- * ui: { onAssistantStart, onText, onAssistantEnd, onToolStart, onToolEnd, confirm, onRetry, onNotice }
54
+ * ui: { onAssistantStart, onText, onReasoning, onAssistantEnd, onToolStart, onToolOutput, onToolEnd, confirm, onRetry, onNotice,
55
+ * takeQueued, onQueuedDelivered }
56
+ * takeQueued(): text the user typed while the model was working (or null). It is handed to the model between steps,
57
+ * so the user can steer without waiting for the whole task to finish.
25
58
  * perm: { mode, allowed: Set<toolName> }
26
59
  */
27
60
  export async function runTurn({ session, userText, cfg, ui, perm, signal, cwd = session.cwd }) {
28
61
  session.push({ role: "user", content: userText });
29
62
  const tools = TOOLS.map(({ name, description, parameters }) => ({ name, description, parameters }));
63
+ const limit = cfg.maxOutputTokens || 8192;
30
64
 
31
65
  for (let step = 1; ; step++) {
32
66
  if (step > cfg.maxSteps) {
33
67
  ui.onNotice?.(`Stopped after ${cfg.maxSteps} model calls for one message. Say "continue" to keep going.`);
34
68
  return { stopped: "max_steps" };
35
69
  }
70
+ compactWrites(session.messages);
36
71
  // a saved model may have a smaller context window than the global budget
37
72
  const saved = findModel(cfg, session.model);
38
73
  const budget = saved?.ctx ? Math.min(cfg.contextTokens, Math.floor(saved.ctx * 0.7)) : cfg.contextTokens;
@@ -46,12 +81,12 @@ export async function runTurn({ session, userText, cfg, ui, perm, signal, cwd =
46
81
  try {
47
82
  res = await chat({
48
83
  spec: session.model, system: systemPrompt({ cwd, mode: perm.mode }), messages: session.messages, tools,
49
- cfg, signal, maxOutputTokens: cfg.maxOutputTokens, toolMode,
50
- onText: (d) => { partial += d; ui.onText?.(d); }, onRetry: ui.onRetry,
84
+ cfg, signal, maxOutputTokens: limit, toolMode, timeouts: cfg,
85
+ onText: (d) => { partial += d; ui.onText?.(d); }, onReasoning: (d) => ui.onReasoning?.(d), onToolDelta: (n, a) => ui.onToolDelta?.(n, a), onRetry: ui.onRetry,
51
86
  });
52
87
  } catch (e) {
53
88
  ui.onAssistantEnd?.(partial);
54
- if (isAbort(e) || signal?.aborted) {
89
+ if (signal?.aborted || (isAbort(e) && !e.timeout)) {
55
90
  session.push({ role: "assistant", content: (partial ? partial + "\n" : "") + "[interrupted by the user]" });
56
91
  return { interrupted: true };
57
92
  }
@@ -63,22 +98,45 @@ export async function runTurn({ session, userText, cfg, ui, perm, signal, cwd =
63
98
  step--;
64
99
  continue;
65
100
  }
66
- // keep the conversation valid for the next message: the user turn stays, no half assistant message
101
+ // keep the conversation valid for the next message: the user turn stays; what streamed so far is kept as context
102
+ if (partial) session.push({ role: "assistant", content: partial + "\n[reply cut off: " + String(e.message).slice(0, 120) + "]" });
67
103
  throw e;
68
104
  }
69
105
  ui.onAssistantEnd?.(res.text);
70
106
  session.usage.calls++;
71
107
  if (res.usage) { session.usage.input += res.usage.input; session.usage.output += res.usage.output; }
108
+ const cutOff = /^(length|max_tokens|MAX_TOKENS)$/.test(res.stopReason || "");
72
109
  session.push({ role: "assistant", content: res.text, ...(res.toolCalls.length ? { toolCalls: res.toolCalls } : {}) });
73
- if (!res.toolCalls.length) return { done: true, text: res.text, stopReason: res.stopReason };
110
+
111
+ if (!res.toolCalls.length) {
112
+ const queued = ui.takeQueued?.();
113
+ if (queued) { // the user said something while this step ran: keep going with it instead of ending the turn
114
+ session.push({ role: "user", content: queued });
115
+ ui.onQueuedDelivered?.(queued);
116
+ continue;
117
+ }
118
+ if (!res.text.trim()) {
119
+ ui.onNotice?.(cutOff
120
+ ? `The model used its whole output limit (${limit} tokens) without writing a reply, often by thinking too long. Ask again, ask for something smaller, or raise "maxOutputTokens" in settings.`
121
+ : `The model sent an empty reply${res.stopReason ? ` (finish reason: ${res.stopReason})` : ""}. Ask again or rephrase; if it keeps happening, try another model with /model.`);
122
+ return { done: true, text: "", empty: true, stopReason: res.stopReason };
123
+ }
124
+ if (cutOff) ui.onNotice?.(`The reply hit the output limit (${limit} tokens) and was cut off. Say "continue" for the rest.`);
125
+ return { done: true, text: res.text, stopReason: res.stopReason };
126
+ }
74
127
 
75
128
  // Every tool call must get a result, even when interrupted, or the next request is invalid.
76
129
  for (const call of res.toolCalls) {
77
130
  const tool = toolByName(call.name);
78
131
  const result = (content, isError = true) => session.push({ role: "tool", toolCallId: call.id, name: call.name, content, ...(isError ? { isError: true } : {}) });
79
132
  if (signal?.aborted) { result("Interrupted by the user before this ran."); continue; }
133
+ if (call.args?.__invalid_json !== undefined) {
134
+ const truncated = cutOff || call.args.__truncated;
135
+ result(truncated ? TRUNCATED_ARGS(tool?.name || "the tool call", limit) : "Your tool arguments were not valid JSON. Send them again as a JSON object.");
136
+ ui.onToolEnd?.(call, tool, { error: truncated ? "cut off at the output limit; the model will retry in smaller parts" : "invalid arguments" });
137
+ continue;
138
+ }
80
139
  if (!tool) { result(`Unknown tool "${call.name}". Available: ${TOOLS.map((t) => t.name).join(", ")}`); ui.onToolEnd?.(call, null, { error: "unknown tool" }); continue; }
81
- if (call.args?.__invalid_json !== undefined) { result("Your tool arguments were not valid JSON. Send them again as a JSON object."); ui.onToolEnd?.(call, tool, { error: "invalid arguments" }); continue; }
82
140
 
83
141
  ui.onToolStart?.(call, tool);
84
142
  if (needsApproval(tool, call.args, { cwd, mode: perm.mode, allowed: perm.allowed })) {
@@ -90,9 +148,10 @@ export async function runTurn({ session, userText, cfg, ui, perm, signal, cwd =
90
148
  ui.onToolEnd?.(call, tool, { denied: true });
91
149
  continue;
92
150
  }
151
+ ui.onToolApproved?.(call, tool);
93
152
  }
94
153
  try {
95
- const out = await tool.run(call.args || {}, { cwd, signal });
154
+ const out = await tool.run(call.args || {}, { cwd, signal, onOutput: (s) => ui.onToolOutput?.(call, s) });
96
155
  result(out.content, !!out.isError);
97
156
  ui.onToolEnd?.(call, tool, out);
98
157
  } catch (e) {
@@ -101,5 +160,8 @@ export async function runTurn({ session, userText, cfg, ui, perm, signal, cwd =
101
160
  }
102
161
  }
103
162
  if (signal?.aborted) return { interrupted: true };
163
+ // anything the user typed meanwhile goes to the model now, before its next step
164
+ const queued = ui.takeQueued?.();
165
+ if (queued) { session.push({ role: "user", content: queued }); ui.onQueuedDelivered?.(queued); }
104
166
  }
105
167
  }
@@ -34,9 +34,13 @@ export function systemPrompt({ cwd, mode }) {
34
34
  How to work:
35
35
  - Use the tools to look before you act. Read a file before editing it. Search with glob/grep instead of guessing paths.
36
36
  - Make the smallest change that does what was asked. Do not refactor, rename or add features nobody asked for.
37
- - Prefer edit_file for changes to existing files; use write_file only for new files or full rewrites.
37
+ - Prefer edit_file for changes to existing files; use write_file only for new files or full rewrites. When the user asks for a change to something you already made, read the file and make small edit_file changes; do not rewrite the whole file to change a few lines.
38
+ - Write large files (more than about 300 lines) in parts: write_file with the first part, then write_file with append=true for the rest. A single huge tool call can be cut off by the output limit.
39
+ - File contents you wrote earlier may be removed from the conversation to save space. Read a file again before editing it.
38
40
  - After changing code, run the relevant tests or a quick check with bash when one exists, and say what you ran and what happened. If you did not verify something, say so plainly.
39
41
  - Use bash for commands (tests, builds, git, package managers). Do not use it to read or write files.
42
+ - Never run anything that keeps running (dev servers, \`npm run dev\`, \`npx serve\`, \`python -m http.server\`, file watchers) without background=true: it would block until it times out. Start it with background=true, check it with the job tool, and tell the user the address. To show a static page, you can also just tell the user which file to open in a browser.
43
+ - The user may send new messages while you work. They arrive as user messages between your steps: read them and follow the latest instructions.
40
44
  - Never claim you did something you did not do. If a tool fails, read the error and adjust; do not repeat the same failing call.
41
45
  - If the user denies a tool call, do not retry it the same way; adapt or ask.
42
46
  - Do not run destructive commands (deleting data, force-pushing, rewriting git history) unless the user explicitly asked.
@@ -69,8 +69,12 @@ export function toOpenAI(system, messages) {
69
69
  export function toAnthropic(messages) {
70
70
  const out = [];
71
71
  for (const m of messages) {
72
- if (m.role === "user") out.push({ role: "user", content: [{ type: "text", text: m.content }] });
73
- else if (m.role === "assistant") {
72
+ if (m.role === "user") {
73
+ // a message typed while tools ran (or after a failed request) follows another user turn: merge them
74
+ const last = out.at(-1);
75
+ if (last?.role === "user") last.content.push({ type: "text", text: m.content });
76
+ else out.push({ role: "user", content: [{ type: "text", text: m.content }] });
77
+ } else if (m.role === "assistant") {
74
78
  const content = [];
75
79
  if (m.content) content.push({ type: "text", text: m.content });
76
80
  for (const c of m.toolCalls || []) content.push({ type: "tool_use", id: c.id, name: c.name, input: c.args ?? {} });
@@ -96,9 +100,74 @@ const sleep = (ms, signal) => new Promise((res, rej) => {
96
100
  signal?.addEventListener("abort", () => { clearTimeout(t); rej(new DOMException("aborted", "AbortError")); }, { once: true });
97
101
  });
98
102
 
99
- async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4 }) {
103
+ // ---- timeouts ------------------------------------------------------------------
104
+ // A model request must never hang forever. Three limits, all in seconds and changeable in settings:
105
+ // requestTimeoutSec no answer at all (not even headers) after this long: give up on the attempt (retried once)
106
+ // idleTimeoutSec a streaming reply goes completely silent this long: stop
107
+ // stallTimeoutSec only keep-alive pings, no text / reasoning / tool calls, this long: stop
108
+ export const TIMEOUT_DEFAULTS = { requestTimeoutSec: 180, idleTimeoutSec: 120, stallTimeoutSec: 300 };
109
+
110
+ export class ModelTimeoutError extends Error {
111
+ constructor(message) { super(message); this.name = "ModelTimeoutError"; this.timeout = true; }
112
+ }
113
+
114
+ export function timeoutsFrom(cfg = {}) {
115
+ const sec = (k) => { const v = Number(cfg?.[k]); return (Number.isFinite(v) && v > 0 ? v : TIMEOUT_DEFAULTS[k]) * 1000; };
116
+ return { request: sec("requestTimeoutSec"), idle: sec("idleTimeoutSec"), stall: sec("stallTimeoutSec") };
117
+ }
118
+ const secs = (ms) => (ms % 1000 ? (ms / 1000).toFixed(1) : String(ms / 1000));
119
+
120
+ // Reads a fetch body, calling onLine for each line, and stops with ModelTimeoutError when the stream goes quiet.
121
+ // progress() is called by the parser when real content arrives (text, reasoning or tool calls).
122
+ async function readStream(r, { signal, idleMs, stallMs, onLine, label }) {
123
+ const reader = r.body.getReader();
124
+ const dec = new TextDecoder();
125
+ let buf = "", fired = null;
126
+ const stop = (why) => { if (!fired) { fired = why; reader.cancel().catch(() => {}); } };
127
+ let idle = setTimeout(() => stop("idle"), idleMs);
128
+ let stall = setTimeout(() => stop("stall"), stallMs);
129
+ const progress = () => { clearTimeout(stall); stall = setTimeout(() => stop("stall"), stallMs); };
130
+ try {
131
+ for (;;) {
132
+ const { done, value } = await reader.read();
133
+ if (done) break;
134
+ clearTimeout(idle); idle = setTimeout(() => stop("idle"), idleMs);
135
+ buf += dec.decode(value, { stream: true });
136
+ let i;
137
+ while ((i = buf.indexOf("\n")) >= 0) {
138
+ const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
139
+ onLine(line, progress);
140
+ }
141
+ }
142
+ if (buf.trim()) onLine(buf.trim(), progress);
143
+ } catch (e) {
144
+ if (!fired || signal?.aborted) { reader.cancel().catch(() => {}); throw e; }
145
+ } finally {
146
+ clearTimeout(idle); clearTimeout(stall);
147
+ }
148
+ if (fired === "idle") throw new ModelTimeoutError(`${label} stopped sending anything for ${secs(idleMs)}s, so Warden stopped waiting. Send your message again, or switch models with /model.`);
149
+ if (fired === "stall") throw new ModelTimeoutError(`${label} kept the connection open for ${secs(stallMs)}s without sending any reply (it may be overloaded), so Warden stopped waiting. Try again, or switch models with /model.`);
150
+ }
151
+
152
+ async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4, timeoutMs = TIMEOUT_DEFAULTS.requestTimeoutSec * 1000, label = "The model" }) {
153
+ let timeoutRetries = 1;
100
154
  for (let attempt = 0; ; attempt++) {
101
- const r = await fetch(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body), signal });
155
+ // each attempt gets its own deadline for the response to start; the user's interrupt still applies throughout
156
+ const deadline = new AbortController();
157
+ const timer = setTimeout(() => deadline.abort(), timeoutMs);
158
+ let r;
159
+ try {
160
+ r = await fetch(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body), signal: signal ? AbortSignal.any([signal, deadline.signal]) : deadline.signal });
161
+ } catch (e) {
162
+ if (signal?.aborted || !deadline.signal.aborted) throw e;
163
+ if (timeoutRetries-- > 0) {
164
+ onRetry?.({ status: "no response", waitMs: 0, attempt: attempt + 1, message: `no response after ${secs(timeoutMs)}s` });
165
+ continue;
166
+ }
167
+ throw new ModelTimeoutError(`${label} did not respond within ${secs(timeoutMs)}s (tried twice). It may be overloaded: try again, or switch models with /model.`);
168
+ } finally {
169
+ clearTimeout(timer);
170
+ }
102
171
  if (r.ok) return r;
103
172
  const text = await r.text().catch(() => "");
104
173
  let msg = text, hintedDelay = null;
@@ -132,16 +201,23 @@ async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4
132
201
  /**
133
202
  * One model call. Streams text through onText. Returns { text, toolCalls, usage, stopReason }.
134
203
  */
135
- export async function chat({ spec, system, messages, tools, cfg = {}, signal, onText = () => {}, onRetry, maxOutputTokens = 8192, env = process.env, toolMode = "native" }) {
204
+ export async function chat({ spec, system, messages, tools, cfg = {}, signal, onText = () => {}, onReasoning = () => {}, onToolDelta = () => {}, onRetry, maxOutputTokens = 8192, env = process.env, toolMode = "native", timeouts }) {
136
205
  const { provider, model } = parseModelSpec(spec);
137
206
  const p = resolveProvider(provider, cfg);
138
207
  const key = keyFor(provider, p, env);
139
- const send = (o) => (p.api === "anthropic" ? anthropicChat({ p, key, model, signal, onRetry, maxOutputTokens, ...o }) : openaiChat({ p, key, model, signal, onRetry, maxOutputTokens, ...o }));
208
+ const t = timeoutsFrom(timeouts || cfg);
209
+ const label = `The model (${spec})`;
210
+ const common = { p, key, model, signal, onRetry, maxOutputTokens, t, label, onReasoning, onToolDelta };
211
+ const send = (o) => (p.api === "anthropic" ? anthropicChat({ ...common, ...o }) : openaiChat({ ...common, ...o }));
140
212
 
141
213
  if (toolMode === "text" && tools?.length) {
142
214
  // the model writes <tool_call> blocks; hide them from the stream and parse them afterwards
143
215
  const filter = new ToolTagFilter(onText);
144
- const res = await send({ system: system + textToolsPrompt(tools), messages: toTextProtocol(messages), tools: null, onText: (d) => filter.write(d) });
216
+ let hidden = "";
217
+ const res = await send({ system: system + textToolsPrompt(tools), messages: toTextProtocol(messages), tools: null, onText: (d) => {
218
+ filter.write(d);
219
+ if (filter.inside) { hidden += d; try { onToolDelta(hidden.match(/"name"s*:s*"([w-]+)"/)?.[1] || "", hidden); } catch {} } else hidden = "";
220
+ } });
145
221
  filter.end();
146
222
  const parsed = parseTextToolCalls(res.text);
147
223
  return { ...res, text: parsed.text, toolCalls: parsed.toolCalls };
@@ -153,53 +229,51 @@ export async function chat({ spec, system, messages, tools, cfg = {}, signal, on
153
229
  return send({ system, messages, tools, onText });
154
230
  }
155
231
 
156
- async function openaiChat({ p, key, model, system, messages, tools, signal, onText, onRetry, maxOutputTokens }) {
232
+ async function openaiChat({ p, key, model, system, messages, tools, signal, onText, onReasoning, onToolDelta, onRetry, maxOutputTokens, t, label }) {
157
233
  const body = { model, messages: toOpenAI(system, messages), stream: true, [p.maxTokensParam || "max_tokens"]: maxOutputTokens };
158
234
  if (tools?.length) body.tools = toolsOpenAI(tools);
159
235
  if (p.streamUsage !== false && !p.local) body.stream_options = { include_usage: true };
160
236
  const headers = key ? { authorization: `Bearer ${key}` } : {};
237
+ const post = () => postWithRetry(`${p.baseUrl}/chat/completions`, headers, body, { signal, onRetry, timeoutMs: t.request, label });
161
238
  let r;
162
239
  try {
163
- r = await postWithRetry(`${p.baseUrl}/chat/completions`, headers, body, { signal, onRetry });
240
+ r = await post();
164
241
  } catch (e) {
165
242
  // some compatible servers reject stream_options; retry once without it
166
243
  if (e.status === 400 && body.stream_options && /stream_options|include_usage|unknown/i.test(e.message)) {
167
244
  delete body.stream_options;
168
- r = await postWithRetry(`${p.baseUrl}/chat/completions`, headers, body, { signal, onRetry });
245
+ r = await post();
169
246
  } else throw e;
170
247
  }
171
248
 
172
- let text = "", stopReason = null, usage = null, buf = "";
249
+ let text = "", stopReason = null, usage = null;
173
250
  const calls = new Map(); // index -> {id, name, args}
174
- const dec = new TextDecoder();
175
- const handle = (data) => {
251
+ const handle = (data, progress) => {
176
252
  if (data === "[DONE]") return;
177
253
  let j; try { j = JSON.parse(data); } catch { return; }
178
- if (j.error) throw new Error(j.error.message || JSON.stringify(j.error));
254
+ if (j.error) { const e = new Error(j.error.message || JSON.stringify(j.error)); e.status = Number(j.error.code) || undefined; throw e; }
179
255
  if (j.usage) usage = { input: j.usage.prompt_tokens ?? 0, output: j.usage.completion_tokens ?? 0 };
180
256
  const ch = j.choices?.[0];
181
257
  if (!ch) return;
182
258
  const d = ch.delta || ch.message || {};
183
- if (typeof d.content === "string" && d.content) { text += d.content; onText(d.content); }
259
+ // reasoning models stream their thinking separately (field name differs by server); show activity, keep it out of the reply
260
+ const thought = d.reasoning_content ?? d.reasoning ?? d.thinking;
261
+ if (typeof thought === "string" && thought) { progress(); onReasoning(thought); }
262
+ if (typeof d.content === "string" && d.content) { progress(); text += d.content; onText(d.content); }
184
263
  for (const tc of d.tool_calls || []) {
264
+ progress();
185
265
  const idx = tc.index ?? calls.size;
186
266
  const cur = calls.get(idx) || { id: "", name: "", args: "" };
187
267
  if (tc.id) cur.id = tc.id;
188
268
  if (tc.function?.name) cur.name += tc.function.name;
189
269
  if (tc.function?.arguments) cur.args += typeof tc.function.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function.arguments);
190
270
  calls.set(idx, cur);
271
+ try { onToolDelta(cur.name, cur.args); } catch {}
191
272
  }
192
273
  if (ch.finish_reason) stopReason = ch.finish_reason;
193
274
  };
194
- for await (const chunk of r.body) {
195
- buf += dec.decode(chunk, { stream: true });
196
- let i;
197
- while ((i = buf.indexOf("\n")) >= 0) {
198
- const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
199
- if (line.startsWith("data:")) handle(line.slice(5).trim());
200
- }
201
- }
202
- if (buf.trim().startsWith("data:")) handle(buf.trim().slice(5).trim());
275
+ // lines starting with ":" are keep-alive comments (OpenRouter sends them while a model is queued): not progress
276
+ await readStream(r, { signal, idleMs: t.idle, stallMs: t.stall, label, onLine: (line, progress) => { if (line.startsWith("data:")) handle(line.slice(5).trim(), progress); } });
203
277
 
204
278
  const toolCalls = [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([i, c], n) => {
205
279
  let args = {};
@@ -209,10 +283,11 @@ async function openaiChat({ p, key, model, system, messages, tools, signal, onTe
209
283
  return { text, toolCalls, usage, stopReason };
210
284
  }
211
285
 
212
- async function anthropicChat({ p, key, model, system, messages, tools, signal, onText, onRetry, maxOutputTokens }) {
286
+ async function anthropicChat({ p, key, model, system, messages, tools, signal, onText, onRetry, maxOutputTokens, t, label }) {
213
287
  const body = { model, system, max_tokens: maxOutputTokens, messages: toAnthropic(messages) };
214
288
  if (tools?.length) body.tools = toolsAnthropic(tools);
215
- const r = await postWithRetry(`${p.baseUrl}/messages`, { "x-api-key": key, "anthropic-version": "2023-06-01" }, body, { signal, onRetry });
289
+ // not streamed: the whole reply arrives at once, so allow the request time plus the stall time for it
290
+ const r = await postWithRetry(`${p.baseUrl}/messages`, { "x-api-key": key, "anthropic-version": "2023-06-01" }, body, { signal, onRetry, timeoutMs: t.request + t.stall, label });
216
291
  const j = await r.json();
217
292
  let text = "";
218
293
  const toolCalls = [];
@@ -54,10 +54,16 @@ export function parseTextToolCalls(reply, idPrefix = "tc") {
54
54
  toolCalls.push(name ? { id: `${idPrefix}_${Date.now().toString(36)}_${n++}`, name: String(name), args: typeof args === "string" ? safeJson(args) : args }
55
55
  : { id: `${idPrefix}_${Date.now().toString(36)}_${n++}`, name: "__invalid__", args: { __invalid_json: body } });
56
56
  }
57
- const text = reply.replace(re, "").replace(/<tool_call>[\s\S]*$/, "").replace(/\n{3,}/g, "\n\n").trim();
57
+ // a <tool_call> that never closed was cut off (usually by the output limit): report it instead of dropping it silently
58
+ const rest = reply.replace(re, "");
59
+ const open = rest.indexOf("<tool_call>");
60
+ if (open >= 0) toolCalls.push({ id: `${idPrefix}_${Date.now().toString(36)}_${n++}`, name: guessName(rest.slice(open)), args: { __invalid_json: rest.slice(open + 11, open + 211), __truncated: true } });
61
+ const text = rest.replace(/<tool_call>[\s\S]*$/, "").replace(/\n{3,}/g, "\n\n").trim();
58
62
  return { text, toolCalls };
59
63
  }
60
64
 
65
+ const guessName = (s) => s.match(/"name"\s*:\s*"([\w-]+)"/)?.[1] || "__invalid__";
66
+
61
67
  function safeJson(s) { try { return JSON.parse(s); } catch { return { __invalid_json: s }; } }
62
68
 
63
69
  // Streaming filter: hides <tool_call>…</tool_call> from what the user sees while the reply streams in.