agentkickoff

package
v0.152.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation ¶

Overview ¶

Package agentkickoff DELIVERS an agent's first turn: it hands the note a dispatch was created with (agents.Agent.PendingNote) to the agent's model gateway once the agent's instance is ready, and records what happened on the row.

🔴 IT IS A BACKGROUND RECONCILE LOOP, NOT A STEP IN THE DISPATCH REQUEST, AND THE CODE THAT ALREADY EXISTED DECIDED THAT. agents.DecideReconcile is a per-tick decision table over (row, live instance, now): "a ready instance whose kickoff was never sent → retry while young, error once it has dwelt past ProvisioningStuckTimeout". That is the shape of a loop, and the alternatives are worse on their own terms:

  • Dispatch runs inside a handler goroutine with a 5-minute operation budget (agentprovision.DefaultOpTimeout) and returns when the Deployment is APPLIED, not when the pod is ready. Delivering there means either blocking that goroutine on image pull + repository clone + runtime boot, or giving up on a slow-but-healthy pod. A loop simply looks again next tick.
  • A first turn that fails BEFORE it is sent (no instance yet, no token) is retried by the loop for free, and the table's dwell bound is what eventually turns "never delivered" into a visible `error` — a mechanism a request-scoped call has no equivalent of.
  • Start of a saved agent owes the same first turn (agentprovision.Adapter.Start says so) and has no request that waits for readiness either. One deliverer serves both doors.

🔴 SINGLE-FLIGHT IS THE PER-AGENT CLAIM, NOT A LEADER LEASE. Two replicas ticking against one database both see `ready && !KickedOff`; each must win agents.Store.ClaimKickoff (a conditional UPDATE — a compare-and-set on the row) before doing anything, and the winner RE-READS the row under the claim, so a replica that lost the race or arrived after a delivery does nothing. No lease is needed and none exists in this module.

🔴 KickedOff IS STAMPED *BEFORE* THE MODEL TURN, SO A PAID TURN IS NEVER RUN TWICE. Once the stamp lands, agents.DecideReconcile stops returning ActionRetryKickoff for the row and this package never touches it again — even if the turn then fails, times out, or this process dies mid-turn. The cost is the mirror image and it is accepted: a turn that fails AFTER the stamp is recorded in agents.kickoff_error and is NOT retried automatically. Everything muster can check without opening a connection to the agent runtime — the token, the claim, a chat session, and the agent's address and credential (agentgateway.Gateway.Resolve, which for the k8s driver reads the Deployment from the Kubernetes API) — is checked before the stamp, and a failure there is retried.

⚠ ONE FAILURE THAT COSTS NOTHING STILL LANDS AFTER THE STAMP: a connection to the runtime that cannot be OPENED (DNS, refused). It happens inside the send, so it is recorded, as agents.KickoffNeverConnectedReason, and not retried. Retrying it would mean un-stamping a row that RecordKickoffDelivery has already counted as an attempt — a second write after the point of no return, and the kicked_off=f with attempts>0 state that round 0 deleted a guard for because no writer produced it. What the record buys instead is a remedy that says re-sending is safe (agents.KickoffResendSafe), which is true for this cause and for the next one.

⚠ AND ONE IS RE-SENT IN PLACE: a typed `not_ready` (ccd, before the Claude Code CLI is at its prompt — answered before anything is pasted). deliver re-sends it within the same delivery, under the same stamp, for a bounded wait; one that outlasts the bound is recorded as agents.KickoffNotAcceptedReason, also resend-safe. Nothing is un-stamped.

🔴 A FIRST TURN THAT NEEDS A REPOSITORY CHECKOUT IS HELD UNTIL IT EXISTS. For a claude-code agent dispatched with a repository, the last pre-stamp check reads ccd's /healthz `repo.state` (agentgateway.Gateway.Checkout); while the checkout is cloning or waiting for the owner's github grant, nothing is sent or stamped and the row records why (agents.DecideFirstTurn, bounded by agents.CheckoutWaitTimeout). See [Deliverer.deliver].

