]> git.giacomolli.uk - onechat/blobdiff - subagent_run.go
Check spacing between Go statement blocks
[onechat] / subagent_run.go
index 67cf79d7ac14949738e2e167ee8269d45b9409a2..c5e98df7e0f15316f14be60563f0aee4a35d5b60 100644 (file)
@@ -87,6 +87,7 @@ func (r *SubagentRuns) Spawn(ctx context.Context, tasks []string, effort *string
        }
 
        r.mutex.Lock()
+
        closed := r.closed
        r.mutex.Unlock()
 
@@ -137,6 +138,7 @@ func (r *SubagentRuns) Spawn(ctx context.Context, tasks []string, effort *string
 // its log with a new run, with the view of now and the effort of its note.
 func (r *SubagentRuns) Tell(ctx context.Context, name, message string) (string, error) {
        r.mutex.Lock()
+
        run, found := r.running[name]
        if found {
                defer r.mutex.Unlock()
@@ -257,10 +259,12 @@ func (r *SubagentRuns) Resume(ctx context.Context) error {
 // Close ends every run and waits for them. No subagent starts after Close.
 func (r *SubagentRuns) Close() {
        r.mutex.Lock()
+
        r.closed = true
        for _, run := range r.running {
                run.cancel()
        }
+
        r.mutex.Unlock()
 
        r.workers.Wait()
@@ -324,6 +328,7 @@ func (r *SubagentRuns) start(ctx context.Context, run *SubagentRun) error {
        ctx, run.cancel = context.WithCancel(context.WithoutCancel(ctx))
 
        r.mutex.Lock()
+
        _, found := r.running[run.subagent.name]
        if r.closed || found {
                r.mutex.Unlock()
@@ -373,6 +378,7 @@ func (r *SubagentRuns) load() ([]string, error) {
        }
 
        var file SubagentWorkFile
+
        err = json.Unmarshal(data, &file)
        if err != nil {
                return nil, fmt.Errorf("decode running subagents: %w", err)
@@ -412,14 +418,18 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
        defer r.workers.Done()
        defer run.cancel()
 
-       var replies []string
-       var failure error
+       var (
+               replies []string
+               failure error
+       )
+
        stopped := false
        status := subagentFinished
        for more := true; more; {
                reply, err := r.chain(ctx, run)
 
                r.mutex.Lock()
+
                stopped = run.stopped
                switch {
                case err != nil && stopped:
@@ -432,13 +442,17 @@ func (r *SubagentRuns) run(ctx context.Context, run *SubagentRun) {
 
                more = err == nil && ctx.Err() == nil && len(run.pending) > 0
                run.pending = nil
+
                var saveErr error
+
                if !more {
                        delete(r.running, run.subagent.name)
+
                        if !r.closed {
                                saveErr = r.save()
                        }
                }
+
                r.mutex.Unlock()
 
                if saveErr != nil {