beads/cmd/bd/prime_test.go
Julian Knutsen 2f1e8be3ca
feat(backends): registry Remote + OpenWith options; beads.OpenBestAvailableWith for embedders (#7188)
* feat(backends): registry Remote + optional OpenWith(ctx, dir, OpenOptions)

Slice S1: backends.Backend gains Remote bool and an optional
OpenWith(ctx, beadsDir, OpenOptions{Credential, HTTPClient, UserAgent}) so a
registered extension backend can honor a per-open credential instead of
dialing with a process-global client — the motivating case is one embedder
process serving many workspaces with distinct credentials. Open keeps
working unmodified for every existing backend: Backend.OpenWithOptions calls
OpenWith when present, otherwise falls back to Open and ignores
HTTPClient/UserAgent, but refuses (ErrCredentialWithoutOpenWith) rather than
silently dropping a non-nil Credential.

Public beads.OpenBestAvailableWith(ctx, beadsDir, opts) carries this through
the SDK surface (mirrored in beads_nocgo.go and aliased in backend/backend.go
and beads.go); OpenBestAvailable is now that call with a zero OpenOptions,
identical to its prior behavior for Dolt, embedded Dolt, and every registered
backend. The plain-Dolt path (no registered backend) applies the same
Credential refusal, since Dolt has no per-open credential seam either.

doltserver.ResolvePhysicalRoots now checks backends.IsRemote(cfg.GetBackend())
before every Dolt-mode branch, including the shared-server check (which reads
only env/config.yaml and never consults cfg) — without this ordering, a global
BEADS_DOLT_SHARED_SERVER=1 would hijack a remote-backend workspace into
reporting a bogus local Dolt root it will never open.

Tests: backends_test new file open_with_test.go covers OpenWithOptions
dispatch/back-compat/Credential refusal and Remote/IsRemote; root package
open_backend_selection_test.go adds a fake OpenWith registrant proving
dispatch and option pass-through through OpenBestAvailableWith, zero-options
back-compat for every path (registered w/ and w/o OpenWith, plain Dolt), and
the Credential-without-OpenWith refusal on both paths; doltserver
physical_root_test.go adds the registered-remote-backend case including the
shared-server-override ordering regression test. Verified under both cgo and
nocgo builds. Live-mutated each new guard (the Credential refusal, the
OpenWith dispatch, the ResolvePhysicalRoots remote-backend case, and the
plain-Dolt Credential refusal) one at a time and confirmed RED before
reverting to GREEN.

No HTTP client code added; that is a later slice.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fixup(backends): address Opus review findings on OpenWith (M1-M3, L1-L2)

- M1: frame cmd/bd store-open errors for remote backends as unreachable
  server failures instead of Dolt-shaped "failed to open database"
  advice (openStoreErrorMessage, backendNameForErrorFraming), with tests.
- M2: rewrite the OpenBestAvailable/OpenBestAvailableWith back-compat
  subtest to assert fixed expected outcomes (error identity/message,
  returned store behavior) now that the two functions are identical,
  instead of comparing them to each other.
- M3: document and enforce the OpenWith contract: an implementation that
  receives a non-nil Credential it cannot type-assert to its expected
  type must refuse with the new ErrUnsupportedCredential sentinel,
  verified with a fake registrant.
- L1: document that a nil OpenOptions.Credential means the backend's
  default/ambient auth, and that multi-tenant embedders must pass a
  non-nil Credential (RequireCredential deferred to S2).
- L2: fail closed on HTTPClient/UserAgent passed to a backend without
  OpenWith, mirroring the existing Credential refusal, via new
  ErrHTTPClientWithoutOpenWith/ErrUserAgentWithoutOpenWith sentinels.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* build(bazel): auto-sync BUILD files

Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/37195230844
(`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy).
Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see
scripts/bazel-autofix-push.sh.

* fix(backends): keep the store-open error chain through the remote framing

Maintainer review fixes for the adopted PR (iteration 1).

openStoreErrorMessage returned a string, so ensureStoreActiveWithContext
rebuilt its error from text (%s) where base wrapped the open error with %w,
and the chain ended at the CLI's framing. Prime tells a store-open timeout
apart from a generic "storage unavailable" only through
errors.Is(err, context.DeadlineExceeded) on what ensureStoreActiveWithContext
returns (cmd/bd/prime.go), so the flattening silently downgraded that
diagnostic for every backend, Dolt included, not just the new Remote ones.

Replace the helper with openStoreError, which returns an error wrapping err
with %w on both arms. The message text is unchanged: "failed to open
database: <err>" for Dolt and any registered backend that is not Remote,
"failed to reach remote backend %q: <err>" for a Remote one. direct_mode.go
wraps the result with %w ahead of its Hint line; main.go keeps printing it
through HandleError("%v", ...), which only formats.

Tests:

- TestOpenStoreErrorFramesRemoteBackends (renamed from
  TestOpenStoreErrorMessageFramesRemoteBackends) now also asserts errors.Is
  on every arm, and the exact original text for the non-Remote arms.
- TestEnsureStoreActiveKeepsOpenErrorChain drives the real
  ensureStoreActiveWithContext against a registered backend whose open
  outlives its context, for a local and a Remote backend, and requires
  errors.Is(err, context.DeadlineExceeded) plus the framing prefix and the
  Hint line.
- TestFormatMemoriesForPrimeTimesOutOpeningRegisteredBackend is the
  unstubbed twin of TestFormatMemoriesForPrimeTimesOutOpeningStore and
  requires the timeout banner, not the "storage unavailable" one.

Measured at this head: turning direct_mode.go's outer %w into %v reddens
both chain-test rows and the prime test; turning the helper's %w into %v
reddens the framing test as well.

Non-gating cleanup in the same area:

- beads_cgo.go, beads_nocgo.go: OpenBestAvailableWith's doc and dispatch
  comments said only a non-nil Credential is refused. The code also refuses
  a non-nil HTTPClient and a non-empty UserAgent, each with its own
  ErrXWithoutOpenWith sentinel.
- internal/doltserver/physical_root.go: the Mode doc lists "remote-backend",
  and the RemoteBackend doc covers a registered Remote backend as well as a
  Dolt server on a non-local host.
- open_backend_selection_test.go: the plain-Dolt refusal test is a table
  over Credential, HTTPClient and UserAgent (renamed
  TestOpenBestAvailableWithRefusesPerOpenOptionsOnPlainDolt). Disabling the
  nocgo HTTPClient arm or the cgo UserAgent arm reddens only that row. The
  zero-options test's comment had the delegation backwards and pointed at
  beads.go; OpenBestAvailable delegates to OpenBestAvailableWith in
  beads_cgo.go and beads_nocgo.go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
2026-10-04 09:34:36 -07:00

929 lines
34 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/config"
)
// stubPrimeStoreUnavailable temporarily disconnects prime from any ambient
// workspace store, so tests assert against prime's own text rather than
// whatever database (and persistent memories) happens to exist in an ancestor
// directory of the test process. Without this, a developer or verify runner
// whose checkout is nested under a real beads workspace gets that workspace's
// live memory text injected into the output under test — e.g. a memory
// containing "git pull" fails the stealth-mode rejectText assertions. CI never
// sees the leak because its checkouts have no ancestor database.
//
// The stub reports ErrNoBeadsDatabase, i.e. "there is no workspace here" — the
// one memory-read failure prime still passes over in silence. Any other error
// would (correctly, since gh#5877) render the storage-unavailable banner into
// every one of these tests' output, and whether it did would depend on the
// runner's ancestor directories.
//
// Returns a function to restore the original store wiring.
// Usage:
//
// defer stubPrimeStoreUnavailable()()
func stubPrimeStoreUnavailable() func() {
origStore := store
origActive := storeActive
origEnsure := ensureStoreActiveForPrime
store = nil
storeActive = false
ensureStoreActiveForPrime = func(context.Context) error {
return fmt.Errorf("prime store stubbed out in test: %w", ErrNoBeadsDatabase)
}
return func() {
store = origStore
storeActive = origActive
ensureStoreActiveForPrime = origEnsure
}
}
func TestOutputContextFunction(t *testing.T) {
defer stubPrimeStoreUnavailable()()
tests := []struct {
name string
mcpMode bool
stealthMode bool
ephemeralMode bool
localOnlyMode bool
noPushMode bool
// noSyncRemoteMode is a separate axis from localOnlyMode: it stubs
// primeHasSyncRemote independently of primeHasGitRemote, so tests can
// exercise "git remote present, no Dolt sync remote configured"
// without conflating the two (gh#4130, gh#4230 review). Defaults to
// false (sync remote present) so existing cases that expect dolt
// hints keep passing unchanged.
noSyncRemoteMode bool
profile config.AgentProfile // "" stubs config.ProfileConservative (default)
expectText []string
rejectText []string
}{
{
name: "CLI Normal (non-ephemeral)",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
expectText: []string{"Beads Workflow Context", "bd dolt push", "Team-maintainer behavior is opt-in", "conservative by default"},
rejectText: []string{"bd export", "--from-main"},
},
{
name: "CLI team-maintainer profile (non-ephemeral)",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
profile: config.ProfileTeamMaintainer,
expectText: []string{"Beads Workflow Context", "agent.profile=team-maintainer", "bd dolt push", "git push"},
rejectText: []string{"bd export", "--from-main", "Team-maintainer behavior is opt-in"},
},
{
name: "CLI Normal (ephemeral)",
mcpMode: false,
stealthMode: false,
ephemeralMode: true,
localOnlyMode: false,
expectText: []string{"Beads Workflow Context", "bd dolt pull", "ephemeral branch"},
rejectText: []string{"bd export", "git push", "--from-main"},
},
{
name: "CLI no-push",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
noPushMode: true,
expectText: []string{"Beads Workflow Context", "push disabled", "report handoff"},
rejectText: []string{"bd export", "--from-main"},
},
{
name: "CLI Stealth",
mcpMode: false,
stealthMode: true,
ephemeralMode: false, // stealth mode overrides ephemeral detection
localOnlyMode: false,
expectText: []string{"Beads Workflow Context", "bd close", "Git authority: no git operations in this context"},
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "bd export", "No git remote configured", "Git authority: local-only/no-remote"},
},
{
name: "CLI Local-only (no git remote)",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: true,
expectText: []string{"Beads Workflow Context", "bd close", "No git remote configured", "Do not push, pull, or run remote sync", "Local git operations follow active user, orchestrator, and repository authority", "Git authority: local-only/no-remote", "git status"},
rejectText: []string{"git push", "git pull", "bd dolt push", "bd dolt pull", "--from-main", "bd export", "Git authority: no git operations in this context"},
},
{
name: "CLI Local-only team-maintainer profile",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: true,
profile: config.ProfileTeamMaintainer,
expectText: []string{"Beads Workflow Context", "Git authority: local-only/no-remote", "agent.profile=team-maintainer", "git commit"},
rejectText: []string{"git push", "git pull", "bd dolt push", "bd dolt pull", "wait for authority", "Git authority: no git operations in this context"},
},
{
name: "CLI Local-only overrides ephemeral",
mcpMode: false,
stealthMode: false,
ephemeralMode: true, // ephemeral is true but local-only takes precedence
localOnlyMode: true,
expectText: []string{"Beads Workflow Context", "bd close", "No git remote configured", "Do not push, pull, or run remote sync", "Local git operations follow active user, orchestrator, and repository authority", "Git authority: local-only/no-remote", "git status"},
rejectText: []string{"git push", "git pull", "bd dolt push", "bd dolt pull", "--from-main", "ephemeral branch", "bd export", "Git authority: no git operations in this context"},
},
{
name: "CLI Stealth overrides local-only",
mcpMode: false,
stealthMode: true,
ephemeralMode: false,
localOnlyMode: true, // local-only is true but stealth takes precedence
expectText: []string{"Beads Workflow Context", "bd close", "Git authority: no git operations in this context"},
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "No git remote configured", "Git authority: local-only/no-remote", "bd export"},
},
{
name: "MCP Normal (non-ephemeral)",
mcpMode: true,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
expectText: []string{"Beads Issue Tracker Active", "Team-maintainer behavior is opt-in"},
rejectText: []string{"bd export", "--from-main"},
},
{
name: "MCP team-maintainer profile (non-ephemeral)",
mcpMode: true,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
profile: config.ProfileTeamMaintainer,
expectText: []string{"Beads Issue Tracker Active", "agent.profile=team-maintainer", "bd dolt push"},
rejectText: []string{"bd export", "--from-main", "Team-maintainer behavior is opt-in"},
},
{
name: "MCP Normal (ephemeral)",
mcpMode: true,
stealthMode: false,
ephemeralMode: true,
localOnlyMode: false,
expectText: []string{"Beads Issue Tracker Active", "ephemeral branch"},
rejectText: []string{"bd export", "git push", "--from-main"},
},
{
name: "MCP no-push",
mcpMode: true,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
noPushMode: true,
expectText: []string{"Beads Issue Tracker Active", "push disabled"},
rejectText: []string{"bd export", "--from-main"},
},
{
name: "MCP Stealth",
mcpMode: true,
stealthMode: true,
ephemeralMode: false, // stealth mode overrides ephemeral detection
localOnlyMode: false,
expectText: []string{"Beads Issue Tracker Active", "bd close", "Git authority: no git operations in this context"},
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "bd export", "No git remote configured", "Git authority: local-only/no-remote"},
},
{
name: "MCP Local-only (no git remote)",
mcpMode: true,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: true,
expectText: []string{"Beads Issue Tracker Active", "bd close", "No git remote configured", "Do not push, pull, or run remote sync", "Local git operations follow active user, orchestrator, and repository authority", "Git authority: local-only/no-remote", "git status"},
rejectText: []string{"git push", "git pull", "bd dolt push", "bd dolt pull", "--from-main", "bd export", "Git authority: no git operations in this context"},
},
{
name: "MCP Local-only team-maintainer profile",
mcpMode: true,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: true,
profile: config.ProfileTeamMaintainer,
expectText: []string{"Beads Issue Tracker Active", "Git authority: local-only/no-remote", "No git remote configured", "agent.profile=team-maintainer", "commit local changes"},
rejectText: []string{"git push", "git pull", "bd dolt push", "bd dolt pull", "proposed handoff", "Git authority: no git operations in this context"},
},
{
name: "MCP Local-only overrides ephemeral",
mcpMode: true,
stealthMode: false,
ephemeralMode: true, // ephemeral is true but local-only takes precedence
localOnlyMode: true,
expectText: []string{"Beads Issue Tracker Active", "bd close", "No git remote configured", "Do not push, pull, or run remote sync", "Local git operations follow active user, orchestrator, and repository authority", "Git authority: local-only/no-remote", "git status"},
rejectText: []string{"git push", "git pull", "bd dolt push", "bd dolt pull", "--from-main", "ephemeral branch", "bd export", "Git authority: no git operations in this context"},
},
{
name: "MCP Stealth overrides local-only",
mcpMode: true,
stealthMode: true,
ephemeralMode: false,
localOnlyMode: true, // local-only is true but stealth takes precedence
expectText: []string{"Beads Issue Tracker Active", "bd close", "Git authority: no git operations in this context"},
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "bd export", "Git authority: local-only/no-remote"},
},
// The following cases pin the two-axis fix (gh#4130, gh#4230 review):
// git-remote presence (localOnlyMode) must drive git push/pull hints
// independently of Dolt sync-remote presence (noSyncRemoteMode),
// which must drive only the "bd dolt push"/"bd dolt pull" hint lines.
{
name: "CLI git remote present, no sync remote (non-ephemeral)",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false, // git remote present -> git hints retained
noSyncRemoteMode: true, // no Dolt sync remote -> dolt hints dropped
expectText: []string{"Beads Workflow Context", "git status", "conservative by default"},
rejectText: []string{"bd dolt push", "bd dolt pull", "No git remote configured", "Git authority: local-only/no-remote"},
},
{
name: "CLI git remote present, no sync remote (ephemeral)",
mcpMode: false,
stealthMode: false,
ephemeralMode: true,
localOnlyMode: false,
noSyncRemoteMode: true,
expectText: []string{"Beads Workflow Context", "ephemeral branch", "git status"},
rejectText: []string{"bd dolt push", "bd dolt pull", "No git remote configured"},
},
{
name: "CLI git remote present, no sync remote (team-maintainer)",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
noSyncRemoteMode: true,
profile: config.ProfileTeamMaintainer,
expectText: []string{"Beads Workflow Context", "agent.profile=team-maintainer", "git push"},
rejectText: []string{"bd dolt push", "bd dolt pull", "No git remote configured"},
},
{
name: "MCP git remote present, no sync remote (team-maintainer)",
mcpMode: true,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
noSyncRemoteMode: true,
profile: config.ProfileTeamMaintainer,
expectText: []string{"Beads Issue Tracker Active", "agent.profile=team-maintainer", "commit and git push"},
rejectText: []string{"bd dolt push", "bd dolt pull", "No git remote configured"},
},
{
name: "CLI sync remote present (no-push) -> dolt hints retained",
mcpMode: false,
stealthMode: false,
ephemeralMode: false,
localOnlyMode: false,
noPushMode: true,
expectText: []string{"Beads Workflow Context", "bd dolt push", "bd dolt pull", "push disabled"},
rejectText: []string{"No git remote configured"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer stubIsEphemeralBranch(tt.ephemeralMode)()
defer stubPrimeHasGitRemote(!tt.localOnlyMode)()
defer stubPrimeHasSyncRemote(!tt.localOnlyMode && !tt.noSyncRemoteMode)()
defer stubPrimeNoPushConfigured(tt.noPushMode)()
profile := tt.profile
if profile == "" {
profile = config.ProfileConservative
}
defer stubPrimeAgentProfile(profile)()
var buf bytes.Buffer
err := outputPrimeContext(&buf, tt.mcpMode, tt.stealthMode)
if err != nil {
t.Fatalf("outputPrimeContext failed: %v", err)
}
output := buf.String()
for _, expected := range tt.expectText {
if !strings.Contains(output, expected) {
t.Errorf("Expected text not found: %s", expected)
}
}
for _, rejected := range tt.rejectText {
if strings.Contains(output, rejected) {
t.Errorf("Unexpected text found: %s", rejected)
}
}
})
}
}
func TestPrimeLocalOnlyDoesNotClaimNoGitAuthority(t *testing.T) {
defer stubPrimeStoreUnavailable()()
defer stubPrimeAgentProfile(config.ProfileConservative)()
for _, tc := range []struct {
name string
mcpMode bool
}{
{name: "CLI", mcpMode: false},
{name: "MCP", mcpMode: true},
} {
t.Run(tc.name, func(t *testing.T) {
defer stubIsEphemeralBranch(false)()
defer stubPrimeHasGitRemote(false)()
defer stubPrimeNoPushConfigured(false)()
var buf bytes.Buffer
if err := outputPrimeContext(&buf, tc.mcpMode, false); err != nil {
t.Fatalf("outputPrimeContext failed: %v", err)
}
output := buf.String()
for _, expected := range []string{
"Git authority: local-only/no-remote",
"No git remote configured",
"Do not push, pull, or run remote sync",
"Local git operations follow active user, orchestrator, and repository authority",
"git status",
} {
if !strings.Contains(output, expected) {
t.Fatalf("expected local-only output to contain %q; output:\n%s", expected, output)
}
}
for _, rejected := range []string{
"Git authority: no git operations in this context",
"git push",
"git pull",
"bd dolt push",
"bd dolt pull",
} {
if strings.Contains(output, rejected) {
t.Fatalf("local-only output should not contain %q; output:\n%s", rejected, output)
}
}
})
}
}
func TestPrimeClaimGuidanceUsesAtomicClaim(t *testing.T) {
defer stubPrimeStoreUnavailable()()
defer stubIsEphemeralBranch(false)()
defer stubPrimeHasGitRemote(true)()
var buf bytes.Buffer
if err := outputPrimeContext(&buf, false, false); err != nil {
t.Fatalf("outputPrimeContext failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "bd update <id> --claim") {
t.Fatal("prime output should teach bd update <id> --claim")
}
if strings.Contains(output, "bd update <id> --status=in_progress") {
t.Fatal("prime output should not teach bd update <id> --status=in_progress")
}
}
// TestPrimeMemoryGuidanceDoesNotProhibitHarnessMemory guards GH#6111: both
// renderings used to emit "Do NOT use MEMORY.md files", which contradicts
// harnesses that ship a first-party memory whose index file has exactly that
// name. The guidance now names the store each content class belongs in, so
// neither rendering may name the file again. The assertion is negative on the
// old prohibition rather than positive on the new prose, so it survives any
// future rewording; the bd remember check keeps it from passing vacuously if
// the memory bullet is dropped altogether.
func TestPrimeMemoryGuidanceDoesNotProhibitHarnessMemory(t *testing.T) {
defer stubPrimeStoreUnavailable()()
defer stubIsEphemeralBranch(false)()
defer stubPrimeHasGitRemote(true)()
defer stubPrimeAgentProfile(config.ProfileConservative)()
for _, tc := range []struct {
name string
mcpMode bool
}{
{name: "CLI", mcpMode: false},
{name: "MCP", mcpMode: true},
} {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
if err := outputPrimeContext(&buf, tc.mcpMode, false); err != nil {
t.Fatalf("outputPrimeContext failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "bd remember") {
t.Fatalf("prime output should still teach bd remember; output:\n%s", output)
}
if strings.Contains(output, "MEMORY.md") {
t.Errorf("prime output should not name MEMORY.md; output:\n%s", output)
}
})
}
}
func TestPrimeStartsWithTruncationDirective(t *testing.T) {
defer stubPrimeStoreUnavailable()()
defer stubIsEphemeralBranch(false)()
defer stubPrimeHasGitRemote(true)()
for _, mcpMode := range []bool{false, true} {
var buf bytes.Buffer
if err := outputPrimeContext(&buf, mcpMode, false); err != nil {
t.Fatalf("outputPrimeContext failed: %v", err)
}
if !strings.HasPrefix(buf.String(), primeTruncationDirective) {
t.Fatalf("prime output should start with truncation directive; got %q", buf.String()[:min(120, buf.Len())])
}
}
}
func TestPrimeMemoriesOnlyNoMemories(t *testing.T) {
defer stubPrimeStoreUnavailable()()
var buf bytes.Buffer
if err := outputPrimeContextWithOptions(&buf, false, false, true); err != nil {
t.Fatalf("outputPrimeContextWithOptions failed: %v", err)
}
output := buf.String()
if !strings.HasPrefix(output, primeTruncationDirective) {
t.Fatal("memories-only output should start with truncation directive")
}
if strings.Contains(output, "Essential Commands") {
t.Fatalf("memories-only output should not include the full workflow guide: %s", output)
}
}
func TestFormatMemoriesForPrimeTimesOutOpeningStore(t *testing.T) {
oldStore := store
oldStoreActive := storeActive
oldEnsure := ensureStoreActiveForPrime
store = nil
storeActive = false
ensureStoreActiveForPrime = func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}
t.Cleanup(func() {
store = oldStore
storeActive = oldStoreActive
ensureStoreActiveForPrime = oldEnsure
})
t.Setenv(primeStoreTimeoutEnv, "1ms")
out := formatMemoriesForPrime(false)
if !strings.Contains(out, "timed out") {
t.Fatalf("expected timeout warning in prime memory output, got %q", out)
}
if !strings.Contains(out, "stale storage lock") {
t.Fatalf("expected stale-lock guidance in prime memory output, got %q", out)
}
}
// TestFormatMemoriesForPrimeTimesOutOpeningRegisteredBackend is the unstubbed
// twin of TestFormatMemoriesForPrimeTimesOutOpeningStore, which replaces
// ensureStoreActiveForPrime and so cannot see how the real open frames its
// error. Here the real open dials a registered remote backend that outlives
// BEADS_PRIME_TIMEOUT, and the timeout banner must still win over the generic
// "storage unavailable" one.
func TestFormatMemoriesForPrimeTimesOutOpeningRegisteredBackend(t *testing.T) {
const backend = "contract-prime-open-timeout"
registerContractBlockingBackend(t, backend, true)
t.Setenv("BEADS_DIR", writeContractBackendConfig(t, backend))
t.Setenv(primeStoreTimeoutEnv, "50ms")
oldStore := store
oldStoreActive := storeActive
oldEnsure := ensureStoreActiveForPrime
oldProxied := proxiedServerMode
store = nil
storeActive = false
ensureStoreActiveForPrime = ensureStoreActiveWithContext
proxiedServerMode = false
t.Cleanup(func() {
store = oldStore
storeActive = oldStoreActive
ensureStoreActiveForPrime = oldEnsure
proxiedServerMode = oldProxied
})
out := formatMemoriesForPrime(false)
if !strings.Contains(out, "timed out after 50ms") {
t.Fatalf("expected the store-open timeout banner in prime memory output, got %q", out)
}
}
// stubPrimeStoreOpen points prime's lazy store open at the given error and
// clears the ambient store, so a test drives formatMemoriesForPrime through a
// chosen failure edge. proxiedServerMode is forced off so the classic route is
// the one under test regardless of ambient wiring.
func stubPrimeStoreOpen(t *testing.T, err error) {
t.Helper()
oldStore := store
oldStoreActive := storeActive
oldEnsure := ensureStoreActiveForPrime
oldProxied := proxiedServerMode
store = nil
storeActive = false
proxiedServerMode = false
ensureStoreActiveForPrime = func(context.Context) error { return err }
t.Cleanup(func() {
store = oldStore
storeActive = oldStoreActive
ensureStoreActiveForPrime = oldEnsure
proxiedServerMode = oldProxied
})
}
// TestFormatMemoriesForPrimeReportsUnavailableStore is the gh#5877 regression:
// a broken or unreachable store used to make prime omit the memory section
// entirely, so "no memories" and "memory plane down" produced identical output
// and an identical exit 0. The failure must now announce itself.
func TestFormatMemoriesForPrimeReportsUnavailableStore(t *testing.T) {
openErr := fmt.Errorf("failed to open database: dial tcp 127.0.0.1:3308: connect: connection refused\nHint: run bd doctor")
tests := []struct {
name string
compact bool
header string
}{
{name: "full", compact: false, header: "## Persistent Memories\n\nSkipped: beads storage unavailable ("},
{name: "compact", compact: true, header: "## Memories\n- Skipped: beads storage unavailable ("},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stubPrimeStoreOpen(t, openErr)
out := formatMemoriesForPrime(tt.compact)
for _, want := range []string{tt.header, "connection refused", "were NOT injected this session", "bd doctor"} {
if !strings.Contains(out, want) {
t.Fatalf("prime memory output missing %q, got %q", want, out)
}
}
if strings.Contains(out, "Hint: run bd doctor") {
t.Fatalf("multi-line store error leaked past the first line into prime output: %q", out)
}
})
}
}
// A workspace-less directory is the one failure prime still swallows: there is
// nothing to inject and nothing is wrong, so a fresh checkout gets no banner.
func TestFormatMemoriesForPrimeStaysSilentWithoutWorkspace(t *testing.T) {
stubPrimeStoreOpen(t, fmt.Errorf("%w.\nHint: run 'bd init'", ErrNoBeadsDatabase))
for _, compact := range []bool{false, true} {
if out := formatMemoriesForPrime(compact); out != "" {
t.Fatalf("formatMemoriesForPrime(compact=%v) = %q, want silence when no workspace is resolved", compact, out)
}
}
}
func TestFormatPrimeMemoryUnavailable(t *testing.T) {
tests := []struct {
name string
compact bool
err error
want []string
reject []string
}{
{
name: "full shape",
compact: false,
err: errors.New("connection refused"),
want: []string{
"\n## Persistent Memories\n\nSkipped: beads storage unavailable (connection refused) — persistent memories were NOT injected this session.",
"if the store is a Dolt server, check it is running and reachable.",
},
},
{
name: "compact shape",
compact: true,
err: errors.New("connection refused"),
want: []string{"\n## Memories\n- Skipped: beads storage unavailable (connection refused) — persistent memories were NOT injected this session."},
reject: []string{"## Persistent Memories"},
},
{
name: "nil error still says injection did not run",
compact: false,
err: nil,
want: []string{"(unknown error)", "NOT injected"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := formatPrimeMemoryUnavailable(tt.compact, tt.err)
for _, want := range tt.want {
if !strings.Contains(out, want) {
t.Fatalf("banner missing %q, got %q", want, out)
}
}
for _, reject := range tt.reject {
if strings.Contains(out, reject) {
t.Fatalf("banner unexpectedly contains %q, got %q", reject, out)
}
}
if !strings.HasSuffix(out, "\n") {
t.Fatalf("banner must end in a newline so the next section starts cleanly, got %q", out)
}
})
}
}
func TestPrimeErrorSummary(t *testing.T) {
long := strings.Repeat("é", primeMemoryErrorMaxLen*2)
tests := []struct {
name string
err error
want string
}{
{name: "nil", err: nil, want: "unknown error"},
{name: "blank", err: errors.New(" \n "), want: "unknown error"},
{name: "single line", err: errors.New("connection refused"), want: "connection refused"},
{name: "first line only", err: errors.New("open failed: boom\nHint: run bd doctor\nmore detail"), want: "open failed: boom"},
{name: "whitespace collapsed", err: errors.New("open failed:\tboom"), want: "open failed: boom"},
{name: "capped", err: errors.New(long), want: strings.Repeat("é", primeMemoryErrorMaxLen) + "…"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := primeErrorSummary(tt.err); got != tt.want {
t.Fatalf("primeErrorSummary() = %q, want %q", got, tt.want)
}
})
}
}
func TestPrimeStoreTimeoutNonPositiveUsesDefault(t *testing.T) {
for _, value := range []string{"0", "0s", "-5s"} {
t.Run(value, func(t *testing.T) {
t.Setenv(primeStoreTimeoutEnv, value)
if got := primeStoreTimeout(); got != primeStoreTimeoutDefault {
t.Fatalf("primeStoreTimeout() = %s, want default %s", got, primeStoreTimeoutDefault)
}
})
}
}
func TestPrimeContextUsesWorkspaceLanguage(t *testing.T) {
defer stubPrimeStoreUnavailable()()
defer stubIsEphemeralBranch(false)()
defer stubPrimeHasGitRemote(true)()
var buf bytes.Buffer
if err := outputPrimeContext(&buf, false, false); err != nil {
t.Fatalf("outputPrimeContext failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "resolved") || !strings.Contains(output, "workspace") {
t.Fatalf("prime output should describe resolved workspace semantics: %s", output)
}
if strings.Contains(output, "when .beads/ detected") {
t.Fatal("prime output should not imply local .beads detection is required")
}
}
// stubIsEphemeralBranch temporarily replaces isEphemeralBranch
// with a stub returning returnValue.
//
// Returns a function to restore the original isEphemeralBranch.
// Usage:
//
// defer stubIsEphemeralBranch(true)()
func stubIsEphemeralBranch(isEphem bool) func() {
original := isEphemeralBranch
isEphemeralBranch = func() bool {
return isEphem
}
return func() {
isEphemeralBranch = original
}
}
// stubPrimeHasGitRemote temporarily replaces primeHasGitRemote
// with a stub returning returnValue.
//
// Returns a function to restore the original primeHasGitRemote.
// Usage:
//
// defer stubPrimeHasGitRemote(true)()
func stubPrimeHasGitRemote(hasRemote bool) func() {
original := primeHasGitRemote
primeHasGitRemote = func() bool {
return hasRemote
}
return func() {
primeHasGitRemote = original
}
}
// stubPrimeNoPushConfigured temporarily replaces primeNoPushConfigured
// with a stub returning noPush.
func stubPrimeNoPushConfigured(noPush bool) func() {
original := primeNoPushConfigured
primeNoPushConfigured = func() bool {
return noPush
}
return func() {
primeNoPushConfigured = original
}
}
// stubPrimeAgentProfile temporarily replaces primeAgentProfile with a stub
// returning profile (gh#3423 agent.profile knob).
func stubPrimeAgentProfile(profile config.AgentProfile) func() {
original := primeAgentProfile
primeAgentProfile = func() config.AgentProfile {
return profile
}
return func() {
primeAgentProfile = original
}
}
func stubPrimeHasSyncRemote(hasSyncRemote bool) func() {
original := primeHasSyncRemote
primeHasSyncRemote = func() bool {
return hasSyncRemote
}
return func() {
primeHasSyncRemote = original
}
}
func TestPrimeGlobalFallback(t *testing.T) {
// Create a temp directory to act as config dir
tmpDir := t.TempDir()
beadsConfigDir := filepath.Join(tmpDir, "beads")
if err := os.MkdirAll(beadsConfigDir, 0755); err != nil {
t.Fatalf("mkdir: %v", err)
}
content := "# Global PRIME override\nCustom instructions here.\n"
if err := os.WriteFile(filepath.Join(beadsConfigDir, "PRIME.md"), []byte(content), 0644); err != nil {
t.Fatalf("write PRIME.md: %v", err)
}
// Call the helper that resolves the global prime path
got := resolveGlobalPrimePath(tmpDir)
if got == "" {
t.Fatal("resolveGlobalPrimePath returned empty, want path to global PRIME.md")
}
data, err := os.ReadFile(got)
if err != nil {
t.Fatalf("ReadFile(%s): %v", got, err)
}
if string(data) != content {
t.Errorf("content = %q, want %q", string(data), content)
}
}
func TestPrimeGlobalFallback_Missing(t *testing.T) {
// When no global PRIME.md exists, should return empty string
tmpDir := t.TempDir()
got := resolveGlobalPrimePath(tmpDir)
if got != "" {
t.Errorf("resolveGlobalPrimePath = %q, want empty for missing file", got)
}
}
// hookJSONEnvelope mirrors the JSON shape produced by outputHookJSON —
// kept in test code so the assertion fails loudly if the production shape
// drifts.
type hookJSONEnvelope struct {
HookSpecificOutput struct {
HookEventName string `json:"hookEventName"`
AdditionalContext string `json:"additionalContext"`
} `json:"hookSpecificOutput"`
}
func TestOutputHookJSON_ShapeWithContent(t *testing.T) {
var buf bytes.Buffer
const payload = "# Hello\n\nbd ready\n"
if err := outputHookJSON(&buf, payload); err != nil {
t.Fatalf("outputHookJSON: %v", err)
}
// json.Encoder.Encode appends a trailing newline; the JSON itself must
// still be valid.
out := strings.TrimRight(buf.String(), "\n")
var env hookJSONEnvelope
if err := json.Unmarshal([]byte(out), &env); err != nil {
t.Fatalf("output is not valid JSON: %v\noutput: %s", err, buf.String())
}
if env.HookSpecificOutput.HookEventName != "SessionStart" {
t.Errorf("hookEventName = %q, want SessionStart", env.HookSpecificOutput.HookEventName)
}
if env.HookSpecificOutput.AdditionalContext != payload {
t.Errorf("additionalContext = %q, want %q", env.HookSpecificOutput.AdditionalContext, payload)
}
}
func TestOutputHookJSON_EmptyContent(t *testing.T) {
// Empty envelope is the contract for "nothing to inject" — the hook host
// still requires valid JSON on stdout, so we cannot just emit nothing.
var buf bytes.Buffer
if err := outputHookJSON(&buf, ""); err != nil {
t.Fatalf("outputHookJSON: %v", err)
}
var env hookJSONEnvelope
if err := json.Unmarshal(bytes.TrimRight(buf.Bytes(), "\n"), &env); err != nil {
t.Fatalf("output is not valid JSON: %v\noutput: %s", err, buf.String())
}
if env.HookSpecificOutput.HookEventName != "SessionStart" {
t.Errorf("hookEventName = %q, want SessionStart", env.HookSpecificOutput.HookEventName)
}
if env.HookSpecificOutput.AdditionalContext != "" {
t.Errorf("additionalContext = %q, want empty", env.HookSpecificOutput.AdditionalContext)
}
}
// TestPrime_RawMarkdown_NotJSON_WithoutFlag is a regression guard: without
// --hook-json, prime output must remain raw markdown (used by CLI users and
// any hook-free integrations). It would be a regression if the JSON envelope
// leaked into the default path.
func TestPrime_RawMarkdown_NotJSON_WithoutFlag(t *testing.T) {
defer stubPrimeStoreUnavailable()()
defer stubIsEphemeralBranch(false)()
defer stubPrimeHasGitRemote(true)()
var buf bytes.Buffer
if err := outputPrimeContext(&buf, false, false); err != nil {
t.Fatalf("outputPrimeContext: %v", err)
}
output := buf.String()
if strings.HasPrefix(strings.TrimSpace(output), "{") {
preview := output
if len(preview) > 200 {
preview = preview[:200]
}
t.Fatalf("prime output without --hook-json should be raw markdown, got JSON-looking content: %q", preview)
}
// Best-effort: confirm the raw markdown contract holds.
var envelope map[string]interface{}
if err := json.Unmarshal([]byte(output), &envelope); err == nil {
t.Fatal("prime output without --hook-json should not be valid JSON (regression guard)")
}
}
// GH#6095: bd prime --help must document all three PRIME.md resolution
// tiers (current-directory, resolved workspace .beads, global config) in
// their actual lookup order, not just the first tier.
func TestPrimeHelpMentionsAllFallbackTiers(t *testing.T) {
// Each needle carries its (N) label so that swapping only the labels,
// not the descriptions, is caught by the presence check below: the swap
// drives every strings.Index to -1, which the ordering loop explicitly
// skips.
needles := []string{
"(1) .beads/PRIME.md relative to the current directory",
"(2) PRIME.md in the .beads directory bd resolves for this workspace",
"(3) the global PRIME.md in bd's user config dir",
}
positions := make([]int, len(needles))
for i, needle := range needles {
pos := strings.Index(primeCmd.Long, needle)
if pos == -1 {
t.Errorf("prime help missing %q", needle)
}
positions[i] = pos
}
for i := 1; i < len(positions); i++ {
if positions[i-1] == -1 || positions[i] == -1 {
continue
}
if positions[i] <= positions[i-1] {
t.Errorf("prime help documents fallback tiers out of order: %q at %d should come before %q at %d", needles[i-1], positions[i-1], needles[i], positions[i])
}
}
// Tier (3) resolves via os.UserConfigDir(), whose location differs per
// platform. The help text is static, so it must spell out every OS's
// path rather than pinning one platform's spelling as if it were
// universal.
for _, configDirPath := range []string{
"~/.config/beads/",
"~/Library/Application Support/beads/",
`%AppData%\beads\`,
} {
if !strings.Contains(primeCmd.Long, configDirPath) {
t.Errorf("prime help tier (3) missing user config dir path %q", configDirPath)
}
}
}