carlos / platform Public

host: adopt's named-units step and its write in one helper

runNamedUnitsStep runs the step, logs its line and writes its record,
in that order; runAdoptUnderRootRelease calls it where the three lines
were, so agent.go is back under its code-health line count. The source
order tests pin the helper's order and the call's place: after the
-restart-only return, before release.ParsePublicKey and the
CARLOS_SECRETS_KEY read, before SyncUnits.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev a82febd5cca40124af6d616bbe02c6b26b4167dc parent 3880c5a
3 files changed, +75 −51
  • internal/host/agent.go +1 −5
  • internal/host/namedunitsfetch.go +12 −0
  • internal/host/namedunitsfetch_test.go +62 −46
diff --git a/internal/host/agent.go b/internal/host/agent.go
index a930c29be..be44cabf8 100644
--- a/internal/host/agent.go
+++ b/internal/host/agent.go
@@ -6166,11 +6166,7 @@ func runAdoptUnderRootRelease(args []string, buildClaims ClaimFactory, ensureUni
// the same file on a box without the key; and its record is written
// before the release key parse, which can end the pass, so an install
// is never left unreported. Never fatal.
- namedUnits := adoptNamedUnitsStep(ctx)
- log.Print(namedUnitsLogLine(namedUnits))
- if err := writeNamedUnitsStatus(namedUnits); err != nil {
- log.Printf("adopt: named-units status: %v", err)
- }
+ namedUnits := runNamedUnitsStep(ctx)
// Parsed here, before config sync, exactly as the full pass always has
// (a box with an unusable release key must die before it converges
// anything), but SKIPPED entirely for -config-only: that mode never
diff --git a/internal/host/namedunitsfetch.go b/internal/host/namedunitsfetch.go
index 1009500b0..b5b233e0b 100644
--- a/internal/host/namedunitsfetch.go
+++ b/internal/host/namedunitsfetch.go
@@ -290,6 +290,18 @@ var adoptNamedUnitsStep = func(ctx context.Context) status.NamedUnitsStatus {
return productionNamedUnitsFetch().run(ctx)
}
+// runNamedUnitsStep is the named-units step as runAdoptUnderRootRelease
+// runs it: the step, its journal line, then its record written, before the
+// pass goes on. It returns the record, which config sync completes.
+func runNamedUnitsStep(ctx context.Context) status.NamedUnitsStatus {
+ st := adoptNamedUnitsStep(ctx)
+ log.Print(namedUnitsLogLine(st))
+ if err := writeNamedUnitsStatus(st); err != nil {
+ log.Printf("adopt: named-units status: %v", err)
+ }
+ return st
+}
+
// writeNamedUnitsStatus replaces status.json in namedUnitsStatusDir.
func writeNamedUnitsStatus(st status.NamedUnitsStatus) error {
raw, err := json.Marshal(st)
diff --git a/internal/host/namedunitsfetch_test.go b/internal/host/namedunitsfetch_test.go
index e2128f9b3..4dbcac681 100644
--- a/internal/host/namedunitsfetch_test.go
+++ b/internal/host/namedunitsfetch_test.go
@@ -471,42 +471,74 @@ func p(a rollbackAttempt) *rollbackAttempt { return &a }
// the invariant. The step runs on full and config-only passes (after the
// -restart-only return), its record is written before the release key
// parse can kill the pass, and it sits outside the CARLOS_SECRETS_KEY
-// branch, so a box without the key still installs.
+// branch, so a box without the key still installs. runNamedUnitsStep
+// holds the step and the write; runAdoptUnderRootRelease calls it.
func TestAdoptRunsNamedUnitsStepBeforeConfigSync(t *testing.T) {
- raw, err := os.ReadFile("agent.go")
+ helper := funcSource(t, "namedunitsfetch.go", "runNamedUnitsStep")
+ hat := sourceAt(t, "runNamedUnitsStep", helper)
+ for _, s := range []string{"adoptNamedUnitsStep(", "writeNamedUnitsStatus(", "return "} {
+ if n := strings.Count(helper, s); n != 1 {
+ t.Fatalf("%s appears %d times in runNamedUnitsStep, want once", s, n)
+ }
+ }
+ if step, write, ret := hat("adoptNamedUnitsStep("), hat("writeNamedUnitsStatus("), hat("return "); !(step < write && write < ret) {
+ t.Fatalf("runNamedUnitsStep order: step %d, write %d, return %d", step, write, ret)
+ }
+
+ body := funcSource(t, "agent.go", "runAdoptUnderRootRelease")
+ at := sourceAt(t, "runAdoptUnderRootRelease", body)
+ restart := at("if *restartOnly {")
+ restartEnd := restart + strings.Index(body[restart:], "\n\t}\n")
+ step := at("runNamedUnitsStep(")
+ pub := at("release.ParsePublicKey(")
+ secrets := at(`os.Getenv("CARLOS_SECRETS_KEY")`)
+ sync := at("appconfig.SyncUnits(")
+ for _, s := range []string{"runNamedUnitsStep(", "adoptNamedUnitsStep(", "writeNamedUnitsStatus("} {
+ want := 0
+ if s == "runNamedUnitsStep(" {
+ want = 1
+ }
+ if n := strings.Count(body, s); n != want {
+ t.Fatalf("%s appears %d times in runAdoptUnderRootRelease, want %d", s, n, want)
+ }
+ }
+ // Before the key is read, so outside any branch on it.
+ if !(restartEnd < step && step < pub && pub < secrets && secrets < sync) {
+ t.Fatalf("order: restart-only end %d, step %d, ParsePublicKey %d, secrets %d, SyncUnits %d", restartEnd, step, pub, secrets, sync)
+ }
+}
+
+// funcSource is the source of func name in file, up to its closing brace
+// at column one.
+func funcSource(t *testing.T, file, name string) string {
+ t.Helper()
+ raw, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
src := string(raw)
- start := strings.Index(src, "\nfunc runAdoptUnderRootRelease(")
+ start := strings.Index(src, "\nfunc "+name+"(")
if start < 0 {
- t.Fatal("runAdoptUnderRootRelease is no longer in agent.go, move this test with it")
+ t.Fatalf("%s is no longer in %s, move this test with it", name, file)
}
body := src[start+1:]
if end := strings.Index(body, "\n}\n"); end >= 0 {
body = body[:end]
}
- at := func(s string) int {
+ return body
+}
+
+// sourceAt returns a lookup of s's offset in body, failing when s is
+// missing.
+func sourceAt(t *testing.T, name, body string) func(s string) int {
+ return func(s string) int {
t.Helper()
i := strings.Index(body, s)
if i < 0 {
- t.Fatalf("runAdoptUnderRootRelease no longer contains %q", s)
+ t.Fatalf("%s no longer contains %q", name, s)
}
return i
}
- restart := at("if *restartOnly {")
- restartEnd := restart + strings.Index(body[restart:], "\n\t}\n")
- step := at("adoptNamedUnitsStep(")
- write := at("writeNamedUnitsStatus(")
- pub := at("release.ParsePublicKey(")
- secrets := at(`os.Getenv("CARLOS_SECRETS_KEY")`)
- sync := at("appconfig.SyncUnits(")
- if n := strings.Count(body, "adoptNamedUnitsStep("); n != 1 {
- t.Fatalf("adoptNamedUnitsStep( appears %d times, want once", n)
- }
- if !(restartEnd < step && step < write && write < pub && pub < secrets && secrets < sync) {
- t.Fatalf("order: restart-only end %d, step %d, write %d, ParsePublicKey %d, secrets %d, SyncUnits %d", restartEnd, step, write, pub, secrets, sync)
- }
}
func TestProductionNamedUnitsFetchIsWired(t *testing.T) {
@@ -612,40 +644,24 @@ func TestAdoptWritesNamedUnitsStatusAfterSync(t *testing.T) {
}
// The real pass calls exactly that, in that order, after the
- // -restart-only return and before the -config-only one.
- raw, err := os.ReadFile("agent.go")
- if err != nil {
- t.Fatal(err)
- }
- src := string(raw)
- start := strings.Index(src, "\nfunc runAdoptUnderRootRelease(")
- if start < 0 {
- t.Fatal("runAdoptUnderRootRelease is no longer in agent.go, move this test with it")
- }
- body := src[start+1:]
- if end := strings.Index(body, "\n}\n"); end >= 0 {
- body = body[:end]
- }
- at := func(s string) int {
- t.Helper()
- i := strings.Index(body, s)
- if i < 0 {
- t.Fatalf("runAdoptUnderRootRelease no longer contains %q", s)
- }
- return i
- }
- for _, s := range []string{"adoptNamedUnitsStep(", "writeNamedUnitsStatus(", "adoptConfigSync(", "appconfig.SyncUnits("} {
+ // -restart-only return and before the -config-only one: the step and
+ // its write through runNamedUnitsStep (whose own order
+ // TestAdoptRunsNamedUnitsStepBeforeConfigSync pins), then
+ // adoptConfigSync with SyncUnits.
+ body := funcSource(t, "agent.go", "runAdoptUnderRootRelease")
+ at := sourceAt(t, "runAdoptUnderRootRelease", body)
+ for _, s := range []string{"runNamedUnitsStep(", "adoptConfigSync(", "appconfig.SyncUnits("} {
if n := strings.Count(body, s); n != 1 {
t.Fatalf("%s appears %d times in runAdoptUnderRootRelease, want once", s, n)
}
}
restart := at("if *restartOnly {")
restartEnd := restart + strings.Index(body[restart:], "\n\t}\n")
- step0, write0 := at("adoptNamedUnitsStep("), at("writeNamedUnitsStatus(")
+ step0 := at("runNamedUnitsStep(")
pub, cfg := at("release.ParsePublicKey("), at("adoptConfigSync(")
sync, configOnly := at("appconfig.SyncUnits("), at("if *configOnly {\n\t\t// Everything below")
- if !(restartEnd < step0 && step0 < write0 && write0 < pub && pub < cfg && cfg < sync && sync < configOnly) {
- t.Fatalf("order: restart-only end %d, step %d, write %d, ParsePublicKey %d, adoptConfigSync %d, SyncUnits %d, config-only return %d",
- restartEnd, step0, write0, pub, cfg, sync, configOnly)
+ if !(restartEnd < step0 && step0 < pub && pub < cfg && cfg < sync && sync < configOnly) {
+ t.Fatalf("order: restart-only end %d, step %d, ParsePublicKey %d, adoptConfigSync %d, SyncUnits %d, config-only return %d",
+ restartEnd, step0, pub, cfg, sync, configOnly)
}
}