Skip to content

Optional fire-and-forget spot-interruption webhook (the named agent.go:1066 enhancement) #228

Closed

Description

@scttfrdmn

Repo: spore-host/spawn
Labels (suggested): enhancement, agent, spot
Relates to: the // Future enhancement: Support webhooks, email, SNS… note at pkg/agent/agent.go:1066 (spot-interruption handler)


Summary

Add an optional, launch-configured webhook that spored fires once,
best-effort,
from the existing checkSpotInterruption handler when an AWS spot
interruption notice arrives — so an external consumer can learn about the
reclamation inside the ~2-minute warning window, which the current
tag-and-poll surface structurally cannot deliver.

This is deliberately scoped to not change spawn's grain. spawn stays
fire-and-forget: the node still owns its own death, opens no connection it waits
on, and the webhook is a courtesy emit (a write to one more sink, next to the tag
and the local file) — not an outbound relationship spawn maintains.

Motivation

Today the spot-interruption fact is caught on-node within the window
(monitorSpotInterruptions, 5s IMDS poll) and surfaced as: a Slack/notifier ping,
the local file /tmp/spawn-spot-interruption.json (agent.go:1053, not visible
off-node), and — eventually — an EC2 State transition + tag change.

A programmatic, off-node consumer (e.g. a cohort-style reconciler driving
ephemeral jobs) therefore learns about a reclamation only after the instance is
gone, via polling ListInstances. By then the warning window is closed, so the
consumer cannot use it to do the things the window exists for:

  • checkpoint partial work before the node dies,
  • re-place the job on a fallback/patient rung (lagotto) proactively,
  • settle the spent budget as a fault with a reason, rather than inferring a
    bare terminated after the fact with the reason already lost on-node.

The information already exists, on-node, in time. The only gap is that it isn't
emitted anywhere a machine can consume it within the window. The note at
agent.go:1066 already anticipates exactly this.

Why this belongs in spored (not an external watcher)

