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
5 changes: 5 additions & 0 deletions .changeset/isolated-config-root.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"opencode-drive": patch
---

Reject artifact roots beneath ambient OpenCode configuration instead of silently contaminating isolated fixtures.
23 changes: 21 additions & 2 deletions packages/drive/src/instance/instance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mkdir } from "node:fs/promises"
import { mkdir, stat } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join, resolve } from "node:path"
import { dirname, join, resolve } from "node:path"
import * as Effect from "effect/Effect"
import { createScriptFileSystem } from "../script/filesystem.js"
import {
Expand All @@ -20,6 +20,11 @@ export function artifactDirectory() {
}

export async function initializeInstance(name?: string) {
const ambient = await ambientConfigSource(artifactDirectory())
if (ambient)
throw new Error(
`Drive's isolated project would inherit ambient OpenCode configuration from ${ambient}; use a neutral TMPDIR`,
)
const artifacts = resolve(
join(artifactDirectory(), `run-${crypto.randomUUID()}`),
)
Expand Down Expand Up @@ -53,6 +58,20 @@ export async function initializeInstance(name?: string) {
return artifacts
}

async function ambientConfigSource(root: string) {
const names = [".opencode", ".claude", ".agents", "opencode.json", "opencode.jsonc"]
let directory = dirname(root)
while (true) {
for (const name of names) {
const source = join(directory, name)
if (await stat(source).then(() => true, () => false)) return source
}
const parent = dirname(directory)
if (parent === directory) return undefined
directory = parent
}
}

export const prepareInstanceProject = Effect.fn("OpenCodeInstance.prepareProject")(function* (options: {
readonly artifacts: string
readonly project?: Project
Expand Down
11 changes: 11 additions & 0 deletions packages/drive/test/cli/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ describe("opencode-drive", () => {
})
})

test("rejects an artifact root beneath ambient OpenCode configuration", async () => {
const root = await temporary()
await mkdir(join(root, ".opencode", "skills", "ambient"), { recursive: true })
const child = spawn(["init", "--name", "ambient-config"], root)
const [status, stderr] = await Promise.all([child.exited, new Response(child.stderr).text()])

expect(status).toBe(1)
expect(stderr).toContain(`would inherit ambient OpenCode configuration from ${join(root, ".opencode")}`)
expect(stderr).toContain("use a neutral TMPDIR")
})

test("starts, drives, prints dir, restarts, and stops a named detached instance", async () => {
const root = await temporary()
const name = "detached-test"
Expand Down
Loading