+// Snapshot flushes and locks the open logs while snapshot reads the subagent
+// files. snapshot must not call Subagents or an open Subagent.
+func (s *Subagents) Snapshot(ctx context.Context, snapshot func() error) error {
+ s.mutex.Lock()
+ defer s.mutex.Unlock()
+
+ for _, subagent := range s.open {
+ subagent.mutex.Lock()
+ defer subagent.mutex.Unlock()
+
+ err := subagent.Flush(ctx)
+ if err != nil {
+ return fmt.Errorf("flush subagent snapshot: %w", err)
+ }
+ }
+
+ err := snapshot()
+ if err != nil {
+ return fmt.Errorf("copy subagent snapshot: %w", err)
+ }
+
+ return nil
+}
+