Skip to content

refactor(coderd/x/chatd): move subagent operations out of tool closures - #28758

Merged
ethanndickson merged 4 commits into
mainfrom
mike/chatd-subagent-manager
Nov 2, 2026
Merged

ethanndickson merged 4 commits into
mainfrom
mike/chatd-subagent-manager

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Aug 29, 2026 •

Copy link
Copy Markdown
Collaborator

Moves the subagent tools' logic (spawn_agent, wait_agent, message_agent, interrupt_agent, list_agents, list_subagent_models) out of large inline closures in subagent.go. Child chat operations become typed *Server functions in subagent_operations.go that other code can call, such as the workflow prototype in #31189. The tools become adapters that parse arguments, apply defaults and build the JSON.

Changes

  • subagent_operations.go: operations take chat IDs, a timeout or a busy behavior and return plain values. awaitSubagent wraps awaitSubagentCompletion with the computer_use desktop recording and a last status check, and returns the chat, report and recording, a timeout, or a *subagentStatusError. sendSubagentMessage and interruptSubagent are main's, unchanged. listSubagents returns unarchived children, most recently updated first. The helpers they call move from main unchanged.
  • subagent.go: keeps main's model override resolution. Each tool parses chat_id, applies the wait timeout or list paging defaults, reads the target chat so errors carry its type, and builds the JSON. spawnAgent validates the spawn_agent arguments.
  • Tool outputs are typed structs whose fields follow sorted key order, so the JSON the model sees is byte-identical to main's map output.

Behavior

No intended behavior change. Error strings, tool names, descriptions and schemas, hook fail-closed handling, the wait loop and recording lifetime match main. wait_agent reads the target chat once more, for the recording decision.

Xum (an AI coding agent) rebuilt this PR on behalf of @ibetitsmike.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T15:54:17.184923Z 2deadd5 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 4d7e41e503

ℹ️ 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 August 29, 2026 13:21
@ibetitsmike
ibetitsmike force-pushed the mike/chatd-subagent-manager branch from 4d7e41e to 5716497 Compare September 16, 2026 22:08
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Requested by Xum 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: 5716497616

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

Copy link
Copy Markdown
Collaborator Author

@codex review

Requested by Xum on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 3edfd1143c

ℹ️ 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 force-pushed the mike/chatd-subagent-manager branch from 3edfd11 to b414901 Compare September 21, 2026 19:12
@github-actions github-actions Bot added the stale This issue is like stale bread. label Sep 29, 2026
@github-actions github-actions Bot closed this Oct 3, 2026
@ibetitsmike ibetitsmike reopened this Oct 13, 2026
@ibetitsmike ibetitsmike removed the stale This issue is like stale bread. label Oct 13, 2026
@ibetitsmike
ibetitsmike force-pushed the mike/chatd-subagent-manager branch from b414901 to 3331bf3 Compare October 13, 2026 14:42
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Requested by Xum on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 3331bf31cf

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

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum, an AI agent, requested this review for @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: b41bbce303

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

@github-actions github-actions Bot removed the stale This issue is like stale bread. label Oct 28, 2026
ethanndickson

This comment was marked as duplicate.

@ethanndickson ethanndickson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the goal. The 470-line subagentTools full of closures is hard to read. I'm less sure about the manager as the new layer, though. As written, it looks like the tool handlers turned into methods, not a layer that other code can use.

  1. It takes the tool's input. The methods take the tool argument structs, so callers pass string chat IDs and the manager parses them:

    func (m *subagentManager) Send(ctx context.Context, args messageAgentArgs) (database.Chat, bool, error)
    func (m *subagentManager) Await(ctx context.Context, args waitAgentArgs) (subagentAwaitResult, error)

    Main's typed helpers sendSubagentMessage(ctx, parentID, targetID, msg, busy) and interruptSubagent(ctx, parentID, targetID) get folded into these. The only non-tool caller I could find is the Starlark workflow prototype (#31189), and it calls the typed *Server helpers directly:

    r.p.awaitSubagentCompletion(ctx, r.parent.ID, step.chatID, defaultSubagentWaitTimeout)

    To use the manager, it would need a currentChat closure and waitAgentArgs{ChatID: id.String()}.

  2. It builds the tool's output. Await and List return structs with JSON tags. subagentManagerError exists only to tell the adapter whether to send a JSON error with type or a plain text error:

    if managerErr, ok := errors.AsType[*subagentManagerError](err); ok {
    	return subagentErrorResponse(managerErr.err, managerErr.chat), nil
    }
    return fantasy.NewTextErrorResponse(err.Error()), nil

    So parsing and output format both stay in the manager, and the adapters mostly call json.Marshal.

Could the manager take typed inputs (parent and target chat IDs, a timeout, queue or interrupt) and return plain values (the chat, the report, recording info, a typed status error)? Then the tool adapters keep the parsing, defaults and JSON output, and sendSubagentMessage and interruptSubagent keep their typed signatures. #31189 looks like a real second caller, so it could help shape the API.

Generated by an agent on behalf of the user

@ibetitsmike

ibetitsmike commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

