opencode-fs-plugin 3.3.0 → 3.4.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
@@ -88,10 +88,12 @@ Prefer the canonical names.
88
88
 
89
89
  ## Pipe
90
90
 
91
- Direct results larger than 1 MiB are written to `/tmp/opencode/fs-spill`
92
- and returned as a 64 KiB preview plus `outputPath`, `bytes`, and `spilled`
93
- metadata; the rendered result points at the file. Pipe stages keep whole
94
- buffers so bytes pass unchanged.
91
+ Direct results larger than the spill threshold are written to
92
+ `/tmp/opencode/fs-spill` and returned as a 64 KiB preview plus `outputPath`,
93
+ `bytes`, and `spilled` metadata; the rendered result points at the file. The
94
+ threshold defaults to a quarter of free memory, clamped between 16 MiB and
95
+ 512 MiB; set `spillThresholdBytes` in the plugin options to fix it. Pipe stages
96
+ keep whole buffers so bytes pass unchanged.
95
97
 
96
98
  `fs.pipe` joins registered tools through byte buffers. Stages run in order;
97
99
  only pipeable tools can be used. Each stage asserts its own `fs_<tool>` action
package/SPEC.md CHANGED
@@ -128,11 +128,12 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
128
128
 
129
129
  ## Large output
130
130
 
131
- A direct tool result larger than 1 MiB is written to
132
- `/tmp/opencode/fs-spill/<tool>-<uuid>.out`. The tool returns a 64 KiB preview,
133
- `outputPath`, `bytes`, and `spilled: true`, and `renderResult` appends a note
134
- with the path. `fs.cat` streams into the spill sink; other tools spill their
135
- finished result. Pipe stages stay in memory so bytes pass between stages.
131
+ A direct tool result larger than the spill threshold is written to
132
+ `/tmp/opencode/fs-spill/<tool>-<uuid>.out`. The threshold is a quarter of free
133
+ memory, clamped to 16 MiB..512 MiB, or the `spillThresholdBytes` plugin option.
134
+ The tool returns a 64 KiB preview, `outputPath`, `bytes`, and `spilled: true`,
135
+ and `renderResult` appends a note with the path. `fs.cat` streams into the sink;
136
+ other tools spill their finished result. Pipe stages stay in memory.
136
137
 
137
138
  ## Deliberate gaps
138
139
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-fs-plugin",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22.12"
package/src/core/spill.ts CHANGED
@@ -5,16 +5,35 @@
5
5
 
6
6
  import { randomUUID } from "node:crypto";
7
7
  import * as fs from "node:fs/promises";
8
+ import * as os from "node:os";
8
9
  import type { FileHandle } from "node:fs/promises";
9
10
  import * as path from "node:path";
11
+ import { FsError } from "./errors.js";
10
12
  import type { ToolResult } from "./types.js";
11
13
 
12
- /** Bytes kept in memory before a direct call spills to a temporary file. */
13
- export const SPILL_THRESHOLD = 1024 * 1024;
14
+ /** The dynamic threshold never goes below this. */
15
+ export const MIN_SPILL_THRESHOLD = 16 * 1024 * 1024;
16
+
17
+ /** The dynamic threshold never goes above this. */
18
+ export const MAX_SPILL_THRESHOLD = 512 * 1024 * 1024;
14
19
 
15
20
  /** Bytes of a spilled result returned to the host as a preview. */
16
21
  export const SPILL_PREVIEW = 64 * 1024;
17
22
 
23
+ /** A quarter of free memory, clamped to a safe range. */
24
+ export function dynamicSpillThreshold(free = os.freemem()): number {
25
+ return Math.max(MIN_SPILL_THRESHOLD, Math.min(MAX_SPILL_THRESHOLD, Math.floor(free / 4)));
26
+ }
27
+
28
+ /** An explicit byte count from the plugin options; undefined means dynamic. */
29
+ export function normalizeSpillThreshold(value: unknown): number | undefined {
30
+ if (value === undefined) return undefined;
31
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
32
+ throw new FsError("spillThresholdBytes must be a positive number.", "invalid");
33
+ }
34
+ return Math.trunc(value);
35
+ }
36
+
18
37
  /** The shared directory for spilled outputs under the plugin's temp area. */
19
38
  export function spillDirectory(tmpdir: string): string {
20
39
  return path.join(tmpdir, "opencode", "fs-spill");
@@ -46,7 +65,7 @@ export class SpillSink {
46
65
  private readonly dir: string,
47
66
  private readonly name: string,
48
67
  private readonly memoryOnly = false,
49
- private readonly threshold = SPILL_THRESHOLD,
68
+ private readonly threshold = dynamicSpillThreshold(),
50
69
  private readonly previewBytes = SPILL_PREVIEW,
51
70
  ) {}
52
71
 
@@ -103,8 +122,13 @@ export class SpillSink {
103
122
  * Spill a finished tool result that is larger than the threshold. Tools that
104
123
  * already streamed into a sink carry `outputPath` and pass through.
105
124
  */
106
- export async function spillResult(result: ToolResult, name: string, tmpdir: string): Promise<ToolResult> {
107
- if (result.stdout.length <= SPILL_THRESHOLD || result.metadata?.outputPath !== undefined) return result;
125
+ export async function spillResult(
126
+ result: ToolResult,
127
+ name: string,
128
+ tmpdir: string,
129
+ threshold = dynamicSpillThreshold(),
130
+ ): Promise<ToolResult> {
131
+ if (result.stdout.length <= threshold || result.metadata?.outputPath !== undefined) return result;
108
132
  const sink = new SpillSink(spillDirectory(tmpdir), name, false, 0);
109
133
  try {
110
134
  await sink.write(result.stdout);
package/src/core/types.ts CHANGED
@@ -31,6 +31,8 @@ export interface RunContext {
31
31
  readonly signal: AbortSignal;
32
32
  readonly tmpdir: string;
33
33
  readonly execAllowlist: readonly string[];
34
+ /** Explicit spill threshold in bytes; unset means the dynamic default. */
35
+ readonly spillThreshold?: number;
34
36
  /** `proxy` forces egress through the run's allowlist proxy; `ip` is all-IP. */
35
37
  readonly execNetworkMode: "proxy" | "ip";
36
38
  }
package/src/index.ts CHANGED
@@ -12,7 +12,7 @@ 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 { spillResult } from "./core/spill.js";
15
+ import { normalizeSpillThreshold, spillResult } from "./core/spill.js";
16
16
  import { parseSchema } from "./core/schema.js";
17
17
  import type { ToolSpec } from "./core/types.js";
18
18
  import { arTool } from "./tools/ar.js";
@@ -201,6 +201,7 @@ export default Plugin.define({
201
201
  typeof ctx.options?.concurrency === "number" ? ctx.options.concurrency : undefined,
202
202
  );
203
203
  const execAllowlist = normalizeExecAllowlist(ctx.options?.execAllowlist);
204
+ const spillThreshold = normalizeSpillThreshold(ctx.options?.spillThresholdBytes);
204
205
  const execNetworkMode = normalizeExecNetworkMode(ctx.options?.execNetworkMode);
205
206
  const jobs = new JobManager(
206
207
  typeof ctx.options?.jobDir === "string" ? { dir: ctx.options.jobDir } : {},
@@ -251,11 +252,12 @@ export default Plugin.define({
251
252
  tmpdir: os.tmpdir(),
252
253
  execAllowlist,
253
254
  execNetworkMode,
255
+ ...(spillThreshold === undefined ? {} : { spillThreshold }),
254
256
  },
255
257
  parsed,
256
258
  {},
257
259
  );
258
- return renderResult(await spillResult(result, spec.name, os.tmpdir()));
260
+ return renderResult(await spillResult(result, spec.name, os.tmpdir(), spillThreshold));
259
261
  };
260
262
 
261
263
  if ((parsed as { background?: unknown }).background === true) {
package/src/tools/cat.ts CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { Type, type Static } from "@sinclair/typebox";
7
7
  import { pathsFrom } from "../core/aliases.js";
8
- import { SpillSink, spillDirectory } from "../core/spill.js";
8
+ import { SpillSink, dynamicSpillThreshold, spillDirectory } from "../core/spill.js";
9
9
  import { inputStream, lines } from "../core/stream.js";
10
10
  import type { ToolSpec } from "../core/types.js";
11
11
 
@@ -36,7 +36,7 @@ export const catTool: ToolSpec<Input> = {
36
36
  const paths = pathsFrom(input) ?? [];
37
37
  const targets = await resolvePaths(ctx, paths);
38
38
  const stream = inputStream(targets.map((entry) => entry.full), io.stdin);
39
- const sink = new SpillSink(spillDirectory(ctx.tmpdir), "cat", io.piped === true);
39
+ const sink = new SpillSink(spillDirectory(ctx.tmpdir), "cat", io.piped === true, ctx.spillThreshold ?? dynamicSpillThreshold());
40
40
  try {
41
41
  if (
42
42
  input.number !== true &&