17 // subagentDirName is the directory of the subagents in the directory of
19 subagentDirName = "subagents"
21 // subagentNameDraws is the most names that Create draws before it
23 subagentNameDraws = 1000
26 // newSubagents returns the subagents in dir, one directory for each name. The
27 // clock gives the dates of their messages, and draw gives the names of new
29 func newSubagents(dir string, clock func() time.Time, draw func() string) *Subagents {
34 open: make(map[string]*Subagent),
38 // Subagents holds the subagents of one agent. Each subagent has a directory
39 // named for it, which holds its log. A log takes an exclusive lock, so
40 // Subagents opens the log of a name once and keeps it open until Close. Its
41 // mutex protects the open logs.
42 type Subagents struct {
44 clock func() time.Time
48 open map[string]*Subagent
51 // Create makes the directory and the log of a new subagent. It draws a name
52 // again when the directory of the name exists.
53 func (s *Subagents) Create(ctx context.Context) (*Subagent, error) {
54 err := os.MkdirAll(s.dir, 0o700)
56 return nil, fmt.Errorf("create subagent directory: %w", err)
59 for range subagentNameDraws {
61 err = os.Mkdir(filepath.Join(s.dir, name), 0o700)
62 if errors.Is(err, fs.ErrExist) {
67 return nil, fmt.Errorf("create subagent directory: %w", err)
70 return s.Get(ctx, name)
73 return nil, fmt.Errorf("no free subagent name after %d draws", subagentNameDraws)
76 // Get returns the subagent name with its log open. A name that is not valid or
77 // that has no directory is an unknown agent.
78 func (s *Subagents) Get(ctx context.Context, name string) (*Subagent, error) {
80 defer s.mutex.Unlock()
82 subagent, found := s.open[name]
87 unknown := fmt.Errorf("no agent %s", name)
88 if !subagentNameValid(name) {
92 root := filepath.Join(s.dir, name)
93 info, err := os.Stat(root)
94 if errors.Is(err, fs.ErrNotExist) {
99 return nil, fmt.Errorf("read subagent directory: %w", err)
106 log, err := openLog(ctx, root, s.clock)
108 return nil, fmt.Errorf("open log of %s: %w", name, err)
111 syncer, err := openSyncer(root)
113 return nil, errors.Join(fmt.Errorf("open log of %s: %w", name, err), log.Close())
116 subagent = &Subagent{
122 s.open[name] = subagent
127 // Snapshot flushes and locks the open logs while snapshot reads the subagent
128 // files. snapshot must not call Subagents or an open Subagent.
129 func (s *Subagents) Snapshot(ctx context.Context, snapshot func() error) error {
131 defer s.mutex.Unlock()
133 for _, subagent := range s.open {
134 subagent.mutex.Lock()
135 defer subagent.mutex.Unlock()
137 err := subagent.Flush(ctx)
139 return fmt.Errorf("flush subagent snapshot: %w", err)
145 return fmt.Errorf("copy subagent snapshot: %w", err)
151 // Close makes the writes of each open log durable and closes it.
152 func (s *Subagents) Close() error {
154 defer s.mutex.Unlock()
158 for name, subagent := range s.open {
159 err = errors.Join(err, subagent.close())
167 // Subagent is one subagent, its directory, and its log. Its mutex protects the
168 // log, because the run of the subagent writes it while the agent reads it with
170 type Subagent struct {
178 // Append logs a message of the subagent.
179 func (a *Subagent) Append(kind, text string) (Message, error) {
181 defer a.mutex.Unlock()
183 message, err := a.log.Append(kind, text)
185 return Message{}, err
193 // Flush returns when every write before the call is durable.
194 func (a *Subagent) Flush(ctx context.Context) error {
195 return a.syncer.Flush(ctx)
198 // First returns the first message of the log.
199 func (a *Subagent) First() (Message, error) {
201 defer a.mutex.Unlock()
203 return a.log.Message(0)
206 // Show returns the whole log, each message as "i|kind: text", with a blank line
208 func (a *Subagent) Show() (string, error) {
210 defer a.mutex.Unlock()
212 lines := make([]string, 0, a.log.Len())
213 for id := range a.log.Len() {
214 message, err := a.log.Message(id)
219 lines = append(lines, strconv.Itoa(id)+"|"+leafSource(message))
222 return strings.Join(lines, "\n\n"), nil
225 // WorkDir returns the directory where the tools of the subagent run: work in
227 func (a *Subagent) WorkDir() string {
228 return filepath.Join(a.dir, "work")
231 // close makes the writes durable and closes the log.
232 func (a *Subagent) close() error {
234 defer a.mutex.Unlock()
236 return errors.Join(a.syncer.Close(), a.log.Close())