Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

Commit 99ebc97

Browse files
authored
fix(cloud-agent): propagate taskRunId through codex loadSession (#1720)
## Problem `newSession` and `unstable_resumeSession` both carry `_meta.taskRunId` / `taskId` into `sessionState` and emit `_posthog/sdk_session` on cloud runs. `loadSession` dropped them on the floor. Re-attaching a desktop client to an existing cloud codex session therefore silenced `TURN_COMPLETE` and `USAGE_UPDATE` on subsequent prompts (both gated on `sessionState.taskRunId`), and the adapter→session mapping was never published. ## Changes In `CodexAcpAgent.loadSession`, propagate `taskRunId` / `taskId` into sessionState and fire `_posthog/sdk_session` when `taskRunId` is present, matching the other session-creation paths. ## How did you test this? - Two new unit tests: `taskRunId` is propagated and `SDK_SESSION` fires when present; no extNotification is emitted when `taskRunId` is absent. - Full `@posthog/agent` suite green.
1 parent 9f31016 commit 99ebc97

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎packages/agent/src/adapters/codex/codex-agent.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,48 @@ describe("CodexAcpAgent", () => {
107107
).toBe("read-only");
108108
});
109109

110+
it("propagates taskRunId and fires SDK_SESSION when loading a cloud session", async () => {
111+
const { agent, client } = createAgent();
112+
mockCodexConnection.loadSession.mockResolvedValue({
113+
modes: { currentModeId: "auto", availableModes: [] },
114+
configOptions: [],
115+
} satisfies Partial<LoadSessionResponse>);
116+
117+
await agent.loadSession({
118+
sessionId: "session-1",
119+
cwd: process.cwd(),
120+
_meta: { taskRunId: "run-1", taskId: "task-1" },
121+
} as never);
122+
123+
expect(
124+
(agent as unknown as { sessionState: { taskRunId?: string } })
125+
.sessionState.taskRunId,
126+
).toBe("run-1");
127+
expect(client.extNotification).toHaveBeenCalledWith(
128+
"_posthog/sdk_session",
129+
{
130+
taskRunId: "run-1",
131+
sessionId: "session-1",
132+
adapter: "codex",
133+
},
134+
);
135+
});
136+
137+
it("does not emit SDK_SESSION on loadSession when taskRunId is absent", async () => {
138+
const { agent, client } = createAgent();
139+
mockCodexConnection.loadSession.mockResolvedValue({
140+
modes: { currentModeId: "auto", availableModes: [] },
141+
configOptions: [],
142+
} satisfies Partial<LoadSessionResponse>);
143+
144+
await agent.loadSession({
145+
sessionId: "session-1",
146+
cwd: process.cwd(),
147+
} as never);
148+
149+
expect(client.extNotification).not.toHaveBeenCalled();
150+
});
151+
110152
it("preserves the live session mode when loading an existing session", async () => {
111153
const { agent } = createAgent();
112154
mockCodexConnection.loadSession.mockResolvedValue({

‎packages/agent/src/adapters/codex/codex-agent.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,27 @@ export class CodexAcpAgent extends BaseAcpAgent {
267267
meta?.permissionMode,
268268
);
269269

270+
// Carry taskRunId/taskId across load so prompt() still emits cloud
271+
// notifications (TURN_COMPLETE, USAGE_UPDATE) after a reload. newSession
272+
// and unstable_resumeSession both do this; loadSession historically did
273+
// not, which silently broke task-completion tracking on re-attach.
270274
this.sessionState = createSessionState(params.sessionId, params.cwd, {
275+
taskRunId: meta?.taskRunId,
276+
taskId: meta?.taskId ?? meta?.persistence?.taskId,
271277
modeId: response.modes?.currentModeId ?? "auto",
272278
permissionMode: currentPermissionMode,
273279
});
274280
this.sessionId = params.sessionId;
275281
this.sessionState.configOptions = response.configOptions ?? [];
276282

283+
if (meta?.taskRunId) {
284+
await this.client.extNotification(POSTHOG_NOTIFICATIONS.SDK_SESSION, {
285+
taskRunId: meta.taskRunId,
286+
sessionId: params.sessionId,
287+
adapter: "codex",
288+
});
289+
}
290+
277291
return response;
278292
}
279293

0 commit comments

Comments
 (0)