refactor: use instance state in small services (#23022)

This commit is contained in:
Kit Langton
2026-04-18 02:16:15 +00:00
committed by GitHub
parent 5fa1673341
commit 1dd257b76a
4 changed files with 13 additions and 21 deletions
+1 -2
View File
@@ -14,7 +14,6 @@ import { ConfigMCP } from "../config/mcp"
import { Log } from "../util" import { Log } from "../util"
import { NamedError } from "@opencode-ai/shared/util/error" import { NamedError } from "@opencode-ai/shared/util/error"
import z from "zod/v4" import z from "zod/v4"
import { Instance } from "../project/instance"
import { Installation } from "../installation" import { Installation } from "../installation"
import { InstallationVersion } from "../installation/version" import { InstallationVersion } from "../installation/version"
import { withTimeout } from "@/util/timeout" import { withTimeout } from "@/util/timeout"
@@ -391,7 +390,7 @@ export const layer = Layer.effect(
mcp: ConfigMCP.Info & { type: "local" }, mcp: ConfigMCP.Info & { type: "local" },
) { ) {
const [cmd, ...args] = mcp.command const [cmd, ...args] = mcp.command
const cwd = Instance.directory const cwd = yield* InstanceState.directory
const transport = new StdioClientTransport({ const transport = new StdioClientTransport({
stderr: "pipe", stderr: "pipe",
command: cmd, command: cmd,
+5 -10
View File
@@ -8,7 +8,6 @@ import { AppFileSystem } from "@opencode-ai/shared/filesystem"
import { FileWatcher } from "@/file/watcher" import { FileWatcher } from "@/file/watcher"
import { Git } from "@/git" import { Git } from "@/git"
import { Log } from "@/util" import { Log } from "@/util"
import { Instance } from "./instance"
import z from "zod" import z from "zod"
const log = Log.create({ service: "vcs" }) const log = Log.create({ service: "vcs" })
@@ -205,21 +204,17 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Git.Serv
}), }),
diff: Effect.fn("Vcs.diff")(function* (mode: Mode) { diff: Effect.fn("Vcs.diff")(function* (mode: Mode) {
const value = yield* InstanceState.get(state) const value = yield* InstanceState.get(state)
if (Instance.project.vcs !== "git") return [] const ctx = yield* InstanceState.context
if (ctx.project.vcs !== "git") return []
if (mode === "git") { if (mode === "git") {
return yield* track( return yield* track(fs, git, ctx.directory, (yield* git.hasHead(ctx.directory)) ? "HEAD" : undefined)
fs,
git,
Instance.directory,
(yield* git.hasHead(Instance.directory)) ? "HEAD" : undefined,
)
} }
if (!value.root) return [] if (!value.root) return []
if (value.current && value.current === value.root.name) return [] if (value.current && value.current === value.root.name) return []
const ref = yield* git.mergeBase(Instance.directory, value.root.ref) const ref = yield* git.mergeBase(ctx.directory, value.root.ref)
if (!ref) return [] if (!ref) return []
return yield* compare(fs, git, Instance.directory, ref) return yield* compare(fs, git, ctx.directory, ref)
}), }),
}) })
}), }),
+2 -5
View File
@@ -13,7 +13,6 @@ import { type LanguageModelV3 } from "@ai-sdk/provider"
import * as ModelsDev from "./models" import * as ModelsDev from "./models"
import { Auth } from "../auth" import { Auth } from "../auth"
import { Env } from "../env" import { Env } from "../env"
import { Instance } from "../project/instance"
import { InstallationVersion } from "../installation/version" import { InstallationVersion } from "../installation/version"
import { Flag } from "../flag/flag" import { Flag } from "../flag/flag"
import { zod } from "@/util/effect-zod" import { zod } from "@/util/effect-zod"
@@ -537,6 +536,7 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
const token = apiKey ?? (yield* dep.get("GITLAB_TOKEN")) const token = apiKey ?? (yield* dep.get("GITLAB_TOKEN"))
const providerConfig = (yield* dep.config()).provider?.["gitlab"] const providerConfig = (yield* dep.config()).provider?.["gitlab"]
const directory = yield* InstanceState.directory
const aiGatewayHeaders = { const aiGatewayHeaders = {
"User-Agent": `opencode/${InstallationVersion} gitlab-ai-provider/${GITLAB_PROVIDER_VERSION} (${os.platform()} ${os.release()}; ${os.arch()})`, "User-Agent": `opencode/${InstallationVersion} gitlab-ai-provider/${GITLAB_PROVIDER_VERSION} (${os.platform()} ${os.release()}; ${os.arch()})`,
@@ -591,10 +591,7 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
auth?.type === "api" ? { "PRIVATE-TOKEN": token } : { Authorization: `Bearer ${token}` } auth?.type === "api" ? { "PRIVATE-TOKEN": token } : { Authorization: `Bearer ${token}` }
log.info("gitlab model discovery starting", { instanceUrl }) log.info("gitlab model discovery starting", { instanceUrl })
const result = await discoverWorkflowModels( const result = await discoverWorkflowModels({ instanceUrl, getHeaders }, { workingDirectory: directory })
{ instanceUrl, getHeaders },
{ workingDirectory: Instance.directory },
)
if (!result.models.length) { if (!result.models.length) {
log.info("gitlab model discovery skipped: no models found", { log.info("gitlab model discovery skipped: no models found", {
+5 -4
View File
@@ -8,7 +8,6 @@ import { Flag } from "@/flag/flag"
import { AppFileSystem } from "@opencode-ai/shared/filesystem" import { AppFileSystem } from "@opencode-ai/shared/filesystem"
import { withTransientReadRetry } from "@/util/effect-http-client" import { withTransientReadRetry } from "@/util/effect-http-client"
import { Global } from "../global" import { Global } from "../global"
import { Instance } from "../project/instance"
import { Log } from "../util" import { Log } from "../util"
import type { MessageV2 } from "./message-v2" import type { MessageV2 } from "./message-v2"
import type { MessageID } from "./schema" import type { MessageID } from "./schema"
@@ -82,9 +81,10 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.S
) )
const relative = Effect.fnUntraced(function* (instruction: string) { const relative = Effect.fnUntraced(function* (instruction: string) {
const ctx = yield* InstanceState.context
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) { if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
return yield* fs return yield* fs
.globUp(instruction, Instance.directory, Instance.worktree) .globUp(instruction, ctx.directory, ctx.worktree)
.pipe(Effect.catch(() => Effect.succeed([] as string[]))) .pipe(Effect.catch(() => Effect.succeed([] as string[])))
} }
if (!Flag.OPENCODE_CONFIG_DIR) { if (!Flag.OPENCODE_CONFIG_DIR) {
@@ -119,12 +119,13 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.S
const systemPaths = Effect.fn("Instruction.systemPaths")(function* () { const systemPaths = Effect.fn("Instruction.systemPaths")(function* () {
const config = yield* cfg.get() const config = yield* cfg.get()
const ctx = yield* InstanceState.context
const paths = new Set<string>() const paths = new Set<string>()
// The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor. // The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor.
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) { if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
for (const file of FILES) { for (const file of FILES) {
const matches = yield* fs.findUp(file, Instance.directory, Instance.worktree) const matches = yield* fs.findUp(file, ctx.directory, ctx.worktree)
if (matches.length > 0) { if (matches.length > 0) {
matches.forEach((item) => paths.add(path.resolve(item))) matches.forEach((item) => paths.add(path.resolve(item)))
break break
@@ -191,9 +192,9 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.S
const already = extract(messages) const already = extract(messages)
const results: { filepath: string; content: string }[] = [] const results: { filepath: string; content: string }[] = []
const s = yield* InstanceState.get(state) const s = yield* InstanceState.get(state)
const root = path.resolve(yield* InstanceState.directory)
const target = path.resolve(filepath) const target = path.resolve(filepath)
const root = path.resolve(Instance.directory)
let current = path.dirname(target) let current = path.dirname(target)
// Walk upward from the file being read and attach nearby instruction files once per message. // Walk upward from the file being read and attach nearby instruction files once per message.