@indigoai-us/hq-cli 5.345.222 → 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 +89 -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 +79 -5
- package/dist/command-catalog.generated.js +95 -5
- package/dist/commands/bot.d.ts +1 -1
- package/dist/commands/bot.js +34 -33
- 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 +210 -188
- 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 +14 -3
- package/dist/commands/sync-core.js +31 -12
- 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/bot/fleet-cron.d.ts +26 -3
- package/dist/lib/bot/fleet-cron.js +44 -4
- package/dist/lib/bot/run.d.ts +4 -4
- package/dist/lib/bot/run.js +5 -3
- package/dist/lib/bot/signals-engine-flag.d.ts +53 -13
- package/dist/lib/bot/signals-engine-flag.js +202 -9
- 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/machine-checks.d.ts +45 -0
- package/dist/lib/daemon/machine-checks.js +95 -11
- package/dist/lib/daemon/merge-sections.d.ts +165 -0
- package/dist/lib/daemon/merge-sections.js +556 -0
- package/dist/lib/daemon/native-notice-ledger.d.ts +9 -0
- package/dist/lib/daemon/native-notice-ledger.js +43 -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 +76 -3
- 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 +11 -1
- package/dist/lib/daemon/supervisor.js +86 -0
- 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/flag-registry-worker-source.generated.d.ts +1 -1
- package/dist/lib/flag-registry-worker-source.generated.js +1 -1
- package/dist/lib/flag-registry-worker.js +33 -3
- package/dist/lib/flag-registry.js +13 -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/scan-packages/index.js +8 -9
- package/dist/lib/signals/engine.d.ts +8 -0
- package/dist/lib/signals/engine.js +15 -5
- package/dist/lib/sync-core/flag.d.ts +18 -0
- package/dist/lib/sync-core/flag.js +66 -0
- package/dist/lib/sync-core/reconcile.d.ts +6 -0
- package/dist/lib/sync-core/reconcile.js +15 -0
- package/dist/lib/sync-core/rollout-state.d.ts +2 -0
- package/dist/lib/sync-core/rollout-state.js +7 -0
- package/dist/lib/sync-core/vault-settings.d.ts +12 -1
- package/dist/lib/sync-core/vault-settings.js +23 -7
- 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/pack-contributions.d.ts +14 -2
- package/dist/utils/pack-contributions.js +31 -5
- 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/scripts/bench-core.mjs +34 -3
- 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
|
@@ -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
|