refactor(worker/state): introduce TaskStateService and route mutations through it
Slice 2 of the worker state consolidation refactor (spec sections 2 and 8). Adds Worker/State/ITaskStateService + TaskStateService as the single component that mutates Status, PlanningPhase, and BlockedByTaskId. Each transition is one atomic ExecuteUpdate with a WHERE filter on the expected source status, so parallel claims are TOCTOU-free. Side effects (queue wake on -> Queued, hub TaskUpdated broadcast, chain advance + parent completion on terminal child) are owned by the service so callers no longer need to remember them. Migrated callers (mechanical, behavior preserved): - TaskRunner: HandleSuccess/HandleFailure/MarkFailed/RunAsync/ContinueAsync - StaleTaskRecovery: bulk recover stale Running tasks - TaskResetService: status flip (worktree cleanup stays in service) - PlanningSessionManager.StartAsync: status flip via state, token write via repo - PlanningChainCoordinator.OnChildFinishedAsync: routes the next-sibling write through state.UnblockAsync (Slice 4 finishes the rewrite) - ExternalMcpService.UpdateTaskStatus: Queued case via state.EnqueueAsync Repo Mark*Async helpers (MarkRunning/MarkDone/MarkFailed/FlipAllRunningToFailed) are now internal; ClaudeDo.Data grants InternalsVisibleTo to ClaudeDo.Worker and ClaudeDo.Worker.Tests for the existing repo-level tests. DI: TaskStateService is registered as Singleton in both the main app and the external-MCP app; the queue-wake delegate captures sp -> QueueService.WakeQueue to break the TaskStateService -> QueueService -> TaskRunner -> TaskStateService construction cycle. PlanningChainCoordinator takes Func<ITaskStateService> for the same reason; Slice 3 will replace both with IQueueWaker. Tests: TaskStateServiceTests covers happy + reject for every transition, the parallel StartRunningAsync claim race, child-terminal chain advancement, and stale recovery. Existing service/repo tests are updated to construct the new state-service via a TaskStateServiceBuilder helper. Pre-existing constructor drift in QueueService/ExternalMcp/PlanningHub tests is patched to keep the test project building (the surrounding test logic is otherwise untouched). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Models;
|
||||
using ClaudeDo.Worker.State;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
||||
|
||||
@@ -8,9 +9,15 @@ namespace ClaudeDo.Worker.Planning;
|
||||
public sealed class PlanningChainCoordinator
|
||||
{
|
||||
private readonly IDbContextFactory<ClaudeDoDbContext> _dbFactory;
|
||||
private readonly Func<ITaskStateService> _state;
|
||||
|
||||
public PlanningChainCoordinator(IDbContextFactory<ClaudeDoDbContext> dbFactory)
|
||||
=> _dbFactory = dbFactory;
|
||||
public PlanningChainCoordinator(
|
||||
IDbContextFactory<ClaudeDoDbContext> dbFactory,
|
||||
Func<ITaskStateService> state)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_state = state;
|
||||
}
|
||||
|
||||
public async Task QueueSubtasksSequentiallyAsync(string parentTaskId, CancellationToken ct = default)
|
||||
{
|
||||
@@ -56,6 +63,7 @@ public sealed class PlanningChainCoordinator
|
||||
if (child?.ParentTaskId is null) return null;
|
||||
|
||||
var next = await ctx.Tasks
|
||||
.AsNoTracking()
|
||||
.Where(t => t.ParentTaskId == child.ParentTaskId
|
||||
&& t.SortOrder > child.SortOrder
|
||||
&& t.Status == TaskStatus.Waiting)
|
||||
@@ -63,8 +71,7 @@ public sealed class PlanningChainCoordinator
|
||||
.FirstOrDefaultAsync(ct);
|
||||
if (next is null) return null;
|
||||
|
||||
next.Status = TaskStatus.Queued;
|
||||
await ctx.SaveChangesAsync(ct);
|
||||
await _state().UnblockAsync(next.Id, ct);
|
||||
return next.Id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using ClaudeDo.Data.Git;
|
||||
using ClaudeDo.Data.Models;
|
||||
using ClaudeDo.Data.Repositories;
|
||||
using ClaudeDo.Worker.Config;
|
||||
using ClaudeDo.Worker.State;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
||||
|
||||
@@ -22,17 +23,20 @@ public sealed class PlanningSessionManager
|
||||
private readonly GitService _git;
|
||||
private readonly WorkerConfig _cfg;
|
||||
private readonly string _rootDirectory;
|
||||
private readonly ITaskStateService? _state;
|
||||
|
||||
// DI constructor.
|
||||
public PlanningSessionManager(
|
||||
IDbContextFactory<ClaudeDoDbContext> factory,
|
||||
GitService git,
|
||||
WorkerConfig cfg,
|
||||
ITaskStateService state,
|
||||
string rootDirectory)
|
||||
{
|
||||
_factory = factory;
|
||||
_git = git;
|
||||
_cfg = cfg;
|
||||
_state = state;
|
||||
_rootDirectory = rootDirectory;
|
||||
}
|
||||
|
||||
@@ -43,13 +47,15 @@ public sealed class PlanningSessionManager
|
||||
AppSettingsRepository settings,
|
||||
GitService git,
|
||||
WorkerConfig cfg,
|
||||
string rootDirectory)
|
||||
string rootDirectory,
|
||||
ITaskStateService? state = null)
|
||||
{
|
||||
_tasksOverride = tasks;
|
||||
_listsOverride = lists;
|
||||
_settingsOverride = settings;
|
||||
_git = git;
|
||||
_cfg = cfg;
|
||||
_state = state;
|
||||
_rootDirectory = rootDirectory;
|
||||
}
|
||||
|
||||
@@ -114,8 +120,19 @@ public sealed class PlanningSessionManager
|
||||
|
||||
// Session dir + token + prompt files.
|
||||
var token = GenerateToken();
|
||||
var started = await tasks.SetPlanningStartedAsync(taskId, token, ct)
|
||||
?? throw new InvalidOperationException("Failed to transition task to Planning.");
|
||||
if (_state is not null)
|
||||
{
|
||||
var startResult = await _state.StartPlanningAsync(taskId, ct);
|
||||
if (!startResult.Ok)
|
||||
throw new InvalidOperationException(startResult.Reason ?? "Failed to transition task to Planning.");
|
||||
await tasks.SetPlanningSessionTokenAsync(taskId, token, ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Test fallback when no state-service is provided.
|
||||
if (await tasks.SetPlanningStartedAsync(taskId, token, ct) is null)
|
||||
throw new InvalidOperationException("Failed to transition task to Planning.");
|
||||
}
|
||||
|
||||
var sessionDir = Path.Combine(_rootDirectory, taskId);
|
||||
Directory.CreateDirectory(sessionDir);
|
||||
|
||||
Reference in New Issue
Block a user