@openclaw/feishu 2026.10.1-beta.2 → 2026.10.2

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.
Files changed (85) hide show
  1. package/dist/.setup/{channel-CX62T71S.mjs → channel-DZakdZ3r.mjs} +7 -7
  2. package/dist/.setup/{channel.runtime-COiaq-Rv.mjs → channel.runtime-rXFuQXLL.mjs} +6 -6
  3. package/dist/.setup/{chat-BdyvImsY.mjs → chat-mTBvMMvx.mjs} +2 -2
  4. package/dist/.setup/{client-BsBVWJzN.mjs → client-CcxmEkoy.mjs} +1 -1
  5. package/dist/.setup/{doctor-contract-B1B9ogdd.mjs → doctor-contract-DyOo8NvC.mjs} +15 -5
  6. package/dist/.setup/{monitor-7DvwUs4Y.mjs → monitor-CXaFHMmi.mjs} +3 -3
  7. package/dist/.setup/{monitor.account-7xVu7XWY.mjs → monitor.account-cJ8gavqv.mjs} +27 -15
  8. package/dist/.setup/{probe-B9o7iey6.mjs → probe-BjSykXB8.mjs} +2 -2
  9. package/dist/.setup/{reply-delivery-result-Bt-BWBEA.mjs → reply-delivery-result-laBNOmBu.mjs} +2 -2
  10. package/dist/.setup/{setup-api-DmzX9S01.mjs → setup-api-fZcYXGxe.mjs} +1 -1
  11. package/dist/.setup/{subagent-hooks-CUXg0pC-.mjs → subagent-hooks-C8Z_x3KJ.mjs} +1 -1
  12. package/dist/api.js +5 -5
  13. package/dist/channel-plugin-api.js +1 -1
  14. package/dist/doctor-contract-api.js +2 -2
  15. package/dist/setup-api.js +1 -1
  16. package/dist/setup-entry.js +1 -1
  17. package/dist/subagent-hooks-api.js +1 -1
  18. package/node_modules/@openclaw/fs-safe/CHANGELOG.md +56 -0
  19. package/node_modules/@openclaw/fs-safe/dist/advanced.d.ts +1 -0
  20. package/node_modules/@openclaw/fs-safe/dist/advanced.js +1 -0
  21. package/node_modules/@openclaw/fs-safe/dist/archive-entry.js +7 -3
  22. package/node_modules/@openclaw/fs-safe/dist/copy-root-file-sync.d.ts +35 -0
  23. package/node_modules/@openclaw/fs-safe/dist/copy-root-file-sync.js +218 -0
  24. package/node_modules/@openclaw/fs-safe/dist/create.d.ts +1 -0
  25. package/node_modules/@openclaw/fs-safe/dist/create.js +4 -1
  26. package/node_modules/@openclaw/fs-safe/dist/entry-publication.js +3 -1
  27. package/node_modules/@openclaw/fs-safe/dist/errors.d.ts +2 -0
  28. package/node_modules/@openclaw/fs-safe/dist/errors.js +5 -0
  29. package/node_modules/@openclaw/fs-safe/dist/index.d.ts +1 -1
  30. package/node_modules/@openclaw/fs-safe/dist/index.js +1 -1
  31. package/node_modules/@openclaw/fs-safe/dist/native-binding.d.ts +3 -0
  32. package/node_modules/@openclaw/fs-safe/dist/native-move-fallback.d.ts +3 -0
  33. package/node_modules/@openclaw/fs-safe/dist/native-move-fallback.js +54 -0
  34. package/node_modules/@openclaw/fs-safe/dist/native-noreplace.d.ts +8 -0
  35. package/node_modules/@openclaw/fs-safe/dist/native-noreplace.js +55 -0
  36. package/node_modules/@openclaw/fs-safe/dist/native-pinned-write.d.ts +1 -1
  37. package/node_modules/@openclaw/fs-safe/dist/native-pinned-write.js +12 -2
  38. package/node_modules/@openclaw/fs-safe/dist/native-rename-outcome.js +3 -1
  39. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.d.ts +1 -1
  40. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.js +48 -5
  41. package/node_modules/@openclaw/fs-safe/dist/pinned-write-input.d.ts +2 -0
  42. package/node_modules/@openclaw/fs-safe/dist/pinned-write-input.js +17 -0
  43. package/node_modules/@openclaw/fs-safe/dist/pinned-write.js +1 -1
  44. package/node_modules/@openclaw/fs-safe/dist/publish-file-failure.d.ts +5 -0
  45. package/node_modules/@openclaw/fs-safe/dist/publish-file-failure.js +1 -0
  46. package/node_modules/@openclaw/fs-safe/dist/publish-file-native.d.ts +4 -0
  47. package/node_modules/@openclaw/fs-safe/dist/publish-file-native.js +35 -0
  48. package/node_modules/@openclaw/fs-safe/dist/publish-file.d.ts +2 -0
  49. package/node_modules/@openclaw/fs-safe/dist/publish-file.js +8 -2
  50. package/node_modules/@openclaw/fs-safe/dist/replace-directory.js +6 -5
  51. package/node_modules/@openclaw/fs-safe/dist/root-errors.js +25 -4
  52. package/node_modules/@openclaw/fs-safe/dist/root-impl.js +3 -3
  53. package/node_modules/@openclaw/fs-safe/dist/root-move-noreplace.d.ts +4 -2
  54. package/node_modules/@openclaw/fs-safe/dist/root-move-noreplace.js +13 -17
  55. package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.js +18 -3
  56. package/node_modules/@openclaw/fs-safe/dist/staged-file-settlement.js +4 -1
  57. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-owner.d.ts +1 -0
  58. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-owner.js +27 -4
  59. package/node_modules/@openclaw/fs-safe/docs/advanced.md +1 -0
  60. package/node_modules/@openclaw/fs-safe/docs/archive.md +2 -1
  61. package/node_modules/@openclaw/fs-safe/docs/copy.md +61 -1
  62. package/node_modules/@openclaw/fs-safe/docs/durability.md +50 -5
  63. package/node_modules/@openclaw/fs-safe/docs/errors.md +37 -0
  64. package/node_modules/@openclaw/fs-safe/docs/native-helper.md +5 -3
  65. package/node_modules/@openclaw/fs-safe/docs/native.md +39 -6
  66. package/node_modules/@openclaw/fs-safe/docs/public-api.md +3 -1
  67. package/node_modules/@openclaw/fs-safe/docs/staged-file.md +10 -3
  68. package/node_modules/@openclaw/fs-safe/docs/watch.md +3 -1
  69. package/node_modules/@openclaw/fs-safe/docs/writing.md +40 -11
  70. package/node_modules/@openclaw/fs-safe/package.json +11 -11
  71. package/node_modules/@openclaw/fs-safe-darwin-arm64/fs-safe-native.node +0 -0
  72. package/node_modules/@openclaw/fs-safe-darwin-arm64/package.json +1 -1
  73. package/node_modules/@openclaw/fs-safe-darwin-x64/fs-safe-native.node +0 -0
  74. package/node_modules/@openclaw/fs-safe-darwin-x64/package.json +1 -1
  75. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/fs-safe-native.node +0 -0
  76. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/package.json +1 -1
  77. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/fs-safe-native.node +0 -0
  78. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/package.json +1 -1
  79. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/fs-safe-native.node +0 -0
  80. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/package.json +1 -1
  81. package/node_modules/@openclaw/fs-safe-linux-x64-musl/fs-safe-native.node +0 -0
  82. package/node_modules/@openclaw/fs-safe-linux-x64-musl/package.json +1 -1
  83. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/fs-safe-native.node +0 -0
  84. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/package.json +1 -1
  85. package/package.json +5 -5
@@ -0,0 +1,218 @@
1
+ import fs, {} from "node:fs";
2
+ import path from "node:path";
3
+ import { normalizeMaxBytes } from "./byte-budget.js";
4
+ import { resolveCopyCloneMode } from "./copy-policy.js";
5
+ import { createFileWithAdmissionSync } from "./create.js";
6
+ import { ownFileDescriptorSync } from "./create-owned-file.js";
7
+ import { creationAdmissionFromParent, removeRecordedCreationFileSync } from "./creation-boundary.js";
8
+ import { assertSyncDirectoryGuard, captureDirectoryGuard } from "./directory-guard.js";
9
+ import { FsSafeError } from "./errors.js";
10
+ import { assertExclusiveCreateLeaf } from "./exclusive-create.js";
11
+ import { copyFileDescriptorSync } from "./file-handle-transfer.js";
12
+ import { captureNativeFdClose } from "./native-binding.js";
13
+ import { getNativeBinding } from "./native.js";
14
+ import { realpathSync } from "./realpath.js";
15
+ import { openRootFileSync } from "./root-file.js";
16
+ import { resolveRootPathSync, ROOT_PATH_ALIAS_POLICIES } from "./root-path.js";
17
+ import { inspectOpenedPathIdentitySync } from "./root-read-admission.js";
18
+ import { openStagedDirectory } from "./staged-directory.js";
19
+ import { inspectFileIdentitySync } from "./strict-file-identity.js";
20
+ function copyError(error) {
21
+ if (error instanceof FsSafeError)
22
+ return error;
23
+ const code = error?.code;
24
+ return new FsSafeError(code === "EEXIST" ? "already-exists" : code === "ENOENT" ? "not-found" :
25
+ code === "too-large" ? "too-large" :
26
+ ["ENOTSUP", "EOPNOTSUPP", "ENOSYS", "EXDEV", "EINVAL"].includes(code ?? "")
27
+ ? "unsupported-platform" : "helper-failed", "guarded synchronous file copy failed", { cause: error });
28
+ }
29
+ function admitCopyPath(input, create = false) {
30
+ if (!path.isAbsolute(input.rootPath) || !path.isAbsolute(input.absolutePath)) {
31
+ throw new FsSafeError("invalid-path", "copy roots and paths must be absolute");
32
+ }
33
+ const root = captureDirectoryGuard(realpathSync(input.rootPath), "native", { bigint: true });
34
+ const resolve = () => {
35
+ const parent = create ? resolveRootPathSync({
36
+ ...input, absolutePath: path.dirname(input.absolutePath), boundaryLabel: "copy parent",
37
+ rejectSymlinks: true, rejectFinalSymlink: true,
38
+ }) : undefined;
39
+ const selected = resolveRootPathSync({
40
+ ...input, boundaryLabel: "copy", rejectSymlinks: !create, rejectFinalSymlink: !create,
41
+ policy: create ? ROOT_PATH_ALIAS_POLICIES.unlinkTarget : undefined,
42
+ });
43
+ if (parent && path.dirname(selected.canonicalPath) !== parent.canonicalPath) {
44
+ throw new FsSafeError("path-mismatch", "copy destination parent changed");
45
+ }
46
+ return selected;
47
+ };
48
+ const selected = resolve();
49
+ const parent = captureDirectoryGuard(path.dirname(selected.canonicalPath), "native", { bigint: true });
50
+ const assertCurrent = () => {
51
+ assertSyncDirectoryGuard(root);
52
+ assertSyncDirectoryGuard(parent);
53
+ const current = resolve();
54
+ if (current.rootCanonicalPath !== root.realPath || current.canonicalPath !== selected.canonicalPath) {
55
+ throw new FsSafeError("path-mismatch", "copy path changed during operation");
56
+ }
57
+ assertSyncDirectoryGuard(root);
58
+ assertSyncDirectoryGuard(parent);
59
+ };
60
+ assertCurrent();
61
+ return { path: selected.canonicalPath, parent, assertCurrent };
62
+ }
63
+ /** Exclusively copies an admitted source and transfers the owned destination fd. */
64
+ export function copyRootFileSync(options) {
65
+ // Snapshot caller options before the first filesystem observation.
66
+ const sourceInput = options.source;
67
+ const sourcePath = { rootPath: sourceInput.rootPath, absolutePath: sourceInput.absolutePath };
68
+ const targetInput = options.destination;
69
+ const targetPath = { rootPath: targetInput.rootPath, absolutePath: targetInput.absolutePath };
70
+ const expectedInput = options.expectedSourceIdentity;
71
+ const expectedSourceIdentity = expectedInput === undefined ? undefined : { dev: expectedInput.dev, ino: expectedInput.ino };
72
+ const clone = resolveCopyCloneMode(options.clone, "never");
73
+ const maxBytes = normalizeMaxBytes(options.maxBytes);
74
+ const mode = options.mode;
75
+ const preserveSourceMode = options.preserveSourceMode;
76
+ const sourceHardlinks = options.sourceHardlinks ?? "reject";
77
+ if (mode !== undefined && (!Number.isInteger(mode) || mode < 0 || mode > 0o777)) {
78
+ throw new FsSafeError("invalid-path", "copy mode must be an integer from 0 to 0o777");
79
+ }
80
+ if (sourceHardlinks !== "allow" && sourceHardlinks !== "reject") {
81
+ throw new FsSafeError("invalid-path", "invalid copy source hardlink policy");
82
+ }
83
+ let sourceOwner;
84
+ let targetOwner;
85
+ let parentOwner;
86
+ let parentOpen = false;
87
+ let target;
88
+ let identity;
89
+ const native = getNativeBinding();
90
+ const nativeCopy = process.platform !== "win32" && native?.copyFileExclusiveSync && native.removeStagedFile
91
+ ? native.copyFileExclusiveSync.bind(native) : undefined;
92
+ let nativeTarget = false;
93
+ try {
94
+ const source = admitCopyPath(sourcePath);
95
+ target = admitCopyPath(targetPath, true);
96
+ // Preserve exclusive-create collision semantics for dangling Windows leaves.
97
+ assertExclusiveCreateLeaf(targetPath.absolutePath);
98
+ if (fs.lstatSync(target.path, { throwIfNoEntry: false })) {
99
+ throw new FsSafeError("already-exists", "copy destination already exists");
100
+ }
101
+ const opened = openRootFileSync({
102
+ ...sourcePath, boundaryLabel: "copy source", maxBytes,
103
+ rejectHardlinks: sourceHardlinks !== "allow",
104
+ });
105
+ if (!opened.ok)
106
+ throw opened.error ?? new FsSafeError("helper-failed", "copy source admission failed");
107
+ sourceOwner = ownFileDescriptorSync(opened.fd);
108
+ const sourceIdentity = inspectFileIdentitySync(() => fs.fstatSync(opened.fd, { bigint: true }), expectedSourceIdentity);
109
+ const verifySource = () => {
110
+ source.assertCurrent();
111
+ for (const inspect of [
112
+ () => fs.fstatSync(opened.fd, { bigint: true }),
113
+ () => inspectOpenedPathIdentitySync(source.path, undefined),
114
+ ]) {
115
+ const current = inspectFileIdentitySync(inspect, sourceIdentity);
116
+ if (!current.isFile())
117
+ throw new FsSafeError("not-file", "copy source must be a regular file");
118
+ if (sourceHardlinks !== "allow" && current.nlink > 1n) {
119
+ throw new FsSafeError("hardlink", "copy source must not be hardlinked");
120
+ }
121
+ }
122
+ };
123
+ const selectedMode = mode ?? (preserveSourceMode ? Number(sourceIdentity.mode & 511n) : 0o600 & ~process.umask());
124
+ let method = "copy";
125
+ let copied;
126
+ verifySource();
127
+ target.assertCurrent();
128
+ if (nativeCopy) {
129
+ const closeNative = captureNativeFdClose(native);
130
+ const parent = openStagedDirectory(target.parent.realPath);
131
+ parentOwner = ownFileDescriptorSync(parent.fd);
132
+ parentOpen = true;
133
+ inspectFileIdentitySync(() => fs.fstatSync(parent.fd, { bigint: true }), { dev: BigInt(target.parent.stat.dev), ino: BigInt(target.parent.stat.ino) });
134
+ target.assertCurrent();
135
+ copied = nativeCopy(opened.fd, parent.fd, path.basename(target.path), clone, maxBytes !== undefined && Number.isFinite(maxBytes) ? maxBytes : undefined);
136
+ targetOwner = ownFileDescriptorSync(copied.fd, closeNative);
137
+ nativeTarget = true;
138
+ }
139
+ else {
140
+ if (clone === "always")
141
+ throw new FsSafeError("unsupported-platform", "synchronous native file cloning is unavailable");
142
+ targetOwner = createFileWithAdmissionSync(target.path, { mode: 0o600, assertBeforeMutation: target.assertCurrent }, creationAdmissionFromParent(target.parent));
143
+ }
144
+ identity = inspectFileIdentitySync(() => fs.fstatSync(targetOwner.fd, { bigint: true }));
145
+ const verifyTarget = () => {
146
+ target.assertCurrent();
147
+ const current = inspectFileIdentitySync(() => fs.fstatSync(targetOwner.fd, { bigint: true }), identity);
148
+ const named = inspectFileIdentitySync(() => inspectOpenedPathIdentitySync(target.path, undefined), identity);
149
+ if (!current.isFile() || current.nlink !== 1n || !named.isFile() || named.nlink !== 1n) {
150
+ throw new FsSafeError("path-mismatch", "copy destination changed");
151
+ }
152
+ return current;
153
+ };
154
+ verifyTarget();
155
+ if (copied?.errorCode) {
156
+ throw copyError(Object.assign(new Error(copied.errorMessage), { code: copied.errorCode }));
157
+ }
158
+ if (copied) {
159
+ if (!["clone", "copy-file-range", "copy"].includes(copied.method)) {
160
+ throw new FsSafeError("helper-failed", "native copy returned an unknown method");
161
+ }
162
+ method = copied.method;
163
+ }
164
+ else {
165
+ copyFileDescriptorSync(opened.fd, targetOwner.fd, { maxBytes });
166
+ }
167
+ verifySource();
168
+ const completed = verifyTarget();
169
+ if (maxBytes !== undefined && Number.isFinite(maxBytes) && completed.size > BigInt(maxBytes)) {
170
+ throw new FsSafeError("too-large", "copy exceeds maxBytes");
171
+ }
172
+ fs.fchmodSync(targetOwner.fd, selectedMode);
173
+ verifySource();
174
+ verifyTarget();
175
+ sourceOwner.close();
176
+ parentOpen = false;
177
+ parentOwner?.close();
178
+ return Object.freeze({
179
+ ...targetOwner, path: target.path, bytes: Number(completed.size), method,
180
+ identity: Object.freeze({ dev: identity.dev, ino: identity.ino }),
181
+ sourceIdentity: Object.freeze({ dev: sourceIdentity.dev, ino: sourceIdentity.ino }),
182
+ });
183
+ }
184
+ catch (error) {
185
+ const primary = copyError(error);
186
+ const cleanup = [];
187
+ if (targetOwner && target) {
188
+ try {
189
+ if (nativeTarget && parentOpen) {
190
+ const removed = native.removeStagedFile(parentOwner.fd, path.basename(target.path), targetOwner.fd);
191
+ if (removed === "preserved")
192
+ throw new FsSafeError("path-mismatch", "copy cleanup preserved a replacement");
193
+ }
194
+ else if (identity) {
195
+ removeRecordedCreationFileSync(target.path, identity, target.assertCurrent);
196
+ }
197
+ }
198
+ catch (failure) {
199
+ cleanup.push(failure);
200
+ }
201
+ }
202
+ for (const owner of [targetOwner, sourceOwner, parentOwner]) {
203
+ try {
204
+ owner?.close();
205
+ }
206
+ catch (failure) {
207
+ cleanup.push(failure);
208
+ }
209
+ }
210
+ if (cleanup.length) {
211
+ throw new FsSafeError(primary.code, primary.message, {
212
+ cause: new AggregateError([primary, ...cleanup], "copy and cleanup failed"),
213
+ details: { path: target?.path, cleanup: "failed" },
214
+ });
215
+ }
216
+ throw primary;
217
+ }
218
+ }
@@ -4,6 +4,7 @@ import { type CreationPermissions, type CreationParentIdentity } from "./creatio
4
4
  export type { OwnedFileDescriptorSync } from "./create-owned-file.js";
