Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ coverage/
*.tgz

.worktrees/

.env.local
255 changes: 255 additions & 0 deletions src/observability/dashboard-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,258 @@ function writeMethodNotAllowed(
allow,
});
}

function writeHtml(
response: ServerResponse,
statusCode: number,
html: string,
): void {
response.statusCode = statusCode;
response.setHeader("content-type", "text/html; charset=utf-8");
response.setHeader("content-length", Buffer.byteLength(html));
response.end(html);
}

function writeNotFound(response: ServerResponse, path: string): void {
response.statusCode = 404;
response.setHeader("content-type", "text/plain; charset=utf-8");
response.end(`Not found: ${path}`);
}

async function readRequestBody(request: IncomingMessage): Promise<void> {
await new Promise<void>((resolve, reject) => {
request.on("error", reject);
request.on("end", resolve);
request.resume();
});
}

async function withTimeout<T>(
promise: Promise<T> | T,
timeoutMs: number,
createError: () => Error,
): Promise<T> {
return await new Promise<T>((resolve, reject) => {
const timeout = setTimeout(() => {
reject(createError());
}, timeoutMs);

Promise.resolve(promise).then(
(value) => {
clearTimeout(timeout);
resolve(value);
},
(error) => {
clearTimeout(timeout);
reject(error);
},
);
});
}

function isSnapshotTimeoutError(error: unknown): boolean {
return (
error instanceof Error &&
error.message.startsWith("Runtime snapshot timed out after ")
);
}

function renderDashboardHtml(snapshot: RuntimeSnapshot): string {
const runningRows =
snapshot.running.length === 0
? '<tr><td colspan="7">No active sessions.</td></tr>'
: snapshot.running
.map(
(row) => `
<tr>
<td>${escapeHtml(row.issue_identifier)}</td>
<td>${escapeHtml(row.state)}</td>
<td>${escapeHtml(row.session_id ?? "-")}</td>
<td>${row.turn_count}</td>
<td>${escapeHtml(row.last_event ?? "-")}</td>
<td>${escapeHtml(row.last_message ?? "-")}</td>
<td>${escapeHtml(row.last_event_at ?? "-")}</td>
</tr>`,
)
.join("");

const retryRows =
snapshot.retrying.length === 0
? '<tr><td colspan="4">No queued retries.</td></tr>'
: snapshot.retrying
.map(
(row) => `
<tr>
<td>${escapeHtml(row.issue_identifier ?? row.issue_id)}</td>
<td>${row.attempt}</td>
<td>${escapeHtml(row.due_at)}</td>
<td>${escapeHtml(row.error ?? "-")}</td>
</tr>`,
)
.join("");

return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="refresh" content="5" />
<title>Symphony Dashboard</title>
<style>
:root {
color-scheme: light;
font-family: ui-sans-serif, system-ui, sans-serif;
background: #f4efe7;
color: #1e1b18;
}
body {
margin: 0;
padding: 24px;
background:
radial-gradient(circle at top left, rgba(198, 110, 66, 0.16), transparent 28rem),
linear-gradient(180deg, #f8f3eb 0%, #efe4d3 100%);
}
main {
max-width: 1100px;
margin: 0 auto;
}
h1, h2 {
margin: 0 0 12px;
}
.grid {
display: grid;
gap: 16px;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
margin: 20px 0 28px;
}
.card, section {
background: rgba(255, 252, 247, 0.9);
border: 1px solid rgba(59, 44, 32, 0.12);
border-radius: 16px;
box-shadow: 0 10px 30px rgba(74, 46, 20, 0.08);
}
.card {
padding: 18px;
}
.metric {
font-size: 2rem;
font-weight: 700;
}
section {
padding: 20px;
margin-bottom: 18px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
text-align: left;
padding: 10px 8px;
border-bottom: 1px solid rgba(59, 44, 32, 0.12);
vertical-align: top;
}
th {
font-size: 0.875rem;
color: #5f5449;
}
pre {
white-space: pre-wrap;
word-break: break-word;
margin: 0;
}
.muted {
color: #6e6256;
}
</style>
</head>
<body>
<main>
<h1>Symphony Dashboard</h1>
<p class="muted">Generated at ${escapeHtml(snapshot.generated_at)}</p>

<div class="grid">
<div class="card">
<div class="muted">Running</div>
<div class="metric">${snapshot.counts.running}</div>
</div>
<div class="card">
<div class="muted">Retrying</div>
<div class="metric">${snapshot.counts.retrying}</div>
</div>
<div class="card">
<div class="muted">Input Tokens</div>
<div class="metric">${snapshot.codex_totals.input_tokens}</div>
</div>
<div class="card">
<div class="muted">Output Tokens</div>
<div class="metric">${snapshot.codex_totals.output_tokens}</div>
</div>
<div class="card">
<div class="muted">Total Tokens</div>
<div class="metric">${snapshot.codex_totals.total_tokens}</div>
</div>
<div class="card">
<div class="muted">Seconds Running</div>
<div class="metric">${snapshot.codex_totals.seconds_running.toFixed(1)}</div>
</div>
</div>

<section>
<h2>Running Sessions</h2>
<table>
<thead>
<tr>
<th>Issue</th>
<th>State</th>
<th>Session</th>
<th>Turns</th>
<th>Last Event</th>
<th>Last Message</th>
<th>Last Event At</th>
</tr>
</thead>
<tbody>${runningRows}</tbody>
</table>
</section>

<section>
<h2>Retry Queue</h2>
<table>
<thead>
<tr>
<th>Issue</th>
<th>Attempt</th>
<th>Due At</th>
<th>Error</th>
</tr>
</thead>
<tbody>${retryRows}</tbody>
</table>
</section>

<section>
<h2>Rate Limits</h2>
<pre>${escapeHtml(JSON.stringify(snapshot.rate_limits, null, 2) ?? "null")}</pre>
</section>
</main>
</body>
</html>`;
}

function escapeHtml(value: string): string {
return value
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#39;");
}

function toErrorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
}

return String(error);
}
3 changes: 3 additions & 0 deletions tests/observability/dashboard-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ describe("dashboard server", () => {
expect(dashboard.headers["content-type"]).toContain("text/html");
expect(dashboard.body).toContain("Operations Dashboard");
expect(dashboard.body).toContain("ABC-123");
expect(dashboard.body).toContain(
'<meta http-equiv="refresh" content="5" />',
);
expect(dashboard.body).toContain("Running sessions");
expect(dashboard.body).toContain("Runtime / turns");
expect(dashboard.body).toContain("Codex update");
Expand Down
Loading