Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,18 @@ func TestStaleBranchesEscalatesOnlyAfterPriorNotify(t *testing.T) {
func TestStaleBranchesEscalateReviewerResolutionFailureReportsError(t *testing.T) {
store := t.TempDir()
gitInit(t, store)
e := commitReviewBranchAt(t, store, "escalate-topic", []string{"rig:web"}, time.Now().Add(-3*time.Hour))
// role:, not rig: -- crn-rott9.1 fixed rig tier to resolve from the
// entry's own declared rig value instead of $GC_RIG, so only role tier can
// still be forced to fail resolution via stubGCWithRig("") below.
e := commitReviewBranchAt(t, store, "escalate-topic", []string{"role:reviewer"}, time.Now().Add(-3*time.Hour))
stateFile := filepath.Join(t.TempDir(), "state.json")

first, err := runStaleBranches(t, store, stubGCWithRig("myrig"),
"--notify-after", "1h", "--escalate-after", "2h", "--state-file", stateFile)
require.NoError(t, err)
require.Len(t, first, 1)
require.Equal(t, "notify", first[0].Status, "precondition: first pass must downgrade to notify, not escalate")
require.Equal(t, "myrig/architect", first[0].Reviewer, "precondition: reviewer must resolve successfully on the first pass")
require.Equal(t, "myrig/reviewer", first[0].Reviewer, "precondition: reviewer must resolve successfully on the first pass")
require.True(t, first[0].Notified)

second, err := runStaleBranches(t, store, stubGCWithRig(""),
Expand Down
28 changes: 28 additions & 0 deletions cmd/cull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ func TestCullEvictSharedTierProposesReviewBranchAndDoesNotDeleteDirectly(t *test
assert.Contains(t, lines[0], branch)
}

// TestCullEvictReviewerResolutionFailureCommitsNothing is crn-rott9.1's
// regression guard for requestCullReview: resolveReviewer must run BEFORE
// EvictToReviewBranch, so a resolution failure leaves no cull/<id> branch at
// all -- true fail-closed, not proposed-but-unnotified. role: is used to
// force the failure since rig: no longer depends on $GC_RIG post-crn-rott9.1.
func TestCullEvictReviewerResolutionFailureCommitsNothing(t *testing.T) {
store := t.TempDir()
gitInit(t, store)
e := seedCommittedEntry(t, store, []string{"role:reviewer"})
headBefore := strings.TrimSpace(gitOutput(t, store, "rev-parse", "HEAD"))

resetCullEvictFlags(t)
t.Cleanup(func() { resetCullEvictFlags(t) })
stubGCWithRig("")(t)
rootCmd.SetArgs([]string{"cull-evict", "--store", store, e.ID})
rootCmd.SetOut(&bytes.Buffer{})
rootCmd.SetErr(&bytes.Buffer{})
err := rootCmd.Execute()
require.Error(t, err, "an unresolvable reviewer must fail cull-evict")
assert.Contains(t, err.Error(), "GC_RIG")

headAfter := strings.TrimSpace(gitOutput(t, store, "rev-parse", "HEAD"))
assert.Equal(t, headBefore, headAfter, "an unresolvable reviewer must leave the store's HEAD untouched")

branches := gitOutput(t, store, "branch", "--list", "cull/*")
assert.Empty(t, strings.TrimSpace(branches), "an unresolvable reviewer must leave no cull/<id> review branch at all")
}

func TestCullEvictUnknownIDReturnsClearError(t *testing.T) {
store := t.TempDir()
gitInit(t, store)
Expand Down
29 changes: 29 additions & 0 deletions cmd/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ func TestPromoteMarkSharedTierProposesReviewBranchAndDoesNotCommitDirectly(t *te
assert.Contains(t, lines[0], branch)
}

// TestPromoteMarkReviewerResolutionFailureCommitsNothing is crn-rott9.1's
// regression guard for requestPromotionReview: resolveReviewer must run
// BEFORE CommitPromotionToReviewBranch, so a resolution failure leaves no
// remember/<id> branch at all -- true fail-closed, not
// committed-but-unnotified. role: is used to force the failure since rig: no
// longer depends on $GC_RIG post-crn-rott9.1.
func TestPromoteMarkReviewerResolutionFailureCommitsNothing(t *testing.T) {
store := t.TempDir()
gitInit(t, store)
e := seedCommittedEntry(t, store, []string{"role:reviewer"})
headBefore := strings.TrimSpace(gitOutput(t, store, "rev-parse", "HEAD"))

resetPromoteMarkFlags(t)
t.Cleanup(func() { resetPromoteMarkFlags(t) })
stubGCWithRig("")(t)
rootCmd.SetArgs([]string{"promote-mark", "--store", store, e.ID, "--bead", "crn-abcd"})
rootCmd.SetOut(&bytes.Buffer{})
rootCmd.SetErr(&bytes.Buffer{})
err := rootCmd.Execute()
require.Error(t, err, "an unresolvable reviewer must fail promote-mark")
assert.Contains(t, err.Error(), "GC_RIG")

headAfter := strings.TrimSpace(gitOutput(t, store, "rev-parse", "HEAD"))
assert.Equal(t, headBefore, headAfter, "an unresolvable reviewer must leave the store's HEAD untouched")

branches := gitOutput(t, store, "branch", "--list", "remember/*")
assert.Empty(t, strings.TrimSpace(branches), "an unresolvable reviewer must leave no remember/<id> review branch at all")
}

func TestPromoteMarkUnknownIDReturnsClearError(t *testing.T) {
store := t.TempDir()
gitInit(t, store)
Expand Down
41 changes: 40 additions & 1 deletion cmd/remember_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,46 @@ func TestRememberBatchGroupsMailByDistinctResolvedReviewer(t *testing.T) {
require.Len(t, c, 7, "gc mail send <reviewer> -s <subject> -m <body>")
recipients[i] = c[2]
}
assert.ElementsMatch(t, []string{"test-rig/architect", "test-rig/builder"}, recipients)
// "web/architect", not "test-rig/architect": since crn-rott9.1, rig:web
// resolves its reviewer from the entry's own declared rig value ("web"),
// not from writeStubGC's pinned $GC_RIG ("test-rig"). role:builder is
// unaffected -- role tier still resolves via $GC_RIG + "/" + value.
assert.ElementsMatch(t, []string{"web/architect", "test-rig/builder"}, recipients)
}

// TestRememberBatchReviewerResolutionFailureCommitsNothing is crn-rott9.1's
// regression guard for commitForBatchReview: resolveReviewer must run BEFORE
// CommitToReviewBranch, so a resolution failure leaves no remember/<id>
// branch for that line at all. role: is used to force the failure since
// rig: no longer depends on $GC_RIG post-crn-rott9.1.
func TestRememberBatchReviewerResolutionFailureCommitsNothing(t *testing.T) {
resetRememberFlags(t)
resetBatchFlag(t)
t.Cleanup(func() {
resetRememberFlags(t)
resetBatchFlag(t)
})

manifest := writeBatchManifest(
t,
batchManifestLine(t, map[string]any{"body": "unresolvable entry", "scope": []string{"role:reviewer"}, "topic": "resolve-fail"}),
)
store := t.TempDir()
gitInit(t, store)
stubGCWithRig("")(t)

var runErr error
stdout := captureStdout(t, func() {
rootCmd.SetArgs([]string{"remember", "--store", store, "--batch-file", manifest})
rootCmd.SetOut(&bytes.Buffer{})
rootCmd.SetErr(&bytes.Buffer{})
runErr = rootCmd.Execute()
})
require.Error(t, runErr, "a batch containing an unresolvable-reviewer line must exit non-zero")
assert.Contains(t, stdout, "GC_RIG", "the per-line failure must be reported in the plain-text summary")

branches := gitOutput(t, store, "branch", "--list", "remember/*")
assert.Empty(t, strings.TrimSpace(branches), "an unresolvable reviewer must leave no remember/<id> review branch at all")
}

// TestRememberBatchRejectsManifestOverSizeCap pins the 5000-line cap's
Expand Down
53 changes: 43 additions & 10 deletions cmd/remember_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,15 @@ func stubGCFail(t *testing.T) {

// stubGCWithRig mirrors stubGC (a stub gc binary that always succeeds), but
// pins GC_RIG to rig instead of writeStubGC's fixed "test-rig" -- letting a
// test control rig/role-tier default-reviewer resolution directly, including
// test control role-tier default-reviewer resolution directly, including
// simulating $GC_RIG being unset (rig == "") to make resolveReviewer's
// rig-tier branch fail deterministically. A caller cannot get this effect by
// just calling t.Setenv("GC_RIG", ...) around a runStaleBranches/runRemember
// call that stubs with plain stubGC: writeStubGC's own GC_RIG pin runs
// inside that call (stub(t) fires before rootCmd.Execute()) and always wins.
// role-tier branch fail deterministically. (Rig-tier resolution no longer
// depends on $GC_RIG at all as of crn-rott9.1 -- it resolves from the
// entry's own declared rig value instead -- so only role: scope can still be
// used to force this failure.) A caller cannot get this effect by just
// calling t.Setenv("GC_RIG", ...) around a runStaleBranches/runRemember call
// that stubs with plain stubGC: writeStubGC's own GC_RIG pin runs inside
// that call (stub(t) fires before rootCmd.Execute()) and always wins.
func stubGCWithRig(rig string) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
Expand Down Expand Up @@ -1169,6 +1172,35 @@ func TestRememberCLIRoundTripAllFieldsRoleTier(t *testing.T) {
assert.NoError(t, err, "created_at must be an RFC3339 timestamp (crn-3476/crn-zcxq FR-5)")
}

// TestRememberSharedTierReviewerResolutionFailureCommitsNothing is
// crn-rott9.1's regression guard for requestReview: resolveReviewer must run
// BEFORE CommitToReviewBranch, so a resolution failure leaves no
// remember/<id> review branch at all -- true fail-closed, not
// committed-but-unnotified as it was before this fix. role: is used to force
// the failure since rig: no longer depends on $GC_RIG post-crn-rott9.1 (see
// TestRememberSharedTierRigScopeVisibleAfterReviewMerge).
//
// stdout is NOT asserted empty: remember.go prints the freshly created
// entry's ID right after e.Create() writes its loose (uncommitted) file,
// before the private/shared tier branch is even decided -- that echo is
// unconditional and orthogonal to this fix, which is about the git commit to
// a review branch, not the local file write. What must be absent is any
// trace that a review branch was committed or a reviewer was mailed.
func TestRememberSharedTierReviewerResolutionFailureCommitsNothing(t *testing.T) {
var store string
var runErr error
stdout := captureStdout(t, func() {
store, runErr = runRememberWithGC(t, stubGCWithRig(""), "--topic", "valid-topic", "--scope", "role:reviewer", "a body")
})
require.Error(t, runErr, "an unresolvable reviewer must fail the command")
assert.Contains(t, runErr.Error(), "GC_RIG")
assert.NotContains(t, stdout, "review branch:", "resolution must fail before any review-branch line is printed")
assert.NotContains(t, stdout, "mailed reviewer:", "resolution must fail before any reviewer is reported as mailed")

branches := gitOutput(t, store, "branch", "--list", "remember/*")
assert.Empty(t, strings.TrimSpace(branches), "an unresolvable reviewer must leave no remember/<id> review branch at all")
}

// TestRememberSharedTierRigScopeVisibleAfterReviewMerge is the full rig:
// scope lifecycle no existing test exercised end to end before crn-0tsu
// FR-6: every other shared-tier test stops at "the review branch exists" or
Expand All @@ -1182,10 +1214,11 @@ func TestRememberCLIRoundTripAllFieldsRoleTier(t *testing.T) {
func TestRememberSharedTierRigScopeVisibleAfterReviewMerge(t *testing.T) {
store := t.TempDir()
gitInit(t, store)
// Pinned explicitly (not just relied on as writeStubGC's side effect):
// this round trip's correctness claim is specifically about $GC_RIG
// flowing through to the reviewer address, so the test states that
// dependency itself rather than borrowing it from the stub helper.
// Pinned explicitly to a value DIFFERENT from the entry's own declared
// rig ("web"), not just relied on as writeStubGC's side effect: since
// crn-rott9.1, a rig:-scope entry's default reviewer resolves from its
// own declared rig value, not $GC_RIG, and this deliberate mismatch is
// what proves $GC_RIG is correctly ignored here.
t.Setenv("GC_RIG", "test-rig")

captureFile := filepath.Join(t.TempDir(), "gc-invocation")
Expand All @@ -1199,7 +1232,7 @@ func TestRememberSharedTierRigScopeVisibleAfterReviewMerge(t *testing.T) {

args := readStubGCArgs(t, captureFile)
require.Len(t, args, 7, "gc mail send <reviewer> -s <subject> -m <body>")
assert.Equal(t, "test-rig/architect", args[2], "a rig:-scope entry's default reviewer must be $GC_RIG/architect")
assert.Equal(t, "web/architect", args[2], "a rig:-scope entry's default reviewer must be <entry's own rig>/architect, ignoring $GC_RIG entirely")

require.NoError(t, runReviewCmd(t, "review", "merge", branch, "--store", store, "--topic-key", "rig-roundtrip"))

Expand Down
75 changes: 47 additions & 28 deletions cmd/reviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ import (
// only invoke this for a scope that has already resolved away from the
// private agent tier (RunE branches on cairn.IsPrivateScope before ever
// reaching here). e's entry file already exists on disk by this point;
// nothing here can undo that, only report a downstream failure.
// nothing here can undo that, only report a downstream failure. The reviewer
// is resolved BEFORE committing to the review branch (crn-rott9.1): a
// resolution failure must leave nothing committed anywhere, not a durable
// review-branch commit nobody was ever told about.
func requestReview(cmd *cobra.Command, e *cairn.Entry, scope []string) error {
tier, value := cairn.ResolvedTier(scope)

reviewer, err := resolveReviewer(cmd, tier, value)
if err != nil {
return emitError(cmd, fmt.Errorf("resolve a reviewer to mail for entry %s: %w", e.ID, err))
}

branch, err := e.CommitToReviewBranch(cmd.Context(), storePath())
if err != nil {
return emitError(cmd, fmt.Errorf("commit shared-tier entry to review branch: %w", err))
Expand All @@ -30,11 +38,6 @@ func requestReview(cmd *cobra.Command, e *cairn.Entry, scope []string) error {
fmt.Printf("review branch: %s\n", branch)
}

reviewer, err := resolveReviewer(cmd, tier, value)
if err != nil {
return emitError(cmd, fmt.Errorf("entry %s committed to review branch %s, but resolving a reviewer to mail failed: %w",
e.ID, branch, err))
}
if err := sendReviewMail(cmd.Context(), reviewer, e, branch); err != nil {
return emitError(cmd, fmt.Errorf("entry %s committed to review branch %s, but mail to reviewer %q failed: %w",
e.ID, branch, reviewer, err))
Expand Down Expand Up @@ -71,15 +74,21 @@ func resolveReviewer(cmd *cobra.Command, tier, value string) (string, error) {
// defaultReviewer computes the per-tier default reviewer: a distinct
// recipient per tier, not one address shared across role/rig/global.
// global's "mayor" is a permanent constant, not an interim placeholder --
// the sole fleet-wide singleton reviewer.
// the sole fleet-wide singleton reviewer. rig resolves from the entry's own
// declared rig value (value), not $GC_RIG (crn-rott9.1): storage under
// rig:<value> is unambiguous about which rig's reviewer should see it,
// regardless of which rig the writing agent happens to be running in. role
// is deliberately NOT changed the same way -- role-tier storage carries no
// rig at all, so which rig's role-holder should review is genuinely
// underspecified without $GC_RIG as a convenience default.
func defaultReviewer(tier, value string) (string, error) {
switch tier {
case "global":
return "mayor", nil
case "rig":
rig := strings.TrimSpace(os.Getenv("GC_RIG"))
rig := strings.TrimSpace(value)
if rig == "" {
return "", errors.New("cannot compute the default rig reviewer: $GC_RIG is not set; pass --reviewer or $CAIRN_REVIEWER")
return "", errors.New("cannot compute the default rig reviewer: entry has no rig value in its scope tag")
}
return rig + "/architect", nil
case "role":
Expand Down Expand Up @@ -156,23 +165,25 @@ type batchReviewCandidate struct {
reviewer string
}

// commitForBatchReview is requestReview's batch-mode half: commit e to its
// own review branch and resolve who should review it, but do not mail --
// commitForBatchReview is requestReview's batch-mode half: resolve who
// should review e and commit it to its own review branch, but do not mail --
// batch mode groups many entries by resolved reviewer and sends one mail per
// group afterward (requestBatchReview) instead of mailing per entry like
// requestReview does.
// requestReview does. The reviewer is resolved BEFORE committing
// (crn-rott9.1), matching requestReview: a resolution failure must leave
// this line's entry uncommitted to any branch, not a durable review-branch
// commit its own batch group never learns about.
func commitForBatchReview(cmd *cobra.Command, e *cairn.Entry, scope []string) (branch, reviewer string, err error) {
tier, value := cairn.ResolvedTier(scope)

branch, err = e.CommitToReviewBranch(cmd.Context(), storePath())
reviewer, err = resolveReviewer(cmd, tier, value)
if err != nil {
return "", "", fmt.Errorf("commit shared-tier entry to review branch: %w", err)
return "", "", fmt.Errorf("resolve a reviewer to mail for entry %s: %w", e.ID, err)
}

reviewer, err = resolveReviewer(cmd, tier, value)
branch, err = e.CommitToReviewBranch(cmd.Context(), storePath())
if err != nil {
return branch, "", fmt.Errorf("entry %s committed to review branch %s, but resolving a reviewer to mail failed: %w",
e.ID, branch, err)
return "", "", fmt.Errorf("commit shared-tier entry to review branch: %w", err)
}
return branch, reviewer, nil
}
Expand Down Expand Up @@ -218,20 +229,24 @@ func sendBatchReviewMail(ctx context.Context, reviewer string, group []batchRevi
// requestCullReview proposes e's eviction on a review branch and mails the
// tier-appropriate reviewer -- mirrors requestReview, applied to a delete
// instead of an add. Callers must only invoke this for a scope that has
// already resolved away from the private agent tier.
// already resolved away from the private agent tier. The reviewer is
// resolved BEFORE proposing the eviction branch (crn-rott9.1): a resolution
// failure must leave nothing committed anywhere, not a durable
// eviction-proposal commit nobody was ever told about.
func requestCullReview(cmd *cobra.Command, e *cairn.Entry) error {
tier, value := cairn.ResolvedTier(e.Scope)

reviewer, err := resolveReviewer(cmd, tier, value)
if err != nil {
return fmt.Errorf("resolve a reviewer to mail for entry %s: %w", e.ID, err)
}

branch, err := e.EvictToReviewBranch(cmd.Context(), storePath())
if err != nil {
return fmt.Errorf("propose shared-tier eviction on a review branch: %w", err)
}
fmt.Printf("cull review branch: %s\n", branch)

reviewer, err := resolveReviewer(cmd, tier, value)
if err != nil {
return fmt.Errorf("entry %s proposed for eviction on branch %s, but resolving a reviewer to mail failed: %w", e.ID, branch, err)
}
if err := sendCullReviewMail(cmd.Context(), reviewer, e, branch); err != nil {
return fmt.Errorf("entry %s proposed for eviction on branch %s, but mail to reviewer %q failed: %w", e.ID, branch, reviewer, err)
}
Expand All @@ -257,22 +272,26 @@ func sendCullReviewMail(ctx context.Context, reviewer string, e *cairn.Entry, br
// has already resolved away from the private agent tier, so DESIGN.md §7's
// curation model applies to a promotion mark exactly as it does to any other
// shared-tier mutation. Mirrors requestReview/requestCullReview's three-step
// shape (commit to review branch, resolve a reviewer, mail). Callers must
// shape (resolve a reviewer, commit to review branch, mail). Callers must
// only invoke this for a scope that has already resolved away from the
// private agent tier.
// private agent tier. The reviewer is resolved BEFORE committing the
// promotion mark (crn-rott9.1): a resolution failure must leave nothing
// committed anywhere, not a durable review-branch commit nobody was ever
// told about.
func requestPromotionReview(cmd *cobra.Command, e *cairn.Entry) error {
tier, value := cairn.ResolvedTier(e.Scope)

reviewer, err := resolveReviewer(cmd, tier, value)
if err != nil {
return fmt.Errorf("resolve a reviewer to mail for entry %s: %w", e.ID, err)
}

branch, err := e.CommitPromotionToReviewBranch(cmd.Context(), storePath())
if err != nil {
return fmt.Errorf("commit promotion mark to review branch: %w", err)
}
fmt.Printf("review branch: %s\n", branch)

reviewer, err := resolveReviewer(cmd, tier, value)
if err != nil {
return fmt.Errorf("entry %s committed to review branch %s, but resolving a reviewer to mail failed: %w", e.ID, branch, err)
}
if err := sendPromotionReviewMail(cmd.Context(), reviewer, e, branch); err != nil {
return fmt.Errorf("entry %s committed to review branch %s, but mail to reviewer %q failed: %w", e.ID, branch, reviewer, err)
}
Expand Down
Loading
Loading