@grada-run/grada 0.40.8 → 0.40.10
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/bin/cli.js +1 -0
- package/package.json +1 -1
- package/src/commands/doctor.js +14 -0
- package/src/core/last-resort.js +132 -0
- package/src/core/telemetry.js +1 -1
package/bin/cli.js
CHANGED
package/package.json
CHANGED
package/src/commands/doctor.js
CHANGED
|
@@ -423,4 +423,18 @@ async function runDoctorMain(input = {}) {
|
|
|
423
423
|
} catch {
|
|
424
424
|
// The checks already printed; losing one event beats crashing.
|
|
425
425
|
}
|
|
426
|
+
|
|
427
|
+
// Red verdicts fail the command: runCommand maps {ok:false, exitCode}
|
|
428
|
+
// to the process exit, so `doctor && apply` gates correctly. The
|
|
429
|
+
// reason mirrors the telemetry failure props; failedChecks names the
|
|
430
|
+
// red cells for programmatic callers. Green (and warn-grade) runs
|
|
431
|
+
// intentionally return undefined, as before.
|
|
432
|
+
if (!success) {
|
|
433
|
+
return {
|
|
434
|
+
ok: false,
|
|
435
|
+
exitCode: 1,
|
|
436
|
+
reason: failureProps.reason,
|
|
437
|
+
failedChecks,
|
|
438
|
+
};
|
|
439
|
+
}
|
|
426
440
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
|
|
3
|
+
export const ISSUES_URL = 'https://github.com/grada-run/grada/issues';
|
|
4
|
+
|
|
5
|
+
// Last-resort crash backstop: unhandled rejections (e.g. the SDK
|
|
6
|
+
// credential chain exploding without a HOME) and uncaught exceptions
|
|
7
|
+
// (e.g. a future import-time throw) render one formatted report and
|
|
8
|
+
// exit 1 instead of dumping a raw stack — or printing nothing at all
|
|
9
|
+
// in --json mode. Registered via a side-effect import as the first
|
|
10
|
+
// line of bin/cli.js so it precedes every other listener (clack
|
|
11
|
+
// spinners register their own print-only rejection listener at
|
|
12
|
+
// start(); exiting first keeps their banner from double-surfacing).
|
|
13
|
+
//
|
|
14
|
+
// Dependency-free by design (node:* only): the crash backstop must
|
|
15
|
+
// never import the application modules that might be mid-failure.
|
|
16
|
+
// Output rule: a known environmental cause (the smithy no-HOME
|
|
17
|
+
// shape) prints guidance without a stack; everything else prints a
|
|
18
|
+
// one-liner, the stack (or String(reason) when there is none), and
|
|
19
|
+
// the report pointer. JSON mode puts the envelope on stdout and the
|
|
20
|
+
// human text on stderr so parsers stay safe. Standing requirement:
|
|
21
|
+
// never start a spinner on a --json/MCP path — the handler exits 1,
|
|
22
|
+
// and clack's exit listener prints "Canceled" to stdout for any
|
|
23
|
+
// spinner still active.
|
|
24
|
+
|
|
25
|
+
function firstLine(text) {
|
|
26
|
+
return String(text).split('\n')[0];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function reasonMessage(reason) {
|
|
30
|
+
if (reason instanceof Error) return reason.message || String(reason);
|
|
31
|
+
return String(reason);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function reasonDetail(reason) {
|
|
35
|
+
if (reason instanceof Error && typeof reason.stack === 'string') return reason.stack;
|
|
36
|
+
return String(reason);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Matches smithy's shared-config loader failing on an undeterminable
|
|
40
|
+
// home directory (both observed uv variants). Deliberately tight:
|
|
41
|
+
// near-misses fail open to the stack-bearing path below.
|
|
42
|
+
function isSmithyHomelessRejection(reason) {
|
|
43
|
+
if (!reason || typeof reason !== 'object') return false;
|
|
44
|
+
const codeMatch = reason.code === 'ERR_SYSTEM_ERROR' || reason.code === 'ENOENT';
|
|
45
|
+
if (!codeMatch) return false;
|
|
46
|
+
return /homedir|passwd/i.test(`${reason.syscall || ''} ${reason.message || ''}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const HOMELESS_GUIDANCE = 'AWS credentials unavailable: the home directory is undeterminable, so credential files cannot be loaded.';
|
|
50
|
+
|
|
51
|
+
export function formatLastResortOutput({ kind, reason, isJson }) {
|
|
52
|
+
const first = firstLine(reasonMessage(reason));
|
|
53
|
+
const detail = reasonDetail(reason);
|
|
54
|
+
let oneLiner;
|
|
55
|
+
let stderrText;
|
|
56
|
+
if (kind === 'unhandledRejection' && isSmithyHomelessRejection(reason)) {
|
|
57
|
+
oneLiner = HOMELESS_GUIDANCE;
|
|
58
|
+
stderrText = `✖ ${HOMELESS_GUIDANCE}\n`
|
|
59
|
+
+ ' Set the HOME environment variable to any directory (it is only used to locate optional config files), then configure credentials.\n';
|
|
60
|
+
} else if (kind === 'uncaughtException') {
|
|
61
|
+
oneLiner = `Grada crashed unexpectedly: ${first}`;
|
|
62
|
+
stderrText = `✖ ${oneLiner}\n${detail}\n Please report this: ${ISSUES_URL}\n`;
|
|
63
|
+
} else {
|
|
64
|
+
oneLiner = `Unexpected failure: ${first}`;
|
|
65
|
+
stderrText = `✖ ${oneLiner}\n${detail}\n Please report this: ${ISSUES_URL}\n`;
|
|
66
|
+
}
|
|
67
|
+
if (!isJson) return { stdoutText: null, stderrText };
|
|
68
|
+
const envelope = {
|
|
69
|
+
ok: false,
|
|
70
|
+
exitCode: 1,
|
|
71
|
+
error: oneLiner,
|
|
72
|
+
reason: kind === 'uncaughtException' ? 'uncaught-exception' : 'unhandled-rejection',
|
|
73
|
+
};
|
|
74
|
+
return { stdoutText: `${JSON.stringify(envelope)}\n`, stderrText };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createLastResortHandlers({
|
|
78
|
+
argv = process.argv,
|
|
79
|
+
exitFn = (code) => process.exit(code),
|
|
80
|
+
writeStdout = (text) => fs.writeSync(1, text),
|
|
81
|
+
writeStderr = (text) => fs.writeSync(2, text),
|
|
82
|
+
isTestEnv = () => Boolean(process.env.VITEST),
|
|
83
|
+
} = {}) {
|
|
84
|
+
const isJson = argv.includes('--json');
|
|
85
|
+
const handle = (kind) => (reason) => {
|
|
86
|
+
// Test workers keep native rejection behavior: a process.exit
|
|
87
|
+
// here would kill the worker mid-suite.
|
|
88
|
+
if (isTestEnv()) return;
|
|
89
|
+
try {
|
|
90
|
+
const { stdoutText, stderrText } = formatLastResortOutput({ kind, reason, isJson });
|
|
91
|
+
if (stdoutText !== null) {
|
|
92
|
+
try {
|
|
93
|
+
writeStdout(stdoutText);
|
|
94
|
+
} catch {
|
|
95
|
+
// A broken stdout must not swallow the stderr report.
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
writeStderr(stderrText);
|
|
99
|
+
} catch {
|
|
100
|
+
try {
|
|
101
|
+
writeStderr('✖ Grada crashed unexpectedly.\n');
|
|
102
|
+
} catch {
|
|
103
|
+
// A crash handler that crashes is the worst outcome;
|
|
104
|
+
// fall through to the exit below no matter what.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
exitFn(1);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
// MCP tool calls stub process.exit to throw InterceptedExit
|
|
111
|
+
// (duck-typed by name: this module must stay dependency-free):
|
|
112
|
+
// the tool's own error path carries the failure, so the
|
|
113
|
+
// backstop's job ends with the printed report. Anything else
|
|
114
|
+
// propagates — swallowing an unknown exit failure could let
|
|
115
|
+
// the loop drain to a silent exit 0.
|
|
116
|
+
if (error?.name !== 'InterceptedExit') throw error;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
unhandledRejection: handle('unhandledRejection'),
|
|
121
|
+
uncaughtException: handle('uncaughtException'),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function registerLastResortHandlers(options = {}) {
|
|
126
|
+
const handlers = createLastResortHandlers(options);
|
|
127
|
+
process.on('unhandledRejection', handlers.unhandledRejection);
|
|
128
|
+
process.on('uncaughtException', handlers.uncaughtException);
|
|
129
|
+
return handlers;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
registerLastResortHandlers();
|
package/src/core/telemetry.js
CHANGED
|
@@ -357,7 +357,7 @@ function telemetryPreference() {
|
|
|
357
357
|
export function writeTelemetryPreference(enabled, { configPath } = {}) {
|
|
358
358
|
const target = configPath ?? resolveConfigPath();
|
|
359
359
|
if (target === null) {
|
|
360
|
-
throw new Error('
|
|
360
|
+
throw new Error('the home directory is undeterminable (HOME is unset and no passwd entry exists).');
|
|
361
361
|
}
|
|
362
362
|
let current = {};
|
|
363
363
|
try {
|