feat(worker): add WorktreeManager.DiscardAsync for task reset

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mika Kuns
2026-04-21 17:21:08 +02:00
parent 133774cb86
commit 44203f3c67
2 changed files with 54 additions and 0 deletions

View File

@@ -140,4 +140,32 @@ public sealed class WorktreeManager
_logger.LogInformation("Committed changes for task {TaskId}: {Head}", task.Id, head);
return true;
}
public async Task DiscardAsync(WorktreeEntity wt, string workingDir, CancellationToken ct)
{
try
{
await _git.WorktreeRemoveAsync(workingDir, wt.Path, force: true, ct);
}
catch (Exception ex)
{
_logger.LogError(ex, "git worktree remove failed for {Path}", wt.Path);
throw;
}
try
{
await _git.BranchDeleteAsync(workingDir, wt.BranchName, force: true, ct);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "git branch -D {Branch} failed after worktree removal; continuing", wt.BranchName);
}
using var context = _dbFactory.CreateDbContext();
var wtRepo = new WorktreeRepository(context);
await wtRepo.SetStateAsync(wt.TaskId, WorktreeState.Discarded, ct);
_logger.LogInformation("Discarded worktree for task {TaskId} (branch {Branch})", wt.TaskId, wt.BranchName);
}
}