Skip to content

feat: the lazy CLI last mile, report mode (libs#78) - #1670

Merged
scttfrdmn merged 3 commits into
mainfrom
feat/lastmile-report
Nov 20, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
feat/lastmile-report

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Part of spore-host/libs#78 (leave-no-trace, spore-host/spore-host#752). This is the spawn half: it wires lastmile from libs v0.55.0 into the spawn CLI, in report mode, following lagotto#337's design. (v0.54.0 was tagged before libs#84 merged and has no lastmile.) The root module and both nested modules that use libs (lambda/ttl-reaper, lambda/sweep-orchestrator, indirect) are on v0.55.0.

Hook point

rootCmd.PersistentPostRunE = lastMileHook (cmd/root.go, cmd/lastmile.go).

  • It runs once per invocation, after the command succeeds. Cobra runs no post-run after a failure, so a failed command keeps its own error and exit status. root.go sets cobra.EnableTraverseRunHooks, so a group with its own post-run neither hides it nor runs it twice (tested).
  • The hook always returns nil and recovers panics. The work runs in a goroutine that the hook stops waiting for after the budget plus 250 ms.
  • Skipped after completion (and __complete), help, version, footprint, teardown, doctor, cleanup, every reaper subcommand, and every hidden top-level command (gen-docs, bootstrap-body, awscli-pin, spored-signing-key; discovered from Hidden, not listed). Gated on lastmile.ModeFromEnv(): SPORE_LASTMILE=off skips it.
  • Output: nothing to stdout, nothing at all at default verbosity. With --verbose, debug: lines on stderr: the Result.Summary() line, each would-action and each note.
  • No STS call. cmd.Execute installs a passive middleware (lastmilespawn.AccountObserver) through a new, inert seam, spawnconfig.SetCLIAPIOptions. The caller's-account loaders (pkg/aws.NewClientWithRegion, LoadComputeAWSConfig, and LoadInfraAWSConfig only when infra is the caller's own profile) append it to every config. It records the account from the command's own GetCallerIdentity responses, the --account guard's included, and records nothing if two calls name different accounts (a compute profile in another account). A command that made no STS call skips the last mile. The seam is empty in every other program, so nothing is recorded inside lagotto, calque or spore-host-mcp.
  • Never in the infra account. An account equal to footprintspawn.InfraAccountID is skipped.
  • No AWS call between checks. lastMileMaybeDue reads ~/.spore-host/lifecycle.json (version 1) first. Anything it can't read counts as due, and lastmile.Run then makes the exact decision.
  • Backoff after a cut-off run. A run the budget cut off is not retried for an hour.

Why there's a new pkg/lastmilespawn

