Skip to content

AI-3464: hosted tool calls in group channels name the writer - #2170

Open
kanat wants to merge 6 commits into
mainfrom
ai-3464-hosted-tool-writer
Open

kanat wants to merge 6 commits into
mainfrom
ai-3464-hosted-tool-writer

Conversation

@kanat

@kanat kanat commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Why

In a channel that an agent config answers through chat_channels (a team's group channel), a call of a tool that the app's backend hosts (a dispatch worker) named no user. A backend that acts for "the user who asked" (tools.ToolCallContextFrom(ctx).User.ID) got an empty id and had to refuse. The same tool in a session opened with the user's token carried the user (AI-3464, live check on staging).

Cause: respondTurn put the writer from agent.WithWriter only into the turn's said participant (internal/agent/agent.go:2463-2466 on b466fb7). lastParticipant stayed the typed caller, which has no user id (agent.go:2479). requesterLocked (internal/agent/requester.go:27-35) then found nobody. The Router opens a chat_channels session with no Caller, so hostedRunner.Run sent no user (internal/session/hosted_tools.go:131-135).

How it works

Anya writes in callouts:team-1
  answerHosted: agent.WithWriter(ctx, anya, "Anya")         (already there, AI-3153)
  Session.FollowUp -> Agent.RespondTo -> respondTurn
    a.lastWriter = listened.writer                           NEW: set again on every turn
  model calls lookup_order -> prepareTool
    requesterLocked:
      heard participant's user   (a call)                    unchanged
      else lastWriter            (a chat_channels writer)    NEW
      else sender                (a typed command's caller)  unchanged
    WithRequester(ctx, {anya, "Anya"})
  hostedRunner.Run -> dispatch tool_call {"user": {"id": anya, "name": "Anya"}}
Ben writes next -> lastWriter = Ben -> {"user": {"id": ben, "name": "Ben"}}

This reuses the writer that agent.WithWriter/WriterOf already own (AI-3154 approvals go to WriterOf). It adds no new path. The same requester also reaches a session's client tools (internal/session/tools.go:147).

Turn Before (b466fb7) After
chat_channels, Anya writes (always or mentioned) no user Anya's id and name
next turn, Ben writes no user Ben's id and name
a turn nobody wrote none none
session opened with a user's token that user that user (control)

Validation

Test Defect it catches On b466fb7
AgentSuite/TestAToolAskedForInAWrittenTurnIsForWhoeverWroteIt the writer is not the requester, or a stale writer stays for the next turn, or the caller goes ahead of the writer fails (Anya and Ben come out empty)
AppHooksSuite/TestAHostedCallInAChannelTypeAnsweringMentionsIsForEachTurnsWriter the worker's tool_call has no user in answer: mentioned fails (user nil)
AppHooksSuite/TestAHostedCallInAChannelTypeAnsweringEveryMessageIsForEachTurnsWriter the same in answer: always fails (user nil)
SessionToolsSuite/TestAHostedCallInASessionOpenedWithAUsersTokenIsForThem (control) the agent-conversation path changes passes, {"id": <user>}

Mutations (one script, every changed line): writer fallback removed, writer never kept, writer kept only when set (stale), writer after sender, writer over a heard participant, writer name dropped. A test kills each one.

AppHooksSuite.SetupSuite now calls manager.HostTools(dispatch), as cmd/router/main.go:1375 does, so its sessions offer what a worker hosts.

🤖 Generated with Claude Code

kanat and others added 3 commits November 21, 2026 00:58
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round-1 review at 810edb5 merged onto b466fb795, the current main. CI: the PR is a draft, so go and integration were skipped. They run at gh pr ready.

Local runs: go test ./internal/agent/ passed. -race -count=3 on the new tests passed. The integration suites TestAppHooksSuite, TestSessionToolsSuite and TestChannelTypeTurnSuite passed. gofmt and vet are clean. The author's 6 mutants are all killed. I added 4 mutants and 1 survived (R1.1).

Checked and sound:

  • lastWriter is written only in respondTurn and read only in requesterLocked, both under a.mu. The lock scope did not change.
  • A user-token session never carries WithWriter, so its requester is the same as on base. The control test passes.
  • WithWriter is set only for a channel type other than agent (threadhooks.go:176), so thread channels and configs without chat_channels are unchanged.
  • The author's two questions (thread channels, per-user connector connections) are about code this PR does not change. Neither one is a hole this PR opens.

Verdict: request changes, for R1.1 (Should fix).

Comment thread acceleration/internal/agent/agent.go Resolved
kanat and others added 3 commits November 21, 2026 01:32
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…participant

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delta review at 4c7e8090e (last reviewed 810edb5c2). The head is current main (175bce1f1) merged in, so the tested tree is the head. Verdict: GO.

Round 1 findings:

  • R1.1 (a second tool round of a written turn): fixed. TestEveryToolRoundOfAWrittenTurnIsForWhoeverWroteIt passes. With a.lastWriter = writer{} added after a.toolRounds++ in respondAfterTool it fails (round 2 expected {anya-1 Anya}, got {}).
  • R1.2 (lastWriter comment untrue for other turn starts): fixed. The comment now says the field is cleared wherever lastParticipant is set for a heard or new turn, and each of those sites clears it. TestAnAdoptedSpeculationDoesNotKeepTheLastWriter fails when the reset at speculate.go:297 is removed (expected Requester{}, got {anya-1 Anya}).
  • The six other resets (agent.go:1878, agent.go:1945, native.go:92, native.go:110, native.go:327, native.go:566) have no test of their own. I removed all six together and the whole ./internal/agent package still passes, as the fixer said. I do not block on that:
    • lastWriter is written in one place, agent.go:2485 in respondTurn, which only the cascade paths reach.
    • RespondTo sends a native agent to respondNative first, and that reads no writer, so on a native agent lastWriter is never non-zero. The four native.go resets can change no state.
    • The two agent.go resets need audio transcripts on an agent that also took a writer turn. A writer comes only from the router-opened text session of a chat_channels config.
    • Each reset zeroes a field on the line group that already refreshes the participant. A missing one cannot give a hosted call a name it does not get today.

Checks on the merged tree, with my own test database:

  • gofmt and go vet (also with -tags integration) are clean.
  • go test ./internal/agent passes.
  • go test -tags integration -p 1 ./internal/api -run 'TestAppHooksSuite|TestSessionToolsSuite|TestChannelTypeTurnSuite' passes.
  • The two new tests pass under -race -count=20 and -cpu 1 -count=30.
  • Every new reset sits inside an existing a.mu section, so no lock scope changed.
  • CI: the PR is a draft, so go and integration skip. CodeQL, the Analyze jobs and GitGuardian pass.
  • No new findings.

Details: reviewer-2.md in the ledger directory.

@kanat
kanat marked this pull request as ready for review November 21, 2026 00:44

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant