Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] 2ea18296c3 test(server): drive background command start through Codex fake 2026-12-03 21:23:58 +00:00
paseo-bot[bot] a68f30aa50 fix(server): settle Codex background commands on their original row
A shell command can finish after its turn has ended. Keep the command’s original turn ID so its completion updates the running timeline row.
2026-12-03 20:16:13 +00:00
3 changed files with 84 additions and 11 deletions
@@ -32,6 +32,7 @@ import {
normalizeCodexOutputSchema,
toAgentUsage,
} from "./codex-app-server-agent.js";
import { projectTimelineRows } from "../timeline-projection.js";
import { AgentTimeline, type TimelineEntry } from "../timeline/index.js";
describe("mapCodexPlanUpdateToTodo", () => {
@@ -9292,6 +9293,55 @@ test("retains a disconnected Codex session while it reconnects", async () => {
}
});
test("projects a Codex shell completion after the reply onto its original row", async () => {
const appServer = createFakeCodexAppServer();
const session = new CodexAppServerAgentSession(
createConfig(),
null,
createTestLogger(),
async () => appServer.child,
);
const events: Extract<AgentStreamEvent, { type: "timeline" }>[] = [];
session.subscribe((event) => {
if (event.type === "timeline") events.push(event);
});
try {
await session.startTurn("Start the command and reply without waiting");
await appServer.waitForTurnStart();
const threadId = "thread-1";
const callId = "background-command";
const command = "sleep 40; echo DONE-AFTER-REPLY";
const started = waitForTimelineToolCall(session, callId);
appServer.startsLegacyCommand({ threadId, callId, command });
await expect(started).resolves.toMatchObject({ item: { status: "running" } });
appServer.says({ threadId, itemId: "reply", text: "Sleep started." });
const replied = waitForNextEvent(session, "turn_completed");
appServer.completeTurn();
await replied;
const finished = waitForTimelineToolCall(session, callId);
appServer.completesSilentLegacyCommand({ threadId, callId, command, cwd: "/workspace" });
await expect(finished).resolves.toMatchObject({ item: { status: "completed" } });
const rows = projectTimelineRows({
mode: "projected",
rows: events.map((event, index) => ({
seq: index + 1,
timestamp: "2026-01-01T00:00:00Z",
turnId: event.turnId,
item: event.item,
})),
});
expect(rows.filter(({ item }) => item.type === "tool_call")).toMatchObject([
{ item: { callId, status: "completed", detail: { exitCode: 0 } } },
]);
} finally {
await session.close();
}
});
test("publishes a materialized image before dispatching the following turn completion", async () => {
const appServer = createFakeCodexAppServer();
const session = new CodexAppServerAgentSession(
@@ -3899,6 +3899,8 @@ export class CodexAppServerAgentSession implements AgentSession {
>();
private nextTerminalInteractionOrdinal = 0;
private emittedTerminalInteractionKeys = new Set<string>();
// Shell calls can outlive the foreground turn that started them.
private readonly runningShellTurnIds = new Map<string, string>();
private emittedExecCommandStartedCallIds = new Set<string>();
private emittedExecCommandCompletedCallIds = new Set<string>();
private emittedItemStartedIds = new Set<string>();
@@ -4187,6 +4189,7 @@ export class CodexAppServerAgentSession implements AgentSession {
private handleUnexpectedTermination(error: Error): void {
this.connectionState = "disconnected";
this.background.stop();
this.runningShellTurnIds.clear();
const hasActiveRootTurn = this.activeForegroundTurnId !== null || this.currentTurnId !== null;
this.clearPendingPermissions({ preservePlanApprovals: !hasActiveRootTurn });
if (hasActiveRootTurn) {
@@ -5618,6 +5621,7 @@ export class CodexAppServerAgentSession implements AgentSession {
this.supportsInstructionSnapshots,
);
this.currentThreadId = threadId;
this.runningShellTurnIds.clear();
// Codex writes a forked thread's rollout when it forks it, even before any turn.
this.savedThreadId = threadId;
this.cachedRuntimeInfo = null;
@@ -5911,6 +5915,7 @@ export class CodexAppServerAgentSession implements AgentSession {
private async disposeClient(): Promise<void> {
this.background.stop();
this.runningShellTurnIds.clear();
const client = this.client;
this.connectionState = "disconnected";
this.currentTurnId = null;
@@ -6248,7 +6253,20 @@ export class CodexAppServerAgentSession implements AgentSession {
}
private notifySubscribers(event: AgentStreamEvent): void {
const turnId = getAgentStreamEventTurnId(event) ?? this.activeForegroundTurnId;
let turnId = getAgentStreamEventTurnId(event) ?? this.activeForegroundTurnId;
if (
event.type === "timeline" &&
event.item.type === "tool_call" &&
event.item.detail.type === "shell"
) {
const { callId, status } = event.item;
turnId = this.runningShellTurnIds.get(callId) ?? turnId;
if (status === "running" && turnId) {
this.runningShellTurnIds.set(callId, turnId);
} else {
this.runningShellTurnIds.delete(callId);
}
}
const tagged = turnId ? { ...event, turnId } : event;
if (this.startupEvents) {
this.startupEvents.push(tagged);
@@ -17,16 +17,15 @@ interface FakeSubAgentActivity {
kind: "started" | "interacted" | "interrupted" | "completed";
parentThreadId?: string;
}
interface FakeLegacyCommand {
interface FakeCommand {
threadId: string;
callId: string;
command: string;
}
interface FakeLegacyCommand extends FakeCommand {
output: string;
}
interface FakeSilentCommand {
threadId: string;
callId: string;
command: string;
interface FakeSilentCommand extends FakeCommand {
cwd: string;
}
interface FakeTerminalCommand {
@@ -102,6 +101,7 @@ export interface FakeCodexAppServer {
beginsSubAgentActivity(params: FakeSubAgentActivity): void;
completesSubAgentActivity(params: FakeSubAgentActivity): void;
completesCompaction(params: { threadId: string; itemId: string }): void;
startsLegacyCommand(params: FakeCommand): void;
runsLegacyCommand(params: FakeLegacyCommand): void;
startsFileChange(params: FakeFileChange): void;
appliesLegacyPatch(params: FakeLegacyPatch): void;
@@ -371,6 +371,14 @@ export function createFakeCodexAppServer(
writeNotification(method, { threadId, msg });
}
function startsLegacyCommand(params: FakeCommand): void {
writeLegacyEvent(params.threadId, "codex/event/exec_command_begin", {
type: "exec_command_begin",
call_id: params.callId,
command: params.command,
});
}
return {
child,
recordedRollbacks,
@@ -485,12 +493,9 @@ export function createFakeCodexAppServer(
completesCompaction(params) {
completeItem(params.threadId, { type: "contextCompaction", id: params.itemId });
},
startsLegacyCommand,
runsLegacyCommand(params) {
writeLegacyEvent(params.threadId, "codex/event/exec_command_begin", {
type: "exec_command_begin",
call_id: params.callId,
command: params.command,
});
startsLegacyCommand(params);
writeLegacyEvent(params.threadId, "codex/event/exec_command_output_delta", {
type: "exec_command_output_delta",
call_id: params.callId,