fix(startup): reconcile profiles before bounded activation

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-10 23:27:53 +03:00
1 parent b1e458a0a5
commit e2cfccedf1
9 files changed
+338 -25

No files matched your search

+1 -1
View File
@@ -15,7 +15,7 @@ Commands:
discover youtube --video-id ID --proxy socks5://HOST:PORT
Discover cache hostname only (no media-transfer check)
health youtube One-shot Player API health through configured loopback SOCKS
managed init|status|apply|monitor
managed init|reconcile|status|apply|monitor
Guarded activation/monitoring in the dedicated managed-test target only
check youtube Refuse checks until probe URLs/video IDs are configured and agreed
tune youtube Test youtube.com frontend only; print options without applying
+45 -4
View File
@@ -55,8 +55,8 @@ func managedDependencies(player playerRunner) managedDeps {
}
func managedArguments(args []string) (string, []int64, error) {
invalid := fmt.Errorf("managed accepts init, status, active-ids, or apply with one or both candidate flags (full desired set); no path flags")
if len(args) == 1 && (args[0] == "init" || args[0] == "status" || args[0] == "active-ids") {
invalid := fmt.Errorf("managed accepts init, reconcile, status, active-ids, or apply with one or both candidate flags (full desired set); no path flags")
if len(args) == 1 && (args[0] == "init" || args[0] == "reconcile" || args[0] == "status" || args[0] == "active-ids") {
return args[0], nil, nil
}
if (len(args) != 3 && len(args) != 5) || args[0] != "apply" {
@@ -326,7 +326,9 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
if command == "init" {
err = managedInitialize(m)
} else if err = managedCheckIdentity(m); err == nil {
if command == "status" || command == "active-ids" {
if command == "reconcile" {
err = managedPrestartReconcile(m, d)
} else if command == "status" || command == "active-ids" {
err = managedReport(m, stdout, d.openDB, command == "active-ids")
} else {
expected := map[int64]string{}
@@ -341,7 +343,7 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
fmt.Fprintln(stderr, "error:", err)
return 1
}
if command == "init" || command == "apply" {
if command == "init" || command == "reconcile" || command == "apply" {
if _, err := fmt.Fprintln(stdout, "managed-test "+command+": OK"); err != nil {
return 1
}
@@ -354,6 +356,45 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
return 0
}
// Startup may activate only a known baseline or DB-qualified rendered config.
// Reconcile restores bytes before its fixed target-local restart hook runs.
func managedPrestartReconcile(m *managedTransaction, d managedDeps) (err error) {
path, err := managedMonitorDatabase(d)
if err != nil {
return err
}
db, err := openStore(path)
if err != nil {
return fmt.Errorf("candidate database unavailable")
}
defer func() {
if closeErr := db.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
// The transaction engine supports legacy hash-only state for other callers;
// initial activation must never adopt it, even with a matching hash.
err = m.locked(func() error {
j, err := m.load()
if err != nil {
return err
}
baselineHash := managedHash([]byte(managedBaseline))
if j.Version != 2 || (len(j.Candidates) == 0 && j.Hash != baselineHash) || (j.State == "prepared" && len(j.PendingCandidates) == 0 && j.Next != baselineHash) {
return fmt.Errorf("startup candidate state unresolved; refusing activation")
}
return nil
})
if err != nil {
return err
}
if err := m.Reconcile(db); err != nil {
return err
}
_, err = m.ActiveCandidates(db)
return err
}
func managedActivate(m *managedTransaction, d managedDeps, ids []int64, expected map[int64]string) error {
info, err := os.Stat(d.script)
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 {
+6 -1
View File
@@ -340,7 +340,7 @@ func TestManagedCLIIdentityAndStatusRedaction(t *testing.T) {
func TestManagedCLIFreshTargetContract(t *testing.T) {
for _, mode := range []string{"wrong-id", "missing-id", "old-env", "other-directory", "old-config", "other-config", "non-mountpoint", "missing-base-runtime", "wrong-port", "other-loopback", "wrong-process", "missing-proxy", "missing-port"} {
for _, command := range []string{"init", "status", "apply"} {
for _, command := range []string{"init", "reconcile", "status", "apply"} {
t.Run(mode+"/"+command, func(t *testing.T) {
d, env, calls := managedCLIFixture(t)
if command != "init" {
@@ -382,6 +382,11 @@ func TestManagedCLIFreshTargetContract(t *testing.T) {
if code, _, _ := callManaged(t, d, args...); code == 0 || *calls != 0 {
t.Fatal("unsafe target contract accepted or reloaded")
}
if command == "reconcile" {
if _, err := os.Lstat(filepath.Join(d.directory, "candidates.db")); !errors.Is(err, os.ErrNotExist) {
t.Fatal("runtime guard opened database", err)
}
}
if command == "init" {
entries, err := os.ReadDir(d.directory)
if err != nil || len(entries) != 0 {
+244 -4
View File
@@ -14,6 +14,213 @@ import (
"time"
)
// Subprocess adapter exercises the real CLI/transaction/DB behind the shell
// entrypoint. Only runtime/socket validation and fixed fixture paths are fake.
func TestManagedPrestartHelper(t *testing.T) {
if os.Getenv("MANAGED_PRESTART_HELPER") != "1" {
return
}
args := os.Args
for len(args) > 0 && args[0] != "--" {
args = args[1:]
}
if len(args) < 3 {
os.Exit(1)
}
args = args[2:] // -- managed COMMAND
if args[0] == "monitor" {
os.Exit(78) // Stop after initial activation; no scans or network.
}
d := managedDependencies(nil)
d.directory, d.script = os.Getenv("FIXTURE_TARGET"), os.Getenv("FIXTURE_SCRIPT")
d.verifyRuntime = func(string) error { return nil }
d.verifySOCKS = func(int) error { return nil }
os.Exit(managedCLI(args, os.Stdout, os.Stderr, d))
}
func TestManagedEntrypointPrestartReconciliation(t *testing.T) {
for _, mode := range []string{"fresh", "baseline", "committed", "prepared", "drift", "unknown", "legacy", "corrupt-backup", "unqualified", "reload-failure"} {
t.Run(mode, func(t *testing.T) {
d, env, _ := managedCLIFixture(t)
env["ZAPRET_AUTOTUNE_DB"] = managedTargetDirectory + "/candidates.db"
m := &managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }}
known := []byte(managedBaseline)
if mode != "fresh" {
initManagedCLI(t, d)
}
if mode == "committed" || mode == "prepared" || mode == "corrupt-backup" || mode == "unqualified" || mode == "reload-failure" {
db, err := openStore(filepath.Join(d.directory, "candidates.db"))
if err != nil {
t.Fatal(err)
}
id := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
if mode != "committed" {
m.fault = func(point string) error {
if point == "replaced" {
return errors.New("simulated process death")
}
return nil
}
}
err = m.Apply(db, []int64{id})
if (mode == "committed") != (err == nil) {
t.Fatal(err)
}
if mode == "committed" {
known, err = renderManagedConfig(db, []int64{id})
if err != nil {
t.Fatal(err)
}
} else {
j, err := m.load()
b, readErr := m.read("config")
if err != nil || readErr != nil || j.State != "prepared" || managedHash(b) != j.Next || j.Next == j.Hash {
t.Fatal("not a true interrupted replacement fixture", j, err, readErr)
}
}
if mode == "unqualified" {
if _, err := db.Exec("DELETE FROM candidate_evidence WHERE candidate_id = ?", id); err != nil {
t.Fatal(err)
}
}
if err := db.Close(); err != nil {
t.Fatal(err)
}
}
if mode == "drift" || mode == "unknown" || mode == "corrupt-backup" {
name := "config"
if mode == "corrupt-backup" {
name = managedBackupName
}
if err := m.replace(name, []byte("UNRECOGNIZED=1\n")); err != nil {
t.Fatal(err)
}
}
if mode == "unknown" || mode == "legacy" {
j, err := m.load()
if err != nil {
t.Fatal(err)
}
if mode == "legacy" {
j.Version = 1
} else {
j.Hash = managedHash([]byte("UNRECOGNIZED=1\n"))
}
if err := m.journal(j); err != nil {
t.Fatal(err)
}
}
root := t.TempDir()
write := func(name, text string) string {
path := filepath.Join(root, name)
if err := os.WriteFile(path, []byte(text), 0700); err != nil {
t.Fatal(err)
}
return path
}
ss := filepath.Join(root, "ss")
if err := os.Mkdir(ss, 0700); err != nil {
t.Fatal(err)
}
write("awk", "#!/bin/sh\nexit 0\n")
child := "#!/bin/sh\ntrap 'exit 0' TERM INT\nwhile :; do sleep 0.1 & wait $!; done\n"
write("ss-server", child)
write("ss-local", child)
log := filepath.Join(root, "log")
zapret := write("zapret", "#!/bin/sh\nprintf '%s ' \"$1\" >> \"$LOG\"\nsha256sum \"$FIXTURE_CONFIG\" >> \"$LOG\"\n[ \"$MODE:$1\" != reload-failure:restart ]\n")
binary, err := os.Executable()
if err != nil {
t.Fatal(err)
}
autotune := write("autotune", "#!/bin/sh\nexec \"$FIXTURE_BINARY\" -test.run='^TestManagedPrestartHelper$' -- \"$@\"\n")
data, err := os.ReadFile("../../managed-target-entrypoint.sh")
if err != nil {
t.Fatal(err)
}
entry := write("entrypoint", strings.NewReplacer("/run/zapret-autotune-ss", ss, "/usr/local/bin/zapret-autotune", autotune, managedZapretScript, zapret, `"$ZAPRET_CONFIG"`, `"$FIXTURE_CONFIG"`).Replace(string(data)))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "/bin/sh", entry)
cmd.Env = append(os.Environ(), "PATH="+root+":"+os.Getenv("PATH"), "LOG="+log, "MODE="+mode, "SS_PASSWORD="+strings.Repeat("a", 32), "MANAGED_PRESTART_HELPER=1", "FIXTURE_BINARY="+binary, "FIXTURE_TARGET="+d.directory, "FIXTURE_SCRIPT="+zapret)
for key, value := range env {
cmd.Env = append(cmd.Env, key+"="+value)
}
// The CLI guard sees fixed production bindings; only fake daemon reads
// use the fixture config, via its own script rather than CLI path flags.
cmd.Env = append(cmd.Env, "FIXTURE_CONFIG="+filepath.Join(d.directory, "config"))
output, err := cmd.CombinedOutput()
var exit *exec.ExitError
if ctx.Err() != nil || !errors.As(err, &exit) || exit.ExitCode() != 78 {
t.Fatal(err, ctx.Err(), string(output))
}
b, _ := os.ReadFile(log)
allowed := mode == "fresh" || mode == "baseline" || mode == "committed" || mode == "prepared"
if !allowed && mode != "reload-failure" && len(b) != 0 {
t.Fatal("unsafe startup invoked daemon script", string(b))
}
if allowed && !strings.Contains("\n"+string(b), "\nstart "+managedHash(known)) {
t.Fatal("no committed initial activation", string(b), string(output))
}
for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n") {
if line != "" && !strings.Contains(line, managedHash(known)) {
t.Fatal("daemon saw uncommitted bytes", line)
}
}
if mode == "reload-failure" && (strings.HasPrefix(string(b), "start ") || strings.Contains(string(b), "\nstart ")) {
t.Fatal("failed rollback continued initial activation", string(b))
}
if err := m.locked(func() error { return nil }); err != nil {
t.Fatal("reconciliation leaked transaction lock", err)
}
if allowed {
for i := 0; i < 2; i++ {
if code, _, diagnostic := callManaged(t, d, "reconcile"); code != 0 {
t.Fatal("non-idempotent reconciliation/lock leak", code, diagnostic)
}
}
j, err := m.load()
if err != nil || j.State != "committed" || j.Hash != managedHash(known) {
t.Fatal(j, err)
}
}
})
}
}
func TestManagedReconcileDatabaseGuards(t *testing.T) {
for _, mode := range []string{"path", "symlink", "public", "flags"} {
t.Run(mode, func(t *testing.T) {
d, env, calls := managedCLIFixture(t)
initManagedCLI(t, d)
env["ZAPRET_AUTOTUNE_DB"] = managedTargetDirectory + "/candidates.db"
path := filepath.Join(d.directory, "candidates.db")
args := []string{"reconcile"}
switch mode {
case "path":
env["ZAPRET_AUTOTUNE_DB"] = "/other/db"
case "symlink":
if err := os.Symlink(filepath.Join(t.TempDir(), "missing"), path); err != nil {
t.Fatal(err)
}
case "public":
if err := os.WriteFile(path, []byte("unchanged"), 0644); err != nil {
t.Fatal(err)
}
case "flags":
args = append(args, "--db", path)
}
if code, _, _ := callManaged(t, d, args...); code == 0 || code == 75 || *calls != 0 {
t.Fatal("unsafe guard accepted", code)
}
if mode == "path" || mode == "flags" {
if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
t.Fatal("guard opened database", err)
}
}
})
}
}
func TestManagedMonitorArguments(t *testing.T) {
lookup := func(string) (string, bool) { return "", false }
cfg, err := managedMonitorConfig(nil, lookup)
@@ -261,7 +468,13 @@ func TestManagedMonitorBootstrapExistingFrontend(t *testing.T) {
// Execute only rewritten target-local contracts and fake programs; no network,
// Docker, real credentials, zapret or host firewall. Exercise the actual traps.
func TestManagedEntrypointSupervision(t *testing.T) {
for _, mode := range []string{"term", "fatal", "technical", "panic", "killed", "segv", "zero", "unknown", "ss", "ss-unsafe", "ss-unknown", "ss-technical", "term-unsafe", "term-unknown", "term-hang", "stop-failure", "stop-hang"} {
modes := []string{"term", "fatal", "technical", "panic", "killed", "segv", "zero", "unknown", "ss", "ss-unsafe", "ss-unknown", "ss-technical", "term-unsafe", "term-unknown", "term-hang", "stop-failure", "stop-hang"}
for _, stage := range []string{"init", "reconcile", "start"} {
for _, failure := range []string{"hang", "75", "124", "137", "7"} {
modes = append(modes, "startup-"+stage+"-"+failure)
}
}
for _, mode := range modes {
t.Run(mode, func(t *testing.T) {
root := t.TempDir()
config := filepath.Join(root, "ss")
@@ -276,12 +489,23 @@ func TestManagedEntrypointSupervision(t *testing.T) {
return path
}
log := filepath.Join(root, "log")
// Shorten only startup watchdogs in this fixture. Execute a real
// timeout, including its forced-kill path, without a 45-second test.
if strings.HasPrefix(mode, "startup-") {
timeout, err := exec.LookPath("timeout")
if err != nil {
t.Fatal(err)
}
write("timeout", "#!/bin/sh\nif [ \"$3\" = 45 ] || { [ \"$3\" = 30 ] && [ \"$5\" = start ]; }; then\nshift 3\nexec \"$REAL_TIMEOUT\" -k 1 1 \"$@\"\nfi\nexec \"$REAL_TIMEOUT\" \"$@\"\n")
t.Setenv("REAL_TIMEOUT", timeout)
}
startupFailure := "case \"$MODE\" in startup-$stage-hang) trap '' TERM INT; while :; do sleep 0.1; done;; startup-$stage-*) exit \"${MODE##*-}\";; esac\n"
write("awk", "#!/bin/sh\ncase \"$*\" in */proc/net/tcp*) until grep -q ready-ss-server \"$LOG\" && grep -q ready-ss-local \"$LOG\"; do sleep 0.02; done;; esac\nexit 0\n")
child := "#!/bin/sh\n[ -z \"${SS_PASSWORD+x}\" ] || exit 88\ntrap 'echo stopped-$0 >> \"$LOG\"; exit 0' TERM INT\necho ready-$(basename \"$0\") >> \"$LOG\"\nwhile :; do\ncase \"$MODE:$0\" in ss*:*/ss-local) if grep -q managed-monitor \"$LOG\"; then echo stopped-$0 >> \"$LOG\"; exit 7; fi;; esac\nsleep 0.1 & wait $!; done\n"
write("ss-server", child)
write("ss-local", child)
zapret := write("zapret", "#!/bin/sh\necho zapret-$1 >> \"$LOG\"\nif [ \"$MODE:$1\" = stop-failure:stop ]; then exit 1; fi\nif [ \"$MODE:$1\" = stop-hang:stop ]; then trap '' TERM INT; while :; do sleep 0.1; done; fi\n")
monitor := write("autotune", "#!/bin/sh\nif [ \"$2\" != monitor ]; then echo managed-$2 >> \"$LOG\"; exit 0; fi\ntrap 'echo monitor-stopped >> \"$LOG\"; case \"$MODE\" in *unsafe) exit 78;; *unknown) exit 7;; ss-technical) exit 75;; esac; exit 0' TERM INT\nif [ \"$MODE\" = term-hang ]; then trap '' TERM INT; fi\necho managed-monitor >> \"$LOG\"\ncase \"$MODE\" in fatal) exit 78;; technical|stop-failure|stop-hang) exit 75;; panic) exit 2;; killed) kill -KILL $$;; segv) exit 139;; zero) exit 0;; unknown) exit 7;; esac\nwhile :; do sleep 0.1 & wait $!; done\n")
zapret := write("zapret", "#!/bin/sh\necho zapret-$1 >> \"$LOG\"\nstage=$1\n"+startupFailure+"if [ \"$MODE:$1\" = stop-failure:stop ]; then exit 1; fi\nif [ \"$MODE:$1\" = stop-hang:stop ]; then trap '' TERM INT; while :; do sleep 0.1; done; fi\n")
monitor := write("autotune", "#!/bin/sh\nif [ \"$2\" != monitor ]; then\necho managed-$2 >> \"$LOG\"\nstage=$2\n"+startupFailure+"exit 0\nfi\ntrap 'echo monitor-stopped >> \"$LOG\"; case \"$MODE\" in *unsafe) exit 78;; *unknown) exit 7;; ss-technical) exit 75;; esac; exit 0' TERM INT\nif [ \"$MODE\" = term-hang ]; then trap '' TERM INT; fi\necho managed-monitor >> \"$LOG\"\ncase \"$MODE\" in fatal) exit 78;; technical|stop-failure|stop-hang) exit 75;; panic) exit 2;; killed) kill -KILL $$;; segv) exit 139;; zero) exit 0;; unknown) exit 7;; esac\nwhile :; do sleep 0.1 & wait $!; done\n")
data, err := os.ReadFile("../../managed-target-entrypoint.sh")
if err != nil {
t.Fatal(err)
@@ -332,6 +556,9 @@ func TestManagedEntrypointSupervision(t *testing.T) {
t.Fatal("entrypoint did not join children", ctx.Err())
}
want := 75
if strings.HasPrefix(mode, "startup-") {
want = 78
}
switch mode {
case "term":
want = 0
@@ -352,7 +579,20 @@ func TestManagedEntrypointSupervision(t *testing.T) {
t.Fatal(err)
}
text = string(b)
if !strings.Contains(text, "managed-init\nzapret-start\nmanaged-monitor\n") || !strings.Contains(text, "zapret-stop") || !strings.Contains(text, "stopped-"+filepath.Join(root, "ss-server")) || !strings.Contains(text, "stopped-"+filepath.Join(root, "ss-local")) {
if strings.HasPrefix(mode, "startup-") {
if strings.Contains(text, "managed-monitor") || !strings.Contains(text, "stopped-"+filepath.Join(root, "ss-server")) || !strings.Contains(text, "stopped-"+filepath.Join(root, "ss-local")) {
t.Fatal("startup failure escaped cleanup", text)
}
if strings.HasPrefix(mode, "startup-start-") {
if !strings.Contains(text, "managed-init\nmanaged-reconcile\nzapret-start\n") || !strings.Contains(text, "zapret-stop") {
t.Fatal("startup ordering/daemon cleanup", text)
}
} else if strings.Contains(text, "zapret-") {
t.Fatal("daemon invoked after failed prestart guard", text)
}
return
}
if !strings.Contains(text, "managed-init\nmanaged-reconcile\nzapret-start\nmanaged-monitor\n") || !strings.Contains(text, "zapret-stop") || !strings.Contains(text, "stopped-"+filepath.Join(root, "ss-server")) || !strings.Contains(text, "stopped-"+filepath.Join(root, "ss-local")) {
t.Fatal(text)
}
if mode == "term" && (!strings.Contains(text, "monitor-stopped") || strings.Index(text, "monitor-stopped") > strings.Index(text, "zapret-stop")) {