The chat `read_template` tool returned the parameter defaults recorded at template import. Defaults that depend on the workspace owner, such as a region chosen from the owner's groups, were therefore wrong for many users, and an agent that omitted the parameter in `create_workspace` built a workspace with a different value than the tool had shown. `read_template` now evaluates the active version's parameters for the chat owner with no inputs, through `dynamicparameters.Prepare` and the same `Render` call `wsbuilder` makes for a first build, via a `RenderTemplateParametersFn` hook wired from coderd like the other chat hooks. The result carries a `parameters_note` naming the source: owner-evaluated build values, import rows for templates on the classic parameter flow (which `wsbuilder` also builds from), a retry note while the version is still importing, or import rows with a warning when evaluation fails. A parameter whose evaluated default fails its own validation is reported as `error` instead of a usable `default`. Only an evaluated default is checked: without one, preview and the static renderer validate an empty value, so required parameters and unevaluated defaults are never reported as broken, and their remaining errors surface at build time. Fields preview cannot evaluate before the build are shown as evaluated rather than filled from the import row, because preview's output does not distinguish a default, option set, or validation bound that is unknown (it depends on `data.coder_provisioner`) from one that is null for this owner: an unevaluated default carries a `default_note` saying so, an option with an unknown value is omitted, with preview's invalid-options error surfaced as `error` when the parameter has an evaluated default, and bounds appear exactly as rendered. The one import-row fill is for parameters a `module_not_loaded` render dropped, keyed on the same diagnostic code `wsbuilder` uses. Import rows carry no module provenance, so that fill also restores root-level rows the render omitted for this owner; every restored row carries a `note` marking it an import-time estimate. The build-values note is scoped to a request that omits every parameter and matches no preset, since a default that depends on another parameter is re-evaluated against the values `create_workspace` passes, a preset can match without a `preset_id`, and a claimed prebuilt workspace can keep its own values. When evaluation falls back, the warning logs only diagnostic codes, because diagnostic text can quote owner attributes. Measured on dogfood, evaluation adds about 4 ms for a two-parameter template and about 70 ms for the 6-parameter, 19-module `coder` template per `read_template` call, matching the workspace create form's evaluate endpoint, with no file cache growth across repeated calls. > Xum acted for Mike on this pull request.
package chattool
import (
"bufio"
"strings"
"unicode"
"github.com/coder/coder/v2/coderd/render"
coderstrings "github.com/coder/coder/v2/coderd/util/strings"
)
// readmeInputMaxBytes caps how many README bytes are parsed so a giant README
// can't OOM coderd. It sits well above the output rune cap, so it never trims a
// real excerpt.
const readmeInputMaxBytes = 64 * 1024
// readmeText returns the README as bounded, frontmatter-stripped plain text
// truncated to maxRunes, or "" when the README is blank or conversion fails.
func readmeText(readme string, maxRunes int) string {
// Cap the parse input first (see readmeInputMaxBytes); goldmark and the
// tokenizer tolerate a mid-line or mid-rune cut.
bounded := readme[:min(len(readme), readmeInputMaxBytes)]
text, err := render.InnerTextFromMarkdown(stripReadmeFrontmatter(bounded))
if err != nil {
return ""
}
return coderstrings.Truncate(text, maxRunes, coderstrings.TruncateWithEllipsis)
}
// stripReadmeFrontmatter strips a leading frontmatter block from the README if it
// exists. An unterminated frontmatter block is treated as a regular body section.
// UTF-8 BOMs are stripped if present, and CRLF is normalized to LF. Leading
// whitespace is also stripped.
func stripReadmeFrontmatter(readme string) string {
trimmed := strings.TrimLeftFunc(readme, func(r rune) bool {
return unicode.IsSpace(r) || r == '\ufeff'
})
var out strings.Builder
scn := bufio.NewScanner(strings.NewReader(trimmed))
scn.Buffer(nil, readmeInputMaxBytes+1) // headroom
var lineNumber int
var fences int
for scn.Scan() {
line := scn.Text()
lineNumber++
// Only handle fences if we haven't already found an
// opening and closing fence.
if fences < 2 {
isFence := strings.TrimRight(line, " \t\r") == "---"
if isFence {
fences++
continue
} else if lineNumber == 1 {
// No leading fence -> no frontmatter. Return the entire document.
return trimmed
}
// We are still in the frontmatter block. Skip writing this line.
continue
}
_, _ = out.WriteString(line)
_, _ = out.WriteString("\n")
}
// Can err if input is too big. Shouldn't happen normally.
if scn.Err() != nil {
return trimmed
}
// Scanner did not scan any lines. No frontmatter to strip.
if lineNumber == 0 {
return trimmed
}
// If we are still fenced but we reached the end of the document
// we have an unterminated fence.
if fences == 1 {
return trimmed
}
return out.String()
}