From 582ca5019b398326ea9207cd167e5da132bca314 Mon Sep 17 00:00:00 2001 From: Mathias Bergqvist Date: Sun, 19 Apr 2026 10:18:23 +0200 Subject: [PATCH] feat(tdd): inject session history into green and refactor worker prompts Adds SessionsDir to tdd.Config, session_id to tool input schemas, and a prependHistory method that reads the session JSONL log and prepends a formatted history block to the task prompt before worker invocation. Co-Authored-By: Claude Sonnet 4.6 --- cmd/supervisor/main.go | 1 + internal/skills/tdd/handlers.go | 20 ++++++++++++++ internal/skills/tdd/handlers_test.go | 40 ++++++++++++++++++++++++++++ internal/skills/tdd/skill.go | 3 +++ 4 files changed, 64 insertions(+) diff --git a/cmd/supervisor/main.go b/cmd/supervisor/main.go index a57494c..4d69c42 100644 --- a/cmd/supervisor/main.go +++ b/cmd/supervisor/main.go @@ -67,6 +67,7 @@ func main() { SkillPrompt: string(tddPrompt), DefaultModel: models.Resolve("tdd", ""), ExecutorFn: executor.Run, + SessionsDir: cfg.SessionsDir, })) reg.Register(brain.New(brain.Config{ IngestBaseURL: cfg.IngestBaseURL, diff --git a/internal/skills/tdd/handlers.go b/internal/skills/tdd/handlers.go index 98d904a..89ce09d 100644 --- a/internal/skills/tdd/handlers.go +++ b/internal/skills/tdd/handlers.go @@ -6,6 +6,7 @@ import ( "fmt" iexec "github.com/mathiasbq/supervisor/internal/exec" + "github.com/mathiasbq/supervisor/internal/session" ) func (s *Skill) Handle(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, error) { @@ -51,6 +52,7 @@ type greenArgs struct { TestPath string `json:"test_path"` Model string `json:"model"` TestCmd string `json:"test_cmd"` + SessionID string `json:"session_id"` } func (s *Skill) handleGreen(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) { @@ -68,6 +70,7 @@ func (s *Skill) handleGreen(ctx context.Context, raw json.RawMessage) (json.RawM "phase: green\nproject_root: %s\ntest_path: %s\nmodel: %s\ntest_cmd: %s", args.ProjectRoot, args.TestPath, s.resolveModel(args.Model), args.TestCmd, ) + task = s.prependHistory(args.SessionID, "green", task) return s.execute(ctx, task) } @@ -77,6 +80,7 @@ type refactorArgs struct { ImplPath string `json:"impl_path"` Model string `json:"model"` TestCmd string `json:"test_cmd"` + SessionID string `json:"session_id"` } func (s *Skill) handleRefactor(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) { @@ -97,9 +101,25 @@ func (s *Skill) handleRefactor(ctx context.Context, raw json.RawMessage) (json.R "phase: refactor\nproject_root: %s\ntest_path: %s\nimpl_path: %s\nmodel: %s\ntest_cmd: %s", args.ProjectRoot, args.TestPath, args.ImplPath, s.resolveModel(args.Model), args.TestCmd, ) + task = s.prependHistory(args.SessionID, "refactor", task) return s.execute(ctx, task) } +func (s *Skill) prependHistory(sessionID, currentPhase, task string) string { + if sessionID == "" || s.cfg.SessionsDir == "" { + return task + } + entries, err := session.Read(s.cfg.SessionsDir, sessionID) + if err != nil || len(entries) == 0 { + return task + } + history := session.FormatHistory(entries, currentPhase) + if history == "" { + return task + } + return history + "\n---\n\n" + task +} + func (s *Skill) resolveModel(override string) string { if override != "" { return override diff --git a/internal/skills/tdd/handlers_test.go b/internal/skills/tdd/handlers_test.go index d0490b6..e299cbb 100644 --- a/internal/skills/tdd/handlers_test.go +++ b/internal/skills/tdd/handlers_test.go @@ -5,6 +5,8 @@ import ( "encoding/json" "testing" + iexec "github.com/mathiasbq/supervisor/internal/exec" + "github.com/mathiasbq/supervisor/internal/session" "github.com/mathiasbq/supervisor/internal/skills/tdd" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -41,5 +43,43 @@ func TestTDDRedRequiresSpec(t *testing.T) { assert.ErrorContains(t, err, "spec") } +func TestTDDGreenInjectsSessionHistory(t *testing.T) { + sessDir := t.TempDir() + require.NoError(t, session.Append(sessDir, "sess-1", session.Entry{ + SessionID: "sess-1", Skill: "tdd", Phase: "red", FinalStatus: "pass", + FilePath: "internal/foo/foo_test.go", + Message: "wrote failing test for Foo", + })) + + var capturedPrompt string + fakeFn := func(_ context.Context, req iexec.Request) (iexec.Result, error) { + capturedPrompt = req.TaskPrompt + return iexec.Result{Status: "pass", Phase: "green", Skill: "tdd", Verified: true, ModelUsed: "self", Message: "ok"}, nil + } + + sk := tdd.New(tdd.Config{SkillPrompt: "tdd", ExecutorFn: fakeFn, SessionsDir: sessDir}) + _, err := sk.Handle(context.Background(), "tdd_green", json.RawMessage( + `{"project_root":"/tmp","test_path":"internal/foo/foo_test.go","test_cmd":"go test ./...","session_id":"sess-1"}`, + )) + require.NoError(t, err) + assert.Contains(t, capturedPrompt, "## Session history") + assert.Contains(t, capturedPrompt, "wrote failing test for Foo") +} + +func TestTDDGreenNoHistoryWhenSessionIDEmpty(t *testing.T) { + var capturedPrompt string + fakeFn := func(_ context.Context, req iexec.Request) (iexec.Result, error) { + capturedPrompt = req.TaskPrompt + return iexec.Result{Status: "pass", Phase: "green", Skill: "tdd", Verified: true, ModelUsed: "self", Message: "ok"}, nil + } + + sk := tdd.New(tdd.Config{SkillPrompt: "tdd", ExecutorFn: fakeFn, SessionsDir: t.TempDir()}) + _, err := sk.Handle(context.Background(), "tdd_green", json.RawMessage( + `{"project_root":"/tmp","test_path":"internal/foo/foo_test.go"}`, + )) + require.NoError(t, err) + assert.NotContains(t, capturedPrompt, "## Session history") +} + // Ensure require is used (avoids import error). var _ = require.New diff --git a/internal/skills/tdd/skill.go b/internal/skills/tdd/skill.go index c3fdbc6..5d74417 100644 --- a/internal/skills/tdd/skill.go +++ b/internal/skills/tdd/skill.go @@ -16,6 +16,7 @@ type Config struct { SkillPrompt string ExecutorFn ExecutorFn // nil = no executor (tests that don't reach execute()) DefaultModel string + SessionsDir string // optional: path to brain/sessions/ for history injection } type Skill struct { @@ -63,6 +64,7 @@ func (s *Skill) Tools() []registry.ToolDef { "test_path": strProp, "model": strProp, "test_cmd": strProp, + "session_id": strProp, }, ), }, @@ -77,6 +79,7 @@ func (s *Skill) Tools() []registry.ToolDef { "impl_path": strProp, "model": strProp, "test_cmd": strProp, + "session_id": strProp, }, ), },