Skip to content

refactor(coderd/x/chatd): clean generation preparer setup - #26511

Closed
hugodutka wants to merge 1 commit into
mainfrom
fix-chatd-generation-preparer-review
Closed

hugodutka wants to merge 1 commit into
mainfrom
fix-chatd-generation-preparer-review

Conversation

@hugodutka

Copy link
Copy Markdown
Collaborator

Addresses deferred review feedback on generation_preparer.go from PR #26109:

This moves the default chat max output tokens to a documented constant, reuses the existing err in advisor setup, and centralizes generation preparation cleanup behind the named return error path.

Generated by Coder Agents.

@hugodutka

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review

coder-agents-review Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-06-18 14:06 UTC by @hugodutka
Spend: $18.35 / $100.00

Review history
  • R1 (2026-06-18): 12 reviewers, 4 Nit, 1 Note, COMMENT. Review

deep-review v0.8.0 | Round 1 | a5a4c49..7188a95

Last posted: Round 1, 5 findings (4 Nit, 1 Note), COMMENT. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 Nit Open chatd.go:129 Comment first sentence restates name and usage site R1 Gon P2 (downgraded: follows sibling pattern) Yes
CRF-2 Nit Open generation_preparer.go:99 Two-line address-of-local can use new(expr) in Go 1.26 R1 Ging-Go Yes
CRF-3 Nit Open chatd.go:129 Comment missing rationale for 32k value R1 Leorio Yes
CRF-4 Nit Open chatd.go:132 Commit subject "clean" is vague R1 Leorio Yes
CRF-5 Note Open generation_preparer.go:158 Cleanup restructuring incidentally fixes latent MCP connection leak R1 Hisoka, Pariston, Meruem, Mafuuu, Kite Yes

Round log

Round 1

Panel. 0 P0-P4, 4 Nit, 1 Note. Netero clean. Panel: Bisky, Hisoka, Pariston, Mafuuu, Mafu-san, Ging-Go, Meruem, Takumi, Komugi, Gon, Leorio, Kite. Reviewed against a5a4c49..7188a95.

About deep-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Komugi flake/determinism
Kurapika security
Law decomposition
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clean, well-scoped refactoring. The named-return-plus-defer pattern eliminates ten manual cleanup() calls and makes future error paths safe by default. The mcpCleanup hoisting and the constant extraction are both straightforward improvements.

Five reviewers independently noticed that the cleanup restructuring incidentally fixes a latent resource leak: in the old code, if g2.Wait() returned an error (e.g. from ConvertMessagesWithFiles), cleanup() only called workspaceCtx.close() because the mcpCleanup chaining happened after the error check. Since errgroup.Wait() guarantees all goroutines complete before returning, mcpCleanup was set but never called. MCP connections leaked until context cancellation. The new code captures mcpCleanup by reference in the initial cleanup closure, fixing this for all error paths.

3 nits, 1 note. No blocking issues.

"'clean' is a chart note that says 'treated patient.' Treated how?" Leorio

This review was generated by Coder Agents.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/chatd.go
Comment on lines +129 to +132
// defaultChatMaxOutputTokens caps a model response when the model config
// does not set a provider-specific output limit. This keeps unbounded
// requests from accidentally exhausting large-context models.
defaultChatMaxOutputTokens int64 = 32_000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit [CRF-1] The first sentence ("caps a model response when the model config does not set a provider-specific output limit") restates what the name + usage site already show: default = fallback, ChatMaxOutputTokens = caps tokens, and the callConfig.MaxOutputTokens == nil guard shows when it fires.

Downgraded from P2 because the sibling defaultAdvisorMaxOutputTokens follows the identical what-then-why pattern, so this matches local convention.

(Gon P2, downgraded to Nit by orchestrator)

🤖

Comment thread coderd/x/chatd/chatd.go
Comment on lines +129 to +132
// defaultChatMaxOutputTokens caps a model response when the model config
// does not set a provider-specific output limit. This keeps unbounded
// requests from accidentally exhausting large-context models.
defaultChatMaxOutputTokens int64 = 32_000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit [CRF-3] The adjacent defaultAdvisorMaxOutputTokens comment explains its value: "intentionally generous relative to the advisor's concise guidance remit so short plans are not truncated mid-reasoning." That tells you why 16,384 and not 8,192.

This constant says what it prevents but not why 32,000. Is it half a 64k context window? A provider default? Empirically determined? The next person tuning this value has no anchor.

(Leorio)

🤖

Comment thread coderd/x/chatd/chatd.go
// defaultChatMaxOutputTokens caps a model response when the model config
// does not set a provider-specific output limit. This keeps unbounded
// requests from accidentally exhausting large-context models.
defaultChatMaxOutputTokens int64 = 32_000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit [CRF-4] Commit subject "clean generation preparer setup" is vague. "clean" doesn't convey what changed. Something like "centralize preparer error cleanup via named return" or "simplify preparer setup: extract token constant, defer cleanup" tells git log --oneline readers what happened.

(Leorio)

🤖


if callConfig.MaxOutputTokens == nil {
maxOutputTokens := int64(32_000)
maxOutputTokens := defaultChatMaxOutputTokens

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit [CRF-2] Go 1.26 supports new(expr) for constant expressions. This two-line address-of-local pattern:

maxOutputTokens := defaultChatMaxOutputTokens
callConfig.MaxOutputTokens = &maxOutputTokens

can be:

callConfig.MaxOutputTokens = new(defaultChatMaxOutputTokens)

Verified: new(defaultChatMaxOutputTokens) produces *int64 with value 32000 under go 1.26.4. Minor, but removes a variable that exists solely to be addressable.

(Ging-Go)

🤖

Comment on lines +158 to +169
var mcpCleanup func()
cleanup := func() {
if mcpCleanup != nil {
mcpCleanup()
}
workspaceCtx.close()
}
defer func() {
if err != nil {
cleanup()
}
}()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note [CRF-5] Five reviewers independently noticed this restructuring incidentally fixes a latent MCP connection leak.

In the old code, cleanup() at the g2.Wait() error return (base ~line 276) was just workspaceCtx.close(). The mcpCleanup chaining into cleanup happened after that error check (base ~line 280). Since errgroup.Wait() guarantees all goroutines complete before returning, the MCP goroutine had already set mcpCleanup, but it was never called on that error path. MCP connections leaked until context cancellation.

The new code captures mcpCleanup by reference from the start, so every error path (including the g2.Wait() path) correctly tears down MCP connections. Low practical impact (triggers only when ConvertMessagesWithFiles fails with active MCP configs), but the correctness improvement is real and worth noting in the PR description.

(Hisoka, Pariston, Meruem, Mafuuu, Kite)

🤖

@github-actions github-actions Bot added the stale This issue is like stale bread. label Jun 28, 2026
@github-actions github-actions Bot closed this Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale This issue is like stale bread.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant