Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
import { useCallback, useEffect, useMemo, useRef, useState, type KeyboardEvent, type ReactNode } from 'react';
import type { AtlasApi } from '../app/atlas-api';
import type { ProjectEntry } from '../provider/rpc-schemas';
import IndexesPanel, { type IndexesApi } from './IndexesPanel';
import { collapseRepeats, cpuText, logLevel, memoryLabel, memoryText, repeatTitle } from './system-model';
import { useSystemPoll, type SystemReading } from './useSystemPoll';
import './system.css';
export interface SystemWorkspaceProps {
api: Pick<AtlasApi, 'processes' | 'logs' | 'indexJobs'> & IndexesApi;
onOpenProjects: () => void;
active?: boolean;
version?: string;
pollMs?: number;
project?: string;
configuration?: ReactNode | ((active: boolean) => ReactNode);
listProjects?: () => Promise<ProjectEntry[]>;
}
type SystemTab = 'overview' | 'configuration' | 'indexes' | 'logs';
const TABS: SystemTab[] = ['overview', 'configuration', 'indexes', 'logs'];
const TAB_LABELS: Record<SystemTab, string> = { overview: 'Overview', configuration: 'Configuration', indexes: 'Indexes', logs: 'Logs' };
function ReadStatus({ reading, paused }: { reading: SystemReading<unknown>; paused: boolean }) {
return <div className="system-read-status">
<span className={reading.error ? 'system-warning' : ''}>
{reading.error ? reading.data ? 'Update failed · showing last reading' : 'Update failed' : paused ? 'Live updates paused' : reading.loading ? 'Updating…' : 'Live updates on'}
</span>
<span>{reading.updatedAt === null ? 'No reading yet' : <>Updated <time dateTime={new Date(reading.updatedAt).toISOString()}>{new Date(reading.updatedAt).toLocaleTimeString()}</time></>}</span>
{reading.error && <p role="alert">{reading.error}</p>}
</div>;
}
/** Daemon readings and configuration; project indexing uses the existing dialog. */
export default function SystemWorkspace({ api, onOpenProjects, active = true, version, pollMs = 3000, project, configuration, listProjects }: SystemWorkspaceProps) {
const [tab, setTab] = useState<SystemTab>('overview');
const [paused, setPaused] = useState(false);
const [level, setLevel] = useState<'all' | 'error' | 'warn' | 'info'>('all');
const [scope, setScope] = useState<'daemon' | 'project' | 'unattributed'>('daemon');
const [copyStatus, setCopyStatus] = useState('');
const [followTail, setFollowTail] = useState(true);
const [indexesRefresh, setIndexesRefresh] = useState(0);
const logRef = useRef<HTMLDivElement>(null);
const readProcesses = useCallback(() => api.processes(), [api]);
const selectedProject = scope === 'project' ? project : undefined;
const readLogs = useCallback(async () => {
if (scope === 'project' && !selectedProject) throw new Error('Select a project to read its attributed events.');
const data = await api.logs(200, level === 'all' ? undefined : level, selectedProject, {
...(scope === 'unattributed' ? { scope: 'unattributed' as const } : {}),
});
if ((scope !== 'daemon' || level !== 'all') && data.persistent !== true) throw new Error('Persistent history is unavailable. Filters require the daemon journal.');
if (scope === 'project' && (data.scope !== 'project' || data.project !== selectedProject)) throw new Error('The daemon did not confirm this project scope.');
if (scope === 'unattributed' && data.scope !== 'unattributed') throw new Error('The daemon did not confirm unattributed scope.');
return data;
}, [api, level, selectedProject, scope]);
const readJobs = useCallback(() => api.indexJobs(), [api]);
const processes = useSystemPoll(readProcesses, active, paused, pollMs);
const logs = useSystemPoll(readLogs, active && tab === 'logs', paused, pollMs);
const jobs = useSystemPoll(readJobs, active && tab === 'indexes', paused, pollMs);
const report = processes.data;
const self = report?.processes.find((process) => process.isSelf);
const visibleLines = logs.data?.lines ?? [];
const visibleRecords = logs.data?.records;
// Identical messages read as one line with a count; a repeated warning cannot push the rest out of view.
const logRows = useMemo(() => visibleRecords ? collapseRepeats(visibleRecords) : undefined, [visibleRecords]);
const selectedReading = tab === 'logs' ? logs : tab === 'indexes' ? jobs : processes;
useEffect(() => {
if (followTail && logRef.current) logRef.current.scrollTop = logRef.current.scrollHeight;
}, [logs.data, followTail, tab]);
useEffect(() => { setCopyStatus(''); }, [level, logs.data]);
const copyLogs = async () => {
try {
if (!navigator.clipboard?.writeText) throw new Error('Clipboard access is unavailable. Select and copy the visible text.');
await navigator.clipboard.writeText(logRows ? logRows.map(({ record, count }) => `${record.ts} ${record.level.toUpperCase()} ${record.source} #${record.id}${count > 1 ? ` ×${count}` : ''}\n${record.message}`).join('\n') : visibleLines.join('\n'));
setCopyStatus('Visible lines copied');
} catch (error) {
setCopyStatus(error instanceof Error ? error.message : 'Could not copy the log.');
}
};
const moveTab = (event: KeyboardEvent<HTMLButtonElement>, current: SystemTab) => {
const index = TABS.indexOf(current);
const target = event.key === 'ArrowDown' ? TABS[(index + 1) % TABS.length]
: event.key === 'ArrowUp' ? TABS[(index + TABS.length - 1) % TABS.length]
: event.key === 'Home' ? TABS[0] : event.key === 'End' ? TABS[TABS.length - 1] : undefined;
if (!target) return;
event.preventDefault();
setTab(target);
document.getElementById(`system-tab-${target}`)?.focus();
};
return <section className="system-workspace" aria-label="System" hidden={!active} data-testid="system-workspace">
<header className="system-heading">
<div><p className="system-eyebrow">DAEMON</p><h1>System</h1><p>Configuration, processes, indexes, and events.</p></div>
<div className="system-heading-actions">
{version && <span className="system-version">{version}</span>}
{tab !== 'configuration' && <>
<button type="button" onClick={() => { selectedReading.refresh(); if (tab === 'indexes') setIndexesRefresh(value => value + 1); }} disabled={selectedReading.loading}>Refresh</button>
<button type="button" aria-label={paused ? 'Resume live updates' : 'Pause live updates'} aria-pressed={paused} onClick={() => setPaused(!paused)}>{paused ? 'Resume live' : 'Pause live'}</button>
</>}
</div>
</header>
<div className="system-layout">
<div className="system-tabs" role="tablist" aria-label="System views" aria-orientation="vertical">
{TABS.map((name) => <button key={name} id={`system-tab-${name}`} type="button" role="tab" aria-selected={tab === name} aria-controls={`system-panel-${name}`} tabIndex={tab === name ? 0 : -1} onKeyDown={(event) => moveTab(event, name)} onClick={() => setTab(name)}>{TAB_LABELS[name]}</button>)}
</div>
<div className="system-view">
{tab !== 'configuration' && tab !== 'indexes' && <ReadStatus reading={selectedReading} paused={paused} />}
<div id="system-panel-overview" role="tabpanel" aria-labelledby="system-tab-overview" hidden={tab !== 'overview'}>
<div className="system-metrics">
<article><span>Serving process</span><strong>{report && report.selfPid > 0 ? `PID ${report.selfPid}` : 'Unavailable'}</strong><small>{self?.elapsed ? `Running ${self.elapsed}` : 'Uptime unavailable'}</small></article>
<article><span>{memoryLabel(report?.telemetry?.selfMemoryKind)}</span><strong>{memoryText(report?.telemetry?.selfMemoryMb)}</strong><small>Serving daemon</small></article>
<article><span>{report?.telemetry?.cpuUnit === 'seconds' ? 'CPU time' : 'CPU'}</span><strong>{cpuText(self?.telemetry?.cpu, report?.telemetry?.cpuUnit)}</strong><small>{report?.telemetry?.cpuUnit === 'seconds' ? 'Cumulative process time' : report?.telemetry?.cpuUnit === 'percent' ? 'Reported by the host process monitor' : 'Server did not specify a unit'}</small></article>
<article><span>Processes listed</span><strong>{report ? report.processes.length : 'Unavailable'}</strong><small>Visible to the serving daemon</small></article>
</div>
<section className="system-section" aria-label="Daemon processes">
<div className="system-section-heading"><h2>Processes</h2><span>{memoryLabel(report?.telemetry?.memoryKind)} · MiB</span></div>
<div className="system-table-wrap"><table><thead><tr><th>Process</th><th>CPU</th><th>Memory</th><th>Uptime</th><th>Command</th></tr></thead><tbody>
{report?.processes.map((process, index) => <tr key={`${process.pid}-${index}`}>
<th scope="row">{process.pid > 0 ? process.pid : 'Unavailable'} {process.isSelf && <span className="system-tag">Serving</span>}</th>
<td>{cpuText(process.telemetry?.cpu, report.telemetry?.cpuUnit)}</td><td>{memoryText(process.telemetry?.memoryMb)}</td><td>{process.elapsed || 'Unavailable'}</td><td><code>{process.command || 'Unavailable'}</code></td>
</tr>)}
</tbody></table></div>
{!report && <p className="system-empty">{processes.loading ? 'Reading processes…' : 'No process reading available.'}</p>}
{report?.processes.length === 0 && <p className="system-empty">The server returned no process entries.</p>}
</section>
</div>
<div id="system-panel-configuration" role="tabpanel" aria-labelledby="system-tab-configuration" hidden={tab !== 'configuration'}>
{(typeof configuration === 'function' ? configuration(active && tab === 'configuration') : configuration) ?? <p className="system-empty">Configuration is unavailable.</p>}
</div>
<div id="system-panel-indexes" role="tabpanel" aria-labelledby="system-tab-indexes" hidden={tab !== 'indexes'}>
<IndexesPanel api={api} listProjects={listProjects} active={active && tab === 'indexes'} paused={paused} refreshToken={indexesRefresh} onOpenProjects={onOpenProjects} />
<details className="system-index-activity">
<summary>Recent indexing activity{jobs.data ? ` · ${jobs.data.length} job slots` : ''}</summary><p className="system-muted">Current daemon job slots. Slots may be reused; this is not a complete history.</p>
<div className="system-table-wrap"><table><thead><tr><th>Repository</th><th>State</th><th>Details</th></tr></thead><tbody>
{jobs.data?.map((job, index) => <tr key={`${job.slot}-${index}`}><th scope="row"><code>{job.path || 'Path unavailable'}</code></th><td><span className={`system-job-state system-job-${job.status}`}>{job.status === 'indexing' ? 'Indexing' : job.status === 'done' ? 'Complete' : job.status === 'error' ? 'Failed' : 'Unknown'}</span></td><td>{job.error || 'No details reported'}</td></tr>)}
</tbody></table></div>
{jobs.data?.length === 0 && <p className="system-empty">No index activity reported by this daemon.</p>}
{!jobs.data && <p className="system-empty">{jobs.loading ? 'Reading index activity…' : 'No index reading available.'}</p>}
{jobs.error && <p className="system-warning" role="alert">Could not update job activity: {jobs.error}</p>}
</details>
</div>
<div id="system-panel-logs" role="tabpanel" aria-labelledby="system-tab-logs" hidden={tab !== 'logs'}>
<section className="system-section">
<div className="system-section-heading"><div><h2>Errors and logs</h2><p>{report && report.selfPid > 0 ? `Serving process ${report.selfPid}` : 'Serving daemon'} · Latest 200 matching events</p></div><button type="button" disabled={(visibleRecords?.length ?? visibleLines.length) === 0} onClick={() => { void copyLogs(); }}>Copy visible</button></div>
{logs.data && <p className="system-muted">{logs.data.persistent === true ? `SQLite history · ${logs.data.retentionLimit ?? 'bounded'} event retention limit · survives daemon restarts` : 'Persistent history unavailable; this tail may not survive a restart.'} Events identify affected paths or jobs only when the daemon recorded them.</p>}
<div className="system-log-controls">
<label><span>Scope</span><select aria-label="Log scope" value={scope} onChange={(event) => setScope(event.currentTarget.value as typeof scope)}>
<option value="daemon">All daemon projects</option>
<option value="project" disabled={!project}>Current project{project ? ` · ${project}` : ' · none selected'}</option>
<option value="unattributed">Unattributed events</option>
</select></label>
<label><span>Severity</span><select aria-label="Log severity" value={level} onChange={(event) => setLevel(event.currentTarget.value as typeof level)}>
<option value="all">All levels</option><option value="info">Info and above</option>
<option value="warn">Warnings and errors</option><option value="error">Errors</option>
</select></label>
<label className="system-tail-toggle"><input type="checkbox" checked={followTail} onChange={(event) => setFollowTail(event.currentTarget.checked)} /> Follow tail</label>
</div>
<p className="system-muted">Scope, severity and search filter the retained SQLite history before the 200-event display limit. {scope === 'project' ? `Only explicitly attributed events for ${project ?? 'the selected project'}.` : scope === 'unattributed' ? 'Project ownership was not recorded; no project is inferred from a path.' : 'Includes all projects and events with unknown ownership.'}</p>
<div className="system-log-meta"><span>{visibleRecords?.length ?? visibleLines.length} shown · {logs.data?.total ?? 'Unknown'} matching retained events</span><span role="status">{copyStatus}</span></div>
<div className="system-log" aria-label="Daemon log lines" tabIndex={0} ref={logRef} onScroll={() => {
const element = logRef.current;
if (element && element.scrollHeight - element.scrollTop - element.clientHeight > 32) setFollowTail(false);
}}>
{logRows ? logRows.map((row) => { const { record } = row; return <div className={`system-log-line system-log-record system-log-${logLevel(`level=${record.level}`)}`} key={`${logs.data?.generation ?? ''}:${record.id}`}><strong>{record.level.toUpperCase() || 'UNCLASSIFIED'}</strong> · <time dateTime={record.ts}>{record.ts || 'Time unavailable'}</time> · {record.source || 'Source unavailable'} · Event #{record.id} · {record.project ? `Project: ${record.project}` : 'Project not recorded'}{row.count > 1 && <> · <span className="system-log-repeat" title={repeatTitle(row)}>×{row.count}</span></>}<pre>{record.message}</pre></div>; }) : visibleLines.map((line, index) => <div className={`system-log-line system-log-${logLevel(line)}`} key={index}>{line || '\u00a0'}</div>)}
{(visibleRecords?.length ?? visibleLines.length) === 0 && <p>{logs.loading ? 'Reading log…' : !logs.data ? 'No log reading available.' : 'No retained events match these filters.'}</p>}
</div>
</section>
</div>
</div>
</div>
</section>;
}