@indigoai-us/hq-cli 5.111.0 → 5.111.1
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 +12 -0
- package/dist/lib/bot/owner-context.d.ts +8 -0
- package/dist/lib/bot/owner-context.js +28 -4
- package/dist/lib/bot/run.js +8 -1
- package/dist/utils/cognito-session.js +49 -13
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [5.111.1] — 2026-09-14
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- A new bot's first message no longer tells its signed-in owner they are not
|
|
10
|
+
signed in. While the bot fetched its own sign-in, a check of the owner's
|
|
11
|
+
sign-in in the same process could read the bot's credentials instead and find
|
|
12
|
+
no account. Sign-in reads now wait for that to finish.
|
|
13
|
+
- When a bot cannot read its owner's account right away, it tries again before
|
|
14
|
+
answering, and never asks a signed-in owner to sign in or run a command. The
|
|
15
|
+
bot's log now records why an owner check failed.
|
|
16
|
+
|
|
5
17
|
## [5.111.0] — 2026-09-14
|
|
6
18
|
|
|
7
19
|
### Added
|
|
@@ -29,6 +29,10 @@ export type OwnerContext = {
|
|
|
29
29
|
ownerUid: string;
|
|
30
30
|
reason: string;
|
|
31
31
|
checkedAt: string;
|
|
32
|
+
/** True when the owner's sign-in exists and only reading their account failed. */
|
|
33
|
+
signedIn?: boolean;
|
|
34
|
+
/** Technical cause, for logs only; never shown to the model. */
|
|
35
|
+
detail?: string;
|
|
32
36
|
} | {
|
|
33
37
|
status: "mismatch";
|
|
34
38
|
ownerUid: string;
|
|
@@ -48,8 +52,12 @@ export interface OwnerLookupIo {
|
|
|
48
52
|
status: string;
|
|
49
53
|
}>>;
|
|
50
54
|
now?: () => Date;
|
|
55
|
+
/** Test seam for the retry pause. */
|
|
56
|
+
sleep?: (ms: number) => Promise<void>;
|
|
51
57
|
}
|
|
52
58
|
export declare const defaultOwnerLookupIo: OwnerLookupIo;
|
|
59
|
+
/** A read right after sign-in or bot start can fail once; try again before reporting it. */
|
|
60
|
+
export declare const OWNER_LOOKUP_RETRY_DELAYS_MS: number[];
|
|
53
61
|
export declare function lookupOwnerContext(ownerUid: string, io?: OwnerLookupIo): Promise<OwnerContext>;
|
|
54
62
|
/** Successful lookups are reused for 5 minutes, failures retried after 1 minute. */
|
|
55
63
|
export declare const OWNER_CONTEXT_TTL_MS: number;
|
|
@@ -33,6 +33,23 @@ export const defaultOwnerLookupIo = {
|
|
|
33
33
|
return data.memberships;
|
|
34
34
|
},
|
|
35
35
|
};
|
|
36
|
+
/** A read right after sign-in or bot start can fail once; try again before reporting it. */
|
|
37
|
+
export const OWNER_LOOKUP_RETRY_DELAYS_MS = [1_000, 2_000];
|
|
38
|
+
async function withRetry(fn, io) {
|
|
39
|
+
const sleep = io.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
40
|
+
let lastErr;
|
|
41
|
+
for (const delay of [0, ...OWNER_LOOKUP_RETRY_DELAYS_MS]) {
|
|
42
|
+
if (delay)
|
|
43
|
+
await sleep(delay);
|
|
44
|
+
try {
|
|
45
|
+
return await fn();
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
lastErr = err;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throw lastErr;
|
|
52
|
+
}
|
|
36
53
|
export async function lookupOwnerContext(ownerUid, io = defaultOwnerLookupIo) {
|
|
37
54
|
const checkedAt = (io.now ?? (() => new Date()))().toISOString();
|
|
38
55
|
let token;
|
|
@@ -44,22 +61,23 @@ export async function lookupOwnerContext(ownerUid, io = defaultOwnerLookupIo) {
|
|
|
44
61
|
}
|
|
45
62
|
let signedInAs;
|
|
46
63
|
try {
|
|
47
|
-
signedInAs = await io.callerPersonUid(token);
|
|
64
|
+
signedInAs = await withRetry(() => io.callerPersonUid(token), io);
|
|
48
65
|
}
|
|
49
66
|
catch (err) {
|
|
50
|
-
|
|
67
|
+
// The raw error ("…Sign in to HQ once…") would read to the model as "not signed in".
|
|
68
|
+
return { status: "unavailable", ownerUid, reason: "their HQ account could not be read just now", detail: message(err), checkedAt, signedIn: true };
|
|
51
69
|
}
|
|
52
70
|
if (signedInAs !== ownerUid)
|
|
53
71
|
return { status: "mismatch", ownerUid, signedInAs, checkedAt };
|
|
54
72
|
try {
|
|
55
|
-
const rows = await io.memberships(token);
|
|
73
|
+
const rows = await withRetry(() => io.memberships(token), io);
|
|
56
74
|
const companies = rows
|
|
57
75
|
.filter((m) => m.status === "active")
|
|
58
76
|
.map((m) => ({ companyUid: m.companyUid, ...(m.companySlug ? { companySlug: m.companySlug } : {}), role: m.role }));
|
|
59
77
|
return { status: "ok", ownerUid, companies, checkedAt };
|
|
60
78
|
}
|
|
61
79
|
catch (err) {
|
|
62
|
-
return { status: "unavailable", ownerUid, reason: `could not read your owner's companies (${message(err)})`, checkedAt };
|
|
80
|
+
return { status: "unavailable", ownerUid, reason: `could not read your owner's companies (${message(err)})`, checkedAt, signedIn: true };
|
|
63
81
|
}
|
|
64
82
|
}
|
|
65
83
|
/** Successful lookups are reused for 5 minutes, failures retried after 1 minute. */
|
|
@@ -141,6 +159,12 @@ export function ownerContextBlock(ctx, agentUid) {
|
|
|
141
159
|
`The HQ sign-in on this computer belongs to ${ctx.signedInAs}, not your owner, so your owner's companies could not be checked. ` +
|
|
142
160
|
"Do not say which companies your owner belongs to; tell them you could not check and why.";
|
|
143
161
|
}
|
|
162
|
+
else if (ctx.signedIn) {
|
|
163
|
+
body =
|
|
164
|
+
`Your owner IS signed in to HQ on this computer, but their companies could not be checked: ${ctx.reason}. ` +
|
|
165
|
+
"Do not guess and do not say they have no company. Do not ask them to sign in or to run any command: " +
|
|
166
|
+
"say you could not read their account just now and that you will check again on their next message.";
|
|
167
|
+
}
|
|
144
168
|
else {
|
|
145
169
|
body =
|
|
146
170
|
`Your owner's companies could not be checked: ${ctx.reason}. ` +
|
package/dist/lib/bot/run.js
CHANGED
|
@@ -311,7 +311,14 @@ export async function runBot(deps) {
|
|
|
311
311
|
// by other people, who must not be handed the owner's companies.
|
|
312
312
|
if (deps.ownerContext && sessionScope === "dm") {
|
|
313
313
|
try {
|
|
314
|
-
|
|
314
|
+
const owner = await deps.ownerContext();
|
|
315
|
+
if (owner.status === "unavailable") {
|
|
316
|
+
log("warn", `owner context unavailable: ${owner.reason}${owner.detail ? ` (${owner.detail})` : ""}`);
|
|
317
|
+
}
|
|
318
|
+
else if (owner.status === "mismatch") {
|
|
319
|
+
log("warn", `owner context mismatch: signed in as ${owner.signedInAs}`);
|
|
320
|
+
}
|
|
321
|
+
prompt = `${ownerContextBlock(owner, config.agentUid)}\n\n${prompt}`;
|
|
315
322
|
}
|
|
316
323
|
catch (err) {
|
|
317
324
|
log("warn", `owner context unavailable: ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* HQ_COGNITO_CALLBACK_PORT — Loopback OAuth callback port
|
|
19
19
|
* HQ_VAULT_API_URL — vault-service API Gateway URL
|
|
20
20
|
*/
|
|
21
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
21
22
|
import * as fs from "fs";
|
|
22
23
|
import * as os from "os";
|
|
23
24
|
import * as path from "path";
|
|
@@ -496,19 +497,39 @@ export function describeCognitoTokenSource(opts = {}) {
|
|
|
496
497
|
* `HQ_MACHINE_TOKEN_STATE_DIR` instead. Restores the prior env afterwards.
|
|
497
498
|
*/
|
|
498
499
|
export async function withMachineTokenStateDir(fn, opts = {}) {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
500
|
+
return withTokenStateDirSection(async () => {
|
|
501
|
+
const home = opts.home ?? os.homedir();
|
|
502
|
+
const env = opts.env ?? process.env;
|
|
503
|
+
const prev = process.env.HQ_STATE_DIR;
|
|
504
|
+
process.env.HQ_STATE_DIR = machineTokenStateDir(home, env);
|
|
505
|
+
try {
|
|
506
|
+
return await fn();
|
|
507
|
+
}
|
|
508
|
+
finally {
|
|
509
|
+
if (prev === undefined)
|
|
510
|
+
delete process.env.HQ_STATE_DIR;
|
|
511
|
+
else
|
|
512
|
+
process.env.HQ_STATE_DIR = prev;
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* The token cache location (`HQ_STATE_DIR`) is process-wide, and a machine mint
|
|
518
|
+
* points it at the machine cache across an await. A person-session read in the
|
|
519
|
+
* same process while that mint is in flight would load the MACHINE tokens: a
|
|
520
|
+
* local bot's resident (machine identity for its own calls) checking its
|
|
521
|
+
* owner's sign-in at startup got the bot's token and "No person entity found".
|
|
522
|
+
* Every section that swaps or reads the cache location runs one at a time;
|
|
523
|
+
* nested sections (a person read falling back to a machine mint) run inline.
|
|
524
|
+
*/
|
|
525
|
+
const tokenStateDirSection = new AsyncLocalStorage();
|
|
526
|
+
let tokenStateDirTail = Promise.resolve();
|
|
527
|
+
function withTokenStateDirSection(fn) {
|
|
528
|
+
if (tokenStateDirSection.getStore())
|
|
529
|
+
return fn();
|
|
530
|
+
const run = tokenStateDirTail.then(() => tokenStateDirSection.run(true, fn));
|
|
531
|
+
tokenStateDirTail = run.catch(() => undefined);
|
|
532
|
+
return run;
|
|
512
533
|
}
|
|
513
534
|
async function ensureMachineTokens() {
|
|
514
535
|
return withMachineTokenStateDir(() => getValidMachineTokens(DEFAULT_COGNITO));
|
|
@@ -567,6 +588,11 @@ function markPersonSessionRejected(accessToken) {
|
|
|
567
588
|
* a browser.
|
|
568
589
|
*/
|
|
569
590
|
export async function ensureCognitoToken(options = {}) {
|
|
591
|
+
if (wantsMachineCognitoTokens(options))
|
|
592
|
+
return ensureCognitoTokenUnserialized(options);
|
|
593
|
+
return withTokenStateDirSection(() => ensureCognitoTokenUnserialized(options));
|
|
594
|
+
}
|
|
595
|
+
async function ensureCognitoTokenUnserialized(options) {
|
|
570
596
|
const interactive = options.interactive ?? true;
|
|
571
597
|
// Machine identities (company agents) mint sessions on demand via
|
|
572
598
|
// USER_PASSWORD_AUTH — no refresh-token dance, no browser. The vault API's
|
|
@@ -631,6 +657,11 @@ export async function ensureCognitoToken(options = {}) {
|
|
|
631
657
|
* identical to `ensureCognitoToken` there.
|
|
632
658
|
*/
|
|
633
659
|
export async function ensureCognitoIdToken(options = {}) {
|
|
660
|
+
if (wantsMachineCognitoTokens(options))
|
|
661
|
+
return ensureCognitoIdTokenUnserialized(options);
|
|
662
|
+
return withTokenStateDirSection(() => ensureCognitoIdTokenUnserialized(options));
|
|
663
|
+
}
|
|
664
|
+
async function ensureCognitoIdTokenUnserialized(options) {
|
|
634
665
|
const interactive = options.interactive ?? true;
|
|
635
666
|
if (wantsMachineCognitoTokens(options)) {
|
|
636
667
|
const machine = await ensureMachineTokensMaybeFallback(options);
|
|
@@ -704,6 +735,11 @@ export function buildVaultConfig(authToken) {
|
|
|
704
735
|
* reason string and can decide what to do next. Never opens a browser.
|
|
705
736
|
*/
|
|
706
737
|
export async function refreshCachedSession(options = {}) {
|
|
738
|
+
if (wantsMachineCognitoTokens(options))
|
|
739
|
+
return refreshCachedSessionUnserialized(options);
|
|
740
|
+
return withTokenStateDirSection(() => refreshCachedSessionUnserialized(options));
|
|
741
|
+
}
|
|
742
|
+
async function refreshCachedSessionUnserialized(options) {
|
|
707
743
|
// Machine identities have no refresh token; ensure a valid cached machine
|
|
708
744
|
// session without forcing a re-mint when the cache is already healthy.
|
|
709
745
|
// Tokens land under the dedicated machine/daemon state dir.
|