From 27146268cf98da23433855087298f03e64695110 Mon Sep 17 00:00:00 2001 From: Ben Firestone Date: Thu, 3 Sep 2026 15:58:47 -0700 Subject: [PATCH] fix(cli): accept agent_id and skip registration on empty id Named agents report their id as agent_id and background launches report none. The register hook only read agentId, so both cases sent an empty id and the server answered 400 "id is required" on every dispatch. Claude-Session: https://claude.ai/code/session_017tBKU2TAsU96uYChRGWhhq --- cmd/arc/ai.go | 21 ++++++++++++++++- cmd/arc/ai_test.go | 31 +++++++++++++++++++++++++ tests/integration/ai_test.go | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/cmd/arc/ai.go b/cmd/arc/ai.go index 7c10c7c9..cbcaaae1 100644 --- a/cmd/arc/ai.go +++ b/cmd/arc/ai.go @@ -30,12 +30,24 @@ type postToolUsePayload struct { ToolResponse struct { Status string `json:"status"` AgentID string `json:"agentId"` //nolint:tagliatelle // matches Claude Code payload + NamedAgentID string `json:"agent_id"` // addressable (named) agents: "name@session-xxx" TotalDurationMs int `json:"totalDurationMs"` //nolint:tagliatelle // matches Claude Code payload TotalTokens int `json:"totalTokens"` //nolint:tagliatelle // matches Claude Code payload TotalToolUseCount int `json:"totalToolUseCount"` //nolint:tagliatelle // matches Claude Code payload } `json:"tool_response"` } +// agentID returns the agent identifier from the tool response, or "" if the +// payload carries none. Classic Agent results report it as agentId. Named +// (addressable) agents report agent_id instead. Background launches report +// neither, since the agent has not finished when the hook fires. +func (p *postToolUsePayload) agentID() string { + if p.ToolResponse.AgentID != "" { + return p.ToolResponse.AgentID + } + return p.ToolResponse.NamedAgentID +} + // parsePostToolUsePayload reads and parses a PostToolUse JSON payload from r. func parsePostToolUsePayload(r io.Reader) (*postToolUsePayload, error) { data, err := io.ReadAll(r) @@ -297,6 +309,13 @@ var aiAgentRegisterCmd = &cobra.Command{ return errors.New("session_id is required in payload") } + agentID := payload.agentID() + if agentID == "" { + // Nothing to register: the server would reject an empty id and + // the hook runs on every Agent call, so skip silently. + return nil + } + // Resolve project from payload CWD; silently skip if unresolvable resolvedProjectID, err := resolveFromServer(payload.CWD) if err != nil { @@ -313,7 +332,7 @@ var aiAgentRegisterCmd = &cobra.Command{ toolUseCount := payload.ToolResponse.TotalToolUseCount agent := &types.AIAgent{ - ID: payload.ToolResponse.AgentID, + ID: agentID, SessionID: payload.SessionID, Description: payload.ToolInput.Description, Prompt: payload.ToolInput.Prompt, diff --git a/cmd/arc/ai_test.go b/cmd/arc/ai_test.go index 99d96a39..03966198 100644 --- a/cmd/arc/ai_test.go +++ b/cmd/arc/ai_test.go @@ -74,6 +74,37 @@ func TestParsePostToolUsePayload(t *testing.T) { require.NoError(t, err) assert.Equal(t, "Bash", p.ToolName) }) + + t.Run("named agent reports agent_id", func(t *testing.T) { + input := `{"session_id": "s3", "tool_name": "Agent", + "tool_response": {"agent_id": "builder-lint-2@session-abc"}}` + p, err := parsePostToolUsePayload(strings.NewReader(input)) + require.NoError(t, err) + assert.Empty(t, p.ToolResponse.AgentID) + assert.Equal(t, "builder-lint-2@session-abc", p.ToolResponse.NamedAgentID) + }) +} + +func TestPostToolUsePayloadAgentID(t *testing.T) { + tests := []struct { + name string + camel string + snake string + want string + }{ + {name: "classic agentId", camel: "abc123", want: "abc123"}, + {name: "named agent_id", snake: "builder-lint-2@session-abc", want: "builder-lint-2@session-abc"}, + {name: "agentId wins when both present", camel: "abc123", snake: "x@session-y", want: "abc123"}, + {name: "background launch has neither", want: ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var p postToolUsePayload + p.ToolResponse.AgentID = tt.camel + p.ToolResponse.NamedAgentID = tt.snake + assert.Equal(t, tt.want, p.agentID()) + }) + } } // hasSubcommand checks whether parent has a subcommand with the given name. diff --git a/tests/integration/ai_test.go b/tests/integration/ai_test.go index 0554952b..353dca7e 100644 --- a/tests/integration/ai_test.go +++ b/tests/integration/ai_test.go @@ -424,6 +424,50 @@ func TestAIAgentRegisterJsonOutput(t *testing.T) { } } +// TestAIAgentRegisterNamedAgentID verifies that a payload carrying the +// addressable-agent shape (agent_id: "name@session-xxx") registers under that id. +func TestAIAgentRegisterNamedAgentID(t *testing.T) { + home, _, workDir := setupAIProject(t) + + arcCmdSuccess(t, home, "ai", "session", "start", + "--id", "test-register-named-sess", + "--transcript-path", "/tmp/register.jsonl", + "--cwd", workDir, + "--server", serverURL) + + payload := strings.Replace( + makePostToolUsePayload("test-register-named-sess", "builder-lint-2@session-abc", "Named agent", workDir), + `"agentId"`, `"agent_id"`, 1) + + output := arcCmdWithStdinSuccess(t, home, payload, + "ai", "agent", "register", "--stdin", "--server", serverURL) + + if !strings.Contains(output, "builder-lint-2@session-abc") { + t.Errorf("expected named agent ID in output, got: %s", output) + } +} + +// TestAIAgentRegisterMissingIDSkips verifies that a payload with no agent id +// (a background launch) exits 0 without registering anything. +func TestAIAgentRegisterMissingIDSkips(t *testing.T) { + home, _, workDir := setupAIProject(t) + + arcCmdSuccess(t, home, "ai", "session", "start", + "--id", "test-register-noid-sess", + "--transcript-path", "/tmp/register.jsonl", + "--cwd", workDir, + "--server", serverURL) + + payload := makePostToolUsePayload("test-register-noid-sess", "", "Background launch", workDir) + + output := arcCmdWithStdinSuccess(t, home, payload, + "ai", "agent", "register", "--stdin", "--server", serverURL) + + if strings.TrimSpace(output) != "" { + t.Errorf("expected no output when agent id is missing, got: %s", output) + } +} + // TestAIAgentRegisterLazySession verifies that registering an agent for a // non-existent session auto-creates the session. func TestAIAgentRegisterLazySession(t *testing.T) {