Skip to content

Commit 31616ad

Browse files
committed
fix(fx): restore backend and model after failed selection
1 parent 0e58bfc commit 31616ad

3 files changed

Lines changed: 126 additions & 26 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/FxAdapterV2.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,16 @@ describe("FxAdapterV2", () => {
383383
fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
384384
),
385385
]
386-
: []),
386+
: setConfig(
387+
"provider",
388+
"codex",
389+
fxConfig({
390+
backend: "codex",
391+
model: CODEX_MODEL,
392+
models: [CODEX_MODEL],
393+
modes: FX_0_0_13_MODES,
394+
}),
395+
)),
387396
],
388397
}).pipe(Effect.exit);
389398
if (modelAvailable) {

‎packages/provider-fx/src/server/acpSupport.test.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { assert, describe, it } from "@effect/vitest";
2+
import { ProviderInstanceId } from "@t3tools/contracts";
3+
import * as Cause from "effect/Cause";
4+
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
6+
import * as EffectAcpErrors from "effect-acp/errors";
27
import type * as EffectAcpSchema from "effect-acp/compat";
38

49
import { ProviderAdapterV2RuntimePolicy } from "@t3tools/provider-core/server/ProviderAdapter";
510

6-
import { fxPermissionDisposition } from "./acpSupport.ts";
11+
import { applyFxModelSelection, fxPermissionDisposition } from "./acpSupport.ts";
712

813
const editRequest = {
914
sessionId: "fx-session-01",
@@ -31,3 +36,68 @@ describe("fx permission prompts", () => {
3136
);
3237
});
3338
});
39+
40+
describe("fx model selection", () => {
41+
it.effect.each(["unavailable", "rejected"] as const)(
42+
"restores the original backend and model when Default is %s",
43+
(failure) =>
44+
Effect.gen(function* () {
45+
let backend = "codex";
46+
let model = "codex-custom";
47+
const configOptions = (): ReadonlyArray<EffectAcpSchema.SessionConfigOption> => [
48+
{
49+
id: "provider",
50+
name: "provider",
51+
type: "select",
52+
currentValue: backend,
53+
options: ["codex", "gateway"].map((value) => ({ value, name: value })),
54+
},
55+
{
56+
id: "model",
57+
name: "model",
58+
type: "select",
59+
currentValue: model,
60+
options: (backend === "codex"
61+
? ["codex-default", "codex-custom"]
62+
: ["gateway-fallback", ...(failure === "rejected" ? ["gateway-default"] : [])]
63+
).map((value) => ({ value, name: value })),
64+
},
65+
];
66+
const runtime = {
67+
getConfigOptions: Effect.sync(configOptions),
68+
setConfigOption: (id: string, value: string | boolean) =>
69+
Effect.gen(function* () {
70+
if (id === "provider") {
71+
backend = String(value);
72+
model = backend === "codex" ? "codex-default" : "gateway-fallback";
73+
} else if (id === "model") {
74+
if (value === "gateway-default") {
75+
return yield* new EffectAcpErrors.AcpRequestError({
76+
code: -32602,
77+
errorMessage: "Model rejected",
78+
});
79+
}
80+
model = String(value);
81+
}
82+
return { configOptions: configOptions() };
83+
}),
84+
};
85+
const result = yield* applyFxModelSelection(
86+
runtime,
87+
{ instanceId: ProviderInstanceId.make("fx"), model: "default" },
88+
Effect.succeed({ backend: "gateway", model: "gateway-default" }),
89+
).pipe(Effect.exit);
90+
assert.isTrue(Exit.isFailure(result));
91+
if (Exit.isFailure(result)) {
92+
assert.include(
93+
Cause.pretty(result.cause),
94+
failure === "unavailable"
95+
? "The default fx model is unavailable on its configured backend."
96+
: "Model rejected",
97+
);
98+
}
99+
assert.equal(backend, "codex");
100+
assert.equal(model, "codex-custom");
101+
}),
102+
);
103+
});

