Skip to content

Fix Git sync when server-spawned Git needs SSH or credential-helper env#310

Closed
ron-ulitsky wants to merge 2 commits into
inkeep:mainfrom
ron-ulitsky:fix-preserve-git-ssh-env
Closed

Fix Git sync when server-spawned Git needs SSH or credential-helper env#310
ron-ulitsky wants to merge 2 commits into
inkeep:mainfrom
ron-ulitsky:fix-preserve-git-ssh-env

Conversation

@ron-ulitsky

Copy link
Copy Markdown

Git auto-sync now preserves the environment Git needs to authenticate when the server fetches or pushes a repository.

The server previously spawned Git with a small environment. That worked for token-backed HTTPS paths, but it broke remotes that depend on the user's home directory, SSH agent, or credential-helper environment. On Windows with an SSH remote, ok sync and editor sync could fail with:

fatal: Could not read from remote repository.

The same git fetch or git push worked from a terminal.

This change keeps the user/home/session variables Git uses to locate SSH keys, SSH agents, and credential helpers. Commit author/committer overrides now merge into that preserved environment, so auto-save commits do not drop auth state before push.

GIT_TERMINAL_PROMPT=0 and locale-stable stderr behavior stay unchanged. GIT_SSH_COMMAND still does not pass through without simple-git's explicit unsafe opt-in.

Testing:

  • npx -y bun@1.3.13 test packages/server/src/git-handle.test.ts
  • git diff --check
  • Fresh Linux container clone of this branch:
    • bun install --frozen-lockfile
    • bun run lint

Manual verification on Windows:

  • reproduced the failure with a stripped server-style Git environment
  • rebuilt this branch and verified ok sync --json succeeds against an SSH remote
  • confirmed sync returns to idle with consecutiveFailures: 0

Notes:

  • bun run check in a fresh Linux container hit existing upstream failures. The same sync-engine.test.ts failures reproduce on inkeep/open-knowledge@main, so they are not introduced by this branch.

@CLAassistant

CLAassistant commented Jun 26, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown

I could not sync this public PR into agents-private automatically.

Patch application failed. The diff could not be applied cleanly. Try rebasing your PR on the latest base branch.

Falling back to direct application...
Applied patch to 'public/open-knowledge/packages/server/src/git-handle.test.ts' cleanly.
Applied patch to 'public/open-knowledge/packages/server/src/git-handle.ts' with conflicts.
Applied patch to 'public/open-knowledge/packages/server/src/sync-engine.ts' with conflicts.
U public/open-knowledge/packages/server/src/git-handle.ts
U public/open-knowledge/packages/server/src/sync-engine.ts

@nick-inkeep

nick-inkeep commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Thanks @ron-ulitsky, first contributor. We'll take a look at this asap and also check the PR bridge (context: we sync this to a private monorepo that has all projects for our company).

@miles-kt-inkeep

Copy link
Copy Markdown
Contributor

Hey @ron-ulitsky, thanks for the PR a fix for this will be included in the next release

inkeep-oss-sync Bot pushed a commit that referenced this pull request Jun 26, 2026
…(#2197)

* fix(open-knowledge): preserve git SSH and credential-helper env for server sync

Reproduces #310 by Ron Ulitsky against current main.

Server-spawned git rebuilt a minimal environment that dropped HOME,
SSH_AUTH_SOCK, and the Windows home variables, so SSH remotes and
credential helpers could not authenticate (most visibly Windows with an
SSH remote). buildGitEnv now preserves the user, home, and SSH auth vars.
applyGitEnv merges commit author and committer overrides into that
preserved env instead of replacing it, so auth state survives into the
subsequent fetch and push.

* fix(open-knowledge): pin commit.gpgsign and core.autocrlf off for sync git

Preserving HOME (so SSH keys and credential helpers resolve) also lets
server-spawned git read the user's global ~/.gitconfig. Two of its
directives are unsafe on the unattended sync path:

- commit.gpgsign: the merge-resolution git commit would try to GPG-sign
  with no TTY. Git aborts the commit on sign failure rather than writing
  it unsigned, so a cache-cold sync tick fails; a success would also sign
  a bot-authored commit with the user's key.
- core.autocrlf: would rewrite content line endings on checkout and merge,
  fighting OK's byte-exact LF round-trip and churning the file watcher to
  CRDT path.

Pin both off via -c at the createGitInstance level so every server-spawned
git command inherits them. -c outranks global config, and the user's own
terminal and IDE git stay untouched.

* docs(open-knowledge): drop stale no-gitconfig note from buildGitEnv

The closing JSDoc sentence claimed server-spawned git must NOT inherit
~/.gitconfig and that the env strip stays minimal. Both are false since
buildGitEnv now preserves HOME and the user/SSH auth vars, and the three
hazards it listed are handled elsewhere (commit.gpgsign and core.autocrlf
pinned off in createGitInstance; url.insteadOf intentionally honored).

* docs(open-knowledge): address review feedback on git-handle JSDoc and tests

Cloud review (no blocking issues) flagged JSDoc this PR's own changes made
stale, plus two polish items:

- buildGitEnv: "Four vars must survive" was wrong once the auth-env allowlist
  (HOME, SSH_AUTH_SOCK, Windows home vars) started surviving too; added the bullet.
- RelayGhToken: dropped the "no HOME" framing (HOME is preserved now). The relay
  still exists because OK's helper has no gh shell-out and server-side resolution
  guarantees the gh tier regardless of the curated env.
- Dropped change-narration "now" from the gpgsign/autocrlf comment.
- Added JSDoc to applyGitEnv (merges into handle.env; .env() mutates in place).
- Consolidated the test env-isolation helpers into one module-scoped
  withEnvEntries; removed withEnv and the manual save/restore.

GitOrigin-RevId: d74508c98ab30136d4240c144e2cabab99c80485
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants