Skip to content
Merged
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
183 changes: 157 additions & 26 deletions src/dashboard-page.ts

Large diffs are not rendered by default.

76 changes: 45 additions & 31 deletions src/dashboard-reader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { taskCloseoutSchema } from './task-closeout.js';
import { LiveExecution, type ExecutionObservation } from './live-execution.js';
import { readTaskCloseout } from './task-closeout.js';
import { intakeRouteSchema, presentTaskSource, type InputProvenance, type ReportEscalation, type ReportRouting, type ReportSource, type Task, type TaskResource } from './domain.js';
import { ProjectLedger, type ProjectState } from './project.js';
import { Store, type Inbox } from './store.js';
Expand Down Expand Up @@ -70,10 +70,16 @@ export interface DashboardTimelineEntry {
phase: string | null;
}

export type DashboardWorkSummary =
| { kind: 'event'; event: DashboardExecutionEvent }
| { kind: 'report'; report: DashboardReport }
| { kind: 'waiting'; text: string; createdAt: null }
| { kind: 'no-record' };

export interface ProjectTaskCard {
id: string;
projectId: string;
role: 'coordinator' | 'worker' | 'intake';
projectId: string | null;
role: 'coordinator' | 'worker' | 'intake' | null;
title: string;
sourceLabel: string;
sourceKind: Task['source']['kind'];
Expand All @@ -82,6 +88,9 @@ export interface ProjectTaskCard {
issueId: string | null;
status: Task['status'];
executionStatus: Task['executionStatus'];
reason: string | null;
lifecycle: ReturnType<typeof readTaskCloseout>['lifecycle'];
workSummary: DashboardWorkSummary;
threadId: string | null;
turnId: string | null;
chatId: string | null;
Expand All @@ -94,8 +103,8 @@ export interface ProjectTaskCard {
createdAt: number;
updatedAt: number;
latestReport: DashboardReport | null;
completion: z.infer<typeof taskCloseoutSchema>['lifecycle'];
completionHistory: z.infer<typeof taskCloseoutSchema>['completionHistory'];
completion: ReturnType<typeof readTaskCloseout>['lifecycle'];
completionHistory: ReturnType<typeof readTaskCloseout>['completionHistory'];
cleanup: { operationId: string; kind: string; scopeId: string; state: string; reason: string | null; directoryRemoved: boolean; branchRemoved: boolean }[];
}

Expand Down Expand Up @@ -160,13 +169,12 @@ function dashboardEvent(event: VisibleExecution): DashboardExecutionEvent | null
return { ...event, type: eventType(event), text: boundedDashboardText(text) } as DashboardExecutionEvent;
}

type DashboardOwnership = { projectId: string; role: ProjectTaskCard['role'] };
type DashboardOwnership = { projectId: string; role: NonNullable<ProjectTaskCard['role']> };
interface DashboardContext {
tasks: Task[];
states: Map<string, ProjectState>;
ownership: Map<string, DashboardOwnership>;
reportsByTask: Map<string, DashboardReport[]>;
latestActivity: Map<string, VisibleExecution>;
}
const membershipMetadataSchema = z.strictObject({ projectId: z.string(), role: z.enum(['coordinator', 'worker']) });

Expand Down Expand Up @@ -219,7 +227,8 @@ export class ProjectDashboardReader {
const list = reportsByTask.get(input.provenance.taskId) ?? [];
list.push(report); reportsByTask.set(input.provenance.taskId, list);
}
return { tasks, states, ownership, reportsByTask, latestActivity: new Map(tasks.flatMap(task => { const event = this.execution.current(task.id, task.threadId).at(-1); return event ? [[task.id, event]] : []; })) };
for (const reports of reportsByTask.values()) reports.sort((a, b) => a.createdAt - b.createdAt);
return { tasks, states, ownership, reportsByTask };
}

