Files
pipelines/internal/pipeline/session_export_gates.go
T

75 lines
3.5 KiB
Go

package pipeline
import "context"
func (s *Store) SessionExportBlocked(ctx context.Context, id int64) (bool, error) {
var blocked bool
err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM session_export_gates WHERE run_id=?)`, id).Scan(&blocked)
return blocked, err
}
// PendingSessionExportGate authorizes only export repair of the exact completed
// visit, never a model call or a human decision. No live executor is implied by
// the workspace reservation maintained for this checkpoint.
func (s *Store) PendingSessionExportGate(ctx context.Context, id int64, sequence int) (bool, error) {
var pending bool
err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM session_export_gates g
JOIN pipeline_runs r ON r.id=g.run_id JOIN run_steps s ON s.run_id=g.run_id AND s.sequence=g.sequence
WHERE g.run_id=? AND g.sequence=? AND r.cancel_requested=0 AND r.cleanup_unconfirmed=0
AND s.status NOT IN ('running','waiting')
AND NOT EXISTS(SELECT 1 FROM run_steps a WHERE (a.run_id=g.run_id OR a.session_id=s.session_id) AND a.agent_active<>0))`, id, sequence).Scan(&pending)
return pending, err
}
// SessionExportReservations survive process restarts and cannot be confused
// with agent-active/executing flags in the UI.
func (s *Store) SessionExportReservations(ctx context.Context) (map[int64]string, error) {
rows, err := s.db.QueryContext(ctx, `SELECT r.id,r.workspace FROM session_export_gates g JOIN pipeline_runs r ON r.id=g.run_id WHERE r.cancel_requested=0 AND r.status<>'canceled'`)
if err != nil {
return nil, err
}
defer rows.Close()
reservations := map[int64]string{}
for rows.Next() {
var id int64
var workspace string
if err := rows.Scan(&id, &workspace); err != nil {
return nil, err
}
reservations[id] = workspace
}
return reservations, rows.Err()
}
// ReadySessionExportGates includes interrupted continuations. The gate remains
// durable until a successor is begun (or a terminal target is committed).
func (s *Store) ReadySessionExportGates(ctx context.Context) (map[int64]int, error) {
rows, err := s.db.QueryContext(ctx, `SELECT g.run_id,g.sequence FROM session_export_gates g
JOIN session_export_jobs j ON j.run_id=g.run_id AND j.sequence=g.sequence
JOIN pipeline_runs r ON r.id=g.run_id
WHERE j.status='complete' AND r.status IN ('waiting','failed','interrupted') AND r.cancel_requested=0 AND r.cleanup_unconfirmed=0
AND NOT EXISTS(SELECT 1 FROM run_steps a WHERE a.run_id=g.run_id AND (a.agent_active<>0 OR a.sequence>g.sequence)) AND `+exactExportJobOwner+` LIMIT 64`)
if err != nil {
return nil, err
}
defer rows.Close()
ready := map[int64]int{}
for rows.Next() {
var id int64
var sequence int
if err := rows.Scan(&id, &sequence); err != nil {
return nil, err
}
ready[id] = sequence
}
return ready, rows.Err()
}
// ResumeSessionExportGate is a CAS, not a retry of the model invocation.
func (s *Store) ResumeSessionExportGate(ctx context.Context, id int64, sequence int) error {
return affected(s.db.ExecContext(ctx, `UPDATE pipeline_runs SET status='running',outcome='',diagnostic='',finished_at=NULL
WHERE id=? AND status IN ('waiting','failed','interrupted') AND cancel_requested=0 AND cleanup_unconfirmed=0
AND EXISTS(SELECT 1 FROM session_export_gates g JOIN session_export_jobs j ON j.run_id=g.run_id AND j.sequence=g.sequence WHERE g.run_id=pipeline_runs.id AND g.sequence=? AND j.status='complete' AND `+exactExportJobOwner+`)
AND NOT EXISTS(SELECT 1 FROM run_steps a WHERE a.run_id=pipeline_runs.id AND (a.agent_active<>0 OR a.sequence>?))`, id, sequence, sequence))
}