‎packages/provider-fx/src/server/acpSupport.ts‎

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,17 @@ const selectOption = (
186186

187187
/** fx's model catalog changes with its backend, so it must be read after the provider write. */
188188
export const applyFxModelSelection = Effect.fn("applyFxModelSelection")(function* (
189-
runtime: AcpSessionRuntime.AcpSessionRuntime["Service"],
189+
runtime: Pick<
190+
AcpSessionRuntime.AcpSessionRuntime["Service"],
191+
"getConfigOptions" | "setConfigOption"
192+
>,
190193
modelSelection: ModelSelection,
191194
defaultChoice: Effect.Effect<FxModelChoice | undefined> | undefined,
192195
): Effect.fn.Return<string | undefined, EffectAcpErrors.AcpError> {
193-
const backendOption = selectOption(yield* runtime.getConfigOptions, FX_PROVIDER_CONFIG_ID);
196+
const originalOptions = yield* runtime.getConfigOptions;
197+
const backendOption = selectOption(originalOptions, FX_PROVIDER_CONFIG_ID);
198+
const originalBackend = backendOption?.currentValue;
199+
const originalModel = selectOption(originalOptions, FX_MODEL_CONFIG_ID)?.currentValue;
194200
const requested = modelSelection.model.trim();
195201
const isDefault = requested.length === 0 || requested === FX_DEFAULT_MODEL_SLUG;
196202
const target = isDefault
@@ -204,31 +210,46 @@ export const applyFxModelSelection = Effect.fn("applyFxModelSelection")(function
204210
(backendOption !== undefined &&
205211
(target.backend === backendOption.currentValue ||
206212
collectSessionConfigOptionValues(backendOption).includes(target.backend)));
207-
if (
213+
const changingBackend =
208214
target?.backend !== undefined &&
209215
backendOption !== undefined &&
210216
canSelectBackend &&
211-
target.backend !== backendOption.currentValue
212-
) {
213-
yield* runtime.setConfigOption(FX_PROVIDER_CONFIG_ID, target.backend);
214-
}
215-
const modelOption = selectOption(yield* runtime.getConfigOptions, FX_MODEL_CONFIG_ID);
216-
const current = modelOption?.currentValue;
217-
if (target === undefined || target.model === current) return current;
218-
if (isDefault && !canSelectBackend) return current;
219-
if (
220-
isDefault &&
221-
(modelOption === undefined ||
222-
!collectSessionConfigOptionValues(modelOption).includes(target.model))
223-
) {
224-
return yield* new EffectAcpErrors.AcpRequestError({
225-
code: -32602,
226-
errorMessage: "The default fx model is unavailable on its configured backend.",
227-
data: { configId: FX_MODEL_CONFIG_ID },
228-
});
229-
}
230-
yield* runtime.setConfigOption(FX_MODEL_CONFIG_ID, target.model, { allowUnlistedValue: true });
231-
return target.model;
217+
target.backend !== backendOption.currentValue;
218+
return yield* Effect.gen(function* () {
219+
if (changingBackend && target?.backend !== undefined) {
220+
yield* runtime.setConfigOption(FX_PROVIDER_CONFIG_ID, target.backend);
221+
}
222+
const modelOption = selectOption(yield* runtime.getConfigOptions, FX_MODEL_CONFIG_ID);
223+
const current = modelOption?.currentValue;
224+
if (target === undefined || target.model === current) return current;
225+
if (isDefault && !canSelectBackend) return current;
226+
if (
227+
isDefault &&
228+
(modelOption === undefined ||
229+
!collectSessionConfigOptionValues(modelOption).includes(target.model))
230+
) {
231+
return yield* new EffectAcpErrors.AcpRequestError({
232+
code: -32602,
233+
errorMessage: "The default fx model is unavailable on its configured backend.",
234+
data: { configId: FX_MODEL_CONFIG_ID },
235+
});
236+
}
237+
yield* runtime.setConfigOption(FX_MODEL_CONFIG_ID, target.model, { allowUnlistedValue: true });
238+
return target.model;
239+
}).pipe(
240+
Effect.onError(() => {
241+
if (!changingBackend || originalBackend === undefined) return Effect.void;
242+
return Effect.gen(function* () {
243+
yield* runtime.setConfigOption(FX_PROVIDER_CONFIG_ID, originalBackend);
244+
const restoredModel = selectOption(yield* runtime.getConfigOptions, FX_MODEL_CONFIG_ID);
245+
if (originalModel !== undefined && restoredModel?.currentValue !== originalModel) {
246+
yield* runtime.setConfigOption(FX_MODEL_CONFIG_ID, originalModel, {
247+
allowUnlistedValue: true,
248+
});
249+
}
250+
}).pipe(Effect.ignoreCause({ log: true }));
251+
}),
252+
);
232253
});
233254

234255
/** The shared option loop writes a stored effort; a thread without one starts at medium. */

0 commit comments

Comments
 (0)