Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(heterogeneous-agents): stop persisting Codex warning items as tool calls (#23252)

Innei9d1506e24987
diff --git a/.agents/skills/heterogeneous-agent/SKILL.md b/.agents/skills/heterogeneous-agent/SKILL.md
index 0dfa5f1b935..0a810c302d3 100644
--- a/.agents/skills/heterogeneous-agent/SKILL.md
+++ b/.agents/skills/heterogeneous-agent/SKILL.md
@@ -33,7 +33,7 @@ Use this skill when the bug or feature lives in the external CLI agent pipeline,
- `apps/desktop/src/main/modules/heterogeneousAgent/drivers/claudeCode.ts`
- `apps/desktop/src/main/modules/heterogeneousAgent/drivers/codex.ts`
- `packages/heterogeneous-agents/src/adapters/claudeCode.ts`
-- `packages/heterogeneous-agents/src/adapters/codex.ts`
+- `packages/heterogeneous-agents/src/adapters/codex/index.ts`
- `src/store/chat/slices/agentRun/actions/transports/hetero/heterogeneousAgentExecutor.ts`
- `src/store/chat/slices/agentRun/actions/__tests__/heterogeneousAgentExecutor.test.ts`
diff --git a/.agents/skills/heterogeneous-agent/references/debug-workflow.md b/.agents/skills/heterogeneous-agent/references/debug-workflow.md
index 1573aa29cef..0918dc219f9 100644
--- a/.agents/skills/heterogeneous-agent/references/debug-workflow.md
+++ b/.agents/skills/heterogeneous-agent/references/debug-workflow.md
@@ -218,7 +218,7 @@ The executor only cuts a new assistant message when it receives a step-boundary
Relevant files:
-- `packages/heterogeneous-agents/src/adapters/codex.ts`
+- `packages/heterogeneous-agents/src/adapters/codex/index.ts`
- `src/store/chat/slices/agentRun/actions/transports/hetero/heterogeneousAgentExecutor.ts`
## 5. Check Tool Persistence Invariants
diff --git a/apps/server/src/services/workRegistration/shellWorkRegistration.ts b/apps/server/src/services/workRegistration/shellWorkRegistration.ts
index f0d37de5d6c..7a8e83e7828 100644
--- a/apps/server/src/services/workRegistration/shellWorkRegistration.ts
+++ b/apps/server/src/services/workRegistration/shellWorkRegistration.ts
@@ -20,7 +20,7 @@ const log = debug('lobe-server:shell-work-registration');
* shell `apiName`:
*
* - codex `command_execution` β€” stdout in `state.stdout` / `state.output`,
- * `state.exitCode` on completion (`adapters/codex.ts`).
+ * `state.exitCode` on completion (`adapters/codex/`).
* - claude-code `Bash` β€” stdout is the tool MESSAGE CONTENT (the adapter
* persists the tool_result text as content, no structured state); failures
* surface via the plugin `error` column (is_error), never an exit code.
diff --git a/packages/builtin-tools/src/fileEditScan/index.ts b/packages/builtin-tools/src/fileEditScan/index.ts
index 89f51dcf2e7..f8980b0672a 100644
--- a/packages/builtin-tools/src/fileEditScan/index.ts
+++ b/packages/builtin-tools/src/fileEditScan/index.ts
@@ -61,7 +61,7 @@ const LOCAL_SYSTEM_FILE_APIS = new Map<string, StructuredFileApiKind>([
/**
* Codex heterogeneous agent.
- * Source: `@lobechat/heterogeneous-agents` `adapters/codex.ts` β€” `toToolPayload`
+ * Source: `@lobechat/heterogeneous-agents` `adapters/codex/` β€” `toToolPayload`
* stamps every Codex tool call with `identifier = 'codex'` (`CODEX_IDENTIFIER`)
* and `synthesizeFileChangePluginState` produces apiName `file_change`, state
* shape `{ changes: [{ path, kind, diffText?, linesAdded, linesDeleted }] }`
@@ -98,7 +98,7 @@ const CLAUDE_CODE_EDIT_APIS = new Set(['Edit', 'Write', 'MultiEdit']);
* `transcript/claudeCode.ts` `CLAUDE_CODE_IDENTIFIER` + tool name `Bash`;
* failures surface via the tool-result `error` (is_error), not a state field.
* - codex command_execution. Source: `@lobechat/heterogeneous-agents`
- * `adapters/codex.ts` `CODEX_IDENTIFIER` + `CODEX_COMMAND_API`; state pluginState
+ * `adapters/codex/` `CODEX_IDENTIFIER` + `CODEX_COMMAND_API`; state pluginState
* carries `{ exitCode?, success, … }`.
*
* - lobe-skills runCommand / execScript, DEVICE rows only. Source:
diff --git a/packages/heterogeneous-agents/src/adapters/codex.errorItem.test.ts b/packages/heterogeneous-agents/src/adapters/codex.errorItem.test.ts
new file mode 100644
index 00000000000..980aae22b51
--- /dev/null
+++ b/packages/heterogeneous-agents/src/adapters/codex.errorItem.test.ts
@@ -0,0 +1,85 @@
+import { describe, expect, it } from 'vitest';
+
+import { CodexAdapter } from './codex';
+
+const configWarningItem = {
+ id: 'item_0',
+ message: '`[features].multi_agent` is deprecated. It still controls the V1 fallback.',
+ type: 'error',
+};
+
+describe('CodexAdapter error items', () => {
+ it('does not map a non-fatal error item into a tool call', () => {
+ const adapter = new CodexAdapter();
+ adapter.adapt({ thread_id: 'thread-1', type: 'thread.started' });
+ adapter.adapt({ type: 'turn.started' });
+
+ const events = [
+ ...adapter.adapt({ item: configWarningItem, type: 'item.started' }),
+ ...adapter.adapt({ item: configWarningItem, type: 'item.completed' }),
+ ];
+
+ expect(events).toEqual([]);
+ });
+
+ it('keeps the run going and leaves real tool calls intact after an error item', () => {
+ const adapter = new CodexAdapter();
+ adapter.adapt({ type: 'turn.started' });
+ adapter.adapt({ item: configWarningItem, type: 'item.completed' });
+
+ const command = {
+ aggregated_output: '/tmp\n',
+ command: '/bin/zsh -lc pwd',
+ exit_code: 0,
+ id: 'item_1',
+ status: 'completed',
+ type: 'command_execution',
+ };
+ const toolEvents = adapter.adapt({ item: command, type: 'item.completed' });
+ const toolsCalling = toolEvents.find(
+ (event) => event.type === 'stream_chunk' && event.data.chunkType === 'tools_calling',
+ );
+
+ expect(toolsCalling?.data.toolsCalling.map((tool: { id: string }) => tool.id)).toEqual([
+ 'item_1',
+ ]);
+ expect(toolEvents.find((event) => event.type === 'tool_end')?.data).toMatchObject({
+ isSuccess: true,
+ toolCallId: 'item_1',
+ });
+
+ const endEvents = adapter.adapt({ type: 'turn.completed', usage: {} });
+ expect(endEvents.some((event) => event.type === 'error')).toBe(false);
+ expect(endEvents.filter((event) => event.type === 'tool_end')).toEqual([]);
+ expect(endEvents.at(-1)?.type).toBe('agent_runtime_end');
+ });
+
+ it('does not open a new step for an error item between agent messages', () => {
+ const adapter = new CodexAdapter();
+ adapter.adapt({ type: 'turn.started' });
+ adapter.adapt({
+ item: { id: 'item_0', text: 'First', type: 'agent_message' },
+ type: 'item.completed',
+ });
+ adapter.adapt({ item: { ...configWarningItem, id: 'item_1' }, type: 'item.completed' });
+
+ const next = adapter.adapt({
+ item: { id: 'item_2', text: 'Second', type: 'agent_message' },
+ type: 'item.completed',
+ });
+
+ expect(next.map((event) => event.type)).toEqual(['stream_chunk']);
+ });
+
+ it('still fails the run on a top-level Codex error event', () => {
+ const adapter = new CodexAdapter();
+ adapter.adapt({ type: 'turn.started' });
+
+ const events = adapter.adapt({ message: 'Selected model is unavailable.', type: 'error' });
+
+ expect(events.at(-1)).toMatchObject({
+ data: { message: 'Selected model is unavailable.' },
+ type: 'error',
+ });
+ });
+});
diff --git a/packages/heterogeneous-agents/src/adapters/codex.ts b/packages/heterogeneous-agents/src/adapters/codex/index.ts
similarity index 92%
rename from packages/heterogeneous-agents/src/adapters/codex.ts
rename to packages/heterogeneous-agents/src/adapters/codex/index.ts
index 3ecd03ad80d..d34c3e1d45f 100644
--- a/packages/heterogeneous-agents/src/adapters/codex.ts
+++ b/packages/heterogeneous-agents/src/adapters/codex/index.ts
@@ -1,31 +1,45 @@
-import { imagePlaceholder } from '../imageEcho';
+import { imagePlaceholder } from '../../imageEcho';
import type {
AgentEventAdapter,
HeterogeneousAgentEvent,
HeterogeneousRateLimitInfo,
HeterogeneousTerminalErrorData,
- HeterogeneousToolResultImage,
StepCompleteData,
StreamStartData,
ToolCallPayload,
ToolResultData,
ToolStateChunkData,
UsageData,
-} from '../types';
+} from '../../types';
import {
CODEX_COMMAND_OUTPUT_MAX_LENGTH,
truncateCodexCommandOutput,
-} from '../utils/codexCommandOutput';
-import { isCodexCapacityError } from '../utils/codexErrors';
-import { toCodexUsageData, toTurnUsageFromCumulative } from '../utils/codexUsage';
+} from '../../utils/codexCommandOutput';
+import { isCodexCapacityError } from '../../utils/codexErrors';
+import { toCodexUsageData, toTurnUsageFromCumulative } from '../../utils/codexUsage';
+import type {
+ CodexCollabToolCallItem,
+ CodexFileChangeItem,
+ CodexMcpToolCallItem,
+ CodexTodoListItem,
+ CodexToolItem,
+ CodexWebSearchItem,
+} from './items';
+import {
+ CODEX_COMMAND_API,
+ isCollabToolCallItem,
+ isCommandExecutionItem,
+ isFileChangeItem,
+ isImageOutputItem,
+ isMcpToolCallItem,
+ isNonToolItemType,
+ isTodoListItem,
+ isWebSearchItem,
+} from './items';
+
+export type { CodexImageOutputItem } from './items';
const CODEX_IDENTIFIER = 'codex';
-const CODEX_COLLAB_TOOL_CALL_API = 'collab_tool_call';
-const CODEX_COMMAND_API = 'command_execution';
-const CODEX_FILE_CHANGE_API = 'file_change';
-const CODEX_MCP_TOOL_CALL_API = 'mcp_tool_call';
-const CODEX_TODO_LIST_API = 'todo_list';
-const CODEX_WEB_SEARCH_API = 'web_search';
const CODEX_USAGE_SETTINGS_URL = 'https://chatgpt.com/codex/settings/usage';
const CODEX_USER_RATE_LIMIT_PATTERNS = [
@@ -37,29 +51,6 @@ const CODEX_USER_RATE_LIMIT_PATTERNS = [
const CODEX_RETRY_AT_PATTERN =
/\btry again at\s+(\d{1,2})(?::(\d{2}))?(?:(AM|PM)|\s+(AM|PM))?(?:\s+\(([^()]+)\))?/i;
-interface CodexBaseItem {
- id: string;
- status?: string;
- type: string;
-}
-
-interface CodexCommandExecutionItem extends CodexBaseItem {
- aggregated_output?: string;
- command?: string;
- exit_code?: number | null;
-}
-
-/** Image tool outputs omitted by exec --json and recovered from its rollout. */
-export interface CodexImageOutputItem extends CodexBaseItem {
- images: HeterogeneousToolResultImage[];
- type: 'image_output';
-}
-
-interface CodexTodoListEntry {
- completed?: boolean;
- text?: string;
-}
-
interface TodoListPluginItem {
status: 'completed' | 'processing' | 'todo';
text: string;
@@ -71,61 +62,6 @@ interface StreamedCommandOutput {
truncated: boolean;
}
-interface CodexTodoListItem extends CodexBaseItem {
- items?: CodexTodoListEntry[];
-}
-
-interface CodexFileChangeEntry {
- diffText?: string;
- kind?: string;
- linesAdded?: number;
- linesDeleted?: number;
- path?: string;
-}
-
-interface CodexFileChangeItem extends CodexBaseItem {
- changes?: CodexFileChangeEntry[];
- diffText?: string;
- linesAdded?: number;
- linesDeleted?: number;
-}
-
-interface CodexMcpToolCallItem extends CodexBaseItem {
- arguments?: unknown;
- error?: unknown;
- result?: unknown;
- server?: string;
- tool?: string;
-}
-
-interface CodexWebSearchItem extends CodexBaseItem {
- action?: unknown;
- query?: unknown;
-}
-
-interface CodexCollabAgentState {
- message?: string | null;
- status?: string;
-}
-
-interface CodexCollabToolCallItem extends CodexBaseItem {
- agents_states?: Record<string, CodexCollabAgentState>;
- prompt?: string | null;
- receiver_thread_ids?: string[];
- sender_thread_id?: string;
- tool?: string;
-}
-
-type CodexToolItem =
- | CodexBaseItem
- | CodexCollabToolCallItem
- | CodexCommandExecutionItem
- | CodexFileChangeItem
- | CodexImageOutputItem
- | CodexMcpToolCallItem
- | CodexTodoListItem
- | CodexWebSearchItem;
-
interface ZonedDateTimeParts {
day: number;
hour: number;
@@ -135,27 +71,6 @@ interface ZonedDateTimeParts {
year: number;
}
-const isImageOutputItem = (item: CodexToolItem): item is CodexImageOutputItem =>
- item.type === 'image_output';
-
-const isCommandExecutionItem = (item: CodexToolItem): item is CodexCommandExecutionItem =>
- item.type === CODEX_COMMAND_API;
-
-const isCollabToolCallItem = (item: CodexToolItem): item is CodexCollabToolCallItem =>
- item.type === CODEX_COLLAB_TOOL_CALL_API;
-
-const isFileChangeItem = (item: CodexToolItem): item is CodexFileChangeItem =>
- item.type === CODEX_FILE_CHANGE_API;
-
-const isMcpToolCallItem = (item: CodexToolItem): item is CodexMcpToolCallItem =>
- item.type === CODEX_MCP_TOOL_CALL_API;
-
-const isTodoListItem = (item: CodexToolItem): item is CodexTodoListItem =>
- item.type === CODEX_TODO_LIST_API;
-
-const isWebSearchItem = (item: CodexToolItem): item is CodexWebSearchItem =>
- item.type === CODEX_WEB_SEARCH_API;
-
const normalizeTodoListItems = (item: CodexTodoListItem) =>
(item.items || [])
.map((todo) => ({
@@ -990,7 +905,7 @@ export class CodexAdapter implements AgentEventAdapter {
}
private handleItemStarted(item: any): HeterogeneousAgentEvent[] {
- if (!item?.id || !item?.type || item.type === 'agent_message') return [];
+ if (!item?.id || !item?.type || isNonToolItemType(item.type)) return [];
this.hasToolActivitySinceAgentMessage = true;
@@ -1117,7 +1032,7 @@ export class CodexAdapter implements AgentEventAdapter {
return this.handleAgentMessageContent(item.id, item.text);
}
- if (!item.id) return [];
+ if (isNonToolItemType(item.type) || !item.id) return [];
this.streamedCommandOutput.delete(item.id);
// Codex emits the same status-less todo completion before both a successful
diff --git a/packages/heterogeneous-agents/src/adapters/codex/items.ts b/packages/heterogeneous-agents/src/adapters/codex/items.ts
new file mode 100644
index 00000000000..34031d314c3
--- /dev/null
+++ b/packages/heterogeneous-agents/src/adapters/codex/items.ts
@@ -0,0 +1,113 @@
+import type { HeterogeneousToolResultImage } from '../../types';
+
+const CODEX_COLLAB_TOOL_CALL_API = 'collab_tool_call';
+export const CODEX_COMMAND_API = 'command_execution';
+const CODEX_ERROR_ITEM_TYPE = 'error';
+const CODEX_FILE_CHANGE_API = 'file_change';
+const CODEX_MCP_TOOL_CALL_API = 'mcp_tool_call';
+const CODEX_TODO_LIST_API = 'todo_list';
+const CODEX_WEB_SEARCH_API = 'web_search';
+
+interface CodexBaseItem {
+ id: string;
+ status?: string;
+ type: string;
+}
+
+interface CodexCommandExecutionItem extends CodexBaseItem {
+ aggregated_output?: string;
+ command?: string;
+ exit_code?: number | null;
+}
+
+/** Image tool outputs omitted by exec --json and recovered from its rollout. */
+export interface CodexImageOutputItem extends CodexBaseItem {
+ images: HeterogeneousToolResultImage[];
+ type: 'image_output';
+}
+
+interface CodexTodoListEntry {
+ completed?: boolean;
+ text?: string;
+}
+
+export interface CodexTodoListItem extends CodexBaseItem {
+ items?: CodexTodoListEntry[];
+}
+
+interface CodexFileChangeEntry {
+ diffText?: string;
+ kind?: string;
+ linesAdded?: number;
+ linesDeleted?: number;
+ path?: string;
+}
+
+export interface CodexFileChangeItem extends CodexBaseItem {
+ changes?: CodexFileChangeEntry[];
+ diffText?: string;
+ linesAdded?: number;
+ linesDeleted?: number;
+}
+
+export interface CodexMcpToolCallItem extends CodexBaseItem {
+ arguments?: unknown;
+ error?: unknown;
+ result?: unknown;
+ server?: string;
+ tool?: string;
+}
+
+export interface CodexWebSearchItem extends CodexBaseItem {
+ action?: unknown;
+ query?: unknown;
+}
+
+interface CodexCollabAgentState {
+ message?: string | null;
+ status?: string;
+}
+
+export interface CodexCollabToolCallItem extends CodexBaseItem {
+ agents_states?: Record<string, CodexCollabAgentState>;
+ prompt?: string | null;
+ receiver_thread_ids?: string[];
+ sender_thread_id?: string;
+ tool?: string;
+}
+
+export type CodexToolItem =
+ | CodexBaseItem
+ | CodexCollabToolCallItem
+ | CodexCommandExecutionItem
+ | CodexFileChangeItem
+ | CodexImageOutputItem
+ | CodexMcpToolCallItem
+ | CodexTodoListItem
+ | CodexWebSearchItem;
+
+// exec --json reports non-fatal notices (config/deprecation warnings) as `error` items;
+// fatal failures arrive as top-level `error` / `turn.failed` events instead.
+export const isNonToolItemType = (type: string) =>
+ type === 'agent_message' || type === CODEX_ERROR_ITEM_TYPE;
+
+export const isImageOutputItem = (item: CodexToolItem): item is CodexImageOutputItem =>
+ item.type === 'image_output';
+
+export const isCommandExecutionItem = (item: CodexToolItem): item is CodexCommandExecutionItem =>
+ item.type === CODEX_COMMAND_API;
+
+export const isCollabToolCallItem = (item: CodexToolItem): item is CodexCollabToolCallItem =>
+ item.type === CODEX_COLLAB_TOOL_CALL_API;
+
+export const isFileChangeItem = (item: CodexToolItem): item is CodexFileChangeItem =>
+ item.type === CODEX_FILE_CHANGE_API;
+
+export const isMcpToolCallItem = (item: CodexToolItem): item is CodexMcpToolCallItem =>
+ item.type === CODEX_MCP_TOOL_CALL_API;
+
+export const isTodoListItem = (item: CodexToolItem): item is CodexTodoListItem =>
+ item.type === CODEX_TODO_LIST_API;
+
+export const isWebSearchItem = (item: CodexToolItem): item is CodexWebSearchItem =>
+ item.type === CODEX_WEB_SEARCH_API;
diff --git a/packages/heterogeneous-agents/src/spawn/classifyProcessFailure.ts b/packages/heterogeneous-agents/src/spawn/classifyProcessFailure.ts
index cd3e480bb6e..e1d4471b8fe 100644
--- a/packages/heterogeneous-agents/src/spawn/classifyProcessFailure.ts
+++ b/packages/heterogeneous-agents/src/spawn/classifyProcessFailure.ts
@@ -9,7 +9,7 @@ import type { HeterogeneousTerminalErrorData } from '../types';
/**
* Process-level failure classification for `lh hetero exec` runs.
*
- * The stream adapters (`adapters/claudeCode.ts`, `adapters/codex.ts`) classify
+ * The stream adapters (`adapters/claudeCode.ts`, `adapters/codex/`) classify
* failures the CLI reports in-stream (overloaded / rate-limit / auth relayed
* via a `result` event), but a run that dies BEFORE the agent CLI produces any
* stream β€” `spawn claude ENOENT`, an auth failure printed straight to stderr β€”