Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(acp): confirm the memory one-shot's model switch; fall back to the session default for an unroutable model

AdityaUmalef1a88a6290ca
diff --git a/server/drivers/acp/background-text.test.ts b/server/drivers/acp/background-text.test.ts
index b1adf50d88..68d708ed08 100644
--- a/server/drivers/acp/background-text.test.ts
+++ b/server/drivers/acp/background-text.test.ts
@@ -16,7 +16,7 @@ import { GrokAgentDriver } from "./grok.ts";
import { QwenAgentDriver } from "./qwen.ts";
const FAKE_CLI = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "testing", "fake-acp-cli.ts");
-const ENV_KEYS = ["FAKE_ACP_MODE", "FAKE_ACP_DUMP", "FAKE_ACP_TEXT_REPLY", "FAKE_ACP_MODELS"];
+const ENV_KEYS = ["FAKE_ACP_MODE", "FAKE_ACP_DUMP", "FAKE_ACP_TEXT_REPLY", "FAKE_ACP_MODELS", "FAKE_ACP_MODEL_STICKS"];
describe("ACP memory one-shot (generateMemoryText)", () => {
let instance: ProviderInstance | undefined;
@@ -152,6 +152,19 @@ describe("ACP memory one-shot (generateMemoryText)", () => {
await (await create(createAcpDriver(support))).generateMemoryText!("prompt", { fallback: botModel("m-gone") });
expect(promptedModels()).toEqual(["m-default"]);
});
+
+ it("stops before the prompt when the session stays on another model after the switch", async () => {
+ process.env.FAKE_ACP_MODEL_STICKS = "1";
+ const engine = await create(createAcpDriver(support));
+ await expect(engine.generateMemoryText!("prompt", { fallback: botModel("m-bot") })).rejects.toThrow("did not switch to m-bot (still m-default)");
+ expect(existsSync(`${process.env.FAKE_ACP_DUMP}.prompts.jsonl`)).toBe(false);
+ });
+
+ it("keeps the session's default when the engine can no longer route the bot's model", async () => {
+ const resolveTurnModel = () => { throw new Error("model is no longer configured"); };
+ await (await create(createAcpDriver({ ...support, resolveTurnModel }))).generateMemoryText!("prompt", { fallback: botModel("m-bot") });
+ expect(promptedModels()).toEqual(["m-default"]);
+ });
});
it("is absent on an engine that opts out", async () => {
diff --git a/server/drivers/acp/background-text.ts b/server/drivers/acp/background-text.ts
index c1c7a8c733..289164b071 100644
--- a/server/drivers/acp/background-text.ts
+++ b/server/drivers/acp/background-text.ts
@@ -21,12 +21,27 @@ export function applyOneShotEnv(
const selection = support.selectModel;
const requestedModel = selection ? options.model ?? options.fallback?.model : undefined;
const model = requestedModel && requestedModel !== selection?.agentDefault
- ? support.resolveTurnModel?.(requestedModel, env, models) ?? requestedModel
+ ? resolveOneShotModel(support, requestedModel, env, models)
: undefined;
support.applyTurnEnv?.(env, { ...(model ? { model, requestedModel } : {}), ...ctx });
return model && selection ? { configId: selection.configId, id: model } : undefined;
}
+/** A model the engine can no longer route (e.g. a deleted Qwen provider entry) leaves the session's default to run, as an
+ * unoffered one does. */
+function resolveOneShotModel(
+ support: Pick<AcpSupport, "resolveTurnModel">,
+ requestedModel: string,
+ env: Record<string, string | undefined>,
+ models: ModelCatalog,
+): string | undefined {
+ try {
+ return support.resolveTurnModel?.(requestedModel, env, models) ?? requestedModel;
+ } catch {
+ return undefined;
+ }
+}
+
export interface AcpOneShotInput {
displayName: string;
spawn: typeof spawnCli;
@@ -195,7 +210,10 @@ export function runAcpOneShot(input: AcpOneShotInput): Promise<string> {
const offered = wanted ? acpModelOption(session, wanted.configId) : undefined;
if (wanted && offered?.configId && offered.options.some(({ id }) => id === wanted.id)) {
if (offered.currentValue !== wanted.id) {
- await request("session/set_config_option", { sessionId, configId: offered.configId, value: wanted.id });
+ const reply = await request("session/set_config_option", { sessionId, configId: offered.configId, value: wanted.id });
+ // Some engines confirm a switch with an empty reply; one that names another model did not switch.
+ const now = acpModelOption(reply, offered.configId)?.currentValue;
+ if (now !== undefined && now !== wanted.id) throw new Error(`${name} background call did not switch to ${wanted.id} (still ${now})`);
}
model = wanted.id;
}