@openclaw/fs-safe 0.8.0 → 0.8.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.8.1 - 2026-09-04
4
+
5
+ - Add `retainOnExit` to sidecar lock acquisition so deliberately retained ownership records (for example fail-closed build locks) survive natural process exit; default process-exit release behavior is unchanged.
6
+
3
7
  ## 0.8.0 - 2026-09-04
4
8
 
5
9
  ### Compatibility and upgrade notes
@@ -13,6 +13,7 @@ export type HeldSidecarLock = {
13
13
  metadata: Record<string, unknown>;
14
14
  releasePromise?: Promise<void>;
15
15
  lockRoot?: Root;
16
+ retainOnExit?: boolean;
16
17
  parsePayload?: (raw: string) => unknown;
17
18
  compromiseTimer?: NodeJS.Timeout;
18
19
  };
@@ -1 +1 @@
1
- {"version":3,"file":"sidecar-lock-acquire.d.ts","sourceRoot":"","sources":["../src/sidecar-lock-acquire.ts"],"names":[],"mappings":"AAOA,OAAO,EAA6B,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1F,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAS3C,OAAO,EAWL,KAAK,mBAAmB,EACzB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,yBAAyB,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAG5F,KAAK,iBAAiB,GAAG,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,MAAM,GAAG,WAAW,CAAC,CAAC;AAEhF,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,MAAM,EAAE,iBAAiB,CAAC;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,mBAAmB,CAAC;IAC9B,UAAU,EAAE,MAAM,CAAC;IAC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EAAE,6BAA6B,GACrC,OAAO,CAAC,iBAAiB,CAAC,CAqT5B"}
@@ -53,6 +53,11 @@ export async function acquireSidecarLock(options, context) {
53
53
  held.reentrantOwner !== undefined &&
54
54
  options.reentrantOwner === held.reentrantOwner) {
55
55
  held.refCount += 1;
56
+ // Retention is monotonic: any same-owner request to keep the sidecar on
57
+ // exit upgrades the held lock; a later default acquisition never revokes it.
58
+ if (options.retainOnExit === true) {
59
+ held.retainOnExit = true;
60
+ }
56
61
  return context.handleForHeldLock(normalizedTargetPath, held);
57
62
  }
58
63
  }
@@ -170,6 +175,7 @@ export async function acquireSidecarLock(options, context) {
170
175
  acquiredAt: Date.now(),
171
176
  metadata: options.metadata ?? {},
172
177
  lockRoot: options.lockRoot,
178
+ retainOnExit: options.retainOnExit,
173
179
  parsePayload: options.parsePayload,
174
180
  };
175
181
  context.held.set(normalizedTargetPath, createdHeld);
@@ -33,6 +33,13 @@ export type SidecarLockAcquireOptions<TPayload extends Record<string, unknown>>
33
33
  metadata?: Record<string, unknown>;
34
34
  parsePayload?: (raw: string) => unknown;
35
35
  lockRoot?: Root;
36
+ /**
37
+ * Keep the lock file when the process exits naturally. Default `false`:
38
+ * process-exit handlers release held locks. Set only for deliberately
39
+ * retained ownership records (for example, fail-closed build locks) whose
40
+ * liveness is governed by the caller's own stale policy.
41
+ */
42
+ retainOnExit?: boolean;
36
43
  onCompromised?: (info: SidecarLockCompromisedInfo) => void;
37
44
  compromiseCheckIntervalMs?: number;
38
45
  };
