File size: 7,150 Bytes
e249c6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | // Correlated CLI tool results already carry their started args; display-only
// results must not duplicate that potentially large payload.
import { describe, expect, it, vi } from "vitest";
import { type AgentEventRuntimePayload, onAgentEvent } from "../../infra/agent-events.js";
import { createTestAdmittedRunContext } from "../admitted-run-context.test-support.js";
import { createCliEventHandlers } from "./execute-events.js";
import type { CliToolTracking } from "./execute-tool-tracking.js";
import type { PreparedCliRunContext } from "./types.js";
function buildContext(runId: string): PreparedCliRunContext {
const backend = {
command: "claude",
args: [],
output: "jsonl" as const,
input: "stdin" as const,
serialize: true,
};
return {
params: {
admittedRunContext: createTestAdmittedRunContext(runId),
agentId: "main",
sessionId: "session-1",
sessionKey: "agent:main:main",
sessionFile: "/tmp/session.jsonl",
workspaceDir: "/tmp",
prompt: "hi",
provider: "claude-cli",
model: "claude-haiku-4-5",
timeoutMs: 1_000,
runId,
},
started: Date.now(),
workspaceDir: "/tmp",
backendResolved: { id: "claude-cli", config: backend, bundleMcp: false },
preparedBackend: { backend, env: {} },
executionTarget: { kind: "process" },
reusableCliSession: { mode: "none" },
hadSessionFile: false,
contextEngineConfig: {},
modelId: "claude-haiku-4-5",
normalizedModel: "claude-haiku-4-5",
systemPrompt: "system",
systemPromptReport: {} as PreparedCliRunContext["systemPromptReport"],
claudeSkillsPluginArgs: [],
authEpochVersion: 2,
} as PreparedCliRunContext;
}
function buildToolTracking(): CliToolTracking {
return {
handleCliToolUseStart: vi.fn(),
handleCliToolResult: vi.fn(),
resolveCliLoopbackTerminalOutcome: vi.fn(() => undefined),
beginGatewayCapture: vi.fn(),
} as unknown as CliToolTracking;
}
function collectToolEvents(runId: string): {
events: AgentEventRuntimePayload[];
dispose: () => void;
} {
const events: AgentEventRuntimePayload[] = [];
const dispose = onAgentEvent((event) => {
if (event.runId === runId && event.stream === "tool") {
events.push(event);
}
});
return { events, dispose };
}
describe("cli tool result events", () => {
it("emits complete CLI commentary as a completed preamble", () => {
const runId = "run-commentary-complete";
const handlers = createCliEventHandlers({
context: buildContext(runId),
toolTracking: buildToolTracking(),
getRunState: () => ({ failed: false, error: undefined }),
});
const events: AgentEventRuntimePayload[] = [];
const dispose = onAgentEvent((event) => {
if (event.runId === runId && event.stream === "item") {
events.push(event);
}
});
try {
// The JSONL parser has already accumulated this whole pre-tool segment.
// An update-only event would leave first-notification buffering waiting forever.
handlers.emitCliCommentaryText("Let me check that for you.");
expect(events).toMatchObject([
{
stream: "item",
data: { kind: "preamble", phase: "end", progressText: "Let me check that for you." },
},
]);
} finally {
dispose();
}
});
it("emits canonical CLI compaction lifecycle events", () => {
const runId = "run-compaction-events";
const handlers = createCliEventHandlers({
context: buildContext(runId),
toolTracking: buildToolTracking(),
getRunState: () => ({ failed: false, error: undefined }),
});
const events: AgentEventRuntimePayload[] = [];
const dispose = onAgentEvent((event) => {
if (event.runId === runId && event.stream === "compaction") {
events.push(event);
}
});
try {
handlers.emitCliCompaction({ phase: "start" });
handlers.emitCliCompaction({ phase: "end", completed: true });
expect(events.map((event) => event.data)).toEqual([
{ phase: "start", backend: "claude-cli" },
{ phase: "end", backend: "claude-cli", completed: true },
]);
} finally {
dispose();
}
});
it("keeps correlated result args without adding them to display results", () => {
const runId = "run-tool-result-args";
const handlers = createCliEventHandlers({
context: buildContext(runId),
toolTracking: buildToolTracking(),
getRunState: () => ({ failed: false, error: undefined }),
});
const { events, dispose } = collectToolEvents(runId);
try {
handlers.emitCliToolUseStart({
toolCallId: "call-1",
name: "Bash",
kind: "tool_use",
args: { command: "nope-not-a-command" },
});
handlers.emitCliToolResult({
toolCallId: "call-1",
name: "Bash",
isError: true,
result: "bash: nope-not-a-command: command not found",
});
handlers.emitCliDisplayToolUseStart({
toolCallId: "call-2",
name: "write",
kind: "tool_use",
args: { path: "note.txt", content: "hello" },
});
handlers.emitCliDisplayToolResult({
toolCallId: "call-2",
name: "write",
isError: false,
result: "wrote note.txt",
});
// The display result also releases correlation state for this call id.
handlers.emitCliToolResult({
toolCallId: "call-2",
name: "write",
isError: false,
result: "duplicate terminal",
});
const results = events.filter((event) => event.data.phase === "result");
expect(results[0]?.data.args).toEqual({ command: "nope-not-a-command" });
expect(results[0]?.data.isError).toBe(true);
expect(results[1]?.data.args).toBeUndefined();
expect(results[1]?.data.isError).toBe(false);
expect(results[2]?.data.args).toBeUndefined();
} finally {
dispose();
}
});
it("forgets a call's args once it reports, so ids cannot leak across calls", () => {
const runId = "run-tool-result-args-forget";
const handlers = createCliEventHandlers({
context: buildContext(runId),
toolTracking: buildToolTracking(),
getRunState: () => ({ failed: false, error: undefined }),
});
const { events, dispose } = collectToolEvents(runId);
try {
handlers.emitCliToolUseStart({
toolCallId: "call-1",
name: "Bash",
kind: "tool_use",
args: { command: "first" },
});
handlers.emitCliToolResult({
toolCallId: "call-1",
name: "Bash",
isError: false,
result: "",
});
// A second result for the same id must not reuse the first call's request.
handlers.emitCliToolResult({
toolCallId: "call-1",
name: "Bash",
isError: false,
result: "",
});
const results = events.filter((event) => event.data.phase === "result");
expect(results[0]?.data.args).toEqual({ command: "first" });
expect(results[1]?.data.args).toBeUndefined();
} finally {
dispose();
}
});
});
|