From 117441c04fedf418f40103200a834a7742e1a781 Mon Sep 17 00:00:00 2001 From: Zeusina Date: Mon, 12 Oct 2026 07:23:22 +0300 Subject: [PATCH] fix(managed): enforce experimental CLI policy under transaction lock Bind ordinary candidate-ID CLI activation to a transaction-local prohibition on experimental policy. Resolve and reject both requested and inherited experimental scope while holding the journal lock, leaving guarded operator activation and trusted monitor/reconciliation behavior unchanged. Cover deterministic operator-policy interleaving without timing-dependent concurrency. Co-authored-by: GPT-6 --- cmd/zapret-autotune/managed_cli.go | 10 +--- .../managed_experimental_test.go | 52 +++++++++++++++++++ cmd/zapret-autotune/managed_transaction.go | 6 +++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/cmd/zapret-autotune/managed_cli.go b/cmd/zapret-autotune/managed_cli.go index 0d30883..97bbaaf 100644 --- a/cmd/zapret-autotune/managed_cli.go +++ b/cmd/zapret-autotune/managed_cli.go @@ -428,14 +428,8 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int { expected[id] = group } } - journal, loadErr := m.load() - if loadErr != nil { - err = loadErr - } else if journal.Scope == GooglevideoScopeExperimental && len(scopes) == 0 { - err = fmt.Errorf("experimental CLI apply requires explicit guarded operator path") - } else { - err = managedActivate(m, d, ids, expected, scopes...) - } + m.forbidExperimentalApply = true + err = managedActivate(m, d, ids, expected, scopes...) } } if err != nil { diff --git a/cmd/zapret-autotune/managed_experimental_test.go b/cmd/zapret-autotune/managed_experimental_test.go index 7296b34..c9b9833 100644 --- a/cmd/zapret-autotune/managed_experimental_test.go +++ b/cmd/zapret-autotune/managed_experimental_test.go @@ -284,12 +284,64 @@ func TestExperimentalSuffixCLIRejectsInheritedPolicy(t *testing.T) { t.Fatal("inherited bypass requested Player") return nil } + d.openDB = func() (*sql.DB, error) { return db, nil } code, _, diagnostic := callManaged(t, d, "apply", "--googlevideo-candidate", fmt.Sprint(id)) if code != 1 || !strings.Contains(diagnostic, "explicit guarded operator path") { t.Fatal(code, diagnostic) } } +func TestExperimentalSuffixCLIInterleavedPolicyChange(t *testing.T) { + d, _, workers := managedCLIFixture(t) + initManagedCLI(t, d) + db := testStore(t) + front := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=fake", true) + asset := unitAssetCandidate(t, db, "i.ytimg.com", "--lua-desync=fake", true) + id := managedCandidateFixture(t, db, "googlevideo", "rr1---sn-alpha.googlevideo.com", managedGooglevideoArgs, false) + operator := &managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }} + if err := operator.Apply(db, []int64{front, asset}); err != nil { + t.Fatal(err) + } + var snapshot map[string][]byte + // No timing race: commit the operator policy after ordinary CLI dispatch, + // before its transaction reads the journal under the apply lock. + d.openDB = func() (*sql.DB, error) { + if err := operator.Apply(db, []int64{front, asset, id}, GooglevideoScopeExperimental); err != nil { + t.Fatal(err) + } + snapshot = map[string][]byte{} + for _, name := range []string{"config", managedJournalName, managedBackupName} { + b, err := operator.read(name) + if err != nil { + t.Fatal(err) + } + snapshot[name] = b + } + return db, nil + } + d.player = func(context.Context, []string, io.Writer, io.Writer) error { + t.Fatal("racing CLI requested Player") + return nil + } + code, _, diagnostic := callManaged(t, d, "apply", "--frontend-candidate", fmt.Sprint(front), "--googlevideo-candidate", fmt.Sprint(id), "--asset-candidate", fmt.Sprint(asset)) + if code != 1 || !strings.Contains(diagnostic, "explicit guarded operator path") { + t.Fatal(code, diagnostic) + } + if *workers != 0 { + t.Fatal("unauthorized reload/worker started", *workers) + } + for name, before := range snapshot { + after, err := operator.read(name) + if err != nil || !bytes.Equal(before, after) { + t.Fatalf("racing CLI mutated %s: %v", name, err) + } + } + j, err := operator.load() + if err != nil || j.Scope != GooglevideoScopeExperimental || !reflect.DeepEqual(j.Candidates, []int64{front, asset, id}) { + t.Fatalf("operator deployment lost: %+v %v", j, err) + } +} + func TestExperimentalSuffixPreparedRollback(t *testing.T) { db := testStore(t) front := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=fake", true) diff --git a/cmd/zapret-autotune/managed_transaction.go b/cmd/zapret-autotune/managed_transaction.go index f2a1656..276178c 100644 --- a/cmd/zapret-autotune/managed_transaction.go +++ b/cmd/zapret-autotune/managed_transaction.go @@ -51,6 +51,9 @@ type managedTransaction struct { Dir, Name string Reload func() error Health func() (string, error) + // Ordinary candidate-ID CLI calls cannot inherit an operator experiment. + // Evaluated against the effective policy only while holding the journal lock. + forbidExperimentalApply bool // Tests may simulate process death after durable boundaries. Such errors leave // the journal intact for Reconcile, deliberately bypassing normal rollback. fault func(string) error @@ -357,6 +360,9 @@ func (m *managedTransaction) applyRenderedPolicy(b []byte, db *sql.DB, requested if requested != nil { scope = *requested } + if m.forbidExperimentalApply && scope == GooglevideoScopeExperimental { + return fmt.Errorf("experimental CLI apply requires explicit guarded operator path") + } if requested != nil && scope == GooglevideoScopeExperimental { for _, id := range j.Candidates { c, err := readStrategyCandidatePolicy(db, id, false)