All jobs

Add HTTP Timeouts to a Go CLI

Yes. An agent replaces bare http.Get calls in your Go command-line tool with a shared client that has a timeout and a context, with tests and a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent starts by finding every network call. In a CLI shaped like charmbracelet/glow, there are five: sourceFromArg in main.go fetches an http or https URL with http.Get(u.String()), and github.go and gitlab.go each make two more, one to the host's API (https://api.github.com/repos/<owner>/<repo>/readme, https://gitlab.com/api/v4/projects/<path>) and one to the raw README it names. All five carry a //nolint: noctx comment, and three carry gosec too, because .golangci.yml enables noctx, gosec and bodyclose. http.Get uses http.DefaultClient, which has no timeout, so a README fetch that stalls hangs the terminal with no way out but Ctrl-C.

It then designs one client for all of them. A package-level httpClient in a new http.go with Timeout set, plus a fetch(ctx, url) helper that builds the request with http.NewRequestWithContext, sets a User-Agent naming the tool and its Version variable, and returns the response. The context comes from cmd.Context(), which cobra sets, so the root command's Execute is switched to ExecuteContext with a context cancelled on os.Interrupt through signal.NotifyContext. That makes the noctx and gosec nolint comments unnecessary, and the agent deletes them rather than leaving dead suppressions behind. A new --timeout flag, bound to viper beside the existing pager, style and width bindings in init(), lets a user change the default, and the defaultConfig string in config_cmd.go gains a matching line.

The two README finders get a second fix while they are open. In github.go, io.ReadAll(res.Body) runs before res.StatusCode is checked, so a 404 body is parsed as JSON and the real error is lost; the agent checks the status first and closes the API response body itself, since only the raw-content response is handed to the caller through source. The bodyclose suppressions then become true for the one body that must stay open, and the agent leaves a comment saying so.

Tests follow the pattern already in the repository. url_test.go skips every case with t.Skip("test uses network, sometimes fails for no reason"), which is exactly what a timeout test must not do, so the agent uses httptest.NewServer with a handler that sleeps longer than a short timeout and asserts a context.DeadlineExceeded error, plus a handler that answers 404 to prove the status check. It runs go build ./..., go test ./... and golangci-lint run with the repository's own config, the same commands the Taskfile's lint and test tasks and the lint workflow run.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add HTTP timeouts and context cancellation to this Go CLI. Read go.mod, main.go, url.go, github.go, gitlab.go, url_test.go, glow_test.go, config_cmd.go, .golangci.yml, Taskfile.yaml and the workflows under .github/workflows first, and list every http.Get call with the nolint comments on it. Add one package-level http.Client with a timeout in a new http.go and a fetch helper that uses http.NewRequestWithContext and sets a User-Agent with the tool's Version. Thread the context from cobra: switch Execute to ExecuteContext with a context cancelled by signal.NotifyContext on os.Interrupt. Add a --timeout flag bound through viper like the other flags and a line for it in defaultConfig. In github.go and gitlab.go, check the API response status before reading its body and close that body; keep only the raw README body open for the caller. Remove nolint comments the change makes unnecessary. Write tests with httptest.NewServer for a slow server, a 404 and a success; do not use the network. Run go build ./..., go test ./..., go vet ./... and golangci-lint run with the repository config, then open a pull request with the test and lint output in the description.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on its own branch that adds http.go and http_test.go, changes the five call sites in main.go, github.go and gitlab.go to go through the shared client, declares --timeout beside the other flags in init(), and adds one line to the default config template. go.mod and go.sum are unchanged, which the description says in its first lines, because net/http, context, os/signal and net/http/httptest are all in the standard library.

The description lists each call site with the nolint comments it removed, quotes the go test ./... output with the new test names, and the golangci-lint result against .golangci.yml, which still enables noctx and gosec, so a reviewer can see those linters now pass without suppressions. It also records the status-before-body fix in github.go as a separate paragraph so it is reviewed on its own.

Before merging, a reviewer runs the tool against a URL that does not answer and checks that it exits with a readable timeout message, presses Ctrl-C during a fetch and checks that the exit is immediate, and decides whether the default timeout belongs in the README's Usage section. A follow-up on the same session can add it without starting over.

Run it on every evening you want the fix ready by morning

This is a change an engineer knows is needed and never gets to: a POST /o/{org}/sessions with this prompt and the repository in resources, sent from the terminal at the end of the day. The run continues in its own sandbox after the laptop closes, and the pull request is there to read in the morning. The sandbox parks with its disk when the turn ends, so a follow-up such as also honour HTTPS_PROXY and NO_PROXY from the environment wakes it with the branch and the module cache already in place.

With a webhook URL on the API key, Rush posts run.completed or run.failed with the session_url when the turn ends, so the link lands in the channel where the team reads. A team with several Go tools that fetch over HTTP can send the same prompt to each repository, one run per repository, and each run reads that tool's own call sites.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

A missing timeout only shows up when a server stalls, which no laptop reproduces on demand. The agent proves the behaviour with an httptest server that sleeps on purpose, runs the whole suite and the repository's linter in its own sandbox, and the output of every command is in the run's record before anyone reads the diff.

The record is also the argument for the design. The transcript shows the agent reading the nolint comments, choosing signal.NotifyContext over a hand-rolled channel, and checking that bodyclose is satisfied for the response it closes and still suppressed for the one it must hand on. A reviewer who wants a different default can say so in a follow-up and the parked sandbox makes the change with the tests already written.

Frequently asked questions

Will it add a dependency for the HTTP client?

No. http.Client, context, signal.NotifyContext and httptest are all in the Go standard library, so go.mod does not change. The pull request says so in its first lines so the reviewer does not have to check.

What happens to the nolint comments?

The agent removes the noctx and gosec suppressions on calls that now carry a context and a fixed URL built from a parsed value, and keeps bodyclose only on the one response whose body is returned to the caller. It runs golangci-lint with the repository's .golangci.yml to prove nothing new is reported.

How does Ctrl-C reach the fetch?

The root command runs through ExecuteContext with a context from signal.NotifyContext on os.Interrupt, and every request is built with http.NewRequestWithContext from cmd.Context(). An interrupt cancels the context, the transport abandons the connection and the tool exits with a short message instead of hanging.

Can the timeout be changed without rebuilding?

Yes. The new --timeout flag is bound through viper like pager, style and width, so it can also be set in the glow.yml config file or a GLOW_TIMEOUT environment variable, because viper.SetEnvPrefix is already called in tryLoadConfigFromDefaultPlaces. The default config template gets a commented line for it.

Do the tests use the real GitHub API?

No. The existing url_test.go skips its network cases because they sometimes fail for no reason, and the new tests avoid the problem by using httptest.NewServer for a slow server, a 404 and a success, so they run offline and in CI.

More Go CLI jobs

The same job on other stacks

More jobs like this: Hand off a long task

Keep reading