🔴 AND THAT UNRETRIED FAILURE IS VISIBLE, NOT JUST RECORDED (an operator decision). The stamp clears the "kickoff owed" badge, so before this a failed turn left a card reading healthy with the failure only in a column no page showed. agents.KickoffFailed (kicked_off AND kickoff_error set) now drives a "kickoff failed" badge carrying the error text and a remedy, and `kickoffFailed` on GET /api/agents. The remedy says re-sending by hand is safe only when nothing was sent (agents.KickoffResendSafe); for every other cause it says to check whether the agent is already working first, because the runtime may still be running the turn. A turn cut off by THIS process shutting down is recorded as such (ShutdownCancelledReason), and the server's shutdown waits for that record (Deliverer.Done) before it closes the pool. Each of these changes calls Config.OnChange, so an open agent page's strip and the sidebar re-fetch.

🔴 IT ACTS ONLY ON ROWS THIS DEPLOYMENT OWNS, AND NEVER ON A KICKED-OFF ROW. The agents table can hold rows another system wrote (a namespace the configured prefix did not produce); escalating those to `error` would be this process asserting a verdict about pods it cannot see. And a kicked-off row's recovery (agents.ActionResendKickoff / ActionErrorKickoffLost) is a re-delivery decision this package deliberately does not take — see [owesFirstTurn].

🔴 AN EMPTY REPLY IS A FAILED DELIVERY. The agent runtime's /v1/responses is known to discard its own successful retry for reasoning models and answer 200 with no text; recording that as delivered would report an agent that was told its task when nothing says it was. It lands in agents.kickoff_error.

Index ¶

Constants ¶

View Source
const DefaultInterval = 10 * time.Second

DefaultInterval is how often Deliverer.Run looks for an owed first turn. A dispatched pod takes tens of seconds to become ready, so this adds at most that much latency to a first turn while costing two list reads per tick.

View Source
const DefaultTurnTimeout = 15 * time.Minute

DefaultTurnTimeout bounds one delivered first turn, end to end. The gateway's own HTTP client bounds each REQUEST; this bounds the turn this package owns.

View Source
const EmptyReplyReason = agents.KickoffEmptyReplyReason

EmptyReplyReason is written to agents.kickoff_error when the first turn came back with no text. It is agents.KickoffEmptyReplyReason, which the agent page's remedy reads.

View Source
const ShutdownCancelledReason = "kickoff turn CANCELLED because muster was shutting down " +
	"(a redeploy or pod stop) while the turn was in flight. It had already been handed to " +
	"the gateway, so it is not retried automatically (a first turn is never paid twice)"

ShutdownCancelledReason opens agents.kickoff_error when a stamped first turn was cut off because THIS PROCESS was shutting down (SIGTERM — a redeploy, a node drain, a scale-down).

🔴 IT IS NAMED, RATHER THAN LEFT AS A BARE "context canceled", BECAUSE THE OPERATOR'S NEXT MOVE DEPENDS ON IT. The turn was handed to the gateway and the row was stamped, so it is not re-run (never paid twice) — but nothing about the agent or the task was wrong. "context canceled" reads as a fault in the agent; this reads as what it is: muster went away mid-turn. The runtime may have kept running the turn after muster stopped waiting, so the remedy is to check whether the agent is already working before re-sending (agents.KickoffResendSafe answers false).

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Config ¶

type Config struct {
	// Store is the agents store. REQUIRED.
	Store agents.Store
	// Instances lists live instances. REQUIRED.
	Instances InstanceLister
	// Gateway runs the turn. REQUIRED.
	Gateway Gateway
	// NamespacePrefix is the deployment's CONFIGURED agent namespace prefix — the
	// same value the row-writing path and the driver use. A row is this
	// deployment's iff its stored namespace is agents.NamespaceFor(prefix, name).
	NamespacePrefix string
	// Owner names this process in a kickoff claim. Defaults to hostname/random.
	Owner string
	// Logger is optional.
	Logger *log.Logger
	// Now is the clock DecideReconcile is evaluated against. Defaults to time.Now.
	Now func() time.Time
	// TurnTimeout defaults to [DefaultTurnTimeout].
	TurnTimeout time.Duration
	// OnChange, when set, is called with an agent's name after this package changes
	// what that agent's page shows: the stamp (the "kickoff owed" badge goes), a
	// recorded post-stamp failure (the "kickoff failed" badge comes), and a stuck
	// verdict (the status goes red). cmd/muster-server passes
	// api.Server.BroadcastAgentChanged, which the agent page's strip (ui's
	// agentStripTrigger), the sidebar and the Tasks board re-fetch on. (The retired
	// Agents list heard it only once its hx-trigger gained `sse:agent.changed`;
	// before that only the Tasks board did.)
	//
	// 🔴 A PRE-SEND FAILURE DOES NOT CALL IT. It changes nothing a page shows (the row
	// still reads "kickoff owed"), and it recurs every tick while the pod boots, so
	// calling it there would re-fetch every open sidebar every 10 s.
	OnChange func(name string)
}

