Skip to content

Commit 33e7f9b

Browse files
committed
feat: add safe agents and commands (#5)
1 parent 1fd290b commit 33e7f9b

10 files changed

Lines changed: 197 additions & 1 deletion

File tree

‎packages/guardrails/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ Current contents focus on the first thin-distribution slice:
4646
- managed enterprise defaults
4747
- packaged custom config dir profile
4848
- packaged plugin for runtime guardrail hooks
49-
- scenario coverage for managed config precedence, project-local asset compatibility, and plugin behavior
49+
- guarded `implement` and `review` agents plus packaged `/implement`, `/review`, `/ship`, and `/handoff` workflow commands
50+
- scenario coverage for managed config precedence, project-local asset compatibility, plugin behavior, and workflow safety defaults
5051

5152
Planned next slices are tracked in the fork:
5253

@@ -69,6 +70,8 @@ opencode-guardrails
6970

7071
It respects an existing `OPENCODE_CONFIG_DIR` so project- or environment-specific overrides can still replace the packaged profile when needed.
7172

73+
The packaged profile defaults to the `implement` agent. Review and release-readiness work should run through the packaged `/review`, `/ship`, and `/handoff` commands so the workflow stays read-only at the gate layer.
74+
7275
## Managed deployment
7376

7477
Copy [managed/opencode.json](/Users/teradakousuke/Developer/opencode/packages/guardrails/managed/opencode.json) into the system managed config directory:

‎packages/guardrails/profile/AGENTS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
- Push checks to the fastest reliable layer first, then fall back to command workflows and CI for authoritative gates.
99
- Keep project-local `.opencode` assets working; use them for repo-specific workflows instead of editing this profile unless the rule is organization-wide.
1010
- Treat `.opencode/guardrails/` as plugin-owned runtime state, not a manual editing surface.
11+
- Use `implement` as the guarded default primary agent. Route review, ship, and handoff work through the packaged `/review`, `/ship`, and `/handoff` commands instead of freeform release flows.
12+
- Keep review paths read-only. If a workflow needs edits, return to `implement` or a project-local implementation agent instead of widening the review agent.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
description: Default guarded implementation agent for internal development workflows.
3+
mode: primary
4+
permission:
5+
question: allow
6+
plan_enter: allow
7+
bash:
8+
"git checkout -- *": deny
9+
"git merge *": deny
10+
"git push --force*": deny
11+
"git push * --force*": deny
12+
"git reset --hard*": deny
13+
"gh pr merge *": deny
14+
---
15+
16+
Implement changes in bounded increments.
17+
18+
Use `/review`, `/ship`, and `/handoff` as explicit workflow gates instead of improvising release steps.
19+
20+
Before claiming completion:
21+
22+
- keep the change aligned to the requested scope
23+
- prefer profile, plugin, command, and config layers over core runtime patches
24+
- run the smallest relevant verification that proves the change works
25+
- call out remaining approvals, CI gates, and release blockers explicitly
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
description: Review changes, risks, and missing checks without editing files.
3+
mode: subagent
4+
permission:
5+
"*": deny
6+
read: allow
7+
grep: allow
8+
glob: allow
9+
list: allow
10+
lsp: allow
11+
skill: allow
12+
webfetch: ask
13+
bash:
14+
"*": deny
15+
"git diff *": allow
16+
"git status *": allow
17+
"git show *": allow
18+
"git log *": allow
19+
---
20+
21+
Review for regressions, missing validation, and missing verification.
22+
23+
Prefer file-backed findings with concrete evidence. Do not edit files. Call out missing tests and workflow gates when they matter to the change.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
description: Produce a structured handoff with guardrail state and pending gates.
3+
agent: review
4+
subtask: true
5+
---
6+
7+
Prepare a handoff for the current work.
8+
9+
Required sections:
10+
11+
- Goal
12+
- Constraints
13+
- Guardrail state
14+
- Files changed
15+
- Verification
16+
- Open risks
17+
- Next steps
18+
19+
Include any pending approvals, denied operations, provider restrictions, or release gates that still block completion.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
description: Implement a bounded change with the guarded primary agent.
3+
agent: implement
4+
---
5+
6+
Implement the requested change under the guardrail profile.
7+
8+
Requirements:
9+
10+
- keep the scope bounded to the stated goal
11+
- prefer upstream-compatible extension points over core runtime patches
12+
- run the smallest relevant verification before claiming completion
13+
- if review, ship, or handoff gates are needed, use `/review`, `/ship`, or `/handoff`
14+
15+
$ARGUMENTS
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
description: Review current work with the read-only guardrail review agent.
3+
agent: review
4+
subtask: true
5+
---
6+
7+
Review the current work for correctness, regressions, missing tests, and missing workflow gates.
8+
9+
Required sections:
10+
11+
- Findings
12+
- Verification
13+
- Open risks
14+
- Recommended next step
15+
16+
Default scope is the current uncommitted work unless `$ARGUMENTS` narrows it.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
description: Run a release-readiness gate without edit access.
3+
agent: review
4+
subtask: true
5+
---
6+
7+
Run a release-readiness check for the current work.
8+
9+
Required gates:
10+
11+
- the scope still matches the requested goal
12+
- relevant verification has been run and cited
13+
- risky shell or write operations did not bypass policy
14+
- remaining approvals, CI, provider, or review gates are listed explicitly
15+
16+
Output:
17+
18+
- Ready or Not ready
19+
- Evidence
20+
- Blocking gates
21+
- Next action
22+
23+
Default scope is the current uncommitted work unless `$ARGUMENTS` narrows it.

‎packages/guardrails/profile/opencode.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "https://opencode.ai/config.json",
3+
"default_agent": "implement",
34
"share": "disabled",
45
"server": {
56
"hostname": "127.0.0.1",
@@ -17,6 +18,12 @@
1718
"*": "ask",
1819
"rm -rf *": "deny",
1920
"sudo *": "deny",
21+
"git checkout -- *": "deny",
22+
"git merge *": "deny",
23+
"git push --force*": "deny",
24+
"git push * --force*": "deny",
25+
"git reset --hard*": "deny",
26+
"gh pr merge *": "deny",
2027
"curl * | sh*": "deny",
2128
"wget * | sh*": "deny"
2229
},

‎packages/opencode/test/scenario/guardrails.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path"
44
import { Agent } from "../../src/agent/agent"
55
import { Command } from "../../src/command"
66
import { Config } from "../../src/config/config"
7+
import { Permission } from "../../src/permission"
78
import { Plugin } from "../../src/plugin"
89
import { Instance } from "../../src/project/instance"
910
import { Skill } from "../../src/skill"
@@ -52,6 +53,11 @@ function wait(ms = 50) {
5253
return new Promise((done) => setTimeout(done, ms))
5354
}
5455

56+
function perm(agent: Agent.Info | undefined, key: string, pattern = "*") {
57+
if (!agent) return undefined
58+
return Permission.evaluate(key, pattern, agent.permission).action
59+
}
60+
5561
test("managed config overrides weaker project defaults", async () => {
5662
await using tmp = await tmpdir({
5763
git: true,
@@ -197,6 +203,63 @@ description: Project-local skill.
197203
})
198204
})
199205

206+
test("guardrail profile ships safe agents and workflow commands", async () => {
207+
await withProfile(async () => {
208+
await using tmp = await tmpdir({
209+
git: true,
210+
init: async (dir) => {
211+
await write(dir, "opencode.json", {
212+
$schema: "https://opencode.ai/config.json",
213+
share: "auto",
214+
})
215+
await Bun.write(
216+
path.join(dir, ".opencode", "commands", "project-local.md"),
217+
`---
218+
description: Project-local workflow.
219+
---
220+
221+
Use the project-local command.
222+
`,
223+
)
224+
},
225+
})
226+
227+
await Instance.provide({
228+
directory: tmp.path,
229+
fn: async () => {
230+
const cfg = await Config.get()
231+
const cmds = await Command.list()
232+
const impl = await Agent.get("implement")
233+
const review = await Agent.get("review")
234+
235+
expect(cfg.default_agent).toBe("implement")
236+
expect(await Agent.defaultAgent()).toBe("implement")
237+
expect(impl?.mode).toBe("primary")
238+
expect(review?.mode).toBe("subagent")
239+
expect(perm(impl, "question")).toBe("allow")
240+
expect(perm(impl, "plan_enter")).toBe("allow")
241+
expect(perm(impl, "edit")).toBe("ask")
242+
expect(perm(impl, "bash", "git reset --hard HEAD")).toBe("deny")
243+
expect(perm(impl, "bash", "git push origin --force-with-lease")).toBe("deny")
244+
expect(perm(review, "edit")).toBe("deny")
245+
expect(perm(review, "read")).toBe("allow")
246+
expect(perm(review, "bash", "git diff HEAD")).toBe("allow")
247+
expect(perm(review, "bash", "bun test")).toBe("deny")
248+
249+
const map = Object.fromEntries(cmds.map((item) => [item.name, item]))
250+
expect(map.implement?.agent).toBe("implement")
251+
expect(map.review?.agent).toBe("review")
252+
expect(map.review?.subtask).toBe(true)
253+
expect(map.ship?.agent).toBe("review")
254+
expect(map.ship?.subtask).toBe(true)
255+
expect(map.handoff?.agent).toBe("review")
256+
expect(map.handoff?.subtask).toBe(true)
257+
expect(map["project-local"]?.name).toBe("project-local")
258+
},
259+
})
260+
})
261+
})
262+
200263
test("guardrail profile plugin injects shell env and blocks protected files", async () => {
201264
await withProfile(async () => {
202265
await using tmp = await tmpdir({ git: true })

0 commit comments

Comments
 (0)