GeezerVison is showing github.com — Add agent-level permissions with whitelist/blacklist support (#1862) · meatcar/opencode@10735f9
File treeExpand file treeCollapse file tree Open diff view settings web/src/content/docs/docs
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +name: Duplicate Issue Detection | | 2 | + | | 3 | +on: | | 4 | + issues: | | 5 | + types: [opened] | | 6 | + | | 7 | +jobs: | | 8 | + check-duplicates: | | 9 | + runs-on: ubuntu-latest | | 10 | + permissions: | | 11 | + contents: read | | 12 | + issues: write | | 13 | + id-token: write | | 14 | + steps: | | 15 | + - name: Checkout repository | | 16 | + uses: actions/checkout@v4 | | 17 | + with: | | 18 | + fetch-depth: 1 | | 19 | + | | 20 | + - name: Install opencode | | 21 | + run: curl -fsSL https://opencode.ai/install | bash | | 22 | + | | 23 | + - name: Check for duplicate issues | | 24 | + env: | | 25 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} | | 26 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | | 27 | + run: | | | 28 | + opencode run --agent github -m anthropic/claude-sonnet-4-20250514 "A new issue has been created: '${{ github.event.issue.title }}' | | 29 | +
| | 30 | + Issue body: | | 31 | + ${{ github.event.issue.body }} | | 32 | +
| | 33 | + Please search through existing issues in this repository to find any potential duplicates of this new issue. Consider: | | 34 | + 1. Similar titles or descriptions | | 35 | + 2. Same error messages or symptoms | | 36 | + 3. Related functionality or components | | 37 | + 4. Similar feature requests | | 38 | +
| | 39 | + If you find any potential duplicates, please comment on the new issue with: | | 40 | + - A brief explanation of why it might be a duplicate | | 41 | + - Links to the potentially duplicate issues | | 42 | + - A suggestion to check those issues first | | 43 | +
| | 44 | + Use this format for the comment: | | 45 | + '👋 This issue might be a duplicate of existing issues. Please check: | | 46 | + - #[issue_number]: [brief description of similarity] | | 47 | +
| | 48 | + If none of these address your specific case, please let us know how this issue differs.' | | 49 | +
| | 50 | + If no clear duplicates are found, do not comment." |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +name: Guidelines Check | | 2 | + | | 3 | +on: | | 4 | + pull_request: | | 5 | + types: [opened, synchronize] | | 6 | + | | 7 | +jobs: | | 8 | + check-guidelines: | | 9 | + runs-on: ubuntu-latest | | 10 | + permissions: | | 11 | + contents: read | | 12 | + pull-requests: write | | 13 | + id-token: write | | 14 | + steps: | | 15 | + - name: Checkout repository | | 16 | + uses: actions/checkout@v4 | | 17 | + with: | | 18 | + fetch-depth: 1 | | 19 | + | | 20 | + - name: Install opencode | | 21 | + run: curl -fsSL https://opencode.ai/install | bash | | 22 | + | | 23 | + - name: Check PR guidelines compliance | | 24 | + env: | | 25 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} | | 26 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | | 27 | + run: | | | 28 | + opencode run --agent github -m anthropic/claude-sonnet-4-20250514 "A new pull request has been created: '${{ github.event.pull_request.title }}' | | 29 | +
| | 30 | + PR description: | | 31 | + ${{ github.event.pull_request.body }} | | 32 | +
| | 33 | + Please check all the code changes in this pull request against the guidelines in AGENTS.md file in this repository. | | 34 | +
| | 35 | + For each violation you find, create a file comment using the gh CLI. Use this exact format for each violation: | | 36 | +
| | 37 | + \`\`\`bash | | 38 | + gh pr review ${{ github.event.pull_request.number }} --comment-body 'This violates the AGENTS.md guideline: [specific rule]. Consider: [suggestion]' --file 'path/to/file.ts' --line [line_number] | | 39 | + \`\`\` | | 40 | +
| | 41 | + When possible, also submit code change suggestions using: | | 42 | +
| | 43 | + \`\`\`bash | | 44 | + gh pr review ${{ github.event.pull_request.number }} --comment-body 'Suggested fix for AGENTS.md guideline violation:' --file 'path/to/file.ts' --line [line_number] --body '```suggestion | | 45 | + [corrected code here] | | 46 | + ```' | | 47 | + \`\`\` | | 48 | +
| | 49 | + Only create comments for actual violations. If the code follows all guidelines, don't run any gh commands." |
Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +permission: | | 3 | + bash: | | 4 | + "*": "deny" | | 5 | + "gh*": "allow" | | 6 | +mode: subagent | | 7 | +--- | | 8 | + | | 9 | +You are running in github actions, typically to evaluate a PR. Do not do | | 10 | +anything that is outside the scope of that. You have access to the bash tool but | | 11 | +you can only run `gh` cli commands with it. | | 12 | + | | 13 | +Diffs are important but be sure to read the whole file to get the full context. |
Collapse file Copy file name to clipboardExpand all lines: opencode.json -3Lines changed: 0 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | { | 2 | 2 | "$schema": "https://opencode.ai/config.json", | 3 | | - "agent": { | 4 | | - "build": {} | 5 | | - }, | 6 | 3 | "mcp": { | 7 | 4 | "context7": { | 8 | 5 | "type": "remote", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/agent/agent.ts +48-9Lines changed: 48 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ import { Provider } from "../provider/provider" | 5 | 5 | import { generateObject, type ModelMessage } from "ai" | 6 | 6 | import PROMPT_GENERATE from "./generate.txt" | 7 | 7 | import { SystemPrompt } from "../session/system" | | 8 | +import { mergeDeep } from "remeda" | 8 | 9 |
| 9 | 10 | export namespace Agent { | 10 | 11 | export const Info = z | @@ -14,6 +15,11 @@ export namespace Agent { | 14 | 15 | mode: z.union([z.literal("subagent"), z.literal("primary"), z.literal("all")]), | 15 | 16 | topP: z.number().optional(), | 16 | 17 | temperature: z.number().optional(), | | 18 | + permission: z.object({ | | 19 | + edit: Config.Permission, | | 20 | + bash: z.record(z.string(), Config.Permission), | | 21 | + webfetch: Config.Permission.optional(), | | 22 | + }), | 17 | 23 | model: z | 18 | 24 | .object({ | 19 | 25 | modelID: z.string(), | @@ -31,6 +37,13 @@ export namespace Agent { | 31 | 37 |
| 32 | 38 | const state = App.state("agent", async () => { | 33 | 39 | const cfg = await Config.get() | | 40 | + const defaultPermission: Info["permission"] = { | | 41 | + edit: "allow", | | 42 | + bash: { | | 43 | + "*": "allow", | | 44 | + }, | | 45 | + webfetch: "allow", | | 46 | + } | 34 | 47 | const result: Record<string, Info> = { | 35 | 48 | general: { | 36 | 49 | name: "general", | @@ -41,17 +54,20 @@ export namespace Agent { | 41 | 54 | todowrite: false, | 42 | 55 | }, | 43 | 56 | options: {}, | | 57 | + permission: defaultPermission, | 44 | 58 | mode: "subagent", | 45 | 59 | }, | 46 | 60 | build: { | 47 | 61 | name: "build", | 48 | 62 | tools: {}, | 49 | 63 | options: {}, | | 64 | + permission: defaultPermission, | 50 | 65 | mode: "primary", | 51 | 66 | }, | 52 | 67 | plan: { | 53 | 68 | name: "plan", | 54 | 69 | options: {}, | | 70 | + permission: defaultPermission, | 55 | 71 | tools: { | 56 | 72 | write: false, | 57 | 73 | edit: false, | @@ -70,25 +86,48 @@ export namespace Agent { | 70 | 86 | item = result[key] = { | 71 | 87 | name: key, | 72 | 88 | mode: "all", | | 89 | + permission: defaultPermission, | 73 | 90 | options: {}, | 74 | 91 | tools: {}, | 75 | 92 | } | 76 | | - const { model, prompt, tools, description, temperature, top_p, mode, ...extra } = value | | 93 | + const { model, prompt, tools, description, temperature, top_p, mode, permission, ...extra } = value | 77 | 94 | item.options = { | 78 | 95 | ...item.options, | 79 | 96 | ...extra, | 80 | 97 | } | 81 | | - if (value.model) item.model = Provider.parseModel(value.model) | 82 | | - if (value.prompt) item.prompt = value.prompt | 83 | | - if (value.tools) | | 98 | + if (model) item.model = Provider.parseModel(model) | | 99 | + if (prompt) item.prompt = prompt | | 100 | + if (tools) | 84 | 101 | item.tools = { | 85 | 102 | ...item.tools, | 86 | | - ...value.tools, | | 103 | + ...tools, | 87 | 104 | } | 88 | | - if (value.description) item.description = value.description | 89 | | - if (value.temperature != undefined) item.temperature = value.temperature | 90 | | - if (value.top_p != undefined) item.topP = value.top_p | 91 | | - if (value.mode) item.mode = value.mode | | 105 | + if (description) item.description = description | | 106 | + if (temperature != undefined) item.temperature = temperature | | 107 | + if (top_p != undefined) item.topP = top_p | | 108 | + if (mode) item.mode = mode | | 109 | + | | 110 | + if (permission ?? cfg.permission) { | | 111 | + const merged = mergeDeep(cfg.permission ?? {}, permission ?? {}) | | 112 | + if (merged.edit) item.permission.edit = merged.edit | | 113 | + if (merged.webfetch) item.permission.webfetch = merged.webfetch | | 114 | + if (merged.bash) { | | 115 | + if (typeof merged.bash === "string") { | | 116 | + item.permission.bash = { | | 117 | + "*": merged.bash, | | 118 | + } | | 119 | + } | | 120 | + // if granular permissions are provided, default to "ask" | | 121 | + if (typeof merged.bash === "object") { | | 122 | + item.permission.bash = mergeDeep( | | 123 | + { | | 124 | + "*": "ask", | | 125 | + }, | | 126 | + merged.bash, | | 127 | + ) | | 128 | + } | | 129 | + } | | 130 | + } | 92 | 131 | } | 93 | 132 | return result | 94 | 133 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -164,6 +164,9 @@ export namespace Config { | 164 | 164 | export const Mcp = z.discriminatedUnion("type", [McpLocal, McpRemote]) | 165 | 165 | export type Mcp = z.infer<typeof Mcp> | 166 | 166 |
| | 167 | + export const Permission = z.union([z.literal("ask"), z.literal("allow"), z.literal("deny")]) | | 168 | + export type Permission = z.infer<typeof Permission> | | 169 | + | 167 | 170 | export const Agent = z | 168 | 171 | .object({ | 169 | 172 | model: z.string().optional(), | @@ -174,6 +177,13 @@ export namespace Config { | 174 | 177 | disable: z.boolean().optional(), | 175 | 178 | description: z.string().optional().describe("Description of when to use the agent"), | 176 | 179 | mode: z.union([z.literal("subagent"), z.literal("primary"), z.literal("all")]).optional(), | | 180 | + permission: z | | 181 | + .object({ | | 182 | + edit: Permission.optional(), | | 183 | + bash: z.union([Permission, z.record(z.string(), Permission)]).optional(), | | 184 | + webfetch: Permission.optional(), | | 185 | + }) | | 186 | + .optional(), | 177 | 187 | }) | 178 | 188 | .catchall(z.any()) | 179 | 189 | .openapi({ | @@ -243,9 +253,6 @@ export namespace Config { | 243 | 253 | }) | 244 | 254 | export type Layout = z.infer<typeof Layout> | 245 | 255 |
| 246 | | - export const Permission = z.union([z.literal("ask"), z.literal("allow"), z.literal("deny")]) | 247 | | - export type Permission = z.infer<typeof Permission> | 248 | | - | 249 | 256 | export const Info = z | 250 | 257 | .object({ | 251 | 258 | $schema: z.string().optional().describe("JSON schema reference for configuration validation"), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/index.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,9 @@ import { GithubCommand } from "./cli/cmd/github" | 21 | 21 |
| 22 | 22 | const cancel = new AbortController() | 23 | 23 |
| | 24 | +try { | | 25 | +} catch (e) {} | | 26 | + | 24 | 27 | process.on("unhandledRejection", (e) => { | 25 | 28 | Log.Default.error("rejection", { | 26 | 29 | e: e instanceof Error ? e.message : e, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/permission/index.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -155,7 +155,7 @@ export namespace Permission { | 155 | 155 | public readonly permissionID: string, | 156 | 156 | public readonly toolCallID?: string, | 157 | 157 | ) { | 158 | | - super(`The user rejected permission to use this functionality`) | | 158 | + super(`The user rejected permission to use this specific tool call. You may try again with different parameters.`) | 159 | 159 | } | 160 | 160 | } | 161 | 161 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/index.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -523,6 +523,7 @@ export namespace Session { | 523 | 523 | t.execute(args, { | 524 | 524 | sessionID: input.sessionID, | 525 | 525 | abort: new AbortController().signal, | | 526 | + agent: agent.name, | 526 | 527 | messageID: userMsg.id, | 527 | 528 | metadata: async () => {}, | 528 | 529 | }), | @@ -765,7 +766,7 @@ export namespace Session { | 765 | 766 |
| 766 | 767 | const enabledTools = pipe( | 767 | 768 | agent.tools, | 768 | | - mergeDeep(await ToolRegistry.enabled(input.providerID, input.modelID)), | | 769 | + mergeDeep(await ToolRegistry.enabled(input.providerID, input.modelID, agent)), | 769 | 770 | mergeDeep(input.tools ?? {}), | 770 | 771 | ) | 771 | 772 | for (const item of await ToolRegistry.tools(input.providerID, input.modelID)) { | @@ -791,6 +792,7 @@ export namespace Session { | 791 | 792 | abort: options.abortSignal!, | 792 | 793 | messageID: assistantMsg.id, | 793 | 794 | callID: options.toolCallId, | | 795 | + agent: agent.name, | 794 | 796 | metadata: async (val) => { | 795 | 797 | const match = processor.partFromToolCall(options.toolCallId) | 796 | 798 | if (match && match.state.status === "running") { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/bash.ts +4-23Lines changed: 4 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,12 +5,12 @@ import { Tool } from "./tool" | 5 | 5 | import DESCRIPTION from "./bash.txt" | 6 | 6 | import { App } from "../app/app" | 7 | 7 | import { Permission } from "../permission" | 8 | | -import { Config } from "../config/config" | 9 | 8 | import { Filesystem } from "../util/filesystem" | 10 | 9 | import { lazy } from "../util/lazy" | 11 | 10 | import { Log } from "../util/log" | 12 | 11 | import { Wildcard } from "../util/wildcard" | 13 | 12 | import { $ } from "bun" | | 13 | +import { Agent } from "../agent/agent" | 14 | 14 |
| 15 | 15 | const MAX_OUTPUT_LENGTH = 30000 | 16 | 16 | const DEFAULT_TIMEOUT = 1 * 60 * 1000 | @@ -40,20 +40,8 @@ export const BashTool = Tool.define("bash", { | 40 | 40 | async execute(params, ctx) { | 41 | 41 | const timeout = Math.min(params.timeout ?? DEFAULT_TIMEOUT, MAX_TIMEOUT) | 42 | 42 | const app = App.info() | 43 | | - const cfg = await Config.get() | 44 | 43 | const tree = await parser().then((p) => p.parse(params.command)) | 45 | | - const permissions = (() => { | 46 | | - const value = cfg.permission?.bash | 47 | | - if (!value) | 48 | | - return { | 49 | | - "*": "allow", | 50 | | - } | 51 | | - if (typeof value === "string") | 52 | | - return { | 53 | | - "*": value, | 54 | | - } | 55 | | - return value | 56 | | - })() | | 44 | + const permissions = await Agent.get(ctx.agent).then((x) => x.permission.bash) | 57 | 45 |
| 58 | 46 | let needsAsk = false | 59 | 47 | for (const node of tree.rootNode.descendantsOfType("command")) { | @@ -93,17 +81,10 @@ export const BashTool = Tool.define("bash", { | 93 | 81 |
| 94 | 82 | // always allow cd if it passes above check | 95 | 83 | if (!needsAsk && command[0] !== "cd") { | 96 | | - const action = (() => { | 97 | | - for (const [pattern, value] of Object.entries(permissions)) { | 98 | | - const match = Wildcard.match(node.text, pattern) | 99 | | - log.info("checking", { text: node.text.trim(), pattern, match }) | 100 | | - if (match) return value | 101 | | - } | 102 | | - return "ask" | 103 | | - })() | | 84 | + const action = Wildcard.all(node.text, permissions) | 104 | 85 | if (action === "deny") { | 105 | 86 | throw new Error( | 106 | | - "The user has specifically restricted access to this command, you are not allowed to execute it.", | | 87 | + `The user has specifically restricted access to this command, you are not allowed to execute it. Here is the configuration: ${JSON.stringify(permissions)}`, | 107 | 88 | ) | 108 | 89 | } | 109 | 90 | if (action === "ask") needsAsk = true | |
|