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 // Subagents holds the subagents of one agent. Each subagent has a directory
27 // named for it, which holds its log. A log takes an exclusive lock, so
28 // Subagents opens the log of a name once and keeps it open until Close. Its
29 // mutex protects the open logs.
30 type Subagents struct {
32 clock func() time.Time
36 open map[string]*Subagent
37 reserved map[string]bool
40 // Subagent is one subagent, its directory, and its log. Its mutex protects the
41 // log, because the run of the subagent writes it while the agent reads it with
43 type Subagent struct {
51 // newSubagents returns the subagents in dir, one directory for each name. The
52 // clock gives the dates of their messages, and draw gives the names of new
54 func newSubagents(dir string, clock func() time.Time, draw func() string) *Subagents {
59 open: make(map[string]*Subagent),
60 reserved: make(map[string]bool),
64 // Create makes the directory and the log of a new subagent. It draws a name
65 // again when the directory of the name exists.
66 func (s *Subagents) Create(ctx context.Context) (*Subagent, error) {
67 err := os.MkdirAll(s.dir, 0o700)
69 return nil, fmt.Errorf("create subagent directory: %w", err)
72 for range subagentNameDraws {
76 reserved := s.reserved[name]
83 err = os.Mkdir(filepath.Join(s.dir, name), 0o700)
84 if errors.Is(err, fs.ErrExist) {
89 return nil, fmt.Errorf("create subagent directory: %w", err)
92 subagent, err := s.Get(ctx, name)
94 return subagent, fmt.Errorf("open created worker: %w", err)
100 return nil, fmt.Errorf("no free subagent name after %d draws", subagentNameDraws)
103 // Get returns the subagent name with its log open. A name that is not valid or
104 // that has no directory is an unknown agent.
105 func (s *Subagents) Get(ctx context.Context, name string) (*Subagent, error) {
107 defer s.mutex.Unlock()
109 subagent, found := s.open[name]
114 unknown := fmt.Errorf("no agent %s", name)
115 if !subagentNameValid(name) {
119 root := filepath.Join(s.dir, name)
120 info, err := os.Stat(root)
121 if errors.Is(err, fs.ErrNotExist) {
126 return nil, fmt.Errorf("read subagent directory: %w", err)
133 log, err := openLog(ctx, root, s.clock)
135 return nil, fmt.Errorf("open log of %s: %w", name, err)
138 syncer, err := openSyncer(root)
140 return nil, errors.Join(fmt.Errorf("open log of %s: %w", name, err), log.Close())
143 subagent = &Subagent{
150 s.open[name] = subagent
155 // Reserve keeps a child name out of future subagent draws. An existing subagent
156 // directory or a reserved name is refused.
157 func (s *Subagents) Reserve(name string) error {
159 defer s.mutex.Unlock()
161 if s.reserved[name] {
162 return fmt.Errorf("name %q is already used", name)
165 _, err := os.Lstat(filepath.Join(s.dir, name))
167 return fmt.Errorf("name %q is already used by a subagent", name)
170 if !errors.Is(err, os.ErrNotExist) {
171 return fmt.Errorf("check subagent name: %w", err)
174 s.reserved[name] = true
179 // Release frees a reservation after a child fails to publish. It does not
180 // remove a subagent directory or change an open log.
181 func (s *Subagents) Release(name string) {
183 defer s.mutex.Unlock()
185 delete(s.reserved, name)
188 // Snapshot flushes and locks the open logs while snapshot reads the subagent
189 // files. snapshot must not call Subagents or an open Subagent.
190 func (s *Subagents) Snapshot(ctx context.Context, snapshot func() error) error {
192 defer s.mutex.Unlock()
194 for _, subagent := range s.open {
195 subagent.mutex.Lock()
196 defer subagent.mutex.Unlock()
198 err := subagent.Flush(ctx)
200 return fmt.Errorf("flush subagent snapshot: %w", err)
206 return fmt.Errorf("copy subagent snapshot: %w", err)
212 // Close makes the writes of each open log durable and closes it.
213 func (s *Subagents) Close() error {
215 defer s.mutex.Unlock()
219 for name, subagent := range s.open {
220 err = errors.Join(err, subagent.close())
228 // Flush returns when every write before the call is durable.
229 func (a *Subagent) Flush(ctx context.Context) error {
230 return a.syncer.Flush(ctx)
233 // close makes the writes durable and closes the log.
234 func (a *Subagent) close() error {
236 defer a.mutex.Unlock()
238 return errors.Join(a.syncer.Close(), a.log.Close())
241 // Append logs a message of the subagent.
242 func (a *Subagent) Append(kind, text string) (Message, error) {
244 defer a.mutex.Unlock()
246 message, err := a.log.Append(kind, text)
248 return Message{}, err
256 // First returns the first message of the log.
257 func (a *Subagent) First() (Message, error) {
259 defer a.mutex.Unlock()
261 return a.log.Message(0)
264 // Show returns the whole log, each message as "i|kind: text", with a blank line
266 func (a *Subagent) Show() (string, error) {
268 defer a.mutex.Unlock()
270 lines := make([]string, 0, a.log.Len())
271 for id := range a.log.Len() {
272 message, err := a.log.Message(id)
277 lines = append(lines, strconv.Itoa(id)+"|"+leafSource(message))
280 return strings.Join(lines, "\n\n"), nil
283 // WorkDir returns the directory where the tools of the subagent run: work in
285 func (a *Subagent) WorkDir() string {
286 return filepath.Join(a.dir, "work")