feat(worktree): base improvement-child worktree on parent HEAD

This commit is contained in:
mika kuns
2026-06-04 15:46:44 +02:00
parent c10f564265
commit da23b6cd3a
2 changed files with 90 additions and 1 deletions

View File

@@ -32,7 +32,7 @@ public sealed class WorktreeManager
if (!await _git.IsGitRepoAsync(workingDir, ct)) if (!await _git.IsGitRepoAsync(workingDir, ct))
throw new InvalidOperationException($"working_dir is not a git repository: {workingDir}"); throw new InvalidOperationException($"working_dir is not a git repository: {workingDir}");
var baseCommit = await _git.RevParseHeadAsync(workingDir, ct); var baseCommit = await ResolveBaseCommitAsync(task, workingDir, ct);
// Use the full task id (dashes stripped) in the branch name so // Use the full task id (dashes stripped) in the branch name so
// two GUIDs sharing an 8-char prefix cannot collide on the same branch. // two GUIDs sharing an 8-char prefix cannot collide on the same branch.
var idForBranch = task.Id.Replace("-", ""); var idForBranch = task.Id.Replace("-", "");
@@ -184,4 +184,25 @@ public sealed class WorktreeManager
_logger.LogInformation("Discarded worktree for task {TaskId} (branch {Branch})", wt.TaskId, wt.BranchName); _logger.LogInformation("Discarded worktree for task {TaskId} (branch {Branch})", wt.TaskId, wt.BranchName);
} }
// Improvement children (parent is a non-planning task with its own worktree) branch
// from the parent's recorded HEAD so they build on the parent's not-yet-merged work.
// Planning children and standalone tasks base off the list's current HEAD.
private async Task<string> ResolveBaseCommitAsync(TaskEntity task, string workingDir, CancellationToken ct)
{
if (task.ParentTaskId is not null)
{
using var ctx = _dbFactory.CreateDbContext();
var parent = await ctx.Tasks.AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == task.ParentTaskId, ct);
if (parent is not null && parent.PlanningPhase == PlanningPhase.None)
{
var parentWt = await new WorktreeRepository(ctx).GetByTaskIdAsync(task.ParentTaskId, ct);
var parentHead = parentWt?.HeadCommit ?? parentWt?.BaseCommit;
if (parentHead is not null)
return parentHead;
}
}
return await _git.RevParseHeadAsync(workingDir, ct);
}
} }

View File

@@ -0,0 +1,68 @@
using ClaudeDo.Data.Git;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Config;
using ClaudeDo.Worker.Runner;
using ClaudeDo.Worker.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
using Xunit;
namespace ClaudeDo.Worker.Tests.Runner;
public sealed class ChildWorktreeBaseTests : IDisposable
{
private readonly List<GitRepoFixture> _repos = new();
private readonly List<DbFixture> _dbs = new();
private readonly List<(string repoDir, string wtPath)> _cleanups = new();
[Fact]
public async Task ImprovementChild_basesOff_parentWorktreeHead()
{
if (!GitRepoFixture.IsGitAvailable()) { Assert.True(true, "git not available -- skipping"); return; }
var repo = new GitRepoFixture(); _repos.Add(repo);
var db = new DbFixture(); _dbs.Add(db);
var listId = Guid.NewGuid().ToString();
var parentId = Guid.NewGuid().ToString();
var childId = Guid.NewGuid().ToString();
var list = new ListEntity { Id = listId, Name = "L", WorkingDir = repo.RepoDir, CreatedAt = DateTime.UtcNow };
var parent = new TaskEntity { Id = parentId, ListId = listId, Title = "Parent",
CommitType = "chore", PlanningPhase = PlanningPhase.None, CreatedAt = DateTime.UtcNow };
var child = new TaskEntity { Id = childId, ListId = listId, Title = "Improve",
CommitType = "chore", ParentTaskId = parentId, CreatedBy = parentId, CreatedAt = DateTime.UtcNow };
using (var seed = db.CreateContext())
{
await new ListRepository(seed).AddAsync(list);
await new TaskRepository(seed).AddAsync(parent);
await new TaskRepository(seed).AddAsync(child);
}
var cfg = new WorkerConfig { WorktreeRootStrategy = "sibling" };
var mgr = new WorktreeManager(new GitService(), db.CreateFactory(), cfg, NullLogger<WorktreeManager>.Instance);
var parentCtx = await mgr.CreateAsync(parent, list, CancellationToken.None);
_cleanups.Add((repo.RepoDir, parentCtx.WorktreePath));
File.WriteAllText(Path.Combine(parentCtx.WorktreePath, "parent.txt"), "parent work");
await mgr.CommitIfChangedAsync(parentCtx, parent, list, CancellationToken.None);
string parentHead;
using (var read = db.CreateContext())
parentHead = (await new WorktreeRepository(read).GetByTaskIdAsync(parentId))!.HeadCommit!;
var childCtx = await mgr.CreateAsync(child, list, CancellationToken.None);
_cleanups.Add((repo.RepoDir, childCtx.WorktreePath));
Assert.Equal(parentHead, childCtx.BaseCommit);
Assert.NotEqual(repo.BaseCommit, childCtx.BaseCommit);
Assert.True(File.Exists(Path.Combine(childCtx.WorktreePath, "parent.txt")));
}
public void Dispose()
{
foreach (var (repoDir, wtPath) in _cleanups)
try { GitRepoFixture.RunGit(repoDir, "worktree", "remove", "--force", wtPath); } catch { }
foreach (var r in _repos) r.Dispose();
foreach (var d in _dbs) d.Dispose();
}
}