@indigoai-us/hq-cli 5.345.247 → 5.345.250
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 +95 -0
- package/assets/lanes-hooks/hq-lanes-drain.sh +1 -1
- package/contracts/host-canary-release.json +14 -0
- package/dist/command-catalog.generated.d.ts +1198 -0
- package/dist/command-catalog.generated.js +1476 -0
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/core-job-run-native.js +4 -0
- package/dist/commands/core-knowledge-prefs.d.ts +1 -1
- package/dist/commands/core-knowledge-prefs.js +1 -16
- package/dist/commands/core.js +0 -2
- package/dist/commands/daemon.js +21 -0
- package/dist/commands/lanes-hold.js +7 -7
- package/dist/commands/lanes-resume.js +2 -2
- package/dist/commands/lanes-review.js +1 -1
- package/dist/commands/lanes-session-start.d.ts +1 -1
- package/dist/commands/lanes-session-start.js +3 -3
- package/dist/commands/lanes-shared.d.ts +1 -1
- package/dist/commands/lanes.d.ts +2 -2
- package/dist/commands/lanes.js +50 -50
- package/dist/commands/mesh.d.ts +25 -0
- package/dist/commands/mesh.js +48 -8
- package/dist/commands/ontology-graph.d.ts +63 -0
- package/dist/commands/ontology-graph.js +470 -0
- package/dist/commands/sync-core.js +6 -0
- package/dist/fast-scaffold.js +1 -3
- package/dist/hq-cloud-sync-runner/bin/sync-runner.js +281 -256
- package/dist/hq-cloud-sync-runner/package.json +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +48 -1
- package/dist/lanes-monitor-check-argv.d.ts +1 -1
- package/dist/lanes-monitor-check-argv.js +1 -1
- package/dist/lanes-monitor-check-fast.d.ts +1 -1
- package/dist/lanes-monitor-check-fast.js +1 -1
- package/dist/lanes-session-start-argv.d.ts +1 -1
- package/dist/lanes-session-start-argv.js +1 -1
- package/dist/lib/capacity/sign-in.d.ts +1 -1
- package/dist/lib/capacity/sign-in.js +1 -1
- package/dist/lib/daemon/hook-requests-conduct-link.js +2 -2
- package/dist/lib/daemon/installed-cli-restart.d.ts +57 -0
- package/dist/lib/daemon/installed-cli-restart.js +114 -0
- package/dist/lib/daemon/lanes-monitor-state.d.ts +2 -2
- package/dist/lib/daemon/lanes-monitor-state.js +2 -2
- package/dist/lib/daemon/legacy-journal-gate.d.ts +32 -0
- package/dist/lib/daemon/legacy-journal-gate.js +102 -0
- package/dist/lib/daemon/machine-checks.d.ts +7 -0
- package/dist/lib/daemon/machine-checks.js +10 -0
- package/dist/lib/daemon/merge-sections.d.ts +3 -3
- package/dist/lib/daemon/merge-sections.js +7 -7
- package/dist/lib/daemon/remote-control-pools.js +1 -1
- package/dist/lib/daemon/run.d.ts +3 -1
- package/dist/lib/daemon/run.js +49 -6
- package/dist/lib/daemon/state-file.d.ts +1 -1
- package/dist/lib/daemon/state-file.js +1 -1
- package/dist/lib/flag-registry-worker-source.generated.d.ts +1 -1
- package/dist/lib/flag-registry-worker-source.generated.js +1 -1
- package/dist/lib/flag-snapshot-file.d.ts +2 -0
- package/dist/lib/flag-snapshot-file.js +118 -48
- package/dist/lib/hook-engine/canary-gate.d.ts +57 -3
- package/dist/lib/hook-engine/canary-gate.js +138 -6
- package/dist/lib/hook-engine/coverage.d.ts +82 -13
- package/dist/lib/hook-engine/coverage.js +214 -18
- package/dist/lib/hook-engine/registration-identity.d.ts +5 -0
- package/dist/lib/hook-engine/registration-identity.js +1 -1
- package/dist/lib/hook-engine/resolve.d.ts +23 -0
- package/dist/lib/hook-engine/resolve.js +48 -1
- package/dist/lib/hook-engine/stable-heal.d.ts +87 -0
- package/dist/lib/hook-engine/stable-heal.js +214 -0
- package/dist/lib/hot-channels/graph-context.d.ts +105 -0
- package/dist/lib/hot-channels/graph-context.js +380 -0
- package/dist/lib/hot-channels/signals-pass.js +27 -1
- package/dist/lib/lanes/claude-credential.d.ts +1 -1
- package/dist/lib/lanes/claude-credential.js +1 -1
- package/dist/lib/lanes/delivery-core.d.ts +1 -1
- package/dist/lib/lanes/delivery-core.js +1 -1
- package/dist/lib/lanes/delivery.d.ts +10 -10
- package/dist/lib/lanes/delivery.js +8 -8
- package/dist/lib/lanes/dropbox.d.ts +1 -1
- package/dist/lib/lanes/dropbox.js +1 -1
- package/dist/lib/lanes/hooks-install.js +2 -2
- package/dist/lib/lanes/index.d.ts +1 -1
- package/dist/lib/lanes/index.js +1 -1
- package/dist/lib/lanes/monitor.js +1 -1
- package/dist/lib/lanes/quota-notice.js +1 -1
- package/dist/lib/lanes/quota.d.ts +1 -1
- package/dist/lib/lanes/quota.js +1 -1
- package/dist/lib/lanes/repo-merge-hold.d.ts +2 -2
- package/dist/lib/lanes/repo-merge-hold.js +2 -2
- package/dist/lib/lanes/spawn.d.ts +4 -4
- package/dist/lib/lanes/spawn.js +5 -5
- package/dist/lib/mesh/live/company-flag-refresh.d.ts +61 -0
- package/dist/lib/mesh/live/company-flag-refresh.js +159 -0
- package/dist/lib/mesh/live/company-reporting-gaps.d.ts +61 -0
- package/dist/lib/mesh/live/company-reporting-gaps.js +238 -0
- package/dist/lib/mesh/live/daemon/duplicate-unit.d.ts +156 -0
- package/dist/lib/mesh/live/daemon/duplicate-unit.js +616 -0
- package/dist/lib/mesh/live/daemon/run.d.ts +13 -0
- package/dist/lib/mesh/live/daemon/run.js +35 -2
- package/dist/lib/mesh/live/direct-drain.js +1 -1
- package/dist/lib/mesh/live/emit.d.ts +3 -0
- package/dist/lib/mesh/live/emit.js +32 -9
- package/dist/lib/mesh/live/flush.d.ts +3 -0
- package/dist/lib/mesh/live/flush.js +115 -32
- package/dist/lib/mesh/live/liveness-carry.d.ts +1 -1
- package/dist/lib/mesh/live/liveness-carry.js +1 -1
- package/dist/lib/mesh/live/reporting-privacy.d.ts +119 -3
- package/dist/lib/mesh/live/reporting-privacy.js +235 -11
- package/dist/lib/mesh/live/spool.d.ts +17 -2
- package/dist/lib/mesh/live/spool.js +22 -6
- package/dist/lib/ontology-graph/legacy/coverage.d.ts +67 -0
- package/dist/lib/ontology-graph/legacy/coverage.js +192 -0
- package/dist/lib/ontology-graph/legacy/index.d.ts +199 -0
- package/dist/lib/ontology-graph/legacy/index.js +1150 -0
- package/dist/lib/ontology-graph/legacy/parse.d.ts +49 -0
- package/dist/lib/ontology-graph/legacy/parse.js +126 -0
- package/dist/lib/ontology-graph/legacy/report.d.ts +14 -0
- package/dist/lib/ontology-graph/legacy/report.js +254 -0
- package/dist/lib/ontology-graph/legacy/verb-map.d.ts +35 -0
- package/dist/lib/ontology-graph/legacy/verb-map.js +84 -0
- package/dist/lib/ontology-graph/lint/checks.d.ts +140 -0
- package/dist/lib/ontology-graph/lint/checks.js +717 -0
- package/dist/lib/ontology-graph/lint/coverage.d.ts +106 -0
- package/dist/lib/ontology-graph/lint/coverage.js +241 -0
- package/dist/lib/ontology-graph/lint/daily-job.d.ts +259 -0
- package/dist/lib/ontology-graph/lint/daily-job.js +595 -0
- package/dist/lib/ontology-graph/lint/index.d.ts +65 -0
- package/dist/lib/ontology-graph/lint/index.js +176 -0
- package/dist/lib/ontology-graph/model/files.d.ts +20 -0
- package/dist/lib/ontology-graph/model/files.js +89 -0
- package/dist/lib/ontology-graph/model/index.d.ts +65 -0
- package/dist/lib/ontology-graph/model/index.js +100 -0
- package/dist/lib/ontology-graph/model/pass-tasks.d.ts +156 -0
- package/dist/lib/ontology-graph/model/pass-tasks.js +345 -0
- package/dist/lib/ontology-graph/model/pass.d.ts +109 -0
- package/dist/lib/ontology-graph/model/pass.js +958 -0
- package/dist/lib/ontology-graph/model/runner.d.ts +50 -0
- package/dist/lib/ontology-graph/model/runner.js +160 -0
- package/dist/lib/ontology-graph/model/step.d.ts +197 -0
- package/dist/lib/ontology-graph/model/step.js +526 -0
- package/dist/lib/ontology-graph/model/tasks.d.ts +183 -0
- package/dist/lib/ontology-graph/model/tasks.js +349 -0
- package/dist/lib/ontology-graph/not-implemented.d.ts +5 -0
- package/dist/lib/ontology-graph/not-implemented.js +8 -0
- package/dist/lib/ontology-graph/org/compare.d.ts +28 -0
- package/dist/lib/ontology-graph/org/compare.js +239 -0
- package/dist/lib/ontology-graph/org/derive.d.ts +67 -0
- package/dist/lib/ontology-graph/org/derive.js +359 -0
- package/dist/lib/ontology-graph/org/index.d.ts +102 -0
- package/dist/lib/ontology-graph/org/index.js +630 -0
- package/dist/lib/ontology-graph/org/load.d.ts +31 -0
- package/dist/lib/ontology-graph/org/load.js +476 -0
- package/dist/lib/ontology-graph/org/role.d.ts +38 -0
- package/dist/lib/ontology-graph/org/role.js +268 -0
- package/dist/lib/ontology-graph/org/structure.d.ts +63 -0
- package/dist/lib/ontology-graph/org/structure.js +437 -0
- package/dist/lib/ontology-graph/org/text.d.ts +97 -0
- package/dist/lib/ontology-graph/org/text.js +321 -0
- package/dist/lib/ontology-graph/org/types.d.ts +457 -0
- package/dist/lib/ontology-graph/org/types.js +80 -0
- package/dist/lib/ontology-graph/pages/index.d.ts +64 -0
- package/dist/lib/ontology-graph/pages/index.js +193 -0
- package/dist/lib/ontology-graph/pages/render.d.ts +144 -0
- package/dist/lib/ontology-graph/pages/render.js +688 -0
- package/dist/lib/ontology-graph/proposals/apply.d.ts +109 -0
- package/dist/lib/ontology-graph/proposals/apply.js +468 -0
- package/dist/lib/ontology-graph/proposals/group.d.ts +63 -0
- package/dist/lib/ontology-graph/proposals/group.js +232 -0
- package/dist/lib/ontology-graph/proposals/index.d.ts +173 -0
- package/dist/lib/ontology-graph/proposals/index.js +474 -0
- package/dist/lib/ontology-graph/proposals/model.d.ts +162 -0
- package/dist/lib/ontology-graph/proposals/model.js +196 -0
- package/dist/lib/ontology-graph/proposals/org-chart.d.ts +82 -0
- package/dist/lib/ontology-graph/proposals/org-chart.js +340 -0
- package/dist/lib/ontology-graph/read/brief.d.ts +26 -0
- package/dist/lib/ontology-graph/read/brief.js +254 -0
- package/dist/lib/ontology-graph/read/graph-index.d.ts +76 -0
- package/dist/lib/ontology-graph/read/graph-index.js +198 -0
- package/dist/lib/ontology-graph/read/index.d.ts +59 -0
- package/dist/lib/ontology-graph/read/index.js +803 -0
- package/dist/lib/ontology-graph/read/member.d.ts +7 -0
- package/dist/lib/ontology-graph/read/member.js +34 -0
- package/dist/lib/ontology-graph/read/names.d.ts +49 -0
- package/dist/lib/ontology-graph/read/names.js +297 -0
- package/dist/lib/ontology-graph/read/reader-setting.d.ts +19 -0
- package/dist/lib/ontology-graph/read/reader-setting.js +62 -0
- package/dist/lib/ontology-graph/read/test-graph.d.ts +37 -0
- package/dist/lib/ontology-graph/read/test-graph.js +150 -0
- package/dist/lib/ontology-graph/replay/buffered-store.d.ts +33 -0
- package/dist/lib/ontology-graph/replay/buffered-store.js +50 -0
- package/dist/lib/ontology-graph/replay/config.d.ts +41 -0
- package/dist/lib/ontology-graph/replay/config.js +105 -0
- package/dist/lib/ontology-graph/replay/engine.d.ts +146 -0
- package/dist/lib/ontology-graph/replay/engine.js +1240 -0
- package/dist/lib/ontology-graph/replay/extractors.d.ts +59 -0
- package/dist/lib/ontology-graph/replay/extractors.js +1555 -0
- package/dist/lib/ontology-graph/replay/index.d.ts +91 -0
- package/dist/lib/ontology-graph/replay/index.js +220 -0
- package/dist/lib/ontology-graph/replay/listing-manifest.d.ts +66 -0
- package/dist/lib/ontology-graph/replay/listing-manifest.js +242 -0
- package/dist/lib/ontology-graph/replay/member.d.ts +19 -0
- package/dist/lib/ontology-graph/replay/member.js +65 -0
- package/dist/lib/ontology-graph/replay/mentions.d.ts +58 -0
- package/dist/lib/ontology-graph/replay/mentions.js +239 -0
- package/dist/lib/ontology-graph/replay/readers.d.ts +98 -0
- package/dist/lib/ontology-graph/replay/readers.js +1251 -0
- package/dist/lib/ontology-graph/replay/rules.d.ts +127 -0
- package/dist/lib/ontology-graph/replay/rules.js +374 -0
- package/dist/lib/ontology-graph/replay/scope.d.ts +35 -0
- package/dist/lib/ontology-graph/replay/scope.js +115 -0
- package/dist/lib/ontology-graph/replay/transcripts.d.ts +39 -0
- package/dist/lib/ontology-graph/replay/transcripts.js +224 -0
- package/dist/lib/ontology-graph/replay/util.d.ts +55 -0
- package/dist/lib/ontology-graph/replay/util.js +256 -0
- package/dist/lib/ontology-graph/resolve/entity-index.d.ts +159 -0
- package/dist/lib/ontology-graph/resolve/entity-index.js +652 -0
- package/dist/lib/ontology-graph/resolve/index.d.ts +113 -0
- package/dist/lib/ontology-graph/resolve/index.js +214 -0
- package/dist/lib/ontology-graph/resolve/machine.d.ts +34 -0
- package/dist/lib/ontology-graph/resolve/machine.js +95 -0
- package/dist/lib/ontology-graph/resolve/normalize.d.ts +35 -0
- package/dist/lib/ontology-graph/resolve/normalize.js +124 -0
- package/dist/lib/ontology-graph/resolve/org-chart.d.ts +107 -0
- package/dist/lib/ontology-graph/resolve/org-chart.js +578 -0
- package/dist/lib/ontology-graph/resolve/org-layer.d.ts +78 -0
- package/dist/lib/ontology-graph/resolve/org-layer.js +159 -0
- package/dist/lib/ontology-graph/resolve/org-seed.d.ts +141 -0
- package/dist/lib/ontology-graph/resolve/org-seed.js +631 -0
- package/dist/lib/ontology-graph/resolve/org-sources.d.ts +165 -0
- package/dist/lib/ontology-graph/resolve/org-sources.js +634 -0
- package/dist/lib/ontology-graph/resolve/roster.d.ts +76 -0
- package/dist/lib/ontology-graph/resolve/roster.js +226 -0
- package/dist/lib/ontology-graph/resolve/seed.d.ts +267 -0
- package/dist/lib/ontology-graph/resolve/seed.js +1198 -0
- package/dist/lib/ontology-graph/resolve/upsert.d.ts +58 -0
- package/dist/lib/ontology-graph/resolve/upsert.js +139 -0
- package/dist/lib/ontology-graph/resolve/writer.d.ts +128 -0
- package/dist/lib/ontology-graph/resolve/writer.js +351 -0
- package/dist/lib/ontology-graph/schema/authority.d.ts +107 -0
- package/dist/lib/ontology-graph/schema/authority.js +186 -0
- package/dist/lib/ontology-graph/schema/default-schema.d.ts +10 -0
- package/dist/lib/ontology-graph/schema/default-schema.js +242 -0
- package/dist/lib/ontology-graph/schema/grammar.d.ts +57 -0
- package/dist/lib/ontology-graph/schema/grammar.js +409 -0
- package/dist/lib/ontology-graph/schema/index.d.ts +12 -0
- package/dist/lib/ontology-graph/schema/index.js +10 -0
- package/dist/lib/ontology-graph/schema/validate.d.ts +13 -0
- package/dist/lib/ontology-graph/schema/validate.js +132 -0
- package/dist/lib/ontology-graph/store/files.d.ts +34 -0
- package/dist/lib/ontology-graph/store/files.js +207 -0
- package/dist/lib/ontology-graph/store/fs.d.ts +30 -0
- package/dist/lib/ontology-graph/store/fs.js +101 -0
- package/dist/lib/ontology-graph/store/graph-store.d.ts +14 -0
- package/dist/lib/ontology-graph/store/graph-store.js +520 -0
- package/dist/lib/ontology-graph/store/index.d.ts +18 -0
- package/dist/lib/ontology-graph/store/index.js +15 -0
- package/dist/lib/ontology-graph/store/ingest.d.ts +37 -0
- package/dist/lib/ontology-graph/store/ingest.js +323 -0
- package/dist/lib/ontology-graph/store/paths.d.ts +114 -0
- package/dist/lib/ontology-graph/store/paths.js +232 -0
- package/dist/lib/ontology-graph/types.d.ts +1190 -0
- package/dist/lib/ontology-graph/types.js +131 -0
- package/dist/lib/secret-scan/gitleaks-rules.generated.js +1 -1
- package/dist/lib/sync-core/engine-holds.d.ts +1 -1
- package/dist/lib/sync-core/engine-holds.js +12 -2
- package/dist/lib/sync-core/hand-back.d.ts +2 -1
- package/dist/lib/sync-core/hand-back.js +16 -2
- package/dist/lib/sync-core/legacy-journal-export-child.d.ts +2 -0
- package/dist/lib/sync-core/legacy-journal-export-child.js +24 -0
- package/dist/lib/sync-core/legacy-journal-export.d.ts +39 -0
- package/dist/lib/sync-core/legacy-journal-export.js +241 -0
- package/dist/lib/sync-core/legacy-journal-reader.d.ts +15 -0
- package/dist/lib/sync-core/legacy-journal-reader.js +56 -0
- package/dist/lib/sync-core/reconcile.d.ts +3 -0
- package/dist/lib/sync-core/reconcile.js +43 -3
- package/dist/utils/cli-telemetry.d.ts +1 -0
- package/dist/utils/cli-telemetry.js +6 -3
- package/dist/utils/frontmatter.d.ts +14 -0
- package/dist/utils/frontmatter.js +18 -0
- package/dist/utils/hook-engine-rollup.d.ts +96 -0
- package/dist/utils/hook-engine-rollup.js +725 -0
- package/dist/utils/interactive-caller.js +1 -1
- package/package.json +9 -9
- package/scripts/install-hook-engine.mjs +4 -0
- package/assets/scaffold/core/scripts/knowledge-prefs.sh +0 -55
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stale stretches of a company's enforcement episode (review of #3469, INFO-2
|
|
3
|
+
* and INFO-A). A company "reporting required" snapshot more than a day old
|
|
4
|
+
* counts as off. When it last said on, the episode carries on and only the
|
|
5
|
+
* stretch from when the snapshot went stale until a fresh one is read is
|
|
6
|
+
* held: events dated inside a stretch are not sent while the member's HQ
|
|
7
|
+
* Anywhere opt-out holds them.
|
|
8
|
+
*
|
|
9
|
+
* A stretch is recorded in two places: by a daemon flush that reads the stale
|
|
10
|
+
* snapshot (it notes the start, and closes the stretch when it reads a fresh
|
|
11
|
+
* on), and by the snapshot writer when it replaces a stale snapshot that said
|
|
12
|
+
* on, for a stretch no flush saw (the daemon was down or asleep).
|
|
13
|
+
*
|
|
14
|
+
* This module has no dependency on the flag snapshot reader or the reporting
|
|
15
|
+
* gate, so the snapshot writer can use it.
|
|
16
|
+
*/
|
|
17
|
+
import * as fs from "node:fs";
|
|
18
|
+
import * as os from "node:os";
|
|
19
|
+
import * as path from "node:path";
|
|
20
|
+
import { workMeshRoot } from "./paths.js";
|
|
21
|
+
import { createLockFile, lockIsStale, parsePidLockRecord, readLockFile, releaseLockFile, } from "../../../utils/exclusive-lock.js";
|
|
22
|
+
/** The company flag that makes Work Mesh reporting required. */
|
|
23
|
+
export const WORK_MESH_COMPANY_REPORTING_REQUIRED = "work-mesh-company-reporting-required";
|
|
24
|
+
/**
|
|
25
|
+
* One file per company whose snapshot this machine saw go stale while it
|
|
26
|
+
* said on: when it went stale. The stretch from then is held; the episode
|
|
27
|
+
* itself carries on (review of #3469, INFO-2).
|
|
28
|
+
*/
|
|
29
|
+
export const COMPANY_REPORTING_STALE_DIRNAME = "company-reporting-stale-since";
|
|
30
|
+
/**
|
|
31
|
+
* One file per company: the stale stretches seen during the current episode,
|
|
32
|
+
* one `<from> <to>` line each (ISO times; `-` for a start that could not be
|
|
33
|
+
* read).
|
|
34
|
+
*/
|
|
35
|
+
export const COMPANY_REPORTING_GAPS_DIRNAME = "company-reporting-stale-gaps";
|
|
36
|
+
/** The most stale stretches kept per company. Past it the two oldest merge into one, which holds more, not less. */
|
|
37
|
+
export const COMPANY_REPORTING_GAPS_MAX = 32;
|
|
38
|
+
const SAFE_COMPANY_UID = /^[A-Za-z0-9_-]{1,128}$/;
|
|
39
|
+
export function readTimeFile(file) {
|
|
40
|
+
try {
|
|
41
|
+
const at = Date.parse(fs.readFileSync(file, "utf8").trim());
|
|
42
|
+
return Number.isFinite(at) ? at : null;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function fileTimeOr(file, now) {
|
|
49
|
+
try {
|
|
50
|
+
return fs.statSync(file).mtimeMs;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return now().getTime();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The stretches recorded in `file`. A file that exists but cannot be read,
|
|
58
|
+
* or a line that cannot be read, holds everything up to when the file was
|
|
59
|
+
* last written, as the episode did before stretches were kept.
|
|
60
|
+
*/
|
|
61
|
+
export function readGaps(file, now) {
|
|
62
|
+
let raw;
|
|
63
|
+
try {
|
|
64
|
+
raw = fs.readFileSync(file, "utf8");
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error?.code === "ENOENT")
|
|
68
|
+
return [];
|
|
69
|
+
return [[Number.NEGATIVE_INFINITY, fileTimeOr(file, now)]];
|
|
70
|
+
}
|
|
71
|
+
const gaps = [];
|
|
72
|
+
for (const line of raw.split("\n")) {
|
|
73
|
+
if (!line.trim())
|
|
74
|
+
continue;
|
|
75
|
+
const [fromText, toText, extra] = line.trim().split(" ");
|
|
76
|
+
const from = fromText === "-" ? Number.NEGATIVE_INFINITY : Date.parse(fromText ?? "");
|
|
77
|
+
const to = Date.parse(toText ?? "");
|
|
78
|
+
if (extra !== undefined || Number.isNaN(from) || !Number.isFinite(to)) {
|
|
79
|
+
gaps.push([Number.NEGATIVE_INFINITY, fileTimeOr(file, now)]);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
gaps.push([from, to]);
|
|
83
|
+
}
|
|
84
|
+
return gaps;
|
|
85
|
+
}
|
|
86
|
+
function gapLine([from, to]) {
|
|
87
|
+
return `${Number.isFinite(from) ? new Date(from).toISOString() : "-"} ${new Date(to).toISOString()}\n`;
|
|
88
|
+
}
|
|
89
|
+
/** Rewrites `file` with `gaps`, merging the oldest past {@link COMPANY_REPORTING_GAPS_MAX}. */
|
|
90
|
+
function writeGaps(folder, file, gaps) {
|
|
91
|
+
const kept = [...gaps].sort((a, b) => a[0] - b[0]);
|
|
92
|
+
while (kept.length > COMPANY_REPORTING_GAPS_MAX) {
|
|
93
|
+
const [first, second] = kept.splice(0, 2);
|
|
94
|
+
kept.unshift([Math.min(first[0], second[0]), Math.max(first[1], second[1])]);
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
fs.mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
98
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
99
|
+
fs.writeFileSync(tmp, kept.map(gapLine).join(""), { mode: 0o600 });
|
|
100
|
+
fs.renameSync(tmp, file);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** A lock older than this was left by a process that stopped; it is taken over. */
|
|
108
|
+
export const COMPANY_REPORTING_GAPS_LOCK_STALE_MS = 10_000;
|
|
109
|
+
/** How long {@link addCompanyReportingGap} waits for another process's lock by default. */
|
|
110
|
+
export const COMPANY_REPORTING_GAPS_LOCK_WAIT_MS = 2_000;
|
|
111
|
+
function sleepSync(ms) {
|
|
112
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Runs `fn` holding the company's gaps lock (`<uid>.lock`, created
|
|
116
|
+
* exclusively through the shared lock helpers, body `pid=<n>\nts=<n>`). A
|
|
117
|
+
* lock older than {@link COMPANY_REPORTING_GAPS_LOCK_STALE_MS} is reclaimed.
|
|
118
|
+
* Null when the lock could not be taken within `waitMs`.
|
|
119
|
+
*/
|
|
120
|
+
function withGapsLock(folder, companyUid, waitMs, fn) {
|
|
121
|
+
const lock = path.join(folder, `${companyUid}.lock`);
|
|
122
|
+
const deadline = Date.now() + waitMs;
|
|
123
|
+
const body = `pid=${process.pid}\nts=${Date.now()}`;
|
|
124
|
+
const mine = parsePidLockRecord(body);
|
|
125
|
+
for (;;) {
|
|
126
|
+
try {
|
|
127
|
+
if (createLockFile(lock, body))
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
let ageMs;
|
|
134
|
+
try {
|
|
135
|
+
ageMs = Date.now() - fs.statSync(lock).mtimeMs;
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
continue; // released meanwhile
|
|
139
|
+
}
|
|
140
|
+
if (lockIsStale({ kind: "age", maxAgeMs: COMPANY_REPORTING_GAPS_LOCK_STALE_MS }, { ageMs })) {
|
|
141
|
+
try {
|
|
142
|
+
fs.rmSync(lock, { force: true });
|
|
143
|
+
}
|
|
144
|
+
catch { /* taken over by another process */ }
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (Date.now() >= deadline)
|
|
148
|
+
return null;
|
|
149
|
+
sleepSync(10);
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
return fn();
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
try {
|
|
156
|
+
releaseLockFile(lock, {
|
|
157
|
+
read: (file) => readLockFile(file, parsePidLockRecord),
|
|
158
|
+
owns: (owner) => owner.pid === mine.pid && owner.ts === mine.ts,
|
|
159
|
+
remove: (file) => fs.rmSync(file, { force: true }),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
catch { /* the next taker finds it stale */ }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Adds a stale stretch for the company. The append, and the rewrite with the
|
|
167
|
+
* oldest merged once there are more than {@link COMPANY_REPORTING_GAPS_MAX}
|
|
168
|
+
* lines, happen under the company's gaps lock, so a daemon flush and an hq
|
|
169
|
+
* command adding one at the same time both keep theirs: no line can be
|
|
170
|
+
* appended between the rewrite's read and its rename (review of #3469,
|
|
171
|
+
* INFO-1 of round 3). False when it could not be stored, including when
|
|
172
|
+
* another process held the lock for `lockWaitMs`.
|
|
173
|
+
*/
|
|
174
|
+
export function addCompanyReportingGap(workMeshRootDir, companyUid, gap, now = () => new Date(), lockWaitMs = COMPANY_REPORTING_GAPS_LOCK_WAIT_MS) {
|
|
175
|
+
if (!SAFE_COMPANY_UID.test(companyUid))
|
|
176
|
+
return false;
|
|
177
|
+
const folder = path.join(workMeshRootDir, COMPANY_REPORTING_GAPS_DIRNAME);
|
|
178
|
+
const file = path.join(folder, companyUid);
|
|
179
|
+
const stored = [gap[0], Math.max(gap[0], gap[1])];
|
|
180
|
+
try {
|
|
181
|
+
fs.mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return withGapsLock(folder, companyUid, lockWaitMs, () => {
|
|
187
|
+
try {
|
|
188
|
+
fs.appendFileSync(file, gapLine(stored), { mode: 0o600 });
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
const gaps = readGaps(file, now);
|
|
194
|
+
return gaps.length <= COMPANY_REPORTING_GAPS_MAX || writeGaps(folder, file, gaps);
|
|
195
|
+
}) ?? false;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The stale stretches of the company's current episode on this machine. A
|
|
199
|
+
* stretch still open (a flush saw the snapshot stale and no fresh one has been
|
|
200
|
+
* read since, or closing it could not be stored) runs to now.
|
|
201
|
+
*/
|
|
202
|
+
export function companyReportingGaps(workMeshRootDir, companyUid, now = () => new Date()) {
|
|
203
|
+
if (!SAFE_COMPANY_UID.test(companyUid))
|
|
204
|
+
return [];
|
|
205
|
+
const gaps = readGaps(path.join(workMeshRootDir, COMPANY_REPORTING_GAPS_DIRNAME, companyUid), now);
|
|
206
|
+
const staleFile = path.join(workMeshRootDir, COMPANY_REPORTING_STALE_DIRNAME, companyUid);
|
|
207
|
+
if (fs.existsSync(staleFile))
|
|
208
|
+
gaps.push([readTimeFile(staleFile) ?? Number.NEGATIVE_INFINITY, now().getTime()]);
|
|
209
|
+
return gaps;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Called by the company snapshot writer before it replaces `previous`, the
|
|
213
|
+
* snapshot on disk (whatever its age). When that snapshot said reporting is
|
|
214
|
+
* required and is older than `snapshotMaxAgeMs` (the snapshot reader's limit,
|
|
215
|
+
* a day) at `nowMs`, the stretch from when it
|
|
216
|
+
* went stale to now is recorded: no daemon flush may have seen it stale (the
|
|
217
|
+
* daemon was down or asleep), and once the fresh snapshot is on disk the
|
|
218
|
+
* stretch could no longer be told apart (review of #3469, INFO-A). Only on a
|
|
219
|
+
* machine whose Work Mesh root exists. Never throws: the snapshot is written
|
|
220
|
+
* whatever happens here.
|
|
221
|
+
*/
|
|
222
|
+
export function noteReplacedCompanySnapshot(companyUid, previous, env, snapshotMaxAgeMs, nowMs = Date.now()) {
|
|
223
|
+
try {
|
|
224
|
+
if (!previous || previous.flags[WORK_MESH_COMPANY_REPORTING_REQUIRED] !== true)
|
|
225
|
+
return;
|
|
226
|
+
const staleFrom = previous.fetchedAtMs + snapshotMaxAgeMs;
|
|
227
|
+
if (!Number.isFinite(staleFrom) || staleFrom >= nowMs)
|
|
228
|
+
return;
|
|
229
|
+
const root = workMeshRoot(env.HOME?.trim() || os.homedir(), env);
|
|
230
|
+
if (!fs.existsSync(root))
|
|
231
|
+
return;
|
|
232
|
+
addCompanyReportingGap(root, companyUid, [staleFrom, nowMs], () => new Date(nowMs));
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
/* the snapshot write goes ahead */
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=company-reporting-gaps.js.map
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { type SystemUnitState } from "./system-unit.js";
|
|
2
|
+
import type { RunCommand } from "../../../service-manager/launchd.js";
|
|
3
|
+
export declare const DUPLICATE_UNIT_RECORD = "duplicate-unit.json";
|
|
4
|
+
export declare const REMOVED_USER_UNIT_BACKUP = "hq-mesh-daemon.service.removed.bak";
|
|
5
|
+
export type DuplicateUnitAction =
|
|
6
|
+
/** Nothing to do: not Linux, no user unit, no system unit, another user's system unit, or a process of neither unit. */
|
|
7
|
+
"none"
|
|
8
|
+
/** The system unit's daemon held the lock for the settle time; the user unit was backed up and removed. */
|
|
9
|
+
| "removed-user-unit"
|
|
10
|
+
/** The user unit was backed up, but its file or wants link could not be deleted; the removal is retried. */
|
|
11
|
+
| "partial-removal"
|
|
12
|
+
/** Both units exist but the user unit was kept; `reason` says why. */
|
|
13
|
+
| "kept-user-unit"
|
|
14
|
+
/** This process belongs to the system unit: removal waits until it has held the pid lock. */
|
|
15
|
+
| "deferred-until-lock"
|
|
16
|
+
/** This process belongs to the user unit and the system unit's daemon holds this home's lock: exit, remove nothing. */
|
|
17
|
+
| "deferred-to-system-unit";
|
|
18
|
+
/** Which unit started this process. */
|
|
19
|
+
export type MeshUnitRole = "user-unit" | "system-unit" | "other";
|
|
20
|
+
export interface DuplicateUnitResult {
|
|
21
|
+
action: DuplicateUnitAction;
|
|
22
|
+
role?: MeshUnitRole;
|
|
23
|
+
reason?: string;
|
|
24
|
+
systemState?: SystemUnitState | null;
|
|
25
|
+
/** Steps that failed while removing the user unit (the removal still went ahead). */
|
|
26
|
+
warnings?: string[];
|
|
27
|
+
/** Where the removed user unit was copied before it was deleted. */
|
|
28
|
+
backupPath?: string;
|
|
29
|
+
/** Files that are still present after a removal attempt (partial-removal). */
|
|
30
|
+
remaining?: string[];
|
|
31
|
+
/** Which removal attempt this result comes from (settled path). */
|
|
32
|
+
attempt?: number;
|
|
33
|
+
/**
|
|
34
|
+
* True when this process was started by the user unit and the system
|
|
35
|
+
* unit's daemon holds this home's pid lock: this one exits instead of
|
|
36
|
+
* competing for the lock. Nothing is removed.
|
|
37
|
+
*/
|
|
38
|
+
exitAfter?: boolean;
|
|
39
|
+
/** True when the caller should run {@link removeDuplicateUserUnitOnceSettled} once it holds the pid lock. */
|
|
40
|
+
removeAfterLock?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface DuplicateUnitDeps {
|
|
43
|
+
platform?: NodeJS.Platform;
|
|
44
|
+
home?: string;
|
|
45
|
+
env?: NodeJS.ProcessEnv;
|
|
46
|
+
/** Directory holding the box's system units (tests pass a fake root). */
|
|
47
|
+
systemUnitDir?: string;
|
|
48
|
+
/**
|
|
49
|
+
* This process's user name and uid, compared with the system unit's User=
|
|
50
|
+
* (tests inject; null: unknown, so nothing is removed).
|
|
51
|
+
*/
|
|
52
|
+
user?: {
|
|
53
|
+
username: string;
|
|
54
|
+
uid: number;
|
|
55
|
+
} | null;
|
|
56
|
+
/** systemctl runner (tests inject a fake; never the real binary). */
|
|
57
|
+
run?: RunCommand;
|
|
58
|
+
sleep?: (ms: number) => Promise<void>;
|
|
59
|
+
/** /proc/self/cgroup text, to tell which unit started this process. */
|
|
60
|
+
readCgroup?: () => string | null;
|
|
61
|
+
/** Whether a pid is alive (tests inject; default signal 0). */
|
|
62
|
+
isProcessAlive?: (pid: number) => boolean;
|
|
63
|
+
/** How long the system unit's daemon must hold the pid lock before the user unit goes. */
|
|
64
|
+
settleMs?: number;
|
|
65
|
+
/** Wait between attempts after a partial removal. */
|
|
66
|
+
retryMs?: number;
|
|
67
|
+
/** Removal attempts before a partial removal is left as it is. */
|
|
68
|
+
maxAttempts?: number;
|
|
69
|
+
/** Delete a file (tests inject failures; default fs.unlinkSync). */
|
|
70
|
+
unlink?: (file: string) => void;
|
|
71
|
+
/** Flush a file or directory to disk (tests observe; default open + fsync). */
|
|
72
|
+
fsyncPath?: (file: string) => void;
|
|
73
|
+
/** Drop-in and environment.d locations for the user unit (tests pass fake dirs). */
|
|
74
|
+
userEnvironmentSources?: UserEnvironmentSources;
|
|
75
|
+
now?: () => Date;
|
|
76
|
+
log?: (line: string) => void;
|
|
77
|
+
}
|
|
78
|
+
export interface AfterLockDeps extends DuplicateUnitDeps {
|
|
79
|
+
/** Daemon dir holding the pid lock (the running daemon's handle.dir). */
|
|
80
|
+
lockDir?: string;
|
|
81
|
+
/** This process's pid, as written in the lock. */
|
|
82
|
+
pid?: number;
|
|
83
|
+
}
|
|
84
|
+
export declare const DEFAULT_SETTLE_MS = 30000;
|
|
85
|
+
export declare const DEFAULT_RETRY_MS: number;
|
|
86
|
+
export declare const DEFAULT_MAX_ATTEMPTS = 12;
|
|
87
|
+
export declare function meshUserUnitPath(home: string): string;
|
|
88
|
+
/** Where the user manager may read unit drop-ins and environment.d files (tests pass fake dirs). */
|
|
89
|
+
export interface UserEnvironmentSources {
|
|
90
|
+
/** Drop-in directories that apply to the user unit, besides ~/.config/systemd/user/<unit>.d. */
|
|
91
|
+
dropInDirs?: string[];
|
|
92
|
+
/** environment.d directories the user manager reads. */
|
|
93
|
+
environmentDirs?: string[];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The daemon dir the user unit's daemon locks: HQ_WORK_MESH_ROOT/daemon when
|
|
97
|
+
* its environment sets that, else <HOME>/.hq/work-mesh/daemon, with HOME
|
|
98
|
+
* taken from its environment when set there. The user manager is asked
|
|
99
|
+
* first. When it cannot be reached, the unit file is read, and the answer is
|
|
100
|
+
* null (do nothing) if the environment could also come from somewhere the
|
|
101
|
+
* file does not show: EnvironmentFile=, a drop-in, or an environment.d file
|
|
102
|
+
* that sets HOME or HQ_WORK_MESH_ROOT.
|
|
103
|
+
*/
|
|
104
|
+
export declare function userUnitDaemonDir(home: string, run?: RunCommand, sources?: UserEnvironmentSources): string | null;
|
|
105
|
+
/**
|
|
106
|
+
* Runner with the user manager's bus address filled in, so `systemctl --user`
|
|
107
|
+
* reaches it from a system service that runs as the box user.
|
|
108
|
+
*/
|
|
109
|
+
export declare function defaultDuplicateUnitRun(env?: NodeJS.ProcessEnv): RunCommand;
|
|
110
|
+
/** True when the cgroup text places this process in a user manager's hq-mesh-daemon.service. */
|
|
111
|
+
export declare function cgroupIsMeshUserUnit(cgroup: string | null): boolean;
|
|
112
|
+
/** True when the cgroup text places this process in the system manager's hq-mesh-daemon.service. */
|
|
113
|
+
export declare function cgroupIsMeshSystemUnit(cgroup: string | null): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Which unit started this process: the cgroup first, then systemd's
|
|
116
|
+
* INVOCATION_ID compared with the system unit's own InvocationID.
|
|
117
|
+
*/
|
|
118
|
+
export declare function meshUnitRole(cgroup: string | null, env: NodeJS.ProcessEnv, run: RunCommand): MeshUnitRole;
|
|
119
|
+
/** What systemd says about who the system unit runs as and its main process. */
|
|
120
|
+
export interface SystemUnitIdentity {
|
|
121
|
+
user: string;
|
|
122
|
+
dynamicUser: string;
|
|
123
|
+
mainPid: number;
|
|
124
|
+
loadState: string;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Read the system unit's effective identity from systemd, which has already
|
|
128
|
+
* merged every unit file and drop-in location in its own order. Null when
|
|
129
|
+
* systemctl cannot answer.
|
|
130
|
+
*/
|
|
131
|
+
export declare function readSystemUnitIdentity(run: RunCommand): SystemUnitIdentity | null;
|
|
132
|
+
/**
|
|
133
|
+
* Null when the system unit runs this user's Work Mesh as far as systemd
|
|
134
|
+
* says; otherwise why it does not (and nothing should happen).
|
|
135
|
+
*/
|
|
136
|
+
export declare function systemUnitIdentityMismatch(identity: SystemUnitIdentity | null, user: {
|
|
137
|
+
username: string;
|
|
138
|
+
uid: number;
|
|
139
|
+
} | null): string | null;
|
|
140
|
+
/**
|
|
141
|
+
* Runs at the start of `hq mesh daemon run`, before the pid lock. Never
|
|
142
|
+
* removes anything. A process started by the system unit gets
|
|
143
|
+
* `removeAfterLock`; a process started by the user unit gets `exitAfter`
|
|
144
|
+
* only when the system unit's daemon holds this home's pid lock.
|
|
145
|
+
*/
|
|
146
|
+
export declare function reconcileDuplicateMeshUnit(deps?: DuplicateUnitDeps): Promise<DuplicateUnitResult>;
|
|
147
|
+
/**
|
|
148
|
+
* Runs in the system unit's daemon once it holds the pid lock. Waits the
|
|
149
|
+
* settle time, then backs up and removes the user unit only if the system
|
|
150
|
+
* unit still runs as this user, this process still holds the lock, that lock
|
|
151
|
+
* is the user unit's own (same daemon dir), and the system unit is still
|
|
152
|
+
* active. A partial removal is recorded and retried while all of that still
|
|
153
|
+
* holds. Never throws.
|
|
154
|
+
*/
|
|
155
|
+
export declare function removeDuplicateUserUnitOnceSettled(deps?: AfterLockDeps): Promise<DuplicateUnitResult>;
|
|
156
|
+
//# sourceMappingURL=duplicate-unit.d.ts.map
|