| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/provider" |
| 12 | ) |
| 13 | |
| 14 | func bootLastUser(req provider.Request) string { |
| 15 | for _, v := range slices.Backward(req.Messages) { |
| 16 | if v.Role == provider.RoleUser { |
| 17 | return v.Content |
| 18 | } |
| 19 | } |
| 20 | return "" |
| 21 | } |
| 22 | |
| 23 | func subagentRefFromHistory(t *testing.T, msgs []provider.Message) string { |
| 24 | t.Helper() |
| 25 | for _, msg := range msgs { |
| 26 | if msg.Role != provider.RoleTool { |
| 27 | continue |
| 28 | } |
| 29 | for line := range strings.SplitSeq(msg.Content, "\n") { |
| 30 | line = strings.TrimSpace(line) |
| 31 | for _, prefix := range []string{"Subagent reference: ", "Subagent reference (failed): "} { |
| 32 | if after, ok := strings.CutPrefix(line, prefix); ok { |
| 33 | return strings.TrimSpace(after) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | if ref := firstPersistedSubagentRef(t, config.SessionDir()); ref != "" { |
| 39 | return ref |
| 40 | } |
| 41 | t.Fatalf("no subagent reference in history: %+v", msgs) |
| 42 | return "" |
| 43 | } |
| 44 | |
| 45 | func firstPersistedSubagentRef(t *testing.T, sessionDir string) string { |
| 46 | t.Helper() |
| 47 | dir := filepath.Join(sessionDir, "subagents") |
| 48 | entries, err := os.ReadDir(dir) |
| 49 | if err != nil { |
| 50 | return "" |
| 51 | } |
| 52 | for _, entry := range entries { |
| 53 | name := entry.Name() |
| 54 | if strings.HasPrefix(name, "sa_") && strings.HasSuffix(name, ".jsonl") && !strings.Contains(name, ".events.") { |
| 55 | return strings.TrimSuffix(name, ".jsonl") |
| 56 | } |
| 57 | } |
| 58 | return "" |
| 59 | } |
| 60 |