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(hetero): steer follow-ups into native Codex runs on connected devices (#23264)

Mocha6933028268f6
diff --git a/apps/cli/src/commands/hetero/index.ts b/apps/cli/src/commands/hetero/index.ts
index 11df2e0de38..b3c6c510322 100644
--- a/apps/cli/src/commands/hetero/index.ts
+++ b/apps/cli/src/commands/hetero/index.ts
@@ -1057,7 +1057,7 @@ const exec = async (options: ExecOptions): Promise<void> => {
return { code: 1, signal: null as NodeJS.Signals | null };
});
- // Stream events (steering Claude Code, see withHeteroSteer); each is optionally written as JSONL
+ // Stream events (steering Claude Code or native Codex, see withHeteroSteer); each is optionally written as JSONL
// and pushed into the ingester. A resume `error` is withheld and flags a retry instead.
let resumeNotFound = false;
let sawTerminalError = false;
diff --git a/apps/cli/src/utils/heteroSteer.ts b/apps/cli/src/utils/heteroSteer.ts
index cec88a7eeeb..48a4d6903d3 100644
--- a/apps/cli/src/utils/heteroSteer.ts
+++ b/apps/cli/src/utils/heteroSteer.ts
@@ -109,13 +109,14 @@ const isMainStreamStart = (event: { data?: any; type: string }) =>
event.type === 'stream_start' && !event.data?.subagent;
/**
- * Stream a Claude Code run's events while typing the follow-ups the user
- * queues behind it into the live session (steer), instead of leaving them for
- * the next turn.
+ * Stream a Claude Code or native Codex run's events while typing the
+ * follow-ups the user queues behind it into the live session (steer), instead
+ * of leaving them for the next turn.
*
* Polls `aiAgent.waitHeteroSteer` for as long as the run streams and its
* session takes input; a message the session refuses goes back with
- * `aiAgent.returnHeteroSteer`. Claude Code replays every message it picks up:
+ * `aiAgent.returnHeteroSteer`. The agent reports every message it picks up
+ * (Claude Code replays it, Codex echoes its client id):
* the replay of a delivery is forwarded for the server to write the message
* into the conversation; any other replay (the run's own prompt) is dropped.
* Once the server answered, the next main `stream_start` carries `liveSteer`
diff --git a/packages/heterogeneous-agents/src/codex/codexAgentHandle.test.ts b/packages/heterogeneous-agents/src/codex/codexAgentHandle.test.ts
index 90e2f98af64..4318577737a 100644
--- a/packages/heterogeneous-agents/src/codex/codexAgentHandle.test.ts
+++ b/packages/heterogeneous-agents/src/codex/codexAgentHandle.test.ts
@@ -15,6 +15,7 @@ const mocks = vi.hoisted(() => ({
(options: CodexThreadTurnOptions, session: CodexThreadSessionOptions) => Promise<void>
>(),
sessionClose: vi.fn(),
+ steer: vi.fn(),
}));
vi.mock('./CodexAppServerClient', () => ({
CodexAppServerClient: class {
@@ -34,6 +35,7 @@ vi.mock('./CodexThreadSession', () => ({
}
close = mocks.sessionClose;
interrupt = mocks.interrupt;
+ steer = mocks.steer;
run(options: CodexThreadTurnOptions) {
return mocks.run(options, this.options);
}
@@ -187,6 +189,75 @@ describe('createCodexAgentHandle', () => {
expect(await handle.exit).toEqual({ code: null, signal: 'SIGINT' });
});
+ const runningStatus = {
+ activeTasks: [],
+ lastEventAt: 1,
+ sessionId: 'op-device',
+ state: 'running' as const,
+ transport: 'codex-app-server' as const,
+ };
+
+ // ROOT CAUSE:
+ // The native handle exposed no `sendUserMessage`, so `lh hetero exec` never polled the
+ // steer queue and a follow-up sent mid-run waited for the whole Codex turn to finish.
+ /** @example A follow-up joins the running native turn through `turn/steer`. */
+ it('steers a follow-up into the running turn once it has started', async () => {
+ let releaseTurn!: () => void;
+ const turnDone = new Promise<void>((resolve) => {
+ releaseTurn = resolve;
+ });
+ let started!: () => void;
+ mocks.steer.mockResolvedValue('accepted');
+ mocks.run.mockImplementation(async (_turn, session) => {
+ await new Promise<void>((resolve) => {
+ started = () => {
+ session.onRuntimeStatus(runningStatus);
+ resolve();
+ };
+ });
+ await turnDone;
+ });
+ const handle = await createCodexAgentHandle(options);
+
+ const sent = handle.sendUserMessage!('look at the tests too', { uuid: 'msg-steer' });
+ await Promise.resolve();
+ /** @example Before turn/start answers there is no turn to steer, so the write waits. */
+ expect(mocks.steer).not.toHaveBeenCalled();
+
+ started();
+ expect(await sent).toBe(true);
+ /** @example The delivery id comes back as the user message's client id for the confirmation. */
+ expect(mocks.steer).toHaveBeenCalledWith(
+ [{ text: 'look at the tests too', text_elements: [], type: 'text' }],
+ 'msg-steer',
+ );
+
+ releaseTurn();
+ await handle.exit;
+ });
+
+ /** @example A turn that takes no input (review, compaction, already ending) leaves the message for the next run. */
+ it('reports a refused steer as not delivered', async () => {
+ mocks.steer.mockResolvedValue('rejected');
+ mocks.run.mockImplementation(async (_turn, session) => {
+ session.onRuntimeStatus(runningStatus);
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ });
+ const handle = await createCodexAgentHandle(options);
+
+ expect(await handle.sendUserMessage!('one more thing', { uuid: 'msg-refused' })).toBe(false);
+ await handle.exit;
+ });
+
+ /** @example A run that ended before its turn started takes nothing; the message waits for the next run. */
+ it('does not steer once the run has ended', async () => {
+ const handle = await createCodexAgentHandle(options);
+ await handle.exit;
+
+ expect(await handle.sendUserMessage!('too late', { uuid: 'msg-late' })).toBe(false);
+ expect(mocks.steer).not.toHaveBeenCalled();
+ });
+
/** @example Unsupported approval policy is refused before executing any native request. */
it('does not downgrade an unsupported native permission policy', async () => {
/** @example The Full access-only base must reject Ask until its permission bridge is available. */
diff --git a/packages/heterogeneous-agents/src/codex/codexAgentHandle.ts b/packages/heterogeneous-agents/src/codex/codexAgentHandle.ts
index 8dde81683a6..23b78298ef2 100644
--- a/packages/heterogeneous-agents/src/codex/codexAgentHandle.ts
+++ b/packages/heterogeneous-agents/src/codex/codexAgentHandle.ts
@@ -1,3 +1,4 @@
+import { randomUUID } from 'node:crypto';
import { PassThrough } from 'node:stream';
import type { AskUserBridge } from '../askUser';
@@ -90,6 +91,13 @@ export const createCodexAgentHandle = async (
// The session ends a turn lost to a transport crash with an interrupted runtime end and
// resolves; without a Stop from this operation that is a truncated run, not a success.
let nativeTurnInterrupted = false;
+ // `turn/steer` names the turn `turn/start` answered: a follow-up arriving before
+ // that waits for it, and one arriving after the run ended is left for the next run.
+ let markTurnStarted!: () => void;
+ const turnStarted = new Promise<void>((resolve) => {
+ markTurnStarted = resolve;
+ });
+ let runEnded = false;
const provenance = pickCodexRunProvenance({
...options.env,
LOBEHUB_OPERATION_ID: options.operationId,
@@ -130,7 +138,9 @@ export const createCodexAgentHandle = async (
}
events.push(batch);
},
- onRuntimeStatus: () => {},
+ onRuntimeStatus: (status) => {
+ if (status.state === 'running') markTurnStarted();
+ },
onSessionId: (id) => {
nativeSessionId = id;
},
@@ -171,6 +181,7 @@ export const createCodexAgentHandle = async (
},
)
.finally(() => {
+ runEnded = true;
// Each CLI operation owns its app-server process; closing it leaves the native history on disk.
session.close();
client.close();
@@ -189,6 +200,22 @@ export const createCodexAgentHandle = async (
});
},
pid: undefined,
+ /**
+ * Add a follow-up to the running turn (`turn/steer`). The app-server echoes it
+ * as a user message carrying `uuid`, which confirms the delivery. Resolves
+ * false when the turn takes no input (it ended, or a review or compaction
+ * turn), so the caller sends the message as the next run instead.
+ */
+ sendUserMessage: async (prompt: AgentPromptInput, sendOptions?: { uuid?: string }) => {
+ const started = await Promise.race([turnStarted.then(() => true), exit.then(() => false)]);
+ if (!started || runEnded) return false;
+ const plan = await buildAgentInput('codex', prompt);
+ const outcome = await session.steer(
+ buildCodexAppServerInput(plan),
+ sendOptions?.uuid ?? randomUUID(),
+ );
+ return outcome === 'accepted';
+ },
get sessionId() {
return nativeSessionId;
},
diff --git a/packages/heterogeneous-agents/src/spawn/spawnAgent.ts b/packages/heterogeneous-agents/src/spawn/spawnAgent.ts
index a62800c1ff8..e6fb0d8364b 100644
--- a/packages/heterogeneous-agents/src/spawn/spawnAgent.ts
+++ b/packages/heterogeneous-agents/src/spawn/spawnAgent.ts
@@ -162,10 +162,11 @@ export interface SpawnAgentHandle {
request: ClaudeCodeControlRequest,
) => ClaudeCodeControlRequestHandle | undefined;
/**
- * (Claude Code `liveInput` only) Deliver a user message into the running
- * session; Claude Code reads it at its next tool boundary, or starts a turn
- * for it when idle. Resolves false once input has closed β€” the caller then
- * runs the message as a new turn instead.
+ * (Claude Code `liveInput`, native Codex) Deliver a user message into the
+ * running session; Claude Code reads it at its next tool boundary, or starts
+ * a turn for it when idle; Codex adds it to the active turn (`turn/steer`).
+ * Resolves false once the session takes no input β€” the caller then runs the
+ * message as a new turn instead.
*/
sendUserMessage?: (prompt: AgentPromptInput, options?: { uuid?: string }) => Promise<boolean>;
/**