Adds a user/MCP-declared task dependency (DependsOnTaskId) distinct from the planning chain's internal BlockedByTaskId: add_task/update_task can set it, the queue picker skips a Queued task until the dependency reaches Done, a Failed/Cancelled dependency leaves the dependent blocked instead of starving silently, and setting a link rejects self-reference/unknown-id/cycles. get_task/list_tasks/batch_get_tasks now report blocked/blockedReason, and wait_for_task_change reports "Blocked" immediately instead of running out its timeout on a task the picker will never claim. preview_merge/preview_merge_set gain staleFiles: files a branch touches that the target branch also changed since the branch's fork point, a more honest staleness signal than `behind` alone.
264 lines
9.9 KiB
C#
264 lines
9.9 KiB
C#
using System.Diagnostics;
|
|
using ClaudeDo.Data;
|
|
using ClaudeDo.Data.Models;
|
|
using ClaudeDo.Data.Repositories;
|
|
using ClaudeDo.Worker.External;
|
|
using ClaudeDo.Worker.Tests.Infrastructure;
|
|
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
|
|
|
namespace ClaudeDo.Worker.Tests.External;
|
|
|
|
public sealed class TaskWaitMcpToolsTests : IDisposable
|
|
{
|
|
private readonly DbFixture _db = new();
|
|
private readonly ClaudeDoDbContext _ctx;
|
|
private readonly TaskRepository _tasks;
|
|
private readonly ListRepository _lists;
|
|
|
|
public TaskWaitMcpToolsTests()
|
|
{
|
|
_ctx = _db.CreateContext();
|
|
_tasks = new TaskRepository(_ctx);
|
|
_lists = new ListRepository(_ctx);
|
|
}
|
|
|
|
public void Dispose() { _ctx.Dispose(); _db.Dispose(); }
|
|
|
|
private TaskWaitMcpTools BuildSut() => new(_db.CreateFactory());
|
|
|
|
private async Task<TaskEntity> SeedTaskAsync(TaskStatus status)
|
|
{
|
|
var listId = Guid.NewGuid().ToString();
|
|
await _lists.AddAsync(new ListEntity { Id = listId, Name = "L", CreatedAt = DateTime.UtcNow });
|
|
var task = new TaskEntity
|
|
{
|
|
Id = Guid.NewGuid().ToString(), ListId = listId, Title = "t",
|
|
Status = status, CreatedAt = DateTime.UtcNow, CommitType = "chore",
|
|
};
|
|
await _tasks.AddAsync(task);
|
|
return task;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_AlreadyOutOfQueuedRunning_ReturnsImmediately()
|
|
{
|
|
var task = await SeedTaskAsync(TaskStatus.WaitingForReview);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange([task.Id], timeoutSeconds: 30, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.False(result.TimedOut);
|
|
Assert.Equal(task.Id, Assert.Single(result.Changed).TaskId);
|
|
Assert.Equal("WaitingForReview", result.Changed[0].Status);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(2), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_UnknownId_ReturnsImmediatelyAsNotFound()
|
|
{
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange(["missing-id"], timeoutSeconds: 30, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.False(result.TimedOut);
|
|
Assert.Equal("NotFound", Assert.Single(result.Changed).Status);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(2), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_StatusChangesWhileWaiting_ReturnsBeforeTimeout()
|
|
{
|
|
var task = await SeedTaskAsync(TaskStatus.Running);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var waitTask = sut.WaitForTaskChange([task.Id], timeoutSeconds: 10, cancellationToken: CancellationToken.None);
|
|
|
|
await Task.Delay(150);
|
|
// Simulate the status change a broadcast would announce, via a separate context
|
|
// (mirrors what TaskStateService does from a different scope/process).
|
|
await using (var writeCtx = _db.CreateContext())
|
|
{
|
|
var writeRepo = new TaskRepository(writeCtx);
|
|
var loaded = await writeRepo.GetByIdAsync(task.Id);
|
|
loaded!.Status = TaskStatus.Done;
|
|
await writeRepo.UpdateAsync(loaded);
|
|
}
|
|
|
|
var result = await waitTask;
|
|
sw.Stop();
|
|
|
|
Assert.False(result.TimedOut);
|
|
Assert.Equal("Done", Assert.Single(result.Changed).Status);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(5), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_NoChange_TimesOut()
|
|
{
|
|
var task = await SeedTaskAsync(TaskStatus.Running);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange([task.Id], timeoutSeconds: 1, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.True(result.TimedOut);
|
|
Assert.Empty(result.Changed);
|
|
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(900), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_EmptyTaskIds_Throws()
|
|
{
|
|
var sut = BuildSut();
|
|
await Assert.ThrowsAsync<ArgumentException>(() =>
|
|
sut.WaitForTaskChange([], timeoutSeconds: 5, cancellationToken: CancellationToken.None));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_WaitingForChildren_DefaultBehavior_ReturnsImmediately()
|
|
{
|
|
var task = await SeedTaskAsync(TaskStatus.WaitingForChildren);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange([task.Id], timeoutSeconds: 30, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.False(result.TimedOut);
|
|
Assert.Equal("WaitingForChildren", Assert.Single(result.Changed).Status);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(2), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_TreatWaitingForChildrenAsBusy_DoesNotReturnImmediately_TimesOut()
|
|
{
|
|
var task = await SeedTaskAsync(TaskStatus.WaitingForChildren);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange(
|
|
[task.Id], timeoutSeconds: 1, treatWaitingForChildrenAsBusy: true, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.True(result.TimedOut);
|
|
Assert.Empty(result.Changed);
|
|
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(900), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_TreatWaitingForChildrenAsBusy_ReturnsWhenParentReachesWaitingForReview()
|
|
{
|
|
var task = await SeedTaskAsync(TaskStatus.WaitingForChildren);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var waitTask = sut.WaitForTaskChange(
|
|
[task.Id], timeoutSeconds: 30, treatWaitingForChildrenAsBusy: true, cancellationToken: CancellationToken.None);
|
|
|
|
await Task.Delay(150);
|
|
await using (var writeCtx = _db.CreateContext())
|
|
{
|
|
var writeRepo = new TaskRepository(writeCtx);
|
|
var loaded = await writeRepo.GetByIdAsync(task.Id);
|
|
loaded!.Status = TaskStatus.WaitingForReview;
|
|
await writeRepo.UpdateAsync(loaded);
|
|
}
|
|
|
|
var result = await waitTask;
|
|
sw.Stop();
|
|
|
|
// Bound is generous (well under timeoutSeconds) purely to prove this didn't just
|
|
// coincidentally land on the timeout path -- correctness is already covered by
|
|
// Assert.False(result.TimedOut) above; this is not a performance assertion.
|
|
Assert.False(result.TimedOut);
|
|
Assert.Equal("WaitingForReview", Assert.Single(result.Changed).Status);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(25), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_TreatWaitingForChildrenAsBusy_UnknownId_StillReturnsImmediatelyAsNotFound()
|
|
{
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange(
|
|
["missing-id"], timeoutSeconds: 30, treatWaitingForChildrenAsBusy: true, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.False(result.TimedOut);
|
|
Assert.Equal("NotFound", Assert.Single(result.Changed).Status);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(2), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_QueuedWithBlockedByTaskId_ReportsBlockedImmediately()
|
|
{
|
|
var predecessor = await SeedTaskAsync(TaskStatus.Queued);
|
|
var task = await SeedTaskAsync(TaskStatus.Queued);
|
|
task.BlockedByTaskId = predecessor.Id;
|
|
await _tasks.UpdateAsync(task);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange([task.Id], timeoutSeconds: 30, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.False(result.TimedOut);
|
|
var change = Assert.Single(result.Changed);
|
|
Assert.Equal("Blocked", change.Status);
|
|
Assert.Contains(predecessor.Id, change.BlockedReason);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(2), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_QueuedWithUnmetDependsOn_ReportsBlockedImmediately_InsteadOfTimingOut()
|
|
{
|
|
var dependency = await SeedTaskAsync(TaskStatus.Idle);
|
|
var task = await SeedTaskAsync(TaskStatus.Queued);
|
|
task.DependsOnTaskId = dependency.Id;
|
|
await _tasks.UpdateAsync(task);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange([task.Id], timeoutSeconds: 30, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.False(result.TimedOut);
|
|
var change = Assert.Single(result.Changed);
|
|
Assert.Equal("Blocked", change.Status);
|
|
Assert.Contains(dependency.Id, change.BlockedReason);
|
|
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(2), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WaitForTaskChange_QueuedWithDoneDependsOn_IsNotBlocked_StillWaitsAsBusy()
|
|
{
|
|
var dependency = await SeedTaskAsync(TaskStatus.Done);
|
|
var task = await SeedTaskAsync(TaskStatus.Queued);
|
|
task.DependsOnTaskId = dependency.Id;
|
|
await _tasks.UpdateAsync(task);
|
|
var sut = BuildSut();
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var result = await sut.WaitForTaskChange([task.Id], timeoutSeconds: 1, cancellationToken: CancellationToken.None);
|
|
|
|
sw.Stop();
|
|
Assert.True(result.TimedOut);
|
|
Assert.Empty(result.Changed);
|
|
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(900), $"took {sw.Elapsed}");
|
|
}
|
|
|
|
[Fact]
|
|
public void MaxTimeoutSeconds_StaysComfortablyUnderMcpToolTimeout()
|
|
{
|
|
// ClaudeProcess / InteractiveLaunchSpecService set MCP_TOOL_TIMEOUT=930000ms.
|
|
Assert.True(TaskWaitMcpTools.MaxTimeoutSeconds < 930);
|
|
}
|
|
}
|