@developerz.ai/ui-debugger-mcp 1.9.0 → 1.11.0

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 (274) hide show
  1. package/README.md +63 -47
  2. package/dist/adapters/android/abortable.d.ts +6 -0
  3. package/dist/adapters/android/abortable.js +22 -0
  4. package/dist/adapters/android/abortable.js.map +1 -0
  5. package/dist/adapters/android/adb.d.ts +19 -7
  6. package/dist/adapters/android/adb.js +50 -13
  7. package/dist/adapters/android/adb.js.map +1 -1
  8. package/dist/adapters/android/android-adapter.d.ts +20 -12
  9. package/dist/adapters/android/android-adapter.js +113 -165
  10. package/dist/adapters/android/android-adapter.js.map +1 -1
  11. package/dist/adapters/android/commands.d.ts +10 -0
  12. package/dist/adapters/android/commands.js +22 -0
  13. package/dist/adapters/android/commands.js.map +1 -1
  14. package/dist/adapters/android/emulator.d.ts +41 -0
  15. package/dist/adapters/android/emulator.js +115 -0
  16. package/dist/adapters/android/emulator.js.map +1 -0
  17. package/dist/adapters/android/logcat.d.ts +87 -0
  18. package/dist/adapters/android/logcat.js +352 -0
  19. package/dist/adapters/android/logcat.js.map +1 -0
  20. package/dist/adapters/android/ports.js.map +1 -1
  21. package/dist/adapters/android/sdk.d.ts +65 -0
  22. package/dist/adapters/android/sdk.js +107 -0
  23. package/dist/adapters/android/sdk.js.map +1 -0
  24. package/dist/adapters/android/uiautomator.d.ts +23 -7
  25. package/dist/adapters/android/uiautomator.js +31 -7
  26. package/dist/adapters/android/uiautomator.js.map +1 -1
  27. package/dist/adapters/availability.d.ts +39 -0
  28. package/dist/adapters/availability.js +62 -0
  29. package/dist/adapters/availability.js.map +1 -0
  30. package/dist/adapters/browser/browser-adapter.d.ts +11 -22
  31. package/dist/adapters/browser/browser-adapter.js +90 -71
  32. package/dist/adapters/browser/browser-adapter.js.map +1 -1
  33. package/dist/adapters/browser/cdp.d.ts +22 -17
  34. package/dist/adapters/browser/cdp.js +118 -72
  35. package/dist/adapters/browser/cdp.js.map +1 -1
  36. package/dist/adapters/browser/exchange-read.d.ts +39 -0
  37. package/dist/adapters/browser/exchange-read.js +102 -0
  38. package/dist/adapters/browser/exchange-read.js.map +1 -0
  39. package/dist/adapters/browser/extractor.js +9 -2
  40. package/dist/adapters/browser/extractor.js.map +1 -1
  41. package/dist/adapters/browser/filters.d.ts +1 -1
  42. package/dist/adapters/browser/filters.js.map +1 -1
  43. package/dist/adapters/browser/geometry.js.map +1 -1
  44. package/dist/adapters/browser/launch.d.ts +14 -5
  45. package/dist/adapters/browser/launch.js +14 -5
  46. package/dist/adapters/browser/launch.js.map +1 -1
  47. package/dist/adapters/browser/lifecycle.d.ts +91 -0
  48. package/dist/adapters/browser/lifecycle.js +141 -0
  49. package/dist/adapters/browser/lifecycle.js.map +1 -0
  50. package/dist/adapters/browser/locate.d.ts +18 -2
  51. package/dist/adapters/browser/locate.js +48 -3
  52. package/dist/adapters/browser/locate.js.map +1 -1
  53. package/dist/adapters/browser/log-filters.d.ts +2 -2
  54. package/dist/adapters/browser/log-filters.js +15 -4
  55. package/dist/adapters/browser/log-filters.js.map +1 -1
  56. package/dist/adapters/browser/log-format.d.ts +23 -20
  57. package/dist/adapters/browser/log-format.js +133 -56
  58. package/dist/adapters/browser/log-format.js.map +1 -1
  59. package/dist/adapters/browser/tab-context.d.ts +30 -0
  60. package/dist/adapters/browser/tab-context.js +41 -0
  61. package/dist/adapters/browser/tab-context.js.map +1 -0
  62. package/dist/adapters/contract.d.ts +113 -10
  63. package/dist/adapters/contract.js +7 -2
  64. package/dist/adapters/contract.js.map +1 -1
  65. package/dist/adapters/desktop/atspi-busctl.d.ts +26 -0
  66. package/dist/adapters/desktop/atspi-busctl.js +332 -0
  67. package/dist/adapters/desktop/atspi-busctl.js.map +1 -0
  68. package/dist/adapters/desktop/atspi-parse.d.ts +39 -0
  69. package/dist/adapters/desktop/atspi-parse.js +155 -0
  70. package/dist/adapters/desktop/atspi-parse.js.map +1 -0
  71. package/dist/adapters/desktop/atspi.d.ts +81 -50
  72. package/dist/adapters/desktop/atspi.js +64 -285
  73. package/dist/adapters/desktop/atspi.js.map +1 -1
  74. package/dist/adapters/desktop/capture.d.ts +11 -20
  75. package/dist/adapters/desktop/capture.js +68 -37
  76. package/dist/adapters/desktop/capture.js.map +1 -1
  77. package/dist/adapters/desktop/desktop-adapter.d.ts +22 -2
  78. package/dist/adapters/desktop/desktop-adapter.js +202 -86
  79. package/dist/adapters/desktop/desktop-adapter.js.map +1 -1
  80. package/dist/adapters/desktop/input.d.ts +19 -5
  81. package/dist/adapters/desktop/input.js +29 -13
  82. package/dist/adapters/desktop/input.js.map +1 -1
  83. package/dist/adapters/desktop/managed-app.d.ts +43 -0
  84. package/dist/adapters/desktop/managed-app.js +114 -0
  85. package/dist/adapters/desktop/managed-app.js.map +1 -0
  86. package/dist/adapters/desktop/proc.d.ts +34 -4
  87. package/dist/adapters/desktop/proc.js +37 -6
  88. package/dist/adapters/desktop/proc.js.map +1 -1
  89. package/dist/adapters/desktop/wait-poll.d.ts +17 -0
  90. package/dist/adapters/desktop/wait-poll.js +38 -0
  91. package/dist/adapters/desktop/wait-poll.js.map +1 -0
  92. package/dist/adapters/factory.d.ts +13 -6
  93. package/dist/adapters/factory.js +30 -4
  94. package/dist/adapters/factory.js.map +1 -1
  95. package/dist/adapters/log-format.d.ts +38 -0
  96. package/dist/adapters/log-format.js +47 -0
  97. package/dist/adapters/log-format.js.map +1 -0
  98. package/dist/agent/belt/act.d.ts +426 -23
  99. package/dist/agent/belt/act.js +44 -64
  100. package/dist/agent/belt/act.js.map +1 -1
  101. package/dist/agent/belt/look.d.ts +479 -16
  102. package/dist/agent/belt/look.js +94 -16
  103. package/dist/agent/belt/look.js.map +1 -1
  104. package/dist/agent/belt/observe.d.ts +652 -65
  105. package/dist/agent/belt/observe.js +119 -231
  106. package/dist/agent/belt/observe.js.map +1 -1
  107. package/dist/agent/belt/redact-output.d.ts +20 -0
  108. package/dist/agent/belt/redact-output.js +70 -0
  109. package/dist/agent/belt/redact-output.js.map +1 -0
  110. package/dist/agent/belt/report.d.ts +754 -48
  111. package/dist/agent/belt/report.js +121 -38
  112. package/dist/agent/belt/report.js.map +1 -1
  113. package/dist/agent/belt/same-origin.d.ts +21 -0
  114. package/dist/agent/belt/same-origin.js +38 -0
  115. package/dist/agent/belt/same-origin.js.map +1 -0
  116. package/dist/agent/belt/target-refs.d.ts +39 -0
  117. package/dist/agent/belt/target-refs.js +60 -0
  118. package/dist/agent/belt/target-refs.js.map +1 -0
  119. package/dist/agent/belt/trail.d.ts +44 -16
  120. package/dist/agent/belt/trail.js +46 -11
  121. package/dist/agent/belt/trail.js.map +1 -1
  122. package/dist/agent/belt/tree-targets.d.ts +79 -0
  123. package/dist/agent/belt/tree-targets.js +242 -0
  124. package/dist/agent/belt/tree-targets.js.map +1 -0
  125. package/dist/agent/capabilities.js.map +1 -1
  126. package/dist/agent/fallback.d.ts +33 -0
  127. package/dist/agent/fallback.js +84 -0
  128. package/dist/agent/fallback.js.map +1 -0
  129. package/dist/agent/loop-frames.d.ts +26 -0
  130. package/dist/agent/loop-frames.js +106 -0
  131. package/dist/agent/loop-frames.js.map +1 -0
  132. package/dist/agent/loop.d.ts +56 -65
  133. package/dist/agent/loop.js +122 -189
  134. package/dist/agent/loop.js.map +1 -1
  135. package/dist/agent/models.d.ts +10 -6
  136. package/dist/agent/models.js +15 -9
  137. package/dist/agent/models.js.map +1 -1
  138. package/dist/agent/prompts/compose.d.ts +1 -1
  139. package/dist/agent/prompts/debug-agent.js +48 -10
  140. package/dist/agent/prompts/debug-agent.js.map +1 -1
  141. package/dist/agent/prompts/web-addendum.d.ts +2 -2
  142. package/dist/agent/prompts/web-addendum.js +26 -25
  143. package/dist/agent/prompts/web-addendum.js.map +1 -1
  144. package/dist/agent/provider.d.ts +41 -1
  145. package/dist/agent/provider.js +59 -5
  146. package/dist/agent/provider.js.map +1 -1
  147. package/dist/boot.d.ts +64 -0
  148. package/dist/boot.js +147 -0
  149. package/dist/boot.js.map +1 -0
  150. package/dist/cli/chromium-libs.d.ts +267 -0
  151. package/dist/cli/chromium-libs.js +546 -0
  152. package/dist/cli/chromium-libs.js.map +1 -0
  153. package/dist/cli/control.js +79 -12
  154. package/dist/cli/control.js.map +1 -1
  155. package/dist/cli/init.d.ts +52 -3
  156. package/dist/cli/init.js +62 -10
  157. package/dist/cli/init.js.map +1 -1
  158. package/dist/config/load.d.ts +25 -8
  159. package/dist/config/load.js +50 -13
  160. package/dist/config/load.js.map +1 -1
  161. package/dist/config/schema.d.ts +18 -6
  162. package/dist/config/schema.js +34 -17
  163. package/dist/config/schema.js.map +1 -1
  164. package/dist/errors.d.ts +30 -15
  165. package/dist/errors.js +49 -30
  166. package/dist/errors.js.map +1 -1
  167. package/dist/findings/schema.d.ts +44 -6
  168. package/dist/findings/schema.js +39 -3
  169. package/dist/findings/schema.js.map +1 -1
  170. package/dist/index.d.ts +1 -1
  171. package/dist/index.js +1 -1
  172. package/dist/index.js.map +1 -1
  173. package/dist/main.js +4 -82
  174. package/dist/main.js.map +1 -1
  175. package/dist/mcp/server.js +10 -1
  176. package/dist/mcp/server.js.map +1 -1
  177. package/dist/mcp/tools/describe.d.ts +5 -4
  178. package/dist/mcp/tools/describe.js +14 -10
  179. package/dist/mcp/tools/describe.js.map +1 -1
  180. package/dist/mcp/tools/end-session.d.ts +3 -1
  181. package/dist/mcp/tools/end-session.js +8 -3
  182. package/dist/mcp/tools/end-session.js.map +1 -1
  183. package/dist/mcp/tools/output.d.ts +12 -4
  184. package/dist/mcp/tools/output.js +15 -2
  185. package/dist/mcp/tools/output.js.map +1 -1
  186. package/dist/mcp/tools/result.d.ts +10 -1
  187. package/dist/mcp/tools/result.js +12 -7
  188. package/dist/mcp/tools/result.js.map +1 -1
  189. package/dist/mcp/tools/start-debug.js +7 -5
  190. package/dist/mcp/tools/start-debug.js.map +1 -1
  191. package/dist/redact.d.ts +44 -0
  192. package/dist/redact.js +166 -0
  193. package/dist/redact.js.map +1 -0
  194. package/dist/services/auth-persona.d.ts +19 -0
  195. package/dist/services/auth-persona.js +32 -0
  196. package/dist/services/auth-persona.js.map +1 -0
  197. package/dist/services/catalog.d.ts +28 -0
  198. package/dist/services/catalog.js +65 -0
  199. package/dist/services/catalog.js.map +1 -0
  200. package/dist/services/debug-api.d.ts +179 -0
  201. package/dist/services/debug-api.js +9 -0
  202. package/dist/services/debug-api.js.map +1 -0
  203. package/dist/services/debug-service.d.ts +26 -174
  204. package/dist/services/debug-service.js +159 -227
  205. package/dist/services/debug-service.js.map +1 -1
  206. package/dist/services/foreign-findings.d.ts +22 -0
  207. package/dist/services/foreign-findings.js +38 -0
  208. package/dist/services/foreign-findings.js.map +1 -0
  209. package/dist/services/login-failure.d.ts +10 -0
  210. package/dist/services/login-failure.js +24 -0
  211. package/dist/services/login-failure.js.map +1 -0
  212. package/dist/services/login-selectors.d.ts +18 -0
  213. package/dist/services/login-selectors.js +44 -0
  214. package/dist/services/login-selectors.js.map +1 -0
  215. package/dist/services/login-session.d.ts +25 -0
  216. package/dist/services/login-session.js +59 -0
  217. package/dist/services/login-session.js.map +1 -0
  218. package/dist/services/login.d.ts +16 -29
  219. package/dist/services/login.js +269 -97
  220. package/dist/services/login.js.map +1 -1
  221. package/dist/services/replay.js +15 -3
  222. package/dist/services/replay.js.map +1 -1
  223. package/dist/services/retained-snapshot.d.ts +36 -0
  224. package/dist/services/retained-snapshot.js +63 -0
  225. package/dist/services/retained-snapshot.js.map +1 -0
  226. package/dist/services/session-builder.d.ts +26 -0
  227. package/dist/services/session-builder.js +63 -21
  228. package/dist/services/session-builder.js.map +1 -1
  229. package/dist/services/shutdown.d.ts +8 -4
  230. package/dist/services/shutdown.js +21 -8
  231. package/dist/services/shutdown.js.map +1 -1
  232. package/dist/services/start-guards.d.ts +37 -0
  233. package/dist/services/start-guards.js +61 -0
  234. package/dist/services/start-guards.js.map +1 -0
  235. package/dist/services/summarize.d.ts +12 -3
  236. package/dist/services/summarize.js +13 -5
  237. package/dist/services/summarize.js.map +1 -1
  238. package/dist/session/cleanup.d.ts +3 -2
  239. package/dist/session/cleanup.js +6 -2
  240. package/dist/session/cleanup.js.map +1 -1
  241. package/dist/session/finalize.d.ts +45 -0
  242. package/dist/session/finalize.js +150 -0
  243. package/dist/session/finalize.js.map +1 -0
  244. package/dist/session/findings-store.d.ts +2 -1
  245. package/dist/session/findings-store.js +30 -2
  246. package/dist/session/findings-store.js.map +1 -1
  247. package/dist/session/flock-backend.d.ts +117 -0
  248. package/dist/session/flock-backend.js +293 -0
  249. package/dist/session/flock-backend.js.map +1 -0
  250. package/dist/session/live-pointer.d.ts +19 -0
  251. package/dist/session/live-pointer.js +41 -0
  252. package/dist/session/live-pointer.js.map +1 -0
  253. package/dist/session/manager.d.ts +11 -1
  254. package/dist/session/manager.js +46 -2
  255. package/dist/session/manager.js.map +1 -1
  256. package/dist/session/owner-lock.d.ts +117 -0
  257. package/dist/session/owner-lock.js +218 -0
  258. package/dist/session/owner-lock.js.map +1 -0
  259. package/dist/session/process-identity.js.map +1 -1
  260. package/dist/session/session.js +40 -109
  261. package/dist/session/session.js.map +1 -1
  262. package/dist/session/state-file.d.ts +74 -14
  263. package/dist/session/state-file.js +211 -21
  264. package/dist/session/state-file.js.map +1 -1
  265. package/dist/session/workspace.d.ts +32 -2
  266. package/dist/session/workspace.js +65 -4
  267. package/dist/session/workspace.js.map +1 -1
  268. package/package.json +11 -11
  269. package/dist/adapters/android/android-adapter.test-helpers.d.ts +0 -52
  270. package/dist/adapters/android/android-adapter.test-helpers.js +0 -115
  271. package/dist/adapters/android/android-adapter.test-helpers.js.map +0 -1
  272. package/dist/mcp/server.test-helpers.d.ts +0 -23
  273. package/dist/mcp/server.test-helpers.js +0 -49
  274. package/dist/mcp/server.test-helpers.js.map +0 -1
