Fix log crash

This commit is contained in:
Colin McDonnell
2025-12-17 12:49:29 -08:00
parent 4826e9acb1
commit 2c92e27b4d
3 changed files with 46 additions and 13 deletions
+21 -5
View File
@@ -75,6 +75,7 @@ export const opencode = agent({
let eventCount = 0;
let output = "";
let stdoutBuffer = ""; // buffer for incomplete lines across chunks
const result = await spawn({
cmd: cliPath,
args,
@@ -83,13 +84,16 @@ export const opencode = agent({
timeout: 600000, // 10 minutes timeout to prevent infinite hangs
stdio: ["ignore", "pipe", "pipe"],
onStdout: async (chunk) => {
const parsed = JSON.parse(chunk);
log.debug(JSON.stringify(parsed, null, 2));
const text = chunk.toString();
output += text;
// parse each line as JSON (opencode outputs one JSON object per line)
const lines = text.split("\n");
// buffer incomplete lines across chunks (NDJSON format)
stdoutBuffer += text;
const lines = stdoutBuffer.split("\n");
// keep the last element (may be incomplete) in the buffer
stdoutBuffer = lines.pop() || "";
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) {
@@ -99,6 +103,12 @@ export const opencode = agent({
try {
const event = JSON.parse(trimmed) as OpenCodeEvent;
eventCount++;
// debug log all events to diagnose ordering and missing MCP/bash tool calls
log.debug(
`» event: type=${event.type} data=${JSON.stringify(event).substring(0, 300)}`
);
const timeSinceLastActivity = Date.now() - lastActivityTime;
if (timeSinceLastActivity > 10000) {
const activeToolCalls = toolCallTimings.size;
@@ -121,7 +131,8 @@ export const opencode = agent({
);
}
} catch {
// non-JSON lines are ignored
// non-JSON lines are ignored (might be debug output from opencode)
log.debug(`» non-JSON stdout line: ${trimmed.substring(0, 200)}`);
}
}
},
@@ -473,6 +484,11 @@ const messageHandlers = {
const status = event.part?.state?.status;
const output = event.part?.state?.output;
// debug log all tool_use events to diagnose missing bash/MCP tool calls
if (!toolName || !toolId) {
log.debug(`» tool_use event missing toolName or toolId: ${JSON.stringify(event)}`);
}
if (toolName && toolId) {
// track tool call in current step
if (stepHistory.length > 0) {