-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathloop.ts
More file actions
296 lines (262 loc) · 12.7 KB
/
Copy pathloop.ts
File metadata and controls
296 lines (262 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { resolveOpencodeToolOutputDir, resolveOpencodeTmpDir } from '../utils/opencode-paths'
import { isRecord } from '../utils/is-record'
import type { PluginConfig, LoopPermissionsConfig } from '../types'
export type PermissionRule = { permission: string; pattern: string; action: 'allow' | 'deny' }
/** Maximum number of plan sections a loop may execute. Shared by the
* deterministic decomposer, section bootstrap, the TUI inline-plan preview,
* and the plan-adjust cap so all four agree. */
export const MAX_TOTAL_SECTIONS = 24
/**
* Tools that author the session-scoped plan of record. Architect agents may
* call them; implementation, audit, splitter, and loop permission rules deny
* this one list rather than repeating the names.
*/
export const PLAN_AUTHORING_TOOL_NAMES = ['plan-write', 'plan-edit'] as const
/** Structural deny names shared by both loop and audit rulesets, in emit order. */
export const SHARED_STRUCTURAL_DENY_PERMISSIONS = [
'plan',
'plan_enter',
'plan_exit',
...PLAN_AUTHORING_TOOL_NAMES,
'execute-plan',
'execute-goal',
'question',
'loop-cancel',
'loop-status',
'launch-group',
'group-status',
'group-cancel',
] as const
/** Structural deny names exclusive to loop sessions (review tools). */
export const LOOP_ONLY_STRUCTURAL_DENY_PERMISSIONS = ['review-write', 'review-delete'] as const
/** Structural deny names exclusive to audit sessions (code-mutation tools). */
export const AUDIT_ONLY_STRUCTURAL_DENY_PERMISSIONS = ['edit', 'write', 'multiedit', 'apply_patch'] as const
/** Permissions config may not name: the blanket allow, the external_directory key, and
* every structural deny. Loop/audit rulesets are the only legal consumers of these. */
export const FORGE_MANAGED_PERMISSIONS: ReadonlySet<string> = new Set([
'*',
'external_directory',
...LOOP_ONLY_STRUCTURAL_DENY_PERMISSIONS,
...AUDIT_ONLY_STRUCTURAL_DENY_PERMISSIONS,
...SHARED_STRUCTURAL_DENY_PERMISSIONS,
])
/** Loop-protocol and core tools a loop cannot function without. Only a *blanket* deny (pattern `*`)
* of one of these is rejected: a loop that cannot read its findings, section plan, or
* plan-of-record — or cannot run `bash` or `read` at all — silently burns iterations to
* maxIterations with nothing pointing at the config. A scoped deny such as
* `{ permission: 'bash', pattern: 'git push *' }` is honoured. */
export const FORGE_REQUIRED_PERMISSIONS: ReadonlySet<string> = new Set([
'review-read',
'plan-read',
'section-read',
'plan-adjust',
'bash',
'read',
])
function denyRulesFor(names: readonly string[]): PermissionRule[] {
return names.map((permission) => ({ permission, pattern: '*', action: 'deny' as const }))
}
/** Normalizes one configured rule into a typed `PermissionRule`, or `null` if it must be dropped. */
function parseLoopPermissionEntry(index: number, raw: unknown, warnings: string[]): PermissionRule | null {
const drop = (): null => {
warnings.push(`loop.permissions.deny[${index}] is ignored: expected a tool name or { permission, pattern }`)
return null
}
if (typeof raw === 'string') {
const permission = raw.trim()
if (!permission) return drop()
return { permission, pattern: '*', action: 'deny' }
}
if (isRecord(raw)) {
const obj = raw as { permission?: unknown; pattern?: unknown }
if (typeof obj.permission !== 'string') return drop()
const permission = obj.permission.trim()
if (!permission) return drop()
if (obj.pattern !== undefined && typeof obj.pattern !== 'string') return drop()
const pattern = typeof obj.pattern === 'string' ? obj.pattern.trim() : ''
return { permission, pattern: pattern || '*', action: 'deny' }
}
return drop()
}
/**
* Parses the user-supplied `loop.permissions` config into typed rules plus warnings for dropped
* entries. Only `deny` entries are supported; a legacy `allow` array is ignored with a migration
* warning. Entries naming a Forge-managed permission are dropped, as are blanket (`*`) denies of a
* Forge-required permission. Returns the same `PermissionRule` shape used by the loop and audit
* rulesets, so parsed rules layer directly onto them.
*/
export function parseLoopPermissionRules(raw: unknown): { rules: PermissionRule[]; warnings: string[] } {
const warnings: string[] = []
if (raw === undefined || raw === null) return { rules: [], warnings }
if (!isRecord(raw)) {
warnings.push('loop.permissions is ignored: expected an object with a "deny" array')
return { rules: [], warnings }
}
const config = raw as LoopPermissionsConfig
const rules: PermissionRule[] = []
const seen = new Set<string>()
if (raw.allow !== undefined) {
warnings.push('loop.permissions.allow is ignored: only deny entries are supported')
}
if (config.deny !== undefined) {
if (!Array.isArray(config.deny)) {
warnings.push('loop.permissions.deny is ignored: expected an array')
} else {
for (let i = 0; i < config.deny.length; i++) {
const rule = parseLoopPermissionEntry(i, config.deny[i], warnings)
if (!rule) continue
if (rule.pattern === '*' && FORGE_REQUIRED_PERMISSIONS.has(rule.permission)) {
warnings.push(
`loop.permissions.deny entry "${rule.permission}" is ignored: a blanket deny of this tool breaks the loop — scope it with a pattern instead`,
)
continue
}
if (FORGE_MANAGED_PERMISSIONS.has(rule.permission)) {
const suffix =
rule.permission === 'external_directory'
? ' — use loop.allowExternalDirectories instead'
: ''
warnings.push(
`loop.permissions.deny entry "${rule.permission}" is ignored: Forge manages this permission for every loop and audit session${suffix}`,
)
continue
}
const signature = `${rule.permission}|${rule.pattern}|${rule.action}`
if (seen.has(signature)) continue
seen.add(signature)
rules.push(rule)
}
}
}
return { rules, warnings }
}
/** Resolves the parsed `loop.permissions` rules for a config, or an empty list when unset. */
export function resolveLoopPermissionRules(config: PluginConfig | undefined): PermissionRule[] {
return parseLoopPermissionRules(config?.loop?.permissions).rules
}
/**
* Resolves the full ruleset options (allowed directories plus configured rules) for a config.
* Single call every ruleset construction site uses so directories and configured rules can never
* diverge. `resolveLoopAllowedDirectories` remains the single source for the directory list.
*/
export function resolveLoopPermissionOptions(config: PluginConfig | undefined): LoopPermissionRulesetOptions {
return {
allowDirectories: resolveLoopAllowedDirectories(config),
extraRules: resolveLoopPermissionRules(config),
}
}
/**
* Resolves ruleset options for a remote loop launched from this machine: the
* configured rules without `allowDirectories`, because host-specific directory
* paths are meaningless on the remote machine. This asymmetry is documented at
* docs/configuration.md.
*/
export function resolveRemoteLoopPermissionOptions(config: PluginConfig | undefined): LoopPermissionRulesetOptions {
return { extraRules: resolveLoopPermissionRules(config) }
}
/** Collects warnings produced while parsing `loop.permissions` (dropped or ignored entries). */
export function collectLoopPermissionConfigWarnings(config: PluginConfig | undefined): string[] {
return parseLoopPermissionRules(config?.loop?.permissions).warnings
}
/**
* Resolves the user-configured external directories loop/audit sessions may access. Single source of
* truth so every permission-ruleset call site grants the same paths regardless of sandbox mode.
* (opencode's tool-output directory and its advertised temp directory are added separately inside
* the ruleset builder.)
*/
export function resolveLoopAllowedDirectories(config: PluginConfig | undefined): string[] {
return config?.loop?.allowExternalDirectories ?? []
}
export interface LoopPermissionRulesetOptions {
/**
* Absolute directory paths to grant access to via `external_directory` allow rules.
* These are layered AFTER the blanket `external_directory` deny so last-match-wins
* permission resolution grants access to these paths while keeping all others denied.
*/
allowDirectories?: string[]
/**
* User-configured rules (`loop.permissions`) inserted after the external-directory allow
* rules and before Forge's structural denies, so they can never override a structural deny.
*/
extraRules?: PermissionRule[]
}
/**
* Builds `external_directory` allow rules. Each directory produces two rules: an exact-path
* allow and a recursive (`/**`) allow.
*
* opencode's tool-output (truncation) directory and its advertised temp directory (`Global.Path.tmp`,
* which its shell-tool description presents as pre-approved) are always included: loop/audit sessions
* must be able to read spilled tool outputs and use the advertised scratch dir without prompting in the
* unattended loop. User-configured directories are layered on top. All are added AFTER the blanket
* `external_directory` deny so last-match-wins resolution grants access to these paths while all
* others stay denied.
*/
function buildExternalDirectoryAllowRules(allowDirectories: string[] = []): PermissionRule[] {
const rules: PermissionRule[] = []
const dirs = [resolveOpencodeToolOutputDir(), resolveOpencodeTmpDir(), ...allowDirectories]
for (const dir of dirs) {
if (typeof dir !== 'string') continue
const trimmed = dir.trim().replace(/\/+$/, '')
if (!trimmed) continue
rules.push({ permission: 'external_directory', pattern: trimmed, action: 'allow' })
rules.push({ permission: 'external_directory', pattern: `${trimmed}/**`, action: 'allow' })
}
return rules
}
/**
* Builds the permission ruleset for loop sessions.
*
* All loops use worktree isolation with a blanket allow-all, plus
* explicit deny rules for review tools, plan tools, and loop-management tools.
* External directory access is denied by default; opencode's tool-output directory (and any
* user-configured directories) are then allowed so spilled tool outputs remain readable.
*/
export function buildLoopPermissionRuleset(options: LoopPermissionRulesetOptions = {}): PermissionRule[] {
const rules: PermissionRule[] = []
// Blanket allow-all for worktree loops (isolated environment).
rules.push({ permission: '*', pattern: '*', action: 'allow' })
// External directory access is denied by default so loop work stays confined to
// the isolated worktree, regardless of whether shell commands run on the host
// or inside a sandbox container.
rules.push({
permission: 'external_directory',
pattern: '*',
action: 'deny',
})
// Allow rules layered after the deny so last-match-wins grants access: opencode's
// tool-output directory (always) plus any opt-in configured paths (e.g. an Obsidian vault),
// while all other external directories stay denied.
rules.push(...buildExternalDirectoryAllowRules(options.allowDirectories))
// User-configured rules layered before Forge's structural denies so they can never
// override a structural deny.
rules.push(...(options.extraRules ?? []))
// Code agent forbidden tools. Placed after *:allow so findLast picks them up.
rules.push(...denyRulesFor([...LOOP_ONLY_STRUCTURAL_DENY_PERMISSIONS, ...SHARED_STRUCTURAL_DENY_PERMISSIONS]))
return rules
}
/**
* Builds the permission ruleset for audit sessions.
*
* Audit sessions run the auditor agent in an isolated session. The ruleset
* allows read-only operations (read, grep, glob, codesearch, webfetch,
* websearch, list, task) and review tools (review-write, review-delete), but
* denies direct code mutation tools.
*
* External directory access is denied by default; opencode's tool-output directory (and any
* user-configured directories) are then allowed so spilled tool outputs remain readable.
*/
export function buildAuditSessionPermissionRuleset(options: LoopPermissionRulesetOptions = {}): PermissionRule[] {
const rules: PermissionRule[] = [
{ permission: '*', pattern: '*', action: 'allow' },
{ permission: 'external_directory', pattern: '*', action: 'deny' },
// Allow rules layered after the deny (last-match-wins): tool-output directory (always)
// plus any opt-in configured directories.
...buildExternalDirectoryAllowRules(options.allowDirectories),
// User-configured rules layered before Forge's structural denies.
...(options.extraRules ?? []),
// Audit sessions must not mutate code, must never launch loops or manage other loops.
// Placed after *:allow so findLast picks them up.
...denyRulesFor([...AUDIT_ONLY_STRUCTURAL_DENY_PERMISSIONS, ...SHARED_STRUCTURAL_DENY_PERMISSIONS]),
]
return rules
}