+ if a.config.Parent != "" && len(entries) != 0 {
+ return nil, errors.New("children cannot have children")
+ }
+
+ var children []*Agent
+
+ keys := map[string]bool{a.CacheKey(): true}
+
+ for _, entry := range entries {
+ name := entry.Name()
+ if !entry.IsDir() {
+ return nil, fmt.Errorf("child %q must be a directory", name)
+ }
+
+ path := filepath.Join(dir, name)
+ _, err = os.Stat(filepath.Join(path, "config.json"))
+ if err != nil {
+ return nil, fmt.Errorf("child %q config: %w", name, err)
+ }
+
+ child, err := openAgent(path)
+ if err != nil {
+ return nil, fmt.Errorf("open child %q: %w", name, err)
+ }
+
+ if child.config.Name != name || child.config.Parent != a.config.Name {
+ return nil, fmt.Errorf("child %q needs name %q and parent %q", name, name, a.config.Name)
+ }
+
+ if child.config.Name == a.config.Name || keys[child.CacheKey()] {
+ return nil, fmt.Errorf("child %q must have its own name and ID", name)
+ }
+
+ keys[child.CacheKey()] = true
+ _, err = child.Children()
+ if err != nil {
+ return nil, fmt.Errorf("read child %q: %w", name, err)
+ }
+
+ children = append(children, child)
+ }
+
+ return children, nil
+}
+
+// CreateChild writes a new direct child with its own ID and the model, effort,
+// and working directory of the root. Child and subagent names share a
+// namespace. The config is complete before an atomic rename publishes the
+// child. Preparation stays outside children, so an interrupted write cannot
+// stop startup.
+func (a *Agent) CreateChild(name, responsibility string, subagents *Subagents) (_ *Agent, err error) {
+ if a.config.Parent != "" {
+ return nil, errors.New("only the root can create children")
+ }
+
+ if !agentChildNameValid(name) {
+ return nil, errors.New("name must have 2 to 32 ASCII letters and start with a capital letter")
+ }
+
+ if name == a.config.Name {
+ return nil, errors.New("the name is already used by the root")
+ }
+
+ responsibility = strings.TrimSpace(responsibility)
+ if responsibility == "" {
+ return nil, errors.New("responsibility must not be empty")
+ }
+
+ err = subagents.Reserve(name)
+ if err != nil {
+ return nil, fmt.Errorf("reserve child name: %w", err)
+ }
+
+ published := false
+ defer func() {
+ if !published {
+ subagents.Release(name)
+ }
+ }()
+
+ staged, err := os.MkdirTemp(a.dir, ".child-*")
+ if err != nil {
+ return nil, fmt.Errorf("prepare child directory: %w", err)
+ }
+
+ defer func() {
+ cleanupErr := os.RemoveAll(staged)
+ if cleanupErr != nil {
+ err = errors.Join(err, fmt.Errorf("remove prepared child: %w", cleanupErr))
+ }
+ }()
+
+ config := a.config
+ config.Name = name
+ config.ID = ""
+ config.Parent = a.config.Name
+ config.Responsibility = responsibility
+ child, err := writeAgent(staged, config)
+ if err != nil {
+ return nil, fmt.Errorf("write child: %w", err)
+ }
+
+ dir := filepath.Join(a.dir, "children")
+ err = os.MkdirAll(dir, 0o700)
+ if err != nil {
+ return nil, fmt.Errorf("create children directory: %w", err)
+ }
+
+ dir = filepath.Join(dir, name)
+ err = unix.Renameat2(unix.AT_FDCWD, staged, unix.AT_FDCWD, dir, unix.RENAME_NOREPLACE)
+ if err != nil {
+ return nil, fmt.Errorf("publish child directory: %w", err)
+ }
+
+ published = true
+ child.dir = dir
+
+ return child, nil