{"record":{"id":"0ad58e8c7bc84049","repo":"gastownhall/beads","slug":"failed-to-read-s-w-0ad58e","errorCode":null,"errorMessage":"failed to read %s: %w","messagePattern":"failed to read (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/init_agent.go","lineNumber":73,"sourceCode":"\t\t// Replace the beads section with the requested profile.\n\t\t// EmbeddedDefault() ships with profile:full; swap to the requested profile\n\t\t// (which defaults to minimal). Also handles legacy markers without profile metadata.\n\t\tif strings.Contains(newContent, \"BEGIN BEADS INTEGRATION\") {\n\t\t\tif replaced, changed, err := agents.ReplaceSectionWithOpts(newContent, profile, opts); err == nil && changed {\n\t\t\t\tnewContent = replaced\n\t\t\t}\n\t\t}\n\n\t\t// #nosec G306 - markdown needs to be readable\n\t\tif err := os.WriteFile(filename, []byte(newContent), 0644); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create %s: %w\", filename, err)\n\t\t}\n\t\tif verbose {\n\t\t\tfmt.Printf(\"  %s Created %s with agent instructions\\n\", ui.RenderPass(\"✓\"), filename)\n\t\t}\n\t\treturn nil\n\t} else if err != nil {\n\t\treturn fmt.Errorf(\"failed to read %s: %w\", filename, err)\n\t}\n\n\t// File exists - check if it already has our sections\n\tcontentStr := string(content)\n\thasBeads := strings.Contains(contentStr, \"BEGIN BEADS INTEGRATION\")\n\n\tif hasBeads {\n\t\t// Preserve existing full profile when minimal is requested (avoid information loss)\n\t\teffectiveProfile := profile\n\t\texistingMeta := agents.ParseMarker(contentStr[strings.Index(contentStr, \"<!-- BEGIN BEADS INTEGRATION\"):])\n\t\tif existingMeta != nil && existingMeta.Profile == agents.ProfileFull && profile == agents.ProfileMinimal {\n\t\t\teffectiveProfile = agents.ProfileFull\n\t\t\tif verbose {\n\t\t\t\tfmt.Printf(\"  ℹ %s already has full profile; preserving (higher-information) content\\n\", filename)\n\t\t\t}\n\t\t}\n\n\t\t// Update existing section to latest versioned format (upgrades legacy markers)","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/init_agent.go#L55-L91","documentation":"When the target agent file exists, updateAgentFile reads it with os.ReadFile to inspect and update its beads section; a read error is wrapped here. This means an existing agent markdown file could not be read, so the update flow aborts before any modification.","triggerScenarios":"bd init/addAgentsInstructions finding an existing AGENTS.md (or CLAUDE.md etc.) that cannot be read: permission denied, path is actually a directory, or I/O error.","commonSituations":"File owned by another user with restrictive mode; the path is a broken symlink or directory; read-only bind mounts; stale container state after user changes.","solutions":["Check the file's read permissions (ls -l) and fix with chmod/chown","Verify the path is a regular file, not a directory or broken symlink","Restore file ownership consistency (e.g. after running as root in a container)","Remove or relocate the unreadable file so init recreates it"],"exampleFix":"// before: AGENTS.md owned by root, mode 600\nfailed to read AGENTS.md: permission denied\n// after\n$ sudo chown $(whoami) AGENTS.md && chmod u+rw AGENTS.md\n$ bd init","handlingStrategy":"validation","validationCode":"[ -f AGENTS.md ] && [ ! -r AGENTS.md ] && { echo \"AGENTS.md unreadable\" >&2; exit 1; }","typeGuard":null,"tryCatchPattern":"if err := addAgentsInstructions(...); err != nil && strings.Contains(err.Error(), \"failed to read\") { log.Fatalf(\"cannot read agent file: %v\", err) }","preventionTips":["Keep agent files owned and readable by the running user","Verify paths are regular files, not directories/symlinks","Normalize ownership after containerized runs as root","Git-tracked files help detect and restore broken state"],"tags":["filesystem","permissions","agents","init"],"backgroundTag":"file-read-permission-denied","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}