opencode-fs-plugin 3.4.0 → 3.6.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 +268 -132
- package/SPEC.md +171 -56
- package/package.json +2 -2
- package/skills/filesystem/SKILL.md +172 -67
- package/src/core/format.ts +3 -9
- package/src/core/fsutil.ts +84 -23
- package/src/core/jobs.ts +210 -34
- package/src/core/permissions.ts +32 -14
- package/src/core/relay-control.ts +343 -0
- package/src/core/relay-protocol.ts +274 -0
- package/src/core/safe-output.ts +290 -0
- package/src/core/sandbox.ts +43 -0
- package/src/core/types.ts +0 -5
- package/src/index.ts +78 -72
- package/src/tools/ar.ts +62 -29
- package/src/tools/cat.ts +31 -46
- package/src/tools/chmod.ts +30 -19
- package/src/tools/chown.ts +24 -16
- package/src/tools/command.ts +536 -0
- package/src/tools/cp.ts +84 -20
- package/src/tools/cpio.ts +204 -65
- package/src/tools/{http_request.ts → curl.ts} +127 -10
- package/src/tools/dd.ts +15 -9
- package/src/tools/diff.ts +46 -15
- package/src/tools/grep.ts +5 -2
- package/src/tools/gzip.ts +13 -15
- package/src/tools/mktemp.ts +41 -26
- package/src/tools/mv.ts +69 -28
- package/src/tools/pipe.ts +24 -16
- package/src/tools/split.ts +26 -12
- package/src/tools/tar.ts +132 -57
- package/src/tools/tee.ts +7 -4
- package/src/tools/touch.ts +12 -4
- package/src/tools/truncate.ts +17 -7
- package/src/tools/zip.ts +119 -54
- package/src/core/spill.ts +0 -145
- package/src/tools/exec.ts +0 -483
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
+
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as fs from "node:fs/promises";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import { constants } from "node:fs";
|
|
9
|
+
import { FsError, errnoCode, fromErrno, isMissing } from "./errors.js";
|
|
10
|
+
import type { PermissionGate } from "./permissions.js";
|
|
11
|
+
import type { ResolvedTarget, Sandbox } from "./sandbox.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the path a following write actually reaches. Existing final
|
|
15
|
+
* symlinks (including a dangling link's would-be target) are followed so the
|
|
16
|
+
* permission gate sees the real destination, not only the link path.
|
|
17
|
+
*/
|
|
18
|
+
export async function resolveOutputTarget(sandbox: Sandbox, target: string): Promise<ResolvedTarget> {
|
|
19
|
+
return sandbox.resolveForWrite(target);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Permission boundary for a file output is its containing directory. */
|
|
23
|
+
export function outputBoundary(full: string): string {
|
|
24
|
+
return path.dirname(full);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Open a file for a direct write without following a final symlink. The caller
|
|
29
|
+
* resolves and gates the canonical target first; `O_NOFOLLOW` closes the gap
|
|
30
|
+
* where the final component is swapped for a link after that check.
|
|
31
|
+
*/
|
|
32
|
+
export async function openOutput(
|
|
33
|
+
full: string,
|
|
34
|
+
append = false,
|
|
35
|
+
mode = 0o666,
|
|
36
|
+
): Promise<fs.FileHandle> {
|
|
37
|
+
const flags = append
|
|
38
|
+
? constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW
|
|
39
|
+
: constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW;
|
|
40
|
+
try {
|
|
41
|
+
return await fs.open(full, flags, mode);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw fromErrno(error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function throwIfAborted(signal?: AbortSignal): void {
|
|
48
|
+
if (signal?.aborted === true) throw new FsError("Operation interrupted.", "interrupted");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Write every byte of a buffer, retrying partial writes. A zero-progress write
|
|
53
|
+
* is a hard I/O error, never a silent truncation.
|
|
54
|
+
*/
|
|
55
|
+
export async function writeFully(
|
|
56
|
+
handle: fs.FileHandle,
|
|
57
|
+
data: Buffer,
|
|
58
|
+
position?: number,
|
|
59
|
+
signal?: AbortSignal,
|
|
60
|
+
): Promise<void> {
|
|
61
|
+
let offset = 0;
|
|
62
|
+
while (offset < data.length) {
|
|
63
|
+
throwIfAborted(signal);
|
|
64
|
+
let bytesWritten: number;
|
|
65
|
+
try {
|
|
66
|
+
({ bytesWritten } = await handle.write(
|
|
67
|
+
data,
|
|
68
|
+
offset,
|
|
69
|
+
data.length - offset,
|
|
70
|
+
position === undefined ? undefined : position + offset,
|
|
71
|
+
));
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw fromErrno(error);
|
|
74
|
+
}
|
|
75
|
+
if (bytesWritten <= 0) throw new FsError("Short write: no bytes written.", "io");
|
|
76
|
+
offset += bytesWritten;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Write a buffer or stream to an already-resolved output path. */
|
|
81
|
+
export async function writeOutput(
|
|
82
|
+
full: string,
|
|
83
|
+
data: Buffer | AsyncIterable<Buffer>,
|
|
84
|
+
append = false,
|
|
85
|
+
mode = 0o666,
|
|
86
|
+
signal?: AbortSignal,
|
|
87
|
+
finalMode?: number,
|
|
88
|
+
): Promise<void> {
|
|
89
|
+
throwIfAborted(signal);
|
|
90
|
+
let handle: fs.FileHandle | undefined;
|
|
91
|
+
try {
|
|
92
|
+
handle = await openOutput(full, append, mode);
|
|
93
|
+
if (Buffer.isBuffer(data)) {
|
|
94
|
+
await writeFully(handle, data, undefined, signal);
|
|
95
|
+
} else {
|
|
96
|
+
for await (const chunk of data) await writeFully(handle, chunk, undefined, signal);
|
|
97
|
+
}
|
|
98
|
+
if (finalMode !== undefined) await handle.chmod(finalMode);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (error instanceof FsError) throw error;
|
|
101
|
+
throw fromErrno(error);
|
|
102
|
+
} finally {
|
|
103
|
+
await handle?.close();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Change a directory mode through an O_DIRECTORY|O_NOFOLLOW handle. An
|
|
109
|
+
* unreadable directory (EACCES on O_RDONLY) falls back to path chmod only
|
|
110
|
+
* after lstat confirms a real, non-symlink directory. That narrow fallback
|
|
111
|
+
* retains the final-component swap race; ordinary readable directories stay
|
|
112
|
+
* on the handle path.
|
|
113
|
+
*/
|
|
114
|
+
export async function chmodDirectoryNoFollow(full: string, mode: number): Promise<void> {
|
|
115
|
+
let handle: fs.FileHandle | undefined;
|
|
116
|
+
try {
|
|
117
|
+
handle = await fs.open(
|
|
118
|
+
full,
|
|
119
|
+
constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW,
|
|
120
|
+
);
|
|
121
|
+
await handle.chmod(mode);
|
|
122
|
+
return;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (errnoCode(error) !== "EACCES") throw fromErrno(error);
|
|
125
|
+
try {
|
|
126
|
+
const stats = await fs.lstat(full);
|
|
127
|
+
/* v8 ignore next 3 -- final-component swap guard cannot be made deterministic */
|
|
128
|
+
if (!stats.isDirectory()) {
|
|
129
|
+
throw new FsError(`Blocked directory mode: "${full}" is not a directory.`, "archive");
|
|
130
|
+
}
|
|
131
|
+
await fs.chmod(full, mode);
|
|
132
|
+
} catch (fallbackError) {
|
|
133
|
+
/* v8 ignore next 2 -- fallback OS errors are environment-dependent */
|
|
134
|
+
if (fallbackError instanceof FsError) throw fallbackError;
|
|
135
|
+
/* v8 ignore next -- fallback OS errors are environment-dependent */
|
|
136
|
+
throw fromErrno(fallbackError);
|
|
137
|
+
}
|
|
138
|
+
} finally {
|
|
139
|
+
await handle?.close();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type ArchiveEntryKind = "file" | "directory" | "symlink";
|
|
144
|
+
|
|
145
|
+
/** File bits safe to apply: strip suid/sgid/sticky, then respect umask. */
|
|
146
|
+
export function safeMode(mode: number, umask = process.umask()): number {
|
|
147
|
+
return (mode & 0o777) & ~umask;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Directory bits: strip suid/sgid, preserve sticky, then respect umask. */
|
|
151
|
+
export function safeDirectoryMode(mode: number, umask = process.umask()): number {
|
|
152
|
+
return (mode & 0o1000) | ((mode & 0o777) & ~umask);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Resolve an archive extraction destination in its canonical parent, refusing
|
|
157
|
+
* an existing final symlink. A target that leaves the sandbox is allowed only
|
|
158
|
+
* through the later bulk `external_directory` gate; one that stays inside the
|
|
159
|
+
* sandbox but escapes the extraction root is refused here.
|
|
160
|
+
*/
|
|
161
|
+
export async function resolveArchiveTarget(
|
|
162
|
+
sandbox: Sandbox,
|
|
163
|
+
root: string,
|
|
164
|
+
destination: string,
|
|
165
|
+
kind: ArchiveEntryKind,
|
|
166
|
+
): Promise<ResolvedTarget> {
|
|
167
|
+
const target = await sandbox.resolveForCreate(destination);
|
|
168
|
+
let stats: Awaited<ReturnType<typeof fs.lstat>> | undefined;
|
|
169
|
+
try {
|
|
170
|
+
stats = await fs.lstat(target.full);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
/* v8 ignore next */
|
|
173
|
+
if (!isMissing(error)) throw fromErrno(error, "access");
|
|
174
|
+
}
|
|
175
|
+
if (stats !== undefined) {
|
|
176
|
+
if (stats.isSymbolicLink()) {
|
|
177
|
+
throw new FsError(`Blocked archive path: "${destination}" is a symbolic link.`, "archive");
|
|
178
|
+
}
|
|
179
|
+
if (kind === "symlink") {
|
|
180
|
+
throw new FsError(`Blocked archive path: "${destination}" already exists.`, "archive");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (!isWithinRoot(root, target.full) && !target.outside) {
|
|
184
|
+
throw new FsError(`Blocked archive path: "${destination}" escapes the extraction root.`, "archive");
|
|
185
|
+
}
|
|
186
|
+
return target;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface ArchivePlan {
|
|
190
|
+
readonly target: ResolvedTarget;
|
|
191
|
+
readonly kind: ArchiveEntryKind;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Assert one bulk permission request for all planned extraction targets. */
|
|
195
|
+
export async function assertArchivePlan(
|
|
196
|
+
gate: PermissionGate,
|
|
197
|
+
plans: readonly ArchivePlan[],
|
|
198
|
+
root: { target: ResolvedTarget; boundary: string },
|
|
199
|
+
): Promise<void> {
|
|
200
|
+
const entries = plans.map(({ target, kind }) => ({
|
|
201
|
+
target,
|
|
202
|
+
boundary: kind === "directory" ? target.full : outputBoundary(target.full),
|
|
203
|
+
}));
|
|
204
|
+
entries.push(root);
|
|
205
|
+
await gate.assert(entries);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Directory plans to apply modes for: canonical target deduplicated with the
|
|
210
|
+
* last archive entry winning, deepest path first.
|
|
211
|
+
*/
|
|
212
|
+
export function finalDirectoryPlans<T extends { target: ResolvedTarget; kind: ArchiveEntryKind }>(
|
|
213
|
+
plans: readonly T[],
|
|
214
|
+
): T[] {
|
|
215
|
+
const last = new Map<string, T>();
|
|
216
|
+
for (const plan of plans) {
|
|
217
|
+
if (plan.kind === "directory") last.set(plan.target.full, plan);
|
|
218
|
+
}
|
|
219
|
+
return [...last.values()].sort(
|
|
220
|
+
(a, b) => b.target.full.split(path.sep).length - a.target.full.split(path.sep).length,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Reject members that collide with, or live beneath, an archive-created
|
|
226
|
+
* symlink. Such descendants would otherwise be written through the newly
|
|
227
|
+
* created link during extraction.
|
|
228
|
+
*/
|
|
229
|
+
export function assertArchiveSymlinkPlan(plans: readonly ArchivePlan[]): void {
|
|
230
|
+
const links = new Set(
|
|
231
|
+
plans.filter((plan) => plan.kind === "symlink").map((plan) => plan.target.full),
|
|
232
|
+
);
|
|
233
|
+
if (links.size === 0) return;
|
|
234
|
+
const seenLinks = new Set<string>();
|
|
235
|
+
for (const plan of plans) {
|
|
236
|
+
const full = plan.target.full;
|
|
237
|
+
if (links.has(full)) {
|
|
238
|
+
if (plan.kind !== "symlink" || seenLinks.has(full)) {
|
|
239
|
+
throw new FsError(`Blocked archive path: "${full}" collides with a symlink member.`, "archive");
|
|
240
|
+
}
|
|
241
|
+
seenLinks.add(full);
|
|
242
|
+
}
|
|
243
|
+
for (let parent = path.dirname(full); parent !== path.dirname(parent); parent = path.dirname(parent)) {
|
|
244
|
+
if (links.has(parent)) {
|
|
245
|
+
throw new FsError(`Blocked archive path: "${full}" is beneath a symlink member.`, "archive");
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Safe member name for archive creation. Relative sources keep their safe
|
|
253
|
+
* relative layout; absolute sources are named relative to the base first, then
|
|
254
|
+
* the project root; unsafe or external sources fall back to the basename.
|
|
255
|
+
*/
|
|
256
|
+
export function archiveSourceName(requested: string, full: string, base: string, root: string): string {
|
|
257
|
+
if (!path.isAbsolute(requested)) {
|
|
258
|
+
const normalized = path.normalize(requested);
|
|
259
|
+
if (!path.isAbsolute(normalized) && !normalized.split(path.sep).includes("..")) {
|
|
260
|
+
return normalized;
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
const fromBase = relativeMember(base, full);
|
|
264
|
+
if (fromBase !== undefined) return fromBase;
|
|
265
|
+
const fromRoot = relativeMember(root, full);
|
|
266
|
+
if (fromRoot !== undefined) return fromRoot;
|
|
267
|
+
}
|
|
268
|
+
return path.basename(full);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function relativeMember(parent: string, child: string): string | undefined {
|
|
272
|
+
if (!isWithinRoot(parent, child)) return undefined;
|
|
273
|
+
const relative = path.relative(parent, child);
|
|
274
|
+
return relative === "" ? undefined : relative;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Reject archived symlink targets that are absolute, empty, or traversing. */
|
|
278
|
+
export function assertSafeLinkTarget(linkname: string): void {
|
|
279
|
+
if (linkname.length === 0 || linkname.includes("\0")) {
|
|
280
|
+
throw new FsError(`Blocked symlink target: ${JSON.stringify(linkname)}`, "archive");
|
|
281
|
+
}
|
|
282
|
+
if (path.isAbsolute(linkname) || linkname.split("/").includes("..")) {
|
|
283
|
+
throw new FsError(`Blocked symlink target: ${linkname}`, "archive");
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function isWithinRoot(root: string, target: string): boolean {
|
|
288
|
+
const rel = path.relative(root, target);
|
|
289
|
+
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
|
|
290
|
+
}
|
package/src/core/sandbox.ts
CHANGED
|
@@ -150,6 +150,49 @@ export class Sandbox {
|
|
|
150
150
|
};
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Resolve the real path a following syscall will mutate. Existing final
|
|
155
|
+
* symlinks and chains are followed; a dangling link resolves to the path its
|
|
156
|
+
* target would create, so callers can gate that location before writing.
|
|
157
|
+
*/
|
|
158
|
+
async resolveForWrite(target: string): Promise<ResolvedTarget> {
|
|
159
|
+
this.assertTarget(target);
|
|
160
|
+
let node = path.resolve(this.root, target);
|
|
161
|
+
for (let hops = 0; ; hops++) {
|
|
162
|
+
let stats: Awaited<ReturnType<typeof fs.lstat>>;
|
|
163
|
+
try {
|
|
164
|
+
stats = await fs.lstat(node);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
/* v8 ignore next */
|
|
167
|
+
if (isMissing(error)) {
|
|
168
|
+
const created = await this.resolveForCreate(node);
|
|
169
|
+
return { ...created, requested: target };
|
|
170
|
+
}
|
|
171
|
+
/* v8 ignore next */
|
|
172
|
+
throw fromErrno(error, "access");
|
|
173
|
+
}
|
|
174
|
+
if (!stats.isSymbolicLink()) {
|
|
175
|
+
let real: string;
|
|
176
|
+
try {
|
|
177
|
+
real = await fs.realpath(node);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
/* v8 ignore next 2 */
|
|
180
|
+
throw fromErrno(error, "access");
|
|
181
|
+
}
|
|
182
|
+
return { full: real, outside: !this.isWithin(real), requested: target, realAncestor: real };
|
|
183
|
+
}
|
|
184
|
+
if (hops >= 40) throw new FsError(`Too many symbolic links: "${target}".`, "io");
|
|
185
|
+
let link: string;
|
|
186
|
+
try {
|
|
187
|
+
link = await fs.readlink(node);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
/* v8 ignore next */
|
|
190
|
+
throw fromErrno(error, "access");
|
|
191
|
+
}
|
|
192
|
+
node = path.resolve(path.dirname(node), link);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
153
196
|
/** Resolve the parent of a target that may be created or replaced. */
|
|
154
197
|
async resolveParent(target: string): Promise<ResolvedTarget> {
|
|
155
198
|
this.assertTarget(target);
|
package/src/core/types.ts
CHANGED
|
@@ -30,11 +30,6 @@ export interface RunContext {
|
|
|
30
30
|
readonly concurrency: number;
|
|
31
31
|
readonly signal: AbortSignal;
|
|
32
32
|
readonly tmpdir: string;
|
|
33
|
-
readonly execAllowlist: readonly string[];
|
|
34
|
-
/** Explicit spill threshold in bytes; unset means the dynamic default. */
|
|
35
|
-
readonly spillThreshold?: number;
|
|
36
|
-
/** `proxy` forces egress through the run's allowlist proxy; `ip` is all-IP. */
|
|
37
|
-
readonly execNetworkMode: "proxy" | "ip";
|
|
38
33
|
}
|
|
39
34
|
|
|
40
35
|
export interface ToolSpec<I> {
|
package/src/index.ts
CHANGED
|
@@ -12,7 +12,6 @@ import { JobManager } from "./core/jobs.js";
|
|
|
12
12
|
import { PermissionGate, type PermissionAssert } from "./core/permissions.js";
|
|
13
13
|
import { clampConcurrency } from "./core/parallel.js";
|
|
14
14
|
import { Sandbox } from "./core/sandbox.js";
|
|
15
|
-
import { normalizeSpillThreshold, spillResult } from "./core/spill.js";
|
|
16
15
|
import { parseSchema } from "./core/schema.js";
|
|
17
16
|
import type { ToolSpec } from "./core/types.js";
|
|
18
17
|
import { arTool } from "./tools/ar.js";
|
|
@@ -21,8 +20,10 @@ import { catTool } from "./tools/cat.js";
|
|
|
21
20
|
import { chmodTool } from "./tools/chmod.js";
|
|
22
21
|
import { chownTool } from "./tools/chown.js";
|
|
23
22
|
import { commTool } from "./tools/comm.js";
|
|
23
|
+
import { commandTool } from "./tools/command.js";
|
|
24
24
|
import { cpTool } from "./tools/cp.js";
|
|
25
25
|
import { cpioTool } from "./tools/cpio.js";
|
|
26
|
+
import { curlTool } from "./tools/curl.js";
|
|
26
27
|
import { cutTool } from "./tools/cut.js";
|
|
27
28
|
import { dateTool } from "./tools/date.js";
|
|
28
29
|
import { ddTool } from "./tools/dd.js";
|
|
@@ -31,18 +32,12 @@ import { diffTool } from "./tools/diff.js";
|
|
|
31
32
|
import { duTool } from "./tools/du.js";
|
|
32
33
|
import { fileTool } from "./tools/file.js";
|
|
33
34
|
import { findTool } from "./tools/find.js";
|
|
34
|
-
import {
|
|
35
|
-
execTool,
|
|
36
|
-
normalizeExecAllowlist,
|
|
37
|
-
normalizeExecNetworkMode,
|
|
38
|
-
} from "./tools/exec.js";
|
|
39
35
|
import { findmntTool } from "./tools/findmnt.js";
|
|
40
36
|
import { foldTool } from "./tools/fold.js";
|
|
41
37
|
import { grepTool } from "./tools/grep.js";
|
|
42
38
|
import { gunzipTool, gzipTool } from "./tools/gzip.js";
|
|
43
39
|
import { hashTool } from "./tools/hash.js";
|
|
44
40
|
import { headTool } from "./tools/head.js";
|
|
45
|
-
import { httpRequestTool } from "./tools/http_request.js";
|
|
46
41
|
import { idTool } from "./tools/id.js";
|
|
47
42
|
import { killTool } from "./tools/kill.js";
|
|
48
43
|
import { lnTool } from "./tools/ln.js";
|
|
@@ -85,70 +80,70 @@ import { unzipTool, zipTool } from "./tools/zip.js";
|
|
|
85
80
|
type AnyTool = ToolSpec<any>;
|
|
86
81
|
|
|
87
82
|
const TOOLS: readonly AnyTool[] = [
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
cpTool,
|
|
92
|
-
mvTool,
|
|
93
|
-
lsTool,
|
|
94
|
-
statTool,
|
|
95
|
-
touchTool,
|
|
96
|
-
truncateTool,
|
|
97
|
-
lnTool,
|
|
98
|
-
readlinkTool,
|
|
83
|
+
arTool,
|
|
84
|
+
base64Tool,
|
|
85
|
+
catTool,
|
|
99
86
|
chmodTool,
|
|
100
|
-
|
|
101
|
-
|
|
87
|
+
chownTool,
|
|
88
|
+
commTool,
|
|
89
|
+
commandTool,
|
|
90
|
+
cpTool,
|
|
91
|
+
cpioTool,
|
|
92
|
+
curlTool,
|
|
93
|
+
cutTool,
|
|
94
|
+
dateTool,
|
|
95
|
+
ddTool,
|
|
102
96
|
dfTool,
|
|
97
|
+
diffTool,
|
|
98
|
+
duTool,
|
|
99
|
+
fileTool,
|
|
103
100
|
findTool,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
101
|
+
findmntTool,
|
|
102
|
+
foldTool,
|
|
103
|
+
grepTool,
|
|
107
104
|
gunzipTool,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
unzipTool,
|
|
111
|
-
diffTool,
|
|
112
|
-
catTool,
|
|
105
|
+
gzipTool,
|
|
106
|
+
hashTool,
|
|
113
107
|
headTool,
|
|
114
|
-
tailTool,
|
|
115
|
-
teeTool,
|
|
116
|
-
httpRequestTool,
|
|
117
|
-
psTool,
|
|
118
|
-
pgrepTool,
|
|
119
|
-
killTool,
|
|
120
|
-
fileTool,
|
|
121
|
-
base64Tool,
|
|
122
|
-
odTool,
|
|
123
|
-
stringsTool,
|
|
124
108
|
idTool,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
109
|
+
killTool,
|
|
110
|
+
lnTool,
|
|
111
|
+
lsTool,
|
|
112
|
+
mkdirTool,
|
|
113
|
+
mktempTool,
|
|
129
114
|
mountpointTool,
|
|
130
|
-
|
|
131
|
-
revTool,
|
|
115
|
+
mvTool,
|
|
132
116
|
nlTool,
|
|
117
|
+
odTool,
|
|
133
118
|
pasteTool,
|
|
134
|
-
|
|
135
|
-
|
|
119
|
+
pgrepTool,
|
|
120
|
+
pipeTool,
|
|
121
|
+
psTool,
|
|
122
|
+
readlinkTool,
|
|
123
|
+
revTool,
|
|
124
|
+
rmTool,
|
|
125
|
+
rmdirTool,
|
|
126
|
+
sedTool,
|
|
136
127
|
shufTool,
|
|
137
|
-
foldTool,
|
|
138
|
-
chownTool,
|
|
139
|
-
syncTool,
|
|
140
|
-
ddTool,
|
|
141
|
-
cpioTool,
|
|
142
|
-
arTool,
|
|
143
|
-
wcTool,
|
|
144
128
|
sortTool,
|
|
145
|
-
|
|
146
|
-
|
|
129
|
+
splitTool,
|
|
130
|
+
statTool,
|
|
131
|
+
stringsTool,
|
|
132
|
+
syncTool,
|
|
133
|
+
tacTool,
|
|
134
|
+
tailTool,
|
|
135
|
+
tarTool,
|
|
136
|
+
teeTool,
|
|
137
|
+
touchTool,
|
|
147
138
|
trTool,
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
139
|
+
trashTool,
|
|
140
|
+
truncateTool,
|
|
141
|
+
unameTool,
|
|
142
|
+
uniqTool,
|
|
143
|
+
unzipTool,
|
|
144
|
+
uptimeTool,
|
|
145
|
+
wcTool,
|
|
146
|
+
zipTool,
|
|
152
147
|
];
|
|
153
148
|
|
|
154
149
|
function withBackground(schema: TSchema): TSchema {
|
|
@@ -185,6 +180,17 @@ const JOB_SCHEMA = Type.Object(
|
|
|
185
180
|
{ additionalProperties: false },
|
|
186
181
|
);
|
|
187
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Background jobs are session-scoped. The host must supply a non-empty session
|
|
185
|
+
* id; never fall back to the direct-call test owner in production.
|
|
186
|
+
*/
|
|
187
|
+
function sessionOwner(context: { sessionID: string }): string {
|
|
188
|
+
if (typeof context.sessionID !== "string" || context.sessionID.length === 0) {
|
|
189
|
+
throw new FsError("background jobs require a non-empty session id.", "invalid");
|
|
190
|
+
}
|
|
191
|
+
return context.sessionID;
|
|
192
|
+
}
|
|
193
|
+
|
|
188
194
|
export default Plugin.define({
|
|
189
195
|
id: "opencode-fs-plugin",
|
|
190
196
|
async setup(ctx) {
|
|
@@ -200,9 +206,6 @@ export default Plugin.define({
|
|
|
200
206
|
const concurrency = clampConcurrency(
|
|
201
207
|
typeof ctx.options?.concurrency === "number" ? ctx.options.concurrency : undefined,
|
|
202
208
|
);
|
|
203
|
-
const execAllowlist = normalizeExecAllowlist(ctx.options?.execAllowlist);
|
|
204
|
-
const spillThreshold = normalizeSpillThreshold(ctx.options?.spillThresholdBytes);
|
|
205
|
-
const execNetworkMode = normalizeExecNetworkMode(ctx.options?.execNetworkMode);
|
|
206
209
|
const jobs = new JobManager(
|
|
207
210
|
typeof ctx.options?.jobDir === "string" ? { dir: ctx.options.jobDir } : {},
|
|
208
211
|
);
|
|
@@ -250,18 +253,16 @@ export default Plugin.define({
|
|
|
250
253
|
concurrency,
|
|
251
254
|
signal,
|
|
252
255
|
tmpdir: os.tmpdir(),
|
|
253
|
-
execAllowlist,
|
|
254
|
-
execNetworkMode,
|
|
255
|
-
...(spillThreshold === undefined ? {} : { spillThreshold }),
|
|
256
256
|
},
|
|
257
257
|
parsed,
|
|
258
258
|
{},
|
|
259
259
|
);
|
|
260
|
-
return renderResult(
|
|
260
|
+
return renderResult(result);
|
|
261
261
|
};
|
|
262
262
|
|
|
263
263
|
if ((parsed as { background?: unknown }).background === true) {
|
|
264
|
-
const
|
|
264
|
+
const owner = sessionOwner(context);
|
|
265
|
+
const job = await jobs.start(spec.name, runSpec, owner);
|
|
265
266
|
return {
|
|
266
267
|
content: JSON.stringify(
|
|
267
268
|
{
|
|
@@ -299,8 +300,9 @@ export default Plugin.define({
|
|
|
299
300
|
"Inspect, wait for, cancel, or list background fs jobs. Jobs are created by passing background: true to any fs tool. wait returns the original tool result when the job completes; a timeout returns the current status.",
|
|
300
301
|
input: JOB_SCHEMA,
|
|
301
302
|
options: { namespace: "fs", permission: "fs_job" },
|
|
302
|
-
execute: async (input,
|
|
303
|
+
execute: async (input, context) => {
|
|
303
304
|
try {
|
|
305
|
+
const owner = sessionOwner(context);
|
|
304
306
|
const parsed = parseSchema<{
|
|
305
307
|
id?: string;
|
|
306
308
|
wait?: boolean;
|
|
@@ -314,7 +316,7 @@ export default Plugin.define({
|
|
|
314
316
|
throw new FsError("list cannot be combined with id, wait, or cancel.", "invalid");
|
|
315
317
|
}
|
|
316
318
|
return {
|
|
317
|
-
content: JSON.stringify({ jobs: jobs.list() }, null, 2),
|
|
319
|
+
content: JSON.stringify({ jobs: jobs.list(owner) }, null, 2),
|
|
318
320
|
metadata: { ok: true },
|
|
319
321
|
};
|
|
320
322
|
}
|
|
@@ -325,7 +327,7 @@ export default Plugin.define({
|
|
|
325
327
|
if (parsed.wait === true) {
|
|
326
328
|
throw new FsError("cancel and wait are mutually exclusive.", "invalid");
|
|
327
329
|
}
|
|
328
|
-
const status = jobs.cancel(parsed.id);
|
|
330
|
+
const status = jobs.cancel(parsed.id, owner);
|
|
329
331
|
return {
|
|
330
332
|
content: JSON.stringify(status, null, 2),
|
|
331
333
|
metadata: { ok: true, jobId: status.id, state: status.state },
|
|
@@ -333,7 +335,11 @@ export default Plugin.define({
|
|
|
333
335
|
}
|
|
334
336
|
|
|
335
337
|
if (parsed.wait === true) {
|
|
336
|
-
const waited = await jobs.wait(
|
|
338
|
+
const waited = await jobs.wait(
|
|
339
|
+
parsed.id,
|
|
340
|
+
parsed.timeoutMs ?? 600000,
|
|
341
|
+
owner,
|
|
342
|
+
);
|
|
337
343
|
if (waited.result) {
|
|
338
344
|
return {
|
|
339
345
|
content: waited.result.content,
|
|
@@ -356,7 +362,7 @@ export default Plugin.define({
|
|
|
356
362
|
};
|
|
357
363
|
}
|
|
358
364
|
|
|
359
|
-
const status = jobs.status(parsed.id);
|
|
365
|
+
const status = jobs.status(parsed.id, owner);
|
|
360
366
|
return {
|
|
361
367
|
content: JSON.stringify(status, null, 2),
|
|
362
368
|
metadata: { ok: true, jobId: status.id, state: status.state },
|