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),
35 reserved: make(map[string]bool),
39 // Subagents holds the subagents of one agent. Each subagent has a directory
40 // named for it, which holds its log. A log takes an exclusive lock, so
41 // Subagents opens the log of a name once and keeps it open until Close. Its
42 // mutex protects the open logs.
43 type Subagents struct {
45 clock func() time.Time
49 open map[string]*Subagent
50 reserved map[string]bool
53 // Create makes the directory and the log of a new subagent. It draws a name
54 // again when the directory of the name exists.
55 func (s *Subagents) Create(ctx context.Context) (*Subagent, error) {
56 err := os.MkdirAll(s.dir, 0o700)
58 return nil, fmt.Errorf("create subagent directory: %w", err)
61 for range subagentNameDraws {
65 reserved := s.reserved[name]
72 err = os.Mkdir(filepath.Join(s.dir, name), 0o700)
73 if errors.Is(err, fs.ErrExist) {
78 return nil, fmt.Errorf("create subagent directory: %w", err)
81 return s.Get(ctx, name)
84 return nil, fmt.Errorf("no free subagent name after %d draws", subagentNameDraws)
87 // Reserve keeps a child name out of future subagent draws. An existing subagent
88 // directory or a reserved name is refused.
89 func (s *Subagents) Reserve(name string) error {
91 defer s.mutex.Unlock()
94 return fmt.Errorf("name %q is already used", name)
97 _, err := os.Lstat(filepath.Join(s.dir, name))
99 return fmt.Errorf("name %q is already used by a subagent", name)
102 if !errors.Is(err, os.ErrNotExist) {
103 return fmt.Errorf("check subagent name: %w", err)
106 s.reserved[name] = true
111 // Release frees a reservation after a child fails to publish. It does not
112 // remove a subagent directory or change an open log.
113 func (s *Subagents) Release(name string) {
115 defer s.mutex.Unlock()
117 delete(s.reserved, name)
120 // Get returns the subagent name with its log open. A name that is not valid or
121 // that has no directory is an unknown agent.
122 func (s *Subagents) Get(ctx context.Context, name string) (*Subagent, error) {
124 defer s.mutex.Unlock()
126 subagent, found := s.open[name]
131 unknown := fmt.Errorf("no agent %s", name)
132 if !subagentNameValid(name) {
136 root := filepath.Join(s.dir, name)
137 info, err := os.Stat(root)
138 if errors.Is(err, fs.ErrNotExist) {
143 return nil, fmt.Errorf("read subagent directory: %w", err)
150 log, err := openLog(ctx, root, s.clock)
152 return nil, fmt.Errorf("open log of %s: %w", name, err)
155 syncer, err := openSyncer(root)
157 return nil, errors.Join(fmt.Errorf("open log of %s: %w", name, err), log.Close())
160 subagent = &Subagent{
166 s.open[name] = subagent
171 // Snapshot flushes and locks the open logs while snapshot reads the subagent
172 // files. snapshot must not call Subagents or an open Subagent.
173 func (s *Subagents) Snapshot(ctx context.Context, snapshot func() error) error {
175 defer s.mutex.Unlock()
177 for _, subagent := range s.open {
178 subagent.mutex.Lock()
179 defer subagent.mutex.Unlock()
181 err := subagent.Flush(ctx)
183 return fmt.Errorf("flush subagent snapshot: %w", err)
189 return fmt.Errorf("copy subagent snapshot: %w", err)
195 // Close makes the writes of each open log durable and closes it.
196 func (s *Subagents) Close() error {
198 defer s.mutex.Unlock()
202 for name, subagent := range s.open {
203 err = errors.Join(err, subagent.close())
211 // Subagent is one subagent, its directory, and its log. Its mutex protects the
212 // log, because the run of the subagent writes it while the agent reads it with
214 type Subagent struct {
222 // Append logs a message of the subagent.
223 func (a *Subagent) Append(kind, text string) (Message, error) {
225 defer a.mutex.Unlock()
227 message, err := a.log.Append(kind, text)
229 return Message{}, err
237 // Flush returns when every write before the call is durable.
238 func (a *Subagent) Flush(ctx context.Context) error {
239 return a.syncer.Flush(ctx)
242 // First returns the first message of the log.
243 func (a *Subagent) First() (Message, error) {
245 defer a.mutex.Unlock()
247 return a.log.Message(0)
250 // Show returns the whole log, each message as "i|kind: text", with a blank line
252 func (a *Subagent) Show() (string, error) {
254 defer a.mutex.Unlock()
256 lines := make([]string, 0, a.log.Len())
257 for id := range a.log.Len() {
258 message, err := a.log.Message(id)
263 lines = append(lines, strconv.Itoa(id)+"|"+leafSource(message))
266 return strings.Join(lines, "\n\n"), nil
269 // WorkDir returns the directory where the tools of the subagent run: work in
271 func (a *Subagent) WorkDir() string {
272 return filepath.Join(a.dir, "work")
275 // close makes the writes durable and closes the log.
276 func (a *Subagent) close() error {
278 defer a.mutex.Unlock()
280 return errors.Join(a.syncer.Close(), a.log.Close())