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 (Classify, Asked, Advance) 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 StateOf(f Facts) (state string, episode bool)
- type Classification
- type Episode
- type Facts
- type Observation
- 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) 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 )
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 (the design's N). A shorter blip — a permission let through, a quick tool call, the owner answering at the terminal — never ends one, so one stop is one episode.
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 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 muster cannot answer: needs_you, an episode;
- waiting on an answerable ask: needs_you, but NO episode (the ask is the owner's item, with its own push);
- 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
// Answerable: the wait is an ask muster itself can answer — a held
// AskUserQuestion, an opencode question or permission request, a Hermes
// approval with a prompt id. It stays an answerable ask with its own
// full-text push, and opens NO episode.
Answerable 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 PG ¶
type PG struct {
// contains filtered or unexported fields
}
PG is Store on Postgres.
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.
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)
// 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.