Skip to content

Run the spored stage runner in pipeline stages, so data_input/data_output work (#910, #911) - #942

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/910
Oct 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/910

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #910
Fixes #911

Stacked on #931 (fix/912). Review and merge that first; this PR's base is fix/912.

Cost safety. Found and tested by reading the code and running it locally against the in-process Substrate emulator. No AWS resources were created, no instances launched, and nothing was deployed.

What was wrong

What changed

User data. It now installs spored (unchanged) and runs timeout -k 60 <stage timeout> spored run-pipeline-stage in /opt/spawn/pipeline/<id>/<stage>. All of #931's guarantees stay as they were: the EXIT trap installed first, FAILED unless complete, API terminate with the IMDSv2 id plus halt, and TTL tags. The command and env no longer appear in the user data at all. The runner reads them from the definition and runs the command with exec.

One marker scheme. pkg/pipeline now has StageKeyPrefix, StageMarkerKey, StageOutputPrefix and StageConfigKey. Three things use them:

  • the runner, which writes COMPLETE only after the outputs are uploaded, and now also writes FAILED itself on any error;
  • the orchestrator's markerExists;
  • the user data's fallback FAILED marker, which is rendered in Go rather than rebuilt in bash.

The keys are unchanged (<prefix>/stages/<id>/COMPLETE|FAILED).

Orchestrator.

  • Tags spawn:s3-bucket, spawn:s3-prefix and spawn:s3-config-key.
  • launchStage writes the definition to s3://<s3_bucket>/<s3_prefix>/config/<pipeline>.json before launching. It does this on every stage launch, so pipelines already in flight also get it.
  • Launches with the pipeline's instance profile.
  • RunInstances and the AMI lookup now go through seams, for tests.

IAM.

  • spawn pipeline launch creates the stages' profile, or reuses it (it is named by policy hash), using the caller's credentials as spawn task run does. The profile has CreateOrGetInstanceProfile's spored baseline and SSM, plus pipeline.StageDataPolicy: s3:GetObject/PutObject on <bucket>/<prefix>/* and s3:ListBucket on the bucket, conditioned on s3:prefix = <prefix>/*. Nothing outside the prefix is granted.
  • The CLI records the profile name in PipelineState.InstanceProfile. Records without it fall back to the default.
  • template.yaml lets the Lambda iam:PassRole spawn-instance-* to ec2.amazonaws.com. The Lambda still creates no IAM.

Region.

  • The runner passes its IMDS-region config to the data handler, the definition download and peer discovery.
  • Its S3 clients use the bucket's own region (manager.GetBucketRegion, an unsigned HeadBucket). A stage in another region therefore still reaches the pipeline bucket.
  • The user data also exports AWS_REGION.

Dependency on #915

#915 (fix/906) gives pkg/pipeline's AWS clients the IMDS region. This PR does not need it to work: every client on the runner's path now gets the region-bearing config, and the user data exports AWS_REGION. I did not cherry-pick it. The two PRs touch the same LoadDefaultConfig lines in stage_runner.go, so whichever merges second has a small, mechanical conflict. This PR adds no new LoadDefaultConfig call in pkg/pipeline, so #915's AST gate stays satisfied. Merge #915 too: IsPipelineInstance and the unused NewStageDataHandler/DownloadResultsToLocal still use a bare config.

Tests

  • End to end (lambda/pipeline-orchestrator/stage_runner_e2e_test.go). The test builds the real spored from cmd/spored. The emulator instance is launched with the orchestrator's own buildStageRunInstancesInput, so it carries exactly the orchestrator's tags. The test then runs the rendered user data under bash, with faked curl/aws/shutdown and fake IMDS, against Substrate:
    • Success: the upstream output is downloaded into dest_path; the command's output (input upper-cased, then the stage env value) is in S3 under the stage's output prefix; the orchestrator's markerExists sees COMPLETE and no FAILED; the instance self-terminates.
    • A failing command: spored writes FAILED, with the exit status in the body, and the orchestrator sees it. No COMPLETE is written, nothing is uploaded, and the instance self-terminates.
  • Orchestrator (TestLaunchStageGivesTheRunnerItsTagsConfigAndProfile). The config is written before any launch, at exactly the key every instance's s3-config-key names. It is the definition, it lives inside the granted prefix, the bucket and prefix tags are set, and the profile is the pipeline's. Also: the fallback profile, and the user data's fallback marker key and runner invocation.
  • Policy, region and CLI. StageDataPolicy covers config, upstream outputs, own outputs and both markers (including an empty prefix) and does not reach another prefix. bucketRegionConfig follows x-amz-bucket-region and falls back. The CLI's role config carries the policy.
  • Terminate failed pipeline stages and make them reapable (#912) #931's three bash tests now use a fake spored and still check FAILED, terminate and halt.

Every gate was watched failing against a compiling revert (15 reverts, each go vet-clean). Reverted: spored not run; s3-config-key tag dropped; s3-bucket tag dropped; config not written; config tag pointing at the CLI's copy; default profile; runner not writing FAILED; COMPLETE at another key; outputs not uploaded; ListBucket dropped; PutObject dropped; grant widened to the bucket; bucket region ignored; CLI policy empty; fallback marker at another key.

Commands run. make check passes except the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker. make check-nested passes (all 9 modules). go test ./lambda/pipeline-orchestrator/ passes, and the e2e tests are stable at -count=3. golangci-lint shows nothing new in the touched packages; the only findings are the pre-existing manager.Uploader deprecations. GOOS=linux GOARCH=arm64 builds the Lambda.

Deployment (not done here)

  1. Merge Terminate failed pipeline stages and make them reapable (#912) #931, then this.
  2. Redeploy the orchestrator, because the template's PassRole changed:
    cd lambda/pipeline-orchestrator
    AWS_PROFILE=spore-host-infra make deploy
    AWS_PROFILE=spore-host-infra make -C ../.. lambda-versions-deployed
  3. Release the CLI. Pipelines must be launched with it to get the scoped profile, and the launcher now needs IAM create permissions, as spawn task run does.
  4. spored: stage instances download the released spored from spawn-binaries-*. Until a spored containing this PR's pkg/pipeline changes is published, they run the old runner. With the old runner, the command runs in /root, the runner does not write FAILED (the user data's fallback still does), and the data handler has no region. In practice: release spored together with the Lambda deploy.

Caveats and follow-ups

  • Not verified on AWS. In particular, not verified that GetBucketRegion works from instances without internet egress. On failure it falls back to the instance's region.
  • A pipeline launched with the old CLI and still in flight uses the default profile. Its stages will fail on the first S3 access, cleanly: FAILED, then terminate.
  • Data handoff semantics are as pkg/pipeline already had them, and are out of scope here. An absolute data_output path keeps its full path under output/, so /data/processed/x arrives downstream as <dest_path>/data/processed/x. With instance_count > 1, instances overwrite each other's same-named outputs. pipeline: with instance_count > 1 the first instance to finish marks the whole stage complete, and the budget uses a flat $0.10/hour #930 (next PR) covers the per-instance markers.

@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.27219% with 57 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/pipeline/stage_runner.go 46.66% 23 Missing and 1 partial ⚠️
lambda/pipeline-orchestrator/main.go 68.75% 18 Missing and 2 partials ⚠️
cmd/pipeline.go 38.46% 8 Missing ⚠️
pkg/pipeline/data_handoff.go 80.00% 3 Missing ⚠️
pkg/pipeline/stage_keys.go 93.75% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

…tput work (#910, #911)

A pipeline stage's data_input and data_output were validated and then
ignored: the stage user data had TODOs where the S3 download and upload
belonged, so stages started with no inputs and lost their outputs, and
the pipeline could still report COMPLETED. The code that implements the
handoff, `spored run-pipeline-stage` (pkg/pipeline's stage runner, data
handoff and peer discovery), had no caller.

The stage user data now runs `spored run-pipeline-stage` under the stage
timeout, inside #912's EXIT trap, so termination, TTL tags and FAILED
handling are unchanged. What the runner needed and lacked:

- Tags: the orchestrator now writes spawn:s3-bucket, spawn:s3-prefix and
  spawn:s3-config-key; without the last the runner refused to start.
- The definition: launchStage writes it to <prefix>/config/<id>.json in
  the pipeline's own bucket (pipeline.StageConfigKey), on every stage
  launch, so in-flight pipelines get it too.
- S3 access: the shared spored profile grants nothing on pipeline data,
  so stages could not read inputs or write outputs or even their
  markers. `spawn pipeline launch` now creates (or reuses, by policy
  hash) a profile scoped to s3_bucket/s3_prefix (StageDataPolicy) and
  records it in the state; the orchestrator launches with it, falling
  back to the default for old records. The template lets the Lambda
  pass spawn-instance-* roles to EC2.
- One marker scheme: StageKeyPrefix/StageMarkerKey/StageOutputPrefix in
  pkg/pipeline are used by the runner, the orchestrator's reader and the
  user data's fallback FAILED marker (rendered in Go). The runner now
  writes FAILED itself on any error, and COMPLETE only after outputs.
- Region: the runner hands its IMDS-region config to the data handler,
  definition download and peer discovery, and its S3 clients use the
  bucket's own region (GetBucketRegion), so a stage in another region
  reaches the pipeline bucket. The user data also exports AWS_REGION.
- The command runs in the user data's work dir, not a fixed /root.

Tests: the rendered user data runs under bash with faked curl/aws/
shutdown and the REAL spored built from cmd/spored, against the
in-process Substrate emulator and a fake IMDS. The stage instance is
launched in the emulator from the orchestrator's own RunInstances
input, so its tags are the orchestrator's. It shows the upstream output
downloaded, the command's output uploaded under the stage's output
prefix, COMPLETE seen by the orchestrator's markerExists, and, for a
failing command, FAILED written by spored and nothing uploaded. Unit
tests cover launchStage's tags, config write ordering and profile, the
policy's coverage and scope, bucket-region resolution and the CLI's
role config. Each was watched failing against a compiling revert.
@scttfrdmn
scttfrdmn merged commit d7fbdf3 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/910 branch October 17, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant