diff --git a/internal/cloud/code-agent-agentrun-adapter.ts b/internal/cloud/code-agent-agentrun-adapter.ts index b07e5119..49d2193b 100644 --- a/internal/cloud/code-agent-agentrun-adapter.ts +++ b/internal/cloud/code-agent-agentrun-adapter.ts @@ -442,7 +442,7 @@ export async function submitAgentRunChatTurn({ params = {}, options = {}, traceI }, backendProfile)); continue; } - const profileThreadId = agentRunSessionThreadIdFromEnsure(ensuredSession); + const profileThreadId = agentRunSessionThreadIdFromEnsure(ensuredSession, backendProfile); const dispatchParams = paramsWithAgentRunSessionThread(attemptParams, profileThreadId); if (safeOpaqueId(attemptParams.threadId) && !profileThreadId) { traceStore.append(traceId, agentRunTraceEvent({ @@ -1263,9 +1263,11 @@ function agentRunSessionRecordFromEnsure(value = null) { return session && typeof session === "object" ? session : null; } -function agentRunSessionThreadIdFromEnsure(value = null) { +function agentRunSessionThreadIdFromEnsure(value = null, backendProfile = null) { const record = agentRunSessionRecordFromEnsure(value); if (agentRunSessionThreadInvalidated(record)) return null; + const existingProfile = firstNonEmpty(record?.backendProfile, record?.metadata?.agentRunSessionProfile); + if (existingProfile && backendProfile && existingProfile !== backendProfile) return null; return safeOpaqueId(record?.threadId) || null; } diff --git a/internal/cloud/server-agent-chat.test.ts b/internal/cloud/server-agent-chat.test.ts index 9f4df007..43751887 100644 --- a/internal/cloud/server-agent-chat.test.ts +++ b/internal/cloud/server-agent-chat.test.ts @@ -1881,6 +1881,7 @@ test("cloud api AgentRun adapter keeps AgentRun sessions scoped to HWLAB session if (request.method === "POST" && url.pathname === "/api/v1/runs") { assert.equal(body.backendProfile, "minimax-m3"); assert.equal(body.sessionRef.sessionId, minimaxSessionId); + assert.equal(body.sessionRef.threadId, undefined); assert.equal(body.sessionRef.metadata.hwlabSessionId, hwlabSessionId); assert.equal(body.sessionRef.metadata.agentRunSessionProfile, "minimax-m3"); return send({ id: "run_hwlab_profile_switch_m3", status: "pending", backendProfile: "minimax-m3", sessionRef: body.sessionRef, resourceBundleRef: body.resourceBundleRef }); @@ -1889,6 +1890,7 @@ test("cloud api AgentRun adapter keeps AgentRun sessions scoped to HWLAB session assert.equal(body.payload.providerProfile, "minimax-m3"); assert.equal(body.payload.sessionId, minimaxSessionId); assert.equal(body.payload.hwlabSessionId, hwlabSessionId); + assert.equal(body.payload.threadId, null); return send({ id: "cmd_hwlab_profile_switch_m3", runId: "run_hwlab_profile_switch_m3", state: "pending", type: "turn", seq: 1 }); } if (request.method === "GET" && url.pathname === "/api/v1/runs/run_hwlab_profile_switch_m3/commands") {