diff --git a/packages/core/src/config/markdown.ts b/packages/core/src/config/markdown.ts index 7421f41762b..90ae38fe29d 100644 --- a/packages/core/src/config/markdown.ts +++ b/packages/core/src/config/markdown.ts @@ -21,6 +21,14 @@ export function parseOption(content: string) { } } +// Join a legacy separate `variant` into a string `model` so it survives native decoding. +// Embedded and structured native selections, and a variant without a model, stay unchanged. +export function joinLegacyVariant(data: Record) { + if (typeof data.model !== "string" || data.model.includes("#")) return data + if (typeof data.variant !== "string" || !/^[^#]+$/.test(data.variant)) return data + return { ...data, model: `${data.model}#${data.variant}` } +} + // Other coding agents accept unquoted colons and leading tags like `[Tag] text` // in frontmatter values. Retry those values as YAML block scalars so existing // config files keep working without rewriting valid YAML flow collections. diff --git a/packages/core/src/config/plugin/agent.ts b/packages/core/src/config/plugin/agent.ts index 6ddfb711f43..aa65e236785 100644 --- a/packages/core/src/config/plugin/agent.ts +++ b/packages/core/src/config/plugin/agent.ts @@ -164,21 +164,13 @@ const decode = Effect.fnUntraced(function* ( const legacy = unsupported.length > 0 && !keys.some((key) => nativeKeys.includes(key)) if (!legacy && unsupported.length > 0) yield* Effect.logWarning("ignored unsupported agent fields", { path: file.filepath, fields: unsupported }) - // Join legacy model + variant without sending native request/permissions through migration. - // Embedded and structured native selections, and a variant without a model, stay unchanged. - const data = - typeof markdown.data.model === "string" && - !markdown.data.model.includes("#") && - typeof markdown.data.variant === "string" && - /^[^#]+$/.test(markdown.data.variant) - ? { ...markdown.data, model: `${markdown.data.model}#${markdown.data.variant}` } - : markdown.data + // Native files join a legacy model + variant without sending request/permissions through migration. const decoded = legacy ? Result.map( decodeLegacyAgent({ name, ...markdown.data, prompt: body }, { errors: "all" }), ConfigMigrateV1.migrateAgent, ) - : decodeAgent({ ...data, system: body }, { errors: "all" }) + : decodeAgent({ ...ConfigMarkdown.joinLegacyVariant(markdown.data), system: body }, { errors: "all" }) if (Result.isFailure(decoded)) { yield* Effect.logWarning("agent file skipped", { filepath: file.filepath, diff --git a/packages/core/src/config/plugin/command.ts b/packages/core/src/config/plugin/command.ts index ab2a59f782d..1a6c3bd6834 100644 --- a/packages/core/src/config/plugin/command.ts +++ b/packages/core/src/config/plugin/command.ts @@ -142,7 +142,10 @@ const decode = Effect.fnUntraced(function* (directory: string, filepath: string, yield* Effect.logWarning("command file skipped", { filepath, reason: "markdown" }) return } - const info = decodeCommand({ ...markdown.data, template: markdown.content.trim() }) + const info = decodeCommand({ + ...ConfigMarkdown.joinLegacyVariant(markdown.data), + template: markdown.content.trim(), + }) if (Result.isFailure(info)) { yield* Effect.logWarning("command file skipped", { filepath, diff --git a/packages/core/test/config/command.test.ts b/packages/core/test/config/command.test.ts index cf77c83690c..512d386f731 100644 --- a/packages/core/test/config/command.test.ts +++ b/packages/core/test/config/command.test.ts @@ -4,6 +4,7 @@ import { describe, expect } from "bun:test" import { DateTime, Deferred, Effect, Fiber, Layer, Option, PubSub, Schema, Stream } from "effect" import { advance, drain } from "../lib/clock" import { Directory, Document, Event, Info } from "@opencode/schema/config" +import { Model } from "@opencode/schema/model" import { Session } from "@opencode/core/session" import { SessionExecution } from "@opencode/core/session/execution" import { Job } from "@opencode/core/job" @@ -30,7 +31,7 @@ import { Watcher } from "@opencode/core/filesystem/watcher" import { emptyCredentialNode, emptyWellknownNode } from "../fixture/config-nodes" import { emptyConfigLayer, emptyMcpLayer, testLocationLayer } from "../fixture/mcp" import { location } from "../fixture/location" -import { tmpdir } from "../fixture/tmpdir" +import { tmpdir, tmpdirScoped } from "../fixture/tmpdir" import { tempGlobalLayer } from "../fixture/global" import { offlineModels } from "../fixture/models" import { testEffect } from "../lib/effect" @@ -73,6 +74,25 @@ const it = testEffect( const decode = Schema.decodeUnknownSync(Info) describe("ConfigCommandPlugin.Plugin", () => { + for (const item of [ + { + name: "merges a separate legacy variant", + frontmatter: "model: example/chat\nvariant: high", + model: "example/chat#high", + }, + { + name: "keeps an embedded variant over a separate one", + frontmatter: "model: example/chat#low\nvariant: high", + model: "example/chat#low", + }, + ]) { + it.live(`Markdown command ${item.name}`, () => + Effect.gen(function* () { + expect(yield* runMarkdownCommand(item.frontmatter)).toEqual([Model.Ref.parse(item.model)]) + }), + ) + } + for (const item of [ ...["$&", "$$", "$`", "$'"].flatMap((input) => [ { template: "Explain $ARGUMENTS.", input, expected: `Explain ${input}.` }, @@ -518,6 +538,45 @@ function watchReady(config: Config.Interface, directory: string) { ) } +function runMarkdownCommand(frontmatter: string) { + return Effect.gen(function* () { + const tmp = yield* tmpdirScoped() + const fs = yield* FSUtil.Service + yield* fs.makeDirectory(path.join(tmp.path, "commands")) + yield* fs.writeFileString(path.join(tmp.path, "commands", "probe.md"), `---\n${frontmatter}\n---\nProbe.`) + const command = yield* Command.Service + const models: Model.Ref[] = [] + yield* ConfigCommandPlugin.Plugin.effect( + host({ + command: { + list: () => Effect.die("unused command.list"), + transform: command.transform, + reload: command.reload, + }, + session: { + switchModel: (input) => Effect.sync(() => void models.push(input.model)), + prompt: (input) => + Effect.succeed( + SessionInbox.User.make({ + id: SessionMessage.ID.make("msg_test"), + sessionID: input.sessionID, + time: { created: DateTime.makeUnsafe(0) }, + type: "user", + payload: { text: input.text }, + delivery: "steer", + }), + ), + }, + }), + ).pipe(Effect.provide(Config.testLayer([directoryEntry(tmp.path)]))) + yield* command.execute({ + name: "probe", + invocation: { sessionID: Session.ID.make("ses_test"), prompt: { text: "" }, delivery: "steer" }, + }) + return models + }) +} + function directoryEntry(directory: string) { return new Directory({ type: "directory", path: AbsolutePath.make(directory) }) }