Skip to content

feat: spawn capacity-block purchase with three typed gates + dry-run (#217) - #219

Merged
scttfrdmn merged 2 commits into
mainfrom
feat/217-capacity-block-purchase
Jun 17, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
feat/217-capacity-block-purchase

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #217. Step 3 of the Capacity Blocks for ML flow (find → purchase → launch). The paid write op.

What

spawn capacity-block purchase <offering-id> --region <r> --instance-type <t> --duration-hours <h> [--count N] [--platform] [--dry-run] — reserves a Capacity Block for ML via ec2:PurchaseCapacityBlock, returning a reservation id that feeds spawn launch --reservation-id … --capacity-block (#216). The offering id comes from truffle capacity-blocks (truffle#67).

Cost-safety (the heart of #217) — all hard requirements met

A Capacity Block is a non-refundable up-front charge, the most expensive action in the toolset. Gated accordingly:

  1. Three typed confirmations, in sequence, each an exact (trimmed) match or it aborts with no purchase:
    • Gate 1: the exact up-front price (e.g. 12480.00).
    • Gate 2: purchase <offering-id> (binds intent to the specific offering).
    • Gate 3: I UNDERSTAND THIS IS NON-REFUNDABLE.
      A bare y/yes is explicitly not accepted.
  2. Refuses on non-interactive stdin (stdinIsInteractive via os.Stdin.Stat() ModeCharDevice) — no --yes bypass.
  3. --dry-run previews price + full terms with no write API call.
  4. Re-validates the offering and re-checks the price immediately before charging; aborts if it moved from what was confirmed.
  5. Audit-logged (initiated/success/failed: offering id, reservation id, price, account).

Code

  • pkg/aws/capacity_block.go: FindCapacityBlockOffering (re-validate by id via DescribeCapacityBlockOfferings) + PurchaseCapacityBlock (InstancePlatform default Linux/UNIX, tag support).
  • cmd/capacity_block.go: the capacity-block purchase command (parent + child, mirrors slurm).
  • cmd/utils.go: confirmTypedPhrase (exact-match gate) + stdinIsInteractive.

Tests / verification

  • TestConfirmTypedPhrase exhaustively covers the gate: wrong text / y / yes / empty / EOF / case-mismatch all abort; only the exact phrase proceeds — for all three phrases (price, offering, ack).
  • No real purchase in tests; CI never calls PurchaseCapacityBlock.
  • Verified live (read-only): validation rejects missing flags; --dry-run reaches DescribeCapacityBlockOfferings and makes no purchase (the dev account isn't Capacity-Block-enabled, which surfaces as a clean API limit error — request well-formed).

Refs truffle#67 (discovery), spawn#216 (launch into the reservation), lagotto#49 (schedule the launch at the block's start). Also carries the [email protected] pin via the #216 merge.

…217)

New 'spawn capacity-block purchase <offering-id>' — reserve an EC2 Capacity
Block for ML. A non-refundable up-front charge (the most expensive action in
the toolset), so it is heavily gated:

- THREE typed confirmations in sequence, each an exact match or it aborts:
  1. the exact up-front price, 2. 'purchase <offering-id>',
  3. 'I UNDERSTAND THIS IS NON-REFUNDABLE'. A bare y/yes is NOT accepted.
- Refuses on non-interactive stdin (stdinIsInteractive via os.Stdin.Stat
  ModeCharDevice); there is NO --yes bypass.
- --dry-run previews price + terms with NO write API call.
- Re-validates the offering and re-checks the price immediately before
  charging; aborts if it moved from what was confirmed.
- Audit-logs initiated/success/failed (offering id, reservation id, price).
- On success prints the reservation id + the 'spawn launch --reservation-id
  <id> --capacity-block --az <az>' command (#216).

pkg/aws/capacity_block.go: FindCapacityBlockOffering (re-validate by id via
DescribeCapacityBlockOfferings) + PurchaseCapacityBlock (PurchaseCapacityBlock,
InstancePlatform default Linux/UNIX, tag support).

cmd/utils.go: confirmTypedPhrase (exact-match gate) + stdinIsInteractive.

Tests cover the gate logic exhaustively (wrong/empty/y/eof/case-mismatch all
abort; only exact proceeds) for all three phrases. No real purchase in tests.

Closes #217
@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 9.54774% with 180 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/capacity_block.go 10.71% 100 Missing ⚠️
pkg/aws/capacity_block.go 0.00% 74 Missing ⚠️
cmd/utils.go 53.84% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

The new capacity-block command must declare a Tier 0 coverage decision
(TestTier0_CommandCoverageGate). Purchase is a non-refundable write never run
in CI and offerings discovery hits real DescribeCapacityBlockOfferings, so it's
deferred to Tier 1/3; the typed-gate logic is unit-tested in cmd.
@scttfrdmn
scttfrdmn merged commit 2f4198a into main Jun 17, 2026
6 of 7 checks passed
@scttfrdmn
scttfrdmn deleted the feat/217-capacity-block-purchase branch June 17, 2026 20:23
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.

Purchase a Capacity Block for ML (PurchaseCapacityBlock) — the paid reservation write op

1 participant