Skip to content

feat(coderd/x/chatd): start a root chat as an agent type - #184

Merged
ibetitsmike merged 10 commits into
mainfrom
mike/agent-usage-1-root-agent-type
Nov 11, 2026
Merged

ibetitsmike merged 10 commits into
mainfrom
mike/agent-usage-1-root-agent-type

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #187 (U1, start a chat as an agent). First of two stacked PRs; the API comes in the next one.

spawn_agent can start a child as a built-in type or a custom agent definition, but a root chat always ran as the default agent. Behind the fork-agent-definitions experiment, chatd can now create a root chat as an agent type:

  • Resolution: ResolveRootAgent resolves the type the way spawn_agent does, from general, explore and the owner's personal, organization and project definitions, with the same bare and qualified names. The type match, the tier merge and the reasoning-effort check are factored out of fork_agent_definitions.go, so spawn and the root share them. Workspace definitions are refused, because a chat reads them only after its workspace agent connects, which is after the chat is created. Other built-in types (computer_use) are refused too.
  • Create: CreateOptions.RootAgent appends the definition's body after the chat's system prompt, sets the explore mode for an explore base type, and stores a depth 0 chat_subagents row with the definition's tool policy in the insert transaction.
  • Tool policy: customAgentToolPolicyOfChat loads the policy for any chat with a row, so it holds for such a root from its first turn. With the experiment on, each root turn makes one indexed lookup.
  • Orchestration guidance: a root whose type can't spawn (explore, readonly: true, or a tools/disallowed_tools list without the spawn tools) drops the subagent guidance block. It uses the same rule as spawn: keepsOrchestrationTools is factored out of childGetsOrchestrationPrompt.
  • Workspace guidance: a root that can't create a workspace (explore, read-only, or a policy without list_templates/create_workspace) gets only the base detached-workspace text, and an attached root whose policy removes execute, read_file or write_file gets a limited attached text that points at the tools it has.
  • Dynamic tools: dynamic tool names whose tools the policy removed are dropped after the policy filter, so calling one fails as an unknown tool instead of waiting for the client.
  • Explore roots drop blocked tools: chatd limits an Explore chat through the turn's active tool list, and chatloop reads an empty active list as every registered tool. Explore children also remove every other tool from the turn's tool set, but roots did not, so an explore-base definition whose disallowed_tools removed all five Explore tools (and had no MCP tools) could call write_file and edit_files. Explore roots now drop blocked tools the same way (the Explore filter after filterToolsForTurn in turn_environment.go) and are left with no tools. Upstream has no API that creates Explore roots, so only fork root agents are affected.
  • Children: CreateChat refuses RootAgent together with ParentChatID, so a child never gets a depth 0 row.
  • ACP guidance: the <acp-subagents> prompt now follows the tools that the custom agent policy keeps, for roots and children alike.
  • No effect on subagent limits, lists or cascades: every count selects children by parent_chat_id or root_chat_id (CountActiveSubagentsForCapacity requires parent_chat_id IS NOT NULL), the chat list returns roots by parent_chat_id IS NULL and children by parent, and the row's only cascade is its own ON DELETE CASCADE on chat_id.

Tests: TestForkRootAgent creates a root as a personal readonly: true definition and checks the prompt (definition body appended, no orchestration block, the rest of the default prompt kept) and the turn's tools (execute kept, write_file and spawn_agent removed). With a depth limit of 1 and one live subagent per family, a spawn from that root still gets depth 1, the root isn't counted as live, and the second spawn hits the limit. It also checks that a readonly root gets neither acp_spawn_agent nor the ACP guidance, that a policy keeping acp_spawn_agent without acp_wait_agent gets no ACP guidance, the tailored workspace texts, that a removed dynamic tool is in neither env.Tools nor env.DynamicToolNames, that an Explore-base root disallowing all five Explore tools has no write_file, and that CreateChat rejects a root agent type for a child. Deliberate breaks (roots skip the policy, no orchestration strip) fail it. Two upstream turn test store wrappers in chatd_test.go (panicOnInTxDB, overrideReadFailStore) get ForkUnwrap/ForkWrap, the dbmetrics pattern, because root turns now read fork queries and forkdb.From refuses unknown wrappers. The API PR covers explore and the error cases.

Checks: targeted ./coderd/x/chatd/ tests (TestForkRootAgent|TestCustomAgentToolPolicy|TestForkAgentDefinition|TestNestedSubagent|TestSubagent|TestSpawn) pass, plus go vet, go build and gofmt.

coderd/x/chatd/ARCHITECTURE.md gets three TODO(ibetitsmike) notes for the root agent behavior, per the chatd docs rule. The pstack adapter doc and its source line references are updated in the next PR, which adds the API.

ResolveRootAgent resolves an agent type for a new root chat the way
spawn_agent does, from the built-in general and explore types and the
owner's personal, organization and project definitions. Workspace
definitions are not pinned when a chat is created, so they are refused.

CreateChat takes the result in CreateOptions.RootAgent. It appends the
definition's prompt, sets the base type's mode, drops the orchestration
guidance when the type cannot spawn, and stores a depth 0 chat_subagents
row with the definition's tool policy in the insert transaction.
customAgentToolPolicyOfChat now loads the policy for any chat with a row,
so the policy holds for such a root from its first turn. The depth 0 row
counts toward no subagent limit: every count selects children by
parent_chat_id or root_chat_id.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented on behalf of @ibetitsmike.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T10:20:27.254853Z 4a4c0a7 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de975cba0d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_root_agent.go Resolved
Comment thread coderd/x/chatd/fork_root_agent.go Resolved
Root turns now read their tool policy with forkdb.From when
fork-agent-definitions is on, so a database.Store wrapper that a turn runs
through must forward ForkUnwrap and ForkWrap like dbmetrics does. Without
them TestProcessChatPanicRecovery failed its turn before the first model
call.
buildTurnEnvironment decided whether to add the ACP subagent guidance
from the tools before the custom agent policy filtered them, so a root
or child whose policy drops acp_spawn_agent still got the guidance. It
now checks the tools that the policy keeps.

CreateChat also refuses a RootAgent for a chat with a parent, so a child
cannot get a depth 0 chat_subagents row.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 011af9df4d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 011af9df4d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/turn_environment.go Resolved
Comment thread coderd/x/chatd/fork_root_agent.go Resolved
…n its policy

Show the ACP guidance only when the policy keeps every ACP tool, drop
dynamic tool names whose tools the policy removed, and leave out the
create_workspace advice for a detached root that cannot create a
workspace.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8e10deccd1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/turn_environment.go Resolved
Comment thread coderd/x/chatd/fork_root_agent.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: cf1a68b75f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike marked this pull request as ready for review November 11, 2026 10:06
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 4a4c0a7aee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike merged commit 94cd032 into main Nov 11, 2026
16 of 23 checks passed
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum merged this PR on behalf of @ibetitsmike.

Merged as a squash of head 4a4c0a7aee on main d523eb1a30.

  • CI: fork-ci run 43002199788 (attempt 2) green on the exact head, which contains main d523eb1a30.
  • Codex: receipt 6793240139 on 4a4c0a7aee. 0 unresolved threads.
  • UAT: covered by the w10-agents stack run on a superset head (f080e0a769).
  • Owner follow-up: this PR adds 3 TODO(ibetitsmike) items to coderd/x/chatd/ARCHITECTURE.md (root agent resolution, the depth-0 row, root tool policy) for @ibetitsmike to write.

@ibetitsmike
ibetitsmike deleted the mike/agent-usage-1-root-agent-type branch November 11, 2026 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant