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

+21
View File
@@ -0,0 +1,21 @@
package pbsdr
import (
"os"
"testing"
"gitea.dooplex.hu/admin/felhom-agent/internal/config"
)
// TestMain points the config write journal (config.CutWriteDir, R-1380) at a temp folder: the real one is the
// agent's state folder on a box, and a test must not need it.
func TestMain(m *testing.M) {
dir, err := os.MkdirTemp("", "felhom-cutwrite-")
if err != nil {
panic(err)
}
config.CutWriteDir = dir
code := m.Run()
_ = os.RemoveAll(dir)
os.Exit(code)
}
+5 -17
View File
@@ -484,7 +484,7 @@ func (m *Manager) consumedFail(h, msg string, block *hub.WirePBSDR) {
// seedEscrowStorageID sets escrow.pbs_storage_id in agent.json when empty/absent — the bare
// `--selftest=escrow-create` one-liner must find its storage with no flags. A different existing
// value is NEVER clobbered (warn-and-keep). Unknown config keys are preserved verbatim
// (map[string]RawMessage read-modify-write, atomic rename).
// (map[string]RawMessage read-modify-write, in place behind a journal — config.ReplaceInPlace).
func (m *Manager) seedEscrowStorageID(storageID string) error {
config.FileMu.Lock() // one writer of agent.json at a time in this process (config.FileMu)
defer config.FileMu.Unlock()
@@ -530,30 +530,18 @@ func (m *Manager) seedEscrowStorageID(storageID string) error {
if err != nil {
return err
}
st, err := os.Stat(m.configPath)
if err != nil {
return err
}
// The config DIRECTORY (/etc/felhom-agent) is root-owned while the FILE is agent-owned —
// tmp+rename is impossible for the non-root agent (rename mutates the directory; live
// finding on the demo). So: park a recovery copy in the agent state dir, then rewrite the
// file IN PLACE (O_TRUNC). The agent is the file's only writer and the content is small;
// a torn write is recoverable from the parked copy.
// finding on the demo). So: park a copy for a person in the agent state dir, then rewrite the
// file IN PLACE through config.ReplaceInPlace, which journals the old bytes first — the next
// start repairs a cut write (R-1380, TestRecoverCutWrite_*).
if err := os.MkdirAll(m.stateDir, 0o700); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(m.stateDir, "agent.json.pre-seed"), raw, 0o600); err != nil {
return fmt.Errorf("parking the pre-seed copy: %w", err)
}
f, err := os.OpenFile(m.configPath, 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
}
if err := f.Close(); err != nil {
if err := config.ReplaceInPlace(m.configPath, raw, out); err != nil {
return err
}
m.logger.Info("pbsdr: seeded escrow.pbs_storage_id (the ceremony one-liner needs no flags)", "storage_id", storageID)