Skip to content

Commit a4ad173

Browse files
authored
fix(core): apply safe defaults to all agents (anomalyco#40316)
1 parent 93e8b75 commit a4ad173

11 files changed

Lines changed: 111 additions & 106 deletions

File tree

‎packages/core/src/agent.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
export * as Agent from "./agent"
22

3+
import path from "path"
34
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
45
import { Array, Context, Effect, Layer, Types } from "effect"
56
import { Agent } from "@opencode-ai/schema/agent"
7+
import { Global } from "@opencode-ai/util/global"
68
import { Bus } from "./bus"
79
import { State } from "./state"
810

11+
const SHELL_OUTPUT_GLOB = (data: string) => path.join(data, "shell", "*", "*")
12+
const TOOL_OUTPUT_GLOB = (data: string) => path.join(data, "tool-output", "*")
13+
914
export const ID = Agent.ID
1015
export type ID = typeof ID.Type
1116
export const Name = Agent.Name
@@ -51,6 +56,13 @@ const layer = Layer.effect(
5156
Service,
5257
Effect.gen(function* () {
5358
const bus = yield* Bus.Service
59+
const global = yield* Global.Service
60+
const permissions: Info["permissions"] = [
61+
{ action: "external_directory", resource: SHELL_OUTPUT_GLOB(global.data), effect: "allow" },
62+
{ action: "external_directory", resource: TOOL_OUTPUT_GLOB(global.data), effect: "allow" },
63+
{ action: "external_directory", resource: path.join(global.tmp, "*"), effect: "allow" },
64+
{ action: "external_directory", resource: path.join(global.config, "*"), effect: "allow" },
65+
]
5466
const state = State.create<Data, Draft>({
5567
name: "agent",
5668
initial: () => ({ agents: new Map() }),
@@ -61,7 +73,13 @@ const layer = Layer.effect(
6173
draft.default = id
6274
},
6375
update: (id, fn) => {
64-
const current = draft.agents.get(id) ?? (Info.default(id) as Types.DeepMutable<Info>)
76+
const defaults = Info.default(id)
77+
const current =
78+
draft.agents.get(id) ??
79+
({
80+
...defaults,
81+
permissions: [...defaults.permissions, ...permissions],
82+
} as Types.DeepMutable<Info>)
6583
if (!draft.agents.has(id)) draft.agents.set(id, current)
6684
fn(current)
6785
current.id = id
@@ -114,4 +132,4 @@ const layer = Layer.effect(
114132
}),
115133
)
116134

117-
export const node = makeLocationNode({ service: Service, layer, deps: [Bus.node] })
135+
export const node = makeLocationNode({ service: Service, layer, deps: [Bus.node, Global.node] })

‎packages/core/src/instructions/builtins.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * as InstructionBuiltIns from "./builtins"
22

33
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
44
import { Context, DateTime, Effect, Layer, Schema } from "effect"
5+
import { Global } from "@opencode-ai/util/global"
56
import { Location } from "../location"
67
import { SessionSchema } from "../session/schema"
78
import { Instructions } from "./index"
@@ -15,6 +16,7 @@ export class Service extends Context.Service<Service, Interface>()("@opencode/In
1516
const layer = Layer.effect(
1617
Service,
1718
Effect.gen(function* () {
19+
const global = yield* Global.Service
1820
const location = yield* Location.Service
1921
return Service.of({
2022
load: (sessionID) =>
@@ -31,6 +33,7 @@ const layer = Layer.effect(
3133
` Workspace root folder: ${location.project.directory}`,
3234
` Is directory a git repo: ${location.vcs?.type === "git" ? "yes" : "no"}`,
3335
` Platform: ${process.platform}`,
36+
` Use ${global.tmp} for temporary work outside the workspace; it already exists and is pre-approved for external directory access.`,
3437
"</env>",
3538
].join("\n"),
3639
),
@@ -58,4 +61,4 @@ const layer = Layer.effect(
5861
}),
5962
)
6063

61-
export const node = makeLocationNode({ service: Service, layer, deps: [Location.node] })
64+
export const node = makeLocationNode({ service: Service, layer, deps: [Global.node, Location.node] })

‎packages/core/src/plugin/agent.ts‎

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
export * as AgentPlugin from "./agent"
22

3-
import path from "path"
43
import { define } from "@opencode-ai/plugin/effect/plugin"
54
import { Effect } from "effect"
65
import { Agent } from "../agent"
7-
import { Global } from "@opencode-ai/util/global"
8-
import { Location } from "../location"
96
import { Permission } from "../permission"
107

11-
// Combined output files written by the Shell service, e.g. `<data>/shell/<projectID>/<shellID>.out`.
12-
// Whitelisted so agents can read a command's full captured output without an external-directory prompt.
13-
const SHELL_OUTPUT_GLOB = path.join(Global.Path.data, "shell", "*", "*")
14-
158
const PROMPT_EXPLORE = `You are a file search specialist. You excel at thoroughly navigating and exploring codebases.
169

1710
Your strengths:
@@ -100,57 +93,21 @@ Rules:
10093
export const Plugin = define({
10194
id: "opencode.agent",
10295
effect: Effect.fn(function* (ctx) {
103-
const location = yield* Location.Service
104-
const worktree = location.directory
105-
const whitelistedDirs = [SHELL_OUTPUT_GLOB, path.join(Global.Path.tmp, "*")]
106-
const readonlyExternalDirectory: Permission.Ruleset = [
107-
{ action: "external_directory", resource: "*", effect: "ask" },
108-
...whitelistedDirs.map(
109-
(resource): Permission.Rule => ({ action: "external_directory", resource, effect: "allow" }),
110-
),
111-
]
112-
const defaults: Permission.Ruleset = [
113-
{ action: "*", resource: "*", effect: "allow" },
114-
...readonlyExternalDirectory,
115-
{ action: "question", resource: "*", effect: "deny" },
116-
{ action: "plan_enter", resource: "*", effect: "deny" },
117-
{ action: "plan_exit", resource: "*", effect: "deny" },
118-
{ action: "read", resource: "*", effect: "allow" },
119-
{ action: "read", resource: "*.env", effect: "ask" },
120-
{ action: "read", resource: "*.env.*", effect: "ask" },
121-
{ action: "read", resource: "*.env.example", effect: "allow" },
122-
]
123-
12496
yield* ctx.agent.transform((draft) => {
12597
draft.update(Agent.defaultID, (item) => {
12698
item.name = Agent.Name.make("Build")
12799
item.description = "The default agent. Executes tools based on configured permissions."
128100
item.mode = "primary"
129-
item.permissions.push(
130-
...Permission.merge(defaults, [
131-
{ action: "question", resource: "*", effect: "allow" },
132-
{ action: "plan_enter", resource: "*", effect: "allow" },
133-
]),
134-
)
101+
item.permissions.push({ action: "question", resource: "*", effect: "allow" })
135102
})
136103

137104
draft.update(Agent.ID.make("plan"), (item) => {
138105
item.name = Agent.Name.make("Plan")
139106
item.description = "Plan mode. Disallows all edit tools."
140107
item.mode = "primary"
141108
item.permissions.push(
142-
...Permission.merge(defaults, [
143-
{ action: "question", resource: "*", effect: "allow" },
144-
{ action: "plan_exit", resource: "*", effect: "allow" },
145-
{ action: "external_directory", resource: path.join(Global.Path.data, "plans", "*"), effect: "allow" },
146-
{ action: "edit", resource: "*", effect: "deny" },
147-
{ action: "edit", resource: path.join(".opencode", "plans", "*.md"), effect: "allow" },
148-
{
149-
action: "edit",
150-
resource: path.relative(worktree, path.join(Global.Path.data, "plans", "*.md")),
151-
effect: "allow",
152-
},
153-
]),
109+
{ action: "question", resource: "*", effect: "allow" },
110+
{ action: "edit", resource: "*", effect: "deny" },
154111
)
155112
})
156113

@@ -159,28 +116,36 @@ export const Plugin = define({
159116
item.description =
160117
"General-purpose agent for researching complex questions and executing multi-step tasks. Use this agent to execute multiple units of work in parallel."
161118
item.mode = "subagent"
162-
item.permissions.push(...Permission.merge(defaults, [{ action: "subagent", resource: "*", effect: "deny" }]))
119+
item.permissions.push(
120+
{ action: "question", resource: "*", effect: "deny" },
121+
{ action: "subagent", resource: "*", effect: "deny" },
122+
)
163123
})
164124

165125
draft.update(Agent.ID.make("explore"), (item) => {
126+
const externalDirectories = item.permissions.filter(
127+
(rule) => rule.action === "external_directory" && rule.effect === "allow",
128+
)
166129
item.name = Agent.Name.make("Explore")
167130
item.description =
168131
'Fast agent specialized for exploring codebases. Use this when you need to quickly find files by patterns (eg. "src/components/**/*.tsx"), search code for keywords (eg. "API endpoints"), or answer questions about the codebase (eg. "how do API endpoints work?"). When calling this agent, specify the desired thoroughness level: "quick" for basic searches, "medium" for moderate exploration, or "very thorough" for comprehensive analysis across multiple locations and naming conventions.'
169132
item.system = PROMPT_EXPLORE
170133
item.mode = "subagent"
171134
item.permissions.push(
172135
...Permission.merge(
173-
defaults,
174136
[
175137
{ action: "*", resource: "*", effect: "deny" },
176138
{ action: "grep", resource: "*", effect: "allow" },
177139
{ action: "glob", resource: "*", effect: "allow" },
178140
{ action: "webfetch", resource: "*", effect: "allow" },
179141
{ action: "websearch", resource: "*", effect: "allow" },
180142
{ action: "read", resource: "*", effect: "allow" },
143+
{ action: "read", resource: "*.env", effect: "ask" },
144+
{ action: "read", resource: "*.env.*", effect: "ask" },
145+
{ action: "read", resource: "*.env.example", effect: "allow" },
181146
{ action: "subagent", resource: "*", effect: "deny" },
182147
],
183-
readonlyExternalDirectory,
148+
[{ action: "external_directory", resource: "*", effect: "ask" }, ...externalDirectories],
184149
),
185150
)
186151
})
@@ -190,23 +155,23 @@ export const Plugin = define({
190155
item.mode = "primary"
191156
item.hidden = true
192157
item.system = PROMPT_COMPACTION
193-
item.permissions.push(...Permission.merge(defaults, [{ action: "*", resource: "*", effect: "deny" }]))
158+
item.permissions.push({ action: "*", resource: "*", effect: "deny" })
194159
})
195160

196161
draft.update(Agent.ID.make("title"), (item) => {
197162
item.name = Agent.Name.make("Title")
198163
item.mode = "primary"
199164
item.hidden = true
200165
item.system = PROMPT_TITLE
201-
item.permissions.push(...Permission.merge(defaults, [{ action: "*", resource: "*", effect: "deny" }]))
166+
item.permissions.push({ action: "*", resource: "*", effect: "deny" })
202167
})
203168

204169
draft.update(Agent.ID.make("summary"), (item) => {
205170
item.name = Agent.Name.make("Summary")
206171
item.mode = "primary"
207172
item.hidden = true
208173
item.system = PROMPT_SUMMARY
209-
item.permissions.push(...Permission.merge(defaults, [{ action: "*", resource: "*", effect: "deny" }]))
174+
item.permissions.push({ action: "*", resource: "*", effect: "deny" })
210175
})
211176
})
212177
}),

‎packages/core/test/agent.test.ts‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path"
12
import { describe, expect } from "bun:test"
23
import { Effect, Exit, Fiber, Layer, Scope, Stream } from "effect"
34
import { TestClock } from "effect/testing"
@@ -9,15 +10,19 @@ import { Location } from "@opencode-ai/core/location"
910
import { Permission } from "@opencode-ai/core/permission"
1011
import { AgentPlugin } from "@opencode-ai/core/plugin/agent"
1112
import { AbsolutePath } from "@opencode-ai/core/schema"
13+
import { Global } from "@opencode-ai/util/global"
1214
import { location } from "./fixture/location"
1315
import { testEffect } from "./lib/effect"
1416
import { agentHost, host } from "./plugin/host"
1517

1618
const testLocation = location({ directory: AbsolutePath.make("/project") })
1719
const locationLayer = Layer.succeed(Location.Service, Location.Service.of(testLocation))
20+
const global = Global.make({ data: "/data", config: "/config", tmp: "/tmp/opencode" })
21+
const globalLayer = Layer.succeed(Global.Service, Global.Service.of(global))
1822

1923
const it = testEffect(
2024
AppNodeBuilder.build(LayerNode.group([Agent.node, Bus.node, Location.node]), [
25+
[Global.node, globalLayer],
2126
[Location.node, locationLayer],
2227
]) as unknown as Layer.Layer<unknown, never>,
2328
)
@@ -120,25 +125,35 @@ describe("Agent", () => {
120125
const id = Agent.ID.make("custom")
121126

122127
yield* agent.transform((editor) => editor.update(id, () => {}))
123-
expect(yield* agent.get(id)).toEqual(Agent.Info.default(id))
128+
const info = yield* agent.get(id)
129+
expect(info?.permissions.slice(0, Agent.Info.default(id).permissions.length)).toEqual(
130+
Agent.Info.default(id).permissions,
131+
)
132+
expect(Permission.evaluate("external_directory", path.join(global.data, "shell", "*", "*"), info?.permissions ?? []).effect).toBe(
133+
"allow",
134+
)
135+
expect(Permission.evaluate("external_directory", path.join(global.data, "tool-output", "*"), info?.permissions ?? []).effect).toBe(
136+
"allow",
137+
)
138+
expect(Permission.evaluate("external_directory", path.join(global.config, "*"), info?.permissions ?? []).effect).toBe(
139+
"allow",
140+
)
141+
expect(Permission.evaluate("external_directory", path.join(global.tmp, "*"), info?.permissions ?? []).effect).toBe(
142+
"allow",
143+
)
124144

125145
yield* agent.transform((editor) => editor.remove(id))
126146
expect(yield* agent.get(id)).toBeUndefined()
127147
}),
128148
)
129149

130-
it.effect("does not ambiently opt built-in agents into bash", () =>
150+
it.effect("applies managed external directories without opting built-in agents into bash", () =>
131151
Effect.gen(function* () {
132152
const agent = yield* Agent.Service
133153
yield* AgentPlugin.Plugin.effect(
134154
host({
135155
agent: agentHost(agent),
136156
}),
137-
).pipe(
138-
Effect.provideService(
139-
Location.Service,
140-
Location.Service.of(location({ directory: AbsolutePath.make("/project") })),
141-
),
142157
)
143158

144159
const agents = yield* agent.list()
@@ -152,6 +167,20 @@ describe("Agent", () => {
152167
"title",
153168
])
154169
expect((yield* agent.get(Agent.defaultID))?.system).toBeUndefined()
170+
const permissions = (yield* agent.get(Agent.defaultID))?.permissions ?? []
171+
expect(
172+
Permission.evaluate("external_directory", path.join(global.data, "shell", "*", "*"), permissions).effect,
173+
).toBe("allow")
174+
expect(
175+
Permission.evaluate("external_directory", path.join(global.data, "tool-output", "*"), permissions).effect,
176+
).toBe("allow")
177+
expect(Permission.evaluate("external_directory", path.join(global.config, "*"), permissions).effect).toBe("allow")
178+
expect(Permission.evaluate("external_directory", path.join(global.tmp, "*"), permissions).effect).toBe("allow")
179+
const explore = yield* agent.get(Agent.ID.make("explore"))
180+
expect(Permission.evaluate("read", ".env", explore?.permissions ?? []).effect).toBe("ask")
181+
expect(Permission.evaluate("read", ".env.local", explore?.permissions ?? []).effect).toBe("ask")
182+
expect(Permission.evaluate("read", ".env.example", explore?.permissions ?? []).effect).toBe("allow")
183+
expect(Permission.evaluate("read", "src/index.ts", explore?.permissions ?? []).effect).toBe("allow")
155184
for (const item of agents) {
156185
expect(item.permissions.some((rule) => rule.action === "bash" && rule.effect !== "deny")).toBe(false)
157186
}
@@ -165,11 +194,6 @@ describe("Agent", () => {
165194
host({
166195
agent: agentHost(agent),
167196
}),
168-
).pipe(
169-
Effect.provideService(
170-
Location.Service,
171-
Location.Service.of(location({ directory: AbsolutePath.make("/project") })),
172-
),
173197
)
174198

175199
yield* Effect.forEach(["general", "explore"], (id) =>

0 commit comments

Comments
 (0)