Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions packages/host-service/src/terminal/harness-transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,89 @@ afterEach(() => {
});

describe("readHarnessTranscript", () => {
test("preserves a consumed busy-session follow-up in conversation order", () => {
// Claude Code 2.1.263: a prompt entered during a tool call is first
// queued, then recorded as queued_command when the turn consumes it.
const prompt = "Keep the existing API.\nAdd a regression for empty input.";
const { worktreePath, sessionId } = seedClaudeSession([
JSON.stringify({ type: "user", message: { content: "Fix the parser." } }),
JSON.stringify({
type: "queue-operation",
operation: "enqueue",
content: prompt,
}),
JSON.stringify({
type: "queue-operation",
operation: "remove",
reason: "absorbed_mid_turn",
content: prompt,
}),
JSON.stringify({
type: "attachment",
attachment: {
type: "queued_command",
commandMode: "prompt",
prompt,
source_uuid: "follow-up",
origin: { kind: "human" },
},
}),
JSON.stringify({
type: "assistant",
message: { content: "Kept the API and added the regression." },
}),
]);

expect(
readHarnessTranscript({
agentId: "claude",
agentSessionId: sessionId,
worktreePath,
})?.text,
).toBe(
`User: Fix the parser.\n\nUser: ${prompt}\n\nAssistant: Kept the API and added the regression.`,
);
});

test("excludes unconsumed queue operations and unrelated attachments", () => {
const { worktreePath, sessionId } = seedClaudeSession([
JSON.stringify({
type: "user",
message: { content: "Keep this instruction." },
}),
JSON.stringify({
type: "queue-operation",
operation: "enqueue",
content: "Pending instruction",
}),
JSON.stringify({
type: "queue-operation",
operation: "remove",
content: "Removed instruction",
}),
...[
undefined,
{ type: "other", commandMode: "prompt", prompt: "Unrelated" },
{ type: "queued_command", commandMode: "bash", prompt: "echo shell" },
{ type: "queued_command", commandMode: "prompt", prompt: " " },
{ type: "queued_command", commandMode: "prompt", prompt: null },
{
type: "queued_command",
commandMode: "prompt",
prompt: { text: "Malformed" },
},
].map((attachment) => JSON.stringify({ type: "attachment", attachment })),
]);

expect(
readHarnessTranscript({
agentId: "claude",
agentSessionId: sessionId,
worktreePath,
})?.text,
).toBe("User: Keep this instruction.");
});

test("reads the conversation out of Claude's own store", () => {
const { worktreePath, sessionId } = seedClaudeSession([
JSON.stringify({ type: "mode", mode: "normal" }),
Expand Down
19 changes: 19 additions & 0 deletions packages/host-service/src/terminal/harness-transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export function readFileTail(path: string, maxBytes: number): string | null {

interface ClaudeEvent {
type?: string;
attachment?: {
type?: string;
commandMode?: string;
prompt?: unknown;
};
message?: {
role?: string;
content?: string | Array<{ type?: string; text?: string }>;
Expand Down Expand Up @@ -133,6 +138,20 @@ function readClaudeTranscript(
} catch {
continue; // a partially written final line while the session runs
}
// Claude records consumed busy-session prompts as attachments. Queue
// operations alone can describe input the user later removes.
if (event.type === "attachment") {
const attachment = event.attachment;
if (
attachment?.type === "queued_command" &&
attachment.commandMode === "prompt" &&
typeof attachment.prompt === "string" &&
attachment.prompt.trim()
) {
turns.push(`User: ${attachment.prompt.trim()}`);
}
continue;
}
if (event.type !== "user" && event.type !== "assistant") continue;
const text = textOf(event);
if (!text) continue;
Expand Down
Loading