Visitar URL original
refactor(core): make node build bind maps conditionally (#34218) · iwwadigital/opencode@a4d3d87 · GitHub
Skip to content

Commit a4d3d87

Browse files
jlongsteriwwa-bot
authored andcommitted
refactor(core): make node build bind maps conditionally (anomalyco#34218)
1 parent 144d847 commit a4d3d87

3 files changed

Lines changed: 149 additions & 110 deletions

File tree

‎packages/core/src/effect/layer-node/layer-node-tree.ts‎

Lines changed: 85 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,52 @@ import { LayerNode } from "./layer-node"
33

44
type AnyNode = LayerNode.Node<unknown, unknown, any>
55
type RuntimeLayer = Layer.Layer<never, unknown, unknown>
6+
type Visit<Result> = (node: AnyNode, context: VisitContext<Result>) => Result
7+
8+
type VisitContext<Result> = {
9+
readonly cache: Map<AnyNode, Result>
10+
readonly visit: (node: AnyNode) => Result
11+
}
12+
13+
function walk<Result>(
14+
root: AnyNode,
15+
visit: Visit<Result>,
16+
options: {
17+
readonly cache?: Map<AnyNode, Result>
18+
readonly resolve?: (node: AnyNode) => AnyNode
19+
readonly detectCycles?: boolean
20+
} = {},
21+
) {
22+
const cache = options.cache ?? new Map<AnyNode, Result>()
23+
const visiting = new Set<AnyNode>()
24+
const stack: AnyNode[] = []
25+
26+
const recur = (node: AnyNode): Result => {
27+
const target = options.resolve?.(node) ?? node
28+
const cached = cache.get(target)
29+
if (cached !== undefined || cache.has(target)) return cached!
30+
31+
if (options.detectCycles !== false && visiting.has(target)) {
32+
const start = stack.indexOf(target)
33+
throw new Error(
34+
`Cycle detected in layer tree: ${[...stack.slice(start), target].map((item) => item.name).join(" -> ")}`,
35+
)
36+
}
37+
38+
visiting.add(target)
39+
stack.push(target)
40+
try {
41+
const result = visit(target, { cache, visit: recur })
42+
if (!cache.has(target)) cache.set(target, result)
43+
return result
44+
} finally {
45+
stack.pop()
46+
visiting.delete(target)
47+
}
48+
}
49+
50+
return recur(root)
51+
}
652

753
export function hoist<A, E, T extends LayerNode.Tag>(
854
root: LayerNode.Node<A, E, any>,
@@ -11,54 +57,28 @@ export function hoist<A, E, T extends LayerNode.Tag>(
1157
readonly node: LayerNode.Node<A, E>
1258
readonly hoisted: LayerNode.Node<unknown, E>
1359
} {
14-
const visited = new Map<AnyNode, AnyNode>()
1560
const hoisted = new Map<string, AnyNode>()
16-
const visiting = new Set<AnyNode>()
17-
const stack: AnyNode[] = []
1861

19-
const visit = (node: AnyNode): AnyNode => {
62+
const node = walk<AnyNode>(root, (node, context) => {
2063
if (node.kind === "group") {
21-
return { ...node, dependencies: node.dependencies.map(visit) }
64+
return { ...node, dependencies: node.dependencies.map(context.visit) }
2265
}
23-
24-
const existingNode = visited.get(node)
25-
if (existingNode) return existingNode
26-
2766
if (node.tag === tag) {
2867
const existing = hoisted.get(node.name)
2968
if (existing && existing !== node) {
3069
throw new Error(`Tag ${tag} has conflicting implementations for ${node.name}`)
3170
}
3271
hoisted.set(node.name, node)
33-
const empty = LayerNode.group([])
34-
visited.set(node, empty)
35-
return empty
72+
return LayerNode.group([])
3673
}
3774
if (node.kind === "unbound") {
3875
return node
3976
}
40-
41-
if (visiting.has(node)) {
42-
const start = stack.indexOf(node)
43-
throw new Error(
44-
`Cycle detected in layer tree: ${[...stack.slice(start), node].map((item) => item.name).join(" -> ")}`,
45-
)
46-
}
47-
visiting.add(node)
48-
stack.push(node)
49-
try {
50-
const dependencies = node.dependencies.map(visit)
51-
const clone = { ...node, dependencies }
52-
visited.set(node, clone)
53-
return clone
54-
} finally {
55-
stack.pop()
56-
visiting.delete(node)
57-
}
58-
}
77+
return { ...node, dependencies: node.dependencies.map(context.visit) }
78+
})
5979

6080
return {
61-
node: visit(root) as LayerNode.Node<A, E>,
81+
node: node as LayerNode.Node<A, E>,
6282
hoisted: LayerNode.group(Array.from(hoisted.values())) as LayerNode.Node<unknown, E>,
6383
}
6484
}
@@ -68,24 +88,32 @@ export function compile<A, E>(
6888
replacements?: ReadonlyMap<Layer.Any, Layer.Any>,
6989
): Layer.Layer<A, E> {
7090
const cache = new Map<AnyNode, RuntimeLayer>()
71-
const compileNode = (node: AnyNode): RuntimeLayer => {
72-
if (node.kind === "unbound") throw new Error(`Unbound layer node: ${node.name}`)
73-
const cached = cache.get(node)
74-
if (cached) return cached
75-
const dependencies = node.dependencies.flatMap(flatten).map(compileNode)
76-
const implementation = (replacements?.get(node.implementation!) ?? node.implementation!) as RuntimeLayer
77-
const layer =
78-
dependencies.length === 0
79-
? implementation
80-
: implementation.pipe(Layer.provide(dependencies as [RuntimeLayer, ...RuntimeLayer[]]))
81-
cache.set(node, layer)
82-
return layer
83-
}
91+
const compileNode = (node: AnyNode) =>
92+
walk<RuntimeLayer>(
93+
node,
94+
(node, context) => {
95+
if (node.kind === "unbound") throw new Error(`Unbound layer node: ${node.name}`)
96+
const dependencies = node.dependencies.flatMap(flatten).map(context.visit)
97+
const implementation = (replacements?.get(node.implementation!) ?? node.implementation!) as RuntimeLayer
98+
return dependencies.length === 0
99+
? implementation
100+
: implementation.pipe(Layer.provide(dependencies as [RuntimeLayer, ...RuntimeLayer[]]))
101+
},
102+
{ cache },
103+
)
84104
const layers = flatten(root).map((node) => compileNode(node))
85105
const layer = layers.reduce<RuntimeLayer>((result, layer) => layer.pipe(Layer.provideMerge(result)), Layer.empty)
86106
return layer as Layer.Layer<A, E>
87107
}
88108

109+
export function hasUnbound(root: LayerNode.Node<unknown, unknown, any>, source: AnyNode): boolean {
110+
if (source.kind !== "unbound") throw new Error(`Cannot check non-unbound layer node: ${source.name}`)
111+
return walk<boolean>(root, (node, context) => {
112+
if (node === source) return true
113+
return node.dependencies.some(context.visit)
114+
})
115+
}
116+
89117
export function bind<A, E, T extends LayerNode.Tag | undefined>(
90118
root: LayerNode.Node<A, E, T>,
91119
source: AnyNode,
@@ -98,17 +126,18 @@ export function bind<A, E, T extends LayerNode.Tag | undefined>(
98126
if (source.tag !== replacement.tag) {
99127
throw new Error(`Cannot bind ${source.name} across tags`)
100128
}
101-
const visited = new Map<AnyNode, AnyNode>()
102-
const visit = (node: AnyNode): AnyNode => {
103-
if (node === source) return replacement
104-
const existing = visited.get(node)
105-
if (existing) return existing
106-
if (node.kind === "unbound") return node
107-
const clone = { ...node, dependencies: node.dependencies.map(visit) }
108-
visited.set(node, clone)
109-
return clone
110-
}
111-
return visit(root) as LayerNode.Node<A, E, T>
129+
return walk<AnyNode>(
130+
root,
131+
(target, context) => {
132+
if (target.kind === "unbound") return target
133+
const dependencies: AnyNode[] = []
134+
const clone = { ...target, dependencies }
135+
context.cache.set(target, clone)
136+
dependencies.push(...target.dependencies.map(context.visit))
137+
return clone
138+
},
139+
{ detectCycles: false, resolve: (node) => (node === source ? replacement : node) },
140+
) as LayerNode.Node<A, E, T>
112141
}
113142

114143
function flatten(node: AnyNode): readonly AnyNode[] {

‎packages/core/src/effect/node-build.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { makeGlobalNode } from "./node"
77
export function build<A, E>(root: LayerNode.Node<A, E, any>, replacements?: readonly LayerNode.Replacement[]) {
88
const replacementMap = new Map(replacements?.map((item) => [item.source, item.replacement]))
99

10+
if (!LayerNodeTree.hasUnbound(root, LocationServiceMap.node)) {
11+
// If the location service map is not needed, we shouldn't pull it
12+
// in. Compile the graph normally
13+
return LayerNodeTree.compile(root, replacementMap)
14+
}
15+
1016
const locationMap = buildLocationServiceMap(replacementMap)
1117
const locationMapNode = makeGlobalNode({ service: LocationServiceMap.Service, layer: locationMap, deps: [] })
1218

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,72 @@
11
import { describe, expect, test } from "bun:test"
2-
import { Context, Effect, Layer } from "effect"
3-
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
2+
import { Context, Effect, Layer, LayerMap, Option } from "effect"
3+
import { LayerNode, LayerNodeTree } from "@opencode-ai/core/effect/layer-node"
44
import { Node } from "@opencode-ai/core/effect/node"
55
import { NodeBuild } from "@opencode-ai/core/effect/node-build"
66
import { Location } from "@opencode-ai/core/location"
77
import { LocationServiceMap } from "@opencode-ai/core/location-service-map"
8+
import type { LocationError, LocationServices } from "@opencode-ai/core/location-services"
89
import { Project } from "@opencode-ai/core/project"
910
import { AbsolutePath } from "@opencode-ai/core/schema"
1011
import { tmpdir } from "../../fixture/tmpdir"
1112

1213
class Value extends Context.Service<Value, { readonly value: string }>()("test/TagValue") {}
1314
class Result extends Context.Service<Result, { readonly value: string }>()("test/TagResult") {}
14-
class Left extends Context.Service<Left, { readonly value: string }>()("test/TagLeft") {}
15-
class Right extends Context.Service<Right, { readonly value: string }>()("test/TagRight") {}
16-
class Last extends Context.Service<Last, { readonly value: string }>()("test/TagLast") {}
15+
class CycleA extends Context.Service<CycleA, {}>()("test/NodeBuildA") {}
16+
class CycleB extends Context.Service<CycleB, { readonly directory: AbsolutePath }>()("test/NodeBuildB") {}
1717

1818
describe("node build", () => {
19+
test("does not build a location service map when the graph does not require it", async () => {
20+
const result = Node.makeGlobalNode({
21+
service: Result,
22+
layer: Layer.succeed(Result, Result.of({ value: "plain" })),
23+
deps: [],
24+
})
25+
const layer = NodeBuild.build(LayerNode.group([result]))
26+
const program = Effect.gen(function* () {
27+
expect(Option.isNone(yield* Effect.serviceOption(LocationServiceMap.Service))).toBe(true)
28+
return (yield* Result).value
29+
}).pipe(Effect.provide(layer))
30+
31+
expect(await Effect.runPromise(program)).toBe("plain")
32+
})
33+
34+
test("detects cycles through a bound location service map", () => {
35+
const a = Node.makeGlobalNode({
36+
service: CycleA,
37+
layer: Layer.effect(CycleA, Effect.as(LocationServiceMap.Service, CycleA.of({}))),
38+
deps: [LocationServiceMap.node],
39+
})
40+
const b = Node.makeGlobalNode({
41+
service: CycleB,
42+
layer: Layer.effect(
43+
CycleB,
44+
Effect.map(CycleA, () => CycleB.of({ directory: AbsolutePath.make(process.cwd()) })),
45+
),
46+
deps: [a],
47+
})
48+
const mapEffect = Effect.gen(function* () {
49+
const service = yield* CycleB
50+
return yield* LayerMap.make(
51+
(ref: Location.Ref) =>
52+
Layer.succeed(
53+
Location.Service,
54+
Location.Service.of({
55+
directory: ref.directory,
56+
workspaceID: ref.workspaceID,
57+
project: { id: Project.ID.global, directory: service.directory },
58+
}),
59+
),
60+
{ idleTimeToLive: "1 minute" },
61+
)
62+
}) as unknown as Effect.Effect<LayerMap.LayerMap<Location.Ref, LocationServices, LocationError>, never, CycleB>
63+
const mapLayer = Layer.effect(LocationServiceMap.Service, mapEffect)
64+
const map = Node.makeGlobalNode({ service: LocationServiceMap.Service, layer: mapLayer, deps: [b] })
65+
const graph = LayerNodeTree.bind(LayerNode.group([a]), LocationServiceMap.node, map)
66+
67+
expect(() => NodeBuild.build(graph)).toThrow("Cycle detected in layer tree")
68+
})
69+
1970
test("shares top-level project with location services", async () => {
2071
await using tmp = await tmpdir()
2172
let acquisitions = 0
@@ -37,6 +88,7 @@ describe("node build", () => {
3788
const program = Effect.gen(function* () {
3889
yield* Project.Service
3990
const locations = yield* LocationServiceMap.Service
91+
expect(Option.isSome(yield* Effect.serviceOption(LocationServiceMap.Service))).toBe(true)
4092
return yield* Location.Service.pipe(Effect.provide(locations.get(ref)))
4193
}).pipe(Effect.provide(layer))
4294

@@ -62,58 +114,10 @@ describe("node build", () => {
62114
})
63115
const serviceLayer = NodeBuild.build(LayerNode.group([result]))
64116
const program = Effect.gen(function* () {
117+
expect(Option.isNone(yield* Effect.serviceOption(LocationServiceMap.Service))).toBe(true)
65118
return (yield* Result).value
66119
}).pipe(Effect.provide(serviceLayer))
67120

68121
expect(await Effect.runPromise(program)).toBe("value")
69122
})
70-
71-
test("rebinds same-tag providers without reacquiring them", async () => {
72-
let firstAcquisitions = 0
73-
const tags = LayerNode.tags({ global: [] })
74-
const global = tags.make("global")
75-
const first = global({
76-
service: Value,
77-
layer: Layer.effect(
78-
Value,
79-
Effect.sync(() => {
80-
firstAcquisitions++
81-
return Value.of({ value: "first" })
82-
}),
83-
),
84-
deps: [],
85-
})
86-
const second = global({ service: Value, layer: Layer.succeed(Value, Value.of({ value: "second" })), deps: [] })
87-
const left = global({
88-
service: Left,
89-
layer: Layer.effect(
90-
Left,
91-
Effect.map(Value, (value) => Left.of({ value: value.value })),
92-
),
93-
deps: [first],
94-
})
95-
const right = global({
96-
service: Right,
97-
layer: Layer.effect(
98-
Right,
99-
Effect.map(Value, (value) => Right.of({ value: value.value })),
100-
),
101-
deps: [second],
102-
})
103-
const last = global({
104-
service: Last,
105-
layer: Layer.effect(
106-
Last,
107-
Effect.map(Value, (value) => Last.of({ value: value.value })),
108-
),
109-
deps: [first],
110-
})
111-
const layer = NodeBuild.build(LayerNode.group([left, right, last])) as Layer.Layer<Left | Right | Last>
112-
const values = Effect.gen(function* () {
113-
return [(yield* Left).value, (yield* Right).value, (yield* Last).value]
114-
}).pipe(Effect.provide(layer))
115-
116-
expect(await Effect.runPromise(values)).toEqual(["first", "second", "first"])
117-
expect(firstAcquisitions).toBe(1)
118-
})
119123
})

0 commit comments

Comments
 (0)