@openclaw/matrix 2026.11.10-beta.3 → 2026.12.1-beta.1

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 (149) hide show
  1. package/node_modules/@openclaw/fs-safe/CHANGELOG.md +57 -0
  2. package/node_modules/@openclaw/fs-safe/README.md +6 -0
  3. package/node_modules/@openclaw/fs-safe/dist/archive-parser.wasm +0 -0
  4. package/node_modules/@openclaw/fs-safe/dist/atomic-io.d.ts +1 -0
  5. package/node_modules/@openclaw/fs-safe/dist/atomic-io.js +10 -0
  6. package/node_modules/@openclaw/fs-safe/dist/bounded-read.d.ts +1 -1
  7. package/node_modules/@openclaw/fs-safe/dist/bounded-read.js +15 -4
  8. package/node_modules/@openclaw/fs-safe/dist/directory-guard.js +11 -2
  9. package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.d.ts +6 -0
  10. package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.js +38 -0
  11. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.d.ts +1 -1
  12. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.js +2 -2
  13. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-acquire.js +13 -8
  14. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-io.js +13 -0
  15. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.d.ts +2 -2
  16. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.js +34 -24
  17. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-stale-admission.js +7 -2
  18. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync.js +20 -20
  19. package/node_modules/@openclaw/fs-safe/dist/file-observation.d.ts +13 -2
  20. package/node_modules/@openclaw/fs-safe/dist/file-observation.js +8 -4
  21. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.d.ts +1 -0
  22. package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.js +11 -1
  23. package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.d.ts +2 -0
  24. package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.js +17 -0
  25. package/node_modules/@openclaw/fs-safe/dist/file-store.d.ts +2 -1
  26. package/node_modules/@openclaw/fs-safe/dist/file-store.js +12 -16
  27. package/node_modules/@openclaw/fs-safe/dist/guest-native-python.js +1 -1
  28. package/node_modules/@openclaw/fs-safe/dist/guest.js +3 -4
  29. package/node_modules/@openclaw/fs-safe/dist/json-document-store.d.ts +2 -0
  30. package/node_modules/@openclaw/fs-safe/dist/json-document-store.js +1 -0
  31. package/node_modules/@openclaw/fs-safe/dist/json-store.js +1 -0
  32. package/node_modules/@openclaw/fs-safe/dist/json.d.ts +5 -2
  33. package/node_modules/@openclaw/fs-safe/dist/json.js +6 -2
  34. package/node_modules/@openclaw/fs-safe/dist/local-file-descriptor.js +16 -10
  35. package/node_modules/@openclaw/fs-safe/dist/native-binding.d.ts +6 -0
  36. package/node_modules/@openclaw/fs-safe/dist/native-pinned-write.js +4 -0
  37. package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.d.ts +20 -0
  38. package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.js +18 -0
  39. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.d.ts +3 -18
  40. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.js +48 -24
  41. package/node_modules/@openclaw/fs-safe/dist/opened-file-failure.js +19 -7
  42. package/node_modules/@openclaw/fs-safe/dist/output.d.ts +1 -0
  43. package/node_modules/@openclaw/fs-safe/dist/output.js +3 -0
  44. package/node_modules/@openclaw/fs-safe/dist/path-policy.js +4 -1
  45. package/node_modules/@openclaw/fs-safe/dist/permissions-windows.js +35 -10
  46. package/node_modules/@openclaw/fs-safe/dist/pinned-open.d.ts +9 -2
  47. package/node_modules/@openclaw/fs-safe/dist/pinned-open.js +42 -23
  48. package/node_modules/@openclaw/fs-safe/dist/pinned-write-staged.js +85 -34
  49. package/node_modules/@openclaw/fs-safe/dist/pinned-write-types.d.ts +3 -0
  50. package/node_modules/@openclaw/fs-safe/dist/pinned-write.js +4 -0
  51. package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.d.ts +1 -0
  52. package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.js +1 -1
  53. package/node_modules/@openclaw/fs-safe/dist/private-temp-workspace.js +2 -2
  54. package/node_modules/@openclaw/fs-safe/dist/publication-recovery.d.ts +24 -0
  55. package/node_modules/@openclaw/fs-safe/dist/publication-recovery.js +151 -0
  56. package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.d.ts +1 -0
  57. package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.js +2 -1
  58. package/node_modules/@openclaw/fs-safe/dist/realpath.d.ts +1 -1
  59. package/node_modules/@openclaw/fs-safe/dist/realpath.js +22 -11
  60. package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.d.ts +1 -0
  61. package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.js +5 -0
  62. package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.d.ts +7 -0
  63. package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.js +75 -2
  64. package/node_modules/@openclaw/fs-safe/dist/replace-file-types.d.ts +2 -0
  65. package/node_modules/@openclaw/fs-safe/dist/replace-file.js +14 -10
  66. package/node_modules/@openclaw/fs-safe/dist/root-context.js +28 -12
  67. package/node_modules/@openclaw/fs-safe/dist/root-copy.js +1 -0
  68. package/node_modules/@openclaw/fs-safe/dist/root-create-input.js +2 -2
  69. package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.d.ts +4 -0
  70. package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.js +53 -36
  71. package/node_modules/@openclaw/fs-safe/dist/root-file-open-batch-sync.js +3 -1
  72. package/node_modules/@openclaw/fs-safe/dist/root-file.d.ts +2 -1
  73. package/node_modules/@openclaw/fs-safe/dist/root-file.js +24 -13
  74. package/node_modules/@openclaw/fs-safe/dist/root-impl.js +17 -1
  75. package/node_modules/@openclaw/fs-safe/dist/root-options.d.ts +4 -2
  76. package/node_modules/@openclaw/fs-safe/dist/root-path.d.ts +1 -1
  77. package/node_modules/@openclaw/fs-safe/dist/root-path.js +2 -1
  78. package/node_modules/@openclaw/fs-safe/dist/root-read-admission.js +31 -9
  79. package/node_modules/@openclaw/fs-safe/dist/root-write-admission.js +6 -2
  80. package/node_modules/@openclaw/fs-safe/dist/secret-file.d.ts +1 -0
  81. package/node_modules/@openclaw/fs-safe/dist/secret-file.js +2 -0
  82. package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.d.ts +1 -0
  83. package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.js +84 -4
  84. package/node_modules/@openclaw/fs-safe/dist/sibling-temp.d.ts +1 -0
  85. package/node_modules/@openclaw/fs-safe/dist/sibling-temp.js +2 -0
  86. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-acquire.js +22 -15
  87. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.d.ts +22 -0
  88. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.js +303 -0
  89. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.d.ts +2 -0
  90. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.js +9 -1
  91. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.d.ts +8 -0
  92. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.js +37 -3
  93. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.d.ts +14 -0
  94. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.js +155 -0
  95. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-stale-admission.js +7 -2
  96. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.d.ts +10 -0
  97. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.js +143 -0
  98. package/node_modules/@openclaw/fs-safe/dist/temp-target.js +28 -7
  99. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.d.ts +2 -2
  100. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.js +34 -18
  101. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.d.ts +1 -1
  102. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.js +13 -3
  103. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-ownership.js +1 -1
  104. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-types.d.ts +9 -0
  105. package/node_modules/@openclaw/fs-safe/dist/test-hooks.d.ts +6 -0
  106. package/node_modules/@openclaw/fs-safe/dist/text-atomic.d.ts +1 -1
  107. package/node_modules/@openclaw/fs-safe/dist/text-atomic.js +1 -0
  108. package/node_modules/@openclaw/fs-safe/dist/windows-owner.d.ts +1 -0
  109. package/node_modules/@openclaw/fs-safe/dist/windows-owner.js +2 -1
  110. package/node_modules/@openclaw/fs-safe/dist/windows-security-helper.exe +0 -0
  111. package/node_modules/@openclaw/fs-safe/docs/advanced.md +6 -1
  112. package/node_modules/@openclaw/fs-safe/docs/atomic.md +64 -0
  113. package/node_modules/@openclaw/fs-safe/docs/contributing.md +69 -0
  114. package/node_modules/@openclaw/fs-safe/docs/file-store.md +2 -0
  115. package/node_modules/@openclaw/fs-safe/docs/json-store.md +2 -0
  116. package/node_modules/@openclaw/fs-safe/docs/json.md +2 -0
  117. package/node_modules/@openclaw/fs-safe/docs/output.md +2 -0
  118. package/node_modules/@openclaw/fs-safe/docs/permissions.md +20 -7
  119. package/node_modules/@openclaw/fs-safe/docs/reading.md +24 -9
  120. package/node_modules/@openclaw/fs-safe/docs/root.md +2 -0
  121. package/node_modules/@openclaw/fs-safe/docs/security-model.md +2 -0
  122. package/node_modules/@openclaw/fs-safe/docs/sidecar-lock.md +12 -7
  123. package/node_modules/@openclaw/fs-safe/docs/staged-file.md +2 -0
  124. package/node_modules/@openclaw/fs-safe/docs/temp-isolation-reference.md +73 -0
  125. package/node_modules/@openclaw/fs-safe/docs/temp.md +65 -0
  126. package/node_modules/@openclaw/fs-safe/docs/testing.md +1 -0
  127. package/node_modules/@openclaw/fs-safe/docs/writing.md +2 -0
  128. package/node_modules/@openclaw/fs-safe/package.json +25 -20
  129. package/node_modules/@openclaw/fs-safe-darwin-arm64/fs-safe-native.node +0 -0
  130. package/node_modules/@openclaw/fs-safe-darwin-arm64/package.json +1 -1
  131. package/node_modules/@openclaw/fs-safe-darwin-x64/fs-safe-native.node +0 -0
  132. package/node_modules/@openclaw/fs-safe-darwin-x64/package.json +1 -1
  133. package/node_modules/@openclaw/fs-safe-freebsd-arm64/fs-safe-native.node +0 -0
  134. package/node_modules/@openclaw/fs-safe-freebsd-arm64/package.json +1 -1
  135. package/node_modules/@openclaw/fs-safe-freebsd-x64/fs-safe-native.node +0 -0
  136. package/node_modules/@openclaw/fs-safe-freebsd-x64/package.json +1 -1
  137. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/fs-safe-native.node +0 -0
  138. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/package.json +1 -1
  139. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/fs-safe-native.node +0 -0
  140. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/package.json +1 -1
  141. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/fs-safe-native.node +0 -0
  142. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/package.json +1 -1
  143. package/node_modules/@openclaw/fs-safe-linux-x64-musl/fs-safe-native.node +0 -0
  144. package/node_modules/@openclaw/fs-safe-linux-x64-musl/package.json +1 -1
  145. package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/fs-safe-native.node +0 -0
  146. package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/package.json +1 -1
  147. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/fs-safe-native.node +0 -0
  148. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/package.json +1 -1
  149. package/package.json +5 -5
