From 125af85692fb235f85224cadc5cbe137d93fec74 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 11 Oct 2026 10:59:18 +0000 Subject: [PATCH 1/6] feat(git): fail-fast SSH and HTTP env for network fetch Extend PrepareNetworkGit with batch-mode SSH (when GIT_SSH_COMMAND and core.sshCommand are unset) and HTTP low-speed limits when unset. Document fetch_timeout_sec, --fetch-timeout, and GIT_RAIN_GLOBAL_FETCH_TIMEOUT_SEC. Co-authored-by: Ben Schellenberger --- AGENTS.md | 2 + README.md | 8 ++++ internal/git/command.go | 5 ++- internal/git/command_test.go | 83 ++++++++++++++++++++++++++++++++++++ internal/git/network_env.go | 56 ++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 internal/git/network_env.go diff --git a/AGENTS.md b/AGENTS.md index 0e39b7c..ceae3ed 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -35,4 +35,6 @@ These notes apply to humans and to automated agents (including Cloud Agents). - **`make test-race` is slow (~60–75s)** because `internal/git` tests shell out to the real `git` binary. This is expected, not a hang. - **Running the CLI mutates user state.** It persists a registry at `~/.config/git-rain/repos.toml` and config at `~/.config/git-rain/config.toml`, so repos discovered in one run are remembered on the next. For isolated runs/tests, always pass `--path ` (and `--dry-run` to preview without touching git). - **Manual end-to-end check:** create a bare remote + a clone under a scan dir, advance the remote, then `./git-rain --sync --path ` fast-forwards the local branch. Requires `git user.name`/`user.email` to be set (already configured here). +- **Network fetch timeouts:** `global.fetch_timeout_sec` (default `120`, `0` disables), `--fetch-timeout`, and `GIT_RAIN_GLOBAL_FETCH_TIMEOUT_SEC` bound per-repo `git fetch` via `RunNetworkGit`. Stalled remotes should surface `timed out after Ns` and not block the run summary. +- **Interrupts:** live runs use `signal.NotifyContext` (SIGINT/SIGTERM) so Ctrl-C cancels in-flight network git and prints a partial summary with exit code `1`. diff --git a/README.md b/README.md index ba3bf05..052b70a 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,7 @@ git-rain --init | `--prune` | Pass `--prune` on fetch for this run (highest precedence; cannot combine with `--no-prune`) | | `--no-prune` | Never pass `--prune` on fetch for this run (overrides `--prune`, config, registry, and `rain.fetchprune`) | | `--tags` | Also pass `--tags` on fetch operations | +| `--fetch-timeout ` | Per-fetch network deadline in seconds (`0` = disabled; default from `global.fetch_timeout_sec`) | | `--path ` | Scan path override (default: config `global.scan_path`) | | `--no-scan` | Skip filesystem scan; hydrate only known registry repos | | `--risky` | Allow destructive local branch realignment after creating backup refs | @@ -302,6 +303,7 @@ scan_path = "/home/you/projects" # root to discover repos under scan_depth = 5 # max directory depth (default in app: 10) scan_workers = 8 # parallel scan workers fetch_workers = 4 # parallel per-repo operations (default in app: 4) +fetch_timeout_sec = 120 # per-fetch network deadline (0 = no timeout) risky_mode = false # allow destructive realignment on full hydration path branch_mode = "mainline" # full hydration: mainline | checked-out | all-local | all-branches fetch_prune = false # pass --prune on fetch when true (default off; see README warning) @@ -325,8 +327,14 @@ All options can be overridden with environment variables using the `GIT_RAIN_` p ```bash GIT_RAIN_GLOBAL_RISKY_MODE=true git-rain GIT_RAIN_GLOBAL_SCAN_PATH=/tmp/repos git-rain +GIT_RAIN_GLOBAL_FETCH_TIMEOUT_SEC=60 git-rain ``` +Network fetches set `GIT_TERMINAL_PROMPT=0` and, unless you already configured +`GIT_SSH_COMMAND`, `core.sshCommand`, or the HTTP low-speed variables, add +batch-mode SSH (`BatchMode=yes`, `ConnectTimeout=30`) and HTTP stall detection +(`GIT_HTTP_LOW_SPEED_LIMIT` / `GIT_HTTP_LOW_SPEED_TIME`). + ### Garden mode tuning (advanced) Picking `rain_animation_mode = "garden"` swaps the rain background for a diff --git a/internal/git/command.go b/internal/git/command.go index 2808601..79778c2 100644 --- a/internal/git/command.go +++ b/internal/git/command.go @@ -7,7 +7,10 @@ import ( ) // PrepareNetworkGit configures cmd for fetch/push operations that may contact -// a remote. Delegates to git-harness so credential behavior stays aligned with git-fire. +// a remote. Delegates to git-harness for GIT_TERMINAL_PROMPT=0, then adds +// fail-fast SSH and HTTP env unless the user already set GIT_SSH_COMMAND, +// GIT_HTTP_LOW_SPEED_*, or core.sshCommand in the repo. func PrepareNetworkGit(cmd *exec.Cmd) { harnessgit.PrepareNetworkGit(cmd) + applyFailFastNetworkEnv(cmd) } diff --git a/internal/git/command_test.go b/internal/git/command_test.go index 26d9cc0..da909d9 100644 --- a/internal/git/command_test.go +++ b/internal/git/command_test.go @@ -1,6 +1,7 @@ package git import ( + "os" "os/exec" "strings" "testing" @@ -16,6 +17,88 @@ func TestFetchFailureReason_TerminalPromptsDisabled(t *testing.T) { } } +func isolatedPrepareNetworkEnv(t *testing.T) []string { + return []string{ + "PATH=" + os.Getenv("PATH"), + "HOME=" + os.Getenv("HOME"), + "USER=" + os.Getenv("USER"), + } +} + +func TestPrepareNetworkGit_SetsFailFastEnv(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-default"}) + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = isolatedPrepareNetworkEnv(t) + PrepareNetworkGit(cmd) + + env := strings.Join(cmd.Env, "\n") + if !strings.Contains(env, "GIT_TERMINAL_PROMPT=0") { + t.Fatalf("expected GIT_TERMINAL_PROMPT=0 in env") + } + if !strings.Contains(env, "GIT_HTTP_LOW_SPEED_LIMIT="+defaultHTTPLowSpeedLimit) { + t.Fatalf("expected HTTP low-speed limit in env, got %q", env) + } + if !strings.Contains(env, "GIT_SSH_COMMAND=ssh -o BatchMode=yes") { + t.Fatalf("expected batch-mode GIT_SSH_COMMAND in env, got %q", env) + } +} + +func TestPrepareNetworkGit_PreservesUserGitSSHCommand(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-user-ssh"}) + t.Setenv("GIT_SSH_COMMAND", "ssh -o IdentityFile=/tmp/custom") + + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = append(isolatedPrepareNetworkEnv(t), "GIT_SSH_COMMAND=ssh -o IdentityFile=/tmp/custom") + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_SSH_COMMAND=") && !strings.Contains(e, "IdentityFile=/tmp/custom") { + t.Fatalf("clobbered user GIT_SSH_COMMAND: %s", e) + } + } +} + +func TestPrepareNetworkGit_PreservesCoreSSHCommand(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-core-ssh"}) + runGit := exec.Command("git", "-C", repo, "config", "core.sshCommand", "ssh -o IdentityFile=/tmp/from-core") + if out, err := runGit.CombinedOutput(); err != nil { + t.Fatalf("git config core.sshCommand: %v (%s)", err, out) + } + + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = isolatedPrepareNetworkEnv(t) + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_SSH_COMMAND=") { + t.Fatalf("should not set GIT_SSH_COMMAND when core.sshCommand is configured, got %s", e) + } + } +} + +func TestPrepareNetworkGit_PreservesUserHTTPLowSpeed(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-http"}) + t.Setenv("GIT_HTTP_LOW_SPEED_LIMIT", "42") + t.Setenv("GIT_HTTP_LOW_SPEED_TIME", "99") + + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = append(isolatedPrepareNetworkEnv(t), + "GIT_HTTP_LOW_SPEED_LIMIT=42", + "GIT_HTTP_LOW_SPEED_TIME=99", + ) + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_HTTP_LOW_SPEED_LIMIT=") && !strings.Contains(e, "=42") { + t.Fatalf("clobbered GIT_HTTP_LOW_SPEED_LIMIT: %s", e) + } + } +} + func TestNetworkFetch_UnauthenticatedHTTPSFailsWithoutPrompt(t *testing.T) { repo := testutil.CreateTestRepo(t, testutil.RepoOptions{ Name: "https-fetch-repo", diff --git a/internal/git/network_env.go b/internal/git/network_env.go new file mode 100644 index 0000000..4c56c22 --- /dev/null +++ b/internal/git/network_env.go @@ -0,0 +1,56 @@ +package git + +import ( + "fmt" + "os" + "os/exec" + "strings" +) + +const ( + defaultSSHConnectTimeoutSec = 30 + defaultHTTPLowSpeedLimit = "1000" + defaultHTTPLowSpeedTimeSec = "30" +) + +func applyFailFastNetworkEnv(cmd *exec.Cmd) { + env := cmd.Env + if env == nil { + env = os.Environ() + } + env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_LIMIT", defaultHTTPLowSpeedLimit) + env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_TIME", defaultHTTPLowSpeedTimeSec) + + if !envHasPrefix(env, "GIT_SSH_COMMAND=") && !repoHasCoreSSHCommand(cmd.Dir) { + sshCmd := fmt.Sprintf("ssh -o BatchMode=yes -o ConnectTimeout=%d", defaultSSHConnectTimeoutSec) + env = append(env, "GIT_SSH_COMMAND="+sshCmd) + } + cmd.Env = env +} + +func setEnvIfUnset(env []string, key, value string) []string { + prefix := key + "=" + for _, e := range env { + if strings.HasPrefix(e, prefix) { + return env + } + } + return append(env, prefix+value) +} + +func envHasPrefix(env []string, prefix string) bool { + for _, e := range env { + if strings.HasPrefix(e, prefix) { + return true + } + } + return false +} + +func repoHasCoreSSHCommand(repoDir string) bool { + if strings.TrimSpace(repoDir) == "" { + return false + } + out, err := exec.Command("git", "-C", repoDir, "config", "--get", "core.sshCommand").Output() + return err == nil && strings.TrimSpace(string(out)) != "" +} From cf9644289eb6c7618db62f3255ee5576dcea3952 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 11 Oct 2026 11:10:01 +0000 Subject: [PATCH 2/6] fix(git): classify timeout and cancel errors after process kill Co-authored-by: Ben Schellenberger --- internal/git/network_git.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/git/network_git.go b/internal/git/network_git.go index 00e0d92..bfcb597 100644 --- a/internal/git/network_git.go +++ b/internal/git/network_git.go @@ -67,6 +67,12 @@ func RunNetworkGit(parent context.Context, cmd *exec.Cmd, timeoutSec int) ([]byt select { case err := <-waitDone: if err != nil { + if errors.Is(runCtx.Err(), context.DeadlineExceeded) && timeoutSec > 0 { + return combined.Bytes(), &FetchTimedOutError{Seconds: timeoutSec} + } + if runCtx.Err() != nil { + return combined.Bytes(), runCtx.Err() + } return combined.Bytes(), err } return combined.Bytes(), nil From a299bb103b5c205c244e56deb6feaaaa3a8d8e39 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 11 Oct 2026 12:25:19 +0000 Subject: [PATCH 3/6] fix(git): respect GIT_SSH and isolate fail-fast env tests from global gitconfig Co-authored-by: Ben Schellenberger --- internal/git/command_test.go | 2 ++ internal/git/network_env.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/git/command_test.go b/internal/git/command_test.go index da909d9..ee86cbf 100644 --- a/internal/git/command_test.go +++ b/internal/git/command_test.go @@ -22,6 +22,8 @@ func isolatedPrepareNetworkEnv(t *testing.T) []string { "PATH=" + os.Getenv("PATH"), "HOME=" + os.Getenv("HOME"), "USER=" + os.Getenv("USER"), + "GIT_CONFIG_NOSYSTEM=1", + "GIT_CONFIG_GLOBAL=/dev/null", } } diff --git a/internal/git/network_env.go b/internal/git/network_env.go index 4c56c22..4a857ec 100644 --- a/internal/git/network_env.go +++ b/internal/git/network_env.go @@ -21,7 +21,7 @@ func applyFailFastNetworkEnv(cmd *exec.Cmd) { env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_LIMIT", defaultHTTPLowSpeedLimit) env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_TIME", defaultHTTPLowSpeedTimeSec) - if !envHasPrefix(env, "GIT_SSH_COMMAND=") && !repoHasCoreSSHCommand(cmd.Dir) { + if !envHasPrefix(env, "GIT_SSH_COMMAND=") && !envHasPrefix(env, "GIT_SSH=") && !repoHasCoreSSHCommand(cmd.Dir) { sshCmd := fmt.Sprintf("ssh -o BatchMode=yes -o ConnectTimeout=%d", defaultSSHConnectTimeoutSec) env = append(env, "GIT_SSH_COMMAND="+sshCmd) } From d9d050bc765a684f3349c168c6d92c9b005cf48f Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 11 Oct 2026 12:29:02 +0000 Subject: [PATCH 4/6] fix(cli): remove redundant newline in skipped fetch message Co-authored-by: Ben Schellenberger --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 9d43608..b16a797 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -392,7 +392,7 @@ func runRainOnRepo(reg *registry.Registry, repo git.Repository, rainOpts git.Rai if !fullSync && !rainFetchMainline { if fetchErr := fetchOnly(repoOpts.RunCtx, repo.Path, repoOpts); fetchErr != nil { if errors.Is(fetchErr, context.Canceled) { - _, _ = fmt.Fprintln(out, " ~ skipped\n") + _, _ = fmt.Fprintln(out, " ~ skipped") _, _ = fmt.Fprintln(out) return delta } From bed18ab9e26e0a67e78aaf44929edb18da74a7dd Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 11 Oct 2026 11:09:34 +0000 Subject: [PATCH 5/6] fix(git): preserve GIT_SSH and isolate fail-fast config probes Skip BatchMode GIT_SSH_COMMAND injection when GIT_SSH is set, and run core.sshCommand probes with the command env so tests can null out global gitconfig via GIT_CONFIG_GLOBAL/SYSTEM. Co-authored-by: Ben Schellenberger --- internal/git/command.go | 2 +- internal/git/command_test.go | 16 ++++++++++++++++ internal/git/network_env.go | 10 +++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/git/command.go b/internal/git/command.go index 79778c2..680223c 100644 --- a/internal/git/command.go +++ b/internal/git/command.go @@ -9,7 +9,7 @@ import ( // PrepareNetworkGit configures cmd for fetch/push operations that may contact // a remote. Delegates to git-harness for GIT_TERMINAL_PROMPT=0, then adds // fail-fast SSH and HTTP env unless the user already set GIT_SSH_COMMAND, -// GIT_HTTP_LOW_SPEED_*, or core.sshCommand in the repo. +// GIT_SSH, GIT_HTTP_LOW_SPEED_*, or core.sshCommand. func PrepareNetworkGit(cmd *exec.Cmd) { harnessgit.PrepareNetworkGit(cmd) applyFailFastNetworkEnv(cmd) diff --git a/internal/git/command_test.go b/internal/git/command_test.go index 26075d4..24cceac 100644 --- a/internal/git/command_test.go +++ b/internal/git/command_test.go @@ -20,12 +20,14 @@ func TestFetchFailureReason_TerminalPromptsDisabled(t *testing.T) { } func isolatedPrepareNetworkEnv(t *testing.T) []string { + t.Helper() return []string{ "PATH=" + os.Getenv("PATH"), "HOME=" + os.Getenv("HOME"), "USER=" + os.Getenv("USER"), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", + "GIT_CONFIG_SYSTEM=/dev/null", } } @@ -64,6 +66,20 @@ func TestPrepareNetworkGit_PreservesUserGitSSHCommand(t *testing.T) { } } +func TestPrepareNetworkGit_PreservesUserGitSSH(t *testing.T) { + repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-user-git-ssh"}) + cmd := exec.Command("git", "fetch", "--all") + cmd.Dir = repo + cmd.Env = append(isolatedPrepareNetworkEnv(t), "GIT_SSH=/usr/bin/custom-ssh") + PrepareNetworkGit(cmd) + + for _, e := range cmd.Env { + if strings.HasPrefix(e, "GIT_SSH_COMMAND=") { + t.Fatalf("should not set GIT_SSH_COMMAND when GIT_SSH is set, got %s", e) + } + } +} + func TestPrepareNetworkGit_PreservesCoreSSHCommand(t *testing.T) { repo := testutil.CreateTestRepo(t, testutil.RepoOptions{Name: "failfast-core-ssh"}) runGit := exec.Command("git", "-C", repo, "config", "core.sshCommand", "ssh -o IdentityFile=/tmp/from-core") diff --git a/internal/git/network_env.go b/internal/git/network_env.go index 4a857ec..f0d0d05 100644 --- a/internal/git/network_env.go +++ b/internal/git/network_env.go @@ -21,7 +21,9 @@ func applyFailFastNetworkEnv(cmd *exec.Cmd) { env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_LIMIT", defaultHTTPLowSpeedLimit) env = setEnvIfUnset(env, "GIT_HTTP_LOW_SPEED_TIME", defaultHTTPLowSpeedTimeSec) - if !envHasPrefix(env, "GIT_SSH_COMMAND=") && !envHasPrefix(env, "GIT_SSH=") && !repoHasCoreSSHCommand(cmd.Dir) { + // GIT_SSH_COMMAND overrides GIT_SSH; only inject when neither is set and + // core.sshCommand is unset (same preserve path as an explicit SSH wrapper). + if !envHasPrefix(env, "GIT_SSH_COMMAND=") && !envHasPrefix(env, "GIT_SSH=") && !repoHasCoreSSHCommand(cmd.Dir, env) { sshCmd := fmt.Sprintf("ssh -o BatchMode=yes -o ConnectTimeout=%d", defaultSSHConnectTimeoutSec) env = append(env, "GIT_SSH_COMMAND="+sshCmd) } @@ -47,10 +49,12 @@ func envHasPrefix(env []string, prefix string) bool { return false } -func repoHasCoreSSHCommand(repoDir string) bool { +func repoHasCoreSSHCommand(repoDir string, env []string) bool { if strings.TrimSpace(repoDir) == "" { return false } - out, err := exec.Command("git", "-C", repoDir, "config", "--get", "core.sshCommand").Output() + cmd := exec.Command("git", "-C", repoDir, "config", "--get", "core.sshCommand") + cmd.Env = env + out, err := cmd.Output() return err == nil && strings.TrimSpace(string(out)) != "" } From 8f76cb474eb07a35fe1990eaf6d931cc9d154287 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 12 Oct 2026 18:04:24 +0000 Subject: [PATCH 6/6] fix: honor cmd.Env when probing core.sshCommand Pass the prepared command environment into the SSH config probe so GIT_CONFIG_* isolation (and any custom Env) is respected when deciding whether to inject fail-fast GIT_SSH_COMMAND. Co-authored-by: Ben Schellenberger --- internal/git/network_env.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/git/network_env.go b/internal/git/network_env.go index f0d0d05..0304477 100644 --- a/internal/git/network_env.go +++ b/internal/git/network_env.go @@ -53,8 +53,8 @@ func repoHasCoreSSHCommand(repoDir string, env []string) bool { if strings.TrimSpace(repoDir) == "" { return false } - cmd := exec.Command("git", "-C", repoDir, "config", "--get", "core.sshCommand") - cmd.Env = env - out, err := cmd.Output() + probe := exec.Command("git", "-C", repoDir, "config", "--get", "core.sshCommand") + probe.Env = env + out, err := probe.Output() return err == nil && strings.TrimSpace(string(out)) != "" }