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

+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 {