Skip to content

Commit 83573b4

Browse files
committed
fix: tighten TestNoMagicValues exemptions, migrate 3 more sites
Removed blanket 2-10 small int exemption. Now only 0, 1, -1, 2, 3, and 10 are exempt. Catches 5, 7, and higher magic numbers. Added constants: - time.DaysPerWeek (7) — used in relative time formatting - governance.DriftCheckMinCalls (5) — tool call drift threshold - format.PreviewLines (5) — status preview line count Signed-off-by: Jose Alekhinne <jose@ctx.ist>
1 parent 5b45457 commit 83573b4

8 files changed

Lines changed: 24 additions & 13 deletions

File tree

internal/audit/magic_values_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ package audit
99
import (
1010
"go/ast"
1111
"go/token"
12-
"strconv"
1312
"strings"
1413
"testing"
1514
)
1615

1716
// exemptIntLiterals lists integer values that are always acceptable.
17+
// 0, 1, -1: universal identity/sentinel values.
18+
// 2, 3: structural constants (split counts, field indices, ternary).
19+
// 10: decimal radix.
1820
var exemptIntLiterals = map[string]bool{
1921
"0": true,
2022
"1": true,
2123
"-1": true,
24+
"2": true,
25+
"3": true,
26+
"10": true,
2227
}
2328

2429
// strconvRadixBitsize lists numeric literals acceptable as strconv
@@ -97,13 +102,6 @@ func TestNoMagicValues(t *testing.T) {
97102
return true
98103
}
99104

100-
// Small integers 2-10 are generally acceptable (arg
101-
// counts, field indices, slice bounds, arithmetic).
102-
if val, err := strconv.Atoi(lit.Value); err == nil &&
103-
val >= 2 && val <= 10 {
104-
return true
105-
}
106-
107105
// Octal permissions are handled by TestNoRawPermissions.
108106
if strings.HasPrefix(lit.Value, "0o") ||
109107
strings.HasPrefix(lit.Value, "0O") {

internal/cli/status/core/out/out.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/ActiveMemory/ctx/internal/cli/status/core/preview"
1818
"github.com/ActiveMemory/ctx/internal/cli/status/core/sort"
1919
"github.com/ActiveMemory/ctx/internal/config/embed/text"
20+
cfgFmt "github.com/ActiveMemory/ctx/internal/config/format"
2021
cfgTime "github.com/ActiveMemory/ctx/internal/config/time"
2122
"github.com/ActiveMemory/ctx/internal/config/token"
2223
"github.com/ActiveMemory/ctx/internal/entity"
@@ -56,7 +57,7 @@ func PersistStatusJSON(
5657
ModTime: f.ModTime.Format(time.RFC3339),
5758
}
5859
if verbose && !f.IsEmpty {
59-
fs.Preview = preview.Content(string(f.Content), 5)
60+
fs.Preview = preview.Content(string(f.Content), cfgFmt.PreviewLines)
6061
}
6162
output.Files = append(output.Files, fs)
6263
}

internal/cli/system/core/archive/backup.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ func BackupProject(
123123

124124
// Touch marker file for check-backup-age hook.
125125
markerDir := filepath.Join(home, archive.BackupMarkerDir)
126-
if mkdirErr := internalIo.SafeMkdirAll(markerDir, cfgFs.PermExec); mkdirErr != nil {
126+
if mkdirErr := internalIo.SafeMkdirAll(
127+
markerDir, cfgFs.PermExec,
128+
); mkdirErr != nil {
127129
logWarn.Warn(warn.Mkdir, markerDir, mkdirErr)
128130
}
129131
markerPath := filepath.Join(markerDir, archive.BackupMarkerFile)

internal/config/format/format.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ const (
3030
// TruncateDescription is the max character width for description
3131
// text in skill listings and similar compact displays.
3232
TruncateDescription = 70
33+
34+
// PreviewLines is the number of content lines shown in status
35+
// previews and similar compact displays.
36+
PreviewLines = 5
3337
)

internal/config/mcp/governance/governance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ const (
2020
// PersistNudgeRepeat is how often the persist nudge repeats after
2121
// the initial threshold.
2222
PersistNudgeRepeat = 8
23+
24+
// DriftCheckMinCalls is the minimum tool call count before
25+
// flagging that drift has never been checked.
26+
DriftCheckMinCalls = 5
2327
)

internal/config/time/hours.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ const (
1111
HoursPerDay = 24
1212
// MinutesPerHour is the number of minutes in an hour.
1313
MinutesPerHour = 60
14+
// DaysPerWeek is the number of days in a week.
15+
DaysPerWeek = 7
1416
)

internal/format/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TimeAgo(hours float64, mins int, fallbackDate string) string {
4646
return desc.Text(text.DescKeyWriteTimeHourAgo)
4747
}
4848
return fmt.Sprintf(desc.Text(text.DescKeyWriteTimeHoursAgo), h)
49-
case hours < 7*cfgTime.HoursPerDay:
49+
case hours < cfgTime.DaysPerWeek*cfgTime.HoursPerDay:
5050
days := int(hours / cfgTime.HoursPerDay)
5151
if days == 1 {
5252
return desc.Text(text.DescKeyWriteTimeDayAgo)

internal/mcp/session/governance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ func (ss *State) CheckGovernance(toolName string) string {
105105
desc.Text(text.DescKeyGovDriftNotChecked),
106106
int(time.Since(ss.lastDriftCheck).Minutes())))
107107
}
108-
} else if ss.ToolCalls > 5 {
109-
// Never checked drift and already 5+ calls in
108+
} else if ss.ToolCalls > governance.DriftCheckMinCalls {
109+
// Never checked drift and already past threshold
110110
warnings = append(warnings,
111111
desc.Text(text.DescKeyGovDriftNeverChecked))
112112
}

0 commit comments

Comments
 (0)