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:
parent
b61aa94c28
commit
0fbe3ac29e
@ -1,6 +1,5 @@
|
|||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import path from "node:path";
|
|
||||||
import { resolveAgentModelFallbacksOverride } from "../../agents/agent-scope.js";
|
import { resolveAgentModelFallbacksOverride } from "../../agents/agent-scope.js";
|
||||||
import { runCliAgent } from "../../agents/cli-runner.js";
|
import { runCliAgent } from "../../agents/cli-runner.js";
|
||||||
import { getCliSessionId } from "../../agents/cli-session.js";
|
import { getCliSessionId } from "../../agents/cli-session.js";
|
||||||
@ -193,50 +192,6 @@ export async function runAgentTurnWithFallback(params: {
|
|||||||
data: { text: cliText },
|
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({
|
emitAgentEvent({
|
||||||
runId,
|
runId,
|
||||||
stream: "lifecycle",
|
stream: "lifecycle",
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
import crypto from "node:crypto";
|
||||||
import {
|
import {
|
||||||
listAgentIds,
|
listAgentIds,
|
||||||
resolveAgentDir,
|
resolveAgentDir,
|
||||||
@ -40,6 +43,7 @@ import {
|
|||||||
type SessionEntry,
|
type SessionEntry,
|
||||||
updateSessionStore,
|
updateSessionStore,
|
||||||
} from "../config/sessions.js";
|
} from "../config/sessions.js";
|
||||||
|
import { resolveSessionTranscriptPath } from "../config/sessions/paths.js";
|
||||||
import {
|
import {
|
||||||
clearAgentRunContext,
|
clearAgentRunContext,
|
||||||
emitAgentEvent,
|
emitAgentEvent,
|
||||||
@ -460,6 +464,43 @@ export async function agentCommand(
|
|||||||
result = fallbackResult.result;
|
result = fallbackResult.result;
|
||||||
fallbackProvider = fallbackResult.provider;
|
fallbackProvider = fallbackResult.provider;
|
||||||
fallbackModel = fallbackResult.model;
|
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) {
|
if (!lifecycleEnded) {
|
||||||
emitAgentEvent({
|
emitAgentEvent({
|
||||||
runId,
|
runId,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user