Skip to content

fix(git-sync): root a new repo at /home/developer and verify init through the agent server (#2938) - #2943

Merged
vybe merged 2 commits into
devfrom
fix/2938-git-init-repo-root
Sep 22, 2026
Merged

vybe merged 2 commits into
devfrom
fix/2938-git-init-repo-root

Conversation

@dolho

@dolho dolho commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

POST /api/agents/{name}/git/initialize placed a new repository in /home/developer/workspace whenever that directory held anything (a template that ships files into workspace/, a data directory, …). The agent server's git router reads exactly one root — /home/developer — so the very next status poll answered git_enabled: false and Sync, Log and the git panel all reported "not enabled". The init endpoint had returned 200, because its verify (git rev-parse --git-dir) ran in the directory the backend chose and could only agree with itself.

Two changes, both in services/git_service:

  1. A new repo is always rooted at /home/developer (gitignore.NEW_REPO_ROOT). _detect_git_dir keeps git's own answer for an existing repo — a genuine legacy workspace-rooted one still resolves to workspace/ and is re-initialised in place, after which the agent-server verify in (2) refuses it with a 400, since the agent server reads only /home/developer — but the pre-_detect_git_dir picks the wrong repo root for agents with a populated workspace/ data directory #2075 _detect_git_dir_fallback content heuristic ("populated workspace/ ⇒ legacy root") is removed. A populated workspace/ is content under home the initial commit should include, not a reason to move the root.
  2. Init is verified where it is consumed. After the backend's own rev-parse, initialize_git_in_container asks the agent server's GET /api/git/status:
    • git_enabled: false → GitInitResult(success=False, error="the agent server cannot see the repository at … (#2938)") — the router already turns that into a 400 and rolls the agent_git_config row back, so the operator gets the reason instead of a green toast.
    • unreachable / non-200 / not JSON → warning, backend verdict kept (unknown is not "no"; the disagreement this guards against is deterministic, a transport blip is not).

Tests

tests/unit/test_2938_git_init_repo_root.py drives the real initialize_git_in_container end-to-end with the container exec and agent_httpx_client faked:

Negative control (same tests on origin/dev's gitignore.py + provisioning.py): 10 failed, 1 passed (the legacy-in-place case — the only one whose placement is unchanged; its verify outcome is new, see the merge-train note below). With the fix: 11 passed. test_2075_detect_git_dir.py updated for the retired fallback (12 passed). Git/compat unit suites: 757 passed, 2 skipped.

Docs

  • docs/memory/feature-flows/github-repo-initialization.md — step 1 placement, LEGACY case, Directory Detection Logic, Verify
  • docs/memory/requirements/github.md §11.2 — repo-root and verified-where-consumed rules

Out of scope

_apply_git_env_from_db's per-agent PAT gate (ent#162 class) is untouched; this PR changes only where a new repo lands and how init is confirmed.

Fixes #2938

🤖 Generated with Claude Code

…it is verified through the agent server (#2938)

`initialize_git_in_container` chose the repo root with a content probe: any
non-empty `/home/developer/workspace` was treated as a legacy repo root, so
an agent whose template ships files into `workspace/` got its brand-new
repo created there. The agent server's git router reads exactly one root,
`/home/developer`, so the next status poll answered `git_enabled: false`
and Sync / Log / the git panel all reported "not enabled" — right after
the init endpoint returned 200, because its verify ran in the directory
the backend had chosen and could only agree with itself.

- `_detect_git_dir`: a container with no repository answers
  `NEW_REPO_ROOT` (`/home/developer`) whatever `workspace/` holds; the
  pre-#2075 `_detect_git_dir_fallback` content heuristic is removed. An
  existing repo — a legacy workspace-rooted one included — is still
  wherever `git rev-parse --show-toplevel` finds it.
- `initialize_git_in_container`: after the backend's own
  `git rev-parse --git-dir`, ask the agent server's `GET /api/git/status`.
  `git_enabled: false` fails the init with a named reason (the router
  answers 400 and rolls the config row back); an unanswerable probe
  (unreachable, non-200, not JSON) warns and keeps the backend verdict.
- Tests: `test_2938_git_init_repo_root.py` drives the real init end-to-end
  with the exec and the agent-server call faked — placement with a
  populated workspace, no content probe, legacy re-init in place, and the
  three probe verdicts. Ten of eleven fail on the pre-fix code.
  `test_2075_detect_git_dir.py` updated for the retired fallback.

Fixes #2938

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@dolho dolho added priority-p1 Critical path type-bug Bug fix theme-reliability Theme: Reliability labels Sep 22, 2026
@dolho
dolho requested a review from vybe September 22, 2026 07:35
…ged" (#2943) — mechanical, per the merge-train note on the PR

The agent server's status route is a bare Path("/home/developer/.git").exists(),
so a repo git roots at workspace/ answers git_enabled: false deterministically
and the new Step 6 verify turns that into a 400 with the config rolled back.
The legacy test stubbed the agent server as git_enabled: true — a combination
production cannot produce — and the feature flow / requirements said the case
was "re-initialised in place, unchanged". Model the real answer in the test and
state the actual outcome in both docs. No code change.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vybe

vybe commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

merge-train 2026-09-22: riding this train, with one mechanical commit pushed to this branch (validated lane B: /validate-pr READY, /review NEEDS-FIX mechanical → fixed here). The fix for the reported case (populated workspace/, no repo → root at /home/developer) is correct and proven by the negative control; nothing in src/ was changed.

What the pushed commit does — the legacy case was described as "re-initialised in place, unchanged", but the code refuses it. The agent server's GET /api/git/status is a bare Path("/home/developer/.git").exists() (docker/base-image/agent_server/routers/git.py:1342-1350, no walk-up), so a repo git roots at workspace/ answers git_enabled: false deterministically, and your new Step 6 (provisioning.py:581-590) turns that into success=False → router 400 + delete_git_config. That is strictly better than dev (200, then "not enabled" forever), but the test test_an_existing_workspace_rooted_repo_is_still_reinitialised_in_place stubbed the agent server as git_enabled: true — a combination production cannot produce — and the feature flow + requirements/github.md + PR body said "unchanged". The commit makes the test model the real answer (renamed …_reinitialised_in_place_then_refused, asserts the in-place re-init still happens and the named 400), and states the actual outcome in both docs and the PR body. 23/23 pass locally.

For you to consider as a follow-up (a design choice, so not done here): the refusal fires after the GitHub repo is created and main + the working branch are pushed, so a legacy agent's operator gets an orphan trinity/<agent>/<id> branch per retry. Hoisting if git_dir != gitignore.NEW_REPO_ROOT: return GitInitResult(success=False, …) to right after Step 1 would fail that case before any side effect — or you may prefer migrating the repo to home. Either way it is your call. Also github-repo-initialization.md still has a few Using workspace directory: log strings in older sections that no longer match the [LEGACY] log line.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge-train: batch validated on train/20260922-1340 (train PR #2954, full suite green)

@vybe
vybe merged commit eee285b into dev Sep 22, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority-p1 Critical path theme-reliability Theme: Reliability type-bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants