@indigoai-us/hq-cli 5.130.0 → 5.131.0
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 +44 -1
- package/contracts/preflight/v1/fixtures.json +87 -0
- package/contracts/preflight/v1/schema.json +58 -0
- package/dist/command-catalog.generated.d.ts +27 -0
- package/dist/command-catalog.generated.js +34 -0
- package/dist/commands/dm.d.ts +2 -0
- package/dist/commands/dm.js +1 -1
- package/dist/commands/files.js +137 -9
- package/dist/commands/mesh.js +46 -12
- package/dist/lib/mesh/api.d.ts +2 -0
- package/dist/lib/mesh/api.js +15 -2
- package/dist/lib/mesh/live/daemon/doctor.d.ts +4 -0
- package/dist/lib/mesh/live/daemon/doctor.js +10 -2
- package/dist/lib/mesh/live/daemon/run.js +3 -0
- package/dist/lib/mesh/live/emit-client.d.ts +2 -0
- package/dist/lib/mesh/live/emit-client.js +15 -8
- package/dist/lib/mesh/live/emit.d.ts +9 -0
- package/dist/lib/mesh/live/emit.js +28 -2
- package/dist/lib/mesh/live/flush.js +9 -3
- package/dist/lib/search-index/index.js +22 -3
- package/dist/lib/work-context/preflight-contract.d.ts +28 -0
- package/dist/lib/work-context/preflight-contract.js +85 -0
- package/dist/main.js +11 -1
- package/dist/utils/cognito-session.d.ts +39 -0
- package/dist/utils/cognito-session.js +119 -3
- package/dist/utils/environmental-error.d.ts +18 -0
- package/dist/utils/environmental-error.js +240 -7
- package/dist/utils/expected-cli-error.d.ts +2 -0
- package/dist/utils/expected-cli-error.js +4 -0
- package/dist/utils/vault-api.d.ts +1 -0
- package/dist/utils/vault-api.js +11 -3
- package/package.json +3 -2
|
@@ -409,6 +409,115 @@ function decodeJwtPayload(token) {
|
|
|
409
409
|
return null;
|
|
410
410
|
}
|
|
411
411
|
}
|
|
412
|
+
/**
|
|
413
|
+
* Refresh wrote a different Cognito subject than the active cache.
|
|
414
|
+
* Callers must not fall through to browser login or machine mint — that
|
|
415
|
+
* would replace the operator session the refusal is protecting.
|
|
416
|
+
*/
|
|
417
|
+
export class IdentitySwapRefusal extends Error {
|
|
418
|
+
constructor(message) {
|
|
419
|
+
super(message);
|
|
420
|
+
this.name = "IdentitySwapRefusal";
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
export function isIdentitySwapRefusal(err) {
|
|
424
|
+
return err instanceof IdentitySwapRefusal;
|
|
425
|
+
}
|
|
426
|
+
/** `sub` plus `custom:entityUid` from an unverified JWT. Either may be absent. */
|
|
427
|
+
export function cognitoSubjectFromToken(token) {
|
|
428
|
+
const claims = decodeJwtPayload(token);
|
|
429
|
+
const sub = typeof claims?.sub === "string" && claims.sub.length > 0 ? claims.sub : null;
|
|
430
|
+
const entity = claims?.["custom:entityUid"];
|
|
431
|
+
const entityUid = typeof entity === "string" && entity.length > 0 ? entity : null;
|
|
432
|
+
return { sub, entityUid };
|
|
433
|
+
}
|
|
434
|
+
/** Stable subject key. Null when neither sub nor entity uid is present. */
|
|
435
|
+
export function cognitoSubjectKey(tokens) {
|
|
436
|
+
if (!tokens)
|
|
437
|
+
return null;
|
|
438
|
+
const fromId = cognitoSubjectFromToken(tokens.idToken);
|
|
439
|
+
const fromAccess = cognitoSubjectFromToken(tokens.accessToken);
|
|
440
|
+
const sub = fromId.sub ?? fromAccess.sub;
|
|
441
|
+
const entityUid = fromId.entityUid ?? fromAccess.entityUid;
|
|
442
|
+
if (!sub && !entityUid)
|
|
443
|
+
return null;
|
|
444
|
+
return `${sub ?? ""}|${entityUid ?? ""}`;
|
|
445
|
+
}
|
|
446
|
+
/** Doctor/status line. Claims only — never token material. */
|
|
447
|
+
export function formatCognitoSubject(tokens) {
|
|
448
|
+
if (!tokens)
|
|
449
|
+
return "sub=- entityUid=-";
|
|
450
|
+
const fromId = cognitoSubjectFromToken(tokens.idToken);
|
|
451
|
+
const fromAccess = cognitoSubjectFromToken(tokens.accessToken);
|
|
452
|
+
return `sub=${fromId.sub ?? fromAccess.sub ?? "-"} entityUid=${fromId.entityUid ?? fromAccess.entityUid ?? "-"}`;
|
|
453
|
+
}
|
|
454
|
+
function readTokenFile(file) {
|
|
455
|
+
try {
|
|
456
|
+
if (!fs.existsSync(file))
|
|
457
|
+
return null;
|
|
458
|
+
const raw = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
459
|
+
if (typeof raw.accessToken !== "string" || typeof raw.idToken !== "string")
|
|
460
|
+
return null;
|
|
461
|
+
return raw;
|
|
462
|
+
}
|
|
463
|
+
catch {
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Person cache as stored on disk for `env`. Does not consult hq-cloud's
|
|
469
|
+
* in-memory fallback, which can surface a different session when the file
|
|
470
|
+
* is missing or unreadable.
|
|
471
|
+
*/
|
|
472
|
+
export function readPersonTokenFile(home = os.homedir(), env = process.env) {
|
|
473
|
+
return readTokenFile(personTokenCacheFile(home, env));
|
|
474
|
+
}
|
|
475
|
+
function restoreTokenFile(file, before) {
|
|
476
|
+
if (before) {
|
|
477
|
+
const dir = path.dirname(file);
|
|
478
|
+
if (!fs.existsSync(dir))
|
|
479
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
480
|
+
const tmp = path.join(dir, `.cognito-tokens.json.restore.${process.pid}`);
|
|
481
|
+
fs.writeFileSync(tmp, before, { mode: 0o600 });
|
|
482
|
+
fs.renameSync(tmp, file);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (fs.existsSync(file))
|
|
486
|
+
fs.unlinkSync(file);
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Refresh the active person session and refuse a subject change.
|
|
490
|
+
*
|
|
491
|
+
* hq-cloud's `refreshTokens` saves as soon as Cognito responds, and on a
|
|
492
|
+
* cache miss or unreadable file it can refresh an in-memory session for a
|
|
493
|
+
* different subject (same app client). Snapshot the active file first; if
|
|
494
|
+
* the response subject differs, put the bytes back and leave them there.
|
|
495
|
+
*/
|
|
496
|
+
export async function refreshActivePersonTokens(cached, env = process.env) {
|
|
497
|
+
const file = personTokenCacheFile(os.homedir(), env);
|
|
498
|
+
const onDisk = readTokenFile(file);
|
|
499
|
+
const diskKey = cognitoSubjectKey(onDisk);
|
|
500
|
+
const cachedKey = cognitoSubjectKey(cached);
|
|
501
|
+
if (diskKey && cachedKey && diskKey !== cachedKey) {
|
|
502
|
+
const message = `refusing token refresh: cached subject ${formatCognitoSubject(cached)} ` +
|
|
503
|
+
`does not match active subject ${formatCognitoSubject(onDisk)}; token file left unchanged`;
|
|
504
|
+
console.error(message);
|
|
505
|
+
throw new IdentitySwapRefusal(message);
|
|
506
|
+
}
|
|
507
|
+
const activeTokens = diskKey && cachedKey && diskKey === cachedKey ? onDisk : cached;
|
|
508
|
+
const activeKey = cognitoSubjectKey(activeTokens);
|
|
509
|
+
const before = fs.existsSync(file) ? fs.readFileSync(file) : null;
|
|
510
|
+
const refreshed = await refreshTokens(DEFAULT_COGNITO, activeTokens.refreshToken, activeTokens.accessToken);
|
|
511
|
+
const nextKey = cognitoSubjectKey(refreshed);
|
|
512
|
+
if (activeKey && nextKey && nextKey !== activeKey) {
|
|
513
|
+
restoreTokenFile(file, before);
|
|
514
|
+
const message = `refusing token refresh: refreshed subject ${formatCognitoSubject(refreshed)} ` +
|
|
515
|
+
`does not match active subject ${formatCognitoSubject(activeTokens)}; token file left unchanged`;
|
|
516
|
+
console.error(message);
|
|
517
|
+
throw new IdentitySwapRefusal(message);
|
|
518
|
+
}
|
|
519
|
+
return refreshed;
|
|
520
|
+
}
|
|
412
521
|
/** Derive actor kind from an ID token's custom:entity* claims (no verify). */
|
|
413
522
|
export function actorKindFromIdToken(idToken) {
|
|
414
523
|
const claims = decodeJwtPayload(idToken);
|
|
@@ -616,10 +725,12 @@ async function ensureCognitoTokenUnserialized(options) {
|
|
|
616
725
|
if (interactive) {
|
|
617
726
|
console.error(chalk.dim(" Refreshing expiring HQ session..."));
|
|
618
727
|
}
|
|
619
|
-
const refreshed = await
|
|
728
|
+
const refreshed = await refreshActivePersonTokens(cached);
|
|
620
729
|
return refreshed.accessToken;
|
|
621
730
|
}
|
|
622
731
|
catch (err) {
|
|
732
|
+
if (isIdentitySwapRefusal(err))
|
|
733
|
+
throw err;
|
|
623
734
|
// Transient/network/server errors stay person errors — never mint machine.
|
|
624
735
|
if (!isDefinitivePersonAuthRejection(err)) {
|
|
625
736
|
throw err;
|
|
@@ -676,10 +787,12 @@ async function ensureCognitoIdTokenUnserialized(options) {
|
|
|
676
787
|
if (interactive) {
|
|
677
788
|
console.error(chalk.dim(" Refreshing expiring HQ session..."));
|
|
678
789
|
}
|
|
679
|
-
const refreshed = await
|
|
790
|
+
const refreshed = await refreshActivePersonTokens(cached);
|
|
680
791
|
return refreshed.idToken;
|
|
681
792
|
}
|
|
682
793
|
catch (err) {
|
|
794
|
+
if (isIdentitySwapRefusal(err))
|
|
795
|
+
throw err;
|
|
683
796
|
if (!isDefinitivePersonAuthRejection(err)) {
|
|
684
797
|
throw err;
|
|
685
798
|
}
|
|
@@ -778,10 +891,13 @@ async function refreshCachedSessionUnserialized(options) {
|
|
|
778
891
|
return { refreshed: true };
|
|
779
892
|
}
|
|
780
893
|
try {
|
|
781
|
-
await
|
|
894
|
+
await refreshActivePersonTokens(cached);
|
|
782
895
|
return { refreshed: true };
|
|
783
896
|
}
|
|
784
897
|
catch (err) {
|
|
898
|
+
if (isIdentitySwapRefusal(err)) {
|
|
899
|
+
return { refreshed: false, reason: err.message };
|
|
900
|
+
}
|
|
785
901
|
// Transient/network/server refresh errors stay person failures.
|
|
786
902
|
if (!isDefinitivePersonAuthRejection(err)) {
|
|
787
903
|
return {
|
|
@@ -7,4 +7,22 @@
|
|
|
7
7
|
* result means "this is a normal error; handle it as usual (capture to Sentry)".
|
|
8
8
|
*/
|
|
9
9
|
export declare function environmentalFsErrorMessage(err: unknown): string | null;
|
|
10
|
+
export type FilesystemErrorCaptureContext = {
|
|
11
|
+
fingerprint: ["fs-error", string, string, string];
|
|
12
|
+
tags: Record<string, string>;
|
|
13
|
+
extra: {
|
|
14
|
+
fs_path: string;
|
|
15
|
+
fs_reason: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
/** Return only the registered command path, never positional user data. */
|
|
19
|
+
export declare function hqCommand(argv?: readonly string[]): string;
|
|
20
|
+
/**
|
|
21
|
+
* Build stable Sentry context for known OS filesystem refusals. The failure is
|
|
22
|
+
* intentionally still captured: this is instrumentation for the OneDrive /
|
|
23
|
+
* antivirus / path-length hypotheses, not an expected-error suppression.
|
|
24
|
+
*/
|
|
25
|
+
export declare function filesystemErrorCaptureContext(err: unknown, argv?: readonly string[]): FilesystemErrorCaptureContext | null;
|
|
26
|
+
/** Plain operator guidance for a captured filesystem failure. */
|
|
27
|
+
export declare function filesystemErrorMessage(err: unknown): string | null;
|
|
10
28
|
//# sourceMappingURL=environmental-error.d.ts.map
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
// src/utils/environmental-error.ts
|
|
2
2
|
//
|
|
3
3
|
// Classify errors that stem from the user's machine/filesystem state rather
|
|
4
|
-
// than an HQ code defect.
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// full disk floods the issue tracker with identical, unfixable crash reports.
|
|
4
|
+
// than an HQ code defect. Full-disk conditions have a known remedy and remain
|
|
5
|
+
// quiet; permission and file-lock conditions remain captured with bounded
|
|
6
|
+
// attribution so the next occurrence can identify the real cause.
|
|
8
7
|
//
|
|
9
8
|
// HQ-CLI-2: `hq reindex` hit ENOSPC in the operation-lock temp-file write
|
|
10
9
|
// (`fs.openSync` → "ENOSPC: no space left on device, open") and the raw error
|
|
11
10
|
// propagated uncaught to the CLI's top-level handler, which captured it to
|
|
12
11
|
// Sentry and exited silently — 5 stack-trace crashes in 6 seconds, with no
|
|
13
12
|
// message telling the user their disk was full.
|
|
13
|
+
import * as fs from "node:fs";
|
|
14
|
+
import * as path from "node:path";
|
|
15
|
+
import { COMMAND_CATALOG } from "../command-catalog.generated.js";
|
|
16
|
+
import { CLI_VERSION } from "../cli-version.js";
|
|
17
|
+
import { redactErrorText } from "./redact-error-text.js";
|
|
14
18
|
/**
|
|
15
19
|
* Node errno codes for "the filesystem cannot accept this write" — purely
|
|
16
20
|
* environmental, never a code bug. Mapped to the message shown to the user.
|
|
@@ -20,6 +24,24 @@ const ENVIRONMENTAL_FS_CODES = {
|
|
|
20
24
|
EDQUOT: "Disk quota exceeded. Free up space (or raise your quota) and try again.",
|
|
21
25
|
EROFS: "The filesystem is read-only, so HQ can't write here. Check the mount/permissions and try again.",
|
|
22
26
|
};
|
|
27
|
+
const MAX_CAUSE_DEPTH = 4;
|
|
28
|
+
function errorRecord(value) {
|
|
29
|
+
return value !== null && typeof value === "object" ? value : null;
|
|
30
|
+
}
|
|
31
|
+
function findErrorWithCode(err, codes) {
|
|
32
|
+
const seen = new Set();
|
|
33
|
+
let current = err;
|
|
34
|
+
for (let depth = 0; depth <= MAX_CAUSE_DEPTH; depth += 1) {
|
|
35
|
+
const record = errorRecord(current);
|
|
36
|
+
if (!record || seen.has(record))
|
|
37
|
+
return null;
|
|
38
|
+
seen.add(record);
|
|
39
|
+
if (typeof record.code === "string" && codes.has(record.code))
|
|
40
|
+
return record;
|
|
41
|
+
current = record.cause;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
23
45
|
/**
|
|
24
46
|
* If `err` is an environmental disk/quota/read-only filesystem error, return a
|
|
25
47
|
* short, user-facing message explaining it; otherwise return `null`.
|
|
@@ -29,9 +51,220 @@ const ENVIRONMENTAL_FS_CODES = {
|
|
|
29
51
|
* result means "this is a normal error; handle it as usual (capture to Sentry)".
|
|
30
52
|
*/
|
|
31
53
|
export function environmentalFsErrorMessage(err) {
|
|
32
|
-
const
|
|
33
|
-
|
|
54
|
+
const record = findErrorWithCode(err, new Set(Object.keys(ENVIRONMENTAL_FS_CODES)));
|
|
55
|
+
return record && typeof record.code === "string"
|
|
56
|
+
? ENVIRONMENTAL_FS_CODES[record.code] ?? null
|
|
57
|
+
: null;
|
|
58
|
+
}
|
|
59
|
+
const CAPTURED_FS_CODES = new Set(["EPERM", "EACCES", "EBUSY", "UNKNOWN"]);
|
|
60
|
+
const CAPTURED_FS_SYSCALLS = new Set(["open", "symlink", "rename", "unlink"]);
|
|
61
|
+
function stringField(value) {
|
|
62
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
63
|
+
}
|
|
64
|
+
function pathFromMessage(message, syscall) {
|
|
65
|
+
const text = stringField(message);
|
|
66
|
+
if (!text)
|
|
67
|
+
return null;
|
|
68
|
+
const escapedSyscall = syscall.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
69
|
+
const match = new RegExp(`${escapedSyscall}\\s+['"]([^'"]+)['"]`, "i").exec(text);
|
|
70
|
+
return match?.[1] ?? null;
|
|
71
|
+
}
|
|
72
|
+
function findFilesystemError(err) {
|
|
73
|
+
const seen = new Set();
|
|
74
|
+
let current = err;
|
|
75
|
+
for (let depth = 0; depth <= MAX_CAUSE_DEPTH; depth += 1) {
|
|
76
|
+
const record = errorRecord(current);
|
|
77
|
+
if (!record || seen.has(record))
|
|
78
|
+
return null;
|
|
79
|
+
seen.add(record);
|
|
80
|
+
const code = stringField(record.code);
|
|
81
|
+
const syscall = stringField(record.syscall);
|
|
82
|
+
if (code && syscall && CAPTURED_FS_CODES.has(code) && CAPTURED_FS_SYSCALLS.has(syscall)) {
|
|
83
|
+
const rawPath = stringField(record.path) ??
|
|
84
|
+
stringField(record.dest) ??
|
|
85
|
+
pathFromMessage(record.message, syscall) ??
|
|
86
|
+
"unknown";
|
|
87
|
+
return { record, code, syscall, rawPath };
|
|
88
|
+
}
|
|
89
|
+
current = record.cause;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
function canonicalPath(rawPath) {
|
|
94
|
+
if (rawPath === "unknown")
|
|
95
|
+
return "";
|
|
96
|
+
const resolved = path.resolve(rawPath);
|
|
97
|
+
try {
|
|
98
|
+
return path.normalize(requireRealpath(resolved));
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// The failed path may not exist. Resolving its parent still canonicalizes
|
|
102
|
+
// symlinked HQ roots without turning missing-file attribution into a throw.
|
|
103
|
+
const parent = path.dirname(resolved);
|
|
104
|
+
try {
|
|
105
|
+
return path.join(requireRealpath(parent), path.basename(resolved));
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return resolved;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function requireRealpath(candidate) {
|
|
113
|
+
// Keep the filesystem read behind one tiny seam so the classifier remains
|
|
114
|
+
// safe when the failed target has already disappeared.
|
|
115
|
+
return fs.realpathSync.native(candidate);
|
|
116
|
+
}
|
|
117
|
+
function pathSegments(value) {
|
|
118
|
+
return value
|
|
119
|
+
.replaceAll("\\", "/")
|
|
120
|
+
.split("/")
|
|
121
|
+
.filter(Boolean)
|
|
122
|
+
.map((segment) => segment.toLowerCase());
|
|
123
|
+
}
|
|
124
|
+
function pathKind(rawPath, resolvedPath) {
|
|
125
|
+
if (rawPath === "unknown")
|
|
126
|
+
return "other";
|
|
127
|
+
const segments = pathSegments(`${resolvedPath}/${rawPath}`);
|
|
128
|
+
if (segments.includes("node_modules") || segments.includes(".pnpm"))
|
|
129
|
+
return "install-tree";
|
|
130
|
+
if (segments.includes("worktree") || segments.includes("worktrees"))
|
|
131
|
+
return "worktree";
|
|
132
|
+
if (segments.includes("companies"))
|
|
133
|
+
return "companies";
|
|
134
|
+
if (segments.includes("workspace"))
|
|
135
|
+
return "workspace";
|
|
136
|
+
if (segments.includes("hq"))
|
|
137
|
+
return "hq-root";
|
|
138
|
+
return "other";
|
|
139
|
+
}
|
|
140
|
+
function syncedFolder(resolvedPath) {
|
|
141
|
+
if (!resolvedPath)
|
|
142
|
+
return "none";
|
|
143
|
+
const segments = pathSegments(resolvedPath);
|
|
144
|
+
// iCloud is identified from canonical path components, not a display-name
|
|
145
|
+
// substring: Desktop/Documents aliases can point into this container.
|
|
146
|
+
for (let i = 0; i + 1 < segments.length; i += 1) {
|
|
147
|
+
if (segments[i] === "library" && segments[i + 1] === "mobile documents")
|
|
148
|
+
return "icloud";
|
|
149
|
+
}
|
|
150
|
+
if (segments.some((segment) => segment === "onedrive" || segment.startsWith("onedrive -")))
|
|
151
|
+
return "onedrive";
|
|
152
|
+
if (segments.includes("dropbox"))
|
|
153
|
+
return "dropbox";
|
|
154
|
+
if (segments.some((segment) => segment === "google drive" || segment === "google-drive" || segment === "googledrive")) {
|
|
155
|
+
return "google-drive";
|
|
156
|
+
}
|
|
157
|
+
return "none";
|
|
158
|
+
}
|
|
159
|
+
function redactedPath(rawPath) {
|
|
160
|
+
if (rawPath === "unknown")
|
|
161
|
+
return "unknown";
|
|
162
|
+
const segments = rawPath.replaceAll("\\", "/").split("/").filter(Boolean);
|
|
163
|
+
const tail = segments.slice(-2).join("/");
|
|
164
|
+
return redactErrorText(tail) || "unknown";
|
|
165
|
+
}
|
|
166
|
+
function fileAttributes(record, rawPath) {
|
|
167
|
+
const metadata = [record.fileAttributes, record.attributes]
|
|
168
|
+
.map(stringField)
|
|
169
|
+
.filter((value) => value !== null)
|
|
170
|
+
.join(" ")
|
|
171
|
+
.toLowerCase();
|
|
172
|
+
if (metadata.includes("offline") || record.offline === true)
|
|
173
|
+
return "offline";
|
|
174
|
+
if (metadata.includes("readonly") || metadata.includes("read-only") || record.readonly === true)
|
|
175
|
+
return "readonly";
|
|
176
|
+
const leaf = rawPath.replaceAll("\\", "/").split("/").filter(Boolean).at(-1) ?? rawPath;
|
|
177
|
+
if (metadata.includes("hidden") || record.hidden === true || leaf.startsWith("."))
|
|
178
|
+
return "hidden";
|
|
179
|
+
return "none";
|
|
180
|
+
}
|
|
181
|
+
function findRegisteredCommand(commands, token) {
|
|
182
|
+
return commands.find((command) => command.name === token || command.aliases.includes(token)) ?? null;
|
|
183
|
+
}
|
|
184
|
+
function optionTakesValue(command, token) {
|
|
185
|
+
const optionName = token.split("=", 1)[0];
|
|
186
|
+
return command.options.some((option) => {
|
|
187
|
+
const names = option.flags
|
|
188
|
+
.split(",")
|
|
189
|
+
.map((flag) => flag.trim().split(/\s+/, 1)[0])
|
|
190
|
+
.filter((name) => Boolean(name));
|
|
191
|
+
return names.includes(optionName) && /(?:<[^>]+>|\[[^\]]+\])/.test(option.flags);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/** Return only the registered command path, never positional user data. */
|
|
195
|
+
export function hqCommand(argv = process.argv) {
|
|
196
|
+
const commandPath = [];
|
|
197
|
+
let commands = COMMAND_CATALOG;
|
|
198
|
+
let currentCommand = null;
|
|
199
|
+
for (let index = 2; index < argv.length; index += 1) {
|
|
200
|
+
const token = argv[index];
|
|
201
|
+
if (!token || token === "--")
|
|
202
|
+
break;
|
|
203
|
+
if (token.startsWith("-")) {
|
|
204
|
+
if (!token.includes("=") && currentCommand && optionTakesValue(currentCommand, token)) {
|
|
205
|
+
index += 1;
|
|
206
|
+
}
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const command = findRegisteredCommand(commands, token);
|
|
210
|
+
if (!command)
|
|
211
|
+
break;
|
|
212
|
+
commandPath.push(command.name);
|
|
213
|
+
currentCommand = command;
|
|
214
|
+
commands = command.subcommands;
|
|
215
|
+
}
|
|
216
|
+
return commandPath.join(" ") || "unknown";
|
|
217
|
+
}
|
|
218
|
+
function pathLengthBucket(rawPath) {
|
|
219
|
+
if (rawPath.length < 200)
|
|
220
|
+
return "<200";
|
|
221
|
+
if (rawPath.length <= 260)
|
|
222
|
+
return "200-260";
|
|
223
|
+
return ">260";
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Build stable Sentry context for known OS filesystem refusals. The failure is
|
|
227
|
+
* intentionally still captured: this is instrumentation for the OneDrive /
|
|
228
|
+
* antivirus / path-length hypotheses, not an expected-error suppression.
|
|
229
|
+
*/
|
|
230
|
+
export function filesystemErrorCaptureContext(err, argv = process.argv) {
|
|
231
|
+
const details = findFilesystemError(err);
|
|
232
|
+
if (!details)
|
|
233
|
+
return null;
|
|
234
|
+
const resolvedPath = canonicalPath(details.rawPath);
|
|
235
|
+
const safePath = redactedPath(details.rawPath);
|
|
236
|
+
const command = hqCommand(argv);
|
|
237
|
+
return {
|
|
238
|
+
fingerprint: ["fs-error", details.code, details.syscall, command],
|
|
239
|
+
tags: {
|
|
240
|
+
fs_code: details.code,
|
|
241
|
+
fs_syscall: details.syscall,
|
|
242
|
+
path_kind: pathKind(details.rawPath, resolvedPath),
|
|
243
|
+
synced_folder: syncedFolder(resolvedPath),
|
|
244
|
+
path_len_bucket: pathLengthBucket(details.rawPath),
|
|
245
|
+
file_attributes: fileAttributes(details.record, details.rawPath),
|
|
246
|
+
hq_command: command,
|
|
247
|
+
platform: process.platform,
|
|
248
|
+
hq_cli_version: CLI_VERSION,
|
|
249
|
+
},
|
|
250
|
+
extra: {
|
|
251
|
+
fs_path: safePath,
|
|
252
|
+
fs_reason: redactErrorText(`${details.code} ${details.syscall} failed at ${safePath}`),
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** Plain operator guidance for a captured filesystem failure. */
|
|
257
|
+
export function filesystemErrorMessage(err) {
|
|
258
|
+
const details = findFilesystemError(err);
|
|
259
|
+
if (!details)
|
|
34
260
|
return null;
|
|
35
|
-
|
|
261
|
+
const resolvedPath = canonicalPath(details.rawPath);
|
|
262
|
+
const safePath = redactedPath(details.rawPath);
|
|
263
|
+
const synced = syncedFolder(resolvedPath);
|
|
264
|
+
const permissionDenied = details.code === "EACCES" || details.code === "EPERM";
|
|
265
|
+
const remedy = permissionDenied && process.platform !== "win32" && synced === "none"
|
|
266
|
+
? "Check file and folder permissions, close the file, then retry."
|
|
267
|
+
: "Close the file or exclude its folder from cloud sync; if it persists, check permissions, then retry.";
|
|
268
|
+
return `Filesystem operation ${details.syscall} failed (${details.code}) at ${safePath}. ${remedy}`;
|
|
36
269
|
}
|
|
37
270
|
//# sourceMappingURL=environmental-error.js.map
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
export interface ExpectedUserError extends Error {
|
|
6
6
|
expected: true;
|
|
7
7
|
}
|
|
8
|
+
/** Stamp an existing Error with the boundary's caller-owned disposition. */
|
|
9
|
+
export declare function markExpectedUserError<T extends Error>(err: T): T & ExpectedUserError;
|
|
8
10
|
/**
|
|
9
11
|
* True when `err` is an Error explicitly marked `expected === true`. A non-null
|
|
10
12
|
* result means the top-level handler should print `err.message` and skip Sentry
|
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
// ("Only a company owner can approve or reject queued integration writes"); the
|
|
17
17
|
// thrown error propagated to the top-level handler, which captured it to Sentry
|
|
18
18
|
// as an error-level crash and printed nothing to the user.
|
|
19
|
+
/** Stamp an existing Error with the boundary's caller-owned disposition. */
|
|
20
|
+
export function markExpectedUserError(err) {
|
|
21
|
+
return Object.assign(err, { expected: true });
|
|
22
|
+
}
|
|
19
23
|
/**
|
|
20
24
|
* True when `err` is an Error explicitly marked `expected === true`. A non-null
|
|
21
25
|
* result means the top-level handler should print `err.message` and skip Sentry
|
package/dist/utils/vault-api.js
CHANGED
|
@@ -6,6 +6,8 @@ import { CompanySelectionError } from './company-selection-error.js';
|
|
|
6
6
|
import { recordPlanLimitStatus } from '../lib/plan-limit-nag.js';
|
|
7
7
|
import { networkTransportErrorCode } from './network-transport-error.js';
|
|
8
8
|
import { planGateErrorFromResponse } from './plan-gate-error.js';
|
|
9
|
+
import { markExpectedUserError } from './expected-cli-error.js';
|
|
10
|
+
import { redactErrorText } from './redact-error-text.js';
|
|
9
11
|
/**
|
|
10
12
|
* Identity / company resolution lookups must never hang forever. These small
|
|
11
13
|
* GETs run BEFORE a command does its real work (e.g. `hq secrets env` resolves
|
|
@@ -193,6 +195,7 @@ export async function vaultApiFetch(opts) {
|
|
|
193
195
|
// skew on EVERY authenticated request, not only health heartbeats.
|
|
194
196
|
// Fixed build constant, never caller-supplied; attribution only.
|
|
195
197
|
'x-hq-client-version': CLI_VERSION,
|
|
198
|
+
...opts.headers,
|
|
196
199
|
},
|
|
197
200
|
body: opts.body ? JSON.stringify(opts.body) : undefined,
|
|
198
201
|
signal: opts.signal,
|
|
@@ -342,6 +345,9 @@ function formatSlugLookupReason(res, body) {
|
|
|
342
345
|
return fromStatus;
|
|
343
346
|
return `HTTP ${res.status}`;
|
|
344
347
|
}
|
|
348
|
+
function redactedSlugLookupMessage(message) {
|
|
349
|
+
return redactErrorText(message) || 'Company lookup failed.';
|
|
350
|
+
}
|
|
345
351
|
function isTransientSlugLookupStatus(status) {
|
|
346
352
|
return status === 408 || status === 425 || status === 429 || status >= 500;
|
|
347
353
|
}
|
|
@@ -475,14 +481,15 @@ async function resolveCompanyUid(token, ref) {
|
|
|
475
481
|
// failure — treating it as not-found sent owners to check a slug that
|
|
476
482
|
// was fine (Shepherd #2306).
|
|
477
483
|
if (res.status === 404 && mine.kind === "miss") {
|
|
478
|
-
throw Object.assign(new Error(`Company slug '${ref}' was not found. Check the slug or run \`hq companies list\`.`), { expected: true });
|
|
484
|
+
throw Object.assign(new Error(redactedSlugLookupMessage(`Company slug '${ref}' was not found. Check the slug or run \`hq companies list\`.`)), { expected: true });
|
|
479
485
|
}
|
|
480
486
|
const reason = formatSlugLookupReason(res, body);
|
|
481
487
|
const retryable = mine.kind === "transient" || isTransientSlugLookupStatus(res.status);
|
|
482
488
|
const hint = retryable
|
|
483
489
|
? " This is a temporary lookup failure, not a missing company. Retry the command."
|
|
484
490
|
: "";
|
|
485
|
-
|
|
491
|
+
const error = new Error(redactedSlugLookupMessage(`Failed to resolve company slug '${ref}': ${reason}.${hint}`));
|
|
492
|
+
throw retryable ? error : markExpectedUserError(error);
|
|
486
493
|
}
|
|
487
494
|
const data = (await res.json());
|
|
488
495
|
return data.entity.uid;
|
|
@@ -494,7 +501,8 @@ async function resolveCompanyFromMemberships(token) {
|
|
|
494
501
|
signal: AbortSignal.timeout(identityLookupTimeoutMs()),
|
|
495
502
|
});
|
|
496
503
|
if (!res.ok) {
|
|
497
|
-
|
|
504
|
+
const error = new Error("Failed to fetch memberships — run `hq login` and try again");
|
|
505
|
+
throw res.status === 401 ? markExpectedUserError(error) : error;
|
|
498
506
|
}
|
|
499
507
|
const data = (await res.json());
|
|
500
508
|
const active = data.memberships.filter((m) => m.status === 'active');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@indigoai-us/hq-cli",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.131.0",
|
|
4
4
|
"description": "HQ by Indigo management CLI \u2014 modules and cloud sync",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"assets/lanes-hooks",
|
|
18
18
|
"templates",
|
|
19
19
|
"CHANGELOG.md",
|
|
20
|
-
"README.md"
|
|
20
|
+
"README.md",
|
|
21
|
+
"contracts"
|
|
21
22
|
],
|
|
22
23
|
"scripts": {
|
|
23
24
|
"build": "node scripts/generate-dsn.mjs && tsc && node scripts/generate-command-catalog.mjs --check && node scripts/copy-schemas.mjs && node scripts/chmod-bins.mjs",
|