@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
package/dist/lib/daemon/run.d.ts
CHANGED
|
@@ -123,6 +123,15 @@ export declare function restartsForParent(opts: {
|
|
|
123
123
|
managed?: boolean;
|
|
124
124
|
host?: HostApp;
|
|
125
125
|
}): boolean;
|
|
126
|
+
/**
|
|
127
|
+
* A parent starts the daemon again after it exits with TCC_EPERM_EXIT_CODE.
|
|
128
|
+
* Only the OS unit (launchd KeepAlive) restarts on any exit code; an app that
|
|
129
|
+
* hosts the daemon restarts it on RESTART_EXIT_CODE only.
|
|
130
|
+
*/
|
|
131
|
+
export declare function restartsAfterTccEpermExit(opts: {
|
|
132
|
+
managed?: boolean;
|
|
133
|
+
host?: HostApp;
|
|
134
|
+
}): boolean;
|
|
126
135
|
/**
|
|
127
136
|
* The desktop app runs sync itself, so the daemon's sync unit waits. When the
|
|
128
137
|
* app hosts the daemon it has handed sync over, and the daemon runs it.
|
package/dist/lib/daemon/run.js
CHANGED
|
@@ -30,9 +30,8 @@ import { assertPersonalServiceSessionCapability, readRemoteControlPoolsConfig, r
|
|
|
30
30
|
import { loadLanesConfig } from "../lanes/config.js";
|
|
31
31
|
import { daemonLanesReconcileHandler } from "../lanes/daemon-reconcile.js";
|
|
32
32
|
import { ensureDaemonLanesReconcileHandler } from "./lanes-reconcile-loader.js";
|
|
33
|
-
import { SUPERVISOR_HANDOFF_FLAG, readDaemonFlag } from "./flag.js";
|
|
34
|
-
import {
|
|
35
|
-
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";
|
|
33
|
+
import { SUPERVISOR_HANDOFF_FLAG, createDaemonFlagSnapshotSource, readDaemonFlag } from "./flag.js";
|
|
34
|
+
import { createDaemonKillSwitchMonitors } from "./timed-flag-monitors.js";
|
|
36
35
|
import { createLanesReconcileWatcher } from "./lanes-reconcile-watcher.js";
|
|
37
36
|
import { findLegacyUnits, legacyMeshUnitsToRetire, legacyOwnerLookup, } from "./legacy.js";
|
|
38
37
|
import { readLegacySyncEnv } from "./legacy-sync-env.js";
|
|
@@ -47,6 +46,8 @@ import { updateAnywhereRuntimeRevocationMarker } from "../anywhere-runtime-revoc
|
|
|
47
46
|
import { classifyWindowsTask, currentWindowsIdentity, daemonRootRefusal } from "./install.js";
|
|
48
47
|
import { detectMachineType } from "./machine-type.js";
|
|
49
48
|
import { appendPrivate, daemonPaths, ensurePrivateDir, openPrivateAppend } from "./paths.js";
|
|
49
|
+
import { SELF_CHECK_RESULT_FILE, readSelfCheckResult } from "./selfcheck.js";
|
|
50
|
+
import { createDaemonStateWriter, daemonStateFilePath, defaultDaemonStateFilePath, readVaultAccessManifest, selfCheckMachineChecks, } from "./state-file.js";
|
|
50
51
|
import { hqdSocketPath } from "./protocol.js";
|
|
51
52
|
import { startHqdServer, syncHqdEnabledMarker } from "./server.js";
|
|
52
53
|
import { startHqdRuntimeListener } from "./anywhere-listener.js";
|
|
@@ -62,6 +63,7 @@ import { listSyncRunnerProcesses, readWatchRunnerOwnerStatus, SYNC_RUNNER_LEASE_
|
|
|
62
63
|
import { STOP_ESCALATION_MS, SYNC_RUNNER_PERSIST_GRACE_MS, createSupervisor, planKey, } from "./supervisor.js";
|
|
63
64
|
import { userToolDirs } from "./tools.js";
|
|
64
65
|
import { logOffset, readLogSince, readUnitHolds, writeUnitHolds } from "./unit-holds.js";
|
|
66
|
+
import { createTccEpermGuard, TCC_EPERM_SENTRY_FINGERPRINT } from "./tcc-eperm.js";
|
|
65
67
|
import { daemonRunGuard } from "./lanes-reconcile-guard.js";
|
|
66
68
|
export const LOG_ROTATE_BYTES = 5 * 1024 * 1024;
|
|
67
69
|
/** Exit code for "restart me": non-zero so every service manager relaunches (Windows tasks only restart on failure). */
|
|
@@ -490,6 +492,14 @@ export function desktopAppRunning(platform) {
|
|
|
490
492
|
export function restartsForParent(opts) {
|
|
491
493
|
return opts.managed === true || opts.host !== undefined;
|
|
492
494
|
}
|
|
495
|
+
/**
|
|
496
|
+
* A parent starts the daemon again after it exits with TCC_EPERM_EXIT_CODE.
|
|
497
|
+
* Only the OS unit (launchd KeepAlive) restarts on any exit code; an app that
|
|
498
|
+
* hosts the daemon restarts it on RESTART_EXIT_CODE only.
|
|
499
|
+
*/
|
|
500
|
+
export function restartsAfterTccEpermExit(opts) {
|
|
501
|
+
return opts.managed === true && opts.host === undefined;
|
|
502
|
+
}
|
|
493
503
|
/**
|
|
494
504
|
* The desktop app runs sync itself, so the daemon's sync unit waits. When the
|
|
495
505
|
* app hosts the daemon it has handed sync over, and the daemon runs it.
|
|
@@ -896,20 +906,24 @@ export async function runDaemon(opts) {
|
|
|
896
906
|
};
|
|
897
907
|
const daemonSyncRunnerPids = new Set();
|
|
898
908
|
let supervisor = null;
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
909
|
+
// One registry request per 30-second tick decides every timed daemon flag.
|
|
910
|
+
// The same request also refreshes the state file hooks read (state-file.ts).
|
|
911
|
+
const stateWriter = createDaemonStateWriter({
|
|
912
|
+
file: daemonStateFilePath(home, env),
|
|
913
|
+
defaultFile: defaultDaemonStateFilePath(home, env),
|
|
914
|
+
log,
|
|
915
|
+
readVaultAccess: () => {
|
|
916
|
+
const configured = readDaemonConfig(paths.configPath).hqRoot ?? (env.HQ_ROOT?.trim() || undefined);
|
|
917
|
+
return readVaultAccessManifest(configured && fs.existsSync(configured) ? configured : undefined);
|
|
918
|
+
},
|
|
919
|
+
readMachineChecks: () => selfCheckMachineChecks(readSelfCheckResult(path.join(paths.root, SELF_CHECK_RESULT_FILE))),
|
|
908
920
|
});
|
|
909
|
-
const
|
|
910
|
-
const
|
|
911
|
-
const
|
|
912
|
-
const
|
|
921
|
+
const flagSnapshots = createDaemonFlagSnapshotSource({ onRead: stateWriter.onRegistryRead });
|
|
922
|
+
const killSwitches = createDaemonKillSwitchMonitors({ env, log, snapshots: flagSnapshots });
|
|
923
|
+
const childBootstrapHandoffWatchFlag = killSwitches.childBootstrapHandoffWatch;
|
|
924
|
+
const lanesReconcileFileWatchFlag = killSwitches.lanesReconcileFileWatch;
|
|
925
|
+
const searchIndexRunnerOwnsIndexFlag = killSwitches.searchIndexRunnerOwnsIndex;
|
|
926
|
+
const syncProcessScanOnMissingFlag = killSwitches.syncProcessScanOnMissing;
|
|
913
927
|
let laneReconcileWatcher = null;
|
|
914
928
|
let laneReconcileWatcherRoot = null;
|
|
915
929
|
let laneReconcileWatchFailedRoot = null;
|
|
@@ -936,10 +950,9 @@ export async function runDaemon(opts) {
|
|
|
936
950
|
laneReconcileWatcher?.close();
|
|
937
951
|
laneReconcileWatcher = null;
|
|
938
952
|
laneReconcileWatcherRoot = null;
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
syncProcessScanOnMissingFlag.close();
|
|
953
|
+
killSwitches.close();
|
|
954
|
+
flagSnapshots.close();
|
|
955
|
+
stateWriter.close();
|
|
943
956
|
};
|
|
944
957
|
const plan = () => {
|
|
945
958
|
const config = readDaemonConfig(paths.configPath);
|
|
@@ -1435,6 +1448,22 @@ export async function runDaemon(opts) {
|
|
|
1435
1448
|
env,
|
|
1436
1449
|
onError: (name) => log("warn", `hqd person setting read failed (${name}); keeping the last read value for up to ${HQD_PERSON_SETTING_GRACE_MS / 60_000} minutes`),
|
|
1437
1450
|
}), { graceMs: HQD_PERSON_SETTING_GRACE_MS });
|
|
1451
|
+
// macOS can stop granting this process access to the HQ folder (TCC) after
|
|
1452
|
+
// HQ.app replaces its bundle. A fresh process gets access again, so trip,
|
|
1453
|
+
// report once, and exit for launchd's KeepAlive to start a new daemon.
|
|
1454
|
+
let daemonStopping = false;
|
|
1455
|
+
let tccEpermExit = () => { };
|
|
1456
|
+
let hqdRootForGuard;
|
|
1457
|
+
const tccEpermGuard = createTccEpermGuard({
|
|
1458
|
+
hqRoot: () => hqdRootForGuard,
|
|
1459
|
+
platform,
|
|
1460
|
+
daemonDir: paths.root,
|
|
1461
|
+
restartable: restartsAfterTccEpermExit(opts),
|
|
1462
|
+
isStopping: () => daemonStopping,
|
|
1463
|
+
log,
|
|
1464
|
+
report: reportTccEperm,
|
|
1465
|
+
exit: (code) => tccEpermExit(code),
|
|
1466
|
+
});
|
|
1438
1467
|
const hqdListener = await startHqdRuntimeListener({
|
|
1439
1468
|
isEnabled: async () => refreshHqdRuntimeDecision(async () => shouldStartHqd(await anywhereRuntimeAllowed(env, {
|
|
1440
1469
|
refreshFlag: async () => {
|
|
@@ -1445,13 +1474,17 @@ export async function runDaemon(opts) {
|
|
|
1445
1474
|
})), home, env, (error) => log("warn", `hqd runtime marker update failed: ${error.message ?? String(error)}`), (enabled) => syncHqdEnabledMarker(hqdSocketPath(home, env), enabled)),
|
|
1446
1475
|
startServer: async () => {
|
|
1447
1476
|
const startConfig = readDaemonConfig(paths.configPath);
|
|
1477
|
+
const hqRoot = startConfig.hqRoot ?? (env.HQ_ROOT?.trim() || undefined);
|
|
1478
|
+
hqdRootForGuard = hqRoot;
|
|
1448
1479
|
return startHqdServer({
|
|
1449
1480
|
socketPath: hqdSocketPath(home, env),
|
|
1450
1481
|
registryPath: registryFile(registryDir(home, env)),
|
|
1451
|
-
hqRoot
|
|
1482
|
+
hqRoot,
|
|
1452
1483
|
log,
|
|
1484
|
+
flagSnapshots,
|
|
1453
1485
|
onShutdown: () => hqdShutdown?.(),
|
|
1454
1486
|
reportError: reportHqdCrash,
|
|
1487
|
+
observeError: (err) => tccEpermGuard.observe(err),
|
|
1455
1488
|
});
|
|
1456
1489
|
},
|
|
1457
1490
|
onServerChange: (server) => {
|
|
@@ -1480,6 +1513,8 @@ export async function runDaemon(opts) {
|
|
|
1480
1513
|
if (stopping)
|
|
1481
1514
|
return;
|
|
1482
1515
|
stopping = true;
|
|
1516
|
+
daemonStopping = true;
|
|
1517
|
+
tccEpermGuard.dispose();
|
|
1483
1518
|
log("info", `${why}; stopping services`);
|
|
1484
1519
|
closeHqd();
|
|
1485
1520
|
closeRuntimeWatchers();
|
|
@@ -1509,6 +1544,8 @@ export async function runDaemon(opts) {
|
|
|
1509
1544
|
if (stopping)
|
|
1510
1545
|
return;
|
|
1511
1546
|
stopping = true;
|
|
1547
|
+
daemonStopping = true;
|
|
1548
|
+
tccEpermGuard.dispose();
|
|
1512
1549
|
closeHqd();
|
|
1513
1550
|
closeRuntimeWatchers();
|
|
1514
1551
|
const released = supervisor.release();
|
|
@@ -1569,6 +1606,7 @@ export async function runDaemon(opts) {
|
|
|
1569
1606
|
});
|
|
1570
1607
|
};
|
|
1571
1608
|
hqdShutdown = () => shutdown("hqd shutdown request");
|
|
1609
|
+
tccEpermExit = (code) => shutdown("macOS refused file access under the HQ root; exiting so launchd starts a fresh daemon", code);
|
|
1572
1610
|
process.on("SIGTERM", () => shutdown("received SIGTERM"));
|
|
1573
1611
|
process.on("SIGINT", () => shutdown("received SIGINT"));
|
|
1574
1612
|
if (platform !== "win32")
|
|
@@ -1592,6 +1630,26 @@ export async function runDaemon(opts) {
|
|
|
1592
1630
|
}
|
|
1593
1631
|
});
|
|
1594
1632
|
}
|
|
1633
|
+
/** Report one macOS file-access trip under a fixed fingerprint (counts only, no paths), then flush before the exit. */
|
|
1634
|
+
async function reportTccEperm(event) {
|
|
1635
|
+
try {
|
|
1636
|
+
const { Sentry, preloadSentrySdk } = await import("../../sentry.js");
|
|
1637
|
+
preloadSentrySdk();
|
|
1638
|
+
Sentry.withScope((scope) => {
|
|
1639
|
+
scope.setLevel("error");
|
|
1640
|
+
scope.setFingerprint([...TCC_EPERM_SENTRY_FINGERPRINT]);
|
|
1641
|
+
scope.setTag("component", "hqd");
|
|
1642
|
+
scope.setTag("hqd.event", event.event);
|
|
1643
|
+
scope.setTag("hqd.tcc_eperm.decision", event.decision);
|
|
1644
|
+
scope.setContext("hqd_tcc_eperm", { ...event });
|
|
1645
|
+
Sentry.captureMessage("hqd lost file access under the HQ root (macOS EPERM)");
|
|
1646
|
+
});
|
|
1647
|
+
await Sentry.flush(2_000);
|
|
1648
|
+
}
|
|
1649
|
+
catch (err) {
|
|
1650
|
+
process.stderr.write(`hqd could not report the file access failure: ${String(err)}\n`);
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1595
1653
|
/** Report an hqd op crash to the CLI's existing Sentry integration, tagged with the failing op. */
|
|
1596
1654
|
function reportHqdCrash(op, err) {
|
|
1597
1655
|
void import("../../sentry.js").then(({ Sentry }) => {
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* units are affected, naming every affected vault in that one DM. The 16-hour outage of 2026-11-03 was
|
|
9
9
|
* found by hand; this makes the next one reach the owner.
|
|
10
10
|
*/
|
|
11
|
+
import { type StoreFileWriteOptions } from "./paths.js";
|
|
11
12
|
import type { DaemonState, PersistedHolds } from "./supervisor.js";
|
|
12
13
|
/** An engine unit that has been held or failing this long needs a person. */
|
|
13
14
|
export declare const ATTENTION_AFTER_MS: number;
|
|
@@ -31,6 +32,25 @@ export interface SelfCheckRecord {
|
|
|
31
32
|
}
|
|
32
33
|
export declare function readSelfCheckRecord(file: string): SelfCheckRecord | null;
|
|
33
34
|
export declare function writeSelfCheckRecord(file: string, record: SelfCheckRecord): void;
|
|
35
|
+
/**
|
|
36
|
+
* What the latest self-check run found, written on every run whether or not a
|
|
37
|
+
* DM was sent. The DM record above only changes when a DM goes out, so it
|
|
38
|
+
* cannot say that a machine is healthy or has recovered; this can.
|
|
39
|
+
*/
|
|
40
|
+
export declare const SELF_CHECK_RESULT_FILE = "selfcheck-result.json";
|
|
41
|
+
export interface SelfCheckResult {
|
|
42
|
+
checkedAt: number;
|
|
43
|
+
/** Engine units needing attention at that time. */
|
|
44
|
+
problems: number;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The latest result, or null. A file that is a symlink, has another link, is
|
|
48
|
+
* not this user's, or that group or others can write is not read: its value
|
|
49
|
+
* reaches the state file hooks trust.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readSelfCheckResult(file: string): SelfCheckResult | null;
|
|
52
|
+
/** Written atomically through src/lib/safe-store.ts in the daemon's private folder; a link at the path is replaced, never written through. */
|
|
53
|
+
export declare function writeSelfCheckResult(file: string, result: SelfCheckResult, options?: StoreFileWriteOptions): void;
|
|
34
54
|
/**
|
|
35
55
|
* Send now: something is wrong and this machine has not sent a self-check DM
|
|
36
56
|
* in the last day. Which units are affected does not matter, so units that
|
|
@@ -48,6 +68,8 @@ export interface SelfCheckDeps {
|
|
|
48
68
|
statePath: string;
|
|
49
69
|
holdsPath: string;
|
|
50
70
|
recordPath: string;
|
|
71
|
+
/** Where the latest run's result is written, for the daemon's state file. Not written when omitted. */
|
|
72
|
+
resultPath?: string;
|
|
51
73
|
now: () => number;
|
|
52
74
|
machine: string;
|
|
53
75
|
readState: () => DaemonState | null;
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
import * as fs from "node:fs";
|
|
12
12
|
import * as path from "node:path";
|
|
13
13
|
import { syncCoreUnitId } from "../sync-core/engine-home.js";
|
|
14
|
-
import { ensurePrivateDir } from "./paths.js";
|
|
14
|
+
import { ensurePrivateDir, readStoreFile, writeStoreFile } from "./paths.js";
|
|
15
15
|
/** An engine unit that has been held or failing this long needs a person. */
|
|
16
16
|
export const ATTENTION_AFTER_MS = 30 * 60_000;
|
|
17
17
|
/** This machine sends at most one self-check DM per this long. */
|
|
@@ -96,6 +96,37 @@ export function writeSelfCheckRecord(file, record) {
|
|
|
96
96
|
fs.writeFileSync(tmp, `${JSON.stringify(record, null, 2)}\n`, { mode: 0o600 });
|
|
97
97
|
fs.renameSync(tmp, file);
|
|
98
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* What the latest self-check run found, written on every run whether or not a
|
|
101
|
+
* DM was sent. The DM record above only changes when a DM goes out, so it
|
|
102
|
+
* cannot say that a machine is healthy or has recovered; this can.
|
|
103
|
+
*/
|
|
104
|
+
export const SELF_CHECK_RESULT_FILE = "selfcheck-result.json";
|
|
105
|
+
/**
|
|
106
|
+
* The latest result, or null. A file that is a symlink, has another link, is
|
|
107
|
+
* not this user's, or that group or others can write is not read: its value
|
|
108
|
+
* reaches the state file hooks trust.
|
|
109
|
+
*/
|
|
110
|
+
export function readSelfCheckResult(file) {
|
|
111
|
+
const read = readStoreFile(file, 4096);
|
|
112
|
+
if (!read)
|
|
113
|
+
return null;
|
|
114
|
+
try {
|
|
115
|
+
const parsed = JSON.parse(read.text);
|
|
116
|
+
if (typeof parsed.checkedAt !== "number" || !Number.isFinite(parsed.checkedAt))
|
|
117
|
+
return null;
|
|
118
|
+
if (typeof parsed.problems !== "number" || !Number.isInteger(parsed.problems) || parsed.problems < 0)
|
|
119
|
+
return null;
|
|
120
|
+
return { checkedAt: parsed.checkedAt, problems: parsed.problems };
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** Written atomically through src/lib/safe-store.ts in the daemon's private folder; a link at the path is replaced, never written through. */
|
|
127
|
+
export function writeSelfCheckResult(file, result, options = {}) {
|
|
128
|
+
writeStoreFile(file, `${JSON.stringify(result)}\n`, { create: true, ...options });
|
|
129
|
+
}
|
|
99
130
|
/**
|
|
100
131
|
* Send now: something is wrong and this machine has not sent a self-check DM
|
|
101
132
|
* in the last day. Which units are affected does not matter, so units that
|
|
@@ -127,6 +158,14 @@ vaultName = () => undefined) {
|
|
|
127
158
|
export async function runSelfCheck(deps) {
|
|
128
159
|
const now = deps.now();
|
|
129
160
|
const problems = engineUnitsNeedingAttention(deps.readState(), deps.readHolds(), now, deps.wanted);
|
|
161
|
+
if (deps.resultPath) {
|
|
162
|
+
try {
|
|
163
|
+
writeSelfCheckResult(deps.resultPath, { checkedAt: now, problems: problems.length });
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
deps.log(`sync engine self-check: could not record this run's result (${error.code ?? "error"})`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
130
169
|
if (problems.length === 0) {
|
|
131
170
|
deps.log("sync engine self-check: every engine unit is healthy");
|
|
132
171
|
return "healthy";
|
|
@@ -12,7 +12,7 @@ export { createResolver };
|
|
|
12
12
|
import { SessionRegistry } from "./session-registry.js";
|
|
13
13
|
import { HqMcpBridge, type RunWorkerRequest } from "./ops/mcp.js";
|
|
14
14
|
import { type StdinMachineCredentials } from "./machine-login.js";
|
|
15
|
-
import {
|
|
15
|
+
import type { DaemonFlagSnapshotSource, ReadDaemonFlagOptions } from "./flag.js";
|
|
16
16
|
import { type WindowsPipeAclReport, type WindowsPipeHost } from "./windows-pipe-host.js";
|
|
17
17
|
export interface HqdServerOptions {
|
|
18
18
|
socketPath: string;
|
|
@@ -45,6 +45,8 @@ export interface HqdServerOptions {
|
|
|
45
45
|
runtimeFlagReader?: (options: ReadDaemonFlagOptions) => Promise<boolean>;
|
|
46
46
|
/** Test seam for the human opt-in setting; production reads hq-pro. */
|
|
47
47
|
personSettingReader?: () => Promise<boolean | null>;
|
|
48
|
+
/** The daemon's shared flag read, so this server's runtime gate adds no registry request of its own. */
|
|
49
|
+
flagSnapshots?: DaemonFlagSnapshotSource;
|
|
48
50
|
/** Test seam for the hosted Streamable HTTP MCP transport. */
|
|
49
51
|
mcpBridge?: HqMcpBridge;
|
|
50
52
|
/** Test seam for machine login while keeping production credentials off disk. */
|
|
@@ -55,6 +57,8 @@ export interface HqdServerOptions {
|
|
|
55
57
|
workerDispatch?: (request: RunWorkerRequest, hqRoot: string, env: NodeJS.ProcessEnv, now: Date) => Promise<Record<string, unknown>>;
|
|
56
58
|
/** Crash reporter for internal op failures (US-012); run.ts wires Sentry here. */
|
|
57
59
|
reportError?: (op: string, err: unknown) => void;
|
|
60
|
+
/** Sees every op failure before it is classified; run.ts watches it for macOS EPERM under the HQ root (tcc-eperm.ts). */
|
|
61
|
+
observeError?: (err: unknown) => void;
|
|
58
62
|
}
|
|
59
63
|
export interface HqdServer {
|
|
60
64
|
socketPath: string;
|
|
@@ -26,9 +26,7 @@ import { authenticateMachineCredentials, machineTokenExpiresAt, MachineLoginErro
|
|
|
26
26
|
import { HQ_MCP_TOOL_NAMES, HQD_NATIVE_TOOL_NAMES } from "../mcp/tools.js";
|
|
27
27
|
import { HQ_ANYWHERE_RUNTIME_FLAG } from "../../commands/anywhere-runtime-flag.js";
|
|
28
28
|
import { updateAnywhereRuntimeRevocationMarker } from "../anywhere-runtime-revocation-marker.js";
|
|
29
|
-
import {
|
|
30
|
-
import { createBoundedDaemonFlagMonitor } from "./flag-monitor.js";
|
|
31
|
-
import { readDaemonFlag } from "./flag.js";
|
|
29
|
+
import { createHqdRuntimeFlagMonitor } from "./timed-flag-monitors.js";
|
|
32
30
|
import { startWindowsPipeHost, validateWindowsPipeAcl, WINDOWS_PIPE_STARTUP_REFUSAL } from "./windows-pipe-host.js";
|
|
33
31
|
// The allowlist is deliberately read-only. A newly added hosted MCP tool must
|
|
34
32
|
// pass hq.policy_check until it is explicitly reviewed as read-only here.
|
|
@@ -306,7 +304,15 @@ export async function startHqdServer(opts) {
|
|
|
306
304
|
const startedAtMs = (opts.now ?? (() => new Date()))().getTime();
|
|
307
305
|
const sessions = new SessionRegistry(opts.now);
|
|
308
306
|
const log = opts.log ?? (() => { });
|
|
309
|
-
const
|
|
307
|
+
const observeError = (err) => {
|
|
308
|
+
try {
|
|
309
|
+
opts.observeError?.(err);
|
|
310
|
+
}
|
|
311
|
+
catch (observeErr) {
|
|
312
|
+
log("warn", `hqd error observer failed: ${observeErr.message}`);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
const resolve = createResolver(opts.registryPath ?? registryFile(), { hqRoot: opts.hqRoot, log, onFsError: observeError });
|
|
310
316
|
const clock = opts.now ?? (() => new Date());
|
|
311
317
|
const env = opts.env ?? process.env;
|
|
312
318
|
const policyIndex = opts.hqRoot ? new PolicyIndex(opts.hqRoot, log) : null;
|
|
@@ -387,25 +393,12 @@ export async function startHqdServer(opts) {
|
|
|
387
393
|
}
|
|
388
394
|
}
|
|
389
395
|
}
|
|
390
|
-
const runtimeFlagMonitor = opts.mcpEnabled ? undefined :
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
localOverrideMode: "off-only",
|
|
397
|
-
};
|
|
398
|
-
const flagEnabled = await (opts.runtimeFlagReader
|
|
399
|
-
? opts.runtimeFlagReader(options)
|
|
400
|
-
: readDaemonFlag(HQ_ANYWHERE_RUNTIME_FLAG, options));
|
|
401
|
-
if (!flagEnabled)
|
|
402
|
-
return false;
|
|
403
|
-
const personSetting = await (opts.personSettingReader ?? (() => readHqAnywherePersonSetting({
|
|
404
|
-
env,
|
|
405
|
-
onError: (name) => log("warn", `hqd person setting refresh failed (${name}); keeping HQ Anywhere off`),
|
|
406
|
-
})))();
|
|
407
|
-
return personSetting === true;
|
|
408
|
-
},
|
|
396
|
+
const runtimeFlagMonitor = opts.mcpEnabled ? undefined : createHqdRuntimeFlagMonitor({
|
|
397
|
+
env,
|
|
398
|
+
log,
|
|
399
|
+
...(opts.flagSnapshots ? { snapshots: opts.flagSnapshots } : {}),
|
|
400
|
+
...(opts.runtimeFlagReader ? { runtimeFlagReader: opts.runtimeFlagReader } : {}),
|
|
401
|
+
...(opts.personSettingReader ? { personSettingReader: opts.personSettingReader } : {}),
|
|
409
402
|
onChange: (enabled) => {
|
|
410
403
|
try {
|
|
411
404
|
updateAnywhereRuntimeRevocationMarker(enabled, env.HOME?.trim() || undefined, env);
|
|
@@ -717,6 +710,7 @@ export async function startHqdServer(opts) {
|
|
|
717
710
|
}
|
|
718
711
|
};
|
|
719
712
|
const fail = (err) => {
|
|
713
|
+
observeError(err);
|
|
720
714
|
const error = err instanceof OpError ||
|
|
721
715
|
err instanceof PolicyOpError ||
|
|
722
716
|
err instanceof JournalOpError ||
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The switch for the daemon's state file (state-file.ts, hq-core-rust US-009).
|
|
3
|
+
* Default ON: with nothing configured the daemon writes the state file and
|
|
4
|
+
* the hook engine reads it, exactly as #2624 shipped.
|
|
5
|
+
*
|
|
6
|
+
* Off when any of these says off (the first one found wins):
|
|
7
|
+
* - env `HQ_FLAG_DAEMON_STATE_FILE=false` (also `0`, `off`, `no`), the
|
|
8
|
+
* CLI's local flag override (`HQ_FLAG_<KEY>`); `true` keeps it on and
|
|
9
|
+
* skips the remaining checks except the off file;
|
|
10
|
+
* - the file `~/.hq/daemon-state-file.off` (any file or link at that path);
|
|
11
|
+
* - flag `daemon-state-file` is `false` in the offline flag snapshot
|
|
12
|
+
* (readDefaultOnFlagSnapshot: a default-on kill switch) or in the flag
|
|
13
|
+
* registry answer the daemon just received.
|
|
14
|
+
*
|
|
15
|
+
* Off means: the daemon writes no hq-state.json and no self-check result for
|
|
16
|
+
* it, the daemon folder is not refused for being a link or another user's
|
|
17
|
+
* (ensurePrivateDir falls back to its behavior before #2624), and the engine
|
|
18
|
+
* (crates/hq-hook/src/state.rs, which reads the same env var, file and
|
|
19
|
+
* snapshot flag) does not read the state file.
|
|
20
|
+
*
|
|
21
|
+
* Where the two sides differ (each difference only ever leads to "no fresh
|
|
22
|
+
* state file, use the flag snapshot", so neither can turn the file on):
|
|
23
|
+
* - Scope: with no company uid, the daemon's snapshot read scans every
|
|
24
|
+
* company's snapshot and the person snapshot, so a `false` in any of them
|
|
25
|
+
* stops the writer. The engine reads only the snapshot of its own
|
|
26
|
+
* HQ_COMPANY_UID (or the person snapshot).
|
|
27
|
+
* - Age: the daemon keeps a snapshot `false` however old it is; the engine
|
|
28
|
+
* uses snapshots up to 24 h old.
|
|
29
|
+
* - Registry only: when the switch is turned off only by the registry flag
|
|
30
|
+
* (no env var, no off file, and the hook's snapshot does not hold `false`
|
|
31
|
+
* yet), the engine cannot see it. The daemon removes its own state file
|
|
32
|
+
* when it first sees the switch off (state-file.ts
|
|
33
|
+
* removeDaemonStateFile); if that removal is refused, the engine keeps
|
|
34
|
+
* reading the last file until it expires (600 s, at most 660 s).
|
|
35
|
+
*
|
|
36
|
+
* Read only: nothing here writes a flag.
|
|
37
|
+
*/
|
|
38
|
+
export declare const DAEMON_STATE_FILE_FLAG = "daemon-state-file";
|
|
39
|
+
/** The local override, named as the CLI names every `HQ_FLAG_<KEY>` override. */
|
|
40
|
+
export declare const DAEMON_STATE_FILE_ENV = "HQ_FLAG_DAEMON_STATE_FILE";
|
|
41
|
+
/** Under the home folder; any file or link here turns the state file off on this machine. */
|
|
42
|
+
export declare const DAEMON_STATE_FILE_OFF_FILE: string;
|
|
43
|
+
export type DaemonStateFileOffReason = "env" | "file" | "flag";
|
|
44
|
+
export interface DaemonStateFileSwitchOptions {
|
|
45
|
+
env?: NodeJS.ProcessEnv;
|
|
46
|
+
/** Home folder for the off file. Default: HOME (USERPROFILE on Windows), else os.homedir(). */
|
|
47
|
+
home?: string;
|
|
48
|
+
/** The flag value from a registry answer the caller already has (the daemon's poll). */
|
|
49
|
+
registryValue?: unknown;
|
|
50
|
+
/** Replaces the offline snapshot read (tests). */
|
|
51
|
+
readSnapshot?: (env: NodeJS.ProcessEnv) => boolean;
|
|
52
|
+
}
|
|
53
|
+
/** Why the state file is off, or null while it is on (the default). */
|
|
54
|
+
export declare function daemonStateFileOffReason(options?: DaemonStateFileSwitchOptions): DaemonStateFileOffReason | null;
|
|
55
|
+
/** True unless something turns the state file off (see the module comment). */
|
|
56
|
+
export declare function daemonStateFileEnabled(options?: DaemonStateFileSwitchOptions): boolean;
|
|
57
|
+
//# sourceMappingURL=state-file-switch.d.ts.map
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The switch for the daemon's state file (state-file.ts, hq-core-rust US-009).
|
|
3
|
+
* Default ON: with nothing configured the daemon writes the state file and
|
|
4
|
+
* the hook engine reads it, exactly as #2624 shipped.
|
|
5
|
+
*
|
|
6
|
+
* Off when any of these says off (the first one found wins):
|
|
7
|
+
* - env `HQ_FLAG_DAEMON_STATE_FILE=false` (also `0`, `off`, `no`), the
|
|
8
|
+
* CLI's local flag override (`HQ_FLAG_<KEY>`); `true` keeps it on and
|
|
9
|
+
* skips the remaining checks except the off file;
|
|
10
|
+
* - the file `~/.hq/daemon-state-file.off` (any file or link at that path);
|
|
11
|
+
* - flag `daemon-state-file` is `false` in the offline flag snapshot
|
|
12
|
+
* (readDefaultOnFlagSnapshot: a default-on kill switch) or in the flag
|
|
13
|
+
* registry answer the daemon just received.
|
|
14
|
+
*
|
|
15
|
+
* Off means: the daemon writes no hq-state.json and no self-check result for
|
|
16
|
+
* it, the daemon folder is not refused for being a link or another user's
|
|
17
|
+
* (ensurePrivateDir falls back to its behavior before #2624), and the engine
|
|
18
|
+
* (crates/hq-hook/src/state.rs, which reads the same env var, file and
|
|
19
|
+
* snapshot flag) does not read the state file.
|
|
20
|
+
*
|
|
21
|
+
* Where the two sides differ (each difference only ever leads to "no fresh
|
|
22
|
+
* state file, use the flag snapshot", so neither can turn the file on):
|
|
23
|
+
* - Scope: with no company uid, the daemon's snapshot read scans every
|
|
24
|
+
* company's snapshot and the person snapshot, so a `false` in any of them
|
|
25
|
+
* stops the writer. The engine reads only the snapshot of its own
|
|
26
|
+
* HQ_COMPANY_UID (or the person snapshot).
|
|
27
|
+
* - Age: the daemon keeps a snapshot `false` however old it is; the engine
|
|
28
|
+
* uses snapshots up to 24 h old.
|
|
29
|
+
* - Registry only: when the switch is turned off only by the registry flag
|
|
30
|
+
* (no env var, no off file, and the hook's snapshot does not hold `false`
|
|
31
|
+
* yet), the engine cannot see it. The daemon removes its own state file
|
|
32
|
+
* when it first sees the switch off (state-file.ts
|
|
33
|
+
* removeDaemonStateFile); if that removal is refused, the engine keeps
|
|
34
|
+
* reading the last file until it expires (600 s, at most 660 s).
|
|
35
|
+
*
|
|
36
|
+
* Read only: nothing here writes a flag.
|
|
37
|
+
*/
|
|
38
|
+
import * as fs from "node:fs";
|
|
39
|
+
import * as os from "node:os";
|
|
40
|
+
import * as path from "node:path";
|
|
41
|
+
import { readDefaultOnFlagSnapshot } from "../flag-snapshot-file.js";
|
|
42
|
+
import { resolveProcessFlagCompanyContext } from "../process-flag-company-context.js";
|
|
43
|
+
export const DAEMON_STATE_FILE_FLAG = "daemon-state-file";
|
|
44
|
+
/** The local override, named as the CLI names every `HQ_FLAG_<KEY>` override. */
|
|
45
|
+
export const DAEMON_STATE_FILE_ENV = "HQ_FLAG_DAEMON_STATE_FILE";
|
|
46
|
+
/** Under the home folder; any file or link here turns the state file off on this machine. */
|
|
47
|
+
export const DAEMON_STATE_FILE_OFF_FILE = path.join(".hq", "daemon-state-file.off");
|
|
48
|
+
const OFF_WORDS = new Set(["false", "0", "off", "no"]);
|
|
49
|
+
const ON_WORDS = new Set(["true", "1", "on", "yes"]);
|
|
50
|
+
function homeOf(env) {
|
|
51
|
+
const fromEnv = (process.platform === "win32" ? env.USERPROFILE : env.HOME)?.trim();
|
|
52
|
+
return fromEnv || os.homedir();
|
|
53
|
+
}
|
|
54
|
+
function offFilePresent(home) {
|
|
55
|
+
try {
|
|
56
|
+
fs.lstatSync(path.join(home, DAEMON_STATE_FILE_OFF_FILE));
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** False only when a snapshot holds `daemon-state-file: false`; a missing or unreadable snapshot is on. */
|
|
64
|
+
function snapshotAllows(env) {
|
|
65
|
+
const { companyUid } = resolveProcessFlagCompanyContext(env);
|
|
66
|
+
return readDefaultOnFlagSnapshot(DAEMON_STATE_FILE_FLAG, companyUid, env);
|
|
67
|
+
}
|
|
68
|
+
/** Why the state file is off, or null while it is on (the default). */
|
|
69
|
+
export function daemonStateFileOffReason(options = {}) {
|
|
70
|
+
const env = options.env ?? process.env;
|
|
71
|
+
const override = env[DAEMON_STATE_FILE_ENV]?.trim().toLowerCase();
|
|
72
|
+
if (override && OFF_WORDS.has(override))
|
|
73
|
+
return "env";
|
|
74
|
+
if (offFilePresent(options.home ?? homeOf(env)))
|
|
75
|
+
return "file";
|
|
76
|
+
if (override && ON_WORDS.has(override))
|
|
77
|
+
return null;
|
|
78
|
+
if (options.registryValue === false)
|
|
79
|
+
return "flag";
|
|
80
|
+
if (!(options.readSnapshot ?? snapshotAllows)(env))
|
|
81
|
+
return "flag";
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
/** True unless something turns the state file off (see the module comment). */
|
|
85
|
+
export function daemonStateFileEnabled(options = {}) {
|
|
86
|
+
return daemonStateFileOffReason(options) === null;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=state-file-switch.js.map
|