Files
magpie/internal/update/notes.go
T
yetone b466aa2803 update: a Daily channel, one release a day that passed its tests, for the app and Docker (fishmagic80 on X)
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.
2026-11-15 13:10:18 +08:00

329 lines
10 KiB
Go

package update
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"slices"
"sort"
"strconv"
"strings"
"github.com/yetone/magpie/internal/lazyre"
"github.com/yetone/magpie/internal/settings"
)
// Note is one release's notes, as the app shows them after an update.
type Note struct {
Version string `json:"version"` // no v
Notes string `json:"notes"` // markdown, its Install section taken out
URL string `json:"url,omitempty"`
// Pending: a release after the one running, up to the update waiting
Pending bool `json:"pending,omitempty"`
}
// NotesFeed is where the notes of past releases are listed: beside the
// update feed, on magpie's site. MAGPIE_NOTES_FEED points it elsewhere.
func NotesFeed() string {
if f := os.Getenv("MAGPIE_NOTES_FEED"); f != "" {
return f
}
return Site + "/api/notes"
}
// ReleasePage is version's page on GitHub, where its notes are when the
// site can't give them (#661).
func ReleasePage(version string) string {
return "https://github.com/yetone/magpie-releases/releases/tag/v" + strings.TrimPrefix(strings.TrimSpace(version), "v")
}
// NotesBetween asks the site for the notes of every release after after
// (none when "") up to and including upto, newest first, in lang (see
// InLang).
func NotesBetween(ctx context.Context, after, upto, lang string) ([]Note, error) {
notes, _, err := NotesBetweenTotal(ctx, after, upto, lang)
return notes, err
}
// NotesBetweenTotal is NotesBetween with how many releases there are in
// all before MostNotes keeps the newest, so a list cut short can say so
// (sujingshen on X: 156 releases behind read "40 releases").
func NotesBetweenTotal(ctx context.Context, after, upto, lang string) ([]Note, int, error) {
q := url.Values{"upto": {upto}}
if after != "" {
q.Set("after", after)
}
notes, err := askNotes(ctx, q, lang)
if err != nil {
return nil, 0, err
}
all := between(notes, after, upto)
return capNotes(all), len(all), nil
}
// BackCounts are the ranges Settings' What's new offers besides the notes
// since the last update: this version and that many before it (Linka on
// Discord).
var BackCounts = []int{10, 20, 50, 100}
// NotesBack asks the site for the notes of upto and the n releases before
// it, newest first, in lang. The site is asked for those alone (limit); one
// from before it took a limit sends them all, and Back keeps n+1.
func NotesBack(ctx context.Context, upto string, n int, lang string) ([]Note, error) {
notes, err := askNotes(ctx, url.Values{"upto": {upto}, "limit": {strconv.Itoa(n + 1)}}, lang)
if err != nil {
return nil, err
}
return Back(notes, upto, n), nil
}
// askNotes is the site's list for q, each release's notes in lang.
func askNotes(ctx context.Context, q url.Values, lang string) ([]Note, error) {
if lang != "" {
q.Set("lang", lang)
}
req, err := http.NewRequestWithContext(ctx, "GET", NotesFeed()+"?"+q.Encode(), nil)
if err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("release notes: %s", res.Status)
}
var out struct {
Releases []Note `json:"releases"`
}
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
return nil, fmt.Errorf("release notes: %w", err)
}
for i := range out.Releases {
out.Releases[i].Notes = InLang(out.Releases[i].Notes, lang)
}
return out.Releases, nil
}
// zhMarker is where a release's notes turn from English to Chinese: the
// release workflow translates them and puts the Chinese below it.
const zhMarker = "<!-- lang:zh -->"
// InLang is a release's notes in lang, as the site cuts them (a site from
// before it did, or a feed elsewhere, hands them whole): "zh" the Chinese
// where there is some, else the English, everything above the marker.
// Notes from before the marker are English alone, in any language.
func InLang(md, lang string) string {
i := strings.Index(md, zhMarker)
if i < 0 {
return md
}
if lang == "zh" {
if zh := strings.TrimSpace(md[i+len(zhMarker):]); zh != "" {
return zh
}
}
return strings.TrimSpace(md[:i])
}
// withLang adds lang to a feed's address, as ?lang= (or &lang=).
func withLang(feed, lang string) string {
if lang == "" {
return feed
}
u, err := url.Parse(feed)
if err != nil {
return feed
}
q := u.Query()
q.Set("lang", lang)
u.RawQuery = q.Encode()
return u.String()
}
// MaxNotes is how many releases' notes are shown at most: magpie releases
// often, and a long-unopened one would have pages of them.
const MaxNotes = 40
// StableMaxNotes is MaxNotes on the Stable and Daily channels, where an
// update is a week or a day of releases and its notes are read as one list
// (ssharpcc on X): every one of them, as far as the site keeps them.
const StableMaxNotes = 1500
// MostNotes is how many releases' notes are shown at most on the channel
// Settings name.
func MostNotes() int {
if Picked() {
return StableMaxNotes
}
return MaxNotes
}
// Channel is Settings' Update channel: settings.UpdateStable, the update
// feed asked for a version a week, the newest that had been out three days
// as the week began (Monday 00:00 UTC); settings.UpdateDaily, a version a
// day, the newest out by 20:00 UTC that passed its tests (the Test run on
// its commit) and has every file; either one with an urgent release (a
// security hole, a ban risk, lost data) as soon as it is out. "" is every
// version, and so is a value magpie doesn't know. usemagpie.ai chooses
// both from the release list (site/worker.js, "The Stable channel" and
// "The Daily channel"); a release is urgent when a line of its notes
// starts with "**Urgent release:**".
func Channel() string {
switch c := settings.Shared().UpdateChannel; c {
case settings.UpdateStable, settings.UpdateDaily:
return c
}
return ""
}
// Picked is whether the update feed picks the version (Stable or Daily)
// rather than offering every one: only the site knows a picked release.
func Picked() bool { return Channel() != "" }
// Stable is whether Settings' Update channel is Stable.
func Stable() bool { return Channel() == settings.UpdateStable }
// withChannel is feed asking for the channel's release on Stable or Daily,
// and feed as it is on every version.
func withChannel(feed string) string {
ch := Channel()
if ch == "" {
return feed
}
u, err := url.Parse(feed)
if err != nil {
return feed
}
q := u.Query()
q.Set("channel", ch)
u.RawQuery = q.Encode()
return u.String()
}
// Between keeps the notes of the releases after after up to and including
// upto, newest first, at most MostNotes; with after "" only upto's. Each
// one's Install section is taken out, and one left empty is dropped.
func Between(notes []Note, after, upto string) []Note {
return capNotes(between(notes, after, upto))
}
// capNotes keeps the newest MostNotes of notes, newest first.
func capNotes(notes []Note) []Note {
if most := MostNotes(); len(notes) > most {
return notes[:most]
}
return notes
}
// between is Between with every release kept.
func between(notes []Note, after, upto string) []Note {
var out []Note
seen := map[string]bool{}
for _, n := range notes {
v := strings.TrimPrefix(strings.TrimSpace(n.Version), "v")
if parse(v) == nil || seen[v] || Newer(v, upto) {
continue
}
if after == "" {
if Newer(upto, v) {
continue
}
} else if !Newer(v, after) {
continue
}
n.Version, n.Notes = v, StripInstall(n.Notes)
if strings.TrimSpace(n.Notes) == "" {
continue
}
seen[v] = true
out = append(out, n)
}
sort.SliceStable(out, func(i, j int) bool { return Newer(out[i].Version, out[j].Version) })
return out
}
// Back keeps upto and the n releases before it, newest first: n+1
// releases counted before any is dropped, so a release whose notes are
// empty once its Install section is out still counts as one. A release
// newer than upto, or one listed twice, isn't counted.
func Back(notes []Note, upto string, n int) []Note {
var keep []Note
seen := map[string]bool{}
for _, r := range notes {
v := strings.TrimPrefix(strings.TrimSpace(r.Version), "v")
if parse(v) == nil || seen[v] || Newer(v, upto) {
continue
}
seen[v] = true
r.Version = v
keep = append(keep, r)
}
sort.SliceStable(keep, func(i, j int) bool { return Newer(keep[i].Version, keep[j].Version) })
if len(keep) > n+1 {
keep = keep[:n+1]
}
out := []Note{}
for _, r := range keep {
if r.Notes = StripInstall(r.Notes); strings.TrimSpace(r.Notes) != "" {
out = append(out, r)
}
}
return out
}
var heading = lazyre.New(`^(#{1,6})\s+(.*?)\s*#*\s*$`)
// installHeadings are the Install section's heading in each language the
// notes come in: the Chinese notes call it 安装, and every release's own
// section would otherwise repeat in What's new.
var installHeadings = []string{"Install", "安装", "安裝", "インストール"}
// StripInstall takes a release's "Install" section (in any language) out of
// its notes: the download links, which mean nothing to a magpie that just
// updated itself.
// The section runs to the next heading of its level or above.
func StripInstall(md string) string {
var out []string
skip := 0 // the level of the Install heading being skipped, 0 when not
for _, line := range strings.Split(strings.ReplaceAll(md, "\r\n", "\n"), "\n") {
if m := heading.FindStringSubmatch(line); m != nil {
level := len(m[1])
if skip > 0 && level <= skip {
skip = 0
}
if slices.ContainsFunc(installHeadings, func(h string) bool { return strings.EqualFold(m[2], h) }) {
skip = level
}
}
if skip == 0 {
out = append(out, line)
}
}
return strings.TrimSpace(strings.Join(out, "\n"))
}
// ShowNotes decides, as magpie starts on current having last run as last
// ("" when it never said), whether to show what changed since, and from
// which version on (exclusive). Only an upgrade between releases shows:
// not a fresh install, the same version again, a downgrade, or a build
// from source. A magpie from before versions were kept has no last; when
// it was already in use (existed) its own version's notes are shown, from
// "", which Between takes as upto's alone.
func ShowNotes(last, current string, existed bool) (after string, show bool) {
if !Released(current) {
return "", false
}
if last == "" {
return "", existed
}
if !Newer(current, last) {
return "", false
}
return strings.TrimPrefix(strings.TrimSpace(last), "v"), true
}