opencode-fs-plugin 1.9.2 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -72,7 +72,7 @@ unit, end-to-end and coverage tests.
72
72
  | `cpio` | `fs.cpio` | `create`/`list`/`extract` (newc), `directory`, `members`, `input`, `member`, `verbose` |
73
73
  | `ar` | `fs.ar` | `create`/`list`/`extract`, `directory`, `members` |
74
74
  | HTTP(S) | `fs.http_request` | `url`, `method`, `headers`, `body`, `json`, `output`, `checksum`, `maxBytes`, `timeoutMs`, `failOnError`, `followRedirects`, `maxRedirects` |
75
- | process | `fs.exec` | `command`, `args`, `cwd`, `stdin`, `timeoutMs`, `network`, `allowPaths`; exact allowlist, procage sandbox, no shell |
75
+ | process | `fs.exec` | `command`, `args`, `cwd`, `stdin`, `timeoutMs`; exact allowlist, procage sandbox, no shell |
76
76
  | shell pipe | `fs.pipe` | `stages`, `input`, `output`, `append` |
77
77
 
78
78
  `fs.head.skip` selects line ranges (`sed -n '20,40p'`). `fs.find` can
@@ -140,24 +140,21 @@ work, fan out with `Promise.all` over `tools.fs.*` (or one multi-path call).
140
140
  ```
141
141
 
142
142
  The workspace (project) is read/write/exec, but **network and external paths are
143
- denied by default**. Approve them per call:
143
+ denied by default**. There are no per-call grants: when the sandbox blocks a
144
+ path or a socket, `fs.exec` asks the matching permission and retries the run.
145
+
146
+ - blocked path → `external_directory` for the absolute path
147
+ - blocked socket → `fs_exec_network`
148
+
149
+ The decision comes from your `opencode.jsonc` rules (`ask`, `allow`, `deny`), the
150
+ same as every other tool. If a path is still blocked after approval, the result
151
+ reports `violations` and a `hint`. `fs.exec` also kills every process it started
152
+ when the run ends, including detached descendants.
144
153
 
145
154
  ```js
146
- await tools.fs.exec({
147
- command: "npm",
148
- args: ["install"],
149
- network: true,
150
- allowPaths: [{ path: "/home/you/.npm", mode: "rw" }],
151
- timeoutMs: 300000,
152
- })
155
+ await tools.fs.exec({ command: "npm", args: ["install"], timeoutMs: 300000 })
153
156
  ```
154
157
 
155
- `network: true` asserts `fs_exec_network`; every `allowPaths` entry asserts
156
- `external_directory`. Use `mode: "ro"` for read-only paths. Denied operations
157
- come back as `violations` and a `hint`; ask the user, then retry with the approved
158
- paths or network. `fs.exec` also kills every process it started when the run
159
- ends, including detached descendants.
160
-
161
158
  `command` is a bare name, `args` are literal, and `cwd` is sandboxed. Direct
162
159
  calls return JSON with `exitCode`, `stdout`, `stderr`, and violations; a pipe
163
160
  stage returns raw stdout so it composes with `grep`, `sort`, `tee`, and the rest.
@@ -262,18 +259,18 @@ Add the registry package directly to the plugin list:
262
259
 
263
260
  ```jsonc
264
261
  {
265
- "plugins": ["opencode-fs-plugin@1.9.0"]
262
+ "plugins": ["opencode-fs-plugin@1.10.0"]
266
263
  }
267
264
  ```
268
265
 
269
- `opencode plugin add opencode-fs-plugin@1.9.0` installs it and writes the config
266
+ `opencode plugin add opencode-fs-plugin@1.10.0` installs it and writes the config
270
267
  entry. A bare `opencode-fs-plugin` follows the `latest` tag.
271
268
 
272
269
  OpenCode manages the package under its cache, so the path is not stable. If you
273
270
  also want the packaged skill, install it into a project instead:
274
271
 
275
272
  ```bash
276
- npm install opencode-fs-plugin@1.9.0
273
+ npm install opencode-fs-plugin@1.10.0
277
274
  ```
278
275
 
279
276
  ```jsonc
@@ -288,7 +285,7 @@ npm install opencode-fs-plugin@1.9.0
288
285
  Download the release tarball, then install it as a normal npm package:
