diff --git a/cloudflare-gastown/gastown-grafana-dash-1.json b/cloudflare-gastown/gastown-grafana-dash-1.json index f09e9b3c5..1a3656509 100644 --- a/cloudflare-gastown/gastown-grafana-dash-1.json +++ b/cloudflare-gastown/gastown-grafana-dash-1.json @@ -2479,6 +2479,700 @@ ], "title": "Agent & Review Events", "type": "timeseries" + }, + { + "collapsed": false, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 104 + }, + "id": 200, + "panels": [], + "title": "Reconciler", + "type": "row" + }, + { + "datasource": { + "type": "vertamedia-clickhouse-datasource", + "uid": "bffxugc31cnpcc" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 15, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "smooth", + "lineWidth": 2, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 105 + }, + "id": 201, + "options": { + "legend": { + "calcs": ["mean", "max"], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "time_series", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT $timeSeries AS t, 'events_drained' AS label, SUM(double2 * _sample_interval) / SUM(_sample_interval) AS events_drained FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "rawSql": "SELECT $timeSeries AS t, 'events_drained' AS label, SUM(double2 * _sample_interval) / SUM(_sample_interval) AS events_drained FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "refId": "A", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + } + ], + "title": "Events Drained per Tick", + "type": "timeseries" + }, + { + "datasource": { + "type": "vertamedia-clickhouse-datasource", + "uid": "bffxugc31cnpcc" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "bars", + "fillOpacity": 80, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 0, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "normal" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 105 + }, + "id": 202, + "options": { + "legend": { + "calcs": ["sum"], + "displayMode": "table", + "placement": "right", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "time_series", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT $timeSeries AS t, arrayJoin(JSONExtractKeysAndValues(blob10, 'Float64')) AS kv, kv.1 AS label, SUM(kv.2 * _sample_interval) AS count FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' AND blob10 != '' AND blob10 != '{}' GROUP BY t, label ORDER BY t", + "rawSql": "SELECT $timeSeries AS t, arrayJoin(JSONExtractKeysAndValues(blob10, 'Float64')) AS kv, kv.1 AS label, SUM(kv.2 * _sample_interval) AS count FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' AND blob10 != '' AND blob10 != '{}' GROUP BY t, label ORDER BY t", + "refId": "A", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + } + ], + "title": "Actions Emitted per Tick by Type", + "type": "timeseries" + }, + { + "datasource": { + "type": "vertamedia-clickhouse-datasource", + "uid": "bffxugc31cnpcc" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 15, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "smooth", + "lineWidth": 2, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "attempted" + }, + "properties": [ + { + "id": "color", + "value": { + "fixedColor": "blue", + "mode": "fixed" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "succeeded" + }, + "properties": [ + { + "id": "color", + "value": { + "fixedColor": "green", + "mode": "fixed" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "failed" + }, + "properties": [ + { + "id": "color", + "value": { + "fixedColor": "red", + "mode": "fixed" + } + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 113 + }, + "id": 203, + "options": { + "legend": { + "calcs": ["sum"], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "time_series", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT $timeSeries AS t, 'attempted' AS label, SUM(double4 * _sample_interval) / SUM(_sample_interval) AS attempted FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "rawSql": "SELECT $timeSeries AS t, 'attempted' AS label, SUM(double4 * _sample_interval) / SUM(_sample_interval) AS attempted FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "refId": "A", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + }, + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "time_series", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT $timeSeries AS t, 'succeeded' AS label, SUM(double5 * _sample_interval) / SUM(_sample_interval) AS succeeded FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "rawSql": "SELECT $timeSeries AS t, 'succeeded' AS label, SUM(double5 * _sample_interval) / SUM(_sample_interval) AS succeeded FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "refId": "B", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + }, + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "time_series", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT $timeSeries AS t, 'failed' AS label, SUM(double6 * _sample_interval) / SUM(_sample_interval) AS failed FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "rawSql": "SELECT $timeSeries AS t, 'failed' AS label, SUM(double6 * _sample_interval) / SUM(_sample_interval) AS failed FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "refId": "C", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + } + ], + "title": "Side Effects (attempted / succeeded / failed)", + "type": "timeseries" + }, + { + "datasource": { + "type": "vertamedia-clickhouse-datasource", + "uid": "bffxugc31cnpcc" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 1 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 4, + "x": 12, + "y": 113 + }, + "id": 204, + "options": { + "colorMode": "background", + "graphMode": "area", + "justifyMode": "auto", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": ["sum"], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "time_series", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT $timeSeries AS t, 'violations' AS label, SUM(double7 * _sample_interval) AS violations FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "rawSql": "SELECT $timeSeries AS t, 'violations' AS label, SUM(double7 * _sample_interval) AS violations FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "refId": "A", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + } + ], + "title": "Invariant Violations", + "type": "stat" + }, + { + "datasource": { + "type": "vertamedia-clickhouse-datasource", + "uid": "bffxugc31cnpcc" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 15, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "smooth", + "lineWidth": 2, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "line+area" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "transparent", + "value": 0 + }, + { + "color": "red", + "value": 500 + } + ] + }, + "unit": "ms" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 16, + "y": 113 + }, + "id": 205, + "options": { + "legend": { + "calcs": ["mean", "max"], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "time_series", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT $timeSeries AS t, 'wall_clock_ms' AS label, SUM(double1 * _sample_interval) / SUM(_sample_interval) AS wall_clock_ms FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "rawSql": "SELECT $timeSeries AS t, 'wall_clock_ms' AS label, SUM(double1 * _sample_interval) / SUM(_sample_interval) AS wall_clock_ms FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' GROUP BY t ORDER BY t", + "refId": "A", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + } + ], + "title": "Reconciler Wall Clock Time", + "type": "timeseries" + }, + { + "datasource": { + "type": "vertamedia-clickhouse-datasource", + "uid": "bffxugc31cnpcc" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "max": 200, + "min": 0, + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "yellow", + "value": 25 + }, + { + "color": "red", + "value": 50 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 121 + }, + "id": 206, + "options": { + "minVizHeight": 75, + "minVizWidth": 75, + "orientation": "auto", + "reduceOptions": { + "calcs": ["lastNotNull"], + "fields": "", + "values": false + }, + "showThresholdLabels": true, + "showThresholdMarkers": true, + "sizing": "auto" + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "adHocFilters": [], + "adHocValuesQuery": "", + "add_metadata": true, + "contextWindowSize": "10", + "dateTimeColDataType": "timestamp", + "dateTimeType": "DATETIME", + "editorMode": "sql", + "extrapolate": true, + "format": "table", + "interval": "", + "intervalFactor": 1, + "nullifySparse": false, + "query": "SELECT SUM(double8 * _sample_interval) / SUM(_sample_interval) AS pending_events FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' ORDER BY timestamp DESC LIMIT 1", + "rawSql": "SELECT SUM(double8 * _sample_interval) / SUM(_sample_interval) AS pending_events FROM gastown_events WHERE $timeFilter AND blob1 = 'reconciler_tick' ORDER BY timestamp DESC LIMIT 1", + "refId": "A", + "round": "0s", + "showFormattedSQL": false, + "showHelp": false, + "skip_comments": true, + "table": "gastown_events", + "useWindowFuncForMacros": true + } + ], + "title": "Pending Event Queue Depth", + "type": "gauge" } ], "preload": false, diff --git a/cloudflare-gastown/src/dos/Town.do.ts b/cloudflare-gastown/src/dos/Town.do.ts index 0cf7c974a..4bd859a99 100644 --- a/cloudflare-gastown/src/dos/Town.do.ts +++ b/cloudflare-gastown/src/dos/Town.do.ts @@ -30,7 +30,7 @@ import * as scheduling from './town/scheduling'; import * as events from './town/events'; import * as reconciler from './town/reconciler'; import { applyAction } from './town/actions'; -import type { ApplyActionContext } from './town/actions'; +import type { Action, ApplyActionContext } from './town/actions'; import { buildRefinerySystemPrompt } from '../prompts/refinery-system.prompt'; import { GitHubPRStatusSchema, GitLabMRStatusSchema } from '../util/platform-pr.util'; @@ -46,6 +46,7 @@ import { review_metadata } from '../db/tables/review-metadata.table'; import { escalation_metadata } from '../db/tables/escalation-metadata.table'; import { convoy_metadata } from '../db/tables/convoy-metadata.table'; import { bead_dependencies } from '../db/tables/bead-dependencies.table'; +import { town_events, TownEventRecord } from '../db/tables/town-events.table'; import { agent_nudges, AgentNudgeRecord, @@ -2891,10 +2892,16 @@ export class TownDO extends DurableObject { townId, row.bead_id ); - events.upsertContainerStatus(this.sql, row.bead_id, { - status: containerInfo.status, - exit_reason: containerInfo.exitReason, - }); + // Skip inserting events for 'running' — it's the steady-state and + // a no-op in applyEvent, so recording it just bloats the event table + // (~720 events/hour/agent). Non-running statuses (stopped, error, + // unknown) still get inserted so the reconciler can detect and handle them. + if (containerInfo.status !== 'running') { + events.upsertContainerStatus(this.sql, row.bead_id, { + status: containerInfo.status, + exit_reason: containerInfo.exitReason, + }); + } } catch (err) { console.warn( `${TOWN_LOG} alarm: container status check failed for agent=${row.bead_id}`, @@ -3006,6 +3013,31 @@ export class TownDO extends DurableObject { metrics.pendingEventCount = events.pendingEventCount(this.sql); this._lastReconcilerMetrics = metrics; + // Emit reconciler metrics to Analytics Engine for Grafana dashboards. + // Field mapping: + // double1 = wallClockMs + // double2 = eventsDrained + // double3 = actionsEmitted + // double4 = sideEffectsAttempted + // double5 = sideEffectsSucceeded + // double6 = sideEffectsFailed + // double7 = invariantViolations + // double8 = pendingEventCount + // blob10 = JSON-encoded actionsByType breakdown + this.emitEvent({ + event: 'reconciler_tick', + townId, + durationMs: metrics.wallClockMs, + value: metrics.eventsDrained, + double3: metrics.actionsEmitted, + double4: metrics.sideEffectsAttempted, + double5: metrics.sideEffectsSucceeded, + double6: metrics.sideEffectsFailed, + double7: metrics.invariantViolations, + double8: metrics.pendingEventCount, + label: JSON.stringify(metrics.actionsByType), + }); + // ── Phase 3: Housekeeping (independent, all parallelizable) ──── await Promise.allSettled([ this.deliverPendingMail().catch(err => @@ -3684,6 +3716,144 @@ export class TownDO extends DurableObject { }; } + // DEBUG: replay events from a time range, apply them to state, run the + // reconciler, and return computed actions. Uses a savepoint + rollback so + // no state is permanently modified. + async debugReplayEvents( + from: string, + to: string + ): Promise<{ + eventsReplayed: number; + actions: Action[]; + stateSnapshot: { + agents: unknown[]; + nonTerminalBeads: unknown[]; + }; + }> { + this.sql.exec('SAVEPOINT debug_replay_events'); + try { + // Query ALL events in the time range regardless of processed_at + const rangeEvents = TownEventRecord.array().parse([ + ...query( + this.sql, + /* sql */ ` + SELECT ${town_events.event_id}, ${town_events.event_type}, + ${town_events.agent_id}, ${town_events.bead_id}, + ${town_events.payload}, ${town_events.created_at}, + ${town_events.processed_at} + FROM ${town_events} + WHERE ${town_events.created_at} >= ? + AND ${town_events.created_at} <= ? + ORDER BY ${town_events.created_at} ASC + `, + [from, to] + ), + ]); + + // Apply each event to reconstruct state transitions + for (const event of rangeEvents) { + reconciler.applyEvent(this.sql, event); + } + + // Run reconciler against the resulting state + const actions = reconciler.reconcile(this.sql); + + // Capture a state snapshot before rollback + const agentSnapshot = [ + ...query( + this.sql, + /* sql */ ` + SELECT ${agent_metadata.bead_id}, + ${agent_metadata.role}, + ${agent_metadata.status}, + ${agent_metadata.current_hook_bead_id}, + ${agent_metadata.dispatch_attempts}, + ${agent_metadata.last_activity_at} + FROM ${agent_metadata} + `, + [] + ), + ]; + + const beadSnapshot = [ + ...query( + this.sql, + /* sql */ ` + SELECT ${beads.bead_id}, + ${beads.type}, + ${beads.status}, + ${beads.title}, + ${beads.assignee_agent_bead_id}, + ${beads.updated_at} + FROM ${beads} + WHERE ${beads.status} NOT IN ('closed', 'failed') + AND ${beads.type} != 'agent' + ORDER BY ${beads.type}, ${beads.status} + `, + [] + ), + ]; + + return { + eventsReplayed: rangeEvents.length, + actions, + stateSnapshot: { + agents: agentSnapshot, + nonTerminalBeads: beadSnapshot, + }, + }; + } finally { + this.sql.exec('ROLLBACK TO SAVEPOINT debug_replay_events'); + this.sql.exec('RELEASE SAVEPOINT debug_replay_events'); + } + } + + // DEBUG: dry-run the reconciler against current state, returning actions + // it would emit without applying them. Drains pending events first (same + // as the real alarm loop) inside a savepoint that is rolled back, so the + // endpoint remains fully side-effect-free. + async debugDryRun(): Promise<{ + actions: Action[]; + metrics: Pick< + reconciler.ReconcilerMetrics, + 'actionsEmitted' | 'actionsByType' | 'pendingEventCount' | 'eventsDrained' + >; + }> { + // Use a savepoint so we can drain events (which mutates state) + // then roll back without permanent side effects + this.sql.exec('SAVEPOINT debug_dry_run'); + try { + // Phase 0: Drain and apply pending events (same as real alarm loop) + const pending = events.drainEvents(this.sql); + for (const event of pending) { + reconciler.applyEvent(this.sql, event); + events.markProcessed(this.sql, event.event_id); + } + + // Phase 1: Reconcile against now-current state + const actions = reconciler.reconcile(this.sql); + const pendingEventCount = events.pendingEventCount(this.sql); + const actionsByType: Record = {}; + for (const a of actions) { + actionsByType[a.type] = (actionsByType[a.type] ?? 0) + 1; + } + + return { + actions, + metrics: { + actionsEmitted: actions.length, + actionsByType, + pendingEventCount, + eventsDrained: pending.length, + }, + }; + } finally { + // Roll back all state mutations — this is a dry run + this.sql.exec('ROLLBACK TO SAVEPOINT debug_dry_run'); + this.sql.exec('RELEASE SAVEPOINT debug_dry_run'); + } + } + // DEBUG: concise non-terminal bead summary — remove after debugging async debugBeadSummary(): Promise { return [ diff --git a/cloudflare-gastown/src/gastown.worker.ts b/cloudflare-gastown/src/gastown.worker.ts index 6c8efab71..f5118d0d6 100644 --- a/cloudflare-gastown/src/gastown.worker.ts +++ b/cloudflare-gastown/src/gastown.worker.ts @@ -206,6 +206,34 @@ app.get('/debug/towns/:townId/status', async c => { return c.json({ alarmStatus, agentMeta, beadSummary }); }); +app.post('/debug/towns/:townId/reconcile-dry-run', async c => { + const townId = c.req.param('townId'); + const town = getTownDOStub(c.env, townId); + // eslint-disable-next-line @typescript-eslint/await-thenable -- DO RPC returns promise at runtime + const result = await town.debugDryRun(); + return c.json(result); +}); + +app.post('/debug/towns/:townId/replay-events', async c => { + const townId = c.req.param('townId'); + const body = (await c.req.json()) as { from?: string; to?: string }; + if (!body.from || !body.to) { + return c.json({ error: 'Missing required fields: from, to (ISO timestamps)' }, 400); + } + const fromDate = new Date(body.from); + const toDate = new Date(body.to); + if (Number.isNaN(fromDate.getTime()) || Number.isNaN(toDate.getTime())) { + return c.json({ error: 'Invalid date format. Use ISO 8601 timestamps.' }, 400); + } + if (fromDate > toDate) { + return c.json({ error: '"from" must be before or equal to "to"' }, 400); + } + const town = getTownDOStub(c.env, townId); + // eslint-disable-next-line @typescript-eslint/await-thenable -- DO RPC returns promise at runtime + const result = await town.debugReplayEvents(body.from, body.to); + return c.json(result); +}); + // ── Town ID + Auth ────────────────────────────────────────────────────── // All rig routes live under /api/towns/:townId/rigs/:rigId so the townId // is always available from the URL path. diff --git a/cloudflare-gastown/src/util/analytics.util.ts b/cloudflare-gastown/src/util/analytics.util.ts index 367960b10..99d1e54c0 100644 --- a/cloudflare-gastown/src/util/analytics.util.ts +++ b/cloudflare-gastown/src/util/analytics.util.ts @@ -42,6 +42,16 @@ export type GastownEventData = { durationMs?: number; value?: number; label?: string; + // Additional doubles for reconciler_tick events (double3–double10). + // Analytics Engine supports up to 20 doubles per data point. + double3?: number; + double4?: number; + double5?: number; + double6?: number; + double7?: number; + double8?: number; + double9?: number; + double10?: number; }; /** @@ -70,7 +80,18 @@ export function writeEvent( data.role ?? '', // blob12 data.beadType ?? '', // blob13 ], - doubles: [data.durationMs ?? 0, data.value ?? 0], + doubles: [ + data.durationMs ?? 0, // double1 + data.value ?? 0, // double2 + data.double3 ?? 0, // double3 + data.double4 ?? 0, // double4 + data.double5 ?? 0, // double5 + data.double6 ?? 0, // double6 + data.double7 ?? 0, // double7 + data.double8 ?? 0, // double8 + data.double9 ?? 0, // double9 + data.double10 ?? 0, // double10 + ], indexes: [data.event], }); } catch { diff --git a/src/app/(app)/claw/components/CreateInstanceCard.tsx b/src/app/(app)/claw/components/CreateInstanceCard.tsx index 29b330c9c..a48c54584 100644 --- a/src/app/(app)/claw/components/CreateInstanceCard.tsx +++ b/src/app/(app)/claw/components/CreateInstanceCard.tsx @@ -1,7 +1,7 @@ 'use client'; import { useEffect, useMemo, useRef, useState } from 'react'; -import { usePostHog } from 'posthog-js/react'; +import { useFeatureFlagVariantKey, usePostHog } from 'posthog-js/react'; import { useQuery } from '@tanstack/react-query'; import { toast } from 'sonner'; import type { useKiloClawMutations } from '@/hooks/useKiloClaw'; @@ -25,6 +25,9 @@ export function CreateInstanceCard({ mutations: ClawMutations; onProvisionStart?: () => void; }) { + // Evaluate the landing-page experiment flag so PostHog attaches + // $feature/button-vs-card to events fired in this component. + useFeatureFlagVariantKey('button-vs-card'); const posthog = usePostHog(); const trpc = useTRPC(); const { data: billingStatus } = useQuery(trpc.kiloclaw.getBillingStatus.queryOptions());