Refuse lead tools that would restart an interrupted or errored agent

This commit is contained in:
Ian Gulliver
2026-10-20 09:33:36 -07:00
parent 09d6c6e023
commit 76c1c673d7
6 changed files with 108 additions and 9 deletions
+18
View File
@@ -1061,6 +1061,24 @@ func (s *Store) TaskHandoffs(ctx context.Context, taskID string) ([]Handoff, err
return handoffs, rows.Err()
}
func (s *Store) SessionStopped(ctx context.Context, taskID, sessionID string) (bool, error) {
stopped := false
err := s.db.QueryRowContext(ctx, `
SELECT coalesce(json_extract(e.payload, '$.stopped'), 0) = 1 AND NOT EXISTS (
SELECT 1 FROM events a
WHERE a.task_id = e.task_id AND a.agent_session_id = e.agent_session_id AND a.seq > e.seq
AND a.kind = 'inbox.updated' AND json_extract(a.payload, '$.state') = 'answered'
)
FROM events e
WHERE e.task_id = ? AND e.agent_session_id = ? AND e.kind = 'session.state'
ORDER BY e.seq DESC LIMIT 1
`, taskID, sessionID).Scan(&stopped)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return stopped, err
}
func (s *Store) LeadEvents(ctx context.Context, owner string, after int64, limit int) ([]api.Event, error) {
rows, err := s.db.QueryContext(ctx, `
SELECT `+eventColumns+`