Skip to content

Consolidate instance lifecycle subscriptions#189

Merged
sjmiller609 merged 2 commits into
mainfrom
codex/consolidate-instance-lifecycle-bus
Apr 6, 2026
Merged

Consolidate instance lifecycle subscriptions#189
sjmiller609 merged 2 commits into
mainfrom
codex/consolidate-instance-lifecycle-bus

Conversation

@sjmiller609

@sjmiller609 sjmiller609 commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • replace the old per-instance state subscription path with one shared lifecycle event bus in lib/instances
  • migrate WaitForState to filter the lifecycle bus and keep its polling fallback
  • add lifecycle bus backpressure metrics for subscriber count, max queue depth, and dropped events

Why This Change

We were about to introduce a second internal subscription mechanism for the upcoming auto-standby work. The existing WaitForState path used a per-instance state subscription that only carried State/StateError, while the new controller needs a global stream of lifecycle changes like create, update, fork, and delete.

Instead of keeping two different in-memory buses, this change consolidates on one richer lifecycle event stream and makes WaitForState a consumer of that shared path. That keeps the public API the same, reduces duplicated fanout logic, and gives us one place to observe subscriber backpressure.

Testing

  • go test ./lib/instances -run 'TestWaitForState|TestLifecycleSubscribers|TestLifecycleEventMetrics'
  • go test ./lib/builds -run 'Test'
  • go test ./lib/instances -run '^$'
  • go test ./lib/builds -run '^$'
  • go test ./cmd/api/api -run '^$'

Note

Medium Risk
Changes core instance lifecycle signaling by replacing per-instance state subscriptions with a shared lifecycle event bus, which could affect wait/polling behavior and internal controllers if events are missed or dropped. Adds new buffering/config/metrics paths that need validation in production workloads.

Overview
Consolidates internal instance change notifications by replacing the per-instance StateChange subscription mechanism with a shared LifecycleEvent bus, and updates the Manager interface to expose SubscribeLifecycleEvents.

WaitForState now consumes this shared stream (filtering by instance_id and handling delete explicitly) while retaining the 5s polling fallback, and manager mutation methods (CreateInstance, UpdateInstance, Start/Stop, Standby/Restore, Fork*, DeleteInstance) publish lifecycle events on success.

Adds observability and tuning for backpressure: new metrics for subscriber count, max queue depth, and dropped events, plus a new instances.lifecycle_event_buffer_size config setting (default 256) wired through providers into instances.ManagerConfig and validated in config/tests.

Reviewed by Cursor Bugbot for commit 134628d. Bugbot is set up for automated code reviews on this repo. Configure here.

@sjmiller609 sjmiller609 requested a review from hiroTamada April 6, 2026 20:07
@sjmiller609 sjmiller609 marked this pull request as ready for review April 6, 2026 20:07

@hiroTamada hiroTamada left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

solid refactor — clean fan-out bus with good observability. two minor comments.

Comment thread lib/instances/wait.go
Comment thread lib/instances/manager.go
@sjmiller609 sjmiller609 merged commit ce389be into main Apr 6, 2026
8 checks passed
@sjmiller609 sjmiller609 deleted the codex/consolidate-instance-lifecycle-bus branch April 6, 2026 21:48
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.

2 participants