From edb2e70aaf2b7adbc2c48092e53a6eaeff01a4db Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 15:19:33 -0500 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B=20agent(bob):=20preflight=20pr?= =?UTF-8?q?obes=20the=20per-agent=20~/.bob=20that=20bob=20actually=20write?= =?UTF-8?q?s=20to?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../fixed-bob-preflight-per-agent-home.md | 1 + src/crux/agent/bob_settings_test.go | 37 +++++++++++++++++++ src/crux/agent/manager.go | 9 ++++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 changelog.d/fixed-bob-preflight-per-agent-home.md diff --git a/changelog.d/fixed-bob-preflight-per-agent-home.md b/changelog.d/fixed-bob-preflight-per-agent-home.md new file mode 100644 index 0000000000..1bebba9724 --- /dev/null +++ b/changelog.d/fixed-bob-preflight-per-agent-home.md @@ -0,0 +1 @@ +- The bob startup preflight now checks that the per-agent `~/.bob` (where bob writes its installation ID and chat recordings since per-agent homes landed) is writable, instead of probing the shared `/data/home/.bob` that only holds the bridged settings, so an unwritable state directory is reported against the directory bob actually uses (#17062). diff --git a/src/crux/agent/bob_settings_test.go b/src/crux/agent/bob_settings_test.go index ae7ce0dac8..def8e13198 100644 --- a/src/crux/agent/bob_settings_test.go +++ b/src/crux/agent/bob_settings_test.go @@ -674,3 +674,40 @@ func TestVerifyBobStateDirsWritableIgnoresNonDirectoryStatePath(t *testing.T) { t.Errorf("verifyBobStateDirsWritable() = %v for non-directory state path, want advisory warning only", got) } } + +// TestBobStateHomeIsPerAgentHome pins that the preflight probes the per-agent +// ~/.bob bob actually writes to, not the shared /data/home/.bob. +func TestBobStateHomeIsPerAgentHome(t *testing.T) { + t.Setenv("HIVE_SHARED_AGENT_HOME", "") + sharedRoot := t.TempDir() + orig := sharedAgentHome + sharedAgentHome = sharedRoot + t.Cleanup(func() { sharedAgentHome = orig }) + + got := bobStateHome("tester", 2004, bobBackend) + if want := interactiveHomePath("tester"); got != want { + t.Fatalf("bobStateHome() = %q, want per-agent home %q", got, want) + } + if got == bobSharedHome || got == sharedRoot { + t.Errorf("bobStateHome() = %q, must not be the shared home", got) + } + + // An unwritable per-agent ~/.bob is reported against the per-agent dir. + agentBob := filepath.Dir(bobSettingsPath(got)) + if err := os.MkdirAll(agentBob, 0o770); err != nil { + t.Fatalf("mkdir: %v", err) + } + if err := os.Chmod(agentBob, 0o555); err != nil { + t.Fatalf("chmod: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(agentBob, 0o770) }) + workDir := t.TempDir() + if err := os.Chmod(workDir, 0o770); err != nil { + t.Fatalf("chmod workDir: %v", err) + } + m := &Manager{logger: discardLogger()} + unwritable := m.verifyBobStateDirsWritable("tester", got, workDir, 2004) + if len(unwritable) == 0 || unwritable[0] != agentBob { + t.Errorf("verifyBobStateDirsWritable() = %v, want first entry %s", unwritable, agentBob) + } +} diff --git a/src/crux/agent/manager.go b/src/crux/agent/manager.go index f5d5043405..c18c86bf64 100644 --- a/src/crux/agent/manager.go +++ b/src/crux/agent/manager.go @@ -1654,5 +1654,12 @@ func (m *Manager) bobPreflightUnlocked(agent *AgentProcess) { // across the agent fleet. m.ensureBobAuthSettings(agent.Name, bobSharedHome) m.verifyBobKeyReadable(agent.Name, m.bobKeyFilePath(), agent.UID) - _ = m.verifyBobStateDirsWritable(agent.Name, bobSharedHome, m.workDir+"/"+agent.Name, agent.UID) + _ = m.verifyBobStateDirsWritable(agent.Name, bobStateHome(agent.Name, agent.UID, backend), m.workDir+"/"+agent.Name, agent.UID) +} + +// bobStateHome is the HOME bob writes its installation_id and tmp/ chat +// recordings under. Since per-agent homes it is not the shared bobSharedHome, +// which holds only the bridged settings. +func bobStateHome(agentName string, uid int, backend string) string { + return AgentHome(agentName, uid, backend) } From f281a2acf96b98610493564fd8b07ed298b44478 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 15:37:17 -0500 Subject: [PATCH 2/4] agent(bob): keep manager.go at its file-size ratchet baseline Move bobStateHome into bob_settings.go beside the other bob helpers so crux-file-size-ratchet stays green. Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/crux/agent/bob_settings.go | 7 +++++++ src/crux/agent/manager.go | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/crux/agent/bob_settings.go b/src/crux/agent/bob_settings.go index ed8d29f380..6281bdd498 100644 --- a/src/crux/agent/bob_settings.go +++ b/src/crux/agent/bob_settings.go @@ -439,3 +439,10 @@ func setBobV2AutoUpdateDisabled(settings map[string]any) bool { shell[config.BobV2AutoUpdateKey] = false return true } + +// bobStateHome is the HOME bob writes its installation_id and tmp/ chat +// recordings under. Since per-agent homes it is not the shared bobSharedHome, +// which holds only the bridged settings. +func bobStateHome(agentName string, uid int, backend string) string { + return AgentHome(agentName, uid, backend) +} diff --git a/src/crux/agent/manager.go b/src/crux/agent/manager.go index c18c86bf64..15ffff292e 100644 --- a/src/crux/agent/manager.go +++ b/src/crux/agent/manager.go @@ -1656,10 +1656,3 @@ func (m *Manager) bobPreflightUnlocked(agent *AgentProcess) { m.verifyBobKeyReadable(agent.Name, m.bobKeyFilePath(), agent.UID) _ = m.verifyBobStateDirsWritable(agent.Name, bobStateHome(agent.Name, agent.UID, backend), m.workDir+"/"+agent.Name, agent.UID) } - -// bobStateHome is the HOME bob writes its installation_id and tmp/ chat -// recordings under. Since per-agent homes it is not the shared bobSharedHome, -// which holds only the bridged settings. -func bobStateHome(agentName string, uid int, backend string) string { - return AgentHome(agentName, uid, backend) -} From 18b3d87a23670e06efdf55f932c5c5a39fab89c0 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 16:27:44 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9B=20agent(bob):=20judge=20agent-?= =?UTF-8?q?owned=20~/.bob=20by=20owner=20bits=20in=20the=20state-dir=20pro?= =?UTF-8?q?be?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The per-agent ~/.bob is created by bob itself as the agent UID (mkdirSync 0755/0700) and no permissions-watcher root widens it, so testing it for group w+x flagged it as unwritable on every launch after first run. When the stat Uid matches the agent uid, consult the owner bits instead; otherwise keep the group test for dev-owned dirs. Applies to both the existing-dir and nearest-existing-parent branches. Hive-Fix-Attempt: 1/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/crux/agent/bob_settings.go | 30 +++++++++++++++-- src/crux/agent/bob_settings_test.go | 52 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/crux/agent/bob_settings.go b/src/crux/agent/bob_settings.go index 6281bdd498..286fb86189 100644 --- a/src/crux/agent/bob_settings.go +++ b/src/crux/agent/bob_settings.go @@ -5,6 +5,7 @@ import ( "io/fs" "os" "path/filepath" + "syscall" "github.com/hivecommons/hive/crux/config" ) @@ -21,6 +22,11 @@ const ( // bobKeyOtherReadBit is `o+r`. Also sufficient for the agent to read, but // it means the secret is world-readable, which is worth flagging. bobKeyOtherReadBit fs.FileMode = 0o004 + // bobStateOwnerWriteBit / bobStateOwnerExecBit are `u+w` / `u+x` — what + // decides access when the agent UID itself OWNS the directory, as it does + // for the per-agent ~/.bob that bob's own mkdirSync creates (0755/0700). + bobStateOwnerWriteBit fs.FileMode = 0o200 + bobStateOwnerExecBit fs.FileMode = 0o100 // bobStateGroupWriteBit is `g+w` — the bit that lets an agent UID create // or replace files inside a dev:node-owned bob state directory. bobStateGroupWriteBit fs.FileMode = 0o020 @@ -171,7 +177,7 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui if parent := nearestExistingDir(filepath.Dir(dir)); parent != "" { if pInfo, pErr := os.Stat(parent); pErr == nil { pMode := pInfo.Mode().Perm() - if pMode&bobStateGroupWriteBit == 0 || pMode&bobKeyGroupExecBit == 0 { + if !bobStateDirWritableBy(pInfo, uid) { m.logger.Error("bob state dir does not exist and its parent is not writable by the agent UID; bob's first-run mkdir will fail with EACCES", "agent", agentName, "dir", dir, "parent", parent, "parent_mode", fs.FileMode(pMode).String(), "uid", uid, @@ -197,7 +203,7 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui // or replace a file inside. Write alone is not enough — without // execute the path cannot be traversed to open the file by name. mode := info.Mode().Perm() - if mode&bobStateGroupWriteBit == 0 || mode&bobKeyGroupExecBit == 0 { + if !bobStateDirWritableBy(info, uid) { m.logger.Error("bob state dir is not writable by the agent UID; bob will report \"error saving your latest settings changes\" and \"Failed to initialize logger\"", "agent", agentName, "dir", dir, "mode", fs.FileMode(mode).String(), "uid", uid, @@ -208,6 +214,26 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui return unwritable } +// bobStateDirWritableBy reports whether the agent UID can create or replace +// files inside the directory described by info. +// +// Two ownership shapes reach this probe. Dev-owned dirs (the shared home, the +// per-agent workspace) are reached through group `node`, so group write+execute +// decides. The per-agent ~/.bob is different: bob creates it itself, AS THE +// AGENT UID, with mkdirSync (0755 or 0700), and no permissions-watcher root +// widens it. Testing that dir for group bits flags it as unwritable on every +// launch after first run even though its owner can write to it just fine, so +// when the directory is owned by the agent UID the owner bits decide instead. +// Write alone is never enough — without execute the path cannot be traversed +// to open the file by name. +func bobStateDirWritableBy(info fs.FileInfo, uid int) bool { + mode := info.Mode().Perm() + if st, ok := info.Sys().(*syscall.Stat_t); ok && int(st.Uid) == uid { + return mode&bobStateOwnerWriteBit != 0 && mode&bobStateOwnerExecBit != 0 + } + return mode&bobStateGroupWriteBit != 0 && mode&bobKeyGroupExecBit != 0 +} + // ensureBobAuthSettings makes hive the owner of bob's auth-selection settings. // // Why this is needed at all: BOBSHELL_DEFAULT_AUTH_TYPE is only a fallback diff --git a/src/crux/agent/bob_settings_test.go b/src/crux/agent/bob_settings_test.go index def8e13198..5e115a164d 100644 --- a/src/crux/agent/bob_settings_test.go +++ b/src/crux/agent/bob_settings_test.go @@ -711,3 +711,55 @@ func TestBobStateHomeIsPerAgentHome(t *testing.T) { t.Errorf("verifyBobStateDirsWritable() = %v, want first entry %s", unwritable, agentBob) } } + +// TestVerifyBobStateDirsWritableAcceptsAgentOwnedDir pins that a per-agent +// ~/.bob created by bob itself — owned by the agent UID with mkdirSync's +// 0755/0700 and no group write bit — is NOT reported as unwritable. The probe +// must judge an agent-owned dir by its owner bits, otherwise every launch after +// first run logs a false "bob state dir is not writable" error. The same holds +// for a missing dir whose nearest existing parent is agent-owned 0700. +func TestVerifyBobStateDirsWritableAcceptsAgentOwnedDir(t *testing.T) { + uid := os.Getuid() + if uid <= 0 { + t.Skip("probe is a no-op for uid 0; cannot model an agent-owned dir as root") + } + home := t.TempDir() + workDir := t.TempDir() + // t.TempDir() is 0700 and owned by this process, which is exactly the + // shape bob's own mkdirSync leaves behind for the agent UID. + agentBob := filepath.Dir(bobSettingsPath(home)) + if err := os.Mkdir(agentBob, 0o700); err != nil { + t.Fatalf("mkdir: %v", err) + } + if err := os.Chmod(agentBob, 0o700); err != nil { + t.Fatalf("chmod: %v", err) + } + m := &Manager{logger: discardLogger()} + if got := m.verifyBobStateDirsWritable("tester", home, workDir, uid); len(got) != 0 { + t.Errorf("verifyBobStateDirsWritable() = %v for agent-owned 0700 dirs, want none", got) + } + + // An agent-owned dir with the owner write bit stripped IS still flagged: + // ownership does not short-circuit the check, it only selects which bits + // are consulted. + if err := os.Chmod(agentBob, 0o500); err != nil { + t.Fatalf("chmod: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(agentBob, 0o700) }) + // Both ~/.bob itself and the missing ~/.bob/settings (whose nearest + // existing parent is that same 0500 dir) are reported; nothing else is. + got := m.verifyBobStateDirsWritable("tester", home, workDir, uid) + wantV2 := filepath.Dir(bobV2SettingsPath(home)) + if len(got) != 2 || got[0] != agentBob || got[1] != wantV2 { + t.Errorf("verifyBobStateDirsWritable() = %v for agent-owned 0500 dir, want [%s %s]", got, agentBob, wantV2) + } + + // A dir owned by someone OTHER than the agent UID still falls back to the + // group test, so the dev-owned shared-home behaviour is unchanged. + if err := os.Chmod(agentBob, 0o700); err != nil { + t.Fatalf("chmod: %v", err) + } + if got := m.verifyBobStateDirsWritable("tester", home, workDir, uid+1); len(got) == 0 || got[0] != agentBob { + t.Errorf("verifyBobStateDirsWritable() = %v for foreign-owned 0700 dir, want [%s ...]", got, agentBob) + } +} From d1d57823e834f3992396f4ed838cc630f312e37e Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 17:17:16 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20agent(bob):=20skip=20$HOME/.?= =?UTF-8?q?bob=20probes=20until=20the=20per-agent=20home=20exists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bobPreflightUnlocked runs before launchInTmux, and the per-agent home is only created by setupInteractiveHome inside the launch, so on a new per-UID bob agent's first launch the home does not exist. The ancestor walk then landed on /data/home/agents (deliberately 0755 dev-owned) and logged two bogus 'chmod 2775 /data/home/agents' errors. Skip the two $HOME/.bob probes when the home is absent; the workspace dir is still probed. Also make the logged remedy ownership-aware: an agent-owned ~/.bob that fails the owner-bit check now gets 'chmod u+rwx ' instead of advice to widen group access, which would undo per-agent isolation. Hive-Fix-Attempt: 2/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/crux/agent/bob_settings.go | 33 +++++++++++++- src/crux/agent/bob_settings_test.go | 68 +++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/crux/agent/bob_settings.go b/src/crux/agent/bob_settings.go index 286fb86189..4c30e41ba1 100644 --- a/src/crux/agent/bob_settings.go +++ b/src/crux/agent/bob_settings.go @@ -162,6 +162,20 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui filepath.Join(workDir, config.BobStateDirName), } + // The per-agent home is provisioned by setupInteractiveHome INSIDE the + // launch, which runs after this preflight, so on a per-UID bob agent's + // very first launch $HOME does not exist yet. Walking up from a missing + // home lands on /data/home/agents, which is deliberately 0755 dev-owned + // (no agent writes there), and the group test then reports a bogus + // "chmod 2775 /data/home/agents" on every new agent. The home that is + // about to be created is agent-owned 0700, so its ~/.bob is writable by + // construction; only the workspace dir is worth probing in that shape. + if _, err := os.Stat(home); err != nil && os.IsNotExist(err) { + m.logger.Debug("bob home does not exist yet; skipping $HOME/.bob probes until the launch provisions it", + "agent", agentName, "home", home, "uid", uid) + probeDirs = probeDirs[2:] + } + for _, dir := range probeDirs { info, err := os.Stat(dir) if err != nil { @@ -181,7 +195,7 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui m.logger.Error("bob state dir does not exist and its parent is not writable by the agent UID; bob's first-run mkdir will fail with EACCES", "agent", agentName, "dir", dir, "parent", parent, "parent_mode", fs.FileMode(pMode).String(), "uid", uid, - "remedy", "chmod 2775 "+parent+" and ensure it is group-owned by node") + "remedy", bobStateDirRemedy(pInfo, parent, uid, "2775")) unwritable = append(unwritable, dir) } } @@ -207,13 +221,28 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui m.logger.Error("bob state dir is not writable by the agent UID; bob will report \"error saving your latest settings changes\" and \"Failed to initialize logger\"", "agent", agentName, "dir", dir, "mode", fs.FileMode(mode).String(), "uid", uid, - "remedy", "chmod 770 "+dir+" and ensure it is group-owned by node") + "remedy", bobStateDirRemedy(info, dir, uid, "770")) unwritable = append(unwritable, dir) } } return unwritable } +// bobStateDirRemedy returns the chmod an operator should run for a state dir +// that bobStateDirWritableBy rejected. The advice must match the branch that +// failed: a dir OWNED by the agent UID (bob's own ~/.bob) was judged by its +// owner bits, so widening the group — the remedy for dev-owned dirs reached +// through group `node` — would not fix it and would hand every agent UID on +// the node write access to one agent's private state. groupMode is the mode +// to prescribe for the dev-owned shape ("2775" for a parent, "770" for the +// dir itself). +func bobStateDirRemedy(info fs.FileInfo, dir string, uid int, groupMode string) string { + if st, ok := info.Sys().(*syscall.Stat_t); ok && int(st.Uid) == uid { + return "chmod u+rwx " + dir + " (agent-owned; do not widen group access)" + } + return "chmod " + groupMode + " " + dir + " and ensure it is group-owned by node" +} + // bobStateDirWritableBy reports whether the agent UID can create or replace // files inside the directory described by info. // diff --git a/src/crux/agent/bob_settings_test.go b/src/crux/agent/bob_settings_test.go index 5e115a164d..72a1c72dac 100644 --- a/src/crux/agent/bob_settings_test.go +++ b/src/crux/agent/bob_settings_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path/filepath" + "strings" "testing" "github.com/hivecommons/hive/crux/config" @@ -763,3 +764,70 @@ func TestVerifyBobStateDirsWritableAcceptsAgentOwnedDir(t *testing.T) { t.Errorf("verifyBobStateDirsWritable() = %v for foreign-owned 0700 dir, want [%s ...]", got, agentBob) } } + +// TestVerifyBobStateDirsWritableSkipsHomeProbesWhenHomeMissing pins the +// first-launch shape of a per-UID bob agent: bobPreflightUnlocked runs BEFORE +// launchInTmux, and the per-agent home is only created by setupInteractiveHome +// inside the launch. With the home absent, the ancestor walk would land on the +// home root — deliberately 0755 dev-owned, nothing is written there — and emit +// two bogus "chmod 2775 " errors on every new agent. The $HOME/.bob +// probes must be skipped in that shape; the workspace dir is still probed. +func TestVerifyBobStateDirsWritableSkipsHomeProbesWhenHomeMissing(t *testing.T) { + root := t.TempDir() + // The production /data/home/agents mode: traversable, not group-writable. + if err := os.Chmod(root, 0o755); err != nil { + t.Fatalf("chmod root: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(root, 0o770) }) + home := filepath.Join(root, "tester") + + workDir := t.TempDir() + if err := os.Chmod(workDir, 0o770); err != nil { + t.Fatalf("chmod workDir: %v", err) + } + + m := &Manager{logger: discardLogger()} + if got := m.verifyBobStateDirsWritable("tester", home, workDir, 2004); len(got) != 0 { + t.Errorf("verifyBobStateDirsWritable() = %v for not-yet-created home, want none", got) + } + + // The workspace dir is NOT exempt: a missing home only silences the two + // $HOME/.bob probes, never the /.bob one. + if err := os.Chmod(workDir, 0o750); err != nil { + t.Fatalf("chmod workDir: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(workDir, 0o770) }) + got := m.verifyBobStateDirsWritable("tester", home, workDir, 2004) + wantDir := filepath.Join(workDir, config.BobStateDirName) + if len(got) != 1 || got[0] != wantDir { + t.Errorf("verifyBobStateDirsWritable() = %v for missing home with unwritable workDir, want [%s]", got, wantDir) + } +} + +// TestBobStateDirRemedy pins that the logged remedy matches the branch that +// failed: an agent-owned dir needs its OWNER bits restored, and must never be +// told to widen group access (which would undo per-agent isolation), while a +// dev-owned dir reached through group `node` keeps the group-mode advice. +func TestBobStateDirRemedy(t *testing.T) { + uid := os.Getuid() + if uid <= 0 { + t.Skip("cannot model an agent-owned dir as root") + } + dir := t.TempDir() + info, err := os.Stat(dir) + if err != nil { + t.Fatalf("stat: %v", err) + } + + got := bobStateDirRemedy(info, dir, uid, "770") + if !strings.HasPrefix(got, "chmod u+rwx "+dir) || strings.Contains(got, "node") { + t.Errorf("agent-owned remedy = %q, want owner-bit chmod with no group widening", got) + } + + for _, mode := range []string{"770", "2775"} { + got := bobStateDirRemedy(info, dir, uid+1, mode) + if want := "chmod " + mode + " " + dir + " and ensure it is group-owned by node"; got != want { + t.Errorf("foreign-owned remedy(%s) = %q, want %q", mode, got, want) + } + } +}