Config is everything the deliverer needs.

type Deliverer ¶

type Deliverer struct {
	// contains filtered or unexported fields
}

Deliverer delivers owed first turns. Build it with New.

func New ¶

func New(cfg Config) (*Deliverer, error)

New validates the configuration and builds the deliverer.

func (*Deliverer) Done ¶

func (d *Deliverer) Done() <-chan struct{}

Done is closed once Deliverer.Run has returned — after its ctx was cancelled AND every turn it started has recorded its outcome.

🔴 THE PROCESS'S SHUTDOWN WAITS ON THIS, AND WITHOUT THAT WAIT A TURN CANCELLED BY A REDEPLOY WAS LOST SILENTLY. Run's ctx is the process's signal context, so SIGTERM cancels an in-flight turn AFTER the row was stamped kicked_off. The failure is then written on a DETACHED budget (bookkeeping), but main used to return straight into pool.Close and exit without waiting for Run's goroutine: the stamp had landed, the record had not, and the row read as a delivered kickoff with no error. cmd/muster-server's shutdown waits on this channel, bounded, before the pool closes.

It is a channel rather than a second wg.Wait because a WaitGroup may not be waited on while Tick can still Add to it from Run's goroutine; Run alone both Adds and Waits, so only Run may say it is finished.

func (*Deliverer) Run ¶

func (d *Deliverer) Run(ctx context.Context, interval time.Duration)

Run ticks every interval until ctx is cancelled, then waits for in-flight turns and closes Deliverer.Done.

func (*Deliverer) Tick ¶

func (d *Deliverer) Tick(ctx context.Context) error

Tick makes one pass: it decides, for every owed row, what agents.DecideReconcile says, acts on it, and starts each delivery in the background (see Deliverer.Wait).

🔴 A FAILED INSTANCE LIST ENDS THE TICK WITH NO ACTION. An unobserved backend is not an empty one: deciding against a nil instance would read every young row as absent and every old one as stuck, and error rows on no information.

func (*Deliverer) Wait ¶

func (d *Deliverer) Wait()

Wait blocks until every delivery started by Deliverer.Tick has finished.

type Gateway ¶

type Gateway interface {
	Resolve(ctx context.Context, a agents.Agent) (agentgateway.Target, error)
	Send(ctx context.Context, t agentgateway.Target, sessionKey, message string, emit func(string)) (string, error)
	Checkout(ctx context.Context, t agentgateway.Target) (agents.CheckoutStatus, error)
}

Gateway is a first turn's two halves: Resolve (no connection to the runtime) and Send (the turn). Together they are agentgateway.Gateway.Chat, the method POST /api/agents/{name}/messages calls, so the kickoff travels the same path an operator's message does. agentgateway.Gateway satisfies it.

🔴 THEY ARE SEPARATE SO RESOLVE CAN RUN BEFORE THE STAMP. See [Deliverer.deliver].

Checkout reads a claude-code agent's repository checkout (ccd's /healthz) — the last pre-stamp check for an agent dispatched with a repository; see [Deliverer.deliver] and agents.DecideFirstTurn.

type InstanceLister ¶

type InstanceLister interface {
	Instances(ctx context.Context) ([]provision.Instance, error)
}

InstanceLister is what the deliverer needs from a provisioner: the live instances, so it can tell a ready recipient from one that is not. agentprovision.Adapter.Instances satisfies it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL