cursedbelt 2.6.1 → 2.7.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 (65) hide show
  1. package/dist/server/sync/alarm.d.ts +35 -0
  2. package/dist/server/sync/alarm.d.ts.map +1 -0
  3. package/dist/server/sync/alarm.js +92 -0
  4. package/dist/server/sync/alarm.js.map +1 -0
  5. package/dist/server/sync/commands.d.ts +88 -0
  6. package/dist/server/sync/commands.d.ts.map +1 -0
  7. package/dist/server/sync/commands.js +242 -0
  8. package/dist/server/sync/commands.js.map +1 -0
  9. package/dist/server/sync/engine.d.ts +63 -0
  10. package/dist/server/sync/engine.d.ts.map +1 -0
  11. package/dist/server/sync/engine.js +185 -0
  12. package/dist/server/sync/engine.js.map +1 -0
  13. package/dist/server/sync/http.d.ts +107 -0
  14. package/dist/server/sync/http.d.ts.map +1 -0
  15. package/dist/server/sync/http.js +244 -0
  16. package/dist/server/sync/http.js.map +1 -0
  17. package/dist/server/sync/index.d.ts +42 -0
  18. package/dist/server/sync/index.d.ts.map +1 -0
  19. package/dist/server/sync/index.js +42 -0
  20. package/dist/server/sync/index.js.map +1 -0
  21. package/dist/server/sync/opLog.d.ts +62 -0
  22. package/dist/server/sync/opLog.d.ts.map +1 -0
  23. package/dist/server/sync/opLog.js +97 -0
  24. package/dist/server/sync/opLog.js.map +1 -0
  25. package/dist/server/sync/planner.d.ts +32 -0
  26. package/dist/server/sync/planner.d.ts.map +1 -0
  27. package/dist/server/sync/planner.js +31 -0
  28. package/dist/server/sync/planner.js.map +1 -0
  29. package/dist/server/sync/status.d.ts +64 -0
  30. package/dist/server/sync/status.d.ts.map +1 -0
  31. package/dist/server/sync/status.js +50 -0
  32. package/dist/server/sync/status.js.map +1 -0
  33. package/dist/server/sync/timer.d.ts +53 -0
  34. package/dist/server/sync/timer.d.ts.map +1 -0
  35. package/dist/server/sync/timer.js +171 -0
  36. package/dist/server/sync/timer.js.map +1 -0
  37. package/dist/server/sync/tokens.d.ts +26 -0
  38. package/dist/server/sync/tokens.d.ts.map +1 -0
  39. package/dist/server/sync/tokens.js +52 -0
  40. package/dist/server/sync/tokens.js.map +1 -0
  41. package/dist/server/sync/types.d.ts +102 -0
  42. package/dist/server/sync/types.d.ts.map +1 -0
  43. package/dist/server/sync/types.js +30 -0
  44. package/dist/server/sync/types.js.map +1 -0
  45. package/package.json +19 -13
  46. package/src/server/sync/alarm.spec.ts +149 -0
  47. package/src/server/sync/alarm.ts +137 -0
  48. package/src/server/sync/commands.spec.ts +145 -0
  49. package/src/server/sync/commands.ts +361 -0
  50. package/src/server/sync/engine.spec.ts +496 -0
  51. package/src/server/sync/engine.ts +255 -0
  52. package/src/server/sync/http.ts +316 -0
  53. package/src/server/sync/httpRemote.spec.ts +158 -0
  54. package/src/server/sync/index.ts +90 -0
  55. package/src/server/sync/opLog.spec.ts +110 -0
  56. package/src/server/sync/opLog.ts +189 -0
  57. package/src/server/sync/planner.spec.ts +53 -0
  58. package/src/server/sync/planner.ts +62 -0
  59. package/src/server/sync/status.spec.ts +92 -0
  60. package/src/server/sync/status.ts +108 -0
  61. package/src/server/sync/timer.spec.ts +150 -0
  62. package/src/server/sync/timer.ts +207 -0
  63. package/src/server/sync/tokens.spec.ts +38 -0
  64. package/src/server/sync/tokens.ts +94 -0
  65. package/src/server/sync/types.ts +108 -0
