@indigoai-us/hq-cloud 5.25.0 → 5.27.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/.github/workflows/ci.yml +34 -0
- package/dist/bin/sync-runner.d.ts +138 -1
- package/dist/bin/sync-runner.d.ts.map +1 -1
- package/dist/bin/sync-runner.js +288 -16
- package/dist/bin/sync-runner.js.map +1 -1
- package/dist/bin/sync-runner.test.js +372 -1
- package/dist/bin/sync-runner.test.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/sync/feature-flags.d.ts +136 -0
- package/dist/sync/feature-flags.d.ts.map +1 -0
- package/dist/sync/feature-flags.js +160 -0
- package/dist/sync/feature-flags.js.map +1 -0
- package/dist/sync/feature-flags.test.d.ts +24 -0
- package/dist/sync/feature-flags.test.d.ts.map +1 -0
- package/dist/sync/feature-flags.test.js +330 -0
- package/dist/sync/feature-flags.test.js.map +1 -0
- package/dist/sync/index.d.ts +19 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/index.js +13 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/sync/logger.d.ts +61 -0
- package/dist/sync/logger.d.ts.map +1 -0
- package/dist/sync/logger.js +51 -0
- package/dist/sync/logger.js.map +1 -0
- package/dist/sync/logger.test.d.ts +19 -0
- package/dist/sync/logger.test.d.ts.map +1 -0
- package/dist/sync/logger.test.js +199 -0
- package/dist/sync/logger.test.js.map +1 -0
- package/dist/sync/metrics.d.ts +89 -0
- package/dist/sync/metrics.d.ts.map +1 -0
- package/dist/sync/metrics.js +105 -0
- package/dist/sync/metrics.js.map +1 -0
- package/dist/sync/metrics.test.d.ts +19 -0
- package/dist/sync/metrics.test.d.ts.map +1 -0
- package/dist/sync/metrics.test.js +280 -0
- package/dist/sync/metrics.test.js.map +1 -0
- package/dist/sync/push-event.d.ts +110 -0
- package/dist/sync/push-event.d.ts.map +1 -0
- package/dist/sync/push-event.js +153 -0
- package/dist/sync/push-event.js.map +1 -0
- package/dist/sync/push-event.test.d.ts +15 -0
- package/dist/sync/push-event.test.d.ts.map +1 -0
- package/dist/sync/push-event.test.js +188 -0
- package/dist/sync/push-event.test.js.map +1 -0
- package/dist/sync/push-receiver.d.ts +442 -0
- package/dist/sync/push-receiver.d.ts.map +1 -0
- package/dist/sync/push-receiver.js +782 -0
- package/dist/sync/push-receiver.js.map +1 -0
- package/dist/sync/push-receiver.test.d.ts +25 -0
- package/dist/sync/push-receiver.test.d.ts.map +1 -0
- package/dist/sync/push-receiver.test.js +477 -0
- package/dist/sync/push-receiver.test.js.map +1 -0
- package/dist/sync/push-transport.d.ts +150 -0
- package/dist/sync/push-transport.d.ts.map +1 -0
- package/dist/sync/push-transport.js +150 -0
- package/dist/sync/push-transport.js.map +1 -0
- package/dist/watcher.d.ts +271 -0
- package/dist/watcher.d.ts.map +1 -1
- package/dist/watcher.js +480 -3
- package/dist/watcher.js.map +1 -1
- package/dist/watcher.test.d.ts +2 -0
- package/dist/watcher.test.d.ts.map +1 -0
- package/dist/watcher.test.js +334 -0
- package/dist/watcher.test.js.map +1 -0
- package/package.json +10 -5
- package/src/bin/sync-runner.test.ts +487 -1
- package/src/bin/sync-runner.ts +406 -9
- package/src/index.ts +38 -0
- package/src/sync/feature-flags.test.ts +392 -0
- package/src/sync/feature-flags.ts +229 -0
- package/src/sync/index.ts +74 -0
- package/src/sync/logger.test.ts +241 -0
- package/src/sync/logger.ts +79 -0
- package/src/sync/metrics.test.ts +380 -0
- package/src/sync/metrics.ts +158 -0
- package/src/sync/push-event.test.ts +224 -0
- package/src/sync/push-event.ts +208 -0
- package/src/sync/push-receiver.test.ts +545 -0
- package/src/sync/push-receiver.ts +1077 -0
- package/src/sync/push-transport.ts +231 -0
- package/src/watcher.test.ts +388 -0
- package/src/watcher.ts +672 -4
- package/test/e2e/sync/cross-tenant-isolation.test.ts +502 -0
- package/test/e2e/watcher-real-chokidar.test.ts +105 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Component-tagged logger factory for the hq-cloud sync subsystem
|
|
3
|
+
* (project event-driven-sync-menubar US-011).
|
|
4
|
+
*
|
|
5
|
+
* Every log line emitted from a sync module SHOULD carry a `component` field
|
|
6
|
+
* so operators can grep / route by subsystem in the aggregated JSON stream.
|
|
7
|
+
* `createLogger({ component: "sync-watcher" })` is the canonical entry point —
|
|
8
|
+
* modules call this once at module-load time and use the returned logger for
|
|
9
|
+
* the lifetime of the process.
|
|
10
|
+
*
|
|
11
|
+
* The 3-log diagnostic chain (US-011)
|
|
12
|
+
* ───────────────────────────────────
|
|
13
|
+
* Three correlated log lines share a single `sequenceNumber` join key so an
|
|
14
|
+
* operator can walk one event end-to-end:
|
|
15
|
+
* 1. `event=watcher.emit` — client push side (US-008 PushEventEmitter)
|
|
16
|
+
* 2. `event=push.receive` — server side (hq-pro; context only here)
|
|
17
|
+
* 3. `event=fanout.receive` — client fanout-receive side (US-009 receiver)
|
|
18
|
+
* The watcher.emit + fanout.receive halves live in this client package; the
|
|
19
|
+
* push.receive half lives server-side in hq-pro. All three stamp the same
|
|
20
|
+
* `sequenceNumber` for log-chain correlation.
|
|
21
|
+
*
|
|
22
|
+
* Output format: pino's default newline-delimited JSON. No transports, no
|
|
23
|
+
* pretty-printing — the daemon consumes the raw JSON stream. (Operators who
|
|
24
|
+
* want pretty output pipe through `pino-pretty` themselves.)
|
|
25
|
+
*
|
|
26
|
+
* Destination injection: tests pass a `destination` stream so they can capture
|
|
27
|
+
* log lines and assert on them. Production callers omit it and pino defaults
|
|
28
|
+
* to `process.stdout`.
|
|
29
|
+
*
|
|
30
|
+
* Adapted from indigoai-us/hq-pro PR #112 (src/sync/logger.ts) into
|
|
31
|
+
* @indigoai-us/hq-cloud (Path B).
|
|
32
|
+
*/
|
|
33
|
+
import { type DestinationStream, type Level, type Logger } from "pino";
|
|
34
|
+
export interface CreateLoggerOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Component tag stamped on every log line as `"component": <value>`.
|
|
37
|
+
* Required so daemon / watcher / receiver lines never appear in the stream
|
|
38
|
+
* untagged.
|
|
39
|
+
*/
|
|
40
|
+
component: string;
|
|
41
|
+
/**
|
|
42
|
+
* Optional pino level. Default: pino's own default (`info`). Set via
|
|
43
|
+
* `LOG_LEVEL` env var, command-line flag, or test injection.
|
|
44
|
+
*/
|
|
45
|
+
level?: Level;
|
|
46
|
+
/**
|
|
47
|
+
* Optional pino destination. Default: `process.stdout`. Tests inject a
|
|
48
|
+
* memory stream here to capture lines for assertion.
|
|
49
|
+
*/
|
|
50
|
+
destination?: DestinationStream;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Build a pino logger pre-bound to a `component` tag.
|
|
54
|
+
*
|
|
55
|
+
* Use this — not a bare `pino()` call — so every sync module's log lines carry
|
|
56
|
+
* the tag uniformly. Adding more bound fields (e.g. `deviceId`, `tenantId`) is
|
|
57
|
+
* a `logger.child({ ... })` call away.
|
|
58
|
+
*/
|
|
59
|
+
export declare function createLogger(opts: CreateLoggerOptions): Logger;
|
|
60
|
+
export type { Logger } from "pino";
|
|
61
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/sync/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,EAEL,KAAK,iBAAiB,EACtB,KAAK,KAAK,EACV,KAAK,MAAM,EAEZ,MAAM,MAAM,CAAC;AAEd,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;IACd;;;OAGG;IACH,WAAW,CAAC,EAAE,iBAAiB,CAAC;CACjC;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,MAAM,CAS9D;AAED,YAAY,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Component-tagged logger factory for the hq-cloud sync subsystem
|
|
3
|
+
* (project event-driven-sync-menubar US-011).
|
|
4
|
+
*
|
|
5
|
+
* Every log line emitted from a sync module SHOULD carry a `component` field
|
|
6
|
+
* so operators can grep / route by subsystem in the aggregated JSON stream.
|
|
7
|
+
* `createLogger({ component: "sync-watcher" })` is the canonical entry point —
|
|
8
|
+
* modules call this once at module-load time and use the returned logger for
|
|
9
|
+
* the lifetime of the process.
|
|
10
|
+
*
|
|
11
|
+
* The 3-log diagnostic chain (US-011)
|
|
12
|
+
* ───────────────────────────────────
|
|
13
|
+
* Three correlated log lines share a single `sequenceNumber` join key so an
|
|
14
|
+
* operator can walk one event end-to-end:
|
|
15
|
+
* 1. `event=watcher.emit` — client push side (US-008 PushEventEmitter)
|
|
16
|
+
* 2. `event=push.receive` — server side (hq-pro; context only here)
|
|
17
|
+
* 3. `event=fanout.receive` — client fanout-receive side (US-009 receiver)
|
|
18
|
+
* The watcher.emit + fanout.receive halves live in this client package; the
|
|
19
|
+
* push.receive half lives server-side in hq-pro. All three stamp the same
|
|
20
|
+
* `sequenceNumber` for log-chain correlation.
|
|
21
|
+
*
|
|
22
|
+
* Output format: pino's default newline-delimited JSON. No transports, no
|
|
23
|
+
* pretty-printing — the daemon consumes the raw JSON stream. (Operators who
|
|
24
|
+
* want pretty output pipe through `pino-pretty` themselves.)
|
|
25
|
+
*
|
|
26
|
+
* Destination injection: tests pass a `destination` stream so they can capture
|
|
27
|
+
* log lines and assert on them. Production callers omit it and pino defaults
|
|
28
|
+
* to `process.stdout`.
|
|
29
|
+
*
|
|
30
|
+
* Adapted from indigoai-us/hq-pro PR #112 (src/sync/logger.ts) into
|
|
31
|
+
* @indigoai-us/hq-cloud (Path B).
|
|
32
|
+
*/
|
|
33
|
+
import { pino, } from "pino";
|
|
34
|
+
/**
|
|
35
|
+
* Build a pino logger pre-bound to a `component` tag.
|
|
36
|
+
*
|
|
37
|
+
* Use this — not a bare `pino()` call — so every sync module's log lines carry
|
|
38
|
+
* the tag uniformly. Adding more bound fields (e.g. `deviceId`, `tenantId`) is
|
|
39
|
+
* a `logger.child({ ... })` call away.
|
|
40
|
+
*/
|
|
41
|
+
export function createLogger(opts) {
|
|
42
|
+
const { component, level, destination } = opts;
|
|
43
|
+
const pinoOpts = {
|
|
44
|
+
base: { component },
|
|
45
|
+
...(level === undefined ? {} : { level }),
|
|
46
|
+
};
|
|
47
|
+
return destination === undefined
|
|
48
|
+
? pino(pinoOpts)
|
|
49
|
+
: pino(pinoOpts, destination);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/sync/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,EACL,IAAI,GAKL,MAAM,MAAM,CAAC;AAqBd;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,IAAyB;IACpD,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;IAC/C,MAAM,QAAQ,GAAkB;QAC9B,IAAI,EAAE,EAAE,SAAS,EAAE;QACnB,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;KAC1C,CAAC;IACF,OAAO,WAAW,KAAK,SAAS;QAC9B,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC;QAChB,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* US-011 — unit tests for the pino logger factory + the 3-log diagnostic
|
|
3
|
+
* chain's CLIENT-side correlated fields.
|
|
4
|
+
*
|
|
5
|
+
* The PRD requires three correlated logs sharing the same `sequenceNumber`:
|
|
6
|
+
* 1. `watcher.emit` — client push (US-008 PushEventEmitter) [client]
|
|
7
|
+
* 2. `push.receive` — server (hq-pro) [server]
|
|
8
|
+
* 3. `fanout.receive` — client fanout-receive (US-009 receiver) [client]
|
|
9
|
+
*
|
|
10
|
+
* This package owns the two CLIENT-side links. These tests prove:
|
|
11
|
+
* - `createLogger` stamps the `component` tag on every line and respects
|
|
12
|
+
* level + injected destination.
|
|
13
|
+
* - the watcher emits `event=watcher.emit` carrying `sequenceNumber`.
|
|
14
|
+
* - the receiver emits `event=fanout.receive` carrying the SAME
|
|
15
|
+
* `sequenceNumber` → an operator can join the two client links (and the
|
|
16
|
+
* server `push.receive` line, which carries the same key) end-to-end.
|
|
17
|
+
*/
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=logger.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.test.d.ts","sourceRoot":"","sources":["../../src/sync/logger.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* US-011 — unit tests for the pino logger factory + the 3-log diagnostic
|
|
3
|
+
* chain's CLIENT-side correlated fields.
|
|
4
|
+
*
|
|
5
|
+
* The PRD requires three correlated logs sharing the same `sequenceNumber`:
|
|
6
|
+
* 1. `watcher.emit` — client push (US-008 PushEventEmitter) [client]
|
|
7
|
+
* 2. `push.receive` — server (hq-pro) [server]
|
|
8
|
+
* 3. `fanout.receive` — client fanout-receive (US-009 receiver) [client]
|
|
9
|
+
*
|
|
10
|
+
* This package owns the two CLIENT-side links. These tests prove:
|
|
11
|
+
* - `createLogger` stamps the `component` tag on every line and respects
|
|
12
|
+
* level + injected destination.
|
|
13
|
+
* - the watcher emits `event=watcher.emit` carrying `sequenceNumber`.
|
|
14
|
+
* - the receiver emits `event=fanout.receive` carrying the SAME
|
|
15
|
+
* `sequenceNumber` → an operator can join the two client links (and the
|
|
16
|
+
* server `push.receive` line, which carries the same key) end-to-end.
|
|
17
|
+
*/
|
|
18
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
19
|
+
import { tmpdir } from "node:os";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
import { Writable } from "node:stream";
|
|
22
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
23
|
+
import { createLogger } from "./logger.js";
|
|
24
|
+
import { encodePushEvent } from "./push-event.js";
|
|
25
|
+
import { NoopPushTransport } from "./push-transport.js";
|
|
26
|
+
import { StaticFlagProvider } from "./feature-flags.js";
|
|
27
|
+
import { SqsPushReceiver, } from "./push-receiver.js";
|
|
28
|
+
import { PushEventEmitter } from "../watcher.js";
|
|
29
|
+
const TENANT = "tenant-indigo";
|
|
30
|
+
// ── Capture stream + JSON line reader ─────────────────────────────────────────
|
|
31
|
+
function captureStream() {
|
|
32
|
+
const chunks = [];
|
|
33
|
+
const stream = new Writable({
|
|
34
|
+
write(chunk, _enc, cb) {
|
|
35
|
+
chunks.push(typeof chunk === "string" ? chunk : chunk.toString("utf8"));
|
|
36
|
+
cb();
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
stream,
|
|
41
|
+
lines: () => chunks
|
|
42
|
+
.join("")
|
|
43
|
+
.split("\n")
|
|
44
|
+
.filter((s) => s.length > 0)
|
|
45
|
+
.map((s) => JSON.parse(s)),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async function until(predicate, timeoutMs = 1000) {
|
|
49
|
+
const start = Date.now();
|
|
50
|
+
while (!predicate()) {
|
|
51
|
+
if (Date.now() - start > timeoutMs)
|
|
52
|
+
throw new Error("until() timed out");
|
|
53
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function makeEvent(overrides = {}) {
|
|
57
|
+
return {
|
|
58
|
+
relativePath: "companies/indigo/notes.md",
|
|
59
|
+
contentHash: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
60
|
+
mtime: "2026-05-21T12:34:56.000Z",
|
|
61
|
+
originDeviceId: "device-A",
|
|
62
|
+
originTenantId: TENANT,
|
|
63
|
+
sequenceNumber: 1,
|
|
64
|
+
eventTimestamp: "2026-05-21T12:34:56.000Z",
|
|
65
|
+
...overrides,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
class OneBatchSqs {
|
|
69
|
+
batch;
|
|
70
|
+
constructor(messages) {
|
|
71
|
+
this.batch = messages;
|
|
72
|
+
}
|
|
73
|
+
async receiveMessage(args) {
|
|
74
|
+
if (this.batch) {
|
|
75
|
+
const b = this.batch;
|
|
76
|
+
this.batch = null;
|
|
77
|
+
return { messages: b };
|
|
78
|
+
}
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
if (args.signal.aborted)
|
|
81
|
+
return resolve({ messages: [] });
|
|
82
|
+
const t = setTimeout(() => resolve({ messages: [] }), 5);
|
|
83
|
+
t.unref?.();
|
|
84
|
+
args.signal.addEventListener("abort", () => {
|
|
85
|
+
clearTimeout(t);
|
|
86
|
+
resolve({ messages: [] });
|
|
87
|
+
}, { once: true });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async deleteMessage() {
|
|
91
|
+
/* no-op */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// ── createLogger ──────────────────────────────────────────────────────────────
|
|
95
|
+
describe("createLogger", () => {
|
|
96
|
+
it("stamps the `component` tag on every line", () => {
|
|
97
|
+
const { stream, lines } = captureStream();
|
|
98
|
+
const log = createLogger({
|
|
99
|
+
component: "sync-watcher",
|
|
100
|
+
destination: stream,
|
|
101
|
+
level: "info",
|
|
102
|
+
});
|
|
103
|
+
log.info({ event: "x" }, "hello");
|
|
104
|
+
log.warn({ event: "y" }, "world");
|
|
105
|
+
const out = lines();
|
|
106
|
+
expect(out).toHaveLength(2);
|
|
107
|
+
expect(out.every((l) => l.component === "sync-watcher")).toBe(true);
|
|
108
|
+
});
|
|
109
|
+
it("respects the injected level (debug below info is dropped at info)", () => {
|
|
110
|
+
const { stream, lines } = captureStream();
|
|
111
|
+
const log = createLogger({
|
|
112
|
+
component: "c",
|
|
113
|
+
destination: stream,
|
|
114
|
+
level: "info",
|
|
115
|
+
});
|
|
116
|
+
log.debug({ event: "d" }, "dropped");
|
|
117
|
+
log.info({ event: "i" }, "kept");
|
|
118
|
+
const out = lines();
|
|
119
|
+
expect(out).toHaveLength(1);
|
|
120
|
+
expect(out[0].event).toBe("i");
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
// ── 3-log chain: client-side correlated fields share sequenceNumber ───────────
|
|
124
|
+
describe("3-log diagnostic chain (client links share sequenceNumber)", () => {
|
|
125
|
+
let tmp;
|
|
126
|
+
let receiver;
|
|
127
|
+
afterEach(async () => {
|
|
128
|
+
if (receiver) {
|
|
129
|
+
await receiver.dispose();
|
|
130
|
+
receiver = undefined;
|
|
131
|
+
}
|
|
132
|
+
if (tmp) {
|
|
133
|
+
await rm(tmp, { recursive: true, force: true });
|
|
134
|
+
tmp = undefined;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
it("watcher.emit and fanout.receive carry the SAME sequenceNumber", async () => {
|
|
138
|
+
const SEQ = 4242;
|
|
139
|
+
// ── Link 1: watcher.emit (client push side) ──────────────────────────
|
|
140
|
+
tmp = await mkdtemp(join(tmpdir(), "us011-logger-"));
|
|
141
|
+
const absPath = join(tmp, "notes.md");
|
|
142
|
+
await writeFile(absPath, "hello world", "utf8");
|
|
143
|
+
const watcherCap = captureStream();
|
|
144
|
+
const watcherLog = createLogger({
|
|
145
|
+
component: "sync-watcher",
|
|
146
|
+
destination: watcherCap.stream,
|
|
147
|
+
level: "info",
|
|
148
|
+
});
|
|
149
|
+
const emitter = new PushEventEmitter({
|
|
150
|
+
originTenantId: TENANT,
|
|
151
|
+
originDeviceId: "device-A",
|
|
152
|
+
transport: new NoopPushTransport(),
|
|
153
|
+
flagProvider: new StaticFlagProvider([TENANT]),
|
|
154
|
+
getSequenceNumber: () => SEQ,
|
|
155
|
+
logger: watcherLog,
|
|
156
|
+
});
|
|
157
|
+
const batch = {
|
|
158
|
+
paths: new Map([[absPath, "companies/indigo/notes.md"]]),
|
|
159
|
+
};
|
|
160
|
+
await emitter.emitForBatch(batch);
|
|
161
|
+
const emitLine = watcherCap
|
|
162
|
+
.lines()
|
|
163
|
+
.find((l) => l.event === "watcher.emit");
|
|
164
|
+
expect(emitLine).toBeDefined();
|
|
165
|
+
expect(emitLine.sequenceNumber).toBe(SEQ);
|
|
166
|
+
expect(emitLine.component).toBe("sync-watcher");
|
|
167
|
+
// ── Link 3: fanout.receive (client receive side) ─────────────────────
|
|
168
|
+
const receiverCap = captureStream();
|
|
169
|
+
const receiverLog = createLogger({
|
|
170
|
+
component: "sync-receiver",
|
|
171
|
+
destination: receiverCap.stream,
|
|
172
|
+
level: "info",
|
|
173
|
+
});
|
|
174
|
+
const event = makeEvent({ sequenceNumber: SEQ });
|
|
175
|
+
receiver = new SqsPushReceiver({
|
|
176
|
+
tenantId: TENANT,
|
|
177
|
+
queueUrl: "https://sqs.local/q",
|
|
178
|
+
sqs: new OneBatchSqs([{ Body: encodePushEvent(event), ReceiptHandle: "rh" }]),
|
|
179
|
+
syncFn: async () => {
|
|
180
|
+
/* success */
|
|
181
|
+
},
|
|
182
|
+
enabled: true,
|
|
183
|
+
logger: receiverLog,
|
|
184
|
+
// Inject a no-op metric so no real AWS is touched.
|
|
185
|
+
publishMetric: async () => { },
|
|
186
|
+
});
|
|
187
|
+
await receiver.start();
|
|
188
|
+
await until(() => receiverCap.lines().some((l) => l.event === "fanout.receive"));
|
|
189
|
+
const recvLine = receiverCap
|
|
190
|
+
.lines()
|
|
191
|
+
.find((l) => l.event === "fanout.receive");
|
|
192
|
+
expect(recvLine).toBeDefined();
|
|
193
|
+
expect(recvLine.sequenceNumber).toBe(SEQ);
|
|
194
|
+
expect(recvLine.component).toBe("sync-receiver");
|
|
195
|
+
// ── The join key matches across both client links ────────────────────
|
|
196
|
+
expect(emitLine.sequenceNumber).toBe(recvLine.sequenceNumber);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
//# sourceMappingURL=logger.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.test.js","sourceRoot":"","sources":["../../src/sync/logger.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAEzD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAkB,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EACL,eAAe,GAGhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAwB,MAAM,eAAe,CAAC;AAEvE,MAAM,MAAM,GAAG,eAAe,CAAC;AAE/B,iFAAiF;AAEjF,SAAS,aAAa;IACpB,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC;QAC1B,KAAK,CAAC,KAAK,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W,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,gBAAgB,CAAC,CAC9D,CAAC;QAEF,MAAM,QAAQ,GAAG,WAAW;aACzB,KAAK,EAAE;aACP,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,gBAAgB,CAAC,CAAC;QAC7C,MAAM,CAAC,QAAQ,CAAC,CAAC,WAAW,EAAE,CAAC;QAC/B,MAAM,CAAC,QAAS,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC3C,MAAM,CAAC,QAAS,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAElD,wEAAwE;QACxE,MAAM,CAAC,QAAS,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,QAAS,CAAC,cAAc,CAAC,CAAC;IAClE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sync pipeline metrics — CloudWatch custom metrics for the event-driven push
|
|
3
|
+
* receive loop (project event-driven-sync-menubar US-011).
|
|
4
|
+
*
|
|
5
|
+
* Publishes one `hq-cloud.sync.p95_latency_seconds` datum per successfully
|
|
6
|
+
* processed push event to the `HQPro/Sync` namespace, with a `TenantId`
|
|
7
|
+
* dimension so the dashboard widget can be filtered per tenant. CloudWatch
|
|
8
|
+
* aggregates p50/p95/p99 across the time window via the dashboard's
|
|
9
|
+
* `Statistics` setting; the receive-loop side just emits one raw value per
|
|
10
|
+
* event.
|
|
11
|
+
*
|
|
12
|
+
* Best-effort emission
|
|
13
|
+
* ────────────────────
|
|
14
|
+
* - module-level singleton CloudWatchClient with a `_setSyncCloudWatchClient`
|
|
15
|
+
* test seam
|
|
16
|
+
* - `publishSyncLatencyMetric` catches all errors and NEVER throws — a
|
|
17
|
+
* CloudWatch outage MUST NOT crash the sync receive loop. The cadence
|
|
18
|
+
* safety net still picks up missed work and metric blanks are recoverable;
|
|
19
|
+
* a crashed loop is not.
|
|
20
|
+
* - explicit `Unit` + `Timestamp` + `Dimensions` per datum
|
|
21
|
+
*
|
|
22
|
+
* Why latency (and not also "events received" / "events failed")?
|
|
23
|
+
* ───────────────────────────────────────────────────────────────
|
|
24
|
+
* US-011 AC#1 calls for the p95 latency metric specifically. The receive-loop's
|
|
25
|
+
* existing `processedCount` / log lines cover the count/failure dimensions for
|
|
26
|
+
* now; widening to additional metric names happens in a follow-up if the
|
|
27
|
+
* operator dashboard grows.
|
|
28
|
+
*
|
|
29
|
+
* Adapted from indigoai-us/hq-pro PR #112 (src/sync/metrics.ts) into
|
|
30
|
+
* @indigoai-us/hq-cloud (Path B).
|
|
31
|
+
*/
|
|
32
|
+
import { CloudWatchClient } from "@aws-sdk/client-cloudwatch";
|
|
33
|
+
import type { Logger } from "pino";
|
|
34
|
+
/**
|
|
35
|
+
* CloudWatch metric namespace for the sync pipeline. Matches the server-side
|
|
36
|
+
* namespace (hq-pro PR #112) so the dashboard + alarm cover the client path.
|
|
37
|
+
*/
|
|
38
|
+
export declare const SYNC_METRIC_NAMESPACE = "HQPro/Sync";
|
|
39
|
+
/**
|
|
40
|
+
* Metric name for per-event sync latency in seconds. The dashboard widget
|
|
41
|
+
* applies `Statistics: ["p95"]` to aggregate across the time window — the
|
|
42
|
+
* receive loop just emits one raw `Seconds` value per processed event.
|
|
43
|
+
*
|
|
44
|
+
* Name chosen to match the PRD's alarm threshold (`p95 > 10s`).
|
|
45
|
+
*/
|
|
46
|
+
export declare const SYNC_LATENCY_METRIC_NAME = "hq-cloud.sync.p95_latency_seconds";
|
|
47
|
+
/**
|
|
48
|
+
* One latency observation. `latencySeconds` is the wall-clock duration from
|
|
49
|
+
* save-on-A to visible-on-B (or, on the receive loop, the `syncFn(ctx)`
|
|
50
|
+
* duration); we ONLY publish on the success path so failed syncs don't skew
|
|
51
|
+
* p95 toward infinity.
|
|
52
|
+
*
|
|
53
|
+
* `relativePath` and `sequenceNumber` are NOT used as CloudWatch dimensions
|
|
54
|
+
* (cardinality explosion) — they're captured here for the optional debug log
|
|
55
|
+
* emitted on failure so operators can correlate back to the 3-log chain when
|
|
56
|
+
* investigating a spike.
|
|
57
|
+
*/
|
|
58
|
+
export interface SyncLatencyMetric {
|
|
59
|
+
tenantId: string;
|
|
60
|
+
relativePath: string;
|
|
61
|
+
sequenceNumber: number;
|
|
62
|
+
latencySeconds: number;
|
|
63
|
+
timestamp: Date;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Replace the CloudWatch client (for testing).
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
export declare function _setSyncCloudWatchClient(client: CloudWatchClient): void;
|
|
70
|
+
export interface PublishSyncLatencyMetricOptions {
|
|
71
|
+
/** Override the CloudWatch client (tests). Defaults to the module singleton. */
|
|
72
|
+
client?: CloudWatchClient;
|
|
73
|
+
/** Optional pino logger for emission failures. */
|
|
74
|
+
logger?: Logger;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Publish a single latency datum to CloudWatch.
|
|
78
|
+
*
|
|
79
|
+
* Best-effort: any error from the SDK is CAUGHT and logged. A CloudWatch outage
|
|
80
|
+
* MUST NOT crash the sync receive loop — the cadence safety net still picks up
|
|
81
|
+
* missed work, and metric blanks are recoverable; a crashed loop is not.
|
|
82
|
+
*
|
|
83
|
+
* Dimension: `TenantId` only. The `relativePath`/`sequenceNumber` fields on the
|
|
84
|
+
* input are intentionally NOT promoted to dimensions (cardinality), but they
|
|
85
|
+
* ride along on the failure log so an operator can correlate a missed datum to
|
|
86
|
+
* its 3-log chain entry.
|
|
87
|
+
*/
|
|
88
|
+
export declare function publishSyncLatencyMetric(metric: SyncLatencyMetric, opts?: PublishSyncLatencyMetricOptions): Promise<void>;
|
|
89
|
+
//# sourceMappingURL=metrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../../src/sync/metrics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,EACL,gBAAgB,EAGjB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAInC;;;GAGG;AACH,eAAO,MAAM,qBAAqB,eAAe,CAAC;AAElD;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,sCAAsC,CAAC;AAI5E;;;;;;;;;;GAUG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,IAAI,CAAC;CACjB;AAaD;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAEvE;AAID,MAAM,WAAW,+BAA+B;IAC9C,gFAAgF;IAChF,MAAM,CAAC,EAAE,gBAAgB,CAAC;IAC1B,kDAAkD;IAClD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,iBAAiB,EACzB,IAAI,GAAE,+BAAoC,GACzC,OAAO,CAAC,IAAI,CAAC,CAqCf"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sync pipeline metrics — CloudWatch custom metrics for the event-driven push
|
|
3
|
+
* receive loop (project event-driven-sync-menubar US-011).
|
|
4
|
+
*
|
|
5
|
+
* Publishes one `hq-cloud.sync.p95_latency_seconds` datum per successfully
|
|
6
|
+
* processed push event to the `HQPro/Sync` namespace, with a `TenantId`
|
|
7
|
+
* dimension so the dashboard widget can be filtered per tenant. CloudWatch
|
|
8
|
+
* aggregates p50/p95/p99 across the time window via the dashboard's
|
|
9
|
+
* `Statistics` setting; the receive-loop side just emits one raw value per
|
|
10
|
+
* event.
|
|
11
|
+
*
|
|
12
|
+
* Best-effort emission
|
|
13
|
+
* ────────────────────
|
|
14
|
+
* - module-level singleton CloudWatchClient with a `_setSyncCloudWatchClient`
|
|
15
|
+
* test seam
|
|
16
|
+
* - `publishSyncLatencyMetric` catches all errors and NEVER throws — a
|
|
17
|
+
* CloudWatch outage MUST NOT crash the sync receive loop. The cadence
|
|
18
|
+
* safety net still picks up missed work and metric blanks are recoverable;
|
|
19
|
+
* a crashed loop is not.
|
|
20
|
+
* - explicit `Unit` + `Timestamp` + `Dimensions` per datum
|
|
21
|
+
*
|
|
22
|
+
* Why latency (and not also "events received" / "events failed")?
|
|
23
|
+
* ───────────────────────────────────────────────────────────────
|
|
24
|
+
* US-011 AC#1 calls for the p95 latency metric specifically. The receive-loop's
|
|
25
|
+
* existing `processedCount` / log lines cover the count/failure dimensions for
|
|
26
|
+
* now; widening to additional metric names happens in a follow-up if the
|
|
27
|
+
* operator dashboard grows.
|
|
28
|
+
*
|
|
29
|
+
* Adapted from indigoai-us/hq-pro PR #112 (src/sync/metrics.ts) into
|
|
30
|
+
* @indigoai-us/hq-cloud (Path B).
|
|
31
|
+
*/
|
|
32
|
+
import { CloudWatchClient, PutMetricDataCommand, } from "@aws-sdk/client-cloudwatch";
|
|
33
|
+
// ── Constants ──────────────────────────────────────────────────────────────
|
|
34
|
+
/**
|
|
35
|
+
* CloudWatch metric namespace for the sync pipeline. Matches the server-side
|
|
36
|
+
* namespace (hq-pro PR #112) so the dashboard + alarm cover the client path.
|
|
37
|
+
*/
|
|
38
|
+
export const SYNC_METRIC_NAMESPACE = "HQPro/Sync";
|
|
39
|
+
/**
|
|
40
|
+
* Metric name for per-event sync latency in seconds. The dashboard widget
|
|
41
|
+
* applies `Statistics: ["p95"]` to aggregate across the time window — the
|
|
42
|
+
* receive loop just emits one raw `Seconds` value per processed event.
|
|
43
|
+
*
|
|
44
|
+
* Name chosen to match the PRD's alarm threshold (`p95 > 10s`).
|
|
45
|
+
*/
|
|
46
|
+
export const SYNC_LATENCY_METRIC_NAME = "hq-cloud.sync.p95_latency_seconds";
|
|
47
|
+
// ── Client ─────────────────────────────────────────────────────────────────
|
|
48
|
+
let _cwClient;
|
|
49
|
+
function getCloudWatchClient() {
|
|
50
|
+
if (!_cwClient) {
|
|
51
|
+
_cwClient = new CloudWatchClient({});
|
|
52
|
+
}
|
|
53
|
+
return _cwClient;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Replace the CloudWatch client (for testing).
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
export function _setSyncCloudWatchClient(client) {
|
|
60
|
+
_cwClient = client;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Publish a single latency datum to CloudWatch.
|
|
64
|
+
*
|
|
65
|
+
* Best-effort: any error from the SDK is CAUGHT and logged. A CloudWatch outage
|
|
66
|
+
* MUST NOT crash the sync receive loop — the cadence safety net still picks up
|
|
67
|
+
* missed work, and metric blanks are recoverable; a crashed loop is not.
|
|
68
|
+
*
|
|
69
|
+
* Dimension: `TenantId` only. The `relativePath`/`sequenceNumber` fields on the
|
|
70
|
+
* input are intentionally NOT promoted to dimensions (cardinality), but they
|
|
71
|
+
* ride along on the failure log so an operator can correlate a missed datum to
|
|
72
|
+
* its 3-log chain entry.
|
|
73
|
+
*/
|
|
74
|
+
export async function publishSyncLatencyMetric(metric, opts = {}) {
|
|
75
|
+
const datum = {
|
|
76
|
+
MetricName: SYNC_LATENCY_METRIC_NAME,
|
|
77
|
+
Value: metric.latencySeconds,
|
|
78
|
+
Unit: "Seconds",
|
|
79
|
+
Timestamp: metric.timestamp,
|
|
80
|
+
Dimensions: [{ Name: "TenantId", Value: metric.tenantId }],
|
|
81
|
+
};
|
|
82
|
+
try {
|
|
83
|
+
const client = opts.client ?? getCloudWatchClient();
|
|
84
|
+
await client.send(new PutMetricDataCommand({
|
|
85
|
+
Namespace: SYNC_METRIC_NAMESPACE,
|
|
86
|
+
MetricData: [datum],
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
91
|
+
if (opts.logger) {
|
|
92
|
+
opts.logger.warn({
|
|
93
|
+
event: "sync.metric.publish_failed",
|
|
94
|
+
tenantId: metric.tenantId,
|
|
95
|
+
relativePath: metric.relativePath,
|
|
96
|
+
sequenceNumber: metric.sequenceNumber,
|
|
97
|
+
err: { message },
|
|
98
|
+
}, "failed to publish sync latency metric to CloudWatch");
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
console.error("Failed to publish sync latency metric to CloudWatch:", message);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/sync/metrics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,EACL,gBAAgB,EAChB,oBAAoB,GAErB,MAAM,4BAA4B,CAAC;AAGpC,8EAA8E;AAE9E;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,YAAY,CAAC;AAElD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,mCAAmC,CAAC;AAuB5E,8EAA8E;AAE9E,IAAI,SAAuC,CAAC;AAE5C,SAAS,mBAAmB;IAC1B,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,SAAS,GAAG,IAAI,gBAAgB,CAAC,EAAE,CAAC,CAAC;IACvC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAAwB;IAC/D,SAAS,GAAG,MAAM,CAAC;AACrB,CAAC;AAWD;;;;;;;;;;;GAWG;AACH,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
|
+
* metric emission wired into {@link SqsPushReceiver}.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors the hq-pro PR #112 metrics test:
|
|
6
|
+
* - mock the CloudWatchClient via `aws-sdk-client-mock`
|
|
7
|
+
* - inject the mocked client via the `_setSyncCloudWatchClient` test seam
|
|
8
|
+
* - assert namespace, metric name, unit, dimensions, value
|
|
9
|
+
* - assert SDK errors are CAUGHT (the receive loop must never crash on a
|
|
10
|
+
* CloudWatch outage)
|
|
11
|
+
*
|
|
12
|
+
* Plus the US-011-specific receiver wiring:
|
|
13
|
+
* - on the receive-SUCCESS path, the receiver publishes exactly one latency
|
|
14
|
+
* datum carrying the event's tenantId + sequenceNumber (mocked publish seam
|
|
15
|
+
* — no real AWS)
|
|
16
|
+
* - a failing publish seam does NOT crash the receiver loop
|
|
17
|
+
*/
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=metrics.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.test.d.ts","sourceRoot":"","sources":["../../src/sync/metrics.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG"}
|