Public: anyone can read this; the crew can change it.

Meter awake time and storage: per app and environment, the two quantities a forge charges for #518

Closed opened this
Closed without merging

The forge now records how long each app's environments are awake and how much storage each app uses, the two quantities a forge charges for. This is the base for the profit slider (#392).

Notes

stopped closed without shipping deploy c58185b

opened this

The forge now records how long each app's environments are awake and how much storage each app uses, the two quantities a forge charges for. This is the base for the profit slider (#392).

What it does

  • Migration 40 adds two tables:
    • meter: one row per awake interval, with repo, env, placement (owned|cloud), start, last heartbeat and end. Only one interval per environment can be open at a time.
    • storage: one row per app per sample, with data bytes, repository bytes and the time.
    • repo_id is plain text in both, so removing an app does not erase what it used (same as usage).
  • src/core/meter.ts writes both ledgers and reads them:
    • Writes: open, close, close-all on shutdown, heartbeat, close leftover intervals at boot, record a storage sample.
    • Reads: usageBetween(db, from, to, now?) gives awake seconds per app for a [from, to) range, split by placement, and GB-hours (bytes × hours / 1e9). Each storage sample covers the hour after it, or until the next sample if that comes sooner. An interval still open counts up to now and no further.
  • src/apps/runtime.ts:
    • An interval opens when a deploy or a wake starts serving. A redeploy keeps the same interval open.
    • It closes when the environment sleeps, is stopped, crashes, is torn down, or the forge shuts down.
    • Every sweep is a heartbeat. At boot, an interval the previous forge left open is closed at its last heartbeat.
    • Storage is sampled at boot and then hourly (storageEveryMs). A failed meter write is logged and never stops a deploy, a sleep or a shutdown.
  • src/apps/bytes.ts counts a directory's bytes: regular files only, symlinks not followed, and a missing directory counts as 0. The walk is async, so it does not block requests.
  • New RuntimeOptions.placement, which defaults to owned.
  • docs/APPS.md has a new "The meter" section.

Things to look at

  • Placement: this runtime always runs processes locally, so nothing records cloud yet; the option is there for a cloud runtime to set.
  • Storage: samples are one row per app, with data bytes summed across all of its environments (previews included), rather than one row per environment.
  • docs/PROFIT.md: the issue refers to it but it does not exist in the repository, so I followed the issue's acceptance list. There is no page or API for the meter yet; that is left for the slider work.

Tests

  • test/meter.test.ts is new and added to the check.
  • Start, redeploy, sleep, wake and stop produce intervals that match a hand count (55 minutes, split owned and cloud), including a partial range.
  • An open interval counts up to now and is closed at shutdown.
  • GB-hours are checked by hand, including a sample cut short by the next one.
  • A restart closes a leftover interval at its last heartbeat (7 minutes).
  • A fixture directory counts to exactly 5,100 bytes, and a 1 MB file behind a symlink is not counted.
  • With a real runtime, the recorded intervals start when a deploy starts serving and end when it sleeps, wake and stop open and close a second one, and the repository bytes sampled match the count on disk.
  • bun x tsc --noEmit is clean. The full check ran 620 tests with 1 failure: "a mirror whose git is not ready yet is fetched again, and lands" in test/fork-flow.test.ts. That file does not touch the meter or the runtime, and run on its own it passes 15/15, so it looks like a timing flake under load.

🤖 Generated with Claude Code

Closes #505

commented

Parked by the crew: the check failed ("the fork flow > a mirror whose git is not ready yet is fetched again, and lands") and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs