From 1f4f84ec40f27eeeb73096525adbc4dc0ca42ee5 Mon Sep 17 00:00:00 2001 From: wolfy Date: Sun, 31 May 2026 14:12:55 -0500 Subject: [PATCH] chore: some retry logic --- agents/ollama.ts | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/agents/ollama.ts b/agents/ollama.ts index c737a61..3d4c9f7 100644 --- a/agents/ollama.ts +++ b/agents/ollama.ts @@ -2,6 +2,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; import { Ollama, type Message, type ToolCall } from "ollama"; import { log } from "../utils/cli.ts"; +import { retry } from "../utils/retry.ts"; import { agent, type AgentResult, type AgentRunContext } from "./shared.ts"; const DEFAULT_MODEL = "qwen3.6:35b"; @@ -113,20 +114,38 @@ async function runOllamaLoop(ctx: AgentRunContext): Promise { let response: Awaited>; try { - response = await ollama.chat({ - model, - messages, - tools, - keep_alive: -1, - think: false, - options: { num_ctx: 262144 }, - }); + response = await retry( + () => ollama.chat({ + model, + messages, + tools, + keep_alive: -1, + think: false, + options: { num_ctx: 262144 }, + }), + { + delaysMs: [3_000, 8_000], + shouldRetry: (err) => { + const msg = err instanceof Error ? err.message : String(err); + return /unexpected EOF|XML syntax error|ECONNRESET|ETIMEDOUT|fetch failed/i.test(msg); + }, + label: `Ollama turn ${iterations}`, + }, + ); } catch (err) { const lastError = err instanceof Error ? err.message : String(err); log.error(`Ollama error: ${lastError}`); return { success: false, error: `Ollama request failed: ${lastError}` }; } + const promptTokens = (response as Record).prompt_eval_count as number | undefined; + const evalTokens = (response as Record).eval_count as number | undefined; + if (promptTokens !== undefined) { + const total = promptTokens + (evalTokens ?? 0); + const pct = Math.round((total / 262144) * 100); + log.info(` context: ${promptTokens} prompt + ${evalTokens ?? 0} eval = ${total} tokens (${pct}% of limit)`); + } + const assistantMessage = response.message; messages.push(assistantMessage);