@indigoai-us/hq-cli 5.345.215-beta.0 → 5.345.217
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 +206 -1
- package/README.md +45 -0
- package/assets/scaffold/core/scripts/hq-delegate-verify.sh +5 -2
- package/assets/scaffold/core/scripts/lib/epochrealtime.sh +30 -0
- package/assets/scaffold/core/scripts/lib/work-mesh-enqueue.sh +12 -5
- package/dist/command-catalog.generated.d.ts +105 -10
- package/dist/command-catalog.generated.js +127 -10
- package/dist/command-help.js +3 -1
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/auth.d.ts +1 -1
- package/dist/commands/auth.js +11 -52
- package/dist/commands/bot.js +75 -10
- package/dist/commands/capacity.d.ts +4 -1
- package/dist/commands/capacity.js +46 -10
- package/dist/commands/cloud-demote.d.ts +19 -4
- package/dist/commands/cloud-demote.js +85 -12
- package/dist/commands/cloud-provision.d.ts +5 -1
- package/dist/commands/cloud-provision.js +6 -2
- package/dist/commands/cloud.d.ts +36 -0
- package/dist/commands/cloud.js +110 -27
- package/dist/commands/core-delegate-w1b.js +15 -1
- package/dist/commands/core-policy.js +67 -8
- package/dist/commands/core.js +1 -0
- package/dist/commands/daemon.js +9 -1
- package/dist/commands/dm.js +7 -0
- package/dist/commands/doctor-usage.d.ts +8 -0
- package/dist/commands/doctor-usage.js +20 -0
- package/dist/commands/doctor.js +7 -1
- package/dist/commands/env.d.ts +18 -0
- package/dist/commands/env.js +82 -0
- package/dist/commands/files-browse.d.ts +11 -4
- package/dist/commands/files-browse.js +21 -4
- package/dist/commands/files.d.ts +1 -1
- package/dist/commands/files.js +25 -11
- package/dist/commands/install-global.js +2 -2
- package/dist/commands/integrations-auth-rules.d.ts +27 -0
- package/dist/commands/integrations-auth-rules.js +79 -0
- package/dist/commands/integrations-import.js +124 -12
- package/dist/commands/lanes.js +102 -26
- package/dist/commands/logout.d.ts +7 -0
- package/dist/commands/logout.js +41 -34
- package/dist/commands/mesh.d.ts +2 -0
- package/dist/commands/mesh.js +60 -35
- package/dist/commands/sources.d.ts +8 -2
- package/dist/commands/sources.js +65 -9
- package/dist/commands/sync-held.js +5 -2
- package/dist/commands/whoami.js +26 -1
- package/dist/data/command-states.json +15 -0
- package/dist/index.js +52 -4
- package/dist/lazy-commands.js +5 -6
- package/dist/lib/anywhere-person-setting.d.ts +2 -0
- package/dist/lib/anywhere-person-setting.js +6 -3
- package/dist/lib/billing/usage-card.d.ts +25 -4
- package/dist/lib/billing/usage-card.js +76 -9
- package/dist/lib/billing/usage-cost.d.ts +3 -3
- package/dist/lib/billing/usage-cost.js +35 -13
- package/dist/lib/billing/usage-only.js +4 -2
- package/dist/lib/billing/usage-rate-card.d.ts +2 -2
- package/dist/lib/billing/usage-rate-card.js +3 -0
- package/dist/lib/bot/fleet-cron.d.ts +8 -0
- package/dist/lib/bot/fleet-cron.js +126 -7
- package/dist/lib/bot/fleet-daemon.d.ts +46 -0
- package/dist/lib/bot/fleet-daemon.js +99 -1
- package/dist/lib/bot/inflight.d.ts +9 -0
- package/dist/lib/bot/inflight.js +10 -0
- package/dist/lib/bot/jobs/hot-signal-sync.js +12 -1
- package/dist/lib/bot/probe.js +7 -1
- package/dist/lib/bot/run.js +20 -1
- package/dist/lib/bot/runtime/codex.js +1 -1
- package/dist/lib/bot/runtime/grok-acp.d.ts +6 -1
- package/dist/lib/bot/runtime/grok-acp.js +32 -14
- package/dist/lib/bot/runtime/grok.js +1 -1
- package/dist/lib/bot/runtime/index.js +19 -9
- package/dist/lib/bot/runtime/messages-stream.js +2 -2
- package/dist/lib/bot/runtime/types.d.ts +15 -0
- package/dist/lib/bot/runtime/types.js +9 -0
- package/dist/lib/bot/runtime-failure.d.ts +23 -0
- package/dist/lib/bot/runtime-failure.js +43 -1
- package/dist/lib/capacity/accounts.d.ts +8 -0
- package/dist/lib/capacity/accounts.js +19 -0
- package/dist/lib/capacity/available.d.ts +154 -0
- package/dist/lib/capacity/available.js +905 -0
- package/dist/lib/capacity/codex-availability.d.ts +14 -1
- package/dist/lib/capacity/codex-availability.js +26 -1
- package/dist/lib/capacity/collectors/claude-oauth.d.ts +29 -2
- package/dist/lib/capacity/collectors/claude-oauth.js +104 -7
- package/dist/lib/capacity/collectors/codex.js +10 -7
- package/dist/lib/capacity/ledger.d.ts +37 -1
- package/dist/lib/capacity/ledger.js +207 -30
- package/dist/lib/capacity/project.d.ts +26 -0
- package/dist/lib/capacity/project.js +137 -8
- package/dist/lib/capacity/refusal-rows.d.ts +31 -0
- package/dist/lib/capacity/refusal-rows.js +61 -0
- package/dist/lib/capacity/refusal.d.ts +53 -2
- package/dist/lib/capacity/refusal.js +284 -65
- package/dist/lib/capacity/route.js +46 -11
- package/dist/lib/capacity/widget.d.ts +12 -1
- package/dist/lib/capacity/widget.js +91 -3
- package/dist/lib/daemon/flag-monitor.d.ts +8 -0
- package/dist/lib/daemon/flag-monitor.js +17 -6
- package/dist/lib/daemon/flag.d.ts +65 -1
- package/dist/lib/daemon/flag.js +190 -40
- package/dist/lib/daemon/ops/policy.js +62 -5
- package/dist/lib/daemon/paths.d.ts +72 -2
- package/dist/lib/daemon/paths.js +154 -3
- package/dist/lib/daemon/resolver.d.ts +2 -0
- package/dist/lib/daemon/resolver.js +3 -0
- package/dist/lib/daemon/run.d.ts +9 -0
- package/dist/lib/daemon/run.js +79 -21
- package/dist/lib/daemon/selfcheck.d.ts +22 -0
- package/dist/lib/daemon/selfcheck.js +40 -1
- package/dist/lib/daemon/server.d.ts +5 -1
- package/dist/lib/daemon/server.js +17 -23
- package/dist/lib/daemon/state-file-switch.d.ts +57 -0
- package/dist/lib/daemon/state-file-switch.js +88 -0
- package/dist/lib/daemon/state-file.d.ts +198 -0
- package/dist/lib/daemon/state-file.js +419 -0
- package/dist/lib/daemon/tcc-eperm.d.ts +153 -0
- package/dist/lib/daemon/tcc-eperm.js +310 -0
- package/dist/lib/daemon/timed-flag-monitors.d.ts +47 -0
- package/dist/lib/daemon/timed-flag-monitors.js +75 -0
- package/dist/lib/delivery/admission.d.ts +86 -0
- package/dist/lib/delivery/admission.js +148 -0
- package/dist/lib/delivery/artifacts.d.ts +7 -1
- package/dist/lib/delivery/artifacts.js +29 -10
- package/dist/lib/delivery/attempt-id.d.ts +13 -0
- package/dist/lib/delivery/attempt-id.js +16 -0
- package/dist/lib/delivery/disposable-target.d.ts +42 -0
- package/dist/lib/delivery/disposable-target.js +111 -0
- package/dist/lib/delivery/effects.d.ts +136 -0
- package/dist/lib/delivery/effects.js +541 -0
- package/dist/lib/delivery/enrollment.d.ts +21 -17
- package/dist/lib/delivery/enrollment.js +83 -5
- package/dist/lib/delivery/escalations.d.ts +51 -0
- package/dist/lib/delivery/escalations.js +60 -0
- package/dist/lib/delivery/lease.d.ts +80 -0
- package/dist/lib/delivery/lease.js +134 -0
- package/dist/lib/delivery/migrations.d.ts +120 -4
- package/dist/lib/delivery/migrations.js +215 -15
- package/dist/lib/delivery/mqtt.d.ts +2 -0
- package/dist/lib/delivery/mqtt.js +7 -0
- package/dist/lib/delivery/reconciler.d.ts +65 -1
- package/dist/lib/delivery/reconciler.js +224 -13
- package/dist/lib/delivery/rpc/client.d.ts +22 -0
- package/dist/lib/delivery/rpc/client.js +159 -0
- package/dist/lib/delivery/rpc/port.d.ts +40 -0
- package/dist/lib/delivery/rpc/port.js +16 -0
- package/dist/lib/delivery/rpc/server.d.ts +30 -0
- package/dist/lib/delivery/rpc/server.js +205 -0
- package/dist/lib/delivery/rpc/wire.d.ts +85 -0
- package/dist/lib/delivery/rpc/wire.js +143 -0
- package/dist/lib/delivery/store.d.ts +2 -0
- package/dist/lib/delivery/store.js +4 -0
- package/dist/lib/delivery/windows-service-install.d.ts +262 -0
- package/dist/lib/delivery/windows-service-install.js +892 -0
- package/dist/lib/delivery/workers/macos.d.ts +16 -1
- package/dist/lib/delivery/workers/macos.js +54 -12
- package/dist/lib/delivery/workers/windows-service-live.d.ts +121 -0
- package/dist/lib/delivery/workers/windows-service-live.js +711 -0
- package/dist/lib/delivery/workers/windows-service-runner.d.ts +33 -0
- package/dist/lib/delivery/workers/windows-service-runner.js +82 -0
- package/dist/lib/delivery/workers/windows-service.d.ts +140 -0
- package/dist/lib/delivery/workers/windows-service.js +584 -0
- package/dist/lib/delivery/workers/windows.d.ts +32 -22
- package/dist/lib/delivery/workers/windows.js +50 -64
- package/dist/lib/doctor/checks/host-core.d.ts +50 -0
- package/dist/lib/doctor/checks/host-core.js +269 -0
- package/dist/lib/doctor/checks/replay-recorder.d.ts +39 -0
- package/dist/lib/doctor/checks/replay-recorder.js +261 -0
- package/dist/lib/doctor/registry.js +4 -0
- package/dist/lib/environment/profile.d.ts +239 -0
- package/dist/lib/environment/profile.js +251 -0
- package/dist/lib/hook-engine/reconcile.js +39 -13
- package/dist/lib/hook-engine/registration.d.ts +9 -0
- package/dist/lib/hook-engine/registration.js +19 -0
- package/dist/lib/hot-channels/adapters/meetings.d.ts +15 -0
- package/dist/lib/hot-channels/adapters/meetings.js +36 -3
- package/dist/lib/hot-channels/adapters/slack.d.ts +21 -5
- package/dist/lib/hot-channels/adapters/slack.js +64 -21
- package/dist/lib/hot-channels/alert.d.ts +2 -0
- package/dist/lib/hot-channels/alert.js +8 -1
- package/dist/lib/hot-channels/contract.d.ts +12 -0
- package/dist/lib/hot-channels/contract.js +7 -0
- package/dist/lib/hot-channels/loop.js +1 -0
- package/dist/lib/hot-channels/run.d.ts +6 -0
- package/dist/lib/hot-channels/run.js +4 -0
- package/dist/lib/hot-channels/signals-pass.d.ts +74 -1
- package/dist/lib/hot-channels/signals-pass.js +82 -41
- package/dist/lib/hot-channels/signals-redo.js +10 -2
- package/dist/lib/install/claude-targets.d.ts +12 -1
- package/dist/lib/install/claude-targets.js +57 -5
- package/dist/lib/install/codex-targets.d.ts +2 -2
- package/dist/lib/install/codex-targets.js +36 -6
- package/dist/lib/install/hook-target.d.ts +3 -0
- package/dist/lib/install/hook-target.js +11 -0
- package/dist/lib/lanes/canonical-host.d.ts +35 -0
- package/dist/lib/lanes/canonical-host.js +65 -0
- package/dist/lib/lanes/host-capacity.d.ts +30 -0
- package/dist/lib/lanes/host-capacity.js +29 -3
- package/dist/lib/lanes/watcher.js +35 -0
- package/dist/lib/mesh/live/daemon/run.d.ts +2 -0
- package/dist/lib/mesh/live/daemon/run.js +5 -2
- package/dist/lib/mesh/live/flush.d.ts +5 -1
- package/dist/lib/mesh/live/flush.js +12 -6
- package/dist/lib/mesh/live/reporting-privacy.d.ts +32 -0
- package/dist/lib/mesh/live/reporting-privacy.js +56 -1
- package/dist/lib/mesh/live/spool.d.ts +7 -0
- package/dist/lib/mesh/live/spool.js +25 -0
- package/dist/lib/person-setting-cache.d.ts +38 -0
- package/dist/lib/person-setting-cache.js +98 -0
- package/dist/lib/safe-store.d.ts +245 -0
- package/dist/lib/safe-store.js +777 -0
- package/dist/lib/signals/engine.d.ts +29 -2
- package/dist/lib/signals/engine.js +103 -4
- package/dist/lib/signals/reply.js +73 -18
- package/dist/lib/signals/steps/adapters.js +7 -2
- package/dist/lib/signals/steps/extract.d.ts +13 -0
- package/dist/lib/signals/steps/extract.js +122 -6
- package/dist/main.js +46 -3
- package/dist/schemas/hq-available-ai.v1.schema.json +76 -0
- package/dist/schemas/hq-environment-profile.v1.schema.json +438 -0
- package/dist/utils/auth-expiry.d.ts +8 -0
- package/dist/utils/auth-expiry.js +53 -0
- package/dist/utils/catalog-runtime-paths.d.ts +34 -0
- package/dist/utils/catalog-runtime-paths.js +53 -0
- package/dist/utils/cli-telemetry.d.ts +21 -0
- package/dist/utils/cli-telemetry.js +34 -0
- package/dist/utils/command-usage.d.ts +275 -0
- package/dist/utils/command-usage.js +797 -0
- package/dist/utils/files-browse-acl.js +1 -1
- package/dist/utils/hook-trust.d.ts +7 -0
- package/dist/utils/hook-trust.js +31 -4
- package/dist/utils/host-registration.d.ts +126 -0
- package/dist/utils/host-registration.js +378 -0
- package/dist/utils/resident-package-preload.js +2 -0
- package/package.json +10 -8
- package/scripts/bench-core.mjs +1096 -0
- package/scripts/check-function-manifest.mjs +702 -0
- package/dist/lib/delivery/workers/windows-boundary.d.ts +0 -453
- package/dist/lib/delivery/workers/windows-boundary.js +0 -693
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Self-recovery for a daemon that macOS privacy (TCC) has cut off from the
|
|
3
|
+
* HQ folder.
|
|
4
|
+
*
|
|
5
|
+
* After HQ.app updates itself, a daemon that was started from the old app
|
|
6
|
+
* bundle keeps running from the deleted bundle. macOS then refuses its file
|
|
7
|
+
* reads and writes under the HQ root with EPERM ("operation not permitted"),
|
|
8
|
+
* and every hqd op that touches companies/ fails until someone runs
|
|
9
|
+
* `launchctl kickstart -k`. A fresh process started by launchd is checked
|
|
10
|
+
* again and gets access.
|
|
11
|
+
*
|
|
12
|
+
* So: when hqd sees EPERM on reads (open, stat, readdir and the like) of paths
|
|
13
|
+
* under the HQ root, repeated across more than one path within a short window,
|
|
14
|
+
* it logs one structured event, reports
|
|
15
|
+
* it once to Sentry under a fixed fingerprint, and exits with
|
|
16
|
+
* TCC_EPERM_EXIT_CODE. The daemon's LaunchAgent has KeepAlive=true, so launchd
|
|
17
|
+
* starts it again (after its ThrottleInterval) whatever the exit code.
|
|
18
|
+
*
|
|
19
|
+
* It never exits for EPERM outside the HQ root, for EACCES, off macOS, while
|
|
20
|
+
* the daemon is already stopping, or when no service manager would start it
|
|
21
|
+
* again. A permission that stays revoked would otherwise restart the daemon
|
|
22
|
+
* forever, so after TCC_EPERM_MAX_EXITS exits within TCC_EPERM_EXIT_WINDOW_MS
|
|
23
|
+
* it stays up and writes FILES_ACCESS_NEEDED_MARKER, which `hq daemon status`
|
|
24
|
+
* reports as a warning while the daemon runs. It also stays up when it cannot
|
|
25
|
+
* write that exit record, since the backoff could never engage. A daemon that
|
|
26
|
+
* stayed up lists the HQ root every TCC_EPERM_PROBE_INTERVAL_MS; once that
|
|
27
|
+
* works it clears the marker and watches again.
|
|
28
|
+
*/
|
|
29
|
+
/** sysexits EX_IOERR. Not 75 (RESTART_EXIT_CODE) and not one exit-cause.ts treats as permanent (77, 78, 126, 127). */
|
|
30
|
+
export declare const TCC_EPERM_EXIT_CODE = 74;
|
|
31
|
+
/** EPERM hits under the HQ root count toward a trip only inside this window. */
|
|
32
|
+
export declare const TCC_EPERM_WINDOW_MS = 60000;
|
|
33
|
+
/** Hits needed inside the window. */
|
|
34
|
+
export declare const TCC_EPERM_MIN_EVENTS = 3;
|
|
35
|
+
/** Distinct paths needed inside the window, so one odd file cannot trip it. */
|
|
36
|
+
export declare const TCC_EPERM_MIN_PATHS = 2;
|
|
37
|
+
/** Exits allowed inside TCC_EPERM_EXIT_WINDOW_MS before the daemon stays up instead. */
|
|
38
|
+
export declare const TCC_EPERM_MAX_EXITS = 3;
|
|
39
|
+
export declare const TCC_EPERM_EXIT_WINDOW_MS: number;
|
|
40
|
+
/** Under the daemon dir: timestamps of recent EPERM exits (the backoff record). */
|
|
41
|
+
export declare const TCC_EPERM_EXITS_FILE = "tcc-eperm-exits.json";
|
|
42
|
+
/** Under the daemon dir: present while hqd is up but cannot read the HQ folder. */
|
|
43
|
+
export declare const FILES_ACCESS_NEEDED_MARKER = "files-access-needed.json";
|
|
44
|
+
export declare const FILES_ACCESS_NEEDED_MESSAGE: string;
|
|
45
|
+
export declare const TCC_EPERM_SENTRY_FINGERPRINT: readonly string[];
|
|
46
|
+
export interface EpermHit {
|
|
47
|
+
path: string;
|
|
48
|
+
syscall?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The EPERM hit `err` carries for a read under `hqRoot`, or null. Follows
|
|
52
|
+
* `cause` a few levels, since some ops wrap the fs error. EACCES is a plain
|
|
53
|
+
* permission bit, not TCC, and never counts; neither does an EPERM with no
|
|
54
|
+
* path (kill(2), for one) or from a syscall that does not read. The root's
|
|
55
|
+
* realpath is looked up only after an EPERM read is found, and is cached.
|
|
56
|
+
*/
|
|
57
|
+
export declare function epermUnderRoot(err: unknown, hqRoot: string): EpermHit | null;
|
|
58
|
+
export interface EpermWindowOptions {
|
|
59
|
+
windowMs?: number;
|
|
60
|
+
minEvents?: number;
|
|
61
|
+
minPaths?: number;
|
|
62
|
+
}
|
|
63
|
+
export interface EpermWindowSummary {
|
|
64
|
+
hits: number;
|
|
65
|
+
distinctPaths: number;
|
|
66
|
+
syscalls: string[];
|
|
67
|
+
}
|
|
68
|
+
/** Sliding window of EPERM hits; `record` returns true once the window holds enough of them. */
|
|
69
|
+
export declare function createEpermWindow(options?: EpermWindowOptions): {
|
|
70
|
+
record(hit: EpermHit, at: number): boolean;
|
|
71
|
+
summary(): EpermWindowSummary;
|
|
72
|
+
};
|
|
73
|
+
export type EpermDecision = {
|
|
74
|
+
action: "exit";
|
|
75
|
+
code: number;
|
|
76
|
+
} | {
|
|
77
|
+
action: "stay";
|
|
78
|
+
reason: StayReason;
|
|
79
|
+
} | {
|
|
80
|
+
action: "ignore";
|
|
81
|
+
reason: "not-macos" | "stopping";
|
|
82
|
+
};
|
|
83
|
+
/** Why the daemon stays up. "exit-not-recorded": the backoff record could not be written, so an exit could loop forever. */
|
|
84
|
+
export type StayReason = "backoff" | "not-restartable" | "exit-not-recorded";
|
|
85
|
+
export interface EpermDecisionInput {
|
|
86
|
+
platform: NodeJS.Platform;
|
|
87
|
+
stopping: boolean;
|
|
88
|
+
/** A service manager starts the daemon again after it exits with any code (launchd KeepAlive). */
|
|
89
|
+
restartable: boolean;
|
|
90
|
+
/** Earlier EPERM exits, epoch ms. */
|
|
91
|
+
recentExits: readonly number[];
|
|
92
|
+
now: number;
|
|
93
|
+
maxExits?: number;
|
|
94
|
+
exitWindowMs?: number;
|
|
95
|
+
}
|
|
96
|
+
/** What a tripped window should do. */
|
|
97
|
+
export declare function decideEpermExit(input: EpermDecisionInput): EpermDecision;
|
|
98
|
+
export declare function readEpermExits(daemonDir: string): number[];
|
|
99
|
+
/** Add an exit at `now`, dropping ones older than the backoff window. */
|
|
100
|
+
export declare function recordEpermExit(daemonDir: string, now: number, exitWindowMs?: number): number[];
|
|
101
|
+
export declare function writeFilesAccessNeeded(daemonDir: string, now: number, reason: StayReason): void;
|
|
102
|
+
export declare function clearFilesAccessNeeded(daemonDir: string): void;
|
|
103
|
+
export declare function filesAccessNeeded(daemonDir: string): boolean;
|
|
104
|
+
/** The one structured event logged and reported per trip. Holds counts only: no paths, no file contents. */
|
|
105
|
+
export interface TccEpermEvent {
|
|
106
|
+
event: "hqd.tcc_eperm";
|
|
107
|
+
platform: NodeJS.Platform;
|
|
108
|
+
hits: number;
|
|
109
|
+
distinctPaths: number;
|
|
110
|
+
syscalls: string[];
|
|
111
|
+
windowMs: number;
|
|
112
|
+
decision: EpermDecision["action"];
|
|
113
|
+
reason?: string;
|
|
114
|
+
exitCode?: number;
|
|
115
|
+
exitsInWindow: number;
|
|
116
|
+
}
|
|
117
|
+
export interface TccEpermGuardDeps {
|
|
118
|
+
/** The HQ root hqd serves; undefined while none is configured. */
|
|
119
|
+
hqRoot: () => string | undefined;
|
|
120
|
+
platform: NodeJS.Platform;
|
|
121
|
+
/** The daemon's private dir (~/.hq/daemon): backoff record and status marker live here. */
|
|
122
|
+
daemonDir: string;
|
|
123
|
+
restartable: boolean;
|
|
124
|
+
isStopping: () => boolean;
|
|
125
|
+
log: (level: "info" | "warn" | "error", message: string) => void;
|
|
126
|
+
/** Report to Sentry; resolves once the report is flushed or given up on. */
|
|
127
|
+
report: (event: TccEpermEvent) => Promise<void>;
|
|
128
|
+
/** Stop the daemon with this exit code. */
|
|
129
|
+
exit: (code: number) => void;
|
|
130
|
+
now?: () => number;
|
|
131
|
+
window?: EpermWindowOptions;
|
|
132
|
+
maxExits?: number;
|
|
133
|
+
exitWindowMs?: number;
|
|
134
|
+
/** Throws while the HQ root cannot be read. Default: list the root. */
|
|
135
|
+
probe?: (hqRoot: string) => void;
|
|
136
|
+
/** How often a daemon that stayed up checks whether access came back. */
|
|
137
|
+
probeIntervalMs?: number;
|
|
138
|
+
/** Run `fn` every `ms`; returns a cancel. Default: an unref'd setInterval. */
|
|
139
|
+
schedule?: (fn: () => void, ms: number) => () => void;
|
|
140
|
+
}
|
|
141
|
+
export type TccEpermGuardState = "watching" | "exiting" | "access-needed";
|
|
142
|
+
export interface TccEpermGuard {
|
|
143
|
+
observe(err: unknown): void;
|
|
144
|
+
state(): TccEpermGuardState;
|
|
145
|
+
/** While access is needed: probe the HQ root, and on success clear the marker and watch again. True when it re-armed. */
|
|
146
|
+
checkAccess(): boolean;
|
|
147
|
+
/** Stop the access probe timer. Called when the daemon starts shutting down. */
|
|
148
|
+
dispose(): void;
|
|
149
|
+
}
|
|
150
|
+
/** A daemon that stayed up checks this often whether macOS gave the HQ folder back. */
|
|
151
|
+
export declare const TCC_EPERM_PROBE_INTERVAL_MS = 60000;
|
|
152
|
+
export declare function createTccEpermGuard(deps: TccEpermGuardDeps): TccEpermGuard;
|
|
153
|
+
//# sourceMappingURL=tcc-eperm.d.ts.map
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Self-recovery for a daemon that macOS privacy (TCC) has cut off from the
|
|
3
|
+
* HQ folder.
|
|
4
|
+
*
|
|
5
|
+
* After HQ.app updates itself, a daemon that was started from the old app
|
|
6
|
+
* bundle keeps running from the deleted bundle. macOS then refuses its file
|
|
7
|
+
* reads and writes under the HQ root with EPERM ("operation not permitted"),
|
|
8
|
+
* and every hqd op that touches companies/ fails until someone runs
|
|
9
|
+
* `launchctl kickstart -k`. A fresh process started by launchd is checked
|
|
10
|
+
* again and gets access.
|
|
11
|
+
*
|
|
12
|
+
* So: when hqd sees EPERM on reads (open, stat, readdir and the like) of paths
|
|
13
|
+
* under the HQ root, repeated across more than one path within a short window,
|
|
14
|
+
* it logs one structured event, reports
|
|
15
|
+
* it once to Sentry under a fixed fingerprint, and exits with
|
|
16
|
+
* TCC_EPERM_EXIT_CODE. The daemon's LaunchAgent has KeepAlive=true, so launchd
|
|
17
|
+
* starts it again (after its ThrottleInterval) whatever the exit code.
|
|
18
|
+
*
|
|
19
|
+
* It never exits for EPERM outside the HQ root, for EACCES, off macOS, while
|
|
20
|
+
* the daemon is already stopping, or when no service manager would start it
|
|
21
|
+
* again. A permission that stays revoked would otherwise restart the daemon
|
|
22
|
+
* forever, so after TCC_EPERM_MAX_EXITS exits within TCC_EPERM_EXIT_WINDOW_MS
|
|
23
|
+
* it stays up and writes FILES_ACCESS_NEEDED_MARKER, which `hq daemon status`
|
|
24
|
+
* reports as a warning while the daemon runs. It also stays up when it cannot
|
|
25
|
+
* write that exit record, since the backoff could never engage. A daemon that
|
|
26
|
+
* stayed up lists the HQ root every TCC_EPERM_PROBE_INTERVAL_MS; once that
|
|
27
|
+
* works it clears the marker and watches again.
|
|
28
|
+
*/
|
|
29
|
+
import * as fs from "node:fs";
|
|
30
|
+
import * as path from "node:path";
|
|
31
|
+
/** sysexits EX_IOERR. Not 75 (RESTART_EXIT_CODE) and not one exit-cause.ts treats as permanent (77, 78, 126, 127). */
|
|
32
|
+
export const TCC_EPERM_EXIT_CODE = 74;
|
|
33
|
+
/** EPERM hits under the HQ root count toward a trip only inside this window. */
|
|
34
|
+
export const TCC_EPERM_WINDOW_MS = 60_000;
|
|
35
|
+
/** Hits needed inside the window. */
|
|
36
|
+
export const TCC_EPERM_MIN_EVENTS = 3;
|
|
37
|
+
/** Distinct paths needed inside the window, so one odd file cannot trip it. */
|
|
38
|
+
export const TCC_EPERM_MIN_PATHS = 2;
|
|
39
|
+
/** Exits allowed inside TCC_EPERM_EXIT_WINDOW_MS before the daemon stays up instead. */
|
|
40
|
+
export const TCC_EPERM_MAX_EXITS = 3;
|
|
41
|
+
export const TCC_EPERM_EXIT_WINDOW_MS = 30 * 60_000;
|
|
42
|
+
/** Under the daemon dir: timestamps of recent EPERM exits (the backoff record). */
|
|
43
|
+
export const TCC_EPERM_EXITS_FILE = "tcc-eperm-exits.json";
|
|
44
|
+
/** Under the daemon dir: present while hqd is up but cannot read the HQ folder. */
|
|
45
|
+
export const FILES_ACCESS_NEEDED_MARKER = "files-access-needed.json";
|
|
46
|
+
export const FILES_ACCESS_NEEDED_MESSAGE = "HQ needs Full Disk Access (or Files and Folders access) to your HQ folder. macOS is refusing the daemon's file reads and writes there. " +
|
|
47
|
+
"Open System Settings > Privacy & Security > Full Disk Access, allow HQ, then run: hq daemon restart";
|
|
48
|
+
export const TCC_EPERM_SENTRY_FINGERPRINT = ["hqd", "tcc-eperm-under-hq-root"];
|
|
49
|
+
/**
|
|
50
|
+
* Syscalls that read. macOS privacy refuses these with EPERM when it cuts a
|
|
51
|
+
* process off from a folder. chmod, chown, utime, unlink, rmdir and rename
|
|
52
|
+
* also fail with EPERM for ordinary reasons (file flags, the sticky bit,
|
|
53
|
+
* ownership), so they never count.
|
|
54
|
+
*/
|
|
55
|
+
const READ_SYSCALLS = new Set([
|
|
56
|
+
"open",
|
|
57
|
+
"read",
|
|
58
|
+
"readdir",
|
|
59
|
+
"scandir",
|
|
60
|
+
"opendir",
|
|
61
|
+
"stat",
|
|
62
|
+
"lstat",
|
|
63
|
+
"access",
|
|
64
|
+
"realpath",
|
|
65
|
+
]);
|
|
66
|
+
/** Last realpath lookup of the HQ root; looked up again only while it keeps failing. */
|
|
67
|
+
let rootCache = null;
|
|
68
|
+
function rootCandidates(hqRoot) {
|
|
69
|
+
if (rootCache && rootCache.hqRoot === hqRoot && rootCache.resolved)
|
|
70
|
+
return rootCache.roots;
|
|
71
|
+
const resolved = path.resolve(hqRoot);
|
|
72
|
+
const roots = [resolved];
|
|
73
|
+
let ok = false;
|
|
74
|
+
try {
|
|
75
|
+
const real = fs.realpathSync(resolved);
|
|
76
|
+
if (real !== resolved)
|
|
77
|
+
roots.push(real);
|
|
78
|
+
ok = true;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// The root itself may be unreadable right now; the lexical path still counts.
|
|
82
|
+
}
|
|
83
|
+
rootCache = { hqRoot, roots, resolved: ok };
|
|
84
|
+
return roots;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Lexical containment. A symlink inside the root that points at a protected
|
|
88
|
+
* folder outside it still counts; the exit backoff caps what that can cost.
|
|
89
|
+
*/
|
|
90
|
+
function within(root, candidate) {
|
|
91
|
+
const rel = path.relative(root, path.resolve(candidate));
|
|
92
|
+
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The EPERM hit `err` carries for a read under `hqRoot`, or null. Follows
|
|
96
|
+
* `cause` a few levels, since some ops wrap the fs error. EACCES is a plain
|
|
97
|
+
* permission bit, not TCC, and never counts; neither does an EPERM with no
|
|
98
|
+
* path (kill(2), for one) or from a syscall that does not read. The root's
|
|
99
|
+
* realpath is looked up only after an EPERM read is found, and is cached.
|
|
100
|
+
*/
|
|
101
|
+
export function epermUnderRoot(err, hqRoot) {
|
|
102
|
+
let current = err;
|
|
103
|
+
for (let depth = 0; depth < 4 && current && typeof current === "object"; depth += 1) {
|
|
104
|
+
const e = current;
|
|
105
|
+
const { code, syscall, path: candidate } = e;
|
|
106
|
+
if (code === "EPERM" && typeof syscall === "string" && READ_SYSCALLS.has(syscall) && typeof candidate === "string" && candidate !== "") {
|
|
107
|
+
if (rootCandidates(hqRoot).some((root) => within(root, candidate)))
|
|
108
|
+
return { path: candidate, syscall };
|
|
109
|
+
}
|
|
110
|
+
current = e.cause;
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
/** Sliding window of EPERM hits; `record` returns true once the window holds enough of them. */
|
|
115
|
+
export function createEpermWindow(options = {}) {
|
|
116
|
+
const windowMs = options.windowMs ?? TCC_EPERM_WINDOW_MS;
|
|
117
|
+
const minEvents = options.minEvents ?? TCC_EPERM_MIN_EVENTS;
|
|
118
|
+
const minPaths = options.minPaths ?? TCC_EPERM_MIN_PATHS;
|
|
119
|
+
let hits = [];
|
|
120
|
+
return {
|
|
121
|
+
record(hit, at) {
|
|
122
|
+
hits = hits.filter((h) => at - h.at < windowMs);
|
|
123
|
+
hits.push({ ...hit, at });
|
|
124
|
+
return hits.length >= minEvents && new Set(hits.map((h) => h.path)).size >= minPaths;
|
|
125
|
+
},
|
|
126
|
+
summary() {
|
|
127
|
+
return {
|
|
128
|
+
hits: hits.length,
|
|
129
|
+
distinctPaths: new Set(hits.map((h) => h.path)).size,
|
|
130
|
+
syscalls: [...new Set(hits.flatMap((h) => (h.syscall ? [h.syscall] : [])))].sort(),
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** What a tripped window should do. */
|
|
136
|
+
export function decideEpermExit(input) {
|
|
137
|
+
if (input.platform !== "darwin")
|
|
138
|
+
return { action: "ignore", reason: "not-macos" };
|
|
139
|
+
if (input.stopping)
|
|
140
|
+
return { action: "ignore", reason: "stopping" };
|
|
141
|
+
if (!input.restartable)
|
|
142
|
+
return { action: "stay", reason: "not-restartable" };
|
|
143
|
+
const windowMs = input.exitWindowMs ?? TCC_EPERM_EXIT_WINDOW_MS;
|
|
144
|
+
const recent = input.recentExits.filter((t) => input.now - t < windowMs && t <= input.now);
|
|
145
|
+
if (recent.length >= (input.maxExits ?? TCC_EPERM_MAX_EXITS))
|
|
146
|
+
return { action: "stay", reason: "backoff" };
|
|
147
|
+
return { action: "exit", code: TCC_EPERM_EXIT_CODE };
|
|
148
|
+
}
|
|
149
|
+
export function readEpermExits(daemonDir) {
|
|
150
|
+
try {
|
|
151
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(daemonDir, TCC_EPERM_EXITS_FILE), "utf8"));
|
|
152
|
+
return Array.isArray(parsed.exits) ? parsed.exits.filter((t) => typeof t === "number" && Number.isFinite(t)) : [];
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function writePrivateJson(file, value) {
|
|
159
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
160
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
161
|
+
fs.writeFileSync(tmp, `${JSON.stringify(value)}\n`, { mode: 0o600 });
|
|
162
|
+
fs.renameSync(tmp, file);
|
|
163
|
+
}
|
|
164
|
+
/** Add an exit at `now`, dropping ones older than the backoff window. */
|
|
165
|
+
export function recordEpermExit(daemonDir, now, exitWindowMs = TCC_EPERM_EXIT_WINDOW_MS) {
|
|
166
|
+
const exits = [...readEpermExits(daemonDir).filter((t) => now - t < exitWindowMs && t <= now), now];
|
|
167
|
+
writePrivateJson(path.join(daemonDir, TCC_EPERM_EXITS_FILE), { exits });
|
|
168
|
+
return exits;
|
|
169
|
+
}
|
|
170
|
+
export function writeFilesAccessNeeded(daemonDir, now, reason) {
|
|
171
|
+
writePrivateJson(path.join(daemonDir, FILES_ACCESS_NEEDED_MARKER), {
|
|
172
|
+
since: new Date(now).toISOString(),
|
|
173
|
+
reason,
|
|
174
|
+
message: FILES_ACCESS_NEEDED_MESSAGE,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
export function clearFilesAccessNeeded(daemonDir) {
|
|
178
|
+
fs.rmSync(path.join(daemonDir, FILES_ACCESS_NEEDED_MARKER), { force: true });
|
|
179
|
+
}
|
|
180
|
+
export function filesAccessNeeded(daemonDir) {
|
|
181
|
+
return fs.existsSync(path.join(daemonDir, FILES_ACCESS_NEEDED_MARKER));
|
|
182
|
+
}
|
|
183
|
+
/** A daemon that stayed up checks this often whether macOS gave the HQ folder back. */
|
|
184
|
+
export const TCC_EPERM_PROBE_INTERVAL_MS = 60_000;
|
|
185
|
+
const defaultSchedule = (fn, ms) => {
|
|
186
|
+
const timer = setInterval(fn, ms);
|
|
187
|
+
timer.unref?.();
|
|
188
|
+
return () => clearInterval(timer);
|
|
189
|
+
};
|
|
190
|
+
export function createTccEpermGuard(deps) {
|
|
191
|
+
const now = deps.now ?? Date.now;
|
|
192
|
+
const probe = deps.probe ?? ((root) => void fs.readdirSync(root));
|
|
193
|
+
const schedule = deps.schedule ?? defaultSchedule;
|
|
194
|
+
let window = createEpermWindow(deps.window);
|
|
195
|
+
const windowMs = deps.window?.windowMs ?? TCC_EPERM_WINDOW_MS;
|
|
196
|
+
let state = "watching";
|
|
197
|
+
let cancelProbe = null;
|
|
198
|
+
if (deps.platform === "darwin") {
|
|
199
|
+
// A new daemon starts unflagged; it writes the marker again if access is still refused.
|
|
200
|
+
try {
|
|
201
|
+
clearFilesAccessNeeded(deps.daemonDir);
|
|
202
|
+
}
|
|
203
|
+
catch (err) {
|
|
204
|
+
deps.log("warn", `could not clear ${FILES_ACCESS_NEEDED_MARKER}: ${err.message}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const report = (event) => deps.report(event).catch((err) => deps.log("warn", `could not report the hqd file access failure: ${String(err)}`));
|
|
208
|
+
const checkAccess = () => {
|
|
209
|
+
if (state !== "access-needed" || deps.isStopping())
|
|
210
|
+
return false;
|
|
211
|
+
const hqRoot = deps.hqRoot();
|
|
212
|
+
if (!hqRoot)
|
|
213
|
+
return false;
|
|
214
|
+
try {
|
|
215
|
+
probe(hqRoot);
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
cancelProbe?.();
|
|
221
|
+
cancelProbe = null;
|
|
222
|
+
window = createEpermWindow(deps.window);
|
|
223
|
+
state = "watching";
|
|
224
|
+
try {
|
|
225
|
+
clearFilesAccessNeeded(deps.daemonDir);
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
deps.log("warn", `could not clear ${FILES_ACCESS_NEEDED_MARKER}: ${err.message}`);
|
|
229
|
+
}
|
|
230
|
+
deps.log("info", "hqd: the HQ folder can be read again; watching for macOS file access errors again");
|
|
231
|
+
return true;
|
|
232
|
+
};
|
|
233
|
+
const stayUp = (at, reason) => {
|
|
234
|
+
state = "access-needed";
|
|
235
|
+
try {
|
|
236
|
+
writeFilesAccessNeeded(deps.daemonDir, at, reason);
|
|
237
|
+
}
|
|
238
|
+
catch (writeErr) {
|
|
239
|
+
deps.log("warn", `could not write ${FILES_ACCESS_NEEDED_MARKER}: ${writeErr.message}`);
|
|
240
|
+
}
|
|
241
|
+
cancelProbe ??= schedule(() => void checkAccess(), deps.probeIntervalMs ?? TCC_EPERM_PROBE_INTERVAL_MS);
|
|
242
|
+
};
|
|
243
|
+
return {
|
|
244
|
+
state: () => state,
|
|
245
|
+
checkAccess,
|
|
246
|
+
dispose() {
|
|
247
|
+
cancelProbe?.();
|
|
248
|
+
cancelProbe = null;
|
|
249
|
+
},
|
|
250
|
+
observe(err) {
|
|
251
|
+
if (state !== "watching" || deps.platform !== "darwin" || deps.isStopping())
|
|
252
|
+
return;
|
|
253
|
+
const hqRoot = deps.hqRoot();
|
|
254
|
+
if (!hqRoot)
|
|
255
|
+
return;
|
|
256
|
+
const hit = epermUnderRoot(err, hqRoot);
|
|
257
|
+
if (!hit)
|
|
258
|
+
return;
|
|
259
|
+
const at = now();
|
|
260
|
+
if (!window.record(hit, at))
|
|
261
|
+
return;
|
|
262
|
+
const recentExits = readEpermExits(deps.daemonDir);
|
|
263
|
+
let decision = decideEpermExit({
|
|
264
|
+
platform: deps.platform,
|
|
265
|
+
stopping: deps.isStopping(),
|
|
266
|
+
restartable: deps.restartable,
|
|
267
|
+
recentExits,
|
|
268
|
+
now: at,
|
|
269
|
+
maxExits: deps.maxExits,
|
|
270
|
+
exitWindowMs: deps.exitWindowMs,
|
|
271
|
+
});
|
|
272
|
+
if (decision.action === "ignore")
|
|
273
|
+
return;
|
|
274
|
+
let exitsInWindow = recentExits.filter((t) => at - t < (deps.exitWindowMs ?? TCC_EPERM_EXIT_WINDOW_MS) && t <= at).length;
|
|
275
|
+
if (decision.action === "exit") {
|
|
276
|
+
try {
|
|
277
|
+
exitsInWindow = recordEpermExit(deps.daemonDir, at, deps.exitWindowMs).length;
|
|
278
|
+
state = "exiting";
|
|
279
|
+
}
|
|
280
|
+
catch (writeErr) {
|
|
281
|
+
// Without the record the backoff never engages and launchd would restart the daemon forever.
|
|
282
|
+
deps.log("warn", `could not record the restart in ${TCC_EPERM_EXITS_FILE}, so staying up: ${writeErr.message}`);
|
|
283
|
+
decision = { action: "stay", reason: "exit-not-recorded" };
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (decision.action === "stay")
|
|
287
|
+
stayUp(at, decision.reason);
|
|
288
|
+
const event = {
|
|
289
|
+
event: "hqd.tcc_eperm",
|
|
290
|
+
platform: deps.platform,
|
|
291
|
+
...window.summary(),
|
|
292
|
+
windowMs,
|
|
293
|
+
decision: decision.action,
|
|
294
|
+
...(decision.action === "stay" ? { reason: decision.reason } : { exitCode: decision.code }),
|
|
295
|
+
exitsInWindow,
|
|
296
|
+
};
|
|
297
|
+
deps.log("error", `hqd: macOS refused file access under the HQ root ${JSON.stringify(event)}`);
|
|
298
|
+
if (decision.action === "exit") {
|
|
299
|
+
deps.log("error", "hqd: exiting so launchd starts a fresh daemon that macOS checks again");
|
|
300
|
+
const code = decision.code;
|
|
301
|
+
void report(event).finally(() => deps.exit(code));
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
deps.log("error", `hqd: staying up; ${FILES_ACCESS_NEEDED_MESSAGE}`);
|
|
305
|
+
void report(event);
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=tcc-eperm.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daemon's timed flag monitors: the four default-on kill switches read by
|
|
3
|
+
* the supervisor loop, and the HQ Anywhere runtime gate read by the hqd
|
|
4
|
+
* socket server. They are built here, apart from run.ts and server.ts, so the
|
|
5
|
+
* call-budget test (daemon-call-budget.test.ts) drives the same construction
|
|
6
|
+
* the daemon uses.
|
|
7
|
+
*
|
|
8
|
+
* Every monitor refreshes on the shared source's timer and reads the shared
|
|
9
|
+
* source's snapshot, so one 30-second tick costs one registry request however
|
|
10
|
+
* many flags are watched.
|
|
11
|
+
*/
|
|
12
|
+
import { type BoundedDaemonFlagMonitor, type BoundedDaemonFlagMonitorOptions } from "./flag-monitor.js";
|
|
13
|
+
import { type DaemonFlagSnapshotSource, type ReadDaemonFlagOptions } from "./flag.js";
|
|
14
|
+
type Log = (level: "info" | "warn" | "error", message: string) => void;
|
|
15
|
+
export interface DaemonKillSwitchMonitors {
|
|
16
|
+
childBootstrapHandoffWatch: BoundedDaemonFlagMonitor;
|
|
17
|
+
lanesReconcileFileWatch: BoundedDaemonFlagMonitor;
|
|
18
|
+
searchIndexRunnerOwnsIndex: BoundedDaemonFlagMonitor;
|
|
19
|
+
syncProcessScanOnMissing: BoundedDaemonFlagMonitor;
|
|
20
|
+
close(): void;
|
|
21
|
+
}
|
|
22
|
+
export interface DaemonKillSwitchMonitorOptions {
|
|
23
|
+
env: NodeJS.ProcessEnv;
|
|
24
|
+
log: Log;
|
|
25
|
+
/** Omit to give each monitor its own timer and its own registry request (the behavior before sharing). */
|
|
26
|
+
snapshots?: DaemonFlagSnapshotSource;
|
|
27
|
+
/** Test seam for the registry request. */
|
|
28
|
+
readOptions?: Pick<ReadDaemonFlagOptions, "createClient" | "getToken">;
|
|
29
|
+
}
|
|
30
|
+
/** One default-on kill switch per daemon behavior that can be turned off from the registry. */
|
|
31
|
+
export declare function createDaemonKillSwitchMonitors(options: DaemonKillSwitchMonitorOptions): DaemonKillSwitchMonitors;
|
|
32
|
+
export interface HqdRuntimeFlagMonitorOptions {
|
|
33
|
+
env: NodeJS.ProcessEnv;
|
|
34
|
+
log: Log;
|
|
35
|
+
/** Omit to keep this monitor on its own timer and its own registry request. */
|
|
36
|
+
snapshots?: DaemonFlagSnapshotSource;
|
|
37
|
+
/** Test seam for the live hq-flags read. */
|
|
38
|
+
runtimeFlagReader?: (options: ReadDaemonFlagOptions) => Promise<boolean>;
|
|
39
|
+
/** Test seam for the human opt-in setting; production reads hq-pro. */
|
|
40
|
+
personSettingReader?: () => Promise<boolean | null>;
|
|
41
|
+
onChange?: BoundedDaemonFlagMonitorOptions["onChange"];
|
|
42
|
+
onReadError?: BoundedDaemonFlagMonitorOptions["onReadError"];
|
|
43
|
+
}
|
|
44
|
+
/** hqd's HQ Anywhere gate: the operator flag, then the person's own setting while the flag is on. */
|
|
45
|
+
export declare function createHqdRuntimeFlagMonitor(options: HqdRuntimeFlagMonitorOptions): BoundedDaemonFlagMonitor;
|
|
46
|
+
export {};
|
|
47
|
+
//# sourceMappingURL=timed-flag-monitors.d.ts.map
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daemon's timed flag monitors: the four default-on kill switches read by
|
|
3
|
+
* the supervisor loop, and the HQ Anywhere runtime gate read by the hqd
|
|
4
|
+
* socket server. They are built here, apart from run.ts and server.ts, so the
|
|
5
|
+
* call-budget test (daemon-call-budget.test.ts) drives the same construction
|
|
6
|
+
* the daemon uses.
|
|
7
|
+
*
|
|
8
|
+
* Every monitor refreshes on the shared source's timer and reads the shared
|
|
9
|
+
* source's snapshot, so one 30-second tick costs one registry request however
|
|
10
|
+
* many flags are watched.
|
|
11
|
+
*/
|
|
12
|
+
import { HQ_ANYWHERE_RUNTIME_FLAG } from "../anywhere-runtime-flag-key.js";
|
|
13
|
+
import { readHqAnywherePersonSetting } from "../anywhere-person-setting.js";
|
|
14
|
+
import { createBoundedDaemonFlagMonitor, } from "./flag-monitor.js";
|
|
15
|
+
import { readDaemonFlag } from "./flag.js";
|
|
16
|
+
import { DAEMON_CHILD_BOOTSTRAP_HANDOFF_WATCH_FLAG, DAEMON_LANES_RECONCILE_FILE_WATCH_FLAG, DAEMON_SEARCH_INDEX_RUNNER_OWNS_INDEX_FLAG, DAEMON_SYNC_PROCESS_SCAN_ON_MISSING_FLAG, } from "./kill-switch-flags.js";
|
|
17
|
+
/** One default-on kill switch per daemon behavior that can be turned off from the registry. */
|
|
18
|
+
export function createDaemonKillSwitchMonitors(options) {
|
|
19
|
+
const { env, log, snapshots } = options;
|
|
20
|
+
const monitor = (flagKey) => createBoundedDaemonFlagMonitor({
|
|
21
|
+
fallback: true,
|
|
22
|
+
read: () => readDaemonFlag(flagKey, {
|
|
23
|
+
env,
|
|
24
|
+
fallback: true,
|
|
25
|
+
localOverrideMode: "ignore",
|
|
26
|
+
throwOnError: true,
|
|
27
|
+
...options.readOptions,
|
|
28
|
+
...(snapshots ? { snapshots } : {}),
|
|
29
|
+
}),
|
|
30
|
+
onReadError: (error) => log("warn", `could not read ${flagKey}; keeping its default-on behavior (${error instanceof Error ? error.name : "UnknownError"})`),
|
|
31
|
+
...(snapshots ? { scheduleRefresh: (tick) => snapshots.schedule(tick) } : {}),
|
|
32
|
+
});
|
|
33
|
+
const monitors = {
|
|
34
|
+
childBootstrapHandoffWatch: monitor(DAEMON_CHILD_BOOTSTRAP_HANDOFF_WATCH_FLAG),
|
|
35
|
+
lanesReconcileFileWatch: monitor(DAEMON_LANES_RECONCILE_FILE_WATCH_FLAG),
|
|
36
|
+
searchIndexRunnerOwnsIndex: monitor(DAEMON_SEARCH_INDEX_RUNNER_OWNS_INDEX_FLAG),
|
|
37
|
+
syncProcessScanOnMissing: monitor(DAEMON_SYNC_PROCESS_SCAN_ON_MISSING_FLAG),
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
...monitors,
|
|
41
|
+
close() {
|
|
42
|
+
for (const each of Object.values(monitors))
|
|
43
|
+
each.close();
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** hqd's HQ Anywhere gate: the operator flag, then the person's own setting while the flag is on. */
|
|
48
|
+
export function createHqdRuntimeFlagMonitor(options) {
|
|
49
|
+
const { env, log, snapshots } = options;
|
|
50
|
+
return createBoundedDaemonFlagMonitor({
|
|
51
|
+
read: async () => {
|
|
52
|
+
const readOptions = {
|
|
53
|
+
env,
|
|
54
|
+
throwOnError: true,
|
|
55
|
+
// A local opt-out is honored, while a local opt-in cannot override the operator kill switch.
|
|
56
|
+
localOverrideMode: "off-only",
|
|
57
|
+
...(snapshots ? { snapshots } : {}),
|
|
58
|
+
};
|
|
59
|
+
const flagEnabled = await (options.runtimeFlagReader
|
|
60
|
+
? options.runtimeFlagReader(readOptions)
|
|
61
|
+
: readDaemonFlag(HQ_ANYWHERE_RUNTIME_FLAG, readOptions));
|
|
62
|
+
if (!flagEnabled)
|
|
63
|
+
return false;
|
|
64
|
+
const personSetting = await (options.personSettingReader ?? (() => readHqAnywherePersonSetting({
|
|
65
|
+
env,
|
|
66
|
+
onError: (name) => log("warn", `hqd person setting refresh failed (${name}); keeping HQ Anywhere off`),
|
|
67
|
+
})))();
|
|
68
|
+
return personSetting === true;
|
|
69
|
+
},
|
|
70
|
+
...(options.onChange ? { onChange: options.onChange } : {}),
|
|
71
|
+
...(options.onReadError ? { onReadError: options.onReadError } : {}),
|
|
72
|
+
...(snapshots ? { scheduleRefresh: (tick) => snapshots.schedule(tick) } : {}),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=timed-flag-monitors.js.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deployment admission (US-009).
|
|
3
|
+
*
|
|
4
|
+
* A deployment needs both an exact artifact and an explicit policy:
|
|
5
|
+
* - the request names the artifact id and the sha256 digest it expects;
|
|
6
|
+
* - the artifact bytes come from ArtifactStore.read(), which re-checks the
|
|
7
|
+
* provenance signature, the promotion signature and the blob digest;
|
|
8
|
+
* - the artifact was built by a release-trust worker into the release
|
|
9
|
+
* partition and promoted;
|
|
10
|
+
* - the requester's company policy, loaded from the store (never from the
|
|
11
|
+
* request) and set there only by an authenticated company admin, carries a
|
|
12
|
+
* valid admin signature, lists the environment, approves it, names this
|
|
13
|
+
* target and allows the artifact's source repository;
|
|
14
|
+
* - the deploy attempt is open, claimed by the requesting worker, in the
|
|
15
|
+
* current controller epoch.
|
|
16
|
+
*
|
|
17
|
+
* Any failure is an exception: admit() returns a decision scoped to the
|
|
18
|
+
* requester's company and environment, which effects.ts records as a blocked
|
|
19
|
+
* deployment and escalates to that company's admin. There is no override
|
|
20
|
+
* field and no bypass; a request with unknown fields, including a policy, is
|
|
21
|
+
* refused outright.
|
|
22
|
+
*
|
|
23
|
+
* Every admission produces a snapshot of the policy, artifact identity and
|
|
24
|
+
* request context. effects.ts stores it with its sha256 (snapshotDigest) and
|
|
25
|
+
* an HMAC (snapshotSignature) and never changes it.
|
|
26
|
+
*/
|
|
27
|
+
import type { ArtifactStore } from "./artifacts.js";
|
|
28
|
+
import type { WorkerIdentity } from "./identity.js";
|
|
29
|
+
import { type DecisionCode, type DeploymentSnapshot, type StoredDeployPolicy, type StoredWorker } from "./migrations.js";
|
|
30
|
+
import { type ArtifactProvenance } from "./provenance.js";
|
|
31
|
+
import type { AttemptView } from "./reconciler.js";
|
|
32
|
+
export interface EnvironmentPolicy {
|
|
33
|
+
/** Explicit approval. A listed but unapproved environment is blocked. */
|
|
34
|
+
approved: boolean;
|
|
35
|
+
/** The deployment target this environment deploys to. */
|
|
36
|
+
targetId: string;
|
|
37
|
+
/** Source repositories whose artifacts may deploy here. */
|
|
38
|
+
allowedRepositories: string[];
|
|
39
|
+
}
|
|
40
|
+
export interface DeployPolicy {
|
|
41
|
+
policyId: string;
|
|
42
|
+
/** The company the policy belongs to. Must be the requester's company. */
|
|
43
|
+
tenantId: string;
|
|
44
|
+
version: number;
|
|
45
|
+
environments: Record<string, EnvironmentPolicy>;
|
|
46
|
+
}
|
|
47
|
+
export interface DeployRequest {
|
|
48
|
+
/** Open deploy attempt claimed by the requesting worker. */
|
|
49
|
+
attemptId: string;
|
|
50
|
+
artifactId: string;
|
|
51
|
+
/** sha256 hex the requester expects the artifact to have. */
|
|
52
|
+
digest: string;
|
|
53
|
+
environment: string;
|
|
54
|
+
}
|
|
55
|
+
export interface AdmissionInput {
|
|
56
|
+
identity: WorkerIdentity;
|
|
57
|
+
worker: StoredWorker;
|
|
58
|
+
attempt: AttemptView | undefined;
|
|
59
|
+
controllerEpoch: number;
|
|
60
|
+
now: string;
|
|
61
|
+
targetId: string;
|
|
62
|
+
artifacts: ArtifactStore;
|
|
63
|
+
request: DeployRequest;
|
|
64
|
+
/** The requester's company policy from the store, or null when none was set. */
|
|
65
|
+
storedPolicy: StoredDeployPolicy | null;
|
|
66
|
+
/** True when the stored policy's admin signature verified with the signing key. */
|
|
67
|
+
policyVerified: boolean;
|
|
68
|
+
}
|
|
69
|
+
/** Shape check of a policy an admin is about to store. Returns the problem, or null. */
|
|
70
|
+
export declare function deployPolicyProblem(p: unknown): string | null;
|
|
71
|
+
export type Admission = {
|
|
72
|
+
ok: true;
|
|
73
|
+
snapshot: DeploymentSnapshot;
|
|
74
|
+
bytes: Buffer;
|
|
75
|
+
provenance: ArtifactProvenance;
|
|
76
|
+
} | {
|
|
77
|
+
ok: false;
|
|
78
|
+
snapshot: DeploymentSnapshot;
|
|
79
|
+
code: DecisionCode;
|
|
80
|
+
found: string;
|
|
81
|
+
};
|
|
82
|
+
/** Refuse a malformed request before anything is recorded. */
|
|
83
|
+
export declare function checkRequest(input: unknown): DeployRequest;
|
|
84
|
+
/** Decide one request. Pure apart from the verified artifact read. */
|
|
85
|
+
export declare function admit(input: AdmissionInput): Admission;
|
|
86
|
+
//# sourceMappingURL=admission.d.ts.map
|