The cheapest, most truthful place to surface this fact is where it's already
known — on the node, inside the window. An external watcher (e.g. subscribing to
AWS EventBridge's interruption notice) would rediscover a fact spored already
holds, seconds later, off-node, having lost nothing the node didn't already
have
— and it adds a whole new relationship-maintaining component to a system
whose primitives (spawn, lagotto) are uniformly fire-and-forget. A fire-once
webhook from spored is the least-invasive option on every axis: no new
component, no new relationship-shape, and it's the only path that preserves the
window.

The grain is preserved because the boundary that matters is "spawn must never
await"
, not "spawn must never emit." A best-effort POST that is allowed to
fail and be dropped — exactly as the node itself is allowed to vanish — does not
await. It's another expression of fire-and-forget, identical in spirit to writing
a tag.

Proposed shape

Three new optional LaunchConfig fields (alongside TTL/IdleTimeout/
CostLimit/OnComplete/PreStop/ClientToken):

Field Type Meaning
SpotInterruptionWebhookURL string (optional) If set, spored POSTs here once on interruption. Empty = disabled (today's behavior).
WebhookCorrelation string (optional, opaque) Caller-supplied blob. spored echoes it verbatim in the payload and never parses it. This is how a consumer correlates the event back to its own entity/grant/record without spawn knowing anything about the consumer.
WebhookTimeout duration (optional, small default e.g. 2s) Hard cap on the POST so it can never eat the reclamation window.

Payload: a fixed, stable on-node fact-struct + the opaque echo. spored emits
what it already holds; the caller takes the fields it cares about and ignores the
rest. Proposed body:

{
  "event": "spot_interruption",
  "instance_id": "i-0abc…",
  "region": "us-east-1",
  "az": "us-east-1a",
  "reason": "spot-interruption",
  "interruption_deadline": "2026-06-21T18:42:00Z",   // AWS-provided ~2-min window end
  "name_tag": "<spawn:name>",
  "client_token": "<LaunchConfig.ClientToken>",       // load-bearing for cohort/Telos correlation
  "compute_seconds": 1234,                             // already tracked (spawn:compute-seconds, agent.go:580)
  "lifecycle_phase": "running",
  "last_activity_time": "2026-06-21T18:39:50Z",
  "correlation": "<WebhookCorrelation, echoed verbatim>",
  "emitted_at": "2026-06-21T18:40:03Z"
}

Hard requirements (these are what keep it in-grain)

  1. Fire-once, best-effort, no retry, no await. A single POST with
    WebhookTimeout cap. Failure (timeout, non-2xx, DNS) is silently dropped —
    logged at most. A node mid-reclamation must not block on a POST any more than it
    blocks on a tag write. No delivery guarantee is offered or implied; the consumer
    must still have a poll-and-infer fallback (the EC2 state + tags remain the
    durable source of truth).
  2. Off the critical path. Fire from the existing spot handler, but it must not
    gate Cleanup/runPreStop/the 2-minute survival work (same discipline as CRITICAL: spored monitor loop never runs after startup — TTL/idle/on-complete/pre-stop all silently dead #65,
    which deliberately decoupled the spot monitor so a blocking IMDS call can't
    disable TTL enforcement). The webhook is the last thing, time-boxed.
  3. Payload is a projection of on-node knowledge only. spored includes only
    fields it already holds. A request for anything off-node is impossible by
    construction (there are no caller-supplied "please include X" fields except the
    opaque echo). This honors the "assuming spored has it" constraint — the contract
    cannot promise a field the node can't produce mid-reclamation.
  4. spored never parses WebhookCorrelation. Pure passthrough, like a user
    tag or ClientToken. spawn stays entirely consumer-agnostic.
  5. Opt-in. Empty URL = exactly today's behavior. No new always-on outbound
    traffic, no new IAM/SNS dependency, no change for existing users.

Explicitly out of scope (to protect the grain)

  • No SNS/EventBridge emission from spawn. That would make spawn maintain a
    managed-service relationship; the webhook is a dumb fire-once POST. (A consumer
    who wants SNS can point the webhook at their own endpoint that republishes —
    their concern, not spawn's.)
  • No delivery guarantees, no queue, no retry/backoff. Durability lives in the
    poll-and-infer fallback, not here.
  • No general lifecycle webhook (yet). This issue is scoped to spot
    interruption specifically, because that's the only lifecycle event where the
    window matters — idle-stop and TTL-terminate are benign, non-time-critical, and
    perfectly served by tag-and-poll. (If a general LifecycleWebhookURL is wanted
    later, it's a separate discussion; keeping this narrow keeps it in-grain.)

Open question for the maintainer

Fixed payload struct vs. selectable field list? This proposal assumes a
fixed on-node fact-struct (above) plus the opaque echo — simplest, most
in-grain, and a typical consumer wants essentially all of it anyway. A selectable
field list adds a validation surface for marginal benefit. Recommend fixed; flag
if you'd prefer selectable.

Consumer note (non-binding, for context)

The motivating consumer is a cohort-style reconciler (the same shape as
pkg/mpicohort): on receiving the POST it correlates by client_token →
its entity → its budget record, settles the spent compute (compute_seconds) as a
fault (not a completion) with reason, and re-places the job on a patient rung
(lagotto) within the window. The poll-and-infer path (EC2 state + spawn:* tags)
remains the always-correct baseline; this webhook is the in-window upgrade over
it. No change to spawn's responsibilities is implied beyond the fire-once emit.

Activity

  1. added
    enhancement
    area:spored
    on Jun 21, 2026
  2. scttfrdmn commented on Jun 21, 2026

    @scttfrdmn
    ContributorAuthor

    Read through this and grounded it against the code — the proposal is sound, in-grain, and the gap is real. One correctness bug in the payload to fix before implementing, plus a couple of small notes.

    Confirmed against the code

    • The cited note is real: agent.go:1066 is exactly the // Future enhancement: Support webhooks, email, SNS, etc. line, inside sendSpotInterruptionNotification, called from checkSpotInterruption (:1039).
    • The gap is real: on-node the fact is caught in-window but only surfaced as a Slack ping, the local /tmp file, and (eventually) an EC2 state/tag change. No machine-consumable, in-window, off-node emit exists.
    • The grain argument holds ("never await" ≠ "never emit"), and there's a direct precedent: notifier.go already does a best-effort outbound POST with a 5s timeout (:147/:153). This webhook is the same shape pointed at a caller-supplied URL with a machine payload — so it's not redundant with the existing notifier.
    • Most payload fields are genuinely on-node: interruption_deadline ← InterruptionInfo.Time (provider.go:103), compute_seconds ← spawn:compute-seconds (written by spored), instance_id/region/az/name_tag/lifecycle_phase/last_activity_time from identity + config + agent state.

    The one real problem: client_token is not on-node

    The payload lists client_token as "load-bearing for cohort/Telos correlation" — but spored can't see it. provider.Config is populated entirely from EC2 tags (+ IMDS) at startup and periodic refresh. ClientToken is the RunInstances idempotency parameter (aws/client.go:124); it is never written as a tag and not exposed via IMDS. So the single field the issue calls load-bearing is the one field the node can't produce — which violates the issue's own hard-requirement #3 ("the contract cannot promise a field the node can't produce mid-reclamation").

    Recommended fix — cleaner and more in-grain: drop client_token from the fixed payload and let WebhookCorrelation (the opaque echo) carry correlation. It's launch-configured, reaches spored through the exact same tag mechanism every other config field uses, is never parsed, and is purpose-built for this. The consumer puts its own client-token / entity / grant ID into the blob.

    The alternative — start tagging ClientToken at launch so spored picks it up — adds an always-written, mildly-sensitive tag for something WebhookCorrelation already covers. Not worth it. Flag if you'd rather go that way, but my recommendation is to remove client_token and rely on the echo.

    Smaller notes

    • Config delivery: the three new fields (SpotInterruptionWebhookURL, WebhookCorrelation, WebhookTimeout) should flow as spawn:* tags → provider.Config, consistent with every other field. The opt-in / passthrough requirements fit that mechanism exactly.
    • reason: the proposal hardcodes "spot-interruption", but InterruptionInfo.Action is "terminate" or "stop" (provider.go:102). Worth echoing action into the payload too, so the consumer knows whether it's a terminate or a stop.
    • Placement: fire it as the last step in checkSpotInterruption, after the existing file notification, WebhookTimeout-boxed, never gating Cleanup/runPreStop (the CRITICAL: spored monitor loop never runs after startup — TTL/idle/on-complete/pre-stop all silently dead #65 discipline). The existing Slack Notify already runs post-Cleanup, so there's a clean precedent slot.

    Open question (fixed vs selectable payload)

    Agree with your own lean — fixed struct + opaque echo. The echo handles the "I need my own field" case; a selectable list is validation surface for no real gain.


    So: net +1 to implement, with one change to the spec — remove client_token; correlation rides on WebhookCorrelation. Confirm that (and the action echo) and I'll pick it up.

  3. scttfrdmn commented on Jun 21, 2026

    @scttfrdmn
    ContributorAuthor

    Confirming the review independently against the source (cloned HEAD) — both findings hold, and I agree with the spec change.

    client_token is genuinely not on-node — confirmed

    • ClientToken is consumed only at the RunInstances call: pkg/aws/client.go:360-361 sets input.ClientToken = aws.String(launchConfig.ClientToken). That's its only use.
    • It is never written as a spawn:* tag (grep for a client-token tag returns nothing), and provider.Config is populated "from EC2 tags at startup" + IMDS (pkg/provider/provider.go:63,111,114; ec2.go IMDS path). Neither carries the token.
    • So spored cannot put client_token in the payload — it would violate the issue's own hard-requirement Raise test coverage on low-coverage packages (aggregate 24.7%) #3. Agreed: drop client_token; correlation rides on WebhookCorrelation. That field reaches spored through the same tag→provider.Config mechanism as everything else, is never parsed, and is exactly the right home. Not worth tagging ClientToken just to recover it.

    reason → echo action — confirmed

    The IMDS spot/instance-action document carries Action ∈ {terminate, stop} (pkg/provider/ec2.go:291 path, :314 struct, :328 Action: action.Action). The hardcoded "spot-interruption" loses that. Agreed: echo the AWS action verbatim (and the consumer can treat the event type as spot_interruption while reading action for terminate-vs-stop — they're different axes).

    One consumer-side consequence worth recording

    For the motivating cohort/Telos consumer, correlation-via-WebhookCorrelation (not client_token) means the consumer must put its own key into the blob at launch — which is fine, because the consumer is the one that minted the cohort EntityIntent.IdempotencyToken in the first place (cohort ports.go: the token is deterministic in cluster/entity/generation, supplied BY the consumer). So the consumer already holds the value to echo; it just passes it as WebhookCorrelation instead of relying on spawn to reflect ClientToken back. Net: the correlation story is actually cleaner this way — spawn stays fully consumer-agnostic, and the consumer owns its own correlation key end to end. No spawn-side change needed beyond the two edits above.

    Net

    +1 to the revised spec: fixed payload struct minus client_token, plus action, correlation via the opaque WebhookCorrelation echo. Everything else in the proposal (fire-once/best-effort/no-await, WebhookTimeout-boxed, fired last after the existing file/Slack notify per the #65 discipline, opt-in) is confirmed in-grain against the code. Good to implement as revised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:sporedenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions