state

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package state manages persistent state for nightshift runs. Tracks run history per project and task to support staleness calculation, duplicate run prevention, and task assignment tracking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssignedTask

type AssignedTask struct {
	TaskID     string    `json:"task_id"`
	Project    string    `json:"project"`
	TaskType   string    `json:"task_type"`
	AssignedAt time.Time `json:"assigned_at"`
}

AssignedTask represents a task currently assigned/in-progress.

type ProjectState

type ProjectState struct {
	Path        string               `json:"path"`
	LastRun     time.Time            `json:"last_run"`
	TaskHistory map[string]time.Time `json:"task_history"` // task type -> last run
	RunCount    int                  `json:"run_count"`
}

ProjectState tracks state for a single project.

type RunRecord

type RunRecord struct {
	ID         string    `json:"id"`
	StartTime  time.Time `json:"start_time"`
	EndTime    time.Time `json:"end_time"`
	Provider   string    `json:"provider,omitempty"`
	Project    string    `json:"project"`
	Tasks      []string  `json:"tasks"`
	TokensUsed int       `json:"tokens_used"`
	Status     string    `json:"status"` // success, failed, partial
	Error      string    `json:"error,omitempty"`
	Branch     string    `json:"branch,omitempty"`
}

RunRecord represents a single nightshift run for history tracking.

type State

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

State manages persistent nightshift state.

func New

func New(database *db.DB) (*State, error)

New creates a new State manager backed by the provided database.

func (*State) AddRunRecord

func (s *State) AddRunRecord(record RunRecord)

AddRunRecord adds a run record to history.

func (*State) ClearAllAssigned

func (s *State) ClearAllAssigned()

ClearAllAssigned removes all assigned tasks (e.g., on daemon restart).

func (*State) ClearAssigned

func (s *State) ClearAssigned(taskID string)

ClearAssigned removes a task from the assigned list.

func (*State) ClearStaleAssignments

func (s *State) ClearStaleAssignments(maxAge time.Duration) int

ClearStaleAssignments removes assignments older than the given duration.

func (*State) DaysSinceLastRun

func (s *State) DaysSinceLastRun(projectPath, taskType string) int

DaysSinceLastRun returns days since a task type was last run for a project. Returns -1 if the task has never run (treated as maximally stale).

func (*State) GetAssigned

func (s *State) GetAssigned(taskID string) (AssignedTask, bool)

GetAssigned returns the assigned task info, if any.

func (*State) GetProjectState

func (s *State) GetProjectState(projectPath string) *ProjectState

GetProjectState returns the state for a project (or nil if not tracked).

func (*State) GetRunHistory

func (s *State) GetRunHistory(n int) []RunRecord

GetRunHistory returns the last N run records (most recent first).

func (*State) GetTodayRuns

func (s *State) GetTodayRuns() []RunRecord

GetTodayRuns returns all runs from today.

func (*State) GetTodaySummary

func (s *State) GetTodaySummary() TodaySummary

GetTodaySummary returns a summary of today's activity.

func (*State) IsAssigned

func (s *State) IsAssigned(taskID string) bool

IsAssigned checks if a task is currently assigned.

func (*State) LastProjectRun

func (s *State) LastProjectRun(projectPath string) time.Time

LastProjectRun returns when a project was last processed.

func (*State) LastTaskRun

func (s *State) LastTaskRun(projectPath, taskType string) time.Time

LastTaskRun returns when a task type was last run for a project.

func (*State) ListAssigned

func (s *State) ListAssigned() []AssignedTask

ListAssigned returns all currently assigned tasks.

func (*State) MarkAssigned

func (s *State) MarkAssigned(taskID, project, taskType string)

MarkAssigned marks a task as assigned/in-progress.

func (*State) ProjectCount

func (s *State) ProjectCount() int

ProjectCount returns the number of tracked projects.

func (*State) RecordProjectRun

func (s *State) RecordProjectRun(projectPath string)

RecordProjectRun marks a project as having been processed.

func (*State) RecordTaskRun

func (s *State) RecordTaskRun(projectPath, taskType string)

RecordTaskRun marks a specific task type as having run for a project.

func (*State) StalenessBonus

func (s *State) StalenessBonus(projectPath, taskType string) float64

StalenessBonus calculates the staleness bonus for task selection. Formula: days since last run * 0.1 (capped at reasonable max). Tasks that have never run get a high bonus.

func (*State) WasProcessedToday

func (s *State) WasProcessedToday(projectPath string) bool

WasProcessedToday returns true if the project was already processed today.

type TodaySummary

type TodaySummary struct {
	TotalRuns      int
	SuccessfulRuns int
	FailedRuns     int
	TotalTokens    int
	TaskCounts     map[string]int
	Projects       []string
}

TodaySummary returns a summary of today's activity.

Jump to

Keyboard shortcuts

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