Skip to content

Commit 734e8ee

Browse files
peyton-altclaude
andcommitted
refactor(redact): drop unused OPF helpers, table-drive cleanup tests
Two trim-and-simplify changes from a self-review of the PR's surface: 1. Drop redact.JoinedPromptsLegacy + its dedicated test file. The function was added to keep the writer's safety net 7-layer-only while signaling the distinction via the type system. With pre-push rewrite owning all OPF execution, the writer is now unambiguously 7-layer-only — the typed-wrapper signal is no longer earning its weight. checkpoint.redactedJoinedPrompts now inlines redact.String(strings.Join(prompts, PromptSeparator)) for the same behavior with less ceremony. 2. Drop redact.GetOPFConfigForTest. The "ForTest" helper was exported for cross-package tests but never had any callers — production code should never introspect the OPF config directly (use OPFEnabled()), and our tests already use ConfigurePrivacyFilterWithRuntime + ResetOPFConfigForTest to manage state. 3. Table-drive the four single-shadow-branch cleanup tests into one TestCleanupPushedShadowBranches_Predicate. The four cases shared ~90% of setup; the table-driven form keeps each scenario as a named sub-test while collapsing the boilerplate. The two non-single-shadow cases (mixed branches, no branches at all) stay standalone because their setups are structurally different. Net diff: -7 tests (collapsed into 4 sub-tests in 1 function), -100 lines of helpers, no behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e1a0ce0 commit 734e8ee

7 files changed

Lines changed: 54 additions & 181 deletions

File tree

