agentoutcome

package
v0.225.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 8 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 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

View Source
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 "").

View Source
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.

View Source
const DoneSentinel = "MUSTER-DONE:"

DoneSentinel is THE completion signal of an agent that has no muster tool to call (a host-spawned opencode agent, docs/design/chief-dispatch.md 3.7): its final reply's LAST non-empty line, exactly this prefix and a one-line summary.

View Source
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).

View Source
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

func FirstLine(s string, max int) string

FirstLine is a reply's first non-empty line, cut to max runes with an ellipsis: what the "finished" push says.

func HumanDuration

func HumanDuration(d time.Duration) string

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

func LastActivity(in Input, now time.Time) time.Time

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

func LastLines(s string, n, max int) []string

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 ParseDone added in v0.179.0

func ParseDone(line string) (summary string, ok bool)

ParseDone is the done grammar, THE one place it is written: line (a reply's last non-empty line) trimmed of surrounding white space must START with DoneSentinel exactly — case, punctuation and all, nothing before it, no markdown around it — and what follows, trimmed, is the summary, which must not be empty. ok is false for anything else, and a reply whose last line is not this is WAITING, never done (the package doc's rule: done is an explicit signal only).

func ParseThreshold

func ParseThreshold(v string, def time.Duration) (time.Duration, error)

ParseThreshold reads one threshold variable: "" is def; otherwise a Go duration of at least MinThreshold.

func SilentReason

func SilentReason(d time.Duration) string

SilentReason is the stuck reason after it: "silent for 30 min".

Types

type Action

type Action int

Action is what the watchdog does with one agent.

const (
	// None: nothing changes.
	None Action = iota
	// MarkStuck: running → stuck, with Verdict.Reason.
	MarkStuck
	// Recover: stuck → running (new activity since it was called stuck).
	Recover
)

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.

func NewPG

func NewPG(pool *pgxpool.Pool) *PGStore

NewPG is a Store on pool.

func (*PGStore) Activity

func (s *PGStore) Activity(ctx context.Context, id int64, now time.Time) (bool, error)

func (*PGStore) Clear

func (s *PGStore) Clear(ctx context.Context, id int64, from []string) (bool, error)

func (*PGStore) Fail

func (s *PGStore) Fail(ctx context.Context, id int64, reason string, setStatus bool, now time.Time) (bool, error)

func (*PGStore) MarkStuck

func (s *PGStore) MarkStuck(ctx context.Context, id int64, reason string, now time.Time, seen *time.Time) (bool, error)

func (*PGStore) Replied

func (s *PGStore) Replied(ctx context.Context, id int64, now time.Time) error

func (*PGStore) Start

func (s *PGStore) Start(ctx context.Context, id int64, dispatchedBy *int64, now time.Time) error

func (*PGStore) Told

func (s *PGStore) Told(ctx context.Context, id int64, episode *time.Time, now time.Time) (bool, error)

func (*PGStore) Transition

func (s *PGStore) Transition(ctx context.Context, id int64, from []string, to, reason string, now time.Time) (bool, error)

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.

Jump to

Keyboard shortcuts

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