Skip to content

feat(coderd): enforce custom agent tool policy on child chats - #58

Merged
ibetitsmike merged 25 commits into
mainfrom
mike/custom-agent-types-4-tool-policy
Nov 9, 2026
Merged

ibetitsmike merged 25 commits into
mainfrom
mike/custom-agent-types-4-tool-policy

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

With the fork-agent-definitions experiment on, the tool fields of a custom agent definition now restrict the child chat it spawns. #51 resolved these types but gave every one of them its base type's full tool set.

  • readonly: true keeps only the explore allowlist (exploreToolAllowed): read_file, execute, process_output, read_skill, read_skill_file, and admin, internal and inline MCP tools; among provider tools it keeps only web_search, as Explore children do. execute stays available. This relaxes PRD R1.5 on purpose, because a read-only agent still has to run read-only commands, so readonly is not a sandbox. Workspace MCP tools are dropped.
  • tools is an allowlist of non-MCP tools, provider tools such as web_search included. Claude Code names work as aliases: Read, Write and Edit/MultiEdit map to read_file, write_file and edit_files; Bash maps to execute plus process_output; Grep and Glob map to read_file, execute and process_output, following docs/fork/pstack-adapter.md; WebSearch maps to web_search; Task maps to the subagent tools (spawn_agent, wait_agent, message_agent, interrupt_agent, list_agents, list_subagent_models). An empty list allows no non-MCP tools. MCP tools are governed by mcp, and so is find_tools, the search tool that delivers deferred MCP tools; only an explicit disallowed_tools entry removes it.
  • disallowed_tools removes any named tool, MCP tools included (by full tool name), with the same aliases. A Claude Code MCP tool ID, mcp__<server>__<tool>, also matches the chatd name <server>__<tool>, sanitized and capped the way chatd exposes MCP tools. Denying find_tools drops the deferred MCP candidates, so no find_tools is built and the allowed MCP tools stay directly callable.
  • mcp: none drops every MCP tool. mcp: [names] keeps only MCP tools whose server matches one of the names exactly, case included, because server slugs and workspace server names are unique case-sensitively. For org, inline and internal servers the name is matched against the server's exact slug, looked up by server ID from the org's MCP server configs, the root chat's inline servers and the internal servers, and only when mcp names servers. The tool-name prefix is not used because mcpclient sanitizes and truncates tool names, so two servers can share a prefix. For workspace MCP servers the name is the untrimmed server name, because workspace config can hold both x and x as distinct servers.
  • When the policy removes spawn_agent, wait_agent or list_agents, the tools the <subagent-orchestration> block tells the child to call (through readonly, a tools allowlist or disallowed_tools), the child's system prompt drops that block, as children that cannot spawn already do (childGetsOrchestrationPrompt, one call site in subagent_operations.go).
  • When the policy removes read_skill (a tools allowlist without it, or disallowed_tools), the child's system prompt drops the <available-skills> index, which tells it to call read_skill. readonly keeps read_skill, so a readonly child keeps the index. buildTurnEnvironment loads the stored policy once, before the system prompt (customAgentToolPolicyOfChat), and the same policy then filters the tools.

The policy is stored at spawn, in the same transaction as the child, in a new nullable chatd.chat_subagents.tool_policy jsonb column (chatdmig node 01a1c08d…_chat_subagent_tool_policy; additive, no backfill). Snapshotting matters because a child that has execute can refresh its pinned context resources and would otherwise be able to rewrite its own definition. Built-in types and older rows store NULL and are unaffected. If a stored policy cannot be decoded, the child's turn fails instead of running unrestricted.

Enforcement is a second fork hook in buildTurnEnvironment (turn_environment.go, 3 lines). It runs once every tool source is assembled, just before the active tool names are computed, and filters the turn's tools, the deferred find_tools candidates and the provider tools. The first hook (appendForkTools) cannot do this job because MCP tools, workspace MCP tools, the deferred catalog and provider tools are all added after it. find_tools itself is built after the hook, but only from the candidates the policy already filtered.

With the experiment off, or for a root chat, the hook returns before it reads anything.

Part of #7.

Xum opened this PR on behalf of @ibetitsmike.

