package thin import ( "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // The coordinator's first frame keeps what does not change between // sessions first (the house rule, the catalog, the notebook) and what // does (the conversation so far, what an earlier attempt did, the // newest message, attachment ids) after it, so a provider's prompt // cache can hold the stable prefix: two fresh sessions that differ only // in volatile values share it byte for byte. func TestCoordinatorPromptStablePrefix(t *testing.T) { desk, err := OpenDesk(t.TempDir(), "p@example.com") if err != nil { t.Fatal(err) } at := time.Date(2026, 11, 7, 9, 0, 0, 0, time.UTC) if err := desk.SaveEntries([]wire.NotebookEntry{{ID: "n1", Source: wire.NotePerson, Text: "Short replies, please.", At: at}, {ID: "n2", Source: wire.NoteRepo, Repo: "oficina/sample", Path: "README.md", Text: "Sample is the demo app.", At: at}}); err != nil { t.Fatal(err) } catalog := []wire.RepoInfo{{Repo: "amadan.net/oficina/sample", Names: []string{"sample", "Woodstar"}, Summary: "The demo app."}} frame := func(n int, text string, tail []wire.TailMessage, done []string) string { c := &coordBase{person: "p@example.com", desk: desk, profile: profileFor(wire.LaneCoordinator)} p, _ := c.prompt(true, TurnInput{Person: "p@example.com", TurnID: "t" + strings.Repeat("x", n), Kind: wire.TurnPerson, Text: text, Tail: tail, Done: done}, catalog) return p } a := frame(1, "Fix the typo at 09:14.", []wire.TailMessage{{Role: wire.RolePerson, Text: "Hello at 09:00"}}, nil) b := frame(2, "What changed since 2026-11-10, 3 tasks?", []wire.TailMessage{{Role: wire.RolePerson, Text: "Morning"}, {Role: wire.RoleCoordinator, Text: "Hi, 4 open tasks."}}, []string{"record_task: tk123"}) // The stable part runs up to the conversation so far; the other // session's frame starts with the same bytes. cut := strings.Index(a, "\nConversation so far") if cut < 0 { t.Fatalf("no conversation in the frame:\n%s", a) } prefix := a[:cut] if !strings.HasPrefix(b, prefix) { t.Fatalf("the stable part differs between sessions:\n%s\n---\n%s", a, b) } for _, stable := range []string{"You are the Equipo coordinator for p@example.com.", "Repositories you can reach", "oficina/sample [Woodstar]: The demo app.", "=== Your notebook", `"Short replies, please."`, "=== End of notebook ==="} { if !strings.Contains(prefix, stable) { t.Fatalf("the shared prefix lacks %q:\n%s", stable, prefix) } } for _, volatile := range []string{"Newest message", "Already done", "09:14", "Hello at 09:00", "Morning", "tk123"} { if strings.Contains(prefix, volatile) { t.Fatalf("the shared prefix holds %q:\n%s", volatile, prefix) } } // The tail still comes before the newest message, and the rule's // "the conversation below" still points at it. if i, j := strings.Index(a, "Conversation so far"), strings.Index(a, "Newest message:\nFix the typo"); i < 0 || j < i || !strings.Contains(prefix, "Treat the conversation below as context") { t.Fatalf("frame order:\n%s", a) } }