+// Report logs the report text of the subagent name as a work message and tells
+// the clients, with the status of the run. Unless the subagent was stopped, the
+// report joins the waiting messages and wakes the loop, as a user message of
+// Submit does. A failed write stops the loop, because the log refuses all
+// writes after it.
+func (l *TurnLoop) Report(name, text, status string) {
+ message, err := l.memory.Append("work", text)
+ if err == nil {
+ l.notify(Event{
+ Kind: "work",
+ ID: message.ID,
+ Text: text,
+ Source: name,
+ Status: status,
+ })
+ }
+
+ l.mutex.Lock()
+ switch {
+ case err != nil:
+ l.failure = fmt.Errorf("%w: %w", errChainLogWrite, err)
+ case status != subagentStopped:
+ l.pending = append(l.pending, message)
+ }
+ l.mutex.Unlock()
+
+ select {
+ case l.wake <- struct{}{}:
+ default:
+ }
+}
+
+// StopSubagent ends the run of the subagent name and tells if it ran. Its
+// report is logged, but it starts no turn.
+func (l *TurnLoop) StopSubagent(name string) (bool, error) {
+ return l.runs.Stop(name)
+}
+