@openclaw/matrix 2026.11.5 → 2026.11.7

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 (159) hide show
  1. package/dist/.setup/{approval-handler.runtime-Ca12_7Qa.mjs → approval-handler.runtime-LBvkygUK.mjs} +3 -3
  2. package/dist/.setup/{channel-FfaEmyIU.mjs → channel-CHryi9FS.mjs} +8 -8
  3. package/dist/.setup/{channel.runtime-CkkHG8zy.mjs → channel.runtime-DluXWaGb.mjs} +5 -5
  4. package/dist/.setup/{channel.setup-B00zzOOL.mjs → channel.setup-BDbs0FLq.mjs} +2 -2
  5. package/dist/.setup/{cli-dz9hd2dR.mjs → cli-C7Jmy2x4.mjs} +15 -40
  6. package/dist/.setup/cli-doctor-BVstkgJE.mjs +36 -0
  7. package/dist/.setup/{cli-metadata-CwUhA9CY.mjs → cli-metadata-B0GHIn05.mjs} +1 -1
  8. package/dist/.setup/{client-D1bsidwU.mjs → client-Bk_tBqNp.mjs} +2 -2
  9. package/dist/.setup/{client-bootstrap-CTT0VF6v.mjs → client-bootstrap-Bhhri97J.mjs} +1 -1
  10. package/dist/.setup/{client-CFPYqaYK.mjs → client-mFRVrUt3.mjs} +2 -2
  11. package/dist/.setup/{create-client-Ccf5wyS3.mjs → create-client-CzUtZgYF.mjs} +2 -2
  12. package/dist/.setup/{crypto-runtime-DZNytKX5.mjs → crypto-runtime-BI9Pc344.mjs} +1 -1
  13. package/dist/.setup/{crypto-unsafe-state-doctor-Dh1dtuSz.mjs → crypto-unsafe-state-doctor-CmF1X1dZ.mjs} +3 -4
  14. package/dist/.setup/{directory-live-ufzRheWx.mjs → directory-live-DmkH9hcp.mjs} +1 -1
  15. package/dist/.setup/{draft-stream-BPjjKSoI.mjs → draft-stream-bVedfEB3.mjs} +1 -1
  16. package/dist/.setup/{exec-approvals-BwEOyI1-.mjs → exec-approvals-BeYF9-3o.mjs} +2 -2
  17. package/dist/.setup/{messages-DpJ9naJa.mjs → messages-BqVczVlt.mjs} +2 -2
  18. package/dist/.setup/{monitor-vpzawYrB.mjs → monitor-BsSo3tCv.mjs} +31 -17
  19. package/dist/.setup/{onboarding-BK-V2ZLT.mjs → onboarding-CoMpgTiK.mjs} +3 -3
  20. package/dist/.setup/{outbound-DydCDuvM.mjs → outbound-ntm0KOfK.mjs} +2 -2
  21. package/dist/.setup/{profile-update-RLsY-gu6.mjs → profile-update-gC436enW.mjs} +9 -4
  22. package/dist/.setup/{reaction-events-lcgVF3M5.mjs → reaction-events-DTThoaJQ.mjs} +3 -3
  23. package/dist/.setup/{resolve-targets-DYW8ULZS.mjs → resolve-targets-CWW9zTYp.mjs} +1 -1
  24. package/dist/.setup/{room-info-Dhc4efkA.mjs → room-info-CM3MKoh4.mjs} +3 -3
  25. package/dist/.setup/{sdk-DPXiQ3jr.mjs → sdk-ByFgA6Il.mjs} +4 -3
  26. package/dist/.setup/{send-HHAbKARg.mjs → send-DFB7JGg2.mjs} +7 -7
  27. package/dist/.setup/{setup-bootstrap-DaT2j28V.mjs → setup-bootstrap-razh2kSp.mjs} +2 -2
  28. package/dist/.setup/{setup-core-DcqQbLZv.mjs → setup-core-BVbxltMM.mjs} +1 -1
  29. package/dist/.setup/setup-plugin-api-CQKySg7F.mjs +2 -0
  30. package/dist/.setup/{thread-bindings-891JsKXF.mjs → thread-bindings-D5VJEDZV.mjs} +1 -1
  31. package/dist/.setup/{tool-actions-8mXdq9OV.mjs → tool-actions-DexlmVDf.mjs} +10 -10
  32. package/dist/.setup/{verification-CcZIVL8P.mjs → verification-B122bsH2.mjs} +1 -1
  33. package/dist/.source-module-urls/1cd4d4ff8d44e1c8b830932b44d7cf8a7f3350e09e169910e1d7a85e131b7bea.json +1 -1
  34. package/dist/api.js +4 -4
  35. package/dist/channel-plugin-api.js +1 -1
  36. package/dist/cli-metadata.js +1 -1
  37. package/dist/contract-api.js +2 -2
  38. package/dist/doctor-contract-api.js +7 -3
  39. package/dist/index.js +1 -1
  40. package/dist/plugin-entry.handlers.runtime.js +2 -2
  41. package/dist/session-binding-contract-api.js +1 -1
  42. package/dist/setup-entry.js +1 -1
  43. package/dist/setup-plugin-api.js +1 -1
  44. package/node_modules/@openclaw/fs-safe/CHANGELOG.md +45 -1
  45. package/node_modules/@openclaw/fs-safe/README.md +4 -0
  46. package/node_modules/@openclaw/fs-safe/dist/absolute-path.js +2 -2
  47. package/node_modules/@openclaw/fs-safe/dist/advanced.d.ts +2 -2
  48. package/node_modules/@openclaw/fs-safe/dist/advanced.js +2 -2
  49. package/node_modules/@openclaw/fs-safe/dist/archive-parser.wasm +0 -0
  50. package/node_modules/@openclaw/fs-safe/dist/atomic-io.d.ts +1 -0
  51. package/node_modules/@openclaw/fs-safe/dist/atomic-io.js +6 -0
  52. package/node_modules/@openclaw/fs-safe/dist/atomic.d.ts +1 -0
  53. package/node_modules/@openclaw/fs-safe/dist/directory-mode-node.d.ts +5 -0
  54. package/node_modules/@openclaw/fs-safe/dist/directory-mode-node.js +13 -0
  55. package/node_modules/@openclaw/fs-safe/dist/directory-mode-options.d.ts +3 -0
  56. package/node_modules/@openclaw/fs-safe/dist/directory-mode-options.js +6 -0
  57. package/node_modules/@openclaw/fs-safe/dist/errors.d.ts +1 -1
  58. package/node_modules/@openclaw/fs-safe/dist/errors.js +1 -0
  59. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-admission.d.ts +0 -1
  60. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-admission.js +1 -11
  61. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-acquire.js +3 -3
  62. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-stale-admission.js +2 -3
  63. package/node_modules/@openclaw/fs-safe/dist/file-store-boundary.d.ts +3 -3
  64. package/node_modules/@openclaw/fs-safe/dist/file-store-boundary.js +30 -4
  65. package/node_modules/@openclaw/fs-safe/dist/file-store-prune.d.ts +1 -1
  66. package/node_modules/@openclaw/fs-safe/dist/file-store-prune.js +7 -1
  67. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-directory.d.ts +1 -1
  68. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-directory.js +30 -4
  69. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.d.ts +1 -1
  70. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.js +2 -0
  71. package/node_modules/@openclaw/fs-safe/dist/file-store.d.ts +2 -1
  72. package/node_modules/@openclaw/fs-safe/dist/file-store.js +5 -0
  73. package/node_modules/@openclaw/fs-safe/dist/guarded-mkdir.d.ts +2 -0
  74. package/node_modules/@openclaw/fs-safe/dist/guarded-mkdir.js +9 -0
  75. package/node_modules/@openclaw/fs-safe/dist/json-store.d.ts +2 -1
  76. package/node_modules/@openclaw/fs-safe/dist/native-binding.d.ts +4 -1
  77. package/node_modules/@openclaw/fs-safe/dist/native-pinned-write.js +1 -1
  78. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.d.ts +1 -1
  79. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.js +19 -13
  80. package/node_modules/@openclaw/fs-safe/dist/overwrite-file-handle.d.ts +2 -0
  81. package/node_modules/@openclaw/fs-safe/dist/overwrite-file-handle.js +27 -5
  82. package/node_modules/@openclaw/fs-safe/dist/path-prefix.d.ts +1 -0
  83. package/node_modules/@openclaw/fs-safe/dist/path-prefix.js +84 -7
  84. package/node_modules/@openclaw/fs-safe/dist/pinned-write-input.js +0 -11
  85. package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.d.ts +9 -0
  86. package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.js +61 -0
  87. package/node_modules/@openclaw/fs-safe/dist/private-temp-workspace.js +2 -1
  88. package/node_modules/@openclaw/fs-safe/dist/realpath.d.ts +2 -0
  89. package/node_modules/@openclaw/fs-safe/dist/realpath.js +16 -0
  90. package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.d.ts +2 -1
  91. package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.js +8 -6
  92. package/node_modules/@openclaw/fs-safe/dist/replace-file-types.d.ts +2 -1
  93. package/node_modules/@openclaw/fs-safe/dist/replace-file.js +20 -13
  94. package/node_modules/@openclaw/fs-safe/dist/root-context.js +4 -1
  95. package/node_modules/@openclaw/fs-safe/dist/root-create-native.d.ts +1 -1
  96. package/node_modules/@openclaw/fs-safe/dist/root-create-native.js +18 -9
  97. package/node_modules/@openclaw/fs-safe/dist/root-errors.d.ts +2 -0
  98. package/node_modules/@openclaw/fs-safe/dist/root-errors.js +9 -0
  99. package/node_modules/@openclaw/fs-safe/dist/root-impl.js +4 -1
  100. package/node_modules/@openclaw/fs-safe/dist/root-mkdir-native.d.ts +1 -1
  101. package/node_modules/@openclaw/fs-safe/dist/root-mkdir-native.js +35 -16
  102. package/node_modules/@openclaw/fs-safe/dist/root-path-existing.d.ts +6 -2
  103. package/node_modules/@openclaw/fs-safe/dist/root-path-existing.js +32 -20
  104. package/node_modules/@openclaw/fs-safe/dist/root-path.js +32 -26
  105. package/node_modules/@openclaw/fs-safe/dist/root-write-admission.js +4 -1
  106. package/node_modules/@openclaw/fs-safe/dist/secret-file.d.ts +2 -1
  107. package/node_modules/@openclaw/fs-safe/dist/secret-file.js +2 -0
  108. package/node_modules/@openclaw/fs-safe/dist/sibling-temp.d.ts +2 -1
  109. package/node_modules/@openclaw/fs-safe/dist/sibling-temp.js +5 -4
  110. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-policy.d.ts +3 -2
  111. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-policy.js +8 -9
  112. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-stale-admission.js +3 -1
  113. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-types.d.ts +2 -1
  114. package/node_modules/@openclaw/fs-safe/dist/text-atomic.d.ts +2 -1
  115. package/node_modules/@openclaw/fs-safe/dist/types.d.ts +1 -0
  116. package/node_modules/@openclaw/fs-safe/dist/watch-hints.js +12 -3
  117. package/node_modules/@openclaw/fs-safe/dist/windows-security-helper.exe +0 -0
  118. package/node_modules/@openclaw/fs-safe/dist/write-file-handle.d.ts +0 -1
  119. package/node_modules/@openclaw/fs-safe/dist/write-file-handle.js +10 -12
  120. package/node_modules/@openclaw/fs-safe/docs/advanced.md +3 -3
  121. package/node_modules/@openclaw/fs-safe/docs/atomic.md +16 -3
  122. package/node_modules/@openclaw/fs-safe/docs/compiler-qualification.md +1 -1
  123. package/node_modules/@openclaw/fs-safe/docs/contributing.md +27 -18
  124. package/node_modules/@openclaw/fs-safe/docs/errors.md +3 -1
  125. package/node_modules/@openclaw/fs-safe/docs/file-store.md +14 -2
  126. package/node_modules/@openclaw/fs-safe/docs/in-place-write.md +31 -0
  127. package/node_modules/@openclaw/fs-safe/docs/install.md +1 -1
  128. package/node_modules/@openclaw/fs-safe/docs/json-store.md +6 -1
  129. package/node_modules/@openclaw/fs-safe/docs/json.md +5 -1
  130. package/node_modules/@openclaw/fs-safe/docs/path-prefix.md +14 -4
  131. package/node_modules/@openclaw/fs-safe/docs/public-api.md +4 -2
  132. package/node_modules/@openclaw/fs-safe/docs/secret-file.md +5 -1
  133. package/node_modules/@openclaw/fs-safe/docs/sidecar-lock.md +4 -0
  134. package/node_modules/@openclaw/fs-safe/docs/temp.md +9 -1
  135. package/node_modules/@openclaw/fs-safe/docs/types.md +5 -1
  136. package/node_modules/@openclaw/fs-safe/package.json +12 -12
  137. package/node_modules/@openclaw/fs-safe-darwin-arm64/fs-safe-native.node +0 -0
  138. package/node_modules/@openclaw/fs-safe-darwin-arm64/package.json +1 -1
  139. package/node_modules/@openclaw/fs-safe-darwin-x64/fs-safe-native.node +0 -0
  140. package/node_modules/@openclaw/fs-safe-darwin-x64/package.json +1 -1
  141. package/node_modules/@openclaw/fs-safe-freebsd-arm64/fs-safe-native.node +0 -0
  142. package/node_modules/@openclaw/fs-safe-freebsd-arm64/package.json +1 -1
  143. package/node_modules/@openclaw/fs-safe-freebsd-x64/fs-safe-native.node +0 -0
  144. package/node_modules/@openclaw/fs-safe-freebsd-x64/package.json +1 -1
  145. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/fs-safe-native.node +0 -0
  146. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/package.json +1 -1
  147. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/fs-safe-native.node +0 -0
  148. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/package.json +1 -1
  149. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/fs-safe-native.node +0 -0
  150. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/package.json +1 -1
  151. package/node_modules/@openclaw/fs-safe-linux-x64-musl/fs-safe-native.node +0 -0
  152. package/node_modules/@openclaw/fs-safe-linux-x64-musl/package.json +1 -1
  153. package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/fs-safe-native.node +0 -0
  154. package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/package.json +1 -1
  155. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/fs-safe-native.node +0 -0
  156. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/package.json +1 -1
  157. package/openclaw.plugin.json +6 -0
  158. package/package.json +5 -5
  159. package/dist/.setup/setup-plugin-api-Cg4TelGZ.mjs +0 -2
