| 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 | states, _, _, err := scanStates(dir) |
| 41 | return states, err |
| 42 | } |
| 43 | |
| 44 | // scanStates lists the run states in dir, newest first, names the records |
| 45 | // it skipped because they don't parse, and returns the read errors of |
| 46 | // those it could not read. saveState rewrites a record in place, so a |
| 47 | // reader can catch one half written; a record removed since the listing is |
| 48 | // none of these. |
| 49 | func scanStates(dir string) ([]*State, []string, []error, error) { |
| 50 | entries, err := os.ReadDir(dir) |
| 51 | if err != nil { |
| 52 | if os.IsNotExist(err) { |
| 53 | return nil, nil, nil, nil |
| 54 | } |
| 55 | return nil, nil, nil, fmt.Errorf("reading state dir: %w", err) |
| 56 | } |
| 57 | |
| 58 | var states []*State |
| 59 | var unparsed []string |
| 60 | var unreadable []error |
| 61 | for _, e := range entries { |
| 62 | if e.IsDir() || !strings.HasSuffix(e.Name(), ".json") { |
| 63 | continue |
| 64 | } |
| 65 | data, err := os.ReadFile(filepath.Join(dir, e.Name())) |
| 66 | if err != nil { |
| 67 | if !os.IsNotExist(err) { |
| 68 | unreadable = append(unreadable, err) |
| 69 | } |
| 70 | continue |
| 71 | } |
| 72 | var st State |
| 73 | if err := json.Unmarshal(data, &st); err != nil { |
| 74 | unparsed = append(unparsed, e.Name()) |
| 75 | continue |
| 76 | } |
| 77 | states = append(states, &st) |
| 78 | } |
| 79 | |
| 80 | sort.Slice(states, func(i, j int) bool { |
| 81 | return states[i].CreatedAt > states[j].CreatedAt |
| 82 | }) |
| 83 | |
| 84 | return states, unparsed, unreadable, nil |
| 85 | } |
| 86 | |
| 87 | func deleteState(dir string, id string) error { |
| 88 | path := filepath.Join(dir, id+".json") |
| 89 | return os.Remove(path) |
| 90 | } |
| 91 | |
| 92 | // StateStore defines the interface for persisting run state. |
| 93 | type StateStore interface { |
| 94 | Save(s *State) error |
| 95 | Load(id string) (*State, error) |
| 96 | List() ([]*State, error) |
| 97 | Delete(id string) error |
| 98 | LogDir() string |
| 99 | StateDir() string |
| 100 | EnsureDirs() error |
| 101 | } |
| 102 | |
| 103 | // PromptDir holds the prompt files workers read on stdin, and the settings |
| 104 | // file a Claude coordinator loads with --settings, beside the store's logs/. |
| 105 | // The prompts are kept after the run as the audit copy of each brief. |
| 106 | func PromptDir(s StateStore) string { |
| 107 | return filepath.Join(filepath.Dir(s.LogDir()), "prompts") |
| 108 | } |
| 109 | |
| 110 | // WritePromptFile writes text owner-only; briefs can quote private context. |
| 111 | func WritePromptFile(path, text string) error { |
| 112 | if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 113 | return fmt.Errorf("creating prompt dir: %w", err) |
| 114 | } |
| 115 | if err := os.WriteFile(path, []byte(text), 0o600); err != nil { |
| 116 | return fmt.Errorf("writing prompt file: %w", err) |
| 117 | } |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | // GitDirStore implements StateStore in a clone's standalone run store |
| 122 | // (location's StandaloneStore), for runs started outside a session. |
| 123 | type GitDirStore struct { |
| 124 | dir string |
| 125 | } |
| 126 | |
| 127 | // NewGitDirStore creates a GitDirStore keeping runs/ and logs/ in dir. |
| 128 | func NewGitDirStore(dir string) *GitDirStore { |
| 129 | return &GitDirStore{dir: dir} |
| 130 | } |
| 131 | |
| 132 | func (s *GitDirStore) StateDir() string { |
| 133 | return filepath.Join(s.dir, "runs") |
| 134 | } |
| 135 | |
| 136 | func (s *GitDirStore) LogDir() string { |
| 137 | return filepath.Join(s.dir, "logs") |
| 138 | } |
| 139 | |
| 140 | func (s *GitDirStore) EnsureDirs() error { |
| 141 | if err := os.MkdirAll(s.StateDir(), 0o755); err != nil { |
| 142 | return fmt.Errorf("creating state dir: %w", err) |
| 143 | } |
| 144 | if err := os.MkdirAll(s.LogDir(), 0o755); err != nil { |
| 145 | return fmt.Errorf("creating log dir: %w", err) |
| 146 | } |
| 147 | return nil |
| 148 | } |
| 149 | |
| 150 | func (s *GitDirStore) Save(st *State) error { |
| 151 | return saveState(s.StateDir(), st) |
| 152 | } |
| 153 | |
| 154 | func (s *GitDirStore) Load(id string) (*State, error) { |
| 155 | return loadState(s.StateDir(), id) |
| 156 | } |
| 157 | |
| 158 | func (s *GitDirStore) List() ([]*State, error) { |
| 159 | return listStates(s.StateDir()) |
| 160 | } |
| 161 | |
| 162 | func (s *GitDirStore) Delete(id string) error { |
| 163 | return deleteState(s.StateDir(), id) |
| 164 | } |