Merge branch 'worktree-phase1-reaktivitaet'

This commit is contained in:
mika kuns
2026-08-07 11:01:54 +02:00
13 changed files with 575 additions and 61 deletions
@@ -1,6 +1,7 @@
using ClaudeDo.Data;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Online;
using ClaudeDo.Worker.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
@@ -55,7 +56,8 @@ public sealed class OnlineSyncServiceTests : IDisposable
}
}
private OnlineSyncService BuildService(FakeApi api, string? token = "test-token", bool enabled = true)
private OnlineSyncService BuildService(
FakeApi api, string? token = "test-token", bool enabled = true, HubBroadcaster? broadcaster = null)
{
var config = new OnlineInboxConfig { Enabled = enabled, PollIntervalSeconds = 60 };
var auth = new StaticTokenAuthProvider(token);
@@ -64,7 +66,8 @@ public sealed class OnlineSyncServiceTests : IDisposable
api,
auth,
config,
NullLogger<OnlineSyncService>.Instance);
NullLogger<OnlineSyncService>.Instance,
broadcaster ?? new HubBroadcaster(new CapturingHubContext()));
}
private async Task<(string ListId, ClaudeDoDbContext Ctx, TaskRepository Tasks, ListRepository Lists)> SeedAsync()
@@ -103,6 +106,26 @@ public sealed class OnlineSyncServiceTests : IDisposable
Assert.Contains(remoteId, api.MarkedImported);
}
[Fact]
public async Task Tick_Imports_RemoteTask_BroadcastsTaskUpdated()
{
var (listId, ctx, _, _) = await SeedAsync();
using var _ = ctx;
var remoteId = Guid.NewGuid().ToString();
var api = new FakeApi
{
UnimportedTasks = [new RemoteTask(remoteId, listId, "From Web", "desc", DateTimeOffset.UtcNow)],
};
var hubContext = new CapturingHubContext();
var svc = BuildService(api, broadcaster: new HubBroadcaster(hubContext));
await svc.TickAsync(CancellationToken.None);
Assert.Contains(hubContext.Proxy.Calls,
c => c.Method == "TaskUpdated" && (string)c.Args[0]! == remoteId);
}
[Fact]
public async Task Tick_UnknownList_Skips_And_DoesNotMark()
{
@@ -121,4 +121,52 @@ public sealed class QueueClaimTaskUpdatedBroadcastTests : IDisposable
releaseProcess.TrySetResult();
await runTask;
}
[Fact]
public async Task Creating_a_worktree_broadcasts_WorktreeUpdated()
{
string listId = Guid.NewGuid().ToString(), taskId = Guid.NewGuid().ToString();
var repoDir = Path.Combine(_tempDir, "repo");
Directory.CreateDirectory(repoDir);
// A real git repo — Worker.Tests run real git by design.
await RunGitAsync(repoDir, "init");
await RunGitAsync(repoDir, "config user.email t@t.t");
await RunGitAsync(repoDir, "config user.name t");
await File.WriteAllTextAsync(Path.Combine(repoDir, "a.txt"), "hi");
await RunGitAsync(repoDir, "add a.txt");
await RunGitAsync(repoDir, "commit -m init");
using (var ctx = _db.CreateContext())
{
ctx.Lists.Add(new ListEntity { Id = listId, Name = "L", WorkingDir = repoDir, CreatedAt = DateTime.UtcNow });
ctx.Tasks.Add(new TaskEntity
{
Id = taskId, ListId = listId, Title = "T", Status = TaskStatus.Running,
StartedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
});
await ctx.SaveChangesAsync();
}
var fake = new FakeClaudeProcess((_, _, _, _, _) =>
Task.FromResult(new RunResult { ExitCode = 0, ResultMarkdown = "ok" }));
var runner = BuildRunner(fake);
using (var ctx = _db.CreateContext())
await runner.RunAsync((await new TaskRepository(ctx).GetByIdAsync(taskId))!, "queue",
CancellationToken.None, alreadyClaimed: true);
Assert.Contains(_hubContext.Proxy.Calls,
c => c.Method == "WorktreeUpdated" && (string)c.Args[0]! == taskId);
}
private static async Task RunGitAsync(string dir, string args)
{
var psi = new System.Diagnostics.ProcessStartInfo("git", args)
{
WorkingDirectory = dir, RedirectStandardOutput = true, RedirectStandardError = true,
};
using var p = System.Diagnostics.Process.Start(psi)!;
await p.WaitForExitAsync();
}
}
@@ -0,0 +1,223 @@
using ClaudeDo.Data;
using ClaudeDo.Data.Git;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Config;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Queue;
using ClaudeDo.Worker.Runner;
using ClaudeDo.Worker.Tests.Infrastructure;
using ClaudeDo.Worker.Usage;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
namespace ClaudeDo.Worker.Tests.Services;
// The queue picker's raw-SQL claim commits status='running' before the runner starts. If
// anything then throws before the runner's own terminal-status write, the task used to stay
// Running forever with the UI never notified (RunInSlotAsync's catch only logged the error).
// It must now mark the task Failed for a real exception (which broadcasts TaskUpdated), but
// must NOT do so for a cancellation — the cancel path already wrote the terminal status.
//
// These drive the real QueueService end to end (StartAsync + the waker), not just the
// FailAsync contract, so they actually exercise the fixed catch block.
public sealed class QueueServiceSlotFailureTests : IDisposable
{
private readonly DbFixture _db = new();
private readonly string _tempDir;
private readonly WorkerConfig _cfg;
public QueueServiceSlotFailureTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), $"claudedo_slotfail_{Guid.NewGuid():N}");
Directory.CreateDirectory(_tempDir);
_cfg = new WorkerConfig
{
SandboxRoot = Path.Combine(_tempDir, "sandbox"),
LogRoot = Path.Combine(_tempDir, "logs"),
QueueBackstopIntervalMs = 50, // fast for tests
};
}
public void Dispose()
{
_db.Dispose();
try { Directory.Delete(_tempDir, true); } catch { }
}
// Mirrors QueueServiceTests.CreateService but takes the picker as a parameter so each test
// can engineer the exact failure path it needs to exercise.
// Build() wires its own CapturingHubContext internally and hands it back as .Hub — the
// broadcaster inside TaskStateService (and therefore FailAsync's TaskUpdated) uses that
// exact instance, so everything else here must share it too rather than constructing a
// second CapturingHubContext that would silently miss FailAsync's broadcast.
private (QueueService service, CapturingHubContext hub, QueueWaker waker) CreateService(IQueuePicker picker)
{
var dbFactory = _db.CreateFactory();
var built = TaskStateServiceBuilder.Build(dbFactory);
var broadcaster = new HubBroadcaster(built.Hub);
var wtManager = new WorktreeManager(new GitService(), dbFactory, _cfg, NullLogger<WorktreeManager>.Instance);
var argsBuilder = new ClaudeArgsBuilder();
var runner = new TaskRunner(new FakeClaudeProcess(), dbFactory, broadcaster, wtManager, argsBuilder, _cfg,
NullLogger<TaskRunner>.Instance, built.State, new TaskRunTokenRegistry(), new AttachmentStore(),
new FakeSessionSkillSeeder(), new FakeTranscriptUsageReader());
var waker = new QueueWaker();
var overrideSlot = new OverrideSlotService(dbFactory, runner, NullLogger<OverrideSlotService>.Instance, built.RunCancels);
var service = new QueueService(dbFactory, runner, _cfg, NullLogger<QueueService>.Instance, waker, picker,
overrideSlot, built.State, built.RunCancels, new FakeUsageGate(), new UsageState(), broadcaster);
return (service, built.Hub, waker);
}
private async Task<string> SeedListAsync()
{
var listId = Guid.NewGuid().ToString();
using var ctx = _db.CreateContext();
ctx.Lists.Add(new ListEntity { Id = listId, Name = "L", CreatedAt = DateTime.UtcNow });
await ctx.SaveChangesAsync();
return listId;
}
// Directly rewrites the task's list_id via a raw connection with FK enforcement off,
// reproducing "the list vanished between the queue claim and the run" without going
// through EF's foreign-key-checked connections (which would reject the write).
private void OrphanTaskListId(string taskId)
{
using var conn = new SqliteConnection($"Data Source={_db.DbPath}");
conn.Open();
using (var pragmaCmd = conn.CreateCommand())
{
pragmaCmd.CommandText = "PRAGMA foreign_keys=OFF;";
pragmaCmd.ExecuteNonQuery();
}
using var cmd = conn.CreateCommand();
cmd.CommandText = "UPDATE tasks SET list_id = 'orphaned-missing-list' WHERE id = $id;";
cmd.Parameters.AddWithValue("$id", taskId);
cmd.ExecuteNonQuery();
}
[Fact]
public async Task A_throwing_slot_run_marks_the_task_Failed_and_broadcasts_TaskUpdated()
{
var listId = await SeedListAsync();
var taskId = Guid.NewGuid().ToString();
using (var ctx = _db.CreateContext())
{
ctx.Tasks.Add(new TaskEntity
{
Id = taskId, ListId = listId, Title = "T", Status = TaskStatus.Queued,
ReviewFeedback = "please fix", CreatedAt = DateTime.UtcNow,
});
await ctx.SaveChangesAsync();
// A prior run with a session id routes RunInSlotAsync into TaskRunner.ContinueAsync
// instead of RunAsync.
await new TaskRunRepository(ctx).AddAsync(new TaskRunEntity
{
Id = Guid.NewGuid().ToString(), TaskId = taskId, RunNumber = 1, IsRetry = false,
Prompt = "original", SessionId = "sess-1", StartedAt = DateTime.UtcNow.AddMinutes(-5),
});
}
// ContinueAsync's setup block reads the list *before* its own try/catch starts
// (TaskRunner.cs, ContinueAsync ~line 232-234) and throws InvalidOperationException
// ("List not found.") straight past TaskRunner's own protection. That's the exact gap
// QueueService.RunInSlotAsync's own catch now has to cover.
OrphanTaskListId(taskId);
var (service, hub, waker) = CreateService(new QueuePicker(_db.CreateFactory()));
using var cts = new CancellationTokenSource();
await service.StartAsync(cts.Token);
waker.Wake();
// FailAsync (TaskStateService.cs:236-249) commits the DB status flip via
// ExecuteUpdateAsync *before* it calls the broadcaster's TaskUpdated — so a poll that
// breaks the instant it observes Status==Failed can race ahead of the broadcast still
// landing in hub.Proxy.Calls. Wait for both signals together so the assertions below
// never sample a genuinely-not-yet-broadcast window as a failure.
TaskEntity? reloaded = null;
var deadline = DateTime.UtcNow.AddSeconds(10);
while (DateTime.UtcNow < deadline)
{
using var verify = _db.CreateContext();
reloaded = await new TaskRepository(verify).GetByIdAsync(taskId);
var broadcastSeen = hub.Proxy.Calls.Any(
c => c.Method == "TaskUpdated" && (string)c.Args[0]! == taskId);
if (reloaded!.Status == TaskStatus.Failed && broadcastSeen) break;
await Task.Delay(25);
}
cts.Cancel();
Assert.Equal(TaskStatus.Failed, reloaded!.Status);
Assert.Contains(hub.Proxy.Calls,
c => c.Method == "TaskUpdated" && (string)c.Args[0]! == taskId);
}
// A fake IQueuePicker that performs the real atomic claim (so the DB row transitions
// Queued->Running exactly like production) and then, synchronously before returning,
// cancels the token QueueService's per-slot CTS is linked from. By the time
// QueueService.ExecuteAsync creates that linked CTS and dispatches RunInSlotAsync, the
// token is already cancelled — deterministic, no timing race required.
private sealed class ClaimThenCancelPicker : IQueuePicker
{
private readonly IQueuePicker _inner;
private readonly CancellationTokenSource _cancelAfterClaim;
public ClaimThenCancelPicker(IQueuePicker inner, CancellationTokenSource cancelAfterClaim)
{
_inner = inner;
_cancelAfterClaim = cancelAfterClaim;
}
public async Task<TaskEntity?> ClaimNextAsync(DateTime now, CancellationToken ct)
{
var claimed = await _inner.ClaimNextAsync(now, ct);
if (claimed is not null) _cancelAfterClaim.Cancel();
return claimed;
}
}
[Fact]
public async Task A_cancelled_slot_run_does_not_mark_the_task_Failed()
{
var listId = await SeedListAsync();
var taskId = Guid.NewGuid().ToString();
using (var ctx = _db.CreateContext())
{
ctx.Tasks.Add(new TaskEntity
{
Id = taskId, ListId = listId, Title = "T", Status = TaskStatus.Queued,
CreatedAt = DateTime.UtcNow,
});
await ctx.SaveChangesAsync();
}
var outerCts = new CancellationTokenSource();
var realPicker = new QueuePicker(_db.CreateFactory());
var picker = new ClaimThenCancelPicker(realPicker, outerCts);
var (service, hub, waker) = CreateService(picker);
await service.StartAsync(outerCts.Token);
waker.Wake();
// Wait for the slot to be claimed and then released again (RunInSlotAsync's
// ContinueWith removes it once the catch block — ours or a stray one — finishes).
var deadline = DateTime.UtcNow.AddSeconds(10);
while (service.GetActive().Any(a => a.taskId == taskId) && DateTime.UtcNow < deadline)
await Task.Delay(25);
await Task.Delay(100); // let the fire-and-forget continuation fully settle
TaskEntity? reloaded;
using (var verify = _db.CreateContext())
reloaded = await new TaskRepository(verify).GetByIdAsync(taskId);
// The picker's atomic claim already flipped it to Running; the cancelled slot run must
// leave it there rather than flipping it to Failed.
Assert.Equal(TaskStatus.Running, reloaded!.Status);
Assert.DoesNotContain(hub.Proxy.Calls,
c => c.Method == "TaskUpdated" && (string)c.Args[0]! == taskId);
}
}