@memnexus-ai/mx-agent-cli 0.1.242 → 0.1.243
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/__tests__/behavioral-git-read.test.d.ts +21 -0
- package/dist/__tests__/behavioral-git-read.test.d.ts.map +1 -0
- package/dist/__tests__/behavioral-git-read.test.js +955 -0
- package/dist/__tests__/behavioral-git-read.test.js.map +1 -0
- package/dist/__tests__/event-shipper.test.d.ts +16 -0
- package/dist/__tests__/event-shipper.test.d.ts.map +1 -0
- package/dist/__tests__/event-shipper.test.js +287 -0
- package/dist/__tests__/event-shipper.test.js.map +1 -0
- package/dist/__tests__/start-report-integration.test.js +4 -0
- package/dist/__tests__/start-report-integration.test.js.map +1 -1
- package/dist/commands/start.d.ts +22 -0
- package/dist/commands/start.d.ts.map +1 -1
- package/dist/commands/start.js +44 -21
- package/dist/commands/start.js.map +1 -1
- package/dist/commands/sync.d.ts.map +1 -1
- package/dist/commands/sync.js +9 -5
- package/dist/commands/sync.js.map +1 -1
- package/dist/lib/behavioral-injection.d.ts +190 -13
- package/dist/lib/behavioral-injection.d.ts.map +1 -1
- package/dist/lib/behavioral-injection.js +653 -37
- package/dist/lib/behavioral-injection.js.map +1 -1
- package/dist/lib/claude.d.ts +77 -1
- package/dist/lib/claude.d.ts.map +1 -1
- package/dist/lib/claude.js +223 -4
- package/dist/lib/claude.js.map +1 -1
- package/dist/lib/startup-shipper.d.ts +108 -20
- package/dist/lib/startup-shipper.d.ts.map +1 -1
- package/dist/lib/startup-shipper.js +133 -29
- package/dist/lib/startup-shipper.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,28 +1,57 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* startup-shipper.ts — ships
|
|
3
|
-
* service (platform v2.58 Phase 2, #4350).
|
|
2
|
+
* startup-shipper.ts — ships startup telemetry to the central observability
|
|
3
|
+
* service (platform v2.58 Phase 2, #4350; generalized for events in #4693).
|
|
4
4
|
*
|
|
5
|
-
* Durability model mirrors the local JSONL from Phase 1: every
|
|
6
|
-
* written to a spool dir
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* Durability model mirrors the local JSONL from Phase 1: every payload is first
|
|
6
|
+
* written to a spool dir, then a best-effort POST deletes it on success. A
|
|
7
|
+
* failed or token-less POST leaves the file spooled; the Stop-hook
|
|
8
|
+
* `mx-agent sync` flushes the backlog on the next session end.
|
|
9
|
+
*
|
|
10
|
+
* Two payload kinds share this one mechanism (#4693 defect 3) — they differ only
|
|
11
|
+
* in spool directory and endpoint:
|
|
12
|
+
*
|
|
13
|
+
* StartupReport → ~/.mx-agent/logs/startup/spool/ → POST /v1/startup-reports
|
|
14
|
+
* startup event → ~/.mx-agent/logs/startup/events-spool/ → POST /v1/events
|
|
15
|
+
*
|
|
16
|
+
* They keep separate directories rather than an envelope with a kind field so
|
|
17
|
+
* that files spooled by an older CLI stay flushable by a newer one: every file
|
|
18
|
+
* in a directory is the raw body for that directory's endpoint, exactly as
|
|
19
|
+
* before.
|
|
10
20
|
*
|
|
11
21
|
* Discipline (identical to startup-report.ts): NOTHING here throws. Shipping is
|
|
12
22
|
* additive telemetry — it must never block, slow, or fail a session start. Every
|
|
13
23
|
* filesystem and network call is wrapped; the worst outcome of any failure is a
|
|
14
|
-
*
|
|
24
|
+
* payload that stays spooled for a later retry.
|
|
15
25
|
*
|
|
16
|
-
* - spoolReport(json) — write one report to the spool. Drops files
|
|
17
|
-
* than 14 days at write time
|
|
18
|
-
* -
|
|
19
|
-
*
|
|
20
|
-
*
|
|
26
|
+
* - spoolReport(json) — write one report to the report spool. Drops files
|
|
27
|
+
* older than 14 days at write time.
|
|
28
|
+
* - spoolEvent(json) — same, for the event spool.
|
|
29
|
+
* - shipEvent(json, cfg) — spool one event, then try to POST it immediately;
|
|
30
|
+
* a 2xx deletes it, anything else leaves it spooled.
|
|
31
|
+
* - flushSpool(cfg) — POST everything in BOTH spools, deleting each on
|
|
32
|
+
* 2xx. Enforces a 50-file cap per spool (drops oldest
|
|
33
|
+
* beyond it) and skips when no token is configured.
|
|
21
34
|
* - postSpoolFile(p, cfg) — POST one spool file with a 3 s timeout.
|
|
22
35
|
*/
|
|
23
|
-
/**
|
|
24
|
-
*
|
|
36
|
+
/**
|
|
37
|
+
* One spool kind: where its files live and where they are POSTed. Adding a third
|
|
38
|
+
* telemetry payload means adding a SpoolKind, not a second shipper.
|
|
39
|
+
*/
|
|
40
|
+
export interface SpoolKind {
|
|
41
|
+
/** Absolute spool path, resolved at call time so tests can repoint $HOME. */
|
|
42
|
+
dir: () => string;
|
|
43
|
+
/** Path appended to the API base URL. */
|
|
44
|
+
endpoint: string;
|
|
45
|
+
/** Singular human label used in log notes ("startup report", "startup event"). */
|
|
46
|
+
label: string;
|
|
47
|
+
}
|
|
48
|
+
/** Absolute path to the StartupReport spool directory. Computed at call time so
|
|
49
|
+
* tests that repoint $HOME observe the change (matches startup-report.ts). */
|
|
25
50
|
export declare function spoolDir(): string;
|
|
51
|
+
/** Absolute path to the startup-event spool directory (#4693). */
|
|
52
|
+
export declare function eventSpoolDir(): string;
|
|
53
|
+
export declare const REPORT_SPOOL: SpoolKind;
|
|
54
|
+
export declare const EVENT_SPOOL: SpoolKind;
|
|
26
55
|
export interface ShipConfig {
|
|
27
56
|
apiUrl: string;
|
|
28
57
|
apiToken?: string;
|
|
@@ -30,26 +59,85 @@ export interface ShipConfig {
|
|
|
30
59
|
log?: (msg: string) => void;
|
|
31
60
|
}
|
|
32
61
|
/**
|
|
33
|
-
* Write a
|
|
62
|
+
* Write a JSON payload to the given spool. Never throws. Returns the file path,
|
|
34
63
|
* or null if the write could not be performed (e.g. a read-only spool dir).
|
|
35
64
|
* Drops files older than 14 days first.
|
|
36
65
|
*/
|
|
66
|
+
export declare function spoolPayload(payloadJson: string, kind: SpoolKind): string | null;
|
|
67
|
+
/** Write one StartupReport to the report spool. Never throws. */
|
|
37
68
|
export declare function spoolReport(reportJson: string): string | null;
|
|
69
|
+
/** Write one startup event to the event spool (#4693). Never throws. */
|
|
70
|
+
export declare function spoolEvent(eventJson: string): string | null;
|
|
71
|
+
/**
|
|
72
|
+
* Spool one startup event, then try to deliver it immediately (#4693 defect 3).
|
|
73
|
+
*
|
|
74
|
+
* The spool write happens first and unconditionally — including when no token is
|
|
75
|
+
* configured — so an event is durable before any network call is attempted. The
|
|
76
|
+
* POST then deletes the file only on 2xx; a 4xx/5xx, a timeout, an unreachable
|
|
77
|
+
* API, or a missing token all leave it on disk for `mx-agent sync` to flush.
|
|
78
|
+
* Before this, an event emitted during an API outage was lost forever, which is
|
|
79
|
+
* exactly when startup events matter most.
|
|
80
|
+
*
|
|
81
|
+
* Delivery is at-least-once: /v1/events has no idempotency key, so duplicates
|
|
82
|
+
* are possible by design. Two windows produce them, not one:
|
|
83
|
+
*
|
|
84
|
+
* 1. A POST that stored the row but whose response was lost (timeout, reset)
|
|
85
|
+
* is retried by the next flush.
|
|
86
|
+
* 2. A concurrent flush. `mx-agent start` kicks off `void flushSpool(...)`
|
|
87
|
+
* fire-and-forget, and that flush drains the report spool before the event
|
|
88
|
+
* spool — so if a report backlog makes it slow (the outage-recovery case,
|
|
89
|
+
* which is exactly when this matters), it can reach the event spool after a
|
|
90
|
+
* newly written event has landed there and POST that same file alongside
|
|
91
|
+
* shipEvent's own POST.
|
|
92
|
+
*
|
|
93
|
+
* A duplicated stale-base event is strictly better than a lost one; these are
|
|
94
|
+
* read as counts of occurrences, not exact ledgers. Closing either window needs
|
|
95
|
+
* an idempotency key on startup_events (a schema change) or a flush lock — both
|
|
96
|
+
* tracked as known issues rather than fixed here.
|
|
97
|
+
*
|
|
98
|
+
* Never throws. Returns what happened for callers that want to log it.
|
|
99
|
+
*/
|
|
100
|
+
export declare function shipEvent(eventJson: string, cfg: ShipConfig): Promise<{
|
|
101
|
+
spooled: string | null;
|
|
102
|
+
shipped: boolean;
|
|
103
|
+
}>;
|
|
38
104
|
/**
|
|
39
105
|
* POST one spool file to the observability service. Returns true and deletes the
|
|
40
106
|
* file on a 2xx response. Any non-2xx, network error, or timeout returns false
|
|
41
107
|
* and leaves the file spooled. Never throws.
|
|
108
|
+
*
|
|
109
|
+
* `kind` selects the endpoint; it defaults to the report spool so existing
|
|
110
|
+
* callers are unchanged.
|
|
42
111
|
*/
|
|
43
|
-
export declare function postSpoolFile(path: string, cfg: ShipConfig): Promise<boolean>;
|
|
112
|
+
export declare function postSpoolFile(path: string, cfg: ShipConfig, kind?: SpoolKind): Promise<boolean>;
|
|
44
113
|
/**
|
|
45
|
-
* Flush
|
|
114
|
+
* Flush one spool kind: POST every spooled file, deleting each on success.
|
|
46
115
|
*
|
|
47
116
|
* Enforces the 50-file cap first (drops the oldest beyond it, with a dim note if
|
|
48
|
-
* a logger is provided). If no token is configured the
|
|
49
|
-
*
|
|
117
|
+
* a logger is provided). If no token is configured the flush is skipped — files
|
|
118
|
+
* stay spooled so a later run with a token can retry. Never throws.
|
|
119
|
+
*/
|
|
120
|
+
export declare function flushSpoolKind(cfg: ShipConfig, kind: SpoolKind): Promise<{
|
|
121
|
+
shipped: number;
|
|
122
|
+
remaining: number;
|
|
123
|
+
}>;
|
|
124
|
+
/**
|
|
125
|
+
* Flush every spool — StartupReports and startup events (#4693).
|
|
126
|
+
*
|
|
127
|
+
* `shipped` / `remaining` are fleet totals across both spools (the shape callers
|
|
128
|
+
* already consume); `reports` and `events` break them down. Never throws: one
|
|
129
|
+
* spool failing does not stop the other.
|
|
50
130
|
*/
|
|
51
131
|
export declare function flushSpool(cfg: ShipConfig): Promise<{
|
|
52
132
|
shipped: number;
|
|
53
133
|
remaining: number;
|
|
134
|
+
reports: {
|
|
135
|
+
shipped: number;
|
|
136
|
+
remaining: number;
|
|
137
|
+
};
|
|
138
|
+
events: {
|
|
139
|
+
shipped: number;
|
|
140
|
+
remaining: number;
|
|
141
|
+
};
|
|
54
142
|
}>;
|
|
55
143
|
//# sourceMappingURL=startup-shipper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"startup-shipper.d.ts","sourceRoot":"","sources":["../../src/lib/startup-shipper.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"startup-shipper.d.ts","sourceRoot":"","sources":["../../src/lib/startup-shipper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAcH;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,6EAA6E;IAC7E,GAAG,EAAE,MAAM,MAAM,CAAC;IAClB,yCAAyC;IACzC,QAAQ,EAAE,MAAM,CAAC;IACjB,kFAAkF;IAClF,KAAK,EAAE,MAAM,CAAC;CACf;AAED;+EAC+E;AAC/E,wBAAgB,QAAQ,IAAI,MAAM,CAEjC;AAED,kEAAkE;AAClE,wBAAgB,aAAa,IAAI,MAAM,CAEtC;AAED,eAAO,MAAM,YAAY,EAAE,SAI1B,CAAC;AAEF,eAAO,MAAM,WAAW,EAAE,SAIzB,CAAC;AAKF,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0EAA0E;IAC1E,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7B;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAYhF;AAED,iEAAiE;AACjE,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAE7D;AAED,wEAAwE;AACxE,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAE3D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,SAAS,CAC7B,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,UAAU,GACd,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAKvD;AAwCD;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,UAAU,EACf,IAAI,GAAE,SAAwB,GAC7B,OAAO,CAAC,OAAO,CAAC,CA8BlB;AAED;;;;;;GAMG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,SAAS,GACd,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAgCjD;AAED;;;;;;GAMG;AACH,wBAAsB,UAAU,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC;IACzD,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IAChD,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;CAChD,CAAC,CAoBD"}
|
|
@@ -1,23 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* startup-shipper.ts — ships
|
|
3
|
-
* service (platform v2.58 Phase 2, #4350).
|
|
2
|
+
* startup-shipper.ts — ships startup telemetry to the central observability
|
|
3
|
+
* service (platform v2.58 Phase 2, #4350; generalized for events in #4693).
|
|
4
4
|
*
|
|
5
|
-
* Durability model mirrors the local JSONL from Phase 1: every
|
|
6
|
-
* written to a spool dir
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* Durability model mirrors the local JSONL from Phase 1: every payload is first
|
|
6
|
+
* written to a spool dir, then a best-effort POST deletes it on success. A
|
|
7
|
+
* failed or token-less POST leaves the file spooled; the Stop-hook
|
|
8
|
+
* `mx-agent sync` flushes the backlog on the next session end.
|
|
9
|
+
*
|
|
10
|
+
* Two payload kinds share this one mechanism (#4693 defect 3) — they differ only
|
|
11
|
+
* in spool directory and endpoint:
|
|
12
|
+
*
|
|
13
|
+
* StartupReport → ~/.mx-agent/logs/startup/spool/ → POST /v1/startup-reports
|
|
14
|
+
* startup event → ~/.mx-agent/logs/startup/events-spool/ → POST /v1/events
|
|
15
|
+
*
|
|
16
|
+
* They keep separate directories rather than an envelope with a kind field so
|
|
17
|
+
* that files spooled by an older CLI stay flushable by a newer one: every file
|
|
18
|
+
* in a directory is the raw body for that directory's endpoint, exactly as
|
|
19
|
+
* before.
|
|
10
20
|
*
|
|
11
21
|
* Discipline (identical to startup-report.ts): NOTHING here throws. Shipping is
|
|
12
22
|
* additive telemetry — it must never block, slow, or fail a session start. Every
|
|
13
23
|
* filesystem and network call is wrapped; the worst outcome of any failure is a
|
|
14
|
-
*
|
|
24
|
+
* payload that stays spooled for a later retry.
|
|
15
25
|
*
|
|
16
|
-
* - spoolReport(json) — write one report to the spool. Drops files
|
|
17
|
-
* than 14 days at write time
|
|
18
|
-
* -
|
|
19
|
-
*
|
|
20
|
-
*
|
|
26
|
+
* - spoolReport(json) — write one report to the report spool. Drops files
|
|
27
|
+
* older than 14 days at write time.
|
|
28
|
+
* - spoolEvent(json) — same, for the event spool.
|
|
29
|
+
* - shipEvent(json, cfg) — spool one event, then try to POST it immediately;
|
|
30
|
+
* a 2xx deletes it, anything else leaves it spooled.
|
|
31
|
+
* - flushSpool(cfg) — POST everything in BOTH spools, deleting each on
|
|
32
|
+
* 2xx. Enforces a 50-file cap per spool (drops oldest
|
|
33
|
+
* beyond it) and skips when no token is configured.
|
|
21
34
|
* - postSpoolFile(p, cfg) — POST one spool file with a 3 s timeout.
|
|
22
35
|
*/
|
|
23
36
|
import { mkdirSync, writeFileSync, readdirSync, statSync, unlinkSync, readFileSync } from 'fs';
|
|
@@ -26,27 +39,43 @@ import { join } from 'path';
|
|
|
26
39
|
import { randomUUID } from 'crypto';
|
|
27
40
|
/** Drop spooled files older than this at write time (bounded disk / staleness). */
|
|
28
41
|
const MAX_SPOOL_AGE_MS = 14 * 24 * 60 * 60 * 1000; // 14 days
|
|
29
|
-
/** Flush keeps at most this many files; the oldest beyond it are dropped. */
|
|
42
|
+
/** Flush keeps at most this many files per spool; the oldest beyond it are dropped. */
|
|
30
43
|
const MAX_SPOOL_FILES = 50;
|
|
31
44
|
/** Per-POST timeout — a single ship attempt must never stall longer than this. */
|
|
32
45
|
const POST_TIMEOUT_MS = 3000;
|
|
33
|
-
/** Absolute path to the spool directory. Computed at call time so
|
|
34
|
-
* repoint $HOME observe the change (matches startup-report.ts). */
|
|
46
|
+
/** Absolute path to the StartupReport spool directory. Computed at call time so
|
|
47
|
+
* tests that repoint $HOME observe the change (matches startup-report.ts). */
|
|
35
48
|
export function spoolDir() {
|
|
36
49
|
return join(homedir(), '.mx-agent', 'logs', 'startup', 'spool');
|
|
37
50
|
}
|
|
51
|
+
/** Absolute path to the startup-event spool directory (#4693). */
|
|
52
|
+
export function eventSpoolDir() {
|
|
53
|
+
return join(homedir(), '.mx-agent', 'logs', 'startup', 'events-spool');
|
|
54
|
+
}
|
|
55
|
+
export const REPORT_SPOOL = {
|
|
56
|
+
dir: spoolDir,
|
|
57
|
+
endpoint: '/v1/startup-reports',
|
|
58
|
+
label: 'startup report',
|
|
59
|
+
};
|
|
60
|
+
export const EVENT_SPOOL = {
|
|
61
|
+
dir: eventSpoolDir,
|
|
62
|
+
endpoint: '/v1/events',
|
|
63
|
+
label: 'startup event',
|
|
64
|
+
};
|
|
65
|
+
/** Every spool the flush walks. Order is report-first (largest payloads first). */
|
|
66
|
+
const ALL_SPOOLS = [REPORT_SPOOL, EVENT_SPOOL];
|
|
38
67
|
/**
|
|
39
|
-
* Write a
|
|
68
|
+
* Write a JSON payload to the given spool. Never throws. Returns the file path,
|
|
40
69
|
* or null if the write could not be performed (e.g. a read-only spool dir).
|
|
41
70
|
* Drops files older than 14 days first.
|
|
42
71
|
*/
|
|
43
|
-
export function
|
|
72
|
+
export function spoolPayload(payloadJson, kind) {
|
|
44
73
|
try {
|
|
45
|
-
const dir =
|
|
74
|
+
const dir = kind.dir();
|
|
46
75
|
mkdirSync(dir, { recursive: true });
|
|
47
76
|
dropAgedFiles(dir);
|
|
48
77
|
const path = join(dir, `${randomUUID()}.json`);
|
|
49
|
-
writeFileSync(path,
|
|
78
|
+
writeFileSync(path, payloadJson);
|
|
50
79
|
return path;
|
|
51
80
|
}
|
|
52
81
|
catch {
|
|
@@ -54,6 +83,50 @@ export function spoolReport(reportJson) {
|
|
|
54
83
|
return null;
|
|
55
84
|
}
|
|
56
85
|
}
|
|
86
|
+
/** Write one StartupReport to the report spool. Never throws. */
|
|
87
|
+
export function spoolReport(reportJson) {
|
|
88
|
+
return spoolPayload(reportJson, REPORT_SPOOL);
|
|
89
|
+
}
|
|
90
|
+
/** Write one startup event to the event spool (#4693). Never throws. */
|
|
91
|
+
export function spoolEvent(eventJson) {
|
|
92
|
+
return spoolPayload(eventJson, EVENT_SPOOL);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Spool one startup event, then try to deliver it immediately (#4693 defect 3).
|
|
96
|
+
*
|
|
97
|
+
* The spool write happens first and unconditionally — including when no token is
|
|
98
|
+
* configured — so an event is durable before any network call is attempted. The
|
|
99
|
+
* POST then deletes the file only on 2xx; a 4xx/5xx, a timeout, an unreachable
|
|
100
|
+
* API, or a missing token all leave it on disk for `mx-agent sync` to flush.
|
|
101
|
+
* Before this, an event emitted during an API outage was lost forever, which is
|
|
102
|
+
* exactly when startup events matter most.
|
|
103
|
+
*
|
|
104
|
+
* Delivery is at-least-once: /v1/events has no idempotency key, so duplicates
|
|
105
|
+
* are possible by design. Two windows produce them, not one:
|
|
106
|
+
*
|
|
107
|
+
* 1. A POST that stored the row but whose response was lost (timeout, reset)
|
|
108
|
+
* is retried by the next flush.
|
|
109
|
+
* 2. A concurrent flush. `mx-agent start` kicks off `void flushSpool(...)`
|
|
110
|
+
* fire-and-forget, and that flush drains the report spool before the event
|
|
111
|
+
* spool — so if a report backlog makes it slow (the outage-recovery case,
|
|
112
|
+
* which is exactly when this matters), it can reach the event spool after a
|
|
113
|
+
* newly written event has landed there and POST that same file alongside
|
|
114
|
+
* shipEvent's own POST.
|
|
115
|
+
*
|
|
116
|
+
* A duplicated stale-base event is strictly better than a lost one; these are
|
|
117
|
+
* read as counts of occurrences, not exact ledgers. Closing either window needs
|
|
118
|
+
* an idempotency key on startup_events (a schema change) or a flush lock — both
|
|
119
|
+
* tracked as known issues rather than fixed here.
|
|
120
|
+
*
|
|
121
|
+
* Never throws. Returns what happened for callers that want to log it.
|
|
122
|
+
*/
|
|
123
|
+
export async function shipEvent(eventJson, cfg) {
|
|
124
|
+
const spooled = spoolPayload(eventJson, EVENT_SPOOL);
|
|
125
|
+
if (!spooled)
|
|
126
|
+
return { spooled: null, shipped: false };
|
|
127
|
+
const shipped = await postSpoolFile(spooled, cfg, EVENT_SPOOL);
|
|
128
|
+
return { spooled, shipped };
|
|
129
|
+
}
|
|
57
130
|
/** Remove spool files whose mtime is older than the age cap. Never throws. */
|
|
58
131
|
function dropAgedFiles(dir) {
|
|
59
132
|
try {
|
|
@@ -100,8 +173,11 @@ function listSpoolFilesOldestFirst(dir) {
|
|
|
100
173
|
* POST one spool file to the observability service. Returns true and deletes the
|
|
101
174
|
* file on a 2xx response. Any non-2xx, network error, or timeout returns false
|
|
102
175
|
* and leaves the file spooled. Never throws.
|
|
176
|
+
*
|
|
177
|
+
* `kind` selects the endpoint; it defaults to the report spool so existing
|
|
178
|
+
* callers are unchanged.
|
|
103
179
|
*/
|
|
104
|
-
export async function postSpoolFile(path, cfg) {
|
|
180
|
+
export async function postSpoolFile(path, cfg, kind = REPORT_SPOOL) {
|
|
105
181
|
if (!cfg.apiToken)
|
|
106
182
|
return false; // no token → cannot ship
|
|
107
183
|
let body;
|
|
@@ -112,7 +188,7 @@ export async function postSpoolFile(path, cfg) {
|
|
|
112
188
|
return false; // file vanished (concurrent flush) — nothing to do
|
|
113
189
|
}
|
|
114
190
|
try {
|
|
115
|
-
const res = await fetch(`${cfg.apiUrl}
|
|
191
|
+
const res = await fetch(`${cfg.apiUrl}${kind.endpoint}`, {
|
|
116
192
|
method: 'POST',
|
|
117
193
|
headers: {
|
|
118
194
|
'Content-Type': 'application/json',
|
|
@@ -137,14 +213,14 @@ export async function postSpoolFile(path, cfg) {
|
|
|
137
213
|
}
|
|
138
214
|
}
|
|
139
215
|
/**
|
|
140
|
-
* Flush
|
|
216
|
+
* Flush one spool kind: POST every spooled file, deleting each on success.
|
|
141
217
|
*
|
|
142
218
|
* Enforces the 50-file cap first (drops the oldest beyond it, with a dim note if
|
|
143
|
-
* a logger is provided). If no token is configured the
|
|
144
|
-
*
|
|
219
|
+
* a logger is provided). If no token is configured the flush is skipped — files
|
|
220
|
+
* stay spooled so a later run with a token can retry. Never throws.
|
|
145
221
|
*/
|
|
146
|
-
export async function
|
|
147
|
-
const dir =
|
|
222
|
+
export async function flushSpoolKind(cfg, kind) {
|
|
223
|
+
const dir = kind.dir();
|
|
148
224
|
let files = listSpoolFilesOldestFirst(dir);
|
|
149
225
|
// 50-file cap: drop the oldest beyond the cap so disk stays bounded.
|
|
150
226
|
if (files.length > MAX_SPOOL_FILES) {
|
|
@@ -158,7 +234,7 @@ export async function flushSpool(cfg) {
|
|
|
158
234
|
}
|
|
159
235
|
}
|
|
160
236
|
if (cfg.log) {
|
|
161
|
-
cfg.log(
|
|
237
|
+
cfg.log(`${kind.label} spool exceeded ${MAX_SPOOL_FILES} files — dropped ${drop.length} oldest`);
|
|
162
238
|
}
|
|
163
239
|
files = files.slice(files.length - MAX_SPOOL_FILES);
|
|
164
240
|
}
|
|
@@ -168,10 +244,38 @@ export async function flushSpool(cfg) {
|
|
|
168
244
|
}
|
|
169
245
|
let shipped = 0;
|
|
170
246
|
for (const f of files) {
|
|
171
|
-
if (await postSpoolFile(join(dir, f), cfg))
|
|
247
|
+
if (await postSpoolFile(join(dir, f), cfg, kind))
|
|
172
248
|
shipped++;
|
|
173
249
|
}
|
|
174
250
|
const remaining = listSpoolFilesOldestFirst(dir).length;
|
|
175
251
|
return { shipped, remaining };
|
|
176
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Flush every spool — StartupReports and startup events (#4693).
|
|
255
|
+
*
|
|
256
|
+
* `shipped` / `remaining` are fleet totals across both spools (the shape callers
|
|
257
|
+
* already consume); `reports` and `events` break them down. Never throws: one
|
|
258
|
+
* spool failing does not stop the other.
|
|
259
|
+
*/
|
|
260
|
+
export async function flushSpool(cfg) {
|
|
261
|
+
const results = new Map();
|
|
262
|
+
for (const kind of ALL_SPOOLS) {
|
|
263
|
+
try {
|
|
264
|
+
results.set(kind, await flushSpoolKind(cfg, kind));
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
// Defensive — flushSpoolKind is written not to throw. A failure in one
|
|
268
|
+
// spool must never swallow the other's backlog.
|
|
269
|
+
results.set(kind, { shipped: 0, remaining: 0 });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const reports = results.get(REPORT_SPOOL) ?? { shipped: 0, remaining: 0 };
|
|
273
|
+
const events = results.get(EVENT_SPOOL) ?? { shipped: 0, remaining: 0 };
|
|
274
|
+
return {
|
|
275
|
+
shipped: reports.shipped + events.shipped,
|
|
276
|
+
remaining: reports.remaining + events.remaining,
|
|
277
|
+
reports,
|
|
278
|
+
events,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
177
281
|
//# sourceMappingURL=startup-shipper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"startup-shipper.js","sourceRoot":"","sources":["../../src/lib/startup-shipper.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"startup-shipper.js","sourceRoot":"","sources":["../../src/lib/startup-shipper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAA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