Documentation
¶
Overview ¶
Package agentoutcome decides how a started agent's work ENDED: done, failed or stuck — never silently "running" (docs/provisioner.md, "How an agent's work ends").
🔴 THE DEFECT IT CLOSES, MEASURED. The chief dispatched a gateway agent with a model DISPLAY NAME where an id belonged. The runtime's first turn failed ("Unknown model"), and so did every later turn. muster kept the agent `running` with an empty error_message and last_output and one chat row (the kickoff it sent); nothing pushed and nothing told the chief. The owner found it hours later by looking. Four things were missing, and each has one home:
- a model nobody can run is refused before a pod exists (internal/api model_validate.go);
- a failed turn sets status `error` with its reason, pushes once and tells the chief that started it (internal/api agent_outcome.go, fed by internal/agentkickoff for the first turn and by chatTurn for the rest);
- an agent that goes quiet is called stuck by a sweep (Decide, run by internal/api agent_watchdog.go);
- an agent that finishes says so (Done) — only on an explicit signal — and one that stops without saying so is Waiting on its owner.
THE STATES (agents.outcome, migration 0088). "" is UNTRACKED — an agent nobody started with a first turn, and every row from before 0088.
running started with a first turn; working (or about to)
waiting its turn ended, idle, with no completion signal (a question, a
progress report): the owner's move — pushed once, with its last line
done it SAID it finished: moved its task to review/complete, or called
agent_report_done. 🔴 NEVER inferred from an idle reply: a question
read as "done" is the lost-agent failure this package exists to end
failed a turn failed; the reason is in outcome_reason and error_message
stuck no reply since its kickoff, or silent past the threshold
🔴 ONE PUSH PER CHANGE, BY CONSTRUCTION RATHER THAN BY A LEDGER. Every transition is a write conditional on the outcome it leaves (Store.Transition's `from`), so of two writers racing one change — two replicas, a turn ending while the sweep runs — exactly one applies it, and only the one whose write applied pushes. A repeated failure of a `failed` agent applies nothing and pushes nothing.
🔴 IT IS NOT A SIXTH agents.status. The five statuses key the reconcile table and every consumer; `stuck` is an outcome BESIDE a status, the shape agents.KickoffOwed already has, because a stuck agent's pod is still running and its recovery is new activity, not a restart.
Index ¶
- Constants
- func FirstLine(s string, max int) string
- func HumanDuration(d time.Duration) string
- func LastActivity(in Input, now time.Time) time.Time
- func LastLines(s string, n, max int) []string
- func ParseThreshold(v string, def time.Duration) (time.Duration, error)
- func SilentReason(d time.Duration) string
- type Action
- type Input
- type PGStore
- func (s *PGStore) Activity(ctx context.Context, id int64, now time.Time) (bool, error)
- func (s *PGStore) Clear(ctx context.Context, id int64, from []string) (bool, error)
- func (s *PGStore) Fail(ctx context.Context, id int64, reason string, setStatus bool, now time.Time) (bool, error)
- func (s *PGStore) MarkStuck(ctx context.Context, id int64, reason string, now time.Time, seen *time.Time) (bool, error)
- func (s *PGStore) Replied(ctx context.Context, id int64, now time.Time) error
- func (s *PGStore) Start(ctx context.Context, id int64, dispatchedBy *int64, now time.Time) error
- func (s *PGStore) Told(ctx context.Context, id int64, episode *time.Time, now time.Time) (bool, error)
- func (s *PGStore) Transition(ctx context.Context, id int64, from []string, to, reason string, now time.Time) (bool, error)
- type Store
- type Thresholds
- type Verdict
Constants ¶
const ( Running = "running" // Waiting: the agent's turn ended and it is idle WITHOUT an explicit // completion signal — it asked something, or reported progress. The owner's // move; the watchdog does not judge it, and its next turn re-arms running. Waiting = "waiting" Done = "done" Failed = "failed" Stuck = "stuck" )
The outcomes (the agents_outcome_check constraint's closed set, minus "").
const ( DefaultNoReplyAfter = 5 * time.Minute DefaultSilentAfter = 30 * time.Minute // DefaultInterval is how often the watchdog sweeps. A verdict lands at most // this long after its threshold passed. DefaultInterval = time.Minute )
The thresholds' defaults (operator decision): no first reply within five minutes of the kickoff, or no activity for thirty once it has replied.
const MinThreshold = time.Second
MinThreshold bounds a configured threshold or interval from below: anything shorter is a typo, not a policy (a browser test sets seconds).
const ReasonNoReply = "no reply since kickoff"
ReasonNoReply is the stuck reason before the agent's first reply.
Variables ¶
This section is empty.
Functions ¶
func FirstLine ¶
FirstLine is a reply's first non-empty line, cut to max runes with an ellipsis: what the "finished" push says.
func HumanDuration ¶
HumanDuration says d the way the reasons do: whole minutes as "30 min", whole hours as "2 h", anything else as Go spells it ("45s").
func LastActivity ¶
LastActivity is the newest evidence that the agent is alive: agents.LastActivity (its row and its transcript — THE definition the sidebar and GET /api/agents already use) and the activity muster observed itself (agents.activity_at: turn starts, ends and stream events, pod work reports).
🔴 ONE DEFINITION, REUSED, NOT A SECOND ONE. The watchdog asks "when was it last active" and agents.LastActivity is where muster answers that; this only adds the half that the transcript cannot see — a long tool run streams events and heartbeats for an hour while writing no transcript row until it ends.
func LastLines ¶
LastLines is a reply's last n non-empty lines, each cut to max runes: what a "waiting for you" push and note quote — the end of a turn is where its question is, not its first line.
func ParseThreshold ¶
ParseThreshold reads one threshold variable: "" is def; otherwise a Go duration of at least MinThreshold.
func SilentReason ¶
SilentReason is the stuck reason after it: "silent for 30 min".
Types ¶
type Input ¶
type Input struct {
Agent agents.Agent
// LastMessage is the agent's newest transcript row (zero when none):
// agents.LastActivity's second half.
LastMessage time.Time
// Working is whether the agent is visibly working NOW: a turn of it is in
// flight (a claude-code turn streams nothing until it ends), or its pod
// reports subagents or background shells (refreshed every minute).
Working bool
}
Input is everything Decide reads about one agent.
type PGStore ¶
type PGStore struct {
// contains filtered or unexported fields
}
PGStore is the Postgres Store.
type Store ¶
type Store interface {
// Start marks agent id as started on a first turn: outcome running, the
// clocks at now, and the chief that started it (nil for another door). It
// re-arms an agent that ended (a new dispatch is a new piece of work).
Start(ctx context.Context, id int64, dispatchedBy *int64, now time.Time) error
// Activity records activity at now. An agent that is stuck recovers (its
// outcome goes back to running); recovered reports whether that happened.
// An untracked agent gets its activity time and nothing else.
Activity(ctx context.Context, id int64, now time.Time) (recovered bool, err error)
// Replied records the agent's first reply at now (kept when one is
// recorded already), and activity.
Replied(ctx context.Context, id int64, now time.Time) error
// Transition moves the outcome to `to` with reason at now when — and only
// when — the stored outcome is one of from. applied is whether it moved:
// the caller that pushes is the one whose write applied.
Transition(ctx context.Context, id int64, from []string, to, reason string, now time.Time) (applied bool, err error)
// Told claims the telling of agent id's wait: it records now as told when
// the agent is still waiting and untold and — with episode — still in the
// wait that began then (outcome_at). applied: this caller tells (the push
// and the chief's note), or, for the owner opening the page, it was seen.
Told(ctx context.Context, id int64, episode *time.Time, now time.Time) (applied bool, err error)
// MarkStuck moves a running agent to stuck with reason at now when — and
// only when — it is still running AND its activity is still seen (the value
// the verdict was taken on; nil for none): activity that landed after the
// read wins, and nothing is said.
MarkStuck(ctx context.Context, id int64, reason string, now time.Time, seen *time.Time) (applied bool, err error)
// Fail records a failed turn: outcome failed with reason (from any outcome
// but failed), and — when setStatus — status `error` with reason as its
// error_message, which moves updated_at (a real transition). applied is
// whether the OUTCOME moved (the one push); the status is written either way.
Fail(ctx context.Context, id int64, reason string, setStatus bool, now time.Time) (applied bool, err error)
// Clear makes the agent untracked again ("" outcome) when its outcome is one
// of from: a restart after a failure, or a chief's own recovery.
Clear(ctx context.Context, id int64, from []string) (bool, error)
}
Store writes an agent's outcome columns (migration 0088). Reads come with the row itself (agents.Agent's Outcome fields, scanned by agents.PGStore).
Every method acts as ctx's principal: agents is under row-level security, so a background caller passes authz.System and a request passes its own.
type Thresholds ¶
type Thresholds struct {
// NoReply is how long a started agent may go without its first reply AND
// without any activity before it is stuck.
NoReply time.Duration
// Silent is how long an agent that has replied, and is still working, may go
// without activity before it is stuck.
Silent time.Duration
}
Thresholds is the watchdog's configuration (MUSTER_AGENT_STUCK_NO_REPLY, MUSTER_AGENT_STUCK_SILENT, MUSTER_AGENT_WATCHDOG_INTERVAL).
func DefaultThresholds ¶
func DefaultThresholds() Thresholds
DefaultThresholds is the operator's defaults.
type Verdict ¶
type Verdict struct {
Action Action
Reason string
// Last is the activity the verdict was taken on (the newest of the evidence).
Last time.Time
}
Verdict is Decide's answer.
func Decide ¶
func Decide(in Input, now time.Time, th Thresholds) Verdict
Decide is THE stuck predicate: whether one tracked agent is stuck now, or has recovered. Pure; the watchdog applies what it answers.
- Only an agent whose outcome is running or stuck is judged; done, failed and untracked are left alone.
- Only a live agent whose first turn was delivered: before the kickoff is stamped, internal/agentkickoff owns the verdict (its dwell bound ends an undelivered first turn in `error`), and a stopped or errored agent is not quiet, it is stopped or failed.
- Before its first reply, NoReply of no activity is stuck ("no reply since kickoff"); after it, Silent of no activity is ("silent for 30 min"). Activity is LastActivity: a working long tool run streams events and heartbeats, so it is never flipped.
- A stuck agent with activity after the verdict recovers.