cmd/entire/cli/checkpoint/prompts.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,12 @@ func SplitPromptContent(content string) []string {
3131

3232
// redactedJoinedPrompts returns the redacted prompt-blob content for the
3333
// supplied prompts. When preRedacted is set it is unwrapped verbatim;
34-
// otherwise the prompts are joined and run through the 7-layer pipeline
35-
// as a safety net.
36-
//
37-
// The safety net is intentionally 7-layer-only (no OPF), even when OPF
38-
// is enabled globally. OPF runs exclusively in the pre-push rewrite
39-
// path so per-commit condensation stays fast and predictable; the
40-
// safety net never drags OPF into a hot path. Callers that have
41-
// already produced an 8-layer blob (e.g. the pre-push rewrite itself)
42-
// pass it as preRedacted so this function returns it verbatim.
43-
//
44-
// ctx is retained on the signature for future extensions; the 7-layer
45-
// pipeline doesn't consume it today.
34+
// otherwise the prompts are joined and run through the 7-layer pipeline.
35+
// OPF runs exclusively in the pre-push rewrite (not here), so the
36+
// writer's hot path stays predictable.
4637
func redactedJoinedPrompts(_ context.Context, prompts []string, preRedacted redact.RedactedJoinedPrompts) string {
4738
if preRedacted.IsSet() {
4839
return preRedacted.String()
4940
}
50-
return redact.JoinedPromptsLegacy(prompts, PromptSeparator).String()
41+
return redact.String(strings.Join(prompts, PromptSeparator))
5142
}

cmd/entire/cli/checkpoint/prompts_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ func TestRedactedJoinedPrompts_PreRedactedIsTrustedVerbatim(t *testing.T) {
4747
assert.Equal(t, preRedacted, got, "preRedacted should pass through verbatim")
4848
}
4949

50-
// TestRedactedJoinedPrompts_ZeroValueFallsBackToLegacyRedaction verifies
51-
// that when the typed preRedacted is the zero value the helper joins the
52-
// prompts and runs the 7-layer pipeline as a safety net. Critically, the
53-
// fallback is JoinedPromptsLegacy (no OPF) — OPF runs exclusively in the
54-
// pre-push rewrite path, never here in the writer's hot path.
55-
func TestRedactedJoinedPrompts_ZeroValueFallsBackToLegacyRedaction(t *testing.T) {
50+
// TestRedactedJoinedPrompts_ZeroValueFallsBackToSafetyNet verifies
51+
// that when the typed preRedacted is the zero value the helper joins
52+
// the prompts and runs the 7-layer pipeline as a safety net. OPF runs
53+
// exclusively in the pre-push rewrite path, never here.
54+
func TestRedactedJoinedPrompts_ZeroValueFallsBackToSafetyNet(t *testing.T) {
5655
t.Parallel()
5756

5857
got := redactedJoinedPrompts(context.Background(), []string{"hello", "world"}, redact.RedactedJoinedPrompts{})
59-
assert.NotEmpty(t, got, "zero-value preRedacted should fall back to running the legacy 7-layer pipeline")
58+
assert.NotEmpty(t, got, "zero-value preRedacted should fall back to running the 7-layer pipeline")
6059
assert.Contains(t, got, PromptSeparator, "fallback output should preserve the prompt separator")
6160
}

cmd/entire/cli/strategy/cleanup_pushed_shadow_test.go

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,57 +71,45 @@ func (e *shadowCleanupEnv) branchExists(name string) bool {
7171
return err == nil
7272
}
7373

74-
// Happy case: ended session with no pending checkpoints → shadow gone.
75-
func TestCleanupPushedShadowBranches_FullyEndedDeleted(t *testing.T) {
76-
env := newShadowCleanupEnv(t)
77-
shadow := env.addShadowBranch(env.baseHash.String(), "")
78-
ended := time.Now().Add(-time.Minute)
79-
env.addSessionState("s1", env.baseHash.String(), "", &ended, nil)
80-
81-
deleted, err := CleanupPushedShadowBranches(context.Background())
82-
require.NoError(t, err)
83-
require.Equal(t, 1, deleted)
84-
require.False(t, env.branchExists(shadow))
85-
}
86-
87-
// Active session (EndedAt == nil) protects the shadow even if another
88-
// session on the same branch already ended.
89-
func TestCleanupPushedShadowBranches_ActiveSessionPreserved(t *testing.T) {
90-
env := newShadowCleanupEnv(t)
91-
shadow := env.addShadowBranch(env.baseHash.String(), "")
74+
// Predicate matrix: each shadow branch is paired with zero or more
75+
// session states; the cleanup must respect the safety rules (active
76+
// session OR pending turn checkpoints protect the branch; ended-clean
77+
// or orphaned branches are deleted).
78+
func TestCleanupPushedShadowBranches_Predicate(t *testing.T) {
9279
ended := time.Now().Add(-time.Minute)
93-
env.addSessionState("s1-ended", env.baseHash.String(), "", &ended, nil)
94-
env.addSessionState("s2-active", env.baseHash.String(), "", nil, nil)
95-
96-
deleted, err := CleanupPushedShadowBranches(context.Background())
97-
require.NoError(t, err)
98-
require.Equal(t, 0, deleted)
99-
require.True(t, env.branchExists(shadow))
100-
}
101-
102-
// Mid-finalize race window: ended session with TurnCheckpointIDs still
103-
// pending → preserve.
104-
func TestCleanupPushedShadowBranches_PendingTurnCheckpointsPreserved(t *testing.T) {
105-
env := newShadowCleanupEnv(t)
106-
shadow := env.addShadowBranch(env.baseHash.String(), "")
107-
ended := time.Now().Add(-time.Minute)
108-
env.addSessionState("s1", env.baseHash.String(), "", &ended, []string{"a1b2c3d4e5f6"})
109-
110-
deleted, err := CleanupPushedShadowBranches(context.Background())
111-
require.NoError(t, err)
112-
require.Equal(t, 0, deleted)
113-
require.True(t, env.branchExists(shadow))
114-
}
115-
116-
// Orphaned shadow branch (no matching session state) gets cleaned up.
117-
func TestCleanupPushedShadowBranches_OrphanedBranchDeleted(t *testing.T) {
118-
env := newShadowCleanupEnv(t)
119-
shadow := env.addShadowBranch(env.baseHash.String(), "")
120-
121-
deleted, err := CleanupPushedShadowBranches(context.Background())
122-
require.NoError(t, err)
123-
require.Equal(t, 1, deleted)
124-
require.False(t, env.branchExists(shadow))
80+
type sessionFixture struct {
81+
id string
82+
ended *time.Time
83+
pendingCheckpoint []string
84+
}
85+
cases := []struct {
86+
name string
87+
sessions []sessionFixture
88+
wantDeleted bool
89+
}{
90+
{name: "ended_no_pending_deleted", sessions: []sessionFixture{{id: "s1", ended: &ended}}, wantDeleted: true},
91+
{name: "active_session_preserved", sessions: []sessionFixture{{id: "s1", ended: &ended}, {id: "s2", ended: nil}}, wantDeleted: false},
92+
{name: "pending_turn_checkpoints_preserved", sessions: []sessionFixture{{id: "s1", ended: &ended, pendingCheckpoint: []string{"a1b2c3d4e5f6"}}}, wantDeleted: false},
93+
{name: "orphaned_branch_no_sessions_deleted", sessions: nil, wantDeleted: true},
94+
}
95+
for _, tc := range cases {
96+
t.Run(tc.name, func(t *testing.T) {
97+
env := newShadowCleanupEnv(t)
98+
shadow := env.addShadowBranch(env.baseHash.String(), "")
99+
for _, s := range tc.sessions {
100+
env.addSessionState(s.id, env.baseHash.String(), "", s.ended, s.pendingCheckpoint)
101+
}
102+
deleted, err := CleanupPushedShadowBranches(context.Background())
103+
require.NoError(t, err)
104+
if tc.wantDeleted {
105+
require.Equal(t, 1, deleted)
106+
require.False(t, env.branchExists(shadow))
107+
} else {
108+
require.Equal(t, 0, deleted)
109+
require.True(t, env.branchExists(shadow))
110+
}
111+
})
112+
}
125113
}
126114

127115
// Mixed: two shadow branches with different worktree IDs and different

cmd/entire/cli/strategy/manual_commit_hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2760,7 +2760,7 @@ func (s *ManualCommitStrategy) finalizeAllTurnCheckpoints(ctx context.Context, s
27602760
}
27612761

27622762
// Post-commit emits 7-layer-only blobs; the writer's safety net
2763-
// (checkpoint.redactedJoinedPrompts via JoinedPromptsLegacy) handles
2763+
// (checkpoint.redactedJoinedPrompts via redact.String) handles
27642764
// joining + 7-layer redaction. OPF is applied later, once per push,
27652765
// by the pre-push rewrite path.
27662766
redactedPrompts := redact.RedactedJoinedPrompts{}

redact/joined_prompts_test.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

redact/redact.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,6 @@ func AlreadyRedactedJoinedPrompts(content string) RedactedJoinedPrompts {
155155
return RedactedJoinedPrompts{content: content}
156156
}
157157

158-
// JoinedPromptsLegacy joins prompts with sep and runs only the seven
159-
// always-on/opt-in regex layers — never the OpenAI Privacy Filter, even
160-
// when OPF is configured globally. Used by the checkpoint writer's
161-
// safety net so post-commit blobs stay on the fast 7-layer pipeline; OPF
162-
// is applied exclusively by the pre-push rewrite path (see
163-
// strategy/manual_commit_opf_rewrite.go), which calls JoinedPrompts.
164-
//
165-
// Mirrors JoinedPrompts in return type so callers can use either
166-
// constructor and feed the result to WriteCommittedOptions.PromptsRedacted
167-
// without changing the type contract.
168-
func JoinedPromptsLegacy(prompts []string, sep string) RedactedJoinedPrompts {
169-
if len(prompts) == 0 {
170-
return RedactedJoinedPrompts{}
171-
}
172-
return RedactedJoinedPrompts{content: String(strings.Join(prompts, sep))}
173-
}
174-
175158
var (
176159
betterleaksDetector *detect.Detector
177160
betterleaksDetectorOnce sync.Once

redact/testhelpers.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
package redact
22

3-
// This file exposes internal helpers needed by tests in OTHER packages
4-
// (e.g. cmd/entire/cli/strategy) that exercise OPF behavior end-to-end.
5-
// They cannot live in export_test.go because that file is invisible to
6-
// importers — only redact's own tests would be able to call them.
7-
//
8-
// The "ForTest" suffix is a convention signal: production code MUST NOT
9-
// call these. A future lint rule (e.g. forbidigo on the "ForTest"
10-
// suffix) could enforce that statically; today it relies on reviewer
11-
// discipline.
3+
// Cross-package test helpers. Lives in a regular .go file (not
4+
// export_test.go) so tests in cmd/entire/cli/strategy can call it.
5+
// The "ForTest" suffix is the production-code-must-not-call signal.
126

13-
// GetOPFConfigForTest returns the current OPF configuration, or nil if
14-
// never configured. Test-only — production code should never need to
15-
// introspect the global config; gate behavior on OPFEnabled() instead.
16-
func GetOPFConfigForTest() *OPFConfig {
17-
return getOPFConfig()
18-
}
19-
20-
// ResetOPFConfigForTest clears configuration and the circuit breaker.
21-
// Used to return to a "never configured" state between test cases.
22-
// Test-only — production code should never need to reset OPF state.
7+
// ResetOPFConfigForTest clears OPF configuration and the circuit
8+
// breaker. Test-only.
239
func ResetOPFConfigForTest() {
2410
resetOPFConfig()
2511
}

0 commit comments

Comments
 (0)