@@ -0,0 +1,293 @@
1
+ /**
2
+ * The `flock(2)` backend behind the workspace ownership lock (`owner-lock.ts`).
3
+ *
4
+ * `flock` comes from the kernel, and the RUNTIME decides how this process
5
+ * reaches it. Under Bun it is libc's, through Bun's FFI; under Node there is
6
+ * no FFI, so the very same kernel lock is taken by util-linux's `flock(1)`,
7
+ * applied to the descriptor this process already holds. Either way it is ONE
8
+ * lock on ONE file: the kernel refuses every other `flock` on it, and it dies
9
+ * with the descriptor. The server targets Linux (see CLAUDE.md); the glibc and
10
+ * musl sonames and the macOS libc are covered, and anything else fails loud —
11
+ * a silent fallback here would be two processes both believing they own the
12
+ * workspace.
13
+ *
14
+ * The FFI access is DYNAMIC: the published bin runs on Node (`package.json` →
15
+ * `dist/main.js` behind `#!/usr/bin/env node`), where `bun:ffi` does not
16
+ * exist — a static import would crash the module on LOAD, before any probe
17
+ * could report why. The boot gate ({@link assertOwnerLockBackend}) resolves
18
+ * the backend BEFORE the server comes up: a host that cannot provide one can
19
+ * never enforce the one-run rule.
20
+ */
21
+ import { spawn } from 'node:child_process';
22
+ import { accessSync, closeSync, constants as fsConstants } from 'node:fs';
23
+ import { delimiter, join } from 'node:path';
24
+ import { WorkspaceError } from '../errors.js';
25
+ /** flock(2) operations — the Linux/glibc values, identical on the BSDs and macOS. */
26
+ const LOCK_EX = 2;
27
+ const LOCK_NB = 4;
28
+ const LOCK_UN = 8;
29
+ /** libc sonames carrying `flock`, in probe order (glibc, musl, macOS). */
30
+ export const LIBC_CANDIDATES = [
31
+ 'libc.so.6',
32
+ 'libc.musl-x86_64.so.1',
33
+ 'libc.musl-aarch64.so.1',
34
+ 'libc.dylib',
35
+ ];
36
+ /**
37
+ * The FFI module, loaded DYNAMICALLY.
38
+ *
39
+ * `bun:ffi` exists only under Bun, and the published bin runs on Node: a
40
+ * static import of it crashes the module on LOAD, before any probe can report
41
+ * why the backend is missing. A failed load is NOT cached — the next attempt
42
+ * retries, so a host that gains the backend (or a later boot in the same
43
+ * process) is not stuck with the first failure.
44
+ */
45
+ let ffiModule;
46
+ async function loadFfi() {
47
+ if (ffiModule !== undefined)
48
+ return ffiModule;
49
+ let loaded;
50
+ try {
51
+ loaded = await import('bun:ffi');
52
+ }
53
+ catch (error) {
54
+ // The runtime has no FFI backend AT ALL (Node): say so plainly. "flock is
55
+ // missing" would send an operator hunting for the wrong libc, when the
56
+ // backend that could have opened one was never there.
57
+ throw new WorkspaceError(`no FFI backend on this runtime — bun:ffi is unavailable: ${error.message}`);
58
+ }
59
+ ffiModule = loaded;
60
+ return loaded;
61
+ }
62
+ /**
63
+ * The default opener: the platform's libc, through Bun's FFI.
64
+ *
65
+ * Exported so a test can compose a failing opener with the REAL backend: a
66
+ * stub symbol cannot prove that a kernel lock was actually taken.
67
+ */
68
+ export async function openFlockInLibc(soname) {
69
+ const ffi = await loadFfi();
70
+ const lib = ffi.dlopen(soname, {
71
+ flock: { args: [ffi.FFIType.int, ffi.FFIType.int], returns: ffi.FFIType.int },
72
+ });
73
+ return lib.symbols.flock;
74
+ }
75
+ /**
76
+ * Probe the libc candidates for `flock(2)`.
77
+ *
78
+ * Fails loud, naming the platform and EVERY soname tried with what each one
79
+ * said: an operator needs to know which libc was looked for, not just that
80
+ * "something went wrong".
81
+ */
82
+ export async function resolveFlock(opener) {
83
+ const misses = [];
84
+ for (const name of LIBC_CANDIDATES) {
85
+ try {
86
+ return await opener(name);
87
+ }
88
+ catch (error) {
89
+ // A house error is a BACKEND-level failure, not a soname-level miss: the
90
+ // FFI itself could not be loaded, so no libc was ever opened. Folding it
91
+ // into the misses would name sonames that were never tried and bury the
92
+ // one fact that matters ("this runtime has no FFI backend") — so it goes
93
+ // out unchanged.
94
+ if (error instanceof WorkspaceError)
95
+ throw error;
96
+ misses.push(`${name}: ${error.message}`);
97
+ }
98
+ }
99
+ throw new WorkspaceError(`cannot load flock(2) for the workspace ownership lock on ${process.platform} ` +
100
+ `(tried ${LIBC_CANDIDATES.join(', ')}): ${misses.join('; ')}`);
101
+ }
102
+ /**
103
+ * The backend over a RESOLVED `flock(2)` symbol.
104
+ *
105
+ * The symbol is the libc one, so the unlock is an explicit `LOCK_UN` — and
106
+ * the descriptor is closed even when that call fails, because closing alone
107
+ * releases the flock and a descriptor left open would keep the kernel lock
108
+ * alive with no registry entry to reach it by.
109
+ */
110
+ export function symbolBackend(flock) {
111
+ return {
112
+ async tryLock(fd) {
113
+ return flock(fd, LOCK_EX | LOCK_NB) === 0;
114
+ },
115
+ release(fd) {
116
+ try {
117
+ flock(fd, LOCK_UN);
118
+ }
119
+ finally {
120
+ closeSync(fd);
121
+ }
122
+ },
123
+ };
124
+ }
125
+ /**
126
+ * The Bun-side backend: libc's `flock(2)`, through Bun's FFI.
127
+ *
128
+ * `bun:ffi` is a Bun-only module, and a STATIC import of it would make every
129
+ * installed copy die on Node with ERR_UNSUPPORTED_ESM_URL_SCHEME before it
130
+ * could print its own help — the published bin runs on the node floor
131
+ * package.json declares. So the import is dynamic ({@link loadFfi}), and only
132
+ * the runtime check that picks this backend ever reaches it.
133
+ */
134
+ async function bunFlockBackend() {
135
+ return symbolBackend(await resolveFlock(openFlockInLibc));
136
+ }
137
+ /** util-linux `flock(1)`'s exit code for "another process holds the lock". */
138
+ const FLOCK_CONFLICT_EXIT = 42;
139
+ /**
140
+ * The slot our descriptor rides in on the child's stdio.
141
+ *
142
+ * `flock(1)` locks THAT descriptor — the one this process already holds — so
143
+ * the lock lands on the open file description we keep: it stays ours after the
144
+ * child exits, every rival is refused by the kernel, and the kernel drops it
145
+ * when we close the descriptor or die.
146
+ */
147
+ const CHILD_FD = 3;
148
+ /** Run util-linux's `flock(1)` against our descriptor and read what it decided. */
149
+ function runFlockCli(fd, bin) {
150
+ return new Promise((resolve) => {
151
+ const child = spawn(bin, [
152
+ '--exclusive',
153
+ '--nonblock',
154
+ '--conflict-exit-code',
155
+ String(FLOCK_CONFLICT_EXIT),
156
+ String(CHILD_FD),
157
+ ], { stdio: ['ignore', 'ignore', 'pipe', fd] });
158
+ let stderr = '';
159
+ child.stderr?.on('data', (chunk) => {
160
+ stderr += chunk.toString();
161
+ });
162
+ child.on('error', (error) => {
163
+ resolve({
164
+ granted: false,
165
+ conflict: false,
166
+ detail: `cannot run ${bin} to take the workspace lock: ${error.message}`,
167
+ });
168
+ });
169
+ child.on('close', (code) => {
170
+ if (code === 0) {
171
+ resolve({ granted: true, conflict: false, detail: '' });
172
+ return;
173
+ }
174
+ if (code === FLOCK_CONFLICT_EXIT) {
175
+ resolve({ granted: false, conflict: true, detail: '' });
176
+ return;
177
+ }
178
+ resolve({
179
+ granted: false,
180
+ conflict: false,
181
+ detail: `${bin} could not take the workspace lock (exit ${code ?? 'none'}): ` +
182
+ (stderr.trim() || 'no output'),
183
+ });
184
+ });
185
+ });
186
+ }
187
+ /**
188
+ * `flock(2)` through util-linux's `flock(1)` — how Node reaches the syscall.
189
+ *
190
+ * Node has no FFI, so a short-lived child inherits our descriptor and locks
191
+ * it. The child exits at once; the lock stays ours because it lives on the
192
+ * open file description we still hold, and the kernel drops it when that
193
+ * descriptor closes or this process dies — a crashed owner never wedges the
194
+ * workspace.
195
+ *
196
+ * `bin` is a seam: the tests point it at a binary that does not exist to
197
+ * prove the failure is loud instead of a quiet "nobody holds this".
198
+ */
199
+ export async function nodeFlockBackend(bin = 'flock') {
200
+ return {
201
+ async tryLock(fd) {
202
+ const outcome = await runFlockCli(fd, bin);
203
+ if (outcome.granted)
204
+ return true;
205
+ if (outcome.conflict)
206
+ return false;
207
+ throw new WorkspaceError(`cannot take the workspace ownership lock on ${process.platform}: ${outcome.detail}`);
208
+ },
209
+ release(fd) {
210
+ // Closing IS the unlock here: the child is long gone, so ours is the
211
+ // last descriptor referring to the locked description, and the kernel
212
+ // drops the flock when it closes.
213
+ closeSync(fd);
214
+ },
215
+ };
216
+ }
217
+ /**
218
+ * The backend for THIS runtime, resolved fresh.
219
+ *
220
+ * Bun reaches libc through its own FFI; Node has none, so the same kernel
221
+ * lock comes from util-linux's `flock(1)`. The choice is the runtime's, made
222
+ * once — never a fallback: a platform with neither fails loud at first use
223
+ * rather than pretending the workspace is free.
224
+ */
225
+ function createBackend() {
226
+ return 'bun' in process.versions ? bunFlockBackend() : nodeFlockBackend();
227
+ }
228
+ /** The resolved backend of THIS runtime, cached once it is known to work. */
229
+ let backend;
230
+ export function loadBackend() {
231
+ backend ??= createBackend().catch((error) => {
232
+ // A failed resolution is not a verdict: the next attempt retries instead
233
+ // of replaying a dead one.
234
+ backend = undefined;
235
+ throw error;
236
+ });
237
+ return backend;
238
+ }
239
+ /**
240
+ * The gate's Node-side check: util-linux's `flock(1)` must be reachable.
241
+ *
242
+ * The Node backend reaches the kernel through that binary, so a host without
243
+ * it can never enforce the one-run rule — the same hole the libc probe closes
244
+ * on Bun. Nothing is locked and no file is touched: this only asks whether
245
+ * the binary is there.
246
+ */
247
+ function assertFlockCli(bin = 'flock') {
248
+ for (const dir of (process.env.PATH ?? '').split(delimiter)) {
249
+ if (dir === '')
250
+ continue;
251
+ try {
252
+ accessSync(join(dir, bin), fsConstants.X_OK);
253
+ return;
254
+ }
255
+ catch {
256
+ // not this directory — keep looking
257
+ }
258
+ }
259
+ throw new WorkspaceError(`cannot take the workspace ownership lock on ${process.platform}: ` +
260
+ `${bin}(1) is not on PATH — the workspace lock needs util-linux's flock`);
261
+ }
262
+ /**
263
+ * The BOOT GATE: resolve this host's `flock(2)` WITHOUT taking a lock.
264
+ *
265
+ * The workspace's one-run rule rests on a kernel-held lock. A host that cannot
266
+ * provide that backend can never enforce it — two servers would each believe
267
+ * they own the workspace — so the server refuses to come up instead. This
268
+ * runs BEFORE the workspace exists, before a config is read and before the MCP
269
+ * server starts.
270
+ *
271
+ * It RESOLVES the backend and nothing more: no file is opened, no `flock` is
272
+ * called, `<workspace>/owner.lock` is never created, and the acquire path's
273
+ * module cache is never written — an injected opener must not leak into the
274
+ * real one. A failed resolution is not cached either: the next attempt
275
+ * retries. A backend that failed outright (no FFI on this runtime) is
276
+ * reported as it is — never as a hunt through sonames it never opened.
277
+ *
278
+ * @param opener the libc seam to resolve through — the real one by default,
279
+ * injectable so a failed resolution is testable. An injected opener probes
280
+ * the libc candidates; without one, THIS runtime's own backend is resolved.
281
+ */
282
+ export async function assertOwnerLockBackend(opener) {
283
+ if (opener !== undefined) {
284
+ await resolveFlock(opener);
285
+ return;
286
+ }
287
+ if ('bun' in process.versions) {
288
+ await bunFlockBackend(); // resolves libc's `flock(2)` — takes no lock
289
+ return;
290
+ }
291
+ assertFlockCli(); // Node has no FFI: util-linux's `flock(1)` is the backend
292
+ }
293
+ //# sourceMappingURL=flock-backend.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flock-backend.js","sourceRoot":"","sources":["../../src/session/flock-backend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,IAAI,WAAW,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,qFAAqF;AACrF,MAAM,OAAO,GAAG,CAAC,CAAC;AAClB,MAAM,OAAO,GAAG,CAAC,CAAC;AAClB,MAAM,OAAO,GAAG,CAAC,CAAC;AA6BlB,0EAA0E;AAC1E,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,WAAW;IACX,uBAAuB;IACvB,wBAAwB;IACxB,YAAY;CACJ,CAAC;AAcX;;;;;;;;GAQG;AACH,IAAI,SAA+C,CAAC;AAEpD,KAAK,UAAU,OAAO;IACpB,IAAI,SAAS,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC9C,IAAI,MAAgC,CAAC;IACrC,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,0EAA0E;QAC1E,uEAAuE;QACvE,sDAAsD;QACtD,MAAM,IAAI,cAAc,CACtB,4DAA6D,KAAe,CAAC,OAAO,EAAE,C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,CAAC;QACzB,MAAM,YAAY,CAAC,MAAM,CAAC,CAAC;QAC3B,OAAO;IACT,CAAC;IACD,IAAI,KAAK,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QAC9B,MAAM,eAAe,EAAE,CAAC,CAAC,6CAA6C;QACtE,OAAO;IACT,CAAC;IACD,cAAc,EAAE,CAAC,CAAC,0DAA0D;AAC9E,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Live-workspace pointer — how `status` / `stop` find a run after the config moved.
3
+ *
4
+ * The server resolves its workspace from the config at boot; the CLI runs later,
5
+ * in another process, and re-derives it from the config AS IT IS NOW. Edit the
6
+ * `workspace` key while a run is live and the two disagree: `status` reads an
7
+ * empty workspace and `stop` cannot reach the server. `state.json` cannot fix
8
+ * that — the CLI would need the root to find it.
9
+ *
10
+ * So the server drops this pointer at a FIXED, config-independent location — the
11
+ * default workspace root for this cwd ({@link workspacePaths} with no base) — naming
12
+ * the workspace root it actually owns.
13
+ */
14
+ /** Where the pointer lives for `cwd` — derived from the cwd alone, never from config. */
15
+ export declare function livePointerPath(cwd: string): string;
16
+ /** Record `root` as the workspace the live server owns. Throws on a failed write (fail loud). */
17
+ export declare function writeLivePointer(cwd: string, root: string): Promise<void>;
18
+ /** The recorded live workspace root, or `null` when absent or malformed (never throws). */
19
+ export declare function readLivePointer(cwd: string): Promise<string | null>;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Live-workspace pointer — how `status` / `stop` find a run after the config moved.
3
+ *
4
+ * The server resolves its workspace from the config at boot; the CLI runs later,
5
+ * in another process, and re-derives it from the config AS IT IS NOW. Edit the
6
+ * `workspace` key while a run is live and the two disagree: `status` reads an
7
+ * empty workspace and `stop` cannot reach the server. `state.json` cannot fix
8
+ * that — the CLI would need the root to find it.
9
+ *
10
+ * So the server drops this pointer at a FIXED, config-independent location — the
11
+ * default workspace root for this cwd ({@link workspacePaths} with no base) — naming
12
+ * the workspace root it actually owns.
13
+ */
14
+ import { mkdir, readFile } from 'node:fs/promises';
15
+ import { dirname, join } from 'node:path';
16
+ import { z } from 'zod';
17
+ import { writeFileAtomic } from './atomic-write.js';
18
+ import { workspacePaths } from './workspace.js';
19
+ const LivePointerSchema = z.object({ root: z.string().min(1) });
20
+ /** Where the pointer lives for `cwd` — derived from the cwd alone, never from config. */
21
+ export function livePointerPath(cwd) {
22
+ return join(workspacePaths(cwd).root, 'live-workspace.json');
23
+ }
24
+ /** Record `root` as the workspace the live server owns. Throws on a failed write (fail loud). */
25
+ export async function writeLivePointer(cwd, root) {
26
+ const path = livePointerPath(cwd);
27
+ await mkdir(dirname(path), { recursive: true });
28
+ await writeFileAtomic(path, `${JSON.stringify({ root }, null, 2)}\n`);
29
+ }
30
+ /** The recorded live workspace root, or `null` when absent or malformed (never throws). */
31
+ export async function readLivePointer(cwd) {
32
+ try {
33
+ const raw = await readFile(livePointerPath(cwd), 'utf8');
34
+ const parsed = LivePointerSchema.safeParse(JSON.parse(raw));
35
+ return parsed.success ? parsed.data.root : null;
36
+ }
37
+ catch {
38
+ return null;
39
+ }
40
+ }
41
+ //# sourceMappingURL=live-pointer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-pointer.js","sourceRoot":"","sources":["../../src/session/live-pointer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AAEhE,yFAAyF;AACzF,MAAM,UAAU,eAAe,CAAC,GAAW;IACzC,OAAO,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,qBAAqB,CAAC,CAAC;AAC/D,CAAC;AAED,iGAAiG;AACjG,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,GAAW,EAAE,IAAY;IAC9D,MAAM,IAAI,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;IAClC,MAAM,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAChD,MAAM,eAAe,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC;AACxE,CAAC;AAED,2FAA2F;AAC3F,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAW;IAC/C,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC;QACzD,MAAM,MAAM,GAAG,iBAAiB,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QAC5D,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IAClD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -5,7 +5,11 @@
5
5
  * That project's persistent Chrome profile can only be driven by a single
