Repository navigation
feat(cloud-agent-next): emit point-in-time Cloud Agent open stock metric - #6243
Conversation
The outcome aggregate counts only turns whose terminal_at falls in a rolling window, so a tick with no settlement reports settled: 0. An operator cannot tell an idle system from one with work in flight. Add the missing read of the non-terminal population. A single grouped statement reads cloud_agent_session_runs filtered to status in (queued, accepted) and terminal_at is null, joined to retained cloud_agent_sessions, and emits one cloud_agent_open_stock record per tick with per-generation queued/accepted turns and sessions, the oldest age in row state and counts of rows missing the age timestamp. A database failure emits collectionStatus failed, never zeros. It reuses the outcome collector's generation expression, coverage label, retention cutoff and envelope pattern, and dispatches under the existing 3-minute cron in finally after the outcome collection so a query outage in one metric does not blank the other. Admission, retry, cancellation, cleanup and terminalization are untouched. Verified against a real local Worker scheduled tick over Postgres, plus Postgres tests for null timestamps, terminal exclusion, generation split, retention and the failure path. EXPLAIN (ANALYZE, BUFFERS) on a 125k-row representative dataset uses IDX_cloud_agent_session_runs_status_terminal and examines 12 rows.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryThe incremental commit ( Files Reviewed (1 file)
Resolved in this incrementThe prior WARNING (whole-table aggregation in the Postgres open-stock suite) is addressed: Previous Review Summary (commit 1784fa7)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 1784fa7)Status: 1 Issue Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
Files Reviewed (6 files)
Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0 Review guidance: REVIEW.md from base branch |
…isting runs The suite aggregated the whole retained population, so its exact counts and its empty-result assertion only held against a database with no open runs. Eight of nine cases failed once the shared local database held seven open runs. Scope every case through the reader's own retention predicate: fixture sessions are created above the real retained population and reads use a cutoff beneath them, so pre-existing rows are excluded by the same predicate the metric uses. The retention case derives its own cutoffs around the fixture base. No production code or reader signature changes.
What
Emit a new point-in-time observability metric,
cloud_agent_open_stock, from thecloud-agent-nextWorker on the existing 3-minute cron (*/3 * * * *), as a separate log record alongsidecloud_agent_outcome_aggregate.Why
cloud_agent_outcome_aggregatecounts only turns whoseterminal_atfalls in a rolling window, so a tick with no settlement reportssettled: 0. That value is truthful but cannot distinguish an idle system from one with work in flight. Nothing read the non-terminal (queued/accepted) population.This is stock as of now, not a stall detector and not a sandbox-liveness probe.
acceptedmeans dispatched, not sandbox-alive. Age is time in that row state, not "stuck".Shape
One grouped statement over
cloud_agent_session_runsfiltered tostatus in ('queued','accepted') and terminal_at is null, joined to retainedcloud_agent_sessions(same 90-day cutoff as outcomes), split by generation (workspace_prefix = control).Per generation the record carries
queuedTurns,queuedSessions,acceptedTurns,acceptedSessions,oldestQueuedAgeMs,oldestAcceptedAgeMs, plus counts of rows missing the age timestamp (so absent timestamps stay visible and no age is invented). Ages are epoch-millisecond differences computed in TypeScript; no timestamp text parsing.Envelope:
metric/logTag=cloud_agent_open_stock,collectionTimestamp= now (no reporting delay),coverageshared with outcomes,expectedGenerations: ['legacy','control'],limitations, andcollectionStatus: complete | failed. A database failure emits onefailedrecord and never zeros.It reuses
generationExpression,COVERAGE_LABEL,EXPECTED_GENERATIONSandretentionCutofffrom the existing code rather than re-deriving them. Dispatch runs infinallyafter the outcome collection, so a query outage in one metric cannot blank the other. Admission, retry, cancellation, cleanup and terminalization are untouched. No new cron and no wrangler change.Verification
wrangler dev --test-scheduledwith Hyperdrive pointed at this worktree's Postgres; the actual scheduled tick emitted thecloud_agent_open_stockrecord withcollectionStatus: complete, both generations, a null age for the all-null control group, and the terminal/pre-cutoff rows contributing nothing. The same tick still emitted the three outcome records first.failedrecord with nogenerations;getPgDbthrowing produces the same.223 files / 6691 passed / 3 skipped; postgres2 files / 27 passed(null and all-null timestamps, terminal exclusion where a non-nullterminal_aton a queued/accepted row makes the predicate load-bearing, generation split, retention boundary, distinct sessions, raw driver-boundary types).lint0 warnings / 0 errors;typecheckclean;format:checkclean.ANALYZEfirst),EXPLAIN (ANALYZE, BUFFERS)usesIDX_cloud_agent_session_runs_status_terminalwithIndex Cond: status = ANY('{queued,accepted}') AND terminal_at IS NULL, examines 12 rows (0.0096% of history), loops bounded to the 12 open rows via the sessions primary key, no full-history scan and no sessions-first traversal. Planning 0.837 ms, execution 0.194 ms.Honest limitations
workerd, not the Cloudflare edge or production Hyperdrive. Post-merge, Logpush lines withmetric = cloud_agent_open_stock(datasetcloudflare-logpush,ScriptName == cloud-agent-next,EventType == scheduled) must still be confirmed after a normal deploy.