From 4ee8579ddc1faa2be401d7f4905456ad3b8ecded Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 28 Jul 2026 01:32:08 +1000 Subject: [PATCH] =?UTF-8?q?refactor:=20phase=202=20of=20lint=20remediation?= =?UTF-8?q?=20=E2=80=94=20mechanical=20fixes=20(qa4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 130 -> 96. forcetypeassert, predeclared and usetesting are now at zero. Fanned out across four isolated worktrees partitioned by package so no two workers could touch the same file: internal/tools (20 assertions), internal/webui, internal/memory + internal/ratelimit, internal/secret. Four stragglers outside those scopes fixed inline (internal/pairing, internal/forge, internal/channels/telegram, internal/tools/mcp). Unchecked type assertions became comma-ok with a t.Fatalf naming the actual type. One site in a goroutine uses t.Errorf + return instead, since t.Fatalf from a non-test goroutine is undefined behaviour. predeclared renames chose names from meaning rather than adding a suffix: max -> maxQueryLen, maxMatchLen, maxRequests, alphabetLen, and nextBackoff's max -> ceiling. `real` -> `base` in the webui tests. os.Setenv became t.Setenv, which restores on cleanup, so the paired deferred Unsetenv calls went with it. Note for future fan-outs: the agent worktrees branched from origin/main (a7388a0), not from this branch, so every diff arrived based on a tree without the phase 1 autofix. They were applied with `git apply --3way` and the phase 1 changes to internal/memory/manager.go were verified to survive — in particular the repaired `ctx, cancel =` assignment. A plain `git apply` would have silently reverted it. --- internal/channels/telegram/update_test.go | 6 ++- internal/forge/github_test.go | 5 ++- internal/memory/builtin/provider_test.go | 10 ++++- internal/memory/manager.go | 6 +-- internal/memory/scanner.go | 6 +-- internal/pairing/code.go | 4 +- internal/ratelimit/limiter.go | 10 ++--- internal/ratelimit/limiter_test.go | 4 +- internal/secret/secret_test.go | 7 +-- internal/tools/disclosure_test.go | 55 ++++++++++++++++++----- internal/tools/mcp/multimodal_test.go | 12 ++++- internal/tools/mcp/transport_sse.go | 6 +-- internal/tools/toolentry_test.go | 36 ++++++++++++--- internal/webui/webui_test.go | 18 ++++---- 14 files changed, 129 insertions(+), 56 deletions(-) diff --git a/internal/channels/telegram/update_test.go b/internal/channels/telegram/update_test.go index f07dc87a3..154742490 100644 --- a/internal/channels/telegram/update_test.go +++ b/internal/channels/telegram/update_test.go @@ -46,7 +46,11 @@ func TestUpdateCallbackRejectsUnauthorizedSender(t *testing.T) { ID: "callback", From: models.User{ID: 99}, Data: updateApproveCallback, }}) - if g.Updates.(*updateStub).installCalls != 0 { + stub, ok := g.Updates.(*updateStub) + if !ok { + t.Fatalf("g.Updates is %T, want *updateStub", g.Updates) + } + if stub.installCalls != 0 { t.Error("unauthorized callback started an installation") } if len(*requests) != 1 || (*requests)[0].method != "answerCallbackQuery" { diff --git a/internal/forge/github_test.go b/internal/forge/github_test.go index 0f5567bfb..6ee8583f2 100644 --- a/internal/forge/github_test.go +++ b/internal/forge/github_test.go @@ -32,7 +32,10 @@ func TestNewConfiguresHost(t *testing.T) { if err != nil { t.Fatal(err) } - client := f.(*GitHubClient) + client, ok := f.(*GitHubClient) + if !ok { + t.Fatalf("f is %T, want *GitHubClient", f) + } if got := client.gh.BaseURL.String(); got != tt.wantBase { t.Errorf("BaseURL = %q, want %q", got, tt.wantBase) } diff --git a/internal/memory/builtin/provider_test.go b/internal/memory/builtin/provider_test.go index f450a109d..7d4b897d8 100644 --- a/internal/memory/builtin/provider_test.go +++ b/internal/memory/builtin/provider_test.go @@ -188,7 +188,10 @@ func TestProvider_HandleToolCall_Replace(t *testing.T) { if err != nil { t.Fatalf("replace: %v", err) } - result := out.(map[string]any) + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } if result["action"] != "replace" { t.Fatalf("expected action replace, got %v", result["action"]) } @@ -210,7 +213,10 @@ func TestProvider_HandleToolCall_Remove(t *testing.T) { if err != nil { t.Fatalf("remove: %v", err) } - result := out.(map[string]any) + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } if result["action"] != "remove" { t.Fatalf("expected action remove, got %v", result["action"]) } diff --git a/internal/memory/manager.go b/internal/memory/manager.go index 3e0296c22..21d8e9921 100644 --- a/internal/memory/manager.go +++ b/internal/memory/manager.go @@ -974,11 +974,11 @@ func (m *Manager) BackupPaths() []string { // truncateQuery limits the query string for log messages so a very long // query does not flood the log output. func truncateQuery(q string) string { - const max = 80 - if len(q) <= max { + const maxQueryLen = 80 + if len(q) <= maxQueryLen { return q } - return q[:max] + "..." + return q[:maxQueryLen] + "..." } // ── Internal helpers ─────────────────────────────────────────────────── diff --git a/internal/memory/scanner.go b/internal/memory/scanner.go index 8472253e1..ca53cc4dc 100644 --- a/internal/memory/scanner.go +++ b/internal/memory/scanner.go @@ -115,9 +115,9 @@ func (s *DefaultScanner) ScanContent(content string) ThreatResult { // truncateMatch limits the matched string for log/error messages so that // a long match (e.g. an entire private key) is not echoed in full. func truncateMatch(s string) string { - const max = 40 - if len(s) <= max { + const maxMatchLen = 40 + if len(s) <= maxMatchLen { return s } - return s[:max] + "..." + return s[:maxMatchLen] + "..." } diff --git a/internal/pairing/code.go b/internal/pairing/code.go index ca577c155..c8de82b91 100644 --- a/internal/pairing/code.go +++ b/internal/pairing/code.go @@ -21,10 +21,10 @@ const CodeLength = 8 // GenerateCode returns a random CodeLength-character code drawn // uniformly from Alphabet using crypto/rand. func GenerateCode() (string, error) { - max := big.NewInt(int64(len(Alphabet))) + alphabetLen := big.NewInt(int64(len(Alphabet))) code := make([]byte, CodeLength) for i := range code { - n, err := rand.Int(rand.Reader, max) + n, err := rand.Int(rand.Reader, alphabetLen) if err != nil { return "", fmt.Errorf("pairing: generate code: %w", err) } diff --git a/internal/ratelimit/limiter.go b/internal/ratelimit/limiter.go index 408296d51..4c37b6b05 100644 --- a/internal/ratelimit/limiter.go +++ b/internal/ratelimit/limiter.go @@ -29,13 +29,13 @@ type limiterKey struct { userID string } -// New returns a Limiter allowing at most max requests in any rolling -// window-length interval, per (platform, userID) pair. A max of 0 blocks -// every request. -func New(window time.Duration, max int) *Limiter { +// New returns a Limiter allowing at most maxRequests requests in any +// rolling window-length interval, per (platform, userID) pair. A +// maxRequests of 0 blocks every request. +func New(window time.Duration, maxRequests int) *Limiter { return &Limiter{ window: window, - max: max, + max: maxRequests, now: time.Now, hits: map[limiterKey][]time.Time{}, } diff --git a/internal/ratelimit/limiter_test.go b/internal/ratelimit/limiter_test.go index 30297946d..dbe3fcdd2 100644 --- a/internal/ratelimit/limiter_test.go +++ b/internal/ratelimit/limiter_test.go @@ -7,9 +7,9 @@ import ( "time" ) -func newTestLimiter(window time.Duration, max int) (*Limiter, *fakeClock) { +func newTestLimiter(window time.Duration, maxRequests int) (*Limiter, *fakeClock) { clock := &fakeClock{t: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)} - l := New(window, max) + l := New(window, maxRequests) l.now = clock.Now return l, clock } diff --git a/internal/secret/secret_test.go b/internal/secret/secret_test.go index 76fec833c..1d68dcbea 100644 --- a/internal/secret/secret_test.go +++ b/internal/secret/secret_test.go @@ -1,13 +1,11 @@ package secret import ( - "os" "testing" ) func TestEnvEngineResolve(t *testing.T) { - _ = os.Setenv("TEST_SECRET_KEY", "test-value") - defer func() { _ = os.Unsetenv("TEST_SECRET_KEY") }() + t.Setenv("TEST_SECRET_KEY", "test-value") e := &envEngine{} if e.Name() != "env" { @@ -65,8 +63,7 @@ func TestRegistryGetMissing(t *testing.T) { } func TestRegistryResolve(t *testing.T) { - _ = os.Setenv("TEST_SECRET", "resolved") - defer func() { _ = os.Unsetenv("TEST_SECRET") }() + t.Setenv("TEST_SECRET", "resolved") r := NewRegistry() v, err := r.Resolve(SecretRef{Engine: "env", Key: "TEST_SECRET"}) diff --git a/internal/tools/disclosure_test.go b/internal/tools/disclosure_test.go index 6f4cfd7b3..6ac059795 100644 --- a/internal/tools/disclosure_test.go +++ b/internal/tools/disclosure_test.go @@ -93,8 +93,14 @@ func TestBridgeToolSearch(t *testing.T) { if err != nil { t.Fatalf("tool_search error: %v", err) } - result := out.(map[string]any) - tools := result["tools"].([]ToolSummary) + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } + tools, ok := result["tools"].([]ToolSummary) + if !ok { + t.Fatalf("result[\"tools\"] is %T, want []ToolSummary", result["tools"]) + } if len(tools) != 3 { t.Errorf("expected 3 tools, got %d", len(tools)) } @@ -105,8 +111,14 @@ func TestBridgeToolSearch(t *testing.T) { if err != nil { t.Fatalf("tool_search error: %v", err) } - result := out.(map[string]any) - tools := result["tools"].([]ToolSummary) + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } + tools, ok := result["tools"].([]ToolSummary) + if !ok { + t.Fatalf("result[\"tools\"] is %T, want []ToolSummary", result["tools"]) + } if len(tools) != 2 { t.Errorf("expected 2 file tools, got %d", len(tools)) } @@ -117,8 +129,14 @@ func TestBridgeToolSearch(t *testing.T) { if err != nil { t.Fatalf("tool_search error: %v", err) } - result := out.(map[string]any) - tools := result["tools"].([]ToolSummary) + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } + tools, ok := result["tools"].([]ToolSummary) + if !ok { + t.Fatalf("result[\"tools\"] is %T, want []ToolSummary", result["tools"]) + } if len(tools) != 0 { t.Errorf("expected 0 tools, got %d", len(tools)) } @@ -129,8 +147,14 @@ func TestBridgeToolSearch(t *testing.T) { if err != nil { t.Fatalf("tool_search error: %v", err) } - result := out.(map[string]any) - tools := result["tools"].([]ToolSummary) + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } + tools, ok := result["tools"].([]ToolSummary) + if !ok { + t.Fatalf("result[\"tools\"] is %T, want []ToolSummary", result["tools"]) + } for _, ts := range tools { if IsBridgeTool(ts.Name) { t.Errorf("bridge tool %q should not appear in search results", ts.Name) @@ -169,8 +193,14 @@ func TestBridgeToolDescribe(t *testing.T) { if err != nil { t.Fatalf("tool_describe error: %v", err) } - result := out.(map[string]any) - toolMap := result["tool"].(map[string]any) + result, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } + toolMap, ok := result["tool"].(map[string]any) + if !ok { + t.Fatalf("result[\"tool\"] is %T, want map[string]any", result["tool"]) + } if toolMap["name"] != "read_file" { t.Errorf("name = %v, want 'read_file'", toolMap["name"]) } @@ -224,7 +254,10 @@ func TestBridgeToolCall(t *testing.T) { if err != nil { t.Fatalf("tool_call error: %v", err) } - m := out.(map[string]any) + m, ok := out.(map[string]any) + if !ok { + t.Fatalf("out is %T, want map[string]any", out) + } if m["msg"] != "hello" { t.Errorf("msg = %v, want 'hello'", m["msg"]) } diff --git a/internal/tools/mcp/multimodal_test.go b/internal/tools/mcp/multimodal_test.go index ea3c5bf41..fe9816008 100644 --- a/internal/tools/mcp/multimodal_test.go +++ b/internal/tools/mcp/multimodal_test.go @@ -310,7 +310,12 @@ func TestWriteMultimodalResultConcurrentCallsDoNotClobberFiles(t *testing.T) { errs[i] = err return } - results[i] = out.(tools.MultimodalResult) + res, ok := out.(tools.MultimodalResult) + if !ok { + t.Errorf("call %d: out is %T, want tools.MultimodalResult", i, out) + return + } + results[i] = res }(i) } wg.Wait() @@ -356,7 +361,10 @@ func TestCallToolHandlerMultipleImagesGetDistinctFiles(t *testing.T) { if err != nil { t.Fatal(err) } - result := out.(tools.MultimodalResult) + result, ok := out.(tools.MultimodalResult) + if !ok { + t.Fatalf("out is %T, want tools.MultimodalResult", out) + } if len(result.Files) != 2 { t.Fatalf("Files = %v, want 2 distinct files", result.Files) } diff --git a/internal/tools/mcp/transport_sse.go b/internal/tools/mcp/transport_sse.go index cafa88482..c366b2dad 100644 --- a/internal/tools/mcp/transport_sse.go +++ b/internal/tools/mcp/transport_sse.go @@ -344,10 +344,10 @@ func (t *SSETransport) reconnect() { } } -func nextBackoff(d, max time.Duration) time.Duration { +func nextBackoff(d, ceiling time.Duration) time.Duration { d *= 2 - if d > max { - return max + if d > ceiling { + return ceiling } return d } diff --git a/internal/tools/toolentry_test.go b/internal/tools/toolentry_test.go index 6972d466f..2c430fdf3 100644 --- a/internal/tools/toolentry_test.go +++ b/internal/tools/toolentry_test.go @@ -204,25 +204,47 @@ func TestToolEntryCloneDeepNested(t *testing.T) { clone := e.Clone() // Mutate nested map in original. - props := e.Schema["properties"].(JSONSchema) + props, ok := e.Schema["properties"].(JSONSchema) + if !ok { + t.Fatalf("e.Schema[\"properties\"] is %T, want JSONSchema", e.Schema["properties"]) + } props["name"] = JSONSchema{"type": "integer"} - cloneProps := clone.Schema["properties"].(JSONSchema) - if cloneProps["name"].(JSONSchema)["type"] != "string" { + cloneProps, ok := clone.Schema["properties"].(JSONSchema) + if !ok { + t.Fatalf("clone.Schema[\"properties\"] is %T, want JSONSchema", clone.Schema["properties"]) + } + clonePropName, ok := cloneProps["name"].(JSONSchema) + if !ok { + t.Fatalf("cloneProps[\"name\"] is %T, want JSONSchema", cloneProps["name"]) + } + if clonePropName["type"] != "string" { t.Error("nested schema map mutation leaked into clone") } // Mutate nested slice in original. - req := e.Schema["required"].([]any) + req, ok := e.Schema["required"].([]any) + if !ok { + t.Fatalf("e.Schema[\"required\"] is %T, want []any", e.Schema["required"]) + } req[0] = "email" - cloneReq := clone.Schema["required"].([]any) + cloneReq, ok := clone.Schema["required"].([]any) + if !ok { + t.Fatalf("clone.Schema[\"required\"] is %T, want []any", clone.Schema["required"]) + } if cloneReq[0] != "name" { t.Error("nested schema []any slice mutation leaked into clone") } // Mutate string slice in original. - tags := e.Schema["tags"].([]string) + tags, ok := e.Schema["tags"].([]string) + if !ok { + t.Fatalf("e.Schema[\"tags\"] is %T, want []string", e.Schema["tags"]) + } tags[0] = "mutated" - cloneTags := clone.Schema["tags"].([]string) + cloneTags, ok := clone.Schema["tags"].([]string) + if !ok { + t.Fatalf("clone.Schema[\"tags\"] is %T, want []string", clone.Schema["tags"]) + } if cloneTags[0] != "a" { t.Error("nested schema []string slice mutation leaked into clone") } diff --git a/internal/webui/webui_test.go b/internal/webui/webui_test.go index f0461d53a..fa22f7c95 100644 --- a/internal/webui/webui_test.go +++ b/internal/webui/webui_test.go @@ -90,13 +90,13 @@ func TestHandleSummary(t *testing.T) { // blank-identifier assignment (workflows, _ := ...), so this test FAILS // (wants 500, gets 200 with nil workflows). func TestHandleSummaryWorkflowStatsError(t *testing.T) { - real := newTestServer(t) + base := newTestServer(t) srv := &Server{ Store: &stubStore{ - TaskStore: real.Store, + TaskStore: base.Store, workflowStatsErr: fmt.Errorf("db locked"), }, - Log: real.Log, + Log: base.Log, } req := httptest.NewRequest(http.MethodGet, "/api/summary", nil) @@ -111,13 +111,13 @@ func TestHandleSummaryWorkflowStatsError(t *testing.T) { // TestHandleSummaryStageStatsError proves that handleSummary does not // silently discard an error from StageStats. func TestHandleSummaryStageStatsError(t *testing.T) { - real := newTestServer(t) + base := newTestServer(t) srv := &Server{ Store: &stubStore{ - TaskStore: real.Store, + TaskStore: base.Store, stageStatsErr: fmt.Errorf("db locked"), }, - Log: real.Log, + Log: base.Log, } req := httptest.NewRequest(http.MethodGet, "/api/summary", nil) @@ -132,13 +132,13 @@ func TestHandleSummaryStageStatsError(t *testing.T) { // TestHandleSummaryTokensByDayError proves that handleSummary does not // silently discard an error from TokensByDay. func TestHandleSummaryTokensByDayError(t *testing.T) { - real := newTestServer(t) + base := newTestServer(t) srv := &Server{ Store: &stubStore{ - TaskStore: real.Store, + TaskStore: base.Store, tokensByDayErr: fmt.Errorf("db locked"), }, - Log: real.Log, + Log: base.Log, } req := httptest.NewRequest(http.MethodGet, "/api/summary", nil)