beads/cmd/bd/prime_disabled_test.go
Julian Knutsen 74a67c3c27
feat(prime): add prime.disabled so a session can turn off an inherited bd prime hook (#7595)
Claude Code merges a project's hook settings into every session started in that
checkout, including an orchestrator's agent sessions, which get their own
briefing. A higher-precedence settings file cannot remove a lower one's hook, so
such sessions run `bd prime` whether it helps them or not. The only per-session
channel their host controls is the environment.

prime.disabled (BD_PRIME_DISABLED) makes bd prime print nothing, the same as
outside a workspace (an empty envelope under --hook-json), whatever its other
flags. It returns before any workspace or store access. Like the prime memory
caps it is a yaml-only key, and it is validated with strconv.ParseBool, the set
that viper's GetBool reads.

Measured on a copy of a 751-memory store, bytes a hook injects:
- bd prime: 1,251,189 (the default is unchanged);
- BD_PRIME_DISABLED=true bd prime, --full, --memories-only: 0;
- BD_PRIME_DISABLED=true bd prime --hook-json: the empty envelope.

In the Gas City deployment that owns that store, 637 agent sessions in 48 hours
fired the inherited hook 1,271 times. Each firing injected a 2.3 KB preview after
host truncation. On top of that, the sessions read 1.81 MB of the persisted
output. That is about 4.8 MB of context these sessions do not use.

Tests:
- TestPrimeDisabledReadsTheConfigKey.
- TestPrimeDisabledPrintsNothingInAWorkspace. A staged workspace's briefing
  prints when the key is off; the test fails if the early return is removed.
- TestValidateYamlConfigValue_PrimeDisabled.
- The embedded TestEmbeddedPrime/prime_disabled_env and
  /prime_disabled_config subtests.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-16 08:31:08 +00:00

82 lines
2.6 KiB
Go

package main
import (
"bytes"
"os"
"path/filepath"
"testing"
)
// stubPrimeConfigBool makes primeConfigBool read from a map for one test.
func stubPrimeConfigBool(t *testing.T, values map[string]bool) {
t.Helper()
orig := primeConfigBool
primeConfigBool = func(key string) bool { return values[key] }
t.Cleanup(func() { primeConfigBool = orig })
}
// stagePrimeWorkspace makes a workspace whose briefing is a known custom
// PRIME.md with no memories, so an enabled bd prime visibly prints.
func stagePrimeWorkspace(t *testing.T, briefing string) {
t.Helper()
beadsDir := filepath.Join(t.TempDir(), ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatal(err)
}
for name, body := range map[string]string{"config.yaml": "", "PRIME.md": briefing} {
if err := os.WriteFile(filepath.Join(beadsDir, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
t.Setenv("BEADS_DIR", beadsDir)
origNoMemories, origHookJSON := primeNoMemories, primeHookJSONMode
primeNoMemories = true
t.Cleanup(func() { primeNoMemories, primeHookJSONMode = origNoMemories, origHookJSON })
}
func hookEnvelope(t *testing.T, content string) string {
t.Helper()
var buf bytes.Buffer
if err := outputHookJSON(&buf, content); err != nil {
t.Fatalf("outputHookJSON: %v", err)
}
return buf.String()
}
func TestPrimeDisabledReadsTheConfigKey(t *testing.T) {
stubPrimeConfigBool(t, map[string]bool{})
if primeDisabled() {
t.Fatal("bd prime must be on unless prime.disabled is set")
}
stubPrimeConfigBool(t, map[string]bool{"prime.disabled": true})
if !primeDisabled() {
t.Fatal("prime.disabled=true must turn bd prime off")
}
}
// With prime.disabled, bd prime adds nothing to the session, exactly as it
// does outside a workspace, even though a workspace with a briefing exists.
func TestPrimeDisabledPrintsNothingInAWorkspace(t *testing.T) {
const briefing = "custom briefing\n"
stagePrimeWorkspace(t, briefing)
run := func(hookJSON bool) string {
primeHookJSONMode = hookJSON
return captureStdout(t, func() error { return primeCmd.RunE(primeCmd, nil) })
}
stubPrimeConfigBool(t, map[string]bool{})
if got := run(false); got != briefing {
t.Fatalf("enabled bd prime printed %q, want the workspace briefing %q", got, briefing)
}
if got := run(true); got != hookEnvelope(t, briefing) {
t.Fatalf("enabled bd prime --hook-json printed %q", got)
}
stubPrimeConfigBool(t, map[string]bool{"prime.disabled": true})
if got := run(false); got != "" {
t.Fatalf("disabled bd prime printed %q, want nothing", got)
}
if got := run(true); got != hookEnvelope(t, "") {
t.Fatalf("disabled bd prime --hook-json printed %q, want the empty envelope", got)
}
}