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 return s.Get(ctx, name)
95 return nil, fmt.Errorf("no free subagent name after %d draws", subagentNameDraws)
98 // Get returns the subagent name with its log open. A name that is not valid or
99 // that has no directory is an unknown agent.
100 func (s *Subagents) Get(ctx context.Context, name string) (*Subagent, error) {
102 defer s.mutex.Unlock()
104 subagent, found := s.open[name]
109 unknown := fmt.Errorf("no agent %s", name)
110 if !subagentNameValid(name) {
114 root := filepath.Join(s.dir, name)
115 info, err := os.Stat(root)
116 if errors.Is(err, fs.ErrNotExist) {
121 return nil, fmt.Errorf("read subagent directory: %w", err)
128 log, err := openLog(ctx, root, s.clock)
130 return nil, fmt.Errorf("open log of %s: %w", name, err)
133 syncer, err := openSyncer(root)
135 return nil, errors.Join(fmt.Errorf("open log of %s: %w", name, err), log.Close())
138 subagent = &Subagent{
145 s.open[name] = subagent
150 // Reserve keeps a child name out of future subagent draws. An existing subagent
151 // directory or a reserved name is refused.
152 func (s *Subagents) Reserve(name string) error {
154 defer s.mutex.Unlock()
156 if s.reserved[name] {
157 return fmt.Errorf("name %q is already used", name)
160 _, err := os.Lstat(filepath.Join(s.dir, name))
162 return fmt.Errorf("name %q is already used by a subagent", name)
165 if !errors.Is(err, os.ErrNotExist) {
166 return fmt.Errorf("check subagent name: %w", err)
169 s.reserved[name] = true
174 // Release frees a reservation after a child fails to publish. It does not
175 // remove a subagent directory or change an open log.
176 func (s *Subagents) Release(name string) {
178 defer s.mutex.Unlock()
180 delete(s.reserved, name)
183 // Snapshot flushes and locks the open logs while snapshot reads the subagent
184 // files. snapshot must not call Subagents or an open Subagent.
185 func (s *Subagents) Snapshot(ctx context.Context, snapshot func() error) error {
187 defer s.mutex.Unlock()
189 for _, subagent := range s.open {
190 subagent.mutex.Lock()
191 defer subagent.mutex.Unlock()
193 err := subagent.Flush(ctx)
195 return fmt.Errorf("flush subagent snapshot: %w", err)
201 return fmt.Errorf("copy subagent snapshot: %w", err)
207 // Close makes the writes of each open log durable and closes it.
208 func (s *Subagents) Close() error {
210 defer s.mutex.Unlock()
214 for name, subagent := range s.open {
215 err = errors.Join(err, subagent.close())
223 // Flush returns when every write before the call is durable.
224 func (a *Subagent) Flush(ctx context.Context) error {
225 return a.syncer.Flush(ctx)
228 // close makes the writes durable and closes the log.
229 func (a *Subagent) close() error {
231 defer a.mutex.Unlock()
233 return errors.Join(a.syncer.Close(), a.log.Close())
236 // Append logs a message of the subagent.
237 func (a *Subagent) Append(kind, text string) (Message, error) {
239 defer a.mutex.Unlock()
241 message, err := a.log.Append(kind, text)
243 return Message{}, err
251 // First returns the first message of the log.
252 func (a *Subagent) First() (Message, error) {
254 defer a.mutex.Unlock()
256 return a.log.Message(0)
259 // Show returns the whole log, each message as "i|kind: text", with a blank line
261 func (a *Subagent) Show() (string, error) {
263 defer a.mutex.Unlock()
265 lines := make([]string, 0, a.log.Len())
266 for id := range a.log.Len() {
267 message, err := a.log.Message(id)
272 lines = append(lines, strconv.Itoa(id)+"|"+leafSource(message))
275 return strings.Join(lines, "\n\n"), nil
278 // WorkDir returns the directory where the tools of the subagent run: work in
280 func (a *Subagent) WorkDir() string {
281 return filepath.Join(a.dir, "work")