From 6997d31585d58b210589ad53f380cd82903775f7 Mon Sep 17 00:00:00 2001 From: Ishaan Shah <70190533+ishaan812@users.noreply.github.com> Date: Thu, 12 Feb 2026 03:28:42 +0530 Subject: [PATCH] refactor: extract magic numbers to named constants Improves code maintainability by replacing hardcoded numeric literals and strings with named constants: - Add MaxCommitTraversalDepth (1000) for git history traversal safety limit - Replaces bare 1000 in strategy/common.go, auto_commit.go, rewind.go, explain.go - Also standardizes auto_commit.go to use errStop sentinel instead of ad-hoc error - Add maxPromptPreviewLen (100) in hooks_geminicli_handlers.go for log truncation - Add maxDisplayPromptLen (500) in manual_commit_condensation.go for display truncation - Add RedactedPlaceholder constant ("REDACTED") to redact package - Replaces bare string literal in String() function All tests pass (23 packages), build succeeds, formatting verified. Co-authored-by: Cursor --- cmd/entire/cli/explain.go | 2 +- cmd/entire/cli/hooks_geminicli_handlers.go | 5 +++-- cmd/entire/cli/rewind.go | 2 +- cmd/entire/cli/strategy/auto_commit.go | 4 ++-- cmd/entire/cli/strategy/common.go | 8 ++++++-- cmd/entire/cli/strategy/manual_commit_condensation.go | 5 +++-- redact/redact.go | 5 ++++- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/cmd/entire/cli/explain.go b/cmd/entire/cli/explain.go index d4a6511be..81db8e52d 100644 --- a/cmd/entire/cli/explain.go +++ b/cmd/entire/cli/explain.go @@ -816,7 +816,7 @@ func computeReachableFromMain(repo *git.Repository) map[plumbing.Hash]bool { } // Walk main's first-parent chain to build the set - _ = walkFirstParentCommits(repo, mainBranchHash, 1000, func(c *object.Commit) error { //nolint:errcheck // Best-effort + _ = walkFirstParentCommits(repo, mainBranchHash, strategy.MaxCommitTraversalDepth, func(c *object.Commit) error { //nolint:errcheck // Best-effort reachableFromMain[c.Hash] = true return nil }) diff --git a/cmd/entire/cli/hooks_geminicli_handlers.go b/cmd/entire/cli/hooks_geminicli_handlers.go index 1e2535244..2fc3fa7c5 100644 --- a/cmd/entire/cli/hooks_geminicli_handlers.go +++ b/cmd/entire/cli/hooks_geminicli_handlers.go @@ -417,9 +417,10 @@ func handleGeminiBeforeAgent() error { } if input.UserPrompt != "" { // Truncate long prompts for logging + const maxPromptPreviewLen = 100 promptPreview := input.UserPrompt - if len(promptPreview) > 100 { - promptPreview = promptPreview[:100] + "..." + if len(promptPreview) > maxPromptPreviewLen { + promptPreview = promptPreview[:maxPromptPreviewLen] + "..." } logArgs = append(logArgs, slog.String("prompt_preview", promptPreview)) } diff --git a/cmd/entire/cli/rewind.go b/cmd/entire/cli/rewind.go index a269f1dd6..3f6bec787 100644 --- a/cmd/entire/cli/rewind.go +++ b/cmd/entire/cli/rewind.go @@ -1164,7 +1164,7 @@ func countCommitsBetween(repo *git.Repository, ancestor, descendant plumbing.Has count := 0 current := descendant - for count < 1000 { // Safety limit + for count < strategy.MaxCommitTraversalDepth { // Safety limit if current == ancestor { return count, nil } diff --git a/cmd/entire/cli/strategy/auto_commit.go b/cmd/entire/cli/strategy/auto_commit.go index 7012e98b8..fc8e4d5a5 100644 --- a/cmd/entire/cli/strategy/auto_commit.go +++ b/cmd/entire/cli/strategy/auto_commit.go @@ -1046,8 +1046,8 @@ func (s *AutoCommitStrategy) findReferencedCheckpoints(repo *git.Repository) map count := 0 _ = iter.ForEach(func(c *object.Commit) error { //nolint:errcheck // Best effort count++ - if count > 1000 { - return errors.New("limit reached") + if count > MaxCommitTraversalDepth { + return errStop } if visited[c.Hash] { return nil diff --git a/cmd/entire/cli/strategy/common.go b/cmd/entire/cli/strategy/common.go index ec998e090..f195a10da 100644 --- a/cmd/entire/cli/strategy/common.go +++ b/cmd/entire/cli/strategy/common.go @@ -32,6 +32,10 @@ const ( branchMaster = "master" ) +// MaxCommitTraversalDepth is the safety limit for walking git commit history. +// Prevents unbounded traversal in repositories with very long histories. +const MaxCommitTraversalDepth = 1000 + // errStop is a sentinel error used to break out of git log iteration. // Shared across strategies that iterate through git commits. // NOTE: A similar sentinel exists in checkpoint/temporary.go - this is intentional. @@ -49,7 +53,7 @@ func IsEmptyRepository(repo *git.Repository) bool { // IsAncestorOf checks if commit is an ancestor of (or equal to) target. // Returns true if target can reach commit by following parent links. -// Limits search to 1000 commits to avoid excessive traversal. +// Limits search to MaxCommitTraversalDepth commits to avoid excessive traversal. func IsAncestorOf(repo *git.Repository, commit, target plumbing.Hash) bool { if commit == target { return true @@ -65,7 +69,7 @@ func IsAncestorOf(repo *git.Repository, commit, target plumbing.Hash) bool { count := 0 _ = iter.ForEach(func(c *object.Commit) error { //nolint:errcheck // Best-effort search, errors are non-fatal count++ - if count > 1000 { + if count > MaxCommitTraversalDepth { return errStop } if c.Hash == commit { diff --git a/cmd/entire/cli/strategy/manual_commit_condensation.go b/cmd/entire/cli/strategy/manual_commit_condensation.go index 6f7475694..acb4c198d 100644 --- a/cmd/entire/cli/strategy/manual_commit_condensation.go +++ b/cmd/entire/cli/strategy/manual_commit_condensation.go @@ -532,9 +532,10 @@ func generateContextFromPrompts(prompts []string) []byte { for i, prompt := range prompts { // Truncate very long prompts for readability + const maxDisplayPromptLen = 500 displayPrompt := prompt - if len(displayPrompt) > 500 { - displayPrompt = displayPrompt[:500] + "..." + if len(displayPrompt) > maxDisplayPromptLen { + displayPrompt = displayPrompt[:maxDisplayPromptLen] + "..." } buf.WriteString(fmt.Sprintf("### Prompt %d\n\n", i+1)) buf.WriteString(displayPrompt) diff --git a/redact/redact.go b/redact/redact.go index 90b123c68..06ee998a1 100644 --- a/redact/redact.go +++ b/redact/redact.go @@ -18,6 +18,9 @@ var secretPattern = regexp.MustCompile(`[A-Za-z0-9/+_=-]{10,}`) // and tokens which tend to have entropy well above 5.0. const entropyThreshold = 4.5 +// RedactedPlaceholder is the replacement text used for redacted secrets. +const RedactedPlaceholder = "REDACTED" + // String replaces high-entropy strings matching secretPattern with REDACTED. func String(s string) string { locs := secretPattern.FindAllStringIndex(s, -1) @@ -30,7 +33,7 @@ func String(s string) string { b.WriteString(s[prev:loc[0]]) match := s[loc[0]:loc[1]] if isSecret(match) { - b.WriteString("REDACTED") + b.WriteString(RedactedPlaceholder) } else { b.WriteString(match) }