Skip to content

Commit 0e58bfc

Browse files
committed
fix(fx): reject Default when its model is unavailable
1 parent a4dbade commit 0e58bfc

2 files changed

Lines changed: 67 additions & 40 deletions

File tree

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

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
2-
import { describe, it } from "@effect/vitest";
2+
import { assert, describe, it } from "@effect/vitest";
33
import {
44
MessageId,
55
NodeId,
@@ -20,7 +20,9 @@ import { ProviderAdapterV2RuntimePolicy } from "@t3tools/provider-core/server/Pr
2020
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
2121
import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
2222
import * as DateTime from "effect/DateTime";
23+
import * as Cause from "effect/Cause";
2324
import * as Effect from "effect/Effect";
25+
import * as Exit from "effect/Exit";
2426
import * as FileSystem from "effect/FileSystem";
2527
import * as Layer from "effect/Layer";
2628
import * as Path from "effect/Path";
@@ -339,36 +341,62 @@ describe("FxAdapterV2", () => {
339341
// `fx status` names a gateway model, but this loaded session runs on Codex,
340342
// whose catalog lacks it: the backend goes back first.
341343
it.effect("moves a loaded session on another backend back to fx's default backend", () =>
342-
openFxSession({
343-
scenario: "default-other-backend",
344-
runtimeMode: "approval-required",
345-
model: "default",
346-
loadSessionId: SESSION_ID,
347-
frames: [
348-
...initialize,
349-
outbound("session/load"),
350-
answer(
351-
"session/load",
352-
fxSession({
353-
model: CODEX_MODEL,
354-
backend: "codex",
355-
models: [CODEX_MODEL],
356-
modes: FX_0_0_13_MODES,
357-
}),
358-
),
359-
// fx then runs the gateway model it saved for that backend.
360-
...setConfig(
361-
"provider",
362-
"gateway",
363-
fxConfig({ model: NAMED_MODEL, modes: FX_0_0_13_MODES }),
364-
),
365-
...setConfig(
366-
"model",
367-
FX_DEFAULT_MODEL,
368-
fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
369-
),
370-
...setConfig("mode", "ask", fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES })),
371-
],
344+
Effect.gen(function* () {
345+
for (const modelAvailable of [true, false]) {
346+
const result = yield* openFxSession({
347+
scenario: `default-other-backend-${modelAvailable}`,
348+
runtimeMode: "approval-required",
349+
model: "default",
350+
loadSessionId: SESSION_ID,
351+
frames: [
352+
...initialize,
353+
outbound("session/load"),
354+
answer(
355+
"session/load",
356+
fxSession({
357+
model: CODEX_MODEL,
358+
backend: "codex",
359+
models: [CODEX_MODEL],
360+
modes: FX_0_0_13_MODES,
361+
}),
362+
),
363+
// fx then runs the gateway model it saved for that backend.
364+
...setConfig(
365+
"provider",
366+
"gateway",
367+
fxConfig({
368+
model: NAMED_MODEL,
369+
...(modelAvailable ? {} : { models: [NAMED_MODEL] }),
370+
modes: FX_0_0_13_MODES,
371+
}),
372+
),
373+
...(modelAvailable
374+
? [
375+
...setConfig(
376+
"model",
377+
FX_DEFAULT_MODEL,
378+
fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
379+
),
380+
...setConfig(
381+
"mode",
382+
"ask",
383+
fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
384+
),
385+
]
386+
: []),
387+
],
388+
}).pipe(Effect.exit);
389+
if (modelAvailable) {
390+
assert.isTrue(Exit.isSuccess(result));
391+
} else if (Exit.isFailure(result)) {
392+
assert.include(
393+
Cause.pretty(result.cause),
394+
"The default fx model is unavailable on its configured backend.",
395+
);
396+
} else {
397+
assert.fail("Expected Default selection to reject the unavailable model");
398+
}
399+
}
372400
}).pipe(Effect.provide(testLayer), Effect.scoped),
373401
);
374402

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,7 @@ const selectOption = (
184184
return option?.type === "select" ? option : undefined;
185185
};
186186

187-
/**
188-
* Writes fx's session-only `provider` (the backend), then `model`. "Default"
189-
* is what `fx status` reports for the session's folder, skipped when that
190-
* backend does not list it; any other unlisted model is still sent, so a
191-
* thread never runs on a model it did not pick.
192-
*/
187+
/** fx's model catalog changes with its backend, so it must be read after the provider write. */
193188
export const applyFxModelSelection = Effect.fn("applyFxModelSelection")(function* (
194189
runtime: AcpSessionRuntime.AcpSessionRuntime["Service"],
195190
modelSelection: ModelSelection,
@@ -220,13 +215,17 @@ export const applyFxModelSelection = Effect.fn("applyFxModelSelection")(function
220215
const modelOption = selectOption(yield* runtime.getConfigOptions, FX_MODEL_CONFIG_ID);
221216
const current = modelOption?.currentValue;
222217
if (target === undefined || target.model === current) return current;
218+
if (isDefault && !canSelectBackend) return current;
223219
if (
224220
isDefault &&
225-
(!canSelectBackend ||
226-
modelOption === undefined ||
221+
(modelOption === undefined ||
227222
!collectSessionConfigOptionValues(modelOption).includes(target.model))
228223
) {
229-
return current;
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+
});
230229
}
231230
yield* runtime.setConfigOption(FX_MODEL_CONFIG_ID, target.model, { allowUnlistedValue: true });
232231
return target.model;

0 commit comments

Comments
 (0)