Skip to content

Commit 51d8219

Browse files
authored
refactor: unwrap session/ tier-2 namespaces + self-reexport (anomalyco#22973)
1 parent d6af5a6 commit 51d8219

14 files changed

Lines changed: 4726 additions & 4726 deletions

File tree

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 338 additions & 338 deletions
Large diffs are not rendered by default.

‎packages/opencode/src/session/instruction.ts‎

Lines changed: 166 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -50,194 +50,194 @@ function extract(messages: MessageV2.WithParts[]) {
5050
return paths
5151
}
5252

53-
export namespace Instruction {
54-
export interface Interface {
55-
readonly clear: (messageID: MessageID) => Effect.Effect<void>
56-
readonly systemPaths: () => Effect.Effect<Set<string>, AppFileSystem.Error>
57-
readonly system: () => Effect.Effect<string[], AppFileSystem.Error>
58-
readonly find: (dir: string) => Effect.Effect<string | undefined, AppFileSystem.Error>
59-
readonly resolve: (
60-
messages: MessageV2.WithParts[],
61-
filepath: string,
62-
messageID: MessageID,
63-
) => Effect.Effect<{ filepath: string; content: string }[], AppFileSystem.Error>
64-
}
65-
66-
export class Service extends Context.Service<Service, Interface>()("@opencode/Instruction") {}
67-
68-
export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.Service | HttpClient.HttpClient> =
69-
Layer.effect(
70-
Service,
71-
Effect.gen(function* () {
72-
const cfg = yield* Config.Service
73-
const fs = yield* AppFileSystem.Service
74-
const http = HttpClient.filterStatusOk(withTransientReadRetry(yield* HttpClient.HttpClient))
75-
76-
const state = yield* InstanceState.make(
77-
Effect.fn("Instruction.state")(() =>
78-
Effect.succeed({
79-
// Track which instruction files have already been attached for a given assistant message.
80-
claims: new Map<MessageID, Set<string>>(),
81-
}),
82-
),
83-
)
53+
export interface Interface {
54+
readonly clear: (messageID: MessageID) => Effect.Effect<void>
55+
readonly systemPaths: () => Effect.Effect<Set<string>, AppFileSystem.Error>
56+
readonly system: () => Effect.Effect<string[], AppFileSystem.Error>
57+
readonly find: (dir: string) => Effect.Effect<string | undefined, AppFileSystem.Error>
58+
readonly resolve: (
59+
messages: MessageV2.WithParts[],
60+
filepath: string,
61+
messageID: MessageID,
62+
) => Effect.Effect<{ filepath: string; content: string }[], AppFileSystem.Error>
63+
}
8464

85-
const relative = Effect.fnUntraced(function* (instruction: string) {
86-
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
87-
return yield* fs
88-
.globUp(instruction, Instance.directory, Instance.worktree)
89-
.pipe(Effect.catch(() => Effect.succeed([] as string[])))
90-
}
91-
if (!Flag.OPENCODE_CONFIG_DIR) {
92-
log.warn(
93-
`Skipping relative instruction "${instruction}" - no OPENCODE_CONFIG_DIR set while project config is disabled`,
94-
)
95-
return []
96-
}
65+
export class Service extends Context.Service<Service, Interface>()("@opencode/Instruction") {}
66+
67+
export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.Service | HttpClient.HttpClient> =
68+
Layer.effect(
69+
Service,
70+
Effect.gen(function* () {
71+
const cfg = yield* Config.Service
72+
const fs = yield* AppFileSystem.Service
73+
const http = HttpClient.filterStatusOk(withTransientReadRetry(yield* HttpClient.HttpClient))
74+
75+
const state = yield* InstanceState.make(
76+
Effect.fn("Instruction.state")(() =>
77+
Effect.succeed({
78+
// Track which instruction files have already been attached for a given assistant message.
79+
claims: new Map<MessageID, Set<string>>(),
80+
}),
81+
),
82+
)
83+
84+
const relative = Effect.fnUntraced(function* (instruction: string) {
85+
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
9786
return yield* fs
98-
.globUp(instruction, Flag.OPENCODE_CONFIG_DIR, Flag.OPENCODE_CONFIG_DIR)
87+
.globUp(instruction, Instance.directory, Instance.worktree)
9988
.pipe(Effect.catch(() => Effect.succeed([] as string[])))
100-
})
101-
102-
const read = Effect.fnUntraced(function* (filepath: string) {
103-
return yield* fs.readFileString(filepath).pipe(Effect.catch(() => Effect.succeed("")))
104-
})
105-
106-
const fetch = Effect.fnUntraced(function* (url: string) {
107-
const res = yield* http.execute(HttpClientRequest.get(url)).pipe(
108-
Effect.timeout(5000),
109-
Effect.catch(() => Effect.succeed(null)),
89+
}
90+
if (!Flag.OPENCODE_CONFIG_DIR) {
91+
log.warn(
92+
`Skipping relative instruction "${instruction}" - no OPENCODE_CONFIG_DIR set while project config is disabled`,
11093
)
111-
if (!res) return ""
112-
const body = yield* res.arrayBuffer.pipe(Effect.catch(() => Effect.succeed(new ArrayBuffer(0))))
113-
return new TextDecoder().decode(body)
114-
})
115-
116-
const clear = Effect.fn("Instruction.clear")(function* (messageID: MessageID) {
117-
const s = yield* InstanceState.get(state)
118-
s.claims.delete(messageID)
119-
})
120-
121-
const systemPaths = Effect.fn("Instruction.systemPaths")(function* () {
122-
const config = yield* cfg.get()
123-
const paths = new Set<string>()
124-
125-
// The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor.
126-
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
127-
for (const file of FILES) {
128-
const matches = yield* fs.findUp(file, Instance.directory, Instance.worktree)
129-
if (matches.length > 0) {
130-
matches.forEach((item) => paths.add(path.resolve(item)))
131-
break
132-
}
94+
return []
95+
}
96+
return yield* fs
97+
.globUp(instruction, Flag.OPENCODE_CONFIG_DIR, Flag.OPENCODE_CONFIG_DIR)
98+
.pipe(Effect.catch(() => Effect.succeed([] as string[])))
99+
})
100+
101+
const read = Effect.fnUntraced(function* (filepath: string) {
102+
return yield* fs.readFileString(filepath).pipe(Effect.catch(() => Effect.succeed("")))
103+
})
104+
105+
const fetch = Effect.fnUntraced(function* (url: string) {
106+
const res = yield* http.execute(HttpClientRequest.get(url)).pipe(
107+
Effect.timeout(5000),
108+
Effect.catch(() => Effect.succeed(null)),
109+
)
110+
if (!res) return ""
111+
const body = yield* res.arrayBuffer.pipe(Effect.catch(() => Effect.succeed(new ArrayBuffer(0))))
112+
return new TextDecoder().decode(body)
113+
})
114+
115+
const clear = Effect.fn("Instruction.clear")(function* (messageID: MessageID) {
116+
const s = yield* InstanceState.get(state)
117+
s.claims.delete(messageID)
118+
})
119+
120+
const systemPaths = Effect.fn("Instruction.systemPaths")(function* () {
121+
const config = yield* cfg.get()
122+
const paths = new Set<string>()
123+
124+
// The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor.
125+
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
126+
for (const file of FILES) {
127+
const matches = yield* fs.findUp(file, Instance.directory, Instance.worktree)
128+
if (matches.length > 0) {
129+
matches.forEach((item) => paths.add(path.resolve(item)))
130+
break
133131
}
134132
}
133+
}
135134

136-
for (const file of globalFiles()) {
137-
if (yield* fs.existsSafe(file)) {
138-
paths.add(path.resolve(file))
139-
break
140-
}
135+
for (const file of globalFiles()) {
136+
if (yield* fs.existsSafe(file)) {
137+
paths.add(path.resolve(file))
138+
break
141139
}
140+
}
142141

143-
if (config.instructions) {
144-
for (const raw of config.instructions) {
145-
if (raw.startsWith("https://") || raw.startsWith("http://")) continue
146-
const instruction = raw.startsWith("~/") ? path.join(os.homedir(), raw.slice(2)) : raw
147-
const matches = yield* (
148-
path.isAbsolute(instruction)
149-
? fs.glob(path.basename(instruction), {
150-
cwd: path.dirname(instruction),
151-
absolute: true,
152-
include: "file",
153-
})
154-
: relative(instruction)
155-
).pipe(Effect.catch(() => Effect.succeed([] as string[])))
156-
matches.forEach((item) => paths.add(path.resolve(item)))
157-
}
142+
if (config.instructions) {
143+
for (const raw of config.instructions) {
144+
if (raw.startsWith("https://") || raw.startsWith("http://")) continue
145+
const instruction = raw.startsWith("~/") ? path.join(os.homedir(), raw.slice(2)) : raw
146+
const matches = yield* (
147+
path.isAbsolute(instruction)
148+
? fs.glob(path.basename(instruction), {
149+
cwd: path.dirname(instruction),
150+
absolute: true,
151+
include: "file",
152+
})
153+
: relative(instruction)
154+
).pipe(Effect.catch(() => Effect.succeed([] as string[])))
155+
matches.forEach((item) => paths.add(path.resolve(item)))
158156
}
157+
}
159158

160-
return paths
161-
})
159+
return paths
160+
})
162161

163-
const system = Effect.fn("Instruction.system")(function* () {
164-
const config = yield* cfg.get()
165-
const paths = yield* systemPaths()
166-
const urls = (config.instructions ?? []).filter(
167-
(item) => item.startsWith("https://") || item.startsWith("http://"),
168-
)
162+
const system = Effect.fn("Instruction.system")(function* () {
163+
const config = yield* cfg.get()
164+
const paths = yield* systemPaths()
165+
const urls = (config.instructions ?? []).filter(
166+
(item) => item.startsWith("https://") || item.startsWith("http://"),
167+
)
169168

170-
const files = yield* Effect.forEach(Array.from(paths), read, { concurrency: 8 })
171-
const remote = yield* Effect.forEach(urls, fetch, { concurrency: 4 })
169+
const files = yield* Effect.forEach(Array.from(paths), read, { concurrency: 8 })
170+
const remote = yield* Effect.forEach(urls, fetch, { concurrency: 4 })
172171

173-
return [
174-
...Array.from(paths).flatMap((item, i) => (files[i] ? [`Instructions from: ${item}\n${files[i]}`] : [])),
175-
...urls.flatMap((item, i) => (remote[i] ? [`Instructions from: ${item}\n${remote[i]}`] : [])),
176-
]
177-
})
172+
return [
173+
...Array.from(paths).flatMap((item, i) => (files[i] ? [`Instructions from: ${item}\n${files[i]}`] : [])),
174+
...urls.flatMap((item, i) => (remote[i] ? [`Instructions from: ${item}\n${remote[i]}`] : [])),
175+
]
176+
})
178177

179-
const find = Effect.fn("Instruction.find")(function* (dir: string) {
180-
for (const file of FILES) {
181-
const filepath = path.resolve(path.join(dir, file))
182-
if (yield* fs.existsSafe(filepath)) return filepath
178+
const find = Effect.fn("Instruction.find")(function* (dir: string) {
179+
for (const file of FILES) {
180+
const filepath = path.resolve(path.join(dir, file))
181+
if (yield* fs.existsSafe(filepath)) return filepath
182+
}
183+
})
184+
185+
const resolve = Effect.fn("Instruction.resolve")(function* (
186+
messages: MessageV2.WithParts[],
187+
filepath: string,
188+
messageID: MessageID,
189+
) {
190+
const sys = yield* systemPaths()
191+
const already = extract(messages)
192+
const results: { filepath: string; content: string }[] = []
193+
const s = yield* InstanceState.get(state)
194+
195+
const target = path.resolve(filepath)
196+
const root = path.resolve(Instance.directory)
197+
let current = path.dirname(target)
198+
199+
// Walk upward from the file being read and attach nearby instruction files once per message.
200+
while (current.startsWith(root) && current !== root) {
201+
const found = yield* find(current)
202+
if (!found || found === target || sys.has(found) || already.has(found)) {
203+
current = path.dirname(current)
204+
continue
183205
}
184-
})
185-
186-
const resolve = Effect.fn("Instruction.resolve")(function* (
187-
messages: MessageV2.WithParts[],
188-
filepath: string,
189-
messageID: MessageID,
190-
) {
191-
const sys = yield* systemPaths()
192-
const already = extract(messages)
193-
const results: { filepath: string; content: string }[] = []
194-
const s = yield* InstanceState.get(state)
195-
196-
const target = path.resolve(filepath)
197-
const root = path.resolve(Instance.directory)
198-
let current = path.dirname(target)
199-
200-
// Walk upward from the file being read and attach nearby instruction files once per message.
201-
while (current.startsWith(root) && current !== root) {
202-
const found = yield* find(current)
203-
if (!found || found === target || sys.has(found) || already.has(found)) {
204-
current = path.dirname(current)
205-
continue
206-
}
207-
208-
let set = s.claims.get(messageID)
209-
if (!set) {
210-
set = new Set()
211-
s.claims.set(messageID, set)
212-
}
213-
if (set.has(found)) {
214-
current = path.dirname(current)
215-
continue
216-
}
217-
218-
set.add(found)
219-
const content = yield* read(found)
220-
if (content) {
221-
results.push({ filepath: found, content: `Instructions from: ${found}\n${content}` })
222-
}
223206

207+
let set = s.claims.get(messageID)
208+
if (!set) {
209+
set = new Set()
210+
s.claims.set(messageID, set)
211+
}
212+
if (set.has(found)) {
224213
current = path.dirname(current)
214+
continue
225215
}
226216

227-
return results
228-
})
217+
set.add(found)
218+
const content = yield* read(found)
219+
if (content) {
220+
results.push({ filepath: found, content: `Instructions from: ${found}\n${content}` })
221+
}
229222

230-
return Service.of({ clear, systemPaths, system, find, resolve })
231-
}),
232-
)
223+
current = path.dirname(current)
224+
}
233225

234-
export const defaultLayer = layer.pipe(
235-
Layer.provide(Config.defaultLayer),
236-
Layer.provide(AppFileSystem.defaultLayer),
237-
Layer.provide(FetchHttpClient.layer),
226+
return results
227+
})
228+
229+
return Service.of({ clear, systemPaths, system, find, resolve })
230+
}),
238231
)
239232

240-
export function loaded(messages: MessageV2.WithParts[]) {
241-
return extract(messages)
242-
}
233+
export const defaultLayer = layer.pipe(
234+
Layer.provide(Config.defaultLayer),
235+
Layer.provide(AppFileSystem.defaultLayer),
236+
Layer.provide(FetchHttpClient.layer),
237+
)
238+
239+
export function loaded(messages: MessageV2.WithParts[]) {
240+
return extract(messages)
243241
}
242+
243+
export * as Instruction from "./instruction"

0 commit comments

Comments
 (0)