carlos / platform Public

boxself: a failed self write withdraws the earlier record (#898)

Fix round 1 (Astra, Important): a refused or failed boxself.Write left
an older self.json readable, so the drill could publish under a
reference or group mode the box no longer has. Write now removes the
earlier record on any failure, so Read returns os.ErrNotExist and the
drill refuses; a failed removal is joined to the error. Regression
tests in boxself and through agentEntry.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 038807ead5798861f57331934e5a651ed2909409 parent 3e962a3
4 files changed, +70 −3
  • internal/boxself/boxself.go +15 −1
  • internal/boxself/boxself_test.go +31 −0
  • internal/host/agententry.go +3 −2
  • internal/host/verifyassign_test.go +21 −0

Intent

Human

<task-notification> <task-id>ae0b97e2593adfd1f</task-id> <tool-use-id>toolu_01QaXyRuajNkQTqJCajwT4Mo</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ab362fc0-f177-4a7f-a92e-4a29ea210aad/tasks/ae0b97e2593adfd1f.output</output-file> <status>completed</status> <summary>Agent "Implement slice 2a Task 2" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE. Not pushed. Commit: 3e962a32 "member backups 2a: box self ref and group drill assignment (#898)" Tests: Step 4's command passes in boxself, boxchoice, edge, host and activator. The existing agentEntry, haself, duty and census tests still pass. gofmt and vet are clean, and the build passes for linux, darwin and windows. Concerns: - **Census change outside the brief.** The host claim census refuses any unclassified file under /data, so I listed `monitor/self.json`, `monitor/verify-assign.json` and their `.tmp` files beside `monitor/alerts.json`. A linux test covers it. Please confirm you want this. - **Unit permissions need no change.** The agent and the drill both run as `carlos`, and both units have /data in ReadWritePaths. The files are 0644 in a 0755 directory. - **Root-binary census.** `internal/boxself` is a new root-binary package, and host, activator and boxchoice changed. I left the census overlay to Task 9. - **First assignment after a restart.** It is written straight away, so if the edge hasn't loaded its view yet every HA host is listed Unknown until the next 5-minute tick. Task 5 may want to check the assignment's time. - **Tests no longer touch /data.** The two existing agentEntry tests now set `CARLOS_DATA_DIR` to a temp dir; otherwise they would try to write the real `/data/monitor/self.json`. - **Sandbox workarounds.** Go commands ran with the sandbox off because the Go cache is read-only inside it. Socket tests used `/tmp/claude/b2t` because `/tmp/g` is blocked; I removed that directory afterwards. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/member-backups-2a/.superpowers/sdd/2026-11-11-member-backups-slice2a/task-2-report.md</result> <usage><subagent_tokens>161214</subagent_tokens><tool_uses>75</tool_uses><duration_ms>939518</duration_ms></usage> </task-notification>

Attributed to this commit: 9.71 g CO₂e (1,223,037 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on member-backups-2a, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/boxself/boxself.go b/internal/boxself/boxself.go
index c02d88c6a..ba700965b 100644
--- a/internal/boxself/boxself.go
+++ b/internal/boxself/boxself.go
@@ -88,8 +88,22 @@ func ValidRef(ref string) bool {
}
// Write records s as this box's self, replacing any earlier record. It
-// refuses a reference ValidRef refuses, writing nothing.
+// refuses a reference ValidRef refuses. A write that fails, refused or
+// not, also removes any earlier record, so Read finds none rather than a
+// reference or group mode this box no longer has; the error then says
+// if that removal failed too.
func Write(dataDir string, s Self) error {
+ err := writeSelf(dataDir, s)
+ if err == nil {
+ return nil
+ }
+ if rerr := os.Remove(SelfPath(dataDir)); rerr != nil && !errors.Is(rerr, os.ErrNotExist) {
+ return errors.Join(err, fmt.Errorf("boxself: the earlier record could not be removed either: %w", rerr))
+ }
+ return err
+}
+
+func writeSelf(dataDir string, s Self) error {
if !ValidRef(s.Ref) {
return fmt.Errorf("boxself: %q is not a box reference", s.Ref)
}
diff --git a/internal/boxself/boxself_test.go b/internal/boxself/boxself_test.go
index abf161d0f..e4d5fe34a 100644
--- a/internal/boxself/boxself_test.go
+++ b/internal/boxself/boxself_test.go
@@ -143,3 +143,34 @@ func TestAssignmentRoundTrip(t *testing.T) {
})
}
}
+
+// TestBoxSelfFailedWriteWithdrawsTheOldSelf: a write that fails leaves no
+// self to read, never the previous one, so the drill refuses rather than
+// publish under an old reference or the old group mode.
+func TestBoxSelfFailedWriteWithdrawsTheOldSelf(t *testing.T) {
+ old := Self{Ref: "g-0123456789abcdef0123", Group: true}
+ for name, fail := range map[string]func(dir string) Self{
+ // The new reference is refused before anything is written.
+ "refused ref": func(string) Self { return Self{Ref: ""} },
+ // The scratch file cannot be created: a directory holds its name.
+ "scratch blocked": func(dir string) Self {
+ if err := os.MkdirAll(filepath.Join(SelfPath(dir)+".tmp", "x"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ return Self{Ref: "jolly-otter"}
+ },
+ } {
+ t.Run(name, func(t *testing.T) {
+ dir := t.TempDir()
+ if err := Write(dir, old); err != nil {
+ t.Fatal(err)
+ }
+ if err := Write(dir, fail(dir)); err == nil {
+ t.Fatal("the replacement write succeeded")
+ }
+ if s, err := Read(dir); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("after a failed write Read = %+v, %v; want os.ErrNotExist", s, err)
+ }
+ })
+ }
+}
diff --git a/internal/host/agententry.go b/internal/host/agententry.go
index 1ef84be14..3ae2123c5 100644
--- a/internal/host/agententry.go
+++ b/internal/host/agententry.go
@@ -50,8 +50,9 @@ func agentEntry(getenv func(string) string, resolve func() (string, error), back
lease.SetSelf(self)
logf("agent: lease locks name this box %s", hafresh.OwnerFor(self, os.Getpid(), false))
// The nightly restore drill publishes under this same reference and
- // cannot work it out itself (boxself). A failed write is logged; the
- // drill then refuses to run rather than guess.
+ // cannot work it out itself (boxself). A failed write is logged and
+ // removes any earlier record, so the drill finds none and refuses to
+ // run rather than publish under a reference this box no longer has.
if err := boxself.Write(dataDirFrom(getenv), boxself.Self{Ref: self.Ref, Group: groupReferenced(getenv)}); err != nil {
logf("agent: the restore drill's record of this box could not be written: %v", err)
}
diff --git a/internal/host/verifyassign_test.go b/internal/host/verifyassign_test.go
index 428024c1a..99693dfe0 100644
--- a/internal/host/verifyassign_test.go
+++ b/internal/host/verifyassign_test.go
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
+ "os"
"reflect"
"slices"
"strings"
@@ -65,6 +66,26 @@ func TestAgentWritesSelfRefEqualToLeaseRef(t *testing.T) {
}
}
+// TestAgentFailedSelfRefLeavesNoIdentity: when the agent cannot record
+// its reference, an earlier box's record does not survive for the drill
+// to publish under.
+func TestAgentFailedSelfRefLeavesNoIdentity(t *testing.T) {
+ dir := t.TempDir()
+ if err := boxself.Write(dir, boxself.Self{Ref: "g-0123456789abcdef0123", Group: true}); err != nil {
+ t.Fatal(err)
+ }
+ var lines []string
+ logf := func(f string, args ...any) { lines = append(lines, fmt.Sprintf(f, args...)) }
+ // An identity with no reference: the write is refused.
+ agentEntry(groupEnv(map[string]string{"CARLOS_DATA_DIR": dir}), resolveGroupBoxReference, nil, logf, func() hafresh.Self { return hafresh.Self{} }, func() {})
+ if s, err := boxself.Read(dir); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("after a failed write Read = %+v, %v; want os.ErrNotExist", s, err)
+ }
+ if !slices.ContainsFunc(lines, func(l string) bool { return strings.Contains(l, "restore drill") }) {
+ t.Fatalf("the failure was not logged: %q", lines)
+ }
+}
+
func TestComputeAssignment(t *testing.T) {
now := time.Date(2026, 11, 11, 3, 0, 0, 0, time.UTC)
serving := func(refs ...string) []boxchoice.Candidate {