private reportsForTask(taskId: string, context: DashboardContext): DashboardReport[] {
Expand All @@ -229,45 +238,50 @@ export class ProjectDashboardReader {
return context.reportsByTask.get(taskId) ?? [];
}

private card(task: Task, projectId: string, role: ProjectTaskCard['role'], context: DashboardContext, issueId: string | null = null): ProjectTaskCard {
const latestActivity = context.latestActivity.get(task.id) ?? null;
private card(task: Task, context: DashboardContext, issueId: string | null = null): ProjectTaskCard {
const ownership = context.ownership.get(task.id);
const latestActivity = this.execution.latestPublished(task.id, task.threadId);
const visibleActivity = latestActivity ? dashboardEvent(latestActivity) : null;
const reports = this.reportsForTask(task.id, context);
const waiting = task.requests.map(request => `${request.kind}: ${boundedDashboardText(request.prompt)}`);
const waitingReason = waiting[0] ?? (task.reason ? boundedDashboardText(task.reason) : null) ?? (task.recovery.lastError ? boundedDashboardText(task.recovery.lastError) : null);
const latestReport = reports.at(-1) ?? null;
const workSummary: DashboardWorkSummary = visibleActivity ? { kind: 'event', event: visibleActivity }
: latestReport ? { kind: 'report', report: latestReport }
: waitingReason ? { kind: 'waiting', text: waitingReason, createdAt: null } : { kind: 'no-record' };
const source = presentTaskSource(task.source, 200);
const repository = task.source.kind === 'github' ? task.source.issue.repository : task.resources.find(resource => resource.kind !== 'project')?.repository.name ?? null;
const rawCloseout = this.store.meta(`task-closeout:${task.id}`);
const closeout = rawCloseout ? taskCloseoutSchema.parse(JSON.parse(rawCloseout)) : null;
const closeout = readTaskCloseout(this.store, task.id);
return {
completion: closeout?.lifecycle ?? { kind: 'open' }, completionHistory: closeout?.completionHistory ?? [],
cleanup: closeout?.operations.flatMap(operation => operation.targets.map(target => ({ operationId: operation.id, kind: operation.kind, scopeId: target.scope.id, state: target.state, reason: target.reason, directoryRemoved: target.directoryRemoved, branchRemoved: target.branchRemoved }))) ?? [],
id: task.id, projectId, role, title: source.title, sourceLabel: source.label, sourceKind: task.source.kind, sourceUrl: source.url, repository,
completion: closeout.lifecycle,
completionHistory: closeout.completionHistory,
cleanup: closeout.operations.flatMap(operation => operation.targets.map(target => ({ operationId: operation.id, kind: operation.kind, scopeId: target.scope.id, state: target.state, reason: target.reason, directoryRemoved: target.directoryRemoved, branchRemoved: target.branchRemoved }))),
id: task.id, projectId: ownership?.projectId ?? null, role: ownership?.role ?? null, title: source.title, sourceLabel: source.label, sourceKind: task.source.kind, sourceUrl: source.url, repository,
issueId: issueId ?? (task.source.kind === 'github' ? task.source.issue.id : null),
status: task.status, executionStatus: task.executionStatus, currentTurn: task.turnId ?? visibleActivity?.turnId ?? null,
status: task.status, executionStatus: task.executionStatus, currentTurn: task.turnId,
reason: task.reason ? boundedDashboardText(task.reason) : null, lifecycle: closeout.lifecycle, workSummary,
threadId: task.threadId, turnId: task.turnId, chatId: task.chatId, resources: dashboardResources(task.resources),
latestActivity: visibleActivity, waitingReason: waiting[0] ?? (task.reason ? boundedDashboardText(task.reason) : null) ?? (task.recovery.lastError ? boundedDashboardText(task.recovery.lastError) : null), requests: task.requests.map(request => ({ kind: request.kind, prompt: boundedDashboardText(request.prompt) })),
recovery: task.recovery, createdAt: task.createdAt, updatedAt: task.updatedAt, latestReport: reports.at(-1) ?? null,
latestActivity: visibleActivity, waitingReason, requests: task.requests.map(request => ({ kind: request.kind, prompt: boundedDashboardText(request.prompt) })),
recovery: task.recovery, createdAt: task.createdAt, updatedAt: task.updatedAt, latestReport,
};
}

projects(tasks = this.store.tasks()): ProjectDashboard[] {
overview(tasks = this.store.tasks()): { tasks: ProjectTaskCard[]; projects: ProjectDashboard[] } {
const context = this.context(tasks);
const cards = new Map<string, ProjectTaskCard[]>();
for (const task of tasks) {
const projected = tasks.map(task => {
const ownership = context.ownership.get(task.id);
if (!ownership) continue;
const state = context.states.get(ownership.projectId);
if (!state) continue;
const issueId = state.members.find(member => member.taskId === task.id)?.issueId ?? null;
const list = cards.get(state.id) ?? [];
list.push(this.card(task, state.id, ownership.role, context, issueId)); cards.set(state.id, list);
}
return [...context.states.values()].sort((a, b) => a.name.localeCompare(b.name)).map(state => ({
const state = ownership ? context.states.get(ownership.projectId) : null;
return this.card(task, context, state?.members.find(member => member.taskId === task.id)?.issueId ?? null);
});
const projects = [...context.states.values()].sort((a, b) => a.name.localeCompare(b.name)).map(state => ({
id: state.id, name: state.name, directory: state.directory, coordinatorTaskId: state.coordinatorTaskId,
members: state.members, tasks: (cards.get(state.id) ?? []).sort((a, b) => b.updatedAt - a.updatedAt || a.id.localeCompare(b.id)),
members: state.members, tasks: projected.filter(task => task.projectId === state.id).sort((a, b) => b.updatedAt - a.updatedAt || a.id.localeCompare(b.id)),
}));
return { tasks: projected, projects };
}

projects(tasks = this.store.tasks()): ProjectDashboard[] { return this.overview(tasks).projects; }

project(projectId: string): ProjectDashboard | null { return this.projects().find(project => project.id === projectId) ?? null; }

timeline(taskId: string, events: VisibleExecution[] = [], limit = 500): DashboardTimelineEntry[] {
Expand Down Expand Up @@ -313,7 +327,7 @@ export class ProjectDashboardReader {
const project = ownership ? context.states.get(ownership.projectId) : null;
const reports = this.reportsForTask(taskId, context);
return {
task: ownership && project ? this.card(task, project.id, ownership.role, context, project.members.find(member => member.taskId === taskId)?.issueId ?? null) : null,
task: this.card(task, context, project?.members.find(member => member.taskId === taskId)?.issueId ?? null),
threadId: task.threadId, projectId: ownership?.projectId ?? null,
inputs: this.store.inputs(taskId), outputs: this.store.outputs(taskId), cursor: observation.cursor, history: observation.history, events: observation.entries.flatMap(event => { const visible = dashboardEvent(event); return visible ? [visible] : []; }), timeline: this.timeline(taskId, observation.entries), reports,
diagnostics: this.store.diagnostics().filter(row => row.scope === taskId || row.scope.endsWith(':' + taskId)),
Expand Down
22 changes: 5 additions & 17 deletions src/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { join } from 'node:path';
import { hostname, networkInterfaces } from 'node:os';
import { z } from 'zod';
import { Store } from './store.js';
import { boundedDashboardText, dashboardResources, ProjectDashboardReader } from './dashboard-reader.js';
import { errorMessage, presentTaskSource } from './domain.js';
import { boundedDashboardText, ProjectDashboardReader } from './dashboard-reader.js';
import { errorMessage } from './domain.js';
import { dashboardHtml, dashboardCss, dashboardScript } from './dashboard-page.js';

const heartbeatSchema = z.object({ pid: z.number(), lastSeenAt: z.number(), intervalMs: z.number(), status: z.enum(['running', 'stopped']) });
Expand All @@ -19,13 +19,11 @@ export async function startDashboard(options: { execution?: LiveExecution; dataD
const clients = new Set<ServerResponse>();
let dashboardReader: ProjectDashboardReader | null = null;
const readerFor = (db: Store) => dashboardReader ??= new ProjectDashboardReader(db, execution);
let readModelCache: { version: number; cursor: string; expiresAt: number; tasks: ReturnType<Store['tasks']>; projects: ReturnType<ProjectDashboardReader['projects']>; diagnostics: ReturnType<Store['diagnostics']> } | null = null;
let readModelCache: { version: number; cursor: string; expiresAt: number; tasks: ReturnType<ProjectDashboardReader['overview']>['tasks']; projects: ReturnType<ProjectDashboardReader['projects']>; diagnostics: ReturnType<Store['diagnostics']> } | null = null;
const readStore = () => {
if (!store && existsSync(join(options.dataDir, 'tasks.sqlite'))) store = new Store(options.dataDir, { readOnly: true });
return store;
};
// Open the read-only connection before listening so the first API request measures the
// dashboard query rather than SQLite connection and schema setup.
readStore();
const json = (response: ServerResponse, status: number, value: unknown) => {
response.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
Expand All @@ -36,8 +34,7 @@ export async function startDashboard(options: { execution?: LiveExecution; dataD
const db = readStore();
const version = db?.dataVersion() ?? 0;
if (readModelCache && readModelCache.version === version && readModelCache.cursor === execution.cursor && readModelCache.expiresAt > now) return readModelCache;
const tasks = db?.tasks() ?? [];
const projects = db ? readerFor(db).projects(tasks) : [];
const { tasks, projects } = db ? readerFor(db).overview() : { tasks: [], projects: [] };
const diagnostics = db?.diagnostics() ?? [];
readModelCache = { version, cursor: execution.cursor, expiresAt: now + 250, tasks, projects, diagnostics };
return readModelCache;
Expand All @@ -50,16 +47,7 @@ export async function startDashboard(options: { execution?: LiveExecution; dataD
const heartbeat = rawHeartbeat ? heartbeatSchema.safeParse(JSON.parse(rawHeartbeat)) : null;
const value = {
dataDir: options.dataDir, now, runtime: heartbeat?.success ? heartbeat.data : null,
tasks: model.tasks.map(task => ({
id: task.id, title: presentTaskSource(task.source, 200).title,
sourceLabel: presentTaskSource(task.source, 200).label,
sourceKind: task.source.kind, sourceUrl: task.source.kind === 'github' ? task.source.issue.url : null,
status: task.status, executionStatus: task.executionStatus, reason: task.reason,
threadId: task.threadId, turnId: task.turnId, chatId: task.chatId,
createdAt: task.createdAt, updatedAt: task.updatedAt, recovery: task.recovery,
requests: task.requests.map(request => ({ kind: request.kind, prompt: boundedDashboardText(request.prompt) })),
resources: dashboardResources(task.resources),
})), projects: model.projects, diagnostics: model.diagnostics,
tasks: model.tasks, projects: model.projects, diagnostics: model.diagnostics,
};
return value;
};
Expand Down
3 changes: 3 additions & 0 deletions src/live-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export class LiveExecution {
current(taskId: string, threadId?: string | null): VisibleExecution[] {
return [...this.items.values()].flatMap(item => item.kind === 'snapshot' && item.event.taskId === taskId && (threadId === undefined || item.event.threadId === threadId) ? [item.event] : []);
}
latestPublished(taskId: string, threadId: string | null): VisibleExecution | null {
return this.changes.findLast(change => change.event.taskId === taskId && change.event.threadId === threadId)?.event ?? null;
}
revision(taskId: string, threadId?: string | null): number {
return Math.max(0, ...[...this.items.values()].map(item => item.kind === 'snapshot' ? item.event : item).filter(item => item.taskId === taskId && (threadId === undefined || item.threadId === threadId)).map(item => item.seq));
}
Expand Down
7 changes: 5 additions & 2 deletions src/task-closeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export const taskCloseoutSchema = z.strictObject({ version: z.number().default(1
lifecycle: z.discriminatedUnion('kind', [z.strictObject({ kind: z.literal('open') }), completionSchema]),
scopes: z.array(scopeSchema), operations: z.array(operationSchema), predecessorTaskId: z.string().nullable() });
type Ledger = z.infer<typeof taskCloseoutSchema>;
export function readTaskCloseout(store: Store, taskId: string): Ledger {
const value = store.meta(`task-closeout:${taskId}`);
return value ? taskCloseoutSchema.parse(JSON.parse(value)) : { version: 2, completionHistory: [], taskId, projectId: null, lifecycle: { kind: 'open' }, scopes: [], operations: [], predecessorTaskId: null };
}
type Scope = z.infer<typeof scopeSchema>;
type Target = z.infer<typeof targetSchema>;
export type CloseoutOperation = z.infer<typeof operationSchema>;
Expand Down Expand Up @@ -129,8 +133,7 @@ export class TaskCloseout {
protectedPaths(): string[];
}) { this.migrate(); }
read(taskId: string): Ledger {
const value = this.store.meta(`task-closeout:${taskId}`);
return value ? taskCloseoutSchema.parse(JSON.parse(value)) : { version: 2, completionHistory: [], taskId, projectId: null, lifecycle: { kind: 'open' }, scopes: [], operations: [], predecessorTaskId: null };
return readTaskCloseout(this.store, taskId);
}
private save(value: Ledger) { this.store.setMeta(`task-closeout:${value.taskId}`, JSON.stringify(taskCloseoutSchema.parse(value))); }
private status(taskId: string) {
Expand Down
Loading