Delete exported symbols that nothing in src/, setup/, scripts/ or runners/ references (and no test exercises), iterated to a fixed point: service-routing lease/failover helpers, paired-workspace-manager fingerprint/registration helpers, message-runtime-rules trigger helpers, utils.fetchWithTimeout, detectPnpmStorePath, isCodexMainService, isDeltaHandoffEnabledForGroup with the unused DELTA_HANDOFF config, the deprecated db/schema.applySchemaMigrations alias, unused db wrappers (memories, router state, sessions-by-agent-type, room skill override writes, recent chat/turn-output queries) and a few unused type aliases. Orphan files: src/dashboard.ts + src/dashboard-status-content.ts (only referenced each other), src/restart-context-cli.ts (never invoked) and the unused src/config/index.ts barrel. Service-handoff / channel-owner-lease code is left for a later batch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
5.5 KiB
TypeScript
200 lines
5.5 KiB
TypeScript
/**
|
|
* Cross-platform detection utilities for EJClaw setup.
|
|
*/
|
|
import { execSync } from 'child_process';
|
|
import fs from 'fs';
|
|
import os from 'os';
|
|
|
|
export type Platform = 'macos' | 'linux' | 'unknown';
|
|
export type ServiceManager = 'launchd' | 'systemd' | 'none';
|
|
export type LinuxReadonlySandboxAppArmorSetupResult =
|
|
| 'not-linux'
|
|
| 'not-needed'
|
|
| 'requires-root'
|
|
| 'failed'
|
|
| 'configured';
|
|
|
|
export function getPlatform(): Platform {
|
|
const platform = os.platform();
|
|
if (platform === 'darwin') return 'macos';
|
|
if (platform === 'linux') return 'linux';
|
|
return 'unknown';
|
|
}
|
|
|
|
export function isWSL(): boolean {
|
|
if (os.platform() !== 'linux') return false;
|
|
try {
|
|
const release = fs.readFileSync('/proc/version', 'utf-8').toLowerCase();
|
|
return release.includes('microsoft') || release.includes('wsl');
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isRoot(): boolean {
|
|
return process.getuid?.() === 0;
|
|
}
|
|
|
|
export function isHeadless(): boolean {
|
|
// No display server available
|
|
if (getPlatform() === 'linux') {
|
|
return !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY;
|
|
}
|
|
// macOS is never headless in practice (even SSH sessions can open URLs)
|
|
return false;
|
|
}
|
|
|
|
export function hasSystemd(): boolean {
|
|
if (getPlatform() !== 'linux') return false;
|
|
try {
|
|
// Check if systemd is PID 1
|
|
const init = fs.readFileSync('/proc/1/comm', 'utf-8').trim();
|
|
return init === 'systemd';
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function getServiceManager(): ServiceManager {
|
|
const platform = getPlatform();
|
|
if (platform === 'macos') return 'launchd';
|
|
if (platform === 'linux') {
|
|
if (hasSystemd()) return 'systemd';
|
|
return 'none';
|
|
}
|
|
return 'none';
|
|
}
|
|
|
|
export function getNodePath(): string {
|
|
try {
|
|
return execSync('command -v bun', { encoding: 'utf-8' }).trim();
|
|
} catch {
|
|
return process.execPath;
|
|
}
|
|
}
|
|
|
|
export function commandExists(name: string): boolean {
|
|
try {
|
|
execSync(`command -v ${name}`, { stdio: 'ignore' });
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function canUseLinuxBubblewrapReadonlySandbox(
|
|
platform: Platform = getPlatform(),
|
|
commandExistsFn: (name: string) => boolean = commandExists,
|
|
execFn: typeof execSync = execSync,
|
|
): boolean {
|
|
if (platform !== 'linux') return false;
|
|
if (!commandExistsFn('bwrap')) return false;
|
|
|
|
try {
|
|
execFn('bwrap --ro-bind / / /bin/true', { stdio: 'ignore' });
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function readProcSysValue(
|
|
procPath: string,
|
|
readFileSyncFn: typeof fs.readFileSync = fs.readFileSync,
|
|
): string | null {
|
|
try {
|
|
return readFileSyncFn(procPath, 'utf-8').trim();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function getAppArmorRestrictUnprivilegedUsernsValue(
|
|
readFileSyncFn: typeof fs.readFileSync = fs.readFileSync,
|
|
): string | null {
|
|
return readProcSysValue(
|
|
'/proc/sys/kernel/apparmor_restrict_unprivileged_userns',
|
|
readFileSyncFn,
|
|
);
|
|
}
|
|
|
|
export function ensureLinuxReadonlySandboxAppArmorSupport(options?: {
|
|
platform?: Platform;
|
|
isRootUser?: boolean;
|
|
apparmorRestrictUnprivilegedUserns?: string | null;
|
|
sandboxCapable?: boolean;
|
|
readFileSyncFn?: typeof fs.readFileSync;
|
|
mkdirSyncFn?: typeof fs.mkdirSync;
|
|
writeFileSyncFn?: typeof fs.writeFileSync;
|
|
execFn?: typeof execSync;
|
|
}): LinuxReadonlySandboxAppArmorSetupResult {
|
|
const platform = options?.platform ?? getPlatform();
|
|
if (platform !== 'linux') return 'not-linux';
|
|
const readFileSyncFn = options?.readFileSyncFn ?? fs.readFileSync;
|
|
|
|
const apparmorRestrictUnprivilegedUserns =
|
|
options?.apparmorRestrictUnprivilegedUserns ??
|
|
getAppArmorRestrictUnprivilegedUsernsValue(readFileSyncFn);
|
|
const sandboxCapable =
|
|
options?.sandboxCapable ?? canUseLinuxBubblewrapReadonlySandbox(platform);
|
|
|
|
if (apparmorRestrictUnprivilegedUserns !== '1' || sandboxCapable) {
|
|
return 'not-needed';
|
|
}
|
|
|
|
const isRootUser = options?.isRootUser ?? isRoot();
|
|
if (!isRootUser) return 'requires-root';
|
|
|
|
const mkdirSyncFn = options?.mkdirSyncFn ?? fs.mkdirSync;
|
|
const writeFileSyncFn = options?.writeFileSyncFn ?? fs.writeFileSync;
|
|
const execFn = options?.execFn ?? execSync;
|
|
const sysctlPath = '/etc/sysctl.d/90-ejclaw-sandbox.conf';
|
|
const sysctlContents =
|
|
'# Managed by EJClaw setup to allow bubblewrap readonly sandboxing.\n' +
|
|
'kernel.apparmor_restrict_unprivileged_userns=0\n';
|
|
let existingContents: string | null;
|
|
|
|
try {
|
|
existingContents = readFileSyncFn(sysctlPath, 'utf-8');
|
|
} catch {
|
|
existingContents = null;
|
|
}
|
|
|
|
try {
|
|
mkdirSyncFn('/etc/sysctl.d', { recursive: true });
|
|
if (existingContents !== sysctlContents) {
|
|
writeFileSyncFn(sysctlPath, sysctlContents);
|
|
}
|
|
execFn('sysctl -w kernel.apparmor_restrict_unprivileged_userns=0', {
|
|
stdio: 'ignore',
|
|
});
|
|
|
|
const appliedValue =
|
|
getAppArmorRestrictUnprivilegedUsernsValue(readFileSyncFn);
|
|
if (appliedValue !== '0') {
|
|
throw new Error(
|
|
`kernel.apparmor_restrict_unprivileged_userns remained ${appliedValue ?? 'unavailable'} after sysctl apply`,
|
|
);
|
|
}
|
|
return 'configured';
|
|
} catch {
|
|
return 'failed';
|
|
}
|
|
}
|
|
|
|
export function getNodeVersion(): string | null {
|
|
try {
|
|
const version = execSync('node --version', { encoding: 'utf-8' }).trim();
|
|
return version.replace(/^v/, '');
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function getNodeMajorVersion(): number | null {
|
|
const version = getNodeVersion();
|
|
if (!version) return null;
|
|
const major = parseInt(version.split('.')[0], 10);
|
|
return isNaN(major) ? null : major;
|
|
}
|