@@ -0,0 +1,35 @@
1
+ /**
2
+ * 🔴 `error` joined on 2026-08-21, and it is the outcome that was MISSING.
3
+ *
4
+ * The console's subdomain cutover left the Mac's `STATION_SYNC_URL` pointing at
5
+ * a host that had become a 301; the dialer followed it into a Cloudflare Access
6
+ * login page and threw `Failed to parse JSON` every round. That is neither
7
+ * `paused` (nothing is version-skewed) nor `auth` (no token was refused) nor
8
+ * `unreachable` (the peer answered), so it recorded NOTHING — the timer logged
9
+ * `[sync] failed (ignored)` and the alarm's counter sat at zero while every
10
+ * mirrored report on prod froze for days, each still rendering a confident age.
11
+ *
12
+ * A sustained hard error is exactly as bad as a sustained pause and is now
13
+ * counted the same. `unreachable` remains the one outcome that neither counts
14
+ * nor resets, because the nightly EC2 sleep must never false-alarm.
15
+ */
16
+ export type AlarmOutcome = "ok" | "paused" | "auth" | "error" | "unreachable";
17
+ export interface SyncAlarmOptions {
18
+ /** Where the counter lives (survives restarts). */
19
+ statePath: string;
20
+ /** Rounds of paused/auth before raising (with the round cadence, pick so the
21
+ * threshold lands around ~45 min of sustained pause). */
22
+ threshold?: number;
23
+ /** Re-raise cadence while the condition persists. Default 4h. */
24
+ reRaiseMs?: number;
25
+ /** Present the alarm. Default: macOS notification via osascript. */
26
+ notify?: (title: string, body: string) => void;
27
+ now?: () => number;
28
+ appName?: string;
29
+ }
30
+ export interface SyncAlarm {
31
+ /** Record one sync round's outcome; raises when the threshold crosses. */
32
+ record(outcome: AlarmOutcome, reason?: string): void;
33
+ }
34
+ export declare function createSyncAlarm(options: SyncAlarmOptions): SyncAlarm;
35
+ //# sourceMappingURL=alarm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alarm.d.ts","sourceRoot":"","sources":["../../../src/server/sync/alarm.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,aAAa,CAAC;AAgB9E,MAAM,WAAW,gBAAgB;IAChC,mDAAmD;IACnD,SAAS,EAAE,MAAM,CAAC;IAClB;8DAC0D;IAC1D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oEAAoE;IACpE,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/C,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,SAAS;IACzB,0EAA0E;IAC1E,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACrD;AAeD,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CA4DpE"}
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Pause-alarm escalation — notes' `syncAlarm` generalized. A sync that is version-
3
+ * skew paused or auth-refused for a sustained stretch is a real problem the owner
4
+ * must hear about (the 2026-07-31 notes incident: prod ran stale code most of a day
5
+ * and only an owner eyeballing counts caught it). An UNREACHABLE peer neither counts
6
+ * nor resets — the nightly EC2 sleep must never false-alarm.
7
+ *
8
+ * State is a small JSON file so the alarm survives process restarts; the notifier is
9
+ * a seam (macOS `osascript` by default) so tests and non-Mac hosts inject their own.
10
+ */
11
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
12
+ import { dirname } from "node:path";
13
+ const EMPTY = {
14
+ consecutiveBad: 0,
15
+ firstBadAt: null,
16
+ lastRaisedAt: null,
17
+ lastReason: null,
18
+ };
19
+ function defaultNotify(title, body) {
20
+ try {
21
+ const esc = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
22
+ Bun.spawnSync([
23
+ "osascript",
24
+ "-e",
25
+ `display notification "${esc(body)}" with title "${esc(title)}"`,
26
+ ]);
27
+ }
28
+ catch {
29
+ // A notifier must never take the loop down.
30
+ }
31
+ }
32
+ export function createSyncAlarm(options) {
33
+ const threshold = options.threshold ?? 9;
34
+ const reRaiseMs = options.reRaiseMs ?? 4 * 60 * 60_000;
35
+ const notify = options.notify ?? defaultNotify;
36
+ const now = options.now ?? Date.now;
37
+ const appName = options.appName ?? "sync";
38
+ const load = () => {
39
+ try {
40
+ if (!existsSync(options.statePath))
41
+ return { ...EMPTY };
42
+ return {
43
+ ...EMPTY,
44
+ ...JSON.parse(readFileSync(options.statePath, "utf8")),
45
+ };
46
+ }
47
+ catch {
48
+ return { ...EMPTY };
49
+ }
50
+ };
51
+ const save = (state) => {
52
+ try {
53
+ mkdirSync(dirname(options.statePath), { recursive: true });
54
+ writeFileSync(options.statePath, JSON.stringify(state));
55
+ }
56
+ catch {
57
+ // Losing the counter degrades to a later alarm, never to a crash.
58
+ }
59
+ };
60
+ return {
61
+ record(outcome, reason) {
62
+ const state = load();
63
+ if (outcome === "ok") {
64
+ save({ ...EMPTY });
65
+ return;
66
+ }
67
+ if (outcome === "unreachable")
68
+ return; // neither counts nor resets
69
+ state.consecutiveBad += 1;
70
+ state.lastReason = reason ?? outcome;
71
+ if (state.firstBadAt === null)
72
+ state.firstBadAt = now();
73
+ const due = state.consecutiveBad >= threshold &&
74
+ (state.lastRaisedAt === null || now() - state.lastRaisedAt >= reRaiseMs);
75
+ if (due) {
76
+ state.lastRaisedAt = now();
77
+ const since = state.firstBadAt
78
+ ? new Date(state.firstBadAt).toLocaleTimeString()
79
+ : "a while";
80
+ notify(outcome === "error" ? `${appName}: sync is FAILING` : `${appName}: sync is paused`, outcome === "auth"
81
+ ? `The sync token is being refused (since ${since}). Re-link the peer.`
82
+ : outcome === "error"
83
+ ? // Names the real error, because this alarm's whole reason for
84
+ // existing is that "failed (ignored)" was invisible.
85
+ `Every sync round has failed since ${since}: ${state.lastReason}`
86
+ : `Push has been paused on "${state.lastReason}" since ${since} — the peer likely needs a deploy.`);
87
+ }
88
+ save(state);
89
+ },
90
+ };
91
+ }
92
+ //# sourceMappingURL=alarm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alarm.js","sourceRoot":"","sources":["../../../src/server/sync/alarm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AA0BpC,MAAM,KAAK,GAAe;IACzB,cAAc,EAAE,CAAC;IACjB,UAAU,EAAE,IAAI;IAChB,YAAY,EAAE,IAAI;IA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GAAG,OAAO,mBAAmB,CAAC,CAAC,CAAC,GAAG,OAAO,kBAAkB,EAClF,OAAO,KAAK,MAAM;oBACjB,CAAC,CAAC,0CAA0C,KAAK,sBAAsB;oBACvE,CAAC,CAAC,OAAO,KAAK,OAAO;wBACpB,CAAC,CAAC,8DAA8D;4BAC/D,qDAAqD;4BACrD,qCAAqC,KAAK,KAAK,KAAK,CAAC,UAAU,EAAE;wBAClE,CAAC,CAAC,4BAA4B,KAAK,CAAC,UAAU,WAAW,KAAK,oCAAoC,CACpG,CAAC;YACH,CAAC;YACD,IAAI,CAAC,KAAK,CAAC,CAAC;QACb,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * The command relay — notes' ask-responder shape generalized: the internet-facing
3
+ * instance PARKS an action it cannot perform (it has no Mac powers), and the Mac's
4
+ * resident worker long-polls for it, executes locally, and posts the outcome back.
5
+ * The Mac dials OUT; the receiving instance never reaches into the house.
6
+ *
7
+ * Delivery is a held GET (default hold 25s) — remember the two fleet traps: the
8
+ * host server needs `idleTimeoutSeconds` ABOVE the hold (Bun's 10s default closed
9
+ * notes' 25s poll and nginx reported a bogus 502), and the routes must be mounted
10
+ * BEFORE any browser-session gate.
11
+ *
12
+ * A command left `claimed` past the stale window is reclaimable — a worker that
13
+ * died mid-execution must not strand the queue. Executors must therefore be
14
+ * idempotent or cheap to re-run.
15
+ */
16
+ import type { Database } from "bun:sqlite";
17
+ import { Hono } from "hono";
18
+ export type CommandStatus = "queued" | "claimed" | "done" | "failed";
19
+ export interface SyncCommand {
20
+ uid: string;
21
+ kind: string;
22
+ args: unknown;
23
+ status: CommandStatus;
24
+ requestedBy: string;
25
+ createdAt: number;
26
+ claimedAt: number | null;
27
+ claimedBy: string | null;
28
+ finishedAt: number | null;
29
+ result: unknown;
30
+ error: string | null;
31
+ }
32
+ export interface CommandQueue {
33
+ enqueue(kind: string, args: unknown, requestedBy: string): SyncCommand;
34
+ /** Atomically claim the oldest queued (or stale-claimed) command. */
35
+ claimNext(claimedBy: string, staleMs?: number): SyncCommand | null;
36
+ complete(uid: string, result: unknown): boolean;
37
+ fail(uid: string, error: string): boolean;
38
+ get(uid: string): SyncCommand | null;
39
+ list(limit?: number): SyncCommand[];
40
+ pendingCount(): number;
41
+ }
42
+ export declare function createCommandQueue(db: Database): CommandQueue;
43
+ export declare const DEFAULT_HOLD_MS = 25000;
44
+ export interface CommandRoutesOptions {
45
+ queue: CommandQueue;
46
+ verifyToken: (presented: string) => boolean;
47
+ /** Cap on the held poll. The HOST SERVER's idleTimeout must exceed this. */
48
+ maxHoldMs?: number;
49
+ pollStepMs?: number;
50
+ }
51
+ /** GET /pending (held) + POST /pending/:uid — the worker's two verbs. */
52
+ export declare function createCommandReceiver(options: CommandRoutesOptions): Hono;
53
+ export interface CommandRemote {
54
+ poll(worker: string, waitMs: number): Promise<SyncCommand | null>;
55
+ report(uid: string, outcome: {
56
+ ok: boolean;
57
+ result?: unknown;
58
+ error?: string;
59
+ }): Promise<void>;
60
+ }
61
+ export declare function createCommandRemote(options: {
62
+ basePath: string;
63
+ token: string;
64
+ fetchImpl?: typeof fetch;
65
+ /** Host-scoped extras for this peer — see `createHttpRemote`'s note. */
66
+ extraHeaders?: Record<string, string>;
67
+ }): CommandRemote;
68
+ export interface CommandWorkerDeps {
69
+ /** Re-read every cycle, like the sync timer's peer — null = not linked. */
70
+ resolveRemote: () => CommandRemote | null;
71
+ execute: (command: SyncCommand) => Promise<unknown>;
72
+ worker?: string;
73
+ holdMs?: number;
74
+ /** Idle gap between an empty poll and the next (on top of the hold). */
75
+ idleGapMs?: number;
76
+ backoffMaxMs?: number;
77
+ log?: (msg: string) => void;
78
+ warn?: (msg: string) => void;
79
+ setTimer?: (fn: () => void, ms: number) => unknown;
80
+ clearTimer?: (handle: unknown) => void;
81
+ }
82
+ export interface CommandWorkerHandle {
83
+ stop(): void;
84
+ }
85
+ /** The resident long-poll → execute → report loop. Unreachable remote backs off to
86
+ * `backoffMaxMs` and recovers silently — the nightly window rule. */
87
+ export declare function startCommandWorker(deps: CommandWorkerDeps): CommandWorkerHandle;
88
+ //# sourceMappingURL=commands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../../../src/server/sync/commands.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,CAAC;AAErE,MAAM,WAAW,WAAW;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,aAAa,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACrB;AAID,MAAM,WAAW,YAAY;IAC5B,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,GAAG,WAAW,CAAC;IACvE,qEAAqE;IACrE,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAAC;IACnE,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC;IAChD,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IAC1C,GAAG,CAAC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AC1C,OAAO,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5B,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,EAAE,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IACnD,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;CACvC;AAED,MAAM,WAAW,mBAAmB;IACnC,IAAI,IAAI,IAAI,CAAC;CACb;AAED;sEACsE;AACtE,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,iBAAiB,GAAG,mBAAmB,CAqE/E"}
@@ -0,0 +1,242 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { Hono } from "hono";
3
+ const STALE_CLAIM_MS = 5 * 60_000;
4
+ const toCommand = (r) => ({
5
+ uid: r.uid,
6
+ kind: r.kind,
7
+ args: JSON.parse(r.args),
8
+ status: r.status,
9
+ requestedBy: r.requested_by,
10
+ createdAt: r.created_at,
11
+ claimedAt: r.claimed_at,
12
+ claimedBy: r.claimed_by,
13
+ finishedAt: r.finished_at,
14
+ result: r.result === null ? null : JSON.parse(r.result),
15
+ error: r.error,
16
+ });
17
+ export function createCommandQueue(db) {
18
+ db.exec(`
19
+ CREATE TABLE IF NOT EXISTS sync_commands (
20
+ uid TEXT PRIMARY KEY,
21
+ kind TEXT NOT NULL,
22
+ args TEXT NOT NULL,
23
+ status TEXT NOT NULL DEFAULT 'queued',
24
+ requested_by TEXT NOT NULL,
25
+ created_at INTEGER NOT NULL,
26
+ claimed_at INTEGER,
27
+ claimed_by TEXT,
28
+ finished_at INTEGER,
29
+ result TEXT,
30
+ error TEXT
31
+ );
32
+ CREATE INDEX IF NOT EXISTS idx_sync_commands_status ON sync_commands(status, created_at);
33
+ `);
34
+ const insert = db.prepare("INSERT INTO sync_commands (uid, kind, args, status, requested_by, created_at) VALUES (?, ?, ?, 'queued', ?, ?)");
35
+ const getStmt = db.prepare("SELECT * FROM sync_commands WHERE uid = ?");
36
+ const listStmt = db.prepare("SELECT * FROM sync_commands ORDER BY created_at DESC LIMIT ?");
37
+ const pendingStmt = db.prepare("SELECT COUNT(*) AS n FROM sync_commands WHERE status IN ('queued','claimed')");
38
+ // The atomic claim: one UPDATE whose WHERE picks the oldest eligible row. sqlite
39
+ // serializes writers, so two workers can never claim the same row.
40
+ const claimStmt = db.prepare(`
41
+ UPDATE sync_commands SET status = 'claimed', claimed_at = ?, claimed_by = ?
42
+ WHERE uid = (
43
+ SELECT uid FROM sync_commands
44
+ WHERE status = 'queued' OR (status = 'claimed' AND claimed_at < ?)
45
+ ORDER BY created_at ASC LIMIT 1
46
+ )
47
+ RETURNING *
48
+ `);
49
+ const finishStmt = db.prepare("UPDATE sync_commands SET status = ?, finished_at = ?, result = ?, error = ? WHERE uid = ? AND status IN ('queued','claimed')");
50
+ return {
51
+ enqueue(kind, args, requestedBy) {
52
+ const uid = `cmd-${randomUUID().slice(0, 18)}`;
53
+ insert.run(uid, kind, JSON.stringify(args ?? null), requestedBy, Date.now());
54
+ return toCommand(getStmt.get(uid));
55
+ },
56
+ claimNext(claimedBy, staleMs = STALE_CLAIM_MS) {
57
+ const now = Date.now();
58
+ const row = claimStmt.get(now, claimedBy, now - staleMs);
59
+ return row ? toCommand(row) : null;
60
+ },
61
+ complete: (uid, result) => finishStmt.run("done", Date.now(), JSON.stringify(result ?? null), null, uid).changes > 0,
62
+ fail: (uid, error) => finishStmt.run("failed", Date.now(), null, error, uid).changes > 0,
63
+ get: (uid) => {
64
+ const row = getStmt.get(uid);
65
+ return row ? toCommand(row) : null;
66
+ },
67
+ list: (limit = 100) => listStmt.all(limit).map(toCommand),
68
+ pendingCount: () => pendingStmt.get().n,
69
+ };
70
+ }
71
+ // ── the receiver routes (mount beside the sync receiver, same token gate) ────────
72
+ const noStore = { "cache-control": "no-store" };
73
+ export const DEFAULT_HOLD_MS = 25_000;
74
+ /** GET /pending (held) + POST /pending/:uid — the worker's two verbs. */
75
+ export function createCommandReceiver(options) {
76
+ const maxHold = options.maxHoldMs ?? DEFAULT_HOLD_MS;
77
+ const step = options.pollStepMs ?? 1_000;
78
+ const api = new Hono();
79
+ api.use("*", async (c, next) => {
80
+ const header = c.req.header("authorization") ?? "";
81
+ const presented = header.startsWith("Bearer ") ? header.slice(7) : "";
82
+ if (presented.length === 0 || !options.verifyToken(presented)) {
83
+ return c.json({ error: "unauthorized" }, 401, noStore);
84
+ }
85
+ await next();
86
+ });
87
+ api.get("/pending", async (c) => {
88
+ const worker = c.req.query("worker") ?? "worker";
89
+ const waitRaw = c.req.query("wait");
90
+ const wait = Math.min(maxHold, waitRaw !== undefined && /^\d+$/.test(waitRaw) ? Number(waitRaw) : maxHold);
91
+ const deadline = Date.now() + wait;
92
+ for (;;) {
93
+ const command = options.queue.claimNext(worker);
94
+ if (command)
95
+ return c.json({ command }, 200, noStore);
96
+ if (Date.now() >= deadline)
97
+ return c.json({ command: null }, 200, noStore);
98
+ await new Promise((resolve) => setTimeout(resolve, Math.min(step, Math.max(1, deadline - Date.now()))));
99
+ }
100
+ });
101
+ api.post("/pending/:uid", async (c) => {
102
+ const uid = c.req.param("uid");
103
+ let body;
104
+ try {
105
+ body = await c.req.json();
106
+ }
107
+ catch {
108
+ return c.json({ error: "bad_request" }, 400, noStore);
109
+ }
110
+ const { ok, result, error } = (body ?? {});
111
+ if (typeof ok !== "boolean")
112
+ return c.json({ error: "bad_request" }, 400, noStore);
113
+ const changed = ok
114
+ ? options.queue.complete(uid, result ?? null)
115
+ : options.queue.fail(uid, typeof error === "string" ? error : "failed");
116
+ if (!changed)
117
+ return c.json({ error: "not_found" }, 404, noStore);
118
+ return c.json({ ok: true }, 200, noStore);
119
+ });
120
+ return api;
121
+ }
122
+ export function createCommandRemote(options) {
123
+ const base = options.basePath.replace(/\/$/, "");
124
+ const doFetch = options.fetchImpl ?? fetch;
125
+ const headers = (extra) => ({
126
+ authorization: `Bearer ${options.token}`,
127
+ ...(options.extraHeaders ?? {}),
128
+ ...(extra ?? {}),
129
+ });
130
+ /** 🔴 `redirect: "manual"`, for the reason in `http.ts`'s header: the relay
131
+ * travels the same road as the sync, so a moved peer hostname must fail here
132
+ * with the destination in the message rather than silently landing on a
133
+ * Cloudflare Access login page and reporting a JSON parse error. */
134
+ const refuseRedirect = (r, path) => {
135
+ if (r.status >= 300 && r.status < 400)
136
+ throw new Error(`${path}: ${r.status} — the peer address redirects to ` +
137
+ `${r.headers.get("location") ?? "an unnamed destination"}; the relay URL points at a host that has moved.`);
138
+ };
139
+ return {
140
+ async poll(worker, waitMs) {
141
+ const r = await doFetch(`${base}/pending?worker=${encodeURIComponent(worker)}&wait=${waitMs}`, {
142
+ headers: headers(),
143
+ redirect: "manual",
144
+ // The abort budget must clear the hold plus network slack.
145
+ signal: AbortSignal.timeout(waitMs + 15_000),
146
+ });
147
+ refuseRedirect(r, "commands/pending");
148
+ if (!r.ok)
149
+ throw new Error(`commands/pending: ${r.status}`);
150
+ const body = (await r.json());
151
+ return body.command;
152
+ },
153
+ async report(uid, outcome) {
154
+ const r = await doFetch(`${base}/pending/${encodeURIComponent(uid)}`, {
155
+ method: "POST",
156
+ headers: headers({ "content-type": "application/json" }),
157
+ redirect: "manual",
158
+ body: JSON.stringify(outcome),
159
+ signal: AbortSignal.timeout(30_000),
160
+ });
161
+ refuseRedirect(r, "commands/report");
162
+ if (!r.ok)
163
+ throw new Error(`commands/report: ${r.status}`);
164
+ },
165
+ };
166
+ }
167
+ /** The resident long-poll → execute → report loop. Unreachable remote backs off to
168
+ * `backoffMaxMs` and recovers silently — the nightly window rule. */
169
+ export function startCommandWorker(deps) {
170
+ const worker = deps.worker ?? "mac";
171
+ const holdMs = deps.holdMs ?? DEFAULT_HOLD_MS;
172
+ const idleGapMs = deps.idleGapMs ?? 1_000;
173
+ const backoffMax = deps.backoffMaxMs ?? 10 * 60_000;
174
+ const log = deps.log ?? ((m) => console.log(m));
175
+ const warn = deps.warn ?? ((m) => console.error(m));
176
+ const setTimer = deps.setTimer ?? ((fn, ms) => setTimeout(fn, ms));
177
+ const clearTimer = deps.clearTimer ?? ((h) => clearTimeout(h));
178
+ let stopped = false;
179
+ let handle = null;
180
+ let failures = 0;
181
+ let reportedUnreachable = false;
182
+ const schedule = (ms) => {
183
+ if (stopped)
184
+ return;
185
+ if (handle !== null)
186
+ clearTimer(handle);
187
+ handle = setTimer(cycle, ms);
188
+ handle?.unref?.();
189
+ };
190
+ const cycle = () => {
191
+ if (stopped)
192
+ return;
193
+ const remote = deps.resolveRemote();
194
+ if (remote === null) {
195
+ schedule(60_000);
196
+ return;
197
+ }
198
+ void remote
199
+ .poll(worker, holdMs)
200
+ .then(async (command) => {
201
+ failures = 0;
202
+ reportedUnreachable = false;
203
+ if (command === null)
204
+ return;
205
+ log(`[commands] executing ${command.kind} (${command.uid})`);
206
+ try {
207
+ const result = await deps.execute(command);
208
+ await remote.report(command.uid, { ok: true, result });
209
+ }
210
+ catch (err) {
211
+ const message = err instanceof Error ? err.message : String(err);
212
+ warn(`[commands] ${command.kind} failed: ${message}`);
213
+ // Best-effort: a failed report leaves the row claimed; the stale-claim
214
+ // window re-serves it, which is why executors must tolerate a re-run.
215
+ await remote.report(command.uid, { ok: false, error: message }).catch(() => { });
216
+ }
217
+ })
218
+ .then(() => {
219
+ if (!stopped)
220
+ schedule(idleGapMs);
221
+ })
222
+ .catch((err) => {
223
+ failures += 1;
224
+ const wait = Math.min(backoffMax, 5_000 * 2 ** Math.min(failures - 1, 7));
225
+ if (!reportedUnreachable) {
226
+ const message = err instanceof Error ? err.message : String(err);
227
+ log(`[commands] remote unreachable (${message}) — backing off. This is normal offline.`);
228
+ reportedUnreachable = true;
229
+ }
230
+ schedule(wait);
231
+ });
232
+ };
233
+ schedule(1_000);
234
+ return {
235
+ stop: () => {
236
+ stopped = true;
237
+ if (handle !== null)
238
+ clearTimer(handle);
239
+ },
240
+ };
241
+ }
242
+ //# sourceMappingURL=commands.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.js","sourceRoot":"","sources":["../../../src/server/sync/commands.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAkB5B,MAAM,cAAc,GAAG,CAAC,GAAG,MAAM,CAAC;AA2BlC,MAAM,SAAS,GAAG,CAAC,CAAM,EAAe,EAAE,CAAC,CAAC;IAC3C,GAAG,EAAE,CAAC,CAAC,GAAG;IACV,IAAI,EAAE,CAAC,CAAC,IAAI;IACZ,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAY;IACnC,MAAM,EAAE,CAAC,CAAC,MAAM;IAChB,WAAW,EAAE,CAAC,CAAC,YAAY;IAC3B,SAAS,EAAE,CAAC,CAAC,UAAU;IACvB,SAAS,EAAE,CAAC,CAAC,UAAU;IACvB,SAAS,EAAE,CAAC,CAAC,UAAU;IACvB,UAAU,EAAE,CAAC,CAAC,WAAW;IACzB,MAAM,EAAE,CAAC,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAa;IACpE,KAAK,EAAE,CAAC,CAAC,KAAK;CACd,CAAC,CAAC;AAEH,MAAM,UAAU,kBAAkB,CAAC,EAAY;IAC9C,EAAE,CAAC,IAAI,CAAC;;;;;;;;;;;;;;;EAeP,CAAC,CAAC;IACH,MAAM,MAAM,GAAG,EA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Q,IAAI,CAAC,CAAC;YACd,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAC1E,IAAI,CAAC,mBAAmB,EAAE,CAAC;gBAC1B,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACjE,GAAG,CAAC,kCAAkC,OAAO,0CAA0C,CAAC,CAAC;gBACzF,mBAAmB,GAAG,IAAI,CAAC;YAC5B,CAAC;YACD,QAAQ,CAAC,IAAI,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,QAAQ,CAAC,KAAK,CAAC,CAAC;IAChB,OAAO;QACN,IAAI,EAAE,GAAG,EAAE;YACV,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,MAAM,KAAK,IAAI;gBAAE,UAAU,CAAC,MAAM,CAAC,CAAC;QACzC,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * The dialer half (push-then-pull) and the batch applier. Pure over {@link RemoteApi}
3
+ * and {@link OpLog} so tests drive two REAL stores through the real receiver — no
4
+ * HTTP mocks (the notes/vault discipline).
5
+ *
6
+ * Push-before-pull is load-bearing: the push cursor must be current before the pull
7
+ * computes concurrency, so exactly ONE side materializes any given conflict artifact
8
+ * (vault's rule). Cursors advance only past durably-processed ops; a stopped batch
9
+ * advances exactly to the failure and the next run resumes there.
10
+ *
11
+ * Version skew (notes' hardest-won lesson): the peer's `/info` advertises `opKinds`;
12
+ * the push stops BEFORE the first op the peer cannot take and reports `blockedBy`
13
+ * instead of wedging with an error that reads like corruption. Nothing is dropped —
14
+ * the moment the peer is upgraded, the next push resumes from exactly that op.
15
+ */
16
+ import type { OpLog } from "./opLog";
17
+ import { type ApplyContext, type ApplyOne, type ApplyReport, type PeerInfo, type RemoteApi, type SyncOp, type SyncSummary } from "./types";
18
+ /** Kit-owned dedupe + ledger insert around the app's per-op merge logic. Everything
19
+ * for one op happens in one transaction: state change AND ledger insert, so a crash
20
+ * can never apply without recording (double-apply) or record without applying
21
+ * (silent drop). */
22
+ export declare function createSyncApplier(log: OpLog, applyOne: ApplyOne): (ops: SyncOp[], ctx: ApplyContext) => ApplyReport;
23
+ /** Hold back ops the far side must never see (vault values, local-only kinds).
24
+ * Return null to hold; return a (possibly redacted) op to send. */
25
+ export type ExportPolicy = (op: SyncOp) => SyncOp | null;
26
+ /**
27
+ * A refusal the peer's `opKinds` cannot express — the SAME "stop before it, never drop
28
+ * it" contract, one layer deeper. Return a reason to block, or null to send.
29
+ *
30
+ * The kind check answers "can the peer take this SHAPE". It is not always the whole
31
+ * compatibility surface: notes negotiates `meta.set` a second time on the KEY, because
32
+ * `meta.set` has been a known kind since the log existed, so a NEW key rides a kind the
33
+ * peer recognizes and fails one layer deeper — as a skip on the receiver that looks like
34
+ * a delivered op, or, against a build predating that skip, as a wedged push.
35
+ *
36
+ * 🔴 This is deliberately NOT the {@link ExportPolicy}. A policy HOLDS an op back
37
+ * forever (the far side must never see it); a refusal PAUSES in front of it and keeps
38
+ * the cursor there, so the moment the peer is upgraded the next run resumes from exactly
39
+ * that op. Expressing "the peer is too old for this" as a hold would silently lose the
40
+ * mutation.
41
+ */
42
+ export type PeerRefusal = (op: SyncOp, info: PeerInfo) => string | null;
43
+ export interface EngineOptions {
44
+ log: OpLog;
45
+ /** The op kinds THIS build understands — advertised via /info and used to stop a
46
+ * push into an older peer before the first op it cannot take. */
47
+ opKinds: readonly string[];
48
+ policy?: ExportPolicy;
49
+ /** Optional second compatibility check, finer than `opKinds`. See {@link PeerRefusal}. */
50
+ peerRefusal?: PeerRefusal;
51
+ /** Byte budget per push request (default ~1 MiB). A backlog of large ops is
52
+ * sent as several requests instead of one that trips the proxy's body cap —
53
+ * the 413 WEDGES otherwise: the batch never shrinks, the cursor never moves.
54
+ * A single op larger than the budget still goes alone. */
55
+ maxPushBytes?: number;
56
+ }
57
+ /** Split ops into request-sized chunks by serialized payload weight. */
58
+ export declare function chunkByBytes(ops: SyncOp[], maxBytes: number): SyncOp[][];
59
+ export declare function pushToRemote(opts: EngineOptions, remote: RemoteApi): Promise<Pick<SyncSummary, "remoteId" | "pushed" | "held" | "conflicts" | "blockedBy">>;
60
+ export declare function pullFromRemote(opts: EngineOptions, remote: RemoteApi, applyBatch: (ops: SyncOp[], ctx: ApplyContext) => ApplyReport): Promise<Pick<SyncSummary, "remoteId" | "pulled" | "conflicts">>;
61
+ /** One full reconciliation: push, then pull. */
62
+ export declare function runSync(opts: EngineOptions, remote: RemoteApi, applyBatch: (ops: SyncOp[], ctx: ApplyContext) => ApplyReport): Promise<SyncSummary>;
63
+ //# sourceMappingURL=engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../../src/server/sync/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,SAAS,EAEd,KAAK,MAAM,EACX,KAAK,WAAW,EAChB,MAAM,SAAS,CAAC;AAIjB;;;qBAGqB;AACrB,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,IACvD,KAAK,MAAM,EAAE,EAAE,KAAK,YAAY,KAAG,WAAW,CAkCtD;AAED;oEACoE;AACpE,MAAM,MAAM,YAAY,GAAG,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,EAAE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