From 13ee0d7114d28ce1d07e3a13dd90467d5f1c18f5 Mon Sep 17 00:00:00 2001 From: Mathias Bergqvist Date: Fri, 17 Apr 2026 20:45:22 +0200 Subject: [PATCH] feat: add retrospective MCP tool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds internal/skills/retrospective/ — an MCP skill that reads a session log and dispatches a worker subprocess (via ExecutorFn) to identify learnings and write them to the brain. Follows the same executor pattern as the TDD skill. Co-Authored-By: Claude Sonnet 4.6 --- internal/skills/retrospective/handlers.go | 70 +++++++++++++++++++ .../skills/retrospective/handlers_test.go | 49 +++++++++++++ internal/skills/retrospective/skill.go | 50 +++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 internal/skills/retrospective/handlers.go create mode 100644 internal/skills/retrospective/handlers_test.go create mode 100644 internal/skills/retrospective/skill.go diff --git a/internal/skills/retrospective/handlers.go b/internal/skills/retrospective/handlers.go new file mode 100644 index 0000000..c1e3326 --- /dev/null +++ b/internal/skills/retrospective/handlers.go @@ -0,0 +1,70 @@ +// internal/skills/retrospective/handlers.go +package retrospective + +import ( + "context" + "encoding/json" + "fmt" + + iexec "github.com/mathiasbq/supervisor/internal/exec" + "github.com/mathiasbq/supervisor/internal/session" +) + +type retroArgs struct { + SessionID string `json:"session_id"` + Model string `json:"model,omitempty"` +} + +// Handle dispatches the retrospective tool call. +func (s *Skill) Handle(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, error) { + if tool != "retrospective" { + return nil, fmt.Errorf("unknown retrospective tool: %s", tool) + } + var a retroArgs + if err := json.Unmarshal(args, &a); err != nil { + return nil, fmt.Errorf("parse args: %w", err) + } + if a.SessionID == "" { + return nil, fmt.Errorf("session_id is required") + } + + model := a.Model + if model == "" { + model = s.cfg.DefaultModel + } + + // Read session log entries (empty slice if no log exists yet). + entries, err := session.Read(s.cfg.SessionsDir, a.SessionID) + if err != nil { + return nil, fmt.Errorf("read session log: %w", err) + } + + logJSON, err := json.MarshalIndent(entries, "", " ") + if err != nil { + return nil, fmt.Errorf("marshal session log: %w", err) + } + + taskPrompt := fmt.Sprintf( + "SESSION_ID: %s\n\nSESSION_LOG:\n%s\n\nReview this session log. Identify what is novel or worth preserving as organizational knowledge. Write structured entries to brain/raw/ via brain_write. Return JSON result when done.", + a.SessionID, string(logJSON), + ) + + if s.cfg.ExecutorFn == nil { + return nil, fmt.Errorf("no executor configured") + } + result, err := s.cfg.ExecutorFn(ctx, iexec.Request{ + SkillPrompt: s.cfg.SkillPrompt, + TaskPrompt: taskPrompt, + Model: model, + Tools: "Bash,Read,Write", + }) + if err != nil { + return nil, fmt.Errorf("retrospective worker: %w", err) + } + + b, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("marshal result: %w", err) + } + return b, nil +} diff --git a/internal/skills/retrospective/handlers_test.go b/internal/skills/retrospective/handlers_test.go new file mode 100644 index 0000000..f7bca54 --- /dev/null +++ b/internal/skills/retrospective/handlers_test.go @@ -0,0 +1,49 @@ +// internal/skills/retrospective/handlers_test.go +package retrospective_test + +import ( + "context" + "encoding/json" + "testing" + + iexec "github.com/mathiasbq/supervisor/internal/exec" + "github.com/mathiasbq/supervisor/internal/skills/retrospective" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestHandle_Retrospective_RequiresSessionID(t *testing.T) { + s := retrospective.New(retrospective.Config{}) + _, err := s.Handle(context.Background(), "retrospective", json.RawMessage(`{}`)) + assert.Error(t, err) + assert.Contains(t, err.Error(), "session_id") +} + +func TestHandle_Retrospective_BuildsPromptWithSessionLog(t *testing.T) { + var capturedReq iexec.Request + s := retrospective.New(retrospective.Config{ + SkillPrompt: "retrospective discipline", + DefaultModel: "ollama/test", + SessionsDir: t.TempDir(), // empty dir, no session file — that's OK, session.Read returns nil + ExecutorFn: func(_ context.Context, req iexec.Request) (iexec.Result, error) { + capturedReq = req + return iexec.Result{ + Status: "pass", + Phase: "retrospective", + Skill: "retrospective", + Verified: true, + Message: "wrote 2 entries to brain", + }, nil + }, + }) + + args, _ := json.Marshal(map[string]string{"session_id": "empty-session"}) + out, err := s.Handle(context.Background(), "retrospective", args) + require.NoError(t, err) + + var result iexec.Result + require.NoError(t, json.Unmarshal(out, &result)) + assert.Equal(t, "pass", result.Status) + assert.Contains(t, capturedReq.SkillPrompt, "retrospective discipline") + assert.Contains(t, capturedReq.TaskPrompt, "empty-session") +} diff --git a/internal/skills/retrospective/skill.go b/internal/skills/retrospective/skill.go new file mode 100644 index 0000000..6c8b582 --- /dev/null +++ b/internal/skills/retrospective/skill.go @@ -0,0 +1,50 @@ +// internal/skills/retrospective/skill.go +package retrospective + +import ( + "context" + "encoding/json" + + iexec "github.com/mathiasbq/supervisor/internal/exec" + "github.com/mathiasbq/supervisor/internal/registry" +) + +// ExecutorFn allows injecting a test double for the subprocess executor. +type ExecutorFn func(ctx context.Context, req iexec.Request) (iexec.Result, error) + +// Config holds retrospective skill configuration. +type Config struct { + SkillPrompt string // content of retrospective.md + DefaultModel string // model to use when not specified in args + SessionsDir string // path to brain/sessions/ + ExecutorFn ExecutorFn // injected executor +} + +// Skill implements registry.Skill for the retrospective tool. +type Skill struct { + cfg Config +} + +// New constructs a retrospective Skill. +func New(cfg Config) *Skill { return &Skill{cfg: cfg} } + +// Name returns the skill name. +func (s *Skill) Name() string { return "retrospective" } + +// Tools returns the MCP tool definitions. +func (s *Skill) Tools() []registry.ToolDef { + return []registry.ToolDef{ + { + Name: "retrospective", + Description: "Run a retrospective on a completed session. Reads the session log, identifies novel learnings, and writes structured entries to the brain for ingestion. Call at the end of each coding session.", + InputSchema: json.RawMessage(`{ + "type": "object", + "required": ["session_id"], + "properties": { + "session_id": {"type": "string"}, + "model": {"type": "string"} + } + }`), + }, + } +}