6
6
  * browser at a time, so the manager is the in-process gate that holds the
7
7
  * profile lock: while a session is registered for a cwd, a second `start()`
8
- * fails loud with `SessionBusyError`. Ending the session frees the lock.
8
+ * fails loud with `SessionBusyError`. Ending frees the slot at once — a failed
9
+ * teardown must never wedge the project — but slot-free ≠ project-free: `end()`
10
+ * keeps a per-cwd *closing* gate shut until the teardown's `close()` settles
11
+ * (success OR failure), so a successor start never launches onto resources the
12
+ * ending run was still releasing.
9
13
  *
10
14
  * The manager is deliberately blind to what a session *is* — it only needs the
11
15
  * `ManagedSession` surface (an id + how to close it). The concrete session
@@ -37,6 +41,12 @@ export declare class SessionManager<S extends ManagedSession = ManagedSession> {
37
41
  get(cwd: string): S;
38
42
  /** Whether a session is currently active for `cwd` (non-throwing). */
39
43
  has(cwd: string): boolean;
44
+ /**
45
+ * The id of the session whose teardown is still in flight for `cwd`, or
46
+ * `undefined` once the close has settled (success or failure) and a start is
47
+ * allowed again. Refusals name it so the caller knows what they are waiting on.
48
+ */
49
+ closingId(cwd: string): string | undefined;
40
50
  /**
41
51
  * End the active session for `cwd`: free the profile lock, then close the session.
42
52
  * The slot is released *before* `close()` runs, so a failed teardown never wedges
@@ -5,7 +5,11 @@
5
5
  * That project's persistent Chrome profile can only be driven by a single
6
6
  * browser at a time, so the manager is the in-process gate that holds the
7
7
  * profile lock: while a session is registered for a cwd, a second `start()`
8
- * fails loud with `SessionBusyError`. Ending the session frees the lock.
8
+ * fails loud with `SessionBusyError`. Ending frees the slot at once — a failed
9
+ * teardown must never wedge the project — but slot-free ≠ project-free: `end()`
10
+ * keeps a per-cwd *closing* gate shut until the teardown's `close()` settles
11
+ * (success OR failure), so a successor start never launches onto resources the
12
+ * ending run was still releasing.
9
13
  *
10
14
  * The manager is deliberately blind to what a session *is* — it only needs the
11
15
  * `ManagedSession` surface (an id + how to close it). The concrete session
@@ -19,12 +23,25 @@ import { SessionBusyError, SessionNotFoundError } from '../errors.js';
19
23
  export class SessionManager {
20
24
  /** Active sessions keyed by absolute cwd. Presence of an entry = the project's profile is locked. */
21
25
  #sessions = new Map();
26
+ /**
27
+ * Sessions whose teardown is still in flight, keyed by absolute cwd — the
28
+ * delayed-teardown gate. The slot is freed before `close()` (see `end`), so
29
+ * this map is what tells a starting run "the previous one is STILL letting go".
30
+ */
31
+ #closing = new Map();
22
32
  /**
23
33
  * Register `session` as the active run for `cwd`, taking the project's profile lock.
24
34
  * @returns the same session, for ergonomic inline construction.
25
35
  * @throws {SessionBusyError} if a session is already active for this cwd.
26
36
  */
27
37
  start(cwd, session) {
38
+ // The closing gate is checked ahead of the slot: a teardown still in flight
39
+ // owns no slot, so the check below would sail straight past it.
40
+ const closing = this.#closing.get(cwd);
41
+ if (closing) {
42
+ throw new SessionBusyError(`The previous debug session ('${closing.id}') for '${cwd}' is still closing — its ` +
43
+ 'teardown is releasing the profile lock. Wait a moment, then retry.');
44
+ }
28
45
  const existing = this.#sessions.get(cwd);
29
46
  if (existing) {
30
47
  throw new SessionBusyError(`A debug session ('${existing.id}') is already active for '${cwd}'. ` +
@@ -48,6 +65,14 @@ export class SessionManager {
48
65
  has(cwd) {
49
66
  return this.#sessions.has(cwd);
50
67
  }
68
+ /**
69
+ * The id of the session whose teardown is still in flight for `cwd`, or
70
+ * `undefined` once the close has settled (success or failure) and a start is
71
+ * allowed again. Refusals name it so the caller knows what they are waiting on.
72
+ */
73
+ closingId(cwd) {
74
+ return this.#closing.get(cwd)?.id;
75
+ }
51
76
  /**
52
77
  * End the active session for `cwd`: free the profile lock, then close the session.
53
78
  * The slot is released *before* `close()` runs, so a failed teardown never wedges
@@ -60,7 +85,26 @@ export class SessionManager {
60
85
  throw new SessionNotFoundError(`No active debug session to end for '${cwd}'.`);
61
86
  }
62
87
  this.#sessions.delete(cwd);
63
- await session.close();
88
+ // The slot frees BEFORE the close (unchanged contract: a failed teardown must
89
+ // never wedge the project) — but the project is not free yet: the teardown is
90
+ // still releasing the browser/profile this cwd's runs share. Register the
91
+ // closing session before awaiting it, so a start landing mid-teardown is
92
+ // refused instead of launching onto resources the ending run still holds.
93
+ const closed = session.close();
94
+ const entry = { id: session.id, closed };
95
+ this.#closing.set(cwd, entry);
96
+ // Forget on settle — success OR failure, so a failed close reopens the gate
97
+ // too. Identity-checked: only OUR entry may lift it (an end→start→end cycle
98
+ // can have registered a newer closing session by the time this one settles).
99
+ const forget = () => {
100
+ if (this.#closing.get(cwd) === entry)
101
+ this.#closing.delete(cwd);
102
+ };
103
+ // Attached ahead of the await, so the gate is already open when the caller
104
+ // below sees the outcome.
105
+ closed.then(forget, forget);
106
+ // The close error still propagates to the caller.
107
+ await closed;
64
108
  }
65
109
  }
66
110
  //# sourceMappingURL=manager.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"manager.js","sourceRoot":"","sources":["../../src/session/manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAUtE;;;GAGG;AACH,MAAM,OAAO,cAAc;IACzB,qGAAqG;IAC5F,SAAS,GAAG,IAAI,GAAG,EAAa,CAAC;IAE1C;;;;OAIG;IACH,KAAK,CAAC,GAAW,EAAE,OAAU;QAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACzC,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,IAAI,gBAAgB,CACxB,qBAAqB,QAAQ,CAAC,EAAE,6BAA6B,GAAG,KAAK;gBACnE,iEAAiE,CACpE,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACjC,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,GAAW;QACb,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,oBAAoB,CAAC,gCAAgC,GAAG,IAAI,CAAC,CAAC;QAC1E,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,sEAAsE;IACtE,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAA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5E,4EAA4E;QAC5E,6EAA6E;QAC7E,MAAM,MAAM,GAAG,GAAS,EAAE;YACxB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,KAAK;gBAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAClE,CAAC,CAAC;QACF,2EAA2E;QAC3E,0BAA0B;QAC1B,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC5B,kDAAkD;QAClD,MAAM,MAAM,CAAC;IACf,CAAC;CACF"}
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Exclusive workspace ownership — a kernel-held lock.
3
+ *
4
+ * Exclusion is `flock(2)` (`LOCK_EX | LOCK_NB`) on `<workspace>/owner.lock`,
5
+ * held on an open fd for as long as this process owns the workspace. A live
6
+ * owner's exclusion is NON-REVOCABLE: no contender, and no recovery logic, can
7
+ * take it or remove it — the kernel refuses every other `flock` on the file —
8
+ * and the kernel releases it when the owner dies. Both halves replace the
9
+ * previous claim-file protocol, which published a link(2)'d claim and reclaimed
10
+ * a stale one with a verified rename-aside into a `.reclaiming-*` quarantine.
11
+ * That protocol's recovery could restore one live owner's claim over another
12
+ * (two stranded quarantines, resolved in readdir order, left the wrong holder
13
+ * at the path and admitted a second owner — PR #104). With the kernel as the
14
+ * referee there is no judgment about a previous holder's liveness, no reclaim,
15
+ * no hole, no quarantine, and nothing to recover: a dead holder is simply a
16
+ * lock nobody holds.
17
+ *
18
+ * `owner.lock` is a NOTICE BOARD, not the lock: the owner writes its claim
19
+ * (pid + process identity + session) under the lock, and a refused contender
20
+ * reads it to name the holder in its error. Two invariants keep the flock
21
+ * sound, and both are absolute:
22
+ *
23
+ * 1. The file is NEVER unlinked. `flock` binds to the inode; unlinking the
24
+ * path and letting a rival create a fresh file hands that rival a lock on a
25
+ * different inode while the old one is still held — two owners. Release
26
+ * empties the file and unlocks; the empty file itself is litter.
27
+ * 2. Only the process holding the lock ever writes or unlocks. A refused
28
+ * contender only ever reads.
29
+ *
30
+ * Re-entrancy is per process identity (pid + start ticks): `flock` treats each
31
+ * open file description as an independent contender — even two fds in ONE
32
+ * process contend — so the acquiring fd is kept in a registry keyed by lock
33
+ * path and reused. A second claim from this process with a DIFFERENT identity
34
+ * is refused like a foreign process: it opens its own fd and the kernel
35
+ * refuses it.
36
+ *
37
+ * Litter from the previous protocol (`.reclaiming-*` quarantines, a stray
38
+ * `owner.lock.reclaim` mutex, an empty or stale claim file) is inert: nothing
39
+ * reads or removes it. It cannot resurrect an owner — exclusion lives in the
40
+ * kernel, not in file contents.
41
+ *
42
+ * How this process reaches `flock(2)` (Bun FFI or util-linux `flock(1)`) lives
43
+ * in `flock-backend.ts`; its public surface is re-exported from here.
44
+ */
45
+ import { z } from 'zod';
46
+ import { SessionBusyError } from '../errors.js';
47
+ import { type FlockOpener } from './flock-backend.js';
48
+ import type { WorkspacePaths } from './workspace.js';
49
+ export { assertOwnerLockBackend, type FlockBackend, type FlockFn, type FlockOpener, LIBC_CANDIDATES, nodeFlockBackend, openFlockInLibc, } from './flock-backend.js';
50
+ /** The owner recorded in `<workspace>/owner.lock`. */
51
+ export declare const OwnerLockSchema: z.ZodObject<{
52
+ pid: z.ZodNumber;
53
+ identity: z.ZodObject<{
54
+ startTicks: z.ZodNullable<z.ZodNumber>;
55
+ }, z.core.$strip>;
56
+ sessionId: z.ZodString;
57
+ acquiredAt: z.ZodString;
58
+ }, z.core.$strip>;
59
+ export type OwnerLock = z.infer<typeof OwnerLockSchema>;
60
+ /** A live owner holds the lock. */
61
+ export declare class OwnerLockBusyError extends SessionBusyError {
62
+ }
63
+ /** `<workspace.root>/owner.lock` */
64
+ export declare function ownerLockPath(workspace: WorkspacePaths): string;
65
+ /**
66
+ * Acquire the exclusive workspace claim.
67
+ *
68
+ * The publish is one non-blocking `flock(2)`: the kernel either grants this
69
+ * process the workspace or refuses it — there is no read-then-act window, so
70
+ * no rival can slip between a judgment and the act, and no admission can be
71
+ * invalidated after the fact. A refused contender reports the holder named on
72
+ * the notice board.
73
+ *
74
+ * An existing claim held by this same process (same pid + start ticks) is
75
+ * re-entrant and reports `created: false`; any fresh take — an empty workspace
76
+ * or one whose previous holder died — reports `created: true`.
77
+ *
78
+ * @param opener the backend to resolve `flock(2)` through — the real one by
79
+ * default, injectable so a failed resolution is testable. An injected
80
+ * opener resolves fresh on every call and never enters the module cache.
81
+ */
82
+ export declare function acquireOwnerLock(lockPath: string, owner: OwnerLock, opener?: FlockOpener): Promise<{
83
+ acquired: true;
84
+ created: boolean;
85
+ } | {
86
+ acquired: false;
87
+ holder: OwnerLock;
88
+ }>;
89
+ /**
90
+ * The `flock(LOCK_UN)` on the held fd — injectable so a failing unlock is
91
+ * testable (the same seam doctrine as `emptyBoard`).
92
+ */
93
+ export type UnlockFn = (fd: number) => void | Promise<void>;
94
+ /**
95
+ * Release the claim only when this process still holds it.
96
+ *
97
+ * Returns `false` when this process holds nothing here (or a different owner
98
+ * does) — another owner's lock is never touched. The notice board is emptied,
99
+ * never unlinked: a stale claim file must never be mistaken for a held lock,
100
+ * and unlinking the path would put a fresh inode where the owner's lock lives.
101
+ *
102
+ * The lock ALWAYS comes home: the release runs in a `finally`, so a failed
103
+ * board write (a read-only remount, say) fails loud but cannot leave this
104
+ * process holding the kernel lock with no registry entry to reach it by —
105
+ * that would wedge every rival until the process exits (PR #104 review).
106
+ *
107
+ * @param emptyBoard the notice-board teardown, injectable for tests (defaults
108
+ * to truncating the file to nothing) — the same seam doctrine as `makeExec`'s
109
+ * `now`: a failure injected through the parameter never leaks into other
110
+ * test files the way a module mock does.
111
+ * @param unlock the `flock(LOCK_UN)` call, injectable for tests. Its failure
112
+ * must not keep the fd open: closing it alone releases the flock, and a fd
113
+ * left open would keep the kernel lock alive with no registry entry to
114
+ * reach it by. Without an injected unlock, the backend that took the lock
115
+ * gives it back — and disposes of the descriptor with it.
116
+ */
117
+ export declare function releaseOwnerLock(lockPath: string, owner: OwnerLock, emptyBoard?: (fd: number) => void, unlock?: UnlockFn): Promise<boolean>;