Skip to content

fix(sweep): honour --detach without --max-concurrent; decide the sweep path once (#1535) - #1536

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/1535-detach-dispatch
Nov 6, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/1535-detach-dispatch

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1535

What was wrong

cmd/launch_sweep.go took the orchestrator only for detach && maxConcurrent > 0, and only the auto-detach branch (no detach flag) set a max-concurrent (min(rows, 10)). An explicit --detach skipped that branch, so max-concurrent stayed 0 and the sweep ran in the foreground while the summary printed Orchestration: Lambda (detached). A sweep with no flag was already genuinely detached (TestDetachedSweepWithNestedRowGetsTheDetachedRefusal pins this), so the issue's "plain sweep is affected too" does not hold. Owner decision (A, on #1535): keep the default, honour --detach.

Dispatch (sweepDispatch, decided once)

flags path effective max-concurrent
(none) detached --max-concurrent if given, else --max-concurrent-auto's, else min(rows, 10) (unchanged)
--detach detached the same rule (was: foreground, all at once)
--detach --max-concurrent N detached N (unchanged)
--no-detach foreground N, or 0 = all at once (unchanged, TTL/idle-timeout still required on every row)
--detach --no-detach / --max-concurrent + -auto / negative refused —

A detached sweep is never recorded with max-concurrent 0. The orchestrator launches only while active < max_concurrent, so 0 would launch nothing.

The summary, --wait, the path-specific refusals (#1331/#1372 detached, #1375 foreground), --max-concurrent-auto and the dispatch all read the decision. TestSweepDispatchIsTheOnlyReaderOfTheDetachFlags type-checks package cmd and fails if anything other than a sweepDispatch(...) argument reads the detach/noDetach/maxConcurrent/maxConcurrentAuto globals.

Behaviour changes

  • --detach without --max-concurrent now goes to the orchestrator. The summary says so, and the same "Setting --max-concurrent=N" message prints.
  • The foreground summary prints Orchestration: foreground (--no-detach: …). It used to print no line at all, and it never mentions Lambda.
  • --wait is accepted on a default (no flag) sweep, which is detached. It used to be refused with "--wait requires --detach". It is still refused with --no-detach. --detach --wait used to run in the foreground and return without waiting. It now waits. The examples/workflows/* pass exactly that.
  • --max-concurrent < 0 is refused. It used to mean a foreground sweep, all at once.

Fail-closed SpawnSweepCrossAccountRole check

Before this change, nothing checked the role. sweep.ValidateParameterSets tries the assume, always fails (the role trusts only SpawnSweepOrchestratorRole) and only warns "validation skipped". A sweep in an account without the role created the CLI IAM role, the spored instance profile, the egress proxies, the S3 params and the DynamoDB record, then invoked the orchestrator and printed "queued successfully", and the orchestrator launched nothing.

Now spawn launch (detached path) and spawn resume --detach call iam:GetRole on the role before anything is created (pkg/sweep.CheckCrossAccountRole). spawn launch and spawn resume already call iam:GetRole, so the IAM inventory is unchanged. A NoSuchEntity refuses, names the account and points at --no-detach. Any other error (no permission to look, a throttle) warns and goes on, as before. Resume skips the check when the credentials' account isn't the account in the record.

Tests (hermetic)

  • TestSweepDispatchTable: the (detach, noDetach, maxConcurrent, auto, rows) table.
  • TestSweepPrintedModeIsTheDispatchedPath: through the real runLaunch with the recording AWS fake (withLogDir). The printed mode matches the path taken, and the foreground output contains no "Lambda".
  • TestDetachedSweepWithoutTheCrossAccountRoleCreatesNothing: default, --detach, and --detach --iam-policy. No AWS call beyond the identity lookups.
  • TestSweepRefusalsFollowTheDispatchedPath, TestSweepWaitFollowsTheDispatchedPath, TestCrossAccountRoleCheckRefusesOnlyAbsence, plus the source gate.

Revert evidence:

  • With cmd/launch_sweep.go from main, every new gate fails. --detach hits DescribeKeyPairs (the foreground path). The default sweep goes on to CreateRole without the role. --wait without --detach is refused. The gate lists 9+ direct reads.
  • Targeted mutations each turn a gate red:
    • sending explicit --detach to the foreground;
    • dropping the role check;
    • a direct detach && maxConcurrent > 0;
    • a constant "Lambda" summary line;
    • --wait keyed on the flag.

Checks

make check, the hermetic go test ./... (AWS env stripped), make test-e2e-tier0, semgrep scan --config=auto --error . (0 findings), make gen-docs (committed), make api-snapshot (no change) and make cli-iam-actions (no change) all pass. lambda/scheduler-handler/go.mod is tidied, because pkg/sweep now imports service/iam.

Live-test checklist (not run; no AWS access from this change)

Detached runs go in spore-host-dev only.

  • dev: spawn launch t --param-file 2rows.yaml --detach --ttl 30m → Setting --max-concurrent=2, Orchestration: Lambda (detached), orchestrator invoked once, spawn sweep status <id> found, 2 members launched by the orchestrator, then reaped/cancelled.
  • dev: same with no detach flag → identical behaviour to v0.160.0 (auto-detach message, min(rows,10)).
  • dev: --detach --wait --wait-timeout 20m waits and returns on COMPLETED.
  • either account: --no-detach --ttl 30m → Orchestration: foreground …, no "Lambda" in output, members launched by the CLI.
  • Missing-role refusal: covered hermetically. A live run needs an account without the role, and detached runs belong in spore-host-dev, which has it. Only run this if the owner names an account. Then verify nothing was created: no spawn-sweep-orchestration item, no S3 params object, no new IAM role or profile.
  • dev: spawn resume --sweep-id <detached id> --detach passes the role check and re-invokes.
  • Verify no instances leak after each run.

…p path once (#1535)

An explicit --detach with no --max-concurrent ran the sweep in the
foreground while printing 'Orchestration: Lambda (detached)'. The path is
now decided once by sweepDispatch and read by the summary, --wait, the
path-specific refusals and the dispatch. A detached sweep checks
SpawnSweepCrossAccountRole exists before creating anything.

Refs #1535
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.00000% with 30 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/launch_sweep_dispatch.go 82.35% 8 Missing and 1 partial ⚠️
pkg/sweep/crossaccount.go 0.00% 8 Missing ⚠️
cmd/resume.go 14.28% 6 Missing ⚠️
cmd/launch_sweep.go 82.75% 5 Missing ⚠️
pkg/sweep/detached.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn

Copy link
Copy Markdown
Contributor Author

Live test at 273aef33: PASS

spore-host-dev (435415984226), default VPC, 2 × t4g.small rows

Run Output Result
--detach (no --max-concurrent) Setting --max-concurrent=2, Orchestration: Lambda (detached), ✓ Lambda invoked COMPLETED 2/2 via the orchestrator. Before this PR it ran in the foreground.
no detach flag Auto-enabling --detach, Setting --max-concurrent=2, Lambda (detached) COMPLETED 2/2, unchanged from v0.160.0
--detach --wait --wait-timeout 20m Progress: 2/2 launched, Status: COMPLETED It waited, returned at COMPLETED, exit 0
--no-detach Orchestration: foreground (--no-detach: this CLI launches every member; keep it running), ✓ Launched … 1/2, 2/2 0 occurrences of "Lambda" in the output

Missing-role refusal, aws account (942542972736, which has no SpawnSweepCrossAccountRole)

With AWS_PROFILE=aws SPAWN_COMPUTE_PROFILE=aws, the default detached sweep exited 1: a detached sweep needs SpawnSweepCrossAccountRole in account 942542972736 … Nothing was created. Run the sweep with --no-detach …

I verified that nothing was created: no record in spawn-sweep-orchestration, 0 params objects in spawn-sweeps-us-east-1, no new IAM role, and no new instances. The check is one read-only iam:GetRole.

A pre-existing bug the first attempt exposed (filed as #1538, not caused by this PR): without SPAWN_COMPUTE_PROFILE, the compute profile defaults to the hardcoded spore-host-dev. So AWS_PROFILE=aws alone checked, and launched in, 435415984226. The orchestrator started 2 members there, which I terminated.

Not run live: spawn resume --detach. The hermetic tests cover it.

Leak check: every test sweep's instances (d1–d4, mr) are terminated, and none are still running.

@scttfrdmn
scttfrdmn merged commit 2e0de0f into main Nov 6, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1535-detach-dispatch branch November 6, 2026 04:00
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.

--detach without --max-concurrent silently runs the sweep in the foreground (prints 'Lambda (detached)')

1 participant