@@ -1 +1 @@
1
- {"version":3,"file":"sidecar-lock-types.d.ts","sourceRoot":"","sources":["../src/sidecar-lock-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAE1E,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAA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ChB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,0BAA0B,KAAK,IAAI,CAAC;IAC3D,yBAAyB,CAAC,EAAE,MAAM,CAAC;CACpC,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,eAAe,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IACxC,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,YAAY,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,sBAAsB,CAAC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,IAAI,CACjF,yBAAyB,CAAC,QAAQ,CAAC,EACnC,YAAY,CACb,GAAG;IACF,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC"}
@@ -1,6 +1,8 @@
1
1
  import type { SidecarLockAcquireOptions, SidecarLockHandle, SidecarLockHeldEntry, WithSidecarLockOptions } from "./sidecar-lock-types.js";
2
2
  export type { SidecarLockStaleSnapshot } from "./sidecar-lock-reclaim.js";
3
3
  export type { SidecarLockAcquireOptions, SidecarLockCompromisedInfo, SidecarLockHandle, SidecarLockHeldEntry, SidecarLockRetryOptions, SidecarLockStaleRecovery, WithSidecarLockOptions, } from "./sidecar-lock-types.js";
4
+ /** True when a retain-unaware package copy registered the process-exit handlers first. */
5
+ export declare function exitCleanupCannotRetain(): boolean;
4
6
  export declare function createSidecarLockManager(key: string): {
5
7
  acquire: <TPayload extends Record<string, unknown>>(options: SidecarLockAcquireOptions<TPayload>) => Promise<SidecarLockHandle>;
6
8
  withLock: <T, TPayload extends Record<string, unknown>>(options: SidecarLockAcquireOptions<TPayload>, fn: () => Promise<T>) => Promise<T>;
@@ -1 +1 @@
1
- {"version":3,"file":"sidecar-lock.d.ts","sourceRoot":"","sources":["../src/sidecar-lock.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACV,yBAAyB,EACzB,iBAAiB,EACjB,oBAAoB,EACpB,sBAAsB,EACvB,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,YAAY,EACV,yBAAyB,EACzB,0BAA0B,EAC1B,iBAAiB,EACjB,oBAAoB,EACpB,uBAAuB,EACvB,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,yBAAyB,CAAC;AAmNjC,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM;cAU3B,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WACpD,yBAAyB,CAAC,QAAQ,CAAC,KAC3C,OAAO,CAAC,iBAAiB,CAAC;eAYL,CAAC,EAAE,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WACxD,yBAAyB,CAAC,QAAQ,CAAC,MACxC,MAAM,OAAO,CAAC,CAAC,CAAC,KACnB,OAAO,CAAC,CAAC,CAAC;iBAqBW,OAAO,CAAC,IAAI,CAAC;iBAQnB,IAAI;uBAIE,oBAAoB,EAAE;EAW/C;AAED,wBAAsB,eAAe,CAAC,CAAC,EAAE,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/E,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,sBAAsB,CAAC,QAAQ,CAAC,EACzC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,CAAC,CAAC,CAMZ"}
1
+ {"version":3,"file":"sidecar-lock.d.ts","sourceRoot":"","sources":["../src/sidecar-lock.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,yBAAyB,EACzB,iBAAiB,EACjB,oBAAoB,EACpB,sBAAsB,EACvB,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,YAAY,EACV,yBAAyB,EACzB,0BAA0B,EAC1B,iBAAiB,EACjB,oBAAoB,EACpB,uBAAuB,EACvB,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,yBAAyB,CAAC;AAqIjC,0FAA0F;AAC1F,wBAAgB,uBAAuB,IAAI,OAAO,CAMjD;AAuFD,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM;cAU3B,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WACpD,yBAAyB,CAAC,QAAQ,CAAC,KAC3C,OAAO,CAAC,iBAAiB,CAAC;eAkBL,CAAC,EAAE,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WACxD,yBAAyB,CAAC,QAAQ,CAAC,MACxC,MAAM,OAAO,CAAC,CAAC,CAAC,KACnB,OAAO,CAAC,CAAC,CAAC;iBAqBW,OAAO,CAAC,IAAI,CAAC;iBAQnB,IAAI;uBAIE,oBAAoB,EAAE;EAW/C;AAED,wBAAsB,eAAe,CAAC,CAAC,EAAE,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/E,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,sBAAsB,CAAC,QAAQ,CAAC,EACzC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,CAAC,CAAC,CAMZ"}
@@ -1,4 +1,5 @@
1
1
  import fsSync from "node:fs";
2
+ import { FsSafeError } from "./errors.js";
2
3
  import { sameFileIdentity } from "./file-identity.js";
3
4
  import { removeSidecarLockIfUnchanged, sidecarLockSnapshotMatches, } from "./sidecar-lock-reclaim.js";
4
5
  import { acquireSidecarLock } from "./sidecar-lock-acquire.js";
@@ -8,6 +9,10 @@ const GLOBAL_STATE_KEY = Symbol.for("fsSafe.sidecarLockManagers");
8
9
  const GLOBAL_CLEANUP_KEY = Symbol.for("fsSafe.sidecarLockCleanupRegistered");
9
10
  const GLOBAL_CLEANUP_HANDLER_KEY = Symbol.for("fsSafe.sidecarLockCleanupHandler");
10
11
  const GLOBAL_BEFORE_EXIT_KEY = Symbol.for("fsSafe.sidecarLockBeforeExitCleanup");
12
+ // Set by copies whose exit handlers honor retainOnExit. When an older package
13
+ // copy registered the handlers first, this marker stays absent and retained
14
+ // acquisitions must fail closed rather than silently lose the guarantee.
15
+ const GLOBAL_RETAIN_AWARE_KEY = Symbol.for("fsSafe.sidecarLockRetainAwareCleanup");
11
16
  function getGlobalManagers() {
12
17
  const globalWithState = globalThis;
13
18
  if (!globalWithState[GLOBAL_STATE_KEY]) {
@@ -90,11 +95,12 @@ function releaseAllReclaimGuardsSync(state) {
90
95
  }
91
96
  }
92
97
  }
93
- function releaseAllLocksSync(state) {
98
+ function releaseAllLocksSync(state, options) {
94
99
  for (const [normalizedTargetPath, held] of state.held) {
95
100
  void held.handle.close().catch(() => undefined);
96
101
  try {
97
- if (!held.lockRoot && snapshotMatchesSync(held.lockPath, held.snapshot)) {
102
+ const retained = options?.preserveRetained === true && held.retainOnExit;
103
+ if (!retained && !held.lockRoot && snapshotMatchesSync(held.lockPath, held.snapshot)) {
98
104
  fsSync.rmSync(held.lockPath, { force: true });
99
105
  }
100
106
  }
@@ -110,14 +116,20 @@ function ensureGlobalExitCleanupRegistered() {
110
116
  if (globalWithCleanup[GLOBAL_CLEANUP_KEY])
111
117
  return;
112
118
  globalWithCleanup[GLOBAL_CLEANUP_KEY] = true;
119
+ globalWithCleanup[GLOBAL_RETAIN_AWARE_KEY] = true;
113
120
  const cleanup = () => {
114
121
  for (const state of getGlobalManagers().values()) {
115
- releaseAllLocksSync(state);
122
+ releaseAllLocksSync(state, { preserveRetained: true });
116
123
  }
117
124
  };
118
125
  globalWithCleanup[GLOBAL_CLEANUP_HANDLER_KEY] = cleanup;
119
126
  process.on("exit", cleanup);
120
127
  }
128
+ /** True when a retain-unaware package copy registered the process-exit handlers first. */
129
+ export function exitCleanupCannotRetain() {
130
+ const globalWithCleanup = globalThis;
131
+ return globalWithCleanup[GLOBAL_CLEANUP_KEY] === true && globalWithCleanup[GLOBAL_RETAIN_AWARE_KEY] !== true;
132
+ }
121
133
  function ensureGlobalBeforeExitCleanupRegistered() {
122
134
  const globalWithCleanup = globalThis;
123
135
  if (globalWithCleanup[GLOBAL_BEFORE_EXIT_KEY]) {
@@ -133,7 +145,7 @@ function ensureGlobalBeforeExitCleanupRegistered() {
133
145
  lifecycle.armed = false;
134
146
  for (const state of getGlobalManagers().values()) {
135
147
  for (const [normalizedTargetPath, held] of Array.from(state.held.entries())) {
136
- if (held.lockRoot) {
148
+ if (held.lockRoot && !held.retainOnExit) {
137
149
  void releaseHeldLock(state, normalizedTargetPath, held, { force: true }).catch(() => undefined);
138
150
  }
139
151
  }
@@ -198,6 +210,9 @@ export function createSidecarLockManager(key) {
198
210
  ensureGlobalBeforeExitCleanupRegistered().armed = true;
199
211
  }
200
212
  async function acquire(options) {
213
+ if (options.retainOnExit === true && exitCleanupCannotRetain()) {
214
+ throw new FsSafeError("helper-unavailable", "retainOnExit requires this process's exit handlers to be retain-aware; an older package copy registered them first");
215
+ }
201
216
  return await acquireSidecarLock(options, {
202
217
  held: state.held,
203
218
  reclaimGuards: state.reclaimGuards,
@@ -21,7 +21,7 @@ try {
21
21
 
22
22
  The lock file sits next to the protected resource. If a process crashes mid-lock, the next acquirer notices the held entry, inspects its payload (PID, host, acquired-at timestamp), and decides — via `shouldReclaim` (defaulting to "is the lock older than `staleMs`?") — whether it should keep waiting or fail.
23
23
 
24
- On natural event-loop shutdown, a globally deduplicated `process.on("beforeExit")` handler attempts asynchronous cleanup of held Root-backed locks through their retained Root capability and ownership receipt. The synchronous `process.on("exit")` handler provides last-chance cleanup for raw locks and reclaim guards. Changed sidecars and failed Root cleanup remain in place; cleanup does not keep retrying during shutdown unless another acquisition re-arms it.
24
+ On natural event-loop shutdown, a globally deduplicated `process.on("beforeExit")` handler attempts asynchronous cleanup of held Root-backed locks through their retained Root capability and ownership receipt. The synchronous `process.on("exit")` handler provides last-chance cleanup for raw locks and reclaim guards. Changed sidecars and failed Root cleanup remain in place; cleanup does not keep retrying during shutdown unless another acquisition re-arms it. Locks acquired with `retainOnExit: true` are exempt from both handlers: their sidecar stays in place after exit and is governed only by the caller's own stale policy. Because exit handlers are globally deduplicated across package copies, `retainOnExit` fails closed with `helper-unavailable` if an older copy that cannot honor it registered the handlers first.
25
25
 
26
26
  Always release locks in a `finally` block. Application-managed graceful shutdown can await `release()` or `manager.drain()` before terminating. Explicit `process.exit()`, uncaught failures, crashes, default signal handling, and fatal termination (including `SIGKILL`) do not reliably run asynchronous Root cleanup and may leave sidecars. Recover only after an application-owned liveness policy proves the holder cannot still be writing.
27
27
 
@@ -82,6 +82,7 @@ type FileLockAcquireOptions<TPayload extends Record<string, unknown>> = {
82
82
  metadata?: Record<string, unknown>; // attached to heldEntries() output for diagnostics
83
83
  parsePayload?: (raw: string) => unknown;
84
84
  lockRoot?: Root;
85
+ retainOnExit?: boolean; // keep the sidecar across process exit (default false)
85
86
  onCompromised?: (info: { lockPath: string; normalizedTargetPath: string }) => void;
86
87
  compromiseCheckIntervalMs?: number;
87
88
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openclaw/fs-safe",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Capability-style filesystem roots for Node.js apps that handle untrusted relative paths.",
5
5
  "keywords": [
6
6
  "filesystem",
@@ -153,13 +153,13 @@
153
153
  "crabbox:warmup": "crabbox warmup"
154
154
  },
155
155
  "optionalDependencies": {
156
- "@openclaw/fs-safe-darwin-arm64": "0.8.0",
157
- "@openclaw/fs-safe-darwin-x64": "0.8.0",
158
- "@openclaw/fs-safe-linux-arm64-gnu": "0.8.0",
159
- "@openclaw/fs-safe-linux-arm64-musl": "0.8.0",
160
- "@openclaw/fs-safe-linux-x64-gnu": "0.8.0",
161
- "@openclaw/fs-safe-linux-x64-musl": "0.8.0",
162
- "@openclaw/fs-safe-win32-x64-msvc": "0.8.0",
156
+ "@openclaw/fs-safe-darwin-arm64": "0.8.1",
157
+ "@openclaw/fs-safe-darwin-x64": "0.8.1",
158
+ "@openclaw/fs-safe-linux-arm64-gnu": "0.8.1",
159
+ "@openclaw/fs-safe-linux-arm64-musl": "0.8.1",
160
+ "@openclaw/fs-safe-linux-x64-gnu": "0.8.1",
161
+ "@openclaw/fs-safe-linux-x64-musl": "0.8.1",
162
+ "@openclaw/fs-safe-win32-x64-msvc": "0.8.1",
163
163
  "jszip": "^3.10.1",
164
164
  "tar": "7.5.22"
165
165
  },