Documentation
¶
Overview ¶
Package sessionstate is one code-computed state per session of an owner's — needs_you, stalled or working — and the EPISODES those states open: one row per stop of one session (docs/design/session-state.md).
What the owner sees about a session that needs them (the episode push, the Attention section's rows, check-ins) is a view of these, so they cannot disagree about who is stalled. This package holds the rules (StateOf, Asked, Advance, OwnerActed, Handled) and the store (session_episodes, migration 0073); the inputs are read by internal/api's stall facts (stallInputs), never a copy.
🔴 A FAILURE NEVER QUIETS A SESSION. A stall classifier that failed or did not run counts as "asked" (Asked), a host that dropped never ends an episode (the caller only ends one as gone from a host that is online and reporting), and nothing here depends on the chief.
Index ¶
- Constants
- Variables
- func Asked(classified bool, errClass string, askStall bool) bool
- func Handled(reason string) bool
- func OwnerActed(f OwnerFacts) bool
- func StateOf(f Facts) (state string, episode bool)
- type Classification
- type Episode
- type Facts
- type Observation
- type OwnerFacts
- type PG
- func (p *PG) ClaimPush(ctx context.Context, id int64, at time.Time) (bool, error)
- func (p *PG) End(ctx context.Context, id int64, reason string, at time.Time) (bool, error)
- func (p *PG) EndRepeat(ctx context.Context, id int64, streak string, at time.Time) (string, bool, error)
- func (p *PG) EndViewed(ctx context.Context, id int64, streak string, at time.Time) (bool, error)
- func (p *PG) Get(ctx context.Context, id int64) (Episode, bool, error)
- func (p *PG) MarkCleared(ctx context.Context, id int64, at time.Time) error
- func (p *PG) Observe(ctx context.Context, id int64, o Observation) error
- func (p *PG) Open(ctx context.Context, e Episode) (Episode, bool, error)
- func (p *PG) OpenEpisodes(ctx context.Context) ([]Episode, error)
- func (p *PG) OpenFor(ctx context.Context, kind, key string) (Episode, bool, error)
- func (p *PG) Owners(ctx context.Context) ([]int64, error)
- func (p *PG) PreviousPush(ctx context.Context, kind, key string, seq int) (time.Time, error)
- func (p *PG) SetClassification(ctx context.Context, id int64, streak string, c Classification, asked bool) error
- func (p *PG) SetQuiet(ctx context.Context, id int64, reason string) error
- func (p *PG) Uncleared(ctx context.Context) ([]Episode, error)
- type Step
- type Store
Constants ¶
const ( StateWorking = "working" StateNeedsYou = "needs_you" StateStalled = "stalled" )
The states. `done` (a session that ended, or whose PRs are all merged) is a LABEL a reader adds, never a state: it can never hide a session.
const ( KindMirror = "mirror" KindAgent = "agent" )
The session kinds, as the stall sweep keys sessions (chiefnudge.KindMirror, chiefnudge.KindAgent).
const ( EndWorked = "worked" // the session worked, continuously, for EndAfter EndGone = "gone" // its window left a host that is online and reporting status EndOwner = "owner" // the owner acted in the session after it opened (OwnerActed) // EndViewed: the owner opened the session's own page in muster (its // rendered view, its terminal, an agent's conversation) after it opened. // Its own reason, not EndOwner's: the end reason is the record of WHY a // push cleared, and "the owner looked" is not "the owner answered". EndViewed = "viewed" // EndReset is written by migration 0079: every episode open when it // applied, ended once so the backlog of stops opened before an episode // could end on the owner's action is withdrawn. Muster itself writes it in // one place only: PG.EndRepeat, ending an episode another process (an // older image, mid-rollout) opened again on the streak a reset episode // ended on. PG.End refuses it. EndReset = "reset" )
The reasons an episode ends.
const ( QuietLeft = "left" // the chief said leave it, and the session asked nothing QuietNudged = "nudged" // the chief's nudge to it was sent QuietOwnerLast = "owner_last" // the owner spoke last (known without a classifier) QuietRepushFloor = "repush_floor" // held: its previous episode pushed under the repush floor ago; pushed once it passes )
The reasons an episode is quiet (Episode.Quiet, migration 0077).
const EndAfter = 120 * time.Second
EndAfter is how long a session must work, continuously, before its episode ends on its own (the design's N). A shorter blip — a quick tool call, a background task's notice — does not end one, so one stop of a session that carries on by itself is one episode. The OWNER acting in the session (OwnerActed) or opening it in muster (EndViewed) ends one at once.
Variables ¶
var ErrBadEpisode = errors.New("sessionstate: not a valid episode")
ErrBadEpisode: an episode a write refused before it reached the database.
Functions ¶
func Asked ¶
Asked is the leave rule's input: whether the session counts as having asked the owner something. A classifier that did not run (classified false) or failed (errClass not "") counts as asked, so its failure cannot quiet the session.
func Handled ¶ added in v0.144.0
Handled reports whether an episode that ended for reason was HANDLED on the streak it ended on: the owner opened the session in muster ('viewed'), or the backlog reset ended it ('reset'). 🔴 A HANDLED STOP IS NOT OPENED AGAIN ON THE SAME STREAK (PG.Open), and an episode another process opened on it anyway is ended again (PG.EndRepeat): the session has not moved since, so a new episode would push the same stop the owner already dealt with.
🔴 'owner' IS NOT HANDLED, ON PURPOSE: it means the owner ACTED — answered — so the session moved, and any stop after it is new. The episode is ended on the pass that READ the owner's word, which can be later than the pass that first saw the session's next stop (a failed read is retried), so the streak an 'owner' episode ends carrying can be the NEXT stop's. Treating it as handled would suppress that real stop's push until the session moved again. 'viewed' and 'reset' carry no such lag: a view ends the episode on the streak the owner was shown (episodeViewed), and the reset on the one last observed. A stop that ended because the session worked ('worked') or left ('gone') is not handled either — its session moved.
func OwnerActed ¶ added in v0.144.0
func OwnerActed(f OwnerFacts) bool
OwnerActed reports whether an open episode ends because its OWNER acted in the session after it opened (EndOwner) — the owner went to the session and answered it, so the push and the Stalled / waiting row are done with:
- a waiting episode whose session no longer waits: the prompt it waited on was answered;
- an owner message in the conversation written AFTER the episode opened. Only the owner's own words count (chiefnudge.OwnerMessages): a chief nudge, a relayed answer or a routed message muster pasted in, a fenced message, or a message with no time is not the owner acting.
🔴 STRICTLY AFTER OpenedAt: the owner's message that preceded the stop (an Esc-interrupted turn, the owner_last case) is at or before the opening, and leaves the episode open until a NEWER one arrives. 🔴 A FAILURE NEVER QUIETS A SESSION: a status that is unknown, or a session that waits now, ends nothing — whatever the conversation says.
The owner OPENING the session in muster is the other way they end it (EndViewed), recorded where the session's own page is rendered (internal/api's episodeViewed), not read here.
func StateOf ¶
StateOf is the session's state, and whether that state opens (or keeps) an episode. Precedence: needs_you > stalled > working.
- waiting on a prompt: needs_you, an episode — 🔴 AN ANSWERABLE ASK TOO (a held AskUserQuestion, an opencode question or permission request, a Hermes approval with a prompt id). Its push waits the same grace as any prompt's (operator decision after #463): until then an answerable ask was no episode and pushed at once, and nearly every one was answered at the terminal within a minute;
- stalled and the classifier says it asked: needs_you, an episode;
- stalled otherwise: stalled, an episode;
- anything else: working.
Types ¶
type Classification ¶
type Classification struct {
Label string
NeedsInput, AsksPermission, Neither float64
AskScore float64
Model string
Err string
}
Classification is the stall classifier's answer as an episode keeps it (chiefnudge.Result's fields; Err its error class, "" = none).
type Episode ¶
type Episode struct {
ID int64
Owner int64
// Kind and Key name the session as the stall sweep keys it.
Kind, Key string
Seq int
State string // StateNeedsYou or StateStalled
// Waiting: opened (or last seen) waiting on a prompt rather than idle.
Waiting bool
// Asked: the leave rule's input (Asked).
Asked bool
// Where the session is.
Runtime string
Host, TmuxSession, Window string
Index int
AgentID, ChatSessionID int64
StreakKey string
// The stall classifier on the session's last message, for the streak it
// classified (ClassifiedStreak; "" = not classified yet).
ClassifiedStreak string
Jev Classification
OpenedAt time.Time
StateSince time.Time
BusySince time.Time // zero: not working
BusySeen time.Time
EndedAt time.Time // zero: open
EndReason string
PushedAt time.Time
ClearedAt time.Time
// Quiet is why the episode is not pushed now (a Quiet* constant), "" for
// none. Not final: a later reason to push clears it (ClaimPush).
Quiet string
}
Episode is one row of session_episodes: one stop of one session.
type Facts ¶
type Facts struct {
// Waiting: the host reports the session waiting on a prompt
// (mirror.StatusWaiting).
Waiting bool
// Stalled: the stall rule (chiefnudge.Exclude) passed — idle past the
// threshold, no background work, no turn error, no open ask — and the
// session's last word is its own.
Stalled bool
// AskStall: the stall classifier scored the session as asking
// (chiefnudge.AskStall). Read only when Stalled.
AskStall bool
}
Facts is what StateOf reads about one session, as the stall sweep read it.
type Observation ¶
type Observation struct {
State string
Waiting bool
Host, TmuxSession, Window string
Index int
StreakKey string
BusySince, BusySeen time.Time
At time.Time
}
Observation is what Store.Observe writes.
type OwnerFacts ¶ added in v0.144.0
type OwnerFacts struct {
// OpenedAt is when the episode opened.
OpenedAt time.Time
// WasWaiting: the episode waits on a prompt muster cannot answer — it
// opened, or was last seen, waiting (Episode.Waiting).
WasWaiting bool
// Known: the session's status is known now. WaitingNow: it waits on a
// prompt now.
Known, WaitingNow bool
// Msgs is the session's conversation as read now, oldest first; nil when
// it was not read. Injected and Prefixes are what muster itself put into
// it — its nudges, relays and routed messages — as chiefnudge.OwnerMessages
// reads them.
Msgs []chiefnudge.Msg
Injected, Prefixes []string
}
OwnerFacts is what OwnerActed reads about one open episode's session.
type PG ¶
type PG struct {
// contains filtered or unexported fields
}
PG is Store on Postgres.
func (*PG) EndRepeat ¶ added in v0.144.0
func (p *PG) EndRepeat(ctx context.Context, id int64, streak string, at time.Time) (string, bool, error)
EndRepeat implements Store. 🔴 THE ONE GUARD, TWO SIDES: Open refuses to write an episode while the session's latest one ended Handled on the streak being opened; EndRepeat ends one that was written anyway, by the same predicate (handledReasons, a non-empty streak equal to the previous episode's). A PERMANENT repair, not a rollout step — the guard cannot refuse every repeat: an image from before migration 0079 has no guard (during a rolling update its pass, after the reset, opens and pushes a still-stopped session again); a waiting hook opens with no streak at all (the older image's, and this one's whenever the host reports no status time), and "" never matches; and a pass whose facts were read before a concurrent view opens on the streak it read, not the one the view ended on. So the streak compared is the session's CURRENT one (the caller's), never the repeat's stored one — and that streak is written onto the repeat as it ends, so it becomes the latest episode ended handled on it and Open's guard keeps holding (a repeat from the old waiting hook stores no streak, and ending it on "" would let the very next pass open the stop a third time). The repeat's push is withdrawn by the pass's clearEndedEpisodes, as any ended episode's.
func (*PG) EndViewed ¶ added in v0.144.0
EndViewed implements Store. 🔴 THE STREAK THE OWNER WAS SHOWN, NOT THE ONE LAST OBSERVED: a pass writes an episode's streak every 30 s, so the stored one can lag the session's (it stopped again since); ending on the lagging one would leave the stop the owner just looked at unhandled, and the next pass would open and push it.
func (*PG) MarkCleared ¶
MarkCleared implements Store.
func (*PG) Observe ¶
Observe implements Store. The state's since-time moves only when the state changes.
func (*PG) OpenEpisodes ¶
OpenEpisodes implements Store.
func (*PG) PreviousPush ¶
PreviousPush implements Store.
func (*PG) SetClassification ¶
func (p *PG) SetClassification(ctx context.Context, id int64, streak string, c Classification, asked bool) error
SetClassification implements Store.
type Step ¶
type Step struct {
// End: the episode ends now, for Reason.
End bool
Reason string
// BusySince and BusySeen are the episode's new busy_since and busy_seen_at
// (zero: not working).
BusySince, BusySeen time.Time
}
Step is what one observation does to an open episode (Advance).
func Advance ¶
Advance is one observation, at now, of an open episode's session: busy reports whether it is working now; busySince and busySeen are the episode's stored first and latest sightings of its current run of work (zero: not seen working).
The episode ends when the session has been SEEN working, continuously, for EndAfter: while it still works, or when it stops after a run whose first and last sightings are at least EndAfter apart (a turn that completed after that much work). Shorter work leaves it open and forgets the run.
🔴 MEASURED BETWEEN SIGHTINGS, NEVER TO NOW: a run is as long as the sweep saw it, so a session that worked 90 s and then sat idle until the next pass is not credited with the idle time. The cost runs the other way: between two passes the work is UNDER-counted by up to one interval, so a real turn of 120–150 s can be seen as 90–120 s and leave its stop open. Its NEXT stop then opens no new episode — and since episodes push (internal/api session_push.go), that next stop gets no push of its own: the open episode already pushed. That is QUIETER, not louder, and it is ACCEPTED, not fixed: the cost is bounded to a turn of 120–150 s, and its window still shows the stop as a row on the Asks page.
type Store ¶
type Store interface {
// Owners is every user who is not disabled: the owners the state pass
// visits, each then as its own principal. The stall nudges' read
// (chiefnudge.PG.Owners), one query and one system-scope helper.
Owners(ctx context.Context) ([]int64, error)
// Open opens an episode for the caller's session (e.Kind, e.Key), at the
// next seq. 🔴 THE CLAIM: claimed is false, and nothing is written, when
// the session already has an open episode (session_episodes_one_open) —
// another pass, or another replica, won it — and when the session's latest
// episode ended Handled on e.StreakKey (the stop was dealt with, and the
// session has not moved since).
Open(ctx context.Context, e Episode) (Episode, bool, error)
// OpenEpisodes is the caller's open episodes, oldest first.
OpenEpisodes(ctx context.Context) ([]Episode, error)
// Observe writes what a pass saw of open episode id: its state, whether it
// waits, where it is and its streak, and its run of work.
Observe(ctx context.Context, id int64, o Observation) error
// SetClassification stores the classifier's answer for streak on open
// episode id, and the leave rule's input it gives.
SetClassification(ctx context.Context, id int64, streak string, c Classification, asked bool) error
// OpenFor is the caller's open episode for session (kind, key), if any.
OpenFor(ctx context.Context, kind, key string) (Episode, bool, error)
// End ends open episode id for reason; ended false when it had already
// ended.
End(ctx context.Context, id int64, reason string, at time.Time) (bool, error)
// EndViewed ends open episode id 'viewed' (EndViewed) and records streak,
// the streak the owner was shown, as the one it ended on ("" keeps the
// stored one); ended false when it had already ended.
EndViewed(ctx context.Context, id int64, streak string, at time.Time) (bool, error)
// EndRepeat ends open episode id when it REPEATS a handled stop: the
// session's previous episode ended Handled on streak, the streak the session
// is on now — so the session has not moved since the owner dealt with it,
// and the open one is that stop opened again by a process without PG.Open's
// guard (an older image, mid-rollout). It ends for the previous episode's
// reason, which is returned; ended false (nothing written) otherwise.
EndRepeat(ctx context.Context, id int64, streak string, at time.Time) (reason string, ended bool, err error)
// Get is the caller's episode id; ok false when it is none of theirs.
Get(ctx context.Context, id int64) (Episode, bool, error)
// ClaimPush claims open episode id's push at at. 🔴 THE PUSH CLAIM:
// claimed is true for exactly one caller — the one whose write took a row
// with no pushed_at — and only that caller sends the push. It clears the
// episode's quiet reason.
ClaimPush(ctx context.Context, id int64, at time.Time) (bool, error)
// SetQuiet records why open, unpushed episode id is quiet now (a Quiet*
// constant).
SetQuiet(ctx context.Context, id int64, reason string) error
// PreviousPush is when the caller's session (kind, key) last had an
// episode before seq pushed; zero for never.
PreviousPush(ctx context.Context, kind, key string, seq int) (time.Time, error)
// Uncleared is the caller's ended episodes whose push was shown and not
// yet closed (cleared_at null): what the restart rule clears.
Uncleared(ctx context.Context) ([]Episode, error)
// MarkCleared records that ended episode id's notification was closed.
MarkCleared(ctx context.Context, id int64, at time.Time) error
}
Store is session_episodes, under row-level security: every method but Owners acts as ctx's principal, on its rows only.