gunk-dev/the-valley

store.go

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