cmd/aws_imports_test.go (#326/#327) allows cmd no new direct AWS SDK service imports. So the AWS-touching pieces live in pkg/lastmilespawn: the account observer, the read-only lifecycle wrapper, the residue and its re-check, the report-only deleter and the infra probe. None of them runs by itself. The wiring (the hook, the skip list, the seams, the rate-limit pre-check, the options and the visibility decision) is all in cmd/lastmile.go, so it can't run inside another program's process. pkg/lastmilespawn and pkg/config are not in the public-surface table, and api/public-surface.txt is unchanged.

Options

Option Value
Tool tags.ToolSpawn
Account the account the command's own STS call reported (above)
Regions every region enabled for the account (GetEnabledRegions, ec2:DescribeRegions), listed from us-east-1 when no region is configured
Lifecycle lastmilespawn.NewLifecycleReader, with ControlRegion = lifecyclemark.ControlRegion (us-east-1, where spawn's creators and the reaper write the global scope). Its DynamoDB clients refuse every write (ReadOnlyLifecycle), as defence in depth under report mode. After one IAM refusal they stop asking.
Footprint pkg/aws.AccountFootprint(account, regions) (spawn footprint --all-regions -o json), read at most once per run (a mutex, not sync.Once: see the iamcoverage note)
Deleter lastmilespawn.ReportOnlyDeleter. The residue is re-checked by ResidueChecker; everything else by pkg/teardown.NewReportOnly, the deleter the reaper's account-idle sweep plans with. Delete always returns ErrReportOnly.
Patterns footprintspawn.Patterns plus the residue's two exact names (lastmilespawn.Patterns)
Residue the role spawn-reaper-teardown-invoke (global), plus the spawn-reaper-teardown schedule group in every region, per #1668. Each group DependsOn the role. All are tagged spore-host:managed-by=spawn and none carries spore-host:role=service.
FullVisibility false, and Activity is nil (below)

How the residue is re-checked. The re-check is targeted and never loads the footprint (the "no spawn item anywhere" case is common: see IAM below):

  • the role is gone when iam:GetRole says NoSuchEntity;
  • the role is in use while the reaper's own role, spawn-ttl-reaper-selfscan-role, exists, because the jobs that remove that role run as this one (a reaper still deployed, or a self-removal that hasn't finished);
  • a group is gone when scheduler:GetScheduleGroup says not found, and in use while ListSchedules finds a job in it. That job also keeps the role (DependsOn).

pkg/teardown has no deleter for either one: spawn reaper teardown removes them. That's why their names are in the last mile's Patterns only, never in footprintspawn.Patterns. When libs#79 turns on reap, someone must wire the reaperdeploy deletes deliberately.

The spore-host-lifecycle tables are not in Residue. They belong to the suite, and lastmile reports each empty one itself, to go through CloseTableIfEmpty.

The FullVisibility decision: false

lastMileVisibility sets FullVisibility only when both of these hold:

  1. there is an activity reader built from the reaper's own verdict code;
  2. the caller can read the infra-account signals: LoadInfraAWSConfig succeeds, and a one-item Scan of each of spawn-sweep-orchestration, spawn-pipeline-orchestration and spawn-schedules works (lastmilespawn.InfraSignalsReadable, the same three tables as the reaper's sweep; a missing table counts as readable, as it does there).

Production has no reader, so FullVisibility is false, Activity is nil, and the infra tables are not even read. The reasons:

  • The verdict is not reusable from cmd. accountVerdict and its signal readers are package main of lambda/ttl-reaper, a separate module. They also import calque/pkg/calquefootprint for calque's activity, which spawn's root module doesn't depend on (calque imports spawn).
  • A second "is spawn active" in cmd is the wrong fix. A teardown driven by one of two opinions deletes what the other could see.
  • Report mode loses nothing. lastmile never calls Observe. Without the reaper's sweep, no scope is ever recorded idle, so FullVisibility could only ever say "Observe would start the grace now".

Moving the verdict into a pkg/ that the reaper and the CLI share is the precondition for setting it, with libs#79. The decision and the probe are tested both ways with fakes.

IAM

Nothing is provisioned and no policy changes. All the last mile's calls are reads, and a refused read is silent at default verbosity: one debug: lastmile: not permitted, skipped: ... line under --verbose, never a warning. After the first refusal by IAM, the lifecycle, IAM and Scheduler readers each stop asking, so an identity without the grants costs one denied request per run per service, not one per region.

What a run can call, once per 6 h per account:

  • ec2:DescribeRegions;
  • per region, dynamodb:GetItem, Scan and DescribeTable on spore-host-lifecycle;
  • when no spawn item is left anywhere: iam:GetRole (×2), and per region scheduler:GetScheduleGroup, plus ListTagsForResource and ListSchedules where the group exists;
  • only for a stale lease: the full footprint, plus the report-only re-check of the plan.

Users today mostly can't read the lease table (spawn#1666), so for them the run ends at the first denied GetItem.

docs/reference/cli-iam-actions.md is regenerated. The inventory walk now includes the root's persistent post-run, so the last mile's calls appear under "Outside any command", and the page says what it is. That list includes delete and write actions: they're the static over-approximation of footprint.Execute and the deleter call graph (the same set spawn teardown lists), and the last mile never makes them. VTA also merges every footprint.Deleter that reaches footprint.Execute, so spawn teardown now lists three Scheduler reads (GetScheduleGroup, ListSchedules, ListTagsForResource). Those are reads, and teardown never makes them.

Tests

cmd/lastmile_test.go, pkg/lastmilespawn/*_test.go, pkg/config/cli_api_options_test.go, pkg/aws/client_region_test.go. All are hermetic, with no AWS access: lifecycletest, fakes and a refusing HTTP client.

When it runs

  • It runs exactly once after a command executed through the real root.
  • It runs once under a group that has its own post-run.
  • Every name in the skip list is a real command and is skipped. So are every reaper subcommand and every hidden top-level command. help, completion bash and __complete run end to end. list runs it.
  • SPORE_LASTMILE=off / OFF / Off skip it before any call. Unset, report, reap and unknown values all run as report, with zero writes and empty Did.
  • No account, or the infra account: skipped with no region listing.

Never in the way

  • Stdout holds exactly the command's output when the last mile finds a stale lease and residue, when the lifecycle reads fail, when Run, the region listing or the footprint panic, and when Run returns warnings and would-actions. This holds with and without --verbose. It's captured through both the command's writers and the process's fds.
  • Stderr is empty at default verbosity. Under --verbose, every line starts with debug: lastmile.
  • Failing commands keep their exit code (remote exit 3, aborted, plain 1), and the last mile doesn't run after them.
  • Budget: a Run that ignores its context, and a lifecycle table that never answers, both return within budget + slack.

Behaviour

  • Options wiring: regions, control region, patterns, residue and its DependsOn, no role=service, FullVisibility false and Activity nil. Deleter.Delete and a MarkActive through the client are both refused.
  • The FullVisibility decision, both ways: no reader gives false, and the infra probe is not called. Reader plus unreadable infra gives false. Reader plus readable infra gives true, and lastmile asks the reader.
  • A stale lease is reported with its plan re-checked, with zero writes, and the item is unchanged.
  • Residue wiring: with no item anywhere, the invoke role is a would-delete and an empty table a would-close. Not while the reaper's role exists. A group holding a job keeps itself and the role. An empty group is a would-delete. A spawn item keeps everything ("residue kept"). None of these load the footprint.
  • AccessDenied: silent at default verbosity, one debug line under --verbose, one denied request across 4 regions.
  • Rate limit: no AWS call inside 6 h, a run after. A cut-off run is not retried at +30 min and is retried at +90 min. lastMileMaybeDue cases.
  • Each pre-Run step that fails skips with one debug line. A footprint failure means no would-delete.
  • Production seams through a refusing client; the observer records the command's own call with no extra request; a source gate confirms Execute installs it.
  • pkg/lastmilespawn (100% statements): the observer (conflict, a doubled middleware, failures); the latch (refusals only, not throttles); every write refused before it reaches the table; the residue checker's every branch and its per-service latches; the deleter's routing; the infra probe.

Revert checks. Each was applied with a script, built, seen to fail, and reverted:

  • the summary printed to stdout (fmt.Println);
  • verbose output sent to cmd.OutOrStdout();
  • logging at default verbosity;
  • the hook returning an error;
  • dropping either recover;
  • waiting 10 s past the budget;
  • dropping the rate-limit pre-check;
  • dropping the cut-off backoff;
  • dropping the line collapse;
  • FullVisibility forced true;
  • skipping the infra probe;
  • dropping the infra-account skip;
  • dropping the hidden-command skip;
  • dropping the read-only wrapper;
  • dropping the latch;
  • dropping the reaper-role in-use check;
  • dropping the group's DependsOn;
  • dropping the job in-use check;
  • Execute not installing the observer;
  • the compute loader and NewClientWithRegion not appending it;
  • the observer ignoring a conflict.

Gates

  • make check from a space-free copy at the physical path /private/tmp/lm-spawn-check: gofmt, vet, every GOOS, staticcheck, golangci-lint (0 issues in all 10 modules), hermetic short tests (./cmd, ./internal/iamcoverage, which includes TestEveryCreatorMarksTheLease and the principal grants, and ./pkg/lastmilespawn), nested modules, tidy and build tags: all passed.

  • The last-mile tests pass with -race.

  • make cli-iam-actions regenerated and current.

  • make gen-docs: no diff. The last mile skips the hidden gen-docs.

  • make api-snapshot: no diff. TestPublicAPISurface passes; no public package changed its exported surface.

  • The changelog test passes: Added has the last mile, Changed has the libs bump.

  • The Tier 0 coverage table is unchanged: no command was added or altered. The hook is a root post-run, and the gate compares the command list.

  • semgrep scan --config=auto --error on every changed file: clean, with no nosemgrep added.

  • Patch coverage of the new and changed code (deduplicated statement blocks): cmd/lastmile.go 97.6% (the 3 lines not hit are the lifecycle-client error branch, which can't fire with a ControlRegion set); pkg/lastmilespawn 100%; pkg/config/cli_api_options.go 100%. Overall 99.0% (298/301). The one other changed line not covered is the SetCLIAPIOptions call in Execute, which calls os.Exit; a source gate covers it.

  • CI: every check is green, including codecov/patch, Test (Windows) and E2E Tier 0 (Substrate). The first Windows run failed. no budget left used a 1 ns budget, and on Windows' coarse clock that can still read as positive. The second commit makes the budget negative, which is already past its deadline everywhere.

For libs#79

  • Reap needs a deleter for the residue. pkg/teardown can't delete the invoke role or the schedule group. Wire pkg/reaperdeploy's deletes, and replace ReportOnlyLifecycle and ReportOnlyDeleter deliberately; the tests pin both.
  • FullVisibility needs the reaper's verdict in a shared pkg/. Until then the CLI can't judge spawn idle.
  • The same two things lagotto asked for would let spawn drop code: an exported lastmile due-check, and lastmile's own backoff after a cut-off scope.
  • Who can reap. As with lagotto: a CLI user that can't read the lease table can't finish what it would resume.

@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.82698% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/lastmile.go 97.88% 3 Missing ⚠️
cmd/root.go 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn marked this pull request as ready for review November 20, 2026 19:04
@scttfrdmn
scttfrdmn force-pushed the feat/lastmile-report branch from 1763ebe to fd567cc Compare November 20, 2026 19:11
scttfrdmn and others added 3 commits November 20, 2026 11:57
After a command succeeds, the root command's post-run hook runs
libs/lastmile (v0.55.0) for spawn: it reports a stale teardown lease (with
its plan re-checked by pkg/teardown's report-only deleter), the self-hosted
reaper's teardown residue (the spawn-reaper-teardown-invoke role and each
region's spawn-reaper-teardown schedule group, #1668) once no spawn
lifecycle item is left, and an empty spore-host-lifecycle table.

Report only and never in the way: nothing on stdout, nothing at all
without --verbose (then debug: lines on stderr), the exit status is
untouched, the hook stops waiting after the 30 s budget, between 6-hourly
checks it makes no AWS call, and a cut-off check waits an hour. The
account comes from the command's own GetCallerIdentity, observed
passively through an inert pkg/config seam that only cmd.Execute sets.
The lifecycle client refuses every write and the Deleter every delete.
Skipped after completion, help, version, footprint, teardown, doctor,
cleanup, every reaper subcommand and hidden plumbing commands, and in the
infra account; SPORE_LASTMILE=off turns it off.

FullVisibility is false: the account-idle verdict is package main of
lambda/ttl-reaper and not reusable from cmd. The decision (an activity
reader AND readable infra-account tables) is encoded and tested both ways.

The wiring is in cmd/lastmile.go; the AWS-touching pieces are in the new
pkg/lastmilespawn (cmd may not import SDK services, #326/#327). libs is
bumped to v0.55.0 in the root, ttl-reaper and sweep-orchestrator modules.

Part of spore-host/libs#78.
…lock)

A 1 ns budget can still read as positive under Windows' coarse clock, so
lastmile.Run ran. A negative budget is past its deadline everywhere.

Part of spore-host/libs#78.
@scttfrdmn
scttfrdmn force-pushed the feat/lastmile-report branch from fd567cc to 2e06e13 Compare November 20, 2026 19:57
@scttfrdmn
scttfrdmn merged commit 391bb7a into main Nov 20, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the feat/lastmile-report branch November 20, 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.

1 participant