fix(web): show command output in work log (#4083)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: codex <[email protected]>
This commit is contained in:
4 files changed
+284
-5
No files matched your search
@@ -44,6 +44,59 @@ describe("projectActivityPayload agent-field survival", () => {
|
||||
expect(data.somethingClientNeverReads).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps a bounded Codex command output summary", () => {
|
||||
const projected = projectActivityPayload(
|
||||
activity({
|
||||
itemType: "command_execution",
|
||||
data: {
|
||||
item: {
|
||||
command: "/bin/zsh -lc 'printf hello'",
|
||||
aggregatedOutput: `hello from codex\n${"x".repeat(5000)}`,
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
const data = (projected.payload as Record<string, unknown>).data as Record<string, unknown>;
|
||||
expect(data.item).toEqual({
|
||||
command: "/bin/zsh -lc 'printf hello'",
|
||||
aggregatedOutput: "hello from codex",
|
||||
});
|
||||
expect(JSON.stringify(projected.payload).length).toBeLessThan(500);
|
||||
});
|
||||
|
||||
it("keeps bounded Claude and ACP command output summaries", () => {
|
||||
const claude = projectActivityPayload(
|
||||
activity({
|
||||
itemType: "command_execution",
|
||||
data: {
|
||||
command: "printf hello",
|
||||
rawOutput: { stdout: `hello from claude\n${"y".repeat(5000)}` },
|
||||
},
|
||||
}),
|
||||
);
|
||||
const acp = projectActivityPayload(
|
||||
activity({
|
||||
itemType: "command_execution",
|
||||
data: {
|
||||
command: "printf hello",
|
||||
content: [
|
||||
{
|
||||
type: "content",
|
||||
content: { type: "text", text: `hello from acp\n${"z".repeat(5000)}` },
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
const claudeData = (claude.payload as Record<string, unknown>).data as Record<string, unknown>;
|
||||
const acpData = (acp.payload as Record<string, unknown>).data as Record<string, unknown>;
|
||||
expect(claudeData.rawOutput).toEqual({ content: "hello from claude" });
|
||||
expect(acpData.rawOutput).toEqual({ content: "hello from acp" });
|
||||
expect(JSON.stringify(claude.payload).length).toBeLessThan(500);
|
||||
expect(JSON.stringify(acp.payload).length).toBeLessThan(500);
|
||||
});
|
||||
|
||||
it("slims Codex-shaped mcp_tool_call items to rendered fields plus a result summary", () => {
|
||||
const projected = projectActivityPayload(
|
||||
activity({
|
||||
|
||||
@@ -91,14 +91,35 @@ function projectCommandData(data: Record<string, unknown>): Record<string, unkno
|
||||
projectedItem.command = item.command;
|
||||
}
|
||||
|
||||
const aggregatedOutput = asTrimmedString(item.aggregatedOutput);
|
||||
if (aggregatedOutput) {
|
||||
const summary = summarizeToolTextOutput(aggregatedOutput);
|
||||
if (summary) {
|
||||
projectedItem.aggregatedOutput = summary;
|
||||
}
|
||||
}
|
||||
|
||||
const input = asRecord(item.input);
|
||||
if (input && "command" in input) {
|
||||
projectedItem.input = { command: input.command };
|
||||
}
|
||||
|
||||
const result = asRecord(item.result);
|
||||
if (result && "command" in result) {
|
||||
projectedItem.result = { command: result.command };
|
||||
if (result) {
|
||||
const projectedResult: Record<string, unknown> = {};
|
||||
if ("command" in result) {
|
||||
projectedResult.command = result.command;
|
||||
}
|
||||
const content = asTrimmedString(result.content);
|
||||
if (content) {
|
||||
const summary = summarizeToolTextOutput(content);
|
||||
if (summary) {
|
||||
projectedResult.content = summary;
|
||||
}
|
||||
}
|
||||
if (Object.keys(projectedResult).length > 0) {
|
||||
projectedItem.result = projectedResult;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.keys(projectedItem).length > 0 ? projectedItem : undefined;
|
||||
@@ -232,6 +253,12 @@ function projectMcpToolCallData(data: Record<string, unknown>): Record<string, u
|
||||
}
|
||||
|
||||
function projectRawOutput(value: unknown): Record<string, unknown> | undefined {
|
||||
const direct = asTrimmedString(value);
|
||||
if (direct) {
|
||||
const summary = summarizeToolTextOutput(direct);
|
||||
return summary ? { content: summary } : undefined;
|
||||
}
|
||||
|
||||
const rawOutput = asRecord(value);
|
||||
if (!rawOutput) {
|
||||
return undefined;
|
||||
@@ -256,9 +283,34 @@ function projectRawOutput(value: unknown): Record<string, unknown> | undefined {
|
||||
return summary ? { content: summary } : undefined;
|
||||
}
|
||||
|
||||
const stderr = asTrimmedString(rawOutput.stderr);
|
||||
if (stderr) {
|
||||
const summary = summarizeToolTextOutput(stderr);
|
||||
return summary ? { content: summary } : undefined;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function projectAcpContent(value: unknown): Record<string, unknown> | undefined {
|
||||
if (!Array.isArray(value)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const text = value
|
||||
.map((entryValue) => {
|
||||
const entry = asRecord(entryValue);
|
||||
const content = asRecord(entry?.content);
|
||||
return entry?.type === "content" && content?.type === "text"
|
||||
? asTrimmedString(content.text)
|
||||
: null;
|
||||
})
|
||||
.filter((entry): entry is string => entry !== null)
|
||||
.join("\n");
|
||||
const summary = summarizeToolTextOutput(text);
|
||||
return summary ? { content: summary } : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes activity payload fields that no current client reads while retaining
|
||||
* the full payload in persistence and the event store.
|
||||
@@ -305,7 +357,7 @@ export function projectActivityPayload(
|
||||
projectedData.kind = data.kind;
|
||||
}
|
||||
|
||||
const rawOutput = projectRawOutput(data.rawOutput);
|
||||
const rawOutput = projectRawOutput(data.rawOutput) ?? projectAcpContent(data.content);
|
||||
if (rawOutput) {
|
||||
projectedData.rawOutput = rawOutput;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import { EventId, TurnId, type OrchestrationThreadActivity } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { deriveWorkLogEntries } from "./session-logic";
|
||||
|
||||
function makeCommandActivity(
|
||||
id: string,
|
||||
payload: Record<string, unknown>,
|
||||
): OrchestrationThreadActivity {
|
||||
return {
|
||||
id: EventId.make(id),
|
||||
createdAt: "2026-07-17T10:00:00.000Z",
|
||||
kind: "tool.completed",
|
||||
summary: "Ran command",
|
||||
tone: "tool",
|
||||
payload,
|
||||
turnId: TurnId.make("turn-1"),
|
||||
};
|
||||
}
|
||||
|
||||
describe("deriveWorkLogEntries command output", () => {
|
||||
it("uses Codex aggregated output instead of repeating the command", () => {
|
||||
const [entry] = deriveWorkLogEntries([
|
||||
makeCommandActivity("codex-command", {
|
||||
itemType: "command_execution",
|
||||
title: "Ran command",
|
||||
detail: "/bin/zsh -lc \"printf 'hello\\n'\"",
|
||||
data: {
|
||||
item: {
|
||||
type: "commandExecution",
|
||||
command: "/bin/zsh -lc \"printf 'hello\\n'\"",
|
||||
commandActions: [{ command: "printf 'hello\\n'", type: "unknown" }],
|
||||
aggregatedOutput: "hello\n<exited with exit code 0>",
|
||||
status: "completed",
|
||||
},
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(entry).toMatchObject({
|
||||
command: "printf 'hello\\n'",
|
||||
rawCommand: "/bin/zsh -lc \"printf 'hello\\n'\"",
|
||||
detail: "hello",
|
||||
});
|
||||
});
|
||||
|
||||
it("uses a projected Claude output summary instead of repeating the command", () => {
|
||||
const [entry] = deriveWorkLogEntries([
|
||||
makeCommandActivity("claude-command", {
|
||||
itemType: "command_execution",
|
||||
title: "Ran command",
|
||||
detail: "printf hello",
|
||||
data: {
|
||||
kind: "execute",
|
||||
command: "printf hello",
|
||||
rawOutput: {
|
||||
content: "hello from claude",
|
||||
},
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(entry).toMatchObject({
|
||||
command: "printf hello",
|
||||
detail: "hello from claude",
|
||||
});
|
||||
});
|
||||
|
||||
it("drops duplicated command detail when the command has no output", () => {
|
||||
const [entry] = deriveWorkLogEntries([
|
||||
makeCommandActivity("empty-command", {
|
||||
itemType: "command_execution",
|
||||
title: "Ran command",
|
||||
detail: "true",
|
||||
data: {
|
||||
kind: "execute",
|
||||
command: "true",
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(entry?.command).toBe("true");
|
||||
expect(entry?.detail).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1382,6 +1382,70 @@ function summarizeToolRawOutput(payload: Record<string, unknown> | null): string
|
||||
return null;
|
||||
}
|
||||
|
||||
function extractAcpTextContent(value: unknown): string | null {
|
||||
if (!Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const chunks: string[] = [];
|
||||
for (const entryValue of value) {
|
||||
const entry = asRecord(entryValue);
|
||||
if (entry?.type !== "content") {
|
||||
continue;
|
||||
}
|
||||
const content = asRecord(entry.content);
|
||||
if (content?.type !== "text") {
|
||||
continue;
|
||||
}
|
||||
const text = asTrimmedString(content.text);
|
||||
if (text) {
|
||||
chunks.push(text);
|
||||
}
|
||||
}
|
||||
|
||||
return chunks.length > 0 ? chunks.join("\n") : null;
|
||||
}
|
||||
|
||||
function extractToolOutput(payload: Record<string, unknown> | null): string | null {
|
||||
const data = asRecord(payload?.data);
|
||||
const item = asRecord(data?.item);
|
||||
const itemResult = asRecord(item?.result);
|
||||
const rawOutput = asRecord(data?.rawOutput);
|
||||
|
||||
const outputStreams: string[] = [];
|
||||
const stdout = asTrimmedString(rawOutput?.stdout);
|
||||
const stderr = asTrimmedString(rawOutput?.stderr);
|
||||
if (stdout) {
|
||||
outputStreams.push(stdout);
|
||||
}
|
||||
if (stderr) {
|
||||
outputStreams.push(stderr);
|
||||
}
|
||||
|
||||
const candidates: unknown[] = [
|
||||
item?.aggregatedOutput,
|
||||
itemResult?.content,
|
||||
data?.rawOutput,
|
||||
rawOutput?.content,
|
||||
outputStreams.length > 0 ? outputStreams.join("\n") : null,
|
||||
rawOutput?.output,
|
||||
extractAcpTextContent(data?.content),
|
||||
];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const text = asTrimmedString(candidate);
|
||||
if (!text) {
|
||||
continue;
|
||||
}
|
||||
const output = stripTrailingExitCode(text).output;
|
||||
if (output) {
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function isCommandToolDetail(payload: Record<string, unknown> | null, heading: string): boolean {
|
||||
const data = asRecord(payload?.data);
|
||||
const kind = asTrimmedString(data?.kind)?.toLowerCase();
|
||||
@@ -1402,12 +1466,37 @@ function extractToolDetail(
|
||||
const detail = rawDetail ? stripTrailingExitCode(rawDetail).output : null;
|
||||
const normalizedHeading = normalizePreviewForComparison(heading);
|
||||
const normalizedDetail = normalizePreviewForComparison(detail);
|
||||
const commandTool = isCommandToolDetail(payload, heading);
|
||||
const commandPreview = commandTool
|
||||
? extractToolCommand(payload)
|
||||
: { command: null, rawCommand: null };
|
||||
const command = commandPreview.command;
|
||||
const normalizedCommand = normalizePreviewForComparison(command);
|
||||
const normalizedRawCommand = normalizePreviewForComparison(commandPreview.rawCommand);
|
||||
|
||||
if (detail && normalizedHeading !== normalizedDetail) {
|
||||
if (
|
||||
detail &&
|
||||
normalizedHeading !== normalizedDetail &&
|
||||
(!commandTool ||
|
||||
(normalizedCommand !== normalizedDetail && normalizedRawCommand !== normalizedDetail))
|
||||
) {
|
||||
return detail;
|
||||
}
|
||||
|
||||
if (isCommandToolDetail(payload, heading)) {
|
||||
if (commandTool) {
|
||||
if (!command) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const output = extractToolOutput(payload);
|
||||
const normalizedOutput = normalizePreviewForComparison(output);
|
||||
if (
|
||||
output &&
|
||||
normalizedOutput !== normalizedHeading &&
|
||||
normalizedOutput !== normalizedCommand
|
||||
) {
|
||||
return output;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user