refactor: phase 2 of lint remediation — mechanical fixes (qa4)

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.
This commit is contained in:
sam committed 2026-07-28 01:37:20 +10:00
1 parent 15d746c2f6
commit 4ee8579ddc
14 files changed
+129 -56

No files matched your search

+5 -1
View File
@@ -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" {
+4 -1
View File
@@ -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)
}
+8 -2
View File
@@ -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"])
}
+3 -3
View File
@@ -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 ───────────────────────────────────────────────────
+3 -3
View File
@@ -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] + "..."
}
+2 -2
View File
@@ -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)
}
+5 -5
View File
@@ -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{},
}
+2 -2
View File
@@ -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
}
+2 -5
View File
@@ -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"})
+44 -11
View File
@@ -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"])
}
+10 -2
View File
@@ -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)
}
+3 -3
View File
@@ -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
}
+29 -7
View File
@@ -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")
}
+9 -9
View File
@@ -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)