fix retry-of-retry memory prefix reconstruction
This commit is contained in:
+127
-26
@@ -420,6 +420,11 @@ type retryCheckpoint struct {
|
||||
handoff map[string]any
|
||||
}
|
||||
|
||||
type retryPrefixRecord struct {
|
||||
stage string
|
||||
sequence int
|
||||
}
|
||||
|
||||
const maxRetryInputBytes = 64 * 1024
|
||||
|
||||
func retryInputOverride(base map[string]any, override map[string]any) (map[string]any, error) {
|
||||
@@ -526,14 +531,16 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.
|
||||
// stage's memory bindings must be resolved from the original prefix before
|
||||
// this override is applied at invocation time.
|
||||
handoff = map[string]any{"input": failed.Input, "override": override, "sequence": failed.Sequence, "stage": failed.Stage, "visit": failed.Sequence}
|
||||
prefix := map[string]any{}
|
||||
for _, step := range source.Steps {
|
||||
if step.Sequence >= failed.Sequence || step.Status != "completed" {
|
||||
continue
|
||||
}
|
||||
prefix[step.Stage] = step.Output
|
||||
prefix, prefixRecords, prefixErr := r.retryPrefix(sourceID, source, failed.Sequence)
|
||||
if prefixErr != nil {
|
||||
return retryCheckpoint{}, prefixErr
|
||||
}
|
||||
handoff["prefix"] = prefix
|
||||
serializedPrefix := make([]map[string]any, 0, len(prefixRecords))
|
||||
for _, record := range prefixRecords {
|
||||
serializedPrefix = append(serializedPrefix, map[string]any{"stage": record.stage, "sequence": record.sequence})
|
||||
}
|
||||
handoff["prefix_provenance"] = serializedPrefix
|
||||
// A newer explicitly selected version may continue only when every recorded
|
||||
// prefix stage still exists with the same kind. Its outputs are evidence from
|
||||
// the source attempt and cannot be safely handed to a changed stage contract.
|
||||
@@ -541,20 +548,21 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.
|
||||
for _, declared := range source.Graph.Stages {
|
||||
sourceStages[declared.Key] = declared
|
||||
}
|
||||
for _, step := range source.Steps {
|
||||
if step.Sequence >= failed.Sequence {
|
||||
break
|
||||
for _, record := range prefixRecords {
|
||||
_, found := retryPrefixStep(source, record)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
var pinnedStage *protocol.Stage
|
||||
for i := range pinned.Graph.Stages {
|
||||
if pinned.Graph.Stages[i].Key == step.Stage {
|
||||
if pinned.Graph.Stages[i].Key == record.stage {
|
||||
pinnedStage = &pinned.Graph.Stages[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
previous, ok := sourceStages[step.Stage]
|
||||
previous, ok := sourceStages[record.stage]
|
||||
if pinnedStage == nil || !ok || !retryStageContractEqual(previous, *pinnedStage) {
|
||||
return retryCheckpoint{}, fmt.Errorf("retry source %d has incompatible changed prefix stage %q", sourceID, step.Stage)
|
||||
return retryCheckpoint{}, fmt.Errorf("retry source %d has incompatible changed prefix stage %q", sourceID, record.stage)
|
||||
}
|
||||
}
|
||||
var sourceSelected protocol.Stage
|
||||
@@ -775,7 +783,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
r.finishRunState(id, d.Name, workspace, "failed", "error", "retry checkpoint is missing handoff context", 0, 0, "", nil)
|
||||
return
|
||||
}
|
||||
state := rebuildRetryState(source, checkpoint.failed)
|
||||
state := rebuildRetryState(source, checkpoint.failed, storedHandoff)
|
||||
if override, ok := storedHandoff["override"].(map[string]any); ok {
|
||||
state.retryOverride = stage.Values(override)
|
||||
}
|
||||
@@ -897,31 +905,124 @@ func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runS
|
||||
// rebuildRetryState restores only the completed prefix. The failed visit is
|
||||
// deliberately not counted and its step/evidence is not copied into the new
|
||||
// attempt, so retries cannot duplicate execution or usage aggregates.
|
||||
func rebuildRetryState(detail *protocol.RunDetail, failed protocol.RunStep) *runState {
|
||||
func rebuildRetryState(detail *protocol.RunDetail, failed protocol.RunStep, handoff map[string]any) *runState {
|
||||
state := newRunState(failed.Stage, stage.Values(failed.Input))
|
||||
state.nextSequence = failed.Sequence
|
||||
if original, err := copyValues(stage.Values(detail.Input)); err == nil {
|
||||
state.memory["input"] = memoryValue{output: original}
|
||||
}
|
||||
for _, step := range detail.Steps {
|
||||
if step.Sequence >= failed.Sequence {
|
||||
break
|
||||
}
|
||||
state.visits[step.Stage]++
|
||||
if step.Status == "completed" {
|
||||
if values, err := copyValues(stage.Values(step.Output)); err == nil {
|
||||
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
|
||||
_, hasCheckpointPrefix := handoff["prefix"]
|
||||
if !hasCheckpointPrefix {
|
||||
for _, step := range detail.Steps {
|
||||
if step.Sequence >= failed.Sequence {
|
||||
break
|
||||
}
|
||||
state.visits[step.Stage]++
|
||||
if step.Status == "completed" {
|
||||
if values, err := copyValues(stage.Values(step.Output)); err == nil {
|
||||
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
|
||||
}
|
||||
state.completed++
|
||||
}
|
||||
state.recordChanges(step.FileChanges, step.Artifacts)
|
||||
if step.Status == "completed" {
|
||||
state.artifactDirs = append(state.artifactDirs, filepath.Join("", strconv.Itoa(step.Sequence)))
|
||||
}
|
||||
state.completed++
|
||||
}
|
||||
state.recordChanges(step.FileChanges, step.Artifacts)
|
||||
if step.Status == "completed" {
|
||||
state.artifactDirs = append(state.artifactDirs, filepath.Join("", strconv.Itoa(step.Sequence)))
|
||||
}
|
||||
// A selected-stage retry has no persisted rows for its inherited prefix.
|
||||
// Restore it from the immutable checkpoint instead of guessing from the
|
||||
// retry source's immediate steps (which is what made retry-of-retry fail
|
||||
// closed with "memory source ... has not completed").
|
||||
if prefix, ok := handoff["prefix"].(map[string]any); ok {
|
||||
for _, record := range retryPrefixRecords(handoff["prefix_provenance"]) {
|
||||
output, exists := prefix[record.stage].(map[string]any)
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
values, err := copyValues(stage.Values(output))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
state.memory[record.stage] = memoryValue{output: values, sequence: record.sequence}
|
||||
state.visits[record.stage]++
|
||||
state.completed++
|
||||
state.artifactDirs = append(state.artifactDirs, filepath.Join("", strconv.Itoa(record.sequence)))
|
||||
}
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
// retryPrefix returns the completed prefix from the source attempt plus the
|
||||
// immutable prefix carried by that attempt's checkpoint. A retry attempt only
|
||||
// persists the selected stage onward, so immediate source steps are not a
|
||||
// complete representation of its memory.
|
||||
func (r *runner) retryPrefix(sourceID int64, source *protocol.RunDetail, failedSequence int) (map[string]any, []retryPrefixRecord, error) {
|
||||
prefix := map[string]any{}
|
||||
records := []retryPrefixRecord{}
|
||||
if source.RetryMode == "resume" && source.RetrySource > 0 {
|
||||
checkpointSource, mode, _, inherited, err := r.store.RetryCheckpoint(r.ctx, sourceID)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("retry source %d has no immutable prefix checkpoint: %w", sourceID, err)
|
||||
}
|
||||
if checkpointSource != source.RetrySource || mode != "resume" {
|
||||
return nil, nil, fmt.Errorf("retry source %d has an invalid prefix checkpoint", sourceID)
|
||||
}
|
||||
if values, ok := inherited["prefix"].(map[string]any); ok {
|
||||
for key, value := range values {
|
||||
prefix[key] = value
|
||||
}
|
||||
}
|
||||
for _, record := range retryPrefixRecords(inherited["prefix_provenance"]) {
|
||||
records = append(records, record)
|
||||
}
|
||||
}
|
||||
for _, step := range source.Steps {
|
||||
if step.Sequence >= failedSequence || step.Status != "completed" {
|
||||
continue
|
||||
}
|
||||
prefix[step.Stage] = step.Output
|
||||
records = append(records, retryPrefixRecord{stage: step.Stage, sequence: step.Sequence})
|
||||
}
|
||||
return prefix, records, nil
|
||||
}
|
||||
|
||||
func retryPrefixRecords(raw any) []retryPrefixRecord {
|
||||
items, ok := raw.([]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
result := make([]retryPrefixRecord, 0, len(items))
|
||||
for _, item := range items {
|
||||
entry, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
stageName, ok := entry["stage"].(string)
|
||||
if !ok || stageName == "" {
|
||||
continue
|
||||
}
|
||||
sequence, ok := entry["sequence"].(float64)
|
||||
if !ok || sequence < 1 {
|
||||
continue
|
||||
}
|
||||
result = append(result, retryPrefixRecord{stage: stageName, sequence: int(sequence)})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func retryPrefixStep(detail *protocol.RunDetail, record retryPrefixRecord) (protocol.RunStep, bool) {
|
||||
for _, step := range detail.Steps {
|
||||
if step.Stage == record.stage && step.Sequence == record.sequence {
|
||||
return step, true
|
||||
}
|
||||
}
|
||||
// Inherited rows intentionally are not present in a selected retry. The
|
||||
// contract check still uses the stage key; this marker keeps that check
|
||||
// independent from model-claimed output.
|
||||
return protocol.RunStep{Stage: record.stage, Sequence: record.sequence}, true
|
||||
}
|
||||
|
||||
// artifactAccess derives exact visit directories; it never consumes a path from
|
||||
// a definition or model result. An unreadable or symlinked root fails closed.
|
||||
func (r *runner) artifactAccess(runID int64, sequence int, prior []string, current string) stage.ArtifactAccess {
|
||||
|
||||
Reference in New Issue
Block a user