@indigoai-us/hq-cli 5.345.223 → 5.345.225
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 +66 -0
- package/assets/bot-workers/setup/context/quick-reference.md +2 -2
- package/assets/mesh-daemon/README.md +11 -12
- package/dist/command-catalog.generated.d.ts +78 -4
- package/dist/command-catalog.generated.js +94 -4
- package/dist/commands/cloud-provision.d.ts +4 -1
- package/dist/commands/cloud-provision.js +7 -3
- package/dist/commands/cloud-retire.js +1 -1
- package/dist/commands/core-delegate-w1b.js +3 -1
- package/dist/commands/core-delegate.d.ts +5 -0
- package/dist/commands/core-dm-bind-native.d.ts +5 -1
- package/dist/commands/core-dm-bind-native.js +4 -0
- package/dist/commands/core-job-notify-native.d.ts +5 -1
- package/dist/commands/core-job-notify-native.js +8 -2
- package/dist/commands/core-jobs-native.d.ts +27 -1
- package/dist/commands/core-jobs-native.js +49 -28
- package/dist/commands/core-pipeline-conductor-native.js +7 -7
- package/dist/commands/core-pipeline-driver-native.js +5 -5
- package/dist/commands/core-refresh-vault-access-native.d.ts +5 -0
- package/dist/commands/core-refresh-vault-access-native.js +3 -1
- package/dist/commands/core-work-mesh-bind-trusted-native.js +1 -1
- package/dist/commands/daemon.d.ts +2 -0
- package/dist/commands/daemon.js +67 -1
- package/dist/commands/lanes-hold.d.ts +2 -27
- package/dist/commands/lanes-hold.js +3 -75
- package/dist/commands/lanes-loop.js +1 -1
- package/dist/commands/lanes-session-start.js +2 -2
- package/dist/commands/mesh.d.ts +12 -0
- package/dist/commands/mesh.js +13 -4
- package/dist/commands/monitor.js +1 -1
- package/dist/commands/packs.js +1 -1
- package/dist/commands/whoami.d.ts +10 -17
- package/dist/commands/whoami.js +7 -79
- package/dist/data/command-states.json +13 -0
- package/dist/index.js +1 -1
- package/dist/lib/daemon/catalog.d.ts +8 -5
- package/dist/lib/daemon/catalog.js +13 -7
- package/dist/lib/daemon/config.d.ts +5 -0
- package/dist/lib/daemon/config.js +6 -0
- package/dist/lib/daemon/lanes-monitor-state.d.ts +55 -0
- package/dist/lib/daemon/lanes-monitor-state.js +129 -0
- package/dist/lib/daemon/lanes-reconcile-loader.d.ts +7 -0
- package/dist/lib/daemon/lanes-reconcile-loader.js +13 -0
- package/dist/lib/daemon/merge-sections.d.ts +165 -0
- package/dist/lib/daemon/merge-sections.js +556 -0
- package/dist/lib/daemon/ops/policy.d.ts +1 -1
- package/dist/lib/daemon/ops/policy.js +1 -1
- package/dist/lib/daemon/protocol.d.ts +1 -1
- package/dist/lib/daemon/protocol.js +0 -6
- package/dist/lib/daemon/run.js +73 -1
- package/dist/lib/daemon/selfcheck.js +2 -2
- package/dist/lib/daemon/server.d.ts +2 -4
- package/dist/lib/daemon/server.js +5 -48
- package/dist/lib/daemon/state-file.d.ts +74 -4
- package/dist/lib/daemon/state-file.js +159 -19
- package/dist/lib/daemon/supervisor.d.ts +1 -1
- package/dist/lib/daemon/updater.d.ts +2 -6
- package/dist/lib/daemon/updater.js +4 -11
- package/dist/lib/daemon/whoami-cache.d.ts +96 -0
- package/dist/lib/daemon/whoami-cache.js +244 -0
- package/dist/lib/hook-engine/coverage.d.ts +2 -0
- package/dist/lib/hook-engine/coverage.js +71 -0
- package/dist/lib/hook-engine/native-precompact-compatibility.json +449 -0
- package/dist/lib/hook-engine/reconcile.js +14 -8
- package/dist/lib/install/hook-target.d.ts +13 -3
- package/dist/lib/install/hook-target.js +14 -11
- package/dist/lib/lanes/envelope-repair-dispatch.js +1 -1
- package/dist/lib/lanes/monitor.d.ts +2 -0
- package/dist/lib/lanes/monitor.js +2 -1
- package/dist/lib/lanes/registry-flag.d.ts +6 -0
- package/dist/lib/lanes/registry-flag.js +3 -2
- package/dist/lib/lanes/repo-merge-hold.d.ts +39 -0
- package/dist/lib/lanes/repo-merge-hold.js +90 -0
- package/dist/lib/lanes/watcher-main.js +3 -3
- package/dist/lib/mesh/live/daemon/doctor.js +1 -1
- package/dist/lib/mesh/live/daemon/install.js +2 -2
- package/dist/lib/windows/install-followup.js +1 -1
- package/dist/utils/client-health-contract.d.ts +1 -0
- package/dist/utils/client-health-contract.js +5 -0
- package/dist/utils/client-health.d.ts +2 -0
- package/dist/utils/client-health.js +40 -0
- package/dist/utils/cognito-session.d.ts +2 -14
- package/dist/utils/cognito-session.js +2 -40
- package/dist/utils/daemon-cli-version.d.ts +13 -0
- package/dist/utils/daemon-cli-version.js +47 -0
- package/dist/utils/duplicate-command-usage.js +1 -1
- package/dist/utils/machine-token-cache.d.ts +21 -0
- package/dist/utils/machine-token-cache.js +49 -0
- package/dist/utils/resident-package-preload.js +4 -0
- package/dist/utils/whoami-identity.d.ts +54 -0
- package/dist/utils/whoami-identity.js +108 -0
- package/package.json +7 -7
- package/dist/lib/daemon/ops/memory.d.ts +0 -18
- package/dist/lib/daemon/ops/memory.js +0 -65
- package/dist/lib/daemon/ops/mesh.d.ts +0 -38
- package/dist/lib/daemon/ops/mesh.js +0 -70
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
* and every service run the new version. The remote-control hosts notice an
|
|
17
17
|
* updated Claude or Codex on disk themselves and restart once idle.
|
|
18
18
|
*/
|
|
19
|
+
import { daemonVersionFromBootstrapCommandLine } from "../../utils/daemon-cli-version.js";
|
|
19
20
|
import type { SelfUpdateOutcome } from "../../utils/self-update.js";
|
|
20
21
|
import type { HqCloudUpdateOutcome } from "./hq-cloud.js";
|
|
21
22
|
import { type ToolInstall, type ToolName } from "./tools.js";
|
|
@@ -57,12 +58,7 @@ export interface UpdaterResult {
|
|
|
57
58
|
}
|
|
58
59
|
/** Only a strictly older, parseable daemon version triggers a restart, so a dev build never loops. */
|
|
59
60
|
export declare function daemonIsOlderThanCli(running: string, installed: string): boolean;
|
|
60
|
-
|
|
61
|
-
* The version a daemon handed to the staged child bootstrap that started this
|
|
62
|
-
* unit: `<node> <...>/hq-child-bootstrap.mjs <prefix> <version> [args]`.
|
|
63
|
-
* Daemons have started units this way since 2026-10-05 (#1541).
|
|
64
|
-
*/
|
|
65
|
-
export declare function daemonVersionFromBootstrapCommandLine(line: string | null): string | null;
|
|
61
|
+
export { daemonVersionFromBootstrapCommandLine };
|
|
66
62
|
export interface RunningDaemonVersionDeps {
|
|
67
63
|
/** Ask hqd; it listens only while HQ Anywhere is on, so this often fails. */
|
|
68
64
|
ping: () => Promise<string>;
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
* updated Claude or Codex on disk themselves and restart once idle.
|
|
18
18
|
*/
|
|
19
19
|
import { spawnSync } from "node:child_process";
|
|
20
|
+
import { daemonVersionFromBootstrapCommandLine } from "../../utils/daemon-cli-version.js";
|
|
20
21
|
import * as fs from "node:fs";
|
|
21
22
|
import semver from "semver";
|
|
22
23
|
import { locateTool, toolUpdateCommand } from "./tools.js";
|
|
@@ -26,17 +27,9 @@ export function daemonIsOlderThanCli(running, installed) {
|
|
|
26
27
|
const i = semver.valid(installed);
|
|
27
28
|
return r !== null && i !== null && semver.lt(r, i);
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* Daemons have started units this way since 2026-10-05 (#1541).
|
|
33
|
-
*/
|
|
34
|
-
export function daemonVersionFromBootstrapCommandLine(line) {
|
|
35
|
-
if (!line)
|
|
36
|
-
return null;
|
|
37
|
-
const match = /hq-child-bootstrap\.m?js\s.*?(?:^|\s)(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)(?=\s|$)/.exec(line);
|
|
38
|
-
return match?.[1] ?? null;
|
|
39
|
-
}
|
|
30
|
+
// Defined in the leaf module so client health can use it without importing the
|
|
31
|
+
// updater (resident daemon paths must not lazily import package modules).
|
|
32
|
+
export { daemonVersionFromBootstrapCommandLine };
|
|
40
33
|
/**
|
|
41
34
|
* The hq-cli version the running daemon was started from. hqd first; then the
|
|
42
35
|
* version a newer daemon puts in the unit's environment; then the version an
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daemon's cached `hq whoami` answer (hq-core-rust US-036).
|
|
3
|
+
*
|
|
4
|
+
* CLI commands that need the signed-in identity used to start `hq whoami
|
|
5
|
+
* --json` as a child, which loads the whole CLI and makes network calls. The
|
|
6
|
+
* daemon is already running on most machines, so it keeps the answer in
|
|
7
|
+
* `<daemon folder>/whoami.json` and those commands read the file instead.
|
|
8
|
+
*
|
|
9
|
+
* The file holds the identity `hq whoami --json` prints without `planLimits`:
|
|
10
|
+
* email, Cognito subject, delegated person, `prs_*`/`agt_*` uids, machine
|
|
11
|
+
* username and session expiry. No token is written. Like the state file it is
|
|
12
|
+
* 0600 in the per-host daemon folder and its contents are never logged.
|
|
13
|
+
*
|
|
14
|
+
* A reader uses the file only when it is current and still describes this
|
|
15
|
+
* session: the schema matches, it is younger than its stated maximum age, and
|
|
16
|
+
* the token source, subject, delegated subject, email, machine username and
|
|
17
|
+
* agent uid equal what the local token caches and machine credentials say now. The session fields that change with time
|
|
18
|
+
* (`authenticated`, `sessionExpiresAt`, `sessionExpiring`) are taken from the
|
|
19
|
+
* local caches, never from the file. Anything else (no daemon, a bot or a
|
|
20
|
+
* Windows box without one, a sign-in as someone else) reads as no answer, and
|
|
21
|
+
* the caller falls back to running `hq whoami` as before.
|
|
22
|
+
*
|
|
23
|
+
* The daemon refreshes the file on an in-process timer. The person-uid lookup
|
|
24
|
+
* is its only network call: once per signed-in subject, retried at most every
|
|
25
|
+
* 30 minutes while it has no answer.
|
|
26
|
+
*/
|
|
27
|
+
import { type WhoamiIdentity, type WhoamiTokenIdentity } from "../../utils/whoami-identity.js";
|
|
28
|
+
export declare const WHOAMI_CACHE_SCHEMA = "hq-whoami.v1";
|
|
29
|
+
export declare const WHOAMI_CACHE_FILE = "whoami.json";
|
|
30
|
+
/** Written into the file; a reader ignores the file past this age. */
|
|
31
|
+
export declare const WHOAMI_CACHE_MAX_AGE_SECONDS = 1800;
|
|
32
|
+
/** How often the daemon rewrites the file. */
|
|
33
|
+
export declare const WHOAMI_CACHE_REFRESH_MS: number;
|
|
34
|
+
/** How long the daemon waits before asking again for a person uid it could not get. */
|
|
35
|
+
export declare const WHOAMI_PERSON_UID_RETRY_MS: number;
|
|
36
|
+
export interface WhoamiCacheFile {
|
|
37
|
+
schema: typeof WHOAMI_CACHE_SCHEMA;
|
|
38
|
+
written_at: string;
|
|
39
|
+
max_age_seconds: number;
|
|
40
|
+
identity: WhoamiIdentity;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* `HQ_WHOAMI_CACHE_FILE` when set in `env` or in this process (tests and
|
|
44
|
+
* isolated daemons), else `<daemon folder>/whoami.json` for this home.
|
|
45
|
+
*/
|
|
46
|
+
export declare function whoamiCachePath(home?: string, env?: NodeJS.ProcessEnv): string;
|
|
47
|
+
/** The file, or null when it is not a complete, current-schema, unexpired file at `now`. */
|
|
48
|
+
export declare function parseWhoamiCache(text: string, now?: Date): WhoamiCacheFile | null;
|
|
49
|
+
export interface ReadCachedWhoamiOptions {
|
|
50
|
+
home?: string;
|
|
51
|
+
env?: NodeJS.ProcessEnv;
|
|
52
|
+
now?: Date;
|
|
53
|
+
/** This session's identity from the local token caches (no network). Read only when the file exists. */
|
|
54
|
+
local: () => WhoamiIdentity;
|
|
55
|
+
/** Test seam for the file read. */
|
|
56
|
+
readFile?: (file: string) => string | null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The daemon's whoami answer for this session, or null when the caller
|
|
60
|
+
* should run `hq whoami` itself (see the file comment).
|
|
61
|
+
*/
|
|
62
|
+
export declare function readCachedWhoami(options: ReadCachedWhoamiOptions): WhoamiIdentity | null;
|
|
63
|
+
/**
|
|
64
|
+
* What `hq whoami` (plain, or `--json`) would print for `identity`, for
|
|
65
|
+
* callers that parse that output. `--json` matches the command's own output
|
|
66
|
+
* with `planLimits` unknown (null); the plain form carries the identity lines
|
|
67
|
+
* only, without the plan or deleted-company notices.
|
|
68
|
+
*/
|
|
69
|
+
export declare function renderCachedWhoami(identity: WhoamiIdentity, json: boolean): string;
|
|
70
|
+
export interface WhoamiCacheWriterOptions {
|
|
71
|
+
home: string;
|
|
72
|
+
env: NodeJS.ProcessEnv;
|
|
73
|
+
log: (level: "info" | "warn" | "error", message: string) => void;
|
|
74
|
+
/** Builds the identity; `readPersonUid` is called only for an authenticated person session. */
|
|
75
|
+
build: (readPersonUid: (token: WhoamiTokenIdentity) => Promise<string | null>) => Promise<WhoamiIdentity>;
|
|
76
|
+
/** The network person-uid lookup (`hq whoami`'s own). */
|
|
77
|
+
lookupPersonUid: (token: WhoamiTokenIdentity) => Promise<string | null>;
|
|
78
|
+
intervalMs?: number;
|
|
79
|
+
now?: () => number;
|
|
80
|
+
schedule?: (callback: () => void, delayMs: number) => () => void;
|
|
81
|
+
write?: (file: string, text: string) => void;
|
|
82
|
+
}
|
|
83
|
+
export interface WhoamiCacheWriter {
|
|
84
|
+
/** Rewrite the file now. Resolves when written; a failure is logged, never thrown. */
|
|
85
|
+
refresh(): Promise<void>;
|
|
86
|
+
close(): void;
|
|
87
|
+
}
|
|
88
|
+
/** The daemon side: write the file at start and on an in-process timer. */
|
|
89
|
+
export declare function startWhoamiCacheWriter(options: WhoamiCacheWriterOptions): WhoamiCacheWriter;
|
|
90
|
+
/**
|
|
91
|
+
* For CLI commands that used to run `hq whoami [--json]`: that command's
|
|
92
|
+
* output from the daemon's cached answer, or null when there is no usable
|
|
93
|
+
* answer and the caller should run `hq whoami` itself.
|
|
94
|
+
*/
|
|
95
|
+
export declare function daemonWhoamiOutput(json: boolean, env?: NodeJS.ProcessEnv): string | null;
|
|
96
|
+
//# sourceMappingURL=whoami-cache.d.ts.map
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daemon's cached `hq whoami` answer (hq-core-rust US-036).
|
|
3
|
+
*
|
|
4
|
+
* CLI commands that need the signed-in identity used to start `hq whoami
|
|
5
|
+
* --json` as a child, which loads the whole CLI and makes network calls. The
|
|
6
|
+
* daemon is already running on most machines, so it keeps the answer in
|
|
7
|
+
* `<daemon folder>/whoami.json` and those commands read the file instead.
|
|
8
|
+
*
|
|
9
|
+
* The file holds the identity `hq whoami --json` prints without `planLimits`:
|
|
10
|
+
* email, Cognito subject, delegated person, `prs_*`/`agt_*` uids, machine
|
|
11
|
+
* username and session expiry. No token is written. Like the state file it is
|
|
12
|
+
* 0600 in the per-host daemon folder and its contents are never logged.
|
|
13
|
+
*
|
|
14
|
+
* A reader uses the file only when it is current and still describes this
|
|
15
|
+
* session: the schema matches, it is younger than its stated maximum age, and
|
|
16
|
+
* the token source, subject, delegated subject, email, machine username and
|
|
17
|
+
* agent uid equal what the local token caches and machine credentials say now. The session fields that change with time
|
|
18
|
+
* (`authenticated`, `sessionExpiresAt`, `sessionExpiring`) are taken from the
|
|
19
|
+
* local caches, never from the file. Anything else (no daemon, a bot or a
|
|
20
|
+
* Windows box without one, a sign-in as someone else) reads as no answer, and
|
|
21
|
+
* the caller falls back to running `hq whoami` as before.
|
|
22
|
+
*
|
|
23
|
+
* The daemon refreshes the file on an in-process timer. The person-uid lookup
|
|
24
|
+
* is its only network call: once per signed-in subject, retried at most every
|
|
25
|
+
* 30 minutes while it has no answer.
|
|
26
|
+
*/
|
|
27
|
+
import * as os from "node:os";
|
|
28
|
+
import * as path from "node:path";
|
|
29
|
+
import { daemonPaths, readStoreFile, StoreRefusedError, writeStoreFile } from "./paths.js";
|
|
30
|
+
import { localWhoamiIdentity } from "../../utils/whoami-identity.js";
|
|
31
|
+
export const WHOAMI_CACHE_SCHEMA = "hq-whoami.v1";
|
|
32
|
+
export const WHOAMI_CACHE_FILE = "whoami.json";
|
|
33
|
+
/** Written into the file; a reader ignores the file past this age. */
|
|
34
|
+
export const WHOAMI_CACHE_MAX_AGE_SECONDS = 1800;
|
|
35
|
+
/** How often the daemon rewrites the file. */
|
|
36
|
+
export const WHOAMI_CACHE_REFRESH_MS = 5 * 60_000;
|
|
37
|
+
/** How long the daemon waits before asking again for a person uid it could not get. */
|
|
38
|
+
export const WHOAMI_PERSON_UID_RETRY_MS = 30 * 60_000;
|
|
39
|
+
const MAX_FILE_BYTES = 64 * 1024;
|
|
40
|
+
/** A file dated this far in the future is treated as unreadable. */
|
|
41
|
+
const FUTURE_SKEW_MS = 60_000;
|
|
42
|
+
/**
|
|
43
|
+
* `HQ_WHOAMI_CACHE_FILE` when set in `env` or in this process (tests and
|
|
44
|
+
* isolated daemons), else `<daemon folder>/whoami.json` for this home.
|
|
45
|
+
*/
|
|
46
|
+
export function whoamiCachePath(home = os.homedir(), env = process.env) {
|
|
47
|
+
const explicit = env.HQ_WHOAMI_CACHE_FILE?.trim() || process.env.HQ_WHOAMI_CACHE_FILE?.trim();
|
|
48
|
+
if (explicit)
|
|
49
|
+
return explicit;
|
|
50
|
+
return path.join(daemonPaths(home, env).root, WHOAMI_CACHE_FILE);
|
|
51
|
+
}
|
|
52
|
+
function isRecord(value) {
|
|
53
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
54
|
+
}
|
|
55
|
+
const stringOrNull = (value) => value === null || typeof value === "string";
|
|
56
|
+
function parseIdentity(value) {
|
|
57
|
+
if (!isRecord(value) || value.schemaVersion !== 1)
|
|
58
|
+
return null;
|
|
59
|
+
if (typeof value.authenticated !== "boolean")
|
|
60
|
+
return null;
|
|
61
|
+
if (value.tokenSource !== "machine" && value.tokenSource !== "person")
|
|
62
|
+
return null;
|
|
63
|
+
for (const key of ["email", "sub", "delegatedEmail", "delegatedSub", "personUid", "agentUid", "username", "sessionExpiresAt"]) {
|
|
64
|
+
if (!stringOrNull(value[key]))
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
if (value.sessionExpiring !== null && typeof value.sessionExpiring !== "boolean")
|
|
68
|
+
return null;
|
|
69
|
+
return {
|
|
70
|
+
schemaVersion: 1,
|
|
71
|
+
authenticated: value.authenticated,
|
|
72
|
+
tokenSource: value.tokenSource,
|
|
73
|
+
email: value.email,
|
|
74
|
+
sub: value.sub,
|
|
75
|
+
delegatedEmail: value.delegatedEmail,
|
|
76
|
+
delegatedSub: value.delegatedSub,
|
|
77
|
+
personUid: value.personUid,
|
|
78
|
+
agentUid: value.agentUid,
|
|
79
|
+
username: value.username,
|
|
80
|
+
sessionExpiresAt: value.sessionExpiresAt,
|
|
81
|
+
sessionExpiring: value.sessionExpiring,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** The file, or null when it is not a complete, current-schema, unexpired file at `now`. */
|
|
85
|
+
export function parseWhoamiCache(text, now = new Date()) {
|
|
86
|
+
let parsed;
|
|
87
|
+
try {
|
|
88
|
+
parsed = JSON.parse(text);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (!isRecord(parsed) || parsed.schema !== WHOAMI_CACHE_SCHEMA)
|
|
94
|
+
return null;
|
|
95
|
+
if (typeof parsed.written_at !== "string" || typeof parsed.max_age_seconds !== "number")
|
|
96
|
+
return null;
|
|
97
|
+
const writtenAt = Date.parse(parsed.written_at);
|
|
98
|
+
if (!Number.isFinite(writtenAt) || !Number.isFinite(parsed.max_age_seconds) || parsed.max_age_seconds <= 0)
|
|
99
|
+
return null;
|
|
100
|
+
const age = now.getTime() - writtenAt;
|
|
101
|
+
if (age < -FUTURE_SKEW_MS || age > parsed.max_age_seconds * 1000)
|
|
102
|
+
return null;
|
|
103
|
+
const identity = parseIdentity(parsed.identity);
|
|
104
|
+
if (!identity)
|
|
105
|
+
return null;
|
|
106
|
+
return { schema: WHOAMI_CACHE_SCHEMA, written_at: parsed.written_at, max_age_seconds: parsed.max_age_seconds, identity };
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The daemon's whoami answer for this session, or null when the caller
|
|
110
|
+
* should run `hq whoami` itself (see the file comment).
|
|
111
|
+
*/
|
|
112
|
+
export function readCachedWhoami(options) {
|
|
113
|
+
const file = whoamiCachePath(options.home ?? options.env?.HOME ?? os.homedir(), options.env ?? process.env);
|
|
114
|
+
let text;
|
|
115
|
+
try {
|
|
116
|
+
text = options.readFile ? options.readFile(file) : readStoreFile(file, MAX_FILE_BYTES)?.text ?? null;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (text === null)
|
|
122
|
+
return null;
|
|
123
|
+
const cached = parseWhoamiCache(text, options.now ?? new Date());
|
|
124
|
+
if (!cached)
|
|
125
|
+
return null;
|
|
126
|
+
let local;
|
|
127
|
+
try {
|
|
128
|
+
local = options.local();
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const identity = cached.identity;
|
|
134
|
+
if (local.tokenSource !== identity.tokenSource
|
|
135
|
+
|| local.sub !== identity.sub
|
|
136
|
+
|| local.email !== identity.email
|
|
137
|
+
|| local.delegatedSub !== identity.delegatedSub
|
|
138
|
+
|| local.delegatedEmail !== identity.delegatedEmail
|
|
139
|
+
|| local.username !== identity.username
|
|
140
|
+
// A re-enrolled machine can keep its username and get a new agt_* uid.
|
|
141
|
+
|| local.agentUid !== identity.agentUid)
|
|
142
|
+
return null;
|
|
143
|
+
return {
|
|
144
|
+
...identity,
|
|
145
|
+
authenticated: local.authenticated,
|
|
146
|
+
sessionExpiresAt: local.sessionExpiresAt,
|
|
147
|
+
sessionExpiring: local.sessionExpiring,
|
|
148
|
+
personUid: local.authenticated ? identity.personUid : null,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* What `hq whoami` (plain, or `--json`) would print for `identity`, for
|
|
153
|
+
* callers that parse that output. `--json` matches the command's own output
|
|
154
|
+
* with `planLimits` unknown (null); the plain form carries the identity lines
|
|
155
|
+
* only, without the plan or deleted-company notices.
|
|
156
|
+
*/
|
|
157
|
+
export function renderCachedWhoami(identity, json) {
|
|
158
|
+
if (json)
|
|
159
|
+
return `${JSON.stringify({ ...identity, planLimits: null }, null, 2)}\n`;
|
|
160
|
+
if (identity.tokenSource === "machine") {
|
|
161
|
+
return `Machine identity ${identity.username ?? "unknown"}${identity.agentUid ? ` (agent ${identity.agentUid})` : ""} — sessions mint automatically\ntoken source: machine\n`;
|
|
162
|
+
}
|
|
163
|
+
if (identity.sessionExpiresAt === null)
|
|
164
|
+
return "Not logged in. Run 'hq login' to authenticate.\n";
|
|
165
|
+
if (!identity.authenticated) {
|
|
166
|
+
return `Session expired for ${identity.email ?? "unknown"}. Run 'hq login' to re-authenticate.\ntoken source: person\n`;
|
|
167
|
+
}
|
|
168
|
+
return `Logged in as ${identity.email ?? "unknown"}${identity.sub ? ` (${identity.sub})` : ""}\ntoken source: person\n`;
|
|
169
|
+
}
|
|
170
|
+
/** The daemon side: write the file at start and on an in-process timer. */
|
|
171
|
+
export function startWhoamiCacheWriter(options) {
|
|
172
|
+
const now = options.now ?? Date.now;
|
|
173
|
+
const file = whoamiCachePath(options.home, options.env);
|
|
174
|
+
const write = options.write ?? ((target, text) => writeStoreFile(target, text, { create: true }));
|
|
175
|
+
const schedule = options.schedule ?? ((callback, delayMs) => {
|
|
176
|
+
const timer = setInterval(callback, delayMs);
|
|
177
|
+
timer.unref();
|
|
178
|
+
return () => clearInterval(timer);
|
|
179
|
+
});
|
|
180
|
+
// One person-uid lookup per subject; a missing answer is asked again after WHOAMI_PERSON_UID_RETRY_MS.
|
|
181
|
+
let memo = null;
|
|
182
|
+
let closed = false;
|
|
183
|
+
let inFlight = null;
|
|
184
|
+
let lastError = null;
|
|
185
|
+
const personUid = async (token) => {
|
|
186
|
+
const key = `${token.sub ?? ""}|${token.entityUid ?? ""}`;
|
|
187
|
+
if (memo && memo.key === key && (memo.personUid !== null || now() - memo.at < WHOAMI_PERSON_UID_RETRY_MS))
|
|
188
|
+
return memo.personUid;
|
|
189
|
+
const found = await options.lookupPersonUid(token);
|
|
190
|
+
memo = { key, personUid: found, at: now() };
|
|
191
|
+
return found;
|
|
192
|
+
};
|
|
193
|
+
const refresh = () => {
|
|
194
|
+
if (closed)
|
|
195
|
+
return Promise.resolve();
|
|
196
|
+
if (inFlight)
|
|
197
|
+
return inFlight;
|
|
198
|
+
inFlight = (async () => {
|
|
199
|
+
try {
|
|
200
|
+
const identity = await options.build(personUid);
|
|
201
|
+
if (closed)
|
|
202
|
+
return;
|
|
203
|
+
const body = {
|
|
204
|
+
schema: WHOAMI_CACHE_SCHEMA,
|
|
205
|
+
written_at: new Date(now()).toISOString(),
|
|
206
|
+
max_age_seconds: WHOAMI_CACHE_MAX_AGE_SECONDS,
|
|
207
|
+
identity,
|
|
208
|
+
};
|
|
209
|
+
write(file, `${JSON.stringify(body, null, 2)}\n`);
|
|
210
|
+
lastError = null;
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
const message = error instanceof StoreRefusedError ? error.code : error instanceof Error ? error.name : "UnknownError";
|
|
214
|
+
if (message !== lastError)
|
|
215
|
+
options.log("warn", `could not refresh the cached whoami answer (${message}); CLI commands run hq whoami instead`);
|
|
216
|
+
lastError = message;
|
|
217
|
+
}
|
|
218
|
+
})().finally(() => {
|
|
219
|
+
inFlight = null;
|
|
220
|
+
});
|
|
221
|
+
return inFlight;
|
|
222
|
+
};
|
|
223
|
+
void refresh();
|
|
224
|
+
const cancel = schedule(() => { void refresh(); }, options.intervalMs ?? WHOAMI_CACHE_REFRESH_MS);
|
|
225
|
+
return {
|
|
226
|
+
refresh,
|
|
227
|
+
close() {
|
|
228
|
+
if (closed)
|
|
229
|
+
return;
|
|
230
|
+
closed = true;
|
|
231
|
+
cancel();
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* For CLI commands that used to run `hq whoami [--json]`: that command's
|
|
237
|
+
* output from the daemon's cached answer, or null when there is no usable
|
|
238
|
+
* answer and the caller should run `hq whoami` itself.
|
|
239
|
+
*/
|
|
240
|
+
export function daemonWhoamiOutput(json, env = process.env) {
|
|
241
|
+
const identity = readCachedWhoami({ env, local: () => localWhoamiIdentity(env) });
|
|
242
|
+
return identity ? renderCachedWhoami(identity, json) : null;
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=whoami-cache.js.map
|
|
@@ -146,6 +146,8 @@ export declare function shellHooks(root: string, host: CoreNextHost, event: stri
|
|
|
146
146
|
* (`personal/hooks/Stop/*.sh`); any other claim names one hook exactly.
|
|
147
147
|
*/
|
|
148
148
|
export declare function claimCovers(pattern: string, hook: string): boolean;
|
|
149
|
+
/** The same install-level checks as stop/precompact/guard.rs, before reindex switches the event. */
|
|
150
|
+
export declare function precompactCompatibilityHold(root: string, env?: NodeJS.ProcessEnv): string | null;
|
|
149
151
|
/** The old dispatcher's hooks for `event` on `host` that the engine does not cover. */
|
|
150
152
|
export declare function uncoveredHooks(root: string, host: CoreNextHost, event: string, covers: Readonly<Record<string, readonly string[]>> | undefined, env?: NodeJS.ProcessEnv): string[];
|
|
151
153
|
/** Records the environment `target` was resolved under; returns `target`. */
|
|
@@ -29,6 +29,7 @@ import * as path from 'node:path';
|
|
|
29
29
|
import { isCoreNextEntry } from './registration.js';
|
|
30
30
|
import stopCompatibility from './native-stop-compatibility.json' with { type: 'json' };
|
|
31
31
|
import gitStopConfig from './git-stop-config.json' with { type: 'json' };
|
|
32
|
+
import precompactCompatibility from './native-precompact-compatibility.json' with { type: 'json' };
|
|
32
33
|
/** Stands for the hook list when it cannot be read; never covered. */
|
|
33
34
|
export const UNREADABLE = '(the old dispatcher hook list could not be read)';
|
|
34
35
|
/** Hooks whose result depends on Git's environment (their queued jobs do not inherit it). */
|
|
@@ -1558,6 +1559,71 @@ export function claimCovers(pattern, hook) {
|
|
|
1558
1559
|
const source = pattern.split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('[^/]*');
|
|
1559
1560
|
return new RegExp(`^${source}$`).test(hook);
|
|
1560
1561
|
}
|
|
1562
|
+
/** The same install-level checks as stop/precompact/guard.rs, before reindex switches the event. */
|
|
1563
|
+
export function precompactCompatibilityHold(root, env = process.env) {
|
|
1564
|
+
let registry;
|
|
1565
|
+
try {
|
|
1566
|
+
const file = path.join(root, '.claude/hooks/hook-registry.json');
|
|
1567
|
+
if (!fs.statSync(file).isFile())
|
|
1568
|
+
return 'unreadable PreCompact registry';
|
|
1569
|
+
registry = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
1570
|
+
}
|
|
1571
|
+
catch {
|
|
1572
|
+
return 'unreadable PreCompact registry';
|
|
1573
|
+
}
|
|
1574
|
+
const groups = registry?.hooks?.PreCompact;
|
|
1575
|
+
const actual = Array.isArray(groups) ? groups : [];
|
|
1576
|
+
// Object key order is immaterial in serde_json; array order is not.
|
|
1577
|
+
const canonical = (value) => {
|
|
1578
|
+
if (Array.isArray(value))
|
|
1579
|
+
return `[${value.map(canonical).join(',')}]`;
|
|
1580
|
+
if (value !== null && typeof value === 'object')
|
|
1581
|
+
return `{${Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(',')}}`;
|
|
1582
|
+
return JSON.stringify(value) ?? 'null';
|
|
1583
|
+
};
|
|
1584
|
+
const projection = (items) => items.map(group => {
|
|
1585
|
+
const g = group;
|
|
1586
|
+
return { matcher: g?.matcher ?? null, hooks: g?.hooks ?? null };
|
|
1587
|
+
});
|
|
1588
|
+
if (canonical(projection(actual)) !== canonical(projection(precompactCompatibility.registry)))
|
|
1589
|
+
return 'custom PreCompact registry execution';
|
|
1590
|
+
const hashes = new Map();
|
|
1591
|
+
for (const relative of precompactCompatibility.fileOrder) {
|
|
1592
|
+
const versions = precompactCompatibility.files[relative];
|
|
1593
|
+
const file = path.join(root, relative);
|
|
1594
|
+
let stat;
|
|
1595
|
+
try {
|
|
1596
|
+
stat = fs.statSync(file);
|
|
1597
|
+
}
|
|
1598
|
+
catch (error) {
|
|
1599
|
+
if (error.code === 'ENOENT')
|
|
1600
|
+
continue;
|
|
1601
|
+
return `unreadable PreCompact file ${relative}`;
|
|
1602
|
+
}
|
|
1603
|
+
if (!stat.isFile())
|
|
1604
|
+
return `nonregular PreCompact file ${relative}`;
|
|
1605
|
+
let bytes;
|
|
1606
|
+
try {
|
|
1607
|
+
bytes = fs.readFileSync(file);
|
|
1608
|
+
}
|
|
1609
|
+
catch {
|
|
1610
|
+
return `unreadable PreCompact file ${relative}`;
|
|
1611
|
+
}
|
|
1612
|
+
const hash = createHash('sha256').update(bytes).digest('hex');
|
|
1613
|
+
if (!versions.includes(hash))
|
|
1614
|
+
return `unreleased PreCompact file ${relative}`;
|
|
1615
|
+
hashes.set(relative, hash);
|
|
1616
|
+
}
|
|
1617
|
+
const master = hashes.get('.claude/hooks/master-hook.sh');
|
|
1618
|
+
if (master && precompactCompatibility.fromBeta104Master.includes(master)) {
|
|
1619
|
+
const reader = hashes.get('core/scripts/lib/session-id.sh');
|
|
1620
|
+
if (reader && reader !== precompactCompatibility.fromBeta104SessionId)
|
|
1621
|
+
return 'mixed PreCompact session reader generation';
|
|
1622
|
+
if (!reader && env.SESSION_ID_PAYLOAD_JQ)
|
|
1623
|
+
return 'PreCompact session reader environment override';
|
|
1624
|
+
}
|
|
1625
|
+
return null;
|
|
1626
|
+
}
|
|
1561
1627
|
/** The old dispatcher's hooks for `event` on `host` that the engine does not cover. */
|
|
1562
1628
|
export function uncoveredHooks(root, host, event, covers, env = process.env) {
|
|
1563
1629
|
const hooks = shellHooks(root, host, event);
|
|
@@ -1570,6 +1636,11 @@ export function uncoveredHooks(root, host, event, covers, env = process.env) {
|
|
|
1570
1636
|
if (hold)
|
|
1571
1637
|
missing.push(`(native Stop compatibility: ${hold})`);
|
|
1572
1638
|
}
|
|
1639
|
+
if (event === 'PreCompact' && covered.includes('master-hook:PreCompact')) {
|
|
1640
|
+
const hold = precompactCompatibilityHold(root, env);
|
|
1641
|
+
if (hold)
|
|
1642
|
+
missing.push(`(native PreCompact compatibility: ${hold})`);
|
|
1643
|
+
}
|
|
1573
1644
|
return missing;
|
|
1574
1645
|
}
|
|
1575
1646
|
/**
|