Files
magpie/build/conpty/main.go
T
yetone 07f7e0b45d claudetui: Windows 10 gets Microsoft's ConPTY with the release, no folder to place (#5205)
a49f3745e used a conpty folder beside magpie.exe holding conpty.dll and
OpenConsole.exe, which a user had to fetch and place. Windows 10's own
ConPTY drops a paste's bracketed-paste marks and loses 1024-byte blocks,
so without that folder long messages reached Claude Code cut.

Now a Windows release carries the pair. internal/conptypair pins
Microsoft.Windows.Console.ConPTY 1.25.260930003 from nuget.org (MIT): the
package's SHA-512 as nuget.org publishes it (catalog packageHash) and
each file's SHA-256. `make conpty-pair` (go run ./build/conpty, before
release-cli and release-windows) downloads it, checks both, and writes
the pair to internal/conptypair/<arch>/ (gitignored), which -tags
conptypair embeds: about 1.2 MB per Windows binary. At run time, on a
build below 22000 with no conpty folder of the user's, the pair is
written to %LocalAppData%\magpie\conpty\<version>\<arch> (temp file and
rename), each file checked against its pin there before conpty.dll is
loaded; a file that doesn't match is written again. The order is the
user's conpty folder, then the bundled pair on Windows 10, then the
system's; any failure leaves the system's, where the whole-message check
(previous commit) catches a cut message. Windows 11 keeps its own.
1.25.260930003 is the version 3leg0ocat ran whole on Windows 10 19044;
CoreyKatzburg ran 1.25.261109001 on 19045 (under two weeks old).

Run:
- The package hash: the download's SHA-512 matches nuget.org's
  packageHash; the four files' SHA-256 are taken from it.
- Authenticode on xiaomi-gpu (Get-AuthenticodeSignature): all four files
  Valid, CN=Microsoft Corporation.
- go run ./build/conpty on mote: fetched and written; a second run keeps
  them with no download; a package with the wrong hash is refused.
  GOOS=windows go build -tags nogui,packedassets,conptypair for amd64
  and arm64: built, 1.2 MB over the build without the tag.
- win-run (Windows 11 26200) go test -tags conptypair ./internal/conptypair:
  ok; TestBundledOpens writes the pair from the embedded bytes and
  CreatePseudoConsole through its conpty.dll returns S_OK.
- go test ./internal/conptypair ./internal/claudetui on mote: ok.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... on mote: ok.
- Full suite on mote (-timeout 45m) was still running at push, per the
  ship-at-once rule; a red it shows gets fixed or filed.

Not tried: Windows 10 itself (no Windows 10 box here; xiaomi-gpu is
Windows 11, whose own ConPTY doesn't have the bug, so the build gate
sends it to the system's), and a real Claude Code turn through the
bundled pair; the user folder route that loads the same way was
confirmed on Windows 10 by CoreyKatzburg (19045) and 3leg0ocat (19044).
2026-11-16 21:51:26 +08:00

126 lines
2.9 KiB
Go

// conpty fetches the ConPTY a Windows release carries for Windows 10
// (internal/conptypair, #5205): Microsoft.Windows.Console.ConPTY from
// nuget.org, checked against the SHA-512 nuget.org publishes for it, and
// writes each architecture's conpty.dll and OpenConsole.exe, each checked
// against its pinned SHA-256, to internal/conptypair/<arch>/ for -tags
// conptypair to embed. Files there already whole are kept, with no
// download.
//
// Usage: go run ./build/conpty [package.nupkg]
package main
import (
"archive/zip"
"bytes"
"crypto/sha512"
"encoding/base64"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/yetone/magpie/internal/conptypair"
)
const out = "internal/conptypair"
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, "conpty:", err)
os.Exit(1)
}
}
func run(args []string) error {
if whole() {
return nil
}
var pkg []byte
var err error
if len(args) > 0 {
pkg, err = os.ReadFile(args[0])
} else {
pkg, err = fetch()
}
if err != nil {
return err
}
got := sha512.Sum512(pkg)
if h := base64.StdEncoding.EncodeToString(got[:]); h != conptypair.SHA512 {
return fmt.Errorf("package SHA-512 %s, nuget.org publishes %s", h, conptypair.SHA512)
}
z, err := zip.NewReader(bytes.NewReader(pkg), int64(len(pkg)))
if err != nil {
return err
}
for arch, files := range conptypair.Files {
for _, f := range files {
b, err := read(z, f.Zip)
if err != nil {
return err
}
path := filepath.Join(out, arch, f.Name)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
if err := os.WriteFile(path, b, 0o644); err != nil {
return err
}
if err := conptypair.Check(path, f.SHA256); err != nil {
_ = os.Remove(path)
return err
}
}
}
fmt.Println("conpty: ConPTY", conptypair.Version, "written to", out)
return nil
}
// whole is whether every file is there and pinned already.
func whole() bool {
for arch, files := range conptypair.Files {
for _, f := range files {
if conptypair.Check(filepath.Join(out, arch, f.Name), f.SHA256) != nil {
return false
}
}
}
return true
}
func fetch() ([]byte, error) {
c := &http.Client{Timeout: 5 * time.Minute}
var last error
for try := 0; try < 3; try++ {
if try > 0 {
time.Sleep(time.Duration(try) * 5 * time.Second)
}
resp, err := c.Get(conptypair.URL)
if err != nil {
last = err
continue
}
b, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
resp.Body.Close()
if err == nil && resp.StatusCode != http.StatusOK {
err = fmt.Errorf("%s: %s", conptypair.URL, resp.Status)
}
if err == nil {
return b, nil
}
last = err
}
return nil, last
}
func read(z *zip.Reader, name string) ([]byte, error) {
f, err := z.Open(name)
if err != nil {
return nil, fmt.Errorf("package has no %s: %w", name, err)
}
defer f.Close()
return io.ReadAll(io.LimitReader(f, 16<<20))
}