Skip to content
Open
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
14 changes: 13 additions & 1 deletion src/CodexEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export class CodexEventHandler {
private planUpdateChain: Promise<void> = Promise.resolve();
private disposed = false;
private readonly seenReasoningDeltaItemIds = new Set<string>();
private readonly seenAgentMessageDeltaItemIds = new Set<string>();
private readonly terminalCommandIds = new Set<string>();
private readonly terminalCommandOutputIds = new Set<string>();
private readonly agentMessagePhases = new Map<string, string | null>();
Expand Down Expand Up @@ -623,6 +624,7 @@ export class CodexEventHandler {
}

private async createTextEvent(event: AgentMessageDeltaNotification): Promise<UpdateSessionEvent> {
this.seenAgentMessageDeltaItemIds.add(event.itemId);
const phase = this.agentMessagePhases.get(event.itemId) ?? null;
return createAgentTextMessageChunk(event.delta, event.itemId, createCodexMessagePhaseMeta(phase));
}
Expand Down Expand Up @@ -745,7 +747,17 @@ export class CodexEventHandler {
return this.subagents.legacyCollaborationStarted(event.item);
case "agentMessage":
this.rememberAgentMessagePhase(event.item);
return null;
if (this.seenAgentMessageDeltaItemIds.delete(event.item.id)) {
return null;
}
if (event.item.text.length === 0) {
return null;
}
return createAgentTextMessageChunk(
event.item.text,
event.item.id,
createCodexMessagePhaseMeta(event.item.phase),
);
case "contextCompaction":
return createContextCompactionStartUpdate(event.item);
case "subAgentActivity":
Expand Down
98 changes: 98 additions & 0 deletions src/__tests__/CodexACPAgent/agent-message-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,102 @@ describe("CodexEventHandler - agent message events", () => {
"data/agent-message-phases.json"
);
});

it("emits completed agent text when a provider omits message deltas", async () => {
const notifications: ServerNotification[] = [
{
method: "item/started",
params: {
threadId: sessionId,
turnId: "turn-1",
startedAtMs: 0,
item: {
type: "agentMessage",
id: "completed-only-message",
text: "Ark-compatible final answer.",
phase: "final_answer",
memoryCitation: null,
delivery: null,
questions: null,
},
},
},
{
method: "item/completed",
params: {
threadId: sessionId,
turnId: "turn-1",
completedAtMs: 1,
item: {
type: "agentMessage",
id: "completed-only-message",
text: "Ark-compatible final answer.",
phase: "final_answer",
memoryCitation: null,
delivery: null,
questions: null,
},
},
},
];

await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, notifications);

const dump = mockFixture.getAcpConnectionDump([]);
expect(dump).toContain("Ark-compatible final answer.");
expect(dump.match(/Ark-compatible final answer\./g)).toHaveLength(1);
});

it("does not duplicate completed text after streamed deltas", async () => {
const notifications: ServerNotification[] = [
{
method: "item/started",
params: {
threadId: sessionId,
turnId: "turn-1",
startedAtMs: 0,
item: {
type: "agentMessage",
id: "streamed-message",
text: "",
phase: null,
memoryCitation: null,
delivery: null,
questions: null,
},
},
},
{
method: "item/agentMessage/delta",
params: {
threadId: sessionId,
turnId: "turn-1",
itemId: "streamed-message",
delta: "Streamed once.",
},
},
{
method: "item/completed",
params: {
threadId: sessionId,
turnId: "turn-1",
completedAtMs: 1,
item: {
type: "agentMessage",
id: "streamed-message",
text: "Streamed once.",
phase: null,
memoryCitation: null,
delivery: null,
questions: null,
},
},
},
];

await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, notifications);

const dump = mockFixture.getAcpConnectionDump([]);
expect(dump.match(/Streamed once\./g)).toHaveLength(1);
});
});