const $ = (selector) => document.querySelector(selector); function empty(message) { return `

${message}

`; } function safe(value) { const element = document.createElement('span'); element.textContent = value ?? ''; return element.innerHTML; } function render(snapshot) { const project = snapshot.project ?? snapshot.repository; $('#project-name').textContent = project?.name ?? project?.id ?? 'Not installed'; $('#project-commit').textContent = snapshot.repository?.head?.slice(0, 9) ?? '—'; $('#fleet-health').textContent = snapshot.mode === 'installed' ? (snapshot.doctor?.healthy ? 'Healthy' : 'Needs attention') : 'Not installed'; $('#context-state').textContent = snapshot.contextLocality?.measured ? 'Measured' : 'Unmeasured'; const agents = snapshot.agents ?? []; $('#agent-count').textContent = `${agents.length} ${agents.length === 1 ? 'identity' : 'identities'}`; $('#agent-list').innerHTML = agents.length ? agents.map((agent) => `
${safe(agent.title ?? agent.areaId)}${safe(agent.summary ?? `${agent.runtime ?? 'runtime'} · ${agent.state}`)}
${safe(agent.areaId)}
`).join('') : empty('Install an approved fleet to see persistent identities.'); const tasks = (snapshot.backlog ?? []).filter((task) => !['completed', 'cancelled'].includes(task.state)); $('#task-count').textContent = `${tasks.length} active`; $('#task-list').innerHTML = tasks.length ? tasks.slice(0, 12).map((task) => `
${safe(task.title ?? task.id)}${safe(task.owner ?? 'unassigned')}
${safe(task.state)}
`).join('') : empty('No active tracked work.'); const changes = snapshot.fleetChanges ?? []; const pendingChanges = changes.filter((change) => !['active', 'rejected'].includes(change.state)); $('#change-count').textContent = `${pendingChanges.length} pending`; $('#change-list').innerHTML = changes.length ? changes.map((change) => `
${safe(change.title ?? change.domainId)}${safe(change.expectedBenefit ?? 'No expected benefit recorded')}
${safe(change.state)}
`).join('') : empty('No Fleet changes proposed. The current roster remains in force.'); const integrations = snapshot.integration ?? []; $('#integration-list').innerHTML = integrations.length ? integrations.map((item) => `
${safe(item.sourceArea)}${safe(item.sourceCommit?.slice(0, 9))}
${safe(item.state)}
`).join('') : empty('No commits waiting to land.'); const holders = snapshot.resources?.holders ?? []; const waiters = snapshot.resources?.waiters ?? []; $('#resource-list').innerHTML = holders.length || waiters.length ? [...holders.map((item) => `
${safe(item.resourceId)}${safe(item.areaId)} holds
`), ...waiters.map((item) => `
${safe(item.resourceId)}${safe(item.areaId)} waits
`)].join('') : empty('All configured resources are available.'); const messages = snapshot.messages?.recent ?? []; $('#message-count').textContent = `${snapshot.messages?.unacknowledged ?? 0} unacknowledged`; $('#message-list').innerHTML = messages.length ? messages.map((message) => `

${safe(message.sender)} to ${safe(message.recipient)}

${safe(message.body)}
`).join('') : empty('No durable messages yet.'); const locality = snapshot.contextLocality; $('#context-detail').innerHTML = locality?.measured ? locality.samples.map((sample) => `
${safe(sample.areaId)}${safe(String(sample.cacheRead ?? 0))} cached tokens
`).join('') : `

${safe(locality?.reason ?? 'Runtime usage reporting is not available.')}

TORCH will compare cache activity with verified outcomes, not token count alone.`; } async function refresh() { $('#console-error').hidden = true; try { const response = await fetch('/api/snapshot', { cache: 'no-store' }); const snapshot = await response.json(); if (!response.ok) throw new Error(snapshot.message ?? snapshot.error); render(snapshot); } catch (error) { $('#console-error').textContent = `Snapshot unavailable: ${error.message}`; $('#console-error').hidden = false; } } $('#refresh-console')?.addEventListener('click', refresh); refresh();