Skip to content

Commit 990d42c

Browse files
committed
fix(gateway): isolate live model session scopes
1 parent e33606e commit 990d42c

2 files changed

Lines changed: 73 additions & 25 deletions

File tree

‎src/gateway/gateway-models.profiles.live.test.ts‎

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { getFreePort, isPortFree } from "../test-utils/ports.js";
8282
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
8383
import { GatewayClient } from "./client.js";
8484
import { restoreLiveEnv, snapshotLiveEnv } from "./live-env-test-helpers.js";
85+
import { READ_SCOPE, WRITE_SCOPE } from "./operator-scopes.js";
8586
import type { GatewayServer } from "./server-public.js";
8687

8788
type ProviderThinkingModelCompat = {
@@ -151,6 +152,7 @@ const GATEWAY_LIVE_STRIP_SCAFFOLDING_MODEL_KEYS = new Set([
151152
"openai/gpt-5.4-pro",
152153
]);
153154
const GATEWAY_LIVE_AGENT_ID = "dev";
155+
const GATEWAY_LIVE_OPERATOR_SCOPES = [READ_SCOPE, WRITE_SCOPE];
154156
const GATEWAY_LIVE_CONFIG_TEST_WORKSPACE = path.join(os.tmpdir(), "openclaw-live-config-test");
155157
const GATEWAY_LIVE_CONFIG_TEST_AGENT_DIR = path.join(
156158
os.tmpdir(),
@@ -2820,6 +2822,63 @@ function sanitizeAuthProfileStoreForLiveGateway(store: AuthProfileStore): AuthPr
28202822
};
28212823
}
28222824

