Skip to content

Commit 873795f

Browse files
Merge branch 'dev' into fix-env-caching-12698
2 parents 0ae35e0 + becf57e commit 873795f

31 files changed

Lines changed: 989 additions & 533 deletions

packages/opencode/src/cli/bootstrap.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { AppRuntime } from "@/effect/app-runtime"
2-
import { InstanceBootstrap } from "../project/bootstrap"
32
import { Instance } from "../project/instance"
43

54
export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
65
return Instance.provide({
76
directory,
8-
init: () => AppRuntime.runPromise(InstanceBootstrap),
97
fn: async () => {
108
try {
119
const result = await cb()

packages/opencode/src/cli/cmd/tui/worker.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Installation } from "@/installation"
22
import { Server } from "@/server/server"
33
import * as Log from "@opencode-ai/core/util/log"
44
import { Instance } from "@/project/instance"
5-
import { InstanceBootstrap } from "@/project/bootstrap"
65
import { Rpc } from "@/util/rpc"
76
import { upgrade } from "@/cli/upgrade"
87
import { Config } from "@/config/config"
@@ -77,7 +76,6 @@ export const rpc = {
7776
async checkUpgrade(input: { directory: string }) {
7877
await Instance.provide({
7978
directory: input.directory,
80-
init: () => AppRuntime.runPromise(InstanceBootstrap),
8179
fn: async () => {
8280
await upgrade().catch(() => {})
8381
},

packages/opencode/src/config/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { AppFileSystem } from "@opencode-ai/core/filesystem"
2323
import { InstanceState } from "@/effect/instance-state"
2424
import { Context, Duration, Effect, Exit, Fiber, Layer, Option, Schema } from "effect"
2525
import { EffectFlock } from "@opencode-ai/core/util/effect-flock"
26-
import { InstanceRef } from "@/effect/instance-ref"
26+
import { containsPath } from "../project/instance-context"
2727
import { zod } from "@/util/effect-zod"
2828
import { NonNegativeInt, PositiveInt, withStatics, type DeepMutable } from "@/util/schema"
2929
import { ConfigAgent } from "./agent"
@@ -459,7 +459,7 @@ export const layer = Layer.effect(
459459
const pluginScopeForSource = Effect.fnUntraced(function* (source: string) {
460460
if (source.startsWith("http://") || source.startsWith("https://")) return "global"
461461
if (source === "OPENCODE_CONFIG_CONTENT") return "local"
462-
if (yield* InstanceRef.use((ctx) => Effect.succeed(Instance.containsPath(source, ctx)))) return "local"
462+
if (containsPath(source, ctx)) return "local"
463463
return "global"
464464
})
465465

packages/opencode/src/file/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import fuzzysort from "fuzzysort"
1010
import ignore from "ignore"
1111
import path from "path"
1212
import { Global } from "@opencode-ai/core/global"
13-
import { Instance } from "../project/instance"
13+
import { containsPath } from "../project/instance-context"
1414
import * as Log from "@opencode-ai/core/util/log"
1515
import { Protected } from "./protected"
1616
import { Ripgrep } from "./ripgrep"
@@ -507,7 +507,7 @@ export const layer = Layer.effect(
507507
const ctx = yield* InstanceState.context
508508
const full = path.join(ctx.directory, file)
509509

510-
if (!Instance.containsPath(full, ctx)) {
510+
if (!containsPath(full, ctx)) {
511511
throw new Error("Access denied: path escapes project directory")
512512
}
513513

@@ -587,7 +587,7 @@ export const layer = Layer.effect(
587587
}
588588

589589
const resolved = dir ? path.join(ctx.directory, dir) : ctx.directory
590-
if (!Instance.containsPath(resolved, ctx)) {
590+
if (!containsPath(resolved, ctx)) {
591591
throw new Error("Access denied: path escapes project directory")
592592
}
593593

packages/opencode/src/lsp/lsp.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Process } from "@/util/process"
1212
import { spawn as lspspawn } from "./launch"
1313
import { Effect, Layer, Context, Schema } from "effect"
1414
import { InstanceState } from "@/effect/instance-state"
15-
import { AppFileSystem } from "@opencode-ai/core/filesystem"
15+
import { containsPath } from "@/project/instance-context"
1616
import { NonNegativeInt, withStatics } from "@/util/schema"
1717
import { zod, ZodOverride } from "@/util/effect-zod"
1818

@@ -221,12 +221,7 @@ export const layer = Layer.effect(
221221

222222
const getClients = Effect.fnUntraced(function* (file: string) {
223223
const ctx = yield* InstanceState.context
224-
if (
225-
!AppFileSystem.contains(ctx.directory, file) &&
226-
(ctx.worktree === "/" || !AppFileSystem.contains(ctx.worktree, file))
227-
) {
228-
return [] as LSPClient.Info[]
229-
}
224+
if (!containsPath(file, ctx)) return [] as LSPClient.Info[]
230225
const s = yield* InstanceState.get(state)
231226
return yield* Effect.promise(async () => {
232227
const extension = path.parse(file).ext || file

packages/opencode/src/project/bootstrap.ts

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,71 @@ import * as Vcs from "./vcs"
88
import { Bus } from "../bus"
99
import { Command } from "../command"
1010
import { InstanceState } from "@/effect/instance-state"
11-
import * as Log from "@opencode-ai/core/util/log"
1211
import { FileWatcher } from "@/file/watcher"
1312
import { ShareNext } from "@/share/share-next"
14-
import * as Effect from "effect/Effect"
13+
import { Context, Effect, Layer } from "effect"
1514
import { Config } from "@/config/config"
1615

17-
export const InstanceBootstrap = Effect.gen(function* () {
18-
const ctx = yield* InstanceState.context
19-
Log.Default.info("bootstrapping", { directory: ctx.directory })
20-
// everything depends on config so eager load it for nice traces
21-
yield* Config.Service.use((svc) => svc.get())
22-
// Plugin can mutate config so it has to be initialized before anything else.
23-
yield* Plugin.Service.use((svc) => svc.init())
24-
yield* Effect.all(
25-
[
26-
LSP.Service,
27-
ShareNext.Service,
28-
Format.Service,
29-
File.Service,
30-
FileWatcher.Service,
31-
Vcs.Service,
32-
Snapshot.Service,
33-
].map((s) => Effect.forkDetach(s.use((i) => i.init()))),
34-
).pipe(Effect.withSpan("InstanceBootstrap.init"))
35-
36-
const projectID = ctx.project.id
37-
yield* Bus.Service.use((svc) =>
38-
svc.subscribeCallback(Command.Event.Executed, async (payload) => {
39-
if (payload.properties.name === Command.Default.INIT) {
40-
Project.setInitialized(projectID)
41-
}
42-
}),
43-
)
44-
}).pipe(Effect.withSpan("InstanceBootstrap"))
16+
export interface Interface {
17+
readonly run: Effect.Effect<void>
18+
}
19+
20+
export class Service extends Context.Service<Service, Interface>()("@opencode/InstanceBootstrap") {}
21+
22+
export const layer = Layer.effect(
23+
Service,
24+
Effect.gen(function* () {
25+
// Yield each bootstrap dep at layer init so `run` itself has R = never.
26+
// This breaks the circular declaration loop through Config → Instance → InstanceStore
27+
// (instance-store.ts only yields this Service tag, never the impl-side services).
28+
const bus = yield* Bus.Service
29+
const config = yield* Config.Service
30+
const file = yield* File.Service
31+
const fileWatcher = yield* FileWatcher.Service
32+
const format = yield* Format.Service
33+
const lsp = yield* LSP.Service
34+
const plugin = yield* Plugin.Service
35+
const shareNext = yield* ShareNext.Service
36+
const snapshot = yield* Snapshot.Service
37+
const vcs = yield* Vcs.Service
38+
39+
const run = Effect.gen(function* () {
40+
const ctx = yield* InstanceState.context
41+
yield* Effect.logInfo("bootstrapping", { directory: ctx.directory })
42+
// everything depends on config so eager load it for nice traces
43+
yield* config.get()
44+
// Plugin can mutate config so it has to be initialized before anything else.
45+
yield* plugin.init()
46+
yield* Effect.all(
47+
[lsp, shareNext, format, file, fileWatcher, vcs, snapshot].map((s) => Effect.forkDetach(s.init())),
48+
).pipe(Effect.withSpan("InstanceBootstrap.init"))
49+
50+
const projectID = ctx.project.id
51+
yield* bus.subscribeCallback(Command.Event.Executed, async (payload) => {
52+
if (payload.properties.name === Command.Default.INIT) {
53+
Project.setInitialized(projectID)
54+
}
55+
})
56+
}).pipe(Effect.withSpan("InstanceBootstrap"))
57+
58+
return Service.of({ run })
59+
}),
60+
)
61+
62+
export const defaultLayer: Layer.Layer<Service> = layer.pipe(
63+
Layer.provide([
64+
Bus.layer,
65+
Config.defaultLayer,
66+
File.defaultLayer,
67+
FileWatcher.defaultLayer,
68+
Format.defaultLayer,
69+
LSP.defaultLayer,
70+
Plugin.defaultLayer,
71+
Project.defaultLayer,
72+
ShareNext.defaultLayer,
73+
Snapshot.defaultLayer,
74+
Vcs.defaultLayer,
75+
]),
76+
)
77+
78+
export * as InstanceBootstrap from "./bootstrap"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { LocalContext } from "@/util/local-context"
2+
import { AppFileSystem } from "@opencode-ai/core/filesystem"
3+
import type * as Project from "./project"
4+
5+
export interface InstanceContext {
6+
directory: string
7+
worktree: string
8+
project: Project.Info
9+
}
10+
11+
export const context = LocalContext.create<InstanceContext>("instance")
12+
13+
/**
14+
* Check if a path is within the project boundary.
15+
* Returns true if path is inside ctx.directory OR ctx.worktree.
16+
* Paths within the worktree but outside the working directory should not trigger external_directory permission.
17+
*/
18+
export function containsPath(filepath: string, ctx: InstanceContext): boolean {
19+
if (AppFileSystem.contains(ctx.directory, filepath)) return true
20+
// Non-git projects set worktree to "/" which would match ANY absolute path.
21+
// Skip worktree check in this case to preserve external_directory permissions.
22+
if (ctx.worktree === "/") return false
23+
return AppFileSystem.contains(ctx.worktree, filepath)
24+
}

0 commit comments

Comments
 (0)