@ethanndickson Thanks. Reshaped as you suggested in 4292e3236cf and 80bb0ce1f31:

  • The subagentManager struct is gone. The child chat operations in subagent_manager.go are typed *Server methods that return plain values, so await, send, interrupt and list need no currentChat closure or tool argument struct:
    • awaitSubagent(ctx, parent database.Chat, targetChatID uuid.UUID, timeout time.Duration) (subagentAwaitOutcome, error). The outcome holds the chat, the report, the recording file IDs, or timedOut. A child in error status returns *subagentStatusError. It takes the parent chat, not only its ID, because a stored recording needs the parent's owner and workspace.
    • sendSubagentMessage(ctx, parentChatID, targetChatID, message, busyBehavior) and interruptSubagent(ctx, parentChatID, targetChatID) are main's, unchanged.
    • listSubagents(ctx, parentChatID) ([]database.Chat, error) returns the unarchived children, most recently updated first.
    • awaitSubagentCompletion, loadSubagentSpawnParentChat and createChildSubagentChatWithOptions keep main's signatures, so the calls in feat: prototype Starlark workflow runs in chatd (do not merge) #31189's workflow.go resolve unchanged.
  • The tools in subagent.go parse chat_id, apply the wait timeout and list paging defaults, read the target chat so errors carry its type, compute message_agent's interrupted, and build the JSON. subagentManagerError and subagentManagerToolResponse are gone.
  • spawn_agent argument validation stays in the tool layer (spawnAgent). feat: prototype Starlark workflow runs in chatd (do not merge) #31189 composes the spawn steps itself, so I did not add a typed spawn wrapper.
  • 80bb0ce1f31 moves subagentLastError, which formats the last_error text wait_agent returns, back to subagent.go next to its only caller, where main keeps it.

Tool JSON, error text, names and schemas do not change. The PR description lists the mutants the adjusted tests catch.

Xum, an AI agent, posted this reply for @ibetitsmike.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum, an AI agent, requested this review for @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 4292e3236c

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

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum, an AI agent, requested this review for @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 80bb0ce1f3

ℹ️ 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 added this pull request to stack #31573 October 30, 2026 15:37
Move the spawn_agent, list_subagent_models, wait_agent, message_agent,
interrupt_agent, and list_agents logic out of the inline tool closures in
subagent.go into a per-turn subagentManager in subagent_manager.go. The
tools become thin adapters that marshal typed results.

Child chat creation, the wait loop, report extraction, and the ancestor
walk move to subagent_manager.go unchanged, including the archived-root
refusal, database error returns, organization instructions, and title
source handling main added since the original branch. Typed tool outputs
marshal byte-identically to the previous map payloads.
The subagent manager took the tools' argument structs and built their
JSON output. Its operations now take chat IDs, a timeout and a busy
behavior and return plain values, and the tools in subagent.go parse
arguments, apply defaults and build the JSON.

- subagent_manager.go: drop subagentManager, subagentManagerError and
  the JSON result types. Restore main's sendSubagentMessage and
  interruptSubagent unchanged, and add awaitSubagent (desktop
  recording and a last status check before a timeout) and
  listSubagents.
- subagent.go: each tool parses chat_id, applies the wait and list
  defaults, looks up the target chat so errors carry its type, and
  builds the JSON. spawnAgent validates the spawn_agent arguments.
- Tests call the typed functions, or the tool where the behavior now
  lives in the adapter (the message_agent interrupted flag and
  list_agents paging).

Tool names, descriptions, schemas, JSON output and error text do not
change.
…t.go

subagentLastError and subagentGenericErrorMessage build the last_error
text that wait_agent returns to the parent model. Their only caller is
subagentAwaitStatusErrorResult in the tool layer, so they belong in
subagent.go, where main keeps them. The moved block is unchanged.
@ibetitsmike
ibetitsmike force-pushed the mike/chatd-subagent-manager branch from 80bb0ce to 2deadd5 Compare October 30, 2026 15:51
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Rebased onto main 55573833d00 (force push, as requested). The only conflict was main's #29292 paused-status checks in interruptSubagent and checkSubagentCompletion, which this PR moves to subagent_manager.go; the rebase carries those lines there. The old merge commits are gone: the branch is now three commits, and the two review commits are unchanged apart from that port. #30985 was rebased on top with the same own diff.

@codex review

Xum (an AI coding agent) posted this comment on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 2deadd5eb4

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

@ethanndickson ethanndickson changed the title refactor(coderd/x/chatd): extract subagent manager refactor(coderd/x/chatd): move subagent tool logic into typed functions Nov 2, 2026
…ations.go

The file holds typed child chat operations, not a manager type. Rename the tests that used the manager name to match the functions they cover.
@ethanndickson ethanndickson changed the title refactor(coderd/x/chatd): move subagent tool logic into typed functions refactor(coderd/x/chatd): move subagent operations out of tool closures Nov 2, 2026
@ethanndickson
ethanndickson merged commit 97b68e3 into main Nov 2, 2026
33 checks passed
@ethanndickson
ethanndickson deleted the mike/chatd-subagent-manager branch November 2, 2026 05:25
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants