From 9d133e227bd9e449a00831333046d2328c252b0c Mon Sep 17 00:00:00 2001 From: mika kuns Date: Thu, 4 Jun 2026 15:51:57 +0200 Subject: [PATCH] feat(mcp): add SuggestImprovement tool (server-stamped, one layer deep) --- .../Runner/TaskRunMcpService.cs | 47 ++++++++++++++ .../SuggestImprovementTests.cs | 61 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/ClaudeDo.Worker/Runner/TaskRunMcpService.cs create mode 100644 tests/ClaudeDo.Worker.Tests/SuggestImprovementTests.cs diff --git a/src/ClaudeDo.Worker/Runner/TaskRunMcpService.cs b/src/ClaudeDo.Worker/Runner/TaskRunMcpService.cs new file mode 100644 index 0000000..1b3735d --- /dev/null +++ b/src/ClaudeDo.Worker/Runner/TaskRunMcpService.cs @@ -0,0 +1,47 @@ +using System.ComponentModel; +using ClaudeDo.Data.Repositories; +using ClaudeDo.Worker.Hub; +using ModelContextProtocol.Server; + +namespace ClaudeDo.Worker.Runner; + +public sealed record SuggestedImprovementDto(string ChildTaskId); + +[McpServerToolType] +public sealed class TaskRunMcpService +{ + private readonly TaskRepository _tasks; + private readonly TaskRunMcpContextAccessor _ctx; + private readonly HubBroadcaster _broadcaster; + + public TaskRunMcpService(TaskRepository tasks, TaskRunMcpContextAccessor ctx, HubBroadcaster broadcaster) + { + _tasks = tasks; + _ctx = ctx; + _broadcaster = broadcaster; + } + + [McpServerTool, Description( + "File an out-of-scope improvement as a child task of the current task. The child runs " + + "automatically after this task finishes and is surfaced for review alongside it. Use ONLY " + + "for work that is genuinely outside this task's scope (a refactor, follow-up, or tech debt) " + + "— never for work that belongs to the current task.")] + public async Task SuggestImprovement( + string title, + string description, + CancellationToken cancellationToken) + { + var callerId = _ctx.Current.CallerTaskId; + var caller = await _tasks.GetByIdAsync(callerId, cancellationToken) + ?? throw new InvalidOperationException("Calling task not found."); + if (caller.ParentTaskId is not null) + throw new InvalidOperationException( + "A child task cannot suggest further improvements (improvements are one layer deep)."); + + var child = await _tasks.CreateChildAsync( + callerId, title, description, commitType: null, createdBy: callerId, cancellationToken); + await _broadcaster.TaskUpdated(child.Id); + await _broadcaster.TaskUpdated(callerId); + return new SuggestedImprovementDto(child.Id); + } +} diff --git a/tests/ClaudeDo.Worker.Tests/SuggestImprovementTests.cs b/tests/ClaudeDo.Worker.Tests/SuggestImprovementTests.cs new file mode 100644 index 0000000..a1e0ed0 --- /dev/null +++ b/tests/ClaudeDo.Worker.Tests/SuggestImprovementTests.cs @@ -0,0 +1,61 @@ +using ClaudeDo.Data.Models; +using ClaudeDo.Data.Repositories; +using ClaudeDo.Worker.Hub; +using ClaudeDo.Worker.Runner; +using ClaudeDo.Worker.Tests.Infrastructure; +using ClaudeDo.Worker.Tests.Services; +using Microsoft.AspNetCore.Http; +using TaskStatus = ClaudeDo.Data.Models.TaskStatus; +using Xunit; + +namespace ClaudeDo.Worker.Tests; + +public sealed class SuggestImprovementTests : IDisposable +{ + private readonly DbFixture _db = new(); + public void Dispose() => _db.Dispose(); + + private static TaskRunMcpContextAccessor AccessorFor(string callerTaskId) + { + var http = new HttpContextAccessor { HttpContext = new DefaultHttpContext() }; + http.HttpContext!.Items["TaskRunContext"] = new TaskRunMcpContext { CallerTaskId = callerTaskId }; + return new TaskRunMcpContextAccessor(http); + } + + private async Task SeedCallerAsync(string id, string? parentId) + { + using var ctx = _db.CreateContext(); + if (!ctx.Lists.Any()) + ctx.Lists.Add(new ListEntity { Id = "l1", Name = "L", CreatedAt = DateTime.UtcNow }); + ctx.Tasks.Add(new TaskEntity { Id = id, ListId = "l1", Title = "Caller", + Status = TaskStatus.Running, ParentTaskId = parentId, CommitType = "feat", CreatedAt = DateTime.UtcNow }); + await ctx.SaveChangesAsync(); + } + + [Fact] + public async Task SuggestImprovement_stamps_parent_createdBy_status_and_list() + { + await SeedCallerAsync("caller", parentId: null); + using var ctx = _db.CreateContext(); + var svc = new TaskRunMcpService(new TaskRepository(ctx), AccessorFor("caller"), + new HubBroadcaster(new FakeHubContext())); + var dto = await svc.SuggestImprovement("Refactor X", "details", default); + var child = await new TaskRepository(ctx).GetByIdAsync(dto.ChildTaskId); + Assert.Equal("caller", child!.ParentTaskId); + Assert.Equal("caller", child.CreatedBy); + Assert.Equal(TaskStatus.Idle, child.Status); + Assert.Equal("l1", child.ListId); + } + + [Fact] + public async Task SuggestImprovement_rejects_when_caller_is_a_child() + { + await SeedCallerAsync("parent", parentId: null); + await SeedCallerAsync("child", parentId: "parent"); + using var ctx = _db.CreateContext(); + var svc = new TaskRunMcpService(new TaskRepository(ctx), AccessorFor("child"), + new HubBroadcaster(new FakeHubContext())); + await Assert.ThrowsAsync( + () => svc.SuggestImprovement("nested", "x", default)); + } +}