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
12 changes: 11 additions & 1 deletion src/CodexAcpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ import {
} from "./FastModeConfig";
import packageJson from "../package.json";
import {isJetBrains2026_1Client} from "./JBUtils";
import {resolveTerminalOutputMode, type TerminalOutputMode} from "./TerminalOutputMode";
import {
clientSupportsTerminalOutputDelta,
resolveTerminalOutputMode,
type TerminalOutputMode,
} from "./TerminalOutputMode";
import {clientSupportsPlanUpdates} from "./PlanCapabilities";
import {
createAgentTextMessageChunk,
Expand Down Expand Up @@ -181,6 +185,7 @@ export interface SessionState {
currentModelSupportsFast: boolean;
sessionMcpServers?: Array<string>;
terminalOutputMode: TerminalOutputMode;
terminalOutputDeltaSupported: boolean;
currentGoal?: ThreadGoalSnapshot | null;
goalRevision: number;
sessionTitle: string | null;
Expand Down Expand Up @@ -269,6 +274,7 @@ export class CodexAcpServer {
private clientInfo: acp.Implementation | null;
private clientCapabilities: acp.ClientCapabilities | null;
private terminalOutputMode: TerminalOutputMode;
private terminalOutputDeltaSupported: boolean;
private booleanConfigOptionsSupported: boolean;
/** Last `authStatus` pushed to the client; used to suppress duplicates. */
private currentAuthStatus: AuthStatus | null;
Expand Down Expand Up @@ -317,6 +323,7 @@ export class CodexAcpServer {
this.clientInfo = null;
this.clientCapabilities = null;
this.terminalOutputMode = "terminal_output_delta";
this.terminalOutputDeltaSupported = false;
this.booleanConfigOptionsSupported = false;
this.currentAuthStatus = null;
this.availableCommands = this.createAvailableCommands(codexAcpClient);
Expand All @@ -340,6 +347,7 @@ export class CodexAcpServer {
this.clientCapabilities = _params.clientCapabilities ?? null;
this.initializeRequest = _params;
this.terminalOutputMode = resolveTerminalOutputMode(_params.clientCapabilities);
this.terminalOutputDeltaSupported = clientSupportsTerminalOutputDelta(_params.clientCapabilities);
this.booleanConfigOptionsSupported = clientSupportsBooleanConfigOptions(_params.clientCapabilities);
await this.runWithProcessCheck(() => this.codexAcpClient.initialize(_params));
this.publishFirstAuthStatusAfterResponse();
Expand Down Expand Up @@ -688,6 +696,7 @@ export class CodexAcpServer {
currentModelSupportsFast: currentModelSupportsFast,
sessionMcpServers: sessionMcpServers,
terminalOutputMode: this.terminalOutputMode,
terminalOutputDeltaSupported: this.terminalOutputDeltaSupported,
goalRevision: 0,
sessionTitle: null,
sessionTitleSource: operation === "resume" ? "unknown" : "unset",
Expand Down Expand Up @@ -1946,6 +1955,7 @@ export class CodexAcpServer {
currentModelSupportsFast: currentModelSupportsFast,
sessionMcpServers: sessionMcpServers,
terminalOutputMode: this.terminalOutputMode,
terminalOutputDeltaSupported: this.terminalOutputDeltaSupported,
goalRevision: 0,
sessionTitle: null,
sessionTitleSource: "unset",
Expand Down
52 changes: 28 additions & 24 deletions src/CodexEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class CodexEventHandler {
private disposed = false;
private readonly seenReasoningDeltaItemIds = new Set<string>();
private readonly terminalCommandIds = new Set<string>();
private readonly terminalCommandOutputIds = new Set<string>();
private readonly commandOutputIds = new Set<string>();
private readonly agentMessagePhases = new Map<string, string | null>();
private readonly turnDiffs = new Map<string, string>();
private readonly oversizedTurnDiffs = new Set<string>();
Expand Down Expand Up @@ -807,7 +807,7 @@ export class CodexEventHandler {
this.terminalCommandIds.add(event.item.id);
} else {
this.terminalCommandIds.delete(event.item.id);
this.terminalCommandOutputIds.delete(event.item.id);
this.commandOutputIds.delete(event.item.id);
}
return await createCommandExecutionUpdate(event.item);
}
Expand Down Expand Up @@ -1022,8 +1022,8 @@ export class CodexEventHandler {
}

private createCommandOutputDeltaEvent(event: CommandExecutionOutputDeltaNotification): UpdateSessionEvent {
if (this.terminalCommandIds.has(event.itemId) && event.delta.length > 0) {
this.terminalCommandOutputIds.add(event.itemId);
if (event.delta.length > 0) {
this.commandOutputIds.add(event.itemId);
}
return this.createCommandOutputEvent(event.itemId, event.delta, this.commandOutputMode(event.itemId));
}
Expand All @@ -1041,12 +1041,11 @@ export class CodexEventHandler {
}

private createTerminalInteractionEvent(event: TerminalInteractionNotification): UpdateSessionEvent {
return this.createCommandOutputDeltaEvent({
threadId: event.threadId,
turnId: event.turnId,
itemId: event.itemId,
delta: `\n${event.stdin}\n`,
});
return this.createCommandOutputEvent(
event.itemId,
`\n${event.stdin}\n`,
this.commandOutputMode(event.itemId),
);
}

private commandOutputMode(itemId: string): TerminalOutputMode {
Expand Down Expand Up @@ -1110,29 +1109,34 @@ export class CodexEventHandler {
toolCallId: item.id,
...(name === undefined ? {} : {name}),
status: item.status === "completed" ? "completed" : "failed",
rawOutput: {
formatted_output: item.aggregatedOutput ?? "",
exit_code: item.exitCode
},
...(this.sessionState.terminalOutputDeltaSupported ? {} : {
rawOutput: {
formatted_output: item.aggregatedOutput ?? "",
exit_code: item.exitCode
},
}),
};

const commandHadTerminal = this.terminalCommandIds.delete(item.id);
const commandHadOutput = this.terminalCommandOutputIds.delete(item.id);
if (!commandHadTerminal) {
return update;
}
const commandHadOutput = this.commandOutputIds.delete(item.id);
const terminalMeta: Record<string, unknown> = {};
if (!commandHadOutput && item.aggregatedOutput) {
if (!commandHadOutput && item.aggregatedOutput &&
(commandHadTerminal || this.sessionState.terminalOutputDeltaSupported)) {
Object.assign(
terminalMeta,
createTerminalOutputMeta(this.sessionState.terminalOutputMode, item.id, item.aggregatedOutput)
);
}
terminalMeta["terminal_exit"] = {
exit_code: item.exitCode,
signal: null,
terminal_id: item.id
};
if (commandHadTerminal) {
terminalMeta["terminal_exit"] = {
exit_code: item.exitCode,
signal: null,
terminal_id: item.id
};
}
if (Object.keys(terminalMeta).length === 0) {
return update;
}
return {
...update,
_meta: terminalMeta,
Expand Down
9 changes: 9 additions & 0 deletions src/TerminalOutputMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ export function resolveTerminalOutputMode(
clientCapabilities?: acp.ClientCapabilities | null
): TerminalOutputMode {
const meta = clientCapabilities?._meta;
if (meta?.["terminal_output_delta"] === true) {
return "terminal_output_delta";
}
if (meta?.["terminal_output"] === true) {
return "terminal_output";
}
return "terminal_output_delta";
}

export function clientSupportsTerminalOutputDelta(
clientCapabilities?: acp.ClientCapabilities | null
): boolean {
return clientCapabilities?._meta?.["terminal_output_delta"] === true;
}

export function createTerminalOutputMeta(
mode: TerminalOutputMode,
terminalId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"sessionUpdate": "tool_call_update",
"toolCallId": "command-123",
"status": "completed",
"rawOutput": {
"formatted_output": "file1.txt\nfile2.txt\nfile3.txt\n",
"exit_code": 0
"_meta": {
"terminal_output_delta": {
"data": "file1.txt\nfile2.txt\nfile3.txt\n",
"terminal_id": "command-123"
}
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/__tests__/CodexACPAgent/data/terminal-full-flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@
"sessionUpdate": "tool_call_update",
"toolCallId": "command-flow",
"status": "completed",
"rawOutput": {
"formatted_output": "hello\n",
"exit_code": 0
},
"_meta": {
"terminal_exit": {
"exit_code": 0,
Expand Down
65 changes: 62 additions & 3 deletions src/__tests__/CodexACPAgent/terminal-output-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ describe('CodexEventHandler - terminal output events', () => {
);
});

it('should send formatted output on command completion', async () => {
it('should send one delta when command completion has no streamed output', async () => {
const deltaSessionState = createTestSessionState({
sessionId,
terminalOutputDeltaSupported: true,
});
const commandCompletedNotification: ServerNotification = {
method: 'item/completed',
params: {
Expand All @@ -154,13 +158,64 @@ describe('CodexEventHandler - terminal output events', () => {
},
};

await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [commandCompletedNotification]);
await setupPromptAndSendNotifications(mockFixture, sessionId, deltaSessionState, [commandCompletedNotification]);

await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
'data/terminal-command-completed.json'
);
});

it('should flush aggregated output after terminal input without streamed output', async () => {
const deltaSessionState = createTestSessionState({
sessionId,
terminalOutputDeltaSupported: true,
});
const terminalInteractionNotification: ServerNotification = {
method: 'item/commandExecution/terminalInteraction',
params: {
threadId: sessionId,
turnId: 'turn-1',
itemId: 'command-123',
processId: 'pid-456',
stdin: 'continue',
},
};
const commandCompletedNotification: ServerNotification = {
method: 'item/completed',
params: {
threadId: sessionId,
turnId: 'turn-1',
completedAtMs: 0,
item: {
type: 'commandExecution',
id: 'command-123',
pluginId: null,
scriptPath: null,
command: 'read answer; echo done',
cwd: '/test/project',
processId: 'pid-456',
source: 'agent',
status: 'completed',
commandActions: [],
aggregatedOutput: 'done\n',
exitCode: 0,
durationMs: 150,
},
},
};

await setupPromptAndSendNotifications(mockFixture, sessionId, deltaSessionState, [
terminalInteractionNotification,
commandCompletedNotification,
]);

const updates = mockFixture.getAcpConnectionEvents([]).map(event => event.args[0].update);
expect(updates).toHaveLength(2);
expect(updates[0]._meta?.terminal_output_delta?.data).toBe('\ncontinue\n');
expect(updates[1]._meta?.terminal_output_delta?.data).toBe('done\n');
expect(updates[1]).not.toHaveProperty('rawOutput');
});

it('should handle failed command completion', async () => {
const commandFailedNotification: ServerNotification = {
method: 'item/completed',
Expand Down Expand Up @@ -222,6 +277,10 @@ describe('CodexEventHandler - terminal output events', () => {
});

it('should handle full terminal output flow: start -> delta -> complete', async () => {
const deltaSessionState = createTestSessionState({
sessionId,
terminalOutputDeltaSupported: true,
});
const commandStartNotification: ServerNotification = {
method: 'item/started',
params: {
Expand Down Expand Up @@ -280,7 +339,7 @@ describe('CodexEventHandler - terminal output events', () => {
},
};

await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [
await setupPromptAndSendNotifications(mockFixture, sessionId, deltaSessionState, [
commandStartNotification,
outputDeltaNotification,
commandCompletedNotification
Expand Down
12 changes: 10 additions & 2 deletions src/__tests__/TerminalOutputMode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { describe, expect, it } from "vitest";
import { resolveTerminalOutputMode } from "../TerminalOutputMode";

describe("resolveTerminalOutputMode", () => {
it("uses terminal_output when advertised", () => {
it("prefers terminal_output_delta when both modes are advertised", () => {
expect(resolveTerminalOutputMode({
_meta: {
terminal_output: true,
terminal_output_delta: true,
},
})).toBe("terminal_output");
})).toBe("terminal_output_delta");
});

it("uses legacy terminal_output_delta when only it is advertised", () => {
Expand All @@ -19,6 +19,14 @@ describe("resolveTerminalOutputMode", () => {
})).toBe("terminal_output_delta");
});

it("uses terminal_output when it is the only advertised mode", () => {
expect(resolveTerminalOutputMode({
_meta: {
terminal_output: true,
},
})).toBe("terminal_output");
});

it("keeps legacy terminal_output_delta when capabilities are absent", () => {
expect(resolveTerminalOutputMode(null)).toBe("terminal_output_delta");
expect(resolveTerminalOutputMode({})).toBe("terminal_output_delta");
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/acp-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ export function createTestSessionState(overrides?: Partial<SessionState>): Sessi
fastModeEnabled: false,
currentModelSupportsFast: false,
terminalOutputMode: "terminal_output_delta",
terminalOutputDeltaSupported: false,
goalRevision: 0,
sessionTitle: null,
sessionTitleSource: "unknown",
Expand Down
Loading