Skip to content

cloud-waste: add idle EC2 instance detection (v1.1, ADR-0022) - #74

Merged
Nabzx merged 1 commit into
mainfrom
feat/idle-instance-detection-73
Sep 4, 2026
Merged

Nabzx merged 1 commit into
mainfrom
feat/idle-instance-detection-73

Conversation

@Nabzx

@Nabzx Nabzx commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Closes #73.

What

A second Adapter in cloud-waste, IdleInstanceAdapter, alongside the existing CloudWasteAdapter - one Adapter per action type per ADR-0006, not one Adapter branching on action shape.

  • client.py: Instance, InstanceNotFoundError, list_instances()/stop_instance()/get_stopped() on CloudClient/FakeCloudClient, and the idle thresholds (IDLE_CPU_PERCENT_THRESHOLD, IDLE_NETWORK_BYTES_THRESHOLD) taken from AWS Trusted Advisor's own "Low Utilization EC2 Instances" convention - flagged in comments as needing re-verification before any real client replaces the fake one.
  • adapter.py: IdleInstanceAdapter - revalidate() re-checks CPU/network against the thresholds and confirms the instance is still running; execute() calls stop_instance.
  • detector.py: scan_for_idle_instances(), mirroring the existing address scan, with the same optional Critic attachment (ADR-0021).
  • demo.py: now runs both flows end to end - address release, then instance stop - sharing one outbox/approval/audit trail.

Completion here is a state fact, not a ledger: StopInstances is inherently idempotent, so get_stopped just checks the instance's current state rather than any per-key record. A fourth distinct idempotency mechanism proven in this codebase (after Stripe's re-issued call, wallet-guard's nonce ledger, and cloud-waste's own "resource ceases to exist" for address release) - and the first that needs no dedup ledger at all.

Chaos coverage

test_chaos.py's crash-injection driver was already adapter-agnostic (it only calls the three Adapter methods), so it's now parametrised over both resource kinds rather than duplicated. Holds exactly-once at the 200-trial CI default and a 5,000-trial local stress run for both release_address and stop_instance - zero violations either way.

Verification

  • ruff check . / ruff format --check . / mypy . - all clean.
  • pytest -v - 54 passed.
  • uv run python -m cloud_waste.demo - both flows print correctly; only the genuinely unassociated address and genuinely idle instance are acted on.
  • Diff scoped entirely to cloud-waste/ - no other package touched.

Second Adapter in the package, IdleInstanceAdapter, alongside the
existing CloudWasteAdapter - one Adapter per action type, not one
Adapter branching on shape (ADR-0006).

Idle detection uses AWS's own idle-instance convention as a starting
threshold (avg CPU and network bytes, re-verify against AWS's current
guidance before any real client replaces the fake one). Completion is
a state fact this time, not a ledger: StopInstances is inherently
idempotent, so get_stopped just checks the instance's current state -
a fourth distinct idempotency mechanism proven in this codebase, and
the first that needs no dedup ledger at all.

Extended test_chaos.py's crash-injection harness to drive both
resource types through one adapter-agnostic driver rather than
duplicating it, per the issue's own scope. Holds exactly-once at the
200-trial CI default and a 5,000-trial local stress run for both
release_address and stop_instance, zero violations either way.

demo.py now runs both flows end to end: unassociated-address release,
then idle-instance stop, sharing one outbox/approval/audit trail.

54 tests passing, ruff/mypy clean.

Closes #73
@Nabzx
Nabzx merged commit 8ce7be2 into main Sep 4, 2026
7 checks passed
@Nabzx
Nabzx deleted the feat/idle-instance-detection-73 branch September 4, 2026 03:13
Nabzx added a commit that referenced this pull request Sep 4, 2026
…rched

ROADMAP's Phase 5 and open-decisions sections, and the README's status
section, both still described #73 as pending. Updated both now it's
merged (#74): cloud-waste has a fourth Adapter shape, and the open-
decisions list has nothing queued.
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.

Build idle-EC2-instance detection (v1.1) for cloud-waste, per ADR-0022

1 participant