Skip to content

fix: unify agent vitals reporting across UI and API (fixes #7)#20

Open
kagura-agent wants to merge 1 commit intoopen-gitagent:mainfrom
kagura-agent:fix/7-agent-vitals-reporting-is-inco
Open

fix: unify agent vitals reporting across UI and API (fixes #7)#20
kagura-agent wants to merge 1 commit intoopen-gitagent:mainfrom
kagura-agent:fix/7-agent-vitals-reporting-is-inco

Conversation

@kagura-agent
Copy link

Problem

Agent vitals (CPU, memory, uptime, tokens) are reported inconsistently between the dashboard UI and the API endpoint. Different surfaces show different values, causing confusion about the agent's actual state.

Root Causes

  1. Uptime mismatch: UI computed uptime from Date.now() at page load (browser time), while API returned process.uptime() (server process time). Opening the dashboard 30 minutes after server start meant they'd permanently differ by 30 minutes.

  2. CPU calculation: API used cumulative process.cpuUsage() divided by total process lifetime, giving a lifetime average instead of instantaneous utilization — misleading when comparing against the UI's 2-second poll interval.

  3. No shared snapshot: Each API call independently computed process.memoryUsage(), process.cpuUsage(), and process.uptime() at slightly different instants, so concurrent consumers (multiple browser tabs, external API clients) could see divergent values.

Fix

  • Centralized getVitalsSnapshot() with 1-second cache — all surfaces (API, UI, future log consumers) read the same frozen snapshot within any 1s window
  • Delta-based CPU measurement using process.hrtime.bigint() for accurate instantaneous CPU readings instead of lifetime averages
  • UI syncs uptime from server response on each 2s poll, with 1s local interpolation for smooth display. Removed the client-side vitalsStart = Date.now() uptime calculation entirely.
  • Response includes ts field (unix-ms) so consumers know exactly when the snapshot was taken

Changes

  • src/voice/server.ts: Add VitalsSnapshot interface, getVitalsSnapshot() with caching and delta CPU. Replace inline vitals computation in /api/vitals handler.
  • src/voice/ui.html: Remove vitalsStart, use vitalsServerUptime synced from API. Add uptime sync in pollSystemVitals().

Fixes #7

…ent#7)

Root cause: UI and API computed vitals independently, causing
inconsistent values across surfaces:

1. Uptime: UI used browser page-load time (Date.now() at load) while
   API returned process.uptime(). Opening the page 30min after server
   start meant they'd permanently differ by 30 minutes.

2. CPU: API used cumulative process.cpuUsage() divided by total uptime,
   giving a lifetime average instead of instantaneous utilization.

3. No shared snapshot: each API call computed mem/cpu/uptime at slightly
   different instants, so concurrent readers could see different values.

Fix:
- Add centralized getVitalsSnapshot() with 1s cache — all surfaces
  (API, UI, future log consumers) read the same frozen snapshot
- Use delta-based CPU measurement for accurate instantaneous readings
- UI now syncs uptime from server response (resets on each 2s poll),
  with 1s local interpolation for smooth display
- Remove client-side vitalsStart/Date.now() uptime calculation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent vitals reporting is inconsistent

1 participant