refactor(worker): single parent-advance path for planning + improvement
Collapse TryCompleteParentAsync (planning -> Done) and TryAdvanceImprovementParentAsync (improvement -> WaitingForReview) into one TryAdvanceParentAsync that surfaces any WaitingForChildren parent for review once all children are terminal. Planning parents no longer auto-complete. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -19,7 +19,7 @@ Shared data layer: models, repositories, SQLite infrastructure, and git operatio
|
||||
|
||||
All repositories use EF Core LINQ queries via `ClaudeDoDbContext`. The atomic `Queued -> Running` claim lives in the Worker's `QueuePicker` (uses `FromSqlRaw`), not here.
|
||||
|
||||
- **TaskRepository** — CRUD, planning helpers (`CreateChildAsync`, `SetPlanningStartedAsync`, `DiscardPlanningAsync`, `TryCompleteParentAsync`, `UpdateChildAsync`), `UpdateAgentSettingsAsync` (model / system-prompt / agent-path overrides). Status-mutation primitives `MarkRunningAsync` / `MarkDoneAsync` / `MarkFailedAsync` / `FlipAllRunningToFailedAsync` are `internal` and called only by `TaskStateService` in the worker. `CreateChildAsync` produces children with `Status=Idle, PlanningPhase=None`; once their parent's `PlanningPhase` becomes `Finalized`, the chain coordinator queues them.
|
||||
- **TaskRepository** — CRUD, planning helpers (`CreateChildAsync`, `SetPlanningStartedAsync`, `DiscardPlanningAsync`, `UpdateChildAsync`), `UpdateAgentSettingsAsync` (model / system-prompt / agent-path overrides). Status-mutation primitives `MarkRunningAsync` / `MarkDoneAsync` / `MarkFailedAsync` / `FlipAllRunningToFailedAsync` are `internal` and called only by `TaskStateService` in the worker. `CreateChildAsync` produces children with `Status=Idle, PlanningPhase=None`; once their parent's `PlanningPhase` becomes `Finalized`, the chain coordinator queues them.
|
||||
- **ListRepository** — CRUD, `GetConfigAsync` / `SetConfigAsync` (upsert) / `DeleteConfigAsync` for `list_config`
|
||||
- **WorktreeRepository** — CRUD, `UpdateHeadAsync`, `SetStateAsync`
|
||||
- **TaskRunRepository**, **SubtaskRepository**, **AppSettingsRepository**
|
||||
|
||||
@@ -474,32 +474,5 @@ public sealed class TaskRepository
|
||||
return chainIds.Count;
|
||||
}
|
||||
|
||||
public async Task TryCompleteParentAsync(
|
||||
string parentId,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var parent = await _context.Tasks.AsNoTracking().FirstOrDefaultAsync(t => t.Id == parentId, ct);
|
||||
if (parent is null || parent.PlanningPhase != PlanningPhase.Finalized) return;
|
||||
|
||||
var children = await _context.Tasks
|
||||
.Where(t => t.ParentTaskId == parentId)
|
||||
.Select(t => t.Status)
|
||||
.ToListAsync(ct);
|
||||
|
||||
if (children.Count == 0) return;
|
||||
|
||||
bool allTerminal = children.All(s => s == TaskStatus.Done || s == TaskStatus.Failed);
|
||||
if (!allTerminal) return;
|
||||
|
||||
bool anyFailed = children.Any(s => s == TaskStatus.Failed);
|
||||
var finalStatus = anyFailed ? TaskStatus.Failed : TaskStatus.Done;
|
||||
var finishedAt = DateTime.UtcNow;
|
||||
await _context.Tasks
|
||||
.Where(t => t.Id == parentId)
|
||||
.ExecuteUpdateAsync(s => s
|
||||
.SetProperty(t => t.Status, finalStatus)
|
||||
.SetProperty(t => t.FinishedAt, finishedAt), ct);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -386,28 +386,18 @@ public sealed class TaskStateService : ITaskStateService
|
||||
|
||||
try
|
||||
{
|
||||
await using var ctx = await _dbFactory.CreateDbContextAsync(CancellationToken.None);
|
||||
await new TaskRepository(ctx).TryCompleteParentAsync(parentId, CancellationToken.None);
|
||||
await TryAdvanceParentAsync(parentId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "TryCompleteParent failed for {ParentId}", parentId);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await TryAdvanceImprovementParentAsync(parentId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "TryAdvanceImprovementParent failed for {ParentId}", parentId);
|
||||
_logger.LogWarning(ex, "TryAdvanceParent failed for {ParentId}", parentId);
|
||||
}
|
||||
}
|
||||
|
||||
// Improvement parents sit in WaitingForChildren while their suggested children run.
|
||||
// Once every child is terminal (Done/Failed/Cancelled) the parent surfaces for review;
|
||||
// a failed or cancelled child does not wedge the parent — it is flagged on the result.
|
||||
private async Task TryAdvanceImprovementParentAsync(string parentId)
|
||||
// Any parent (planning or improvement) sitting in WaitingForChildren surfaces for review
|
||||
// once every child is terminal (Done/Failed/Cancelled). A failed or cancelled child does
|
||||
// not wedge the parent — it is flagged on the result.
|
||||
private async Task TryAdvanceParentAsync(string parentId)
|
||||
{
|
||||
string? parentResult;
|
||||
List<TaskStatus> childStatuses;
|
||||
|
||||
Reference in New Issue
Block a user