diff --git a/packages/adapters/opencode-local/src/server/execute.ts b/packages/adapters/opencode-local/src/server/execute.ts index 496027f6d1d..1d1d6a328a8 100644 --- a/packages/adapters/opencode-local/src/server/execute.ts +++ b/packages/adapters/opencode-local/src/server/execute.ts @@ -332,7 +332,9 @@ export async function execute(ctx: AdapterExecutionContext): Promise { await prepared.cleanup(); }); + it("loads global instructions and skills from the source config so the prompt is the same in every run", async () => { + const configHome = await makeConfigHome({ instructions: ["/team/RULES.md"], skills: { paths: ["/team/skills"], urls: ["https://skills.example/"] } }); + const sourceDir = path.join(configHome, "opencode"); + await fs.writeFile(path.join(sourceDir, "AGENTS.md"), "Global rules\n", "utf8"); + await fs.mkdir(path.join(sourceDir, "skills", "notes"), { recursive: true }); + await fs.writeFile(path.join(sourceDir, "skills", "notes", "SKILL.md"), "---\nname: notes\n---\n", "utf8"); + + const runs = await Promise.all([1, 2].map(() => prepareOpenCodeRuntimeConfig({ env: { XDG_CONFIG_HOME: configHome }, config: {}, useSourceGlobalPaths: true }))); + const configs = await Promise.all(runs.map(async (prepared) => { + cleanupPaths.add(prepared.env.XDG_CONFIG_HOME); + const runtimeDir = path.join(prepared.env.XDG_CONFIG_HOME, "opencode"); + await expect(fs.stat(path.join(runtimeDir, "AGENTS.md"))).rejects.toThrow(); + await expect(fs.stat(path.join(runtimeDir, "skills"))).rejects.toThrow(); + return JSON.parse(await fs.readFile(path.join(runtimeDir, "opencode.json"), "utf8")); + })); + + expect(runs[0]!.env.XDG_CONFIG_HOME).not.toBe(runs[1]!.env.XDG_CONFIG_HOME); + expect(configs[0].instructions).toEqual([path.join(sourceDir, "AGENTS.md"), "/team/RULES.md"]); + expect(configs[0].skills).toEqual({ paths: ["/team/skills", path.join(sourceDir, "skills")], urls: ["https://skills.example/"] }); + expect({ instructions: configs[1].instructions, skills: configs[1].skills }) + .toEqual({ instructions: configs[0].instructions, skills: configs[0].skills }); + expect(await fs.readFile(path.join(sourceDir, "AGENTS.md"), "utf8")).toBe("Global rules\n"); + await Promise.all(runs.map((prepared) => prepared.cleanup())); + }); + + it("keeps copied global instructions and skills when source paths are not used, as for remote targets", async () => { + const configHome = await makeConfigHome({ instructions: ["/team/RULES.md"] }); + const sourceDir = path.join(configHome, "opencode"); + await fs.writeFile(path.join(sourceDir, "AGENTS.md"), "Global rules\n", "utf8"); + await fs.mkdir(path.join(sourceDir, "skills", "notes"), { recursive: true }); + await fs.writeFile(path.join(sourceDir, "skills", "notes", "SKILL.md"), "---\nname: notes\n---\n", "utf8"); + + const prepared = await prepareOpenCodeRuntimeConfig({ env: { XDG_CONFIG_HOME: configHome }, config: {} }); + cleanupPaths.add(prepared.env.XDG_CONFIG_HOME); + const runtimeDir = path.join(prepared.env.XDG_CONFIG_HOME, "opencode"); + expect(await fs.readFile(path.join(runtimeDir, "AGENTS.md"), "utf8")).toBe("Global rules\n"); + await expect(fs.stat(path.join(runtimeDir, "skills", "notes", "SKILL.md"))).resolves.toBeTruthy(); + const config = JSON.parse(await fs.readFile(path.join(runtimeDir, "opencode.json"), "utf8")); + expect(config.instructions).toEqual(["/team/RULES.md"]); + expect(config).not.toHaveProperty("skills"); + await prepared.cleanup(); + }); + + it("leaves instructions and skills unset when the source config has neither", async () => { + const configHome = await makeConfigHome({}); + const prepared = await prepareOpenCodeRuntimeConfig({ env: { XDG_CONFIG_HOME: configHome }, config: {}, useSourceGlobalPaths: true }); + cleanupPaths.add(prepared.env.XDG_CONFIG_HOME); + const config = JSON.parse(await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8")); + expect(config).not.toHaveProperty("instructions"); + expect(config).not.toHaveProperty("skills"); + await prepared.cleanup(); + }); + it("allows all tools and connected tools by default", async () => { const configHome = await makeConfigHome({ permission: { diff --git a/packages/adapters/opencode-local/src/server/runtime-config.ts b/packages/adapters/opencode-local/src/server/runtime-config.ts index f3bae796307..80006649d82 100644 --- a/packages/adapters/opencode-local/src/server/runtime-config.ts +++ b/packages/adapters/opencode-local/src/server/runtime-config.ts @@ -128,6 +128,12 @@ export function applyOpenCodeProviderEnvAliases( export async function prepareOpenCodeRuntimeConfig(input: { env: Record; config: Record; + /** + * Point OpenCode at the global AGENTS.md and skills in the source config dir + * instead of the per-run copy. Only valid when the run can read host paths; + * remote targets get the copied dir shipped instead. + */ + useSourceGlobalPaths?: boolean; sandboxToolOomPreference?: boolean; }): Promise { const skipPermissions = asBoolean(input.config.dangerouslySkipPermissions, true); @@ -152,6 +158,9 @@ export async function prepareOpenCodeRuntimeConfig(input: { } } + const sourcePaths = input.useSourceGlobalPaths + ? await detachSourcePathEntries(sourceConfigDir, runtimeConfigDir) + : { instructions: [], skills: [] }; const existingConfig = await readJsonObject(runtimeConfigPath); const notes = [ skipPermissions ? "Injected runtime OpenCode config with permission=allow for all tools and connections." : "Prepared isolated OpenCode provider configuration.", @@ -210,6 +219,16 @@ export async function prepareOpenCodeRuntimeConfig(input: { ...existingConfig, ...(skipPermissions ? { permission: "allow" } : {}), }; + if (sourcePaths.instructions.length > 0) { + nextConfig.instructions = uniqueStrings([...sourcePaths.instructions, ...stringList(existingConfig.instructions)]); + } + if (sourcePaths.skills.length > 0) { + const skills = isPlainObject(existingConfig.skills) ? existingConfig.skills : {}; + nextConfig.skills = { ...skills, paths: uniqueStrings([...stringList(skills.paths), ...sourcePaths.skills]) }; + } + if (sourcePaths.instructions.length > 0 || sourcePaths.skills.length > 0) { + notes.push("Loaded OpenCode global instructions and skills from their source paths, so the prompt does not change between runs."); + } const inlineContent = input.env.OPENCODE_CONFIG_CONTENT ?? process.env.OPENCODE_CONFIG_CONTENT; let inlineConfig: Record | undefined; if (inlineContent) { @@ -261,6 +280,35 @@ export async function prepareOpenCodeRuntimeConfig(input: { }; } +async function detachSourcePathEntries(sourceDir: string, runtimeDir: string): Promise<{ instructions: string[]; skills: string[] }> { + const instructions: string[] = []; + const skills: string[] = []; + if (await isEntry(path.join(sourceDir, "AGENTS.md"), "file")) { + await fs.rm(path.join(runtimeDir, "AGENTS.md"), { force: true }); + instructions.push(path.join(sourceDir, "AGENTS.md")); + } + for (const name of ["skill", "skills"]) { + if (await isEntry(path.join(sourceDir, name), "directory")) { + await fs.rm(path.join(runtimeDir, name), { recursive: true, force: true }); + skills.push(path.join(sourceDir, name)); + } + } + return { instructions, skills }; +} + +async function isEntry(target: string, kind: "file" | "directory"): Promise { + const stat = await fs.stat(target).catch(() => null); + return kind === "file" ? Boolean(stat?.isFile()) : Boolean(stat?.isDirectory()); +} + +function stringList(value: unknown): string[] { + return Array.isArray(value) ? value.filter((entry): entry is string => typeof entry === "string") : []; +} + +function uniqueStrings(values: string[]): string[] { + return [...new Set(values)]; +} + /** Managed credentials must never leave host-only homes in a remote process. */ export function prepareManagedOpenCodeRemoteHomes(input: { env: Record; diff --git a/packages/adapters/opencode-local/src/server/test.ts b/packages/adapters/opencode-local/src/server/test.ts index 367ac7c6aa1..b2362b612df 100644 --- a/packages/adapters/opencode-local/src/server/test.ts +++ b/packages/adapters/opencode-local/src/server/test.ts @@ -128,7 +128,11 @@ export async function testEnvironment( // Prevent OpenCode from writing an opencode.json into the working directory. env.OPENCODE_DISABLE_PROJECT_CONFIG = "true"; - const preparedRuntimeConfig = await prepareOpenCodeRuntimeConfig({ env, config }); + const preparedRuntimeConfig = await prepareOpenCodeRuntimeConfig({ + env, + config, + useSourceGlobalPaths: !targetIsRemote, + }); const localRuntimeConfigHome = preparedRuntimeConfig.notes.length > 0 ? preparedRuntimeConfig.env.XDG_CONFIG_HOME : ""; if (asBoolean(config.dangerouslySkipPermissions, true)) {