5
5
  export type CreateFileOptions = CreateDirectoryOptions;
6
6
  export declare function createFileSync(targetPath: string, options?: CreateFileOptions): OwnedFileDescriptorSync;
7
+ export declare function createFileWithAdmissionSync(targetPath: string, options?: CreateFileOptions, admission?: CreationAdmission): OwnedFileDescriptorSync;
7
8
  /** Internal async writer; Windows preparation uses the nonblocking stage owner. */
8
9
  export declare function createFileHandle(targetPath: string, options?: CreateFileOptions, admission?: CreationAdmission): Promise<FileHandle>;
9
10
  export type CreateDirectoryOptions = CreationPermissions & {
@@ -108,9 +108,12 @@ function createPrivateWindowsFile(selected, options, mode) {
108
108
  }
109
109
  }
110
110
  export function createFileSync(targetPath, options = {}) {
111
+ return createFileWithAdmissionSync(targetPath, options);
112
+ }
113
+ export function createFileWithAdmissionSync(targetPath, options = {}, admission = {}) {
111
114
  const permissions = resolveCreationPermissions(options, false);
112
115
  const assertion = options.assertBeforeMutation;
113
- const selected = prepareCreationPath(targetPath);
116
+ const selected = prepareCreationPath(targetPath, admission.expectedParentIdentity);
114
117
  if (permissions.private && process.platform === "win32") {
115
118
  assertPrivateFileCreationAvailable();
116
119
  return createPrivateWindowsFile(selected, { ...permissions, assertBeforeMutation: assertion }, permissions.mode);
@@ -2,6 +2,7 @@ import fs, {} from "node:fs";
2
2
  import path from "node:path";
3
3
  import { assertDirectoryIdentitySync } from "./directory-guard.js";
4
4
  import { FsSafeError } from "./errors.js";
5
+ import { noReplaceUnavailable } from "./native-noreplace.js";
5
6
  import { assertSynchronousCallbackResult } from "./mutation-authority.js";
6
7
  import { captureNativeFdClose } from "./native-binding.js";
7
8
  import { requireNativeBinding } from "./native.js";
@@ -120,7 +121,8 @@ class Publication {
120
121
  }
121
122
  transition = native.outcome; // Capture BEFORE diagnostics, observations or close.
122
123
  if (native.errorCode || transition !== "committed") {
123
- throw Object.assign(new Error(native.errorMessage ?? "entry publication did not commit"), { code: native.errorCode ?? "helper-failed" });
124
+ const error = Object.assign(new Error(native.errorMessage ?? "entry publication did not commit"), { code: native.errorCode ?? "helper-failed" });
125
+ throw noReplaceUnavailable(error, "retained entry publication") ?? error;
124
126
  }
125
127
  phase = "postcheck";
126
128
  verification = "failed";
@@ -12,4 +12,6 @@ export declare class FsSafeError extends Error {
12
12
  details?: FsSafeErrorDetails;
13
13
  });
14
14
  }