289
286
 
290
287
  ```bash
291
- VERSION=1.9.0
288
+ VERSION=1.10.0
292
289
  curl -L -o "opencode-fs-plugin-${VERSION}.tgz" \
293
290
  "https://github.com/masterjg/opencode-fs-plugin/releases/download/v${VERSION}/opencode-fs-plugin-${VERSION}.tgz"
294
291
  npm install "./opencode-fs-plugin-${VERSION}.tgz"
@@ -317,20 +314,20 @@ OpenCode installs it from Git:
317
314
 
318
315
  ```jsonc
319
316
  {
320
- "plugins": ["github:masterjg/opencode-fs-plugin#v1.9.0"]
317
+ "plugins": ["github:masterjg/opencode-fs-plugin#v1.10.0"]
321
318
  }
322
319
  ```
323
320
 
324
- `opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.9.0'` installs the
321
+ `opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.10.0'` installs the
325
322
  same spec and writes the config entry. The equivalent full URL is
326
- `git+https://github.com/masterjg/opencode-fs-plugin.git#v1.9.0`. A tag is
323
+ `git+https://github.com/masterjg/opencode-fs-plugin.git#v1.10.0`. A tag is
327
324
  checked for updates; use a full commit hash to pin it.
328
325
 
329
326
  Git plugins are installed under OpenCode's cache, so their package path is not
330
327
  stable. If you also want the packaged skill, install with npm instead:
331
328
 
332
329
  ```bash
333
- npm install "github:masterjg/opencode-fs-plugin#v1.9.0"
330
+ npm install "github:masterjg/opencode-fs-plugin#v1.10.0"
334
331
  ```
335
332
 
336
333
  Then use `./node_modules/opencode-fs-plugin` in `plugins` and
