@indigoai-us/hq-cloud 6.3.0 → 6.3.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.
- package/dist/bin/sync-runner.d.ts +22 -2
- package/dist/bin/sync-runner.d.ts.map +1 -1
- package/dist/bin/sync-runner.js +85 -2
- package/dist/bin/sync-runner.js.map +1 -1
- package/dist/bin/sync-runner.test.js +201 -0
- package/dist/bin/sync-runner.test.js.map +1 -1
- package/dist/cli/rescue-core.js +14 -2
- package/dist/cli/rescue-core.js.map +1 -1
- package/dist/cli/rescue-hq-root-guard.test.d.ts +2 -0
- package/dist/cli/rescue-hq-root-guard.test.d.ts.map +1 -0
- package/dist/cli/rescue-hq-root-guard.test.js +176 -0
- package/dist/cli/rescue-hq-root-guard.test.js.map +1 -0
- package/dist/skill-telemetry.d.ts +42 -6
- package/dist/skill-telemetry.d.ts.map +1 -1
- package/dist/skill-telemetry.js +253 -10
- package/dist/skill-telemetry.js.map +1 -1
- package/dist/skill-telemetry.test.js +287 -1
- package/dist/skill-telemetry.test.js.map +1 -1
- package/dist/sync/event-sync.d.ts +181 -0
- package/dist/sync/event-sync.d.ts.map +1 -0
- package/dist/sync/event-sync.js +316 -0
- package/dist/sync/event-sync.js.map +1 -0
- package/dist/sync/event-sync.test.d.ts +14 -0
- package/dist/sync/event-sync.test.d.ts.map +1 -0
- package/dist/sync/event-sync.test.js +440 -0
- package/dist/sync/event-sync.test.js.map +1 -0
- package/package.json +1 -1
- package/src/bin/sync-runner.test.ts +246 -0
- package/src/bin/sync-runner.ts +117 -3
- package/src/cli/rescue-core.ts +15 -2
- package/src/cli/rescue-hq-root-guard.test.ts +193 -0
- package/src/skill-telemetry.test.ts +433 -0
- package/src/skill-telemetry.ts +260 -10
- package/src/sync/event-sync.test.ts +533 -0
- package/src/sync/event-sync.ts +481 -0
- package/test/e2e/sync/cross-tenant-isolation.test.ts +126 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event-driven sync wiring — Phase 3 of event-driven-sync-menubar
|
|
3
|
+
* (US-017 publish / US-018 receive / US-019 rollout gate).
|
|
4
|
+
*
|
|
5
|
+
* Phases 1–2 built every piece but left them unconnected: the watcher runs
|
|
6
|
+
* targeted *push passes* (S3 upload) but never publishes a PushEvent, and the
|
|
7
|
+
* runner's receiver seam defaults to {@link NoopPushReceiver}. This module is
|
|
8
|
+
* the connective tissue that turns both on for enrolled accounts:
|
|
9
|
+
*
|
|
10
|
+
* - {@link resolveEventSync} — the rollout gate. EXACT-email allowlist
|
|
11
|
+
* (mirrors `resolvePresignTransport`'s shape in sync-runner.ts, but full
|
|
12
|
+
* address, not domain suffix) + `HQ_SYNC_EVENT_SYNC` env override in both
|
|
13
|
+
* directions. ONE gate governs publish AND receive so a device is never
|
|
14
|
+
* publish-only or receive-only in a half-rolled state.
|
|
15
|
+
* - {@link subscribeSyncReceive} — `POST /v1/sync/subscribe` (US-015/US-016):
|
|
16
|
+
* mints the per-device queue and returns `{queueUrl, region, credentials}`,
|
|
17
|
+
* where `credentials` are short-lived STS creds scoped to receive/delete on
|
|
18
|
+
* exactly that queue.
|
|
19
|
+
* - {@link sqsClientFromAwsSdk} — the doc-promised thin adapter from the AWS
|
|
20
|
+
* SDK `SQSClient` to the receiver's narrow {@link SqsClientLike} seam.
|
|
21
|
+
* - {@link createRefreshingSqsClient} — wraps the adapter with credential
|
|
22
|
+
* lifecycle: proactive re-vend before expiry (skew window) + one reactive
|
|
23
|
+
* retry on an expiry-class error. The queue URL is stable across re-vends
|
|
24
|
+
* (same device → same queue, idempotent endpoint); only creds rotate.
|
|
25
|
+
* - {@link startEventSync} — the wiring entry the runner calls from the
|
|
26
|
+
* `--event-push` watch block when the gate is ON. Resolves tenant + device
|
|
27
|
+
* identity, builds the {@link HttpPushTransport} + {@link PushEventEmitter}
|
|
28
|
+
* (publish leg) and the {@link SqsPushReceiver} (receive leg, self-echo
|
|
29
|
+
* filtered), and returns handles. Any startup failure degrades to
|
|
30
|
+
* poll-only — it NEVER takes the daemon down.
|
|
31
|
+
*
|
|
32
|
+
* The 10-minute `--poll-remote-ms` pass remains the correctness backstop for
|
|
33
|
+
* every path here; event delivery is best-effort by design.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import {
|
|
37
|
+
SQSClient,
|
|
38
|
+
ReceiveMessageCommand,
|
|
39
|
+
DeleteMessageCommand,
|
|
40
|
+
} from "@aws-sdk/client-sqs";
|
|
41
|
+
|
|
42
|
+
import { HttpPushTransport, type AuthTokenSource } from "./push-transport.js";
|
|
43
|
+
import {
|
|
44
|
+
SqsPushReceiver,
|
|
45
|
+
type PushReceiver,
|
|
46
|
+
type SqsClientLike,
|
|
47
|
+
type SyncEngineFn,
|
|
48
|
+
} from "./push-receiver.js";
|
|
49
|
+
import { PushEventEmitter } from "../watcher.js";
|
|
50
|
+
import type { TreeChangeBatch } from "../watcher.js";
|
|
51
|
+
|
|
52
|
+
// ─── US-019: rollout gate ───────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Accounts enrolled in event-driven sync (publish + receive).
|
|
56
|
+
*
|
|
57
|
+
* EXACT full-address matching, case-insensitive — NOT a domain suffix. The
|
|
58
|
+
* single-account Phase 3 rollout (2026-06-10) targets the operator's own
|
|
59
|
+
* devices; `xhassaan@getindigo.ai` and `hassaan@getindigo.ai.evil.com` must
|
|
60
|
+
* never match. Broadening later is an entry here (or a domain-set like
|
|
61
|
+
* `PRESIGN_ROLLOUT_DOMAINS` once GA'd).
|
|
62
|
+
*/
|
|
63
|
+
export const EVENT_SYNC_ROLLOUT_EMAILS: ReadonlySet<string> = new Set([
|
|
64
|
+
"hassaan@getindigo.ai",
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Decide whether this session runs event-driven sync (publish + receive).
|
|
69
|
+
*
|
|
70
|
+
* Mirrors `resolvePresignTransport` precedence: `HQ_SYNC_EVENT_SYNC`
|
|
71
|
+
* overrides in both directions (`1`/`true`/`yes`/`on` → force on,
|
|
72
|
+
* `0`/`false`/`no`/`off` → force off) so unenrolled testers can exercise it
|
|
73
|
+
* and enrolled accounts can be rolled back without a release. An unset/blank
|
|
74
|
+
* override falls through to the exact-email check; an unrecognized override
|
|
75
|
+
* value is ignored (email check wins).
|
|
76
|
+
*/
|
|
77
|
+
export function resolveEventSync(
|
|
78
|
+
email: string | undefined,
|
|
79
|
+
override: string | undefined,
|
|
80
|
+
): boolean {
|
|
81
|
+
const o = (override ?? "").trim().toLowerCase();
|
|
82
|
+
if (o === "1" || o === "true" || o === "yes" || o === "on") return true;
|
|
83
|
+
if (o === "0" || o === "false" || o === "no" || o === "off") return false;
|
|
84
|
+
if (typeof email !== "string") return false;
|
|
85
|
+
return EVENT_SYNC_ROLLOUT_EMAILS.has(email.trim().toLowerCase());
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ─── US-018: subscribe + credentials ────────────────────────────────────────
|
|
89
|
+
|
|
90
|
+
export interface SubscribeSyncCredentials {
|
|
91
|
+
accessKeyId: string;
|
|
92
|
+
secretAccessKey: string;
|
|
93
|
+
sessionToken: string;
|
|
94
|
+
/** ISO8601 expiry of the vended STS credentials. */
|
|
95
|
+
expiration: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface SubscribeSyncResponse {
|
|
99
|
+
/** The caller's own per-device queue URL (stable across calls). */
|
|
100
|
+
queueUrl: string;
|
|
101
|
+
/** Region the queue lives in. */
|
|
102
|
+
region: string;
|
|
103
|
+
/** Short-lived creds scoped to receive/delete on exactly this queue. */
|
|
104
|
+
credentials: SubscribeSyncCredentials;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Minimal fetch seam (matches push-transport.ts's FetchLike posture). */
|
|
108
|
+
type FetchLike = (
|
|
109
|
+
url: string,
|
|
110
|
+
init: {
|
|
111
|
+
method: string;
|
|
112
|
+
headers: Record<string, string>;
|
|
113
|
+
body: string;
|
|
114
|
+
signal?: AbortSignal;
|
|
115
|
+
},
|
|
116
|
+
) => Promise<{ ok: boolean; status: number; text(): Promise<string> }>;
|
|
117
|
+
|
|
118
|
+
function asNonEmptyString(v: unknown, field: string): string {
|
|
119
|
+
if (typeof v !== "string" || v.length === 0) {
|
|
120
|
+
throw new Error(`subscribeSyncReceive: response missing ${field}`);
|
|
121
|
+
}
|
|
122
|
+
return v;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* `POST /v1/sync/subscribe` — provision (idempotently) this device's queue
|
|
127
|
+
* and vend fresh receive credentials. Auth mirrors HttpPushTransport: Bearer
|
|
128
|
+
* token resolved per-call via the supplied source.
|
|
129
|
+
*/
|
|
130
|
+
export async function subscribeSyncReceive(opts: {
|
|
131
|
+
apiUrl: string;
|
|
132
|
+
authToken: AuthTokenSource;
|
|
133
|
+
deviceId: string;
|
|
134
|
+
timeoutMs?: number;
|
|
135
|
+
fetchImpl?: FetchLike;
|
|
136
|
+
}): Promise<SubscribeSyncResponse> {
|
|
137
|
+
const apiUrl = opts.apiUrl.replace(/\/+$/, "");
|
|
138
|
+
const tok = opts.authToken;
|
|
139
|
+
const token = typeof tok === "function" ? await tok() : tok;
|
|
140
|
+
const fetchImpl =
|
|
141
|
+
opts.fetchImpl ??
|
|
142
|
+
((input, init) => (globalThis.fetch as unknown as FetchLike)(input, init));
|
|
143
|
+
|
|
144
|
+
const controller = new AbortController();
|
|
145
|
+
const timeout = setTimeout(
|
|
146
|
+
() => controller.abort(),
|
|
147
|
+
opts.timeoutMs ?? 15_000,
|
|
148
|
+
);
|
|
149
|
+
let res: Awaited<ReturnType<FetchLike>>;
|
|
150
|
+
try {
|
|
151
|
+
res = await fetchImpl(`${apiUrl}/v1/sync/subscribe`, {
|
|
152
|
+
method: "POST",
|
|
153
|
+
headers: {
|
|
154
|
+
Authorization: `Bearer ${token}`,
|
|
155
|
+
"Content-Type": "application/json",
|
|
156
|
+
},
|
|
157
|
+
body: JSON.stringify({ deviceId: opts.deviceId }),
|
|
158
|
+
signal: controller.signal,
|
|
159
|
+
});
|
|
160
|
+
} finally {
|
|
161
|
+
clearTimeout(timeout);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!res.ok) {
|
|
165
|
+
const text = await res.text().catch(() => "");
|
|
166
|
+
throw new Error(
|
|
167
|
+
`subscribeSyncReceive: POST /v1/sync/subscribe failed (${res.status}): ${text.slice(0, 300)}`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const parsed: unknown = JSON.parse(await res.text());
|
|
171
|
+
const obj = parsed as Record<string, unknown>;
|
|
172
|
+
const creds = (obj.credentials ?? {}) as Record<string, unknown>;
|
|
173
|
+
return {
|
|
174
|
+
queueUrl: asNonEmptyString(obj.queueUrl, "queueUrl"),
|
|
175
|
+
region: asNonEmptyString(obj.region, "region"),
|
|
176
|
+
credentials: {
|
|
177
|
+
accessKeyId: asNonEmptyString(creds.accessKeyId, "credentials.accessKeyId"),
|
|
178
|
+
secretAccessKey: asNonEmptyString(
|
|
179
|
+
creds.secretAccessKey,
|
|
180
|
+
"credentials.secretAccessKey",
|
|
181
|
+
),
|
|
182
|
+
sessionToken: asNonEmptyString(
|
|
183
|
+
creds.sessionToken,
|
|
184
|
+
"credentials.sessionToken",
|
|
185
|
+
),
|
|
186
|
+
expiration: asNonEmptyString(creds.expiration, "credentials.expiration"),
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ─── SQS SDK adapter ────────────────────────────────────────────────────────
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Adapt the AWS SDK `SQSClient` to the receiver's narrow {@link SqsClientLike}
|
|
195
|
+
* seam (the doc-promised `sqsClientFromAwsSdk` from push-receiver.ts). The
|
|
196
|
+
* abort signal is forwarded so `dispose()` can cut a 20s long-poll short.
|
|
197
|
+
*/
|
|
198
|
+
export function sqsClientFromAwsSdk(
|
|
199
|
+
client: Pick<SQSClient, "send">,
|
|
200
|
+
): SqsClientLike {
|
|
201
|
+
return {
|
|
202
|
+
async receiveMessage({ queueUrl, maxMessages, waitTimeSeconds, signal }) {
|
|
203
|
+
const out = await client.send(
|
|
204
|
+
new ReceiveMessageCommand({
|
|
205
|
+
QueueUrl: queueUrl,
|
|
206
|
+
MaxNumberOfMessages: maxMessages,
|
|
207
|
+
WaitTimeSeconds: waitTimeSeconds,
|
|
208
|
+
}),
|
|
209
|
+
{ abortSignal: signal },
|
|
210
|
+
);
|
|
211
|
+
return {
|
|
212
|
+
messages: (out.Messages ?? []).map((m) => ({
|
|
213
|
+
Body: m.Body,
|
|
214
|
+
ReceiptHandle: m.ReceiptHandle,
|
|
215
|
+
MessageId: m.MessageId,
|
|
216
|
+
})),
|
|
217
|
+
};
|
|
218
|
+
},
|
|
219
|
+
async deleteMessage({ queueUrl, receiptHandle }) {
|
|
220
|
+
await client.send(
|
|
221
|
+
new DeleteMessageCommand({
|
|
222
|
+
QueueUrl: queueUrl,
|
|
223
|
+
ReceiptHandle: receiptHandle,
|
|
224
|
+
}),
|
|
225
|
+
);
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ─── Refreshing SQS client (credential lifecycle) ───────────────────────────
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Error names the SQS SDK surfaces when STS session creds have expired or
|
|
234
|
+
* become invalid. Used for the reactive retry-once path; the proactive
|
|
235
|
+
* skew-window refresh below should make these rare.
|
|
236
|
+
*/
|
|
237
|
+
const EXPIRY_ERROR_NAMES = new Set([
|
|
238
|
+
"ExpiredToken",
|
|
239
|
+
"ExpiredTokenException",
|
|
240
|
+
"InvalidSecurityToken",
|
|
241
|
+
"InvalidClientTokenId",
|
|
242
|
+
"UnrecognizedClientException",
|
|
243
|
+
]);
|
|
244
|
+
|
|
245
|
+
function isExpiryError(err: unknown): boolean {
|
|
246
|
+
return err instanceof Error && EXPIRY_ERROR_NAMES.has(err.name);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Re-vend this long before the recorded expiry (proactive refresh window). */
|
|
250
|
+
const REFRESH_SKEW_MS = 120_000;
|
|
251
|
+
|
|
252
|
+
export interface RefreshingSqsClientOptions {
|
|
253
|
+
/** The initial subscribe response (creds + region + queue URL). */
|
|
254
|
+
initial: SubscribeSyncResponse;
|
|
255
|
+
/** Re-vend: called when creds are near/past expiry. Idempotent server-side. */
|
|
256
|
+
subscribe: () => Promise<SubscribeSyncResponse>;
|
|
257
|
+
/**
|
|
258
|
+
* Build the underlying narrow client from a subscribe response. Default:
|
|
259
|
+
* AWS SDK `SQSClient` via {@link sqsClientFromAwsSdk}. Tests inject a fake.
|
|
260
|
+
*/
|
|
261
|
+
buildSqs?: (resp: SubscribeSyncResponse) => SqsClientLike;
|
|
262
|
+
/** Clock seam (tests). Default `Date.now`. */
|
|
263
|
+
now?: () => number;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function defaultBuildSqs(resp: SubscribeSyncResponse): SqsClientLike {
|
|
267
|
+
return sqsClientFromAwsSdk(
|
|
268
|
+
new SQSClient({
|
|
269
|
+
region: resp.region,
|
|
270
|
+
credentials: {
|
|
271
|
+
accessKeyId: resp.credentials.accessKeyId,
|
|
272
|
+
secretAccessKey: resp.credentials.secretAccessKey,
|
|
273
|
+
sessionToken: resp.credentials.sessionToken,
|
|
274
|
+
expiration: new Date(resp.credentials.expiration),
|
|
275
|
+
},
|
|
276
|
+
}),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* An {@link SqsClientLike} that owns the vended-credential lifecycle:
|
|
282
|
+
*
|
|
283
|
+
* - PROACTIVE: before each call, if the recorded expiry is within the skew
|
|
284
|
+
* window, re-subscribe (re-vend) and rebuild the inner client first.
|
|
285
|
+
* - REACTIVE: if a call still fails with an expiry-class error (clock skew,
|
|
286
|
+
* revocation), re-vend once and retry the call once. Anything else — or a
|
|
287
|
+
* second failure — propagates to the receiver's own backoff/reconnect
|
|
288
|
+
* loop, whose retention-backed redelivery makes the miss recoverable.
|
|
289
|
+
*
|
|
290
|
+
* Concurrent refreshes collapse onto one in-flight subscribe promise.
|
|
291
|
+
*/
|
|
292
|
+
export function createRefreshingSqsClient(
|
|
293
|
+
opts: RefreshingSqsClientOptions,
|
|
294
|
+
): SqsClientLike {
|
|
295
|
+
const now = opts.now ?? (() => Date.now());
|
|
296
|
+
const buildSqs = opts.buildSqs ?? defaultBuildSqs;
|
|
297
|
+
|
|
298
|
+
let inner = buildSqs(opts.initial);
|
|
299
|
+
let expiresAtMs = Date.parse(opts.initial.credentials.expiration);
|
|
300
|
+
let refreshing: Promise<void> | null = null;
|
|
301
|
+
|
|
302
|
+
const refresh = (): Promise<void> => {
|
|
303
|
+
refreshing ??= (async () => {
|
|
304
|
+
try {
|
|
305
|
+
const resp = await opts.subscribe();
|
|
306
|
+
inner = buildSqs(resp);
|
|
307
|
+
expiresAtMs = Date.parse(resp.credentials.expiration);
|
|
308
|
+
} finally {
|
|
309
|
+
refreshing = null;
|
|
310
|
+
}
|
|
311
|
+
})();
|
|
312
|
+
return refreshing;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
const ensureFresh = async (): Promise<void> => {
|
|
316
|
+
if (Number.isFinite(expiresAtMs) && now() >= expiresAtMs - REFRESH_SKEW_MS) {
|
|
317
|
+
await refresh();
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const withRetry = async <T>(call: () => Promise<T>): Promise<T> => {
|
|
322
|
+
await ensureFresh();
|
|
323
|
+
try {
|
|
324
|
+
return await call();
|
|
325
|
+
} catch (err) {
|
|
326
|
+
if (!isExpiryError(err)) throw err;
|
|
327
|
+
await refresh();
|
|
328
|
+
return await call();
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
return {
|
|
333
|
+
receiveMessage: (args) => withRetry(() => inner.receiveMessage(args)),
|
|
334
|
+
deleteMessage: (args) => withRetry(() => inner.deleteMessage(args)),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ─── US-017 + US-018: full wiring ───────────────────────────────────────────
|
|
339
|
+
|
|
340
|
+
/** Structured line logger seam — the runner passes its stderr logger. */
|
|
341
|
+
export type EventSyncLog = (message: string) => void;
|
|
342
|
+
|
|
343
|
+
export interface StartEventSyncOptions {
|
|
344
|
+
hqRoot: string;
|
|
345
|
+
/** Vault API base URL (the runner's DEFAULT_VAULT_API_URL). */
|
|
346
|
+
apiUrl: string;
|
|
347
|
+
/** Cognito access-token source (getter for long-running daemons). */
|
|
348
|
+
authToken: AuthTokenSource;
|
|
349
|
+
/** This device's stable id (getOrCreateMachineId). */
|
|
350
|
+
deviceId: string;
|
|
351
|
+
/**
|
|
352
|
+
* Resolve the caller's tenant id (canonical person `prs_*` uid). The server
|
|
353
|
+
* rejects publishes whose `originTenantId` mismatches the JWT principal, so
|
|
354
|
+
* this MUST be the same identity the JWT resolves to.
|
|
355
|
+
*/
|
|
356
|
+
resolveTenantId: () => Promise<string>;
|
|
357
|
+
/**
|
|
358
|
+
* The already-routed targeted-pull bridge (the runner's receiverSyncFn,
|
|
359
|
+
* funneled through its runGuarded mutex). Self-echo filtering happens HERE,
|
|
360
|
+
* before this is invoked.
|
|
361
|
+
*/
|
|
362
|
+
syncFn: SyncEngineFn;
|
|
363
|
+
/** Diagnostic logger (one line per lifecycle event). Default: console.error. */
|
|
364
|
+
log?: EventSyncLog;
|
|
365
|
+
// ── test seams ──
|
|
366
|
+
subscribe?: (deviceId: string) => Promise<SubscribeSyncResponse>;
|
|
367
|
+
buildSqs?: (resp: SubscribeSyncResponse) => SqsClientLike;
|
|
368
|
+
transport?: HttpPushTransport;
|
|
369
|
+
now?: () => number;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export interface EventSyncHandles {
|
|
373
|
+
/**
|
|
374
|
+
* Publish PushEvents for a settled change batch. Called by the runner
|
|
375
|
+
* AFTER the targeted push pass succeeds — an event must never announce
|
|
376
|
+
* bytes that are not in S3 yet. Fire-and-forget: failures are logged by
|
|
377
|
+
* the emitter's onError and the cadence poll covers the miss.
|
|
378
|
+
*/
|
|
379
|
+
publishBatch: (batch: TreeChangeBatch) => void;
|
|
380
|
+
/** The live receiver (already started). */
|
|
381
|
+
receiver: PushReceiver;
|
|
382
|
+
/** This device's id — the runner uses it nowhere else, exposed for logs. */
|
|
383
|
+
ownDeviceId: string;
|
|
384
|
+
/** Tear down transport + receiver (runner shutdown path). */
|
|
385
|
+
dispose: () => Promise<void>;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Bring up the publish + receive legs. Returns `null` (poll-only degradation)
|
|
390
|
+
* on ANY startup failure — subscribe 5xx, tenant resolution failure, etc. —
|
|
391
|
+
* after logging the reason. The caller treats `null` as "today's behavior".
|
|
392
|
+
*/
|
|
393
|
+
export async function startEventSync(
|
|
394
|
+
opts: StartEventSyncOptions,
|
|
395
|
+
): Promise<EventSyncHandles | null> {
|
|
396
|
+
const log = opts.log ?? ((m: string) => console.error(m));
|
|
397
|
+
try {
|
|
398
|
+
const tenantId = await opts.resolveTenantId();
|
|
399
|
+
const deviceId = opts.deviceId;
|
|
400
|
+
|
|
401
|
+
// ── publish leg (US-017) ──
|
|
402
|
+
const transport =
|
|
403
|
+
opts.transport ??
|
|
404
|
+
new HttpPushTransport({
|
|
405
|
+
apiUrl: opts.apiUrl,
|
|
406
|
+
pushPath: "/v1/sync/push",
|
|
407
|
+
authToken: opts.authToken,
|
|
408
|
+
});
|
|
409
|
+
await transport.start();
|
|
410
|
+
const emitter = new PushEventEmitter({
|
|
411
|
+
originTenantId: tenantId,
|
|
412
|
+
originDeviceId: deviceId,
|
|
413
|
+
transport,
|
|
414
|
+
// The rollout gate (resolveEventSync) already said yes — the emitter's
|
|
415
|
+
// per-tenant env flag is a server-side convention, not the client gate.
|
|
416
|
+
flagProvider: { isEnabled: () => true },
|
|
417
|
+
// Wall-clock sequence numbers: monotonic per device ACROSS daemon
|
|
418
|
+
// restarts (the emitter's default in-process counter restarts at 0,
|
|
419
|
+
// which would make peers' highest-seq dedupe drop every post-restart
|
|
420
|
+
// event). Receiver dedupe is per-path highest-seq, so ms-epoch values
|
|
421
|
+
// also stay comparable when two devices touch the same path.
|
|
422
|
+
getSequenceNumber: () => (opts.now ?? Date.now)(),
|
|
423
|
+
onError: (err, ctx) =>
|
|
424
|
+
log(
|
|
425
|
+
`event-sync: publish failed for ${ctx.relativePath ?? "?"}: ${err.message}`,
|
|
426
|
+
),
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// ── receive leg (US-018) ──
|
|
430
|
+
const subscribe =
|
|
431
|
+
opts.subscribe ??
|
|
432
|
+
((dev: string) =>
|
|
433
|
+
subscribeSyncReceive({
|
|
434
|
+
apiUrl: opts.apiUrl,
|
|
435
|
+
authToken: opts.authToken,
|
|
436
|
+
deviceId: dev,
|
|
437
|
+
}));
|
|
438
|
+
const initial = await subscribe(deviceId);
|
|
439
|
+
const sqs = createRefreshingSqsClient({
|
|
440
|
+
initial,
|
|
441
|
+
subscribe: () => subscribe(deviceId),
|
|
442
|
+
buildSqs: opts.buildSqs,
|
|
443
|
+
now: opts.now,
|
|
444
|
+
});
|
|
445
|
+
const receiver = new SqsPushReceiver({
|
|
446
|
+
tenantId,
|
|
447
|
+
queueUrl: initial.queueUrl,
|
|
448
|
+
sqs,
|
|
449
|
+
// Self-echo filter: this device's own publishes fan out to its own
|
|
450
|
+
// queue too — pulling them back is wasted work at best and a conflict
|
|
451
|
+
// source at worst. Skip before bridging into the sync engine.
|
|
452
|
+
syncFn: async (ctx) => {
|
|
453
|
+
if (ctx.event.originDeviceId === deviceId) return;
|
|
454
|
+
await opts.syncFn(ctx);
|
|
455
|
+
},
|
|
456
|
+
// The client rollout gate is the authority; the env-driven per-tenant
|
|
457
|
+
// flag provider is a server-side convention not set on user machines.
|
|
458
|
+
enabled: true,
|
|
459
|
+
});
|
|
460
|
+
await receiver.start();
|
|
461
|
+
|
|
462
|
+
log(
|
|
463
|
+
`event-sync: live (tenant=${tenantId} device=${deviceId} queue=${initial.queueUrl})`,
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
publishBatch: (batch) => void emitter.emitForBatch(batch),
|
|
468
|
+
receiver,
|
|
469
|
+
ownDeviceId: deviceId,
|
|
470
|
+
dispose: async () => {
|
|
471
|
+
await receiver.dispose().catch(() => undefined);
|
|
472
|
+
await transport.dispose().catch(() => undefined);
|
|
473
|
+
},
|
|
474
|
+
};
|
|
475
|
+
} catch (err) {
|
|
476
|
+
log(
|
|
477
|
+
`event-sync: startup failed, continuing poll-only: ${err instanceof Error ? err.message : String(err)}`,
|
|
478
|
+
);
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
@@ -500,3 +500,129 @@ describe("US-010: cross-tenant isolation (BLOCKING — failure is P0)", () => {
|
|
|
500
500
|
5_000,
|
|
501
501
|
);
|
|
502
502
|
});
|
|
503
|
+
|
|
504
|
+
// ---------------------------------------------------------------------------
|
|
505
|
+
// Phase 3 extension (US-018) — receive path with VENDED credentials
|
|
506
|
+
// ---------------------------------------------------------------------------
|
|
507
|
+
//
|
|
508
|
+
// US-016 moved queue access from "client holds no way to read" to "subscribe
|
|
509
|
+
// vends per-device STS creds scoped to exactly one queue ARN". The server-
|
|
510
|
+
// side policy-document tests pin the IAM scoping; THIS extension pins the
|
|
511
|
+
// client half of the same invariant: the startEventSync wiring polls ONLY
|
|
512
|
+
// the queue URL its own subscribe returned — a tenant-B (or peer-device)
|
|
513
|
+
// queue is never touched, and a same-tenant peer's events flow while the
|
|
514
|
+
// device's own echoes are filtered.
|
|
515
|
+
|
|
516
|
+
import {
|
|
517
|
+
startEventSync,
|
|
518
|
+
type SubscribeSyncResponse,
|
|
519
|
+
} from "../../../src/sync/event-sync.js";
|
|
520
|
+
import type {
|
|
521
|
+
SqsClientLike,
|
|
522
|
+
PushReceiverContext,
|
|
523
|
+
} from "../../../src/sync/push-receiver.js";
|
|
524
|
+
import type { PushEvent } from "../../../src/sync/push-event.js";
|
|
525
|
+
|
|
526
|
+
describe("US-018: receive path with vended credentials (isolation extension)", () => {
|
|
527
|
+
function vendedResponse(tenant: string, device: string): SubscribeSyncResponse {
|
|
528
|
+
return {
|
|
529
|
+
queueUrl: `https://sqs.us-east-1.amazonaws.com/000000000000/sync-push-${tenant}-${device}`,
|
|
530
|
+
region: "us-east-1",
|
|
531
|
+
credentials: {
|
|
532
|
+
accessKeyId: `AKIA-${tenant}-${device}`,
|
|
533
|
+
secretAccessKey: "s",
|
|
534
|
+
sessionToken: "t",
|
|
535
|
+
expiration: "2099-01-01T00:00:00.000Z",
|
|
536
|
+
},
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function phase3Event(overrides: Partial<PushEvent>): PushEvent {
|
|
541
|
+
return {
|
|
542
|
+
relativePath: "companies/indigo/docs/x.md",
|
|
543
|
+
contentHash: `sha256:${"b".repeat(64)}`,
|
|
544
|
+
mtime: "2026-06-10T12:00:00.000Z",
|
|
545
|
+
originDeviceId: "peer",
|
|
546
|
+
originTenantId: "prs_A",
|
|
547
|
+
sequenceNumber: 10,
|
|
548
|
+
eventTimestamp: "2026-06-10T12:00:00.000Z",
|
|
549
|
+
...overrides,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
it("polls ONLY the queue URL its own subscribe returned, with its own vended creds", async () => {
|
|
554
|
+
// The suite-level beforeEach arms fake timers for the simulated-60s
|
|
555
|
+
// tests; this extension drives a REAL async poll loop (vi.waitFor +
|
|
556
|
+
// receiver microtasks), so switch back. afterEach re-reals anyway.
|
|
557
|
+
vi.useRealTimers();
|
|
558
|
+
const polledQueues = new Set<string>();
|
|
559
|
+
const credsUsed = new Set<string>();
|
|
560
|
+
let deliver: ((body: string) => void) | undefined;
|
|
561
|
+
|
|
562
|
+
const pulled: PushEvent[] = [];
|
|
563
|
+
const handles = await startEventSync({
|
|
564
|
+
hqRoot: "/tmp/hq-a1",
|
|
565
|
+
apiUrl: "https://api.example.com",
|
|
566
|
+
authToken: "jwt-A1",
|
|
567
|
+
deviceId: "devA1",
|
|
568
|
+
resolveTenantId: async () => "prs_A",
|
|
569
|
+
syncFn: async (ctx: PushReceiverContext) => {
|
|
570
|
+
pulled.push(ctx.event);
|
|
571
|
+
},
|
|
572
|
+
subscribe: async () => vendedResponse("prs_A", "devA1"),
|
|
573
|
+
buildSqs: (resp): SqsClientLike => {
|
|
574
|
+
credsUsed.add(resp.credentials.accessKeyId);
|
|
575
|
+
return {
|
|
576
|
+
receiveMessage: ({ queueUrl, signal }) =>
|
|
577
|
+
new Promise((resolve) => {
|
|
578
|
+
polledQueues.add(queueUrl);
|
|
579
|
+
signal.addEventListener(
|
|
580
|
+
"abort",
|
|
581
|
+
() => resolve({ messages: [] }),
|
|
582
|
+
{ once: true },
|
|
583
|
+
);
|
|
584
|
+
deliver = (body: string) => {
|
|
585
|
+
deliver = undefined;
|
|
586
|
+
resolve({ messages: [{ Body: body, ReceiptHandle: "rh" }] });
|
|
587
|
+
};
|
|
588
|
+
}),
|
|
589
|
+
deleteMessage: async () => {},
|
|
590
|
+
};
|
|
591
|
+
},
|
|
592
|
+
log: () => {},
|
|
593
|
+
});
|
|
594
|
+
expect(handles).not.toBeNull();
|
|
595
|
+
|
|
596
|
+
await vi.waitFor(() => {
|
|
597
|
+
if (!deliver) throw new Error("not polling yet");
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
// The ONLY queue ever polled is this device's own vended queue, with this
|
|
601
|
+
// device's own vended credentials — the structural mirror of the server
|
|
602
|
+
// session policy (single queue ARN, nothing else).
|
|
603
|
+
expect([...polledQueues]).toEqual([
|
|
604
|
+
"https://sqs.us-east-1.amazonaws.com/000000000000/sync-push-prs_A-devA1",
|
|
605
|
+
]);
|
|
606
|
+
expect([...credsUsed]).toEqual(["AKIA-prs_A-devA1"]);
|
|
607
|
+
|
|
608
|
+
// A same-tenant PEER event is bridged into the targeted pull…
|
|
609
|
+
deliver!(
|
|
610
|
+
JSON.stringify(phase3Event({ originDeviceId: "devA2", sequenceNumber: 11 })),
|
|
611
|
+
);
|
|
612
|
+
await vi.waitFor(() => expect(pulled).toHaveLength(1));
|
|
613
|
+
expect(pulled[0].originDeviceId).toBe("devA2");
|
|
614
|
+
|
|
615
|
+
// …while the device's OWN echo is filtered before the sync engine.
|
|
616
|
+
await vi.waitFor(() => {
|
|
617
|
+
if (!deliver) throw new Error("not polling again yet");
|
|
618
|
+
});
|
|
619
|
+
deliver!(
|
|
620
|
+
JSON.stringify(phase3Event({ originDeviceId: "devA1", sequenceNumber: 12 })),
|
|
621
|
+
);
|
|
622
|
+
// Give the loop a beat — the echo must NOT add a pull.
|
|
623
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
624
|
+
expect(pulled).toHaveLength(1);
|
|
625
|
+
|
|
626
|
+
await handles!.dispose();
|
|
627
|
+
});
|
|
628
|
+
});
|