Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/acp/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@ type Prepared =
};

/** The logical name a routed value carries, when it is one. */
/**
* The runtime member a call carries when its use names a model.
*
* Omitted for an unconfigured or effort-only use.
*/
function modelRequest(configuration: SessionConfiguration | undefined): {
requestedModel?: string;
} {
return configuration?.model === undefined ? {} : { requestedModel: configuration.model };
}

function asName(routed: string | Session | undefined): string | undefined {
return typeof routed === "string" ? routed : undefined;
}
Expand Down Expand Up @@ -1803,6 +1814,11 @@ function* useAcpxProviderState(
* acceptance rather than after this ensure.
*/
deferEstablished?: boolean;
/**
* The model requested by the use this ensure belongs to, for the reconnect
* this ensure itself may perform. Never kept on the entry it returns.
*/
requestedModel?: string;
}

function* ensureFromPrepared(
Expand Down Expand Up @@ -1840,6 +1856,7 @@ function* useAcpxProviderState(
...(intent.materialization === true
? { materialization: "first-turn-acceptance" as const }
: {}),
...(intent.requestedModel === undefined ? {} : { requestedModel: intent.requestedModel }),
},
(handle, entry) => {
// The exact value this provider already issued, when it issued one.
Expand Down Expand Up @@ -2611,6 +2628,7 @@ function* useAcpxProviderState(
agentName: string,
entry: ManagedSession,
requireWrite: boolean,
configuration?: SessionConfiguration,
): ConfigurationTarget {
const runtime = entry.runtime.runtime;
const readStatus = runtime.getStatus;
Expand Down Expand Up @@ -2664,6 +2682,9 @@ function* useAcpxProviderState(
handle: entry.handle,
key: optionId,
value,
// The same attempt's requested model for ordinary and restoring
// writes alike: either may be the one that reconnects.
...modelRequest(configuration),
})
.then(() => undefined);
yield* until(pending);
Expand Down Expand Up @@ -2718,7 +2739,7 @@ function* useAcpxProviderState(
configuration: SessionConfiguration,
): Operation<SessionConfiguration> {
const selectors = yield* applySessionConfiguration(
configurationTarget(agentName, entry, true),
configurationTarget(agentName, entry, true, configuration),
configuration,
);
return {
Expand Down Expand Up @@ -2896,6 +2917,7 @@ function* useAcpxProviderState(
...(construction === undefined ? {} : { build: construction.build }),
...(resumeSessionId === undefined ? {} : { attachment: { resumeSessionId } }),
state,
...modelRequest(desired),
materialization:
constructing && adapterFor(agentName)?.acceptance === "first-turn-acceptance",
deferEstablished:
Expand Down Expand Up @@ -2951,6 +2973,7 @@ function* useAcpxProviderState(
mode: "prompt",
requestId,
timeoutMs,
...modelRequest(desired),
...(callbacks === undefined
? {}
: {
Expand Down Expand Up @@ -3643,6 +3666,7 @@ function* useAcpxProviderState(
...(bound === undefined ? {} : { build: bound.build }),
attachment: { resumeSessionId: prepared.nativeSessionId },
state: "existing",
...modelRequest(requested),
});
} catch (error) {
if (error instanceof AttachmentRefused) {
Expand Down Expand Up @@ -3812,6 +3836,7 @@ function* useAcpxProviderState(
text: plan.prompt,
mode: "prompt",
requestId: plan.requestId,
...modelRequest(retainedConfiguration(prepared)),
});
activeTurns.add(turn);

Expand Down Expand Up @@ -4647,6 +4672,9 @@ function* useAcpxProviderState(
...(construction === undefined ? {} : { build: construction.build }),
...(resumeSessionId === undefined ? {} : { attachment: { resumeSessionId } }),
state,
...(setup?.requestedModel === undefined
? {}
: { requestedModel: setup.requestedModel }),
});
// An existing session is reattached here: its route and its
// identity already exist, so this is a conversation that can be
Expand Down
154 changes: 154 additions & 0 deletions packages/acp/tests/adapter-protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,157 @@ describe("Tier EA — the embedded adapters' prompt-response metadata", () => {
expect(acceptances(adapter)).toEqual([]);
});
});

/** The continuation marker a client sends when its use names a model (#1177). */
const HAS_EXPLICIT_MODEL_META = "executablemd.has-explicit-model/v1";

/**
* A Claude adapter over the stateful fake, with a private global model
* preference and one conversation already running `running`.
*/
function* useResumable(
running: string,
environment: Record<string, string> = {},
): Operation<{ adapter: Adapter; sessionId: string; cwd: string }> {
const root = yield* until(mkdtemp(join(tmpdir(), "xmd ea model ")));
yield* ensure(() => rm(root, { recursive: true, force: true }));
const sessionId = "22222222-2222-4222-8222-222222222222";
yield* until(
import("node:fs/promises").then(async (fs) => {
await fs.mkdir(join(root, "state"));
await fs.writeFile(join(root, "settings.json"), JSON.stringify({ model: "fable" }));
await fs.writeFile(
join(root, "state", `${sessionId}.json`),
JSON.stringify({ model: running }),
);
}),
);
const adapter = yield* useAdapter("claude", {
...CLAUDE_ENV,
CLAUDE_CONFIG_DIR: root,
ANTHROPIC_MODEL: "",
FAKE_CLAUDE_STATE_DIR: join(root, "state"),
...environment,
});
yield* initialize(adapter);
// The conversation's own directory, so no project settings outside this
// case decide the global preference.
return { adapter, sessionId, cwd: root };
}

/** What a load/resume reports as the current model selection. */
function reportedModel(response: Record<string, unknown>): unknown {
const options = response["configOptions"];
if (!Array.isArray(options)) {
return undefined;
}
return options.find((option) => option?.["id"] === "model")?.["currentValue"];
}

function* modelWrites(adapter: Adapter): Operation<string[]> {
return (yield* readTextFile(adapter.log))
.split("\n")
.filter((line) => line.includes('"control":"set_model"'))
.map((line) => JSON.parse(line).model);
}

describe("Tier EA — the Claude adapter's resumed model under an explicit use (#1177)", () => {
it("EA15: a marked resume reports the exact running model and reasserts nothing", function* () {
for (const method of ["session/resume", "session/load"]) {
const { adapter, sessionId, cwd } = yield* useResumable("claude-fable-5");
const response = yield* adapter.request(method, {
sessionId,
cwd,
mcpServers: [],
_meta: { [HAS_EXPLICIT_MODEL_META]: true },
});
expect(reportedModel(response)).toBe("claude-fable-5");
expect(yield* modelWrites(adapter)).toEqual([]);
}
});

it("EA16: a different context hint is a different exact id, reported verbatim", function* () {
const { adapter, sessionId, cwd } = yield* useResumable("claude-fable-5[1m]", {
FAKE_CLAUDE_MODELS: JSON.stringify([
{
value: "claude-fable-5",
resolvedModel: "claude-fable-5",
displayName: "A",
description: "",
},
{
value: "claude-fable-5[1m]",
resolvedModel: "claude-fable-5[1m]",
displayName: "B",
description: "",
},
]),
});
const response = yield* adapter.request("session/resume", {
sessionId,
cwd,
mcpServers: [],
_meta: { [HAS_EXPLICIT_MODEL_META]: true },
});
expect(reportedModel(response)).toBe("claude-fable-5[1m]");
});

for (const [name, environment, refusal] of [
["absent", { FAKE_CLAUDE_CONTEXT_USAGE: "absent" }, "reported no model"],
["failed", { FAKE_CLAUDE_CONTEXT_USAGE: "fail" }, "could not report the model it runs"],
[
"unrepresentable",
{
FAKE_CLAUDE_MODELS: JSON.stringify([
{
value: "default",
resolvedModel: "claude-fable-5-1",
displayName: "D",
description: "",
},
{ value: "fable", resolvedModel: "claude-fable-5-1", displayName: "F", description: "" },
]),
},
"none of its offered models names exactly",
],
] as const) {
it(`EA17: a marked resume refuses a ${name} observation as an invalid request`, function* () {
const { adapter, sessionId, cwd } = yield* useResumable("fable", environment);
let failure = "";
try {
yield* adapter.request("session/resume", {
sessionId,
cwd,
mcpServers: [],
_meta: { [HAS_EXPLICIT_MODEL_META]: true },
});
} catch (error) {
failure = String(error);
}
expect(failure).toContain(refusal);
// Not a missing conversation, which a client could replace.
expect(failure).toContain('"code":-32600');
expect(yield* modelWrites(adapter)).toEqual([]);
expect(yield* readTextFile(adapter.log)).not.toContain('"type":"user"');
});
}

it("EA18: without the marker, resume and new keep the existing model choice", function* () {
const { adapter, sessionId, cwd } = yield* useResumable("claude-fable-5");
const resumed = yield* adapter.request("session/resume", {
sessionId,
cwd,
mcpServers: [],
});
// The global preference is reasserted, exactly as before.
expect(reportedModel(resumed)).toBe("fable");
expect(yield* modelWrites(adapter)).toEqual(["fable"]);
// A fresh session ignores the marker: there is nothing resumed to observe.
const fresh = yield* adapter.request("session/new", {
cwd,
mcpServers: [],
_meta: { [HAS_EXPLICIT_MODEL_META]: true },
});
expect(reportedModel(fresh)).toBe("fable");
});
});
Loading
Loading