fix(agents): guard untagged settings edits from switchAgentProvider entry

This commit is contained in:
Mohamed Boudra
2026-11-26 02:49:20 +01:00
parent 4cc532cb32
commit f4abc198f8
2 changed files with 48 additions and 7 deletions
@@ -1182,7 +1182,7 @@ export class AgentManager {
{ readonly openedAt: number }
>();
private readonly collectingRuntimes = new Set<string>();
private readonly switchingAgentIds = new Set<string>();
private readonly switchingAgentCounts = new Map<string, number>();
private readonly lifecycleMutationTails = new Map<string, Promise<void>>();
private readonly pendingCancellations = new Map<string, Promise<unknown>>();
private readonly pendingSubmittedPrompts = new Map<
@@ -2368,10 +2368,21 @@ export class AgentManager {
input: SwitchAgentProviderInput,
): Promise<SwitchAgentProviderResult> {
const resolvedAgentId = validateAgentId(agentId, "switchAgentProvider");
this.switchingAgentCounts.set(
resolvedAgentId,
(this.switchingAgentCounts.get(resolvedAgentId) ?? 0) + 1,
);
return this.trackAgentRegistrationOperation(
this.runLifecycleMutation(resolvedAgentId, () =>
this.switchAgentProviderUnlocked(resolvedAgentId, input),
),
).finally(() => {
const remaining = (this.switchingAgentCounts.get(resolvedAgentId) ?? 1) - 1;
if (remaining <= 0) {
this.switchingAgentCounts.delete(resolvedAgentId);
} else {
this.switchingAgentCounts.set(resolvedAgentId, remaining);
}
}),
);
}
@@ -2692,7 +2703,6 @@ export class AgentManager {
};
try {
this.switchingAgentIds.add(agentId);
this.paseoToolPolicies.set(agentId, paseoToolPolicy);
const launchContext = await this.buildLaunchContext(
agentId,
@@ -2814,7 +2824,6 @@ export class AgentManager {
}
throw error;
} finally {
this.switchingAgentIds.delete(agentId);
releaseSwitchRunLock();
if (!handedToRegistration) {
this.restoreToolPolicyOnSwitchFailure(
@@ -3505,7 +3514,7 @@ export class AgentManager {
agentId: string,
options?: { expectedProvider?: AgentProvider },
): void {
if (!options?.expectedProvider && this.switchingAgentIds.has(agentId)) {
if (!options?.expectedProvider && this.switchingAgentCounts.has(agentId)) {
throw new Error(
`Cannot update settings while agent ${agentId} is switching providers without specifying expectedProvider`,
);
@@ -143,6 +143,8 @@ class E2EFakeClient implements AgentClient {
readonly sessions: E2EFakeSession[] = [];
failNextCreate: Error | null = null;
failNextTurn: Error | null = null;
availabilityGate: Promise<void> | null = null;
onAvailabilityCheckStarted: (() => void) | null = null;
createSessionGate: Promise<void> | null = null;
onCreateSessionStarted: (() => void) | null = null;
turnCompletionGate: Promise<void> | null = null;
@@ -154,6 +156,12 @@ class E2EFakeClient implements AgentClient {
) {}
async isAvailable(): Promise<boolean> {
this.onAvailabilityCheckStarted?.();
if (this.availabilityGate) {
const gate = this.availabilityGate;
this.availabilityGate = null;
await gate;
}
return true;
}
async fetchCatalog() {
@@ -679,6 +687,16 @@ describe("Provider switching WebSocket RPC, receipts, and capability gating", ()
});
await new Promise((resolve) => setTimeout(resolve, 100));
let resolveCodexAvail!: () => void;
codex.availabilityGate = new Promise<void>((resolve) => {
resolveCodexAvail = resolve;
});
let resolveCodexAvailStarted!: () => void;
const codexAvailStarted = new Promise<void>((resolve) => {
resolveCodexAvailStarted = resolve;
});
codex.onAvailabilityCheckStarted = resolveCodexAvailStarted;
let resolveCodexCreate!: () => void;
codex.createSessionGate = new Promise<void>((resolve) => {
resolveCodexCreate = resolve;
@@ -694,6 +712,15 @@ describe("Provider switching WebSocket RPC, receipts, and capability gating", ()
text: "Switch to Codex",
messageId: "msg-ep-sw",
});
await codexAvailStarted;
// An older client omitting expectedProvider during the availability check MUST also reject.
const legacyDuringAvail = client2.client.setAgentModel(agent.id, "claude-opus-avail").then(
() => ({ status: "fulfilled" as const }),
(error: unknown) => ({ status: "rejected" as const, error }),
);
await new Promise((resolve) => setTimeout(resolve, 30));
resolveCodexAvail();
await codexCreateStarted;
// Both a current client sending expectedProvider: "claude" AND an older client omitting
@@ -711,14 +738,19 @@ describe("Provider switching WebSocket RPC, receipts, and capability gating", ()
await new Promise((resolve) => setTimeout(resolve, 30));
resolveCodexCreate();
const [switched, staleEditOutcome, legacyEditOutcome] = await Promise.all([
const [switched, availEditOutcome, staleEditOutcome, legacyEditOutcome] = await Promise.all([
switchPromise,
legacyDuringAvail,
staleModelEdit,
legacyModelEdit,
]);
expect(switched.agent?.provider).toBe("codex");
// Both stale Claude model edits MUST reject instead of applying to Codex!
// All three stale Claude model edits MUST reject instead of applying to Codex!
expect(availEditOutcome.status).toBe("rejected");
if (availEditOutcome.status === "rejected") {
expect(String(availEditOutcome.error)).toMatch(/switching providers/i);
}
expect(staleEditOutcome.status).toBe("rejected");
if (staleEditOutcome.status === "rejected") {
expect(String(staleEditOutcome.error)).toMatch(/provider 'claude'.*now using 'codex'/i);