fix: move CLI transcript writing to correct location

The original fix was in agent-runner-execution.ts, but CLI subagents
spawned via sessions_spawn go through commands/agent.ts instead.

Moved the transcript writing logic to run after runWithModelFallback
completes in agentCommand, which is the actual code path for CLI
subagent runs.

Tested and verified working on live server.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ryan McMillan 2026-01-28 15:48:26 -06:00
parent b61aa94c28
commit 0fbe3ac29e
2 changed files with 41 additions and 45 deletions

View File

@ -1,6 +1,5 @@
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { resolveAgentModelFallbacksOverride } from "../../agents/agent-scope.js";
import { runCliAgent } from "../../agents/cli-runner.js";
import { getCliSessionId } from "../../agents/cli-session.js";
@ -193,50 +192,6 @@ export async function runAgentTurnWithFallback(params: {
data: { text: cliText },
});
}
// Write CLI result to transcript for subagent announce flow
if (cliText && params.sessionKey && params.followupRun.run.sessionId) {
try {
const agentId = resolveAgentIdFromSessionKey(params.sessionKey);
const transcriptPath = resolveSessionTranscriptPath(
params.followupRun.run.sessionId,
agentId,
);
// Ensure directory exists
fs.mkdirSync(path.dirname(transcriptPath), { recursive: true });
// Create session header if file doesn't exist
if (!fs.existsSync(transcriptPath)) {
const header = {
type: "session",
version: 1,
id: params.followupRun.run.sessionId,
timestamp: new Date().toISOString(),
cwd: process.cwd(),
};
fs.writeFileSync(transcriptPath, JSON.stringify(header) + "\n", "utf-8");
}
// Append assistant message
const messageEntry = {
type: "message",
id: crypto.randomUUID().slice(0, 8),
timestamp: new Date().toISOString(),
message: {
role: "assistant",
content: [{ type: "text", text: cliText }],
timestamp: Date.now(),
stopReason: "cli",
usage: result.meta?.agentMeta?.usage ?? {
input: 0,
output: 0,
totalTokens: 0,
},
},
};
fs.appendFileSync(transcriptPath, JSON.stringify(messageEntry) + "\n", "utf-8");
} catch (err) {
// Best effort - don't fail the run if transcript write fails
logVerbose(`CLI transcript write failed: ${String(err)}`);
}
}
emitAgentEvent({
runId,
stream: "lifecycle",

View File

@ -1,3 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import {
listAgentIds,
resolveAgentDir,
@ -40,6 +43,7 @@ import {
type SessionEntry,
updateSessionStore,
} from "../config/sessions.js";
import { resolveSessionTranscriptPath } from "../config/sessions/paths.js";
import {
clearAgentRunContext,
emitAgentEvent,
@ -460,6 +464,43 @@ export async function agentCommand(
result = fallbackResult.result;
fallbackProvider = fallbackResult.provider;
fallbackModel = fallbackResult.model;
// Write CLI transcript for subagent announce flow
// CLI backends don't normally write transcripts, but subagent-announce
// needs them to read the findings when the run completes.
if (isCliProvider(fallbackProvider, cfg) && result.payloads?.[0]?.text) {
try {
const agentId = resolveAgentIdFromSessionKey(sessionKey);
const transcriptPath = resolveSessionTranscriptPath(sessionId, agentId);
fs.mkdirSync(path.dirname(transcriptPath), { recursive: true });
if (!fs.existsSync(transcriptPath)) {
const header = {
type: "session",
version: 1,
id: sessionId,
timestamp: new Date().toISOString(),
cwd: process.cwd(),
};
fs.writeFileSync(transcriptPath, JSON.stringify(header) + "\n", "utf-8");
}
const messageEntry = {
type: "message",
id: crypto.randomUUID().slice(0, 8),
timestamp: new Date().toISOString(),
message: {
role: "assistant",
content: [{ type: "text", text: result.payloads[0].text }],
timestamp: Date.now(),
stopReason: "cli",
usage: result.meta?.usage ?? { input: 0, output: 0, totalTokens: 0 },
},
};
fs.appendFileSync(transcriptPath, JSON.stringify(messageEntry) + "\n", "utf-8");
} catch {
// Transcript write failure is non-fatal - log silently
}
}
if (!lifecycleEnded) {
emitAgentEvent({
runId,