Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
refactor(runner): extract conversation tool forward and reply delivery from readControls (#545)
fx-kurumi4a1b3a06a4e3
diff --git a/internal/runner/control.go b/internal/runner/control.goindex 3e6e3306..102eb577 100644--- a/internal/runner/control.go+++ b/internal/runner/control.go@@ -98,27 +98,7 @@ func (d *Daemon) readControls(ctx context.Context, conn *websocket.Conn) error {d.conversationReplies = map[string]chan domain.RunnerToolResponse{}}d.conversationForward = func(callCtx context.Context, token string, body json.RawMessage) (json.RawMessage, error) {- id := fmt.Sprintf("tool-%d", time.Now().UnixNano())- reply := make(chan domain.RunnerToolResponse, 1)- d.conversationMu.Lock()- d.conversationReplies[id] = reply- d.conversationMu.Unlock()- defer func() { d.conversationMu.Lock(); delete(d.conversationReplies, id); d.conversationMu.Unlock() }()- err := writeMessage(controlMessage{Type: "agent_tool.request", RequestID: id, ToolRequest: &domain.RunnerToolRequest{Token: token, Body: body}})- if err != nil {- return nil, err- }- select {- case response := <-reply:- if response.Error != "" {- return nil, errors.New(response.Error)- }- return response.Body, nil- case <-callCtx.Done():- return nil, callCtx.Err()- case <-ctx.Done():- return nil, ctx.Err()- }+ return d.forwardConversationTool(ctx, callCtx, writeMessage, token, body)}d.conversationMu.Unlock()slots := make(chan struct{}, 8)@@ -151,15 +131,7 @@ func (d *Daemon) readControls(ctx context.Context, conn *websocket.Conn) error {}_ = conn.SetReadDeadline(time.Now().Add(controlReadTimeout))if message.Type == "agent_tool.response" && message.ToolResponse != nil {- d.conversationMu.Lock()- reply := d.conversationReplies[message.RequestID]- d.conversationMu.Unlock()- if reply != nil {- select {- case reply <- *message.ToolResponse:- default:- }- }+ d.deliverConversationReply(message.RequestID, *message.ToolResponse)continue}if message.Type == "workspace.cancel" {@@ -274,3 +246,46 @@ func (d *Daemon) activeTaskIDs() []string {}return ids}++// forwardConversationTool sends one agent_tool.request over the control+// connection and waits for the matching agent_tool.response. It returns early+// when either the caller (callCtx) or the control connection (connCtx) ends.+// readControls must have initialised d.conversationReplies.+func (d *Daemon) forwardConversationTool(connCtx, callCtx context.Context, write func(controlMessage) error, token string, body json.RawMessage) (json.RawMessage, error) {+ id := fmt.Sprintf("tool-%d", time.Now().UnixNano())+ reply := make(chan domain.RunnerToolResponse, 1)+ d.conversationMu.Lock()+ d.conversationReplies[id] = reply+ d.conversationMu.Unlock()+ defer func() { d.conversationMu.Lock(); delete(d.conversationReplies, id); d.conversationMu.Unlock() }()+ err := write(controlMessage{Type: "agent_tool.request", RequestID: id, ToolRequest: &domain.RunnerToolRequest{Token: token, Body: body}})+ if err != nil {+ return nil, err+ }+ select {+ case response := <-reply:+ if response.Error != "" {+ return nil, errors.New(response.Error)+ }+ return response.Body, nil+ case <-callCtx.Done():+ return nil, callCtx.Err()+ case <-connCtx.Done():+ return nil, connCtx.Err()+ }+}++// deliverConversationReply hands an agent_tool.response to the waiting+// forwardConversationTool call. Replies for unknown or already-answered+// request IDs are dropped without blocking the control read loop.+func (d *Daemon) deliverConversationReply(requestID string, response domain.RunnerToolResponse) {+ d.conversationMu.Lock()+ reply := d.conversationReplies[requestID]+ d.conversationMu.Unlock()+ if reply != nil {+ select {+ case reply <- response:+ default:+ }+ }+}diff --git a/internal/runner/control_conversation_test.go b/internal/runner/control_conversation_test.gonew file mode 100644index 00000000..17fb425d--- /dev/null+++ b/internal/runner/control_conversation_test.go@@ -0,0 +1,111 @@+package runner++import (+ "context"+ "encoding/json"+ "errors"+ "testing"++ "agentflow/internal/domain"+)++func newConversationDaemon() *Daemon {+ return &Daemon{conversationReplies: map[string]chan domain.RunnerToolResponse{}}+}++func pendingReplies(d *Daemon) int {+ d.conversationMu.Lock()+ defer d.conversationMu.Unlock()+ return len(d.conversationReplies)+}++// replyingWriter answers every agent_tool.request it sees through+// deliverConversationReply, standing in for the platform side of the socket.+func replyingWriter(t *testing.T, d *Daemon, response domain.RunnerToolResponse, sent *controlMessage) func(controlMessage) error {+ t.Helper()+ return func(message controlMessage) error {+ *sent = message+ go d.deliverConversationReply(message.RequestID, response)+ return nil+ }+}++func TestForwardConversationToolReturnsReplyBody(t *testing.T) {+ d := newConversationDaemon()+ var sent controlMessage+ write := replyingWriter(t, d, domain.RunnerToolResponse{Body: json.RawMessage(`{"ok":true}`)}, &sent)+ body, err := d.forwardConversationTool(context.Background(), context.Background(), write, "fixture-token", json.RawMessage(`{"method":"ping"}`))+ if err != nil {+ t.Fatal(err)+ }+ if string(body) != `{"ok":true}` {+ t.Fatalf("body = %s", body)+ }+ if sent.Type != "agent_tool.request" || sent.RequestID == "" || sent.ToolRequest == nil || sent.ToolRequest.Token != "fixture-token" || string(sent.ToolRequest.Body) != `{"method":"ping"}` {+ t.Fatalf("unexpected request: %+v", sent)+ }+ if pendingReplies(d) != 0 {+ t.Fatalf("reply channel not cleaned up: %d pending", pendingReplies(d))+ }+}++func TestForwardConversationToolReturnsReplyError(t *testing.T) {+ d := newConversationDaemon()+ var sent controlMessage+ write := replyingWriter(t, d, domain.RunnerToolResponse{Error: "tool denied"}, &sent)+ _, err := d.forwardConversationTool(context.Background(), context.Background(), write, "token", nil)+ if err == nil || err.Error() != "tool denied" {+ t.Fatalf("err = %v, want tool denied", err)+ }+}++func TestForwardConversationToolReturnsWriteError(t *testing.T) {+ d := newConversationDaemon()+ writeErr := errors.New("socket closed")+ _, err := d.forwardConversationTool(context.Background(), context.Background(), func(controlMessage) error { return writeErr }, "token", nil)+ if !errors.Is(err, writeErr) {+ t.Fatalf("err = %v, want %v", err, writeErr)+ }+ if pendingReplies(d) != 0 {+ t.Fatalf("reply channel not cleaned up: %d pending", pendingReplies(d))+ }+}++func TestForwardConversationToolStopsOnCallerOrConnectionCancel(t *testing.T) {+ cancelled, cancel := context.WithCancel(context.Background())+ cancel()+ noReply := func(controlMessage) error { return nil }+ for name, contexts := range map[string][2]context.Context{+ "caller": {context.Background(), cancelled},+ "connection": {cancelled, context.Background()},+ } {+ t.Run(name, func(t *testing.T) {+ d := newConversationDaemon()+ _, err := d.forwardConversationTool(contexts[0], contexts[1], noReply, "token", nil)+ if !errors.Is(err, context.Canceled) {+ t.Fatalf("err = %v, want context.Canceled", err)+ }+ if pendingReplies(d) != 0 {+ t.Fatalf("reply channel not cleaned up: %d pending", pendingReplies(d))+ }+ })+ }+}++func TestDeliverConversationReplyIgnoresUnknownAndDuplicateReplies(t *testing.T) {+ d := newConversationDaemon()+ d.deliverConversationReply("missing", domain.RunnerToolResponse{Body: json.RawMessage(`1`)})++ reply := make(chan domain.RunnerToolResponse, 1)+ d.conversationReplies["tool-1"] = reply+ d.deliverConversationReply("tool-1", domain.RunnerToolResponse{Body: json.RawMessage(`"first"`)})+ d.deliverConversationReply("tool-1", domain.RunnerToolResponse{Body: json.RawMessage(`"second"`)})+ if got := <-reply; string(got.Body) != `"first"` {+ t.Fatalf("delivered %s, want first reply", got.Body)+ }+ select {+ case extra := <-reply:+ t.Fatalf("duplicate reply was queued: %s", extra.Body)+ default:+ }+}