@@ -388,12 +385,13 @@ Every tool asserts an action named after the tool (`fs_cp`, `fs_rm`,
388
385
  external targets first require `external_directory` on the absolute boundary.
389
386
  `allow always` persists the same patterns as the built-in tools.
390
387
 
391
- `fs.exec` asserts `fs_exec`; `network: true` also asserts `fs_exec_network`, and
392
- each `allowPaths` entry asserts `external_directory`:
388
+ `fs.exec` asserts `fs_exec` for the run, then asks `external_directory` for any
389
+ blocked path and `fs_exec_network` for blocked sockets before retrying:
393
390
 
394
391
  ```jsonc
395
392
  {
396
393
  "permissions": [
394
+ { "action": "external_directory", "resource": "$HOME/.npm/**", "effect": "ask" },
397
395
  { "action": "fs_exec_network", "resource": "*", "effect": "ask" }
398
396
  ]
399
397
  }
package/SPEC.md CHANGED
@@ -86,13 +86,15 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
86
86
  Default: `npm`, `npx`, `uv`, `uvx`, `pip`, `pip3`. Override with the plugin
87
87
  option `execAllowlist`; an empty list disables execution.
88
88
  - The policy is procage `agent_workspace` v3: the project is read/write/exec.
89
- Network and external paths are denied by default.
90
- - `network: true` asserts `fs_exec_network` and sets the policy network to IP.
91
- `allowPaths` entries assert `external_directory`; `mode: "ro"` adds a read
92
- rule, `mode: "rw"` adds a write rule.
93
- - Denied file or network access is reported as `violations` plus a `hint`, and
94
- stderr containing EACCES/EPERM/EROFS marks `sandboxBlocked`. Ask the user, then
95
- retry with the approved paths or network.
89
+ Network and external paths are denied by default; there are no per-call
90
+ grants.
91
+ - Denied access is handled by asserting the configured permission and retrying,
92
+ bounded to six attempts: a blocked path asks `external_directory` on the
93
+ absolute path, a blocked socket asks `fs_exec_network`. The `allow`/`ask`/`deny`
94
+ decision comes from `opencode.jsonc`, not from the tool call.
95
+ - If a path or socket stays blocked after approval, the result reports
96
+ `violations` plus a `hint`; stderr containing EACCES/EPERM/EROFS marks
97
+ `sandboxBlocked`.
96
98
  - `args` are passed verbatim. `cwd` resolves inside the sandbox and asserts
97
99
  `external_directory` when it leaves the project; the action is `fs_exec`.
98
100
  - Timeout defaults to 120000 ms, capped at 600000. Procage ends the run and kills
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-fs-plugin",
3
- "version": "1.9.2",
3
+ "version": "1.10.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22.12"
@@ -100,7 +100,7 @@ host drops unknown fields, so a name typo runs with defaults.
100
100
  | `paths` + optional `keep`/`force`/`stdout`/`level`/`suffix` | `fs.gzip`, `fs.gunzip` |
101
101
  | `url` + optional `output` | `fs.http_request` |
102
102
  | `stages` + optional `input`/`output` | `fs.pipe` |
103
- | `command` + `args` + optional `cwd`/`stdin`/`timeoutMs`/`network`/`allowPaths` | `fs.exec` |
103
+ | `command` + `args` + optional `cwd`/`stdin`/`timeoutMs` | `fs.exec` |
104
104
 
105
105
  `fs.head` takes `skip` for line ranges: `sed -n '20,40p'` is
106
106
  `fs.head({ paths, skip: 19, lines: 21 })`. `fs.find` takes
@@ -157,21 +157,16 @@ override it with the fs plugin option `execAllowlist` in `opencode.jsonc`:
157
157
  ```
158
158
 
159
159
  The project is read/write/exec. **Network and external paths are denied by
160
- default.** `network: true` asserts `fs_exec_network`; every `allowPaths` entry
161
- asserts `external_directory`, and `mode: "ro"` keeps it read-only:
160
+ default, and there are no per-call grants.** When the sandbox blocks something,
161
+ `fs.exec` asks `external_directory` for the path or `fs_exec_network` for a
162
+ socket, then retries; the decision comes from `opencode.jsonc`:
162
163
 
163
164
  ```js
164
- await tools.fs.exec({
165
- command: "npm",
166
- args: ["install"],
167
- network: true,
168
- allowPaths: [{ path: "/home/you/.npm", mode: "rw" }],
169
- timeoutMs: 300000,
170
- })
165
+ await tools.fs.exec({ command: "npm", args: ["install"], timeoutMs: 300000 })
171
166
  ```
172
167
 
173
- Denied access returns `violations` and a `hint`; ask the user, then retry with
174
- the approved path or network. `command` is a bare name, `args` are literal, and
168
+ Denied access returns `violations` and a `hint`; if a path stays blocked after
169
+ approval, the result says so. `command` is a bare name, `args` are literal, and
175
170
  `cwd` is sandboxed. Direct calls return JSON with `exitCode`/`stdout`/`stderr`
176
171
  and violations; in `fs.pipe` the stage passes raw stdout.
177
172
 
@@ -476,8 +471,9 @@ in agent rules. `fs.pipe` asserts each stage's action; a denied stage cannot be
476
471
  hidden inside a pipeline. Do not work around a denied tool with shell.
477
472
 
478
473
  `fs_exec` runs only exact allowlisted names in a procage sandbox. It asserts
479
- `fs_exec`, plus `fs_exec_network` for `network: true` and `external_directory`
480
- for each `allowPaths` entry or an external `cwd`. There is no shell.
474
+ `fs_exec`, then asks `external_directory` for a blocked path or `fs_exec_network`
475
+ for a blocked socket and retries; there are no per-call grants. There is no
476
+ shell.
481
477
 
482
478
  `fs_http_request` is allowed by default, like `fetch`. Its resource is the
483
479
  request host; rules can allow one host and ask or deny the rest. Each redirect
package/src/tools/exec.ts CHANGED
@@ -42,24 +42,6 @@ const Schema = Type.Object(
42
42
  timeoutMs: Type.Optional(
43
43
  Type.Integer({ minimum: 100, maximum: 600_000, description: "Timeout in milliseconds. Default: 120000." }),
44
44
  ),
45
- network: Type.Optional(
46
- Type.Boolean({
47
- description: "Allow IP network after `fs_exec_network` approval. Default: false.",
48
- }),
49
- ),
50
- allowPaths: Type.Optional(
51
- Type.Array(
52
- Type.Object(
53
- {
54
- path: Type.String({ minLength: 1, description: "External path to expose after approval." }),
55
- mode: Type.Optional(
56
- Type.Union([Type.Literal("ro"), Type.Literal("rw")], { description: "Read-only or read-write. Default: rw." }),
57
- ),
58
- },
59
- { additionalProperties: false },
60
- ),
61
- ),
62
- ),
63
45
  },
64
46
  { additionalProperties: false },
65
47
  );
