refactor: go modernisation fixes

This commit is contained in:
sam committed 2026-08-29 00:20:05 +10:00
1 parent 8e4923991d
commit b2da85ead1
14 files changed
+41 -53

No files matched your search

+3 -2
View File
@@ -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()
}
}
+3 -2
View File
@@ -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 {
+2 -2
View File
@@ -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()
+4 -3
View File
@@ -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
+9 -9
View File
@@ -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
}
+1 -1
View File
@@ -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
}
+6 -18
View File
@@ -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",
},
+1 -2
View File
@@ -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)
+2 -3
View File
@@ -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 {
+3 -2
View File
@@ -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
}
+4 -4
View File
@@ -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)
+1 -2
View File
@@ -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
}
+1 -2
View File
@@ -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
}
+1 -1
View File
@@ -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)
}