feat: Bao/Iris-Statusrechte + Bao→Iris-Notifications + Agent-Workflow-Übersicht
- Bao darf jetzt Status ändern (neben Iris), Sub-Agents weiterhin nicht - CanEditContent für Inhaltsbearbeitung durch alle bekannten Caller - Bao-Content-Änderungen triggern task_content_changed-Notification an Iris - Bao-Status-Änderungen triggern task_status_changed-Notification an Iris - Iris-Status-Änderungen triggern task_status_changed-Notification an Bao - Neue WorkTask-Felder: IsAgentTask (bool), ExpectedFrom (string) - Agent-Workflow-API: CreateAgentTask, WaitingTasks, AgentOverview - Frontend: Agent-Task-Badge, Iris-Overview-Panel, isBao-Getter - Login-Rate-Limiter mit strukturiertem JSON-Fehlermeldungs-Body - Volume-Name: nexus-postgres → postgres-data (Standardisierung)
This commit is contained in:
@@ -23,15 +23,21 @@ public interface ITaskService
|
||||
// Dashboard-facing task operations
|
||||
Task<IReadOnlyList<WorkTask>> GetOpenAsync(CancellationToken ct = default);
|
||||
Task<WorkTask> CreateDashboardTaskAsync(string title, string? detail, string? source, string? priority, string? assignedTo, Guid? parentTaskId = null, CancellationToken ct = default);
|
||||
Task<WorkTask> CreateAgentTaskAsync(string title, string? detail, string? source, string? priority, string? assignedTo, string? expectedFrom, Guid? parentTaskId = null, CancellationToken ct = default);
|
||||
Task<TaskOperationResult> UpdateDashboardTaskAsync(Guid id, string? title, string? detail, string? source, string? priority, string? assignedTo, DateTimeOffset? dueDate = null, CancellationToken ct = default);
|
||||
Task<TaskOperationResult> UpdateStatusAsync(Guid id, string status, CancellationToken ct = default);
|
||||
Task<TaskOperationResult> CompleteViaQueueAsync(Guid id, CancellationToken ct = default);
|
||||
Task<TaskOperationResult> CyclePriorityAsync(Guid id, CancellationToken ct = default);
|
||||
|
||||
// Task Board
|
||||
Task<TaskBoardResponse> GetBoardAsync(CancellationToken ct = default);
|
||||
Task<BoardResponse> GetBoardAsync(CancellationToken ct = default);
|
||||
Task<TaskOperationResult> MoveTaskAsync(Guid id, string newState, CancellationToken ct = default);
|
||||
Task<int> ResetStaleAsync(int staleHours, CancellationToken ct = default);
|
||||
Task<int> ResetStaleInProgressTasksAsync(TimeSpan staleThreshold, CancellationToken ct = default);
|
||||
Task<IReadOnlyList<WorkTask>> GetChildTasksAsync(Guid parentId, CancellationToken ct = default);
|
||||
Task<List<ActivityEvent>> GetTaskActivityAsync(Guid taskId, CancellationToken ct = default);
|
||||
|
||||
// Agent Workflow Overview
|
||||
Task<IReadOnlyList<WorkTask>> GetWaitingTasksAsync(CancellationToken ct = default);
|
||||
Task<AgentWorkflowOverview> GetAgentWorkflowOverviewAsync(TimeSpan staleThreshold, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
+246
-18
@@ -8,7 +8,8 @@ namespace Nexus.Api.Services;
|
||||
public sealed class TaskService(
|
||||
ITaskRepository taskRepo,
|
||||
IActivityRepository activityRepo,
|
||||
INotificationService notificationService) : ITaskService
|
||||
INotificationService notificationService,
|
||||
IHttpContextAccessor httpContextAccessor) : ITaskService
|
||||
{
|
||||
private static readonly HashSet<string> ValidAssignees =
|
||||
["bao", "iris", "programmer", "reviewer", "architekt"];
|
||||
@@ -71,6 +72,11 @@ public sealed class TaskService(
|
||||
var task = await taskRepo.GetByIdAsync(id, ct);
|
||||
if (task is null) return new TaskOperationResult(TaskOperationOutcome.NotFound);
|
||||
|
||||
// Enforce workflow rules
|
||||
var caller = ResolveCaller();
|
||||
if (!TaskStateHelper.CanChangeState(caller, task))
|
||||
return new TaskOperationResult(TaskOperationOutcome.InvalidState);
|
||||
|
||||
task.State = canonical;
|
||||
await taskRepo.UpdateAsync(task, ct);
|
||||
await activityRepo.AddAsync(new ActivityEvent { Type = "task", Message = $"Task {task.Title} moved to {task.State}", TaskId = task.Id }, ct);
|
||||
@@ -83,15 +89,27 @@ public sealed class TaskService(
|
||||
var task = await taskRepo.GetByIdAsync(id, ct);
|
||||
if (task is null) return new TaskOperationResult(TaskOperationOutcome.NotFound);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Title))
|
||||
var changes = new List<string>();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Title) && !string.Equals(task.Title, request.Title.Trim(), StringComparison.Ordinal))
|
||||
{
|
||||
changes.Add($"Titel: \"{task.Title}\" → \"{request.Title.Trim()}\"");
|
||||
task.Title = request.Title.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(request.Priority))
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(request.Priority) && !string.Equals(task.Priority, request.Priority.Trim(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
changes.Add($"Priorität: {task.Priority} → {request.Priority.Trim()}");
|
||||
task.Priority = request.Priority.Trim();
|
||||
}
|
||||
if (request.ProjectId.HasValue)
|
||||
{
|
||||
changes.Add($"Projekt-ID geändert");
|
||||
task.ProjectId = request.ProjectId.Value == Guid.Empty ? null : request.ProjectId;
|
||||
}
|
||||
|
||||
await taskRepo.UpdateAsync(task, ct);
|
||||
await activityRepo.AddAsync(new ActivityEvent { Type = "task", Message = $"Task {task.Title} updated", TaskId = task.Id }, ct);
|
||||
var changeSummary = changes.Count > 0 ? string.Join("; ", changes) : "keine sichtbaren Änderungen";
|
||||
await activityRepo.AddAsync(new ActivityEvent { Type = "task", Message = $"Task \"{task.Title}\" aktualisiert: {changeSummary}", TaskId = task.Id }, ct);
|
||||
return new TaskOperationResult(TaskOperationOutcome.Success, task);
|
||||
}
|
||||
|
||||
@@ -118,6 +136,66 @@ public sealed class TaskService(
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns agent-tasks that are still open and where an agent is expected to respond.
|
||||
/// Iris Dashboard uses this to see who she is waiting for.
|
||||
/// </summary>
|
||||
public async Task<IReadOnlyList<WorkTask>> GetWaitingTasksAsync(CancellationToken ct = default)
|
||||
{
|
||||
var all = await taskRepo.GetAllAsync(ct);
|
||||
return all
|
||||
.Where(t => t.IsAgentTask && !string.Equals(t.State, "Done", StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(t => t.ExpectedFrom != null ? 0 : 1)
|
||||
.ThenByDescending(t => t.UpdatedAt)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns agent-tasks grouped by which agent is expected to respond,
|
||||
/// with stale-detection: tasks in InProgress/Delegated that haven't been
|
||||
/// updated within the stale threshold.
|
||||
/// </summary>
|
||||
public async Task<AgentWorkflowOverview> GetAgentWorkflowOverviewAsync(TimeSpan staleThreshold, CancellationToken ct = default)
|
||||
{
|
||||
var all = await taskRepo.GetAllAsync(ct);
|
||||
var threshold = DateTimeOffset.UtcNow - staleThreshold;
|
||||
|
||||
var agentTasks = all.Where(t => t.IsAgentTask).ToList();
|
||||
|
||||
var waitingForBao = agentTasks
|
||||
.Where(t => string.Equals(t.ExpectedFrom, "bao", StringComparison.OrdinalIgnoreCase) &&
|
||||
!string.Equals(t.State, "Done", StringComparison.OrdinalIgnoreCase))
|
||||
.Select(MapToDto)
|
||||
.ToList();
|
||||
|
||||
var waitingForIris = agentTasks
|
||||
.Where(t => string.Equals(t.ExpectedFrom, "iris", StringComparison.OrdinalIgnoreCase) &&
|
||||
!string.Equals(t.State, "Done", StringComparison.OrdinalIgnoreCase))
|
||||
.Select(MapToDto)
|
||||
.ToList();
|
||||
|
||||
var waitingForOthers = agentTasks
|
||||
.Where(t =>
|
||||
{
|
||||
var expected = (t.ExpectedFrom ?? "").ToLowerInvariant();
|
||||
return expected != "bao" && expected != "iris" && !string.IsNullOrWhiteSpace(expected) &&
|
||||
!string.Equals(t.State, "Done", StringComparison.OrdinalIgnoreCase);
|
||||
})
|
||||
.Select(MapToDto)
|
||||
.ToList();
|
||||
|
||||
var staleTasks = agentTasks
|
||||
.Where(t =>
|
||||
(string.Equals(t.State, "In progress", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(t.State, "Delegated", StringComparison.OrdinalIgnoreCase)) &&
|
||||
t.UpdatedAt < threshold)
|
||||
.Select(MapToDto)
|
||||
.ToList();
|
||||
|
||||
return new AgentWorkflowOverview(waitingForBao, waitingForIris, waitingForOthers,
|
||||
staleTasks, staleThreshold);
|
||||
}
|
||||
|
||||
public async Task<WorkTask> CreateDashboardTaskAsync(
|
||||
string title, string? detail, string? source, string? priority,
|
||||
string? assignedTo, Guid? parentTaskId = null, CancellationToken ct = default)
|
||||
@@ -161,22 +239,108 @@ public sealed class TaskService(
|
||||
return task;
|
||||
}
|
||||
|
||||
public async Task<WorkTask> CreateAgentTaskAsync(
|
||||
string title, string? detail, string? source, string? priority,
|
||||
string? assignedTo, string? expectedFrom, Guid? parentTaskId = null, CancellationToken ct = default)
|
||||
{
|
||||
var task = await CreateDashboardTaskAsync(title, detail, source, priority, assignedTo, parentTaskId, ct);
|
||||
|
||||
task.IsAgentTask = true;
|
||||
task.ExpectedFrom = string.IsNullOrWhiteSpace(expectedFrom) ? null : expectedFrom.Trim().ToLowerInvariant();
|
||||
|
||||
// Persist the agent-task-specific fields
|
||||
await taskRepo.UpdateAsync(task, ct);
|
||||
|
||||
await activityRepo.AddAsync(new ActivityEvent
|
||||
{
|
||||
Type = "agent_task",
|
||||
Message = $"Agent-Task created: \"{task.Title}\" (Source: {task.Source}, Expected: {task.ExpectedFrom ?? "none"})",
|
||||
TaskId = task.Id
|
||||
}, ct);
|
||||
|
||||
// Notify iris about new agent-task
|
||||
await notificationService.CreateAsync(
|
||||
"agent_task_created",
|
||||
$"Neuer Agent-Task: {task.Title}",
|
||||
detail,
|
||||
"iris",
|
||||
task.Id,
|
||||
ct);
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
public async Task<TaskOperationResult> UpdateDashboardTaskAsync(
|
||||
Guid id, string? title, string? detail, string? source,
|
||||
string? priority, string? assignedTo, DateTimeOffset? dueDate = null, CancellationToken ct = default)
|
||||
{
|
||||
var caller = ResolveCaller();
|
||||
var task = await taskRepo.GetByIdAsync(id, ct);
|
||||
if (task is null) return new TaskOperationResult(TaskOperationOutcome.NotFound);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(title)) task.Title = title.Trim();
|
||||
if (detail is not null) task.Detail = string.IsNullOrWhiteSpace(detail) ? null : detail.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(source)) task.Source = source.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(priority)) task.Priority = priority.Trim();
|
||||
if (assignedTo is not null) task.AssignedTo = ValidateAssignedTo(assignedTo);
|
||||
if (dueDate.HasValue) task.DueDate = dueDate;
|
||||
var changes = new List<string>();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(title) && !string.Equals(task.Title, title.Trim(), StringComparison.Ordinal))
|
||||
{
|
||||
changes.Add($"Titel: \"{task.Title}\" → \"{title.Trim()}\"");
|
||||
task.Title = title.Trim();
|
||||
}
|
||||
if (detail is not null)
|
||||
{
|
||||
var newDetail = string.IsNullOrWhiteSpace(detail) ? null : detail.Trim();
|
||||
if (!string.Equals(task.Detail ?? "", newDetail ?? "", StringComparison.Ordinal))
|
||||
{
|
||||
changes.Add("Beschreibung aktualisiert");
|
||||
task.Detail = newDetail;
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(source))
|
||||
task.Source = source.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(priority) && !string.Equals(task.Priority, priority.Trim(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
changes.Add($"Priorität: {task.Priority} → {priority.Trim()}");
|
||||
task.Priority = priority.Trim();
|
||||
}
|
||||
if (assignedTo is not null)
|
||||
{
|
||||
var validated = ValidateAssignedTo(assignedTo);
|
||||
if (!string.Equals(task.AssignedTo ?? "", validated ?? "", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
changes.Add($"Zuständig: {task.AssignedTo ?? "niemand"} → {validated ?? "niemand"}");
|
||||
task.AssignedTo = validated;
|
||||
}
|
||||
}
|
||||
if (dueDate.HasValue)
|
||||
{
|
||||
if (task.DueDate?.Date != dueDate.Value.Date)
|
||||
{
|
||||
changes.Add($"Fällig: {task.DueDate?.ToString("yyyy-MM-dd") ?? "kein Datum"} → {dueDate.Value:yyyy-MM-dd}");
|
||||
task.DueDate = dueDate;
|
||||
}
|
||||
}
|
||||
|
||||
await taskRepo.UpdateAsync(task, ct);
|
||||
await activityRepo.AddAsync(new ActivityEvent { Type = "task", Message = $"Task \"{task.Title}\" updated", TaskId = task.Id }, ct);
|
||||
|
||||
var changeSummary = changes.Count > 0 ? string.Join("; ", changes) : "keine sichtbaren Änderungen";
|
||||
await activityRepo.AddAsync(new ActivityEvent
|
||||
{
|
||||
Type = "task",
|
||||
Message = $"Task \"{task.Title}\" aktualisiert von {caller}: {changeSummary}",
|
||||
TaskId = task.Id
|
||||
}, ct);
|
||||
|
||||
// Notification: wenn Bao die Task geändert hat, Iris benachrichtigen
|
||||
if (changes.Count > 0 && caller == "bao")
|
||||
{
|
||||
await notificationService.CreateAsync(
|
||||
"task_content_changed",
|
||||
$"Bao hat \"{task.Title}\" geändert",
|
||||
$"{changeSummary}",
|
||||
"iris",
|
||||
task.Id,
|
||||
ct);
|
||||
}
|
||||
|
||||
return new TaskOperationResult(TaskOperationOutcome.Success, task);
|
||||
}
|
||||
|
||||
@@ -188,6 +352,11 @@ public sealed class TaskService(
|
||||
var task = await taskRepo.GetByIdAsync(id, ct);
|
||||
if (task is null) return new TaskOperationResult(TaskOperationOutcome.NotFound);
|
||||
|
||||
// Enforce workflow rules
|
||||
var caller = ResolveCaller();
|
||||
if (!TaskStateHelper.CanChangeState(caller, task))
|
||||
return new TaskOperationResult(TaskOperationOutcome.InvalidState);
|
||||
|
||||
var canonical = TaskStateHelper.AllStates.First(s => s.Equals(status, StringComparison.OrdinalIgnoreCase));
|
||||
task.State = canonical;
|
||||
await taskRepo.UpdateAsync(task, ct);
|
||||
@@ -227,7 +396,7 @@ public sealed class TaskService(
|
||||
|
||||
// ── Board operations ──
|
||||
|
||||
public async Task<TaskBoardResponse> GetBoardAsync(CancellationToken ct = default)
|
||||
public async Task<BoardResponse> GetBoardAsync(CancellationToken ct = default)
|
||||
{
|
||||
var all = await taskRepo.GetAllAsync(ct);
|
||||
var offen = new List<DashboardTaskDto>();
|
||||
@@ -259,7 +428,6 @@ public sealed class TaskService(
|
||||
}
|
||||
}
|
||||
|
||||
// Priority sort within each group: High > Medium > Low, then by CreatedAt ascending
|
||||
offen.Sort(SortByPriorityThenCreatedAt);
|
||||
inProgress.Sort(SortByPriorityThenCreatedAt);
|
||||
delegated.Sort(SortByPriorityThenCreatedAt);
|
||||
@@ -267,7 +435,7 @@ public sealed class TaskService(
|
||||
blocked.Sort(SortByPriorityThenCreatedAt);
|
||||
done.Sort(SortByPriorityThenCreatedAt);
|
||||
|
||||
return new TaskBoardResponse(offen, inProgress, delegated, review, blocked, done);
|
||||
return new BoardResponse(offen, inProgress, delegated, review, blocked, done);
|
||||
}
|
||||
|
||||
private static int SortByPriorityThenCreatedAt(DashboardTaskDto a, DashboardTaskDto b)
|
||||
@@ -303,6 +471,11 @@ public sealed class TaskService(
|
||||
var task = await taskRepo.GetByIdAsync(id, ct);
|
||||
if (task is null) return new TaskOperationResult(TaskOperationOutcome.NotFound);
|
||||
|
||||
// Enforce workflow rules
|
||||
var caller = ResolveCaller();
|
||||
if (!TaskStateHelper.CanChangeState(caller, task))
|
||||
return new TaskOperationResult(TaskOperationOutcome.InvalidState);
|
||||
|
||||
task.State = canonical;
|
||||
await taskRepo.UpdateAsync(task, ct);
|
||||
await activityRepo.AddAsync(new ActivityEvent { Type = "task", Message = $"Task \"{task.Title}\" moved to {canonical}", TaskId = task.Id }, ct);
|
||||
@@ -310,6 +483,12 @@ public sealed class TaskService(
|
||||
return new TaskOperationResult(TaskOperationOutcome.Success, task);
|
||||
}
|
||||
|
||||
public Task<int> ResetStaleAsync(int staleHours, CancellationToken ct = default)
|
||||
{
|
||||
var normalizedHours = Math.Max(1, staleHours);
|
||||
return ResetStaleInProgressTasksAsync(TimeSpan.FromHours(normalizedHours), ct);
|
||||
}
|
||||
|
||||
public async Task<int> ResetStaleInProgressTasksAsync(TimeSpan staleThreshold, CancellationToken ct = default)
|
||||
{
|
||||
var all = await taskRepo.GetAllAsync(ct);
|
||||
@@ -351,7 +530,8 @@ public sealed class TaskService(
|
||||
|
||||
private static DashboardTaskDto MapToDto(WorkTask t) => new(
|
||||
t.Id, t.Title, t.Detail, t.Source, t.State, t.Priority, t.AssignedTo,
|
||||
t.ParentTaskId, t.DueDate, t.CreatedAt, t.UpdatedAt);
|
||||
t.ParentTaskId, t.DueDate, t.CreatedAt, t.UpdatedAt,
|
||||
t.IsAgentTask, t.ExpectedFrom);
|
||||
|
||||
/// <summary>
|
||||
/// Validates AssignedTo — only recognized agent values are accepted.
|
||||
@@ -365,16 +545,40 @@ public sealed class TaskService(
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates status-change notifications when a task moves to Review or Blocked.
|
||||
/// Resolves the caller identity from the HTTP context.
|
||||
/// Reads the X-Agent-Id header for agent calls, falls back to JWT name.
|
||||
/// Outside HTTP context → "nexus-system" (allowed for internal Cron/ResetStale ops).
|
||||
/// </summary>
|
||||
private string ResolveCaller()
|
||||
{
|
||||
var httpContext = httpContextAccessor.HttpContext;
|
||||
if (httpContext is null) return "nexus-system"; // internal system ops allowed
|
||||
|
||||
var agentHeader = httpContext.Request.Headers["X-Agent-Id"].FirstOrDefault();
|
||||
if (!string.IsNullOrWhiteSpace(agentHeader))
|
||||
return agentHeader.Trim().ToLowerInvariant();
|
||||
|
||||
var user = httpContext.User;
|
||||
var nameClaim = user?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
|
||||
return nameClaim?.ToLowerInvariant() ?? "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates status-change notifications when a task moves to a new state.
|
||||
/// - Wenn Bao ändert → Iris benachrichtigen
|
||||
/// - Wenn Iris ändert → Bao benachrichtigen
|
||||
/// - Review/Blocked bekommen spezifische Töne
|
||||
/// </summary>
|
||||
private async Task CreateStatusChangeNotificationsAsync(WorkTask task, string canonical, CancellationToken ct)
|
||||
{
|
||||
var caller = ResolveCaller();
|
||||
|
||||
if (string.Equals(canonical, "Review", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await notificationService.CreateAsync(
|
||||
"task_review",
|
||||
$"Task zur Überprüfung: {task.Title}",
|
||||
$"Status auf Review geändert von {task.AssignedTo ?? "unbekannt"}",
|
||||
$"Status auf Review geändert von {caller}",
|
||||
"bao",
|
||||
task.Id,
|
||||
ct);
|
||||
@@ -384,10 +588,34 @@ public sealed class TaskService(
|
||||
await notificationService.CreateAsync(
|
||||
"task_blocked",
|
||||
$"Aufgabe blockiert: {task.Title}",
|
||||
"Die Task konnte nicht abgeschlossen werden und wurde blockiert.",
|
||||
$"Die Task wurde von {caller} auf Blockiert gesetzt.",
|
||||
"iris",
|
||||
task.Id,
|
||||
ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Allgemeine Statusänderung: Gegenüber benachrichtigen
|
||||
if (caller == "bao")
|
||||
{
|
||||
await notificationService.CreateAsync(
|
||||
"task_status_changed",
|
||||
$"Bao hat Status geändert: {task.Title}",
|
||||
$"Status → {canonical}",
|
||||
"iris",
|
||||
task.Id,
|
||||
ct);
|
||||
}
|
||||
else if (caller == "iris")
|
||||
{
|
||||
await notificationService.CreateAsync(
|
||||
"task_status_changed",
|
||||
$"Iris hat Status geändert: {task.Title}",
|
||||
$"Status → {canonical}",
|
||||
"bao",
|
||||
task.Id,
|
||||
ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user