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 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-12 07:23:22 +03:00
1 parent f88c8e9c45
commit 117441c04f
3 files changed
+59 -7

No files matched your search

+1 -7
View File
@@ -428,16 +428,10 @@ 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 {
m.forbidExperimentalApply = true
err = managedActivate(m, d, ids, expected, scopes...)
}
}
}
if err != nil {
fmt.Fprintln(stderr, "error:", err)
if errors.Is(err, errManagedScopeUnresolved) {
@@ -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)
@@ -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)