Skip to content

Commit bfea409

Browse files
committed
fix(errors): classify remove outcomes as operational
1 parent e96eb9c commit bfea409

6 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
### Security and Correctness
1010

11-
- Report the documented `remove()` failure codes instead of collapsing every failure to `path-alias`. A missing target now throws `not-found`, a non-empty directory throws `not-empty`, and any other filesystem failure throws `not-removable`, while directory identity drift keeps reporting `path-mismatch`. The errno classification covers only the deletion syscalls, so a parent-directory guard failure still fails closed with `path-alias` rather than implying a removal was attempted.
11+
- Report documented `remove()` failures instead of collapsing them to the policy code `path-alias`: missing targets now throw `not-found`, non-empty directories throw `not-empty`, and other deletion syscall failures throw `not-removable`; consumers that caught `path-alias` around `remove()` must handle these operational codes after upgrading. These three codes now have `category: "operational"` package-wide, changing the category of pre-existing `not-found` errors from `"policy"`; directory identity drift remains `path-mismatch`, and ambiguous parent-guard failures remain fail-closed as `path-alias`. Thanks @Yigtwxx (#84).
1212

1313
## 0.5.1 - 2026-08-01
1414

‎README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,17 @@ Codes are grouped by category:
506506
```ts
507507
if (err instanceof FsSafeError) {
508508
if (err.category === "policy") {
509-
// Unsafe caller input or filesystem state.
509+
// Unsafe caller input or filesystem state rejected by a safety policy.
510510
} else {
511-
// Operational problem such as helper startup, timeout, or unverifiable permissions.
511+
// Routine filesystem outcome or runtime/environment problem.
512512
}
513513
}
514514
```
515515

516+
Routine filesystem outcomes such as `not-found`, `not-empty`, and
517+
`not-removable` are operational; they do not indicate that a filesystem
518+
boundary policy was violated.
519+
516520
Current `FsSafeErrorCode` values are `already-exists`, `denied-path`, `device-path`, `hardlink`, `helper-failed`, `helper-unavailable`, `invalid-path`, `insecure-permissions`, `not-empty`, `not-file`, `not-found`, `not-owned`, `not-removable`, `outside-workspace`, `path-alias`, `path-mismatch`, `permission-unverified`, `secret-exists`, `symlink`, `timeout`, `too-large`, and `unsupported-platform`.
517521

518522
## Safety model

‎docs/errors.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ destination.
5050

5151
`category` separates caller-policy failures from operational failures:
5252

53-
- `"policy"` — unsafe input or target state, such as `outside-workspace`, `symlink`, `hardlink`, or `too-large`.
54-
- `"operational"` — environment/runtime failures, such as helper startup, platform support, timeout, or unverifiable permissions.
53+
- `"policy"` — unsafe input or target state rejected by a safety policy, such as `outside-workspace`, `symlink`, `hardlink`, or `too-large`.
54+
- `"operational"` — routine filesystem outcomes or environment/runtime failures, such as `not-found`, `not-empty`, `not-removable`, helper startup, platform support, timeout, or unverifiable permissions.
55+
56+
Routine absence or inability to remove a path does not by itself indicate a
57+
filesystem boundary violation. Branch on the specific code when the distinction
58+
between those operational outcomes matters.
5559

5660
## Code union
5761

‎docs/types.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ type FsSafeErrorCode =
161161

162162
Closed union you switch on. See the [Errors](errors.md) reference for what each one means.
163163

164-
`FsSafeError.category` is `"policy"` for unsafe input/target-state failures and `"operational"` for environment/runtime failures.
164+
`FsSafeError.category` is `"policy"` for unsafe input or target state rejected by a safety policy and `"operational"` for routine filesystem outcomes or environment/runtime failures. `not-found`, `not-empty`, and `not-removable` are operational.
165165

166166
## See also
167167

‎src/errors.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export type FsSafeErrorDetails = Readonly<Record<string, unknown>>;
2929
const OPERATIONAL_CODES: ReadonlySet<FsSafeErrorCode> = new Set([
3030
"helper-failed",
3131
"helper-unavailable",
32+
"not-empty",
33+
"not-found",
34+
"not-removable",
3235
"permission-unverified",
3336
"timeout",
3437
"unsupported-platform",

‎test/fs-safe.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { chmod, mkdtemp, readdir, readFile, rename, rm, stat, symlink, writeFile
33
import os from "node:os";
44
import path from "node:path";
55
import { afterEach, describe, expect, it } from "vitest";
6-
import { configureFsSafeNative, FsSafeError, root as openRoot } from "../src/index.js";
6+
import {
7+
categorizeFsSafeError,
8+
configureFsSafeNative,
9+
FsSafeError,
10+
root as openRoot,
11+
} from "../src/index.js";
712
import { openLocalFileSafely, readLocalFileSafely } from "../src/root.js";
813
import { __setFsSafeTestHooksForTest } from "../src/test-hooks.js";
914
import { expectedFsSafeCode } from "./helpers/security.js";
@@ -26,6 +31,20 @@ afterEach(async () => {
2631
});
2732

2833
describe("@openclaw/fs-safe", () => {
34+
it("classifies routine removal outcomes as operational", () => {
35+
for (const code of ["not-found", "not-empty", "not-removable"] as const) {
36+
expect(categorizeFsSafeError(code)).toBe("operational");
37+
expect(new FsSafeError(code, "remove failed")).toMatchObject({
38+
category: "operational",
39+
code,
40+
});
41+
}
42+
43+
for (const code of ["path-alias", "path-mismatch"] as const) {
44+
expect(categorizeFsSafeError(code)).toBe("policy");
45+
}
46+
});
47+
2948
it.skipIf(skipOnWindows)("reuses a root capability across filesystem operations", async () => {
3049
const rootPath = await tempRoot("fs-root-object-");
3150
const root = await openRoot(rootPath);

0 commit comments

Comments
 (0)