Skip to content

Record workload hibernation - #89

Merged
vitramir merged 3 commits into
mainfrom
claude/hibernation-statuses-da6d73
Oct 31, 2026
Merged

vitramir merged 3 commits into
mainfrom
claude/hibernation-statuses-da6d73

Conversation

@vitramir

@vitramir vitramir commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Runners-service half of workload hibernation: the record, the write path and the turn-boundary report the Orchestrator and the coming k8s-microvm-runner need. The k8s-runner is unaffected and nothing advertises hibernate yet.

Design: agynio/architecture#192, change doc changes/2026-10-27-workload-hibernation.md (Runners service row), spec architecture/runners.md (Workload Resource, Workload State Management, Runner-Reported Workload State, Idle Timeout, Agent Activity Sweep). Interface: buf.build/agynio/api as merged in agynio/api#199.

What changed

  • Status vocabulary gains hibernating, hibernated, waking in the Go maps and in the DB check (migrations/0017_workload_hibernation.sql replaces workloads_status_check). None of the three is terminal: a hibernated workload is live.
  • Record fields hibernated_at, woken_at, snapshot_size_bytes (nullable), scanned, returned on every read, accepted by UpdateWorkload.
  • Transition effects are derived from the recorded status and the one being written, never from the writer, so a runner report and the Orchestrator's reconciliation leave the record identical:
    • entering hibernated stamps hibernated_at = now() unless the writer supplied it;
    • waking → running sets woken_at and last_metering_sampled_at to now (explicit values win), agent_state = processing, last_activity_at = now(), and clears snapshot_size_bytes and hibernated_at;
    • hibernating → running (the runner could not persist) resets nothing: the agent stays idle for the retry.
  • ReportWorkloadState accepts the three statuses and carries snapshot_size_bytes through the UpdateWorkload path; the observed_at ordering and the no-change-no-event rule are unchanged.
  • ReportWorkloadIdle flips a running agent workload from processing to idle in one conditional UPDATE and publishes workload.updated the way TouchWorkload and the sweep do. Authorized as TouchWorkload: the workload's own identity, agent-instance owners only. A no-op (no event, no error) when already idle or not running, so a retry is not a second event.
  • TouchWorkload admits the identity-less call on an agent workload as the Terminal Proxy's, which touches every attached TTY session's workload whatever its owner kind and carries no x-identity-id (the mesh policy admits that call from its principal alone). It runs as the agent's own keepalive: an idle agent flips to processing with workload.updated, a processing one only moves last_activity_at, so an agent is not hibernated under the person at its shell. A present identity still has to be the owner's; the sandbox case is unchanged.
  • Failure reasons wake_failed and snapshot_lost.
  • Rooms: every workload.updated, whether from UpdateWorkload, TouchWorkload, ReportWorkloadIdle or the sweep, goes to the cluster-wide workloads room as well as the organization's and the workload's own, so the Orchestrator, which holds only the cluster-wide rooms, hears the agent_state flip at the turn boundary instead of on its poll tick (architecture/notifications.md, Cluster-wide rooms). workload.status_changed keeps its two rooms.
  • Chart: ReportWorkloadIdle joins TouchWorkload in the Istio policy's identity-required gateway paths.
  • Catalog: hibernate already passes the capability name rule; a test pins it.

Decisions where the spec left room

  • hibernated_at is cleared on the wake. The field table says it is NULL while the workload is awake; the step-5 write list in Workload State Management did not mention it. Nothing reads it after a wake (TTL and the sampler key on status = hibernated).
  • A report carrying snapshot_size_bytes with a status other than hibernated is refused with InvalidArgument: the size describes a sleep, and on running it would outlive the wake meant to clear it.
  • pending_sample needed no code change. Every hibernated workload has removed_at IS NULL, which the existing clause already returns, covering both the compute tail up to hibernated_at and the snapshot storage. A comment at the clause says so.
  • ReportWorkloadIdle does not touch last_activity_at; the idle clock keeps running from the last keepalive, and hibernate_after is measured from there.
  • Generated code is not committed here (.gen/ is ignored; CI and the Dockerfile generate from the module's latest main). buf dep update cannot run in this repo because it has no local protos, so buf.lock is unchanged.

Verification

  • go build ./..., go vet ./..., gofmt -l, go test ./... green; 25 new tests across vocabulary, transitions, UpdateWorkload, ReportWorkloadState, ReportWorkloadIdle, TouchWorkload, notification rooms and the catalog.
  • All 17 migrations applied in order to a throwaway Postgres 16: the replaced check carries the eight statuses, the three columns are nullable, the new statuses write, an unknown status is still refused.
  • helm template and helm lint of the chart with its dependency fetched.

Nothing in the platform knew a workload that is neither running nor
stopped: an idle agent either costs its flavor until the idle timeout
or cold-starts for every message. The k8s-microvm-runner will pause a
workload, persist it and resume it in under a second, and the
Orchestrator will decide when. This is what the Runners service holds
for them; the k8s-runner is unaffected and nothing advertises hibernate
yet.

The workload record gains hibernating, hibernated and waking, with
hibernated_at, woken_at and snapshot_size_bytes. What a status change
carries is derived from the recorded status, never from who is writing,
so a runner's report and the Orchestrator's reconciliation leave the
record identical: entering hibernated stamps hibernated_at unless the
writer supplied it; waking -> running records woken_at, moves
last_metering_sampled_at to now so compute billing resumes at the wake
and never covers the sleep, puts agent_state back to processing, opens
a fresh keepalive window and clears the fields that described the
sleep. A hibernation that did not take (hibernating -> running) resets
nothing.

ReportWorkloadState accepts the three states and, with hibernated, the
size of the persisted state. ReportWorkloadIdle is the turn-boundary
report: agynd calls it after acking a turn's inbox items, and the
workload goes idle at once instead of after the keepalive grace, with
the event the sweep would have published -- the event the Orchestrator
hibernates on. It is authorized as TouchWorkload is, and the chart's
policy requires an identity on it the same way. wake_failed and
snapshot_lost name the two ways a sleep ends badly.

pending_sample needed no change: a hibernated workload has removed_at
NULL, which already returns it to the sampler for its compute tail and
its snapshot storage. hibernate passes the catalog's name rule as any
capability does.
The Orchestrator hibernates an agent on the agent_state flip that ends
its turn, and it holds only the cluster-wide rooms: it reconciles every
workload and cannot subscribe per workload without racing the creation
of the next one. workload.updated went to the organization's room and
the workload's own alone, so the flip that ReportWorkloadIdle,
TouchWorkload and the activity sweep record never reached it, and the
hibernation decision waited for the next poll tick instead of following
the turn end.

Every workload.updated now goes to the flat workloads room as well,
whoever emits it, as the spec now says. workload.status_changed is as
it was.
The Terminal Proxy touches the workload of every attached TTY session,
a sandbox's or an agent's, and carries no identity as it never did; the
mesh policy admits an identity-less TouchWorkload from its principal
alone. touchWorkloadForOwner refused every such call on an agent
workload, so an agent that had finished its turn was hibernated under
the person at its shell, the one thing the touch exists to prevent.

An identity-less touch on an agent workload is now the Terminal Proxy's
and runs as the agent's own keepalive does: an idle agent flips back to
processing and workload.updated goes out, a processing one only moves
its timestamp. A present identity still has to be the owner's, and the
sandbox case is as it was.
@vitramir
vitramir merged commit b736905 into main Oct 31, 2026
1 of 2 checks passed
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