fishmagic80 on X: Stable is too slow (community plugins need a newer
magpie than it has), every release is too fast, and Docker users have
nothing in between. Daily sits between them.
What it is:
- site/worker.js keepDaily: at the daily cut (20:00 UTC, the quarter-hour
cron) the newest tag whose commit has a successful Test run and whose
release has all 17 assets is picked and kept in LEDGER "daily". A day
with none keeps the day before's; a failed GitHub read writes nothing.
/api/latest?channel=daily (503 until a pick exists), /download/daily/<p>,
and an urgent release overlays it as on Stable.
- .github/workflows/docker-daily.yml reads that feed and points
ghcr.io/yetone/magpie:daily at the picked version's image.
- App: settings.UpdateDaily ("daily"); update.Channel()/Picked() (Stable()
kept); the feed is asked channel=daily, never falls back to GitHub's
latest, What's new lists every change since yours. Settings > About >
Update channel has Every version / Daily / Stable; the version row says
"Up to date on Daily (· {v} is out on Every version)". CLI: magpie
update channel [stable|daily|every].
- Words in en, zh, zh-TW, ja, de, on top of 7f832b2f2's 尝鲜版/搶先版/先行版
for Every version (kept as it landed).
- The channel row wraps its switch under its name when narrow (ja at
420px was 18px past the window with three segments).
- app.js budget +338: the segment, its line and the two Up to date on
Daily words, packed to one t() per choice; Settings and renderUpdate
draw on every window, nothing lazy to move them to.
- Docs: docs/reference.md, site docs (docker, privacy in en/zh/ja),
llms-full.txt, search indexes.
Red on the old code (a daily-old worktree, then only settings allowing
"daily"): the feed was not asked channel=daily, 40 notes instead of all,
Picked false, notesKey "zh" not "zh|daily", the Daily row showed
"available"; the GUI tests had no Daily segment.
Run on mote at this commit's tree (on origin/main ec1a1a7a5; the Daily pick itself is live on the deployed worker: /api/latest?channel=daily answers 0.1.4171, /download/daily 302s to its dmg):
- go test -count=1 ./internal/settings ./internal/settingsapi
./internal/update ./internal/cli ./internal/gui: all ok (TestCoreScriptBudget
included).
- GUI, chromium and webkit, every language: update-*.test.cjs (22 files),
i18n-*.test.cjs, settings-search.test.cjs: 554 of 554 pass.
- site: node --test site/*.test.mjs, 132 of 132 (130 before the rebuild).
- gofmt -l clean; go vet darwin, windows (gui/update/cli) and
GOOS=linux -tags nogui.
Also: site/public/search/*.json and llms-full.txt rebuilt
(search-index.mjs, llms-full.mjs); they still listed Antigravity's old
sign-in after 9cd5f3652, so the two site freshness tests were red.
Filed: #6730, engines.magpie is never read, so a plugin needing a newer
magpie installs on Stable (0.1.3078) and fails without saying why
(page-amp-usage >=0.1.3681, middleware-company-guard >=0.1.3841,
page-day-card >=0.1.3427).
Not tried: a real self-update on Daily, a real docker-daily workflow run,
the whole GUI suite, the worker's unauthenticated GitHub reads under a
rate limit.
217 lines
7.0 KiB
Go
217 lines
7.0 KiB
Go
package update
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/source"
|
|
)
|
|
|
|
// A network that lets github.com through and nothing else (a company's
|
|
// allow list: ssharpcc on X) can't reach usemagpie.ai's feed. When the
|
|
// default feed fails, the newest release is read from GitHub itself:
|
|
// yetone/magpie-releases' API, or, when the API is rate-limited, the
|
|
// release page's redirect and the SHA256SUMS beside the files. Nothing
|
|
// is configured. The result goes through Release.verify like the feed's,
|
|
// and here a release must be signed whatever its version: the feed is the
|
|
// only thing that may carry an unsigned old release, and this isn't the
|
|
// feed. The files themselves are on github.com (Asset.GitHub), which
|
|
// download already takes, with its API route.
|
|
//
|
|
// The Stable channel is the site's choice (a release a week), which
|
|
// GitHub can't give: on Stable a failed feed stays a failed feed.
|
|
|
|
// releaseRepo is where releases are published.
|
|
const releaseRepo = "yetone/magpie-releases"
|
|
|
|
// feedWait is how long the feed has to answer when GitHub stands behind
|
|
// it: a network that drops usemagpie.ai's packets would otherwise hold
|
|
// the check for the client's ten minutes. A var for tests.
|
|
var feedWait = 20 * time.Second
|
|
|
|
// siteFeed is the default feed; a var for tests.
|
|
var siteFeed = Site + "/api/latest"
|
|
|
|
// maxSmall bounds SHA256SUMS and its signature.
|
|
const maxSmall = 1 << 20
|
|
|
|
// fallsBack says whether a failed feed goes on to GitHub: the default
|
|
// feed, not the Stable or Daily channel, and not an ask already cancelled.
|
|
func fallsBack(ctx context.Context) bool {
|
|
return ctx.Err() == nil && Feed() == siteFeed && !Picked() && Source() == ""
|
|
}
|
|
|
|
// githubOnly says whether the release is read from GitHub alone, with no
|
|
// try at the feed: Settings' GitHub only, on the default feed and not on
|
|
// Stable or Daily (whose release only the site knows; its files still come
|
|
// from GitHub).
|
|
func githubOnly() bool {
|
|
return Feed() == siteFeed && !Picked() && Source() == settings.UpdateSourceGitHub
|
|
}
|
|
|
|
// githubLatest is the newest release as GitHub gives it, verified.
|
|
func githubLatest(ctx context.Context, lang string) (*Release, error) {
|
|
tag, notes, page, files, err := githubTag(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
base := fmt.Sprintf("%s/%s/releases/download/%s/", githubWeb, releaseRepo, tag)
|
|
sums, err := githubFile(ctx, base+"SHA256SUMS", files["SHA256SUMS"].api)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sig, err := githubFile(ctx, base+"SHA256SUMS.sig", files["SHA256SUMS.sig"].api)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s has no signature: %w", tag, err)
|
|
}
|
|
rel := &Release{
|
|
Version: strings.TrimPrefix(tag, "v"),
|
|
Notes: InLang(notes, lang),
|
|
URL: page,
|
|
Assets: map[string]Asset{},
|
|
Signed: &Signed{Sums: sums, Sig: strings.TrimSpace(sig)},
|
|
}
|
|
if parse(rel.Version) == nil {
|
|
return nil, fmt.Errorf("GitHub's release tag %q is no version", tag)
|
|
}
|
|
// the names come from the signed list; verify() drops any other and
|
|
// gives each file its hash
|
|
for _, line := range strings.Split(sums, "\n") {
|
|
f := strings.Fields(line)
|
|
if len(f) != 2 {
|
|
continue
|
|
}
|
|
name := strings.TrimPrefix(f[1], "*")
|
|
if name == "" || strings.ContainsAny(name, `/\`) || name == "SHA256SUMS" || name == "SHA256SUMS.sig" {
|
|
continue
|
|
}
|
|
u := base + url.PathEscape(name)
|
|
rel.Assets[name] = Asset{URL: u, GitHub: u, API: files[name].api, Size: files[name].size}
|
|
}
|
|
if err := rel.verify(); err != nil {
|
|
return nil, err
|
|
}
|
|
return rel, nil
|
|
}
|
|
|
|
type ghFile struct {
|
|
api string
|
|
size int64
|
|
}
|
|
|
|
// githubTag is the newest release's tag, notes, page and the files the
|
|
// API lists. The API is asked first; limited or unreachable, the release
|
|
// page's redirect gives the tag alone.
|
|
func githubTag(ctx context.Context) (tag, notes, page string, files map[string]ghFile, err error) {
|
|
files = map[string]ghFile{}
|
|
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/repos/%s/releases/latest", githubAPI, releaseRepo), nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
req.Header.Set("Accept", "application/vnd.github+json")
|
|
res, aerr := source.Do(client, req)
|
|
if aerr == nil {
|
|
defer res.Body.Close()
|
|
if res.StatusCode == 200 {
|
|
var gh struct {
|
|
Tag string `json:"tag_name"`
|
|
Body string `json:"body"`
|
|
Page string `json:"html_url"`
|
|
Assets []struct {
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
Size int64 `json:"size"`
|
|
} `json:"assets"`
|
|
}
|
|
if derr := json.NewDecoder(io.LimitReader(res.Body, 8<<20)).Decode(&gh); derr == nil && gh.Tag != "" {
|
|
for _, a := range gh.Assets {
|
|
if strings.HasPrefix(a.URL, githubAPI+"/") {
|
|
files[a.Name] = ghFile{api: a.URL, size: a.Size}
|
|
}
|
|
}
|
|
return gh.Tag, gh.Body, gh.Page, files, nil
|
|
}
|
|
} else {
|
|
aerr = fmt.Errorf("GitHub's API: %s", res.Status)
|
|
}
|
|
}
|
|
// the release page: /releases/latest redirects to /releases/tag/<tag>
|
|
c := *client
|
|
c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
|
pr, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/releases/latest", githubWeb, releaseRepo), nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
pres, perr := source.Do(&c, pr)
|
|
if perr != nil {
|
|
err = fmt.Errorf("GitHub: %w", errors.Join(aerr, perr))
|
|
return
|
|
}
|
|
defer pres.Body.Close()
|
|
loc, _ := url.Parse(pres.Header.Get("Location"))
|
|
if loc != nil {
|
|
if i := strings.LastIndex(loc.Path, "/releases/tag/"); i >= 0 {
|
|
tag = strings.Trim(loc.Path[i+len("/releases/tag/"):], "/")
|
|
}
|
|
}
|
|
if tag == "" || strings.Contains(tag, "/") {
|
|
tag = ""
|
|
err = fmt.Errorf("GitHub's release page named no release (%s)", pres.Status)
|
|
if aerr != nil {
|
|
err = fmt.Errorf("%w; the API: %v", err, aerr)
|
|
}
|
|
return
|
|
}
|
|
return tag, "", fmt.Sprintf("%s/%s/releases/tag/%s", githubWeb, releaseRepo, tag), files, nil
|
|
}
|
|
|
|
// githubFile reads a small release file from its github.com address (the
|
|
// mirror the user gave in front of it), else through the API route when
|
|
// that is known. Its bytes are trusted for nothing until the signature
|
|
// over them is checked.
|
|
func githubFile(ctx context.Context, raw, api string) (string, error) {
|
|
var errs []error
|
|
tries := []struct {
|
|
u string
|
|
api bool
|
|
}{{mirrored(mirrorOf(ctx), raw), false}, {api, true}}
|
|
for _, try := range tries {
|
|
if try.u == "" {
|
|
continue
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, "GET", try.u, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if try.api {
|
|
req.Header.Set("Accept", "application/octet-stream")
|
|
}
|
|
res, err := source.Do(client, req)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
b, err := io.ReadAll(io.LimitReader(res.Body, maxSmall+1))
|
|
res.Body.Close()
|
|
switch {
|
|
case res.StatusCode != 200:
|
|
errs = append(errs, fmt.Errorf("%s: %s", hostOf(try.u), res.Status))
|
|
case err != nil:
|
|
errs = append(errs, err)
|
|
case len(b) > maxSmall:
|
|
errs = append(errs, fmt.Errorf("%s: too large", raw))
|
|
default:
|
|
return string(b), nil
|
|
}
|
|
}
|
|
return "", errors.Join(errs...)
|
|
}
|