-
Notifications
You must be signed in to change notification settings - Fork 0
git: add bounded rewrite scenario orchestration #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // ErrRewriteAttemptsExceeded indicates a rewrite run remained dirty after all | ||
| // configured attempts were used. | ||
| var ErrRewriteAttemptsExceeded = errors.New("rewrite scenario exceeded maximum attempts") | ||
|
|
||
| // RewriteScenarioOptions configures detect -> intervene -> verify orchestration. | ||
| // | ||
| // The flow is: | ||
| // 1. Detect whether rewrite intervention is required. | ||
| // 2. Intervene when required. | ||
| // 3. Verify whether the repo is now clean. | ||
| // 4. Repeat until clean or attempts are exhausted. | ||
| type RewriteScenarioOptions struct { | ||
| // MaxAttempts bounds total detect passes. Must be >= 1. | ||
| MaxAttempts int | ||
|
|
||
| // Detect reports whether intervention is needed for the attempt. | ||
| // Return true when the repository still needs rewrite intervention. | ||
| Detect func(ctx context.Context, attempt int) (needsIntervention bool, err error) | ||
|
|
||
| // Intervene performs one rewrite intervention pass for the attempt. | ||
| Intervene func(ctx context.Context, attempt int) error | ||
|
|
||
| // Verify checks whether the repository is clean after intervention. | ||
| // Return true when no further rewrite intervention is needed. | ||
| Verify func(ctx context.Context, attempt int) (clean bool, err error) | ||
| } | ||
|
|
||
| // RewriteAttempt captures one detect pass in the orchestration loop. | ||
| type RewriteAttempt struct { | ||
| Attempt int // 1-based pass number | ||
| NeedsIntervention bool // Detect outcome | ||
| Intervened bool // Whether Intervene ran | ||
| CleanAfterVerify bool // Verify outcome when Intervene ran | ||
| } | ||
|
|
||
| // RewriteScenarioResult captures pass-by-pass outcomes. | ||
| type RewriteScenarioResult struct { | ||
| Clean bool | ||
| Attempts int | ||
| Passes []RewriteAttempt | ||
| } | ||
|
|
||
| // RunRewriteScenario executes bounded detect -> intervene -> verify passes until | ||
| // the repository is clean or attempts are exhausted. | ||
| func RunRewriteScenario(ctx context.Context, opts RewriteScenarioOptions) (RewriteScenarioResult, error) { | ||
| result := RewriteScenarioResult{} | ||
|
|
||
| if ctx == nil { | ||
| ctx = context.Background() | ||
| } | ||
| if opts.MaxAttempts < 1 { | ||
| return result, fmt.Errorf("max attempts must be >= 1") | ||
| } | ||
| if opts.Detect == nil { | ||
| return result, fmt.Errorf("detect callback is required") | ||
| } | ||
| if opts.Intervene == nil { | ||
| return result, fmt.Errorf("intervene callback is required") | ||
| } | ||
| if opts.Verify == nil { | ||
| return result, fmt.Errorf("verify callback is required") | ||
| } | ||
|
|
||
| for attempt := 1; attempt <= opts.MaxAttempts; attempt++ { | ||
| if err := ctx.Err(); err != nil { | ||
| result.Attempts = attempt - 1 | ||
| return result, fmt.Errorf("rewrite scenario cancelled before attempt %d: %w", attempt, err) | ||
| } | ||
|
|
||
| pass := RewriteAttempt{Attempt: attempt} | ||
|
|
||
| needsIntervention, err := opts.Detect(ctx, attempt) | ||
| if err != nil { | ||
| result.Attempts = attempt | ||
| result.Passes = append(result.Passes, pass) | ||
| return result, fmt.Errorf("detect failed on attempt %d: %w", attempt, err) | ||
| } | ||
| pass.NeedsIntervention = needsIntervention | ||
|
|
||
| if !needsIntervention { | ||
| result.Attempts = attempt | ||
| result.Clean = true | ||
| result.Passes = append(result.Passes, pass) | ||
| return result, nil | ||
| } | ||
|
|
||
| if err := ctx.Err(); err != nil { | ||
| result.Attempts = attempt | ||
| result.Passes = append(result.Passes, pass) | ||
| return result, fmt.Errorf("rewrite scenario cancelled before intervene on attempt %d: %w", attempt, err) | ||
| } | ||
|
|
||
| if err := opts.Intervene(ctx, attempt); err != nil { | ||
| result.Attempts = attempt | ||
| result.Passes = append(result.Passes, pass) | ||
| return result, fmt.Errorf("intervene failed on attempt %d: %w", attempt, err) | ||
| } | ||
| pass.Intervened = true | ||
|
|
||
| if err := ctx.Err(); err != nil { | ||
| result.Attempts = attempt | ||
| result.Passes = append(result.Passes, pass) | ||
| return result, fmt.Errorf("rewrite scenario cancelled before verify on attempt %d: %w", attempt, err) | ||
| } | ||
|
|
||
| clean, err := opts.Verify(ctx, attempt) | ||
| if err != nil { | ||
| result.Attempts = attempt | ||
| result.Passes = append(result.Passes, pass) | ||
| return result, fmt.Errorf("verify failed on attempt %d: %w", attempt, err) | ||
| } | ||
| pass.CleanAfterVerify = clean | ||
| result.Passes = append(result.Passes, pass) | ||
|
|
||
| if clean { | ||
| result.Attempts = attempt | ||
| result.Clean = true | ||
| return result, nil | ||
| } | ||
|
|
||
| if err := ctx.Err(); err != nil { | ||
| result.Attempts = attempt | ||
| return result, fmt.Errorf("rewrite scenario cancelled after verify on attempt %d: %w", attempt, err) | ||
| } | ||
| } | ||
|
|
||
| result.Attempts = opts.MaxAttempts | ||
| return result, fmt.Errorf("%w: attempts=%d", ErrRewriteAttemptsExceeded, opts.MaxAttempts) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.