@spooky-sync/core 0.0.1-canary.208 → 0.0.1-canary.209

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.
@@ -128,13 +128,16 @@ export class WorkerSqliteTransport extends BaseTransport {
128
128
  this.handleMessage(ev.data);
129
129
  };
130
130
  // Surface a worker crash (wasm abort / OOM) instead of leaving every
131
- // pending call hung forever — reject them all with a clear error.
131
+ // pending call hung forever — reject them all with a clear error, AND
132
+ // close: `failAll` alone left `connected` true, so every later call was
133
+ // posted to the dead worker and parked in `pending` for good. Closed, the
134
+ // engine sees `!connected` and spawns a fresh worker on its next open.
132
135
  const crash = (msg: string) => {
133
136
  this.logger.error(
134
137
  { err: this.makeError(msg), Category: 'sp00ky-client::SqliteCacheEngine::worker' },
135
138
  'Worker error'
136
139
  );
137
- this.failAll(msg);
140
+ this.close(msg);
138
141
  };
139
142
  this.worker.onerror = (e: ErrorEvent) => crash(e.message || 'onerror');
140
143
  this.worker.onmessageerror = () => crash('messageerror');
@@ -47,6 +47,7 @@
47
47
  * hot path; the per-statement ops remain for the write/shim paths.
48
48
  */
49
49
  import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
50
+ import { verifyLockStillHeld as verifyLockStillHeldBounded } from './sqlite-lock-verify';
50
51
  import {
51
52
  openDb,
52
53
  type OpenDbOptions,
@@ -201,17 +202,16 @@ function noteTick(): void {
201
202
  }
202
203
  setInterval(noteTick, 1000);
203
204
 
204
- async function verifyLockStillHeld(): Promise<void> {
205
- const locks = workerLocks();
206
- const name = currentLock?.name;
207
- if (!locks || !name || typeof locks.query !== 'function') return;
208
- try {
209
- const state = await locks.query();
210
- const stillHeld = (state.held ?? []).some((l) => l.name === name);
211
- if (!stillHeld) await fence('lock missing after suspected freeze');
212
- } catch {
213
- /* query unavailable: fall back to the steal callback */
214
- }
205
+ // Bounded (see sqlite-lock-verify.ts): every op on the chain awaits this, and
206
+ // a `locks.query()` that never answered stopped every reply to every client.
207
+ function verifyLockStillHeld(): Promise<void> {
208
+ return verifyLockStillHeldBounded(
209
+ workerLocks() as any,
210
+ currentLock?.name,
211
+ (reason) => fence(reason),
212
+ undefined,
213
+ (msg) => console.warn(`[sqlite-worker] ${msg}`)
214
+ );
215
215
  }
216
216
 
217
217
  // ==================== db ops ====================
@@ -29,6 +29,14 @@ export interface StreamUpdate {
29
29
  queryHash: string;
30
30
  localArray: RecordVersionArray;
31
31
  op?: 'CREATE' | 'UPDATE' | 'DELETE'; // Operation type for conditional debouncing
32
+ /**
33
+ * Client-internal: not from the circuit. A membership-only change that
34
+ * needed no fetch is re-materialized through this same path so it cannot
35
+ * race a real update (DataModule.scheduleRematerialize). Carries the last
36
+ * known `localArray`; consumers that describe an INGEST (persist, metrics,
37
+ * devtools events) skip it.
38
+ */
39
+ synthetic?: boolean;
32
40
  /**
33
41
  * End-to-end ingest latency for the WASM call that produced this update,
34
42
  * in milliseconds. Populated by StreamProcessorService.ingest. Undefined
package/src/types.ts CHANGED
@@ -140,6 +140,16 @@ export interface Sp00kyConfig<S extends SchemaStructure> {
140
140
  * failure the queue retries. `0` disables. Defaults to `60_000`.
141
141
  */
142
142
  queryTimeoutMs?: number;
143
+ /**
144
+ * Deadline (ms) for every LOCAL store operation (a SQLite worker round trip,
145
+ * or a query on the in-process surrealdb engine). Local ops are serialized
146
+ * too, and one that never answered - a worker starved behind a long select,
147
+ * a lock verification with no clock - left every `db.create`/`db.update`
148
+ * promise pending for the tab's lifetime. On expiry the call rejects with
149
+ * `LocalOpTimeoutError` (the op itself keeps running in the engine and is
150
+ * not retried). `0` disables. Defaults to `30_000`.
151
+ */
152
+ localOpTimeoutMs?: number;
143
153
  };
144
154
  /** The schema definition. */
145
155
  schema: S;
@@ -176,6 +176,9 @@ export async function withRetry<T>(
176
176
  return await operation();
177
177
  } catch (err: any) {
178
178
  lastError = err;
179
+ // A deadline expiry is explicitly not retryable: the op is still running
180
+ // in the engine, and a retry only queues a second copy behind it.
181
+ if (err?.retryable === false) throw err;
179
182
  if (
180
183
  err?.message?.includes('Can not open transaction') ||
181
184
  err?.message?.includes('transaction') ||
@@ -220,11 +223,14 @@ export async function withRetry<T>(
220
223
  export function withTimeout<T>(
221
224
  promise: Promise<T>,
222
225
  timeoutMs: number,
223
- message: string
226
+ message: string | (() => Error)
224
227
  ): Promise<T> {
225
228
  if (!(timeoutMs > 0)) return promise;
226
229
  return new Promise<T>((resolve, reject) => {
227
- const timer = setTimeout(() => reject(new Error(message)), timeoutMs);
230
+ const timer = setTimeout(
231
+ () => reject(typeof message === 'function' ? message() : new Error(message)),
232
+ timeoutMs
233
+ );
228
234
  promise.then(
229
235
  (value) => {
230
236
  clearTimeout(timer);