@@ -3,9 +3,9 @@ export { createAsyncLock } from "./async-lock.js";
3
3
  export { createDirectory, createDirectorySync, createFileSync, type CreateDirectoryOptions, type CreateFileOptions, type OwnedFileDescriptorSync, } from "./create.js";
4
4
  export { copyFileHandle, copyFileDescriptorSync, type CopyFileHandleOptions } from "./file-handle-transfer.js";
5
5
  export { sameFileContentsSync, type SameFileContentsOptions } from "./file-contents.js";
6
- export { overwriteFileHandle, type OverwriteFileHandleOptions } from "./overwrite-file-handle.js";
6
+ export { overwriteFileHandle, overwriteFileHandleWithAuthority, type OverwriteFileHandleOptions } from "./overwrite-file-handle.js";
7
7
  export { probePathCaseInsensitiveSync, type ProbePathCaseOptions } from "./path-case.js";
8
- export { resolvePathPrefixSync, type ResolvedPathPrefix } from "./path-prefix.js";
8
+ export { resolvePathPrefix, resolvePathPrefixSync, type ResolvedPathPrefix } from "./path-prefix.js";
9
9
  export { probePathSuffixAliasesSync, type ProbePathSuffixAliasesOptions } from "./path-suffix-aliases.js";
10
10
  export { readDirectoryIdentity, assertDirectoryIdentitySync, type DirectoryIdentity, } from "./directory-guard.js";
11
11
  export { stageFileInDirectory, } from "./native-staged-file.js";
@@ -6,9 +6,9 @@ export { createAsyncLock } from "./async-lock.js";
6
6
  export { createDirectory, createDirectorySync, createFileSync, } from "./create.js";
7
7
  export { copyFileHandle, copyFileDescriptorSync } from "./file-handle-transfer.js";
8
8
  export { sameFileContentsSync } from "./file-contents.js";
9
- export { overwriteFileHandle } from "./overwrite-file-handle.js";
9
+ export { overwriteFileHandle, overwriteFileHandleWithAuthority } from "./overwrite-file-handle.js";
10
10
  export { probePathCaseInsensitiveSync } from "./path-case.js";
11
- export { resolvePathPrefixSync } from "./path-prefix.js";
11
+ export { resolvePathPrefix, resolvePathPrefixSync } from "./path-prefix.js";
12
12
  export { probePathSuffixAliasesSync } from "./path-suffix-aliases.js";
13
13
  export { readDirectoryIdentity, assertDirectoryIdentitySync, } from "./directory-guard.js";
14
14
  export { stageFileInDirectory, } from "./native-staged-file.js";
@@ -22,6 +22,7 @@ export declare class AtomicIo {
22
22
  lstat(pathname: string): Procedure<Stats>;
23
23
  lstatExact(pathname: string, synchronousBuiltin?: boolean): BigIntStats | Promise<BigIntStats>;
24
24
  mkdir(directory: string, mode: number): Procedure<void>;
25
+ mkdirExclusive(directory: string, mode: number): Procedure<void>;
25
26
  rename(source: string, destination: string): Procedure<void>;
26
27
  remove(pathname: string): Procedure<void>;
27
28
  unlink(pathname: string): Procedure<void>;
@@ -115,6 +115,12 @@ export class AtomicIo {
115
115
  this.syncFs.mkdirSync(pathname, { recursive: true, mode });
116
116
  }
117
117
  }
