Files
hapi/cli/src/codex/codexRemoteLauncher.test.ts
T
3c3bffdfbd feat: message-level conversation fork and rewind (#1263)
* feat: add message-level conversation fork and rewind

Expose native Codex/Grok/Claude history controls through hub REST+RPC and web ConfirmDialog actions, without file rewind or composed forks. Also reconcile the duplicate hub V14→V15 migration so typecheck can pass.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: hydrate fork transcript and consume Claude --fork-session

Forked HAPI children now copy the source transcript prefix so navigation is not a blank thread, and Claude drops --fork-session after the first launch so relaunches do not branch again.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: harden fork/rewind concurrency and durable history points

Skip pending scheduled rows when hydrating fork transcripts, serialize fork/rewind per session, and persist conversation history points/indexes across existing-session bootstrap and Grok relaunches.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: close remaining fork/rewind races and UI anchoring

Block sends and scheduled maturation while history actions run, order fork prefixes by invocation time, inherit history locators into children, and only offer Fork current on the live tail boundary.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: address remaining fork/rewind bot findings

Materialize Claude --fork-session before the first child prompt, validate
HAPI history boundaries before native RPC, expose forkCurrent on a latest
user boundary, fully demote unsupported conversationHistory capabilities,
and fix the truncate test setup order.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: align fork-current ids and Claude fork bootstrap

Compare the latest fork boundary in assistant-ui threadMessageId space,
spawn Claude forks with the persisted session mode, and preserve
forkedFrom across existing-session bootstrap.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: close fork/rewind consistency holes at the contract layer

Hold the source history lock until Claude child binds a distinct native
id, persist Codex localId→turnId locators, and mark/block diverged
sessions when native rewind outruns HAPI truncate.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: use Codex stable lastTurnId for historical fork

Map HAPI's exclusive boundary to the previous turn's inclusive
lastTurnId so native fork context matches the hydrated transcript.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: require exact Grok native resume for fork children

Reject newSession fallback when forkedFrom is set, and keep the hub
history lock until the child binds the forked grokSessionId.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: kill active fork children before failed-fork cleanup

Bind/readiness failures can leave the child process running; deleteSession
rejects active rows, so terminate first then remove the HAPI session.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: close remaining fork lock, hydrate, and todos gaps

Reject mode switches during history actions, batch-copy fork
transcripts in one SQLite transaction, and rebuild todos after
fork hydrate / rewind truncate.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: allow Codex historical fork before the first turn

Use experimental beforeTurnId when there is no previous turn for the
stable inclusive lastTurnId boundary.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: mark Grok history busy immediately after dequeue

Hub idle checks clear once messages-consumed fires; hold the busy flag
across permission sync and rewind-points lookup before prompt starts.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: encode copied conversation history content

* fix(web): hide local conversation history actions

* style(codex): remove trailing whitespace

* fix(fork): preserve children when cleanup is unconfirmed

* fix(history): confirm cleanup and guard rewind divergence

* fix(history): probe capabilities before advertising

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-03 09:28:01 +08:00

2980 lines
123 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { MessageQueue2 } from '@/utils/MessageQueue2';
import type { EnhancedMode } from './loop';
const harness = vi.hoisted(() => ({
notifications: [] as Array<{ method: string; params: unknown }>,
dispatchNotification: null as ((method: string, params: unknown) => void) | null,
registerRequestCalls: [] as string[],
requestHandlers: new Map<string, (params: unknown) => Promise<unknown> | unknown>(),
initializeCalls: [] as unknown[],
setFeatureEnablementCalls: [] as unknown[],
failSetFeatureEnablement: false,
listCollaborationModeCalls: 0,
collaborationModeResponse: { data: [{ mode: 'default' }, { mode: 'plan' }] } as unknown,
failListCollaborationModes: false,
listSkillsCalls: [] as unknown[],
skillsListResponse: {
data: [{
cwd: '/tmp/hapi-update',
skills: [{
name: 'hapi',
description: 'Manage HAPI',
path: '/home/user/.agents/skills/hapi/SKILL.md',
scope: 'user',
enabled: true
}],
errors: []
}]
} as unknown,
startThreadIds: [] as string[],
startThreadParams: [] as Array<Record<string, unknown>>,
resumeThreadIds: [] as string[],
resumeThreadParams: [] as Array<Record<string, unknown>>,
startTurnThreadIds: [] as string[],
startTurnParams: [] as Array<Record<string, unknown>>,
startTurnErrors: [] as Error[],
interruptedTurns: [] as Array<{ threadId: string; turnId: string }>,
interruptErrors: [] as Error[],
rollbackCalls: [] as Array<{ threadId: string; numTurns: number }>,
rollbackErrors: [] as Error[],
compactThreadIds: [] as string[],
goalSetCalls: [] as unknown[],
goalGetCalls: [] as unknown[],
goalClearCalls: [] as unknown[],
goal: null as Record<string, unknown> | null,
suppressGoalNotifications: false,
suppressTurnCompletion: false,
remainingThreadSystemErrors: 0,
emitFailedCompletionAfterThreadSystemError: false,
emitCyberPolicyAfterThreadSystemError: false,
emitSafetyBuffering: false,
safetyBufferingFasterModel: null as string | null,
emitModelSafetyNotices: false,
startTurnMessages: [] as string[],
failResumeThreadIds: [] as string[],
nextThreadSystemErrorMessage: null as string | null,
failNextCompact: false,
deferCompactCompletion: false,
deferThreadStatusNotifications: false,
emitChildThreadEvents: false,
emitChildUsageEvents: false,
emitChildGoalEvent: false,
emitChildReasoningBurst: false,
emitChildDoneStatusWithoutMessage: false,
emitChildWaitStructuredOutput: false,
emitChildTaskCompleteBeforeMessage: false,
suppressChildTaskCompleteEvent: false,
emitSecondChildMessage: false,
emitLateChildCommandAfterParentTool: false,
emitParentUsageEvents: false,
emitParentGoalDuplicateEvents: false,
emitChildNestedAgentTool: false,
emitParentTitleChange: false,
emitParentSpawnFailureWithoutAgentId: false,
emitParentSpawnStartWithoutEnd: false,
emitParentSpawnRouterStderrError: false,
emitChildTaskStartedAfterParentSpawnStart: false,
emitSecondParentSpawnStartWithoutEnd: false,
emitParentSendInputFailure: false,
emitParentResumeSuccess: false,
emitParentV2AgentTools: false,
emitRunningChildTurnBeforeSuppressedParent: false,
emitCompletedChildTurnBeforeSuppressedParent: false,
emitTurnAbortedOnInterrupt: false,
bridgeOptions: [] as unknown[]
}));
vi.mock('./codexAppServerClient', () => {
class MockCodexAppServerClient {
private notificationHandler: ((method: string, params: unknown) => void) | null = null;
private stderrHandler: ((text: string) => void) | null = null;
async connect(): Promise<void> {}
async initialize(params: unknown): Promise<{ protocolVersion: number }> {
harness.initializeCalls.push(params);
return { protocolVersion: 1 };
}
setNotificationHandler(handler: ((method: string, params: unknown) => void) | null): void {
this.notificationHandler = handler;
harness.dispatchNotification = handler;
}
setStderrHandler(handler: ((text: string) => void) | null): void {
this.stderrHandler = handler;
}
async listCollaborationModes(): Promise<unknown> {
harness.listCollaborationModeCalls += 1;
if (harness.failListCollaborationModes) {
throw new Error('collaborationMode/list failed');
}
return harness.collaborationModeResponse;
}
async listSkills(params: unknown): Promise<unknown> {
harness.listSkillsCalls.push(params);
return harness.skillsListResponse;
}
async setExperimentalFeatureEnablement(params: unknown): Promise<unknown> {
harness.setFeatureEnablementCalls.push(params);
if (harness.failSetFeatureEnablement) {
throw new Error('unsupported feature enablement');
}
return params;
}
registerRequestHandler(method: string, handler: (params: unknown) => Promise<unknown> | unknown): void {
harness.registerRequestCalls.push(method);
harness.requestHandlers.set(method, handler);
}
async startThread(params?: Record<string, unknown>): Promise<{ thread: { id: string }; model: string }> {
const id = `thread-${harness.startThreadIds.length + 1}`;
harness.startThreadIds.push(id);
harness.startThreadParams.push(params ?? {});
return { thread: { id }, model: 'gpt-5.4' };
}
async resumeThread(params?: Record<string, unknown>): Promise<{ thread: { id: string }; model: string }> {
const id = typeof params?.threadId === 'string' ? params.threadId : 'thread-resumed';
harness.resumeThreadIds.push(id);
harness.resumeThreadParams.push(params ?? {});
if (harness.failResumeThreadIds.includes(id)) {
throw new Error('resume failed');
}
return { thread: { id }, model: 'gpt-5.4' };
}
async compactThread(params?: { threadId?: string }): Promise<Record<string, never>> {
const threadId = params?.threadId ?? 'thread-unknown';
harness.compactThreadIds.push(threadId);
if (harness.failNextCompact) {
harness.failNextCompact = false;
throw new Error('compact failed');
}
if (harness.deferCompactCompletion) {
return {};
}
const compacted = { threadId, turnId: `compact-${harness.compactThreadIds.length}` };
harness.notifications.push({ method: 'thread/compacted', params: compacted });
this.notificationHandler?.('thread/compacted', compacted);
return {};
}
async setThreadGoal(params?: { threadId?: string; objective?: string; status?: string }): Promise<{ goal: Record<string, unknown> }> {
harness.goalSetCalls.push(params ?? {});
const threadId = params?.threadId ?? 'thread-unknown';
harness.goal = {
threadId,
objective: params?.objective ?? harness.goal?.objective ?? 'existing goal',
status: params?.status ?? 'active',
tokenBudget: null,
tokensUsed: 0,
timeUsedSeconds: 0,
createdAt: 1,
updatedAt: 2
};
const notification = { threadId, goal: harness.goal };
if (!harness.suppressGoalNotifications) {
harness.notifications.push({ method: 'thread/goal/updated', params: notification });
this.notificationHandler?.('thread/goal/updated', notification);
}
return { goal: harness.goal };
}
async getThreadGoal(params?: { threadId?: string }): Promise<{ goal: Record<string, unknown> | null }> {
harness.goalGetCalls.push(params ?? {});
return { goal: harness.goal };
}
async clearThreadGoal(params?: { threadId?: string }): Promise<{ cleared: boolean }> {
harness.goalClearCalls.push(params ?? {});
const cleared = harness.goal !== null;
harness.goal = null;
if (cleared) {
const notification = { threadId: params?.threadId ?? 'thread-unknown' };
if (!harness.suppressGoalNotifications) {
harness.notifications.push({ method: 'thread/goal/cleared', params: notification });
this.notificationHandler?.('thread/goal/cleared', notification);
}
}
return { cleared };
}
async startTurn(params?: { threadId?: string; input?: Array<{ text?: string }>; message?: string; userMessage?: string }): Promise<{ turn: { id?: string } }> {
harness.startTurnParams.push((params ?? {}) as Record<string, unknown>);
const nextError = harness.startTurnErrors.shift();
if (nextError) {
throw nextError;
}
const threadId = params?.threadId ?? 'thread-unknown';
harness.startTurnThreadIds.push(threadId);
harness.startTurnMessages.push(params?.input?.[0]?.text ?? params?.message ?? params?.userMessage ?? '');
const turnId = `turn-${harness.startTurnThreadIds.length}`;
const started = { turn: { id: turnId } };
harness.notifications.push({ method: 'turn/started', params: started });
this.notificationHandler?.('turn/started', started);
if (harness.remainingThreadSystemErrors > 0) {
harness.remainingThreadSystemErrors -= 1;
const failed = {
thread: { id: threadId },
status: { type: 'systemError', ...(harness.nextThreadSystemErrorMessage ? { message: harness.nextThreadSystemErrorMessage } : {}) }
};
harness.notifications.push({ method: 'thread/status/changed', params: failed });
const notify = () => this.notificationHandler?.('thread/status/changed', failed);
if (harness.deferThreadStatusNotifications) {
setTimeout(notify, 0);
} else {
notify();
}
if (harness.emitCyberPolicyAfterThreadSystemError) {
const policyError = {
threadId,
turnId,
error: {
message: 'This content was flagged for possible cybersecurity risk.',
codexErrorInfo: 'cyberPolicy'
},
willRetry: false
};
harness.notifications.push({ method: 'error', params: policyError });
this.notificationHandler?.('error', policyError);
const completed = {
threadId,
turnId,
turn: { id: turnId, status: 'failed' }
};
harness.notifications.push({ method: 'turn/completed', params: completed });
this.notificationHandler?.('turn/completed', completed);
} else if (harness.emitFailedCompletionAfterThreadSystemError) {
const completed = {
threadId,
turnId,
turn: { id: turnId, status: 'failed' }
};
harness.notifications.push({ method: 'turn/completed', params: completed });
this.notificationHandler?.('turn/completed', completed);
}
return { turn: { id: turnId } };
}
if (harness.emitSafetyBuffering) {
harness.emitSafetyBuffering = false;
const notification = {
threadId,
turnId,
model: 'gpt-5.4',
useCases: ['cyber'],
reasons: ['review'],
showBufferingUi: true,
fasterModel: harness.safetyBufferingFasterModel
};
harness.notifications.push({ method: 'model/safetyBuffering/updated', params: notification });
this.notificationHandler?.('model/safetyBuffering/updated', notification);
return { turn: { id: turnId } };
}
if (harness.emitModelSafetyNotices) {
harness.emitModelSafetyNotices = false;
const rerouted = {
threadId,
turnId,
fromModel: 'gpt-5.4',
toModel: 'gpt-5.4-codex',
reason: 'highRiskCyberActivity'
};
harness.notifications.push({ method: 'model/rerouted', params: rerouted });
this.notificationHandler?.('model/rerouted', rerouted);
const verification = {
threadId,
turnId,
verifications: ['trustedAccessForCyber']
};
harness.notifications.push({ method: 'model/verification', params: verification });
this.notificationHandler?.('model/verification', verification);
}
if (
harness.emitRunningChildTurnBeforeSuppressedParent
|| harness.emitCompletedChildTurnBeforeSuppressedParent
) {
const childStarted = {
msg: {
type: 'task_started',
thread_id: 'child-thread',
turn_id: 'child-turn'
}
};
harness.notifications.push({ method: 'codex/event/task_started', params: childStarted });
this.notificationHandler?.('codex/event/task_started', childStarted);
if (harness.emitCompletedChildTurnBeforeSuppressedParent) {
const childCompleted = {
msg: {
type: 'task_complete',
thread_id: 'child-thread',
turn_id: 'child-turn'
}
};
harness.notifications.push({ method: 'codex/event/task_complete', params: childCompleted });
this.notificationHandler?.('codex/event/task_complete', childCompleted);
}
}
if (harness.suppressTurnCompletion) {
return { turn: { id: turnId } };
}
if (params?.threadId === 'thread-1') {
if (harness.emitParentGoalDuplicateEvents) {
const goalBase = {
threadId,
objective: 'keep benchmark work moving',
status: 'active',
tokenBudget: null,
tokensUsed: 0,
timeUsedSeconds: 0,
createdAt: 1
};
for (let index = 0; index < 4; index += 1) {
const notification = {
threadId,
goal: {
...goalBase,
timeUsedSeconds: index,
updatedAt: 2 + index
}
};
harness.notifications.push({ method: 'thread/goal/updated', params: notification });
this.notificationHandler?.('thread/goal/updated', notification);
}
const pausedNotification = {
threadId,
goal: {
...goalBase,
status: 'paused',
timeUsedSeconds: 4,
updatedAt: 6
}
};
harness.notifications.push({ method: 'thread/goal/updated', params: pausedNotification });
this.notificationHandler?.('thread/goal/updated', pausedNotification);
}
if (harness.emitParentTitleChange) {
const titleStart = {
item: {
id: 'title-parent',
type: 'mcpToolCall',
server: 'hapi',
tool: 'change_title',
arguments: { title: 'Parent Title' }
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/started', params: titleStart });
this.notificationHandler?.('item/started', titleStart);
const titleEnd = {
item: {
id: 'title-parent',
type: 'mcpToolCall',
server: 'hapi',
tool: 'change_title',
result: {
content: [
{ type: 'text', text: 'Successfully changed chat title to: "Parent Title"' }
]
}
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/completed', params: titleEnd });
this.notificationHandler?.('item/completed', titleEnd);
}
const commandStart = {
item: {
id: 'cmd-1',
type: 'commandExecution',
command: 'echo ok',
cwd: '/tmp/hapi-update'
}
};
harness.notifications.push({ method: 'item/started', params: commandStart });
this.notificationHandler?.('item/started', commandStart);
this.notificationHandler?.('item/commandExecution/outputDelta', {
itemId: 'cmd-1',
delta: 'ok\n'
});
const commandEnd = {
item: {
id: 'cmd-1',
type: 'commandExecution',
exitCode: 0
}
};
harness.notifications.push({ method: 'item/completed', params: commandEnd });
this.notificationHandler?.('item/completed', commandEnd);
if (harness.emitParentUsageEvents) {
const parentUsage = {
tokenUsage: {
thread_id: threadId,
turn_id: turnId,
last_token_usage: {
input_tokens: 100,
output_tokens: 10
},
model_context_window: 200_000
}
};
harness.notifications.push({ method: 'thread/tokenUsage/updated', params: parentUsage });
this.notificationHandler?.('thread/tokenUsage/updated', parentUsage);
const parentCompact = { thread: { id: threadId } };
harness.notifications.push({ method: 'thread/compacted', params: parentCompact });
this.notificationHandler?.('thread/compacted', parentCompact);
}
if (harness.emitParentV2AgentTools) {
const v2Calls = [{
callId: 'v2-spawn',
name: 'spawn_agent',
input: { task_name: 'review', message: 'Review this' },
output: { task_name: '/root/review', nickname: 'Reviewer' }
}, {
callId: 'v2-wait',
name: 'wait_agent',
input: { timeout_ms: 10_000 },
output: { message: 'Wait timed out.', timed_out: true }
}];
for (const call of v2Calls) {
const started = {
threadId,
turnId,
item: {
type: 'function_call',
namespace: 'collaboration',
name: call.name,
arguments: JSON.stringify(call.input),
call_id: call.callId
}
};
harness.notifications.push({ method: 'rawResponseItem/completed', params: started });
this.notificationHandler?.('rawResponseItem/completed', started);
const completed = {
threadId,
turnId,
item: {
type: 'function_call_output',
call_id: call.callId,
output: JSON.stringify(call.output)
}
};
harness.notifications.push({ method: 'rawResponseItem/completed', params: completed });
this.notificationHandler?.('rawResponseItem/completed', completed);
}
}
if (harness.emitParentSpawnFailureWithoutAgentId || harness.emitParentSpawnStartWithoutEnd) {
const spawnStart = {
item: {
id: 'failed-spawn',
type: 'collabAgentToolCall',
tool: 'spawnAgent',
prompt: 'do side work',
reasoningEffort: 'medium',
senderThreadId: threadId,
receiverThreadIds: []
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/started', params: spawnStart });
this.notificationHandler?.('item/started', spawnStart);
if (harness.emitSecondParentSpawnStartWithoutEnd) {
const secondSpawnStart = {
item: {
id: 'second-spawn',
type: 'collabAgentToolCall',
tool: 'spawnAgent',
prompt: 'do other side work',
senderThreadId: threadId,
receiverThreadIds: []
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/started', params: secondSpawnStart });
this.notificationHandler?.('item/started', secondSpawnStart);
}
if (harness.emitParentSpawnRouterStderrError) {
this.stderrHandler?.(
'codex_core::tools::router: error=Full-history forked agents inherit the parent agent type, model, and reasoning effort; ' +
'omit agent_type, model, and reasoning_effort, or spawn without a full-history fork.'
);
}
if (harness.emitChildTaskStartedAfterParentSpawnStart) {
const childStarted = {
msg: {
type: 'task_started',
thread_id: 'child-thread',
turn_id: 'child-turn'
}
};
harness.notifications.push({ method: 'codex/event/task_started', params: childStarted });
this.notificationHandler?.('codex/event/task_started', childStarted);
}
if (harness.emitParentSpawnFailureWithoutAgentId) {
const spawnCompleted = {
item: {
id: 'failed-spawn',
type: 'collabAgentToolCall',
tool: 'spawnAgent',
status: 'failed',
error: 'invalid spawn arguments',
senderThreadId: threadId,
receiverThreadIds: [],
agentsStates: {}
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/completed', params: spawnCompleted });
this.notificationHandler?.('item/completed', spawnCompleted);
}
}
}
if (harness.emitChildThreadEvents) {
const childThreadId = 'child-thread';
const childTurnId = 'child-turn';
const childMessage = 'child output should stay hidden';
const secondChildMessage = 'final child output should win';
const emitChildDone = () => {
const childDone = {
msg: {
type: 'task_complete',
thread_id: childThreadId,
turn_id: childTurnId
}
};
harness.notifications.push({ method: 'codex/event/task_complete', params: childDone });
this.notificationHandler?.('codex/event/task_complete', childDone);
};
if (harness.emitChildReasoningBurst) {
for (let i = 0; i < 20; i += 1) {
const reasoningDelta = {
msg: {
type: 'reasoning_content_delta',
item_id: 'child-reasoning',
delta: `step-${i} `,
thread_id: childThreadId,
turn_id: childTurnId
}
};
harness.notifications.push({ method: 'codex/event/reasoning_content_delta', params: reasoningDelta });
this.notificationHandler?.('codex/event/reasoning_content_delta', reasoningDelta);
}
}
if (harness.emitChildDoneStatusWithoutMessage && harness.emitChildTaskCompleteBeforeMessage) {
emitChildDone();
}
const childMessageCompleted = {
item: {
id: 'child-msg-1',
type: 'agentMessage',
content: [{ type: 'text', text: childMessage }]
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/completed', params: childMessageCompleted });
this.notificationHandler?.('item/completed', childMessageCompleted);
if (harness.emitSecondChildMessage) {
const secondChildMessageCompleted = {
item: {
id: 'child-msg-2',
type: 'agentMessage',
content: [{ type: 'text', text: secondChildMessage }]
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/completed', params: secondChildMessageCompleted });
this.notificationHandler?.('item/completed', secondChildMessageCompleted);
}
if (
harness.emitChildDoneStatusWithoutMessage
&& !harness.emitChildTaskCompleteBeforeMessage
&& !harness.suppressChildTaskCompleteEvent
) {
emitChildDone();
}
if (harness.emitChildUsageEvents) {
const childUsage = {
tokenUsage: {
thread_id: childThreadId,
turn_id: childTurnId,
last_token_usage: {
input_tokens: 30,
output_tokens: 3
},
model_context_window: 200_000
}
};
harness.notifications.push({ method: 'thread/tokenUsage/updated', params: childUsage });
this.notificationHandler?.('thread/tokenUsage/updated', childUsage);
const childCompact = {
msg: {
type: 'context_compacted',
thread_id: childThreadId,
turn_id: childTurnId
}
};
harness.notifications.push({ method: 'codex/event/context_compacted', params: childCompact });
this.notificationHandler?.('codex/event/context_compacted', childCompact);
const ambiguousUsage = {
tokenUsage: {
last_token_usage: {
input_tokens: 999,
output_tokens: 1
}
}
};
harness.notifications.push({ method: 'thread/tokenUsage/updated', params: ambiguousUsage });
this.notificationHandler?.('thread/tokenUsage/updated', ambiguousUsage);
}
if (harness.emitChildGoalEvent) {
const childGoal = {
threadId: childThreadId,
goal: {
threadId: childThreadId,
objective: 'child-only goal',
status: 'active',
tokenBudget: null,
tokensUsed: 0,
timeUsedSeconds: 0,
createdAt: 1,
updatedAt: 2
}
};
harness.notifications.push({ method: 'thread/goal/updated', params: childGoal });
this.notificationHandler?.('thread/goal/updated', childGoal);
}
const childCommandStart = {
item: {
id: 'child-cmd-1',
type: 'commandExecution',
command: 'echo child'
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/started', params: childCommandStart });
this.notificationHandler?.('item/started', childCommandStart);
this.notificationHandler?.('item/commandExecution/outputDelta', {
itemId: 'child-cmd-1',
delta: 'child stdout\n',
threadId: childThreadId,
turnId: childTurnId
});
const childCommandEnd = {
item: {
id: 'child-cmd-1',
type: 'commandExecution',
exitCode: 0
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/completed', params: childCommandEnd });
this.notificationHandler?.('item/completed', childCommandEnd);
const childTitleStart = {
item: {
id: 'title-child',
type: 'mcpToolCall',
server: 'hapi',
tool: 'change_title',
arguments: { title: 'Child Title' }
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/started', params: childTitleStart });
this.notificationHandler?.('item/started', childTitleStart);
const childTitleEnd = {
item: {
id: 'title-child',
type: 'mcpToolCall',
server: 'hapi',
tool: 'change_title',
result: {
content: [
{ type: 'text', text: 'Successfully changed chat title to: "Child Title"' }
]
}
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/completed', params: childTitleEnd });
this.notificationHandler?.('item/completed', childTitleEnd);
if (harness.emitChildNestedAgentTool) {
const nestedSpawnStart = {
item: {
id: 'nested-spawn',
type: 'collabAgentToolCall',
tool: 'spawn',
senderThreadId: childThreadId,
receiverThreadIds: ['grandchild-thread'],
prompt: 'do nested work'
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/started', params: nestedSpawnStart });
this.notificationHandler?.('item/started', nestedSpawnStart);
const nestedSpawnCompleted = {
item: {
id: 'nested-spawn',
type: 'collabAgentToolCall',
tool: 'spawn',
status: 'completed',
senderThreadId: childThreadId,
receiverThreadIds: ['grandchild-thread'],
agentsStates: {}
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/completed', params: nestedSpawnCompleted });
this.notificationHandler?.('item/completed', nestedSpawnCompleted);
}
const waitStarted = {
item: {
id: 'wait-child',
type: 'collabAgentToolCall',
tool: 'wait',
senderThreadId: threadId,
receiverThreadIds: [childThreadId],
agentsStates: {}
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/started', params: waitStarted });
this.notificationHandler?.('item/started', waitStarted);
const waitCompleted = {
item: {
id: 'wait-child',
type: 'collabAgentToolCall',
tool: 'wait',
status: 'completed',
senderThreadId: threadId,
receiverThreadIds: [childThreadId],
agentsStates: {
[childThreadId]: {
status: harness.emitChildDoneStatusWithoutMessage ? 'done' : 'completed',
message: harness.emitChildWaitStructuredOutput
? ''
: harness.emitChildDoneStatusWithoutMessage
? null
: harness.emitSecondChildMessage
? secondChildMessage
: childMessage,
...(harness.emitChildWaitStructuredOutput ? { output: { value: 42 } } : {})
}
}
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/completed', params: waitCompleted });
this.notificationHandler?.('item/completed', waitCompleted);
if (harness.emitParentSendInputFailure) {
const sendInputStarted = {
item: {
id: 'send-child',
type: 'collabAgentToolCall',
tool: 'sendInput',
senderThreadId: threadId,
receiverThreadIds: [childThreadId],
message: 'follow up'
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/started', params: sendInputStarted });
this.notificationHandler?.('item/started', sendInputStarted);
const sendInputCompleted = {
item: {
id: 'send-child',
type: 'collabAgentToolCall',
tool: 'sendInput',
status: 'failed',
error: 'send failed',
senderThreadId: threadId,
receiverThreadIds: [childThreadId],
agentsStates: {}
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/completed', params: sendInputCompleted });
this.notificationHandler?.('item/completed', sendInputCompleted);
}
if (harness.emitParentResumeSuccess) {
const resumeStarted = {
item: {
id: 'resume-child',
type: 'collabAgentToolCall',
tool: 'resumeAgent',
senderThreadId: threadId,
receiverThreadIds: [childThreadId]
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/started', params: resumeStarted });
this.notificationHandler?.('item/started', resumeStarted);
const resumeCompleted = {
item: {
id: 'resume-child',
type: 'collabAgentToolCall',
tool: 'resumeAgent',
status: 'completed',
senderThreadId: threadId,
receiverThreadIds: [childThreadId],
agentsStates: {}
},
threadId,
turnId
};
harness.notifications.push({ method: 'item/completed', params: resumeCompleted });
this.notificationHandler?.('item/completed', resumeCompleted);
}
if (harness.emitLateChildCommandAfterParentTool) {
const lateChildCommandStart = {
item: {
id: 'late-child-cmd',
type: 'commandExecution',
command: 'echo late'
},
threadId: childThreadId,
turnId: childTurnId
};
harness.notifications.push({ method: 'item/started', params: lateChildCommandStart });
this.notificationHandler?.('item/started', lateChildCommandStart);
}
}
const completed = { status: 'Completed', turn: { id: turnId } };
harness.notifications.push({ method: 'turn/completed', params: completed });
this.notificationHandler?.('turn/completed', completed);
return { turn: { id: turnId } };
}
async interruptTurn(params?: { threadId?: string; turnId?: string }): Promise<Record<string, never>> {
const threadId = params?.threadId ?? 'thread-unknown';
const turnId = params?.turnId ?? 'turn-unknown';
harness.interruptedTurns.push({ threadId, turnId });
const error = harness.interruptErrors.shift();
if (error) {
throw error;
}
if (harness.emitTurnAbortedOnInterrupt) {
const interrupted = {
threadId,
turnId,
status: 'interrupted',
turn: { id: turnId }
};
harness.notifications.push({ method: 'turn/completed', params: interrupted });
this.notificationHandler?.('turn/completed', interrupted);
}
return {};
}
async rollbackThread(params?: { threadId?: string; numTurns?: number }): Promise<{ thread: { id: string } }> {
const threadId = params?.threadId ?? 'thread-unknown';
harness.rollbackCalls.push({ threadId, numTurns: params?.numTurns ?? 0 });
const error = harness.rollbackErrors.shift();
if (error) {
throw error;
}
return { thread: { id: threadId } };
}
async disconnect(): Promise<void> {}
}
return { CodexAppServerClient: MockCodexAppServerClient };
});
vi.mock('./utils/buildHapiMcpBridge', () => ({
buildHapiMcpBridge: async (_client: unknown, options?: unknown) => {
harness.bridgeOptions.push(options);
return {
server: {
stop: () => {}
},
mcpServers: {}
};
}
}));
import { codexRemoteLauncher } from './codexRemoteLauncher';
type FakeAgentState = {
requests: Record<string, unknown>;
completedRequests: Record<string, unknown>;
};
function createMode(): EnhancedMode {
return {
permissionMode: 'default',
collaborationMode: 'default',
model: 'gpt-5.4'
};
}
function createSessionStub(
messages = ['hello from launcher test'],
mode = createMode(),
isolateMessages = false
) {
const queue = new MessageQueue2<EnhancedMode>((mode) => JSON.stringify(mode));
messages.forEach((message, index) => {
if (isolateMessages) {
queue.pushIsolated(message, mode);
} else if (index === 0 && messages.length > 1) {
queue.pushIsolateAndClear(message, mode);
} else {
queue.push(message, mode);
}
});
queue.close();
const sessionEvents: Array<{ type: string; [key: string]: unknown }> = [];
const codexMessages: unknown[] = [];
const summaryMessages: unknown[] = [];
const thinkingChanges: boolean[] = [];
const foundSessionIds: string[] = [];
const resetThreadCalls: string[] = [];
const collaborationModes: Array<EnhancedMode['collaborationMode'] | undefined> = [];
let currentPermissionMode: EnhancedMode['permissionMode'] = mode.permissionMode;
let currentModel: string | null | undefined = mode.model;
let currentModelReasoningEffort = mode.modelReasoningEffort;
let currentCollaborationMode: EnhancedMode['collaborationMode'] | undefined = mode.collaborationMode;
let agentState: FakeAgentState = {
requests: {},
completedRequests: {}
};
const rpcHandlers = new Map<string, (params: unknown) => unknown>();
const client = {
rpcHandlerManager: {
registerHandler(method: string, handler: (params: unknown) => unknown) {
rpcHandlers.set(method, handler);
}
},
updateMetadata(_handler: (metadata: Record<string, unknown>) => Record<string, unknown>) {},
updateAgentState(handler: (state: FakeAgentState) => FakeAgentState) {
agentState = handler(agentState);
},
sendAgentMessage(message: unknown) {
codexMessages.push(message);
},
sendUserMessage(_text: string) {},
sendClaudeSessionMessage(message: unknown) {
summaryMessages.push(message);
},
sendSessionEvent(event: { type: string; [key: string]: unknown }) {
sessionEvents.push(event);
}
};
const session = {
path: '/tmp/hapi-update',
logPath: '/tmp/hapi-update/test.log',
client,
queue,
codexArgs: undefined,
codexCliOverrides: undefined,
sessionId: null as string | null,
thinking: false,
getPermissionMode() {
return currentPermissionMode;
},
setModel(nextModel: string | null) {
currentModel = nextModel;
},
getModel() {
return currentModel;
},
setModelReasoningEffort(nextEffort: EnhancedMode['modelReasoningEffort']) {
currentModelReasoningEffort = nextEffort;
},
getCollaborationMode() {
return currentCollaborationMode;
},
setCollaborationMode(nextMode: EnhancedMode['collaborationMode']) {
currentCollaborationMode = nextMode;
collaborationModes.push(nextMode);
},
onThinkingChange(nextThinking: boolean) {
session.thinking = nextThinking;
thinkingChanges.push(nextThinking);
},
onSessionFound(id: string) {
session.sessionId = id;
foundSessionIds.push(id);
},
resetCodexThread() {
resetThreadCalls.push(session.sessionId ?? 'none');
session.sessionId = null;
},
sendAgentMessage(message: unknown) {
client.sendAgentMessage(message);
},
sendSessionEvent(event: { type: string; [key: string]: unknown }) {
client.sendSessionEvent(event);
},
sendUserMessage(text: string) {
client.sendUserMessage(text);
}
};
return {
session,
sessionEvents,
codexMessages,
summaryMessages,
thinkingChanges,
foundSessionIds,
resetThreadCalls,
rpcHandlers,
setPermissionMode: (nextMode: EnhancedMode['permissionMode']) => {
currentPermissionMode = nextMode;
},
getModel: () => currentModel,
getModelReasoningEffort: () => currentModelReasoningEffort,
getCollaborationMode: () => currentCollaborationMode,
collaborationModes,
getAgentState: () => agentState
};
}
describe('codexRemoteLauncher', () => {
afterEach(() => {
harness.notifications = [];
harness.dispatchNotification = null;
harness.registerRequestCalls = [];
harness.requestHandlers = new Map();
harness.initializeCalls = [];
harness.setFeatureEnablementCalls = [];
harness.failSetFeatureEnablement = false;
harness.listCollaborationModeCalls = 0;
harness.collaborationModeResponse = { data: [{ mode: 'default' }, { mode: 'plan' }] };
harness.failListCollaborationModes = false;
harness.listSkillsCalls = [];
harness.skillsListResponse = {
data: [{
cwd: '/tmp/hapi-update',
skills: [{
name: 'hapi',
description: 'Manage HAPI',
path: '/home/user/.agents/skills/hapi/SKILL.md',
scope: 'user',
enabled: true
}],
errors: []
}]
};
harness.startThreadIds = [];
harness.startThreadParams = [];
harness.resumeThreadIds = [];
harness.resumeThreadParams = [];
harness.startTurnThreadIds = [];
harness.startTurnParams = [];
harness.startTurnErrors = [];
harness.interruptedTurns = [];
harness.interruptErrors = [];
harness.rollbackCalls = [];
harness.rollbackErrors = [];
harness.compactThreadIds = [];
harness.goalSetCalls = [];
harness.goalGetCalls = [];
harness.goalClearCalls = [];
harness.goal = null;
harness.suppressGoalNotifications = false;
harness.suppressTurnCompletion = false;
harness.emitFailedCompletionAfterThreadSystemError = false;
harness.emitCyberPolicyAfterThreadSystemError = false;
harness.emitSafetyBuffering = false;
harness.safetyBufferingFasterModel = null;
harness.emitModelSafetyNotices = false;
harness.startTurnMessages = [];
harness.failResumeThreadIds = [];
harness.remainingThreadSystemErrors = 0;
harness.nextThreadSystemErrorMessage = null;
harness.failNextCompact = false;
harness.deferCompactCompletion = false;
harness.deferThreadStatusNotifications = false;
harness.emitChildThreadEvents = false;
harness.emitChildUsageEvents = false;
harness.emitChildGoalEvent = false;
harness.emitChildReasoningBurst = false;
harness.emitChildDoneStatusWithoutMessage = false;
harness.emitChildWaitStructuredOutput = false;
harness.emitChildTaskCompleteBeforeMessage = false;
harness.suppressChildTaskCompleteEvent = false;
harness.emitSecondChildMessage = false;
harness.emitLateChildCommandAfterParentTool = false;
harness.emitParentUsageEvents = false;
harness.emitParentGoalDuplicateEvents = false;
harness.emitChildNestedAgentTool = false;
harness.emitParentTitleChange = false;
harness.emitParentSpawnFailureWithoutAgentId = false;
harness.emitParentSpawnStartWithoutEnd = false;
harness.emitParentSpawnRouterStderrError = false;
harness.emitChildTaskStartedAfterParentSpawnStart = false;
harness.emitSecondParentSpawnStartWithoutEnd = false;
harness.emitParentSendInputFailure = false;
harness.emitParentResumeSuccess = false;
harness.emitParentV2AgentTools = false;
harness.emitRunningChildTurnBeforeSuppressedParent = false;
harness.emitCompletedChildTurnBeforeSuppressedParent = false;
harness.emitTurnAbortedOnInterrupt = false;
harness.bridgeOptions = [];
});
it('finishes a turn and emits ready when task lifecycle events include turn_id', async () => {
const {
session,
sessionEvents,
thinkingChanges,
foundSessionIds,
getModel
} = createSessionStub();
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(foundSessionIds).toContain('thread-1');
expect(getModel()).toBe('gpt-5.4');
expect(harness.startThreadParams).toHaveLength(1);
expect(harness.startThreadParams[0]?.threadSource).toBe('user');
expect(harness.initializeCalls).toEqual([{
clientInfo: {
name: 'hapi-codex-client',
version: '1.0.0'
},
capabilities: {
experimentalApi: true
}
}]);
expect(harness.setFeatureEnablementCalls).toEqual([{ enablement: { goals: true } }]);
expect(harness.notifications.map((entry) => entry.method)).toEqual([
'turn/started',
'item/started',
'item/completed',
'turn/completed'
]);
expect(sessionEvents.filter((event) => event.type === 'ready').length).toBeGreaterThanOrEqual(1);
expect(thinkingChanges).toContain(true);
expect(session.thinking).toBe(false);
});
it('uses the native skill catalog for completion and structured turn input', async () => {
const { session, rpcHandlers } = createSessionStub(['$hapi inspect']);
await codexRemoteLauncher(session as never);
expect(harness.listSkillsCalls).toEqual([{
cwds: ['/tmp/hapi-update'],
forceReload: false
}]);
expect(Array.from(rpcHandlers.keys())).toContain('listSkills');
expect(await rpcHandlers.get('listSkills')?.({})).toEqual({
success: true,
skills: [{ name: 'hapi', description: 'Manage HAPI' }]
});
expect(harness.startTurnParams[0]?.input).toEqual([
{ type: 'skill', name: 'hapi', path: '/home/user/.agents/skills/hapi/SKILL.md' },
{ type: 'text', text: ' inspect' }
]);
});
it('keeps the filesystem skill handler when native discovery reports errors', async () => {
harness.skillsListResponse = {
data: [{
cwd: '/tmp/hapi-update',
skills: [],
errors: ['failed to read skills']
}]
};
const { session, rpcHandlers } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(rpcHandlers.has('listSkills')).toBe(false);
});
it('reloads the native skill catalog after skills/changed', async () => {
const { session, rpcHandlers } = createSessionStub();
await codexRemoteLauncher(session as never);
harness.skillsListResponse = {
data: [{
cwd: '/tmp/hapi-update',
skills: [{
name: 'new-skill',
description: 'New skill',
path: '/tmp/new-skill/SKILL.md',
scope: 'repo',
enabled: true
}],
errors: []
}]
};
harness.dispatchNotification?.('skills/changed', {});
await vi.waitFor(() => {
expect(harness.listSkillsCalls.at(-1)).toEqual({
cwds: ['/tmp/hapi-update'],
forceReload: true
});
});
expect(await rpcHandlers.get('listSkills')?.({})).toEqual({
success: true,
skills: [{ name: 'new-skill', description: 'New skill' }]
});
});
it('routes app-server MCP elicitation through the existing user-input transport', async () => {
const { session, codexMessages, rpcHandlers, setPermissionMode } = createSessionStub();
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
const handler = harness.requestHandlers.get('mcpServer/elicitation/request');
expect(handler).toBeTypeOf('function');
const request = {
threadId: 'thread-1',
turnId: 'turn-1',
serverName: 'qmd',
mode: 'form',
message: 'Allow the qmd MCP server to run tool "status"?',
_meta: null,
requestedSchema: {
type: 'object',
properties: {
approval: {
type: 'string',
enum: ['allow', 'deny']
}
},
required: ['approval']
}
};
setPermissionMode('yolo');
const response = handler?.(request);
await vi.waitFor(() => {
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call',
name: 'request_user_input'
}));
});
const requestMessage = codexMessages.find((message) => (
typeof message === 'object' && message !== null && 'name' in message && message.name === 'request_user_input'
)) as { callId: string };
await rpcHandlers.get('permission')?.({
id: requestMessage.callId,
approved: true,
answers: { approval: { answers: ['allow'] } }
});
await expect(response).resolves.toEqual({
action: 'accept',
content: {
approval: 'allow'
},
_meta: null
});
});
it('sends Codex plan collaboration mode when the app-server advertises it', async () => {
const { session } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.listCollaborationModeCalls).toBe(1);
expect(harness.startTurnParams).toHaveLength(1);
expect(harness.startTurnParams[0]?.collaborationMode).toMatchObject({
mode: 'plan',
settings: {
model: 'gpt-5.4'
}
});
expect(harness.startTurnParams[0]?.model).toBeUndefined();
});
it('recognizes name-only plan collaboration mode entries', async () => {
harness.collaborationModeResponse = { data: [{ name: 'plan' }] };
const { session } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnParams).toHaveLength(1);
expect(harness.startTurnParams[0]?.collaborationMode).toMatchObject({
mode: 'plan'
});
});
it('recognizes alternate collaboration mode list envelopes', async () => {
harness.collaborationModeResponse = { collaborationModes: [{ id: 'plan' }] };
const { session } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnParams).toHaveLength(1);
expect(harness.startTurnParams[0]?.collaborationMode).toMatchObject({
mode: 'plan'
});
});
it('retries plan turns without collaborationMode when the runtime rejects the field', async () => {
harness.startTurnErrors.push(new Error('unknown field collaborationMode'));
const { session, sessionEvents } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnParams).toHaveLength(2);
expect(harness.startTurnParams[0]?.collaborationMode).toMatchObject({
mode: 'plan'
});
expect(harness.startTurnParams[1]?.collaborationMode).toBeUndefined();
expect(harness.startTurnParams[1]?.model).toBe('gpt-5.4');
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Plan mode is not supported by this Codex runtime. Sent as a normal turn instead.'
});
});
it('retries plan turns when unsupported errors use spaced collaboration mode wording', async () => {
harness.startTurnErrors.push(new Error('unsupported collaboration mode'));
const { session, sessionEvents } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnParams).toHaveLength(2);
expect(harness.startTurnParams[0]?.collaborationMode).toMatchObject({
mode: 'plan'
});
expect(harness.startTurnParams[1]?.collaborationMode).toBeUndefined();
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Plan mode is not supported by this Codex runtime. Sent as a normal turn instead.'
});
});
it('does not retry unrelated collaborationMode errors as normal turns', async () => {
harness.startTurnErrors.push(new Error('collaborationMode value failed policy validation'));
const { session, sessionEvents } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnParams).toHaveLength(1);
expect(sessionEvents).not.toContainEqual({
type: 'message',
message: 'Plan mode is not supported by this Codex runtime. Sent as a normal turn instead.'
});
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Process exited unexpectedly'
});
});
it('still attempts plan mode when collaborationMode/list omits plan', async () => {
harness.collaborationModeResponse = { data: [{ mode: 'default' }] };
const { session, sessionEvents } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnParams).toHaveLength(1);
expect(harness.startTurnParams[0]?.collaborationMode).toMatchObject({
mode: 'plan'
});
expect(sessionEvents).not.toContainEqual({
type: 'message',
message: 'Plan mode is not supported by this Codex runtime. Sent as a normal turn instead.'
});
});
it('sets a Codex goal without starting a normal turn', async () => {
const { session, sessionEvents, codexMessages, foundSessionIds } = createSessionStub(['/goal improve benchmark coverage']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(foundSessionIds).toEqual(['thread-1']);
expect(harness.startThreadParams).toHaveLength(1);
expect(harness.startThreadParams[0]?.threadSource).toBe('user');
expect(harness.startTurnParams).toHaveLength(0);
expect(harness.goalSetCalls).toEqual([{
threadId: 'thread-1',
objective: 'improve benchmark coverage',
status: 'active'
}]);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Goal active'
});
expect(sessionEvents).not.toContainEqual({
type: 'ready'
});
expect(codexMessages).toEqual(expect.arrayContaining([
expect.objectContaining({
type: 'thread_goal_updated',
thread_id: 'thread-1',
goal: expect.objectContaining({
objective: 'improve benchmark coverage',
status: 'active'
})
})
]));
});
it('still attempts goal RPC when dynamic goals feature enablement is unsupported', async () => {
harness.failSetFeatureEnablement = true;
const { session, sessionEvents } = createSessionStub(['/goal improve benchmark coverage']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.goalSetCalls).toEqual([{
threadId: 'thread-1',
objective: 'improve benchmark coverage',
status: 'active'
}]);
expect(harness.startTurnParams).toHaveLength(0);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Goal active'
});
});
it('forwards goal RPC responses when the app-server does not emit goal notifications', async () => {
harness.suppressGoalNotifications = true;
const { session, codexMessages } = createSessionStub(['/goal improve benchmark coverage']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(codexMessages).toEqual(expect.arrayContaining([
expect.objectContaining({
type: 'thread_goal_updated',
thread_id: 'thread-1',
goal: expect.objectContaining({
objective: 'improve benchmark coverage',
status: 'active'
})
})
]));
});
it('does not emit ready when a goal command interrupts an active turn', async () => {
harness.suppressTurnCompletion = true;
harness.emitTurnAbortedOnInterrupt = true;
const { session, sessionEvents } = createSessionStub(['first message', '/goal improve benchmark coverage']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.interruptedTurns).toEqual([{ threadId: 'thread-1', turnId: 'turn-1' }]);
expect(harness.goalSetCalls).toEqual([{
threadId: 'thread-1',
objective: 'improve benchmark coverage',
status: 'active'
}]);
expect(sessionEvents).not.toContainEqual({
type: 'ready'
});
});
it('switches collaboration mode to default after approving exit_plan_mode', async () => {
const { session, rpcHandlers, collaborationModes, getCollaborationMode } = createSessionStub(['plan this'], {
permissionMode: 'default',
collaborationMode: 'plan',
model: 'gpt-5.4'
});
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(harness.requestHandlers.has('item/tool/requestApproval')).toBe(true);
expect(rpcHandlers.has('permission')).toBe(true);
});
const approvalHandler = harness.requestHandlers.get('item/tool/requestApproval');
const approvalPromise = approvalHandler?.({
itemId: 'exit-1',
toolName: 'exit_plan_mode',
input: { plan: '1. Edit files' }
});
await vi.waitFor(() => {
expect(rpcHandlers.has('permission')).toBe(true);
});
await rpcHandlers.get('permission')?.({ id: 'exit-1', approved: true, decision: 'approved' });
await expect(approvalPromise).resolves.toEqual({ decision: 'accept' });
await running;
expect(collaborationModes).toContain('default');
expect(getCollaborationMode()).toBe('default');
});
it('surfaces thread-level systemError only after same-thread retries are exhausted', async () => {
harness.remainingThreadSystemErrors = 4;
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.resumeThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual(['thread-1', 'thread-1', 'thread-1', 'thread-1']);
expect(harness.startTurnMessages).toEqual(['first message', 'first message', 'first message', 'first message']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Task failed: Codex thread entered systemError'
});
expect(sessionEvents.filter((event) => event.type === 'ready').length).toBeGreaterThanOrEqual(1);
expect(session.thinking).toBe(false);
});
it('retries a thread-level systemError on the same thread without starting a fresh thread', async () => {
harness.remainingThreadSystemErrors = 1;
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.resumeThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual(['thread-1', 'thread-1']);
expect(harness.startTurnMessages).toEqual(['first message', 'first message']);
expect(session.sessionId).toBe('thread-1');
expect(sessionEvents).not.toContainEqual({
type: 'message',
message: 'Task failed: Codex thread entered systemError'
});
expect(session.thinking).toBe(false);
});
it('still retries a generic systemError when an empty failed turn completion confirms it', async () => {
harness.remainingThreadSystemErrors = 1;
harness.emitFailedCompletionAfterThreadSystemError = true;
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnMessages).toEqual(['first message', 'first message']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Task failed: Codex thread entered systemError; retrying same conversation (1/3)'
});
expect(session.thinking).toBe(false);
});
it('does not retry when a generic systemError is followed by a cyber-policy block', async () => {
harness.remainingThreadSystemErrors = 1;
harness.emitCyberPolicyAfterThreadSystemError = true;
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(harness.startTurnMessages).toEqual(['first message']);
const failureMessages = sessionEvents.filter((event) => event.type === 'message');
expect(failureMessages).toHaveLength(1);
expect(failureMessages[0]?.message).toContain('This content was flagged for possible cybersecurity risk.');
expect(failureMessages[0]?.message).toContain('https://openai.com/form/enterprise-trusted-access-for-cyber/');
expect(failureMessages[0]?.message).toContain('https://help.openai.com/en/articles/20001326');
expect(sessionEvents.filter((event) => event.type === 'ready').length).toBeGreaterThanOrEqual(1);
expect(session.thinking).toBe(false);
});
it('does not retry an explicitly non-retryable error even when its text is retryable', async () => {
harness.suppressTurnCompletion = true;
const { session, sessionEvents } = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(harness.startTurnMessages).toEqual(['first message']);
});
harness.dispatchNotification?.('error', {
threadId: 'thread-1',
turnId: 'turn-1',
error: { message: 'Selected model is at capacity' },
willRetry: false
});
await expect(running).resolves.toBe('exit');
expect(harness.startTurnMessages).toEqual(['first message']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Task failed: Selected model is at capacity'
});
expect(sessionEvents.some((event) => String(event.message ?? '').includes('retrying same conversation'))).toBe(false);
expect(session.thinking).toBe(false);
});
it('retries a safety-buffered turn with the offered faster model only after user opt-in', async () => {
harness.emitSafetyBuffering = true;
harness.safetyBufferingFasterModel = 'gpt-5.4-mini';
harness.emitTurnAbortedOnInterrupt = true;
const {
session,
rpcHandlers,
getAgentState,
getModel,
getModelReasoningEffort
} = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(Object.values(getAgentState().requests)).toContainEqual(expect.objectContaining({
tool: 'request_user_input'
}));
});
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(harness.interruptedTurns).toEqual([]);
expect(harness.rollbackCalls).toEqual([]);
const requestId = Object.keys(getAgentState().requests)[0];
await rpcHandlers.get('permission')?.({
id: requestId,
approved: true,
answers: {
safety_buffering_action: {
answers: ['Retry with a faster model']
}
}
});
await expect(running).resolves.toBe('exit');
expect(harness.interruptedTurns).toEqual([{ threadId: 'thread-1', turnId: 'turn-1' }]);
expect(harness.rollbackCalls).toEqual([{ threadId: 'thread-1', numTurns: 1 }]);
expect(harness.startTurnMessages).toEqual(['first message', 'first message']);
expect(harness.startTurnParams[1]).toMatchObject({
threadId: 'thread-1',
effort: 'low',
input: [{ type: 'text', text: 'first message' }],
collaborationMode: {
mode: 'default',
settings: {
model: 'gpt-5.4-mini',
reasoning_effort: 'low'
}
}
});
expect(getModel()).toBe('gpt-5.4-mini');
expect(getModelReasoningEffort()).toBe('low');
});
it('keeps the original safety-buffered turn running when the user dismisses the retry', async () => {
harness.emitSafetyBuffering = true;
harness.safetyBufferingFasterModel = 'gpt-5.4-mini';
const { session, rpcHandlers, getAgentState } = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(Object.keys(getAgentState().requests)).toHaveLength(1);
});
const requestId = Object.keys(getAgentState().requests)[0];
await rpcHandlers.get('permission')?.({
id: requestId,
approved: true,
answers: {
safety_buffering_action: {
answers: ['Keep waiting']
}
}
});
expect(harness.interruptedTurns).toEqual([]);
expect(harness.rollbackCalls).toEqual([]);
expect(harness.startTurnMessages).toEqual(['first message']);
await new Promise((resolve) => setTimeout(resolve, 0));
harness.dispatchNotification?.('model/safetyBuffering/updated', {
threadId: 'thread-1',
turnId: 'turn-1',
model: 'gpt-5.4',
useCases: ['cyber'],
reasons: ['review'],
showBufferingUi: true,
fasterModel: 'gpt-5.4-mini'
});
await new Promise((resolve) => setTimeout(resolve, 20));
expect(getAgentState().requests).toEqual({});
harness.dispatchNotification?.('model/safetyBuffering/updated', {
threadId: 'thread-1',
turnId: 'turn-1',
model: 'gpt-5.4',
useCases: ['cyber'],
reasons: ['review'],
showBufferingUi: false,
fasterModel: 'gpt-5.4-mini'
});
harness.dispatchNotification?.('model/safetyBuffering/updated', {
threadId: 'thread-1',
turnId: 'turn-1',
model: 'gpt-5.4',
useCases: ['cyber'],
reasons: ['review'],
showBufferingUi: true,
fasterModel: 'gpt-5.4-mini'
});
await vi.waitFor(() => {
expect(Object.keys(getAgentState().requests)).toHaveLength(1);
});
harness.dispatchNotification?.('turn/completed', {
threadId: 'thread-1',
turnId: 'turn-1',
turn: { id: 'turn-1', status: 'completed' }
});
await expect(running).resolves.toBe('exit');
});
it('dismisses safety-buffering choices when hidden or when agent output starts', async () => {
harness.emitSafetyBuffering = true;
harness.safetyBufferingFasterModel = 'gpt-5.4-mini';
const { session, getAgentState } = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(Object.keys(getAgentState().requests)).toHaveLength(1);
});
const hiddenRequestId = Object.keys(getAgentState().requests)[0];
harness.dispatchNotification?.('model/safetyBuffering/updated', {
threadId: 'thread-1',
turnId: 'turn-1',
model: 'gpt-5.4',
useCases: ['cyber'],
reasons: ['review'],
showBufferingUi: false,
fasterModel: 'gpt-5.4-mini'
});
await vi.waitFor(() => {
expect(getAgentState().requests).toEqual({});
expect(getAgentState().completedRequests[hiddenRequestId]).toMatchObject({
status: 'canceled',
reason: 'Safety buffering ended'
});
});
harness.dispatchNotification?.('model/safetyBuffering/updated', {
threadId: 'thread-1',
turnId: 'turn-1',
model: 'gpt-5.4',
useCases: ['cyber'],
reasons: ['review'],
showBufferingUi: true,
fasterModel: 'gpt-5.4-mini'
});
await vi.waitFor(() => {
expect(Object.keys(getAgentState().requests)).toHaveLength(1);
});
const outputRequestId = Object.keys(getAgentState().requests)[0];
expect(outputRequestId).not.toBe(hiddenRequestId);
harness.dispatchNotification?.('item/agentMessage/delta', {
threadId: 'thread-1',
turnId: 'turn-1',
itemId: 'message-1',
delta: 'Visible response'
});
await vi.waitFor(() => {
expect(getAgentState().requests).toEqual({});
expect(getAgentState().completedRequests[outputRequestId]).toMatchObject({
status: 'canceled',
reason: 'Agent output started'
});
});
expect(harness.interruptedTurns).toEqual([]);
expect(harness.rollbackCalls).toEqual([]);
harness.dispatchNotification?.('turn/completed', {
threadId: 'thread-1',
turnId: 'turn-1',
turn: { id: 'turn-1', status: 'completed' }
});
await expect(running).resolves.toBe('exit');
});
it('surfaces safety buffering without offering retry when fasterModel is null', async () => {
harness.emitSafetyBuffering = true;
harness.safetyBufferingFasterModel = null;
const { session, sessionEvents, getAgentState } = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Codex is taking extra time to review this request. Learn more: https://help.openai.com/en/articles/20001326'
});
});
expect(getAgentState().requests).toEqual({});
expect(harness.interruptedTurns).toEqual([]);
expect(harness.rollbackCalls).toEqual([]);
harness.dispatchNotification?.('turn/completed', {
threadId: 'thread-1',
turnId: 'turn-1',
turn: { id: 'turn-1', status: 'completed' }
});
await expect(running).resolves.toBe('exit');
});
it('does not replay a safety-buffered turn when rollback is unavailable', async () => {
harness.emitSafetyBuffering = true;
harness.safetyBufferingFasterModel = 'gpt-5.4-mini';
harness.emitTurnAbortedOnInterrupt = true;
harness.rollbackErrors.push(new Error('thread/rollback is unsupported'));
const { session, sessionEvents, rpcHandlers, getAgentState } = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(Object.keys(getAgentState().requests)).toHaveLength(1);
});
const requestId = Object.keys(getAgentState().requests)[0];
await rpcHandlers.get('permission')?.({
id: requestId,
approved: true,
answers: {
safety_buffering_action: {
answers: ['Retry with a faster model']
}
}
});
await expect(running).resolves.toBe('exit');
expect(harness.startTurnMessages).toEqual(['first message']);
expect(harness.rollbackCalls).toEqual([{ threadId: 'thread-1', numTurns: 1 }]);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Failed to retry with a faster model: thread/rollback is unsupported'
});
await vi.waitFor(() => {
expect(sessionEvents).toContainEqual({ type: 'ready' });
});
expect(session.thinking).toBe(false);
});
it('keeps the original turn running when safety-buffering interrupt fails', async () => {
harness.emitSafetyBuffering = true;
harness.safetyBufferingFasterModel = 'gpt-5.4-mini';
harness.interruptErrors.push(new Error('turn/interrupt failed'));
const { session, sessionEvents, rpcHandlers, getAgentState } = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(Object.keys(getAgentState().requests)).toHaveLength(1);
});
const requestId = Object.keys(getAgentState().requests)[0];
await rpcHandlers.get('permission')?.({
id: requestId,
approved: true,
answers: {
safety_buffering_action: {
answers: ['Retry with a faster model']
}
}
});
await vi.waitFor(() => {
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Failed to retry with a faster model: turn/interrupt failed'
});
});
expect(harness.startTurnMessages).toEqual(['first message']);
expect(harness.rollbackCalls).toEqual([]);
expect(session.thinking).toBe(true);
harness.dispatchNotification?.('turn/completed', {
threadId: 'thread-1',
turnId: 'turn-1',
turn: { id: 'turn-1', status: 'completed' }
});
await expect(running).resolves.toBe('exit');
expect(session.thinking).toBe(false);
});
it('surfaces model reroute and Trusted Access verification notices', async () => {
harness.emitModelSafetyNotices = true;
const { session, sessionEvents } = createSessionStub(['first message']);
await codexRemoteLauncher(session as never);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Codex rerouted the model from gpt-5.4 to gpt-5.4-codex (highRiskCyberActivity).'
});
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Your conversations have multiple flags for possible cybersecurity risk. Responses may take longer because extra safety checks are on. To get authorized for security work, join [Trusted Access for Cyber](https://chatgpt.com/cyber).'
});
});
it('compacts the same thread before retrying context-window overflow', async () => {
harness.remainingThreadSystemErrors = 1;
harness.nextThreadSystemErrorMessage = "Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.";
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.compactThreadIds).toEqual(['thread-1']);
expect(harness.startTurnThreadIds).toEqual(['thread-1', 'thread-1']);
expect(harness.startTurnMessages).toEqual(['first message', 'first message']);
expect(session.sessionId).toBe('thread-1');
expect(sessionEvents).not.toContainEqual({
type: 'message',
message: "Task failed: Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying."
});
expect(session.thinking).toBe(false);
});
it('retries asynchronous thread-level systemError notifications on the same thread', async () => {
harness.remainingThreadSystemErrors = 1;
harness.deferThreadStatusNotifications = true;
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.resumeThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual(['thread-1', 'thread-1']);
expect(harness.startTurnMessages).toEqual(['first message', 'first message']);
expect(session.sessionId).toBe('thread-1');
expect(sessionEvents).not.toContainEqual({
type: 'message',
message: 'Task failed: Codex thread entered systemError'
});
expect(session.thinking).toBe(false);
});
it('compacts before retrying asynchronous context-window overflow notifications', async () => {
harness.remainingThreadSystemErrors = 1;
harness.deferThreadStatusNotifications = true;
harness.nextThreadSystemErrorMessage = "Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.";
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.compactThreadIds).toEqual(['thread-1']);
expect(harness.startTurnThreadIds).toEqual(['thread-1', 'thread-1']);
expect(harness.startTurnMessages).toEqual(['first message', 'first message']);
expect(session.sessionId).toBe('thread-1');
expect(sessionEvents).not.toContainEqual({
type: 'message',
message: "Task failed: Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying."
});
expect(session.thinking).toBe(false);
});
it('does not create a new thread when same-conversation compact fails', async () => {
harness.remainingThreadSystemErrors = 1;
harness.nextThreadSystemErrorMessage = "Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.";
harness.failNextCompact = true;
const { session, sessionEvents } = createSessionStub(['first message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.compactThreadIds).toEqual(['thread-1']);
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(session.sessionId).toBe('thread-1');
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Task failed: context window overflow and same-conversation compact failed'
});
expect(session.thinking).toBe(false);
});
it('keeps using the old thread for later messages after same-thread retries are exhausted', async () => {
harness.remainingThreadSystemErrors = 4;
const { session } = createSessionStub(['first message', 'second message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.resumeThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual(['thread-1', 'thread-1', 'thread-1', 'thread-1', 'thread-1']);
expect(harness.startTurnMessages).toEqual(['first message', 'first message', 'first message', 'first message', 'second message']);
expect(session.sessionId).toBe('thread-1');
expect(session.thinking).toBe(false);
});
it('does not create a new thread when an existing conversation cannot be resumed', async () => {
harness.failResumeThreadIds = ['thread-old'];
const { session, sessionEvents } = createSessionStub(['first message']);
session.sessionId = 'thread-old';
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.resumeThreadIds).toEqual(['thread-old']);
expect(harness.resumeThreadParams).toHaveLength(1);
expect(harness.resumeThreadParams[0]?.threadSource).toBeUndefined();
expect(harness.startThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual([]);
expect(session.sessionId).toBe('thread-old');
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Task failed: Codex conversation thread-old could not be resumed; no new conversation was created. Reason: resume failed'
});
expect(session.thinking).toBe(false);
});
it('does not start a fresh thread for the next queued message after thread-level systemError', async () => {
harness.remainingThreadSystemErrors = 1;
const { session } = createSessionStub(['first message', 'second message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.resumeThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual(['thread-1', 'thread-1', 'thread-1']);
expect(harness.startTurnMessages).toEqual(['first message', 'first message', 'second message']);
expect(session.sessionId).toBe('thread-1');
expect(session.thinking).toBe(false);
});
it('surfaces Codex bash stdout instead of duplicating raw output json', async () => {
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call-result',
callId: 'cmd-1',
output: expect.objectContaining({
command: 'echo ok',
cwd: '/tmp/hapi-update',
stdout: 'ok\n',
exit_code: 0
})
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'tool-call-result',
callId: 'cmd-1',
output: expect.objectContaining({
output: 'ok\n'
})
}));
});
it('routes child thread messages into agent-run trace while keeping them out of the parent timeline', async () => {
harness.emitChildThreadEvents = true;
const { session, codexMessages, summaryMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'message',
message: 'child output should stay hidden'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'tool-call',
callId: 'child-cmd-1'
}));
expect(summaryMessages).not.toContainEqual(expect.objectContaining({
type: 'summary',
summary: 'Child Title'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'tool-call',
name: 'wait_agent',
callId: 'wait-child'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-trace',
agentId: 'child-thread',
message: expect.objectContaining({
type: 'message',
message: 'child output should stay hidden'
})
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-trace',
agentId: 'child-thread',
message: expect.objectContaining({
type: 'tool-call',
callId: 'child-cmd-1'
})
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
activity: 'Running command: echo child',
activityKind: 'running-command'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
summary: 'Child Title'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
status: 'completed',
result: 'child output should stay hidden',
activity: 'Completed: child output should stay hidden',
activityKind: 'completed'
}));
});
it('keeps the child final message as result when wait_agent only reports done', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildDoneStatusWithoutMessage = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
const completedUpdates = codexMessages.filter((message) => {
const record = message as Record<string, unknown>;
return record.type === 'agent-run-update'
&& record.agentId === 'child-thread'
&& record.status === 'completed';
}) as Array<Record<string, unknown>>;
expect(completedUpdates).toContainEqual(expect.objectContaining({
result: 'child output should stay hidden',
activity: 'Completed: child output should stay hidden'
}));
expect(completedUpdates).not.toContainEqual(expect.objectContaining({
result: expect.objectContaining({
status: 'done'
})
}));
});
it('fills wait_agent done without message from the latest child message', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildDoneStatusWithoutMessage = true;
harness.suppressChildTaskCompleteEvent = true;
harness.emitSecondChildMessage = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
const completedUpdates = codexMessages.filter((message) => {
const record = message as Record<string, unknown>;
return record.type === 'agent-run-update'
&& record.agentId === 'child-thread'
&& record.status === 'completed';
}) as Array<Record<string, unknown>>;
const lastCompleted = completedUpdates.at(-1);
expect(lastCompleted).toEqual(expect.objectContaining({
result: 'final child output should win',
activity: 'Completed: final child output should win'
}));
});
it('preserves wait_agent structured output when status message is empty', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildWaitStructuredOutput = true;
harness.suppressChildTaskCompleteEvent = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
const completedUpdates = codexMessages.filter((message) => {
const record = message as Record<string, unknown>;
return record.type === 'agent-run-update'
&& record.agentId === 'child-thread'
&& record.status === 'completed';
}) as Array<Record<string, unknown>>;
const lastCompleted = completedUpdates.at(-1);
expect(lastCompleted).toEqual(expect.objectContaining({
result: { value: 42 },
activity: 'Completed: {"value":42}'
}));
});
it('does not regress a completed child agent to running when message arrives late', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildDoneStatusWithoutMessage = true;
harness.emitChildTaskCompleteBeforeMessage = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
const terminalIndex = codexMessages.findIndex((message) => {
const record = message as Record<string, unknown>;
return record.type === 'agent-run-update'
&& record.agentId === 'child-thread'
&& record.status === 'completed';
});
expect(terminalIndex).toBeGreaterThanOrEqual(0);
const laterUpdates = codexMessages.slice(terminalIndex + 1).filter((message) => {
const record = message as Record<string, unknown>;
return record.type === 'agent-run-update'
&& record.agentId === 'child-thread';
}) as Array<Record<string, unknown>>;
expect(laterUpdates).not.toContainEqual(expect.objectContaining({
status: 'running'
}));
expect(laterUpdates).toContainEqual(expect.objectContaining({
status: 'completed',
result: 'child output should stay hidden',
activity: 'Completed: child output should stay hidden'
}));
});
it('surfaces send_input failures on the target child agent card', async () => {
harness.emitChildThreadEvents = true;
harness.emitParentSendInputFailure = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
status: 'running',
statusText: 'Sending input',
activity: 'Sending input',
activityKind: 'send_input'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
status: 'failed',
statusText: 'Send input failed',
activity: 'Send input failed: send failed',
activityKind: 'failed',
error: expect.objectContaining({
error: 'send failed'
})
}));
});
it('updates the target child agent card when resume_agent completes', async () => {
harness.emitChildThreadEvents = true;
harness.emitParentResumeSuccess = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
status: 'running',
statusText: 'Resuming agent',
activity: 'Resuming agent',
activityKind: 'resume_agent'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
status: 'running',
statusText: 'Resumed',
activity: 'Resumed',
activityKind: 'resume_agent',
result: expect.objectContaining({
status: 'completed',
targets: ['child-thread']
})
}));
});
it('does not regress a terminal child after resume_agent when a late command starts', async () => {
harness.emitChildThreadEvents = true;
harness.emitParentResumeSuccess = true;
harness.emitLateChildCommandAfterParentTool = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-trace',
agentId: 'child-thread',
message: expect.objectContaining({
type: 'tool-call',
callId: 'late-child-cmd'
})
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
activity: 'Running command: echo late',
activityKind: 'running-command',
status: 'running'
}));
});
it('throttles child agent reasoning activity updates instead of emitting one per delta', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildReasoningBurst = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
const thinkingUpdates = codexMessages.filter((message): message is Record<string, unknown> => {
return typeof message === 'object'
&& message !== null
&& (message as Record<string, unknown>).type === 'agent-run-update'
&& (message as Record<string, unknown>).agentId === 'child-thread'
&& (message as Record<string, unknown>).activityKind === 'thinking';
});
expect(thinkingUpdates.length).toBeLessThan(20);
expect(thinkingUpdates.length).toBeLessThanOrEqual(1);
});
it('keeps child usage and compact events out of the parent context stream', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildUsageEvents = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'token_count',
thread_id: 'child-thread'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'token_count',
info: expect.objectContaining({
last_token_usage: expect.objectContaining({
input_tokens: 999
})
})
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-trace',
agentId: 'child-thread',
message: expect.objectContaining({
type: 'context_compacted'
})
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'context_compacted',
thread_id: 'child-thread'
}));
});
it('keeps child goal events out of the parent goal stream', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildGoalEvent = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'thread_goal_updated',
thread_id: 'child-thread'
}));
});
it('suppresses duplicate parent goal updates that only change runtime counters', async () => {
harness.emitParentGoalDuplicateEvents = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
const goalMessages = codexMessages.filter((message): message is Record<string, unknown> => {
return Boolean(message && typeof message === 'object' && (message as Record<string, unknown>).type === 'thread_goal_updated');
});
expect(goalMessages).toHaveLength(2);
expect(goalMessages).toEqual([
expect.objectContaining({
thread_id: 'thread-1',
goal: expect.objectContaining({
status: 'active',
updatedAt: 2
})
}),
expect.objectContaining({
thread_id: 'thread-1',
goal: expect.objectContaining({
status: 'paused',
updatedAt: 6
})
})
]);
});
it('suppresses duplicate goal events from repeated show commands', async () => {
const { session, codexMessages } = createSessionStub([
'/goal keep benchmark work moving',
'/goal'
]);
await codexRemoteLauncher(session as never);
expect(harness.goalSetCalls).toHaveLength(1);
expect(harness.goalGetCalls).toEqual([{ threadId: 'thread-1' }]);
const goalMessages = codexMessages.filter((message): message is Record<string, unknown> => {
return Boolean(message && typeof message === 'object' && (message as Record<string, unknown>).type === 'thread_goal_updated');
});
expect(goalMessages).toHaveLength(1);
expect(goalMessages[0]).toEqual(expect.objectContaining({
thread_id: 'thread-1',
goal: expect.objectContaining({
objective: 'keep benchmark work moving',
status: 'active'
})
}));
});
it('marks parent usage and compact events with parent scope', async () => {
harness.emitParentUsageEvents = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'token_count',
thread_id: 'thread-1',
scope_role: 'parent',
scope: expect.objectContaining({
role: 'parent',
thread_id: 'thread-1'
})
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'context_compacted',
thread_id: 'thread-1',
scope_role: 'parent',
scope: expect.objectContaining({
role: 'parent',
thread_id: 'thread-1'
})
}));
});
it('marks child agents failed when they attempt to start nested agents', async () => {
harness.emitChildThreadEvents = true;
harness.emitChildNestedAgentTool = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-trace',
agentId: 'child-thread',
message: expect.objectContaining({
type: 'tool-call',
name: 'spawn_agent',
callId: 'nested-spawn'
})
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-trace',
agentId: 'child-thread',
message: expect.objectContaining({
type: 'tool-call-result',
callId: 'nested-spawn',
is_error: true,
output: 'Nested agent calls are disabled for child agents.'
})
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
status: 'failed',
activity: 'Failed: Nested agent calls are disabled for child agents.',
activityKind: 'failed'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'grandchild-thread'
}));
});
it('marks spawn_agent cards failed when Codex returns no agent id', async () => {
harness.emitParentSpawnFailureWithoutAgentId = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-start',
cardId: 'failed-spawn',
status: 'starting'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:failed-spawn',
cardId: 'failed-spawn',
status: 'failed',
statusText: 'Failed to start',
activityKind: 'failed',
error: expect.objectContaining({
status: 'failed',
error: 'invalid spawn arguments'
})
}));
});
it('forwards shared MultiAgent V2 tools as direct tool cards', async () => {
harness.emitParentV2AgentTools = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call',
name: 'spawn_agent',
callId: 'v2-spawn',
input: { task_name: 'review', message: 'Review this' }
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call-result',
callId: 'v2-spawn',
output: '{"task_name":"/root/review","nickname":"Reviewer"}'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call',
name: 'wait_agent',
callId: 'v2-wait',
input: { timeout_ms: 10_000 }
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call-result',
callId: 'v2-wait',
output: '{"message":"Wait timed out.","timed_out":true}'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-start',
cardId: 'v2-spawn'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:v2-spawn'
}));
});
it('marks pending spawn_agent cards failed with the Codex router argument error from stderr', async () => {
harness.emitParentSpawnStartWithoutEnd = true;
harness.emitParentSpawnRouterStderrError = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:failed-spawn',
cardId: 'failed-spawn',
status: 'failed',
statusText: 'Failed to start',
activityKind: 'failed',
error: 'Full-history forked agents inherit the parent agent type, model, and reasoning effort; ' +
'omit agent_type, model, and reasoning_effort, or spawn without a full-history fork.'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:failed-spawn',
cardId: 'failed-spawn',
error: 'spawn_agent did not return an agent id before the Codex session ended'
}));
});
it('links a lone pending spawn_agent card from the child task_started event', async () => {
harness.emitParentSpawnStartWithoutEnd = true;
harness.emitChildTaskStartedAfterParentSpawnStart = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
cardId: 'failed-spawn',
status: 'running',
activity: 'Started',
activityKind: 'running'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:failed-spawn',
cardId: 'failed-spawn'
}));
});
it('does not guess a child task_started card when multiple spawn_agent starts are pending', async () => {
harness.emitParentSpawnStartWithoutEnd = true;
harness.emitSecondParentSpawnStartWithoutEnd = true;
harness.emitChildTaskStartedAfterParentSpawnStart = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
cardId: 'failed-spawn'
}));
expect(codexMessages).not.toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'child-thread',
cardId: 'second-spawn'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:failed-spawn',
cardId: 'failed-spawn'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:second-spawn',
cardId: 'second-spawn'
}));
});
it('marks pending spawn_agent cards failed when the session ends before a result', async () => {
harness.emitParentSpawnStartWithoutEnd = true;
const { session, codexMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-start',
cardId: 'failed-spawn',
status: 'starting'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'agent-run-update',
agentId: 'spawn-error:failed-spawn',
cardId: 'failed-spawn',
status: 'failed',
statusText: 'Failed to start',
activityKind: 'failed',
error: 'spawn_agent did not return an agent id before the Codex session ended'
}));
});
it('applies parent-thread hapi change_title after disabling MCP-side title writes', async () => {
harness.emitParentTitleChange = true;
const { session, codexMessages, summaryMessages } = createSessionStub();
await codexRemoteLauncher(session as never);
expect(harness.bridgeOptions).toEqual([{ emitTitleSummary: false }]);
expect(summaryMessages).toContainEqual(expect.objectContaining({
type: 'summary',
summary: 'Parent Title'
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call',
name: 'mcp__hapi__change_title',
callId: 'title-parent',
input: { title: 'Parent Title' }
}));
expect(codexMessages).toContainEqual(expect.objectContaining({
type: 'tool-call-result',
callId: 'title-parent',
is_error: false
}));
});
it('clears codex thread state without starting a turn', async () => {
const { session, sessionEvents, resetThreadCalls } = createSessionStub(['/clear', 'next message']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(resetThreadCalls).toEqual(['none']);
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Context was reset'
});
expect(session.sessionId).toBe('thread-1');
});
it('interrupts an in-flight turn before clearing codex thread state', async () => {
harness.suppressTurnCompletion = true;
const { session, sessionEvents, resetThreadCalls } = createSessionStub(['first message', '/clear']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(harness.interruptedTurns).toEqual([{ threadId: 'thread-1', turnId: 'turn-1' }]);
expect(resetThreadCalls).toEqual(['thread-1']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Context was reset'
});
expect(session.thinking).toBe(false);
});
it('interrupts active child agent turns before clearing codex thread state', async () => {
harness.suppressTurnCompletion = true;
harness.emitRunningChildTurnBeforeSuppressedParent = true;
const { session, resetThreadCalls } = createSessionStub(['first message', '/clear']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.interruptedTurns).toEqual([
{ threadId: 'thread-1', turnId: 'turn-1' },
{ threadId: 'child-thread', turnId: 'child-turn' }
]);
expect(resetThreadCalls).toEqual(['thread-1']);
expect(session.thinking).toBe(false);
});
it('interrupts active child agent turns when the abort RPC is invoked', async () => {
harness.suppressTurnCompletion = true;
harness.emitRunningChildTurnBeforeSuppressedParent = true;
harness.emitTurnAbortedOnInterrupt = true;
const { session, rpcHandlers } = createSessionStub(['first message']);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(rpcHandlers.has('abort')).toBe(true);
});
await rpcHandlers.get('abort')?.({});
const exitReason = await running;
expect(exitReason).toBe('exit');
expect(harness.interruptedTurns).toEqual([
{ threadId: 'thread-1', turnId: 'turn-1' },
{ threadId: 'child-thread', turnId: 'child-turn' }
]);
expect(session.thinking).toBe(false);
});
it('does not interrupt completed child agent turns when clearing codex thread state', async () => {
harness.suppressTurnCompletion = true;
harness.emitCompletedChildTurnBeforeSuppressedParent = true;
const { session, resetThreadCalls } = createSessionStub(['first message', '/clear']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.interruptedTurns).toEqual([
{ threadId: 'thread-1', turnId: 'turn-1' }
]);
expect(resetThreadCalls).toEqual(['thread-1']);
expect(session.thinking).toBe(false);
});
it('compacts the current thread without starting a turn', async () => {
const { session, sessionEvents } = createSessionStub(['first message', '/compact']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(harness.compactThreadIds).toEqual(['thread-1']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Compaction started'
});
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Compaction completed'
});
});
it('does not start the next turn until manual compaction finishes', async () => {
harness.deferCompactCompletion = true;
const { session, sessionEvents } = createSessionStub([
'first message',
'/compact',
'after compact'
], createMode(), true);
const running = codexRemoteLauncher(session as never);
await vi.waitFor(() => {
expect(harness.compactThreadIds).toEqual(['thread-1']);
});
expect(harness.startTurnMessages).toEqual(['first message']);
expect(sessionEvents).not.toContainEqual({
type: 'message',
message: 'Compaction completed'
});
harness.dispatchNotification?.('item/completed', {
threadId: 'thread-1',
turnId: 'compact-1',
item: { id: 'compact-item-1', type: 'contextCompaction' }
});
harness.dispatchNotification?.('turn/completed', {
threadId: 'thread-1',
turn: { id: 'compact-1', status: 'completed' }
});
const exitReason = await running;
expect(exitReason).toBe('exit');
expect(harness.startTurnMessages).toEqual(['first message', 'after compact']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Compaction completed'
});
});
it('interrupts an in-flight turn before compacting the current thread', async () => {
harness.suppressTurnCompletion = true;
const { session, sessionEvents } = createSessionStub(['first message', '/compact']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual(['thread-1']);
expect(harness.startTurnThreadIds).toEqual(['thread-1']);
expect(harness.interruptedTurns).toEqual([{ threadId: 'thread-1', turnId: 'turn-1' }]);
expect(harness.compactThreadIds).toEqual(['thread-1']);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Compaction completed'
});
expect(session.thinking).toBe(false);
});
it('reports nothing to compact when no codex thread exists', async () => {
const { session, sessionEvents } = createSessionStub(['/compact']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual([]);
expect(harness.compactThreadIds).toEqual([]);
expect(sessionEvents).toContainEqual({
type: 'message',
message: 'Nothing to compact'
});
});
it('rejects argument-bearing codex slash commands without starting a turn', async () => {
const { session, sessionEvents } = createSessionStub(['/compact now']);
const exitReason = await codexRemoteLauncher(session as never);
expect(exitReason).toBe('exit');
expect(harness.startThreadIds).toEqual([]);
expect(harness.startTurnThreadIds).toEqual([]);
expect(harness.compactThreadIds).toEqual([]);
expect(sessionEvents).toContainEqual({
type: 'message',
message: '/compact does not accept arguments'
});
});
});