clawmem 0.10.7 → 0.11.2

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.
@@ -114,6 +114,40 @@ export function releaseWorkerLease(
114
114
  return result.changes > 0;
115
115
  }
116
116
 
117
+ /**
118
+ * Extend a lease's expiry if the caller's token still owns it. Returns `true`
119
+ * if THIS holder still owns the lease and the TTL was extended; `false` if the
120
+ * row is gone or was reclaimed by another token (we lost the lease). A long
121
+ * embed run renews on a heartbeat well inside its TTL; on a `false` result the
122
+ * caller MUST abort, because another process now holds the lease and could run
123
+ * concurrently. Token-fenced like releaseWorkerLease (the UPDATE matches only
124
+ * our own (worker_name, lease_token) row); any DB error → `false` (fail-safe:
125
+ * a renewal failure is treated as lease-lost → abort).
126
+ */
127
+ export function renewWorkerLease(
128
+ store: Store,
129
+ workerName: string,
130
+ token: string,
131
+ ttlMs: number,
132
+ now: Date = new Date(),
133
+ ): boolean {
134
+ if (ttlMs <= 0) {
135
+ throw new Error(`renewWorkerLease: ttlMs must be positive, got ${ttlMs}`);
136
+ }
137
+ try {
138
+ const result = store.db.prepare(
139
+ `UPDATE worker_leases SET acquired_at = ?, expires_at = ?
140
+ WHERE worker_name = ? AND lease_token = ?`,
141
+ ).run(nowIso(now), futureIso(now, ttlMs), workerName, token);
142
+ return result.changes > 0;
143
+ } catch (err) {
144
+ console.error(
145
+ `[worker-lease] renew error for ${workerName}: ${(err as Error).message}`,
146
+ );
147
+ return false;
148
+ }
149
+ }
150
+
117
151
  /**
118
152
  * Run `fn` under an exclusive lease on `workerName`. If the lease cannot
119
153
  * be acquired, returns `{acquired: false}` without invoking `fn`. The