Changes: - Non-blocking coding-data sharing upsell banner - Consolidate remediation in Doctor - Auto mode defers fail-closed gate asks to the classifier - Coalesce marketplace list fetches - Allow removing a marketplace source by name - Contain hung git marketplace sources (timeouts, non-blocking refresh, unbrick modal) - Label failed workspace RPCs with error_kind - Drop redundant explicit tonic/prost deps from xai-grok-shell - Report real exit codes for completed background shells - Narrow the date-rollover reminder to date-bearing templates - Wire toolOverrides through the session and agent - Security: Bash(git:*) allowlist matches whole command chain by prefix - Split prompt-trigger telemetry and record classifier provenance - Raise connectors-manager timeout to 60s - Auto classifier honors recorded approvals for repeat actions - Apply doctor fixes in the TUI - Auto-mode classifier timeouts prompt instead of silently denying - Scope subagent completion drains to the owning session - Add the toolOverrides wire types - Set client_identifier=grok-agent-sdk - Accept both spellings of the workspace-teleport kill switch - Persist one-shot occurrence journal - Stop turns that poll the exact same tool call 16x in a row - Copy compaction checkpoint files when forking sessions - Auto-focus permission prompt from scrollback - Esc cancels the running turn in non-vim and minimal modes - List Ctrl+Z undo and redo in keyboard shortcuts - Out-of-process macOS mic capture - Show active auth mode on session-info - Install the npm binary under $GROK_HOME - Remove hover/click dead zones between dashboard items - Route startup warnings to doctor - Document [feedback.user] author identity config - Extend bang command timeout - Close combine-queued edit-hold race - Integrate relocation recovery - Expose privacy notice rollout flag - Break harness discovery ref cycle so connections can idle-evict - Shift/Alt+Enter inserts newline when editing a queued prompt - Gate project Claude permissions on folder trust - Echo response.create.event_id on response.created - Toast when session creation fails from disk full - Add shared test process lifecycle - Enable dynamic workflows by default - Add relocation transaction state machine - Add shared test sandbox - Surface auth failures on model-switch compact - Persist durable scheduler expiry - Confirm before removing extensions-modal items - Re-run compact and prompt after login when compact hit expired auth - Recap sends hosted tools under backend search
240 lines
9.3 KiB
JavaScript
240 lines
9.3 KiB
JavaScript
#!/usr/bin/env node
|
|
// Runs once after npm install/update. Reads the grok binary from the
|
|
// matching per-platform optional dependency (@xai-official/grok-<platform>)
|
|
// and installs it to ~/.grok/bin/ using versioned filenames:
|
|
//
|
|
// Unix: grok-<version> + grok (symlink)
|
|
// Windows: grok-<version>.exe + grok.exe (copy)
|
|
//
|
|
// Versioned files ensure running processes are never disrupted on macOS
|
|
// (replacing a binary that a running process has mmap'd causes SIGKILL
|
|
// because the kernel can no longer verify the code signature).
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const zlib = require('zlib');
|
|
const { execSync } = require('child_process');
|
|
const TOML = require('@iarna/toml');
|
|
|
|
// $GROK_HOME (else ~/.grok), matching the Rust grok_home() including its
|
|
// canonicalized-home default. Lets fleets relocate the binary off a slow $HOME
|
|
// (NFS); old code hardcoded os.homedir().
|
|
function defaultGrokHome() {
|
|
const home = os.homedir();
|
|
try { return path.join(fs.realpathSync(home), '.grok'); } catch { return path.join(home, '.grok'); }
|
|
}
|
|
const GROK_HOME = process.env.GROK_HOME ?? defaultGrokHome();
|
|
const CANONICAL_DIR = path.join(GROK_HOME, 'bin');
|
|
|
|
const key = `${process.platform}-${process.arch}`;
|
|
const SUPPORTED = new Set([
|
|
'darwin-arm64',
|
|
'darwin-x64',
|
|
'linux-x64',
|
|
'linux-arm64',
|
|
'win32-x64',
|
|
'win32-arm64',
|
|
]);
|
|
if (!SUPPORTED.has(key)) {
|
|
console.error(`@xai-official/grok: unsupported platform ${key}`);
|
|
process.exit(0);
|
|
}
|
|
|
|
// Resolve the per-platform sibling package's directory. The matching
|
|
// optionalDependency is installed by npm based on `os`/`cpu` filters; the
|
|
// other five are silently skipped. If the matching one is missing, npm was
|
|
// likely invoked with --no-optional or the platform is unsupported.
|
|
function resolvePlatformPackageDir() {
|
|
const platformPkg = `@xai-official/grok-${key}`;
|
|
try {
|
|
return path.dirname(require.resolve(`${platformPkg}/package.json`));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
let version;
|
|
try { version = require('../package.json').version; } catch {}
|
|
if (!version) {
|
|
console.error('@xai-official/grok: unable to determine version');
|
|
process.exit(0);
|
|
}
|
|
|
|
const IS_WINDOWS = process.platform === 'win32';
|
|
const EXE = IS_WINDOWS ? '.exe' : '';
|
|
|
|
fs.mkdirSync(CANONICAL_DIR, { recursive: true });
|
|
|
|
function writeVendorBinary(brPath, rawPath, destPath) {
|
|
const tmp = destPath + `.tmp.${process.pid}`;
|
|
try {
|
|
if (fs.existsSync(brPath)) {
|
|
fs.writeFileSync(tmp, zlib.brotliDecompressSync(fs.readFileSync(brPath)));
|
|
} else if (fs.existsSync(rawPath)) {
|
|
fs.copyFileSync(rawPath, tmp);
|
|
} else {
|
|
return false;
|
|
}
|
|
if (!IS_WINDOWS) fs.chmodSync(tmp, 0o755);
|
|
fs.renameSync(tmp, destPath);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
} finally {
|
|
try { fs.unlinkSync(tmp); } catch {}
|
|
}
|
|
}
|
|
|
|
function installBinary(binName, sourceDir, vendorSubpath) {
|
|
const brPath = path.join(sourceDir, 'bin', vendorSubpath + '.br');
|
|
const rawPath = path.join(sourceDir, 'bin', vendorSubpath);
|
|
|
|
const versionedName = `${binName}-${version}${EXE}`;
|
|
const versionedPath = path.join(CANONICAL_DIR, versionedName);
|
|
const canonicalName = `${binName}${EXE}`;
|
|
const canonicalPath = path.join(CANONICAL_DIR, canonicalName);
|
|
|
|
// Skip if this exact version is already installed.
|
|
if (!fs.existsSync(versionedPath) && !writeVendorBinary(brPath, rawPath, versionedPath)) {
|
|
console.error(`@xai-official/grok: missing binary at ${brPath}`);
|
|
return false;
|
|
}
|
|
|
|
if (IS_WINDOWS) {
|
|
// Symlinks need elevation on Windows; copy instead. If the exe is
|
|
// locked by a running process, rename it aside then retry.
|
|
const oldPath = canonicalPath + '.old';
|
|
try { fs.unlinkSync(oldPath); } catch {} // stale backup from prior update
|
|
try {
|
|
try { fs.unlinkSync(canonicalPath); } catch {}
|
|
fs.copyFileSync(versionedPath, canonicalPath);
|
|
} catch (e) {
|
|
try {
|
|
fs.renameSync(canonicalPath, oldPath);
|
|
try {
|
|
fs.copyFileSync(versionedPath, canonicalPath);
|
|
} catch (copyErr) {
|
|
// Rollback: restore the old binary so the install isn't broken.
|
|
try { fs.renameSync(oldPath, canonicalPath); } catch {}
|
|
throw copyErr;
|
|
}
|
|
} catch (e2) {
|
|
console.error(`@xai-official/grok: failed to update ${canonicalPath}: ${e2.message}`);
|
|
console.error('Close all running grok processes and try again.');
|
|
return false;
|
|
}
|
|
}
|
|
} else {
|
|
// Atomic symlink swap.
|
|
const tmpLink = canonicalPath + `.link.${process.pid}`;
|
|
try { fs.unlinkSync(tmpLink); } catch {}
|
|
fs.symlinkSync(versionedName, tmpLink);
|
|
fs.renameSync(tmpLink, canonicalPath);
|
|
}
|
|
|
|
// Don't report a broken wire-up as success.
|
|
if (!fs.existsSync(canonicalPath)) {
|
|
console.error(`@xai-official/grok: ${canonicalName} did not resolve after install`);
|
|
return false;
|
|
}
|
|
|
|
console.log(`${binName} ${version} installed to ${canonicalPath} -> ${versionedName}`);
|
|
return true;
|
|
}
|
|
|
|
// Comparator: sort "<prefix>X.Y.Z" filenames by version, newest first.
|
|
function byVersionDescending(prefix) {
|
|
return (a, b) => {
|
|
const pa = a.slice(prefix.length).split('.').map(Number);
|
|
const pb = b.slice(prefix.length).split('.').map(Number);
|
|
for (let i = 0; i < 3; i++) {
|
|
if ((pa[i] || 0) !== (pb[i] || 0)) return (pb[i] || 0) - (pa[i] || 0);
|
|
}
|
|
return 0;
|
|
};
|
|
}
|
|
|
|
// Best-effort cleanup of old versioned binaries for a given binary name.
|
|
// Keeps the current version and the previous one (in case a process is still
|
|
// running the old binary and hasn't fully loaded all pages yet).
|
|
// Uses an exact prefix match + hyphen + digit to avoid grok-* matching grok-pager-*.
|
|
function cleanupOldVersions(binName) {
|
|
try {
|
|
const prefix = `${binName}-`;
|
|
const currentVersioned = `${binName}-${version}${EXE}`;
|
|
const entries = fs.readdirSync(CANONICAL_DIR);
|
|
const versionedBinaries = entries
|
|
.filter(e => {
|
|
if (!e.startsWith(prefix)) return false;
|
|
if (e.includes('.tmp.') || e.includes('.link.')) return false;
|
|
if (e === currentVersioned) return false;
|
|
const suffix = e.slice(prefix.length);
|
|
return /^\d/.test(suffix);
|
|
})
|
|
.sort(byVersionDescending(prefix));
|
|
for (const old of versionedBinaries.slice(1)) {
|
|
try { fs.unlinkSync(path.join(CANONICAL_DIR, old)); } catch {}
|
|
}
|
|
} catch {}
|
|
}
|
|
|
|
const platformDir = resolvePlatformPackageDir();
|
|
if (!platformDir) {
|
|
console.error(`@xai-official/grok: platform package @xai-official/grok-${key} not installed.`);
|
|
console.error(' This usually means npm was invoked with --no-optional, or the install failed.');
|
|
console.error(' Try: npm install -g @xai-official/grok');
|
|
process.exit(0);
|
|
}
|
|
|
|
installBinary('grok', platformDir, `grok${EXE}`);
|
|
cleanupOldVersions('grok');
|
|
cleanupOldVersions('grok-pager');
|
|
|
|
// Write installer config
|
|
const configDir = GROK_HOME;
|
|
const configPath = path.join(configDir, 'config.toml');
|
|
let obj = {};
|
|
try { obj = TOML.parse(fs.readFileSync(configPath, 'utf8')); } catch { }
|
|
obj.cli ??= {};
|
|
obj.cli.installer = 'npm';
|
|
|
|
// Persist the npm registry so `grok update` and the trampoline use the same one.
|
|
const npmRegistry = process.env.GROK_NPM_REGISTRY
|
|
|| (() => {
|
|
try {
|
|
const resolved = execSync(
|
|
'npm config get @xai-official:registry',
|
|
{ encoding: 'utf8', timeout: 5000 }
|
|
).trim();
|
|
if (resolved && resolved !== 'undefined') return resolved;
|
|
} catch {}
|
|
return null;
|
|
})();
|
|
|
|
if (npmRegistry) {
|
|
obj.cli.npm_registry = npmRegistry;
|
|
}
|
|
|
|
fs.writeFileSync(configPath, TOML.stringify(obj), 'utf8');
|
|
|
|
// Shell completions: print setup hints (no silent shell config mutation).
|
|
// Set GROK_INSTALL_COMPLETIONS=1 to auto-generate to ~/.grok/completions.
|
|
const GROK_PATH = path.join(CANONICAL_DIR, `grok${EXE}`);
|
|
if (process.env.GROK_INSTALL_COMPLETIONS === '1' && !IS_WINDOWS) {
|
|
try {
|
|
const { spawnSync } = require('child_process');
|
|
const completionsDir = path.join(GROK_HOME, 'completions');
|
|
const bashPath = path.join(completionsDir, 'bash', 'grok.bash');
|
|
const zshPath = path.join(completionsDir, 'zsh', '_grok');
|
|
fs.mkdirSync(path.dirname(bashPath), { recursive: true });
|
|
fs.mkdirSync(path.dirname(zshPath), { recursive: true });
|
|
const bashRes = spawnSync(GROK_PATH, ['completions', 'bash'], { encoding: 'utf8' });
|
|
if (bashRes.status === 0) fs.writeFileSync(bashPath, bashRes.stdout);
|
|
const zshRes = spawnSync(GROK_PATH, ['completions', 'zsh'], { encoding: 'utf8' });
|
|
if (zshRes.status === 0) fs.writeFileSync(zshPath, zshRes.stdout);
|
|
console.log('Completions generated to ~/.grok/completions (bash/zsh)');
|
|
} catch {}
|
|
} else if (!IS_WINDOWS) {
|
|
console.log('Tip: grok completions bash > ~/.local/share/bash-completion/completions/grok');
|
|
console.log(' grok completions zsh > ~/.zsh/completions/_grok');
|
|
}
|