Skip to content

feat(hosted): a hosted agent with heartbeat on runs a turn on its schedule, hourly at most and 6 a day per owner (D244) - #3417

Merged
lilyshen0722 merged 1 commit into
mainfrom
swe/hosted-heartbeat
Nov 4, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
swe/hosted-heartbeat

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

D244 dogfood finding 3. A hosted install acked every heartbeat with no run, so a self-serve user couldn't have a scheduled agent. Our Triage pilot needed an admin to switch it to native to get one.

Why it skipped

HOSTED_NATIVE_EVENT_TYPES came from #2224 (TASK-261, 10-15). That PR moved hosted agents onto the native runtime and kept the Durable Object's two event types, chat.mention and first_contact, as parity: "Other events are acked with no run and no model call." No Sam direction or ledger row chose it. D244 lists it as a bug.

What changes

  • heartbeat joins the hosted event types (hostedEngine.ts). A hosted install with heartbeat on (Settings → wake, per pod) now runs a real turn on its schedule.
  • Floor: one heartbeat run per install an hour. This holds whatever config.heartbeat.everyMinutes says; the scheduler allows 1. The check looks back 55 minutes, so dispatch jitter on an hourly schedule doesn't skip every other tick.
  • Cap: HOSTED_HEARTBEAT_RUNS_PER_DAY, default 6. That's 6 heartbeat runs per owner per UTC day, across all their hosted installs: a 4-hourly agent, or an hourly one for a morning. Setting 0 switches hosted heartbeats off.
  • Both counts fail closed. They come from AgentRun, and a failed count returns heartbeat_cap_check_failed, like the per-user ceiling. A held heartbeat is acked with no AgentRun, like any other hosted decline.
  • Unchanged. Heartbeats still count against the plan line (30 turns a day free, 200 pro) and the per-user ceiling. They run on the server-built hosted config: the deployment's native model (claude-haiku-5-5), the hosted tool set, and the same grant and refusal rules. Mentions never touch the heartbeat counts. The owner cap counts every heartbeat run stamped with the owner, so heartbeats from a non-hosted native install the owner has also count toward the 6. That makes it conservative.

Cost at the cap

The baseline is CTO's measure of native runs over 30 days (11-04): 1,467 runs, 18.8M tokens in and 1.17M out, and at most 31.9K per call.

  • Per run: about 12.8K in and 0.8K out. At Haiku 5.5's $0.10/$0.50 per MTok that's about $0.0017.
  • One owner at the cap: 6 a day × 30 days = 180 runs, about $0.30 a month. That's 12% of the baseline's run count.
  • The whole baseline is about $2.50 a month at list price. CTO's estimate was $3–6.
  • Route budget: the Haiku route's LiteLLM budget is $50 a month. After the baseline, it covers about 150 owners at the full heartbeat cap.
  • Worst case per run is the 200K-token run budget. That's at most $0.10 even if every token were output. Heartbeats can't raise an owner's ceiling, though, because they spend the same plan-line turns a mention does. The most an owner can run in a day doesn't change; heartbeats only let turns fire without a person.

Tests

nativeRuntimeService.hostedEngine.test.js adds seven tests. Six failed before this change; the other two guard against regressions.

  • A heartbeat runs a real turn, recorded as heartbeat for the owner.
  • A second heartbeat within the hour is held, and the floor query is per install over a 55-minute window.
  • The owner's cap: 5 today runs, 6 doesn't.
  • The env var sets the cap, and 0 is off.
  • A failed count fails closed.
  • A heartbeat still counts against the plan line.
  • A mention isn't touched by the heartbeat counts.

Every hosted, native, scheduler and heartbeat unit suite passes, 56 suites and 890 tests. tsc --noEmit is clean, and eslint reports no errors in the changed files.

Follow-up, not in this PR

Settings → wake still offers 15 and 30 minutes, and its "turns used" preview counts 1440 ÷ minutes. For a hosted agent the server now floors those to hourly and caps them at 6 a day, so the select and the preview overstate what will run. A small frontend change could start the hosted options at 60 and preview against the cap.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MDs9DRRdfD6THbfDgHSPZ5

