diff --git a/cmd/zapret-autotune/managed_recovery.go b/cmd/zapret-autotune/managed_recovery.go index 3befc3d..00e688a 100644 --- a/cmd/zapret-autotune/managed_recovery.go +++ b/cmd/zapret-autotune/managed_recovery.go @@ -227,17 +227,6 @@ func recoverManagedTarget(ctx context.Context, d managedRecoveryDeps) (result ma if err := ctx.Err(); err != nil { return result, err } - if d.service { - _, healthErr := d.health(ctx) - var fatal *managedRecoveryFatal - if ctx.Err() != nil || errors.As(healthErr, &fatal) || errors.Is(healthErr, context.Canceled) || errors.Is(healthErr, context.DeadlineExceeded) { - return result, managedRecoveryContextError(ctx, healthErr) - } - var failure *managedResourceFailure - if errors.As(healthErr, &failure) && assetEndpoint(failure.Host) { - return result, repairManagedAssets(ctx, d, failure.Host) - } - } m := *d.transaction // Rollback must restart even after job cancellation; independently bounded. var cleanupCtx context.Context @@ -272,6 +261,20 @@ func recoverManagedTarget(ctx context.Context, d managedRecoveryDeps) (result ma } return status, err } + // Nested asset/GGC recovery reads the same transaction before binding its + // apply-specific hooks. Production adapters start with path-only transactions. + d.transaction = &m + if d.service { + _, healthErr := d.health(ctx) + var fatal *managedRecoveryFatal + if ctx.Err() != nil || errors.As(healthErr, &fatal) || errors.Is(healthErr, context.Canceled) || errors.Is(healthErr, context.DeadlineExceeded) { + return result, managedRecoveryContextError(ctx, healthErr) + } + var failure *managedResourceFailure + if errors.As(healthErr, &failure) && assetEndpoint(failure.Host) { + return result, repairManagedAssets(ctx, d, failure.Host) + } + } ids, err := m.ActiveCandidates(d.db) if err != nil { return result, &managedRecoveryFatal{Err: err} diff --git a/cmd/zapret-autotune/managed_recovery_hooks_test.go b/cmd/zapret-autotune/managed_recovery_hooks_test.go new file mode 100644 index 0000000..5253985 --- /dev/null +++ b/cmd/zapret-autotune/managed_recovery_hooks_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "context" + "database/sql" + "errors" + "os" + "testing" +) + +func TestManagedRecoveryProductionTransactionHooks(t *testing.T) { + d, front, _ := recoveryFixture(t, false) + if path := os.Getenv("RC_PRIVATE_DB"); path != "" { + db, err := sql.Open("sqlite", "file:"+path+"?mode=ro") + if err != nil { + t.Fatal("private snapshot open failed") + } + defer db.Close() + ids, err := qualifiedCandidateIDs(db, "frontend") + if err != nil || len(ids) != 1 { + t.Fatal("private qualification unavailable") + } + if err := d.transaction.Apply(db, ids); err != nil { + t.Fatal("private offline apply failed") + } + d.db, front = db, ids[0] + } else { + front = managedCandidateFixture(t, d.db, "frontend", "youtube.com", "--lua-desync=fake", true) + if err := d.transaction.Apply(d.db, []int64{front}); err != nil { + t.Fatal(err) + } + } + // Production adapters supply only the target path; recovery binds hooks. + d.transaction = &managedTransaction{Dir: d.transaction.Dir, Name: "config"} + d.service, d.adaptive = true, true + d.frontHealth = d.health + d.mediaSamples = func(context.Context) ([]mediaResource, error) { + t.Fatal("asset qualification must precede private media discovery") + return nil, nil + } + d.verifyGooglevideo = func(context.Context, storedCandidate, []mediaResource) (GooglevideoQualificationProof, error) { + t.Fatal("unexpected verification") + return GooglevideoQualificationProof{}, nil + } + result, err := recoverManagedTarget(context.Background(), d) + var failure *managedResourceFailure + if err != nil || result.FrontendID != front || !errors.As(result.GGCError, &failure) { + // Never expose private DB contents or error causes. + t.Fatalf("frontend_preserved=%t recovery_error_type=%T ggc_error_type=%T", result.FrontendID == front, err, result.GGCError) + } +}