Zielkette, Stufe 2: ruhende Aufgabenbaeume werden geprueft (#714)
- TaskTreeWatch findet offene Aufgaben, deren ganzer Teilbaum zur Ruhe gekommen ist: nichts laeuft, nichts wird gleich gestartet (eine todo an einen Menschen bewegt sich nicht von selbst). Nur der oberste ruhende Baum zaehlt. Rein und ohne Instanz pruefbar. - Erkennen ist Code, Deuten ist Modell: Die Leitung der Einheit, zu der der Baum gehoert (sonst die darueber, sonst ein Mensch), bekommt eine Pruefaufgabe mit dem Baum und den typischen Fehlhalten. Sie darf Arbeit zurueckgeben, nicht selbst nachbessern. - Einmal je Ruhezustand: Ein Fingerabdruck des Baums im StateStore. Wer den Stillstand hinnimmt, wird nicht erneut gefragt; erst wenn sich der Baum bewegt hat und wieder ruht. Die Pruefaufgabe steht neben dem Baum, nicht darin. - Der Scanner ruft die Pruefung je Takt, sie schaut alle 30 Minuten. Erster Baustein von #646 (Erkennen); die uebrigen Muster dort folgen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b22272345e
commit
cd244b8cbd
@@ -137,6 +137,7 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
private readonly ILateRunGate? _lateRunGate;
|
||||
|
||||
private readonly IBudgetOutlook? _outlook;
|
||||
private readonly ITaskTreeWatch? _treeWatch;
|
||||
|
||||
/// <summary>How often the day ahead is looked at — a forecast, not a watchdog.</summary>
|
||||
public static readonly TimeSpan OutlookInterval = TimeSpan.FromMinutes(30);
|
||||
@@ -163,8 +164,10 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
int maxParallelRuns = DefaultMaxParallelRuns,
|
||||
Func<TaskItem, BudgetMissPolicy>? budgetMissPolicy = null,
|
||||
ILateRunGate? lateRunGate = null,
|
||||
IBudgetOutlook? budgetOutlook = null)
|
||||
IBudgetOutlook? budgetOutlook = null,
|
||||
ITaskTreeWatch? treeWatch = null)
|
||||
{
|
||||
_treeWatch = treeWatch;
|
||||
_repo = repo;
|
||||
_dispatcher = dispatcher;
|
||||
_market = market ?? new AlwaysOpenMarketCalendar();
|
||||
@@ -286,6 +289,10 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
SummarizeMissesIfNewDay(now);
|
||||
await WarnAboutShortfallsAsync(candidates, now, ct);
|
||||
|
||||
// #714: resting task trees are looked at — the watch decides itself how often.
|
||||
if (_treeWatch is not null)
|
||||
await _treeWatch.CheckAsync(now, ct);
|
||||
|
||||
var due = new List<(TaskItem Task, string Occurrence, bool Late)>();
|
||||
foreach (var task in candidates)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using ClawdDotNet.Core.Organisation;
|
||||
using ClawdDotNet.Core.State;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ClawdDotNet.Core.Tasks;
|
||||
|
||||
/// <summary>A check the scanner runs on every tick; it decides itself how often it acts.</summary>
|
||||
public interface ITaskTreeWatch
|
||||
{
|
||||
Task CheckAsync(DateTime nowUtc, CancellationToken ct);
|
||||
}
|
||||
|
||||
/// <summary>An open task whose whole subtree has come to rest.</summary>
|
||||
public sealed record RestingTree(TaskItem Root, IReadOnlyList<TaskItem> Below);
|
||||
|
||||
/// <summary>
|
||||
/// #714: A task tree that has stopped moving is looked at. Taken from Paperclip's "task
|
||||
/// watchdog" (Organisation-Konzept, 9a): agents stop for wrong reasons — "done" without
|
||||
/// evidence, a review nobody does, a blocker read wrongly, an error given up on that could
|
||||
/// have been fixed. Without a check the tree simply lies still, and nobody notices.
|
||||
///
|
||||
/// <para><b>Detecting is code, interpreting is a model.</b> Whether a tree rests is a query
|
||||
/// over the board (this class, the first piece of #646). Whether the rest is justified is
|
||||
/// decided by the lead of the unit the tree belongs to — or a human, where there is no lead
|
||||
/// — through a review task. The reviewer may send work back (comment, reopen), not do it
|
||||
/// itself (the line of #680).</para>
|
||||
///
|
||||
/// <para><b>Once per state of rest.</b> The review is not repeated while the tree stays
|
||||
/// exactly as it was: a reviewer who accepts the rest is not asked again. Only when the
|
||||
/// tree has moved and comes to rest in a different state is it looked at again. The review
|
||||
/// task is not part of the tree, so it does not change what it reviews.</para>
|
||||
/// </summary>
|
||||
public sealed class TaskTreeWatch(
|
||||
ITaskRepository repo,
|
||||
Func<TaskItem, CancellationToken, Task> createTask,
|
||||
IStateStore state,
|
||||
Func<OrgChart?> organisation,
|
||||
ILogger logger) : ITaskTreeWatch
|
||||
{
|
||||
/// <summary>How often the board is looked at.</summary>
|
||||
public static readonly TimeSpan Interval = TimeSpan.FromMinutes(30);
|
||||
|
||||
private const string StateKeyPrefix = "tasktree.rest.";
|
||||
|
||||
private DateTime _lastCheckAt = DateTime.MinValue;
|
||||
|
||||
public async Task CheckAsync(DateTime nowUtc, CancellationToken ct)
|
||||
{
|
||||
if (nowUtc - _lastCheckAt < Interval)
|
||||
return;
|
||||
_lastCheckAt = nowUtc;
|
||||
|
||||
try
|
||||
{
|
||||
var all = await repo.ListAllAsync(includeArchived: false, ct);
|
||||
foreach (var tree in FindResting(all))
|
||||
await ReviewOnceAsync(tree, ct);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogWarning(ex, "Check of resting task trees failed — next attempt in {Minutes} min", Interval.TotalMinutes);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The open tasks whose subtree has come to rest — only the topmost of them, so a tree is
|
||||
/// reviewed once and not again for each of its resting branches. Pure, so the rules can
|
||||
/// be tested without an instance.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<RestingTree> FindResting(IReadOnlyList<TaskItem> all)
|
||||
{
|
||||
var byId = all.ToDictionary(t => t.Id, StringComparer.OrdinalIgnoreCase);
|
||||
var children = all
|
||||
.Where(t => !string.IsNullOrWhiteSpace(t.ParentId) && byId.ContainsKey(t.ParentId))
|
||||
.GroupBy(t => t.ParentId, StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(g => g.Key, g => g.ToList(), StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
List<TaskItem> Below(string id)
|
||||
{
|
||||
var result = new List<TaskItem>();
|
||||
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { id };
|
||||
var pending = new Stack<string>();
|
||||
pending.Push(id);
|
||||
while (pending.TryPop(out var current))
|
||||
{
|
||||
foreach (var child in children.GetValueOrDefault(current) ?? [])
|
||||
{
|
||||
if (!seen.Add(child.Id))
|
||||
continue; // a cycle in the data must not hang this
|
||||
result.Add(child);
|
||||
pending.Push(child.Id);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
var resting = new Dictionary<string, RestingTree>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var root in all.Where(t => children.ContainsKey(t.Id) && IsOpen(t)))
|
||||
{
|
||||
var below = Below(root.Id);
|
||||
if (below.Count > 0 && below.All(IsAtRest) && IsAtRest(root))
|
||||
resting[root.Id] = new RestingTree(root, below);
|
||||
}
|
||||
|
||||
// Only the topmost: a resting tree inside another resting tree is part of that one.
|
||||
return resting.Values
|
||||
.Where(tree => !Ancestors(tree.Root, byId).Any(a => resting.ContainsKey(a.Id)))
|
||||
.OrderBy(tree => tree.Root.Id, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>Not finished: the tree still has a purpose to reach.</summary>
|
||||
private static bool IsOpen(TaskItem task)
|
||||
=> task.Status is not (TaskItemStatus.Done or TaskItemStatus.Canceled or TaskItemStatus.Archived);
|
||||
|
||||
/// <summary>
|
||||
/// Nothing will move this task by itself: it is not running, and the scanner will not
|
||||
/// start it — a todo for an agent is still on its way, a todo for a human is not.
|
||||
/// </summary>
|
||||
private static bool IsAtRest(TaskItem task) => task.Status switch
|
||||
{
|
||||
TaskItemStatus.InProgress => false,
|
||||
TaskItemStatus.Todo => TaskAssignee.KindOf(task.Assignee) == TaskAssigneeKind.Human,
|
||||
_ => true
|
||||
};
|
||||
|
||||
private static IEnumerable<TaskItem> Ancestors(TaskItem task, IReadOnlyDictionary<string, TaskItem> byId)
|
||||
{
|
||||
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { task.Id };
|
||||
var next = task.ParentId;
|
||||
while (!string.IsNullOrWhiteSpace(next) && seen.Add(next) && byId.TryGetValue(next, out var parent))
|
||||
{
|
||||
yield return parent;
|
||||
next = parent.ParentId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>What the tree looks like — the same value means nothing moved since the last review.</summary>
|
||||
public static string Fingerprint(RestingTree tree)
|
||||
{
|
||||
var text = string.Join("|", new[] { tree.Root }.Concat(tree.Below)
|
||||
.OrderBy(t => t.Id, StringComparer.OrdinalIgnoreCase)
|
||||
.Select(t => $"{t.Id}:{TaskText.Of(t.Status)}"));
|
||||
return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(text)))[..16];
|
||||
}
|
||||
|
||||
private async Task ReviewOnceAsync(RestingTree tree, CancellationToken ct)
|
||||
{
|
||||
var key = StateKeyPrefix + tree.Root.Id;
|
||||
var fingerprint = Fingerprint(tree);
|
||||
if (await state.GetAsync(key, ct) == fingerprint)
|
||||
return;
|
||||
|
||||
var reviewer = ReviewerOf(tree.Root, organisation());
|
||||
await createTask(BuildReview(tree, reviewer), ct);
|
||||
await state.SetAsync(key, fingerprint, ct);
|
||||
|
||||
logger.LogInformation(
|
||||
"Task tree {Root} has come to rest ({Count} tasks below) — review assigned to {Reviewer}",
|
||||
tree.Root.Id, tree.Below.Count, reviewer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The lead of the unit whose board the tree is on — or, where that unit has none, the
|
||||
/// lead above it; without any, a human.
|
||||
/// </summary>
|
||||
public static string ReviewerOf(TaskItem root, OrgChart? org)
|
||||
{
|
||||
if (org is not { HasUnits: true } || TaskBoard.UnitOf(root, org) is not { } unitId)
|
||||
return TaskAssignee.Human;
|
||||
|
||||
foreach (var unit in new[] { org.Unit(unitId)! }.Concat(org.Ancestors(unitId)))
|
||||
{
|
||||
if (org.LeaderOf(unit.Id) is { } lead)
|
||||
return "@" + lead.AgentId;
|
||||
}
|
||||
return TaskAssignee.Human;
|
||||
}
|
||||
|
||||
/// <summary>The review task. It stands beside the tree, not in it — it must not change what it reviews.</summary>
|
||||
public static TaskItem BuildReview(RestingTree tree, string reviewer)
|
||||
{
|
||||
var body = new StringBuilder();
|
||||
body.AppendLine($"The work under {tree.Root.Id} \"{tree.Root.Title}\" has come to rest: nothing in it is running or about to run, but {tree.Root.Id} is not finished.");
|
||||
body.AppendLine();
|
||||
body.AppendLine($"- {tree.Root.Id} [{TaskText.Of(tree.Root.Status)}] {tree.Root.Title} ({tree.Root.Assignee})");
|
||||
foreach (var task in tree.Below.OrderBy(t => t.Id, StringComparer.OrdinalIgnoreCase))
|
||||
body.AppendLine($" - {task.Id} [{TaskText.Of(task.Status)}] {task.Title} ({task.Assignee}{(string.IsNullOrWhiteSpace(task.ParentId) ? "" : $", serves {task.ParentId}")})");
|
||||
body.AppendLine();
|
||||
body.AppendLine("Decide whether the standstill is justified. Read the tasks and their comments. Typical wrong stops:");
|
||||
body.AppendLine("- marked done without evidence that the result exists,");
|
||||
body.AppendLine("- waiting for a review that nobody does,");
|
||||
body.AppendLine("- blocked by something that is already finished, or was misread,");
|
||||
body.AppendLine("- an error given up on that could have been fixed.");
|
||||
body.AppendLine();
|
||||
body.AppendLine("If it is justified, say why in your result. If not, send the work back: comment on the task and reopen it (task_comment with reopen), or finish or cancel the top task. Do not do the work yourself — your job is to judge it, not to redo it.");
|
||||
|
||||
return new TaskItem
|
||||
{
|
||||
Title = $"Review: work under {tree.Root.Id} has come to rest",
|
||||
Body = body.ToString().TrimEnd(),
|
||||
Assignee = reviewer,
|
||||
Priority = Math.Clamp(tree.Root.Priority, 1, 5),
|
||||
Status = TaskItemStatus.Todo,
|
||||
Type = TaskItemType.Work,
|
||||
Acceptance = "The result says, for each wrongly stopped task, what was sent back — or why the standstill is justified."
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user