Skip to content

feat(task): real 'spawn task run' — launch + S3 staging + durable completion record (spawn#386) - #404

Merged
scttfrdmn merged 4 commits into
mainfrom
feat/386-task-run
Jul 19, 2026
Merged

scttfrdmn merged 4 commits into
mainfrom
feat/386-task-run

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Summary

First of two stacked PRs for spawn#386 increment 2. Makes spawn task run actually launch (previously --dry-run only).

task run --spec <file>:

  1. sizes the cheapest fitting instance (reuses the increment-1 truffle sizer),
  2. ensures spawn-results-<account>-<region> exists,
  3. creates a scoped IAM instance profile granting exactly the input/output/results buckets (no wildcard),
  4. launches via launcher.Provision (keyless/SSM, auto AMI+IAM+user-data) running a generated bash wrapper,
  5. returns immediately with the task id + poll location.

The wrapper (pkg/taskproto/wrapper.go) is the reference impl

Assigned to LaunchConfig.JobArrayCommand, embedded by Provision — zero changes to bootstrap.go or spored. It: stages inputs (aws s3 cp) → runs the argv command capturing rc=$? → stages outputs (even on failure) → writes the durable record to s3://.../tasks/<task_id>/ (completion.json, .exitcode, best-effort command.log) → signals /tmp/SPAWN_COMPLETE so on_complete: terminate fires → exit $rc.

Design points (commented in the generated script):

  • No set -e — it would abort before stage-out/record on a failing command. rc=$? is captured explicitly.
  • All argv/env/paths single-quoted (shQuote, the safe single-quote form — not strconv.Quote). Env keys validated as shell identifiers in Validate().

New taskproto types

LaunchResult, CompletionRecord (JSON shape authoritative for cross-lang adapters; timestamps kept as strings), RetryClass + ClassifyLaunchError. Wired: app_error, staging_error, capacity. Defined-but-stubbed: spot_interruption, instance_health, ttl_expired, controller_lost.

pkg/aws change

IAMRoleConfig.InlinePolicyJSON — attach a scoped inline policy from a string (mirrors the existing PolicyFile path), folded into hashPolicies so different bucket sets get different cached roles.

Deferred

  • Container (spec.container) — rejected clearly; host command path ships fully.
  • spawn task status + --wait poller — PR B (stacked on this).

Testing

  • pkg/taskproto: wrapper structure/quoting/ordering golden-ish assertions (incl. no-set -e, metachar args), CompletionRecord round-trip + parse-the-wrapper-shape, Retryable()/ClassifyLaunchError tables, env-key validation.
  • cmd: taskLaunchConfig mapping, s3Bucket(s), taskStagingPolicy (scoped, valid JSON, no wildcard, input≠write).
  • pkg/aws: InlinePolicyJSON role-name determinism.
  • TestFlagConventions/TestTier0 pass; docs-gen regenerated; Semgrep clean on new files. No billable EC2 — all unit-level. A real end-to-end launch is gated for a later approved run.

Refs #386

…pletion record (spawn#386)

Increment 2 of the task-execution protocol: task run --spec now launches (not
just --dry-run). It sizes the cheapest fitting instance (reusing the existing
truffle sizer), ensures the per-account results bucket, and launches an
ephemeral instance via launcher.Provision running a generated bash wrapper that
stages inputs, runs the command, stages outputs, and writes a durable
completion record to s3://spawn-results-<acct>-<region>/tasks/<task_id>/ —
completion.json (authoritative), .exitcode (legacy signal), command.log.

The wrapper (pkg/taskproto/wrapper.go) IS the reference impl — no bootstrap or
spored change. It deliberately avoids 'set -e' (which would abort before the
completion record) and single-quotes all argv/env/paths. New taskproto types:
LaunchResult, CompletionRecord, RetryClass + ClassifyLaunchError (app_error,
staging_error, capacity wired; spot/health/ttl/controller-lost stubbed).

A scoped IAM instance profile grants exactly the input/output/results buckets
the task touches (no wildcard), via a new IAMRoleConfig.InlinePolicyJSON field.
Spot falls back to on-demand once on a capacity error when configured. Container
execution is deferred (rejected clearly).

Refs #386
@codecov

codecov Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 64.03509% with 123 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/task.go 44.07% 85 Missing ⚠️
pkg/aws/iam.go 12.50% 33 Missing and 2 partials ⚠️
pkg/taskproto/completion.go 83.33% 2 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

…into interface{}

Semgrep's go-unsafe-deserialization-interface rule flags json.Unmarshal into an
interface{}. The intent here is only to assert the string-built policy is valid
JSON, so json.Valid is both correct and rule-clean.
SearchInstanceTypes does not return on-demand pricing (that's a separate call),
so every candidate had OnDemandPrice=0. The sizer ranks on price, so 'cheapest'
collapsed to the alphabetical name tie-break — which picked the LARGEST matching
type (a 2-vCPU/2-GiB request sized to c7i.12xlarge, 48 vCPU). Caught by the
increment-2 smoke test.

truffleFinder.FindCandidates now looks up each candidate's on-demand price via
tc.OnDemandPrice (AWS pricing API with a static libs/pricing fallback). A lookup
miss leaves price 0, which the sizer sorts last, so a priced option always wins
and we never silently pick a huge box because pricing was briefly unavailable.

Refs #386
CreateOrGetInstanceProfile built a trust policy with an empty Service principal
when the caller passed no TrustServices, which IAM rejects with
MalformedPolicyDocument ('statement with no principals'). An instance profile is
always assumed by the EC2 service, so default to ec2 when unset. This is a latent
bug on any headless CreateOrGetInstanceProfile caller (lagotto included); caught
by the increment-2 smoke test when 'spawn task run' created a scoped profile.

Also set TrustServices explicitly at the task-run call site for clarity.

Refs #386
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