@openclaw/feishu 2026.10.1 → 2026.10.5-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 (116) hide show
  1. package/dist/.setup/{app-registration-BB6TubaW.mjs → app-registration-DQOq5urV.mjs} +13 -32
  2. package/dist/.setup/{channel-DoRSlOQd.mjs → channel-CjGGyrdx.mjs} +87 -174
  3. package/dist/.setup/{channel.runtime-jLSFNkJh.mjs → channel.runtime-SaC_uTPM.mjs} +57 -74
  4. package/dist/.setup/{chat-mTBvMMvx.mjs → chat-fC3dvxFs.mjs} +16 -28
  5. package/dist/.setup/{client-CcxmEkoy.mjs → client-wwr8PGPH.mjs} +24 -30
  6. package/dist/.setup/{conversation-id-C1ofUfU8.mjs → conversation-id-D00i9uy5.mjs} +5 -10
  7. package/dist/.setup/{doctor-contract-DyOo8NvC.mjs → doctor-contract-oIpUWtNO.mjs} +8 -15
  8. package/dist/.setup/{monitor-DX3vAlY9.mjs → monitor-Yt__BUki.mjs} +33 -54
  9. package/dist/.setup/{monitor.account-C_uDFPtG.mjs → monitor.account-CVz16781.mjs} +377 -664
  10. package/dist/.setup/{probe-BjSykXB8.mjs → probe-DXnFxq5j.mjs} +14 -17
  11. package/dist/.setup/{reply-delivery-result-laBNOmBu.mjs → reply-delivery-result-iCCrdxE1.mjs} +153 -181
  12. package/dist/.setup/{session-conversation-C53IEUWt.mjs → session-conversation-BSBNK5Mq.mjs} +1 -1
  13. package/dist/.setup/{setup-api-DTTsvzfC.mjs → setup-api-B_LFQQ-Q.mjs} +1 -1
  14. package/dist/.setup/{subagent-hooks-C8Z_x3KJ.mjs → subagent-hooks-9Pc1-k37.mjs} +7 -17
  15. package/dist/.setup/{thread-bindings-jW2x5VuN.mjs → thread-bindings-DdNLxoL3.mjs} +21 -44
  16. package/dist/api.js +218 -326
  17. package/dist/channel-plugin-api.js +1 -1
  18. package/dist/contract-api.js +1 -1
  19. package/dist/doctor-contract-api.js +1 -1
  20. package/dist/session-binding-contract-api.js +1 -1
  21. package/dist/session-key-api.js +1 -1
  22. package/dist/setup-api.js +1 -1
  23. package/dist/setup-entry.js +1 -1
  24. package/dist/subagent-hooks-api.js +1 -1
  25. package/node_modules/@openclaw/fs-safe/CHANGELOG.md +35 -0
  26. package/node_modules/@openclaw/fs-safe/README.md +1 -1
  27. package/node_modules/@openclaw/fs-safe/dist/advanced.d.ts +1 -1
  28. package/node_modules/@openclaw/fs-safe/dist/advanced.js +1 -1
  29. package/node_modules/@openclaw/fs-safe/dist/archive-durability.js +1 -1
  30. package/node_modules/@openclaw/fs-safe/dist/archive-merge.js +4 -4
  31. package/node_modules/@openclaw/fs-safe/dist/archive-parser.wasm +0 -0
  32. package/node_modules/@openclaw/fs-safe/dist/archive-read.js +5 -4
  33. package/node_modules/@openclaw/fs-safe/dist/archive-staging.d.ts +1 -1
  34. package/node_modules/@openclaw/fs-safe/dist/archive-staging.js +22 -38
  35. package/node_modules/@openclaw/fs-safe/dist/copy-path-admission.d.ts +17 -0
  36. package/node_modules/@openclaw/fs-safe/dist/copy-path-admission.js +117 -0
  37. package/node_modules/@openclaw/fs-safe/dist/copy-root-file-sync.d.ts +8 -5
  38. package/node_modules/@openclaw/fs-safe/dist/copy-root-file-sync.js +27 -50
  39. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-acquire.d.ts +3 -0
  40. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-acquire.js +57 -11
  41. package/node_modules/@openclaw/fs-safe/dist/guarded-mutation.js +5 -5
  42. package/node_modules/@openclaw/fs-safe/dist/guest-native-python.js +52 -5
  43. package/node_modules/@openclaw/fs-safe/dist/guest.js +1 -1
  44. package/node_modules/@openclaw/fs-safe/dist/native-binding.d.ts +1 -0
  45. package/node_modules/@openclaw/fs-safe/dist/native-operations.d.ts +0 -1
  46. package/node_modules/@openclaw/fs-safe/dist/native-operations.js +22 -26
  47. package/node_modules/@openclaw/fs-safe/dist/native-pinned-write-windows.js +5 -1
  48. package/node_modules/@openclaw/fs-safe/dist/native-staged-file.js +4 -1
  49. package/node_modules/@openclaw/fs-safe/dist/permission-exec.js +2 -1
  50. package/node_modules/@openclaw/fs-safe/dist/pinned-write-types.d.ts +6 -1
  51. package/node_modules/@openclaw/fs-safe/dist/private-temp-workspace.js +2 -2
  52. package/node_modules/@openclaw/fs-safe/dist/publish-file.js +34 -7
  53. package/node_modules/@openclaw/fs-safe/dist/replace-file-copy-fallback.js +1 -4
  54. package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.js +6 -0
  55. package/node_modules/@openclaw/fs-safe/dist/root-create-input.d.ts +15 -1
  56. package/node_modules/@openclaw/fs-safe/dist/root-create-input.js +4 -0
  57. package/node_modules/@openclaw/fs-safe/dist/root-directory-entry.js +1 -1
  58. package/node_modules/@openclaw/fs-safe/dist/root-directory-list.d.ts +0 -1
  59. package/node_modules/@openclaw/fs-safe/dist/root-directory-list.js +6 -12
  60. package/node_modules/@openclaw/fs-safe/dist/root-impl.js +15 -15
  61. package/node_modules/@openclaw/fs-safe/dist/root-paths.js +1 -1
  62. package/node_modules/@openclaw/fs-safe/dist/root-walk.js +4 -4
  63. package/node_modules/@openclaw/fs-safe/dist/root-write-verification.d.ts +10 -1
  64. package/node_modules/@openclaw/fs-safe/dist/root-write-verification.js +20 -0
  65. package/node_modules/@openclaw/fs-safe/dist/secure-temp-dir.js +19 -7
  66. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-acquire.js +25 -3
  67. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-admission.d.ts +2 -0
  68. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-retained.d.ts +14 -0
  69. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-retained.js +187 -0
  70. package/node_modules/@openclaw/fs-safe/dist/sidecar-lock.js +15 -1
  71. package/node_modules/@openclaw/fs-safe/dist/temp-directory-diagnostics.d.ts +10 -0
  72. package/node_modules/@openclaw/fs-safe/dist/temp-directory-diagnostics.js +29 -0
  73. package/node_modules/@openclaw/fs-safe/dist/temp-target.js +1 -1
  74. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.js +6 -6
  75. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.d.ts +4 -4
  76. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.js +17 -16
  77. package/node_modules/@openclaw/fs-safe/dist/temp-workspace-descriptor.js +20 -28
  78. package/node_modules/@openclaw/fs-safe/dist/walk.js +2 -13
  79. package/node_modules/@openclaw/fs-safe/dist/watch-native.d.ts +2 -2
  80. package/node_modules/@openclaw/fs-safe/dist/watch-native.js +3 -1
  81. package/node_modules/@openclaw/fs-safe/dist/watch-rescan.js +3 -9
  82. package/node_modules/@openclaw/fs-safe/dist/watch.js +2 -3
  83. package/node_modules/@openclaw/fs-safe/docs/advanced.md +1 -1
  84. package/node_modules/@openclaw/fs-safe/docs/archive.md +3 -0
  85. package/node_modules/@openclaw/fs-safe/docs/atomic.md +11 -3
  86. package/node_modules/@openclaw/fs-safe/docs/contributing.md +1 -1
  87. package/node_modules/@openclaw/fs-safe/docs/copy.md +48 -0
  88. package/node_modules/@openclaw/fs-safe/docs/guest.md +21 -4
  89. package/node_modules/@openclaw/fs-safe/docs/path-scope.md +4 -0
  90. package/node_modules/@openclaw/fs-safe/docs/sidecar-lock.md +46 -8
  91. package/node_modules/@openclaw/fs-safe/docs/temp.md +12 -0
  92. package/node_modules/@openclaw/fs-safe/docs/watch.md +3 -2
  93. package/node_modules/@openclaw/fs-safe/package.json +15 -15
  94. package/node_modules/@openclaw/fs-safe-darwin-arm64/fs-safe-native.node +0 -0
  95. package/node_modules/@openclaw/fs-safe-darwin-arm64/package.json +1 -1
  96. package/node_modules/@openclaw/fs-safe-darwin-x64/fs-safe-native.node +0 -0
  97. package/node_modules/@openclaw/fs-safe-darwin-x64/package.json +1 -1
  98. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/fs-safe-native.node +0 -0
  99. package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/package.json +1 -1
  100. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/fs-safe-native.node +0 -0
  101. package/node_modules/@openclaw/fs-safe-linux-arm64-musl/package.json +1 -1
  102. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/fs-safe-native.node +0 -0
  103. package/node_modules/@openclaw/fs-safe-linux-x64-gnu/package.json +1 -1
  104. package/node_modules/@openclaw/fs-safe-linux-x64-musl/fs-safe-native.node +0 -0
  105. package/node_modules/@openclaw/fs-safe-linux-x64-musl/package.json +1 -1
  106. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/fs-safe-native.node +0 -0
  107. package/node_modules/@openclaw/fs-safe-win32-x64-msvc/package.json +1 -1
  108. package/node_modules/follow-redirects/index.js +10 -1
  109. package/node_modules/follow-redirects/package.json +1 -1
  110. package/package.json +5 -5
  111. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-arbitration.d.ts +0 -17
  112. package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-arbitration.js +0 -66
  113. package/node_modules/@openclaw/fs-safe/dist/publish-file-native.d.ts +0 -4
  114. package/node_modules/@openclaw/fs-safe/dist/publish-file-native.js +0 -35
  115. package/node_modules/@openclaw/fs-safe/dist/root-move-preflight.d.ts +0 -7
  116. package/node_modules/@openclaw/fs-safe/dist/root-move-preflight.js +0 -16
@@ -10,10 +10,40 @@ import { SyncLockAcquisition } from "./file-lock-sync-acquisition.js";
10
10
  import { assertSynchronousCallbackResult } from "./mutation-authority.js";
11
11
  import { assertNoWindowsPathAlias } from "./windows-path-alias.js";
12
12
  import { admitFileLockSyncRootPath, assertFileLockSyncRootPathsCurrent, captureFileLockSyncRootAuthority, normalizeFileLockSyncTargetWithRoot, } from "./file-lock-sync-root.js";
13
- import { cleanupCreatedRootSyncLock, FileLockSyncRootArbitrationCollision, tryReuseCurrentRootSyncHeldLock, } from "./file-lock-sync-root-arbitration.js";
14
13
  import { fileLockSyncRootSnapshotStillCurrent, } from "./file-lock-sync-root-io.js";
15
14
  import { createFileLockSyncRootDirectory, createFileLockSyncRootFile, fileLockSyncRootGuardExists, fileLockSyncRootDirectoryReceiptStillCurrent, refreshFileLockSyncRootFileReceipt, removeFileLockSyncRootDirectory, removeFileLockSyncRootFile, } from "./file-lock-sync-root-mutation.js";
16
- import { createRootSyncHeldLockHandle, ensureRootSyncExitCleanupRegistered, getRootSyncHeldLocks, readRootSidecarSnapshotSync, } from "./file-lock-sync-root-held.js";
15
+ import { createRootSyncHeldLockHandle, ensureRootSyncExitCleanupRegistered, getRootSyncHeldLocks, readRootSidecarSnapshotSync, verifyRootSyncHeldLock, } from "./file-lock-sync-root-held.js";
16
+ class FileLockSyncRootArbitrationCollision extends FsSafeError {
17
+ constructor() {
18
+ super("path-mismatch", "file lock arbitration changed during local creation");
19
+ }
20
+ }
21
+ export function cleanupCreatedRootSyncLock(lockRootPath, fd, receipt, timer) {
22
+ let timerCleanupFailed = false;
23
+ let timerCleanupError;
24
+ try {
25
+ if (timer)
26
+ clearInterval(timer);
27
+ }
28
+ catch (error) {
29
+ timerCleanupFailed = true;
30
+ timerCleanupError = error;
31
+ }
32
+ try {
33
+ fs.closeSync(fd);
34
+ if (!removeFileLockSyncRootFile(lockRootPath, receipt)) {
35
+ throw new FsSafeError("path-mismatch", "created sidecar lock changed before cleanup");
36
+ }
37
+ }
38
+ catch (fileCleanupError) {
39
+ if (timerCleanupFailed) {
40
+ throw createSuppressedError(timerCleanupError, fileCleanupError, "unpublished lock timer and file cleanup both failed");
41
+ }
42
+ throw fileCleanupError;
43
+ }
44
+ if (timerCleanupFailed)
45
+ throw timerCleanupError;
46
+ }
17
47
  export function acquireFileLockSyncWithRoot(targetPath, inputOptions, lockRoot) {
18
48
  // The public dispatcher already read lockRoot exactly once. Validate and
19
49
  // snapshot that genuine Root before any remaining option or retry getter can
@@ -37,23 +67,39 @@ export function acquireFileLockSyncWithRoot(targetPath, inputOptions, lockRoot)
37
67
  const heldLocks = getRootSyncHeldLocks();
38
68
  const currentTargetHolder = () => heldLocks.get(normalizedTargetPath) ??
39
69
  foreignSyncHeldLock("root", normalizedTargetPath);
40
- const arbitration = Object.freeze({
41
- authority,
42
- heldLocks,
43
- lockRootPath,
44
- normalizedTargetPath,
45
- reentrantOwner: options.reentrantOwner,
46
- });
70
+ const tryReuseCurrentHeld = () => {
71
+ const held = heldLocks.get(normalizedTargetPath);
72
+ const reusable = held && !foreignSyncHeldLock("root", normalizedTargetPath) &&
73
+ options.reentrantOwner !== undefined && held.reentrantOwner !== undefined &&
74
+ options.reentrantOwner === held.reentrantOwner &&
75
+ authority.adapter === held.rootAuthority.adapter &&
76
+ lockRootPath.relativePath === held.rootPath.relativePath &&
77
+ path.relative(lockRootPath.path, held.rootPath.path) === "" &&
78
+ held.releaseState !== "released";
79
+ if (!reusable)
80
+ return undefined;
81
+ if (!verifyRootSyncHeldLock(held)) {
82
+ throw new FsSafeError("path-mismatch", "held sidecar lock changed before reentrant reuse");
83
+ }
84
+ // The parser can reenter acquisition; recheck the exact held entry before reuse.
85
+ if (heldLocks.get(normalizedTargetPath) !== held || held.releaseState === "released" ||
86
+ foreignSyncHeldLock("root", normalizedTargetPath)) {
87
+ throw new FsSafeError("path-mismatch", "held sidecar lock changed during reentrant reuse");
88
+ }
89
+ held.refCount += 1;
90
+ held.revision += 1;
91
+ return createRootSyncHeldLockHandle(held);
92
+ };
47
93
  if (heldLocks.has(normalizedTargetPath) && !foreignSyncHeldLock("root", normalizedTargetPath)) {
48
94
  assertFileLockSyncRootPathsCurrent(guardedPaths);
49
- const initiallyReusable = tryReuseCurrentRootSyncHeldLock(arbitration);
95
+ const initiallyReusable = tryReuseCurrentHeld();
50
96
  if (initiallyReusable)
51
97
  return initiallyReusable;
52
98
  }
53
99
  const acquisition = new SyncLockAcquisition(lockPath, normalizedTargetPath, options.retry, options.timeoutMs);
54
100
  let ownedReclaimGuard;
55
101
  let reclaimCleanupAttempted = false;
56
- const reuseCurrentHeld = () => ownedReclaimGuard ? undefined : tryReuseCurrentRootSyncHeldLock(arbitration);
102
+ const reuseCurrentHeld = () => ownedReclaimGuard ? undefined : tryReuseCurrentHeld();
57
103
  const releaseReclaimGuard = () => {
58
104
  const receipt = ownedReclaimGuard;
59
105
  if (!receipt)
@@ -1,16 +1,16 @@
1
1
  import fsSync, {} from "node:fs";
2
2
  import fs from "node:fs/promises";
3
3
  import path from "node:path";
4
- import { assertAsyncDirectoryGuard, assertSyncDirectoryGuard, createAsyncDirectoryGuard, createSyncDirectoryGuard, } from "./directory-guard.js";
4
+ import { assertDirectoryGuard, assertSyncDirectoryGuard, createAsyncDirectoryGuard, createSyncDirectoryGuard, } from "./directory-guard.js";
5
5
  export async function withAsyncDirectoryGuards(guards, mutate, options = {}) {
6
6
  for (const guard of guards) {
7
- await assertAsyncDirectoryGuard(guard);
7
+ assertDirectoryGuard(guard, "native");
8
8
  }
9
9
  const result = await mutate();
10
10
  if (options.verifyAfter !== false) {
11
11
  try {
12
12
  for (const guard of guards) {
13
- await assertAsyncDirectoryGuard(guard);
13
+ assertDirectoryGuard(guard, "native");
14
14
  }
15
15
  }
16
16
  catch (error) {
@@ -46,11 +46,11 @@ export async function guardedRename(params) {
46
46
  const sourceGuard = await createAsyncDirectoryGuard(path.dirname(params.from));
47
47
  const targetGuard = await createAsyncDirectoryGuard(path.dirname(params.to));
48
48
  await withAsyncDirectoryGuards([sourceGuard, targetGuard], async () => {
49
+ // Authority must survive all awaited guards; do not yield before rename dispatch.
50
+ params.assertBeforeRename?.();
49
51
  if (params.onSourceInspected) {
50
52
  params.onSourceInspected(fsSync.lstatSync(params.from, { bigint: true }));
51
53
  }
52
- // Authority must survive all awaited guards; do not yield before rename dispatch.
53
- params.assertBeforeRename?.();
54
54
  await fs.rename(params.from, params.to);
55
55
  params.onRenamed?.();
56
56
  });
@@ -32,6 +32,16 @@ export const GUEST_FILESYSTEM_CREATE_STAGING_PYTHON = [
32
32
  ].join("\n");
33
33
  export const GUEST_FILESYSTEM_RENAME_NO_REPLACE_PYTHON = [
34
34
  "def rename_no_replace(src_parent_fd, src_basename, dst_parent_fd, dst_basename):",
35
+ " import stat",
36
+ " source = os.lstat(src_basename, dir_fd=src_parent_fd)",
37
+ " kind = stat.S_IFMT(source.st_mode)",
38
+ " expected = (source.st_dev, source.st_ino, kind)",
39
+ " def verify(parent_fd, basename, links):",
40
+ " current = os.lstat(basename, dir_fd=parent_fd)",
41
+ " if (current.st_dev, current.st_ino, stat.S_IFMT(current.st_mode)) != expected:",
42
+ " raise OSError(errno.ESTALE, 'no-replace publication identity changed', basename)",
43
+ " if kind == stat.S_IFREG and current.st_nlink != links:",
44
+ " raise OSError(errno.ESTALE, 'no-replace publication link count changed', basename)",
35
45
  " libc = ctypes.CDLL(None, use_errno=True)",
36
46
  " is_linux = sys.platform.startswith('linux')",
37
47
  " if is_linux:",
@@ -62,6 +72,31 @@ export const GUEST_FILESYSTEM_RENAME_NO_REPLACE_PYTHON = [
62
72
  " raise OSError(error_code, os.strerror(error_code), dst_basename)",
63
73
  " elif not is_linux:",
64
74
  " raise OSError(errno.ENOSYS, 'atomic no-replace rename is unavailable')",
75
+ " # Only a definitive rejection of the fixed flags reaches this fallback.",
76
+ " if kind not in (stat.S_IFDIR, stat.S_IFREG):",
77
+ " raise OSError(errno.EINVAL, 'no-replace fallback requires a file or directory', src_basename)",
78
+ " verify(src_parent_fd, src_basename, 1)",
79
+ " try:",
80
+ " os.lstat(dst_basename, dir_fd=dst_parent_fd)",
81
+ " except FileNotFoundError:",
82
+ " pass",
83
+ " else:",
84
+ " raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), dst_basename)",
85
+ " if kind == stat.S_IFDIR:",
86
+ " verify(src_parent_fd, src_basename, 1)",
87
+ " # A directory rename can replace only an EMPTY directory raced into",
88
+ " # the absent target. Non-empty directories and files are refused.",
89
+ " # The slash enforces source directory type at the syscall, even if",
90
+ " # a peer substitutes a file/symlink after verification. Identity",
91
+ " # checks are not compare-and-swap; verify publication afterward.",
92
+ " try:",
93
+ " os.rename(src_basename + '/', dst_basename, src_dir_fd=src_parent_fd, dst_dir_fd=dst_parent_fd)",
94
+ " except OSError as error:",
95
+ " if error.errno in (errno.ENOTEMPTY, errno.ENOTDIR, errno.EEXIST):",
96
+ " raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), dst_basename) from error",
97
+ " raise",
98
+ " verify(dst_parent_fd, dst_basename, 1)",
99
+ " return",
65
100
  " os.link(",
66
101
  " src_basename,",
67
102
  " dst_basename,",
@@ -69,12 +104,19 @@ export const GUEST_FILESYSTEM_RENAME_NO_REPLACE_PYTHON = [
69
104
  " dst_dir_fd=dst_parent_fd,",
70
105
  " follow_symlinks=False,",
71
106
  " )",
107
+ " # Only this exact two-name interval permits nlink=2. Never roll back the",
108
+ " # target on failure: it may be the only remaining link to the source.",
109
+ " verify(dst_parent_fd, dst_basename, 2)",
110
+ " verify(src_parent_fd, src_basename, 2)",
111
+ " os.unlink(src_basename, dir_fd=src_parent_fd)",
112
+ " verify(dst_parent_fd, dst_basename, 1)",
72
113
  ].join("\n");
73
114
  export const GUEST_FILESYSTEM_CREATE_EXCLUSIVE_PYTHON = [
74
115
  "def create_exclusive(parent_fd, basename, stdin_buffer):",
75
116
  " staging_fd = None",
76
117
  " staging_name = None",
77
118
  " temp_fd = None",
119
+ " temp_inode = None",
78
120
  " temp_name = 'payload'",
79
121
  " try:",
80
122
  " try:",
@@ -85,6 +127,7 @@ export const GUEST_FILESYSTEM_CREATE_EXCLUSIVE_PYTHON = [
85
127
  " raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), basename)",
86
128
  " staging_name, staging_fd = create_staging_dir(parent_fd)",
87
129
  " temp_fd = os.open(temp_name, WRITE_FLAGS, 0o600, dir_fd=staging_fd)",
130
+ " temp_inode = inode_identity(os.fstat(temp_fd))",
88
131
  " while True:",
89
132
  " chunk = stdin_buffer.read(65536)",
90
133
  " if not chunk:",
@@ -92,8 +135,7 @@ export const GUEST_FILESYSTEM_CREATE_EXCLUSIVE_PYTHON = [
92
135
  " write_all(temp_fd, chunk)",
93
136
  " # exclusive create payload is durable before publication",
94
137
  " os.fsync(temp_fd)",
95
- " temp_inode = inode_identity(os.fstat(temp_fd))",
96
- " # Publish with a native atomic no-replace rename.",
138
+ " # Publish with no-replace rename or the checked file fallback.",
97
139
  " rename_no_replace(staging_fd, temp_name, parent_fd, basename)",
98
140
  " target_stat = os.lstat(basename, dir_fd=parent_fd)",
99
141
  " if temp_inode != inode_identity(target_stat):",
@@ -105,14 +147,19 @@ export const GUEST_FILESYSTEM_CREATE_EXCLUSIVE_PYTHON = [
105
147
  " if staging_fd is not None:",
106
148
  " # Cleanup stays relative to the private pinned directory, so a",
107
149
  " # parent-path substitution cannot redirect payload deletion.",
150
+ " payload_removed = False",
108
151
  " try:",
109
- " os.unlink(temp_name, dir_fd=staging_fd)",
152
+ " current_payload = os.lstat(temp_name, dir_fd=staging_fd)",
153
+ " # Preserve ambiguous or partially published link pairs.",
154
+ " if temp_inode == inode_identity(current_payload) and current_payload.st_nlink == 1:",
155
+ " os.unlink(temp_name, dir_fd=staging_fd)",
156
+ " payload_removed = True",
110
157
  " except FileNotFoundError:",
111
- " pass",
158
+ " payload_removed = True",
112
159
  " staging_identity = entry_identity(os.fstat(staging_fd))",
113
160
  " os.close(staging_fd)",
114
161
  " staging_removed = False",
115
- " if staging_name is not None:",
162
+ " if staging_name is not None and payload_removed:",
116
163
  " try:",
117
164
  " current_staging = os.lstat(staging_name, dir_fd=parent_fd)",
118
165
  " if same_identity(staging_identity, current_staging):",
@@ -395,7 +395,7 @@ export const GUEST_FILESYSTEM_PYTHON = [
395
395
  " break",
396
396
  " write_all(temp_fd, chunk)",
397
397
  " try:",
398
- " os.fchmod(temp_fd, stat.S_IMODE(src_stat.st_mode))",
398
+ " os.fchmod(temp_fd, stat.S_IMODE(src_file_stat.st_mode))",
399
399
  " except AttributeError:",
400
400
  " pass",
401
401
  " os.fsync(temp_fd)",
@@ -139,6 +139,7 @@ export interface NativeBinding {
139
139
  renameReplaceWithIdentity?(sourceParent: number, source: string, targetParent: number, target: string, dev: bigint, ino: bigint): void;
140
140
  /** Windows-only, private handle custody; no borrowed/runtime descriptors. */
141
141
  retainWindowsFile?(directory: string, basename: string, parentDev: bigint, parentIno: bigint, dev: bigint, ino: bigint, size: bigint, mtimeNs: bigint, ctimeNs: bigint, sha256: string, maxBytes: number): NativeRetainedFile;
142
+ retainWindowsSidecar?(parentFd: number, directory: string, basename: string, parentDev: bigint, parentIno: bigint, dev: bigint, ino: bigint, size: bigint, sha256: string, maxBytes: number): NativeRetainedFile;
142
143
  watchRegister?(root: string, limit: number, callback: (batch: import("./watch-native.js").NativeWatchWireBatch) => void, persistent: boolean): number;
143
144
  watchConfigure?(id: number, anchors: string[], exclusions: string[]): void;
144
145
  watchEntries?(id: number, entries: import("./watch-native.js").NativeWatchEntry[]): {
@@ -5,7 +5,6 @@ export type NativeFileHandle = {
5
5
  writeFile(data: string | Buffer, encoding?: BufferEncoding): Promise<void>;
6
6
  };
7
7
  export declare function nativeOpenFlags(flags: number): number;
8
- export declare function writeNativeFd(fd: number, data: Buffer): void;
9
8
  export declare function removeNativeCreatedFileIfStillPinned(params: {
10
9
  parentPath: string;
11
10
  parentFd: number;
@@ -10,31 +10,6 @@ export function nativeOpenFlags(flags) {
10
10
  (closeOnExec ?? 0) |
11
11
  (typeof fsSync.constants.O_NOFOLLOW === "number" ? fsSync.constants.O_NOFOLLOW : 0));
12
12
  }
13
- export function writeNativeFd(fd, data) {
14
- let offset = 0;
15
- while (offset < data.byteLength) {
16
- const written = fsSync.writeSync(fd, data, offset, data.byteLength - offset);
17
- if (written <= 0) {
18
- throw Object.assign(new Error("native file write made no progress"), { code: "EIO" });
19
- }
20
- offset += written;
21
- }
22
- }
23
- function wrapNativeFd(fd, closeFd) {
24
- let open = true;
25
- return {
26
- fd,
27
- async close() {
28
- if (open) {
29
- open = false;
30
- closeFd(fd);
31
- }
32
- },
33
- async writeFile(data, encoding) {
34
- writeNativeFd(fd, Buffer.isBuffer(data) ? data : Buffer.from(data, encoding ?? "utf8"));
35
- },
36
- };
37
- }
38
13
  export function removeNativeCreatedFileIfStillPinned(params) {
39
14
  if (!params.created) {
40
15
  return;
@@ -86,7 +61,28 @@ export async function createNativeExclusiveFile(targetPath, mode) {
86
61
  fd = opened.fd;
87
62
  fsSync.fchmodSync(fd, mode);
88
63
  created = fsSync.fstatSync(fd, { bigint: true });
89
- return wrapNativeFd(fd, closeFd);
64
+ const ownedFd = fd;
65
+ let open = true;
66
+ return {
67
+ fd: ownedFd,
68
+ async close() {
69
+ if (open) {
70
+ open = false;
71
+ closeFd(ownedFd);
72
+ }
73
+ },
74
+ async writeFile(data, encoding) {
75
+ const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data, encoding ?? "utf8");
76
+ let offset = 0;
77
+ while (offset < buffer.byteLength) {
78
+ const written = fsSync.writeSync(ownedFd, buffer, offset, buffer.byteLength - offset);
79
+ if (written <= 0) {
80
+ throw Object.assign(new Error("native file write made no progress"), { code: "EIO" });
81
+ }
82
+ offset += written;
83
+ }
84
+ },
85
+ };
90
86
  }
91
87
  catch (error) {
92
88
  if (fd !== undefined) {
@@ -188,7 +188,11 @@ export async function runPinnedWriteWindows(binding, params, root, parentFd, par
188
188
  if (params.sync !== false)
189
189
  syncFileBestEffortSync(parentFd);
190
190
  // Verification follows publication and final chmod, outside rollback handling.
191
- await params.verifyPublished?.(targetFd, verificationIdentity, parentGuard);
191
+ if (params.overwrite === false && params.input.kind === "buffer" && params.input.stageBeforePublish === false) {
192
+ await params.verifyPublished?.(targetFd, verificationIdentity, parentGuard, { fd: parentFd, binding });
193
+ }
194
+ else
195
+ await params.verifyPublished?.(targetFd, verificationIdentity, parentGuard);
192
196
  completed = true;
193
197
  return { dev: targetIdentity.dev, ino: targetIdentity.ino };
194
198
  }
@@ -177,7 +177,10 @@ class NativeStagedFile {
177
177
  }
178
178
  const identity = published.staged.identity;
179
179
  try {
180
- await params.verifyPublished?.(staged.#file(), identity, parentGuard);
180
+ if (exclusive)
181
+ await params.verifyPublished?.(staged.#file(), identity, parentGuard, { fd: parentFd, binding });
182
+ else
183
+ await params.verifyPublished?.(staged.#file(), identity, parentGuard);
181
184
  }
182
185
  catch (error) {
183
186
  if (params.overwrite === false && params.input.kind !== "file" && params.input.stageBeforePublish === true) {
@@ -5,7 +5,8 @@ import { formatErrorDetail } from "./error-detail.js";
5
5
  const execFileAsync = promisify(execFile);
6
6
  export const DEFAULT_PERMISSION_EXEC_TIMEOUT_MS = 30_000;
7
7
  export function formatPermissionErrorDetail(value) {
8
- const formatted = formatErrorDetail(value);
8
+ // Escaping cannot shrink input; 401 code units decide the 400-character excerpt.
9
+ const formatted = formatErrorDetail(value.slice(0, 401));
9
10
  return formatted.length > 400 ? `${formatted.slice(0, 399)}…` : formatted;
10
11
  }
11
12
  const MAX_CAUGHT_FAILURE_PROTOTYPES = 4;
@@ -2,6 +2,11 @@ import type { CopyFileInput } from "./copy-file-input.js";
2
2
  import type { AnyAsyncDirectoryGuard } from "./directory-guard.js";
3
3
  import type { FileIdentityStat } from "./file-identity.js";
4
4
  import type { MutationDirectoryObservation } from "./pinned-mutation-observation.js";
5
+ import type { NativeBinding } from "./native-binding.js";
6
+ export type NativeWriteParent = Readonly<{
7
+ fd: number;
8
+ binding: NativeBinding;
9
+ }>;
5
10
  export type PinnedWriteInput = {
6
11
  kind: "buffer";
7
12
  data: string | Buffer;
@@ -64,5 +69,5 @@ export type PinnedWriteParams = {
64
69
  mutationAdmission?: PinnedWriteMutationAdmission;
65
70
  onRenameIdentityMismatch?: "verify-content";
66
71
  onPublished?: (identity: PublishedWriteIdentity) => void;
67
- verifyPublished?: (fd: number, identity: PublishedWriteIdentity, parentGuard: AnyAsyncDirectoryGuard) => Promise<void>;
72
+ verifyPublished?: (fd: number, identity: PublishedWriteIdentity, parentGuard: AnyAsyncDirectoryGuard, nativeParent?: NativeWriteParent) => Promise<void>;
68
73
  };
@@ -166,7 +166,7 @@ async function createTempWorkspace(options, scopedPrefix = false) {
166
166
  else
167
167
  admission.assertCurrent();
168
168
  const stat = inspectDirectoryIdentitySync(dir);
169
- const needsModeInitialization = validateInitialTempWorkspaceChild(stat, admission.ownerUid, dirMode);
169
+ const needsModeInitialization = validateInitialTempWorkspaceChild(stat, admission.ownerUid, dirMode, dir);
170
170
  // Retain while the child still has its private creation mode so an
171
171
  // explicit dirMode such as 0 cannot make identity descriptor acquisition fail.
172
172
  retainedChild = TempWorkspaceRetainedChild.retain(dir, stat);
@@ -261,7 +261,7 @@ function createTempWorkspaceSync(options, scopedPrefix = false) {
261
261
  else {
262
262
  stat = inspectDirectoryIdentitySync(dir);
263
263
  }
264
- const needsModeInitialization = validateInitialTempWorkspaceChild(stat, admission.ownerUid, dirMode);
264
+ const needsModeInitialization = validateInitialTempWorkspaceChild(stat, admission.ownerUid, dirMode, dir);
265
265
  retainedChild ??= TempWorkspaceRetainedChild.retain(dir, stat);
266
266
  if (needsModeInitialization) {
267
267
  retainedChild.initializeModeSync(dirMode, admission.ownerUid, admission.assertCurrent);
@@ -1,4 +1,5 @@
1
- import { publishFileNoReplaceNative } from "./publish-file-native.js";
1
+ import { nativeMoveNoReplace } from "./native-move-fallback.js";
2
+ import { getFsSafeNativeConfig } from "./native-config.js";
2
3
  import { createHash } from "node:crypto";
3
4
  import fsSync, {} from "node:fs";
4
5
  import fs, {} from "node:fs/promises";
@@ -270,12 +271,38 @@ export async function publishFileExclusive(params) {
270
271
  }
271
272
  if (strategy === "rename-noreplace") {
272
273
  const binding = requireNativeBinding();
273
- publishFileNoReplaceNative(binding, [
274
- sourceNativeParent.handle.fd,
275
- sourceNativeParent.basename,
276
- targetNativeParent.handle.fd,
277
- targetNativeParent.basename,
278
- ], sourceExactIdentity, failure);
274
+ try {
275
+ const fallback = nativeMoveNoReplace(binding, [
276
+ sourceNativeParent.handle.fd, sourceNativeParent.basename,
277
+ targetNativeParent.handle.fd, targetNativeParent.basename,
278
+ ], sourceExactIdentity, getFsSafeNativeConfig().mode === "auto", "file publication");
279
+ if (fallback) {
280
+ failure.fallback = fallback;
281
+ failure.sourceRemoval = "removed";
282
+ }
283
+ }
284
+ catch (error) {
285
+ if (error instanceof FsSafeError) {
286
+ if (error.details?.publication === "published") {
287
+ failure.targetCreated = true;
288
+ failure.preserveTarget = true;
289
+ failure.phase = "rename-verify";
290
+ failure.fallback = "link-unlink";
291
+ failure.sourceRemoval = error.details.sourceRemoval;
292
+ // Changed/unverified pairs cannot establish which inode was published.
293
+ if (failure.sourceRemoval === "still-linked")
294
+ rememberCreatedTarget(failure, sourceExactIdentity, "rename-verify");
295
+ }
296
+ throw error;
297
+ }
298
+ const code = error?.code;
299
+ if (code === "path-mismatch" || code === "hardlink") {
300
+ throw new FsSafeError(code, "publication source changed before fallback", { cause: error });
301
+ }
302
+ if (code === "ELOOP")
303
+ throw new FsSafeError("symlink", "publication source became a symlink", { cause: error });
304
+ throw error;
305
+ }
279
306
  rememberCreatedTarget(failure, sourceExactIdentity, "rename-verify");
280
307
  // A failed post-rename fence must not delete the only remaining name.
281
308
  failure.preserveTarget = true;
@@ -139,15 +139,12 @@ function* replacePinnedWithRestore(io, file, replacement, maxRestoreBytes, repla
139
139
  if (io.asynchronous)
140
140
  yield chmod;
141
141
  yield* file.sync();
142
- throw restoreFailure(writeError, "restored");
143
142
  }
144
143
  catch (restoreError) {
145
144
  mutation.rethrowRefusal();
146
- if (restoreError instanceof FsSafeError && restoreError.details?.cleanup === "restored") {
147
- throw restoreError;
148
- }
149
145
  throw restoreFailure(writeError, "restore-failed", restoreError);
150
146
  }
147
+ throw restoreFailure(writeError, "restored");
151
148
  }
152
149
  }
153
150
  export function* copyFallbackReplace(io, params) {
@@ -139,6 +139,12 @@ export function* writeTempFile(io, params) {
139
139
  const writing = file.writeFile(params.content, true);
140
140
  if (io.asynchronous)
141
141
  yield writing;
142
+ if (io.asyncFs === fs && process.platform !== "win32" && !params.sync) {
143
+ // Matching non-durable writes have no I/O left after this identity fence.
144
+ const current = inspectAtomicIdentity(io, () => file.statExact(), identity, true);
145
+ if (Number(current.mode & 4095n) === (params.mode & 0o7777))
146
+ return { file, identity };
147
+ }
142
148
  const chmod = file.chmod(params.mode);
143
149
  if (io.asynchronous)
144
150
  yield chmod;
@@ -1,14 +1,28 @@
1
- import type { PinnedWriteInput } from "./pinned-write-types.js";
1
+ import type { NativeWriteParent, PinnedWriteInput, PublishedWriteIdentity } from "./pinned-write-types.js";
2
2
  import type { RootCreateOptions, RootCreateStreamOptions, RootWriteOptions } from "./root-options.js";
3
+ import type { AnyAsyncDirectoryGuard } from "./directory-guard.js";
3
4
  declare const exclusiveSidecarCreate: unique symbol;
4
5
  export declare const sidecarExclusiveCreate: Readonly<{
5
6
  [exclusiveSidecarCreate]: true;
6
7
  }>;
8
+ export declare const retainSidecarCreated: unique symbol;
9
+ export type SidecarCreationRetention = Readonly<{
10
+ retain(fd: number, parent: AnyAsyncDirectoryGuard, nativeParent?: NativeWriteParent): void;
11
+ cleanup(fd: number, parent: NativeWriteParent, identity: PublishedWriteIdentity): void;
12
+ }>;
7
13
  export type RootWriteParams = RootWriteOptions & RootCreateOptions & RootCreateStreamOptions & {
8
14
  relativePath: string;
9
15
  data: string | Buffer | AsyncIterable<Uint8Array>;
10
16
  strictFileSync?: boolean;
11
17
  [exclusiveSidecarCreate]?: true;
18
+ [retainSidecarCreated]?: SidecarCreationRetention;
19
+ };
20
+ export declare function sidecarRetainedCreate(retain: NonNullable<RootWriteParams[typeof retainSidecarCreated]>): {
21
+ [exclusiveSidecarCreate]: true;
22
+ [retainSidecarCreated]: Readonly<{
23
+ retain(fd: number, parent: AnyAsyncDirectoryGuard, nativeParent?: NativeWriteParent): void;
24
+ cleanup(fd: number, parent: NativeWriteParent, identity: PublishedWriteIdentity): void;
25
+ }>;
12
26
  };
13
27
  export declare function rethrowCreateInputError(error: unknown): never;
14
28
  export declare function createInputOptions(data: RootWriteParams["data"], options: RootCreateOptions & RootCreateStreamOptions, defaultMaxBytes?: number): RootCreateOptions & RootCreateStreamOptions;
@@ -5,6 +5,10 @@ import { MutationAuthorityError } from "./mutation-authority.js";
5
5
  // Keep this composition hook off the public Root options and package exports.
6
6
  const exclusiveSidecarCreate = Symbol("exclusiveSidecarCreate");
7
7
  export const sidecarExclusiveCreate = Object.freeze({ [exclusiveSidecarCreate]: true });
8
+ export const retainSidecarCreated = Symbol("retainSidecarCreated");
9
+ export function sidecarRetainedCreate(retain) {
10
+ return { ...sidecarExclusiveCreate, [retainSidecarCreated]: retain };
11
+ }
8
12
  class CreateInputError extends FsSafeError {
9
13
  rejection;
10
14
  constructor(rejection) {
@@ -17,7 +17,7 @@ export async function lookupRootDirectoryEntry(root, guard, name) {
17
17
  const observed = inspectStatObservationSync(bigint => bigint
18
18
  ? fs.lstatSync(pathname, { bigint: true }) : fs.lstatSync(pathname));
19
19
  await assertRootDirectoryObservationGuard(root, guard);
20
- return { entry: { name, ...pathStatFromStats(observed.stat) }, identity: observed.identity };
20
+ return { entry: pathStatFromStats(observed.stat, name), identity: observed.identity };
21
21
  }
22
22
  catch (error) {
23
23
  await assertRootDirectoryObservationGuard(root, guard);
@@ -16,6 +16,5 @@ export declare function assertRootDirectoryObservationGuard(root: RootContext, g
16
16
  * both its identity and canonical check here.
17
17
  */
18
18
  export declare function assertRootPathObservationReceiptCurrent(root: RootContext, receipt: RootPathObservationReceipt | RootPathParentObservationReceipt, finalTarget?: Stats | BigIntStats): void;
19
- export declare function listDirectoryPath(root: RootContext, directory: string, withFileTypes: true, receipt?: RootPathObservationReceipt): Promise<DirEntry[]>;
20
19
  export declare function listDirectoryPath(root: RootContext, directory: string, withFileTypes: boolean, receipt?: RootPathObservationReceipt): Promise<string[] | DirEntry[]>;
21
20
  export declare function openRootDirectoryListing(root: RootContext, directory: string, options: RootDirectoryListingOptions, receipt?: RootPathObservationReceipt): Promise<RootDirectoryListing>;
@@ -105,10 +105,6 @@ export async function assertRootDirectoryObservationGuard(root, guard) {
105
105
  throw directoryChangedError(error);
106
106
  }
107
107
  }
108
- function sameObservationDirectory(left, right) {
109
- return left.dir === right.dir && left.realPath === right.realPath &&
110
- left.identity.dev === right.identity.dev && left.identity.ino === right.identity.ino;
111
- }
112
108
  function assertReceiptDirectoryGuardSync(root, guard, backend) {
113
109
  if (backend) {
114
110
  const observed = inspectNativeDirectoryObservation(backend, guard.dir, guard.identity);
@@ -139,7 +135,8 @@ export function assertRootPathObservationReceiptCurrent(root, receipt, finalTarg
139
135
  rootGuard.identity.dev !== root.rootIdentity.dev || rootGuard.identity.ino !== root.rootIdentity.ino) {
140
136
  throw rootPathChangedError();
141
137
  }
142
- if (sameObservationDirectory(rootGuard, directoryGuard)) {
138
+ if (rootGuard.dir === directoryGuard.dir && rootGuard.realPath === directoryGuard.realPath &&
139
+ rootGuard.identity.dev === directoryGuard.identity.dev && rootGuard.identity.ino === directoryGuard.identity.ino) {
143
140
  try {
144
141
  if (receipt.kind === "stat" && receipt.target === rootGuard && finalTarget &&
145
142
  !receipt.directoryObserver) {
@@ -176,12 +173,6 @@ function normalizeDirectoryError(error) {
176
173
  }
177
174
  return error;
178
175
  }
179
- function normalizeInitialDirectoryError(error) {
180
- if (error instanceof FsSafeError && error.code === "not-file") {
181
- return new FsSafeError("not-found", "directory not found", { cause: error });
182
- }
183
- return normalizeDirectoryError(error);
184
- }
185
176
  export async function listDirectoryPath(root, directory, withFileTypes, receipt) {
186
177
  let guard;
187
178
  if (receipt) {
@@ -192,7 +183,10 @@ export async function listDirectoryPath(root, directory, withFileTypes, receipt)
192
183
  guard = await createRootDirectoryObservationGuard(root, directory);
193
184
  }
194
185
  catch (error) {
195
- throw normalizeInitialDirectoryError(error);
186
+ if (error instanceof FsSafeError && error.code === "not-file") {
187
+ throw new FsSafeError("not-found", "directory not found", { cause: error });
188
+ }
189
+ throw normalizeDirectoryError(error);
196
190
  }
197
191
  }
198
192
  return await listGuardedDirectoryPath(root, guard, withFileTypes, receipt);