Skip to content

Commit 49434c9

Browse files
authored
Merge pull request #23 from d3cker/codex/repository-file-permissions
Add per-repository automatic file approvals
2 parents f437f32 + 42d5847 commit 49434c9

22 files changed

Lines changed: 274 additions & 25 deletions

‎AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ steps, project setup, headless operation, and removal.
2727
| --- | --- | --- |
2828
| [docs/architecture.md](docs/architecture.md) | Component responsibilities, a short issue-to-PR overview, configuration ownership, scheduler ownership, and shared state. | Start here to understand how the system is divided before locating implementation code. |
2929
| [docs/bot-workflow.md](docs/bot-workflow.md) | Eight Mermaid diagrams and detailed implementation notes: startup and polling; discovery and routing; task phases; sessions and questions; media helpers; verification and publication; feedback, merging, and tab closure; status, retries, and recovery. Includes links to the source for each area. | Use for exact execution order, state transitions, checkpoint behavior, failure paths, and tracing a bot task from issue to merged PR. |
30-
| [docs/configuration.md](docs/configuration.md) | The standard `.opencode/automation.json` format, defaults, setup flags, configuration tracking across Git branches, authors, triggers, checks, base branches, model capabilities, media helpers, custom prompts, signatures, and auto-merge settings. | Use when adding or changing user-facing configuration, defaults, or setup examples. |
30+
| [docs/configuration.md](docs/configuration.md) | The standard `.opencode/automation.json` format, defaults, setup flags, configuration tracking across Git branches, repository file approvals, authors, triggers, checks, base branches, model capabilities, media helpers, custom prompts, signatures, and auto-merge settings. | Use when adding or changing user-facing configuration, defaults, or setup examples. |
3131
| [docs/runtime.md](docs/runtime.md) | User-visible behavior while the bot runs: GitHub questions and permission replies, branch selection, media inputs, prompt loading, follow-up comments, session tabs, runtime sidebar/status freshness, host repository inventory and discovery, local task closure, cancelling rounds while retaining tracking, and routine management commands. | Use when changing issue conversations, session continuation, runtime tools, or TUI behavior. |
3232
| [docs/advanced.md](docs/advanced.md) | Separate scheduler/dispatcher setup, multiple repositories, custom RPC jobs, full options, timeouts, management and retry commands, persistence, reconciliation, locks, and known limits. | Use for low-level configuration, operational troubleshooting, recovery, or ownership/concurrency changes. |
3333
| [docs/installation.md](docs/installation.md) | Loader registration, config-directory precedence, prerequisites, source installation, project-local installation, upgrade conflicts, testing on another machine, and migration limits. | Use when working on packaging, installers, registration, upgrades, or deployment troubleshooting. |
@@ -72,6 +72,8 @@ the installation block without making remote writes. Keep its markers intact.
7272
- `src/runtime.ts`, `src/worker.ts`, and `src/bridge.ts` implement worker hooks,
7373
runtime installation, and communication with the owner. `src/prompt.ts` loads
7474
instructions; `prompts/bot.md` contains the bundled bot instructions.
75+
`src/repository-permissions.ts` checks canonical path boundaries for opt-in
76+
repository file approvals; `src/runtime.ts` applies them only to bot sessions.
7577
- `src/tui.ts`, `src/ui.ts`, and `src/activity.ts` implement terminal integration
7678
and task activity. `src/sidebar.ts` renders the runtime panel;
7779
`src/runtime-panel.ts` owns polling, freshness and presentation;

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ include the full version, for example `## 0.7.0-beta.1`.
4747

4848
### Added
4949

