agent (unreleased): a cut write of agent.json cannot leave a blind box — a journal of the old bytes, a repair at start (R-1380)
gates / gates (push) Successful in 42s

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 08:20:49 +01:00
1 parent 54ac2c2ac9
commit db01ae0f2a
12 files changed
+582 -66

No files matched your search

+5 -15
View File
@@ -146,8 +146,8 @@ func (s *Server) validateBackupTargetMount(ctx context.Context, where string) er
// begin with. A typed round-trip would silently drop any key this build does not know about.
//
// Written IN PLACE (O_TRUNC), not tmp+rename: /etc/felhom-agent is root-owned while agent.json is
// agent-owned 0600, so the non-root agent cannot rename into that directory. A recovery copy is
// parked first, so a torn write is recoverable.
// agent-owned 0600, so the non-root agent cannot rename into that directory. config.ReplaceInPlace
// journals the old bytes first; the next start repairs a cut write (TestRecoverCutWrite_*).
func (s *Server) setConfiguredBackupTarget(id string) error {
config.FileMu.Lock() // one writer of agent.json at a time in this process (config.FileMu)
defer config.FileMu.Unlock()
@@ -182,24 +182,14 @@ func (s *Server) setConfiguredBackupTarget(id string) error {
if err != nil {
return err
}
st, err := os.Stat(path)
if err != nil {
return err
}
if s.stateDir != "" {
if err := os.MkdirAll(s.stateDir, 0o700); err == nil {
_ = os.WriteFile(filepath.Join(s.stateDir, "agent.json.pre-backup-target"), raw, 0o600)
}
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, st.Mode().Perm())
if err != nil {
return err
}
if _, err := f.Write(out); err != nil {
f.Close()
return err
}
return f.Close()
// config.ReplaceInPlace: the old bytes go into a journal first, so a power cut in the middle of the write is
// repaired at the next start (R-1380).
return config.ReplaceInPlace(path, raw, out)
}
// wrapperReason surfaces the wrapper's own REFUSED line when it produced one — it explains WHY in