Documentation
¶
Overview ¶
Package team is the business layer of the Team entity (design docs/design/2026-11-18-resident-sessions-and-team-design.md §7, plan docs/plans/2026-11-26-team-plan.md): a team is one leader seat plus member seats of one project. The rows live in jobstore (teams / team_members / team_routes / team_evals); this package adds validation, seat-name resolution, the roster (members joined with their seat status) and the route / eval record writers.
The routing engine and the leader protocol are not here: they build on this package. It imports neither httpapi nor commands (G022).
Index ¶
- Constants
- Variables
- type AttentionFact
- type ChainEnd
- type CreateInput
- type Decision
- type Deps
- type Failure
- type Input
- type Party
- type Protocol
- func (p *Protocol) BeginTurn(lang i18n.Lang, ref string, human bool) string
- func (p *Protocol) CheckSend(origin Party) error
- func (p *Protocol) EndTurn(ref string) bool
- func (p *Protocol) IsLeader(o Party) (jobstore.Team, bool, error)
- func (p *Protocol) NoteSent(origin Party)
- func (p *Protocol) OnChainEnd(f func(ChainEnd) error)
- func (p *Protocol) PeekNote(lang i18n.Lang, ids ...string) string
- func (p *Protocol) RecordEval(origin Party, outcome, reason string) (jobstore.TeamEval, error)
- func (p *Protocol) SetBoundary(f func() string)
- func (p *Protocol) SetClock(f func() time.Time)
- func (p *Protocol) TurnEnded(ids ...string)
- func (p *Protocol) TurnNote(lang i18n.Lang, ids ...string) string
- type Roster
- type RosterEntry
- type Router
- type Service
- func (s *Service) AddMember(teamID, seatRef string) (jobstore.Seat, error)
- func (s *Service) Archive(id string) (jobstore.Team, error)
- func (s *Service) Attention(allow func(project string) bool) ([]AttentionFact, error)
- func (s *Service) ByName(projectKey, name string) (jobstore.Team, error)
- func (s *Service) Create(in CreateInput) (jobstore.Team, error)
- func (s *Service) Evals(teamID string, limit int) ([]jobstore.TeamEval, error)
- func (s *Service) Find(projectKey, ref string) (jobstore.Team, error)
- func (s *Service) Get(id string) (jobstore.Team, error)
- func (s *Service) List(f jobstore.TeamFilter) ([]jobstore.Team, error)
- func (s *Service) RecordEval(e jobstore.TeamEval) (jobstore.TeamEval, error)
- func (s *Service) RecordRoute(r jobstore.TeamRoute) (jobstore.TeamRoute, error)
- func (s *Service) RemoveMember(teamID, seatRef string) (bool, error)
- func (s *Service) Roster(viewer, teamID string) (Roster, error)
- func (s *Service) Routes(teamID string, limit int) ([]jobstore.TeamRoute, error)
- func (s *Service) SeatRef(callerID, projectKey, ref string) (jobstore.Seat, error)
- func (s *Service) SetBrief(id, brief string) (jobstore.Team, error)
- func (s *Service) SetLeader(id, seatRef string) (jobstore.Team, error)
- func (s *Service) TeamOfSeat(seatID string) (jobstore.Team, bool, error)
- func (s *Service) Timeline(viewer, teamID string, q TimelineQuery) (TimelinePage, error)
- func (s *Service) Update(id string, in UpdateInput) (jobstore.Team, error)
- type StageBriefer
- type TimelineItem
- type TimelinePage
- type TimelineParty
- type TimelineQuery
- type UpdateInput
Constants ¶
const ( // AttentionMissingEval: the leader ended MissingEvalLimit turns in a row without evaluating. AttentionMissingEval = "missing_eval" // AttentionRelayHanded: a relay between members was handed to the leader by the loop guard // and nothing has happened since. AttentionRelayHanded = "relay_handed" // AttentionMembersOffline: no member seat has a live session while letters wait for the // team's seats. AttentionMembersOffline = "members_offline" )
Fact kinds.
const ( // RuleSelf: the leader wrote to the team or to itself — recorded, never delivered (1). RuleSelf = "self" // RuleHumanNamed / RuleHumanDefault: a human (or anyone outside the team) wrote to the // team; `@member` in the body picks the members, else the leader gets it (2). RuleHumanNamed = "human_named" RuleHumanDefault = "human_default" // RuleMemberResult: a member's result letter goes to the leader (3). RuleMemberResult = "member_result" // RuleRelay: a member names another member (4); over the loop guard it is handed to the // leader instead (outcome handed_to_leader). RuleRelay = "relay" // RuleLeaderDispatch: the leader writes to a member; it only breaks relay runs. RuleLeaderDispatch = "leader_dispatch" // RulePeerLeader: the leader of another team wrote; only this team's leader gets it (10). RulePeerLeader = "peer_leader" // RuleMemberFailed: the system letter a failed member job sends the leader (9). RuleMemberFailed = "member_failed" // RuleChainEnd: the system letter that puts a finished chain's last result in front of the // team creator (leader protocol, plan §2.4). RuleChainEnd = "chain_end" // RuleOutbound: a team seat wrote to somebody outside the team; recorded so the ledger // run is broken. RuleOutbound = "outbound" // RuleDirect: somebody outside the team addressed a member seat by name; delivered as // addressed. RuleDirect = "direct" )
Rule names (team_routes.rule).
const ( SystemMemberFailed = "member_failed" SystemChainEnd = "chain_end" )
SystemMemberFailed is mail.system_kind of the letter a failed member job produces; SystemChainEnd that of the letter a chain end produces.
const ( RecipientLeader = "leader" RecipientTeam = "team" )
Recipient words of a scoped send.
const ( RoleLeader = "leader" RoleMember = jobstore.TeamRoleMember )
Role of a roster row.
const ( ItemLetter = "letter" ItemEval = "eval" ItemLedger = "ledger" )
Timeline item kinds.
const ( TimelineDefaultLimit = 50 TimelineMaxLimit = 200 )
TimelineDefaultLimit / TimelineMaxLimit bound one page.
const MissingEvalLimit = 3
MissingEvalLimit is how many consecutive turns without an evaluation raise the 「等我」 card.
const PreviewRunes = 240
PreviewRunes bounds the body preview of a letter.
const RelayHandedWindow = 24 * time.Hour
RelayHandedWindow is how long a handed relay stays a fact. After it the loop was either resolved by the leader or abandoned, and a card that nobody can clear would only be noise. The fact also ends earlier, when the leader records a real evaluation (anything but missing_eval) after the handed row: the leader took the redirected letter up.
Variables ¶
var ( // ErrNotLeader: the caller is not the session / job sitting in the leader seat of a team. ErrNotLeader = errors.New("team: only the session or job sitting in a team's leader seat may do this") // ErrIdleTurn: the leader already recorded idle in this turn, so it may not send a team // letter (or evaluate again) before the turn ends. ErrIdleTurn = errors.New("team: this turn already recorded idle, so no team letter may be sent in it; a new turn starts when the next message or letter wakes you") )
var ( // ErrNotInTeam: the sender does not currently sit in a seat that belongs to an active team. ErrNotInTeam = errors.New("team: this session does not sit in a seat of a team, so it has no team to write to") // ErrNotTeamRecipient: the recipient is not the leader or a member of the sender's own team. ErrNotTeamRecipient = errors.New("team: the recipient is not the leader or a member of your own team") // ErrBadTeamRecipient: the recipient is empty, or names the sender itself / is meaningless for // the sender's role. ErrBadTeamRecipient = errors.New("team: invalid recipient") )
var ErrBadCursor = jobstore.ErrTeamInvalid
ErrBadCursor is returned for a Before that is not a cursor this service produced.
Functions ¶
This section is empty.
Types ¶
type AttentionFact ¶
type AttentionFact struct {
TeamID string
Name string
ProjectKey string
Kind string
// Since is when the condition began: the newest missing_eval, the handed relay, or the oldest
// letter still waiting.
Since int64
// Count is kind specific: the letters waiting (members_offline), 0 otherwise.
Count int
}
AttentionFact is a team that needs a person. Every kind is derived from stored rows on each read, so a fact goes away by itself when its condition ends (or the team is archived).
type ChainEnd ¶
type ChainEnd struct {
Team jobstore.Team
Leader jobstore.Seat
// Member is the name of the member whose result it was ("" = unknown).
Member string
// Trigger is the result letter that woke the leader (the newest one when several did).
Trigger jobstore.Mail
TurnRef string
}
ChainEnd is the fact "a leader turn woken by a member result ended with nothing sent, nothing dispatched and no wakeup left": the result that started it must reach a person.
type CreateInput ¶
type CreateInput struct {
CallerID string
ProjectKey string
Name string
// Leader is the leader seat; Members the initial member seats.
Leader string
Members []string
Brief string
// RelayMode is leader (default) or direct.
RelayMode string
RelayRoundLimit *int
RelayDailyLimit *int
MergeWindowSec *int
}
CreateInput is what a new team needs. Seat references are a seat id or a seat name of the caller in the project. A nil limit takes the default.
type Decision ¶
type Decision struct {
Applies bool
// Team is the team whose ledger records the decision (the recipient's, or the sender's
// for an outbound letter); Ledgers lists every ledger to write (a peer letter lands in
// both leaders' teams).
Team jobstore.Team
Ledgers []jobstore.Team
Rule string
// FromRef is who the ledger says sent it.
FromRef string
// Recipients are the seats to deliver to; empty keeps the address's own recipient.
Recipients []jobstore.Seat
// Handed: the loop guard redirected the letter to the leader (outcome handed_to_leader).
Handed bool
// Drop: record only, deliver nothing (rule self).
Drop bool
// SenderLeader: the sender is, right now, the session / job sitting in the leader seat of
// the team that sends (the leader protocol's eval binding keys on it).
SenderLeader bool
// Hold is set when the letter must be parked instead of delivered (a peer pair over the
// loop limit: there is nobody to redirect to but the leader that is already the target).
Hold string
// Hop is the hop the letter carries; Tag the source line to prefix to its body ("" none).
Hop int
Tag string
// Note tells the recipient why it got a letter it was not addressed with (a redirect).
Note string
// contains filtered or unexported fields
}
Decision is what the engine decided. Applies=false means the letter is not a team matter and goes through the mailbox exactly as before.
type Deps ¶
type Deps struct {
Seats *seat.Service
StageBrief StageBriefer
}
Deps are the collaborators of a Service. Seats is required for the roster; StageBrief is optional (no flow information in the roster without it).
type Input ¶
type Input struct {
Origin Party
// ToRaw is the address as written ("" = none: the sender's own box).
ToRaw string
// ViaTeamAddr says the address was `team:` (the letter was addressed to the team itself).
ViaTeamAddr bool
// TeamID is the team the address resolved through (team: or the seat: of a team seat).
TeamID string
// Target* is the resolved recipient: the session / job (empty when the seat is vacant) and
// the seat it sits in when the address named one.
TargetKind, TargetID, TargetSeatID string
Body string
// ReplyHop is the hop of the letter this one answers (0 = none).
ReplyHop int
// SourceMailID is the letter this one forwards or answers (0 = none); with IdemKey it
// forms the route key.
SourceMailID int64
// IdemKey is an idempotency key the sender (or the server for a job) supplied.
IdemKey string
Now time.Time
}
Input is one letter about to be stored.
type Party ¶
type Party struct {
// Kind is jobstore.MailKindSession / MailKindJob / MailKindCaller / MailKindGofer.
Kind string
ID string
Caller string
}
Party is a sender as the entry layer vouched for it.
type Protocol ¶
type Protocol struct {
// contains filtered or unexported fields
}
Protocol is the leader protocol engine. The zero value is unusable; build it with NewProtocol.
func NewProtocol ¶
NewProtocol builds the engine over the store, the team service (for the roster) and the router (for leader identity).
func (*Protocol) BeginTurn ¶
BeginTurn opens a turn for ref when it sits in a leader seat and returns the injection text; "" when it does not. human says a person typed the prompt that starts the turn.
func (*Protocol) CheckSend ¶
CheckSend refuses a team letter from the leader in a turn that already recorded idle.
func (*Protocol) IsLeader ¶
IsLeader reports whether ref (a job or session id) sits in a leader seat right now.
func (*Protocol) OnChainEnd ¶
OnChainEnd installs the sink that puts a chain end in front of a person (the mailbox writes the creator a letter).
func (*Protocol) PeekNote ¶
PeekNote is TurnNote without opening a turn: the block of a say that is sent again.
func (*Protocol) RecordEval ¶
RecordEval records the leader's evaluation of its turn. Only the session / job sitting in a leader seat may (rule 8); once a turn recorded idle it is final.
func (*Protocol) SetBoundary ¶
SetBoundary replaces the random boundary tag generator (tests).
func (*Protocol) TurnNote ¶
TurnNote starts a turn of the leader known by the first of ids that sits in a leader seat and returns the text to put in front of it: "" when none of them does (no team ⇒ no change). ids are the names a resident session is known by (its job id, the agent's own session id); the first one is the turn's key.
type Roster ¶
type Roster struct {
Team jobstore.Team
Entries []RosterEntry
}
Roster is a team with its leader and members, leader first.
type RosterEntry ¶
type RosterEntry struct {
Role string
Status seat.Status
// Stage is where the flow of the sitting session stands (nil: none, or not wired).
Stage *stagenote.Brief
}
RosterEntry is one seat of a team with what is derived from the session sitting in it.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router decides routes over the metadata store.
func (*Router) IsLeader ¶
IsLeader reports whether the origin is, right now, the session / job sitting in the leader seat of an active team, and returns that team. It is the one judgement every leader-only privilege uses (rule 8): it reads the seat table, never a letter.
func (*Router) MemberFailure ¶
MemberFailure says whether a job is the one sitting in a member seat of an active team (rule 9). The leader's own jobs are not covered: a leader that fails has nobody above it to tell.
func (*Router) ScopedAddress ¶ added in v0.221.0
ScopedAddress turns a scoped recipient (`leader`, `team` or a member seat name) of the sender into the mailbox address it stands for, after checking that the sender sits in a team and the recipient belongs to that same team. It writes nothing.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the team service.
func (*Service) Attention ¶
func (s *Service) Attention(allow func(project string) bool) ([]AttentionFact, error)
Attention lists the facts of the active teams. allow narrows by project (nil = every project). A team yields its facts in the order missing_eval, members_offline, relay_handed.
func (*Service) Create ¶
func (s *Service) Create(in CreateInput) (jobstore.Team, error)
Create adds a team with its leader and initial members.
func (*Service) Find ¶
Find reads a team by id, or — when ref is not an id — by name inside project.
func (*Service) RecordEval ¶
RecordEval appends one leader evaluation.
func (*Service) RecordRoute ¶
RecordRoute appends one routing decision. It is the writer the routing engine calls.
func (*Service) RemoveMember ¶
RemoveMember removes a member seat (id or name); the seat itself is untouched. It reports whether the seat was a member.
func (*Service) Roster ¶
Roster reads the roster of a team for one viewer (the viewer decides "waiting" and "unread"). It costs a fixed number of queries plus one stage lookup per seated member.
func (*Service) SeatRef ¶
SeatRef resolves a seat reference (id, or name inside the project of the caller) to a seat.
func (*Service) TeamOfSeat ¶
TeamOfSeat returns the active team a seat belongs to (ok=false: none).
func (*Service) Timeline ¶ added in v0.221.0
func (s *Service) Timeline(viewer, teamID string, q TimelineQuery) (TimelinePage, error)
Timeline reads one page of the timeline of a team. viewer is the caller whose seat status is used to resolve workbench threads.
type StageBriefer ¶
StageBriefer returns where the flow of the work item the session `ref` is on stands, or nil (it must read stored rows only: the roster is built per request).
type TimelineItem ¶ added in v0.221.0
type TimelineItem struct {
Kind string
// Key is "<kind>:<id>", unique per item.
Key string
At int64
ID int64
// Letter.
From, To TimelineParty
Hop int
Rule string
Outcome string
SystemKind string
State string
Preview string
MailID int64
// Eval.
EvalOutcome, Reason, LeaderRef string
// Ledger.
FromRef string
ToSeatIDs []string
}
TimelineItem is one entry. Only the fields of its Kind are set.
type TimelinePage ¶ added in v0.221.0
type TimelinePage struct {
Items []TimelineItem
// Next is the cursor of the next page ("" = this was the last).
Next string
}
TimelinePage is one page of the timeline.
type TimelineParty ¶ added in v0.221.0
type TimelineParty struct {
// Kind is session / job / caller / gofer ("" = nobody: a letter that resolved to no one).
Kind string
// Ref is the session or job id (or the caller id) of the end; "" when unknown.
Ref string
// SeatID / SeatName / Role are set when the end sits in a seat of this team now.
SeatID, SeatName, Role string
// ThreadID is the workbench thread of the seat's current session when Ref is that session.
ThreadID string
// Address is the address as written (the recipient side only).
Address string
}
TimelineParty is one end of a letter as the timeline shows it.
type TimelineQuery ¶ added in v0.221.0
TimelineQuery selects a page. Before is the Next of the previous page ("" = from the newest).