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:
@@ -90,6 +90,9 @@ public interface IWorkerClient : INotifyPropertyChanged
|
||||
/// <summary>Launch spec for an embedded ConPTY terminal to open an interactive session
|
||||
/// in a task's worktree (same worktree prep as an autonomous run).</summary>
|
||||
Task<LaunchSpec> GetInteractiveLaunchSpecAsync(string taskId, CancellationToken ct = default);
|
||||
/// <summary>Launch spec for an ad-hoc interactive session in an arbitrary directory --
|
||||
/// no task, no worktree.</summary>
|
||||
Task<LaunchSpec> GetAdHocLaunchSpecAsync(string directory, CancellationToken ct = default);
|
||||
Task ResumePlanningSessionAsync(string taskId, CancellationToken ct = default);
|
||||
Task<DiscardPlanningOutcome> DiscardPlanningSessionAsync(string taskId, bool dequeueQueuedChildren = false, CancellationToken ct = default);
|
||||
Task FinalizePlanningSessionAsync(string taskId, bool queueAgentTasks = true, CancellationToken ct = default);
|
||||
|
||||
@@ -567,6 +567,9 @@ public partial class WorkerClient : ObservableObject, IAsyncDisposable, IWorkerC
|
||||
public async Task<LaunchSpec> GetInteractiveLaunchSpecAsync(string taskId, CancellationToken ct = default)
|
||||
=> await _hub.InvokeAsync<LaunchSpec>("GetInteractiveLaunchSpec", taskId, ct);
|
||||
|
||||
public async Task<LaunchSpec> GetAdHocLaunchSpecAsync(string directory, CancellationToken ct = default)
|
||||
=> await _hub.InvokeAsync<LaunchSpec>("GetAdHocLaunchSpec", directory, ct);
|
||||
|
||||
public async Task<DiscardPlanningOutcome> DiscardPlanningSessionAsync(string taskId, bool dequeueQueuedChildren = false, CancellationToken ct = default)
|
||||
=> await _hub.InvokeAsync<DiscardPlanningOutcome>("DiscardPlanningSessionAsync", taskId, dequeueQueuedChildren, ct);
|
||||
|
||||
|
||||
@@ -671,6 +671,15 @@ public sealed class WorkerHub : Microsoft.AspNetCore.SignalR.Hub
|
||||
return _interactiveLaunchSpec.BuildForTaskAsync(taskId, Context.ConnectionAborted);
|
||||
});
|
||||
|
||||
// Builds the launch spec for an ad-hoc interactive session in an arbitrary directory --
|
||||
// no task, no worktree, no session-skills seeding.
|
||||
public Task<LaunchSpec> GetAdHocLaunchSpec(string directory) => HubGuard(() =>
|
||||
{
|
||||
if (_interactiveLaunchSpec is null)
|
||||
throw new InvalidOperationException("Interactive launch spec service is not configured.");
|
||||
return _interactiveLaunchSpec.BuildForDirectoryAsync(directory, Context.ConnectionAborted);
|
||||
});
|
||||
|
||||
public Task SendInteractiveMessage(string taskId, string text) =>
|
||||
_interactive.SendAsync(taskId, text, Context.ConnectionAborted);
|
||||
|
||||
|
||||
@@ -28,17 +28,20 @@ public sealed class InteractiveLaunchSpecService : IInteractiveLaunchSpecService
|
||||
private readonly IDbContextFactory<ClaudeDoDbContext> _dbFactory;
|
||||
private readonly ISessionSkillSeeder _skillSeeder;
|
||||
private readonly ISessionSkillRegistry _skillRegistry;
|
||||
private readonly WorktreeManager _wtManager;
|
||||
private readonly string _claudePath;
|
||||
|
||||
public InteractiveLaunchSpecService(
|
||||
IDbContextFactory<ClaudeDoDbContext> dbFactory,
|
||||
ISessionSkillSeeder skillSeeder,
|
||||
ISessionSkillRegistry skillRegistry,
|
||||
WorktreeManager wtManager,
|
||||
WorkerConfig cfg)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_skillSeeder = skillSeeder;
|
||||
_skillRegistry = skillRegistry;
|
||||
_wtManager = wtManager;
|
||||
_claudePath = cfg.ClaudeBin;
|
||||
}
|
||||
|
||||
@@ -52,14 +55,32 @@ public sealed class InteractiveLaunchSpecService : IInteractiveLaunchSpecService
|
||||
throw new InvalidOperationException("Can't open an interactive session for a running or queued task -- interrupt it first.");
|
||||
|
||||
var worktree = await new WorktreeRepository(ctx).GetByTaskIdAsync(taskId, ct);
|
||||
var isFreshWorktree = false;
|
||||
|
||||
if (worktree is null || worktree.State is not (WorktreeState.Active or WorktreeState.Kept))
|
||||
throw new InvalidOperationException("This task has no active worktree to open a session in.");
|
||||
if (!Directory.Exists(worktree.Path))
|
||||
{
|
||||
// No usable worktree yet -- if the task's list points at a git repo, create one
|
||||
// on demand via the SAME mechanism an autonomous run uses (WorktreeManager.CreateAsync:
|
||||
// branch naming, base commit resolution, worktree-root strategy, DB registration).
|
||||
var list = await new ListRepository(ctx).GetByIdAsync(task.ListId, ct);
|
||||
if (list?.WorkingDir is null)
|
||||
throw new InvalidOperationException("This task has no working directory configured -- can't create a worktree.");
|
||||
|
||||
await _wtManager.CreateAsync(task, list, ct);
|
||||
worktree = await new WorktreeRepository(ctx).GetByTaskIdAsync(taskId, ct)
|
||||
?? throw new InvalidOperationException("Worktree creation did not persist a worktree row.");
|
||||
isFreshWorktree = true;
|
||||
}
|
||||
else if (!Directory.Exists(worktree.Path))
|
||||
{
|
||||
throw new InvalidOperationException("The task's worktree directory no longer exists.");
|
||||
}
|
||||
|
||||
var listConfig = await new ListRepository(ctx).GetConfigAsync(task.ListId, ct);
|
||||
var globalSettings = await new AppSettingsRepository(ctx).GetAsync(ct);
|
||||
var run = await new TaskRunRepository(ctx).GetLatestByTaskIdAsync(taskId, ct);
|
||||
// A brand-new worktree has no prior session to resume, regardless of any session
|
||||
// history the task accumulated before its previous worktree went away.
|
||||
var run = isFreshWorktree ? null : await new TaskRunRepository(ctx).GetLatestByTaskIdAsync(taskId, ct);
|
||||
|
||||
var requestedSkills = TaskRunner.UnionSkillNames(task.SessionSkills, listConfig?.SessionSkills, globalSettings.SessionSkills);
|
||||
var skillNames = await FilterToInstalledSkillsAsync(requestedSkills, ct);
|
||||
@@ -83,6 +104,22 @@ public sealed class InteractiveLaunchSpecService : IInteractiveLaunchSpecService
|
||||
return new LaunchSpec(worktree.Path, resolvedClaude, args, env);
|
||||
}
|
||||
|
||||
public Task<LaunchSpec> BuildForDirectoryAsync(string directory, CancellationToken ct)
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
throw new InvalidOperationException($"Directory does not exist: {directory}");
|
||||
|
||||
var resolvedClaude = WindowsTerminalLauncher.Resolve(_claudePath)
|
||||
?? throw new InvalidOperationException($"claude executable not found: {_claudePath}");
|
||||
|
||||
var env = new Dictionary<string, string>
|
||||
{
|
||||
["MCP_TOOL_TIMEOUT"] = "200000",
|
||||
};
|
||||
|
||||
return Task.FromResult(new LaunchSpec(directory, resolvedClaude, Array.Empty<string>(), env));
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyList<string>> FilterToInstalledSkillsAsync(IReadOnlyList<string> requested, CancellationToken ct)
|
||||
{
|
||||
if (requested.Count == 0) return requested;
|
||||
|
||||
@@ -2,9 +2,17 @@ namespace ClaudeDo.Worker.Runner;
|
||||
|
||||
public interface IInteractiveLaunchSpecService
|
||||
{
|
||||
/// <summary>Builds a LaunchSpec for opening an interactive session in an existing task's
|
||||
/// worktree. Throws KeyNotFoundException if the task doesn't exist, InvalidOperationException
|
||||
/// if it's Running/Queued or has no usable worktree. A task that has never run (no persisted
|
||||
/// SessionId) is not an error -- the spec omits --resume for a fresh start.</summary>
|
||||
/// <summary>Builds a LaunchSpec for opening an interactive session in a task's worktree.
|
||||
/// Throws KeyNotFoundException if the task doesn't exist, InvalidOperationException
|
||||
/// if it's Running/Queued. If the task has no usable worktree yet, one is created on
|
||||
/// demand (same mechanism as an autonomous run) provided the task's list has a working
|
||||
/// directory pointing at a git repo -- otherwise throws InvalidOperationException. A task
|
||||
/// that has never run, or whose worktree was just created fresh, gets a fresh-start spec
|
||||
/// (no --resume); an existing worktree with a persisted SessionId gets --resume.</summary>
|
||||
Task<LaunchSpec> BuildForTaskAsync(string taskId, CancellationToken ct);
|
||||
|
||||
/// <summary>Builds a LaunchSpec for an ad-hoc interactive session in an arbitrary directory --
|
||||
/// no task, no worktree, no session-skills seeding. Throws InvalidOperationException if the
|
||||
/// directory doesn't exist.</summary>
|
||||
Task<LaunchSpec> BuildForDirectoryAsync(string directory, CancellationToken ct);
|
||||
}
|
||||
|
||||
@@ -107,6 +107,8 @@ public abstract class StubWorkerClient : IWorkerClient
|
||||
public virtual Task ResumeTaskInTerminalAsync(string taskId, CancellationToken ct = default) => Task.CompletedTask;
|
||||
public virtual Task<LaunchSpec> GetInteractiveLaunchSpecAsync(string taskId, CancellationToken ct = default)
|
||||
=> Task.FromResult(new LaunchSpec(".", "claude", Array.Empty<string>(), new Dictionary<string, string>()));
|
||||
public virtual Task<LaunchSpec> GetAdHocLaunchSpecAsync(string directory, CancellationToken ct = default)
|
||||
=> Task.FromResult(new LaunchSpec(directory, "claude", Array.Empty<string>(), new Dictionary<string, string>()));
|
||||
public virtual Task ResumePlanningSessionAsync(string taskId, CancellationToken ct = default) => Task.CompletedTask;
|
||||
public virtual Task<DiscardPlanningOutcome> DiscardPlanningSessionAsync(string taskId, bool dequeueQueuedChildren = false, CancellationToken ct = default)
|
||||
=> Task.FromResult(new DiscardPlanningOutcome(DiscardPlanningResult.Discarded, 0, 0));
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user