50+
- Per-repository `autoApproveRepositoryFiles` configuration, an opt-in setup
51+
prompt, and `init --auto-approve-repository-files`. Bot sessions and native
52+
workers can automatically access files in the repository and assigned worktree
53+
across rounds/restarts, without global permission changes. Explicit denials,
54+
pending questions, media-helper limits, and shell permissions remain unchanged.
55+
5056
- Cancel a single bot round while retaining issue/PR tracking, with durable stop
5157
recovery, preserved draft worktrees and fresh worktrees for later feedback.
5258
Resume tracking a locally closed task without replaying its abandoned round

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ and respects `XDG_CONFIG_HOME` and `OPENCODE_CONFIG_DIR`.
9898
3. Load the project with the [headless command below](#run-without-the-tui), or
9999
open it with `opencode2 /absolute/path/to/your-project`.
100100

101+
The wizard can enable automatic file access within this repository and its bot
102+
worktrees. Choose **yes** at the file-access prompt, or pass
103+
`--auto-approve-repository-files` to `init`. Existing projects can set
104+
`"autoApproveRepositoryFiles": true` in their configuration. This does not change
105+
global permissions or approve arbitrary shell commands. See
106+
[repository file approvals](docs/configuration.md#repository-file-approvals) for
107+
scope, restart steps, and pending questions.
108+
101109
Settings are saved to `/absolute/path/to/your-project/.opencode/automation.json`.
102110
If it already exists, edit it directly and skip `init`. Repeat setup for each
103111
repository; the plugin is installed only once. After editing settings, restart

‎docs/advanced.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ OpenCode service must be running for polling to work.
2929
| `ownerDirectory` | Absolute path of the checkout that owns automation. Worker worktrees do not activate another scheduler. |
3030
| `stateDirectory` | Shared location for queues, locks, and worktrees. Keep it consistent across components and restarts. |
3131
| `repositories` | Repositories with existing local checkouts, default base branches, allowed authors, and checks. A natural-language request can override the base before work starts. |
32+
| `repositories[].autoApproveRepositoryFiles` | Opt-in file-access approval for that repository and the assigned task worktree, including worktrees outside the checkout. Defaults off; does not approve shell commands. |
3233
| `allowedAuthors` | GitHub users authorized to request work and approve merging. Merging also requires repository write access. |
3334
| `checks` | Arrays of executable arguments, e.g. `[["npm", "test"]]`. `[]` skips dispatcher test commands; the PR distinguishes this from agent-reported tests. No implicit shell. |
3435
| `routes` | Maps full mentions to agents and models available in OpenCode. |
@@ -47,8 +48,9 @@ OpenCode service must be running for polling to work.
4748
Other plugins can expose idempotent RPC methods for custom scheduler jobs. A
4849
transport timeout does not prove the server never executed a request.
4950

50-
The executor uses the configured OpenCode permissions. Interactive permission
51-
requests are posted to the issue and suspend the task. An authorized author must
51+
The executor uses the configured OpenCode permissions. The optional repository
52+
file policy handles eligible `ask` decisions first; see
53+
[repository file approvals](configuration.md#repository-file-approvals). Remaining requests are posted to the issue and suspend the task. An authorized author must
5254
reply with the exact `/allow QUESTION_ID` or `/deny QUESTION_ID` command. Explicit
5355
OpenCode deny rules remain. Install project dependencies before running it or
5456
include suitable setup commands in your checks.

‎docs/architecture.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ The easy setup writes a per-project `.opencode/automation.json`. Global plugin
6363
loaders remain inactive in projects without configuration. Account defaults come
6464
from GitHub authentication, while the wizard queries OpenCode for a model default.
6565
User-configured values are preserved rather than replaced during upgrades.
66+
An opt-in repository file-access policy is stored in that project's configuration
67+
and passed to its worktree runtime. Permission hooks apply it to associated bot
68+
sessions and native workers using canonical paths, while retaining explicit
69+
denials, shell rules, and the media helper's no-tools restriction.
6670

6771
The primary checkout owns scheduling. Worker worktrees do not start additional
6872
schedulers. A shared Git-directory state folder holds the queue and locks; separate

‎docs/bot-workflow.md‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ flowchart TD
2222
Owner -->|Yes| Config[Use nonempty plugin options or read .opencode/automation.json]
2323
Config -->|No project config| Inactive
2424
Config --> Register[Register primary checkout in local user inventory without activating other owners]
25-
Register --> Resolve[Validate settings and resolve GitHub auth, routes and defaults]
25+
Register --> Resolve[Validate repository file policy and resolve GitHub auth, routes and defaults]
2626
Resolve --> Metadata[Register resolved repositories and base branches]
2727
Metadata --> GH[Acquire github lock and load queue.json]
2828
GH --> RPC[Register runtime bridge and dispatcher RPC]
@@ -297,7 +297,20 @@ sequenceDiagram
297297
end
298298
end
299299
D->>S: Wait for completion
300-
opt Clarification or permission required
300+
opt OpenCode permission evaluation
301+
S->>R: Permission action, resources and current effect
302+
alt Explicit OpenCode allow or deny
303+
R-->>S: Preserve effect
304+
else Exact saved decision for this main session
305+
R-->>S: Apply saved allow or deny
306+
else Repo file policy enabled, eligible action and all paths inside repo or task worktree
307+
Note over R: Resolve canonical paths, exclude media helpers and unanswered questions
308+
R-->>S: Allow file access without an issue question
309+
else Permission still needs approval
310+
R-->>S: Deny this attempt and use the question flow below
311+
end
312+
end
313+
opt Clarification or remaining permission question
301314
S->>R: ask_issue / intercepted question / permission ask
302315
R->>D: Register against main task session
303316
D->>D: Persist pending question
@@ -357,6 +370,13 @@ sequenceDiagram
357370
phases or enforced review gates. The executor's `verifying` phase remains
358371
separate. A prose blocker in the final summary does not set `blocked` status;
359372
user-input blockers must go through `ask_issue`.
373+
- `autoApproveRepositoryFiles` is a repository opt-in, carried in the generated
374+
worktree runtime settings. It handles `external_directory`, `read`, and `edit`
375+
requests for canonical paths within the configured checkout or assigned
376+
worktree. Native workers inherit task association through parent lookup; new
377+
rounds use the same repository policy. Explicit denials, pending questions,
378+
media-helper restrictions, and shell rules are unchanged. See
379+
[configuration and reload behavior](configuration.md#repository-file-approvals).
360380
- One unresolved question is retained at a time. Runtime hooks remove tools and
361381
reject non-question tool execution while a question is pending. Native subagent
362382
questions are attached to the main task; the reply resumes the main session.
@@ -389,7 +409,7 @@ sequenceDiagram
389409
dispatcher advances to `verifying`.
390410

391411
Sources: [executor.ts — runSession](../src/executor.ts),
392-
[runtime.ts](../src/runtime.ts), [prompt.ts](../src/prompt.ts),
412+
[runtime.ts](../src/runtime.ts), [repository-permissions.ts](../src/repository-permissions.ts), [prompt.ts](../src/prompt.ts),
393413
[dispatcher.ts — workOnce, question, publishQuestion, restartWorkflow](../src/dispatcher.ts).
394414

395415
## 5. Optional media inspection

‎docs/configuration.md‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Use a model available in your own OpenCode 2 installation. Optional fields:
5353

5454
| Field | Purpose |
5555
| --- | --- |
56+
| `autoApproveRepositoryFiles` | Opt-in automatic `external_directory`, `read`, and `edit` approval within this repository and its assigned task worktree; omitted/`false` keeps existing permission behavior. Shell rules are unchanged. |
5657
| `baseBranch` | Base for new worktrees and PRs; defaults to the GitHub default branch. |
5758
| `capabilities` | Main model support: `text`, `vision`, `audio`; defaults to `["text"]`. |
5859
| `mediaModel` | Separate helper model and its capabilities; example below. |
@@ -80,11 +81,53 @@ cd /absolute/path/to/your-project
8081
"$HOME/.local/bin/opencode2-automation" init --model provider/model --skip-tests --yes
8182
```
8283

83-
Optional flags: `--base-branch develop`, `--capabilities text`,
84+
Optional flags: `--auto-approve-repository-files`, `--base-branch develop`, `--capabilities text`,
8485
`--media-model provider/vision-model`, `--media-capabilities text,vision`,
8586
`--system-prompt .opencode/bot.md`. With `--yes`, supply a helper explicitly
8687
if you want media support with a text-only main model.
8788

89+
## Repository file approvals
90+
91+
During interactive `init`, choose **yes** for "Automatically approve file access
92+
in this repository and its task worktrees". The default is **no**. Noninteractive
93+
setup opts in with `--auto-approve-repository-files`; `--yes` alone does not enable it.
94+
For an existing project, add this field to its existing `.opencode/automation.json`
95+
without rerunning `init` or replacing the other settings:
96+
97+
```json
98+
{
99+
"model": "provider/model",
100+
"autoApproveRepositoryFiles": true
101+
}
102+
```
103+
104+
This is a plugin setting for this repository, not a global OpenCode permission.
105+
The runtime resolves the configured checkout path automatically, and also includes
106+
the assigned task worktree when advanced state storage places it outside the
107+
checkout. The policy follows new rounds and native subagents through their main
108+
task; it is not tied to a previous session's `/allow` reply. Other repositories
109+
and ordinary non-bot sessions are unaffected. No user-wide configuration is written.
110+
111+
Only `ask` decisions for `external_directory`, `read`, and `edit` qualify. Every
112+
resource must resolve inside the repository or assigned worktree. Symlink targets
113+
are checked, including existing parents of new files. Paths to siblings, symlink
114+
escapes, unknown patterns, and unresolvable boundaries use the normal approval
115+
flow. Explicit OpenCode denials and exact saved `/deny` decisions remain effective.
116+
Media helpers remain read-only and cannot use tools. Shell, network, subagent
117+
launch, and other action permissions are unchanged; a shell command can affect
118+
files outside its working directory, so its location does not grant blanket consent.
119+
120+
After changing the setting, restart the idle service and activate the owner again.
121+
The executor refreshes generated worktree runtime settings before continuing a
122+
saved session. An already-pending permission question still needs its exact
123+
`/allow QUESTION_ID` or `/deny QUESTION_ID` reply; enabling this option does not
124+
answer it or bypass unrelated pending questions. Existing configs remain opt-out.
125+
Set the field to `false` and reload to disable automatic file approval; previously
126+
saved explicit approvals still have their original session scope.
127+
128+
The model must still build, edit, and test in its assigned worktree. Permission to
129+
access another checkout does not make that checkout the correct validation target.
130+
88131
## Questions, branches, media, and bot instructions
89132

90133
- **Questions:** reply in the issue as an account in `authors`; no repeated

‎docs/installation.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ GitHub user, not the repository owner. The model default is queried from the run
7878
OpenCode service; without it, the model is required. Command-line flags override
7979
prompts. `--skip-tests` explicitly disables tests; Enter otherwise accepts the
8080
shown test command or `skip`.
81-
The wizard also asks for model capabilities, a vision helper if the main model
81+
The wizard also asks whether to auto-approve repository file access (default no),
82+
plus model capabilities, a vision helper if the main model
8283
lacks vision, and the base branch. Existing JSON files can be extended manually;
8384
see [runtime settings](runtime.md).
8485

‎docs/runtime.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ not by excluding the posting account's login. GitHub Bot accounts and unauthoriz
3535
authors are also excluded. A regular comment from the shared account can answer
3636
a question; the bot's own marked question, acknowledgement, or other post cannot.
3737

38-
For permission requests, use the exact `/allow QUESTION_ID` or `/deny QUESTION_ID`
38+
When `autoApproveRepositoryFiles` is enabled for the repository, the runtime
39+
approves eligible file-access requests within the checkout and assigned worktree,
40+
including native subagents and later rounds. It still honors explicit denials and
41+
does not grant shell permissions or tools to media helpers. A new session does not
42+
need to repeat `/allow` for these eligible file requests. Existing unanswered
43+
questions still require their explicit replies. See
44+
[repository file approvals](configuration.md#repository-file-approvals).
45+
46+
For permission requests that still require a reply, use the exact `/allow QUESTION_ID` or `/deny QUESTION_ID`
3947
shown in the question as your entire reply. Plain conversation does not grant
4048
permission. The decision is scoped to the operation and resource set in the
4149
current main session and its workers; explicit OpenCode deny rules still apply.

‎prompts/bot.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ repository inspection in the implementation session.
3636
- Preserve existing work, including changes from earlier rounds. Inspect the
3737
current state before editing; do not assume a fresh checkout.
3838
- Work only in the assigned worktree and retain its branch and pinned base.
39+
"Repository root" means that worktree's root for edits, builds, and tests.
40+
Do not substitute the primary checkout's build or executable. Automatic file
41+
permission within the repository does not change the assigned worktree.
3942
- Do not switch branches, push, merge, open PRs, or post directly to GitHub.
4043
The dispatcher owns publication and appends the configured message signature.
4144
- Do not change automation configuration, credentials, or permissions merely
@@ -57,6 +60,9 @@ repository inspection in the implementation session.
5760
verification, or delegation while waiting.
5861
- A delegated worker that asks must return control to the main agent.
5962
The dispatcher delivers the reply to the main session.
63+
- A repository may enable automatic file-access approval for its checkout and
64+
assigned worktree. The runtime applies it; do not ask again for an operation
65+
it already permits. This does not grant blanket shell approval or bypass denials.
6066
- If permission approval is pending, stop. Never bypass a denied operation.
6167
Approval requires an authorized user's exact `/allow QUESTION_ID` or
6268
`/deny QUESTION_ID` reply in the issue. Never supply that approval yourself.

0 commit comments

Comments
 (0)