@grada-run/grada 0.40.7 → 0.40.8
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/package.json +1 -1
- package/src/commands/mcp.js +17 -2
- package/src/core/homedir.js +18 -0
- package/src/core/telemetry.js +40 -20
- package/src/utils/bedrock-catalog.js +18 -6
package/package.json
CHANGED
package/src/commands/mcp.js
CHANGED
|
@@ -8,6 +8,7 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
|
8
8
|
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
9
9
|
import { z } from 'zod';
|
|
10
10
|
import { getCliVersion, trackSuccess, setActiveCommandName, resetActiveCommandName } from '../core/telemetry.js';
|
|
11
|
+
import { safeHomedir } from '../core/homedir.js';
|
|
11
12
|
import { parseFlags, normalizeOptions, normalizeArgv } from '../utils/args.js';
|
|
12
13
|
import { failCommand } from '../utils/command.js';
|
|
13
14
|
import { runStatus } from './status.js';
|
|
@@ -451,7 +452,10 @@ export const MCP_INSTALL_EDITORS = ['windsurf', 'zed', 'cursor', 'vscode', 'clau
|
|
|
451
452
|
|
|
452
453
|
export const MCP_SERVER_CONFIG = { command: 'npx', args: ['grada-run', 'mcp'] };
|
|
453
454
|
|
|
454
|
-
export function mcpInstallTarget(editor, homeDir =
|
|
455
|
+
export function mcpInstallTarget(editor, homeDir = safeHomedir(), platform = os.platform()) {
|
|
456
|
+
// No home, no target: null reads as "unresolvable" like an unknown
|
|
457
|
+
// editor, and installMcpConfig below names the cause precisely.
|
|
458
|
+
if (homeDir === null || homeDir === undefined) return null;
|
|
455
459
|
switch (editor) {
|
|
456
460
|
case 'windsurf':
|
|
457
461
|
return { path: path.join(homeDir, '.codeium', 'windsurf', 'mcp_config.json'), key: 'mcpServers' };
|
|
@@ -499,10 +503,21 @@ function isSameServerConfig(entry) {
|
|
|
499
503
|
// preserving every existing key and server. `homeDir` is injectable so
|
|
500
504
|
// tests never touch the real home directory. Never throws and never
|
|
501
505
|
// exits: all outcomes are structured `{ ok, ... }` results.
|
|
502
|
-
export function installMcpConfig(editor, { homeDir =
|
|
506
|
+
export function installMcpConfig(editor, { homeDir = safeHomedir(), platform = os.platform() } = {}) {
|
|
503
507
|
const normalized = typeof editor === 'string' ? editor.trim().toLowerCase() : '';
|
|
504
508
|
const target = normalized ? mcpInstallTarget(normalized, homeDir, platform) : null;
|
|
505
509
|
if (!target) {
|
|
510
|
+
// A supported editor that resolves to no target means HOME is
|
|
511
|
+
// undeterminable (usage errors keep their own result below).
|
|
512
|
+
if (normalized && MCP_INSTALL_EDITORS.includes(normalized)) {
|
|
513
|
+
return {
|
|
514
|
+
ok: false,
|
|
515
|
+
reason: 'home-undeterminable',
|
|
516
|
+
errorCode: 'MCP_INSTALL_HOME_UNDETERMINABLE',
|
|
517
|
+
editor: normalized,
|
|
518
|
+
message: '\n✖ Could not determine your home directory (HOME is unset and no passwd entry exists). Set HOME and retry.',
|
|
519
|
+
};
|
|
520
|
+
}
|
|
506
521
|
return {
|
|
507
522
|
ok: false,
|
|
508
523
|
reason: normalized ? 'unsupported-editor' : 'missing-editor',
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
|
|
3
|
+
// Null-safe home directory resolution: os.homedir() throws when HOME is
|
|
4
|
+
// unset and no passwd entry exists (minimal containers, `env -i`), which
|
|
5
|
+
// used to kill the whole CLI at import time via add.js's module-scope
|
|
6
|
+
// catalog load. Every HOME-dependent path goes through here; callers
|
|
7
|
+
// branch on null with their own fallback (bundled catalog, ephemeral
|
|
8
|
+
// salt, fingerprint identity — or a loud failure where HOME is a genuine
|
|
9
|
+
// requirement). `homedirFn` is injectable so tests can pin both branches
|
|
10
|
+
// without stubbing the os module.
|
|
11
|
+
export function safeHomedir(homedirFn = () => os.homedir()) {
|
|
12
|
+
try {
|
|
13
|
+
const home = homedirFn();
|
|
14
|
+
return typeof home === 'string' && home.trim() !== '' ? home : null;
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/core/telemetry.js
CHANGED
|
@@ -3,6 +3,7 @@ import fs from 'fs';
|
|
|
3
3
|
import os from 'os';
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import { fileURLToPath } from 'url';
|
|
6
|
+
import { safeHomedir } from './homedir.js';
|
|
6
7
|
|
|
7
8
|
const TELEMETRY_ENDPOINT = 'https://eu.i.posthog.com/capture/';
|
|
8
9
|
const POSTHOG_API_KEY = 'phc_o2wgA3jVT9rVDiGSDzFAR42zZeiVGhhCY53HXVHUcYGT';
|
|
@@ -164,11 +165,13 @@ export function isRunningUnderVitest(env = process.env) {
|
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
export function defaultTelemetryIdPath() {
|
|
167
|
-
|
|
168
|
+
const home = safeHomedir();
|
|
169
|
+
return home === null ? null : path.join(home, '.grada', 'telemetry-id');
|
|
168
170
|
}
|
|
169
171
|
|
|
170
172
|
export function legacyTelemetryIdPath() {
|
|
171
|
-
|
|
173
|
+
const home = safeHomedir();
|
|
174
|
+
return home === null ? null : path.join(home, '.deploy-stack', 'telemetry-id');
|
|
172
175
|
}
|
|
173
176
|
|
|
174
177
|
// Migrates a legacy telemetry identity forward: when the new path is
|
|
@@ -176,6 +179,7 @@ export function legacyTelemetryIdPath() {
|
|
|
176
179
|
// new file so the stable distinct ID survives the rebrand. Returns true
|
|
177
180
|
// when a migration happened. Never throws.
|
|
178
181
|
export function migrateLegacyTelemetryId(newPath, legacyPath) {
|
|
182
|
+
if (!newPath || !legacyPath) return false;
|
|
179
183
|
try {
|
|
180
184
|
if (fs.existsSync(newPath)) return false;
|
|
181
185
|
const raw = fs.readFileSync(legacyPath, 'utf-8').trim();
|
|
@@ -233,15 +237,22 @@ export function resolveDistinctId({ ciProvider = detectCiProvider(), testEnv = i
|
|
|
233
237
|
resolved = fingerprintDistinctId();
|
|
234
238
|
} else {
|
|
235
239
|
const idPath = defaultTelemetryIdPath();
|
|
236
|
-
|
|
237
|
-
|
|
240
|
+
if (idPath === null) {
|
|
241
|
+
// Nowhere to persist an identity: behave like an ephemeral
|
|
242
|
+
// runner rather than throwing past trackEvent's callers.
|
|
243
|
+
resolved = fingerprintDistinctId();
|
|
244
|
+
} else {
|
|
245
|
+
migrateLegacyTelemetryId(idPath, legacyTelemetryIdPath());
|
|
246
|
+
resolved = persistentDistinctId(idPath);
|
|
247
|
+
}
|
|
238
248
|
}
|
|
239
249
|
cachedDistinctId = resolved;
|
|
240
250
|
return resolved;
|
|
241
251
|
}
|
|
242
252
|
|
|
243
253
|
export function defaultTelemetrySaltPath() {
|
|
244
|
-
|
|
254
|
+
const home = safeHomedir();
|
|
255
|
+
return home === null ? null : path.join(home, '.grada', 'telemetry-salt');
|
|
245
256
|
}
|
|
246
257
|
|
|
247
258
|
let cachedTelemetrySalt = null;
|
|
@@ -253,10 +264,11 @@ export function resetTelemetrySaltCache() {
|
|
|
253
264
|
|
|
254
265
|
// Per-installation salt for the keyed identity hashes below. Get-or-create
|
|
255
266
|
// like the persistent distinct ID: GRADA_TELEMETRY_SALT_PATH overrides the
|
|
256
|
-
// default path for tests and hermetic environments.
|
|
257
|
-
//
|
|
258
|
-
//
|
|
259
|
-
// flowing and an unsalted hash
|
|
267
|
+
// default path for tests and hermetic environments. A null path
|
|
268
|
+
// (undeterminable HOME) and any filesystem failure both fall back to the
|
|
269
|
+
// process-ephemeral random value below — stable within this process,
|
|
270
|
+
// distinct across runs — so telemetry keeps flowing and an unsalted hash
|
|
271
|
+
// is never emitted. Never throws.
|
|
260
272
|
function resolveTelemetrySalt() {
|
|
261
273
|
if (cachedTelemetrySalt) return cachedTelemetrySalt;
|
|
262
274
|
const overridePath = process.env.GRADA_TELEMETRY_SALT_PATH;
|
|
@@ -264,19 +276,21 @@ function resolveTelemetrySalt() {
|
|
|
264
276
|
? overridePath
|
|
265
277
|
: defaultTelemetrySaltPath();
|
|
266
278
|
let salt = '';
|
|
267
|
-
|
|
279
|
+
if (saltPath !== null) {
|
|
268
280
|
try {
|
|
269
|
-
|
|
281
|
+
try {
|
|
282
|
+
salt = fs.readFileSync(saltPath, 'utf-8').trim();
|
|
283
|
+
} catch {
|
|
284
|
+
salt = '';
|
|
285
|
+
}
|
|
286
|
+
if (!salt) {
|
|
287
|
+
salt = crypto.randomBytes(32).toString('hex');
|
|
288
|
+
fs.mkdirSync(path.dirname(saltPath), { recursive: true });
|
|
289
|
+
fs.writeFileSync(saltPath, `${salt}\n`, 'utf-8');
|
|
290
|
+
}
|
|
270
291
|
} catch {
|
|
271
292
|
salt = '';
|
|
272
293
|
}
|
|
273
|
-
if (!salt) {
|
|
274
|
-
salt = crypto.randomBytes(32).toString('hex');
|
|
275
|
-
fs.mkdirSync(path.dirname(saltPath), { recursive: true });
|
|
276
|
-
fs.writeFileSync(saltPath, `${salt}\n`, 'utf-8');
|
|
277
|
-
}
|
|
278
|
-
} catch {
|
|
279
|
-
salt = '';
|
|
280
294
|
}
|
|
281
295
|
if (!salt) {
|
|
282
296
|
if (!processEphemeralSalt) processEphemeralSalt = crypto.randomBytes(32).toString('hex');
|
|
@@ -296,7 +310,8 @@ function saltedHash(raw) {
|
|
|
296
310
|
}
|
|
297
311
|
|
|
298
312
|
export function defaultConfigPath() {
|
|
299
|
-
|
|
313
|
+
const home = safeHomedir();
|
|
314
|
+
return home === null ? null : path.join(home, '.grada', 'config.json');
|
|
300
315
|
}
|
|
301
316
|
|
|
302
317
|
export function resolveConfigPath(env = process.env) {
|
|
@@ -313,7 +328,9 @@ let cachedTelemetryPreference;
|
|
|
313
328
|
// hard switch stays DO_NOT_TRACK) so telemetry can never fail a command.
|
|
314
329
|
export function readTelemetryPreference({ configPath, readFile = fs.readFileSync } = {}) {
|
|
315
330
|
try {
|
|
316
|
-
const
|
|
331
|
+
const target = configPath ?? resolveConfigPath();
|
|
332
|
+
if (target === null) return null;
|
|
333
|
+
const parsed = JSON.parse(readFile(target, 'utf-8'));
|
|
317
334
|
if (parsed && typeof parsed === 'object' && typeof parsed.telemetry === 'boolean') {
|
|
318
335
|
return parsed.telemetry;
|
|
319
336
|
}
|
|
@@ -339,6 +356,9 @@ function telemetryPreference() {
|
|
|
339
356
|
// it — unlike the sending path, an explicit user action must fail loudly.
|
|
340
357
|
export function writeTelemetryPreference(enabled, { configPath } = {}) {
|
|
341
358
|
const target = configPath ?? resolveConfigPath();
|
|
359
|
+
if (target === null) {
|
|
360
|
+
throw new Error('Cannot save the telemetry preference: the home directory is undeterminable (HOME is unset and no passwd entry exists).');
|
|
361
|
+
}
|
|
342
362
|
let current = {};
|
|
343
363
|
try {
|
|
344
364
|
const parsed = JSON.parse(fs.readFileSync(target, 'utf-8'));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
-
import os from 'os';
|
|
3
2
|
import path from 'path';
|
|
3
|
+
import { safeHomedir } from '../core/homedir.js';
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
5
|
import { log } from '@clack/prompts';
|
|
6
6
|
import {
|
|
@@ -351,16 +351,24 @@ function resolveCacheEnvPath() {
|
|
|
351
351
|
return process.env.GRADA_BEDROCK_CACHE_PATH || process.env.DEPLOY_STACK_BEDROCK_CACHE_PATH;
|
|
352
352
|
}
|
|
353
353
|
|
|
354
|
+
// Home-rooted cache candidate: null when HOME is undeterminable (the
|
|
355
|
+
// loader drops null candidates, so homeless runs fall through to the
|
|
356
|
+
// bundled catalog instead of throwing at import time).
|
|
357
|
+
function homeCachePath(...segments) {
|
|
358
|
+
const home = safeHomedir();
|
|
359
|
+
return home === null ? null : path.join(home, ...segments);
|
|
360
|
+
}
|
|
361
|
+
|
|
354
362
|
function resolveCachePath(options = {}) {
|
|
355
363
|
return (
|
|
356
364
|
options.cachePath ||
|
|
357
365
|
resolveCacheEnvPath() ||
|
|
358
|
-
|
|
366
|
+
homeCachePath('.grada', 'bedrock-models-cache.json')
|
|
359
367
|
);
|
|
360
368
|
}
|
|
361
369
|
|
|
362
370
|
function legacyCachePath() {
|
|
363
|
-
return
|
|
371
|
+
return homeCachePath('.deploy-stack', 'bedrock-models-cache.json');
|
|
364
372
|
}
|
|
365
373
|
|
|
366
374
|
export function loadBedrockCatalog(options = {}) {
|
|
@@ -369,11 +377,11 @@ export function loadBedrockCatalog(options = {}) {
|
|
|
369
377
|
const testEnv = Boolean(process.env.VITEST || process.env.NODE_ENV === 'test');
|
|
370
378
|
if (testEnv && !explicitCachePath) return bundled;
|
|
371
379
|
// An explicit path wins outright; otherwise the new default path is
|
|
372
|
-
// tried before the legacy one.
|
|
373
|
-
// the bundled catalog.
|
|
380
|
+
// tried before the legacy one. Null candidates (undeterminable HOME)
|
|
381
|
+
// and unusable files fall through to the bundled catalog.
|
|
374
382
|
const candidates = explicitCachePath
|
|
375
383
|
? [explicitCachePath]
|
|
376
|
-
: [resolveCachePath(options), legacyCachePath()];
|
|
384
|
+
: [resolveCachePath(options), legacyCachePath()].filter((candidate) => candidate !== null);
|
|
377
385
|
for (const candidate of candidates) {
|
|
378
386
|
try {
|
|
379
387
|
const cached = JSON.parse(fs.readFileSync(candidate, 'utf-8'));
|
|
@@ -455,6 +463,10 @@ export async function refreshBedrockCatalog(options = {}) {
|
|
|
455
463
|
const bundled = loadBundledCatalog();
|
|
456
464
|
const region = options.region || process.env.AWS_REGION || 'us-east-2';
|
|
457
465
|
const cachePath = resolveCachePath(options);
|
|
466
|
+
if (cachePath === null) {
|
|
467
|
+
log.warn('Bedrock catalog refresh skipped: the home directory is undeterminable, so there is nowhere to write the cache. Using the bundled catalog.');
|
|
468
|
+
return bundled;
|
|
469
|
+
}
|
|
458
470
|
const pruneMissing = options.pruneMissing === true;
|
|
459
471
|
try {
|
|
460
472
|
const client = options.bedrockClient || new BedrockClient({ region });
|