Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): strip Windows chcp noise from runtime version (#2516) (#2521)

Bohan Jiang178cfb500846
diff --git a/server/pkg/agent/claude.go b/server/pkg/agent/claude.go
index 11adfb40b0..a50cf6235b 100644
--- a/server/pkg/agent/claude.go
+++ b/server/pkg/agent/claude.go
@@ -581,7 +581,31 @@ func detectCLIVersion(ctx context.Context, execPath string) (string, error) {
if err != nil {
return "", fmt.Errorf("detect version for %s: %w", execPath, err)
}
- return strings.TrimSpace(string(data)), nil
+ return extractVersionLine(string(data)), nil
+}
+
+// extractVersionLine pulls the version line out of a `<cli> --version` capture,
+// discarding leading shell noise. On Windows, npm-installed CLI shims (notably
+// gemini's) emit `chcp` output like `Active code page: 65001` before the real
+// version reaches stdout, and the raw concatenation was being persisted as the
+// runtime version (see #2516).
+//
+// The heuristic: return the first non-empty line that contains a semver-shaped
+// token (matches versionRe). Full version strings like "2.1.5 (Claude Code)"
+// or "codex-cli 0.118.0" survive unchanged because the whole matching line is
+// returned. If no line carries a semver token, fall back to the trimmed raw
+// output so unusual version formats aren't silently dropped to empty.
+func extractVersionLine(raw string) string {
+ for _, line := range strings.Split(raw, "\n") {
+ line = strings.TrimSpace(line)
+ if line == "" {
+ continue
+ }
+ if versionRe.MatchString(line) {
+ return line
+ }
+ }
+ return strings.TrimSpace(raw)
}
// logWriter adapts a *slog.Logger to an io.Writer for capturing stderr.
diff --git a/server/pkg/agent/version_test.go b/server/pkg/agent/version_test.go
index 1aa6ccdec2..da65ce14e3 100644
--- a/server/pkg/agent/version_test.go
+++ b/server/pkg/agent/version_test.go
@@ -78,6 +78,65 @@ func TestCheckMinCLIVersion(t *testing.T) {
}
}
+func TestExtractVersionLine(t *testing.T) {
+ tests := []struct {
+ name string
+ raw string
+ want string
+ }{
+ {
+ name: "bare semver",
+ raw: "0.42.0\n",
+ want: "0.42.0",
+ },
+ {
+ name: "claude full string preserved",
+ raw: "2.1.5 (Claude Code)\n",
+ want: "2.1.5 (Claude Code)",
+ },
+ {
+ name: "codex prefix preserved",
+ raw: "codex-cli 0.118.0\n",
+ want: "codex-cli 0.118.0",
+ },
+ // Reproduces #2516: gemini's Windows shim emits `chcp` output to stdout
+ // before the real version. The chcp line has no dotted-number form,
+ // so the semver scan skips it and picks up "0.42.0" from the next line.
+ {
+ name: "windows chcp prefix before version",
+ raw: "Active code page: 65001\n0.42.0\n",
+ want: "0.42.0",
+ },
+ {
+ name: "windows chcp prefix CRLF",
+ raw: "Active code page: 65001\r\n0.42.0\r\n",
+ want: "0.42.0",
+ },
+ {
+ name: "leading blank lines",
+ raw: "\n\n 0.42.0\n",
+ want: "0.42.0",
+ },
+ {
+ name: "non-semver output falls back to trimmed raw",
+ raw: " some-build-id \n",
+ want: "some-build-id",
+ },
+ {
+ name: "empty input",
+ raw: "",
+ want: "",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := extractVersionLine(tt.raw); got != tt.want {
+ t.Errorf("extractVersionLine(%q) = %q, want %q", tt.raw, got, tt.want)
+ }
+ })
+ }
+}
+
func TestCheckMinVersion(t *testing.T) {
tests := []struct {
agentType string