@@ -50,6 +50,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
50
50
  var e = new Error(message);
51
51
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
52
  });
53
+ import { SidecarFallbackWriteError } from "./sidecar-lock-write.js";
53
54
  import { exactFileIdentity, numberStats } from "./exact-identity.js";
54
55
  import { registerRootHandleContext } from "./root-handle-context.js";
55
56
  import fsSync, { constants as fsConstants } from "node:fs";
@@ -313,6 +314,7 @@ export class RootHandle {
313
314
  mkdir: this.defaults.mkdir,
314
315
  mode: this.defaults.mode,
315
316
  renameIdentity: this.defaults.renameIdentity,
317
+ allowDrvfsRenameRecovery: this.defaults.allowDrvfsRenameRecovery,
316
318
  ...this.mutationOptions(options),
317
319
  atomic: undefined,
318
320
  private: undefined,
@@ -328,6 +330,7 @@ export class RootHandle {
328
330
  await writeFileInRoot(this.context, {
329
331
  mkdir: this.defaults.mkdir,
330
332
  mode: this.defaults.mode,
333
+ allowDrvfsRenameRecovery: this.defaults.allowDrvfsRenameRecovery,
331
334
  ...this.mutationOptions(createInputOptions(data, options, this.defaults.maxBytes)),
332
335
  relativePath,
333
336
  data,
@@ -354,6 +357,7 @@ export class RootHandle {
354
357
  await copyFileInRoot(this.context, {
355
358
  maxBytes: normalizeMaxBytes(maxBytes, { defaultValue: this.defaults.maxBytes }),
356
359
  mkdir: this.defaults.mkdir,
360
+ allowDrvfsRenameRecovery: this.defaults.allowDrvfsRenameRecovery,
357
361
  ...copyOptions,
358
362
  source: copySource,
359
363
  relativePath,
@@ -954,8 +958,11 @@ async function writeFileInRoot(root, params) {
954
958
  params = { ...params, private: permissions.private, mode: permissions.mode };
955
959
  }
956
960
  const input = rootWriteInput(params);
961
+ if (input.kind === "buffer" && input.sidecar) {
962
+ params = { ...params, ...snapshotPinnedMutationPolicy(params.denyMutations, params.mutationSymlinks) };
963
+ }
957
964
  await serializePathWrite(rootWriteQueueKey(root, params.relativePath), async () => {
958
- if (!params.private && input.kind === "buffer" && !input.stageBeforePublish && process.platform === "win32" &&
965
+ if (!params.private && input.kind === "buffer" && !input.sidecar && !input.stageBeforePublish && process.platform === "win32" &&
959
966
  (params.renameIdentity === "verify-content-with-lock" || !getPinnedWriteNativeBinding({ input }))) {
960
967
  await writeFileFallback(root, { ...params, data: input.data });
961
968
  return;
@@ -983,6 +990,7 @@ async function commitPinnedWriteInRoot(root, pinned, params, input) {
983
990
  basename: pinned.basename,
984
991
  targetPath: pinned.targetPath,
985
992
  renameIdentity: params.renameIdentity,
993
+ allowDrvfsRenameRecovery: params.allowDrvfsRenameRecovery,
986
994
  mkdir: !params.private && params.mkdir !== false,
987
995
  private: params.private,
988
996
  mode: params.mode ?? pinned.mode,
@@ -995,6 +1003,8 @@ async function commitPinnedWriteInRoot(root, pinned, params, input) {
995
1003
  rootIdentity: root.rootIdentity,
996
1004
  mutationAdmission: pinned.mutationAdmission,
997
1005
  assertBeforeMutation: params.assertBeforeMutation,
1006
+ assertSidecarCleanupCandidate: input.kind === "buffer" && input.sidecar
1007
+ ? candidate => assertMutationNotDenied(candidate, params.denyMutations) : undefined,
998
1008
  verifyPublished: async (fd, expectedIdentity, parentGuard, nativeParent) => {
999
1009
  verifyingPublication = true;
1000
1010
  try {
@@ -1012,6 +1022,12 @@ async function commitPinnedWriteInRoot(root, pinned, params, input) {
1012
1022
  });
1013
1023
  }
1014
1024
  catch (error) {
1025
+ if (error instanceof SidecarFallbackWriteError) {
1026
+ // Windows keeps its historical direct-write error identity.
1027
+ if (process.platform === "win32")
1028
+ throw error.failure;
1029
+ error = error.failure;
1030
+ }
1015
1031
  if (verifyingPublication)
1016
1032
  throw error;
1017
1033
  const errorCode = error?.code;
@@ -20,19 +20,21 @@ export type RootDefaults = {
20
20
  mode?: number;
21
21
  denyMutations?: DenyMutationPolicy;
22
22
  renameIdentity?: RenameIdentityPolicy;
23
+ /** Explicit verified-DrvFS close-all recovery; off by default. */
24
+ allowDrvfsRenameRecovery?: boolean;
23
25
  symlinks?: SymlinkPolicy;
24
26
  mutationSymlinks?: MutationSymlinkPolicy;
25
27
  };
26
28
  export type RootReadOptions = Pick<RootDefaults, "hardlinks" | "maxBytes" | "symlinks">;
27
29
  export type RootOpenOptions = Omit<RootReadOptions, "maxBytes">;
28
- export type RootWriteOptions = Pick<RootDefaults, "assertBeforeMutation" | "denyMutations" | "mutationSymlinks" | "durable" | "mkdir" | "mode" | "renameIdentity"> & {
30
+ export type RootWriteOptions = Pick<RootDefaults, "assertBeforeMutation" | "denyMutations" | "mutationSymlinks" | "durable" | "mkdir" | "mode" | "renameIdentity" | "allowDrvfsRenameRecovery"> & {
29
31
  encoding?: BufferEncoding;
30
32
  overwrite?: boolean;
31
33
  };
32
34
  export type RootOpenWritableOptions = Pick<RootDefaults, "assertBeforeMutation" | "denyMutations" | "mutationSymlinks" | "mkdir" | "mode"> & {
33
35
  writeMode?: WritableOpenMode;
34
36
  };
35
- export type RootCopyOptions = Pick<RootDefaults, "assertBeforeMutation" | "denyMutations" | "mutationSymlinks" | "durable" | "maxBytes" | "mkdir" | "mode"> & {
37
+ export type RootCopyOptions = Pick<RootDefaults, "assertBeforeMutation" | "denyMutations" | "mutationSymlinks" | "durable" | "maxBytes" | "mkdir" | "mode" | "allowDrvfsRenameRecovery"> & {
36
38
  sourceHardlinks?: HardlinkPolicy;
37
39
  overwrite?: boolean;
38
40
  clone?: CopyCloneMode;
@@ -46,7 +46,7 @@ export type ObservedRootPath = {
46
46
  receipt?: RootPathObservationReceipt;
47
47
  };
48
48
  export declare function resolveRootPath(params: ResolveRootPathParams): Promise<ResolvedRootPath>;
49
- export declare function resolveRootPathWithCanonicalRootObservation(params: ResolveRootPathParams, observeRoot: (rootCanonicalPath: string) => void): Promise<ResolvedRootPath>;
49
+ export declare function resolveRootPathWithCanonicalRootObservation(params: ResolveRootPathParams, observeRoot: (rootCanonicalPath: string) => void | Promise<void>): Promise<ResolvedRootPath>;
50
50
  export declare function resolveRootPathWithObservation(params: ResolveRootPathParams, request: RootPathObservationRequest): Promise<ObservedRootPath>;
51
51
  export declare function resolveRootPathForRemoval(params: ResolveRootPathParams, receipts: RemovalPathReceipts): Promise<ResolvedRootPath>;
52
52
  export declare function resolveRootPathSync(params: ResolveRootPathParams): ResolvedRootPath;
@@ -273,7 +273,8 @@ function* traverseRootPath(params, { mode = "native", observation: observationOu
273
273
  assertNoWindowsPathAlias(rootPath);
274
274
  assertNoWindowsPathAlias(absolutePath);
275
275
  assertNoWindowsPathAlias(rootCanonicalPath);
276
- observeRoot?.(rootCanonicalPath);
276
+ if (observeRoot)
277
+ yield* wait(observeRoot(rootCanonicalPath));
277
278
  const context = createLexicalTraversalContext(yield* prepareRootTraversal(input, rootPath, rootCanonicalPath, absolutePath, rawAbsolutePath, synchronous));
278
279
  const { state } = context;
279
280
  const observation = createLexicalTraversalObservation(context, observationOutput?.request);
@@ -1,12 +1,14 @@
1
1
  import fsSync, {} from "node:fs";
2
+ import fs from "node:fs/promises";
2
3
  import { FsSafeError } from "./errors.js";
3
4
  import { openedPathResolutionError, recordOpenedFileFailure } from "./opened-file-failure.js";
4
5
  import { isNotFoundPathError } from "./path.js";
5
6
  import { admitPathInsideRoot } from "./root-boundary.js";
6
- import { assertRootIdentityCurrentSync } from "./root-context.js";
7
+ import { assertRootIdentityCurrent, assertRootIdentityCurrentSync } from "./root-context.js";
8
+ import { runAsync, runSync, wait } from "./atomic-io.js";
7
9
  import { fileNotFoundError, hardlinkedPathNotAllowedError, outsideWorkspaceError } from "./root-errors.js";
8
- import { realpathSync } from "./realpath.js";
9
- import { inspectFileIdentitySync } from "./strict-file-identity.js";
10
+ import { realpathNative, realpathSync } from "./realpath.js";
11
+ import { inspectFileIdentity, inspectFileIdentitySync } from "./strict-file-identity.js";
10
12
  import { assertNoWindowsPathAlias } from "./windows-path-alias.js";
11
13
  function resolveFinalOpenedRealPath(filePath) {
12
14
  try {
@@ -31,6 +33,12 @@ export function inspectOpenedPathIdentitySync(filePath, symlinks) {
31
33
  }
32
34
  return stat;
33
35
  }
36
+ async function inspectOpenedPathIdentity(filePath, symlinks) {
37
+ const stat = await (symlinks === "follow-within-root" ? fs.stat(filePath, { bigint: true }) : fs.lstat(filePath, { bigint: true }));
38
+ if (stat.isSymbolicLink() && symlinks !== "follow-within-root")
39
+ throw new FsSafeError("symlink", "symlink not allowed");
40
+ return stat;
41
+ }
34
42
  export async function admitRootReadHandle(params) {
35
43
  try {
36
44
  if (params.hardlinks !== "allow" && params.opened.stat.nlink > 1) {
@@ -39,14 +47,20 @@ export async function admitRootReadHandle(params) {
39
47
  if (params.beforeFinalFence) {
40
48
  await params.beforeFinalFence(params.filePath, params.opened.handle);
41
49
  }
42
- try {
43
- assertRootIdentityCurrentSync(params.root);
50
+ function* admit() {
51
+ const synchronous = process.platform === "win32";
52
+ if (synchronous)
53
+ assertRootIdentityCurrentSync(params.root);
54
+ else
55
+ yield* wait(assertRootIdentityCurrent(params.root));
44
56
  let realPath;
45
57
  try {
46
- const current = inspectFileIdentitySync(() => inspectOpenedPathIdentitySync(params.filePath, params.symlinks), params.identity);
58
+ const current = synchronous
59
+ ? inspectFileIdentitySync(() => inspectOpenedPathIdentitySync(params.filePath, params.symlinks), params.identity)
60
+ : yield* wait(inspectFileIdentity(() => inspectOpenedPathIdentity(params.filePath, params.symlinks), params.identity));
47
61
  if (params.hardlinks !== "allow" && current.nlink > 1n)
48
62
  throw hardlinkedPathNotAllowedError();
49
- const canonicalPath = resolveFinalOpenedRealPath(params.filePath);
63
+ const canonicalPath = synchronous ? resolveFinalOpenedRealPath(params.filePath) : yield* wait(realpathNative(params.filePath));
50
64
  assertNoWindowsPathAlias(canonicalPath, "filesystem", "resolved file path uses a Windows filesystem namespace alias");
51
65
  const admittedRealPath = admitPathInsideRoot({
52
66
  rootPath: params.root.rootReal,
@@ -58,7 +72,9 @@ export async function admitRootReadHandle(params) {
58
72
  }
59
73
  // This is a fresh no-follow observation even when canonicalPath and
60
74
  // filePath have the same spelling; realpath may have raced a replacement.
61
- const canonical = inspectFileIdentitySync(() => inspectOpenedPathIdentitySync(canonicalPath, undefined), params.identity);
75
+ const canonical = synchronous
76
+ ? inspectFileIdentitySync(() => inspectOpenedPathIdentitySync(canonicalPath, undefined), params.identity)
77
+ : yield* wait(inspectFileIdentity(() => inspectOpenedPathIdentity(canonicalPath), params.identity));
62
78
  if (params.hardlinks !== "allow" && canonical.nlink > 1n)
63
79
  throw hardlinkedPathNotAllowedError();
64
80
  realPath = admittedRealPath.path;
@@ -69,9 +85,15 @@ export async function admitRootReadHandle(params) {
69
85
  : error;
70
86
  }
71
87
  params.afterPathIdentityCheck?.(params.filePath, params.opened.handle);
72
- assertRootIdentityCurrentSync(params.root);
88
+ if (synchronous)
89
+ assertRootIdentityCurrentSync(params.root);
90
+ else
91
+ yield* wait(assertRootIdentityCurrent(params.root));
73
92
  return { ...params.opened, realPath };
74
93
  }
94
+ try {
95
+ return process.platform === "win32" ? runSync(admit()) : await runAsync(admit());
96
+ }
75
97
  catch (error) {
76
98
  if (params.identity.nlink <= 1n) {
77
99
  await recordOpenedFileFailure(error, params.opened.handle, params.filePath, params.identity);
@@ -3,6 +3,7 @@ import path from "node:path";
3
3
  import { assertMutationNotDenied } from "./deny-mutations.js";
4
4
  import { assertAsyncDirectoryGuard, assertSyncDirectoryGuard, createAsyncDirectoryGuard, } from "./directory-guard.js";
5
5
  import { FsSafeError } from "./errors.js";
6
+ import { recordFileObservationFailure } from "./file-observation.js";
6
7
  import { isNotFoundPathError } from "./path.js";
7
8
  import { assertNoPathAliasEscape } from "./path-policy.js";
8
9
  import { preparePinnedWriteMutationAdmission, snapshotPinnedMutationPolicy, } from "./pinned-mutation-admission.js";
@@ -267,8 +268,11 @@ export async function resolvePinnedWriteTargetInRoot(root, relativePath, request
267
268
  if (existing) {
268
269
  if (!existing.isFile() && !existing.isDirectory())
269
270
  throw new FsSafeError("not-file", "not a file");
270
- if (existing.isFile() && existing.nlink > 1)
271
- throw hardlinkedPathNotAllowedError();
271
+ if (existing.isFile() && existing.nlink > 1) {
272
+ const error = hardlinkedPathNotAllowedError();
273
+ recordFileObservationFailure(error, "hardlink");
274
+ throw error;
275
+ }
272
276
  throw new FsSafeError("already-exists", "file already exists");
273
277
  }
274
278
  }
@@ -7,6 +7,7 @@ export declare const PRIVATE_SECRET_FILE_MODE = 384;
7
7
  export declare function readSecretFileSync(filePath: string, label: string, options?: SecretFileReadOptions): string;
8
8
  export declare function tryReadSecretFileSync(filePath: string | undefined, label: string, options?: SecretFileReadOptions): string | undefined;
9
9
  type SecretFileWriteParams = {
10
+ allowDrvfsRenameRecovery?: boolean;
10
11
  rootDir: string;
11
12
  filePath: string;
12
13
  content: string | Uint8Array;
@@ -209,6 +209,7 @@ function snapshotSecretFileWriteParams(params) {
209
209
  rootDir,
210
210
  filePath,
211
211
  content: params.content,
212
+ allowDrvfsRenameRecovery: params.allowDrvfsRenameRecovery,
212
213
  mode: params.mode,
213
214
  dirMode: params.dirMode,
214
215
  durable: params.durable,
@@ -289,6 +290,7 @@ async function materializeSecretFileAtomic(params, createOnly) {
289
290
  mkdir: false,
290
291
  mode,
291
292
  verifyPosixMode: true,
293
+ allowDrvfsRenameRecovery: params.allowDrvfsRenameRecovery,
292
294
  sync: params.durable !== false,
293
295
  strictFileSync: createOnly && params.durable === "file",
294
296
  overwrite: !createOnly,
@@ -14,6 +14,7 @@ export declare function writeCallbackSibling<T>(params: {
14
14
  maxBytes?: number;
15
15
  syncTempFile: boolean;
16
16
  syncParentDir: boolean;
17
+ allowDrvfsRenameRecovery?: boolean;
17
18
  }): Promise<{
18
19
  filePath: string;
19
20
  result: T;
@@ -7,6 +7,10 @@ import { assertDirectoryIdentitySync, createAsyncDirectoryGuard, } from "./direc
7
7
  import { syncDirectoryBestEffort } from "./directory-durability.js";
8
8
  import { isWindowsReservedDeviceName } from "./device-path.js";
9
9
  import { FsSafeError } from "./errors.js";
10
+ import { retainDrvfsParent } from "./drvfs-parent.js";
11
+ import { sha256FileSync } from "./file-hash.js";
12
+ import { hasErrorCode } from "./file-cleanup.js";
13
+ import { assertRecoveredFile, recoverAfterHiddenRename, shouldPrepareDrvfsRecovery } from "./publication-recovery.js";
10
14
  import { getNativeBinding } from "./native.js";
11
15
  import { isFsSafeNativeRequired } from "./native-config.js";
12
16
  import { cachedNoReplaceDevice, isNoReplaceUnavailable } from "./native-noreplace.js";
@@ -161,6 +165,7 @@ export async function writeCallbackSibling(params) {
161
165
  const maxBytes = params.maxBytes;
162
166
  const syncTempFile = params.syncTempFile;
163
167
  const syncParentDir = params.syncParentDir;
168
+ const allowDrvfsRenameRecovery = params.allowDrvfsRenameRecovery === true;
164
169
  const rootBoundary = params.rootBoundary;
165
170
  const guard = await createAsyncDirectoryGuard(parent, { bigint: true });
166
171
  const assertParent = () => {
@@ -177,6 +182,9 @@ export async function writeCallbackSibling(params) {
177
182
  throw new FsSafeError("outside-workspace", "sibling parent resolves outside the root");
178
183
  }
179
184
  };
185
+ let recoveryParent;
186
+ let recoveryIdentity;
187
+ let recoveryHash;
180
188
  let handle;
181
189
  let identity;
182
190
  let unregister;
@@ -186,7 +194,7 @@ export async function writeCallbackSibling(params) {
186
194
  const inspectPath = (pathname, expected) => inspectStage(() => fsSync.lstatSync(pathname, { bigint: true }), expected);
187
195
  const assertCurrent = (pathname) => {
188
196
  assertParent();
189
- const opened = inspectStage(() => fsSync.fstatSync(handle.fd, { bigint: true }), identity);
197
+ const opened = handle ? inspectStage(() => fsSync.fstatSync(handle.fd, { bigint: true }), identity) : identity;
190
198
  const current = inspectPath(pathname, opened);
191
199
  if (maxBytes !== undefined &&
192
200
  (opened.size > maxBytes || current.size > maxBytes)) {
@@ -272,15 +280,81 @@ export async function writeCallbackSibling(params) {
272
280
  await syncFileBestEffort(handle);
273
281
  assertCurrent(tempPath);
274
282
  }
283
+ if (shouldPrepareDrvfsRecovery(allowDrvfsRenameRecovery)) {
284
+ recoveryParent = retainDrvfsParent(parent, guard.stat.dev);
285
+ if (recoveryParent) {
286
+ recoveryIdentity = fsSync.fstatSync(handle.fd, { bigint: true });
287
+ recoveryHash = sha256FileSync(handle.fd, { maxBytes }).digest;
288
+ }
289
+ }
275
290
  await fs.rename(tempPath, filePath);
276
291
  // A later verification failure never authorizes rollback of the final name.
277
292
  renamed = true;
278
293
  unregister();
279
- assertCurrent(filePath);
280
- if (syncParentDir) {
281
- await syncDirectoryBestEffort(parent);
294
+ let recoveredHandle;
295
+ let settled = false;
296
+ try {
282
297
  assertCurrent(filePath);
283
298
  }
299
+ catch (error) {
300
+ if (!hasErrorCode(error, "ENOENT") || !recoveryParent)
301
+ throw error;
302
+ // Keep the recovered descriptor through parent synchronization and final admission.
303
+ const assertPublishedCurrent = (reopened) => {
304
+ assertParent();
305
+ recoveryParent.assertCurrent();
306
+ assertRecoveredFile(reopened.fd, recoveryIdentity);
307
+ if (sha256FileSync(reopened.fd, { maxBytes }).digest !== recoveryHash) {
308
+ throw new FsSafeError("path-mismatch", "sibling content changed during DrvFS recovery");
309
+ }
310
+ const opened = inspectStage(() => fsSync.fstatSync(reopened.fd, { bigint: true }), identity);
311
+ const current = inspectPath(filePath, opened);
312
+ if (maxBytes !== undefined && (opened.size > maxBytes || current.size > maxBytes)) {
313
+ throw new FsSafeError("too-large", `sibling temp exceeds maxBytes (${maxBytes})`);
314
+ }
315
+ };
316
+ const previous = handle;
317
+ handle = undefined;
318
+ try {
319
+ recoveredHandle = await recoverAfterHiddenRename({
320
+ closeWriter: () => previous.close(),
321
+ parentFd: recoveryParent.fd,
322
+ reopen: async () => {
323
+ const reopened = await fs.open(filePath, resolveReadOpenFlags());
324
+ try {
325
+ assertPublishedCurrent(reopened);
326
+ return reopened;
327
+ }
328
+ catch (verifyError) {
329
+ await reopened.close().catch(() => undefined);
330
+ throw verifyError;
331
+ }
332
+ },
333
+ retain: async (reopened) => {
334
+ if (syncParentDir)
335
+ await syncDirectoryBestEffort(parent);
336
+ assertPublishedCurrent(reopened);
337
+ },
338
+ dispose: async (reopened) => { await reopened.close().catch(() => undefined); },
339
+ });
340
+ }
341
+ catch (error) {
342
+ // Reclaim the writer if recovery failed before closing it; a
343
+ // second close on an already-closed handle is swallowed.
344
+ await previous.close().catch(() => undefined);
345
+ throw error;
346
+ }
347
+ settled = true;
348
+ }
349
+ try {
350
+ if (syncParentDir && !settled) {
351
+ await syncDirectoryBestEffort(parent);
352
+ assertCurrent(filePath);
353
+ }
354
+ }
355
+ finally {
356
+ await recoveredHandle?.close().catch(() => undefined);
357
+ }
284
358
  });
285
359
  return { filePath, result };
286
360
  }
@@ -290,6 +364,12 @@ export async function writeCallbackSibling(params) {
290
364
  }
291
365
  finally {
292
366
  const settlementErrors = [];
367
+ try {
368
+ recoveryParent?.close();
369
+ }
370
+ catch (error) {
371
+ settlementErrors.push(error);
372
+ }
293
373
  if (cleanupWorkspace) {
294
374
  try {
295
375
  await cleanupWorkspace();
@@ -14,6 +14,7 @@ export type WriteSiblingTempFileOptions<T> = {
14
14
  syncTempFile?: boolean;
15
15
  /** Best-effort parent directory sync after rename; defaults to false. */
16
16
  syncParentDir?: boolean;
17
+ allowDrvfsRenameRecovery?: boolean;
17
18
  };
18
19
  export type WriteSiblingTempFileResult<T> = {
19
20
  filePath: string;
@@ -34,6 +34,7 @@ export async function writeSiblingTempFile(options) {
34
34
  const mode = options.mode;
35
35
  const syncTempFile = options.syncTempFile === true;
36
36
  const syncParentDir = options.syncParentDir === true;
37
+ const allowDrvfsRenameRecovery = options.allowDrvfsRenameRecovery === true;
37
38
  if (dirMode === "preserve" || chmodDir) {
38
39
  await runAsync(prepareDirectoryMode(AtomicIo.async(fs), {
39
40
  dirPath: dir,
@@ -53,6 +54,7 @@ export async function writeSiblingTempFile(options) {
53
54
  ignoreModeError: true,
54
55
  syncTempFile,
55
56
  syncParentDir,
57
+ allowDrvfsRenameRecovery,
56
58
  });
57
59
  }
58
60
  function buildSiblingTempName(params) {
@@ -4,9 +4,9 @@ import { FsSafeError } from "./errors.js";
4
4
  import { fileObservation } from "./file-observation.js";
5
5
  import { sameFileIdentityForCleanup } from "./file-identity.js";
6
6
  import { readFileHandleBounded } from "./bounded-read.js";
7
- import { openSidecarRoot } from "./sidecar-lock-root.js";
7
+ import { openSidecarRoot, rootSidecarPublicationPending } from "./sidecar-lock-root.js";
8
8
  import { createNativeExclusiveFile } from "./native-operations.js";
9
- import { assertExclusiveCreateLeaf } from "./exclusive-create.js";
9
+ import { createSidecarFile } from "./sidecar-lock-publication.js";
10
10
  import { sidecarLockRetryDelay, sidecarLockTimeout, isTransientLockFileDenial, maxTransientLockDenials, validateSidecarLockStaleMs, validateSidecarLockRetryOptions, validateSidecarLockCompromiseCheckIntervalMs, validateSidecarLockTimeoutMs, } from "./sidecar-lock-policy.js";
11
11
  import { relativeSidecarLockPath, removeSidecarLockIfUnchanged, serializeSidecarLockPayload, sidecarReclaimGuardExists, } from "./sidecar-lock-reclaim.js";
12
12
  import { awaitSidecarAdmissionBoundary, conditionalSidecarLockParser, observeHeldSidecarParser, observeSidecarLockParser, runSidecarAdmissionBoundary, } from "./sidecar-lock-admission-parser.js";
@@ -259,9 +259,17 @@ export async function acquireSidecarLock(options, context) {
259
259
  }));
260
260
  }
261
261
  catch (error) {
262
+ let cause = error;
263
+ for (let depth = 0; depth < 4 && cause && typeof cause === "object"; depth++) {
264
+ if (observation.has(cause, "hardlink") && await rootSidecarPublicationPending(lockRoot, relativeLockPath, { cleanAbandoned: true })) {
265
+ exclusiveCreateConflict = true;
266
+ throw Object.assign(new Error("sidecar publication in progress"), { code: "EEXIST" });
267
+ }
268
+ cause = Object.getOwnPropertyDescriptor(cause, "cause")?.value;
269
+ }
262
270
  // Only this invocation's failed exclusive open grants denial retry authority.
263
- lockFileCreateDenied = observation.has(error, `exclusive-create:${lockPath}`) &&
264
- isTransientLockFileDenial(error, lockPath);
271
+ lockFileCreateDenied = observation.has(error, `sidecar-create-denial:${lockPath}`) ||
272
+ (observation.has(error, `exclusive-create:${lockPath}`) && isTransientLockFileDenial(error, lockPath));
265
273
  if (error instanceof FsSafeError && error.code === "already-exists") {
266
274
  exclusiveCreateConflict = true;
267
275
  throw Object.assign(new Error("sidecar lock exists"), { code: "EEXIST" });
@@ -289,23 +297,22 @@ export async function acquireSidecarLock(options, context) {
289
297
  handle = opened.handle;
290
298
  }
291
299
  else {
292
- const failedOpen = (createError) => {
293
- lockFileCreateDenied = isTransientLockFileDenial(createError, lockPath);
294
- exclusiveCreateConflict = createError.code === "EEXIST";
300
+ const failedOpen = (createError, pathname = lockPath) => {
301
+ lockFileCreateDenied = isTransientLockFileDenial(createError, pathname);
302
+ exclusiveCreateConflict = pathname === lockPath && createError.code === "EEXIST";
295
303
  throw createError;
296
304
  };
297
- handle = await createNativeExclusiveFile(lockPath, 0o600).catch(failedOpen) ?? null;
298
- if (!handle) {
305
+ const created = await createSidecarFile(lockPath, raw, async (pathname) => {
299
306
  try {
300
- assertExclusiveCreateLeaf(lockPath);
307
+ return await createNativeExclusiveFile(pathname, 0o600) ?? await fs.open(pathname, fsSync.constants.O_WRONLY | fsSync.constants.O_CREAT | fsSync.constants.O_EXCL |
308
+ (process.platform !== "win32" ? fsSync.constants.O_NOFOLLOW ?? 0 : 0), 0o600);
301
309
  }
302
310
  catch (error) {
303
- exclusiveCreateConflict = error.code === "EEXIST";
304
- throw error;
311
+ return failedOpen(error, pathname);
305
312
  }
306
- handle = await fs.open(lockPath, "wx", 0o600).catch(failedOpen);
307
- }
308
- await handle.writeFile(raw, "utf8");
313
+ }, { onConflict: failedOpen });
314
+ handle = created.handle;
315
+ createdSnapshot = { raw, payload, stat: created.identity, ownershipToken };
309
316
  }
310
317
  const snapshot = { raw, payload, stat: fsSync.fstatSync(handle.fd, { bigint: true }), ownershipToken };
311
318
  createdSnapshot = snapshot;
@@ -0,0 +1,22 @@
1
+ import { type BigIntStats } from "node:fs";
2
+ import type { NativeWriteParent } from "./pinned-write-types.js";
3
+ export type SidecarPublicationOptions = {
4
+ nativeParent?: NativeWriteParent;
5
+ assertCurrent?: () => void;
6
+ assertCleanupCandidate?: (candidate: string) => void;
7
+ onConflict?: (error: unknown) => void;
8
+ prepare?: (fd: number) => void;
9
+ requireExactIdentity?: boolean;
10
+ };
11
+ export declare function createSidecarFileSync(targetPath: string, raw: string, open: (pathname: string) => number, options?: SidecarPublicationOptions): {
12
+ fd: number;
13
+ identity: BigIntStats;
14
+ };
15
+ export declare function createSidecarFile<T extends {
16
+ fd: number;
17
+ close(): Promise<void>;
18
+ writeFile(data: string, encoding: BufferEncoding): Promise<void>;
19
+ }>(targetPath: string, raw: string, open: (pathname: string) => Promise<T>, options?: SidecarPublicationOptions): Promise<{
20
+ handle: T;
21
+ identity: BigIntStats;
22
+ }>;