@@ -72,7 +54,7 @@ interface PolicyDocument {
72
54
  [key: string]: unknown;
73
55
  }
74
56
 
75
- interface ExecOutcome {
57
+ export interface ExecOutcome {
76
58
  stdout: Buffer;
77
59
  stderr: Buffer;
78
60
  exitCode: number;
@@ -120,34 +102,53 @@ export const execTool: ToolSpec<Input> = {
120
102
  await ctx.permission.assert([{ target: cwdTarget, boundary: cwdTarget.full }]);
121
103
  if (ctx.signal.aborted) throw new FsError("exec interrupted.", "interrupted");
122
104
 
123
- if (input.network === true) await ctx.permissionFor("fs_exec_network").assert([]);
124
-
125
- const allowed: Array<{ full: string; mode: "ro" | "rw" }> = [];
126
- for (const entry of input.allowPaths ?? []) {
127
- const target = await ctx.sandbox.resolveExisting(entry.path, { allowRoot: true });
128
- await ctx.permission.assert([{ target, boundary: target.full }]);
129
- const mode = entry.mode ?? "rw";
130
- if (!allowed.some((item) => item.full === target.full && item.mode === mode)) {
131
- allowed.push({ full: target.full, mode });
132
- }
133
- }
134
-
135
105
  const executable = await resolveExecutable(command);
136
106
  const tempDir = await fs.mkdtemp(path.join(ctx.tmpdir, "fs-exec-"));
137
107
  try {
138
- const outcome = await runCaged(ctx, {
139
- command,
140
- executable,
141
- args,
142
- cwd: cwdTarget.full,
143
- timeoutMs: input.timeoutMs ?? 120_000,
144
- network: input.network === true,
145
- allowed,
146
- tempDir,
147
- input: io.stdin === undefined
148
- ? input.stdin === undefined ? undefined : Buffer.from(input.stdin)
149
- : await collect(io.stdin),
150
- });
108
+ const inputData = io.stdin === undefined
109
+ ? input.stdin === undefined ? undefined : Buffer.from(input.stdin)
110
+ : await collect(io.stdin);
111
+ let networkAllowed = false;
112
+ const approved: Array<{ full: string }> = [];
113
+ let outcome: ExecOutcome | undefined;
114
+
115
+ for (let attempt = 0; attempt < 6; attempt++) {
116
+ outcome = await runCaged(ctx, {
117
+ command,
118
+ executable,
119
+ args,
120
+ cwd: cwdTarget.full,
121
+ timeoutMs: input.timeoutMs ?? 120_000,
122
+ network: networkAllowed,
123
+ allowed: approved,
124
+ tempDir,
125
+ input: inputData,
126
+ });
127
+
128
+ const networkDenied = !networkAllowed && detectsNetworkDenial(outcome);
129
+ const deniedPaths = discoverExternalPaths(ctx, outcome)
130
+ .filter((candidate) => !approved.some((entry) => entry.full === candidate));
131
+ if (!networkDenied && deniedPaths.length === 0) break;
132
+
133
+ if (networkDenied) {
134
+ await ctx.permissionFor("fs_exec_network").assert([]);
135
+ networkAllowed = true;
136
+ continue;
137
+ }
138
+
139
+ let added = false;
140
+ for (const candidate of deniedPaths) {
141
+ const target = await ctx.sandbox.resolveExisting(candidate, { allowRoot: true }).catch(() => undefined);
142
+ if (target === undefined) continue;
143
+ await ctx.permission.assert([{ target, boundary: target.full }]);
144
+ approved.push({ full: target.full });
145
+ added = true;
146
+ }
147
+ if (!added) break;
148
+ }
149
+
150
+ /* v8 ignore next */
151
+ if (outcome === undefined) throw new FsError("exec produced no result.", "io");
151
152
  const metadata = {
152
153
  command,
153
154
  args,
@@ -159,8 +160,8 @@ export const execTool: ToolSpec<Input> = {
159
160
  stderrTruncated: outcome.stderrTruncated,
160
161
  sandboxBlocked: isSandboxBlocked(outcome),
161
162
  violations: outcome.violations.length,
162
- network: input.network === true,
163
- allowPaths: allowed,
163
+ network: networkAllowed,
164
+ externalPaths: approved.map((entry) => entry.full),
164
165
  };
165
166
  if (io.piped === true) {
166
167
  return {
@@ -179,7 +180,7 @@ export const execTool: ToolSpec<Input> = {
179
180
  ...(sandboxBlocked
180
181
  ? {
181
182
  hint:
182
- "The sandbox denied an external path or network access. Ask the user, then retry with allowPaths or network: true.",
183
+ "The sandbox denied an external path or network access. Approve the path or network in the permission prompt and retry.",
183
184
  }
184
185
  : {}),
185
186
  };
@@ -202,7 +203,7 @@ interface CagedInput {
202
203
  cwd: string;
203
204
  timeoutMs: number;
204
205
  network: boolean;
205
- allowed: Array<{ full: string; mode: "ro" | "rw" }>;
206
+ allowed: Array<{ full: string }>;
206
207
  tempDir: string;
207
208
  input: Buffer | undefined;
208
209
  }
@@ -235,7 +236,7 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
235
236
  }
236
237
  addRule(doc, "write", input.tempDir);
237
238
  addRule(doc, "write", input.cwd);
238
- for (const allowed of input.allowed) addRule(doc, allowed.mode === "ro" ? "read" : "write", allowed.full);
239
+ for (const allowed of input.allowed) addRule(doc, "write", allowed.full);
239
240
 
240
241
  let cage: InstanceType<typeof api.Cage>;
241
242
  let policy: ReturnType<typeof api.Policy.fromJSON>;
@@ -326,12 +327,43 @@ export function filterViolations(violations: readonly Violation[]): Violation[]
326
327
  });
327
328
  }
328
329
 
329
- function isSandboxBlocked(outcome: ExecOutcome): boolean {
330
+ export function isSandboxBlocked(outcome: ExecOutcome): boolean {
330
331
  if (outcome.violations.length > 0) return true;
331
332
  if (outcome.exitCode === 0) return false;
332
333
  return /EACCES|EPERM|EROFS|Permission denied|Read-only file system/i.test(outcome.stderr.toString("utf8"));
333
334
  }
334
335
 
336
+ export function detectsNetworkDenial(outcome: ExecOutcome): boolean {
337
+ if (outcome.violations.some((violation) => /AF_INET|AF_INET6/.test(violation.target ?? ""))) return true;
338
+ return /EAI_AGAIN|getaddrinfo|ENETUNREACH|ECONNREFUSED/i.test(outcome.stderr.toString("utf8"));
339
+ }
340
+
341
+ export function discoverExternalPaths(ctx: RunContext, outcome: ExecOutcome): string[] {
342
+ const found = new Set<string>();
343
+ for (const violation of outcome.violations) {
344
+ if (violation.target?.startsWith("/")) found.add(violation.target);
345
+ }
346
+
347
+ const stderr = outcome.stderr.toString("utf8");
348
+ for (const line of stderr.split("\n")) {
349
+ if (!/EACCES|EPERM|EROFS|Permission denied|Read-only file system|error writing|not written|cannot open|failed to open/i.test(line)) {
350
+ continue;
351
+ }
352
+ for (const match of line.matchAll(/(?:^|[\s'"`(])(\/[^\s'"`),]+)/g)) {
353
+ found.add(match[1]!);
354
+ }
355
+ }
356
+ for (const match of stderr.matchAll(/path:\s*['"]([^'"]+)['"]/g)) {
357
+ found.add(match[1]!);
358
+ }
359
+
360
+ const root = ctx.sandbox.root;
361
+ return [...found]
362
+ .map((candidate) => path.resolve(candidate))
363
+ .filter((candidate) => candidate !== root && !candidate.startsWith(`${root}${path.sep}`))
364
+ .filter((candidate, index, all) => all.indexOf(candidate) === index);
365
+ }
366
+
335
367
  async function resolveExecutable(command: string): Promise<{ path: string; real: string }> {
336
368
  const directories = (process.env.PATH ?? "").split(path.delimiter).filter(Boolean);
337
369
  for (const directory of directories) {