…edule (D244)

A hosted install acked every heartbeat with no run, kept from the Durable
Object's two event types (#2224), so a self-serve user couldn't have a
scheduled agent. It now runs, with two spend bounds of its own, both
counted from AgentRun and failing closed: one heartbeat run per install
an hour, and HOSTED_HEARTBEAT_RUNS_PER_DAY (default 6, 0 = off) across an
owner's hosted installs a day. Heartbeats still count against the plan
line and the per-user ceiling, and run on the hosted config (the
deployment's native model, Haiku), so grants and refusals are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MDs9DRRdfD6THbfDgHSPZ5

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f9a1b40

This is the one review, run on a stronger model because the change touches wake and money.

  • Spend bounds hold. The run row is stamped trigger: 'heartbeat' and userId (nativeRuntimeService.ts:1746-1748, 2163-2170), so both counts in hostedHeartbeatHold see it. A decline returns before AgentRun.create, so no row is written. Count-then-run has no lock, but a duplicate can't get through. The in-process lane serialises runs of one install (agentEventService.ts:1819-1840). The scheduler runs under a cluster lease and skips an install with a pending heartbeat (schedulerService.ts:1509-1517).
  • No new grants. The hosted config sets no model, so the run uses NATIVE_RUNTIME_MODEL (Haiku). triggerTextIsHumanWritten excludes heartbeat, so the heartbeat cue and the digest frame can't unlock a fetch. wasDirectlyAddressed is false for a heartbeat. The tool set and outsider-grant narrowing don't depend on the trigger.
  • Wake path. dispatchAgentHeartbeats enqueues for hosted installs with heartbeat.enabled, and the settings PATCH writes it for hosted installs. runsInProcess includes hostedRunsNative with no type filter.
  • Tests. The new tests fail with the change reverted.
  • Not blocking. The owner cap also counts heartbeats from the owner's non-hosted native installs. That's conservative, and the PR body says so.

Locally, 56 suites and 890 tests pass, and tsc is clean. CI must be green at this head before merge.

GATE: PASS

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

Hosted heartbeat events can now start real runs. The runtime applies per-installation hourly and per-owner daily limits before execution. Tests cover successful runs, limit handling, count errors, plan limits, and mention events.

Changes: Hosted heartbeat execution

Layer / File(s) Summary
Heartbeat event policy
backend/services/hostedEngine.ts
The hosted event set now includes heartbeat. The module defines a 60-minute minimum interval, five-minute floor slack, and a daily cap that defaults to six and accepts valid nonnegative integer overrides.
Heartbeat run checks
backend/services/nativeRuntimeService.ts, backend/__tests__/unit/services/nativeRuntimeService.hostedEngine.test.js
Before selecting the run configuration, the runtime holds heartbeat runs that exceed installation or owner limits. Count-check errors fail with heartbeat_cap_check_failed. Tests cover execution, limits, configuration, and unaffected mention events.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant HostedHeartbeat
  participant runAgent
  participant hostedHeartbeatHold
  participant AgentRunCounts
  participant AgentRun
  HostedHeartbeat->>runAgent: trigger heartbeat
  runAgent->>hostedHeartbeatHold: check heartbeat limits
  hostedHeartbeatHold->>AgentRunCounts: count installation and owner runs
  AgentRunCounts-->>hostedHeartbeatHold: return counts or error
  hostedHeartbeatHold-->>runAgent: return hold, failure, or null
  runAgent->>AgentRun: create run for an eligible heartbeat
Loading

Possibly related issues

  • The hosted-heartbeat silence issue: this change allows eligible hosted heartbeats to create runs. Other observability requirements remain outside the change.
Merge Risk: Low · up to f9a1b

Hosted heartbeats now run real turns with hourly and daily limits. In rare concurrent cases, one owner's installations can slightly exceed the daily heartbeat cap. This is a bounded cost overshoot and does not block merge.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title names the main change: hosted agents with heartbeat on now run scheduled turns, limited to hourly per install and 6 per owner per day. It matches the changeset (adds heartbeat to HOSTED_NATI…
Description check Passed The description explains the bug, which is that hosted installs acknowledged heartbeats with no run. It lists the change to HOSTED_NATIVE_EVENT_TYPES, the hourly per-install floor, the daily per-owner…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: hosted agents with heartbeat on now run scheduled turns, limited to hourly per install and 6 per owner per day. It matches the changeset (adds heartbeat to HOSTED_NATIVE_EVENT_TYPES, adds hourly floor and daily cap). The title is long, at 119 characters, but it is specific and accurate.

Full details: Description check

Explanation

The description explains the bug, which is that hosted installs acknowledged heartbeats with no run. It lists the change to HOSTED_NATIVE_EVENT_TYPES, the hourly per-install floor, the daily per-owner cap with HOSTED_HEARTBEAT_RUNS_PER_DAY, fail-closed counting, cost estimates, the tests added, and a follow-up item. It is directly related to the changeset.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 3
  • Waived: $0.75 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comments: 1
backend/services/nativeRuntimeService.ts-1922-1927: 1

1922-1927: Major · Stability & Availability · Heavy lift

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Retry a heartbeat after a count failure instead of retiring its event.

If either count throws, this result makes agentEventService mark the heartbeat event failed. That state is terminal, so recovery of the count service cannot deliver the held heartbeat. Keep model execution blocked while the count is unavailable, but preserve a retryable wake. As per path instructions, a failed guard lookup must not lead to “a message or event nobody sees.” (raw.githubusercontent.com)

Source: Path instructions

Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 349032f and f9a1b40.

Files selected for processing: 3
  • backend/__tests__/unit/services/nativeRuntimeService.hostedEngine.test.js
  • backend/services/hostedEngine.ts
  • backend/services/nativeRuntimeService.ts

Limit details: You’ve used all 10 included reviews currently available.

@lilyshen0722
lilyshen0722 added this pull request to the merge queue Nov 4, 2026
Merged via the queue into main with commit 0589361 Nov 4, 2026
35 checks passed
cursor Bot pushed a commit to KGKallasmaa/commonly that referenced this pull request Nov 5, 2026
… previews its daily cap (Team-Commonly#3489)

* fix(settings): a hosted agent's heartbeat select starts at hourly and previews its daily cap

The hosted engine runs a heartbeat at most hourly per install and caps an
owner's hosted heartbeats a day (D244, Team-Commonly#3417), but Settings → Wake still
offered 15 and 30 minutes and previewed 1440 ÷ minutes. manage-settings now
returns hostedHeartbeat { minMinutes, runsPerDay } for a hosted install; the
panel offers intervals from the floor up, counts a stored shorter interval as
hourly, caps the preview at the daily cap, and says so in one line (en, zh-CN).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MDs9DRRdfD6THbfDgHSPZ5

* fix(settings): only an install the native engine runs is told it heartbeats

From the PR's review: the route treated every runtimeType 'hosted' row as
heartbeating, but the engine floors and caps (and runs) a hosted heartbeat
only when hostedRunsNative holds. A Durable Object install, a deprovisioned
native row or an unfinished migration runs none, and is now told runsPerDay 0,
so the panel previews 0 and says heartbeats aren't available. The English line
names "your" agents, matching zh-CN's 你.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MDs9DRRdfD6THbfDgHSPZ5

* fix(settings): hosted heartbeat bounds are per installation

From CodeRabbit on Team-Commonly#3489: runtimeType is a per-row field, so one identity's
installations need not agree. Each manage-settings row now carries its own
hostedHeartbeat, and the wake panel applies the hourly floor and the daily cap
to hosted rows only; a row that isn't hosted keeps every interval and counts
as it is set. The agent-level field stays for the explanatory line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MDs9DRRdfD6THbfDgHSPZ5

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant