| 1 | package run |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "sort" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // Shared helpers used by both GitDirStore and HomeDirStore. |
| 13 | |
| 14 | func saveState(dir string, st *State) error { |
| 15 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 16 | return err |
| 17 | } |
| 18 | data, err := json.MarshalIndent(st, "", " ") |
| 19 | if err != nil { |
| 20 | return fmt.Errorf("marshaling state: %w", err) |
| 21 | } |
| 22 | path := filepath.Join(dir, st.ID+".json") |
| 23 | return os.WriteFile(path, data, 0o644) |
| 24 | } |
| 25 | |
| 26 | func loadState(dir string, id string) (*State, error) { |
| 27 | path := filepath.Join(dir, id+".json") |
| 28 | data, err := os.ReadFile(path) |
| 29 | if err != nil { |
| 30 | return nil, fmt.Errorf("reading state file: %w", err) |
| 31 | } |
| 32 | var st State |
| 33 | if err := json.Unmarshal(data, &st); err != nil { |
| 34 | return nil, fmt.Errorf("parsing state file: %w", err) |
| 35 | } |
| 36 | return &st, nil |
| 37 | } |
| 38 | |
| 39 | func listStates(dir string) ([]*State, error) { |
| 40 | entries, err := os.ReadDir(dir) |
| 41 | if err != nil { |
| 42 | if os.IsNotExist(err) { |
| 43 | return nil, nil |
| 44 | } |
| 45 | return nil, fmt.Errorf("reading state dir: %w", err) |
| 46 | } |
| 47 | |
| 48 | var states []*State |
| 49 | for _, e := range entries { |
| 50 | if e.IsDir() || !strings.HasSuffix(e.Name(), ".json") { |
| 51 | continue |
| 52 | } |
| 53 | id := strings.TrimSuffix(e.Name(), ".json") |
| 54 | st, err := loadState(dir, id) |
| 55 | if err != nil { |
| 56 | continue // skip corrupt files |
| 57 | } |
| 58 | states = append(states, st) |
| 59 | } |
| 60 | |
| 61 | sort.Slice(states, func(i, j int) bool { |
| 62 | return states[i].CreatedAt > states[j].CreatedAt |
| 63 | }) |
| 64 | |
| 65 | return states, nil |
| 66 | } |
| 67 | |
| 68 | func deleteState(dir string, id string) error { |
| 69 | path := filepath.Join(dir, id+".json") |
| 70 | return os.Remove(path) |
| 71 | } |
| 72 | |
| 73 | // StateStore defines the interface for persisting run state. |
| 74 | type StateStore interface { |
| 75 | Save(s *State) error |
| 76 | Load(id string) (*State, error) |
| 77 | List() ([]*State, error) |
| 78 | Delete(id string) error |
| 79 | LogDir() string |
| 80 | StateDir() string |
| 81 | EnsureDirs() error |
| 82 | } |
| 83 | |
| 84 | // PromptDir holds the prompt files workers read on stdin, and the settings |
| 85 | // file a Claude coordinator loads with --settings, beside the store's logs/. |
| 86 | // The prompts are kept after the run as the audit copy of each brief. |
| 87 | func PromptDir(s StateStore) string { |
| 88 | return filepath.Join(filepath.Dir(s.LogDir()), "prompts") |
| 89 | } |
| 90 | |
| 91 | // WritePromptFile writes text owner-only; briefs can quote private context. |
| 92 | func WritePromptFile(path, text string) error { |
| 93 | if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 94 | return fmt.Errorf("creating prompt dir: %w", err) |
| 95 | } |
| 96 | if err := os.WriteFile(path, []byte(text), 0o600); err != nil { |
| 97 | return fmt.Errorf("writing prompt file: %w", err) |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | // GitDirStore implements StateStore using the .git/klaus/ directory structure. |
| 103 | type GitDirStore struct { |
| 104 | gitCommonDir string |
| 105 | } |
| 106 | |
| 107 | // NewGitDirStore creates a new GitDirStore rooted at the given git common directory. |
| 108 | func NewGitDirStore(gitCommonDir string) *GitDirStore { |
| 109 | return &GitDirStore{gitCommonDir: gitCommonDir} |
| 110 | } |
| 111 | |
| 112 | func (s *GitDirStore) StateDir() string { |
| 113 | return filepath.Join(s.gitCommonDir, "klaus", "runs") |
| 114 | } |
| 115 | |
| 116 | func (s *GitDirStore) LogDir() string { |
| 117 | return filepath.Join(s.gitCommonDir, "klaus", "logs") |
| 118 | } |
| 119 | |
| 120 | func (s *GitDirStore) EnsureDirs() error { |
| 121 | if err := os.MkdirAll(s.StateDir(), 0o755); err != nil { |
| 122 | return fmt.Errorf("creating state dir: %w", err) |
| 123 | } |
| 124 | if err := os.MkdirAll(s.LogDir(), 0o755); err != nil { |
| 125 | return fmt.Errorf("creating log dir: %w", err) |
| 126 | } |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | func (s *GitDirStore) Save(st *State) error { |
| 131 | return saveState(s.StateDir(), st) |
| 132 | } |
| 133 | |
| 134 | func (s *GitDirStore) Load(id string) (*State, error) { |
| 135 | return loadState(s.StateDir(), id) |
| 136 | } |
| 137 | |
| 138 | func (s *GitDirStore) List() ([]*State, error) { |
| 139 | return listStates(s.StateDir()) |
| 140 | } |
| 141 | |
| 142 | func (s *GitDirStore) Delete(id string) error { |
| 143 | return deleteState(s.StateDir(), id) |
| 144 | } |