From b2da85ead101162ae41e9eb7b853c5e166cd5e91 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 29 Aug 2026 00:20:05 +1000 Subject: [PATCH] refactor: go modernisation fixes --- cmd/archied/bootstrap.go | 5 +++-- internal/domain/curator/activity.go | 5 +++-- internal/domain/curator/registry.go | 4 ++-- internal/domain/workflow/tdd.go | 7 ++++--- internal/forge/gitea.go | 18 ++++++++--------- internal/forge/github.go | 2 +- internal/gateway/gateway_test.go | 24 ++++++----------------- internal/indexing/codesearch.go | 3 +-- internal/memory/manager.go | 5 ++--- internal/tools/builtin/truncate.go | 5 +++-- internal/tools/provider/provider.go | 8 ++++---- internal/webui/api_tasks.go | 3 +-- internal/webui/api_workflows.go | 3 +-- internal/worktree/worktree_repair_test.go | 2 +- 14 files changed, 41 insertions(+), 53 deletions(-) diff --git a/cmd/archied/bootstrap.go b/cmd/archied/bootstrap.go index 3d37d6064..04972396e 100644 --- a/cmd/archied/bootstrap.go +++ b/cmd/archied/bootstrap.go @@ -14,6 +14,7 @@ import ( "os" "os/signal" "path/filepath" + "slices" "sort" "sync/atomic" "syscall" @@ -150,8 +151,8 @@ func (b *boot) addCleanup(fn func()) { } func (b *boot) cleanup() { - for i := len(b.cleanups) - 1; i >= 0; i-- { - b.cleanups[i]() + for _, v := range slices.Backward(b.cleanups) { + v() } } diff --git a/internal/domain/curator/activity.go b/internal/domain/curator/activity.go index d222b713d..eb0db8234 100644 --- a/internal/domain/curator/activity.go +++ b/internal/domain/curator/activity.go @@ -1,6 +1,7 @@ package curator import ( + "slices" "sync" "time" ) @@ -51,8 +52,8 @@ func (t *activityTracker) record(name string, at time.Time, actions []Action) { a.LastRunActions = len(actions) merged := make([]Action, 0, len(actions)+len(a.Recent)) - for i := len(actions) - 1; i >= 0; i-- { - merged = append(merged, actions[i]) + for _, action := range slices.Backward(actions) { + merged = append(merged, action) } merged = append(merged, a.Recent...) if len(merged) > maxRecentActions { diff --git a/internal/domain/curator/registry.go b/internal/domain/curator/registry.go index 8fcf18c6a..065115a21 100644 --- a/internal/domain/curator/registry.go +++ b/internal/domain/curator/registry.go @@ -272,8 +272,8 @@ func (r *Registry) Stop(ctx context.Context) error { r.mu.Unlock() var errs []error - for i := len(order) - 1; i >= 0; i-- { - name := order[i] + for _, name := range slices.Backward(order) { + r.mu.Lock() st, ok := r.status[name] r.mu.Unlock() diff --git a/internal/domain/workflow/tdd.go b/internal/domain/workflow/tdd.go index cf1af9cd6..c90bf2041 100644 --- a/internal/domain/workflow/tdd.go +++ b/internal/domain/workflow/tdd.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os/exec" + "slices" "strings" "github.com/samcharles93/archie-core/internal/agentexec" @@ -180,9 +181,9 @@ func testCommand(repo config.Repo) string { // testCommandArgv returns the test command argv (the last entry in // repo.Gate), or nil when no gate is configured. func testCommandArgv(repo config.Repo) []string { - for i := len(repo.Gate) - 1; i >= 0; i-- { - if len(repo.Gate[i]) > 0 { - return repo.Gate[i] + for _, v := range slices.Backward(repo.Gate) { + if len(v) > 0 { + return v } } return nil diff --git a/internal/forge/gitea.go b/internal/forge/gitea.go index d28f9cbd5..231b7f192 100644 --- a/internal/forge/gitea.go +++ b/internal/forge/gitea.go @@ -45,9 +45,9 @@ func (c *GiteaClient) AcceptInvitations(ctx context.Context) error { // AssignedIssues returns open issues assigned to the given user, excluding PRs. func (c *GiteaClient) AssignedIssues(ctx context.Context, owner, repo, assignee string) ([]Issue, error) { opts := gitea.ListIssueOption{ - ListOptions: gitea.ListOptions{PageSize: 50}, - State: gitea.StateOpen, - AssignedBy: assignee, + PageSize: 50, + State: gitea.StateOpen, + AssignedBy: assignee, } var out []Issue for { @@ -76,9 +76,9 @@ func (c *GiteaClient) AssignedIssues(ctx context.Context, owner, repo, assignee // IssuesWithLabel returns open issues matching the given label, excluding PRs. func (c *GiteaClient) IssuesWithLabel(ctx context.Context, owner, repo, label string) ([]Issue, error) { opts := gitea.ListIssueOption{ - ListOptions: gitea.ListOptions{PageSize: 50}, - State: gitea.StateOpen, - Labels: []string{label}, + PageSize: 50, + State: gitea.StateOpen, + Labels: []string{label}, } var out []Issue for { @@ -117,7 +117,7 @@ func (c *GiteaClient) Comment(ctx context.Context, owner, repo string, number in // by exclude. func (c *GiteaClient) RepliesAfter(ctx context.Context, owner, repo string, number int, afterID int64, exclude string) ([]Reply, error) { comments, _, err := c.cli.ListIssueComments(owner, repo, int64(number), gitea.ListIssueCommentOptions{ - ListOptions: gitea.ListOptions{PageSize: 50}, + PageSize: 50, }) if err != nil { return nil, err @@ -247,7 +247,7 @@ func (c *GiteaClient) SetStateLabel(ctx context.Context, owner, repo string, num // resolveLabelID returns the ID of a label, creating it if necessary. func (c *GiteaClient) resolveLabelID(owner, repo, name string) int64 { - labels, _, err := c.cli.ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: gitea.ListOptions{PageSize: 100}}) + labels, _, err := c.cli.ListRepoLabels(owner, repo, gitea.ListLabelsOptions{PageSize: 100}) if err != nil { c.log.Warn("list repo labels failed", "err", err) return 0 @@ -260,7 +260,7 @@ func (c *GiteaClient) resolveLabelID(owner, repo, name string) int64 { // Create the label. c.ensureLabel(owner, repo, name) // Re-list to get the new ID. - labels, _, err = c.cli.ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: gitea.ListOptions{PageSize: 100}}) + labels, _, err = c.cli.ListRepoLabels(owner, repo, gitea.ListLabelsOptions{PageSize: 100}) if err != nil { return 0 } diff --git a/internal/forge/github.go b/internal/forge/github.go index c5fb6c094..e5b8895ec 100644 --- a/internal/forge/github.go +++ b/internal/forge/github.go @@ -152,7 +152,7 @@ type Reply struct { // not written by exclude (the bot) -- the human side of waiting_human. func (c *GitHubClient) RepliesAfter(ctx context.Context, owner, repo string, number int, afterID int64, exclude string) ([]Reply, error) { comments, _, err := c.gh.Issues.ListComments(ctx, owner, repo, number, - &github.IssueListCommentsOptions{ListOptions: github.ListOptions{PerPage: 50}}) + &github.IssueListCommentsOptions{PerPage: 50}) if err != nil { return nil, err } diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index 5e668cd24..9da3c759a 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -36,10 +36,8 @@ func TestRouteStatusWithTasks(t *testing.T) { func TestRouteStatusIncludesActiveProviderAndModel(t *testing.T) { manager := &fakeProviderModelManager{ - fakeModelManager: fakeModelManager{ - models: []string{"openai/gpt-5.6", "openrouter/openai/gpt-5.6"}, - activeModel: "openai/gpt-5.6", - }, + models: []string{"openai/gpt-5.6", "openrouter/openai/gpt-5.6"}, + activeModel: "openai/gpt-5.6", } r := NewRouter(&fakeStore{counts: map[string]int{"running": 1}}, nil, "test") r.Models = manager @@ -99,9 +97,7 @@ func TestFormatStatus(t *testing.T) { name: "example from specification - single parked task with provider and model", counts: map[string]int{"parked": 2}, models: &fakeProviderModelManager{ - fakeModelManager: fakeModelManager{ - activeModel: "openai/gpt-5.6-luna", - }, + activeModel: "openai/gpt-5.6-luna", }, want: "šŸ“Š Archie status\n\nTasks\nāø Parked: 2\n\nRuntime\nProvider: OpenAI\nModel: openai/gpt-5.6-luna", }, @@ -120,9 +116,7 @@ func TestFormatStatus(t *testing.T) { "custom_state": 10, }, models: &fakeProviderModelManager{ - fakeModelManager: fakeModelManager{ - activeModel: "deepseek/deepseek-v4-pro", - }, + activeModel: "deepseek/deepseek-v4-pro", }, want: "šŸ“Š Archie status\n\nTasks\nā–¶ Running: 1\nšŸ‘¤ Waiting: 2\nāø Parked: 3\nā³ Queued: 4\nšŸ”€ PR open: 5\nāœ… Merged: 6\nāŒ Rejected: 7\n🚫 Declined: 8\nšŸ›‘ Dead: 9\n• Custom state: 10\n\nRuntime\nProvider: DeepSeek\nModel: deepseek/deepseek-v4-pro", }, @@ -158,11 +152,7 @@ func TestFormatStatus(t *testing.T) { name: "custom display namer interface", counts: map[string]int{"running": 1}, models: &fakeCustomDisplayNamerManager{ - fakeProviderModelManager: fakeProviderModelManager{ - fakeModelManager: fakeModelManager{ - activeModel: "openai/gpt-5.6", - }, - }, + activeModel: "openai/gpt-5.6", }, want: "šŸ“Š Archie status\n\nTasks\nā–¶ Running: 1\n\nRuntime\nProvider: OpenAI Custom Enterprise\nModel: openai/gpt-5.6", }, @@ -170,9 +160,7 @@ func TestFormatStatus(t *testing.T) { name: "provider set but model empty", counts: map[string]int{"queued": 1}, models: &fakeProviderModelManager{ - fakeModelManager: fakeModelManager{ - activeModel: "", - }, + activeModel: "", }, want: "šŸ“Š Archie status\n\nTasks\nā³ Queued: 1\n\nRuntime\nNot configured", }, diff --git a/internal/indexing/codesearch.go b/internal/indexing/codesearch.go index e41567818..adf8962b7 100644 --- a/internal/indexing/codesearch.go +++ b/internal/indexing/codesearch.go @@ -415,8 +415,7 @@ func runHelper(ctx context.Context, args ...string) ([]byte, error) { cmd := exec.CommandContext(ctx, executable, append([]string{"workspace-codesearch"}, args...)...) out, err := cmd.Output() if err != nil { - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { return nil, fmt.Errorf("codesearch helper: %s", strings.TrimSpace(string(exitErr.Stderr))) } return nil, fmt.Errorf("run codesearch helper: %w", err) diff --git a/internal/memory/manager.go b/internal/memory/manager.go index 9ca016b80..d1efa1d61 100644 --- a/internal/memory/manager.go +++ b/internal/memory/manager.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "runtime/debug" + "slices" "strings" "sync" "sync/atomic" @@ -716,9 +717,7 @@ func (m *Manager) ShutdownContext(ctx context.Context) error { // Reverse the order: external (index 1) before builtin (index 0). var firstErr error - for i := len(providers) - 1; i >= 0; i-- { - p := providers[i] - + for _, p := range slices.Backward(providers) { // Give each provider its own deadline from the parent context. if sp, ok := p.(ShutdownProvider); ok { if err := sp.Shutdown(); err != nil && firstErr == nil { diff --git a/internal/tools/builtin/truncate.go b/internal/tools/builtin/truncate.go index fca379fb9..ee2cec255 100644 --- a/internal/tools/builtin/truncate.go +++ b/internal/tools/builtin/truncate.go @@ -11,6 +11,7 @@ package builtin import ( "fmt" + "slices" "strings" "time" ) @@ -116,8 +117,8 @@ func TruncateTail(content string, maxLines, maxBytes int) TruncationResult { byteCount := 0 lineCount := 0 - for i := len(lines) - 1; i >= 0; i-- { - lineLen := len(lines[i]) + for i, line := range slices.Backward(lines) { + lineLen := len(line) if i < len(lines)-1 { lineLen++ // account for the \n } diff --git a/internal/tools/provider/provider.go b/internal/tools/provider/provider.go index 2a80f7d1c..7d3f9bd09 100644 --- a/internal/tools/provider/provider.go +++ b/internal/tools/provider/provider.go @@ -342,8 +342,8 @@ func (r *Registry) Stop(ctx context.Context) error { var errs []error var failed []runningProvider - for i := len(running) - 1; i >= 0; i-- { - current := running[i] + for _, current := range slices.Backward(running) { + r.index.Unregister(current.toolNames...) if err := safeStop(ctx, current.registration.engine); err != nil { failed = append(failed, current) @@ -495,8 +495,8 @@ func rollback( ) ([]runningProvider, error) { var errs []error var failed []runningProvider - for i := len(started) - 1; i >= 0; i-- { - current := started[i] + for _, current := range slices.Backward(started) { + index.Unregister(current.toolNames...) if err := safeStop(ctx, current.registration.engine); err != nil { failed = append(failed, current) diff --git a/internal/webui/api_tasks.go b/internal/webui/api_tasks.go index a6faf563c..b45bb8f38 100644 --- a/internal/webui/api_tasks.go +++ b/internal/webui/api_tasks.go @@ -185,8 +185,7 @@ func decodeTaskAction(w http.ResponseWriter, r *http.Request) (taskstate.Action, decoder := json.NewDecoder(r.Body) decoder.DisallowUnknownFields() if err := decoder.Decode(&req); err != nil { - var tooLarge *http.MaxBytesError - if errors.As(err, &tooLarge) { + if _, ok := errors.AsType[*http.MaxBytesError](err); ok { http.Error(w, "action body too large", http.StatusRequestEntityTooLarge) return "", false } diff --git a/internal/webui/api_workflows.go b/internal/webui/api_workflows.go index dba56ba71..7e4c09111 100644 --- a/internal/webui/api_workflows.go +++ b/internal/webui/api_workflows.go @@ -54,8 +54,7 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) decoder.DisallowUnknownFields() if err := decoder.Decode(&request); err != nil { - var tooLarge *http.MaxBytesError - if errors.As(err, &tooLarge) { + if _, ok := errors.AsType[*http.MaxBytesError](err); ok { http.Error(w, "work request body too large", http.StatusRequestEntityTooLarge) return } diff --git a/internal/worktree/worktree_repair_test.go b/internal/worktree/worktree_repair_test.go index 394ead235..3378833dd 100644 --- a/internal/worktree/worktree_repair_test.go +++ b/internal/worktree/worktree_repair_test.go @@ -398,7 +398,7 @@ func TestBranchNamingPrecedence(t *testing.T) { t.Errorf("archieBranch(%d, %q, %q) = %q, want %q", tc.issue, tc.title, tc.labels, got, tc.want) } gotPrefix := branchPrefix(tc.title, tc.labels) - wantPrefix := strings.Split(tc.want, "/")[0] + wantPrefix, _, _ := strings.Cut(tc.want, "/") if gotPrefix != wantPrefix { t.Errorf("branchPrefix(%q, %q) = %q, want %q", tc.title, tc.labels, gotPrefix, wantPrefix) }