thread

package
v0.223.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 9 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 thread is the external-thread scheduler (design 2026-11-30-closed-loop-backbone §5): it turns the events queued on a thread (jobstore.Thread / ThreadEvent) into dispatches to the thread's owner — a one-off job, a resident session or a seat's session — with one dispatch in flight per thread, a merge window, a cooldown and a daily cap.

Layering (G022): thread sits on jobstore; the job service is reached through the Host interface the assembly satisfies with *job.Service, so the scheduler holds no job state. All of the scheduler's state lives in the thread tables, so a restart resumes from them.

Index

Constants

View Source
const (
	DefaultMergeWindowSec    = 20
	DefaultMaxDispatchPerDay = 20
	DefaultScanInterval      = 30 * time.Second
	DefaultRetryDelay        = 30 * time.Second
	// DefaultStaleClaim is how long a dispatched batch may stay without a settled carrier
	// (no ref recorded, or a session turn that never started) before it is returned to
	// pending.
	DefaultStaleClaim = 2 * time.Minute
)

Defaults of the dispatch rules (design §5).

View Source
const DefaultIdleCloseHours = 72

DefaultIdleCloseHours is how long a thread stays quiet before it turns idle; it is closed after the same period again (so after twice the value in total).

Variables

This section is empty.

Functions

func BuildInput

func BuildInput(lang i18n.Lang, th jobstore.Thread, evs []jobstore.ThreadEvent, batch int64) string

BuildInput turns one batch of thread events into the single input sent to the owner. The text always starts with the source mark, so an agent can tell external content from a person's own instruction; a batch of several events is numbered, and says how many came.

Types

type Clock

type Clock interface {
	Now() time.Time
	AfterFunc(d time.Duration, f func()) Timer
}

Clock is the scheduler's time source (injected in tests).

type Deps

type Deps struct {
	Store *jobstore.Store
	Host  Host
	Seats SeatResolver
	// Settings returns the dispatch rules of a thread; nil = all defaults.
	Settings func(t jobstore.Thread) Params
	// JobRequest builds the one-off job of a batch (the thread's owner is `job`, or a seat /
	// session owner that fell back). input is the already annotated text of the batch.
	// Nil means job dispatch is unavailable: such batches stay pending.
	JobRequest func(t jobstore.Thread, input string, batch int64) (job.JobRequest, error)
	// Lang picks the text language of a project; nil = auto.
	Lang func(project string) i18n.Lang
	// Clock defaults to the wall clock.
	Clock Clock
	// ScanInterval / RetryDelay / StaleClaim default to the Default* constants.
	ScanInterval time.Duration
	RetryDelay   time.Duration
	StaleClaim   time.Duration
}

Deps wires the scheduler to the rest of gofer.

type Host

type Host interface {
	Submit(req job.JobRequest) (job.JobResult, error)
	Get(id string) (job.JobResult, bool)
	SaySession(id, message string) error
}

Host is the slice of the job service the scheduler drives (satisfied by *job.Service).

type Params

type Params struct {
	MergeWindowSec    int
	CooldownSec       int
	MaxDispatchPerDay int
	// IdleCloseHours is the quiet period after which a thread turns idle (and, after the
	// same period again, closed); 0 = DefaultIdleCloseHours.
	IdleCloseHours int
}

Params are the per-thread dispatch rules (T3 reads them from the trigger's `thread:` block). A zero MergeWindowSec / MaxDispatchPerDay means the default; a negative MaxDispatchPerDay means no cap, a negative MergeWindowSec no window.

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler dispatches thread batches. Create with New, Start once, Stop before the store is closed (G051).

func New

func New(d Deps) *Scheduler

New builds a scheduler; it does nothing until Start (or the first Enqueue / signal).

func (*Scheduler) Enqueue

func (s *Scheduler) Enqueue(threadID string, ev jobstore.ThreadEvent) (stored jobstore.ThreadEvent, duplicate bool, err error)

Enqueue queues an event on a thread and arms its dispatch. duplicate reports an event already queued under the same DedupKey (nothing is queued then).

func (*Scheduler) JobEvent

func (s *Scheduler) JobEvent(jobID, eventType string, _ map[string]any)

JobEvent is the job.Service.AddEventObserver hook: a resident session returning to awaiting_input ends the turn that carried a batch. It only starts a goroutine, as an observer runs on the recording path.

func (*Scheduler) JobTerminal

func (s *Scheduler) JobTerminal(res job.JobResult)

JobTerminal is the job.Service.OnTerminal hook: a finished job may end an in-flight batch.

func (*Scheduler) Scan

func (s *Scheduler) Scan()

Scan settles the batches whose carrier ended and evaluates every thread with pending events.

func (*Scheduler) Start

func (s *Scheduler) Start()

Start runs one recovery scan synchronously (events pending after a restart are picked up, batches whose carrier already ended are settled) and then scans every ScanInterval.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the timers and the scan loop and waits for running dispatches. Idempotent.

func (*Scheduler) SweepIdle

func (s *Scheduler) SweepIdle()

SweepIdle moves quiet threads along open -> idle -> closed: a thread with no pending or in-flight event and no new event for IdleCloseHours (default 72) becomes idle, and one that stayed quiet for twice that becomes closed. A new event reopens either (Enqueue).

func (*Scheduler) TryDispatch

func (s *Scheduler) TryDispatch(threadID string)

TryDispatch evaluates one thread now (window, cooldown, cap, single in flight).

type SeatResolver

type SeatResolver interface {
	SeatSession(projectKey, seatName string) (sessionJobID string, ok bool)
}

SeatResolver names the resident session sitting in a seat. ok=false means the seat is vacant, unknown, archived or not an ACP seat the scheduler can talk to.

type StoreSeats

type StoreSeats struct{ Store *jobstore.Store }

StoreSeats resolves `seat:<name>` owners from the seat roster: the ACP job sitting in the active seat of that name in the project. A terminal seat, a vacant seat, an archived seat and an unknown name all report ok=false (the scheduler then falls back to a job).

func (StoreSeats) SeatSession

func (r StoreSeats) SeatSession(projectKey, seatName string) (string, bool)

SeatSession implements SeatResolver.

type Timer

type Timer interface{ Stop() bool }

Timer is a stoppable pending call.

Jump to

Keyboard shortcuts

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