diff --git a/src/changelog.d/added-live-run-line.md b/src/changelog.d/added-live-run-line.md new file mode 100644 index 0000000000..fc6a6839b6 --- /dev/null +++ b/src/changelog.d/added-live-run-line.md @@ -0,0 +1 @@ +- Agent cards and the transcript pane header show a live per-run line while an agent is working — `$cost · tokens · elapsed · tests ✓/✗/– · N files` — built from the token collector's existing per-agent cumulative figures (delta since the kick was delivered, so a reload does not reset it) plus the last test-runner result and `N files changed` summary visible in the pane. `GET /api/status` carries it as `agents[].run` and the governor strip gains a fleet `running N · $X this hour` tile from the new top-level `fleetRun` ([#14576](https://github.com/hivecommons/hive/issues/14576)). diff --git a/src/docs/api-reference.md b/src/docs/api-reference.md index 6ca064ebea..4b8a06642f 100644 --- a/src/docs/api-reference.md +++ b/src/docs/api-reference.md @@ -13,17 +13,17 @@ History point limits, timestamp cutoffs, incremental cost reads and ETag/304 beh | `GET` | `/api/version` | Dashboard auth/session | Build/version metadata; includes `upgradeMarker` (`target`, `current`, `attempts`, `maxAttempts`, `failed`, `requestedAt`, `lastError`) while a self-upgrade is in flight or has failed ([#6765](https://github.com/hivecommons/hive/issues/6765)), the hub-delivered `upgradePolicy` (`hub_managed`, `spoke_managed`, `schedule`, `schedule_hour`, `schedule_timezone`, `schedule_weekday`, `paused`, `branch`, `channel`, `target_sha`, `target_resolved`, `armed_target`, and `next_update_at` — the hub's RFC3339 ETA for the next promotion into the `stable` channel, omitted when unknown ([#10256](https://github.com/hivecommons/hive/issues/10256))) when available, and an `autoUpdate` object (`enabled`, `state` — one of `disabled`/`up_to_date`/`behind`/`retrying`/`failed`/`unknown` — `healthy`, `period`, `targetBranch`, `targetCommit`, `currentCommit`, `commitsBehind`, `lastAttemptAt`, `lastError`, `detail`, and `nextUpdateAt` — the hub's RFC3339 ETA for the next update, omitted when unknown, disabled or paused ([#10257](https://github.com/hivecommons/hive/issues/10257))) that never reports a failed or unknown update as healthy ([#6962](https://github.com/hivecommons/hive/issues/6962), [#6963](https://github.com/hivecommons/hive/issues/6963)) | `pkg/dashboard/api.go:42` | | `GET` | `/api/version/release-notes` | Dashboard auth/session | Changelog gap between two revisions: `?from=&to=` returns `{from:{sha,version?}, to:{sha,ref,version?}, sections:[{version,date,categories:{added,changed,fixed,security,deprecated}}], unreleased?, truncated, source, error?}` — the `CHANGELOG.md` release sections present at `to` and absent at `from`, newest first, plus (when `to` is not a release tag) the `changelog.d/` fragments new at `to` under an `Unreleased (in this build)` section. Fetch failures return `source: "unavailable"` with `error` rather than an error status; results are cached per (from, to) for an hour ([#11065](https://github.com/hivecommons/hive/issues/11065)) | `pkg/dashboard/api.go:43` | | `POST` | `/api/release-channel` | Owner only | Hosted spoke self-service release-channel selector; relays `stable`/`candidate`/`edge` to the hub's existing switch-branch endpoint with the spoke dashboard-token proof, and reports the requested channel as pending until the Deployment image lands | `pkg/dashboard/api.go:64` | -| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1478` | -| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1479` | -| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1480` | -| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1486` | -| `GET` | `/api/status` | Dashboard auth/session | Dashboard aggregate status; `labelPresets[]` (`{key,label,description,labels[],states[]}`) lists the built-in label-filter presets for the Projects cards, led by `needs-human` whose `labels[]` are the effective hard-suppress labels plus `needs-signal`, the hold labels, `stage/071-needs-author` and the reporter-trust awaiting label (#12172); accepts `?fields=a,b` for top-level selection and `?omit=repos` to drop heavy sections, and honors `Accept-Encoding: gzip`; `agents[]` includes busy-pane visibility fields (`kicksUndeliverable`, `busySince`, `lastTranscriptActivity`, `condition`, `conditionMessage`) when a Working pane cannot accept kicks; `sla` (fleet) and `repos[].sla` (per repo) carry the Delivery SLA block derived from the lifecycle timeline with no extra GitHub calls: `windows.{7d,30d}.{open_to_triage,triage_to_start,start_to_pr,pr_to_merge,open_to_merge}` as `{p50_h,p95_h,n}` (hours, nearest-rank), `history[]` of up to 30 daily buckets `{day,start_to_pr_p50,pr_to_merge_p50,open_to_merge_p50,n_started,n_pr,n_merged}` persisted under `/data/sla-history.json`, `stale_in_progress[]` (`{number,repo,started_at,age_h,agent}` — issues carrying `hive/in-progress` longer than the agent claim TTL with no linked PR), `stale_count`, `merged_7d`, `claim_ttl_s` and, on the fleet block, the effective `targets` (`start_to_pr_h`, `pr_to_merge_h`, `open_to_merge_h`); `staleSince` and `repos[].countsStale` / `countsStaleSince` mark totals served from last-good figures — the previous enumeration (#13382) or the ForgeState cache's last-good counts (#13376) | `pkg/dashboard/server.go:1491` | -| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1492` | -| `GET` | `/api/overview/issues.csv` | Dashboard auth/session | Cached Overview issue-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1493` | -| `GET` | `/api/overview/issues.json` | Dashboard auth/session | Cached Overview issue-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1494` | -| `GET` | `/api/overview/prs.csv` | Dashboard auth/session | Cached Overview PR-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1495` | -| `GET` | `/api/overview/prs.json` | Dashboard auth/session | Cached Overview PR-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1496` | -| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1497` | +| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1484` | +| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1485` | +| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1486` | +| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1492` | +| `GET` | `/api/status` | Dashboard auth/session | Dashboard aggregate status; `labelPresets[]` (`{key,label,description,labels[],states[]}`) lists the built-in label-filter presets for the Projects cards, led by `needs-human` whose `labels[]` are the effective hard-suppress labels plus `needs-signal`, the hold labels, `stage/071-needs-author` and the reporter-trust awaiting label (#12172); accepts `?fields=a,b` for top-level selection and `?omit=repos` to drop heavy sections, and honors `Accept-Encoding: gzip`; `agents[]` includes busy-pane visibility fields (`kicksUndeliverable`, `busySince`, `lastTranscriptActivity`, `condition`, `conditionMessage`) when a Working pane cannot accept kicks; a working agent also carries `agents[].run` — the live per-run line (#14576): `{since, elapsedS, costUSD, tokens, priced, tests, filesChanged}`, the estimated spend/tokens since the kick was delivered (`lastKickAt`, falling back to `busySince`) (delta of the token collector's per-agent cumulative figure; `priced:false` when the agent has none yet), the last visible test-runner result (`pass`/`fail`/empty) and the last `N files changed` git summary (`-1` when none) parsed from the captured pane; top-level `fleetRun` `{running, hourUSD, hourPriced}` is the governor strip's `running N · $X this hour` figure; `sla` (fleet) and `repos[].sla` (per repo) carry the Delivery SLA block derived from the lifecycle timeline with no extra GitHub calls: `windows.{7d,30d}.{open_to_triage,triage_to_start,start_to_pr,pr_to_merge,open_to_merge}` as `{p50_h,p95_h,n}` (hours, nearest-rank), `history[]` of up to 30 daily buckets `{day,start_to_pr_p50,pr_to_merge_p50,open_to_merge_p50,n_started,n_pr,n_merged}` persisted under `/data/sla-history.json`, `stale_in_progress[]` (`{number,repo,started_at,age_h,agent}` — issues carrying `hive/in-progress` longer than the agent claim TTL with no linked PR), `stale_count`, `merged_7d`, `claim_ttl_s` and, on the fleet block, the effective `targets` (`start_to_pr_h`, `pr_to_merge_h`, `open_to_merge_h`); `staleSince` and `repos[].countsStale` / `countsStaleSince` mark totals served from last-good figures — the previous enumeration (#13382) or the ForgeState cache's last-good counts (#13376) | `pkg/dashboard/server.go:1497` | +| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1498` | +| `GET` | `/api/overview/issues.csv` | Dashboard auth/session | Cached Overview issue-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1499` | +| `GET` | `/api/overview/issues.json` | Dashboard auth/session | Cached Overview issue-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1500` | +| `GET` | `/api/overview/prs.csv` | Dashboard auth/session | Cached Overview PR-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1501` | +| `GET` | `/api/overview/prs.json` | Dashboard auth/session | Cached Overview PR-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1502` | +| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1503` | | `GET` | `/api/swarm` | Dashboard auth/session | Current swarm status: display name, duration, active repo, expiry, persisted prep metrics when available, and idle-unlock fields. | `pkg/dashboard/swarm.go:310` | | `POST` | `/api/swarm` | Owner only | Start a 24h repo swarm for a configured repo, returning 409 when another swarm is active and 423 when a previous swarm keeps the next swarm locked until enough agents are idle unless body `{"repo":"owner/name","force":true}` is used; saves the active record before repo prep, persists prep metrics, and announces the start to Discord when configured. | `pkg/dashboard/swarm.go:311` | | `DELETE` | `/api/swarm` | Owner only | End the active swarm, score issues closed and PRs merged in the window, append it to history, and announce the result to Discord when configured. | `pkg/dashboard/swarm.go:312` | @@ -66,7 +66,7 @@ History point limits, timestamp cutoffs, incremental cost reads and ETag/304 beh | Method | Path | Auth | Purpose | Source | |---|---|---|---|---| | `GET` | `/api/style` | Public | Sanitized custom dashboard CSS | `pkg/dashboard/api.go:44` | -| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](branding.md)) | `pkg/dashboard/server.go:1558` | +| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](branding.md)) | `pkg/dashboard/server.go:1564` | | `GET` | `/api/snapshot/frame-ancestors` | Public | Snapshot framing allowlist | `pkg/dashboard/api.go:77` | | `GET` | `/api/snapshot` | Public | Snapshot data | `pkg/dashboard/api.go:78` | | `GET` | `/snapshot` | Public | Public read-only snapshot page | `pkg/dashboard/api.go:79` | @@ -94,9 +94,9 @@ History point limits, timestamp cutoffs, incremental cost reads and ETag/304 beh | `GET` | `/api/openrouter/models` | Dashboard auth/session | Open Router Models | `pkg/dashboard/openrouter.go:47` | | `GET` | `/api/openrouter/credit` | Dashboard auth/session | Open Router Credit | `pkg/dashboard/openrouter.go:48` | | `GET` | `/openrouter/callback` | Public | Open Router Callback | `pkg/dashboard/openrouter.go:49` | -| `POST` | `/api/github-app/recheck` | Dashboard auth/session | GitHub App Recheck | `pkg/dashboard/server.go:1498` | -| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1499` | -| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1500` | +| `POST` | `/api/github-app/recheck` | Dashboard auth/session | GitHub App Recheck | `pkg/dashboard/server.go:1504` | +| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1505` | +| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1506` | ## Configuration @@ -593,7 +593,7 @@ always resolved server-side from the validated token. | `POST` | `/api/hives/{id}/heartbeat` | Dashboard auth/session | Hives Heartbeat | `pkg/dashboard/api_contribute.go:275` | | `DELETE` | `/api/hives/{id}` | Owner only | Hives Delete | `pkg/dashboard/api_contribute.go:276` | | `POST` | `/api/hives/onboard` | Dashboard auth/session | Hives Onboard | `pkg/dashboard/api_contribute.go:277` | -| `GET` | `/sso` | Public | SSO | `pkg/dashboard/server.go:1505` | +| `GET` | `/sso` | Public | SSO | `pkg/dashboard/server.go:1511` | ## Hub SaaS diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 000eb76ba3..4b0ca253ca 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -97,6 +97,7 @@ How busy Hive thinks your projects are right now, and how often it wakes its wor - The Governor picks a mode by comparing the amount of actionable work with three thresholds. Actionable work is work Hive could do now, with nothing waiting on a person. - The **actionable issues** and **actionable PRs** tiles use the same count as the Overview's Actionable now tile. The two always agree. - The **hold** list names every held item. Held means someone parked it with a hold label. +- The compact strip at the top of the Agents view also shows **running** — how many workers are mid-round right now, and the estimated spend the whole fleet has accrued in the current clock hour (`3 · $1.20 this hour`). The hour figure is the rise in the cumulative estimate since the top of the hour; `$–` means the hive has not yet taken a reading this hour. - The **cadence table** shows, for each worker, how often it runs in each mode. Continuous means it runs again as soon as it finishes. - The **budget** shows how much of the AI spending allowance for the current period has been used. - The **next kick** estimate follows the eval tick, which is longer while GitHub webhooks are trusted (`/api/status.webhooks.mode` reads `webhook`) and drops back to `governor.eval_interval_s` when they degrade (`polling`). How that decision is made is in [webhooks.md](webhooks.md#fallback-when-webhooks-degrade). @@ -615,6 +616,14 @@ More about the line: - **Updates.** The line updates with each worker update, every 10 seconds by default, so a change shows within about 15 seconds. - **Privacy.** Titles come only from the issue and pull request lists the dashboard already shows you. If it shows you no repositories' issues, the line shows no item numbers either. +**The run line.** While a worker is working, a second line under **Now:** reads like `$0.42 · 38.1K tok · 4m 12s · tests ✓ · 3 files`. The same line is pinned in the worker's detail panel header. It is built only from figures Hive already keeps: + +- **`$` and `tok`** are the estimated cost and tokens of *this* round of work: the worker's cumulative token count × public list price (the same basis as the Cost section), minus what it was when Hive started the round (the last kick). Hive remembers the starting point, so a page reload does not reset the figure. `$–` means the token collector has no figure for this worker yet. +- **Elapsed** counts from the start of the round. +- **`tests ✓` / `tests ✗` / `tests –`** is the last test-runner summary visible in the worker's terminal (`go test` `ok`/`FAIL`, pytest `N passed`/`N failed`, jest/vitest `Tests:` lines). `–` means no result line is visible. +- **`N files`** is the last `N files changed` git summary visible in the terminal; `–` means none is. +- It updates with each worker update and disappears when the round ends. The **spend (range)** row further down the card is unchanged: that is the worker's spend over the Cost section's timeframe, not this round's. + **What it is good for.** Use it to pause a worker, restart a stuck one, or change its settings. Fix any card that says it needs a login. Read the **Now:** lines to see what each worker is on without opening its log. **Example.** A card says "needs login". You click **Login** and sign in again. Another card says `Now: working — no issue or pull request yet · started 50 min ago`. That worker has changed nothing through the hive for 50 minutes, so you open its log to see what it is doing. diff --git a/src/pkg/dashboard/run_meter.go b/src/pkg/dashboard/run_meter.go new file mode 100644 index 0000000000..44d2994a42 --- /dev/null +++ b/src/pkg/dashboard/run_meter.go @@ -0,0 +1,275 @@ +package dashboard + +import ( + "regexp" + "strconv" + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/pkg/tokens" +) + +// Live per-run line (hivecommons/hive#14576). +// +// The token collector already keeps a per-agent cumulative token/cost figure +// (the same one GET /api/cost reports under estimated.by_agent). A running +// kick's own spend is the difference between that cumulative figure now and +// the figure when the kick started, so the run meter snapshots the baseline +// the first time it sees an agent busy for a given kick time and reports the +// delta on every status build after that. Nothing new is metered. + +// FrontendAgentRun is the live per-run figure on a busy agent card and in the +// transcript pane header. It is present only while the agent is working. +type FrontendAgentRun struct { + // Since is when the kick was delivered (the agent's LastKick), RFC3339; + // BusySince when no kick time is known. + Since string `json:"since"` + // ElapsedS is seconds since Since at build time. + ElapsedS int64 `json:"elapsedS"` + // CostUSD and Tokens are this run's estimated spend and token count + // (input + output + cache) since the kick started. Priced is false when + // the collector has no per-agent figure for this agent yet, in which + // case both are zero and the UI renders them as unknown. + CostUSD float64 `json:"costUSD"` + Tokens int64 `json:"tokens"` + Priced bool `json:"priced"` + // Tests is the last test-runner outcome visible in the pane: "pass", + // "fail" or "" when no result line is visible. + Tests string `json:"tests,omitempty"` + // FilesChanged is the last `N files changed` git summary visible in the + // pane; -1 when none is visible. + FilesChanged int `json:"filesChanged"` +} + +// FrontendFleetRun is the governor-strip fleet figure: how many agents are +// mid-kick and the estimated spend accrued in the current clock hour. +type FrontendFleetRun struct { + Running int `json:"running"` + HourUSD float64 `json:"hourUSD"` + // HourPriced is false until the meter has observed a cumulative total + // in the current hour, i.e. when HourUSD would be a guess. + HourPriced bool `json:"hourPriced"` +} + +type runBaseline struct { + since time.Time + usd float64 + tokens int64 +} + +// runMeter remembers per-agent kick baselines and the hourly fleet baseline. +// It is a package-level singleton, like the other status-build providers in +// this file's neighbours, because the status builders are free functions. +type runMeter struct { + mu sync.Mutex + baselines map[string]runBaseline + // last is the most recent per-agent cumulative figure seen, so builds + // without a collector (BuildAgentOnlyStatus) still report a delta. + last map[string]tokens.ModelCost + hourStart time.Time + hourBase float64 + hourTotal float64 + hourSeen bool +} + +func newRunMeter() *runMeter { + return &runMeter{baselines: map[string]runBaseline{}, last: map[string]tokens.ModelCost{}} +} + +var defaultRunMeter = newRunMeter() + +// paneLinesForRun bounds how much pane text the test/files parsers read. +const paneLinesForRun = 400 + +// observeCollector refreshes the meter's view of per-agent cumulative cost +// from the token collector. A nil collector or summary leaves the last +// observation in place. +func (m *runMeter) observeCollector(tc *tokens.Collector, now time.Time) { + if tc == nil { + return + } + summary := tc.Summary() + if summary == nil { + return + } + m.observeEstimate(tokens.EstimateFromSummary(summary), now) +} + +func (m *runMeter) observeEstimate(est tokens.EstimatedCost, now time.Time) { + m.mu.Lock() + defer m.mu.Unlock() + for name, mc := range est.ByAgent { + m.last[name] = mc + } + hour := now.Truncate(time.Hour) + if !m.hourSeen || !hour.Equal(m.hourStart) { + m.hourStart = hour + m.hourBase = est.TotalUSD + m.hourSeen = true + } + // A cumulative total that fell (pod roll, session-window shift) would + // otherwise read as negative spend for the rest of the hour. + if est.TotalUSD < m.hourBase { + m.hourBase = est.TotalUSD + } + m.hourTotal = est.TotalUSD +} + +// run returns the live figure for one agent, resetting its baseline when the +// kick time changed. A zero since means the agent is not running and +// the baseline is dropped. +func (m *runMeter) run(name string, since time.Time, now time.Time) *FrontendAgentRun { + m.mu.Lock() + defer m.mu.Unlock() + if since.IsZero() { + delete(m.baselines, name) + return nil + } + cur, priced := m.last[name] + curTokens := modelCostTokens(cur) + b, ok := m.baselines[name] + if !ok || !b.since.Equal(since) { + b = runBaseline{since: since, usd: cur.USD, tokens: curTokens} + m.baselines[name] = b + } + r := &FrontendAgentRun{ + Since: since.UTC().Format(time.RFC3339), + ElapsedS: int64(now.Sub(since).Seconds()), + Priced: priced, + FilesChanged: -1, + } + if r.ElapsedS < 0 { + r.ElapsedS = 0 + } + if priced { + // The cumulative counter can fall across a pod roll; never report a + // negative run. + if cur.USD >= b.usd { + r.CostUSD = cur.USD - b.usd + } + if curTokens >= b.tokens { + r.Tokens = curTokens - b.tokens + } + } + return r +} + +func (m *runMeter) fleet(running int) FrontendFleetRun { + m.mu.Lock() + defer m.mu.Unlock() + f := FrontendFleetRun{Running: running, HourPriced: m.hourSeen} + if m.hourSeen && m.hourTotal >= m.hourBase { + f.HourUSD = m.hourTotal - m.hourBase + } + return f +} + +func modelCostTokens(mc tokens.ModelCost) int64 { + return mc.Input + mc.Output + mc.CacheRead + mc.CacheCreate +} + +// applyRunMeter attaches the live per-run figure to every busy agent and +// returns the fleet figure. It is called from both status builders. +func applyRunMeter(m *runMeter, agents []FrontendAgent, statuses map[string]*crux.Process, tc *tokens.Collector, now time.Time) FrontendFleetRun { + m.observeCollector(tc, now) + running := 0 + for i := range agents { + a := &agents[i] + proc := statuses[a.Name] + if a.Busy != "working" || proc == nil { + m.run(a.Name, time.Time{}, now) + continue + } + // The kick's delivery time is the run's start: busy == "working" is + // defined as "that kick's turn has not settled". BusySince is only + // stamped by the busy-pane watcher, so it is the fallback, not the + // primary. + var since time.Time + if proc.LastKick != nil { + since = *proc.LastKick + } + if since.IsZero() { + since = proc.BusySince + } + if since.IsZero() { + continue + } + running++ + r := m.run(a.Name, since, now) + lines := proc.PaneLines(paneLinesForRun) + r.Tests = parseTestSignal(lines) + r.FilesChanged = parseFilesChanged(lines) + a.Run = r + } + return m.fleet(running) +} + +var ( + // go test: "ok \tpkg\t0.1s" / "PASS" vs "FAIL\tpkg" / "--- FAIL". + reGoTestOK = regexp.MustCompile(`^(?:ok\s+\S+|PASS)\b`) + reGoTestFail = regexp.MustCompile(`^(?:FAIL(?:\s+\S+)?|--- FAIL:)\b`) + // pytest summary: "=== 12 passed in 1.2s ===" / "=== 1 failed, 11 passed ===". + rePytestLine = regexp.MustCompile(`^=+ .*\b(\d+ passed|\d+ failed|\d+ error)`) + // jest/vitest/mocha: "Tests: 1 failed, 3 passed" / "Tests 3 passed". + reJSTests = regexp.MustCompile(`^Tests:?\s+(.*)$`) + // git: "3 files changed, 10 insertions(+)" / "1 file changed". + reFilesChanged = regexp.MustCompile(`\b(\d+) files? changed\b`) +) + +// parseTestSignal returns "pass", "fail" or "" from the LAST test-runner +// result line visible in lines. Only whole-run summary lines count, so a +// single `--- FAIL` inside an otherwise green run still reads as fail (it +// is the later line that decides) and a transcript with no runner output +// reads as unknown. +func parseTestSignal(lines []string) string { + for i := len(lines) - 1; i >= 0; i-- { + l := strings.TrimSpace(stripANSI(lines[i])) + if l == "" { + continue + } + switch { + case reGoTestFail.MatchString(l): + return "fail" + case reGoTestOK.MatchString(l): + return "pass" + case rePytestLine.MatchString(l): + if strings.Contains(l, " failed") || strings.Contains(l, " error") { + return "fail" + } + return "pass" + } + if m := reJSTests.FindStringSubmatch(l); m != nil { + if strings.Contains(m[1], "failed") { + return "fail" + } + if strings.Contains(m[1], "passed") { + return "pass" + } + } + } + return "" +} + +// parseFilesChanged returns the N of the last `N files changed` git summary +// line visible in lines, or -1 when none is. +func parseFilesChanged(lines []string) int { + for i := len(lines) - 1; i >= 0; i-- { + if m := reFilesChanged.FindStringSubmatch(stripANSI(lines[i])); m != nil { + if n, err := strconv.Atoi(m[1]); err == nil { + return n + } + } + } + return -1 +} + +var reANSI = regexp.MustCompile(`\x1b\[[0-9;?]*[ -/]*[@-~]`) + +func stripANSI(s string) string { + if !strings.Contains(s, "\x1b") { + return s + } + return reANSI.ReplaceAllString(s, "") +} diff --git a/src/pkg/dashboard/run_meter_test.go b/src/pkg/dashboard/run_meter_test.go new file mode 100644 index 0000000000..95f18ef7f3 --- /dev/null +++ b/src/pkg/dashboard/run_meter_test.go @@ -0,0 +1,157 @@ +package dashboard + +import ( + "testing" + "time" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/pkg/tokens" +) + +func TestRunMeterDeltaResetsOnNewKick(t *testing.T) { + m := newRunMeter() + t0 := time.Date(2026, 10, 27, 14, 0, 0, 0, time.UTC) + est := tokens.EstimatedCost{TotalUSD: 10, ByAgent: map[string]tokens.ModelCost{ + "dev": {USD: 1.00, Input: 1000, Output: 200, Priced: true}, + }} + m.observeEstimate(est, t0) + + r := m.run("dev", t0, t0.Add(30*time.Second)) + if r == nil || !r.Priced || r.CostUSD != 0 || r.Tokens != 0 || r.ElapsedS != 30 { + t.Fatalf("baseline run: %+v", r) + } + + est.ByAgent["dev"] = tokens.ModelCost{USD: 1.25, Input: 1500, Output: 300, CacheRead: 200, Priced: true} + est.TotalUSD = 10.25 + m.observeEstimate(est, t0.Add(time.Minute)) + r = m.run("dev", t0, t0.Add(time.Minute)) + if r.CostUSD < 0.249 || r.CostUSD > 0.251 || r.Tokens != 800 || r.ElapsedS != 60 { + t.Fatalf("delta run: %+v", r) + } + + // A new kick (different BusySince) restarts from the current cumulative. + t1 := t0.Add(2 * time.Minute) + r = m.run("dev", t1, t1.Add(5*time.Second)) + if r.CostUSD != 0 || r.Tokens != 0 || r.ElapsedS != 5 || r.Since != t1.Format(time.RFC3339) { + t.Fatalf("new kick run: %+v", r) + } + + // Idle drops the baseline. + if got := m.run("dev", time.Time{}, t1); got != nil { + t.Fatalf("idle run should be nil, got %+v", got) + } + if _, ok := m.baselines["dev"]; ok { + t.Fatal("baseline kept after idle") + } +} + +func TestRunMeterUnpricedAgentAndCounterDrop(t *testing.T) { + m := newRunMeter() + now := time.Date(2026, 10, 27, 14, 0, 0, 0, time.UTC) + m.observeEstimate(tokens.EstimatedCost{ByAgent: map[string]tokens.ModelCost{"dev": {USD: 2, Input: 100}}}, now) + r := m.run("ghost", now, now) + if r == nil || r.Priced || r.CostUSD != 0 || r.FilesChanged != -1 { + t.Fatalf("unpriced agent: %+v", r) + } + // Counter falls (pod roll): the run never goes negative. + m.run("dev", now, now) + m.observeEstimate(tokens.EstimatedCost{ByAgent: map[string]tokens.ModelCost{"dev": {USD: 0.5, Input: 10}}}, now) + r = m.run("dev", now, now.Add(-time.Second)) + if r.CostUSD != 0 || r.Tokens != 0 || r.ElapsedS != 0 { + t.Fatalf("counter drop: %+v", r) + } +} + +func TestRunMeterFleetHourRollsAndClamps(t *testing.T) { + m := newRunMeter() + t0 := time.Date(2026, 10, 27, 14, 10, 0, 0, time.UTC) + if f := m.fleet(0); f.HourPriced || f.HourUSD != 0 { + t.Fatalf("empty fleet: %+v", f) + } + m.observeEstimate(tokens.EstimatedCost{TotalUSD: 100}, t0) + m.observeEstimate(tokens.EstimatedCost{TotalUSD: 101.5}, t0.Add(10*time.Minute)) + f := m.fleet(3) + if f.Running != 3 || !f.HourPriced || f.HourUSD < 1.49 || f.HourUSD > 1.51 { + t.Fatalf("hour spend: %+v", f) + } + // New hour: baseline resets. + m.observeEstimate(tokens.EstimatedCost{TotalUSD: 102}, t0.Add(time.Hour)) + if f := m.fleet(1); f.HourUSD != 0 { + t.Fatalf("new hour should reset: %+v", f) + } + // Total drops within the hour: baseline follows it down, never negative. + m.observeEstimate(tokens.EstimatedCost{TotalUSD: 50}, t0.Add(time.Hour+time.Minute)) + m.observeEstimate(tokens.EstimatedCost{TotalUSD: 50.2}, t0.Add(time.Hour+2*time.Minute)) + if f := m.fleet(1); f.HourUSD < 0.19 || f.HourUSD > 0.21 { + t.Fatalf("after drop: %+v", f) + } +} + +func TestParseTestSignal(t *testing.T) { + cases := []struct { + name string + lines []string + want string + }{ + {"none", []string{"$ ls", "foo.go"}, ""}, + {"go ok", []string{"$ go test ./...", "ok \tgithub.com/x/y\t0.123s"}, "pass"}, + {"go fail", []string{"--- FAIL: TestX (0.00s)", "FAIL", "FAIL\tgithub.com/x/y\t0.1s"}, "fail"}, + {"last wins", []string{"FAIL\tgithub.com/x/y", "$ go test ./y", "ok \tgithub.com/x/y\t0.1s"}, "pass"}, + {"pytest pass", []string{"\x1b[32m========== 12 passed in 1.20s ==========\x1b[0m"}, "pass"}, + {"pytest fail", []string{"=========== 1 failed, 11 passed in 1.20s ==========="}, "fail"}, + {"jest fail", []string{"Tests: 1 failed, 3 passed, 4 total"}, "fail"}, + {"vitest pass", []string{"Tests 7 passed (7)"}, "pass"}, + {"PASS alone", []string{"PASS", "coverage: 91.0% of statements"}, "pass"}, + } + for _, c := range cases { + if got := parseTestSignal(c.lines); got != c.want { + t.Errorf("%s: got %q want %q", c.name, got, c.want) + } + } +} + +func TestParseFilesChanged(t *testing.T) { + if n := parseFilesChanged([]string{"nothing here"}); n != -1 { + t.Fatalf("no summary: %d", n) + } + lines := []string{" 2 files changed, 4 insertions(+)", "later", " 1 file changed, 1 deletion(-)"} + if n := parseFilesChanged(lines); n != 1 { + t.Fatalf("last summary wins: %d", n) + } + if n := parseFilesChanged([]string{"\x1b[1m 13 files changed\x1b[0m, 200 insertions(+)"}); n != 13 { + t.Fatalf("ansi: %d", n) + } +} + +func TestApplyRunMeterAttachesOnlyToWorkingAgents(t *testing.T) { + m := newRunMeter() + now := time.Date(2026, 10, 27, 14, 0, 0, 0, time.UTC) + since := now.Add(-90 * time.Second) + m.observeEstimate(tokens.EstimatedCost{TotalUSD: 1, ByAgent: map[string]tokens.ModelCost{"dev": {USD: 1, Input: 10}}}, now) + + working := &crux.Process{Name: "dev", BusySince: since} + working.SetPaneCapture([]string{"ok \tpkg\t0.1s", " 3 files changed, 9 insertions(+)"}, func(l []string, n int) []string { return l }) + idle := &crux.Process{Name: "idle"} + statuses := map[string]*crux.Process{"dev": working, "idle": idle} + agents := []FrontendAgent{{Name: "dev", Busy: "working"}, {Name: "idle", Busy: "idle"}, {Name: "orphan", Busy: "working"}} + + fleet := applyRunMeter(m, agents, statuses, nil, now) + if fleet.Running != 1 { + t.Fatalf("running: %+v", fleet) + } + r := agents[0].Run + if r == nil || r.ElapsedS != 90 || r.Tests != "pass" || r.FilesChanged != 3 || !r.Priced { + t.Fatalf("working run: %+v", r) + } + if agents[1].Run != nil || agents[2].Run != nil { + t.Fatalf("idle/orphan must carry no run: %+v %+v", agents[1].Run, agents[2].Run) + } + + // A kick time wins over BusySince (the busy-pane watcher's stamp). + lk := now.Add(-10 * time.Second) + working.LastKick = &lk + applyRunMeter(m, agents, statuses, nil, now) + if agents[0].Run == nil || agents[0].Run.ElapsedS != 10 { + t.Fatalf("lastKick preferred: %+v", agents[0].Run) + } +} diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index fa4d791872..e092420a18 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -544,17 +544,19 @@ type StatusPayload struct { // from the shallow /api/health liveness signal or the repo-workflow // Health map above, so the pill can never show "Health OK" while the // spoke's own agents are down (#2465). - DeepHealth map[string]any `json:"deepHealth,omitempty"` - Budget FrontendBudget `json:"budget"` - CadenceMatrix []FrontendCadence `json:"cadenceMatrix"` - GHRateLimits map[string]any `json:"ghRateLimits"` - APIBudget map[string]any `json:"api_budget"` - AgentMetrics map[string]any `json:"agentMetrics"` - Hold FrontendHold `json:"hold"` - IssueToMerge map[string]any `json:"issueToMerge"` - ACMMLevel int `json:"acmmLevel"` - ACMMLevelConfigured bool `json:"acmmLevelConfigured"` - ACMMPackAgents []string `json:"acmmPackAgents"` + DeepHealth map[string]any `json:"deepHealth,omitempty"` + Budget FrontendBudget `json:"budget"` + CadenceMatrix []FrontendCadence `json:"cadenceMatrix"` + GHRateLimits map[string]any `json:"ghRateLimits"` + APIBudget map[string]any `json:"api_budget"` + AgentMetrics map[string]any `json:"agentMetrics"` + Hold FrontendHold `json:"hold"` + // FleetRun is the governor-strip `running N · $X this hour` figure (#14576). + FleetRun FrontendFleetRun `json:"fleetRun"` + IssueToMerge map[string]any `json:"issueToMerge"` + ACMMLevel int `json:"acmmLevel"` + ACMMLevelConfigured bool `json:"acmmLevelConfigured"` + ACMMPackAgents []string `json:"acmmPackAgents"` // ACMMAdvice is the advisory level-up recommendation (#5225), derived from // the same live signals the /api/acmm-recommendation endpoint serves so the // two surfaces can never drift. It is ADVISORY ONLY: nothing acts on it @@ -824,6 +826,10 @@ type FrontendAgent struct { // the turn is still running. KickOutcomeReason is the deciding pane line. KickOutcome string `json:"kickOutcome,omitempty"` KickOutcomeReason string `json:"kickOutcomeReason,omitempty"` + // Run is the live per-run figure (#14576): this kick's estimated cost, + // tokens, elapsed time, last visible test result and files changed. + // Present only while Busy is "working". + Run *FrontendAgentRun `json:"run,omitempty"` // BackendAuth* surfaces this agent's backend-auth canary (#6558): derived // from the same classifyProviderError verdict as StructuredStatus's // "blocked: inference" evidence, so the dashboard can render an explicit diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 1ac61344d6..1ae4934f2c 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1288,6 +1288,14 @@ .agent-now .an-lead { font-weight: 600; color: var(--text); } .agent-now.outdated { color: var(--yellow); } .oc-agent-detail .agent-now { text-align: left; } + /* Live per-run line (#14576): $cost · tokens · elapsed · tests · files. */ + .oc-run-line { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--sp-1) var(--sp-3); font-size: var(--fs-xs); color: var(--muted); margin-top: var(--sp-1); font-variant-numeric: tabular-nums; } + .oc-run-line .rl-cost { font-weight: 600; color: var(--text); } + .oc-run-line .rl-tests.pass { color: var(--green); } + .oc-run-line .rl-tests.fail { color: var(--red); } + .oc-run-line .rl-sep { opacity: 0.5; } + .oc-agent-detail .oc-run-line { justify-content: flex-start; } + .oc-agent-detail-header .oc-run-line { margin-top: var(--sp-0); margin-left: auto; } /* Agent activity section (#10935): one row per agent. */ .agent-activity-scroll { width: 100%; max-width: 100%; overflow-x: auto; } .agent-activity-table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); } @@ -10757,6 +10765,7 @@

Where to get help

actionable${govActionable}
PRs${govPrs}
MTTR${formatFixTime(itmMedian)}
+ ${fleetRunStripHtml()}
on hold${window._lastStatus?.hold?.total || 0}
${(() => { const plan = window._lastStatus?.planning || {}; @@ -10823,6 +10832,7 @@

Where to get help

${esc(a.displayName || a.name)}${a.sandboxed ? ' ' + statusBadgeHTML('ready', 'sandboxed', 'Sandboxed runner active', 'done') : ''}${needsLoginDown ? '\uD83D\uDD11' : ''} + ${agentRunLineHtml(a)} ${stateLabel}${pauseInfoIcon(a)}
@@ -13520,6 +13530,39 @@

Where to get help

return agentNowLineHtml(a, agentNowStateOf(a), agentNowContext(Date.now())); } + // Live per-run line (#14576): "$0.42 · 38.1K tok · 4m 12s · tests ✓ · 3 files". + // Rendered only while /api/status carries agents[].run (busy agents), from + // the per-agent cumulative figures the token collector already keeps; the + // server reports the delta since the kick's BusySince, so a reload does not + // reset it. Missing signals render as "–" rather than a guess. + const RUN_LINE_HELP = 'Live figures for the kick in progress: estimated cost and tokens since the kick started (token × list price, same basis as the Cost section), elapsed time, the last test-runner result visible in the transcript, and the last "N files changed" git summary. "–" means not observed yet.'; + function fmtRunElapsed(sec) { + sec = Math.max(0, Math.floor(Number(sec) || 0)); + const h = Math.floor(sec / 3600), m = Math.floor((sec % 3600) / 60), s = sec % 60; + if (h > 0) return `${h}h ${String(m).padStart(2, '0')}m`; + if (m > 0) return `${m}m ${String(s).padStart(2, '0')}s`; + return `${s}s`; + } + function agentRunLineHtml(a) { + const r = a && a.run; + if (!r) return ''; + const sep = '·'; + const cost = r.priced ? fmtUSD(r.costUSD || 0) : '$–'; + const toks = r.priced ? `${fmtTokens(r.tokens || 0)} tok` : '– tok'; + const tests = r.tests === 'pass' ? 'tests ✓' + : r.tests === 'fail' ? 'tests ✗' + : 'tests –'; + const files = (typeof r.filesChanged === 'number' && r.filesChanged >= 0) + ? `${r.filesChanged} file${r.filesChanged === 1 ? '' : 's'}` : '– files'; + return `
${cost}${sep}${toks}${sep}${fmtRunElapsed(r.elapsedS)}${sep}${tests}${sep}${files}
`; + } + function fleetRunStripHtml() { + const f = window._lastStatus?.fleetRun; + if (!f) return ''; + const hour = f.hourPriced ? fmtUSD(f.hourUSD || 0) : '$–'; + return `
running${f.running || 0} · ${hour} this hour
`; + } + // Repaints every "Now:" line in place so "ago" keeps counting up (and // turns into "information may be out of date") without new data. function refreshAgentNowLines() { @@ -17733,6 +17776,7 @@

All labels

return ''; })()}${kickOutcomeBadgeHtml(a)}${agentSpendChip(a.name)}
${agentNowHtml(a)} + ${agentRunLineHtml(a)} ${agentBlockerLineHtml(a)} ${(() => { // Last upstream/launch error stays on its own line below the state diff --git a/src/pkg/dashboard/status_builder.go b/src/pkg/dashboard/status_builder.go index 6a0968c939..847e456307 100644 --- a/src/pkg/dashboard/status_builder.go +++ b/src/pkg/dashboard/status_builder.go @@ -346,6 +346,7 @@ func BuildAgentOnlyStatus( cfg *config.Config, ) *AgentStatusPayload { agents, hidden := buildAgentsWithHidden(agentStatuses, cfg, govState) + applyRunMeter(defaultRunMeter, agents, agentStatuses, nil, time.Now()) return &AgentStatusPayload{ Timestamp: time.Now().UTC().Format(time.RFC3339), Agents: agents, @@ -376,6 +377,7 @@ func BuildFrontendStatus( ghRateLimits := buildGHRateLimits(ghClient, ctx, cfg) agents, hiddenAgents := buildAgentsWithHidden(agentStatuses, cfg, govState) + fleetRun := applyRunMeter(defaultRunMeter, agents, agentStatuses, tokenCollector, time.Now()) health := buildHealth(ghClient, ctx) mergeAgentAuthHealth(health, agents) @@ -413,6 +415,7 @@ func BuildFrontendStatus( AgentMetrics: agentMetrics, OverviewCoverage: buildOverviewCoverage(agentMetrics, cfg), Hold: buildHold(actionable), + FleetRun: fleetRun, IssueToMerge: issueToMerge, IssuesDisabledRepos: metricsCollector.GetIssuesDisabledRepos(), ACMMLevel: detectACMMLevel(cfg),