2825+
function createGatewayLiveModelSession(params: {
2826+
agentId: string;
2827+
credentialAttempt: number;
2828+
label: string;
2829+
modelIndex: number;
2830+
modelKey: string;
2831+
thinkingLevel?: string;
2832+
}) {
2833+
const key = `agent:${params.agentId}:${params.label}:model-${params.modelIndex + 1}:attempt-${params.credentialAttempt + 1}`;
2834+
return {
2835+
key,
2836+
// Session creation accepts thinking selection at write scope; patching it requires admin.
2837+
method: params.thinkingLevel ? ("sessions.create" as const) : ("sessions.patch" as const),
2838+
request: {
2839+
key,
2840+
model: params.modelKey,
2841+
...(params.thinkingLevel ? { thinkingLevel: params.thinkingLevel } : {}),
2842+
},
2843+
};
2844+
}
2845+
2846+
describe("gateway live model session policy", () => {
2847+
const modelSession = (credentialAttempt: number, thinkingLevel?: string) =>
2848+
createGatewayLiveModelSession({
2849+
agentId: GATEWAY_LIVE_AGENT_ID,
2850+
credentialAttempt,
2851+
label: "all-models",
2852+
modelIndex: 0,
2853+
modelKey: "openai/gpt-5.6-luna",
2854+
thinkingLevel,
2855+
});
2856+
2857+
it("requests only read and write operator scopes", () => {
2858+
expect(GATEWAY_LIVE_OPERATOR_SCOPES).toEqual([READ_SCOPE, WRITE_SCOPE]);
2859+
});
2860+
2861+
it("isolates every model credential retry in a fresh session", () => {
2862+
const first = modelSession(0);
2863+
const retry = modelSession(1);
2864+
2865+
expect(first.key).not.toBe(retry.key);
2866+
expect(first.request).toMatchObject({ key: first.key, model: "openai/gpt-5.6-luna" });
2867+
expect(retry.request).toMatchObject({ key: retry.key, model: "openai/gpt-5.6-luna" });
2868+
});
2869+
2870+
it("initializes explicit thinking levels without an admin-scoped session patch", () => {
2871+
const session = modelSession(0, "ultra");
2872+
2873+
expect(session.method).toBe("sessions.create");
2874+
expect(session.request).toMatchObject({
2875+
key: session.key,
2876+
model: "openai/gpt-5.6-luna",
2877+
thinkingLevel: "ultra",
2878+
});
2879+
});
2880+
});
2881+
28232882
async function connectClient(params: { url: string; token: string; timeoutMs?: number }) {
28242883
const timeoutMs = params.timeoutMs ?? GATEWAY_LIVE_PROBE_TIMEOUT_MS;
28252884
const startedAt = Date.now();
@@ -2872,6 +2931,7 @@ async function connectClientOnce(params: { url: string; token: string; timeoutMs
28722931
const client: GatewayClient | undefined = new GatewayClient({
28732932
url: params.url,
28742933
token: params.token,
2934+
scopes: GATEWAY_LIVE_OPERATOR_SCOPES,
28752935
requestTimeoutMs: Math.max(timeoutMs, GATEWAY_LIVE_MODEL_TIMEOUT_MS),
28762936
connectChallengeTimeoutMs: timeoutMs,
28772937
clientName: GATEWAY_CLIENT_NAMES.TEST,
@@ -4835,37 +4895,28 @@ async function runGatewayModelSuite(params: GatewayModelSuiteParams) {
48354895
if (thinkingLevel !== params.thinkingLevel) {
48364896
logProgress(`${progressLabel}: thinking ${params.thinkingLevel} -> ${thinkingLevel}`);
48374897
}
4838-
// Use a separate session per model: live providers can finalize late after
4839-
// skip/retry paths, and a reset on a reused key does not isolate those
4840-
// delayed transcript writes from the next model probe.
4841-
const sessionKey = `agent:${agentId}:${params.label}:model-${index + 1}`;
4842-
48434898
const attemptMax =
48444899
model.provider === "anthropic" && anthropicKeys.length > 0 ? anthropicKeys.length : 1;
48454900

48464901
for (let attempt = 0; attempt < attemptMax; attempt += 1) {
4902+
const session = createGatewayLiveModelSession({
4903+
agentId,
4904+
credentialAttempt: attempt,
4905+
label: params.label,
4906+
modelIndex: index,
4907+
modelKey,
4908+
...(strictUltraProof ? { thinkingLevel } : {}),
4909+
});
4910+
const sessionKey = session.key;
48474911
if (model.provider === "anthropic" && anthropicKeys.length > 0) {
48484912
process.env.ANTHROPIC_API_KEY = anthropicKeys[attempt];
48494913
}
48504914
try {
48514915
const modelResult = await withGatewayLiveModelTimeout<"done" | "skip">(
48524916
(async () => {
4853-
// Ensure session exists + override model for this run.
4854-
// Reset between models: avoids cross-provider transcript incompatibilities
4855-
// (notably OpenAI Responses requiring reasoning replay for function_call items).
48564917
await withGatewayLiveSessionControlTimeout(
4857-
client.request("sessions.reset", {
4858-
key: sessionKey,
4859-
}),
4860-
`${progressLabel}: sessions-reset`,
4861-
);
4862-
await withGatewayLiveSessionControlTimeout(
4863-
client.request("sessions.patch", {
4864-
key: sessionKey,
4865-
model: modelKey,
4866-
...(strictUltraProof ? { thinkingLevel } : {}),
4867-
}),
4868-
`${progressLabel}: sessions-patch`,
4918+
client.request(session.method, session.request),
4919+
`${progressLabel}: ${session.method}`,
48694920
);
48704921
if (strictUltraProof) {
48714922
await assertGatewayLiveSessionSelection({

‎src/gateway/server-methods/sessions-mutations.sticky-model.test.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,16 @@ describe("sessions.patch sticky model persistence", () => {
229229
).toHaveLength(0);
230230
});
231231

232-
it("keeps a write-scoped model switch session-only without persisting the configured default", async () => {
232+
it("creates a write-scoped model session without persisting the configured default", async () => {
233233
const sessionKey = "agent:main:dm:non-admin";
234-
await upsertSessionEntryCore(
235-
{ agentId: "main", sessionKey },
236-
{ sessionId: "session-non-admin", updatedAt: 1 },
237-
);
238234

239235
const response = await patchSession({ key: sessionKey, model: "openai/gpt-5.6-sol" }, [
240236
"operator.write",
241237
]);
242238

243239
expect(response[0]).toBe(true);
244240
expect(loadSessionEntry({ agentId: "main", sessionKey })).toMatchObject({
241+
sessionId: expect.any(String),
245242
providerOverride: "openai",
246243
modelOverride: "gpt-5.6-sol",
247244
});

0 commit comments

Comments
 (0)