Files
pipelines/internal/pipeline/retry_checkpoints.go
T

53 lines
2.0 KiB
Go

package pipeline
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// SaveRetryCheckpoint records the immutable source and the completed-prefix
// handoff before a retry is launched. It intentionally stores no step or usage
// rows: those remain evidence belonging only to the source attempt.
func (s *Store) SaveRetryCheckpoint(ctx context.Context, runID, sourceID int64, mode string, sequence int, source *protocol.RunDetail, handoff map[string]any) error {
if mode == "" {
mode = "resume"
}
data, err := json.Marshal(handoff)
if err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
var oldSource int64
var oldMode, oldHandoff, oldHash string
var oldSeq, oldVersion int
err = s.db.QueryRowContext(ctx, `SELECT source_run_id,mode,failed_sequence,handoff_json,source_version,source_hash FROM retry_checkpoints WHERE run_id=?`, runID).Scan(&oldSource, &oldMode, &oldSeq, &oldHandoff, &oldVersion, &oldHash)
if err == nil {
if oldSource != sourceID || oldMode != mode || oldSeq != sequence || oldHandoff != string(data) || oldVersion != source.Version || oldHash != source.ContentHash {
return fmt.Errorf("retry checkpoint conflicts with the existing request")
}
return nil
}
if err != sql.ErrNoRows {
return err
}
_, err = s.db.ExecContext(ctx, `INSERT INTO retry_checkpoints(run_id,source_run_id,mode,failed_sequence,handoff_json,source_version,source_hash) VALUES(?,?,?,?,?,?,?)`, runID, sourceID, mode, sequence, string(data), source.Version, source.ContentHash)
return err
}
func (s *Store) RetryCheckpoint(ctx context.Context, runID int64) (sourceID int64, mode string, sequence int, handoff map[string]any, err error) {
s.mu.Lock()
defer s.mu.Unlock()
var data string
err = s.db.QueryRowContext(ctx, `SELECT source_run_id,mode,failed_sequence,handoff_json FROM retry_checkpoints WHERE run_id=?`, runID).Scan(&sourceID, &mode, &sequence, &data)
if err != nil {
return 0, "", 0, nil, err
}
err = json.Unmarshal([]byte(data), &handoff)
return
}