xonecas.io
5875
Os

ollama-space-molt-player / commit 3414585

fix: handle empty LLM responses and improve agent loop visibility

- Add "Round starting" logging with message/tool counts for debugging
- Fix stop() to log before closing database (prevents crash)
- Detect and handle empty LLM responses that caused infinite loops
- Add nudge mechanism: inject user prompt when LLM returns no action
- Add comprehensive game-client.test.ts covering:
  - Agent loop continues after text-only responses
  - Round start logging for visibility
  - LLM timeout handling and recovery
  - Empty response detection and nudging

The root cause was that empty LLM responses (no content, no tool calls)
silently broke the conversation - no message was added, so subsequent
rounds had identical context, causing the LLM to repeat empty responses.

xonecas2026-02-03 3414585589384258695a02732e8704a59c34ca88 parent b60bedb

2 files changed

packages/client/src/game-client.ts+20−1
@@ -83,9 +83,10 @@ export class GameClient {
8383
8484 async stop(): Promise<void> {
8585 this.running = false;
86+ // Log before closing DB since getLogContext() queries it
87+ logClientEvent(this.getLogContext(), "Client stopping");
8688 await this.mcp.disconnect();
8789 this.db.close();
88- logClientEvent(this.getLogContext(), "Client stopped");
8990 }
9091
9192 private async initializeWithAccount(account: ResolvedAccount): Promise<void> {
@@ -203,6 +204,13 @@ You decide your goals: mining, trading, combat, exploration, faction politics -
203204 while (iterations < MAX_TOOL_ITERATIONS) {
204205 iterations++;
205206
207+ if (iterations === 1) {
208+ logClientEvent(this.getLogContext(), "Round starting", {
209+ messages: this.messages.length,
210+ tools: this.tools.length,
211+ });
212+ }
213+
206214 const result = await this.ollama.chat(this.messages, this.tools);
207215
208216 if (result.thinking) {
@@ -224,6 +232,17 @@ You decide your goals: mining, trading, combat, exploration, faction politics -
224232 logClientEvent(this.getLogContext(), "LLM response", { content: result.content });
225233 this.db.saveMessage(Date.now(), "client", { response: result.content });
226234 this.messages.push({ role: "assistant", content: result.content });
235+ } else {
236+ // Empty response with no tool calls - LLM is stuck
237+ logClientEvent(this.getLogContext(), "LLM returned empty response, nudging");
238+ this.messages.push({
239+ role: "user",
240+ content:
241+ "You didn't take any action. Use your available tools to interact with the game. " +
242+ "Try get_status() to see your current state, or get_notifications() to check for messages.",
243+ });
244+ // Don't break - continue the loop to give LLM another chance with the nudge
245+ continue;
227246 }
228247 break;
229248 }
packages/client/tests/game-client.test.ts+463−0
@@ -0,0 +1,463 @@
1+import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
2+import type { Server } from "bun";
3+import { GameClient } from "../src/game-client.ts";
4+import { GameDatabase } from "../src/db.ts";
5+import { unlink, exists } from "node:fs/promises";
6+
7+/**
8+ * Tests for GameClient agent loop behavior.
9+ *
10+ * These tests verify that:
11+ * 1. The agent loop continues after text-only LLM responses
12+ * 2. Timeouts are properly handled and logged
13+ * 3. Multiple rounds execute correctly
14+ */
15+
16+describe("GameClient agent loop", () => {
17+ let ollamaServer: Server<unknown> | null = null;
18+ let mcpServer: Server<unknown> | null = null;
19+ let db: GameDatabase | null = null;
20+ const testInstanceId = "test-agent-loop";
21+ const dbPath = `data/${testInstanceId}.db`;
22+
23+ beforeEach(async () => {
24+ // Clean up any existing test database
25+ if (await exists(dbPath)) {
26+ await unlink(dbPath);
27+ }
28+ });
29+
30+ afterEach(async () => {
31+ ollamaServer?.stop();
32+ ollamaServer = null;
33+ mcpServer?.stop();
34+ mcpServer = null;
35+ db?.close();
36+ db = null;
37+
38+ // Clean up test database
39+ if (await exists(dbPath)) {
40+ await unlink(dbPath);
41+ }
42+ });
43+
44+ test("agent loop should continue after text-only LLM response", async () => {
45+ const llmResponses: Array<{ content: string; tool_calls?: unknown[] }> = [];
46+ let llmCallCount = 0;
47+ const maxRounds = 3;
48+
49+ // Mock Ollama server that tracks calls and returns text-only after first tool call
50+ ollamaServer = Bun.serve({
51+ port: 0,
52+ fetch: async (req) => {
53+ llmCallCount++;
54+
55+ // First call: return a register tool call
56+ if (llmCallCount === 1) {
57+ const response = {
58+ model: "test",
59+ created_at: new Date().toISOString(),
60+ message: {
61+ role: "assistant",
62+ content: "",
63+ tool_calls: [{
64+ function: {
65+ name: "register",
66+ arguments: { username: "TestBot", empire: "solarian" }
67+ }
68+ }]
69+ },
70+ done: true,
71+ };
72+ llmResponses.push(response.message);
73+ return Response.json(response);
74+ }
75+
76+ // Subsequent calls: return text-only responses
77+ const response = {
78+ model: "test",
79+ created_at: new Date().toISOString(),
80+ message: {
81+ role: "assistant",
82+ content: `Round ${llmCallCount} response - continuing play`,
83+ },
84+ done: true,
85+ };
86+ llmResponses.push(response.message);
87+ return Response.json(response);
88+ },
89+ });
90+
91+ // Mock MCP server
92+ mcpServer = Bun.serve({
93+ port: 0,
94+ fetch: async (req) => {
95+ const body = await req.json();
96+
97+ // Handle MCP initialize
98+ if (body.method === "initialize") {
99+ return Response.json({
100+ jsonrpc: "2.0",
101+ id: body.id,
102+ result: {
103+ protocolVersion: "2024-11-05",
104+ capabilities: { tools: {} },
105+ serverInfo: { name: "test-mcp", version: "1.0.0" }
106+ }
107+ });
108+ }
109+
110+ // Handle tools/list
111+ if (body.method === "tools/list") {
112+ return Response.json({
113+ jsonrpc: "2.0",
114+ id: body.id,
115+ result: {
116+ tools: [{
117+ name: "register",
118+ description: "Register a new player",
119+ inputSchema: {
120+ type: "object",
121+ properties: {
122+ username: { type: "string" },
123+ empire: { type: "string" }
124+ },
125+ required: ["username", "empire"]
126+ }
127+ }]
128+ }
129+ });
130+ }
131+
132+ // Handle tool calls - return error for register
133+ if (body.method === "tools/call") {
134+ return Response.json({
135+ jsonrpc: "2.0",
136+ id: body.id,
137+ result: {
138+ content: [{ type: "text", text: JSON.stringify({ error: "username_taken" }) }],
139+ isError: true
140+ }
141+ });
142+ }
143+
144+ return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
145+ },
146+ });
147+
148+ db = new GameDatabase(testInstanceId);
149+ const client = new GameClient(db, {
150+ instanceId: testInstanceId,
151+ model: "qwen3-8b",
152+ verbose: false,
153+ tickRate: 20000,
154+ ollamaTimeout: 5000,
155+ serverUrl: `http://localhost:${mcpServer.port}`,
156+ });
157+
158+ // Override the Ollama client's base URL
159+ // @ts-expect-error - accessing private property for testing
160+ client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
161+
162+ // Start client and let it run for a short time
163+ const startPromise = client.start();
164+
165+ // Wait for multiple rounds to complete
166+ await new Promise(resolve => setTimeout(resolve, 3500));
167+
168+ // Stop the client
169+ await client.stop();
170+
171+ // CRITICAL ASSERTION: The LLM should have been called multiple times
172+ // If the loop hangs after the first text-only response, this will fail
173+ expect(llmCallCount).toBeGreaterThanOrEqual(maxRounds);
174+
175+ // Verify we got text-only responses after the first tool call
176+ const textOnlyResponses = llmResponses.filter(r => !r.tool_calls || r.tool_calls.length === 0);
177+ expect(textOnlyResponses.length).toBeGreaterThanOrEqual(2);
178+ });
179+
180+ test("agent loop should log round start for visibility", async () => {
181+ const logs: string[] = [];
182+ const originalLog = console.log;
183+ console.log = (...args: unknown[]) => {
184+ logs.push(args.join(" "));
185+ originalLog(...args);
186+ };
187+
188+ let llmCallCount = 0;
189+
190+ ollamaServer = Bun.serve({
191+ port: 0,
192+ fetch: async () => {
193+ llmCallCount++;
194+ return Response.json({
195+ model: "test",
196+ created_at: new Date().toISOString(),
197+ message: {
198+ role: "assistant",
199+ content: `Response ${llmCallCount}`,
200+ },
201+ done: true,
202+ });
203+ },
204+ });
205+
206+ mcpServer = Bun.serve({
207+ port: 0,
208+ fetch: async (req) => {
209+ const body = await req.json();
210+ if (body.method === "initialize") {
211+ return Response.json({
212+ jsonrpc: "2.0",
213+ id: body.id,
214+ result: {
215+ protocolVersion: "2024-11-05",
216+ capabilities: { tools: {} },
217+ serverInfo: { name: "test-mcp", version: "1.0.0" }
218+ }
219+ });
220+ }
221+ if (body.method === "tools/list") {
222+ return Response.json({
223+ jsonrpc: "2.0",
224+ id: body.id,
225+ result: { tools: [] }
226+ });
227+ }
228+ return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
229+ },
230+ });
231+
232+ db = new GameDatabase(testInstanceId);
233+ const client = new GameClient(db, {
234+ instanceId: testInstanceId,
235+ model: "qwen3-8b",
236+ verbose: false,
237+ tickRate: 20000,
238+ ollamaTimeout: 5000,
239+ serverUrl: `http://localhost:${mcpServer.port}`,
240+ });
241+
242+ // @ts-expect-error - accessing private property for testing
243+ client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
244+
245+ const startPromise = client.start();
246+ await new Promise(resolve => setTimeout(resolve, 2500));
247+ await client.stop();
248+
249+ console.log = originalLog;
250+
251+ // Should see multiple LLM calls even when all responses are text-only
252+ expect(llmCallCount).toBeGreaterThanOrEqual(2);
253+
254+ // Should see "Starting agent loop" log
255+ expect(logs.some(l => l.includes("Starting agent loop"))).toBe(true);
256+ });
257+
258+ test("agent loop should handle LLM timeout gracefully", { timeout: 15000 }, async () => {
259+ const errors: string[] = [];
260+ const originalError = console.error;
261+ console.error = (...args: unknown[]) => {
262+ errors.push(args.join(" "));
263+ originalError(...args);
264+ };
265+
266+ let requestCount = 0;
267+
268+ // Mock Ollama that takes longer than timeout
269+ ollamaServer = Bun.serve({
270+ port: 0,
271+ fetch: async () => {
272+ requestCount++;
273+ // First request times out, subsequent requests succeed
274+ if (requestCount === 1) {
275+ await new Promise(resolve => setTimeout(resolve, 2000));
276+ }
277+ return Response.json({
278+ model: "test",
279+ created_at: new Date().toISOString(),
280+ message: { role: "assistant", content: "response" },
281+ done: true,
282+ });
283+ },
284+ });
285+
286+ mcpServer = Bun.serve({
287+ port: 0,
288+ fetch: async (req) => {
289+ const body = await req.json();
290+ if (body.method === "initialize") {
291+ return Response.json({
292+ jsonrpc: "2.0",
293+ id: body.id,
294+ result: {
295+ protocolVersion: "2024-11-05",
296+ capabilities: { tools: {} },
297+ serverInfo: { name: "test-mcp", version: "1.0.0" }
298+ }
299+ });
300+ }
301+ if (body.method === "tools/list") {
302+ return Response.json({
303+ jsonrpc: "2.0",
304+ id: body.id,
305+ result: { tools: [] }
306+ });
307+ }
308+ return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
309+ },
310+ });
311+
312+ db = new GameDatabase(testInstanceId);
313+ const client = new GameClient(db, {
314+ instanceId: testInstanceId,
315+ model: "qwen3-8b",
316+ verbose: false,
317+ tickRate: 20000,
318+ ollamaTimeout: 500, // Very short timeout
319+ serverUrl: `http://localhost:${mcpServer.port}`,
320+ });
321+
322+ // @ts-expect-error - accessing private property for testing
323+ client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
324+
325+ const startPromise = client.start();
326+ // Wait long enough for: timeout (500ms) + error delay (5000ms) + next round
327+ await new Promise(resolve => setTimeout(resolve, 7000));
328+ await client.stop();
329+
330+ console.error = originalError;
331+
332+ // Should have logged an error about timeout
333+ expect(errors.some(e => e.includes("Agent loop error") || e.includes("timed out"))).toBe(true);
334+
335+ // Should have retried after the timeout error
336+ expect(requestCount).toBeGreaterThan(1);
337+ });
338+
339+ test("agent loop should nudge LLM when it returns empty response", async () => {
340+ const logs: string[] = [];
341+ const originalLog = console.log;
342+ console.log = (...args: unknown[]) => {
343+ logs.push(args.join(" "));
344+ originalLog(...args);
345+ };
346+
347+ let llmCallCount = 0;
348+ let lastRequestMessages: unknown[] = [];
349+
350+ ollamaServer = Bun.serve({
351+ port: 0,
352+ fetch: async (req) => {
353+ llmCallCount++;
354+ const body = await req.json();
355+ lastRequestMessages = body.messages || [];
356+
357+ // First 2 calls return empty response (simulating stuck LLM)
358+ // Third call returns actual content
359+ if (llmCallCount <= 2) {
360+ return Response.json({
361+ model: "test",
362+ created_at: new Date().toISOString(),
363+ message: {
364+ role: "assistant",
365+ content: "", // Empty response
366+ },
367+ done: true,
368+ });
369+ }
370+
371+ return Response.json({
372+ model: "test",
373+ created_at: new Date().toISOString(),
374+ message: {
375+ role: "assistant",
376+ content: "Now taking action!",
377+ tool_calls: [{
378+ function: {
379+ name: "get_status",
380+ arguments: {}
381+ }
382+ }]
383+ },
384+ done: true,
385+ });
386+ },
387+ });
388+
389+ mcpServer = Bun.serve({
390+ port: 0,
391+ fetch: async (req) => {
392+ const body = await req.json();
393+ if (body.method === "initialize") {
394+ return Response.json({
395+ jsonrpc: "2.0",
396+ id: body.id,
397+ result: {
398+ protocolVersion: "2024-11-05",
399+ capabilities: { tools: {} },
400+ serverInfo: { name: "test-mcp", version: "1.0.0" }
401+ }
402+ });
403+ }
404+ if (body.method === "tools/list") {
405+ return Response.json({
406+ jsonrpc: "2.0",
407+ id: body.id,
408+ result: {
409+ tools: [{
410+ name: "get_status",
411+ description: "Get player status",
412+ inputSchema: { type: "object", properties: {}, required: [] }
413+ }]
414+ }
415+ });
416+ }
417+ if (body.method === "tools/call") {
418+ return Response.json({
419+ jsonrpc: "2.0",
420+ id: body.id,
421+ result: {
422+ content: [{ type: "text", text: JSON.stringify({ status: "ok" }) }]
423+ }
424+ });
425+ }
426+ return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
427+ },
428+ });
429+
430+ db = new GameDatabase(testInstanceId);
431+ const client = new GameClient(db, {
432+ instanceId: testInstanceId,
433+ model: "qwen3-8b",
434+ verbose: false,
435+ tickRate: 20000,
436+ ollamaTimeout: 5000,
437+ serverUrl: `http://localhost:${mcpServer.port}`,
438+ });
439+
440+ // @ts-expect-error - accessing private property for testing
441+ client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
442+
443+ const startPromise = client.start();
444+ await new Promise(resolve => setTimeout(resolve, 2500));
445+ await client.stop();
446+
447+ console.log = originalLog;
448+
449+ // Should have called LLM at least 3 times (2 empty + 1 with content)
450+ expect(llmCallCount).toBeGreaterThanOrEqual(3);
451+
452+ // Should have logged about empty response
453+ expect(logs.some(l => l.includes("empty response") || l.includes("nudging"))).toBe(true);
454+
455+ // The nudge message should have been added to the conversation
456+ const hasNudgeMessage = lastRequestMessages.some((msg: any) =>
457+ msg.role === "user" &&
458+ typeof msg.content === "string" &&
459+ msg.content.includes("didn't take any action")
460+ );
461+ expect(hasNudgeMessage).toBe(true);
462+ });
463+});