feat(interactive): fresh-worktree-on-demand + ad-hoc launch specs

BuildForTaskAsync now creates a worktree on demand (via WorktreeManager.CreateAsync,
the same path TaskRunner uses) when a task has a configured working dir but no
Active/Kept worktree, returning a fresh-start spec -- so never-run tasks can be
opened interactively. Adds BuildForDirectoryAsync + GetAdHocLaunchSpec hub/client
for ad-hoc sessions in an arbitrary directory (no task, no worktree, no skill seeding).
This commit is contained in:
mika kuns
2026-07-23 16:47:15 +02:00
parent 0513265c49
commit 9ab48d7094
8 changed files with 148 additions and 11 deletions
@@ -1,9 +1,11 @@
using ClaudeDo.Data.Git;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Config;
using ClaudeDo.Worker.Runner;
using ClaudeDo.Worker.Skills;
using ClaudeDo.Worker.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
namespace ClaudeDo.Worker.Tests.Runner;
@@ -21,6 +23,9 @@ public sealed class InteractiveLaunchSpecServiceTests : IDisposable
private readonly string _claudeStubPath;
private readonly FakeSessionSkillSeeder _seeder = new();
private readonly FakeSessionSkillRegistry _registry = new();
private readonly List<GitRepoFixture> _gitFixtures = new();
private static bool GitAvailable => GitRepoFixture.IsGitAvailable();
public InteractiveLaunchSpecServiceTests()
{
@@ -34,9 +39,17 @@ public sealed class InteractiveLaunchSpecServiceTests : IDisposable
File.WriteAllText(_claudeStubPath, "stub");
}
private GitRepoFixture CreateRepo()
{
var f = new GitRepoFixture();
_gitFixtures.Add(f);
return f;
}
public void Dispose()
{
_db.Dispose();
foreach (var f in _gitFixtures) f.Dispose();
try { Directory.Delete(_tempDir, true); } catch { /* best effort */ }
}
@@ -52,15 +65,17 @@ public sealed class InteractiveLaunchSpecServiceTests : IDisposable
}
private InteractiveLaunchSpecService BuildService() =>
new(_db.CreateFactory(), _seeder, _registry, new WorkerConfig { ClaudeBin = _claudeStubPath });
new(_db.CreateFactory(), _seeder, _registry,
new WorktreeManager(new GitService(), _db.CreateFactory(), new WorkerConfig(), NullLogger<WorktreeManager>.Instance),
new WorkerConfig { ClaudeBin = _claudeStubPath });
private async Task<string> SeedListAsync()
private async Task<string> SeedListAsync(string? workingDir = null)
{
var listId = Guid.NewGuid().ToString();
using var ctx = _db.CreateContext();
await new ListRepository(ctx).AddAsync(new ListEntity
{
Id = listId, Name = "L", WorkingDir = _tempDir, CreatedAt = DateTime.UtcNow,
Id = listId, Name = "L", WorkingDir = workingDir ?? _tempDir, CreatedAt = DateTime.UtcNow,
});
return listId;
}
@@ -122,7 +137,7 @@ public sealed class InteractiveLaunchSpecServiceTests : IDisposable
}
[Fact]
public async Task BuildForTaskAsync_NoWorktreeRow_ThrowsInvalidOperation()
public async Task BuildForTaskAsync_NoWorktreeRow_NotAGitRepo_ThrowsInvalidOperation()
{
var listId = await SeedListAsync();
var taskId = Guid.NewGuid().ToString();
@@ -133,6 +148,43 @@ public sealed class InteractiveLaunchSpecServiceTests : IDisposable
() => svc.BuildForTaskAsync(taskId, CancellationToken.None));
}
[Fact]
public async Task BuildForTaskAsync_NoWorktreeRow_NoWorkingDirConfigured_ThrowsInvalidOperation()
{
var listId = await SeedListAsync(workingDir: null);
var taskId = Guid.NewGuid().ToString();
await SeedTaskAsync(taskId, listId, TaskStatus.Idle);
var svc = BuildService();
await Assert.ThrowsAsync<InvalidOperationException>(
() => svc.BuildForTaskAsync(taskId, CancellationToken.None));
}
[Fact]
public async Task BuildForTaskAsync_FreshTask_NoWorktreeButGitRepoConfigured_CreatesWorktreeOnDemand()
{
if (!GitAvailable) { Assert.True(true, "git not available -- skipping"); return; }
var repo = CreateRepo();
var listId = await SeedListAsync(workingDir: repo.RepoDir);
var taskId = Guid.NewGuid().ToString();
await SeedTaskAsync(taskId, listId, TaskStatus.Idle);
// No worktree row seeded -- task has never run.
var svc = BuildService();
var spec = await svc.BuildForTaskAsync(taskId, CancellationToken.None);
using var readCtx = _db.CreateContext();
var wtRow = await new WorktreeRepository(readCtx).GetByTaskIdAsync(taskId);
Assert.NotNull(wtRow);
Assert.Equal(WorktreeState.Active, wtRow!.State);
Assert.True(Directory.Exists(wtRow.Path));
Assert.Equal(wtRow.Path, spec.Cwd);
Assert.Empty(spec.Args);
Assert.Equal(_claudeStubPath, spec.Exe);
}
[Theory]
[InlineData(WorktreeState.Merged)]
[InlineData(WorktreeState.Discarded)]
@@ -249,4 +301,25 @@ public sealed class InteractiveLaunchSpecServiceTests : IDisposable
Assert.True(call.IsWorktree);
Assert.Equal(new[] { "installed-skill" }, call.SkillNames);
}
[Fact]
public async Task BuildForDirectoryAsync_ExistingDirectory_ReturnsFreshStartSpec()
{
var svc = BuildService();
var spec = await svc.BuildForDirectoryAsync(_tempDir, CancellationToken.None);
Assert.Equal(_tempDir, spec.Cwd);
Assert.Equal(_claudeStubPath, spec.Exe);
Assert.Empty(spec.Args);
Assert.Equal("200000", spec.Env["MCP_TOOL_TIMEOUT"]);
Assert.Empty(_seeder.Calls);
}
[Fact]
public async Task BuildForDirectoryAsync_NonExistentDirectory_ThrowsInvalidOperation()
{
var svc = BuildService();
await Assert.ThrowsAsync<InvalidOperationException>(
() => svc.BuildForDirectoryAsync(Path.Combine(_tempDir, "does-not-exist"), CancellationToken.None));
}
}
@@ -79,6 +79,8 @@ sealed class FakeWorkerClient : IWorkerClient
public Task ResumeTaskInTerminalAsync(string taskId, CancellationToken ct = default) { PickUpInTerminalCalls++; LastPickUpTaskId = taskId; return Task.CompletedTask; }
public Task<LaunchSpec> GetInteractiveLaunchSpecAsync(string taskId, CancellationToken ct = default)
=> Task.FromResult(new LaunchSpec(".", "claude", Array.Empty<string>(), new Dictionary<string, string>()));
public Task<LaunchSpec> GetAdHocLaunchSpecAsync(string directory, CancellationToken ct = default)
=> Task.FromResult(new LaunchSpec(directory, "claude", Array.Empty<string>(), new Dictionary<string, string>()));
public Task QueuePlanningSubtasksAsync(string parentTaskId, CancellationToken ct = default) => Task.CompletedTask;
public Task ResumePlanningSessionAsync(string taskId, CancellationToken ct = default) { ResumePlanningCalls++; return Task.CompletedTask; }
public Task<DiscardPlanningOutcome> DiscardPlanningSessionAsync(string taskId, bool dequeueQueuedChildren = false, CancellationToken ct = default)