Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】拆分 managedcli.updateLocked 中的版本闸门判断与候选安装校验 (#262)

fx-kurumi2adda62237f3
diff --git a/internal/managedcli/updater.go b/internal/managedcli/updater.go
index bd150514..1bee0876 100644
--- a/internal/managedcli/updater.go
+++ b/internal/managedcli/updater.go
@@ -115,34 +115,16 @@ func (m *Manager) updateLocked(ctx context.Context, state *State, report *Report
candidate, err := m.candidate(ctx, provider)
if err == nil {
result.To = candidate.Version
- switch {
- case candidate.Version == result.From:
- result.Status = "unchanged"
- case result.From != "" && compareVersions(candidate.Version, result.From) < 0:
- result.Status, result.Message = "skipped", "automatic downgrade refused"
- case state.Rejected[provider] == candidate.Version:
- result.Status, result.Message = "skipped", "this release was rolled back; waiting for a newer release"
- case m.Now().Before(candidate.Published.Add(observationWindow)):
- result.Status, result.Message = "skipped", "release is still in the observation window"
- }
+ result.Status, result.Message = m.releaseGate(state, provider, result.From, candidate)
}
if err == nil && result.Status == "" {
if !toolsReady {
err = m.prepareTools(ctx)
toolsReady = err == nil
}
- installed := state.Candidates[provider]
- if err == nil && (installed.Version != candidate.Version || installed.Integrity != candidate.Dist.Integrity || validateInventory(m.Root, Inventory{provider: installed}) != nil) {
- installed, err = m.install(ctx, provider, candidate)
- if err == nil {
- // Cache before smoke validation: a failed probe must not cause
- // another download. No active inventory changes here.
- state.Candidates[provider] = installed
- err = m.save(*state)
- }
- }
+ var installed Installation
if err == nil {
- err = m.validate(ctx, provider, installed)
+ installed, err = m.stageCandidate(ctx, state, provider, candidate)
}
if err == nil {
candidates[provider] = installed
@@ -181,6 +163,44 @@ func (m *Manager) updateLocked(ctx context.Context, state *State, report *Report
return errors.Join(failures...)
}
+// releaseGate reports why a resolved release must not be staged for provider.
+// An empty status means the release is eligible for installation.
+func (m *Manager) releaseGate(state *State, provider, from string, candidate release) (status, message string) {
+ switch {
+ case candidate.Version == from:
+ return "unchanged", ""
+ case from != "" && compareVersions(candidate.Version, from) < 0:
+ return "skipped", "automatic downgrade refused"
+ case state.Rejected[provider] == candidate.Version:
+ return "skipped", "this release was rolled back; waiting for a newer release"
+ case m.Now().Before(candidate.Published.Add(observationWindow)):
+ return "skipped", "release is still in the observation window"
+ }
+ return "", ""
+}
+
+// stageCandidate reuses the cached candidate installation when it still matches
+// the release, otherwise installs it, then smoke-validates the result.
+func (m *Manager) stageCandidate(ctx context.Context, state *State, provider string, candidate release) (Installation, error) {
+ installed := state.Candidates[provider]
+ if installed.Version != candidate.Version || installed.Integrity != candidate.Dist.Integrity || validateInventory(m.Root, Inventory{provider: installed}) != nil {
+ var err error
+ if installed, err = m.install(ctx, provider, candidate); err != nil {
+ return Installation{}, err
+ }
+ // Cache before smoke validation: a failed probe must not cause
+ // another download. No active inventory changes here.
+ state.Candidates[provider] = installed
+ if err := m.save(*state); err != nil {
+ return Installation{}, err
+ }
+ }
+ if err := m.validate(ctx, provider, installed); err != nil {
+ return Installation{}, err
+ }
+ return installed, nil
+}
+
func (m *Manager) rollbackLocked(ctx context.Context, provider string, state *State, report *Report) error {
if _, ok := providerSpecs[provider]; !ok {
return errors.New("rollback requires a known provider")
diff --git a/internal/managedcli/updater_gate_test.go b/internal/managedcli/updater_gate_test.go
new file mode 100644
index 00000000..f68236aa
--- /dev/null
+++ b/internal/managedcli/updater_gate_test.go
@@ -0,0 +1,34 @@
+package managedcli
+
+import (
+ "testing"
+ "time"
+)
+
+func TestReleaseGate(t *testing.T) {
+ now := time.Date(2026, 11, 13, 2, 0, 0, 0, time.UTC)
+ m := &Manager{Now: func() time.Time { return now }}
+ old := now.Add(-2 * observationWindow)
+ state := &State{Rejected: map[string]string{"codex": "3.0.0"}}
+ for _, tc := range []struct {
+ name, provider, from, version string
+ published time.Time
+ wantStatus, wantMessage string
+ }{
+ {"same version", "codex", "2.0.0", "2.0.0", old, "unchanged", ""},
+ {"downgrade", "codex", "2.0.0", "1.9.0", old, "skipped", "automatic downgrade refused"},
+ {"rolled back", "codex", "2.0.0", "3.0.0", old, "skipped", "this release was rolled back; waiting for a newer release"},
+ {"rejected only for its provider", "claude", "2.0.0", "3.0.0", old, "", ""},
+ {"observation window", "codex", "2.0.0", "2.1.0", now.Add(-time.Hour), "skipped", "release is still in the observation window"},
+ {"eligible upgrade", "codex", "2.0.0", "2.1.0", old, "", ""},
+ {"first install", "codex", "", "2.1.0", old, "", ""},
+ {"first install in observation window", "codex", "", "2.1.0", now, "skipped", "release is still in the observation window"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ status, message := m.releaseGate(state, tc.provider, tc.from, release{Version: tc.version, Published: tc.published})
+ if status != tc.wantStatus || message != tc.wantMessage {
+ t.Fatalf("releaseGate() = (%q, %q), want (%q, %q)", status, message, tc.wantStatus, tc.wantMessage)
+ }
+ })
+ }
+}
diff --git a/internal/managedcli/updater_stage_test.go b/internal/managedcli/updater_stage_test.go
new file mode 100644
index 00000000..8ab99a45
--- /dev/null
+++ b/internal/managedcli/updater_stage_test.go
@@ -0,0 +1,87 @@
+package managedcli
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// stageFixture returns a manager whose registry offers codex 2.0.0 and whose
+// tools are prepared, plus the resolved codex candidate.
+func stageFixture(t *testing.T) (*updaterFixture, release) {
+ t.Helper()
+ f := newUpdaterFixture(t)
+ f.update(t, false)
+ f.setVersion("2.0.0")
+ ctx := context.Background()
+ must(t, f.manager.prepareTools(ctx))
+ candidate, err := f.manager.candidate(ctx, "codex")
+ must(t, err)
+ return f, candidate
+}
+
+func TestStageCandidateInstallsCachesAndReuses(t *testing.T) {
+ f, candidate := stageFixture(t)
+ ctx := context.Background()
+ state := f.state(t)
+ installed, err := f.manager.stageCandidate(ctx, &state, "codex", candidate)
+ must(t, err)
+ if installed.Version != "2.0.0" || f.installs["codex"] != 2 {
+ t.Fatalf("installed=%+v installs=%d", installed, f.installs["codex"])
+ }
+ if state.Candidates["codex"] != installed || f.state(t).Candidates["codex"] != installed {
+ t.Fatal("staged candidate was not cached in memory and on disk")
+ }
+ // A matching, intact cache is validated again but never reinstalled.
+ again, err := f.manager.stageCandidate(ctx, &state, "codex", candidate)
+ must(t, err)
+ if again != installed || f.installs["codex"] != 2 {
+ t.Fatalf("cache not reused: %+v installs=%d", again, f.installs["codex"])
+ }
+ // A different integrity for the same version invalidates the cache.
+ changed := candidate
+ changed.Dist.Integrity = "sha512-other"
+ if _, err := f.manager.stageCandidate(ctx, &state, "codex", changed); err == nil {
+ t.Fatal("lockfile integrity mismatch accepted")
+ }
+ if f.installs["codex"] != 3 {
+ t.Fatalf("integrity change did not trigger reinstall: installs=%d", f.installs["codex"])
+ }
+}
+
+func TestStageCandidateSaveFailureStopsBeforeValidation(t *testing.T) {
+ f, candidate := stageFixture(t)
+ state := f.state(t)
+ statePath := filepath.Join(f.manager.Root, "state.json")
+ must(t, os.Remove(statePath))
+ must(t, os.Mkdir(statePath, 0700))
+ probes := len(f.environments)
+ installed, err := f.manager.stageCandidate(context.Background(), &state, "codex", candidate)
+ if err == nil {
+ t.Fatal("save failure ignored")
+ }
+ if installed != (Installation{}) {
+ t.Fatalf("returned installation on failure: %+v", installed)
+ }
+ // Only the npm install ran; no --version/--help smoke probe followed.
+ if f.installs["codex"] != 2 || len(f.environments)-probes != 2 {
+ t.Fatalf("installs=%d commands=%d", f.installs["codex"], len(f.environments)-probes)
+ }
+}
+
+func TestStageCandidateValidationFailureKeepsCache(t *testing.T) {
+ f, candidate := stageFixture(t)
+ f.failHelp = "codex"
+ state := f.state(t)
+ installed, err := f.manager.stageCandidate(context.Background(), &state, "codex", candidate)
+ if err == nil || installed != (Installation{}) {
+ t.Fatalf("installed=%+v err=%v", installed, err)
+ }
+ if cached := f.state(t).Candidates["codex"]; cached.Version != "2.0.0" {
+ t.Fatalf("failed smoke test dropped the cached download: %+v", cached)
+ }
+ if f.state(t).Current["codex"].Version != "1.0.0" {
+ t.Fatal("staging changed the active inventory")
+ }
+}