15
+ /** A rejected no-replace rename capability, not a generic missing native helper. */
16
+ export declare function isNoReplaceUnsupported(error: unknown): error is FsSafeError;
15
17
  export {};
@@ -25,3 +25,8 @@ export class FsSafeError extends Error {
25
25
  this.details = options.details;
26
26
  }
27
27
  }
28
+ /** A rejected no-replace rename capability, not a generic missing native helper. */
29
+ export function isNoReplaceUnsupported(error) {
30
+ return error instanceof FsSafeError && error.code === "helper-unavailable" &&
31
+ error.details?.capability === "rename-noreplace";
32
+ }
@@ -1,4 +1,4 @@
1
- export { FsSafeError, categorizeFsSafeError, } from "./errors.js";
1
+ export { FsSafeError, categorizeFsSafeError, isNoReplaceUnsupported, } from "./errors.js";
2
2
  export type * from "./errors.js";
3
3
  export { DEFAULT_ROOT_MAX_BYTES, root, type Root, } from "./root.js";
4
4
  export type * from "./root-public-types.js";
@@ -1,4 +1,4 @@
1
- export { FsSafeError, categorizeFsSafeError, } from "./errors.js";
1
+ export { FsSafeError, categorizeFsSafeError, isNoReplaceUnsupported, } from "./errors.js";
2
2
  export { DEFAULT_ROOT_MAX_BYTES, root, } from "./root.js";