Behind the fork-agent-definitions experiment, spawn_agent resolves custom
agent types from the chat's pinned subagent context resources after the
built-in types. A custom child runs with the definition body as a system
message and the definition's model and reasoning effort unless the call
passes explicit ones, and its chat_subagents row records the custom type.
The spawn_agent description lists the custom types within a byte budget.
…es-4-tool-policy

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_agent_definitions.go
#	coderd/x/chatd/fork_agent_definitions_internal_test.go
#	coderd/x/chatd/fork_subagents.go
#	coderd/x/chatd/subagent_operations.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 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-09T17:54:00.875359Z 5c4912c 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: 38636cb16f

ℹ️ 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
Comment thread coderd/x/chatd/turn_environment.go Resolved
Comment thread coderd/x/chatd/turn_environment.go Resolved
Comment thread coderd/database/forkdb/models.go Resolved
…agent tool policy

Move the policy hook after provider tools are built so tools allowlists and disallowed_tools also govern web_search and other provider tools, take the MCP server name from the tool name prefix so named mode works for inline and internal servers, add Grep, Glob and WebSearch aliases, and forward ToolPolicy in dbgen.ChatSubagent.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: cda71ab79e

ℹ️ 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".

…l name

mcpclient sanitizes and truncates tool names, so two servers can share a name prefix. Map each MCP tool's server ID to its slug from org configs, the root chat's inline servers and internal servers.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: 355c81420e

ℹ️ 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".

…e servers in tool policy

disallowed_tools now also matches mcp__<server>__<tool> against the chatd name <server>__<tool>. Named mcp no longer trims workspace server names, so a padded server cannot pass as its plain sibling.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: 13f45b9ecb

ℹ️ 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/subagent_operations.go Resolved
…policy removes spawn_agent

A readonly custom child, or one whose tools or disallowed_tools exclude spawn_agent, no longer gets the <subagent-orchestration> block telling it to use tools it lacks.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: aea9107b4d

ℹ️ 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".

…tool policy

Server slugs and workspace server names are unique case-sensitively, so case folding let mcp: [docs] reach a distinct Docs server.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: 2c6f1252a6

ℹ️ 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
… policy

find_tools is built after the policy hook from the remaining deferred candidates, so denying it now drops the candidates and leaves the MCP tools undeferred.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: 6982f7cfb6

ℹ️ 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_subagents.go Resolved
… names

mcpclient sanitizes MCP tool names and caps them at the provider limit, so mcp__awslabs.foo__lookup has to become awslabs_foo__lookup before it can match.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: 5d71442534

ℹ️ 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".

docs/fork/pstack-adapter.md maps Task to spawn_agent then wait_agent; the alias covers the whole subagent tool family the orchestration guidance names.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: bc85cf8d7f

ℹ️ 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_subagents.go Resolved
The <subagent-orchestration> block tells the child to call spawn_agent, wait_agent and list_agents, so a policy that removes any of them now drops the block.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 0f1112b7d7

ℹ️ 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".

…es-4-tool-policy

# Conflicts:
#	coderd/database/forkdb/sqlc.yaml
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this 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: aa178d2f8d

ℹ️ 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
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 5c4912c13d

ℹ️ 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 b457b3f into main Nov 9, 2026
10 checks passed
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) merged this on behalf of @ibetitsmike.

Merge evidence for head 5c4912c13d7c5108a47c1022311e030ab876f34e:

  • fork-ci pull_request run on this exact head succeeded: https://github.com/ibetitsmike/coder/actions/runs/42668479039. All PR checks pass, and fork-quiet succeeded.
  • Codex review at 17:53:59Z on 5c4912c13d: no major issues. 0 unresolved review threads.
  • GitHub reported MERGEABLE/CLEAN. The branch contains main 106a9355f6, and git merge-tree was clean.
  • UAT round 7 passed on aa178d2f8d. The only change since then is the skill-index gate in 5c4912c13d, which has unit test coverage.
  • Lead review confirmed the policy is gated on fork-agent-definitions and applies only to child chats. The declined Codex findings were confirmed, including cross-level policy intersection for nested children: the policy is not a security boundary, and readonly keeps execute. That item is recorded as a product question for the maintainer.

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