feat(session): export PrependHistory for shared use across skills
This commit is contained in:
@@ -36,3 +36,21 @@ func FormatHistory(entries []Entry, excludePhase string) string {
|
|||||||
}
|
}
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrependHistory reads the session log for sessionID and prepends a formatted
|
||||||
|
// history block to task. Returns task unchanged if sessionID or sessionsDir is
|
||||||
|
// empty, or if no prior entries exist.
|
||||||
|
func PrependHistory(sessionsDir, sessionID, currentPhase, task string) string {
|
||||||
|
if sessionID == "" || sessionsDir == "" {
|
||||||
|
return task
|
||||||
|
}
|
||||||
|
entries, err := Read(sessionsDir, sessionID)
|
||||||
|
if err != nil || len(entries) == 0 {
|
||||||
|
return task
|
||||||
|
}
|
||||||
|
history := FormatHistory(entries, currentPhase)
|
||||||
|
if history == "" {
|
||||||
|
return task
|
||||||
|
}
|
||||||
|
return history + "\n---\n\n" + task
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@
|
|||||||
package session_test
|
package session_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mathiasbq/supervisor/internal/session"
|
"github.com/mathiasbq/supervisor/internal/session"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFormatHistoryEmpty(t *testing.T) {
|
func TestFormatHistoryEmpty(t *testing.T) {
|
||||||
@@ -39,3 +41,45 @@ func TestFormatHistoryExcludesCurrentPhase(t *testing.T) {
|
|||||||
assert.Contains(t, result, "red done")
|
assert.Contains(t, result, "red done")
|
||||||
assert.NotContains(t, result, "green done")
|
assert.NotContains(t, result, "green done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrependHistoryNoSessionID(t *testing.T) {
|
||||||
|
result := session.PrependHistory("", "", "review", "do the task")
|
||||||
|
assert.Equal(t, "do the task", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrependHistoryNoLog(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
result := session.PrependHistory(dir, "sess-abc", "review", "do the task")
|
||||||
|
assert.Equal(t, "do the task", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrependHistoryPrependsHistory(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
entry := session.Entry{
|
||||||
|
SessionID: "sess-abc", Skill: "tdd", Phase: "red",
|
||||||
|
FinalStatus: "pass", Message: "wrote test",
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
}
|
||||||
|
require.NoError(t, session.Append(dir, "sess-abc", entry))
|
||||||
|
|
||||||
|
result := session.PrependHistory(dir, "sess-abc", "review", "do the task")
|
||||||
|
assert.Contains(t, result, "## Session history")
|
||||||
|
assert.Contains(t, result, "wrote test")
|
||||||
|
assert.True(t, strings.HasSuffix(result, "do the task"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrependHistoryExcludesCurrentPhase(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
require.NoError(t, session.Append(dir, "sess-abc", session.Entry{
|
||||||
|
SessionID: "sess-abc", Skill: "tdd", Phase: "red",
|
||||||
|
FinalStatus: "pass", Message: "red done", Timestamp: time.Now(),
|
||||||
|
}))
|
||||||
|
require.NoError(t, session.Append(dir, "sess-abc", session.Entry{
|
||||||
|
SessionID: "sess-abc", Skill: "tdd", Phase: "green",
|
||||||
|
FinalStatus: "pass", Message: "green done", Timestamp: time.Now(),
|
||||||
|
}))
|
||||||
|
|
||||||
|
result := session.PrependHistory(dir, "sess-abc", "green", "do the task")
|
||||||
|
assert.Contains(t, result, "red done")
|
||||||
|
assert.NotContains(t, result, "green done")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user