{"record":{"id":"ed6cec72d916bd92","repo":"gastownhall/beads","slug":"bd-s-w-s","errorCode":null,"errorMessage":"bd %s: %w: %s","messagePattern":"bd (.+?): %w: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/agent_hook.go","lineNumber":29,"sourceCode":"\t\"strings\"\n)\n\n// Shared helpers for the agent lifecycle hook commands (codex-hook, cursor-hook).\n// Each agent keeps its own event names, input/output schemas, and test stubs, but\n// the prime-runner and one-shot refresh-marker mechanics are identical and live\n// here to avoid divergence.\n\n// runBdPrime shells out to `bd prime [args...]` and returns its combined output.\n// The hooks exec a subprocess (rather than calling prime in process) to avoid\n// re-entrant store initialization.\nfunc runBdPrime(ctx context.Context, args ...string) (string, error) {\n\tcmdArgs := append([]string{\"prime\"}, args...)\n\t// #nosec G702 - os.Args[0] is this bd binary re-invoking itself; cmdArgs is the\n\t// fixed \"prime\" subcommand plus internal flags, never attacker-controlled input.\n\tcmd := exec.CommandContext(ctx, os.Args[0], cmdArgs...)\n\tout, err := cmd.CombinedOutput()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"bd %s: %w: %s\", strings.Join(cmdArgs, \" \"), err, strings.TrimSpace(string(out)))\n\t}\n\treturn string(out), nil\n}\n\n// agentHookMarkerBaseDir returns the cache directory for one-shot\n// post-compaction refresh markers for a given agent (subdir e.g. \"codex-hooks\").\n// override redirects the location for tests.\nfunc agentHookMarkerBaseDir(subdir, override string) string {\n\tif override != \"\" {\n\t\treturn override\n\t}\n\tif dir, err := os.UserCacheDir(); err == nil && dir != \"\" {\n\t\treturn filepath.Join(dir, \"beads\", subdir)\n\t}\n\treturn filepath.Join(os.TempDir(), \"beads-\"+subdir)\n}\n\n// agentHookMarkerPath derives a per-session, per-workspace marker file under","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/agent_hook.go#L11-L47","documentation":"Produced by runBdPrime's helper when re-invoking the bd binary (`os.Args[0] bd prime ...`) via exec.CommandContext and the subprocess fails. The error bundles the full command line, the exec/exit error, and the trimmed combined stdout+stderr of the child, so %w wraps e.g. 'exit status 1' while the tail shows prime's actual output.","triggerScenarios":"The re-executed `bd prime` subcommand exits non-zero: prime's internal checks fail, the binary cannot start, or ctx is cancelled before completion; the parent (an agent hook) then surfaces this wrapped error.","commonSituations":"PATH/cwd where os.Args[0] resolves to a broken or stale binary; hooks running in an environment missing the database so prime errors; context deadline exceeded in long compaction hooks; permissions preventing re-exec of the same binary.","solutions":["Read the trailing %s portion — it contains bd prime's own stderr explaining the failure.","Run `bd prime` manually in the same directory to reproduce and see the full output.","Ensure the current working directory has an initialized beads store (bd init) before hooks fire.","If the binary path is stale, reinstall/upgrade bd and retry.","For timeouts, increase the hook's context budget or reduce prime's workload."],"exampleFix":"// before\nError: bd prime: exit status 1: no beads database found\n\n// after\n$ cd /repo && bd init && bd prime   # then rerun the hook","handlingStrategy":"try-catch","validationCode":"bin, err := os.Executable() // resolve a guaranteed-valid bd binary\nif err != nil { return err }\nif _, err := os.Stat(bin); err != nil { return fmt.Errorf(\"bd binary missing: %w\", err) }","typeGuard":"func isPrimeExecError(err error) bool {\n\treturn err != nil && strings.Contains(err.Error(), \"bd prime:\")\n}","tryCatchPattern":"out, err := runBdPrime(ctx, args...)\nif err != nil {\n\tvar exitErr *exec.ExitError\n\tif errors.As(err, &exitErr) {\n\t\t// %w is the exit error; the message tail holds prime's stderr\n\t\tlog.Printf(\"bd prime failed: %v\", err)\n\t}\n\tif errors.Is(err, context.DeadlineExceeded) {\n\t\tlog.Printf(\"bd prime timed out\")\n\t}\n}","preventionTips":["Invoke hooks with the resolved executable path, not a fragile os.Args[0] assumption.","Initialize the beads store before hooks that call bd prime run.","Give hook contexts an adequate timeout.","Keep bd upgraded consistently across shell, PATH, and hook environments."],"tags":["exec","subprocess","hooks","cli"],"backgroundTag":"subprocess-exit-error","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}