Files
pipelines/internal/pipeline/run_loop_state.go
T
alan 34b8b94223 Engine: run findings ledger, findings gate, loop continuation, larger memory, loop lint, retry partial work, resume_session
- Runner-owned findings ledger per run: stages report a # findings section
  (new items and F<n> status changes); stable IDs, persisted (run_loop_state),
  in RunDetail.findings, findings.md, memory.md, inspect.
- type=findings gate stage routes open/clear on the ledger mechanically.
- retry --extra-visits N [--stage KEY] continues an exhausted loop; limits
  on_exhausted=decide parks the run on accept/continue/abort.
- Essential memory cap 20 KB, task never truncated, handoff and open findings
  ahead of the outcome ledger.
- Lint rejects limits.on outcomes on forward edges; warns on ineffective
  limits and resume_session.
- A resumed retry carries the failed visit's output.md and private/ under
  previous-attempt/.
- resume_session=true resumes a Claude stage's previous session on loop visits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 14:22:28 +00:00

92 lines
2.8 KiB
Go

package pipeline
import (
"context"
"database/sql"
"encoding/json"
"errors"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// initRunLoopState records the runner-owned loop state of a run: its findings
// ledger and the extra visits granted to its stages (by a retry with
// --extra-visits or a "continue" exhaustion decision). One row per run, written
// by the runner as the state changes.
func (s *Store) initRunLoopState() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS run_loop_state (
run_id INTEGER PRIMARY KEY REFERENCES pipeline_runs(id),
findings_json TEXT NOT NULL DEFAULT '[]',
grants_json TEXT NOT NULL DEFAULT '{}')`)
return err
}
// SetRunFindings replaces a run's findings ledger.
func (s *Store) SetRunFindings(ctx context.Context, runID int64, ledger []protocol.Finding) error {
if ledger == nil {
ledger = []protocol.Finding{}
}
encoded, err := json.Marshal(ledger)
if err != nil {
return err
}
_, err = s.db.ExecContext(ctx, `INSERT INTO run_loop_state(run_id,findings_json) VALUES(?,?)
ON CONFLICT(run_id) DO UPDATE SET findings_json=excluded.findings_json`, runID, string(encoded))
return err
}
// SetRunVisitGrants replaces a run's extra-visit grants.
func (s *Store) SetRunVisitGrants(ctx context.Context, runID int64, grants map[string]int) error {
if grants == nil {
grants = map[string]int{}
}
encoded, err := json.Marshal(grants)
if err != nil {
return err
}
_, err = s.db.ExecContext(ctx, `INSERT INTO run_loop_state(run_id,grants_json) VALUES(?,?)
ON CONFLICT(run_id) DO UPDATE SET grants_json=excluded.grants_json`, runID, string(encoded))
return err
}
// AddRunVisitGrant adds extra visits for one stage and returns the new grants.
func (s *Store) AddRunVisitGrant(ctx context.Context, runID int64, stageKey string, extra int) (map[string]int, error) {
_, grants, err := runLoopState(ctx, s.db, runID)
if err != nil {
return nil, err
}
if grants == nil {
grants = map[string]int{}
}
grants[stageKey] += extra
return grants, s.SetRunVisitGrants(ctx, runID, grants)
}
func runLoopState(ctx context.Context, q interface {
QueryRowContext(context.Context, string, ...any) *sql.Row
}, runID int64) ([]protocol.Finding, map[string]int, error) {
var ledgerJSON, grantsJSON string
err := q.QueryRowContext(ctx, `SELECT findings_json,grants_json FROM run_loop_state WHERE run_id=?`, runID).Scan(&ledgerJSON, &grantsJSON)
if errors.Is(err, sql.ErrNoRows) || missingTable(err) {
return nil, nil, nil
}
if err != nil {
return nil, nil, err
}
var ledger []protocol.Finding
if err := json.Unmarshal([]byte(ledgerJSON), &ledger); err != nil {
return nil, nil, err
}
var grants map[string]int
if err := json.Unmarshal([]byte(grantsJSON), &grants); err != nil {
return nil, nil, err
}
if len(ledger) == 0 {
ledger = nil
}
if len(grants) == 0 {
grants = nil
}
return ledger, grants, nil
}