cursedbelt-server 1.1.0 → 2.0.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.
- package/dist/server/sync/http.d.ts +20 -3
- package/dist/server/sync/http.js +20 -14
- package/dist/server/sync/index.d.ts +10 -2
- package/dist/server/sync/index.js +9 -1
- package/dist/server/sync/planner.d.ts +38 -8
- package/dist/server/sync/planner.js +32 -8
- package/dist/server/sync/signal.d.ts +161 -0
- package/dist/server/sync/signal.js +348 -0
- package/dist/server/sync/timer.d.ts +63 -19
- package/dist/server/sync/timer.js +104 -45
- package/dist/server/sync/types.d.ts +0 -2
- package/package.json +1 -1
- package/src/noTimerDialsAPeer.spec.ts +469 -0
- package/src/server/sync/http.ts +31 -16
- package/src/server/sync/index.ts +23 -1
- package/src/server/sync/planner.spec.ts +33 -16
- package/src/server/sync/planner.ts +48 -11
- package/src/server/sync/signal.spec.ts +306 -0
- package/src/server/sync/signal.ts +422 -0
- package/src/server/sync/timer.spec.ts +97 -16
- package/src/server/sync/timer.ts +124 -47
- package/src/server/sync/types.ts +0 -2
package/src/server/sync/timer.ts
CHANGED
|
@@ -1,13 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The in-process
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* The in-process sync loop. Runs on the dialing half only; a failure NEVER takes
|
|
3
|
+
* the host daemon down (an unsynced instance still serves and still accepts writes
|
|
4
|
+
* — that is the point of two independently-writable instances). The timer is
|
|
5
|
+
* `unref`'d so it never holds the process open.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* ── 🔴 This loop does not poll, and it may never be made to again ────────────────
|
|
8
|
+
*
|
|
9
|
+
* Until 2026-09-15 this file was a dialer twice over: a `GET /requested` probe at a
|
|
10
|
+
* peer every 20 s while healthy (`REQUEST_POLL_MS`), and a full reconciliation every
|
|
11
|
+
* 5 minutes (`DEFAULT_LOOP.intervalMs`) whether or not either side had news. The
|
|
12
|
+
* owner ruled both out — *"There should be no polling in cb unless you can make some
|
|
13
|
+
* good case for it that beats the api option"* — and the case against was asked for
|
|
14
|
+
* and not found. Measured in `vault`: the 20 s probe alone was **68 % of that app's
|
|
15
|
+
* entire traffic, one request every 22 seconds, for an app with one user.**
|
|
16
|
+
*
|
|
17
|
+
* Both are gone. Every wake-up this loop books now carries a {@link WakeReason}, and
|
|
18
|
+
* that union is deliberately closed with no periodic member — there is no value you
|
|
19
|
+
* can pass to {@link schedule} that means "again in a while". That makes the poll
|
|
20
|
+
* unrepresentable rather than merely discouraged, which matters because
|
|
21
|
+
* `REQUEST_POLL_MS` did not survive as a habit; it survived as **exported public
|
|
22
|
+
* API**, and the next consumer to import it would have made its removal a breaking
|
|
23
|
+
* change instead of an edit.
|
|
24
|
+
*
|
|
25
|
+
* What replaced each half:
|
|
26
|
+
*
|
|
27
|
+
* · **Mac → peer** is a PUSH. `markDirty()` after a local write books one
|
|
28
|
+
* debounced run: the side that has news says so, which is what "sync now"
|
|
29
|
+
* should always have been.
|
|
30
|
+
* · **peer → Mac** is `./signal` — the dialing half holds one long-lived stream
|
|
31
|
+
* open and the receiver writes a frame when its log grows. The Mac is still
|
|
32
|
+
* purely a client: it dials OUT, listens on nothing, and no timer fires.
|
|
33
|
+
*
|
|
34
|
+
* 🔴 **`markDirty()` is now REQUIRED wiring, not an optimization.** With the steady
|
|
35
|
+
* interval gone it is the only thing that pushes a local write. A consumer that
|
|
36
|
+
* takes the handle and drops `markDirty` (which `apps/vault` did while the interval
|
|
37
|
+
* still covered for it) will sit clean and silent forever.
|
|
38
|
+
*
|
|
39
|
+
* The peer is re-resolved on every run, so revoking a link takes effect without a
|
|
40
|
+
* restart. Linking a NEW peer is a user action and is noticed at the next
|
|
41
|
+
* `syncNow()` / `markDirty()` rather than by a timer that was watching for it —
|
|
42
|
+
* call `syncNow()` after a link and it is immediate.
|
|
11
43
|
*/
|
|
12
44
|
import {
|
|
13
45
|
isUnreachable,
|
|
@@ -19,21 +51,35 @@ import {
|
|
|
19
51
|
import type { SyncStatusReporter } from "./status";
|
|
20
52
|
import type { SyncSummary } from "./types";
|
|
21
53
|
|
|
22
|
-
|
|
54
|
+
/**
|
|
55
|
+
* One reconciliation shortly after start: catch up on whatever happened while this
|
|
56
|
+
* process was down. A single shot at boot, not a cadence — the same catch-up a push
|
|
57
|
+
* model needs anyway.
|
|
58
|
+
*/
|
|
23
59
|
const KICKOFF_MS = 5_000;
|
|
24
60
|
|
|
61
|
+
/**
|
|
62
|
+
* 🔴 Every reason this loop is allowed to wake up. There is deliberately no
|
|
63
|
+
* `"interval"` / `"poll"` member, and adding one is the change this whole file
|
|
64
|
+
* exists to refuse.
|
|
65
|
+
*
|
|
66
|
+
* · `boot` — one catch-up run at start.
|
|
67
|
+
* · `dirty` — local ops are waiting to be pushed (debounced).
|
|
68
|
+
* · `forced` — `syncNow()`, or a frame off `./signal` saying the peer has news.
|
|
69
|
+
* · `retry` — the last attempt failed. Runs only while disconnected, never
|
|
70
|
+
* against a healthy peer; that is a reconnect backoff, not a poll.
|
|
71
|
+
*/
|
|
72
|
+
export type WakeReason = "boot" | "dirty" | "forced" | "retry";
|
|
73
|
+
|
|
25
74
|
export interface SyncTimerDeps {
|
|
26
|
-
/** Re-read every
|
|
75
|
+
/** Re-read on every run: null = not linked (quiet no-op). */
|
|
27
76
|
resolvePeer: () => { basePath: string; token: string } | null;
|
|
28
77
|
/** Run one full reconciliation against the resolved peer. */
|
|
29
78
|
sync: (peer: { basePath: string; token: string }) => Promise<SyncSummary>;
|
|
30
79
|
/** The local op-log head — a head beyond the last synced one marks dirty. */
|
|
31
80
|
head: () => number;
|
|
32
|
-
/** Ask the peer whether its "Sync now" was pressed; null on any failure. */
|
|
33
|
-
checkRequest?: (peer: { basePath: string; token: string }) => Promise<number | null>;
|
|
34
81
|
status?: SyncStatusReporter;
|
|
35
82
|
loop?: SyncLoopConfig;
|
|
36
|
-
requestPollMs?: number;
|
|
37
83
|
now?: () => number;
|
|
38
84
|
setTimer?: (fn: () => void, ms: number) => unknown;
|
|
39
85
|
clearTimer?: (handle: unknown) => void;
|
|
@@ -45,9 +91,16 @@ export interface SyncTimerDeps {
|
|
|
45
91
|
|
|
46
92
|
export interface SyncTimerHandle {
|
|
47
93
|
stop(): void;
|
|
48
|
-
/**
|
|
94
|
+
/**
|
|
95
|
+
* Run now. The "Sync now" button on the dialing half, and the handler for a
|
|
96
|
+
* `./signal` frame — both are "something has news", which is a push.
|
|
97
|
+
*/
|
|
49
98
|
syncNow(): void;
|
|
50
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Mark local writes pending — call after any local op. 🔴 Required wiring: with
|
|
101
|
+
* no steady interval this is what pushes a local write. A burst coalesces into
|
|
102
|
+
* one run via the planner's debounce.
|
|
103
|
+
*/
|
|
51
104
|
markDirty(): void;
|
|
52
105
|
}
|
|
53
106
|
|
|
@@ -68,7 +121,6 @@ export function startSyncTimer(deps: SyncTimerDeps): SyncTimerHandle {
|
|
|
68
121
|
const warn = deps.warn ?? ((m: string) => console.error(m));
|
|
69
122
|
const status = deps.status;
|
|
70
123
|
const peerLabel = deps.peerLabel ?? defaultPeerLabel;
|
|
71
|
-
const requestPollMs = deps.requestPollMs ?? REQUEST_POLL_MS;
|
|
72
124
|
status?.update({ enabled: true, role: "dialer" });
|
|
73
125
|
|
|
74
126
|
const state: SyncLoopState = { lastAttempt: 0, consecutiveFailures: 0, dirtySince: null };
|
|
@@ -76,38 +128,56 @@ export function startSyncTimer(deps: SyncTimerDeps): SyncTimerHandle {
|
|
|
76
128
|
let stopped = false;
|
|
77
129
|
let handle: unknown = null;
|
|
78
130
|
let reportedUnreachable = false;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
131
|
+
/**
|
|
132
|
+
* 🔴 Starts TRUE: the boot catch-up is a forced run, not a scheduled one.
|
|
133
|
+
*
|
|
134
|
+
* It used to fall out of the planner for free — `lastAttempt: 0` plus a steady
|
|
135
|
+
* `intervalMs` made the first tick always due. With no interval left, a clean
|
|
136
|
+
* healthy state plans nothing, so the one run this loop genuinely owes at
|
|
137
|
+
* startup has to be asked for explicitly. It has a reason: this process may have
|
|
138
|
+
* missed ops while it was down, and catching up is exactly what a push model
|
|
139
|
+
* does on reconnect.
|
|
140
|
+
*/
|
|
141
|
+
let forceRun = true;
|
|
82
142
|
let inFlight = false;
|
|
143
|
+
/** Why the currently-booked wake exists, or null when the loop is quiet. */
|
|
144
|
+
let bookedFor: WakeReason | null = null;
|
|
83
145
|
|
|
84
|
-
|
|
146
|
+
/**
|
|
147
|
+
* Book the next wake-up. 🔴 The `reason` is not decoration — it is the type-level
|
|
148
|
+
* guard: {@link WakeReason} has no periodic member, so there is no way to spell
|
|
149
|
+
* "wake again in `intervalMs`" through this function. It is also read back by
|
|
150
|
+
* {@link SyncTimerHandle.markDirty}, which must not push an imminent forced run
|
|
151
|
+
* out to a debounce.
|
|
152
|
+
*/
|
|
153
|
+
const schedule = (reason: WakeReason, ms: number): void => {
|
|
85
154
|
if (stopped) return;
|
|
86
155
|
if (handle !== null) clearTimer(handle);
|
|
87
156
|
handle = setTimer(tick, ms);
|
|
88
157
|
(handle as { unref?: () => void })?.unref?.();
|
|
158
|
+
bookedFor = reason;
|
|
89
159
|
};
|
|
90
160
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
161
|
+
/** Nothing to push, nothing to retry: cancel any pending wake and go quiet. */
|
|
162
|
+
const idle = (): void => {
|
|
163
|
+
if (handle !== null) clearTimer(handle);
|
|
164
|
+
handle = null;
|
|
165
|
+
bookedFor = null;
|
|
94
166
|
};
|
|
95
167
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
probing = false;
|
|
110
|
-
});
|
|
168
|
+
/** Apply the planner's verdict — the ONE place a wake-up is booked or declined. */
|
|
169
|
+
const bookNext = (): void => {
|
|
170
|
+
if (stopped) return;
|
|
171
|
+
if (forceRun) {
|
|
172
|
+
schedule("forced", 1);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const plan = planNextSync(state, now(), loop);
|
|
176
|
+
if (plan.waitMs === null || plan.reason === null) {
|
|
177
|
+
idle();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
schedule(plan.reason, Math.max(1, plan.waitMs));
|
|
111
181
|
};
|
|
112
182
|
|
|
113
183
|
const tick = (): void => {
|
|
@@ -122,15 +192,17 @@ export function startSyncTimer(deps: SyncTimerDeps): SyncTimerHandle {
|
|
|
122
192
|
pending: Math.max(0, head - syncedHead),
|
|
123
193
|
});
|
|
124
194
|
if (peer === null) {
|
|
125
|
-
|
|
195
|
+
// Not linked. Nothing to dial and nothing to wait for — `syncNow()` or the
|
|
196
|
+
// next local write re-checks. A re-check timer here would be a poll at a
|
|
197
|
+
// peer that does not exist yet.
|
|
198
|
+
forceRun = false;
|
|
199
|
+
idle();
|
|
126
200
|
return;
|
|
127
201
|
}
|
|
128
202
|
|
|
129
203
|
const plan = planNextSync(state, now(), loop);
|
|
130
204
|
if (!plan.runNow && !forceRun) {
|
|
131
|
-
|
|
132
|
-
schedule(sleepFor(plan.waitMs));
|
|
133
|
-
if (linkedAndHealthy) probeRequest(peer);
|
|
205
|
+
bookNext();
|
|
134
206
|
return;
|
|
135
207
|
}
|
|
136
208
|
|
|
@@ -182,26 +254,31 @@ export function startSyncTimer(deps: SyncTimerDeps): SyncTimerHandle {
|
|
|
182
254
|
})
|
|
183
255
|
.finally(() => {
|
|
184
256
|
inFlight = false;
|
|
185
|
-
|
|
186
|
-
|
|
257
|
+
// A local write that landed mid-run is news the planner must see.
|
|
258
|
+
if (deps.head() > syncedHead && state.dirtySince === null) state.dirtySince = now();
|
|
259
|
+
bookNext();
|
|
187
260
|
});
|
|
188
261
|
};
|
|
189
262
|
|
|
190
|
-
schedule(KICKOFF_MS);
|
|
263
|
+
schedule("boot", KICKOFF_MS);
|
|
191
264
|
return {
|
|
192
265
|
stop: () => {
|
|
193
266
|
stopped = true;
|
|
194
267
|
if (handle !== null) clearTimer(handle);
|
|
268
|
+
handle = null;
|
|
195
269
|
},
|
|
196
270
|
syncNow: () => {
|
|
197
271
|
if (stopped) return;
|
|
198
272
|
forceRun = true;
|
|
199
|
-
schedule(1);
|
|
273
|
+
schedule("forced", 1);
|
|
200
274
|
},
|
|
201
275
|
markDirty: () => {
|
|
202
276
|
if (stopped) return;
|
|
203
277
|
if (state.dirtySince === null) state.dirtySince = now();
|
|
204
|
-
|
|
278
|
+
// A forced run is already due in ~1ms and will push these ops; re-booking
|
|
279
|
+
// would replace it with a 3s debounce and make "Sync now" slower.
|
|
280
|
+
if (bookedFor === "forced") return;
|
|
281
|
+
bookNext();
|
|
205
282
|
},
|
|
206
283
|
};
|
|
207
284
|
}
|
package/src/server/sync/types.ts
CHANGED
|
@@ -67,8 +67,6 @@ export interface RemoteApi {
|
|
|
67
67
|
excludeOrigin: string,
|
|
68
68
|
): Promise<{ ops: SyncOp[]; cursor: number; head: number }>;
|
|
69
69
|
push(payload: { ops: SyncOp[]; peerHas: number }): Promise<ApplyReport>;
|
|
70
|
-
/** The receiver's "someone pressed Sync now here" stamp; null when unsupported. */
|
|
71
|
-
requestedAt?(): Promise<number | null>;
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
/** Context the applier gets per batch — vault's concurrency bound. */
|