Lagebild, Stufe 0: Teamuebersicht in der WebUI (#711)
Wer arbeitet, wer hat in den letzten Sekunden wen erreicht, und wo klemmt es, auf einen Blick. Stufe 0 kommt ohne Einheiten aus; mit #683 werden die Agenten zu aufklappbaren Containern, und die Ansicht faellt mit #688 zusammen. Kern: - AgentActivity: fuehrt die laufenden Laeufe (Beginn, Quelle, Aufgabe, Aufrufer) und die letzten Verbindungen (begrenzt auf 500), meldet Start, Ende und Verbindung als Ereignis. Vorher war nur ein Lauf ueber ChatAsync "laufend"; ein frischer Lauf (@new) war unsichtbar, und niemand hielt fest, wer wen gerufen hatte. - Die Engine meldet beide Einstiege an; "arbeitet" beginnt hinter dem Agententor. Anwendung: - TeamOverview stellt den Lagebericht zusammen. Die Zustaende entscheidet der Code aus erfassten Tatsachen: laufender Lauf, offene Freigabe, Status des letzten Belegs, Laufzeit gegen den Median des Agenten (haengt ab dem Dreifachen, fruehestens nach 5 Minuten, ohne Verlauf nach 15). Rangfolge: haengt, wartet auf Freigabe, arbeitet; Problem und Budget nur, solange nichts Neues laeuft. Web: - GET /api/team, Ereignis "activity" (started, finished, link); das Ereignis "run" traegt jetzt die Agenten-Id. - Ansicht #/team mit Cytoscape.js und ELK (bei Bedarf geladen): Layout aus der Struktur statt von Hand, Farbe und Symbol je Zustand, Kanten verblassen nach 30 s, Klick oeffnet eine Seitenleiste (laufende Laeufe, Freigaben, letzter Lauf im Wortlaut) mit "Freigaben oeffnen" vorne, danach Chat und Laeufe. Sprachpakete en/de. - Ein Layout, das lief, solange die Flaeche noch 0 px breit war, wird neu gerechnet, sobald sie eine Breite hat (im Pruefgestell gefunden). Geprueft im Browser mit einem Pruefgestell (API und Ereignisstrom durch Attrappen ersetzt): sieben Agenten in allen Zustaenden, Seitenleiste, Live-Kante und Verblassen. Tests: AgentActivityTests (8), TeamOverviewTests (14). 1.283 gruen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d587dc3f20
commit
f627918729
@@ -0,0 +1,170 @@
|
||||
using ClawdDotNet.Core.Accounting;
|
||||
using ClawdDotNet.Core.Activity;
|
||||
using ClawdDotNet.Core.Audit;
|
||||
using ClawdDotNet.Core.Config;
|
||||
using ClawdDotNet.Core.Engine;
|
||||
using ClawdDotNet.Core.Staging;
|
||||
|
||||
namespace ClawdDotNet.App.Services;
|
||||
|
||||
/// <summary>What an agent is doing, as the team overview shows it (#711).</summary>
|
||||
public enum TeamState
|
||||
{
|
||||
/// <summary>Nothing running, nothing open, the last run went fine (or there was none).</summary>
|
||||
Idle,
|
||||
|
||||
/// <summary>At least one run in progress.</summary>
|
||||
Working,
|
||||
|
||||
/// <summary>A run has taken much longer than this agent's runs usually take.</summary>
|
||||
Stuck,
|
||||
|
||||
/// <summary>Tool calls of this agent wait for a human decision.</summary>
|
||||
WaitingForApproval,
|
||||
|
||||
/// <summary>The last run failed, hit a limit or found the target busy.</summary>
|
||||
Problem,
|
||||
|
||||
/// <summary>The last run was refused because the budget is used up.</summary>
|
||||
BudgetExhausted
|
||||
}
|
||||
|
||||
/// <summary>One agent in the team overview.</summary>
|
||||
public sealed record TeamMember(
|
||||
string AgentId,
|
||||
string DisplayName,
|
||||
string Model,
|
||||
TeamState State,
|
||||
IReadOnlyList<ActiveRun> Runs,
|
||||
int PendingApprovals,
|
||||
string? LastRunStatus,
|
||||
DateTime? LastRunAtUtc,
|
||||
string? LastRunResult,
|
||||
long TypicalRunMs);
|
||||
|
||||
/// <summary>The whole picture at one moment: every agent, and who reached whom recently.</summary>
|
||||
public sealed record TeamSnapshot(IReadOnlyList<TeamMember> Members, IReadOnlyList<AgentLink> RecentLinks, DateTime AtUtc);
|
||||
|
||||
/// <summary>
|
||||
/// #711: The team overview — who is working, who reached whom, where something is stuck.
|
||||
///
|
||||
/// <para><b>Detecting is code, interpreting is model</b> (Organisation-Konzept, 10a). Every
|
||||
/// state here comes from a fact the instance recorded: a run in progress, an open staged
|
||||
/// call, the status of the last receipt. Nothing is judged by a model, so the picture is
|
||||
/// the same for everyone who looks and costs nothing to draw.</para>
|
||||
///
|
||||
/// <para><b>Order of states.</b> When several apply, the one that needs someone first wins:
|
||||
/// stuck, then waiting for approval, then working; a problem or an exhausted budget only
|
||||
/// shows while nothing new is running — a new run is the attempt to get past it.</para>
|
||||
/// </summary>
|
||||
public sealed class TeamOverview(
|
||||
AgentEngine? engine,
|
||||
StagingService? staging,
|
||||
IAuditRepository? audit,
|
||||
IUsageRepository? usage,
|
||||
Func<IReadOnlyList<AgentConfig>> agents,
|
||||
TimeProvider? clock = null)
|
||||
{
|
||||
/// <summary>How far back the last receipt of each agent is looked for.</summary>
|
||||
public static readonly TimeSpan ReceiptWindow = TimeSpan.FromDays(3);
|
||||
|
||||
/// <summary>How long recent links stay in the snapshot — the overview fades them out.</summary>
|
||||
public static readonly TimeSpan LinkWindow = TimeSpan.FromMinutes(10);
|
||||
|
||||
/// <summary>A run counts as stuck after this many times the agent's typical duration…</summary>
|
||||
public const int StuckFactor = 3;
|
||||
|
||||
/// <summary>…but never before this — short runs vary a lot in relative terms.</summary>
|
||||
public static readonly TimeSpan StuckFloor = TimeSpan.FromMinutes(5);
|
||||
|
||||
/// <summary>Without history, a run counts as stuck after this long.</summary>
|
||||
public static readonly TimeSpan StuckWithoutHistory = TimeSpan.FromMinutes(15);
|
||||
|
||||
private readonly TimeProvider _clock = clock ?? TimeProvider.System;
|
||||
|
||||
public async Task<TeamSnapshot> SnapshotAsync(CancellationToken ct)
|
||||
{
|
||||
var now = _clock.GetUtcNow().UtcDateTime;
|
||||
var configured = agents();
|
||||
|
||||
var active = engine?.Activity.ActiveRuns() ?? [];
|
||||
var links = engine?.Activity.RecentLinks(LinkWindow) ?? [];
|
||||
|
||||
var pending = staging is null ? [] : await staging.ListPendingAsync(ct);
|
||||
var pendingByAgent = pending
|
||||
.GroupBy(c => c.AgentId, StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(g => g.Key, g => g.Count(), StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var lastReceipt = new Dictionary<string, RunReceipt>(StringComparer.OrdinalIgnoreCase);
|
||||
if (audit is not null)
|
||||
{
|
||||
var receipts = await audit.ListReceiptsAsync(now - ReceiptWindow, now, 5_000, ct);
|
||||
foreach (var receipt in receipts)
|
||||
{
|
||||
if (!lastReceipt.TryGetValue(receipt.AgentId, out var known) || receipt.OccurredAt > known.OccurredAt)
|
||||
lastReceipt[receipt.AgentId] = receipt;
|
||||
}
|
||||
}
|
||||
|
||||
var members = new List<TeamMember>();
|
||||
foreach (var agent in configured)
|
||||
{
|
||||
var runs = active
|
||||
.Where(r => string.Equals(r.AgentId, agent.AgentId, StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(r => r.StartedAtUtc)
|
||||
.ToList();
|
||||
var approvals = pendingByAgent.GetValueOrDefault(agent.AgentId);
|
||||
lastReceipt.TryGetValue(agent.AgentId, out var last);
|
||||
var typical = await TypicalRunAsync(agent.AgentId, ct);
|
||||
|
||||
var state = Classify(runs, approvals, last?.Status, typical, now);
|
||||
members.Add(new TeamMember(
|
||||
agent.AgentId, agent.DisplayName, agent.Model, state, runs, approvals,
|
||||
last?.Status, last?.OccurredAt, last?.ResultRef, (long)typical.TotalMilliseconds));
|
||||
}
|
||||
|
||||
return new TeamSnapshot(members, links, now);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The state from the facts. Pure, so the rules can be tested without an instance.
|
||||
/// </summary>
|
||||
public static TeamState Classify(
|
||||
IReadOnlyList<ActiveRun> runs, int pendingApprovals, string? lastRunStatus, TimeSpan typicalRun, DateTime nowUtc)
|
||||
{
|
||||
if (runs.Count > 0)
|
||||
{
|
||||
var limit = typicalRun > TimeSpan.Zero
|
||||
? TimeSpan.FromTicks(Math.Max(typicalRun.Ticks * StuckFactor, StuckFloor.Ticks))
|
||||
: StuckWithoutHistory;
|
||||
if (runs.Any(r => nowUtc - r.StartedAtUtc > limit))
|
||||
return TeamState.Stuck;
|
||||
}
|
||||
|
||||
if (pendingApprovals > 0)
|
||||
return TeamState.WaitingForApproval;
|
||||
|
||||
if (runs.Count > 0)
|
||||
return TeamState.Working;
|
||||
|
||||
return lastRunStatus switch
|
||||
{
|
||||
nameof(AgentRunStatus.BudgetExceeded) => TeamState.BudgetExhausted,
|
||||
nameof(AgentRunStatus.Failed)
|
||||
or nameof(AgentRunStatus.LoopLimitExceeded)
|
||||
or nameof(AgentRunStatus.TargetBusy) => TeamState.Problem,
|
||||
_ => TeamState.Idle
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The agent's typical run duration — the median of its recent runs.</summary>
|
||||
private async Task<TimeSpan> TypicalRunAsync(string agentId, CancellationToken ct)
|
||||
{
|
||||
if (usage is null)
|
||||
return TimeSpan.Zero;
|
||||
|
||||
var recent = await usage.GetRecentForAgentAsync(agentId, 20, ct);
|
||||
var durations = recent.Where(r => r.DurationMs > 0).Select(r => r.DurationMs).Order().ToList();
|
||||
return durations.Count == 0 ? TimeSpan.Zero : TimeSpan.FromMilliseconds(durations[durations.Count / 2]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user