feat(core): add managed repository cache (#30408)

This commit is contained in:
Shoubhit Dash
2026-06-02 19:10:09 +05:30
committed by GitHub
parent a78adb1b09
commit 371ee321e0
7 changed files with 839 additions and 4 deletions

View File

@@ -0,0 +1,49 @@
import { execFile } from "child_process"
import fs from "fs/promises"
import path from "path"
import { promisify } from "util"
import { pathToFileURL } from "url"
import { Repository } from "@opencode-ai/core/repository"
const exec = promisify(execFile)
export async function gitRemote(root: string) {
const origin = path.join(root, "origin.git")
const source = path.join(root, "source")
await git(root, "init", "--bare", origin)
await git(root, "init", source)
await git(source, "config", "user.email", "test@example.com")
await git(source, "config", "user.name", "Test")
await fs.writeFile(path.join(source, "README.md"), "one\n")
await git(source, "add", "README.md")
await git(source, "commit", "-m", "initial")
await git(source, "branch", "-M", "main")
await git(source, "remote", "add", "origin", pathToFileURL(origin).href)
await git(source, "push", "-u", "origin", "main")
await git(root, "--git-dir", origin, "symbolic-ref", "HEAD", "refs/heads/main")
return {
root,
source,
remote: pathToFileURL(origin).href,
reference: { ...Repository.parseRemote("owner/repo"), remote: pathToFileURL(origin).href },
}
}
export async function commit(source: string, content: string, message: string) {
await fs.writeFile(path.join(source, "README.md"), content)
await git(source, "add", "README.md")
await git(source, "commit", "-m", message)
await git(source, "push")
}
export async function branch(source: string, name: string, content: string) {
await git(source, "checkout", "-b", name)
await fs.writeFile(path.join(source, "README.md"), content)
await git(source, "add", "README.md")
await git(source, "commit", "-m", name)
await git(source, "push", "-u", "origin", name)
}
export async function git(cwd: string, ...args: string[]) {
await exec("git", args, { cwd })
}

View File

@@ -0,0 +1,66 @@
import { describe, expect } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { Effect } from "effect"
import { Git } from "@opencode-ai/core/git"
import { branch, commit, gitRemote } from "./fixture/git"
import { tmpdir } from "./fixture/tmpdir"
import { testEffect } from "./lib/effect"
const it = testEffect(Git.defaultLayer)
describe("Git", () => {
it.live("clones a remote and reads checkout metadata", () =>
withRemote((fixture) =>
Effect.gen(function* () {
const git = yield* Git.Service
const target = path.join(fixture.root, "checkout")
const result = yield* git.clone({ remote: fixture.remote, target })
expect(result.exitCode).toBe(0)
expect(yield* git.origin(target)).toBe(fixture.remote)
expect(yield* git.head(target)).toBeString()
expect(yield* git.branch(target)).toBe("main")
expect(yield* git.remoteHead(target)).toBe("origin/main")
expect(yield* read(path.join(target, "README.md"))).toBe("one\n")
}),
),
)
it.live("fetches, checks out, and resets remote changes", () =>
withRemote((fixture) =>
Effect.gen(function* () {
const git = yield* Git.Service
const target = path.join(fixture.root, "checkout")
yield* git.clone({ remote: fixture.remote, target })
yield* Effect.promise(() => commit(fixture.source, "two\n", "second"))
expect((yield* git.fetch(target)).exitCode).toBe(0)
expect((yield* git.reset(target, "origin/main")).exitCode).toBe(0)
expect(yield* read(path.join(target, "README.md"))).toBe("two\n")
yield* Effect.promise(() => branch(fixture.source, "feature/docs", "feature\n"))
expect((yield* git.fetchBranch(target, "feature/docs")).exitCode).toBe(0)
expect((yield* git.checkout(target, "feature/docs")).exitCode).toBe(0)
expect((yield* git.reset(target, "origin/feature/docs")).exitCode).toBe(0)
expect(yield* git.branch(target)).toBe("feature/docs")
expect(yield* read(path.join(target, "README.md"))).toBe("feature\n")
}),
),
)
})
function withRemote<A, E, R>(body: (fixture: Awaited<ReturnType<typeof gitRemote>>) => Effect.Effect<A, E, R>) {
return Effect.acquireUseRelease(
Effect.promise(async () => {
const root = await tmpdir()
return { root, fixture: await gitRemote(root.path) }
}),
(input) => body(input.fixture),
(input) => Effect.promise(() => input.root[Symbol.asyncDispose]()),
)
}
function read(file: string) {
return Effect.promise(() => fs.readFile(file, "utf8")).pipe(Effect.map((content) => content.replace(/\r\n/g, "\n")))
}

View File

@@ -0,0 +1,120 @@
import { describe, expect } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { pathToFileURL } from "url"
import { Effect, Layer } from "effect"
import { AppFileSystem } from "@opencode-ai/core/filesystem"
import { Git } from "@opencode-ai/core/git"
import { Global } from "@opencode-ai/core/global"
import { Repository } from "@opencode-ai/core/repository"
import { RepositoryCache } from "@opencode-ai/core/repository-cache"
import { EffectFlock } from "@opencode-ai/core/util/effect-flock"
import { git, gitRemote } from "./fixture/git"
import { tmpdir } from "./fixture/tmpdir"
import { testEffect } from "./lib/effect"
const it = testEffect(Layer.empty)
describe("RepositoryCache", () => {
it.live("replaces a stale cache directory before cloning", () =>
withRemote((fixture) =>
Effect.gen(function* () {
const localPath = Repository.cachePath(path.join(fixture.root, "repos"), fixture.reference)
yield* Effect.promise(async () => {
await fs.mkdir(localPath, { recursive: true })
await fs.writeFile(path.join(localPath, "stale.txt"), "stale")
})
const result = yield* (yield* RepositoryCache.Service).ensure({ reference: fixture.reference })
expect(result.status).toBe("cloned")
expect(yield* exists(path.join(localPath, "stale.txt"))).toBe(false)
expect(yield* read(path.join(localPath, "README.md"))).toBe("one\n")
}).pipe(Effect.provide(cacheLayer(fixture.root))),
),
)
it.live("serializes concurrent materialization for the same checkout", () =>
withRemote((fixture) =>
Effect.gen(function* () {
const cache = yield* RepositoryCache.Service
const results = yield* Effect.all(
[cache.ensure({ reference: fixture.reference }), cache.ensure({ reference: fixture.reference })],
{ concurrency: "unbounded" },
)
expect(results.map((result) => result.status).toSorted()).toEqual(["cached", "cloned"])
expect(results[0].localPath).toBe(results[1].localPath)
}).pipe(Effect.provide(cacheLayer(fixture.root))),
),
)
it.live("replaces an existing checkout whose origin does not match", () =>
withRemote((fixture) =>
Effect.gen(function* () {
const cache = yield* RepositoryCache.Service
const initial = yield* cache.ensure({ reference: fixture.reference })
yield* Effect.promise(async () => {
await git(initial.localPath, "config", "remote.origin.url", "https://github.com/other/repo.git")
await fs.writeFile(path.join(initial.localPath, "stale.txt"), "stale")
})
const replaced = yield* cache.ensure({ reference: fixture.reference })
expect(replaced.status).toBe("cloned")
expect(yield* exists(path.join(replaced.localPath, "stale.txt"))).toBe(false)
}).pipe(Effect.provide(cacheLayer(fixture.root))),
),
)
it.live("returns typed validation and clone failures", () =>
withRemote((fixture) =>
Effect.gen(function* () {
const cache = yield* RepositoryCache.Service
const invalidRepository = yield* Effect.flip(RepositoryCache.parseRemote("not-a-repo"))
expect(invalidRepository).toBeInstanceOf(RepositoryCache.InvalidRepositoryError)
const invalidBranch = yield* Effect.flip(cache.ensure({ reference: fixture.reference, branch: "../unsafe" }))
expect(invalidBranch).toBeInstanceOf(RepositoryCache.InvalidBranchError)
const cloneFailure = yield* Effect.flip(
cache.ensure({
reference: { ...fixture.reference, remote: pathToFileURL(path.join(fixture.root, "missing.git")).href },
}),
)
expect(cloneFailure).toBeInstanceOf(RepositoryCache.CloneFailedError)
}).pipe(Effect.provide(cacheLayer(fixture.root))),
),
)
})
function cacheLayer(root: string) {
const dependencies = Layer.mergeAll(
Global.layerWith({ state: path.join(root, "state"), repos: path.join(root, "repos") }),
AppFileSystem.defaultLayer,
)
return RepositoryCache.layer.pipe(
Layer.provide(EffectFlock.layer.pipe(Layer.provide(dependencies))),
Layer.provide(Git.defaultLayer),
Layer.provide(dependencies),
)
}
function withRemote<A, E, R>(body: (fixture: Awaited<ReturnType<typeof gitRemote>>) => Effect.Effect<A, E, R>) {
return Effect.acquireUseRelease(
Effect.promise(async () => {
const root = await tmpdir()
return { root, fixture: await gitRemote(root.path) }
}),
(input) => body(input.fixture),
(input) => Effect.promise(() => input.root[Symbol.asyncDispose]()),
)
}
function read(file: string) {
return Effect.promise(() => fs.readFile(file, "utf8")).pipe(Effect.map((content) => content.replace(/\r\n/g, "\n")))
}
function exists(file: string) {
return Effect.promise(() => fs.stat(file).then(() => true, () => false))
}

View File

@@ -0,0 +1,63 @@
import { describe, expect, test } from "bun:test"
import path from "path"
import { pathToFileURL } from "url"
import { Repository } from "@opencode-ai/core/repository"
describe("Repository", () => {
test("parses github shorthand and builds an explicit-root cache path", () => {
const reference = Repository.parseRemote("owner/repo")
expect(reference).toMatchObject({
host: "github.com",
path: "owner/repo",
segments: ["owner", "repo"],
owner: "owner",
repo: "repo",
remote: "https://github.com/owner/repo.git",
label: "owner/repo",
})
expect(Repository.cachePath("/cache", reference)).toBe(path.join("/cache", "github.com", "owner", "repo"))
expect(Repository.cacheIdentity(reference)).toBe("github.com/owner/repo")
})
test("parses host path and scp remote references", () => {
expect(Repository.parseRemote("gitlab.com/group/repo")).toMatchObject({
host: "gitlab.com",
path: "group/repo",
remote: "https://gitlab.com/group/repo.git",
label: "gitlab.com/group/repo",
})
expect(Repository.parseRemote("git@github.com:owner/repo.git")).toMatchObject({
host: "github.com",
path: "owner/repo",
remote: "git@github.com:owner/repo.git",
label: "owner/repo",
})
})
test("keeps local file repositories distinct from remote repositories", () => {
const localPath = path.resolve("repo.git")
const reference = Repository.parse(pathToFileURL(localPath).href)
expect(reference).toMatchObject({ host: "file", protocol: "file:", label: localPath })
expect(reference && Repository.isFile(reference)).toBe(true)
expect(reference && Repository.isRemote(reference)).toBe(false)
expect(() => Repository.parseRemote(pathToFileURL(localPath).href)).toThrow(Repository.UnsupportedLocalRepositoryError)
})
test("rejects unsafe remote references and branches with typed errors", () => {
expect(() => Repository.parseRemote("not-a-repo")).toThrow(Repository.InvalidReferenceError)
expect(() => Repository.parseRemote("git@github.com:../../../etc/passwd")).toThrow(Repository.InvalidReferenceError)
expect(() => Repository.validateBranch("feature/docs.v1")).not.toThrow()
expect(() => Repository.validateBranch("-bad")).toThrow(Repository.InvalidBranchError)
expect(() => Repository.validateBranch("bad..branch")).toThrow(Repository.InvalidBranchError)
expect(() => Repository.validateBranch("bad branch")).toThrow(Repository.InvalidBranchError)
})
test("compares cache identity independent of input spelling", () => {
const shorthand = Repository.parseRemote("owner/repo")
expect(Repository.same(shorthand, Repository.parseRemote("https://github.com/owner/repo.git"))).toBe(true)
expect(Repository.same(shorthand, Repository.parseRemote("github.com/owner/repo"))).toBe(true)
})
})