118
+ *mkdirExclusive(directory, mode) {
119
+ if (this.asyncFs)
120
+ yield* wait(this.asyncFs.mkdir(directory, { recursive: false, mode }));
121
+ else
122
+ this.syncFs.mkdirSync(directory, { recursive: false, mode });
123
+ }
118
124
  *rename(source, destination) {
119
125
  if (this.asyncFs)
120
126
  yield* wait(this.asyncFs.rename(source, destination));
@@ -1,5 +1,6 @@
1
1
  export { replaceFileAtomic, replaceFileAtomicSync, type ReplaceFileAtomicFileSystem, type ReplaceFileAtomicOptions, type ReplaceFileAtomicResult, type ReplaceFileAtomicDestinationState, type ReplaceFileAtomicSyncFileSystem, type ReplaceFileAtomicSyncOptions, } from "./replace-file.js";
2
2
  export type { RenameIdentityPolicy } from "./pinned-write-types.js";
3
+ export type { DirectoryMode } from "./directory-mode-options.js";
3
4
  export type { ReplaceFileAtomicRestoreCleanup, ReplaceFileAtomicRestoreFailureDetails, ReplaceFileCopyFallbackRestorePolicy, ReplaceFileDestinationHardlinkPolicy, } from "./replace-file-copy-fallback.js";
4
5
  export { writeTextAtomic, type WriteTextAtomicOptions } from "./text-atomic.js";
5
6
  export { replaceDirectoryAtomic, type ReplaceDirectoryAtomicOptions } from "./replace-directory.js";
@@ -10,6 +10,11 @@ export type DirectoryModeOwner = {
10
10
  close(): Promise<void>;
11
11
  };
12
12
  export declare function assertOwnedDirectory(expected: Stats | BigIntStats, actual: Stats | BigIntStats): void;
13
+ /** The caller retains a verified native mkdir receipt and owns the descriptor lifetime. */
14
+ export declare function finalizeCreatedDirectoryModeSync(child: {
15
+ fd: number;
16
+ created: boolean;
17
+ }, mode: number | undefined): void;
13
18
  /** Serializes use and close: even a queued path-based fd operation retains its descriptor. */
14
19
  export declare function ownDirectoryMode(params: {
15
20
  inspect: () => Promise<number>;
@@ -14,6 +14,19 @@ export function assertOwnedDirectory(expected, actual) {
14
14
  throw new FsSafeError("path-mismatch", "directory changed before its mode could be applied");
15
15
  }
16
16
  }
17
+ /** The caller retains a verified native mkdir receipt and owns the descriptor lifetime. */
18
+ export function finalizeCreatedDirectoryModeSync(child, mode) {
19
+ if (mode === undefined || !child.created || process.platform === "win32")
20
+ return;
21
+ const expected = inspectFileIdentitySync(() => fsSync.fstatSync(child.fd, { bigint: true }));
22
+ assertOwnedDirectory(expected, expected);
23
+ fsSync.fchmodSync(child.fd, mode & 0o7777);
24
+ const finalized = inspectFileIdentitySync(() => fsSync.fstatSync(child.fd, { bigint: true }), expected);
25
+ assertOwnedDirectory(expected, finalized);
26
+ if ((finalized.mode & 4095n) !== BigInt(mode & 0o7777)) {
27
+ throw new FsSafeError("path-mismatch", "created directory mode could not be finalized");
28
+ }
29
+ }
17
30
  /** Serializes use and close: even a queued path-based fd operation retains its descriptor. */
18
31
  export function ownDirectoryMode(params) {
19
32
  let pending = Promise.resolve();
@@ -0,0 +1,3 @@
1
+ /** Preserve verified existing directories; finalize exclusively created directories at 0700. */
2
+ export type DirectoryMode = number | "preserve";
3
+ export declare function rejectPrivateDirectoryPreservation(mode: DirectoryMode | undefined): asserts mode is number | undefined;
@@ -0,0 +1,6 @@
1
+ import { FsSafeError } from "./errors.js";
2
+ export function rejectPrivateDirectoryPreservation(mode) {
3
+ if (mode === "preserve") {
4
+ throw new FsSafeError("invalid-path", "Private directories require an explicit numeric dirMode; preservation is not supported.");
5
+ }
6
+ }
@@ -1,7 +1,7 @@
1
1
  export type FsSafeErrorCode = (typeof OPERATIONAL_CODE_VALUES)[number] | "already-exists" | "denied-path" | "device-path" | "hardlink" | "invalid-path" | "insecure-permissions" | "not-file" | "not-owned" | "outside-workspace" | "path-alias" | "path-mismatch" | "secret-exists" | "store-reentrant-update" | "symlink" | "too-large";
2
2
  export type FsSafeErrorCategory = "policy" | "operational";
3
3
  export type FsSafeErrorDetails = Readonly<Record<string, unknown>>;
4
- declare const OPERATIONAL_CODE_VALUES: readonly ["helper-failed", "helper-unavailable", "not-empty", "not-found", "not-removable", "permission-unverified", "read-failed", "timeout", "unsupported-platform"];
4
+ declare const OPERATIONAL_CODE_VALUES: readonly ["helper-failed", "helper-unavailable", "not-empty", "not-found", "not-removable", "permission-denied", "permission-unverified", "read-failed", "timeout", "unsupported-platform"];
5
5
  export declare function categorizeFsSafeError(code: FsSafeErrorCode): FsSafeErrorCategory;
6
6
  export declare class FsSafeError extends Error {
7
7
  readonly code: FsSafeErrorCode;
@@ -4,6 +4,7 @@ const OPERATIONAL_CODE_VALUES = [
4
4
  "not-empty",
5
5
  "not-found",
6
6
  "not-removable",
7
+ "permission-denied",
7
8
  "permission-unverified",
8
9
  "read-failed",
9
10
  "timeout",
@@ -12,6 +12,5 @@ export type SyncHeldLock = {
12
12
  export declare function foreignSyncHeldLock(route: "raw" | "root", target: string): object | undefined;
13
13
  export declare function getSyncHeldLocks(): Map<string, SyncHeldLock>;
14
14
  export declare function getSyncLockAdmissions(): Map<string, object>;
15
- export declare function defaultSyncShouldReclaim(snapshot: SidecarLockSnapshot, staleMs: number, nowMs: number): boolean;
16
15
  export declare function syncReclaimGuardExists(reclaimGuardPath: string): boolean;
17
16
  export declare function ensureSyncLockExitCleanupRegistered(cleanup: () => void, lockPath: string, normalizedTargetPath: string): void;
@@ -1,5 +1,5 @@
1
1
  import fs from "node:fs";
2
- import { sidecarLockPayloadCreatedAtMs, sidecarLockTimeout } from "./sidecar-lock-policy.js";
2
+ import { sidecarLockTimeout } from "./sidecar-lock-policy.js";
3
3
  import { ensureSidecarLockCleanupRegistered } from "./sidecar-lock-registration.js";
4
4
  const SYNC_HELD_LOCKS_KEY = Symbol.for("fsSafe.syncSidecarLocks");
5
5
  const SYNC_ADMISSIONS_KEY = Symbol.for("fsSafe.syncSidecarLockAdmissions");
@@ -26,16 +26,6 @@ export function getSyncLockAdmissions() {
26
26
  }
27
27
  return globalWithState[SYNC_ADMISSIONS_KEY];
28
28
  }
29
- export function defaultSyncShouldReclaim(snapshot, staleMs, nowMs) {
30
- const createdAtMs = sidecarLockPayloadCreatedAtMs(snapshot.payload);
31
- if (createdAtMs !== null)
32
- return nowMs - createdAtMs > staleMs;
33
- if (!snapshot.stat)
34
- return true;
35
- const mtimeMs = "mtimeNs" in snapshot.stat
36
- ? Number(snapshot.stat.mtimeNs) / 1e6 : snapshot.stat.mtimeMs;
37
- return nowMs - mtimeMs > staleMs;
38
- }
39
29
  export function syncReclaimGuardExists(reclaimGuardPath) {
40
30
  try {
41
31
  return fs.lstatSync(reclaimGuardPath, { throwIfNoEntry: false }) !== undefined;
@@ -2,8 +2,8 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { FsSafeError } from "./errors.js";
4
4
  import { captureRootSyncAcquireOptions } from "./file-lock-sync-root-options.js";
5
- import { defaultSyncShouldReclaim, foreignSyncHeldLock } from "./file-lock-sync-admission.js";
6
- import { isTransientLockFileDenial, sidecarLockStale } from "./sidecar-lock-policy.js";
5
+ import { foreignSyncHeldLock } from "./file-lock-sync-admission.js";
6
+ import { defaultSidecarLockShouldReclaim, isTransientLockFileDenial, sidecarLockStale } from "./sidecar-lock-policy.js";
7
7
  import { serializeSidecarLockPayload, } from "./sidecar-lock-reclaim.js";
8
8
  import { createSuppressedError } from "./suppressed-error.js";
9
9
  import { SyncLockAcquisition } from "./file-lock-sync-acquisition.js";
@@ -287,7 +287,7 @@ export function acquireFileLockSyncWithRoot(targetPath, inputOptions, lockRoot)
287
287
  }
288
288
  }
289
289
  else {
290
- reclaim = defaultSyncShouldReclaim(snapshot, options.staleMs, nowMs);
290
+ reclaim = defaultSidecarLockShouldReclaim(snapshot, options.staleMs, nowMs);
291
291
  }
292
292
  if (reclaim) {
293
293
  if (options.staleRecovery === "remove-if-unchanged" &&
@@ -1,8 +1,7 @@
1
1
  import fs from "node:fs";
2
- import { defaultSyncShouldReclaim } from "./file-lock-sync-admission.js";
3
2
  import { parseSidecarLockSnapshot, readSidecarLockRawSnapshotSync, removeSidecarLockIfUnchangedSync, } from "./sidecar-lock-reclaim.js";
4
3
  import { assertSynchronousCallbackResult } from "./mutation-authority.js";
5
- import { sidecarLockStale } from "./sidecar-lock-policy.js";
4
+ import { defaultSidecarLockShouldReclaim, sidecarLockStale } from "./sidecar-lock-policy.js";
6
5
  /** Handle one exclusive-create conflict without surrendering canonical admission. */
7
6
  export function handleSyncStaleAdmission(acquisition, params) {
8
7
  const holderChanged = {};
@@ -109,7 +108,7 @@ export function handleSyncStaleAdmission(acquisition, params) {
109
108
  nowMs,
110
109
  heldByThisProcess: false,
111
110
  }])
112
- : defaultSyncShouldReclaim(snapshot, params.staleMs, nowMs);
111
+ : defaultSidecarLockShouldReclaim(snapshot, params.staleMs, nowMs);
113
112
  assertSynchronousCallbackResult(reclaim, "shouldReclaim");
114
113
  }
115
114
  finally {
@@ -3,10 +3,10 @@ import { ensureSyncStoreDirectory, type SyncStoreDirectoryReceipt } from "./file
3
3
  import { type Root } from "./root.js";
4
4
  import { prepareSecretFileWrite } from "./secret-file.js";
5
5
  export type SyncParentGuard = SyncStoreDirectoryReceipt;
6
- export declare function ensureParentInRoot(scopedRoot: Root, relativePath: string, mode: number): Promise<void>;
6
+ export declare function ensureParentInRoot(scopedRoot: Root, relativePath: string, mode: number | "preserve"): Promise<void>;
7
7
  export declare function openWritableStoreRoot(params: {
8
8
  rootDir: string;
9
- dirMode: number;
9
+ dirMode: number | "preserve";
10
10
  maxBytes?: number;
11
11
  }): Promise<Root>;
12
12
  export declare function openPrivateStoreLockRoot(params: Parameters<typeof prepareSecretFileWrite>[0]): Promise<Root>;
@@ -21,7 +21,7 @@ export declare function writeStreamToTempSource(params: {
21
21
  export declare function ensureParentSync(params: {
22
22
  rootDir: string;
23
23
  filePath: string;
24
- mode: number;
24
+ mode: number | "preserve";
25
25
  }): SyncParentGuard;
26
26
  export declare function ensureStoreDirectorySync(params: Parameters<typeof ensureSyncStoreDirectory>[0]): SyncParentGuard;
27
27
  export declare function literalStoreRootPath(relativePath: string): string;
@@ -11,6 +11,13 @@ import { rootFromDirectoryGuard } from "./root-impl.js";
11
11
  import { prepareSecretFileWrite } from "./secret-file.js";
12
12
  import { resolveSecureTempRoot } from "./secure-temp-dir.js";
13
13
  import { recursiveMkdirPath } from "./recursive-mkdir-path.js";
14
+ import { AtomicIo, runAsync } from "./atomic-io.js";
15
+ import { prepareDirectoryMode } from "./prepare-directory-mode.js";
16
+ import { mkdirPathComponentsWithGuards } from "./guarded-mkdir.js";
17
+ import { rootHandleContext } from "./root-handle-context.js";
18
+ import { isFsSafeNativeRequired } from "./native-config.js";
19
+ import { tryMkdirNativeDirectChild } from "./root-create-native.js";
20
+ import { tryMkdirNativeParents } from "./root-mkdir-native.js";
14
21
  import { readRegularFile } from "./regular-file.js";
15
22
  import { assertExclusiveCreateLeaf } from "./exclusive-create.js";
16
23
  import { errorCauseOptions } from "./root-errors.js";
@@ -20,13 +27,32 @@ export async function ensureParentInRoot(scopedRoot, relativePath, mode) {
20
27
  if (parent === ".") {
21
28
  return;
22
29
  }
23
- await scopedRoot.mkdir(parent);
24
- await chmodDirectoryInRootBestEffort(scopedRoot, parent, mode).catch(() => undefined);
30
+ if (mode === "preserve") {
31
+ const context = rootHandleContext(scopedRoot);
32
+ const relativeParent = path.posix.dirname(relativePath);
33
+ if (isFsSafeNativeRequired() && (tryMkdirNativeDirectChild(context, relativeParent, 0o700) ||
34
+ tryMkdirNativeParents(context, relativeParent, 0o700)))
35
+ return;
36
+ await mkdirPathComponentsWithGuards({
37
+ rootReal: context.rootReal, rootIdentity: context.rootIdentity,
38
+ targetPath: path.join(context.rootReal, parent),
39
+ mode: 0o700, finalizeCreatedMode: true, rejectSymlinks: true,
40
+ });
41
+ }
42
+ else {
43
+ await scopedRoot.mkdir(parent);
44
+ await chmodDirectoryInRootBestEffort(scopedRoot, parent, mode).catch(() => undefined);
45
+ }
25
46
  }
26
47
  export async function openWritableStoreRoot(params) {
27
48
  assertNoWindowsPathAlias(params.rootDir, "filesystem", "store root uses a Windows filesystem namespace alias");
28
- await fs.mkdir(recursiveMkdirPath(params.rootDir), { recursive: true, mode: params.dirMode });
29
- await fs.chmod(params.rootDir, params.dirMode).catch(() => undefined);
49
+ if (params.dirMode === "preserve") {
50
+ await runAsync(prepareDirectoryMode(AtomicIo.async(fs), { dirPath: params.rootDir, mode: "preserve" }));
51
+ }
52
+ else {
53
+ await fs.mkdir(recursiveMkdirPath(params.rootDir), { recursive: true, mode: params.dirMode });
54
+ await fs.chmod(params.rootDir, params.dirMode).catch(() => undefined);
55
+ }
30
56
  return await root(params.rootDir, { hardlinks: "reject", maxBytes: params.maxBytes });
31
57
  }
32
58
  export async function openPrivateStoreLockRoot(params) {
@@ -6,6 +6,6 @@ export type FileStorePruneOptions = {
6
6
  };
7
7
  export declare function pruneExpiredStoreEntries(params: {
8
8
  rootDir: string;
9
- dirMode: number;
9
+ dirMode: number | "preserve";
10
10
  options: FileStorePruneOptions;
11
11
  }): Promise<void>;
@@ -6,6 +6,8 @@ import { literalStoreRootPath } from "./file-store-boundary.js";
6
6
  import { isPathInside } from "./path.js";
7
7
  import { realpathSync } from "./realpath.js";
8
8
  import { recursiveMkdirPath } from "./recursive-mkdir-path.js";
9
+ import { AtomicIo, runAsync } from "./atomic-io.js";
10
+ import { prepareDirectoryMode } from "./prepare-directory-mode.js";
9
11
  import { root } from "./root.js";
10
12
  import { nonrecursiveRemovalKind } from "./root-remove.js";
11
13
  import { getFsSafeTestHooks } from "./test-hooks.js";
@@ -17,7 +19,11 @@ export async function pruneExpiredStoreEntries(params) {
17
19
  const recursive = params.options.recursive ?? false;
18
20
  const maxDepth = params.options.maxDepth;
19
21
  const pruneEmptyDirs = (recursive || maxDepth !== undefined) && (params.options.pruneEmptyDirs ?? false);
20
- await fs.mkdir(recursiveMkdirPath(params.rootDir), { recursive: true, mode: params.dirMode });
22
+ if (params.dirMode === "preserve") {
23
+ await runAsync(prepareDirectoryMode(AtomicIo.async(fs), { dirPath: params.rootDir, mode: "preserve" }));
24
+ }
25
+ else
26
+ await fs.mkdir(recursiveMkdirPath(params.rootDir), { recursive: true, mode: params.dirMode });
21
27
  const rootReal = realpathSync.native(params.rootDir);
22
28
  const scopedRoot = await root(rootReal);
23
29
  const rootGuard = {
@@ -8,7 +8,7 @@ export type SyncStoreDirectoryReceipt = Readonly<{
8
8
  export declare function ensureSyncStoreDirectory(params: {
9
9
  rootDir: string;
10
10
  targetDir: string;
11
- mode: number;
11
+ mode: number | "preserve";
12
12
  messagePrefix: StoreMessagePrefix;
13
13
  }): SyncStoreDirectoryReceipt;
14
14
  export declare function assertSyncStoreDirectoryReceipt(receipt: SyncStoreDirectoryReceipt): void;
@@ -4,6 +4,9 @@ import { FsSafeError } from "./errors.js";
4
4
  import { isPathInside, isPathRelativeEscape } from "./path.js";
5
5
  import { realpathSync } from "./realpath.js";
6
6
  import { recursiveMkdirPath } from "./recursive-mkdir-path.js";
7
+ import { AtomicIo, runSync } from "./atomic-io.js";
8
+ import { prepareDirectoryMode } from "./prepare-directory-mode.js";
9
+ import { applyDirectoryMode } from "./replace-file-descriptor.js";
7
10
  import { inspectFileIdentitySync } from "./strict-file-identity.js";
8
11
  import { createSuppressedError } from "./suppressed-error.js";
9
12
  import { assertNoWindowsPathAlias, pathForWindowsFilesystem, resolvePathPreservingWindowsRoot, } from "./windows-path-alias.js";
@@ -212,6 +215,14 @@ class StoreDirectoryChain {
212
215
  throw changedStoreDirectory(this.messagePrefix, error);
213
216
  }
214
217
  }
218
+ preserveMode() {
219
+ const receipt = this.receipts.at(-1);
220
+ this.assertEdge();
221
+ runSync(applyDirectoryMode(AtomicIo.sync(fs), {
222
+ dirPath: pathForWindowsFilesystem(receipt.dir), mode: "preserve", expectedIdentity: receipt.stat,
223
+ }));
224
+ this.assertEdge();
225
+ }
215
226
  finalizeMode(mode) {
216
227
  const receipt = this.receipts.at(-1);
217
228
  const requestedMode = BigInt(mode & 0o7777);
@@ -274,9 +285,18 @@ export function ensureSyncStoreDirectory(params) {
274
285
  if (isPathRelativeEscape(relative)) {
275
286
  throw new FsSafeError("outside-workspace", "file path escapes store root");
276
287
  }
277
- fs.mkdirSync(recursiveMkdirPath(pathForWindowsFilesystem(rootDir)), { recursive: true, mode: params.mode });
288
+ const preserve = (directory) => runSync(prepareDirectoryMode(AtomicIo.sync(fs, fs.fchmodSync), {
289
+ dirPath: pathForWindowsFilesystem(directory), mode: "preserve",
290
+ }));
291
+ if (params.mode === "preserve")
292
+ preserve(rootDir);
293
+ else
294
+ fs.mkdirSync(recursiveMkdirPath(pathForWindowsFilesystem(rootDir)), { recursive: true, mode: params.mode });
278
295
  const chain = new StoreDirectoryChain(rootDir, params.messagePrefix);
279
- chain.finalizeMode(params.mode);
296
+ if (params.mode === "preserve")
297
+ chain.preserveMode();
298
+ else
299
+ chain.finalizeMode(params.mode);
280
300
  let current = rootDir;
281
301
  for (const segment of relative.split(path.sep).filter(Boolean)) {
282
302
  current = path.join(current, segment);
@@ -286,10 +306,16 @@ export function ensureSyncStoreDirectory(params) {
286
306
  catch (error) {
287
307
  if (error.code !== "ENOENT")
288
308
  throw error;
289
- fs.mkdirSync(pathForWindowsFilesystem(current), { mode: params.mode });
309
+ if (params.mode === "preserve")
310
+ preserve(current);
311
+ else
312
+ fs.mkdirSync(pathForWindowsFilesystem(current), { mode: params.mode });
290
313
  chain.observe(current);
291
314
  }
292
- chain.finalizeMode(params.mode);
315
+ if (params.mode === "preserve")
316
+ chain.preserveMode();
317
+ else
318
+ chain.finalizeMode(params.mode);
293
319
  }
294
320
  return chain.finish(dir);
295
321
  }
@@ -4,6 +4,6 @@ export declare function writeFileSyncAtomic(params: {
4
4
  content: string | Uint8Array;
5
5
  privateMode: boolean;
6
6
  durable: boolean;
7
- dirMode: number;
7
+ dirMode: number | "preserve";
8
8
  mode: number;
9
9
  }): string;
@@ -1,6 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import fs, {} from "node:fs";
3
3
  import path from "node:path";
4
+ import { rejectPrivateDirectoryPreservation } from "./directory-mode-options.js";
4
5
  import { syncDirectorySync } from "./directory-durability.js";
5
6
  import { createDirectoryReceiptFromIdentity } from "./directory-receipt.js";
6
7
  import { FsSafeError } from "./errors.js";
@@ -50,6 +51,7 @@ export function writeFileSyncAtomic(params) {
50
51
  }
51
52
  let parentGuard;
52
53
  if (params.privateMode) {
54
+ rejectPrivateDirectoryPreservation(params.dirMode);
53
55
  parentGuard = ensureStoreDirectorySync({
54
56
  rootDir: params.rootDir,
55
57
  targetDir: path.dirname(filePath),
@@ -1,3 +1,4 @@
1
+ import { type DirectoryMode } from "./directory-mode-options.js";
1
2
  import type { Readable } from "node:stream";
2
3
  import { type FileStorePruneOptions } from "./file-store-prune.js";
3
4
  export type { FileStorePruneOptions } from "./file-store-prune.js";
@@ -8,7 +9,7 @@ export type FileStoreOptions = {
8
9
  private?: boolean;
9
10
  /** Default for writes, streams, and copies; true syncs file and parent where supported. */
10
11
  durable?: boolean;
11
- dirMode?: number;
12
+ dirMode?: DirectoryMode;
12
13
  mode?: number;
13
14
  maxBytes?: number;
14
15
  };
@@ -1,4 +1,5 @@
1
1
  import syncFs from "node:fs";
2
+ import { rejectPrivateDirectoryPreservation } from "./directory-mode-options.js";
2
3
  import { normalizeMaxBytes } from "./byte-budget.js";
3
4
  import { readFileDescriptorBoundedSync } from "./bounded-read.js";
4
5
  import { FsSafeError } from "./errors.js";
@@ -84,6 +85,8 @@ export function fileStore(options) {
84
85
  assertNoWindowsPathAlias(rootDir, "filesystem", "store root uses a Windows filesystem namespace alias");
85
86
  const privateMode = options.private ?? false;
86
87
  const dirMode = options.dirMode ?? 0o700;
88
+ if (privateMode)
89
+ rejectPrivateDirectoryPreservation(dirMode);
87
90
  const mode = options.mode ?? 0o600;
88
91
  const durable = options.durable ?? true;
89
92
  const maxBytes = normalizeMaxBytes(options.maxBytes);
@@ -289,6 +292,8 @@ export function fileStoreSync(options) {
289
292
  assertNoWindowsPathAlias(rootDir, "filesystem", "store root uses a Windows filesystem namespace alias");
290
293
  const privateMode = options.private ?? false;
291
294
  const dirMode = options.dirMode ?? 0o700;
295
+ if (privateMode)
296
+ rejectPrivateDirectoryPreservation(dirMode);
292
297
  const mode = options.mode ?? 0o600;
293
298
  const durable = options.durable ?? true;
294
299
  const maxBytes = normalizeMaxBytes(options.maxBytes);
@@ -16,6 +16,8 @@ export declare function mkdirPathComponentsWithGuards(params: {
16
16
  createMissing?: boolean;
17
17
  synchronousMkdir?: boolean;
18
18
  mode?: number;
19
+ /** Correct only directories this walk exclusively creates; never change existing entries. */
20
+ finalizeCreatedMode?: boolean;
19
21
  private?: boolean;
20
22
  rejectSymlinks?: boolean;
21
23
  revalidateParentAfterBeforeComponent?: boolean;
@@ -2,6 +2,8 @@ import fsSync, {} from "node:fs";
2
2
  import fs from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { createDirectoryWithAdmission } from "./create.js";
5
+ import { AtomicIo, runAsync } from "./atomic-io.js";
6
+ import { applyDirectoryMode } from "./replace-file-descriptor.js";
5
7
  import { assertDirectoryGuard, captureDirectoryGuard, inspectDirectoryIdentitySync, } from "./directory-guard.js";
6
8
  import { FsSafeError } from "./errors.js";
7
9
  import { hasNodeErrorCode, isNotFoundPathError, isPathRelativeEscape } from "./path.js";
@@ -270,6 +272,13 @@ export async function mkdirPathComponentsWithGuards(params) {
270
272
  if (!createdAuthorization || !createdEvidence) {
271
273
  assertDirectoryGuard(parentGuard, "native");
272
274
  }
275
+ if (created && params.finalizeCreatedMode && params.mode !== undefined) {
276
+ await runAsync(applyDirectoryMode(AtomicIo.async(fs), {
277
+ dirPath: currentGuard.dir, mode: params.mode, expectedIdentity: currentGuard.stat,
278
+ }));
279
+ assertDirectoryGuard(parentGuard, "native");
280
+ assertDirectoryGuard(currentGuard, "native");
281
+ }
273
282
  current = admittedNextPath;
274
283
  }
275
284
  return current;
@@ -1,7 +1,8 @@
1
+ import type { DirectoryMode } from "./directory-mode-options.js";
1
2
  import { type JsonFileStoreOptions, type JsonStore, type JsonStoreLockOptions } from "./json-document-store.js";
2
3
  export type JsonStoreOptions<T> = JsonFileStoreOptions & {
3
4
  filePath: string;
4
- dirMode?: number;
5
+ dirMode?: DirectoryMode;
5
6
  mode?: number;
6
7
  };
7
8
  export type { JsonFileStoreOptions, JsonStore, JsonStoreLockOptions };
@@ -247,7 +247,10 @@ export interface NativeBinding {
247
247
  stagedSymlinkMatches?(parentFd: number, basename: string, linkFd: number): boolean;
248
248
  publishStagedSymlink?(parentFd: number, basename: string, linkFd: number, destination: string): void;
249
249
  removeStagedSymlink?(parentFd: number, basename: string, linkFd: number): "removed" | "name-absent" | "preserved";
250
- createStagedFile?(parentFd: number, basename: string): number;
250
+ createStagedFile?(parentFd: number, basename: string): Promise<number>;
251
+ /** POSIX staging: caller retains both parents until the worker settles. */
252
+ renameReplaceAsync?(sourceFd: number, source: string, targetFd: number, target: string): Promise<void>;
253
+ renameNoReplaceAsync?(sourceFd: number, source: string, targetFd: number, target: string): Promise<void>;
251
254
  stagedFileMetadata?(parentFd: number, basename: string, fd: number): {
252
255
  mode: bigint;
253
256
  nlink: bigint;
@@ -62,7 +62,7 @@ import { cachedNoReplaceUnavailable } from "./native-noreplace.js";
62
62
  import { isFsSafeNativeRequired } from "./native-config.js";
63
63
  import { copyLinkAtParent } from "./copy-link.js";
64
64
  const writeCapabilities = ["openBeneath", "mkdirBeneath", "renameNoReplace", "renameReplace"];
65
- const posixWriteCapabilities = [...writeCapabilities, "createStagedFile", "stagedFileMatches", "removeStagedFile"];
65
+ const posixWriteCapabilities = [...writeCapabilities, "createStagedFile", "stagedFileMatches", "removeStagedFile", "renameReplaceAsync", "renameNoReplaceAsync"];
66
66
  function pinnedWriteCapabilities(params) {
67
67
  const windows = process.platform === "win32";
68
68
  const capabilities = windows ? [...writeCapabilities, "fstatIdentity"] : posixWriteCapabilities;
@@ -5,7 +5,7 @@ import type { FileIdentityStat } from "./file-identity.js";
5
5
  import { type NativeBinding } from "./native-binding.js";
6
6
  import type { PinnedWriteInput, PinnedWriteParams } from "./pinned-write-types.js";
7
7
  import type { PublishedFileReceipt, StagedFile, StagedFileCleanupReceipt, StagedFileReceipt } from "./staged-file-types.js";
8
- export type NativeStagingBinding = NativeBinding & Required<Pick<NativeBinding, "createStagedFile" | "stagedFileMatches" | "removeStagedFile" | "renameReplace" | "renameNoReplace">>;
8
+ export type NativeStagingBinding = NativeBinding & Required<Pick<NativeBinding, "createStagedFile" | "stagedFileMatches" | "removeStagedFile" | "renameReplaceAsync" | "renameNoReplaceAsync">>;
9
9
  export declare function assertNativeStaging(binding: NativeBinding): asserts binding is NativeStagingBinding;
10
10
  type StagedPermissionPolicy = "private-creation" | "mode-only";
11
11
  type NativeStageOwner = Readonly<{
@@ -76,8 +76,8 @@ export function assertNativeStaging(binding) {
76
76
  binding.createStagedFile,
77
77
  binding.stagedFileMatches,
78
78
  binding.removeStagedFile,
79
- binding.renameReplace,
80
- binding.renameNoReplace,
79
+ binding.renameReplaceAsync,
80
+ binding.renameNoReplaceAsync,
81
81
  ].some((fn) => typeof fn !== "function")) {
82
82
  throw new FsSafeError("helper-unavailable", "native retained-directory staging is unavailable");
83
83
  }
@@ -97,7 +97,7 @@ class NativeStagedFile {
97
97
  #verifyMode;
98
98
  #state = { status: "open", publication: NOT_PUBLISHED };
99
99
  #receipt;
100
- #publicationFlush;
100
+ #publicationTask;
101
101
  #rejectFinalSymlink = false;
102
102
  #unsupportedNoReplace;
103
103
  #fallbackCleanup;
@@ -288,7 +288,7 @@ class NativeStagedFile {
288
288
  if (this.#private)
289
289
  assertDarwinCreationAcl(this.#owner.parentFd, "file");
290
290
  try {
291
- state.fileFd = this.#owner.binding.createStagedFile(this.#owner.parentFd, this.#owner.name);
291
+ state.fileFd = await this.#owner.binding.createStagedFile(this.#owner.parentFd, this.#owner.name);
292
292
  }
293
293
  catch (error) {
294
294
  annotateReadOnlyOpenError(error, path.join(this.#owner.directory.realPath, this.#owner.name));
@@ -337,7 +337,14 @@ class NativeStagedFile {
337
337
  async assertCurrent() {
338
338
  this.#assertCurrent();
339
339
  }
340
- async publish(basename, options, onPublished) {
340
+ publish(basename, options, onPublished) {
341
+ if (this.#publicationTask)
342
+ return Promise.reject(stagedFailure("file", new FsSafeError("helper-failed", "staged file publication is pending"), { phase: "publish", publication: this.#state.status === "closed" ? this.#state.receipt.publication : this.#state.publication }));
343
+ // Claim the operation before yielding so cleanup cannot recycle a worker's fds.
344
+ return this.#publicationTask = Promise.resolve().then(() => this.#publish(basename, options, onPublished))
345
+ .finally(() => { this.#publicationTask = undefined; });
346
+ }
347
+ async #publish(basename, options, onPublished) {
341
348
  const overwrite = options?.overwrite;
342
349
  try {
343
350
  const state = this.#open();
@@ -354,10 +361,10 @@ class NativeStagedFile {
354
361
  }
355
362
  try {
356
363
  if (overwrite) {
357
- this.#owner.binding.renameReplace(this.#owner.parentFd, this.#owner.name, this.#owner.parentFd, basename);
364
+ await this.#owner.binding.renameReplaceAsync(this.#owner.parentFd, this.#owner.name, this.#owner.parentFd, basename);
358
365
  }
359
366
  else {
360
- this.#owner.binding.renameNoReplace(this.#owner.parentFd, this.#owner.name, this.#owner.parentFd, basename);
367
+ await this.#owner.binding.renameNoReplaceAsync(this.#owner.parentFd, this.#owner.name, this.#owner.parentFd, basename);
361
368
  }
362
369
  }
363
370
  catch (error) {
@@ -412,14 +419,13 @@ class NativeStagedFile {
412
419
  };
413
420
  if (this.#owner.sync === false)
414
421
  return verify();
415
- this.#publicationFlush = Promise.resolve().then(async () => {
422
+ return Promise.resolve().then(async () => {
416
423
  if ((this.#owner.mode & 0o600) !== 0o600 || (stagedMode & ~this.#owner.mode) !== 0) {
417
424
  await (this.#owner.strictFileSync ? syncFile(fd) : syncFileBestEffort(fd));
418
425
  }
419
426
  await syncFileBestEffort(this.#owner.parentFd);
420
427
  return verify();
421
- }).finally(() => { this.#publicationFlush = undefined; });
422
- return this.#publicationFlush;
428
+ });
423
429
  }
424
430
  #finalize() {
425
431
  if (this.#state.status === "closed") {
@@ -474,9 +480,9 @@ class NativeStagedFile {
474
480
  return this.#state;
475
481
  }
476
482
  async cleanup() {
477
- // Native descriptors cannot close or be recycled while libuv is synchronizing them.
478
- if (this.#publicationFlush)
479
- await this.#publicationFlush.catch(() => { });
483
+ // Native descriptors stay owned through the rename and all durability fences.
484
+ if (this.#publicationTask)
485
+ await this.#publicationTask.catch(() => { });
480
486
  const closed = this.#finalize();
481
487
  if (closed.error) {
482
488
  throw closed.error;
@@ -5,3 +5,5 @@ export type OverwriteFileHandleOptions = {
5
5
  };
6
6
  /** Overwrite a borrowed, non-append read/write handle with best-effort failure rollback. */
7
7
  export declare function overwriteFileHandle(handle: FileHandle, data: Uint8Array, options?: OverwriteFileHandleOptions): Promise<void>;
8
+ /** Await authority preparation, then synchronously admit the whole borrowed-handle write. */
9
+ export declare function overwriteFileHandleWithAuthority(handle: FileHandle, data: Uint8Array, prepareAuthority: () => Promise<() => void>): Promise<void>;