From 098f60079b794cce333723c817894f08ab2c3eff Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 10:18:57 +1000 Subject: [PATCH] refactor(gateway): delete orphaned SessionState goal/steer/queue machinery No /goal, /steer, or /queue command exists in messaging.localCommands, no Router handler dispatches them, and BuildGoalPrompt was never merged into the turn's system prompt. NewSessionState had no caller outside its own test. Delete the entire mechanism rather than let a fully-built but unreachable feature be mistaken for a live one. --- internal/gateway/session_state.go | 307 ------------------------ internal/gateway/session_state_test.go | 308 ------------------------- 2 files changed, 615 deletions(-) delete mode 100644 internal/gateway/session_state.go delete mode 100644 internal/gateway/session_state_test.go diff --git a/internal/gateway/session_state.go b/internal/gateway/session_state.go deleted file mode 100644 index eed2c9a85..000000000 --- a/internal/gateway/session_state.go +++ /dev/null @@ -1,307 +0,0 @@ -package gateway - -import ( - "fmt" - "strings" - "sync" -) - -// GoalStatus is the operational state of a goal. -type GoalStatus string - -const ( - GoalActive GoalStatus = "active" - GoalPaused GoalStatus = "paused" -) - -// Goal is a standing objective for a session. -type Goal struct { - Text string `json:"text"` - Status GoalStatus `json:"status"` -} - -// Subgoal is a child objective under a goal. -type Subgoal struct { - Text string `json:"text"` -} - -// QueueEntry is a single item in the session's background queue. -type QueueEntry struct { - Text string `json:"text"` -} - -// SessionState holds per-session control state for goal, steer, and -// queue commands. All access is guarded by the embedded mutex. -type SessionState struct { - mu sync.Mutex - - // Goal management - goals []Goal - subgoals []Subgoal - waiting bool // /goal wait flag - - // Steer injection - steerText string - - // Background queue - queue []QueueEntry -} - -// NewSessionState returns an empty, usable SessionState. -func NewSessionState() *SessionState { - return &SessionState{} -} - -// ── Goal operations ────────────────────────────────────────────────── - -// SetGoal replaces the current goal. If there is an existing goal with -// subgoals, subgoals are preserved unless clearSubgoals is true. -func (s *SessionState) SetGoal(text string, clearSubgoals bool) { - s.mu.Lock() - defer s.mu.Unlock() - if len(s.goals) == 0 { - s.goals = []Goal{{Text: text, Status: GoalActive}} - return - } - s.goals[0] = Goal{Text: text, Status: GoalActive} - if clearSubgoals { - s.subgoals = nil - } -} - -// Goals returns a snapshot of current goals. -func (s *SessionState) Goals() []Goal { - s.mu.Lock() - defer s.mu.Unlock() - out := make([]Goal, len(s.goals)) - copy(out, s.goals) - return out -} - -// PauseGoal marks the first goal as paused. -func (s *SessionState) PauseGoal() bool { - s.mu.Lock() - defer s.mu.Unlock() - if len(s.goals) == 0 { - return false - } - s.goals[0].Status = GoalPaused - return true -} - -// ResumeGoal marks the first goal as active. -func (s *SessionState) ResumeGoal() bool { - s.mu.Lock() - defer s.mu.Unlock() - if len(s.goals) == 0 { - return false - } - s.goals[0].Status = GoalActive - return true -} - -// ClearGoal removes all goals and subgoals. -func (s *SessionState) ClearGoal() { - s.mu.Lock() - defer s.mu.Unlock() - s.goals = nil - s.subgoals = nil - s.waiting = false -} - -// HasGoal reports whether a goal is set. -func (s *SessionState) HasGoal() bool { - s.mu.Lock() - defer s.mu.Unlock() - return len(s.goals) > 0 -} - -// ── Subgoal operations ─────────────────────────────────────────────── - -// AddSubgoal appends a subgoal. -func (s *SessionState) AddSubgoal(text string) bool { - s.mu.Lock() - defer s.mu.Unlock() - if len(s.goals) == 0 { - return false - } - s.subgoals = append(s.subgoals, Subgoal{Text: text}) - return true -} - -// Subgoals returns a snapshot of current subgoals. -func (s *SessionState) Subgoals() []Subgoal { - s.mu.Lock() - defer s.mu.Unlock() - out := make([]Subgoal, len(s.subgoals)) - copy(out, s.subgoals) - return out -} - -// RemoveSubgoal removes the subgoal at the given 1-indexed position. -// Returns false if the index is out of range. -func (s *SessionState) RemoveSubgoal(n int) bool { - s.mu.Lock() - defer s.mu.Unlock() - idx := n - 1 - if idx < 0 || idx >= len(s.subgoals) { - return false - } - s.subgoals = append(s.subgoals[:idx], s.subgoals[idx+1:]...) - return true -} - -// ClearSubgoals removes all subgoals. -func (s *SessionState) ClearSubgoals() { - s.mu.Lock() - defer s.mu.Unlock() - s.subgoals = nil -} - -// ── Wait operations ────────────────────────────────────────────────── - -// SetWait sets the wait flag. -func (s *SessionState) SetWait() { - s.mu.Lock() - defer s.mu.Unlock() - s.waiting = true -} - -// ClearWait clears the wait flag. -func (s *SessionState) ClearWait() { - s.mu.Lock() - defer s.mu.Unlock() - s.waiting = false -} - -// IsWaiting reports the wait flag. -func (s *SessionState) IsWaiting() bool { - s.mu.Lock() - defer s.mu.Unlock() - return s.waiting -} - -// ── Steer operations ───────────────────────────────────────────────── - -// SetSteer stores a pending steer message. The previous steer (if not yet -// consumed) is replaced. -func (s *SessionState) SetSteer(text string) { - s.mu.Lock() - defer s.mu.Unlock() - s.steerText = text -} - -// PollSteer returns and clears any pending steer message. The second -// return value is false when no steer was pending. -func (s *SessionState) PollSteer() (string, bool) { - s.mu.Lock() - defer s.mu.Unlock() - if s.steerText == "" { - return "", false - } - text := s.steerText - s.steerText = "" - return text, true -} - -// PeekSteer returns the pending steer message without consuming it. -func (s *SessionState) PeekSteer() (string, bool) { - s.mu.Lock() - defer s.mu.Unlock() - if s.steerText == "" { - return "", false - } - return s.steerText, true -} - -// HasPendingSteer reports whether a steer message is waiting. -func (s *SessionState) HasPendingSteer() bool { - s.mu.Lock() - defer s.mu.Unlock() - return s.steerText != "" -} - -// ── Queue operations ───────────────────────────────────────────────── - -// AddToQueue appends an entry to the queue. -func (s *SessionState) AddToQueue(text string) { - s.mu.Lock() - defer s.mu.Unlock() - s.queue = append(s.queue, QueueEntry{Text: text}) -} - -// QueueEntries returns a snapshot of the queue. -func (s *SessionState) QueueEntries() []QueueEntry { - s.mu.Lock() - defer s.mu.Unlock() - out := make([]QueueEntry, len(s.queue)) - copy(out, s.queue) - return out -} - -// RemoveFromQueue removes the entry at the given 1-indexed position. -// Returns false if the index is out of range. -func (s *SessionState) RemoveFromQueue(n int) bool { - s.mu.Lock() - defer s.mu.Unlock() - idx := n - 1 - if idx < 0 || idx >= len(s.queue) { - return false - } - s.queue = append(s.queue[:idx], s.queue[idx+1:]...) - return true -} - -// ClearQueue removes all queue entries. -func (s *SessionState) ClearQueue() { - s.mu.Lock() - defer s.mu.Unlock() - s.queue = nil -} - -// QueueLen returns the number of queue entries. -func (s *SessionState) QueueLen() int { - s.mu.Lock() - defer s.mu.Unlock() - return len(s.queue) -} - -// ── Prompt rendering ───────────────────────────────────────────────── - -// BuildGoalPrompt returns a string suitable for injecting into the system -// prompt. Returns empty string when no goal is set. -func (s *SessionState) BuildGoalPrompt() string { - s.mu.Lock() - defer s.mu.Unlock() - if len(s.goals) == 0 { - return "" - } - var b strings.Builder - b.WriteString("\n") - b.WriteString(s.goals[0].Text) - b.WriteString("\n") - - if len(s.subgoals) > 0 { - b.WriteString("\nSubgoals:\n") - for i, sg := range s.subgoals { - fmt.Fprintf(&b, "%d. %s\n", i+1, sg.Text) - } - } - b.WriteString("") - - if s.waiting && s.goals[0].Status == GoalActive { - b.WriteString("\n\nThe goal has a wait flag set. Do not begin new work toward the goal unless the user explicitly asks you to proceed. Continue responding to other requests normally.") - } - if s.goals[0].Status == GoalPaused { - b.WriteString("\n\nThe goal is paused. Do not work toward it unless the user explicitly resumes it.") - } - - return b.String() -} diff --git a/internal/gateway/session_state_test.go b/internal/gateway/session_state_test.go deleted file mode 100644 index 5d9ece183..000000000 --- a/internal/gateway/session_state_test.go +++ /dev/null @@ -1,308 +0,0 @@ -package gateway - -import ( - "strings" - "testing" -) - -func TestSessionStateGoalLifecycle(t *testing.T) { - s := NewSessionState() - - if s.HasGoal() { - t.Error("new state should have no goal") - } - if g := s.Goals(); len(g) != 0 { - t.Errorf("Goals() = %d, want 0", len(g)) - } - - s.SetGoal("ship the thing", true) - if !s.HasGoal() { - t.Error("HasGoal() = false after SetGoal") - } - goals := s.Goals() - if len(goals) != 1 || goals[0].Text != "ship the thing" || goals[0].Status != GoalActive { - t.Errorf("Goals() = %+v, want active goal", goals) - } - - s.PauseGoal() - goals = s.Goals() - if goals[0].Status != GoalPaused { - t.Errorf("status = %s, want paused", goals[0].Status) - } - - s.ResumeGoal() - goals = s.Goals() - if goals[0].Status != GoalActive { - t.Errorf("status = %s, want active after resume", goals[0].Status) - } - - s.ClearGoal() - if s.HasGoal() { - t.Error("HasGoal() = true after ClearGoal") - } -} - -func TestPauseResumeWithoutGoalIsNoop(t *testing.T) { - s := NewSessionState() - if s.PauseGoal() { - t.Error("PauseGoal() without goal should return false") - } - if s.ResumeGoal() { - t.Error("ResumeGoal() without goal should return false") - } -} - -func TestSessionStateSubgoals(t *testing.T) { - s := NewSessionState() - - // Cannot add subgoal without a goal - if s.AddSubgoal("do thing 1") { - t.Error("AddSubgoal() without goal should return false") - } - - s.SetGoal("ship", true) - if !s.AddSubgoal("sub-a") { - t.Error("AddSubgoal() should succeed with goal present") - } - s.AddSubgoal("sub-b") - s.AddSubgoal("sub-c") - - sgs := s.Subgoals() - if len(sgs) != 3 { - t.Fatalf("Subgoals() = %d, want 3", len(sgs)) - } - - // Remove subgoal at position 2 (sub-b) - if !s.RemoveSubgoal(2) { - t.Error("RemoveSubgoal(2) should succeed") - } - sgs = s.Subgoals() - if len(sgs) != 2 || sgs[0].Text != "sub-a" || sgs[1].Text != "sub-c" { - t.Errorf("Subgoals() = %+v, want [sub-a, sub-c]", sgs) - } - - // Out of range - if s.RemoveSubgoal(10) { - t.Error("RemoveSubgoal(10) should return false") - } - if s.RemoveSubgoal(0) { - t.Error("RemoveSubgoal(0) should return false") - } - - s.ClearSubgoals() - if len(s.Subgoals()) != 0 { - t.Error("Subgoals() not empty after ClearSubgoals") - } -} - -func TestSessionStateSetGoalPreservesSubgoalsByDefault(t *testing.T) { - s := NewSessionState() - s.SetGoal("goal-a", false) - s.AddSubgoal("sg-1") - s.AddSubgoal("sg-2") - - s.SetGoal("goal-b", false) // no clear - sgs := s.Subgoals() - if len(sgs) != 2 { - t.Errorf("subgoals = %d after SetGoal without clear, want 2", len(sgs)) - } - - s.SetGoal("goal-c", true) // clear - sgs = s.Subgoals() - if len(sgs) != 0 { - t.Errorf("subgoals = %d after SetGoal with clear, want 0", len(sgs)) - } -} - -func TestSessionStateWait(t *testing.T) { - s := NewSessionState() - if s.IsWaiting() { - t.Error("IsWaiting() = true on new state") - } - s.SetWait() - if !s.IsWaiting() { - t.Error("IsWaiting() = false after SetWait") - } - s.ClearWait() - if s.IsWaiting() { - t.Error("IsWaiting() = true after ClearWait") - } -} - -func TestSessionStateSteer(t *testing.T) { - s := NewSessionState() - - if s.HasPendingSteer() { - t.Error("HasPendingSteer() = true on new state") - } - if _, ok := s.PollSteer(); ok { - t.Error("PollSteer() should return false on empty") - } - - s.SetSteer("do the thing") - if !s.HasPendingSteer() { - t.Error("HasPendingSteer() = false after SetSteer") - } - if text, ok := s.PeekSteer(); !ok || text != "do the thing" { - t.Errorf("PeekSteer() = (%q, %v), want (do the thing, true)", text, ok) - } - // Peek doesn't consume - if !s.HasPendingSteer() { - t.Error("PeekSteer should not consume") - } - - text, ok := s.PollSteer() - if !ok || text != "do the thing" { - t.Errorf("PollSteer() = (%q, %v), want (do the thing, true)", text, ok) - } - if s.HasPendingSteer() { - t.Error("HasPendingSteer() = true after PollSteer") - } - - // Second set replaces - s.SetSteer("first") - s.SetSteer("second") - text, _ = s.PollSteer() - if text != "second" { - t.Errorf("second SetSteer should replace: got %q", text) - } -} - -func TestSessionStateQueue(t *testing.T) { - s := NewSessionState() - - if n := s.QueueLen(); n != 0 { - t.Errorf("QueueLen() = %d, want 0", n) - } - - s.AddToQueue("item-a") - s.AddToQueue("item-b") - s.AddToQueue("item-c") - - if n := s.QueueLen(); n != 3 { - t.Errorf("QueueLen() = %d, want 3", n) - } - - entries := s.QueueEntries() - if len(entries) != 3 { - t.Fatalf("QueueEntries() = %d, want 3", len(entries)) - } - - if !s.RemoveFromQueue(2) { - t.Error("RemoveFromQueue(2) should succeed") - } - entries = s.QueueEntries() - if len(entries) != 2 || entries[0].Text != "item-a" || entries[1].Text != "item-c" { - t.Errorf("QueueEntries() = %+v, want [item-a, item-c]", entries) - } - - s.ClearQueue() - if n := s.QueueLen(); n != 0 { - t.Errorf("QueueLen() = %d after clear, want 0", n) - } -} - -func TestSessionStateClearGoalResetsWait(t *testing.T) { - s := NewSessionState() - s.SetGoal("g", false) - s.SetWait() - - s.ClearGoal() - if s.IsWaiting() { - t.Error("ClearGoal should reset wait flag") - } -} - -func TestBuildGoalPromptEmpty(t *testing.T) { - s := NewSessionState() - if p := s.BuildGoalPrompt(); p != "" { - t.Errorf("BuildGoalPrompt() = %q, want empty", p) - } -} - -func TestBuildGoalPromptActive(t *testing.T) { - s := NewSessionState() - s.SetGoal("build the API", false) - s.AddSubgoal("add auth") - s.AddSubgoal("add rate limiting") - - p := s.BuildGoalPrompt() - if p == "" { - t.Fatal("BuildGoalPrompt() returned empty") - } - if !strings.Contains(p, "build the API") { - t.Error("prompt missing goal text") - } - if !strings.Contains(p, "add auth") { - t.Error("prompt missing subgoal") - } - if !strings.Contains(p, "add rate limiting") { - t.Error("prompt missing subgoal") - } - if !strings.Contains(p, "standing_goal") { - t.Error("prompt missing standing_goal tag") - } - // Active goal should not mention pause/wait - if strings.Contains(p, "paused") { - t.Error("active goal should not mention paused") - } -} - -func TestBuildGoalPromptPaused(t *testing.T) { - s := NewSessionState() - s.SetGoal("refactor", false) - s.PauseGoal() - - p := s.BuildGoalPrompt() - if !strings.Contains(p, `status="paused"`) { - t.Error("prompt missing paused status attribute") - } - if !strings.Contains(p, "paused") { - t.Error("prompt should mention goal is paused") - } -} - -func TestBuildGoalPromptWaiting(t *testing.T) { - s := NewSessionState() - s.SetGoal("write docs", false) - s.SetWait() - - p := s.BuildGoalPrompt() - if !strings.Contains(p, `wait="true"`) { - t.Error("prompt missing wait attribute") - } - if !strings.Contains(p, "wait flag") { - t.Error("prompt should mention wait flag") - } -} - -func TestBuildGoalPromptConcurrency(t *testing.T) { - // Verify that concurrent access to BuildGoalPrompt and mutations - // does not race. Run with -race. - s := NewSessionState() - s.SetGoal("concurrent goal", false) - s.AddSubgoal("sg-1") - s.AddSubgoal("sg-2") - - done := make(chan struct{}) - go func() { - for i := range 100 { - s.SetSteer("steer " + string(rune('a'+i%26))) - s.SetWait() - s.ClearWait() - s.AddToQueue("q") - } - close(done) - }() - - for range 1000 { - p := s.BuildGoalPrompt() - if p == "" { - t.Error("BuildGoalPrompt returned empty during concurrent access") - break - } - s.PollSteer() - s.QueueEntries() - } - <-done -}