3
3
  export { configureFsSafeNative, getFsSafeNativeConfig, } from "./native-config.js";
4
4
  export { writeExternalFileWithinRoot, } from "./output.js";
@@ -203,6 +203,7 @@ export interface NativeBinding {
203
203
  stagedFileMatches?(parentFd: number, basename: string, fileFd: number): boolean;
204
204
  removeStagedFile?(parentFd: number, basename: string, fileFd: number): "removed" | "name-absent" | "preserved";
205
205
  copyFileExclusive?(sourceFd: number, parentFd: number, basename: string, clone: CopyCloneMode, maxBytes: number | undefined, signal: AbortSignal | undefined): Promise<NativeFileCopyResult>;
206
+ copyFileExclusiveSync?(sourceFd: number, parentFd: number, basename: string, clone: CopyCloneMode, maxBytes: number | undefined): NativeFileCopyResult;
206
207
  cloneFileExclusive(sourceFd: number, targetRootFd: number, targetRelPath: string): number;
207
208
  copyFileRangeExclusive(sourceFd: number, targetRootFd: number, targetRelPath: string): Promise<NativeCopyResult>;
208
209
  createPrivateDirectory(path: string): void;
@@ -235,6 +236,8 @@ export interface NativeBinding {
235
236
  entryPublicationFilesystem?(parentFd: number): string;
236
237
  publishRetainedEntryNoReplace?(sourceParentFd: number, sourceBasename: string, sourceFd: number, targetParentFd: number, targetBasename: string): NativePublicationTransition;
237
238
  renameNoReplace(...args: NativeTwoPathArgs): void;
239
+ /** Linux auto-mode fallback through already-admitted direct-child parents. */
240
+ moveNoReplaceFallback?(...args: [...paths: NativeTwoPathArgs, expectedSourceDev: bigint, expectedSourceIno: bigint]): void;
238
241
  /** Identity-fenced retained-directory rename capability. */
239
242
  renameNoReplaceWithIdentity?(...args: [...paths: NativeTwoPathArgs, expectedSourceDev: bigint, expectedSourceIno: bigint]): void;
240
243
  renameReplace(...args: NativeTwoPathArgs): void;
@@ -0,0 +1,3 @@
1
+ import type { BigIntStats } from "node:fs";
2
+ import type { NativeBinding } from "./native-binding.js";
3
+ export declare function nativeMoveNoReplace(binding: NativeBinding, paths: Parameters<NativeBinding["renameNoReplace"]>, expected: BigIntStats | undefined, allowFallback: boolean, operation?: string): "link-unlink" | undefined;
@@ -0,0 +1,54 @@
1
+ import { FsSafeError } from "./errors.js";
2
+ import { cachedNoReplaceUnavailable, noReplaceUnavailable, rememberNoReplaceUnavailable, } from "./native-noreplace.js";
3
+ function fallbackError(error, operation) {
4
+ const code = error?.code;
5
+ const message = error instanceof Error ? error.message : "native no-replace move fallback failed";
6
+ if (code === "EEXIST") {
7
+ return new FsSafeError("already-exists", "no-replace fallback destination exists", { cause: error });
8
+ }
9
+ if (code === "FS_SAFE_INTERNAL_MOVE_LINK_UNSUPPORTED") {
10
+ return new FsSafeError("helper-unavailable", message, { cause: error,
11
+ details: { capability: "rename-noreplace", fallback: "link-unlink", fallbackCapability: "linkat" } });
12
+ }
13
+ const sourceRemoval = new Map([
14
+ ["FS_SAFE_INTERNAL_MOVE_LINK_CHANGED", "not-attempted"],
15
+ ["FS_SAFE_INTERNAL_MOVE_SOURCE_LINKED", "still-linked"],
16
+ ["FS_SAFE_INTERNAL_MOVE_UNLINK_UNVERIFIED", "unverified"],
17
+ ["FS_SAFE_INTERNAL_MOVE_PUBLISHED", "removed"],
18
+ ]).get(code ?? "");
19
+ if (sourceRemoval) {
20
+ return new FsSafeError(code === "FS_SAFE_INTERNAL_MOVE_LINK_CHANGED" ? "path-mismatch" : "helper-failed", message, { cause: error, details: { operation, fallback: "link-unlink",
21
+ publication: "published", sourceRemoval } });
22
+ }
23
+ // Keep identity errors available to the caller's normalizer.
24
+ return error;
25
+ }
26
+ export function nativeMoveNoReplace(binding, paths, expected, allowFallback, operation = "move") {
27
+ const [sourceFd, source, targetFd, target] = paths;
28
+ let unavailable = cachedNoReplaceUnavailable(binding, sourceFd, operation);
29
+ if (!unavailable) {
30
+ try {
31
+ binding.renameNoReplace(...paths);
32
+ return;
33
+ }
34
+ catch (error) {
35
+ unavailable = noReplaceUnavailable(error, operation, true);
36
+ if (!unavailable)
37
+ throw error;
38
+ // As in sibling publication/quarantine, distinct sibling names cannot
39
+ // have a directory-ancestry EINVAL. Cross-parent moves must not cache it.
40
+ if (sourceFd === targetFd && source !== target) {
41
+ rememberNoReplaceUnavailable(binding, sourceFd, unavailable);
42
+ }
43
+ }
44
+ }
45
+ if (!allowFallback || process.platform !== "linux" || !binding.moveNoReplaceFallback || !expected)
46
+ throw unavailable;
47
+ try {
48
+ binding.moveNoReplaceFallback(...paths, expected.dev, expected.ino);
49
+ return expected.isFile() ? "link-unlink" : undefined;
50
+ }
51
+ catch (error) {
52
+ throw fallbackError(error, operation);
53
+ }
54
+ }
@@ -0,0 +1,8 @@
1
+ import { FsSafeError } from "./errors.js";
2
+ import type { NativeBinding } from "./native-binding.js";
3
+ export declare const NATIVE_NOREPLACE_UNSUPPORTED = "FS_SAFE_INTERNAL_RENAME_NOREPLACE_UNSUPPORTED";
4
+ export declare function isNoReplaceUnavailable(error: unknown): error is FsSafeError;
5
+ export declare function noReplaceUnavailable(error: unknown, operation: string, syscallOnly?: boolean): FsSafeError | undefined;
6
+ export declare function rememberNoReplaceUnavailable(binding: NativeBinding, parentFd: number, error: FsSafeError): void;
7
+ export declare function cachedNoReplaceUnavailable(binding: NativeBinding, parentFd: number, operation?: string): FsSafeError | undefined;
8
+ export declare function cachedNoReplaceDevice(binding: NativeBinding, device: bigint, operation: string): FsSafeError | undefined;
@@ -0,0 +1,55 @@
1
+ import fs from "node:fs";
2
+ import { FsSafeError } from "./errors.js";
3
+ export const NATIVE_NOREPLACE_UNSUPPORTED = "FS_SAFE_INTERNAL_RENAME_NOREPLACE_UNSUPPORTED";
4
+ const unsupported = new WeakMap();
5
+ const capabilityErrors = new WeakMap();
6
+ const MAX_DEVICES = 128;
7
+ function unavailable(operation, detail, cause) {
8
+ const error = new FsSafeError("helper-unavailable", `native no-replace ${operation} is unsupported on this filesystem (${detail})`, { cause, details: { capability: "rename-noreplace" } });
9
+ capabilityErrors.set(error, detail);
10
+ return error;
11
+ }
12
+ export function isNoReplaceUnavailable(error) {
13
+ return error instanceof FsSafeError && capabilityErrors.has(error);
14
+ }
15
+ export function noReplaceUnavailable(error, operation, syscallOnly = false) {
16
+ try {
17
+ const code = error?.code;
18
+ if (code !== NATIVE_NOREPLACE_UNSUPPORTED &&
19
+ !(syscallOnly && ["EINVAL", "ENOSYS", "ENOTSUP", "EOPNOTSUPP"].includes(code ?? "")))
20
+ return;
21
+ const capability = process.platform === "darwin" ? "renameatx_np RENAME_EXCL"
22
+ : process.platform === "win32" ? "native no-replace rename" : "renameat2 RENAME_NOREPLACE";
23
+ const detail = code === NATIVE_NOREPLACE_UNSUPPORTED
24
+ ? error.message : `${capability}: ${code}`;
25
+ return unavailable(operation, detail, error);
26
+ }
27
+ catch {
28
+ // Diagnostic getters do not establish non-publication.
29
+ return;
30
+ }
31
+ }
32
+ export function rememberNoReplaceUnavailable(binding, parentFd, error) {
33
+ const detail = capabilityErrors.get(error);
34
+ if (!detail)
35
+ return;
36
+ const device = fs.fstatSync(parentFd, { bigint: true }).dev;
37
+ let devices = unsupported.get(binding);
38
+ if (!devices)
39
+ unsupported.set(binding, devices = new Map());
40
+ if (!devices.has(device) && devices.size >= MAX_DEVICES)
41
+ devices.delete(devices.keys().next().value);
42
+ devices.set(device, detail);
43
+ }
44
+ export function cachedNoReplaceUnavailable(binding, parentFd, operation = "publication") {
45
+ if (!unsupported.has(binding))
46
+ return;
47
+ return cachedNoReplaceDevice(binding, fs.fstatSync(parentFd, { bigint: true }).dev, operation);
48
+ }
49
+ export function cachedNoReplaceDevice(binding, device, operation) {
50
+ const devices = unsupported.get(binding);
51
+ if (!devices)
52
+ return;
53
+ const detail = devices.get(device);
54
+ return detail ? unavailable(operation, detail) : undefined;
55
+ }
@@ -1,4 +1,4 @@
1
1
  import type { FileIdentityStat } from "./file-identity.js";
2
2
  import type { NativeBinding } from "./native.js";
3
3
  import type { PinnedWriteParams } from "./pinned-write-types.js";
4
- export declare function runPinnedWriteNative(binding: NativeBinding, params: PinnedWriteParams): Promise<FileIdentityStat>;
4
+ export declare function runPinnedWriteNative(binding: NativeBinding, params: PinnedWriteParams, fallback: (input: PinnedWriteParams["input"]) => Promise<FileIdentityStat>): Promise<FileIdentityStat>;
@@ -57,7 +57,9 @@ import { capturePolicyAwareNativeParent } from "./native-policy-parent.js";
57
57
  import { openNativeParentAdmission, openNativeRootAdmission } from "./native-parent-admission.js";
58
58
  import { assertNativeStaging, writeNativeStage } from "./native-staged-file.js";
59
59
  import { captureNativeFdClose } from "./native-binding.js";
60
- export async function runPinnedWriteNative(binding, params) {
60
+ import { cachedNoReplaceUnavailable } from "./native-noreplace.js";
61
+ import { isFsSafeNativeRequired } from "./native-config.js";
62
+ export async function runPinnedWriteNative(binding, params, fallback) {
61
63
  const env_1 = { stack: [], error: void 0, hasError: false };
62
64
  try {
63
65
  const closeFd = captureNativeFdClose(binding);
@@ -126,9 +128,17 @@ export async function runPinnedWriteNative(binding, params) {
126
128
  windowsOwnsDirectories = true;
127
129
  return await runPinnedWriteWindows(binding, params, root, parentFd, verificationGuard);
128
130
  }
131
+ if (params.overwrite === false && !(params.input.kind === "buffer" && params.input.stageBeforePublish === false)) {
132
+ const unavailable = cachedNoReplaceUnavailable(binding, parentFd);
133
+ if (unavailable) {
134
+ if (isFsSafeNativeRequired() || (params.input.kind === "file" && params.input.clone === "always"))
135
+ throw unavailable;
136
+ return await fallback(params.input);
137
+ }
138
+ }
129
139
  const ownedParent = parentFd;
130
140
  parentFd = undefined;
131
- return await writeNativeStage(binding, ownedParent, closeFd, directory, params, verificationGuard);
141
+ return await writeNativeStage(binding, ownedParent, closeFd, directory, params, verificationGuard, fallback);
132
142
  }
133
143
  catch (error) {
134
144
  admissionFailure = { error };
@@ -1,7 +1,9 @@
1
+ import { NATIVE_NOREPLACE_UNSUPPORTED } from "./native-noreplace.js";
1
2
  export const NATIVE_RENAME_SOURCE_IDENTITY_MISMATCH = "FS_SAFE_INTERNAL_RENAME_SOURCE_IDENTITY_MISMATCH";
2
3
  export function classifyNativeRenameFailure(error) {
3
4
  try {
4
- if (error?.code === NATIVE_RENAME_SOURCE_IDENTITY_MISMATCH) {
5
+ const code = error?.code;
6
+ if (code === NATIVE_RENAME_SOURCE_IDENTITY_MISMATCH || code === NATIVE_NOREPLACE_UNSUPPORTED) {
5
7
  return "uncommitted";
6
8
  }
7
9
  }
@@ -25,7 +25,7 @@ declare class NativeStagedFile implements StagedFile {
25
25
  #private;
26
26
  constructor(owner: NativeStageOwner);
27
27
  static create(owner: NativeStageOwner, input: PinnedWriteInput, maxBytes?: number): Promise<NativeStagedFile>;
28
- static write(binding: NativeStagingBinding, parentFd: number, closeParentFd: (fd: number) => void, directory: StagedFileReceipt["directory"], params: PinnedWriteParams, parentGuard: AnyAsyncDirectoryGuard): Promise<FileIdentityStat>;
28
+ static write(binding: NativeStagingBinding, parentFd: number, closeParentFd: (fd: number) => void, directory: StagedFileReceipt["directory"], params: PinnedWriteParams, parentGuard: AnyAsyncDirectoryGuard, fallback: (input: PinnedWriteInput) => Promise<FileIdentityStat>): Promise<FileIdentityStat>;
29
29
  get receipt(): StagedFileReceipt;
30
30
  assertCurrent(): Promise<void>;
31
31
  publish(basename: string, options: {
@@ -59,12 +59,14 @@ import path from "node:path";
59
59
  import { FsSafeError } from "./errors.js";
60
60
  import { assertSynchronousCallbackResult, isMutationAuthorityError } from "./mutation-authority.js";
61
61
  import { captureNativeFdClose } from "./native-binding.js";
62
- import { writePinnedInput } from "./pinned-write-input.js";
62
+ import { replayPinnedInput, writePinnedInput } from "./pinned-write-input.js";
63
63
  import { assertNativeCopyCompleted, createNativeCopyFile } from "./copy-file-input.js";
64
64
  import { assertStagedDirectoryCurrent, openStagedDirectory } from "./staged-directory.js";
65
65
  import { assertFinalSymlinkRejected } from "./root-symlink-policy.js";
66
66
  import { classifyNativeRenameFailure } from "./native-rename-outcome.js";
67
67
  import { createStagedFileReceipt, stagedFailure } from "./staged-file-settlement.js";
68
+ import { noReplaceUnavailable, rememberNoReplaceUnavailable } from "./native-noreplace.js";
69
+ import { isFsSafeNativeRequired } from "./native-config.js";
68
70
  export function assertNativeStaging(binding) {
69
71
  if ([
70
72
  binding.closeOwnedFd,
@@ -93,6 +95,9 @@ class NativeStagedFile {
93
95
  #state = { status: "open", publication: NOT_PUBLISHED };
94
96
  #receipt;
95
97
  #rejectFinalSymlink = false;
98
+ #unsupportedNoReplace;
99
+ #fallbackCleanup;
100
+ #fallbackCleanupError;
96
101
  constructor(owner) {
97
102
  const name = owner.name ?? `.fs-safe-${randomUUID()}.tmp`;
98
103
  assertBasename(name, owner.portableNames !== false);
@@ -121,7 +126,7 @@ class NativeStagedFile {
121
126
  await staged.#prepare(input, maxBytes);
122
127
  return staged;
123
128
  }
124
- static async write(binding, parentFd, closeParentFd, directory, params, parentGuard) {
129
+ static async write(binding, parentFd, closeParentFd, directory, params, parentGuard, fallback) {
125
130
  const env_1 = { stack: [], error: void 0, hasError: false };
126
131
  try {
127
132
  const exclusive = params.overwrite === false && params.input.kind === "buffer" && params.input.stageBeforePublish === false;
@@ -143,9 +148,33 @@ class NativeStagedFile {
143
148
  if (staged.#verifyMode)
144
149
  staged.#assertCurrent();
145
150
  }
146
- const published = exclusive
147
- ? staged.#completePublication(params.basename, false, params.onPublished)
148
- : await staged.publish(params.basename, { overwrite: params.overwrite !== false }, params.onPublished);
151
+ let published;
152
+ try {
153
+ published = exclusive
154
+ ? staged.#completePublication(params.basename, false, params.onPublished)
155
+ : await staged.publish(params.basename, { overwrite: params.overwrite !== false }, params.onPublished);
156
+ }
157
+ catch (error) {
158
+ if (!staged.#unsupportedNoReplace || isFsSafeNativeRequired() ||
159
+ (params.input.kind === "file" && params.input.clone === "always"))
160
+ throw error;
161
+ // Remove only our identity-known stage before re-admitting the JS write.
162
+ // Keep its descriptor alive to replay a one-shot stream in bounded chunks.
163
+ staged.#fallbackCleanup = "failed";
164
+ let removed;
165
+ try {
166
+ removed = binding.removeStagedFile(parentFd, staged.#owner.name, staged.#file());
167
+ staged.#fallbackCleanup = removed;
168
+ }
169
+ catch (cleanupError) {
170
+ staged.#fallbackCleanupError = { error: cleanupError };
171
+ throw error;
172
+ }
173
+ if (removed !== "removed" && removed !== "name-absent")
174
+ throw error;
175
+ rememberNoReplaceUnavailable(binding, parentFd, staged.#unsupportedNoReplace);
176
+ return await fallback(replayPinnedInput(staged.#file(), params.input));
177
+ }
149
178
  const identity = published.staged.identity;
150
179
  try {
151
180
  await params.verifyPublished?.(staged.#file(), identity, parentGuard);
@@ -321,6 +350,15 @@ class NativeStagedFile {
321
350
  catch (error) {
322
351
  // Record before inspecting metadata, whose getters can reenter cleanup.
323
352
  state.publication = Object.freeze({ status: "indeterminate", basename, overwrite });
353
+ // Linux VFS and rename(2): unsupported flags return EINVAL; ENOSYS
354
+ // means no syscall. A regular file renamed to a distinct sibling cannot
355
+ // hit the directory-into-itself EINVAL case. Nothing was published.
356
+ const unavailable = !overwrite ? noReplaceUnavailable(error, "publication", true) : undefined;
357
+ if (unavailable && this.#state === state && state.status === "open") {
358
+ state.publication = NOT_PUBLISHED;
359
+ this.#unsupportedNoReplace = unavailable;
360
+ throw unavailable;
361
+ }
324
362
  // Only explicit pre-dispatch provenance can rule out a committed rename.
325
363
  if (classifyNativeRenameFailure(error) === "uncommitted") {
326
364
  state.publication = NOT_PUBLISHED;
@@ -377,6 +415,11 @@ class NativeStagedFile {
377
415
  if (state.publication.status === "indeterminate") {
378
416
  outcome = "preserved";
379
417
  }
418
+ else if (this.#fallbackCleanup) {
419
+ outcome = this.#fallbackCleanup;
420
+ if (this.#fallbackCleanupError)
421
+ errors.push(this.#fallbackCleanupError.error);
422
+ }
380
423
  else if (state.publication.status === "not-published" && state.fileFd !== undefined) {
381
424
  try {
382
425
  outcome = this.#owner.binding.removeStagedFile(this.#owner.parentFd, this.#owner.name, state.fileFd);
@@ -1,3 +1,5 @@
1
1
  import type { FileHandle } from "node:fs/promises";
2
2
  import type { PinnedWriteInput } from "./pinned-write-types.js";
3
+ /** Replay a consumed stream from an owned, readable stage descriptor. */
4
+ export declare function replayPinnedInput(fd: number, input: PinnedWriteInput): PinnedWriteInput;
3
5
  export declare function writePinnedInput(target: FileHandle | number, input: PinnedWriteInput, maxBytes?: number, assertBeforeMutation?: () => void): Promise<void>;
@@ -1,8 +1,25 @@
1
+ import { readSync } from "node:fs";
1
2
  import { types } from "node:util";
2
3
  import { snapshotByteView } from "./byte-view.js";
3
4
  import { writeCopyFileToFd } from "./copy-file-input.js";
4
5
  import { FsSafeError } from "./errors.js";
5
6
  import { writeAllToFile } from "./write-file-handle.js";
7
+ /** Replay a consumed stream from an owned, readable stage descriptor. */
8
+ export function replayPinnedInput(fd, input) {
9
+ if (input.kind !== "stream")
10
+ return input;
11
+ return { kind: "stream", stageBeforePublish: true, stream: (async function* () {
12
+ let position = 0;
13
+ const buffer = Buffer.alloc(64 * 1024);
14
+ for (;;) {
15
+ const bytes = readSync(fd, buffer, 0, buffer.length, position);
16
+ if (!bytes)
17
+ return;
18
+ position += bytes;
19
+ yield buffer.subarray(0, bytes);
20
+ }
21
+ })() };
22
+ }
6
23
  const isUint8Array = types.isUint8Array;
7
24
  function streamChunkBytes(chunk) {
8
25
  if (Buffer.isBuffer(chunk))
@@ -90,7 +90,7 @@ export async function runPinnedWriteHelper(params) {
90
90
  }
91
91
  const native = getNativeBinding();
92
92
  if (native) {
93
- return await runPinnedWriteNative(native, normalizedParams);
93
+ return await runPinnedWriteNative(native, normalizedParams, input => runPinnedWriteFallback({ ...normalizedParams, input }));
94
94
  }
95
95
  return await runPinnedWriteFallback(normalizedParams);
96
96
  }