weir-harness 1.1.1 → 1.1.3

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.
@@ -10,14 +10,23 @@ import { dirname, isAbsolute } from 'node:path'
10
10
  */
11
11
 
12
12
  /**
13
- * Read-only POSIX observations, not ownership or publication authority.
13
+ * Read-only filesystem observations, not ownership or publication authority.
14
14
  * Filesystem calls are not an atomic snapshot: callers must coordinate mutation.
15
15
  * dev/ino continuity cannot detect inode reuse or change-and-restore (ABA).
16
+ *
17
+ * Verified platform rows: darwin/linux (POSIX spellings) and win32
18
+ * (openspec windows-platform-adaptation: drive-letter absolute spellings with
19
+ * both separators normalized to '/'; NTFS dev/ino/nlink verified real on Node
20
+ * v24; junctions are reparse points and surface as symbolic links). UNC and
21
+ * device paths are rejected on win32 — network shares are outside the
22
+ * supported envelope (edit-lock.md).
23
+ * @param {{ platform?: string }} [options]
16
24
  */
17
- export function createResourceIdentity() {
18
- if (process.platform !== 'darwin' && process.platform !== 'linux') {
19
- throw new Error(`unsupported platform: ${process.platform}`)
25
+ export function createResourceIdentity({ platform = process.platform } = {}) {
26
+ if (platform !== 'darwin' && platform !== 'linux' && platform !== 'win32') {
27
+ throw new Error(`unsupported platform: ${platform}`)
20
28
  }
29
+ const win = platform === 'win32'
21
30
  /** @type {WeakMap<object, Evidence>} */
22
31
  const observations = new WeakMap()
23
32
  /** @param {string} filePath @param {{ cwd: string }} options */
@@ -25,7 +34,8 @@ export function createResourceIdentity() {
25
34
  if (typeof cwd !== 'string' || !isAbsolute(cwd)) throw new TypeError('expected absolute cwd')
26
35
  if (typeof filePath !== 'string' || filePath.length === 0) throw new TypeError('expected non-empty path')
27
36
  if (filePath.includes('\0') || cwd.includes('\0')) throw new TypeError('NUL in path')
28
- const result = inspect(isAbsolute(filePath) ? filePath : `${cwd}/${filePath}`)
37
+ const joined = isAbsolute(filePath) ? filePath : `${cwd}/${filePath}`
38
+ const result = inspect(win ? normalizeWin(joined) : joined, win)
29
39
  const observation = Object.freeze(result.value)
30
40
  observations.set(observation, { filePath, cwd, kind: observation.kind, witnesses: result.witnesses, links: result.links })
31
41
  return observation
@@ -48,11 +58,22 @@ export function createResourceIdentity() {
48
58
  }
49
59
  }
50
60
 
61
+ /** Canonical spelling for the win32 walk: single forward slashes under a
62
+ * drive-letter root. UNC/device roots are rejected, never reinterpreted.
63
+ * @param {string} spelling */
64
+ function normalizeWin(spelling) {
65
+ const normalized = spelling.replace(/\\/g, '/').replace(/\/{2,}/g, '/')
66
+ if (!/^[A-Za-z]:\//.test(normalized)) throw new Error(`unsupported path root (drive-letter absolute paths only): ${spelling.slice(0, 64)}`)
67
+ return normalized
68
+ }
69
+
51
70
  /** Walk original prefixes without lexically collapsing symlink/.. or missing/..
52
- * @param {string} spelling @returns {Inspection} */
53
- function inspect(spelling) {
71
+ * The ancestor chain terminates at the filesystem root of the spelling —
72
+ * '/' on POSIX, '<drive>:/' on win32 — detected as dirname's fixed point.
73
+ * @param {string} spelling @param {boolean} win @returns {Inspection} */
74
+ function inspect(spelling, win) {
54
75
  const parts = spelling.split('/')
55
- let ancestor = '/'
76
+ let ancestor = win ? /** @type {string} */ (parts[0]) : '/'
56
77
  const witnesses = []
57
78
  let links = 0
58
79
  for (let index = 1; index < parts.length; index++) {
@@ -71,11 +92,11 @@ function inspect(spelling) {
71
92
  ancestor = realpathSync.native(prefix)
72
93
  const node = statSync(ancestor, { bigint: true })
73
94
  witnesses.push(`${ancestor}:${node.dev}:${node.ino}:${entry.dev}:${entry.ino}`)
74
- if (entry.isSymbolicLink()) traceLink(physical, witnesses, 0)
95
+ if (entry.isSymbolicLink()) traceLink(physical, witnesses, 0, win)
75
96
  for (let parent = dirname(ancestor); ; parent = dirname(parent)) {
76
97
  const directory = statSync(parent, { bigint: true })
77
98
  witnesses.push(`${parent}:${directory.dev}:${directory.ino}`)
78
- if (parent === '/') break
99
+ if (parent === dirname(parent)) break
79
100
  }
80
101
  if (index < parts.length - 1) {
81
102
  if (!node.isDirectory()) throw new Error('non-directory ancestor')
@@ -90,18 +111,20 @@ function inspect(spelling) {
90
111
  * the physical cursor avoids recursively replaying the original parent spelling.
91
112
  * Resolve each component only AFTER tracing it: lexical .. collapse or resolving
92
113
  * the whole target up front would hide nested links and their topology changes.
93
- * @param {string} path @param {string[]} witnesses @param {number} depth */
94
- function traceLink(path, witnesses, depth) {
114
+ * On win32 readlink returns backslash spellings; they are normalized before the
115
+ * walk so component splitting matches the POSIX row.
116
+ * @param {string} path @param {string[]} witnesses @param {number} depth @param {boolean} win */
117
+ function traceLink(path, witnesses, depth, win) {
95
118
  if (depth >= 40) throw new Error('unsupported symlink depth')
96
- const target = readlinkSync(path)
119
+ const target = win ? readlinkSync(path).replace(/\\/g, '/') : readlinkSync(path)
97
120
  witnesses.push(`link:${path}:${target}`)
98
- let parent = isAbsolute(target) ? '/' : dirname(path)
121
+ let parent = isAbsolute(target) ? (win ? /** @type {string} */ (target.split('/')[0]) : '/') : dirname(path)
99
122
  const parts = isAbsolute(target) ? target.split('/').slice(1) : target.split('/')
100
123
  for (const part of parts) {
101
124
  const prefix = `${parent}/${part}`
102
125
  const node = lstatSync(prefix, { bigint: true })
103
126
  witnesses.push(`${prefix}:${node.dev}:${node.ino}`)
104
- if (node.isSymbolicLink()) traceLink(prefix, witnesses, depth + 1)
127
+ if (node.isSymbolicLink()) traceLink(prefix, witnesses, depth + 1, win)
105
128
  parent = realpathSync.native(prefix)
106
129
  }
107
130
  }
@@ -17,6 +17,9 @@ async function writeAuthorityFile(directory, file, bytes) {
17
17
  const handle = await open(temporary, 'wx', 0o600)
18
18
  try { await handle.writeFile(bytes); await handle.sync() } finally { await handle.close() }
19
19
  await rename(temporary, join(directory, file))
20
+ // Directory fsync is POSIX-only (EPERM on win32): the file fsync above and
21
+ // the atomic rename stand alone there (reservation.js SYNC_SUPPORTED stance).
22
+ if (process.platform !== 'darwin' && process.platform !== 'linux') return
20
23
  const dir = await open(directory, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW)
21
24
  try { await dir.sync() } finally { await dir.close() }
22
25
  }
@@ -3,6 +3,7 @@
3
3
  // the scheduler's job, clock and timer are injected so the composition and
4
4
  // the unit tests share one core.
5
5
  import { lstatSync } from 'node:fs'
6
+ import { dirname } from 'node:path'
6
7
 
7
8
  /** Missing-target classification for one stored canonical resource id. ONLY
8
9
  * ENOENT counts: a successful lstat — including of a dangling symlink — is
@@ -16,7 +17,23 @@ export function isMissingTarget(resourceId) {
16
17
  lstatSync(resourceId)
17
18
  return false
18
19
  } catch (error) {
19
- return /** @type {any} */ (error)?.code === 'ENOENT'
20
+ const code = /** @type {any} */ (error)?.code
21
+ if (code !== 'ENOENT') return false
22
+ // Windows reports a path through a regular-file component as ENOENT as
23
+ // well (there is no ENOTDIR): preserve the conservative POSIX
24
+ // classification by walking up — a non-directory ancestor means the path
25
+ // is BLOCKED (skip the row), not missing.
26
+ if (process.platform === 'win32') {
27
+ for (let parent = dirname(resourceId); parent !== dirname(parent); parent = dirname(parent)) {
28
+ let stat
29
+ try { stat = lstatSync(parent) } catch (ancestorError) {
30
+ if (/** @type {any} */ (ancestorError)?.code === 'ENOENT') continue
31
+ return false
32
+ }
33
+ return stat.isDirectory()
34
+ }
35
+ }
36
+ return true
20
37
  }
21
38
  }
22
39
 
@@ -6,6 +6,11 @@ import { validateOperationTransitions } from './operation-history.js'
6
6
  import { canonical, parseSnapshot, validateImage } from './snapshot.js'
7
7
  import { validateAdminTransition } from './admin-ledger.js'
8
8
 
9
+ // Directory fsync is POSIX-only (EPERM on win32, Node v24 — same stance as
10
+ // reservation.js SYNC_SUPPORTED): the win32 row persists with file fsync +
11
+ // atomic rename and skips the directory sync steps.
12
+ const DIR_SYNC_SUPPORTED = process.platform === 'darwin' || process.platform === 'linux'
13
+
9
14
  /**
10
15
  * Unmounted historical Edit Lock snapshot storage, NOT a kernel restore interface.
11
16
  * The caller MUST already hold an externally proven exclusive directory lifecycle
@@ -79,11 +84,14 @@ export async function openEditLockStore({ directory, domainId, mode, maxSnapshot
79
84
  await io('file-sync', async () => { await openedFile.sync() })
80
85
  await io('file-close', async () => { await openedFile.close(); file = undefined })
81
86
  await io('rename', async () => { renameStarted = true; await rename(temporary, target) })
82
- await io('directory-open', async () => { dir = await open(directory, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW) })
83
- valid(dir, 'opened directory descriptor')
87
+ // The directory steps fire on every platform — checkpoint cadence is a
88
+ // contract; only the real directory fsync is POSIX-only (EPERM on win32,
89
+ // same stance as reservation.js SYNC_SUPPORTED).
90
+ await io('directory-open', async () => { dir = DIR_SYNC_SUPPORTED ? await open(directory, constants.O_RDONLY | (constants.O_DIRECTORY ?? 0) | (constants.O_NOFOLLOW ?? 0)) : undefined })
91
+ if (DIR_SYNC_SUPPORTED) valid(dir, 'opened directory descriptor')
84
92
  const openedDirectory = dir
85
- await io('directory-sync', async () => { await openedDirectory.sync() })
86
- await io('directory-close', async () => { await openedDirectory.close(); dir = undefined })
93
+ await io('directory-sync', async () => { await openedDirectory?.sync() })
94
+ await io('directory-close', async () => { await openedDirectory?.close(); dir = undefined })
87
95
  committedBytes = Buffer.from(bytes, 'utf8')
88
96
  } catch (cause) {
89
97
  // Never remove/promote a temp or roll back a rename. Once rename was
@@ -108,7 +116,9 @@ export async function openEditLockStore({ directory, domainId, mode, maxSnapshot
108
116
  await persist(current)
109
117
  } else {
110
118
  valid((await lstat(target)).isFile(), 'snapshot must be regular file')
111
- const file = await open(target, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK)
119
+ // win32: O_NOFOLLOW/O_NONBLOCK are undefined — the lstat above and the post-open
120
+ // stat below carry the identity checks (same stance as read-authority.js).
121
+ const file = await open(target, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0))
112
122
  try {
113
123
  valid((await file.stat()).isFile(), 'snapshot must be regular file')
114
124
  let bytes