@indigoai-us/hq-cli 5.345.255 → 5.345.258
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/CHANGELOG.md +72 -0
- package/README.md +21 -0
- package/assets/scaffold/core/scripts/handoff-finalize.sh +981 -46
- package/assets/scaffold/core/scripts/hq-delegate-bundle.sh +51 -2
- package/assets/scaffold/core/scripts/hq-delegate-send.sh +49 -6
- package/assets/scaffold/core/scripts/hq-delegate-transfer.sh +20 -0
- package/assets/scaffold/core/scripts/hq-delegate-verify.sh +56 -10
- package/assets/scaffold/core/scripts/lib/fingerprint.sh +423 -0
- package/assets/scaffold/core/scripts/rebuild-companies-index.sh +10 -1
- package/assets/scaffold/core/scripts/rebuild-company-knowledge-index.sh +10 -1
- package/assets/scaffold/core/scripts/rebuild-projects-index.sh +10 -1
- package/assets/scaffold/core/scripts/register-project.sh +110 -19
- package/contracts/host-canary-release.json +7 -7
- package/dist/command-catalog.generated.d.ts +1 -1
- package/dist/command-catalog.generated.js +1 -1
- package/dist/commands/cloud.js +4 -1
- package/dist/commands/core-delegate-w1b.js +107 -16
- package/dist/commands/core-delegate.d.ts +7 -0
- package/dist/commands/core-delegate.js +66 -2
- package/dist/commands/core-handoff-fast.d.ts +1 -1
- package/dist/commands/core-handoff-fast.js +2 -2
- package/dist/commands/core-handoff.d.ts +3 -1
- package/dist/commands/core-handoff.js +1407 -177
- package/dist/commands/core-register-project-native.js +1 -1
- package/dist/commands/core.js +2 -2
- package/dist/commands/files.d.ts +2 -0
- package/dist/commands/files.js +34 -5
- package/dist/commands/keyed-sync-pull.d.ts +7 -3
- package/dist/commands/keyed-sync-pull.js +90 -22
- package/dist/commands/team-sync.js +1 -1
- package/dist/lib/company-skill-wrapper.js +72 -4
- package/dist/lib/daemon/requests.d.ts +14 -1
- package/dist/lib/daemon/requests.js +17 -3
- package/dist/lib/daemon/run.js +44 -2
- package/dist/lib/daemon/supervisor.d.ts +11 -0
- package/dist/lib/daemon/supervisor.js +33 -0
- package/dist/lib/daemon/sync-perf-telemetry.d.ts +85 -0
- package/dist/lib/daemon/sync-perf-telemetry.js +206 -0
- package/dist/lib/daemon/windows-relaunch.d.ts +55 -0
- package/dist/lib/daemon/windows-relaunch.js +72 -0
- package/dist/lib/doctor/checks/replay-recorder.js +3 -3
- package/dist/lib/flag-snapshot-file.d.ts +47 -1
- package/dist/lib/flag-snapshot-file.js +143 -20
- package/dist/lib/hook-engine/coverage.d.ts +82 -8
- package/dist/lib/hook-engine/coverage.js +192 -14
- package/dist/lib/index-render/companies.js +3 -2
- package/dist/lib/index-render/index.js +2 -1
- package/dist/lib/index-render/session-scope.d.ts +2 -3
- package/dist/lib/index-render/session-scope.js +2 -5
- package/dist/lib/index-render/shared.d.ts +7 -0
- package/dist/lib/index-render/shared.js +9 -0
- package/dist/lib/mesh/live/company-flag-refresh.js +2 -2
- package/dist/lib/work-context/windows-acl.d.ts +14 -0
- package/dist/lib/work-context/windows-acl.js +26 -0
- package/dist/utils/cli-telemetry.d.ts +26 -7
- package/dist/utils/cli-telemetry.js +70 -13
- package/dist/utils/git.js +1 -1
- package/dist/utils/resident-package-preload.js +4 -0
- package/dist/utils/sync-perf-telemetry-flag.d.ts +26 -0
- package/dist/utils/sync-perf-telemetry-flag.js +32 -0
- package/dist/utils/telemetry-opt-out.d.ts +13 -0
- package/dist/utils/telemetry-opt-out.js +19 -0
- package/package.json +8 -8
|
@@ -31,6 +31,7 @@ import type { UnitPlan } from "./catalog.js";
|
|
|
31
31
|
import type { MachineType } from "./machine-type.js";
|
|
32
32
|
import type { ControlRequest } from "./requests.js";
|
|
33
33
|
import { syncPassResult } from "./sync-runner.js";
|
|
34
|
+
import { type SyncPerfEvent } from "./sync-perf-telemetry.js";
|
|
34
35
|
export declare const HEALTHY_RUN_MS = 60000;
|
|
35
36
|
/**
|
|
36
37
|
* Failures in one episode before the unit is held: the first start plus five
|
|
@@ -224,7 +225,17 @@ export interface SupervisorDeps {
|
|
|
224
225
|
};
|
|
225
226
|
/** Whether a process is alive (signal 0). Enables releasing exit-17 holds when the lock holder exits. */
|
|
226
227
|
pidAlive?: (pid: number) => boolean;
|
|
228
|
+
/**
|
|
229
|
+
* Receives each valid `{"type":"perf"}` line from a sync-core engine. Must return
|
|
230
|
+
* at once (queue and send elsewhere); a throw is caught and logged. Not called
|
|
231
|
+
* while `HQ_TELEMETRY` turns telemetry off for the daemon or the engine unit.
|
|
232
|
+
*/
|
|
233
|
+
onSyncPerfEvent?: (event: SyncPerfEvent) => void;
|
|
234
|
+
/** The daemon's environment, read for the `HQ_TELEMETRY` opt-out (defaults to `process.env`). */
|
|
235
|
+
env?: NodeJS.ProcessEnv;
|
|
227
236
|
}
|
|
237
|
+
/** Invalid perf lines logged per engine run; later ones are dropped without a log line. */
|
|
238
|
+
export declare const SYNC_PERF_INVALID_LOG_MAX = 5;
|
|
228
239
|
export interface Supervisor {
|
|
229
240
|
start(): void;
|
|
230
241
|
/** Resolves once every child process has exited. */
|
|
@@ -32,6 +32,8 @@ import { StringDecoder } from "node:string_decoder";
|
|
|
32
32
|
import { classifyExit } from "./exit-cause.js";
|
|
33
33
|
import { runFleetBotsUpdateWorker } from "../bot/fleet-bots-update-task.js";
|
|
34
34
|
import { SYNC_RUNNER_LEASE_CONFLICT_EXIT, SYNC_RUNNER_LEASE_RETRY_MS, parseSyncRunnerProtocolLine, syncPassResult, } from "./sync-runner.js";
|
|
35
|
+
import { syncPerfCompanyUid, validateSyncPerfEvent } from "./sync-perf-telemetry.js";
|
|
36
|
+
import { isCliTelemetryDisabled } from "../../utils/telemetry-opt-out.js";
|
|
35
37
|
const BACKOFF_BASE_MS = 1_000;
|
|
36
38
|
const BACKOFF_MAX_MS = 60_000;
|
|
37
39
|
export const HEALTHY_RUN_MS = 60_000;
|
|
@@ -148,6 +150,8 @@ function recordPassRequestDir(state, dir) {
|
|
|
148
150
|
else
|
|
149
151
|
delete state.passRequestDir;
|
|
150
152
|
}
|
|
153
|
+
/** Invalid perf lines logged per engine run; later ones are dropped without a log line. */
|
|
154
|
+
export const SYNC_PERF_INVALID_LOG_MAX = 5;
|
|
151
155
|
/** Plan reads in a row that must find a running remote-control host blocked before it is paused. */
|
|
152
156
|
const BLOCKED_READS_BEFORE_PAUSE = 2;
|
|
153
157
|
function backoffMs(attempt) {
|
|
@@ -752,6 +756,33 @@ export function createSupervisor(deps) {
|
|
|
752
756
|
emit();
|
|
753
757
|
}, timeoutMs);
|
|
754
758
|
};
|
|
759
|
+
let invalidPerfLogged = 0;
|
|
760
|
+
// A perf line still counts as protocol activity below, as before; this only adds forwarding.
|
|
761
|
+
const acceptPerfEvent = (event) => {
|
|
762
|
+
const forward = deps.onSyncPerfEvent;
|
|
763
|
+
if (!forward)
|
|
764
|
+
return;
|
|
765
|
+
if (isCliTelemetryDisabled(deps.env ?? process.env) || isCliTelemetryDisabled(slot.plan.env))
|
|
766
|
+
return;
|
|
767
|
+
const result = validateSyncPerfEvent(event);
|
|
768
|
+
if (!result.ok) {
|
|
769
|
+
if (invalidPerfLogged < SYNC_PERF_INVALID_LOG_MAX) {
|
|
770
|
+
invalidPerfLogged++;
|
|
771
|
+
deps.log("info", `debug: dropped a perf event from ${slot.plan.id}: ${result.reason}`);
|
|
772
|
+
}
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
// A company vault's engine attributes its samples to that company, so
|
|
776
|
+
// hq-pro applies its company-scope consent rule; a personal vault's
|
|
777
|
+
// samples stay personal rows (the person's opt-in decides).
|
|
778
|
+
const companyUid = syncPerfCompanyUid(slot.plan.id);
|
|
779
|
+
try {
|
|
780
|
+
forward(companyUid ? { ...result.event, companyUid } : result.event);
|
|
781
|
+
}
|
|
782
|
+
catch (err) {
|
|
783
|
+
deps.log("info", `debug: could not queue a perf event from ${slot.plan.id}: ${errorMessage(err)}`);
|
|
784
|
+
}
|
|
785
|
+
};
|
|
755
786
|
const acceptProtocolLine = (line) => {
|
|
756
787
|
let event;
|
|
757
788
|
try {
|
|
@@ -788,6 +819,8 @@ export function createSupervisor(deps) {
|
|
|
788
819
|
runDeferredCliRestart();
|
|
789
820
|
}
|
|
790
821
|
}
|
|
822
|
+
if (slot.plan.service === "sync-core" && event.type === "perf")
|
|
823
|
+
acceptPerfEvent(event);
|
|
791
824
|
if (slot.plan.service === "sync-core") {
|
|
792
825
|
if (event.type === "fanout-plan")
|
|
793
826
|
slot.engineCycleActive = true;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sampled perf events from the sync engine (hq-syncd), forwarded to HQ telemetry.
|
|
3
|
+
*
|
|
4
|
+
* Under `HQ_SYNC_PROTOCOL=1` the engine writes `{"type":"perf","v":1,...}` lines on
|
|
5
|
+
* the protocol stdout the supervisor already reads (hq-sync-core
|
|
6
|
+
* `crates/hq-sync-daemon/src/perf_telemetry.rs`). This module validates a line
|
|
7
|
+
* strictly against that v1 schema and queues valid events for an asynchronous,
|
|
8
|
+
* batched send. Nothing here blocks or throws into the supervisor's line loop.
|
|
9
|
+
*
|
|
10
|
+
* A valid event holds only an operation name from a closed set, a duration, the
|
|
11
|
+
* sample rate, size buckets from a closed set, small counts and booleans. Any
|
|
12
|
+
* other key, any other string, or any out-of-range number rejects the whole line.
|
|
13
|
+
*/
|
|
14
|
+
export declare const SYNC_PERF_EVENT_VERSION = 1;
|
|
15
|
+
export declare const SYNC_PERF_OPS: readonly ["plan", "snapshot_ingest", "index_open", "first_sync"];
|
|
16
|
+
export type SyncPerfOp = (typeof SYNC_PERF_OPS)[number];
|
|
17
|
+
export declare const SYNC_PERF_SIZE_BUCKETS: readonly ["0-1k", "1k-10k", "10k-100k", "100k+"];
|
|
18
|
+
export type SyncPerfSizeBucket = (typeof SYNC_PERF_SIZE_BUCKETS)[number];
|
|
19
|
+
/** A day, in milliseconds. A longer sampled operation is not a plausible measurement. */
|
|
20
|
+
export declare const SYNC_PERF_MAX_DURATION_MS = 86400000;
|
|
21
|
+
/** Upper bound for `pages` and `migrationBatches` (u32 on the engine side; real values are tens). */
|
|
22
|
+
export declare const SYNC_PERF_MAX_COUNT = 100000;
|
|
23
|
+
export interface SyncPerfEvent {
|
|
24
|
+
op: SyncPerfOp;
|
|
25
|
+
durationMs: number;
|
|
26
|
+
sampleRate: number;
|
|
27
|
+
files?: SyncPerfSizeBucket;
|
|
28
|
+
actions?: SyncPerfSizeBucket;
|
|
29
|
+
placed?: SyncPerfSizeBucket;
|
|
30
|
+
pages?: number;
|
|
31
|
+
migrationBatches?: number;
|
|
32
|
+
resumed?: boolean;
|
|
33
|
+
created?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The company whose vault the engine syncs (`sync-core:cmp_*` units). Set by
|
|
36
|
+
* the supervisor from the unit id, never from the protocol line (the
|
|
37
|
+
* validator rejects the key there). hq-pro membership-checks it.
|
|
38
|
+
*/
|
|
39
|
+
companyUid?: string;
|
|
40
|
+
}
|
|
41
|
+
/** The company UID of a sync-core engine unit for a company vault, else undefined. */
|
|
42
|
+
export declare function syncPerfCompanyUid(unitId: string): string | undefined;
|
|
43
|
+
export type SyncPerfValidation = {
|
|
44
|
+
ok: true;
|
|
45
|
+
event: SyncPerfEvent;
|
|
46
|
+
}
|
|
47
|
+
/** `reason` names the failed check only; it never echoes a value or an unknown key from the line. */
|
|
48
|
+
| {
|
|
49
|
+
ok: false;
|
|
50
|
+
reason: string;
|
|
51
|
+
};
|
|
52
|
+
/** Validate one parsed protocol event against the v1 perf schema. Never throws. */
|
|
53
|
+
export declare function validateSyncPerfEvent(raw: Record<string, unknown>): SyncPerfValidation;
|
|
54
|
+
/** Rows per POST /v1/usage request (cli-telemetry SYNC_PERF_USAGE_MAX_ROWS). */
|
|
55
|
+
export declare const SYNC_PERF_MAX_BATCH = 50;
|
|
56
|
+
/** Events held while a send is pending; newer events are dropped past this. */
|
|
57
|
+
export declare const SYNC_PERF_MAX_QUEUE = 200;
|
|
58
|
+
/** How long queued events wait for more to join their batch. */
|
|
59
|
+
export declare const SYNC_PERF_FLUSH_DELAY_MS = 30000;
|
|
60
|
+
/** A send still pending after this stops holding the queue (the send itself times out sooner). */
|
|
61
|
+
export declare const SYNC_PERF_SEND_STALL_MS = 60000;
|
|
62
|
+
export interface SyncPerfForwarderDeps {
|
|
63
|
+
/** Sends one batch. May reject or never settle; the forwarder does not wait on it. */
|
|
64
|
+
send: (events: SyncPerfEvent[]) => Promise<void> | void;
|
|
65
|
+
/** False while telemetry is off. Checked when an event arrives and again before each send. */
|
|
66
|
+
enabled: () => boolean;
|
|
67
|
+
setTimeout?: (fn: () => void, ms: number) => unknown;
|
|
68
|
+
clearTimeout?: (handle: unknown) => void;
|
|
69
|
+
flushDelayMs?: number;
|
|
70
|
+
maxBatch?: number;
|
|
71
|
+
maxQueue?: number;
|
|
72
|
+
sendStallMs?: number;
|
|
73
|
+
}
|
|
74
|
+
export interface SyncPerfForwarder {
|
|
75
|
+
/** Queue one validated event. Synchronous and constant time; never throws. */
|
|
76
|
+
push(event: SyncPerfEvent): void;
|
|
77
|
+
/** Drop queued events and clear timers. */
|
|
78
|
+
stop(): void;
|
|
79
|
+
/** Events waiting to be sent (for tests and status). */
|
|
80
|
+
queued(): number;
|
|
81
|
+
/** Events dropped because the queue was full. */
|
|
82
|
+
dropped(): number;
|
|
83
|
+
}
|
|
84
|
+
export declare function createSyncPerfForwarder(deps: SyncPerfForwarderDeps): SyncPerfForwarder;
|
|
85
|
+
//# sourceMappingURL=sync-perf-telemetry.d.ts.map
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sampled perf events from the sync engine (hq-syncd), forwarded to HQ telemetry.
|
|
3
|
+
*
|
|
4
|
+
* Under `HQ_SYNC_PROTOCOL=1` the engine writes `{"type":"perf","v":1,...}` lines on
|
|
5
|
+
* the protocol stdout the supervisor already reads (hq-sync-core
|
|
6
|
+
* `crates/hq-sync-daemon/src/perf_telemetry.rs`). This module validates a line
|
|
7
|
+
* strictly against that v1 schema and queues valid events for an asynchronous,
|
|
8
|
+
* batched send. Nothing here blocks or throws into the supervisor's line loop.
|
|
9
|
+
*
|
|
10
|
+
* A valid event holds only an operation name from a closed set, a duration, the
|
|
11
|
+
* sample rate, size buckets from a closed set, small counts and booleans. Any
|
|
12
|
+
* other key, any other string, or any out-of-range number rejects the whole line.
|
|
13
|
+
*/
|
|
14
|
+
export const SYNC_PERF_EVENT_VERSION = 1;
|
|
15
|
+
export const SYNC_PERF_OPS = ["plan", "snapshot_ingest", "index_open", "first_sync"];
|
|
16
|
+
export const SYNC_PERF_SIZE_BUCKETS = ["0-1k", "1k-10k", "10k-100k", "100k+"];
|
|
17
|
+
/** A day, in milliseconds. A longer sampled operation is not a plausible measurement. */
|
|
18
|
+
export const SYNC_PERF_MAX_DURATION_MS = 86_400_000;
|
|
19
|
+
/** Upper bound for `pages` and `migrationBatches` (u32 on the engine side; real values are tens). */
|
|
20
|
+
export const SYNC_PERF_MAX_COUNT = 100_000;
|
|
21
|
+
/** The company UID of a sync-core engine unit for a company vault, else undefined. */
|
|
22
|
+
export function syncPerfCompanyUid(unitId) {
|
|
23
|
+
const match = /^sync-core:(cmp_[A-Za-z0-9][A-Za-z0-9_-]*)$/.exec(unitId);
|
|
24
|
+
return match?.[1];
|
|
25
|
+
}
|
|
26
|
+
/** The optional fields each operation may carry (the `PerfFields` constructors in perf_telemetry.rs). */
|
|
27
|
+
const FIELDS_BY_OP = {
|
|
28
|
+
plan: new Set(["files", "actions"]),
|
|
29
|
+
snapshot_ingest: new Set(["files", "pages", "resumed"]),
|
|
30
|
+
index_open: new Set(["files", "migrationBatches", "created"]),
|
|
31
|
+
first_sync: new Set(["actions", "placed"]),
|
|
32
|
+
};
|
|
33
|
+
const REQUIRED_KEYS = new Set(["type", "v", "op", "durationMs", "sampleRate"]);
|
|
34
|
+
const BUCKET_FIELDS = new Set(["files", "actions", "placed"]);
|
|
35
|
+
const COUNT_FIELDS = new Set(["pages", "migrationBatches"]);
|
|
36
|
+
const BOOLEAN_FIELDS = new Set(["resumed", "created"]);
|
|
37
|
+
const OPS = new Set(SYNC_PERF_OPS);
|
|
38
|
+
const BUCKETS = new Set(SYNC_PERF_SIZE_BUCKETS);
|
|
39
|
+
function isIntegerIn(value, min, max) {
|
|
40
|
+
return typeof value === "number" && Number.isInteger(value) && value >= min && value <= max;
|
|
41
|
+
}
|
|
42
|
+
/** Validate one parsed protocol event against the v1 perf schema. Never throws. */
|
|
43
|
+
export function validateSyncPerfEvent(raw) {
|
|
44
|
+
try {
|
|
45
|
+
if (raw.type !== "perf")
|
|
46
|
+
return { ok: false, reason: "not a perf event" };
|
|
47
|
+
if (raw.v !== SYNC_PERF_EVENT_VERSION)
|
|
48
|
+
return { ok: false, reason: "unsupported version" };
|
|
49
|
+
if (typeof raw.op !== "string" || !OPS.has(raw.op))
|
|
50
|
+
return { ok: false, reason: "unknown op" };
|
|
51
|
+
const op = raw.op;
|
|
52
|
+
const allowed = FIELDS_BY_OP[op];
|
|
53
|
+
for (const key of Object.keys(raw)) {
|
|
54
|
+
if (REQUIRED_KEYS.has(key))
|
|
55
|
+
continue;
|
|
56
|
+
if (!allowed.has(key))
|
|
57
|
+
return { ok: false, reason: `unexpected key for op ${op}` };
|
|
58
|
+
}
|
|
59
|
+
const durationMs = raw.durationMs;
|
|
60
|
+
if (!isIntegerIn(durationMs, 0, SYNC_PERF_MAX_DURATION_MS)) {
|
|
61
|
+
return { ok: false, reason: "durationMs out of range" };
|
|
62
|
+
}
|
|
63
|
+
const sampleRate = raw.sampleRate;
|
|
64
|
+
if (typeof sampleRate !== "number" || !Number.isFinite(sampleRate) || sampleRate <= 0 || sampleRate > 1) {
|
|
65
|
+
return { ok: false, reason: "sampleRate out of range" };
|
|
66
|
+
}
|
|
67
|
+
const event = { op, durationMs, sampleRate };
|
|
68
|
+
for (const field of allowed) {
|
|
69
|
+
if (!Object.prototype.hasOwnProperty.call(raw, field))
|
|
70
|
+
continue;
|
|
71
|
+
const value = raw[field];
|
|
72
|
+
if (BUCKET_FIELDS.has(field)) {
|
|
73
|
+
if (typeof value !== "string" || !BUCKETS.has(value))
|
|
74
|
+
return { ok: false, reason: `${field} is not a size bucket` };
|
|
75
|
+
event[field] = value;
|
|
76
|
+
}
|
|
77
|
+
else if (COUNT_FIELDS.has(field)) {
|
|
78
|
+
if (!isIntegerIn(value, 0, SYNC_PERF_MAX_COUNT))
|
|
79
|
+
return { ok: false, reason: `${field} out of range` };
|
|
80
|
+
event[field] = value;
|
|
81
|
+
}
|
|
82
|
+
else if (BOOLEAN_FIELDS.has(field)) {
|
|
83
|
+
if (typeof value !== "boolean")
|
|
84
|
+
return { ok: false, reason: `${field} is not a boolean` };
|
|
85
|
+
event[field] = value;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, event };
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return { ok: false, reason: "unreadable event" };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Rows per POST /v1/usage request (cli-telemetry SYNC_PERF_USAGE_MAX_ROWS). */
|
|
95
|
+
export const SYNC_PERF_MAX_BATCH = 50;
|
|
96
|
+
/** Events held while a send is pending; newer events are dropped past this. */
|
|
97
|
+
export const SYNC_PERF_MAX_QUEUE = 200;
|
|
98
|
+
/** How long queued events wait for more to join their batch. */
|
|
99
|
+
export const SYNC_PERF_FLUSH_DELAY_MS = 30_000;
|
|
100
|
+
/** A send still pending after this stops holding the queue (the send itself times out sooner). */
|
|
101
|
+
export const SYNC_PERF_SEND_STALL_MS = 60_000;
|
|
102
|
+
function unrefTimeout(fn, ms) {
|
|
103
|
+
const handle = setTimeout(fn, ms);
|
|
104
|
+
handle.unref?.();
|
|
105
|
+
return handle;
|
|
106
|
+
}
|
|
107
|
+
export function createSyncPerfForwarder(deps) {
|
|
108
|
+
const setT = deps.setTimeout ?? unrefTimeout;
|
|
109
|
+
const clearT = deps.clearTimeout ?? ((handle) => clearTimeout(handle));
|
|
110
|
+
const flushDelayMs = deps.flushDelayMs ?? SYNC_PERF_FLUSH_DELAY_MS;
|
|
111
|
+
const maxBatch = Math.max(1, Math.min(deps.maxBatch ?? SYNC_PERF_MAX_BATCH, SYNC_PERF_MAX_BATCH));
|
|
112
|
+
const maxQueue = Math.max(maxBatch, deps.maxQueue ?? SYNC_PERF_MAX_QUEUE);
|
|
113
|
+
const sendStallMs = deps.sendStallMs ?? SYNC_PERF_SEND_STALL_MS;
|
|
114
|
+
const queue = [];
|
|
115
|
+
let flushTimer;
|
|
116
|
+
let stallTimer;
|
|
117
|
+
let inFlight = 0; // generation of the pending send; 0 when none
|
|
118
|
+
let generation = 0;
|
|
119
|
+
let droppedCount = 0;
|
|
120
|
+
let stopped = false;
|
|
121
|
+
const isEnabled = () => {
|
|
122
|
+
try {
|
|
123
|
+
return deps.enabled();
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const schedule = (ms) => {
|
|
130
|
+
if (stopped || flushTimer !== undefined || queue.length === 0)
|
|
131
|
+
return;
|
|
132
|
+
flushTimer = setT(() => {
|
|
133
|
+
flushTimer = undefined;
|
|
134
|
+
flush();
|
|
135
|
+
}, ms);
|
|
136
|
+
};
|
|
137
|
+
const settle = (gen) => {
|
|
138
|
+
if (inFlight !== gen)
|
|
139
|
+
return;
|
|
140
|
+
inFlight = 0;
|
|
141
|
+
if (stallTimer !== undefined) {
|
|
142
|
+
clearT(stallTimer);
|
|
143
|
+
stallTimer = undefined;
|
|
144
|
+
}
|
|
145
|
+
schedule(queue.length >= maxBatch ? 0 : flushDelayMs);
|
|
146
|
+
};
|
|
147
|
+
const flush = () => {
|
|
148
|
+
if (stopped || inFlight !== 0 || queue.length === 0)
|
|
149
|
+
return;
|
|
150
|
+
if (!isEnabled()) {
|
|
151
|
+
queue.length = 0;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const batch = queue.splice(0, maxBatch);
|
|
155
|
+
const gen = ++generation;
|
|
156
|
+
inFlight = gen;
|
|
157
|
+
stallTimer = setT(() => {
|
|
158
|
+
stallTimer = undefined;
|
|
159
|
+
settle(gen);
|
|
160
|
+
}, sendStallMs);
|
|
161
|
+
let pending;
|
|
162
|
+
try {
|
|
163
|
+
pending = Promise.resolve(deps.send(batch));
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
pending = Promise.resolve();
|
|
167
|
+
}
|
|
168
|
+
pending.then(() => settle(gen), () => settle(gen));
|
|
169
|
+
};
|
|
170
|
+
return {
|
|
171
|
+
push(event) {
|
|
172
|
+
if (stopped || !isEnabled())
|
|
173
|
+
return;
|
|
174
|
+
if (queue.length >= maxQueue) {
|
|
175
|
+
droppedCount++;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
queue.push(event);
|
|
179
|
+
if (inFlight !== 0)
|
|
180
|
+
return;
|
|
181
|
+
if (queue.length >= maxBatch) {
|
|
182
|
+
if (flushTimer !== undefined) {
|
|
183
|
+
clearT(flushTimer);
|
|
184
|
+
flushTimer = undefined;
|
|
185
|
+
}
|
|
186
|
+
schedule(0);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
schedule(flushDelayMs);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
stop() {
|
|
193
|
+
stopped = true;
|
|
194
|
+
queue.length = 0;
|
|
195
|
+
if (flushTimer !== undefined)
|
|
196
|
+
clearT(flushTimer);
|
|
197
|
+
if (stallTimer !== undefined)
|
|
198
|
+
clearT(stallTimer);
|
|
199
|
+
flushTimer = undefined;
|
|
200
|
+
stallTimer = undefined;
|
|
201
|
+
},
|
|
202
|
+
queued: () => queue.length,
|
|
203
|
+
dropped: () => droppedCount,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=sync-perf-telemetry.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Start the Windows Scheduled Task again after a managed daemon exits to
|
|
3
|
+
* restart (RESTART_EXIT_CODE, for example onto an updated CLI).
|
|
4
|
+
*
|
|
5
|
+
* launchd (KeepAlive) and systemd (Restart=) start the daemon again when it
|
|
6
|
+
* exits. The Scheduled Task does not: its only trigger is logon, and
|
|
7
|
+
* RestartOnFailure fires when the task fails to start, not when the started
|
|
8
|
+
* process exits with a non-zero code. Without this the daemon stayed down
|
|
9
|
+
* until the next logon (feedback_a89efa81).
|
|
10
|
+
*
|
|
11
|
+
* The task's MultipleInstancesPolicy is IgnoreNew, so `schtasks /Run` while
|
|
12
|
+
* this daemon is still running is dropped. A small detached node process waits
|
|
13
|
+
* for this daemon's pid to exit, waits a little longer for the task's launcher
|
|
14
|
+
* to finish, and then runs the task. Services handed over on restart already
|
|
15
|
+
* outlive the daemon the same way, so the helper does too.
|
|
16
|
+
*/
|
|
17
|
+
import { type SpawnOptions } from "node:child_process";
|
|
18
|
+
/** Polls the daemon pid, then runs the command once; exits on its own after the deadline. */
|
|
19
|
+
export declare const WINDOWS_RELAUNCH_HELPER = "\nconst [pidArg, settleArg, deadlineArg, command, argsJson] = process.argv.slice(1);\nconst pid = Number(pidArg);\nconst settleMs = Number(settleArg);\nconst deadline = Date.now() + Number(deadlineArg);\nconst alive = () => { try { process.kill(pid, 0); return true; } catch (err) { return err && err.code === \"EPERM\"; } };\nconst run = () => {\n const r = require(\"node:child_process\").spawnSync(command, JSON.parse(argsJson), { windowsHide: true, stdio: \"ignore\" });\n process.exit(r.status === 0 ? 0 : 1);\n};\nconst tick = () => {\n if (alive() && Date.now() < deadline) { setTimeout(tick, 250); return; }\n setTimeout(run, settleMs);\n};\ntick();\n";
|
|
20
|
+
export interface WindowsRelaunchOptions {
|
|
21
|
+
/** The per-user daemon task name (install.ts currentDaemonWindowsTaskName). */
|
|
22
|
+
taskName: string;
|
|
23
|
+
/** The exiting daemon's pid; the helper waits for it to be gone. */
|
|
24
|
+
daemonPid?: number;
|
|
25
|
+
/** Extra wait after the pid is gone, for the task's launcher (conhost) to exit. */
|
|
26
|
+
settleMs?: number;
|
|
27
|
+
/** Longest wait for the pid before running the task anyway. */
|
|
28
|
+
deadlineMs?: number;
|
|
29
|
+
execPath?: string;
|
|
30
|
+
/** schtasks.exe; System32's by default. */
|
|
31
|
+
schtasks?: string;
|
|
32
|
+
env?: NodeJS.ProcessEnv;
|
|
33
|
+
spawn?: (command: string, args: string[], options: SpawnOptions) => {
|
|
34
|
+
unref(): void;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export declare function system32Schtasks(env?: NodeJS.ProcessEnv): string;
|
|
38
|
+
/** The command line the detached helper runs; exported for tests. */
|
|
39
|
+
export declare function windowsRelaunchCommand(opts: WindowsRelaunchOptions): {
|
|
40
|
+
command: string;
|
|
41
|
+
args: string[];
|
|
42
|
+
};
|
|
43
|
+
/** Spawns the detached helper. Never throws: a failure is returned for the caller to log. */
|
|
44
|
+
export declare function scheduleWindowsTaskRelaunch(opts: WindowsRelaunchOptions): {
|
|
45
|
+
ok: true;
|
|
46
|
+
} | {
|
|
47
|
+
ok: false;
|
|
48
|
+
error: string;
|
|
49
|
+
};
|
|
50
|
+
/** Only the Windows Scheduled Task needs the helper; an app that hosts the daemon relaunches it itself. */
|
|
51
|
+
export declare function needsWindowsTaskRelaunch(platform: NodeJS.Platform, opts: {
|
|
52
|
+
managed?: boolean;
|
|
53
|
+
host?: unknown;
|
|
54
|
+
}): boolean;
|
|
55
|
+
//# sourceMappingURL=windows-relaunch.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Start the Windows Scheduled Task again after a managed daemon exits to
|
|
3
|
+
* restart (RESTART_EXIT_CODE, for example onto an updated CLI).
|
|
4
|
+
*
|
|
5
|
+
* launchd (KeepAlive) and systemd (Restart=) start the daemon again when it
|
|
6
|
+
* exits. The Scheduled Task does not: its only trigger is logon, and
|
|
7
|
+
* RestartOnFailure fires when the task fails to start, not when the started
|
|
8
|
+
* process exits with a non-zero code. Without this the daemon stayed down
|
|
9
|
+
* until the next logon (feedback_a89efa81).
|
|
10
|
+
*
|
|
11
|
+
* The task's MultipleInstancesPolicy is IgnoreNew, so `schtasks /Run` while
|
|
12
|
+
* this daemon is still running is dropped. A small detached node process waits
|
|
13
|
+
* for this daemon's pid to exit, waits a little longer for the task's launcher
|
|
14
|
+
* to finish, and then runs the task. Services handed over on restart already
|
|
15
|
+
* outlive the daemon the same way, so the helper does too.
|
|
16
|
+
*/
|
|
17
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
18
|
+
import * as path from "node:path";
|
|
19
|
+
/** Polls the daemon pid, then runs the command once; exits on its own after the deadline. */
|
|
20
|
+
export const WINDOWS_RELAUNCH_HELPER = `
|
|
21
|
+
const [pidArg, settleArg, deadlineArg, command, argsJson] = process.argv.slice(1);
|
|
22
|
+
const pid = Number(pidArg);
|
|
23
|
+
const settleMs = Number(settleArg);
|
|
24
|
+
const deadline = Date.now() + Number(deadlineArg);
|
|
25
|
+
const alive = () => { try { process.kill(pid, 0); return true; } catch (err) { return err && err.code === "EPERM"; } };
|
|
26
|
+
const run = () => {
|
|
27
|
+
const r = require("node:child_process").spawnSync(command, JSON.parse(argsJson), { windowsHide: true, stdio: "ignore" });
|
|
28
|
+
process.exit(r.status === 0 ? 0 : 1);
|
|
29
|
+
};
|
|
30
|
+
const tick = () => {
|
|
31
|
+
if (alive() && Date.now() < deadline) { setTimeout(tick, 250); return; }
|
|
32
|
+
setTimeout(run, settleMs);
|
|
33
|
+
};
|
|
34
|
+
tick();
|
|
35
|
+
`;
|
|
36
|
+
export function system32Schtasks(env = process.env) {
|
|
37
|
+
const root = env.SystemRoot?.trim() || env.windir?.trim() || "C:\\Windows";
|
|
38
|
+
return path.win32.join(root, "System32", "schtasks.exe");
|
|
39
|
+
}
|
|
40
|
+
/** The command line the detached helper runs; exported for tests. */
|
|
41
|
+
export function windowsRelaunchCommand(opts) {
|
|
42
|
+
const schtasks = opts.schtasks ?? system32Schtasks(opts.env);
|
|
43
|
+
return {
|
|
44
|
+
command: opts.execPath ?? process.execPath,
|
|
45
|
+
args: [
|
|
46
|
+
"-e",
|
|
47
|
+
WINDOWS_RELAUNCH_HELPER,
|
|
48
|
+
String(opts.daemonPid ?? process.pid),
|
|
49
|
+
String(opts.settleMs ?? 3_000),
|
|
50
|
+
String(opts.deadlineMs ?? 60_000),
|
|
51
|
+
schtasks,
|
|
52
|
+
JSON.stringify(["/Run", "/TN", opts.taskName]),
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Spawns the detached helper. Never throws: a failure is returned for the caller to log. */
|
|
57
|
+
export function scheduleWindowsTaskRelaunch(opts) {
|
|
58
|
+
const { command, args } = windowsRelaunchCommand(opts);
|
|
59
|
+
try {
|
|
60
|
+
const child = (opts.spawn ?? nodeSpawn)(command, args, { detached: true, stdio: "ignore", windowsHide: true });
|
|
61
|
+
child.unref();
|
|
62
|
+
return { ok: true };
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
return { ok: false, error: err.message };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** Only the Windows Scheduled Task needs the helper; an app that hosts the daemon relaunches it itself. */
|
|
69
|
+
export function needsWindowsTaskRelaunch(platform, opts) {
|
|
70
|
+
return platform === "win32" && opts.managed === true && opts.host === undefined;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=windows-relaunch.js.map
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
import * as fs from "node:fs";
|
|
12
12
|
import * as os from "node:os";
|
|
13
13
|
import * as path from "node:path";
|
|
14
|
-
import { FLAG_SNAPSHOT_MAX_AGE_MS } from "../../flag-snapshot-file.js";
|
|
14
|
+
import { FLAG_SNAPSHOT_MAX_AGE_MS, flagSnapshotFileName } from "../../flag-snapshot-file.js";
|
|
15
15
|
export const REPLAY_RECORDER_FLAG = "core.replay-recorder";
|
|
16
16
|
export const REPLAY_RECORDER_KILL_ENV = "HQ_REPLAY_RECORDER";
|
|
17
17
|
export const REPLAY_RECORDER_MAX_BYTES_ENV = "HQ_REPLAY_RECORDER_MAX_BYTES";
|
|
@@ -74,7 +74,7 @@ function fallbackPersonSnapshotFile(tmpdir) {
|
|
|
74
74
|
return null;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
return path.join(root, "hq", "flags", "
|
|
77
|
+
return path.join(root, "hq", "flags", flagSnapshotFileName("person"));
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* Same rule as `flags::person_flag_enabled` in the engine: the newer valid one
|
|
@@ -83,7 +83,7 @@ function fallbackPersonSnapshotFile(tmpdir) {
|
|
|
83
83
|
*/
|
|
84
84
|
function personFlagOn(home, env, now, tmpdir) {
|
|
85
85
|
const cacheHome = env.XDG_CACHE_HOME || path.join(home, ".cache");
|
|
86
|
-
const primary = readPersonSnapshot(path.join(cacheHome, "hq", "flags", "
|
|
86
|
+
const primary = readPersonSnapshot(path.join(cacheHome, "hq", "flags", flagSnapshotFileName("person")), now);
|
|
87
87
|
const fallbackFile = fallbackPersonSnapshotFile(tmpdir);
|
|
88
88
|
const fallback = fallbackFile ? readPersonSnapshot(fallbackFile, now) : null;
|
|
89
89
|
const chosen = primary && fallback ? (fallback.fetchedAt > primary.fetchedAt ? fallback : primary) : (primary ?? fallback);
|
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
/** Private, synchronous, offline snapshot used by the native CLI fast route. */
|
|
2
2
|
export declare const FLAG_SNAPSHOT_MAX_AGE_MS: number;
|
|
3
|
+
/**
|
|
4
|
+
* Snapshot file names. Every reader in this CLI and in hq-hook reads only the
|
|
5
|
+
* `snapshot-v2-` files. hq-cli before f99ed6013 asked for flags without
|
|
6
|
+
* reporting its version and platform, so the server answered `false` for rows
|
|
7
|
+
* that require that report, and it wrote that answer to `snapshot-<scope>.json`.
|
|
8
|
+
* Those releases never write a `snapshot-v2-` file, so what they write cannot
|
|
9
|
+
* reach a current reader. This CLI still writes the `snapshot-<scope>.json`
|
|
10
|
+
* file beside each `snapshot-v2-` file, with the same contents, for readers
|
|
11
|
+
* released before the rename (an older hq-hook, the hq-core shell hooks), so
|
|
12
|
+
* they behave as they did. The shared names are pinned in
|
|
13
|
+
* `crates/hq-hook/src/flags.rs` (`snapshot_file_name`).
|
|
14
|
+
*
|
|
15
|
+
* Upgrade window: until a scope has a `snapshot-v2-` file, an explicit
|
|
16
|
+
* `false` in its old-name file still counts, and nothing else in it does
|
|
17
|
+
* (`legacyFalseFlags`). A `false` is the only wrong answer an old client
|
|
18
|
+
* gets (a row that requires a client report fails closed), and for a
|
|
19
|
+
* default-on kill switch it keeps the answer the CLI gave before the rename.
|
|
20
|
+
* A `true` from the old file is never used, and once any `snapshot-v2-` file
|
|
21
|
+
* exists for the scope the old file is not read at all.
|
|
22
|
+
*/
|
|
23
|
+
export declare const FLAG_SNAPSHOT_FILE_PREFIX = "snapshot-v2-";
|
|
24
|
+
/** The current snapshot file name for a company uid or the person scope. */
|
|
25
|
+
export declare function flagSnapshotFileName(scope: string): string;
|
|
26
|
+
/** The same file under the name hq-cli used before the rename. */
|
|
27
|
+
export declare function legacyFlagSnapshotPath(currentPath: string): string;
|
|
3
28
|
/** Epoch ms of a snapshot `fetched_at`, or NaN when it is not in the shared grammar. */
|
|
4
29
|
export declare function parseSnapshotTime(text: string): number;
|
|
5
30
|
export type FlagSnapshotWriteStep = "mkdir" | "chmod_dir" | "open_tmp" | "write_tmp" | "rename" | "chmod_file";
|
|
@@ -28,17 +53,38 @@ export declare function readPersonFlagSnapshotFile(options?: {
|
|
|
28
53
|
now?: number;
|
|
29
54
|
maxAgeMs?: number;
|
|
30
55
|
}): PersistedPersonFlagSnapshot | null;
|
|
56
|
+
/** The person `snapshot-v2-` file alone: never the old-name file. */
|
|
57
|
+
export declare function readCurrentPersonFlagSnapshotFile(options?: {
|
|
58
|
+
env?: NodeJS.ProcessEnv;
|
|
59
|
+
now?: number;
|
|
60
|
+
maxAgeMs?: number;
|
|
61
|
+
}): PersistedPersonFlagSnapshot | null;
|
|
31
62
|
/** Write just the validated flag decisions and company scope, never auth data. */
|
|
32
63
|
export declare function writeFlagSnapshotFile(companyUid: string, flags: Readonly<Record<string, unknown>>, options?: {
|
|
33
64
|
env?: NodeJS.ProcessEnv;
|
|
34
65
|
fetchedAt?: string;
|
|
35
66
|
}): string;
|
|
36
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* Return null for every miss, malformed file, company mismatch, or stale file.
|
|
69
|
+
* With no `snapshot-v2-` file for the company at all, the explicit `false`
|
|
70
|
+
* values of its old-name file answer, and only those (see
|
|
71
|
+
* `FLAG_SNAPSHOT_FILE_PREFIX`).
|
|
72
|
+
*/
|
|
37
73
|
export declare function readFlagSnapshotFile(companyUid: string, options?: {
|
|
38
74
|
env?: NodeJS.ProcessEnv;
|
|
39
75
|
now?: number;
|
|
40
76
|
maxAgeMs?: number;
|
|
41
77
|
}): PersistedFlagSnapshot | null;
|
|
78
|
+
/**
|
|
79
|
+
* The company's `snapshot-v2-` file alone, never the old-name file. For a
|
|
80
|
+
* caller that must know whether a current snapshot exists, such as the
|
|
81
|
+
* refresh that writes one.
|
|
82
|
+
*/
|
|
83
|
+
export declare function readCurrentFlagSnapshotFile(companyUid: string, options?: {
|
|
84
|
+
env?: NodeJS.ProcessEnv;
|
|
85
|
+
now?: number;
|
|
86
|
+
maxAgeMs?: number;
|
|
87
|
+
}): PersistedFlagSnapshot | null;
|
|
42
88
|
/** Resolve a default-on kill switch from offline snapshots, allowing stale decisions. */
|
|
43
89
|
export declare function readDefaultOnFlagSnapshot(flagKey: string, companyUid?: string, env?: NodeJS.ProcessEnv): boolean;
|
|
44
90
|
//# sourceMappingURL=flag-snapshot-file.d.ts.map
|